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