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