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