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