udhcpc: add cidr routes support (patch from #5299)
[openwrt/svn-archive/archive.git] / package / base-files / files / usr / share / udhcpc / default.script
1 #!/bin/sh
2 [ -z "$1" ] && echo "Error: should be run by udhcpc" && exit 1
3
4 . /etc/functions.sh
5 include /lib/network
6 RESOLV_CONF="/tmp/resolv.conf.auto"
7
8 change_state () {
9 uci_revert_state "$1" "$2" "$3" "$4"
10 uci_set_state "$1" "$2" "$3" "$4"
11 }
12
13 uci_get() {
14 uci -P /dev/null get "$1" 2>/dev/null
15 }
16
17 setup_interface () {
18 local old_ip
19 local old_broadcast
20 local old_subnet
21 local old_router
22 local old_dns
23 local user_dns
24 local user_router
25
26 config_get old_ip "$ifc" ipaddr
27 config_get old_broadcast "$ifc" broadcast
28 config_get old_subnet "$ifc" netmask
29
30 [ "$ip" != "$old_ip" ] \
31 || [ "${broadcast:-+}" != "$old_broadcast" ] \
32 || [ "${subnet:-255.255.255.0}" != "$old_subnet" ] && {
33 echo "udhcpc: ifconfig $interface $ip netmask ${subnet:-255.255.255.0} broadcast ${broadcast:-+}"
34 ifconfig $interface $ip netmask ${subnet:-255.255.255.0} broadcast ${broadcast:-+}
35
36 change_state network "$ifc" ipaddr "$ip"
37 change_state network "$ifc" broadcast "${broadcast:-+}"
38 change_state network "$ifc" netmask "${subnet:-255.255.255.0}"
39 }
40
41
42 # Default Route
43 change_state network "$ifc" lease_gateway "$router"
44 config_get old_router "$ifc" gateway
45 user_router=$(uci_get "network.$ifc.gateway")
46 [ -n "$user_router" ] && router="$user_router"
47
48 [ -n "$router" ] && [ "$router" != "0.0.0.0" ] && [ "$router" != "$old_router" ] && {
49 echo "udhcpc: setting default routers: $router"
50
51 local valid_gw=""
52 for i in $router ; do
53 route add default gw $i dev $interface
54 valid_gw="${valid_gw:+$valid_gw|}$i"
55 done
56
57 eval $(route -n | awk '
58 /^0.0.0.0\W{9}('$valid_gw')\W/ {next}
59 /^0.0.0.0/ {print "route del -net "$1" gw "$2";"}
60 ')
61
62 change_state network "$ifc" gateway "$router"
63 }
64
65 # CIDR STATIC ROUTES (rfc3442)
66 [ -n "$cidrroute" ] && {
67 # This defines how many CIDR Routes can be assigned so that we do not enter
68 # an endless loop on malformed data
69 MAXCIDRROUTES=24;
70 while [ ${MAXCIDRROUTES} -gt "0" ]; do
71 # Format is
72 # $MASK $NW $GW
73 # $NW == AAA.BBB.CCC.DDD
74 # $GW == EEE.FFF.CCC.DDD
75 # $MASK AAA.[BBB].[CCC].[DDD] EEE.FFF.GGG.HHH
76 # 1 2 3 4 5 6 7 8 9
77 MASK=$(echo $cidrroute | awk '{ print $1 }')
78 if [ ${MASK} = "0" ] ; then
79 # $MASK EEE.FFF.GGG.HHH
80 # 1 2 3 5 6
81 NW="0"
82 GW=$(echo $cidrroute | awk '{ print $2"."$3"."$4"."$5 }' )
83 elif [ ${MASK} -le "8" ] ; then
84 # $MASK AAA EEE.FFF.GGG.HHH
85 # 1 2 3 5 6 7
86 NW=$(echo $cidrroute | awk '{ print $2 }' )
87 GW=$(echo $cidrroute | awk '{ print $3"."$4"."$5"."$6 }' )
88 elif [ ${MASK} -le "16" ] ; then
89 # $MASK AAA.BBB EEE.FFF.GGG.HHH
90 # 1 2 3 5 6 7 8
91 NW=$(echo $cidrroute | awk '{ print $2"."$3 }' )
92 GW=$(echo $cidrroute | awk '{ print $4"."$5"."$6"."$7 }' )
93 elif [ ${MASK} -le "24" ] ; then
94 # $MASK AAA.BBB.CCC EEE.FFF.GGG.HHH
95 # 1 2 3 4 5 6 7 8
96 NW=$(echo $cidrroute | awk '{ print $2"."$3"."$4 }' )
97 GW=$(echo $cidrroute | awk '{ print $5"."$6"."$7"."$8 }' )
98
99 else
100 # $MASK AAA.BBB.CCC.DDD EEE.FFF.GGG.HHH
101 # 1 2 3 4 5 6 7 8 9
102 NW=$(echo $cidrroute | awk '{ print $2"."$3"."$4"."$5 }' )
103 GW=$(echo $cidrroute | awk '{ print $6"."$7"."$8"."$9 }' )
104 fi
105 echo [$ROUTECOUNTER] Route Network: $NW/$MASK Gateway: $GW on $interface
106
107 # TODO: Check for malformed data here to eliminate counter workaround
108 # Malformed data is: ... or xxx... or xxx.yyy.. or xxx.yyy.zzz.
109
110 [ -n "$NW" ] && [ -n "$GW" ] && {
111 route add $NW gw $GW dev $interface
112 }
113
114 # Clear the strings incase they don't get set next time around
115 if [ ${NW} = "0" ]; then
116 NW=""
117 fi
118 TMP="$MASK $NW $GW "
119 NW=""
120 GW=""
121
122 # Remove the '.' so that we can delete them from the input with sed
123 TMP=$(echo $TMP | sed "s/\./ /g")
124
125 # Remove the previous entry from cidrroute
126 cidrroute=$(echo $cidrroute | sed "s/$TMP//g")
127
128 # Add to counter
129 let ROUTECOUNTER=$ROUTECOUNTER+1;
130 let MAXCIDRROUTES=$MAXCIDRROUTES-1;
131
132 # Leave the loop if cidrroutes is empty (we've parsed everything)
133 [ ! -n "$cidrroute" ] && break
134
135 done
136
137 echo "done."
138 }
139
140 # DNS
141 config_get old_dns "$ifc" dns
142 user_dns=$(uci_get "network.$ifc.dns")
143 [ -n "$user_dns" ] && dns="$user_dns"
144
145 [ -n "$dns" ] && [ ! -s "${RESOLV_CONF}" -o "$dns" != "$old_dns" ] && {
146 echo "udhcpc: setting dns servers: $dns"
147 echo -n > "${RESOLV_CONF}.tmp"
148 for i in $dns ; do
149 echo "nameserver $i" >> "${RESOLV_CONF}.tmp"
150 done
151 ${domain:+echo search $domain} >> "${RESOLV_CONF}.tmp"
152 mv "${RESOLV_CONF}.tmp" "$RESOLV_CONF"
153
154 change_state network "$ifc" dnsdomain "$domain"
155 change_state network "$ifc" dns "$dns"
156 }
157
158
159 # UCI State
160 change_state network "$ifc" lease_server "$serverid"
161 change_state network "$ifc" lease_acquired "$(date '+%s')"
162 change_state network "$ifc" lease_lifetime "$lease"
163 [ -n "$ntpsrv" ] && change_state network "$ifc" lease_ntpsrv "$ntpsrv"
164 [ -n "$timesvr" ] && change_state network "$ifc" lease_timesrv "$timesvr"
165 [ -n "$hostname" ] && change_state network "$ifc" lease_hostname "$hostname"
166 [ -n "$timezone" ] && change_state network "$ifc" lease_timezone "$timezone"
167
168
169 # Hotplug
170 env -i ACTION="$1" INTERFACE="$ifc" DEVICE="$ifname" PROTO=dhcp /sbin/hotplug-call iface
171 }
172
173
174 scan_interfaces
175
176 for ifc in $interfaces; do
177 config_get ifname "$ifc" ifname
178 [ "$ifname" = "$interface" ] || continue
179
180 config_get proto "$ifc" proto
181 [ "$proto" = "dhcp" ] || continue
182
183 case "$1" in
184 deconfig)
185 ifconfig "$interface" 0.0.0.0
186 env -i ACTION="ifdown" INTERFACE="$ifc" DEVICE="$ifname" PROTO=dhcp /sbin/hotplug-call iface
187
188 config_get device "$ifc" device
189 config_get ifname "$ifc" ifname
190 config_get aliases "$ifc" aliases
191 uci_revert_state network "$ifc"
192 [ -n "$device" ] && uci_set_state network "$ifc" device "$device"
193 [ -n "$ifname" ] && uci_set_state network "$ifc" ifname "$ifname"
194 [ -n "$aliases" ] && uci_set_state network "$ifc" aliases "$aliases"
195 ;;
196 renew)
197 setup_interface update
198 ;;
199 bound)
200 setup_interface ifup
201 ;;
202 esac
203
204 # user rules
205 [ -f /etc/udhcpc.user ] && . /etc/udhcpc.user
206 done
207
208 exit 0