brcm47xx: Additional BCM4716 support
[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         (
47                 if grep -E 'mtd0: 000(6|a)0000' /proc/mtd 2>&- >&-; then
48                         # WGT634u
49                         echo boardtype=wgt634u
50                 else
51                         strings "$(find_mtd_part nvram)"
52                 fi
53         ) | awk '
54         function p(cfgname, name) {
55                 if (c[name] != "") print "      option " cfgname "      \"" c[name] "\""
56         }
57
58         function vlan(id, name) {
59                 if (c[name] != "") {
60                         print "config switch_vlan eth0_" id
61                         print " option device   \"eth0\""
62                         print " option vlan     " id
63                         print " option ports    \"" c[name] "\""
64                         print ""
65                 }
66         }
67
68         function macinc(mac, maca, i, result) {
69                 split(mac, maca, ":")
70                 for (i = 1; i <= 6; i++) maca[i] = "0x" maca[i]
71                 if (++maca[6] > 0xff) {
72                         maca[5]++
73                         maca[6] = 0
74                 }
75                 for (i = 1; i <= 6; i++) {
76                         if (i != 1) result = result ":"
77                         result = result sprintf("%02x", maca[i])
78                 }
79                 return result
80         }
81
82         BEGIN {
83                 FS="="
84                 c["lan_ifname"]="eth0.0"
85                 c["wan_ifname"]="eth0.1"
86                 c["vlan0ports"]="1 2 3 4 5"
87                 c["vlan1ports"]="0 5"
88                 getline < "/proc/diag/model"
89                 model=$0
90                 for (i = 0; i < 6; i++) {
91                         if (mac_check != "") mac_check = mac_check ":"
92                         mac_check = mac_check "[0-9a-fA-F][0-9a-fA-F]"
93                 }
94         }
95
96         ($1 == "boardnum") || ($1 == "boardtype") || ($1 == "boardflags") || ($1 ~ /macaddr/) {
97                 nvram[$1] = $2
98         }
99
100         END {
101                 if ((model == "ASUS WL-HDD") || (model == "ASUS WL-300g") || (model == "Linksys WAP54G V1")) {
102                         c["wan_ifname"] = ""
103                         c["lan_ifname"] = "eth1"
104                 }
105                 if (model == "ASUS WL-330gE") {
106                         c["wan_ifname"] = ""
107                         c["lan_ifname"] = "eth0.0"
108                         c["vlan0ports"] = "4 5"
109                         c["vlan1ports"] = ""
110                 }
111                 if ((model == "ASUS WL-500g") || (model == "Microsoft MN-700")) {
112                         c["wan_ifname"] = "eth1"
113                         c["lan_ifname"] = "eth0"
114                 }
115                 if ((model == "ASUS WL-500g Premium V2") || (model == "Dell TrueMobile 2300 v2") || (model == "Buffalo WHR-G125")) {
116                         c["vlan0ports"] = "0 1 2 3 5"
117                         c["vlan1ports"] = "4 5"
118                 }
119                 if (model == "Dell TrueMobile 2300") {
120                         c["lan_ifname"] = "eth0"
121                         c["wan_ifname"] = "eth1"
122                         c["vlan0ports"] = "0 1 2 3 4 5u"
123                         c["vlan1ports"] = ""
124                 }
125                 if (nvram["boardtype"] == "bcm94710r4") {
126                         # Toshiba WRC-1000
127                         c["lan_ifname"] = "eth0"
128                         c["wan_ifname"] = "eth1"
129                 }
130                 if ((nvram["boardtype"] == "wgt634u") || (nvram["boardtype"] == "0x0467")) {
131                         c["vlan0ports"] = "0 1 2 3 5"
132                         c["vlan1ports"] = "4 5"
133                 }
134                 if ((nvram["boardtype"] == "0x042f") || (nvram["boardtype"] == "0x0472")) {
135                         if (nvram["boardnum"] == "45") {
136                                 # WL-500gP
137                                 c["vlan0ports"] = "1 2 3 4 5"
138                                 c["vlan1ports"] = "0 5"
139                         } else {
140                                 # Generic BCM94704
141                                 c["vlan0ports"] = "0 1 2 3 4 5u"
142                                 c["vlan1ports"] = ""
143                                 c["lan_ifname"] = "eth0"
144                                 c["wan_ifname"] = "eth1"
145
146                                 # MAC addresses on 4704 tend to be screwed up. Add a workaround here
147                                 if (nvram["et0macaddr"] ~ mac_check) {
148                                         c["lan_macaddr"] = nvram["et0macaddr"]
149                                         c["wan_macaddr"] = macinc(c["lan_macaddr"])
150                                 }
151                         }
152                 }
153                 # Buffalo WBR-B11 and Buffalo WBR-G54
154                 if (nvram["boardtype"] == "bcm94710ap") {
155                         c["vlan0ports"] = "0 1 2 3 4 5u"
156                         c["vlan1ports"] = ""
157                         c["lan_ifname"] = "eth0"
158                         c["wan_ifname"] = "eth1"
159                 }
160                 # generic broadcom 4705/4785 processor with 5397 switch?
161                 #  EXCEPT Linksys WRT300N V1.1
162                 if ((nvram["boardtype"] == "0x478") && \
163                     (model != "Linksys WRT300N V1.1")) {
164                         c["vlan0ports"] = "1 2 3 4 8*"
165                         c["vlan1ports"] = "0 8"
166                 }
167
168                 # generic broadcom 4716 processor with 53115 switch
169                 if (nvram["boardtype"] == "0x04cf") {
170                         c["vlan0ports"] = "1 2 3 4 8*"
171                         c["vlan1ports"] = "0 8"
172                 }
173
174                 # WAP54G
175                 if ((nvram["boardnum"] == "2") || \
176                         (nvram["boardnum"] == "1024")) {
177                         c["lan_ifname"]="eth0"
178                         c["wan_ifname"]=""
179                 }
180
181                 # Sitecom WL-105b
182                 if ((nvram["boardum"] == "2") && \
183                         (nvram["GemtekPmonVer"] == "1")) {
184                         c["lan_ifname"]="eth0"
185                         c["wan_ifname"]=""
186                 }
187
188                 # ASUS WL-700gE
189                 # These are actually same as defaults above. For some reason this script applies
190                 # Generic BCM94704 settings instead so we revert to proper settings here.
191                 # Hopefully someone will fix this properly soon.
192                 if (model == "ASUS WL-700gE") {
193                         c["lan_ifname"]="eth0.0"
194                         c["wan_ifname"]="eth0.1"
195                         c["vlan0ports"]="1 2 3 4 5"
196                         c["vlan1ports"]="0 5"
197                 }
198                 if ((model == "Motorola WR850G V2/V3") || (model == "Siemens SE505 V2")) {
199                         c["vlan0ports"]="0 1 2 3 5"
200                         c["vlan1ports"]="4 5"
201                 }
202                 if (model == "ASUS WL-500W") {
203                         c["lan_ifname"] = "eth0"
204                         c["wan_ifname"] = "eth1"
205                         c["vlan0ports"] = "0 1 2 3 4 5u"
206                         c["vlan1ports"] = ""
207                 }
208                 if (model == "OvisLink WL-1600GL") {
209                         c["lan_ifname"] = "eth0.0"
210                         c["wan_ifname"] = "eth0.1"
211                         c["vlan0ports"] = "0 1 2 3 5"
212                         c["vlan1ports"] = "4 5"
213                 }
214
215                 if (c["vlan0ports"] || c["vlan1ports"]) {
216                         print "#### VLAN configuration "
217                         print "config switch eth0"
218                         print " option enable   1"
219                         print ""
220                         vlan(0, "vlan0ports")
221                         vlan(1, "vlan1ports")
222                 }
223                 print "#### Loopback configuration"
224                 print "config interface loopback"
225                 print " option ifname   \"lo\""
226                 print " option proto    static"
227                 print " option ipaddr   127.0.0.1"
228                 print " option netmask  255.0.0.0"
229                 print ""
230                 print ""
231                 print "#### LAN configuration"
232                 print "config interface lan"
233                 print " option type     bridge"
234                 p("ifname", "lan_ifname")
235                 p("macaddr", "lan_macaddr")
236                 print " option proto    static"
237                 print " option ipaddr   192.168.1.1"
238                 print " option netmask  255.255.255.0"
239                 print ""
240                 print ""
241                 if (c["wan_ifname"]) {
242                         print "#### WAN configuration"
243                         print "config interface wan"
244                         p("ifname", "wan_ifname")
245                         p("macaddr", "wan_macaddr")
246                         print " option proto    dhcp"
247                 } else {
248                         print "#### WAN configuration (disabled)"
249                         print "#config interface wan"
250                         print "#        option proto    dhcp"
251                 }
252         }' > /etc/config/network
253 }