brcm47xx: get switch name from swconfig first
[openwrt.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 WL-HDD") || (model == "ASUS WL-300g") || (model == "Linksys WAP54G V1")) {
109                         c["wan_ifname"] = ""
110                         c["lan_ifname"] = "eth1"
111                 }
112                 if (model == "ASUS WL-330gE") {
113                         c["wan_ifname"] = ""
114                         c["lan_ifname"] = "eth0.1"
115                         c["vlan1ports"] = "4 5t"
116                         c["vlan2ports"] = ""
117                 }
118                 if ((model == "ASUS WL-500g") || (model == "Microsoft MN-700")) {
119                         c["wan_ifname"] = "eth1"
120                         c["lan_ifname"] = "eth0"
121                 }
122                 if ((model == "ASUS WL-500g Premium V2") || (model == "Dell TrueMobile 2300 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 WL-700gE") {
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 V2/V3") || (model == "Siemens SE505 V2")) {
193                         c["vlan1ports"]="0 1 2 3 5t"
194                         c["vlan2ports"]="4 5t"
195                 }
196                 if (model == "ASUS WL-500W") {
197                         c["lan_ifname"] = "eth0"
198                         c["wan_ifname"] = "eth1"
199                         c["vlan1ports"] = "0 1 2 3 4 5"
200                         c["vlan2ports"] = ""
201                 }
202                 if (model == "OvisLink WL-1600GL") {
203                         c["lan_ifname"] = "eth0.1"
204                         c["wan_ifname"] = "eth0.2"
205                         c["vlan1ports"] = "0 1 2 3 5t"
206                         c["vlan2ports"] = "4 5t"
207                 }
208                 if (model == "SimpleTech SimpleShare NAS") {
209                         c["lan_ifname"] = "eth0"
210                         c["wan_ifname"] = ""
211                         c["vlan1ports"] = ""
212                         c["vlan2ports"] = ""
213                 }
214                 print "local vlan1ports=\"" c["vlan1ports"] "\";"
215                 print "local vlan2ports=\"" c["vlan2ports"] "\";"
216                 print "local lan_ifname=\"" c["lan_ifname"] "\";"
217                 print "local lan_macaddr=\"" c["lan_macaddr"] "\";"
218                 print "local wan_ifname=\"" c["wan_ifname"] "\";"
219                 print "local wan_macaddr=\"" c["wan_macaddr"] "\";"
220         }'`
221
222         . /lib/functions/uci-defaults.sh
223
224         touch /etc/config/network
225
226         eval "$network_defs"
227
228         [ -n "$vlan1ports" -o -n "$vlan2ports" ] && {
229                 local cfg=`ucidef_add_switch "$switchname" 1 1`
230                 [ -n "$cfg" ] && uci rename network.$cfg=eth0
231                 [ -n "$vlan1ports" ] && {
232                         cfg=`ucidef_add_switch_vlan "$switchname" 1 "$vlan1ports"`
233                         [ -n "$cfg" ] && uci rename network.$cfg=eth0_1
234                 }
235                 [ -n "$vlan2ports" ] && {
236                         cfg=`ucidef_add_switch_vlan "$switchname" 2 "$vlan2ports"`
237                         [ -n "$cfg" ] && uci rename network.$cfg=eth0_2
238                 }
239         }
240
241         ucidef_set_interface_loopback
242
243         ucidef_set_interface_lan "$lan_ifname"
244         [ -n "$lan_macaddr" ] && ucidef_set_interface_macaddr lan "$lan_macaddr"
245
246         [ -n "$wan_ifname" ] && {
247                 ucidef_set_interface_wan "$wan_ifname"
248                 [ -n "$wan_macaddr" ] && ucidef_set_interface_macaddr wan "$wan_macaddr"
249         }
250
251         uci commit network
252 }