77d4909af2a294e6898dcb47de6695414d647c11
[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 program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "utils/includes.h"
16
17 #include "utils/common.h"
18 #include "drivers/driver.h"
19 #include "common/ieee802_11_defs.h"
20 #include "wps/wps.h"
21 #include "hostapd.h"
22 #include "ieee802_11.h"
23 #include "sta_info.h"
24 #include "ap_config.h"
25 #include "p2p_hostapd.h"
26 #include "ap_drv_ops.h"
27
28
29 u32 hostapd_sta_flags_to_drv(u32 flags)
30 {
31         int res = 0;
32         if (flags & WLAN_STA_AUTHORIZED)
33                 res |= WPA_STA_AUTHORIZED;
34         if (flags & WLAN_STA_WMM)
35                 res |= WPA_STA_WMM;
36         if (flags & WLAN_STA_SHORT_PREAMBLE)
37                 res |= WPA_STA_SHORT_PREAMBLE;
38         if (flags & WLAN_STA_MFP)
39                 res |= WPA_STA_MFP;
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[100], *pos;
51
52         *beacon_ret = *proberesp_ret = *assocresp_ret = NULL;
53
54         pos = buf;
55         pos = hostapd_eid_ext_capab(hapd, pos);
56         if (pos != buf) {
57                 if (wpabuf_resize(&assocresp, pos - buf) != 0)
58                         goto fail;
59                 wpabuf_put_data(assocresp, buf, pos - buf);
60         }
61         pos = hostapd_eid_interworking(hapd, pos);
62         if (pos != buf) {
63                 if (wpabuf_resize(&beacon, pos - buf) != 0)
64                         goto fail;
65                 wpabuf_put_data(beacon, buf, pos - buf);
66
67                 if (wpabuf_resize(&proberesp, pos - buf) != 0)
68                         goto fail;
69                 wpabuf_put_data(proberesp, buf, pos - buf);
70         }
71
72         if (hapd->wps_beacon_ie) {
73                 if (wpabuf_resize(&beacon, wpabuf_len(hapd->wps_beacon_ie)) <
74                     0)
75                         goto fail;
76                 wpabuf_put_buf(beacon, hapd->wps_beacon_ie);
77         }
78
79         if (hapd->wps_probe_resp_ie) {
80                 if (wpabuf_resize(&proberesp,
81                                   wpabuf_len(hapd->wps_probe_resp_ie)) < 0)
82                         goto fail;
83                 wpabuf_put_buf(proberesp, hapd->wps_probe_resp_ie);
84         }
85
86 #ifdef CONFIG_P2P
87         if (hapd->p2p_beacon_ie) {
88                 if (wpabuf_resize(&beacon, wpabuf_len(hapd->p2p_beacon_ie)) <
89                     0)
90                         goto fail;
91                 wpabuf_put_buf(beacon, hapd->p2p_beacon_ie);
92         }
93
94         if (hapd->p2p_probe_resp_ie) {
95                 if (wpabuf_resize(&beacon, wpabuf_len(hapd->p2p_probe_resp_ie))
96                     < 0)
97                         goto fail;
98                 wpabuf_put_buf(beacon, hapd->p2p_probe_resp_ie);
99         }
100 #endif /* CONFIG_P2P */
101
102 #ifdef CONFIG_P2P_MANAGER
103         if (hapd->conf->p2p & P2P_MANAGE) {
104                 if (wpabuf_resize(&beacon, 100) == 0) {
105                         u8 *start, *p;
106                         start = wpabuf_put(beacon, 0);
107                         p = hostapd_eid_p2p_manage(hapd, start);
108                         wpabuf_put(beacon, p - start);
109                 }
110
111                 if (wpabuf_resize(&proberesp, 100) == 0) {
112                         u8 *start, *p;
113                         start = wpabuf_put(proberesp, 0);
114                         p = hostapd_eid_p2p_manage(hapd, start);
115                         wpabuf_put(proberesp, p - start);
116                 }
117         }
118 #endif /* CONFIG_P2P_MANAGER */
119
120 #ifdef CONFIG_WPS2
121         if (hapd->conf->wps_state) {
122                 struct wpabuf *a = wps_build_assoc_resp_ie();
123                 if (a && wpabuf_resize(&assocresp, wpabuf_len(a)) == 0)
124                         wpabuf_put_buf(assocresp, a);
125                 wpabuf_free(a);
126         }
127 #endif /* CONFIG_WPS2 */
128
129 #ifdef CONFIG_P2P_MANAGER
130         if (hapd->conf->p2p & P2P_MANAGE) {
131                 if (wpabuf_resize(&assocresp, 100) == 0) {
132                         u8 *start, *p;
133                         start = wpabuf_put(assocresp, 0);
134                         p = hostapd_eid_p2p_manage(hapd, start);
135                         wpabuf_put(assocresp, p - start);
136                 }
137         }
138 #endif /* CONFIG_P2P_MANAGER */
139
140         *beacon_ret = beacon;
141         *proberesp_ret = proberesp;
142         *assocresp_ret = assocresp;
143
144         return 0;
145
146 fail:
147         wpabuf_free(beacon);
148         wpabuf_free(proberesp);
149         wpabuf_free(assocresp);
150         return -1;
151 }
152
153
154 void hostapd_free_ap_extra_ies(struct hostapd_data *hapd,
155                                struct wpabuf *beacon,
156                                struct wpabuf *proberesp,
157                                struct wpabuf *assocresp)
158 {
159         wpabuf_free(beacon);
160         wpabuf_free(proberesp);
161         wpabuf_free(assocresp);
162 }
163
164
165 int hostapd_set_ap_wps_ie(struct hostapd_data *hapd)
166 {
167         struct wpabuf *beacon, *proberesp, *assocresp;
168         int ret;
169
170         if (hapd->driver == NULL || hapd->driver->set_ap_wps_ie == NULL)
171                 return 0;
172
173         if (hostapd_build_ap_extra_ies(hapd, &beacon, &proberesp, &assocresp) <
174             0)
175                 return -1;
176
177         ret = hapd->driver->set_ap_wps_ie(hapd->drv_priv, beacon, proberesp,
178                                           assocresp);
179
180         hostapd_free_ap_extra_ies(hapd, beacon, proberesp, assocresp);
181
182         return ret;
183 }
184
185
186 int hostapd_set_authorized(struct hostapd_data *hapd,
187                            struct sta_info *sta, int authorized)
188 {
189         if (authorized) {
190                 return hostapd_sta_set_flags(hapd, sta->addr,
191                                              hostapd_sta_flags_to_drv(
192                                                      sta->flags),
193                                              WPA_STA_AUTHORIZED, ~0);
194         }
195
196         return hostapd_sta_set_flags(hapd, sta->addr,
197                                      hostapd_sta_flags_to_drv(sta->flags),
198                                      0, ~WPA_STA_AUTHORIZED);
199 }
200
201
202 int hostapd_set_sta_flags(struct hostapd_data *hapd, struct sta_info *sta)
203 {
204         int set_flags, total_flags, flags_and, flags_or;
205         total_flags = hostapd_sta_flags_to_drv(sta->flags);
206         set_flags = WPA_STA_SHORT_PREAMBLE | WPA_STA_WMM | WPA_STA_MFP;
207         if (((!hapd->conf->ieee802_1x && !hapd->conf->wpa) ||
208              sta->auth_alg == WLAN_AUTH_FT) &&
209             sta->flags & WLAN_STA_AUTHORIZED)
210                 set_flags |= WPA_STA_AUTHORIZED;
211         flags_or = total_flags & set_flags;
212         flags_and = total_flags | ~set_flags;
213         return hostapd_sta_set_flags(hapd, sta->addr, total_flags,
214                                      flags_or, flags_and);
215 }
216
217
218 int hostapd_set_drv_ieee8021x(struct hostapd_data *hapd, const char *ifname,
219                               int enabled)
220 {
221         struct wpa_bss_params params;
222         os_memset(&params, 0, sizeof(params));
223         params.ifname = ifname;
224         params.enabled = enabled;
225         if (enabled) {
226                 params.wpa = hapd->conf->wpa;
227                 params.ieee802_1x = hapd->conf->ieee802_1x;
228                 params.wpa_group = hapd->conf->wpa_group;
229                 params.wpa_pairwise = hapd->conf->wpa_pairwise;
230                 params.wpa_key_mgmt = hapd->conf->wpa_key_mgmt;
231                 params.rsn_preauth = hapd->conf->rsn_preauth;
232 #ifdef CONFIG_IEEE80211W
233                 params.ieee80211w = hapd->conf->ieee80211w;
234 #endif /* CONFIG_IEEE80211W */
235         }
236         return hostapd_set_ieee8021x(hapd, &params);
237 }
238
239
240 int hostapd_vlan_if_add(struct hostapd_data *hapd, const char *ifname)
241 {
242         char force_ifname[IFNAMSIZ];
243         u8 if_addr[ETH_ALEN];
244         return hostapd_if_add(hapd, WPA_IF_AP_VLAN, ifname, hapd->own_addr,
245                               NULL, NULL, force_ifname, if_addr, NULL);
246 }
247
248
249 int hostapd_vlan_if_remove(struct hostapd_data *hapd, const char *ifname)
250 {
251         return hostapd_if_remove(hapd, WPA_IF_AP_VLAN, ifname);
252 }
253
254
255 int hostapd_set_wds_sta(struct hostapd_data *hapd, const u8 *addr, int aid,
256                         int val)
257 {
258         const char *bridge = NULL;
259
260         if (hapd->driver == NULL || hapd->driver->set_wds_sta == NULL)
261                 return 0;
262         if (hapd->conf->wds_bridge[0])
263                 bridge = hapd->conf->wds_bridge;
264         else if (hapd->conf->bridge[0])
265                 bridge = hapd->conf->bridge;
266         return hapd->driver->set_wds_sta(hapd->drv_priv, addr, aid, val,
267                                          bridge);
268 }
269
270
271 int hostapd_add_sta_node(struct hostapd_data *hapd, const u8 *addr,
272                          u16 auth_alg)
273 {
274         if (hapd->driver == NULL || hapd->driver->add_sta_node == NULL)
275                 return 0;
276         return hapd->driver->add_sta_node(hapd->drv_priv, addr, auth_alg);
277 }
278
279
280 int hostapd_sta_auth(struct hostapd_data *hapd, const u8 *addr,
281                      u16 seq, u16 status, const u8 *ie, size_t len)
282 {
283         if (hapd->driver == NULL || hapd->driver->sta_auth == NULL)
284                 return 0;
285         return hapd->driver->sta_auth(hapd->drv_priv, hapd->own_addr, addr,
286                                       seq, status, ie, len);
287 }
288
289
290 int hostapd_sta_assoc(struct hostapd_data *hapd, const u8 *addr,
291                       int reassoc, u16 status, const u8 *ie, size_t len)
292 {
293         if (hapd->driver == NULL || hapd->driver->sta_assoc == NULL)
294                 return 0;
295         return hapd->driver->sta_assoc(hapd->drv_priv, hapd->own_addr, addr,
296                                        reassoc, status, ie, len);
297 }
298
299
300 int hostapd_sta_add(struct hostapd_data *hapd,
301                     const u8 *addr, u16 aid, u16 capability,
302                     const u8 *supp_rates, size_t supp_rates_len,
303                     u16 listen_interval,
304                     const struct ieee80211_ht_capabilities *ht_capab,
305                     u32 flags)
306 {
307         struct hostapd_sta_add_params params;
308
309         if (hapd->driver == NULL)
310                 return 0;
311         if (hapd->driver->sta_add == NULL)
312                 return 0;
313
314         os_memset(&params, 0, sizeof(params));
315         params.addr = addr;
316         params.aid = aid;
317         params.capability = capability;
318         params.supp_rates = supp_rates;
319         params.supp_rates_len = supp_rates_len;
320         params.listen_interval = listen_interval;
321         params.ht_capabilities = ht_capab;
322         params.flags = hostapd_sta_flags_to_drv(flags);
323         return hapd->driver->sta_add(hapd->drv_priv, &params);
324 }
325
326
327 int hostapd_add_tspec(struct hostapd_data *hapd, const u8 *addr,
328                       u8 *tspec_ie, size_t tspec_ielen)
329 {
330         if (hapd->driver == NULL || hapd->driver->add_tspec == NULL)
331                 return 0;
332         return hapd->driver->add_tspec(hapd->drv_priv, addr, tspec_ie,
333                                        tspec_ielen);
334 }
335
336
337 int hostapd_set_privacy(struct hostapd_data *hapd, int enabled)
338 {
339         if (hapd->driver == NULL || hapd->driver->set_privacy == NULL)
340                 return 0;
341         return hapd->driver->set_privacy(hapd->drv_priv, enabled);
342 }
343
344
345 int hostapd_set_generic_elem(struct hostapd_data *hapd, const u8 *elem,
346                              size_t elem_len)
347 {
348         if (hapd->driver == NULL || hapd->driver->set_generic_elem == NULL)
349                 return 0;
350         return hapd->driver->set_generic_elem(hapd->drv_priv, elem, elem_len);
351 }
352
353
354 int hostapd_get_ssid(struct hostapd_data *hapd, u8 *buf, size_t len)
355 {
356         if (hapd->driver == NULL || hapd->driver->hapd_get_ssid == NULL)
357                 return 0;
358         return hapd->driver->hapd_get_ssid(hapd->drv_priv, buf, len);
359 }
360
361
362 int hostapd_set_ssid(struct hostapd_data *hapd, const u8 *buf, size_t len)
363 {
364         if (hapd->driver == NULL || hapd->driver->hapd_set_ssid == NULL)
365                 return 0;
366         return hapd->driver->hapd_set_ssid(hapd->drv_priv, buf, len);
367 }
368
369
370 int hostapd_if_add(struct hostapd_data *hapd, enum wpa_driver_if_type type,
371                    const char *ifname, const u8 *addr, void *bss_ctx,
372                    void **drv_priv, char *force_ifname, u8 *if_addr,
373                    const char *bridge)
374 {
375         if (hapd->driver == NULL || hapd->driver->if_add == NULL)
376                 return -1;
377         return hapd->driver->if_add(hapd->drv_priv, type, ifname, addr,
378                                     bss_ctx, drv_priv, force_ifname, if_addr,
379                                     bridge);
380 }
381
382
383 int hostapd_if_remove(struct hostapd_data *hapd, enum wpa_driver_if_type type,
384                       const char *ifname)
385 {
386         if (hapd->driver == NULL || hapd->driver->if_remove == NULL)
387                 return -1;
388         return hapd->driver->if_remove(hapd->drv_priv, type, ifname);
389 }
390
391
392 int hostapd_set_ieee8021x(struct hostapd_data *hapd,
393                           struct wpa_bss_params *params)
394 {
395         if (hapd->driver == NULL || hapd->driver->set_ieee8021x == NULL)
396                 return 0;
397         return hapd->driver->set_ieee8021x(hapd->drv_priv, params);
398 }
399
400
401 int hostapd_get_seqnum(const char *ifname, struct hostapd_data *hapd,
402                        const u8 *addr, int idx, u8 *seq)
403 {
404         if (hapd->driver == NULL || hapd->driver->get_seqnum == NULL)
405                 return 0;
406         return hapd->driver->get_seqnum(ifname, hapd->drv_priv, addr, idx,
407                                         seq);
408 }
409
410
411 int hostapd_flush(struct hostapd_data *hapd)
412 {
413         if (hapd->driver == NULL || hapd->driver->flush == NULL)
414                 return 0;
415         return hapd->driver->flush(hapd->drv_priv);
416 }
417
418
419 int hostapd_set_freq(struct hostapd_data *hapd, int mode, int freq,
420                      int channel, int ht_enabled, int sec_channel_offset)
421 {
422         struct hostapd_freq_params data;
423         if (hapd->driver == NULL)
424                 return 0;
425         if (hapd->driver->set_freq == NULL)
426                 return 0;
427         os_memset(&data, 0, sizeof(data));
428         data.mode = mode;
429         data.freq = freq;
430         data.channel = channel;
431         data.ht_enabled = ht_enabled;
432         data.sec_channel_offset = sec_channel_offset;
433         return hapd->driver->set_freq(hapd->drv_priv, &data);
434 }
435
436 int hostapd_set_rts(struct hostapd_data *hapd, int rts)
437 {
438         if (hapd->driver == NULL || hapd->driver->set_rts == NULL)
439                 return 0;
440         return hapd->driver->set_rts(hapd->drv_priv, rts);
441 }
442
443
444 int hostapd_set_frag(struct hostapd_data *hapd, int frag)
445 {
446         if (hapd->driver == NULL || hapd->driver->set_frag == NULL)
447                 return 0;
448         return hapd->driver->set_frag(hapd->drv_priv, frag);
449 }
450
451
452 int hostapd_sta_set_flags(struct hostapd_data *hapd, u8 *addr,
453                           int total_flags, int flags_or, int flags_and)
454 {
455         if (hapd->driver == NULL || hapd->driver->sta_set_flags == NULL)
456                 return 0;
457         return hapd->driver->sta_set_flags(hapd->drv_priv, addr, total_flags,
458                                            flags_or, flags_and);
459 }
460
461
462 int hostapd_set_rate_sets(struct hostapd_data *hapd, int *supp_rates,
463                           int *basic_rates, int mode)
464 {
465         if (hapd->driver == NULL || hapd->driver->set_rate_sets == NULL)
466                 return 0;
467         return hapd->driver->set_rate_sets(hapd->drv_priv, supp_rates,
468                                            basic_rates, mode);
469 }
470
471
472 int hostapd_set_country(struct hostapd_data *hapd, const char *country)
473 {
474         if (hapd->driver == NULL ||
475             hapd->driver->set_country == NULL)
476                 return 0;
477         return hapd->driver->set_country(hapd->drv_priv, country);
478 }
479
480
481 int hostapd_set_tx_queue_params(struct hostapd_data *hapd, int queue, int aifs,
482                                 int cw_min, int cw_max, int burst_time)
483 {
484         if (hapd->driver == NULL || hapd->driver->set_tx_queue_params == NULL)
485                 return 0;
486         return hapd->driver->set_tx_queue_params(hapd->drv_priv, queue, aifs,
487                                                  cw_min, cw_max, burst_time);
488 }
489
490
491 int hostapd_valid_bss_mask(struct hostapd_data *hapd, const u8 *addr,
492                            const u8 *mask)
493 {
494         if (hapd->driver == NULL || hapd->driver->valid_bss_mask == NULL)
495                 return 1;
496         return hapd->driver->valid_bss_mask(hapd->drv_priv, addr, mask);
497 }
498
499
500 struct hostapd_hw_modes *
501 hostapd_get_hw_feature_data(struct hostapd_data *hapd, u16 *num_modes,
502                             u16 *flags)
503 {
504         if (hapd->driver == NULL ||
505             hapd->driver->get_hw_feature_data == NULL)
506                 return NULL;
507         return hapd->driver->get_hw_feature_data(hapd->drv_priv, num_modes,
508                                                  flags);
509 }
510
511
512 int hostapd_driver_commit(struct hostapd_data *hapd)
513 {
514         if (hapd->driver == NULL || hapd->driver->commit == NULL)
515                 return 0;
516         return hapd->driver->commit(hapd->drv_priv);
517 }
518
519
520 int hostapd_drv_none(struct hostapd_data *hapd)
521 {
522         return hapd->driver && os_strcmp(hapd->driver->name, "none") == 0;
523 }
524
525
526 int hostapd_driver_scan(struct hostapd_data *hapd,
527                         struct wpa_driver_scan_params *params)
528 {
529         if (hapd->driver && hapd->driver->scan2)
530                 return hapd->driver->scan2(hapd->drv_priv, params);
531         return -1;
532 }
533
534
535 struct wpa_scan_results * hostapd_driver_get_scan_results(
536         struct hostapd_data *hapd)
537 {
538         if (hapd->driver && hapd->driver->get_scan_results2)
539                 return hapd->driver->get_scan_results2(hapd->drv_priv);
540         return NULL;
541 }
542
543
544 int hostapd_driver_set_noa(struct hostapd_data *hapd, u8 count, int start,
545                            int duration)
546 {
547         if (hapd->driver && hapd->driver->set_noa)
548                 return hapd->driver->set_noa(hapd->drv_priv, count, start,
549                                              duration);
550         return -1;
551 }
552
553
554 int hostapd_drv_set_key(const char *ifname, struct hostapd_data *hapd,
555                         enum wpa_alg alg, const u8 *addr,
556                         int key_idx, int set_tx,
557                         const u8 *seq, size_t seq_len,
558                         const u8 *key, size_t key_len)
559 {
560         if (hapd->driver == NULL || hapd->driver->set_key == NULL)
561                 return 0;
562         return hapd->driver->set_key(ifname, hapd->drv_priv, alg, addr,
563                                      key_idx, set_tx, seq, seq_len, key,
564                                      key_len);
565 }
566
567
568 int hostapd_drv_send_mlme(struct hostapd_data *hapd,
569                           const void *msg, size_t len)
570 {
571         if (hapd->driver == NULL || hapd->driver->send_mlme == NULL)
572                 return 0;
573         return hapd->driver->send_mlme(hapd->drv_priv, msg, len);
574 }
575
576
577 int hostapd_drv_sta_deauth(struct hostapd_data *hapd,
578                            const u8 *addr, int reason)
579 {
580         if (hapd->driver == NULL || hapd->driver->sta_deauth == NULL)
581                 return 0;
582         return hapd->driver->sta_deauth(hapd->drv_priv, hapd->own_addr, addr,
583                                         reason);
584 }
585
586
587 int hostapd_drv_sta_disassoc(struct hostapd_data *hapd,
588                              const u8 *addr, int reason)
589 {
590         if (hapd->driver == NULL || hapd->driver->sta_disassoc == NULL)
591                 return 0;
592         return hapd->driver->sta_disassoc(hapd->drv_priv, hapd->own_addr, addr,
593                                           reason);
594 }