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