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