3408d86696c070517f842b6d12c93fc62e650356
3 char const *cvs_id_ppp
= "$Id: ppp.c,v 1.61 2005-05-13 09:23:00 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
);
160 sessionshutdown(s
, "Short CHAP packet.", 3, 0);
164 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
166 LOG(1, s
, t
, "Length mismatch CHAP %u/%u\n", hl
, l
);
167 STAT(tunnel_rx_errors
);
168 sessionshutdown(s
, "CHAP length mismatch.", 3, 0);
175 LOG(1, s
, t
, "Unexpected CHAP response code %d\n", *p
);
176 STAT(tunnel_rx_errors
);
177 sessionshutdown(s
, "CHAP length mismatch.", 3, 0);
180 if (p
[1] != radius
[r
].id
)
182 LOG(1, s
, t
, "Wrong CHAP response ID %d (should be %d) (%d)\n", p
[1], radius
[r
].id
, r
);
183 STAT(tunnel_rx_errors
);
184 sessionshutdown(s
, "Unexpected CHAP response ID.", 3, 0);
188 if (l
< 5 || p
[4] != 16)
190 LOG(1, s
, t
, "Bad CHAP response length %d\n", l
< 5 ? -1 : p
[4]);
191 STAT(tunnel_rx_errors
);
192 sessionshutdown(s
, "Bad CHAP response length.", 3, 0);
198 if (l
< 16 || l
- 16 >= sizeof(session
[s
].user
))
200 LOG(1, s
, t
, "CHAP user too long %d\n", l
- 16);
201 STAT(tunnel_rx_errors
);
202 sessionshutdown(s
, "CHAP username too long.", 3, 0);
206 // Run PRE_AUTH plugins
208 struct param_pre_auth packet
= { &tunnel
[t
], &session
[s
], NULL
, NULL
, PPPCHAP
, 1 };
210 packet
.password
= calloc(17, 1);
211 memcpy(packet
.password
, p
, 16);
216 packet
.username
= calloc(l
+ 1, 1);
217 memcpy(packet
.username
, p
, l
);
219 run_plugins(PLUGIN_PRE_AUTH
, &packet
);
220 if (!packet
.continue_auth
)
222 LOG(3, s
, t
, "A plugin rejected PRE_AUTH\n");
223 if (packet
.username
) free(packet
.username
);
224 if (packet
.password
) free(packet
.password
);
228 strncpy(session
[s
].user
, packet
.username
, sizeof(session
[s
].user
) - 1);
229 memcpy(radius
[r
].pass
, packet
.password
, 16);
231 free(packet
.username
);
232 free(packet
.password
);
236 LOG(3, s
, t
, "CHAP login %s\n", session
[s
].user
);
237 radiussend(r
, RADIUSAUTH
);
240 static void dumplcp(uint8_t *p
, int l
)
243 uint8_t *o
= (p
+ 4);
245 LOG_HEX(5, "PPP LCP Packet", p
, l
);
246 LOG(4, 0, 0, "PPP LCP Packet type %d (%s len %d)\n", *p
, ppp_lcp_type((int)*p
), ntohs( ((uint16_t *) p
)[1]) );
247 LOG(4, 0, 0, "Length: %d\n", l
);
248 if (*p
!= ConfigReq
&& *p
!= ConfigRej
&& *p
!= ConfigAck
)
257 LOG(4, 0, 0, " Option length is %d...\n", length
);
262 LOG(4, 0, 0, " Option type is 0...\n");
269 case 1: // Maximum-Receive-Unit
271 LOG(4, 0, 0, " %s %d\n", lcp_type(type
), ntohs(*(uint16_t *)(o
+ 2)));
273 LOG(4, 0, 0, " %s odd length %d\n", lcp_type(type
), length
);
275 case 2: // Async-Control-Character-Map
278 uint32_t asyncmap
= ntohl(*(uint32_t *)(o
+ 2));
279 LOG(4, 0, 0, " %s %x\n", lcp_type(type
), asyncmap
);
282 LOG(4, 0, 0, " %s odd length %d\n", lcp_type(type
), length
);
284 case 3: // Authentication-Protocol
287 int proto
= ntohs(*(uint16_t *)(o
+ 2));
288 LOG(4, 0, 0, " %s 0x%x (%s)\n", lcp_type(type
), proto
,
289 proto
== PPPPAP
? "PAP" : "UNSUPPORTED");
291 else if (length
== 5)
293 int proto
= ntohs(*(uint16_t *)(o
+ 2));
294 int algo
= *(uint8_t *)(o
+ 4);
295 LOG(4, 0, 0, " %s 0x%x 0x%x (%s)\n", lcp_type(type
), proto
, algo
,
296 (proto
== PPPCHAP
&& algo
== 5) ? "CHAP MD5" : "UNSUPPORTED");
299 LOG(4, 0, 0, " %s odd length %d\n", lcp_type(type
), length
);
301 case 4: // Quality-Protocol
303 uint32_t qp
= ntohl(*(uint32_t *)(o
+ 2));
304 LOG(4, 0, 0, " %s %x\n", lcp_type(type
), qp
);
307 case 5: // Magic-Number
310 uint32_t magicno
= ntohl(*(uint32_t *)(o
+ 2));
311 LOG(4, 0, 0, " %s %x\n", lcp_type(type
), magicno
);
314 LOG(4, 0, 0, " %s odd length %d\n", lcp_type(type
), length
);
316 case 7: // Protocol-Field-Compression
317 case 8: // Address-And-Control-Field-Compression
318 LOG(4, 0, 0, " %s\n", lcp_type(type
));
321 LOG(2, 0, 0, " Unknown PPP LCP Option type %d\n", type
);
329 // Process LCP messages
330 void processlcp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
332 uint8_t b
[MAXCONTROL
];
334 uint32_t magicno
= 0;
339 LOG_HEX(5, "LCP", p
, l
);
342 LOG(1, s
, t
, "Short LCP %d bytes\n", l
);
343 STAT(tunnel_rx_errors
);
347 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
349 LOG(1, s
, t
, "Length mismatch LCP %u/%u\n", hl
, l
);
350 STAT(tunnel_rx_errors
);
358 uint8_t *o
= (p
+ 4);
360 LOG(3, s
, t
, "LCP: ConfigAck (%d bytes)...\n", l
);
361 if (config
->debug
> 3) dumplcp(p
, l
);
368 if (length
== 0 || type
== 0 || x
< length
) break;
371 case 3: // Authentication-Protocol
373 int proto
= ntohs(*(uint16_t *)(o
+ 2));
374 if (proto
== PPPCHAP
&& *(o
+ 4) == 5)
384 session
[s
].flags
|= SF_LCP_ACKED
;
386 else if (*p
== ConfigReq
)
389 uint8_t *o
= (p
+ 4);
390 uint8_t *response
= 0;
392 LOG(3, s
, t
, "LCP: ConfigReq (%d bytes)...\n", l
);
393 if (config
->debug
> 3) dumplcp(p
, l
);
400 if (length
== 0 || type
== 0 || x
< length
) break;
403 case 1: // Maximum-Receive-Unit
404 session
[s
].mru
= ntohs(*(uint16_t *)(o
+ 2));
407 case 2: // Async-Control-Character-Map
408 if (!ntohl(*(uint32_t *)(o
+ 2))) // all bits zero is OK
411 if (response
&& *response
!= ConfigNak
) // rej already queued
414 LOG(2, s
, t
, " Remote requesting asyncmap. Rejecting.\n");
417 q
= response
= makeppp(b
, sizeof(b
), p
, 2, t
, s
, PPPLCP
);
423 if ((q
- b
+ 11) > sizeof(b
))
425 LOG(2, s
, t
, "LCP overflow for asyncmap ConfigNak.\n");
431 memset(q
, 0, 4); // asyncmap 0
435 case 3: // Authentication-Protocol
437 int proto
= ntohs(*(uint16_t *)(o
+ 2));
438 char proto_name
[] = "0x0000";
443 if (config
->radius_authtypes
& AUTHPAP
)
446 strcpy(proto_name
, "PAP");
448 else if (proto
== PPPCHAP
)
450 if (config
->radius_authtypes
& AUTHCHAP
451 && *(o
+ 4) == 5) // MD5
454 strcpy(proto_name
, "CHAP");
457 sprintf(proto_name
, "%#4.4x", proto
);
459 if (response
&& *response
!= ConfigNak
) // rej already queued
462 LOG(2, s
, t
, " Remote requesting %s authentication. Rejecting.\n", proto_name
);
466 q
= response
= makeppp(b
, sizeof(b
), p
, 2, t
, s
, PPPLCP
);
472 a
= add_lcp_auth(q
, sizeof(b
) - (q
- b
), config
->radius_authprefer
);
475 LOG(2, s
, t
, "LCP overflow for %s ConfigNak.\n", proto_name
);
481 if (config
->radius_authtypes
!= config
->radius_authprefer
)
483 a
= add_lcp_auth(q
, sizeof(b
) - (q
- b
), config
->radius_authtypes
& ~config
->radius_authprefer
);
486 LOG(2, s
, t
, "LCP overflow for %s ConfigNak.\n", proto_name
);
497 case 5: // Magic-Number
498 magicno
= ntohl(*(uint32_t *)(o
+ 2));
501 case 4: // Quality-Protocol
502 case 7: // Protocol-Field-Compression
503 case 8: // Address-And-Control-Field-Compression
506 default: // Reject any unknown options
507 LOG(2, s
, t
, " Rejecting PPP LCP Option type %d\n", type
);
508 if (!response
|| *response
!= ConfigRej
) // drop nak in favour of rej
510 q
= response
= makeppp(b
, sizeof(b
), p
, 2, t
, s
, PPPLCP
);
516 if ((q
- b
+ length
) > sizeof(b
))
518 LOG(2, s
, t
, "LCP overflow for ConfigRej (type=%d).\n", type
);
522 memcpy(q
, o
, length
);
531 l
= q
- response
; // LCP packet length
532 *((uint16_t *) (response
+ 2)) = htons(l
); // update header
536 // Send packet back as ConfigAck
537 response
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
538 if (!response
) return;
539 *response
= ConfigAck
;
542 LOG(3, s
, t
, "Sending %s\n", ppp_lcp_type(*response
));
543 tunnelsend(b
, l
+ response
- b
, t
);
545 if (!(session
[s
].flags
& SF_LCP_ACKED
))
546 sendlcp(t
, s
, config
->radius_authprefer
);
548 else if (*p
== ConfigNak
)
551 uint8_t *o
= (p
+ 4);
554 LOG(3, s
, t
, "LCP: ConfigNak (%d bytes)...\n", l
);
555 if (config
->debug
> 3) dumplcp(p
, l
);
562 if (length
== 0 || type
== 0 || x
< length
) break;
565 case 1: // Maximum-Receive-Unit
566 session
[s
].mru
= ntohs(*(uint16_t *)(o
+ 2));
567 LOG(3, s
, t
, " Remote requested MRU of %u\n", session
[s
].mru
);
570 case 3: // Authentication-Protocol
575 int proto
= ntohs(*(uint16_t *)(o
+ 2));
578 authtype
= config
->radius_authtypes
& AUTHPAP
;
579 LOG(3, s
, t
, " Remote requested PAP authentication...%sing\n",
580 authtype
? "accept" : "reject");
582 else if (proto
== PPPCHAP
&& *(o
+ 4) == 5)
584 authtype
= config
->radius_authtypes
& AUTHCHAP
;
585 LOG(3, s
, t
, " Remote requested CHAP authentication...%sing\n",
586 authtype
? "accept" : "reject");
590 LOG(3, s
, t
, " Rejecting unsupported authentication %#4x\n",
598 LOG(2, s
, t
, " Remote NAKed LCP type %u?\n", type
);
607 sessionshutdown(s
, "Unsupported authentication.", 3, 0);
612 authtype
= config
->radius_authprefer
;
614 sendlcp(t
, s
, authtype
);
616 else if (*p
== TerminateReq
)
618 LOG(3, s
, t
, "LCP: Received TerminateReq. Sending TerminateAck\n");
619 *p
= TerminateAck
; // close
620 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
622 tunnelsend(b
, l
+ (q
- b
), t
); // send it
623 sessionshutdown(s
, "Remote end closed connection.", 3, 0);
625 else if (*p
== TerminateAck
)
627 sessionshutdown(s
, "Connection closed.", 3, 0);
629 else if (*p
== ProtocolRej
)
631 if (*(uint16_t *) (p
+4) == htons(PPPIPV6CP
))
633 LOG(3, s
, t
, "IPv6 rejected\n");
634 session
[s
].flags
|= SF_IPV6_NACKED
;
638 LOG(1, s
, t
, "Unexpected LCP protocol reject 0x%X\n",
639 ntohs(*(uint16_t *) (p
+4)));
640 STAT(tunnel_rx_errors
);
643 else if (*p
== EchoReq
)
645 LOG(5, s
, t
, "LCP: Received EchoReq. Sending EchoReply\n");
646 *p
= EchoReply
; // reply
647 *(uint32_t *) (p
+ 4) = htonl(session
[s
].magic
); // our magic number
648 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
650 tunnelsend(b
, l
+ (q
- b
), t
); // send it
652 else if (*p
== EchoReply
)
654 // Ignore it, last_packet time is set earlier than this.
656 else if (*p
== IdentRequest
)
661 LOG(1, s
, t
, "Truncated Ident Packet (length=%d) to 1400 bytes\n", l
);
664 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
666 LOG_HEX(5, "LCPIdentRej", q
, l
+ 4);
667 tunnelsend(b
, 12 + 4 + l
, t
);
671 LOG(1, s
, t
, "Unexpected LCP code %d\n", *p
);
672 STAT(tunnel_rx_errors
);
676 // find a PPP option, returns point to option, or 0 if not found
677 static uint8_t *findppp(uint8_t *b
, uint8_t mtype
)
679 uint16_t l
= ntohs(*(uint16_t *) (b
+ 2));
686 if (l
< b
[1] || !b
[1])
696 // Process IPCP messages
697 void processipcp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
703 LOG_HEX(5, "IPCP", p
, l
);
706 LOG(1, s
, t
, "Short IPCP %d bytes\n", l
);
707 STAT(tunnel_rx_errors
);
711 if ((hl
= ntohs(*(uint16_t *) (p
+ 2))) > l
)
713 LOG(1, s
, t
, "Length mismatch IPCP %u/%u\n", hl
, l
);
714 STAT(tunnel_rx_errors
);
721 // happy with our IPCP
722 uint16_t r
= sess_local
[s
].radius
;
723 if ((!r
|| radius
[r
].state
== RADIUSIPCP
) && !session
[s
].walled_garden
)
728 radiussend(r
, RADIUSSTART
); // send radius start, having got IPCP at last
730 session
[s
].flags
|= SF_IPCP_ACKED
;
732 LOG(3, s
, t
, "IPCP Acked, session is now active\n");
734 // clear LCP_ACKED/CCP_ACKED flag for possible fast renegotiaion for routers
735 session
[s
].flags
&= ~(SF_LCP_ACKED
|SF_CCP_ACKED
);
741 LOG(1, s
, t
, "Unexpected IPCP code %d\n", *p
);
742 STAT(tunnel_rx_errors
);
745 LOG(4, s
, t
, "IPCP ConfigReq received\n");
749 LOG(3, s
, t
, "Waiting on radius reply\n");
750 return; // have to wait on RADIUS reply
752 // form a config reply quoting the IP in the session
754 uint8_t b
[MAXCONTROL
];
759 while (q
< i
&& q
[1])
761 if (*q
!= 0x81 && *q
!= 0x83 && *q
!= 3)
770 if (!(q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPIPCP
)))
775 while (p
< i
&& p
[1])
777 if (*p
!= 0x81 && *p
!= 0x83 && *p
!= 3)
779 LOG(2, s
, t
, "IPCP reject %d\n", *p
);
780 memcpy(q
+ n
, p
, p
[1]);
785 *(uint16_t *) (q
+ 2) = htons(n
);
786 LOG(4, s
, t
, "Sending ConfigRej\n");
787 tunnelsend(b
, n
+ (q
- b
), t
); // send it
791 LOG(4, s
, t
, "Sending ConfigAck\n");
793 if ((i
= findppp(p
, 0x81))) // Primary DNS address
795 if (*(uint32_t *) (i
+ 2) != htonl(session
[s
].dns1
))
797 *(uint32_t *) (i
+ 2) = htonl(session
[s
].dns1
);
799 LOG(5, s
, t
, " DNS1 = %s\n",
800 fmtaddr(htonl(session
[s
].dns1
), 0));
803 if ((i
= findppp(p
, 0x83))) // Secondary DNS address (TBA, is it)
805 if (*(uint32_t *) (i
+ 2) != htonl(session
[s
].dns2
))
807 *(uint32_t *) (i
+ 2) = htonl(session
[s
].dns2
);
809 LOG(5, s
, t
, " DNS2 = %s\n",
810 fmtaddr(htonl(session
[s
].dns2
), 0));
813 i
= findppp(p
, 3); // IP address
816 LOG(1, s
, t
, "No IP in IPCP request\n");
817 STAT(tunnel_rx_errors
);
820 if (*(uint32_t *) (i
+ 2) != htonl(session
[s
].ip
))
822 *(uint32_t *) (i
+ 2) = htonl(session
[s
].ip
);
824 LOG(4, s
, t
, " No, a ConfigNak, client is requesting IP - sending %s\n",
825 fmtaddr(htonl(session
[s
].ip
), 0));
827 if (!(q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPIPCP
)))
830 tunnelsend(b
, l
+ (q
- b
), t
); // send it
835 // Process IPV6CP messages
836 void processipv6cp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
839 CSTAT(processipv6cp
);
841 LOG_HEX(5, "IPV6CP", p
, l
);
844 LOG(1, s
, t
, "Short IPV6CP %d bytes\n", l
);
845 STAT(tunnel_rx_errors
);
850 // happy with our IPV6CP
851 session
[s
].flags
|= SF_IPV6CP_ACKED
;
853 LOG(3, s
, t
, "IPV6CP Acked, IPv6 is now active\n");
854 // Add a routed block if configured.
855 if (session
[s
].ipv6prefixlen
)
857 route6set(s
, session
[s
].ipv6route
, session
[s
].ipv6prefixlen
, 1);
858 session
[s
].flags
|= SF_IPV6_ROUTED
;
861 // Send an initial RA (TODO: Should we send these regularly?)
862 send_ipv6_ra(t
, s
, NULL
);
867 LOG(1, s
, t
, "Unexpected IPV6CP code %d\n", *p
);
868 STAT(tunnel_rx_errors
);
872 LOG(4, s
, t
, "IPV6CP ConfigReq received\n");
873 if (ntohs(*(uint16_t *) (p
+ 2)) > l
)
875 LOG(1, s
, t
, "Length mismatch IPV6CP %d/%d\n", ntohs(*(uint16_t *) (p
+ 2)), l
);
876 STAT(tunnel_rx_errors
);
881 LOG(3, s
, t
, "Waiting on radius reply\n");
882 return; // have to wait on RADIUS reply
884 // form a config reply quoting the IP in the session
886 uint8_t b
[MAXCONTROL
];
890 l
= ntohs(*(uint16_t *) (p
+ 2)); // We must use length from IPV6CP len field
893 while (q
< i
&& q
[1])
904 if (!(q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPIPV6CP
)))
906 LOG(2, s
, t
, "Failed to send IPV6CP ConfigRej\n");
911 while (p
< i
&& p
[1])
915 LOG(2, s
, t
, "IPV6CP reject %d\n", *p
);
916 memcpy(q
+ n
, p
, p
[1]);
921 *(uint16_t *) (q
+ 2) = htons(n
);
922 LOG(4, s
, t
, "Sending ConfigRej\n");
923 tunnelsend(b
, n
+ (q
- b
), t
); // send it
927 LOG(4, s
, t
, "Sending ConfigAck\n");
929 i
= findppp(p
, 1); // IP address
930 if (!i
|| i
[1] != 10)
932 LOG(1, s
, t
, "No IP in IPV6CP request\n");
933 STAT(tunnel_rx_errors
);
936 if ((*(uint32_t *) (i
+ 2) != htonl(session
[s
].ip
)) ||
937 (*(uint32_t *) (i
+ 6) != 0))
939 *(uint32_t *) (i
+ 2) = htonl(session
[s
].ip
);
940 *(uint32_t *) (i
+ 6) = 0;
943 " No, a ConfigNak, client is "
944 "requesting IP - sending %s\n",
945 fmtaddr(htonl(session
[s
].ip
), 0));
947 if (!(q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPIPV6CP
)))
949 LOG(2, s
, t
, " Failed to send IPV6CP packet.\n");
952 tunnelsend(b
, l
+ (q
- b
), t
); // send it
957 // process IP packet received
959 // This MUST be called with at least 4 byte behind 'p'.
960 // (i.e. this routine writes to p[-4]).
961 void processipin(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
967 LOG_HEX(5, "IP", p
, l
);
969 ip
= ntohl(*(uint32_t *)(p
+ 12));
973 LOG(1, s
, t
, "IP packet too long %d\n", l
);
974 STAT(tunnel_rx_errors
);
978 // no spoof (do sessionbyip to handled statically routed subnets)
979 if (ip
!= session
[s
].ip
&& sessionbyip(htonl(ip
)) != s
)
981 LOG(5, s
, t
, "Dropping packet with spoofed IP %s\n", fmtaddr(htonl(ip
), 0));
985 // run access-list if any
986 if (session
[s
].filter_in
&& !ip_filter(p
, l
, session
[s
].filter_in
- 1))
989 // Add on the tun header
991 *(uint32_t *) p
= htonl(PKTIP
);
994 // Are we throttled and a slave?
995 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) {
996 // Pass it to the master for handling.
997 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
1001 // Are we throttled and a master??
1002 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) {
1003 // Actually handle the throttled packets.
1004 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
1009 if (tun_write(p
, l
) < 0)
1011 STAT(tun_tx_errors
);
1012 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1013 l
, strerror(errno
), tunfd
, p
);
1018 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1020 // Snooping this session
1021 snoop_send_packet(p
+ 4, l
- 4, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1024 session
[s
].cin
+= l
- 4;
1025 session
[s
].total_cin
+= l
- 4;
1026 sess_local
[s
].cin
+= l
- 4;
1031 STAT(tun_tx_packets
);
1032 INC_STAT(tun_tx_bytes
, l
- 4);
1035 // process IPv6 packet received
1037 // This MUST be called with at least 4 byte behind 'p'.
1038 // (i.e. this routine writes to p[-4]).
1039 void processipv6in(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
1044 CSTAT(processipv6in
);
1046 LOG_HEX(5, "IPv6", p
, l
);
1048 ip
= *(struct in6_addr
*) (p
+ 8);
1049 ipv4
= ntohl(*(uint32_t *)(p
+ 16));
1053 LOG(1, s
, t
, "IP packet too long %d\n", l
);
1054 STAT(tunnel_rx_errors
);
1059 if (ipv4
!= session
[s
].ip
&& memcmp(&config
->ipv6_prefix
, &ip
, 8) && sessionbyipv6(ip
) != s
)
1061 char str
[INET6_ADDRSTRLEN
];
1062 LOG(5, s
, t
, "Dropping packet with spoofed IP %s\n",
1063 inet_ntop(AF_INET6
, &ip
, str
, INET6_ADDRSTRLEN
));
1067 // Check if it's a Router Solicition message.
1068 if (*(p
+ 6) == 58 && *(p
+ 7) == 255 && *(p
+ 24) == 0xFF && *(p
+ 25) == 2 &&
1069 *(uint32_t *)(p
+ 26) == 0 && *(uint32_t *)(p
+ 30) == 0 &&
1070 *(uint32_t *)(p
+ 34) == 0 &&
1071 *(p
+ 38) == 0 && *(p
+ 39) == 2 && *(p
+ 40) == 133) {
1072 LOG(3, s
, t
, "Got IPv6 RS\n");
1073 send_ipv6_ra(t
, s
, &ip
);
1077 // Add on the tun header
1079 *(uint32_t *) p
= htonl(PKTIPV6
);
1082 // Are we throttled and a slave?
1083 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) {
1084 // Pass it to the master for handling.
1085 master_throttle_packet(session
[s
].tbf_in
, p
, l
);
1089 // Are we throttled and a master??
1090 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) {
1091 // Actually handle the throttled packets.
1092 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
1097 if (tun_write(p
, l
) < 0)
1099 STAT(tun_tx_errors
);
1100 LOG(0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1101 l
, strerror(errno
), tunfd
, p
);
1106 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1108 // Snooping this session
1109 snoop_send_packet(p
+ 4, l
- 4, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1112 session
[s
].cin
+= l
- 4;
1113 session
[s
].total_cin
+= l
- 4;
1114 sess_local
[s
].cin
+= l
- 4;
1119 STAT(tun_tx_packets
);
1120 INC_STAT(tun_tx_bytes
, l
- 4);
1124 // Helper routine for the TBF filters.
1125 // Used to send queued data in from the user.
1127 void send_ipin(sessionidt s
, uint8_t *buf
, int len
)
1129 LOG_HEX(5, "IP in throttled", buf
, len
);
1131 if (write(tunfd
, buf
, len
) < 0)
1133 STAT(tun_tx_errors
);
1134 LOG(0, 0, 0, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
1135 len
, strerror(errno
), tunfd
, buf
);
1140 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
1142 // Snooping this session
1143 snoop_send_packet(buf
+ 4, len
- 4, session
[s
].snoop_ip
, session
[s
].snoop_port
);
1146 // Increment packet counters
1147 session
[s
].cin
+= len
- 4;
1148 session
[s
].total_cin
+= len
- 4;
1149 sess_local
[s
].cin
+= len
- 4;
1154 STAT(tun_tx_packets
);
1155 INC_STAT(tun_tx_bytes
, len
- 4);
1159 // Process CCP messages
1160 void processccp(tunnelidt t
, sessionidt s
, uint8_t *p
, uint16_t l
)
1162 uint8_t b
[MAXCONTROL
];
1167 LOG_HEX(5, "CCP", p
, l
);
1168 switch (l
> 1 ? *p
: 0)
1171 session
[s
].flags
|= SF_CCP_ACKED
;
1175 if (l
< 6) // accept no compression
1181 // compression requested--reject
1184 // send CCP request for no compression for our end if not negotiated
1185 if (!(session
[s
].flags
& SF_CCP_ACKED
))
1196 LOG(1, s
, t
, "Unexpected CCP request code %d\n", *p
);
1198 LOG(1, s
, t
, "Short CCP packet\n");
1200 STAT(tunnel_rx_errors
);
1204 if (!(q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPCCP
)))
1207 tunnelsend(b
, l
+ (q
- b
), t
); // send it
1210 // send a CHAP challenge
1211 void sendchap(tunnelidt t
, sessionidt s
)
1213 uint8_t b
[MAXCONTROL
];
1214 uint16_t r
= sess_local
[s
].radius
;
1221 LOG(1, s
, t
, "No RADIUS to send challenge\n");
1222 STAT(tunnel_tx_errors
);
1226 LOG(1, s
, t
, "Send CHAP challenge\n");
1228 radius
[r
].chap
= 1; // CHAP not PAP
1230 if (radius
[r
].state
!= RADIUSCHAP
)
1233 radius
[r
].state
= RADIUSCHAP
;
1234 radius
[r
].retry
= backoff(radius
[r
].try++);
1235 if (radius
[r
].try > 5)
1237 sessionshutdown(s
, "CHAP timeout.", 3, 0);
1238 STAT(tunnel_tx_errors
);
1241 q
= makeppp(b
, sizeof(b
), 0, 0, t
, s
, PPPCHAP
);
1244 *q
= 1; // challenge
1245 q
[1] = radius
[r
].id
; // ID
1246 q
[4] = 16; // value size (size of challenge)
1247 memcpy(q
+ 5, radius
[r
].auth
, 16); // challenge
1248 strcpy(q
+ 21, hostname
); // our name
1249 *(uint16_t *) (q
+ 2) = htons(strlen(hostname
) + 21); // length
1250 tunnelsend(b
, strlen(hostname
) + 21 + (q
- b
), t
); // send it
1253 // fill in a L2TP message with a PPP frame,
1254 // copies existing PPP message and changes magic number if seen
1255 // returns start of PPP frame
1256 uint8_t *makeppp(uint8_t *b
, int size
, uint8_t *p
, int l
, tunnelidt t
, sessionidt s
, uint16_t mtype
)
1258 if (size
< 12) // Need more space than this!!
1260 static int backtrace_count
= 0;
1261 LOG(0, s
, t
, "makeppp buffer too small for L2TP header (size=%d)\n", size
);
1262 log_backtrace(backtrace_count
, 5)
1266 *(uint16_t *) (b
+ 0) = htons(0x0002); // L2TP with no options
1267 *(uint16_t *) (b
+ 2) = htons(tunnel
[t
].far
); // tunnel
1268 *(uint16_t *) (b
+ 4) = htons(session
[s
].far
); // session
1270 if (mtype
== PPPLCP
|| !(session
[s
].l2tp_flags
& SESSIONACFC
))
1272 *(uint16_t *) b
= htons(0xFF03); // HDLC header
1275 if (mtype
< 0x100 && session
[s
].l2tp_flags
& SESSIONPFC
)
1279 *(uint16_t *) b
= htons(mtype
);
1285 static int backtrace_count
= 0;
1286 LOG(2, s
, t
, "makeppp would overflow buffer (size=%d, header+payload=%d)\n", size
, l
+ 12);
1287 log_backtrace(backtrace_count
, 5)
1297 static uint8_t *add_lcp_auth(uint8_t *b
, int size
, int authtype
)
1299 if ((authtype
== AUTHCHAP
&& size
< 5) || size
< 4)
1302 *b
++ = 3; // Authentication-Protocol
1303 if (authtype
== AUTHCHAP
)
1306 *(uint16_t *) b
= htons(PPPCHAP
); b
+= 2;
1309 else if (authtype
== AUTHPAP
)
1312 *(uint16_t *) b
= htons(PPPPAP
); b
+= 2;
1316 LOG(0, 0, 0, "add_lcp_auth called with unsupported auth type %d\n", authtype
);
1322 // Send initial LCP ConfigReq for MRU, authentication type and magic no
1323 void sendlcp(tunnelidt t
, sessionidt s
, int authtype
)
1325 char b
[500], *q
, *l
;
1327 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, t
, s
, PPPLCP
)))
1330 LOG(4, s
, t
, "Sending LCP ConfigReq for %s\n",
1331 authtype
== AUTHCHAP
? "CHAP" : "PAP");
1333 if (!session
[s
].mru
)
1334 session
[s
].mru
= DEFAULT_MRU
;
1338 *l
++ = (time_now
% 255) + 1; // ID
1340 l
+= 2; //Save space for length
1342 *l
++ = 1; *l
++ = 4; // Maximum-Receive-Unit (length 4)
1343 *(uint16_t *) l
= htons(session
[s
].mru
); l
+= 2;
1345 l
= add_lcp_auth(l
, sizeof(b
) - (l
- b
), authtype
);
1347 *l
++ = 5; *l
++ = 6; // Magic-Number (length 6)
1348 *(uint32_t *) l
= htonl(session
[s
].magic
);
1351 *(uint16_t *)(q
+ 2) = htons(l
- q
); // Length
1353 LOG_HEX(5, "PPPLCP", q
, l
- q
);
1354 tunnelsend(b
, (l
- b
), t
);
1357 // Send CCP request for no compression
1358 static void initccp(tunnelidt t
, sessionidt s
)
1362 if (!(q
= makeppp(b
, sizeof(b
), NULL
, 0, t
, s
, PPPCCP
)))
1365 LOG(4, s
, t
, "Sending CCP ConfigReq for no compression\n");
1367 *(uint8_t *)(q
+ 1) = (time_now
% 255) + 1; // ID
1368 *(uint16_t *)(q
+ 2) = htons(4); // Length
1370 LOG_HEX(5, "PPPCCP", q
, 4);
1371 tunnelsend(b
, (q
- b
) + 4 , t
);