broadcom.sh 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. append DRIVERS "broadcom"
  2. scan_broadcom() {
  3. local device="$1"
  4. local vif vifs wds
  5. local adhoc sta apmode mon disabled
  6. local adhoc_if sta_if ap_if mon_if
  7. config_get vifs "$device" vifs
  8. for vif in $vifs; do
  9. config_get_bool disabled "$vif" disabled 0
  10. [ $disabled -eq 0 ] || continue
  11. local mode
  12. config_get mode "$vif" mode
  13. case "$mode" in
  14. adhoc)
  15. adhoc=1
  16. adhoc_if="$vif"
  17. ;;
  18. sta)
  19. sta=1
  20. sta_if="$vif"
  21. ;;
  22. ap)
  23. apmode=1
  24. ap_if="${ap_if:+$ap_if }$vif"
  25. ;;
  26. wds)
  27. local addr
  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. local pid_file=/var/run/nas.$device.pid
  97. [ -e $pid_file ] && start-stop-daemon -K -q -s SIGKILL -p $pid_file && rm $pid_file
  98. # make sure the interfaces are down and removed from all bridges
  99. local dev
  100. for dev in $device ${device}-1 ${device}-2 ${device}-3; do
  101. ifconfig "$dev" down 2>/dev/null >/dev/null && {
  102. unbridge "$dev"
  103. }
  104. done
  105. )
  106. true
  107. }
  108. enable_broadcom() {
  109. local device="$1"
  110. local channel country maxassoc wds vifs distance slottime rxantenna txantenna
  111. local frameburst macfilter maclist macaddr txpower frag rts hwmode htmode
  112. config_get channel "$device" channel
  113. config_get country "$device" country
  114. config_get maxassoc "$device" maxassoc
  115. config_get wds "$device" wds
  116. config_get vifs "$device" vifs
  117. config_get distance "$device" distance
  118. config_get slottime "$device" slottime
  119. config_get rxantenna "$device" rxantenna
  120. config_get txantenna "$device" txantenna
  121. config_get_bool frameburst "$device" frameburst
  122. config_get macfilter "$device" macfilter
  123. config_get maclist "$device" maclist
  124. config_get macaddr "$device" macaddr
  125. config_get txpower "$device" txpower
  126. config_get frag "$device" frag
  127. config_get rts "$device" rts
  128. config_get hwmode "$device" hwmode
  129. config_get htmode "$device" htmode
  130. local doth=0
  131. local wmm=1
  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. local gmode=2 nmode=0 nreqd=
  152. case "$hwmode" in
  153. *a) gmode=;;
  154. *b) gmode=0;;
  155. *bg) gmode=1;;
  156. *g) gmode=2;;
  157. *gst) gmode=4;;
  158. *lrs) gmode=5;;
  159. *) nmode=1; nreqd=0;;
  160. esac
  161. case "$hwmode" in
  162. n|11n) nmode=1; nreqd=1;;
  163. *n*) nmode=1; nreqd=0;;
  164. esac
  165. # Use 'nmode' for N-Phy only
  166. [ "$(wlc ifname "$device" phytype)" = 4 ] || nmode=
  167. local band chanspec
  168. [ ${channel:-0} -ge 1 -a ${channel:-0} -le 14 ] && band=2
  169. [ ${channel:-0} -ge 36 ] && {
  170. band=1
  171. gmode=
  172. }
  173. # Use 'chanspec' instead of 'channel' for 'N' modes (See bcmwifi.h)
  174. [ ${nmode:-0} -ne 0 -a -n "$band" -a -n "$channel" ] && {
  175. case "$htmode" in
  176. HT40-) chanspec=$(printf 0x%x%x%02x $band 0xe $(($channel - 2))); channel=;;
  177. HT40+) chanspec=$(printf 0x%x%x%02x $band 0xd $(($channel + 2))); channel=;;
  178. HT20) chanspec=$(printf 0x%x%x%02x $band 0xb $channel); channel=;;
  179. *) ;;
  180. esac
  181. }
  182. local _c=0
  183. local nas="$(which nas)"
  184. local if_up nas_cmd
  185. local vif vif_pre_up vif_post_up vif_do_up vif_txpower
  186. for vif in $vifs; do
  187. config_get vif_txpower "$vif" txpower
  188. local mode
  189. config_get mode "$vif" mode
  190. append vif_pre_up "vif $_c" "$N"
  191. append vif_post_up "vif $_c" "$N"
  192. append vif_do_up "vif $_c" "$N"
  193. config_get_bool wmm "$vif" wmm "$wmm"
  194. config_get_bool doth "$vif" doth "$doth"
  195. [ "$mode" = "sta" ] || {
  196. local hidden isolate
  197. config_get_bool hidden "$vif" hidden 0
  198. append vif_pre_up "closed $hidden" "$N"
  199. config_get_bool isolate "$vif" isolate 0
  200. append vif_pre_up "ap_isolate $isolate" "$N"
  201. }
  202. local wsec_r=0
  203. local eap_r=0
  204. local wsec=0
  205. local auth=0
  206. local nasopts=
  207. local enc key rekey
  208. config_get enc "$vif" encryption
  209. case "$enc" in
  210. *wep*)
  211. local def defkey k knr
  212. wsec_r=1
  213. wsec=1
  214. defkey=1
  215. config_get key "$vif" key
  216. case "$enc" in
  217. *shared*) append vif_do_up "wepauth 1" "$N";;
  218. *) append vif_do_up "wepauth 0" "$N";;
  219. esac
  220. case "$key" in
  221. [1234])
  222. defkey="$key"
  223. for knr in 1 2 3 4; do
  224. config_get k "$vif" key$knr
  225. [ -n "$k" ] || continue
  226. [ "$defkey" = "$knr" ] && def="=" || def=""
  227. append vif_do_up "wepkey $def$knr,$k" "$N"
  228. done
  229. ;;
  230. "");;
  231. *) append vif_do_up "wepkey =1,$key" "$N";;
  232. esac
  233. ;;
  234. *psk*)
  235. wsec_r=1
  236. config_get key "$vif" key
  237. # psk version + default cipher
  238. case "$enc" in
  239. *mixed*|*psk+psk2*) auth=132; wsec=6;;
  240. *psk2*) auth=128; wsec=4;;
  241. *) auth=4; wsec=2;;
  242. esac
  243. # cipher override
  244. case "$enc" in
  245. *tkip+aes*|*tkip+ccmp*|*aes+tkip*|*ccmp+tkip*) wsec=6;;
  246. *aes*|*ccmp*) wsec=4;;
  247. *tkip*) wsec=2;;
  248. esac
  249. # group rekey interval
  250. config_get rekey "$vif" wpa_group_rekey
  251. eval "${vif}_key=\"\$key\""
  252. nasopts="-k \"\$${vif}_key\"${rekey:+ -g $rekey}"
  253. ;;
  254. *wpa*)
  255. local auth_port auth_secret auth_server
  256. wsec_r=1
  257. eap_r=1
  258. config_get auth_server "$vif" auth_server
  259. [ -z "$auth_server" ] && config_get auth_server "$vif" server
  260. config_get auth_port "$vif" auth_port
  261. [ -z "$auth_port" ] && config_get auth_port "$vif" port
  262. config_get auth_secret "$vif" auth_secret
  263. [ -z "$auth_secret" ] && config_get auth_secret "$vif" key
  264. # wpa version + default cipher
  265. case "$enc" in
  266. *mixed*|*wpa+wpa2*) auth=66; wsec=6;;
  267. *wpa2*) auth=64; wsec=4;;
  268. *) auth=2; wsec=2;;
  269. esac
  270. # cipher override
  271. case "$enc" in
  272. *tkip+aes*|*tkip+ccmp*|*aes+tkip*|*ccmp+tkip*) wsec=6;;
  273. *aes*|*ccmp*) wsec=4;;
  274. *tkip*) wsec=2;;
  275. esac
  276. # group rekey interval
  277. config_get rekey "$vif" wpa_group_rekey
  278. eval "${vif}_key=\"\$auth_secret\""
  279. nasopts="-r \"\$${vif}_key\" -h $auth_server -p ${auth_port:-1812}${rekey:+ -g $rekey}"
  280. ;;
  281. esac
  282. append vif_do_up "wsec $wsec" "$N"
  283. append vif_do_up "wpa_auth $auth" "$N"
  284. append vif_do_up "wsec_restrict $wsec_r" "$N"
  285. append vif_do_up "eap_restrict $eap_r" "$N"
  286. local ssid
  287. config_get ssid "$vif" ssid
  288. append vif_post_up "vlan_mode 0" "$N"
  289. append vif_post_up "ssid $ssid" "$N"
  290. append vif_do_up "ssid $ssid" "$N"
  291. [ "$mode" = "monitor" ] && {
  292. append vif_post_up "monitor $monitor" "$N"
  293. }
  294. [ "$mode" = "adhoc" ] && {
  295. local bssid
  296. config_get bssid "$vif" bssid
  297. [ -n "$bssid" ] && {
  298. append vif_pre_up "bssid $bssid" "$N"
  299. append vif_pre_up "ibss_merge 0" "$N"
  300. } || {
  301. append vif_pre_up "ibss_merge 1" "$N"
  302. }
  303. }
  304. append vif_post_up "enabled 1" "$N"
  305. local ifname
  306. config_get ifname "$vif" ifname
  307. #append if_up "ifconfig $ifname up" ";$N"
  308. local net_cfg="$(find_net_config "$vif")"
  309. [ -z "$net_cfg" ] || {
  310. append if_up "set_wifi_up '$vif' '$ifname'" ";$N"
  311. append if_up "start_net '$ifname' '$net_cfg'" ";$N"
  312. }
  313. [ -z "$nas" -o -z "$nasopts" ] || {
  314. eval "${vif}_ssid=\"\$ssid\""
  315. local nas_mode="-A"
  316. [ "$mode" = "sta" ] && nas_mode="-S"
  317. [ -z "$nas_cmd" ] && {
  318. local pid_file=/var/run/nas.$device.pid
  319. nas_cmd="start-stop-daemon -S -b -p $pid_file -x $nas -- -P $pid_file -H 34954"
  320. }
  321. append nas_cmd "-i $ifname $nas_mode -m $auth -w $wsec -s \"\$${vif}_ssid\" -g 3600 -F $nasopts"
  322. }
  323. _c=$(($_c + 1))
  324. done
  325. wlc ifname "$device" stdin <<EOF
  326. $ifdown
  327. band ${band:-0}
  328. ${nmode:+nmode $nmode}
  329. ${nmode:+${nreqd:+nreqd $nreqd}}
  330. ${gmode:+gmode $gmode}
  331. apsta $apsta
  332. ap $ap
  333. ${mssid:+mssid $mssid}
  334. infra $infra
  335. ${wet:+wet 1}
  336. 802.11d 0
  337. 802.11h ${doth:-0}
  338. wme ${wmm:-1}
  339. rxant ${rxantenna:-3}
  340. txant ${txantenna:-3}
  341. fragthresh ${frag:-2346}
  342. rtsthresh ${rts:-2347}
  343. monitor ${monitor:-0}
  344. radio ${radio:-1}
  345. macfilter ${macfilter:-0}
  346. maclist ${maclist:-none}
  347. wds none
  348. ${wds:+wds $wds}
  349. country ${country:-US}
  350. ${channel:+channel $channel}
  351. ${chanspec:+chanspec $chanspec}
  352. maxassoc ${maxassoc:-128}
  353. slottime ${slottime:--1}
  354. ${frameburst:+frameburst $frameburst}
  355. $vif_pre_up
  356. up
  357. $vif_post_up
  358. EOF
  359. eval "$if_up"
  360. wlc ifname "$device" stdin <<EOF
  361. $vif_do_up
  362. EOF
  363. # use vif_txpower (from last wifi-iface) instead of txpower (from
  364. # wifi-device) if the latter does not exist
  365. txpower=${txpower:-$vif_txpower}
  366. [ -z "$txpower" ] || iwconfig $device txpower ${txpower}dBm
  367. eval "$nas_cmd"
  368. }
  369. detect_broadcom() {
  370. local i=-1
  371. while grep -qs "^ *wl$((++i)):" /proc/net/dev; do
  372. local channel type
  373. config_get type wl${i} type
  374. [ "$type" = broadcom ] && continue
  375. channel=`wlc ifname wl${i} channel`
  376. cat <<EOF
  377. config wifi-device wl${i}
  378. option type broadcom
  379. option channel ${channel:-11}
  380. # REMOVE THIS LINE TO ENABLE WIFI:
  381. option disabled 1
  382. config wifi-iface
  383. option device wl${i}
  384. option network lan
  385. option mode ap
  386. option ssid OpenWrt${i#0}
  387. option encryption none
  388. EOF
  389. done
  390. }