01_detect 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2006-2015 OpenWrt.org
  3. . /lib/functions/system.sh
  4. . /lib/functions/uci-defaults.sh
  5. detect_by_vlanports() {
  6. local vlan0ports="$(nvram get vlan0ports)"
  7. local vlan1ports="$(nvram get vlan1ports)"
  8. local vlan2ports="$(nvram get vlan2ports)"
  9. local cpuport="$(swconfig dev switch0 help 2>/dev/null | sed -ne "s|.*cpu @ \([0-9]*\).*|\1|p")"
  10. if [ "${vlan0ports:0:9}" = "0 1 2 3 8" -a "${vlan1ports:0:3}" = "4 8" -a ${cpuport:-0} -eq 8 ] || \
  11. [ "${vlan1ports:0:9}" = "0 1 2 3 8" -a "${vlan2ports:0:3}" = "4 8" -a ${cpuport:-0} -eq 8 ] || \
  12. [ "${vlan2ports:0:9}" = "0 1 2 3 8" -a "${vlan1ports:0:3}" = "4 8" -a ${cpuport:-0} -eq 8 ];
  13. then
  14. ucidef_add_switch "switch0" \
  15. "0:lan" "1:lan" "2:lan" "3:lan" "4:wan" "8@eth0"
  16. elif [ "${vlan0ports:0:9}" = "1 2 3 4 8" -a "${vlan1ports:0:3}" = "0 8" -a ${cpuport:-0} -eq 8 ] || \
  17. [ "${vlan1ports:0:9}" = "1 2 3 4 8" -a "${vlan2ports:0:3}" = "0 8" -a ${cpuport:-0} -eq 8 ] || \
  18. [ "${vlan2ports:0:9}" = "1 2 3 4 8" -a "${vlan1ports:0:3}" = "0 8" -a ${cpuport:-0} -eq 8 ];
  19. then
  20. ucidef_add_switch "switch0" \
  21. "1:lan" "2:lan" "3:lan" "4:lan" "0:wan" "8@eth0"
  22. elif [ "${vlan0ports:0:9}" = "0 1 2 3 5" -a "${vlan1ports:0:3}" = "4 5" -a ${cpuport:-0} -eq 5 ] || \
  23. [ "${vlan1ports:0:9}" = "0 1 2 3 5" -a "${vlan2ports:0:3}" = "4 5" -a ${cpuport:-0} -eq 5 ] || \
  24. [ "${vlan2ports:0:9}" = "0 1 2 3 5" -a "${vlan1ports:0:3}" = "4 5" -a ${cpuport:-0} -eq 5 ];
  25. then
  26. ucidef_add_switch "switch0" \
  27. "0:lan" "1:lan" "2:lan" "3:lan" "4:wan" "5@eth0"
  28. elif [ "${vlan0ports:0:9}" = "1 2 3 4 5" -a "${vlan1ports:0:3}" = "0 5" -a ${cpuport:-0} -eq 5 ] || \
  29. [ "${vlan1ports:0:9}" = "1 2 3 4 5" -a "${vlan2ports:0:3}" = "0 5" -a ${cpuport:-0} -eq 5 ] || \
  30. [ "${vlan2ports:0:9}" = "1 2 3 4 5" -a "${vlan1ports:0:3}" = "0 5" -a ${cpuport:-0} -eq 5 ];
  31. then
  32. ucidef_add_switch "switch0" \
  33. "1:lan" "2:lan" "3:lan" "4:lan" "0:wan" "5@eth0"
  34. else
  35. logger -t "01_detect" "Unable to determine network configuration"
  36. ucidef_set_interface_lan "eth0"
  37. fi
  38. }
  39. detect_by_boardnum() {
  40. local boardnum="$1"
  41. case "$boardnum" in
  42. # WAP54G, Sitecom WL-105b
  43. "2" | \
  44. "1024")
  45. ucidef_set_interface_lan "eth0"
  46. ;;
  47. # Generic detection fallback
  48. *)
  49. detect_by_vlanports
  50. ;;
  51. esac
  52. }
  53. detect_by_boardtype() {
  54. local boardtype="$1"
  55. local boardnum="$2"
  56. case "$boardtype" in
  57. "bcm94710r4")
  58. ucidef_set_interfaces_lan_wan "eth0" "eth1"
  59. ;;
  60. "0x0467")
  61. ucidef_add_switch "switch0" \
  62. "0:lan" "1:lan" "2:lan" "3:lan" "4:wan" "5@eth0"
  63. ;;
  64. "0x042f" | \
  65. "0x0472")
  66. # WL-500gP
  67. if [ "$boardnum" = "45" ]; then
  68. ucidef_add_switch "switch0" \
  69. "0:wan" "1:lan" "2:lan" "3:lan" "4:lan" "5@eth0"
  70. # Generic BCM94704
  71. else
  72. ucidef_set_interface_wan "eth1"
  73. ucidef_add_switch "switch0" \
  74. "0:lan" "1:lan" "2:lan" "3:lan" "4:lan" "5@eth0"
  75. # MAC addresses on 4704 tend to be screwed up. Add a workaround here
  76. local mac="$(nvram get et0macaddr)"
  77. local pat="[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]"
  78. pat="$pat:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]"
  79. pat="$pat:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]"
  80. case "$mac" in
  81. $pat)
  82. ucidef_set_interface_macaddr "lan" "$mac"
  83. ucidef_set_interface_macaddr "wan" "$(macaddr_add "$mac" 1)"
  84. ;;
  85. esac
  86. fi
  87. ;;
  88. # Buffalo WBR-B11 and Buffalo WBR-G54
  89. "bcm94710ap")
  90. ucidef_set_interface_wan "eth1"
  91. ucidef_add_switch "switch0" \
  92. "0:lan" "1:lan" "2:lan" "3:lan" "4:lan" "5@eth0"
  93. ;;
  94. *)
  95. detect_by_boardnum "$boardnum"
  96. ;;
  97. esac
  98. }
  99. detect_by_model() {
  100. local model="$1"
  101. local boardtype="$2"
  102. local boardnum="$3"
  103. # Netgear WGT634U exception
  104. if grep -sqE 'mtd0: 000(6|a)0000' /proc/mtd; then
  105. ucidef_add_switch "switch0" \
  106. "0:lan" "1:lan" "2:lan" "3:lan" "4:wan" "5@eth0"
  107. return
  108. fi
  109. case "$model" in
  110. "Asus WLHDD" | \
  111. "Asus WL300G")
  112. ucidef_set_interface_lan "eth1"
  113. ;;
  114. "Asus WL330GE")
  115. ucidef_add_switch "switch0" "4:lan" "5t@eth0"
  116. ;;
  117. "Asus WL500G" | \
  118. "Asus WL500W" | \
  119. "Microsoft MN-700")
  120. ucidef_set_interfaces_lan_wan "eth0" "eth1"
  121. ;;
  122. "Asus WL500GP V2" | \
  123. "Buffalo WHR-G125" | \
  124. "Motorola WR850G" | \
  125. "Siemens SE505 V2")
  126. ucidef_add_switch "switch0" \
  127. "0:lan" "1:lan" "2:lan" "3:lan" "4:wan" "5@eth0"
  128. ;;
  129. "Asus WL700")
  130. ucidef_add_switch "switch0" \
  131. "0:wan" "1:lan" "2:lan" "3:lan" "4:lan" "5@eth0"
  132. ;;
  133. "Dell TrueMobile 2300")
  134. ucidef_set_interface_wan "eth1"
  135. ucidef_add_switch "switch0" \
  136. "0:lan" "1:lan" "2:lan" "3:lan" "5@eth0"
  137. ;;
  138. "Linksys E3000 V1")
  139. ucidef_add_switch "switch0" \
  140. "0:wan" "1:lan:4" "2:lan:3" "3:lan:2" "4:lan:1" "8@eth0"
  141. ;;
  142. *)
  143. detect_by_boardtype "$boardtype" "$boardnum"
  144. ;;
  145. esac
  146. }
  147. local model="$(sed -ne 's/^machine[ \t]*: //p' /proc/cpuinfo)"
  148. local boardtype="$(nvram get boardtype)"
  149. local boardnum="$(nvram get boardnum)"
  150. board_config_update
  151. ucidef_set_board_id "$boardtype${boardnum:+:$boardnum}"
  152. ucidef_set_model_name "$model"
  153. detect_by_model "$model" "$boardtype" "$boardnum"
  154. board_config_flush