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