Remove unnecessary ifname parameter from hapd_get_ssid/hapd_set_ssid
[libeap.git] / src / ap / ap_drv_ops.c
1 /*
2  * hostapd - Driver operations
3  * Copyright (c) 2009, 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 "hostapd.h"
20 #include "ieee802_11.h"
21 #include "sta_info.h"
22 #include "ap_config.h"
23 #include "ap_drv_ops.h"
24
25
26 static int hostapd_sta_flags_to_drv(int flags)
27 {
28         int res = 0;
29         if (flags & WLAN_STA_AUTHORIZED)
30                 res |= WPA_STA_AUTHORIZED;
31         if (flags & WLAN_STA_WMM)
32                 res |= WPA_STA_WMM;
33         if (flags & WLAN_STA_SHORT_PREAMBLE)
34                 res |= WPA_STA_SHORT_PREAMBLE;
35         if (flags & WLAN_STA_MFP)
36                 res |= WPA_STA_MFP;
37         return res;
38 }
39
40
41 static int hostapd_set_ap_wps_ie(struct hostapd_data *hapd,
42                                  const struct wpabuf *beacon,
43                                  const struct wpabuf *proberesp)
44 {
45         if (hapd->driver == NULL || hapd->driver->set_ap_wps_ie == NULL)
46                 return 0;
47         return hapd->driver->set_ap_wps_ie(hapd->conf->iface, hapd->drv_priv,
48                                            beacon, proberesp);
49 }
50
51
52 static int hostapd_send_mgmt_frame(struct hostapd_data *hapd, const void *msg,
53                            size_t len)
54 {
55         if (hapd->driver == NULL || hapd->driver->send_mlme == NULL)
56                 return 0;
57         return hapd->driver->send_mlme(hapd->drv_priv, msg, len);
58 }
59
60
61 static int hostapd_send_eapol(struct hostapd_data *hapd, const u8 *addr,
62                               const u8 *data, size_t data_len, int encrypt)
63 {
64         if (hapd->driver == NULL || hapd->driver->hapd_send_eapol == NULL)
65                 return 0;
66         return hapd->driver->hapd_send_eapol(hapd->drv_priv, addr, data,
67                                              data_len, encrypt,
68                                              hapd->own_addr);
69 }
70
71
72 static int hostapd_set_authorized(struct hostapd_data *hapd,
73                                   struct sta_info *sta, int authorized)
74 {
75         if (authorized) {
76                 return hostapd_sta_set_flags(hapd, sta->addr,
77                                              hostapd_sta_flags_to_drv(
78                                                      sta->flags),
79                                              WPA_STA_AUTHORIZED, ~0);
80         }
81
82         return hostapd_sta_set_flags(hapd, sta->addr,
83                                      hostapd_sta_flags_to_drv(sta->flags),
84                                      0, ~WPA_STA_AUTHORIZED);
85 }
86
87
88 static int hostapd_set_key(const char *ifname, struct hostapd_data *hapd,
89                            enum wpa_alg alg, const u8 *addr, int key_idx,
90                            int set_tx, const u8 *seq, size_t seq_len,
91                            const u8 *key, size_t key_len)
92 {
93         if (hapd->driver == NULL || hapd->driver->set_key == NULL)
94                 return 0;
95         return hapd->driver->set_key(ifname, hapd->drv_priv, alg, addr,
96                                      key_idx, set_tx, seq, seq_len, key,
97                                      key_len);
98 }
99
100
101 static int hostapd_read_sta_data(struct hostapd_data *hapd,
102                                  struct hostap_sta_driver_data *data,
103                                  const u8 *addr)
104 {
105         if (hapd->driver == NULL || hapd->driver->read_sta_data == NULL)
106                 return -1;
107         return hapd->driver->read_sta_data(hapd->drv_priv, data, addr);
108 }
109
110
111 static int hostapd_sta_clear_stats(struct hostapd_data *hapd, const u8 *addr)
112 {
113         if (hapd->driver == NULL || hapd->driver->sta_clear_stats == NULL)
114                 return 0;
115         return hapd->driver->sta_clear_stats(hapd->drv_priv, addr);
116 }
117
118
119 static int hostapd_set_sta_flags(struct hostapd_data *hapd,
120                                  struct sta_info *sta)
121 {
122         int set_flags, total_flags, flags_and, flags_or;
123         total_flags = hostapd_sta_flags_to_drv(sta->flags);
124         set_flags = WPA_STA_SHORT_PREAMBLE | WPA_STA_WMM | WPA_STA_MFP;
125         if (!hapd->conf->ieee802_1x && !hapd->conf->wpa &&
126             sta->flags & WLAN_STA_AUTHORIZED)
127                 set_flags |= WPA_STA_AUTHORIZED;
128         flags_or = total_flags & set_flags;
129         flags_and = total_flags | ~set_flags;
130         return hostapd_sta_set_flags(hapd, sta->addr, total_flags,
131                                      flags_or, flags_and);
132 }
133
134
135 static int hostapd_set_drv_ieee8021x(struct hostapd_data *hapd,
136                                      const char *ifname, int enabled)
137 {
138         struct wpa_bss_params params;
139         os_memset(&params, 0, sizeof(params));
140         params.ifname = ifname;
141         params.enabled = enabled;
142         if (enabled) {
143                 params.wpa = hapd->conf->wpa;
144                 params.ieee802_1x = hapd->conf->ieee802_1x;
145                 params.wpa_group = hapd->conf->wpa_group;
146                 params.wpa_pairwise = hapd->conf->wpa_pairwise;
147                 params.wpa_key_mgmt = hapd->conf->wpa_key_mgmt;
148                 params.rsn_preauth = hapd->conf->rsn_preauth;
149         }
150         return hostapd_set_ieee8021x(hapd, &params);
151 }
152
153
154 static int hostapd_set_radius_acl_auth(struct hostapd_data *hapd,
155                                        const u8 *mac, int accepted,
156                                        u32 session_timeout)
157 {
158         if (hapd->driver == NULL || hapd->driver->set_radius_acl_auth == NULL)
159                 return 0;
160         return hapd->driver->set_radius_acl_auth(hapd->drv_priv, mac, accepted,
161                                                  session_timeout);
162 }
163
164
165 static int hostapd_set_radius_acl_expire(struct hostapd_data *hapd,
166                                          const u8 *mac)
167 {
168         if (hapd->driver == NULL ||
169             hapd->driver->set_radius_acl_expire == NULL)
170                 return 0;
171         return hapd->driver->set_radius_acl_expire(hapd->drv_priv, mac);
172 }
173
174
175 static int hostapd_set_bss_params(struct hostapd_data *hapd,
176                                   int use_protection)
177 {
178         int ret = 0;
179         int preamble;
180 #ifdef CONFIG_IEEE80211N
181         u8 buf[60], *ht_capab, *ht_oper, *pos;
182
183         pos = buf;
184         ht_capab = pos;
185         pos = hostapd_eid_ht_capabilities(hapd, pos);
186         ht_oper = pos;
187         pos = hostapd_eid_ht_operation(hapd, pos);
188         if (pos > ht_oper && ht_oper > ht_capab &&
189             hostapd_set_ht_params(hapd->conf->iface, hapd,
190                                   ht_capab + 2, ht_capab[1],
191                                   ht_oper + 2, ht_oper[1])) {
192                 wpa_printf(MSG_ERROR, "Could not set HT capabilities "
193                            "for kernel driver");
194                 ret = -1;
195         }
196
197 #endif /* CONFIG_IEEE80211N */
198
199         if (hostapd_set_cts_protect(hapd, use_protection)) {
200                 wpa_printf(MSG_ERROR, "Failed to set CTS protect in kernel "
201                            "driver");
202                 ret = -1;
203         }
204
205         if (hapd->iface->current_mode &&
206             hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
207             hostapd_set_short_slot_time(hapd,
208                                         hapd->iface->num_sta_no_short_slot_time
209                                         > 0 ? 0 : 1)) {
210                 wpa_printf(MSG_ERROR, "Failed to set Short Slot Time option "
211                            "in kernel driver");
212                 ret = -1;
213         }
214
215         if (hapd->iface->num_sta_no_short_preamble == 0 &&
216             hapd->iconf->preamble == SHORT_PREAMBLE)
217                 preamble = SHORT_PREAMBLE;
218         else
219                 preamble = LONG_PREAMBLE;
220         if (hostapd_set_preamble(hapd, preamble)) {
221                 wpa_printf(MSG_ERROR, "Could not set preamble for kernel "
222                            "driver");
223                 ret = -1;
224         }
225
226         return ret;
227 }
228
229
230 static int hostapd_set_beacon(struct hostapd_data *hapd,
231                               const u8 *head, size_t head_len,
232                               const u8 *tail, size_t tail_len, int dtim_period,
233                               int beacon_int)
234 {
235         if (hapd->driver == NULL || hapd->driver->set_beacon == NULL)
236                 return 0;
237         return hapd->driver->set_beacon(hapd->drv_priv,
238                                         head, head_len, tail, tail_len,
239                                         dtim_period, beacon_int);
240 }
241
242
243 static int hostapd_vlan_if_add(struct hostapd_data *hapd, const char *ifname)
244 {
245         return hostapd_if_add(hapd, WPA_IF_AP_VLAN, ifname, NULL, NULL, NULL);
246 }
247
248 static int hostapd_vlan_if_remove(struct hostapd_data *hapd,
249                                   const char *ifname)
250 {
251         return hostapd_if_remove(hapd, WPA_IF_AP_VLAN, ifname);
252 }
253
254
255 static int hostapd_set_wds_sta(struct hostapd_data *hapd, const u8 *addr,
256                                int aid, int val)
257 {
258         if (hapd->driver == NULL || hapd->driver->set_wds_sta == NULL)
259                 return 0;
260         return hapd->driver->set_wds_sta(hapd->drv_priv, addr, aid, val);
261 }
262
263
264 static int hostapd_set_sta_vlan(const char *ifname, struct hostapd_data *hapd,
265                                 const u8 *addr, int vlan_id)
266 {
267         if (hapd->driver == NULL || hapd->driver->set_sta_vlan == NULL)
268                 return 0;
269         return hapd->driver->set_sta_vlan(hapd->drv_priv, addr, ifname,
270                                           vlan_id);
271 }
272
273
274 static int hostapd_get_inact_sec(struct hostapd_data *hapd, const u8 *addr)
275 {
276         if (hapd->driver == NULL || hapd->driver->get_inact_sec == NULL)
277                 return 0;
278         return hapd->driver->get_inact_sec(hapd->drv_priv, addr);
279 }
280
281
282 static int hostapd_sta_deauth(struct hostapd_data *hapd, const u8 *addr,
283                               int reason)
284 {
285         if (hapd->driver == NULL || hapd->driver->sta_deauth == NULL)
286                 return 0;
287         return hapd->driver->sta_deauth(hapd->drv_priv, hapd->own_addr, addr,
288                                         reason);
289 }
290
291
292 static int hostapd_sta_disassoc(struct hostapd_data *hapd, const u8 *addr,
293                                 int reason)
294 {
295         if (hapd->driver == NULL || hapd->driver->sta_disassoc == NULL)
296                 return 0;
297         return hapd->driver->sta_disassoc(hapd->drv_priv, hapd->own_addr, addr,
298                                           reason);
299 }
300
301
302 static int hostapd_sta_add(const char *ifname, struct hostapd_data *hapd,
303                            const u8 *addr, u16 aid, u16 capability,
304                            const u8 *supp_rates, size_t supp_rates_len,
305                            u16 listen_interval,
306                            const struct ieee80211_ht_capabilities *ht_capab)
307 {
308         struct hostapd_sta_add_params params;
309
310         if (hapd->driver == NULL)
311                 return 0;
312         if (hapd->driver->sta_add == NULL)
313                 return 0;
314
315         os_memset(&params, 0, sizeof(params));
316         params.addr = addr;
317         params.aid = aid;
318         params.capability = capability;
319         params.supp_rates = supp_rates;
320         params.supp_rates_len = supp_rates_len;
321         params.listen_interval = listen_interval;
322         params.ht_capabilities = ht_capab;
323         return hapd->driver->sta_add(ifname, hapd->drv_priv, &params);
324 }
325
326
327 static int hostapd_sta_remove(struct hostapd_data *hapd, const u8 *addr)
328 {
329         if (hapd->driver == NULL || hapd->driver->sta_remove == NULL)
330                 return 0;
331         return hapd->driver->sta_remove(hapd->drv_priv, addr);
332 }
333
334
335 static int hostapd_set_countermeasures(struct hostapd_data *hapd, int enabled)
336 {
337         if (hapd->driver == NULL ||
338             hapd->driver->hapd_set_countermeasures == NULL)
339                 return 0;
340         return hapd->driver->hapd_set_countermeasures(hapd->drv_priv, enabled);
341 }
342
343
344 void hostapd_set_driver_ops(struct hostapd_driver_ops *ops)
345 {
346         ops->set_ap_wps_ie = hostapd_set_ap_wps_ie;
347         ops->send_mgmt_frame = hostapd_send_mgmt_frame;
348         ops->send_eapol = hostapd_send_eapol;
349         ops->set_authorized = hostapd_set_authorized;
350         ops->set_key = hostapd_set_key;
351         ops->read_sta_data = hostapd_read_sta_data;
352         ops->sta_clear_stats = hostapd_sta_clear_stats;
353         ops->set_sta_flags = hostapd_set_sta_flags;
354         ops->set_drv_ieee8021x = hostapd_set_drv_ieee8021x;
355         ops->set_radius_acl_auth = hostapd_set_radius_acl_auth;
356         ops->set_radius_acl_expire = hostapd_set_radius_acl_expire;
357         ops->set_bss_params = hostapd_set_bss_params;
358         ops->set_beacon = hostapd_set_beacon;
359         ops->vlan_if_add = hostapd_vlan_if_add;
360         ops->vlan_if_remove = hostapd_vlan_if_remove;
361         ops->set_wds_sta = hostapd_set_wds_sta;
362         ops->set_sta_vlan = hostapd_set_sta_vlan;
363         ops->get_inact_sec = hostapd_get_inact_sec;
364         ops->sta_deauth = hostapd_sta_deauth;
365         ops->sta_disassoc = hostapd_sta_disassoc;
366         ops->sta_add = hostapd_sta_add;
367         ops->sta_remove = hostapd_sta_remove;
368         ops->set_countermeasures = hostapd_set_countermeasures;
369 }
370
371
372 int hostapd_set_privacy(struct hostapd_data *hapd, int enabled)
373 {
374         if (hapd->driver == NULL || hapd->driver->set_privacy == NULL)
375                 return 0;
376         return hapd->driver->set_privacy(hapd->drv_priv, enabled);
377 }
378
379
380 int hostapd_set_generic_elem(struct hostapd_data *hapd, const u8 *elem,
381                              size_t elem_len)
382 {
383         if (hapd->driver == NULL || hapd->driver->set_generic_elem == NULL)
384                 return 0;
385         return hapd->driver->set_generic_elem(hapd->drv_priv, elem, elem_len);
386 }
387
388
389 int hostapd_get_ssid(struct hostapd_data *hapd, u8 *buf, size_t len)
390 {
391         if (hapd->driver == NULL || hapd->driver->hapd_get_ssid == NULL)
392                 return 0;
393         return hapd->driver->hapd_get_ssid(hapd->drv_priv, buf, len);
394 }
395
396
397 int hostapd_set_ssid(struct hostapd_data *hapd, const u8 *buf, size_t len)
398 {
399         if (hapd->driver == NULL || hapd->driver->hapd_set_ssid == NULL)
400                 return 0;
401         return hapd->driver->hapd_set_ssid(hapd->drv_priv, buf, len);
402 }
403
404
405 int hostapd_if_add(struct hostapd_data *hapd, enum wpa_driver_if_type type,
406                    const char *ifname, const u8 *addr, void *bss_ctx,
407                    void **drv_priv)
408 {
409         if (hapd->driver == NULL || hapd->driver->if_add == NULL)
410                 return -1;
411         return hapd->driver->if_add(hapd->drv_priv, type, ifname, addr,
412                                     bss_ctx, drv_priv);
413 }
414
415
416 int hostapd_if_remove(struct hostapd_data *hapd, enum wpa_driver_if_type type,
417                       const char *ifname)
418 {
419         if (hapd->driver == NULL || hapd->driver->if_remove == NULL)
420                 return -1;
421         return hapd->driver->if_remove(hapd->drv_priv, type, ifname);
422 }
423
424
425 int hostapd_set_ieee8021x(struct hostapd_data *hapd,
426                           struct wpa_bss_params *params)
427 {
428         if (hapd->driver == NULL || hapd->driver->set_ieee8021x == NULL)
429                 return 0;
430         return hapd->driver->set_ieee8021x(hapd->drv_priv, params);
431 }
432
433
434 int hostapd_get_seqnum(const char *ifname, struct hostapd_data *hapd,
435                        const u8 *addr, int idx, u8 *seq)
436 {
437         if (hapd->driver == NULL || hapd->driver->get_seqnum == NULL)
438                 return 0;
439         return hapd->driver->get_seqnum(ifname, hapd->drv_priv, addr, idx,
440                                         seq);
441 }
442
443
444 int hostapd_flush(struct hostapd_data *hapd)
445 {
446         if (hapd->driver == NULL || hapd->driver->flush == NULL)
447                 return 0;
448         return hapd->driver->flush(hapd->drv_priv);
449 }
450
451
452 int hostapd_set_freq(struct hostapd_data *hapd, int mode, int freq,
453                      int channel, int ht_enabled, int sec_channel_offset)
454 {
455         struct hostapd_freq_params data;
456         if (hapd->driver == NULL)
457                 return 0;
458         if (hapd->driver->set_freq == NULL)
459                 return 0;
460         os_memset(&data, 0, sizeof(data));
461         data.mode = mode;
462         data.freq = freq;
463         data.channel = channel;
464         data.ht_enabled = ht_enabled;
465         data.sec_channel_offset = sec_channel_offset;
466         return hapd->driver->set_freq(hapd->drv_priv, &data);
467 }
468
469 int hostapd_set_rts(struct hostapd_data *hapd, int rts)
470 {
471         if (hapd->driver == NULL || hapd->driver->set_rts == NULL)
472                 return 0;
473         return hapd->driver->set_rts(hapd->drv_priv, rts);
474 }
475
476
477 int hostapd_set_frag(struct hostapd_data *hapd, int frag)
478 {
479         if (hapd->driver == NULL || hapd->driver->set_frag == NULL)
480                 return 0;
481         return hapd->driver->set_frag(hapd->drv_priv, frag);
482 }
483
484
485 int hostapd_sta_set_flags(struct hostapd_data *hapd, u8 *addr,
486                           int total_flags, int flags_or, int flags_and)
487 {
488         if (hapd->driver == NULL || hapd->driver->sta_set_flags == NULL)
489                 return 0;
490         return hapd->driver->sta_set_flags(hapd->conf->iface, hapd->drv_priv, addr, total_flags,
491                                            flags_or, flags_and);
492 }
493
494
495 int hostapd_set_rate_sets(struct hostapd_data *hapd, int *supp_rates,
496                           int *basic_rates, int mode)
497 {
498         if (hapd->driver == NULL || hapd->driver->set_rate_sets == NULL)
499                 return 0;
500         return hapd->driver->set_rate_sets(hapd->drv_priv, supp_rates,
501                                            basic_rates, mode);
502 }
503
504
505 int hostapd_set_country(struct hostapd_data *hapd, const char *country)
506 {
507         if (hapd->driver == NULL ||
508             hapd->driver->set_country == NULL)
509                 return 0;
510         return hapd->driver->set_country(hapd->drv_priv, country);
511 }
512
513
514 int hostapd_set_cts_protect(struct hostapd_data *hapd, int value)
515 {
516         if (hapd->driver == NULL || hapd->driver->set_cts_protect == NULL)
517                 return 0;
518         return hapd->driver->set_cts_protect(hapd->drv_priv, value);
519 }
520
521
522 int hostapd_set_preamble(struct hostapd_data *hapd, int value)
523 {
524         if (hapd->driver == NULL || hapd->driver->set_preamble == NULL)
525                 return 0;
526         return hapd->driver->set_preamble(hapd->drv_priv, value);
527 }
528
529
530 int hostapd_set_short_slot_time(struct hostapd_data *hapd, int value)
531 {
532         if (hapd->driver == NULL || hapd->driver->set_short_slot_time == NULL)
533                 return 0;
534         return hapd->driver->set_short_slot_time(hapd->drv_priv, value);
535 }
536
537
538 int hostapd_set_tx_queue_params(struct hostapd_data *hapd, int queue, int aifs,
539                                 int cw_min, int cw_max, int burst_time)
540 {
541         if (hapd->driver == NULL || hapd->driver->set_tx_queue_params == NULL)
542                 return 0;
543         return hapd->driver->set_tx_queue_params(hapd->drv_priv, queue, aifs,
544                                                  cw_min, cw_max, burst_time);
545 }
546
547
548 int hostapd_valid_bss_mask(struct hostapd_data *hapd, const u8 *addr,
549                            const u8 *mask)
550 {
551         if (hapd->driver == NULL || hapd->driver->valid_bss_mask == NULL)
552                 return 1;
553         return hapd->driver->valid_bss_mask(hapd->drv_priv, addr, mask);
554 }
555
556
557 struct hostapd_hw_modes *
558 hostapd_get_hw_feature_data(struct hostapd_data *hapd, u16 *num_modes,
559                             u16 *flags)
560 {
561         if (hapd->driver == NULL ||
562             hapd->driver->get_hw_feature_data == NULL)
563                 return NULL;
564         return hapd->driver->get_hw_feature_data(hapd->drv_priv, num_modes,
565                                                  flags);
566 }
567
568
569 int hostapd_driver_commit(struct hostapd_data *hapd)
570 {
571         if (hapd->driver == NULL || hapd->driver->commit == NULL)
572                 return 0;
573         return hapd->driver->commit(hapd->drv_priv);
574 }
575
576
577 int hostapd_set_ht_params(const char *ifname, struct hostapd_data *hapd,
578                           const u8 *ht_capab, size_t ht_capab_len,
579                           const u8 *ht_oper, size_t ht_oper_len)
580 {
581         if (hapd->driver == NULL || hapd->driver->set_ht_params == NULL ||
582             ht_capab == NULL || ht_oper == NULL)
583                 return 0;
584         return hapd->driver->set_ht_params(
585                 ifname, hapd->drv_priv, ht_capab, ht_capab_len,
586                 ht_oper, ht_oper_len);
587 }
588
589
590 int hostapd_drv_none(struct hostapd_data *hapd)
591 {
592         return hapd->driver && os_strcmp(hapd->driver->name, "none") == 0;
593 }
594
595
596 int hostapd_driver_scan(struct hostapd_data *hapd,
597                         struct wpa_driver_scan_params *params)
598 {
599         if (hapd->driver && hapd->driver->scan2)
600                 return hapd->driver->scan2(hapd->drv_priv, params);
601         return -1;
602 }
603
604
605 struct wpa_scan_results * hostapd_driver_get_scan_results(
606         struct hostapd_data *hapd)
607 {
608         if (hapd->driver && hapd->driver->get_scan_results2)
609                 return hapd->driver->get_scan_results2(hapd->drv_priv);
610         return NULL;
611 }