e765b1b8f4e61eb729f9d6d931e029b89ec54717
[l2tpns.git] / radius.c
1 // L2TPNS Radius Stuff
2
3 char const *cvs_id_radius = "$Id: radius.c,v 1.36 2005/06/30 14:31:26 bodea Exp $";
4
5 #include <time.h>
6 #include <stdio.h>
7 #include <sys/types.h>
8 #include <sys/socket.h>
9 #include <malloc.h>
10 #include <string.h>
11 #include <fcntl.h>
12 #include <arpa/inet.h>
13 #include <ctype.h>
14 #include <netinet/in.h>
15 #include <errno.h>
16 #include "md5.h"
17 #include "constants.h"
18 #include "l2tpns.h"
19 #include "plugin.h"
20 #include "util.h"
21 #include "cluster.h"
22
23 extern radiust *radius;
24 extern sessiont *session;
25 extern tunnelt *tunnel;
26 extern configt *config;
27 extern int *radfds;
28 extern ip_filtert *ip_filters;
29
30 // Set up socket for radius requests
31 void initrad(void)
32 {
33 int i;
34 LOG(3, 0, 0, "Creating %d sockets for RADIUS queries\n", RADIUS_FDS);
35 radfds = calloc(sizeof(int), RADIUS_FDS);
36 for (i = 0; i < RADIUS_FDS; i++)
37 {
38 int flags;
39 radfds[i] = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
40 flags = fcntl(radfds[i], F_GETFL, 0);
41 fcntl(radfds[i], F_SETFL, flags | O_NONBLOCK);
42 }
43 }
44
45 void radiusclear(uint16_t r, sessionidt s)
46 {
47 if (s) sess_local[s].radius = 0;
48 memset(&radius[r], 0, sizeof(radius[r])); // radius[r].state = RADIUSNULL;
49 }
50
51 static uint16_t get_free_radius()
52 {
53 int count;
54 static uint32_t next_radius_id = 0;
55
56 for (count = MAXRADIUS; count > 0; --count)
57 {
58 ++next_radius_id; // Find the next ID to check.
59 if (next_radius_id >= MAXRADIUS)
60 next_radius_id = 1;
61
62 if (radius[next_radius_id].state == RADIUSNULL)
63 {
64 return next_radius_id;
65 }
66 }
67
68 LOG(0, 0, 0, "Can't find a free radius session! This is very bad!\n");
69 return 0;
70 }
71
72 uint16_t radiusnew(sessionidt s)
73 {
74 uint16_t r = sess_local[s].radius;
75
76 /* re-use */
77 if (r)
78 {
79 LOG(3, s, session[s].tunnel, "Re-used radius %d\n", r);
80 return r;
81 }
82
83 if (!(r = get_free_radius()))
84 {
85 LOG(1, s, session[s].tunnel, "No free RADIUS sessions\n");
86 STAT(radius_overflow);
87 return 0;
88 };
89
90 memset(&radius[r], 0, sizeof(radius[r]));
91 sess_local[s].radius = r;
92 radius[r].session = s;
93 radius[r].state = RADIUSWAIT;
94 radius[r].retry = TIME + 1200; // Wait at least 120 seconds to re-claim this.
95
96 random_data(radius[r].auth, sizeof(radius[r].auth));
97
98 LOG(3, s, session[s].tunnel, "Allocated radius %d\n", r);
99 return r;
100 }
101
102 // Send a RADIUS request
103 void radiussend(uint16_t r, uint8_t state)
104 {
105 struct sockaddr_in addr;
106 uint8_t b[4096]; // RADIUS packet
107 char pass[129];
108 int pl;
109 uint8_t *p;
110 sessionidt s;
111
112 CSTAT(radiussend);
113
114 s = radius[r].session;
115 if (!config->numradiusservers)
116 {
117 LOG(0, s, session[s].tunnel, "No RADIUS servers\n");
118 return;
119 }
120 if (!*config->radiussecret)
121 {
122 LOG(0, s, session[s].tunnel, "No RADIUS secret\n");
123 return;
124 }
125
126 if (state != RADIUSAUTH && !config->radius_accounting)
127 {
128 // Radius accounting is turned off
129 radiusclear(r, s);
130 return;
131 }
132
133 if (radius[r].state != state)
134 radius[r].try = 0;
135
136 radius[r].state = state;
137 radius[r].retry = backoff(radius[r].try++) + 20; // 3s, 4s, 6s, 10s...
138 LOG(4, s, session[s].tunnel, "Send RADIUS id %d sock %d state %s try %d\n",
139 r >> RADIUS_SHIFT, r & RADIUS_MASK,
140 radius_state(radius[r].state), radius[r].try);
141
142 if (radius[r].try > config->numradiusservers * 2)
143 {
144 if (s)
145 {
146 if (state == RADIUSAUTH)
147 sessionshutdown(s, "RADIUS timeout.", 3, 0);
148 else
149 {
150 LOG(1, s, session[s].tunnel, "RADIUS timeout, but in state %s so don't timeout session\n",
151 radius_state(state));
152 radiusclear(r, s);
153 }
154 STAT(radius_timeout);
155 }
156 else
157 {
158 STAT(radius_retries);
159 radius[r].state = RADIUSWAIT;
160 radius[r].retry = 100;
161 }
162 return;
163 }
164 // contruct RADIUS access request
165 switch (state)
166 {
167 case RADIUSAUTH:
168 b[0] = AccessRequest; // access request
169 break;
170 case RADIUSSTART:
171 case RADIUSSTOP:
172 case RADIUSINTERIM:
173 b[0] = AccountingRequest; // accounting request
174 break;
175 default:
176 LOG(0, 0, 0, "Unknown radius state %d\n", state);
177 }
178 b[1] = r >> RADIUS_SHIFT; // identifier
179 memcpy(b + 4, radius[r].auth, 16);
180 p = b + 20;
181 if (s)
182 {
183 *p = 1; // user name
184 p[1] = strlen(session[s].user) + 2;
185 strcpy(p + 2, session[s].user);
186 p += p[1];
187 }
188 if (state == RADIUSAUTH)
189 {
190 if (radius[r].chap)
191 {
192 *p = 3; // CHAP password
193 p[1] = 19; // length
194 p[2] = radius[r].id; // ID
195 memcpy(p + 3, radius[r].pass, 16); // response from CHAP request
196 p += p[1];
197 *p = 60; // CHAP Challenge
198 p[1] = 18; // length
199 memcpy(p + 2, radius[r].auth, 16);
200 p += p[1];
201 }
202 else
203 {
204 strcpy(pass, radius[r].pass);
205 pl = strlen(pass);
206 while (pl & 15)
207 pass[pl++] = 0; // pad
208 if (pl)
209 { // encrypt
210 hasht hash;
211 int p = 0;
212 while (p < pl)
213 {
214 MD5_CTX ctx;
215 MD5Init(&ctx);
216 MD5Update(&ctx, config->radiussecret, strlen(config->radiussecret));
217 if (p)
218 MD5Update(&ctx, pass + p - 16, 16);
219 else
220 MD5Update(&ctx, radius[r].auth, 16);
221 MD5Final(hash, &ctx);
222 do
223 {
224 pass[p] ^= hash[p & 15];
225 p++;
226 }
227 while (p & 15);
228 }
229 }
230 *p = 2; // password
231 p[1] = pl + 2;
232 if (pl)
233 memcpy(p + 2, pass, pl);
234 p += p[1];
235 }
236 }
237 else if (state == RADIUSSTART || state == RADIUSSTOP || state == RADIUSINTERIM)
238 { // accounting
239 *p = 40; // accounting type
240 p[1] = 6;
241 *(uint32_t *) (p + 2) = htonl(state - RADIUSSTART + 1); // start=1, stop=2, interim=3
242 p += p[1];
243 if (s)
244 {
245 *p = 44; // session ID
246 p[1] = 18;
247 sprintf(p + 2, "%08X%08X", session[s].unique_id, session[s].opened);
248 p += p[1];
249 if (state == RADIUSSTART)
250 { // start
251 *p = 41; // delay
252 p[1] = 6;
253 *(uint32_t *) (p + 2) = htonl(time(NULL) - session[s].opened);
254 p += p[1];
255 sess_local[s].last_interim = time_now; // Setup "first" Interim
256 }
257 else
258 { // stop, interim
259 *p = 42; // input octets
260 p[1] = 6;
261 *(uint32_t *) (p + 2) = htonl(session[s].cin);
262 p += p[1];
263
264 *p = 43; // output octets
265 p[1] = 6;
266 *(uint32_t *) (p + 2) = htonl(session[s].cout);
267 p += p[1];
268 if (state == RADIUSSTOP)
269 {
270 *p = 46; // session time
271 p[1] = 6;
272 *(uint32_t *) (p + 2) = htonl(time(NULL) - session[s].opened);
273 p += p[1];
274 }
275
276 *p = 47; // input packets
277 p[1] = 6;
278 *(uint32_t *) (p + 2) = htonl(session[s].pin);
279 p += p[1];
280
281 *p = 48; // output packets
282 p[1] = 6;
283 *(uint32_t *) (p + 2) = htonl(session[s].pout);
284 p += p[1];
285
286 *p = 52; // input gigawords
287 p[1] = 6;
288 *(uint32_t *) (p + 2) = htonl(session[s].cin_wrap);
289 p += p[1];
290
291 *p = 53; // output gigawords
292 p[1] = 6;
293 *(uint32_t *) (p + 2) = htonl(session[s].cout_wrap);
294 p += p[1];
295 }
296
297 if (session[s].snoop_ip && session[s].snoop_port)
298 {
299 *p = 26; // vendor-specific
300 *(uint32_t *) (p + 2) = htonl(9); // Cisco
301 p[6] = 1; // Cisco-AVPair
302 p[7] = 2 + sprintf(p + 8, "intercept=%s:%d",
303 fmtaddr(session[s].snoop_ip, 0), session[s].snoop_port);
304
305 p[1] = p[7] + 6;
306 p += p[1];
307 }
308 }
309 }
310 if (s)
311 {
312 *p = 5; // NAS-Port
313 p[1] = 6;
314 *(uint32_t *) (p + 2) = htonl(s);
315 p += p[1];
316 }
317 if (s && session[s].ip)
318 {
319 *p = 8; // Framed-IP-Address
320 p[1] = 6;
321 *(uint32_t *) (p + 2) = htonl(session[s].ip);
322 p += p[1];
323 }
324 if (*session[s].called)
325 {
326 *p = 30; // called
327 p[1] = strlen(session[s].called) + 2;
328 strcpy(p + 2, session[s].called);
329 p += p[1];
330 }
331 if (*radius[r].calling)
332 {
333 *p = 31; // calling
334 p[1] = strlen(radius[r].calling) + 2;
335 strcpy(p + 2, radius[r].calling);
336 p += p[1];
337 }
338 else if (*session[s].calling)
339 {
340 *p = 31; // calling
341 p[1] = strlen(session[s].calling) + 2;
342 strcpy(p + 2, session[s].calling);
343 p += p[1];
344 }
345 // NAS-IP-Address
346 *p = 4;
347 p[1] = 6;
348 *(uint32_t *)(p + 2) = config->bind_address;
349 p += p[1];
350
351 // All AVpairs added
352 *(uint16_t *) (b + 2) = htons(p - b);
353 if (state != RADIUSAUTH)
354 {
355 // Build auth for accounting packet
356 char z[16] = {0};
357 char hash[16] = {0};
358 MD5_CTX ctx;
359 MD5Init(&ctx);
360 MD5Update(&ctx, b, 4);
361 MD5Update(&ctx, z, 16);
362 MD5Update(&ctx, b + 20, (p - b) - 20);
363 MD5Update(&ctx, config->radiussecret, strlen(config->radiussecret));
364 MD5Final(hash, &ctx);
365 memcpy(b + 4, hash, 16);
366 memcpy(radius[r].auth, hash, 16);
367 }
368 memset(&addr, 0, sizeof(addr));
369 addr.sin_family = AF_INET;
370 *(uint32_t *) & addr.sin_addr = config->radiusserver[(radius[r].try - 1) % config->numradiusservers];
371 {
372 // get radius port
373 uint16_t port = config->radiusport[(radius[r].try - 1) % config->numradiusservers];
374 // assume RADIUS accounting port is the authentication port +1
375 addr.sin_port = htons((state == RADIUSAUTH) ? port : port+1);
376 }
377
378 LOG_HEX(5, "RADIUS Send", b, (p - b));
379 sendto(radfds[r & RADIUS_MASK], b, p - b, 0, (void *) &addr, sizeof(addr));
380 }
381
382 static void handle_avpair(sessionidt s, uint8_t *avp, int len)
383 {
384 char *key = avp;
385 char *value = memchr(avp, '=', len);
386 char tmp[2048] = "";
387
388 if (value)
389 {
390 *value++ = 0;
391 len -= value - key;
392 }
393 else
394 {
395 value = tmp;
396 len = 0;
397 }
398
399 // strip quotes
400 if (len > 2 && (*value == '"' || *value == '\'') && value[len - 1] == *value)
401 {
402 value++;
403 len--;
404 value[len - 1] = 0;
405 }
406 // copy and null terminate
407 else if (len < sizeof(tmp) - 1)
408 {
409 memcpy(tmp, value, len);
410 tmp[len] = 0;
411 value = tmp;
412 }
413 else
414 return;
415
416 // Run hooks
417 {
418 struct param_radius_response p = { &tunnel[session[s].tunnel], &session[s], key, value };
419 run_plugins(PLUGIN_RADIUS_RESPONSE, &p);
420 }
421 }
422
423 // process RADIUS response
424 void processrad(uint8_t *buf, int len, char socket_index)
425 {
426 uint8_t b[MAXCONTROL];
427 MD5_CTX ctx;
428 uint16_t r;
429 sessionidt s;
430 tunnelidt t = 0;
431 hasht hash;
432 uint8_t routes = 0;
433 int r_code;
434 int r_id;
435
436 CSTAT(processrad);
437
438 LOG_HEX(5, "RADIUS Response", buf, len);
439 if (len < 20 || len < ntohs(*(uint16_t *) (buf + 2)))
440 {
441 LOG(1, 0, 0, "Duff RADIUS response length %d\n", len);
442 return ;
443 }
444
445 r_code = buf[0]; // response type
446 r_id = buf[1]; // radius reply indentifier.
447
448 len = ntohs(*(uint16_t *) (buf + 2));
449 r = socket_index | (r_id << RADIUS_SHIFT);
450 s = radius[r].session;
451 LOG(3, s, session[s].tunnel, "Received %s, radius %d response for session %u (%s, id %d)\n",
452 radius_state(radius[r].state), r, s, radius_code(r_code), r_id);
453
454 if (!s && radius[r].state != RADIUSSTOP)
455 {
456 LOG(1, s, session[s].tunnel, " Unexpected RADIUS response\n");
457 return;
458 }
459 if (radius[r].state != RADIUSAUTH && radius[r].state != RADIUSSTART
460 && radius[r].state != RADIUSSTOP && radius[r].state != RADIUSINTERIM)
461 {
462 LOG(1, s, session[s].tunnel, " Unexpected RADIUS response\n");
463 return;
464 }
465 t = session[s].tunnel;
466 MD5Init(&ctx);
467 MD5Update(&ctx, buf, 4);
468 MD5Update(&ctx, radius[r].auth, 16);
469 MD5Update(&ctx, buf + 20, len - 20);
470 MD5Update(&ctx, config->radiussecret, strlen(config->radiussecret));
471 MD5Final(hash, &ctx);
472 do {
473 if (memcmp(hash, buf + 4, 16))
474 {
475 LOG(0, s, session[s].tunnel, " Incorrect auth on RADIUS response!! (wrong secret in radius config?)\n");
476 return; // Do nothing. On timeout, it will try the next radius server.
477 }
478
479 if ((radius[r].state == RADIUSAUTH && r_code != AccessAccept && r_code != AccessReject) ||
480 ((radius[r].state == RADIUSSTART || radius[r].state == RADIUSSTOP || radius[r].state == RADIUSINTERIM) && r_code != AccountingResponse))
481 {
482 LOG(1, s, session[s].tunnel, " Unexpected RADIUS response %s\n", radius_code(r_code));
483 return; // We got something we didn't expect. Let the timeouts take
484 // care off finishing the radius session if that's really correct.
485 }
486
487 if (radius[r].state == RADIUSAUTH)
488 {
489 // run post-auth plugin
490 struct param_post_auth packet = {
491 &tunnel[t],
492 &session[s],
493 session[s].user,
494 (r_code == AccessAccept),
495 radius[r].chap ? PPPCHAP : PPPPAP
496 };
497
498 run_plugins(PLUGIN_POST_AUTH, &packet);
499 r_code = packet.auth_allowed ? AccessAccept : AccessReject;
500
501 // process auth response
502 if (radius[r].chap)
503 {
504 // CHAP
505 uint8_t *p = makeppp(b, sizeof(b), 0, 0, t, s, PPPCHAP);
506 if (!p) return; // Abort!
507
508 *p = (r_code == AccessAccept) ? 3 : 4; // ack/nak
509 p[1] = radius[r].id;
510 *(uint16_t *) (p + 2) = ntohs(4); // no message
511 tunnelsend(b, (p - b) + 4, t); // send it
512
513 LOG(3, s, session[s].tunnel, " CHAP User %s authentication %s.\n", session[s].user,
514 (r_code == AccessAccept) ? "allowed" : "denied");
515 }
516 else
517 {
518 // PAP
519 uint8_t *p = makeppp(b, sizeof(b), 0, 0, t, s, PPPPAP);
520 if (!p) return; // Abort!
521
522 // ack/nak
523 *p = r_code;
524 p[1] = radius[r].id;
525 *(uint16_t *) (p + 2) = ntohs(5);
526 p[4] = 0; // no message
527 tunnelsend(b, (p - b) + 5, t); // send it
528
529 LOG(3, s, session[s].tunnel, " PAP User %s authentication %s.\n", session[s].user,
530 (r_code == AccessAccept) ? "allowed" : "denied");
531 }
532
533 if (r_code == AccessAccept)
534 {
535 // Login successful
536 // Extract IP, routes, etc
537 uint8_t *p = buf + 20;
538 uint8_t *e = buf + len;
539 for (; p + 2 <= e && p[1] && p + p[1] <= e; p += p[1])
540 {
541 if (*p == 8)
542 {
543 // Framed-IP-Address
544 if (p[1] < 6) continue;
545 session[s].ip = ntohl(*(uint32_t *) (p + 2));
546 session[s].ip_pool_index = -1;
547 LOG(3, s, session[s].tunnel, " Radius reply contains IP address %s\n",
548 fmtaddr(htonl(session[s].ip), 0));
549
550 if (session[s].ip == 0xFFFFFFFE)
551 session[s].ip = 0; // assign from pool
552 }
553 else if (*p == 135)
554 {
555 // DNS address
556 if (p[1] < 6) continue;
557 session[s].dns1 = ntohl(*(uint32_t *) (p + 2));
558 LOG(3, s, session[s].tunnel, " Radius reply contains primary DNS address %s\n",
559 fmtaddr(htonl(session[s].dns1), 0));
560 }
561 else if (*p == 136)
562 {
563 // DNS address
564 if (p[1] < 6) continue;
565 session[s].dns2 = ntohl(*(uint32_t *) (p + 2));
566 LOG(3, s, session[s].tunnel, " Radius reply contains secondary DNS address %s\n",
567 fmtaddr(htonl(session[s].dns2), 0));
568 }
569 else if (*p == 22)
570 {
571 // Framed-Route
572 in_addr_t ip = 0, mask = 0;
573 uint8_t u = 0;
574 uint8_t bits = 0;
575 uint8_t *n = p + 2;
576 uint8_t *e = p + p[1];
577 while (n < e && (isdigit(*n) || *n == '.'))
578 {
579 if (*n == '.')
580 {
581 ip = (ip << 8) + u;
582 u = 0;
583 }
584 else
585 u = u * 10 + *n - '0';
586 n++;
587 }
588 ip = (ip << 8) + u;
589 if (*n == '/')
590 {
591 n++;
592 while (n < e && isdigit(*n))
593 bits = bits * 10 + *n++ - '0';
594 mask = (( -1) << (32 - bits));
595 }
596 else if ((ip >> 24) < 128)
597 mask = 0xFF0000;
598 else if ((ip >> 24) < 192)
599 mask = 0xFFFF0000;
600 else
601 mask = 0xFFFFFF00;
602
603 if (routes == MAXROUTE)
604 {
605 LOG(1, s, session[s].tunnel, " Too many routes\n");
606 }
607 else if (ip)
608 {
609 LOG(3, s, session[s].tunnel, " Radius reply contains route for %s/%s\n",
610 fmtaddr(htonl(ip), 0), fmtaddr(htonl(mask), 1));
611
612 session[s].route[routes].ip = ip;
613 session[s].route[routes].mask = mask;
614 routes++;
615 }
616 }
617 else if (*p == 11)
618 {
619 // Filter-Id
620 char *filter = p + 2;
621 int l = p[1] - 2;
622 char *suffix;
623 int f;
624 uint8_t *fp = 0;
625
626 LOG(3, s, session[s].tunnel, " Radius reply contains Filter-Id \"%.*s\"\n", l, filter);
627 if ((suffix = memchr(filter, '.', l)))
628 {
629 int b = suffix - filter;
630 if (l - b == 3 && !memcmp("in", suffix+1, 2))
631 fp = &session[s].filter_in;
632 else if (l - b == 4 && !memcmp("out", suffix+1, 3))
633 fp = &session[s].filter_out;
634
635 l = b;
636 }
637
638 if (!fp)
639 {
640 LOG(3, s, session[s].tunnel, " Invalid filter\n");
641 continue;
642 }
643
644 if ((f = find_filter(filter, l)) < 0 || !*ip_filters[f].name)
645 {
646 LOG(3, s, session[s].tunnel, " Unknown filter\n");
647 }
648 else
649 {
650 *fp = f + 1;
651 ip_filters[f].used++;
652 }
653 }
654 else if (*p == 26 && p[1] >= 7)
655 {
656 // Vendor-Specific Attribute
657 int vendor = ntohl(*(int *)(p + 2));
658 char attrib = *(p + 6);
659 int attrib_length = *(p + 7) - 2;
660
661 LOG(3, s, session[s].tunnel, " Radius reply contains Vendor-Specific. Vendor=%d Attrib=%d Length=%d\n", vendor, attrib, attrib_length);
662 if (vendor != 9 || attrib != 1)
663 {
664 LOG(3, s, session[s].tunnel, " Unknown vendor-specific\n");
665 continue;
666 }
667
668 if (attrib_length > 0)
669 {
670 LOG(3, s, session[s].tunnel, " Cisco-AVPair value: %.*s\n",
671 attrib_length, p + 8);
672
673 handle_avpair(s, p + 8, attrib_length);
674 }
675 }
676 else if (*p == 99)
677 {
678 // Framed-IPv6-Route
679 struct in6_addr r6;
680 int prefixlen;
681 uint8_t *n = p + 2;
682 uint8_t *e = p + p[1];
683 uint8_t *m = strchr(n, '/');
684
685 *m++ = 0;
686 inet_pton(AF_INET6, n, &r6);
687
688 prefixlen = 0;
689 while (m < e && isdigit(*m)) {
690 prefixlen = prefixlen * 10 + *m++ - '0';
691 }
692
693 if (prefixlen)
694 {
695 LOG(3, s, session[s].tunnel,
696 " Radius reply contains route for %s/%d\n",
697 n, prefixlen);
698 session[s].ipv6route = r6;
699 session[s].ipv6prefixlen = prefixlen;
700 }
701 }
702 }
703 }
704 else if (r_code == AccessReject)
705 {
706 LOG(2, s, session[s].tunnel, " Authentication rejected for %s\n", session[s].user);
707 sessionkill(s, "Authentication rejected");
708 break;
709 }
710
711 if (!session[s].dns1 && config->default_dns1)
712 {
713 session[s].dns1 = htonl(config->default_dns1);
714 LOG(3, s, t, " Sending dns1 = %s\n", fmtaddr(config->default_dns1, 0));
715 }
716 if (!session[s].dns2 && config->default_dns2)
717 {
718 session[s].dns2 = htonl(config->default_dns2);
719 LOG(3, s, t, " Sending dns2 = %s\n", fmtaddr(config->default_dns2, 0));
720 }
721
722 // Valid Session, set it up
723 session[s].unique_id = 0;
724 sessionsetup(t, s);
725 }
726 else
727 {
728 // An ack for a stop or start record.
729 LOG(3, s, t, " RADIUS accounting ack recv in state %s\n", radius_state(radius[r].state));
730 break;
731 }
732 } while (0);
733
734 // finished with RADIUS
735 radiusclear(r, s);
736 }
737
738 // Send a retry for RADIUS/CHAP message
739 void radiusretry(uint16_t r)
740 {
741 sessionidt s = radius[r].session;
742 tunnelidt t = 0;
743
744 CSTAT(radiusretry);
745
746 if (s) t = session[s].tunnel;
747
748 switch (radius[r].state)
749 {
750 case RADIUSCHAP: // sending CHAP down PPP
751 sendchap(t, s);
752 break;
753 case RADIUSIPCP:
754 sendipcp(t, s); // send IPCP
755 break;
756 case RADIUSAUTH: // sending auth to RADIUS server
757 radiussend(r, RADIUSAUTH);
758 break;
759 case RADIUSSTART: // sending start accounting to RADIUS server
760 radiussend(r, RADIUSSTART);
761 break;
762 case RADIUSSTOP: // sending stop accounting to RADIUS server
763 radiussend(r, RADIUSSTOP);
764 break;
765 case RADIUSINTERIM: // sending interim accounting to RADIUS server
766 radiussend(r, RADIUSINTERIM);
767 break;
768 default:
769 case RADIUSNULL: // Not in use
770 case RADIUSWAIT: // waiting timeout before available, in case delayed reply from RADIUS server
771 // free up RADIUS task
772 radiusclear(r, s);
773 LOG(3, s, session[s].tunnel, "Freeing up radius session %d\n", r);
774 break;
775 }
776 }
777
778 extern int daefd;
779
780 void processdae(uint8_t *buf, int len, struct sockaddr_in *addr, int alen)
781 {
782 int i, r_code, r_id, length, attribute_length;
783 uint8_t vector[16], hash[16], *packet, attribute;
784 MD5_CTX ctx;
785 char username[MAXUSER] = "";
786 in_addr_t nas = 0;
787 in_addr_t ip = 0;
788 uint32_t port = 0;
789 uint32_t error = 0;
790 sessionidt s = 0;
791 tunnelidt t;
792 int fin = -1;
793 int fout = -1;
794 uint8_t *avpair[64];
795 int avpair_len[sizeof(avpair)/sizeof(*avpair)];
796 int avp = 0;
797 int auth_only = 0;
798 uint8_t *p;
799
800 LOG(3, 0, 0, "DAE request from %s\n", fmtaddr(addr->sin_addr.s_addr, 0));
801 LOG_HEX(5, "DAE Request", buf, len);
802
803 if (len < 20 || len < ntohs(*(uint16_t *) (buf + 2)))
804 {
805 LOG(1, 0, 0, "Duff DAE request length %d\n", len);
806 return;
807 }
808
809 r_code = buf[0]; // request type
810 r_id = buf[1]; // radius indentifier.
811
812 if (r_code != DisconnectRequest && r_code != CoARequest)
813 {
814 LOG(1, 0, 0, "Unrecognised DAE request %s\n", radius_code(r_code));
815 return;
816 }
817
818 if (!config->cluster_iam_master)
819 {
820 master_forward_dae_packet(buf, len, addr->sin_addr.s_addr, addr->sin_port);
821 return;
822 }
823
824 len = ntohs(*(uint16_t *) (buf + 2));
825
826 LOG(3, 0, 0, "Received DAE %s, id %d\n", radius_code(r_code), r_id);
827
828 // check authenticator
829 memcpy(vector, buf + 4, 16);
830 memset(buf + 4, 0, 16);
831
832 i = strlen(config->radiussecret);
833 if (i > 16) i = 16;
834
835 MD5Init(&ctx);
836 MD5Update(&ctx, buf, len);
837 MD5Update(&ctx, buf, config->radiussecret, i);
838 MD5Final(hash, &ctx);
839 if (memcmp(hash, vector, 16) != 0)
840 {
841 LOG(1, 0, 0, "Incorrect vector in DAE request (wrong secret in radius config?)\n");
842 return;
843 }
844
845 // unpack attributes
846 packet = buf + 20;
847 length = len - 20;
848
849 while (length > 0)
850 {
851 attribute = *packet++;
852 attribute_length = *packet++;
853 if (attribute_length < 2)
854 break;
855
856 length -= attribute_length;
857 attribute_length -= 2;
858 switch (attribute)
859 {
860 case 1: /* username */
861 len = attribute_length < MAXUSER ? attribute_length : MAXUSER - 1;
862 memcpy(username, packet, len);
863 username[len] = 0;
864 LOG(4, 0, 0, " Received DAE User-Name: %s\n", username);
865 break;
866
867 case 4: /* nas ip address */
868 nas = *(uint32_t *) packet; // net order
869 if (nas != config->bind_address)
870 error = 403; // NAS identification mismatch
871
872 LOG(4, 0, 0, " Received DAE NAS-IP-Address: %s\n", fmtaddr(nas, 0));
873 break;
874
875 case 5: /* nas port */
876 port = ntohl(*(uint32_t *) packet);
877 if (port < 1 || port > MAXSESSION)
878 error = 404;
879
880 LOG(4, 0, 0, " Received DAE NAS-Port: %u\n", port);
881 break;
882
883 case 6: /* service type */
884 {
885 uint32_t service_type = ntohl(*(uint32_t *) packet);
886 auth_only = service_type == 8; // Authenticate only
887
888 LOG(4, 0, 0, " Received DAE Service-Type: %u\n", service_type);
889 }
890 break;
891
892 case 8: /* ip address */
893 ip = *(uint32_t *) packet; // net order
894 LOG(4, 0, 0, " Received DAE Framed-IP-Address: %s\n", fmtaddr(ip, 0));
895 break;
896
897 case 11: /* filter id */
898 LOG(4, 0, 0, " Received DAE Filter-Id: %.*s\n", attribute_length, packet);
899 if (!(p = memchr(packet, '.', attribute_length)))
900 {
901 error = 404; // invalid request
902 break;
903 }
904
905 len = p - packet;
906 i = find_filter(packet, len);
907 if (i < 0 || !*ip_filters[i].name)
908 {
909 error = 404;
910 break;
911 }
912
913 if (!memcmp(p, ".in", attribute_length - len))
914 fin = i + 1;
915 else if (!memcmp(p, ".out", attribute_length - len))
916 fout = i + 1;
917 else
918 error = 404;
919
920 break;
921
922 case 26: /* vendor specific */
923 if (attribute_length >= 6
924 && ntohl(*(uint32_t *) packet) == 9 // Cisco
925 && *(packet + 4) == 1 // Cisco-AVPair
926 && *(packet + 5) >= 2) // length
927 {
928 int len = *(packet + 5) - 2;
929 uint8_t *a = packet + 6;
930
931 LOG(4, 0, 0, " Received DAE Cisco-AVPair: %.*s\n", len, a);
932 if (avp < sizeof(avpair)/sizeof(*avpair) - 1)
933 {
934 avpair[avp] = a;
935 avpair_len[avp++] = len;
936 }
937 }
938 break;
939 }
940
941 packet += attribute_length;
942 }
943
944 if (!error && auth_only)
945 {
946 if (fin != -1 || fout != -1 || avp)
947 error = 401; // unsupported attribute
948 else
949 error = 405; // unsupported service
950 }
951
952 if (!error && !(port || ip || *username))
953 error = 402; // missing attribute
954
955 // exact match for SID if given
956 if (!error && port)
957 {
958 s = port;
959 if (!session[s].opened)
960 error = 503; // not found
961 }
962
963 if (!error && ip)
964 {
965 // find/check session by IP
966 i = sessionbyip(ip);
967 if (!i || (s && s != i)) // not found or mismatching port
968 error = 503;
969 else
970 s = i;
971 }
972
973 if (!error && *username)
974 {
975 if (s)
976 {
977 if (strcmp(session[s].user, username))
978 error = 503;
979 }
980 else if (!(s = sessionbyuser(username)))
981 error = 503;
982 }
983
984 t = session[s].tunnel;
985
986 switch (r_code)
987 {
988 case DisconnectRequest: // Packet of Disconnect/Death
989 if (error)
990 {
991 r_code = DisconnectNAK;
992 break;
993 }
994
995 LOG(3, s, t, " DAE Disconnect %d (%s)\n", s, session[s].user);
996 r_code = DisconnectACK;
997
998 sessionshutdown(s, "Requested by PoD", 3, 0); // disconnect session
999 break;
1000
1001 case CoARequest: // Change of Authorization
1002 if (error)
1003 {
1004 r_code = CoANAK;
1005 break;
1006 }
1007
1008 LOG(3, s, t, " DAE Change %d (%s)\n", s, session[s].user);
1009 r_code = CoAACK;
1010
1011 // reset
1012 {
1013 struct param_radius_reset p = { &tunnel[session[s].tunnel], &session[s] };
1014 run_plugins(PLUGIN_RADIUS_RESET, &p);
1015 }
1016
1017 // apply filters
1018 if (fin == -1)
1019 fin = 0;
1020 else
1021 LOG(3, s, t, " Filter in %d (%s)\n", fin, ip_filters[fin - 1].name);
1022
1023 if (fout == -1)
1024 fout = 0;
1025 else
1026 LOG(3, s, t, " Filter out %d (%s)\n", fout, ip_filters[fout - 1].name);
1027
1028 filter_session(s, fin, fout);
1029
1030 // process cisco av-pair(s)
1031 for (i = 0; i < avp; i++)
1032 {
1033 LOG(3, s, t, " Cisco-AVPair: %.*s\n", avpair_len[i], avpair[i]);
1034 handle_avpair(s, avpair[i], avpair_len[i]);
1035 }
1036
1037 cluster_send_session(s);
1038 break;
1039 }
1040
1041 // send response
1042 packet = buf;
1043 *packet++ = r_code;
1044 *packet++ = r_id;
1045 packet += 2;
1046 memset(packet, 0, 16);
1047 packet += 16;
1048 len = 20;
1049
1050 // add attributes
1051 if (error)
1052 {
1053 // add error cause
1054 *packet++ = 101;
1055 *packet++ = 6;
1056 *(uint32_t *) packet = htonl(error);
1057 len += 6;
1058 }
1059
1060 *((uint16_t *)(buf + 2)) = htons(len);
1061
1062 // make vector
1063 i = strlen(config->radiussecret);
1064 if (i > 16) i = 16;
1065
1066 MD5Init(&ctx);
1067 MD5Update(&ctx, buf, len);
1068 MD5Update(&ctx, config->radiussecret, i);
1069 MD5Final(hash, &ctx);
1070 memcpy(buf + 4, hash, 16);
1071
1072 LOG(3, 0, 0, "Sending DAE %s, id=%d\n", radius_code(r_code), r_id);
1073
1074 // send DAE response
1075 if (sendto(daefd, buf, len, MSG_DONTWAIT | MSG_NOSIGNAL, (struct sockaddr *) addr, alen) < 0)
1076 LOG(0, 0, 0, "Error sending DAE response packet: %s\n", strerror(errno));
1077 }