bb85511b8351c6f1080fd736ec997855bb1d48b9
[lede.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 | awk '{IGNORECASE=1} /^eth.*'$1'/ {print $1; exit}')
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 2>/dev/null
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   $DEBUG ifconfig $if down
59   if [ "${if%[0-9]}" = "br" ]; then
60     stp=$(nvram_get ${type}_stp)
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   [ -z "$if_mac" ] || $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 "$if_gateway" || return 
88       $DEBUG route add default gw $if_gateway
89
90       [ -f /etc/resolv.conf ] && return
91
92       echo "# --- creating /etc/resolv.conf ---"
93       for dns in $(nvram_get ${type}_dns); do {
94         echo "nameserver $dns" >> /etc/resolv.conf
95       }; done
96     ;;
97     dhcp)
98       pidfile=/tmp/dhcp-${type}.pid
99       if [ -f $pidfile ]; then
100         $DEBUG kill $(cat $pidfile)
101       fi
102       $DEBUG udhcpc -i $if -b -p /tmp/dhcp-${type}.pid
103     ;;
104     pppoe)
105       if_username=$(nvram_get ppp_username)
106       if_password=$(nvram_get ppp_passwd)
107       if_redial=$(nvram_get ppp_redialperiod)
108       if_idletime=$(nvram_get ppp_idletime)
109       
110       $DEBUG ifconfig $if 0.0.0.0 up
111       
112       $DEBUG /sbin/pppoecd $if -u $if_username -p $if_password -i 0 -I $if_redial -T $if_idletime -k
113     ;;
114     *)
115       echo "$if: $if_proto is not supported"
116     ;;
117   esac
118 }
119
120 ### START NETWORKING ###
121 wifi_init
122
123 $DEBUG vconfig set_name_type VLAN_PLUS_VID_NO_PAD
124
125 # hacks for 1.x hardware
126 [ -z "$(nvram_get vlan0hwname)" ] && {
127   echo "# 1.x HACK"
128   vlan1hwname="et0"
129   vlan2hwname="et0"
130
131   # we remap old device names to new
132   # it's recommended that you continue to
133   # use the old names to preserve backwards
134   # compatibility
135   remap () {
136     eval $1=\"$(nvram_get $1 | awk '{
137           gsub(/eth0/,"vlan2")
138           gsub(/eth1/,"vlan1")
139           print $0
140     }')\"
141   }
142
143   remap lan_ifname
144   remap lan_ifnames
145   remap wifi_ifname
146   remap wifi_ifnames
147   remap wan_ifname
148   remap wan_ifnames
149   remap pppoe_ifname
150 }
151
152 # failsafe if reset is held 
153 [ "$FAILSAFE" = "true" ] && {
154   lan_ifname="br0"
155   lan_ifnames="vlan0 vlan2 eth1 eth2 eth3"
156   lan_ipaddr="192.168.1.1"
157   lan_netmask="255.255.255.0"
158   lan_hwaddr="00:0B:AD:0A:DD:00"
159   wan_ifname="none"
160   wifi_ifname="none"
161 }
162
163 # linksys bug has lan doing dhcp; force static
164 lan_proto="static"
165
166 configure lan
167 configure wifi
168 configure wan
169
170 for route in $(nvram_get static_route); do {
171       ip=${route%%:*} route=${route#*:}
172  netmask=${route%%:*} route=${route#*:}
173  gateway=${route%%:*} route=${route#*:}
174   metric=${route%%:*} route=${route#*:}
175       if=${route%%:*}
176   $DEBUG route add -net $ip netmask $netmask gw $gateway metric $metric dev $if
177 } done