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