578273ac342717a204296add51850774d12fd607
[l2tpns.git] / ppp.c
1 // L2TPNS PPP Stuff
2
3 char const *cvs_id_ppp = "$Id: ppp.c,v 1.97 2006/03/27 03:01:08 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 < MINMTU) mru = MINMTU;
453 if (mru > size) mru = size;
454
455 l += 4;
456 if (l > mru) l = mru;
457
458 q = makeppp(buf, size, 0, 0, s, t, proto);
459 if (!q) return;
460
461 *q = CodeRej;
462 *(q + 1) = ++sess_local[s].lcp_ident;
463 *(uint16_t *)(q + 2) = htons(l);
464 memcpy(q + 4, p, l - 4);
465
466 LOG(2, s, t, "Unexpected %s code %s\n", pname, ppp_code(*p));
467 LOG(3, s, t, "%s: send %s\n", pname, ppp_code(*q));
468 if (config->debug > 3) dumplcp(q, l);
469
470 tunnelsend(buf, l + (q - buf), t);
471 }
472
473 // Process LCP messages
474 void processlcp(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
475 {
476 uint8_t b[MAXETHER];
477 uint8_t *q = NULL;
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 || *p == EchoReply) ? 4 : 3, s, t,
502 "LCP: recv %s\n", ppp_code(*p));
503
504 if (config->debug > 3) dumplcp(p, l);
505
506 if (*p == ConfigAck)
507 {
508 int x = l - 4;
509 uint8_t *o = (p + 4);
510 int authtype = 0;
511
512 while (x > 2)
513 {
514 int type = o[0];
515 int length = o[1];
516
517 if (length == 0 || type == 0 || x < length) break;
518 switch (type)
519 {
520 case 3: // Authentication-Protocol
521 {
522 int proto = ntohs(*(uint16_t *)(o + 2));
523 if (proto == PPPPAP)
524 authtype = AUTHPAP;
525 else if (proto == PPPCHAP && *(o + 4) == 5)
526 authtype = AUTHCHAP;
527 }
528
529 break;
530 }
531 x -= length;
532 o += length;
533 }
534
535 if (!session[s].ip && authtype)
536 sess_local[s].lcp_authtype = authtype;
537
538 switch (session[s].ppp.lcp)
539 {
540 case RequestSent:
541 initialise_restart_count(s, lcp);
542 change_state(s, lcp, AckReceived);
543 break;
544
545 case AckReceived:
546 case Opened:
547 LOG(2, s, t, "LCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session[s].ppp.lcp));
548 if (session[s].ppp.lcp == Opened)
549 lcp_restart(s);
550
551 sendlcp(s, t);
552 change_state(s, lcp, RequestSent);
553 break;
554
555 case AckSent:
556 lcp_open(s, t);
557 break;
558
559 default:
560 LOG(2, s, t, "LCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.lcp));
561 }
562 }
563 else if (*p == ConfigReq)
564 {
565 int x = l - 4;
566 uint8_t *o = (p + 4);
567 uint8_t *response = 0;
568 static uint8_t asyncmap[4] = { 0, 0, 0, 0 }; // all zero
569 static uint8_t authproto[5];
570
571 while (x > 2)
572 {
573 int type = o[0];
574 int length = o[1];
575
576 if (length == 0 || type == 0 || x < length) break;
577 switch (type)
578 {
579 case 1: // Maximum-Receive-Unit
580 {
581 uint16_t mru = ntohs(*(uint16_t *)(o + 2));
582 if (mru >= MINMTU)
583 {
584 session[s].mru = mru;
585 cluster_send_session(s);
586 break;
587 }
588
589 LOG(3, s, t, " Remote requesting MRU of %u. Rejecting.\n", mru);
590 mru = htons(MRU);
591 q = ppp_conf_nak(s, b, sizeof(b), PPPLCP, &response, q, p, o, (uint8_t *) &mru, sizeof(mru));
592 }
593 break;
594
595 case 2: // Async-Control-Character-Map
596 if (!ntohl(*(uint32_t *)(o + 2))) // all bits zero is OK
597 break;
598
599 LOG(3, s, t, " Remote requesting asyncmap. Rejecting.\n");
600 q = ppp_conf_nak(s, b, sizeof(b), PPPLCP, &response, q, p, o, asyncmap, sizeof(asyncmap));
601 break;
602
603 case 3: // Authentication-Protocol
604 {
605 int proto = ntohs(*(uint16_t *)(o + 2));
606 char proto_name[] = "0x0000";
607 int alen;
608
609 if (proto == PPPPAP)
610 {
611 if (config->radius_authtypes & AUTHPAP)
612 {
613 sess_local[s].lcp_authtype = AUTHPAP;
614 break;
615 }
616
617 strcpy(proto_name, "PAP");
618 }
619 else if (proto == PPPCHAP)
620 {
621 if (config->radius_authtypes & AUTHCHAP
622 && *(o + 4) == 5) // MD5
623 {
624 sess_local[s].lcp_authtype = AUTHCHAP;
625 break;
626 }
627
628 strcpy(proto_name, "CHAP");
629 }
630 else
631 sprintf(proto_name, "%#4.4x", proto);
632
633 LOG(3, s, t, " Remote requesting %s authentication. Rejecting.\n", proto_name);
634
635 alen = add_lcp_auth(authproto, sizeof(authproto), config->radius_authprefer);
636 if (alen < 2) break; // paranoia
637
638 q = ppp_conf_nak(s, b, sizeof(b), PPPLCP, &response, q, p, o, authproto + 2, alen - 2);
639 if (q && *response == ConfigNak &&
640 config->radius_authtypes != config->radius_authprefer)
641 {
642 // alternate type
643 alen = add_lcp_auth(authproto, sizeof(authproto), config->radius_authtypes & ~config->radius_authprefer);
644 if (alen < 2) break;
645 q = ppp_conf_nak(s, b, sizeof(b), PPPLCP, &response, q, p, o, authproto + 2, alen - 2);
646 }
647
648 break;
649 }
650 break;
651
652 case 4: // Quality-Protocol
653 case 5: // Magic-Number
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 if (length < 4) break;
751 sess_local[s].ppp_mru = ntohs(*(uint16_t *)(o + 2));
752 LOG(3, s, t, " Remote requested MRU of %u\n", sess_local[s].ppp_mru);
753 }
754 else
755 {
756 sess_local[s].ppp_mru = 0;
757 LOG(3, s, t, " Remote rejected MRU negotiation\n");
758 }
759
760 break;
761
762 case 3: // Authentication-Protocol
763 if (authtype > 0)
764 break;
765
766 if (*p == ConfigNak)
767 {
768 int proto;
769
770 if (length < 4) break;
771 proto = ntohs(*(uint16_t *)(o + 2));
772
773 if (proto == PPPPAP)
774 {
775 authtype = config->radius_authtypes & AUTHPAP;
776 LOG(3, s, t, " Remote requested PAP authentication...%sing\n",
777 authtype ? "accept" : "reject");
778 }
779 else if (proto == PPPCHAP && length > 4 && *(o + 4) == 5)
780 {
781 authtype = config->radius_authtypes & AUTHCHAP;
782 LOG(3, s, t, " Remote requested CHAP authentication...%sing\n",
783 authtype ? "accept" : "reject");
784 }
785 else
786 {
787 LOG(3, s, t, " Rejecting unsupported authentication %#4x\n",
788 proto);
789 }
790 }
791 else
792 {
793 LOG(2, s, t, "LCP: remote rejected auth negotiation\n");
794 authtype = 0; // shutdown
795 }
796
797 break;
798
799 case 5: // Magic-Number
800 session[s].magic = 0;
801 if (*p == ConfigNak)
802 {
803 if (length < 6) break;
804 session[s].magic = ntohl(*(uint32_t *)(o + 2));
805 }
806
807 if (session[s].magic)
808 LOG(3, s, t, " Remote requested magic-no %x\n", session[s].magic);
809 else
810 LOG(3, s, t, " Remote rejected magic-no\n");
811
812 cluster_send_session(s);
813 break;
814
815 default:
816 LOG(2, s, t, "LCP: remote sent %s for type %u?\n", ppp_code(*p), type);
817 sessionshutdown(s, "Unable to negotiate LCP.", 3, 0);
818 return;
819 }
820 x -= length;
821 o += length;
822 }
823
824 if (!authtype)
825 {
826 sessionshutdown(s, "Unsupported authentication.", 3, 0);
827 return;
828 }
829
830 if (authtype > 0)
831 sess_local[s].lcp_authtype = authtype;
832
833 switch (session[s].ppp.lcp)
834 {
835 case Closed:
836 case Stopped:
837 {
838 uint8_t *response = makeppp(b, sizeof(b), p, 2, s, t, PPPLCP);
839 if (!response) return;
840 *response = TerminateAck;
841 *((uint16_t *) (response + 2)) = htons(l = 4);
842
843 LOG(3, s, t, "LCP: send %s\n", ppp_code(*response));
844 if (config->debug > 3) dumplcp(response, l);
845
846 tunnelsend(b, l + (response - b), t);
847 }
848 break;
849
850 case RequestSent:
851 case AckSent:
852 initialise_restart_count(s, lcp);
853 sendlcp(s, t);
854 break;
855
856 case AckReceived:
857 LOG(2, s, t, "LCP: ConfigNak in state %s? Sending ConfigReq\n", ppp_state(session[s].ppp.lcp));
858 sendlcp(s, t);
859 break;
860
861 case Opened:
862 lcp_restart(s);
863 sendlcp(s, t);
864 break;
865
866 default:
867 LOG(2, s, t, "LCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.lcp));
868 return;
869 }
870 }
871 else if (*p == TerminateReq)
872 {
873 *p = TerminateAck; // close
874 q = makeppp(b, sizeof(b), p, l, s, t, PPPLCP);
875 if (!q) return;
876
877 LOG(3, s, t, "LCP: send %s\n", ppp_code(*q));
878 if (config->debug > 3) dumplcp(q, l);
879
880 tunnelsend(b, l + (q - b), t); // send it
881 sessionshutdown(s, "Remote end closed connection.", 3, 0);
882 }
883 else if (*p == TerminateAck)
884 {
885 sessionshutdown(s, "Connection closed.", 3, 0);
886 }
887 else if (*p == ProtocolRej)
888 {
889 uint16_t proto = 0;
890
891 if (l > 4)
892 {
893 proto = *(p+4);
894 if (l > 5 && !(proto & 1))
895 {
896 proto <<= 8;
897 proto |= *(p+5);
898 }
899 }
900
901 if (proto == PPPIPV6CP)
902 {
903 LOG(3, s, t, "IPv6 rejected\n");
904 change_state(s, ipv6cp, Closed);
905 }
906 else
907 {
908 LOG(3, s, t, "LCP protocol reject: 0x%04X\n", proto);
909 }
910 }
911 else if (*p == EchoReq)
912 {
913 *p = EchoReply; // reply
914 *(uint32_t *) (p + 4) = htonl(session[s].magic); // our magic number
915 q = makeppp(b, sizeof(b), p, l, s, t, PPPLCP);
916 if (!q) return;
917
918 LOG(4, s, t, "LCP: send %s\n", ppp_code(*q));
919 if (config->debug > 3) dumplcp(q, l);
920
921 tunnelsend(b, l + (q - b), t); // send it
922 }
923 else if (*p == EchoReply)
924 {
925 // Ignore it, last_packet time is set earlier than this.
926 }
927 else if (*p != CodeRej)
928 {
929 ppp_code_rej(s, t, PPPLCP, "LCP", p, l, b, sizeof(b));
930 }
931 }
932
933 static void ipcp_open(sessionidt s, tunnelidt t)
934 {
935 LOG(3, s, t, "IPCP: Opened, session is now active\n");
936
937 change_state(s, ipcp, Opened);
938
939 if (!(session[s].walled_garden || session[s].flags & SESSION_STARTED))
940 {
941 uint16_t r = radiusnew(s);
942 if (r)
943 {
944 radiussend(r, RADIUSSTART); // send radius start
945
946 // don't send further Start records if IPCP is restarted
947 session[s].flags |= SESSION_STARTED;
948 cluster_send_session(s);
949 }
950 }
951
952 // start IPv6 if configured and still in passive state
953 if (session[s].ppp.ipv6cp == Stopped)
954 {
955 sendipv6cp(s, t);
956 change_state(s, ipv6cp, RequestSent);
957 }
958 }
959
960 // Process IPCP messages
961 void processipcp(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
962 {
963 uint8_t b[MAXETHER];
964 uint8_t *q = 0;
965 uint16_t hl;
966
967 CSTAT(processipcp);
968
969 LOG_HEX(5, "IPCP", p, l);
970 if (l < 4)
971 {
972 LOG(1, s, t, "Short IPCP %d bytes\n", l);
973 STAT(tunnel_rx_errors);
974 return ;
975 }
976
977 if ((hl = ntohs(*(uint16_t *) (p + 2))) > l)
978 {
979 LOG(1, s, t, "Length mismatch IPCP %u/%u\n", hl, l);
980 STAT(tunnel_rx_errors);
981 return ;
982 }
983 l = hl;
984
985 if (session[s].ppp.phase < Network)
986 {
987 LOG(2, s, t, "IPCP %s ignored in %s phase\n", ppp_code(*p), ppp_phase(session[s].ppp.phase));
988 return;
989 }
990
991 LOG(3, s, t, "IPCP: recv %s\n", ppp_code(*p));
992
993 if (*p == ConfigAck)
994 {
995 switch (session[s].ppp.ipcp)
996 {
997 case RequestSent:
998 initialise_restart_count(s, ipcp);
999 change_state(s, ipcp, AckReceived);
1000 break;
1001
1002 case AckReceived:
1003 case Opened:
1004 LOG(2, s, t, "IPCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session[s].ppp.ipcp));
1005 sendipcp(s, t);
1006 change_state(s, ipcp, RequestSent);
1007 break;
1008
1009 case AckSent:
1010 ipcp_open(s, t);
1011 break;
1012
1013 default:
1014 LOG(2, s, t, "IPCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ipcp));
1015 }
1016 }
1017 else if (*p == ConfigReq)
1018 {
1019 uint8_t *response = 0;
1020 uint8_t *o = p + 4;
1021 int length = l - 4;
1022 int gotip = 0;
1023 in_addr_t addr;
1024
1025 while (length > 2)
1026 {
1027 if (!o[1] || o[1] > length) return;
1028
1029 switch (*o)
1030 {
1031 case 3: // ip address
1032 gotip++; // seen address
1033 if (o[1] != 6) return;
1034
1035 addr = htonl(session[s].ip);
1036 if (memcmp(o + 2, &addr, (sizeof addr)))
1037 {
1038 uint8_t *oq = q;
1039 q = ppp_conf_nak(s, b, sizeof(b), PPPIPCP, &response, q, p, o, (uint8_t *) &addr, sizeof(addr));
1040 if (!q || (q != oq && *response == ConfigRej))
1041 {
1042 sessionshutdown(s, "Can't negotiate IPCP.", 3, 0);
1043 return;
1044 }
1045 }
1046
1047 break;
1048
1049 case 129: // primary DNS
1050 if (o[1] != 6) return;
1051
1052 addr = htonl(session[s].dns1);
1053 if (memcmp(o + 2, &addr, (sizeof addr)))
1054 {
1055 q = ppp_conf_nak(s, b, sizeof(b), PPPIPCP, &response, q, p, o, (uint8_t *) &addr, sizeof(addr));
1056 if (!q) return;
1057 }
1058
1059 break;
1060
1061 case 131: // secondary DNS
1062 if (o[1] != 6) return;
1063
1064 addr = htonl(session[s].dns2);
1065 if (memcmp(o + 2, &addr, sizeof(addr)))
1066 {
1067 q = ppp_conf_nak(s, b, sizeof(b), PPPIPCP, &response, q, p, o, (uint8_t *) &addr, sizeof(addr));
1068 if (!q) return;
1069 }
1070
1071 break;
1072
1073 default:
1074 LOG(2, s, t, " Rejecting PPP IPCP Option type %d\n", *o);
1075 q = ppp_conf_rej(s, b, sizeof(b), PPPIPCP, &response, q, p, o);
1076 if (!q) return;
1077 }
1078
1079 length -= o[1];
1080 o += o[1];
1081 }
1082
1083 if (response)
1084 {
1085 l = q - response; // IPCP packet length
1086 *((uint16_t *) (response + 2)) = htons(l); // update header
1087 }
1088 else if (gotip)
1089 {
1090 // Send packet back as ConfigAck
1091 response = makeppp(b, sizeof(b), p, l, s, t, PPPIPCP);
1092 if (!response) return;
1093 *response = ConfigAck;
1094 }
1095 else
1096 {
1097 LOG(1, s, t, "No IP in IPCP request\n");
1098 STAT(tunnel_rx_errors);
1099 return;
1100 }
1101
1102 switch (session[s].ppp.ipcp)
1103 {
1104 case Closed:
1105 response = makeppp(b, sizeof(b), p, 2, s, t, PPPIPCP);
1106 if (!response) return;
1107 *response = TerminateAck;
1108 *((uint16_t *) (response + 2)) = htons(l = 4);
1109 break;
1110
1111 case Stopped:
1112 initialise_restart_count(s, ipcp);
1113 sendipcp(s, t);
1114 if (*response == ConfigAck)
1115 change_state(s, ipcp, AckSent);
1116 else
1117 change_state(s, ipcp, RequestSent);
1118
1119 break;
1120
1121 case RequestSent:
1122 if (*response == ConfigAck)
1123 change_state(s, ipcp, AckSent);
1124
1125 break;
1126
1127 case AckReceived:
1128 if (*response == ConfigAck)
1129 ipcp_open(s, t);
1130
1131 break;
1132
1133 case Opened:
1134 initialise_restart_count(s, ipcp);
1135 sendipcp(s, t);
1136 /* fallthrough */
1137
1138 case AckSent:
1139 if (*response == ConfigAck)
1140 change_state(s, ipcp, AckSent);
1141 else
1142 change_state(s, ipcp, RequestSent);
1143
1144 break;
1145
1146 default:
1147 LOG(2, s, t, "IPCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ipcp));
1148 return;
1149 }
1150
1151 LOG(3, s, t, "IPCP: send %s\n", ppp_code(*response));
1152 tunnelsend(b, l + (response - b), t);
1153 }
1154 else if (*p == TerminateReq)
1155 {
1156 *p = TerminateAck;
1157 q = makeppp(b, sizeof(b), p, l, s, t, PPPIPCP);
1158 if (!q) return;
1159 LOG(3, s, t, "IPCP: send %s\n", ppp_code(*q));
1160 tunnelsend(b, l + (q - b), t);
1161 change_state(s, ipcp, Stopped);
1162 }
1163 else if (*p != CodeRej)
1164 {
1165 ppp_code_rej(s, t, PPPIPCP, "IPCP", p, l, b, sizeof(b));
1166 }
1167 }
1168
1169 static void ipv6cp_open(sessionidt s, tunnelidt t)
1170 {
1171 LOG(3, s, t, "IPV6CP: Opened\n");
1172
1173 change_state(s, ipv6cp, Opened);
1174 if (session[s].ipv6prefixlen)
1175 route6set(s, session[s].ipv6route, session[s].ipv6prefixlen, 1);
1176
1177 // Send an initial RA (TODO: Should we send these regularly?)
1178 send_ipv6_ra(s, t, NULL);
1179 }
1180
1181 // Process IPV6CP messages
1182 void processipv6cp(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
1183 {
1184 uint8_t b[MAXETHER];
1185 uint8_t *q = 0;
1186 uint16_t hl;
1187
1188 CSTAT(processipv6cp);
1189
1190 LOG_HEX(5, "IPV6CP", p, l);
1191 if (l < 4)
1192 {
1193 LOG(1, s, t, "Short IPV6CP %d bytes\n", l);
1194 STAT(tunnel_rx_errors);
1195 return ;
1196 }
1197
1198 if ((hl = ntohs(*(uint16_t *) (p + 2))) > l)
1199 {
1200 LOG(1, s, t, "Length mismatch IPV6CP %u/%u\n", hl, l);
1201 STAT(tunnel_rx_errors);
1202 return ;
1203 }
1204 l = hl;
1205
1206 if (session[s].ppp.phase < Network)
1207 {
1208 LOG(2, s, t, "IPV6CP %s ignored in %s phase\n", ppp_code(*p), ppp_phase(session[s].ppp.phase));
1209 return;
1210 }
1211
1212 LOG(3, s, t, "IPV6CP: recv %s\n", ppp_code(*p));
1213
1214 if (!session[s].ip)
1215 {
1216 LOG(3, s, t, "IPV6CP: no IPv4 address (IPCP in state %s)\n", ppp_state(session[s].ppp.ipcp));
1217 return; // need IPCP to complete...
1218 }
1219
1220 if (*p == ConfigAck)
1221 {
1222 switch (session[s].ppp.ipv6cp)
1223 {
1224 case RequestSent:
1225 initialise_restart_count(s, ipv6cp);
1226 change_state(s, ipv6cp, AckReceived);
1227 break;
1228
1229 case AckReceived:
1230 case Opened:
1231 LOG(2, s, t, "IPV6CP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session[s].ppp.ipv6cp));
1232 sendipv6cp(s, t);
1233 change_state(s, ipv6cp, RequestSent);
1234 break;
1235
1236 case AckSent:
1237 ipv6cp_open(s, t);
1238 break;
1239
1240 default:
1241 LOG(2, s, t, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ipv6cp));
1242 }
1243 }
1244 else if (*p == ConfigReq)
1245 {
1246 uint8_t *response = 0;
1247 uint8_t *o = p + 4;
1248 int length = l - 4;
1249 int gotip = 0;
1250 uint8_t ident[8];
1251
1252 while (length > 2)
1253 {
1254 if (!o[1] || o[1] > length) return;
1255
1256 switch (*o)
1257 {
1258 case 1: // interface identifier
1259 gotip++; // seen address
1260 if (o[1] != 10) return;
1261
1262 *(uint32_t *) ident = htonl(session[s].ip);
1263 *(uint32_t *) (ident + 4) = 0;
1264
1265 if (memcmp(o + 2, ident, sizeof(ident)))
1266 {
1267 q = ppp_conf_nak(s, b, sizeof(b), PPPIPV6CP, &response, q, p, o, ident, sizeof(ident));
1268 if (!q) return;
1269 }
1270
1271 break;
1272
1273 default:
1274 LOG(2, s, t, " Rejecting PPP IPV6CP Option type %d\n", *o);
1275 q = ppp_conf_rej(s, b, sizeof(b), PPPIPV6CP, &response, q, p, o);
1276 if (!q) return;
1277 }
1278
1279 length -= o[1];
1280 o += o[1];
1281 }
1282
1283 if (response)
1284 {
1285 l = q - response; // IPV6CP packet length
1286 *((uint16_t *) (response + 2)) = htons(l); // update header
1287 }
1288 else if (gotip)
1289 {
1290 // Send packet back as ConfigAck
1291 response = makeppp(b, sizeof(b), p, l, s, t, PPPIPV6CP);
1292 if (!response) return;
1293 *response = ConfigAck;
1294 }
1295 else
1296 {
1297 LOG(1, s, t, "No interface identifier in IPV6CP request\n");
1298 STAT(tunnel_rx_errors);
1299 return;
1300 }
1301
1302 switch (session[s].ppp.ipv6cp)
1303 {
1304 case Closed:
1305 response = makeppp(b, sizeof(b), p, 2, s, t, PPPIPV6CP);
1306 if (!response) return;
1307 *response = TerminateAck;
1308 *((uint16_t *) (response + 2)) = htons(l = 4);
1309 break;
1310
1311 case Stopped:
1312 initialise_restart_count(s, ipv6cp);
1313 sendipv6cp(s, t);
1314 if (*response == ConfigAck)
1315 change_state(s, ipv6cp, AckSent);
1316 else
1317 change_state(s, ipv6cp, RequestSent);
1318
1319 break;
1320
1321 case RequestSent:
1322 if (*response == ConfigAck)
1323 change_state(s, ipv6cp, AckSent);
1324
1325 break;
1326
1327 case AckReceived:
1328 if (*response == ConfigAck)
1329 ipv6cp_open(s, t);
1330
1331 break;
1332
1333 case Opened:
1334 initialise_restart_count(s, ipv6cp);
1335 sendipv6cp(s, t);
1336 /* fallthrough */
1337
1338 case AckSent:
1339 if (*response == ConfigAck)
1340 change_state(s, ipv6cp, AckSent);
1341 else
1342 change_state(s, ipv6cp, RequestSent);
1343
1344 break;
1345
1346 default:
1347 LOG(2, s, t, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ipv6cp));
1348 return;
1349 }
1350
1351 LOG(3, s, t, "IPV6CP: send %s\n", ppp_code(*response));
1352 tunnelsend(b, l + (response - b), t);
1353 }
1354 else if (*p == TerminateReq)
1355 {
1356 *p = TerminateAck;
1357 q = makeppp(b, sizeof(b), p, l, s, t, PPPIPV6CP);
1358 if (!q) return;
1359 LOG(3, s, t, "IPV6CP: send %s\n", ppp_code(*q));
1360 tunnelsend(b, l + (q - b), t);
1361 change_state(s, ipv6cp, Stopped);
1362 }
1363 else if (*p != CodeRej)
1364 {
1365 ppp_code_rej(s, t, PPPIPV6CP, "IPV6CP", p, l, b, sizeof(b));
1366 }
1367 }
1368
1369 // process IP packet received
1370 //
1371 // This MUST be called with at least 4 byte behind 'p'.
1372 // (i.e. this routine writes to p[-4]).
1373 void processipin(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
1374 {
1375 in_addr_t ip;
1376
1377 CSTAT(processipin);
1378
1379 LOG_HEX(5, "IP", p, l);
1380
1381 if (l < 20)
1382 {
1383 LOG(1, s, t, "IP packet too short %d\n", l);
1384 STAT(tunnel_rx_errors);
1385 return ;
1386 }
1387
1388 ip = ntohl(*(uint32_t *)(p + 12));
1389
1390 if (l > MAXETHER)
1391 {
1392 LOG(1, s, t, "IP packet too long %d\n", l);
1393 STAT(tunnel_rx_errors);
1394 return ;
1395 }
1396
1397 if (session[s].ppp.phase != Network || session[s].ppp.ipcp != Opened)
1398 return;
1399
1400 // no spoof (do sessionbyip to handled statically routed subnets)
1401 if (ip != session[s].ip && sessionbyip(htonl(ip)) != s)
1402 {
1403 LOG(5, s, t, "Dropping packet with spoofed IP %s\n", fmtaddr(htonl(ip), 0));
1404 return;
1405 }
1406
1407 // run access-list if any
1408 if (session[s].filter_in && !ip_filter(p, l, session[s].filter_in - 1))
1409 return;
1410
1411 // adjust MSS on SYN and SYN,ACK packets with options
1412 if ((ntohs(*(uint16_t *) (p + 6)) & 0x1fff) == 0 && p[9] == IPPROTO_TCP) // first tcp fragment
1413 {
1414 int ihl = (p[0] & 0xf) * 4; // length of IP header
1415 if (l >= ihl + 20 && (p[ihl + 13] & TCP_FLAG_SYN) && ((p[ihl + 12] >> 4) > 5))
1416 adjust_tcp_mss(s, t, p, l, p + ihl);
1417 }
1418
1419 // Add on the tun header
1420 p -= 4;
1421 *(uint32_t *) p = htonl(PKTIP);
1422 l += 4;
1423
1424 // Are we throttled and a slave?
1425 if (session[s].tbf_in && !config->cluster_iam_master) {
1426 // Pass it to the master for handling.
1427 master_throttle_packet(session[s].tbf_in, p, l);
1428 return;
1429 }
1430
1431 // Are we throttled and a master??
1432 if (session[s].tbf_in && config->cluster_iam_master) {
1433 // Actually handle the throttled packets.
1434 tbf_queue_packet(session[s].tbf_in, p, l);
1435 return;
1436 }
1437
1438 // send to ethernet
1439 if (tun_write(p, l) < 0)
1440 {
1441 STAT(tun_tx_errors);
1442 LOG(0, s, t, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1443 l, strerror(errno), tunfd, p);
1444
1445 return;
1446 }
1447
1448 p += 4;
1449 l -= 4;
1450
1451 if (session[s].snoop_ip && session[s].snoop_port)
1452 {
1453 // Snooping this session
1454 snoop_send_packet(p, l, session[s].snoop_ip, session[s].snoop_port);
1455 }
1456
1457 increment_counter(&session[s].cin, &session[s].cin_wrap, l);
1458 session[s].cin_delta += l;
1459 session[s].pin++;
1460
1461 sess_local[s].cin += l;
1462 sess_local[s].pin++;
1463
1464 eth_tx += l;
1465
1466 STAT(tun_tx_packets);
1467 INC_STAT(tun_tx_bytes, l);
1468 }
1469
1470 // process IPv6 packet received
1471 //
1472 // This MUST be called with at least 4 byte behind 'p'.
1473 // (i.e. this routine writes to p[-4]).
1474 void processipv6in(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
1475 {
1476 struct in6_addr ip;
1477 in_addr_t ipv4;
1478
1479 CSTAT(processipv6in);
1480
1481 LOG_HEX(5, "IPv6", p, l);
1482
1483 ip = *(struct in6_addr *) (p + 8);
1484 ipv4 = ntohl(*(uint32_t *)(p + 16));
1485
1486 if (l > MAXETHER)
1487 {
1488 LOG(1, s, t, "IP packet too long %d\n", l);
1489 STAT(tunnel_rx_errors);
1490 return ;
1491 }
1492
1493 if (session[s].ppp.phase != Network || session[s].ppp.ipv6cp != Opened)
1494 return;
1495
1496 // no spoof
1497 if (ipv4 != session[s].ip && memcmp(&config->ipv6_prefix, &ip, 8) && sessionbyipv6(ip) != s)
1498 {
1499 char str[INET6_ADDRSTRLEN];
1500 LOG(5, s, t, "Dropping packet with spoofed IP %s\n",
1501 inet_ntop(AF_INET6, &ip, str, INET6_ADDRSTRLEN));
1502 return;
1503 }
1504
1505 // Check if it's a Router Solicition message.
1506 if (*(p + 6) == 58 && *(p + 7) == 255 && *(p + 24) == 0xFF && *(p + 25) == 2 &&
1507 *(uint32_t *)(p + 26) == 0 && *(uint32_t *)(p + 30) == 0 &&
1508 *(uint32_t *)(p + 34) == 0 &&
1509 *(p + 38) == 0 && *(p + 39) == 2 && *(p + 40) == 133) {
1510 LOG(3, s, t, "Got IPv6 RS\n");
1511 send_ipv6_ra(s, t, &ip);
1512 return;
1513 }
1514
1515 // Add on the tun header
1516 p -= 4;
1517 *(uint32_t *) p = htonl(PKTIPV6);
1518 l += 4;
1519
1520 // Are we throttled and a slave?
1521 if (session[s].tbf_in && !config->cluster_iam_master) {
1522 // Pass it to the master for handling.
1523 master_throttle_packet(session[s].tbf_in, p, l);
1524 return;
1525 }
1526
1527 // Are we throttled and a master??
1528 if (session[s].tbf_in && config->cluster_iam_master) {
1529 // Actually handle the throttled packets.
1530 tbf_queue_packet(session[s].tbf_in, p, l);
1531 return;
1532 }
1533
1534 // send to ethernet
1535 if (tun_write(p, l) < 0)
1536 {
1537 STAT(tun_tx_errors);
1538 LOG(0, s, t, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1539 l, strerror(errno), tunfd, p);
1540
1541 return;
1542 }
1543
1544 p += 4;
1545 l -= 4;
1546
1547 if (session[s].snoop_ip && session[s].snoop_port)
1548 {
1549 // Snooping this session
1550 snoop_send_packet(p, l, session[s].snoop_ip, session[s].snoop_port);
1551 }
1552
1553 increment_counter(&session[s].cin, &session[s].cin_wrap, l);
1554 session[s].cin_delta += l;
1555 session[s].pin++;
1556
1557 sess_local[s].cin += l;
1558 sess_local[s].pin++;
1559
1560 eth_tx += l;
1561
1562 STAT(tun_tx_packets);
1563 INC_STAT(tun_tx_bytes, l);
1564 }
1565
1566 //
1567 // Helper routine for the TBF filters.
1568 // Used to send queued data in from the user.
1569 //
1570 void send_ipin(sessionidt s, uint8_t *buf, int len)
1571 {
1572 LOG_HEX(5, "IP in throttled", buf, len);
1573
1574 if (write(tunfd, buf, len) < 0)
1575 {
1576 STAT(tun_tx_errors);
1577 LOG(0, 0, 0, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1578 len, strerror(errno), tunfd, buf);
1579
1580 return;
1581 }
1582
1583 buf += 4;
1584 len -= 4;
1585
1586 if (session[s].snoop_ip && session[s].snoop_port)
1587 {
1588 // Snooping this session
1589 snoop_send_packet(buf, len, session[s].snoop_ip, session[s].snoop_port);
1590 }
1591
1592 // Increment packet counters
1593 increment_counter(&session[s].cin, &session[s].cin_wrap, len);
1594 session[s].cin_delta += len;
1595 session[s].pin++;
1596
1597 sess_local[s].cin += len;
1598 sess_local[s].pin++;
1599
1600 eth_tx += len;
1601
1602 STAT(tun_tx_packets);
1603 INC_STAT(tun_tx_bytes, len - 4);
1604 }
1605
1606
1607 // Process CCP messages
1608 void processccp(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
1609 {
1610 uint8_t b[MAXETHER];
1611 uint8_t *q;
1612
1613 CSTAT(processccp);
1614
1615 LOG_HEX(5, "CCP", p, l);
1616
1617 if (session[s].ppp.phase < Network)
1618 {
1619 LOG(2, s, t, "CCP %s ignored in %s phase\n", ppp_code(*p), ppp_phase(session[s].ppp.phase));
1620 return;
1621 }
1622
1623 if (l < 1)
1624 {
1625 LOG(1, s, t, "Short CCP packet\n");
1626 STAT(tunnel_rx_errors);
1627 }
1628
1629 LOG(3, s, t, "CCP: recv %s\n", ppp_code(*p));
1630 if (*p == ConfigAck)
1631 {
1632 switch (session[s].ppp.ccp)
1633 {
1634 case RequestSent:
1635 initialise_restart_count(s, ccp);
1636 change_state(s, ccp, AckReceived);
1637 break;
1638
1639 case AckReceived:
1640 case Opened:
1641 LOG(2, s, t, "CCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session[s].ppp.ccp));
1642 sendccp(s, t);
1643 change_state(s, ccp, RequestSent);
1644 break;
1645
1646 case AckSent:
1647 LOG(3, s, t, "CCP: Opened\n");
1648 change_state(s, ccp, Opened);
1649 break;
1650
1651 default:
1652 LOG(2, s, t, "CCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ccp));
1653 }
1654 }
1655 else if (*p == ConfigReq)
1656 {
1657 if (l < 6) // accept no compression
1658 *p = ConfigAck;
1659 else // compression requested--reject
1660 *p = ConfigRej;
1661
1662 q = makeppp(b, sizeof(b), p, l, s, t, PPPCCP);
1663 if (!q) return;
1664
1665 switch (session[s].ppp.ccp)
1666 {
1667 case Closed:
1668 q = makeppp(b, sizeof(b), p, 2, s, t, PPPCCP);
1669 if (!q) return;
1670 *q = TerminateAck;
1671 *((uint16_t *) (q + 2)) = htons(l = 4);
1672 break;
1673
1674 case Stopped:
1675 initialise_restart_count(s, ccp);
1676 sendccp(s, t);
1677 if (*q == ConfigAck)
1678 change_state(s, ccp, AckSent);
1679 else
1680 change_state(s, ccp, RequestSent);
1681
1682 break;
1683
1684 case RequestSent:
1685 if (*q == ConfigAck)
1686 change_state(s, ccp, AckSent);
1687
1688 break;
1689
1690 case AckReceived:
1691 if (*q == ConfigAck)
1692 change_state(s, ccp, Opened);
1693
1694 break;
1695
1696 case Opened:
1697 initialise_restart_count(s, ccp);
1698 sendccp(s, t);
1699 /* fallthrough */
1700
1701 case AckSent:
1702 if (*q == ConfigAck)
1703 change_state(s, ccp, AckSent);
1704 else
1705 change_state(s, ccp, RequestSent);
1706
1707 break;
1708
1709 default:
1710 LOG(2, s, t, "CCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ccp));
1711 return;
1712 }
1713
1714 LOG(3, s, t, "CCP: send %s\n", ppp_code(*q));
1715 tunnelsend(b, l + (q - b), t);
1716 }
1717 else if (*p == TerminateReq)
1718 {
1719 *p = TerminateAck;
1720 q = makeppp(b, sizeof(b), p, l, s, t, PPPCCP);
1721 if (!q) return;
1722 LOG(3, s, t, "CCP: send %s\n", ppp_code(*q));
1723 tunnelsend(b, l + (q - b), t);
1724 change_state(s, ccp, Stopped);
1725 }
1726 else if (*p != CodeRej)
1727 {
1728 ppp_code_rej(s, t, PPPCCP, "CCP", p, l, b, sizeof(b));
1729 }
1730 }
1731
1732 // send a CHAP challenge
1733 void sendchap(sessionidt s, tunnelidt t)
1734 {
1735 uint8_t b[MAXETHER];
1736 uint16_t r;
1737 uint8_t *q;
1738
1739 CSTAT(sendchap);
1740
1741 r = radiusnew(s);
1742 if (!r)
1743 {
1744 LOG(1, s, t, "No RADIUS to send challenge\n");
1745 STAT(tunnel_tx_errors);
1746 return;
1747 }
1748
1749 LOG(1, s, t, "Send CHAP challenge\n");
1750
1751 radius[r].chap = 1; // CHAP not PAP
1752 radius[r].id++;
1753 if (radius[r].state != RADIUSCHAP)
1754 radius[r].try = 0;
1755
1756 radius[r].state = RADIUSCHAP;
1757 radius[r].retry = backoff(radius[r].try++);
1758 if (radius[r].try > 5)
1759 {
1760 sessionshutdown(s, "CHAP timeout.", 3, 0);
1761 STAT(tunnel_tx_errors);
1762 return ;
1763 }
1764 q = makeppp(b, sizeof(b), 0, 0, s, t, PPPCHAP);
1765 if (!q) return;
1766
1767 *q = 1; // challenge
1768 q[1] = radius[r].id; // ID
1769 q[4] = 16; // value size (size of challenge)
1770 memcpy(q + 5, radius[r].auth, 16); // challenge
1771 strcpy((char *) q + 21, hostname); // our name
1772 *(uint16_t *) (q + 2) = htons(strlen(hostname) + 21); // length
1773 tunnelsend(b, strlen(hostname) + 21 + (q - b), t); // send it
1774 }
1775
1776 // fill in a L2TP message with a PPP frame,
1777 // returns start of PPP frame
1778 uint8_t *makeppp(uint8_t *b, int size, uint8_t *p, int l, sessionidt s, tunnelidt t, uint16_t mtype)
1779 {
1780 if (size < 12) // Need more space than this!!
1781 {
1782 LOG(0, s, t, "makeppp buffer too small for L2TP header (size=%d)\n", size);
1783 return NULL;
1784 }
1785
1786 *(uint16_t *) (b + 0) = htons(0x0002); // L2TP with no options
1787 *(uint16_t *) (b + 2) = htons(tunnel[t].far); // tunnel
1788 *(uint16_t *) (b + 4) = htons(session[s].far); // session
1789 b += 6;
1790 if (mtype == PPPLCP || !(session[s].flags & SESSION_ACFC))
1791 {
1792 *(uint16_t *) b = htons(0xFF03); // HDLC header
1793 b += 2;
1794 }
1795 if (mtype < 0x100 && session[s].flags & SESSION_PFC)
1796 *b++ = mtype;
1797 else
1798 {
1799 *(uint16_t *) b = htons(mtype);
1800 b += 2;
1801 }
1802
1803 if (l + 12 > size)
1804 {
1805 LOG(2, s, t, "makeppp would overflow buffer (size=%d, header+payload=%d)\n", size, l + 12);
1806 return NULL;
1807 }
1808
1809 if (p && l)
1810 memcpy(b, p, l);
1811
1812 return b;
1813 }
1814
1815 static int add_lcp_auth(uint8_t *b, int size, int authtype)
1816 {
1817 int len = 0;
1818 if ((authtype == AUTHCHAP && size < 5) || size < 4)
1819 return 0;
1820
1821 *b++ = 3; // Authentication-Protocol
1822 if (authtype == AUTHCHAP)
1823 {
1824 len = *b++ = 5; // length
1825 *(uint16_t *) b = htons(PPPCHAP); b += 2;
1826 *b++ = 5; // MD5
1827 }
1828 else if (authtype == AUTHPAP)
1829 {
1830 len = *b++ = 4; // length
1831 *(uint16_t *) b = htons(PPPPAP); b += 2;
1832 }
1833 else
1834 {
1835 LOG(0, 0, 0, "add_lcp_auth called with unsupported auth type %d\n", authtype);
1836 }
1837
1838 return len;
1839 }
1840
1841 // Send LCP ConfigReq for MRU, authentication type and magic no
1842 void sendlcp(sessionidt s, tunnelidt t)
1843 {
1844 uint8_t b[500], *q, *l;
1845 int authtype = sess_local[s].lcp_authtype;
1846
1847 if (!(q = makeppp(b, sizeof(b), NULL, 0, s, t, PPPLCP)))
1848 return;
1849
1850 LOG(3, s, t, "LCP: send ConfigReq%s%s%s\n",
1851 authtype ? " (" : "",
1852 authtype ? (authtype == AUTHCHAP ? "CHAP" : "PAP") : "",
1853 authtype ? ")" : "");
1854
1855 l = q;
1856 *l++ = ConfigReq;
1857 *l++ = ++sess_local[s].lcp_ident; // ID
1858
1859 l += 2; //Save space for length
1860
1861 if (sess_local[s].ppp_mru)
1862 {
1863 *l++ = 1; *l++ = 4; // Maximum-Receive-Unit (length 4)
1864 *(uint16_t *) l = htons(sess_local[s].ppp_mru); l += 2;
1865 }
1866
1867 if (authtype)
1868 l += add_lcp_auth(l, sizeof(b) - (l - b), authtype);
1869
1870 if (session[s].magic)
1871 {
1872 *l++ = 5; *l++ = 6; // Magic-Number (length 6)
1873 *(uint32_t *) l = htonl(session[s].magic);
1874 l += 4;
1875 }
1876
1877 *(uint16_t *)(q + 2) = htons(l - q); // Length
1878
1879 LOG_HEX(5, "PPPLCP", q, l - q);
1880 if (config->debug > 3) dumplcp(q, l - q);
1881
1882 tunnelsend(b, (l - b), t);
1883 restart_timer(s, lcp);
1884 }
1885
1886 // Send CCP request for no compression
1887 void sendccp(sessionidt s, tunnelidt t)
1888 {
1889 uint8_t b[500], *q;
1890
1891 if (!(q = makeppp(b, sizeof(b), NULL, 0, s, t, PPPCCP)))
1892 return;
1893
1894 LOG(3, s, t, "CCP: send ConfigReq (no compression)\n");
1895
1896 *q = ConfigReq;
1897 *(q + 1) = ++sess_local[s].lcp_ident; // ID
1898 *(uint16_t *)(q + 2) = htons(4); // Length
1899
1900 LOG_HEX(5, "PPPCCP", q, 4);
1901 tunnelsend(b, (q - b) + 4 , t);
1902 restart_timer(s, ccp);
1903 }
1904
1905 // Reject unknown/unconfigured protocols
1906 void protoreject(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l, uint16_t proto)
1907 {
1908
1909 uint8_t buf[MAXETHER];
1910 uint8_t *q;
1911 int mru = session[s].mru;
1912 if (mru < MINMTU) mru = MINMTU;
1913 if (mru > sizeof(buf)) mru = sizeof(buf);
1914
1915 l += 6;
1916 if (l > mru) l = mru;
1917
1918 q = makeppp(buf, sizeof(buf), 0, 0, s, t, PPPLCP);
1919 if (!q) return;
1920
1921 *q = ProtocolRej;
1922 *(q + 1) = ++sess_local[s].lcp_ident;
1923 *(uint16_t *)(q + 2) = htons(l);
1924 *(uint16_t *)(q + 4) = htons(proto);
1925 memcpy(q + 6, p, l - 6);
1926
1927 if (proto == PPPIPV6CP)
1928 LOG(3, s, t, "LCP: send ProtocolRej (IPV6CP: not configured)\n");
1929 else
1930 LOG(2, s, t, "LCP: sent ProtocolRej (0x%04X: unsupported)\n", proto);
1931
1932 tunnelsend(buf, l + (q - buf), t);
1933 }