inital commit of 20040316
[openwrt.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 rem vlan${i}
35     $DEBUG vconfig add $vif $i
36   }
37   ifconfig "$1" >/dev/null 2>&1 || [ "${1%[0-9]}" = "br" ] 
38   return $?
39 }
40
41 wifi_init () {
42   echo "# --- wifi init ---"
43   hwaddr=$(nvram_get il0macaddr)
44   [ -z "$hwaddr" ] && hwaddr=$(nvram_get wl0_hwaddr)
45   if=$(mac2if $hwaddr)
46   $DEBUG wlconf $if up
47 }
48
49 configure () {
50   type=$1
51   echo "# --- $type ---"
52   
53   if=$(nvram_get ${type}_ifname)
54   if [ "${if%[0-9]}" = "ppp" ]; then
55     if=$(nvram get pppoe_ifname) 
56   fi
57   if_valid $if || return
58   
59   if [ "${if%[0-9]}" = "br" ]; then
60     $DEBUG ifconfig $if down
61     $DEBUG brctl delbr $if
62     $DEBUG brctl addbr $if
63     $DEBUG brctl setfd $if 0
64     if_list=$(nvram_get ${type}_ifnames)
65     for sif in $if_list; do {
66       if_valid $sif || continue
67       $DEBUG ifconfig $sif 0.0.0.0 up
68       $DEBUG brctl addif $if $sif 
69     }; done
70   fi
71
72   if_mac=$(nvram_get ${type}_hwaddr)
73   $DEBUG ifconfig $if hw ether $if_mac
74  
75   if_proto=$(nvram_get ${type}_proto)
76   case "$if_proto" in
77     static)
78       if_ip=$(nvram_get ${type}_ipaddr)
79       if_netmask=$(nvram_get ${type}_netmask)
80       if_gateway=$(nvram_get ${type}_gateway)
81       
82       ipcalc -s "$if_ip"      || return 
83       ipcalc -s "$if_netmask" || return 
84       $DEBUG ifconfig $if $if_ip netmask $if_netmask up
85
86       ipcalc -s "$ip_gateway" || return 
87       $DEBUG route add default gw $ip_gateway
88     ;;
89     dhcp)
90       pidfile=/tmp/dhcp-${type}.pid
91       if [ -f $pidfile ]; then
92         $DEBUG kill $(cat $pidfile)
93       fi
94       $DEBUG udhcpc -i $if -b -p /tmp/dhcp-${type}.pid
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 pppd user "$if_username" password "$if_password" defaultroute  
105     ;;
106     *)
107       echo "$if: $if_proto is not supported"
108     ;;
109   esac
110 }
111
112 ### START NETWORKING ###
113 wifi_init
114
115 $DEBUG vconfig set_name_type VLAN_PLUS_VID_NO_PAD
116
117 # hacks for 1.x hardware
118 [ -z "$(nvram_get vlan0hwname)" ] && {
119   echo "# 1.x HACK"
120   vlan1hwname="et0"
121   vlan2hwname="et0"
122
123   # we remap old device names to new
124   # it's recommended that you continue to
125   # use the old names to preserve backwards
126   # compatibility
127   remap () {
128     eval $1=\"$(nvram_get $1 | awk '{
129           gsub(/eth0/,"vlan2")
130           gsub(/eth1/,"vlan1")
131           print $0
132     }')\"
133   }
134
135   remap lan_ifname
136   remap lan_ifnames
137   remap wifi_ifname
138   remap wifi_ifnames
139   remap wan_ifname
140   remap wan_ifnames
141   remap pppoe_ifname
142 }
143
144 # failsafe if reset is held 
145 [ "$FAILSAFE" = "true" ] && {
146   lan_ifname="br0"
147   lan_ifnames="vlan0 vlan2 eth1 eth2 eth3"
148   lan_ipaddr="192.168.1.1"
149   lan_netmask="255.255.255.0"
150   lan_hwaddr="00:0B:AD:0A:DD:00"
151   wan_ifname="none"
152   wifi_ifname="none"
153 }
154
155 # linksys bug has lan doing dhcp; force static
156 lan_proto="static"
157
158 configure lan
159 configure wifi
160 configure wan