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