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