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