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