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