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