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