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