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