OSDN Git Service

Accumulative patch from commit 8fd0f0f323a922aa88ec720ee524f7105d3b0f64
[android-x86/external-wpa_supplicant_8.git] / src / ap / wpa_auth_glue.c
1 /*
2  * hostapd / WPA authenticator glue code
3  * Copyright (c) 2002-2011, 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 "common/ieee802_11_defs.h"
19 #include "eapol_auth/eapol_auth_sm.h"
20 #include "eapol_auth/eapol_auth_sm_i.h"
21 #include "eap_server/eap.h"
22 #include "l2_packet/l2_packet.h"
23 #include "drivers/driver.h"
24 #include "hostapd.h"
25 #include "ieee802_1x.h"
26 #include "preauth_auth.h"
27 #include "sta_info.h"
28 #include "tkip_countermeasures.h"
29 #include "ap_drv_ops.h"
30 #include "ap_config.h"
31 #include "wpa_auth.h"
32
33
34 static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
35                                   struct wpa_auth_config *wconf)
36 {
37         wconf->wpa = conf->wpa;
38         wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
39         wconf->wpa_pairwise = conf->wpa_pairwise;
40         wconf->wpa_group = conf->wpa_group;
41         wconf->wpa_group_rekey = conf->wpa_group_rekey;
42         wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
43         wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
44         wconf->wpa_ptk_rekey = conf->wpa_ptk_rekey;
45         wconf->rsn_pairwise = conf->rsn_pairwise;
46         wconf->rsn_preauth = conf->rsn_preauth;
47         wconf->eapol_version = conf->eapol_version;
48         wconf->peerkey = conf->peerkey;
49         wconf->wmm_enabled = conf->wmm_enabled;
50         wconf->wmm_uapsd = conf->wmm_uapsd;
51         wconf->disable_pmksa_caching = conf->disable_pmksa_caching;
52         wconf->okc = conf->okc;
53 #ifdef CONFIG_IEEE80211W
54         wconf->ieee80211w = conf->ieee80211w;
55 #endif /* CONFIG_IEEE80211W */
56 #ifdef CONFIG_IEEE80211R
57         wconf->ssid_len = conf->ssid.ssid_len;
58         if (wconf->ssid_len > SSID_LEN)
59                 wconf->ssid_len = SSID_LEN;
60         os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
61         os_memcpy(wconf->mobility_domain, conf->mobility_domain,
62                   MOBILITY_DOMAIN_ID_LEN);
63         if (conf->nas_identifier &&
64             os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
65                 wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
66                 os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
67                           wconf->r0_key_holder_len);
68         }
69         os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
70         wconf->r0_key_lifetime = conf->r0_key_lifetime;
71         wconf->reassociation_deadline = conf->reassociation_deadline;
72         wconf->r0kh_list = conf->r0kh_list;
73         wconf->r1kh_list = conf->r1kh_list;
74         wconf->pmk_r1_push = conf->pmk_r1_push;
75         wconf->ft_over_ds = conf->ft_over_ds;
76 #endif /* CONFIG_IEEE80211R */
77 }
78
79
80 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
81                                     logger_level level, const char *txt)
82 {
83 #ifndef CONFIG_NO_HOSTAPD_LOGGER
84         struct hostapd_data *hapd = ctx;
85         int hlevel;
86
87         switch (level) {
88         case LOGGER_WARNING:
89                 hlevel = HOSTAPD_LEVEL_WARNING;
90                 break;
91         case LOGGER_INFO:
92                 hlevel = HOSTAPD_LEVEL_INFO;
93                 break;
94         case LOGGER_DEBUG:
95         default:
96                 hlevel = HOSTAPD_LEVEL_DEBUG;
97                 break;
98         }
99
100         hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
101 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
102 }
103
104
105 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
106                                         u16 reason)
107 {
108         struct hostapd_data *hapd = ctx;
109         wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
110                    "STA " MACSTR " reason %d",
111                    __func__, MAC2STR(addr), reason);
112         ap_sta_disconnect(hapd, NULL, addr, reason);
113 }
114
115
116 static void hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
117 {
118         struct hostapd_data *hapd = ctx;
119         michael_mic_failure(hapd, addr, 0);
120 }
121
122
123 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
124                                        wpa_eapol_variable var, int value)
125 {
126         struct hostapd_data *hapd = ctx;
127         struct sta_info *sta = ap_get_sta(hapd, addr);
128         if (sta == NULL)
129                 return;
130         switch (var) {
131         case WPA_EAPOL_portEnabled:
132                 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
133                 break;
134         case WPA_EAPOL_portValid:
135                 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
136                 break;
137         case WPA_EAPOL_authorized:
138                 ieee802_1x_set_sta_authorized(hapd, sta, value);
139                 break;
140         case WPA_EAPOL_portControl_Auto:
141                 if (sta->eapol_sm)
142                         sta->eapol_sm->portControl = Auto;
143                 break;
144         case WPA_EAPOL_keyRun:
145                 if (sta->eapol_sm)
146                         sta->eapol_sm->keyRun = value ? TRUE : FALSE;
147                 break;
148         case WPA_EAPOL_keyAvailable:
149                 if (sta->eapol_sm)
150                         sta->eapol_sm->eap_if->eapKeyAvailable =
151                                 value ? TRUE : FALSE;
152                 break;
153         case WPA_EAPOL_keyDone:
154                 if (sta->eapol_sm)
155                         sta->eapol_sm->keyDone = value ? TRUE : FALSE;
156                 break;
157         case WPA_EAPOL_inc_EapolFramesTx:
158                 if (sta->eapol_sm)
159                         sta->eapol_sm->dot1xAuthEapolFramesTx++;
160                 break;
161         }
162 }
163
164
165 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
166                                       wpa_eapol_variable var)
167 {
168         struct hostapd_data *hapd = ctx;
169         struct sta_info *sta = ap_get_sta(hapd, addr);
170         if (sta == NULL || sta->eapol_sm == NULL)
171                 return -1;
172         switch (var) {
173         case WPA_EAPOL_keyRun:
174                 return sta->eapol_sm->keyRun;
175         case WPA_EAPOL_keyAvailable:
176                 return sta->eapol_sm->eap_if->eapKeyAvailable;
177         default:
178                 return -1;
179         }
180 }
181
182
183 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
184                                            const u8 *prev_psk)
185 {
186         struct hostapd_data *hapd = ctx;
187         return hostapd_get_psk(hapd->conf, addr, prev_psk);
188 }
189
190
191 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
192                                     size_t *len)
193 {
194         struct hostapd_data *hapd = ctx;
195         const u8 *key;
196         size_t keylen;
197         struct sta_info *sta;
198
199         sta = ap_get_sta(hapd, addr);
200         if (sta == NULL)
201                 return -1;
202
203         key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
204         if (key == NULL)
205                 return -1;
206
207         if (keylen > *len)
208                 keylen = *len;
209         os_memcpy(msk, key, keylen);
210         *len = keylen;
211
212         return 0;
213 }
214
215
216 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
217                                     const u8 *addr, int idx, u8 *key,
218                                     size_t key_len)
219 {
220         struct hostapd_data *hapd = ctx;
221         const char *ifname = hapd->conf->iface;
222
223         if (vlan_id > 0) {
224                 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
225                 if (ifname == NULL)
226                         return -1;
227         }
228
229         return hostapd_drv_set_key(ifname, hapd, alg, addr, idx, 1, NULL, 0,
230                                    key, key_len);
231 }
232
233
234 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
235                                        u8 *seq)
236 {
237         struct hostapd_data *hapd = ctx;
238         return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
239 }
240
241
242 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
243                                        const u8 *data, size_t data_len,
244                                        int encrypt)
245 {
246         struct hostapd_data *hapd = ctx;
247         struct sta_info *sta;
248         u32 flags = 0;
249
250         sta = ap_get_sta(hapd, addr);
251         if (sta)
252                 flags = hostapd_sta_flags_to_drv(sta->flags);
253
254         return hostapd_drv_hapd_send_eapol(hapd, addr, data, data_len,
255                                            encrypt, flags);
256 }
257
258
259 static int hostapd_wpa_auth_for_each_sta(
260         void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
261         void *cb_ctx)
262 {
263         struct hostapd_data *hapd = ctx;
264         struct sta_info *sta;
265
266         for (sta = hapd->sta_list; sta; sta = sta->next) {
267                 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
268                         return 1;
269         }
270         return 0;
271 }
272
273
274 struct wpa_auth_iface_iter_data {
275         int (*cb)(struct wpa_authenticator *sm, void *ctx);
276         void *cb_ctx;
277 };
278
279 static int wpa_auth_iface_iter(struct hostapd_iface *iface, void *ctx)
280 {
281         struct wpa_auth_iface_iter_data *data = ctx;
282         size_t i;
283         for (i = 0; i < iface->num_bss; i++) {
284                 if (iface->bss[i]->wpa_auth &&
285                     data->cb(iface->bss[i]->wpa_auth, data->cb_ctx))
286                         return 1;
287         }
288         return 0;
289 }
290
291
292 static int hostapd_wpa_auth_for_each_auth(
293         void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
294         void *cb_ctx)
295 {
296         struct hostapd_data *hapd = ctx;
297         struct wpa_auth_iface_iter_data data;
298         if (hapd->iface->for_each_interface == NULL)
299                 return -1;
300         data.cb = cb;
301         data.cb_ctx = cb_ctx;
302         return hapd->iface->for_each_interface(hapd->iface->interfaces,
303                                                wpa_auth_iface_iter, &data);
304 }
305
306
307 #ifdef CONFIG_IEEE80211R
308
309 struct wpa_auth_ft_iface_iter_data {
310         struct hostapd_data *src_hapd;
311         const u8 *dst;
312         const u8 *data;
313         size_t data_len;
314 };
315
316
317 static int hostapd_wpa_auth_ft_iter(struct hostapd_iface *iface, void *ctx)
318 {
319         struct wpa_auth_ft_iface_iter_data *idata = ctx;
320         struct hostapd_data *hapd;
321         size_t j;
322
323         for (j = 0; j < iface->num_bss; j++) {
324                 hapd = iface->bss[j];
325                 if (hapd == idata->src_hapd)
326                         continue;
327                 if (os_memcmp(hapd->own_addr, idata->dst, ETH_ALEN) == 0) {
328                         wpa_printf(MSG_DEBUG, "FT: Send RRB data directly to "
329                                    "locally managed BSS " MACSTR "@%s -> "
330                                    MACSTR "@%s",
331                                    MAC2STR(idata->src_hapd->own_addr),
332                                    idata->src_hapd->conf->iface,
333                                    MAC2STR(hapd->own_addr), hapd->conf->iface);
334                         wpa_ft_rrb_rx(hapd->wpa_auth,
335                                       idata->src_hapd->own_addr,
336                                       idata->data, idata->data_len);
337                         return 1;
338                 }
339         }
340
341         return 0;
342 }
343
344 #endif /* CONFIG_IEEE80211R */
345
346
347 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
348                                        const u8 *data, size_t data_len)
349 {
350         struct hostapd_data *hapd = ctx;
351         struct l2_ethhdr *buf;
352         int ret;
353
354 #ifdef CONFIG_IEEE80211R
355         if (proto == ETH_P_RRB && hapd->iface->for_each_interface) {
356                 int res;
357                 struct wpa_auth_ft_iface_iter_data idata;
358                 idata.src_hapd = hapd;
359                 idata.dst = dst;
360                 idata.data = data;
361                 idata.data_len = data_len;
362                 res = hapd->iface->for_each_interface(hapd->iface->interfaces,
363                                                       hostapd_wpa_auth_ft_iter,
364                                                       &idata);
365                 if (res == 1)
366                         return data_len;
367         }
368 #endif /* CONFIG_IEEE80211R */
369
370         if (hapd->driver && hapd->driver->send_ether)
371                 return hapd->driver->send_ether(hapd->drv_priv, dst,
372                                                 hapd->own_addr, proto,
373                                                 data, data_len);
374         if (hapd->l2 == NULL)
375                 return -1;
376
377         buf = os_malloc(sizeof(*buf) + data_len);
378         if (buf == NULL)
379                 return -1;
380         os_memcpy(buf->h_dest, dst, ETH_ALEN);
381         os_memcpy(buf->h_source, hapd->own_addr, ETH_ALEN);
382         buf->h_proto = host_to_be16(proto);
383         os_memcpy(buf + 1, data, data_len);
384         ret = l2_packet_send(hapd->l2, dst, proto, (u8 *) buf,
385                              sizeof(*buf) + data_len);
386         os_free(buf);
387         return -1;
388 }
389
390
391 #ifdef CONFIG_IEEE80211R
392
393 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
394                                            const u8 *data, size_t data_len)
395 {
396         struct hostapd_data *hapd = ctx;
397         int res;
398         struct ieee80211_mgmt *m;
399         size_t mlen;
400         struct sta_info *sta;
401
402         sta = ap_get_sta(hapd, dst);
403         if (sta == NULL || sta->wpa_sm == NULL)
404                 return -1;
405
406         m = os_zalloc(sizeof(*m) + data_len);
407         if (m == NULL)
408                 return -1;
409         mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
410         m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
411                                         WLAN_FC_STYPE_ACTION);
412         os_memcpy(m->da, dst, ETH_ALEN);
413         os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
414         os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
415         os_memcpy(&m->u, data, data_len);
416
417         res = hostapd_drv_send_mlme(hapd, (u8 *) m, mlen);
418         os_free(m);
419         return res;
420 }
421
422
423 static struct wpa_state_machine *
424 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
425 {
426         struct hostapd_data *hapd = ctx;
427         struct sta_info *sta;
428
429         sta = ap_sta_add(hapd, sta_addr);
430         if (sta == NULL)
431                 return NULL;
432         if (sta->wpa_sm) {
433                 sta->auth_alg = WLAN_AUTH_FT;
434                 return sta->wpa_sm;
435         }
436
437         sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
438         if (sta->wpa_sm == NULL) {
439                 ap_free_sta(hapd, sta);
440                 return NULL;
441         }
442         sta->auth_alg = WLAN_AUTH_FT;
443
444         return sta->wpa_sm;
445 }
446
447
448 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
449                                 size_t len)
450 {
451         struct hostapd_data *hapd = ctx;
452         struct l2_ethhdr *ethhdr;
453         if (len < sizeof(*ethhdr))
454                 return;
455         ethhdr = (struct l2_ethhdr *) buf;
456         wpa_printf(MSG_DEBUG, "FT: RRB received packet " MACSTR " -> "
457                    MACSTR, MAC2STR(ethhdr->h_source), MAC2STR(ethhdr->h_dest));
458         wpa_ft_rrb_rx(hapd->wpa_auth, ethhdr->h_source, buf + sizeof(*ethhdr),
459                       len - sizeof(*ethhdr));
460 }
461
462 #endif /* CONFIG_IEEE80211R */
463
464
465 int hostapd_setup_wpa(struct hostapd_data *hapd)
466 {
467         struct wpa_auth_config _conf;
468         struct wpa_auth_callbacks cb;
469         const u8 *wpa_ie;
470         size_t wpa_ie_len;
471
472         hostapd_wpa_auth_conf(hapd->conf, &_conf);
473         if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_EAPOL_TX_STATUS)
474                 _conf.tx_status = 1;
475         os_memset(&cb, 0, sizeof(cb));
476         cb.ctx = hapd;
477         cb.logger = hostapd_wpa_auth_logger;
478         cb.disconnect = hostapd_wpa_auth_disconnect;
479         cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
480         cb.set_eapol = hostapd_wpa_auth_set_eapol;
481         cb.get_eapol = hostapd_wpa_auth_get_eapol;
482         cb.get_psk = hostapd_wpa_auth_get_psk;
483         cb.get_msk = hostapd_wpa_auth_get_msk;
484         cb.set_key = hostapd_wpa_auth_set_key;
485         cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
486         cb.send_eapol = hostapd_wpa_auth_send_eapol;
487         cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
488         cb.for_each_auth = hostapd_wpa_auth_for_each_auth;
489         cb.send_ether = hostapd_wpa_auth_send_ether;
490 #ifdef CONFIG_IEEE80211R
491         cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
492         cb.add_sta = hostapd_wpa_auth_add_sta;
493 #endif /* CONFIG_IEEE80211R */
494         hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
495         if (hapd->wpa_auth == NULL) {
496                 wpa_printf(MSG_ERROR, "WPA initialization failed.");
497                 return -1;
498         }
499
500         if (hostapd_set_privacy(hapd, 1)) {
501                 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
502                            "for interface %s", hapd->conf->iface);
503                 return -1;
504         }
505
506         wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
507         if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
508                 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
509                            "the kernel driver.");
510                 return -1;
511         }
512
513         if (rsn_preauth_iface_init(hapd)) {
514                 wpa_printf(MSG_ERROR, "Initialization of RSN "
515                            "pre-authentication failed.");
516                 return -1;
517         }
518
519 #ifdef CONFIG_IEEE80211R
520         if (!hostapd_drv_none(hapd)) {
521                 hapd->l2 = l2_packet_init(hapd->conf->bridge[0] ?
522                                           hapd->conf->bridge :
523                                           hapd->conf->iface, NULL, ETH_P_RRB,
524                                           hostapd_rrb_receive, hapd, 1);
525                 if (hapd->l2 == NULL &&
526                     (hapd->driver == NULL ||
527                      hapd->driver->send_ether == NULL)) {
528                         wpa_printf(MSG_ERROR, "Failed to open l2_packet "
529                                    "interface");
530                         return -1;
531                 }
532         }
533 #endif /* CONFIG_IEEE80211R */
534
535         return 0;
536
537 }
538
539
540 void hostapd_reconfig_wpa(struct hostapd_data *hapd)
541 {
542         struct wpa_auth_config wpa_auth_conf;
543         hostapd_wpa_auth_conf(hapd->conf, &wpa_auth_conf);
544         wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
545 }
546
547
548 void hostapd_deinit_wpa(struct hostapd_data *hapd)
549 {
550         rsn_preauth_iface_deinit(hapd);
551         if (hapd->wpa_auth) {
552                 wpa_deinit(hapd->wpa_auth);
553                 hapd->wpa_auth = NULL;
554
555                 if (hostapd_set_privacy(hapd, 0)) {
556                         wpa_printf(MSG_DEBUG, "Could not disable "
557                                    "PrivacyInvoked for interface %s",
558                                    hapd->conf->iface);
559                 }
560
561                 if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
562                         wpa_printf(MSG_DEBUG, "Could not remove generic "
563                                    "information element from interface %s",
564                                    hapd->conf->iface);
565                 }
566         }
567         ieee802_1x_deinit(hapd);
568
569 #ifdef CONFIG_IEEE80211R
570         l2_packet_deinit(hapd->l2);
571 #endif /* CONFIG_IEEE80211R */
572 }