4e96ad77ca21d3bc315aee7fc4e7f3bed00907fe
[openwrt.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 wifi () (
32 debug "### wifi $1 ###"
33 if=$(awk 'gsub(":","") {print $1}' /proc/net/wireless)
34 $DEBUG wlconf $if $1
35 )
36
37 ifup () (
38 type=$1
39 debug "### ifup $type ###"
40
41 if=$(nvram_get ${type}_ifname)
42 if [ "${if%%[0-9]}" = "ppp" ]; then
43 if=$(nvram_get pppoe_ifname)
44 fi
45
46 if_valid $if || return
47
48 $DEBUG ifconfig $if down
49 if [ "${if%%[0-9]}" = "br" ]; then
50 stp=$(nvram_get ${type}_stp)
51 $DEBUG brctl delbr $if
52 $DEBUG brctl addbr $if
53 $DEBUG brctl setfd $if 0
54 $DEBUG brctl stp $if $stp
55 if_list=$(nvram_get ${type}_ifnames)
56 for sif in $if_list; do {
57 if_valid $sif || continue
58 $DEBUG ifconfig $sif 0.0.0.0 up
59 $DEBUG brctl addif $if $sif
60 } done
61 fi
62
63 if_mac=$(nvram_get ${type}_hwaddr)
64 [ -z "$if_mac" ] || $DEBUG ifconfig $if hw ether $if_mac
65
66 if_proto=$(nvram_get ${type}_proto)
67 case "$if_proto" in
68 static)
69 if_ip=$(nvram_get ${type}_ipaddr)
70 if_netmask=$(nvram_get ${type}_netmask)
71 if_gateway=$(nvram_get ${type}_gateway)
72
73 ipcalc -s "$if_ip" || return
74 ipcalc -s "$if_netmask" || return
75 $DEBUG ifconfig $if $if_ip netmask $if_netmask up
76
77 ipcalc -s "$if_gateway" || return
78 $DEBUG route add default gw $if_gateway
79
80 [ -f /etc/resolv.conf ] && return
81
82 debug "# --- creating /etc/resolv.conf ---"
83 for dns in $(nvram_get ${type}_dns); do {
84 echo "nameserver $dns" >> /etc/resolv.conf
85 } done
86 ;;
87 dhcp)
88 pidfile=/tmp/dhcp-${type}.pid
89 if [ -f $pidfile ]; then
90 $DEBUG kill $(cat $pidfile)
91 fi
92 cmd="udhcpc -i $if -b -p $pidfile &"
93 ${DEBUG:-eval} $cmd
94 ;;
95 pppoe)
96 if_username=$(nvram_get ppp_username)
97 if_password=$(nvram_get ppp_passwd)
98 if_redial=$(nvram_get ppp_redialperiod)
99 if_idletime=$(nvram_get ppp_idletime)
100
101 $DEBUG ifconfig $if 0.0.0.0 up
102
103 $DEBUG /sbin/pppoecd $if -u $if_username -p $if_password -i 0 -I $if_redial -T $if_idletime -k
104 ;;
105 *)
106 echo "### WARNING $if: $if_proto is not supported"
107 ;;
108 esac
109 )
110
111 ifdown () (
112 type=$1
113 debug "### ifdown $type ###"
114 if=$(nvram_get ${type}_ifname)
115 if_valid $if || return
116 $DEBUG ifdown $if
117 )
This page took 0.043495 seconds and 3 git commands to generate.