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