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