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