mac80211.sh 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  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. NEWAPLIST=
  15. OLDAPLIST=
  16. NEWSPLIST=
  17. OLDSPLIST=
  18. NEWUMLIST=
  19. OLDUMLIST=
  20. drv_mac80211_init_device_config() {
  21. hostapd_common_add_device_config
  22. config_add_string path phy 'macaddr:macaddr'
  23. config_add_string tx_burst
  24. config_add_string distance
  25. config_add_int beacon_int chanbw frag rts
  26. config_add_int rxantenna txantenna antenna_gain txpower
  27. config_add_boolean noscan ht_coex acs_exclude_dfs
  28. config_add_array ht_capab
  29. config_add_array channels
  30. config_add_array scan_list
  31. config_add_boolean \
  32. rxldpc \
  33. short_gi_80 \
  34. short_gi_160 \
  35. tx_stbc_2by1 \
  36. su_beamformer \
  37. su_beamformee \
  38. mu_beamformer \
  39. mu_beamformee \
  40. he_su_beamformer \
  41. he_su_beamformee \
  42. he_mu_beamformer \
  43. vht_txop_ps \
  44. htc_vht \
  45. rx_antenna_pattern \
  46. tx_antenna_pattern \
  47. he_spr_sr_control \
  48. he_twt_required
  49. config_add_int \
  50. vht_max_a_mpdu_len_exp \
  51. vht_max_mpdu \
  52. vht_link_adapt \
  53. vht160 \
  54. rx_stbc \
  55. tx_stbc \
  56. he_bss_color \
  57. he_spr_non_srg_obss_pd_max_offset
  58. config_add_boolean \
  59. ldpc \
  60. greenfield \
  61. short_gi_20 \
  62. short_gi_40 \
  63. max_amsdu \
  64. dsss_cck_40
  65. }
  66. drv_mac80211_init_iface_config() {
  67. hostapd_common_add_bss_config
  68. config_add_string 'macaddr:macaddr' ifname
  69. config_add_boolean wds powersave enable
  70. config_add_string wds_bridge
  71. config_add_int maxassoc
  72. config_add_int max_listen_int
  73. config_add_int dtim_period
  74. config_add_int start_disabled
  75. # mesh
  76. config_add_string mesh_id
  77. config_add_int $MP_CONFIG_INT
  78. config_add_boolean $MP_CONFIG_BOOL
  79. config_add_string $MP_CONFIG_STRING
  80. }
  81. mac80211_add_capabilities() {
  82. local __var="$1"; shift
  83. local __mask="$1"; shift
  84. local __out= oifs
  85. oifs="$IFS"
  86. IFS=:
  87. for capab in "$@"; do
  88. set -- $capab
  89. [ "$(($4))" -gt 0 ] || continue
  90. [ "$(($__mask & $2))" -eq "$((${3:-$2}))" ] || continue
  91. __out="$__out[$1]"
  92. done
  93. IFS="$oifs"
  94. export -n -- "$__var=$__out"
  95. }
  96. mac80211_add_he_capabilities() {
  97. local __out= oifs
  98. oifs="$IFS"
  99. IFS=:
  100. for capab in "$@"; do
  101. set -- $capab
  102. [ "$(($4))" -gt 0 ] || continue
  103. [ "$(((0x$2) & $3))" -gt 0 ] || {
  104. eval "$1=0"
  105. continue
  106. }
  107. append base_cfg "$1=1" "$N"
  108. done
  109. IFS="$oifs"
  110. }
  111. mac80211_hostapd_setup_base() {
  112. local phy="$1"
  113. json_select config
  114. [ "$auto_channel" -gt 0 ] && channel=acs_survey
  115. [ "$auto_channel" -gt 0 ] && json_get_vars acs_exclude_dfs
  116. [ -n "$acs_exclude_dfs" ] && [ "$acs_exclude_dfs" -gt 0 ] &&
  117. append base_cfg "acs_exclude_dfs=1" "$N"
  118. json_get_vars noscan ht_coex
  119. json_get_values ht_capab_list ht_capab tx_burst
  120. json_get_values channel_list channels
  121. [ "$auto_channel" = 0 ] && [ -z "$channel_list" ] && \
  122. channel_list="$channel"
  123. set_default noscan 0
  124. [ "$noscan" -gt 0 ] && hostapd_noscan=1
  125. [ "$tx_burst" = 0 ] && tx_burst=
  126. chan_ofs=0
  127. [ "$band" = "6g" ] && chan_ofs=1
  128. ieee80211n=1
  129. ht_capab=
  130. case "$htmode" in
  131. VHT20|HT20|HE20) ;;
  132. HT40*|VHT40|VHT80|VHT160|HE40|HE80|HE160)
  133. case "$hwmode" in
  134. a)
  135. case "$(( (($channel / 4) + $chan_ofs) % 2 ))" in
  136. 1) ht_capab="[HT40+]";;
  137. 0) ht_capab="[HT40-]";;
  138. esac
  139. ;;
  140. *)
  141. case "$htmode" in
  142. HT40+) ht_capab="[HT40+]";;
  143. HT40-) ht_capab="[HT40-]";;
  144. *)
  145. if [ "$channel" -lt 7 ]; then
  146. ht_capab="[HT40+]"
  147. else
  148. ht_capab="[HT40-]"
  149. fi
  150. ;;
  151. esac
  152. ;;
  153. esac
  154. [ "$auto_channel" -gt 0 ] && ht_capab="[HT40+]"
  155. ;;
  156. *) ieee80211n= ;;
  157. esac
  158. [ -n "$ieee80211n" ] && {
  159. append base_cfg "ieee80211n=1" "$N"
  160. set_default ht_coex 0
  161. append base_cfg "ht_coex=$ht_coex" "$N"
  162. json_get_vars \
  163. ldpc:1 \
  164. greenfield:0 \
  165. short_gi_20:1 \
  166. short_gi_40:1 \
  167. tx_stbc:1 \
  168. rx_stbc:3 \
  169. max_amsdu:1 \
  170. dsss_cck_40:1
  171. ht_cap_mask=0
  172. for cap in $(iw phy "$phy" info | grep 'Capabilities:' | cut -d: -f2); do
  173. ht_cap_mask="$(($ht_cap_mask | $cap))"
  174. done
  175. cap_rx_stbc=$((($ht_cap_mask >> 8) & 3))
  176. [ "$rx_stbc" -lt "$cap_rx_stbc" ] && cap_rx_stbc="$rx_stbc"
  177. ht_cap_mask="$(( ($ht_cap_mask & ~(0x300)) | ($cap_rx_stbc << 8) ))"
  178. mac80211_add_capabilities ht_capab_flags $ht_cap_mask \
  179. LDPC:0x1::$ldpc \
  180. GF:0x10::$greenfield \
  181. SHORT-GI-20:0x20::$short_gi_20 \
  182. SHORT-GI-40:0x40::$short_gi_40 \
  183. TX-STBC:0x80::$tx_stbc \
  184. RX-STBC1:0x300:0x100:1 \
  185. RX-STBC12:0x300:0x200:1 \
  186. RX-STBC123:0x300:0x300:1 \
  187. MAX-AMSDU-7935:0x800::$max_amsdu \
  188. DSSS_CCK-40:0x1000::$dsss_cck_40
  189. ht_capab="$ht_capab$ht_capab_flags"
  190. [ -n "$ht_capab" ] && append base_cfg "ht_capab=$ht_capab" "$N"
  191. }
  192. # 802.11ac
  193. enable_ac=0
  194. vht_oper_chwidth=0
  195. vht_center_seg0=
  196. idx="$channel"
  197. case "$htmode" in
  198. VHT20|HE20) enable_ac=1;;
  199. VHT40|HE40)
  200. case "$(( (($channel / 4) + $chan_ofs) % 2 ))" in
  201. 1) idx=$(($channel + 2));;
  202. 0) idx=$(($channel - 2));;
  203. esac
  204. enable_ac=1
  205. vht_center_seg0=$idx
  206. ;;
  207. VHT80|HE80)
  208. case "$(( (($channel / 4) + $chan_ofs) % 4 ))" in
  209. 1) idx=$(($channel + 6));;
  210. 2) idx=$(($channel + 2));;
  211. 3) idx=$(($channel - 2));;
  212. 0) idx=$(($channel - 6));;
  213. esac
  214. enable_ac=1
  215. vht_oper_chwidth=1
  216. vht_center_seg0=$idx
  217. ;;
  218. VHT160|HE160)
  219. if [ "$band" = "6g" ]; then
  220. case "$channel" in
  221. 1|5|9|13|17|21|25|29) idx=15;;
  222. 33|37|41|45|49|53|57|61) idx=47;;
  223. 65|69|73|77|81|85|89|93) idx=79;;
  224. 97|101|105|109|113|117|121|125) idx=111;;
  225. 129|133|137|141|145|149|153|157) idx=143;;
  226. 161|165|169|173|177|181|185|189) idx=175;;
  227. 193|197|201|205|209|213|217|221) idx=207;;
  228. esac
  229. else
  230. case "$channel" in
  231. 36|40|44|48|52|56|60|64) idx=50;;
  232. 100|104|108|112|116|120|124|128) idx=114;;
  233. esac
  234. fi
  235. enable_ac=1
  236. vht_oper_chwidth=2
  237. vht_center_seg0=$idx
  238. ;;
  239. esac
  240. [ "$band" = "6g" ] && {
  241. op_class=
  242. case "$htmode" in
  243. HE20) op_class=131;;
  244. HE*) op_class=$((132 + $vht_oper_chwidth))
  245. esac
  246. [ -n "$op_class" ] && append base_cfg "op_class=$op_class" "$N"
  247. }
  248. [ "$hwmode" = "a" ] || enable_ac=0
  249. if [ "$enable_ac" != "0" ]; then
  250. json_get_vars \
  251. rxldpc:1 \
  252. short_gi_80:1 \
  253. short_gi_160:1 \
  254. tx_stbc_2by1:1 \
  255. su_beamformer:1 \
  256. su_beamformee:1 \
  257. mu_beamformer:1 \
  258. mu_beamformee:1 \
  259. vht_txop_ps:1 \
  260. htc_vht:1 \
  261. rx_antenna_pattern:1 \
  262. tx_antenna_pattern:1 \
  263. vht_max_a_mpdu_len_exp:7 \
  264. vht_max_mpdu:11454 \
  265. rx_stbc:4 \
  266. vht_link_adapt:3 \
  267. vht160:2
  268. set_default tx_burst 2.0
  269. append base_cfg "ieee80211ac=1" "$N"
  270. vht_cap=0
  271. for cap in $(iw phy "$phy" info | awk -F "[()]" '/VHT Capabilities/ { print $2 }'); do
  272. vht_cap="$(($vht_cap | $cap))"
  273. done
  274. append base_cfg "vht_oper_chwidth=$vht_oper_chwidth" "$N"
  275. append base_cfg "vht_oper_centr_freq_seg0_idx=$vht_center_seg0" "$N"
  276. cap_rx_stbc=$((($vht_cap >> 8) & 7))
  277. [ "$rx_stbc" -lt "$cap_rx_stbc" ] && cap_rx_stbc="$rx_stbc"
  278. vht_cap="$(( ($vht_cap & ~(0x700)) | ($cap_rx_stbc << 8) ))"
  279. mac80211_add_capabilities vht_capab $vht_cap \
  280. RXLDPC:0x10::$rxldpc \
  281. SHORT-GI-80:0x20::$short_gi_80 \
  282. SHORT-GI-160:0x40::$short_gi_160 \
  283. TX-STBC-2BY1:0x80::$tx_stbc_2by1 \
  284. SU-BEAMFORMER:0x800::$su_beamformer \
  285. SU-BEAMFORMEE:0x1000::$su_beamformee \
  286. MU-BEAMFORMER:0x80000::$mu_beamformer \
  287. MU-BEAMFORMEE:0x100000::$mu_beamformee \
  288. VHT-TXOP-PS:0x200000::$vht_txop_ps \
  289. HTC-VHT:0x400000::$htc_vht \
  290. RX-ANTENNA-PATTERN:0x10000000::$rx_antenna_pattern \
  291. TX-ANTENNA-PATTERN:0x20000000::$tx_antenna_pattern \
  292. RX-STBC-1:0x700:0x100:1 \
  293. RX-STBC-12:0x700:0x200:1 \
  294. RX-STBC-123:0x700:0x300:1 \
  295. RX-STBC-1234:0x700:0x400:1 \
  296. # supported Channel widths
  297. vht160_hw=0
  298. [ "$(($vht_cap & 12))" -eq 4 -a 1 -le "$vht160" ] && \
  299. vht160_hw=1
  300. [ "$(($vht_cap & 12))" -eq 8 -a 2 -le "$vht160" ] && \
  301. vht160_hw=2
  302. [ "$vht160_hw" = 1 ] && vht_capab="$vht_capab[VHT160]"
  303. [ "$vht160_hw" = 2 ] && vht_capab="$vht_capab[VHT160-80PLUS80]"
  304. # maximum MPDU length
  305. vht_max_mpdu_hw=3895
  306. [ "$(($vht_cap & 3))" -ge 1 -a 7991 -le "$vht_max_mpdu" ] && \
  307. vht_max_mpdu_hw=7991
  308. [ "$(($vht_cap & 3))" -ge 2 -a 11454 -le "$vht_max_mpdu" ] && \
  309. vht_max_mpdu_hw=11454
  310. [ "$vht_max_mpdu_hw" != 3895 ] && \
  311. vht_capab="$vht_capab[MAX-MPDU-$vht_max_mpdu_hw]"
  312. # maximum A-MPDU length exponent
  313. vht_max_a_mpdu_len_exp_hw=0
  314. [ "$(($vht_cap & 58720256))" -ge 8388608 -a 1 -le "$vht_max_a_mpdu_len_exp" ] && \
  315. vht_max_a_mpdu_len_exp_hw=1
  316. [ "$(($vht_cap & 58720256))" -ge 16777216 -a 2 -le "$vht_max_a_mpdu_len_exp" ] && \
  317. vht_max_a_mpdu_len_exp_hw=2
  318. [ "$(($vht_cap & 58720256))" -ge 25165824 -a 3 -le "$vht_max_a_mpdu_len_exp" ] && \
  319. vht_max_a_mpdu_len_exp_hw=3
  320. [ "$(($vht_cap & 58720256))" -ge 33554432 -a 4 -le "$vht_max_a_mpdu_len_exp" ] && \
  321. vht_max_a_mpdu_len_exp_hw=4
  322. [ "$(($vht_cap & 58720256))" -ge 41943040 -a 5 -le "$vht_max_a_mpdu_len_exp" ] && \
  323. vht_max_a_mpdu_len_exp_hw=5
  324. [ "$(($vht_cap & 58720256))" -ge 50331648 -a 6 -le "$vht_max_a_mpdu_len_exp" ] && \
  325. vht_max_a_mpdu_len_exp_hw=6
  326. [ "$(($vht_cap & 58720256))" -ge 58720256 -a 7 -le "$vht_max_a_mpdu_len_exp" ] && \
  327. vht_max_a_mpdu_len_exp_hw=7
  328. vht_capab="$vht_capab[MAX-A-MPDU-LEN-EXP$vht_max_a_mpdu_len_exp_hw]"
  329. # whether or not the STA supports link adaptation using VHT variant
  330. vht_link_adapt_hw=0
  331. [ "$(($vht_cap & 201326592))" -ge 134217728 -a 2 -le "$vht_link_adapt" ] && \
  332. vht_link_adapt_hw=2
  333. [ "$(($vht_cap & 201326592))" -ge 201326592 -a 3 -le "$vht_link_adapt" ] && \
  334. vht_link_adapt_hw=3
  335. [ "$vht_link_adapt_hw" != 0 ] && \
  336. vht_capab="$vht_capab[VHT-LINK-ADAPT-$vht_link_adapt_hw]"
  337. [ -n "$vht_capab" ] && append base_cfg "vht_capab=$vht_capab" "$N"
  338. fi
  339. # 802.11ax
  340. enable_ax=0
  341. case "$htmode" in
  342. HE*) enable_ax=1 ;;
  343. esac
  344. if [ "$enable_ax" != "0" ]; then
  345. json_get_vars \
  346. he_su_beamformer:1 \
  347. he_su_beamformee:0 \
  348. he_mu_beamformer:1 \
  349. he_twt_required:0 \
  350. he_spr_sr_control:0 \
  351. he_spr_non_srg_obss_pd_max_offset:1 \
  352. he_bss_color
  353. he_phy_cap=$(iw phy "$phy" info | awk -F "[()]" '/HE PHY Capabilities/ { print $2 }' | head -1)
  354. he_phy_cap=${he_phy_cap:2}
  355. he_mac_cap=$(iw phy "$phy" info | awk -F "[()]" '/HE MAC Capabilities/ { print $2 }' | head -1)
  356. he_mac_cap=${he_mac_cap:2}
  357. append base_cfg "ieee80211ax=1" "$N"
  358. [ -n "$he_bss_color" ] && append base_cfg "he_bss_color=$he_bss_color" "$N"
  359. [ "$hwmode" = "a" ] && {
  360. append base_cfg "he_oper_chwidth=$vht_oper_chwidth" "$N"
  361. append base_cfg "he_oper_centr_freq_seg0_idx=$vht_center_seg0" "$N"
  362. }
  363. mac80211_add_he_capabilities \
  364. he_su_beamformer:${he_phy_cap:6:2}:0x80:$he_su_beamformer \
  365. he_su_beamformee:${he_phy_cap:8:2}:0x1:$he_su_beamformee \
  366. he_mu_beamformer:${he_phy_cap:8:2}:0x2:$he_mu_beamformer \
  367. he_spr_sr_control:${he_phy_cap:14:2}:0x1:$he_spr_sr_control \
  368. he_twt_required:${he_mac_cap:0:2}:0x6:$he_twt_required
  369. [ "$he_spr_sr_control" -gt 0 ] && append base_cfg "he_spr_non_srg_obss_pd_max_offset=$he_spr_non_srg_obss_pd_max_offset" "$N"
  370. append base_cfg "he_default_pe_duration=4" "$N"
  371. append base_cfg "he_rts_threshold=1023" "$N"
  372. append base_cfg "he_mu_edca_qos_info_param_count=0" "$N"
  373. append base_cfg "he_mu_edca_qos_info_q_ack=0" "$N"
  374. append base_cfg "he_mu_edca_qos_info_queue_request=0" "$N"
  375. append base_cfg "he_mu_edca_qos_info_txop_request=0" "$N"
  376. append base_cfg "he_mu_edca_ac_be_aifsn=8" "$N"
  377. append base_cfg "he_mu_edca_ac_be_aci=0" "$N"
  378. append base_cfg "he_mu_edca_ac_be_ecwmin=9" "$N"
  379. append base_cfg "he_mu_edca_ac_be_ecwmax=10" "$N"
  380. append base_cfg "he_mu_edca_ac_be_timer=255" "$N"
  381. append base_cfg "he_mu_edca_ac_bk_aifsn=15" "$N"
  382. append base_cfg "he_mu_edca_ac_bk_aci=1" "$N"
  383. append base_cfg "he_mu_edca_ac_bk_ecwmin=9" "$N"
  384. append base_cfg "he_mu_edca_ac_bk_ecwmax=10" "$N"
  385. append base_cfg "he_mu_edca_ac_bk_timer=255" "$N"
  386. append base_cfg "he_mu_edca_ac_vi_ecwmin=5" "$N"
  387. append base_cfg "he_mu_edca_ac_vi_ecwmax=7" "$N"
  388. append base_cfg "he_mu_edca_ac_vi_aifsn=5" "$N"
  389. append base_cfg "he_mu_edca_ac_vi_aci=2" "$N"
  390. append base_cfg "he_mu_edca_ac_vi_timer=255" "$N"
  391. append base_cfg "he_mu_edca_ac_vo_aifsn=5" "$N"
  392. append base_cfg "he_mu_edca_ac_vo_aci=3" "$N"
  393. append base_cfg "he_mu_edca_ac_vo_ecwmin=5" "$N"
  394. append base_cfg "he_mu_edca_ac_vo_ecwmax=7" "$N"
  395. append base_cfg "he_mu_edca_ac_vo_timer=255" "$N"
  396. fi
  397. hostapd_prepare_device_config "$hostapd_conf_file" nl80211
  398. cat >> "$hostapd_conf_file" <<EOF
  399. ${channel:+channel=$channel}
  400. ${channel_list:+chanlist=$channel_list}
  401. ${hostapd_noscan:+noscan=1}
  402. ${tx_burst:+tx_queue_data2_burst=$tx_burst}
  403. $base_cfg
  404. EOF
  405. json_select ..
  406. radio_md5sum=$(md5sum $hostapd_conf_file | cut -d" " -f1)
  407. echo "radio_config_id=${radio_md5sum}" >> $hostapd_conf_file
  408. }
  409. mac80211_hostapd_setup_bss() {
  410. local phy="$1"
  411. local ifname="$2"
  412. local macaddr="$3"
  413. local type="$4"
  414. hostapd_cfg=
  415. append hostapd_cfg "$type=$ifname" "$N"
  416. hostapd_set_bss_options hostapd_cfg "$phy" "$vif" || return 1
  417. json_get_vars wds wds_bridge dtim_period max_listen_int start_disabled
  418. set_default wds 0
  419. set_default start_disabled 0
  420. [ "$wds" -gt 0 ] && {
  421. append hostapd_cfg "wds_sta=1" "$N"
  422. [ -n "$wds_bridge" ] && append hostapd_cfg "wds_bridge=$wds_bridge" "$N"
  423. }
  424. [ "$staidx" -gt 0 -o "$start_disabled" -eq 1 ] && append hostapd_cfg "start_disabled=1" "$N"
  425. cat >> /var/run/hostapd-$phy.conf <<EOF
  426. $hostapd_cfg
  427. bssid=$macaddr
  428. ${dtim_period:+dtim_period=$dtim_period}
  429. ${max_listen_int:+max_listen_interval=$max_listen_int}
  430. EOF
  431. }
  432. mac80211_get_addr() {
  433. local phy="$1"
  434. local idx="$(($2 + 1))"
  435. head -n $idx /sys/class/ieee80211/${phy}/addresses | tail -n1
  436. }
  437. mac80211_generate_mac() {
  438. local phy="$1"
  439. local id="${macidx:-0}"
  440. local ref="$(cat /sys/class/ieee80211/${phy}/macaddress)"
  441. local mask="$(cat /sys/class/ieee80211/${phy}/address_mask)"
  442. [ "$mask" = "00:00:00:00:00:00" ] && {
  443. mask="ff:ff:ff:ff:ff:ff";
  444. [ "$(wc -l < /sys/class/ieee80211/${phy}/addresses)" -gt $id ] && {
  445. addr="$(mac80211_get_addr "$phy" "$id")"
  446. [ -n "$addr" ] && {
  447. echo "$addr"
  448. return
  449. }
  450. }
  451. }
  452. local oIFS="$IFS"; IFS=":"; set -- $mask; IFS="$oIFS"
  453. local mask1=$1
  454. local mask6=$6
  455. local oIFS="$IFS"; IFS=":"; set -- $ref; IFS="$oIFS"
  456. macidx=$(($id + 1))
  457. [ "$((0x$mask1))" -gt 0 ] && {
  458. b1="0x$1"
  459. [ "$id" -gt 0 ] && \
  460. b1=$(($b1 ^ ((($id - !($b1 & 2)) << 2)) | 0x2))
  461. printf "%02x:%s:%s:%s:%s:%s" $b1 $2 $3 $4 $5 $6
  462. return
  463. }
  464. [ "$((0x$mask6))" -lt 255 ] && {
  465. printf "%s:%s:%s:%s:%s:%02x" $1 $2 $3 $4 $5 $(( 0x$6 ^ $id ))
  466. return
  467. }
  468. off2=$(( (0x$6 + $id) / 0x100 ))
  469. printf "%s:%s:%s:%s:%02x:%02x" \
  470. $1 $2 $3 $4 \
  471. $(( (0x$5 + $off2) % 0x100 )) \
  472. $(( (0x$6 + $id) % 0x100 ))
  473. }
  474. find_phy() {
  475. [ -n "$phy" -a -d /sys/class/ieee80211/$phy ] && return 0
  476. [ -n "$path" ] && {
  477. phy="$(iwinfo nl80211 phyname "path=$path")"
  478. [ -n "$phy" ] && return 0
  479. }
  480. [ -n "$macaddr" ] && {
  481. for phy in $(ls /sys/class/ieee80211 2>/dev/null); do
  482. grep -i -q "$macaddr" "/sys/class/ieee80211/${phy}/macaddress" && return 0
  483. done
  484. }
  485. return 1
  486. }
  487. mac80211_check_ap() {
  488. has_ap=1
  489. }
  490. mac80211_iw_interface_add() {
  491. local phy="$1"
  492. local ifname="$2"
  493. local type="$3"
  494. local wdsflag="$4"
  495. local rc
  496. local oldifname
  497. iw phy "$phy" interface add "$ifname" type "$type" $wdsflag >/dev/null 2>&1
  498. rc="$?"
  499. [ "$rc" = 233 ] && {
  500. # Device might have just been deleted, give the kernel some time to finish cleaning it up
  501. sleep 1
  502. iw phy "$phy" interface add "$ifname" type "$type" $wdsflag >/dev/null 2>&1
  503. rc="$?"
  504. }
  505. [ "$rc" = 233 ] && {
  506. # Keep matching pre-existing interface
  507. [ -d "/sys/class/ieee80211/${phy}/device/net/${ifname}" ] && \
  508. case "$(iw dev $ifname info | grep "^\ttype" | cut -d' ' -f2- 2>/dev/null)" in
  509. "AP")
  510. [ "$type" = "__ap" ] && rc=0
  511. ;;
  512. "IBSS")
  513. [ "$type" = "adhoc" ] && rc=0
  514. ;;
  515. "managed")
  516. [ "$type" = "managed" ] && rc=0
  517. ;;
  518. "mesh point")
  519. [ "$type" = "mp" ] && rc=0
  520. ;;
  521. "monitor")
  522. [ "$type" = "monitor" ] && rc=0
  523. ;;
  524. esac
  525. }
  526. [ "$rc" = 233 ] && {
  527. iw dev "$ifname" del >/dev/null 2>&1
  528. [ "$?" = 0 ] && {
  529. sleep 1
  530. iw phy "$phy" interface add "$ifname" type "$type" $wdsflag >/dev/null 2>&1
  531. rc="$?"
  532. }
  533. }
  534. [ "$rc" != 0 ] && {
  535. # Device might not support virtual interfaces, so the interface never got deleted in the first place.
  536. # Check if the interface already exists, and avoid failing in this case.
  537. [ -d "/sys/class/ieee80211/${phy}/device/net/${ifname}" ] && rc=0
  538. }
  539. [ "$rc" != 0 ] && {
  540. # Device doesn't support virtual interfaces and may have existing interface other than ifname.
  541. oldifname="$(basename "/sys/class/ieee80211/${phy}/device/net"/* 2>/dev/null)"
  542. [ "$oldifname" ] && ip link set "$oldifname" name "$ifname" 1>/dev/null 2>&1
  543. rc="$?"
  544. }
  545. [ "$rc" != 0 ] && echo "Failed to create interface $ifname"
  546. return $rc
  547. }
  548. mac80211_prepare_vif() {
  549. json_select config
  550. json_get_vars ifname mode ssid wds powersave macaddr enable wpa_psk_file vlan_file
  551. [ -n "$ifname" ] || ifname="wlan${phy#phy}${if_idx:+-$if_idx}"
  552. if_idx=$((${if_idx:-0} + 1))
  553. set_default wds 0
  554. set_default powersave 0
  555. json_select ..
  556. [ -n "$macaddr" ] || {
  557. macaddr="$(mac80211_generate_mac $phy)"
  558. macidx="$(($macidx + 1))"
  559. }
  560. json_add_object data
  561. json_add_string ifname "$ifname"
  562. json_close_object
  563. [ "$mode" == "ap" ] && {
  564. [ -z "$wpa_psk_file" ] && hostapd_set_psk "$ifname"
  565. [ -z "$vlan_file" ] && hostapd_set_vlan "$ifname"
  566. }
  567. json_select config
  568. # It is far easier to delete and create the desired interface
  569. case "$mode" in
  570. adhoc)
  571. mac80211_iw_interface_add "$phy" "$ifname" adhoc || return
  572. ;;
  573. ap)
  574. # Hostapd will handle recreating the interface and
  575. # subsequent virtual APs belonging to the same PHY
  576. if [ -n "$hostapd_ctrl" ]; then
  577. type=bss
  578. else
  579. type=interface
  580. fi
  581. mac80211_hostapd_setup_bss "$phy" "$ifname" "$macaddr" "$type" || return
  582. NEWAPLIST="${NEWAPLIST}$ifname "
  583. [ -n "$hostapd_ctrl" ] || {
  584. ap_ifname="${ifname}"
  585. hostapd_ctrl="${hostapd_ctrl:-/var/run/hostapd/$ifname}"
  586. }
  587. ;;
  588. mesh)
  589. mac80211_iw_interface_add "$phy" "$ifname" mp || return
  590. ;;
  591. monitor)
  592. mac80211_iw_interface_add "$phy" "$ifname" monitor || return
  593. ;;
  594. sta)
  595. local wdsflag=
  596. [ "$enable" = 0 ] || staidx="$(($staidx + 1))"
  597. [ "$wds" -gt 0 ] && wdsflag="4addr on"
  598. mac80211_iw_interface_add "$phy" "$ifname" managed "$wdsflag" || return
  599. if [ "$wds" -gt 0 ]; then
  600. iw "$ifname" set 4addr on
  601. else
  602. iw "$ifname" set 4addr off
  603. fi
  604. [ "$powersave" -gt 0 ] && powersave="on" || powersave="off"
  605. iw "$ifname" set power_save "$powersave"
  606. ;;
  607. esac
  608. case "$mode" in
  609. monitor|mesh)
  610. [ "$auto_channel" -gt 0 ] || iw dev "$ifname" set channel "$channel" $iw_htmode
  611. ;;
  612. esac
  613. if [ "$mode" != "ap" ]; then
  614. # ALL ap functionality will be passed to hostapd
  615. # All interfaces must have unique mac addresses
  616. # which can either be explicitly set in the device
  617. # section, or automatically generated
  618. ip link set dev "$ifname" address "$macaddr"
  619. fi
  620. json_select ..
  621. }
  622. mac80211_setup_supplicant() {
  623. local enable=$1
  624. local add_sp=0
  625. local spobj="$(ubus -S list | grep wpa_supplicant.${ifname})"
  626. [ "$enable" = 0 ] && {
  627. ubus call wpa_supplicant.${phy} config_remove "{\"iface\":\"$ifname\"}"
  628. ip link set dev "$ifname" down
  629. iw dev "$ifname" del
  630. return 0
  631. }
  632. wpa_supplicant_prepare_interface "$ifname" nl80211 || {
  633. iw dev "$ifname" del
  634. return 1
  635. }
  636. if [ "$mode" = "sta" ]; then
  637. wpa_supplicant_add_network "$ifname"
  638. else
  639. wpa_supplicant_add_network "$ifname" "$freq" "$htmode" "$noscan"
  640. fi
  641. NEWSPLIST="${NEWSPLIST}$ifname "
  642. if [ "${NEWAPLIST%% *}" != "${OLDAPLIST%% *}" ]; then
  643. [ "$spobj" ] && ubus call wpa_supplicant config_remove "{\"iface\":\"$ifname\"}"
  644. add_sp=1
  645. fi
  646. [ -z "$spobj" ] && add_sp=1
  647. NEW_MD5_SP=$(test -e "${_config}" && md5sum ${_config})
  648. OLD_MD5_SP=$(uci -q -P /var/state get wireless._${phy}.md5_${ifname})
  649. if [ "$add_sp" = "1" ]; then
  650. wpa_supplicant_run "$ifname" "$hostapd_ctrl"
  651. else
  652. [ "${NEW_MD5_SP}" == "${OLD_MD5_SP}" ] || ubus call $spobj reload
  653. fi
  654. uci -q -P /var/state set wireless._${phy}.md5_${ifname}="${NEW_MD5_SP}"
  655. return 0
  656. }
  657. mac80211_setup_supplicant_noctl() {
  658. local enable=$1
  659. local spobj="$(ubus -S list | grep wpa_supplicant.${ifname})"
  660. wpa_supplicant_prepare_interface "$ifname" nl80211 || {
  661. iw dev "$ifname" del
  662. return 1
  663. }
  664. wpa_supplicant_add_network "$ifname" "$freq" "$htmode" "$noscan"
  665. NEWSPLIST="${NEWSPLIST}$ifname "
  666. [ "$enable" = 0 ] && {
  667. ubus call wpa_supplicant config_remove "{\"iface\":\"$ifname\"}"
  668. ip link set dev "$ifname" down
  669. return 0
  670. }
  671. if [ -z "$spobj" ]; then
  672. wpa_supplicant_run "$ifname"
  673. else
  674. ubus call $spobj reload
  675. fi
  676. }
  677. mac80211_prepare_iw_htmode() {
  678. case "$htmode" in
  679. VHT20|HT20) iw_htmode=HT20;;
  680. HT40*|VHT40|VHT160)
  681. case "$band" in
  682. 2g)
  683. case "$htmode" in
  684. HT40+) iw_htmode="HT40+";;
  685. HT40-) iw_htmode="HT40-";;
  686. *)
  687. if [ "$channel" -lt 7 ]; then
  688. iw_htmode="HT40+"
  689. else
  690. iw_htmode="HT40-"
  691. fi
  692. ;;
  693. esac
  694. ;;
  695. *)
  696. case "$(( ($channel / 4) % 2 ))" in
  697. 1) iw_htmode="HT40+" ;;
  698. 0) iw_htmode="HT40-";;
  699. esac
  700. ;;
  701. esac
  702. [ "$auto_channel" -gt 0 ] && iw_htmode="HT40+"
  703. ;;
  704. VHT80)
  705. iw_htmode="80MHZ"
  706. ;;
  707. NONE|NOHT)
  708. iw_htmode="NOHT"
  709. ;;
  710. *) iw_htmode="" ;;
  711. esac
  712. }
  713. mac80211_setup_adhoc() {
  714. local enable=$1
  715. json_get_vars bssid ssid key mcast_rate
  716. NEWUMLIST="${NEWUMLIST}$ifname "
  717. [ "$enable" = 0 ] && {
  718. ip link set dev "$ifname" down
  719. return 0
  720. }
  721. keyspec=
  722. [ "$auth_type" = "wep" ] && {
  723. set_default key 1
  724. case "$key" in
  725. [1234])
  726. local idx
  727. for idx in 1 2 3 4; do
  728. json_get_var ikey "key$idx"
  729. [ -n "$ikey" ] && {
  730. ikey="$(($idx - 1)):$(prepare_key_wep "$ikey")"
  731. [ $idx -eq $key ] && ikey="d:$ikey"
  732. append keyspec "$ikey"
  733. }
  734. done
  735. ;;
  736. *)
  737. append keyspec "d:0:$(prepare_key_wep "$key")"
  738. ;;
  739. esac
  740. }
  741. brstr=
  742. for br in $basic_rate_list; do
  743. wpa_supplicant_add_rate brstr "$br"
  744. done
  745. mcval=
  746. [ -n "$mcast_rate" ] && wpa_supplicant_add_rate mcval "$mcast_rate"
  747. iw dev "$ifname" set type ibss
  748. iw dev "$ifname" ibss join "$ssid" $freq $iw_htmode fixed-freq $bssid \
  749. beacon-interval $beacon_int \
  750. ${brstr:+basic-rates $brstr} \
  751. ${mcval:+mcast-rate $mcval} \
  752. ${keyspec:+keys $keyspec}
  753. }
  754. mac80211_setup_mesh() {
  755. local enable=$1
  756. json_get_vars ssid mesh_id mcast_rate
  757. NEWUMLIST="${NEWUMLIST}$ifname "
  758. [ "$enable" = 0 ] && {
  759. ip link set dev "$ifname" down
  760. return 0
  761. }
  762. mcval=
  763. [ -n "$mcast_rate" ] && wpa_supplicant_add_rate mcval "$mcast_rate"
  764. [ -n "$mesh_id" ] && ssid="$mesh_id"
  765. iw dev "$ifname" mesh join "$ssid" freq $freq $iw_htmode \
  766. ${mcval:+mcast-rate $mcval} \
  767. beacon-interval $beacon_int
  768. }
  769. mac80211_setup_vif() {
  770. local name="$1"
  771. local failed
  772. local action=up
  773. json_select data
  774. json_get_vars ifname
  775. json_select ..
  776. json_select config
  777. json_get_vars mode
  778. json_get_var vif_txpower
  779. json_get_var vif_enable enable 1
  780. [ "$vif_enable" = 1 ] || action=down
  781. if [ "$mode" != "ap" ] || [ "$ifname" = "$ap_ifname" ]; then
  782. ip link set dev "$ifname" "$action" || {
  783. wireless_setup_vif_failed IFUP_ERROR
  784. json_select ..
  785. return
  786. }
  787. [ -z "$vif_txpower" ] || iw dev "$ifname" set txpower fixed "${vif_txpower%%.*}00"
  788. fi
  789. case "$mode" in
  790. mesh)
  791. wireless_vif_parse_encryption
  792. [ -z "$htmode" ] && htmode="NOHT";
  793. if [ "$wpa" -gt 0 -o "$auto_channel" -gt 0 ] || chan_is_dfs "$phy" "$channel"; then
  794. mac80211_setup_supplicant $vif_enable || failed=1
  795. else
  796. mac80211_setup_mesh $vif_enable
  797. fi
  798. for var in $MP_CONFIG_INT $MP_CONFIG_BOOL $MP_CONFIG_STRING; do
  799. json_get_var mp_val "$var"
  800. [ -n "$mp_val" ] && iw dev "$ifname" set mesh_param "$var" "$mp_val"
  801. done
  802. ;;
  803. adhoc)
  804. wireless_vif_parse_encryption
  805. if [ "$wpa" -gt 0 -o "$auto_channel" -gt 0 ]; then
  806. mac80211_setup_supplicant_noctl $vif_enable || failed=1
  807. else
  808. mac80211_setup_adhoc $vif_enable
  809. fi
  810. ;;
  811. sta)
  812. mac80211_setup_supplicant $vif_enable || failed=1
  813. ;;
  814. esac
  815. json_select ..
  816. [ -n "$failed" ] || wireless_add_vif "$name" "$ifname"
  817. }
  818. get_freq() {
  819. local phy="$1"
  820. local channel="$2"
  821. local band="$3"
  822. case "$band" in
  823. 2g) band="1:";;
  824. 5g) band="2:";;
  825. 60g) band="3:";;
  826. 6g) band="4:";;
  827. esac
  828. iw "$phy" info | awk -v band="$band" -v channel="[$channel]" '
  829. $1 ~ /Band/ {
  830. band_match = band == $2
  831. }
  832. band_match && $3 == "MHz" && $4 == channel {
  833. print $2
  834. exit
  835. }
  836. '
  837. }
  838. chan_is_dfs() {
  839. local phy="$1"
  840. local chan="$2"
  841. iw "$phy" info | grep -E -m1 "(\* ${chan:-....} MHz${chan:+|\\[$chan\\]})" | grep -q "MHz.*radar detection"
  842. return $!
  843. }
  844. mac80211_vap_cleanup() {
  845. local service="$1"
  846. local vaps="$2"
  847. for wdev in $vaps; do
  848. [ "$service" != "none" ] && ubus call ${service} config_remove "{\"iface\":\"$wdev\"}"
  849. ip link set dev "$wdev" down 2>/dev/null
  850. iw dev "$wdev" del
  851. done
  852. }
  853. mac80211_interface_cleanup() {
  854. local phy="$1"
  855. local primary_ap=$(uci -q -P /var/state get wireless._${phy}.aplist)
  856. primary_ap=${primary_ap%% *}
  857. mac80211_vap_cleanup hostapd "${primary_ap}"
  858. mac80211_vap_cleanup wpa_supplicant "$(uci -q -P /var/state get wireless._${phy}.splist)"
  859. mac80211_vap_cleanup none "$(uci -q -P /var/state get wireless._${phy}.umlist)"
  860. }
  861. mac80211_set_noscan() {
  862. hostapd_noscan=1
  863. }
  864. drv_mac80211_cleanup() {
  865. hostapd_common_cleanup
  866. }
  867. drv_mac80211_setup() {
  868. json_select config
  869. json_get_vars \
  870. phy macaddr path \
  871. country chanbw distance \
  872. txpower antenna_gain \
  873. rxantenna txantenna \
  874. frag rts beacon_int:100 htmode
  875. json_get_values basic_rate_list basic_rate
  876. json_get_values scan_list scan_list
  877. json_select ..
  878. find_phy || {
  879. echo "Could not find PHY for device '$1'"
  880. wireless_set_retry 0
  881. return 1
  882. }
  883. wireless_set_data phy="$phy"
  884. [ -z "$(uci -q -P /var/state show wireless._${phy})" ] && uci -q -P /var/state set wireless._${phy}=phy
  885. OLDAPLIST=$(uci -q -P /var/state get wireless._${phy}.aplist)
  886. OLDSPLIST=$(uci -q -P /var/state get wireless._${phy}.splist)
  887. OLDUMLIST=$(uci -q -P /var/state get wireless._${phy}.umlist)
  888. local wdev
  889. local cwdev
  890. local found
  891. for wdev in $(list_phy_interfaces "$phy"); do
  892. found=0
  893. for cwdev in $OLDAPLIST $OLDSPLIST $OLDUMLIST; do
  894. if [ "$wdev" = "$cwdev" ]; then
  895. found=1
  896. break
  897. fi
  898. done
  899. if [ "$found" = "0" ]; then
  900. ip link set dev "$wdev" down
  901. iw dev "$wdev" del
  902. fi
  903. done
  904. # convert channel to frequency
  905. [ "$auto_channel" -gt 0 ] || freq="$(get_freq "$phy" "$channel" "$band")"
  906. [ -n "$country" ] && {
  907. iw reg get | grep -q "^country $country:" || {
  908. iw reg set "$country"
  909. sleep 1
  910. }
  911. }
  912. hostapd_conf_file="/var/run/hostapd-$phy.conf"
  913. no_ap=1
  914. macidx=0
  915. staidx=0
  916. [ -n "$chanbw" ] && {
  917. for file in /sys/kernel/debug/ieee80211/$phy/ath9k*/chanbw /sys/kernel/debug/ieee80211/$phy/ath5k/bwmode; do
  918. [ -f "$file" ] && echo "$chanbw" > "$file"
  919. done
  920. }
  921. set_default rxantenna 0xffffffff
  922. set_default txantenna 0xffffffff
  923. set_default distance 0
  924. set_default antenna_gain 0
  925. [ "$txantenna" = "all" ] && txantenna=0xffffffff
  926. [ "$rxantenna" = "all" ] && rxantenna=0xffffffff
  927. iw phy "$phy" set antenna $txantenna $rxantenna >/dev/null 2>&1
  928. iw phy "$phy" set antenna_gain $antenna_gain >/dev/null 2>&1
  929. iw phy "$phy" set distance "$distance" >/dev/null 2>&1
  930. if [ -n "$txpower" ]; then
  931. iw phy "$phy" set txpower fixed "${txpower%%.*}00"
  932. else
  933. iw phy "$phy" set txpower auto
  934. fi
  935. [ -n "$frag" ] && iw phy "$phy" set frag "${frag%%.*}"
  936. [ -n "$rts" ] && iw phy "$phy" set rts "${rts%%.*}"
  937. has_ap=
  938. hostapd_ctrl=
  939. ap_ifname=
  940. hostapd_noscan=
  941. for_each_interface "ap" mac80211_check_ap
  942. rm -f "$hostapd_conf_file"
  943. for_each_interface "sta adhoc mesh" mac80211_set_noscan
  944. [ -n "$has_ap" ] && mac80211_hostapd_setup_base "$phy"
  945. mac80211_prepare_iw_htmode
  946. for_each_interface "sta adhoc mesh monitor" mac80211_prepare_vif
  947. NEWAPLIST=
  948. for_each_interface "ap" mac80211_prepare_vif
  949. NEW_MD5=$(test -e "${hostapd_conf_file}" && md5sum ${hostapd_conf_file})
  950. OLD_MD5=$(uci -q -P /var/state get wireless._${phy}.md5)
  951. if [ "${NEWAPLIST}" != "${OLDAPLIST}" ]; then
  952. mac80211_vap_cleanup hostapd "${OLDAPLIST}"
  953. fi
  954. [ -n "${NEWAPLIST}" ] && mac80211_iw_interface_add "$phy" "${NEWAPLIST%% *}" __ap
  955. local add_ap=0
  956. local primary_ap=${NEWAPLIST%% *}
  957. [ -n "$hostapd_ctrl" ] && {
  958. local no_reload=1
  959. if [ -n "$(ubus list | grep hostapd.$primary_ap)" ]; then
  960. no_reload=0
  961. [ "${NEW_MD5}" = "${OLD_MD5}" ] || {
  962. ubus call hostapd.$primary_ap reload
  963. no_reload=$?
  964. if [ "$no_reload" != "0" ]; then
  965. mac80211_vap_cleanup hostapd "${OLDAPLIST}"
  966. mac80211_vap_cleanup wpa_supplicant "$(uci -q -P /var/state get wireless._${phy}.splist)"
  967. mac80211_vap_cleanup none "$(uci -q -P /var/state get wireless._${phy}.umlist)"
  968. sleep 2
  969. mac80211_iw_interface_add "$phy" "${NEWAPLIST%% *}" __ap
  970. for_each_interface "sta adhoc mesh monitor" mac80211_prepare_vif
  971. fi
  972. }
  973. fi
  974. if [ "$no_reload" != "0" ]; then
  975. add_ap=1
  976. ubus wait_for hostapd
  977. local hostapd_res="$(ubus call hostapd config_add "{\"iface\":\"$primary_ap\", \"config\":\"${hostapd_conf_file}\"}")"
  978. ret="$?"
  979. [ "$ret" != 0 -o -z "$hostapd_res" ] && {
  980. wireless_setup_failed HOSTAPD_START_FAILED
  981. return
  982. }
  983. wireless_add_process "$(jsonfilter -s "$hostapd_res" -l 1 -e @.pid)" "/usr/sbin/hostapd" 1 1
  984. fi
  985. }
  986. uci -q -P /var/state set wireless._${phy}.aplist="${NEWAPLIST}"
  987. uci -q -P /var/state set wireless._${phy}.md5="${NEW_MD5}"
  988. [ "${add_ap}" = 1 ] && sleep 1
  989. for_each_interface "ap" mac80211_setup_vif
  990. NEWSPLIST=
  991. NEWUMLIST=
  992. for_each_interface "sta adhoc mesh monitor" mac80211_setup_vif
  993. uci -q -P /var/state set wireless._${phy}.splist="${NEWSPLIST}"
  994. uci -q -P /var/state set wireless._${phy}.umlist="${NEWUMLIST}"
  995. local foundvap
  996. local dropvap=""
  997. for oldvap in $OLDSPLIST; do
  998. foundvap=0
  999. for newvap in $NEWSPLIST; do
  1000. [ "$oldvap" = "$newvap" ] && foundvap=1
  1001. done
  1002. [ "$foundvap" = "0" ] && dropvap="$dropvap $oldvap"
  1003. done
  1004. [ -n "$dropvap" ] && mac80211_vap_cleanup wpa_supplicant "$dropvap"
  1005. wireless_set_up
  1006. }
  1007. _list_phy_interfaces() {
  1008. local phy="$1"
  1009. if [ -d "/sys/class/ieee80211/${phy}/device/net" ]; then
  1010. ls "/sys/class/ieee80211/${phy}/device/net" 2>/dev/null;
  1011. else
  1012. ls "/sys/class/ieee80211/${phy}/device" 2>/dev/null | grep net: | sed -e 's,net:,,g'
  1013. fi
  1014. }
  1015. list_phy_interfaces() {
  1016. local phy="$1"
  1017. for dev in $(_list_phy_interfaces "$phy"); do
  1018. readlink "/sys/class/net/${dev}/phy80211" | grep -q "/${phy}\$" || continue
  1019. echo "$dev"
  1020. done
  1021. }
  1022. drv_mac80211_teardown() {
  1023. json_select data
  1024. json_get_vars phy
  1025. json_select ..
  1026. [ -n "$phy" ] || {
  1027. echo "Bug: PHY is undefined for device '$1'"
  1028. return 1
  1029. }
  1030. mac80211_interface_cleanup "$phy"
  1031. uci -q -P /var/state revert wireless._${phy}
  1032. }
  1033. add_driver mac80211