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