8dbff4747540c9b82339e52ba65c8431244ae712
[openwrt.git] / package / base-files-network / 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 set_classless_routes() {
15         local max=128
16         local type
17         while [ -n "$1" -a -n "$2" -a $max -gt 0 ]; do
18                 [ ${1##*/} -eq 32 ] && type=host || type=net
19                 echo "udhcpc: adding route for $type $1 via $2"
20                 route add -$type "$1" gw "$2" dev "$interface"
21                 max=$(($max-1))
22                 shift 2
23         done
24 }
25
26 setup_interface () {
27         local old_ip
28         local old_broadcast
29         local old_subnet
30         local old_router
31         local old_dns
32         local user_dns
33         local user_router
34         local user_metric
35
36         [ -n "$ifc" ] && {
37                 old_ip="$(uci_get_state network "$ifc" ipaddr)"
38                 old_broadcast="$(uci_get_state network "$ifc" broadcast)"
39                 old_subnet="$(uci_get_state network "$ifc" netmask)"
40         }
41
42         [ "$ip" != "$old_ip" ] \
43         || [ "${broadcast:-+}" != "$old_broadcast" ] \
44         || [ "${subnet:-255.255.255.0}" != "$old_subnet" ] && {
45                 echo "udhcpc: ifconfig $interface $ip netmask ${subnet:-255.255.255.0} broadcast ${broadcast:-+}"
46                 ifconfig $interface $ip netmask ${subnet:-255.255.255.0} broadcast ${broadcast:-+}
47
48                 change_state network "$ifc" ipaddr "$ip"
49                 change_state network "$ifc" broadcast "${broadcast:-+}"
50                 change_state network "$ifc" netmask "${subnet:-255.255.255.0}"
51         }
52
53
54         # Default Route
55         [ -n "$ifc" ] && {
56                 change_state network "$ifc" lease_gateway "$router"
57                 old_router="$(uci_get_state network "$ifc" gateway)"
58                 user_router="$(uci_get network "$ifc" gateway)"
59                 user_metric="$(uci_get network "$ifc" metric)"
60                 [ -n "$user_router" ] && router="$user_router"
61         }
62
63         [ -n "$router" ] && [ "$router" != "0.0.0.0" ] && [ "$router" != "255.255.255.255" ] && [ "$router" != "$old_router" ] && {
64                 echo "udhcpc: setting default routers: $router"
65
66                 local valid_gw=""
67                 for i in $router ; do
68                         route add default gw $i ${user_metric:+metric $user_metric} dev $interface
69                         valid_gw="${valid_gw:+$valid_gw|}$i"
70                 done
71                 
72                 eval $(route -n | awk '
73                         /^0.0.0.0\W{9}('$valid_gw')\W/ {next}
74                         /^0.0.0.0/ {print "route del -net "$1" gw "$2";"}
75                 ')
76
77                 change_state network "$ifc" gateway "$router"
78         }
79
80         # CIDR STATIC ROUTES (rfc3442)
81         [ -n "$staticroutes" ] && set_classless_routes $staticroutes
82         [ -n "$msstaticroutes" ] && set_classless_routes $msstaticroutes
83
84         # DNS
85         old_dns=$(uci_get_state network "$ifc" dns)
86         old_domain=$(uci_get_state network "$ifc" dnsdomain)
87         user_dns=$(uci_get "network.$ifc.dns")
88         [ -n "$user_dns" ] && dns="$user_dns"
89
90         [ -n "$dns" ] && [ "$dns" != "$old_dns" -o -n "$user_dns" ] && {
91                 echo "udhcpc: setting dns servers: $dns"
92                 add_dns "$ifc" $dns
93
94                 [ -n "$domain" ] && [ "$domain" != "$old_domain" ] && {
95                         echo "udhcpc: setting dns domain: $domain"
96                         sed -i -e "${old_domain:+/^search $old_domain$/d; }/^search $domain$/d" "${RESOLV_CONF}"
97                         echo "search $domain" >> "${RESOLV_CONF}"
98                         change_state network "$ifc" dnsdomain "$domain"
99                 }
100         }
101
102         [ -n "$ifc" ] || return
103
104         # UCI State
105         change_state network "$ifc" lease_server "$serverid"
106         change_state network "$ifc" lease_acquired "$(sed -ne 's![^0-9].*$!!p' /proc/uptime)"
107         change_state network "$ifc" lease_lifetime "$lease"
108         [ -n "$ntpsrv" ] &&     change_state network "$ifc" lease_ntpsrv "$ntpsrv"
109         [ -n "$timesvr" ] &&    change_state network "$ifc" lease_timesrv "$timesvr"
110         [ -n "$hostname" ] &&   change_state network "$ifc" lease_hostname "$hostname"
111         [ -n "$timezone" ] &&   change_state network "$ifc" lease_timezone "$timezone"
112
113
114         # Hotplug
115         env -i ACTION="$1" INTERFACE="$ifc" DEVICE="$ifname" PROTO=dhcp /sbin/hotplug-call iface
116 }
117
118
119 scan_interfaces
120 applied=
121 for ifc in $interfaces __default; do
122         if [ "$ifc" = __default ]; then
123                 ifc=""
124                 [ -n "$applied" ] && continue
125         else
126                 config_get ifname "$ifc" ifname
127                 [ "$ifname" = "$interface" ] || continue
128
129                 config_get proto "$ifc" proto
130                 [ "$proto" = "dhcp" ] || continue
131                 applied=true
132         fi
133
134         case "$1" in
135                 deconfig)
136                         ifconfig "$interface" 0.0.0.0
137                         [ -n "$ifc" ] && {
138                                 env -i ACTION="ifdown" INTERFACE="$ifc" DEVICE="$ifname" PROTO=dhcp /sbin/hotplug-call iface
139                         
140                                 config_get device "$ifc" device
141                                 config_get ifname "$ifc" ifname
142                                 config_get aliases "$ifc" aliases
143                                 uci_revert_state network "$ifc"
144                                 [ -n "$device" ] && uci_set_state network "$ifc" device "$device"
145                                 [ -n "$ifname" ] && uci_set_state network "$ifc" ifname "$ifname"
146                                 [ -n "$aliases" ] && uci_set_state network "$ifc" aliases "$aliases"
147                         }
148                 ;;
149                 renew)
150                         setup_interface update
151                 ;;
152                 bound)
153                         setup_interface ifup
154                 ;;
155         esac
156 done
157
158 # user rules
159 [ -f /etc/udhcpc.user ] && . /etc/udhcpc.user
160
161 exit 0