08351e5cce8447f5754ffc9af5c14a78c6ac1eb0
3 char const *cvs_id_ppp
= "$Id: ppp.c,v 1.52 2005-05-07 13:12:26 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
);
27 static uint8_t *add_lcp_auth(uint8_t *b
, int size
, int authtype
);
29 // Process PAP messages
30 void processpap(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
38 LOG_HEX(5, "PAP", p
, l
);
41 LOG(1, s
, t
, "Short PAP %u bytes\n", l
);
42 STAT(tunnel_rx_errors
);
43 sessionshutdown(s
, "Short PAP packet.", 3, 0);
47 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
49 LOG(1, s
, t
, "Length mismatch PAP %u/%u\n", hl
, l
);
50 STAT(tunnel_rx_errors
);
51 sessionshutdown(s
, "PAP length mismatch.", 3, 0);
58 LOG(1, s
, t
, "Unexpected PAP code %d\n", *p
);
59 STAT(tunnel_rx_errors
);
60 sessionshutdown(s
, "Unexpected PAP code.", 3, 0);
67 user
[0] = pass
[0] = 0;
68 if (*b
&& *b
< sizeof(user
))
70 memcpy(user
, b
+ 1, *b
);
73 if (*b
&& *b
< sizeof(pass
))
75 memcpy(pass
, b
+ 1, *b
);
79 LOG(3, s
, t
, "PAP login %s/%s\n", user
, pass
);
81 if (session
[s
].ip
|| !sess_local
[s
].radius
)
83 // respond now, either no RADIUS available or already authenticated
84 uint8_t b
[MAXCONTROL
];
86 uint8_t *p
= makeppp(b
, sizeof(b
), 0, 0, t
, s
, PPPPAP
);
92 *p
= 3; // cant authorise
94 *(uint16_t *) (p
+ 2) = htons(5); // length
95 p
[4] = 0; // no message
98 LOG(3, s
, t
, "Already an IP allocated: %s (%d)\n",
99 fmtaddr(htonl(session
[s
].ip
), 0), session
[s
].ip_pool_index
);
101 session
[s
].flags
&= ~SF_IPCP_ACKED
;
105 LOG(1, s
, t
, "No radius session available to authenticate session...\n");
107 LOG(3, s
, t
, "Fallback response to PAP (%s)\n", (session
[s
].ip
) ? "ACK" : "NAK");
108 tunnelsend(b
, 5 + (p
- b
), t
); // send it
109 sessionshutdown(s
, "PAP authentication failed.", 3, 0);
113 // set up RADIUS request
114 uint16_t r
= sess_local
[s
].radius
;
116 // Run PRE_AUTH plugins
117 struct param_pre_auth packet
= { &tunnel
[t
], &session
[s
], strdup(user
), strdup(pass
), PPPPAP
, 1 };
118 run_plugins(PLUGIN_PRE_AUTH
, &packet
);
119 if (!packet
.continue_auth
)
121 LOG(3, s
, t
, "A plugin rejected PRE_AUTH\n");
122 if (packet
.username
) free(packet
.username
);
123 if (packet
.password
) free(packet
.password
);
127 strncpy(session
[s
].user
, packet
.username
, sizeof(session
[s
].user
) - 1);
128 strncpy(radius
[r
].pass
, packet
.password
, sizeof(radius
[r
].pass
) - 1);
130 free(packet
.username
);
131 free(packet
.password
);
134 LOG(3, s
, t
, "Sending login for %s/%s to radius\n", user
, pass
);
135 radiussend(r
, RADIUSAUTH
);
139 // Process CHAP messages
140 void processchap(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
147 LOG_HEX(5, "CHAP", p
, l
);
148 r
= sess_local
[s
].radius
;
151 LOG(1, s
, t
, "Unexpected CHAP message\n");
152 STAT(tunnel_rx_errors
);
158 LOG(1, s
, t
, "Short CHAP %u bytes\n", l
);
159 STAT(tunnel_rx_errors
);
163 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
165 LOG(1, s
, t
, "Length mismatch CHAP %u/%u\n", hl
, l
);
166 STAT(tunnel_rx_errors
);
173 LOG(1, s
, t
, "Unexpected CHAP response code %d\n", *p
);
174 STAT(tunnel_rx_errors
);
177 if (p
[1] != radius
[r
].id
)
179 LOG(1, s
, t
, "Wrong CHAP response ID %d (should be %d) (%d)\n", p
[1], radius
[r
].id
, r
);
180 STAT(tunnel_rx_errors
);
184 if (l
< 5 || p
[4] != 16)
186 LOG(1, s
, t
, "Bad CHAP response length %d\n", l
< 5 ? -1 : p
[4]);
187 STAT(tunnel_rx_errors
);
193 if (l
< 16 || l
- 16 >= sizeof(session
[s
].user
))
195 LOG(1, s
, t
, "CHAP user too long %d\n", l
- 16);
196 STAT(tunnel_rx_errors
);
200 // Run PRE_AUTH plugins
202 struct param_pre_auth packet
= { &tunnel
[t
], &session
[s
], NULL
, NULL
, PPPCHAP
, 1 };
204 packet
.password
= calloc(17, 1);
205 memcpy(packet
.password
, p
, 16);
210 packet
.username
= calloc(l
+ 1, 1);
211 memcpy(packet
.username
, p
, l
);
213 run_plugins(PLUGIN_PRE_AUTH
, &packet
);
214 if (!packet
.continue_auth
)
216 LOG(3, s
, t
, "A plugin rejected PRE_AUTH\n");
217 if (packet
.username
) free(packet
.username
);
218 if (packet
.password
) free(packet
.password
);
222 strncpy(session
[s
].user
, packet
.username
, sizeof(session
[s
].user
) - 1);
223 memcpy(radius
[r
].pass
, packet
.password
, 16);
225 free(packet
.username
);
226 free(packet
.password
);
230 LOG(3, s
, t
, "CHAP login %s\n", session
[s
].user
);
231 radiussend(r
, RADIUSAUTH
);
234 static void dumplcp(uint8_t *p
, int l
)
237 uint8_t *o
= (p
+ 4);
239 LOG_HEX(5, "PPP LCP Packet", p
, l
);
240 LOG(4, 0, 0, "PPP LCP Packet type %d (%s len %d)\n", *p
, ppp_lcp_type((int)*p
), ntohs( ((uint16_t *) p
)[1]) );
241 LOG(4, 0, 0, "Length: %d\n", l
);
242 if (*p
!= ConfigReq
&& *p
!= ConfigRej
&& *p
!= ConfigAck
)
251 LOG(4, 0, 0, " Option length is %d...\n", length
);
256 LOG(4, 0, 0, " Option type is 0...\n");
263 case 1: // Maximum-Receive-Unit
265 LOG(4, 0, 0, " %s %d\n", lcp_type(type
), ntohs(*(uint16_t *)(o
+ 2)));
267 LOG(4, 0, 0, " %s odd length %d\n", lcp_type(type
), length
);
269 case 2: // Async-Control-Character-Map
272 uint32_t asyncmap
= ntohl(*(uint32_t *)(o
+ 2));
273 LOG(4, 0, 0, " %s %x\n", lcp_type(type
), asyncmap
);
276 LOG(4, 0, 0, " %s odd length %d\n", lcp_type(type
), length
);
278 case 3: // Authentication-Protocol
281 int proto
= ntohs(*(uint16_t *)(o
+ 2));
282 LOG(4, 0, 0, " %s 0x%x (%s)\n", lcp_type(type
), proto
,
283 proto
== PPPPAP
? "PAP" : "UNSUPPORTED");
285 else if (length
== 5)
287 int proto
= ntohs(*(uint16_t *)(o
+ 2));
288 int algo
= *(uint8_t *)(o
+ 4);
289 LOG(4, 0, 0, " %s 0x%x 0x%x (%s)\n", lcp_type(type
), proto
, algo
,
290 (proto
== PPPCHAP
&& algo
== 5) ? "CHAP MD5" : "UNSUPPORTED");
293 LOG(4, 0, 0, " %s odd length %d\n", lcp_type(type
), length
);
295 case 4: // Quality-Protocol
297 uint32_t qp
= ntohl(*(uint32_t *)(o
+ 2));
298 LOG(4, 0, 0, " %s %x\n", lcp_type(type
), qp
);
301 case 5: // Magic-Number
304 uint32_t magicno
= ntohl(*(uint32_t *)(o
+ 2));
305 LOG(4, 0, 0, " %s %x\n", lcp_type(type
), magicno
);
308 LOG(4, 0, 0, " %s odd length %d\n", lcp_type(type
), length
);
310 case 7: // Protocol-Field-Compression
311 case 8: // Address-And-Control-Field-Compression
312 LOG(4, 0, 0, " %s\n", lcp_type(type
));
315 LOG(2, 0, 0, " Unknown PPP LCP Option type %d\n", type
);
323 // Process LCP messages
324 void processlcp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
326 uint8_t b
[MAXCONTROL
];
328 uint32_t magicno
= 0;
333 LOG_HEX(5, "LCP", p
, l
);
336 LOG(1, s
, t
, "Short LCP %d bytes\n", l
);
337 STAT(tunnel_rx_errors
);
341 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
343 LOG(1, s
, t
, "Length mismatch LCP %u/%u\n", hl
, l
);
344 STAT(tunnel_rx_errors
);
351 LOG(3, s
, t
, "LCP: Discarding ConfigAck\n");
352 session
[s
].flags
|= SF_LCP_ACKED
;
354 else if (*p
== ConfigReq
)
357 uint8_t *o
= (p
+ 4);
358 uint8_t *response
= 0;
360 LOG(3, s
, t
, "LCP: ConfigReq (%d bytes)...\n", l
);
361 if (config
->debug
> 3) dumplcp(p
, l
);
368 if (length
== 0 || type
== 0 || x
< length
) break;
371 case 1: // Maximum-Receive-Unit
372 session
[s
].mru
= ntohs(*(uint16_t *)(o
+ 2));
375 case 2: // Async-Control-Character-Map
376 if (!ntohl(*(uint32_t *)(o
+ 2))) // all bits zero is OK
379 if (response
&& *response
!= ConfigNak
) // rej already queued
382 LOG(2, s
, t
, " Remote requesting asyncmap. Rejecting.\n");
385 q
= response
= makeppp(b
, sizeof(b
), p
, 2, t
, s
, PPPLCP
);
391 if ((q
- b
+ 11) > sizeof(b
))
393 LOG(2, s
, t
, "LCP overflow for asyncmap ConfigNak.\n");
399 memset(q
, 0, 4); // asyncmap 0
401 *((uint16_t *) (response
+ 2)) = htons(q
- response
); // LCP header length
404 case 3: // Authentication-Protocol
406 int proto
= ntohs(*(uint16_t *)(o
+ 2));
407 char proto_name
[] = "0x0000";
412 if (config
->radius_authtypes
& AUTHPAP
)
415 strcpy(proto_name
, "PAP");
417 else if (proto
== PPPCHAP
)
419 if (config
->radius_authtypes
& AUTHCHAP
420 && *(o
+ 4) == 5) // MD5
423 strcpy(proto_name
, "CHAP");
426 sprintf(proto_name
, "%#4.4x", proto
);
428 if (response
&& *response
!= ConfigNak
) // rej already queued
431 LOG(2, s
, t
, " Remote requesting %s authentication. Rejecting.\n", proto_name
);
435 q
= response
= makeppp(b
, sizeof(b
), p
, 2, t
, s
, PPPLCP
);
441 a
= add_lcp_auth(q
, sizeof(b
) - (q
- b
), config
->radius_authprefer
);
444 LOG(2, s
, t
, "LCP overflow for %s ConfigNak.\n", proto_name
);
450 if (config
->radius_authtypes
!= config
->radius_authprefer
)
452 a
= add_lcp_auth(q
, sizeof(b
) - (q
- b
), config
->radius_authtypes
& ~config
->radius_authprefer
);
455 LOG(2, s
, t
, "LCP overflow for %s ConfigNak.\n", proto_name
);
462 *((uint16_t *) (response
+ 2)) = htons(q
- response
); // LCP header length
467 case 5: // Magic-Number
468 magicno
= ntohl(*(uint32_t *)(o
+ 2));
471 case 4: // Quality-Protocol
472 case 7: // Protocol-Field-Compression
473 case 8: // Address-And-Control-Field-Compression
476 default: // Reject any unknown options
477 LOG(2, s
, t
, " Rejecting PPP LCP Option type %d\n", type
);
478 if (!response
|| *response
!= ConfigRej
) // drop nak in favour of rej
480 q
= response
= makeppp(b
, sizeof(b
), p
, 2, t
, s
, PPPLCP
);
486 if ((q
- b
+ length
) > sizeof(b
))
488 LOG(2, s
, t
, "LCP overflow for ConfigRej (type=%d).\n", type
);
492 memcpy(q
, o
, length
);
494 *((uint16_t *) (response
+ 2)) = htons(q
- response
); // LCP header length
502 // Send back a ConfigAck
503 q
= response
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
508 LOG(3, s
, t
, "Sending %s\n", ppp_lcp_type(*response
));
509 tunnelsend(b
, l
+ (q
- b
), t
);
511 if (!(session
[s
].flags
& SF_LCP_ACKED
))
512 sendlcp(t
, s
, config
->radius_authprefer
);
514 else if (*p
== ConfigNak
)
516 LOG(1, s
, t
, "Remote end sent a ConfigNak. Ignoring\n");
517 if (config
->debug
> 3) dumplcp(p
, l
);
518 // FIXME: handle MRU, authentication type
521 else if (*p
== TerminateReq
)
523 LOG(3, s
, t
, "LCP: Received TerminateReq. Sending TerminateAck\n");
524 *p
= TerminateAck
; // close
525 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
527 tunnelsend(b
, l
+ (q
- b
), t
); // send it
528 sessionshutdown(s
, "Remote end closed connection.", 3, 0);
530 else if (*p
== TerminateAck
)
532 sessionshutdown(s
, "Connection closed.", 3, 0);
534 else if (*p
== ProtocolRej
)
536 if (*(uint16_t *) (p
+4) == htons(PPPIPV6CP
))
538 LOG(3, s
, t
, "IPv6 rejected\n");
539 session
[s
].flags
|= SF_IPV6_NACKED
;
543 LOG(1, s
, t
, "Unexpected LCP protocol reject 0x%X\n",
544 ntohs(*(uint16_t *) (p
+4)));
545 STAT(tunnel_rx_errors
);
548 else if (*p
== EchoReq
)
550 LOG(5, s
, t
, "LCP: Received EchoReq. Sending EchoReply\n");
551 *p
= EchoReply
; // reply
552 *(uint32_t *) (p
+ 4) = htonl(session
[s
].magic
); // our magic number
553 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
555 tunnelsend(b
, l
+ (q
- b
), t
); // send it
557 else if (*p
== EchoReply
)
559 // Ignore it, last_packet time is set earlier than this.
561 else if (*p
== IdentRequest
)
566 LOG(1, s
, t
, "Truncated Ident Packet (length=%d) to 1400 bytes\n", l
);
569 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
571 LOG_HEX(5, "LCPIdentRej", q
, l
+ 4);
572 tunnelsend(b
, 12 + 4 + l
, t
);
576 LOG(1, s
, t
, "Unexpected LCP code %d\n", *p
);
577 STAT(tunnel_rx_errors
);
582 // find a PPP option, returns point to option, or 0 if not found
583 static uint8_t *findppp(uint8_t *b
, uint8_t mtype
)
585 uint16_t l
= ntohs(*(uint16_t *) (b
+ 2));
592 if (l
< b
[1] || !b
[1])
602 // Process IPCP messages
603 void processipcp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
609 LOG_HEX(5, "IPCP", p
, l
);
612 LOG(1, s
, t
, "Short IPCP %d bytes\n", l
);
613 STAT(tunnel_rx_errors
);
617 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
619 LOG(1, s
, t
, "Length mismatch IPCP %u/%u\n", hl
, l
);
620 STAT(tunnel_rx_errors
);
627 // happy with our IPCP
628 uint16_t r
= sess_local
[s
].radius
;
629 if ((!r
|| radius
[r
].state
== RADIUSIPCP
) && !session
[s
].walled_garden
)
634 radiussend(r
, RADIUSSTART
); // send radius start, having got IPCP at last
636 session
[s
].flags
|= SF_IPCP_ACKED
;
638 LOG(3, s
, t
, "IPCP Acked, session is now active\n");
640 // clear LCP_ACKED/CCP_ACKED flag for possible fast renegotiaion for routers
641 session
[s
].flags
&= ~(SF_LCP_ACKED
|SF_CCP_ACKED
);
647 LOG(1, s
, t
, "Unexpected IPCP code %d\n", *p
);
648 STAT(tunnel_rx_errors
);
651 LOG(4, s
, t
, "IPCP ConfigReq received\n");
655 LOG(3, s
, t
, "Waiting on radius reply\n");
656 return; // have to wait on RADIUS reply
658 // form a config reply quoting the IP in the session
660 uint8_t b
[MAXCONTROL
];
665 while (q
< i
&& q
[1])
667 if (*q
!= 0x81 && *q
!= 0x83 && *q
!= 3)
676 if (!(q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPIPCP
)))
681 while (p
< i
&& p
[1])
683 if (*p
!= 0x81 && *p
!= 0x83 && *p
!= 3)
685 LOG(2, s
, t
, "IPCP reject %d\n", *p
);
686 memcpy(q
+ n
, p
, p
[1]);
691 *(uint16_t *) (q
+ 2) = htons(n
);
692 LOG(4, s
, t
, "Sending ConfigRej\n");
693 tunnelsend(b
, n
+ (q
- b
), t
); // send it
697 LOG(4, s
, t
, "Sending ConfigAck\n");
699 if ((i
= findppp(p
, 0x81))) // Primary DNS address
701 if (*(uint32_t *) (i
+ 2) != htonl(session
[s
].dns1
))
703 *(uint32_t *) (i
+ 2) = htonl(session
[s
].dns1
);
705 LOG(5, s
, t
, " DNS1 = %s\n",
706 fmtaddr(htonl(session
[s
].dns1
), 0));
709 if ((i
= findppp(p
, 0x83))) // Secondary DNS address (TBA, is it)
711 if (*(uint32_t *) (i
+ 2) != htonl(session
[s
].dns2
))
713 *(uint32_t *) (i
+ 2) = htonl(session
[s
].dns2
);
715 LOG(5, s
, t
, " DNS2 = %s\n",
716 fmtaddr(htonl(session
[s
].dns2
), 0));
719 i
= findppp(p
, 3); // IP address
722 LOG(1, s
, t
, "No IP in IPCP request\n");
723 STAT(tunnel_rx_errors
);
726 if (*(uint32_t *) (i
+ 2) != htonl(session
[s
].ip
))
728 *(uint32_t *) (i
+ 2) = htonl(session
[s
].ip
);
730 LOG(4, s
, t
, " No, a ConfigNak, client is requesting IP - sending %s\n",
731 fmtaddr(htonl(session
[s
].ip
), 0));
733 if (!(q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPIPCP
)))
736 tunnelsend(b
, l
+ (q
- b
), t
); // send it
741 // Process IPV6CP messages
742 void processipv6cp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
745 CSTAT(processipv6cp
);
747 LOG_HEX(5, "IPV6CP", p
, l
);
750 LOG(1, s
, t
, "Short IPV6CP %d bytes\n", l
);
751 STAT(tunnel_rx_errors
);
756 // happy with our IPV6CP
757 session
[s
].flags
|= SF_IPV6CP_ACKED
;
759 LOG(3, s
, t
, "IPV6CP Acked, IPv6 is now active\n");
760 // Add a routed block if configured.
761 if (session
[s
].ipv6prefixlen
)
763 route6set(s
, session
[s
].ipv6route
, session
[s
].ipv6prefixlen
, 1);
764 session
[s
].flags
|= SF_IPV6_ROUTED
;
767 // Send an initial RA (TODO: Should we send these regularly?)
768 send_ipv6_ra(t
, s
, NULL
);
773 LOG(1, s
, t
, "Unexpected IPV6CP code %d\n", *p
);
774 STAT(tunnel_rx_errors
);
778 LOG(4, s
, t
, "IPV6CP ConfigReq received\n");
779 if (ntohs(*(uint16_t *) (p
+ 2)) > l
)
781 LOG(1, s
, t
, "Length mismatch IPV6CP %d/%d\n", ntohs(*(uint16_t *) (p
+ 2)), l
);
782 STAT(tunnel_rx_errors
);
787 LOG(3, s
, t
, "Waiting on radius reply\n");
788 return; // have to wait on RADIUS reply
790 // form a config reply quoting the IP in the session
792 uint8_t b
[MAXCONTROL
];
796 l
= ntohs(*(uint16_t *) (p
+ 2)); // We must use length from IPV6CP len field
799 while (q
< i
&& q
[1])
810 if (!(q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPIPV6CP
)))
812 LOG(2, s
, t
, "Failed to send IPV6CP ConfigRej\n");
817 while (p
< i
&& p
[1])
821 LOG(2, s
, t
, "IPV6CP reject %d\n", *p
);
822 memcpy(q
+ n
, p
, p
[1]);
827 *(uint16_t *) (q
+ 2) = htons(n
);
828 LOG(4, s
, t
, "Sending ConfigRej\n");
829 tunnelsend(b
, n
+ (q
- b
), t
); // send it
833 LOG(4, s
, t
, "Sending ConfigAck\n");
835 i
= findppp(p
, 1); // IP address
836 if (!i
|| i
[1] != 10)
838 LOG(1, s
, t
, "No IP in IPV6CP request\n");
839 STAT(tunnel_rx_errors
);
842 if ((*(uint32_t *) (i
+ 2) != htonl(session
[s
].ip
)) ||
843 (*(uint32_t *) (i
+ 6) != 0))
845 *(uint32_t *) (i
+ 2) = htonl(session
[s
].ip
);
846 *(uint32_t *) (i
+ 6) = 0;
849 " No, a ConfigNak, client is "
850 "requesting IP - sending %s\n",
851 fmtaddr(htonl(session
[s
].ip
), 0));
853 if (!(q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPIPV6CP
)))
855 LOG(2, s
, t
, " Failed to send IPV6CP packet.\n");
858 tunnelsend(b
, l
+ (q
- b
), t
); // send it
863 // process IP packet received
865 // This MUST be called with at least 4 byte behind 'p'.
866 // (i.e. this routine writes to p[-4]).
867 void processipin(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
873 LOG_HEX(5, "IP", p
, l
);
875 ip
= ntohl(*(uint32_t *)(p
+ 12));
879 LOG(1, s
, t
, "IP packet too long %d\n", l
);
880 STAT(tunnel_rx_errors
);
884 // no spoof (do sessionbyip to handled statically routed subnets)
885 if (ip
!= session
[s
].ip
&& sessionbyip(htonl(ip
)) != s
)
887 LOG(5, s
, t
, "Dropping packet with spoofed IP %s\n", fmtaddr(htonl(ip
), 0));
891 // run access-list if any
892 if (session
[s
].filter_in
&& !ip_filter(p
, l
, session
[s
].filter_in
- 1))
895 // Add on the tun header
897 *(uint32_t *) p
= htonl(PKTIP
);
900 // Are we throttled and a slave?
901 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) {
902 // Pass it to the master for handling.
903 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
907 // Are we throttled and a master??
908 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) {
909 // Actually handle the throttled packets.
910 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
915 if (tun_write(p
, l
) < 0)
918 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
919 l
, strerror(errno
), tunfd
, p
);
924 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
926 // Snooping this session
927 snoop_send_packet(p
+ 4, l
- 4, session
[s
].snoop_ip
, session
[s
].snoop_port
);
930 session
[s
].cin
+= l
- 4;
931 session
[s
].total_cin
+= l
- 4;
932 sess_local
[s
].cin
+= l
- 4;
937 STAT(tun_tx_packets
);
938 INC_STAT(tun_tx_bytes
, l
- 4);
941 // process IPv6 packet received
943 // This MUST be called with at least 4 byte behind 'p'.
944 // (i.e. this routine writes to p[-4]).
945 void processipv6in(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
950 CSTAT(processipv6in
);
952 LOG_HEX(5, "IPv6", p
, l
);
954 ip
= *(struct in6_addr
*) (p
+ 8);
955 ipv4
= ntohl(*(uint32_t *)(p
+ 16));
959 LOG(1, s
, t
, "IP packet too long %d\n", l
);
960 STAT(tunnel_rx_errors
);
965 if (ipv4
!= session
[s
].ip
&& memcmp(&config
->ipv6_prefix
, &ip
, 8) && sessionbyipv6(ip
) != s
)
967 char str
[INET6_ADDRSTRLEN
];
968 LOG(5, s
, t
, "Dropping packet with spoofed IP %s\n",
969 inet_ntop(AF_INET6
, &ip
, str
, INET6_ADDRSTRLEN
));
973 // Check if it's a Router Solicition message.
974 if (*(p
+ 6) == 58 && *(p
+ 7) == 255 && *(p
+ 24) == 0xFF && *(p
+ 25) == 2 &&
975 *(uint32_t *)(p
+ 26) == 0 && *(uint32_t *)(p
+ 30) == 0 &&
976 *(uint32_t *)(p
+ 34) == 0 &&
977 *(p
+ 38) == 0 && *(p
+ 39) == 2 && *(p
+ 40) == 133) {
978 LOG(3, s
, t
, "Got IPv6 RS\n");
979 send_ipv6_ra(t
, s
, &ip
);
983 // Add on the tun header
985 *(uint32_t *) p
= htonl(PKTIPV6
);
988 // Are we throttled and a slave?
989 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) {
990 // Pass it to the master for handling.
991 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
995 // Are we throttled and a master??
996 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) {
997 // Actually handle the throttled packets.
998 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
1003 if (tun_write(p
, l
) < 0)
1005 STAT(tun_tx_errors
);
1006 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1007 l
, strerror(errno
), tunfd
, p
);
1012 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1014 // Snooping this session
1015 snoop_send_packet(p
+ 4, l
- 4, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1018 session
[s
].cin
+= l
- 4;
1019 session
[s
].total_cin
+= l
- 4;
1020 sess_local
[s
].cin
+= l
- 4;
1025 STAT(tun_tx_packets
);
1026 INC_STAT(tun_tx_bytes
, l
- 4);
1030 // Helper routine for the TBF filters.
1031 // Used to send queued data in from the user.
1033 void send_ipin(sessionidt s
, uint8_t *buf
, int len
)
1035 LOG_HEX(5, "IP in throttled", buf
, len
);
1037 if (write(tunfd
, buf
, len
) < 0)
1039 STAT(tun_tx_errors
);
1040 LOG(0, 0, 0, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1041 len
, strerror(errno
), tunfd
, buf
);
1046 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1048 // Snooping this session
1049 snoop_send_packet(buf
+ 4, len
- 4, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1052 // Increment packet counters
1053 session
[s
].cin
+= len
- 4;
1054 session
[s
].total_cin
+= len
- 4;
1055 sess_local
[s
].cin
+= len
- 4;
1060 STAT(tun_tx_packets
);
1061 INC_STAT(tun_tx_bytes
, len
- 4);
1065 // Process CCP messages
1066 void processccp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
1068 uint8_t b
[MAXCONTROL
];
1073 LOG_HEX(5, "CCP", p
, l
);
1074 switch (l
> 1 ? *p
: 0)
1077 session
[s
].flags
|= SF_CCP_ACKED
;
1081 if (l
< 6) // accept no compression
1087 // compression requested--reject
1090 // send CCP request for no compression for our end if not negotiated
1091 if (!(session
[s
].flags
& SF_CCP_ACKED
))
1102 LOG(1, s
, t
, "Unexpected CCP request code %d\n", *p
);
1104 LOG(1, s
, t
, "Short CCP packet\n");
1106 STAT(tunnel_rx_errors
);
1110 if (!(q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPCCP
)))
1113 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1116 // send a CHAP challenge
1117 void sendchap(tunnelidt t
, sessionidt s
)
1119 uint8_t b
[MAXCONTROL
];
1120 uint16_t r
= sess_local
[s
].radius
;
1127 LOG(1, s
, t
, "No RADIUS to send challenge\n");
1128 STAT(tunnel_tx_errors
);
1132 LOG(1, s
, t
, "Send CHAP challenge\n");
1134 radius
[r
].chap
= 1; // CHAP not PAP
1136 if (radius
[r
].state
!= RADIUSCHAP
)
1139 radius
[r
].state
= RADIUSCHAP
;
1140 radius
[r
].retry
= backoff(radius
[r
].try++);
1141 if (radius
[r
].try > 5)
1143 sessionshutdown(s
, "CHAP timeout.", 3, 0);
1144 STAT(tunnel_tx_errors
);
1147 q
= makeppp(b
, sizeof(b
), 0, 0, t
, s
, PPPCHAP
);
1150 *q
= 1; // challenge
1151 q
[1] = radius
[r
].id
; // ID
1152 q
[4] = 16; // value size (size of challenge)
1153 memcpy(q
+ 5, radius
[r
].auth
, 16); // challenge
1154 strcpy(q
+ 21, hostname
); // our name
1155 *(uint16_t *) (q
+ 2) = htons(strlen(hostname
) + 21); // length
1156 tunnelsend(b
, strlen(hostname
) + 21 + (q
- b
), t
); // send it
1159 // fill in a L2TP message with a PPP frame,
1160 // copies existing PPP message and changes magic number if seen
1161 // returns start of PPP frame
1162 uint8_t *makeppp(uint8_t *b
, int size
, uint8_t *p
, int l
, tunnelidt t
, sessionidt s
, uint16_t mtype
)
1164 if (size
< 12) // Need more space than this!!
1166 static int backtrace_count
= 0;
1167 LOG(0, s
, t
, "makeppp buffer too small for L2TP header (size=%d)\n", size
);
1168 log_backtrace(backtrace_count
, 5)
1172 *(uint16_t *) (b
+ 0) = htons(0x0002); // L2TP with no options
1173 *(uint16_t *) (b
+ 2) = htons(tunnel
[t
].far
); // tunnel
1174 *(uint16_t *) (b
+ 4) = htons(session
[s
].far
); // session
1176 if (mtype
== PPPLCP
|| !(session
[s
].l2tp_flags
& SESSIONACFC
))
1178 *(uint16_t *) b
= htons(0xFF03); // HDLC header
1181 if (mtype
< 0x100 && session
[s
].l2tp_flags
& SESSIONPFC
)
1185 *(uint16_t *) b
= htons(mtype
);
1191 static int backtrace_count
= 0;
1192 LOG(2, s
, t
, "makeppp would overflow buffer (size=%d, header+payload=%d)\n", size
, l
+ 12);
1193 log_backtrace(backtrace_count
, 5)
1203 static uint8_t *add_lcp_auth(uint8_t *b
, int size
, int authtype
)
1205 if ((authtype
== AUTHCHAP
&& size
< 5) || size
< 4)
1208 *b
++ = 3; // Authentication-Protocol
1209 if (authtype
== AUTHCHAP
)
1212 *(uint16_t *) b
= htons(PPPCHAP
); b
+= 2;
1215 else if (authtype
== AUTHPAP
)
1218 *(uint16_t *) b
= htons(PPPPAP
); b
+= 2;
1222 LOG(0, 0, 0, "add_lcp_auth called with unsupported auth type %d\n", authtype
);
1228 // Send initial LCP ConfigReq for MRU, authentication type and magic no
1229 void sendlcp(tunnelidt t
, sessionidt s
, int authtype
)
1231 char b
[500], *q
, *l
;
1233 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, t
, s
, PPPLCP
)))
1236 LOG(4, s
, t
, "Sending LCP ConfigReq for %s\n",
1237 config
->radius_authprefer
== AUTHCHAP
? "CHAP" : "PAP");
1239 if (!session
[s
].mru
)
1240 session
[s
].mru
= DEFAULT_MRU
;
1244 *l
++ = (time_now
% 255) + 1; // ID
1246 *l
++ = 1; *l
++ = 4; // Maximum-Receive-Unit (length 4)
1247 *(uint16_t *) l
= htons(session
[s
].mru
); l
+= 2;
1249 l
= add_lcp_auth(l
, sizeof(b
) - (l
- b
), authtype
);
1251 *l
++ = 5; *l
++ = 6; // Magic-Number (length 6)
1252 *(uint32_t *) l
= htonl(session
[s
].magic
);
1255 *(uint16_t *)(q
+ 2) = htons(l
- q
); // Length
1257 LOG_HEX(5, "PPPLCP", q
, l
- q
);
1258 tunnelsend(b
, (l
- b
), t
);
1261 // Send CCP request for no compression
1262 static void initccp(tunnelidt t
, sessionidt s
)
1266 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, t
, s
, PPPCCP
)))
1269 LOG(4, s
, t
, "Sending CCP ConfigReq for no compression\n");
1271 *(uint8_t *)(q
+ 1) = (time_now
% 255) + 1; // ID
1272 *(uint16_t *)(q
+ 2) = htons(4); // Length
1274 LOG_HEX(5, "PPPCCP", q
, 4);
1275 tunnelsend(b
, (q
- b
) + 4 , t
);