mac80211: try to get interface addresses from wiphy sysfs 'addresses' if no mask...
[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
5 init_wireless_driver "$@"
6
7 MP_CONFIG_INT="mesh_retry_timeout mesh_confirm_timeout mesh_holding_timeout mesh_max_peer_links
8                mesh_max_retries mesh_ttl mesh_element_ttl mesh_hwmp_max_preq_retries
9                mesh_path_refresh_time mesh_min_discovery_timeout mesh_hwmp_active_path_timeout
10                mesh_hwmp_preq_min_interval mesh_hwmp_net_diameter_traversal_time mesh_hwmp_rootmode
11                mesh_hwmp_rann_interval mesh_gate_announcements mesh_sync_offset_max_neighor
12                mesh_rssi_threshold mesh_hwmp_active_path_to_root_timeout mesh_hwmp_root_interval
13                mesh_hwmp_confirmation_interval mesh_awake_window mesh_plink_timeout"
14 MP_CONFIG_BOOL="mesh_auto_open_plinks mesh_fwding"
15 MP_CONFIG_STRING="mesh_power_mode"
16
17 drv_mac80211_init_device_config() {
18         hostapd_common_add_device_config
19
20         config_add_string path phy 'macaddr:macaddr'
21         config_add_string hwmode
22         config_add_int beacon_int chanbw frag rts
23         config_add_int rxantenna txantenna antenna_gain txpower distance
24         config_add_boolean noscan
25         config_add_array ht_capab
26         config_add_boolean \
27                 rxldpc \
28                 short_gi_80 \
29                 short_gi_160 \
30                 tx_stbc_2by1 \
31                 su_beamformer \
32                 su_beamformee \
33                 mu_beamformer \
34                 mu_beamformee \
35                 vht_txop_ps \
36                 htc_vht \
37                 rx_antenna_pattern \
38                 tx_antenna_pattern
39         config_add_int vht_max_a_mpdu_len_exp vht_max_mpdu vht_link_adapt vht160 rx_stbc tx_stbc
40         config_add_boolean \
41                 ldpc \
42                 greenfield \
43                 short_gi_20 \
44                 short_gi_40 \
45                 dsss_cck_40
46 }
47
48 drv_mac80211_init_iface_config() {
49         hostapd_common_add_bss_config
50
51         config_add_string 'macaddr:macaddr' ifname
52
53         config_add_boolean wds powersave
54         config_add_int maxassoc
55         config_add_int max_listen_int
56         config_add_int dtim_period
57
58         # mesh
59         config_add_string mesh_id
60         config_add_int $MP_CONFIG_INT
61         config_add_boolean $MP_CONFIG_BOOL
62         config_add_string $MP_CONFIG_STRING
63 }
64
65 mac80211_add_capabilities() {
66         local __var="$1"; shift
67         local __mask="$1"; shift
68         local __out= oifs
69
70         oifs="$IFS"
71         IFS=:
72         for capab in "$@"; do
73                 set -- $capab
74
75                 [ "$(($4))" -gt 0 ] || continue
76                 [ "$(($__mask & $2))" -eq "$((${3:-$2}))" ] || continue
77                 __out="$__out[$1]"
78         done
79         IFS="$oifs"
80
81         export -n -- "$__var=$__out"
82 }
83
84 mac80211_hostapd_setup_base() {
85         local phy="$1"
86
87         json_select config
88
89         [ "$auto_channel" -gt 0 ] && channel=acs_survey
90
91         json_get_vars noscan htmode
92         json_get_values ht_capab_list ht_capab
93
94         ieee80211n=1
95         ht_capab=
96         case "$htmode" in
97                 VHT20|HT20) ;;
98                 HT40*|VHT40|VHT80|VHT160)
99                         case "$hwmode" in
100                                 a)
101                                         case "$(( ($channel / 4) % 2 ))" in
102                                                 1) ht_capab="[HT40+]";;
103                                                 0) ht_capab="[HT40-]";;
104                                         esac
105                                 ;;
106                                 *)
107                                         case "$htmode" in
108                                                 HT40+) ht_capab="[HT40+]";;
109                                                 HT40-) ht_capab="[HT40-]";;
110                                                 *)
111                                                         if [ "$channel" -lt 7 ]; then
112                                                                 ht_capab="[HT40+]"
113                                                         else
114                                                                 ht_capab="[HT40-]"
115                                                         fi
116                                                 ;;
117                                         esac
118                                 ;;
119                         esac
120                         [ "$auto_channel" -gt 0 ] && ht_capab="[HT40+]"
121                 ;;
122                 *) ieee80211n= ;;
123         esac
124
125         [ -n "$ieee80211n" ] && {
126                 append base_cfg "ieee80211n=1" "$N"
127
128                 json_get_vars \
129                         ldpc:1 \
130                         greenfield:0 \
131                         short_gi_20:1 \
132                         short_gi_40:1 \
133                         tx_stbc:1 \
134                         rx_stbc:3 \
135                         dsss_cck_40:1
136
137                 ht_cap_mask=0
138                 for cap in $(iw phy "$phy" info | grep 'Capabilities:' | cut -d: -f2); do
139                         ht_cap_mask="$(($ht_cap_mask | $cap))"
140                 done
141
142                 cap_rx_stbc=$((($ht_cap_mask >> 8) & 3))
143                 [ "$rx_stbc" -lt "$cap_rx_stbc" ] && cap_rx_stbc="$rx_stbc"
144                 ht_cap_mask="$(( ($ht_cap_mask & ~(0x300)) | ($cap_rx_stbc << 8) ))"
145
146                 mac80211_add_capabilities ht_capab_flags $ht_cap_mask \
147                         LDPC:0x1::$ldpc \
148                         GF:0x10::$greenfield \
149                         SHORT-GI-20:0x20::$short_gi_20 \
150                         SHORT-GI-40:0x40::$short_gi_40 \
151                         TX-STBC:0x80::$tx_stbc \
152                         RX-STBC1:0x300:0x100:1 \
153                         RX-STBC12:0x300:0x200:1 \
154                         RX-STBC123:0x300:0x300:1 \
155                         DSSS_CCK-40:0x1000::$dsss_cck_40
156
157                 ht_capab="$ht_capab$ht_capab_flags"
158                 [ -n "$ht_capab" ] && append base_cfg "ht_capab=$ht_capab" "$N"
159         }
160
161         # 802.11ac
162         enable_ac=0
163         idx="$channel"
164         case "$htmode" in
165                 VHT20) enable_ac=1;;
166                 VHT40)
167                         case "$(( ($channel / 4) % 2 ))" in
168                                 1) idx=$(($channel + 2));;
169                                 0) idx=$(($channel - 2));;
170                         esac
171                         enable_ac=1
172                         append base_cfg "vht_oper_chwidth=0" "$N"
173                         append base_cfg "vht_oper_centr_freq_seg0_idx=$idx" "$N"
174                 ;;
175                 VHT80)
176                         case "$(( ($channel / 4) % 4 ))" in
177                                 1) idx=$(($channel + 6));;
178                                 2) idx=$(($channel + 2));;
179                                 3) idx=$(($channel - 2));;
180                                 0) idx=$(($channel - 6));;
181                         esac
182                         enable_ac=1
183                         append base_cfg "vht_oper_chwidth=1" "$N"
184                         append base_cfg "vht_oper_centr_freq_seg0_idx=$idx" "$N"
185                 ;;
186                 VHT160)
187                         case "$channel" in
188                                 36|40|44|48|52|56|60|64) idx=50;;
189                                 100|104|108|112|116|120|124|128) idx=114;;
190                         esac
191                         enable_ac=1
192                         append base_cfg "vht_oper_chwidth=2" "$N"
193                         append base_cfg "vht_oper_centr_freq_seg0_idx=$idx" "$N"
194                 ;;
195         esac
196
197         if [ "$enable_ac" != "0" ]; then
198                 json_get_vars \
199                         rxldpc:1 \
200                         short_gi_80:1 \
201                         short_gi_160:1 \
202                         tx_stbc_2by1:1 \
203                         su_beamformer:1 \
204                         su_beamformee:1 \
205                         mu_beamformer:1 \
206                         mu_beamformee:1 \
207                         vht_txop_ps:1 \
208                         htc_vht:1 \
209                         rx_antenna_pattern:1 \
210                         tx_antenna_pattern:1 \
211                         vht_max_a_mpdu_len_exp:7 \
212                         vht_max_mpdu:11454 \
213                         rx_stbc:4 \
214                         tx_stbc:4 \
215                         vht_link_adapt:3 \
216                         vht160:2
217
218                 append base_cfg "ieee80211ac=1" "$N"
219                 vht_cap=0
220                 for cap in $(iw phy "$phy" info | awk -F "[()]" '/VHT Capabilities/ { print $2 }'); do
221                         vht_cap="$(($vht_cap | $cap))"
222                 done
223
224                 cap_rx_stbc=$((($vht_cap >> 8) & 7))
225                 [ "$rx_stbc" -lt "$cap_rx_stbc" ] && cap_rx_stbc="$rx_stbc"
226                 ht_cap_mask="$(( ($vht_cap & ~(0x700)) | ($cap_rx_stbc << 8) ))"
227
228                 mac80211_add_capabilities vht_capab $vht_cap \
229                         RXLDPC:0x10::$rxldpc \
230                         SHORT-GI-80:0x20::$short_gi_80 \
231                         SHORT-GI-160:0x40::$short_gi_160 \
232                         TX-STBC-2BY1:0x80::$tx_stbc \
233                         SU-BEAMFORMER:0x800::$su_beamformer \
234                         SU-BEAMFORMEE:0x1000::$su_beamformee \
235                         MU-BEAMFORMER:0x80000::$mu_beamformer \
236                         MU-BEAMFORMEE:0x100000::$mu_beamformee \
237                         VHT-TXOP-PS:0x200000::$vht_txop_ps \
238                         HTC-VHT:0x400000::$htc_vht \
239                         RX-ANTENNA-PATTERN:0x10000000::$rx_antenna_pattern \
240                         TX-ANTENNA-PATTERN:0x20000000::$tx_antenna_pattern \
241                         RX-STBC1:0x700:0x100:1 \
242                         RX-STBC12:0x700:0x200:1 \
243                         RX-STBC123:0x700:0x300:1 \
244                         RX-STBC1234:0x700:0x400:1 \
245
246                 # supported Channel widths
247                 vht160_hw=0
248                 [ "$(($vht_cap & 12))" -eq 4 -a 1 -le "$vht160" ] && \
249                         vht160_hw=1
250                 [ "$(($vht_cap & 12))" -eq 8 -a 2 -le "$vht160" ] && \
251                         vht160_hw=2
252                 [ "$vht160_hw" = 1 ] && vht_capab="$vht_capab[VHT160]"
253                 [ "$vht160_hw" = 2 ] && vht_capab="$vht_capab[VHT160-80PLUS80]"
254
255                 # maximum MPDU length
256                 vht_max_mpdu_hw=3895
257                 [ "$(($vht_cap & 3))" -ge 1 -a 7991 -le "$vht_max_mpdu" ] && \
258                         vht_max_mpdu_hw=7991
259                 [ "$(($vht_cap & 3))" -ge 2 -a 11454 -le "$vht_max_mpdu" ] && \
260                         vht_max_mpdu_hw=11454
261                 [ "$vht_max_mpdu_hw" != 3895 ] && \
262                         vht_capab="$vht_capab[MAX-MPDU-$vht_max_mpdu_hw]"
263                         
264                 # maximum A-MPDU length exponent
265                 vht_max_a_mpdu_len_exp_hw=0
266                 [ "$(($vht_cap & 58720256))" -ge 8388608 -a 1 -le "$vht_max_a_mpdu_len_exp" ] && \
267                         vht_max_a_mpdu_len_exp_hw=1
268                 [ "$(($vht_cap & 58720256))" -ge 16777216 -a 2 -le "$vht_max_a_mpdu_len_exp" ] && \
269                         vht_max_a_mpdu_len_exp_hw=2
270                 [ "$(($vht_cap & 58720256))" -ge 25165824 -a 3 -le "$vht_max_a_mpdu_len_exp" ] && \
271                         vht_max_a_mpdu_len_exp_hw=3
272                 [ "$(($vht_cap & 58720256))" -ge 33554432 -a 4 -le "$vht_max_a_mpdu_len_exp" ] && \
273                         vht_max_a_mpdu_len_exp_hw=4
274                 [ "$(($vht_cap & 58720256))" -ge 41943040 -a 5 -le "$vht_max_a_mpdu_len_exp" ] && \
275                         vht_max_a_mpdu_len_exp_hw=5
276                 [ "$(($vht_cap & 58720256))" -ge 50331648 -a 6 -le "$vht_max_a_mpdu_len_exp" ] && \
277                         vht_max_a_mpdu_len_exp_hw=6
278                 [ "$(($vht_cap & 58720256))" -ge 58720256 -a 7 -le "$vht_max_a_mpdu_len_exp" ] && \
279                         vht_max_a_mpdu_len_exp_hw=7
280                 vht_capab="$vht_capab[MAX-A-MPDU-LEN-EXP$vht_max_a_mpdu_len_exp_hw]"
281
282                 # whether or not the STA supports link adaptation using VHT variant
283                 vht_link_adapt_hw=0
284                 [ "$(($vht_cap & 201326592))" -ge 134217728 -a 2 -le "$vht_link_adapt" ] && \
285                         vht_link_adapt_hw=2
286                 [ "$(($vht_cap & 201326592))" -ge 201326592 -a 3 -le "$vht_link_adapt" ] && \
287                         vht_link_adapt_hw=3
288                 [ "$vht_link_adapt_hw" != 0 ] && \
289                         vht_capab="$vht_capab[VHT-LINK-ADAPT-$vht_link_adapt_hw]"
290
291                 [ -n "$vht_capab" ] && append base_cfg "vht_capab=$vht_capab" "$N"
292         fi
293
294         hostapd_prepare_device_config "$hostapd_conf_file" nl80211
295         cat >> "$hostapd_conf_file" <<EOF
296 ${channel:+channel=$channel}
297 ${noscan:+noscan=$noscan}
298 $base_cfg
299
300 EOF
301         json_select ..
302 }
303
304 mac80211_hostapd_setup_bss() {
305         local phy="$1"
306         local ifname="$2"
307         local macaddr="$3"
308         local type="$4"
309
310         hostapd_cfg=
311         append hostapd_cfg "$type=$ifname" "$N"
312
313         hostapd_set_bss_options hostapd_cfg "$vif" || return 1
314         json_get_vars wds dtim_period max_listen_int
315
316         set_default wds 0
317
318         [ "$wds" -gt 0 ] && append hostapd_cfg "wds_sta=1" "$N"
319         [ "$staidx" -gt 0 ] && append hostapd_cfg "start_disabled=1" "$N"
320
321         cat >> /var/run/hostapd-$phy.conf <<EOF
322 $hostapd_cfg
323 bssid=$macaddr
324 ${dtim_period:+dtim_period=$dtim_period}
325 ${max_listen_int:+max_listen_interval=$max_listen_int}
326 EOF
327 }
328
329 mac80211_get_addr() {
330         local phy="$1"
331         local idx="$(($2 + 1))"
332
333         head -n $(($macidx + 1)) /sys/class/ieee80211/${phy}/addresses | tail -n1
334 }
335
336 mac80211_generate_mac() {
337         local phy="$1"
338         local id="${macidx:-0}"
339
340         local ref="$(cat /sys/class/ieee80211/${phy}/macaddress)"
341         local mask="$(cat /sys/class/ieee80211/${phy}/address_mask)"
342
343         [ "$mask" = "00:00:00:00:00:00" ] && {
344                 mask="ff:ff:ff:ff:ff:ff";
345
346                 [ "$(wc -l < /sys/class/ieee80211/${phy}/addresses)" -gt 1 ] && {
347                         addr="$(mac80211_get_addr "$phy" "$id")"
348                         [ -n "$addr" ] && {
349                                 echo "$addr"
350                                 return
351                         }
352                 }
353         }
354
355         local oIFS="$IFS"; IFS=":"; set -- $mask; IFS="$oIFS"
356
357         local mask1=$1
358         local mask6=$6
359
360         local oIFS="$IFS"; IFS=":"; set -- $ref; IFS="$oIFS"
361
362         macidx=$(($id + 1))
363         [ "$((0x$mask1))" -gt 0 ] && {
364                 b1="0x$1"
365                 [ "$id" -gt 0 ] && \
366                         b1=$(($b1 ^ ((($id - 1) << 2) | 0x2)))
367                 printf "%02x:%s:%s:%s:%s:%s" $b1 $2 $3 $4 $5 $6
368                 return
369         }
370
371         [ "$((0x$mask6))" -lt 255 ] && {
372                 printf "%s:%s:%s:%s:%s:%02x" $1 $2 $3 $4 $5 $(( 0x$6 ^ $id ))
373                 return
374         }
375
376         off2=$(( (0x$6 + $id) / 0x100 ))
377         printf "%s:%s:%s:%s:%02x:%02x" \
378                 $1 $2 $3 $4 \
379                 $(( (0x$5 + $off2) % 0x100 )) \
380                 $(( (0x$6 + $id) % 0x100 ))
381 }
382
383 find_phy() {
384         [ -n "$phy" -a -d /sys/class/ieee80211/$phy ] && return 0
385         [ -n "$path" ] && {
386                 for phy in /sys/devices/$path/ieee80211/phy*; do
387                         [ -e "$phy" ] && {
388                                 phy="${phy##*/}"
389                                 return 0
390                         }
391                 done
392         }
393         [ -n "$macaddr" ] && {
394                 for phy in $(ls /sys/class/ieee80211 2>/dev/null); do
395                         grep -i -q "$macaddr" "/sys/class/ieee80211/${phy}/macaddress" && return 0
396                 done
397         }
398         return 1
399 }
400
401 mac80211_check_ap() {
402         has_ap=1
403 }
404
405 mac80211_prepare_vif() {
406         json_select config
407
408         json_get_vars ifname mode ssid wds powersave macaddr
409
410         [ -n "$ifname" ] || ifname="wlan${phy#phy}${if_idx:+-$if_idx}"
411         if_idx=$((${if_idx:-0} + 1))
412
413         set_default wds 0
414         set_default powersave 0
415
416         json_select ..
417
418         [ -n "$macaddr" ] || {
419                 macaddr="$(mac80211_generate_mac $phy)"
420                 macidx="$(($macidx + 1))"
421         }
422
423         json_add_object data
424         json_add_string ifname "$ifname"
425         json_close_object
426         json_select config
427
428         # It is far easier to delete and create the desired interface
429         case "$mode" in
430                 adhoc)
431                         iw phy "$phy" interface add "$ifname" type adhoc
432                 ;;
433                 ap)
434                         # Hostapd will handle recreating the interface and
435                         # subsequent virtual APs belonging to the same PHY
436                         if [ -n "$hostapd_ctrl" ]; then
437                                 type=bss
438                         else
439                                 type=interface
440                         fi
441
442                         mac80211_hostapd_setup_bss "$phy" "$ifname" "$macaddr" "$type" || return
443
444                         [ -n "$hostapd_ctrl" ] || {
445                                 iw phy "$phy" interface add "$ifname" type managed
446                                 hostapd_ctrl="${hostapd_ctrl:-/var/run/hostapd/$ifname}"
447                         }
448                 ;;
449                 mesh)
450                         json_get_vars key mesh_id
451                         if [ -n "$key" ]; then
452                                 iw phy "$phy" interface add "$ifname" type mp
453                         else
454                                 iw phy "$phy" interface add "$ifname" type mp mesh_id "$mesh_id"
455                         fi
456                 ;;
457                 monitor)
458                         iw phy "$phy" interface add "$ifname" type monitor
459                 ;;
460                 sta)
461                         local wdsflag=
462                         staidx="$(($staidx + 1))"
463                         [ "$wds" -gt 0 ] && wdsflag="4addr on"
464                         iw phy "$phy" interface add "$ifname" type managed $wdsflag
465                         [ "$powersave" -gt 0 ] && powersave="on" || powersave="off"
466                         iw "$ifname" set power_save "$powersave"
467                 ;;
468         esac
469
470         case "$mode" in
471                 monitor|mesh)
472                         [ "$auto_channel" -gt 0 ] || iw dev "$ifname" set channel "$channel" $htmode
473                 ;;
474         esac
475
476         if [ "$mode" != "ap" ]; then
477                 # ALL ap functionality will be passed to hostapd
478                 # All interfaces must have unique mac addresses
479                 # which can either be explicitly set in the device
480                 # section, or automatically generated
481                 ifconfig "$ifname" hw ether "$macaddr"
482         fi
483
484         json_select ..
485 }
486
487 mac80211_setup_supplicant() {
488         wpa_supplicant_prepare_interface "$ifname" nl80211 || return 1
489         wpa_supplicant_add_network "$ifname"
490         wpa_supplicant_run "$ifname" ${hostapd_ctrl:+-H $hostapd_ctrl}
491 }
492
493 mac80211_setup_adhoc() {
494         json_get_vars bssid ssid key mcast_rate
495
496         keyspec=
497         [ "$auth_type" = "wep" ] && {
498                 set_default key 1
499                 case "$key" in
500                         [1234])
501                                 local idx
502                                 for idx in 1 2 3 4; do
503                                         json_get_var ikey "key$idx"
504
505                                         [ -n "$ikey" ] && {
506                                                 ikey="$(($idx - 1)):$(prepare_key_wep "$ikey")"
507                                                 [ $idx -eq $key ] && ikey="d:$ikey"
508                                                 append keyspec "$ikey"
509                                         }
510                                 done
511                         ;;
512                         *)
513                                 append keyspec "d:0:$(prepare_key_wep "$key")"
514                         ;;
515                 esac
516         }
517
518         brstr=
519         for br in $basic_rate_list; do
520                 hostapd_add_rate brstr "$br"
521         done
522
523         mcval=
524         [ -n "$mcast_rate" ] && hostapd_add_rate mcval "$mcast_rate"
525
526         iw dev "$ifname" ibss join "$ssid" $freq $htmode fixed-freq $bssid \
527                 ${beacon_int:+beacon-interval $beacon_int} \
528                 ${brstr:+basic-rates $brstr} \
529                 ${mcval:+mcast-rate $mcval} \
530                 ${keyspec:+keys $keyspec}
531 }
532
533 mac80211_setup_vif() {
534         local name="$1"
535         local failed
536
537         json_select data
538         json_get_vars ifname
539         json_select ..
540
541         json_select config
542         json_get_vars mode
543         json_get_var vif_txpower txpower
544
545         ifconfig "$ifname" up || {
546                 wireless_setup_vif_failed IFUP_ERROR
547                 json_select ..
548                 return
549         }
550
551         set_default vif_txpower "$txpower"
552         [ -z "$vif_txpower" ] || iw dev "$ifname" set txpower fixed "${vif_txpower%%.*}00"
553
554         case "$mode" in
555                 mesh)
556                         for var in $MP_CONFIG_INT $MP_CONFIG_BOOL $MP_CONFIG_STRING; do
557                                 json_get_var mp_val "$var"
558                                 [ -n "$mp_val" ] && iw dev "$ifname" set mesh_param "$var" "$mp_val"
559                         done
560
561                         # authsae
562                         json_get_vars key
563                         if [ -n "$key" ]; then
564                                 if [ -e "/lib/wifi/authsae.sh" ]; then
565                                         . /lib/wifi/authsae.sh
566                                         authsae_start_interface || failed=1
567                                 else
568                                         wireless_setup_vif_failed AUTHSAE_NOT_INSTALLED
569                                         json_select ..
570                                         return
571                                 fi
572                         fi
573                 ;;
574                 adhoc)
575                         wireless_vif_parse_encryption
576                         if [ "$wpa" -gt 0 -o "$auto_channel" -gt 0 ]; then
577                                 mac80211_setup_supplicant || failed=1
578                         else
579                                 mac80211_setup_adhoc
580                         fi
581                 ;;
582                 sta)
583                         mac80211_setup_supplicant || failed=1
584                 ;;
585         esac
586
587         json_select ..
588         [ -n "$failed" ] || wireless_add_vif "$name" "$ifname"
589 }
590
591 get_freq() {
592         local phy="$1"
593         local chan="$2"
594         iw "$phy" info | grep -E -m1 "(\* ${chan:-....} MHz${chan:+|\\[$chan\\]})" | grep MHz | awk '{print $2}'
595 }
596
597 mac80211_interface_cleanup() {
598         local phy="$1"
599
600         for wdev in $(list_phy_interfaces "$phy"); do
601                 ifconfig "$wdev" down 2>/dev/null
602                 iw dev "$wdev" del
603         done
604 }
605
606 drv_mac80211_cleanup() {
607         hostapd_common_cleanup
608 }
609
610 drv_mac80211_setup() {
611         json_select config
612         json_get_vars \
613                 phy macaddr path \
614                 country chanbw distance \
615                 txpower antenna_gain \
616                 rxantenna txantenna \
617                 frag rts beacon_int
618         json_get_values basic_rate_list basic_rate
619         json_select ..
620
621         find_phy || {
622                 echo "Could not find PHY for device '$1'"
623                 wireless_set_retry 0
624                 return 1
625         }
626
627         wireless_set_data phy="$phy"
628         mac80211_interface_cleanup "$phy"
629
630         # convert channel to frequency
631         [ "$auto_channel" -gt 0 ] || freq="$(get_freq "$phy" "$channel")"
632
633         [ -n "$country" ] && {
634                 iw reg get | grep -q "^country $country:" || {
635                         iw reg set "$country"
636                         sleep 1
637                 }
638         }
639
640         hostapd_conf_file="/var/run/hostapd-$phy.conf"
641
642         no_ap=1
643         macidx=0
644         staidx=0
645
646         [ -n "$chanbw" ] && {
647                 for file in /sys/kernel/debug/ieee80211/$phy/ath9k/chanbw /sys/kernel/debug/ieee80211/$phy/ath5k/bwmode; do
648                         [ -f "$file" ] && echo "$chanbw" > "$file"
649                 done
650         }
651
652         set_default rxantenna all
653         set_default txantenna all
654         set_default distance 0
655         set_default antenna_gain 0
656
657         iw phy "$phy" set antenna $txantenna $rxantenna >/dev/null 2>&1
658         iw phy "$phy" set antenna_gain $antenna_gain
659         iw phy "$phy" set distance "$distance"
660
661         [ -n "$frag" ] && iw phy "$phy" set frag "${frag%%.*}"
662         [ -n "$rts" ] && iw phy "$phy" set rts "${rts%%.*}"
663
664         has_ap=
665         hostapd_ctrl=
666         for_each_interface "ap" mac80211_check_ap
667
668         rm -f "$hostapd_conf_file"
669         [ -n "$has_ap" ] && mac80211_hostapd_setup_base "$phy"
670
671         for_each_interface "sta adhoc mesh monitor" mac80211_prepare_vif
672         for_each_interface "ap" mac80211_prepare_vif
673
674         [ -n "$hostapd_ctrl" ] && {
675                 /usr/sbin/hostapd -P /var/run/wifi-$phy.pid -B "$hostapd_conf_file"
676                 ret="$?"
677                 wireless_add_process "$(cat /var/run/wifi-$phy.pid)" "/usr/sbin/hostapd" 1
678                 [ "$ret" != 0 ] && {
679                         wireless_setup_failed HOSTAPD_START_FAILED
680                         return
681                 }
682         }
683
684         for_each_interface "ap sta adhoc mesh monitor" mac80211_setup_vif
685
686         wireless_set_up
687 }
688
689 list_phy_interfaces() {
690         local phy="$1"
691         if [ -d "/sys/class/ieee80211/${phy}/device/net" ]; then
692                 ls "/sys/class/ieee80211/${phy}/device/net" 2>/dev/null;
693         else
694                 ls "/sys/class/ieee80211/${phy}/device" 2>/dev/null | grep net: | sed -e 's,net:,,g'
695         fi
696 }
697
698 drv_mac80211_teardown() {
699         wireless_process_kill_all
700
701         json_select data
702         json_get_vars phy
703         json_select ..
704
705         mac80211_interface_cleanup "$phy"
706 }
707
708 add_driver mac80211