715ed2cc308932d75e0d8e546f0023a6e3f86f71
3 char const *cvs_id_ppp
= "$Id: ppp.c,v 1.51 2005/05/07 11:57:53 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_type(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 char proto_name
[] = "0x0000";
410 if (config
->radius_authtypes
& AUTHPAP
)
413 strcpy(proto_name
, "PAP");
415 else if (proto
== PPPCHAP
)
417 if (config
->radius_authtypes
& AUTHCHAP
418 && *(o
+ 4) == 5) // MD5
421 strcpy(proto_name
, "CHAP");
424 sprintf(proto_name
, "%#4.4x", proto
);
426 if (response
&& *response
!= ConfigNak
) // rej already queued
429 LOG(2, s
, t
, " Remote requesting %s authentication. Rejecting.\n", proto_name
);
433 q
= response
= makeppp(b
, sizeof(b
), p
, 2, t
, s
, PPPLCP
);
439 if ((q
- b
+ 5) > sizeof(b
)) // 5 is the larger (CHAP+MD5) of the two NAKs
441 LOG(2, s
, t
, "LCP overflow for %s ConfigNak.\n", proto_name
);
446 if (config
->radius_authprefer
== AUTHCHAP
)
449 *(uint16_t *) q
= htons(PPPCHAP
); q
+= 2;
455 *(uint16_t *) q
= htons(PPPPAP
); q
+= 2;
458 *((uint16_t *) (response
+ 2)) = htons(q
- response
); // LCP header length
463 case 5: // Magic-Number
464 magicno
= ntohl(*(uint32_t *)(o
+ 2));
467 case 4: // Quality-Protocol
468 case 7: // Protocol-Field-Compression
469 case 8: // Address-And-Control-Field-Compression
472 default: // Reject any unknown options
473 LOG(2, s
, t
, " Rejecting PPP LCP Option type %d\n", type
);
474 if (!response
|| *response
!= ConfigRej
) // drop nak in favour of rej
476 q
= response
= makeppp(b
, sizeof(b
), p
, 2, t
, s
, PPPLCP
);
482 if ((q
- b
+ length
) > sizeof(b
))
484 LOG(2, s
, t
, "LCP overflow for ConfigRej (type=%d).\n", type
);
488 memcpy(q
, o
, length
);
490 *((uint16_t *) (response
+ 2)) = htons(q
- response
); // LCP header length
498 // Send back a ConfigAck
499 q
= response
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
504 LOG(3, s
, t
, "Sending %s\n", ppp_lcp_type(*response
));
505 tunnelsend(b
, l
+ (q
- b
), t
);
507 if (!(session
[s
].flags
& SF_LCP_ACKED
))
510 else if (*p
== ConfigNak
)
512 LOG(1, s
, t
, "Remote end sent a ConfigNak. Ignoring\n");
513 if (config
->debug
> 3) dumplcp(p
, l
);
516 else if (*p
== TerminateReq
)
518 LOG(3, s
, t
, "LCP: Received TerminateReq. Sending TerminateAck\n");
519 *p
= TerminateAck
; // close
520 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
522 tunnelsend(b
, l
+ (q
- b
), t
); // send it
523 sessionshutdown(s
, "Remote end closed connection.", 3, 0);
525 else if (*p
== TerminateAck
)
527 sessionshutdown(s
, "Connection closed.", 3, 0);
529 else if (*p
== ProtocolRej
)
531 if (*(uint16_t *) (p
+4) == htons(PPPIPV6CP
))
533 LOG(3, s
, t
, "IPv6 rejected\n");
534 session
[s
].flags
|= SF_IPV6_NACKED
;
538 LOG(1, s
, t
, "Unexpected LCP protocol reject 0x%X\n",
539 ntohs(*(uint16_t *) (p
+4)));
540 STAT(tunnel_rx_errors
);
543 else if (*p
== EchoReq
)
545 LOG(5, s
, t
, "LCP: Received EchoReq. Sending EchoReply\n");
546 *p
= EchoReply
; // reply
547 *(uint32_t *) (p
+ 4) = htonl(session
[s
].magic
); // our magic number
548 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
550 tunnelsend(b
, l
+ (q
- b
), t
); // send it
552 else if (*p
== EchoReply
)
554 // Ignore it, last_packet time is set earlier than this.
556 else if (*p
== IdentRequest
)
561 LOG(1, s
, t
, "Truncated Ident Packet (length=%d) to 1400 bytes\n", l
);
564 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
566 LOG_HEX(5, "LCPIdentRej", q
, l
+ 4);
567 tunnelsend(b
, 12 + 4 + l
, t
);
571 LOG(1, s
, t
, "Unexpected LCP code %d\n", *p
);
572 STAT(tunnel_rx_errors
);
577 // find a PPP option, returns point to option, or 0 if not found
578 static uint8_t *findppp(uint8_t *b
, uint8_t mtype
)
580 uint16_t l
= ntohs(*(uint16_t *) (b
+ 2));
587 if (l
< b
[1] || !b
[1])
597 // Process IPCP messages
598 void processipcp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
604 LOG_HEX(5, "IPCP", p
, l
);
607 LOG(1, s
, t
, "Short IPCP %d bytes\n", l
);
608 STAT(tunnel_rx_errors
);
612 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
614 LOG(1, s
, t
, "Length mismatch IPCP %u/%u\n", hl
, l
);
615 STAT(tunnel_rx_errors
);
622 // happy with our IPCP
623 uint16_t r
= sess_local
[s
].radius
;
624 if ((!r
|| radius
[r
].state
== RADIUSIPCP
) && !session
[s
].walled_garden
)
629 radiussend(r
, RADIUSSTART
); // send radius start, having got IPCP at last
631 session
[s
].flags
|= SF_IPCP_ACKED
;
633 LOG(3, s
, t
, "IPCP Acked, session is now active\n");
635 // clear LCP_ACKED/CCP_ACKED flag for possible fast renegotiaion for routers
636 session
[s
].flags
&= ~(SF_LCP_ACKED
|SF_CCP_ACKED
);
642 LOG(1, s
, t
, "Unexpected IPCP code %d\n", *p
);
643 STAT(tunnel_rx_errors
);
646 LOG(4, s
, t
, "IPCP ConfigReq received\n");
650 LOG(3, s
, t
, "Waiting on radius reply\n");
651 return; // have to wait on RADIUS reply
653 // form a config reply quoting the IP in the session
655 uint8_t b
[MAXCONTROL
];
660 while (q
< i
&& q
[1])
662 if (*q
!= 0x81 && *q
!= 0x83 && *q
!= 3)
671 if (!(q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPIPCP
)))
676 while (p
< i
&& p
[1])
678 if (*p
!= 0x81 && *p
!= 0x83 && *p
!= 3)
680 LOG(2, s
, t
, "IPCP reject %d\n", *p
);
681 memcpy(q
+ n
, p
, p
[1]);
686 *(uint16_t *) (q
+ 2) = htons(n
);
687 LOG(4, s
, t
, "Sending ConfigRej\n");
688 tunnelsend(b
, n
+ (q
- b
), t
); // send it
692 LOG(4, s
, t
, "Sending ConfigAck\n");
694 if ((i
= findppp(p
, 0x81))) // Primary DNS address
696 if (*(uint32_t *) (i
+ 2) != htonl(session
[s
].dns1
))
698 *(uint32_t *) (i
+ 2) = htonl(session
[s
].dns1
);
700 LOG(5, s
, t
, " DNS1 = %s\n",
701 fmtaddr(htonl(session
[s
].dns1
), 0));
704 if ((i
= findppp(p
, 0x83))) // Secondary DNS address (TBA, is it)
706 if (*(uint32_t *) (i
+ 2) != htonl(session
[s
].dns2
))
708 *(uint32_t *) (i
+ 2) = htonl(session
[s
].dns2
);
710 LOG(5, s
, t
, " DNS2 = %s\n",
711 fmtaddr(htonl(session
[s
].dns2
), 0));
714 i
= findppp(p
, 3); // IP address
717 LOG(1, s
, t
, "No IP in IPCP request\n");
718 STAT(tunnel_rx_errors
);
721 if (*(uint32_t *) (i
+ 2) != htonl(session
[s
].ip
))
723 *(uint32_t *) (i
+ 2) = htonl(session
[s
].ip
);
725 LOG(4, s
, t
, " No, a ConfigNak, client is requesting IP - sending %s\n",
726 fmtaddr(htonl(session
[s
].ip
), 0));
728 if (!(q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPIPCP
)))
731 tunnelsend(b
, l
+ (q
- b
), t
); // send it
736 // Process IPV6CP messages
737 void processipv6cp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
740 CSTAT(processipv6cp
);
742 LOG_HEX(5, "IPV6CP", p
, l
);
745 LOG(1, s
, t
, "Short IPV6CP %d bytes\n", l
);
746 STAT(tunnel_rx_errors
);
751 // happy with our IPV6CP
752 session
[s
].flags
|= SF_IPV6CP_ACKED
;
754 LOG(3, s
, t
, "IPV6CP Acked, IPv6 is now active\n");
755 // Add a routed block if configured.
756 if (session
[s
].ipv6prefixlen
)
758 route6set(s
, session
[s
].ipv6route
, session
[s
].ipv6prefixlen
, 1);
759 session
[s
].flags
|= SF_IPV6_ROUTED
;
762 // Send an initial RA (TODO: Should we send these regularly?)
763 send_ipv6_ra(t
, s
, NULL
);
768 LOG(1, s
, t
, "Unexpected IPV6CP code %d\n", *p
);
769 STAT(tunnel_rx_errors
);
773 LOG(4, s
, t
, "IPV6CP ConfigReq received\n");
774 if (ntohs(*(uint16_t *) (p
+ 2)) > l
)
776 LOG(1, s
, t
, "Length mismatch IPV6CP %d/%d\n", ntohs(*(uint16_t *) (p
+ 2)), l
);
777 STAT(tunnel_rx_errors
);
782 LOG(3, s
, t
, "Waiting on radius reply\n");
783 return; // have to wait on RADIUS reply
785 // form a config reply quoting the IP in the session
787 uint8_t b
[MAXCONTROL
];
791 l
= ntohs(*(uint16_t *) (p
+ 2)); // We must use length from IPV6CP len field
794 while (q
< i
&& q
[1])
805 if (!(q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPIPV6CP
)))
807 LOG(2, s
, t
, "Failed to send IPV6CP ConfigRej\n");
812 while (p
< i
&& p
[1])
816 LOG(2, s
, t
, "IPV6CP reject %d\n", *p
);
817 memcpy(q
+ n
, p
, p
[1]);
822 *(uint16_t *) (q
+ 2) = htons(n
);
823 LOG(4, s
, t
, "Sending ConfigRej\n");
824 tunnelsend(b
, n
+ (q
- b
), t
); // send it
828 LOG(4, s
, t
, "Sending ConfigAck\n");
830 i
= findppp(p
, 1); // IP address
831 if (!i
|| i
[1] != 10)
833 LOG(1, s
, t
, "No IP in IPV6CP request\n");
834 STAT(tunnel_rx_errors
);
837 if ((*(uint32_t *) (i
+ 2) != htonl(session
[s
].ip
)) ||
838 (*(uint32_t *) (i
+ 6) != 0))
840 *(uint32_t *) (i
+ 2) = htonl(session
[s
].ip
);
841 *(uint32_t *) (i
+ 6) = 0;
844 " No, a ConfigNak, client is "
845 "requesting IP - sending %s\n",
846 fmtaddr(htonl(session
[s
].ip
), 0));
848 if (!(q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPIPV6CP
)))
850 LOG(2, s
, t
, " Failed to send IPV6CP packet.\n");
853 tunnelsend(b
, l
+ (q
- b
), t
); // send it
858 // process IP packet received
860 // This MUST be called with at least 4 byte behind 'p'.
861 // (i.e. this routine writes to p[-4]).
862 void processipin(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
868 LOG_HEX(5, "IP", p
, l
);
870 ip
= ntohl(*(uint32_t *)(p
+ 12));
874 LOG(1, s
, t
, "IP packet too long %d\n", l
);
875 STAT(tunnel_rx_errors
);
879 // no spoof (do sessionbyip to handled statically routed subnets)
880 if (ip
!= session
[s
].ip
&& sessionbyip(htonl(ip
)) != s
)
882 LOG(5, s
, t
, "Dropping packet with spoofed IP %s\n", fmtaddr(htonl(ip
), 0));
886 // run access-list if any
887 if (session
[s
].filter_in
&& !ip_filter(p
, l
, session
[s
].filter_in
- 1))
890 // Add on the tun header
892 *(uint32_t *) p
= htonl(PKTIP
);
895 // Are we throttled and a slave?
896 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) {
897 // Pass it to the master for handling.
898 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
902 // Are we throttled and a master??
903 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) {
904 // Actually handle the throttled packets.
905 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
910 if (tun_write(p
, l
) < 0)
913 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
914 l
, strerror(errno
), tunfd
, p
);
919 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
921 // Snooping this session
922 snoop_send_packet(p
+ 4, l
- 4, session
[s
].snoop_ip
, session
[s
].snoop_port
);
925 session
[s
].cin
+= l
- 4;
926 session
[s
].total_cin
+= l
- 4;
927 sess_local
[s
].cin
+= l
- 4;
932 STAT(tun_tx_packets
);
933 INC_STAT(tun_tx_bytes
, l
- 4);
936 // process IPv6 packet received
938 // This MUST be called with at least 4 byte behind 'p'.
939 // (i.e. this routine writes to p[-4]).
940 void processipv6in(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
945 CSTAT(processipv6in
);
947 LOG_HEX(5, "IPv6", p
, l
);
949 ip
= *(struct in6_addr
*) (p
+ 8);
950 ipv4
= ntohl(*(uint32_t *)(p
+ 16));
954 LOG(1, s
, t
, "IP packet too long %d\n", l
);
955 STAT(tunnel_rx_errors
);
960 if (ipv4
!= session
[s
].ip
&& memcmp(&config
->ipv6_prefix
, &ip
, 8) && sessionbyipv6(ip
) != s
)
962 char str
[INET6_ADDRSTRLEN
];
963 LOG(5, s
, t
, "Dropping packet with spoofed IP %s\n",
964 inet_ntop(AF_INET6
, &ip
, str
, INET6_ADDRSTRLEN
));
968 // Check if it's a Router Solicition message.
969 if (*(p
+ 6) == 58 && *(p
+ 7) == 255 && *(p
+ 24) == 0xFF && *(p
+ 25) == 2 &&
970 *(uint32_t *)(p
+ 26) == 0 && *(uint32_t *)(p
+ 30) == 0 &&
971 *(uint32_t *)(p
+ 34) == 0 &&
972 *(p
+ 38) == 0 && *(p
+ 39) == 2 && *(p
+ 40) == 133) {
973 LOG(3, s
, t
, "Got IPv6 RS\n");
974 send_ipv6_ra(t
, s
, &ip
);
978 // Add on the tun header
980 *(uint32_t *) p
= htonl(PKTIPV6
);
983 // Are we throttled and a slave?
984 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) {
985 // Pass it to the master for handling.
986 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
990 // Are we throttled and a master??
991 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) {
992 // Actually handle the throttled packets.
993 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
998 if (tun_write(p
, l
) < 0)
1000 STAT(tun_tx_errors
);
1001 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1002 l
, strerror(errno
), tunfd
, p
);
1007 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1009 // Snooping this session
1010 snoop_send_packet(p
+ 4, l
- 4, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1013 session
[s
].cin
+= l
- 4;
1014 session
[s
].total_cin
+= l
- 4;
1015 sess_local
[s
].cin
+= l
- 4;
1020 STAT(tun_tx_packets
);
1021 INC_STAT(tun_tx_bytes
, l
- 4);
1025 // Helper routine for the TBF filters.
1026 // Used to send queued data in from the user.
1028 void send_ipin(sessionidt s
, uint8_t *buf
, int len
)
1030 LOG_HEX(5, "IP in throttled", buf
, len
);
1032 if (write(tunfd
, buf
, len
) < 0)
1034 STAT(tun_tx_errors
);
1035 LOG(0, 0, 0, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1036 len
, strerror(errno
), tunfd
, buf
);
1041 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1043 // Snooping this session
1044 snoop_send_packet(buf
+ 4, len
- 4, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1047 // Increment packet counters
1048 session
[s
].cin
+= len
- 4;
1049 session
[s
].total_cin
+= len
- 4;
1050 sess_local
[s
].cin
+= len
- 4;
1055 STAT(tun_tx_packets
);
1056 INC_STAT(tun_tx_bytes
, len
- 4);
1060 // Process CCP messages
1061 void processccp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
1063 uint8_t b
[MAXCONTROL
];
1068 LOG_HEX(5, "CCP", p
, l
);
1069 switch (l
> 1 ? *p
: 0)
1072 session
[s
].flags
|= SF_CCP_ACKED
;
1076 if (l
< 6) // accept no compression
1082 // compression requested--reject
1085 // send CCP request for no compression for our end if not negotiated
1086 if (!(session
[s
].flags
& SF_CCP_ACKED
))
1097 LOG(1, s
, t
, "Unexpected CCP request code %d\n", *p
);
1099 LOG(1, s
, t
, "Short CCP packet\n");
1101 STAT(tunnel_rx_errors
);
1105 if (!(q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPCCP
)))
1108 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1111 // send a CHAP challenge
1112 void sendchap(tunnelidt t
, sessionidt s
)
1114 uint8_t b
[MAXCONTROL
];
1115 uint16_t r
= sess_local
[s
].radius
;
1122 LOG(1, s
, t
, "No RADIUS to send challenge\n");
1123 STAT(tunnel_tx_errors
);
1127 LOG(1, s
, t
, "Send CHAP challenge\n");
1129 radius
[r
].chap
= 1; // CHAP not PAP
1131 if (radius
[r
].state
!= RADIUSCHAP
)
1134 radius
[r
].state
= RADIUSCHAP
;
1135 radius
[r
].retry
= backoff(radius
[r
].try++);
1136 if (radius
[r
].try > 5)
1138 sessionshutdown(s
, "CHAP timeout.", 3, 0);
1139 STAT(tunnel_tx_errors
);
1142 q
= makeppp(b
, sizeof(b
), 0, 0, t
, s
, PPPCHAP
);
1145 *q
= 1; // challenge
1146 q
[1] = radius
[r
].id
; // ID
1147 q
[4] = 16; // value size (size of challenge)
1148 memcpy(q
+ 5, radius
[r
].auth
, 16); // challenge
1149 strcpy(q
+ 21, hostname
); // our name
1150 *(uint16_t *) (q
+ 2) = htons(strlen(hostname
) + 21); // length
1151 tunnelsend(b
, strlen(hostname
) + 21 + (q
- b
), t
); // send it
1154 // fill in a L2TP message with a PPP frame,
1155 // copies existing PPP message and changes magic number if seen
1156 // returns start of PPP frame
1157 uint8_t *makeppp(uint8_t *b
, int size
, uint8_t *p
, int l
, tunnelidt t
, sessionidt s
, uint16_t mtype
)
1159 if (size
< 12) // Need more space than this!!
1161 static int backtrace_count
= 0;
1162 LOG(0, s
, t
, "makeppp buffer too small for L2TP header (size=%d)\n", size
);
1163 log_backtrace(backtrace_count
, 5)
1167 *(uint16_t *) (b
+ 0) = htons(0x0002); // L2TP with no options
1168 *(uint16_t *) (b
+ 2) = htons(tunnel
[t
].far
); // tunnel
1169 *(uint16_t *) (b
+ 4) = htons(session
[s
].far
); // session
1171 if (mtype
== PPPLCP
|| !(session
[s
].l2tp_flags
& SESSIONACFC
))
1173 *(uint16_t *) b
= htons(0xFF03); // HDLC header
1176 if (mtype
< 0x100 && session
[s
].l2tp_flags
& SESSIONPFC
)
1180 *(uint16_t *) b
= htons(mtype
);
1186 static int backtrace_count
= 0;
1187 LOG(2, s
, t
, "makeppp would overflow buffer (size=%d, header+payload=%d)\n", size
, l
+ 12);
1188 log_backtrace(backtrace_count
, 5)
1198 // Send initial LCP ConfigReq for preferred authentication type, set magic no and MRU
1199 void initlcp(tunnelidt t
, sessionidt s
)
1201 char b
[500], *q
, *l
;
1203 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, t
, s
, PPPLCP
)))
1206 LOG(4, s
, t
, "Sending LCP ConfigReq for %s\n",
1207 config
->radius_authprefer
== AUTHCHAP
? "CHAP" : "PAP");
1209 if (!session
[s
].mru
)
1210 session
[s
].mru
= DEFAULT_MRU
;
1214 *l
++ = (time_now
% 255) + 1; // ID
1216 *l
++ = 1; *l
++ = 4; // Maximum-Receive-Unit (length 4)
1217 *(uint16_t *) l
= htons(session
[s
].mru
); l
+= 2;
1219 *l
++ = 3; // Authentication-Protocol
1220 if (config
->radius_authprefer
== AUTHCHAP
)
1223 *(uint16_t *) l
= htons(PPPCHAP
); l
+= 2;
1229 *(uint16_t *) l
= htons(PPPPAP
); l
+= 2;
1232 *l
++ = 5; *l
++ = 6; // Magic-Number (length 6)
1233 *(uint32_t *) l
= htonl(session
[s
].magic
);
1236 *(uint16_t *)(q
+ 2) = htons(l
- q
); // Length
1238 LOG_HEX(5, "PPPLCP", q
, l
- q
);
1239 tunnelsend(b
, (l
- b
), t
);
1242 // Send CCP request for no compression
1243 static void initccp(tunnelidt t
, sessionidt s
)
1247 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, t
, s
, PPPCCP
)))
1250 LOG(4, s
, t
, "Sending CCP ConfigReq for no compression\n");
1252 *(uint8_t *)(q
+ 1) = (time_now
% 255) + 1; // ID
1253 *(uint16_t *)(q
+ 2) = htons(4); // Length
1255 LOG_HEX(5, "PPPCCP", q
, 4);
1256 tunnelsend(b
, (q
- b
) + 4 , t
);