IPv6 load-balancing
[l2tpns.git] / radius.c
1 // L2TPNS Radius Stuff
2
3 #include <time.h>
4 #include <stdio.h>
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 #include <malloc.h>
8 #include <string.h>
9 #include <fcntl.h>
10 #include <arpa/inet.h>
11 #include <ctype.h>
12 #include <netinet/in.h>
13 #include <errno.h>
14 #include <linux/rtnetlink.h>
15
16 #include "md5.h"
17 #include "constants.h"
18 #include "dhcp6.h"
19 #include "l2tpns.h"
20 #include "plugin.h"
21 #include "util.h"
22 #include "cluster.h"
23
24 #include "l2tplac.h"
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 int routes = 0;
538 int routes6 = 0;
539 int r_code;
540 int r_id;
541 int OpentunnelReq = 0;
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 if (r_code == AccessAccept)
604 {
605 // Login successful
606 // Extract IP, routes, etc
607 uint8_t *p = buf + 20;
608 uint8_t *e = buf + len;
609 uint8_t tag;
610 uint8_t strtemp[256];
611 lac_reset_rad_tag_tunnel_ctxt();
612
613 for (; p + 2 <= e && p[1] && p + p[1] <= e; p += p[1])
614 {
615 if (*p == 26 && p[1] >= 7)
616 {
617 // Vendor-Specific Attribute
618 uint32_t vendor = ntohl(*(int *)(p + 2));
619 uint8_t attrib = *(p + 6);
620 int attrib_length = *(p + 7) - 2;
621
622 LOG(4, s, session[s].tunnel, " Radius reply contains Vendor-Specific. Vendor=%u Attrib=%u Length=%d\n", vendor, attrib, attrib_length);
623 if (vendor == 9 && attrib == 1) // Cisco-AVPair
624 {
625 if (attrib_length < 0) continue;
626 LOG(3, s, session[s].tunnel, " Cisco-AVPair value: %.*s\n",
627 attrib_length, p + 8);
628
629 handle_avpair(s, p + 8, attrib_length);
630 continue;
631 }
632 else if (vendor == 529 && attrib >= 135 && attrib <= 136) // Ascend
633 {
634 // handle old-format ascend DNS attributes below
635 p += 6;
636 }
637 else if (vendor == 64520) // Sames
638 {
639 //Sames vendor-specific 64520
640 uint8_t *pvs = p + 6; // pvs set to begin to attribute
641 LOG(3, s, session[s].tunnel, " Sames vendor-specific: %d, Attrib: %d, lenght: %d\n", vendor, attrib, attrib_length);
642 grp_processvendorspecific(s, pvs);
643 continue;
644 }
645 else
646 {
647 LOG(3, s, session[s].tunnel, " Unknown vendor-specific: %d, Attrib: %d\n", vendor, attrib);
648 continue;
649 }
650 }
651
652 if (*p == 8)
653 {
654 // Framed-IP-Address
655 if (p[1] < 6) continue;
656 session[s].ip = ntohl(*(uint32_t *) (p + 2));
657 session[s].ip_pool_index = -1;
658 LOG(3, s, session[s].tunnel, " Radius reply contains IP address %s\n",
659 fmtaddr(htonl(session[s].ip), 0));
660
661 if (session[s].ip == 0xFFFFFFFE)
662 session[s].ip = 0; // assign from pool
663 }
664 else if (*p == 135)
665 {
666 // DNS address
667 if (p[1] < 6) continue;
668 session[s].dns1 = ntohl(*(uint32_t *) (p + 2));
669 LOG(3, s, session[s].tunnel, " Radius reply contains primary DNS address %s\n",
670 fmtaddr(htonl(session[s].dns1), 0));
671 }
672 else if (*p == 136)
673 {
674 // DNS address
675 if (p[1] < 6) continue;
676 session[s].dns2 = ntohl(*(uint32_t *) (p + 2));
677 LOG(3, s, session[s].tunnel, " Radius reply contains secondary DNS address %s\n",
678 fmtaddr(htonl(session[s].dns2), 0));
679 }
680 else if (*p == 22)
681 {
682 // Framed-Route
683 in_addr_t ip = 0;
684 uint8_t u = 0;
685 uint8_t bits = 0;
686 uint8_t *n = p + 2;
687 uint8_t *e = p + p[1];
688 while (n < e && (isdigit(*n) || *n == '.'))
689 {
690 if (*n == '.')
691 {
692 ip = (ip << 8) + u;
693 u = 0;
694 }
695 else
696 u = u * 10 + *n - '0';
697 n++;
698 }
699 ip = (ip << 8) + u;
700 if (*n == '/')
701 {
702 n++;
703 while (n < e && isdigit(*n))
704 bits = bits * 10 + *n++ - '0';
705 }
706 else if ((ip >> 24) < 128)
707 bits = 8;
708 else if ((ip >> 24) < 192)
709 bits = 16;
710 else
711 bits = 24;
712
713 if (routes == MAXROUTE)
714 {
715 LOG(1, s, session[s].tunnel, " Too many routes\n");
716 }
717 else if (ip)
718 {
719 LOG(3, s, session[s].tunnel, " Radius reply contains route for %s/%d\n",
720 fmtaddr(htonl(ip), 0), bits);
721
722 session[s].route[routes].ip = ip;
723 session[s].route[routes].prefixlen = bits;
724 routes++;
725 }
726 }
727 else if (*p == 11)
728 {
729 // Filter-Id
730 char *filter = (char *) p + 2;
731 int l = p[1] - 2;
732 char *suffix;
733 int f;
734 uint8_t *fp = 0;
735
736 LOG(3, s, session[s].tunnel, " Radius reply contains Filter-Id \"%.*s\"\n", l, filter);
737 if ((suffix = memchr(filter, '.', l)))
738 {
739 int b = suffix - filter;
740 if (l - b == 3 && !memcmp("in", suffix+1, 2))
741 fp = &session[s].filter_in;
742 else if (l - b == 4 && !memcmp("out", suffix+1, 3))
743 fp = &session[s].filter_out;
744
745 l = b;
746 }
747
748 if (!fp)
749 {
750 LOG(3, s, session[s].tunnel, " Invalid filter\n");
751 continue;
752 }
753
754 if ((f = find_filter(filter, l)) < 0 || !*ip_filters[f].name)
755 {
756 LOG(3, s, session[s].tunnel, " Unknown filter\n");
757 }
758 else
759 {
760 *fp = f + 1;
761 ip_filters[f].used++;
762 }
763 }
764 else if (*p == 27)
765 {
766 // Session-Timeout
767 if (p[1] < 6) continue;
768 session[s].session_timeout = ntohl(*(uint32_t *)(p + 2));
769 LOG(3, s, session[s].tunnel, " Radius reply contains Session-Timeout = %u\n", session[s].session_timeout);
770 if(!session[s].session_timeout && config->kill_timedout_sessions)
771 sessionshutdown(s, "Session timeout is zero", CDN_ADMIN_DISC, 0);
772 }
773 else if (*p == 28)
774 {
775 // Idle-Timeout
776 if (p[1] < 6) continue;
777 session[s].idle_timeout = ntohl(*(uint32_t *)(p + 2));
778 LOG(3, s, session[s].tunnel, " Radius reply contains Idle-Timeout = %u\n", session[s].idle_timeout);
779 }
780 else if (*p == 99)
781 {
782 // Framed-IPv6-Route
783 struct in6_addr r6;
784 int prefixlen;
785 uint8_t *n = p + 2;
786 uint8_t *e = p + p[1];
787 uint8_t *m = memchr(n, '/', e - n);
788
789 *m++ = 0;
790 inet_pton(AF_INET6, (char *) n, &r6);
791
792 prefixlen = 0;
793 while (m < e && isdigit(*m)) {
794 prefixlen = prefixlen * 10 + *m++ - '0';
795 }
796
797 if (prefixlen)
798 {
799 if (routes6 == MAXROUTE6)
800 {
801 LOG(1, s, session[s].tunnel, " Too many IPv6 routes\n");
802 }
803 else
804 {
805 LOG(3, s, session[s].tunnel, " Radius reply contains route for %s/%d\n", n, prefixlen);
806 session[s].route6[routes6].ipv6route = r6;
807 session[s].route6[routes6].ipv6prefixlen = prefixlen;
808 routes6++;
809 }
810 }
811 }
812 else if (*p == 123)
813 {
814 // Delegated-IPv6-Prefix
815 if ((p[1] > 4) && (p[3] > 0) && (p[3] <= 128))
816 {
817 char ipv6addr[INET6_ADDRSTRLEN];
818
819 if (routes6 == MAXROUTE6)
820 {
821 LOG(1, s, session[s].tunnel, " Too many IPv6 routes\n");
822 }
823 else
824 {
825 memcpy(&session[s].route6[routes6].ipv6route, &p[4], p[1] - 4);
826 session[s].route6[routes6].ipv6prefixlen = p[3];
827 LOG(3, s, session[s].tunnel, " Radius reply contains Delegated IPv6 Prefix %s/%d\n",
828 inet_ntop(AF_INET6, &session[s].route6[routes6].ipv6route, ipv6addr, INET6_ADDRSTRLEN), session[s].route6[routes6].ipv6prefixlen);
829 routes6++;
830 }
831 }
832 }
833 else if (*p == 168)
834 {
835 // Framed-IPv6-Address
836 if (p[1] == 18)
837 {
838 char ipv6addr[INET6_ADDRSTRLEN];
839 memcpy(&session[s].ipv6address, &p[2], 16);
840 LOG(3, s, session[s].tunnel, " Radius reply contains Framed-IPv6-Address %s\n", inet_ntop(AF_INET6, &session[s].ipv6address, ipv6addr, INET6_ADDRSTRLEN));
841 }
842 }
843 else if (*p == 25)
844 {
845 // Class
846 if (p[1] < 3) continue;
847 session[s].classlen = p[1] - 2;
848 if (session[s].classlen > MAXCLASS)
849 session[s].classlen = MAXCLASS;
850 memcpy(session[s].class, p + 2, session[s].classlen);
851 }
852 else if (*p == 64)
853 {
854 // Tunnel-Type
855 if (p[1] != 6) continue;
856 tag = p[2];
857 LOG(3, s, session[s].tunnel, " Radius reply Tunnel-Type:%d %d\n",
858 tag, ntohl(*(uint32_t *)(p + 2)) & 0xFFFFFF);
859 // Fill context
860 lac_set_rad_tag_tunnel_type(tag, ntohl(*(uint32_t *)(p + 2)) & 0xFFFFFF);
861 /* Request open tunnel to remote LNS*/
862 OpentunnelReq = 1;
863 }
864 else if (*p == 65)
865 {
866 // Tunnel-Medium-Type
867 if (p[1] < 6) continue;
868 tag = p[2];
869 LOG(3, s, session[s].tunnel, " Radius reply Tunnel-Medium-Type:%d %d\n",
870 tag, ntohl(*(uint32_t *)(p + 2)) & 0xFFFFFF);
871 // Fill context
872 lac_set_rad_tag_tunnel_medium_type(tag, ntohl(*(uint32_t *)(p + 2)) & 0xFFFFFF);
873 }
874 else if (*p == 67)
875 {
876 // Tunnel-Server-Endpoint
877 if (p[1] < 3) continue;
878 tag = p[2];
879 //If the Tag field is greater than 0x1F,
880 // it SHOULD be interpreted as the first byte of the following String field.
881 memset(strtemp, 0, 256);
882 if (tag > 0x1F)
883 {
884 tag = 0;
885 memcpy(strtemp, (p + 2), p[1]-2);
886 }
887 else
888 memcpy(strtemp, (p + 3), p[1]-3);
889
890 LOG(3, s, session[s].tunnel, " Radius reply Tunnel-Server-Endpoint:%d %s\n", tag, strtemp);
891 // Fill context
892 lac_set_rad_tag_tunnel_serv_endpt(tag, (char *) strtemp);
893 }
894 else if (*p == 69)
895 {
896 // Tunnel-Password
897 size_t lentemp;
898
899 if (p[1] < 5) continue;
900 tag = p[2];
901
902 memset(strtemp, 0, 256);
903 lentemp = p[1]-3;
904 memcpy(strtemp, (p + 3), lentemp);
905 if (!rad_tunnel_pwdecode(strtemp, &lentemp, config->radiussecret, radius[r].auth))
906 {
907 LOG_HEX(3, "Error Decode Tunnel-Password, Dump Radius reponse:", p, p[1]);
908 continue;
909 }
910
911 LOG(3, s, session[s].tunnel, " Radius reply Tunnel-Password:%d %s\n", tag, strtemp);
912 if (strlen((char *) strtemp) > 63)
913 {
914 LOG(1, s, session[s].tunnel, "tunnel password is too long (>63)\n");
915 continue;
916 }
917 // Fill context
918 lac_set_rad_tag_tunnel_password(tag, (char *) strtemp);
919 }
920 else if (*p == 82)
921 {
922 // Tunnel-Assignment-Id
923 if (p[1] < 3) continue;
924 tag = p[2];
925 //If the Tag field is greater than 0x1F,
926 // it SHOULD be interpreted as the first byte of the following String field.
927 memset(strtemp, 0, 256);
928 if (tag > 0x1F)
929 {
930 tag = 0;
931 memcpy(strtemp, (p + 2), p[1]-2);
932 }
933 else
934 memcpy(strtemp, (p + 3), p[1]-3);
935
936 LOG(3, s, session[s].tunnel, " Radius reply Tunnel-Assignment-Id:%d %s\n", tag, strtemp);
937 // Fill context
938 lac_set_rad_tag_tunnel_assignment_id(tag, (char *) strtemp);
939 }
940 }
941 }
942 else if (r_code == AccessReject)
943 {
944 LOG(2, s, session[s].tunnel, " Authentication rejected for %s\n", session[s].user);
945 sessionkill(s, "Authentication rejected");
946 break;
947 }
948
949 if ((!config->disable_lac_func) && OpentunnelReq)
950 {
951 char assignment_id[256];
952 // Save radius tag context to conf
953 lac_save_rad_tag_tunnels(s);
954
955 memset(assignment_id, 0, 256);
956 if (!lac_rad_select_assignment_id(s, assignment_id))
957 break; // Error no assignment_id
958
959 LOG(3, s, session[s].tunnel, "Select Tunnel Remote LNS for assignment_id == %s\n", assignment_id);
960
961 if (lac_rad_forwardtoremotelns(s, assignment_id, session[s].user))
962 {
963 int ro;
964 // Sanity check, no local IP to session forwarded
965 session[s].ip = 0;
966 for (ro = 0; r < MAXROUTE && session[s].route[ro].ip; r++)
967 {
968 session[s].route[ro].ip = 0;
969 }
970 break;
971 }
972 }
973
974 // process auth response
975 if (radius[r].chap)
976 {
977 // CHAP
978 uint8_t *p = makeppp(b, sizeof(b), 0, 0, s, t, PPPCHAP, 0, 0, 0);
979 if (!p) return; // Abort!
980
981 *p = (r_code == AccessAccept) ? 3 : 4; // ack/nak
982 p[1] = radius[r].id;
983 *(uint16_t *) (p + 2) = ntohs(4); // no message
984 tunnelsend(b, (p - b) + 4, t); // send it
985
986 LOG(3, s, session[s].tunnel, " CHAP User %s authentication %s.\n", session[s].user,
987 (r_code == AccessAccept) ? "allowed" : "denied");
988 }
989 else
990 {
991 // PAP
992 uint8_t *p = makeppp(b, sizeof(b), 0, 0, s, t, PPPPAP, 0, 0, 0);
993 if (!p) return; // Abort!
994
995 // ack/nak
996 *p = r_code;
997 p[1] = radius[r].id;
998 *(uint16_t *) (p + 2) = ntohs(5);
999 p[4] = 0; // no message
1000 tunnelsend(b, (p - b) + 5, t); // send it
1001
1002 LOG(3, s, session[s].tunnel, " PAP User %s authentication %s.\n", session[s].user,
1003 (r_code == AccessAccept) ? "allowed" : "denied");
1004 }
1005
1006 if (!session[s].dns1 && config->default_dns1)
1007 {
1008 session[s].dns1 = ntohl(config->default_dns1);
1009 LOG(3, s, t, " Sending dns1 = %s\n", fmtaddr(config->default_dns1, 0));
1010 }
1011 if (!session[s].dns2 && config->default_dns2)
1012 {
1013 session[s].dns2 = ntohl(config->default_dns2);
1014 LOG(3, s, t, " Sending dns2 = %s\n", fmtaddr(config->default_dns2, 0));
1015 }
1016
1017 // Valid Session, set it up
1018 session[s].unique_id = 0;
1019 sessionsetup(s, t);
1020 }
1021 else
1022 {
1023 // An ack for a stop or start record.
1024 LOG(3, s, t, " RADIUS accounting ack recv in state %s\n", radius_state(radius[r].state));
1025 break;
1026 }
1027 } while (0);
1028
1029 // finished with RADIUS
1030 radiusclear(r, s);
1031 }
1032
1033 // Send a retry for RADIUS/CHAP message
1034 void radiusretry(uint16_t r)
1035 {
1036 sessionidt s = radius[r].session;
1037 tunnelidt t = 0;
1038
1039 CSTAT(radiusretry);
1040
1041 if (s) t = session[s].tunnel;
1042
1043 switch (radius[r].state)
1044 {
1045 case RADIUSCHAP: // sending CHAP down PPP
1046 sendchap(s, t);
1047 break;
1048 case RADIUSAUTH: // sending auth to RADIUS server
1049 case RADIUSJUSTAUTH: // sending auth to RADIUS server
1050 case RADIUSSTART: // sending start accounting to RADIUS server
1051 case RADIUSSTOP: // sending stop accounting to RADIUS server
1052 case RADIUSINTERIM: // sending interim accounting to RADIUS server
1053 radiussend(r, radius[r].state);
1054 break;
1055 default:
1056 case RADIUSNULL: // Not in use
1057 case RADIUSWAIT: // waiting timeout before available, in case delayed reply from RADIUS server
1058 // free up RADIUS task
1059 radiusclear(r, s);
1060 LOG(3, s, session[s].tunnel, "Freeing up radius session %d\n", r);
1061 break;
1062 }
1063 }
1064
1065 extern int daefd;
1066
1067 void processdae(uint8_t *buf, int len, struct sockaddr_in *addr, int alen, struct in_addr *local)
1068 {
1069 int i, r_code, r_id, length, attribute_length;
1070 uint8_t *packet, attribute;
1071 hasht hash;
1072 char username[MAXUSER] = "";
1073 in_addr_t nas = 0;
1074 in_addr_t ip = 0;
1075 uint32_t port = 0;
1076 uint32_t error = 0;
1077 sessionidt s = 0;
1078 tunnelidt t;
1079 int fin = -1;
1080 int fout = -1;
1081 uint8_t *avpair[64];
1082 int avpair_len[sizeof(avpair)/sizeof(*avpair)];
1083 int avp = 0;
1084 int auth_only = 0;
1085 uint8_t *p;
1086
1087 LOG(3, 0, 0, "DAE request from %s\n", fmtaddr(addr->sin_addr.s_addr, 0));
1088 LOG_HEX(5, "DAE Request", buf, len);
1089
1090 if (len < 20 || len < ntohs(*(uint16_t *) (buf + 2)))
1091 {
1092 LOG(1, 0, 0, "Duff DAE request length %d\n", len);
1093 return;
1094 }
1095
1096 r_code = buf[0]; // request type
1097 r_id = buf[1]; // radius indentifier.
1098
1099 if (r_code != DisconnectRequest && r_code != CoARequest)
1100 {
1101 LOG(1, 0, 0, "Unrecognised DAE request %s\n", radius_code(r_code));
1102 return;
1103 }
1104
1105 if (!config->cluster_iam_master)
1106 {
1107 master_forward_dae_packet(buf, len, addr->sin_addr.s_addr, addr->sin_port);
1108 return;
1109 }
1110
1111 len = ntohs(*(uint16_t *) (buf + 2));
1112
1113 LOG(3, 0, 0, "Received DAE %s, id %d\n", radius_code(r_code), r_id);
1114
1115 // check authenticator
1116 calc_auth(buf, len, zero, hash);
1117 if (memcmp(hash, buf + 4, 16) != 0)
1118 {
1119 LOG(1, 0, 0, "Incorrect vector in DAE request (wrong secret in radius config?)\n");
1120 return;
1121 }
1122
1123 // unpack attributes
1124 packet = buf + 20;
1125 length = len - 20;
1126
1127 while (length > 0)
1128 {
1129 attribute = *packet++;
1130 attribute_length = *packet++;
1131 if (attribute_length < 2)
1132 break;
1133
1134 length -= attribute_length;
1135 attribute_length -= 2;
1136 switch (attribute)
1137 {
1138 case 1: /* username */
1139 len = attribute_length < MAXUSER ? attribute_length : MAXUSER - 1;
1140 memcpy(username, packet, len);
1141 username[len] = 0;
1142 LOG(4, 0, 0, " Received DAE User-Name: %s\n", username);
1143 break;
1144
1145 case 4: /* nas ip address */
1146 nas = *(uint32_t *) packet; // net order
1147 if (nas != config->bind_address)
1148 error = 403; // NAS identification mismatch
1149
1150 LOG(4, 0, 0, " Received DAE NAS-IP-Address: %s\n", fmtaddr(nas, 0));
1151 break;
1152
1153 case 5: /* nas port */
1154 port = ntohl(*(uint32_t *) packet);
1155 if (port < 1 || port > MAXSESSION)
1156 error = 404;
1157
1158 LOG(4, 0, 0, " Received DAE NAS-Port: %u\n", port);
1159 break;
1160
1161 case 6: /* service type */
1162 {
1163 uint32_t service_type = ntohl(*(uint32_t *) packet);
1164 auth_only = service_type == 8; // Authenticate only
1165
1166 LOG(4, 0, 0, " Received DAE Service-Type: %u\n", service_type);
1167 }
1168 break;
1169
1170 case 8: /* ip address */
1171 ip = *(uint32_t *) packet; // net order
1172 LOG(4, 0, 0, " Received DAE Framed-IP-Address: %s\n", fmtaddr(ip, 0));
1173 break;
1174
1175 case 11: /* filter id */
1176 LOG(4, 0, 0, " Received DAE Filter-Id: %.*s\n", attribute_length, packet);
1177 if (!(p = memchr(packet, '.', attribute_length)))
1178 {
1179 error = 404; // invalid request
1180 break;
1181 }
1182
1183 len = p - packet;
1184 i = find_filter((char *) packet, len);
1185 if (i < 0 || !*ip_filters[i].name)
1186 {
1187 error = 404;
1188 break;
1189 }
1190
1191 if (!memcmp(p, ".in", attribute_length - len))
1192 fin = i + 1;
1193 else if (!memcmp(p, ".out", attribute_length - len))
1194 fout = i + 1;
1195 else
1196 error = 404;
1197
1198 break;
1199
1200 case 26: /* vendor specific */
1201 if (attribute_length >= 6
1202 && ntohl(*(uint32_t *) packet) == 9 // Cisco
1203 && *(packet + 4) == 1 // Cisco-AVPair
1204 && *(packet + 5) >= 2) // length
1205 {
1206 int len = *(packet + 5) - 2;
1207 uint8_t *a = packet + 6;
1208
1209 LOG(4, 0, 0, " Received DAE Cisco-AVPair: %.*s\n", len, a);
1210 if (avp < sizeof(avpair)/sizeof(*avpair) - 1)
1211 {
1212 avpair[avp] = a;
1213 avpair_len[avp++] = len;
1214 }
1215 }
1216 break;
1217 }
1218
1219 packet += attribute_length;
1220 }
1221
1222 if (!error && auth_only)
1223 {
1224 if (fin != -1 || fout != -1 || avp)
1225 error = 401; // unsupported attribute
1226 else
1227 error = 405; // unsupported service
1228 }
1229
1230 if (!error && !(port || ip || *username))
1231 error = 402; // missing attribute
1232
1233 // exact match for SID if given
1234 if (!error && port)
1235 {
1236 s = port;
1237 if (!session[s].opened)
1238 error = 503; // not found
1239 }
1240
1241 if (!error && ip)
1242 {
1243 // find/check session by IP
1244 i = sessionbyip(ip);
1245 if (!i || (s && s != i)) // not found or mismatching port
1246 error = 503;
1247 else
1248 s = i;
1249 }
1250
1251 if (!error && *username)
1252 {
1253 if (s)
1254 {
1255 if (strcmp(session[s].user, username))
1256 error = 503;
1257 }
1258 else if (!(s = sessionbyuser(username)))
1259 error = 503;
1260 }
1261
1262 t = session[s].tunnel;
1263
1264 switch (r_code)
1265 {
1266 case DisconnectRequest: // Packet of Disconnect/Death
1267 if (error)
1268 {
1269 r_code = DisconnectNAK;
1270 break;
1271 }
1272
1273 LOG(3, s, t, " DAE Disconnect %d (%s)\n", s, session[s].user);
1274 r_code = DisconnectACK;
1275
1276 sessionshutdown(s, "Requested by PoD", CDN_ADMIN_DISC, TERM_ADMIN_RESET); // disconnect session
1277 break;
1278
1279 case CoARequest: // Change of Authorization
1280 if (error)
1281 {
1282 r_code = CoANAK;
1283 break;
1284 }
1285
1286 LOG(3, s, t, " DAE Change %d (%s)\n", s, session[s].user);
1287 r_code = CoAACK;
1288
1289 // reset
1290 {
1291 struct param_radius_reset p = { &tunnel[session[s].tunnel], &session[s] };
1292 run_plugins(PLUGIN_RADIUS_RESET, &p);
1293 }
1294
1295 // apply filters
1296 if (fin == -1)
1297 fin = 0;
1298 else
1299 LOG(3, s, t, " Filter in %d (%s)\n", fin, ip_filters[fin - 1].name);
1300
1301 if (fout == -1)
1302 fout = 0;
1303 else
1304 LOG(3, s, t, " Filter out %d (%s)\n", fout, ip_filters[fout - 1].name);
1305
1306 filter_session(s, fin, fout);
1307
1308 // process cisco av-pair(s)
1309 for (i = 0; i < avp; i++)
1310 {
1311 LOG(3, s, t, " Cisco-AVPair: %.*s\n", avpair_len[i], avpair[i]);
1312 handle_avpair(s, avpair[i], avpair_len[i]);
1313 }
1314
1315 cluster_send_session(s);
1316 break;
1317 }
1318
1319 // send response
1320 packet = buf;
1321 *packet++ = r_code;
1322 *packet++ = r_id;
1323 // skip len + auth
1324 packet += 2 + 16;
1325 len = packet - buf;
1326
1327 // add attributes
1328 if (error)
1329 {
1330 // add error cause
1331 *packet++ = 101;
1332 *packet++ = 6;
1333 *(uint32_t *) packet = htonl(error);
1334 len += 6;
1335 }
1336
1337 *((uint16_t *)(buf + 2)) = htons(len);
1338
1339 // make vector
1340 calc_auth(buf, len, hash, buf + 4);
1341
1342 LOG(3, 0, 0, "Sending DAE %s, id=%d\n", radius_code(r_code), r_id);
1343
1344 // send DAE response
1345 if (sendtofrom(daefd, buf, len, MSG_DONTWAIT | MSG_NOSIGNAL, (struct sockaddr *) addr, alen, local) < 0)
1346 LOG(0, 0, 0, "Error sending DAE response packet: %s\n", strerror(errno));
1347 }
1348
1349 // Decrypte the encrypted Tunnel Password.
1350 // Defined in RFC-2868.
1351 // the pl2tpsecret buffer must set to 256 characters.
1352 // return 0 on decoding error else length of decoded l2tpsecret
1353 int rad_tunnel_pwdecode(uint8_t *pl2tpsecret, size_t *pl2tpsecretlen,
1354 const char *radiussecret, const uint8_t * auth)
1355 {
1356 MD5_CTX ctx, oldctx;
1357 hasht hash;
1358 int secretlen;
1359 unsigned i, n, len, decodedlen;
1360
1361 /* 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
1362 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1363 | Salt | Salt | String ..........
1364 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+*/
1365
1366 len = *pl2tpsecretlen;
1367
1368 if (len < 2)
1369 {
1370 LOG(1, 0, 0, "tunnel password is too short, We need at least a salt\n");
1371 return 0;
1372 }
1373
1374 if (len <= 3)
1375 {
1376 pl2tpsecret[0] = 0;
1377 *pl2tpsecretlen = 0;
1378 LOG(1, 0, 0, "tunnel passwd is empty !!!\n");
1379 return 0;
1380 }
1381
1382 len -= 2; /* discount the salt */
1383
1384 //Use the secret to setup the decryption
1385 secretlen = strlen(radiussecret);
1386
1387 MD5_Init(&ctx);
1388 MD5_Update(&ctx, (void *) radiussecret, secretlen);
1389 oldctx = ctx; /* save intermediate work */
1390
1391 // Set up the initial key:
1392 // b(1) = MD5(radiussecret + auth + salt)
1393 MD5_Update(&ctx, (void *) auth, 16);
1394 MD5_Update(&ctx, pl2tpsecret, 2);
1395
1396 decodedlen = 0;
1397 for (n = 0; n < len; n += 16)
1398 {
1399 int base = 0;
1400
1401 if (n == 0)
1402 {
1403 MD5_Final(hash, &ctx);
1404
1405 ctx = oldctx;
1406
1407 // the first octet, it's the 'data_len'
1408 // Check is correct
1409 decodedlen = pl2tpsecret[2] ^ hash[0];
1410 if (decodedlen >= len)
1411 {
1412 LOG(1, 0, 0, "tunnel password is too long !!!\n");
1413 return 0;
1414 }
1415
1416 MD5_Update(&ctx, pl2tpsecret + 2, 16);
1417 base = 1;
1418 } else
1419 {
1420 MD5_Final(hash, &ctx);
1421
1422 ctx = oldctx;
1423 MD5_Update(&ctx, pl2tpsecret + n + 2, 16);
1424 }
1425
1426 for (i = base; i < 16; i++)
1427 {
1428 pl2tpsecret[n + i - 1] = pl2tpsecret[n + i + 2] ^ hash[i];
1429 }
1430 }
1431
1432 if (decodedlen > 239) decodedlen = 239;
1433
1434 *pl2tpsecretlen = decodedlen;
1435 pl2tpsecret[decodedlen] = 0;
1436
1437 return decodedlen;
1438 };