3839f9f106c5860b1fb568b99fb9749fcabcd7d1
[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                 if (system("[ -d /proc/switch/eth0 ] ") == 0) {
95                         getline < "/proc/switch/eth0/cpuport"
96                         cpuport=$0
97                         if (cpuport == "8") {
98                                 c["vlan0ports"]="1 2 3 4 8"
99                                 c["vlan1ports"]="0 8"
100                         }
101                 }
102         }
103
104         ($1 == "boardnum") || ($1 == "boardtype") || ($1 == "boardflags") || ($1 ~ /macaddr/) || \
105          ($1 ~ /^vlan[0-9]{1,2}ports$/) {
106                 nvram[$1] = $2
107         }
108
109         END {
110                 if (((nvram["vlan0ports"] ~ /^0 1 2 3 8/ ) && (nvram["vlan1ports"] ~ /^4 8/ && (cpuport == "8"))) || \
111                     ((nvram["vlan1ports"] ~ /^0 1 2 3 8/ ) && (nvram["vlan2ports"] ~ /^4 8/ && (cpuport == "8")))) {
112                         c["vlan0ports"] = "0 1 2 3 8"
113                         c["vlan1ports"] = "4 8"
114                 }
115                 if (((nvram["vlan0ports"] ~ /^0 1 2 3 5/ ) && (nvram["vlan1ports"] ~ /^4 5/ && (cpuport == "5"))) || \
116                     ((nvram["vlan1ports"] ~ /^0 1 2 3 5/ ) && (nvram["vlan2ports"] ~ /^4 5/ && (cpuport == "5")))) {
117                         c["vlan0ports"] = "0 1 2 3 5"
118                         c["vlan1ports"] = "4 5"
119                 }
120                 if ((model == "ASUS WL-HDD") || (model == "ASUS WL-300g") || (model == "Linksys WAP54G V1")) {
121                         c["wan_ifname"] = ""
122                         c["lan_ifname"] = "eth1"
123                 }
124                 if (model == "ASUS WL-330gE") {
125                         c["wan_ifname"] = ""
126                         c["lan_ifname"] = "eth0.0"
127                         c["vlan0ports"] = "4 5"
128                         c["vlan1ports"] = ""
129                 }
130                 if ((model == "ASUS WL-500g") || (model == "Microsoft MN-700")) {
131                         c["wan_ifname"] = "eth1"
132                         c["lan_ifname"] = "eth0"
133                 }
134                 if ((model == "ASUS WL-500g Premium V2") || (model == "Dell TrueMobile 2300 v2") || (model == "Buffalo WHR-G125")) {
135                         c["vlan0ports"] = "0 1 2 3 5"
136                         c["vlan1ports"] = "4 5"
137                 }
138                 if (model == "Dell TrueMobile 2300") {
139                         c["lan_ifname"] = "eth0"
140                         c["wan_ifname"] = "eth1"
141                         c["vlan0ports"] = "0 1 2 3 4 5u"
142                         c["vlan1ports"] = ""
143                 }
144                 if (nvram["boardtype"] == "bcm94710r4") {
145                         # Toshiba WRC-1000
146                         c["lan_ifname"] = "eth0"
147                         c["wan_ifname"] = "eth1"
148                 }
149                 if ((nvram["boardtype"] == "wgt634u") || (nvram["boardtype"] == "0x0467")) {
150                         c["vlan0ports"] = "0 1 2 3 5"
151                         c["vlan1ports"] = "4 5"
152                 }
153                 if ((nvram["boardtype"] == "0x042f") || (nvram["boardtype"] == "0x0472")) {
154                         if (nvram["boardnum"] == "45") {
155                                 # WL-500gP
156                                 c["vlan0ports"] = "1 2 3 4 5"
157                                 c["vlan1ports"] = "0 5"
158                         } else {
159                                 # Generic BCM94704
160                                 c["vlan0ports"] = "0 1 2 3 4 5u"
161                                 c["vlan1ports"] = ""
162                                 c["lan_ifname"] = "eth0"
163                                 c["wan_ifname"] = "eth1"
164
165                                 # MAC addresses on 4704 tend to be screwed up. Add a workaround here
166                                 if (nvram["et0macaddr"] ~ mac_check) {
167                                         c["lan_macaddr"] = nvram["et0macaddr"]
168                                         c["wan_macaddr"] = macinc(c["lan_macaddr"])
169                                 }
170                         }
171                 }
172                 # Buffalo WBR-B11 and Buffalo WBR-G54
173                 if (nvram["boardtype"] == "bcm94710ap") {
174                         c["vlan0ports"] = "0 1 2 3 4 5u"
175                         c["vlan1ports"] = ""
176                         c["lan_ifname"] = "eth0"
177                         c["wan_ifname"] = "eth1"
178                 }
179
180                 # WAP54G
181                 if ((nvram["boardnum"] == "2") || \
182                         (nvram["boardnum"] == "1024")) {
183                         c["lan_ifname"]="eth0"
184                         c["wan_ifname"]=""
185                 }
186
187                 # Sitecom WL-105b
188                 if ((nvram["boardum"] == "2") && \
189                         (nvram["GemtekPmonVer"] == "1")) {
190                         c["lan_ifname"]="eth0"
191                         c["wan_ifname"]=""
192                 }
193
194                 # ASUS WL-700gE
195                 # These are actually same as defaults above. For some reason this script applies
196                 # Generic BCM94704 settings instead so we revert to proper settings here.
197                 # Hopefully someone will fix this properly soon.
198                 if (model == "ASUS WL-700gE") {
199                         c["lan_ifname"]="eth0.0"
200                         c["wan_ifname"]="eth0.1"
201                         c["vlan0ports"]="1 2 3 4 5"
202                         c["vlan1ports"]="0 5"
203                 }
204                 if ((model == "Motorola WR850G V2/V3") || (model == "Siemens SE505 V2")) {
205                         c["vlan0ports"]="0 1 2 3 5"
206                         c["vlan1ports"]="4 5"
207                 }
208                 if (model == "ASUS WL-500W") {
209                         c["lan_ifname"] = "eth0"
210                         c["wan_ifname"] = "eth1"
211                         c["vlan0ports"] = "0 1 2 3 4 5u"
212                         c["vlan1ports"] = ""
213                 }
214                 if (model == "OvisLink WL-1600GL") {
215                         c["lan_ifname"] = "eth0.0"
216                         c["wan_ifname"] = "eth0.1"
217                         c["vlan0ports"] = "0 1 2 3 5"
218                         c["vlan1ports"] = "4 5"
219                 }
220                 if (model == "SimpleTech SimpleShare NAS") {
221                         c["lan_ifname"] = "eth0"
222                         c["wan_ifname"] = ""
223                         c["vlan0ports"] = ""
224                         c["vlan1ports"] = ""
225                 }
226
227                 if (c["vlan0ports"] || c["vlan1ports"]) {
228                         print "#### VLAN configuration "
229                         print "config switch eth0"
230                         print " option enable   1"
231                         print ""
232                         vlan(0, "vlan0ports")
233                         vlan(1, "vlan1ports")
234                 }
235                 print "#### Loopback configuration"
236                 print "config interface loopback"
237                 print " option ifname   \"lo\""
238                 print " option proto    static"
239                 print " option ipaddr   127.0.0.1"
240                 print " option netmask  255.0.0.0"
241                 print ""
242                 print ""
243                 print "#### LAN configuration"
244                 print "config interface lan"
245                 print " option type     bridge"
246                 p("ifname", "lan_ifname")
247                 p("macaddr", "lan_macaddr")
248                 print " option proto    static"
249                 print " option ipaddr   192.168.1.1"
250                 print " option netmask  255.255.255.0"
251                 print ""
252                 print ""
253                 if (c["wan_ifname"]) {
254                         print "#### WAN configuration"
255                         print "config interface wan"
256                         p("ifname", "wan_ifname")
257                         p("macaddr", "wan_macaddr")
258                         print " option proto    dhcp"
259                 } else {
260                         print "#### WAN configuration (disabled)"
261                         print "#config interface wan"
262                         print "#        option proto    dhcp"
263                 }
264         }' > /etc/config/network
265 }