mac80211.sh 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. #!/bin/sh
  2. . /lib/netifd/netifd-wireless.sh
  3. . /lib/netifd/hostapd.sh
  4. init_wireless_driver "$@"
  5. MP_CONFIG_INT="mesh_retry_timeout mesh_confirm_timeout mesh_holding_timeout mesh_max_peer_links
  6. mesh_max_retries mesh_ttl mesh_element_ttl mesh_hwmp_max_preq_retries
  7. mesh_path_refresh_time mesh_min_discovery_timeout mesh_hwmp_active_path_timeout
  8. mesh_hwmp_preq_min_interval mesh_hwmp_net_diameter_traversal_time mesh_hwmp_rootmode
  9. mesh_hwmp_rann_interval mesh_gate_announcements mesh_sync_offset_max_neighor
  10. mesh_rssi_threshold mesh_hwmp_active_path_to_root_timeout mesh_hwmp_root_interval
  11. mesh_hwmp_confirmation_interval mesh_awake_window mesh_plink_timeout"
  12. MP_CONFIG_BOOL="mesh_auto_open_plinks mesh_fwding"
  13. MP_CONFIG_STRING="mesh_power_mode"
  14. drv_mac80211_init_device_config() {
  15. hostapd_common_add_device_config
  16. config_add_string path phy 'macaddr:macaddr'
  17. config_add_string hwmode
  18. config_add_int beacon_int chanbw frag rts
  19. config_add_int rxantenna txantenna antenna_gain txpower distance
  20. config_add_boolean noscan
  21. config_add_array ht_capab
  22. config_add_boolean \
  23. rxldpc \
  24. short_gi_80 \
  25. short_gi_160 \
  26. tx_stbc_2by1 \
  27. su_beamformer \
  28. su_beamformee \
  29. mu_beamformer \
  30. mu_beamformee \
  31. vht_txop_ps \
  32. htc_vht \
  33. rx_antenna_pattern \
  34. tx_antenna_pattern
  35. config_add_int vht_max_a_mpdu_len_exp vht_max_mpdu vht_link_adapt vht160 rx_stbc tx_stbc
  36. config_add_boolean \
  37. ldpc \
  38. greenfield \
  39. short_gi_20 \
  40. short_gi_40 \
  41. dsss_cck_40
  42. }
  43. drv_mac80211_init_iface_config() {
  44. hostapd_common_add_bss_config
  45. config_add_string 'macaddr:macaddr' ifname
  46. config_add_boolean wds powersave
  47. config_add_int maxassoc
  48. config_add_int max_listen_int
  49. config_add_int dtim_period
  50. # mesh
  51. config_add_string mesh_id
  52. config_add_int $MP_CONFIG_INT
  53. config_add_boolean $MP_CONFIG_BOOL
  54. config_add_string $MP_CONFIG_STRING
  55. }
  56. mac80211_add_capabilities() {
  57. local __var="$1"; shift
  58. local __mask="$1"; shift
  59. local __out= oifs
  60. oifs="$IFS"
  61. IFS=:
  62. for capab in "$@"; do
  63. set -- $capab
  64. [ "$(($4))" -gt 0 ] || continue
  65. [ "$(($__mask & $2))" -eq "$((${3:-$2}))" ] || continue
  66. __out="$__out[$1]"
  67. done
  68. IFS="$oifs"
  69. export -n -- "$__var=$__out"
  70. }
  71. mac80211_hostapd_setup_base() {
  72. local phy="$1"
  73. json_select config
  74. [ "$auto_channel" -gt 0 ] && channel=acs_survey
  75. json_get_vars noscan htmode
  76. json_get_values ht_capab_list ht_capab
  77. ieee80211n=1
  78. ht_capab=
  79. case "$htmode" in
  80. VHT20|HT20) ;;
  81. HT40*|VHT40|VHT80|VHT160)
  82. case "$hwmode" in
  83. a)
  84. case "$(( ($channel / 4) % 2 ))" in
  85. 1) ht_capab="[HT40+]";;
  86. 0) ht_capab="[HT40-]";;
  87. esac
  88. ;;
  89. *)
  90. case "$htmode" in
  91. HT40+) ht_capab="[HT40+]";;
  92. HT40-) ht_capab="[HT40-]";;
  93. *)
  94. if [ "$channel" -lt 7 ]; then
  95. ht_capab="[HT40+]"
  96. else
  97. ht_capab="[HT40-]"
  98. fi
  99. ;;
  100. esac
  101. ;;
  102. esac
  103. [ "$auto_channel" -gt 0 ] && ht_capab="[HT40+]"
  104. ;;
  105. *) ieee80211n= ;;
  106. esac
  107. [ -n "$ieee80211n" ] && {
  108. append base_cfg "ieee80211n=1" "$N"
  109. json_get_vars \
  110. ldpc:1 \
  111. greenfield:0 \
  112. short_gi_20:1 \
  113. short_gi_40:1 \
  114. tx_stbc:1 \
  115. rx_stbc:3 \
  116. dsss_cck_40:1
  117. ht_cap_mask=0
  118. for cap in $(iw phy "$phy" info | grep 'Capabilities:' | cut -d: -f2); do
  119. ht_cap_mask="$(($ht_cap_mask | $cap))"
  120. done
  121. cap_rx_stbc=$((($ht_cap_mask >> 8) & 3))
  122. [ "$rx_stbc" -lt "$cap_rx_stbc" ] && cap_rx_stbc="$rx_stbc"
  123. ht_cap_mask="$(( ($ht_cap_mask & ~(0x300)) | ($cap_rx_stbc << 8) ))"
  124. mac80211_add_capabilities ht_capab_flags $ht_cap_mask \
  125. LDPC:0x1::$ldpc \
  126. GF:0x10::$greenfield \
  127. SHORT-GI-20:0x20::$short_gi_20 \
  128. SHORT-GI-40:0x40::$short_gi_40 \
  129. TX-STBC:0x80::$tx_stbc \
  130. RX-STBC1:0x300:0x100:1 \
  131. RX-STBC12:0x300:0x200:1 \
  132. RX-STBC123:0x300:0x300:1 \
  133. DSSS_CCK-40:0x1000::$dsss_cck_40
  134. ht_capab="$ht_capab$ht_capab_flags"
  135. [ -n "$ht_capab" ] && append base_cfg "ht_capab=$ht_capab" "$N"
  136. }
  137. # 802.11ac
  138. enable_ac=0
  139. idx="$channel"
  140. case "$htmode" in
  141. VHT20) enable_ac=1;;
  142. VHT40)
  143. case "$(( ($channel / 4) % 2 ))" in
  144. 1) idx=$(($channel + 2));;
  145. 0) idx=$(($channel - 2));;
  146. esac
  147. enable_ac=1
  148. append base_cfg "vht_oper_chwidth=0" "$N"
  149. append base_cfg "vht_oper_centr_freq_seg0_idx=$idx" "$N"
  150. ;;
  151. VHT80)
  152. case "$(( ($channel / 4) % 4 ))" in
  153. 1) idx=$(($channel + 6));;
  154. 2) idx=$(($channel + 2));;
  155. 3) idx=$(($channel - 2));;
  156. 0) idx=$(($channel - 6));;
  157. esac
  158. enable_ac=1
  159. append base_cfg "vht_oper_chwidth=1" "$N"
  160. append base_cfg "vht_oper_centr_freq_seg0_idx=$idx" "$N"
  161. ;;
  162. VHT160)
  163. case "$channel" in
  164. 36|40|44|48|52|56|60|64) idx=50;;
  165. 100|104|108|112|116|120|124|128) idx=114;;
  166. esac
  167. enable_ac=1
  168. append base_cfg "vht_oper_chwidth=2" "$N"
  169. append base_cfg "vht_oper_centr_freq_seg0_idx=$idx" "$N"
  170. ;;
  171. esac
  172. if [ "$enable_ac" != "0" ]; then
  173. json_get_vars \
  174. rxldpc:1 \
  175. short_gi_80:1 \
  176. short_gi_160:1 \
  177. tx_stbc_2by1:1 \
  178. su_beamformer:1 \
  179. su_beamformee:1 \
  180. mu_beamformer:1 \
  181. mu_beamformee:1 \
  182. vht_txop_ps:1 \
  183. htc_vht:1 \
  184. rx_antenna_pattern:1 \
  185. tx_antenna_pattern:1 \
  186. vht_max_a_mpdu_len_exp:7 \
  187. vht_max_mpdu:11454 \
  188. rx_stbc:4 \
  189. tx_stbc:4 \
  190. vht_link_adapt:3 \
  191. vht160:2
  192. append base_cfg "ieee80211ac=1" "$N"
  193. vht_cap=0
  194. for cap in $(iw phy "$phy" info | awk -F "[()]" '/VHT Capabilities/ { print $2 }'); do
  195. vht_cap="$(($vht_cap | $cap))"
  196. done
  197. cap_rx_stbc=$((($vht_cap >> 8) & 7))
  198. [ "$rx_stbc" -lt "$cap_rx_stbc" ] && cap_rx_stbc="$rx_stbc"
  199. ht_cap_mask="$(( ($vht_cap & ~(0x700)) | ($cap_rx_stbc << 8) ))"
  200. mac80211_add_capabilities vht_capab $vht_cap \
  201. RXLDPC:0x10::$rxldpc \
  202. SHORT-GI-80:0x20::$short_gi_80 \
  203. SHORT-GI-160:0x40::$short_gi_160 \
  204. TX-STBC-2BY1:0x80::$tx_stbc \
  205. SU-BEAMFORMER:0x800::$su_beamformer \
  206. SU-BEAMFORMEE:0x1000::$su_beamformee \
  207. MU-BEAMFORMER:0x80000::$mu_beamformer \
  208. MU-BEAMFORMEE:0x100000::$mu_beamformee \
  209. VHT-TXOP-PS:0x200000::$vht_txop_ps \
  210. HTC-VHT:0x400000::$htc_vht \
  211. RX-ANTENNA-PATTERN:0x10000000::$rx_antenna_pattern \
  212. TX-ANTENNA-PATTERN:0x20000000::$tx_antenna_pattern \
  213. RX-STBC1:0x700:0x100:1 \
  214. RX-STBC12:0x700:0x200:1 \
  215. RX-STBC123:0x700:0x300:1 \
  216. RX-STBC1234:0x700:0x400:1 \
  217. # supported Channel widths
  218. vht160_hw=0
  219. [ "$(($vht_cap & 12))" -eq 4 -a 1 -le "$vht160" ] && \
  220. vht160_hw=1
  221. [ "$(($vht_cap & 12))" -eq 8 -a 2 -le "$vht160" ] && \
  222. vht160_hw=2
  223. [ "$vht160_hw" = 1 ] && vht_capab="$vht_capab[VHT160]"
  224. [ "$vht160_hw" = 2 ] && vht_capab="$vht_capab[VHT160-80PLUS80]"
  225. # maximum MPDU length
  226. vht_max_mpdu_hw=3895
  227. [ "$(($vht_cap & 3))" -ge 1 -a 7991 -le "$vht_max_mpdu" ] && \
  228. vht_max_mpdu_hw=7991
  229. [ "$(($vht_cap & 3))" -ge 2 -a 11454 -le "$vht_max_mpdu" ] && \
  230. vht_max_mpdu_hw=11454
  231. [ "$vht_max_mpdu_hw" != 3895 ] && \
  232. vht_capab="$vht_capab[MAX-MPDU-$vht_max_mpdu_hw]"
  233. # maximum A-MPDU length exponent
  234. vht_max_a_mpdu_len_exp_hw=0
  235. [ "$(($vht_cap & 58720256))" -ge 8388608 -a 1 -le "$vht_max_a_mpdu_len_exp" ] && \
  236. vht_max_a_mpdu_len_exp_hw=1
  237. [ "$(($vht_cap & 58720256))" -ge 16777216 -a 2 -le "$vht_max_a_mpdu_len_exp" ] && \
  238. vht_max_a_mpdu_len_exp_hw=2
  239. [ "$(($vht_cap & 58720256))" -ge 25165824 -a 3 -le "$vht_max_a_mpdu_len_exp" ] && \
  240. vht_max_a_mpdu_len_exp_hw=3
  241. [ "$(($vht_cap & 58720256))" -ge 33554432 -a 4 -le "$vht_max_a_mpdu_len_exp" ] && \
  242. vht_max_a_mpdu_len_exp_hw=4
  243. [ "$(($vht_cap & 58720256))" -ge 41943040 -a 5 -le "$vht_max_a_mpdu_len_exp" ] && \
  244. vht_max_a_mpdu_len_exp_hw=5
  245. [ "$(($vht_cap & 58720256))" -ge 50331648 -a 6 -le "$vht_max_a_mpdu_len_exp" ] && \
  246. vht_max_a_mpdu_len_exp_hw=6
  247. [ "$(($vht_cap & 58720256))" -ge 58720256 -a 7 -le "$vht_max_a_mpdu_len_exp" ] && \
  248. vht_max_a_mpdu_len_exp_hw=7
  249. vht_capab="$vht_capab[MAX-A-MPDU-LEN-EXP$vht_max_a_mpdu_len_exp_hw]"
  250. # whether or not the STA supports link adaptation using VHT variant
  251. vht_link_adapt_hw=0
  252. [ "$(($vht_cap & 201326592))" -ge 134217728 -a 2 -le "$vht_link_adapt" ] && \
  253. vht_link_adapt_hw=2
  254. [ "$(($vht_cap & 201326592))" -ge 201326592 -a 3 -le "$vht_link_adapt" ] && \
  255. vht_link_adapt_hw=3
  256. [ "$vht_link_adapt_hw" != 0 ] && \
  257. vht_capab="$vht_capab[VHT-LINK-ADAPT-$vht_link_adapt_hw]"
  258. [ -n "$vht_capab" ] && append base_cfg "vht_capab=$vht_capab" "$N"
  259. fi
  260. hostapd_prepare_device_config "$hostapd_conf_file" nl80211
  261. cat >> "$hostapd_conf_file" <<EOF
  262. ${channel:+channel=$channel}
  263. ${noscan:+noscan=$noscan}
  264. $base_cfg
  265. EOF
  266. json_select ..
  267. }
  268. mac80211_hostapd_setup_bss() {
  269. local phy="$1"
  270. local ifname="$2"
  271. local macaddr="$3"
  272. local type="$4"
  273. hostapd_cfg=
  274. append hostapd_cfg "$type=$ifname" "$N"
  275. hostapd_set_bss_options hostapd_cfg "$vif" || return 1
  276. json_get_vars wds dtim_period max_listen_int
  277. set_default wds 0
  278. [ "$wds" -gt 0 ] && append hostapd_cfg "wds_sta=1" "$N"
  279. [ "$staidx" -gt 0 ] && append hostapd_cfg "start_disabled=1" "$N"
  280. cat >> /var/run/hostapd-$phy.conf <<EOF
  281. $hostapd_cfg
  282. bssid=$macaddr
  283. ${dtim_period:+dtim_period=$dtim_period}
  284. ${max_listen_int:+max_listen_interval=$max_listen_int}
  285. EOF
  286. }
  287. mac80211_generate_mac() {
  288. local phy="$1"
  289. local id="${macidx:-0}"
  290. local ref="$(cat /sys/class/ieee80211/${phy}/macaddress)"
  291. local mask="$(cat /sys/class/ieee80211/${phy}/address_mask)"
  292. [ "$mask" = "00:00:00:00:00:00" ] && mask="ff:ff:ff:ff:ff:ff";
  293. local oIFS="$IFS"; IFS=":"; set -- $mask; IFS="$oIFS"
  294. local mask1=$1
  295. local mask6=$6
  296. local oIFS="$IFS"; IFS=":"; set -- $ref; IFS="$oIFS"
  297. macidx=$(($id + 1))
  298. [ "$((0x$mask1))" -gt 0 ] && {
  299. b1="0x$1"
  300. [ "$id" -gt 0 ] && \
  301. b1=$(($b1 ^ ((($id - 1) << 2) | 0x2)))
  302. printf "%02x:%s:%s:%s:%s:%s" $b1 $2 $3 $4 $5 $6
  303. return
  304. }
  305. [ "$((0x$mask6))" -lt 255 ] && {
  306. printf "%s:%s:%s:%s:%s:%02x" $1 $2 $3 $4 $5 $(( 0x$6 ^ $id ))
  307. return
  308. }
  309. off2=$(( (0x$6 + $id) / 0x100 ))
  310. printf "%s:%s:%s:%s:%02x:%02x" \
  311. $1 $2 $3 $4 \
  312. $(( (0x$5 + $off2) % 0x100 )) \
  313. $(( (0x$6 + $id) % 0x100 ))
  314. }
  315. find_phy() {
  316. [ -n "$phy" -a -d /sys/class/ieee80211/$phy ] && return 0
  317. [ -n "$path" ] && {
  318. for phy in /sys/devices/$path/ieee80211/phy*; do
  319. [ -e "$phy" ] && {
  320. phy="${phy##*/}"
  321. return 0
  322. }
  323. done
  324. }
  325. [ -n "$macaddr" ] && {
  326. for phy in $(ls /sys/class/ieee80211 2>/dev/null); do
  327. grep -i -q "$macaddr" "/sys/class/ieee80211/${phy}/macaddress" && return 0
  328. done
  329. }
  330. return 1
  331. }
  332. mac80211_check_ap() {
  333. has_ap=1
  334. }
  335. mac80211_prepare_vif() {
  336. json_select config
  337. json_get_vars ifname mode ssid wds powersave macaddr
  338. [ -n "$ifname" ] || ifname="wlan${phy#phy}${if_idx:+-$if_idx}"
  339. if_idx=$((${if_idx:-0} + 1))
  340. set_default wds 0
  341. set_default powersave 0
  342. json_select ..
  343. [ -n "$macaddr" ] || {
  344. macaddr="$(mac80211_generate_mac $phy)"
  345. macidx="$(($macidx + 1))"
  346. }
  347. json_add_object data
  348. json_add_string ifname "$ifname"
  349. json_close_object
  350. json_select config
  351. # It is far easier to delete and create the desired interface
  352. case "$mode" in
  353. adhoc)
  354. iw phy "$phy" interface add "$ifname" type adhoc
  355. ;;
  356. ap)
  357. # Hostapd will handle recreating the interface and
  358. # subsequent virtual APs belonging to the same PHY
  359. if [ -n "$hostapd_ctrl" ]; then
  360. type=bss
  361. else
  362. type=interface
  363. fi
  364. mac80211_hostapd_setup_bss "$phy" "$ifname" "$macaddr" "$type" || return
  365. [ -n "$hostapd_ctrl" ] || {
  366. iw phy "$phy" interface add "$ifname" type managed
  367. hostapd_ctrl="${hostapd_ctrl:-/var/run/hostapd/$ifname}"
  368. }
  369. ;;
  370. mesh)
  371. json_get_vars key mesh_id
  372. if [ -n "$key" ]; then
  373. iw phy "$phy" interface add "$ifname" type mp
  374. else
  375. iw phy "$phy" interface add "$ifname" type mp mesh_id "$mesh_id"
  376. fi
  377. ;;
  378. monitor)
  379. iw phy "$phy" interface add "$ifname" type monitor
  380. ;;
  381. sta)
  382. local wdsflag=
  383. staidx="$(($staidx + 1))"
  384. [ "$wds" -gt 0 ] && wdsflag="4addr on"
  385. iw phy "$phy" interface add "$ifname" type managed $wdsflag
  386. [ "$powersave" -gt 0 ] && powersave="on" || powersave="off"
  387. iw "$ifname" set power_save "$powersave"
  388. ;;
  389. esac
  390. case "$mode" in
  391. monitor|mesh)
  392. [ "$auto_channel" -gt 0 ] || iw dev "$ifname" set channel "$channel" $htmode
  393. ;;
  394. esac
  395. if [ "$mode" != "ap" ]; then
  396. # ALL ap functionality will be passed to hostapd
  397. # All interfaces must have unique mac addresses
  398. # which can either be explicitly set in the device
  399. # section, or automatically generated
  400. ifconfig "$ifname" hw ether "$macaddr"
  401. fi
  402. json_select ..
  403. }
  404. mac80211_setup_supplicant() {
  405. wpa_supplicant_prepare_interface "$ifname" nl80211 || return 1
  406. wpa_supplicant_add_network "$ifname"
  407. wpa_supplicant_run "$ifname" ${hostapd_ctrl:+-H $hostapd_ctrl}
  408. }
  409. mac80211_setup_adhoc() {
  410. json_get_vars bssid ssid key mcast_rate
  411. keyspec=
  412. [ "$auth_type" == "wep" ] && {
  413. set_default key 1
  414. case "$key" in
  415. [1234])
  416. local idx
  417. for idx in 1 2 3 4; do
  418. json_get_var ikey "key$idx"
  419. [ -n "$ikey" ] && {
  420. ikey="$(($idx - 1)):$(prepare_key_wep "$ikey")"
  421. [ $idx -eq $key ] && ikey="d:$ikey"
  422. append keyspec "$ikey"
  423. }
  424. done
  425. ;;
  426. *)
  427. append keyspec "d:0:$(prepare_key_wep "$key")"
  428. ;;
  429. esac
  430. }
  431. brstr=
  432. for br in $basic_rate_list; do
  433. hostapd_add_rate brstr "$br"
  434. done
  435. mcval=
  436. [ -n "$mcast_rate" ] && hostapd_add_rate mcval "$mcast_rate"
  437. iw dev "$ifname" ibss join "$ssid" $freq $htmode fixed-freq $bssid \
  438. ${beacon_int:+beacon-interval $beacon_int} \
  439. ${brstr:+basic-rates $brstr} \
  440. ${mcval:+mcast-rate $mcval} \
  441. ${keyspec:+keys $keyspec}
  442. }
  443. mac80211_setup_vif() {
  444. local name="$1"
  445. local failed
  446. json_select data
  447. json_get_vars ifname
  448. json_select ..
  449. json_select config
  450. json_get_vars mode
  451. json_get_var vif_txpower txpower
  452. ifconfig "$ifname" up || {
  453. wireless_setup_vif_failed IFUP_ERROR
  454. json_select ..
  455. return
  456. }
  457. set_default vif_txpower "$txpower"
  458. [ -z "$vif_txpower" ] || iw dev "$ifname" set txpower fixed "${vif_txpower%%.*}00"
  459. case "$mode" in
  460. mesh)
  461. for var in $MP_CONFIG_INT $MP_CONFIG_BOOL $MP_CONFIG_STRING; do
  462. json_get_var mp_val "$var"
  463. [ -n "$mp_val" ] && iw dev "$ifname" set mesh_param "$var" "$mp_val"
  464. done
  465. # authsae
  466. json_get_vars key
  467. if [ -n "$key" ]; then
  468. if [ -e "/lib/wifi/authsae.sh" ]; then
  469. . /lib/wifi/authsae.sh
  470. authsae_start_interface || failed=1
  471. else
  472. wireless_setup_vif_failed AUTHSAE_NOT_INSTALLED
  473. json_select ..
  474. return
  475. fi
  476. fi
  477. ;;
  478. adhoc)
  479. wireless_vif_parse_encryption
  480. if [ "$wpa" -gt 0 -o "$auto_channel" -gt 0 ]; then
  481. mac80211_setup_supplicant || failed=1
  482. else
  483. mac80211_setup_adhoc
  484. fi
  485. ;;
  486. sta)
  487. mac80211_setup_supplicant || failed=1
  488. ;;
  489. esac
  490. json_select ..
  491. [ -n "$failed" ] || wireless_add_vif "$name" "$ifname"
  492. }
  493. get_freq() {
  494. local phy="$1"
  495. local chan="$2"
  496. iw "$phy" info | grep -E -m1 "(\* ${chan:-....} MHz${chan:+|\\[$chan\\]})" | grep MHz | awk '{print $2}'
  497. }
  498. mac80211_interface_cleanup() {
  499. local phy="$1"
  500. for wdev in $(list_phy_interfaces "$phy"); do
  501. ifconfig "$wdev" down 2>/dev/null
  502. iw dev "$wdev" del
  503. done
  504. }
  505. drv_mac80211_cleanup() {
  506. hostapd_common_cleanup
  507. }
  508. drv_mac80211_setup() {
  509. json_select config
  510. json_get_vars \
  511. phy macaddr path \
  512. country chanbw distance \
  513. txpower antenna_gain \
  514. rxantenna txantenna \
  515. frag rts beacon_int
  516. json_get_values basic_rate_list basic_rate
  517. json_select ..
  518. find_phy || {
  519. echo "Could not find PHY for device '$1'"
  520. wireless_set_retry 0
  521. return 1
  522. }
  523. wireless_set_data phy="$phy"
  524. mac80211_interface_cleanup "$phy"
  525. # convert channel to frequency
  526. [ "$auto_channel" -gt 0 ] || freq="$(get_freq "$phy" "$channel")"
  527. [ -n "$country" ] && {
  528. iw reg get | grep -q "^country $country:" || {
  529. iw reg set "$country"
  530. sleep 1
  531. }
  532. }
  533. hostapd_conf_file="/var/run/hostapd-$phy.conf"
  534. no_ap=1
  535. macidx=0
  536. staidx=0
  537. [ -n "$chanbw" ] && {
  538. for file in /sys/kernel/debug/ieee80211/$phy/ath9k/chanbw /sys/kernel/debug/ieee80211/$phy/ath5k/bwmode; do
  539. [ -f "$file" ] && echo "$chanbw" > "$file"
  540. done
  541. }
  542. set_default rxantenna all
  543. set_default txantenna all
  544. set_default distance 0
  545. set_default antenna_gain 0
  546. iw phy "$phy" set antenna $txantenna $rxantenna >/dev/null 2>&1
  547. iw phy "$phy" set antenna_gain $antenna_gain
  548. iw phy "$phy" set distance "$distance"
  549. [ -n "$frag" ] && iw phy "$phy" set frag "${frag%%.*}"
  550. [ -n "$rts" ] && iw phy "$phy" set rts "${rts%%.*}"
  551. has_ap=
  552. hostapd_ctrl=
  553. for_each_interface "ap" mac80211_check_ap
  554. rm -f "$hostapd_conf_file"
  555. [ -n "$has_ap" ] && mac80211_hostapd_setup_base "$phy"
  556. for_each_interface "sta adhoc mesh monitor" mac80211_prepare_vif
  557. for_each_interface "ap" mac80211_prepare_vif
  558. [ -n "$hostapd_ctrl" ] && {
  559. /usr/sbin/hostapd -P /var/run/wifi-$phy.pid -B "$hostapd_conf_file"
  560. ret="$?"
  561. wireless_add_process "$(cat /var/run/wifi-$phy.pid)" "/usr/sbin/hostapd" 1
  562. [ "$ret" != 0 ] && {
  563. wireless_setup_failed HOSTAPD_START_FAILED
  564. return
  565. }
  566. }
  567. for_each_interface "ap sta adhoc mesh monitor" mac80211_setup_vif
  568. wireless_set_up
  569. }
  570. list_phy_interfaces() {
  571. local phy="$1"
  572. if [ -d "/sys/class/ieee80211/${phy}/device/net" ]; then
  573. ls "/sys/class/ieee80211/${phy}/device/net" 2>/dev/null;
  574. else
  575. ls "/sys/class/ieee80211/${phy}/device" 2>/dev/null | grep net: | sed -e 's,net:,,g'
  576. fi
  577. }
  578. drv_mac80211_teardown() {
  579. wireless_process_kill_all
  580. json_select data
  581. json_get_vars phy
  582. json_select ..
  583. mac80211_interface_cleanup "$phy"
  584. }
  585. add_driver mac80211