20833f11f3b285d263e4e88f777786c4f5914a25
3 char const *cvs_id_ppp
= "$Id: ppp.c,v 1.55 2005/05/10 09:35:27 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
);
153 sessionshutdown(s
, "Unexpected CHAP message.", 3, 0);
159 LOG(1, s
, t
, "Short CHAP %u bytes\n", l
);
160 STAT(tunnel_rx_errors
);
161 sessionshutdown(s
, "Short CHAP packet.", 3, 0);
165 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
167 LOG(1, s
, t
, "Length mismatch CHAP %u/%u\n", hl
, l
);
168 STAT(tunnel_rx_errors
);
169 sessionshutdown(s
, "CHAP length mismatch.", 3, 0);
176 LOG(1, s
, t
, "Unexpected CHAP response code %d\n", *p
);
177 STAT(tunnel_rx_errors
);
178 sessionshutdown(s
, "CHAP length mismatch.", 3, 0);
181 if (p
[1] != radius
[r
].id
)
183 LOG(1, s
, t
, "Wrong CHAP response ID %d (should be %d) (%d)\n", p
[1], radius
[r
].id
, r
);
184 STAT(tunnel_rx_errors
);
185 sessionshutdown(s
, "Unexpected CHAP response ID.", 3, 0);
189 if (l
< 5 || p
[4] != 16)
191 LOG(1, s
, t
, "Bad CHAP response length %d\n", l
< 5 ? -1 : p
[4]);
192 STAT(tunnel_rx_errors
);
193 sessionshutdown(s
, "Bad CHAP response length.", 3, 0);
199 if (l
< 16 || l
- 16 >= sizeof(session
[s
].user
))
201 LOG(1, s
, t
, "CHAP user too long %d\n", l
- 16);
202 STAT(tunnel_rx_errors
);
203 sessionshutdown(s
, "CHAP username too long.", 3, 0);
207 // Run PRE_AUTH plugins
209 struct param_pre_auth packet
= { &tunnel
[t
], &session
[s
], NULL
, NULL
, PPPCHAP
, 1 };
211 packet
.password
= calloc(17, 1);
212 memcpy(packet
.password
, p
, 16);
217 packet
.username
= calloc(l
+ 1, 1);
218 memcpy(packet
.username
, p
, l
);
220 run_plugins(PLUGIN_PRE_AUTH
, &packet
);
221 if (!packet
.continue_auth
)
223 LOG(3, s
, t
, "A plugin rejected PRE_AUTH\n");
224 if (packet
.username
) free(packet
.username
);
225 if (packet
.password
) free(packet
.password
);
229 strncpy(session
[s
].user
, packet
.username
, sizeof(session
[s
].user
) - 1);
230 memcpy(radius
[r
].pass
, packet
.password
, 16);
232 free(packet
.username
);
233 free(packet
.password
);
237 LOG(3, s
, t
, "CHAP login %s\n", session
[s
].user
);
238 radiussend(r
, RADIUSAUTH
);
241 static void dumplcp(uint8_t *p
, int l
)
244 uint8_t *o
= (p
+ 4);
246 LOG_HEX(5, "PPP LCP Packet", p
, l
);
247 LOG(4, 0, 0, "PPP LCP Packet type %d (%s len %d)\n", *p
, ppp_lcp_type((int)*p
), ntohs( ((uint16_t *) p
)[1]) );
248 LOG(4, 0, 0, "Length: %d\n", l
);
249 if (*p
!= ConfigReq
&& *p
!= ConfigRej
&& *p
!= ConfigAck
)
258 LOG(4, 0, 0, " Option length is %d...\n", length
);
263 LOG(4, 0, 0, " Option type is 0...\n");
270 case 1: // Maximum-Receive-Unit
272 LOG(4, 0, 0, " %s %d\n", lcp_type(type
), ntohs(*(uint16_t *)(o
+ 2)));
274 LOG(4, 0, 0, " %s odd length %d\n", lcp_type(type
), length
);
276 case 2: // Async-Control-Character-Map
279 uint32_t asyncmap
= ntohl(*(uint32_t *)(o
+ 2));
280 LOG(4, 0, 0, " %s %x\n", lcp_type(type
), asyncmap
);
283 LOG(4, 0, 0, " %s odd length %d\n", lcp_type(type
), length
);
285 case 3: // Authentication-Protocol
288 int proto
= ntohs(*(uint16_t *)(o
+ 2));
289 LOG(4, 0, 0, " %s 0x%x (%s)\n", lcp_type(type
), proto
,
290 proto
== PPPPAP
? "PAP" : "UNSUPPORTED");
292 else if (length
== 5)
294 int proto
= ntohs(*(uint16_t *)(o
+ 2));
295 int algo
= *(uint8_t *)(o
+ 4);
296 LOG(4, 0, 0, " %s 0x%x 0x%x (%s)\n", lcp_type(type
), proto
, algo
,
297 (proto
== PPPCHAP
&& algo
== 5) ? "CHAP MD5" : "UNSUPPORTED");
300 LOG(4, 0, 0, " %s odd length %d\n", lcp_type(type
), length
);
302 case 4: // Quality-Protocol
304 uint32_t qp
= ntohl(*(uint32_t *)(o
+ 2));
305 LOG(4, 0, 0, " %s %x\n", lcp_type(type
), qp
);
308 case 5: // Magic-Number
311 uint32_t magicno
= ntohl(*(uint32_t *)(o
+ 2));
312 LOG(4, 0, 0, " %s %x\n", lcp_type(type
), magicno
);
315 LOG(4, 0, 0, " %s odd length %d\n", lcp_type(type
), length
);
317 case 7: // Protocol-Field-Compression
318 case 8: // Address-And-Control-Field-Compression
319 LOG(4, 0, 0, " %s\n", lcp_type(type
));
322 LOG(2, 0, 0, " Unknown PPP LCP Option type %d\n", type
);
330 // Process LCP messages
331 void processlcp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
333 uint8_t b
[MAXCONTROL
];
335 uint32_t magicno
= 0;
340 LOG_HEX(5, "LCP", p
, l
);
343 LOG(1, s
, t
, "Short LCP %d bytes\n", l
);
344 STAT(tunnel_rx_errors
);
348 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
350 LOG(1, s
, t
, "Length mismatch LCP %u/%u\n", hl
, l
);
351 STAT(tunnel_rx_errors
);
358 LOG(3, s
, t
, "LCP: Discarding ConfigAck\n");
359 session
[s
].flags
|= SF_LCP_ACKED
;
361 else if (*p
== ConfigReq
)
364 uint8_t *o
= (p
+ 4);
365 uint8_t *response
= 0;
367 LOG(3, s
, t
, "LCP: ConfigReq (%d bytes)...\n", l
);
368 if (config
->debug
> 3) dumplcp(p
, l
);
375 if (length
== 0 || type
== 0 || x
< length
) break;
378 case 1: // Maximum-Receive-Unit
379 session
[s
].mru
= ntohs(*(uint16_t *)(o
+ 2));
382 case 2: // Async-Control-Character-Map
383 if (!ntohl(*(uint32_t *)(o
+ 2))) // all bits zero is OK
386 if (response
&& *response
!= ConfigNak
) // rej already queued
389 LOG(2, s
, t
, " Remote requesting asyncmap. Rejecting.\n");
392 q
= response
= makeppp(b
, sizeof(b
), p
, 2, t
, s
, PPPLCP
);
398 if ((q
- b
+ 11) > sizeof(b
))
400 LOG(2, s
, t
, "LCP overflow for asyncmap ConfigNak.\n");
406 memset(q
, 0, 4); // asyncmap 0
408 *((uint16_t *) (response
+ 2)) = htons(l
= q
- response
); // LCP header length
411 case 3: // Authentication-Protocol
413 int proto
= ntohs(*(uint16_t *)(o
+ 2));
414 char proto_name
[] = "0x0000";
419 if (config
->radius_authtypes
& AUTHPAP
)
422 strcpy(proto_name
, "PAP");
424 else if (proto
== PPPCHAP
)
426 if (config
->radius_authtypes
& AUTHCHAP
427 && *(o
+ 4) == 5) // MD5
430 strcpy(proto_name
, "CHAP");
433 sprintf(proto_name
, "%#4.4x", proto
);
435 if (response
&& *response
!= ConfigNak
) // rej already queued
438 LOG(2, s
, t
, " Remote requesting %s authentication. Rejecting.\n", proto_name
);
442 q
= response
= makeppp(b
, sizeof(b
), p
, 2, t
, s
, PPPLCP
);
448 a
= add_lcp_auth(q
, sizeof(b
) - (q
- b
), config
->radius_authprefer
);
451 LOG(2, s
, t
, "LCP overflow for %s ConfigNak.\n", proto_name
);
457 if (config
->radius_authtypes
!= config
->radius_authprefer
)
459 a
= add_lcp_auth(q
, sizeof(b
) - (q
- b
), config
->radius_authtypes
& ~config
->radius_authprefer
);
462 LOG(2, s
, t
, "LCP overflow for %s ConfigNak.\n", proto_name
);
469 *((uint16_t *) (response
+ 2)) = htons(l
= q
- response
); // LCP header length
474 case 5: // Magic-Number
475 magicno
= ntohl(*(uint32_t *)(o
+ 2));
478 case 4: // Quality-Protocol
479 case 7: // Protocol-Field-Compression
480 case 8: // Address-And-Control-Field-Compression
483 default: // Reject any unknown options
484 LOG(2, s
, t
, " Rejecting PPP LCP Option type %d\n", type
);
485 if (!response
|| *response
!= ConfigRej
) // drop nak in favour of rej
487 q
= response
= makeppp(b
, sizeof(b
), p
, 2, t
, s
, PPPLCP
);
493 if ((q
- b
+ length
) > sizeof(b
))
495 LOG(2, s
, t
, "LCP overflow for ConfigRej (type=%d).\n", type
);
499 memcpy(q
, o
, length
);
501 *((uint16_t *) (response
+ 2)) = htons(l
= q
- response
); // LCP header length
509 // Send back a ConfigAck
510 q
= response
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
515 LOG(3, s
, t
, "Sending %s\n", ppp_lcp_type(*response
));
516 tunnelsend(b
, l
+ response
- b
, t
);
518 if (!(session
[s
].flags
& SF_LCP_ACKED
))
519 sendlcp(t
, s
, config
->radius_authprefer
);
521 else if (*p
== ConfigNak
)
524 uint8_t *o
= (p
+ 4);
527 LOG(3, s
, t
, "LCP: ConfigNak (%d bytes)...\n", l
);
528 if (config
->debug
> 3) dumplcp(p
, l
);
535 if (length
== 0 || type
== 0 || x
< length
) break;
538 case 1: // Maximum-Receive-Unit
539 session
[s
].mru
= ntohs(*(uint16_t *)(o
+ 2));
540 LOG(3, s
, t
, " Remote requested MRU of %u\n", session
[s
].mru
);
543 case 3: // Authentication-Protocol
548 int proto
= ntohs(*(uint16_t *)(o
+ 2));
551 authtype
= config
->radius_authtypes
& AUTHPAP
;
552 LOG(3, s
, t
, " Remote requested PAP authentication...%sing\n",
553 authtype
? "accept" : "reject");
555 else if (proto
== PPPCHAP
&& *(o
+ 4) == 5)
557 authtype
= config
->radius_authtypes
& AUTHCHAP
;
558 LOG(3, s
, t
, " Remote requested CHAP authentication...%sing\n",
559 authtype
? "accept" : "reject");
563 LOG(3, s
, t
, " Rejecting unsupported authentication %#4x\n",
570 sessionshutdown(s
, "Unsupported authentication.", 3, 0);
577 LOG(2, s
, t
, " Remote NAKed LCP type %u?\n", type
);
583 authtype
= config
->radius_authprefer
;
585 sendlcp(t
, s
, authtype
);
587 else if (*p
== TerminateReq
)
589 LOG(3, s
, t
, "LCP: Received TerminateReq. Sending TerminateAck\n");
590 *p
= TerminateAck
; // close
591 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
593 tunnelsend(b
, l
+ (q
- b
), t
); // send it
594 sessionshutdown(s
, "Remote end closed connection.", 3, 0);
596 else if (*p
== TerminateAck
)
598 sessionshutdown(s
, "Connection closed.", 3, 0);
600 else if (*p
== ProtocolRej
)
602 if (*(uint16_t *) (p
+4) == htons(PPPIPV6CP
))
604 LOG(3, s
, t
, "IPv6 rejected\n");
605 session
[s
].flags
|= SF_IPV6_NACKED
;
609 LOG(1, s
, t
, "Unexpected LCP protocol reject 0x%X\n",
610 ntohs(*(uint16_t *) (p
+4)));
611 STAT(tunnel_rx_errors
);
614 else if (*p
== EchoReq
)
616 LOG(5, s
, t
, "LCP: Received EchoReq. Sending EchoReply\n");
617 *p
= EchoReply
; // reply
618 *(uint32_t *) (p
+ 4) = htonl(session
[s
].magic
); // our magic number
619 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
621 tunnelsend(b
, l
+ (q
- b
), t
); // send it
623 else if (*p
== EchoReply
)
625 // Ignore it, last_packet time is set earlier than this.
627 else if (*p
== IdentRequest
)
632 LOG(1, s
, t
, "Truncated Ident Packet (length=%d) to 1400 bytes\n", l
);
635 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
637 LOG_HEX(5, "LCPIdentRej", q
, l
+ 4);
638 tunnelsend(b
, 12 + 4 + l
, t
);
642 LOG(1, s
, t
, "Unexpected LCP code %d\n", *p
);
643 STAT(tunnel_rx_errors
);
647 // find a PPP option, returns point to option, or 0 if not found
648 static uint8_t *findppp(uint8_t *b
, uint8_t mtype
)
650 uint16_t l
= ntohs(*(uint16_t *) (b
+ 2));
657 if (l
< b
[1] || !b
[1])
667 // Process IPCP messages
668 void processipcp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
674 LOG_HEX(5, "IPCP", p
, l
);
677 LOG(1, s
, t
, "Short IPCP %d bytes\n", l
);
678 STAT(tunnel_rx_errors
);
682 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
684 LOG(1, s
, t
, "Length mismatch IPCP %u/%u\n", hl
, l
);
685 STAT(tunnel_rx_errors
);
692 // happy with our IPCP
693 uint16_t r
= sess_local
[s
].radius
;
694 if ((!r
|| radius
[r
].state
== RADIUSIPCP
) && !session
[s
].walled_garden
)
699 radiussend(r
, RADIUSSTART
); // send radius start, having got IPCP at last
701 session
[s
].flags
|= SF_IPCP_ACKED
;
703 LOG(3, s
, t
, "IPCP Acked, session is now active\n");
705 // clear LCP_ACKED/CCP_ACKED flag for possible fast renegotiaion for routers
706 session
[s
].flags
&= ~(SF_LCP_ACKED
|SF_CCP_ACKED
);
712 LOG(1, s
, t
, "Unexpected IPCP code %d\n", *p
);
713 STAT(tunnel_rx_errors
);
716 LOG(4, s
, t
, "IPCP ConfigReq received\n");
720 LOG(3, s
, t
, "Waiting on radius reply\n");
721 return; // have to wait on RADIUS reply
723 // form a config reply quoting the IP in the session
725 uint8_t b
[MAXCONTROL
];
730 while (q
< i
&& q
[1])
732 if (*q
!= 0x81 && *q
!= 0x83 && *q
!= 3)
741 if (!(q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPIPCP
)))
746 while (p
< i
&& p
[1])
748 if (*p
!= 0x81 && *p
!= 0x83 && *p
!= 3)
750 LOG(2, s
, t
, "IPCP reject %d\n", *p
);
751 memcpy(q
+ n
, p
, p
[1]);
756 *(uint16_t *) (q
+ 2) = htons(n
);
757 LOG(4, s
, t
, "Sending ConfigRej\n");
758 tunnelsend(b
, n
+ (q
- b
), t
); // send it
762 LOG(4, s
, t
, "Sending ConfigAck\n");
764 if ((i
= findppp(p
, 0x81))) // Primary DNS address
766 if (*(uint32_t *) (i
+ 2) != htonl(session
[s
].dns1
))
768 *(uint32_t *) (i
+ 2) = htonl(session
[s
].dns1
);
770 LOG(5, s
, t
, " DNS1 = %s\n",
771 fmtaddr(htonl(session
[s
].dns1
), 0));
774 if ((i
= findppp(p
, 0x83))) // Secondary DNS address (TBA, is it)
776 if (*(uint32_t *) (i
+ 2) != htonl(session
[s
].dns2
))
778 *(uint32_t *) (i
+ 2) = htonl(session
[s
].dns2
);
780 LOG(5, s
, t
, " DNS2 = %s\n",
781 fmtaddr(htonl(session
[s
].dns2
), 0));
784 i
= findppp(p
, 3); // IP address
787 LOG(1, s
, t
, "No IP in IPCP request\n");
788 STAT(tunnel_rx_errors
);
791 if (*(uint32_t *) (i
+ 2) != htonl(session
[s
].ip
))
793 *(uint32_t *) (i
+ 2) = htonl(session
[s
].ip
);
795 LOG(4, s
, t
, " No, a ConfigNak, client is requesting IP - sending %s\n",
796 fmtaddr(htonl(session
[s
].ip
), 0));
798 if (!(q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPIPCP
)))
801 tunnelsend(b
, l
+ (q
- b
), t
); // send it
806 // Process IPV6CP messages
807 void processipv6cp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
810 CSTAT(processipv6cp
);
812 LOG_HEX(5, "IPV6CP", p
, l
);
815 LOG(1, s
, t
, "Short IPV6CP %d bytes\n", l
);
816 STAT(tunnel_rx_errors
);
821 // happy with our IPV6CP
822 session
[s
].flags
|= SF_IPV6CP_ACKED
;
824 LOG(3, s
, t
, "IPV6CP Acked, IPv6 is now active\n");
825 // Add a routed block if configured.
826 if (session
[s
].ipv6prefixlen
)
828 route6set(s
, session
[s
].ipv6route
, session
[s
].ipv6prefixlen
, 1);
829 session
[s
].flags
|= SF_IPV6_ROUTED
;
832 // Send an initial RA (TODO: Should we send these regularly?)
833 send_ipv6_ra(t
, s
, NULL
);
838 LOG(1, s
, t
, "Unexpected IPV6CP code %d\n", *p
);
839 STAT(tunnel_rx_errors
);
843 LOG(4, s
, t
, "IPV6CP ConfigReq received\n");
844 if (ntohs(*(uint16_t *) (p
+ 2)) > l
)
846 LOG(1, s
, t
, "Length mismatch IPV6CP %d/%d\n", ntohs(*(uint16_t *) (p
+ 2)), l
);
847 STAT(tunnel_rx_errors
);
852 LOG(3, s
, t
, "Waiting on radius reply\n");
853 return; // have to wait on RADIUS reply
855 // form a config reply quoting the IP in the session
857 uint8_t b
[MAXCONTROL
];
861 l
= ntohs(*(uint16_t *) (p
+ 2)); // We must use length from IPV6CP len field
864 while (q
< i
&& q
[1])
875 if (!(q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPIPV6CP
)))
877 LOG(2, s
, t
, "Failed to send IPV6CP ConfigRej\n");
882 while (p
< i
&& p
[1])
886 LOG(2, s
, t
, "IPV6CP reject %d\n", *p
);
887 memcpy(q
+ n
, p
, p
[1]);
892 *(uint16_t *) (q
+ 2) = htons(n
);
893 LOG(4, s
, t
, "Sending ConfigRej\n");
894 tunnelsend(b
, n
+ (q
- b
), t
); // send it
898 LOG(4, s
, t
, "Sending ConfigAck\n");
900 i
= findppp(p
, 1); // IP address
901 if (!i
|| i
[1] != 10)
903 LOG(1, s
, t
, "No IP in IPV6CP request\n");
904 STAT(tunnel_rx_errors
);
907 if ((*(uint32_t *) (i
+ 2) != htonl(session
[s
].ip
)) ||
908 (*(uint32_t *) (i
+ 6) != 0))
910 *(uint32_t *) (i
+ 2) = htonl(session
[s
].ip
);
911 *(uint32_t *) (i
+ 6) = 0;
914 " No, a ConfigNak, client is "
915 "requesting IP - sending %s\n",
916 fmtaddr(htonl(session
[s
].ip
), 0));
918 if (!(q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPIPV6CP
)))
920 LOG(2, s
, t
, " Failed to send IPV6CP packet.\n");
923 tunnelsend(b
, l
+ (q
- b
), t
); // send it
928 // process IP packet received
930 // This MUST be called with at least 4 byte behind 'p'.
931 // (i.e. this routine writes to p[-4]).
932 void processipin(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
938 LOG_HEX(5, "IP", p
, l
);
940 ip
= ntohl(*(uint32_t *)(p
+ 12));
944 LOG(1, s
, t
, "IP packet too long %d\n", l
);
945 STAT(tunnel_rx_errors
);
949 // no spoof (do sessionbyip to handled statically routed subnets)
950 if (ip
!= session
[s
].ip
&& sessionbyip(htonl(ip
)) != s
)
952 LOG(5, s
, t
, "Dropping packet with spoofed IP %s\n", fmtaddr(htonl(ip
), 0));
956 // run access-list if any
957 if (session
[s
].filter_in
&& !ip_filter(p
, l
, session
[s
].filter_in
- 1))
960 // Add on the tun header
962 *(uint32_t *) p
= htonl(PKTIP
);
965 // Are we throttled and a slave?
966 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) {
967 // Pass it to the master for handling.
968 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
972 // Are we throttled and a master??
973 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) {
974 // Actually handle the throttled packets.
975 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
980 if (tun_write(p
, l
) < 0)
983 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
984 l
, strerror(errno
), tunfd
, p
);
989 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
991 // Snooping this session
992 snoop_send_packet(p
+ 4, l
- 4, session
[s
].snoop_ip
, session
[s
].snoop_port
);
995 session
[s
].cin
+= l
- 4;
996 session
[s
].total_cin
+= l
- 4;
997 sess_local
[s
].cin
+= l
- 4;
1002 STAT(tun_tx_packets
);
1003 INC_STAT(tun_tx_bytes
, l
- 4);
1006 // process IPv6 packet received
1008 // This MUST be called with at least 4 byte behind 'p'.
1009 // (i.e. this routine writes to p[-4]).
1010 void processipv6in(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
1015 CSTAT(processipv6in
);
1017 LOG_HEX(5, "IPv6", p
, l
);
1019 ip
= *(struct in6_addr
*) (p
+ 8);
1020 ipv4
= ntohl(*(uint32_t *)(p
+ 16));
1024 LOG(1, s
, t
, "IP packet too long %d\n", l
);
1025 STAT(tunnel_rx_errors
);
1030 if (ipv4
!= session
[s
].ip
&& memcmp(&config
->ipv6_prefix
, &ip
, 8) && sessionbyipv6(ip
) != s
)
1032 char str
[INET6_ADDRSTRLEN
];
1033 LOG(5, s
, t
, "Dropping packet with spoofed IP %s\n",
1034 inet_ntop(AF_INET6
, &ip
, str
, INET6_ADDRSTRLEN
));
1038 // Check if it's a Router Solicition message.
1039 if (*(p
+ 6) == 58 && *(p
+ 7) == 255 && *(p
+ 24) == 0xFF && *(p
+ 25) == 2 &&
1040 *(uint32_t *)(p
+ 26) == 0 && *(uint32_t *)(p
+ 30) == 0 &&
1041 *(uint32_t *)(p
+ 34) == 0 &&
1042 *(p
+ 38) == 0 && *(p
+ 39) == 2 && *(p
+ 40) == 133) {
1043 LOG(3, s
, t
, "Got IPv6 RS\n");
1044 send_ipv6_ra(t
, s
, &ip
);
1048 // Add on the tun header
1050 *(uint32_t *) p
= htonl(PKTIPV6
);
1053 // Are we throttled and a slave?
1054 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) {
1055 // Pass it to the master for handling.
1056 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
1060 // Are we throttled and a master??
1061 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) {
1062 // Actually handle the throttled packets.
1063 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
1068 if (tun_write(p
, l
) < 0)
1070 STAT(tun_tx_errors
);
1071 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1072 l
, strerror(errno
), tunfd
, p
);
1077 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1079 // Snooping this session
1080 snoop_send_packet(p
+ 4, l
- 4, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1083 session
[s
].cin
+= l
- 4;
1084 session
[s
].total_cin
+= l
- 4;
1085 sess_local
[s
].cin
+= l
- 4;
1090 STAT(tun_tx_packets
);
1091 INC_STAT(tun_tx_bytes
, l
- 4);
1095 // Helper routine for the TBF filters.
1096 // Used to send queued data in from the user.
1098 void send_ipin(sessionidt s
, uint8_t *buf
, int len
)
1100 LOG_HEX(5, "IP in throttled", buf
, len
);
1102 if (write(tunfd
, buf
, len
) < 0)
1104 STAT(tun_tx_errors
);
1105 LOG(0, 0, 0, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1106 len
, strerror(errno
), tunfd
, buf
);
1111 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1113 // Snooping this session
1114 snoop_send_packet(buf
+ 4, len
- 4, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1117 // Increment packet counters
1118 session
[s
].cin
+= len
- 4;
1119 session
[s
].total_cin
+= len
- 4;
1120 sess_local
[s
].cin
+= len
- 4;
1125 STAT(tun_tx_packets
);
1126 INC_STAT(tun_tx_bytes
, len
- 4);
1130 // Process CCP messages
1131 void processccp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
1133 uint8_t b
[MAXCONTROL
];
1138 LOG_HEX(5, "CCP", p
, l
);
1139 switch (l
> 1 ? *p
: 0)
1142 session
[s
].flags
|= SF_CCP_ACKED
;
1146 if (l
< 6) // accept no compression
1152 // compression requested--reject
1155 // send CCP request for no compression for our end if not negotiated
1156 if (!(session
[s
].flags
& SF_CCP_ACKED
))
1167 LOG(1, s
, t
, "Unexpected CCP request code %d\n", *p
);
1169 LOG(1, s
, t
, "Short CCP packet\n");
1171 STAT(tunnel_rx_errors
);
1175 if (!(q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPCCP
)))
1178 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1181 // send a CHAP challenge
1182 void sendchap(tunnelidt t
, sessionidt s
)
1184 uint8_t b
[MAXCONTROL
];
1185 uint16_t r
= sess_local
[s
].radius
;
1192 LOG(1, s
, t
, "No RADIUS to send challenge\n");
1193 STAT(tunnel_tx_errors
);
1197 LOG(1, s
, t
, "Send CHAP challenge\n");
1199 radius
[r
].chap
= 1; // CHAP not PAP
1201 if (radius
[r
].state
!= RADIUSCHAP
)
1204 radius
[r
].state
= RADIUSCHAP
;
1205 radius
[r
].retry
= backoff(radius
[r
].try++);
1206 if (radius
[r
].try > 5)
1208 sessionshutdown(s
, "CHAP timeout.", 3, 0);
1209 STAT(tunnel_tx_errors
);
1212 q
= makeppp(b
, sizeof(b
), 0, 0, t
, s
, PPPCHAP
);
1215 *q
= 1; // challenge
1216 q
[1] = radius
[r
].id
; // ID
1217 q
[4] = 16; // value size (size of challenge)
1218 memcpy(q
+ 5, radius
[r
].auth
, 16); // challenge
1219 strcpy(q
+ 21, hostname
); // our name
1220 *(uint16_t *) (q
+ 2) = htons(strlen(hostname
) + 21); // length
1221 tunnelsend(b
, strlen(hostname
) + 21 + (q
- b
), t
); // send it
1224 // fill in a L2TP message with a PPP frame,
1225 // copies existing PPP message and changes magic number if seen
1226 // returns start of PPP frame
1227 uint8_t *makeppp(uint8_t *b
, int size
, uint8_t *p
, int l
, tunnelidt t
, sessionidt s
, uint16_t mtype
)
1229 if (size
< 12) // Need more space than this!!
1231 static int backtrace_count
= 0;
1232 LOG(0, s
, t
, "makeppp buffer too small for L2TP header (size=%d)\n", size
);
1233 log_backtrace(backtrace_count
, 5)
1237 *(uint16_t *) (b
+ 0) = htons(0x0002); // L2TP with no options
1238 *(uint16_t *) (b
+ 2) = htons(tunnel
[t
].far
); // tunnel
1239 *(uint16_t *) (b
+ 4) = htons(session
[s
].far
); // session
1241 if (mtype
== PPPLCP
|| !(session
[s
].l2tp_flags
& SESSIONACFC
))
1243 *(uint16_t *) b
= htons(0xFF03); // HDLC header
1246 if (mtype
< 0x100 && session
[s
].l2tp_flags
& SESSIONPFC
)
1250 *(uint16_t *) b
= htons(mtype
);
1256 static int backtrace_count
= 0;
1257 LOG(2, s
, t
, "makeppp would overflow buffer (size=%d, header+payload=%d)\n", size
, l
+ 12);
1258 log_backtrace(backtrace_count
, 5)
1268 static uint8_t *add_lcp_auth(uint8_t *b
, int size
, int authtype
)
1270 if ((authtype
== AUTHCHAP
&& size
< 5) || size
< 4)
1273 *b
++ = 3; // Authentication-Protocol
1274 if (authtype
== AUTHCHAP
)
1277 *(uint16_t *) b
= htons(PPPCHAP
); b
+= 2;
1280 else if (authtype
== AUTHPAP
)
1283 *(uint16_t *) b
= htons(PPPPAP
); b
+= 2;
1287 LOG(0, 0, 0, "add_lcp_auth called with unsupported auth type %d\n", authtype
);
1293 // Send initial LCP ConfigReq for MRU, authentication type and magic no
1294 void sendlcp(tunnelidt t
, sessionidt s
, int authtype
)
1296 char b
[500], *q
, *l
;
1298 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, t
, s
, PPPLCP
)))
1301 LOG(4, s
, t
, "Sending LCP ConfigReq for %s\n",
1302 config
->radius_authprefer
== AUTHCHAP
? "CHAP" : "PAP");
1304 if (!session
[s
].mru
)
1305 session
[s
].mru
= DEFAULT_MRU
;
1309 *l
++ = (time_now
% 255) + 1; // ID
1311 l
+= 2; //Save space for length
1313 *l
++ = 1; *l
++ = 4; // Maximum-Receive-Unit (length 4)
1314 *(uint16_t *) l
= htons(session
[s
].mru
); l
+= 2;
1316 l
= add_lcp_auth(l
, sizeof(b
) - (l
- b
), authtype
);
1318 *l
++ = 5; *l
++ = 6; // Magic-Number (length 6)
1319 *(uint32_t *) l
= htonl(session
[s
].magic
);
1322 *(uint16_t *)(q
+ 2) = htons(l
- q
); // Length
1324 LOG_HEX(5, "PPPLCP", q
, l
- q
);
1325 tunnelsend(b
, (l
- b
), t
);
1328 // Send CCP request for no compression
1329 static void initccp(tunnelidt t
, sessionidt s
)
1333 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, t
, s
, PPPCCP
)))
1336 LOG(4, s
, t
, "Sending CCP ConfigReq for no compression\n");
1338 *(uint8_t *)(q
+ 1) = (time_now
% 255) + 1; // ID
1339 *(uint16_t *)(q
+ 2) = htons(4); // Length
1341 LOG_HEX(5, "PPPCCP", q
, 4);
1342 tunnelsend(b
, (q
- b
) + 4 , t
);