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