[package] base-files:
[openwrt/svn-archive/archive.git] / package / base-files / files / lib / network / config.sh
index 12b08a29ff49bb2fa7a5ca2a9145d4c1f45d0ea6..1bb3a46ecd133f428138051d39854a264d287c2c 100755 (executable)
@@ -66,6 +66,52 @@ add_vlan() {
        return 1
 }
 
+# add dns entries if they are not in resolv.conf yet
+add_dns() {
+       local cfg="$1"; shift
+
+       remove_dns "$cfg"
+
+       # We may be called by pppd's ip-up which has a nonstandard umask set.
+       # Create an empty file here and force its permission to 0644, otherwise
+       # dnsmasq will not be able to re-read the resolv.conf.auto .
+       [ ! -f /tmp/resolv.conf.auto ] && {
+               touch /tmp/resolv.conf.auto
+               chmod 0644 /tmp/resolv.conf.auto
+       }
+
+       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
+
+       [ -n "$cfg" ] && {
+               uci_set_state network "$cfg" dns "$add"
+               uci_set_state network "$cfg" resolv_dns "$add"
+       }
+}
+
+# remove dns entries of the given iface
+remove_dns() {
+       local cfg="$1"
+
+       [ -n "$cfg" ] && {
+               [ -f /tmp/resolv.conf.auto ] && {
+                       local dns=$(uci_get_state network "$cfg" resolv_dns)
+                       for dns in $dns; do
+                               sed -i -e "/^nameserver $dns$/d" /tmp/resolv.conf.auto
+                       done
+               }
+
+               uci_revert_state network "$cfg" dns
+               uci_revert_state network "$cfg" resolv_dns
+       }
+}
+
 # sort the device list, drop duplicates
 sort_list() {
        local arg="$*"
@@ -115,6 +161,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
@@ -139,7 +187,7 @@ prepare_interface() {
                                        # 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
                        }
                ;;
@@ -181,13 +229,7 @@ setup_interface_static() {
        [ -z "$ip6addr" ] || $DEBUG ifconfig "$iface" add "$ip6addr"
        [ -z "$gateway" ] || $DEBUG route add default gw "$gateway" dev "$iface"
        [ -z "$ip6gw" ] || $DEBUG route -A inet6 add default gw "$ip6gw" dev "$iface"
-       [ -z "$dns" ] || {
-               for ns in $dns; do
-                       grep "$ns" /tmp/resolv.conf.auto 2>/dev/null >/dev/null || {
-                               echo "nameserver $ns" >> /tmp/resolv.conf.auto
-                       }
-               done
-       }
+       [ -z "$dns" ] || add_dns "$config" $dns
 
        config_get type "$config" TYPE
        [ "$type" = "alias" ] && return 0
@@ -264,7 +306,6 @@ setup_interface() {
        }
        set_interface_ifname "$config" "$iface_main"
 
-       pidfile="/var/run/$iface_main.pid"
        [ -n "$proto" ] || config_get proto "$config" proto
        case "$proto" in
                static)
@@ -272,11 +313,14 @@ setup_interface() {
                        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_main"
+                       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_main"
+                       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
@@ -292,7 +336,7 @@ setup_interface() {
                                local dhcpopts
                                [ ."$proto1" != ."$proto" ] && dhcpopts="-n -q"
                                $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 "/var/lock/dhcp-$iface_main"
+                               lock -u "$lockfile"
                        fi
                ;;
                none)
@@ -327,12 +371,34 @@ setup_interface() {
        }
 }
 
+stop_interface_dhcp() {
+       local config="$1"
+
+       local ifname
+       config_get ifname "$config" ifname
+
+       local lock="/var/lock/dhcp-${ifname}"
+       [ -f "$lock" ] && lock -u "$lock"
+
+       remove_dns "$config"
+
+       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() {
        local dev="$1"
        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