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