2 # Copyright (C) 2006 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 # 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.
152 prepare_interface
() {
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;
162 ifconfig
"$iface" 2>/dev
/null
>/dev
/null
&& {
164 config_get proto
"$config" proto
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)
170 local mtu macaddr txqueuelen
171 config_get mtu
"$config" mtu
172 [ -n "$macaddr" ] || config_get macaddr
"$config" macaddr
173 config_get txqueuelen
"$config" txqueuelen
174 [ -n "$macaddr" ] && $DEBUG ifconfig
"$iface" down
175 $DEBUG ifconfig
"$iface" ${macaddr:+hw ether "$macaddr"} ${mtu:+mtu $mtu} ${txqueuelen:+txqueuelen $txqueuelen} up
177 [ "$proto" = none
] || ifconfig
"$iface" 0.0.0.0
179 # Apply sysctl settings
180 map_sysctls
"$config" "$iface"
183 # Setup VLAN interfaces
184 add_vlan
"$iface" && return 1
185 ifconfig
"$iface" 2>/dev
/null
>/dev
/null ||
return 0
189 config_get iftype
"$config" type
193 config_get macaddr
"$config" macaddr
194 [ -x /usr
/sbin
/brctl
] && {
195 ifconfig
"br-$config" 2>/dev
/null
>/dev
/null
&& {
196 local newdevs devices
197 config_get devices
"$config" device
198 for dev
in $
(sort_list
"$devices" "$iface"); do
199 append newdevs
"$dev"
201 uci_toggle_state network
"$config" device
"$newdevs"
202 $DEBUG ifconfig
"$iface" 0.0.0.0
203 $DEBUG do_sysctl
"net.ipv6.conf.$iface.disable_ipv6" 1
204 $DEBUG brctl addif
"br-$config" "$iface"
205 # Bridge existed already. No further processing necesary
207 local stp igmp_snooping
208 config_get_bool stp
"$config" stp
0
209 config_get_bool igmp_snooping
"$config" igmp_snooping
1
210 $DEBUG brctl addbr
"br-$config"
211 $DEBUG brctl setfd
"br-$config" 0
212 $DEBUG ifconfig
"$iface" 0.0.0.0
213 $DEBUG do_sysctl
"net.ipv6.conf.$iface.disable_ipv6" 1
214 $DEBUG brctl addif
"br-$config" "$iface"
215 $DEBUG brctl stp
"br-$config" $stp
216 [ -z "$macaddr" ] && macaddr
="$(cat /sys/class/net/$iface/address)"
217 echo $igmp_snooping > /sys
/devices
/virtual
/net
/br-
$config/bridge
/multicast_snooping
2>/dev
/null
218 $DEBUG ifconfig
"br-$config" hw ether
$macaddr up
219 # Creating the bridge here will have triggered a hotplug event, which will
220 # result in another setup_interface() call, so we simply stop processing
221 # the current event at this point.
223 ifconfig
"$iface" ${macaddr:+hw ether "${macaddr}"} up
2>/dev
/null
>/dev
/null
231 set_interface_ifname
() {
236 config_get device
"$1" device
237 uci_toggle_state network
"$config" ifname
"$ifname"
238 uci_toggle_state network
"$config" device
"$device"
241 setup_interface_none
() {
242 env
-i ACTION
="ifup" INTERFACE
="$2" DEVICE
="$1" PROTO
=none
/sbin
/hotplug-call
"iface" &
245 setup_interface_static
() {
249 local ipaddr netmask ip6addr
250 config_get ipaddr
"$config" ipaddr
251 config_get netmask
"$config" netmask
252 config_get ip6addr
"$config" ip6addr
253 [ -z "$ipaddr" -o -z "$netmask" ] && [ -z "$ip6addr" ] && return 1
255 local gateway ip6gw dns bcast metric
256 config_get gateway
"$config" gateway
257 config_get ip6gw
"$config" ip6gw
258 config_get dns
"$config" dns
259 config_get bcast
"$config" broadcast
260 config_get metric
"$config" metric
264 *:*) ip6addr
="$ip6addr/64" ;;
267 [ -z "$ipaddr" ] ||
$DEBUG ifconfig
"$iface" "$ipaddr" netmask
"$netmask" broadcast
"${bcast:-+}"
268 [ -z "$ip6addr" ] ||
$DEBUG ifconfig
"${iface%:*}" add
"$ip6addr"
269 [ -z "$gateway" ] ||
$DEBUG route add default gw
"$gateway" ${metric:+metric $metric} dev
"$iface"
270 [ -z "$ip6gw" ] ||
$DEBUG route
-A inet6 add default gw
"$ip6gw" ${metric:+metric $metric} dev
"${iface%:*}"
271 [ -z "$dns" ] || add_dns
"$config" $dns
273 config_get
type "$config" TYPE
274 [ "$type" = "alias" ] && return 0
276 env
-i ACTION
="ifup" INTERFACE
="$config" DEVICE
="$iface" PROTO
=static
/sbin
/hotplug-call
"iface" &
279 setup_interface_alias
() {
285 config_get cfg
"$config" interface
286 [ "$parent" == "$cfg" ] ||
return 0
288 # parent device and ifname
289 local p_device p_type
290 config_get p_device
"$cfg" device
291 config_get p_type
"$cfg" type
293 # select alias ifname
294 local layer use_iface
295 config_get layer
"$config" layer
2
296 case "$layer:$p_type" in
297 # layer 3: e.g. pppoe-wan or pptp-vpn
298 3:*) use_iface
="$iface" ;;
300 # layer 2 and parent is bridge: e.g. br-wan
301 2:bridge
) use_iface
="br-$cfg" ;;
303 # layer 1: e.g. eth0 or ath0
304 *) use_iface
="$p_device" ;;
309 config_get ctr
"$parent" alias_count
0
311 config_set
"$parent" alias_count
"$ctr"
315 config_get list
"$parent" aliases
316 append list
"$config"
317 config_set
"$parent" aliases
"$list"
319 use_iface
="$use_iface:$ctr"
320 set_interface_ifname
"$config" "$use_iface"
323 config_get proto
"$config" proto
"static"
326 setup_interface_static
"$use_iface" "$config"
329 echo "Unsupported type '$proto' for alias config '$config'"
341 [ -n "$config" ] ||
{
342 config
=$
(find_config
"$iface")
343 [ "$?" = 0 ] ||
return 1
346 prepare_interface
"$iface" "$config" "$vifmac" ||
return 0
348 [ "$iface" = "br-$config" ] && {
349 # need to bring up the bridge and wait a second for
350 # it to switch to the 'forwarding' state, otherwise
351 # it will lose its routes...
357 set_interface_ifname
"$config" "$iface"
359 [ -n "$proto" ] || config_get proto
"$config" proto
362 setup_interface_static
"$iface" "$config"
365 # kill running udhcpc instance
366 local pidfile
="/var/run/dhcp-${iface}.pid"
367 service_kill udhcpc
"$pidfile"
369 local ipaddr netmask hostname proto1 clientid vendorid broadcast reqopts
370 config_get ipaddr
"$config" ipaddr
371 config_get netmask
"$config" netmask
372 config_get hostname
"$config" hostname
373 config_get proto1
"$config" proto
374 config_get clientid
"$config" clientid
375 config_get vendorid
"$config" vendorid
376 config_get_bool broadcast
"$config" broadcast
0
377 config_get reqopts
"$config" reqopts
379 [ -z "$ipaddr" ] || \
380 $DEBUG ifconfig
"$iface" "$ipaddr" ${netmask:+netmask "$netmask"}
382 # additional request options
384 for opt
in $reqopts; do
385 append dhcpopts
"-O $opt"
388 # don't stay running in background if dhcp is not the main proto on the interface (e.g. when using pptp)
389 [ "$proto1" != "$proto" ] && append dhcpopts
"-n -q" || append dhcpopts
"-O rootpath -R &"
390 [ "$broadcast" = 1 ] && broadcast
="-O broadcast" || broadcast
=
392 $DEBUG eval udhcpc
-t 0 -i "$iface" \
393 ${ipaddr:+-r $ipaddr} \
394 ${hostname:+-H $hostname} \
395 ${clientid:+-c $clientid} \
396 ${vendorid:+-V $vendorid} \
397 -b -p "$pidfile" $broadcast \
401 setup_interface_none
"$iface" "$config"
404 if ( eval "type setup_interface_$proto" ) >/dev
/null
2>/dev
/null
; then
405 eval "setup_interface_$proto '$iface' '$config' '$proto'"
407 echo "Interface type $proto not supported."
414 stop_interface_dhcp
() {
418 config_get ifname
"$config" ifname
420 local lock
="/var/lock/dhcp-${ifname}"
421 [ -f "$lock" ] && lock
-u "$lock"
425 local pidfile
="/var/run/dhcp-${ifname}.pid"
426 service_kill udhcpc
"$pidfile"
428 uci
-P /var
/state revert
"network.$config"
435 [ -x /usr
/sbin
/brctl
] ||
return 0
436 brctl show
2>/dev
/null |
grep "$dev" >/dev
/null
&& {
437 # interface is still part of a bridge, correct that
439 for brdev
in $
(brctl show |
awk '$2 ~ /^[0-9].*\./ { print $1 }'); do
440 brctl delif
"$brdev" "$dev" 2>/dev
/null
>/dev
/null
441 do_sysctl
"net.ipv6.conf.$dev.disable_ipv6" 0