mac80211.sh 18 KB

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