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