keep track of the network interface states (stored in uci format in /var/state/networ...
[openwrt/staging/wigyori.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 . /etc/functions.sh
4 include /lib/network
5
6 RESOLV_CONF="/tmp/resolv.conf.auto"
7
8 hotplug_event() {
9 scan_interfaces
10 for ifc in $interfaces; do
11 config_get ifname $ifc ifname
12 [ "$ifname" = "$interface" ] || continue
13
14 config_get proto $ifc proto
15 [ "$proto" = "dhcp" ] || continue
16 [ ifup = "$1" ] && {
17 uci set "/var/state/network.$ifc.ipaddr=$ip"
18 uci set "/var/state/network.$ifc.netmask=${subnet:-255.255.255.0}"
19 uci set "/var/state/network.$ifc.dnsdomain=$domain"
20 uci set "/var/state/network.$ifc.dns=$dns"
21 uci set "/var/state/network.$ifc.gateway=$router"
22 }
23 env -i ACTION="$1" INTERFACE="$ifc" DEVICE="$ifname" PROTO=dhcp /sbin/hotplug-call iface
24 done
25 }
26
27 case "$1" in
28 deconfig)
29 ifconfig $interface 0.0.0.0
30 hotplug_event ifdown
31 ;;
32 renew|bound)
33 ifconfig $interface $ip \
34 netmask ${subnet:-255.255.255.0} \
35 broadcast ${broadcast:-+}
36
37 [ -n "$router" ] && {
38 for i in $router ; do
39 echo "adding router $i"
40 route add default gw $i dev $interface
41 valid="$valid|$i"
42
43 done
44
45 echo "deleting old routes"
46 $(route -n | awk '/^0.0.0.0\W{9}('$valid')\W/ {next} /^0.0.0.0/ {print "route del -net "$1" gw "$2";"}')
47 }
48
49 [ -n "$dns" ] && {
50 echo -n > $RESOLV_CONF
51 ${domain:+echo search $domain} >> $RESOLV_CONF
52 for i in $dns ; do
53 echo "adding dns $i"
54 echo "nameserver $i" >> $RESOLV_CONF
55 done
56 }
57
58 hotplug_event ifup
59
60 # user rules
61 [ -f /etc/udhcpc.user ] && . /etc/udhcpc.user
62 ;;
63 esac
64
65 exit 0