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