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