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