2 # Copyright (C) 2006-2011 OpenWrt.org
8 sysctl
-n -e -w "$1=$2" >/dev
/null || \
17 for fam
in ipv4 ipv6
; do
18 if [ -d /proc
/sys
/net
/$fam ]; then
20 for key
in /proc
/sys
/net
/$fam/*/$ifn/*; do
22 config_get val
"$cfg" "${fam}_${key##*/}"
23 [ -n "$val" ] && echo -n "$val" > "$key"
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
35 bridge
) config_get ifaces
"$ifn" ifnames
;;
37 config_get device
"$ifn" device
38 for ifc
in $device $iface $ifaces; do
39 [ .
"$ifc" = .
"$1" ] && {
50 local cfgfile
="${1:-network}"
55 config_set
"$2" auto
1
58 local iftype ifname device proto
59 config_get iftype
"$CONFIG_SECTION" TYPE
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"
70 config_set
"$CONFIG_SECTION" ifnames
"$device"
71 config_set
"$CONFIG_SECTION" ifname br-
"$CONFIG_SECTION"
74 ( type "scan_$proto" ) >/dev
/null
2>/dev
/null
&& eval "scan_$proto '$CONFIG_SECTION'"
78 config_load
"${cfgfile}"
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##*\.}"
92 # add dns entries if they are not in resolv.conf yet
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
109 grep -qsE "^nameserver ${dns//./\\.}$" /tmp
/resolv.conf.auto ||
{
110 add
="${add:+$add }$dns"
111 echo "nameserver $dns" >> /tmp
/resolv.conf.auto
116 uci_toggle_state network
"$cfg" dns
"$add"
117 uci_toggle_state network
"$cfg" resolv_dns
"$add"
121 # remove dns entries of the given iface
126 [ -f /tmp
/resolv.conf.auto
] && {
127 local dns
=$
(uci_get_state network
"$cfg" resolv_dns
)
129 sed -i -e "/^nameserver ${dns//./\\.}$/d" /tmp
/resolv.conf.auto
133 uci_revert_state network
"$cfg" dns
134 uci_revert_state network
"$cfg" resolv_dns
138 # sort the device list, drop duplicates
148 prepare_interface_bridge
() {
152 # Create the interface, if necessary.
153 # Return status 0 indicates that the setup_interface() call should continue
154 # Return status 1 means that everything is set up already.
156 prepare_interface
() {
161 # if we're called for the bridge interface itself, don't bother trying
162 # to create any interfaces here. The scripts have already done that, otherwise
163 # the bridge interface wouldn't exist.
164 [ "br-$config" = "$iface" -o -e "$iface" ] && return 0;
166 ifconfig
"$iface" 2>/dev
/null
>/dev
/null
&& {
168 config_get proto
"$config" proto
170 # make sure the interface is removed from any existing bridge and deconfigured,
171 # (deconfigured only if the interface is not set to proto=none)
174 local mtu macaddr txqueuelen
175 config_get mtu
"$config" mtu
176 [ -n "$macaddr" ] || config_get macaddr
"$config" macaddr
177 config_get txqueuelen
"$config" txqueuelen
178 [ -n "$macaddr" ] && $DEBUG ifconfig
"$iface" down
179 $DEBUG ifconfig
"$iface" ${macaddr:+hw ether "$macaddr"} ${mtu:+mtu $mtu} ${txqueuelen:+txqueuelen $txqueuelen} up
181 [ "$proto" = none
] || ifconfig
"$iface" 0.0.0.0
183 # Apply sysctl settings
184 map_sysctls
"$config" "$iface"
187 # Setup VLAN interfaces
188 add_vlan
"$iface" && return 1
189 ifconfig
"$iface" 2>/dev
/null
>/dev
/null ||
return 0
193 config_get iftype
"$config" type
197 config_get macaddr
"$config" macaddr
198 [ -x /usr
/sbin
/brctl
] && {
199 ifconfig
"br-$config" 2>/dev
/null
>/dev
/null
&& {
200 local newdevs devices
201 config_get devices
"$config" device
202 for dev
in $
(sort_list
"$devices" "$iface"); do
203 append newdevs
"$dev"
205 uci_toggle_state network
"$config" device
"$newdevs"
206 $DEBUG ifconfig
"$iface" 0.0.0.0
207 $DEBUG do_sysctl
"net.ipv6.conf.$iface.disable_ipv6" 1
208 $DEBUG brctl addif
"br-$config" "$iface"
209 # Bridge existed already. No further processing necesary
211 local stp igmp_snooping
212 config_get_bool stp
"$config" stp
0
213 config_get_bool igmp_snooping
"$config" igmp_snooping
1
214 $DEBUG brctl addbr
"br-$config"
215 $DEBUG brctl setfd
"br-$config" 0
216 $DEBUG ifconfig
"$iface" 0.0.0.0
217 $DEBUG do_sysctl
"net.ipv6.conf.$iface.disable_ipv6" 1
218 $DEBUG brctl addif
"br-$config" "$iface"
219 $DEBUG brctl stp
"br-$config" $stp
220 [ -z "$macaddr" ] && macaddr
="$(cat /sys/class/net/$iface/address)"
221 echo $igmp_snooping > /sys
/devices
/virtual
/net
/br-
$config/bridge
/multicast_snooping
2>/dev
/null
222 $DEBUG ifconfig
"br-$config" hw ether
$macaddr up
223 # Creating the bridge here will have triggered a hotplug event, which will
224 # result in another setup_interface() call, so we simply stop processing
225 # the current event at this point.
227 ifconfig
"$iface" ${macaddr:+hw ether "${macaddr}"} up
2>/dev
/null
>/dev
/null
235 set_interface_ifname
() {
240 config_get device
"$1" device
241 uci_toggle_state network
"$config" ifname
"$ifname"
242 uci_toggle_state network
"$config" device
"$device"
245 setup_interface_none
() {
246 env
-i ACTION
="ifup" INTERFACE
="$2" DEVICE
="$1" PROTO
=none
/sbin
/hotplug-call
"iface" &
249 setup_interface_static
() {
253 local ipaddr netmask ip6addr
254 config_get ipaddr
"$config" ipaddr
255 config_get netmask
"$config" netmask
256 config_get ip6addr
"$config" ip6addr
257 [ -z "$ipaddr" -o -z "$netmask" ] && [ -z "$ip6addr" ] && return 1
259 local gateway ip6gw dns bcast metric
260 config_get gateway
"$config" gateway
261 config_get ip6gw
"$config" ip6gw
262 config_get dns
"$config" dns
263 config_get bcast
"$config" broadcast
264 config_get metric
"$config" metric
268 *:*) ip6addr
="$ip6addr/64" ;;
271 [ -z "$ipaddr" ] ||
$DEBUG ifconfig
"$iface" "$ipaddr" netmask
"$netmask" broadcast
"${bcast:-+}"
272 [ -z "$ip6addr" ] ||
$DEBUG ifconfig
"${iface%:*}" add
"$ip6addr"
273 [ -z "$gateway" ] ||
$DEBUG route add default gw
"$gateway" ${metric:+metric $metric} dev
"$iface"
274 [ -z "$ip6gw" ] ||
$DEBUG route
-A inet6 add default gw
"$ip6gw" ${metric:+metric $metric} dev
"${iface%:*}"
275 [ -z "$dns" ] || add_dns
"$config" $dns
277 config_get
type "$config" TYPE
278 [ "$type" = "alias" ] && return 0
280 env
-i ACTION
="ifup" INTERFACE
="$config" DEVICE
="$iface" PROTO
=static
/sbin
/hotplug-call
"iface" &
283 setup_interface_alias
() {
289 config_get cfg
"$config" interface
290 [ "$parent" == "$cfg" ] ||
return 0
292 # parent device and ifname
293 local p_device p_type
294 config_get p_device
"$cfg" device
295 config_get p_type
"$cfg" type
297 # select alias ifname
298 local layer use_iface
299 config_get layer
"$config" layer
2
300 case "$layer:$p_type" in
301 # layer 3: e.g. pppoe-wan or pptp-vpn
302 3:*) use_iface
="$iface" ;;
304 # layer 2 and parent is bridge: e.g. br-wan
305 2:bridge
) use_iface
="br-$cfg" ;;
307 # layer 1: e.g. eth0 or ath0
308 *) use_iface
="$p_device" ;;
313 config_get ctr
"$parent" alias_count
0
315 config_set
"$parent" alias_count
"$ctr"
319 config_get list
"$parent" aliases
320 append list
"$config"
321 config_set
"$parent" aliases
"$list"
323 use_iface
="$use_iface:$ctr"
324 set_interface_ifname
"$config" "$use_iface"
327 config_get proto
"$config" proto
"static"
330 setup_interface_static
"$use_iface" "$config"
333 echo "Unsupported type '$proto' for alias config '$config'"
345 [ -n "$config" ] ||
{
346 config
=$
(find_config
"$iface")
347 [ "$?" = 0 ] ||
return 1
350 prepare_interface
"$iface" "$config" "$vifmac" ||
return 0
352 [ "$iface" = "br-$config" ] && {
353 # need to bring up the bridge and wait a second for
354 # it to switch to the 'forwarding' state, otherwise
355 # it will lose its routes...
361 set_interface_ifname
"$config" "$iface"
363 [ -n "$proto" ] || config_get proto
"$config" proto
366 setup_interface_static
"$iface" "$config"
369 # kill running udhcpc instance
370 local pidfile
="/var/run/dhcp-${iface}.pid"
372 SERVICE_PID_FILE
="$pidfile" \
373 service_stop
/sbin
/udhcpc
375 local ipaddr netmask hostname proto1 clientid vendorid broadcast reqopts
376 config_get ipaddr
"$config" ipaddr
377 config_get netmask
"$config" netmask
378 config_get hostname
"$config" hostname
379 config_get proto1
"$config" proto
380 config_get clientid
"$config" clientid
381 config_get vendorid
"$config" vendorid
382 config_get_bool broadcast
"$config" broadcast
0
383 config_get reqopts
"$config" reqopts
385 [ -z "$ipaddr" ] || \
386 $DEBUG ifconfig
"$iface" "$ipaddr" ${netmask:+netmask "$netmask"}
388 # additional request options
389 local opt dhcpopts daemonize
390 for opt
in $reqopts; do
391 append dhcpopts
"-O $opt"
394 # don't stay running in background if dhcp is not the main proto on the interface (e.g. when using pptp)
395 [ "$proto1" != "$proto" ] && {
396 append dhcpopts
"-n -q"
398 append dhcpopts
"-O rootpath -R"
401 [ "$broadcast" = 1 ] && broadcast
="-O broadcast" || broadcast
=
403 SERVICE_DAEMONIZE
=$daemonize \
404 SERVICE_PID_FILE
="$pidfile" \
405 service_start
/sbin
/udhcpc
-t 0 -i "$iface" \
406 ${ipaddr:+-r $ipaddr} \
407 ${hostname:+-H $hostname} \
408 ${clientid:+-c $clientid} \
409 ${vendorid:+-V $vendorid} \
410 -b -p "$pidfile" $broadcast \
414 setup_interface_none
"$iface" "$config"
417 if ( eval "type setup_interface_$proto" ) >/dev
/null
2>/dev
/null
; then
418 eval "setup_interface_$proto '$iface' '$config' '$proto'"
420 echo "Interface type $proto not supported."
427 stop_interface_dhcp
() {
431 config_get ifname
"$config" ifname
433 local lock
="/var/lock/dhcp-${ifname}"
434 [ -f "$lock" ] && lock
-u "$lock"
438 SERVICE_PID_FILE
="/var/run/dhcp-${ifname}.pid" \
439 service_stop
/sbin
/udhcpc
441 uci
-P /var
/state revert
"network.$config"
448 [ -x /usr
/sbin
/brctl
] ||
return 0
449 brctl show
2>/dev
/null |
grep "$dev" >/dev
/null
&& {
450 # interface is still part of a bridge, correct that
452 for brdev
in $
(brctl show |
awk '$2 ~ /^[0-9].*\./ { print $1 }'); do
453 brctl delif
"$brdev" "$dev" 2>/dev
/null
>/dev
/null
454 do_sysctl
"net.ipv6.conf.$dev.disable_ipv6" 0