1c97b0cbf134bbe8f248b0e5b226380403821d7e
[l2tpns.git] / ppp.c
1 // L2TPNS PPP Stuff
2
3 char const *cvs_id_ppp = "$Id: ppp.c,v 1.104 2009/12/08 14:49:28 bodea Exp $";
4
5 #include <stdio.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <errno.h>
9 #include <stdlib.h>
10 #include "l2tpns.h"
11 #include "constants.h"
12 #include "plugin.h"
13 #include "util.h"
14 #include "tbf.h"
15 #include "cluster.h"
16
17 extern tunnelt *tunnel;
18 extern bundlet *bundle;
19 extern fragmentationt *frag;
20 extern sessiont *session;
21 extern radiust *radius;
22 extern int tunfd;
23 extern char hostname[];
24 extern uint32_t eth_tx;
25 extern time_t time_now;
26 extern configt *config;
27
28 static int add_lcp_auth(uint8_t *b, int size, int authtype);
29 static bundleidt new_bundle(void);
30 static int epdiscmp(epdist, epdist);
31 static void setepdis(epdist *, epdist);
32 static void ipcp_open(sessionidt s, tunnelidt t);
33
34 static int first_session_in_bundle(sessionidt s)
35 {
36 bundleidt i;
37 for (i = 1; i < MAXBUNDLE; i++)
38 if (bundle[i].state != BUNDLEFREE)
39 if (epdiscmp(session[s].epdis,bundle[i].epdis) && !strcmp(session[s].user, bundle[i].user))
40 return 0;
41 return 1;
42 }
43
44 // Process PAP messages
45 void processpap(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
46 {
47 char user[MAXUSER];
48 char pass[MAXPASS];
49 uint16_t hl;
50 uint16_t r;
51
52 CSTAT(processpap);
53
54 LOG_HEX(5, "PAP", p, l);
55 if (l < 4)
56 {
57 LOG(1, s, t, "Short PAP %u bytes\n", l);
58 STAT(tunnel_rx_errors);
59 sessionshutdown(s, "Short PAP packet.", CDN_ADMIN_DISC, TERM_USER_ERROR);
60 return;
61 }
62
63 if ((hl = ntohs(*(uint16_t *) (p + 2))) > l)
64 {
65 LOG(1, s, t, "Length mismatch PAP %u/%u\n", hl, l);
66 STAT(tunnel_rx_errors);
67 sessionshutdown(s, "PAP length mismatch.", CDN_ADMIN_DISC, TERM_USER_ERROR);
68 return;
69 }
70 l = hl;
71
72 if (*p != 1)
73 {
74 LOG(1, s, t, "Unexpected PAP code %d\n", *p);
75 STAT(tunnel_rx_errors);
76 sessionshutdown(s, "Unexpected PAP code.", CDN_ADMIN_DISC, TERM_USER_ERROR);
77 return;
78 }
79
80 if (session[s].ppp.phase != Authenticate)
81 {
82 LOG(2, s, t, "PAP ignored in %s phase\n", ppp_phase(session[s].ppp.phase));
83 return;
84 }
85
86 {
87 uint8_t *b = p;
88 b += 4;
89 user[0] = pass[0] = 0;
90 if (*b && *b < sizeof(user))
91 {
92 memcpy(user, b + 1, *b);
93 user[*b] = 0;
94 b += 1 + *b;
95 if (*b && *b < sizeof(pass))
96 {
97 memcpy(pass, b + 1, *b);
98 pass[*b] = 0;
99 }
100 }
101 LOG(3, s, t, "PAP login %s/%s\n", user, pass);
102 }
103
104 if (session[s].ip || !(r = radiusnew(s)))
105 {
106 // respond now, either no RADIUS available or already authenticated
107 uint8_t b[MAXETHER];
108 uint8_t id = p[1];
109 uint8_t *p = makeppp(b, sizeof(b), 0, 0, s, t, PPPPAP, 0, 0, 0);
110 if (!p) return;
111
112 if (session[s].ip)
113 *p = 2; // ACK
114 else
115 *p = 3; // cant authorise
116 p[1] = id;
117 *(uint16_t *) (p + 2) = htons(5); // length
118 p[4] = 0; // no message
119 tunnelsend(b, 5 + (p - b), t); // send it
120
121 if (session[s].ip)
122 {
123 LOG(3, s, t, "Already an IP allocated: %s (%d)\n",
124 fmtaddr(htonl(session[s].ip), 0), session[s].ip_pool_index);
125 }
126 else
127 {
128 LOG(1, s, t, "No RADIUS session available to authenticate session...\n");
129 sessionshutdown(s, "No free RADIUS sessions.", CDN_UNAVAILABLE, TERM_SERVICE_UNAVAILABLE);
130 }
131 }
132 else
133 {
134 // Run PRE_AUTH plugins
135 struct param_pre_auth packet = { &tunnel[t], &session[s], strdup(user), strdup(pass), PPPPAP, 1 };
136 run_plugins(PLUGIN_PRE_AUTH, &packet);
137 if (!packet.continue_auth)
138 {
139 LOG(3, s, t, "A plugin rejected PRE_AUTH\n");
140 if (packet.username) free(packet.username);
141 if (packet.password) free(packet.password);
142 return;
143 }
144
145 strncpy(session[s].user, packet.username, sizeof(session[s].user) - 1);
146 strncpy(radius[r].pass, packet.password, sizeof(radius[r].pass) - 1);
147
148 free(packet.username);
149 free(packet.password);
150
151 radius[r].id = p[1];
152 LOG(3, s, t, "Sending login for %s/%s to RADIUS\n", user, pass);
153 if ((session[s].mrru) && (!first_session_in_bundle(s)))
154 radiussend(r, RADIUSJUSTAUTH);
155 else
156 radiussend(r, RADIUSAUTH);
157 }
158 }
159
160 // Process CHAP messages
161 void processchap(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
162 {
163 uint16_t r;
164 uint16_t hl;
165
166 CSTAT(processchap);
167
168 LOG_HEX(5, "CHAP", p, l);
169
170 if (l < 4)
171 {
172 LOG(1, s, t, "Short CHAP %u bytes\n", l);
173 STAT(tunnel_rx_errors);
174 sessionshutdown(s, "Short CHAP packet.", CDN_ADMIN_DISC, TERM_USER_ERROR);
175 return;
176 }
177
178 if ((hl = ntohs(*(uint16_t *) (p + 2))) > l)
179 {
180 LOG(1, s, t, "Length mismatch CHAP %u/%u\n", hl, l);
181 STAT(tunnel_rx_errors);
182 sessionshutdown(s, "CHAP length mismatch.", CDN_ADMIN_DISC, TERM_USER_ERROR);
183 return;
184 }
185 l = hl;
186
187 if (*p != 2)
188 {
189 LOG(1, s, t, "Unexpected CHAP response code %d\n", *p);
190 STAT(tunnel_rx_errors);
191 sessionshutdown(s, "CHAP length mismatch.", CDN_ADMIN_DISC, TERM_USER_ERROR);
192 return;
193 }
194
195 if (session[s].ppp.phase != Authenticate)
196 {
197 LOG(2, s, t, "CHAP ignored in %s phase\n", ppp_phase(session[s].ppp.phase));
198 return;
199 }
200
201 r = sess_local[s].radius;
202 if (!r)
203 {
204 LOG(3, s, t, "Unexpected CHAP message\n");
205
206 // Some modems (Netgear DM602, possibly others) persist in using CHAP even
207 // after ACKing our ConfigReq for PAP.
208 if (sess_local[s].lcp_authtype == AUTHPAP && config->radius_authtypes & AUTHCHAP)
209 {
210 sess_local[s].lcp_authtype = AUTHCHAP;
211 sendchap(s, t);
212 }
213 return;
214 }
215
216 if (p[1] != radius[r].id)
217 {
218 LOG(1, s, t, "Wrong CHAP response ID %d (should be %d) (%d)\n", p[1], radius[r].id, r);
219 STAT(tunnel_rx_errors);
220 sessionshutdown(s, "Unexpected CHAP response ID.", CDN_ADMIN_DISC, TERM_USER_ERROR);
221 return;
222 }
223
224 if (l < 5 || p[4] != 16)
225 {
226 LOG(1, s, t, "Bad CHAP response length %d\n", l < 5 ? -1 : p[4]);
227 STAT(tunnel_rx_errors);
228 sessionshutdown(s, "Bad CHAP response length.", CDN_ADMIN_DISC, TERM_USER_ERROR);
229 return;
230 }
231
232 l -= 5;
233 p += 5;
234 if (l < 16 || l - 16 >= sizeof(session[s].user))
235 {
236 LOG(1, s, t, "CHAP user too long %d\n", l - 16);
237 STAT(tunnel_rx_errors);
238 sessionshutdown(s, "CHAP username too long.", CDN_ADMIN_DISC, TERM_USER_ERROR);
239 return;
240 }
241
242 // Run PRE_AUTH plugins
243 {
244 struct param_pre_auth packet = { &tunnel[t], &session[s], NULL, NULL, PPPCHAP, 1 };
245
246 packet.password = calloc(17, 1);
247 memcpy(packet.password, p, 16);
248
249 p += 16;
250 l -= 16;
251
252 packet.username = calloc(l + 1, 1);
253 memcpy(packet.username, p, l);
254
255 run_plugins(PLUGIN_PRE_AUTH, &packet);
256 if (!packet.continue_auth)
257 {
258 LOG(3, s, t, "A plugin rejected PRE_AUTH\n");
259 if (packet.username) free(packet.username);
260 if (packet.password) free(packet.password);
261 return;
262 }
263
264 strncpy(session[s].user, packet.username, sizeof(session[s].user) - 1);
265 memcpy(radius[r].pass, packet.password, 16);
266
267 free(packet.username);
268 free(packet.password);
269 }
270
271 radius[r].chap = 1;
272 LOG(3, s, t, "CHAP login %s\n", session[s].user);
273 if ((session[s].mrru) && (!first_session_in_bundle(s)))
274 radiussend(r, RADIUSJUSTAUTH);
275 else
276 radiussend(r, RADIUSAUTH);
277 }
278
279 static void dumplcp(uint8_t *p, int l)
280 {
281 int x = l - 4;
282 uint8_t *o = (p + 4);
283
284 LOG_HEX(5, "PPP LCP Packet", p, l);
285 LOG(4, 0, 0, "PPP LCP Packet type %d (%s len %d)\n", *p, ppp_code((int)*p), ntohs( ((uint16_t *) p)[1]) );
286 LOG(4, 0, 0, "Length: %d\n", l);
287 if (*p != ConfigReq && *p != ConfigRej && *p != ConfigAck)
288 return;
289
290 while (x > 2)
291 {
292 int type = o[0];
293 int length = o[1];
294 if (length < 2)
295 {
296 LOG(4, 0, 0, " Option length is %d...\n", length);
297 break;
298 }
299 if (type == 0)
300 {
301 LOG(4, 0, 0, " Option type is 0...\n");
302 x -= length;
303 o += length;
304 continue;
305 }
306 switch (type)
307 {
308 case 1: // Maximum-Receive-Unit
309 if (length == 4)
310 LOG(4, 0, 0, " %s %d\n", ppp_lcp_option(type), ntohs(*(uint16_t *)(o + 2)));
311 else
312 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type), length);
313 break;
314 case 2: // Async-Control-Character-Map
315 if (length == 6)
316 {
317 uint32_t asyncmap = ntohl(*(uint32_t *)(o + 2));
318 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type), asyncmap);
319 }
320 else
321 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type), length);
322 break;
323 case 3: // Authentication-Protocol
324 if (length == 4)
325 {
326 int proto = ntohs(*(uint16_t *)(o + 2));
327 LOG(4, 0, 0, " %s 0x%x (%s)\n", ppp_lcp_option(type), proto,
328 proto == PPPPAP ? "PAP" : "UNSUPPORTED");
329 }
330 else if (length == 5)
331 {
332 int proto = ntohs(*(uint16_t *)(o + 2));
333 int algo = *(o + 4);
334 LOG(4, 0, 0, " %s 0x%x 0x%x (%s)\n", ppp_lcp_option(type), proto, algo,
335 (proto == PPPCHAP && algo == 5) ? "CHAP MD5" : "UNSUPPORTED");
336 }
337 else
338 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type), length);
339 break;
340 case 4: // Quality-Protocol
341 {
342 uint32_t qp = ntohl(*(uint32_t *)(o + 2));
343 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type), qp);
344 }
345 break;
346 case 5: // Magic-Number
347 if (length == 6)
348 {
349 uint32_t magicno = ntohl(*(uint32_t *)(o + 2));
350 LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type), magicno);
351 }
352 else
353 LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type), length);
354 break;
355 case 7: // Protocol-Field-Compression
356 case 8: // Address-And-Control-Field-Compression
357 LOG(4, 0, 0, " %s\n", ppp_lcp_option(type));
358 break;
359 default:
360 LOG(2, 0, 0, " Unknown PPP LCP Option type %d\n", type);
361 break;
362 }
363 x -= length;
364 o += length;
365 }
366 }
367
368 void lcp_open(sessionidt s, tunnelidt t)
369 {
370 // transition to Authentication or Network phase:
371 session[s].ppp.phase = sess_local[s].lcp_authtype ? Authenticate : Network;
372
373 LOG(3, s, t, "LCP: Opened, phase %s\n", ppp_phase(session[s].ppp.phase));
374
375 // LCP now Opened
376 change_state(s, lcp, Opened);
377
378 if (session[s].ppp.phase == Authenticate)
379 {
380 if (sess_local[s].lcp_authtype == AUTHCHAP)
381 sendchap(s, t);
382 }
383 else
384 {
385 if(session[s].bundle == 0 || bundle[session[s].bundle].num_of_links == 1)
386 {
387 // This-Layer-Up
388 sendipcp(s, t);
389 change_state(s, ipcp, RequestSent);
390 // move to passive state for IPv6 (if configured), CCP
391 if (config->ipv6_prefix.s6_addr[0])
392 change_state(s, ipv6cp, Stopped);
393 else
394 change_state(s, ipv6cp, Closed);
395
396 change_state(s, ccp, Stopped);
397 }
398 else
399 {
400 sessionidt first_ses = bundle[session[s].bundle].members[0];
401 LOG(3, s, t, "MPPP: Skipping IPCP negotiation for session:%d, first session of bundle is:%d\n",s,first_ses);
402 ipcp_open(s, t);
403 }
404 }
405 }
406
407 static void lcp_restart(sessionidt s)
408 {
409 session[s].ppp.phase = Establish;
410 // This-Layer-Down
411 change_state(s, ipcp, Dead);
412 change_state(s, ipv6cp, Dead);
413 change_state(s, ccp, Dead);
414 }
415
416 static uint8_t *ppp_conf_rej(sessionidt s, uint8_t *buf, size_t blen, uint16_t mtype,
417 uint8_t **response, uint8_t *queued, uint8_t *packet, uint8_t *option)
418 {
419 if (!*response || **response != ConfigRej)
420 {
421 queued = *response = makeppp(buf, blen, packet, 2, s, session[s].tunnel, mtype, 0, 0, 0);
422 if (!queued)
423 return 0;
424
425 *queued = ConfigRej;
426 queued += 4;
427 }
428
429 if ((queued - buf + option[1]) > blen)
430 {
431 LOG(2, s, session[s].tunnel, "PPP overflow for ConfigRej (proto %u, option %u).\n", mtype, *option);
432 return 0;
433 }
434
435 memcpy(queued, option, option[1]);
436 return queued + option[1];
437 }
438
439 static uint8_t *ppp_conf_nak(sessionidt s, uint8_t *buf, size_t blen, uint16_t mtype,
440 uint8_t **response, uint8_t *queued, uint8_t *packet, uint8_t *option,
441 uint8_t *value, size_t vlen)
442 {
443 int *nak_sent;
444 switch (mtype)
445 {
446 case PPPLCP: nak_sent = &sess_local[s].lcp.nak_sent; break;
447 case PPPIPCP: nak_sent = &sess_local[s].ipcp.nak_sent; break;
448 case PPPIPV6CP: nak_sent = &sess_local[s].ipv6cp.nak_sent; break;
449 default: return 0; // ?
450 }
451
452 if (*response && **response != ConfigNak)
453 {
454 if (*nak_sent < config->ppp_max_failure) // reject queued
455 return queued;
456
457 return ppp_conf_rej(s, buf, blen, mtype, response, 0, packet, option);
458 }
459
460 if (!*response)
461 {
462 if (*nak_sent >= config->ppp_max_failure)
463 return ppp_conf_rej(s, buf, blen, mtype, response, 0, packet, option);
464
465 queued = *response = makeppp(buf, blen, packet, 2, s, session[s].tunnel, mtype, 0, 0, 0);
466 if (!queued)
467 return 0;
468
469 (*nak_sent)++;
470 *queued = ConfigNak;
471 queued += 4;
472 }
473
474 if ((queued - buf + vlen + 2) > blen)
475 {
476 LOG(2, s, session[s].tunnel, "PPP overflow for ConfigNak (proto %u, option %u).\n", mtype, *option);
477 return 0;
478 }
479
480 *queued++ = *option;
481 *queued++ = vlen + 2;
482 memcpy(queued, value, vlen);
483 return queued + vlen;
484 }
485
486 static void ppp_code_rej(sessionidt s, tunnelidt t, uint16_t proto,
487 char *pname, uint8_t *p, uint16_t l, uint8_t *buf, size_t size)
488 {
489 uint8_t *q;
490 int mru = session[s].mru;
491 if (mru < MINMTU) mru = MINMTU;
492 if (mru > size) mru = size;
493
494 l += 4;
495 if (l > mru) l = mru;
496
497 q = makeppp(buf, size, 0, 0, s, t, proto, 0, 0, 0);
498 if (!q) return;
499
500 *q = CodeRej;
501 *(q + 1) = ++sess_local[s].lcp_ident;
502 *(uint16_t *)(q + 2) = htons(l);
503 memcpy(q + 4, p, l - 4);
504
505 LOG(2, s, t, "Unexpected %s code %s\n", pname, ppp_code(*p));
506 LOG(3, s, t, "%s: send %s\n", pname, ppp_code(*q));
507 if (config->debug > 3) dumplcp(q, l);
508
509 tunnelsend(buf, l + (q - buf), t);
510 }
511
512 // Process LCP messages
513 void processlcp(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
514 {
515 uint8_t b[MAXETHER];
516 uint8_t *q = NULL;
517 uint16_t hl;
518
519 CSTAT(processlcp);
520
521 LOG_HEX(5, "LCP", p, l);
522 if (l < 4)
523 {
524 LOG(1, s, t, "Short LCP %d bytes\n", l);
525 STAT(tunnel_rx_errors);
526 return ;
527 }
528
529 if ((hl = ntohs(*(uint16_t *) (p + 2))) > l)
530 {
531 LOG(1, s, t, "Length mismatch LCP %u/%u\n", hl, l);
532 STAT(tunnel_rx_errors);
533 return ;
534 }
535 l = hl;
536
537 if (session[s].die) // going down...
538 return;
539
540 LOG((*p == EchoReq || *p == EchoReply) ? 4 : 3, s, t,
541 "LCP: recv %s\n", ppp_code(*p));
542
543 if (config->debug > 3) dumplcp(p, l);
544
545 if (*p == ConfigAck)
546 {
547 int x = l - 4;
548 uint8_t *o = (p + 4);
549 int authtype = 0;
550
551 while (x > 2)
552 {
553 int type = o[0];
554 int length = o[1];
555
556 if (length == 0 || type == 0 || x < length) break;
557 switch (type)
558 {
559 case 3: // Authentication-Protocol
560 {
561 int proto = ntohs(*(uint16_t *)(o + 2));
562 if (proto == PPPPAP)
563 authtype = AUTHPAP;
564 else if (proto == PPPCHAP && *(o + 4) == 5)
565 authtype = AUTHCHAP;
566 }
567
568 break;
569 }
570 x -= length;
571 o += length;
572 }
573
574 if (!session[s].ip && authtype)
575 sess_local[s].lcp_authtype = authtype;
576
577 switch (session[s].ppp.lcp)
578 {
579 case RequestSent:
580 initialise_restart_count(s, lcp);
581 change_state(s, lcp, AckReceived);
582 break;
583
584 case AckReceived:
585 case Opened:
586 LOG(2, s, t, "LCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session[s].ppp.lcp));
587 if (session[s].ppp.lcp == Opened)
588 lcp_restart(s);
589
590 sendlcp(s, t);
591 change_state(s, lcp, RequestSent);
592 break;
593
594 case AckSent:
595 lcp_open(s, t);
596 break;
597
598 default:
599 LOG(2, s, t, "LCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.lcp));
600 }
601 }
602 else if (*p == ConfigReq)
603 {
604 int x = l - 4;
605 uint8_t *o = (p + 4);
606 uint8_t *response = 0;
607 static uint8_t asyncmap[4] = { 0, 0, 0, 0 }; // all zero
608 static uint8_t authproto[5];
609 int changed = 0;
610
611 while (x > 2)
612 {
613 int type = o[0];
614 int length = o[1];
615
616 if (length == 0 || type == 0 || x < length) break;
617 switch (type)
618 {
619 case 1: // Maximum-Receive-Unit
620 {
621 uint16_t mru = ntohs(*(uint16_t *)(o + 2));
622 if (mru >= MINMTU)
623 {
624 session[s].mru = mru;
625 changed++;
626 break;
627 }
628
629 LOG(3, s, t, " Remote requesting MRU of %u. Rejecting.\n", mru);
630 mru = htons(MRU);
631 q = ppp_conf_nak(s, b, sizeof(b), PPPLCP, &response, q, p, o, (uint8_t *) &mru, sizeof(mru));
632 }
633 break;
634
635 case 2: // Async-Control-Character-Map
636 if (!ntohl(*(uint32_t *)(o + 2))) // all bits zero is OK
637 break;
638
639 LOG(3, s, t, " Remote requesting asyncmap. Rejecting.\n");
640 q = ppp_conf_nak(s, b, sizeof(b), PPPLCP, &response, q, p, o, asyncmap, sizeof(asyncmap));
641 break;
642
643 case 3: // Authentication-Protocol
644 {
645 int proto = ntohs(*(uint16_t *)(o + 2));
646 char proto_name[] = "0x0000";
647 int alen;
648
649 if (proto == PPPPAP)
650 {
651 if (config->radius_authtypes & AUTHPAP)
652 {
653 sess_local[s].lcp_authtype = AUTHPAP;
654 break;
655 }
656
657 strcpy(proto_name, "PAP");
658 }
659 else if (proto == PPPCHAP)
660 {
661 if (config->radius_authtypes & AUTHCHAP
662 && *(o + 4) == 5) // MD5
663 {
664 sess_local[s].lcp_authtype = AUTHCHAP;
665 break;
666 }
667
668 strcpy(proto_name, "CHAP");
669 }
670 else
671 sprintf(proto_name, "%#4.4x", proto);
672
673 LOG(3, s, t, " Remote requesting %s authentication. Rejecting.\n", proto_name);
674
675 alen = add_lcp_auth(authproto, sizeof(authproto), config->radius_authprefer);
676 if (alen < 2) break; // paranoia
677
678 q = ppp_conf_nak(s, b, sizeof(b), PPPLCP, &response, q, p, o, authproto + 2, alen - 2);
679 if (q && *response == ConfigNak &&
680 config->radius_authtypes != config->radius_authprefer)
681 {
682 // alternate type
683 alen = add_lcp_auth(authproto, sizeof(authproto), config->radius_authtypes & ~config->radius_authprefer);
684 if (alen < 2) break;
685 q = ppp_conf_nak(s, b, sizeof(b), PPPLCP, &response, q, p, o, authproto + 2, alen - 2);
686 }
687
688 break;
689 }
690 break;
691
692 case 4: // Quality-Protocol
693 case 5: // Magic-Number
694 case 7: // Protocol-Field-Compression
695 case 8: // Address-And-Control-Field-Compression
696 break;
697
698 case 17: // Multilink Max-Receive-Reconstructed-Unit
699 {
700 uint16_t mrru = ntohs(*(uint16_t *)(o + 2));
701 session[s].mrru = mrru;
702 changed++;
703 LOG(3, s, t, " Received PPP LCP option MRRU: %d\n",mrru);
704 }
705 break;
706
707 case 18: // Multilink Short Sequence Number Header Format
708 {
709 session[s].mssf = 1;
710 changed++;
711 LOG(3, s, t, " Received PPP LCP option MSSN format\n");
712 }
713 break;
714
715 case 19: // Multilink Endpoint Discriminator
716 {
717 uint8_t epdis_class = o[2];
718 int addr;
719
720 session[s].epdis.addr_class = epdis_class;
721 session[s].epdis.length = length - 3;
722 if (session[s].epdis.length > 20)
723 {
724 LOG(1, s, t, "Error: received EndDis Address Length more than 20: %d\n", session[s].epdis.length);
725 session[s].epdis.length = 20;
726 }
727
728 for (addr = 0; addr < session[s].epdis.length; addr++)
729 session[s].epdis.address[addr] = o[3+addr];
730
731 changed++;
732
733 switch (epdis_class)
734 {
735 case LOCALADDR:
736 LOG(3, s, t, " Received PPP LCP option Multilink EndDis Local Address Class: %d\n",epdis_class);
737 break;
738 case IPADDR:
739 LOG(3, s, t, " Received PPP LCP option Multilink EndDis IP Address Class: %d\n",epdis_class);
740 break;
741 case IEEEMACADDR:
742 LOG(3, s, t, " Received PPP LCP option Multilink EndDis IEEE MAC Address Class: %d\n",epdis_class);
743 break;
744 case PPPMAGIC:
745 LOG(3, s, t, " Received PPP LCP option Multilink EndDis PPP Magic No Class: %d\n",epdis_class);
746 break;
747 case PSNDN:
748 LOG(3, s, t, " Received PPP LCP option Multilink EndDis PSND No Class: %d\n",epdis_class);
749 break;
750 default:
751 LOG(3, s, t, " Received PPP LCP option Multilink EndDis NULL Class %d\n",epdis_class);
752 }
753 }
754 break;
755
756 default: // Reject any unknown options
757 LOG(3, s, t, " Rejecting unknown PPP LCP option %d\n", type);
758 q = ppp_conf_rej(s, b, sizeof(b), PPPLCP, &response, q, p, o);
759 }
760 x -= length;
761 o += length;
762 }
763
764 if (changed)
765 cluster_send_session(s);
766
767 if (response)
768 {
769 l = q - response; // LCP packet length
770 *((uint16_t *) (response + 2)) = htons(l); // update header
771 }
772 else
773 {
774 // Send packet back as ConfigAck
775 response = makeppp(b, sizeof(b), p, l, s, t, PPPLCP, 0, 0, 0);
776 if (!response) return;
777 *response = ConfigAck;
778 }
779
780 switch (session[s].ppp.lcp)
781 {
782 case Closed:
783 response = makeppp(b, sizeof(b), p, 2, s, t, PPPLCP, 0, 0, 0);
784 if (!response) return;
785 *response = TerminateAck;
786 *((uint16_t *) (response + 2)) = htons(l = 4);
787 break;
788
789 case Stopped:
790 initialise_restart_count(s, lcp);
791 sendlcp(s, t);
792 if (*response == ConfigAck)
793 change_state(s, lcp, AckSent);
794 else
795 change_state(s, lcp, RequestSent);
796
797 break;
798
799 case RequestSent:
800 if (*response == ConfigAck)
801 change_state(s, lcp, AckSent);
802
803 break;
804
805 case AckReceived:
806 if (*response == ConfigAck)
807 lcp_open(s, t);
808
809 break;
810
811 case Opened:
812 lcp_restart(s);
813 sendlcp(s, t);
814 /* fallthrough */
815
816 case AckSent:
817 if (*response == ConfigAck)
818 change_state(s, lcp, AckSent);
819 else
820 change_state(s, lcp, RequestSent);
821
822 break;
823
824 case Closing:
825 sessionshutdown(s, "LCP: ConfigReq in state Closing. This should not happen. Killing session.", CDN_ADMIN_DISC, TERM_LOST_SERVICE);
826 break;
827
828 default:
829 LOG(2, s, t, "LCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.lcp));
830 return;
831 }
832
833 LOG(3, s, t, "LCP: send %s\n", ppp_code(*response));
834 if (config->debug > 3) dumplcp(response, l);
835
836 tunnelsend(b, l + (response - b), t);
837 }
838 else if (*p == ConfigNak || *p == ConfigRej)
839 {
840 int x = l - 4;
841 uint8_t *o = (p + 4);
842 int authtype = -1;
843
844 while (x > 2)
845 {
846 int type = o[0];
847 int length = o[1];
848
849 if (length == 0 || type == 0 || x < length) break;
850 switch (type)
851 {
852 case 1: // Maximum-Receive-Unit
853 if (*p == ConfigNak)
854 {
855 if (length < 4) break;
856 sess_local[s].ppp_mru = ntohs(*(uint16_t *)(o + 2));
857 LOG(3, s, t, " Remote requested MRU of %u\n", sess_local[s].ppp_mru);
858 }
859 else
860 {
861 sess_local[s].ppp_mru = 0;
862 LOG(3, s, t, " Remote rejected MRU negotiation\n");
863 }
864
865 break;
866
867 case 3: // Authentication-Protocol
868 if (authtype > 0)
869 break;
870
871 if (*p == ConfigNak)
872 {
873 int proto;
874
875 if (length < 4) break;
876 proto = ntohs(*(uint16_t *)(o + 2));
877
878 if (proto == PPPPAP)
879 {
880 authtype = config->radius_authtypes & AUTHPAP;
881 LOG(3, s, t, " Remote requested PAP authentication...%sing\n",
882 authtype ? "accept" : "reject");
883 }
884 else if (proto == PPPCHAP && length > 4 && *(o + 4) == 5)
885 {
886 authtype = config->radius_authtypes & AUTHCHAP;
887 LOG(3, s, t, " Remote requested CHAP authentication...%sing\n",
888 authtype ? "accept" : "reject");
889 }
890 else
891 {
892 LOG(3, s, t, " Rejecting unsupported authentication %#4x\n",
893 proto);
894 }
895 }
896 else
897 {
898 LOG(2, s, t, "LCP: remote rejected auth negotiation\n");
899 authtype = 0; // shutdown
900 }
901
902 break;
903
904 case 5: // Magic-Number
905 session[s].magic = 0;
906 if (*p == ConfigNak)
907 {
908 if (length < 6) break;
909 session[s].magic = ntohl(*(uint32_t *)(o + 2));
910 }
911
912 if (session[s].magic)
913 LOG(3, s, t, " Remote requested magic-no %x\n", session[s].magic);
914 else
915 LOG(3, s, t, " Remote rejected magic-no\n");
916
917 cluster_send_session(s);
918 break;
919
920 case 17: // Multilink Max-Receive-Reconstructed-Unit
921 {
922 if (*p == ConfigNak)
923 {
924 sess_local[s].mp_mrru = ntohs(*(uint16_t *)(o + 2));
925 LOG(3, s, t, " Remote requested MRRU of %u\n", sess_local[s].mp_mrru);
926 }
927 else
928 {
929 sess_local[s].mp_mrru = 0;
930 LOG(3, s, t, " Remote rejected MRRU negotiation\n");
931 }
932 }
933 break;
934
935 case 18: // Multilink Short Sequence Number Header Format
936 {
937 if (*p == ConfigNak)
938 {
939 sess_local[s].mp_mssf = 0;
940 LOG(3, s, t, " Remote requested Naked mssf\n");
941 }
942 else
943 {
944 sess_local[s].mp_mssf = 0;
945 LOG(3, s, t, " Remote rejected mssf\n");
946 }
947 }
948 break;
949
950 case 19: // Multilink Endpoint Discriminator
951 {
952 if (*p == ConfigNak)
953 {
954 LOG(2, s, t, " Remote should not configNak Endpoint Dis!\n");
955 }
956 else
957 {
958 sess_local[s].mp_epdis = 0;
959 LOG(3, s, t, " Remote rejected Endpoint Discriminator\n");
960 }
961 }
962 break;
963
964 default:
965 LOG(2, s, t, "LCP: remote sent %s for type %u?\n", ppp_code(*p), type);
966 sessionshutdown(s, "Unable to negotiate LCP.", CDN_ADMIN_DISC, TERM_USER_ERROR);
967 return;
968 }
969 x -= length;
970 o += length;
971 }
972
973 if (!authtype)
974 {
975 sessionshutdown(s, "Unsupported authentication.", CDN_ADMIN_DISC, TERM_USER_ERROR);
976 return;
977 }
978
979 if (authtype > 0)
980 sess_local[s].lcp_authtype = authtype;
981
982 switch (session[s].ppp.lcp)
983 {
984 case Closed:
985 case Stopped:
986 {
987 uint8_t *response = makeppp(b, sizeof(b), p, 2, s, t, PPPLCP, 0, 0, 0);
988 if (!response) return;
989 *response = TerminateAck;
990 *((uint16_t *) (response + 2)) = htons(l = 4);
991
992 LOG(3, s, t, "LCP: send %s\n", ppp_code(*response));
993 if (config->debug > 3) dumplcp(response, l);
994
995 tunnelsend(b, l + (response - b), t);
996 }
997 break;
998
999 case RequestSent:
1000 case AckSent:
1001 initialise_restart_count(s, lcp);
1002 sendlcp(s, t);
1003 break;
1004
1005 case AckReceived:
1006 LOG(2, s, t, "LCP: ConfigNak in state %s? Sending ConfigReq\n", ppp_state(session[s].ppp.lcp));
1007 sendlcp(s, t);
1008 break;
1009
1010 case Opened:
1011 lcp_restart(s);
1012 sendlcp(s, t);
1013 break;
1014
1015 default:
1016 LOG(2, s, t, "LCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.lcp));
1017 return;
1018 }
1019 }
1020 else if (*p == TerminateReq)
1021 {
1022 switch (session[s].ppp.lcp)
1023 {
1024 case Closed:
1025 case Stopped:
1026 case Closing:
1027 case Stopping:
1028 case RequestSent:
1029 case AckReceived:
1030 case AckSent:
1031 break;
1032
1033 case Opened:
1034 lcp_restart(s);
1035 zero_restart_count(s, lcp);
1036 change_state(s, lcp, Closing);
1037 break;
1038
1039 default:
1040 LOG(2, s, t, "LCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.lcp));
1041 return;
1042 }
1043
1044 *p = TerminateAck; // send ack
1045 q = makeppp(b, sizeof(b), p, l, s, t, PPPLCP, 0, 0, 0);
1046 if (!q) return;
1047
1048 LOG(3, s, t, "LCP: send %s\n", ppp_code(*q));
1049 if (config->debug > 3) dumplcp(q, l);
1050
1051 tunnelsend(b, l + (q - b), t); // send it
1052 }
1053 else if (*p == ProtocolRej)
1054 {
1055 uint16_t proto = 0;
1056
1057 if (l > 4)
1058 {
1059 proto = *(p+4);
1060 if (l > 5 && !(proto & 1))
1061 {
1062 proto <<= 8;
1063 proto |= *(p+5);
1064 }
1065 }
1066
1067 if (proto == PPPIPV6CP)
1068 {
1069 LOG(3, s, t, "IPv6 rejected\n");
1070 change_state(s, ipv6cp, Closed);
1071 }
1072 else
1073 {
1074 LOG(3, s, t, "LCP protocol reject: 0x%04X\n", proto);
1075 }
1076 }
1077 else if (*p == EchoReq)
1078 {
1079 *p = EchoReply; // reply
1080 *(uint32_t *) (p + 4) = htonl(session[s].magic); // our magic number
1081 q = makeppp(b, sizeof(b), p, l, s, t, PPPLCP, 0, 0, 0);
1082 if (!q) return;
1083
1084 LOG(4, s, t, "LCP: send %s\n", ppp_code(*q));
1085 if (config->debug > 3) dumplcp(q, l);
1086
1087 tunnelsend(b, l + (q - b), t); // send it
1088 }
1089 else if (*p == EchoReply)
1090 {
1091 // Ignore it, last_packet time is set earlier than this.
1092 }
1093 else if (*p != CodeRej)
1094 {
1095 ppp_code_rej(s, t, PPPLCP, "LCP", p, l, b, sizeof(b));
1096 }
1097 }
1098
1099 int join_bundle(sessionidt s)
1100 {
1101 // Search for a bundle to join
1102 bundleidt i;
1103 bundleidt b;
1104 for (i = 1; i < MAXBUNDLE; i++)
1105 {
1106 if (bundle[i].state != BUNDLEFREE)
1107 {
1108 if (epdiscmp(session[s].epdis,bundle[i].epdis) && !strcmp(session[s].user, bundle[i].user))
1109 {
1110 sessionidt first_ses = bundle[i].members[0];
1111 if (bundle[i].mssf != session[s].mssf)
1112 {
1113 // uniformity of sequence number format must be insured
1114 LOG(3, s, session[s].tunnel, "MPPP: unable to bundle session %d in bundle %d cause of different mssf\n", s, i);
1115 return -1;
1116 }
1117 session[s].bundle = i;
1118 session[s].ip = session[first_ses].ip;
1119 session[s].dns1 = session[first_ses].dns1;
1120 session[s].dns2 = session[first_ses].dns2;
1121 session[s].timeout = session[first_ses].timeout;
1122
1123 if(session[s].epdis.length > 0)
1124 setepdis(&bundle[i].epdis, session[s].epdis);
1125
1126 strcpy(bundle[i].user, session[s].user);
1127 bundle[i].members[bundle[i].num_of_links] = s;
1128 bundle[i].num_of_links++;
1129 LOG(3, s, session[s].tunnel, "MPPP: Bundling additional line in bundle (%d), lines:%d\n",i,bundle[i].num_of_links);
1130 return i;
1131 }
1132 }
1133 }
1134
1135 // No previously created bundle was found for this session, so create a new one
1136 if (!(b = new_bundle())) return 0;
1137
1138 session[s].bundle = b;
1139 bundle[b].mrru = session[s].mrru;
1140 bundle[b].mssf = session[s].mssf;
1141 // FIXME !!! to enable l2tpns reading mssf frames receiver_max_seq, sender_max_seq must be introduce
1142 // now session[s].mssf flag indecates that the receiver wish to receive frames in mssf, so max_seq (i.e. recv_max_seq) = 1<<24
1143 /*
1144 if (bundle[b].mssf)
1145 bundle[b].max_seq = 1 << 12;
1146 else */
1147 bundle[b].max_seq = 1 << 24;
1148 if(session[s].epdis.length > 0)
1149 setepdis(&bundle[b].epdis, session[s].epdis);
1150
1151 strcpy(bundle[b].user, session[s].user);
1152 bundle[b].members[0] = s;
1153 bundle[b].timeout = session[s].timeout;
1154 LOG(3, s, session[s].tunnel, "MPPP: Created a new bundle (%d)\n", b);
1155 return b;
1156 }
1157
1158 static int epdiscmp(epdist ep1, epdist ep2)
1159 {
1160 int ad;
1161 if (ep1.length != ep2.length)
1162 return 0;
1163
1164 if (ep1.addr_class != ep2.addr_class)
1165 return 0;
1166
1167 for (ad = 0; ad < ep1.length; ad++)
1168 if (ep1.address[ad] != ep2.address[ad])
1169 return 0;
1170
1171 return 1;
1172 }
1173
1174 static void setepdis(epdist *ep1, epdist ep2)
1175 {
1176 int ad;
1177 ep1->length = ep2.length;
1178 ep1->addr_class = ep2.addr_class;
1179 for (ad = 0; ad < ep2.length; ad++)
1180 ep1->address[ad] = ep2.address[ad];
1181 }
1182
1183 static bundleidt new_bundle()
1184 {
1185 bundleidt i;
1186 for (i = 1; i < MAXBUNDLE; i++)
1187 {
1188 if (bundle[i].state == BUNDLEFREE)
1189 {
1190 LOG(4, 0, 0, "MPPP: Assigning bundle ID %d\n", i);
1191 bundle[i].num_of_links = 1;
1192 bundle[i].last_check = time_now; // Initialize last_check value
1193 bundle[i].state = BUNDLEOPEN;
1194 bundle[i].current_ses = -1; // This is to enforce the first session 0 to be used at first
1195 memset(&frag[i], 0, sizeof(fragmentationt));
1196 if (i > config->cluster_highest_bundleid)
1197 config->cluster_highest_bundleid = i;
1198 return i;
1199 }
1200 }
1201 LOG(0, 0, 0, "MPPP: Can't find a free bundle! There shouldn't be this many in use!\n");
1202 return 0;
1203 }
1204
1205 static void ipcp_open(sessionidt s, tunnelidt t)
1206 {
1207 LOG(3, s, t, "IPCP: Opened, session is now active\n");
1208
1209 change_state(s, ipcp, Opened);
1210
1211 if (!(session[s].walled_garden || session[s].flags & SESSION_STARTED))
1212 {
1213 uint16_t r = radiusnew(s);
1214 if (r)
1215 {
1216 radiussend(r, RADIUSSTART); // send radius start
1217
1218 // don't send further Start records if IPCP is restarted
1219 session[s].flags |= SESSION_STARTED;
1220 cluster_send_session(s);
1221 }
1222 }
1223
1224 // start IPv6 if configured and still in passive state
1225 if (session[s].ppp.ipv6cp == Stopped)
1226 {
1227 sendipv6cp(s, t);
1228 change_state(s, ipv6cp, RequestSent);
1229 }
1230 }
1231
1232 // Process IPCP messages
1233 void processipcp(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
1234 {
1235 uint8_t b[MAXETHER];
1236 uint8_t *q = 0;
1237 uint16_t hl;
1238
1239 CSTAT(processipcp);
1240
1241 LOG_HEX(5, "IPCP", p, l);
1242 if (l < 4)
1243 {
1244 LOG(1, s, t, "Short IPCP %d bytes\n", l);
1245 STAT(tunnel_rx_errors);
1246 return ;
1247 }
1248
1249 if ((hl = ntohs(*(uint16_t *) (p + 2))) > l)
1250 {
1251 LOG(1, s, t, "Length mismatch IPCP %u/%u\n", hl, l);
1252 STAT(tunnel_rx_errors);
1253 return ;
1254 }
1255 l = hl;
1256
1257 if (session[s].ppp.phase < Network)
1258 {
1259 LOG(2, s, t, "IPCP %s ignored in %s phase\n", ppp_code(*p), ppp_phase(session[s].ppp.phase));
1260 return;
1261 }
1262
1263 LOG(3, s, t, "IPCP: recv %s\n", ppp_code(*p));
1264
1265 if (*p == ConfigAck)
1266 {
1267 switch (session[s].ppp.ipcp)
1268 {
1269 case RequestSent:
1270 initialise_restart_count(s, ipcp);
1271 change_state(s, ipcp, AckReceived);
1272 break;
1273
1274 case AckReceived:
1275 case Opened:
1276 LOG(2, s, t, "IPCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session[s].ppp.ipcp));
1277 sendipcp(s, t);
1278 change_state(s, ipcp, RequestSent);
1279 break;
1280
1281 case AckSent:
1282 ipcp_open(s, t);
1283 break;
1284
1285 default:
1286 LOG(2, s, t, "IPCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ipcp));
1287 }
1288 }
1289 else if (*p == ConfigReq)
1290 {
1291 uint8_t *response = 0;
1292 uint8_t *o = p + 4;
1293 int length = l - 4;
1294 int gotip = 0;
1295 in_addr_t addr;
1296
1297 while (length > 2)
1298 {
1299 if (!o[1] || o[1] > length) return;
1300
1301 switch (*o)
1302 {
1303 case 3: // ip address
1304 gotip++; // seen address
1305 if (o[1] != 6) return;
1306
1307 addr = htonl(session[s].ip);
1308 if (memcmp(o + 2, &addr, (sizeof addr)))
1309 {
1310 uint8_t *oq = q;
1311 q = ppp_conf_nak(s, b, sizeof(b), PPPIPCP, &response, q, p, o, (uint8_t *) &addr, sizeof(addr));
1312 if (!q || (q != oq && *response == ConfigRej))
1313 {
1314 sessionshutdown(s, "Can't negotiate IPCP.", CDN_ADMIN_DISC, TERM_USER_ERROR);
1315 return;
1316 }
1317 }
1318
1319 break;
1320
1321 case 129: // primary DNS
1322 if (o[1] != 6) return;
1323
1324 addr = htonl(session[s].dns1);
1325 if (memcmp(o + 2, &addr, (sizeof addr)))
1326 {
1327 q = ppp_conf_nak(s, b, sizeof(b), PPPIPCP, &response, q, p, o, (uint8_t *) &addr, sizeof(addr));
1328 if (!q) return;
1329 }
1330
1331 break;
1332
1333 case 131: // secondary DNS
1334 if (o[1] != 6) return;
1335
1336 addr = htonl(session[s].dns2);
1337 if (memcmp(o + 2, &addr, sizeof(addr)))
1338 {
1339 q = ppp_conf_nak(s, b, sizeof(b), PPPIPCP, &response, q, p, o, (uint8_t *) &addr, sizeof(addr));
1340 if (!q) return;
1341 }
1342
1343 break;
1344
1345 default:
1346 LOG(2, s, t, " Rejecting PPP IPCP Option type %d\n", *o);
1347 q = ppp_conf_rej(s, b, sizeof(b), PPPIPCP, &response, q, p, o);
1348 if (!q) return;
1349 }
1350
1351 length -= o[1];
1352 o += o[1];
1353 }
1354
1355 if (response)
1356 {
1357 l = q - response; // IPCP packet length
1358 *((uint16_t *) (response + 2)) = htons(l); // update header
1359 }
1360 else if (gotip)
1361 {
1362 // Send packet back as ConfigAck
1363 response = makeppp(b, sizeof(b), p, l, s, t, PPPIPCP, 0, 0, 0);
1364 if (!response) return;
1365 *response = ConfigAck;
1366 }
1367 else
1368 {
1369 LOG(1, s, t, "No IP in IPCP request\n");
1370 STAT(tunnel_rx_errors);
1371 return;
1372 }
1373
1374 switch (session[s].ppp.ipcp)
1375 {
1376 case Closed:
1377 response = makeppp(b, sizeof(b), p, 2, s, t, PPPIPCP, 0, 0, 0);
1378 if (!response) return;
1379 *response = TerminateAck;
1380 *((uint16_t *) (response + 2)) = htons(l = 4);
1381 break;
1382
1383 case Stopped:
1384 initialise_restart_count(s, ipcp);
1385 sendipcp(s, t);
1386 if (*response == ConfigAck)
1387 change_state(s, ipcp, AckSent);
1388 else
1389 change_state(s, ipcp, RequestSent);
1390
1391 break;
1392
1393 case RequestSent:
1394 if (*response == ConfigAck)
1395 change_state(s, ipcp, AckSent);
1396
1397 break;
1398
1399 case AckReceived:
1400 if (*response == ConfigAck)
1401 ipcp_open(s, t);
1402
1403 break;
1404
1405 case Opened:
1406 initialise_restart_count(s, ipcp);
1407 sendipcp(s, t);
1408 /* fallthrough */
1409
1410 case AckSent:
1411 if (*response == ConfigAck)
1412 change_state(s, ipcp, AckSent);
1413 else
1414 change_state(s, ipcp, RequestSent);
1415
1416 break;
1417
1418 default:
1419 LOG(2, s, t, "IPCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ipcp));
1420 return;
1421 }
1422
1423 LOG(3, s, t, "IPCP: send %s\n", ppp_code(*response));
1424 tunnelsend(b, l + (response - b), t);
1425 }
1426 else if (*p == TerminateReq)
1427 {
1428 switch (session[s].ppp.ipcp)
1429 {
1430 case Closed:
1431 case Stopped:
1432 case Closing:
1433 case Stopping:
1434 case RequestSent:
1435 case AckReceived:
1436 case AckSent:
1437 break;
1438
1439 case Opened:
1440 zero_restart_count(s, ipcp);
1441 change_state(s, ipcp, Closing);
1442 break;
1443
1444 default:
1445 LOG(2, s, t, "IPCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ipcp));
1446 return;
1447 }
1448
1449 *p = TerminateAck; // send ack
1450 q = makeppp(b, sizeof(b), p, l, s, t, PPPIPCP, 0, 0, 0);
1451 if (!q) return;
1452
1453 LOG(3, s, t, "IPCP: send %s\n", ppp_code(*q));
1454 tunnelsend(b, l + (q - b), t); // send it
1455 }
1456 else if (*p != CodeRej)
1457 {
1458 ppp_code_rej(s, t, PPPIPCP, "IPCP", p, l, b, sizeof(b));
1459 }
1460 }
1461
1462 static void ipv6cp_open(sessionidt s, tunnelidt t)
1463 {
1464 LOG(3, s, t, "IPV6CP: Opened\n");
1465
1466 change_state(s, ipv6cp, Opened);
1467 if (session[s].ipv6prefixlen)
1468 route6set(s, session[s].ipv6route, session[s].ipv6prefixlen, 1);
1469
1470 // Send an initial RA (TODO: Should we send these regularly?)
1471 send_ipv6_ra(s, t, NULL);
1472 }
1473
1474 // Process IPV6CP messages
1475 void processipv6cp(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
1476 {
1477 uint8_t b[MAXETHER];
1478 uint8_t *q = 0;
1479 uint16_t hl;
1480
1481 CSTAT(processipv6cp);
1482
1483 LOG_HEX(5, "IPV6CP", p, l);
1484 if (l < 4)
1485 {
1486 LOG(1, s, t, "Short IPV6CP %d bytes\n", l);
1487 STAT(tunnel_rx_errors);
1488 return ;
1489 }
1490
1491 if ((hl = ntohs(*(uint16_t *) (p + 2))) > l)
1492 {
1493 LOG(1, s, t, "Length mismatch IPV6CP %u/%u\n", hl, l);
1494 STAT(tunnel_rx_errors);
1495 return ;
1496 }
1497 l = hl;
1498
1499 if (session[s].ppp.phase < Network)
1500 {
1501 LOG(2, s, t, "IPV6CP %s ignored in %s phase\n", ppp_code(*p), ppp_phase(session[s].ppp.phase));
1502 return;
1503 }
1504
1505 LOG(3, s, t, "IPV6CP: recv %s\n", ppp_code(*p));
1506
1507 if (!session[s].ip)
1508 {
1509 LOG(3, s, t, "IPV6CP: no IPv4 address (IPCP in state %s)\n", ppp_state(session[s].ppp.ipcp));
1510 return; // need IPCP to complete...
1511 }
1512
1513 if (*p == ConfigAck)
1514 {
1515 switch (session[s].ppp.ipv6cp)
1516 {
1517 case RequestSent:
1518 initialise_restart_count(s, ipv6cp);
1519 change_state(s, ipv6cp, AckReceived);
1520 break;
1521
1522 case AckReceived:
1523 case Opened:
1524 LOG(2, s, t, "IPV6CP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session[s].ppp.ipv6cp));
1525 sendipv6cp(s, t);
1526 change_state(s, ipv6cp, RequestSent);
1527 break;
1528
1529 case AckSent:
1530 ipv6cp_open(s, t);
1531 break;
1532
1533 default:
1534 LOG(2, s, t, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ipv6cp));
1535 }
1536 }
1537 else if (*p == ConfigReq)
1538 {
1539 uint8_t *response = 0;
1540 uint8_t *o = p + 4;
1541 int length = l - 4;
1542 int gotip = 0;
1543 uint32_t ident[2];
1544
1545 while (length > 2)
1546 {
1547 if (!o[1] || o[1] > length) return;
1548
1549 switch (*o)
1550 {
1551 case 1: // interface identifier
1552 gotip++; // seen address
1553 if (o[1] != 10) return;
1554
1555 ident[0] = htonl(session[s].ip);
1556 ident[1] = 0;
1557
1558 if (memcmp(o + 2, ident, sizeof(ident)))
1559 {
1560 q = ppp_conf_nak(s, b, sizeof(b), PPPIPV6CP, &response, q, p, o, (uint8_t *)ident, sizeof(ident));
1561 if (!q) return;
1562 }
1563
1564 break;
1565
1566 default:
1567 LOG(2, s, t, " Rejecting PPP IPV6CP Option type %d\n", *o);
1568 q = ppp_conf_rej(s, b, sizeof(b), PPPIPV6CP, &response, q, p, o);
1569 if (!q) return;
1570 }
1571
1572 length -= o[1];
1573 o += o[1];
1574 }
1575
1576 if (response)
1577 {
1578 l = q - response; // IPV6CP packet length
1579 *((uint16_t *) (response + 2)) = htons(l); // update header
1580 }
1581 else if (gotip)
1582 {
1583 // Send packet back as ConfigAck
1584 response = makeppp(b, sizeof(b), p, l, s, t, PPPIPV6CP, 0, 0, 0);
1585 if (!response) return;
1586 *response = ConfigAck;
1587 }
1588 else
1589 {
1590 LOG(1, s, t, "No interface identifier in IPV6CP request\n");
1591 STAT(tunnel_rx_errors);
1592 return;
1593 }
1594
1595 switch (session[s].ppp.ipv6cp)
1596 {
1597 case Closed:
1598 response = makeppp(b, sizeof(b), p, 2, s, t, PPPIPV6CP, 0, 0, 0);
1599 if (!response) return;
1600 *response = TerminateAck;
1601 *((uint16_t *) (response + 2)) = htons(l = 4);
1602 break;
1603
1604 case Stopped:
1605 initialise_restart_count(s, ipv6cp);
1606 sendipv6cp(s, t);
1607 if (*response == ConfigAck)
1608 change_state(s, ipv6cp, AckSent);
1609 else
1610 change_state(s, ipv6cp, RequestSent);
1611
1612 break;
1613
1614 case RequestSent:
1615 if (*response == ConfigAck)
1616 change_state(s, ipv6cp, AckSent);
1617
1618 break;
1619
1620 case AckReceived:
1621 if (*response == ConfigAck)
1622 ipv6cp_open(s, t);
1623
1624 break;
1625
1626 case Opened:
1627 initialise_restart_count(s, ipv6cp);
1628 sendipv6cp(s, t);
1629 /* fallthrough */
1630
1631 case AckSent:
1632 if (*response == ConfigAck)
1633 change_state(s, ipv6cp, AckSent);
1634 else
1635 change_state(s, ipv6cp, RequestSent);
1636
1637 break;
1638
1639 default:
1640 LOG(2, s, t, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ipv6cp));
1641 return;
1642 }
1643
1644 LOG(3, s, t, "IPV6CP: send %s\n", ppp_code(*response));
1645 tunnelsend(b, l + (response - b), t);
1646 }
1647 else if (*p == TerminateReq)
1648 {
1649 switch (session[s].ppp.ipv6cp)
1650 {
1651 case Closed:
1652 case Stopped:
1653 case Closing:
1654 case Stopping:
1655 case RequestSent:
1656 case AckReceived:
1657 case AckSent:
1658 break;
1659
1660 case Opened:
1661 zero_restart_count(s, ipv6cp);
1662 change_state(s, ipv6cp, Closing);
1663 break;
1664
1665 default:
1666 LOG(2, s, t, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ipv6cp));
1667 return;
1668 }
1669
1670 *p = TerminateAck; // send ack
1671 q = makeppp(b, sizeof(b), p, l, s, t, PPPIPV6CP, 0, 0, 0);
1672 if (!q) return;
1673
1674 LOG(3, s, t, "IPV6CP: send %s\n", ppp_code(*q));
1675 tunnelsend(b, l + (q - b), t); // send it
1676 }
1677 else if (*p != CodeRej)
1678 {
1679 ppp_code_rej(s, t, PPPIPV6CP, "IPV6CP", p, l, b, sizeof(b));
1680 }
1681 }
1682
1683 static void update_sessions_in_stat(sessionidt s, uint16_t l)
1684 {
1685 bundleidt b = session[s].bundle;
1686 if (!b)
1687 {
1688 increment_counter(&session[s].cin, &session[s].cin_wrap, l);
1689 session[s].cin_delta += l;
1690 session[s].pin++;
1691
1692 sess_local[s].cin += l;
1693 sess_local[s].pin++;
1694 }
1695 else
1696 {
1697 int i = frag[b].re_frame_begin_index;
1698 int end = frag[b].re_frame_end_index;
1699 for (;;)
1700 {
1701 l = frag[b].fragment[i].length;
1702 s = frag[b].fragment[i].sid;
1703 increment_counter(&session[s].cin, &session[s].cin_wrap, l);
1704 session[s].cin_delta += l;
1705 session[s].pin++;
1706
1707 sess_local[s].cin += l;
1708 sess_local[s].pin++;
1709 if (i == end)
1710 return;
1711 i = (i + 1) & MAXFRAGNUM_MASK;
1712 }
1713 }
1714 }
1715
1716 // process IP packet received
1717 //
1718 // This MUST be called with at least 4 byte behind 'p'.
1719 // (i.e. this routine writes to p[-4]).
1720 void processipin(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
1721 {
1722 in_addr_t ip;
1723
1724 CSTAT(processipin);
1725
1726 LOG_HEX(5, "IP", p, l);
1727
1728 if (l < 20)
1729 {
1730 LOG(1, s, t, "IP packet too short %d\n", l);
1731 STAT(tunnel_rx_errors);
1732 return ;
1733 }
1734
1735 ip = ntohl(*(uint32_t *)(p + 12));
1736
1737 if (l > MAXETHER)
1738 {
1739 LOG(1, s, t, "IP packet too long %d\n", l);
1740 STAT(tunnel_rx_errors);
1741 return ;
1742 }
1743
1744 if (session[s].ppp.phase != Network || session[s].ppp.ipcp != Opened)
1745 return;
1746
1747 if (!session[s].bundle || bundle[session[s].bundle].num_of_links < 2) // FIXME:
1748 {
1749 // no spoof (do sessionbyip to handled statically routed subnets)
1750 if (ip != session[s].ip && sessionbyip(htonl(ip)) != s)
1751 {
1752 LOG(4, s, t, "Dropping packet with spoofed IP %s\n", fmtaddr(htonl(ip), 0));
1753 return;
1754 }
1755 }
1756
1757 // run access-list if any
1758 if (session[s].filter_in && !ip_filter(p, l, session[s].filter_in - 1))
1759 return;
1760
1761 // adjust MSS on SYN and SYN,ACK packets with options
1762 if ((ntohs(*(uint16_t *) (p + 6)) & 0x1fff) == 0 && p[9] == IPPROTO_TCP) // first tcp fragment
1763 {
1764 int ihl = (p[0] & 0xf) * 4; // length of IP header
1765 if (l >= ihl + 20 && (p[ihl + 13] & TCP_FLAG_SYN) && ((p[ihl + 12] >> 4) > 5))
1766 adjust_tcp_mss(s, t, p, l, p + ihl);
1767 }
1768
1769 // Add on the tun header
1770 p -= 4;
1771 *(uint32_t *) p = htonl(PKTIP);
1772 l += 4;
1773
1774 if (session[s].tbf_in)
1775 {
1776 // Are we throttling this session?
1777 if (config->cluster_iam_master)
1778 tbf_queue_packet(session[s].tbf_in, p, l);
1779 else
1780 master_throttle_packet(session[s].tbf_in, p, l);
1781 return;
1782 }
1783
1784 // send to ethernet
1785 if (tun_write(p, l) < 0)
1786 {
1787 STAT(tun_tx_errors);
1788 LOG(0, s, t, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1789 l, strerror(errno), tunfd, p);
1790
1791 return;
1792 }
1793
1794 p += 4;
1795 l -= 4;
1796
1797 if (session[s].snoop_ip && session[s].snoop_port)
1798 {
1799 // Snooping this session
1800 snoop_send_packet(p, l, session[s].snoop_ip, session[s].snoop_port);
1801 }
1802
1803 update_sessions_in_stat(s, l);
1804
1805 eth_tx += l;
1806
1807 STAT(tun_tx_packets);
1808 INC_STAT(tun_tx_bytes, l);
1809 }
1810
1811 // process Multilink PPP packet received
1812 void processmpin(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
1813 {
1814 bundleidt b = session[s].bundle;
1815 bundlet * this_bundle = &bundle[b];
1816 uint32_t frag_offset, M_offset;
1817 uint16_t frag_index, M_index;
1818 fragmentationt *this_fragmentation = &frag[b];
1819 uint8_t begin_frame = (*p & MP_BEGIN);
1820 uint8_t end_frame = (*p & MP_END);
1821 uint32_t seq_num;
1822 uint8_t flags = *p;
1823 uint16_t begin_index, end_index;
1824
1825 // Perform length checking
1826 if(l > MAXFRAGLEN)
1827 {
1828 LOG(2, s, t, "MPPP: discarding fragment larger than MAXFRAGLEN\n");
1829 return;
1830 }
1831
1832 if(!b)
1833 {
1834 LOG(2, s, t, "MPPP: Invalid bundle id: 0\n");
1835 return;
1836 }
1837 // FIXME !! session[s].mssf means that the receiver wants to receive frames in mssf not means the receiver will send frames in mssf
1838 /* if(session[s].mssf)
1839 {
1840 // Get 12 bit for seq number
1841 seq_num = ntohs((*(uint16_t *) p) & 0xFF0F);
1842 p += 2;
1843 l -= 2;
1844 // After this point the pointer should be advanced 2 bytes
1845 LOG(3, s, t, "MPPP: 12 bits, sequence number: %d\n",seq_num);
1846 }
1847 else */
1848 {
1849 // Get 24 bit for seq number
1850 seq_num = ntohl((*(uint32_t *) p) & 0xFFFFFF00);
1851 p += 4;
1852 l -= 4;
1853 // After this point the pointer should be advanced 4 bytes
1854 LOG(4, s, t, "MPPP: 24 bits sequence number:%d\n",seq_num);
1855 }
1856
1857 // calculate this fragment's offset from the begin seq in the bundle
1858 frag_offset = (seq_num + this_bundle->max_seq - this_fragmentation->start_seq) & (this_bundle->max_seq-1);
1859
1860 // discard this fragment if frag_offset is bigger that the fragmentation buffer size
1861 if (frag_offset >= MAXFRAGNUM)
1862 {
1863 LOG(3, s, t, "MPPP: Index out of range, received more than MAXFRAGNUM fragment (lost frag) seq:%d, begin_seq:%d, bundle:%d, max:%d\n",seq_num, this_fragmentation->start_seq, b, this_bundle->max_seq);
1864 return;
1865 }
1866
1867 // update M
1868 sess_local[s].last_seq = seq_num;
1869 if (seq_num < this_fragmentation->M)
1870 this_fragmentation->M = seq_num;
1871 else
1872 {
1873 uint32_t i, min = sess_local[(this_bundle->members[0])].last_seq;;
1874 for (i = 1; i < this_bundle->num_of_links; i++)
1875 {
1876 uint32_t s_seq = sess_local[(this_bundle->members[i])].last_seq;
1877 if (s_seq < min)
1878 min = s_seq;
1879 }
1880 this_fragmentation->M = min;
1881 }
1882
1883 LOG(4, s, t, "MPPP: Setting M to %d\n", this_fragmentation->M);
1884 //calculate M's offset from the begin seq in the bundle
1885 M_offset = (this_fragmentation->M + this_bundle->max_seq - this_fragmentation->start_seq) & (this_bundle->max_seq-1);
1886
1887 //caculate M's index in the fragment array
1888 M_index = (M_offset + this_fragmentation->start_index) & MAXFRAGNUM_MASK;
1889
1890 //caculate received fragment's index in the fragment array
1891 frag_index = (frag_offset + this_fragmentation->start_index) & MAXFRAGNUM_MASK;
1892
1893 //frame with a single fragment
1894 if (begin_frame && end_frame)
1895 {
1896 // process and reset fragmentation
1897 LOG(4, s, t, "MPPP: Both bits are set (Begin and End).\n");
1898 this_fragmentation->fragment[frag_index].length = l;
1899 this_fragmentation->fragment[frag_index].sid = s;
1900 this_fragmentation->fragment[frag_index].flags = flags;
1901 this_fragmentation->fragment[frag_index].seq = seq_num;
1902 this_fragmentation->re_frame_begin_index = frag_index;
1903 this_fragmentation->re_frame_end_index = frag_index;
1904 processmpframe(s, t, p, l, 0);
1905 this_fragmentation->fragment[frag_index].length = 0;
1906 this_fragmentation->fragment[frag_index].flags = 0;
1907 end_index = frag_index;
1908 }
1909 else
1910 {
1911 // insert the frame in it's place
1912 fragmentt *this_frag = &this_fragmentation->fragment[frag_index];
1913 this_frag->length = l;
1914 this_frag->sid = s;
1915 this_frag->flags = flags;
1916 this_frag->seq = seq_num;
1917 memcpy(this_frag->data, p, l);
1918
1919 // try to assemble the frame that has the received fragment as a member
1920 // get the beginning of this frame
1921 begin_index = end_index = frag_index;
1922 while (this_fragmentation->fragment[begin_index].length)
1923 {
1924 if (this_fragmentation->fragment[begin_index].flags & MP_BEGIN)
1925 break;
1926 begin_index = (begin_index ? (begin_index -1) : (MAXFRAGNUM -1));
1927 }
1928
1929 // return if a lost fragment is found
1930 if (!(this_fragmentation->fragment[begin_index].length))
1931 return; // assembling frame failed
1932 // get the end of his frame
1933 while (this_fragmentation->fragment[end_index].length)
1934 {
1935 if (this_fragmentation->fragment[end_index].flags & MP_END)
1936 break;
1937 end_index = (end_index +1) & MAXFRAGNUM_MASK;
1938 }
1939
1940 // return if a lost fragment is found
1941 if (!(this_fragmentation->fragment[end_index].length))
1942 return; // assembling frame failed
1943
1944 // assemble the packet
1945 //assemble frame, process it, reset fragmentation
1946 uint16_t cur_len = 4; // This is set to 4 to leave 4 bytes for function processipin
1947 uint32_t i;
1948
1949 LOG(4, s, t, "MPPP: processing fragments from %d to %d\n", begin_index, end_index);
1950 // Push to the receive buffer
1951
1952 for (i = begin_index;; i = (i + 1) & MAXFRAGNUM_MASK)
1953 {
1954 this_frag = &this_fragmentation->fragment[i];
1955 if(cur_len + this_frag->length > MAXETHER)
1956 {
1957 LOG(2, s, t, "MPPP: discarding reassembled frames larger than MAXETHER\n");
1958 break;
1959 }
1960 memcpy(this_fragmentation->reassembled_frame+cur_len, this_frag->data, this_frag->length);
1961 LOG(5, s, t, "MPPP: processing frame at %d, with len %d\n", i, this_frag->length);
1962 cur_len += this_frag->length;
1963 if (i == end_index)
1964 {
1965 this_fragmentation->re_frame_len = cur_len;
1966 this_fragmentation->re_frame_begin_index = begin_index;
1967 this_fragmentation->re_frame_end_index = end_index;
1968 // Process the resassembled frame
1969 LOG(5, s, t, "MPPP: Process the reassembled frame, len=%d\n",cur_len);
1970 processmpframe(s, t, this_fragmentation->reassembled_frame, this_fragmentation->re_frame_len, 1);
1971 break;
1972 }
1973 }
1974 // Set reassembled frame length to zero after processing it
1975 this_fragmentation->re_frame_len = 0;
1976 for (i = begin_index;; i = (i + 1) & MAXFRAGNUM_MASK)
1977 {
1978 this_fragmentation->fragment[i].length = 0; // Indicates that this fragment has been consumed
1979 this_fragmentation->fragment[i].flags = 0;
1980 if (i == end_index)
1981 break;
1982 }
1983 }
1984 //discard fragments received before the recently assembled frame
1985 begin_index = this_fragmentation->start_index;
1986 this_fragmentation->start_index = (end_index + 1) & MAXFRAGNUM_MASK;
1987 this_fragmentation->start_seq = (this_fragmentation->fragment[end_index].seq + 1) & (this_bundle->max_seq-1);
1988 //clear length and flags of the discarded fragments
1989 while (begin_index != this_fragmentation->start_index)
1990 {
1991 this_fragmentation->fragment[begin_index].flags = 0;
1992 this_fragmentation->fragment[begin_index].length = 0;
1993 begin_index = (begin_index + 1) & MAXFRAGNUM_MASK;
1994 }
1995
1996 LOG(4, s, t, "MPPP after assembling: M index is =%d, start index is = %d, start seq=%d\n",M_index, this_fragmentation->start_index, this_fragmentation->start_seq);
1997 return;
1998 }
1999
2000 // process IPv6 packet received
2001 //
2002 // This MUST be called with at least 4 byte behind 'p'.
2003 // (i.e. this routine writes to p[-4]).
2004 void processipv6in(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
2005 {
2006 struct in6_addr ip;
2007 in_addr_t ipv4;
2008
2009 CSTAT(processipv6in);
2010
2011 LOG_HEX(5, "IPv6", p, l);
2012
2013 ip = *(struct in6_addr *) (p + 8);
2014 ipv4 = ntohl(*(uint32_t *)(p + 16));
2015
2016 if (l > MAXETHER)
2017 {
2018 LOG(1, s, t, "IP packet too long %d\n", l);
2019 STAT(tunnel_rx_errors);
2020 return ;
2021 }
2022
2023 if (session[s].ppp.phase != Network || session[s].ppp.ipv6cp != Opened)
2024 return;
2025
2026 // no spoof
2027 if (ipv4 != session[s].ip && memcmp(&config->ipv6_prefix, &ip, 8) && sessionbyipv6(ip) != s)
2028 {
2029 char str[INET6_ADDRSTRLEN];
2030 LOG(5, s, t, "Dropping packet with spoofed IP %s\n",
2031 inet_ntop(AF_INET6, &ip, str, INET6_ADDRSTRLEN));
2032 return;
2033 }
2034
2035 // Check if it's a Router Solicition message.
2036 if (*(p + 6) == 58 && *(p + 7) == 255 && *(p + 24) == 0xFF && *(p + 25) == 2 &&
2037 *(uint32_t *)(p + 26) == 0 && *(uint32_t *)(p + 30) == 0 &&
2038 *(uint32_t *)(p + 34) == 0 &&
2039 *(p + 38) == 0 && *(p + 39) == 2 && *(p + 40) == 133) {
2040 LOG(3, s, t, "Got IPv6 RS\n");
2041 send_ipv6_ra(s, t, &ip);
2042 return;
2043 }
2044
2045 // Add on the tun header
2046 p -= 4;
2047 *(uint32_t *) p = htonl(PKTIPV6);
2048 l += 4;
2049
2050 // Are we throttled and a slave?
2051 if (session[s].tbf_in && !config->cluster_iam_master) {
2052 // Pass it to the master for handling.
2053 master_throttle_packet(session[s].tbf_in, p, l);
2054 return;
2055 }
2056
2057 // Are we throttled and a master??
2058 if (session[s].tbf_in && config->cluster_iam_master) {
2059 // Actually handle the throttled packets.
2060 tbf_queue_packet(session[s].tbf_in, p, l);
2061 return;
2062 }
2063
2064 // send to ethernet
2065 if (tun_write(p, l) < 0)
2066 {
2067 STAT(tun_tx_errors);
2068 LOG(0, s, t, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
2069 l, strerror(errno), tunfd, p);
2070
2071 return;
2072 }
2073
2074 p += 4;
2075 l -= 4;
2076
2077 if (session[s].snoop_ip && session[s].snoop_port)
2078 {
2079 // Snooping this session
2080 snoop_send_packet(p, l, session[s].snoop_ip, session[s].snoop_port);
2081 }
2082
2083 update_sessions_in_stat(s, l);
2084
2085 eth_tx += l;
2086
2087 STAT(tun_tx_packets);
2088 INC_STAT(tun_tx_bytes, l);
2089 }
2090
2091 //
2092 // Helper routine for the TBF filters.
2093 // Used to send queued data in from the user.
2094 //
2095 void send_ipin(sessionidt s, uint8_t *buf, int len)
2096 {
2097 LOG_HEX(5, "IP in throttled", buf, len);
2098
2099 if (write(tunfd, buf, len) < 0)
2100 {
2101 STAT(tun_tx_errors);
2102 LOG(0, 0, 0, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
2103 len, strerror(errno), tunfd, buf);
2104
2105 return;
2106 }
2107
2108 buf += 4;
2109 len -= 4;
2110
2111 if (session[s].snoop_ip && session[s].snoop_port)
2112 {
2113 // Snooping this session
2114 snoop_send_packet(buf, len, session[s].snoop_ip, session[s].snoop_port);
2115 }
2116
2117 // Increment packet counters
2118 increment_counter(&session[s].cin, &session[s].cin_wrap, len);
2119 session[s].cin_delta += len;
2120 session[s].pin++;
2121
2122 sess_local[s].cin += len;
2123 sess_local[s].pin++;
2124
2125 eth_tx += len;
2126
2127 STAT(tun_tx_packets);
2128 INC_STAT(tun_tx_bytes, len - 4);
2129 }
2130
2131
2132 // Process CCP messages
2133 void processccp(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
2134 {
2135 uint8_t b[MAXETHER];
2136 uint8_t *q;
2137
2138 CSTAT(processccp);
2139
2140 LOG_HEX(5, "CCP", p, l);
2141
2142 if (session[s].ppp.phase < Network)
2143 {
2144 LOG(2, s, t, "CCP %s ignored in %s phase\n", ppp_code(*p), ppp_phase(session[s].ppp.phase));
2145 return;
2146 }
2147
2148 if (l < 1)
2149 {
2150 LOG(1, s, t, "Short CCP packet\n");
2151 STAT(tunnel_rx_errors);
2152 }
2153
2154 LOG(4, s, t, "CCP: recv %s\n", ppp_code(*p));
2155 if (*p == ConfigAck)
2156 {
2157 switch (session[s].ppp.ccp)
2158 {
2159 case RequestSent:
2160 initialise_restart_count(s, ccp);
2161 change_state(s, ccp, AckReceived);
2162 break;
2163
2164 case AckReceived:
2165 case Opened:
2166 LOG(2, s, t, "CCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session[s].ppp.ccp));
2167 sendccp(s, t);
2168 change_state(s, ccp, RequestSent);
2169 break;
2170
2171 case AckSent:
2172 LOG(3, s, t, "CCP: Opened\n");
2173 change_state(s, ccp, Opened);
2174 break;
2175
2176 default:
2177 LOG(2, s, t, "CCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ccp));
2178 }
2179 }
2180 else if (*p == ConfigReq)
2181 {
2182 if (l < 6) // accept no compression
2183 *p = ConfigAck;
2184 else // compression requested--reject
2185 *p = ConfigRej;
2186
2187 q = makeppp(b, sizeof(b), p, l, s, t, PPPCCP, 0, 0, 0);
2188 if (!q) return;
2189
2190 switch (session[s].ppp.ccp)
2191 {
2192 case Closed:
2193 q = makeppp(b, sizeof(b), p, 2, s, t, PPPCCP, 0, 0, 0);
2194 if (!q) return;
2195 *q = TerminateAck;
2196 *((uint16_t *) (q + 2)) = htons(l = 4);
2197 break;
2198
2199 case Stopped:
2200 initialise_restart_count(s, ccp);
2201 sendccp(s, t);
2202 if (*q == ConfigAck)
2203 change_state(s, ccp, AckSent);
2204 else
2205 change_state(s, ccp, RequestSent);
2206
2207 break;
2208
2209 case RequestSent:
2210 if (*q == ConfigAck)
2211 change_state(s, ccp, AckSent);
2212
2213 break;
2214
2215 case AckReceived:
2216 if (*q == ConfigAck)
2217 change_state(s, ccp, Opened);
2218
2219 break;
2220
2221 case Opened:
2222 initialise_restart_count(s, ccp);
2223 sendccp(s, t);
2224 /* fallthrough */
2225
2226 case AckSent:
2227 if (*q == ConfigAck)
2228 change_state(s, ccp, AckSent);
2229 else
2230 change_state(s, ccp, RequestSent);
2231
2232 break;
2233
2234 default:
2235 LOG(2, s, t, "CCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ccp));
2236 return;
2237 }
2238
2239 LOG(4, s, t, "CCP: send %s\n", ppp_code(*q));
2240 tunnelsend(b, l + (q - b), t);
2241 }
2242 else if (*p == TerminateReq)
2243 {
2244 *p = TerminateAck;
2245 q = makeppp(b, sizeof(b), p, l, s, t, PPPCCP, 0, 0, 0);
2246 if (!q) return;
2247 LOG(3, s, t, "CCP: send %s\n", ppp_code(*q));
2248 tunnelsend(b, l + (q - b), t);
2249 change_state(s, ccp, Stopped);
2250 }
2251 else if (*p != CodeRej)
2252 {
2253 ppp_code_rej(s, t, PPPCCP, "CCP", p, l, b, sizeof(b));
2254 }
2255 }
2256
2257 // send a CHAP challenge
2258 void sendchap(sessionidt s, tunnelidt t)
2259 {
2260 uint8_t b[MAXETHER];
2261 uint16_t r;
2262 uint8_t *q;
2263
2264 CSTAT(sendchap);
2265
2266 r = radiusnew(s);
2267 if (!r)
2268 {
2269 LOG(1, s, t, "No RADIUS to send challenge\n");
2270 STAT(tunnel_tx_errors);
2271 return;
2272 }
2273
2274 LOG(1, s, t, "Send CHAP challenge\n");
2275
2276 radius[r].chap = 1; // CHAP not PAP
2277 radius[r].id++;
2278 if (radius[r].state != RADIUSCHAP)
2279 radius[r].try = 0;
2280
2281 radius[r].state = RADIUSCHAP;
2282 radius[r].retry = backoff(radius[r].try++);
2283 if (radius[r].try > 5)
2284 {
2285 sessionshutdown(s, "CHAP timeout.", CDN_ADMIN_DISC, TERM_REAUTHENTICATION_FAILURE);
2286 STAT(tunnel_tx_errors);
2287 return ;
2288 }
2289 q = makeppp(b, sizeof(b), 0, 0, s, t, PPPCHAP, 0, 0, 0);
2290 if (!q) return;
2291
2292 *q = 1; // challenge
2293 q[1] = radius[r].id; // ID
2294 q[4] = 16; // value size (size of challenge)
2295 memcpy(q + 5, radius[r].auth, 16); // challenge
2296 strcpy((char *) q + 21, hostname); // our name
2297 *(uint16_t *) (q + 2) = htons(strlen(hostname) + 21); // length
2298 tunnelsend(b, strlen(hostname) + 21 + (q - b), t); // send it
2299 }
2300
2301 // fill in a L2TP message with a PPP frame,
2302 // returns start of PPP frame
2303 uint8_t *makeppp(uint8_t *b, int size, uint8_t *p, int l, sessionidt s, tunnelidt t, uint16_t mtype, uint8_t prio, bundleidt bid, uint8_t mp_bits)
2304 {
2305 uint16_t hdr = 0x0002; // L2TP with no options
2306 uint16_t type = mtype;
2307 uint8_t *start = b;
2308
2309 if (size < 16) // Need more space than this!!
2310 {
2311 LOG(0, s, t, "makeppp buffer too small for L2TP header (size=%d)\n", size);
2312 return NULL;
2313 }
2314
2315 if (prio) hdr |= 0x0100; // set priority bit
2316
2317 *(uint16_t *) (b + 0) = htons(hdr);
2318 *(uint16_t *) (b + 2) = htons(tunnel[t].far); // tunnel
2319 *(uint16_t *) (b + 4) = htons(session[s].far); // session
2320 b += 6;
2321
2322 // Check whether this session is part of multilink
2323 if (bid)
2324 {
2325 if (bundle[bid].num_of_links > 1)
2326 type = PPPMP; // Change PPP message type to the PPPMP
2327 else
2328 bid = 0;
2329 }
2330
2331 if (type == PPPLCP || !(session[s].flags & SESSION_ACFC))
2332 {
2333 *(uint16_t *) b = htons(0xFF03); // HDLC header
2334 b += 2;
2335 }
2336
2337 if (type < 0x100 && session[s].flags & SESSION_PFC)
2338 {
2339 *b++ = type;
2340 }
2341 else
2342 {
2343 *(uint16_t *) b = htons(type);
2344 b += 2;
2345 }
2346
2347 if (bid)
2348 {
2349 // Set the sequence number and (B)egin (E)nd flags
2350 if (session[s].mssf)
2351 {
2352 // Set the multilink bits
2353 uint16_t bits_send = mp_bits;
2354 *(uint16_t *) b = htons((bundle[bid].seq_num_t & 0x0FFF)|bits_send);
2355 b += 2;
2356 }
2357 else
2358 {
2359 *(uint32_t *) b = htonl(bundle[bid].seq_num_t);
2360 // Set the multilink bits
2361 *b = mp_bits;
2362 b += 4;
2363 }
2364
2365 bundle[bid].seq_num_t++;
2366
2367 // Add the message type if this fragment has the begin bit set
2368 if (mp_bits & MP_BEGIN)
2369 {
2370 //*b++ = mtype; // The next two lines are instead of this
2371 *(uint16_t *) b = htons(mtype); // Message type
2372 b += 2;
2373 }
2374 }
2375
2376 if ((b - start) + l > size)
2377 {
2378 LOG(2, s, t, "makeppp would overflow buffer (size=%d, header+payload=%d)\n", size, (b - start) + l);
2379 return NULL;
2380 }
2381
2382 // Copy the payload
2383 if (p && l)
2384 memcpy(b, p, l);
2385
2386 return b;
2387 }
2388
2389 static int add_lcp_auth(uint8_t *b, int size, int authtype)
2390 {
2391 int len = 0;
2392 if ((authtype == AUTHCHAP && size < 5) || size < 4)
2393 return 0;
2394
2395 *b++ = 3; // Authentication-Protocol
2396 if (authtype == AUTHCHAP)
2397 {
2398 len = *b++ = 5; // length
2399 *(uint16_t *) b = htons(PPPCHAP); b += 2;
2400 *b++ = 5; // MD5
2401 }
2402 else if (authtype == AUTHPAP)
2403 {
2404 len = *b++ = 4; // length
2405 *(uint16_t *) b = htons(PPPPAP); b += 2;
2406 }
2407 else
2408 {
2409 LOG(0, 0, 0, "add_lcp_auth called with unsupported auth type %d\n", authtype);
2410 }
2411
2412 return len;
2413 }
2414
2415 // Send LCP ConfigReq for MRU, authentication type and magic no
2416 void sendlcp(sessionidt s, tunnelidt t)
2417 {
2418 uint8_t b[500], *q, *l;
2419 int authtype = sess_local[s].lcp_authtype;
2420
2421 if (!(q = makeppp(b, sizeof(b), NULL, 0, s, t, PPPLCP, 0, 0, 0)))
2422 return;
2423
2424 LOG(3, s, t, "LCP: send ConfigReq%s%s%s including MP options\n",
2425 authtype ? " (" : "",
2426 authtype ? (authtype == AUTHCHAP ? "CHAP" : "PAP") : "",
2427 authtype ? ")" : "");
2428
2429 l = q;
2430 *l++ = ConfigReq;
2431 *l++ = ++sess_local[s].lcp_ident; // ID
2432
2433 l += 2; //Save space for length
2434
2435 if (sess_local[s].ppp_mru)
2436 {
2437 *l++ = 1; *l++ = 4; // Maximum-Receive-Unit (length 4)
2438 *(uint16_t *) l = htons(sess_local[s].ppp_mru); l += 2;
2439 }
2440
2441 if (authtype)
2442 l += add_lcp_auth(l, sizeof(b) - (l - b), authtype);
2443
2444 if (session[s].magic)
2445 {
2446 *l++ = 5; *l++ = 6; // Magic-Number (length 6)
2447 *(uint32_t *) l = htonl(session[s].magic);
2448 l += 4;
2449 }
2450
2451 if (sess_local[s].mp_mrru)
2452 {
2453 *l++ = 17; *l++ = 4; // Multilink Max-Receive-Reconstructed-Unit (length 4)
2454 *(uint16_t *) l = htons(sess_local[s].mp_mrru); l += 2;
2455 }
2456
2457 if (sess_local[s].mp_epdis)
2458 {
2459 *l++ = 19; *l++ = 7; // Multilink Endpoint Discriminator (length 7)
2460 *l++ = IPADDR; // Endpoint Discriminator class
2461 *(uint32_t *) l = htonl(sess_local[s].mp_epdis);
2462 l += 4;
2463 }
2464
2465 *(uint16_t *)(q + 2) = htons(l - q); // Length
2466
2467 LOG_HEX(5, "PPPLCP", q, l - q);
2468 if (config->debug > 3) dumplcp(q, l - q);
2469
2470 tunnelsend(b, (l - b), t);
2471 restart_timer(s, lcp);
2472 }
2473
2474 // Send CCP request for no compression
2475 void sendccp(sessionidt s, tunnelidt t)
2476 {
2477 uint8_t b[500], *q;
2478
2479 if (!(q = makeppp(b, sizeof(b), NULL, 0, s, t, PPPCCP, 0, 0, 0)))
2480 return;
2481
2482 LOG(3, s, t, "CCP: send ConfigReq (no compression)\n");
2483
2484 *q = ConfigReq;
2485 *(q + 1) = ++sess_local[s].lcp_ident; // ID
2486 *(uint16_t *)(q + 2) = htons(4); // Length
2487
2488 LOG_HEX(5, "PPPCCP", q, 4);
2489 tunnelsend(b, (q - b) + 4 , t);
2490 restart_timer(s, ccp);
2491 }
2492
2493 // Reject unknown/unconfigured protocols
2494 void protoreject(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l, uint16_t proto)
2495 {
2496
2497 uint8_t buf[MAXETHER];
2498 uint8_t *q;
2499 int mru = session[s].mru;
2500 if (mru < MINMTU) mru = MINMTU;
2501 if (mru > sizeof(buf)) mru = sizeof(buf);
2502
2503 l += 6;
2504 if (l > mru) l = mru;
2505
2506 q = makeppp(buf, sizeof(buf), 0, 0, s, t, PPPLCP, 0, 0, 0);
2507 if (!q) return;
2508
2509 *q = ProtocolRej;
2510 *(q + 1) = ++sess_local[s].lcp_ident;
2511 *(uint16_t *)(q + 2) = htons(l);
2512 *(uint16_t *)(q + 4) = htons(proto);
2513 memcpy(q + 6, p, l - 6);
2514
2515 if (proto == PPPIPV6CP)
2516 LOG(3, s, t, "LCP: send ProtocolRej (IPV6CP: not configured)\n");
2517 else
2518 LOG(2, s, t, "LCP: sent ProtocolRej (0x%04X: unsupported)\n", proto);
2519
2520 tunnelsend(buf, l + (q - buf), t);
2521 }