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