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