c804023cd29be3e3f7d76b25ec4dc123e786812a
[l2tpns.git] / radius.c
1 // L2TPNS Radius Stuff
2
3 char const *cvs_id_radius = "$Id: radius.c,v 1.52 2006-07-01 12:40:17 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 uint16_t port = 0;
49 uint16_t min = config->radius_bind_min;
50 uint16_t max = config->radius_bind_max;
51 int inc = 1;
52 struct sockaddr_in addr;
53
54 if (min)
55 {
56 port = min;
57 if (!max)
58 max = ~0 - 1;
59 }
60 else if (max) /* no minimum specified, bind from max down */
61 {
62 port = max;
63 min = 1;
64 inc = -1;
65 }
66
67 LOG(3, 0, 0, "Creating %d sockets for RADIUS queries\n", RADIUS_FDS);
68 radfds = calloc(sizeof(int), RADIUS_FDS);
69 for (i = 0; i < RADIUS_FDS; i++)
70 {
71 int flags;
72 radfds[i] = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
73 flags = fcntl(radfds[i], F_GETFL, 0);
74 fcntl(radfds[i], F_SETFL, flags | O_NONBLOCK);
75
76 if (port)
77 {
78 int b;
79
80 memset(&addr, 0, sizeof(addr));
81 addr.sin_family = AF_INET;
82 addr.sin_addr.s_addr = INADDR_ANY;
83
84 do {
85 addr.sin_port = htons(port);
86 if ((b = bind(radfds[i], (struct sockaddr *) &addr, sizeof(addr))) < 0)
87 {
88 if ((port += inc) < min || port > max)
89 {
90 LOG(0, 0, 0, "Can't bind RADIUS socket in range %u-%u\n", min, max);
91 exit(1);
92 }
93 }
94 } while (b < 0);
95 }
96 }
97 }
98
99 void radiusclear(uint16_t r, sessionidt s)
100 {
101 if (s) sess_local[s].radius = 0;
102 memset(&radius[r], 0, sizeof(radius[r])); // radius[r].state = RADIUSNULL;
103 }
104
105 static uint16_t get_free_radius()
106 {
107 int count;
108 static uint32_t next_radius_id = 0;
109
110 for (count = MAXRADIUS; count > 0; --count)
111 {
112 ++next_radius_id; // Find the next ID to check.
113 if (next_radius_id >= MAXRADIUS)
114 next_radius_id = 1;
115
116 if (radius[next_radius_id].state == RADIUSNULL)
117 {
118 return next_radius_id;
119 }
120 }
121
122 LOG(0, 0, 0, "Can't find a free radius session! This is very bad!\n");
123 return 0;
124 }
125
126 uint16_t radiusnew(sessionidt s)
127 {
128 uint16_t r = sess_local[s].radius;
129
130 /* re-use */
131 if (r)
132 {
133 LOG(3, s, session[s].tunnel, "Re-used radius %d\n", r);
134 return r;
135 }
136
137 if (!(r = get_free_radius()))
138 {
139 LOG(1, s, session[s].tunnel, "No free RADIUS sessions\n");
140 STAT(radius_overflow);
141 return 0;
142 };
143
144 memset(&radius[r], 0, sizeof(radius[r]));
145 sess_local[s].radius = r;
146 radius[r].session = s;
147 radius[r].state = RADIUSWAIT;
148 radius[r].retry = TIME + 1200; // Wait at least 120 seconds to re-claim this.
149
150 random_data(radius[r].auth, sizeof(radius[r].auth));
151
152 LOG(3, s, session[s].tunnel, "Allocated radius %d\n", r);
153 return r;
154 }
155
156 // Send a RADIUS request
157 void radiussend(uint16_t r, uint8_t state)
158 {
159 struct sockaddr_in addr;
160 uint8_t b[4096]; // RADIUS packet
161 char pass[129];
162 int pl;
163 uint8_t *p;
164 sessionidt s;
165
166 CSTAT(radiussend);
167
168 s = radius[r].session;
169 if (!config->numradiusservers)
170 {
171 LOG(0, s, session[s].tunnel, "No RADIUS servers\n");
172 return;
173 }
174 if (!*config->radiussecret)
175 {
176 LOG(0, s, session[s].tunnel, "No RADIUS secret\n");
177 return;
178 }
179
180 if (state != RADIUSAUTH && !config->radius_accounting)
181 {
182 // Radius accounting is turned off
183 radiusclear(r, s);
184 return;
185 }
186
187 if (radius[r].state != state)
188 radius[r].try = 0;
189
190 radius[r].state = state;
191 radius[r].retry = backoff(radius[r].try++) + 20; // 3s, 4s, 6s, 10s...
192 LOG(4, s, session[s].tunnel, "Send RADIUS id %d sock %d state %s try %d\n",
193 r >> RADIUS_SHIFT, r & RADIUS_MASK,
194 radius_state(radius[r].state), radius[r].try);
195
196 if (radius[r].try > config->numradiusservers * 2)
197 {
198 if (s)
199 {
200 if (state == RADIUSAUTH)
201 sessionshutdown(s, "RADIUS timeout.", CDN_ADMIN_DISC, TERM_REAUTHENTICATION_FAILURE);
202 else
203 {
204 LOG(1, s, session[s].tunnel, "RADIUS timeout, but in state %s so don't timeout session\n",
205 radius_state(state));
206 radiusclear(r, s);
207 }
208 STAT(radius_timeout);
209 }
210 else
211 {
212 STAT(radius_retries);
213 radius[r].state = RADIUSWAIT;
214 radius[r].retry = 100;
215 }
216 return;
217 }
218 // contruct RADIUS access request
219 switch (state)
220 {
221 case RADIUSAUTH:
222 b[0] = AccessRequest; // access request
223 break;
224 case RADIUSSTART:
225 case RADIUSSTOP:
226 case RADIUSINTERIM:
227 b[0] = AccountingRequest; // accounting request
228 break;
229 default:
230 LOG(0, 0, 0, "Unknown radius state %d\n", state);
231 }
232 b[1] = r >> RADIUS_SHIFT; // identifier
233 memcpy(b + 4, radius[r].auth, 16);
234 p = b + 20;
235 if (s)
236 {
237 *p = 1; // user name
238 p[1] = strlen(session[s].user) + 2;
239 strcpy((char *) p + 2, session[s].user);
240 p += p[1];
241 }
242 if (state == RADIUSAUTH)
243 {
244 if (radius[r].chap)
245 {
246 *p = 3; // CHAP password
247 p[1] = 19; // length
248 p[2] = radius[r].id; // ID
249 memcpy(p + 3, radius[r].pass, 16); // response from CHAP request
250 p += p[1];
251 *p = 60; // CHAP Challenge
252 p[1] = 18; // length
253 memcpy(p + 2, radius[r].auth, 16);
254 p += p[1];
255 }
256 else
257 {
258 strcpy(pass, radius[r].pass);
259 pl = strlen(pass);
260 while (pl & 15)
261 pass[pl++] = 0; // pad
262 if (pl)
263 { // encrypt
264 hasht hash;
265 int p = 0;
266 while (p < pl)
267 {
268 MD5_CTX ctx;
269 MD5_Init(&ctx);
270 MD5_Update(&ctx, config->radiussecret, strlen(config->radiussecret));
271 if (p)
272 MD5_Update(&ctx, pass + p - 16, 16);
273 else
274 MD5_Update(&ctx, radius[r].auth, 16);
275 MD5_Final(hash, &ctx);
276 do
277 {
278 pass[p] ^= hash[p & 15];
279 p++;
280 }
281 while (p & 15);
282 }
283 }
284 *p = 2; // password
285 p[1] = pl + 2;
286 if (pl)
287 memcpy(p + 2, pass, pl);
288 p += p[1];
289 }
290 }
291 else // accounting
292 {
293 *p = 40; // accounting type
294 p[1] = 6;
295 *(uint32_t *) (p + 2) = htonl(state - RADIUSSTART + 1); // start=1, stop=2, interim=3
296 p += p[1];
297 if (s)
298 {
299 *p = 44; // session ID
300 p[1] = 18;
301 sprintf((char *) p + 2, "%08X%08X", session[s].unique_id, session[s].opened);
302 p += p[1];
303 if (state == RADIUSSTART)
304 { // start
305 *p = 41; // delay
306 p[1] = 6;
307 *(uint32_t *) (p + 2) = htonl(time(NULL) - session[s].opened);
308 p += p[1];
309 sess_local[s].last_interim = time_now; // Setup "first" Interim
310 }
311 else
312 { // stop, interim
313 *p = 42; // input octets
314 p[1] = 6;
315 *(uint32_t *) (p + 2) = htonl(session[s].cin);
316 p += p[1];
317
318 *p = 43; // output octets
319 p[1] = 6;
320 *(uint32_t *) (p + 2) = htonl(session[s].cout);
321 p += p[1];
322
323 *p = 46; // session time
324 p[1] = 6;
325 *(uint32_t *) (p + 2) = htonl(time(NULL) - session[s].opened);
326 p += p[1];
327
328 *p = 47; // input packets
329 p[1] = 6;
330 *(uint32_t *) (p + 2) = htonl(session[s].pin);
331 p += p[1];
332
333 *p = 48; // output packets
334 p[1] = 6;
335 *(uint32_t *) (p + 2) = htonl(session[s].pout);
336 p += p[1];
337
338 *p = 52; // input gigawords
339 p[1] = 6;
340 *(uint32_t *) (p + 2) = htonl(session[s].cin_wrap);
341 p += p[1];
342
343 *p = 53; // output gigawords
344 p[1] = 6;
345 *(uint32_t *) (p + 2) = htonl(session[s].cout_wrap);
346 p += p[1];
347
348 if (state == RADIUSSTOP && radius[r].term_cause)
349 {
350 *p = 49; // acct-terminate-cause
351 p[1] = 6;
352 *(uint32_t *) (p + 2) = htonl(radius[r].term_cause);
353 p += p[1];
354
355 if (radius[r].term_msg)
356 {
357 *p = 26; // vendor-specific
358 *(uint32_t *) (p + 2) = htonl(9); // Cisco
359 p[6] = 1; // Cisco-AVPair
360 p[7] = 2 + sprintf((char *) p + 8, "disc-cause-ext=%s", radius[r].term_msg);
361 p[1] = p[7] + 6;
362 p += p[1];
363 }
364 }
365 }
366
367 {
368 struct param_radius_account acct = { &tunnel[session[s].tunnel], &session[s], &p };
369 run_plugins(PLUGIN_RADIUS_ACCOUNT, &acct);
370 }
371 }
372 }
373
374 if (s)
375 {
376 *p = 5; // NAS-Port
377 p[1] = 6;
378 *(uint32_t *) (p + 2) = htonl(s);
379 p += p[1];
380
381 *p = 6; // Service-Type
382 p[1] = 6;
383 *(uint32_t *) (p + 2) = htonl(2); // Framed-User
384 p += p[1];
385
386 *p = 7; // Framed-Protocol
387 p[1] = 6;
388 *(uint32_t *) (p + 2) = htonl(1); // PPP
389 p += p[1];
390
391 if (session[s].ip)
392 {
393 *p = 8; // Framed-IP-Address
394 p[1] = 6;
395 *(uint32_t *) (p + 2) = htonl(session[s].ip);
396 p += p[1];
397 }
398
399 if (session[s].route[0].ip)
400 {
401 int r;
402 for (r = 0; s && r < MAXROUTE && session[s].route[r].ip; r++)
403 {
404 int width = 32;
405 if (session[s].route[r].mask)
406 {
407 int mask = session[s].route[r].mask;
408 while (!(mask & 1))
409 {
410 width--;
411 mask >>= 1;
412 }
413 }
414
415 *p = 22; // Framed-Route
416 p[1] = sprintf((char *) p + 2, "%s/%d %s 1",
417 fmtaddr(htonl(session[s].route[r].ip), 0),
418 width, fmtaddr(htonl(session[s].ip), 1)) + 2;
419
420 p += p[1];
421 }
422 }
423
424 if (session[s].session_timeout)
425 {
426 *p = 27; // Session-Timeout
427 p[1] = 6;
428 *(uint32_t *) (p + 2) = htonl(session[s].session_timeout);
429 p += p[1];
430 }
431
432 if (session[s].idle_timeout)
433 {
434 *p = 28; // Idle-Timeout
435 p[1] = 6;
436 *(uint32_t *) (p + 2) = htonl(session[s].idle_timeout);
437 p += p[1];
438 }
439
440 if (*session[s].called)
441 {
442 *p = 30; // called
443 p[1] = strlen(session[s].called) + 2;
444 strcpy((char *) p + 2, session[s].called);
445 p += p[1];
446 }
447
448 if (*session[s].calling)
449 {
450 *p = 31; // calling
451 p[1] = strlen(session[s].calling) + 2;
452 strcpy((char *) p + 2, session[s].calling);
453 p += p[1];
454 }
455 }
456
457 // NAS-IP-Address
458 *p = 4;
459 p[1] = 6;
460 *(uint32_t *)(p + 2) = config->bind_address;
461 p += p[1];
462
463 // All AVpairs added
464 *(uint16_t *) (b + 2) = htons(p - b);
465 if (state != RADIUSAUTH)
466 {
467 // Build auth for accounting packet
468 calc_auth(b, p - b, zero, b + 4);
469 memcpy(radius[r].auth, b + 4, 16);
470 }
471 memset(&addr, 0, sizeof(addr));
472 addr.sin_family = AF_INET;
473 *(uint32_t *) & addr.sin_addr = config->radiusserver[(radius[r].try - 1) % config->numradiusservers];
474 {
475 // get radius port
476 uint16_t port = config->radiusport[(radius[r].try - 1) % config->numradiusservers];
477 // assume RADIUS accounting port is the authentication port +1
478 addr.sin_port = htons((state == RADIUSAUTH) ? port : port+1);
479 }
480
481 LOG_HEX(5, "RADIUS Send", b, (p - b));
482 sendto(radfds[r & RADIUS_MASK], b, p - b, 0, (void *) &addr, sizeof(addr));
483 }
484
485 static void handle_avpair(sessionidt s, uint8_t *avp, int len)
486 {
487 uint8_t *key = avp;
488 uint8_t *value = memchr(avp, '=', len);
489 uint8_t tmp[2048] = "";
490
491 if (value)
492 {
493 *value++ = 0;
494 len -= value - key;
495 }
496 else
497 {
498 value = tmp;
499 len = 0;
500 }
501
502 // strip quotes
503 if (len > 2 && (*value == '"' || *value == '\'') && value[len - 1] == *value)
504 {
505 value++;
506 len--;
507 value[len - 1] = 0;
508 }
509 // copy and null terminate
510 else if (len < sizeof(tmp) - 1)
511 {
512 memcpy(tmp, value, len);
513 tmp[len] = 0;
514 value = tmp;
515 }
516 else
517 return;
518
519 // Run hooks
520 {
521 struct param_radius_response p = { &tunnel[session[s].tunnel], &session[s], (char *) key, (char *) value };
522 run_plugins(PLUGIN_RADIUS_RESPONSE, &p);
523 }
524 }
525
526 // process RADIUS response
527 void processrad(uint8_t *buf, int len, char socket_index)
528 {
529 uint8_t b[MAXETHER];
530 uint16_t r;
531 sessionidt s;
532 tunnelidt t = 0;
533 hasht hash;
534 uint8_t routes = 0;
535 int r_code;
536 int r_id;
537
538 CSTAT(processrad);
539
540 LOG_HEX(5, "RADIUS Response", buf, len);
541 if (len < 20 || len < ntohs(*(uint16_t *) (buf + 2)))
542 {
543 LOG(1, 0, 0, "Duff RADIUS response length %d\n", len);
544 return ;
545 }
546
547 r_code = buf[0]; // response type
548 r_id = buf[1]; // radius reply indentifier.
549
550 len = ntohs(*(uint16_t *) (buf + 2));
551 r = socket_index | (r_id << RADIUS_SHIFT);
552 s = radius[r].session;
553 LOG(3, s, session[s].tunnel, "Received %s, radius %d response for session %u (%s, id %d)\n",
554 radius_state(radius[r].state), r, s, radius_code(r_code), r_id);
555
556 if (!s && radius[r].state != RADIUSSTOP)
557 {
558 LOG(1, s, session[s].tunnel, " Unexpected RADIUS response\n");
559 return;
560 }
561 if (radius[r].state != RADIUSAUTH && radius[r].state != RADIUSSTART
562 && radius[r].state != RADIUSSTOP && radius[r].state != RADIUSINTERIM)
563 {
564 LOG(1, s, session[s].tunnel, " Unexpected RADIUS response\n");
565 return;
566 }
567 t = session[s].tunnel;
568 calc_auth(buf, len, radius[r].auth, hash);
569 do {
570 if (memcmp(hash, buf + 4, 16))
571 {
572 LOG(0, s, session[s].tunnel, " Incorrect auth on RADIUS response!! (wrong secret in radius config?)\n");
573 return; // Do nothing. On timeout, it will try the next radius server.
574 }
575
576 if ((radius[r].state == RADIUSAUTH && r_code != AccessAccept && r_code != AccessReject) ||
577 ((radius[r].state == RADIUSSTART || radius[r].state == RADIUSSTOP || radius[r].state == RADIUSINTERIM) && r_code != AccountingResponse))
578 {
579 LOG(1, s, session[s].tunnel, " Unexpected RADIUS response %s\n", radius_code(r_code));
580 return; // We got something we didn't expect. Let the timeouts take
581 // care off finishing the radius session if that's really correct.
582 }
583
584 if (radius[r].state == RADIUSAUTH)
585 {
586 // run post-auth plugin
587 struct param_post_auth packet = {
588 &tunnel[t],
589 &session[s],
590 session[s].user,
591 (r_code == AccessAccept),
592 radius[r].chap ? PPPCHAP : PPPPAP
593 };
594
595 run_plugins(PLUGIN_POST_AUTH, &packet);
596 r_code = packet.auth_allowed ? AccessAccept : AccessReject;
597
598 // process auth response
599 if (radius[r].chap)
600 {
601 // CHAP
602 uint8_t *p = makeppp(b, sizeof(b), 0, 0, s, t, PPPCHAP, 0, 0, 0);
603 if (!p) return; // Abort!
604
605 *p = (r_code == AccessAccept) ? 3 : 4; // ack/nak
606 p[1] = radius[r].id;
607 *(uint16_t *) (p + 2) = ntohs(4); // no message
608 tunnelsend(b, (p - b) + 4, t); // send it
609
610 LOG(3, s, session[s].tunnel, " CHAP User %s authentication %s.\n", session[s].user,
611 (r_code == AccessAccept) ? "allowed" : "denied");
612 }
613 else
614 {
615 // PAP
616 uint8_t *p = makeppp(b, sizeof(b), 0, 0, s, t, PPPPAP, 0, 0, 0);
617 if (!p) return; // Abort!
618
619 // ack/nak
620 *p = r_code;
621 p[1] = radius[r].id;
622 *(uint16_t *) (p + 2) = ntohs(5);
623 p[4] = 0; // no message
624 tunnelsend(b, (p - b) + 5, t); // send it
625
626 LOG(3, s, session[s].tunnel, " PAP User %s authentication %s.\n", session[s].user,
627 (r_code == AccessAccept) ? "allowed" : "denied");
628 }
629
630 if (r_code == AccessAccept)
631 {
632 // Login successful
633 // Extract IP, routes, etc
634 uint8_t *p = buf + 20;
635 uint8_t *e = buf + len;
636 for (; p + 2 <= e && p[1] && p + p[1] <= e; p += p[1])
637 {
638 if (*p == 8)
639 {
640 // Framed-IP-Address
641 if (p[1] < 6) continue;
642 session[s].ip = ntohl(*(uint32_t *) (p + 2));
643 session[s].ip_pool_index = -1;
644 LOG(3, s, session[s].tunnel, " Radius reply contains IP address %s\n",
645 fmtaddr(htonl(session[s].ip), 0));
646
647 if (session[s].ip == 0xFFFFFFFE)
648 session[s].ip = 0; // assign from pool
649 }
650 else if (*p == 135)
651 {
652 // DNS address
653 if (p[1] < 6) continue;
654 session[s].dns1 = ntohl(*(uint32_t *) (p + 2));
655 LOG(3, s, session[s].tunnel, " Radius reply contains primary DNS address %s\n",
656 fmtaddr(htonl(session[s].dns1), 0));
657 }
658 else if (*p == 27)
659 {
660 // Session timeout
661 if (p[1] < 6) {
662 LOG(2, s, session[s].tunnel, "Error: Received Session timeout with length %d < 6\n", p[1]);
663 continue;
664 }
665
666 session[s].timeout = ntohl(*(uint32_t *) (p + 2));
667 LOG(3, s, session[s].tunnel, " Radius reply contains Session timeout %d\n", session[s].timeout);
668 if (!session[s].timeout)
669 sessionshutdown(s, "Session timeout is zero", CDN_ADMIN_DISC, TERM_SESSION_TIMEOUT);
670 }
671 else if (*p == 136)
672 {
673 // DNS address
674 if (p[1] < 6) continue;
675 session[s].dns2 = ntohl(*(uint32_t *) (p + 2));
676 LOG(3, s, session[s].tunnel, " Radius reply contains secondary DNS address %s\n",
677 fmtaddr(htonl(session[s].dns2), 0));
678 }
679 else if (*p == 22)
680 {
681 // Framed-Route
682 in_addr_t ip = 0, mask = 0;
683 uint8_t u = 0;
684 uint8_t bits = 0;
685 uint8_t *n = p + 2;
686 uint8_t *e = p + p[1];
687 while (n < e && (isdigit(*n) || *n == '.'))
688 {
689 if (*n == '.')
690 {
691 ip = (ip << 8) + u;
692 u = 0;
693 }
694 else
695 u = u * 10 + *n - '0';
696 n++;
697 }
698 ip = (ip << 8) + u;
699 if (*n == '/')
700 {
701 n++;
702 while (n < e && isdigit(*n))
703 bits = bits * 10 + *n++ - '0';
704 mask = (( -1) << (32 - bits));
705 }
706 else if ((ip >> 24) < 128)
707 mask = 0xFF0000;
708 else if ((ip >> 24) < 192)
709 mask = 0xFFFF0000;
710 else
711 mask = 0xFFFFFF00;
712
713 if (routes == MAXROUTE)
714 {
715 LOG(1, s, session[s].tunnel, " Too many routes\n");
716 }
717 else if (ip)
718 {
719 LOG(3, s, session[s].tunnel, " Radius reply contains route for %s/%s\n",
720 fmtaddr(htonl(ip), 0), fmtaddr(htonl(mask), 1));
721
722 session[s].route[routes].ip = ip;
723 session[s].route[routes].mask = mask;
724 routes++;
725 }
726 }
727 else if (*p == 11)
728 {
729 // Filter-Id
730 char *filter = (char *) p + 2;
731 int l = p[1] - 2;
732 char *suffix;
733 int f;
734 uint8_t *fp = 0;
735
736 LOG(3, s, session[s].tunnel, " Radius reply contains Filter-Id \"%.*s\"\n", l, filter);
737 if ((suffix = memchr(filter, '.', l)))
738 {
739 int b = suffix - filter;
740 if (l - b == 3 && !memcmp("in", suffix+1, 2))
741 fp = &session[s].filter_in;
742 else if (l - b == 4 && !memcmp("out", suffix+1, 3))
743 fp = &session[s].filter_out;
744
745 l = b;
746 }
747
748 if (!fp)
749 {
750 LOG(3, s, session[s].tunnel, " Invalid filter\n");
751 continue;
752 }
753
754 if ((f = find_filter(filter, l)) < 0 || !*ip_filters[f].name)
755 {
756 LOG(3, s, session[s].tunnel, " Unknown filter\n");
757 }
758 else
759 {
760 *fp = f + 1;
761 ip_filters[f].used++;
762 }
763 }
764 else if (*p == 27)
765 {
766 // Session-Timeout
767 uint32_t to = ntohl(*(uint32_t *)(p + 2));
768
769 LOG(3, s, session[s].tunnel, " Radius reply contains Session-Timeout = %u\n", to);
770 if (to > 0)
771 {
772 session[s].session_timeout = to;
773 }
774 }
775 else if (*p == 28)
776 {
777 // Idle-Timeout
778 uint32_t to = ntohl(*(uint32_t *)(p + 2));
779
780 LOG(3, s, session[s].tunnel, " Radius reply contains Idle-Timeout = %u\n", to);
781 if (to > 0)
782 {
783 session[s].idle_timeout = to;
784 }
785 }
786 else if (*p == 26 && p[1] >= 7)
787 {
788 // Vendor-Specific Attribute
789 int vendor = ntohl(*(int *)(p + 2));
790 char attrib = *(p + 6);
791 int attrib_length = *(p + 7) - 2;
792
793 LOG(3, s, session[s].tunnel, " Radius reply contains Vendor-Specific. Vendor=%d Attrib=%d Length=%d\n", vendor, attrib, attrib_length);
794 if (vendor != 9 || attrib != 1)
795 {
796 LOG(3, s, session[s].tunnel, " Unknown vendor-specific\n");
797 continue;
798 }
799
800 if (attrib_length > 0)
801 {
802 LOG(3, s, session[s].tunnel, " Cisco-AVPair value: %.*s\n",
803 attrib_length, p + 8);
804
805 handle_avpair(s, p + 8, attrib_length);
806 }
807 }
808 else if (*p == 99)
809 {
810 // Framed-IPv6-Route
811 struct in6_addr r6;
812 int prefixlen;
813 uint8_t *n = p + 2;
814 uint8_t *e = p + p[1];
815 uint8_t *m = memchr(n, '/', e - p);
816
817 *m++ = 0;
818 inet_pton(AF_INET6, (char *) n, &r6);
819
820 prefixlen = 0;
821 while (m < e && isdigit(*m)) {
822 prefixlen = prefixlen * 10 + *m++ - '0';
823 }
824
825 if (prefixlen)
826 {
827 LOG(3, s, session[s].tunnel,
828 " Radius reply contains route for %s/%d\n",
829 n, prefixlen);
830 session[s].ipv6route = r6;
831 session[s].ipv6prefixlen = prefixlen;
832 }
833 }
834 }
835 }
836 else if (r_code == AccessReject)
837 {
838 LOG(2, s, session[s].tunnel, " Authentication rejected for %s\n", session[s].user);
839 sessionkill(s, "Authentication rejected");
840 break;
841 }
842
843 if (!session[s].dns1 && config->default_dns1)
844 {
845 session[s].dns1 = ntohl(config->default_dns1);
846 LOG(3, s, t, " Sending dns1 = %s\n", fmtaddr(config->default_dns1, 0));
847 }
848 if (!session[s].dns2 && config->default_dns2)
849 {
850 session[s].dns2 = ntohl(config->default_dns2);
851 LOG(3, s, t, " Sending dns2 = %s\n", fmtaddr(config->default_dns2, 0));
852 }
853
854 // Valid Session, set it up
855 session[s].unique_id = 0;
856 sessionsetup(s, t);
857 }
858 else
859 {
860 // An ack for a stop or start record.
861 LOG(3, s, t, " RADIUS accounting ack recv in state %s\n", radius_state(radius[r].state));
862 break;
863 }
864 } while (0);
865
866 // finished with RADIUS
867 radiusclear(r, s);
868 }
869
870 // Send a retry for RADIUS/CHAP message
871 void radiusretry(uint16_t r)
872 {
873 sessionidt s = radius[r].session;
874 tunnelidt t = 0;
875
876 CSTAT(radiusretry);
877
878 if (s) t = session[s].tunnel;
879
880 switch (radius[r].state)
881 {
882 case RADIUSCHAP: // sending CHAP down PPP
883 sendchap(s, t);
884 break;
885 case RADIUSAUTH: // sending auth to RADIUS server
886 case RADIUSSTART: // sending start accounting to RADIUS server
887 case RADIUSSTOP: // sending stop accounting to RADIUS server
888 case RADIUSINTERIM: // sending interim accounting to RADIUS server
889 radiussend(r, radius[r].state);
890 break;
891 default:
892 case RADIUSNULL: // Not in use
893 case RADIUSWAIT: // waiting timeout before available, in case delayed reply from RADIUS server
894 // free up RADIUS task
895 radiusclear(r, s);
896 LOG(3, s, session[s].tunnel, "Freeing up radius session %d\n", r);
897 break;
898 }
899 }
900
901 extern int daefd;
902
903 void processdae(uint8_t *buf, int len, struct sockaddr_in *addr, int alen, struct in_addr *local)
904 {
905 int i, r_code, r_id, length, attribute_length;
906 uint8_t *packet, attribute;
907 hasht hash;
908 char username[MAXUSER] = "";
909 in_addr_t nas = 0;
910 in_addr_t ip = 0;
911 uint32_t port = 0;
912 uint32_t error = 0;
913 sessionidt s = 0;
914 tunnelidt t;
915 int fin = -1;
916 int fout = -1;
917 uint8_t *avpair[64];
918 int avpair_len[sizeof(avpair)/sizeof(*avpair)];
919 int avp = 0;
920 int auth_only = 0;
921 uint8_t *p;
922
923 LOG(3, 0, 0, "DAE request from %s\n", fmtaddr(addr->sin_addr.s_addr, 0));
924 LOG_HEX(5, "DAE Request", buf, len);
925
926 if (len < 20 || len < ntohs(*(uint16_t *) (buf + 2)))
927 {
928 LOG(1, 0, 0, "Duff DAE request length %d\n", len);
929 return;
930 }
931
932 r_code = buf[0]; // request type
933 r_id = buf[1]; // radius indentifier.
934
935 if (r_code != DisconnectRequest && r_code != CoARequest)
936 {
937 LOG(1, 0, 0, "Unrecognised DAE request %s\n", radius_code(r_code));
938 return;
939 }
940
941 if (!config->cluster_iam_master)
942 {
943 master_forward_dae_packet(buf, len, addr->sin_addr.s_addr, addr->sin_port);
944 return;
945 }
946
947 len = ntohs(*(uint16_t *) (buf + 2));
948
949 LOG(3, 0, 0, "Received DAE %s, id %d\n", radius_code(r_code), r_id);
950
951 // check authenticator
952 calc_auth(buf, len, zero, hash);
953 if (memcmp(hash, buf + 4, 16) != 0)
954 {
955 LOG(1, 0, 0, "Incorrect vector in DAE request (wrong secret in radius config?)\n");
956 return;
957 }
958
959 // unpack attributes
960 packet = buf + 20;
961 length = len - 20;
962
963 while (length > 0)
964 {
965 attribute = *packet++;
966 attribute_length = *packet++;
967 if (attribute_length < 2)
968 break;
969
970 length -= attribute_length;
971 attribute_length -= 2;
972 switch (attribute)
973 {
974 case 1: /* username */
975 len = attribute_length < MAXUSER ? attribute_length : MAXUSER - 1;
976 memcpy(username, packet, len);
977 username[len] = 0;
978 LOG(4, 0, 0, " Received DAE User-Name: %s\n", username);
979 break;
980
981 case 4: /* nas ip address */
982 nas = *(uint32_t *) packet; // net order
983 if (nas != config->bind_address)
984 error = 403; // NAS identification mismatch
985
986 LOG(4, 0, 0, " Received DAE NAS-IP-Address: %s\n", fmtaddr(nas, 0));
987 break;
988
989 case 5: /* nas port */
990 port = ntohl(*(uint32_t *) packet);
991 if (port < 1 || port > MAXSESSION)
992 error = 404;
993
994 LOG(4, 0, 0, " Received DAE NAS-Port: %u\n", port);
995 break;
996
997 case 6: /* service type */
998 {
999 uint32_t service_type = ntohl(*(uint32_t *) packet);
1000 auth_only = service_type == 8; // Authenticate only
1001
1002 LOG(4, 0, 0, " Received DAE Service-Type: %u\n", service_type);
1003 }
1004 break;
1005
1006 case 8: /* ip address */
1007 ip = *(uint32_t *) packet; // net order
1008 LOG(4, 0, 0, " Received DAE Framed-IP-Address: %s\n", fmtaddr(ip, 0));
1009 break;
1010
1011 case 11: /* filter id */
1012 LOG(4, 0, 0, " Received DAE Filter-Id: %.*s\n", attribute_length, packet);
1013 if (!(p = memchr(packet, '.', attribute_length)))
1014 {
1015 error = 404; // invalid request
1016 break;
1017 }
1018
1019 len = p - packet;
1020 i = find_filter((char *) packet, len);
1021 if (i < 0 || !*ip_filters[i].name)
1022 {
1023 error = 404;
1024 break;
1025 }
1026
1027 if (!memcmp(p, ".in", attribute_length - len))
1028 fin = i + 1;
1029 else if (!memcmp(p, ".out", attribute_length - len))
1030 fout = i + 1;
1031 else
1032 error = 404;
1033
1034 break;
1035
1036 case 26: /* vendor specific */
1037 if (attribute_length >= 6
1038 && ntohl(*(uint32_t *) packet) == 9 // Cisco
1039 && *(packet + 4) == 1 // Cisco-AVPair
1040 && *(packet + 5) >= 2) // length
1041 {
1042 int len = *(packet + 5) - 2;
1043 uint8_t *a = packet + 6;
1044
1045 LOG(4, 0, 0, " Received DAE Cisco-AVPair: %.*s\n", len, a);
1046 if (avp < sizeof(avpair)/sizeof(*avpair) - 1)
1047 {
1048 avpair[avp] = a;
1049 avpair_len[avp++] = len;
1050 }
1051 }
1052 break;
1053 }
1054
1055 packet += attribute_length;
1056 }
1057
1058 if (!error && auth_only)
1059 {
1060 if (fin != -1 || fout != -1 || avp)
1061 error = 401; // unsupported attribute
1062 else
1063 error = 405; // unsupported service
1064 }
1065
1066 if (!error && !(port || ip || *username))
1067 error = 402; // missing attribute
1068
1069 // exact match for SID if given
1070 if (!error && port)
1071 {
1072 s = port;
1073 if (!session[s].opened)
1074 error = 503; // not found
1075 }
1076
1077 if (!error && ip)
1078 {
1079 // find/check session by IP
1080 i = sessionbyip(ip);
1081 if (!i || (s && s != i)) // not found or mismatching port
1082 error = 503;
1083 else
1084 s = i;
1085 }
1086
1087 if (!error && *username)
1088 {
1089 if (s)
1090 {
1091 if (strcmp(session[s].user, username))
1092 error = 503;
1093 }
1094 else if (!(s = sessionbyuser(username)))
1095 error = 503;
1096 }
1097
1098 t = session[s].tunnel;
1099
1100 switch (r_code)
1101 {
1102 case DisconnectRequest: // Packet of Disconnect/Death
1103 if (error)
1104 {
1105 r_code = DisconnectNAK;
1106 break;
1107 }
1108
1109 LOG(3, s, t, " DAE Disconnect %d (%s)\n", s, session[s].user);
1110 r_code = DisconnectACK;
1111
1112 sessionshutdown(s, "Requested by PoD", CDN_ADMIN_DISC, TERM_ADMIN_RESET); // disconnect session
1113 break;
1114
1115 case CoARequest: // Change of Authorization
1116 if (error)
1117 {
1118 r_code = CoANAK;
1119 break;
1120 }
1121
1122 LOG(3, s, t, " DAE Change %d (%s)\n", s, session[s].user);
1123 r_code = CoAACK;
1124
1125 // reset
1126 {
1127 struct param_radius_reset p = { &tunnel[session[s].tunnel], &session[s] };
1128 run_plugins(PLUGIN_RADIUS_RESET, &p);
1129 }
1130
1131 // apply filters
1132 if (fin == -1)
1133 fin = 0;
1134 else
1135 LOG(3, s, t, " Filter in %d (%s)\n", fin, ip_filters[fin - 1].name);
1136
1137 if (fout == -1)
1138 fout = 0;
1139 else
1140 LOG(3, s, t, " Filter out %d (%s)\n", fout, ip_filters[fout - 1].name);
1141
1142 filter_session(s, fin, fout);
1143
1144 // process cisco av-pair(s)
1145 for (i = 0; i < avp; i++)
1146 {
1147 LOG(3, s, t, " Cisco-AVPair: %.*s\n", avpair_len[i], avpair[i]);
1148 handle_avpair(s, avpair[i], avpair_len[i]);
1149 }
1150
1151 cluster_send_session(s);
1152 break;
1153 }
1154
1155 // send response
1156 packet = buf;
1157 *packet++ = r_code;
1158 *packet++ = r_id;
1159 // skip len + auth
1160 packet += 2 + 16;
1161 len = packet - buf;
1162
1163 // add attributes
1164 if (error)
1165 {
1166 // add error cause
1167 *packet++ = 101;
1168 *packet++ = 6;
1169 *(uint32_t *) packet = htonl(error);
1170 len += 6;
1171 }
1172
1173 *((uint16_t *)(buf + 2)) = htons(len);
1174
1175 // make vector
1176 calc_auth(buf, len, hash, buf + 4);
1177
1178 LOG(3, 0, 0, "Sending DAE %s, id=%d\n", radius_code(r_code), r_id);
1179
1180 // send DAE response
1181 if (sendtofrom(daefd, buf, len, MSG_DONTWAIT | MSG_NOSIGNAL, (struct sockaddr *) addr, alen, local) < 0)
1182 LOG(0, 0, 0, "Error sending DAE response packet: %s\n", strerror(errno));
1183 }