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