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