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