0832f97801c199e244c17d21757dee19cda206bc
3 char const *cvs_id_ppp
= "$Id: ppp.c,v 1.49 2005-05-07 08:17:25 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
)
37 LOG_HEX(5, "PAP", p
, l
);
40 LOG(1, s
, t
, "Short PAP %u bytes\n", l
);
41 STAT(tunnel_rx_errors
);
42 sessionshutdown(s
, "Short PAP packet.", 3, 0);
46 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
48 LOG(1, s
, t
, "Length mismatch PAP %u/%u\n", hl
, l
);
49 STAT(tunnel_rx_errors
);
50 sessionshutdown(s
, "PAP length mismatch.", 3, 0);
57 LOG(1, s
, t
, "Unexpected PAP code %d\n", *p
);
58 STAT(tunnel_rx_errors
);
59 sessionshutdown(s
, "Unexpected PAP code.", 3, 0);
66 user
[0] = pass
[0] = 0;
67 if (*b
&& *b
< sizeof(user
))
69 memcpy(user
, b
+ 1, *b
);
72 if (*b
&& *b
< sizeof(pass
))
74 memcpy(pass
, b
+ 1, *b
);
78 LOG(3, s
, t
, "PAP login %s/%s\n", user
, pass
);
80 if (session
[s
].ip
|| !sess_local
[s
].radius
)
82 // respond now, either no RADIUS available or already authenticated
83 uint8_t b
[MAXCONTROL
];
85 uint8_t *p
= makeppp(b
, sizeof(b
), 0, 0, t
, s
, PPPPAP
);
91 *p
= 3; // cant authorise
93 *(uint16_t *) (p
+ 2) = htons(5); // length
94 p
[4] = 0; // no message
97 LOG(3, s
, t
, "Already an IP allocated: %s (%d)\n",
98 fmtaddr(htonl(session
[s
].ip
), 0), session
[s
].ip_pool_index
);
100 session
[s
].flags
&= ~SF_IPCP_ACKED
;
104 LOG(1, s
, t
, "No radius session available to authenticate session...\n");
106 LOG(3, s
, t
, "Fallback response to PAP (%s)\n", (session
[s
].ip
) ? "ACK" : "NAK");
107 tunnelsend(b
, 5 + (p
- b
), t
); // send it
108 sessionshutdown(s
, "PAP authentication failed.", 3, 0);
112 // set up RADIUS request
113 uint16_t r
= sess_local
[s
].radius
;
115 // Run PRE_AUTH plugins
116 struct param_pre_auth packet
= { &tunnel
[t
], &session
[s
], strdup(user
), strdup(pass
), PPPPAP
, 1 };
117 run_plugins(PLUGIN_PRE_AUTH
, &packet
);
118 if (!packet
.continue_auth
)
120 LOG(3, s
, t
, "A plugin rejected PRE_AUTH\n");
121 if (packet
.username
) free(packet
.username
);
122 if (packet
.password
) free(packet
.password
);
126 strncpy(session
[s
].user
, packet
.username
, sizeof(session
[s
].user
) - 1);
127 strncpy(radius
[r
].pass
, packet
.password
, sizeof(radius
[r
].pass
) - 1);
129 free(packet
.username
);
130 free(packet
.password
);
133 LOG(3, s
, t
, "Sending login for %s/%s to radius\n", user
, pass
);
134 radiussend(r
, RADIUSAUTH
);
138 // Process CHAP messages
139 void processchap(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
146 LOG_HEX(5, "CHAP", p
, l
);
147 r
= sess_local
[s
].radius
;
150 LOG(1, s
, t
, "Unexpected CHAP message\n");
151 STAT(tunnel_rx_errors
);
157 LOG(1, s
, t
, "Short CHAP %u bytes\n", l
);
158 STAT(tunnel_rx_errors
);
162 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
164 LOG(1, s
, t
, "Length mismatch CHAP %u/%u\n", hl
, l
);
165 STAT(tunnel_rx_errors
);
172 LOG(1, s
, t
, "Unexpected CHAP response code %d\n", *p
);
173 STAT(tunnel_rx_errors
);
176 if (p
[1] != radius
[r
].id
)
178 LOG(1, s
, t
, "Wrong CHAP response ID %d (should be %d) (%d)\n", p
[1], radius
[r
].id
, r
);
179 STAT(tunnel_rx_errors
);
183 if (l
< 5 || p
[4] != 16)
185 LOG(1, s
, t
, "Bad CHAP response length %d\n", l
< 5 ? -1 : p
[4]);
186 STAT(tunnel_rx_errors
);
192 if (l
< 16 || l
- 16 >= sizeof(session
[s
].user
))
194 LOG(1, s
, t
, "CHAP user too long %d\n", l
- 16);
195 STAT(tunnel_rx_errors
);
199 // Run PRE_AUTH plugins
201 struct param_pre_auth packet
= { &tunnel
[t
], &session
[s
], NULL
, NULL
, PPPCHAP
, 1 };
203 packet
.password
= calloc(17, 1);
204 memcpy(packet
.password
, p
, 16);
209 packet
.username
= calloc(l
+ 1, 1);
210 memcpy(packet
.username
, p
, l
);
212 run_plugins(PLUGIN_PRE_AUTH
, &packet
);
213 if (!packet
.continue_auth
)
215 LOG(3, s
, t
, "A plugin rejected PRE_AUTH\n");
216 if (packet
.username
) free(packet
.username
);
217 if (packet
.password
) free(packet
.password
);
221 strncpy(session
[s
].user
, packet
.username
, sizeof(session
[s
].user
) - 1);
222 memcpy(radius
[r
].pass
, packet
.password
, 16);
224 free(packet
.username
);
225 free(packet
.password
);
229 LOG(3, s
, t
, "CHAP login %s\n", session
[s
].user
);
230 radiussend(r
, RADIUSAUTH
);
233 static void dumplcp(uint8_t *p
, int l
)
236 uint8_t *o
= (p
+ 4);
238 LOG_HEX(5, "PPP LCP Packet", p
, l
);
239 LOG(4, 0, 0, "PPP LCP Packet type %d (%s len %d)\n", *p
, ppp_lcp_type((int)*p
), ntohs( ((uint16_t *) p
)[1]) );
240 LOG(4, 0, 0, "Length: %d\n", l
);
241 if (*p
!= ConfigReq
&& *p
!= ConfigRej
&& *p
!= ConfigAck
)
250 LOG(4, 0, 0, " Option length is %d...\n", length
);
255 LOG(4, 0, 0, " Option type is 0...\n");
262 case 1: // Maximum-Receive-Unit
264 LOG(4, 0, 0, " %s %d\n", lcp_type(type
), ntohs(*(uint16_t *)(o
+ 2)));
266 LOG(4, 0, 0, " %s odd length %d\n", lcp_type(type
), length
);
268 case 2: // Async-Control-Character-Map
271 uint32_t asyncmap
= ntohl(*(uint32_t *)(o
+ 2));
272 LOG(4, 0, 0, " %s %x\n", lcp_type(type
), asyncmap
);
275 LOG(4, 0, 0, " %s odd length %d\n", lcp_type(type
), length
);
277 case 3: // Authentication-Protocol
280 int proto
= ntohs(*(uint16_t *)(o
+ 2));
281 LOG(4, 0, 0, " %s 0x%x (%s)\n", lcp_type(type
), proto
,
282 proto
== PPPPAP
? "PAP" : "UNSUPPORTED");
284 else if (length
== 5)
286 int proto
= ntohs(*(uint16_t *)(o
+ 2));
287 int algo
= *(uint8_t *)(o
+ 4);
288 LOG(4, 0, 0, " %s 0x%x 0x%x (%s)\n", lcp_types
[type
], proto
, algo
,
289 (proto
== PPPCHAP
&& algo
== 5) ? "CHAP MD5" : "UNSUPPORTED");
292 LOG(4, 0, 0, " %s odd length %d\n", lcp_type(type
), length
);
294 case 4: // Quality-Protocol
296 uint32_t qp
= ntohl(*(uint32_t *)(o
+ 2));
297 LOG(4, 0, 0, " %s %x\n", lcp_type(type
), qp
);
300 case 5: // Magic-Number
303 uint32_t magicno
= ntohl(*(uint32_t *)(o
+ 2));
304 LOG(4, 0, 0, " %s %x\n", lcp_type(type
), magicno
);
307 LOG(4, 0, 0, " %s odd length %d\n", lcp_type(type
), length
);
309 case 7: // Protocol-Field-Compression
310 case 8: // Address-And-Control-Field-Compression
311 LOG(4, 0, 0, " %s\n", lcp_type(type
));
314 LOG(2, 0, 0, " Unknown PPP LCP Option type %d\n", type
);
322 // Process LCP messages
323 void processlcp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
325 uint8_t b
[MAXCONTROL
];
327 uint32_t magicno
= 0;
332 LOG_HEX(5, "LCP", p
, l
);
335 LOG(1, s
, t
, "Short LCP %d bytes\n", l
);
336 STAT(tunnel_rx_errors
);
340 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
342 LOG(1, s
, t
, "Length mismatch LCP %u/%u\n", hl
, l
);
343 STAT(tunnel_rx_errors
);
350 LOG(3, s
, t
, "LCP: Discarding ConfigAck\n");
351 session
[s
].flags
|= SF_LCP_ACKED
;
353 else if (*p
== ConfigReq
)
356 uint8_t *o
= (p
+ 4);
357 uint8_t *response
= 0;
359 LOG(3, s
, t
, "LCP: ConfigReq (%d bytes)...\n", l
);
360 if (config
->debug
> 3) dumplcp(p
, l
);
367 if (length
== 0 || type
== 0 || x
< length
) break;
370 case 1: // Maximum-Receive-Unit
371 session
[s
].mru
= ntohs(*(uint16_t *)(o
+ 2));
374 case 2: // Async-Control-Character-Map
375 if (!ntohl(*(uint32_t *)(o
+ 2))) // all bits zero is OK
378 if (response
&& *response
!= ConfigNak
) // rej already queued
381 LOG(2, s
, t
, " Remote requesting asyncmap. Rejecting.\n");
384 q
= response
= makeppp(b
, sizeof(b
), p
, 2, t
, s
, PPPLCP
);
390 if ((q
- b
+ 11) > sizeof(b
))
392 LOG(2, s
, t
, "LCP overflow for asyncmap ConfigNak.\n");
398 memset(q
, 0, 4); // asyncmap 0
400 *((uint16_t *) (response
+ 2)) = htons(q
- response
); // LCP header length
403 case 3: // Authentication-Protocol
405 int proto
= ntohs(*(uint16_t *)(o
+ 2));
406 uint16_t wanted_proto
;
407 char proto_name
[] = "0x0000";
411 if (config
->radius_authtypes
& AUTHPAP
)
414 strcpy(proto_name
, "PAP");
416 else if (proto
== PPPCHAP
)
418 if (config
->radius_authtypes
& AUTHCHAP
419 && *(o
+ 4) == 5) // MD5
422 strcpy(proto_name
, "CHAP");
425 sprintf(proto_name
, "%#4.4x", proto
);
427 if (response
&& *response
!= ConfigNak
) // rej already queued
430 LOG(2, s
, t
, " Remote requesting %s authentication. Rejecting.\n", proto_name
);
434 q
= response
= makeppp(b
, sizeof(b
), p
, 2, t
, s
, PPPLCP
);
440 if ((q
- b
+ 5) > sizeof(b
)) // 5 is the larger (CHAP+MD5) of the two NAKs
442 LOG(2, s
, t
, "LCP overflow for %s ConfigNak.\n", proto_name
);
447 if (config
->radius_authprefer
== AUTHCHAP
)
450 *(uint16_t *) q
= htons(PPPCHAP
); q
+= 2;
456 *(uint16_t *) q
= htons(PPPPAP
); q
+= 2;
459 *((uint16_t *) (response
+ 2)) = htons(q
- response
); // LCP header length
464 case 5: // Magic-Number
465 magicno
= ntohl(*(uint32_t *)(o
+ 2));
468 case 4: // Quality-Protocol
469 case 7: // Protocol-Field-Compression
470 case 8: // Address-And-Control-Field-Compression
473 default: // Reject any unknown options
474 LOG(2, s
, t
, " Rejecting PPP LCP Option type %d\n", type
);
475 if (!response
|| *response
!= ConfigRej
) // drop nak in favour of rej
477 q
= response
= makeppp(b
, sizeof(b
), p
, 2, t
, s
, PPPLCP
);
483 if ((q
- b
+ length
) > sizeof(b
))
485 LOG(2, s
, t
, "LCP overflow for ConfigRej (type=%d).\n", type
);
489 memcpy(q
, o
, length
);
491 *((uint16_t *) (response
+ 2)) = htons(q
- response
); // LCP header length
499 // Send back a ConfigAck
500 q
= response
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
505 LOG(3, s
, t
, "Sending %s\n", ppp_lcp_type(*response
));
506 tunnelsend(b
, l
+ (q
- b
), t
);
508 if (!(session
[s
].flags
& SF_LCP_ACKED
))
511 else if (*p
== ConfigNak
)
513 LOG(1, s
, t
, "Remote end sent a ConfigNak. Ignoring\n");
514 if (config
->debug
> 3) dumplcp(p
, l
);
517 else if (*p
== TerminateReq
)
519 LOG(3, s
, t
, "LCP: Received TerminateReq. Sending TerminateAck\n");
520 *p
= TerminateAck
; // close
521 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
523 tunnelsend(b
, l
+ (q
- b
), t
); // send it
524 sessionshutdown(s
, "Remote end closed connection.", 3, 0);
526 else if (*p
== TerminateAck
)
528 sessionshutdown(s
, "Connection closed.", 3, 0);
530 else if (*p
== ProtocolRej
)
532 if (*(uint16_t *) (p
+4) == htons(PPPIPV6CP
))
534 LOG(3, s
, t
, "IPv6 rejected\n");
535 session
[s
].flags
|= SF_IPV6_NACKED
;
539 LOG(1, s
, t
, "Unexpected LCP protocol reject 0x%X\n",
540 ntohs(*(uint16_t *) (p
+4)));
541 STAT(tunnel_rx_errors
);
544 else if (*p
== EchoReq
)
546 LOG(5, s
, t
, "LCP: Received EchoReq. Sending EchoReply\n");
547 *p
= EchoReply
; // reply
548 *(uint32_t *) (p
+ 4) = htonl(session
[s
].magic
); // our magic number
549 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
551 tunnelsend(b
, l
+ (q
- b
), t
); // send it
553 else if (*p
== EchoReply
)
555 // Ignore it, last_packet time is set earlier than this.
557 else if (*p
== IdentRequest
)
562 LOG(1, s
, t
, "Truncated Ident Packet (length=%d) to 1400 bytes\n", l
);
565 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
567 LOG_HEX(5, "LCPIdentRej", q
, l
+ 4);
568 tunnelsend(b
, 12 + 4 + l
, t
);
572 LOG(1, s
, t
, "Unexpected LCP code %d\n", *p
);
573 STAT(tunnel_rx_errors
);
578 // find a PPP option, returns point to option, or 0 if not found
579 static uint8_t *findppp(uint8_t *b
, uint8_t mtype
)
581 uint16_t l
= ntohs(*(uint16_t *) (b
+ 2));
588 if (l
< b
[1] || !b
[1])
598 // Process IPCP messages
599 void processipcp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
605 LOG_HEX(5, "IPCP", p
, l
);
608 LOG(1, s
, t
, "Short IPCP %d bytes\n", l
);
609 STAT(tunnel_rx_errors
);
613 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
615 LOG(1, s
, t
, "Length mismatch IPCP %u/%u\n", hl
, l
);
616 STAT(tunnel_rx_errors
);
623 // happy with our IPCP
624 uint16_t r
= sess_local
[s
].radius
;
625 if ((!r
|| radius
[r
].state
== RADIUSIPCP
) && !session
[s
].walled_garden
)
630 radiussend(r
, RADIUSSTART
); // send radius start, having got IPCP at last
632 session
[s
].flags
|= SF_IPCP_ACKED
;
634 LOG(3, s
, t
, "IPCP Acked, session is now active\n");
636 // clear LCP_ACKED/CCP_ACKED flag for possible fast renegotiaion for routers
637 session
[s
].flags
&= ~(SF_LCP_ACKED
|SF_CCP_ACKED
);
643 LOG(1, s
, t
, "Unexpected IPCP code %d\n", *p
);
644 STAT(tunnel_rx_errors
);
647 LOG(4, s
, t
, "IPCP ConfigReq received\n");
651 LOG(3, s
, t
, "Waiting on radius reply\n");
652 return; // have to wait on RADIUS reply
654 // form a config reply quoting the IP in the session
656 uint8_t b
[MAXCONTROL
];
661 while (q
< i
&& q
[1])
663 if (*q
!= 0x81 && *q
!= 0x83 && *q
!= 3)
672 if (!(q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPIPCP
)))
677 while (p
< i
&& p
[1])
679 if (*p
!= 0x81 && *p
!= 0x83 && *p
!= 3)
681 LOG(2, s
, t
, "IPCP reject %d\n", *p
);
682 memcpy(q
+ n
, p
, p
[1]);
687 *(uint16_t *) (q
+ 2) = htons(n
);
688 LOG(4, s
, t
, "Sending ConfigRej\n");
689 tunnelsend(b
, n
+ (q
- b
), t
); // send it
693 LOG(4, s
, t
, "Sending ConfigAck\n");
695 if ((i
= findppp(p
, 0x81))) // Primary DNS address
697 if (*(uint32_t *) (i
+ 2) != htonl(session
[s
].dns1
))
699 *(uint32_t *) (i
+ 2) = htonl(session
[s
].dns1
);
701 LOG(5, s
, t
, " DNS1 = %s\n",
702 fmtaddr(htonl(session
[s
].dns1
), 0));
705 if ((i
= findppp(p
, 0x83))) // Secondary DNS address (TBA, is it)
707 if (*(uint32_t *) (i
+ 2) != htonl(session
[s
].dns2
))
709 *(uint32_t *) (i
+ 2) = htonl(session
[s
].dns2
);
711 LOG(5, s
, t
, " DNS2 = %s\n",
712 fmtaddr(htonl(session
[s
].dns2
), 0));
715 i
= findppp(p
, 3); // IP address
718 LOG(1, s
, t
, "No IP in IPCP request\n");
719 STAT(tunnel_rx_errors
);
722 if (*(uint32_t *) (i
+ 2) != htonl(session
[s
].ip
))
724 *(uint32_t *) (i
+ 2) = htonl(session
[s
].ip
);
726 LOG(4, s
, t
, " No, a ConfigNak, client is requesting IP - sending %s\n",
727 fmtaddr(htonl(session
[s
].ip
), 0));
729 if (!(q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPIPCP
)))
732 tunnelsend(b
, l
+ (q
- b
), t
); // send it
737 // Process IPV6CP messages
738 void processipv6cp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
741 CSTAT(processipv6cp
);
743 LOG_HEX(5, "IPV6CP", p
, l
);
746 LOG(1, s
, t
, "Short IPV6CP %d bytes\n", l
);
747 STAT(tunnel_rx_errors
);
752 // happy with our IPV6CP
753 session
[s
].flags
|= SF_IPV6CP_ACKED
;
755 LOG(3, s
, t
, "IPV6CP Acked, IPv6 is now active\n");
756 // Add a routed block if configured.
757 if (session
[s
].ipv6prefixlen
)
759 route6set(s
, session
[s
].ipv6route
, session
[s
].ipv6prefixlen
, 1);
760 session
[s
].flags
|= SF_IPV6_ROUTED
;
763 // Send an initial RA (TODO: Should we send these regularly?)
764 send_ipv6_ra(t
, s
, NULL
);
769 LOG(1, s
, t
, "Unexpected IPV6CP code %d\n", *p
);
770 STAT(tunnel_rx_errors
);
774 LOG(4, s
, t
, "IPV6CP ConfigReq received\n");
775 if (ntohs(*(uint16_t *) (p
+ 2)) > l
)
777 LOG(1, s
, t
, "Length mismatch IPV6CP %d/%d\n", ntohs(*(uint16_t *) (p
+ 2)), l
);
778 STAT(tunnel_rx_errors
);
783 LOG(3, s
, t
, "Waiting on radius reply\n");
784 return; // have to wait on RADIUS reply
786 // form a config reply quoting the IP in the session
788 uint8_t b
[MAXCONTROL
];
792 l
= ntohs(*(uint16_t *) (p
+ 2)); // We must use length from IPV6CP len field
795 while (q
< i
&& q
[1])
806 if (!(q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPIPV6CP
)))
808 LOG(2, s
, t
, "Failed to send IPV6CP ConfigRej\n");
813 while (p
< i
&& p
[1])
817 LOG(2, s
, t
, "IPV6CP reject %d\n", *p
);
818 memcpy(q
+ n
, p
, p
[1]);
823 *(uint16_t *) (q
+ 2) = htons(n
);
824 LOG(4, s
, t
, "Sending ConfigRej\n");
825 tunnelsend(b
, n
+ (q
- b
), t
); // send it
829 LOG(4, s
, t
, "Sending ConfigAck\n");
831 i
= findppp(p
, 1); // IP address
832 if (!i
|| i
[1] != 10)
834 LOG(1, s
, t
, "No IP in IPV6CP request\n");
835 STAT(tunnel_rx_errors
);
838 if ((*(uint32_t *) (i
+ 2) != htonl(session
[s
].ip
)) ||
839 (*(uint32_t *) (i
+ 6) != 0))
841 *(uint32_t *) (i
+ 2) = htonl(session
[s
].ip
);
842 *(uint32_t *) (i
+ 6) = 0;
845 " No, a ConfigNak, client is "
846 "requesting IP - sending %s\n",
847 fmtaddr(htonl(session
[s
].ip
), 0));
849 if (!(q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPIPV6CP
)))
851 LOG(2, s
, t
, " Failed to send IPV6CP packet.\n");
854 tunnelsend(b
, l
+ (q
- b
), t
); // send it
859 // process IP packet received
861 // This MUST be called with at least 4 byte behind 'p'.
862 // (i.e. this routine writes to p[-4]).
863 void processipin(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
869 LOG_HEX(5, "IP", p
, l
);
871 ip
= ntohl(*(uint32_t *)(p
+ 12));
875 LOG(1, s
, t
, "IP packet too long %d\n", l
);
876 STAT(tunnel_rx_errors
);
880 // no spoof (do sessionbyip to handled statically routed subnets)
881 if (ip
!= session
[s
].ip
&& sessionbyip(htonl(ip
)) != s
)
883 LOG(5, s
, t
, "Dropping packet with spoofed IP %s\n", fmtaddr(htonl(ip
), 0));
887 // run access-list if any
888 if (session
[s
].filter_in
&& !ip_filter(p
, l
, session
[s
].filter_in
- 1))
891 // Add on the tun header
893 *(uint32_t *) p
= htonl(PKTIP
);
896 // Are we throttled and a slave?
897 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) {
898 // Pass it to the master for handling.
899 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
903 // Are we throttled and a master??
904 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) {
905 // Actually handle the throttled packets.
906 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
911 if (tun_write(p
, l
) < 0)
914 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
915 l
, strerror(errno
), tunfd
, p
);
920 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
922 // Snooping this session
923 snoop_send_packet(p
+ 4, l
- 4, session
[s
].snoop_ip
, session
[s
].snoop_port
);
926 session
[s
].cin
+= l
- 4;
927 session
[s
].total_cin
+= l
- 4;
928 sess_local
[s
].cin
+= l
- 4;
933 STAT(tun_tx_packets
);
934 INC_STAT(tun_tx_bytes
, l
- 4);
937 // process IPv6 packet received
939 // This MUST be called with at least 4 byte behind 'p'.
940 // (i.e. this routine writes to p[-4]).
941 void processipv6in(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
946 CSTAT(processipv6in
);
948 LOG_HEX(5, "IPv6", p
, l
);
950 ip
= *(struct in6_addr
*) (p
+ 8);
951 ipv4
= ntohl(*(uint32_t *)(p
+ 16));
955 LOG(1, s
, t
, "IP packet too long %d\n", l
);
956 STAT(tunnel_rx_errors
);
961 if (ipv4
!= session
[s
].ip
&& memcmp(&config
->ipv6_prefix
, &ip
, 8) && sessionbyipv6(ip
) != s
)
963 char str
[INET6_ADDRSTRLEN
];
964 LOG(5, s
, t
, "Dropping packet with spoofed IP %s\n",
965 inet_ntop(AF_INET6
, &ip
, str
, INET6_ADDRSTRLEN
));
969 // Check if it's a Router Solicition message.
970 if (*(p
+ 6) == 58 && *(p
+ 7) == 255 && *(p
+ 24) == 0xFF && *(p
+ 25) == 2 &&
971 *(uint32_t *)(p
+ 26) == 0 && *(uint32_t *)(p
+ 30) == 0 &&
972 *(uint32_t *)(p
+ 34) == 0 &&
973 *(p
+ 38) == 0 && *(p
+ 39) == 2 && *(p
+ 40) == 133) {
974 LOG(3, s
, t
, "Got IPv6 RS\n");
975 send_ipv6_ra(t
, s
, &ip
);
979 // Add on the tun header
981 *(uint32_t *) p
= htonl(PKTIPV6
);
984 // Are we throttled and a slave?
985 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) {
986 // Pass it to the master for handling.
987 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
991 // Are we throttled and a master??
992 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) {
993 // Actually handle the throttled packets.
994 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
999 if (tun_write(p
, l
) < 0)
1001 STAT(tun_tx_errors
);
1002 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1003 l
, strerror(errno
), tunfd
, p
);
1008 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1010 // Snooping this session
1011 snoop_send_packet(p
+ 4, l
- 4, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1014 session
[s
].cin
+= l
- 4;
1015 session
[s
].total_cin
+= l
- 4;
1016 sess_local
[s
].cin
+= l
- 4;
1021 STAT(tun_tx_packets
);
1022 INC_STAT(tun_tx_bytes
, l
- 4);
1026 // Helper routine for the TBF filters.
1027 // Used to send queued data in from the user.
1029 void send_ipin(sessionidt s
, uint8_t *buf
, int len
)
1031 LOG_HEX(5, "IP in throttled", buf
, len
);
1033 if (write(tunfd
, buf
, len
) < 0)
1035 STAT(tun_tx_errors
);
1036 LOG(0, 0, 0, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1037 len
, strerror(errno
), tunfd
, buf
);
1042 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1044 // Snooping this session
1045 snoop_send_packet(buf
+ 4, len
- 4, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1048 // Increment packet counters
1049 session
[s
].cin
+= len
- 4;
1050 session
[s
].total_cin
+= len
- 4;
1051 sess_local
[s
].cin
+= len
- 4;
1056 STAT(tun_tx_packets
);
1057 INC_STAT(tun_tx_bytes
, len
- 4);
1061 // Process CCP messages
1062 void processccp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
1064 uint8_t b
[MAXCONTROL
];
1069 LOG_HEX(5, "CCP", p
, l
);
1070 switch (l
> 1 ? *p
: 0)
1073 session
[s
].flags
|= SF_CCP_ACKED
;
1077 if (l
< 6) // accept no compression
1083 // compression requested--reject
1086 // send CCP request for no compression for our end if not negotiated
1087 if (!(session
[s
].flags
& SF_CCP_ACKED
))
1098 LOG(1, s
, t
, "Unexpected CCP request code %d\n", *p
);
1100 LOG(1, s
, t
, "Short CCP packet\n");
1102 STAT(tunnel_rx_errors
);
1106 if (!(q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPCCP
)))
1109 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1112 // send a CHAP challenge
1113 void sendchap(tunnelidt t
, sessionidt s
)
1115 uint8_t b
[MAXCONTROL
];
1116 uint16_t r
= sess_local
[s
].radius
;
1123 LOG(1, s
, t
, "No RADIUS to send challenge\n");
1124 STAT(tunnel_tx_errors
);
1128 LOG(1, s
, t
, "Send CHAP challenge\n");
1130 radius
[r
].chap
= 1; // CHAP not PAP
1132 if (radius
[r
].state
!= RADIUSCHAP
)
1135 radius
[r
].state
= RADIUSCHAP
;
1136 radius
[r
].retry
= backoff(radius
[r
].try++);
1137 if (radius
[r
].try > 5)
1139 sessionshutdown(s
, "CHAP timeout.", 3, 0);
1140 STAT(tunnel_tx_errors
);
1143 q
= makeppp(b
, sizeof(b
), 0, 0, t
, s
, PPPCHAP
);
1146 *q
= 1; // challenge
1147 q
[1] = radius
[r
].id
; // ID
1148 q
[4] = 16; // value size (size of challenge)
1149 memcpy(q
+ 5, radius
[r
].auth
, 16); // challenge
1150 strcpy(q
+ 21, hostname
); // our name
1151 *(uint16_t *) (q
+ 2) = htons(strlen(hostname
) + 21); // length
1152 tunnelsend(b
, strlen(hostname
) + 21 + (q
- b
), t
); // send it
1155 // fill in a L2TP message with a PPP frame,
1156 // copies existing PPP message and changes magic number if seen
1157 // returns start of PPP frame
1158 uint8_t *makeppp(uint8_t *b
, int size
, uint8_t *p
, int l
, tunnelidt t
, sessionidt s
, uint16_t mtype
)
1160 if (size
< 12) // Need more space than this!!
1162 static int backtrace_count
= 0;
1163 LOG(0, s
, t
, "makeppp buffer too small for L2TP header (size=%d)\n", size
);
1164 log_backtrace(backtrace_count
, 5)
1168 *(uint16_t *) (b
+ 0) = htons(0x0002); // L2TP with no options
1169 *(uint16_t *) (b
+ 2) = htons(tunnel
[t
].far
); // tunnel
1170 *(uint16_t *) (b
+ 4) = htons(session
[s
].far
); // session
1172 if (mtype
== PPPLCP
|| !(session
[s
].l2tp_flags
& SESSIONACFC
))
1174 *(uint16_t *) b
= htons(0xFF03); // HDLC header
1177 if (mtype
< 0x100 && session
[s
].l2tp_flags
& SESSIONPFC
)
1181 *(uint16_t *) b
= htons(mtype
);
1187 static int backtrace_count
= 0;
1188 LOG(2, s
, t
, "makeppp would overflow buffer (size=%d, header+payload=%d)\n", size
, l
+ 12);
1189 log_backtrace(backtrace_count
, 5)
1199 // Send initial LCP ConfigReq for PAP, set magic no.
1200 void initlcp(tunnelidt t
, sessionidt s
)
1205 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, t
, s
, PPPLCP
)))
1208 LOG(4, s
, t
, "Sending LCP ConfigReq for PAP\n");
1210 *(uint8_t *)(q
+ 1) = (time_now
% 255) + 1; // ID
1211 *(uint16_t *)(q
+ 2) = htons(14); // Length
1212 *(uint8_t *)(q
+ 4) = 5;
1213 *(uint8_t *)(q
+ 5) = 6;
1214 *(uint32_t *)(q
+ 6) = htonl(session
[s
].magic
);
1215 *(uint8_t *)(q
+ 10) = 3;
1216 if (config
->radius_authprefer
== AUTHCHAP
)
1218 *(uint8_t *)(q
+ 11) = 5;
1219 *(uint16_t *)(q
+ 12) = htons(PPPCHAP
);
1220 *(uint8_t *)(q
+ 14) = 5; // MD5
1225 *(uint8_t *)(q
+ 11) = 4;
1226 *(uint16_t *)(q
+ 12) = htons(PPPPAP
);
1230 LOG_HEX(5, "PPPLCP", q
, size
);
1231 tunnelsend(b
, (q
- b
) + size
, t
);
1234 // Send CCP request for no compression
1235 static void initccp(tunnelidt t
, sessionidt s
)
1239 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, t
, s
, PPPCCP
)))
1242 LOG(4, s
, t
, "Sending CCP ConfigReq for no compression\n");
1244 *(uint8_t *)(q
+ 1) = (time_now
% 255) + 1; // ID
1245 *(uint16_t *)(q
+ 2) = htons(4); // Length
1247 LOG_HEX(5, "PPPCCP", q
, 4);
1248 tunnelsend(b
, (q
- b
) + 4 , t
);