From d3a6fe9915875cb78ca3d22f8330a0380dcd58a0 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Sun, 30 Jul 2006 03:09:09 +0000 Subject: rewrite of the network scripts and configuration git-svn-id: svn://svn.openwrt.org/openwrt/branches/buildroot-ng/openwrt@4323 3c298f89-4303-0410-b956-a3cf2f4a3e73 --- .../base-files/default/etc/hotplug.d/net/10-net | 201 ++------------------- package/base-files/default/etc/init.d/S10boot | 3 +- package/base-files/default/etc/init.d/S40network | 9 + package/base-files/default/etc/init.d/rcS | 11 +- package/base-files/default/lib/network/config.sh | 147 +++++++++++++++ package/base-files/default/sbin/hotplug | 1 - package/base-files/default/sbin/ifdown | 32 +++- package/base-files/default/sbin/ifup | 42 ++--- package/base-files/default/sbin/wifi | 7 +- .../default/usr/share/udhcpc/default.script | 14 +- 10 files changed, 227 insertions(+), 240 deletions(-) create mode 100755 package/base-files/default/etc/init.d/S40network create mode 100755 package/base-files/default/lib/network/config.sh (limited to 'package/base-files/default') diff --git a/package/base-files/default/etc/hotplug.d/net/10-net b/package/base-files/default/etc/hotplug.d/net/10-net index f306ae342d..d5b7f6b0fa 100644 --- a/package/base-files/default/etc/hotplug.d/net/10-net +++ b/package/base-files/default/etc/hotplug.d/net/10-net @@ -1,191 +1,28 @@ -#!/bin/sh # Copyright (C) 2006 OpenWrt.org -find_name() -{ - for ifname in lan wan wifi ${ifnames}; do - IFTYPE="${ifname}" - eval "IFPROTO=\"\${${IFTYPE}_proto}\"" - eval "IFACE=\"\${${IFTYPE}_ifname}\"" - case "$IFPROTO" in - ""|none);; - static|dhcp) - [ "${IFACE}" = "$INTERFACE" ] && return 0 - [ "${IFACE%%[0-9]*}" = "br" ] && { - eval "ifs=\"\${${IFTYPE}_ifnames}\"" - for part in $ifs; do - [ "$part" = "$INTERFACE" ] && return 0 - done - } - ;; - pppoa) - [ "$INTERFACE" = "atm0" \ - -a -x /sbin/ifup.${IFPROTO} ] && return 0 - ;; - *) - eval "device=\"\${${IFTYPE}_device}\"" - [ "$device" = "$INTERFACE" \ - -a -x /sbin/ifup.${IFPROTO} ] && return 0 - ;; - esac - done - IFACE="" - IFTYPE="" - IFPROTO="" - return 255 -} - -do_ifup() { - if="$3" - eval "if_proto=\"\${${2}_proto}\"" - - pidfile=/var/run/${if}.pid - [ -f $pidfile ] && $DEBUG kill $(cat $pidfile) - - case "$1" in - static) - eval "ip=\"\${${2}_ipaddr}\"" - eval "ip6=\"\${${2}_ip6addr}\"" - eval "netmask=\"\${${2}_netmask}\"" - eval "gateway=\"\${${2}_gateway}\"" - eval "dns=\"\${${2}_dns}\"" - eval "static_route=\"\${${2}_static_route}\"" - - $DEBUG ifconfig $if $ip ${netmask:+netmask $netmask} broadcast + up - [ -n "$ip6" ] && $DEBUG ifconfig $if add $ip6 - - [ -n "$static_route" ] && { - for route in $static_route; do - if [ "$(echo $route | cut -d \/ -f2)" != "32" ]; - then - route add -net $(echo $route | cut -d \/ -f1) netmask $(echo $route | cut -d \/ -f1) dev $if - else - route add -host $(echo $route | cut -d \/ -f1) dev $if - fi - done - } +include network - ${gateway:+$DEBUG route add default gw $gateway} +addif() { + scan_interfaces + setup_interface "$INTERFACE" - [ -f /tmp/resolv.conf ] || { - debug "# --- creating /tmp/resolv.conf ---" - for ns in $dns; do - echo "nameserver $ns" >> /tmp/resolv.conf - done - } - - env -i ACTION="ifup" INTERFACE="${2}" PROTO=static /sbin/hotplug "iface" & - ;; - dhcp) - eval "ip=\"\${${2}_ipaddr}\"" - eval "netmask=\"\${${2}_netmask}\"" - eval "hostname=\"\${${2}_hostname}\"" - - $DEBUG ifconfig $if $ip ${netmask:+netmask $netmask} broadcast + up - - args="-i $if ${ip:+-r $ip} -b -p $pidfile" - hostname="${hostname%%.*}" - args="$args${hostname:+ -H $hostname}" - [ "$if_proto" = "pptp" ] && args="$args -n -q" || args="$args -R &" - ${DEBUG:-eval} "udhcpc $args" - # hotplug events are handled by /usr/share/udhcpc/default.script - ;; - *) - if [ -x "/sbin/ifup.$1" ]; then - ( $DEBUG . /sbin/ifup.$1 ${2} $3 ) - fi - ;; - esac -} - -do_register() -{ - [ -z "$IFTYPE" -o -z "$IFPROTO" ] && { - find_name || { - case "${INTERFACE%%[0-9]*}" in - wds) - for ifname in lan wifi; do - eval "if=\"\${${ifname}_ifname}\"" - [ -z "$IFPROTO" ] && [ "$if" = "br0" ] && { - eval "IFPROTO=\"\${${2}_proto}\"" - IFTYPE="${tmp}" - } - done - [ -z "$IFPROTO" ] && return 0 - ;; - atm) - for tmp in lan wan wifi ${ifnames}; do - eval "if_proto=\"\${${2}_proto}\"" - [ "$if_proto" = "pppoa" ] && { - do_ifup "pppoa" "$tmp" "$INTERFACE" - return 0 - } - done - ;; - *) - return 0 - ;; - esac - } - } - - case "${INTERFACE%%[0-9]*}" in - ppp|atm);; - *) - eval "mac=\"\${${IFTYPE}_hwaddr}\"" - ${mac:+$DEBUG ifconfig $INTERFACE down hw ether $mac} - ;; - esac - - eval "if=\"\${${IFTYPE}_ifname}\"" - if [ "${if%%[0-9]}" = "br" ]; then - if_valid "$INTERFACE" && { - ifconfig "$if" 2>&- >&- || { - eval "stp=\"\${${IFTYPE}_stp}\"" - $DEBUG brctl addbr "$if" - $DEBUG brctl setfd "$if" 0 - $DEBUG brctl stp "$if" "${stp:-0}" + # find all vlan configurations for this interface and set them up as well + for ifc in $interfaces; do + config_get type "$ifc" type + case "$type" in + bridge) config_get ifs "$ifc" ifnames;; + *) config_get ifs "$ifc" ifname;; + esac + for dev in $ifs; do + [ "${dev%%\.*}" = "$INTERFACE" -a "$dev" != "$INTERFACE" ] && { + add_vlan "$dev" } - - if [ "$INTERFACE" != "$if" ]; then - $DEBUG ifconfig "$INTERFACE" 0.0.0.0 up - $DEBUG brctl addif "$if" "$INTERFACE" - $DEBUG ifconfig "$INTERFACE" allmulti - else - do_ifup "$IFPROTO" "$IFTYPE" "$if" - fi - } - else - [ "${INTERFACE%%[0-9]*}" = "ppp" ] || do_ifup "$IFPROTO" "$IFTYPE" "$if" - fi -} - -do_unregister() { - [ -z "$IFTYPE" -o -z "$IFPROTO" ] && { - find_name || return 0 - } - - [ "${IFACE%%[0-9]*}" = "br" ] && { - if [ "$INTERFACE" != "$IFACE" ]; then - brctl delif "$IFACE" "$INTERFACE" 2>&- >&- - else - brctl delbr "$IFACE" 2>&- >&- - fi - } - - case "$IFPROTO" in - pppoe|pppoa|pptp) - killall pppd 2>&- >&- - ;; - dhcp) - [ -f /var/run/${INTERFACE}.pid ] && kill "$(cat /var/run/${INTERFACE}.pid)" 2>&- >&- - ;; - esac - - [ "${INTERFACE%%[0-9]*}" = "atm" ] || ifconfig "$INTERFACE" 0.0.0.0 down 2>&- + done + done } case "$ACTION" in - add|register) do_register;; - remove|unregister) do_unregister;; + add|register) + addif + ;; esac diff --git a/package/base-files/default/etc/init.d/S10boot b/package/base-files/default/etc/init.d/S10boot index cb5c6ac4a7..19420eda53 100755 --- a/package/base-files/default/etc/init.d/S10boot +++ b/package/base-files/default/etc/init.d/S10boot @@ -5,13 +5,14 @@ [ -f /proc/mounts ] || /sbin/mount_root [ -f /proc/jffs2_bbc ] && echo "S" > /proc/jffs2_bbc -vconfig set_name_type VLAN_PLUS_VID_NO_PAD +vconfig set_name_type DEV_PLUS_VID_NO_PAD HOSTNAME=${wan_hostname%%.*} echo ${HOSTNAME:=OpenWrt}>/proc/sys/kernel/hostname mkdir -p /var/run mkdir -p /var/log +mkdir -p /var/lock touch /var/log/wtmp touch /var/log/lastlog [ "$FAILSAFE" = "true" ] && touch /tmp/.failsafe diff --git a/package/base-files/default/etc/init.d/S40network b/package/base-files/default/etc/init.d/S40network new file mode 100755 index 0000000000..20a11845b6 --- /dev/null +++ b/package/base-files/default/etc/init.d/S40network @@ -0,0 +1,9 @@ +#!/bin/sh +# Copyright (C) 2006 OpenWrt.orga + +setup_switch() { return 0; } + +. /etc/functions.sh +include network +setup_switch +/sbin/wifi diff --git a/package/base-files/default/etc/init.d/rcS b/package/base-files/default/etc/init.d/rcS index dad989fe09..2aa70910c3 100755 --- a/package/base-files/default/etc/init.d/rcS +++ b/package/base-files/default/etc/init.d/rcS @@ -3,12 +3,13 @@ ${FAILSAFE:+exit} -[ -f /etc/config/network ] && . /etc/config/network -eval $(ipcalc "$log_ipaddr") -[ "$log_ipaddr" = "$IP" ] || log_ipaddr="" -syslogd -C 16 ${log_ipaddr:+-L -R $log_ipaddr} +# FIXME: add logging configuration +#[ -f /etc/config/network ] && . /etc/config/network +#eval $(ipcalc "$log_ipaddr") +#[ "$log_ipaddr" = "$IP" ] || log_ipaddr="" +syslogd -C 16 #${log_ipaddr:+-L -R $log_ipaddr} klogd -#${FAILSAFE:+telnetd -l /bin/login; ifup lan; exit} + for i in /etc/init.d/S*; do $i start 2>&1 done | logger -s -p 6 -t '' & diff --git a/package/base-files/default/lib/network/config.sh b/package/base-files/default/lib/network/config.sh new file mode 100755 index 0000000000..59ec84aba8 --- /dev/null +++ b/package/base-files/default/lib/network/config.sh @@ -0,0 +1,147 @@ +#!/bin/sh +# Copyright (C) 2006 OpenWrt.org + +# DEBUG="echo" + +find_config() { + local type iface ifn + for ifn in $interfaces; do + config_get type "$ifn" type + config_get iface "$ifn" ifname + case "$type" in + bridge) + config_get iface "$ifn" ifnames + ;; + esac + for ifc in $iface; do + [ "$ifc" = "$1" ] && { + echo "$ifn" + return 0 + } + done + done + + return 1; +} + +scan_interfaces() { + local mode type iface + interfaces= + config_cb() { + config_get type "$CONFIG_SECTION" TYPE + case "$type" in + interface) + config_get type "$CONFIG_SECTION" type + config_get mode "$CONFIG_SECTION" proto + case "$type" in + bridge) + config_get iface "$CONFIG_SECTION" ifname + iface="${iface:-br-$CONFIG_SECTION}" + config_set "$CONFIG_SECTION" ifname "$iface" + ;; + esac + append interfaces "$CONFIG_SECTION" + ( type "scan_$mode" ) >/dev/null 2>/dev/null && eval "scan_$mode '$CONFIG_SECTION'" + ;; + esac + } + config_load network +} + +add_vlan() { + local vif="${1%\.*}" + + [ "$1" = "$vif" ] || ifconfig "$1" >/dev/null 2>/dev/null || { + ifconfig "$vif" up 2>/dev/null >/dev/null || add_vlan "$vif" + $DEBUG vconfig add "$vif" "${1##*\.}" + } +} + +setup_interface() { + local iface="$1" + local config="$2" + local proto="$3" + + [ -n "$config" ] || { + config=$(find_config "$iface") + [ "$?" = 0 ] || return 1 + } + + [ -n "$proto" ] || { + config_get proto "$config" proto + } + + config_get iftype "$config" type + + # Setup VLAN interfaces + add_vlan "$iface" + + # Setup bridging + case "$iftype" in + bridge) + config_get bridge_ifname "$config" ifname + ifconfig "$iface" up 2>/dev/null >/dev/null + ifconfig "$bridge_ifname" 2>/dev/null >/dev/null && { + $DEBUG brctl addif "$bridge_ifname" "$iface" + return 0 + } || { + $DEBUG brctl addbr "$bridge_ifname" + $DEBUG brctl setfd "$bridge_ifname" 0 + $DEBUG brctl addif "$bridge_ifname" "$iface" + iface="$bridge_ifname" + } + ;; + esac + + # Interface settings + config_get mtu "$config" mtu + $DEBUG ifconfig "$iface" ${mtu:+mtu $mtu} up + + pidfile="/var/run/$iface.pid" + case "$proto" in + static) + config_get ipaddr "$config" ipaddr + config_get netmask "$config" netmask + [ -z "$ipaddr" -o -z "$netmask" ] && return 1 + + config_get ip6addr "$config" ip6addr + config_get gateway "$config" gateway + config_get dns "$config" dns + + $DEBUG ifconfig "$iface" "$ipaddr" netmask "$netmask" + [ -z "$gateway" ] || route add default gw "$gateway" + [ -z "$dns" -o -f /tmp/resolv.conf ] || { + for ns in $dns; do + echo "nameserver $ns" >> /tmp/resolv.conf + done + } + + env -i ACTION="ifup" INTERFACE="config" DEVICE="$iface" PROTO=static /sbin/hotplug "iface" & + ;; + dhcp) + pid="$(cat "$pidfile" 2>/dev/null)" + [ -n "$pid" -a -d "/proc/$pid" ] && kill -9 "$pid" + + config_get ipaddr "$config" ipaddr + config_get netmask "$config" netmask + config_get hostname "$config" hostname + config_get proto1 "$config" proto + + [ -z "$ipaddr" ] || \ + $DEBUG ifconfig "$iface" "$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) + [ "$proto1" != "$proto" ] && dhcpopts="-n -q" + $DEBUG udhcpc -i "$iface" ${ipaddr:+-r $ipaddr} ${hostname:+-H $hostname} -b -p "$pidfile" ${dhcpopts:- -R &} + ;; + *) + if ( eval "type setup_interface_$proto" ) >/dev/null 2>/dev/null; then + eval "setup_interface_$proto '$iface' '$config' '$proto'" + else + echo "Interface type $proto not supported." + return 1 + fi + ;; + esac +} + diff --git a/package/base-files/default/sbin/hotplug b/package/base-files/default/sbin/hotplug index afe52a446f..b1b6f97b23 100755 --- a/package/base-files/default/sbin/hotplug +++ b/package/base-files/default/sbin/hotplug @@ -13,7 +13,6 @@ } . /etc/functions.sh -. /etc/config/network PATH=/bin:/sbin:/usr/bin:/usr/sbin LOGNAME=root diff --git a/package/base-files/default/sbin/ifdown b/package/base-files/default/sbin/ifdown index 50ffee5304..7debb9559f 100755 --- a/package/base-files/default/sbin/ifdown +++ b/package/base-files/default/sbin/ifdown @@ -3,18 +3,32 @@ [ $# = 0 ] && { echo " $0 "; exit; } . /etc/functions.sh -. /etc/config/network +include network +scan_interfaces debug "### ifdown $type ###" type=$1 -eval "if_proto=\"\${${type}_proto}\"" -eval "if=\"\${${type}_ifname}\"" -[ "${if%%[0-9]}" = "ppp" ] && eval "if=\"\${${type}_device}\"" +config_get proto "$type" proto +[ -z "$proto" ] && { echo "interface not found."; exit; } -case "$if_proto" in - pppoa) hotplug_dev unregister atm0; exit 0 ;; - ""|none) exit 0;; -esac +# kill active ppp daemon +pid="$(cat /var/run/ppp-${type}.pid 2>/dev/null)" +[ -n "$pid" -a -d "/proc/$pid" ] && { + kill $pid + sleep 1 + [ -d "/proc/$pid" ] && kill -9 $pid +} -hotplug_dev unregister "$if" +# kill any other process associated with the interface +config_get ifname "$type" ifname +pid="$(cat /var/run/${ifname}.pid 2>/dev/null)" +[ -n "$pid" -a -d "/proc/$pid" ] && kill -9 $pid + +config_get ifname "$type" ifname +ifconfig "$ifname" >/dev/null 2>/dev/null && { + ifconfig "$ifname" 0.0.0.0 down + + config_get iftype "$type" type + [ "$iftype" = "bridge" ] && brctl delbr "$ifname" +} diff --git a/package/base-files/default/sbin/ifup b/package/base-files/default/sbin/ifup index 390f960395..c40de38ad8 100755 --- a/package/base-files/default/sbin/ifup +++ b/package/base-files/default/sbin/ifup @@ -1,38 +1,18 @@ #!/bin/sh # Copyright (C) 2006 OpenWrt.org -[ $# = 0 ] && { echo " $0 "; exit; } -. /etc/functions.sh -. /etc/config/network +. /sbin/ifdown "$@" -ifdown $1 - -debug "### ifup $type ###" -type=$1 - -eval "if_proto=\"\${${type}_proto}\"" -eval "if=\"\${${type}_ifname}\"" -[ "${if%%[0-9]}" = "ppp" ] && eval "if=\"\${${type}_device}\"" - -case "$if_proto" in - pppoa) hotplug_dev register atm0; exit 0 ;; - pppoe) - # PPPoE over ATM - [ "$if" = "nas0" ] && { - ifconfig nas0 2>&- >&- || { - hotplug_dev register atm0 - exit 0 - } - } +config_get iftype "$1" type +case "$iftype" in + bridge) + config_get ifname "$1" ifnames + ;; + *) + config_get ifname "$1" ifname ;; - none|"") exit 0;; esac -if [ "${if%%[0-9]}" = "br" ]; then - eval "ifnames=\"\${${type}_ifnames}\"" - for sif in $ifnames; do - hotplug_dev register "$sif" - done -else - hotplug_dev register "$if" -fi +for dev in $ifname; do + setup_interface "$dev" "$1" +done diff --git a/package/base-files/default/sbin/wifi b/package/base-files/default/sbin/wifi index 63b2f121bd..f1ac94fa3a 100755 --- a/package/base-files/default/sbin/wifi +++ b/package/base-files/default/sbin/wifi @@ -30,12 +30,7 @@ config_cb() { } config_load wireless - -[ -d /lib/wifi -a -n "$(ls /lib/wifi/*.sh 2>&-)" ] && { - for script in /lib/wifi/*.sh; do - . $script - done -} +include wifi for device in $DEVICES; do ( config_get type "$device" type diff --git a/package/base-files/default/usr/share/udhcpc/default.script b/package/base-files/default/usr/share/udhcpc/default.script index 4604648e6b..a244e05a15 100755 --- a/package/base-files/default/usr/share/udhcpc/default.script +++ b/package/base-files/default/usr/share/udhcpc/default.script @@ -1,15 +1,19 @@ #!/bin/sh [ -z "$1" ] && echo "Error: should be run by udhcpc" && exit 1 -. /etc/config/network +. /etc/functions.sh +include network RESOLV_CONF="/tmp/resolv.conf" hotplug_event() { - for ifname in lan wan wifi ${ifnames}; do - eval "proto=\"\${${ifname}_proto}\"" - eval "if=\"\${${ifname}_ifname}\"" + scan_interfaces + for ifc in $interfaces; do + config_get ifname $ifc ifname + [ "$ifname" = "$interface" ] || continue + + config_get proto $ifc proto [ "$proto" = "dhcp" ] || continue - [ "$if" = "$interface" ] || continue + env -i ACTION="$1" INTERFACE="$ifname" PROTO=dhcp /sbin/hotplug iface done } -- cgit v1.2.3