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