hostapd: remove version tag from directory
[dragonfly.git] / contrib / hostapd / preauth.c
1 /*
2  * hostapd - Authenticator for IEEE 802.11i RSN pre-authentication
3  * Copyright (c) 2004-2006, 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.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         HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "RSN: receive pre-auth packet "
54                       "from interface '%s'\n", piface->ifname);
55         if (len < sizeof(*ethhdr) + sizeof(*hdr)) {
56                 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "RSN: too short pre-auth "
57                               "packet (len=%lu)\n", (unsigned long) len);
58                 return;
59         }
60
61         ethhdr = (struct l2_ethhdr *) buf;
62         hdr = (struct ieee802_1x_hdr *) (ethhdr + 1);
63
64         if (memcmp(ethhdr->h_dest, hapd->own_addr, ETH_ALEN) != 0) {
65                 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "RSN: pre-auth for "
66                               "foreign address " MACSTR "\n",
67                               MAC2STR(ethhdr->h_dest));
68                 return;
69         }
70
71         sta = ap_get_sta(hapd, ethhdr->h_source);
72         if (sta && (sta->flags & WLAN_STA_ASSOC)) {
73                 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "RSN: pre-auth for "
74                               "already association STA " MACSTR "\n",
75                               MAC2STR(sta->addr));
76                 return;
77         }
78         if (!sta && hdr->type == IEEE802_1X_TYPE_EAPOL_START) {
79                 sta = ap_sta_add(hapd, ethhdr->h_source);
80                 if (sta == NULL)
81                         return;
82                 sta->flags = WLAN_STA_PREAUTH;
83
84                 ieee802_1x_new_station(hapd, sta);
85                 if (sta->eapol_sm == NULL) {
86                         ap_free_sta(hapd, sta);
87                         sta = NULL;
88                 } else {
89                         sta->eapol_sm->radius_identifier = -1;
90                         sta->eapol_sm->portValid = TRUE;
91                         sta->eapol_sm->flags |= EAPOL_SM_PREAUTH;
92                 }
93         }
94         if (sta == NULL)
95                 return;
96         sta->preauth_iface = piface;
97         ieee802_1x_receive(hapd, ethhdr->h_source, (u8 *) (ethhdr + 1),
98                            len - sizeof(*ethhdr));
99 }
100
101
102 static int rsn_preauth_iface_add(struct hostapd_data *hapd, const char *ifname)
103 {
104         struct rsn_preauth_interface *piface;
105
106         HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "RSN pre-auth interface '%s'\n",
107                       ifname);
108
109         piface = wpa_zalloc(sizeof(*piface));
110         if (piface == NULL)
111                 return -1;
112         piface->hapd = hapd;
113
114         piface->ifname = strdup(ifname);
115         if (piface->ifname == NULL) {
116                 goto fail1;
117         }
118
119         piface->l2 = l2_packet_init(piface->ifname, NULL, ETH_P_PREAUTH,
120                                     rsn_preauth_receive, piface, 1);
121         if (piface->l2 == NULL) {
122                 printf("Failed to open register layer 2 access to "
123                        "ETH_P_PREAUTH\n");
124                 goto fail2;
125         }
126
127         piface->next = hapd->preauth_iface;
128         hapd->preauth_iface = piface;
129         return 0;
130
131 fail2:
132         free(piface->ifname);
133 fail1:
134         free(piface);
135         return -1;
136 }
137
138
139 void rsn_preauth_iface_deinit(struct hostapd_data *hapd)
140 {
141         struct rsn_preauth_interface *piface, *prev;
142
143         piface = hapd->preauth_iface;
144         hapd->preauth_iface = NULL;
145         while (piface) {
146                 prev = piface;
147                 piface = piface->next;
148                 l2_packet_deinit(prev->l2);
149                 free(prev->ifname);
150                 free(prev);
151         }
152 }
153
154
155 int rsn_preauth_iface_init(struct hostapd_data *hapd)
156 {
157         char *tmp, *start, *end;
158
159         if (hapd->conf->rsn_preauth_interfaces == NULL)
160                 return 0;
161
162         tmp = strdup(hapd->conf->rsn_preauth_interfaces);
163         if (tmp == NULL)
164                 return -1;
165         start = tmp;
166         for (;;) {
167                 while (*start == ' ')
168                         start++;
169                 if (*start == '\0')
170                         break;
171                 end = strchr(start, ' ');
172                 if (end)
173                         *end = '\0';
174
175                 if (rsn_preauth_iface_add(hapd, start)) {
176                         rsn_preauth_iface_deinit(hapd);
177                         return -1;
178                 }
179
180                 if (end)
181                         start = end + 1;
182                 else
183                         break;
184         }
185         free(tmp);
186         return 0;
187 }
188
189
190 static void rsn_preauth_finished_cb(void *eloop_ctx, void *timeout_ctx)
191 {
192         struct hostapd_data *hapd = eloop_ctx;
193         struct sta_info *sta = timeout_ctx;
194         wpa_printf(MSG_DEBUG, "RSN: Removing pre-authentication STA entry for "
195                    MACSTR, MAC2STR(sta->addr));
196         ap_free_sta(hapd, sta);
197 }
198
199
200 void rsn_preauth_finished(struct hostapd_data *hapd, struct sta_info *sta,
201                           int success)
202 {
203         u8 *key;
204         size_t len;
205         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
206                        HOSTAPD_LEVEL_INFO, "pre-authentication %s",
207                        success ? "succeeded" : "failed");
208
209         key = ieee802_1x_get_key_crypt(sta->eapol_sm, &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                 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "RSN: Could not find "
249                               "pre-authentication interface for " MACSTR "\n",
250                               MAC2STR(sta->addr));
251                 return;
252         }
253
254         ethhdr = malloc(sizeof(*ethhdr) + len);
255         if (ethhdr == NULL)
256                 return;
257
258         memcpy(ethhdr->h_dest, sta->addr, ETH_ALEN);
259         memcpy(ethhdr->h_source, hapd->own_addr, ETH_ALEN);
260         ethhdr->h_proto = htons(ETH_P_PREAUTH);
261         memcpy(ethhdr + 1, buf, len);
262
263         if (l2_packet_send(piface->l2, sta->addr, ETH_P_PREAUTH, (u8 *) ethhdr,
264                            sizeof(*ethhdr) + len) < 0) {
265                 printf("Failed to send preauth packet using l2_packet_send\n");
266         }
267         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 */