3 char const *cvs_id_ppp
= "$Id: ppp.c,v 1.39.2.1 2005/01/13 07:58:54 bodea Exp $";
11 #include "constants.h"
17 extern tunnelt
*tunnel
;
18 extern sessiont
*session
;
19 extern radiust
*radius
;
21 extern char hostname
[];
22 extern uint32_t eth_tx
;
23 extern time_t time_now
;
24 extern configt
*config
;
26 static void initccp(tunnelidt t
, sessionidt s
);
28 // Process PAP messages
29 void processpap(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
35 CSTAT(call_processpap
);
37 LOG_HEX(5, "PAP", p
, l
);
40 LOG(1, s
, t
, "Short PAP %u bytes\n", l
);
41 STAT(tunnel_rx_errors
);
45 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
47 LOG(1, s
, t
, "Length mismatch PAP %u/%u\n", hl
, l
);
48 STAT(tunnel_rx_errors
);
55 LOG(1, s
, t
, "Unexpected PAP code %d\n", *p
);
56 STAT(tunnel_rx_errors
);
63 if (*b
&& *b
< sizeof(user
))
64 memcpy(user
, b
+ 1, *b
);
67 if (*b
&& *b
< sizeof(pass
))
68 memcpy(pass
, b
+ 1, *b
);
70 LOG(3, s
, t
, "PAP login %s/%s\n", user
, pass
);
72 if (session
[s
].ip
|| !session
[s
].radius
)
74 // respond now, either no RADIUS available or already authenticated
75 uint8_t b
[MAXCONTROL
];
77 uint8_t *p
= makeppp(b
, sizeof(b
), 0, 0, t
, s
, PPPPAP
);
83 *p
= 3; // cant authorise
85 *(uint16_t *) (p
+ 2) = htons(5); // length
86 p
[4] = 0; // no message
89 LOG(3, s
, t
, "Already an IP allocated: %s (%d)\n",
90 fmtaddr(htonl(session
[s
].ip
), 0), session
[s
].ip_pool_index
);
92 session
[s
].flags
&= ~SF_IPCP_ACKED
;
96 LOG(1, s
, t
, "No radius session available to authenticate session...\n");
98 LOG(3, s
, t
, "Fallback response to PAP (%s)\n", (session
[s
].ip
) ? "ACK" : "NAK");
99 tunnelsend(b
, 5 + (p
- b
), t
); // send it
103 // set up RADIUS request
104 uint16_t r
= session
[s
].radius
;
106 // Run PRE_AUTH plugins
107 struct param_pre_auth packet
= { &tunnel
[t
], &session
[s
], strdup(user
), strdup(pass
), PPPPAP
, 1 };
108 run_plugins(PLUGIN_PRE_AUTH
, &packet
);
109 if (!packet
.continue_auth
)
111 LOG(3, s
, t
, "A plugin rejected PRE_AUTH\n");
112 if (packet
.username
) free(packet
.username
);
113 if (packet
.password
) free(packet
.password
);
117 strncpy(session
[s
].user
, packet
.username
, sizeof(session
[s
].user
) - 1);
118 strncpy(radius
[r
].pass
, packet
.password
, sizeof(radius
[r
].pass
) - 1);
120 free(packet
.username
);
121 free(packet
.password
);
124 LOG(3, s
, t
, "Sending login for %s/%s to radius\n", user
, pass
);
125 radiussend(r
, RADIUSAUTH
);
129 // Process CHAP messages
130 void processchap(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
135 CSTAT(call_processchap
);
137 LOG_HEX(5, "CHAP", p
, l
);
138 r
= session
[s
].radius
;
141 LOG(1, s
, t
, "Unexpected CHAP message\n");
143 // FIXME: Need to drop the session here.
145 STAT(tunnel_rx_errors
);
151 LOG(1, s
, t
, "Short CHAP %u bytes\n", l
);
152 STAT(tunnel_rx_errors
);
156 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
158 LOG(1, s
, t
, "Length mismatch CHAP %u/%u\n", hl
, l
);
159 STAT(tunnel_rx_errors
);
166 LOG(1, s
, t
, "Unexpected CHAP response code %d\n", *p
);
167 STAT(tunnel_rx_errors
);
170 if (p
[1] != radius
[r
].id
)
172 LOG(1, s
, t
, "Wrong CHAP response ID %d (should be %d) (%d)\n", p
[1], radius
[r
].id
, r
);
173 STAT(tunnel_rx_errors
);
177 if (l
< 5 || p
[4] != 16)
179 LOG(1, s
, t
, "Bad CHAP response length %d\n", l
< 5 ? -1 : p
[4]);
180 STAT(tunnel_rx_errors
);
186 if (l
< 16 || l
- 16 >= sizeof(session
[s
].user
))
188 LOG(1, s
, t
, "CHAP user too long %d\n", l
- 16);
189 STAT(tunnel_rx_errors
);
193 // Run PRE_AUTH plugins
195 struct param_pre_auth packet
= { &tunnel
[t
], &session
[s
], NULL
, NULL
, PPPCHAP
, 1 };
197 packet
.password
= calloc(17, 1);
198 memcpy(packet
.password
, p
, 16);
203 packet
.username
= calloc(l
+ 1, 1);
204 memcpy(packet
.username
, p
, l
);
206 run_plugins(PLUGIN_PRE_AUTH
, &packet
);
207 if (!packet
.continue_auth
)
209 LOG(3, s
, t
, "A plugin rejected PRE_AUTH\n");
210 if (packet
.username
) free(packet
.username
);
211 if (packet
.password
) free(packet
.password
);
215 strncpy(session
[s
].user
, packet
.username
, sizeof(session
[s
].user
) - 1);
216 memcpy(radius
[r
].pass
, packet
.password
, 16);
218 free(packet
.username
);
219 free(packet
.password
);
223 LOG(3, s
, t
, "CHAP login %s\n", session
[s
].user
);
224 radiussend(r
, RADIUSAUTH
);
227 static char *ppp_lcp_types
[] = {
243 static void dumplcp(uint8_t *p
, int l
)
246 uint8_t *o
= (p
+ 4);
248 LOG_HEX(5, "PPP LCP Packet", p
, l
);
249 LOG(4, 0, 0, "PPP LCP Packet type %d (%s len %d)\n", *p
, ppp_lcp_types
[(int)*p
], ntohs( ((uint16_t *) p
)[1]) );
250 LOG(4, 0, 0, "Length: %d\n", l
);
251 if (*p
!= ConfigReq
&& *p
!= ConfigRej
&& *p
!= ConfigAck
)
260 LOG(4, 0, 0, " Option length is %d...\n", length
);
265 LOG(4, 0, 0, " Option type is 0...\n");
272 case 1: // Maximum-Receive-Unit
274 LOG(4, 0, 0, " %s %d\n", lcp_types
[type
], ntohs(*(uint16_t *)(o
+ 2)));
276 LOG(4, 0, 0, " %s odd length %d\n", lcp_types
[type
], length
);
278 case 2: // Async-Control-Character-Map
281 uint32_t asyncmap
= ntohl(*(uint32_t *)(o
+ 2));
282 LOG(4, 0, 0, " %s %x\n", lcp_types
[type
], asyncmap
);
285 LOG(4, 0, 0, " %s odd length %d\n", lcp_types
[type
], length
);
287 case 3: // Authentication-Protocol
290 int proto
= ntohs(*(uint16_t *)(o
+ 2));
291 LOG(4, 0, 0, " %s 0x%x (%s)\n", lcp_types
[type
], proto
,
292 proto
== PPPCHAP
? "CHAP" :
293 proto
== PPPPAP
? "PAP" : "UNKNOWN");
296 LOG(4, 0, 0, " %s odd length %d\n", lcp_types
[type
], length
);
298 case 4: // Quality-Protocol
300 uint32_t qp
= ntohl(*(uint32_t *)(o
+ 2));
301 LOG(4, 0, 0, " %s %x\n", lcp_types
[type
], qp
);
304 case 5: // Magic-Number
307 uint32_t magicno
= ntohl(*(uint32_t *)(o
+ 2));
308 LOG(4, 0, 0, " %s %x\n", lcp_types
[type
], magicno
);
311 LOG(4, 0, 0, " %s odd length %d\n", lcp_types
[type
], length
);
313 case 7: // Protocol-Field-Compression
314 case 8: // Address-And-Control-Field-Compression
315 LOG(4, 0, 0, " %s\n", lcp_types
[type
]);
318 LOG(2, 0, 0, " Unknown PPP LCP Option type %d\n", type
);
326 // Process LCP messages
327 void processlcp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
329 uint8_t b
[MAXCONTROL
];
331 uint32_t magicno
= 0;
334 CSTAT(call_processlcp
);
336 LOG_HEX(5, "LCP", p
, l
);
339 LOG(1, s
, t
, "Short LCP %d bytes\n", l
);
340 STAT(tunnel_rx_errors
);
344 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
346 LOG(1, s
, t
, "Length mismatch LCP %u/%u\n", hl
, l
);
347 STAT(tunnel_rx_errors
);
354 LOG(3, s
, t
, "LCP: Discarding ConfigAck\n");
355 session
[s
].flags
|= SF_LCP_ACKED
;
357 else if (*p
== ConfigReq
)
360 uint8_t *o
= (p
+ 4);
361 uint8_t *response
= 0;
363 LOG(3, s
, t
, "LCP: ConfigReq (%d bytes)...\n", l
);
364 if (config
->debug
> 3) dumplcp(p
, l
);
371 if (length
== 0 || type
== 0 || x
< length
) break;
374 case 1: // Maximum-Receive-Unit
375 session
[s
].mru
= ntohs(*(uint16_t *)(o
+ 2));
378 case 2: // Async-Control-Character-Map
379 if (!ntohl(*(uint32_t *)(o
+ 2))) // all bits zero is OK
382 if (response
&& *response
!= ConfigNak
) // rej already queued
385 LOG(2, s
, t
, " Remote requesting asyncmap. Rejecting.\n");
388 q
= response
= makeppp(b
, sizeof(b
), p
, 2, t
, s
, PPPLCP
);
394 if ((q
- b
+ 11) > sizeof(b
))
396 LOG(2, s
, t
, "LCP overflow for asyncmap ConfigNak.\n");
402 memset(q
, 0, 4); // asyncmap 0
404 *((uint16_t *) (response
+ 2)) = htons(q
- response
); // LCP header length
407 case 3: // Authentication-Protocol
409 int proto
= ntohs(*(uint16_t *)(o
+ 2));
410 char proto_name
[] = "0x0000";
414 if (response
&& *response
!= ConfigNak
) // rej already queued
417 if (proto
== PPPCHAP
)
418 strcpy(proto_name
, "CHAP");
420 sprintf(proto_name
, "%#4.4x", proto
);
422 LOG(2, s
, t
, " Remote requesting %s authentication. Rejecting.\n", proto_name
);
426 q
= response
= makeppp(b
, sizeof(b
), p
, 2, t
, s
, PPPLCP
);
432 if ((q
- b
+ length
) > sizeof(b
))
434 LOG(2, s
, t
, "LCP overflow for %s ConfigNak.\n", proto_name
);
438 memcpy(q
, o
, length
);
439 *(uint16_t *)(q
+= 2) = htons(PPPPAP
); // NAK -> Use PAP instead
441 *((uint16_t *) (response
+ 2)) = htons(q
- response
);
445 case 5: // Magic-Number
446 magicno
= ntohl(*(uint32_t *)(o
+ 2));
449 case 4: // Quality-Protocol
450 case 7: // Protocol-Field-Compression
451 case 8: // Address-And-Control-Field-Compression
454 default: // Reject any unknown options
455 LOG(2, s
, t
, " Rejecting PPP LCP Option type %d\n", type
);
456 if (!response
|| *response
!= ConfigRej
) // drop nak in favour of rej
458 q
= response
= makeppp(b
, sizeof(b
), p
, 2, t
, s
, PPPLCP
);
464 if ((q
- b
+ length
) > sizeof(b
))
466 LOG(2, s
, t
, "LCP overflow for ConfigRej (type=%d).\n", type
);
470 memcpy(q
, o
, length
);
472 *((uint16_t *) (response
+ 2)) = htons(q
- response
); // LCP header length
480 // Send back a ConfigAck
481 q
= response
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
486 LOG(3, s
, t
, "Sending %s\n", ppp_lcp_types
[*response
]);
487 tunnelsend(b
, l
+ (q
- b
), t
);
489 if (!(session
[s
].flags
& SF_LCP_ACKED
))
492 else if (*p
== ConfigNak
)
494 LOG(1, s
, t
, "Remote end sent a ConfigNak. Ignoring\n");
495 if (config
->debug
> 3) dumplcp(p
, l
);
498 else if (*p
== TerminateReq
)
500 LOG(3, s
, t
, "LCP: Received TerminateReq. Sending TerminateAck\n");
501 *p
= TerminateAck
; // close
502 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
504 tunnelsend(b
, l
+ (q
- b
), t
); // send it
505 sessionshutdown(s
, "Remote end closed connection.");
507 else if (*p
== TerminateAck
)
509 sessionshutdown(s
, "Connection closed.");
511 else if (*p
== EchoReq
)
513 LOG(5, s
, t
, "LCP: Received EchoReq. Sending EchoReply\n");
514 *p
= EchoReply
; // reply
515 *(uint32_t *) (p
+ 4) = htonl(session
[s
].magic
); // our magic number
516 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
518 tunnelsend(b
, l
+ (q
- b
), t
); // send it
520 else if (*p
== EchoReply
)
522 // Ignore it, last_packet time is set earlier than this.
524 else if (*p
== IdentRequest
)
529 LOG(1, s
, t
, "Truncated Ident Packet (length=%d) to 1400 bytes\n", l
);
532 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
534 LOG_HEX(5, "LCPIdentRej", q
, l
+ 4);
535 tunnelsend(b
, 12 + 4 + l
, t
);
539 LOG(1, s
, t
, "Unexpected LCP code %d\n", *p
);
540 STAT(tunnel_rx_errors
);
545 // find a PPP option, returns point to option, or 0 if not found
546 static uint8_t *findppp(uint8_t *b
, uint8_t mtype
)
548 uint16_t l
= ntohs(*(uint16_t *) (b
+ 2));
555 if (l
< b
[1] || !b
[1])
565 // Process IPCP messages
566 void processipcp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
570 CSTAT(call_processipcp
);
572 LOG_HEX(5, "IPCP", p
, l
);
575 LOG(1, s
, t
, "Short IPCP %d bytes\n", l
);
576 STAT(tunnel_rx_errors
);
580 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
582 LOG(1, s
, t
, "Length mismatch IPCP %u/%u\n", hl
, l
);
583 STAT(tunnel_rx_errors
);
590 // happy with our IPCP
591 uint16_t r
= session
[s
].radius
;
592 if ((!r
|| radius
[r
].state
== RADIUSIPCP
) && !session
[s
].walled_garden
)
597 radiussend(r
, RADIUSSTART
); // send radius start, having got IPCP at last
599 session
[s
].flags
|= SF_IPCP_ACKED
;
601 LOG(3, s
, t
, "IPCP Acked, session is now active\n");
603 // clear LCP_ACKED/CCP_ACKED flag for possible fast renegotiaion for routers
604 session
[s
].flags
&= ~(SF_LCP_ACKED
|SF_CCP_ACKED
);
610 LOG(1, s
, t
, "Unexpected IPCP code %d\n", *p
);
611 STAT(tunnel_rx_errors
);
614 LOG(4, s
, t
, "IPCP ConfigReq received\n");
618 LOG(3, s
, t
, "Waiting on radius reply\n");
619 return; // have to wait on RADIUS reply
621 // form a config reply quoting the IP in the session
623 uint8_t b
[MAXCONTROL
];
628 while (q
< i
&& q
[1])
630 if (*q
!= 0x81 && *q
!= 0x83 && *q
!= 3)
639 if (!(q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPIPCP
)))
644 while (p
< i
&& p
[1])
646 if (*p
!= 0x81 && *p
!= 0x83 && *p
!= 3)
648 LOG(2, s
, t
, "IPCP reject %d\n", *p
);
649 memcpy(q
+ n
, p
, p
[1]);
654 *(uint16_t *) (q
+ 2) = htons(n
);
655 LOG(4, s
, t
, "Sending ConfigRej\n");
656 tunnelsend(b
, n
+ (q
- b
), t
); // send it
660 LOG(4, s
, t
, "Sending ConfigAck\n");
662 if ((i
= findppp(p
, 0x81))) // Primary DNS address
664 if (*(uint32_t *) (i
+ 2) != htonl(session
[s
].dns1
))
666 *(uint32_t *) (i
+ 2) = htonl(session
[s
].dns1
);
668 LOG(5, s
, t
, " DNS1 = %s\n",
669 fmtaddr(htonl(session
[s
].dns1
), 0));
672 if ((i
= findppp(p
, 0x83))) // Secondary DNS address (TBA, is it)
674 if (*(uint32_t *) (i
+ 2) != htonl(session
[s
].dns2
))
676 *(uint32_t *) (i
+ 2) = htonl(session
[s
].dns2
);
678 LOG(5, s
, t
, " DNS2 = %s\n",
679 fmtaddr(htonl(session
[s
].dns2
), 0));
682 i
= findppp(p
, 3); // IP address
685 LOG(1, s
, t
, "No IP in IPCP request\n");
686 STAT(tunnel_rx_errors
);
689 if (*(uint32_t *) (i
+ 2) != htonl(session
[s
].ip
))
691 *(uint32_t *) (i
+ 2) = htonl(session
[s
].ip
);
693 LOG(4, s
, t
, " No, a ConfigNak, client is requesting IP - sending %s\n",
694 fmtaddr(htonl(session
[s
].ip
), 0));
696 if (!(q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPIPCP
)))
699 tunnelsend(b
, l
+ (q
- b
), t
); // send it
704 // process IP packet received
706 // This MUST be called with at least 4 byte behind 'p'.
707 // (i.e. this routine writes to p[-4]).
708 void processipin(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
712 CSTAT(call_processipin
);
714 LOG_HEX(5, "IP", p
, l
);
716 ip
= ntohl(*(uint32_t *)(p
+ 12));
720 LOG(1, s
, t
, "IP packet too long %d\n", l
);
721 STAT(tunnel_rx_errors
);
725 // no spoof (do sessionbyip to handled statically routed subnets)
726 if (ip
!= session
[s
].ip
&& sessionbyip(htonl(ip
)) != s
)
728 LOG(5, s
, t
, "Dropping packet with spoofed IP %s\n", fmtaddr(htonl(ip
), 0));
732 // run access-list if any
733 if (session
[s
].filter_in
&& !ip_filter(p
, l
, session
[s
].filter_in
- 1))
736 // Add on the tun header
738 *(uint32_t *) p
= htonl(0x00000800);
741 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) { // Are we throttled and a slave?
742 master_throttle_packet(session
[s
].tbf_in
, p
, l
); // Pass it to the master for handling.
746 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) { // Are we throttled and a master?? actually handle the throttled packets.
747 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
752 if (tun_write(p
, l
) < 0)
755 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
756 l
, strerror(errno
), tunfd
, p
);
761 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
763 // Snooping this session
764 snoop_send_packet(p
+ 4, l
- 4, session
[s
].snoop_ip
, session
[s
].snoop_port
);
767 session
[s
].cin
+= l
- 4;
768 session
[s
].total_cin
+= l
- 4;
769 sess_local
[s
].cin
+= l
- 4;
774 STAT(tun_tx_packets
);
775 INC_STAT(tun_tx_bytes
, l
- 4);
779 // Helper routine for the TBF filters.
780 // Used to send queued data in from the user.
782 void send_ipin(sessionidt s
, uint8_t *buf
, int len
)
784 LOG_HEX(5, "IP in throttled", buf
, len
);
786 if (write(tunfd
, buf
, len
) < 0)
789 LOG(0, 0, 0, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
790 len
, strerror(errno
), tunfd
, buf
);
795 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
797 // Snooping this session
798 snoop_send_packet(buf
+ 4, len
- 4, session
[s
].snoop_ip
, session
[s
].snoop_port
);
801 // Increment packet counters
802 session
[s
].cin
+= len
- 4;
803 session
[s
].total_cin
+= len
- 4;
804 sess_local
[s
].cin
+= len
- 4;
809 STAT(tun_tx_packets
);
810 INC_STAT(tun_tx_bytes
, len
- 4);
814 // Process CCP messages
815 void processccp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
817 uint8_t b
[MAXCONTROL
];
820 CSTAT(call_processccp
);
822 LOG_HEX(5, "CCP", p
, l
);
823 switch (l
> 1 ? *p
: 0)
826 session
[s
].flags
|= SF_CCP_ACKED
;
830 if (l
< 6) // accept no compression
836 // compression requested--reject
839 // send CCP request for no compression for our end if not negotiated
840 if (!(session
[s
].flags
& SF_CCP_ACKED
))
851 LOG(1, s
, t
, "Unexpected CCP request code %d\n", *p
);
853 LOG(1, s
, t
, "Short CCP packet\n");
855 STAT(tunnel_rx_errors
);
859 if (!(q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPCCP
)))
862 tunnelsend(b
, l
+ (q
- b
), t
); // send it
865 // send a CHAP PP packet
866 void sendchap(tunnelidt t
, sessionidt s
)
868 uint8_t b
[MAXCONTROL
];
869 uint16_t r
= session
[s
].radius
;
872 CSTAT(call_sendchap
);
876 LOG(1, s
, t
, "No RADIUS to send challenge\n");
877 STAT(tunnel_tx_errors
);
880 LOG(1, s
, t
, "Send CHAP challenge\n");
884 for (n
= 0; n
< 15; n
++)
885 radius
[r
].auth
[n
] = rand();
887 radius
[r
].chap
= 1; // CHAP not PAP
889 if (radius
[r
].state
!= RADIUSCHAP
)
891 radius
[r
].state
= RADIUSCHAP
;
892 radius
[r
].retry
= backoff(radius
[r
].try++);
893 if (radius
[r
].try > 5)
895 sessionshutdown(s
, "Timeout CHAP");
896 STAT(tunnel_tx_errors
);
899 q
= makeppp(b
, sizeof(b
), 0, 0, t
, s
, PPPCHAP
);
903 q
[1] = radius
[r
].id
; // ID
905 memcpy(q
+ 5, radius
[r
].auth
, 16); // challenge
906 strcpy(q
+ 21, hostname
); // our name
907 *(uint16_t *) (q
+ 2) = htons(strlen(hostname
) + 21); // length
908 tunnelsend(b
, strlen(hostname
) + 21 + (q
- b
), t
); // send it
911 // fill in a L2TP message with a PPP frame,
912 // copies existing PPP message and changes magic number if seen
913 // returns start of PPP frame
914 uint8_t *makeppp(uint8_t *b
, int size
, uint8_t *p
, int l
, tunnelidt t
, sessionidt s
, uint16_t mtype
)
916 if (size
< 12) // Need more space than this!!
918 static int backtrace_count
= 0;
919 LOG(0, s
, t
, "makeppp buffer too small for L2TP header (size=%d)\n", size
);
920 log_backtrace(backtrace_count
, 5)
924 *(uint16_t *) (b
+ 0) = htons(0x0002); // L2TP with no options
925 *(uint16_t *) (b
+ 2) = htons(tunnel
[t
].far
); // tunnel
926 *(uint16_t *) (b
+ 4) = htons(session
[s
].far
); // session
928 if (mtype
== PPPLCP
|| !(session
[s
].l2tp_flags
& SESSIONACFC
))
930 *(uint16_t *) b
= htons(0xFF03); // HDLC header
933 if (mtype
< 0x100 && session
[s
].l2tp_flags
& SESSIONPFC
)
937 *(uint16_t *) b
= htons(mtype
);
943 static int backtrace_count
= 0;
944 LOG(2, s
, t
, "makeppp would overflow buffer (size=%d, header+payload=%d)\n", size
, l
+ 12);
945 log_backtrace(backtrace_count
, 5)
955 // Send initial LCP ConfigReq for PAP, set magic no.
956 void initlcp(tunnelidt t
, sessionidt s
)
960 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, t
, s
, PPPLCP
)))
963 LOG(4, s
, t
, "Sending LCP ConfigReq for PAP\n");
965 *(uint8_t *)(q
+ 1) = (time_now
% 255) + 1; // ID
966 *(uint16_t *)(q
+ 2) = htons(14); // Length
967 *(uint8_t *)(q
+ 4) = 5;
968 *(uint8_t *)(q
+ 5) = 6;
969 *(uint32_t *)(q
+ 6) = htonl(session
[s
].magic
);
970 *(uint8_t *)(q
+ 10) = 3;
971 *(uint8_t *)(q
+ 11) = 4;
972 *(uint16_t *)(q
+ 12) = htons(PPPPAP
); // PAP
974 LOG_HEX(5, "PPPLCP", q
, 14);
975 tunnelsend(b
, (q
- b
) + 14, t
);
978 // Send CCP request for no compression
979 static void initccp(tunnelidt t
, sessionidt s
)
983 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, t
, s
, PPPCCP
)))
986 LOG(4, s
, t
, "Sending CCP ConfigReq for no compression\n");
988 *(uint8_t *)(q
+ 1) = (time_now
% 255) + 1; // ID
989 *(uint16_t *)(q
+ 2) = htons(4); // Length
991 LOG_HEX(5, "PPPCCP", q
, 4);
992 tunnelsend(b
, (q
- b
) + 4 , t
);