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