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