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