blob: 44755ebbfd3265731d093b18316e5a19d744fd34 [file] [log] [blame]
Brian Waters13d96012017-12-08 16:53:31 -06001/*********************************************************************************************************
2* Software License Agreement (BSD License) *
3* Author: Sebastien Decugis <sdecugis@freediameter.net> *
4* *
5* Copyright (c) 2013, WIDE Project and NICT *
6* All rights reserved. *
7* *
8* Redistribution and use of this software in source and binary forms, with or without modification, are *
9* permitted provided that the following conditions are met: *
10* *
11* * Redistributions of source code must retain the above *
12* copyright notice, this list of conditions and the *
13* following disclaimer. *
14* *
15* * Redistributions in binary form must reproduce the above *
16* copyright notice, this list of conditions and the *
17* following disclaimer in the documentation and/or other *
18* materials provided with the distribution. *
19* *
20* * Neither the name of the WIDE Project or NICT nor the *
21* names of its contributors may be used to endorse or *
22* promote products derived from this software without *
23* specific prior written permission of WIDE Project and *
24* NICT. *
25* *
26* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
27* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
28* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
29* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *
30* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS *
31* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
32* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF *
33* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
34*********************************************************************************************************/
35
36/* freeDiameter author note:
37 * The content from this file comes for the main part from the hostap project.
38 * It is redistributed under the terms of the BSD license, as allowed
39 * by the original copyright reproduced below.
40 * The modifications to this file are placed under the copyright of the freeDiameter project.
41 */
42
43/*
44 * hostapd / RADIUS message processing
45 * Copyright (c) 2002-2008, Jouni Malinen <j@w1.fi>
46 *
47 * This program is free software; you can redistribute it and/or modify
48 * it under the terms of the GNU General Public License version 2 as
49 * published by the Free Software Foundation.
50 *
51 * Alternatively, this software may be distributed under the terms of BSD
52 * license.
53 *
54 * See README and COPYING for more details.
55 */
56
57/*********************************************************************************/
58#include "rgw.h"
59
60static struct radius_attr_hdr *
61radius_get_attr_hdr(struct radius_msg *msg, int idx)
62{
63 return (struct radius_attr_hdr *) (msg->buf + msg->attr_pos[idx]);
64}
65
66
67struct radius_msg *radius_msg_new(u8 code, u8 identifier)
68{
69 struct radius_msg *msg;
70
71 msg = os_malloc(sizeof(*msg));
72 if (msg == NULL)
73 return NULL;
74
75 if (radius_msg_initialize(msg, RADIUS_DEFAULT_MSG_SIZE)) {
76 os_free(msg);
77 return NULL;
78 }
79
80 radius_msg_set_hdr(msg, code, identifier);
81
82 return msg;
83}
84
85
86int radius_msg_initialize(struct radius_msg *msg, size_t init_len)
87{
88 if (msg == NULL || init_len < sizeof(struct radius_hdr))
89 return -1;
90
91 os_memset(msg, 0, sizeof(*msg));
92 msg->buf = os_zalloc(init_len);
93 if (msg->buf == NULL)
94 return -1;
95
96 msg->buf_size = init_len;
97 msg->hdr = (struct radius_hdr *) msg->buf;
98 msg->buf_used = sizeof(*msg->hdr);
99
100 msg->attr_pos =
101 os_zalloc(RADIUS_DEFAULT_ATTR_COUNT * sizeof(*msg->attr_pos));
102 if (msg->attr_pos == NULL) {
103 os_free(msg->buf);
104 msg->buf = NULL;
105 msg->hdr = NULL;
106 return -1;
107 }
108
109 msg->attr_size = RADIUS_DEFAULT_ATTR_COUNT;
110 msg->attr_used = 0;
111
112 return 0;
113}
114
115
116void radius_msg_set_hdr(struct radius_msg *msg, u8 code, u8 identifier)
117{
118 msg->hdr->code = code;
119 msg->hdr->identifier = identifier;
120}
121
122
123void radius_msg_free(struct radius_msg *msg)
124{
125 os_free(msg->buf);
126 msg->buf = NULL;
127 msg->hdr = NULL;
128 msg->buf_size = msg->buf_used = 0;
129
130 os_free(msg->attr_pos);
131 msg->attr_pos = NULL;
132 msg->attr_size = msg->attr_used = 0;
133}
134
135/* Destroy a message */
136void rgw_msg_free(struct rgw_radius_msg_meta ** msg)
137{
138 if (!msg || !*msg)
139 return;
140
141 radius_msg_free(&(*msg)->radius);
142 free(*msg);
143 *msg = NULL;
144}
145
146
147
148struct radius_attr_type {
149 u8 type;
150 char *name;
151 enum {
152 RADIUS_ATTR_UNDIST, RADIUS_ATTR_TEXT, RADIUS_ATTR_IP,
153 RADIUS_ATTR_HEXDUMP, RADIUS_ATTR_INT32, RADIUS_ATTR_IPV6
154 } data_type;
155};
156
157static struct radius_attr_type radius_attrs[] =
158{
159 { RADIUS_ATTR_USER_NAME, "User-Name", RADIUS_ATTR_TEXT },
160 { RADIUS_ATTR_USER_PASSWORD, "User-Password", RADIUS_ATTR_UNDIST },
161 { RADIUS_ATTR_NAS_IP_ADDRESS, "NAS-IP-Address", RADIUS_ATTR_IP },
162 { RADIUS_ATTR_NAS_PORT, "NAS-Port", RADIUS_ATTR_INT32 },
163 { RADIUS_ATTR_FRAMED_MTU, "Framed-MTU", RADIUS_ATTR_INT32 },
164 { RADIUS_ATTR_REPLY_MESSAGE, "Reply-Message", RADIUS_ATTR_TEXT },
165 { RADIUS_ATTR_STATE, "State", RADIUS_ATTR_UNDIST },
166 { RADIUS_ATTR_CLASS, "Class", RADIUS_ATTR_UNDIST },
167 { RADIUS_ATTR_VENDOR_SPECIFIC, "Vendor-Specific", RADIUS_ATTR_UNDIST },
168 { RADIUS_ATTR_SESSION_TIMEOUT, "Session-Timeout", RADIUS_ATTR_INT32 },
169 { RADIUS_ATTR_IDLE_TIMEOUT, "Idle-Timeout", RADIUS_ATTR_INT32 },
170 { RADIUS_ATTR_TERMINATION_ACTION, "Termination-Action",
171 RADIUS_ATTR_INT32 },
172 { RADIUS_ATTR_CALLED_STATION_ID, "Called-Station-Id",
173 RADIUS_ATTR_TEXT },
174 { RADIUS_ATTR_CALLING_STATION_ID, "Calling-Station-Id",
175 RADIUS_ATTR_TEXT },
176 { RADIUS_ATTR_NAS_IDENTIFIER, "NAS-Identifier", RADIUS_ATTR_TEXT },
177 { RADIUS_ATTR_PROXY_STATE, "Proxy-State", RADIUS_ATTR_UNDIST },
178 { RADIUS_ATTR_ACCT_STATUS_TYPE, "Acct-Status-Type",
179 RADIUS_ATTR_INT32 },
180 { RADIUS_ATTR_ACCT_DELAY_TIME, "Acct-Delay-Time", RADIUS_ATTR_INT32 },
181 { RADIUS_ATTR_ACCT_INPUT_OCTETS, "Acct-Input-Octets",
182 RADIUS_ATTR_INT32 },
183 { RADIUS_ATTR_ACCT_OUTPUT_OCTETS, "Acct-Output-Octets",
184 RADIUS_ATTR_INT32 },
185 { RADIUS_ATTR_ACCT_SESSION_ID, "Acct-Session-Id", RADIUS_ATTR_TEXT },
186 { RADIUS_ATTR_ACCT_AUTHENTIC, "Acct-Authentic", RADIUS_ATTR_INT32 },
187 { RADIUS_ATTR_ACCT_SESSION_TIME, "Acct-Session-Time",
188 RADIUS_ATTR_INT32 },
189 { RADIUS_ATTR_ACCT_INPUT_PACKETS, "Acct-Input-Packets",
190 RADIUS_ATTR_INT32 },
191 { RADIUS_ATTR_ACCT_OUTPUT_PACKETS, "Acct-Output-Packets",
192 RADIUS_ATTR_INT32 },
193 { RADIUS_ATTR_ACCT_TERMINATE_CAUSE, "Acct-Terminate-Cause",
194 RADIUS_ATTR_INT32 },
195 { RADIUS_ATTR_ACCT_MULTI_SESSION_ID, "Acct-Multi-Session-Id",
196 RADIUS_ATTR_TEXT },
197 { RADIUS_ATTR_ACCT_LINK_COUNT, "Acct-Link-Count", RADIUS_ATTR_INT32 },
198 { RADIUS_ATTR_ACCT_INPUT_GIGAWORDS, "Acct-Input-Gigawords",
199 RADIUS_ATTR_INT32 },
200 { RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS, "Acct-Output-Gigawords",
201 RADIUS_ATTR_INT32 },
202 { RADIUS_ATTR_EVENT_TIMESTAMP, "Event-Timestamp",
203 RADIUS_ATTR_INT32 },
204 { RADIUS_ATTR_NAS_PORT_TYPE, "NAS-Port-Type", RADIUS_ATTR_INT32 },
205 { RADIUS_ATTR_TUNNEL_TYPE, "Tunnel-Type", RADIUS_ATTR_HEXDUMP },
206 { RADIUS_ATTR_TUNNEL_MEDIUM_TYPE, "Tunnel-Medium-Type",
207 RADIUS_ATTR_HEXDUMP },
208 { RADIUS_ATTR_CONNECT_INFO, "Connect-Info", RADIUS_ATTR_TEXT },
209 { RADIUS_ATTR_EAP_MESSAGE, "EAP-Message", RADIUS_ATTR_UNDIST },
210 { RADIUS_ATTR_MESSAGE_AUTHENTICATOR, "Message-Authenticator",
211 RADIUS_ATTR_UNDIST },
212 { RADIUS_ATTR_TUNNEL_PRIVATE_GROUP_ID, "Tunnel-Private-Group-Id",
213 RADIUS_ATTR_HEXDUMP },
214 { RADIUS_ATTR_ACCT_INTERIM_INTERVAL, "Acct-Interim-Interval",
215 RADIUS_ATTR_INT32 },
216 { RADIUS_ATTR_CHARGEABLE_USER_IDENTITY, "Chargeable-User-Identity",
217 RADIUS_ATTR_TEXT },
218 { RADIUS_ATTR_NAS_IPV6_ADDRESS, "NAS-IPv6-Address", RADIUS_ATTR_IPV6 },
219};
220#define RADIUS_ATTRS (sizeof(radius_attrs) / sizeof(radius_attrs[0]))
221
222
223static struct radius_attr_type *radius_get_attr_type(u8 type)
224{
225 size_t i;
226
227 for (i = 0; i < RADIUS_ATTRS; i++) {
228 if (type == radius_attrs[i].type)
229 return &radius_attrs[i];
230 }
231
232 return NULL;
233}
234
235static char print_char_buf[5];
236
237static char * print_char(char c)
238{
239 if (c >= 32 && c < 127)
240 sprintf(print_char_buf, "%c", c);
241 else
242 sprintf(print_char_buf, "<%02x>", c);
243 return print_char_buf;
244}
245
246
247static char * radius_msg_dump_attr_val(struct radius_attr_hdr *hdr, char * outbuf, size_t buflen)
248{
249 struct radius_attr_type *attr;
250 int i, len;
251 unsigned char *pos;
252 u8 attrtype;
253
254 memset(outbuf, 0, buflen);
255
256 attr = radius_get_attr_type(hdr->type);
257
258 if (attr == NULL)
259 attrtype = RADIUS_ATTR_HEXDUMP;
260 else
261 attrtype = attr->data_type;
262
263 len = hdr->length - sizeof(struct radius_attr_hdr);
264 pos = (unsigned char *) (hdr + 1);
265
266 switch (attrtype) {
267 case RADIUS_ATTR_TEXT:
268 snprintf(outbuf + strlen(outbuf), buflen - strlen(outbuf), " Value: '");
269 for (i = 0; i < len; i++)
270 snprintf(outbuf + strlen(outbuf), buflen - strlen(outbuf), "%s", print_char(pos[i]));
271 snprintf(outbuf + strlen(outbuf), buflen - strlen(outbuf), "'");
272 break;
273
274 case RADIUS_ATTR_IP:
275 if (len == 4) {
276 struct in_addr addr;
277 os_memcpy(&addr, pos, 4);
278 snprintf(outbuf + strlen(outbuf), buflen - strlen(outbuf), " Value: %s", inet_ntoa(addr));
279 } else
280 snprintf(outbuf + strlen(outbuf), buflen - strlen(outbuf), " Invalid IP address length %d", len);
281 break;
282
283 case RADIUS_ATTR_IPV6:
284 if (len == 16) {
285 char buf[128];
286 const char *atxt;
287 struct in6_addr *addr = (struct in6_addr *) pos;
288 atxt = inet_ntop(AF_INET6, addr, buf, sizeof(buf));
289 snprintf(outbuf + strlen(outbuf), buflen - strlen(outbuf), " Value: %s", atxt ? atxt : "?");
290 } else
291 snprintf(outbuf + strlen(outbuf), buflen - strlen(outbuf), " Invalid IPv6 address length %d", len);
292 break;
293
294 case RADIUS_ATTR_INT32:
295 if (len == 4)
296 snprintf(outbuf + strlen(outbuf), buflen - strlen(outbuf), " Value: %u", WPA_GET_BE32(pos));
297 else
298 snprintf(outbuf + strlen(outbuf), buflen - strlen(outbuf), " Invalid INT32 length %d", len);
299 break;
300
301 case RADIUS_ATTR_HEXDUMP:
302 case RADIUS_ATTR_UNDIST:
303 default:
304 snprintf(outbuf + strlen(outbuf), buflen - strlen(outbuf), " Value:");
305 for (i = 0; i < len; i++)
306 snprintf(outbuf + strlen(outbuf), buflen - strlen(outbuf), " %02x", pos[i]);
307 break;
308 }
309
310 return outbuf;
311}
312
313/* Dump a message */
314void rgw_msg_dump(struct rgw_radius_msg_meta * msg, int has_meta)
315{
316 unsigned char *auth;
317 char buf[256];
318 size_t i;
319 if (! TRACE_BOOL(FULL) )
320 return;
321
322 auth = &(msg->radius.hdr->authenticator[0]);
323
324 fd_log_debug("------ RADIUS msg dump -------");
325 fd_log_debug(" id : 0x%02hhx, code : %hhd (%s), length : %d", msg->radius.hdr->identifier, msg->radius.hdr->code, rgw_msg_code_str(msg->radius.hdr->code), ntohs(msg->radius.hdr->length));
326 fd_log_debug(" auth: %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx",
327 auth[0], auth[1], auth[2], auth[3], auth[4], auth[5], auth[6], auth[7]);
328 fd_log_debug(" %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx",
329 auth[8], auth[9], auth[10], auth[11], auth[12], auth[13], auth[14], auth[15]);
330 for (i = 0; i < msg->radius.attr_used; i++) {
331 struct radius_attr_hdr *attr = (struct radius_attr_hdr *)(msg->radius.buf + msg->radius.attr_pos[i]);
332 fd_log_debug(" - Type: 0x%02hhx (%s) Len: %-3hhu", attr->type, rgw_msg_attrtype_str(attr->type), attr->length);
333 fd_log_debug("%s", radius_msg_dump_attr_val(attr, buf, sizeof(buf)));
334 }
335 if (has_meta && msg->ps_nb) {
336 fd_log_debug("---- hidden attributes:");
337 for (i = msg->ps_first; i < msg->ps_first + msg->ps_nb; i++) {
338 struct radius_attr_hdr *attr = (struct radius_attr_hdr *)(msg->radius.buf + msg->radius.attr_pos[i]);
339 fd_log_debug(" - Type: 0x%02hhx (%s) Len: %-3hhu", attr->type, rgw_msg_attrtype_str(attr->type), attr->length);
340 fd_log_debug("%s", radius_msg_dump_attr_val(attr, buf, sizeof(buf)));
341 }
342 }
343 fd_log_debug("-----------------------------");
344}
345
346
347int radius_msg_finish(struct radius_msg *msg, const u8 *secret,
348 size_t secret_len)
349{
350 if (secret) {
351 u8 auth[MD5_MAC_LEN];
352 struct radius_attr_hdr *attr;
353
354 os_memset(auth, 0, MD5_MAC_LEN);
355 attr = radius_msg_add_attr(msg,
356 RADIUS_ATTR_MESSAGE_AUTHENTICATOR,
357 auth, MD5_MAC_LEN);
358 if (attr == NULL) {
359 fd_log_debug("WARNING: Could not add Message-Authenticator");
360 return -1;
361 }
362 msg->hdr->length = htons(msg->buf_used);
363 hmac_md5(secret, secret_len, msg->buf, msg->buf_used,
364 (u8 *) (attr + 1));
365 } else
366 msg->hdr->length = htons(msg->buf_used);
367
368 if (msg->buf_used > 0xffff) {
369 fd_log_debug("WARNING: too long RADIUS message (%lu)",
370 (unsigned long) msg->buf_used);
371 return -1;
372 }
373 return 0;
374}
375
376
377int radius_msg_finish_srv(struct radius_msg *msg, const u8 *secret,
378 size_t secret_len, const u8 *req_authenticator)
379{
380 u8 auth[MD5_MAC_LEN];
381 struct radius_attr_hdr *attr;
382 const u8 *addr[4];
383 size_t len[4];
384
385 if (msg->hdr->code != RADIUS_CODE_ACCOUNTING_RESPONSE) {
386 os_memset(auth, 0, MD5_MAC_LEN);
387 attr = radius_msg_add_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR,
388 auth, MD5_MAC_LEN);
389 if (attr == NULL) {
390 fd_log_debug("WARNING: Could not add Message-Authenticator");
391 return -1;
392 }
393 msg->hdr->length = htons(msg->buf_used);
394 os_memcpy(msg->hdr->authenticator, req_authenticator,
395 sizeof(msg->hdr->authenticator));
396 hmac_md5(secret, secret_len, msg->buf, msg->buf_used,
397 (u8 *) (attr + 1));
398 } else {
399 msg->hdr->length = htons(msg->buf_used);
400 }
401
402 /* ResponseAuth = MD5(Code+ID+Length+RequestAuth+Attributes+Secret) */
403 addr[0] = (u8 *) msg->hdr;
404 len[0] = 1 + 1 + 2;
405 addr[1] = req_authenticator;
406 len[1] = MD5_MAC_LEN;
407 addr[2] = (u8 *) (msg->hdr + 1);
408 len[2] = msg->buf_used - sizeof(*msg->hdr);
409 addr[3] = secret;
410 len[3] = secret_len;
411 md5_vector(4, addr, len, msg->hdr->authenticator);
412
413 if (msg->buf_used > 0xffff) {
414 fd_log_debug("WARNING: too long RADIUS message (%lu)",
415 (unsigned long) msg->buf_used);
416 return -1;
417 }
418 return 0;
419}
420
421
422void radius_msg_finish_acct(struct radius_msg *msg, const u8 *secret,
423 size_t secret_len)
424{
425 const u8 *addr[2];
426 size_t len[2];
427
428 msg->hdr->length = htons(msg->buf_used);
429 os_memset(msg->hdr->authenticator, 0, MD5_MAC_LEN);
430 addr[0] = msg->buf;
431 len[0] = msg->buf_used;
432 addr[1] = secret;
433 len[1] = secret_len;
434 md5_vector(2, addr, len, msg->hdr->authenticator);
435
436 if (msg->buf_used > 0xffff) {
437 fd_log_debug("WARNING: too long RADIUS messages (%lu)",
438 (unsigned long) msg->buf_used);
439 }
440}
441
442
443int radius_msg_add_attr_to_array(struct radius_msg *msg,
444 struct radius_attr_hdr *attr)
445{
446 if (msg->attr_used >= msg->attr_size) {
447 size_t *nattr_pos;
448 int nlen = msg->attr_size * 2;
449
450 nattr_pos = os_realloc(msg->attr_pos,
451 nlen * sizeof(*msg->attr_pos));
452 if (nattr_pos == NULL)
453 return -1;
454
455 msg->attr_pos = nattr_pos;
456 msg->attr_size = nlen;
457 }
458
459 msg->attr_pos[msg->attr_used++] = (unsigned char *) attr - msg->buf;
460
461 return 0;
462}
463
464
465struct radius_attr_hdr *radius_msg_add_attr(struct radius_msg *msg, u8 type,
466 const u8 *data, size_t data_len)
467{
468 size_t buf_needed;
469 struct radius_attr_hdr *attr;
470
471 if (data_len > RADIUS_MAX_ATTR_LEN) {
472 fd_log_debug("radius_msg_add_attr: too long attribute (%lu bytes)",
473 (unsigned long) data_len);
474 return NULL;
475 }
476
477 buf_needed = msg->buf_used + sizeof(*attr) + data_len;
478
479 if (msg->buf_size < buf_needed) {
480 /* allocate more space for message buffer */
481 unsigned char *nbuf;
482 size_t nlen = msg->buf_size;
483
484 while (nlen < buf_needed)
485 nlen *= 2;
486 nbuf = os_realloc(msg->buf, nlen);
487 if (nbuf == NULL)
488 return NULL;
489 msg->buf = nbuf;
490 msg->hdr = (struct radius_hdr *) msg->buf;
491 os_memset(msg->buf + msg->buf_size, 0, nlen - msg->buf_size);
492 msg->buf_size = nlen;
493 }
494
495 attr = (struct radius_attr_hdr *) (msg->buf + msg->buf_used);
496 attr->type = type;
497 attr->length = sizeof(*attr) + data_len;
498 if (data_len > 0)
499 os_memcpy(attr + 1, data, data_len);
500
501 msg->buf_used += sizeof(*attr) + data_len;
502
503 if (radius_msg_add_attr_to_array(msg, attr))
504 return NULL;
505
506 return attr;
507}
508
509
510/* Modified version of radius_msg_parse */
511int rgw_msg_parse(unsigned char * buf, size_t len, struct rgw_radius_msg_meta ** msg)
512{
513 struct rgw_radius_msg_meta * temp_msg = NULL;
514 struct radius_hdr *hdr;
515 struct radius_attr_hdr *attr;
516 size_t msg_len;
517 unsigned char *pos, *end;
518 int ret = 0;
519
520 TRACE_ENTRY("%p %zd %p", buf, len, msg);
521
522 CHECK_PARAMS( buf && len >= sizeof(*hdr) && msg );
523
524 *msg = NULL;
525
526 /* Parse the RADIUS message */
527 hdr = (struct radius_hdr *) buf;
528 msg_len = ntohs(hdr->length);
529 if (msg_len < sizeof(*hdr) || msg_len > len) {
530 TRACE_DEBUG(INFO, "Invalid RADIUS message length");
531 return EINVAL;
532 }
533
534 if (msg_len < len) {
535 TRACE_DEBUG(INFO, "Ignored %lu extra bytes after RADIUS message",
536 (unsigned long) len - msg_len);
537 }
538
539 CHECK_MALLOC( temp_msg = malloc(sizeof(struct rgw_radius_msg_meta)) );
540 memset(temp_msg, 0, sizeof(struct rgw_radius_msg_meta));
541
542 if (radius_msg_initialize(&temp_msg->radius, msg_len)) {
543 TRACE_DEBUG(INFO, "Error in radius_msg_initialize, returning ENOMEM.");
544 free(temp_msg);
545 return ENOMEM;
546 }
547
548 /* Store the received data in the alloc'd buffer */
549 memcpy(temp_msg->radius.buf, buf, msg_len);
550 temp_msg->radius.buf_size = temp_msg->radius.buf_used = msg_len;
551
552 /* parse attributes */
553 pos = (unsigned char *) (temp_msg->radius.hdr + 1);
554 end = temp_msg->radius.buf + temp_msg->radius.buf_used;
555
556 while (pos < end) {
557 if ((size_t) (end - pos) < sizeof(*attr)) {
558 TRACE_DEBUG(INFO, "Trucated attribute found in RADIUS buffer, EINVAL.");
559 ret = EINVAL;
560 break;
561 }
562
563 attr = (struct radius_attr_hdr *) pos;
564
565 if (pos + attr->length > end || attr->length < sizeof(*attr)) {
566 TRACE_DEBUG(INFO, "Trucated attribute found in RADIUS buffer, EINVAL.");
567 ret = EINVAL;
568 break;
569 }
570
571 if (radius_msg_add_attr_to_array(&temp_msg->radius, attr)) {
572 TRACE_DEBUG(INFO, "Error in radius_msg_add_attr_to_array, ENOMEM");
573 ret = ENOMEM;
574 break;
575 }
576
577 if (attr->type == RADIUS_ATTR_PROXY_STATE)
578 temp_msg->ps_nb += 1;
579
580 pos += attr->length;
581 }
582
583 if (ret != 0) {
584 radius_msg_free(&temp_msg->radius);
585 free(temp_msg);
586 return ret;
587 }
588
589 /* Now move all the proxy-state attributes at the end of the attr_pos array */
590 if (temp_msg->ps_nb) {
591 size_t *temp_ps = NULL;
592 int n, new_n = 0, p = 0;
593
594 CHECK_MALLOC( temp_ps = calloc(temp_msg->ps_nb, sizeof(size_t)) );
595
596 /* Move all the Proxy-State attributes into the temp_ps array */
597 for (n=0; n < temp_msg->radius.attr_used; n++) {
598 struct radius_attr_hdr * attr = (struct radius_attr_hdr *)(temp_msg->radius.buf + temp_msg->radius.attr_pos[n]);
599
600 if (attr->type == RADIUS_ATTR_PROXY_STATE) {
601 temp_ps[p++] = temp_msg->radius.attr_pos[n];
602 } else {
603 temp_msg->radius.attr_pos[new_n++] = temp_msg->radius.attr_pos[n];
604 }
605 }
606 temp_msg->radius.attr_used = new_n; /* hide the proxy-state to other modules */
607 temp_msg->ps_first = new_n;
608
609 /* And back into the array */
610 memcpy(temp_msg->radius.attr_pos + new_n, temp_ps, p * sizeof(size_t));
611 free(temp_ps);
612 }
613
614 *msg = temp_msg;
615 return 0;
616}
617
618
619
620int radius_msg_add_eap(struct radius_msg *msg, const u8 *data, size_t data_len)
621{
622 const u8 *pos = data;
623 size_t left = data_len;
624
625 while (left > 0) {
626 int len;
627 if (left > RADIUS_MAX_ATTR_LEN)
628 len = RADIUS_MAX_ATTR_LEN;
629 else
630 len = left;
631
632 if (!radius_msg_add_attr(msg, RADIUS_ATTR_EAP_MESSAGE,
633 pos, len))
634 return 0;
635
636 pos += len;
637 left -= len;
638 }
639
640 return 1;
641}
642
643
644u8 *radius_msg_get_eap(struct radius_msg *msg, size_t *eap_len)
645{
646 u8 *eap, *pos;
647 size_t len, i;
648 struct radius_attr_hdr *attr;
649
650 if (msg == NULL)
651 return NULL;
652
653 len = 0;
654 for (i = 0; i < msg->attr_used; i++) {
655 attr = radius_get_attr_hdr(msg, i);
656 if (attr->type == RADIUS_ATTR_EAP_MESSAGE)
657 len += attr->length - sizeof(struct radius_attr_hdr);
658 }
659
660 if (len == 0)
661 return NULL;
662
663 eap = os_malloc(len);
664 if (eap == NULL)
665 return NULL;
666
667 pos = eap;
668 for (i = 0; i < msg->attr_used; i++) {
669 attr = radius_get_attr_hdr(msg, i);
670 if (attr->type == RADIUS_ATTR_EAP_MESSAGE) {
671 int flen = attr->length - sizeof(*attr);
672 os_memcpy(pos, attr + 1, flen);
673 pos += flen;
674 }
675 }
676
677 if (eap_len)
678 *eap_len = len;
679
680 return eap;
681}
682
683
684int radius_msg_verify_msg_auth(struct radius_msg *msg, const u8 *secret,
685 size_t secret_len, const u8 *req_auth)
686{
687 u8 auth[MD5_MAC_LEN], orig[MD5_MAC_LEN];
688 u8 orig_authenticator[16];
689 struct radius_attr_hdr *attr = NULL, *tmp;
690 size_t i;
691
692 for (i = 0; i < msg->attr_used; i++) {
693 tmp = radius_get_attr_hdr(msg, i);
694 if (tmp->type == RADIUS_ATTR_MESSAGE_AUTHENTICATOR) {
695 if (attr != NULL) {
696 fd_log_debug("Multiple Message-Authenticator attributes in RADIUS message");
697 return 1;
698 }
699 attr = tmp;
700 }
701 }
702
703 if (attr == NULL) {
704 fd_log_debug("No Message-Authenticator attribute found");
705 return 1;
706 }
707
708 os_memcpy(orig, attr + 1, MD5_MAC_LEN);
709 os_memset(attr + 1, 0, MD5_MAC_LEN);
710 if (req_auth) {
711 os_memcpy(orig_authenticator, msg->hdr->authenticator,
712 sizeof(orig_authenticator));
713 os_memcpy(msg->hdr->authenticator, req_auth,
714 sizeof(msg->hdr->authenticator));
715 }
716 hmac_md5(secret, secret_len, msg->buf, msg->buf_used, auth);
717 os_memcpy(attr + 1, orig, MD5_MAC_LEN);
718 if (req_auth) {
719 os_memcpy(msg->hdr->authenticator, orig_authenticator,
720 sizeof(orig_authenticator));
721 }
722
723 if (os_memcmp(orig, auth, MD5_MAC_LEN) != 0) {
724 fd_log_debug("Invalid Message-Authenticator!");
725 return 1;
726 }
727
728 return 0;
729}
730
731
732int radius_msg_verify(struct radius_msg *msg, const u8 *secret,
733 size_t secret_len, struct radius_msg *sent_msg, int auth)
734{
735 const u8 *addr[4];
736 size_t len[4];
737 u8 hash[MD5_MAC_LEN];
738
739 if (sent_msg == NULL) {
740 fd_log_debug("No matching Access-Request message found");
741 return 1;
742 }
743
744 if (auth &&
745 radius_msg_verify_msg_auth(msg, secret, secret_len,
746 sent_msg->hdr->authenticator)) {
747 return 1;
748 }
749
750 /* ResponseAuth = MD5(Code+ID+Length+RequestAuth+Attributes+Secret) */
751 addr[0] = (u8 *) msg->hdr;
752 len[0] = 1 + 1 + 2;
753 addr[1] = sent_msg->hdr->authenticator;
754 len[1] = MD5_MAC_LEN;
755 addr[2] = (u8 *) (msg->hdr + 1);
756 len[2] = msg->buf_used - sizeof(*msg->hdr);
757 addr[3] = secret;
758 len[3] = secret_len;
759 md5_vector(4, addr, len, hash);
760 if (os_memcmp(hash, msg->hdr->authenticator, MD5_MAC_LEN) != 0) {
761 fd_log_debug("Response Authenticator invalid!");
762 return 1;
763 }
764
765 return 0;
766}
767
768
769int radius_msg_copy_attr(struct radius_msg *dst, struct radius_msg *src,
770 u8 type)
771{
772 struct radius_attr_hdr *attr;
773 size_t i;
774 int count = 0;
775
776 for (i = 0; i < src->attr_used; i++) {
777 attr = radius_get_attr_hdr(src, i);
778 if (attr->type == type) {
779 if (!radius_msg_add_attr(dst, type, (u8 *) (attr + 1),
780 attr->length - sizeof(*attr)))
781 return -1;
782 count++;
783 }
784 }
785
786 return count;
787}
788
789
790/* Create Request Authenticator. The value should be unique over the lifetime
791 * of the shared secret between authenticator and authentication server.
792 * Use one-way MD5 hash calculated from current timestamp and some data given
793 * by the caller. */
794void radius_msg_make_authenticator(struct radius_msg *msg,
795 const u8 *data, size_t len)
796{
797 struct os_time tv;
798 long int l;
799 const u8 *addr[3];
800 size_t elen[3];
801
802 os_get_time(&tv);
803 l = os_random();
804 addr[0] = (u8 *) &tv;
805 elen[0] = sizeof(tv);
806 addr[1] = data;
807 elen[1] = len;
808 addr[2] = (u8 *) &l;
809 elen[2] = sizeof(l);
810 md5_vector(3, addr, elen, msg->hdr->authenticator);
811}
812
813
814/* Get Vendor-specific RADIUS Attribute from a parsed RADIUS message.
815 * Returns the Attribute payload and sets alen to indicate the length of the
816 * payload if a vendor attribute with subtype is found, otherwise returns NULL.
817 * The returned payload is allocated with os_malloc() and caller must free it
818 * by calling os_free().
819 */
820static u8 *radius_msg_get_vendor_attr(struct radius_msg *msg, u32 vendor,
821 u8 subtype, size_t *alen)
822{
823 u8 *data, *pos;
824 size_t i, len;
825
826 if (msg == NULL)
827 return NULL;
828
829 for (i = 0; i < msg->attr_used; i++) {
830 struct radius_attr_hdr *attr = radius_get_attr_hdr(msg, i);
831 size_t left;
832 u32 vendor_id;
833 struct radius_attr_vendor *vhdr;
834
835 if (attr->type != RADIUS_ATTR_VENDOR_SPECIFIC)
836 continue;
837
838 left = attr->length - sizeof(*attr);
839 if (left < 4)
840 continue;
841
842 pos = (u8 *) (attr + 1);
843
844 os_memcpy(&vendor_id, pos, 4);
845 pos += 4;
846 left -= 4;
847
848 if (ntohl(vendor_id) != vendor)
849 continue;
850
851 while (left >= sizeof(*vhdr)) {
852 vhdr = (struct radius_attr_vendor *) pos;
853 if (vhdr->vendor_length > left ||
854 vhdr->vendor_length < sizeof(*vhdr)) {
855 left = 0;
856 break;
857 }
858 if (vhdr->vendor_type != subtype) {
859 pos += vhdr->vendor_length;
860 left -= vhdr->vendor_length;
861 continue;
862 }
863
864 len = vhdr->vendor_length - sizeof(*vhdr);
865 data = os_malloc(len);
866 if (data == NULL)
867 return NULL;
868 os_memcpy(data, pos + sizeof(*vhdr), len);
869 if (alen)
870 *alen = len;
871 return data;
872 }
873 }
874
875 return NULL;
876}
877
878
879static u8 * decrypt_ms_key(const u8 *key, size_t len,
880 const u8 *req_authenticator,
881 const u8 *secret, size_t secret_len, size_t *reslen)
882{
883 u8 *plain, *ppos, *res;
884 const u8 *pos;
885 size_t left, plen;
886 u8 hash[MD5_MAC_LEN];
887 int i, first = 1;
888 const u8 *addr[3];
889 size_t elen[3];
890
891 /* key: 16-bit salt followed by encrypted key info */
892
893 if (len < 2 + 16)
894 return NULL;
895
896 pos = key + 2;
897 left = len - 2;
898 if (left % 16) {
899 fd_log_debug("Invalid ms key len %lu", (unsigned long) left);
900 return NULL;
901 }
902
903 plen = left;
904 ppos = plain = os_malloc(plen);
905 if (plain == NULL)
906 return NULL;
907 plain[0] = 0;
908
909 while (left > 0) {
910 /* b(1) = MD5(Secret + Request-Authenticator + Salt)
911 * b(i) = MD5(Secret + c(i - 1)) for i > 1 */
912
913 addr[0] = secret;
914 elen[0] = secret_len;
915 if (first) {
916 addr[1] = req_authenticator;
917 elen[1] = MD5_MAC_LEN;
918 addr[2] = key;
919 elen[2] = 2; /* Salt */
920 } else {
921 addr[1] = pos - MD5_MAC_LEN;
922 elen[1] = MD5_MAC_LEN;
923 }
924 md5_vector(first ? 3 : 2, addr, elen, hash);
925 first = 0;
926
927 for (i = 0; i < MD5_MAC_LEN; i++)
928 *ppos++ = *pos++ ^ hash[i];
929 left -= MD5_MAC_LEN;
930 }
931
932 if (plain[0] == 0 || plain[0] > plen - 1) {
933 fd_log_debug("Failed to decrypt MPPE key");
934 os_free(plain);
935 return NULL;
936 }
937
938 res = os_malloc(plain[0]);
939 if (res == NULL) {
940 os_free(plain);
941 return NULL;
942 }
943 os_memcpy(res, plain + 1, plain[0]);
944 if (reslen)
945 *reslen = plain[0];
946 os_free(plain);
947 return res;
948}
949
950
951static void encrypt_ms_key(const u8 *key, size_t key_len, u16 salt,
952 const u8 *req_authenticator,
953 const u8 *secret, size_t secret_len,
954 u8 *ebuf, size_t *elen)
955{
956 int i, len, first = 1;
957 u8 hash[MD5_MAC_LEN], saltbuf[2], *pos;
958 const u8 *addr[3];
959 size_t _len[3];
960
961 WPA_PUT_BE16(saltbuf, salt);
962
963 len = 1 + key_len;
964 if (len & 0x0f) {
965 len = (len & 0xf0) + 16;
966 }
967 os_memset(ebuf, 0, len);
968 ebuf[0] = key_len;
969 os_memcpy(ebuf + 1, key, key_len);
970
971 *elen = len;
972
973 pos = ebuf;
974 while (len > 0) {
975 /* b(1) = MD5(Secret + Request-Authenticator + Salt)
976 * b(i) = MD5(Secret + c(i - 1)) for i > 1 */
977 addr[0] = secret;
978 _len[0] = secret_len;
979 if (first) {
980 addr[1] = req_authenticator;
981 _len[1] = MD5_MAC_LEN;
982 addr[2] = saltbuf;
983 _len[2] = sizeof(saltbuf);
984 } else {
985 addr[1] = pos - MD5_MAC_LEN;
986 _len[1] = MD5_MAC_LEN;
987 }
988 md5_vector(first ? 3 : 2, addr, _len, hash);
989 first = 0;
990
991 for (i = 0; i < MD5_MAC_LEN; i++)
992 *pos++ ^= hash[i];
993
994 len -= MD5_MAC_LEN;
995 }
996}
997
998
999struct radius_ms_mppe_keys *
1000radius_msg_get_ms_keys(struct radius_msg *msg, struct radius_msg *sent_msg,
1001 const u8 *secret, size_t secret_len)
1002{
1003 u8 *key;
1004 size_t keylen;
1005 struct radius_ms_mppe_keys *keys;
1006
1007 if (msg == NULL || sent_msg == NULL)
1008 return NULL;
1009
1010 keys = os_zalloc(sizeof(*keys));
1011 if (keys == NULL)
1012 return NULL;
1013
1014 key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_MICROSOFT,
1015 RADIUS_VENDOR_ATTR_MS_MPPE_SEND_KEY,
1016 &keylen);
1017 if (key) {
1018 keys->send = decrypt_ms_key(key, keylen,
1019 sent_msg->hdr->authenticator,
1020 secret, secret_len,
1021 &keys->send_len);
1022 os_free(key);
1023 }
1024
1025 key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_MICROSOFT,
1026 RADIUS_VENDOR_ATTR_MS_MPPE_RECV_KEY,
1027 &keylen);
1028 if (key) {
1029 keys->recv = decrypt_ms_key(key, keylen,
1030 sent_msg->hdr->authenticator,
1031 secret, secret_len,
1032 &keys->recv_len);
1033 os_free(key);
1034 }
1035
1036 return keys;
1037}
1038
1039
1040struct radius_ms_mppe_keys *
1041radius_msg_get_cisco_keys(struct radius_msg *msg, struct radius_msg *sent_msg,
1042 const u8 *secret, size_t secret_len)
1043{
1044 u8 *key;
1045 size_t keylen;
1046 struct radius_ms_mppe_keys *keys;
1047
1048 if (msg == NULL || sent_msg == NULL)
1049 return NULL;
1050
1051 keys = os_zalloc(sizeof(*keys));
1052 if (keys == NULL)
1053 return NULL;
1054
1055 key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_CISCO,
1056 RADIUS_CISCO_AV_PAIR, &keylen);
1057 if (key && keylen == 51 &&
1058 os_memcmp(key, "leap:session-key=", 17) == 0) {
1059 keys->recv = decrypt_ms_key(key + 17, keylen - 17,
1060 sent_msg->hdr->authenticator,
1061 secret, secret_len,
1062 &keys->recv_len);
1063 }
1064 os_free(key);
1065
1066 return keys;
1067}
1068
1069
1070int radius_msg_add_mppe_keys(struct radius_msg *msg,
1071 const u8 *req_authenticator,
1072 const u8 *secret, size_t secret_len,
1073 const u8 *send_key, size_t send_key_len,
1074 const u8 *recv_key, size_t recv_key_len)
1075{
1076 struct radius_attr_hdr *attr;
1077 u32 vendor_id = htonl(RADIUS_VENDOR_ID_MICROSOFT);
1078 u8 *buf;
1079 struct radius_attr_vendor *vhdr;
1080 u8 *pos;
1081 size_t elen;
1082 int hlen;
1083 u16 salt;
1084
1085 hlen = sizeof(vendor_id) + sizeof(*vhdr) + 2;
1086
1087 /* MS-MPPE-Send-Key */
1088 buf = os_malloc(hlen + send_key_len + 16);
1089 if (buf == NULL) {
1090 return 0;
1091 }
1092 pos = buf;
1093 os_memcpy(pos, &vendor_id, sizeof(vendor_id));
1094 pos += sizeof(vendor_id);
1095 vhdr = (struct radius_attr_vendor *) pos;
1096 vhdr->vendor_type = RADIUS_VENDOR_ATTR_MS_MPPE_SEND_KEY;
1097 pos = (u8 *) (vhdr + 1);
1098 salt = os_random() | 0x8000;
1099 WPA_PUT_BE16(pos, salt);
1100 pos += 2;
1101 encrypt_ms_key(send_key, send_key_len, salt, req_authenticator, secret,
1102 secret_len, pos, &elen);
1103 vhdr->vendor_length = hlen + elen - sizeof(vendor_id);
1104
1105 attr = radius_msg_add_attr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1106 buf, hlen + elen);
1107 os_free(buf);
1108 if (attr == NULL) {
1109 return 0;
1110 }
1111
1112 /* MS-MPPE-Recv-Key */
1113 buf = os_malloc(hlen + send_key_len + 16);
1114 if (buf == NULL) {
1115 return 0;
1116 }
1117 pos = buf;
1118 os_memcpy(pos, &vendor_id, sizeof(vendor_id));
1119 pos += sizeof(vendor_id);
1120 vhdr = (struct radius_attr_vendor *) pos;
1121 vhdr->vendor_type = RADIUS_VENDOR_ATTR_MS_MPPE_RECV_KEY;
1122 pos = (u8 *) (vhdr + 1);
1123 salt ^= 1;
1124 WPA_PUT_BE16(pos, salt);
1125 pos += 2;
1126 encrypt_ms_key(recv_key, recv_key_len, salt, req_authenticator, secret,
1127 secret_len, pos, &elen);
1128 vhdr->vendor_length = hlen + elen - sizeof(vendor_id);
1129
1130 attr = radius_msg_add_attr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1131 buf, hlen + elen);
1132 os_free(buf);
1133 if (attr == NULL) {
1134 return 0;
1135 }
1136
1137 return 1;
1138}
1139
1140
1141/* Add User-Password attribute to a RADIUS message and encrypt it as specified
1142 * in RFC 2865, Chap. 5.2 */
1143struct radius_attr_hdr *
1144radius_msg_add_attr_user_password(struct radius_msg *msg,
1145 const u8 *data, size_t data_len,
1146 const u8 *secret, size_t secret_len)
1147{
1148 u8 buf[128];
1149 int padlen, i;
1150 size_t buf_len, pos;
1151 const u8 *addr[2];
1152 size_t len[2];
1153 u8 hash[16];
1154
1155 if (data_len > 128)
1156 return NULL;
1157
1158 os_memcpy(buf, data, data_len);
1159 buf_len = data_len;
1160
1161 padlen = data_len % 16;
1162 if (padlen) {
1163 padlen = 16 - padlen;
1164 os_memset(buf + data_len, 0, padlen);
1165 buf_len += padlen;
1166 }
1167
1168 addr[0] = secret;
1169 len[0] = secret_len;
1170 addr[1] = msg->hdr->authenticator;
1171 len[1] = 16;
1172 md5_vector(2, addr, len, hash);
1173
1174 for (i = 0; i < 16; i++)
1175 buf[i] ^= hash[i];
1176 pos = 16;
1177
1178 while (pos < buf_len) {
1179 addr[0] = secret;
1180 len[0] = secret_len;
1181 addr[1] = &buf[pos - 16];
1182 len[1] = 16;
1183 md5_vector(2, addr, len, hash);
1184
1185 for (i = 0; i < 16; i++)
1186 buf[pos + i] ^= hash[i];
1187
1188 pos += 16;
1189 }
1190
1191 return radius_msg_add_attr(msg, RADIUS_ATTR_USER_PASSWORD,
1192 buf, buf_len);
1193}
1194
1195
1196int radius_msg_get_attr(struct radius_msg *msg, u8 type, u8 *buf, size_t len)
1197{
1198 struct radius_attr_hdr *attr = NULL, *tmp;
1199 size_t i, dlen;
1200
1201 for (i = 0; i < msg->attr_used; i++) {
1202 tmp = radius_get_attr_hdr(msg, i);
1203 if (tmp->type == type) {
1204 attr = tmp;
1205 break;
1206 }
1207 }
1208
1209 if (!attr)
1210 return -1;
1211
1212 dlen = attr->length - sizeof(*attr);
1213 if (buf)
1214 os_memcpy(buf, (attr + 1), dlen > len ? len : dlen);
1215 return dlen;
1216}
1217
1218
1219int radius_msg_get_attr_ptr(struct radius_msg *msg, u8 type, u8 **buf,
1220 size_t *len, const u8 *start)
1221{
1222 size_t i;
1223 struct radius_attr_hdr *attr = NULL, *tmp;
1224
1225 for (i = 0; i < msg->attr_used; i++) {
1226 tmp = radius_get_attr_hdr(msg, i);
1227 if (tmp->type == type &&
1228 (start == NULL || (u8 *) tmp > start)) {
1229 attr = tmp;
1230 break;
1231 }
1232 }
1233
1234 if (!attr)
1235 return -1;
1236
1237 *buf = (u8 *) (attr + 1);
1238 *len = attr->length - sizeof(*attr);
1239 return 0;
1240}
1241
1242
1243int radius_msg_count_attr(struct radius_msg *msg, u8 type, int min_len)
1244{
1245 size_t i;
1246 int count;
1247
1248 for (count = 0, i = 0; i < msg->attr_used; i++) {
1249 struct radius_attr_hdr *attr = radius_get_attr_hdr(msg, i);
1250 if (attr->type == type &&
1251 attr->length >= sizeof(struct radius_attr_hdr) + min_len)
1252 count++;
1253 }
1254
1255 return count;
1256}
1257
1258
1259struct radius_tunnel_attrs {
1260 int tag_used;
1261 int type; /* Tunnel-Type */
1262 int medium_type; /* Tunnel-Medium-Type */
1263 int vlanid;
1264};
1265
1266
1267/**
1268 * radius_msg_get_vlanid - Parse RADIUS attributes for VLAN tunnel information
1269 * @msg: RADIUS message
1270 * Returns: VLAN ID for the first tunnel configuration of -1 if none is found
1271 */
1272int radius_msg_get_vlanid(struct radius_msg *msg)
1273{
1274 struct radius_tunnel_attrs tunnel[RADIUS_TUNNEL_TAGS], *tun;
1275 size_t i;
1276 struct radius_attr_hdr *attr = NULL;
1277 const u8 *data;
1278 char buf[10];
1279 size_t dlen;
1280
1281 os_memset(&tunnel, 0, sizeof(tunnel));
1282
1283 for (i = 0; i < msg->attr_used; i++) {
1284 attr = radius_get_attr_hdr(msg, i);
1285 data = (const u8 *) (attr + 1);
1286 dlen = attr->length - sizeof(*attr);
1287 if (attr->length < 3)
1288 continue;
1289 if (data[0] >= RADIUS_TUNNEL_TAGS)
1290 tun = &tunnel[0];
1291 else
1292 tun = &tunnel[data[0]];
1293
1294 switch (attr->type) {
1295 case RADIUS_ATTR_TUNNEL_TYPE:
1296 if (attr->length != 6)
1297 break;
1298 tun->tag_used++;
1299 tun->type = WPA_GET_BE24(data + 1);
1300 break;
1301 case RADIUS_ATTR_TUNNEL_MEDIUM_TYPE:
1302 if (attr->length != 6)
1303 break;
1304 tun->tag_used++;
1305 tun->medium_type = WPA_GET_BE24(data + 1);
1306 break;
1307 case RADIUS_ATTR_TUNNEL_PRIVATE_GROUP_ID:
1308 if (data[0] < RADIUS_TUNNEL_TAGS) {
1309 data++;
1310 dlen--;
1311 }
1312 if (dlen >= sizeof(buf))
1313 break;
1314 os_memcpy(buf, data, dlen);
1315 buf[dlen] = '\0';
1316 tun->tag_used++;
1317 tun->vlanid = atoi(buf);
1318 break;
1319 }
1320 }
1321
1322 for (i = 0; i < RADIUS_TUNNEL_TAGS; i++) {
1323 tun = &tunnel[i];
1324 if (tun->tag_used &&
1325 tun->type == RADIUS_TUNNEL_TYPE_VLAN &&
1326 tun->medium_type == RADIUS_TUNNEL_MEDIUM_TYPE_802 &&
1327 tun->vlanid > 0)
1328 return tun->vlanid;
1329 }
1330
1331 return -1;
1332}
1333
1334
1335void radius_free_class(struct radius_class_data *c)
1336{
1337 size_t i;
1338 if (c == NULL)
1339 return;
1340 for (i = 0; i < c->count; i++)
1341 os_free(c->attr[i].data);
1342 os_free(c->attr);
1343 c->attr = NULL;
1344 c->count = 0;
1345}
1346
1347
1348int radius_copy_class(struct radius_class_data *dst,
1349 const struct radius_class_data *src)
1350{
1351 size_t i;
1352
1353 if (src->attr == NULL)
1354 return 0;
1355
1356 dst->attr = os_zalloc(src->count * sizeof(struct radius_attr_data));
1357 if (dst->attr == NULL)
1358 return -1;
1359
1360 dst->count = 0;
1361
1362 for (i = 0; i < src->count; i++) {
1363 dst->attr[i].data = os_malloc(src->attr[i].len);
1364 if (dst->attr[i].data == NULL)
1365 break;
1366 dst->count++;
1367 os_memcpy(dst->attr[i].data, src->attr[i].data,
1368 src->attr[i].len);
1369 dst->attr[i].len = src->attr[i].len;
1370 }
1371
1372 return 0;
1373}