fbc72d8e073e4a674bb7c3d6bdf1e94ab349a969
3 char const *cvs_id_ppp
= "$Id: ppp.c,v 1.47 2005-04-27 13:53:17 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
|| !session
[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
= session
[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
= session
[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
== PPPCHAP
? "CHAP" :
283 proto
== PPPPAP
? "PAP" : "UNKNOWN");
286 LOG(4, 0, 0, " %s odd length %d\n", lcp_type(type
), length
);
288 case 4: // Quality-Protocol
290 uint32_t qp
= ntohl(*(uint32_t *)(o
+ 2));
291 LOG(4, 0, 0, " %s %x\n", lcp_type(type
), qp
);
294 case 5: // Magic-Number
297 uint32_t magicno
= ntohl(*(uint32_t *)(o
+ 2));
298 LOG(4, 0, 0, " %s %x\n", lcp_type(type
), magicno
);
301 LOG(4, 0, 0, " %s odd length %d\n", lcp_type(type
), length
);
303 case 7: // Protocol-Field-Compression
304 case 8: // Address-And-Control-Field-Compression
305 LOG(4, 0, 0, " %s\n", lcp_type(type
));
308 LOG(2, 0, 0, " Unknown PPP LCP Option type %d\n", type
);
316 // Process LCP messages
317 void processlcp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
319 uint8_t b
[MAXCONTROL
];
321 uint32_t magicno
= 0;
326 LOG_HEX(5, "LCP", p
, l
);
329 LOG(1, s
, t
, "Short LCP %d bytes\n", l
);
330 STAT(tunnel_rx_errors
);
334 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
336 LOG(1, s
, t
, "Length mismatch LCP %u/%u\n", hl
, l
);
337 STAT(tunnel_rx_errors
);
344 LOG(3, s
, t
, "LCP: Discarding ConfigAck\n");
345 session
[s
].flags
|= SF_LCP_ACKED
;
347 else if (*p
== ConfigReq
)
350 uint8_t *o
= (p
+ 4);
351 uint8_t *response
= 0;
353 LOG(3, s
, t
, "LCP: ConfigReq (%d bytes)...\n", l
);
354 if (config
->debug
> 3) dumplcp(p
, l
);
361 if (length
== 0 || type
== 0 || x
< length
) break;
364 case 1: // Maximum-Receive-Unit
365 session
[s
].mru
= ntohs(*(uint16_t *)(o
+ 2));
368 case 2: // Async-Control-Character-Map
369 if (!ntohl(*(uint32_t *)(o
+ 2))) // all bits zero is OK
372 if (response
&& *response
!= ConfigNak
) // rej already queued
375 LOG(2, s
, t
, " Remote requesting asyncmap. Rejecting.\n");
378 q
= response
= makeppp(b
, sizeof(b
), p
, 2, t
, s
, PPPLCP
);
384 if ((q
- b
+ 11) > sizeof(b
))
386 LOG(2, s
, t
, "LCP overflow for asyncmap ConfigNak.\n");
392 memset(q
, 0, 4); // asyncmap 0
394 *((uint16_t *) (response
+ 2)) = htons(q
- response
); // LCP header length
397 case 3: // Authentication-Protocol
399 int proto
= ntohs(*(uint16_t *)(o
+ 2));
400 char proto_name
[] = "0x0000";
404 if (response
&& *response
!= ConfigNak
) // rej already queued
407 if (proto
== PPPCHAP
)
408 strcpy(proto_name
, "CHAP");
410 sprintf(proto_name
, "%#4.4x", proto
);
412 LOG(2, s
, t
, " Remote requesting %s authentication. Rejecting.\n", proto_name
);
416 q
= response
= makeppp(b
, sizeof(b
), p
, 2, t
, s
, PPPLCP
);
422 if ((q
- b
+ length
) > sizeof(b
))
424 LOG(2, s
, t
, "LCP overflow for %s ConfigNak.\n", proto_name
);
428 memcpy(q
, o
, length
);
429 *(uint16_t *)(q
+= 2) = htons(PPPPAP
); // NAK -> Use PAP instead
431 *((uint16_t *) (response
+ 2)) = htons(q
- response
);
435 case 5: // Magic-Number
436 magicno
= ntohl(*(uint32_t *)(o
+ 2));
439 case 4: // Quality-Protocol
440 case 7: // Protocol-Field-Compression
441 case 8: // Address-And-Control-Field-Compression
444 default: // Reject any unknown options
445 LOG(2, s
, t
, " Rejecting PPP LCP Option type %d\n", type
);
446 if (!response
|| *response
!= ConfigRej
) // drop nak in favour of rej
448 q
= response
= makeppp(b
, sizeof(b
), p
, 2, t
, s
, PPPLCP
);
454 if ((q
- b
+ length
) > sizeof(b
))
456 LOG(2, s
, t
, "LCP overflow for ConfigRej (type=%d).\n", type
);
460 memcpy(q
, o
, length
);
462 *((uint16_t *) (response
+ 2)) = htons(q
- response
); // LCP header length
470 // Send back a ConfigAck
471 q
= response
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
476 LOG(3, s
, t
, "Sending %s\n", ppp_lcp_type(*response
));
477 tunnelsend(b
, l
+ (q
- b
), t
);
479 if (!(session
[s
].flags
& SF_LCP_ACKED
))
482 else if (*p
== ConfigNak
)
484 LOG(1, s
, t
, "Remote end sent a ConfigNak. Ignoring\n");
485 if (config
->debug
> 3) dumplcp(p
, l
);
488 else if (*p
== TerminateReq
)
490 LOG(3, s
, t
, "LCP: Received TerminateReq. Sending TerminateAck\n");
491 *p
= TerminateAck
; // close
492 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
494 tunnelsend(b
, l
+ (q
- b
), t
); // send it
495 sessionshutdown(s
, "Remote end closed connection.", 3, 0);
497 else if (*p
== TerminateAck
)
499 sessionshutdown(s
, "Connection closed.", 3, 0);
501 else if (*p
== ProtocolRej
)
503 if (*(uint16_t *) (p
+4) == htons(PPPIPV6CP
))
505 LOG(3, s
, t
, "IPv6 rejected\n");
506 session
[s
].flags
|= SF_IPV6_NACKED
;
510 LOG(1, s
, t
, "Unexpected LCP protocol reject 0x%X\n",
511 ntohs(*(uint16_t *) (p
+4)));
512 STAT(tunnel_rx_errors
);
515 else if (*p
== EchoReq
)
517 LOG(5, s
, t
, "LCP: Received EchoReq. Sending EchoReply\n");
518 *p
= EchoReply
; // reply
519 *(uint32_t *) (p
+ 4) = htonl(session
[s
].magic
); // our magic number
520 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
522 tunnelsend(b
, l
+ (q
- b
), t
); // send it
524 else if (*p
== EchoReply
)
526 // Ignore it, last_packet time is set earlier than this.
528 else if (*p
== IdentRequest
)
533 LOG(1, s
, t
, "Truncated Ident Packet (length=%d) to 1400 bytes\n", l
);
536 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
538 LOG_HEX(5, "LCPIdentRej", q
, l
+ 4);
539 tunnelsend(b
, 12 + 4 + l
, t
);
543 LOG(1, s
, t
, "Unexpected LCP code %d\n", *p
);
544 STAT(tunnel_rx_errors
);
549 // find a PPP option, returns point to option, or 0 if not found
550 static uint8_t *findppp(uint8_t *b
, uint8_t mtype
)
552 uint16_t l
= ntohs(*(uint16_t *) (b
+ 2));
559 if (l
< b
[1] || !b
[1])
569 // Process IPCP messages
570 void processipcp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
576 LOG_HEX(5, "IPCP", p
, l
);
579 LOG(1, s
, t
, "Short IPCP %d bytes\n", l
);
580 STAT(tunnel_rx_errors
);
584 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
586 LOG(1, s
, t
, "Length mismatch IPCP %u/%u\n", hl
, l
);
587 STAT(tunnel_rx_errors
);
594 // happy with our IPCP
595 uint16_t r
= session
[s
].radius
;
596 if ((!r
|| radius
[r
].state
== RADIUSIPCP
) && !session
[s
].walled_garden
)
601 radiussend(r
, RADIUSSTART
); // send radius start, having got IPCP at last
603 session
[s
].flags
|= SF_IPCP_ACKED
;
605 LOG(3, s
, t
, "IPCP Acked, session is now active\n");
607 // clear LCP_ACKED/CCP_ACKED flag for possible fast renegotiaion for routers
608 session
[s
].flags
&= ~(SF_LCP_ACKED
|SF_CCP_ACKED
);
614 LOG(1, s
, t
, "Unexpected IPCP code %d\n", *p
);
615 STAT(tunnel_rx_errors
);
618 LOG(4, s
, t
, "IPCP ConfigReq received\n");
622 LOG(3, s
, t
, "Waiting on radius reply\n");
623 return; // have to wait on RADIUS reply
625 // form a config reply quoting the IP in the session
627 uint8_t b
[MAXCONTROL
];
632 while (q
< i
&& q
[1])
634 if (*q
!= 0x81 && *q
!= 0x83 && *q
!= 3)
643 if (!(q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPIPCP
)))
648 while (p
< i
&& p
[1])
650 if (*p
!= 0x81 && *p
!= 0x83 && *p
!= 3)
652 LOG(2, s
, t
, "IPCP reject %d\n", *p
);
653 memcpy(q
+ n
, p
, p
[1]);
658 *(uint16_t *) (q
+ 2) = htons(n
);
659 LOG(4, s
, t
, "Sending ConfigRej\n");
660 tunnelsend(b
, n
+ (q
- b
), t
); // send it
664 LOG(4, s
, t
, "Sending ConfigAck\n");
666 if ((i
= findppp(p
, 0x81))) // Primary DNS address
668 if (*(uint32_t *) (i
+ 2) != htonl(session
[s
].dns1
))
670 *(uint32_t *) (i
+ 2) = htonl(session
[s
].dns1
);
672 LOG(5, s
, t
, " DNS1 = %s\n",
673 fmtaddr(htonl(session
[s
].dns1
), 0));
676 if ((i
= findppp(p
, 0x83))) // Secondary DNS address (TBA, is it)
678 if (*(uint32_t *) (i
+ 2) != htonl(session
[s
].dns2
))
680 *(uint32_t *) (i
+ 2) = htonl(session
[s
].dns2
);
682 LOG(5, s
, t
, " DNS2 = %s\n",
683 fmtaddr(htonl(session
[s
].dns2
), 0));
686 i
= findppp(p
, 3); // IP address
689 LOG(1, s
, t
, "No IP in IPCP request\n");
690 STAT(tunnel_rx_errors
);
693 if (*(uint32_t *) (i
+ 2) != htonl(session
[s
].ip
))
695 *(uint32_t *) (i
+ 2) = htonl(session
[s
].ip
);
697 LOG(4, s
, t
, " No, a ConfigNak, client is requesting IP - sending %s\n",
698 fmtaddr(htonl(session
[s
].ip
), 0));
700 if (!(q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPIPCP
)))
703 tunnelsend(b
, l
+ (q
- b
), t
); // send it
708 // Process IPV6CP messages
709 void processipv6cp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
712 CSTAT(processipv6cp
);
714 LOG_HEX(5, "IPV6CP", p
, l
);
717 LOG(1, s
, t
, "Short IPV6CP %d bytes\n", l
);
718 STAT(tunnel_rx_errors
);
723 // happy with our IPV6CP
724 session
[s
].flags
|= SF_IPV6CP_ACKED
;
726 LOG(3, s
, t
, "IPV6CP Acked, IPv6 is now active\n");
727 // Add a routed block if configured.
728 if (session
[s
].ipv6prefixlen
)
730 route6set(s
, session
[s
].ipv6route
, session
[s
].ipv6prefixlen
, 1);
731 session
[s
].flags
|= SF_IPV6_ROUTED
;
734 // Send an initial RA (TODO: Should we send these regularly?)
735 send_ipv6_ra(t
, s
, NULL
);
740 LOG(1, s
, t
, "Unexpected IPV6CP code %d\n", *p
);
741 STAT(tunnel_rx_errors
);
745 LOG(4, s
, t
, "IPV6CP ConfigReq received\n");
746 if (ntohs(*(uint16_t *) (p
+ 2)) > l
)
748 LOG(1, s
, t
, "Length mismatch IPV6CP %d/%d\n", ntohs(*(uint16_t *) (p
+ 2)), l
);
749 STAT(tunnel_rx_errors
);
754 LOG(3, s
, t
, "Waiting on radius reply\n");
755 return; // have to wait on RADIUS reply
757 // form a config reply quoting the IP in the session
759 uint8_t b
[MAXCONTROL
];
763 l
= ntohs(*(uint16_t *) (p
+ 2)); // We must use length from IPV6CP len field
766 while (q
< i
&& q
[1])
777 if (!(q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPIPV6CP
)))
779 LOG(2, s
, t
, "Failed to send IPV6CP ConfigRej\n");
784 while (p
< i
&& p
[1])
788 LOG(2, s
, t
, "IPV6CP reject %d\n", *p
);
789 memcpy(q
+ n
, p
, p
[1]);
794 *(uint16_t *) (q
+ 2) = htons(n
);
795 LOG(4, s
, t
, "Sending ConfigRej\n");
796 tunnelsend(b
, n
+ (q
- b
), t
); // send it
800 LOG(4, s
, t
, "Sending ConfigAck\n");
802 i
= findppp(p
, 1); // IP address
803 if (!i
|| i
[1] != 10)
805 LOG(1, s
, t
, "No IP in IPV6CP request\n");
806 STAT(tunnel_rx_errors
);
809 if ((*(uint32_t *) (i
+ 2) != htonl(session
[s
].ip
)) ||
810 (*(uint32_t *) (i
+ 6) != 0))
812 *(uint32_t *) (i
+ 2) = htonl(session
[s
].ip
);
813 *(uint32_t *) (i
+ 6) = 0;
816 " No, a ConfigNak, client is "
817 "requesting IP - sending %s\n",
818 fmtaddr(htonl(session
[s
].ip
), 0));
820 if (!(q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPIPV6CP
)))
822 LOG(2, s
, t
, " Failed to send IPV6CP packet.\n");
825 tunnelsend(b
, l
+ (q
- b
), t
); // send it
830 // process IP packet received
832 // This MUST be called with at least 4 byte behind 'p'.
833 // (i.e. this routine writes to p[-4]).
834 void processipin(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
840 LOG_HEX(5, "IP", p
, l
);
842 ip
= ntohl(*(uint32_t *)(p
+ 12));
846 LOG(1, s
, t
, "IP packet too long %d\n", l
);
847 STAT(tunnel_rx_errors
);
851 // no spoof (do sessionbyip to handled statically routed subnets)
852 if (ip
!= session
[s
].ip
&& sessionbyip(htonl(ip
)) != s
)
854 LOG(5, s
, t
, "Dropping packet with spoofed IP %s\n", fmtaddr(htonl(ip
), 0));
858 // run access-list if any
859 if (session
[s
].filter_in
&& !ip_filter(p
, l
, session
[s
].filter_in
- 1))
862 // Add on the tun header
864 *(uint32_t *) p
= htonl(PKTIP
);
867 // Are we throttled and a slave?
868 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) {
869 // Pass it to the master for handling.
870 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
874 // Are we throttled and a master??
875 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) {
876 // Actually handle the throttled packets.
877 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
882 if (tun_write(p
, l
) < 0)
885 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
886 l
, strerror(errno
), tunfd
, p
);
891 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
893 // Snooping this session
894 snoop_send_packet(p
+ 4, l
- 4, session
[s
].snoop_ip
, session
[s
].snoop_port
);
897 session
[s
].cin
+= l
- 4;
898 session
[s
].total_cin
+= l
- 4;
899 sess_local
[s
].cin
+= l
- 4;
904 STAT(tun_tx_packets
);
905 INC_STAT(tun_tx_bytes
, l
- 4);
908 // process IPv6 packet received
910 // This MUST be called with at least 4 byte behind 'p'.
911 // (i.e. this routine writes to p[-4]).
912 void processipv6in(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
917 CSTAT(processipv6in
);
919 LOG_HEX(5, "IPv6", p
, l
);
921 ip
= *(struct in6_addr
*) (p
+ 8);
922 ipv4
= ntohl(*(uint32_t *)(p
+ 16));
926 LOG(1, s
, t
, "IP packet too long %d\n", l
);
927 STAT(tunnel_rx_errors
);
932 if (ipv4
!= session
[s
].ip
&& memcmp(&config
->ipv6_prefix
, &ip
, 8) && sessionbyipv6(ip
) != s
)
934 char str
[INET6_ADDRSTRLEN
];
935 LOG(5, s
, t
, "Dropping packet with spoofed IP %s\n",
936 inet_ntop(AF_INET6
, &ip
, str
, INET6_ADDRSTRLEN
));
940 // Check if it's a Router Solicition message.
941 if (*(p
+ 6) == 58 && *(p
+ 7) == 255 && *(p
+ 24) == 0xFF && *(p
+ 25) == 2 &&
942 *(uint32_t *)(p
+ 26) == 0 && *(uint32_t *)(p
+ 30) == 0 &&
943 *(uint32_t *)(p
+ 34) == 0 &&
944 *(p
+ 38) == 0 && *(p
+ 39) == 2 && *(p
+ 40) == 133) {
945 LOG(3, s
, t
, "Got IPv6 RS\n");
946 send_ipv6_ra(t
, s
, &ip
);
950 // Add on the tun header
952 *(uint32_t *) p
= htonl(PKTIPV6
);
955 // Are we throttled and a slave?
956 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) {
957 // Pass it to the master for handling.
958 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
962 // Are we throttled and a master??
963 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) {
964 // Actually handle the throttled packets.
965 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
970 if (tun_write(p
, l
) < 0)
973 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
974 l
, strerror(errno
), tunfd
, p
);
979 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
981 // Snooping this session
982 snoop_send_packet(p
+ 4, l
- 4, session
[s
].snoop_ip
, session
[s
].snoop_port
);
985 session
[s
].cin
+= l
- 4;
986 session
[s
].total_cin
+= l
- 4;
987 sess_local
[s
].cin
+= l
- 4;
992 STAT(tun_tx_packets
);
993 INC_STAT(tun_tx_bytes
, l
- 4);
997 // Helper routine for the TBF filters.
998 // Used to send queued data in from the user.
1000 void send_ipin(sessionidt s
, uint8_t *buf
, int len
)
1002 LOG_HEX(5, "IP in throttled", buf
, len
);
1004 if (write(tunfd
, buf
, len
) < 0)
1006 STAT(tun_tx_errors
);
1007 LOG(0, 0, 0, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1008 len
, strerror(errno
), tunfd
, buf
);
1013 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1015 // Snooping this session
1016 snoop_send_packet(buf
+ 4, len
- 4, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1019 // Increment packet counters
1020 session
[s
].cin
+= len
- 4;
1021 session
[s
].total_cin
+= len
- 4;
1022 sess_local
[s
].cin
+= len
- 4;
1027 STAT(tun_tx_packets
);
1028 INC_STAT(tun_tx_bytes
, len
- 4);
1032 // Process CCP messages
1033 void processccp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
1035 uint8_t b
[MAXCONTROL
];
1040 LOG_HEX(5, "CCP", p
, l
);
1041 switch (l
> 1 ? *p
: 0)
1044 session
[s
].flags
|= SF_CCP_ACKED
;
1048 if (l
< 6) // accept no compression
1054 // compression requested--reject
1057 // send CCP request for no compression for our end if not negotiated
1058 if (!(session
[s
].flags
& SF_CCP_ACKED
))
1069 LOG(1, s
, t
, "Unexpected CCP request code %d\n", *p
);
1071 LOG(1, s
, t
, "Short CCP packet\n");
1073 STAT(tunnel_rx_errors
);
1077 if (!(q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPCCP
)))
1080 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1083 // send a CHAP challenge
1084 void sendchap(tunnelidt t
, sessionidt s
)
1086 uint8_t b
[MAXCONTROL
];
1087 uint16_t r
= session
[s
].radius
;
1095 LOG(1, s
, t
, "No RADIUS to send challenge\n");
1096 STAT(tunnel_tx_errors
);
1100 LOG(1, s
, t
, "Send CHAP challenge\n");
1103 random_data(radius
[r
].auth
, sizeof(radius
[r
].auth
));
1104 radius
[r
].chap
= 1; // CHAP not PAP
1106 if (radius
[r
].state
!= RADIUSCHAP
)
1109 radius
[r
].state
= RADIUSCHAP
;
1110 radius
[r
].retry
= backoff(radius
[r
].try++);
1111 if (radius
[r
].try > 5)
1113 sessionshutdown(s
, "CHAP timeout.", 3, 0);
1114 STAT(tunnel_tx_errors
);
1117 q
= makeppp(b
, sizeof(b
), 0, 0, t
, s
, PPPCHAP
);
1120 *q
= 1; // challenge
1121 q
[1] = radius
[r
].id
; // ID
1122 q
[4] = 16; // value size (size of challenge)
1123 memcpy(q
+ 5, radius
[r
].auth
, 16); // challenge
1124 strcpy(q
+ 21, hostname
); // our name
1125 *(uint16_t *) (q
+ 2) = htons(strlen(hostname
) + 21); // length
1126 tunnelsend(b
, strlen(hostname
) + 21 + (q
- b
), t
); // send it
1129 // fill in a L2TP message with a PPP frame,
1130 // copies existing PPP message and changes magic number if seen
1131 // returns start of PPP frame
1132 uint8_t *makeppp(uint8_t *b
, int size
, uint8_t *p
, int l
, tunnelidt t
, sessionidt s
, uint16_t mtype
)
1134 if (size
< 12) // Need more space than this!!
1136 static int backtrace_count
= 0;
1137 LOG(0, s
, t
, "makeppp buffer too small for L2TP header (size=%d)\n", size
);
1138 log_backtrace(backtrace_count
, 5)
1142 *(uint16_t *) (b
+ 0) = htons(0x0002); // L2TP with no options
1143 *(uint16_t *) (b
+ 2) = htons(tunnel
[t
].far
); // tunnel
1144 *(uint16_t *) (b
+ 4) = htons(session
[s
].far
); // session
1146 if (mtype
== PPPLCP
|| !(session
[s
].l2tp_flags
& SESSIONACFC
))
1148 *(uint16_t *) b
= htons(0xFF03); // HDLC header
1151 if (mtype
< 0x100 && session
[s
].l2tp_flags
& SESSIONPFC
)
1155 *(uint16_t *) b
= htons(mtype
);
1161 static int backtrace_count
= 0;
1162 LOG(2, s
, t
, "makeppp would overflow buffer (size=%d, header+payload=%d)\n", size
, l
+ 12);
1163 log_backtrace(backtrace_count
, 5)
1173 // Send initial LCP ConfigReq for PAP, set magic no.
1174 void initlcp(tunnelidt t
, sessionidt s
)
1178 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, t
, s
, PPPLCP
)))
1181 LOG(4, s
, t
, "Sending LCP ConfigReq for PAP\n");
1183 *(uint8_t *)(q
+ 1) = (time_now
% 255) + 1; // ID
1184 *(uint16_t *)(q
+ 2) = htons(14); // Length
1185 *(uint8_t *)(q
+ 4) = 5;
1186 *(uint8_t *)(q
+ 5) = 6;
1187 *(uint32_t *)(q
+ 6) = htonl(session
[s
].magic
);
1188 *(uint8_t *)(q
+ 10) = 3;
1189 *(uint8_t *)(q
+ 11) = 4;
1190 *(uint16_t *)(q
+ 12) = htons(PPPPAP
); // PAP
1192 LOG_HEX(5, "PPPLCP", q
, 14);
1193 tunnelsend(b
, (q
- b
) + 14, t
);
1196 // Send CCP request for no compression
1197 static void initccp(tunnelidt t
, sessionidt s
)
1201 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, t
, s
, PPPCCP
)))
1204 LOG(4, s
, t
, "Sending CCP ConfigReq for no compression\n");
1206 *(uint8_t *)(q
+ 1) = (time_now
% 255) + 1; // ID
1207 *(uint16_t *)(q
+ 2) = htons(4); // Length
1209 LOG_HEX(5, "PPPCCP", q
, 4);
1210 tunnelsend(b
, (q
- b
) + 4 , t
);