hostapd: Update vendor branch to 0.6.10
[dragonfly.git] / contrib / hostapd / hostapd / preauth.c
1 /*
2  * hostapd - Authenticator for IEEE 802.11i RSN pre-authentication
3  * Copyright (c) 2004-2007, 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 #ifdef CONFIG_RSN_PREAUTH
18
19 #include "hostapd.h"
20 #include "l2_packet/l2_packet.h"
21 #include "ieee802_1x.h"
22 #include "eloop.h"
23 #include "sta_info.h"
24 #include "wpa_common.h"
25 #include "eapol_sm.h"
26 #include "wpa.h"
27 #include "preauth.h"
28
29 #ifndef ETH_P_PREAUTH
30 #define ETH_P_PREAUTH 0x88C7 /* IEEE 802.11i pre-authentication */
31 #endif /* ETH_P_PREAUTH */
32
33 static const int dot11RSNAConfigPMKLifetime = 43200;
34
35 struct rsn_preauth_interface {
36         struct rsn_preauth_interface *next;
37         struct hostapd_data *hapd;
38         struct l2_packet_data *l2;
39         char *ifname;
40         int ifindex;
41 };
42
43
44 static void rsn_preauth_receive(void *ctx, const u8 *src_addr,
45                                 const u8 *buf, size_t len)
46 {
47         struct rsn_preauth_interface *piface = ctx;
48         struct hostapd_data *hapd = piface->hapd;
49         struct ieee802_1x_hdr *hdr;
50         struct sta_info *sta;
51         struct l2_ethhdr *ethhdr;
52
53         wpa_printf(MSG_DEBUG, "RSN: receive pre-auth packet "
54                    "from interface '%s'", piface->ifname);
55         if (len < sizeof(*ethhdr) + sizeof(*hdr)) {
56                 wpa_printf(MSG_DEBUG, "RSN: too short pre-auth packet "
57                            "(len=%lu)", (unsigned long) len);
58                 return;
59         }
60
61         ethhdr = (struct l2_ethhdr *) buf;
62         hdr = (struct ieee802_1x_hdr *) (ethhdr + 1);
63
64         if (os_memcmp(ethhdr->h_dest, hapd->own_addr, ETH_ALEN) != 0) {
65                 wpa_printf(MSG_DEBUG, "RSN: pre-auth for foreign address "
66                            MACSTR, MAC2STR(ethhdr->h_dest));
67                 return;
68         }
69
70         sta = ap_get_sta(hapd, ethhdr->h_source);
71         if (sta && (sta->flags & WLAN_STA_ASSOC)) {
72                 wpa_printf(MSG_DEBUG, "RSN: pre-auth for already association "
73                            "STA " MACSTR, MAC2STR(sta->addr));
74                 return;
75         }
76         if (!sta && hdr->type == IEEE802_1X_TYPE_EAPOL_START) {
77                 sta = ap_sta_add(hapd, ethhdr->h_source);
78                 if (sta == NULL)
79                         return;
80                 sta->flags = WLAN_STA_PREAUTH;
81
82                 ieee802_1x_new_station(hapd, sta);
83                 if (sta->eapol_sm == NULL) {
84                         ap_free_sta(hapd, sta);
85                         sta = NULL;
86                 } else {
87                         sta->eapol_sm->radius_identifier = -1;
88                         sta->eapol_sm->portValid = TRUE;
89                         sta->eapol_sm->flags |= EAPOL_SM_PREAUTH;
90                 }
91         }
92         if (sta == NULL)
93                 return;
94         sta->preauth_iface = piface;
95         ieee802_1x_receive(hapd, ethhdr->h_source, (u8 *) (ethhdr + 1),
96                            len - sizeof(*ethhdr));
97 }
98
99
100 static int rsn_preauth_iface_add(struct hostapd_data *hapd, const char *ifname)
101 {
102         struct rsn_preauth_interface *piface;
103
104         wpa_printf(MSG_DEBUG, "RSN pre-auth interface '%s'", ifname);
105
106         piface = os_zalloc(sizeof(*piface));
107         if (piface == NULL)
108                 return -1;
109         piface->hapd = hapd;
110
111         piface->ifname = os_strdup(ifname);
112         if (piface->ifname == NULL) {
113                 goto fail1;
114         }
115
116         piface->l2 = l2_packet_init(piface->ifname, NULL, ETH_P_PREAUTH,
117                                     rsn_preauth_receive, piface, 1);
118         if (piface->l2 == NULL) {
119                 wpa_printf(MSG_ERROR, "Failed to open register layer 2 access "
120                            "to ETH_P_PREAUTH");
121                 goto fail2;
122         }
123
124         piface->next = hapd->preauth_iface;
125         hapd->preauth_iface = piface;
126         return 0;
127
128 fail2:
129         os_free(piface->ifname);
130 fail1:
131         os_free(piface);
132         return -1;
133 }
134
135
136 void rsn_preauth_iface_deinit(struct hostapd_data *hapd)
137 {
138         struct rsn_preauth_interface *piface, *prev;
139
140         piface = hapd->preauth_iface;
141         hapd->preauth_iface = NULL;
142         while (piface) {
143                 prev = piface;
144                 piface = piface->next;
145                 l2_packet_deinit(prev->l2);
146                 os_free(prev->ifname);
147                 os_free(prev);
148         }
149 }
150
151
152 int rsn_preauth_iface_init(struct hostapd_data *hapd)
153 {
154         char *tmp, *start, *end;
155
156         if (hapd->conf->rsn_preauth_interfaces == NULL)
157                 return 0;
158
159         tmp = os_strdup(hapd->conf->rsn_preauth_interfaces);
160         if (tmp == NULL)
161                 return -1;
162         start = tmp;
163         for (;;) {
164                 while (*start == ' ')
165                         start++;
166                 if (*start == '\0')
167                         break;
168                 end = os_strchr(start, ' ');
169                 if (end)
170                         *end = '\0';
171
172                 if (rsn_preauth_iface_add(hapd, start)) {
173                         rsn_preauth_iface_deinit(hapd);
174                         os_free(tmp);
175                         return -1;
176                 }
177
178                 if (end)
179                         start = end + 1;
180                 else
181                         break;
182         }
183         os_free(tmp);
184         return 0;
185 }
186
187
188 static void rsn_preauth_finished_cb(void *eloop_ctx, void *timeout_ctx)
189 {
190         struct hostapd_data *hapd = eloop_ctx;
191         struct sta_info *sta = timeout_ctx;
192         wpa_printf(MSG_DEBUG, "RSN: Removing pre-authentication STA entry for "
193                    MACSTR, MAC2STR(sta->addr));
194         ap_free_sta(hapd, sta);
195 }
196
197
198 void rsn_preauth_finished(struct hostapd_data *hapd, struct sta_info *sta,
199                           int success)
200 {
201         const u8 *key;
202         size_t len;
203         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
204                        HOSTAPD_LEVEL_INFO, "pre-authentication %s",
205                        success ? "succeeded" : "failed");
206
207         key = ieee802_1x_get_key(sta->eapol_sm, &len);
208         if (len > PMK_LEN)
209                 len = PMK_LEN;
210         if (success && key) {
211                 if (wpa_auth_pmksa_add_preauth(hapd->wpa_auth, key, len,
212                                                sta->addr,
213                                                dot11RSNAConfigPMKLifetime,
214                                                sta->eapol_sm) == 0) {
215                         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
216                                        HOSTAPD_LEVEL_DEBUG,
217                                        "added PMKSA cache entry (pre-auth)");
218                 } else {
219                         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
220                                        HOSTAPD_LEVEL_DEBUG,
221                                        "failed to add PMKSA cache entry "
222                                        "(pre-auth)");
223                 }
224         }
225
226         /*
227          * Finish STA entry removal from timeout in order to avoid freeing
228          * STA data before the caller has finished processing.
229          */
230         eloop_register_timeout(0, 0, rsn_preauth_finished_cb, hapd, sta);
231 }
232
233
234 void rsn_preauth_send(struct hostapd_data *hapd, struct sta_info *sta,
235                       u8 *buf, size_t len)
236 {
237         struct rsn_preauth_interface *piface;
238         struct l2_ethhdr *ethhdr;
239
240         piface = hapd->preauth_iface;
241         while (piface) {
242                 if (piface == sta->preauth_iface)
243                         break;
244                 piface = piface->next;
245         }
246
247         if (piface == NULL) {
248                 wpa_printf(MSG_DEBUG, "RSN: Could not find pre-authentication "
249                            "interface for " MACSTR, MAC2STR(sta->addr));
250                 return;
251         }
252
253         ethhdr = os_malloc(sizeof(*ethhdr) + len);
254         if (ethhdr == NULL)
255                 return;
256
257         os_memcpy(ethhdr->h_dest, sta->addr, ETH_ALEN);
258         os_memcpy(ethhdr->h_source, hapd->own_addr, ETH_ALEN);
259         ethhdr->h_proto = htons(ETH_P_PREAUTH);
260         os_memcpy(ethhdr + 1, buf, len);
261
262         if (l2_packet_send(piface->l2, sta->addr, ETH_P_PREAUTH, (u8 *) ethhdr,
263                            sizeof(*ethhdr) + len) < 0) {
264                 wpa_printf(MSG_ERROR, "Failed to send preauth packet using "
265                            "l2_packet_send\n");
266         }
267         os_free(ethhdr);
268 }
269
270
271 void rsn_preauth_free_station(struct hostapd_data *hapd, struct sta_info *sta)
272 {
273         eloop_cancel_timeout(rsn_preauth_finished_cb, hapd, sta);
274 }
275
276 #endif /* CONFIG_RSN_PREAUTH */