0059fcc144e0625866f04bdd239fb8dbb0db7f09
[l2tpns.git] / radius.c
1 // L2TPNS Radius Stuff
2
3 char const *cvs_id_radius = "$Id: radius.c,v 1.43 2005/10/11 02:27:40 foonly 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 if (session[s].snoop_ip && session[s].snoop_port)
310 {
311 *p = 26; // vendor-specific
312 *(uint32_t *) (p + 2) = htonl(9); // Cisco
313 p[6] = 1; // Cisco-AVPair
314 p[7] = 2 + sprintf((char *) p + 8, "intercept=%s:%d",
315 fmtaddr(session[s].snoop_ip, 0), session[s].snoop_port);
316
317 p[1] = p[7] + 6;
318 p += p[1];
319 }
320 }
321 }
322 if (s)
323 {
324 *p = 5; // NAS-Port
325 p[1] = 6;
326 *(uint32_t *) (p + 2) = htonl(s);
327 p += p[1];
328 }
329 if (s && session[s].ip)
330 {
331 *p = 8; // Framed-IP-Address
332 p[1] = 6;
333 *(uint32_t *) (p + 2) = htonl(session[s].ip);
334 p += p[1];
335 }
336 if (*session[s].called)
337 {
338 *p = 30; // called
339 p[1] = strlen(session[s].called) + 2;
340 strcpy((char *) p + 2, session[s].called);
341 p += p[1];
342 }
343 if (*session[s].calling)
344 {
345 *p = 31; // calling
346 p[1] = strlen(session[s].calling) + 2;
347 strcpy((char *) p + 2, session[s].calling);
348 p += p[1];
349 }
350 // NAS-IP-Address
351 *p = 4;
352 p[1] = 6;
353 *(uint32_t *)(p + 2) = config->bind_address;
354 p += p[1];
355
356 // All AVpairs added
357 *(uint16_t *) (b + 2) = htons(p - b);
358 if (state != RADIUSAUTH)
359 {
360 // Build auth for accounting packet
361 calc_auth(b, p - b, zero, b + 4);
362 memcpy(radius[r].auth, b + 4, 16);
363 }
364 memset(&addr, 0, sizeof(addr));
365 addr.sin_family = AF_INET;
366 *(uint32_t *) & addr.sin_addr = config->radiusserver[(radius[r].try - 1) % config->numradiusservers];
367 {
368 // get radius port
369 uint16_t port = config->radiusport[(radius[r].try - 1) % config->numradiusservers];
370 // assume RADIUS accounting port is the authentication port +1
371 addr.sin_port = htons((state == RADIUSAUTH) ? port : port+1);
372 }
373
374 LOG_HEX(5, "RADIUS Send", b, (p - b));
375 sendto(radfds[r & RADIUS_MASK], b, p - b, 0, (void *) &addr, sizeof(addr));
376 }
377
378 static void handle_avpair(sessionidt s, uint8_t *avp, int len)
379 {
380 uint8_t *key = avp;
381 uint8_t *value = memchr(avp, '=', len);
382 uint8_t tmp[2048] = "";
383
384 if (value)
385 {
386 *value++ = 0;
387 len -= value - key;
388 }
389 else
390 {
391 value = tmp;
392 len = 0;
393 }
394
395 // strip quotes
396 if (len > 2 && (*value == '"' || *value == '\'') && value[len - 1] == *value)
397 {
398 value++;
399 len--;
400 value[len - 1] = 0;
401 }
402 // copy and null terminate
403 else if (len < sizeof(tmp) - 1)
404 {
405 memcpy(tmp, value, len);
406 tmp[len] = 0;
407 value = tmp;
408 }
409 else
410 return;
411
412 // Run hooks
413 {
414 struct param_radius_response p = { &tunnel[session[s].tunnel], &session[s], (char *) key, (char *) value };
415 run_plugins(PLUGIN_RADIUS_RESPONSE, &p);
416 }
417 }
418
419 // process RADIUS response
420 void processrad(uint8_t *buf, int len, char socket_index)
421 {
422 uint8_t b[MAXETHER];
423 uint16_t r;
424 sessionidt s;
425 tunnelidt t = 0;
426 hasht hash;
427 uint8_t routes = 0;
428 int r_code;
429 int r_id;
430
431 CSTAT(processrad);
432
433 LOG_HEX(5, "RADIUS Response", buf, len);
434 if (len < 20 || len < ntohs(*(uint16_t *) (buf + 2)))
435 {
436 LOG(1, 0, 0, "Duff RADIUS response length %d\n", len);
437 return ;
438 }
439
440 r_code = buf[0]; // response type
441 r_id = buf[1]; // radius reply indentifier.
442
443 len = ntohs(*(uint16_t *) (buf + 2));
444 r = socket_index | (r_id << RADIUS_SHIFT);
445 s = radius[r].session;
446 LOG(3, s, session[s].tunnel, "Received %s, radius %d response for session %u (%s, id %d)\n",
447 radius_state(radius[r].state), r, s, radius_code(r_code), r_id);
448
449 if (!s && radius[r].state != RADIUSSTOP)
450 {
451 LOG(1, s, session[s].tunnel, " Unexpected RADIUS response\n");
452 return;
453 }
454 if (radius[r].state != RADIUSAUTH && radius[r].state != RADIUSSTART
455 && radius[r].state != RADIUSSTOP && radius[r].state != RADIUSINTERIM)
456 {
457 LOG(1, s, session[s].tunnel, " Unexpected RADIUS response\n");
458 return;
459 }
460 t = session[s].tunnel;
461 calc_auth(buf, len, radius[r].auth, hash);
462 do {
463 if (memcmp(hash, buf + 4, 16))
464 {
465 LOG(0, s, session[s].tunnel, " Incorrect auth on RADIUS response!! (wrong secret in radius config?)\n");
466 return; // Do nothing. On timeout, it will try the next radius server.
467 }
468
469 if ((radius[r].state == RADIUSAUTH && r_code != AccessAccept && r_code != AccessReject) ||
470 ((radius[r].state == RADIUSSTART || radius[r].state == RADIUSSTOP || radius[r].state == RADIUSINTERIM) && r_code != AccountingResponse))
471 {
472 LOG(1, s, session[s].tunnel, " Unexpected RADIUS response %s\n", radius_code(r_code));
473 return; // We got something we didn't expect. Let the timeouts take
474 // care off finishing the radius session if that's really correct.
475 }
476
477 if (radius[r].state == RADIUSAUTH)
478 {
479 // run post-auth plugin
480 struct param_post_auth packet = {
481 &tunnel[t],
482 &session[s],
483 session[s].user,
484 (r_code == AccessAccept),
485 radius[r].chap ? PPPCHAP : PPPPAP
486 };
487
488 run_plugins(PLUGIN_POST_AUTH, &packet);
489 r_code = packet.auth_allowed ? AccessAccept : AccessReject;
490
491 // process auth response
492 if (radius[r].chap)
493 {
494 // CHAP
495 uint8_t *p = makeppp(b, sizeof(b), 0, 0, s, t, PPPCHAP);
496 if (!p) return; // Abort!
497
498 *p = (r_code == AccessAccept) ? 3 : 4; // ack/nak
499 p[1] = radius[r].id;
500 *(uint16_t *) (p + 2) = ntohs(4); // no message
501 tunnelsend(b, (p - b) + 4, t); // send it
502
503 LOG(3, s, session[s].tunnel, " CHAP User %s authentication %s.\n", session[s].user,
504 (r_code == AccessAccept) ? "allowed" : "denied");
505 }
506 else
507 {
508 // PAP
509 uint8_t *p = makeppp(b, sizeof(b), 0, 0, s, t, PPPPAP);
510 if (!p) return; // Abort!
511
512 // ack/nak
513 *p = r_code;
514 p[1] = radius[r].id;
515 *(uint16_t *) (p + 2) = ntohs(5);
516 p[4] = 0; // no message
517 tunnelsend(b, (p - b) + 5, t); // send it
518
519 LOG(3, s, session[s].tunnel, " PAP User %s authentication %s.\n", session[s].user,
520 (r_code == AccessAccept) ? "allowed" : "denied");
521 }
522
523 if (r_code == AccessAccept)
524 {
525 // Login successful
526 // Extract IP, routes, etc
527 uint8_t *p = buf + 20;
528 uint8_t *e = buf + len;
529 for (; p + 2 <= e && p[1] && p + p[1] <= e; p += p[1])
530 {
531 if (*p == 8)
532 {
533 // Framed-IP-Address
534 if (p[1] < 6) continue;
535 session[s].ip = ntohl(*(uint32_t *) (p + 2));
536 session[s].ip_pool_index = -1;
537 LOG(3, s, session[s].tunnel, " Radius reply contains IP address %s\n",
538 fmtaddr(htonl(session[s].ip), 0));
539
540 if (session[s].ip == 0xFFFFFFFE)
541 session[s].ip = 0; // assign from pool
542 }
543 else if (*p == 135)
544 {
545 // DNS address
546 if (p[1] < 6) continue;
547 session[s].dns1 = ntohl(*(uint32_t *) (p + 2));
548 LOG(3, s, session[s].tunnel, " Radius reply contains primary DNS address %s\n",
549 fmtaddr(htonl(session[s].dns1), 0));
550 }
551 else if (*p == 136)
552 {
553 // DNS address
554 if (p[1] < 6) continue;
555 session[s].dns2 = ntohl(*(uint32_t *) (p + 2));
556 LOG(3, s, session[s].tunnel, " Radius reply contains secondary DNS address %s\n",
557 fmtaddr(htonl(session[s].dns2), 0));
558 }
559 else if (*p == 22)
560 {
561 // Framed-Route
562 in_addr_t ip = 0, mask = 0;
563 uint8_t u = 0;
564 uint8_t bits = 0;
565 uint8_t *n = p + 2;
566 uint8_t *e = p + p[1];
567 while (n < e && (isdigit(*n) || *n == '.'))
568 {
569 if (*n == '.')
570 {
571 ip = (ip << 8) + u;
572 u = 0;
573 }
574 else
575 u = u * 10 + *n - '0';
576 n++;
577 }
578 ip = (ip << 8) + u;
579 if (*n == '/')
580 {
581 n++;
582 while (n < e && isdigit(*n))
583 bits = bits * 10 + *n++ - '0';
584 mask = (( -1) << (32 - bits));
585 }
586 else if ((ip >> 24) < 128)
587 mask = 0xFF0000;
588 else if ((ip >> 24) < 192)
589 mask = 0xFFFF0000;
590 else
591 mask = 0xFFFFFF00;
592
593 if (routes == MAXROUTE)
594 {
595 LOG(1, s, session[s].tunnel, " Too many routes\n");
596 }
597 else if (ip)
598 {
599 LOG(3, s, session[s].tunnel, " Radius reply contains route for %s/%s\n",
600 fmtaddr(htonl(ip), 0), fmtaddr(htonl(mask), 1));
601
602 session[s].route[routes].ip = ip;
603 session[s].route[routes].mask = mask;
604 routes++;
605 }
606 }
607 else if (*p == 11)
608 {
609 // Filter-Id
610 char *filter = (char *) p + 2;
611 int l = p[1] - 2;
612 char *suffix;
613 int f;
614 uint8_t *fp = 0;
615
616 LOG(3, s, session[s].tunnel, " Radius reply contains Filter-Id \"%.*s\"\n", l, filter);
617 if ((suffix = memchr(filter, '.', l)))
618 {
619 int b = suffix - filter;
620 if (l - b == 3 && !memcmp("in", suffix+1, 2))
621 fp = &session[s].filter_in;
622 else if (l - b == 4 && !memcmp("out", suffix+1, 3))
623 fp = &session[s].filter_out;
624
625 l = b;
626 }
627
628 if (!fp)
629 {
630 LOG(3, s, session[s].tunnel, " Invalid filter\n");
631 continue;
632 }
633
634 if ((f = find_filter(filter, l)) < 0 || !*ip_filters[f].name)
635 {
636 LOG(3, s, session[s].tunnel, " Unknown filter\n");
637 }
638 else
639 {
640 *fp = f + 1;
641 ip_filters[f].used++;
642 }
643 }
644 else if (*p == 26 && p[1] >= 7)
645 {
646 // Vendor-Specific Attribute
647 int vendor = ntohl(*(int *)(p + 2));
648 char attrib = *(p + 6);
649 int attrib_length = *(p + 7) - 2;
650
651 LOG(3, s, session[s].tunnel, " Radius reply contains Vendor-Specific. Vendor=%d Attrib=%d Length=%d\n", vendor, attrib, attrib_length);
652 if (vendor != 9 || attrib != 1)
653 {
654 LOG(3, s, session[s].tunnel, " Unknown vendor-specific\n");
655 continue;
656 }
657
658 if (attrib_length > 0)
659 {
660 LOG(3, s, session[s].tunnel, " Cisco-AVPair value: %.*s\n",
661 attrib_length, p + 8);
662
663 handle_avpair(s, p + 8, attrib_length);
664 }
665 }
666 else if (*p == 99)
667 {
668 // Framed-IPv6-Route
669 struct in6_addr r6;
670 int prefixlen;
671 uint8_t *n = p + 2;
672 uint8_t *e = p + p[1];
673 uint8_t *m = memchr(n, '/', e - p);
674
675 *m++ = 0;
676 inet_pton(AF_INET6, (char *) n, &r6);
677
678 prefixlen = 0;
679 while (m < e && isdigit(*m)) {
680 prefixlen = prefixlen * 10 + *m++ - '0';
681 }
682
683 if (prefixlen)
684 {
685 LOG(3, s, session[s].tunnel,
686 " Radius reply contains route for %s/%d\n",
687 n, prefixlen);
688 session[s].ipv6route = r6;
689 session[s].ipv6prefixlen = prefixlen;
690 }
691 }
692 }
693 }
694 else if (r_code == AccessReject)
695 {
696 LOG(2, s, session[s].tunnel, " Authentication rejected for %s\n", session[s].user);
697 sessionkill(s, "Authentication rejected");
698 break;
699 }
700
701 if (!session[s].dns1 && config->default_dns1)
702 {
703 session[s].dns1 = ntohl(config->default_dns1);
704 LOG(3, s, t, " Sending dns1 = %s\n", fmtaddr(config->default_dns1, 0));
705 }
706 if (!session[s].dns2 && config->default_dns2)
707 {
708 session[s].dns2 = ntohl(config->default_dns2);
709 LOG(3, s, t, " Sending dns2 = %s\n", fmtaddr(config->default_dns2, 0));
710 }
711
712 // Valid Session, set it up
713 session[s].unique_id = 0;
714 sessionsetup(s, t);
715 }
716 else
717 {
718 // An ack for a stop or start record.
719 LOG(3, s, t, " RADIUS accounting ack recv in state %s\n", radius_state(radius[r].state));
720 break;
721 }
722 } while (0);
723
724 // finished with RADIUS
725 radiusclear(r, s);
726 }
727
728 // Send a retry for RADIUS/CHAP message
729 void radiusretry(uint16_t r)
730 {
731 sessionidt s = radius[r].session;
732 tunnelidt t = 0;
733
734 CSTAT(radiusretry);
735
736 if (s) t = session[s].tunnel;
737
738 switch (radius[r].state)
739 {
740 case RADIUSCHAP: // sending CHAP down PPP
741 sendchap(s, t);
742 break;
743 case RADIUSAUTH: // sending auth to RADIUS server
744 case RADIUSSTART: // sending start accounting to RADIUS server
745 case RADIUSSTOP: // sending stop accounting to RADIUS server
746 case RADIUSINTERIM: // sending interim accounting to RADIUS server
747 radiussend(r, radius[r].state);
748 break;
749 default:
750 case RADIUSNULL: // Not in use
751 case RADIUSWAIT: // waiting timeout before available, in case delayed reply from RADIUS server
752 // free up RADIUS task
753 radiusclear(r, s);
754 LOG(3, s, session[s].tunnel, "Freeing up radius session %d\n", r);
755 break;
756 }
757 }
758
759 extern int daefd;
760
761 void processdae(uint8_t *buf, int len, struct sockaddr_in *addr, int alen)
762 {
763 int i, r_code, r_id, length, attribute_length;
764 uint8_t *packet, attribute;
765 hasht hash;
766 char username[MAXUSER] = "";
767 in_addr_t nas = 0;
768 in_addr_t ip = 0;
769 uint32_t port = 0;
770 uint32_t error = 0;
771 sessionidt s = 0;
772 tunnelidt t;
773 int fin = -1;
774 int fout = -1;
775 uint8_t *avpair[64];
776 int avpair_len[sizeof(avpair)/sizeof(*avpair)];
777 int avp = 0;
778 int auth_only = 0;
779 uint8_t *p;
780
781 LOG(3, 0, 0, "DAE request from %s\n", fmtaddr(addr->sin_addr.s_addr, 0));
782 LOG_HEX(5, "DAE Request", buf, len);
783
784 if (len < 20 || len < ntohs(*(uint16_t *) (buf + 2)))
785 {
786 LOG(1, 0, 0, "Duff DAE request length %d\n", len);
787 return;
788 }
789
790 r_code = buf[0]; // request type
791 r_id = buf[1]; // radius indentifier.
792
793 if (r_code != DisconnectRequest && r_code != CoARequest)
794 {
795 LOG(1, 0, 0, "Unrecognised DAE request %s\n", radius_code(r_code));
796 return;
797 }
798
799 if (!config->cluster_iam_master)
800 {
801 master_forward_dae_packet(buf, len, addr->sin_addr.s_addr, addr->sin_port);
802 return;
803 }
804
805 len = ntohs(*(uint16_t *) (buf + 2));
806
807 LOG(3, 0, 0, "Received DAE %s, id %d\n", radius_code(r_code), r_id);
808
809 // check authenticator
810 calc_auth(buf, len, zero, hash);
811 if (memcmp(hash, buf + 4, 16) != 0)
812 {
813 LOG(1, 0, 0, "Incorrect vector in DAE request (wrong secret in radius config?)\n");
814 return;
815 }
816
817 // unpack attributes
818 packet = buf + 20;
819 length = len - 20;
820
821 while (length > 0)
822 {
823 attribute = *packet++;
824 attribute_length = *packet++;
825 if (attribute_length < 2)
826 break;
827
828 length -= attribute_length;
829 attribute_length -= 2;
830 switch (attribute)
831 {
832 case 1: /* username */
833 len = attribute_length < MAXUSER ? attribute_length : MAXUSER - 1;
834 memcpy(username, packet, len);
835 username[len] = 0;
836 LOG(4, 0, 0, " Received DAE User-Name: %s\n", username);
837 break;
838
839 case 4: /* nas ip address */
840 nas = *(uint32_t *) packet; // net order
841 if (nas != config->bind_address)
842 error = 403; // NAS identification mismatch
843
844 LOG(4, 0, 0, " Received DAE NAS-IP-Address: %s\n", fmtaddr(nas, 0));
845 break;
846
847 case 5: /* nas port */
848 port = ntohl(*(uint32_t *) packet);
849 if (port < 1 || port > MAXSESSION)
850 error = 404;
851
852 LOG(4, 0, 0, " Received DAE NAS-Port: %u\n", port);
853 break;
854
855 case 6: /* service type */
856 {
857 uint32_t service_type = ntohl(*(uint32_t *) packet);
858 auth_only = service_type == 8; // Authenticate only
859
860 LOG(4, 0, 0, " Received DAE Service-Type: %u\n", service_type);
861 }
862 break;
863
864 case 8: /* ip address */
865 ip = *(uint32_t *) packet; // net order
866 LOG(4, 0, 0, " Received DAE Framed-IP-Address: %s\n", fmtaddr(ip, 0));
867 break;
868
869 case 11: /* filter id */
870 LOG(4, 0, 0, " Received DAE Filter-Id: %.*s\n", attribute_length, packet);
871 if (!(p = memchr(packet, '.', attribute_length)))
872 {
873 error = 404; // invalid request
874 break;
875 }
876
877 len = p - packet;
878 i = find_filter((char *) packet, len);
879 if (i < 0 || !*ip_filters[i].name)
880 {
881 error = 404;
882 break;
883 }
884
885 if (!memcmp(p, ".in", attribute_length - len))
886 fin = i + 1;
887 else if (!memcmp(p, ".out", attribute_length - len))
888 fout = i + 1;
889 else
890 error = 404;
891
892 break;
893
894 case 26: /* vendor specific */
895 if (attribute_length >= 6
896 && ntohl(*(uint32_t *) packet) == 9 // Cisco
897 && *(packet + 4) == 1 // Cisco-AVPair
898 && *(packet + 5) >= 2) // length
899 {
900 int len = *(packet + 5) - 2;
901 uint8_t *a = packet + 6;
902
903 LOG(4, 0, 0, " Received DAE Cisco-AVPair: %.*s\n", len, a);
904 if (avp < sizeof(avpair)/sizeof(*avpair) - 1)
905 {
906 avpair[avp] = a;
907 avpair_len[avp++] = len;
908 }
909 }
910 break;
911 }
912
913 packet += attribute_length;
914 }
915
916 if (!error && auth_only)
917 {
918 if (fin != -1 || fout != -1 || avp)
919 error = 401; // unsupported attribute
920 else
921 error = 405; // unsupported service
922 }
923
924 if (!error && !(port || ip || *username))
925 error = 402; // missing attribute
926
927 // exact match for SID if given
928 if (!error && port)
929 {
930 s = port;
931 if (!session[s].opened)
932 error = 503; // not found
933 }
934
935 if (!error && ip)
936 {
937 // find/check session by IP
938 i = sessionbyip(ip);
939 if (!i || (s && s != i)) // not found or mismatching port
940 error = 503;
941 else
942 s = i;
943 }
944
945 if (!error && *username)
946 {
947 if (s)
948 {
949 if (strcmp(session[s].user, username))
950 error = 503;
951 }
952 else if (!(s = sessionbyuser(username)))
953 error = 503;
954 }
955
956 t = session[s].tunnel;
957
958 switch (r_code)
959 {
960 case DisconnectRequest: // Packet of Disconnect/Death
961 if (error)
962 {
963 r_code = DisconnectNAK;
964 break;
965 }
966
967 LOG(3, s, t, " DAE Disconnect %d (%s)\n", s, session[s].user);
968 r_code = DisconnectACK;
969
970 sessionshutdown(s, "Requested by PoD", 3, 0); // disconnect session
971 break;
972
973 case CoARequest: // Change of Authorization
974 if (error)
975 {
976 r_code = CoANAK;
977 break;
978 }
979
980 LOG(3, s, t, " DAE Change %d (%s)\n", s, session[s].user);
981 r_code = CoAACK;
982
983 // reset
984 {
985 struct param_radius_reset p = { &tunnel[session[s].tunnel], &session[s] };
986 run_plugins(PLUGIN_RADIUS_RESET, &p);
987 }
988
989 // apply filters
990 if (fin == -1)
991 fin = 0;
992 else
993 LOG(3, s, t, " Filter in %d (%s)\n", fin, ip_filters[fin - 1].name);
994
995 if (fout == -1)
996 fout = 0;
997 else
998 LOG(3, s, t, " Filter out %d (%s)\n", fout, ip_filters[fout - 1].name);
999
1000 filter_session(s, fin, fout);
1001
1002 // process cisco av-pair(s)
1003 for (i = 0; i < avp; i++)
1004 {
1005 LOG(3, s, t, " Cisco-AVPair: %.*s\n", avpair_len[i], avpair[i]);
1006 handle_avpair(s, avpair[i], avpair_len[i]);
1007 }
1008
1009 cluster_send_session(s);
1010 break;
1011 }
1012
1013 // send response
1014 packet = buf;
1015 *packet++ = r_code;
1016 *packet++ = r_id;
1017 // skip len + auth
1018 packet += 2 + 16;
1019 len = packet - buf;
1020
1021 // add attributes
1022 if (error)
1023 {
1024 // add error cause
1025 *packet++ = 101;
1026 *packet++ = 6;
1027 *(uint32_t *) packet = htonl(error);
1028 len += 6;
1029 }
1030
1031 *((uint16_t *)(buf + 2)) = htons(len);
1032
1033 // make vector
1034 calc_auth(buf, len, hash, buf + 4);
1035
1036 LOG(3, 0, 0, "Sending DAE %s, id=%d\n", radius_code(r_code), r_id);
1037
1038 // send DAE response
1039 if (sendto(daefd, buf, len, MSG_DONTWAIT | MSG_NOSIGNAL, (struct sockaddr *) addr, alen) < 0)
1040 LOG(0, 0, 0, "Error sending DAE response packet: %s\n", strerror(errno));
1041 }