195cb192637af5076ce8df4b58e51cd32ea21703
[mech_eap.git] / src / ap / ap_drv_ops.c
1 /*
2  * hostapd - Driver operations
3  * Copyright (c) 2009-2010, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include "utils/includes.h"
10
11 #include "utils/common.h"
12 #include "common/ieee802_11_defs.h"
13 #include "common/hw_features_common.h"
14 #include "wps/wps.h"
15 #include "p2p/p2p.h"
16 #include "hostapd.h"
17 #include "ieee802_11.h"
18 #include "sta_info.h"
19 #include "ap_config.h"
20 #include "p2p_hostapd.h"
21 #include "hs20.h"
22 #include "ap_drv_ops.h"
23
24
25 u32 hostapd_sta_flags_to_drv(u32 flags)
26 {
27         int res = 0;
28         if (flags & WLAN_STA_AUTHORIZED)
29                 res |= WPA_STA_AUTHORIZED;
30         if (flags & WLAN_STA_WMM)
31                 res |= WPA_STA_WMM;
32         if (flags & WLAN_STA_SHORT_PREAMBLE)
33                 res |= WPA_STA_SHORT_PREAMBLE;
34         if (flags & WLAN_STA_MFP)
35                 res |= WPA_STA_MFP;
36         if (flags & WLAN_STA_AUTH)
37                 res |= WPA_STA_AUTHENTICATED;
38         if (flags & WLAN_STA_ASSOC)
39                 res |= WPA_STA_ASSOCIATED;
40         return res;
41 }
42
43
44 int hostapd_build_ap_extra_ies(struct hostapd_data *hapd,
45                                struct wpabuf **beacon_ret,
46                                struct wpabuf **proberesp_ret,
47                                struct wpabuf **assocresp_ret)
48 {
49         struct wpabuf *beacon = NULL, *proberesp = NULL, *assocresp = NULL;
50         u8 buf[200], *pos;
51
52         *beacon_ret = *proberesp_ret = *assocresp_ret = NULL;
53
54         pos = buf;
55         pos = hostapd_eid_time_adv(hapd, pos);
56         if (pos != buf) {
57                 if (wpabuf_resize(&beacon, pos - buf) != 0)
58                         goto fail;
59                 wpabuf_put_data(beacon, buf, pos - buf);
60         }
61         pos = hostapd_eid_time_zone(hapd, pos);
62         if (pos != buf) {
63                 if (wpabuf_resize(&proberesp, pos - buf) != 0)
64                         goto fail;
65                 wpabuf_put_data(proberesp, buf, pos - buf);
66         }
67
68         pos = buf;
69         pos = hostapd_eid_ext_capab(hapd, pos);
70         if (pos != buf) {
71                 if (wpabuf_resize(&assocresp, pos - buf) != 0)
72                         goto fail;
73                 wpabuf_put_data(assocresp, buf, pos - buf);
74         }
75         pos = hostapd_eid_interworking(hapd, pos);
76         pos = hostapd_eid_adv_proto(hapd, pos);
77         pos = hostapd_eid_roaming_consortium(hapd, pos);
78         if (pos != buf) {
79                 if (wpabuf_resize(&beacon, pos - buf) != 0)
80                         goto fail;
81                 wpabuf_put_data(beacon, buf, pos - buf);
82
83                 if (wpabuf_resize(&proberesp, pos - buf) != 0)
84                         goto fail;
85                 wpabuf_put_data(proberesp, buf, pos - buf);
86         }
87
88 #ifdef CONFIG_FST
89         if (hapd->iface->fst_ies) {
90                 size_t add = wpabuf_len(hapd->iface->fst_ies);
91
92                 if (wpabuf_resize(&beacon, add) < 0)
93                         goto fail;
94                 wpabuf_put_buf(beacon, hapd->iface->fst_ies);
95                 if (wpabuf_resize(&proberesp, add) < 0)
96                         goto fail;
97                 wpabuf_put_buf(proberesp, hapd->iface->fst_ies);
98                 if (wpabuf_resize(&assocresp, add) < 0)
99                         goto fail;
100                 wpabuf_put_buf(assocresp, hapd->iface->fst_ies);
101         }
102 #endif /* CONFIG_FST */
103
104         if (hapd->wps_beacon_ie) {
105                 if (wpabuf_resize(&beacon, wpabuf_len(hapd->wps_beacon_ie)) <
106                     0)
107                         goto fail;
108                 wpabuf_put_buf(beacon, hapd->wps_beacon_ie);
109         }
110
111         if (hapd->wps_probe_resp_ie) {
112                 if (wpabuf_resize(&proberesp,
113                                   wpabuf_len(hapd->wps_probe_resp_ie)) < 0)
114                         goto fail;
115                 wpabuf_put_buf(proberesp, hapd->wps_probe_resp_ie);
116         }
117
118 #ifdef CONFIG_P2P
119         if (hapd->p2p_beacon_ie) {
120                 if (wpabuf_resize(&beacon, wpabuf_len(hapd->p2p_beacon_ie)) <
121                     0)
122                         goto fail;
123                 wpabuf_put_buf(beacon, hapd->p2p_beacon_ie);
124         }
125
126         if (hapd->p2p_probe_resp_ie) {
127                 if (wpabuf_resize(&proberesp,
128                                   wpabuf_len(hapd->p2p_probe_resp_ie)) < 0)
129                         goto fail;
130                 wpabuf_put_buf(proberesp, hapd->p2p_probe_resp_ie);
131         }
132 #endif /* CONFIG_P2P */
133
134 #ifdef CONFIG_P2P_MANAGER
135         if (hapd->conf->p2p & P2P_MANAGE) {
136                 if (wpabuf_resize(&beacon, 100) == 0) {
137                         u8 *start, *p;
138                         start = wpabuf_put(beacon, 0);
139                         p = hostapd_eid_p2p_manage(hapd, start);
140                         wpabuf_put(beacon, p - start);
141                 }
142
143                 if (wpabuf_resize(&proberesp, 100) == 0) {
144                         u8 *start, *p;
145                         start = wpabuf_put(proberesp, 0);
146                         p = hostapd_eid_p2p_manage(hapd, start);
147                         wpabuf_put(proberesp, p - start);
148                 }
149         }
150 #endif /* CONFIG_P2P_MANAGER */
151
152 #ifdef CONFIG_WPS
153         if (hapd->conf->wps_state) {
154                 struct wpabuf *a = wps_build_assoc_resp_ie();
155                 if (a && wpabuf_resize(&assocresp, wpabuf_len(a)) == 0)
156                         wpabuf_put_buf(assocresp, a);
157                 wpabuf_free(a);
158         }
159 #endif /* CONFIG_WPS */
160
161 #ifdef CONFIG_P2P_MANAGER
162         if (hapd->conf->p2p & P2P_MANAGE) {
163                 if (wpabuf_resize(&assocresp, 100) == 0) {
164                         u8 *start, *p;
165                         start = wpabuf_put(assocresp, 0);
166                         p = hostapd_eid_p2p_manage(hapd, start);
167                         wpabuf_put(assocresp, p - start);
168                 }
169         }
170 #endif /* CONFIG_P2P_MANAGER */
171
172 #ifdef CONFIG_WIFI_DISPLAY
173         if (hapd->p2p_group) {
174                 struct wpabuf *a;
175                 a = p2p_group_assoc_resp_ie(hapd->p2p_group, P2P_SC_SUCCESS);
176                 if (a && wpabuf_resize(&assocresp, wpabuf_len(a)) == 0)
177                         wpabuf_put_buf(assocresp, a);
178                 wpabuf_free(a);
179         }
180 #endif /* CONFIG_WIFI_DISPLAY */
181
182 #ifdef CONFIG_HS20
183         pos = buf;
184         pos = hostapd_eid_hs20_indication(hapd, pos);
185         if (pos != buf) {
186                 if (wpabuf_resize(&beacon, pos - buf) != 0)
187                         goto fail;
188                 wpabuf_put_data(beacon, buf, pos - buf);
189
190                 if (wpabuf_resize(&proberesp, pos - buf) != 0)
191                         goto fail;
192                 wpabuf_put_data(proberesp, buf, pos - buf);
193         }
194
195         pos = hostapd_eid_osen(hapd, buf);
196         if (pos != buf) {
197                 if (wpabuf_resize(&beacon, pos - buf) != 0)
198                         goto fail;
199                 wpabuf_put_data(beacon, buf, pos - buf);
200
201                 if (wpabuf_resize(&proberesp, pos - buf) != 0)
202                         goto fail;
203                 wpabuf_put_data(proberesp, buf, pos - buf);
204         }
205 #endif /* CONFIG_HS20 */
206
207         if (hapd->conf->vendor_elements) {
208                 size_t add = wpabuf_len(hapd->conf->vendor_elements);
209                 if (wpabuf_resize(&beacon, add) == 0)
210                         wpabuf_put_buf(beacon, hapd->conf->vendor_elements);
211                 if (wpabuf_resize(&proberesp, add) == 0)
212                         wpabuf_put_buf(proberesp, hapd->conf->vendor_elements);
213         }
214
215         *beacon_ret = beacon;
216         *proberesp_ret = proberesp;
217         *assocresp_ret = assocresp;
218
219         return 0;
220
221 fail:
222         wpabuf_free(beacon);
223         wpabuf_free(proberesp);
224         wpabuf_free(assocresp);
225         return -1;
226 }
227
228
229 void hostapd_free_ap_extra_ies(struct hostapd_data *hapd,
230                                struct wpabuf *beacon,
231                                struct wpabuf *proberesp,
232                                struct wpabuf *assocresp)
233 {
234         wpabuf_free(beacon);
235         wpabuf_free(proberesp);
236         wpabuf_free(assocresp);
237 }
238
239
240 int hostapd_reset_ap_wps_ie(struct hostapd_data *hapd)
241 {
242         if (hapd->driver == NULL || hapd->driver->set_ap_wps_ie == NULL)
243                 return 0;
244
245         return hapd->driver->set_ap_wps_ie(hapd->drv_priv, NULL, NULL, NULL);
246 }
247
248
249 int hostapd_set_ap_wps_ie(struct hostapd_data *hapd)
250 {
251         struct wpabuf *beacon, *proberesp, *assocresp;
252         int ret;
253
254         if (hapd->driver == NULL || hapd->driver->set_ap_wps_ie == NULL)
255                 return 0;
256
257         if (hostapd_build_ap_extra_ies(hapd, &beacon, &proberesp, &assocresp) <
258             0)
259                 return -1;
260
261         ret = hapd->driver->set_ap_wps_ie(hapd->drv_priv, beacon, proberesp,
262                                           assocresp);
263
264         hostapd_free_ap_extra_ies(hapd, beacon, proberesp, assocresp);
265
266         return ret;
267 }
268
269
270 int hostapd_set_authorized(struct hostapd_data *hapd,
271                            struct sta_info *sta, int authorized)
272 {
273         if (authorized) {
274                 return hostapd_sta_set_flags(hapd, sta->addr,
275                                              hostapd_sta_flags_to_drv(
276                                                      sta->flags),
277                                              WPA_STA_AUTHORIZED, ~0);
278         }
279
280         return hostapd_sta_set_flags(hapd, sta->addr,
281                                      hostapd_sta_flags_to_drv(sta->flags),
282                                      0, ~WPA_STA_AUTHORIZED);
283 }
284
285
286 int hostapd_set_sta_flags(struct hostapd_data *hapd, struct sta_info *sta)
287 {
288         int set_flags, total_flags, flags_and, flags_or;
289         total_flags = hostapd_sta_flags_to_drv(sta->flags);
290         set_flags = WPA_STA_SHORT_PREAMBLE | WPA_STA_WMM | WPA_STA_MFP;
291         if (((!hapd->conf->ieee802_1x && !hapd->conf->wpa) ||
292              sta->auth_alg == WLAN_AUTH_FT) &&
293             sta->flags & WLAN_STA_AUTHORIZED)
294                 set_flags |= WPA_STA_AUTHORIZED;
295         flags_or = total_flags & set_flags;
296         flags_and = total_flags | ~set_flags;
297         return hostapd_sta_set_flags(hapd, sta->addr, total_flags,
298                                      flags_or, flags_and);
299 }
300
301
302 int hostapd_set_drv_ieee8021x(struct hostapd_data *hapd, const char *ifname,
303                               int enabled)
304 {
305         struct wpa_bss_params params;
306         os_memset(&params, 0, sizeof(params));
307         params.ifname = ifname;
308         params.enabled = enabled;
309         if (enabled) {
310                 params.wpa = hapd->conf->wpa;
311                 params.ieee802_1x = hapd->conf->ieee802_1x;
312                 params.wpa_group = hapd->conf->wpa_group;
313                 if ((hapd->conf->wpa & (WPA_PROTO_WPA | WPA_PROTO_RSN)) ==
314                     (WPA_PROTO_WPA | WPA_PROTO_RSN))
315                         params.wpa_pairwise = hapd->conf->wpa_pairwise |
316                                 hapd->conf->rsn_pairwise;
317                 else if (hapd->conf->wpa & WPA_PROTO_RSN)
318                         params.wpa_pairwise = hapd->conf->rsn_pairwise;
319                 else if (hapd->conf->wpa & WPA_PROTO_WPA)
320                         params.wpa_pairwise = hapd->conf->wpa_pairwise;
321                 params.wpa_key_mgmt = hapd->conf->wpa_key_mgmt;
322                 params.rsn_preauth = hapd->conf->rsn_preauth;
323 #ifdef CONFIG_IEEE80211W
324                 params.ieee80211w = hapd->conf->ieee80211w;
325 #endif /* CONFIG_IEEE80211W */
326         }
327         return hostapd_set_ieee8021x(hapd, &params);
328 }
329
330
331 int hostapd_vlan_if_add(struct hostapd_data *hapd, const char *ifname)
332 {
333         char force_ifname[IFNAMSIZ];
334         u8 if_addr[ETH_ALEN];
335         return hostapd_if_add(hapd, WPA_IF_AP_VLAN, ifname, hapd->own_addr,
336                               NULL, NULL, force_ifname, if_addr, NULL, 0);
337 }
338
339
340 int hostapd_vlan_if_remove(struct hostapd_data *hapd, const char *ifname)
341 {
342         return hostapd_if_remove(hapd, WPA_IF_AP_VLAN, ifname);
343 }
344
345
346 int hostapd_set_wds_sta(struct hostapd_data *hapd, char *ifname_wds,
347                         const u8 *addr, int aid, int val)
348 {
349         const char *bridge = NULL;
350
351         if (hapd->driver == NULL || hapd->driver->set_wds_sta == NULL)
352                 return -1;
353         if (hapd->conf->wds_bridge[0])
354                 bridge = hapd->conf->wds_bridge;
355         else if (hapd->conf->bridge[0])
356                 bridge = hapd->conf->bridge;
357         return hapd->driver->set_wds_sta(hapd->drv_priv, addr, aid, val,
358                                          bridge, ifname_wds);
359 }
360
361
362 int hostapd_add_sta_node(struct hostapd_data *hapd, const u8 *addr,
363                          u16 auth_alg)
364 {
365         if (hapd->driver == NULL || hapd->driver->add_sta_node == NULL)
366                 return 0;
367         return hapd->driver->add_sta_node(hapd->drv_priv, addr, auth_alg);
368 }
369
370
371 int hostapd_sta_auth(struct hostapd_data *hapd, const u8 *addr,
372                      u16 seq, u16 status, const u8 *ie, size_t len)
373 {
374         if (hapd->driver == NULL || hapd->driver->sta_auth == NULL)
375                 return 0;
376         return hapd->driver->sta_auth(hapd->drv_priv, hapd->own_addr, addr,
377                                       seq, status, ie, len);
378 }
379
380
381 int hostapd_sta_assoc(struct hostapd_data *hapd, const u8 *addr,
382                       int reassoc, u16 status, const u8 *ie, size_t len)
383 {
384         if (hapd->driver == NULL || hapd->driver->sta_assoc == NULL)
385                 return 0;
386         return hapd->driver->sta_assoc(hapd->drv_priv, hapd->own_addr, addr,
387                                        reassoc, status, ie, len);
388 }
389
390
391 int hostapd_sta_add(struct hostapd_data *hapd,
392                     const u8 *addr, u16 aid, u16 capability,
393                     const u8 *supp_rates, size_t supp_rates_len,
394                     u16 listen_interval,
395                     const struct ieee80211_ht_capabilities *ht_capab,
396                     const struct ieee80211_vht_capabilities *vht_capab,
397                     u32 flags, u8 qosinfo, u8 vht_opmode, int set)
398 {
399         struct hostapd_sta_add_params params;
400
401         if (hapd->driver == NULL)
402                 return 0;
403         if (hapd->driver->sta_add == NULL)
404                 return 0;
405
406         os_memset(&params, 0, sizeof(params));
407         params.addr = addr;
408         params.aid = aid;
409         params.capability = capability;
410         params.supp_rates = supp_rates;
411         params.supp_rates_len = supp_rates_len;
412         params.listen_interval = listen_interval;
413         params.ht_capabilities = ht_capab;
414         params.vht_capabilities = vht_capab;
415         params.vht_opmode_enabled = !!(flags & WLAN_STA_VHT_OPMODE_ENABLED);
416         params.vht_opmode = vht_opmode;
417         params.flags = hostapd_sta_flags_to_drv(flags);
418         params.qosinfo = qosinfo;
419         params.set = set;
420         return hapd->driver->sta_add(hapd->drv_priv, &params);
421 }
422
423
424 int hostapd_add_tspec(struct hostapd_data *hapd, const u8 *addr,
425                       u8 *tspec_ie, size_t tspec_ielen)
426 {
427         if (hapd->driver == NULL || hapd->driver->add_tspec == NULL)
428                 return 0;
429         return hapd->driver->add_tspec(hapd->drv_priv, addr, tspec_ie,
430                                        tspec_ielen);
431 }
432
433
434 int hostapd_set_privacy(struct hostapd_data *hapd, int enabled)
435 {
436         if (hapd->driver == NULL || hapd->driver->set_privacy == NULL)
437                 return 0;
438         return hapd->driver->set_privacy(hapd->drv_priv, enabled);
439 }
440
441
442 int hostapd_set_generic_elem(struct hostapd_data *hapd, const u8 *elem,
443                              size_t elem_len)
444 {
445         if (hapd->driver == NULL || hapd->driver->set_generic_elem == NULL)
446                 return 0;
447         return hapd->driver->set_generic_elem(hapd->drv_priv, elem, elem_len);
448 }
449
450
451 int hostapd_get_ssid(struct hostapd_data *hapd, u8 *buf, size_t len)
452 {
453         if (hapd->driver == NULL || hapd->driver->hapd_get_ssid == NULL)
454                 return 0;
455         return hapd->driver->hapd_get_ssid(hapd->drv_priv, buf, len);
456 }
457
458
459 int hostapd_set_ssid(struct hostapd_data *hapd, const u8 *buf, size_t len)
460 {
461         if (hapd->driver == NULL || hapd->driver->hapd_set_ssid == NULL)
462                 return 0;
463         return hapd->driver->hapd_set_ssid(hapd->drv_priv, buf, len);
464 }
465
466
467 int hostapd_if_add(struct hostapd_data *hapd, enum wpa_driver_if_type type,
468                    const char *ifname, const u8 *addr, void *bss_ctx,
469                    void **drv_priv, char *force_ifname, u8 *if_addr,
470                    const char *bridge, int use_existing)
471 {
472         if (hapd->driver == NULL || hapd->driver->if_add == NULL)
473                 return -1;
474         return hapd->driver->if_add(hapd->drv_priv, type, ifname, addr,
475                                     bss_ctx, drv_priv, force_ifname, if_addr,
476                                     bridge, use_existing, 1);
477 }
478
479
480 int hostapd_if_remove(struct hostapd_data *hapd, enum wpa_driver_if_type type,
481                       const char *ifname)
482 {
483         if (hapd->driver == NULL || hapd->drv_priv == NULL ||
484             hapd->driver->if_remove == NULL)
485                 return -1;
486         return hapd->driver->if_remove(hapd->drv_priv, type, ifname);
487 }
488
489
490 int hostapd_set_ieee8021x(struct hostapd_data *hapd,
491                           struct wpa_bss_params *params)
492 {
493         if (hapd->driver == NULL || hapd->driver->set_ieee8021x == NULL)
494                 return 0;
495         return hapd->driver->set_ieee8021x(hapd->drv_priv, params);
496 }
497
498
499 int hostapd_get_seqnum(const char *ifname, struct hostapd_data *hapd,
500                        const u8 *addr, int idx, u8 *seq)
501 {
502         if (hapd->driver == NULL || hapd->driver->get_seqnum == NULL)
503                 return 0;
504         return hapd->driver->get_seqnum(ifname, hapd->drv_priv, addr, idx,
505                                         seq);
506 }
507
508
509 int hostapd_flush(struct hostapd_data *hapd)
510 {
511         if (hapd->driver == NULL || hapd->driver->flush == NULL)
512                 return 0;
513         return hapd->driver->flush(hapd->drv_priv);
514 }
515
516
517 int hostapd_set_freq(struct hostapd_data *hapd, enum hostapd_hw_mode mode,
518                      int freq, int channel, int ht_enabled, int vht_enabled,
519                      int sec_channel_offset, int vht_oper_chwidth,
520                      int center_segment0, int center_segment1)
521 {
522         struct hostapd_freq_params data;
523
524         if (hostapd_set_freq_params(&data, mode, freq, channel, ht_enabled,
525                                     vht_enabled, sec_channel_offset,
526                                     vht_oper_chwidth,
527                                     center_segment0, center_segment1,
528                                     hapd->iface->current_mode ?
529                                     hapd->iface->current_mode->vht_capab : 0))
530                 return -1;
531
532         if (hapd->driver == NULL)
533                 return 0;
534         if (hapd->driver->set_freq == NULL)
535                 return 0;
536         return hapd->driver->set_freq(hapd->drv_priv, &data);
537 }
538
539 int hostapd_set_rts(struct hostapd_data *hapd, int rts)
540 {
541         if (hapd->driver == NULL || hapd->driver->set_rts == NULL)
542                 return 0;
543         return hapd->driver->set_rts(hapd->drv_priv, rts);
544 }
545
546
547 int hostapd_set_frag(struct hostapd_data *hapd, int frag)
548 {
549         if (hapd->driver == NULL || hapd->driver->set_frag == NULL)
550                 return 0;
551         return hapd->driver->set_frag(hapd->drv_priv, frag);
552 }
553
554
555 int hostapd_sta_set_flags(struct hostapd_data *hapd, u8 *addr,
556                           int total_flags, int flags_or, int flags_and)
557 {
558         if (hapd->driver == NULL || hapd->driver->sta_set_flags == NULL)
559                 return 0;
560         return hapd->driver->sta_set_flags(hapd->drv_priv, addr, total_flags,
561                                            flags_or, flags_and);
562 }
563
564
565 int hostapd_set_country(struct hostapd_data *hapd, const char *country)
566 {
567         if (hapd->driver == NULL ||
568             hapd->driver->set_country == NULL)
569                 return 0;
570         return hapd->driver->set_country(hapd->drv_priv, country);
571 }
572
573
574 int hostapd_set_tx_queue_params(struct hostapd_data *hapd, int queue, int aifs,
575                                 int cw_min, int cw_max, int burst_time)
576 {
577         if (hapd->driver == NULL || hapd->driver->set_tx_queue_params == NULL)
578                 return 0;
579         return hapd->driver->set_tx_queue_params(hapd->drv_priv, queue, aifs,
580                                                  cw_min, cw_max, burst_time);
581 }
582
583
584 struct hostapd_hw_modes *
585 hostapd_get_hw_feature_data(struct hostapd_data *hapd, u16 *num_modes,
586                             u16 *flags)
587 {
588         if (hapd->driver == NULL ||
589             hapd->driver->get_hw_feature_data == NULL)
590                 return NULL;
591         return hapd->driver->get_hw_feature_data(hapd->drv_priv, num_modes,
592                                                  flags);
593 }
594
595
596 int hostapd_driver_commit(struct hostapd_data *hapd)
597 {
598         if (hapd->driver == NULL || hapd->driver->commit == NULL)
599                 return 0;
600         return hapd->driver->commit(hapd->drv_priv);
601 }
602
603
604 int hostapd_drv_none(struct hostapd_data *hapd)
605 {
606         return hapd->driver && os_strcmp(hapd->driver->name, "none") == 0;
607 }
608
609
610 int hostapd_driver_scan(struct hostapd_data *hapd,
611                         struct wpa_driver_scan_params *params)
612 {
613         if (hapd->driver && hapd->driver->scan2)
614                 return hapd->driver->scan2(hapd->drv_priv, params);
615         return -1;
616 }
617
618
619 struct wpa_scan_results * hostapd_driver_get_scan_results(
620         struct hostapd_data *hapd)
621 {
622         if (hapd->driver && hapd->driver->get_scan_results2)
623                 return hapd->driver->get_scan_results2(hapd->drv_priv);
624         return NULL;
625 }
626
627
628 int hostapd_driver_set_noa(struct hostapd_data *hapd, u8 count, int start,
629                            int duration)
630 {
631         if (hapd->driver && hapd->driver->set_noa)
632                 return hapd->driver->set_noa(hapd->drv_priv, count, start,
633                                              duration);
634         return -1;
635 }
636
637
638 int hostapd_drv_set_key(const char *ifname, struct hostapd_data *hapd,
639                         enum wpa_alg alg, const u8 *addr,
640                         int key_idx, int set_tx,
641                         const u8 *seq, size_t seq_len,
642                         const u8 *key, size_t key_len)
643 {
644         if (hapd->driver == NULL || hapd->driver->set_key == NULL)
645                 return 0;
646         return hapd->driver->set_key(ifname, hapd->drv_priv, alg, addr,
647                                      key_idx, set_tx, seq, seq_len, key,
648                                      key_len);
649 }
650
651
652 int hostapd_drv_send_mlme(struct hostapd_data *hapd,
653                           const void *msg, size_t len, int noack)
654 {
655         if (hapd->driver == NULL || hapd->driver->send_mlme == NULL)
656                 return 0;
657         return hapd->driver->send_mlme(hapd->drv_priv, msg, len, noack, 0,
658                                        NULL, 0);
659 }
660
661
662 int hostapd_drv_send_mlme_csa(struct hostapd_data *hapd,
663                               const void *msg, size_t len, int noack,
664                               const u16 *csa_offs, size_t csa_offs_len)
665 {
666         if (hapd->driver == NULL || hapd->driver->send_mlme == NULL)
667                 return 0;
668         return hapd->driver->send_mlme(hapd->drv_priv, msg, len, noack, 0,
669                                        csa_offs, csa_offs_len);
670 }
671
672
673 int hostapd_drv_sta_deauth(struct hostapd_data *hapd,
674                            const u8 *addr, int reason)
675 {
676         if (hapd->driver == NULL || hapd->driver->sta_deauth == NULL)
677                 return 0;
678         return hapd->driver->sta_deauth(hapd->drv_priv, hapd->own_addr, addr,
679                                         reason);
680 }
681
682
683 int hostapd_drv_sta_disassoc(struct hostapd_data *hapd,
684                              const u8 *addr, int reason)
685 {
686         if (hapd->driver == NULL || hapd->driver->sta_disassoc == NULL)
687                 return 0;
688         return hapd->driver->sta_disassoc(hapd->drv_priv, hapd->own_addr, addr,
689                                           reason);
690 }
691
692
693 int hostapd_drv_wnm_oper(struct hostapd_data *hapd, enum wnm_oper oper,
694                          const u8 *peer, u8 *buf, u16 *buf_len)
695 {
696         if (hapd->driver == NULL || hapd->driver->wnm_oper == NULL)
697                 return -1;
698         return hapd->driver->wnm_oper(hapd->drv_priv, oper, peer, buf,
699                                       buf_len);
700 }
701
702
703 int hostapd_drv_send_action(struct hostapd_data *hapd, unsigned int freq,
704                             unsigned int wait, const u8 *dst, const u8 *data,
705                             size_t len)
706 {
707         if (hapd->driver == NULL || hapd->driver->send_action == NULL)
708                 return 0;
709         return hapd->driver->send_action(hapd->drv_priv, freq, wait, dst,
710                                          hapd->own_addr, hapd->own_addr, data,
711                                          len, 0);
712 }
713
714
715 int hostapd_start_dfs_cac(struct hostapd_iface *iface,
716                           enum hostapd_hw_mode mode, int freq,
717                           int channel, int ht_enabled, int vht_enabled,
718                           int sec_channel_offset, int vht_oper_chwidth,
719                           int center_segment0, int center_segment1)
720 {
721         struct hostapd_data *hapd = iface->bss[0];
722         struct hostapd_freq_params data;
723         int res;
724
725         if (!hapd->driver || !hapd->driver->start_dfs_cac)
726                 return 0;
727
728         if (!iface->conf->ieee80211h) {
729                 wpa_printf(MSG_ERROR, "Can't start DFS CAC, DFS functionality "
730                            "is not enabled");
731                 return -1;
732         }
733
734         if (hostapd_set_freq_params(&data, mode, freq, channel, ht_enabled,
735                                     vht_enabled, sec_channel_offset,
736                                     vht_oper_chwidth, center_segment0,
737                                     center_segment1,
738                                     iface->current_mode->vht_capab)) {
739                 wpa_printf(MSG_ERROR, "Can't set freq params");
740                 return -1;
741         }
742
743         res = hapd->driver->start_dfs_cac(hapd->drv_priv, &data);
744         if (!res) {
745                 iface->cac_started = 1;
746                 os_get_reltime(&iface->dfs_cac_start);
747         }
748
749         return res;
750 }
751
752
753 int hostapd_drv_set_qos_map(struct hostapd_data *hapd,
754                             const u8 *qos_map_set, u8 qos_map_set_len)
755 {
756         if (hapd->driver == NULL || hapd->driver->set_qos_map == NULL)
757                 return 0;
758         return hapd->driver->set_qos_map(hapd->drv_priv, qos_map_set,
759                                          qos_map_set_len);
760 }
761
762
763 static void hostapd_get_hw_mode_any_channels(struct hostapd_data *hapd,
764                                              struct hostapd_hw_modes *mode,
765                                              int acs_ch_list_all,
766                                              int **freq_list)
767 {
768         int i;
769
770         for (i = 0; i < mode->num_channels; i++) {
771                 struct hostapd_channel_data *chan = &mode->channels[i];
772
773                 if ((acs_ch_list_all ||
774                      freq_range_list_includes(&hapd->iface->conf->acs_ch_list,
775                                               chan->chan)) &&
776                     !(chan->flag & HOSTAPD_CHAN_DISABLED))
777                         int_array_add_unique(freq_list, chan->freq);
778         }
779 }
780
781
782 int hostapd_drv_do_acs(struct hostapd_data *hapd)
783 {
784         struct drv_acs_params params;
785         int ret, i, acs_ch_list_all = 0;
786         u8 *channels = NULL;
787         unsigned int num_channels = 0;
788         struct hostapd_hw_modes *mode;
789         int *freq_list = NULL;
790
791         if (hapd->driver == NULL || hapd->driver->do_acs == NULL)
792                 return 0;
793
794         os_memset(&params, 0, sizeof(params));
795         params.hw_mode = hapd->iface->conf->hw_mode;
796
797         /*
798          * If no chanlist config parameter is provided, include all enabled
799          * channels of the selected hw_mode.
800          */
801         if (!hapd->iface->conf->acs_ch_list.num)
802                 acs_ch_list_all = 1;
803
804         mode = hapd->iface->current_mode;
805         if (mode) {
806                 channels = os_malloc(mode->num_channels);
807                 if (channels == NULL)
808                         return -1;
809
810                 for (i = 0; i < mode->num_channels; i++) {
811                         struct hostapd_channel_data *chan = &mode->channels[i];
812                         if (!acs_ch_list_all &&
813                             !freq_range_list_includes(
814                                     &hapd->iface->conf->acs_ch_list,
815                                     chan->chan))
816                                 continue;
817                         if (!(chan->flag & HOSTAPD_CHAN_DISABLED)) {
818                                 channels[num_channels++] = chan->chan;
819                                 int_array_add_unique(&freq_list, chan->freq);
820                         }
821                 }
822         } else {
823                 for (i = 0; i < hapd->iface->num_hw_features; i++) {
824                         mode = &hapd->iface->hw_features[i];
825                         hostapd_get_hw_mode_any_channels(hapd, mode,
826                                                          acs_ch_list_all,
827                                                          &freq_list);
828                 }
829         }
830
831         params.ch_list = channels;
832         params.ch_list_len = num_channels;
833         params.freq_list = freq_list;
834
835         params.ht_enabled = !!(hapd->iface->conf->ieee80211n);
836         params.ht40_enabled = !!(hapd->iface->conf->ht_capab &
837                                  HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET);
838         params.vht_enabled = !!(hapd->iface->conf->ieee80211ac);
839         params.ch_width = 20;
840         if (hapd->iface->conf->ieee80211n && params.ht40_enabled)
841                 params.ch_width = 40;
842
843         /* Note: VHT20 is defined by combination of ht_capab & vht_oper_chwidth
844          */
845         if (hapd->iface->conf->ieee80211ac && params.ht40_enabled) {
846                 if (hapd->iface->conf->vht_oper_chwidth == VHT_CHANWIDTH_80MHZ)
847                         params.ch_width = 80;
848                 else if (hapd->iface->conf->vht_oper_chwidth ==
849                          VHT_CHANWIDTH_160MHZ ||
850                          hapd->iface->conf->vht_oper_chwidth ==
851                          VHT_CHANWIDTH_80P80MHZ)
852                         params.ch_width = 160;
853         }
854
855         ret = hapd->driver->do_acs(hapd->drv_priv, &params);
856         os_free(channels);
857
858         return ret;
859 }