brcm47xx: don't create named switch_vlan sections
[openwrt/svn-archive/archive.git] / target / linux / brcm47xx / base-files / etc / init.d / netconfig
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2006 OpenWrt.org
3
4 START=05
5
6 start() {
7 [ -e /etc/config/network ] && {
8 local batch
9
10 config_cb() {
11 case "$1" in
12 switch)
13 option_cb() {
14 case "$1" in
15 vlan[0-9]|vlan1[0-5])
16 local id="${1#vlan}"
17 local ports="${2%\*}"
18 append batch "delete network.eth0.${1}${N}"
19 append batch "set network.eth0_${id}=switch_vlan${N}"
20 append batch "set network.eth0_${id}.device=eth0${N}"
21 append batch "set network.eth0_${id}.vlan=${id}${N}"
22 append batch "set network.eth0_${id}.ports='${ports}'${N}"
23 ;;
24 esac
25 }
26 ;;
27 switch_vlan)
28 option_cb() { :; }
29 batch=""
30 ;;
31 esac
32 }
33
34 config_load network
35
36 [ -n "$batch" ] && {
37 logger -t netconfig "migrating switch config to new format ..."
38 echo "$batch${N}commit network" | uci batch
39 }
40
41 exit 0
42 }
43
44 mkdir -p /etc/config
45
46 local cpuport=5
47 [ -e /sbin/swconfig ] && cpuport=$(swconfig dev switch0 help 2>/dev/null | sed -ne "s|.*cpu @ \([0-9]*\).*|\1|p")
48 local switchname=eth0
49 [ -e /sbin/swconfig ] && switchname=$(swconfig dev switch0 help 2>/dev/null | sed -ne "s|switch0: \([^\\\\(]*\).*|\1|p")
50 local model=$(awk 'BEGIN{FS="[ \t]+:[ \t]"} /machine/ {print $2}' /proc/cpuinfo)
51
52 local network_defs=`(
53 if grep -E 'mtd0: 000(6|a)0000' /proc/mtd 2>&- >&-; then
54 # WGT634u
55 echo boardtype=wgt634u
56 else
57 strings "$(find_mtd_part nvram)"
58 fi
59 ) | awk -v cpuport="$cpuport" -v model="$model" '
60 function macinc(mac, maca, i, result) {
61 split(mac, maca, ":")
62 for (i = 1; i <= 6; i++) maca[i] = "0x" maca[i]
63 if (++maca[6] > 0xff) {
64 maca[5]++
65 maca[6] = 0
66 }
67 for (i = 1; i <= 6; i++) {
68 if (i != 1) result = result ":"
69 result = result sprintf("%02x", maca[i])
70 }
71 return result
72 }
73
74 BEGIN {
75 FS="="
76 c["lan_ifname"]="eth0.1"
77 c["wan_ifname"]="eth0.2"
78 c["vlan1ports"]="1 2 3 4 5t"
79 c["vlan2ports"]="0 5t"
80 for (i = 0; i < 6; i++) {
81 if (mac_check != "") mac_check = mac_check ":"
82 mac_check = mac_check "[0-9a-fA-F][0-9a-fA-F]"
83 }
84 if (cpuport == "8") {
85 c["vlan1ports"]="1 2 3 4 8t"
86 c["vlan2ports"]="0 8t"
87 }
88 }
89
90 ($1 == "boardnum") || ($1 == "boardtype") || ($1 == "boardflags") || ($1 ~ /macaddr/) || \
91 ($1 ~ /^vlan[0-9]{1,2}ports$/) {
92 nvram[$1] = $2
93 }
94
95 END {
96 if (((nvram["vlan0ports"] ~ /^0 1 2 3 8/ ) && (nvram["vlan1ports"] ~ /^4 8/ && (cpuport == "8"))) || \
97 ((nvram["vlan1ports"] ~ /^0 1 2 3 8/ ) && (nvram["vlan2ports"] ~ /^4 8/ && (cpuport == "8"))) || \
98 ((nvram["vlan2ports"] ~ /^0 1 2 3 8/ ) && (nvram["vlan1ports"] ~ /^4 8/ && (cpuport == "8")))) {
99 c["vlan1ports"] = "0 1 2 3 8t"
100 c["vlan2ports"] = "4 8t"
101 }
102 if (((nvram["vlan0ports"] ~ /^0 1 2 3 5/ ) && (nvram["vlan1ports"] ~ /^4 5/ && (cpuport == "5"))) || \
103 ((nvram["vlan1ports"] ~ /^0 1 2 3 5/ ) && (nvram["vlan2ports"] ~ /^4 5/ && (cpuport == "5"))) || \
104 ((nvram["vlan2ports"] ~ /^0 1 2 3 5/ ) && (nvram["vlan1ports"] ~ /^4 5/ && (cpuport == "5")))) {
105 c["vlan1ports"] = "0 1 2 3 5t"
106 c["vlan2ports"] = "4 5t"
107 }
108 if ((model == "Asus WLHDD") || (model == "Asus WL300G")) {
109 c["wan_ifname"] = ""
110 c["lan_ifname"] = "eth1"
111 }
112 if (model == "Asus WL330GE") {
113 c["wan_ifname"] = ""
114 c["lan_ifname"] = "eth0.1"
115 c["vlan1ports"] = "4 5t"
116 c["vlan2ports"] = ""
117 }
118 if ((model == "Asus WL500G") || (model == "Microsoft MN-700")) {
119 c["wan_ifname"] = "eth1"
120 c["lan_ifname"] = "eth0"
121 }
122 if ((model == "Asus WL500GP V2") || (model == "Buffalo WHR-G125")) {
123 c["vlan1ports"] = "0 1 2 3 5t"
124 c["vlan2ports"] = "4 5t"
125 }
126 if (model == "Dell TrueMobile 2300") {
127 c["lan_ifname"] = "eth0"
128 c["wan_ifname"] = "eth1"
129 c["vlan1ports"] = "0 1 2 3 4 5"
130 c["vlan2ports"] = ""
131 }
132 if (nvram["boardtype"] == "bcm94710r4") {
133 # Toshiba WRC-1000
134 c["lan_ifname"] = "eth0"
135 c["wan_ifname"] = "eth1"
136 }
137 if ((nvram["boardtype"] == "wgt634u") || (nvram["boardtype"] == "0x0467")) {
138 c["vlan1ports"] = "0 1 2 3 5t"
139 c["vlan2ports"] = "4 5t"
140 }
141 if ((nvram["boardtype"] == "0x042f") || (nvram["boardtype"] == "0x0472")) {
142 if (nvram["boardnum"] == "45") {
143 # WL-500gP
144 c["vlan1ports"] = "1 2 3 4 5t"
145 c["vlan2ports"] = "0 5t"
146 } else {
147 # Generic BCM94704
148 c["vlan1ports"] = "0 1 2 3 4 5"
149 c["vlan2ports"] = ""
150 c["lan_ifname"] = "eth0"
151 c["wan_ifname"] = "eth1"
152
153 # MAC addresses on 4704 tend to be screwed up. Add a workaround here
154 if (nvram["et0macaddr"] ~ mac_check) {
155 c["lan_macaddr"] = nvram["et0macaddr"]
156 c["wan_macaddr"] = macinc(c["lan_macaddr"])
157 }
158 }
159 }
160 # Buffalo WBR-B11 and Buffalo WBR-G54
161 if (nvram["boardtype"] == "bcm94710ap") {
162 c["vlan1ports"] = "0 1 2 3 4 5"
163 c["vlan2ports"] = ""
164 c["lan_ifname"] = "eth0"
165 c["wan_ifname"] = "eth1"
166 }
167
168 # WAP54G
169 if ((nvram["boardnum"] == "2") || \
170 (nvram["boardnum"] == "1024")) {
171 c["lan_ifname"]="eth0"
172 c["wan_ifname"]=""
173 }
174
175 # Sitecom WL-105b
176 if ((nvram["boardum"] == "2") && \
177 (nvram["GemtekPmonVer"] == "1")) {
178 c["lan_ifname"]="eth0"
179 c["wan_ifname"]=""
180 }
181
182 # ASUS WL-700gE
183 # These are actually same as defaults above. For some reason this script applies
184 # Generic BCM94704 settings instead so we revert to proper settings here.
185 # Hopefully someone will fix this properly soon.
186 if (model == "Asus WL700") {
187 c["lan_ifname"]="eth0.1"
188 c["wan_ifname"]="eth0.2"
189 c["vlan1ports"]="1 2 3 4 5t"
190 c["vlan2ports"]="0 5t"
191 }
192 if ((model == "Motorola WR850G") || (model == "Siemens SE505 V2")) {
193 c["vlan1ports"]="0 1 2 3 5t"
194 c["vlan2ports"]="4 5t"
195 }
196 if (model == "Asus WL500W") {
197 c["lan_ifname"] = "eth0"
198 c["wan_ifname"] = "eth1"
199 c["vlan1ports"] = "0 1 2 3 4 5"
200 c["vlan2ports"] = ""
201 }
202 print "local vlan1ports=\"" c["vlan1ports"] "\";"
203 print "local vlan2ports=\"" c["vlan2ports"] "\";"
204 print "local lan_ifname=\"" c["lan_ifname"] "\";"
205 print "local lan_macaddr=\"" c["lan_macaddr"] "\";"
206 print "local wan_ifname=\"" c["wan_ifname"] "\";"
207 print "local wan_macaddr=\"" c["wan_macaddr"] "\";"
208 }'`
209
210 . /lib/functions/uci-defaults.sh
211
212 touch /etc/config/network
213
214 eval "$network_defs"
215
216 [ -n "$vlan1ports" -o -n "$vlan2ports" ] && {
217 local cfg=`ucidef_add_switch "$switchname" 1 1`
218 [ -n "$cfg" ] && uci rename network.$cfg=eth0
219 [ -n "$vlan1ports" ] && {
220 cfg=`ucidef_add_switch_vlan "$switchname" 1 "$vlan1ports"`
221 }
222 [ -n "$vlan2ports" ] && {
223 cfg=`ucidef_add_switch_vlan "$switchname" 2 "$vlan2ports"`
224 }
225 }
226
227 ucidef_set_interface_loopback
228
229 ucidef_set_interface_lan "$lan_ifname"
230 [ -n "$lan_macaddr" ] && ucidef_set_interface_macaddr lan "$lan_macaddr"
231
232 [ -n "$wan_ifname" ] && {
233 ucidef_set_interface_wan "$wan_ifname"
234 [ -n "$wan_macaddr" ] && ucidef_set_interface_macaddr wan "$wan_macaddr"
235 }
236
237 uci commit network
238 }