base-files: revert r21595
[openwrt/staging/chunkeey.git] / package / base-files / files / lib / network / config.sh
index a5cadf61bdba262a407c687beedb0eaac711fc9f..32170bb274a3f79c20f2c55393abdd2f3ae8a84c 100755 (executable)
@@ -66,6 +66,35 @@ add_vlan() {
        return 1
 }
 
+# add dns entries if they are not in resolv.conf yet
+add_dns() {
+       local cfg="$1"; shift
+
+       local dns
+       local add
+       for dns in "$@"; do
+               grep -qsF "nameserver $dns" /tmp/resolv.conf.auto || {
+                       add="${add:+$add }$dns"
+                       echo "nameserver $dns" >> /tmp/resolv.conf.auto
+               }
+       done
+
+       uci_set_state network "$cfg" dns "$add"
+}
+
+# remove dns entries of the given iface
+remove_dns() {
+       local cfg="$1"
+
+       local dns
+       config_get dns "$cfg" dns
+       for dns in $dns; do
+               sed -i -e "/^nameserver $dns$/d" /tmp/resolv.conf.auto
+       done
+
+       uci_revert_state network "$cfg" dns
+}
+
 # sort the device list, drop duplicates
 sort_list() {
        local arg="$*"
@@ -115,6 +144,8 @@ prepare_interface() {
        config_get iftype "$config" type
        case "$iftype" in
                bridge)
+                       local macaddr
+                       config_get macaddr "$config" macaddr
                        [ -x /usr/sbin/brctl ] && {
                                ifconfig "br-$config" 2>/dev/null >/dev/null && {
                                        local newdevs devices
@@ -123,6 +154,7 @@ prepare_interface() {
                                                append newdevs "$dev"
                                        done
                                        uci_set_state network "$config" device "$newdevs"
+                                       $DEBUG ifconfig "$iface" 0.0.0.0
                                        $DEBUG brctl addif "br-$config" "$iface"
                                        # Bridge existed already. No further processing necesary
                                } || {
@@ -131,13 +163,14 @@ prepare_interface() {
                                        $DEBUG brctl addbr "br-$config"
                                        $DEBUG brctl setfd "br-$config" 0
                                        $DEBUG ifconfig "br-$config" up
+                                       $DEBUG ifconfig "$iface" 0.0.0.0
                                        $DEBUG brctl addif "br-$config" "$iface"
                                        $DEBUG brctl stp "br-$config" $stp
                                        # Creating the bridge here will have triggered a hotplug event, which will
                                        # result in another setup_interface() call, so we simply stop processing
                                        # the current event at this point.
                                }
-                               ifconfig "$iface" up 2>/dev/null >/dev/null
+                               ifconfig "$iface" ${macaddr:+hw ether "${macaddr}"} up 2>/dev/null >/dev/null
                                return 1
                        }
                ;;
@@ -231,57 +264,52 @@ setup_interface_alias() {
 }
 
 setup_interface() {
-       local iface="$1"
+       local iface_main="$1"
        local config="$2"
        local proto="$3"
        local vifmac="$4"
+       local ip6addr_main=
 
        [ -n "$config" ] || {
-               config=$(find_config "$iface")
+               config=$(find_config "$iface_main")
                [ "$?" = 0 ] || return 1
        }
 
-       prepare_interface "$iface" "$config" "$vifmac" || return 0
+       prepare_interface "$iface_main" "$config" "$vifmac" || return 0
 
-       [ "$iface" = "br-$config" ] && {
+       [ "$iface_main" = "br-$config" ] && {
                # need to bring up the bridge and wait a second for
                # it to switch to the 'forwarding' state, otherwise
                # it will lose its routes...
-               ifconfig "$iface" up
+               ifconfig "$iface_main" up
                sleep 1
        }
 
-       # Check whether this interface has an IPv6 address
-       # defined and ensure that the kmod is loaded since
-       # ifup could be triggered before modules are loaded.
-       local hasipv6
-       config_get hasipv6 "$config" ip6addr
-       [ -n "$hasipv6" ] && [ ! -d /proc/sys/net/ipv6 ] && {
-               grep -q '^ipv6' /etc/modules.d/* && insmod ipv6
-       }
-
        # Interface settings
-       grep "$iface:" /proc/net/dev > /dev/null && {
+       grep "$iface_main:" /proc/net/dev > /dev/null && {
                local mtu macaddr
                config_get mtu "$config" mtu
                config_get macaddr "$config" macaddr
-               [ -n "$macaddr" ] && $DEBUG ifconfig "$iface" down
-               $DEBUG ifconfig "$iface" ${macaddr:+hw ether "$macaddr"} ${mtu:+mtu $mtu} up
+               [ -n "$macaddr" ] && $DEBUG ifconfig "$iface_main" down
+               $DEBUG ifconfig "$iface_main" ${macaddr:+hw ether "$macaddr"} ${mtu:+mtu $mtu} up
        }
-       set_interface_ifname "$config" "$iface"
+       set_interface_ifname "$config" "$iface_main"
 
-       pidfile="/var/run/$iface.pid"
        [ -n "$proto" ] || config_get proto "$config" proto
        case "$proto" in
                static)
-                       setup_interface_static "$iface" "$config"
+                       config_get ip6addr_main "$config" ip6addr
+                       setup_interface_static "$iface_main" "$config"
                ;;
                dhcp)
+                       local lockfile="/var/lock/dhcp-$iface_main"
+                       lock "$lockfile"
+
                        # prevent udhcpc from starting more than once
-                       lock "/var/lock/dhcp-$iface"
+                       local pidfile="/var/run/dhcp-${iface_main}.pid"
                        local pid="$(cat "$pidfile" 2>/dev/null)"
-                       if [ -d "/proc/$pid" ] && grep udhcpc "/proc/${pid}/cmdline" >/dev/null 2>/dev/null; then
-                               lock -u "/var/lock/dhcp-$iface"
+                       if [ -d "/proc/$pid" ] && grep -qs udhcpc "/proc/${pid}/cmdline"; then
+                               lock -u "$lockfile"
                        else
                                local ipaddr netmask hostname proto1 clientid
                                config_get ipaddr "$config" ipaddr
@@ -291,21 +319,21 @@ setup_interface() {
                                config_get clientid "$config" clientid
 
                                [ -z "$ipaddr" ] || \
-                                       $DEBUG ifconfig "$iface" "$ipaddr" ${netmask:+netmask "$netmask"}
+                                       $DEBUG ifconfig "$iface_main" "$ipaddr" ${netmask:+netmask "$netmask"}
 
                                # don't stay running in background if dhcp is not the main proto on the interface (e.g. when using pptp)
                                local dhcpopts
                                [ ."$proto1" != ."$proto" ] && dhcpopts="-n -q"
-                               $DEBUG eval udhcpc -t 0 -i "$iface" ${ipaddr:+-r $ipaddr} ${hostname:+-H $hostname} ${clientid:+-c $clientid} -b -p "$pidfile" ${dhcpopts:- -R &}
-                               lock -u "/var/lock/dhcp-$iface"
+                               $DEBUG eval udhcpc -t 0 -i "$iface_main" ${ipaddr:+-r $ipaddr} ${hostname:+-H $hostname} ${clientid:+-c $clientid} -b -p "$pidfile" ${dhcpopts:- -O rootpath -R &}
+                               lock -u "$lockfile"
                        fi
                ;;
                none)
-                       setup_interface_none "$iface" "$config"
+                       setup_interface_none "$iface_main" "$config"
                ;;
                *)
                        if ( eval "type setup_interface_$proto" ) >/dev/null 2>/dev/null; then
-                               eval "setup_interface_$proto '$iface' '$config' '$proto'"
+                               eval "setup_interface_$proto '$iface_main' '$config' '$proto'"
                        else
                                echo "Interface type $proto not supported."
                                return 1
@@ -313,7 +341,7 @@ setup_interface() {
                ;;
        esac
        [ "$proto" = none ] || {
-               for ifn in `ifconfig | grep "^$iface:" | awk '{print $1}'`; do
+               for ifn in `ifconfig | grep "^$iface_main:" | awk '{print $1}'`; do
                        ifconfig "$ifn" down
                done
        }
@@ -321,9 +349,35 @@ setup_interface() {
        local aliases
        config_set "$config" aliases ""
        config_set "$config" alias_count 0
-       config_foreach setup_interface_alias alias "$config" "$iface"
+       config_foreach setup_interface_alias alias "$config" "$iface_main"
        config_get aliases "$config" aliases
        [ -z "$aliases" ] || uci_set_state network "$config" aliases "$aliases"
+
+       # put the ip6addr back to the beginning to become the main ip again
+       [ -z "$ip6addr_main" ] || {
+               $DEBUG ifconfig "$iface_main" del "$ip6addr_main"
+               $DEBUG ifconfig "$iface_main" add "$ip6addr_main"
+       }
+}
+
+stop_interface_dhcp() {
+       local config="$1"
+
+       local iface
+       config_get ifname "$config" ifname
+
+       local lock="/var/lock/dhcp-${ifname}"
+       [ -f "$lock" ] && lock -u "$lock"
+
+       local pidfile="/var/run/dhcp-${ifname}.pid"
+       local pid="$(cat "$pidfile" 2>/dev/null)"
+       [ -d "/proc/$pid" ] && {
+               grep -qs udhcpc "/proc/$pid/cmdline" && kill -TERM $pid && \
+                       while grep -qs udhcpc "/proc/$pid/cmdline"; do sleep 1; done
+               rm -f "$pidfile"
+       }
+
+       uci -P /var/state revert "network.$config"
 }
 
 unbridge() {
@@ -331,7 +385,7 @@ unbridge() {
        local brdev
 
        [ -x /usr/sbin/brctl ] || return 0
-       brctl show | grep "$dev" >/dev/null && {
+       brctl show 2>/dev/null | grep "$dev" >/dev/null && {
                # interface is still part of a bridge, correct that
 
                for brdev in $(brctl show | awk '$2 ~ /^[0-9].*\./ { print $1 }'); do