6eb967b14c80b080a2ee9147d3df339b7e4e0679
[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 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 "$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 ${if}_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 sleep 5
114 $DEBUG /sbin/route add default $if
115 ;;
116 *)
117 echo "$if: $if_proto is not supported"
118 ;;
119 esac
120 }
121
122 ### START NETWORKING ###
123 wifi_init
124
125 $DEBUG vconfig set_name_type VLAN_PLUS_VID_NO_PAD
126
127 # hacks for 1.x hardware
128 [ -z "$(nvram_get vlan0hwname)" ] && {
129 echo "# 1.x HACK"
130 vlan1hwname="et0"
131 vlan2hwname="et0"
132
133 # we remap old device names to new
134 # it's recommended that you continue to
135 # use the old names to preserve backwards
136 # compatibility
137 remap () {
138 eval $1=\"$(nvram_get $1 | awk '{
139 gsub(/eth0/,"vlan2")
140 gsub(/eth1/,"vlan1")
141 print $0
142 }')\"
143 }
144
145 remap lan_ifname
146 remap lan_ifnames
147 remap wifi_ifname
148 remap wifi_ifnames
149 remap wan_ifname
150 remap wan_ifnames
151 remap pppoe_ifname
152 }
153
154 # failsafe if reset is held
155 [ "$FAILSAFE" = "true" ] && {
156 lan_ifname="br0"
157 lan_ifnames="vlan0 vlan2 eth1 eth2 eth3"
158 lan_ipaddr="192.168.1.1"
159 lan_netmask="255.255.255.0"
160 lan_hwaddr="00:0B:AD:0A:DD:00"
161 wan_ifname="none"
162 wifi_ifname="none"
163 }
164
165 # linksys bug has lan doing dhcp; force static
166 lan_proto="static"
167
168 configure lan
169 configure wifi
170 configure wan
171
172 for route in $(nvram_get static_route); do {
173 ip=${route%%:*} route=${route#*:}
174 netmask=${route%%:*} route=${route#*:}
175 gateway=${route%%:*} route=${route#*:}
176 metric=${route%%:*} route=${route#*:}
177 if=${route%%:*}
178 $DEBUG route add -net $ip netmask $netmask gw $gateway metric $metric dev $if
179 } done
This page took 0.054117 seconds and 3 git commands to generate.