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