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