mac80211.sh 21 KB

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