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