Re-initialize hostapd/wpa_supplicant git repository based on 0.6.3 release
[wpasupplicant] / hostapd / ieee802_11_auth.c
1 /*
2  * hostapd / IEEE 802.11 authentication (ACL)
3  * Copyright (c) 2003-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 #ifndef CONFIG_NATIVE_WINDOWS
18
19 #include "hostapd.h"
20 #include "ieee802_11.h"
21 #include "ieee802_11_auth.h"
22 #include "radius/radius.h"
23 #include "radius/radius_client.h"
24 #include "eloop.h"
25
26 #define RADIUS_ACL_TIMEOUT 30
27
28
29 struct hostapd_cached_radius_acl {
30         time_t timestamp;
31         macaddr addr;
32         int accepted; /* HOSTAPD_ACL_* */
33         struct hostapd_cached_radius_acl *next;
34         u32 session_timeout;
35         u32 acct_interim_interval;
36         int vlan_id;
37 };
38
39
40 struct hostapd_acl_query_data {
41         time_t timestamp;
42         u8 radius_id;
43         macaddr addr;
44         u8 *auth_msg; /* IEEE 802.11 authentication frame from station */
45         size_t auth_msg_len;
46         struct hostapd_acl_query_data *next;
47 };
48
49
50 static void hostapd_acl_cache_free(struct hostapd_cached_radius_acl *acl_cache)
51 {
52         struct hostapd_cached_radius_acl *prev;
53
54         while (acl_cache) {
55                 prev = acl_cache;
56                 acl_cache = acl_cache->next;
57                 os_free(prev);
58         }
59 }
60
61
62 static int hostapd_acl_cache_get(struct hostapd_data *hapd, const u8 *addr,
63                                  u32 *session_timeout,
64                                  u32 *acct_interim_interval, int *vlan_id)
65 {
66         struct hostapd_cached_radius_acl *entry;
67         time_t now;
68
69         time(&now);
70         entry = hapd->acl_cache;
71
72         while (entry) {
73                 if (os_memcmp(entry->addr, addr, ETH_ALEN) == 0) {
74                         if (now - entry->timestamp > RADIUS_ACL_TIMEOUT)
75                                 return -1; /* entry has expired */
76                         if (entry->accepted == HOSTAPD_ACL_ACCEPT_TIMEOUT)
77                                 *session_timeout = entry->session_timeout;
78                         *acct_interim_interval = entry->acct_interim_interval;
79                         if (vlan_id)
80                                 *vlan_id = entry->vlan_id;
81                         return entry->accepted;
82                 }
83
84                 entry = entry->next;
85         }
86
87         return -1;
88 }
89
90
91 static void hostapd_acl_query_free(struct hostapd_acl_query_data *query)
92 {
93         if (query == NULL)
94                 return;
95         os_free(query->auth_msg);
96         os_free(query);
97 }
98
99
100 static int hostapd_radius_acl_query(struct hostapd_data *hapd, const u8 *addr,
101                                     struct hostapd_acl_query_data *query)
102 {
103         struct radius_msg *msg;
104         char buf[128];
105
106         query->radius_id = radius_client_get_id(hapd->radius);
107         msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST, query->radius_id);
108         if (msg == NULL)
109                 return -1;
110
111         radius_msg_make_authenticator(msg, addr, ETH_ALEN);
112
113         os_snprintf(buf, sizeof(buf), RADIUS_ADDR_FORMAT, MAC2STR(addr));
114         if (!radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME, (u8 *) buf,
115                                  os_strlen(buf))) {
116                 wpa_printf(MSG_DEBUG, "Could not add User-Name");
117                 goto fail;
118         }
119
120         if (!radius_msg_add_attr_user_password(
121                     msg, (u8 *) buf, os_strlen(buf),
122                     hapd->conf->radius->auth_server->shared_secret,
123                     hapd->conf->radius->auth_server->shared_secret_len)) {
124                 wpa_printf(MSG_DEBUG, "Could not add User-Password");
125                 goto fail;
126         }
127
128         if (hapd->conf->own_ip_addr.af == AF_INET &&
129             !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
130                                  (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
131                 wpa_printf(MSG_DEBUG, "Could not add NAS-IP-Address");
132                 goto fail;
133         }
134
135 #ifdef CONFIG_IPV6
136         if (hapd->conf->own_ip_addr.af == AF_INET6 &&
137             !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
138                                  (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
139                 wpa_printf(MSG_DEBUG, "Could not add NAS-IPv6-Address");
140                 goto fail;
141         }
142 #endif /* CONFIG_IPV6 */
143
144         if (hapd->conf->nas_identifier &&
145             !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
146                                  (u8 *) hapd->conf->nas_identifier,
147                                  os_strlen(hapd->conf->nas_identifier))) {
148                 wpa_printf(MSG_DEBUG, "Could not add NAS-Identifier");
149                 goto fail;
150         }
151
152         os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":%s",
153                     MAC2STR(hapd->own_addr), hapd->conf->ssid.ssid);
154         if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
155                                  (u8 *) buf, os_strlen(buf))) {
156                 wpa_printf(MSG_DEBUG, "Could not add Called-Station-Id");
157                 goto fail;
158         }
159
160         os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
161                     MAC2STR(addr));
162         if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
163                                  (u8 *) buf, os_strlen(buf))) {
164                 wpa_printf(MSG_DEBUG, "Could not add Calling-Station-Id");
165                 goto fail;
166         }
167
168         if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
169                                        RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
170                 wpa_printf(MSG_DEBUG, "Could not add NAS-Port-Type");
171                 goto fail;
172         }
173
174         os_snprintf(buf, sizeof(buf), "CONNECT 11Mbps 802.11b");
175         if (!radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
176                                  (u8 *) buf, os_strlen(buf))) {
177                 wpa_printf(MSG_DEBUG, "Could not add Connect-Info");
178                 goto fail;
179         }
180
181         radius_client_send(hapd->radius, msg, RADIUS_AUTH, addr);
182         return 0;
183
184  fail:
185         radius_msg_free(msg);
186         os_free(msg);
187         return -1;
188 }
189
190
191 int hostapd_allowed_address(struct hostapd_data *hapd, const u8 *addr,
192                             const u8 *msg, size_t len, u32 *session_timeout,
193                             u32 *acct_interim_interval, int *vlan_id)
194 {
195         *session_timeout = 0;
196         *acct_interim_interval = 0;
197         if (vlan_id)
198                 *vlan_id = 0;
199
200         if (hostapd_maclist_found(hapd->conf->accept_mac,
201                                   hapd->conf->num_accept_mac, addr))
202                 return HOSTAPD_ACL_ACCEPT;
203
204         if (hostapd_maclist_found(hapd->conf->deny_mac,
205                                   hapd->conf->num_deny_mac, addr))
206                 return HOSTAPD_ACL_REJECT;
207
208         if (hapd->conf->macaddr_acl == ACCEPT_UNLESS_DENIED)
209                 return HOSTAPD_ACL_ACCEPT;
210         if (hapd->conf->macaddr_acl == DENY_UNLESS_ACCEPTED)
211                 return HOSTAPD_ACL_REJECT;
212
213         if (hapd->conf->macaddr_acl == USE_EXTERNAL_RADIUS_AUTH) {
214                 struct hostapd_acl_query_data *query;
215
216                 /* Check whether ACL cache has an entry for this station */
217                 int res = hostapd_acl_cache_get(hapd, addr, session_timeout,
218                                                 acct_interim_interval,
219                                                 vlan_id);
220                 if (res == HOSTAPD_ACL_ACCEPT ||
221                     res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
222                         return res;
223                 if (res == HOSTAPD_ACL_REJECT)
224                         return HOSTAPD_ACL_REJECT;
225
226                 query = hapd->acl_queries;
227                 while (query) {
228                         if (os_memcmp(query->addr, addr, ETH_ALEN) == 0) {
229                                 /* pending query in RADIUS retransmit queue;
230                                  * do not generate a new one */
231                                 return HOSTAPD_ACL_PENDING;
232                         }
233                         query = query->next;
234                 }
235
236                 if (!hapd->conf->radius->auth_server)
237                         return HOSTAPD_ACL_REJECT;
238
239                 /* No entry in the cache - query external RADIUS server */
240                 query = os_zalloc(sizeof(*query));
241                 if (query == NULL) {
242                         wpa_printf(MSG_ERROR, "malloc for query data failed");
243                         return HOSTAPD_ACL_REJECT;
244                 }
245                 time(&query->timestamp);
246                 os_memcpy(query->addr, addr, ETH_ALEN);
247                 if (hostapd_radius_acl_query(hapd, addr, query)) {
248                         wpa_printf(MSG_DEBUG, "Failed to send Access-Request "
249                                    "for ACL query.");
250                         hostapd_acl_query_free(query);
251                         return HOSTAPD_ACL_REJECT;
252                 }
253
254                 query->auth_msg = os_malloc(len);
255                 if (query->auth_msg == NULL) {
256                         wpa_printf(MSG_ERROR, "Failed to allocate memory for "
257                                    "auth frame.");
258                         hostapd_acl_query_free(query);
259                         return HOSTAPD_ACL_REJECT;
260                 }
261                 os_memcpy(query->auth_msg, msg, len);
262                 query->auth_msg_len = len;
263                 query->next = hapd->acl_queries;
264                 hapd->acl_queries = query;
265
266                 /* Queued data will be processed in hostapd_acl_recv_radius()
267                  * when RADIUS server replies to the sent Access-Request. */
268                 return HOSTAPD_ACL_PENDING;
269         }
270
271         return HOSTAPD_ACL_REJECT;
272 }
273
274
275 static void hostapd_acl_expire_cache(struct hostapd_data *hapd, time_t now)
276 {
277         struct hostapd_cached_radius_acl *prev, *entry, *tmp;
278
279         prev = NULL;
280         entry = hapd->acl_cache;
281
282         while (entry) {
283                 if (now - entry->timestamp > RADIUS_ACL_TIMEOUT) {
284                         wpa_printf(MSG_DEBUG, "Cached ACL entry for " MACSTR
285                                    " has expired.", MAC2STR(entry->addr));
286                         if (prev)
287                                 prev->next = entry->next;
288                         else
289                                 hapd->acl_cache = entry->next;
290
291                         tmp = entry;
292                         entry = entry->next;
293                         os_free(tmp);
294                         continue;
295                 }
296
297                 prev = entry;
298                 entry = entry->next;
299         }
300 }
301
302
303 static void hostapd_acl_expire_queries(struct hostapd_data *hapd, time_t now)
304 {
305         struct hostapd_acl_query_data *prev, *entry, *tmp;
306
307         prev = NULL;
308         entry = hapd->acl_queries;
309
310         while (entry) {
311                 if (now - entry->timestamp > RADIUS_ACL_TIMEOUT) {
312                         wpa_printf(MSG_DEBUG, "ACL query for " MACSTR
313                                    " has expired.", MAC2STR(entry->addr));
314                         if (prev)
315                                 prev->next = entry->next;
316                         else
317                                 hapd->acl_queries = entry->next;
318
319                         tmp = entry;
320                         entry = entry->next;
321                         hostapd_acl_query_free(tmp);
322                         continue;
323                 }
324
325                 prev = entry;
326                 entry = entry->next;
327         }
328 }
329
330
331 static void hostapd_acl_expire(void *eloop_ctx, void *timeout_ctx)
332 {
333         struct hostapd_data *hapd = eloop_ctx;
334         time_t now;
335
336         time(&now);
337         hostapd_acl_expire_cache(hapd, now);
338         hostapd_acl_expire_queries(hapd, now);
339
340         eloop_register_timeout(10, 0, hostapd_acl_expire, hapd, NULL);
341 }
342
343
344 /* Return 0 if RADIUS message was a reply to ACL query (and was processed here)
345  * or -1 if not. */
346 static RadiusRxResult
347 hostapd_acl_recv_radius(struct radius_msg *msg, struct radius_msg *req,
348                         u8 *shared_secret, size_t shared_secret_len,
349                         void *data)
350 {
351         struct hostapd_data *hapd = data;
352         struct hostapd_acl_query_data *query, *prev;
353         struct hostapd_cached_radius_acl *cache;
354
355         query = hapd->acl_queries;
356         prev = NULL;
357         while (query) {
358                 if (query->radius_id == msg->hdr->identifier)
359                         break;
360                 prev = query;
361                 query = query->next;
362         }
363         if (query == NULL)
364                 return RADIUS_RX_UNKNOWN;
365
366         wpa_printf(MSG_DEBUG, "Found matching Access-Request for RADIUS "
367                    "message (id=%d)", query->radius_id);
368
369         if (radius_msg_verify(msg, shared_secret, shared_secret_len, req, 0)) {
370                 wpa_printf(MSG_INFO, "Incoming RADIUS packet did not have "
371                            "correct authenticator - dropped\n");
372                 return RADIUS_RX_INVALID_AUTHENTICATOR;
373         }
374
375         if (msg->hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
376             msg->hdr->code != RADIUS_CODE_ACCESS_REJECT) {
377                 wpa_printf(MSG_DEBUG, "Unknown RADIUS message code %d to ACL "
378                            "query", msg->hdr->code);
379                 return RADIUS_RX_UNKNOWN;
380         }
381
382         /* Insert Accept/Reject info into ACL cache */
383         cache = os_zalloc(sizeof(*cache));
384         if (cache == NULL) {
385                 wpa_printf(MSG_DEBUG, "Failed to add ACL cache entry");
386                 goto done;
387         }
388         time(&cache->timestamp);
389         os_memcpy(cache->addr, query->addr, sizeof(cache->addr));
390         if (msg->hdr->code == RADIUS_CODE_ACCESS_ACCEPT) {
391                 if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
392                                               &cache->session_timeout) == 0)
393                         cache->accepted = HOSTAPD_ACL_ACCEPT_TIMEOUT;
394                 else
395                         cache->accepted = HOSTAPD_ACL_ACCEPT;
396
397                 if (radius_msg_get_attr_int32(
398                             msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
399                             &cache->acct_interim_interval) == 0 &&
400                     cache->acct_interim_interval < 60) {
401                         wpa_printf(MSG_DEBUG, "Ignored too small "
402                                    "Acct-Interim-Interval %d for STA " MACSTR,
403                                    cache->acct_interim_interval,
404                                    MAC2STR(query->addr));
405                         cache->acct_interim_interval = 0;
406                 }
407
408                 cache->vlan_id = radius_msg_get_vlanid(msg);
409         } else
410                 cache->accepted = HOSTAPD_ACL_REJECT;
411         cache->next = hapd->acl_cache;
412         hapd->acl_cache = cache;
413
414         /* Re-send original authentication frame for 802.11 processing */
415         wpa_printf(MSG_DEBUG, "Re-sending authentication frame after "
416                    "successful RADIUS ACL query");
417         ieee802_11_mgmt(hapd, query->auth_msg, query->auth_msg_len,
418                         WLAN_FC_STYPE_AUTH, NULL);
419
420  done:
421         if (prev == NULL)
422                 hapd->acl_queries = query->next;
423         else
424                 prev->next = query->next;
425
426         hostapd_acl_query_free(query);
427
428         return RADIUS_RX_PROCESSED;
429 }
430
431
432 int hostapd_acl_init(struct hostapd_data *hapd)
433 {
434         if (radius_client_register(hapd->radius, RADIUS_AUTH,
435                                    hostapd_acl_recv_radius, hapd))
436                 return -1;
437
438         eloop_register_timeout(10, 0, hostapd_acl_expire, hapd, NULL);
439
440         return 0;
441 }
442
443
444 void hostapd_acl_deinit(struct hostapd_data *hapd)
445 {
446         struct hostapd_acl_query_data *query, *prev;
447
448         eloop_cancel_timeout(hostapd_acl_expire, hapd, NULL);
449
450         hostapd_acl_cache_free(hapd->acl_cache);
451
452         query = hapd->acl_queries;
453         while (query) {
454                 prev = query;
455                 query = query->next;
456                 hostapd_acl_query_free(prev);
457         }
458 }
459
460
461 int hostapd_acl_reconfig(struct hostapd_data *hapd,
462                          struct hostapd_config *oldconf)
463 {
464         if (!hapd->radius_client_reconfigured)
465                 return 0;
466
467         hostapd_acl_deinit(hapd);
468         return hostapd_acl_init(hapd);
469 }
470
471 #endif /* CONFIG_NATIVE_WINDOWS */