962184196a42fd948bdecb244772c0f8194a4710
[l2tpns.git] / ppp.c
1 // L2TPNS PPP Stuff
2 // $Id: ppp.c,v 1.5 2004/06/23 03:52:24 fred_nerk Exp $
3
4 #include <stdio.h>
5 #include <string.h>
6 #include <unistd.h>
7 #include <errno.h>
8 #include <stdlib.h>
9 #include "l2tpns.h"
10 #include "constants.h"
11 #include "plugin.h"
12 #include "util.h"
13 #include "tbf.h"
14 #include "cluster.h"
15
16 extern tunnelt *tunnel;
17 extern sessiont *session;
18 extern radiust *radius;
19 extern int tapfd;
20 extern char hostname[];
21 extern u32 eth_tx;
22 extern time_t time_now;
23 extern struct configt *config;
24
25 // Process PAP messages
26 void processpap(tunnelidt t, sessionidt s, u8 * p, u16 l)
27 {
28 char user[129];
29 char pass[129];
30
31 #ifdef STAT_CALLS
32 STAT(call_processpap);
33 #endif
34 log_hex(5, "PAP", p, l);
35 if (l < 4)
36 {
37 log(1, 0, s, t, "Short PAP %u bytes", l);
38 STAT(tunnel_rx_errors);
39 return ;
40 }
41 if (*p != 1)
42 {
43 log(1, 0, s, t, "Unexpected PAP code %d\n", *p);
44 STAT(tunnel_rx_errors);
45 return ;
46 }
47 if (ntohs(*(u16 *) (p + 2)) > l)
48 {
49 log(1, 0, s, t, "Length mismatch PAP %d/%d\n", ntohs(*(u16 *) (p + 2)), l);
50 STAT(tunnel_rx_errors);
51 return ;
52 }
53 {
54 u8 *b = p;
55 b += 4;
56 if (*b && *b < sizeof(user))
57 memcpy(user, b + 1, *b);
58 user[*b] = 0;
59 b += 1 + *b;
60 if (*b && *b < sizeof(pass))
61 memcpy(pass, b + 1, *b);
62 pass[*b] = 0;
63 log(3, 0, s, t, "PAP login %s/%s\n", user, pass);
64 }
65 if (session[s].ip || !session[s].radius)
66 {
67 // respond now, either no RADIUS available or already authenticated
68 u8 b[MAXCONTROL];
69 u8 id = p[1];
70 u8 *p = makeppp(b, sizeof(b), 0, 0, t, s, PPPPAP);
71 if (!p) { // Failed to make ppp header!
72 log(1,0,0,0, "Failed to make PPP header in process pap!\n");
73 return;
74 }
75 if (session[s].ip)
76 *p = 2; // ACK
77 else
78 *p = 3; // cant authorise
79 p[1] = id;
80 *(u16 *) (p + 2) = htons(5); // length
81 p[4] = 0; // no message
82 if (session[s].ip)
83 {
84 log(3, session[s].ip, s, t, "%d Already an IP allocated: %s (%d)\n", getpid(), inet_toa(htonl(session[s].ip)), session[s].ip_pool_index);
85 }
86 else
87 {
88 log(1, 0, s, t, "No radius session available to authenticate session...\n");
89 }
90 log(3, 0, s, t, "Fallback response to PAP (%s)\n", (session[s].ip) ? "ACK" : "NAK");
91 tunnelsend(b, 5 + (p - b), t); // send it
92 }
93 else
94 { // set up RADIUS request
95 u16 r = session[s].radius;
96
97 // Run PRE_AUTH plugins
98 struct param_pre_auth packet = { &tunnel[t], &session[s], strdup(user), strdup(pass), PPPPAP, 1 };
99 run_plugins(PLUGIN_PRE_AUTH, &packet);
100 if (!packet.continue_auth)
101 {
102 log(3, 0, s, t, "A plugin rejected PRE_AUTH\n");
103 if (packet.username) free(packet.username);
104 if (packet.password) free(packet.password);
105 return;
106 }
107
108 strncpy(session[s].user, packet.username, sizeof(session[s].user) - 1);
109 strncpy(radius[r].pass, packet.password, sizeof(radius[r].pass) - 1);
110
111 free(packet.username);
112 free(packet.password);
113
114 radius[r].id = p[1];
115 log(3, 0, s, t, "Sending login for %s/%s to radius\n", user, pass);
116 radiussend(r, RADIUSAUTH);
117 }
118 }
119
120 // Process CHAP messages
121 void processchap(tunnelidt t, sessionidt s, u8 * p, u16 l)
122 {
123 u16 r;
124 u16 len;
125
126 #ifdef STAT_CALLS
127 STAT(call_processchap);
128 #endif
129 log_hex(5, "CHAP", p, l);
130 r = session[s].radius;
131 if (!r)
132 {
133 log(1, 0, s, t, "Unexpected CHAP message\n");
134
135 // FIXME: Need to drop the session here.
136
137 STAT(tunnel_rx_errors);
138 return;
139 }
140 if (*p != 2)
141 {
142 log(1, 0, s, t, "Unexpected CHAP response code %d\n", *p);
143 STAT(tunnel_rx_errors);
144 return;
145 }
146 if (p[1] != radius[r].id)
147 {
148 log(1, 0, s, t, "Wrong CHAP response ID %d (should be %d) (%d)\n", p[1], radius[r].id, r);
149 STAT(tunnel_rx_errors);
150 return ;
151 }
152 len = ntohs(*(u16 *) (p + 2));
153 if (len > l)
154 {
155 log(1, 0, s, t, "Bad CHAP length %d\n", len);
156 STAT(tunnel_rx_errors);
157 return ;
158 }
159 if (p[4] != 16)
160 {
161 log(1, 0, s, t, "Bad CHAP response length %d\n", p[4]);
162 STAT(tunnel_rx_errors);
163 return ;
164 }
165 if (len - 21 >= sizeof(session[s].user))
166 {
167 log(1, 0, s, t, "CHAP user too long %d\n", len - 21);
168 STAT(tunnel_rx_errors);
169 return ;
170 }
171
172 // Run PRE_AUTH plugins
173 {
174 struct param_pre_auth packet = { &tunnel[t], &session[s], NULL, NULL, PPPCHAP, 1 };
175
176 packet.username = calloc(len-20, 1);
177 packet.password = calloc(16, 1);
178 memcpy(packet.username, p + 21, len - 21);
179 memcpy(packet.password, p + 5, 16);
180
181 run_plugins(PLUGIN_PRE_AUTH, &packet);
182 if (!packet.continue_auth)
183 {
184 log(3, 0, s, t, "A plugin rejected PRE_AUTH\n");
185 if (packet.username) free(packet.username);
186 if (packet.password) free(packet.password);
187 return;
188 }
189
190 strncpy(session[s].user, packet.username, sizeof(session[s].user) - 1);
191 memcpy(radius[r].pass, packet.password, 16);
192
193 free(packet.username);
194 free(packet.password);
195 }
196
197 radius[r].chap = 1;
198 log(3, 0, s, t, "CHAP login %s\n", session[s].user);
199 radiussend(r, RADIUSAUTH);
200 }
201
202 char *ppp_lcp_types[] = {
203 NULL,
204 "ConfigReq",
205 "ConfigAck",
206 "ConfigNak",
207 "ConfigRej",
208 "TerminateReq",
209 "TerminateAck",
210 "CodeRej",
211 "ProtocolRej",
212 "EchoReq",
213 "EchoReply",
214 "DiscardRequest",
215 };
216
217 void dumplcp(u8 *p, int l)
218 {
219 signed int x = l - 4;
220 u8 *o = (p + 4);
221
222 log_hex(5, "PPP LCP Packet", p, l);
223 log(4, 0, 0, 0, "PPP LCP Packet type %d (%s len %d)\n", *p, ppp_lcp_types[(int)*p], ntohs( ((u16 *) p)[1]) );
224 log(4, 0, 0, 0, "Length: %d\n", l);
225 if (*p != ConfigReq && *p != ConfigRej && *p != ConfigAck)
226 return;
227
228 while (x > 2)
229 {
230 int type = o[0];
231 int length = o[1];
232 if (length == 0)
233 {
234 log(4, 0, 0, 0, " Option length is 0...\n");
235 break;
236 }
237 if (type == 0)
238 {
239 log(4, 0, 0, 0, " Option type is 0...\n");
240 x -= length;
241 o += length;
242 continue;
243 }
244 switch (type)
245 {
246 case 1: // Maximum-Receive-Unit
247 log(4, 0, 0, 0, " %s %d\n", lcp_types[type], ntohs(*(u16 *)(o + 2)));
248 break;
249 case 3: // Authentication-Protocol
250 {
251 int proto = ntohs(*(u16 *)(o + 2));
252 log(4, 0, 0, 0, " %s %s\n", lcp_types[type],
253 proto == 0xC223 ? "CHAP" : "PAP");
254 break;
255 }
256 case 4: // Quality-Protocol
257 {
258 u32 qp = ntohl(*(u32 *)(o + 2));
259 log(4, 0, 0, 0, " %s %x\n", lcp_types[type], qp);
260 break;
261 }
262 case 5: // Magic-Number
263 {
264 u32 magicno = ntohl(*(u32 *)(o + 2));
265 log(4, 0, 0, 0, " %s %x\n", lcp_types[type], magicno);
266 break;
267 }
268 case 7: // Protocol-Field-Compression
269 {
270 u32 pfc = ntohl(*(u32 *)(o + 2));
271 log(4, 0, 0, 0, " %s %x\n", lcp_types[type], pfc);
272 break;
273 }
274 case 8: // Address-And-Control-Field-Compression
275 {
276 u32 afc = ntohl(*(u32 *)(o + 2));
277 log(4, 0, 0, 0, " %s %x\n", lcp_types[type], afc);
278 break;
279 }
280 default:
281 log(2, 0, 0, 0, " Unknown PPP LCP Option type %d\n", type);
282 break;
283 }
284 x -= length;
285 o += length;
286 }
287 }
288
289 // Process LCP messages
290 void processlcp(tunnelidt t, sessionidt s, u8 * p, u16 l)
291 {
292 u8 b[MAXCONTROL];
293 u8 *q = NULL;
294
295 #ifdef STAT_CALLS
296 STAT(call_processlcp);
297 #endif
298 log_hex(5, "LCP", p, l);
299 if (l < 4)
300 {
301 log(1, session[s].ip, s, t, "Short LCP %d bytes", l);
302 STAT(tunnel_rx_errors);
303 return ;
304 }
305 if (*p == ConfigAck)
306 {
307 log(3, session[s].ip, s, t, "LCP: Discarding ConfigAck\n");
308 }
309 else if (*p == ConfigReq)
310 {
311 signed int x = l - 4;
312 u8 *o = (p + 4);
313
314 log(3, session[s].ip, s, t, "LCP: ConfigReq (%d bytes)...\n", l);
315 dumplcp(p, l);
316
317 while (x > 2)
318 {
319 int type = o[0];
320 int length = o[1];
321 if (length == 0 || type == 0) break;
322 switch (type)
323 {
324 case 1: // Maximum-Receive-Unit
325 session[s].mru = ntohs(*(u16 *)(o + 2));
326 break;
327 case 3: // Authentication-Protocol
328 {
329 int proto = ntohs(*(u16 *)(o + 2));
330 if (proto == 0xC223)
331 {
332 log(2, session[s].ip, s, t, " Remote end is trying to do CHAP. Rejecting it.\n");
333
334 if (!q)
335 {
336 q = makeppp(b, sizeof(b), p, l, t, s, PPPLCP);
337 if (!q) {
338 log(2, session[s].ip, s, t, " Failed to send packet.\n");
339 break;
340 }
341 *q++ = ConfigNak;
342 }
343 memcpy(q, o, length);
344 *(u16 *)(q += 2) = htons(0xC023); // NAK -> Use PAP instead
345 q += length;
346 }
347 break;
348 }
349 case 5: // Magic-Number
350 {
351 // u32 magicno = ntohl(*(u32 *)(o + 2));
352 break;
353 }
354 case 4: // Quality-Protocol
355 {
356 // u32 qp = ntohl(*(u32 *)(o + 2));
357 break;
358 }
359 case 7: // Protocol-Field-Compression
360 {
361 // u32 pfc = ntohl(*(u32 *)(o + 2));
362 break;
363 }
364 case 8: // Address-And-Control-Field-Compression
365 {
366 // u32 afc = ntohl(*(u32 *)(o + 2));
367 break;
368 }
369 default:
370 log(2, session[s].ip, s, t, " Unknown PPP LCP Option type %d\n", type);
371 break;
372 }
373 x -= length;
374 o += length;
375 }
376
377 if (!q)
378 {
379 // Send back a ConfigAck
380 log(3, session[s].ip, s, t, "ConfigReq accepted, sending as Ack\n");
381 q = makeppp(b, sizeof(b), p, l, t, s, PPPLCP);
382 if (!q) {
383 log(3, session[s].ip, s, t, " failed to create packet.\n");
384 return;
385 }
386 *q = ConfigAck;
387 tunnelsend(b, l + (q - b), t);
388 }
389 else
390 {
391 // Already built a ConfigNak... send it
392 log(3, session[s].ip, s, t, "Sending ConfigNak\n");
393 tunnelsend(b, l + (q - b), t);
394
395 log(3, session[s].ip, s, t, "Sending ConfigReq, requesting PAP login\n");
396 q = makeppp(b, sizeof(b), NULL, 0, t, s, PPPLCP);
397 *q++ = ConfigReq;
398 *(u8 *)(q++) = 3;
399 *(u8 *)(q++) = 4;
400 *(u16 *)(q += 2) = htons(0xC023);
401 tunnelsend(b, l + (q - b), t);
402 }
403 }
404 else if (*p == ConfigNak)
405 {
406 log(1, session[s].ip, s, t, "Remote end sent a ConfigNak. Ignoring\n");
407 dumplcp(p, l);
408 return ;
409 }
410 else if (*p == TerminateReq)
411 {
412 *p = TerminateAck; // close
413 q = makeppp(b, sizeof(b), p, l, t, s, PPPLCP);
414 if (!q) {
415 log(3, session[s].ip, s, t, "Failed to create PPP packet in processlcp.\n");
416 return;
417 }
418 log(3, session[s].ip, s, t, "LCP: Received TerminateReq. Sending TerminateAck\n");
419 sessionshutdown(s, "Remote end closed connection.");
420 tunnelsend(b, l + (q - b), t); // send it
421 }
422 else if (*p == TerminateReq)
423 {
424 sessionshutdown(s, "Remote end closed connection.");
425 }
426 else if (*p == EchoReq)
427 {
428 *p = EchoReply; // reply
429 *(u32 *) (p + 4) = htonl(session[s].magic); // our magic number
430 q = makeppp(b, sizeof(b), p, l, t, s, PPPLCP);
431 if (!q) {
432 log(3, session[s].ip, s, t, " failed to send EchoReply.\n");
433 return;
434 }
435 log(5, session[s].ip, s, t, "LCP: Received EchoReq. Sending EchoReply\n");
436 tunnelsend(b, l + (q - b), t); // send it
437 }
438 else if (*p == EchoReply)
439 {
440 // Ignore it, last_packet time is set earlier than this.
441 }
442 else
443 {
444 log(1, session[s].ip, s, t, "Unexpected LCP code %d\n", *p);
445 STAT(tunnel_rx_errors);
446 return ;
447 }
448 }
449
450 // Process IPCP messages
451 void processipcp(tunnelidt t, sessionidt s, u8 * p, u16 l)
452 {
453 #ifdef STAT_CALLS
454 STAT(call_processipcp);
455 #endif
456 log_hex(5, "IPCP", p, l);
457 if (l < 5)
458 {
459 log(1, 0, s, t, "Short IPCP %d bytes", l);
460 STAT(tunnel_rx_errors);
461 return ;
462 }
463 if (*p == ConfigAck)
464 { // happy with our IPCP
465 u16 r = session[s].radius;
466 if ((!r || radius[r].state == RADIUSIPCP) && !session[s].walled_garden) {
467 if (!r)
468 r = radiusnew(s);
469 if (r)
470 radiussend(r, RADIUSSTART); // send radius start, having got IPCP at last
471 }
472 session[s].flags |= SF_IPCP_ACKED;
473
474 return ; // done
475 }
476 if (*p != ConfigReq)
477 {
478 log(1, 0, s, t, "Unexpected IPCP code %d\n", *p);
479 STAT(tunnel_rx_errors);
480 return ;
481 }
482 if (ntohs(*(u16 *) (p + 2)) > l)
483 {
484 log(1, 0, s, t, "Length mismatch IPCP %d/%d\n", ntohs(*(u16 *) (p + 2)), l);
485 STAT(tunnel_rx_errors);
486 return ;
487 }
488 if (!session[s].ip)
489 {
490 log(3, 0, s, t, "Waiting on radius reply\n");
491 return ; // have to wait on RADIUS reply
492 }
493 // form a config reply quoting the IP in the session
494 {
495 u8 b[MAXCONTROL];
496 u8 *i,
497 *q;
498
499 q = p + 4;
500 i = p + l;
501 while (q < i && q[1])
502 {
503 if (*q != 0x81 && *q != 0x83 && *q != 3)
504 break;
505 q += q[1];
506 }
507 if (q < i)
508 { // reject
509 u16 n = 4;
510 i = p + l;
511 q = makeppp(b, sizeof(b), p, l, t, s, PPPIPCP);
512 if (!q) {
513 log(2, 0, s, t, "Failed to send IPCP.\n");
514 return;
515 }
516 *q = ConfigRej;
517 p += 4;
518 while (p < i && p[1])
519 {
520 if (*p != 0x81 && *p != 0x83 && *p != 3)
521 {
522 log(2, 0, s, t, "IPCP reject %d\n", *p);
523 memcpy(q + n, p, p[1]);
524 n += p[1];
525 }
526 p += p[1];
527 }
528 *(u16 *) (q + 2) = htons(n);
529 tunnelsend(b, n + (q - b), t); // send it
530 }
531 else
532 {
533 *p = ConfigAck;
534 i = findppp(p, 0x81); // Primary DNS address
535 if (i)
536 {
537 if (*(u32 *) (i + 2) != htonl(session[s].dns1))
538 {
539 *(u32 *) (i + 2) = htonl(session[s].dns1);
540 *p = ConfigNak;
541 }
542 }
543 i = findppp(p, 0x83); // Secondary DNS address (TBA, is it)
544 if (i)
545 {
546 if (*(u32 *) (i + 2) != htonl(session[s].dns2))
547 {
548 *(u32 *) (i + 2) = htonl(session[s].dns2);
549 *p = ConfigNak;
550 }
551 }
552 i = findppp(p, 3); // IP address
553 if (!i || i[1] != 6)
554 {
555 log(1, 0, s, t, "No IP in IPCP request\n");
556 STAT(tunnel_rx_errors);
557 return ;
558 }
559 if (*(u32 *) (i + 2) != htonl(session[s].ip))
560 {
561 *(u32 *) (i + 2) = htonl(session[s].ip);
562 *p = ConfigNak;
563 }
564 q = makeppp(b, sizeof(b), p, l, t, s, PPPIPCP);
565 if (!q) {
566 log(2, 0, s, t, " Failed to send IPCP packet.\n");
567 return;
568 }
569 tunnelsend(b, l + (q - b), t); // send it
570 }
571 }
572 }
573
574 // process IP packet received
575 //
576 // This MUST be called with at least 4 byte behind 'p'.
577 // (i.e. this routine writes to p[-4]).
578 void processipin(tunnelidt t, sessionidt s, u8 * p, u16 l)
579 {
580 ipt ip;
581
582 #ifdef STAT_CALLS
583 STAT(call_processipin);
584 #endif
585 log_hex(5, "IP", p, l);
586
587 ip = ntohl(*(u32 *)(p + 12));
588
589 if (l > MAXETHER)
590 {
591 log(1, ip, s, t, "IP packet too long %d\n", l);
592 STAT(tunnel_rx_errors);
593 return ;
594 }
595
596 // no spoof (do sessionbyip to handled statically routed subnets)
597 if (ip != session[s].ip && sessionbyip(htonl(ip)) != s)
598 {
599 log(5, ip, s, t, "Dropping packet with spoofed IP %s\n", inet_toa(htonl(ip)));
600 return;
601 }
602
603 // Add on the tun header
604 p -= 4;
605 *(u32 *)p = htonl(0x00000800);
606 l += 4;
607
608 if (session[s].tbf_in && !config->cluster_iam_master) { // Are we throttled and a slave?
609 master_throttle_packet(session[s].tbf_in, p, l); // Pass it to the master for handling.
610 return;
611 }
612
613 session[s].cin += l - 4;
614 session[s].total_cin += l - 4;
615 sess_count[s].cin += l - 4;
616
617 session[s].pin++;
618 eth_tx += l - 4;
619
620 if (session[s].snoop_ip && session[s].snoop_port)
621 {
622 // Snooping this session, send it to ASIO
623 snoop_send_packet(p, l, session[s].snoop_ip, session[s].snoop_port);
624 }
625 STAT(tap_tx_packets);
626 INC_STAT(tap_tx_bytes, l);
627
628 if (session[s].tbf_in && config->cluster_iam_master) { // Are we throttled and a master?? actually handle the throttled packets.
629 tbf_queue_packet(session[s].tbf_in, p, l);
630 return;
631 }
632
633 // send to ethernet
634 if (tun_write(p, l) < 0)
635 {
636 STAT(tap_tx_errors);
637 log(0, 0, s, t, "Error writing %d bytes to TAP device: %s (tapfd=%d, p=%p)\n",
638 l, strerror(errno), tapfd, p);
639 }
640
641 }
642
643 //
644 // Helper routine for the TBF filters.
645 // Used to send queued data in from the user.
646 //
647 void send_ipin(sessionidt s, u8 *buf, int len)
648 {
649 log_hex(5, "IP in throttled", buf, len);
650 if (write(tapfd, buf, len) < 0)
651 {
652 STAT(tap_tx_errors);
653 log(0, 0, 0, 0, "Error writing %d bytes to TAP device: %s (tapfd=%d, p=%p)\n",
654 len, strerror(errno), tapfd, buf);
655 }
656
657 // Increment packet counters
658 session[s].cin += len - 4;
659 session[s].total_cin += len - 4;
660 sess_count[s].cin += len - 4;
661
662 session[s].pin++;
663 eth_tx += len - 4;
664 }
665
666
667 // Process LCP messages
668 void processccp(tunnelidt t, sessionidt s, u8 * p, u16 l)
669 {
670 #ifdef STAT_CALLS
671 STAT(call_processccp);
672 #endif
673 log_hex(5, "CCP", p, l);
674 if (l < 2 || (*p != ConfigReq && *p != TerminateReq))
675 {
676 log(1, 0, s, t, "Unexpecetd CCP request code %d\n", *p);
677 STAT(tunnel_rx_errors);
678 return ;
679 }
680 // reject
681 {
682 u8 b[MAXCONTROL];
683 u8 *q;
684 if (*p == ConfigReq)
685 {
686 if (l < 6)
687 {
688 *p = ConfigAck; // accept no compression
689 }
690 else
691 {
692 *p = ConfigRej; // reject
693 }
694 }
695 else
696 *p = TerminateAck; // close
697 q = makeppp(b, sizeof(b), p, l, t, s, PPPCCP);
698 if (!q) {
699 log(1,0,0,0, "Failed to send CCP packet.\n");
700 return;
701 }
702 tunnelsend(b, l + (q - b), t); // send it
703 }
704 }
705
706 // send a CHAP PP packet
707 void sendchap(tunnelidt t, sessionidt s)
708 {
709 u8 b[MAXCONTROL];
710 u16 r = session[s].radius;
711 u8 *q;
712 #ifdef STAT_CALLS
713 STAT(call_sendchap);
714 #endif
715 if (!r)
716 {
717 log(1, 0, s, t, "No RADIUS to send challenge\n");
718 STAT(tunnel_tx_errors);
719 return ;
720 }
721 log(1, 0, s, t, "Send CHAP challenge\n");
722 { // new challenge
723 int n;
724 for (n = 0; n < 15; n++)
725 radius[r].auth[n] = rand();
726 }
727 radius[r].chap = 1; // CHAP not PAP
728 radius[r].id++;
729 if (radius[r].state != RADIUSCHAP)
730 radius[r].try = 0;
731 radius[r].state = RADIUSCHAP;
732 radius[r].retry = backoff(radius[r].try++);
733 if (radius[r].try > 5)
734 {
735 sessionshutdown(s, "Timeout CHAP");
736 STAT(tunnel_tx_errors);
737 return ;
738 }
739 q = makeppp(b, sizeof(b), 0, 0, t, s, PPPCHAP);
740 if (!q) {
741 log(1, 0, s, t, "failed to send CHAP challenge.\n");
742 return;
743 }
744 *q = 1; // challenhe
745 q[1] = radius[r].id; // ID
746 q[4] = 16; // length
747 memcpy(q + 5, radius[r].auth, 16); // challenge
748 strcpy(q + 21, hostname); // our name
749 *(u16 *) (q + 2) = htons(strlen(hostname) + 21); // length
750 tunnelsend(b, strlen(hostname) + 21 + (q - b), t); // send it
751 }
752
753 // fill in a L2TP message with a PPP frame,
754 // copies existing PPP message and changes magic number if seen
755 // returns start of PPP frame
756 u8 *makeppp(u8 * b, int size, u8 * p, int l, tunnelidt t, sessionidt s, u16 mtype)
757 {
758
759 if (size < 12)
760 return NULL; // Need more space than this!!
761
762 *(u16 *) (b + 0) = htons(0x0002); // L2TP with no options
763 *(u16 *) (b + 2) = htons(tunnel[t].far); // tunnel
764 *(u16 *) (b + 4) = htons(session[s].far); // session
765 b += 6;
766 if (mtype != PPPLCP && !(session[s].l2tp_flags & SESSIONACFC))
767 {
768 *(u16 *) b = htons(0xFF03); // HDLC header
769 b += 2;
770 }
771 if (mtype < 0x100 && session[s].l2tp_flags & SESSIONPFC)
772 *b++ = mtype;
773 else
774 {
775 *(u16 *) b = htons(mtype);
776 b += 2;
777 }
778
779 if (l + 12 > size) {
780 log(3,0,0,0, "Would have overflowed the buffer in makeppp: size %d, len %d.\n", size, l);
781 return NULL; // Run out of room to hold the packet!
782 }
783 if (p && l)
784 memcpy(b, p, l);
785 return b;
786 }
787
788 // find a PPP option, returns point to option, or 0 if not found
789 u8 *findppp(u8 * b, u8 mtype)
790 {
791 u16 l = ntohs(*(u16 *) (b + 2));
792 if (l < 4)
793 return 0;
794 b += 4;
795 l -= 4;
796 while (l)
797 {
798 if (l < b[1] || !b[1])
799 return 0; // faulty
800 if (*b == mtype)
801 return b;
802 l -= b[1];
803 b += b[1];
804 }
805 return 0;
806 }
807
808 // Send initial LCP ConfigReq
809 void initlcp(tunnelidt t, sessionidt s)
810 {
811 char b[500] = {0}, *q;
812
813 q = makeppp(b, sizeof(b), NULL, 0, t, s, PPPLCP);
814 if (!q) {
815 log(1, 0, s, t, "Failed to send LCP ConfigReq.\n");
816 return;
817 }
818 log(4, 0, s, t, "Sending LCP ConfigReq for PAP\n");
819 *q = ConfigReq;
820 *(u8 *)(q + 1) = (time_now % 255) + 1; // ID
821 *(u16 *)(q + 2) = htons(8); // Length
822 *(u8 *)(q + 4) = 3;
823 *(u8 *)(q + 5) = 4;
824 *(u16 *)(q + 6) = htons(0xC023); // PAP
825 tunnelsend(b, 12 + 8, t);
826 }
827