mac80211.sh 34 KB

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