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