ensure MRU is sane
[l2tpns.git] / ppp.c
1 // L2TPNS PPP Stuff
2
3 char const *cvs_id_ppp = "$Id: ppp.c,v 1.86 2005/11/17 05:24:17 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 int add_lcp_auth(uint8_t *b, int size, int authtype);
27
28 // Process PAP messages
29 void processpap(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
30 {
31 char user[MAXUSER];
32 char pass[MAXPASS];
33 uint16_t hl;
34 uint16_t r;
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 if (session[s].ppp.phase != Authenticate)
65 {
66 LOG(2, s, t, "PAP ignored in %s phase\n", ppp_phase(session[s].ppp.phase));
67 return;
68 }
69
70 {
71 uint8_t *b = p;
72 b += 4;
73 user[0] = pass[0] = 0;
74 if (*b && *b < sizeof(user))
75 {
76 memcpy(user, b + 1, *b);
77 user[*b] = 0;
78 b += 1 + *b;
79 if (*b && *b < sizeof(pass))
80 {
81 memcpy(pass, b + 1, *b);
82 pass[*b] = 0;
83 }
84 }
85 LOG(3, s, t, "PAP login %s/%s\n", user, pass);
86 }
87
88 if (session[s].ip || !(r = radiusnew(s)))
89 {
90 // respond now, either no RADIUS available or already authenticated
91 uint8_t b[MAXETHER];
92 uint8_t id = p[1];
93 uint8_t *p = makeppp(b, sizeof(b), 0, 0, s, t, PPPPAP);
94 if (!p) return;
95
96 if (session[s].ip)
97 *p = 2; // ACK
98 else
99 *p = 3; // cant authorise
100 p[1] = id;
101 *(uint16_t *) (p + 2) = htons(5); // length
102 p[4] = 0; // no message
103 tunnelsend(b, 5 + (p - b), t); // send it
104
105 if (session[s].ip)
106 {
107 LOG(3, s, t, "Already an IP allocated: %s (%d)\n",
108 fmtaddr(htonl(session[s].ip), 0), session[s].ip_pool_index);
109 }
110 else
111 {
112 LOG(1, s, t, "No RADIUS session available to authenticate session...\n");
113 sessionshutdown(s, "No free RADIUS sessions.", 4, 0);
114 }
115 }
116 else
117 {
118 // Run PRE_AUTH plugins
119 struct param_pre_auth packet = { &tunnel[t], &session[s], strdup(user), strdup(pass), PPPPAP, 1 };
120 run_plugins(PLUGIN_PRE_AUTH, &packet);
121 if (!packet.continue_auth)
122 {
123 LOG(3, s, t, "A plugin rejected PRE_AUTH\n");
124 if (packet.username) free(packet.username);
125 if (packet.password) free(packet.password);
126 return;
127 }
128
129 strncpy(session[s].user, packet.username, sizeof(session[s].user) - 1);
130 strncpy(radius[r].pass, packet.password, sizeof(radius[r].pass) - 1);
131
132 free(packet.username);
133 free(packet.password);
134
135 radius[r].id = p[1];
136 LOG(3, s, t, "Sending login for %s/%s to RADIUS\n", user, pass);
137 radiussend(r, RADIUSAUTH);
138 }
139 }
140
141 // Process CHAP messages
142 void processchap(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
143 {
144 uint16_t r;
145 uint16_t hl;
146
147 CSTAT(processchap);
148
149 LOG_HEX(5, "CHAP", p, l);
150
151 if (l < 4)
152 {
153 LOG(1, s, t, "Short CHAP %u bytes\n", l);
154 STAT(tunnel_rx_errors);
155 sessionshutdown(s, "Short CHAP packet.", 3, 0);
156 return;
157 }
158
159 if ((hl = ntohs(*(uint16_t *) (p + 2))) > l)
160 {
161 LOG(1, s, t, "Length mismatch CHAP %u/%u\n", hl, l);
162 STAT(tunnel_rx_errors);
163 sessionshutdown(s, "CHAP length mismatch.", 3, 0);
164 return;
165 }
166 l = hl;
167
168 if (*p != 2)
169 {
170 LOG(1, s, t, "Unexpected CHAP response code %d\n", *p);
171 STAT(tunnel_rx_errors);
172 sessionshutdown(s, "CHAP length mismatch.", 3, 0);
173 return;
174 }
175
176 r = sess_local[s].radius;
177 if (!r)
178 {
179 LOG(3, s, t, "Unexpected CHAP message\n");
180 return;
181 }
182
183 if (session[s].ppp.phase != Authenticate)
184 {
185 LOG(2, s, t, "CHAP ignored in %s phase\n", ppp_phase(session[s].ppp.phase));
186 return;
187 }
188
189 if (p[1] != radius[r].id)
190 {
191 LOG(1, s, t, "Wrong CHAP response ID %d (should be %d) (%d)\n", p[1], radius[r].id, r);
192 STAT(tunnel_rx_errors);
193 sessionshutdown(s, "Unexpected CHAP response ID.", 3, 0);
194 return;
195 }
196
197 if (l < 5 || p[4] != 16)
198 {
199 LOG(1, s, t, "Bad CHAP response length %d\n", l < 5 ? -1 : p[4]);
200 STAT(tunnel_rx_errors);
201 sessionshutdown(s, "Bad CHAP response length.", 3, 0);
202 return;
203 }
204
205 l -= 5;
206 p += 5;
207 if (l < 16 || l - 16 >= sizeof(session[s].user))
208 {
209 LOG(1, s, t, "CHAP user too long %d\n", l - 16);
210 STAT(tunnel_rx_errors);
211 sessionshutdown(s, "CHAP username too long.", 3, 0);
212 return;
213 }
214
215 // Run PRE_AUTH plugins
216 {
217 struct param_pre_auth packet = { &tunnel[t], &session[s], NULL, NULL, PPPCHAP, 1 };
218
219 packet.password = calloc(17, 1);
220 memcpy(packet.password, p, 16);
221
222 p += 16;
223 l -= 16;
224
225 packet.username = calloc(l + 1, 1);
226 memcpy(packet.username, p, l);
227
228 run_plugins(PLUGIN_PRE_AUTH, &packet);
229 if (!packet.continue_auth)
230 {
231 LOG(3, s, t, "A plugin rejected PRE_AUTH\n");
232 if (packet.username) free(packet.username);
233 if (packet.password) free(packet.password);
234 return;
235 }
236
237 strncpy(session[s].user, packet.username, sizeof(session[s].user) - 1);
238 memcpy(radius[r].pass, packet.password, 16);
239
240 free(packet.username);
241 free(packet.password);
242 }
243
244 radius[r].chap = 1;
245 LOG(3, s, t, "CHAP login %s\n", session[s].user);
246 radiussend(r, RADIUSAUTH);
247 }
248
249 static void dumplcp(uint8_t *p, int l)
250 {
251 int x = l - 4;
252 uint8_t *o = (p + 4);
253
254 LOG_HEX(5, "PPP LCP Packet", p, l);
255 LOG(4, 0, 0, "PPP LCP Packet type %d (%s len %d)\n", *p, ppp_code((int)*p), ntohs( ((uint16_t *) p)[1]) );
256 LOG(4, 0, 0, "Length: %d\n", l);
257 if (*p != ConfigReq && *p != ConfigRej && *p != ConfigAck)
258 return;
259
260 while (x > 2)
261 {
262 int type = o[0];
263 int length = o[1];
264 if (length < 2)
265 {
266 LOG(4, 0, 0, " Option length is %d...\n", length);
267 break;
268 }
269 if (type == 0)
270 {
271 LOG(4, 0, 0, " Option type is 0...\n");
272 x -= length;
273 o += length;
274 continue;
275 }
276 switch (type)
277 {
278 case 1: // Maximum-Receive-Unit
279 if (length == 4)
280 LOG(4, 0, 0, " %s %d\n", ppp_lcp_option(type), ntohs(*(uint16_t *)(o + 2)));
281 else
282 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type), length);
283 break;
284 case 2: // Async-Control-Character-Map
285 if (length == 6)
286 {
287 uint32_t asyncmap = ntohl(*(uint32_t *)(o + 2));
288 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type), asyncmap);
289 }
290 else
291 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type), length);
292 break;
293 case 3: // Authentication-Protocol
294 if (length == 4)
295 {
296 int proto = ntohs(*(uint16_t *)(o + 2));
297 LOG(4, 0, 0, " %s 0x%x (%s)\n", ppp_lcp_option(type), proto,
298 proto == PPPPAP ? "PAP" : "UNSUPPORTED");
299 }
300 else if (length == 5)
301 {
302 int proto = ntohs(*(uint16_t *)(o + 2));
303 int algo = *(o + 4);
304 LOG(4, 0, 0, " %s 0x%x 0x%x (%s)\n", ppp_lcp_option(type), proto, algo,
305 (proto == PPPCHAP && algo == 5) ? "CHAP MD5" : "UNSUPPORTED");
306 }
307 else
308 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type), length);
309 break;
310 case 4: // Quality-Protocol
311 {
312 uint32_t qp = ntohl(*(uint32_t *)(o + 2));
313 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type), qp);
314 }
315 break;
316 case 5: // Magic-Number
317 if (length == 6)
318 {
319 uint32_t magicno = ntohl(*(uint32_t *)(o + 2));
320 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type), magicno);
321 }
322 else
323 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type), length);
324 break;
325 case 7: // Protocol-Field-Compression
326 case 8: // Address-And-Control-Field-Compression
327 LOG(4, 0, 0, " %s\n", ppp_lcp_option(type));
328 break;
329 default:
330 LOG(2, 0, 0, " Unknown PPP LCP Option type %d\n", type);
331 break;
332 }
333 x -= length;
334 o += length;
335 }
336 }
337
338 void lcp_open(sessionidt s, tunnelidt t)
339 {
340 // transition to Authentication or Network phase:
341 session[s].ppp.phase = sess_local[s].lcp_authtype ? Authenticate : Network;
342
343 LOG(3, s, t, "LCP: Opened, phase %s\n", ppp_phase(session[s].ppp.phase));
344
345 // LCP now Opened
346 change_state(s, lcp, Opened);
347
348 if (session[s].ppp.phase == Authenticate)
349 {
350 if (sess_local[s].lcp_authtype == AUTHCHAP)
351 sendchap(s, t);
352 }
353 else
354 {
355 // This-Layer-Up
356 sendipcp(s, t);
357 change_state(s, ipcp, RequestSent);
358 // move to passive state for IPv6 (if configured), CCP
359 if (config->ipv6_prefix.s6_addr[0])
360 change_state(s, ipv6cp, Stopped);
361 else
362 change_state(s, ipv6cp, Closed);
363
364 change_state(s, ccp, Stopped);
365 }
366 }
367
368 static void lcp_restart(sessionidt s)
369 {
370 session[s].ppp.phase = Establish;
371 // This-Layer-Down
372 change_state(s, ipcp, Dead);
373 change_state(s, ipv6cp, Dead);
374 change_state(s, ccp, Dead);
375 }
376
377 static uint8_t *ppp_conf_rej(sessionidt s, uint8_t *buf, size_t blen, uint16_t mtype,
378 uint8_t **response, uint8_t *queued, uint8_t *packet, uint8_t *option)
379 {
380 if (!*response || **response != ConfigRej)
381 {
382 queued = *response = makeppp(buf, blen, packet, 2, s, session[s].tunnel, mtype);
383 if (!queued)
384 return 0;
385
386 *queued = ConfigRej;
387 queued += 4;
388 }
389
390 if ((queued - buf + option[1]) > blen)
391 {
392 LOG(2, s, session[s].tunnel, "PPP overflow for ConfigRej (proto %u, option %u).\n", mtype, *option);
393 return 0;
394 }
395
396 memcpy(queued, option, option[1]);
397 return queued + option[1];
398 }
399
400 static uint8_t *ppp_conf_nak(sessionidt s, uint8_t *buf, size_t blen, uint16_t mtype,
401 uint8_t **response, uint8_t *queued, uint8_t *packet, uint8_t *option,
402 uint8_t *value, size_t vlen)
403 {
404 int *nak_sent;
405 switch (mtype)
406 {
407 case PPPLCP: nak_sent = &sess_local[s].lcp.nak_sent; break;
408 case PPPIPCP: nak_sent = &sess_local[s].ipcp.nak_sent; break;
409 case PPPIPV6CP: nak_sent = &sess_local[s].ipv6cp.nak_sent; break;
410 default: return 0; // ?
411 }
412
413 if (*response && **response != ConfigNak)
414 {
415 if (*nak_sent < config->ppp_max_failure) // reject queued
416 return queued;
417
418 return ppp_conf_rej(s, buf, blen, mtype, response, 0, packet, option);
419 }
420
421 if (!*response)
422 {
423 if (*nak_sent >= config->ppp_max_failure)
424 return ppp_conf_rej(s, buf, blen, mtype, response, 0, packet, option);
425
426 queued = *response = makeppp(buf, blen, packet, 2, s, session[s].tunnel, mtype);
427 if (!queued)
428 return 0;
429
430 (*nak_sent)++;
431 *queued = ConfigNak;
432 queued += 4;
433 }
434
435 if ((queued - buf + vlen + 2) > blen)
436 {
437 LOG(2, s, session[s].tunnel, "PPP overflow for ConfigNak (proto %u, option %u).\n", mtype, *option);
438 return 0;
439 }
440
441 *queued++ = *option;
442 *queued++ = vlen + 2;
443 memcpy(queued, value, vlen);
444 return queued + vlen;
445 }
446
447 static void ppp_code_rej(sessionidt s, tunnelidt t, uint16_t proto,
448 char *pname, uint8_t *p, uint16_t l, uint8_t *buf, size_t size)
449 {
450 uint8_t *q;
451 int mru = session[s].mru;
452 if (mru > size) mru = size;
453
454 l += 4;
455 if (l > mru) l = mru;
456
457 q = makeppp(buf, size, 0, 0, s, t, proto);
458 if (!q) return;
459
460 *q = CodeRej;
461 *(q + 1) = ++sess_local[s].lcp_ident;
462 *(uint16_t *)(q + 2) = htons(l);
463 memcpy(q + 4, p, l - 4);
464
465 LOG(2, s, t, "Unexpected %s code %s\n", pname, ppp_code(*p));
466 LOG(3, s, t, "%s: send %s\n", pname, ppp_code(*q));
467 if (config->debug > 3) dumplcp(q, l);
468
469 tunnelsend(buf, l + (q - buf), t);
470 }
471
472 // Process LCP messages
473 void processlcp(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
474 {
475 uint8_t b[MAXETHER];
476 uint8_t *q = NULL;
477 uint32_t magicno = 0;
478 uint16_t hl;
479
480 CSTAT(processlcp);
481
482 LOG_HEX(5, "LCP", p, l);
483 if (l < 4)
484 {
485 LOG(1, s, t, "Short LCP %d bytes\n", l);
486 STAT(tunnel_rx_errors);
487 return ;
488 }
489
490 if ((hl = ntohs(*(uint16_t *) (p + 2))) > l)
491 {
492 LOG(1, s, t, "Length mismatch LCP %u/%u\n", hl, l);
493 STAT(tunnel_rx_errors);
494 return ;
495 }
496 l = hl;
497
498 if (session[s].die) // going down...
499 return;
500
501 LOG(*p == EchoReq ? 4 : 3, s, t, "LCP: recv %s\n", ppp_code(*p));
502 if (config->debug > 3) dumplcp(p, l);
503
504 if (*p == ConfigAck)
505 {
506 int x = l - 4;
507 uint8_t *o = (p + 4);
508 int authtype = 0;
509
510 while (x > 2)
511 {
512 int type = o[0];
513 int length = o[1];
514
515 if (length == 0 || type == 0 || x < length) break;
516 switch (type)
517 {
518 case 3: // Authentication-Protocol
519 {
520 int proto = ntohs(*(uint16_t *)(o + 2));
521 if (proto == PPPPAP)
522 authtype = AUTHPAP;
523 else if (proto == PPPCHAP && *(o + 4) == 5)
524 authtype = AUTHCHAP;
525 }
526
527 break;
528 }
529 x -= length;
530 o += length;
531 }
532
533 if (!session[s].ip && authtype)
534 sess_local[s].lcp_authtype = authtype;
535
536 switch (session[s].ppp.lcp)
537 {
538 case RequestSent:
539 initialise_restart_count(s, lcp);
540 change_state(s, lcp, AckReceived);
541 break;
542
543 case AckReceived:
544 case Opened:
545 LOG(2, s, t, "LCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session[s].ppp.lcp));
546 if (session[s].ppp.lcp == Opened)
547 lcp_restart(s);
548
549 sendlcp(s, t);
550 change_state(s, lcp, RequestSent);
551 break;
552
553 case AckSent:
554 lcp_open(s, t);
555 break;
556
557 default:
558 LOG(2, s, t, "LCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.lcp));
559 }
560 }
561 else if (*p == ConfigReq)
562 {
563 int x = l - 4;
564 uint8_t *o = (p + 4);
565 uint8_t *response = 0;
566 static uint8_t asyncmap[4] = { 0, 0, 0, 0 }; // all zero
567 static uint8_t authproto[5];
568
569 while (x > 2)
570 {
571 int type = o[0];
572 int length = o[1];
573
574 if (length == 0 || type == 0 || x < length) break;
575 switch (type)
576 {
577 case 1: // Maximum-Receive-Unit
578 {
579 uint16_t mru = ntohs(*(uint16_t *)(o + 2));
580 if (mru >= 576)
581 {
582 session[s].mru = mru;
583 break;
584 }
585
586 LOG(3, s, t, " Remote requesting MRU of %u. Rejecting.\n", mru);
587 mru = htons(MRU);
588 q = ppp_conf_nak(s, b, sizeof(b), PPPLCP, &response, q, p, o, (uint8_t *) &mru, sizeof(mru));
589 }
590 break;
591
592 case 2: // Async-Control-Character-Map
593 if (!ntohl(*(uint32_t *)(o + 2))) // all bits zero is OK
594 break;
595
596 LOG(3, s, t, " Remote requesting asyncmap. Rejecting.\n");
597 q = ppp_conf_nak(s, b, sizeof(b), PPPLCP, &response, q, p, o, asyncmap, sizeof(asyncmap));
598 break;
599
600 case 3: // Authentication-Protocol
601 {
602 int proto = ntohs(*(uint16_t *)(o + 2));
603 char proto_name[] = "0x0000";
604 int alen;
605
606 if (proto == PPPPAP)
607 {
608 if (config->radius_authtypes & AUTHPAP)
609 {
610 sess_local[s].lcp_authtype = AUTHPAP;
611 break;
612 }
613
614 strcpy(proto_name, "PAP");
615 }
616 else if (proto == PPPCHAP)
617 {
618 if (config->radius_authtypes & AUTHCHAP
619 && *(o + 4) == 5) // MD5
620 {
621 sess_local[s].lcp_authtype = AUTHCHAP;
622 break;
623 }
624
625 strcpy(proto_name, "CHAP");
626 }
627 else
628 sprintf(proto_name, "%#4.4x", proto);
629
630 LOG(3, s, t, " Remote requesting %s authentication. Rejecting.\n", proto_name);
631
632 alen = add_lcp_auth(authproto, sizeof(authproto), config->radius_authprefer);
633 if (alen < 2) break; // paranoia
634
635 q = ppp_conf_nak(s, b, sizeof(b), PPPLCP, &response, q, p, o, authproto + 2, alen - 2);
636 if (q && *response == ConfigNak &&
637 config->radius_authtypes != config->radius_authprefer)
638 {
639 // alternate type
640 alen = add_lcp_auth(authproto, sizeof(authproto), config->radius_authtypes & ~config->radius_authprefer);
641 if (alen < 2) break;
642 q = ppp_conf_nak(s, b, sizeof(b), PPPLCP, &response, q, p, o, authproto + 2, alen - 2);
643 }
644
645 break;
646 }
647 break;
648
649 case 5: // Magic-Number
650 magicno = ntohl(*(uint32_t *)(o + 2));
651 break;
652
653 case 4: // Quality-Protocol
654 case 7: // Protocol-Field-Compression
655 case 8: // Address-And-Control-Field-Compression
656 break;
657
658 default: // Reject any unknown options
659 LOG(3, s, t, " Rejecting unknown PPP LCP option %d\n", type);
660 q = ppp_conf_rej(s, b, sizeof(b), PPPLCP, &response, q, p, o);
661 }
662 x -= length;
663 o += length;
664 }
665
666 if (response)
667 {
668 l = q - response; // LCP packet length
669 *((uint16_t *) (response + 2)) = htons(l); // update header
670 }
671 else
672 {
673 // Send packet back as ConfigAck
674 response = makeppp(b, sizeof(b), p, l, s, t, PPPLCP);
675 if (!response) return;
676 *response = ConfigAck;
677 }
678
679 switch (session[s].ppp.lcp)
680 {
681 case Closed:
682 response = makeppp(b, sizeof(b), p, 2, s, t, PPPLCP);
683 if (!response) return;
684 *response = TerminateAck;
685 *((uint16_t *) (response + 2)) = htons(l = 4);
686 break;
687
688 case Stopped:
689 initialise_restart_count(s, lcp);
690 sendlcp(s, t);
691 if (*response == ConfigAck)
692 change_state(s, lcp, AckSent);
693 else
694 change_state(s, lcp, RequestSent);
695
696 break;
697
698 case RequestSent:
699 if (*response == ConfigAck)
700 change_state(s, lcp, AckSent);
701
702 break;
703
704 case AckReceived:
705 if (*response == ConfigAck)
706 lcp_open(s, t);
707
708 break;
709
710 case Opened:
711 lcp_restart(s);
712 sendlcp(s, t);
713 /* fallthrough */
714
715 case AckSent:
716 if (*response == ConfigAck)
717 change_state(s, lcp, AckSent);
718 else
719 change_state(s, lcp, RequestSent);
720
721 break;
722
723 default:
724 LOG(2, s, t, "LCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.lcp));
725 return;
726 }
727
728 LOG(3, s, t, "LCP: send %s\n", ppp_code(*response));
729 if (config->debug > 3) dumplcp(response, l);
730
731 tunnelsend(b, l + (response - b), t);
732 }
733 else if (*p == ConfigNak || *p == ConfigRej)
734 {
735 int x = l - 4;
736 uint8_t *o = (p + 4);
737 int authtype = -1;
738
739 while (x > 2)
740 {
741 int type = o[0];
742 int length = o[1];
743
744 if (length == 0 || type == 0 || x < length) break;
745 switch (type)
746 {
747 case 1: // Maximum-Receive-Unit
748 if (*p == ConfigNak)
749 {
750 sess_local[s].ppp_mru = ntohs(*(uint16_t *)(o + 2));
751 LOG(3, s, t, " Remote requested MRU of %u\n", sess_local[s].ppp_mru);
752 }
753 else
754 {
755 sess_local[s].ppp_mru = 0;
756 LOG(3, s, t, " Remote rejected MRU negotiation\n");
757 }
758
759 break;
760
761 case 3: // Authentication-Protocol
762 if (authtype > 0)
763 break;
764
765 if (*p == ConfigNak)
766 {
767 int proto = ntohs(*(uint16_t *)(o + 2));
768 if (proto == PPPPAP)
769 {
770 authtype = config->radius_authtypes & AUTHPAP;
771 LOG(3, s, t, " Remote requested PAP authentication...%sing\n",
772 authtype ? "accept" : "reject");
773 }
774 else if (proto == PPPCHAP && *(o + 4) == 5)
775 {
776 authtype = config->radius_authtypes & AUTHCHAP;
777 LOG(3, s, t, " Remote requested CHAP authentication...%sing\n",
778 authtype ? "accept" : "reject");
779 }
780 else
781 {
782 LOG(3, s, t, " Rejecting unsupported authentication %#4x\n",
783 proto);
784 }
785 }
786 else
787 {
788 LOG(2, s, t, "LCP: remote rejected auth negotiation\n");
789 authtype = 0; // shutdown
790 }
791
792 break;
793
794 default:
795 LOG(2, s, t, "LCP: remote sent %s for type %u?\n", ppp_code(*p), type);
796 break;
797 }
798 x -= length;
799 o += length;
800 }
801
802 if (!authtype)
803 {
804 sessionshutdown(s, "Unsupported authentication.", 3, 0);
805 return;
806 }
807
808 if (authtype > 0)
809 sess_local[s].lcp_authtype = authtype;
810
811 switch (session[s].ppp.lcp)
812 {
813 case Closed:
814 case Stopped:
815 {
816 uint8_t *response = makeppp(b, sizeof(b), p, 2, s, t, PPPLCP);
817 if (!response) return;
818 *response = TerminateAck;
819 *((uint16_t *) (response + 2)) = htons(l = 4);
820
821 LOG(3, s, t, "LCP: send %s\n", ppp_code(*response));
822 if (config->debug > 3) dumplcp(response, l);
823
824 tunnelsend(b, l + (response - b), t);
825 }
826 break;
827
828 case RequestSent:
829 case AckSent:
830 initialise_restart_count(s, lcp);
831 sendlcp(s, t);
832 break;
833
834 case AckReceived:
835 LOG(2, s, t, "LCP: ConfigNak in state %s? Sending ConfigReq\n", ppp_state(session[s].ppp.lcp));
836 sendlcp(s, t);
837 break;
838
839 case Opened:
840 lcp_restart(s);
841 sendlcp(s, t);
842 break;
843
844 default:
845 LOG(2, s, t, "LCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.lcp));
846 return;
847 }
848 }
849 else if (*p == TerminateReq)
850 {
851 *p = TerminateAck; // close
852 q = makeppp(b, sizeof(b), p, l, s, t, PPPLCP);
853 if (!q) return;
854
855 LOG(3, s, t, "LCP: send %s\n", ppp_code(*q));
856 if (config->debug > 3) dumplcp(q, l);
857
858 tunnelsend(b, l + (q - b), t); // send it
859 sessionshutdown(s, "Remote end closed connection.", 3, 0);
860 }
861 else if (*p == TerminateAck)
862 {
863 sessionshutdown(s, "Connection closed.", 3, 0);
864 }
865 else if (*p == ProtocolRej)
866 {
867 uint16_t proto = 0;
868
869 if (l > 4)
870 {
871 proto = *(p+4);
872 if (l > 5 && !(proto & 1))
873 {
874 proto <<= 8;
875 proto |= *(p+5);
876 }
877 }
878
879 if (proto == PPPIPV6CP)
880 {
881 LOG(3, s, t, "IPv6 rejected\n");
882 change_state(s, ipv6cp, Closed);
883 }
884 else
885 {
886 LOG(3, s, t, "LCP protocol reject: 0x%04X\n", proto);
887 }
888 }
889 else if (*p == EchoReq)
890 {
891 *p = EchoReply; // reply
892 *(uint32_t *) (p + 4) = htonl(session[s].magic); // our magic number
893 q = makeppp(b, sizeof(b), p, l, s, t, PPPLCP);
894 if (!q) return;
895
896 LOG(4, s, t, "LCP: send %s\n", ppp_code(*q));
897 if (config->debug > 3) dumplcp(q, l);
898
899 tunnelsend(b, l + (q - b), t); // send it
900 }
901 else if (*p == EchoReply)
902 {
903 // Ignore it, last_packet time is set earlier than this.
904 }
905 else if (*p != CodeRej)
906 {
907 ppp_code_rej(s, t, PPPLCP, "LCP", p, l, b, sizeof(b));
908 }
909 }
910
911 static void ipcp_open(sessionidt s, tunnelidt t)
912 {
913 LOG(3, s, t, "IPCP: Opened, session is now active\n");
914
915 change_state(s, ipcp, Opened);
916
917 if (!(session[s].walled_garden || session[s].flags & SESSION_STARTED))
918 {
919 uint16_t r = radiusnew(s);
920 if (r)
921 {
922 radiussend(r, RADIUSSTART); // send radius start
923
924 // don't send further Start records if IPCP is restarted
925 session[s].flags |= SESSION_STARTED;
926 cluster_send_session(s);
927 }
928 }
929
930 // start IPv6 if configured and still in passive state
931 if (session[s].ppp.ipv6cp == Stopped)
932 {
933 sendipv6cp(s, t);
934 change_state(s, ipv6cp, RequestSent);
935 }
936 }
937
938 // Process IPCP messages
939 void processipcp(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
940 {
941 uint8_t b[MAXETHER];
942 uint8_t *q = 0;
943 uint16_t hl;
944
945 CSTAT(processipcp);
946
947 LOG_HEX(5, "IPCP", p, l);
948 if (l < 5)
949 {
950 LOG(1, s, t, "Short IPCP %d bytes\n", l);
951 STAT(tunnel_rx_errors);
952 return ;
953 }
954
955 if ((hl = ntohs(*(uint16_t *) (p + 2))) > l)
956 {
957 LOG(1, s, t, "Length mismatch IPCP %u/%u\n", hl, l);
958 STAT(tunnel_rx_errors);
959 return ;
960 }
961 l = hl;
962
963 if (session[s].ppp.phase < Network)
964 {
965 LOG(2, s, t, "IPCP %s ignored in %s phase\n", ppp_code(*p), ppp_phase(session[s].ppp.phase));
966 return;
967 }
968
969 LOG(3, s, t, "IPCP: recv %s\n", ppp_code(*p));
970
971 if (*p == ConfigAck)
972 {
973 switch (session[s].ppp.ipcp)
974 {
975 case RequestSent:
976 initialise_restart_count(s, ipcp);
977 change_state(s, ipcp, AckReceived);
978 break;
979
980 case AckReceived:
981 case Opened:
982 LOG(2, s, t, "IPCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session[s].ppp.ipcp));
983 sendipcp(s, t);
984 change_state(s, ipcp, RequestSent);
985 break;
986
987 case AckSent:
988 ipcp_open(s, t);
989 break;
990
991 default:
992 LOG(2, s, t, "IPCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ipcp));
993 }
994 }
995 else if (*p == ConfigReq)
996 {
997 uint8_t *response = 0;
998 uint8_t *o = p + 4;
999 int length = l - 4;
1000 int gotip = 0;
1001 in_addr_t addr;
1002
1003 while (length > 2)
1004 {
1005 switch (*o)
1006 {
1007 case 3: // ip address
1008 gotip++; // seen address
1009 if (o[1] != 6 || o[1] > length) return;
1010
1011 addr = htonl(session[s].ip);
1012 if (memcmp(o + 2, &addr, (sizeof addr)))
1013 {
1014 uint8_t *oq = q;
1015 q = ppp_conf_nak(s, b, sizeof(b), PPPIPCP, &response, q, p, o, (uint8_t *) &addr, sizeof(addr));
1016 if (!q || (q != oq && *response == ConfigRej))
1017 {
1018 sessionshutdown(s, "Can't negotiate IPCP.", 3, 0);
1019 return;
1020 }
1021 }
1022
1023 break;
1024
1025 case 129: // primary DNS
1026 if (o[1] != 6 || o[1] > length) return;
1027
1028 addr = htonl(session[s].dns1);
1029 if (memcmp(o + 2, &addr, (sizeof addr)))
1030 {
1031 q = ppp_conf_nak(s, b, sizeof(b), PPPIPCP, &response, q, p, o, (uint8_t *) &addr, sizeof(addr));
1032 if (!q) return;
1033 }
1034
1035 break;
1036
1037 case 131: // secondary DNS
1038 if (o[1] != 6 || o[1] > length) return;
1039
1040 addr = htonl(session[s].dns1);
1041 if (memcmp(o + 2, &addr, sizeof(addr)))
1042 {
1043 q = ppp_conf_nak(s, b, sizeof(b), PPPIPCP, &response, q, p, o, (uint8_t *) &addr, sizeof(addr));
1044 if (!q) return;
1045 }
1046
1047 break;
1048
1049 default:
1050 LOG(2, s, t, " Rejecting PPP IPCP Option type %d\n", *o);
1051 q = ppp_conf_rej(s, b, sizeof(b), PPPIPCP, &response, q, p, o);
1052 if (!q) return;
1053 }
1054
1055 length -= o[1];
1056 o += o[1];
1057 }
1058
1059 if (response)
1060 {
1061 l = q - response; // IPCP packet length
1062 *((uint16_t *) (response + 2)) = htons(l); // update header
1063 }
1064 else if (gotip)
1065 {
1066 // Send packet back as ConfigAck
1067 response = makeppp(b, sizeof(b), p, l, s, t, PPPIPCP);
1068 if (!response) return;
1069 *response = ConfigAck;
1070 }
1071 else
1072 {
1073 LOG(1, s, t, "No IP in IPCP request\n");
1074 STAT(tunnel_rx_errors);
1075 return;
1076 }
1077
1078 switch (session[s].ppp.ipcp)
1079 {
1080 case Closed:
1081 response = makeppp(b, sizeof(b), p, 2, s, t, PPPIPCP);
1082 if (!response) return;
1083 *response = TerminateAck;
1084 *((uint16_t *) (response + 2)) = htons(l = 4);
1085 break;
1086
1087 case Stopped:
1088 initialise_restart_count(s, ipcp);
1089 sendipcp(s, t);
1090 if (*response == ConfigAck)
1091 change_state(s, ipcp, AckSent);
1092 else
1093 change_state(s, ipcp, RequestSent);
1094
1095 break;
1096
1097 case RequestSent:
1098 if (*response == ConfigAck)
1099 change_state(s, ipcp, AckSent);
1100
1101 break;
1102
1103 case AckReceived:
1104 if (*response == ConfigAck)
1105 ipcp_open(s, t);
1106
1107 break;
1108
1109 case Opened:
1110 initialise_restart_count(s, ipcp);
1111 sendipcp(s, t);
1112 /* fallthrough */
1113
1114 case AckSent:
1115 if (*response == ConfigAck)
1116 change_state(s, ipcp, AckSent);
1117 else
1118 change_state(s, ipcp, RequestSent);
1119
1120 break;
1121
1122 default:
1123 LOG(2, s, t, "IPCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ipcp));
1124 return;
1125 }
1126
1127 LOG(3, s, t, "IPCP: send %s\n", ppp_code(*response));
1128 tunnelsend(b, l + (response - b), t);
1129 }
1130 else if (*p == TerminateReq)
1131 {
1132 *p = TerminateAck;
1133 q = makeppp(b, sizeof(b), p, l, s, t, PPPIPCP);
1134 if (!q) return;
1135 LOG(3, s, t, "IPCP: send %s\n", ppp_code(*q));
1136 tunnelsend(b, l + (q - b), t);
1137 change_state(s, ipcp, Stopped);
1138 }
1139 else if (*p != CodeRej)
1140 {
1141 ppp_code_rej(s, t, PPPIPCP, "IPCP", p, l, b, sizeof(b));
1142 }
1143 }
1144
1145 static void ipv6cp_open(sessionidt s, tunnelidt t)
1146 {
1147 LOG(3, s, t, "IPV6CP: Opened\n");
1148
1149 change_state(s, ipv6cp, Opened);
1150 if (session[s].ipv6prefixlen)
1151 route6set(s, session[s].ipv6route, session[s].ipv6prefixlen, 1);
1152
1153 // Send an initial RA (TODO: Should we send these regularly?)
1154 send_ipv6_ra(s, t, NULL);
1155 }
1156
1157 // Process IPV6CP messages
1158 void processipv6cp(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
1159 {
1160 uint8_t b[MAXETHER];
1161 uint8_t *q = 0;
1162 uint16_t hl;
1163
1164 CSTAT(processipv6cp);
1165
1166 LOG_HEX(5, "IPV6CP", p, l);
1167 if (l < 4)
1168 {
1169 LOG(1, s, t, "Short IPV6CP %d bytes\n", l);
1170 STAT(tunnel_rx_errors);
1171 return ;
1172 }
1173
1174 if ((hl = ntohs(*(uint16_t *) (p + 2))) > l)
1175 {
1176 LOG(1, s, t, "Length mismatch IPV6CP %u/%u\n", hl, l);
1177 STAT(tunnel_rx_errors);
1178 return ;
1179 }
1180 l = hl;
1181
1182 if (session[s].ppp.phase < Network)
1183 {
1184 LOG(2, s, t, "IPV6CP %s ignored in %s phase\n", ppp_code(*p), ppp_phase(session[s].ppp.phase));
1185 return;
1186 }
1187
1188 LOG(3, s, t, "IPV6CP: recv %s\n", ppp_code(*p));
1189
1190 if (!session[s].ip)
1191 {
1192 LOG(3, s, t, "IPV6CP: no IPv4 address (IPCP in state %s)\n", ppp_state(session[s].ppp.ipcp));
1193 return; // need IPCP to complete...
1194 }
1195
1196 if (*p == ConfigAck)
1197 {
1198 switch (session[s].ppp.ipv6cp)
1199 {
1200 case RequestSent:
1201 initialise_restart_count(s, ipv6cp);
1202 change_state(s, ipv6cp, AckReceived);
1203 break;
1204
1205 case AckReceived:
1206 case Opened:
1207 LOG(2, s, t, "IPV6CP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session[s].ppp.ipv6cp));
1208 sendipv6cp(s, t);
1209 change_state(s, ipv6cp, RequestSent);
1210 break;
1211
1212 case AckSent:
1213 ipv6cp_open(s, t);
1214 break;
1215
1216 default:
1217 LOG(2, s, t, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ipv6cp));
1218 }
1219 }
1220 else if (*p == ConfigReq)
1221 {
1222 uint8_t *response = 0;
1223 uint8_t *o = p + 4;
1224 int length = l - 4;
1225 int gotip = 0;
1226 uint8_t ident[8];
1227
1228 while (length > 2)
1229 {
1230 switch (*o)
1231 {
1232 case 1: // interface identifier
1233 gotip++; // seen address
1234 if (o[1] != 10 || o[1] > length) return;
1235
1236 *(uint32_t *) ident = htonl(session[s].ip);
1237 *(uint32_t *) (ident + 4) = 0;
1238
1239 if (memcmp(o + 2, ident, sizeof(ident)))
1240 {
1241 q = ppp_conf_nak(s, b, sizeof(b), PPPIPV6CP, &response, q, p, o, ident, sizeof(ident));
1242 if (!q) return;
1243 }
1244
1245 break;
1246
1247 default:
1248 LOG(2, s, t, " Rejecting PPP IPV6CP Option type %d\n", *o);
1249 q = ppp_conf_rej(s, b, sizeof(b), PPPIPV6CP, &response, q, p, o);
1250 if (!q) return;
1251 }
1252
1253 length -= o[1];
1254 o += o[1];
1255 }
1256
1257 if (response)
1258 {
1259 l = q - response; // IPV6CP packet length
1260 *((uint16_t *) (response + 2)) = htons(l); // update header
1261 }
1262 else if (gotip)
1263 {
1264 // Send packet back as ConfigAck
1265 response = makeppp(b, sizeof(b), p, l, s, t, PPPIPV6CP);
1266 if (!response) return;
1267 *response = ConfigAck;
1268 }
1269 else
1270 {
1271 LOG(1, s, t, "No interface identifier in IPV6CP request\n");
1272 STAT(tunnel_rx_errors);
1273 return;
1274 }
1275
1276 switch (session[s].ppp.ipv6cp)
1277 {
1278 case Closed:
1279 response = makeppp(b, sizeof(b), p, 2, s, t, PPPIPV6CP);
1280 if (!response) return;
1281 *response = TerminateAck;
1282 *((uint16_t *) (response + 2)) = htons(l = 4);
1283 break;
1284
1285 case Stopped:
1286 initialise_restart_count(s, ipv6cp);
1287 sendipv6cp(s, t);
1288 if (*response == ConfigAck)
1289 change_state(s, ipv6cp, AckSent);
1290 else
1291 change_state(s, ipv6cp, RequestSent);
1292
1293 break;
1294
1295 case RequestSent:
1296 if (*response == ConfigAck)
1297 change_state(s, ipv6cp, AckSent);
1298
1299 break;
1300
1301 case AckReceived:
1302 if (*response == ConfigAck)
1303 ipv6cp_open(s, t);
1304
1305 break;
1306
1307 case Opened:
1308 initialise_restart_count(s, ipv6cp);
1309 sendipv6cp(s, t);
1310 /* fallthrough */
1311
1312 case AckSent:
1313 if (*response == ConfigAck)
1314 change_state(s, ipv6cp, AckSent);
1315 else
1316 change_state(s, ipv6cp, RequestSent);
1317
1318 break;
1319
1320 default:
1321 LOG(2, s, t, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ipv6cp));
1322 return;
1323 }
1324
1325 LOG(3, s, t, "IPV6CP: send %s\n", ppp_code(*response));
1326 tunnelsend(b, l + (response - b), t);
1327 }
1328 else if (*p == TerminateReq)
1329 {
1330 *p = TerminateAck;
1331 q = makeppp(b, sizeof(b), p, l, s, t, PPPIPV6CP);
1332 if (!q) return;
1333 LOG(3, s, t, "IPV6CP: send %s\n", ppp_code(*q));
1334 tunnelsend(b, l + (q - b), t);
1335 change_state(s, ipv6cp, Stopped);
1336 }
1337 else if (*p != CodeRej)
1338 {
1339 ppp_code_rej(s, t, PPPIPV6CP, "IPV6CP", p, l, b, sizeof(b));
1340 }
1341 }
1342
1343 // process IP packet received
1344 //
1345 // This MUST be called with at least 4 byte behind 'p'.
1346 // (i.e. this routine writes to p[-4]).
1347 void processipin(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
1348 {
1349 in_addr_t ip;
1350
1351 CSTAT(processipin);
1352
1353 LOG_HEX(5, "IP", p, l);
1354
1355 if (l < 20)
1356 {
1357 LOG(1, s, t, "IP packet too short %d\n", l);
1358 STAT(tunnel_rx_errors);
1359 return ;
1360 }
1361
1362 ip = ntohl(*(uint32_t *)(p + 12));
1363
1364 if (l > MAXETHER)
1365 {
1366 LOG(1, s, t, "IP packet too long %d\n", l);
1367 STAT(tunnel_rx_errors);
1368 return ;
1369 }
1370
1371 if (session[s].ppp.phase != Network || session[s].ppp.ipcp != Opened)
1372 return;
1373
1374 // no spoof (do sessionbyip to handled statically routed subnets)
1375 if (ip != session[s].ip && sessionbyip(htonl(ip)) != s)
1376 {
1377 LOG(5, s, t, "Dropping packet with spoofed IP %s\n", fmtaddr(htonl(ip), 0));
1378 return;
1379 }
1380
1381 // run access-list if any
1382 if (session[s].filter_in && !ip_filter(p, l, session[s].filter_in - 1))
1383 return;
1384
1385 // adjust MSS on SYN and SYN,ACK packets with options
1386 if ((ntohs(*(uint16_t *) (p + 6)) & 0x1fff) == 0 && p[9] == IPPROTO_TCP) // first tcp fragment
1387 {
1388 int ihl = (p[0] & 0xf) * 4; // length of IP header
1389 if (l >= ihl + 20 && (p[ihl + 13] & TCP_FLAG_SYN) && ((p[ihl + 12] >> 4) > 5))
1390 adjust_tcp_mss(s, t, p, l, p + ihl);
1391 }
1392
1393 // Add on the tun header
1394 p -= 4;
1395 *(uint32_t *) p = htonl(PKTIP);
1396 l += 4;
1397
1398 // Are we throttled and a slave?
1399 if (session[s].tbf_in && !config->cluster_iam_master) {
1400 // Pass it to the master for handling.
1401 master_throttle_packet(session[s].tbf_in, p, l);
1402 return;
1403 }
1404
1405 // Are we throttled and a master??
1406 if (session[s].tbf_in && config->cluster_iam_master) {
1407 // Actually handle the throttled packets.
1408 tbf_queue_packet(session[s].tbf_in, p, l);
1409 return;
1410 }
1411
1412 // send to ethernet
1413 if (tun_write(p, l) < 0)
1414 {
1415 STAT(tun_tx_errors);
1416 LOG(0, s, t, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1417 l, strerror(errno), tunfd, p);
1418
1419 return;
1420 }
1421
1422 p += 4;
1423 l -= 4;
1424
1425 if (session[s].snoop_ip && session[s].snoop_port)
1426 {
1427 // Snooping this session
1428 snoop_send_packet(p, l, session[s].snoop_ip, session[s].snoop_port);
1429 }
1430
1431 increment_counter(&session[s].cin, &session[s].cin_wrap, l);
1432 session[s].cin_delta += l;
1433 session[s].pin++;
1434
1435 sess_local[s].cin += l;
1436 sess_local[s].pin++;
1437
1438 eth_tx += l;
1439
1440 STAT(tun_tx_packets);
1441 INC_STAT(tun_tx_bytes, l);
1442 }
1443
1444 // process IPv6 packet received
1445 //
1446 // This MUST be called with at least 4 byte behind 'p'.
1447 // (i.e. this routine writes to p[-4]).
1448 void processipv6in(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
1449 {
1450 struct in6_addr ip;
1451 in_addr_t ipv4;
1452
1453 CSTAT(processipv6in);
1454
1455 LOG_HEX(5, "IPv6", p, l);
1456
1457 ip = *(struct in6_addr *) (p + 8);
1458 ipv4 = ntohl(*(uint32_t *)(p + 16));
1459
1460 if (l > MAXETHER)
1461 {
1462 LOG(1, s, t, "IP packet too long %d\n", l);
1463 STAT(tunnel_rx_errors);
1464 return ;
1465 }
1466
1467 if (session[s].ppp.phase != Network || session[s].ppp.ipv6cp != Opened)
1468 return;
1469
1470 // no spoof
1471 if (ipv4 != session[s].ip && memcmp(&config->ipv6_prefix, &ip, 8) && sessionbyipv6(ip) != s)
1472 {
1473 char str[INET6_ADDRSTRLEN];
1474 LOG(5, s, t, "Dropping packet with spoofed IP %s\n",
1475 inet_ntop(AF_INET6, &ip, str, INET6_ADDRSTRLEN));
1476 return;
1477 }
1478
1479 // Check if it's a Router Solicition message.
1480 if (*(p + 6) == 58 && *(p + 7) == 255 && *(p + 24) == 0xFF && *(p + 25) == 2 &&
1481 *(uint32_t *)(p + 26) == 0 && *(uint32_t *)(p + 30) == 0 &&
1482 *(uint32_t *)(p + 34) == 0 &&
1483 *(p + 38) == 0 && *(p + 39) == 2 && *(p + 40) == 133) {
1484 LOG(3, s, t, "Got IPv6 RS\n");
1485 send_ipv6_ra(s, t, &ip);
1486 return;
1487 }
1488
1489 // Add on the tun header
1490 p -= 4;
1491 *(uint32_t *) p = htonl(PKTIPV6);
1492 l += 4;
1493
1494 // Are we throttled and a slave?
1495 if (session[s].tbf_in && !config->cluster_iam_master) {
1496 // Pass it to the master for handling.
1497 master_throttle_packet(session[s].tbf_in, p, l);
1498 return;
1499 }
1500
1501 // Are we throttled and a master??
1502 if (session[s].tbf_in && config->cluster_iam_master) {
1503 // Actually handle the throttled packets.
1504 tbf_queue_packet(session[s].tbf_in, p, l);
1505 return;
1506 }
1507
1508 // send to ethernet
1509 if (tun_write(p, l) < 0)
1510 {
1511 STAT(tun_tx_errors);
1512 LOG(0, s, t, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1513 l, strerror(errno), tunfd, p);
1514
1515 return;
1516 }
1517
1518 p += 4;
1519 l -= 4;
1520
1521 if (session[s].snoop_ip && session[s].snoop_port)
1522 {
1523 // Snooping this session
1524 snoop_send_packet(p, l, session[s].snoop_ip, session[s].snoop_port);
1525 }
1526
1527 increment_counter(&session[s].cin, &session[s].cin_wrap, l);
1528 session[s].cin_delta += l;
1529 session[s].pin++;
1530
1531 sess_local[s].cin += l;
1532 sess_local[s].pin++;
1533
1534 eth_tx += l;
1535
1536 STAT(tun_tx_packets);
1537 INC_STAT(tun_tx_bytes, l);
1538 }
1539
1540 //
1541 // Helper routine for the TBF filters.
1542 // Used to send queued data in from the user.
1543 //
1544 void send_ipin(sessionidt s, uint8_t *buf, int len)
1545 {
1546 LOG_HEX(5, "IP in throttled", buf, len);
1547
1548 if (write(tunfd, buf, len) < 0)
1549 {
1550 STAT(tun_tx_errors);
1551 LOG(0, 0, 0, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1552 len, strerror(errno), tunfd, buf);
1553
1554 return;
1555 }
1556
1557 buf += 4;
1558 len -= 4;
1559
1560 if (session[s].snoop_ip && session[s].snoop_port)
1561 {
1562 // Snooping this session
1563 snoop_send_packet(buf, len, session[s].snoop_ip, session[s].snoop_port);
1564 }
1565
1566 // Increment packet counters
1567 increment_counter(&session[s].cin, &session[s].cin_wrap, len);
1568 session[s].cin_delta += len;
1569 session[s].pin++;
1570
1571 sess_local[s].cin += len;
1572 sess_local[s].pin++;
1573
1574 eth_tx += len;
1575
1576 STAT(tun_tx_packets);
1577 INC_STAT(tun_tx_bytes, len - 4);
1578 }
1579
1580
1581 // Process CCP messages
1582 void processccp(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
1583 {
1584 uint8_t b[MAXETHER];
1585 uint8_t *q;
1586
1587 CSTAT(processccp);
1588
1589 LOG_HEX(5, "CCP", p, l);
1590
1591 if (session[s].ppp.phase < Network)
1592 {
1593 LOG(2, s, t, "CCP %s ignored in %s phase\n", ppp_code(*p), ppp_phase(session[s].ppp.phase));
1594 return;
1595 }
1596
1597 if (l < 1)
1598 {
1599 LOG(1, s, t, "Short CCP packet\n");
1600 STAT(tunnel_rx_errors);
1601 }
1602
1603 LOG(3, s, t, "CCP: recv %s\n", ppp_code(*p));
1604 if (*p == ConfigAck)
1605 {
1606 switch (session[s].ppp.ccp)
1607 {
1608 case RequestSent:
1609 initialise_restart_count(s, ccp);
1610 change_state(s, ccp, AckReceived);
1611 break;
1612
1613 case AckReceived:
1614 case Opened:
1615 LOG(2, s, t, "CCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session[s].ppp.ccp));
1616 sendccp(s, t);
1617 change_state(s, ccp, RequestSent);
1618 break;
1619
1620 case AckSent:
1621 LOG(3, s, t, "CCP: Opened\n");
1622 change_state(s, ccp, Opened);
1623 break;
1624
1625 default:
1626 LOG(2, s, t, "CCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ccp));
1627 }
1628 }
1629 else if (*p == ConfigReq)
1630 {
1631 if (l < 6) // accept no compression
1632 *p = ConfigAck;
1633 else // compression requested--reject
1634 *p = ConfigRej;
1635
1636 q = makeppp(b, sizeof(b), p, l, s, t, PPPCCP);
1637 if (!q) return;
1638
1639 switch (session[s].ppp.ccp)
1640 {
1641 case Closed:
1642 q = makeppp(b, sizeof(b), p, 2, s, t, PPPCCP);
1643 if (!q) return;
1644 *q = TerminateAck;
1645 *((uint16_t *) (q + 2)) = htons(l = 4);
1646 break;
1647
1648 case Stopped:
1649 initialise_restart_count(s, ccp);
1650 sendccp(s, t);
1651 if (*q == ConfigAck)
1652 change_state(s, ccp, AckSent);
1653 else
1654 change_state(s, ccp, RequestSent);
1655
1656 break;
1657
1658 case RequestSent:
1659 if (*q == ConfigAck)
1660 change_state(s, ccp, AckSent);
1661
1662 break;
1663
1664 case AckReceived:
1665 if (*q == ConfigAck)
1666 change_state(s, ccp, Opened);
1667
1668 break;
1669
1670 case Opened:
1671 initialise_restart_count(s, ccp);
1672 sendccp(s, t);
1673 /* fallthrough */
1674
1675 case AckSent:
1676 if (*q == ConfigAck)
1677 change_state(s, ccp, AckSent);
1678 else
1679 change_state(s, ccp, RequestSent);
1680
1681 break;
1682
1683 default:
1684 LOG(2, s, t, "CCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ccp));
1685 return;
1686 }
1687
1688 LOG(3, s, t, "CCP: send %s\n", ppp_code(*q));
1689 tunnelsend(b, l + (q - b), t);
1690 }
1691 else if (*p == TerminateReq)
1692 {
1693 *p = TerminateAck;
1694 q = makeppp(b, sizeof(b), p, l, s, t, PPPCCP);
1695 if (!q) return;
1696 LOG(3, s, t, "CCP: send %s\n", ppp_code(*q));
1697 tunnelsend(b, l + (q - b), t);
1698 change_state(s, ccp, Stopped);
1699 }
1700 else if (*p != CodeRej)
1701 {
1702 ppp_code_rej(s, t, PPPCCP, "CCP", p, l, b, sizeof(b));
1703 }
1704 }
1705
1706 // send a CHAP challenge
1707 void sendchap(sessionidt s, tunnelidt t)
1708 {
1709 uint8_t b[MAXETHER];
1710 uint16_t r;
1711 uint8_t *q;
1712
1713 CSTAT(sendchap);
1714
1715 r = radiusnew(s);
1716 if (!r)
1717 {
1718 LOG(1, s, t, "No RADIUS to send challenge\n");
1719 STAT(tunnel_tx_errors);
1720 return;
1721 }
1722
1723 LOG(1, s, t, "Send CHAP challenge\n");
1724
1725 radius[r].chap = 1; // CHAP not PAP
1726 radius[r].id++;
1727 if (radius[r].state != RADIUSCHAP)
1728 radius[r].try = 0;
1729
1730 radius[r].state = RADIUSCHAP;
1731 radius[r].retry = backoff(radius[r].try++);
1732 if (radius[r].try > 5)
1733 {
1734 sessionshutdown(s, "CHAP timeout.", 3, 0);
1735 STAT(tunnel_tx_errors);
1736 return ;
1737 }
1738 q = makeppp(b, sizeof(b), 0, 0, s, t, PPPCHAP);
1739 if (!q) return;
1740
1741 *q = 1; // challenge
1742 q[1] = radius[r].id; // ID
1743 q[4] = 16; // value size (size of challenge)
1744 memcpy(q + 5, radius[r].auth, 16); // challenge
1745 strcpy((char *) q + 21, hostname); // our name
1746 *(uint16_t *) (q + 2) = htons(strlen(hostname) + 21); // length
1747 tunnelsend(b, strlen(hostname) + 21 + (q - b), t); // send it
1748 }
1749
1750 // fill in a L2TP message with a PPP frame,
1751 // copies existing PPP message and changes magic number if seen
1752 // returns start of PPP frame
1753 uint8_t *makeppp(uint8_t *b, int size, uint8_t *p, int l, sessionidt s, tunnelidt t, uint16_t mtype)
1754 {
1755 if (size < 12) // Need more space than this!!
1756 {
1757 static int backtrace_count = 0;
1758 LOG(0, s, t, "makeppp buffer too small for L2TP header (size=%d)\n", size);
1759 log_backtrace(backtrace_count, 5)
1760 return NULL;
1761 }
1762
1763 *(uint16_t *) (b + 0) = htons(0x0002); // L2TP with no options
1764 *(uint16_t *) (b + 2) = htons(tunnel[t].far); // tunnel
1765 *(uint16_t *) (b + 4) = htons(session[s].far); // session
1766 b += 6;
1767 if (mtype == PPPLCP || !(session[s].flags & SESSION_ACFC))
1768 {
1769 *(uint16_t *) b = htons(0xFF03); // HDLC header
1770 b += 2;
1771 }
1772 if (mtype < 0x100 && session[s].flags & SESSION_PFC)
1773 *b++ = mtype;
1774 else
1775 {
1776 *(uint16_t *) b = htons(mtype);
1777 b += 2;
1778 }
1779
1780 if (l + 12 > size)
1781 {
1782 static int backtrace_count = 0;
1783 LOG(2, s, t, "makeppp would overflow buffer (size=%d, header+payload=%d)\n", size, l + 12);
1784 log_backtrace(backtrace_count, 5)
1785 return NULL;
1786 }
1787
1788 if (p && l)
1789 memcpy(b, p, l);
1790
1791 return b;
1792 }
1793
1794 static int add_lcp_auth(uint8_t *b, int size, int authtype)
1795 {
1796 int len = 0;
1797 if ((authtype == AUTHCHAP && size < 5) || size < 4)
1798 return 0;
1799
1800 *b++ = 3; // Authentication-Protocol
1801 if (authtype == AUTHCHAP)
1802 {
1803 len = *b++ = 5; // length
1804 *(uint16_t *) b = htons(PPPCHAP); b += 2;
1805 *b++ = 5; // MD5
1806 }
1807 else if (authtype == AUTHPAP)
1808 {
1809 len = *b++ = 4; // length
1810 *(uint16_t *) b = htons(PPPPAP); b += 2;
1811 }
1812 else
1813 {
1814 LOG(0, 0, 0, "add_lcp_auth called with unsupported auth type %d\n", authtype);
1815 }
1816
1817 return len;
1818 }
1819
1820 // Send initial LCP ConfigReq for MRU, authentication type and magic no
1821 void sendlcp(sessionidt s, tunnelidt t)
1822 {
1823 uint8_t b[500], *q, *l;
1824 int authtype = sess_local[s].lcp_authtype;
1825
1826 if (!(q = makeppp(b, sizeof(b), NULL, 0, s, t, PPPLCP)))
1827 return;
1828
1829 LOG(3, s, t, "LCP: send ConfigReq%s%s%s\n",
1830 authtype ? " (" : "",
1831 authtype ? (authtype == AUTHCHAP ? "CHAP" : "PAP") : "",
1832 authtype ? ")" : "");
1833
1834 l = q;
1835 *l++ = ConfigReq;
1836 *l++ = ++sess_local[s].lcp_ident; // ID
1837
1838 l += 2; //Save space for length
1839
1840 if (sess_local[s].ppp_mru)
1841 {
1842 *l++ = 1; *l++ = 4; // Maximum-Receive-Unit (length 4)
1843 *(uint16_t *) l = htons(sess_local[s].ppp_mru); l += 2;
1844 }
1845
1846 if (authtype)
1847 l += add_lcp_auth(l, sizeof(b) - (l - b), authtype);
1848
1849 *l++ = 5; *l++ = 6; // Magic-Number (length 6)
1850 *(uint32_t *) l = htonl(session[s].magic);
1851 l += 4;
1852
1853 *(uint16_t *)(q + 2) = htons(l - q); // Length
1854
1855 LOG_HEX(5, "PPPLCP", q, l - q);
1856 if (config->debug > 3) dumplcp(q, l - q);
1857
1858 tunnelsend(b, (l - b), t);
1859 restart_timer(s, lcp);
1860 }
1861
1862 // Send CCP request for no compression
1863 void sendccp(sessionidt s, tunnelidt t)
1864 {
1865 uint8_t b[500], *q;
1866
1867 if (!(q = makeppp(b, sizeof(b), NULL, 0, s, t, PPPCCP)))
1868 return;
1869
1870 LOG(3, s, t, "CCP: send ConfigReq (no compression)\n");
1871
1872 *q = ConfigReq;
1873 *(q + 1) = ++sess_local[s].lcp_ident; // ID
1874 *(uint16_t *)(q + 2) = htons(4); // Length
1875
1876 LOG_HEX(5, "PPPCCP", q, 4);
1877 tunnelsend(b, (q - b) + 4 , t);
1878 restart_timer(s, ccp);
1879 }