Move WPA authenticator glue code into a separate file
[libeap.git] / hostapd / 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 "includes.h"
16
17 #include "common.h"
18 #include "ap/hostapd.h"
19 #include "ap/ieee802_11.h"
20 #include "ap/sta_info.h"
21 #include "driver_i.h"
22
23
24 static int hostapd_sta_flags_to_drv(int flags)
25 {
26         int res = 0;
27         if (flags & WLAN_STA_AUTHORIZED)
28                 res |= WPA_STA_AUTHORIZED;
29         if (flags & WLAN_STA_WMM)
30                 res |= WPA_STA_WMM;
31         if (flags & WLAN_STA_SHORT_PREAMBLE)
32                 res |= WPA_STA_SHORT_PREAMBLE;
33         if (flags & WLAN_STA_MFP)
34                 res |= WPA_STA_MFP;
35         return res;
36 }
37
38
39 static int hostapd_set_ap_wps_ie(struct hostapd_data *hapd,
40                                  const struct wpabuf *beacon,
41                                  const struct wpabuf *proberesp)
42 {
43         if (hapd->driver == NULL || hapd->driver->set_ap_wps_ie == NULL)
44                 return 0;
45         return hapd->driver->set_ap_wps_ie(hapd->conf->iface, hapd->drv_priv,
46                                            beacon, proberesp);
47 }
48
49
50 static int hostapd_send_mgmt_frame(struct hostapd_data *hapd, const void *msg,
51                            size_t len)
52 {
53         if (hapd->driver == NULL || hapd->driver->send_mlme == NULL)
54                 return 0;
55         return hapd->driver->send_mlme(hapd->drv_priv, msg, len);
56 }
57
58
59 static int hostapd_send_eapol(struct hostapd_data *hapd, const u8 *addr,
60                               const u8 *data, size_t data_len, int encrypt)
61 {
62         if (hapd->driver == NULL || hapd->driver->hapd_send_eapol == NULL)
63                 return 0;
64         return hapd->driver->hapd_send_eapol(hapd->drv_priv, addr, data,
65                                              data_len, encrypt,
66                                              hapd->own_addr);
67 }
68
69
70 static int hostapd_set_authorized(struct hostapd_data *hapd,
71                                   struct sta_info *sta, int authorized)
72 {
73         if (authorized) {
74                 return hostapd_sta_set_flags(hapd, sta->addr,
75                                              hostapd_sta_flags_to_drv(
76                                                      sta->flags),
77                                              WPA_STA_AUTHORIZED, ~0);
78         }
79
80         return hostapd_sta_set_flags(hapd, sta->addr,
81                                      hostapd_sta_flags_to_drv(sta->flags),
82                                      0, ~WPA_STA_AUTHORIZED);
83 }
84
85
86 static int hostapd_set_key(const char *ifname, struct hostapd_data *hapd,
87                            wpa_alg alg, const u8 *addr, int key_idx,
88                            int set_tx, const u8 *seq, size_t seq_len,
89                            const u8 *key, size_t key_len)
90 {
91         if (hapd->driver == NULL || hapd->driver->set_key == NULL)
92                 return 0;
93         return hapd->driver->set_key(ifname, hapd->drv_priv, alg, addr,
94                                      key_idx, set_tx, seq, seq_len, key,
95                                      key_len);
96 }
97
98
99 static int hostapd_read_sta_data(struct hostapd_data *hapd,
100                                  struct hostap_sta_driver_data *data,
101                                  const u8 *addr)
102 {
103         if (hapd->driver == NULL || hapd->driver->read_sta_data == NULL)
104                 return -1;
105         return hapd->driver->read_sta_data(hapd->drv_priv, data, addr);
106 }
107
108
109 static int hostapd_sta_clear_stats(struct hostapd_data *hapd, const u8 *addr)
110 {
111         if (hapd->driver == NULL || hapd->driver->sta_clear_stats == NULL)
112                 return 0;
113         return hapd->driver->sta_clear_stats(hapd->drv_priv, addr);
114 }
115
116
117 static int hostapd_set_sta_flags(struct hostapd_data *hapd,
118                                  struct sta_info *sta)
119 {
120         int set_flags, total_flags, flags_and, flags_or;
121         total_flags = hostapd_sta_flags_to_drv(sta->flags);
122         set_flags = WPA_STA_SHORT_PREAMBLE | WPA_STA_WMM | WPA_STA_MFP;
123         if (!hapd->conf->ieee802_1x && !hapd->conf->wpa &&
124             sta->flags & WLAN_STA_AUTHORIZED)
125                 set_flags |= WPA_STA_AUTHORIZED;
126         flags_or = total_flags & set_flags;
127         flags_and = total_flags | ~set_flags;
128         return hostapd_sta_set_flags(hapd, sta->addr, total_flags,
129                                      flags_or, flags_and);
130 }
131
132
133 static int hostapd_set_drv_ieee8021x(struct hostapd_data *hapd,
134                                      const char *ifname, int enabled)
135 {
136         struct wpa_bss_params params;
137         os_memset(&params, 0, sizeof(params));
138         params.ifname = ifname;
139         params.enabled = enabled;
140         if (enabled) {
141                 params.wpa = hapd->conf->wpa;
142                 params.ieee802_1x = hapd->conf->ieee802_1x;
143                 params.wpa_group = hapd->conf->wpa_group;
144                 params.wpa_pairwise = hapd->conf->wpa_pairwise;
145                 params.wpa_key_mgmt = hapd->conf->wpa_key_mgmt;
146                 params.rsn_preauth = hapd->conf->rsn_preauth;
147         }
148         return hostapd_set_ieee8021x(hapd, &params);
149 }
150
151
152 static int hostapd_set_radius_acl_auth(struct hostapd_data *hapd,
153                                        const u8 *mac, int accepted,
154                                        u32 session_timeout)
155 {
156         if (hapd->driver == NULL || hapd->driver->set_radius_acl_auth == NULL)
157                 return 0;
158         return hapd->driver->set_radius_acl_auth(hapd->drv_priv, mac, accepted,
159                                                  session_timeout);
160 }
161
162
163 static int hostapd_set_radius_acl_expire(struct hostapd_data *hapd,
164                                          const u8 *mac)
165 {
166         if (hapd->driver == NULL ||
167             hapd->driver->set_radius_acl_expire == NULL)
168                 return 0;
169         return hapd->driver->set_radius_acl_expire(hapd->drv_priv, mac);
170 }
171
172
173 static int hostapd_set_bss_params(struct hostapd_data *hapd,
174                                   int use_protection)
175 {
176         int ret = 0;
177         int preamble;
178 #ifdef CONFIG_IEEE80211N
179         u8 buf[60], *ht_capab, *ht_oper, *pos;
180
181         pos = buf;
182         ht_capab = pos;
183         pos = hostapd_eid_ht_capabilities(hapd, pos);
184         ht_oper = pos;
185         pos = hostapd_eid_ht_operation(hapd, pos);
186         if (pos > ht_oper && ht_oper > ht_capab &&
187             hostapd_set_ht_params(hapd->conf->iface, hapd,
188                                   ht_capab + 2, ht_capab[1],
189                                   ht_oper + 2, ht_oper[1])) {
190                 wpa_printf(MSG_ERROR, "Could not set HT capabilities "
191                            "for kernel driver");
192                 ret = -1;
193         }
194
195 #endif /* CONFIG_IEEE80211N */
196
197         if (hostapd_set_cts_protect(hapd, use_protection)) {
198                 wpa_printf(MSG_ERROR, "Failed to set CTS protect in kernel "
199                            "driver");
200                 ret = -1;
201         }
202
203         if (hapd->iface->current_mode &&
204             hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
205             hostapd_set_short_slot_time(hapd,
206                                         hapd->iface->num_sta_no_short_slot_time
207                                         > 0 ? 0 : 1)) {
208                 wpa_printf(MSG_ERROR, "Failed to set Short Slot Time option "
209                            "in kernel driver");
210                 ret = -1;
211         }
212
213         if (hapd->iface->num_sta_no_short_preamble == 0 &&
214             hapd->iconf->preamble == SHORT_PREAMBLE)
215                 preamble = SHORT_PREAMBLE;
216         else
217                 preamble = LONG_PREAMBLE;
218         if (hostapd_set_preamble(hapd, preamble)) {
219                 wpa_printf(MSG_ERROR, "Could not set preamble for kernel "
220                            "driver");
221                 ret = -1;
222         }
223
224         return ret;
225 }
226
227
228 static int hostapd_set_beacon(const char *ifname, struct hostapd_data *hapd,
229                               const u8 *head, size_t head_len,
230                               const u8 *tail, size_t tail_len, int dtim_period,
231                               int beacon_int)
232 {
233         if (hapd->driver == NULL || hapd->driver->set_beacon == NULL)
234                 return 0;
235         return hapd->driver->set_beacon(ifname, hapd->drv_priv,
236                                         head, head_len, tail, tail_len,
237                                         dtim_period, beacon_int);
238 }
239
240
241 static int hostapd_vlan_if_add(struct hostapd_data *hapd, const char *ifname)
242 {
243         return hostapd_if_add(hapd, WPA_IF_AP_VLAN, ifname, NULL, NULL);
244 }
245
246 static int hostapd_vlan_if_remove(struct hostapd_data *hapd,
247                                   const char *ifname)
248 {
249         return hostapd_if_remove(hapd, WPA_IF_AP_VLAN, ifname);
250 }
251
252
253 static int hostapd_set_wds_sta(struct hostapd_data *hapd, const u8 *addr,
254                                int aid, int val)
255 {
256         if (hapd->driver == NULL || hapd->driver->set_wds_sta == NULL)
257                 return 0;
258         return hapd->driver->set_wds_sta(hapd->drv_priv, addr, aid, val);
259 }
260
261
262 static int hostapd_set_sta_vlan(const char *ifname, struct hostapd_data *hapd,
263                                 const u8 *addr, int vlan_id)
264 {
265         if (hapd->driver == NULL || hapd->driver->set_sta_vlan == NULL)
266                 return 0;
267         return hapd->driver->set_sta_vlan(hapd->drv_priv, addr, ifname,
268                                           vlan_id);
269 }
270
271
272 static int hostapd_get_inact_sec(struct hostapd_data *hapd, const u8 *addr)
273 {
274         if (hapd->driver == NULL || hapd->driver->get_inact_sec == NULL)
275                 return 0;
276         return hapd->driver->get_inact_sec(hapd->drv_priv, addr);
277 }
278
279
280 static int hostapd_sta_deauth(struct hostapd_data *hapd, const u8 *addr,
281                               int reason)
282 {
283         if (hapd->driver == NULL || hapd->driver->sta_deauth == NULL)
284                 return 0;
285         return hapd->driver->sta_deauth(hapd->drv_priv, hapd->own_addr, addr,
286                                         reason);
287 }
288
289
290 static int hostapd_sta_disassoc(struct hostapd_data *hapd, const u8 *addr,
291                                 int reason)
292 {
293         if (hapd->driver == NULL || hapd->driver->sta_disassoc == NULL)
294                 return 0;
295         return hapd->driver->sta_disassoc(hapd->drv_priv, hapd->own_addr, addr,
296                                           reason);
297 }
298
299
300 static int hostapd_sta_add(const char *ifname, 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 {
306         struct hostapd_sta_add_params params;
307
308         if (hapd->driver == NULL)
309                 return 0;
310         if (hapd->driver->sta_add == NULL)
311                 return 0;
312
313         os_memset(&params, 0, sizeof(params));
314         params.addr = addr;
315         params.aid = aid;
316         params.capability = capability;
317         params.supp_rates = supp_rates;
318         params.supp_rates_len = supp_rates_len;
319         params.listen_interval = listen_interval;
320         params.ht_capabilities = ht_capab;
321         return hapd->driver->sta_add(ifname, hapd->drv_priv, &params);
322 }
323
324
325 static int hostapd_sta_remove(struct hostapd_data *hapd, const u8 *addr)
326 {
327         if (hapd->driver == NULL || hapd->driver->sta_remove == NULL)
328                 return 0;
329         return hapd->driver->sta_remove(hapd->drv_priv, addr);
330 }
331
332
333 static int hostapd_set_countermeasures(struct hostapd_data *hapd, int enabled)
334 {
335         if (hapd->driver == NULL ||
336             hapd->driver->hapd_set_countermeasures == NULL)
337                 return 0;
338         return hapd->driver->hapd_set_countermeasures(hapd->drv_priv, enabled);
339 }
340
341
342 void hostapd_set_driver_ops(struct hostapd_driver_ops *ops)
343 {
344         ops->set_ap_wps_ie = hostapd_set_ap_wps_ie;
345         ops->send_mgmt_frame = hostapd_send_mgmt_frame;
346         ops->send_eapol = hostapd_send_eapol;
347         ops->set_authorized = hostapd_set_authorized;
348         ops->set_key = hostapd_set_key;
349         ops->read_sta_data = hostapd_read_sta_data;
350         ops->sta_clear_stats = hostapd_sta_clear_stats;
351         ops->set_sta_flags = hostapd_set_sta_flags;
352         ops->set_drv_ieee8021x = hostapd_set_drv_ieee8021x;
353         ops->set_radius_acl_auth = hostapd_set_radius_acl_auth;
354         ops->set_radius_acl_expire = hostapd_set_radius_acl_expire;
355         ops->set_bss_params = hostapd_set_bss_params;
356         ops->set_beacon = hostapd_set_beacon;
357         ops->vlan_if_add = hostapd_vlan_if_add;
358         ops->vlan_if_remove = hostapd_vlan_if_remove;
359         ops->set_wds_sta = hostapd_set_wds_sta;
360         ops->set_sta_vlan = hostapd_set_sta_vlan;
361         ops->get_inact_sec = hostapd_get_inact_sec;
362         ops->sta_deauth = hostapd_sta_deauth;
363         ops->sta_disassoc = hostapd_sta_disassoc;
364         ops->sta_add = hostapd_sta_add;
365         ops->sta_remove = hostapd_sta_remove;
366         ops->set_countermeasures = hostapd_set_countermeasures;
367 }