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