netconfig 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2006 OpenWrt.org
  3. START=05
  4. start() {
  5. [ -e /etc/config/network ] && {
  6. local batch
  7. config_cb() {
  8. case "$1" in
  9. switch)
  10. option_cb() {
  11. case "$1" in
  12. vlan[0-9]|vlan1[0-5])
  13. local id="${1#vlan}"
  14. local ports="${2%\*}"
  15. append batch "delete network.eth0.${1}${N}"
  16. append batch "set network.eth0_${id}=switch_vlan${N}"
  17. append batch "set network.eth0_${id}.device=eth0${N}"
  18. append batch "set network.eth0_${id}.vlan=${id}${N}"
  19. append batch "set network.eth0_${id}.ports='${ports}'${N}"
  20. ;;
  21. esac
  22. }
  23. ;;
  24. switch_vlan)
  25. option_cb() { :; }
  26. batch=""
  27. ;;
  28. esac
  29. }
  30. config_load network
  31. [ -n "$batch" ] && {
  32. logger -t netconfig "migrating switch config to new format ..."
  33. echo "$batch${N}commit network" | uci batch
  34. }
  35. exit 0
  36. }
  37. mkdir -p /etc/config
  38. local cpuport=5
  39. [ -e /sbin/swconfig ] && cpuport=$(swconfig dev switch0 help 2>/dev/null | sed -ne "s|.*cpu @ \([0-9]*\).*|\1|p")
  40. local switchname=eth0
  41. [ -e /sbin/swconfig ] && switchname=$(swconfig dev switch0 help 2>/dev/null | sed -ne "s|switch0: \([^\\\\(]*\).*|\1|p")
  42. local model=$(awk 'BEGIN{FS="[ \t]+:[ \t]"} /machine/ {print $2}' /proc/cpuinfo)
  43. local network_defs=`(
  44. if grep -E 'mtd0: 000(6|a)0000' /proc/mtd 2>&- >&-; then
  45. # WGT634u
  46. echo boardtype=wgt634u
  47. else
  48. strings "$(find_mtd_part nvram)"
  49. fi
  50. ) | awk -v cpuport="$cpuport" -v model="$model" '
  51. function macinc(mac, maca, i, result) {
  52. split(mac, maca, ":")
  53. for (i = 1; i <= 6; i++) maca[i] = "0x" maca[i]
  54. if (++maca[6] > 0xff) {
  55. maca[5]++
  56. maca[6] = 0
  57. }
  58. for (i = 1; i <= 6; i++) {
  59. if (i != 1) result = result ":"
  60. result = result sprintf("%02x", maca[i])
  61. }
  62. return result
  63. }
  64. BEGIN {
  65. FS="="
  66. c["lan_ifname"]="eth0.1"
  67. c["wan_ifname"]="eth0.2"
  68. c["vlan1ports"]="1 2 3 4 5t"
  69. c["vlan2ports"]="0 5t"
  70. for (i = 0; i < 6; i++) {
  71. if (mac_check != "") mac_check = mac_check ":"
  72. mac_check = mac_check "[0-9a-fA-F][0-9a-fA-F]"
  73. }
  74. if (cpuport == "8") {
  75. c["vlan1ports"]="1 2 3 4 8t"
  76. c["vlan2ports"]="0 8t"
  77. }
  78. }
  79. ($1 == "boardnum") || ($1 == "boardtype") || ($1 == "boardflags") || ($1 ~ /macaddr/) || \
  80. ($1 ~ /^vlan[0-9]{1,2}ports$/) {
  81. nvram[$1] = $2
  82. }
  83. END {
  84. if (((nvram["vlan0ports"] ~ /^0 1 2 3 8/ ) && (nvram["vlan1ports"] ~ /^4 8/ && (cpuport == "8"))) || \
  85. ((nvram["vlan1ports"] ~ /^0 1 2 3 8/ ) && (nvram["vlan2ports"] ~ /^4 8/ && (cpuport == "8"))) || \
  86. ((nvram["vlan2ports"] ~ /^0 1 2 3 8/ ) && (nvram["vlan1ports"] ~ /^4 8/ && (cpuport == "8")))) {
  87. c["vlan1ports"] = "0 1 2 3 8t"
  88. c["vlan2ports"] = "4 8t"
  89. }
  90. if (((nvram["vlan0ports"] ~ /^0 1 2 3 5/ ) && (nvram["vlan1ports"] ~ /^4 5/ && (cpuport == "5"))) || \
  91. ((nvram["vlan1ports"] ~ /^0 1 2 3 5/ ) && (nvram["vlan2ports"] ~ /^4 5/ && (cpuport == "5"))) || \
  92. ((nvram["vlan2ports"] ~ /^0 1 2 3 5/ ) && (nvram["vlan1ports"] ~ /^4 5/ && (cpuport == "5")))) {
  93. c["vlan1ports"] = "0 1 2 3 5t"
  94. c["vlan2ports"] = "4 5t"
  95. }
  96. if ((model == "Asus WLHDD") || (model == "Asus WL300G")) {
  97. c["wan_ifname"] = ""
  98. c["lan_ifname"] = "eth1"
  99. }
  100. if (model == "Asus WL330GE") {
  101. c["wan_ifname"] = ""
  102. c["lan_ifname"] = "eth0.1"
  103. c["vlan1ports"] = "4 5t"
  104. c["vlan2ports"] = ""
  105. }
  106. if ((model == "Asus WL500G") || (model == "Microsoft MN-700")) {
  107. c["wan_ifname"] = "eth1"
  108. c["lan_ifname"] = "eth0"
  109. }
  110. if ((model == "Asus WL500GP V2") || (model == "Buffalo WHR-G125")) {
  111. c["vlan1ports"] = "0 1 2 3 5t"
  112. c["vlan2ports"] = "4 5t"
  113. }
  114. if (model == "Dell TrueMobile 2300") {
  115. c["lan_ifname"] = "eth0"
  116. c["wan_ifname"] = "eth1"
  117. c["vlan1ports"] = "0 1 2 3 4 5"
  118. c["vlan2ports"] = ""
  119. }
  120. if (nvram["boardtype"] == "bcm94710r4") {
  121. # Toshiba WRC-1000
  122. c["lan_ifname"] = "eth0"
  123. c["wan_ifname"] = "eth1"
  124. }
  125. if ((nvram["boardtype"] == "wgt634u") || (nvram["boardtype"] == "0x0467")) {
  126. c["vlan1ports"] = "0 1 2 3 5t"
  127. c["vlan2ports"] = "4 5t"
  128. }
  129. if ((nvram["boardtype"] == "0x042f") || (nvram["boardtype"] == "0x0472")) {
  130. if (nvram["boardnum"] == "45") {
  131. # WL-500gP
  132. c["vlan1ports"] = "1 2 3 4 5t"
  133. c["vlan2ports"] = "0 5t"
  134. } else {
  135. # Generic BCM94704
  136. c["vlan1ports"] = "0 1 2 3 4 5"
  137. c["vlan2ports"] = ""
  138. c["lan_ifname"] = "eth0"
  139. c["wan_ifname"] = "eth1"
  140. # MAC addresses on 4704 tend to be screwed up. Add a workaround here
  141. if (nvram["et0macaddr"] ~ mac_check) {
  142. c["lan_macaddr"] = nvram["et0macaddr"]
  143. c["wan_macaddr"] = macinc(c["lan_macaddr"])
  144. }
  145. }
  146. }
  147. # Buffalo WBR-B11 and Buffalo WBR-G54
  148. if (nvram["boardtype"] == "bcm94710ap") {
  149. c["vlan1ports"] = "0 1 2 3 4 5"
  150. c["vlan2ports"] = ""
  151. c["lan_ifname"] = "eth0"
  152. c["wan_ifname"] = "eth1"
  153. }
  154. # WAP54G
  155. if ((nvram["boardnum"] == "2") || \
  156. (nvram["boardnum"] == "1024")) {
  157. c["lan_ifname"]="eth0"
  158. c["wan_ifname"]=""
  159. }
  160. # Sitecom WL-105b
  161. if ((nvram["boardum"] == "2") && \
  162. (nvram["GemtekPmonVer"] == "1")) {
  163. c["lan_ifname"]="eth0"
  164. c["wan_ifname"]=""
  165. }
  166. # ASUS WL-700gE
  167. # These are actually same as defaults above. For some reason this script applies
  168. # Generic BCM94704 settings instead so we revert to proper settings here.
  169. # Hopefully someone will fix this properly soon.
  170. if (model == "Asus WL700") {
  171. c["lan_ifname"]="eth0.1"
  172. c["wan_ifname"]="eth0.2"
  173. c["vlan1ports"]="1 2 3 4 5t"
  174. c["vlan2ports"]="0 5t"
  175. }
  176. if ((model == "Motorola WR850G") || (model == "Siemens SE505 V2")) {
  177. c["vlan1ports"]="0 1 2 3 5t"
  178. c["vlan2ports"]="4 5t"
  179. }
  180. if (model == "Asus WL500W") {
  181. c["lan_ifname"] = "eth0"
  182. c["wan_ifname"] = "eth1"
  183. c["vlan1ports"] = "0 1 2 3 4 5"
  184. c["vlan2ports"] = ""
  185. }
  186. print "local vlan1ports=\"" c["vlan1ports"] "\";"
  187. print "local vlan2ports=\"" c["vlan2ports"] "\";"
  188. print "local lan_ifname=\"" c["lan_ifname"] "\";"
  189. print "local lan_macaddr=\"" c["lan_macaddr"] "\";"
  190. print "local wan_ifname=\"" c["wan_ifname"] "\";"
  191. print "local wan_macaddr=\"" c["wan_macaddr"] "\";"
  192. }'`
  193. . /lib/functions/uci-defaults.sh
  194. touch /etc/config/network
  195. eval "$network_defs"
  196. [ -n "$vlan1ports" -o -n "$vlan2ports" ] && {
  197. local cfg=`ucidef_add_switch "$switchname" 1 1`
  198. [ -n "$cfg" ] && uci rename network.$cfg=eth0
  199. [ -n "$vlan1ports" ] && {
  200. cfg=`ucidef_add_switch_vlan "$switchname" 1 "$vlan1ports"`
  201. }
  202. [ -n "$vlan2ports" ] && {
  203. cfg=`ucidef_add_switch_vlan "$switchname" 2 "$vlan2ports"`
  204. }
  205. }
  206. ucidef_set_interface_loopback
  207. ucidef_set_interface_lan "$lan_ifname"
  208. [ -n "$lan_macaddr" ] && ucidef_set_interface_macaddr lan "$lan_macaddr"
  209. [ -n "$wan_ifname" ] && {
  210. ucidef_set_interface_wan "$wan_ifname"
  211. [ -n "$wan_macaddr" ] && ucidef_set_interface_macaddr wan "$wan_macaddr"
  212. }
  213. uci commit network
  214. }