reset length of lcp packets
[l2tpns.git] / ppp.c
1 // L2TPNS PPP Stuff
2
3 char const *cvs_id_ppp = "$Id: ppp.c,v 1.55 2005/05/10 09:35:27 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 LOG(3, s, t, "LCP: Discarding ConfigAck\n");
359 session[s].flags |= SF_LCP_ACKED;
360 }
361 else if (*p == ConfigReq)
362 {
363 int x = l - 4;
364 uint8_t *o = (p + 4);
365 uint8_t *response = 0;
366
367 LOG(3, s, t, "LCP: ConfigReq (%d bytes)...\n", l);
368 if (config->debug > 3) dumplcp(p, l);
369
370 while (x > 2)
371 {
372 int type = o[0];
373 int length = o[1];
374
375 if (length == 0 || type == 0 || x < length) break;
376 switch (type)
377 {
378 case 1: // Maximum-Receive-Unit
379 session[s].mru = ntohs(*(uint16_t *)(o + 2));
380 break;
381
382 case 2: // Async-Control-Character-Map
383 if (!ntohl(*(uint32_t *)(o + 2))) // all bits zero is OK
384 break;
385
386 if (response && *response != ConfigNak) // rej already queued
387 break;
388
389 LOG(2, s, t, " Remote requesting asyncmap. Rejecting.\n");
390 if (!response)
391 {
392 q = response = makeppp(b, sizeof(b), p, 2, t, s, PPPLCP);
393 if (!q) break;
394 *q = ConfigNak;
395 q += 4;
396 }
397
398 if ((q - b + 11) > sizeof(b))
399 {
400 LOG(2, s, t, "LCP overflow for asyncmap ConfigNak.\n");
401 break;
402 }
403
404 *q++ = type;
405 *q++ = 6;
406 memset(q, 0, 4); // asyncmap 0
407 q += 4;
408 *((uint16_t *) (response + 2)) = htons(l = q - response); // LCP header length
409 break;
410
411 case 3: // Authentication-Protocol
412 {
413 int proto = ntohs(*(uint16_t *)(o + 2));
414 char proto_name[] = "0x0000";
415 uint8_t *a;
416
417 if (proto == PPPPAP)
418 {
419 if (config->radius_authtypes & AUTHPAP)
420 break;
421
422 strcpy(proto_name, "PAP");
423 }
424 else if (proto == PPPCHAP)
425 {
426 if (config->radius_authtypes & AUTHCHAP
427 && *(o + 4) == 5) // MD5
428 break;
429
430 strcpy(proto_name, "CHAP");
431 }
432 else
433 sprintf(proto_name, "%#4.4x", proto);
434
435 if (response && *response != ConfigNak) // rej already queued
436 break;
437
438 LOG(2, s, t, " Remote requesting %s authentication. Rejecting.\n", proto_name);
439
440 if (!response)
441 {
442 q = response = makeppp(b, sizeof(b), p, 2, t, s, PPPLCP);
443 if (!q) break;
444 *q = ConfigNak;
445 q += 4;
446 }
447
448 a = add_lcp_auth(q, sizeof(b) - (q - b), config->radius_authprefer);
449 if (!a)
450 {
451 LOG(2, s, t, "LCP overflow for %s ConfigNak.\n", proto_name);
452 break;
453 }
454
455 q = a;
456
457 if (config->radius_authtypes != config->radius_authprefer)
458 {
459 a = add_lcp_auth(q, sizeof(b) - (q - b), config->radius_authtypes & ~config->radius_authprefer);
460 if (!a)
461 {
462 LOG(2, s, t, "LCP overflow for %s ConfigNak.\n", proto_name);
463 break;
464 }
465
466 q = a;
467 }
468
469 *((uint16_t *) (response + 2)) = htons(l = q - response); // LCP header length
470 break;
471 }
472 break;
473
474 case 5: // Magic-Number
475 magicno = ntohl(*(uint32_t *)(o + 2));
476 break;
477
478 case 4: // Quality-Protocol
479 case 7: // Protocol-Field-Compression
480 case 8: // Address-And-Control-Field-Compression
481 break;
482
483 default: // Reject any unknown options
484 LOG(2, s, t, " Rejecting PPP LCP Option type %d\n", type);
485 if (!response || *response != ConfigRej) // drop nak in favour of rej
486 {
487 q = response = makeppp(b, sizeof(b), p, 2, t, s, PPPLCP);
488 if (!q) break;
489 *q = ConfigRej;
490 q += 4;
491 }
492
493 if ((q - b + length) > sizeof(b))
494 {
495 LOG(2, s, t, "LCP overflow for ConfigRej (type=%d).\n", type);
496 break;
497 }
498
499 memcpy(q, o, length);
500 q += length;
501 *((uint16_t *) (response + 2)) = htons(l = q - response); // LCP header length
502 }
503 x -= length;
504 o += length;
505 }
506
507 if (!response)
508 {
509 // Send back a ConfigAck
510 q = response = makeppp(b, sizeof(b), p, l, t, s, PPPLCP);
511 if (!q) return;
512 *q = ConfigAck;
513 }
514
515 LOG(3, s, t, "Sending %s\n", ppp_lcp_type(*response));
516 tunnelsend(b, l + response - b, t);
517
518 if (!(session[s].flags & SF_LCP_ACKED))
519 sendlcp(t, s, config->radius_authprefer);
520 }
521 else if (*p == ConfigNak)
522 {
523 int x = l - 4;
524 uint8_t *o = (p + 4);
525 int authtype = 0;
526
527 LOG(3, s, t, "LCP: ConfigNak (%d bytes)...\n", l);
528 if (config->debug > 3) dumplcp(p, l);
529
530 while (x > 2)
531 {
532 int type = o[0];
533 int length = o[1];
534
535 if (length == 0 || type == 0 || x < length) break;
536 switch (type)
537 {
538 case 1: // Maximum-Receive-Unit
539 session[s].mru = ntohs(*(uint16_t *)(o + 2));
540 LOG(3, s, t, " Remote requested MRU of %u\n", session[s].mru);
541 break;
542
543 case 3: // Authentication-Protocol
544 if (authtype)
545 break;
546
547 {
548 int proto = ntohs(*(uint16_t *)(o + 2));
549 if (proto == PPPPAP)
550 {
551 authtype = config->radius_authtypes & AUTHPAP;
552 LOG(3, s, t, " Remote requested PAP authentication...%sing\n",
553 authtype ? "accept" : "reject");
554 }
555 else if (proto == PPPCHAP && *(o + 4) == 5)
556 {
557 authtype = config->radius_authtypes & AUTHCHAP;
558 LOG(3, s, t, " Remote requested CHAP authentication...%sing\n",
559 authtype ? "accept" : "reject");
560 }
561 else
562 {
563 LOG(3, s, t, " Rejecting unsupported authentication %#4x\n",
564 proto);
565 }
566 }
567
568 if (!authtype)
569 {
570 sessionshutdown(s, "Unsupported authentication.", 3, 0);
571 return;
572 }
573
574 break;
575
576 default:
577 LOG(2, s, t, " Remote NAKed LCP type %u?\n", type);
578 break;
579 }
580 }
581
582 if (!authtype)
583 authtype = config->radius_authprefer;
584
585 sendlcp(t, s, authtype);
586 }
587 else if (*p == TerminateReq)
588 {
589 LOG(3, s, t, "LCP: Received TerminateReq. Sending TerminateAck\n");
590 *p = TerminateAck; // close
591 q = makeppp(b, sizeof(b), p, l, t, s, PPPLCP);
592 if (!q) return;
593 tunnelsend(b, l + (q - b), t); // send it
594 sessionshutdown(s, "Remote end closed connection.", 3, 0);
595 }
596 else if (*p == TerminateAck)
597 {
598 sessionshutdown(s, "Connection closed.", 3, 0);
599 }
600 else if (*p == ProtocolRej)
601 {
602 if (*(uint16_t *) (p+4) == htons(PPPIPV6CP))
603 {
604 LOG(3, s, t, "IPv6 rejected\n");
605 session[s].flags |= SF_IPV6_NACKED;
606 }
607 else
608 {
609 LOG(1, s, t, "Unexpected LCP protocol reject 0x%X\n",
610 ntohs(*(uint16_t *) (p+4)));
611 STAT(tunnel_rx_errors);
612 }
613 }
614 else if (*p == EchoReq)
615 {
616 LOG(5, s, t, "LCP: Received EchoReq. Sending EchoReply\n");
617 *p = EchoReply; // reply
618 *(uint32_t *) (p + 4) = htonl(session[s].magic); // our magic number
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 }
623 else if (*p == EchoReply)
624 {
625 // Ignore it, last_packet time is set earlier than this.
626 }
627 else if (*p == IdentRequest)
628 {
629 *p = CodeRej;
630 if (l > MAXCONTROL)
631 {
632 LOG(1, s, t, "Truncated Ident Packet (length=%d) to 1400 bytes\n", l);
633 l = 1400;
634 }
635 q = makeppp(b, sizeof(b), p, l, t, s, PPPLCP);
636 if (!q) return;
637 LOG_HEX(5, "LCPIdentRej", q, l + 4);
638 tunnelsend(b, 12 + 4 + l, t);
639 }
640 else
641 {
642 LOG(1, s, t, "Unexpected LCP code %d\n", *p);
643 STAT(tunnel_rx_errors);
644 }
645 }
646
647 // find a PPP option, returns point to option, or 0 if not found
648 static uint8_t *findppp(uint8_t *b, uint8_t mtype)
649 {
650 uint16_t l = ntohs(*(uint16_t *) (b + 2));
651 if (l < 4)
652 return 0;
653 b += 4;
654 l -= 4;
655 while (l)
656 {
657 if (l < b[1] || !b[1])
658 return 0; // faulty
659 if (*b == mtype)
660 return b;
661 l -= b[1];
662 b += b[1];
663 }
664 return 0;
665 }
666
667 // Process IPCP messages
668 void processipcp(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
669 {
670 uint16_t hl;
671
672 CSTAT(processipcp);
673
674 LOG_HEX(5, "IPCP", p, l);
675 if (l < 5)
676 {
677 LOG(1, s, t, "Short IPCP %d bytes\n", l);
678 STAT(tunnel_rx_errors);
679 return ;
680 }
681
682 if ((hl = ntohs(*(uint16_t *) (p + 2))) > l)
683 {
684 LOG(1, s, t, "Length mismatch IPCP %u/%u\n", hl, l);
685 STAT(tunnel_rx_errors);
686 return ;
687 }
688 l = hl;
689
690 if (*p == ConfigAck)
691 {
692 // happy with our IPCP
693 uint16_t r = sess_local[s].radius;
694 if ((!r || radius[r].state == RADIUSIPCP) && !session[s].walled_garden)
695 {
696 if (!r)
697 r = radiusnew(s);
698 if (r)
699 radiussend(r, RADIUSSTART); // send radius start, having got IPCP at last
700 }
701 session[s].flags |= SF_IPCP_ACKED;
702
703 LOG(3, s, t, "IPCP Acked, session is now active\n");
704
705 // clear LCP_ACKED/CCP_ACKED flag for possible fast renegotiaion for routers
706 session[s].flags &= ~(SF_LCP_ACKED|SF_CCP_ACKED);
707
708 return;
709 }
710 if (*p != ConfigReq)
711 {
712 LOG(1, s, t, "Unexpected IPCP code %d\n", *p);
713 STAT(tunnel_rx_errors);
714 return ;
715 }
716 LOG(4, s, t, "IPCP ConfigReq received\n");
717
718 if (!session[s].ip)
719 {
720 LOG(3, s, t, "Waiting on radius reply\n");
721 return; // have to wait on RADIUS reply
722 }
723 // form a config reply quoting the IP in the session
724 {
725 uint8_t b[MAXCONTROL];
726 uint8_t *i, *q;
727
728 q = p + 4;
729 i = p + l;
730 while (q < i && q[1])
731 {
732 if (*q != 0x81 && *q != 0x83 && *q != 3)
733 break;
734 q += q[1];
735 }
736 if (q < i)
737 {
738 // reject
739 uint16_t n = 4;
740 i = p + l;
741 if (!(q = makeppp(b, sizeof(b), p, l, t, s, PPPIPCP)))
742 return;
743
744 *q = ConfigRej;
745 p += 4;
746 while (p < i && p[1])
747 {
748 if (*p != 0x81 && *p != 0x83 && *p != 3)
749 {
750 LOG(2, s, t, "IPCP reject %d\n", *p);
751 memcpy(q + n, p, p[1]);
752 n += p[1];
753 }
754 p += p[1];
755 }
756 *(uint16_t *) (q + 2) = htons(n);
757 LOG(4, s, t, "Sending ConfigRej\n");
758 tunnelsend(b, n + (q - b), t); // send it
759 }
760 else
761 {
762 LOG(4, s, t, "Sending ConfigAck\n");
763 *p = ConfigAck;
764 if ((i = findppp(p, 0x81))) // Primary DNS address
765 {
766 if (*(uint32_t *) (i + 2) != htonl(session[s].dns1))
767 {
768 *(uint32_t *) (i + 2) = htonl(session[s].dns1);
769 *p = ConfigNak;
770 LOG(5, s, t, " DNS1 = %s\n",
771 fmtaddr(htonl(session[s].dns1), 0));
772 }
773 }
774 if ((i = findppp(p, 0x83))) // Secondary DNS address (TBA, is it)
775 {
776 if (*(uint32_t *) (i + 2) != htonl(session[s].dns2))
777 {
778 *(uint32_t *) (i + 2) = htonl(session[s].dns2);
779 *p = ConfigNak;
780 LOG(5, s, t, " DNS2 = %s\n",
781 fmtaddr(htonl(session[s].dns2), 0));
782 }
783 }
784 i = findppp(p, 3); // IP address
785 if (!i || i[1] != 6)
786 {
787 LOG(1, s, t, "No IP in IPCP request\n");
788 STAT(tunnel_rx_errors);
789 return ;
790 }
791 if (*(uint32_t *) (i + 2) != htonl(session[s].ip))
792 {
793 *(uint32_t *) (i + 2) = htonl(session[s].ip);
794 *p = ConfigNak;
795 LOG(4, s, t, " No, a ConfigNak, client is requesting IP - sending %s\n",
796 fmtaddr(htonl(session[s].ip), 0));
797 }
798 if (!(q = makeppp(b, sizeof(b), p, l, t, s, PPPIPCP)))
799 return;
800
801 tunnelsend(b, l + (q - b), t); // send it
802 }
803 }
804 }
805
806 // Process IPV6CP messages
807 void processipv6cp(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
808 {
809
810 CSTAT(processipv6cp);
811
812 LOG_HEX(5, "IPV6CP", p, l);
813 if (l < 4)
814 {
815 LOG(1, s, t, "Short IPV6CP %d bytes\n", l);
816 STAT(tunnel_rx_errors);
817 return ;
818 }
819 if (*p == ConfigAck)
820 {
821 // happy with our IPV6CP
822 session[s].flags |= SF_IPV6CP_ACKED;
823
824 LOG(3, s, t, "IPV6CP Acked, IPv6 is now active\n");
825 // Add a routed block if configured.
826 if (session[s].ipv6prefixlen)
827 {
828 route6set(s, session[s].ipv6route, session[s].ipv6prefixlen, 1);
829 session[s].flags |= SF_IPV6_ROUTED;
830 }
831
832 // Send an initial RA (TODO: Should we send these regularly?)
833 send_ipv6_ra(t, s, NULL);
834 return;
835 }
836 if (*p != ConfigReq)
837 {
838 LOG(1, s, t, "Unexpected IPV6CP code %d\n", *p);
839 STAT(tunnel_rx_errors);
840 return;
841 }
842
843 LOG(4, s, t, "IPV6CP ConfigReq received\n");
844 if (ntohs(*(uint16_t *) (p + 2)) > l)
845 {
846 LOG(1, s, t, "Length mismatch IPV6CP %d/%d\n", ntohs(*(uint16_t *) (p + 2)), l);
847 STAT(tunnel_rx_errors);
848 return ;
849 }
850 if (!session[s].ip)
851 {
852 LOG(3, s, t, "Waiting on radius reply\n");
853 return; // have to wait on RADIUS reply
854 }
855 // form a config reply quoting the IP in the session
856 {
857 uint8_t b[MAXCONTROL];
858 uint8_t *i,
859 *q;
860
861 l = ntohs(*(uint16_t *) (p + 2)); // We must use length from IPV6CP len field
862 q = p + 4;
863 i = p + l;
864 while (q < i && q[1])
865 {
866 if (*q != 1)
867 break;
868 q += q[1];
869 }
870 if (q < i)
871 {
872 // reject
873 uint16_t n = 4;
874 i = p + l;
875 if (!(q = makeppp(b, sizeof(b), p, l, t, s, PPPIPV6CP)))
876 {
877 LOG(2, s, t, "Failed to send IPV6CP ConfigRej\n");
878 return;
879 }
880 *q = ConfigRej;
881 p += 4;
882 while (p < i && p[1])
883 {
884 if (*p != 1)
885 {
886 LOG(2, s, t, "IPV6CP reject %d\n", *p);
887 memcpy(q + n, p, p[1]);
888 n += p[1];
889 }
890 p += p[1];
891 }
892 *(uint16_t *) (q + 2) = htons(n);
893 LOG(4, s, t, "Sending ConfigRej\n");
894 tunnelsend(b, n + (q - b), t); // send it
895 }
896 else
897 {
898 LOG(4, s, t, "Sending ConfigAck\n");
899 *p = ConfigAck;
900 i = findppp(p, 1); // IP address
901 if (!i || i[1] != 10)
902 {
903 LOG(1, s, t, "No IP in IPV6CP request\n");
904 STAT(tunnel_rx_errors);
905 return ;
906 }
907 if ((*(uint32_t *) (i + 2) != htonl(session[s].ip)) ||
908 (*(uint32_t *) (i + 6) != 0))
909 {
910 *(uint32_t *) (i + 2) = htonl(session[s].ip);
911 *(uint32_t *) (i + 6) = 0;
912 *p = ConfigNak;
913 LOG(4, s, t,
914 " No, a ConfigNak, client is "
915 "requesting IP - sending %s\n",
916 fmtaddr(htonl(session[s].ip), 0));
917 }
918 if (!(q = makeppp(b, sizeof(b), p, l, t, s, PPPIPV6CP)))
919 {
920 LOG(2, s, t, " Failed to send IPV6CP packet.\n");
921 return;
922 }
923 tunnelsend(b, l + (q - b), t); // send it
924 }
925 }
926 }
927
928 // process IP packet received
929 //
930 // This MUST be called with at least 4 byte behind 'p'.
931 // (i.e. this routine writes to p[-4]).
932 void processipin(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
933 {
934 in_addr_t ip;
935
936 CSTAT(processipin);
937
938 LOG_HEX(5, "IP", p, l);
939
940 ip = ntohl(*(uint32_t *)(p + 12));
941
942 if (l > MAXETHER)
943 {
944 LOG(1, s, t, "IP packet too long %d\n", l);
945 STAT(tunnel_rx_errors);
946 return ;
947 }
948
949 // no spoof (do sessionbyip to handled statically routed subnets)
950 if (ip != session[s].ip && sessionbyip(htonl(ip)) != s)
951 {
952 LOG(5, s, t, "Dropping packet with spoofed IP %s\n", fmtaddr(htonl(ip), 0));
953 return;
954 }
955
956 // run access-list if any
957 if (session[s].filter_in && !ip_filter(p, l, session[s].filter_in - 1))
958 return;
959
960 // Add on the tun header
961 p -= 4;
962 *(uint32_t *) p = htonl(PKTIP);
963 l += 4;
964
965 // Are we throttled and a slave?
966 if (session[s].tbf_in && !config->cluster_iam_master) {
967 // Pass it to the master for handling.
968 master_throttle_packet(session[s].tbf_in, p, l);
969 return;
970 }
971
972 // Are we throttled and a master??
973 if (session[s].tbf_in && config->cluster_iam_master) {
974 // Actually handle the throttled packets.
975 tbf_queue_packet(session[s].tbf_in, p, l);
976 return;
977 }
978
979 // send to ethernet
980 if (tun_write(p, l) < 0)
981 {
982 STAT(tun_tx_errors);
983 LOG(0, s, t, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
984 l, strerror(errno), tunfd, p);
985
986 return;
987 }
988
989 if (session[s].snoop_ip && session[s].snoop_port)
990 {
991 // Snooping this session
992 snoop_send_packet(p + 4, l - 4, session[s].snoop_ip, session[s].snoop_port);
993 }
994
995 session[s].cin += l - 4;
996 session[s].total_cin += l - 4;
997 sess_local[s].cin += l - 4;
998
999 session[s].pin++;
1000 eth_tx += l - 4;
1001
1002 STAT(tun_tx_packets);
1003 INC_STAT(tun_tx_bytes, l - 4);
1004 }
1005
1006 // process IPv6 packet received
1007 //
1008 // This MUST be called with at least 4 byte behind 'p'.
1009 // (i.e. this routine writes to p[-4]).
1010 void processipv6in(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
1011 {
1012 struct in6_addr ip;
1013 in_addr_t ipv4;
1014
1015 CSTAT(processipv6in);
1016
1017 LOG_HEX(5, "IPv6", p, l);
1018
1019 ip = *(struct in6_addr *) (p + 8);
1020 ipv4 = ntohl(*(uint32_t *)(p + 16));
1021
1022 if (l > MAXETHER)
1023 {
1024 LOG(1, s, t, "IP packet too long %d\n", l);
1025 STAT(tunnel_rx_errors);
1026 return ;
1027 }
1028
1029 // no spoof
1030 if (ipv4 != session[s].ip && memcmp(&config->ipv6_prefix, &ip, 8) && sessionbyipv6(ip) != s)
1031 {
1032 char str[INET6_ADDRSTRLEN];
1033 LOG(5, s, t, "Dropping packet with spoofed IP %s\n",
1034 inet_ntop(AF_INET6, &ip, str, INET6_ADDRSTRLEN));
1035 return;
1036 }
1037
1038 // Check if it's a Router Solicition message.
1039 if (*(p + 6) == 58 && *(p + 7) == 255 && *(p + 24) == 0xFF && *(p + 25) == 2 &&
1040 *(uint32_t *)(p + 26) == 0 && *(uint32_t *)(p + 30) == 0 &&
1041 *(uint32_t *)(p + 34) == 0 &&
1042 *(p + 38) == 0 && *(p + 39) == 2 && *(p + 40) == 133) {
1043 LOG(3, s, t, "Got IPv6 RS\n");
1044 send_ipv6_ra(t, s, &ip);
1045 return;
1046 }
1047
1048 // Add on the tun header
1049 p -= 4;
1050 *(uint32_t *) p = htonl(PKTIPV6);
1051 l += 4;
1052
1053 // Are we throttled and a slave?
1054 if (session[s].tbf_in && !config->cluster_iam_master) {
1055 // Pass it to the master for handling.
1056 master_throttle_packet(session[s].tbf_in, p, l);
1057 return;
1058 }
1059
1060 // Are we throttled and a master??
1061 if (session[s].tbf_in && config->cluster_iam_master) {
1062 // Actually handle the throttled packets.
1063 tbf_queue_packet(session[s].tbf_in, p, l);
1064 return;
1065 }
1066
1067 // send to ethernet
1068 if (tun_write(p, l) < 0)
1069 {
1070 STAT(tun_tx_errors);
1071 LOG(0, s, t, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1072 l, strerror(errno), tunfd, p);
1073
1074 return;
1075 }
1076
1077 if (session[s].snoop_ip && session[s].snoop_port)
1078 {
1079 // Snooping this session
1080 snoop_send_packet(p + 4, l - 4, session[s].snoop_ip, session[s].snoop_port);
1081 }
1082
1083 session[s].cin += l - 4;
1084 session[s].total_cin += l - 4;
1085 sess_local[s].cin += l - 4;
1086
1087 session[s].pin++;
1088 eth_tx += l - 4;
1089
1090 STAT(tun_tx_packets);
1091 INC_STAT(tun_tx_bytes, l - 4);
1092 }
1093
1094 //
1095 // Helper routine for the TBF filters.
1096 // Used to send queued data in from the user.
1097 //
1098 void send_ipin(sessionidt s, uint8_t *buf, int len)
1099 {
1100 LOG_HEX(5, "IP in throttled", buf, len);
1101
1102 if (write(tunfd, buf, len) < 0)
1103 {
1104 STAT(tun_tx_errors);
1105 LOG(0, 0, 0, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1106 len, strerror(errno), tunfd, buf);
1107
1108 return;
1109 }
1110
1111 if (session[s].snoop_ip && session[s].snoop_port)
1112 {
1113 // Snooping this session
1114 snoop_send_packet(buf + 4, len - 4, session[s].snoop_ip, session[s].snoop_port);
1115 }
1116
1117 // Increment packet counters
1118 session[s].cin += len - 4;
1119 session[s].total_cin += len - 4;
1120 sess_local[s].cin += len - 4;
1121
1122 session[s].pin++;
1123 eth_tx += len - 4;
1124
1125 STAT(tun_tx_packets);
1126 INC_STAT(tun_tx_bytes, len - 4);
1127 }
1128
1129
1130 // Process CCP messages
1131 void processccp(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
1132 {
1133 uint8_t b[MAXCONTROL];
1134 uint8_t *q;
1135
1136 CSTAT(processccp);
1137
1138 LOG_HEX(5, "CCP", p, l);
1139 switch (l > 1 ? *p : 0)
1140 {
1141 case ConfigAck:
1142 session[s].flags |= SF_CCP_ACKED;
1143 return;
1144
1145 case ConfigReq:
1146 if (l < 6) // accept no compression
1147 {
1148 *p = ConfigAck;
1149 break;
1150 }
1151
1152 // compression requested--reject
1153 *p = ConfigRej;
1154
1155 // send CCP request for no compression for our end if not negotiated
1156 if (!(session[s].flags & SF_CCP_ACKED))
1157 initccp(t, s);
1158
1159 break;
1160
1161 case TerminateReq:
1162 *p = TerminateAck;
1163 break;
1164
1165 default:
1166 if (l > 1)
1167 LOG(1, s, t, "Unexpected CCP request code %d\n", *p);
1168 else
1169 LOG(1, s, t, "Short CCP packet\n");
1170
1171 STAT(tunnel_rx_errors);
1172 return;
1173 }
1174
1175 if (!(q = makeppp(b, sizeof(b), p, l, t, s, PPPCCP)))
1176 return;
1177
1178 tunnelsend(b, l + (q - b), t); // send it
1179 }
1180
1181 // send a CHAP challenge
1182 void sendchap(tunnelidt t, sessionidt s)
1183 {
1184 uint8_t b[MAXCONTROL];
1185 uint16_t r = sess_local[s].radius;
1186 uint8_t *q;
1187
1188 CSTAT(sendchap);
1189
1190 if (!r)
1191 {
1192 LOG(1, s, t, "No RADIUS to send challenge\n");
1193 STAT(tunnel_tx_errors);
1194 return;
1195 }
1196
1197 LOG(1, s, t, "Send CHAP challenge\n");
1198
1199 radius[r].chap = 1; // CHAP not PAP
1200 radius[r].id++;
1201 if (radius[r].state != RADIUSCHAP)
1202 radius[r].try = 0;
1203
1204 radius[r].state = RADIUSCHAP;
1205 radius[r].retry = backoff(radius[r].try++);
1206 if (radius[r].try > 5)
1207 {
1208 sessionshutdown(s, "CHAP timeout.", 3, 0);
1209 STAT(tunnel_tx_errors);
1210 return ;
1211 }
1212 q = makeppp(b, sizeof(b), 0, 0, t, s, PPPCHAP);
1213 if (!q) return;
1214
1215 *q = 1; // challenge
1216 q[1] = radius[r].id; // ID
1217 q[4] = 16; // value size (size of challenge)
1218 memcpy(q + 5, radius[r].auth, 16); // challenge
1219 strcpy(q + 21, hostname); // our name
1220 *(uint16_t *) (q + 2) = htons(strlen(hostname) + 21); // length
1221 tunnelsend(b, strlen(hostname) + 21 + (q - b), t); // send it
1222 }
1223
1224 // fill in a L2TP message with a PPP frame,
1225 // copies existing PPP message and changes magic number if seen
1226 // returns start of PPP frame
1227 uint8_t *makeppp(uint8_t *b, int size, uint8_t *p, int l, tunnelidt t, sessionidt s, uint16_t mtype)
1228 {
1229 if (size < 12) // Need more space than this!!
1230 {
1231 static int backtrace_count = 0;
1232 LOG(0, s, t, "makeppp buffer too small for L2TP header (size=%d)\n", size);
1233 log_backtrace(backtrace_count, 5)
1234 return NULL;
1235 }
1236
1237 *(uint16_t *) (b + 0) = htons(0x0002); // L2TP with no options
1238 *(uint16_t *) (b + 2) = htons(tunnel[t].far); // tunnel
1239 *(uint16_t *) (b + 4) = htons(session[s].far); // session
1240 b += 6;
1241 if (mtype == PPPLCP || !(session[s].l2tp_flags & SESSIONACFC))
1242 {
1243 *(uint16_t *) b = htons(0xFF03); // HDLC header
1244 b += 2;
1245 }
1246 if (mtype < 0x100 && session[s].l2tp_flags & SESSIONPFC)
1247 *b++ = mtype;
1248 else
1249 {
1250 *(uint16_t *) b = htons(mtype);
1251 b += 2;
1252 }
1253
1254 if (l + 12 > size)
1255 {
1256 static int backtrace_count = 0;
1257 LOG(2, s, t, "makeppp would overflow buffer (size=%d, header+payload=%d)\n", size, l + 12);
1258 log_backtrace(backtrace_count, 5)
1259 return NULL;
1260 }
1261
1262 if (p && l)
1263 memcpy(b, p, l);
1264
1265 return b;
1266 }
1267
1268 static uint8_t *add_lcp_auth(uint8_t *b, int size, int authtype)
1269 {
1270 if ((authtype == AUTHCHAP && size < 5) || size < 4)
1271 return 0;
1272
1273 *b++ = 3; // Authentication-Protocol
1274 if (authtype == AUTHCHAP)
1275 {
1276 *b++ = 5; // length
1277 *(uint16_t *) b = htons(PPPCHAP); b += 2;
1278 *b++ = 5; // MD5
1279 }
1280 else if (authtype == AUTHPAP)
1281 {
1282 *b++ = 4; // length
1283 *(uint16_t *) b = htons(PPPPAP); b += 2;
1284 }
1285 else
1286 {
1287 LOG(0, 0, 0, "add_lcp_auth called with unsupported auth type %d\n", authtype);
1288 }
1289
1290 return b;
1291 }
1292
1293 // Send initial LCP ConfigReq for MRU, authentication type and magic no
1294 void sendlcp(tunnelidt t, sessionidt s, int authtype)
1295 {
1296 char b[500], *q, *l;
1297
1298 if (!(q = makeppp(b, sizeof(b), NULL, 0, t, s, PPPLCP)))
1299 return;
1300
1301 LOG(4, s, t, "Sending LCP ConfigReq for %s\n",
1302 config->radius_authprefer == AUTHCHAP ? "CHAP" : "PAP");
1303
1304 if (!session[s].mru)
1305 session[s].mru = DEFAULT_MRU;
1306
1307 l = q;
1308 *l++ = ConfigReq;
1309 *l++ = (time_now % 255) + 1; // ID
1310
1311 l += 2; //Save space for length
1312
1313 *l++ = 1; *l++ = 4; // Maximum-Receive-Unit (length 4)
1314 *(uint16_t *) l = htons(session[s].mru); l += 2;
1315
1316 l = add_lcp_auth(l, sizeof(b) - (l - b), authtype);
1317
1318 *l++ = 5; *l++ = 6; // Magic-Number (length 6)
1319 *(uint32_t *) l = htonl(session[s].magic);
1320 l += 4;
1321
1322 *(uint16_t *)(q + 2) = htons(l - q); // Length
1323
1324 LOG_HEX(5, "PPPLCP", q, l - q);
1325 tunnelsend(b, (l - b), t);
1326 }
1327
1328 // Send CCP request for no compression
1329 static void initccp(tunnelidt t, sessionidt s)
1330 {
1331 char b[500], *q;
1332
1333 if (!(q = makeppp(b, sizeof(b), NULL, 0, t, s, PPPCCP)))
1334 return;
1335
1336 LOG(4, s, t, "Sending CCP ConfigReq for no compression\n");
1337 *q = ConfigReq;
1338 *(uint8_t *)(q + 1) = (time_now % 255) + 1; // ID
1339 *(uint16_t *)(q + 2) = htons(4); // Length
1340
1341 LOG_HEX(5, "PPPCCP", q, 4);
1342 tunnelsend(b, (q - b) + 4 , t);
1343 }