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