de37bf2bd96fce274aff9139bfb0986872ad04bc
3 char const *cvs_id_ppp
= "$Id: ppp.c,v 1.9 2004-08-02 03:38:01 fred_nerk Exp $";
11 #include "constants.h"
17 extern tunnelt
*tunnel
;
18 extern sessiont
*session
;
19 extern radiust
*radius
;
21 extern char hostname
[];
23 extern time_t time_now
;
24 extern struct configt
*config
;
26 // Process PAP messages
27 void processpap(tunnelidt t
, sessionidt s
, u8
* p
, u16 l
)
33 CSTAT(call_processpap
);
35 log_hex(5, "PAP", p
, l
);
38 log(1, 0, s
, t
, "Short PAP %u bytes\n", l
);
39 STAT(tunnel_rx_errors
);
44 log(1, 0, s
, t
, "Unexpected PAP code %d\n", *p
);
45 STAT(tunnel_rx_errors
);
48 if (ntohs(*(u16
*) (p
+ 2)) > l
)
50 log(1, 0, s
, t
, "Length mismatch PAP %d/%d\n", ntohs(*(u16
*) (p
+ 2)), l
);
51 STAT(tunnel_rx_errors
);
57 if (*b
&& *b
< sizeof(user
))
58 memcpy(user
, b
+ 1, *b
);
61 if (*b
&& *b
< sizeof(pass
))
62 memcpy(pass
, b
+ 1, *b
);
64 log(3, 0, s
, t
, "PAP login %s/%s\n", user
, pass
);
66 if (session
[s
].ip
|| !session
[s
].radius
)
68 // respond now, either no RADIUS available or already authenticated
71 u8
*p
= makeppp(b
, sizeof(b
), 0, 0, t
, s
, PPPPAP
);
72 if (!p
) { // Failed to make ppp header!
73 log(1,0,0,0, "Failed to make PPP header in process pap!\n");
79 *p
= 3; // cant authorise
81 *(u16
*) (p
+ 2) = htons(5); // length
82 p
[4] = 0; // no message
85 log(3, session
[s
].ip
, s
, t
, "%d Already an IP allocated: %s (%d)\n", getpid(), inet_toa(htonl(session
[s
].ip
)), session
[s
].ip_pool_index
);
89 log(1, 0, s
, t
, "No radius session available to authenticate session...\n");
91 log(3, 0, s
, t
, "Fallback response to PAP (%s)\n", (session
[s
].ip
) ? "ACK" : "NAK");
92 tunnelsend(b
, 5 + (p
- b
), t
); // send it
95 { // set up RADIUS request
96 u16 r
= session
[s
].radius
;
98 // Run PRE_AUTH plugins
99 struct param_pre_auth packet
= { &tunnel
[t
], &session
[s
], strdup(user
), strdup(pass
), PPPPAP
, 1 };
100 run_plugins(PLUGIN_PRE_AUTH
, &packet
);
101 if (!packet
.continue_auth
)
103 log(3, 0, s
, t
, "A plugin rejected PRE_AUTH\n");
104 if (packet
.username
) free(packet
.username
);
105 if (packet
.password
) free(packet
.password
);
109 strncpy(session
[s
].user
, packet
.username
, sizeof(session
[s
].user
) - 1);
110 strncpy(radius
[r
].pass
, packet
.password
, sizeof(radius
[r
].pass
) - 1);
112 free(packet
.username
);
113 free(packet
.password
);
116 log(3, 0, s
, t
, "Sending login for %s/%s to radius\n", user
, pass
);
117 radiussend(r
, RADIUSAUTH
);
121 // Process CHAP messages
122 void processchap(tunnelidt t
, sessionidt s
, u8
* p
, u16 l
)
128 CSTAT(call_processchap
);
130 log_hex(5, "CHAP", p
, l
);
131 r
= session
[s
].radius
;
134 log(1, 0, s
, t
, "Unexpected CHAP message\n");
136 // FIXME: Need to drop the session here.
138 STAT(tunnel_rx_errors
);
143 log(1, 0, s
, t
, "Unexpected CHAP response code %d\n", *p
);
144 STAT(tunnel_rx_errors
);
147 if (p
[1] != radius
[r
].id
)
149 log(1, 0, s
, t
, "Wrong CHAP response ID %d (should be %d) (%d)\n", p
[1], radius
[r
].id
, r
);
150 STAT(tunnel_rx_errors
);
153 len
= ntohs(*(u16
*) (p
+ 2));
156 log(1, 0, s
, t
, "Bad CHAP length %d\n", len
);
157 STAT(tunnel_rx_errors
);
162 log(1, 0, s
, t
, "Bad CHAP response length %d\n", p
[4]);
163 STAT(tunnel_rx_errors
);
166 if (len
- 21 >= sizeof(session
[s
].user
))
168 log(1, 0, s
, t
, "CHAP user too long %d\n", len
- 21);
169 STAT(tunnel_rx_errors
);
173 // Run PRE_AUTH plugins
175 struct param_pre_auth packet
= { &tunnel
[t
], &session
[s
], NULL
, NULL
, PPPCHAP
, 1 };
177 packet
.username
= calloc(len
-20, 1);
178 packet
.password
= calloc(16, 1);
179 memcpy(packet
.username
, p
+ 21, len
- 21);
180 memcpy(packet
.password
, p
+ 5, 16);
182 run_plugins(PLUGIN_PRE_AUTH
, &packet
);
183 if (!packet
.continue_auth
)
185 log(3, 0, s
, t
, "A plugin rejected PRE_AUTH\n");
186 if (packet
.username
) free(packet
.username
);
187 if (packet
.password
) free(packet
.password
);
191 strncpy(session
[s
].user
, packet
.username
, sizeof(session
[s
].user
) - 1);
192 memcpy(radius
[r
].pass
, packet
.password
, 16);
194 free(packet
.username
);
195 free(packet
.password
);
199 log(3, 0, s
, t
, "CHAP login %s\n", session
[s
].user
);
200 radiussend(r
, RADIUSAUTH
);
203 char *ppp_lcp_types
[] = {
218 void dumplcp(u8
*p
, int l
)
220 signed int x
= l
- 4;
223 log_hex(5, "PPP LCP Packet", p
, l
);
224 log(4, 0, 0, 0, "PPP LCP Packet type %d (%s len %d)\n", *p
, ppp_lcp_types
[(int)*p
], ntohs( ((u16
*) p
)[1]) );
225 log(4, 0, 0, 0, "Length: %d\n", l
);
226 if (*p
!= ConfigReq
&& *p
!= ConfigRej
&& *p
!= ConfigAck
)
235 log(4, 0, 0, 0, " Option length is %d...\n", length
);
240 log(4, 0, 0, 0, " Option type is 0...\n");
247 case 1: // Maximum-Receive-Unit
249 log(4, 0, 0, 0, " %s %d\n", lcp_types
[type
], ntohs(*(u16
*)(o
+ 2)));
251 log(4, 0, 0, 0, " %s odd length %d\n", lcp_types
[type
], length
);
253 case 3: // Authentication-Protocol
257 int proto
= ntohs(*(u16
*)(o
+ 2));
258 log(4, 0, 0, 0, " %s 0x%x (%s)\n", lcp_types
[type
], proto
,
259 proto
== 0xC223 ? "CHAP" :
260 proto
== 0xC023 ? "PAP" : "UNKNOWN");
263 log(4, 0, 0, 0, " %s odd length %d\n", lcp_types
[type
], length
);
266 case 4: // Quality-Protocol
268 u32 qp
= ntohl(*(u32
*)(o
+ 2));
269 log(4, 0, 0, 0, " %s %x\n", lcp_types
[type
], qp
);
272 case 5: // Magic-Number
276 u32 magicno
= ntohl(*(u32
*)(o
+ 2));
277 log(4, 0, 0, 0, " %s %x\n", lcp_types
[type
], magicno
);
280 log(4, 0, 0, 0, " %s odd length %d\n", lcp_types
[type
], length
);
283 case 7: // Protocol-Field-Compression
285 log(4, 0, 0, 0, " %s\n", lcp_types
[type
]);
288 case 8: // Address-And-Control-Field-Compression
290 log(4, 0, 0, 0, " %s\n", lcp_types
[type
]);
294 log(2, 0, 0, 0, " Unknown PPP LCP Option type %d\n", type
);
302 // Process LCP messages
303 void processlcp(tunnelidt t
, sessionidt s
, u8
* p
, u16 l
)
309 CSTAT(call_processlcp
);
311 log_hex(5, "LCP", p
, l
);
314 log(1, session
[s
].ip
, s
, t
, "Short LCP %d bytes\n", l
);
315 STAT(tunnel_rx_errors
);
320 log(3, session
[s
].ip
, s
, t
, "LCP: Discarding ConfigAck\n");
321 session
[s
].flags
|= SESSIONLCPACK
;
323 else if (*p
== ConfigReq
)
325 signed int x
= l
- 4;
328 log(3, session
[s
].ip
, s
, t
, "LCP: ConfigReq (%d bytes)...\n", l
);
335 if (length
== 0 || type
== 0) break;
338 case 1: // Maximum-Receive-Unit
339 session
[s
].mru
= ntohs(*(u16
*)(o
+ 2));
341 case 3: // Authentication-Protocol
343 int proto
= ntohs(*(u16
*)(o
+ 2));
346 log(2, session
[s
].ip
, s
, t
, " Remote end is trying to do CHAP. Rejecting it.\n");
350 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
352 log(2, session
[s
].ip
, s
, t
, " Failed to send packet.\n");
357 memcpy(q
, o
, length
);
358 *(u16
*)(q
+= 2) = htons(0xC023); // NAK -> Use PAP instead
363 case 5: // Magic-Number
365 // u32 magicno = ntohl(*(u32 *)(o + 2));
368 case 4: // Quality-Protocol
370 // u32 qp = ntohl(*(u32 *)(o + 2));
373 case 7: // Protocol-Field-Compression
375 // u32 pfc = ntohl(*(u32 *)(o + 2));
378 case 8: // Address-And-Control-Field-Compression
380 // u32 afc = ntohl(*(u32 *)(o + 2));
384 log(2, session
[s
].ip
, s
, t
, " Unknown PPP LCP Option type %d\n", type
);
393 // Send back a ConfigAck
394 log(3, session
[s
].ip
, s
, t
, "ConfigReq accepted, sending as Ack\n");
395 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
397 log(3, session
[s
].ip
, s
, t
, " failed to create packet.\n");
401 tunnelsend(b
, l
+ (q
- b
), t
);
405 // Already built a ConfigNak... send it
406 log(3, session
[s
].ip
, s
, t
, "Sending ConfigNak\n");
407 tunnelsend(b
, l
+ (q
- b
), t
);
410 if (!(session
[s
].flags
& SESSIONLCPACK
))
413 else if (*p
== ConfigNak
)
415 log(1, session
[s
].ip
, s
, t
, "Remote end sent a ConfigNak. Ignoring\n");
419 else if (*p
== TerminateReq
)
421 *p
= TerminateAck
; // close
422 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
424 log(3, session
[s
].ip
, s
, t
, "Failed to create PPP packet in processlcp.\n");
427 log(3, session
[s
].ip
, s
, t
, "LCP: Received TerminateReq. Sending TerminateAck\n");
428 sessionshutdown(s
, "Remote end closed connection.");
429 tunnelsend(b
, l
+ (q
- b
), t
); // send it
431 else if (*p
== TerminateAck
)
433 sessionshutdown(s
, "Connection closed.");
435 else if (*p
== EchoReq
)
437 *p
= EchoReply
; // reply
438 *(u32
*) (p
+ 4) = htonl(session
[s
].magic
); // our magic number
439 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
441 log(3, session
[s
].ip
, s
, t
, " failed to send EchoReply.\n");
444 log(5, session
[s
].ip
, s
, t
, "LCP: Received EchoReq. Sending EchoReply\n");
445 tunnelsend(b
, l
+ (q
- b
), t
); // send it
447 else if (*p
== EchoReply
)
449 // Ignore it, last_packet time is set earlier than this.
453 log(1, session
[s
].ip
, s
, t
, "Unexpected LCP code %d\n", *p
);
454 STAT(tunnel_rx_errors
);
459 // Process IPCP messages
460 void processipcp(tunnelidt t
, sessionidt s
, u8
* p
, u16 l
)
463 CSTAT(call_processipcp
);
465 log_hex(5, "IPCP", p
, l
);
468 log(1, 0, s
, t
, "Short IPCP %d bytes\n", l
);
469 STAT(tunnel_rx_errors
);
473 { // happy with our IPCP
474 u16 r
= session
[s
].radius
;
475 if ((!r
|| radius
[r
].state
== RADIUSIPCP
) && !session
[s
].walled_garden
) {
479 radiussend(r
, RADIUSSTART
); // send radius start, having got IPCP at last
481 session
[s
].flags
|= SF_IPCP_ACKED
;
487 log(1, 0, s
, t
, "Unexpected IPCP code %d\n", *p
);
488 STAT(tunnel_rx_errors
);
491 if (ntohs(*(u16
*) (p
+ 2)) > l
)
493 log(1, 0, s
, t
, "Length mismatch IPCP %d/%d\n", ntohs(*(u16
*) (p
+ 2)), l
);
494 STAT(tunnel_rx_errors
);
499 log(3, 0, s
, t
, "Waiting on radius reply\n");
500 return ; // have to wait on RADIUS reply
502 // form a config reply quoting the IP in the session
510 while (q
< i
&& q
[1])
512 if (*q
!= 0x81 && *q
!= 0x83 && *q
!= 3)
520 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPIPCP
);
522 log(2, 0, s
, t
, "Failed to send IPCP.\n");
527 while (p
< i
&& p
[1])
529 if (*p
!= 0x81 && *p
!= 0x83 && *p
!= 3)
531 log(2, 0, s
, t
, "IPCP reject %d\n", *p
);
532 memcpy(q
+ n
, p
, p
[1]);
537 *(u16
*) (q
+ 2) = htons(n
);
538 tunnelsend(b
, n
+ (q
- b
), t
); // send it
543 i
= findppp(p
, 0x81); // Primary DNS address
546 if (*(u32
*) (i
+ 2) != htonl(session
[s
].dns1
))
548 *(u32
*) (i
+ 2) = htonl(session
[s
].dns1
);
552 i
= findppp(p
, 0x83); // Secondary DNS address (TBA, is it)
555 if (*(u32
*) (i
+ 2) != htonl(session
[s
].dns2
))
557 *(u32
*) (i
+ 2) = htonl(session
[s
].dns2
);
561 i
= findppp(p
, 3); // IP address
564 log(1, 0, s
, t
, "No IP in IPCP request\n");
565 STAT(tunnel_rx_errors
);
568 if (*(u32
*) (i
+ 2) != htonl(session
[s
].ip
))
570 *(u32
*) (i
+ 2) = htonl(session
[s
].ip
);
573 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPIPCP
);
575 log(2, 0, s
, t
, " Failed to send IPCP packet.\n");
578 tunnelsend(b
, l
+ (q
- b
), t
); // send it
583 // process IP packet received
585 // This MUST be called with at least 4 byte behind 'p'.
586 // (i.e. this routine writes to p[-4]).
587 void processipin(tunnelidt t
, sessionidt s
, u8
* p
, u16 l
)
592 CSTAT(call_processipin
);
594 log_hex(5, "IP", p
, l
);
596 ip
= ntohl(*(u32
*)(p
+ 12));
600 log(1, ip
, s
, t
, "IP packet too long %d\n", l
);
601 STAT(tunnel_rx_errors
);
605 // no spoof (do sessionbyip to handled statically routed subnets)
606 if (ip
!= session
[s
].ip
&& sessionbyip(htonl(ip
)) != s
)
608 log(5, ip
, s
, t
, "Dropping packet with spoofed IP %s\n", inet_toa(htonl(ip
)));
612 // Add on the tun header
614 *(u32
*)p
= htonl(0x00000800);
617 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) { // Are we throttled and a slave?
618 master_throttle_packet(session
[s
].tbf_in
, p
, l
); // Pass it to the master for handling.
622 session
[s
].cin
+= l
- 4;
623 session
[s
].total_cin
+= l
- 4;
624 sess_count
[s
].cin
+= l
- 4;
629 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
631 // Snooping this session, send it to ASIO
632 snoop_send_packet(p
, l
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
634 STAT(tun_tx_packets
);
635 INC_STAT(tun_tx_bytes
, l
);
637 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) { // Are we throttled and a master?? actually handle the throttled packets.
638 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
643 if (tun_write(p
, l
) < 0)
646 log(0, 0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
647 l
, strerror(errno
), tunfd
, p
);
653 // Helper routine for the TBF filters.
654 // Used to send queued data in from the user.
656 void send_ipin(sessionidt s
, u8
*buf
, int len
)
658 log_hex(5, "IP in throttled", buf
, len
);
659 if (write(tunfd
, buf
, len
) < 0)
662 log(0, 0, 0, 0, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
663 len
, strerror(errno
), tunfd
, buf
);
666 // Increment packet counters
667 session
[s
].cin
+= len
- 4;
668 session
[s
].total_cin
+= len
- 4;
669 sess_count
[s
].cin
+= len
- 4;
676 // Process LCP messages
677 void processccp(tunnelidt t
, sessionidt s
, u8
* p
, u16 l
)
680 CSTAT(call_processccp
);
682 log_hex(5, "CCP", p
, l
);
683 if (l
< 2 || (*p
!= ConfigReq
&& *p
!= TerminateReq
))
685 log(1, 0, s
, t
, "Unexpecetd CCP request code %d\n", *p
);
686 STAT(tunnel_rx_errors
);
697 *p
= ConfigAck
; // accept no compression
701 *p
= ConfigRej
; // reject
705 *p
= TerminateAck
; // close
706 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPCCP
);
708 log(1,0,0,0, "Failed to send CCP packet.\n");
711 tunnelsend(b
, l
+ (q
- b
), t
); // send it
715 // send a CHAP PP packet
716 void sendchap(tunnelidt t
, sessionidt s
)
719 u16 r
= session
[s
].radius
;
722 CSTAT(call_sendchap
);
726 log(1, 0, s
, t
, "No RADIUS to send challenge\n");
727 STAT(tunnel_tx_errors
);
730 log(1, 0, s
, t
, "Send CHAP challenge\n");
733 for (n
= 0; n
< 15; n
++)
734 radius
[r
].auth
[n
] = rand();
736 radius
[r
].chap
= 1; // CHAP not PAP
738 if (radius
[r
].state
!= RADIUSCHAP
)
740 radius
[r
].state
= RADIUSCHAP
;
741 radius
[r
].retry
= backoff(radius
[r
].try++);
742 if (radius
[r
].try > 5)
744 sessionshutdown(s
, "Timeout CHAP");
745 STAT(tunnel_tx_errors
);
748 q
= makeppp(b
, sizeof(b
), 0, 0, t
, s
, PPPCHAP
);
750 log(1, 0, s
, t
, "failed to send CHAP challenge.\n");
754 q
[1] = radius
[r
].id
; // ID
756 memcpy(q
+ 5, radius
[r
].auth
, 16); // challenge
757 strcpy(q
+ 21, hostname
); // our name
758 *(u16
*) (q
+ 2) = htons(strlen(hostname
) + 21); // length
759 tunnelsend(b
, strlen(hostname
) + 21 + (q
- b
), t
); // send it
762 // fill in a L2TP message with a PPP frame,
763 // copies existing PPP message and changes magic number if seen
764 // returns start of PPP frame
765 u8
*makeppp(u8
* b
, int size
, u8
* p
, int l
, tunnelidt t
, sessionidt s
, u16 mtype
)
769 return NULL
; // Need more space than this!!
771 *(u16
*) (b
+ 0) = htons(0x0002); // L2TP with no options
772 *(u16
*) (b
+ 2) = htons(tunnel
[t
].far
); // tunnel
773 *(u16
*) (b
+ 4) = htons(session
[s
].far
); // session
775 if (mtype
!= PPPLCP
&& !(session
[s
].l2tp_flags
& SESSIONACFC
))
777 *(u16
*) b
= htons(0xFF03); // HDLC header
780 if (mtype
< 0x100 && session
[s
].l2tp_flags
& SESSIONPFC
)
784 *(u16
*) b
= htons(mtype
);
789 log(3,0,0,0, "Would have overflowed the buffer in makeppp: size %d, len %d.\n", size
, l
);
790 return NULL
; // Run out of room to hold the packet!
797 // find a PPP option, returns point to option, or 0 if not found
798 u8
*findppp(u8
* b
, u8 mtype
)
800 u16 l
= ntohs(*(u16
*) (b
+ 2));
807 if (l
< b
[1] || !b
[1])
817 // Send initial LCP ConfigReq
818 void initlcp(tunnelidt t
, sessionidt s
)
820 char b
[500] = {0}, *q
;
822 q
= makeppp(b
, sizeof(b
), NULL
, 0, t
, s
, PPPLCP
);
824 log(1, 0, s
, t
, "Failed to send LCP ConfigReq.\n");
827 log(4, 0, s
, t
, "Sending LCP ConfigReq for PAP\n");
829 *(u8
*)(q
+ 1) = (time_now
% 255) + 1; // ID
830 *(u16
*)(q
+ 2) = htons(8); // Length
833 *(u16
*)(q
+ 6) = htons(0xC023); // PAP
834 tunnelsend(b
, 12 + 8, t
);