allow username to be set when ungardening
[l2tpns.git] / ppp.c
1 // L2TPNS PPP Stuff
2
3 char const *cvs_id_ppp = "$Id: ppp.c,v 1.45 2005/03/10 03:31:25 bodea Exp $";
4
5 #include <stdio.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <errno.h>
9 #include <stdlib.h>
10 #include "l2tpns.h"
11 #include "constants.h"
12 #include "plugin.h"
13 #include "util.h"
14 #include "tbf.h"
15 #include "cluster.h"
16
17 extern tunnelt *tunnel;
18 extern sessiont *session;
19 extern radiust *radius;
20 extern int tunfd;
21 extern char hostname[];
22 extern uint32_t eth_tx;
23 extern time_t time_now;
24 extern configt *config;
25
26 static void initccp(tunnelidt t, sessionidt s);
27
28 // Process PAP messages
29 void processpap(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
30 {
31 char user[MAXUSER];
32 char pass[MAXPASS];
33 uint16_t hl;
34
35 CSTAT(processpap);
36
37 LOG_HEX(5, "PAP", p, l);
38 if (l < 4)
39 {
40 LOG(1, s, t, "Short PAP %u bytes\n", l);
41 STAT(tunnel_rx_errors);
42 return ;
43 }
44
45 if ((hl = ntohs(*(uint16_t *) (p + 2))) > l)
46 {
47 LOG(1, s, t, "Length mismatch PAP %u/%u\n", hl, l);
48 STAT(tunnel_rx_errors);
49 return ;
50 }
51 l = hl;
52
53 if (*p != 1)
54 {
55 LOG(1, s, t, "Unexpected PAP code %d\n", *p);
56 STAT(tunnel_rx_errors);
57 return ;
58 }
59
60 {
61 uint8_t *b = p;
62 b += 4;
63 user[0] = pass[0] = 0;
64 if (*b && *b < sizeof(user))
65 {
66 memcpy(user, b + 1, *b);
67 user[*b] = 0;
68 b += 1 + *b;
69 if (*b && *b < sizeof(pass))
70 {
71 memcpy(pass, b + 1, *b);
72 pass[*b] = 0;
73 }
74 }
75 LOG(3, s, t, "PAP login %s/%s\n", user, pass);
76 }
77 if (session[s].ip || !session[s].radius)
78 {
79 // respond now, either no RADIUS available or already authenticated
80 uint8_t b[MAXCONTROL];
81 uint8_t id = p[1];
82 uint8_t *p = makeppp(b, sizeof(b), 0, 0, t, s, PPPPAP);
83 if (!p) return;
84
85 if (session[s].ip)
86 *p = 2; // ACK
87 else
88 *p = 3; // cant authorise
89 p[1] = id;
90 *(uint16_t *) (p + 2) = htons(5); // length
91 p[4] = 0; // no message
92 if (session[s].ip)
93 {
94 LOG(3, s, t, "Already an IP allocated: %s (%d)\n",
95 fmtaddr(htonl(session[s].ip), 0), session[s].ip_pool_index);
96
97 session[s].flags &= ~SF_IPCP_ACKED;
98 }
99 else
100 {
101 LOG(1, s, t, "No radius session available to authenticate session...\n");
102 }
103 LOG(3, s, t, "Fallback response to PAP (%s)\n", (session[s].ip) ? "ACK" : "NAK");
104 tunnelsend(b, 5 + (p - b), t); // send it
105 }
106 else
107 {
108 // set up RADIUS request
109 uint16_t r = session[s].radius;
110
111 // Run PRE_AUTH plugins
112 struct param_pre_auth packet = { &tunnel[t], &session[s], strdup(user), strdup(pass), PPPPAP, 1 };
113 run_plugins(PLUGIN_PRE_AUTH, &packet);
114 if (!packet.continue_auth)
115 {
116 LOG(3, s, t, "A plugin rejected PRE_AUTH\n");
117 if (packet.username) free(packet.username);
118 if (packet.password) free(packet.password);
119 return;
120 }
121
122 strncpy(session[s].user, packet.username, sizeof(session[s].user) - 1);
123 strncpy(radius[r].pass, packet.password, sizeof(radius[r].pass) - 1);
124
125 free(packet.username);
126 free(packet.password);
127
128 radius[r].id = p[1];
129 LOG(3, s, t, "Sending login for %s/%s to radius\n", user, pass);
130 radiussend(r, RADIUSAUTH);
131 }
132 }
133
134 // Process CHAP messages
135 void processchap(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
136 {
137 uint16_t r;
138 uint16_t hl;
139
140 CSTAT(processchap);
141
142 LOG_HEX(5, "CHAP", p, l);
143 r = session[s].radius;
144 if (!r)
145 {
146 LOG(1, s, t, "Unexpected CHAP message\n");
147 STAT(tunnel_rx_errors);
148 return;
149 }
150
151 if (l < 4)
152 {
153 LOG(1, s, t, "Short CHAP %u bytes\n", l);
154 STAT(tunnel_rx_errors);
155 return ;
156 }
157
158 if ((hl = ntohs(*(uint16_t *) (p + 2))) > l)
159 {
160 LOG(1, s, t, "Length mismatch CHAP %u/%u\n", hl, l);
161 STAT(tunnel_rx_errors);
162 return ;
163 }
164 l = hl;
165
166 if (*p != 2)
167 {
168 LOG(1, s, t, "Unexpected CHAP response code %d\n", *p);
169 STAT(tunnel_rx_errors);
170 return;
171 }
172 if (p[1] != radius[r].id)
173 {
174 LOG(1, s, t, "Wrong CHAP response ID %d (should be %d) (%d)\n", p[1], radius[r].id, r);
175 STAT(tunnel_rx_errors);
176 return ;
177 }
178
179 if (l < 5 || p[4] != 16)
180 {
181 LOG(1, s, t, "Bad CHAP response length %d\n", l < 5 ? -1 : p[4]);
182 STAT(tunnel_rx_errors);
183 return ;
184 }
185
186 l -= 5;
187 p += 5;
188 if (l < 16 || l - 16 >= sizeof(session[s].user))
189 {
190 LOG(1, s, t, "CHAP user too long %d\n", l - 16);
191 STAT(tunnel_rx_errors);
192 return ;
193 }
194
195 // Run PRE_AUTH plugins
196 {
197 struct param_pre_auth packet = { &tunnel[t], &session[s], NULL, NULL, PPPCHAP, 1 };
198
199 packet.password = calloc(17, 1);
200 memcpy(packet.password, p, 16);
201
202 p += 16;
203 l -= 16;
204
205 packet.username = calloc(l + 1, 1);
206 memcpy(packet.username, p, l);
207
208 run_plugins(PLUGIN_PRE_AUTH, &packet);
209 if (!packet.continue_auth)
210 {
211 LOG(3, s, t, "A plugin rejected PRE_AUTH\n");
212 if (packet.username) free(packet.username);
213 if (packet.password) free(packet.password);
214 return;
215 }
216
217 strncpy(session[s].user, packet.username, sizeof(session[s].user) - 1);
218 memcpy(radius[r].pass, packet.password, 16);
219
220 free(packet.username);
221 free(packet.password);
222 }
223
224 radius[r].chap = 1;
225 LOG(3, s, t, "CHAP login %s\n", session[s].user);
226 radiussend(r, RADIUSAUTH);
227 }
228
229 static void dumplcp(uint8_t *p, int l)
230 {
231 int x = l - 4;
232 uint8_t *o = (p + 4);
233
234 LOG_HEX(5, "PPP LCP Packet", p, l);
235 LOG(4, 0, 0, "PPP LCP Packet type %d (%s len %d)\n", *p, ppp_lcp_type((int)*p), ntohs( ((uint16_t *) p)[1]) );
236 LOG(4, 0, 0, "Length: %d\n", l);
237 if (*p != ConfigReq && *p != ConfigRej && *p != ConfigAck)
238 return;
239
240 while (x > 2)
241 {
242 int type = o[0];
243 int length = o[1];
244 if (length < 2)
245 {
246 LOG(4, 0, 0, " Option length is %d...\n", length);
247 break;
248 }
249 if (type == 0)
250 {
251 LOG(4, 0, 0, " Option type is 0...\n");
252 x -= length;
253 o += length;
254 continue;
255 }
256 switch (type)
257 {
258 case 1: // Maximum-Receive-Unit
259 if (length == 4)
260 LOG(4, 0, 0, " %s %d\n", lcp_type(type), ntohs(*(uint16_t *)(o + 2)));
261 else
262 LOG(4, 0, 0, " %s odd length %d\n", lcp_type(type), length);
263 break;
264 case 2: // Async-Control-Character-Map
265 if (length == 6)
266 {
267 uint32_t asyncmap = ntohl(*(uint32_t *)(o + 2));
268 LOG(4, 0, 0, " %s %x\n", lcp_type(type), asyncmap);
269 }
270 else
271 LOG(4, 0, 0, " %s odd length %d\n", lcp_type(type), length);
272 break;
273 case 3: // Authentication-Protocol
274 if (length == 4)
275 {
276 int proto = ntohs(*(uint16_t *)(o + 2));
277 LOG(4, 0, 0, " %s 0x%x (%s)\n", lcp_type(type), proto,
278 proto == PPPCHAP ? "CHAP" :
279 proto == PPPPAP ? "PAP" : "UNKNOWN");
280 }
281 else
282 LOG(4, 0, 0, " %s odd length %d\n", lcp_type(type), length);
283 break;
284 case 4: // Quality-Protocol
285 {
286 uint32_t qp = ntohl(*(uint32_t *)(o + 2));
287 LOG(4, 0, 0, " %s %x\n", lcp_type(type), qp);
288 }
289 break;
290 case 5: // Magic-Number
291 if (length == 6)
292 {
293 uint32_t magicno = ntohl(*(uint32_t *)(o + 2));
294 LOG(4, 0, 0, " %s %x\n", lcp_type(type), magicno);
295 }
296 else
297 LOG(4, 0, 0, " %s odd length %d\n", lcp_type(type), length);
298 break;
299 case 7: // Protocol-Field-Compression
300 case 8: // Address-And-Control-Field-Compression
301 LOG(4, 0, 0, " %s\n", lcp_type(type));
302 break;
303 default:
304 LOG(2, 0, 0, " Unknown PPP LCP Option type %d\n", type);
305 break;
306 }
307 x -= length;
308 o += length;
309 }
310 }
311
312 // Process LCP messages
313 void processlcp(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
314 {
315 uint8_t b[MAXCONTROL];
316 uint8_t *q = NULL;
317 uint32_t magicno = 0;
318 uint16_t hl;
319
320 CSTAT(processlcp);
321
322 LOG_HEX(5, "LCP", p, l);
323 if (l < 4)
324 {
325 LOG(1, s, t, "Short LCP %d bytes\n", l);
326 STAT(tunnel_rx_errors);
327 return ;
328 }
329
330 if ((hl = ntohs(*(uint16_t *) (p + 2))) > l)
331 {
332 LOG(1, s, t, "Length mismatch LCP %u/%u\n", hl, l);
333 STAT(tunnel_rx_errors);
334 return ;
335 }
336 l = hl;
337
338 if (*p == ConfigAck)
339 {
340 LOG(3, s, t, "LCP: Discarding ConfigAck\n");
341 session[s].flags |= SF_LCP_ACKED;
342 }
343 else if (*p == ConfigReq)
344 {
345 int x = l - 4;
346 uint8_t *o = (p + 4);
347 uint8_t *response = 0;
348
349 LOG(3, s, t, "LCP: ConfigReq (%d bytes)...\n", l);
350 if (config->debug > 3) dumplcp(p, l);
351
352 while (x > 2)
353 {
354 int type = o[0];
355 int length = o[1];
356
357 if (length == 0 || type == 0 || x < length) break;
358 switch (type)
359 {
360 case 1: // Maximum-Receive-Unit
361 session[s].mru = ntohs(*(uint16_t *)(o + 2));
362 break;
363
364 case 2: // Async-Control-Character-Map
365 if (!ntohl(*(uint32_t *)(o + 2))) // all bits zero is OK
366 break;
367
368 if (response && *response != ConfigNak) // rej already queued
369 break;
370
371 LOG(2, s, t, " Remote requesting asyncmap. Rejecting.\n");
372 if (!response)
373 {
374 q = response = makeppp(b, sizeof(b), p, 2, t, s, PPPLCP);
375 if (!q) break;
376 *q = ConfigNak;
377 q += 4;
378 }
379
380 if ((q - b + 11) > sizeof(b))
381 {
382 LOG(2, s, t, "LCP overflow for asyncmap ConfigNak.\n");
383 break;
384 }
385
386 *q++ = type;
387 *q++ = 6;
388 memset(q, 0, 4); // asyncmap 0
389 q += 4;
390 *((uint16_t *) (response + 2)) = htons(q - response); // LCP header length
391 break;
392
393 case 3: // Authentication-Protocol
394 {
395 int proto = ntohs(*(uint16_t *)(o + 2));
396 char proto_name[] = "0x0000";
397 if (proto == PPPPAP)
398 break;
399
400 if (response && *response != ConfigNak) // rej already queued
401 break;
402
403 if (proto == PPPCHAP)
404 strcpy(proto_name, "CHAP");
405 else
406 sprintf(proto_name, "%#4.4x", proto);
407
408 LOG(2, s, t, " Remote requesting %s authentication. Rejecting.\n", proto_name);
409
410 if (!response)
411 {
412 q = response = makeppp(b, sizeof(b), p, 2, t, s, PPPLCP);
413 if (!q) break;
414 *q = ConfigNak;
415 q += 4;
416 }
417
418 if ((q - b + length) > sizeof(b))
419 {
420 LOG(2, s, t, "LCP overflow for %s ConfigNak.\n", proto_name);
421 break;
422 }
423
424 memcpy(q, o, length);
425 *(uint16_t *)(q += 2) = htons(PPPPAP); // NAK -> Use PAP instead
426 q += length;
427 *((uint16_t *) (response + 2)) = htons(q - response);
428 }
429 break;
430
431 case 5: // Magic-Number
432 magicno = ntohl(*(uint32_t *)(o + 2));
433 break;
434
435 case 4: // Quality-Protocol
436 case 7: // Protocol-Field-Compression
437 case 8: // Address-And-Control-Field-Compression
438 break;
439
440 default: // Reject any unknown options
441 LOG(2, s, t, " Rejecting PPP LCP Option type %d\n", type);
442 if (!response || *response != ConfigRej) // drop nak in favour of rej
443 {
444 q = response = makeppp(b, sizeof(b), p, 2, t, s, PPPLCP);
445 if (!q) break;
446 *q = ConfigRej;
447 q += 4;
448 }
449
450 if ((q - b + length) > sizeof(b))
451 {
452 LOG(2, s, t, "LCP overflow for ConfigRej (type=%d).\n", type);
453 break;
454 }
455
456 memcpy(q, o, length);
457 q += length;
458 *((uint16_t *) (response + 2)) = htons(q - response); // LCP header length
459 }
460 x -= length;
461 o += length;
462 }
463
464 if (!response)
465 {
466 // Send back a ConfigAck
467 q = response = makeppp(b, sizeof(b), p, l, t, s, PPPLCP);
468 if (!q) return;
469 *q = ConfigAck;
470 }
471
472 LOG(3, s, t, "Sending %s\n", ppp_lcp_type(*response));
473 tunnelsend(b, l + (q - b), t);
474
475 if (!(session[s].flags & SF_LCP_ACKED))
476 initlcp(t, s);
477 }
478 else if (*p == ConfigNak)
479 {
480 LOG(1, s, t, "Remote end sent a ConfigNak. Ignoring\n");
481 if (config->debug > 3) dumplcp(p, l);
482 return ;
483 }
484 else if (*p == TerminateReq)
485 {
486 LOG(3, s, t, "LCP: Received TerminateReq. Sending TerminateAck\n");
487 *p = TerminateAck; // close
488 q = makeppp(b, sizeof(b), p, l, t, s, PPPLCP);
489 if (!q) return;
490 tunnelsend(b, l + (q - b), t); // send it
491 sessionshutdown(s, "Remote end closed connection.");
492 }
493 else if (*p == TerminateAck)
494 {
495 sessionshutdown(s, "Connection closed.");
496 }
497 else if (*p == ProtocolRej)
498 {
499 if (*(uint16_t *) (p+4) == htons(PPPIPV6CP))
500 {
501 LOG(3, s, t, "IPv6 rejected\n");
502 session[s].flags |= SF_IPV6_NACKED;
503 }
504 else
505 {
506 LOG(1, s, t, "Unexpected LCP protocol reject 0x%X\n",
507 ntohs(*(uint16_t *) (p+4)));
508 STAT(tunnel_rx_errors);
509 }
510 }
511 else if (*p == EchoReq)
512 {
513 LOG(5, s, t, "LCP: Received EchoReq. Sending EchoReply\n");
514 *p = EchoReply; // reply
515 *(uint32_t *) (p + 4) = htonl(session[s].magic); // our magic number
516 q = makeppp(b, sizeof(b), p, l, t, s, PPPLCP);
517 if (!q) return;
518 tunnelsend(b, l + (q - b), t); // send it
519 }
520 else if (*p == EchoReply)
521 {
522 // Ignore it, last_packet time is set earlier than this.
523 }
524 else if (*p == IdentRequest)
525 {
526 *p = CodeRej;
527 if (l > MAXCONTROL)
528 {
529 LOG(1, s, t, "Truncated Ident Packet (length=%d) to 1400 bytes\n", l);
530 l = 1400;
531 }
532 q = makeppp(b, sizeof(b), p, l, t, s, PPPLCP);
533 if (!q) return;
534 LOG_HEX(5, "LCPIdentRej", q, l + 4);
535 tunnelsend(b, 12 + 4 + l, t);
536 }
537 else
538 {
539 LOG(1, s, t, "Unexpected LCP code %d\n", *p);
540 STAT(tunnel_rx_errors);
541 return ;
542 }
543 }
544
545 // find a PPP option, returns point to option, or 0 if not found
546 static uint8_t *findppp(uint8_t *b, uint8_t mtype)
547 {
548 uint16_t l = ntohs(*(uint16_t *) (b + 2));
549 if (l < 4)
550 return 0;
551 b += 4;
552 l -= 4;
553 while (l)
554 {
555 if (l < b[1] || !b[1])
556 return 0; // faulty
557 if (*b == mtype)
558 return b;
559 l -= b[1];
560 b += b[1];
561 }
562 return 0;
563 }
564
565 // Process IPCP messages
566 void processipcp(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
567 {
568 uint16_t hl;
569
570 CSTAT(processipcp);
571
572 LOG_HEX(5, "IPCP", p, l);
573 if (l < 5)
574 {
575 LOG(1, s, t, "Short IPCP %d bytes\n", l);
576 STAT(tunnel_rx_errors);
577 return ;
578 }
579
580 if ((hl = ntohs(*(uint16_t *) (p + 2))) > l)
581 {
582 LOG(1, s, t, "Length mismatch IPCP %u/%u\n", hl, l);
583 STAT(tunnel_rx_errors);
584 return ;
585 }
586 l = hl;
587
588 if (*p == ConfigAck)
589 {
590 // happy with our IPCP
591 uint16_t r = session[s].radius;
592 if ((!r || radius[r].state == RADIUSIPCP) && !session[s].walled_garden)
593 {
594 if (!r)
595 r = radiusnew(s);
596 if (r)
597 radiussend(r, RADIUSSTART); // send radius start, having got IPCP at last
598 }
599 session[s].flags |= SF_IPCP_ACKED;
600
601 LOG(3, s, t, "IPCP Acked, session is now active\n");
602
603 // clear LCP_ACKED/CCP_ACKED flag for possible fast renegotiaion for routers
604 session[s].flags &= ~(SF_LCP_ACKED|SF_CCP_ACKED);
605
606 return;
607 }
608 if (*p != ConfigReq)
609 {
610 LOG(1, s, t, "Unexpected IPCP code %d\n", *p);
611 STAT(tunnel_rx_errors);
612 return ;
613 }
614 LOG(4, s, t, "IPCP ConfigReq received\n");
615
616 if (!session[s].ip)
617 {
618 LOG(3, s, t, "Waiting on radius reply\n");
619 return; // have to wait on RADIUS reply
620 }
621 // form a config reply quoting the IP in the session
622 {
623 uint8_t b[MAXCONTROL];
624 uint8_t *i, *q;
625
626 q = p + 4;
627 i = p + l;
628 while (q < i && q[1])
629 {
630 if (*q != 0x81 && *q != 0x83 && *q != 3)
631 break;
632 q += q[1];
633 }
634 if (q < i)
635 {
636 // reject
637 uint16_t n = 4;
638 i = p + l;
639 if (!(q = makeppp(b, sizeof(b), p, l, t, s, PPPIPCP)))
640 return;
641
642 *q = ConfigRej;
643 p += 4;
644 while (p < i && p[1])
645 {
646 if (*p != 0x81 && *p != 0x83 && *p != 3)
647 {
648 LOG(2, s, t, "IPCP reject %d\n", *p);
649 memcpy(q + n, p, p[1]);
650 n += p[1];
651 }
652 p += p[1];
653 }
654 *(uint16_t *) (q + 2) = htons(n);
655 LOG(4, s, t, "Sending ConfigRej\n");
656 tunnelsend(b, n + (q - b), t); // send it
657 }
658 else
659 {
660 LOG(4, s, t, "Sending ConfigAck\n");
661 *p = ConfigAck;
662 if ((i = findppp(p, 0x81))) // Primary DNS address
663 {
664 if (*(uint32_t *) (i + 2) != htonl(session[s].dns1))
665 {
666 *(uint32_t *) (i + 2) = htonl(session[s].dns1);
667 *p = ConfigNak;
668 LOG(5, s, t, " DNS1 = %s\n",
669 fmtaddr(htonl(session[s].dns1), 0));
670 }
671 }
672 if ((i = findppp(p, 0x83))) // Secondary DNS address (TBA, is it)
673 {
674 if (*(uint32_t *) (i + 2) != htonl(session[s].dns2))
675 {
676 *(uint32_t *) (i + 2) = htonl(session[s].dns2);
677 *p = ConfigNak;
678 LOG(5, s, t, " DNS2 = %s\n",
679 fmtaddr(htonl(session[s].dns2), 0));
680 }
681 }
682 i = findppp(p, 3); // IP address
683 if (!i || i[1] != 6)
684 {
685 LOG(1, s, t, "No IP in IPCP request\n");
686 STAT(tunnel_rx_errors);
687 return ;
688 }
689 if (*(uint32_t *) (i + 2) != htonl(session[s].ip))
690 {
691 *(uint32_t *) (i + 2) = htonl(session[s].ip);
692 *p = ConfigNak;
693 LOG(4, s, t, " No, a ConfigNak, client is requesting IP - sending %s\n",
694 fmtaddr(htonl(session[s].ip), 0));
695 }
696 if (!(q = makeppp(b, sizeof(b), p, l, t, s, PPPIPCP)))
697 return;
698
699 tunnelsend(b, l + (q - b), t); // send it
700 }
701 }
702 }
703
704 // Process IPV6CP messages
705 void processipv6cp(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
706 {
707
708 CSTAT(processipv6cp);
709
710 LOG_HEX(5, "IPV6CP", p, l);
711 if (l < 4)
712 {
713 LOG(1, s, t, "Short IPV6CP %d bytes\n", l);
714 STAT(tunnel_rx_errors);
715 return ;
716 }
717 if (*p == ConfigAck)
718 {
719 // happy with our IPV6CP
720 session[s].flags |= SF_IPV6CP_ACKED;
721
722 LOG(3, s, t, "IPV6CP Acked, IPv6 is now active\n");
723 // Add a routed block if configured.
724 if (session[s].ipv6prefixlen)
725 {
726 route6set(s, session[s].ipv6route, session[s].ipv6prefixlen, 1);
727 session[s].flags |= SF_IPV6_ROUTED;
728 }
729
730 // Send an initial RA (TODO: Should we send these regularly?)
731 send_ipv6_ra(t, s, NULL);
732 return;
733 }
734 if (*p != ConfigReq)
735 {
736 LOG(1, s, t, "Unexpected IPV6CP code %d\n", *p);
737 STAT(tunnel_rx_errors);
738 return;
739 }
740
741 LOG(4, s, t, "IPV6CP ConfigReq received\n");
742 if (ntohs(*(uint16_t *) (p + 2)) > l)
743 {
744 LOG(1, s, t, "Length mismatch IPV6CP %d/%d\n", ntohs(*(uint16_t *) (p + 2)), l);
745 STAT(tunnel_rx_errors);
746 return ;
747 }
748 if (!session[s].ip)
749 {
750 LOG(3, s, t, "Waiting on radius reply\n");
751 return; // have to wait on RADIUS reply
752 }
753 // form a config reply quoting the IP in the session
754 {
755 uint8_t b[MAXCONTROL];
756 uint8_t *i,
757 *q;
758
759 l = ntohs(*(uint16_t *) (p + 2)); // We must use length from IPV6CP len field
760 q = p + 4;
761 i = p + l;
762 while (q < i && q[1])
763 {
764 if (*q != 1)
765 break;
766 q += q[1];
767 }
768 if (q < i)
769 {
770 // reject
771 uint16_t n = 4;
772 i = p + l;
773 if (!(q = makeppp(b, sizeof(b), p, l, t, s, PPPIPV6CP)))
774 {
775 LOG(2, s, t, "Failed to send IPV6CP ConfigRej\n");
776 return;
777 }
778 *q = ConfigRej;
779 p += 4;
780 while (p < i && p[1])
781 {
782 if (*p != 1)
783 {
784 LOG(2, s, t, "IPV6CP reject %d\n", *p);
785 memcpy(q + n, p, p[1]);
786 n += p[1];
787 }
788 p += p[1];
789 }
790 *(uint16_t *) (q + 2) = htons(n);
791 LOG(4, s, t, "Sending ConfigRej\n");
792 tunnelsend(b, n + (q - b), t); // send it
793 }
794 else
795 {
796 LOG(4, s, t, "Sending ConfigAck\n");
797 *p = ConfigAck;
798 i = findppp(p, 1); // IP address
799 if (!i || i[1] != 10)
800 {
801 LOG(1, s, t, "No IP in IPV6CP request\n");
802 STAT(tunnel_rx_errors);
803 return ;
804 }
805 if ((*(uint32_t *) (i + 2) != htonl(session[s].ip)) ||
806 (*(uint32_t *) (i + 6) != 0))
807 {
808 *(uint32_t *) (i + 2) = htonl(session[s].ip);
809 *(uint32_t *) (i + 6) = 0;
810 *p = ConfigNak;
811 LOG(4, s, t,
812 " No, a ConfigNak, client is "
813 "requesting IP - sending %s\n",
814 fmtaddr(htonl(session[s].ip), 0));
815 }
816 if (!(q = makeppp(b, sizeof(b), p, l, t, s, PPPIPV6CP)))
817 {
818 LOG(2, s, t, " Failed to send IPV6CP packet.\n");
819 return;
820 }
821 tunnelsend(b, l + (q - b), t); // send it
822 }
823 }
824 }
825
826 // process IP packet received
827 //
828 // This MUST be called with at least 4 byte behind 'p'.
829 // (i.e. this routine writes to p[-4]).
830 void processipin(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
831 {
832 in_addr_t ip;
833
834 CSTAT(processipin);
835
836 LOG_HEX(5, "IP", p, l);
837
838 ip = ntohl(*(uint32_t *)(p + 12));
839
840 if (l > MAXETHER)
841 {
842 LOG(1, s, t, "IP packet too long %d\n", l);
843 STAT(tunnel_rx_errors);
844 return ;
845 }
846
847 // no spoof (do sessionbyip to handled statically routed subnets)
848 if (ip != session[s].ip && sessionbyip(htonl(ip)) != s)
849 {
850 LOG(5, s, t, "Dropping packet with spoofed IP %s\n", fmtaddr(htonl(ip), 0));
851 return;
852 }
853
854 // run access-list if any
855 if (session[s].filter_in && !ip_filter(p, l, session[s].filter_in - 1))
856 return;
857
858 // Add on the tun header
859 p -= 4;
860 *(uint32_t *) p = htonl(PKTIP);
861 l += 4;
862
863 // Are we throttled and a slave?
864 if (session[s].tbf_in && !config->cluster_iam_master) {
865 // Pass it to the master for handling.
866 master_throttle_packet(session[s].tbf_in, p, l);
867 return;
868 }
869
870 // Are we throttled and a master??
871 if (session[s].tbf_in && config->cluster_iam_master) {
872 // Actually handle the throttled packets.
873 tbf_queue_packet(session[s].tbf_in, p, l);
874 return;
875 }
876
877 // send to ethernet
878 if (tun_write(p, l) < 0)
879 {
880 STAT(tun_tx_errors);
881 LOG(0, s, t, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
882 l, strerror(errno), tunfd, p);
883
884 return;
885 }
886
887 if (session[s].snoop_ip && session[s].snoop_port)
888 {
889 // Snooping this session
890 snoop_send_packet(p + 4, l - 4, session[s].snoop_ip, session[s].snoop_port);
891 }
892
893 session[s].cin += l - 4;
894 session[s].total_cin += l - 4;
895 sess_local[s].cin += l - 4;
896
897 session[s].pin++;
898 eth_tx += l - 4;
899
900 STAT(tun_tx_packets);
901 INC_STAT(tun_tx_bytes, l - 4);
902 }
903
904 // process IPv6 packet received
905 //
906 // This MUST be called with at least 4 byte behind 'p'.
907 // (i.e. this routine writes to p[-4]).
908 void processipv6in(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
909 {
910 struct in6_addr ip;
911 in_addr_t ipv4;
912
913 CSTAT(processipv6in);
914
915 LOG_HEX(5, "IPv6", p, l);
916
917 ip = *(struct in6_addr *) (p + 8);
918 ipv4 = ntohl(*(uint32_t *)(p + 16));
919
920 if (l > MAXETHER)
921 {
922 LOG(1, s, t, "IP packet too long %d\n", l);
923 STAT(tunnel_rx_errors);
924 return ;
925 }
926
927 // no spoof
928 if (ipv4 != session[s].ip && memcmp(&config->ipv6_prefix, &ip, 8) && sessionbyipv6(ip) != s)
929 {
930 char str[INET6_ADDRSTRLEN];
931 LOG(5, s, t, "Dropping packet with spoofed IP %s\n",
932 inet_ntop(AF_INET6, &ip, str, INET6_ADDRSTRLEN));
933 return;
934 }
935
936 // Check if it's a Router Solicition message.
937 if (*(p + 6) == 58 && *(p + 7) == 255 && *(p + 24) == 0xFF && *(p + 25) == 2 &&
938 *(uint32_t *)(p + 26) == 0 && *(uint32_t *)(p + 30) == 0 &&
939 *(uint32_t *)(p + 34) == 0 &&
940 *(p + 38) == 0 && *(p + 39) == 2 && *(p + 40) == 133) {
941 LOG(3, s, t, "Got IPv6 RS\n");
942 send_ipv6_ra(t, s, &ip);
943 return;
944 }
945
946 // Add on the tun header
947 p -= 4;
948 *(uint32_t *) p = htonl(PKTIPV6);
949 l += 4;
950
951 // Are we throttled and a slave?
952 if (session[s].tbf_in && !config->cluster_iam_master) {
953 // Pass it to the master for handling.
954 master_throttle_packet(session[s].tbf_in, p, l);
955 return;
956 }
957
958 // Are we throttled and a master??
959 if (session[s].tbf_in && config->cluster_iam_master) {
960 // Actually handle the throttled packets.
961 tbf_queue_packet(session[s].tbf_in, p, l);
962 return;
963 }
964
965 // send to ethernet
966 if (tun_write(p, l) < 0)
967 {
968 STAT(tun_tx_errors);
969 LOG(0, s, t, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
970 l, strerror(errno), tunfd, p);
971
972 return;
973 }
974
975 if (session[s].snoop_ip && session[s].snoop_port)
976 {
977 // Snooping this session
978 snoop_send_packet(p + 4, l - 4, session[s].snoop_ip, session[s].snoop_port);
979 }
980
981 session[s].cin += l - 4;
982 session[s].total_cin += l - 4;
983 sess_local[s].cin += l - 4;
984
985 session[s].pin++;
986 eth_tx += l - 4;
987
988 STAT(tun_tx_packets);
989 INC_STAT(tun_tx_bytes, l - 4);
990 }
991
992 //
993 // Helper routine for the TBF filters.
994 // Used to send queued data in from the user.
995 //
996 void send_ipin(sessionidt s, uint8_t *buf, int len)
997 {
998 LOG_HEX(5, "IP in throttled", buf, len);
999
1000 if (write(tunfd, buf, len) < 0)
1001 {
1002 STAT(tun_tx_errors);
1003 LOG(0, 0, 0, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1004 len, strerror(errno), tunfd, buf);
1005
1006 return;
1007 }
1008
1009 if (session[s].snoop_ip && session[s].snoop_port)
1010 {
1011 // Snooping this session
1012 snoop_send_packet(buf + 4, len - 4, session[s].snoop_ip, session[s].snoop_port);
1013 }
1014
1015 // Increment packet counters
1016 session[s].cin += len - 4;
1017 session[s].total_cin += len - 4;
1018 sess_local[s].cin += len - 4;
1019
1020 session[s].pin++;
1021 eth_tx += len - 4;
1022
1023 STAT(tun_tx_packets);
1024 INC_STAT(tun_tx_bytes, len - 4);
1025 }
1026
1027
1028 // Process CCP messages
1029 void processccp(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
1030 {
1031 uint8_t b[MAXCONTROL];
1032 uint8_t *q;
1033
1034 CSTAT(processccp);
1035
1036 LOG_HEX(5, "CCP", p, l);
1037 switch (l > 1 ? *p : 0)
1038 {
1039 case ConfigAck:
1040 session[s].flags |= SF_CCP_ACKED;
1041 return;
1042
1043 case ConfigReq:
1044 if (l < 6) // accept no compression
1045 {
1046 *p = ConfigAck;
1047 break;
1048 }
1049
1050 // compression requested--reject
1051 *p = ConfigRej;
1052
1053 // send CCP request for no compression for our end if not negotiated
1054 if (!(session[s].flags & SF_CCP_ACKED))
1055 initccp(t, s);
1056
1057 break;
1058
1059 case TerminateReq:
1060 *p = TerminateAck;
1061 break;
1062
1063 default:
1064 if (l > 1)
1065 LOG(1, s, t, "Unexpected CCP request code %d\n", *p);
1066 else
1067 LOG(1, s, t, "Short CCP packet\n");
1068
1069 STAT(tunnel_rx_errors);
1070 return;
1071 }
1072
1073 if (!(q = makeppp(b, sizeof(b), p, l, t, s, PPPCCP)))
1074 return;
1075
1076 tunnelsend(b, l + (q - b), t); // send it
1077 }
1078
1079 // send a CHAP PP packet
1080 void sendchap(tunnelidt t, sessionidt s)
1081 {
1082 uint8_t b[MAXCONTROL];
1083 uint16_t r = session[s].radius;
1084 uint8_t *q;
1085
1086 CSTAT(sendchap);
1087
1088 if (!r)
1089 {
1090 LOG(1, s, t, "No RADIUS to send challenge\n");
1091 STAT(tunnel_tx_errors);
1092 return;
1093 }
1094
1095 LOG(1, s, t, "Send CHAP challenge\n");
1096
1097 // new challenge
1098 random_data(radius[r].auth, sizeof(radius[r].auth));
1099 radius[r].chap = 1; // CHAP not PAP
1100 radius[r].id++;
1101 if (radius[r].state != RADIUSCHAP)
1102 radius[r].try = 0;
1103
1104 radius[r].state = RADIUSCHAP;
1105 radius[r].retry = backoff(radius[r].try++);
1106 if (radius[r].try > 5)
1107 {
1108 sessionshutdown(s, "Timeout CHAP");
1109 STAT(tunnel_tx_errors);
1110 return ;
1111 }
1112 q = makeppp(b, sizeof(b), 0, 0, t, s, PPPCHAP);
1113 if (!q) return;
1114
1115 *q = 1; // challenge
1116 q[1] = radius[r].id; // ID
1117 q[4] = 16; // length
1118 memcpy(q + 5, radius[r].auth, 16); // challenge
1119 strcpy(q + 21, hostname); // our name
1120 *(uint16_t *) (q + 2) = htons(strlen(hostname) + 21); // length
1121 tunnelsend(b, strlen(hostname) + 21 + (q - b), t); // send it
1122 }
1123
1124 // fill in a L2TP message with a PPP frame,
1125 // copies existing PPP message and changes magic number if seen
1126 // returns start of PPP frame
1127 uint8_t *makeppp(uint8_t *b, int size, uint8_t *p, int l, tunnelidt t, sessionidt s, uint16_t mtype)
1128 {
1129 if (size < 12) // Need more space than this!!
1130 {
1131 static int backtrace_count = 0;
1132 LOG(0, s, t, "makeppp buffer too small for L2TP header (size=%d)\n", size);
1133 log_backtrace(backtrace_count, 5)
1134 return NULL;
1135 }
1136
1137 *(uint16_t *) (b + 0) = htons(0x0002); // L2TP with no options
1138 *(uint16_t *) (b + 2) = htons(tunnel[t].far); // tunnel
1139 *(uint16_t *) (b + 4) = htons(session[s].far); // session
1140 b += 6;
1141 if (mtype == PPPLCP || !(session[s].l2tp_flags & SESSIONACFC))
1142 {
1143 *(uint16_t *) b = htons(0xFF03); // HDLC header
1144 b += 2;
1145 }
1146 if (mtype < 0x100 && session[s].l2tp_flags & SESSIONPFC)
1147 *b++ = mtype;
1148 else
1149 {
1150 *(uint16_t *) b = htons(mtype);
1151 b += 2;
1152 }
1153
1154 if (l + 12 > size)
1155 {
1156 static int backtrace_count = 0;
1157 LOG(2, s, t, "makeppp would overflow buffer (size=%d, header+payload=%d)\n", size, l + 12);
1158 log_backtrace(backtrace_count, 5)
1159 return NULL;
1160 }
1161
1162 if (p && l)
1163 memcpy(b, p, l);
1164
1165 return b;
1166 }
1167
1168 // Send initial LCP ConfigReq for PAP, set magic no.
1169 void initlcp(tunnelidt t, sessionidt s)
1170 {
1171 char b[500], *q;
1172
1173 if (!(q = makeppp(b, sizeof(b), NULL, 0, t, s, PPPLCP)))
1174 return;
1175
1176 LOG(4, s, t, "Sending LCP ConfigReq for PAP\n");
1177 *q = ConfigReq;
1178 *(uint8_t *)(q + 1) = (time_now % 255) + 1; // ID
1179 *(uint16_t *)(q + 2) = htons(14); // Length
1180 *(uint8_t *)(q + 4) = 5;
1181 *(uint8_t *)(q + 5) = 6;
1182 *(uint32_t *)(q + 6) = htonl(session[s].magic);
1183 *(uint8_t *)(q + 10) = 3;
1184 *(uint8_t *)(q + 11) = 4;
1185 *(uint16_t *)(q + 12) = htons(PPPPAP); // PAP
1186
1187 LOG_HEX(5, "PPPLCP", q, 14);
1188 tunnelsend(b, (q - b) + 14, t);
1189 }
1190
1191 // Send CCP request for no compression
1192 static void initccp(tunnelidt t, sessionidt s)
1193 {
1194 char b[500], *q;
1195
1196 if (!(q = makeppp(b, sizeof(b), NULL, 0, t, s, PPPCCP)))
1197 return;
1198
1199 LOG(4, s, t, "Sending CCP ConfigReq for no compression\n");
1200 *q = ConfigReq;
1201 *(uint8_t *)(q + 1) = (time_now % 255) + 1; // ID
1202 *(uint16_t *)(q + 2) = htons(4); // Length
1203
1204 LOG_HEX(5, "PPPCCP", q, 4);
1205 tunnelsend(b, (q - b) + 4 , t);
1206 }