udhcpc: run dhcp hotplug events after dns has been set
[openwrt-10.03/.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                         config_get userdns "$ifc" dns
18                         [ -n "$userdns" ] && {
19                                 for i in $userdns; do
20                                         echo "custom dns $i"
21                                         echo "nameserver $i" >> "${RESOLV_CONF}.tmp"
22                                 done
23                                 dns="$userdns"
24                         }
25                         uci_set_state network "$ifc" ipaddr "$ip"
26                         uci_set_state network "$ifc" netmask "${subnet:-255.255.255.0}"
27                         uci_set_state network "$ifc" dnsdomain "$domain"
28                         uci_set_state network "$ifc" dns "$dns"
29                         uci_set_state network "$ifc" gateway "$router"
30                 }
31                 
32                 env -i ACTION="$1" INTERFACE="$ifc" DEVICE="$ifname" PROTO=dhcp /sbin/hotplug-call iface
33         done
34 }
35
36 case "$1" in
37         deconfig)
38                 ifconfig $interface 0.0.0.0
39                 hotplug_event ifdown
40         ;;
41         renew|bound)
42                 ifconfig $interface $ip \
43                 netmask ${subnet:-255.255.255.0} \
44                 broadcast ${broadcast:-+}
45
46                 [ -n "$router" ] && {
47                         for i in $router ; do
48                                 echo "adding router $i"
49                                 route add default gw $i dev $interface
50                                 valid="$valid|$i"
51
52                         done
53
54                         echo "deleting old routes"
55                         $(route -n | awk '/^0.0.0.0\W{9}('$valid')\W/ {next} /^0.0.0.0/ {print "route del -net "$1" gw "$2";"}')
56                 }
57
58                 [ -n "$dns" ] && \
59                         echo -n > "${RESOLV_CONF}.tmp"
60                 
61                 [ -n "$dns" ] && {
62                         [ -s "${RESOLV_CONF}.tmp" ] || {
63                                 for i in $dns ; do
64                                         echo "adding dns $i"
65                                         echo "nameserver $i" >> "${RESOLV_CONF}.tmp"
66                                 done
67                         }
68
69                         ${domain:+echo search $domain} >> "${RESOLV_CONF}.tmp"
70
71                         mv "${RESOLV_CONF}.tmp" "$RESOLV_CONF"
72                 }
73
74                 if [ "$1" = "renew" ]; then
75                         hotplug_event update
76                 else
77                         hotplug_event ifup
78                 fi
79
80                 # user rules
81                 [ -f /etc/udhcpc.user ] && . /etc/udhcpc.user
82         ;;
83 esac
84
85 exit 0