add Makefile, cleanup for -std=c99
[l2tpns.git] / radius.c
1 // L2TPNS Radius Stuff
2
3 char const *cvs_id_radius = "$Id: radius.c,v 1.55 2006/08/02 14:17:30 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 == 26 && p[1] >= 7)
639 {
640 // Vendor-Specific Attribute
641 uint32_t vendor = ntohl(*(int *)(p + 2));
642 uint8_t attrib = *(p + 6);
643 int attrib_length = *(p + 7) - 2;
644
645 LOG(4, s, session[s].tunnel, " Radius reply contains Vendor-Specific. Vendor=%u Attrib=%u Length=%d\n", vendor, attrib, attrib_length);
646 if (vendor == 9 && attrib == 1) // Cisco-AVPair
647 {
648 if (attrib_length < 0) continue;
649 LOG(3, s, session[s].tunnel, " Cisco-AVPair value: %.*s\n",
650 attrib_length, p + 8);
651
652 handle_avpair(s, p + 8, attrib_length);
653 continue;
654 }
655 else if (vendor == 529 && attrib >= 135 && attrib <= 136) // Ascend
656 {
657 // handle old-format ascend DNS attributes below
658 p += 6;
659 }
660 else
661 {
662 LOG(3, s, session[s].tunnel, " Unknown vendor-specific\n");
663 continue;
664 }
665 }
666
667 if (*p == 8)
668 {
669 // Framed-IP-Address
670 if (p[1] < 6) continue;
671 session[s].ip = ntohl(*(uint32_t *) (p + 2));
672 session[s].ip_pool_index = -1;
673 LOG(3, s, session[s].tunnel, " Radius reply contains IP address %s\n",
674 fmtaddr(htonl(session[s].ip), 0));
675
676 if (session[s].ip == 0xFFFFFFFE)
677 session[s].ip = 0; // assign from pool
678 }
679 else if (*p == 135)
680 {
681 // DNS address
682 if (p[1] < 6) continue;
683 session[s].dns1 = ntohl(*(uint32_t *) (p + 2));
684 LOG(3, s, session[s].tunnel, " Radius reply contains primary DNS address %s\n",
685 fmtaddr(htonl(session[s].dns1), 0));
686 }
687 else if (*p == 136)
688 {
689 // DNS address
690 if (p[1] < 6) continue;
691 session[s].dns2 = ntohl(*(uint32_t *) (p + 2));
692 LOG(3, s, session[s].tunnel, " Radius reply contains secondary DNS address %s\n",
693 fmtaddr(htonl(session[s].dns2), 0));
694 }
695 else if (*p == 22)
696 {
697 // Framed-Route
698 in_addr_t ip = 0, mask = 0;
699 uint8_t u = 0;
700 uint8_t bits = 0;
701 uint8_t *n = p + 2;
702 uint8_t *e = p + p[1];
703 while (n < e && (isdigit(*n) || *n == '.'))
704 {
705 if (*n == '.')
706 {
707 ip = (ip << 8) + u;
708 u = 0;
709 }
710 else
711 u = u * 10 + *n - '0';
712 n++;
713 }
714 ip = (ip << 8) + u;
715 if (*n == '/')
716 {
717 n++;
718 while (n < e && isdigit(*n))
719 bits = bits * 10 + *n++ - '0';
720 mask = (( -1) << (32 - bits));
721 }
722 else if ((ip >> 24) < 128)
723 mask = 0xFF0000;
724 else if ((ip >> 24) < 192)
725 mask = 0xFFFF0000;
726 else
727 mask = 0xFFFFFF00;
728
729 if (routes == MAXROUTE)
730 {
731 LOG(1, s, session[s].tunnel, " Too many routes\n");
732 }
733 else if (ip)
734 {
735 LOG(3, s, session[s].tunnel, " Radius reply contains route for %s/%s\n",
736 fmtaddr(htonl(ip), 0), fmtaddr(htonl(mask), 1));
737
738 session[s].route[routes].ip = ip;
739 session[s].route[routes].mask = mask;
740 routes++;
741 }
742 }
743 else if (*p == 11)
744 {
745 // Filter-Id
746 char *filter = (char *) p + 2;
747 int l = p[1] - 2;
748 char *suffix;
749 int f;
750 uint8_t *fp = 0;
751
752 LOG(3, s, session[s].tunnel, " Radius reply contains Filter-Id \"%.*s\"\n", l, filter);
753 if ((suffix = memchr(filter, '.', l)))
754 {
755 int b = suffix - filter;
756 if (l - b == 3 && !memcmp("in", suffix+1, 2))
757 fp = &session[s].filter_in;
758 else if (l - b == 4 && !memcmp("out", suffix+1, 3))
759 fp = &session[s].filter_out;
760
761 l = b;
762 }
763
764 if (!fp)
765 {
766 LOG(3, s, session[s].tunnel, " Invalid filter\n");
767 continue;
768 }
769
770 if ((f = find_filter(filter, l)) < 0 || !*ip_filters[f].name)
771 {
772 LOG(3, s, session[s].tunnel, " Unknown filter\n");
773 }
774 else
775 {
776 *fp = f + 1;
777 ip_filters[f].used++;
778 }
779 }
780 else if (*p == 27)
781 {
782 // Session-Timeout
783 if (p[1] < 6) continue;
784 session[s].session_timeout = ntohl(*(uint32_t *)(p + 2));
785 LOG(3, s, session[s].tunnel, " Radius reply contains Session-Timeout = %u\n", session[s].session_timeout);
786 }
787 else if (*p == 28)
788 {
789 // Idle-Timeout
790 if (p[1] < 6) continue;
791 session[s].idle_timeout = ntohl(*(uint32_t *)(p + 2));
792 LOG(3, s, session[s].tunnel, " Radius reply contains Idle-Timeout = %u\n", session[s].idle_timeout);
793 }
794 else if (*p == 99)
795 {
796 // Framed-IPv6-Route
797 struct in6_addr r6;
798 int prefixlen;
799 uint8_t *n = p + 2;
800 uint8_t *e = p + p[1];
801 uint8_t *m = memchr(n, '/', e - p);
802
803 *m++ = 0;
804 inet_pton(AF_INET6, (char *) n, &r6);
805
806 prefixlen = 0;
807 while (m < e && isdigit(*m)) {
808 prefixlen = prefixlen * 10 + *m++ - '0';
809 }
810
811 if (prefixlen)
812 {
813 LOG(3, s, session[s].tunnel,
814 " Radius reply contains route for %s/%d\n",
815 n, prefixlen);
816 session[s].ipv6route = r6;
817 session[s].ipv6prefixlen = prefixlen;
818 }
819 }
820 }
821 }
822 else if (r_code == AccessReject)
823 {
824 LOG(2, s, session[s].tunnel, " Authentication rejected for %s\n", session[s].user);
825 sessionkill(s, "Authentication rejected");
826 break;
827 }
828
829 if (!session[s].dns1 && config->default_dns1)
830 {
831 session[s].dns1 = ntohl(config->default_dns1);
832 LOG(3, s, t, " Sending dns1 = %s\n", fmtaddr(config->default_dns1, 0));
833 }
834 if (!session[s].dns2 && config->default_dns2)
835 {
836 session[s].dns2 = ntohl(config->default_dns2);
837 LOG(3, s, t, " Sending dns2 = %s\n", fmtaddr(config->default_dns2, 0));
838 }
839
840 // Valid Session, set it up
841 session[s].unique_id = 0;
842 sessionsetup(s, t);
843 }
844 else
845 {
846 // An ack for a stop or start record.
847 LOG(3, s, t, " RADIUS accounting ack recv in state %s\n", radius_state(radius[r].state));
848 break;
849 }
850 } while (0);
851
852 // finished with RADIUS
853 radiusclear(r, s);
854 }
855
856 // Send a retry for RADIUS/CHAP message
857 void radiusretry(uint16_t r)
858 {
859 sessionidt s = radius[r].session;
860 tunnelidt t = 0;
861
862 CSTAT(radiusretry);
863
864 if (s) t = session[s].tunnel;
865
866 switch (radius[r].state)
867 {
868 case RADIUSCHAP: // sending CHAP down PPP
869 sendchap(s, t);
870 break;
871 case RADIUSAUTH: // sending auth to RADIUS server
872 case RADIUSSTART: // sending start accounting to RADIUS server
873 case RADIUSSTOP: // sending stop accounting to RADIUS server
874 case RADIUSINTERIM: // sending interim accounting to RADIUS server
875 radiussend(r, radius[r].state);
876 break;
877 default:
878 case RADIUSNULL: // Not in use
879 case RADIUSWAIT: // waiting timeout before available, in case delayed reply from RADIUS server
880 // free up RADIUS task
881 radiusclear(r, s);
882 LOG(3, s, session[s].tunnel, "Freeing up radius session %d\n", r);
883 break;
884 }
885 }
886
887 extern int daefd;
888
889 void processdae(uint8_t *buf, int len, struct sockaddr_in *addr, int alen, struct in_addr *local)
890 {
891 int i, r_code, r_id, length, attribute_length;
892 uint8_t *packet, attribute;
893 hasht hash;
894 char username[MAXUSER] = "";
895 in_addr_t nas = 0;
896 in_addr_t ip = 0;
897 uint32_t port = 0;
898 uint32_t error = 0;
899 sessionidt s = 0;
900 tunnelidt t;
901 int fin = -1;
902 int fout = -1;
903 uint8_t *avpair[64];
904 int avpair_len[sizeof(avpair)/sizeof(*avpair)];
905 int avp = 0;
906 int auth_only = 0;
907 uint8_t *p;
908
909 LOG(3, 0, 0, "DAE request from %s\n", fmtaddr(addr->sin_addr.s_addr, 0));
910 LOG_HEX(5, "DAE Request", buf, len);
911
912 if (len < 20 || len < ntohs(*(uint16_t *) (buf + 2)))
913 {
914 LOG(1, 0, 0, "Duff DAE request length %d\n", len);
915 return;
916 }
917
918 r_code = buf[0]; // request type
919 r_id = buf[1]; // radius indentifier.
920
921 if (r_code != DisconnectRequest && r_code != CoARequest)
922 {
923 LOG(1, 0, 0, "Unrecognised DAE request %s\n", radius_code(r_code));
924 return;
925 }
926
927 if (!config->cluster_iam_master)
928 {
929 master_forward_dae_packet(buf, len, addr->sin_addr.s_addr, addr->sin_port);
930 return;
931 }
932
933 len = ntohs(*(uint16_t *) (buf + 2));
934
935 LOG(3, 0, 0, "Received DAE %s, id %d\n", radius_code(r_code), r_id);
936
937 // check authenticator
938 calc_auth(buf, len, zero, hash);
939 if (memcmp(hash, buf + 4, 16) != 0)
940 {
941 LOG(1, 0, 0, "Incorrect vector in DAE request (wrong secret in radius config?)\n");
942 return;
943 }
944
945 // unpack attributes
946 packet = buf + 20;
947 length = len - 20;
948
949 while (length > 0)
950 {
951 attribute = *packet++;
952 attribute_length = *packet++;
953 if (attribute_length < 2)
954 break;
955
956 length -= attribute_length;
957 attribute_length -= 2;
958 switch (attribute)
959 {
960 case 1: /* username */
961 len = attribute_length < MAXUSER ? attribute_length : MAXUSER - 1;
962 memcpy(username, packet, len);
963 username[len] = 0;
964 LOG(4, 0, 0, " Received DAE User-Name: %s\n", username);
965 break;
966
967 case 4: /* nas ip address */
968 nas = *(uint32_t *) packet; // net order
969 if (nas != config->bind_address)
970 error = 403; // NAS identification mismatch
971
972 LOG(4, 0, 0, " Received DAE NAS-IP-Address: %s\n", fmtaddr(nas, 0));
973 break;
974
975 case 5: /* nas port */
976 port = ntohl(*(uint32_t *) packet);
977 if (port < 1 || port > MAXSESSION)
978 error = 404;
979
980 LOG(4, 0, 0, " Received DAE NAS-Port: %u\n", port);
981 break;
982
983 case 6: /* service type */
984 {
985 uint32_t service_type = ntohl(*(uint32_t *) packet);
986 auth_only = service_type == 8; // Authenticate only
987
988 LOG(4, 0, 0, " Received DAE Service-Type: %u\n", service_type);
989 }
990 break;
991
992 case 8: /* ip address */
993 ip = *(uint32_t *) packet; // net order
994 LOG(4, 0, 0, " Received DAE Framed-IP-Address: %s\n", fmtaddr(ip, 0));
995 break;
996
997 case 11: /* filter id */
998 LOG(4, 0, 0, " Received DAE Filter-Id: %.*s\n", attribute_length, packet);
999 if (!(p = memchr(packet, '.', attribute_length)))
1000 {
1001 error = 404; // invalid request
1002 break;
1003 }
1004
1005 len = p - packet;
1006 i = find_filter((char *) packet, len);
1007 if (i < 0 || !*ip_filters[i].name)
1008 {
1009 error = 404;
1010 break;
1011 }
1012
1013 if (!memcmp(p, ".in", attribute_length - len))
1014 fin = i + 1;
1015 else if (!memcmp(p, ".out", attribute_length - len))
1016 fout = i + 1;
1017 else
1018 error = 404;
1019
1020 break;
1021
1022 case 26: /* vendor specific */
1023 if (attribute_length >= 6
1024 && ntohl(*(uint32_t *) packet) == 9 // Cisco
1025 && *(packet + 4) == 1 // Cisco-AVPair
1026 && *(packet + 5) >= 2) // length
1027 {
1028 int len = *(packet + 5) - 2;
1029 uint8_t *a = packet + 6;
1030
1031 LOG(4, 0, 0, " Received DAE Cisco-AVPair: %.*s\n", len, a);
1032 if (avp < sizeof(avpair)/sizeof(*avpair) - 1)
1033 {
1034 avpair[avp] = a;
1035 avpair_len[avp++] = len;
1036 }
1037 }
1038 break;
1039 }
1040
1041 packet += attribute_length;
1042 }
1043
1044 if (!error && auth_only)
1045 {
1046 if (fin != -1 || fout != -1 || avp)
1047 error = 401; // unsupported attribute
1048 else
1049 error = 405; // unsupported service
1050 }
1051
1052 if (!error && !(port || ip || *username))
1053 error = 402; // missing attribute
1054
1055 // exact match for SID if given
1056 if (!error && port)
1057 {
1058 s = port;
1059 if (!session[s].opened)
1060 error = 503; // not found
1061 }
1062
1063 if (!error && ip)
1064 {
1065 // find/check session by IP
1066 i = sessionbyip(ip);
1067 if (!i || (s && s != i)) // not found or mismatching port
1068 error = 503;
1069 else
1070 s = i;
1071 }
1072
1073 if (!error && *username)
1074 {
1075 if (s)
1076 {
1077 if (strcmp(session[s].user, username))
1078 error = 503;
1079 }
1080 else if (!(s = sessionbyuser(username)))
1081 error = 503;
1082 }
1083
1084 t = session[s].tunnel;
1085
1086 switch (r_code)
1087 {
1088 case DisconnectRequest: // Packet of Disconnect/Death
1089 if (error)
1090 {
1091 r_code = DisconnectNAK;
1092 break;
1093 }
1094
1095 LOG(3, s, t, " DAE Disconnect %d (%s)\n", s, session[s].user);
1096 r_code = DisconnectACK;
1097
1098 sessionshutdown(s, "Requested by PoD", CDN_ADMIN_DISC, TERM_ADMIN_RESET); // disconnect session
1099 break;
1100
1101 case CoARequest: // Change of Authorization
1102 if (error)
1103 {
1104 r_code = CoANAK;
1105 break;
1106 }
1107
1108 LOG(3, s, t, " DAE Change %d (%s)\n", s, session[s].user);
1109 r_code = CoAACK;
1110
1111 // reset
1112 {
1113 struct param_radius_reset p = { &tunnel[session[s].tunnel], &session[s] };
1114 run_plugins(PLUGIN_RADIUS_RESET, &p);
1115 }
1116
1117 // apply filters
1118 if (fin == -1)
1119 fin = 0;
1120 else
1121 LOG(3, s, t, " Filter in %d (%s)\n", fin, ip_filters[fin - 1].name);
1122
1123 if (fout == -1)
1124 fout = 0;
1125 else
1126 LOG(3, s, t, " Filter out %d (%s)\n", fout, ip_filters[fout - 1].name);
1127
1128 filter_session(s, fin, fout);
1129
1130 // process cisco av-pair(s)
1131 for (i = 0; i < avp; i++)
1132 {
1133 LOG(3, s, t, " Cisco-AVPair: %.*s\n", avpair_len[i], avpair[i]);
1134 handle_avpair(s, avpair[i], avpair_len[i]);
1135 }
1136
1137 cluster_send_session(s);
1138 break;
1139 }
1140
1141 // send response
1142 packet = buf;
1143 *packet++ = r_code;
1144 *packet++ = r_id;
1145 // skip len + auth
1146 packet += 2 + 16;
1147 len = packet - buf;
1148
1149 // add attributes
1150 if (error)
1151 {
1152 // add error cause
1153 *packet++ = 101;
1154 *packet++ = 6;
1155 *(uint32_t *) packet = htonl(error);
1156 len += 6;
1157 }
1158
1159 *((uint16_t *)(buf + 2)) = htons(len);
1160
1161 // make vector
1162 calc_auth(buf, len, hash, buf + 4);
1163
1164 LOG(3, 0, 0, "Sending DAE %s, id=%d\n", radius_code(r_code), r_id);
1165
1166 // send DAE response
1167 if (sendtofrom(daefd, buf, len, MSG_DONTWAIT | MSG_NOSIGNAL, (struct sockaddr *) addr, alen, local) < 0)
1168 LOG(0, 0, 0, "Error sending DAE response packet: %s\n", strerror(errno));
1169 }