005d42a6af6faa720e2ac81d22f8c14373942b01
[l2tpns.git] / ppp.c
1 // L2TPNS PPP Stuff
2
3 char const *cvs_id_ppp = "$Id: ppp.c,v 1.76 2005/08/29 03:21:14 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
151 if (l < 4)
152 {
153 LOG(1, s, t, "Short CHAP %u bytes\n", l);
154 STAT(tunnel_rx_errors);
155 sessionshutdown(s, "Short CHAP packet.", 3, 0);
156 return;
157 }
158
159 if ((hl = ntohs(*(uint16_t *) (p + 2))) > l)
160 {
161 LOG(1, s, t, "Length mismatch CHAP %u/%u\n", hl, l);
162 STAT(tunnel_rx_errors);
163 sessionshutdown(s, "CHAP length mismatch.", 3, 0);
164 return;
165 }
166 l = hl;
167
168 if (*p != 2)
169 {
170 LOG(1, s, t, "Unexpected CHAP response code %d\n", *p);
171 STAT(tunnel_rx_errors);
172 sessionshutdown(s, "CHAP length mismatch.", 3, 0);
173 return;
174 }
175
176 r = sess_local[s].radius;
177 if (!r)
178 {
179 LOG(3, s, t, "Unexpected CHAP message\n");
180 return;
181 }
182
183 if (session[s].ppp.phase != Authenticate)
184 {
185 LOG(2, s, t, "CHAP ignored in %s phase\n", ppp_phase(session[s].ppp.phase));
186 return;
187 }
188
189 if (p[1] != radius[r].id)
190 {
191 LOG(1, s, t, "Wrong CHAP response ID %d (should be %d) (%d)\n", p[1], radius[r].id, r);
192 STAT(tunnel_rx_errors);
193 sessionshutdown(s, "Unexpected CHAP response ID.", 3, 0);
194 return;
195 }
196
197 if (l < 5 || p[4] != 16)
198 {
199 LOG(1, s, t, "Bad CHAP response length %d\n", l < 5 ? -1 : p[4]);
200 STAT(tunnel_rx_errors);
201 sessionshutdown(s, "Bad CHAP response length.", 3, 0);
202 return;
203 }
204
205 l -= 5;
206 p += 5;
207 if (l < 16 || l - 16 >= sizeof(session[s].user))
208 {
209 LOG(1, s, t, "CHAP user too long %d\n", l - 16);
210 STAT(tunnel_rx_errors);
211 sessionshutdown(s, "CHAP username too long.", 3, 0);
212 return;
213 }
214
215 // Run PRE_AUTH plugins
216 {
217 struct param_pre_auth packet = { &tunnel[t], &session[s], NULL, NULL, PPPCHAP, 1 };
218
219 packet.password = calloc(17, 1);
220 memcpy(packet.password, p, 16);
221
222 p += 16;
223 l -= 16;
224
225 packet.username = calloc(l + 1, 1);
226 memcpy(packet.username, p, l);
227
228 run_plugins(PLUGIN_PRE_AUTH, &packet);
229 if (!packet.continue_auth)
230 {
231 LOG(3, s, t, "A plugin rejected PRE_AUTH\n");
232 if (packet.username) free(packet.username);
233 if (packet.password) free(packet.password);
234 return;
235 }
236
237 strncpy(session[s].user, packet.username, sizeof(session[s].user) - 1);
238 memcpy(radius[r].pass, packet.password, 16);
239
240 free(packet.username);
241 free(packet.password);
242 }
243
244 radius[r].chap = 1;
245 LOG(3, s, t, "CHAP login %s\n", session[s].user);
246 radiussend(r, RADIUSAUTH);
247 }
248
249 static void dumplcp(uint8_t *p, int l)
250 {
251 int x = l - 4;
252 uint8_t *o = (p + 4);
253
254 LOG_HEX(5, "PPP LCP Packet", p, l);
255 LOG(4, 0, 0, "PPP LCP Packet type %d (%s len %d)\n", *p, ppp_code((int)*p), ntohs( ((uint16_t *) p)[1]) );
256 LOG(4, 0, 0, "Length: %d\n", l);
257 if (*p != ConfigReq && *p != ConfigRej && *p != ConfigAck)
258 return;
259
260 while (x > 2)
261 {
262 int type = o[0];
263 int length = o[1];
264 if (length < 2)
265 {
266 LOG(4, 0, 0, " Option length is %d...\n", length);
267 break;
268 }
269 if (type == 0)
270 {
271 LOG(4, 0, 0, " Option type is 0...\n");
272 x -= length;
273 o += length;
274 continue;
275 }
276 switch (type)
277 {
278 case 1: // Maximum-Receive-Unit
279 if (length == 4)
280 LOG(4, 0, 0, " %s %d\n", ppp_lcp_option(type), ntohs(*(uint16_t *)(o + 2)));
281 else
282 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type), length);
283 break;
284 case 2: // Async-Control-Character-Map
285 if (length == 6)
286 {
287 uint32_t asyncmap = ntohl(*(uint32_t *)(o + 2));
288 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type), asyncmap);
289 }
290 else
291 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type), length);
292 break;
293 case 3: // Authentication-Protocol
294 if (length == 4)
295 {
296 int proto = ntohs(*(uint16_t *)(o + 2));
297 LOG(4, 0, 0, " %s 0x%x (%s)\n", ppp_lcp_option(type), proto,
298 proto == PPPPAP ? "PAP" : "UNSUPPORTED");
299 }
300 else if (length == 5)
301 {
302 int proto = ntohs(*(uint16_t *)(o + 2));
303 int algo = *(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 uint16_t proto = 0;
817
818 if (l > 4)
819 {
820 proto = *(p+4);
821 if (l > 5 && !(proto & 1))
822 {
823 proto <<= 8;
824 proto |= *(p+5);
825 }
826 }
827
828 if (proto == PPPIPV6CP)
829 {
830 LOG(3, s, t, "IPv6 rejected\n");
831 change_state(s, ipv6cp, Closed);
832 }
833 else
834 {
835 LOG(3, s, t, "LCP protocol reject: 0x%04X\n", proto);
836 }
837 }
838 else if (*p == EchoReq)
839 {
840 *p = EchoReply; // reply
841 *(uint32_t *) (p + 4) = htonl(session[s].magic); // our magic number
842 q = makeppp(b, sizeof(b), p, l, s, t, PPPLCP);
843 if (!q) return;
844
845 LOG(4, s, t, "LCP: send %s\n", ppp_code(*q));
846 if (config->debug > 3) dumplcp(q, l);
847
848 tunnelsend(b, l + (q - b), t); // send it
849 }
850 else if (*p == EchoReply)
851 {
852 // Ignore it, last_packet time is set earlier than this.
853 }
854 else
855 {
856 int code = *p;
857 int mru = session[s].mru;
858 if (!mru)
859 mru = DEFAULT_MRU;
860
861 if (l > mru) l = mru;
862
863 *p = CodeRej;
864 q = makeppp(b, sizeof(b), p, l, s, t, PPPLCP);
865 if (!q) return;
866
867 LOG(2, s, t, "Unexpected LCP code %s\n", ppp_code(code));
868 LOG(3, s, t, "LCP: send %s\n", ppp_code(*q));
869 if (config->debug > 3) dumplcp(q, l);
870
871 tunnelsend(b, l + (q - b), t);
872 }
873 }
874
875 static void ipcp_open(sessionidt s, tunnelidt t)
876 {
877 LOG(3, s, t, "IPCP: Opened, session is now active\n");
878
879 change_state(s, ipcp, Opened);
880
881 if (!session[s].walled_garden)
882 {
883 uint16_t r = radiusnew(s);
884 if (r)
885 radiussend(r, RADIUSSTART); // send radius start
886 }
887
888 // start IPv6 if configured and still in passive state
889 if (session[s].ppp.ipv6cp == Stopped)
890 {
891 sendipv6cp(s, t);
892 change_state(s, ipv6cp, RequestSent);
893 }
894 }
895
896 // Process IPCP messages
897 void processipcp(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
898 {
899 uint8_t b[MAXCONTROL];
900 uint8_t *q = 0;
901 uint16_t hl;
902
903 CSTAT(processipcp);
904
905 LOG_HEX(5, "IPCP", p, l);
906 if (l < 5)
907 {
908 LOG(1, s, t, "Short IPCP %d bytes\n", l);
909 STAT(tunnel_rx_errors);
910 return ;
911 }
912
913 if ((hl = ntohs(*(uint16_t *) (p + 2))) > l)
914 {
915 LOG(1, s, t, "Length mismatch IPCP %u/%u\n", hl, l);
916 STAT(tunnel_rx_errors);
917 return ;
918 }
919 l = hl;
920
921 if (session[s].ppp.phase < Network)
922 {
923 LOG(2, s, t, "IPCP %s ignored in %s phase\n", ppp_code(*p), ppp_phase(session[s].ppp.phase));
924 return;
925 }
926
927 LOG(3, s, t, "IPCP: recv %s\n", ppp_code(*p));
928
929 if (*p == ConfigAck)
930 {
931 switch (session[s].ppp.ipcp)
932 {
933 case RequestSent:
934 initialise_restart_count(s, ipcp);
935 change_state(s, ipcp, AckReceived);
936 break;
937
938 case AckReceived:
939 case Opened:
940 LOG(2, s, t, "IPCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session[s].ppp.ipcp));
941 sendipcp(s, t);
942 change_state(s, ipcp, RequestSent);
943 break;
944
945 case AckSent:
946 ipcp_open(s, t);
947 break;
948
949 default:
950 LOG(2, s, t, "IPCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ipcp));
951 }
952 }
953 else if (*p == ConfigReq)
954 {
955 uint8_t *response = 0;
956 uint8_t *o = p + 4;
957 int length = l - 4;
958 int gotip = 0;
959 in_addr_t addr;
960
961 while (length > 2)
962 {
963 switch (*o)
964 {
965 case 3: // ip address
966 gotip++; // seen address
967 if (o[1] != 6 || o[1] > length) return;
968
969 addr = htonl(session[s].ip);
970 if (memcmp(o + 2, &addr, (sizeof addr)))
971 {
972 uint8_t *oq = q;
973 q = ppp_nak(s, b, sizeof(b), PPPIPCP, &response, q, p, o, (uint8_t *) &addr, sizeof(addr));
974 if (!q || (q != oq && *response == ConfigRej))
975 {
976 sessionshutdown(s, "Can't negotiate IPCP.", 3, 0);
977 return;
978 }
979 }
980
981 break;
982
983 case 129: // primary DNS
984 if (o[1] != 6 || o[1] > length) return;
985
986 addr = htonl(session[s].dns1);
987 if (memcmp(o + 2, &addr, (sizeof addr)))
988 {
989 q = ppp_nak(s, b, sizeof(b), PPPIPCP, &response, q, p, o, (uint8_t *) &addr, sizeof(addr));
990 if (!q) return;
991 }
992
993 break;
994
995 case 131: // secondary DNS
996 if (o[1] != 6 || o[1] > length) return;
997
998 addr = htonl(session[s].dns1);
999 if (memcmp(o + 2, &addr, sizeof(addr)))
1000 {
1001 q = ppp_nak(s, b, sizeof(b), PPPIPCP, &response, q, p, o, (uint8_t *) &addr, sizeof(addr));
1002 if (!q) return;
1003 }
1004
1005 break;
1006
1007 default:
1008 LOG(2, s, t, " Rejecting PPP IPCP Option type %d\n", *o);
1009 q = ppp_rej(s, b, sizeof(b), PPPIPCP, &response, q, p, o);
1010 if (!q) return;
1011 }
1012
1013 length -= o[1];
1014 o += o[1];
1015 }
1016
1017 if (response)
1018 {
1019 l = q - response; // IPCP packet length
1020 *((uint16_t *) (response + 2)) = htons(l); // update header
1021 }
1022 else if (gotip)
1023 {
1024 // Send packet back as ConfigAck
1025 response = makeppp(b, sizeof(b), p, l, s, t, PPPIPCP);
1026 if (!response) return;
1027 *response = ConfigAck;
1028 }
1029 else
1030 {
1031 LOG(1, s, t, "No IP in IPCP request\n");
1032 STAT(tunnel_rx_errors);
1033 return;
1034 }
1035
1036 switch (session[s].ppp.ipcp)
1037 {
1038 case Closed:
1039 response = makeppp(b, sizeof(b), p, 2, s, t, PPPIPCP);
1040 if (!response) return;
1041 *response = TerminateAck;
1042 *((uint16_t *) (response + 2)) = htons(l = 4);
1043 break;
1044
1045 case Stopped:
1046 initialise_restart_count(s, ipcp);
1047 sendipcp(s, t);
1048 if (*response == ConfigAck)
1049 change_state(s, ipcp, AckSent);
1050 else
1051 change_state(s, ipcp, RequestSent);
1052
1053 break;
1054
1055 case RequestSent:
1056 if (*response == ConfigAck)
1057 change_state(s, ipcp, AckSent);
1058
1059 break;
1060
1061 case AckReceived:
1062 if (*response == ConfigAck)
1063 ipcp_open(s, t);
1064
1065 break;
1066
1067 case Opened:
1068 initialise_restart_count(s, ipcp);
1069 sendipcp(s, t);
1070 /* fallthrough */
1071
1072 case AckSent:
1073 if (*response == ConfigAck)
1074 change_state(s, ipcp, AckSent);
1075 else
1076 change_state(s, ipcp, RequestSent);
1077
1078 break;
1079
1080 default:
1081 LOG(2, s, t, "IPCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ipcp));
1082 return;
1083 }
1084
1085 LOG(3, s, t, "IPCP: send %s\n", ppp_code(*response));
1086 tunnelsend(b, l + (response - b), t);
1087 }
1088 else if (*p == TerminateReq)
1089 {
1090 *p = TerminateAck;
1091 q = makeppp(b, sizeof(b), p, l, s, t, PPPIPCP);
1092 if (!q) return;
1093 LOG(3, s, t, "IPCP: send %s\n", ppp_code(*q));
1094 tunnelsend(b, l + (q - b), t);
1095 change_state(s, ipcp, Stopped);
1096 }
1097 else
1098 {
1099 int code = *p;
1100 int mru = session[s].mru;
1101 if (!mru)
1102 mru = DEFAULT_MRU;
1103
1104 if (l > mru) l = mru;
1105
1106 *p = CodeRej;
1107 q = makeppp(b, sizeof(b), p, l, s, t, PPPIPCP);
1108 if (!q) return;
1109
1110 LOG(2, s, t, "Unexpected IPCP code %s\n", ppp_code(code));
1111 LOG(3, s, t, "IPCP: send %s\n", ppp_code(*q));
1112 tunnelsend(b, l + (q - b), t);
1113 }
1114 }
1115
1116 static void ipv6cp_open(sessionidt s, tunnelidt t)
1117 {
1118 LOG(3, s, t, "IPV6CP: Opened\n");
1119
1120 change_state(s, ipv6cp, Opened);
1121 if (session[s].ipv6prefixlen)
1122 route6set(s, session[s].ipv6route, session[s].ipv6prefixlen, 1);
1123
1124 // Send an initial RA (TODO: Should we send these regularly?)
1125 send_ipv6_ra(s, t, NULL);
1126 }
1127
1128 // Process IPV6CP messages
1129 void processipv6cp(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
1130 {
1131 uint8_t b[MAXCONTROL];
1132 uint8_t *q = 0;
1133 uint16_t hl;
1134
1135 CSTAT(processipv6cp);
1136
1137 LOG_HEX(5, "IPV6CP", p, l);
1138 if (l < 4)
1139 {
1140 LOG(1, s, t, "Short IPV6CP %d bytes\n", l);
1141 STAT(tunnel_rx_errors);
1142 return ;
1143 }
1144
1145 if ((hl = ntohs(*(uint16_t *) (p + 2))) > l)
1146 {
1147 LOG(1, s, t, "Length mismatch IPV6CP %u/%u\n", hl, l);
1148 STAT(tunnel_rx_errors);
1149 return ;
1150 }
1151 l = hl;
1152
1153 if (session[s].ppp.phase < Network)
1154 {
1155 LOG(2, s, t, "IPV6CP %s ignored in %s phase\n", ppp_code(*p), ppp_phase(session[s].ppp.phase));
1156 return;
1157 }
1158
1159 LOG(3, s, t, "IPV6CP: recv %s\n", ppp_code(*p));
1160
1161 if (!config->ipv6_prefix.s6_addr[0])
1162 {
1163 LOG(2, s, t, "IPV6CP: %s rejected (not configured)\n", ppp_code(*p));
1164 *p = ProtocolRej;
1165 q = makeppp(b, sizeof(b), p, l, s, t, PPPIPV6CP);
1166 if (!q) return;
1167 tunnelsend(b, l + (q - b), t);
1168 return;
1169 }
1170
1171 if (!session[s].ip)
1172 {
1173 LOG(3, s, t, "IPV6CP: no IPv4 address (IPCP in state %s)\n", ppp_state(session[s].ppp.ipcp));
1174 return; // need IPCP to complete...
1175 }
1176
1177 if (*p == ConfigAck)
1178 {
1179 switch (session[s].ppp.ipv6cp)
1180 {
1181 case RequestSent:
1182 initialise_restart_count(s, ipv6cp);
1183 change_state(s, ipv6cp, AckReceived);
1184 break;
1185
1186 case AckReceived:
1187 case Opened:
1188 LOG(2, s, t, "IPV6CP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session[s].ppp.ipv6cp));
1189 sendipv6cp(s, t);
1190 change_state(s, ipv6cp, RequestSent);
1191 break;
1192
1193 case AckSent:
1194 ipv6cp_open(s, t);
1195 break;
1196
1197 default:
1198 LOG(2, s, t, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ipv6cp));
1199 }
1200 }
1201 else if (*p == ConfigReq)
1202 {
1203 uint8_t *response = 0;
1204 uint8_t *o = p + 4;
1205 int length = l - 4;
1206 int gotip = 0;
1207 uint8_t ident[8];
1208
1209 while (length > 2)
1210 {
1211 switch (*o)
1212 {
1213 case 1: // interface identifier
1214 gotip++; // seen address
1215 if (o[1] != 10 || o[1] > length) return;
1216
1217 *(uint32_t *) ident = htonl(session[s].ip);
1218 *(uint32_t *) (ident + 4) = 0;
1219
1220 if (memcmp(o + 2, ident, sizeof(ident)))
1221 {
1222 q = ppp_nak(s, b, sizeof(b), PPPIPV6CP, &response, q, p, o, ident, sizeof(ident));
1223 if (!q) return;
1224 }
1225
1226 break;
1227
1228 default:
1229 LOG(2, s, t, " Rejecting PPP IPV6CP Option type %d\n", *o);
1230 q = ppp_rej(s, b, sizeof(b), PPPIPV6CP, &response, q, p, o);
1231 if (!q) return;
1232 }
1233
1234 length -= o[1];
1235 o += o[1];
1236 }
1237
1238 if (response)
1239 {
1240 l = q - response; // IPV6CP packet length
1241 *((uint16_t *) (response + 2)) = htons(l); // update header
1242 }
1243 else if (gotip)
1244 {
1245 // Send packet back as ConfigAck
1246 response = makeppp(b, sizeof(b), p, l, s, t, PPPIPV6CP);
1247 if (!response) return;
1248 *response = ConfigAck;
1249 }
1250 else
1251 {
1252 LOG(1, s, t, "No interface identifier in IPV6CP request\n");
1253 STAT(tunnel_rx_errors);
1254 return;
1255 }
1256
1257 switch (session[s].ppp.ipv6cp)
1258 {
1259 case Closed:
1260 response = makeppp(b, sizeof(b), p, 2, s, t, PPPIPV6CP);
1261 if (!response) return;
1262 *response = TerminateAck;
1263 *((uint16_t *) (response + 2)) = htons(l = 4);
1264 break;
1265
1266 case Stopped:
1267 initialise_restart_count(s, ipv6cp);
1268 sendipv6cp(s, t);
1269 if (*response == ConfigAck)
1270 change_state(s, ipv6cp, AckSent);
1271 else
1272 change_state(s, ipv6cp, RequestSent);
1273
1274 break;
1275
1276 case RequestSent:
1277 if (*response == ConfigAck)
1278 change_state(s, ipv6cp, AckSent);
1279
1280 break;
1281
1282 case AckReceived:
1283 if (*response == ConfigAck)
1284 ipv6cp_open(s, t);
1285
1286 break;
1287
1288 case Opened:
1289 initialise_restart_count(s, ipv6cp);
1290 sendipv6cp(s, t);
1291 /* fallthrough */
1292
1293 case AckSent:
1294 if (*response == ConfigAck)
1295 change_state(s, ipv6cp, AckSent);
1296 else
1297 change_state(s, ipv6cp, RequestSent);
1298
1299 break;
1300
1301 default:
1302 LOG(2, s, t, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ipv6cp));
1303 return;
1304 }
1305
1306 LOG(3, s, t, "IPV6CP: send %s\n", ppp_code(*response));
1307 tunnelsend(b, l + (response - b), t);
1308 }
1309 else if (*p == TerminateReq)
1310 {
1311 *p = TerminateAck;
1312 q = makeppp(b, sizeof(b), p, l, s, t, PPPIPV6CP);
1313 if (!q) return;
1314 LOG(3, s, t, "IPV6CP: send %s\n", ppp_code(*q));
1315 tunnelsend(b, l + (q - b), t);
1316 change_state(s, ipv6cp, Stopped);
1317 }
1318 else
1319 {
1320 int code = *p;
1321 int mru = session[s].mru;
1322 if (!mru)
1323 mru = DEFAULT_MRU;
1324
1325 if (l > mru) l = mru;
1326
1327 *p = CodeRej;
1328 q = makeppp(b, sizeof(b), p, l, s, t, PPPIPV6CP);
1329 if (!q) return;
1330
1331 LOG(2, s, t, "Unexpected IPV6CP code %s\n", ppp_code(code));
1332 LOG(3, s, t, "IPV6CP: send %s\n", ppp_code(*q));
1333 tunnelsend(b, l + (q - b), t);
1334 }
1335 }
1336
1337 // process IP packet received
1338 //
1339 // This MUST be called with at least 4 byte behind 'p'.
1340 // (i.e. this routine writes to p[-4]).
1341 void processipin(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
1342 {
1343 in_addr_t ip;
1344
1345 CSTAT(processipin);
1346
1347 LOG_HEX(5, "IP", p, l);
1348
1349 ip = ntohl(*(uint32_t *)(p + 12));
1350
1351 if (l > MAXETHER)
1352 {
1353 LOG(1, s, t, "IP packet too long %d\n", l);
1354 STAT(tunnel_rx_errors);
1355 return ;
1356 }
1357
1358 if (session[s].ppp.phase != Network || session[s].ppp.ipcp != Opened)
1359 return;
1360
1361 // no spoof (do sessionbyip to handled statically routed subnets)
1362 if (ip != session[s].ip && sessionbyip(htonl(ip)) != s)
1363 {
1364 LOG(5, s, t, "Dropping packet with spoofed IP %s\n", fmtaddr(htonl(ip), 0));
1365 return;
1366 }
1367
1368 // run access-list if any
1369 if (session[s].filter_in && !ip_filter(p, l, session[s].filter_in - 1))
1370 return;
1371
1372 // Add on the tun header
1373 p -= 4;
1374 *(uint32_t *) p = htonl(PKTIP);
1375 l += 4;
1376
1377 // Are we throttled and a slave?
1378 if (session[s].tbf_in && !config->cluster_iam_master) {
1379 // Pass it to the master for handling.
1380 master_throttle_packet(session[s].tbf_in, p, l);
1381 return;
1382 }
1383
1384 // Are we throttled and a master??
1385 if (session[s].tbf_in && config->cluster_iam_master) {
1386 // Actually handle the throttled packets.
1387 tbf_queue_packet(session[s].tbf_in, p, l);
1388 return;
1389 }
1390
1391 // send to ethernet
1392 if (tun_write(p, l) < 0)
1393 {
1394 STAT(tun_tx_errors);
1395 LOG(0, s, t, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1396 l, strerror(errno), tunfd, p);
1397
1398 return;
1399 }
1400
1401 p += 4;
1402 l -= 4;
1403
1404 if (session[s].snoop_ip && session[s].snoop_port)
1405 {
1406 // Snooping this session
1407 snoop_send_packet(p, l, session[s].snoop_ip, session[s].snoop_port);
1408 }
1409
1410 increment_counter(&session[s].cin, &session[s].cin_wrap, l);
1411 session[s].cin_delta += l;
1412 session[s].pin++;
1413
1414 sess_local[s].cin += l;
1415 sess_local[s].pin++;
1416
1417 eth_tx += l;
1418
1419 STAT(tun_tx_packets);
1420 INC_STAT(tun_tx_bytes, l);
1421 }
1422
1423 // process IPv6 packet received
1424 //
1425 // This MUST be called with at least 4 byte behind 'p'.
1426 // (i.e. this routine writes to p[-4]).
1427 void processipv6in(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
1428 {
1429 struct in6_addr ip;
1430 in_addr_t ipv4;
1431
1432 CSTAT(processipv6in);
1433
1434 LOG_HEX(5, "IPv6", p, l);
1435
1436 ip = *(struct in6_addr *) (p + 8);
1437 ipv4 = ntohl(*(uint32_t *)(p + 16));
1438
1439 if (l > MAXETHER)
1440 {
1441 LOG(1, s, t, "IP packet too long %d\n", l);
1442 STAT(tunnel_rx_errors);
1443 return ;
1444 }
1445
1446 if (session[s].ppp.phase != Network || session[s].ppp.ipv6cp != Opened)
1447 return;
1448
1449 // no spoof
1450 if (ipv4 != session[s].ip && memcmp(&config->ipv6_prefix, &ip, 8) && sessionbyipv6(ip) != s)
1451 {
1452 char str[INET6_ADDRSTRLEN];
1453 LOG(5, s, t, "Dropping packet with spoofed IP %s\n",
1454 inet_ntop(AF_INET6, &ip, str, INET6_ADDRSTRLEN));
1455 return;
1456 }
1457
1458 // Check if it's a Router Solicition message.
1459 if (*(p + 6) == 58 && *(p + 7) == 255 && *(p + 24) == 0xFF && *(p + 25) == 2 &&
1460 *(uint32_t *)(p + 26) == 0 && *(uint32_t *)(p + 30) == 0 &&
1461 *(uint32_t *)(p + 34) == 0 &&
1462 *(p + 38) == 0 && *(p + 39) == 2 && *(p + 40) == 133) {
1463 LOG(3, s, t, "Got IPv6 RS\n");
1464 send_ipv6_ra(s, t, &ip);
1465 return;
1466 }
1467
1468 // Add on the tun header
1469 p -= 4;
1470 *(uint32_t *) p = htonl(PKTIPV6);
1471 l += 4;
1472
1473 // Are we throttled and a slave?
1474 if (session[s].tbf_in && !config->cluster_iam_master) {
1475 // Pass it to the master for handling.
1476 master_throttle_packet(session[s].tbf_in, p, l);
1477 return;
1478 }
1479
1480 // Are we throttled and a master??
1481 if (session[s].tbf_in && config->cluster_iam_master) {
1482 // Actually handle the throttled packets.
1483 tbf_queue_packet(session[s].tbf_in, p, l);
1484 return;
1485 }
1486
1487 // send to ethernet
1488 if (tun_write(p, l) < 0)
1489 {
1490 STAT(tun_tx_errors);
1491 LOG(0, s, t, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1492 l, strerror(errno), tunfd, p);
1493
1494 return;
1495 }
1496
1497 p += 4;
1498 l -= 4;
1499
1500 if (session[s].snoop_ip && session[s].snoop_port)
1501 {
1502 // Snooping this session
1503 snoop_send_packet(p, l, session[s].snoop_ip, session[s].snoop_port);
1504 }
1505
1506 increment_counter(&session[s].cin, &session[s].cin_wrap, l);
1507 session[s].cin_delta += l;
1508 session[s].pin++;
1509
1510 sess_local[s].cin += l;
1511 sess_local[s].pin++;
1512
1513 eth_tx += l;
1514
1515 STAT(tun_tx_packets);
1516 INC_STAT(tun_tx_bytes, l);
1517 }
1518
1519 //
1520 // Helper routine for the TBF filters.
1521 // Used to send queued data in from the user.
1522 //
1523 void send_ipin(sessionidt s, uint8_t *buf, int len)
1524 {
1525 LOG_HEX(5, "IP in throttled", buf, len);
1526
1527 if (write(tunfd, buf, len) < 0)
1528 {
1529 STAT(tun_tx_errors);
1530 LOG(0, 0, 0, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1531 len, strerror(errno), tunfd, buf);
1532
1533 return;
1534 }
1535
1536 buf += 4;
1537 len -= 4;
1538
1539 if (session[s].snoop_ip && session[s].snoop_port)
1540 {
1541 // Snooping this session
1542 snoop_send_packet(buf, len, session[s].snoop_ip, session[s].snoop_port);
1543 }
1544
1545 // Increment packet counters
1546 increment_counter(&session[s].cin, &session[s].cin_wrap, len);
1547 session[s].cin_delta += len;
1548 session[s].pin++;
1549
1550 sess_local[s].cin += len;
1551 sess_local[s].pin++;
1552
1553 eth_tx += len;
1554
1555 STAT(tun_tx_packets);
1556 INC_STAT(tun_tx_bytes, len - 4);
1557 }
1558
1559
1560 // Process CCP messages
1561 void processccp(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
1562 {
1563 uint8_t b[MAXCONTROL];
1564 uint8_t *q;
1565
1566 CSTAT(processccp);
1567
1568 LOG_HEX(5, "CCP", p, l);
1569
1570 if (session[s].ppp.phase < Network)
1571 {
1572 LOG(2, s, t, "CCP %s ignored in %s phase\n", ppp_code(*p), ppp_phase(session[s].ppp.phase));
1573 return;
1574 }
1575
1576 if (l < 1)
1577 {
1578 LOG(1, s, t, "Short CCP packet\n");
1579 STAT(tunnel_rx_errors);
1580 }
1581
1582 LOG(3, s, t, "CCP: recv %s\n", ppp_code(*p));
1583 if (*p == ConfigAck)
1584 {
1585 switch (session[s].ppp.ccp)
1586 {
1587 case RequestSent:
1588 initialise_restart_count(s, ccp);
1589 change_state(s, ccp, AckReceived);
1590 break;
1591
1592 case AckReceived:
1593 case Opened:
1594 LOG(2, s, t, "CCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session[s].ppp.ccp));
1595 sendccp(s, t);
1596 change_state(s, ccp, RequestSent);
1597 break;
1598
1599 case AckSent:
1600 LOG(3, s, t, "CCP: Opened\n");
1601 change_state(s, ccp, Opened);
1602 break;
1603
1604 default:
1605 LOG(2, s, t, "CCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ccp));
1606 }
1607 }
1608 else if (*p == ConfigReq)
1609 {
1610 if (l < 6) // accept no compression
1611 *p = ConfigAck;
1612 else // compression requested--reject
1613 *p = ConfigRej;
1614
1615 q = makeppp(b, sizeof(b), p, l, s, t, PPPCCP);
1616 if (!q) return;
1617
1618 switch (session[s].ppp.ccp)
1619 {
1620 case Closed:
1621 q = makeppp(b, sizeof(b), p, 2, s, t, PPPCCP);
1622 if (!q) return;
1623 *q = TerminateAck;
1624 *((uint16_t *) (q + 2)) = htons(l = 4);
1625 break;
1626
1627 case Stopped:
1628 initialise_restart_count(s, ccp);
1629 sendccp(s, t);
1630 if (*q == ConfigAck)
1631 change_state(s, ccp, AckSent);
1632 else
1633 change_state(s, ccp, RequestSent);
1634
1635 break;
1636
1637 case RequestSent:
1638 if (*q == ConfigAck)
1639 change_state(s, ccp, AckSent);
1640
1641 break;
1642
1643 case AckReceived:
1644 if (*q == ConfigAck)
1645 change_state(s, ccp, Opened);
1646
1647 break;
1648
1649 case Opened:
1650 initialise_restart_count(s, ccp);
1651 sendccp(s, t);
1652 /* fallthrough */
1653
1654 case AckSent:
1655 if (*q == ConfigAck)
1656 change_state(s, ccp, AckSent);
1657 else
1658 change_state(s, ccp, RequestSent);
1659
1660 break;
1661
1662 default:
1663 LOG(2, s, t, "CCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ccp));
1664 return;
1665 }
1666
1667 LOG(3, s, t, "CCP: send %s\n", ppp_code(*q));
1668 tunnelsend(b, l + (q - b), t);
1669 }
1670 else if (*p == TerminateReq)
1671 {
1672 *p = TerminateAck;
1673 q = makeppp(b, sizeof(b), p, l, s, t, PPPCCP);
1674 if (!q) return;
1675 LOG(3, s, t, "CCP: send %s\n", ppp_code(*q));
1676 tunnelsend(b, l + (q - b), t);
1677 change_state(s, ccp, Stopped);
1678 }
1679 else
1680 {
1681 int code = *p;
1682 int mru = session[s].mru;
1683 if (!mru)
1684 mru = DEFAULT_MRU;
1685
1686 if (l > mru) l = mru;
1687
1688 *p = CodeRej;
1689 q = makeppp(b, sizeof(b), p, l, s, t, PPPCCP);
1690 if (!q) return;
1691
1692 LOG(2, s, t, "Unexpected CCP code %s\n", ppp_code(code));
1693 LOG(3, s, t, "CCP: send %s\n", ppp_code(*q));
1694 tunnelsend(b, l + (q - b), t);
1695 }
1696 }
1697
1698 // send a CHAP challenge
1699 void sendchap(sessionidt s, tunnelidt t)
1700 {
1701 uint8_t b[MAXCONTROL];
1702 uint16_t r;
1703 uint8_t *q;
1704
1705 CSTAT(sendchap);
1706
1707 r = radiusnew(s);
1708 if (!r)
1709 {
1710 LOG(1, s, t, "No RADIUS to send challenge\n");
1711 STAT(tunnel_tx_errors);
1712 return;
1713 }
1714
1715 LOG(1, s, t, "Send CHAP challenge\n");
1716
1717 radius[r].chap = 1; // CHAP not PAP
1718 radius[r].id++;
1719 if (radius[r].state != RADIUSCHAP)
1720 radius[r].try = 0;
1721
1722 radius[r].state = RADIUSCHAP;
1723 radius[r].retry = backoff(radius[r].try++);
1724 if (radius[r].try > 5)
1725 {
1726 sessionshutdown(s, "CHAP timeout.", 3, 0);
1727 STAT(tunnel_tx_errors);
1728 return ;
1729 }
1730 q = makeppp(b, sizeof(b), 0, 0, s, t, PPPCHAP);
1731 if (!q) return;
1732
1733 *q = 1; // challenge
1734 q[1] = radius[r].id; // ID
1735 q[4] = 16; // value size (size of challenge)
1736 memcpy(q + 5, radius[r].auth, 16); // challenge
1737 strcpy((char *) q + 21, hostname); // our name
1738 *(uint16_t *) (q + 2) = htons(strlen(hostname) + 21); // length
1739 tunnelsend(b, strlen(hostname) + 21 + (q - b), t); // send it
1740 }
1741
1742 // fill in a L2TP message with a PPP frame,
1743 // copies existing PPP message and changes magic number if seen
1744 // returns start of PPP frame
1745 uint8_t *makeppp(uint8_t *b, int size, uint8_t *p, int l, sessionidt s, tunnelidt t, uint16_t mtype)
1746 {
1747 if (size < 12) // Need more space than this!!
1748 {
1749 static int backtrace_count = 0;
1750 LOG(0, s, t, "makeppp buffer too small for L2TP header (size=%d)\n", size);
1751 log_backtrace(backtrace_count, 5)
1752 return NULL;
1753 }
1754
1755 *(uint16_t *) (b + 0) = htons(0x0002); // L2TP with no options
1756 *(uint16_t *) (b + 2) = htons(tunnel[t].far); // tunnel
1757 *(uint16_t *) (b + 4) = htons(session[s].far); // session
1758 b += 6;
1759 if (mtype == PPPLCP || !(session[s].l2tp_flags & SESSIONACFC))
1760 {
1761 *(uint16_t *) b = htons(0xFF03); // HDLC header
1762 b += 2;
1763 }
1764 if (mtype < 0x100 && session[s].l2tp_flags & SESSIONPFC)
1765 *b++ = mtype;
1766 else
1767 {
1768 *(uint16_t *) b = htons(mtype);
1769 b += 2;
1770 }
1771
1772 if (l + 12 > size)
1773 {
1774 static int backtrace_count = 0;
1775 LOG(2, s, t, "makeppp would overflow buffer (size=%d, header+payload=%d)\n", size, l + 12);
1776 log_backtrace(backtrace_count, 5)
1777 return NULL;
1778 }
1779
1780 if (p && l)
1781 memcpy(b, p, l);
1782
1783 return b;
1784 }
1785
1786 static int add_lcp_auth(uint8_t *b, int size, int authtype)
1787 {
1788 int len = 0;
1789 if ((authtype == AUTHCHAP && size < 5) || size < 4)
1790 return 0;
1791
1792 *b++ = 3; // Authentication-Protocol
1793 if (authtype == AUTHCHAP)
1794 {
1795 len = *b++ = 5; // length
1796 *(uint16_t *) b = htons(PPPCHAP); b += 2;
1797 *b++ = 5; // MD5
1798 }
1799 else if (authtype == AUTHPAP)
1800 {
1801 len = *b++ = 4; // length
1802 *(uint16_t *) b = htons(PPPPAP); b += 2;
1803 }
1804 else
1805 {
1806 LOG(0, 0, 0, "add_lcp_auth called with unsupported auth type %d\n", authtype);
1807 }
1808
1809 return len;
1810 }
1811
1812 // Send initial LCP ConfigReq for MRU, authentication type and magic no
1813 void sendlcp(sessionidt s, tunnelidt t, int authtype)
1814 {
1815 uint8_t b[500], *q, *l;
1816
1817 if (!(q = makeppp(b, sizeof(b), NULL, 0, s, t, PPPLCP)))
1818 return;
1819
1820 LOG(3, s, t, "LCP: send ConfigReq%s%s%s\n",
1821 authtype ? " (" : "",
1822 authtype ? (authtype == AUTHCHAP ? "CHAP" : "PAP") : "",
1823 authtype ? ")" : "");
1824
1825 if (!session[s].mru)
1826 session[s].mru = DEFAULT_MRU;
1827
1828 l = q;
1829 *l++ = ConfigReq;
1830 *l++ = (time_now % 255) + 1; // ID
1831
1832 l += 2; //Save space for length
1833
1834 *l++ = 1; *l++ = 4; // Maximum-Receive-Unit (length 4)
1835 *(uint16_t *) l = htons(session[s].mru); l += 2;
1836
1837 if (authtype)
1838 l += add_lcp_auth(l, sizeof(b) - (l - b), authtype);
1839
1840 *l++ = 5; *l++ = 6; // Magic-Number (length 6)
1841 *(uint32_t *) l = htonl(session[s].magic);
1842 l += 4;
1843
1844 *(uint16_t *)(q + 2) = htons(l - q); // Length
1845
1846 LOG_HEX(5, "PPPLCP", q, l - q);
1847 if (config->debug > 3) dumplcp(q, l - q);
1848
1849 tunnelsend(b, (l - b), t);
1850 }
1851
1852 // Send CCP request for no compression
1853 void sendccp(sessionidt s, tunnelidt t)
1854 {
1855 uint8_t b[500], *q;
1856
1857 if (!(q = makeppp(b, sizeof(b), NULL, 0, s, t, PPPCCP)))
1858 return;
1859
1860 LOG(3, s, t, "CCP: send ConfigReq (no compression)\n");
1861
1862 *q = ConfigReq;
1863 *(uint8_t *)(q + 1) = (time_now % 255) + 1; // ID
1864 *(uint16_t *)(q + 2) = htons(4); // Length
1865
1866 LOG_HEX(5, "PPPCCP", q, 4);
1867 tunnelsend(b, (q - b) + 4 , t);
1868 }