b857a86c062f045c5636c96e2609183797443da3
[openwrt/svn-archive/archive.git] / root / etc / networking.sh
1 #!/bin/sh
2 # OpenWrt Networking script
3 # $Id$
4 # Copyright (c) 2004 Mike Baker <mbm at alt.org>
5
6 # to debug:
7 # export DEBUG=echo
8
9 export PATH=/usr/bin:/bin:/usr/sbin:/sbin
10
11 # lookup an interface by mac address
12 mac2if () {
13 if=$(ifconfig -a | grep -i "$1" | grep -e "^eth" | awk '{print $1}')
14 echo $if
15 }
16
17 # allow env to override nvram
18 nvram_get () {
19 eval "echo \${$1:=\$(nvram get $1)}"
20 }
21
22 # valid interface?
23 if_valid () {
24 [ "${1%[0-9]}" = "vlan" ] && {
25 i=${1##vlan}
26 hwname=$(nvram_get vlan${i}hwname)
27 hwaddr=$(nvram_get ${hwname}macaddr)
28 [ -z "$hwaddr" ] && return 1
29
30 vif=$(mac2if $hwaddr)
31 echo "# vlan${i}: $hwname $hwaddr => $vif"
32
33 $DEBUG ifconfig $vif up
34 $DEBUG vconfig add $vif $i
35 }
36 ifconfig "$1" >/dev/null 2>&1 || [ "${1%[0-9]}" = "br" ]
37 return $?
38 }
39
40 wifi_init () {
41 echo "# --- wifi init ---"
42 hwaddr=$(nvram_get il0macaddr)
43 [ -z "$hwaddr" ] && hwaddr=$(nvram_get wl0_hwaddr)
44 if=$(mac2if $hwaddr)
45 $DEBUG wlconf $if up
46 }
47
48 configure () {
49 type=$1
50 echo "# --- $type ---"
51
52 if=$(nvram_get ${type}_ifname)
53 if [ "${if%[0-9]}" = "ppp" ]; then
54 if=$(nvram get pppoe_ifname)
55 fi
56 if_valid $if || return
57
58 if [ "${if%[0-9]}" = "br" ]; then
59 stp=$(nvram get ${type}_stp)
60 $DEBUG ifconfig $if down
61 $DEBUG brctl delbr $if
62 $DEBUG brctl addbr $if
63 $DEBUG brctl setfd $if 0
64 $DEBUG brctl stp $if $stp
65 if_list=$(nvram_get ${type}_ifnames)
66 for sif in $if_list; do {
67 if_valid $sif || continue
68 $DEBUG ifconfig $sif 0.0.0.0 up
69 $DEBUG brctl addif $if $sif
70 }; done
71 fi
72
73 if_mac=$(nvram_get ${type}_hwaddr)
74 $DEBUG ifconfig $if hw ether $if_mac
75
76 if_proto=$(nvram_get ${type}_proto)
77 case "$if_proto" in
78 static)
79 if_ip=$(nvram_get ${type}_ipaddr)
80 if_netmask=$(nvram_get ${type}_netmask)
81 if_gateway=$(nvram_get ${type}_gateway)
82
83 ipcalc -s "$if_ip" || return
84 ipcalc -s "$if_netmask" || return
85 $DEBUG ifconfig $if $if_ip netmask $if_netmask up
86
87 ipcalc -s "$ip_gateway" || return
88 $DEBUG route add default gw $ip_gateway
89 ;;
90 dhcp)
91 pidfile=/tmp/dhcp-${type}.pid
92 if [ -f $pidfile ]; then
93 $DEBUG kill $(cat $pidfile)
94 fi
95 $DEBUG udhcpc -i $if -b -p /tmp/dhcp-${type}.pid
96 ;;
97 pppoe)
98 if_username=$(nvram_get ppp_username)
99 if_password=$(nvram_get ppp_passwd)
100 if_redial=$(nvram_get ppp_redialperiod)
101 if_idletime=$(nvram_get ppp_idletime)
102
103 $DEBUG ifconfig $if 0.0.0.0 up
104
105 $DEBUG /sbin/pppoecd $if -u $if_username -p $if_password -i 0 -I $if_redial -T $if_idletime -k
106 sleep 5
107 $DEBUG /sbin/route add default $if
108 ;;
109 *)
110 echo "$if: $if_proto is not supported"
111 ;;
112 esac
113 }
114
115 ### START NETWORKING ###
116 wifi_init
117
118 $DEBUG vconfig set_name_type VLAN_PLUS_VID_NO_PAD
119
120 # hacks for 1.x hardware
121 [ -z "$(nvram_get vlan0hwname)" ] && {
122 echo "# 1.x HACK"
123 vlan1hwname="et0"
124 vlan2hwname="et0"
125
126 # we remap old device names to new
127 # it's recommended that you continue to
128 # use the old names to preserve backwards
129 # compatibility
130 remap () {
131 eval $1=\"$(nvram_get $1 | awk '{
132 gsub(/eth0/,"vlan2")
133 gsub(/eth1/,"vlan1")
134 print $0
135 }')\"
136 }
137
138 remap lan_ifname
139 remap lan_ifnames
140 remap wifi_ifname
141 remap wifi_ifnames
142 remap wan_ifname
143 remap wan_ifnames
144 remap pppoe_ifname
145 }
146
147 # failsafe if reset is held
148 [ "$FAILSAFE" = "true" ] && {
149 lan_ifname="br0"
150 lan_ifnames="vlan0 vlan2 eth1 eth2 eth3"
151 lan_ipaddr="192.168.1.1"
152 lan_netmask="255.255.255.0"
153 lan_hwaddr="00:0B:AD:0A:DD:00"
154 wan_ifname="none"
155 wifi_ifname="none"
156 }
157
158 # linksys bug has lan doing dhcp; force static
159 lan_proto="static"
160
161 configure lan
162 configure wifi
163 configure wan