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