[brcm-2.4] add migration code to automatically convert old to new switch config on...
[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_${n}=switch_vlan${N}"
19                                                 append batch "set network.eth0_${n}.device=eth0${N}"
20                                                 append batch "set network.eth0_${n}.vlan=${id}${N}"
21                                                 append batch "set network.eth0_${n}.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") {
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                 if (nvram["boardtype"] == "0x478") {
168                         # generic broadcom 4785 processor with 5397 switch?
169                         c["vlan0ports"] = "1 2 3 4 8*"
170                         c["vlan1ports"] = "0 8"
171                 }
172
173                 # WAP54G
174                 if ((nvram["boardnum"] == "2") || \
175                         (nvram["boardnum"] == "1024")) {
176                         c["lan_ifname"]="eth0"
177                         c["wan_ifname"]=""
178                 }
179
180                 # Sitecom WL-105b
181                 if ((nvram["boardum"] == "2") && \
182                         (nvram["GemtekPmonVer"] == "1")) {
183                         c["lan_ifname"]="eth0"
184                         c["wan_ifname"]=""
185                 }
186
187                 # ASUS WL-700gE
188                 # These are actually same as defaults above. For some reason this script applies
189                 # Generic BCM94704 settings instead so we revert to proper settings here.
190                 # Hopefully someone will fix this properly soon.
191                 if (model == "ASUS WL-700gE") {
192                         c["lan_ifname"]="eth0.0"
193                         c["wan_ifname"]="eth0.1"
194                         c["vlan0ports"]="1 2 3 4 5*"
195                         c["vlan1ports"]="0 5"
196                 }
197
198                 if (model == "Motorola WR850G V2/V3") {
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
209                 if (c["vlan0ports"] || c["vlan1ports"]) {
210                         print "#### VLAN configuration "
211                         print "config switch eth0"
212                         print " option enable   1"
213                         print ""                
214                         vlan(0, "vlan0ports")
215                         vlan(1, "vlan1ports")
216                 }
217                 print "#### Loopback configuration"
218                 print "config interface loopback"
219                 print " option ifname   \"lo\""
220                 print " option proto    static"
221                 print " option ipaddr   127.0.0.1"
222                 print " option netmask  255.0.0.0"
223                 print ""
224                 print ""
225                 print "#### LAN configuration"
226                 print "config interface lan"
227                 print " option type     bridge"
228                 p("ifname", "lan_ifname")
229                 p("macaddr", "lan_macaddr")
230                 print " option proto    static"
231                 print " option ipaddr   192.168.1.1"
232                 print " option netmask  255.255.255.0"
233                 print ""
234                 print ""
235                 if (c["wan_ifname"]) {
236                         print "#### WAN configuration"
237                         print "config interface wan"
238                         p("ifname", "wan_ifname")
239                         p("macaddr", "wan_macaddr")
240                         print " option proto    dhcp"
241                 } else {
242                         print "#### WAN configuration (disabled)"
243                         print "#config interface wan"
244                         print "#        option proto    dhcp"
245                 }
246         }' > /etc/config/network
247 }