Merge hostapd driver init functions into one
[wpasupplicant] / hostapd / driver_i.h
1 /*
2  * hostapd - internal driver interface wrappers
3  * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
4  * Copyright (c) 2007-2008, Intel Corporation
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * Alternatively, this software may be distributed under the terms of BSD
11  * license.
12  *
13  * See README and COPYING for more details.
14  */
15
16 #ifndef DRIVER_I_H
17 #define DRIVER_I_H
18
19 #include "drivers/driver.h"
20 #include "config.h"
21
22 static inline void *
23 hostapd_driver_init(struct hostapd_data *hapd, const u8 *bssid)
24 {
25         struct wpa_init_params params;
26         void *ret;
27         size_t i;
28
29         if (hapd->driver == NULL || hapd->driver->hapd_init == NULL)
30                 return NULL;
31
32         os_memset(&params, 0, sizeof(params));
33         params.bssid = bssid;
34         params.ifname = hapd->conf->iface;
35         params.ssid = (const u8 *) hapd->conf->ssid.ssid;
36         params.ssid_len = hapd->conf->ssid.ssid_len;
37         params.test_socket = hapd->conf->test_socket;
38         params.use_pae_group_addr = hapd->conf->use_pae_group_addr;
39         params.ht_40mhz_scan = hapd->iconf->secondary_channel != 0;
40
41         params.num_bridge = hapd->iface->num_bss;
42         params.bridge = os_zalloc(hapd->iface->num_bss * sizeof(char *));
43         if (params.bridge == NULL)
44                 return NULL;
45         for (i = 0; i < hapd->iface->num_bss; i++) {
46                 struct hostapd_data *bss = hapd->iface->bss[i];
47                 if (bss->conf->bridge[0])
48                         params.bridge[i] = bss->conf->bridge;
49         }
50         ret = hapd->driver->hapd_init(hapd, &params);
51         os_free(params.bridge);
52
53         return ret;
54 }
55
56 static inline void
57 hostapd_driver_deinit(struct hostapd_data *hapd)
58 {
59         if (hapd->driver == NULL || hapd->driver->hapd_deinit == NULL)
60                 return;
61         hapd->driver->hapd_deinit(hapd->drv_priv);
62 }
63
64 static inline int
65 hostapd_set_ieee8021x(const char *ifname, struct hostapd_data *hapd,
66                       int enabled)
67 {
68         if (hapd->driver == NULL || hapd->driver->set_ieee8021x == NULL)
69                 return 0;
70         return hapd->driver->set_ieee8021x(ifname, hapd->drv_priv, enabled);
71 }
72
73 static inline int
74 hostapd_set_privacy(struct hostapd_data *hapd, int enabled)
75 {
76         if (hapd->driver == NULL || hapd->driver->set_privacy == NULL)
77                 return 0;
78         return hapd->driver->set_privacy(hapd->conf->iface, hapd->drv_priv,
79                                          enabled);
80 }
81
82 static inline int
83 hostapd_set_key(const char *ifname, struct hostapd_data *hapd,
84                 wpa_alg alg, const u8 *addr, int key_idx,
85                 int set_tx, const u8 *seq, size_t seq_len,
86                 const u8 *key, size_t key_len)
87 {
88         if (hapd->driver == NULL || hapd->driver->hapd_set_key == NULL)
89                 return 0;
90         return hapd->driver->hapd_set_key(ifname, hapd->drv_priv, alg, addr,
91                                           key_idx, set_tx, seq, seq_len, key,
92                                           key_len);
93 }
94
95 static inline int
96 hostapd_get_seqnum(const char *ifname, struct hostapd_data *hapd,
97                    const u8 *addr, int idx, u8 *seq)
98 {
99         if (hapd->driver == NULL || hapd->driver->get_seqnum == NULL)
100                 return 0;
101         return hapd->driver->get_seqnum(ifname, hapd->drv_priv, addr, idx,
102                                         seq);
103 }
104
105 static inline int
106 hostapd_get_seqnum_igtk(const char *ifname, struct hostapd_data *hapd,
107                         const u8 *addr, int idx, u8 *seq)
108 {
109         if (hapd->driver == NULL || hapd->driver->get_seqnum_igtk == NULL)
110                 return -1;
111         return hapd->driver->get_seqnum_igtk(ifname, hapd->drv_priv, addr, idx,
112                                              seq);
113 }
114
115 static inline int
116 hostapd_flush(struct hostapd_data *hapd)
117 {
118         if (hapd->driver == NULL || hapd->driver->flush == NULL)
119                 return 0;
120         return hapd->driver->flush(hapd->drv_priv);
121 }
122
123 static inline int
124 hostapd_set_generic_elem(struct hostapd_data *hapd, const u8 *elem,
125                          size_t elem_len)
126 {
127         if (hapd->driver == NULL || hapd->driver->set_generic_elem == NULL)
128                 return 0;
129         return hapd->driver->set_generic_elem(hapd->conf->iface,
130                                               hapd->drv_priv, elem, elem_len);
131 }
132
133 static inline int
134 hostapd_read_sta_data(struct hostapd_data *hapd,
135                       struct hostap_sta_driver_data *data, const u8 *addr)
136 {
137         if (hapd->driver == NULL || hapd->driver->read_sta_data == NULL)
138                 return -1;
139         return hapd->driver->read_sta_data(hapd->drv_priv, data, addr);
140 }
141
142 static inline int
143 hostapd_send_eapol(struct hostapd_data *hapd, const u8 *addr, const u8 *data,
144                    size_t data_len, int encrypt)
145 {
146         if (hapd->driver == NULL || hapd->driver->hapd_send_eapol == NULL)
147                 return 0;
148         return hapd->driver->hapd_send_eapol(hapd->drv_priv, addr, data,
149                                              data_len, encrypt,
150                                              hapd->own_addr);
151 }
152
153 static inline int
154 hostapd_sta_deauth(struct hostapd_data *hapd, const u8 *addr, int reason)
155 {
156         if (hapd->driver == NULL || hapd->driver->sta_deauth == NULL)
157                 return 0;
158         return hapd->driver->sta_deauth(hapd->drv_priv, addr, reason);
159 }
160
161 static inline int
162 hostapd_sta_disassoc(struct hostapd_data *hapd, const u8 *addr, int reason)
163 {
164         if (hapd->driver == NULL || hapd->driver->sta_disassoc == NULL)
165                 return 0;
166         return hapd->driver->sta_disassoc(hapd->drv_priv, addr, reason);
167 }
168
169 static inline int
170 hostapd_sta_remove(struct hostapd_data *hapd, const u8 *addr)
171 {
172         if (hapd->driver == NULL || hapd->driver->sta_remove == NULL)
173                 return 0;
174         return hapd->driver->sta_remove(hapd->drv_priv, addr);
175 }
176
177 static inline int
178 hostapd_get_ssid(struct hostapd_data *hapd, u8 *buf, size_t len)
179 {
180         if (hapd->driver == NULL || hapd->driver->hapd_get_ssid == NULL)
181                 return 0;
182         return hapd->driver->hapd_get_ssid(hapd->conf->iface, hapd->drv_priv,
183                                            buf, len);
184 }
185
186 static inline int
187 hostapd_set_ssid(struct hostapd_data *hapd, const u8 *buf, size_t len)
188 {
189         if (hapd->driver == NULL || hapd->driver->hapd_set_ssid == NULL)
190                 return 0;
191         return hapd->driver->hapd_set_ssid(hapd->conf->iface, hapd->drv_priv,
192                                            buf, len);
193 }
194
195 static inline int
196 hostapd_send_mgmt_frame(struct hostapd_data *hapd, const void *msg, size_t len)
197 {
198         if (hapd->driver == NULL || hapd->driver->send_mlme == NULL)
199                 return 0;
200         return hapd->driver->send_mlme(hapd->drv_priv, msg, len);
201 }
202
203 static inline int
204 hostapd_set_countermeasures(struct hostapd_data *hapd, int enabled)
205 {
206         if (hapd->driver == NULL ||
207             hapd->driver->hapd_set_countermeasures == NULL)
208                 return 0;
209         return hapd->driver->hapd_set_countermeasures(hapd->drv_priv, enabled);
210 }
211
212 static inline int
213 hostapd_sta_add(const char *ifname, struct hostapd_data *hapd, const u8 *addr,
214                 u16 aid, u16 capability, const u8 *supp_rates,
215                 size_t supp_rates_len, int flags, u16 listen_interval,
216                 const struct ht_cap_ie *ht_capabilities)
217 {
218         struct hostapd_sta_add_params params;
219
220         if (hapd->driver == NULL)
221                 return 0;
222         if (hapd->driver->sta_add == NULL)
223                 return 0;
224
225         os_memset(&params, 0, sizeof(params));
226         params.addr = addr;
227         params.aid = aid;
228         params.capability = capability;
229         params.supp_rates = supp_rates;
230         params.supp_rates_len = supp_rates_len;
231         params.flags = flags;
232         params.listen_interval = listen_interval;
233         params.ht_capabilities = ht_capabilities;
234         return hapd->driver->sta_add(ifname, hapd->drv_priv, &params);
235 }
236
237 static inline int
238 hostapd_get_inact_sec(struct hostapd_data *hapd, const u8 *addr)
239 {
240         if (hapd->driver == NULL || hapd->driver->get_inact_sec == NULL)
241                 return 0;
242         return hapd->driver->get_inact_sec(hapd->drv_priv, addr);
243 }
244
245 static inline int
246 hostapd_set_freq(struct hostapd_data *hapd, int mode, int freq, int channel,
247                  int ht_enabled, int sec_channel_offset)
248 {
249         struct hostapd_freq_params data;
250         if (hapd->driver == NULL)
251                 return 0;
252         if (hapd->driver->set_freq == NULL)
253                 return 0;
254         os_memset(&data, 0, sizeof(data));
255         data.mode = mode;
256         data.freq = freq;
257         data.channel = channel;
258         data.ht_enabled = ht_enabled;
259         data.sec_channel_offset = sec_channel_offset;
260         return hapd->driver->set_freq(hapd->drv_priv, &data);
261 }
262
263 static inline int
264 hostapd_set_rts(struct hostapd_data *hapd, int rts)
265 {
266         if (hapd->driver == NULL || hapd->driver->set_rts == NULL)
267                 return 0;
268         return hapd->driver->set_rts(hapd->drv_priv, rts);
269 }
270
271 static inline int
272 hostapd_set_frag(struct hostapd_data *hapd, int frag)
273 {
274         if (hapd->driver == NULL || hapd->driver->set_frag == NULL)
275                 return 0;
276         return hapd->driver->set_frag(hapd->drv_priv, frag);
277 }
278
279 static inline int
280 hostapd_set_retry(struct hostapd_data *hapd, int short_retry, int long_retry)
281 {
282         if (hapd->driver == NULL || hapd->driver->set_retry == NULL)
283                 return 0;
284         return hapd->driver->set_retry(hapd->drv_priv, short_retry,
285                                        long_retry);
286 }
287
288 static inline int
289 hostapd_sta_set_flags(struct hostapd_data *hapd, u8 *addr,
290                       int total_flags, int flags_or, int flags_and)
291 {
292         if (hapd->driver == NULL || hapd->driver->sta_set_flags == NULL)
293                 return 0;
294         return hapd->driver->sta_set_flags(hapd->drv_priv, addr, total_flags,
295                                            flags_or, flags_and);
296 }
297
298 static inline int
299 hostapd_set_rate_sets(struct hostapd_data *hapd, int *supp_rates,
300                       int *basic_rates, int mode)
301 {
302         if (hapd->driver == NULL || hapd->driver->set_rate_sets == NULL)
303                 return 0;
304         return hapd->driver->set_rate_sets(hapd->drv_priv, supp_rates,
305                                            basic_rates, mode);
306 }
307
308 static inline int
309 hostapd_set_country(struct hostapd_data *hapd, const char *country)
310 {
311         if (hapd->driver == NULL ||
312             hapd->driver->set_country == NULL)
313                 return 0;
314         return hapd->driver->set_country(hapd->drv_priv, country);
315 }
316
317 static inline int
318 hostapd_set_ieee80211d(struct hostapd_data *hapd, int enabled)
319 {
320         if (hapd->driver == NULL ||
321             hapd->driver->set_ieee80211d == NULL)
322                 return 0;
323         return hapd->driver->set_ieee80211d(hapd->drv_priv, enabled);
324 }
325
326 static inline int
327 hostapd_sta_clear_stats(struct hostapd_data *hapd, const u8 *addr)
328 {
329         if (hapd->driver == NULL || hapd->driver->sta_clear_stats == NULL)
330                 return 0;
331         return hapd->driver->sta_clear_stats(hapd->drv_priv, addr);
332 }
333
334 static inline int
335 hostapd_set_beacon(const char *ifname, struct hostapd_data *hapd,
336                    const u8 *head, size_t head_len,
337                    const u8 *tail, size_t tail_len, int dtim_period)
338 {
339         if (hapd->driver == NULL || hapd->driver->hapd_set_beacon == NULL)
340                 return 0;
341         return hapd->driver->hapd_set_beacon(ifname, hapd->drv_priv,
342                                              head, head_len,
343                                              tail, tail_len, dtim_period);
344 }
345
346 static inline int
347 hostapd_set_internal_bridge(struct hostapd_data *hapd, int value)
348 {
349         if (hapd->driver == NULL || hapd->driver->set_internal_bridge == NULL)
350                 return 0;
351         return hapd->driver->set_internal_bridge(hapd->drv_priv, value);
352 }
353
354 static inline int
355 hostapd_set_beacon_int(struct hostapd_data *hapd, int value)
356 {
357         if (hapd->driver == NULL || hapd->driver->hapd_set_beacon_int == NULL)
358                 return 0;
359         return hapd->driver->hapd_set_beacon_int(hapd->drv_priv, value);
360 }
361
362 static inline int
363 hostapd_set_broadcast_ssid(struct hostapd_data *hapd, int value)
364 {
365         if (hapd->driver == NULL || hapd->driver->set_broadcast_ssid == NULL)
366                 return 0;
367         return hapd->driver->set_broadcast_ssid(hapd->drv_priv, value);
368 }
369
370 static inline int
371 hostapd_set_cts_protect(struct hostapd_data *hapd, int value)
372 {
373         if (hapd->driver == NULL || hapd->driver->set_cts_protect == NULL)
374                 return 0;
375         return hapd->driver->set_cts_protect(hapd->drv_priv, value);
376 }
377
378 static inline int
379 hostapd_set_preamble(struct hostapd_data *hapd, int value)
380 {
381         if (hapd->driver == NULL || hapd->driver->set_preamble == NULL)
382                 return 0;
383         return hapd->driver->set_preamble(hapd->drv_priv, value);
384 }
385
386 static inline int
387 hostapd_set_short_slot_time(struct hostapd_data *hapd, int value)
388 {
389         if (hapd->driver == NULL || hapd->driver->set_short_slot_time == NULL)
390                 return 0;
391         return hapd->driver->set_short_slot_time(hapd->drv_priv, value);
392 }
393
394 static inline int
395 hostapd_set_tx_queue_params(struct hostapd_data *hapd, int queue, int aifs,
396                             int cw_min, int cw_max, int burst_time)
397 {
398         if (hapd->driver == NULL || hapd->driver->set_tx_queue_params == NULL)
399                 return 0;
400         return hapd->driver->set_tx_queue_params(hapd->drv_priv, queue, aifs,
401                                                  cw_min, cw_max, burst_time);
402 }
403
404 static inline int
405 hostapd_bss_add(struct hostapd_data *hapd, const char *ifname, const u8 *bssid)
406 {
407         if (hapd->driver == NULL || hapd->driver->bss_add == NULL)
408                 return 0;
409         return hapd->driver->bss_add(hapd->drv_priv, ifname, bssid);
410 }
411
412 static inline int
413 hostapd_bss_remove(struct hostapd_data *hapd, const char *ifname)
414 {
415         if (hapd->driver == NULL || hapd->driver->bss_remove == NULL)
416                 return 0;
417         return hapd->driver->bss_remove(hapd->drv_priv, ifname);
418 }
419
420 static inline int
421 hostapd_valid_bss_mask(struct hostapd_data *hapd, const u8 *addr,
422                        const u8 *mask)
423 {
424         if (hapd->driver == NULL || hapd->driver->valid_bss_mask == NULL)
425                 return 1;
426         return hapd->driver->valid_bss_mask(hapd->drv_priv, addr, mask);
427 }
428
429 static inline int
430 hostapd_if_add(struct hostapd_data *hapd, enum hostapd_driver_if_type type,
431                char *ifname, const u8 *addr)
432 {
433         if (hapd->driver == NULL || hapd->driver->if_add == NULL)
434                 return -1;
435         return hapd->driver->if_add(hapd->conf->iface, hapd->drv_priv, type,
436                                     ifname, addr);
437 }
438
439 static inline int
440 hostapd_if_update(struct hostapd_data *hapd, enum hostapd_driver_if_type type,
441                   char *ifname, const u8 *addr)
442 {
443         if (hapd->driver == NULL || hapd->driver->if_update == NULL)
444                 return -1;
445         return hapd->driver->if_update(hapd->drv_priv, type, ifname, addr);
446 }
447
448 static inline int
449 hostapd_if_remove(struct hostapd_data *hapd, enum hostapd_driver_if_type type,
450                   char *ifname, const u8 *addr)
451 {
452         if (hapd->driver == NULL || hapd->driver->if_remove == NULL)
453                 return -1;
454         return hapd->driver->if_remove(hapd->drv_priv, type, ifname, addr);
455 }
456
457 static inline int
458 hostapd_passive_scan(struct hostapd_data *hapd, int now, int our_mode_only,
459                      int interval, int _listen, int *channel,
460                      int *last_rx)
461 {
462         if (hapd->driver == NULL || hapd->driver->passive_scan == NULL)
463                 return -1;
464         return hapd->driver->passive_scan(hapd->drv_priv, now, our_mode_only,
465                                           interval, _listen, channel, last_rx);
466 }
467
468 static inline struct hostapd_hw_modes *
469 hostapd_get_hw_feature_data(struct hostapd_data *hapd, u16 *num_modes,
470                             u16 *flags)
471 {
472         if (hapd->driver == NULL ||
473             hapd->driver->get_hw_feature_data == NULL)
474                 return NULL;
475         return hapd->driver->get_hw_feature_data(hapd->drv_priv, num_modes,
476                                                  flags);
477 }
478
479 static inline int
480 hostapd_set_sta_vlan(const char *ifname, struct hostapd_data *hapd,
481                      const u8 *addr, int vlan_id)
482 {
483         if (hapd->driver == NULL || hapd->driver->set_sta_vlan == NULL)
484                 return 0;
485         return hapd->driver->set_sta_vlan(hapd->drv_priv, addr, ifname, vlan_id);
486 }
487
488 static inline int
489 hostapd_driver_commit(struct hostapd_data *hapd)
490 {
491         if (hapd->driver == NULL || hapd->driver->commit == NULL)
492                 return 0;
493         return hapd->driver->commit(hapd->drv_priv);
494 }
495
496 static inline int
497 hostapd_set_radius_acl_auth(struct hostapd_data *hapd, const u8 *mac,
498                             int accepted, u32 session_timeout)
499 {
500         if (hapd->driver == NULL || hapd->driver->set_radius_acl_auth == NULL)
501                 return 0;
502         return hapd->driver->set_radius_acl_auth(hapd->drv_priv, mac, accepted,
503                                                  session_timeout);
504 }
505
506 static inline int
507 hostapd_set_radius_acl_expire(struct hostapd_data *hapd, const u8 *mac)
508 {
509         if (hapd->driver == NULL ||
510             hapd->driver->set_radius_acl_expire == NULL)
511                 return 0;
512         return hapd->driver->set_radius_acl_expire(hapd->drv_priv, mac);
513 }
514
515 #ifdef CONFIG_IEEE80211N
516 static inline int
517 hostapd_set_ht_params(const char *ifname, struct hostapd_data *hapd,
518                       const u8 *ht_capab, size_t ht_capab_len,
519                       const u8 *ht_oper, size_t ht_oper_len)
520 {
521         if (hapd->driver == NULL || hapd->driver->set_ht_params == NULL ||
522             ht_capab == NULL || ht_oper == NULL)
523                 return 0;
524         return hapd->driver->set_ht_params(
525                 ifname, hapd->drv_priv, ht_capab, ht_capab_len,
526                 ht_oper, ht_oper_len);
527 }
528 #endif /* CONFIG_IEEE80211N */
529
530 static inline int
531 hostapd_drv_none(struct hostapd_data *hapd)
532 {
533         return hapd->driver && os_strcmp(hapd->driver->name, "none") == 0;
534 }
535
536 static inline int
537 hostapd_set_wps_beacon_ie(struct hostapd_data *hapd, const u8 *ie, size_t len)
538 {
539         if (hapd->driver == NULL || hapd->driver->set_wps_beacon_ie == NULL)
540                 return 0;
541         return hapd->driver->set_wps_beacon_ie(hapd->conf->iface,
542                                                hapd->drv_priv, ie, len);
543 }
544
545 static inline int
546 hostapd_set_wps_probe_resp_ie(struct hostapd_data *hapd, const u8 *ie,
547                               size_t len)
548 {
549         if (hapd->driver == NULL ||
550             hapd->driver->set_wps_probe_resp_ie == NULL)
551                 return 0;
552         return hapd->driver->set_wps_probe_resp_ie(hapd->conf->iface,
553                                                    hapd->drv_priv, ie, len);
554 }
555
556 static inline const struct hostapd_neighbor_bss *
557 hostapd_driver_get_neighbor_bss(struct hostapd_data *hapd, size_t *num)
558 {
559         if (hapd->driver == NULL || hapd->driver->get_neighbor_bss == NULL)
560                 return NULL;
561         return hapd->driver->get_neighbor_bss(hapd->drv_priv, num);
562 }
563
564 #endif /* DRIVER_I_H */