Merge from 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 <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, ip_dst;
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 ip_dst = *(uint32_t *)(p + 16);
1756
1757 if (l > MAXETHER)
1758 {
1759 LOG(1, s, t, "IP packet too long %d\n", l);
1760 STAT(tunnel_rx_errors);
1761 return ;
1762 }
1763
1764 if (session[s].ppp.phase != Network || session[s].ppp.ipcp != Opened)
1765 return;
1766
1767 if (!session[s].bundle || bundle[session[s].bundle].num_of_links < 2) // FIXME:
1768 {
1769 // no spoof (do sessionbyip to handled statically routed subnets)
1770 if (!config->disable_no_spoof && ip != session[s].ip && sessionbyip(htonl(ip)) != s)
1771 {
1772 LOG(4, s, t, "Dropping packet with spoofed IP %s\n", fmtaddr(htonl(ip), 0));
1773 return;
1774 }
1775 }
1776
1777 // run access-list if any
1778 if (session[s].filter_in && !ip_filter(p, l, session[s].filter_in - 1))
1779 return;
1780
1781 // adjust MSS on SYN and SYN,ACK packets with options
1782 if ((ntohs(*(uint16_t *) (p + 6)) & 0x1fff) == 0 && p[9] == IPPROTO_TCP) // first tcp fragment
1783 {
1784 int ihl = (p[0] & 0xf) * 4; // length of IP header
1785 if (l >= ihl + 20 && (p[ihl + 13] & TCP_FLAG_SYN) && ((p[ihl + 12] >> 4) > 5))
1786 adjust_tcp_mss(s, t, p, l, p + ihl);
1787 }
1788
1789 // Add on the tun header
1790 p -= 4;
1791 *(uint32_t *) p = htonl(PKTIP);
1792 l += 4;
1793
1794 if (session[s].tbf_in)
1795 {
1796 if (!config->no_throttle_local_IP || !sessionbyip(ip_dst))
1797 {
1798 // Are we throttling this session?
1799 if (config->cluster_iam_master)
1800 tbf_queue_packet(session[s].tbf_in, p, l);
1801 else
1802 master_throttle_packet(session[s].tbf_in, p, l);
1803 return;
1804 }
1805 }
1806
1807 // send to ethernet
1808 if (tun_write(p, l) < 0)
1809 {
1810 STAT(tun_tx_errors);
1811 LOG(0, s, t, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1812 l, strerror(errno), tunfd, p);
1813
1814 return;
1815 }
1816
1817 p += 4;
1818 l -= 4;
1819
1820 if (session[s].snoop_ip && session[s].snoop_port)
1821 {
1822 // Snooping this session
1823 snoop_send_packet(p, l, session[s].snoop_ip, session[s].snoop_port);
1824 }
1825
1826 update_sessions_in_stat(s, l);
1827
1828 eth_tx += l;
1829
1830 STAT(tun_tx_packets);
1831 INC_STAT(tun_tx_bytes, l);
1832 }
1833
1834 // process Multilink PPP packet received
1835 void processmpin(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
1836 {
1837 bundleidt b = session[s].bundle;
1838 bundlet * this_bundle = &bundle[b];
1839 uint32_t maskSeq, max_seq;
1840 int frag_offset;
1841 uint16_t frag_index, frag_index_next, frag_index_prev;
1842 fragmentationt *this_fragmentation = &frag[b];
1843 uint8_t begin_frame = (*p & MP_BEGIN);
1844 uint8_t end_frame = (*p & MP_END);
1845 uint32_t seq_num, seq_num_next, seq_num_prev;
1846 uint32_t i;
1847 uint8_t flags = *p;
1848 uint16_t begin_index, end_index;
1849
1850 // Perform length checking
1851 if(l > MAXFRAGLEN)
1852 {
1853 LOG(2, s, t, "MPPP: discarding fragment larger than MAXFRAGLEN\n");
1854 return;
1855 }
1856
1857 if(!b)
1858 {
1859 LOG(2, s, t, "MPPP: Invalid bundle id: 0\n");
1860 return;
1861 }
1862 // FIXME !! session[s].mssf means that the receiver wants to receive frames in mssf not means the receiver will send frames in mssf
1863 /* if(session[s].mssf)
1864 {
1865 // Get 12 bit for seq number
1866 seq_num = ntohs((*(uint16_t *) p) & 0xFF0F);
1867 p += 2;
1868 l -= 2;
1869 // After this point the pointer should be advanced 2 bytes
1870 LOG(3, s, t, "MPPP: 12 bits, sequence number: %d\n",seq_num);
1871 }
1872 else */
1873 {
1874 // Get 24 bit for seq number
1875 seq_num = ntohl((*(uint32_t *) p) & 0xFFFFFF00);
1876 p += 4;
1877 l -= 4;
1878 // After this point the pointer should be advanced 4 bytes
1879 LOG(4, s, t, "MPPP: 24 bits sequence number:%d\n",seq_num);
1880 }
1881
1882 max_seq = this_bundle->max_seq;
1883 maskSeq = max_seq - 1;
1884
1885 /*
1886 * Expand sequence number to 32 bits, making it as close
1887 * as possible to this_fragmentation->M.
1888 */
1889 seq_num |= this_fragmentation->M & ~maskSeq;
1890 if ((int)(this_fragmentation->M - seq_num) > (int)(maskSeq >> 1))
1891 {
1892 seq_num += maskSeq + 1;
1893 }
1894 else if ((int)(seq_num - this_fragmentation->M) > (int)(maskSeq >> 1))
1895 {
1896 seq_num -= maskSeq + 1; /* should never happen */
1897 }
1898
1899 // calculate this fragment's offset from the begin seq in the bundle
1900 frag_offset = (int) (seq_num - this_fragmentation->start_seq);
1901
1902 sess_local[s].last_seq = seq_num;
1903
1904 // calculate the jitter average
1905 uint32_t ljitter = time_now_ms - sess_local[s].prev_time;
1906 if (ljitter > 0)
1907 {
1908 sess_local[s].jitteravg = (sess_local[s].jitteravg + ljitter)>>1;
1909 sess_local[s].prev_time = time_now_ms;
1910 }
1911
1912 uint32_t Mmin;
1913
1914 if (seq_num < this_fragmentation->M)
1915 {
1916 Mmin = seq_num;
1917 this_fragmentation->M = seq_num;
1918 }
1919 else
1920 {
1921 Mmin = sess_local[(this_bundle->members[0])].last_seq;
1922 for (i = 1; i < this_bundle->num_of_links; i++)
1923 {
1924 uint32_t s_seq = sess_local[(this_bundle->members[i])].last_seq;
1925 if (s_seq < Mmin)
1926 Mmin = s_seq;
1927 }
1928 this_fragmentation->M = Mmin;
1929 }
1930
1931 // calculate M offset of the M seq in the bundle
1932 int M_offset = (int) (Mmin - this_fragmentation->start_seq);
1933
1934 if (M_offset >= MAXFRAGNUM)
1935 {
1936 // There have a long break of the link !!!!!!!!
1937 // M_offset is bigger that the fragmentation buffer size
1938 LOG(3, s, t, "MPPP: M_offset out of range, min:%d, begin_seq:%d\n", Mmin, this_fragmentation->start_seq);
1939
1940 // Calculate the new start index, the previous frag are lost
1941 begin_index = (M_offset + this_fragmentation->start_index) & MAXFRAGNUM_MASK;
1942
1943 // Set new Start sequence
1944 this_fragmentation->start_index = begin_index;
1945 this_fragmentation->start_seq = Mmin;
1946 M_offset = 0;
1947 // recalculate the fragment offset from the new begin seq in the bundle
1948 frag_offset = (int) (seq_num - Mmin);
1949 }
1950
1951 // discard this fragment if the packet comes before the start sequence
1952 if (frag_offset < 0)
1953 {
1954 // this packet comes before the next
1955 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);
1956 return;
1957 }
1958
1959 // discard if frag_offset is bigger that the fragmentation buffer size
1960 if (frag_offset >= MAXFRAGNUM)
1961 {
1962 // frag_offset is bigger that the fragmentation buffer size
1963 LOG(3, s, t, "MPPP: Index out of range, seq:%d, begin_seq:%d\n", seq_num, this_fragmentation->start_seq);
1964 return;
1965 }
1966
1967 //caculate received fragment's index in the fragment array
1968 frag_index = (frag_offset + this_fragmentation->start_index) & MAXFRAGNUM_MASK;
1969
1970 // insert the frame in it's place
1971 fragmentt *this_frag = &this_fragmentation->fragment[frag_index];
1972
1973 if (this_frag->length > 0)
1974 // This fragment is lost, It was around the buffer and it was never completed the packet.
1975 LOG(3, this_frag->sid, this_frag->tid, "MPPP: (INSERT) seq_num:%d frag_index:%d flags:%02X is LOST\n",
1976 this_frag->seq, frag_index, this_frag->flags);
1977
1978 this_frag->length = l;
1979 this_frag->sid = s;
1980 this_frag->tid = t;
1981 this_frag->flags = flags;
1982 this_frag->seq = seq_num;
1983 this_frag->jitteravg = sess_local[s].jitteravg;
1984 memcpy(this_frag->data, p, l);
1985
1986 LOG(4, s, t, "MPPP: seq_num:%d frag_index:%d INSERTED flags: %02X\n", seq_num, frag_index, flags);
1987
1988 //next frag index
1989 frag_index_next = (frag_index + 1) & MAXFRAGNUM_MASK;
1990 //previous frag index
1991 frag_index_prev = (frag_index - 1) & MAXFRAGNUM_MASK;
1992 // next seq
1993 seq_num_next = seq_num + 1;
1994 // previous seq
1995 seq_num_prev = seq_num - 1;
1996
1997 // Clean the buffer and log the lost fragments
1998 if ((frag_index_next != this_fragmentation->start_index) && this_fragmentation->fragment[frag_index_next].length)
1999 {
2000 // check if the next frag is a lost fragment
2001 if (this_fragmentation->fragment[frag_index_next].seq != seq_num_next)
2002 {
2003 // This fragment is lost, It was around the buffer and it was never completed the packet.
2004 LOG(3, this_fragmentation->fragment[frag_index_next].sid, this_fragmentation->fragment[frag_index_next].tid,
2005 "MPPP: (NEXT) seq_num:%d frag_index:%d flags:%02X is LOST\n",
2006 this_fragmentation->fragment[frag_index_next].seq, frag_index_next,
2007 this_fragmentation->fragment[frag_index_next].flags);
2008 // this frag is lost
2009 this_fragmentation->fragment[frag_index_next].length = 0;
2010 this_fragmentation->fragment[frag_index_next].flags = 0;
2011
2012 if (begin_frame && (!end_frame)) return; // assembling frame failed
2013 }
2014 }
2015
2016 // Clean the buffer and log the lost fragments
2017 if ((frag_index != this_fragmentation->start_index) && this_fragmentation->fragment[frag_index_prev].length)
2018 {
2019 // check if the next frag is a lost fragment
2020 if (this_fragmentation->fragment[frag_index_prev].seq != seq_num_prev)
2021 {
2022 // This fragment is lost, It was around the buffer and it was never completed the packet.
2023 LOG(3, this_fragmentation->fragment[frag_index_prev].sid, this_fragmentation->fragment[frag_index_prev].tid,
2024 "MPPP: (PREV) seq_num:%d frag_index:%d flags:%02X is LOST\n",
2025 this_fragmentation->fragment[frag_index_prev].seq, frag_index_prev,
2026 this_fragmentation->fragment[frag_index_prev].flags);
2027
2028 this_fragmentation->fragment[frag_index_prev].length = 0;
2029 this_fragmentation->fragment[frag_index_prev].flags = 0;
2030
2031 if (end_frame && (!begin_frame)) return; // assembling frame failed
2032 }
2033 }
2034
2035 find_frame:
2036 begin_index = this_fragmentation->start_index;
2037 uint32_t b_seq = this_fragmentation->start_seq;
2038 // Try to find a Begin sequence from the start_seq sequence to M sequence
2039 while (b_seq < Mmin)
2040 {
2041 if (this_fragmentation->fragment[begin_index].length)
2042 {
2043 if (b_seq == this_fragmentation->fragment[begin_index].seq)
2044 {
2045 if (this_fragmentation->fragment[begin_index].flags & MP_BEGIN)
2046 {
2047 int isfoundE = 0;
2048 // Adjust the new start sequence and start index
2049 this_fragmentation->start_index = begin_index;
2050 this_fragmentation->start_seq = b_seq;
2051 // Begin Sequence found, now try to found the End Sequence to complete the frame
2052 end_index = begin_index;
2053 while (b_seq < Mmin)
2054 {
2055 if (this_fragmentation->fragment[end_index].length)
2056 {
2057 if (b_seq == this_fragmentation->fragment[end_index].seq)
2058 {
2059 if (this_fragmentation->fragment[end_index].flags & MP_END)
2060 {
2061 // The End sequence was found and the frame is complete
2062 isfoundE = 1;
2063 break;
2064 }
2065 }
2066 else
2067 {
2068 // This fragment is lost, it was never completed the packet.
2069 LOG(3, this_fragmentation->fragment[end_index].sid, this_fragmentation->fragment[end_index].tid,
2070 "MPPP: (FIND END) seq_num:%d frag_index:%d flags:%02X is LOST\n",
2071 this_fragmentation->fragment[end_index].seq, begin_index,
2072 this_fragmentation->fragment[end_index].flags);
2073 // this frag is lost
2074 this_fragmentation->fragment[end_index].length = 0;
2075 this_fragmentation->fragment[end_index].flags = 0;
2076 // This frame is not complete find the next Begin
2077 break;
2078 }
2079 }
2080 else
2081 {
2082 // This frame is not complete find the next Begin if exist
2083 break;
2084 }
2085 end_index = (end_index +1) & MAXFRAGNUM_MASK;
2086 b_seq++;
2087 }
2088
2089 if (isfoundE)
2090 // The End sequence was found and the frame is complete
2091 break;
2092 else
2093 // find the next Begin
2094 begin_index = end_index;
2095 }
2096 }
2097 else
2098 {
2099 // This fragment is lost, it was never completed the packet.
2100 LOG(3, this_fragmentation->fragment[begin_index].sid, this_fragmentation->fragment[begin_index].tid,
2101 "MPPP: (FIND BEGIN) seq_num:%d frag_index:%d flags:%02X is LOST\n",
2102 this_fragmentation->fragment[begin_index].seq, begin_index,
2103 this_fragmentation->fragment[begin_index].flags);
2104 // this frag is lost
2105 this_fragmentation->fragment[begin_index].length = 0;
2106 this_fragmentation->fragment[begin_index].flags = 0;
2107 }
2108 }
2109 begin_index = (begin_index +1) & MAXFRAGNUM_MASK;
2110 b_seq++;
2111 }
2112
2113 assembling_frame:
2114 // try to assemble the frame that has the received fragment as a member
2115 // get the beginning of this frame
2116 begin_index = end_index = this_fragmentation->start_index;
2117 if (this_fragmentation->fragment[begin_index].length)
2118 {
2119 if (!(this_fragmentation->fragment[begin_index].flags & MP_BEGIN))
2120 {
2121 LOG(3, this_fragmentation->fragment[begin_index].sid, this_fragmentation->fragment[begin_index].tid,
2122 "MPPP: (NOT BEGIN) seq_num:%d frag_index:%d flags:%02X\n",
2123 this_fragmentation->fragment[begin_index].seq, begin_index,
2124 this_fragmentation->fragment[begin_index].flags);
2125 // should occur only after an "M_Offset out of range"
2126 // The start sequence must be a begin sequence
2127 this_fragmentation->start_index = (begin_index +1) & MAXFRAGNUM_MASK;
2128 this_fragmentation->start_seq++;
2129 return; // assembling frame failed
2130 }
2131 }
2132 else
2133 return; // assembling frame failed
2134
2135 // get the end of his frame
2136 while (this_fragmentation->fragment[end_index].length)
2137 {
2138 if (this_fragmentation->fragment[end_index].flags & MP_END)
2139 break;
2140
2141 end_index = (end_index +1) & MAXFRAGNUM_MASK;
2142
2143 if (end_index == this_fragmentation->start_index)
2144 return; // assembling frame failed
2145 }
2146
2147 // return if a lost fragment is found
2148 if (!(this_fragmentation->fragment[end_index].length))
2149 return; // assembling frame failed
2150
2151 // assemble the packet
2152 //assemble frame, process it, reset fragmentation
2153 uint16_t cur_len = 4; // This is set to 4 to leave 4 bytes for function processipin
2154
2155 LOG(4, s, t, "MPPP: processing fragments from %d to %d\n", begin_index, end_index);
2156 // Push to the receive buffer
2157
2158 for (i = begin_index;; i = (i + 1) & MAXFRAGNUM_MASK)
2159 {
2160 this_frag = &this_fragmentation->fragment[i];
2161 if(cur_len + this_frag->length > MAXETHER)
2162 {
2163 LOG(2, s, t, "MPPP: discarding reassembled frames larger than MAXETHER\n");
2164 break;
2165 }
2166
2167 memcpy(this_fragmentation->reassembled_frame+cur_len, this_frag->data, this_frag->length);
2168 LOG(5, s, t, "MPPP: processing frame at %d, with len %d\n", i, this_frag->length);
2169
2170 cur_len += this_frag->length;
2171 if (i == end_index)
2172 {
2173 this_fragmentation->re_frame_len = cur_len;
2174 this_fragmentation->re_frame_begin_index = begin_index;
2175 this_fragmentation->re_frame_end_index = end_index;
2176 // Process the resassembled frame
2177 LOG(5, s, t, "MPPP: Process the reassembled frame, len=%d\n",cur_len);
2178 processmpframe(s, t, this_fragmentation->reassembled_frame, this_fragmentation->re_frame_len, 1);
2179 break;
2180 }
2181 }
2182
2183 // Set reassembled frame length to zero after processing it
2184 this_fragmentation->re_frame_len = 0;
2185 for (i = begin_index;; i = (i + 1) & MAXFRAGNUM_MASK)
2186 {
2187 this_fragmentation->fragment[i].length = 0; // Indicates that this fragment has been consumed
2188 this_fragmentation->fragment[i].flags = 0;
2189 if (i == end_index)
2190 break;
2191 }
2192
2193 // Set the new start_index and start_seq
2194 this_fragmentation->start_index = (end_index + 1) & MAXFRAGNUM_MASK;
2195 this_fragmentation->start_seq = this_fragmentation->fragment[end_index].seq + 1;
2196 LOG(4, s, t, "MPPP after assembling: start index is = %d, start seq=%d\n", this_fragmentation->start_index, this_fragmentation->start_seq);
2197
2198 begin_index = this_fragmentation->start_index;
2199 if ((this_fragmentation->fragment[begin_index].length) &&
2200 (this_fragmentation->fragment[begin_index].seq != this_fragmentation->start_seq))
2201 {
2202 LOG(3, this_fragmentation->fragment[begin_index].sid, this_fragmentation->fragment[begin_index].tid,
2203 "MPPP: (START) seq_num:%d frag_index:%d flags:%02X is LOST\n",
2204 this_fragmentation->fragment[begin_index].seq, begin_index,
2205 this_fragmentation->fragment[begin_index].flags);
2206 this_fragmentation->fragment[begin_index].length = 0;
2207 this_fragmentation->fragment[begin_index].flags = 0;
2208 }
2209
2210 if (this_fragmentation->start_seq <= Mmin)
2211 // It's possible to find other complete frame or lost frame.
2212 goto find_frame;
2213 else if ((this_fragmentation->fragment[begin_index].length) &&
2214 (this_fragmentation->fragment[begin_index].flags & MP_BEGIN))
2215 // may be that the next frame is completed
2216 goto assembling_frame;
2217
2218 return;
2219 }
2220
2221 // process IPv6 packet received
2222 //
2223 // This MUST be called with at least 4 byte behind 'p'.
2224 // (i.e. this routine writes to p[-4]).
2225 void processipv6in(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
2226 {
2227 struct in6_addr ip;
2228 in_addr_t ipv4;
2229
2230 CSTAT(processipv6in);
2231
2232 LOG_HEX(5, "IPv6", p, l);
2233
2234 ip = *(struct in6_addr *) (p + 8);
2235 ipv4 = ntohl(*(uint32_t *)(p + 16));
2236
2237 if (l > MAXETHER)
2238 {
2239 LOG(1, s, t, "IP packet too long %d\n", l);
2240 STAT(tunnel_rx_errors);
2241 return ;
2242 }
2243
2244 if (session[s].ppp.phase != Network || session[s].ppp.ipv6cp != Opened)
2245 return;
2246
2247 // no spoof
2248 if ((ipv4 != session[s].ip || memcmp(&config->ipv6_prefix, &ip, 8)) && sessionbyipv6(ip) != s)
2249 {
2250 char str[INET6_ADDRSTRLEN];
2251 LOG(5, s, t, "Dropping packet with spoofed IP %s\n",
2252 inet_ntop(AF_INET6, &ip, str, INET6_ADDRSTRLEN));
2253 return;
2254 }
2255
2256 // Check if it's a Router Solicition message.
2257 if (*(p + 6) == 58 && *(p + 7) == 255 && *(p + 24) == 0xFF && *(p + 25) == 2 &&
2258 *(uint32_t *)(p + 26) == 0 && *(uint32_t *)(p + 30) == 0 &&
2259 *(uint32_t *)(p + 34) == 0 &&
2260 *(p + 38) == 0 && *(p + 39) == 2 && *(p + 40) == 133) {
2261 LOG(3, s, t, "Got IPv6 RS\n");
2262 send_ipv6_ra(s, t, &ip);
2263 return;
2264 }
2265
2266 // Add on the tun header
2267 p -= 4;
2268 *(uint32_t *) p = htonl(PKTIPV6);
2269 l += 4;
2270
2271 // Are we throttled and a slave?
2272 if (session[s].tbf_in && !config->cluster_iam_master) {
2273 // Pass it to the master for handling.
2274 master_throttle_packet(session[s].tbf_in, p, l);
2275 return;
2276 }
2277
2278 // Are we throttled and a master??
2279 if (session[s].tbf_in && config->cluster_iam_master) {
2280 // Actually handle the throttled packets.
2281 tbf_queue_packet(session[s].tbf_in, p, l);
2282 return;
2283 }
2284
2285 // send to ethernet
2286 if (tun_write(p, l) < 0)
2287 {
2288 STAT(tun_tx_errors);
2289 LOG(0, s, t, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
2290 l, strerror(errno), tunfd, p);
2291
2292 return;
2293 }
2294
2295 p += 4;
2296 l -= 4;
2297
2298 if (session[s].snoop_ip && session[s].snoop_port)
2299 {
2300 // Snooping this session
2301 snoop_send_packet(p, l, session[s].snoop_ip, session[s].snoop_port);
2302 }
2303
2304 update_sessions_in_stat(s, l);
2305
2306 eth_tx += l;
2307
2308 STAT(tun_tx_packets);
2309 INC_STAT(tun_tx_bytes, l);
2310 }
2311
2312 //
2313 // Helper routine for the TBF filters.
2314 // Used to send queued data in from the user.
2315 //
2316 void send_ipin(sessionidt s, uint8_t *buf, int len)
2317 {
2318 LOG_HEX(5, "IP in throttled", buf, len);
2319
2320 if (write(tunfd, buf, len) < 0)
2321 {
2322 STAT(tun_tx_errors);
2323 LOG(0, 0, 0, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
2324 len, strerror(errno), tunfd, buf);
2325
2326 return;
2327 }
2328
2329 buf += 4;
2330 len -= 4;
2331
2332 if (session[s].snoop_ip && session[s].snoop_port)
2333 {
2334 // Snooping this session
2335 snoop_send_packet(buf, len, session[s].snoop_ip, session[s].snoop_port);
2336 }
2337
2338 // Increment packet counters
2339 increment_counter(&session[s].cin, &session[s].cin_wrap, len);
2340 session[s].cin_delta += len;
2341 session[s].pin++;
2342
2343 sess_local[s].cin += len;
2344 sess_local[s].pin++;
2345
2346 eth_tx += len;
2347
2348 STAT(tun_tx_packets);
2349 INC_STAT(tun_tx_bytes, len - 4);
2350 }
2351
2352
2353 // Process CCP messages
2354 void processccp(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
2355 {
2356 uint8_t b[MAXETHER];
2357 uint8_t *q;
2358
2359 CSTAT(processccp);
2360
2361 LOG_HEX(5, "CCP", p, l);
2362
2363 if (session[s].ppp.phase < Network)
2364 {
2365 LOG(2, s, t, "CCP %s ignored in %s phase\n", ppp_code(*p), ppp_phase(session[s].ppp.phase));
2366 return;
2367 }
2368
2369 if (l < 1)
2370 {
2371 LOG(1, s, t, "Short CCP packet\n");
2372 STAT(tunnel_rx_errors);
2373 }
2374
2375 LOG(4, s, t, "CCP: recv %s\n", ppp_code(*p));
2376 if (*p == ConfigAck)
2377 {
2378 switch (session[s].ppp.ccp)
2379 {
2380 case RequestSent:
2381 initialise_restart_count(s, ccp);
2382 change_state(s, ccp, AckReceived);
2383 break;
2384
2385 case AckReceived:
2386 case Opened:
2387 LOG(2, s, t, "CCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session[s].ppp.ccp));
2388 sendccp(s, t);
2389 change_state(s, ccp, RequestSent);
2390 break;
2391
2392 case AckSent:
2393 LOG(3, s, t, "CCP: Opened\n");
2394 change_state(s, ccp, Opened);
2395 break;
2396
2397 default:
2398 LOG(2, s, t, "CCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ccp));
2399 }
2400 }
2401 else if (*p == ConfigReq)
2402 {
2403 if (l < 6) // accept no compression
2404 *p = ConfigAck;
2405 else // compression requested--reject
2406 *p = ConfigRej;
2407
2408 q = makeppp(b, sizeof(b), p, l, s, t, PPPCCP, 0, 0, 0);
2409 if (!q) return;
2410
2411 switch (session[s].ppp.ccp)
2412 {
2413 case Closed:
2414 q = makeppp(b, sizeof(b), p, 2, s, t, PPPCCP, 0, 0, 0);
2415 if (!q) return;
2416 *q = TerminateAck;
2417 *((uint16_t *) (q + 2)) = htons(l = 4);
2418 break;
2419
2420 case Stopped:
2421 initialise_restart_count(s, ccp);
2422 sendccp(s, t);
2423 if (*q == ConfigAck)
2424 change_state(s, ccp, AckSent);
2425 else
2426 change_state(s, ccp, RequestSent);
2427
2428 break;
2429
2430 case RequestSent:
2431 if (*q == ConfigAck)
2432 change_state(s, ccp, AckSent);
2433
2434 break;
2435
2436 case AckReceived:
2437 if (*q == ConfigAck)
2438 change_state(s, ccp, Opened);
2439
2440 break;
2441
2442 case Opened:
2443 initialise_restart_count(s, ccp);
2444 sendccp(s, t);
2445 /* fallthrough */
2446
2447 case AckSent:
2448 if (*q == ConfigAck)
2449 change_state(s, ccp, AckSent);
2450 else
2451 change_state(s, ccp, RequestSent);
2452
2453 break;
2454
2455 default:
2456 LOG(2, s, t, "CCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ccp));
2457 return;
2458 }
2459
2460 LOG(4, s, t, "CCP: send %s\n", ppp_code(*q));
2461 tunnelsend(b, l + (q - b), t);
2462 }
2463 else if (*p == TerminateReq)
2464 {
2465 *p = TerminateAck;
2466 q = makeppp(b, sizeof(b), p, l, s, t, PPPCCP, 0, 0, 0);
2467 if (!q) return;
2468 LOG(3, s, t, "CCP: send %s\n", ppp_code(*q));
2469 tunnelsend(b, l + (q - b), t);
2470 change_state(s, ccp, Stopped);
2471 }
2472 else if (*p != CodeRej)
2473 {
2474 ppp_code_rej(s, t, PPPCCP, "CCP", p, l, b, sizeof(b));
2475 }
2476 }
2477
2478 // send a CHAP challenge
2479 void sendchap(sessionidt s, tunnelidt t)
2480 {
2481 uint8_t b[MAXETHER];
2482 uint16_t r;
2483 uint8_t *q;
2484
2485 CSTAT(sendchap);
2486
2487 r = radiusnew(s);
2488 if (!r)
2489 {
2490 LOG(1, s, t, "No RADIUS to send challenge\n");
2491 STAT(tunnel_tx_errors);
2492 return;
2493 }
2494
2495 LOG(1, s, t, "Send CHAP challenge\n");
2496
2497 radius[r].chap = 1; // CHAP not PAP
2498 radius[r].id++;
2499 if (radius[r].state != RADIUSCHAP)
2500 radius[r].try = 0;
2501
2502 radius[r].state = RADIUSCHAP;
2503 radius[r].retry = backoff(radius[r].try++);
2504 if (radius[r].try > 5)
2505 {
2506 sessionshutdown(s, "CHAP timeout.", CDN_ADMIN_DISC, TERM_REAUTHENTICATION_FAILURE);
2507 STAT(tunnel_tx_errors);
2508 return ;
2509 }
2510 q = makeppp(b, sizeof(b), 0, 0, s, t, PPPCHAP, 0, 0, 0);
2511 if (!q) return;
2512
2513 *q = 1; // challenge
2514 q[1] = radius[r].id; // ID
2515 q[4] = 16; // value size (size of challenge)
2516 memcpy(q + 5, radius[r].auth, 16); // challenge
2517 strcpy((char *) q + 21, config->multi_n_hostname[tunnel[t].indexudp][0]?config->multi_n_hostname[tunnel[t].indexudp]:hostname); // our name
2518 *(uint16_t *) (q + 2) = htons(strlen(config->multi_n_hostname[tunnel[t].indexudp][0]?config->multi_n_hostname[tunnel[t].indexudp]:hostname) + 21); // length
2519 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
2520 }
2521
2522 // fill in a L2TP message with a PPP frame,
2523 // returns start of PPP frame
2524 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)
2525 {
2526 uint16_t hdr = 0x0002; // L2TP with no options
2527 uint16_t type = mtype;
2528 uint8_t *start = b;
2529
2530 if (t == TUNNEL_ID_PPPOE)
2531 {
2532 return pppoe_makeppp(b, size, p, l, s, t, mtype, prio, bid, mp_bits);
2533 }
2534
2535 if (size < 16) // Need more space than this!!
2536 {
2537 LOG(0, s, t, "makeppp buffer too small for L2TP header (size=%d)\n", size);
2538 return NULL;
2539 }
2540
2541 if (prio) hdr |= 0x0100; // set priority bit
2542
2543 *(uint16_t *) (b + 0) = htons(hdr);
2544 *(uint16_t *) (b + 2) = htons(tunnel[t].far); // tunnel
2545 *(uint16_t *) (b + 4) = htons(session[s].far); // session
2546 b += 6;
2547
2548 // Check whether this session is part of multilink
2549 if (bid)
2550 {
2551 if (bundle[bid].num_of_links > 1)
2552 type = PPPMP; // Change PPP message type to the PPPMP
2553 else
2554 bid = 0;
2555 }
2556
2557 if (type == PPPLCP || !(session[s].flags & SESSION_ACFC))
2558 {
2559 *(uint16_t *) b = htons(0xFF03); // HDLC header
2560 b += 2;
2561 }
2562
2563 if (type < 0x100 && session[s].flags & SESSION_PFC)
2564 {
2565 *b++ = type;
2566 }
2567 else
2568 {
2569 *(uint16_t *) b = htons(type);
2570 b += 2;
2571 }
2572
2573 if (bid)
2574 {
2575 // Set the sequence number and (B)egin (E)nd flags
2576 if (session[s].mssf)
2577 {
2578 // Set the multilink bits
2579 uint16_t bits_send = mp_bits;
2580 *(uint16_t *) b = htons((bundle[bid].seq_num_t & 0x0FFF)|bits_send);
2581 b += 2;
2582 }
2583 else
2584 {
2585 *(uint32_t *) b = htonl(bundle[bid].seq_num_t);
2586 // Set the multilink bits
2587 *b = mp_bits;
2588 b += 4;
2589 }
2590
2591 bundle[bid].seq_num_t++;
2592
2593 // Add the message type if this fragment has the begin bit set
2594 if (mp_bits & MP_BEGIN)
2595 {
2596 //*b++ = mtype; // The next two lines are instead of this
2597 *(uint16_t *) b = htons(mtype); // Message type
2598 b += 2;
2599 }
2600 }
2601
2602 if ((b - start) + l > size)
2603 {
2604 LOG(2, s, t, "makeppp would overflow buffer (size=%d, header+payload=%td)\n", size, (b - start) + l);
2605 return NULL;
2606 }
2607
2608 // Copy the payload
2609 if (p && l)
2610 memcpy(b, p, l);
2611
2612 return b;
2613 }
2614
2615 // fill in a L2TP message with a PPP frame,
2616 // returns start of PPP frame
2617 //(note: THIS ROUTINE CAN WRITES TO p[-28]).
2618 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)
2619 {
2620 uint16_t hdr = 0x0002; // L2TP with no options
2621 uint16_t type = mtype;
2622 uint8_t *b = p;
2623
2624 if (t == TUNNEL_ID_PPPOE)
2625 {
2626 return opt_pppoe_makeppp(p, l, s, t, mtype, prio, bid, mp_bits);
2627 }
2628
2629 // Check whether this session is part of multilink
2630 if (bid)
2631 {
2632 if (bundle[bid].num_of_links > 1)
2633 type = PPPMP; // Change PPP message type to the PPPMP
2634 else
2635 bid = 0;
2636 }
2637
2638 if (bid)
2639 {
2640 // Add the message type if this fragment has the begin bit set
2641 if (mp_bits & MP_BEGIN)
2642 {
2643 b -= 2;
2644 *(uint16_t *) b = htons(mtype); // Message type
2645 }
2646
2647 // Set the sequence number and (B)egin (E)nd flags
2648 if (session[s].mssf)
2649 {
2650 // Set the multilink bits
2651 uint16_t bits_send = mp_bits;
2652 b -= 2;
2653 *(uint16_t *) b = htons((bundle[bid].seq_num_t & 0x0FFF)|bits_send);
2654 }
2655 else
2656 {
2657 b -= 4;
2658 *(uint32_t *) b = htonl(bundle[bid].seq_num_t);
2659 // Set the multilink bits
2660 *b = mp_bits;
2661 }
2662
2663 bundle[bid].seq_num_t++;
2664 }
2665
2666 if (type < 0x100 && session[s].flags & SESSION_PFC)
2667 {
2668 b--;
2669 *b = type;
2670 }
2671 else
2672 {
2673 b -= 2;
2674 *(uint16_t *) b = htons(type);
2675 }
2676
2677 if (type == PPPLCP || !(session[s].flags & SESSION_ACFC))
2678 {
2679 b -= 2;
2680 *(uint16_t *) b = htons(0xFF03); // HDLC header
2681 }
2682
2683 if (prio) hdr |= 0x0100; // set priority bit
2684 b -= 2;
2685 *(uint16_t *) b = htons(session[s].far); // session
2686 b -= 2;
2687 *(uint16_t *) b = htons(tunnel[t].far); // tunnel
2688 b -= 2;
2689 *(uint16_t *) b = htons(hdr);
2690
2691 return b;
2692 }
2693
2694 static int add_lcp_auth(uint8_t *b, int size, int authtype)
2695 {
2696 int len = 0;
2697 if ((authtype == AUTHCHAP && size < 5) || size < 4)
2698 return 0;
2699
2700 *b++ = 3; // Authentication-Protocol
2701 if (authtype == AUTHCHAP)
2702 {
2703 len = *b++ = 5; // length
2704 *(uint16_t *) b = htons(PPPCHAP); b += 2;
2705 *b++ = 5; // MD5
2706 }
2707 else if (authtype == AUTHPAP)
2708 {
2709 len = *b++ = 4; // length
2710 *(uint16_t *) b = htons(PPPPAP); b += 2;
2711 }
2712 else
2713 {
2714 LOG(0, 0, 0, "add_lcp_auth called with unsupported auth type %d\n", authtype);
2715 }
2716
2717 return len;
2718 }
2719
2720 // Send LCP ConfigReq for MRU, authentication type and magic no
2721 void sendlcp(sessionidt s, tunnelidt t)
2722 {
2723 uint8_t b[500], *q, *l;
2724 int authtype = sess_local[s].lcp_authtype;
2725
2726 if (!(q = makeppp(b, sizeof(b), NULL, 0, s, t, PPPLCP, 0, 0, 0)))
2727 return;
2728
2729 LOG(3, s, t, "LCP: send ConfigReq%s%s%s including MP options\n",
2730 authtype ? " (" : "",
2731 authtype ? (authtype == AUTHCHAP ? "CHAP" : "PAP") : "",
2732 authtype ? ")" : "");
2733
2734 l = q;
2735 *l++ = ConfigReq;
2736 *l++ = ++sess_local[s].lcp_ident; // ID
2737
2738 l += 2; //Save space for length
2739
2740 if (sess_local[s].ppp_mru)
2741 {
2742 *l++ = 1; *l++ = 4; // Maximum-Receive-Unit (length 4)
2743 *(uint16_t *) l = htons(sess_local[s].ppp_mru); l += 2;
2744 }
2745
2746 if (authtype)
2747 l += add_lcp_auth(l, sizeof(b) - (l - b), authtype);
2748
2749 if (session[s].magic)
2750 {
2751 *l++ = 5; *l++ = 6; // Magic-Number (length 6)
2752 *(uint32_t *) l = htonl(session[s].magic);
2753 l += 4;
2754 }
2755
2756 if (sess_local[s].mp_mrru)
2757 {
2758 *l++ = 17; *l++ = 4; // Multilink Max-Receive-Reconstructed-Unit (length 4)
2759 *(uint16_t *) l = htons(sess_local[s].mp_mrru); l += 2;
2760 }
2761
2762 if (sess_local[s].mp_epdis)
2763 {
2764 *l++ = 19; *l++ = 7; // Multilink Endpoint Discriminator (length 7)
2765 *l++ = IPADDR; // Endpoint Discriminator class
2766 *(uint32_t *) l = htonl(sess_local[s].mp_epdis);
2767 l += 4;
2768 }
2769
2770 *(uint16_t *)(q + 2) = htons(l - q); // Length
2771
2772 LOG_HEX(5, "PPPLCP", q, l - q);
2773 if (config->debug > 3) dumplcp(q, l - q);
2774
2775 tunnelsend(b, (l - b), t);
2776 restart_timer(s, lcp);
2777 }
2778
2779 // Send CCP request for no compression
2780 void sendccp(sessionidt s, tunnelidt t)
2781 {
2782 uint8_t b[500], *q;
2783
2784 if (!(q = makeppp(b, sizeof(b), NULL, 0, s, t, PPPCCP, 0, 0, 0)))
2785 return;
2786
2787 LOG(3, s, t, "CCP: send ConfigReq (no compression)\n");
2788
2789 *q = ConfigReq;
2790 *(q + 1) = ++sess_local[s].lcp_ident; // ID
2791 *(uint16_t *)(q + 2) = htons(4); // Length
2792
2793 LOG_HEX(5, "PPPCCP", q, 4);
2794 tunnelsend(b, (q - b) + 4 , t);
2795 restart_timer(s, ccp);
2796 }
2797
2798 // Reject unknown/unconfigured protocols
2799 void protoreject(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l, uint16_t proto)
2800 {
2801
2802 uint8_t buf[MAXETHER];
2803 uint8_t *q;
2804 int mru = session[s].mru;
2805 if (mru < MINMTU) mru = MINMTU;
2806 if (mru > sizeof(buf)) mru = sizeof(buf);
2807
2808 l += 6;
2809 if (l > mru) l = mru;
2810
2811 q = makeppp(buf, sizeof(buf), 0, 0, s, t, PPPLCP, 0, 0, 0);
2812 if (!q) return;
2813
2814 *q = ProtocolRej;
2815 *(q + 1) = ++sess_local[s].lcp_ident;
2816 *(uint16_t *)(q + 2) = htons(l);
2817 *(uint16_t *)(q + 4) = htons(proto);
2818 memcpy(q + 6, p, l - 6);
2819
2820 if (proto == PPPIPV6CP)
2821 LOG(3, s, t, "LCP: send ProtocolRej (IPV6CP: not configured)\n");
2822 else
2823 LOG(2, s, t, "LCP: sent ProtocolRej (0x%04X: unsupported)\n", proto);
2824
2825 tunnelsend(buf, l + (q - buf), t);
2826 }