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