d2c0cc48a7a0acd7971a264f17b86c5618b7cf3d
[l2tpns.git] / ppp.c
1 // L2TPNS PPP Stuff
2
3 char const *cvs_id_ppp = "$Id: ppp.c,v 1.74 2005/08/17 03:56:27 bodea Exp $";
4
5 #include <stdio.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <errno.h>
9 #include <stdlib.h>
10 #include "l2tpns.h"
11 #include "constants.h"
12 #include "plugin.h"
13 #include "util.h"
14 #include "tbf.h"
15 #include "cluster.h"
16
17 extern tunnelt *tunnel;
18 extern sessiont *session;
19 extern radiust *radius;
20 extern int tunfd;
21 extern char hostname[];
22 extern uint32_t eth_tx;
23 extern time_t time_now;
24 extern configt *config;
25
26 static 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 uint8_t *oq = q;
963 q = ppp_nak(s, b, sizeof(b), PPPIPCP, &response, q, p, o, (uint8_t *) &addr, sizeof(addr));
964 if (!q || (q != oq && *response == ConfigRej))
965 {
966 sessionshutdown(s, "Can't negotiate IPCP.", 3, 0);
967 return;
968 }
969 }
970
971 break;
972
973 case 129: // primary DNS
974 if (o[1] != 6 || o[1] > length) return;
975
976 addr = htonl(session[s].dns1);
977 if (memcmp(o + 2, &addr, (sizeof addr)))
978 {
979 q = ppp_nak(s, b, sizeof(b), PPPIPCP, &response, q, p, o, (uint8_t *) &addr, sizeof(addr));
980 if (!q) return;
981 }
982
983 break;
984
985 case 131: // secondary DNS
986 if (o[1] != 6 || o[1] > length) return;
987
988 addr = htonl(session[s].dns1);
989 if (memcmp(o + 2, &addr, sizeof(addr)))
990 {
991 q = ppp_nak(s, b, sizeof(b), PPPIPCP, &response, q, p, o, (uint8_t *) &addr, sizeof(addr));
992 if (!q) return;
993 }
994
995 break;
996
997 default:
998 LOG(2, s, t, " Rejecting PPP IPCP Option type %d\n", *o);
999 q = ppp_rej(s, b, sizeof(b), PPPIPCP, &response, q, p, o);
1000 if (!q) return;
1001 }
1002
1003 length -= o[1];
1004 o += o[1];
1005 }
1006
1007 if (response)
1008 {
1009 l = q - response; // IPCP packet length
1010 *((uint16_t *) (response + 2)) = htons(l); // update header
1011 }
1012 else if (gotip)
1013 {
1014 // Send packet back as ConfigAck
1015 response = makeppp(b, sizeof(b), p, l, s, t, PPPIPCP);
1016 if (!response) return;
1017 *response = ConfigAck;
1018 }
1019 else
1020 {
1021 LOG(1, s, t, "No IP in IPCP request\n");
1022 STAT(tunnel_rx_errors);
1023 return;
1024 }
1025
1026 switch (session[s].ppp.ipcp)
1027 {
1028 case Closed:
1029 response = makeppp(b, sizeof(b), p, 2, s, t, PPPIPCP);
1030 if (!response) return;
1031 *response = TerminateAck;
1032 *((uint16_t *) (response + 2)) = htons(l = 4);
1033 break;
1034
1035 case Stopped:
1036 initialise_restart_count(s, ipcp);
1037 sendipcp(s, t);
1038 if (*response == ConfigAck)
1039 change_state(s, ipcp, AckSent);
1040 else
1041 change_state(s, ipcp, RequestSent);
1042
1043 break;
1044
1045 case RequestSent:
1046 if (*response == ConfigAck)
1047 change_state(s, ipcp, AckSent);
1048
1049 break;
1050
1051 case AckReceived:
1052 if (*response == ConfigAck)
1053 ipcp_open(s, t);
1054
1055 break;
1056
1057 case Opened:
1058 initialise_restart_count(s, ipcp);
1059 sendipcp(s, t);
1060 /* fallthrough */
1061
1062 case AckSent:
1063 if (*response == ConfigAck)
1064 change_state(s, ipcp, AckSent);
1065 else
1066 change_state(s, ipcp, RequestSent);
1067
1068 break;
1069
1070 default:
1071 LOG(2, s, t, "IPCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ipcp));
1072 return;
1073 }
1074
1075 LOG(3, s, t, "IPCP: send %s\n", ppp_code(*response));
1076 tunnelsend(b, l + (response - b), t);
1077 }
1078 else if (*p == TerminateReq)
1079 {
1080 *p = TerminateAck;
1081 q = makeppp(b, sizeof(b), p, l, s, t, PPPIPCP);
1082 if (!q) return;
1083 LOG(3, s, t, "IPCP: send %s\n", ppp_code(*q));
1084 tunnelsend(b, l + (q - b), t);
1085 change_state(s, ipcp, Stopped);
1086 }
1087 else
1088 {
1089 int code = *p;
1090 int mru = session[s].mru;
1091 if (!mru)
1092 mru = DEFAULT_MRU;
1093
1094 if (l > mru) l = mru;
1095
1096 *p = CodeRej;
1097 q = makeppp(b, sizeof(b), p, l, s, t, PPPIPCP);
1098 if (!q) return;
1099
1100 LOG(2, s, t, "Unexpected IPCP code %s\n", ppp_code(code));
1101 LOG(3, s, t, "IPCP: send %s\n", ppp_code(*q));
1102 tunnelsend(b, l + (q - b), t);
1103 }
1104 }
1105
1106 static void ipv6cp_open(sessionidt s, tunnelidt t)
1107 {
1108 LOG(3, s, t, "IPV6CP: Opened\n");
1109
1110 change_state(s, ipv6cp, Opened);
1111 if (session[s].ipv6prefixlen)
1112 route6set(s, session[s].ipv6route, session[s].ipv6prefixlen, 1);
1113
1114 // Send an initial RA (TODO: Should we send these regularly?)
1115 send_ipv6_ra(s, t, NULL);
1116 }
1117
1118 // Process IPV6CP messages
1119 void processipv6cp(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
1120 {
1121 uint8_t b[MAXCONTROL];
1122 uint8_t *q = 0;
1123 uint16_t hl;
1124
1125 CSTAT(processipv6cp);
1126
1127 LOG_HEX(5, "IPV6CP", p, l);
1128 if (l < 4)
1129 {
1130 LOG(1, s, t, "Short IPV6CP %d bytes\n", l);
1131 STAT(tunnel_rx_errors);
1132 return ;
1133 }
1134
1135 if ((hl = ntohs(*(uint16_t *) (p + 2))) > l)
1136 {
1137 LOG(1, s, t, "Length mismatch IPV6CP %u/%u\n", hl, l);
1138 STAT(tunnel_rx_errors);
1139 return ;
1140 }
1141 l = hl;
1142
1143 if (session[s].ppp.phase < Network)
1144 {
1145 LOG(2, s, t, "IPV6CP %s ignored in %s phase\n", ppp_code(*p), ppp_phase(session[s].ppp.phase));
1146 return;
1147 }
1148
1149 LOG(3, s, t, "IPV6CP: recv %s\n", ppp_code(*p));
1150
1151 if (!config->ipv6_prefix.s6_addr[0])
1152 {
1153 LOG(2, s, t, "IPV6CP: %s rejected (not configured)\n", ppp_code(*p));
1154 *p = ProtocolRej;
1155 q = makeppp(b, sizeof(b), p, l, s, t, PPPIPV6CP);
1156 if (!q) return;
1157 tunnelsend(b, l + (q - b), t);
1158 return;
1159 }
1160
1161 if (!session[s].ip)
1162 {
1163 LOG(3, s, t, "IPV6CP: no IPv4 address (IPCP in state %s)\n", ppp_state(session[s].ppp.ipcp));
1164 return; // need IPCP to complete...
1165 }
1166
1167 if (*p == ConfigAck)
1168 {
1169 switch (session[s].ppp.ipv6cp)
1170 {
1171 case RequestSent:
1172 initialise_restart_count(s, ipv6cp);
1173 change_state(s, ipv6cp, AckReceived);
1174 break;
1175
1176 case AckReceived:
1177 case Opened:
1178 LOG(2, s, t, "IPV6CP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session[s].ppp.ipv6cp));
1179 sendipv6cp(s, t);
1180 change_state(s, ipv6cp, RequestSent);
1181 break;
1182
1183 case AckSent:
1184 ipv6cp_open(s, t);
1185 break;
1186
1187 default:
1188 LOG(2, s, t, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ipv6cp));
1189 }
1190 }
1191 else if (*p == ConfigReq)
1192 {
1193 uint8_t *response = 0;
1194 uint8_t *o = p + 4;
1195 int length = l - 4;
1196 int gotip = 0;
1197 uint8_t ident[8];
1198
1199 while (length > 2)
1200 {
1201 switch (*o)
1202 {
1203 case 1: // interface identifier
1204 gotip++; // seen address
1205 if (o[1] != 10 || o[1] > length) return;
1206
1207 *(uint32_t *) ident = htonl(session[s].ip);
1208 *(uint32_t *) (ident + 4) = 0;
1209
1210 if (memcmp(o + 2, ident, sizeof(ident)))
1211 {
1212 q = ppp_nak(s, b, sizeof(b), PPPIPV6CP, &response, q, p, o, ident, sizeof(ident));
1213 if (!q) return;
1214 }
1215
1216 break;
1217
1218 default:
1219 LOG(2, s, t, " Rejecting PPP IPV6CP Option type %d\n", *o);
1220 q = ppp_rej(s, b, sizeof(b), PPPIPV6CP, &response, q, p, o);
1221 if (!q) return;
1222 }
1223
1224 length -= o[1];
1225 o += o[1];
1226 }
1227
1228 if (response)
1229 {
1230 l = q - response; // IPV6CP packet length
1231 *((uint16_t *) (response + 2)) = htons(l); // update header
1232 }
1233 else if (gotip)
1234 {
1235 // Send packet back as ConfigAck
1236 response = makeppp(b, sizeof(b), p, l, s, t, PPPIPV6CP);
1237 if (!response) return;
1238 *response = ConfigAck;
1239 }
1240 else
1241 {
1242 LOG(1, s, t, "No interface identifier in IPV6CP request\n");
1243 STAT(tunnel_rx_errors);
1244 return;
1245 }
1246
1247 switch (session[s].ppp.ipv6cp)
1248 {
1249 case Closed:
1250 response = makeppp(b, sizeof(b), p, 2, s, t, PPPIPV6CP);
1251 if (!response) return;
1252 *response = TerminateAck;
1253 *((uint16_t *) (response + 2)) = htons(l = 4);
1254 break;
1255
1256 case Stopped:
1257 initialise_restart_count(s, ipv6cp);
1258 sendipv6cp(s, t);
1259 if (*response == ConfigAck)
1260 change_state(s, ipv6cp, AckSent);
1261 else
1262 change_state(s, ipv6cp, RequestSent);
1263
1264 break;
1265
1266 case RequestSent:
1267 if (*response == ConfigAck)
1268 change_state(s, ipv6cp, AckSent);
1269
1270 break;
1271
1272 case AckReceived:
1273 if (*response == ConfigAck)
1274 ipv6cp_open(s, t);
1275
1276 break;
1277
1278 case Opened:
1279 initialise_restart_count(s, ipv6cp);
1280 sendipv6cp(s, t);
1281 /* fallthrough */
1282
1283 case AckSent:
1284 if (*response == ConfigAck)
1285 change_state(s, ipv6cp, AckSent);
1286 else
1287 change_state(s, ipv6cp, RequestSent);
1288
1289 break;
1290
1291 default:
1292 LOG(2, s, t, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ipv6cp));
1293 return;
1294 }
1295
1296 LOG(3, s, t, "IPV6CP: send %s\n", ppp_code(*response));
1297 tunnelsend(b, l + (response - b), t);
1298 }
1299 else if (*p == TerminateReq)
1300 {
1301 *p = TerminateAck;
1302 q = makeppp(b, sizeof(b), p, l, s, t, PPPIPV6CP);
1303 if (!q) return;
1304 LOG(3, s, t, "IPV6CP: send %s\n", ppp_code(*q));
1305 tunnelsend(b, l + (q - b), t);
1306 change_state(s, ipv6cp, Stopped);
1307 }
1308 else
1309 {
1310 int code = *p;
1311 int mru = session[s].mru;
1312 if (!mru)
1313 mru = DEFAULT_MRU;
1314
1315 if (l > mru) l = mru;
1316
1317 *p = CodeRej;
1318 q = makeppp(b, sizeof(b), p, l, s, t, PPPIPV6CP);
1319 if (!q) return;
1320
1321 LOG(2, s, t, "Unexpected IPV6CP code %s\n", ppp_code(code));
1322 LOG(3, s, t, "IPV6CP: send %s\n", ppp_code(*q));
1323 tunnelsend(b, l + (q - b), t);
1324 }
1325 }
1326
1327 // process IP packet received
1328 //
1329 // This MUST be called with at least 4 byte behind 'p'.
1330 // (i.e. this routine writes to p[-4]).
1331 void processipin(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
1332 {
1333 in_addr_t ip;
1334
1335 CSTAT(processipin);
1336
1337 LOG_HEX(5, "IP", p, l);
1338
1339 ip = ntohl(*(uint32_t *)(p + 12));
1340
1341 if (l > MAXETHER)
1342 {
1343 LOG(1, s, t, "IP packet too long %d\n", l);
1344 STAT(tunnel_rx_errors);
1345 return ;
1346 }
1347
1348 if (session[s].ppp.phase != Network || session[s].ppp.ipcp != Opened)
1349 return;
1350
1351 // no spoof (do sessionbyip to handled statically routed subnets)
1352 if (ip != session[s].ip && sessionbyip(htonl(ip)) != s)
1353 {
1354 LOG(5, s, t, "Dropping packet with spoofed IP %s\n", fmtaddr(htonl(ip), 0));
1355 return;
1356 }
1357
1358 // run access-list if any
1359 if (session[s].filter_in && !ip_filter(p, l, session[s].filter_in - 1))
1360 return;
1361
1362 // Add on the tun header
1363 p -= 4;
1364 *(uint32_t *) p = htonl(PKTIP);
1365 l += 4;
1366
1367 // Are we throttled and a slave?
1368 if (session[s].tbf_in && !config->cluster_iam_master) {
1369 // Pass it to the master for handling.
1370 master_throttle_packet(session[s].tbf_in, p, l);
1371 return;
1372 }
1373
1374 // Are we throttled and a master??
1375 if (session[s].tbf_in && config->cluster_iam_master) {
1376 // Actually handle the throttled packets.
1377 tbf_queue_packet(session[s].tbf_in, p, l);
1378 return;
1379 }
1380
1381 // send to ethernet
1382 if (tun_write(p, l) < 0)
1383 {
1384 STAT(tun_tx_errors);
1385 LOG(0, s, t, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1386 l, strerror(errno), tunfd, p);
1387
1388 return;
1389 }
1390
1391 p += 4;
1392 l -= 4;
1393
1394 if (session[s].snoop_ip && session[s].snoop_port)
1395 {
1396 // Snooping this session
1397 snoop_send_packet(p, l, session[s].snoop_ip, session[s].snoop_port);
1398 }
1399
1400 increment_counter(&session[s].cin, &session[s].cin_wrap, l);
1401 session[s].cin_delta += l;
1402 session[s].pin++;
1403
1404 sess_local[s].cin += l;
1405 sess_local[s].pin++;
1406
1407 eth_tx += l;
1408
1409 STAT(tun_tx_packets);
1410 INC_STAT(tun_tx_bytes, l);
1411 }
1412
1413 // process IPv6 packet received
1414 //
1415 // This MUST be called with at least 4 byte behind 'p'.
1416 // (i.e. this routine writes to p[-4]).
1417 void processipv6in(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
1418 {
1419 struct in6_addr ip;
1420 in_addr_t ipv4;
1421
1422 CSTAT(processipv6in);
1423
1424 LOG_HEX(5, "IPv6", p, l);
1425
1426 ip = *(struct in6_addr *) (p + 8);
1427 ipv4 = ntohl(*(uint32_t *)(p + 16));
1428
1429 if (l > MAXETHER)
1430 {
1431 LOG(1, s, t, "IP packet too long %d\n", l);
1432 STAT(tunnel_rx_errors);
1433 return ;
1434 }
1435
1436 if (session[s].ppp.phase != Network || session[s].ppp.ipv6cp != Opened)
1437 return;
1438
1439 // no spoof
1440 if (ipv4 != session[s].ip && memcmp(&config->ipv6_prefix, &ip, 8) && sessionbyipv6(ip) != s)
1441 {
1442 char str[INET6_ADDRSTRLEN];
1443 LOG(5, s, t, "Dropping packet with spoofed IP %s\n",
1444 inet_ntop(AF_INET6, &ip, str, INET6_ADDRSTRLEN));
1445 return;
1446 }
1447
1448 // Check if it's a Router Solicition message.
1449 if (*(p + 6) == 58 && *(p + 7) == 255 && *(p + 24) == 0xFF && *(p + 25) == 2 &&
1450 *(uint32_t *)(p + 26) == 0 && *(uint32_t *)(p + 30) == 0 &&
1451 *(uint32_t *)(p + 34) == 0 &&
1452 *(p + 38) == 0 && *(p + 39) == 2 && *(p + 40) == 133) {
1453 LOG(3, s, t, "Got IPv6 RS\n");
1454 send_ipv6_ra(s, t, &ip);
1455 return;
1456 }
1457
1458 // Add on the tun header
1459 p -= 4;
1460 *(uint32_t *) p = htonl(PKTIPV6);
1461 l += 4;
1462
1463 // Are we throttled and a slave?
1464 if (session[s].tbf_in && !config->cluster_iam_master) {
1465 // Pass it to the master for handling.
1466 master_throttle_packet(session[s].tbf_in, p, l);
1467 return;
1468 }
1469
1470 // Are we throttled and a master??
1471 if (session[s].tbf_in && config->cluster_iam_master) {
1472 // Actually handle the throttled packets.
1473 tbf_queue_packet(session[s].tbf_in, p, l);
1474 return;
1475 }
1476
1477 // send to ethernet
1478 if (tun_write(p, l) < 0)
1479 {
1480 STAT(tun_tx_errors);
1481 LOG(0, s, t, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1482 l, strerror(errno), tunfd, p);
1483
1484 return;
1485 }
1486
1487 p += 4;
1488 l -= 4;
1489
1490 if (session[s].snoop_ip && session[s].snoop_port)
1491 {
1492 // Snooping this session
1493 snoop_send_packet(p, l, session[s].snoop_ip, session[s].snoop_port);
1494 }
1495
1496 increment_counter(&session[s].cin, &session[s].cin_wrap, l);
1497 session[s].cin_delta += l;
1498 session[s].pin++;
1499
1500 sess_local[s].cin += l;
1501 sess_local[s].pin++;
1502
1503 eth_tx += l;
1504
1505 STAT(tun_tx_packets);
1506 INC_STAT(tun_tx_bytes, l);
1507 }
1508
1509 //
1510 // Helper routine for the TBF filters.
1511 // Used to send queued data in from the user.
1512 //
1513 void send_ipin(sessionidt s, uint8_t *buf, int len)
1514 {
1515 LOG_HEX(5, "IP in throttled", buf, len);
1516
1517 if (write(tunfd, buf, len) < 0)
1518 {
1519 STAT(tun_tx_errors);
1520 LOG(0, 0, 0, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1521 len, strerror(errno), tunfd, buf);
1522
1523 return;
1524 }
1525
1526 buf += 4;
1527 len -= 4;
1528
1529 if (session[s].snoop_ip && session[s].snoop_port)
1530 {
1531 // Snooping this session
1532 snoop_send_packet(buf, len, session[s].snoop_ip, session[s].snoop_port);
1533 }
1534
1535 // Increment packet counters
1536 increment_counter(&session[s].cin, &session[s].cin_wrap, len);
1537 session[s].cin_delta += len;
1538 session[s].pin++;
1539
1540 sess_local[s].cin += len;
1541 sess_local[s].pin++;
1542
1543 eth_tx += len;
1544
1545 STAT(tun_tx_packets);
1546 INC_STAT(tun_tx_bytes, len - 4);
1547 }
1548
1549
1550 // Process CCP messages
1551 void processccp(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
1552 {
1553 uint8_t b[MAXCONTROL];
1554 uint8_t *q;
1555
1556 CSTAT(processccp);
1557
1558 LOG_HEX(5, "CCP", p, l);
1559
1560 if (session[s].ppp.phase < Network)
1561 {
1562 LOG(2, s, t, "CCP %s ignored in %s phase\n", ppp_code(*p), ppp_phase(session[s].ppp.phase));
1563 return;
1564 }
1565
1566 if (l < 1)
1567 {
1568 LOG(1, s, t, "Short CCP packet\n");
1569 STAT(tunnel_rx_errors);
1570 }
1571
1572 LOG(3, s, t, "CCP: recv %s\n", ppp_code(*p));
1573 if (*p == ConfigAck)
1574 {
1575 switch (session[s].ppp.ccp)
1576 {
1577 case RequestSent:
1578 initialise_restart_count(s, ccp);
1579 change_state(s, ccp, AckReceived);
1580 break;
1581
1582 case AckReceived:
1583 case Opened:
1584 LOG(2, s, t, "CCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session[s].ppp.ccp));
1585 sendccp(s, t);
1586 change_state(s, ccp, RequestSent);
1587 break;
1588
1589 case AckSent:
1590 LOG(3, s, t, "CCP: Opened\n");
1591 change_state(s, ccp, Opened);
1592 break;
1593
1594 default:
1595 LOG(2, s, t, "CCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ccp));
1596 }
1597 }
1598 else if (*p == ConfigReq)
1599 {
1600 if (l < 6) // accept no compression
1601 *p = ConfigAck;
1602 else // compression requested--reject
1603 *p = ConfigRej;
1604
1605 q = makeppp(b, sizeof(b), p, l, s, t, PPPCCP);
1606 if (!q) return;
1607
1608 switch (session[s].ppp.ccp)
1609 {
1610 case Closed:
1611 q = makeppp(b, sizeof(b), p, 2, s, t, PPPCCP);
1612 if (!q) return;
1613 *q = TerminateAck;
1614 *((uint16_t *) (q + 2)) = htons(l = 4);
1615 break;
1616
1617 case Stopped:
1618 initialise_restart_count(s, ccp);
1619 sendccp(s, t);
1620 if (*q == ConfigAck)
1621 change_state(s, ccp, AckSent);
1622 else
1623 change_state(s, ccp, RequestSent);
1624
1625 break;
1626
1627 case RequestSent:
1628 if (*q == ConfigAck)
1629 change_state(s, ccp, AckSent);
1630
1631 break;
1632
1633 case AckReceived:
1634 if (*q == ConfigAck)
1635 change_state(s, ccp, Opened);
1636
1637 break;
1638
1639 case Opened:
1640 initialise_restart_count(s, ccp);
1641 sendccp(s, t);
1642 /* fallthrough */
1643
1644 case AckSent:
1645 if (*q == ConfigAck)
1646 change_state(s, ccp, AckSent);
1647 else
1648 change_state(s, ccp, RequestSent);
1649
1650 break;
1651
1652 default:
1653 LOG(2, s, t, "CCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ccp));
1654 return;
1655 }
1656
1657 LOG(3, s, t, "CCP: send %s\n", ppp_code(*q));
1658 tunnelsend(b, l + (q - b), t);
1659 }
1660 else if (*p == TerminateReq)
1661 {
1662 *p = TerminateAck;
1663 q = makeppp(b, sizeof(b), p, l, s, t, PPPCCP);
1664 if (!q) return;
1665 LOG(3, s, t, "CCP: send %s\n", ppp_code(*q));
1666 tunnelsend(b, l + (q - b), t);
1667 change_state(s, ccp, Stopped);
1668 }
1669 else
1670 {
1671 int code = *p;
1672 int mru = session[s].mru;
1673 if (!mru)
1674 mru = DEFAULT_MRU;
1675
1676 if (l > mru) l = mru;
1677
1678 *p = CodeRej;
1679 q = makeppp(b, sizeof(b), p, l, s, t, PPPCCP);
1680 if (!q) return;
1681
1682 LOG(2, s, t, "Unexpected CCP code %s\n", ppp_code(code));
1683 LOG(3, s, t, "CCP: send %s\n", ppp_code(*q));
1684 tunnelsend(b, l + (q - b), t);
1685 }
1686 }
1687
1688 // send a CHAP challenge
1689 void sendchap(sessionidt s, tunnelidt t)
1690 {
1691 uint8_t b[MAXCONTROL];
1692 uint16_t r;
1693 uint8_t *q;
1694
1695 CSTAT(sendchap);
1696
1697 r = radiusnew(s);
1698 if (!r)
1699 {
1700 LOG(1, s, t, "No RADIUS to send challenge\n");
1701 STAT(tunnel_tx_errors);
1702 return;
1703 }
1704
1705 LOG(1, s, t, "Send CHAP challenge\n");
1706
1707 radius[r].chap = 1; // CHAP not PAP
1708 radius[r].id++;
1709 if (radius[r].state != RADIUSCHAP)
1710 radius[r].try = 0;
1711
1712 radius[r].state = RADIUSCHAP;
1713 radius[r].retry = backoff(radius[r].try++);
1714 if (radius[r].try > 5)
1715 {
1716 sessionshutdown(s, "CHAP timeout.", 3, 0);
1717 STAT(tunnel_tx_errors);
1718 return ;
1719 }
1720 q = makeppp(b, sizeof(b), 0, 0, s, t, PPPCHAP);
1721 if (!q) return;
1722
1723 *q = 1; // challenge
1724 q[1] = radius[r].id; // ID
1725 q[4] = 16; // value size (size of challenge)
1726 memcpy(q + 5, radius[r].auth, 16); // challenge
1727 strcpy((char *) q + 21, hostname); // our name
1728 *(uint16_t *) (q + 2) = htons(strlen(hostname) + 21); // length
1729 tunnelsend(b, strlen(hostname) + 21 + (q - b), t); // send it
1730 }
1731
1732 // fill in a L2TP message with a PPP frame,
1733 // copies existing PPP message and changes magic number if seen
1734 // returns start of PPP frame
1735 uint8_t *makeppp(uint8_t *b, int size, uint8_t *p, int l, sessionidt s, tunnelidt t, uint16_t mtype)
1736 {
1737 if (size < 12) // Need more space than this!!
1738 {
1739 static int backtrace_count = 0;
1740 LOG(0, s, t, "makeppp buffer too small for L2TP header (size=%d)\n", size);
1741 log_backtrace(backtrace_count, 5)
1742 return NULL;
1743 }
1744
1745 *(uint16_t *) (b + 0) = htons(0x0002); // L2TP with no options
1746 *(uint16_t *) (b + 2) = htons(tunnel[t].far); // tunnel
1747 *(uint16_t *) (b + 4) = htons(session[s].far); // session
1748 b += 6;
1749 if (mtype == PPPLCP || !(session[s].l2tp_flags & SESSIONACFC))
1750 {
1751 *(uint16_t *) b = htons(0xFF03); // HDLC header
1752 b += 2;
1753 }
1754 if (mtype < 0x100 && session[s].l2tp_flags & SESSIONPFC)
1755 *b++ = mtype;
1756 else
1757 {
1758 *(uint16_t *) b = htons(mtype);
1759 b += 2;
1760 }
1761
1762 if (l + 12 > size)
1763 {
1764 static int backtrace_count = 0;
1765 LOG(2, s, t, "makeppp would overflow buffer (size=%d, header+payload=%d)\n", size, l + 12);
1766 log_backtrace(backtrace_count, 5)
1767 return NULL;
1768 }
1769
1770 if (p && l)
1771 memcpy(b, p, l);
1772
1773 return b;
1774 }
1775
1776 static int add_lcp_auth(uint8_t *b, int size, int authtype)
1777 {
1778 int len = 0;
1779 if ((authtype == AUTHCHAP && size < 5) || size < 4)
1780 return 0;
1781
1782 *b++ = 3; // Authentication-Protocol
1783 if (authtype == AUTHCHAP)
1784 {
1785 len = *b++ = 5; // length
1786 *(uint16_t *) b = htons(PPPCHAP); b += 2;
1787 *b++ = 5; // MD5
1788 }
1789 else if (authtype == AUTHPAP)
1790 {
1791 len = *b++ = 4; // length
1792 *(uint16_t *) b = htons(PPPPAP); b += 2;
1793 }
1794 else
1795 {
1796 LOG(0, 0, 0, "add_lcp_auth called with unsupported auth type %d\n", authtype);
1797 }
1798
1799 return len;
1800 }
1801
1802 // Send initial LCP ConfigReq for MRU, authentication type and magic no
1803 void sendlcp(sessionidt s, tunnelidt t, int authtype)
1804 {
1805 uint8_t b[500], *q, *l;
1806
1807 if (!(q = makeppp(b, sizeof(b), NULL, 0, s, t, PPPLCP)))
1808 return;
1809
1810 LOG(3, s, t, "LCP: send ConfigReq%s%s%s\n",
1811 authtype ? " (" : "",
1812 authtype ? (authtype == AUTHCHAP ? "CHAP" : "PAP") : "",
1813 authtype ? ")" : "");
1814
1815 if (!session[s].mru)
1816 session[s].mru = DEFAULT_MRU;
1817
1818 l = q;
1819 *l++ = ConfigReq;
1820 *l++ = (time_now % 255) + 1; // ID
1821
1822 l += 2; //Save space for length
1823
1824 *l++ = 1; *l++ = 4; // Maximum-Receive-Unit (length 4)
1825 *(uint16_t *) l = htons(session[s].mru); l += 2;
1826
1827 if (authtype)
1828 l += add_lcp_auth(l, sizeof(b) - (l - b), authtype);
1829
1830 *l++ = 5; *l++ = 6; // Magic-Number (length 6)
1831 *(uint32_t *) l = htonl(session[s].magic);
1832 l += 4;
1833
1834 *(uint16_t *)(q + 2) = htons(l - q); // Length
1835
1836 LOG_HEX(5, "PPPLCP", q, l - q);
1837 if (config->debug > 3) dumplcp(q, l - q);
1838
1839 tunnelsend(b, (l - b), t);
1840 }
1841
1842 // Send CCP request for no compression
1843 void sendccp(sessionidt s, tunnelidt t)
1844 {
1845 uint8_t b[500], *q;
1846
1847 if (!(q = makeppp(b, sizeof(b), NULL, 0, s, t, PPPCCP)))
1848 return;
1849
1850 LOG(3, s, t, "CCP: send ConfigReq (no compression)\n");
1851
1852 *q = ConfigReq;
1853 *(uint8_t *)(q + 1) = (time_now % 255) + 1; // ID
1854 *(uint16_t *)(q + 2) = htons(4); // Length
1855
1856 LOG_HEX(5, "PPPCCP", q, 4);
1857 tunnelsend(b, (q - b) + 4 , t);
1858 }