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 set_name_type DEV_PLUS_VID_NO_PAD
87 $DEBUG vconfig add "$vif" "${1##*\.}"
93 # add dns entries if they are not in resolv.conf yet
99 # We may be called by pppd's ip-up which has a nonstandard umask set.
100 # Create an empty file here and force its permission to 0644, otherwise
101 # dnsmasq will not be able to re-read the resolv.conf.auto .
102 [ ! -f /tmp/resolv.conf.auto ] && {
103 touch /tmp/resolv.conf.auto
104 chmod 0644 /tmp/resolv.conf.auto
110 grep -qsE "^nameserver ${dns//./\\.}$" /tmp/resolv.conf.auto || {
111 add="${add:+$add }$dns"
112 echo "nameserver $dns" >> /tmp/resolv.conf.auto
117 uci_toggle_state network "$cfg" dns "$add"
118 uci_toggle_state network "$cfg" resolv_dns "$add"
122 # remove dns entries of the given iface
127 [ -f /tmp/resolv.conf.auto ] && {
128 local dns=$(uci_get_state network "$cfg" resolv_dns)
130 sed -i -e "/^nameserver ${dns//./\\.}$/d" /tmp/resolv.conf.auto
134 uci_revert_state network "$cfg" dns
135 uci_revert_state network "$cfg" resolv_dns
139 # sort the device list, drop duplicates
149 prepare_interface_bridge() {
153 # Create the interface, if necessary.
154 # Return status 0 indicates that the setup_interface() call should continue
155 # Return status 1 means that everything is set up already.
157 prepare_interface() {
162 # if we're called for the bridge interface itself, don't bother trying
163 # to create any interfaces here. The scripts have already done that, otherwise
164 # the bridge interface wouldn't exist.
165 [ "br-$config" = "$iface" -o -e "$iface" ] && return 0;
167 ifconfig "$iface" 2>/dev/null >/dev/null && {
169 config_get proto "$config" proto
171 # make sure the interface is removed from any existing bridge and deconfigured,
172 # (deconfigured only if the interface is not set to proto=none)
175 local mtu macaddr txqueuelen
176 config_get mtu "$config" mtu
177 [ -n "$macaddr" ] || config_get macaddr "$config" macaddr
178 config_get txqueuelen "$config" txqueuelen
179 [ -n "$macaddr" ] && $DEBUG ifconfig "$iface" down
180 $DEBUG ifconfig "$iface" ${macaddr:+hw ether "$macaddr"} ${mtu:+mtu $mtu} ${txqueuelen:+txqueuelen $txqueuelen} up
182 [ "$proto" = none ] || ifconfig "$iface" 0.0.0.0
184 # Apply sysctl settings
185 map_sysctls "$config" "$iface"
188 # Setup VLAN interfaces
189 add_vlan "$iface" && return 1
190 ifconfig "$iface" 2>/dev/null >/dev/null || return 0
194 config_get iftype "$config" type
198 config_get macaddr "$config" macaddr
199 [ -x /usr/sbin/brctl ] && {
200 ifconfig "br-$config" 2>/dev/null >/dev/null && {
201 local newdevs devices
202 config_get devices "$config" device
203 for dev in $(sort_list "$devices" "$iface"); do
204 append newdevs "$dev"
206 uci_toggle_state network "$config" device "$newdevs"
207 $DEBUG ifconfig "$iface" 0.0.0.0
208 $DEBUG do_sysctl "net.ipv6.conf.$iface.disable_ipv6" 1
209 $DEBUG brctl addif "br-$config" "$iface"
210 # Bridge existed already. No further processing necesary
212 local stp igmp_snooping
213 config_get_bool stp "$config" stp 0
214 config_get_bool igmp_snooping "$config" igmp_snooping 1
215 $DEBUG brctl addbr "br-$config"
216 $DEBUG brctl setfd "br-$config" 0
217 $DEBUG ifconfig "$iface" 0.0.0.0
218 $DEBUG do_sysctl "net.ipv6.conf.$iface.disable_ipv6" 1
219 $DEBUG brctl addif "br-$config" "$iface"
220 $DEBUG brctl stp "br-$config" $stp
221 [ -z "$macaddr" ] && macaddr="$(cat /sys/class/net/$iface/address)"
222 [ -e /sys/devices/virtual/net/br-$config/bridge/multicast_snooping ] && \
223 echo $igmp_snooping > /sys/devices/virtual/net/br-$config/bridge/multicast_snooping
224 $DEBUG ifconfig "br-$config" hw ether $macaddr up
225 # Creating the bridge here will have triggered a hotplug event, which will
226 # result in another setup_interface() call, so we simply stop processing
227 # the current event at this point.
229 ifconfig "$iface" ${macaddr:+hw ether "${macaddr}"} up 2>/dev/null >/dev/null
237 set_interface_ifname() {
242 config_get device "$1" device
243 uci_toggle_state network "$config" ifname "$ifname"
244 uci_toggle_state network "$config" device "$device"
247 setup_interface_none() {
248 env -i ACTION="ifup" INTERFACE="$2" DEVICE="$1" PROTO=none /sbin/hotplug-call "iface" &
251 setup_interface_static() {
255 local ipaddr netmask ip6addr
256 config_get ipaddr "$config" ipaddr
257 config_get netmask "$config" netmask
258 config_get ip6addr "$config" ip6addr
259 [ -z "$ipaddr" -o -z "$netmask" ] && [ -z "$ip6addr" ] && return 1
261 local gateway ip6gw dns bcast metric
262 config_get gateway "$config" gateway
263 config_get ip6gw "$config" ip6gw
264 config_get dns "$config" dns
265 config_get bcast "$config" broadcast
266 config_get metric "$config" metric
270 *:*) ip6addr="$ip6addr/64" ;;
273 [ -z "$ipaddr" ] || $DEBUG ifconfig "$iface" "$ipaddr" netmask "$netmask" broadcast "${bcast:-+}"
274 [ -z "$ip6addr" ] || $DEBUG ifconfig "${iface%:*}" add "$ip6addr"
275 [ -z "$gateway" ] || $DEBUG route add default gw "$gateway" ${metric:+metric $metric} dev "$iface"
276 [ -z "$ip6gw" ] || $DEBUG route -A inet6 add default gw "$ip6gw" ${metric:+metric $metric} dev "${iface%:*}"
277 [ -z "$dns" ] || add_dns "$config" $dns
279 config_get type "$config" TYPE
280 [ "$type" = "alias" ] && return 0
282 env -i ACTION="ifup" INTERFACE="$config" DEVICE="$iface" PROTO=static /sbin/hotplug-call "iface" &
285 setup_interface_alias() {
291 config_get cfg "$config" interface
292 [ "$parent" == "$cfg" ] || return 0
294 # parent device and ifname
295 local p_device p_type
296 config_get p_device "$cfg" device
297 config_get p_type "$cfg" type
299 # select alias ifname
300 local layer use_iface
301 config_get layer "$config" layer 2
302 case "$layer:$p_type" in
303 # layer 3: e.g. pppoe-wan or pptp-vpn
304 3:*) use_iface="$iface" ;;
306 # layer 2 and parent is bridge: e.g. br-wan
307 2:bridge) use_iface="br-$cfg" ;;
309 # layer 1: e.g. eth0 or ath0
310 *) use_iface="$p_device" ;;
315 config_get ctr "$parent" alias_count 0
317 config_set "$parent" alias_count "$ctr"
321 config_get list "$parent" aliases
322 append list "$config"
323 config_set "$parent" aliases "$list"
325 use_iface="$use_iface:$ctr"
326 set_interface_ifname "$config" "$use_iface"
329 config_get proto "$config" proto "static"
332 setup_interface_static "$use_iface" "$config"
335 echo "Unsupported type '$proto' for alias config '$config'"
347 [ -n "$config" ] || {
348 config=$(find_config "$iface")
349 [ "$?" = 0 ] || return 1
352 prepare_interface "$iface" "$config" "$vifmac" || return 0
354 [ "$iface" = "br-$config" ] && {
355 # need to bring up the bridge and wait a second for
356 # it to switch to the 'forwarding' state, otherwise
357 # it will lose its routes...
363 set_interface_ifname "$config" "$iface"
365 [ -n "$proto" ] || config_get proto "$config" proto
368 setup_interface_static "$iface" "$config"
371 # kill running udhcpc instance
372 local pidfile="/var/run/dhcp-${iface}.pid"
374 SERVICE_PID_FILE="$pidfile" \
375 service_stop /sbin/udhcpc
377 local ipaddr netmask hostname proto1 clientid vendorid broadcast reqopts
378 config_get ipaddr "$config" ipaddr
379 config_get netmask "$config" netmask
380 config_get hostname "$config" hostname
381 config_get proto1 "$config" proto
382 config_get clientid "$config" clientid
383 config_get vendorid "$config" vendorid
384 config_get_bool broadcast "$config" broadcast 0
385 config_get reqopts "$config" reqopts
387 [ -z "$ipaddr" ] || \
388 $DEBUG ifconfig "$iface" "$ipaddr" ${netmask:+netmask "$netmask"}
390 # additional request options
391 local opt dhcpopts daemonize
392 for opt in $reqopts; do
393 append dhcpopts "-O $opt"
396 # don't stay running in background if dhcp is not the main proto on the interface (e.g. when using pptp)
397 [ "$proto1" != "$proto" ] && {
398 append dhcpopts "-n -q"
400 append dhcpopts "-O rootpath -R"
403 [ "$broadcast" = 1 ] && broadcast="-O broadcast" || broadcast=
405 SERVICE_DAEMONIZE=$daemonize \
406 SERVICE_PID_FILE="$pidfile" \
407 service_start /sbin/udhcpc -t 0 -i "$iface" \
408 ${ipaddr:+-r $ipaddr} \
409 ${hostname:+-H $hostname} \
410 ${clientid:+-c $clientid} \
411 ${vendorid:+-V $vendorid} \
412 -b -p "$pidfile" $broadcast \
416 setup_interface_none "$iface" "$config"
419 if ( eval "type setup_interface_$proto" ) >/dev/null 2>/dev/null; then
420 eval "setup_interface_$proto '$iface' '$config' '$proto'"
422 echo "Interface type $proto not supported."
429 stop_interface_dhcp() {
433 config_get ifname "$config" ifname
435 local lock="/var/lock/dhcp-${ifname}"
436 [ -f "$lock" ] && lock -u "$lock"
440 SERVICE_PID_FILE="/var/run/dhcp-${ifname}.pid" \
441 service_stop /sbin/udhcpc
443 uci -P /var/state revert "network.$config"
450 [ -x /usr/sbin/brctl ] || return 0
451 brctl show 2>/dev/null | grep "$dev" >/dev/null && {
452 # interface is still part of a bridge, correct that
454 for brdev in $(brctl show | awk '$2 ~ /^[0-9].*\./ { print $1 }'); do
455 brctl delif "$brdev" "$dev" 2>/dev/null >/dev/null
456 do_sysctl "net.ipv6.conf.$dev.disable_ipv6" 0