2 # Copyright (C) 2006-2011 OpenWrt.org
6 .
/lib
/functions
/service.sh
10 sysctl
-n -e -w "$1=$2" >/dev
/null || \
19 for fam
in ipv4 ipv6
; do
20 if [ -d /proc
/sys
/net
/$fam ]; then
22 for key
in /proc
/sys
/net
/$fam/*/$ifn/*; do
24 config_get val
"$cfg" "${fam}_${key##*/}"
25 [ -n "$val" ] && echo -n "$val" > "$key"
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
37 bridge
) config_get ifaces
"$ifn" ifnames
;;
39 config_get device
"$ifn" device
40 for ifc
in $device $iface $ifaces; do
41 [ .
"$ifc" = .
"$1" ] && {
52 local iftype ifname device proto
55 config_get iftype
"$__cfg" TYPE
58 append interfaces
"$__cfg"
59 config_get proto
"$__cfg" proto
60 config_get iftype
"$__cfg" type
61 config_get ifname
"$__cfg" ifname
62 config_get device
"$__cfg" device
"$ifname"
63 config_set
"$__cfg" device
"$device"
66 config_set
"$__cfg" ifnames
"$device"
67 config_set
"$__cfg" ifname br-
"$CONFIG_SECTION"
70 ( type "scan_$proto" ) >/dev
/null
2>/dev
/null
&& eval "scan_$proto '$__cfg'"
76 local cfgfile
="${1:-network}"
81 config_set
"$2" auto
1
84 fixup_interface
"$CONFIG_SECTION"
86 config_load
"${cfgfile}"
92 [ "$1" = "$vif" ] || ifconfig
"$1" >/dev
/null
2>/dev
/null ||
{
93 ifconfig
"$vif" up
2>/dev
/null
>/dev
/null || add_vlan
"$vif"
94 $DEBUG vconfig set_name_type DEV_PLUS_VID_NO_PAD
95 $DEBUG vconfig add
"$vif" "${1##*\.}"
101 # add dns entries if they are not in resolv.conf yet
103 local cfg
="$1"; shift
107 # We may be called by pppd's ip-up which has a nonstandard umask set.
108 # Create an empty file here and force its permission to 0644, otherwise
109 # dnsmasq will not be able to re-read the resolv.conf.auto .
110 [ ! -f /tmp
/resolv.conf.auto
] && {
111 touch /tmp
/resolv.conf.auto
112 chmod 0644 /tmp
/resolv.conf.auto
118 grep -qsE "^nameserver ${dns//./\\.}$" /tmp
/resolv.conf.auto ||
{
119 add
="${add:+$add }$dns"
120 echo "nameserver $dns" >> /tmp
/resolv.conf.auto
125 uci_toggle_state network
"$cfg" dns
"$add"
126 uci_toggle_state network
"$cfg" resolv_dns
"$add"
130 # remove dns entries of the given iface
135 [ -f /tmp
/resolv.conf.auto
] && {
136 local dns
=$
(uci_get_state network
"$cfg" resolv_dns
)
138 sed -i -e "/^nameserver ${dns//./\\.}$/d" /tmp
/resolv.conf.auto
142 uci_revert_state network
"$cfg" dns
143 uci_revert_state network
"$cfg" resolv_dns
147 # sort the device list, drop duplicates
157 prepare_interface_bridge
() {
161 # Create the interface, if necessary.
162 # Return status 0 indicates that the setup_interface() call should continue
163 # Return status 1 means that everything is set up already.
165 prepare_interface
() {
170 # if we're called for the bridge interface itself, don't bother trying
171 # to create any interfaces here. The scripts have already done that, otherwise
172 # the bridge interface wouldn't exist.
173 [ "br-$config" = "$iface" -o -e "$iface" ] && return 0;
175 ifconfig
"$iface" 2>/dev
/null
>/dev
/null
&& {
177 config_get proto
"$config" proto
179 # make sure the interface is removed from any existing bridge and deconfigured,
180 # (deconfigured only if the interface is not set to proto=none)
183 local mtu macaddr txqueuelen
184 config_get mtu
"$config" mtu
185 [ -n "$macaddr" ] || config_get macaddr
"$config" macaddr
186 config_get txqueuelen
"$config" txqueuelen
187 [ -n "$macaddr" ] && $DEBUG ifconfig
"$iface" down
188 $DEBUG ifconfig
"$iface" ${macaddr:+hw ether "$macaddr"} ${mtu:+mtu $mtu} ${txqueuelen:+txqueuelen $txqueuelen} up
190 [ "$proto" = none
] || ifconfig
"$iface" 0.0.0.0
192 # Apply sysctl settings
193 map_sysctls
"$config" "$iface"
196 # Setup VLAN interfaces
197 add_vlan
"$iface" && return 1
198 ifconfig
"$iface" 2>/dev
/null
>/dev
/null ||
return 0
202 config_get iftype
"$config" type
206 config_get macaddr
"$config" macaddr
207 [ -x /usr
/sbin
/brctl
] && {
208 ifconfig
"br-$config" 2>/dev
/null
>/dev
/null
&& {
209 local newdevs devices
210 config_get devices
"$config" device
211 for dev
in $
(sort_list
"$devices" "$iface"); do
212 append newdevs
"$dev"
214 uci_toggle_state network
"$config" device
"$newdevs"
215 $DEBUG ifconfig
"$iface" 0.0.0.0
216 $DEBUG do_sysctl
"net.ipv6.conf.$iface.disable_ipv6" 1
217 $DEBUG brctl addif
"br-$config" "$iface"
218 # Bridge existed already. No further processing necesary
220 local stp igmp_snooping
221 config_get_bool stp
"$config" stp
0
222 config_get_bool igmp_snooping
"$config" igmp_snooping
1
223 $DEBUG brctl addbr
"br-$config"
224 $DEBUG brctl setfd
"br-$config" 0
225 $DEBUG ifconfig
"$iface" 0.0.0.0
226 $DEBUG do_sysctl
"net.ipv6.conf.$iface.disable_ipv6" 1
227 $DEBUG brctl addif
"br-$config" "$iface"
228 $DEBUG brctl stp
"br-$config" $stp
229 [ -z "$macaddr" ] && macaddr
="$(cat /sys/class/net/$iface/address)"
230 [ -e /sys
/devices
/virtual
/net
/br-
$config/bridge
/multicast_snooping
] && \
231 echo $igmp_snooping > /sys
/devices
/virtual
/net
/br-
$config/bridge
/multicast_snooping
232 $DEBUG ifconfig
"br-$config" hw ether
$macaddr up
233 # Creating the bridge here will have triggered a hotplug event, which will
234 # result in another setup_interface() call, so we simply stop processing
235 # the current event at this point.
237 ifconfig
"$iface" ${macaddr:+hw ether "${macaddr}"} up
2>/dev
/null
>/dev
/null
245 set_interface_ifname
() {
250 config_get device
"$1" device
251 uci_toggle_state network
"$config" ifname
"$ifname"
252 uci_toggle_state network
"$config" device
"$device"
255 setup_interface_none
() {
256 env
-i ACTION
="ifup" INTERFACE
="$2" DEVICE
="$1" PROTO
=none
/sbin
/hotplug-call
"iface" &
259 setup_interface_static
() {
263 local ipaddr netmask ip6addr
264 config_get ipaddr
"$config" ipaddr
265 config_get netmask
"$config" netmask
266 config_get ip6addr
"$config" ip6addr
267 [ -z "$ipaddr" -o -z "$netmask" ] && [ -z "$ip6addr" ] && return 1
269 local gateway ip6gw dns bcast metric
270 config_get gateway
"$config" gateway
271 config_get ip6gw
"$config" ip6gw
272 config_get dns
"$config" dns
273 config_get bcast
"$config" broadcast
274 config_get metric
"$config" metric
278 *:*) ip6addr
="$ip6addr/64" ;;
281 [ -z "$ipaddr" ] ||
$DEBUG ifconfig
"$iface" "$ipaddr" netmask
"$netmask" broadcast
"${bcast:-+}"
282 [ -z "$ip6addr" ] ||
$DEBUG ifconfig
"${iface%:*}" add
"$ip6addr"
283 [ -z "$gateway" ] ||
$DEBUG route add default gw
"$gateway" ${metric:+metric $metric} dev
"$iface"
284 [ -z "$ip6gw" ] ||
$DEBUG route
-A inet6 add default gw
"$ip6gw" ${metric:+metric $metric} dev
"${iface%:*}"
285 [ -z "$dns" ] || add_dns
"$config" $dns
287 config_get
type "$config" TYPE
288 [ "$type" = "alias" ] && return 0
290 env
-i ACTION
="ifup" INTERFACE
="$config" DEVICE
="$iface" PROTO
=static
/sbin
/hotplug-call
"iface" &
293 setup_interface_alias
() {
299 config_get cfg
"$config" interface
300 [ "$parent" == "$cfg" ] ||
return 0
302 # parent device and ifname
303 local p_device p_type
304 config_get p_device
"$cfg" device
305 config_get p_type
"$cfg" type
307 # select alias ifname
308 local layer use_iface
309 config_get layer
"$config" layer
2
310 case "$layer:$p_type" in
311 # layer 3: e.g. pppoe-wan or pptp-vpn
312 3:*) use_iface
="$iface" ;;
314 # layer 2 and parent is bridge: e.g. br-wan
315 2:bridge
) use_iface
="br-$cfg" ;;
317 # layer 1: e.g. eth0 or ath0
318 *) use_iface
="$p_device" ;;
323 config_get ctr
"$parent" alias_count
0
325 config_set
"$parent" alias_count
"$ctr"
329 config_get list
"$parent" aliases
330 append list
"$config"
331 config_set
"$parent" aliases
"$list"
333 use_iface
="$use_iface:$ctr"
334 set_interface_ifname
"$config" "$use_iface"
337 config_get proto
"$config" proto
"static"
340 setup_interface_static
"$use_iface" "$config"
343 echo "Unsupported type '$proto' for alias config '$config'"
355 [ -n "$config" ] ||
{
356 config
=$
(find_config
"$iface")
357 [ "$?" = 0 ] ||
return 1
360 prepare_interface
"$iface" "$config" "$vifmac" ||
return 0
362 [ "$iface" = "br-$config" ] && {
363 # need to bring up the bridge and wait a second for
364 # it to switch to the 'forwarding' state, otherwise
365 # it will lose its routes...
371 set_interface_ifname
"$config" "$iface"
373 [ -n "$proto" ] || config_get proto
"$config" proto
376 setup_interface_static
"$iface" "$config"
379 # kill running udhcpc instance
380 local pidfile
="/var/run/dhcp-${iface}.pid"
382 SERVICE_PID_FILE
="$pidfile" \
383 service_stop
/sbin
/udhcpc
385 local ipaddr netmask hostname proto1 clientid vendorid broadcast reqopts
386 config_get ipaddr
"$config" ipaddr
387 config_get netmask
"$config" netmask
388 config_get hostname
"$config" hostname
389 config_get proto1
"$config" proto
390 config_get clientid
"$config" clientid
391 config_get vendorid
"$config" vendorid
392 config_get_bool broadcast
"$config" broadcast
0
393 config_get reqopts
"$config" reqopts
395 [ -z "$ipaddr" ] || \
396 $DEBUG ifconfig
"$iface" "$ipaddr" ${netmask:+netmask "$netmask"}
398 # additional request options
399 local opt dhcpopts daemonize
400 for opt
in $reqopts; do
401 append dhcpopts
"-O $opt"
404 # don't stay running in background if dhcp is not the main proto on the interface (e.g. when using pptp)
405 [ "$proto1" != "$proto" ] && {
406 append dhcpopts
"-n -q"
408 append dhcpopts
"-O rootpath -R"
411 [ "$broadcast" = 1 ] && broadcast
="-O broadcast" || broadcast
=
413 SERVICE_DAEMONIZE
=$daemonize \
414 SERVICE_PID_FILE
="$pidfile" \
415 service_start
/sbin
/udhcpc
-t 0 -i "$iface" \
416 ${ipaddr:+-r $ipaddr} \
417 ${hostname:+-H $hostname} \
418 ${clientid:+-c $clientid} \
419 ${vendorid:+-V $vendorid} \
420 -b -p "$pidfile" $broadcast \
424 setup_interface_none
"$iface" "$config"
427 if ( eval "type setup_interface_$proto" ) >/dev
/null
2>/dev
/null
; then
428 eval "setup_interface_$proto '$iface' '$config' '$proto'"
430 echo "Interface type $proto not supported."
437 stop_interface_dhcp
() {
441 config_get ifname
"$config" ifname
443 local lock
="/var/lock/dhcp-${ifname}"
444 [ -f "$lock" ] && lock
-u "$lock"
448 SERVICE_PID_FILE
="/var/run/dhcp-${ifname}.pid" \
449 service_stop
/sbin
/udhcpc
451 uci
-P /var
/state revert
"network.$config"
458 [ -x /usr
/sbin
/brctl
] ||
return 0
459 brctl show
2>/dev
/null |
grep "$dev" >/dev
/null
&& {
460 # interface is still part of a bridge, correct that
462 for brdev
in $
(brctl show |
awk '$2 ~ /^[0-9].*\./ { print $1 }'); do
463 brctl delif
"$brdev" "$dev" 2>/dev
/null
>/dev
/null
464 do_sysctl
"net.ipv6.conf.$dev.disable_ipv6" 0