cleanup clash between timeout and session_timout
[l2tpns.git] / ppp.c
1 // L2TPNS PPP Stuff
2
3 char const *cvs_id_ppp = "$Id: ppp.c,v 1.102 2006-08-02 13:35:39 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 // Are we throttled and a slave?
1707 if (session[s].tbf_in && !config->cluster_iam_master) {
1708 // Pass it to the master for handling.
1709 master_throttle_packet(session[s].tbf_in, p, l);
1710 return;
1711 }
1712
1713 // Are we throttled and a master??
1714 if (session[s].tbf_in && config->cluster_iam_master) {
1715 // Actually handle the throttled packets.
1716 tbf_queue_packet(session[s].tbf_in, p, l);
1717 return;
1718 }
1719
1720 // send to ethernet
1721 if (tun_write(p, l) < 0)
1722 {
1723 STAT(tun_tx_errors);
1724 LOG(0, s, t, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1725 l, strerror(errno), tunfd, p);
1726
1727 return;
1728 }
1729
1730 p += 4;
1731 l -= 4;
1732
1733 if (session[s].snoop_ip && session[s].snoop_port)
1734 {
1735 // Snooping this session
1736 snoop_send_packet(p, l, session[s].snoop_ip, session[s].snoop_port);
1737 }
1738
1739 increment_counter(&session[s].cin, &session[s].cin_wrap, l);
1740 session[s].cin_delta += l;
1741 session[s].pin++;
1742
1743 sess_local[s].cin += l;
1744 sess_local[s].pin++;
1745
1746 eth_tx += l;
1747
1748 STAT(tun_tx_packets);
1749 INC_STAT(tun_tx_bytes, l);
1750 }
1751
1752 // process Multilink PPP packet received
1753 void processmpin(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
1754 {
1755 bundleidt b = session[s].bundle;
1756 uint8_t begin_frame;
1757 uint8_t end_frame;
1758 uint32_t seq_num;
1759 uint32_t offset;
1760
1761 if (!b)
1762 {
1763 LOG(3, s, t, "MPPP: Invalid bundle id: 0\n");
1764 return;
1765 }
1766
1767 begin_frame = (*p & 0x80);
1768 end_frame = (*p & 0x40);
1769 if (session[s].mssf)
1770 {
1771 // Get 12 bit for seq number
1772 uint16_t short_seq_num = ntohs((*(uint16_t *) p) & 0xFF0F);
1773 uint16_t short_seq_num2 = short_seq_num >> 4;
1774 p += 2;
1775 l -= 2;
1776 seq_num = short_seq_num2;
1777 // After this point the pointer should be advanced 2 bytes
1778 LOG(3, s, t, "MPPP: 12 bits, sequence number: %d, short1: %d, short2: %d\n",seq_num, short_seq_num, short_seq_num2);
1779 }
1780 else
1781 {
1782 // Get 24 bit for seq number
1783 p++;
1784 seq_num = ntohl((*(uint32_t *) p) & 0xFFFFFF00);
1785 seq_num = seq_num >> 8;
1786 p += 3;
1787 l -= 4;
1788 // After this point the pointer should be advanced 4 bytes
1789 LOG(4, s, t, "MPPP: 24 bits sequence number:%d\n",seq_num);
1790 }
1791
1792 if (seq_num - bundle[b].offset < 0)
1793 {
1794 bundle[b].offset = 0;
1795 bundle[b].pending_frag = 0;
1796 }
1797
1798 offset = bundle[b].offset;
1799 if (begin_frame)
1800 {
1801 // Check for previous non-assembled frames
1802 int error = 0;
1803 if (bundle[b].pending_frag)
1804 {
1805 uint32_t fn = bundle[b].seq_num_m - offset;
1806 uint16_t cur_len;
1807 bundle[b].pending_frag = 0;
1808 // Check for array indexes
1809 if (fn < 0 || fn > MAXFRAGNUM)
1810 {
1811 LOG(2, s, t, "ERROR: Index out of range fn:%d, bundle:%d\n",fn,b);
1812 return;
1813 }
1814
1815 if (seq_num-offset < 0 || seq_num-offset > MAXFRAGNUM)
1816 {
1817 LOG(2, s, t, "ERROR: Index out of range fn(last):%d, bundle:%d\n",fn,b);
1818 return;
1819 }
1820 /////////////////////////////////////////////////////
1821 cur_len = 4; // This is set to 4 to leave 4 bytes for function processipin
1822 for (fn = bundle[b].seq_num_m - offset; fn < seq_num - offset; fn++)
1823 {
1824 if (!frag[b].fragment[fn].length)
1825 {
1826 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);
1827 error = 1;
1828 break;
1829 }
1830
1831 if (cur_len + frag[b].fragment[fn].length > MAXETHER)
1832 {
1833 LOG(2, s, t, "MPPP: ERROR: very long frame after assembling %d\n", frag[b].fragment[fn].length+cur_len);
1834 error = 1;
1835 break;
1836 }
1837
1838 memcpy(frag[b].reassembled_frame+cur_len, frag[b].fragment[fn].data, frag[b].fragment[fn].length);
1839 cur_len += frag[b].fragment[fn].length;
1840 frag[b].fragment[fn].length = 0; // Indicates that this fragment has been consumed
1841 // This is usefull for compression
1842 memset(frag[b].fragment[fn].data, 0, sizeof(frag[b].fragment[fn].data));
1843 }
1844
1845 if (!error)
1846 {
1847 frag[b].re_frame_len = cur_len;
1848 // Process the resassembled frame
1849 LOG(4, s, t, "MPPP: Process the reassembled frame, len=%d\n",cur_len);
1850 processmpframe(s, t, frag[b].reassembled_frame, frag[b].re_frame_len, 1);
1851 // Set reassembled frame length to zero after processing it
1852 frag[b].re_frame_len = 0;
1853 memset(frag[b].reassembled_frame, 0, sizeof(frag[b].reassembled_frame));
1854 }
1855 }
1856 //////////////////////////////////////////
1857 bundle[b].seq_num_m = seq_num;
1858 if (end_frame)
1859 {
1860 // Both bits are set
1861 LOG(4, s, t, "MPPP: Both bits are set (Begin and End).\n");
1862 processmpframe(s, t, p, l, 0);
1863 // The maximum number of fragments is 1500
1864 if (seq_num - bundle[b].offset >= 1400)
1865 {
1866 bundle[b].offset = seq_num;
1867 LOG(4, s, t, "MPPP: Setting offset to: %d\n",bundle[b].offset);
1868 }
1869 }
1870 else
1871 {
1872 bundle[b].pending_frag = 1;
1873 // End bit is clear
1874 LOG(4, s, t, "MPPP: Push to receive buffer\n");
1875 // Push to the receive buffer
1876 // Array indexes checking
1877 if (seq_num-offset < 0 || seq_num-offset >= MAXFRAGNUM)
1878 {
1879 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);
1880 return;
1881 }
1882 // Perform length checking
1883 if (l > MAXFRAGLEN)
1884 {
1885 LOG(2, s, t, "MPPP: ERROR: very long fragment length (1)\n");
1886 return;
1887 }
1888 frag[b].fragment[seq_num - offset].length = l;
1889 memcpy(frag[b].fragment[seq_num - offset].data, p, l);
1890 }
1891 }
1892 else
1893 {
1894 LOG(4, s, t, "MPPP: Push to receive buffer\n");
1895 // Push to the receive buffer
1896 // Array indexes checking
1897 if (seq_num-offset < 0 || seq_num-offset >= MAXFRAGNUM)
1898 {
1899 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);
1900 return;
1901 }
1902 // Perform length checking
1903 if (l > MAXFRAGLEN)
1904 {
1905 LOG(2, s, t, "MPPP: ERROR: very long fragment length (2).\n");
1906 return;
1907 }
1908 frag[b].fragment[seq_num - offset].length = l;
1909 memcpy(frag[b].fragment[seq_num - offset].data, p, l);
1910 }
1911 }
1912
1913 // process IPv6 packet received
1914 //
1915 // This MUST be called with at least 4 byte behind 'p'.
1916 // (i.e. this routine writes to p[-4]).
1917 void processipv6in(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
1918 {
1919 struct in6_addr ip;
1920 in_addr_t ipv4;
1921
1922 CSTAT(processipv6in);
1923
1924 LOG_HEX(5, "IPv6", p, l);
1925
1926 ip = *(struct in6_addr *) (p + 8);
1927 ipv4 = ntohl(*(uint32_t *)(p + 16));
1928
1929 if (l > MAXETHER)
1930 {
1931 LOG(1, s, t, "IP packet too long %d\n", l);
1932 STAT(tunnel_rx_errors);
1933 return ;
1934 }
1935
1936 if (session[s].ppp.phase != Network || session[s].ppp.ipv6cp != Opened)
1937 return;
1938
1939 // no spoof
1940 if (ipv4 != session[s].ip && memcmp(&config->ipv6_prefix, &ip, 8) && sessionbyipv6(ip) != s)
1941 {
1942 char str[INET6_ADDRSTRLEN];
1943 LOG(5, s, t, "Dropping packet with spoofed IP %s\n",
1944 inet_ntop(AF_INET6, &ip, str, INET6_ADDRSTRLEN));
1945 return;
1946 }
1947
1948 // Check if it's a Router Solicition message.
1949 if (*(p + 6) == 58 && *(p + 7) == 255 && *(p + 24) == 0xFF && *(p + 25) == 2 &&
1950 *(uint32_t *)(p + 26) == 0 && *(uint32_t *)(p + 30) == 0 &&
1951 *(uint32_t *)(p + 34) == 0 &&
1952 *(p + 38) == 0 && *(p + 39) == 2 && *(p + 40) == 133) {
1953 LOG(3, s, t, "Got IPv6 RS\n");
1954 send_ipv6_ra(s, t, &ip);
1955 return;
1956 }
1957
1958 // Add on the tun header
1959 p -= 4;
1960 *(uint32_t *) p = htonl(PKTIPV6);
1961 l += 4;
1962
1963 // Are we throttled and a slave?
1964 if (session[s].tbf_in && !config->cluster_iam_master) {
1965 // Pass it to the master for handling.
1966 master_throttle_packet(session[s].tbf_in, p, l);
1967 return;
1968 }
1969
1970 // Are we throttled and a master??
1971 if (session[s].tbf_in && config->cluster_iam_master) {
1972 // Actually handle the throttled packets.
1973 tbf_queue_packet(session[s].tbf_in, p, l);
1974 return;
1975 }
1976
1977 // send to ethernet
1978 if (tun_write(p, l) < 0)
1979 {
1980 STAT(tun_tx_errors);
1981 LOG(0, s, t, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1982 l, strerror(errno), tunfd, p);
1983
1984 return;
1985 }
1986
1987 p += 4;
1988 l -= 4;
1989
1990 if (session[s].snoop_ip && session[s].snoop_port)
1991 {
1992 // Snooping this session
1993 snoop_send_packet(p, l, session[s].snoop_ip, session[s].snoop_port);
1994 }
1995
1996 increment_counter(&session[s].cin, &session[s].cin_wrap, l);
1997 session[s].cin_delta += l;
1998 session[s].pin++;
1999
2000 sess_local[s].cin += l;
2001 sess_local[s].pin++;
2002
2003 eth_tx += l;
2004
2005 STAT(tun_tx_packets);
2006 INC_STAT(tun_tx_bytes, l);
2007 }
2008
2009 //
2010 // Helper routine for the TBF filters.
2011 // Used to send queued data in from the user.
2012 //
2013 void send_ipin(sessionidt s, uint8_t *buf, int len)
2014 {
2015 LOG_HEX(5, "IP in throttled", buf, len);
2016
2017 if (write(tunfd, buf, len) < 0)
2018 {
2019 STAT(tun_tx_errors);
2020 LOG(0, 0, 0, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
2021 len, strerror(errno), tunfd, buf);
2022
2023 return;
2024 }
2025
2026 buf += 4;
2027 len -= 4;
2028
2029 if (session[s].snoop_ip && session[s].snoop_port)
2030 {
2031 // Snooping this session
2032 snoop_send_packet(buf, len, session[s].snoop_ip, session[s].snoop_port);
2033 }
2034
2035 // Increment packet counters
2036 increment_counter(&session[s].cin, &session[s].cin_wrap, len);
2037 session[s].cin_delta += len;
2038 session[s].pin++;
2039
2040 sess_local[s].cin += len;
2041 sess_local[s].pin++;
2042
2043 eth_tx += len;
2044
2045 STAT(tun_tx_packets);
2046 INC_STAT(tun_tx_bytes, len - 4);
2047 }
2048
2049
2050 // Process CCP messages
2051 void processccp(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
2052 {
2053 uint8_t b[MAXETHER];
2054 uint8_t *q;
2055
2056 CSTAT(processccp);
2057
2058 LOG_HEX(5, "CCP", p, l);
2059
2060 if (session[s].ppp.phase < Network)
2061 {
2062 LOG(2, s, t, "CCP %s ignored in %s phase\n", ppp_code(*p), ppp_phase(session[s].ppp.phase));
2063 return;
2064 }
2065
2066 if (l < 1)
2067 {
2068 LOG(1, s, t, "Short CCP packet\n");
2069 STAT(tunnel_rx_errors);
2070 }
2071
2072 LOG(4, s, t, "CCP: recv %s\n", ppp_code(*p));
2073 if (*p == ConfigAck)
2074 {
2075 switch (session[s].ppp.ccp)
2076 {
2077 case RequestSent:
2078 initialise_restart_count(s, ccp);
2079 change_state(s, ccp, AckReceived);
2080 break;
2081
2082 case AckReceived:
2083 case Opened:
2084 LOG(2, s, t, "CCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session[s].ppp.ccp));
2085 sendccp(s, t);
2086 change_state(s, ccp, RequestSent);
2087 break;
2088
2089 case AckSent:
2090 LOG(3, s, t, "CCP: Opened\n");
2091 change_state(s, ccp, Opened);
2092 break;
2093
2094 default:
2095 LOG(2, s, t, "CCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ccp));
2096 }
2097 }
2098 else if (*p == ConfigReq)
2099 {
2100 if (l < 6) // accept no compression
2101 *p = ConfigAck;
2102 else // compression requested--reject
2103 *p = ConfigRej;
2104
2105 q = makeppp(b, sizeof(b), p, l, s, t, PPPCCP, 0, 0, 0);
2106 if (!q) return;
2107
2108 switch (session[s].ppp.ccp)
2109 {
2110 case Closed:
2111 q = makeppp(b, sizeof(b), p, 2, s, t, PPPCCP, 0, 0, 0);
2112 if (!q) return;
2113 *q = TerminateAck;
2114 *((uint16_t *) (q + 2)) = htons(l = 4);
2115 break;
2116
2117 case Stopped:
2118 initialise_restart_count(s, ccp);
2119 sendccp(s, t);
2120 if (*q == ConfigAck)
2121 change_state(s, ccp, AckSent);
2122 else
2123 change_state(s, ccp, RequestSent);
2124
2125 break;
2126
2127 case RequestSent:
2128 if (*q == ConfigAck)
2129 change_state(s, ccp, AckSent);
2130
2131 break;
2132
2133 case AckReceived:
2134 if (*q == ConfigAck)
2135 change_state(s, ccp, Opened);
2136
2137 break;
2138
2139 case Opened:
2140 initialise_restart_count(s, ccp);
2141 sendccp(s, t);
2142 /* fallthrough */
2143
2144 case AckSent:
2145 if (*q == ConfigAck)
2146 change_state(s, ccp, AckSent);
2147 else
2148 change_state(s, ccp, RequestSent);
2149
2150 break;
2151
2152 default:
2153 LOG(2, s, t, "CCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ccp));
2154 return;
2155 }
2156
2157 LOG(4, s, t, "CCP: send %s\n", ppp_code(*q));
2158 tunnelsend(b, l + (q - b), t);
2159 }
2160 else if (*p == TerminateReq)
2161 {
2162 *p = TerminateAck;
2163 q = makeppp(b, sizeof(b), p, l, s, t, PPPCCP, 0, 0, 0);
2164 if (!q) return;
2165 LOG(3, s, t, "CCP: send %s\n", ppp_code(*q));
2166 tunnelsend(b, l + (q - b), t);
2167 change_state(s, ccp, Stopped);
2168 }
2169 else if (*p != CodeRej)
2170 {
2171 ppp_code_rej(s, t, PPPCCP, "CCP", p, l, b, sizeof(b));
2172 }
2173 }
2174
2175 // send a CHAP challenge
2176 void sendchap(sessionidt s, tunnelidt t)
2177 {
2178 uint8_t b[MAXETHER];
2179 uint16_t r;
2180 uint8_t *q;
2181
2182 CSTAT(sendchap);
2183
2184 r = radiusnew(s);
2185 if (!r)
2186 {
2187 LOG(1, s, t, "No RADIUS to send challenge\n");
2188 STAT(tunnel_tx_errors);
2189 return;
2190 }
2191
2192 LOG(1, s, t, "Send CHAP challenge\n");
2193
2194 radius[r].chap = 1; // CHAP not PAP
2195 radius[r].id++;
2196 if (radius[r].state != RADIUSCHAP)
2197 radius[r].try = 0;
2198
2199 radius[r].state = RADIUSCHAP;
2200 radius[r].retry = backoff(radius[r].try++);
2201 if (radius[r].try > 5)
2202 {
2203 sessionshutdown(s, "CHAP timeout.", CDN_ADMIN_DISC, TERM_REAUTHENTICATION_FAILURE);
2204 STAT(tunnel_tx_errors);
2205 return ;
2206 }
2207 q = makeppp(b, sizeof(b), 0, 0, s, t, PPPCHAP, 0, 0, 0);
2208 if (!q) return;
2209
2210 *q = 1; // challenge
2211 q[1] = radius[r].id; // ID
2212 q[4] = 16; // value size (size of challenge)
2213 memcpy(q + 5, radius[r].auth, 16); // challenge
2214 strcpy((char *) q + 21, hostname); // our name
2215 *(uint16_t *) (q + 2) = htons(strlen(hostname) + 21); // length
2216 tunnelsend(b, strlen(hostname) + 21 + (q - b), t); // send it
2217 }
2218
2219 // fill in a L2TP message with a PPP frame,
2220 // returns start of PPP frame
2221 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)
2222 {
2223 uint16_t hdr = 0x0002; // L2TP with no options
2224 uint16_t type = mtype;
2225 uint8_t *start = b;
2226
2227 if (size < 16) // Need more space than this!!
2228 {
2229 LOG(0, s, t, "makeppp buffer too small for L2TP header (size=%d)\n", size);
2230 return NULL;
2231 }
2232
2233 if (prio) hdr |= 0x0100; // set priority bit
2234
2235 *(uint16_t *) (b + 0) = htons(hdr);
2236 *(uint16_t *) (b + 2) = htons(tunnel[t].far); // tunnel
2237 *(uint16_t *) (b + 4) = htons(session[s].far); // session
2238 b += 6;
2239
2240 // Check whether this session is part of multilink
2241 if (bid)
2242 {
2243 if (bundle[bid].num_of_links > 1)
2244 type = PPPMP; // Change PPP message type to the PPPMP
2245 else
2246 bid = 0;
2247 }
2248
2249 if (type == PPPLCP || !(session[s].flags & SESSION_ACFC))
2250 {
2251 *(uint16_t *) b = htons(0xFF03); // HDLC header
2252 b += 2;
2253 }
2254
2255 if (type < 0x100 && session[s].flags & SESSION_PFC)
2256 {
2257 *b++ = type;
2258 }
2259 else
2260 {
2261 *(uint16_t *) b = htons(type);
2262 b += 2;
2263 }
2264
2265 if (bid)
2266 {
2267 // Set the sequence number and (B)egin (E)nd flags
2268 if (session[s].mssf)
2269 {
2270 // Set the multilink bits
2271 uint16_t bits_send = mp_bits;
2272 *(uint16_t *) b = htons((bundle[bid].seq_num_t & 0xFF0F)|bits_send);
2273 b += 2;
2274 }
2275 else
2276 {
2277 *(uint32_t *) b = htonl(bundle[bid].seq_num_t);
2278 // Set the multilink bits
2279 *b = mp_bits;
2280 b += 4;
2281 }
2282
2283 bundle[bid].seq_num_t++;
2284
2285 // Add the message type if this fragment has the begin bit set
2286 if (mp_bits & MP_BEGIN)
2287 {
2288 //*b++ = mtype; // The next two lines are instead of this
2289 *(uint16_t *) b = htons(mtype); // Message type
2290 b += 2;
2291 }
2292 }
2293
2294 if ((b - start) + l > size)
2295 {
2296 LOG(2, s, t, "makeppp would overflow buffer (size=%d, header+payload=%d)\n", size, (b - start) + l);
2297 return NULL;
2298 }
2299
2300 // Copy the payload
2301 if (p && l)
2302 memcpy(b, p, l);
2303
2304 return b;
2305 }
2306
2307 static int add_lcp_auth(uint8_t *b, int size, int authtype)
2308 {
2309 int len = 0;
2310 if ((authtype == AUTHCHAP && size < 5) || size < 4)
2311 return 0;
2312
2313 *b++ = 3; // Authentication-Protocol
2314 if (authtype == AUTHCHAP)
2315 {
2316 len = *b++ = 5; // length
2317 *(uint16_t *) b = htons(PPPCHAP); b += 2;
2318 *b++ = 5; // MD5
2319 }
2320 else if (authtype == AUTHPAP)
2321 {
2322 len = *b++ = 4; // length
2323 *(uint16_t *) b = htons(PPPPAP); b += 2;
2324 }
2325 else
2326 {
2327 LOG(0, 0, 0, "add_lcp_auth called with unsupported auth type %d\n", authtype);
2328 }
2329
2330 return len;
2331 }
2332
2333 // Send LCP ConfigReq for MRU, authentication type and magic no
2334 void sendlcp(sessionidt s, tunnelidt t)
2335 {
2336 uint8_t b[500], *q, *l;
2337 int authtype = sess_local[s].lcp_authtype;
2338
2339 if (!(q = makeppp(b, sizeof(b), NULL, 0, s, t, PPPLCP, 0, 0, 0)))
2340 return;
2341
2342 LOG(3, s, t, "LCP: send ConfigReq%s%s%s including MP options\n",
2343 authtype ? " (" : "",
2344 authtype ? (authtype == AUTHCHAP ? "CHAP" : "PAP") : "",
2345 authtype ? ")" : "");
2346
2347 l = q;
2348 *l++ = ConfigReq;
2349 *l++ = ++sess_local[s].lcp_ident; // ID
2350
2351 l += 2; //Save space for length
2352
2353 if (sess_local[s].ppp_mru)
2354 {
2355 *l++ = 1; *l++ = 4; // Maximum-Receive-Unit (length 4)
2356 *(uint16_t *) l = htons(sess_local[s].ppp_mru); l += 2;
2357 }
2358
2359 if (authtype)
2360 l += add_lcp_auth(l, sizeof(b) - (l - b), authtype);
2361
2362 if (session[s].magic)
2363 {
2364 *l++ = 5; *l++ = 6; // Magic-Number (length 6)
2365 *(uint32_t *) l = htonl(session[s].magic);
2366 l += 4;
2367 }
2368
2369 if (sess_local[s].mp_mrru)
2370 {
2371 *l++ = 17; *l++ = 4; // Multilink Max-Receive-Reconstructed-Unit (length 4)
2372 *(uint16_t *) l = htons(sess_local[s].mp_mrru); l += 2;
2373 }
2374
2375 if (sess_local[s].mp_epdis)
2376 {
2377 *l++ = 19; *l++ = 7; // Multilink Endpoint Discriminator (length 7)
2378 *l++ = IPADDR; // Endpoint Discriminator class
2379 *(uint32_t *) l = htonl(sess_local[s].mp_epdis);
2380 l += 4;
2381 }
2382
2383 *(uint16_t *)(q + 2) = htons(l - q); // Length
2384
2385 LOG_HEX(5, "PPPLCP", q, l - q);
2386 if (config->debug > 3) dumplcp(q, l - q);
2387
2388 tunnelsend(b, (l - b), t);
2389 restart_timer(s, lcp);
2390 }
2391
2392 // Send CCP request for no compression
2393 void sendccp(sessionidt s, tunnelidt t)
2394 {
2395 uint8_t b[500], *q;
2396
2397 if (!(q = makeppp(b, sizeof(b), NULL, 0, s, t, PPPCCP, 0, 0, 0)))
2398 return;
2399
2400 LOG(3, s, t, "CCP: send ConfigReq (no compression)\n");
2401
2402 *q = ConfigReq;
2403 *(q + 1) = ++sess_local[s].lcp_ident; // ID
2404 *(uint16_t *)(q + 2) = htons(4); // Length
2405
2406 LOG_HEX(5, "PPPCCP", q, 4);
2407 tunnelsend(b, (q - b) + 4 , t);
2408 restart_timer(s, ccp);
2409 }
2410
2411 // Reject unknown/unconfigured protocols
2412 void protoreject(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l, uint16_t proto)
2413 {
2414
2415 uint8_t buf[MAXETHER];
2416 uint8_t *q;
2417 int mru = session[s].mru;
2418 if (mru < MINMTU) mru = MINMTU;
2419 if (mru > sizeof(buf)) mru = sizeof(buf);
2420
2421 l += 6;
2422 if (l > mru) l = mru;
2423
2424 q = makeppp(buf, sizeof(buf), 0, 0, s, t, PPPLCP, 0, 0, 0);
2425 if (!q) return;
2426
2427 *q = ProtocolRej;
2428 *(q + 1) = ++sess_local[s].lcp_ident;
2429 *(uint16_t *)(q + 2) = htons(l);
2430 *(uint16_t *)(q + 4) = htons(proto);
2431 memcpy(q + 6, p, l - 6);
2432
2433 if (proto == PPPIPV6CP)
2434 LOG(3, s, t, "LCP: send ProtocolRej (IPV6CP: not configured)\n");
2435 else
2436 LOG(2, s, t, "LCP: sent ProtocolRej (0x%04X: unsupported)\n", proto);
2437
2438 tunnelsend(buf, l + (q - buf), t);
2439 }