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 cfgfile
="${1:-network}"
57 config_set
"$2" auto
1
60 local iftype ifname device proto
61 config_get iftype
"$CONFIG_SECTION" TYPE
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"
72 config_set
"$CONFIG_SECTION" ifnames
"$device"
73 config_set
"$CONFIG_SECTION" ifname br-
"$CONFIG_SECTION"
76 ( type "scan_$proto" ) >/dev
/null
2>/dev
/null
&& eval "scan_$proto '$CONFIG_SECTION'"
80 config_load
"${cfgfile}"
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##*\.}"
95 # add dns entries if they are not in resolv.conf yet
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
112 grep -qsE "^nameserver ${dns//./\\.}$" /tmp
/resolv.conf.auto ||
{
113 add
="${add:+$add }$dns"
114 echo "nameserver $dns" >> /tmp
/resolv.conf.auto
119 uci_toggle_state network
"$cfg" dns
"$add"
120 uci_toggle_state network
"$cfg" resolv_dns
"$add"
124 # remove dns entries of the given iface
129 [ -f /tmp
/resolv.conf.auto
] && {
130 local dns
=$
(uci_get_state network
"$cfg" resolv_dns
)
132 sed -i -e "/^nameserver ${dns//./\\.}$/d" /tmp
/resolv.conf.auto
136 uci_revert_state network
"$cfg" dns
137 uci_revert_state network
"$cfg" resolv_dns
141 # sort the device list, drop duplicates
151 prepare_interface_bridge
() {
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.
159 prepare_interface
() {
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;
169 ifconfig
"$iface" 2>/dev
/null
>/dev
/null
&& {
171 config_get proto
"$config" proto
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)
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
184 [ "$proto" = none
] || ifconfig
"$iface" 0.0.0.0
186 # Apply sysctl settings
187 map_sysctls
"$config" "$iface"
190 # Setup VLAN interfaces
191 add_vlan
"$iface" && return 1
192 ifconfig
"$iface" 2>/dev
/null
>/dev
/null ||
return 0
196 config_get iftype
"$config" type
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"
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
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.
231 ifconfig
"$iface" ${macaddr:+hw ether "${macaddr}"} up
2>/dev
/null
>/dev
/null
239 set_interface_ifname
() {
244 config_get device
"$1" device
245 uci_toggle_state network
"$config" ifname
"$ifname"
246 uci_toggle_state network
"$config" device
"$device"
249 setup_interface_none
() {
250 env
-i ACTION
="ifup" INTERFACE
="$2" DEVICE
="$1" PROTO
=none
/sbin
/hotplug-call
"iface" &
253 setup_interface_static
() {
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
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
272 *:*) ip6addr
="$ip6addr/64" ;;
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
281 config_get
type "$config" TYPE
282 [ "$type" = "alias" ] && return 0
284 env
-i ACTION
="ifup" INTERFACE
="$config" DEVICE
="$iface" PROTO
=static
/sbin
/hotplug-call
"iface" &
287 setup_interface_alias
() {
293 config_get cfg
"$config" interface
294 [ "$parent" == "$cfg" ] ||
return 0
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
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" ;;
308 # layer 2 and parent is bridge: e.g. br-wan
309 2:bridge
) use_iface
="br-$cfg" ;;
311 # layer 1: e.g. eth0 or ath0
312 *) use_iface
="$p_device" ;;
317 config_get ctr
"$parent" alias_count
0
319 config_set
"$parent" alias_count
"$ctr"
323 config_get list
"$parent" aliases
324 append list
"$config"
325 config_set
"$parent" aliases
"$list"
327 use_iface
="$use_iface:$ctr"
328 set_interface_ifname
"$config" "$use_iface"
331 config_get proto
"$config" proto
"static"
334 setup_interface_static
"$use_iface" "$config"
337 echo "Unsupported type '$proto' for alias config '$config'"
349 [ -n "$config" ] ||
{
350 config
=$
(find_config
"$iface")
351 [ "$?" = 0 ] ||
return 1
354 prepare_interface
"$iface" "$config" "$vifmac" ||
return 0
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...
365 set_interface_ifname
"$config" "$iface"
367 [ -n "$proto" ] || config_get proto
"$config" proto
370 setup_interface_static
"$iface" "$config"
373 # kill running udhcpc instance
374 local pidfile
="/var/run/dhcp-${iface}.pid"
376 SERVICE_PID_FILE
="$pidfile" \
377 service_stop
/sbin
/udhcpc
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
389 [ -z "$ipaddr" ] || \
390 $DEBUG ifconfig
"$iface" "$ipaddr" ${netmask:+netmask "$netmask"}
392 # additional request options
393 local opt dhcpopts daemonize
394 for opt
in $reqopts; do
395 append dhcpopts
"-O $opt"
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"
402 append dhcpopts
"-O rootpath -R"
405 [ "$broadcast" = 1 ] && broadcast
="-O broadcast" || broadcast
=
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 \
418 setup_interface_none
"$iface" "$config"
421 if ( eval "type setup_interface_$proto" ) >/dev
/null
2>/dev
/null
; then
422 eval "setup_interface_$proto '$iface' '$config' '$proto'"
424 echo "Interface type $proto not supported."
431 stop_interface_dhcp
() {
435 config_get ifname
"$config" ifname
437 local lock
="/var/lock/dhcp-${ifname}"
438 [ -f "$lock" ] && lock
-u "$lock"
442 SERVICE_PID_FILE
="/var/run/dhcp-${ifname}.pid" \
443 service_stop
/sbin
/udhcpc
445 uci
-P /var
/state revert
"network.$config"
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
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