7dfb0f93e2a4911224630a69511c1b77859b2f80
[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
66 # DNS
67 config_get old_dns "$ifc" dns
68 user_dns=$(uci_get "network.$ifc.dns")
69 [ -n "$user_dns" ] && dns="$user_dns"
70
71 [ -n "$dns" ] && [ "$dns" != "$old_dns" ] && {
72 echo "udhcpc: setting dns servers: $dns"
73 echo -n > "${RESOLV_CONF}.tmp"
74 for i in $dns ; do
75 echo "nameserver $i" >> "${RESOLV_CONF}.tmp"
76 done
77 ${domain:+echo search $domain} >> "${RESOLV_CONF}.tmp"
78 mv "${RESOLV_CONF}.tmp" "$RESOLV_CONF"
79
80 change_state network "$ifc" dnsdomain "$domain"
81 change_state network "$ifc" dns "$dns"
82 }
83
84
85 # UCI State
86 change_state network "$ifc" lease_server "$serverid"
87 change_state network "$ifc" lease_acquired "$(date '+%s')"
88 change_state network "$ifc" lease_lifetime "$lease"
89 [ -n "$ntpsrv" ] && change_state network "$ifc" lease_ntpsrv "$ntpsrv"
90 [ -n "$timesvr" ] && change_state network "$ifc" lease_timesrv "$timesvr"
91 [ -n "$hostname" ] && change_state network "$ifc" lease_hostname "$hostname"
92 [ -n "$timezone" ] && change_state network "$ifc" lease_timezone "$timezone"
93
94
95 # Hotplug
96 env -i ACTION="$1" INTERFACE="$ifc" DEVICE="$ifname" PROTO=dhcp /sbin/hotplug-call iface
97 }
98
99
100 scan_interfaces
101
102 for ifc in $interfaces; do
103 config_get ifname "$ifc" ifname
104 [ "$ifname" = "$interface" ] || continue
105
106 config_get proto "$ifc" proto
107 [ "$proto" = "dhcp" ] || continue
108
109 case "$1" in
110 deconfig)
111 ifconfig "$interface" 0.0.0.0
112 env -i ACTION="ifdown" INTERFACE="$ifc" DEVICE="$ifname" PROTO=dhcp /sbin/hotplug-call iface
113 uci_revert_state network "$ifc"
114 ;;
115 renew)
116 setup_interface update
117 ;;
118 bound)
119 setup_interface ifup
120 ;;
121 esac
122
123 # user rules
124 [ -f /etc/udhcpc.user ] && . /etc/udhcpc.user
125 done
126
127 exit 0