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