[backfire] backport r27616
[10.03/openwrt.git] / package / base-files / files / lib / network / config.sh
1 #!/bin/sh
2 # Copyright (C) 2006 OpenWrt.org
3
4 # DEBUG="echo"
5
6 do_sysctl() {
7         [ -n "$2" ] && \
8                 sysctl -n -e -w "$1=$2" >/dev/null || \
9                 sysctl -n -e "$1"
10 }
11
12 find_config() {
13         local iftype device iface ifaces ifn
14         for ifn in $interfaces; do
15                 config_get iftype "$ifn" type
16                 config_get iface "$ifn" ifname
17                 case "$iftype" in
18                         bridge) config_get ifaces "$ifn" ifnames;;
19                 esac
20                 config_get device "$ifn" device
21                 for ifc in $device $iface $ifaces; do
22                         [ ."$ifc" = ."$1" ] && {
23                                 echo "$ifn"
24                                 return 0
25                         }
26                 done
27         done
28
29         return 1;
30 }
31
32 scan_interfaces() {
33         local cfgfile="${1:-network}"
34         interfaces=
35         config_cb() {
36                 case "$1" in
37                         interface)
38                                 config_set "$2" auto 1
39                         ;;
40                 esac
41                 local iftype ifname device proto
42                 config_get iftype "$CONFIG_SECTION" TYPE
43                 case "$iftype" in
44                         interface)
45                                 append interfaces "$CONFIG_SECTION"
46                                 config_get proto "$CONFIG_SECTION" proto
47                                 config_get iftype "$CONFIG_SECTION" type
48                                 config_get ifname "$CONFIG_SECTION" ifname
49                                 config_get device "$CONFIG_SECTION" device "$ifname"
50                                 config_set "$CONFIG_SECTION" device "$device"
51                                 case "$iftype" in
52                                         bridge)
53                                                 config_set "$CONFIG_SECTION" ifnames "$device"
54                                                 config_set "$CONFIG_SECTION" ifname br-"$CONFIG_SECTION"
55                                         ;;
56                                 esac
57                                 ( type "scan_$proto" ) >/dev/null 2>/dev/null && eval "scan_$proto '$CONFIG_SECTION'"
58                         ;;
59                 esac
60         }
61         config_load "${cfgfile}"
62 }
63
64 add_vlan() {
65         local vif="${1%\.*}"
66
67         [ "$1" = "$vif" ] || ifconfig "$1" >/dev/null 2>/dev/null || {
68                 ifconfig "$vif" up 2>/dev/null >/dev/null || add_vlan "$vif"
69                 $DEBUG vconfig add "$vif" "${1##*\.}"
70                 return 0
71         }
72         return 1
73 }
74
75 # add dns entries if they are not in resolv.conf yet
76 add_dns() {
77         local cfg="$1"; shift
78
79         remove_dns "$cfg"
80
81         # We may be called by pppd's ip-up which has a nonstandard umask set.
82         # Create an empty file here and force its permission to 0644, otherwise
83         # dnsmasq will not be able to re-read the resolv.conf.auto .
84         [ ! -f /tmp/resolv.conf.auto ] && {
85                 touch /tmp/resolv.conf.auto
86                 chmod 0644 /tmp/resolv.conf.auto
87         }
88
89         local dns
90         local add
91         for dns in "$@"; do
92                 grep -qsF "nameserver $dns" /tmp/resolv.conf.auto || {
93                         add="${add:+$add }$dns"
94                         echo "nameserver $dns" >> /tmp/resolv.conf.auto
95                 }
96         done
97
98         [ -n "$cfg" ] && {
99                 uci_toggle_state network "$cfg" dns "$add"
100                 uci_toggle_state network "$cfg" resolv_dns "$add"
101         }
102 }
103
104 # remove dns entries of the given iface
105 remove_dns() {
106         local cfg="$1"
107
108         [ -n "$cfg" ] && {
109                 [ -f /tmp/resolv.conf.auto ] && {
110                         local dns=$(uci_get_state network "$cfg" resolv_dns)
111                         for dns in $dns; do
112                                 sed -i -e "/^nameserver $dns$/d" /tmp/resolv.conf.auto
113                         done
114                 }
115
116                 uci_revert_state network "$cfg" dns
117                 uci_revert_state network "$cfg" resolv_dns
118         }
119 }
120
121 # sort the device list, drop duplicates
122 sort_list() {
123         local arg="$*"
124         (
125                 for item in $arg; do
126                         echo "$item"
127                 done
128         ) | sort -u
129 }
130
131 # Create the interface, if necessary.
132 # Return status 0 indicates that the setup_interface() call should continue
133 # Return status 1 means that everything is set up already.
134
135 prepare_interface() {
136         local iface="$1"
137         local config="$2"
138         local vifmac="$3"
139
140         # if we're called for the bridge interface itself, don't bother trying
141         # to create any interfaces here. The scripts have already done that, otherwise
142         # the bridge interface wouldn't exist.
143         [ "br-$config" = "$iface" -o -e "$iface" ] && return 0;
144
145         ifconfig "$iface" 2>/dev/null >/dev/null && {
146                 local proto
147                 config_get proto "$config" proto
148
149                 # make sure the interface is removed from any existing bridge and deconfigured,
150                 # (deconfigured only if the interface is not set to proto=none)
151                 unbridge "$iface"
152                 [ "$proto" = none ] || ifconfig "$iface" 0.0.0.0
153
154                 # Change interface MAC address if requested
155                 [ -n "$vifmac" ] && {
156                         ifconfig "$iface" down
157                         ifconfig "$iface" hw ether "$vifmac" up
158                 }
159         }
160
161         # Setup VLAN interfaces
162         add_vlan "$iface" && return 1
163         ifconfig "$iface" 2>/dev/null >/dev/null || return 0
164
165         # Setup bridging
166         local iftype
167         config_get iftype "$config" type
168         case "$iftype" in
169                 bridge)
170                         local macaddr
171                         config_get macaddr "$config" macaddr
172                         [ -x /usr/sbin/brctl ] && {
173                                 ifconfig "br-$config" 2>/dev/null >/dev/null && {
174                                         local newdevs devices
175                                         config_get devices "$config" device
176                                         for dev in $(sort_list "$devices" "$iface"); do
177                                                 append newdevs "$dev"
178                                         done
179                                         uci_toggle_state network "$config" device "$newdevs"
180                                         $DEBUG ifconfig "$iface" 0.0.0.0
181                                         $DEBUG do_sysctl "net.ipv6.conf.$iface.disable_ipv6" 1
182                                         $DEBUG brctl addif "br-$config" "$iface"
183                                         # Bridge existed already. No further processing necesary
184                                 } || {
185                                         local stp
186                                         config_get_bool stp "$config" stp 0
187                                         $DEBUG brctl addbr "br-$config"
188                                         $DEBUG brctl setfd "br-$config" 0
189                                         $DEBUG ifconfig "$iface" 0.0.0.0
190                                         $DEBUG do_sysctl "net.ipv6.conf.$iface.disable_ipv6" 1
191                                         $DEBUG brctl addif "br-$config" "$iface"
192                                         $DEBUG brctl stp "br-$config" $stp
193                                         [ -z "$macaddr" -a -e "/sys/class/net/$iface/address" ] && macaddr="$(cat /sys/class/net/$iface/address)"
194                                         $DEBUG ifconfig "br-$config" ${macaddr:+hw ether "${macaddr}"} up
195                                         # Creating the bridge here will have triggered a hotplug event, which will
196                                         # result in another setup_interface() call, so we simply stop processing
197                                         # the current event at this point.
198                                 }
199                                 ifconfig "$iface" ${macaddr:+hw ether "${macaddr}"} up 2>/dev/null >/dev/null
200                                 return 1
201                         }
202                 ;;
203         esac
204         return 0
205 }
206
207 set_interface_ifname() {
208         local config="$1"
209         local ifname="$2"
210
211         local device
212         config_get device "$1" device
213         uci_toggle_state network "$config" ifname "$ifname"
214         uci_toggle_state network "$config" device "$device"
215 }
216
217 setup_interface_none() {
218         env -i ACTION="ifup" INTERFACE="$2" DEVICE="$1" PROTO=none /sbin/hotplug-call "iface" &
219 }
220
221 setup_interface_static() {
222         local iface="$1"
223         local config="$2"
224
225         local ipaddr netmask ip6addr
226         config_get ipaddr "$config" ipaddr
227         config_get netmask "$config" netmask
228         config_get ip6addr "$config" ip6addr
229         [ -z "$ipaddr" -o -z "$netmask" ] && [ -z "$ip6addr" ] && return 1
230
231         local gateway ip6gw dns bcast metric
232         config_get gateway "$config" gateway
233         config_get ip6gw "$config" ip6gw
234         config_get dns "$config" dns
235         config_get bcast "$config" broadcast
236         config_get metric "$config" metric
237
238         case "$ip6addr" in
239                 */*) ;;
240                 *:*) ip6addr="$ip6addr/64" ;;
241         esac
242
243         [ -z "$ipaddr" ] || $DEBUG ifconfig "$iface" "$ipaddr" netmask "$netmask" broadcast "${bcast:-+}"
244         [ -z "$ip6addr" ] || $DEBUG ifconfig "$iface" add "$ip6addr"
245         [ -z "$gateway" ] || $DEBUG route add default gw "$gateway" ${metric:+metric $metric} dev "$iface"
246         [ -z "$ip6gw" ] || $DEBUG route -A inet6 add default gw "$ip6gw" ${metric:+metric $metric} dev "$iface"
247         [ -z "$dns" ] || add_dns "$config" $dns
248
249         config_get type "$config" TYPE
250         [ "$type" = "alias" ] && return 0
251
252         env -i ACTION="ifup" INTERFACE="$config" DEVICE="$iface" PROTO=static /sbin/hotplug-call "iface" &
253 }
254
255 setup_interface_alias() {
256         local config="$1"
257         local parent="$2"
258         local iface="$3"
259
260         local cfg
261         config_get cfg "$config" interface
262         [ "$parent" == "$cfg" ] || return 0
263
264         # parent device and ifname
265         local p_device p_type
266         config_get p_device "$cfg" device
267         config_get p_type   "$cfg" type
268
269         # select alias ifname
270         local layer use_iface
271         config_get layer "$config" layer 2
272         case "$layer:$p_type" in
273                 # layer 3: e.g. pppoe-wan or pptp-vpn
274                 3:*)      use_iface="$iface" ;;
275
276                 # layer 2 and parent is bridge: e.g. br-wan
277                 2:bridge) use_iface="br-$cfg" ;;
278
279                 # layer 1: e.g. eth0 or ath0
280                 *)        use_iface="$p_device" ;;
281         esac
282
283         # alias counter
284         local ctr
285         config_get ctr "$parent" alias_count 0
286         ctr="$(($ctr + 1))"
287         config_set "$parent" alias_count "$ctr"
288
289         # alias list
290         local list
291         config_get list "$parent" aliases
292         append list "$config"
293         config_set "$parent" aliases "$list"
294
295         use_iface="$use_iface:$ctr"
296         set_interface_ifname "$config" "$use_iface"
297
298         local proto
299         config_get proto "$config" proto "static"
300         case "${proto}" in
301                 static)
302                         setup_interface_static "$use_iface" "$config"
303                 ;;
304                 *)
305                         echo "Unsupported type '$proto' for alias config '$config'"
306                         return 1
307                 ;;
308         esac
309 }
310
311 setup_interface() {
312         local iface="$1"
313         local config="$2"
314         local proto="$3"
315         local vifmac="$4"
316
317         [ -n "$config" ] || {
318                 config=$(find_config "$iface")
319                 [ "$?" = 0 ] || return 1
320         }
321
322         prepare_interface "$iface" "$config" "$vifmac" || return 0
323
324         [ "$iface" = "br-$config" ] && {
325                 # need to bring up the bridge and wait a second for
326                 # it to switch to the 'forwarding' state, otherwise
327                 # it will lose its routes...
328                 ifconfig "$iface" up
329                 sleep 1
330         }
331
332         # Interface settings
333         grep -qE "^ *$iface:" /proc/net/dev && {
334                 local mtu macaddr
335                 config_get mtu "$config" mtu
336                 config_get macaddr "$config" macaddr
337                 [ -n "$macaddr" ] && $DEBUG ifconfig "$iface" down
338                 $DEBUG ifconfig "$iface" ${macaddr:+hw ether "$macaddr"} ${mtu:+mtu $mtu} up
339         }
340         set_interface_ifname "$config" "$iface"
341
342         [ -n "$proto" ] || config_get proto "$config" proto
343         case "$proto" in
344                 static)
345                         setup_interface_static "$iface" "$config"
346                 ;;
347                 dhcp)
348                         # kill running udhcpc instance
349                         local pidfile="/var/run/dhcp-${iface}.pid"
350                         service_kill udhcpc "$pidfile"
351
352                         local ipaddr netmask hostname proto1 clientid vendorid broadcast
353                         config_get ipaddr "$config" ipaddr
354                         config_get netmask "$config" netmask
355                         config_get hostname "$config" hostname
356                         config_get proto1 "$config" proto
357                         config_get clientid "$config" clientid
358                         config_get vendorid "$config" vendorid
359                         config_get_bool broadcast "$config" broadcast 0
360
361                         [ -z "$ipaddr" ] || \
362                                 $DEBUG ifconfig "$iface" "$ipaddr" ${netmask:+netmask "$netmask"}
363
364                         # don't stay running in background if dhcp is not the main proto on the interface (e.g. when using pptp)
365                         local dhcpopts
366                         [ ."$proto1" != ."$proto" ] && dhcpopts="-n -q"
367                         [ "$broadcast" = 1 ] && broadcast="-O broadcast" || broadcast=
368
369                         $DEBUG eval udhcpc -t 0 -i "$iface" \
370                                 ${ipaddr:+-r $ipaddr} \
371                                 ${hostname:+-H $hostname} \
372                                 ${clientid:+-c $clientid} \
373                                 ${vendorid:+-V $vendorid} \
374                                 -b -p "$pidfile" $broadcast \
375                                 ${dhcpopts:- -O rootpath -R &}
376                 ;;
377                 none)
378                         setup_interface_none "$iface" "$config"
379                 ;;
380                 *)
381                         if ( eval "type setup_interface_$proto" ) >/dev/null 2>/dev/null; then
382                                 eval "setup_interface_$proto '$iface' '$config' '$proto'"
383                         else
384                                 echo "Interface type $proto not supported."
385                                 return 1
386                         fi
387                 ;;
388         esac
389 }
390
391 stop_interface_dhcp() {
392         local config="$1"
393
394         local ifname
395         config_get ifname "$config" ifname
396
397         local lock="/var/lock/dhcp-${ifname}"
398         [ -f "$lock" ] && lock -u "$lock"
399
400         remove_dns "$config"
401
402         local pidfile="/var/run/dhcp-${ifname}.pid"
403         service_kill udhcpc "$pidfile"
404
405         uci -P /var/state revert "network.$config"
406 }
407
408 unbridge() {
409         local dev="$1"
410         local brdev
411
412         [ -x /usr/sbin/brctl ] || return 0
413         brctl show 2>/dev/null | grep "$dev" >/dev/null && {
414                 # interface is still part of a bridge, correct that
415
416                 for brdev in $(brctl show | awk '$2 ~ /^[0-9].*\./ { print $1 }'); do
417                         brctl delif "$brdev" "$dev" 2>/dev/null >/dev/null
418                         do_sysctl "net.ipv6.conf.$dev.disable_ipv6" 0
419                 done
420         }
421 }