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