Disable spanning tree by default
[oweals/openwrt.git] / root / etc / functions.sh
1 #!/bin/ash
2
3 alias debug=${DEBUG:-:}
4
5 # allow env to override nvram
6 nvram_get () {
7  eval "echo \${$1:-\$(nvram get $1)}"
8 }
9 . /etc/nvram.overrides
10
11 # valid interface?
12 if_valid () (
13   [ "${1%%[0-9]}" = "vlan" ] && {
14     i=${1#vlan}
15     hwname=$(nvram_get vlan${i}hwname)
16     hwaddr=$(nvram_get ${hwname}macaddr)
17     [ -z "$hwaddr" ] && return 1
18
19     vif=$(ifconfig -a | awk '{IGNORECASE=1} /^eth.*'$hwaddr'/ {print $1; exit}')
20     debug "# vlan$i: $hwname $hwaddr => $vif"
21
22     $DEBUG ifconfig $vif up
23     $DEBUG vconfig add $vif $i 2>/dev/null
24   }
25   ifconfig "$1" >/dev/null 2>&1 || [ "${1%%[0-9]}" = "br" ]
26 )
27
28 wifi () (
29   debug "### wifi $1 ###"
30   if=$(awk 'gsub(":","") {print $1}' /proc/net/wireless)
31   $DEBUG wlconf $if $1
32 )
33
34 ifup () (
35   type=$1
36   debug "### ifup $type ###"
37
38   if=$(nvram_get ${type}_ifname)
39   if [ "${if%%[0-9]}" = "ppp" ]; then
40     if=$(nvram_get pppoe_ifname)
41   fi
42
43   if_valid $if || return
44
45   $DEBUG ifconfig $if down
46   if [ "${if%%[0-9]}" = "br" ]; then
47     stp=$(nvram_get ${type}_stp)
48     $DEBUG brctl delbr $if
49     $DEBUG brctl addbr $if
50     $DEBUG brctl setfd $if 0
51     $DEBUG brctl stp $if ${stp:-0}
52     if_list=$(nvram_get ${type}_ifnames)
53     for sif in $if_list; do {
54       if_valid $sif || continue
55       $DEBUG ifconfig $sif 0.0.0.0 up
56       $DEBUG brctl addif $if $sif
57     } done
58   fi
59
60   if_mac=$(nvram_get ${type}_hwaddr)
61   ${if_mac:+$DEBUG ifconfig $if hw ether $if_mac}
62
63   if_proto=$(nvram_get ${type}_proto)
64   case "$if_proto" in
65     static)
66       if_ip=$(nvram_get ${type}_ipaddr)
67       if_netmask=$(nvram_get ${type}_netmask)
68       if_gateway=$(nvram_get ${type}_gateway)
69
70       $DEBUG ifconfig $if $if_ip ${if_netmask:+netmask $if_netmask} broadcast + up
71       ${if_gateway:+$DEBUG route add default gw $if_gateway}
72
73       [ -f /etc/resolv.conf ] && return
74
75       debug "# --- creating /etc/resolv.conf ---"
76       for dns in $(nvram_get ${type}_dns); do {
77         echo "nameserver $dns" >> /etc/resolv.conf
78       } done
79     ;;
80     dhcp)
81       pidfile=/tmp/dhcp-${type}.pid
82       if [ -f $pidfile ]; then
83         $DEBUG kill $(cat $pidfile)
84       fi
85       ${DEBUG:-eval} "udhcpc -i $if -b -p $pidfile &" 
86     ;;
87     pppoe)
88       if_username=$(nvram_get ppp_username)
89       if_password=$(nvram_get ppp_passwd)
90       if_redial=$(nvram_get ppp_redialperiod)
91       if_idletime=$(nvram_get ppp_idletime)
92       if_mtu=$(nvram_get wan_mtu)
93
94       $DEBUG ifconfig $if 0.0.0.0 up
95
96       $DEBUG /sbin/pppoecd $if -u $if_username -p $if_password \
97         -i 0 -I $if_redial -T $if_idletime -t $if_mtu -k
98     ;;
99     *)
100       echo "### WARNING $if: $if_proto is not supported"
101     ;;
102   esac
103 )
104
105 ifdown () (
106   type=$1
107   debug "### ifdown $type ###"
108   if=$(nvram_get ${type}_ifname)
109   if_valid $if || return
110   kill $(cat /var/run/${if}.pid 2>-) 2>-
111   $DEBUG ifconfig $if down
112 )