broadcom.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. ap=1
  49. infra=1
  50. if [ "$_c" -gt 1 ]; then
  51. mssid=1
  52. else
  53. mssid=
  54. fi
  55. apsta=0
  56. radio=1
  57. monitor=0
  58. case "$adhoc:$sta:$apmode:$mon" in
  59. 1*)
  60. ap=0
  61. mssid=
  62. infra=0
  63. ;;
  64. :1:1:)
  65. apsta=1
  66. wet=1
  67. ;;
  68. :1::)
  69. wet=1
  70. ap=0
  71. mssid=
  72. ;;
  73. :::1)
  74. wet=1
  75. ap=0
  76. mssid=
  77. monitor=1
  78. ;;
  79. ::)
  80. radio=0
  81. ;;
  82. esac
  83. }
  84. disable_broadcom() {
  85. local device="$1"
  86. set_wifi_down "$device"
  87. (
  88. include /lib/network
  89. local pid_file=/var/run/nas.$device.pid
  90. [ -e $pid_file ] && start-stop-daemon -K -q -s SIGKILL -p $pid_file && rm $pid_file
  91. # make sure the interfaces are down and removed from all bridges
  92. local dev ifname
  93. for dev in /sys/class/net/wds${device##wl}-* /sys/class/net/${device}-* /sys/class/net/${device}; do
  94. if [ -e "$dev" ]; then
  95. ifname=${dev##/sys/class/net/}
  96. ifconfig "$ifname" down
  97. unbridge "$ifname"
  98. fi
  99. done
  100. # make sure all of the devices are disabled in the driver
  101. local ifdown=
  102. local bssmax=$(wlc ifname "$device" bssmax)
  103. local vif=$((${bssmax:-4} - 1))
  104. append ifdown "down" "$N"
  105. append ifdown "wds none" "$N"
  106. while [ $vif -ge 0 ]; do
  107. append ifdown "vif $vif" "$N"
  108. append ifdown "enabled 0" "$N"
  109. vif=$(($vif - 1))
  110. done
  111. wlc ifname "$device" stdin <<EOF
  112. $ifdown
  113. leddc 0xffff
  114. EOF
  115. )
  116. true
  117. }
  118. enable_broadcom() {
  119. local device="$1"
  120. local channel country maxassoc wds vifs distance slottime rxantenna txantenna
  121. local frameburst macfilter maclist macaddr txpower frag rts hwmode htmode
  122. config_get channel "$device" channel
  123. config_get country "$device" country
  124. config_get maxassoc "$device" maxassoc
  125. config_get wds "$device" wds
  126. config_get vifs "$device" vifs
  127. config_get distance "$device" distance
  128. config_get slottime "$device" slottime
  129. config_get rxantenna "$device" rxantenna
  130. config_get txantenna "$device" txantenna
  131. config_get_bool frameburst "$device" frameburst
  132. config_get macfilter "$device" macfilter
  133. config_get maclist "$device" maclist
  134. config_get macaddr "$device" macaddr $(wlc ifname "$device" default_bssid)
  135. config_get txpower "$device" txpower
  136. config_get frag "$device" frag
  137. config_get rts "$device" rts
  138. config_get hwmode "$device" hwmode
  139. config_get htmode "$device" htmode
  140. local doth=0
  141. local wmm=1
  142. [ -z "$slottime" ] && {
  143. [ -n "$distance" ] && {
  144. # slottime = 9 + (distance / 150) + (distance % 150 ? 1 : 0)
  145. slottime="$((9 + ($distance / 150) + 1 - (150 - ($distance % 150)) / 150 ))"
  146. }
  147. } || {
  148. slottime="${slottime:--1}"
  149. }
  150. case "$macfilter" in
  151. allow|2)
  152. macfilter=2;
  153. ;;
  154. deny|1)
  155. macfilter=1;
  156. ;;
  157. disable|none|0)
  158. macfilter=0;
  159. ;;
  160. esac
  161. local gmode=2 nmode=0 nreqd=
  162. case "$hwmode" in
  163. *a) gmode=;;
  164. *b) gmode=0;;
  165. *bg) gmode=1;;
  166. *g) gmode=2;;
  167. *gst) gmode=4;;
  168. *lrs) gmode=5;;
  169. *) nmode=1; nreqd=0;;
  170. esac
  171. case "$hwmode" in
  172. n|11n) nmode=1; nreqd=1;;
  173. *n*) nmode=1; nreqd=0;;
  174. esac
  175. # Use 'nmode' for N-Phy only
  176. [ "$(wlc ifname "$device" phytype)" = 4 ] || nmode=
  177. local band chanspec
  178. [ ${channel:-0} -ge 1 -a ${channel:-0} -le 14 ] && band=2
  179. [ ${channel:-0} -ge 36 ] && {
  180. band=1
  181. gmode=
  182. }
  183. # Use 'chanspec' instead of 'channel' for 'N' modes (See bcmwifi.h)
  184. [ -n "$nmode" -a -n "$band" -a -n "$channel" ] && {
  185. case "$htmode" in
  186. HT40)
  187. if [ -n "$gmode" ]; then
  188. [ $channel -lt 7 ] && htmode="HT40+" || htmode="HT40-"
  189. else
  190. [ $(( ($channel / 4) % 2 )) -eq 1 ] && htmode="HT40+" || htmode="HT40-"
  191. fi
  192. ;;
  193. esac
  194. case "$htmode" in
  195. HT40-) chanspec=$(printf 0x%x%x%02x $band 0xe $(($channel - 2))); nmode=1; channel=;;
  196. HT40+) chanspec=$(printf 0x%x%x%02x $band 0xd $(($channel + 2))); nmode=1; channel=;;
  197. HT20) chanspec=$(printf 0x%x%x%02x $band 0xb $channel); nmode=1; channel=;;
  198. *) ;;
  199. esac
  200. }
  201. local leddc=$(wlc ifname "$device" leddc)
  202. [ $((leddc)) -eq $((0xffff)) ] && {
  203. leddc=0x005a000a;
  204. }
  205. local _c=0
  206. local nas="$(which nas)"
  207. local if_pre_up if_up nas_cmd
  208. local vif vif_pre_up vif_post_up vif_do_up vif_txpower
  209. local bssmax=$(wlc ifname "$device" bssmax)
  210. bssmax=${bssmax:-4}
  211. for vif in $vifs; do
  212. [ $_c -ge $bssmax ] && break
  213. config_get vif_txpower "$vif" txpower
  214. local mode
  215. config_get mode "$vif" mode
  216. append vif_pre_up "vif $_c" "$N"
  217. append vif_post_up "vif $_c" "$N"
  218. append vif_do_up "vif $_c" "$N"
  219. config_get_bool wmm "$vif" wmm "$wmm"
  220. config_get_bool doth "$vif" doth "$doth"
  221. [ "$mode" = "sta" ] || {
  222. local hidden isolate
  223. config_get_bool hidden "$vif" hidden 0
  224. append vif_pre_up "closed $hidden" "$N"
  225. config_get_bool isolate "$vif" isolate 0
  226. append vif_pre_up "ap_isolate $isolate" "$N"
  227. }
  228. local wsec_r=0
  229. local eap_r=0
  230. local wsec=0
  231. local auth=0
  232. local nasopts=
  233. local enc key rekey
  234. config_get enc "$vif" encryption
  235. case "$enc" in
  236. *wep*)
  237. local def defkey k knr
  238. wsec_r=1
  239. wsec=1
  240. defkey=1
  241. config_get key "$vif" key
  242. case "$enc" in
  243. *shared*) append vif_do_up "wepauth 1" "$N";;
  244. *) append vif_do_up "wepauth 0" "$N";;
  245. esac
  246. case "$key" in
  247. [1234])
  248. defkey="$key"
  249. for knr in 1 2 3 4; do
  250. config_get k "$vif" key$knr
  251. [ -n "$k" ] || continue
  252. [ "$defkey" = "$knr" ] && def="=" || def=""
  253. append vif_do_up "wepkey $def$knr,$k" "$N"
  254. done
  255. ;;
  256. "");;
  257. *) append vif_do_up "wepkey =1,$key" "$N";;
  258. esac
  259. ;;
  260. *psk*)
  261. wsec_r=1
  262. config_get key "$vif" key
  263. # psk version + default cipher
  264. case "$enc" in
  265. *mixed*|*psk+psk2*) auth=132; wsec=6;;
  266. *psk2*) auth=128; wsec=4;;
  267. *) auth=4; wsec=2;;
  268. esac
  269. # cipher override
  270. case "$enc" in
  271. *tkip+aes*|*tkip+ccmp*|*aes+tkip*|*ccmp+tkip*) wsec=6;;
  272. *aes*|*ccmp*) wsec=4;;
  273. *tkip*) wsec=2;;
  274. esac
  275. # group rekey interval
  276. config_get rekey "$vif" wpa_group_rekey
  277. eval "${vif}_key=\"\$key\""
  278. nasopts="-k \"\$${vif}_key\"${rekey:+ -g $rekey}"
  279. ;;
  280. *wpa*)
  281. local auth_port auth_secret auth_server
  282. wsec_r=1
  283. eap_r=1
  284. config_get auth_server "$vif" auth_server
  285. [ -z "$auth_server" ] && config_get auth_server "$vif" server
  286. config_get auth_port "$vif" auth_port
  287. [ -z "$auth_port" ] && config_get auth_port "$vif" port
  288. config_get auth_secret "$vif" auth_secret
  289. [ -z "$auth_secret" ] && config_get auth_secret "$vif" key
  290. # wpa version + default cipher
  291. case "$enc" in
  292. *mixed*|*wpa+wpa2*) auth=66; wsec=6;;
  293. *wpa2*) auth=64; wsec=4;;
  294. *) auth=2; wsec=2;;
  295. esac
  296. # cipher override
  297. case "$enc" in
  298. *tkip+aes*|*tkip+ccmp*|*aes+tkip*|*ccmp+tkip*) wsec=6;;
  299. *aes*|*ccmp*) wsec=4;;
  300. *tkip*) wsec=2;;
  301. esac
  302. # group rekey interval
  303. config_get rekey "$vif" wpa_group_rekey
  304. eval "${vif}_key=\"\$auth_secret\""
  305. nasopts="-r \"\$${vif}_key\" -h $auth_server -p ${auth_port:-1812}${rekey:+ -g $rekey}"
  306. ;;
  307. esac
  308. append vif_do_up "wsec $wsec" "$N"
  309. append vif_do_up "wpa_auth $auth" "$N"
  310. append vif_do_up "wsec_restrict $wsec_r" "$N"
  311. append vif_do_up "eap_restrict $eap_r" "$N"
  312. local ssid
  313. config_get ssid "$vif" ssid
  314. append vif_post_up "vlan_mode 0" "$N"
  315. append vif_pre_up "ssid $ssid" "$N"
  316. [ "$mode" = "monitor" ] && {
  317. append vif_post_up "monitor $monitor" "$N"
  318. }
  319. [ "$mode" = "adhoc" ] && {
  320. local bssid
  321. config_get bssid "$vif" bssid
  322. [ -n "$bssid" ] && {
  323. append vif_pre_up "bssid $bssid" "$N"
  324. append vif_pre_up "ibss_merge 0" "$N"
  325. } || {
  326. append vif_pre_up "ibss_merge 1" "$N"
  327. }
  328. }
  329. append vif_post_up "enabled 1" "$N"
  330. local ifname
  331. config_get ifname "$vif" ifname
  332. local if_cmd="if_pre_up"
  333. [ "$ifname" != "${ifname##${device}-}" ] && if_cmd="if_up"
  334. append $if_cmd "macaddr=\$(wlc ifname '$ifname' cur_etheraddr)" ";$N"
  335. append $if_cmd "ifconfig '$ifname' \${macaddr:+hw ether \$macaddr}" ";$N"
  336. append if_up "ifconfig '$ifname' up" ";$N"
  337. local net_cfg="$(find_net_config "$vif")"
  338. [ -z "$net_cfg" ] || {
  339. ubus -t 30 wait_for network.interface."$net_cfg"
  340. append if_up "set_wifi_up '$vif' '$ifname'" ";$N"
  341. append if_up "start_net '$ifname' '$net_cfg'" ";$N"
  342. }
  343. [ -z "$nas" -o -z "$nasopts" ] || {
  344. eval "${vif}_ssid=\"\$ssid\""
  345. local nas_mode="-A"
  346. [ "$mode" = "sta" ] && nas_mode="-S"
  347. [ -z "$nas_cmd" ] && {
  348. local pid_file=/var/run/nas.$device.pid
  349. nas_cmd="start-stop-daemon -S -b -p $pid_file -x $nas -- -P $pid_file -H 34954"
  350. }
  351. append nas_cmd "-i $ifname $nas_mode -m $auth -w $wsec -s \"\$${vif}_ssid\" -g 3600 -F $nasopts"
  352. }
  353. _c=$(($_c + 1))
  354. done
  355. wlc ifname "$device" stdin <<EOF
  356. ${macaddr:+bssid $macaddr}
  357. ${macaddr:+cur_etheraddr $macaddr}
  358. band ${band:-0}
  359. ${nmode:+nmode $nmode}
  360. ${nmode:+${nreqd:+nreqd $nreqd}}
  361. ${gmode:+gmode $gmode}
  362. leddc $leddc
  363. apsta $apsta
  364. ap $ap
  365. ${mssid:+mssid $mssid}
  366. infra $infra
  367. ${wet:+wet 1}
  368. 802.11d 0
  369. 802.11h ${doth:-0}
  370. wme ${wmm:-1}
  371. rxant ${rxantenna:-3}
  372. txant ${txantenna:-3}
  373. fragthresh ${frag:-2346}
  374. rtsthresh ${rts:-2347}
  375. monitor ${monitor:-0}
  376. radio ${radio:-1}
  377. macfilter ${macfilter:-0}
  378. maclist ${maclist:-none}
  379. ${wds:+wds $wds}
  380. country ${country:-US}
  381. ${channel:+channel $channel}
  382. ${chanspec:+chanspec $chanspec}
  383. maxassoc ${maxassoc:-128}
  384. slottime ${slottime:--1}
  385. ${frameburst:+frameburst $frameburst}
  386. $vif_pre_up
  387. EOF
  388. eval "$if_pre_up"
  389. wlc ifname "$device" stdin <<EOF
  390. up
  391. $vif_post_up
  392. EOF
  393. eval "$if_up"
  394. wlc ifname "$device" stdin <<EOF
  395. $vif_do_up
  396. EOF
  397. # use vif_txpower (from last wifi-iface) instead of txpower (from
  398. # wifi-device) if the latter does not exist
  399. txpower=${txpower:-$vif_txpower}
  400. [ -z "$txpower" ] || iwconfig $device txpower ${txpower}dBm
  401. eval "$nas_cmd"
  402. }
  403. detect_broadcom() {
  404. local i=-1
  405. while grep -qs "^ *wl$((++i)):" /proc/net/dev; do
  406. local channel type
  407. config_get type wl${i} type
  408. [ "$type" = broadcom ] && continue
  409. channel=`wlc ifname wl${i} channel`
  410. cat <<EOF
  411. config wifi-device wl${i}
  412. option type broadcom
  413. option channel ${channel:-11}
  414. option txantenna 3
  415. option rxantenna 3
  416. # REMOVE THIS LINE TO ENABLE WIFI:
  417. option disabled 1
  418. config wifi-iface
  419. option device wl${i}
  420. option network lan
  421. option mode ap
  422. option ssid OpenWrt${i#0}
  423. option encryption none
  424. EOF
  425. done
  426. }