8e93f5d90a12e16439813aa3d397471d4a41a73e
3 char const *cvs_id_ppp
= "$Id: ppp.c,v 1.8 2004-07-11 07:57:35 bodea 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 0...\n");
240 log(4, 0, 0, 0, " Option type is 0...\n");
247 case 1: // Maximum-Receive-Unit
248 log(4, 0, 0, 0, " %s %d\n", lcp_types
[type
], ntohs(*(u16
*)(o
+ 2)));
250 case 3: // Authentication-Protocol
252 int proto
= ntohs(*(u16
*)(o
+ 2));
253 log(4, 0, 0, 0, " %s %s\n", lcp_types
[type
],
254 proto
== 0xC223 ? "CHAP" : "PAP");
257 case 4: // Quality-Protocol
259 u32 qp
= ntohl(*(u32
*)(o
+ 2));
260 log(4, 0, 0, 0, " %s %x\n", lcp_types
[type
], qp
);
263 case 5: // Magic-Number
265 u32 magicno
= ntohl(*(u32
*)(o
+ 2));
266 log(4, 0, 0, 0, " %s %x\n", lcp_types
[type
], magicno
);
269 case 7: // Protocol-Field-Compression
271 u32 pfc
= ntohl(*(u32
*)(o
+ 2));
272 log(4, 0, 0, 0, " %s %x\n", lcp_types
[type
], pfc
);
275 case 8: // Address-And-Control-Field-Compression
277 u32 afc
= ntohl(*(u32
*)(o
+ 2));
278 log(4, 0, 0, 0, " %s %x\n", lcp_types
[type
], afc
);
282 log(2, 0, 0, 0, " Unknown PPP LCP Option type %d\n", type
);
290 // Process LCP messages
291 void processlcp(tunnelidt t
, sessionidt s
, u8
* p
, u16 l
)
297 CSTAT(call_processlcp
);
299 log_hex(5, "LCP", p
, l
);
302 log(1, session
[s
].ip
, s
, t
, "Short LCP %d bytes\n", l
);
303 STAT(tunnel_rx_errors
);
308 log(3, session
[s
].ip
, s
, t
, "LCP: Discarding ConfigAck\n");
310 else if (*p
== ConfigReq
)
312 signed int x
= l
- 4;
315 log(3, session
[s
].ip
, s
, t
, "LCP: ConfigReq (%d bytes)...\n", l
);
322 if (length
== 0 || type
== 0) break;
325 case 1: // Maximum-Receive-Unit
326 session
[s
].mru
= ntohs(*(u16
*)(o
+ 2));
328 case 3: // Authentication-Protocol
330 int proto
= ntohs(*(u16
*)(o
+ 2));
333 log(2, session
[s
].ip
, s
, t
, " Remote end is trying to do CHAP. Rejecting it.\n");
337 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
339 log(2, session
[s
].ip
, s
, t
, " Failed to send packet.\n");
344 memcpy(q
, o
, length
);
345 *(u16
*)(q
+= 2) = htons(0xC023); // NAK -> Use PAP instead
350 case 5: // Magic-Number
352 // u32 magicno = ntohl(*(u32 *)(o + 2));
355 case 4: // Quality-Protocol
357 // u32 qp = ntohl(*(u32 *)(o + 2));
360 case 7: // Protocol-Field-Compression
362 // u32 pfc = ntohl(*(u32 *)(o + 2));
365 case 8: // Address-And-Control-Field-Compression
367 // u32 afc = ntohl(*(u32 *)(o + 2));
371 log(2, session
[s
].ip
, s
, t
, " Unknown PPP LCP Option type %d\n", type
);
380 // Send back a ConfigAck
381 log(3, session
[s
].ip
, s
, t
, "ConfigReq accepted, sending as Ack\n");
382 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
384 log(3, session
[s
].ip
, s
, t
, " failed to create packet.\n");
388 tunnelsend(b
, l
+ (q
- b
), t
);
392 // Already built a ConfigNak... send it
393 log(3, session
[s
].ip
, s
, t
, "Sending ConfigNak\n");
394 tunnelsend(b
, l
+ (q
- b
), t
);
396 log(3, session
[s
].ip
, s
, t
, "Sending ConfigReq, requesting PAP login\n");
397 q
= makeppp(b
, sizeof(b
), NULL
, 0, t
, s
, PPPLCP
);
401 *(u16
*)(q
+= 2) = htons(0xC023);
402 tunnelsend(b
, l
+ (q
- b
), t
);
405 else if (*p
== ConfigNak
)
407 log(1, session
[s
].ip
, s
, t
, "Remote end sent a ConfigNak. Ignoring\n");
411 else if (*p
== TerminateReq
)
413 *p
= TerminateAck
; // close
414 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
416 log(3, session
[s
].ip
, s
, t
, "Failed to create PPP packet in processlcp.\n");
419 log(3, session
[s
].ip
, s
, t
, "LCP: Received TerminateReq. Sending TerminateAck\n");
420 sessionshutdown(s
, "Remote end closed connection.");
421 tunnelsend(b
, l
+ (q
- b
), t
); // send it
423 else if (*p
== TerminateAck
)
425 sessionshutdown(s
, "Connection closed.");
427 else if (*p
== EchoReq
)
429 *p
= EchoReply
; // reply
430 *(u32
*) (p
+ 4) = htonl(session
[s
].magic
); // our magic number
431 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
433 log(3, session
[s
].ip
, s
, t
, " failed to send EchoReply.\n");
436 log(5, session
[s
].ip
, s
, t
, "LCP: Received EchoReq. Sending EchoReply\n");
437 tunnelsend(b
, l
+ (q
- b
), t
); // send it
439 else if (*p
== EchoReply
)
441 // Ignore it, last_packet time is set earlier than this.
445 log(1, session
[s
].ip
, s
, t
, "Unexpected LCP code %d\n", *p
);
446 STAT(tunnel_rx_errors
);
451 // Process IPCP messages
452 void processipcp(tunnelidt t
, sessionidt s
, u8
* p
, u16 l
)
455 CSTAT(call_processipcp
);
457 log_hex(5, "IPCP", p
, l
);
460 log(1, 0, s
, t
, "Short IPCP %d bytes\n", l
);
461 STAT(tunnel_rx_errors
);
465 { // happy with our IPCP
466 u16 r
= session
[s
].radius
;
467 if ((!r
|| radius
[r
].state
== RADIUSIPCP
) && !session
[s
].walled_garden
) {
471 radiussend(r
, RADIUSSTART
); // send radius start, having got IPCP at last
473 session
[s
].flags
|= SF_IPCP_ACKED
;
479 log(1, 0, s
, t
, "Unexpected IPCP code %d\n", *p
);
480 STAT(tunnel_rx_errors
);
483 if (ntohs(*(u16
*) (p
+ 2)) > l
)
485 log(1, 0, s
, t
, "Length mismatch IPCP %d/%d\n", ntohs(*(u16
*) (p
+ 2)), l
);
486 STAT(tunnel_rx_errors
);
491 log(3, 0, s
, t
, "Waiting on radius reply\n");
492 return ; // have to wait on RADIUS reply
494 // form a config reply quoting the IP in the session
502 while (q
< i
&& q
[1])
504 if (*q
!= 0x81 && *q
!= 0x83 && *q
!= 3)
512 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPIPCP
);
514 log(2, 0, s
, t
, "Failed to send IPCP.\n");
519 while (p
< i
&& p
[1])
521 if (*p
!= 0x81 && *p
!= 0x83 && *p
!= 3)
523 log(2, 0, s
, t
, "IPCP reject %d\n", *p
);
524 memcpy(q
+ n
, p
, p
[1]);
529 *(u16
*) (q
+ 2) = htons(n
);
530 tunnelsend(b
, n
+ (q
- b
), t
); // send it
535 i
= findppp(p
, 0x81); // Primary DNS address
538 if (*(u32
*) (i
+ 2) != htonl(session
[s
].dns1
))
540 *(u32
*) (i
+ 2) = htonl(session
[s
].dns1
);
544 i
= findppp(p
, 0x83); // Secondary DNS address (TBA, is it)
547 if (*(u32
*) (i
+ 2) != htonl(session
[s
].dns2
))
549 *(u32
*) (i
+ 2) = htonl(session
[s
].dns2
);
553 i
= findppp(p
, 3); // IP address
556 log(1, 0, s
, t
, "No IP in IPCP request\n");
557 STAT(tunnel_rx_errors
);
560 if (*(u32
*) (i
+ 2) != htonl(session
[s
].ip
))
562 *(u32
*) (i
+ 2) = htonl(session
[s
].ip
);
565 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPIPCP
);
567 log(2, 0, s
, t
, " Failed to send IPCP packet.\n");
570 tunnelsend(b
, l
+ (q
- b
), t
); // send it
575 // process IP packet received
577 // This MUST be called with at least 4 byte behind 'p'.
578 // (i.e. this routine writes to p[-4]).
579 void processipin(tunnelidt t
, sessionidt s
, u8
* p
, u16 l
)
584 CSTAT(call_processipin
);
586 log_hex(5, "IP", p
, l
);
588 ip
= ntohl(*(u32
*)(p
+ 12));
592 log(1, ip
, s
, t
, "IP packet too long %d\n", l
);
593 STAT(tunnel_rx_errors
);
597 // no spoof (do sessionbyip to handled statically routed subnets)
598 if (ip
!= session
[s
].ip
&& sessionbyip(htonl(ip
)) != s
)
600 log(5, ip
, s
, t
, "Dropping packet with spoofed IP %s\n", inet_toa(htonl(ip
)));
604 // Add on the tun header
606 *(u32
*)p
= htonl(0x00000800);
609 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) { // Are we throttled and a slave?
610 master_throttle_packet(session
[s
].tbf_in
, p
, l
); // Pass it to the master for handling.
614 session
[s
].cin
+= l
- 4;
615 session
[s
].total_cin
+= l
- 4;
616 sess_count
[s
].cin
+= l
- 4;
621 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
623 // Snooping this session, send it to ASIO
624 snoop_send_packet(p
, l
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
626 STAT(tun_tx_packets
);
627 INC_STAT(tun_tx_bytes
, l
);
629 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) { // Are we throttled and a master?? actually handle the throttled packets.
630 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
635 if (tun_write(p
, l
) < 0)
638 log(0, 0, s
, t
, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
639 l
, strerror(errno
), tunfd
, p
);
645 // Helper routine for the TBF filters.
646 // Used to send queued data in from the user.
648 void send_ipin(sessionidt s
, u8
*buf
, int len
)
650 log_hex(5, "IP in throttled", buf
, len
);
651 if (write(tunfd
, buf
, len
) < 0)
654 log(0, 0, 0, 0, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
655 len
, strerror(errno
), tunfd
, buf
);
658 // Increment packet counters
659 session
[s
].cin
+= len
- 4;
660 session
[s
].total_cin
+= len
- 4;
661 sess_count
[s
].cin
+= len
- 4;
668 // Process LCP messages
669 void processccp(tunnelidt t
, sessionidt s
, u8
* p
, u16 l
)
672 CSTAT(call_processccp
);
674 log_hex(5, "CCP", p
, l
);
675 if (l
< 2 || (*p
!= ConfigReq
&& *p
!= TerminateReq
))
677 log(1, 0, s
, t
, "Unexpecetd CCP request code %d\n", *p
);
678 STAT(tunnel_rx_errors
);
689 *p
= ConfigAck
; // accept no compression
693 *p
= ConfigRej
; // reject
697 *p
= TerminateAck
; // close
698 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPCCP
);
700 log(1,0,0,0, "Failed to send CCP packet.\n");
703 tunnelsend(b
, l
+ (q
- b
), t
); // send it
707 // send a CHAP PP packet
708 void sendchap(tunnelidt t
, sessionidt s
)
711 u16 r
= session
[s
].radius
;
714 CSTAT(call_sendchap
);
718 log(1, 0, s
, t
, "No RADIUS to send challenge\n");
719 STAT(tunnel_tx_errors
);
722 log(1, 0, s
, t
, "Send CHAP challenge\n");
725 for (n
= 0; n
< 15; n
++)
726 radius
[r
].auth
[n
] = rand();
728 radius
[r
].chap
= 1; // CHAP not PAP
730 if (radius
[r
].state
!= RADIUSCHAP
)
732 radius
[r
].state
= RADIUSCHAP
;
733 radius
[r
].retry
= backoff(radius
[r
].try++);
734 if (radius
[r
].try > 5)
736 sessionshutdown(s
, "Timeout CHAP");
737 STAT(tunnel_tx_errors
);
740 q
= makeppp(b
, sizeof(b
), 0, 0, t
, s
, PPPCHAP
);
742 log(1, 0, s
, t
, "failed to send CHAP challenge.\n");
746 q
[1] = radius
[r
].id
; // ID
748 memcpy(q
+ 5, radius
[r
].auth
, 16); // challenge
749 strcpy(q
+ 21, hostname
); // our name
750 *(u16
*) (q
+ 2) = htons(strlen(hostname
) + 21); // length
751 tunnelsend(b
, strlen(hostname
) + 21 + (q
- b
), t
); // send it
754 // fill in a L2TP message with a PPP frame,
755 // copies existing PPP message and changes magic number if seen
756 // returns start of PPP frame
757 u8
*makeppp(u8
* b
, int size
, u8
* p
, int l
, tunnelidt t
, sessionidt s
, u16 mtype
)
761 return NULL
; // Need more space than this!!
763 *(u16
*) (b
+ 0) = htons(0x0002); // L2TP with no options
764 *(u16
*) (b
+ 2) = htons(tunnel
[t
].far
); // tunnel
765 *(u16
*) (b
+ 4) = htons(session
[s
].far
); // session
767 if (mtype
!= PPPLCP
&& !(session
[s
].l2tp_flags
& SESSIONACFC
))
769 *(u16
*) b
= htons(0xFF03); // HDLC header
772 if (mtype
< 0x100 && session
[s
].l2tp_flags
& SESSIONPFC
)
776 *(u16
*) b
= htons(mtype
);
781 log(3,0,0,0, "Would have overflowed the buffer in makeppp: size %d, len %d.\n", size
, l
);
782 return NULL
; // Run out of room to hold the packet!
789 // find a PPP option, returns point to option, or 0 if not found
790 u8
*findppp(u8
* b
, u8 mtype
)
792 u16 l
= ntohs(*(u16
*) (b
+ 2));
799 if (l
< b
[1] || !b
[1])
809 // Send initial LCP ConfigReq
810 void initlcp(tunnelidt t
, sessionidt s
)
812 char b
[500] = {0}, *q
;
814 q
= makeppp(b
, sizeof(b
), NULL
, 0, t
, s
, PPPLCP
);
816 log(1, 0, s
, t
, "Failed to send LCP ConfigReq.\n");
819 log(4, 0, s
, t
, "Sending LCP ConfigReq for PAP\n");
821 *(u8
*)(q
+ 1) = (time_now
% 255) + 1; // ID
822 *(u16
*)(q
+ 2) = htons(8); // Length
825 *(u16
*)(q
+ 6) = htons(0xC023); // PAP
826 tunnelsend(b
, 12 + 8, t
);