broadcom.sh 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. append DRIVERS "broadcom"
  2. scan_broadcom() {
  3. local device="$1"
  4. local wds
  5. local adhoc sta apmode mon disabled
  6. local adhoc_if sta_if ap_if mon_if
  7. local _c=0
  8. config_get vifs "$device" vifs
  9. for vif in $vifs; do
  10. config_get_bool disabled "$vif" disabled 0
  11. [ $disabled -eq 0 ] || continue
  12. config_get mode "$vif" mode
  13. _c=$(($_c + 1))
  14. case "$mode" in
  15. adhoc)
  16. adhoc=1
  17. adhoc_if="$vif"
  18. ;;
  19. sta)
  20. sta=1
  21. sta_if="$vif"
  22. ;;
  23. ap)
  24. apmode=1
  25. ap_if="${ap_if:+$ap_if }$vif"
  26. ;;
  27. wds)
  28. config_get addr "$vif" bssid
  29. [ -z "$addr" ] || {
  30. addr=$(echo "$addr" | tr 'A-F' 'a-f')
  31. append wds "$addr"
  32. }
  33. ;;
  34. monitor)
  35. mon=1
  36. mon_if="$vif"
  37. ;;
  38. *) echo "$device($vif): Invalid mode";;
  39. esac
  40. done
  41. config_set "$device" wds "$wds"
  42. local _c=
  43. for vif in ${adhoc_if:-$sta_if $ap_if $mon_if}; do
  44. config_set "$vif" ifname "${device}${_c:+.$_c}"
  45. _c=$((${_c:-0} + 1))
  46. done
  47. config_set "$device" vifs "${adhoc_if:-$sta_if $ap_if $mon_if}"
  48. ifdown="down"
  49. for vif in 0 1 2 3; do
  50. append ifdown "vif $vif" "$N"
  51. append ifdown "enabled 0" "$N"
  52. done
  53. ap=1
  54. infra=1
  55. if [ "$_c" -gt 1 ]; then
  56. mssid=1
  57. else
  58. mssid=
  59. fi
  60. apsta=0
  61. radio=1
  62. monitor=0
  63. case "$adhoc:$sta:$apmode:$mon" in
  64. 1*)
  65. ap=0
  66. mssid=
  67. infra=0
  68. ;;
  69. :1:1:)
  70. apsta=1
  71. wet=1
  72. ;;
  73. :1::)
  74. wet=1
  75. ap=0
  76. mssid=
  77. ;;
  78. :::1)
  79. wet=1
  80. ap=0
  81. mssid=
  82. monitor=1
  83. ;;
  84. ::)
  85. radio=0
  86. ;;
  87. esac
  88. }
  89. disable_broadcom() {
  90. local device="$1"
  91. set_wifi_down "$device"
  92. wlc ifname "$device" down
  93. wlc ifname "$device" bssid `wlc ifname "$device" default_bssid`
  94. (
  95. include /lib/network
  96. # make sure the interfaces are down and removed from all bridges
  97. for dev in $device ${device}.1 ${device}.2 ${device}.3; do
  98. ifconfig "$dev" down 2>/dev/null >/dev/null && {
  99. unbridge "$dev"
  100. }
  101. done
  102. )
  103. true
  104. }
  105. enable_broadcom() {
  106. local device="$1"
  107. local _c
  108. config_get channel "$device" channel
  109. config_get country "$device" country
  110. config_get maxassoc "$device" maxassoc
  111. config_get wds "$device" wds
  112. config_get vifs "$device" vifs
  113. config_get distance "$device" distance
  114. config_get slottime "$device" slottime
  115. config_get rxantenna "$device" rxantenna
  116. config_get txantenna "$device" txantenna
  117. config_get_bool frameburst "$device" frameburst
  118. config_get macfilter "$device" macfilter
  119. config_get maclist "$device" maclist
  120. config_get macaddr "$device" macaddr
  121. config_get txpower "$device" txpower
  122. config_get frag "$device" frag
  123. config_get rts "$device" rts
  124. config_get hwmode "$device" hwmode
  125. local vif_pre_up vif_post_up vif_do_up vif_txpower
  126. local doth=0
  127. local wmm=1
  128. _c=0
  129. nas="$(which nas)"
  130. nas_cmd=
  131. if_up=
  132. [ -z "$slottime" ] && {
  133. [ -n "$distance" ] && {
  134. # slottime = 9 + (distance / 150) + (distance % 150 ? 1 : 0)
  135. slottime="$((9 + ($distance / 150) + 1 - (150 - ($distance % 150)) / 150 ))"
  136. }
  137. } || {
  138. slottime="${slottime:--1}"
  139. }
  140. case "$macfilter" in
  141. allow|2)
  142. macfilter=2;
  143. ;;
  144. deny|1)
  145. macfilter=1;
  146. ;;
  147. disable|none|0)
  148. macfilter=0;
  149. ;;
  150. esac
  151. case "$hwmode" in
  152. *b) hwmode=0;;
  153. *bg) hwmode=1;;
  154. *g) hwmode=2;;
  155. *gst) hwmode=4;;
  156. *lrs) hwmode=5;;
  157. *) hwmode=1;;
  158. esac
  159. for vif in $vifs; do
  160. config_get vif_txpower "$vif" txpower
  161. config_get mode "$vif" mode
  162. append vif_pre_up "vif $_c" "$N"
  163. append vif_post_up "vif $_c" "$N"
  164. append vif_do_up "vif $_c" "$N"
  165. config_get_bool wmm "$vif" wmm "$wmm"
  166. config_get_bool doth "$vif" doth "$doth"
  167. [ "$mode" = "sta" ] || {
  168. config_get_bool hidden "$vif" hidden 0
  169. append vif_pre_up "closed $hidden" "$N"
  170. config_get_bool isolate "$vif" isolate 0
  171. append vif_pre_up "ap_isolate $isolate" "$N"
  172. }
  173. wsec_r=0
  174. eap_r=0
  175. wsec=0
  176. auth=0
  177. nasopts=
  178. config_get enc "$vif" encryption
  179. case "$enc" in
  180. *wep*)
  181. wsec_r=1
  182. wsec=1
  183. defkey=1
  184. config_get key "$vif" key
  185. case "$enc" in
  186. *shared*) append vif_do_up "wepauth 1" "$N";;
  187. *) append vif_do_up "wepauth 0" "$N";;
  188. esac
  189. case "$key" in
  190. [1234])
  191. defkey="$key"
  192. for knr in 1 2 3 4; do
  193. config_get k "$vif" key$knr
  194. [ -n "$k" ] || continue
  195. [ "$defkey" = "$knr" ] && def="=" || def=""
  196. append vif_do_up "wepkey $def$knr,$k" "$N"
  197. done
  198. ;;
  199. "");;
  200. *) append vif_do_up "wepkey =1,$key" "$N";;
  201. esac
  202. ;;
  203. *psk*)
  204. wsec_r=1
  205. config_get key "$vif" key
  206. # psk version + default cipher
  207. case "$enc" in
  208. *mixed*|*psk+psk2*) auth=132; wsec=6;;
  209. *psk2*) auth=128; wsec=4;;
  210. *) auth=4; wsec=2;;
  211. esac
  212. # cipher override
  213. case "$enc" in
  214. *tkip+aes*|*tkip+ccmp*|*aes+tkip*|*ccmp+tkip*) wsec=6;;
  215. *aes*|*ccmp*) wsec=4;;
  216. *tkip*) wsec=2;;
  217. esac
  218. # group rekey interval
  219. config_get rekey "$vif" wpa_group_rekey
  220. eval "${vif}_key=\"\$key\""
  221. nasopts="-k \"\$${vif}_key\"${rekey:+ -g $rekey}"
  222. ;;
  223. *wpa*)
  224. wsec_r=1
  225. eap_r=1
  226. config_get auth_server "$vif" auth_server
  227. [ -z "$auth_server" ] && config_get auth_server "$vif" server
  228. config_get auth_port "$vif" auth_port
  229. [ -z "$auth_port" ] && config_get auth_port "$vif" port
  230. config_get auth_secret "$vif" auth_secret
  231. [ -z "$auth_secret" ] && config_get auth_secret "$vif" key
  232. # wpa version + default cipher
  233. case "$enc" in
  234. *mixed*|*wpa+wpa2*) auth=66; wsec=6;;
  235. *wpa2*) auth=64; wsec=4;;
  236. *) auth=2; wsec=2;;
  237. esac
  238. # cipher override
  239. case "$enc" in
  240. *tkip+aes*|*tkip+ccmp*|*aes+tkip*|*ccmp+tkip*) wsec=6;;
  241. *aes*|*ccmp*) wsec=4;;
  242. *tkip*) wsec=2;;
  243. esac
  244. # group rekey interval
  245. config_get rekey "$vif" wpa_group_rekey
  246. eval "${vif}_key=\"\$auth_secret\""
  247. nasopts="-r \"\$${vif}_key\" -h $auth_server -p ${auth_port:-1812}${rekey:+ -g $rekey}"
  248. ;;
  249. esac
  250. append vif_do_up "wsec $wsec" "$N"
  251. append vif_do_up "wpa_auth $auth" "$N"
  252. append vif_do_up "wsec_restrict $wsec_r" "$N"
  253. append vif_do_up "eap_restrict $eap_r" "$N"
  254. config_get ssid "$vif" ssid
  255. append vif_post_up "vlan_mode 0" "$N"
  256. append vif_post_up "ssid $ssid" "$N"
  257. append vif_do_up "ssid $ssid" "$N"
  258. [ "$mode" = "monitor" ] && {
  259. append vif_post_up "monitor $monitor" "$N"
  260. }
  261. [ "$mode" = "adhoc" ] && {
  262. config_get bssid "$vif" bssid
  263. [ -n "$bssid" ] && {
  264. append vif_pre_up "bssid $bssid" "$N"
  265. append vif_pre_up "ibss_merge 0" "$N"
  266. } || {
  267. append vif_pre_up "ibss_merge 1" "$N"
  268. }
  269. }
  270. append vif_post_up "enabled 1" "$N"
  271. config_get ifname "$vif" ifname
  272. #append if_up "ifconfig $ifname up" ";$N"
  273. local net_cfg
  274. net_cfg="$(find_net_config "$vif")"
  275. [ -z "$net_cfg" ] || {
  276. append if_up "set_wifi_up '$vif' '$ifname'" ";$N"
  277. append if_up "start_net '$ifname' '$net_cfg'" ";$N"
  278. }
  279. [ -z "$nasopts" ] || {
  280. eval "${vif}_ssid=\"\$ssid\""
  281. nas_mode="-A"
  282. [ "$mode" = "sta" ] && nas_mode="-S"
  283. [ -z "$nas" ] || {
  284. nas_cmd="${nas_cmd:+$nas_cmd$N}start-stop-daemon -S -b -p /var/run/nas.$ifname.pid -x $nas -- -P /var/run/nas.$ifname.pid -H 34954 -i $ifname $nas_mode -m $auth -w $wsec -s \"\$${vif}_ssid\" -g 3600 -F $nasopts"
  285. }
  286. }
  287. _c=$(($_c + 1))
  288. done
  289. killall -KILL nas >&- 2>&-
  290. wlc ifname "$device" stdin <<EOF
  291. $ifdown
  292. gmode ${hwmode:-1}
  293. apsta $apsta
  294. ap $ap
  295. ${mssid:+mssid $mssid}
  296. infra $infra
  297. ${wet:+wet 1}
  298. 802.11d 0
  299. 802.11h ${doth:-0}
  300. wme ${wmm:-1}
  301. rxant ${rxantenna:-3}
  302. txant ${txantenna:-3}
  303. fragthresh ${frag:-2346}
  304. rtsthresh ${rts:-2347}
  305. monitor ${monitor:-0}
  306. radio ${radio:-1}
  307. macfilter ${macfilter:-0}
  308. maclist ${maclist:-none}
  309. wds none
  310. ${wds:+wds $wds}
  311. country ${country:-US}
  312. ${channel:+channel $channel}
  313. maxassoc ${maxassoc:-128}
  314. slottime ${slottime:--1}
  315. ${frameburst:+frameburst $frameburst}
  316. $vif_pre_up
  317. up
  318. $vif_post_up
  319. EOF
  320. eval "$if_up"
  321. wlc ifname "$device" stdin <<EOF
  322. $vif_do_up
  323. EOF
  324. # use vif_txpower (from last wifi-iface) instead of txpower (from
  325. # wifi-device) if the latter does not exist
  326. txpower=${txpower:-$vif_txpower}
  327. [ -z "$txpower" ] || iwconfig $device txpower ${txpower}dBm
  328. eval "$nas_cmd"
  329. }
  330. detect_broadcom() {
  331. local i=-1
  332. while grep -qs "^ *wl$((++i)):" /proc/net/dev; do
  333. local channel
  334. config_get type wl${i} type
  335. [ "$type" = broadcom ] && continue
  336. channel=`wlc ifname wl${i} channel`
  337. cat <<EOF
  338. config wifi-device wl${i}
  339. option type broadcom
  340. option channel ${channel:-11}
  341. # REMOVE THIS LINE TO ENABLE WIFI:
  342. option disabled 1
  343. config wifi-iface
  344. option device wl${i}
  345. option network lan
  346. option mode ap
  347. option ssid OpenWrt${i#0}
  348. option encryption none
  349. EOF
  350. done
  351. }