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