mac80211.sh 20 KB

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