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