deab28410c2fa368cfa1dc2b48a530a87556d008
2 // $Id: ppp.c,v 1.5 2004-06-23 03:52:24 fred_nerk Exp $
10 #include "constants.h"
16 extern tunnelt
*tunnel
;
17 extern sessiont
*session
;
18 extern radiust
*radius
;
20 extern char hostname
[];
22 extern time_t time_now
;
23 extern struct configt
*config
;
25 // Process PAP messages
26 void processpap(tunnelidt t
, sessionidt s
, u8
* p
, u16 l
)
32 STAT(call_processpap
);
34 log_hex(5, "PAP", p
, l
);
37 log(1, 0, s
, t
, "Short PAP %u bytes", l
);
38 STAT(tunnel_rx_errors
);
43 log(1, 0, s
, t
, "Unexpected PAP code %d\n", *p
);
44 STAT(tunnel_rx_errors
);
47 if (ntohs(*(u16
*) (p
+ 2)) > l
)
49 log(1, 0, s
, t
, "Length mismatch PAP %d/%d\n", ntohs(*(u16
*) (p
+ 2)), l
);
50 STAT(tunnel_rx_errors
);
56 if (*b
&& *b
< sizeof(user
))
57 memcpy(user
, b
+ 1, *b
);
60 if (*b
&& *b
< sizeof(pass
))
61 memcpy(pass
, b
+ 1, *b
);
63 log(3, 0, s
, t
, "PAP login %s/%s\n", user
, pass
);
65 if (session
[s
].ip
|| !session
[s
].radius
)
67 // respond now, either no RADIUS available or already authenticated
70 u8
*p
= makeppp(b
, sizeof(b
), 0, 0, t
, s
, PPPPAP
);
71 if (!p
) { // Failed to make ppp header!
72 log(1,0,0,0, "Failed to make PPP header in process pap!\n");
78 *p
= 3; // cant authorise
80 *(u16
*) (p
+ 2) = htons(5); // length
81 p
[4] = 0; // no message
84 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
);
88 log(1, 0, s
, t
, "No radius session available to authenticate session...\n");
90 log(3, 0, s
, t
, "Fallback response to PAP (%s)\n", (session
[s
].ip
) ? "ACK" : "NAK");
91 tunnelsend(b
, 5 + (p
- b
), t
); // send it
94 { // set up RADIUS request
95 u16 r
= session
[s
].radius
;
97 // Run PRE_AUTH plugins
98 struct param_pre_auth packet
= { &tunnel
[t
], &session
[s
], strdup(user
), strdup(pass
), PPPPAP
, 1 };
99 run_plugins(PLUGIN_PRE_AUTH
, &packet
);
100 if (!packet
.continue_auth
)
102 log(3, 0, s
, t
, "A plugin rejected PRE_AUTH\n");
103 if (packet
.username
) free(packet
.username
);
104 if (packet
.password
) free(packet
.password
);
108 strncpy(session
[s
].user
, packet
.username
, sizeof(session
[s
].user
) - 1);
109 strncpy(radius
[r
].pass
, packet
.password
, sizeof(radius
[r
].pass
) - 1);
111 free(packet
.username
);
112 free(packet
.password
);
115 log(3, 0, s
, t
, "Sending login for %s/%s to radius\n", user
, pass
);
116 radiussend(r
, RADIUSAUTH
);
120 // Process CHAP messages
121 void processchap(tunnelidt t
, sessionidt s
, u8
* p
, u16 l
)
127 STAT(call_processchap
);
129 log_hex(5, "CHAP", p
, l
);
130 r
= session
[s
].radius
;
133 log(1, 0, s
, t
, "Unexpected CHAP message\n");
135 // FIXME: Need to drop the session here.
137 STAT(tunnel_rx_errors
);
142 log(1, 0, s
, t
, "Unexpected CHAP response code %d\n", *p
);
143 STAT(tunnel_rx_errors
);
146 if (p
[1] != radius
[r
].id
)
148 log(1, 0, s
, t
, "Wrong CHAP response ID %d (should be %d) (%d)\n", p
[1], radius
[r
].id
, r
);
149 STAT(tunnel_rx_errors
);
152 len
= ntohs(*(u16
*) (p
+ 2));
155 log(1, 0, s
, t
, "Bad CHAP length %d\n", len
);
156 STAT(tunnel_rx_errors
);
161 log(1, 0, s
, t
, "Bad CHAP response length %d\n", p
[4]);
162 STAT(tunnel_rx_errors
);
165 if (len
- 21 >= sizeof(session
[s
].user
))
167 log(1, 0, s
, t
, "CHAP user too long %d\n", len
- 21);
168 STAT(tunnel_rx_errors
);
172 // Run PRE_AUTH plugins
174 struct param_pre_auth packet
= { &tunnel
[t
], &session
[s
], NULL
, NULL
, PPPCHAP
, 1 };
176 packet
.username
= calloc(len
-20, 1);
177 packet
.password
= calloc(16, 1);
178 memcpy(packet
.username
, p
+ 21, len
- 21);
179 memcpy(packet
.password
, p
+ 5, 16);
181 run_plugins(PLUGIN_PRE_AUTH
, &packet
);
182 if (!packet
.continue_auth
)
184 log(3, 0, s
, t
, "A plugin rejected PRE_AUTH\n");
185 if (packet
.username
) free(packet
.username
);
186 if (packet
.password
) free(packet
.password
);
190 strncpy(session
[s
].user
, packet
.username
, sizeof(session
[s
].user
) - 1);
191 memcpy(radius
[r
].pass
, packet
.password
, 16);
193 free(packet
.username
);
194 free(packet
.password
);
198 log(3, 0, s
, t
, "CHAP login %s\n", session
[s
].user
);
199 radiussend(r
, RADIUSAUTH
);
202 char *ppp_lcp_types
[] = {
217 void dumplcp(u8
*p
, int l
)
219 signed int x
= l
- 4;
222 log_hex(5, "PPP LCP Packet", p
, l
);
223 log(4, 0, 0, 0, "PPP LCP Packet type %d (%s len %d)\n", *p
, ppp_lcp_types
[(int)*p
], ntohs( ((u16
*) p
)[1]) );
224 log(4, 0, 0, 0, "Length: %d\n", l
);
225 if (*p
!= ConfigReq
&& *p
!= ConfigRej
&& *p
!= ConfigAck
)
234 log(4, 0, 0, 0, " Option length is 0...\n");
239 log(4, 0, 0, 0, " Option type is 0...\n");
246 case 1: // Maximum-Receive-Unit
247 log(4, 0, 0, 0, " %s %d\n", lcp_types
[type
], ntohs(*(u16
*)(o
+ 2)));
249 case 3: // Authentication-Protocol
251 int proto
= ntohs(*(u16
*)(o
+ 2));
252 log(4, 0, 0, 0, " %s %s\n", lcp_types
[type
],
253 proto
== 0xC223 ? "CHAP" : "PAP");
256 case 4: // Quality-Protocol
258 u32 qp
= ntohl(*(u32
*)(o
+ 2));
259 log(4, 0, 0, 0, " %s %x\n", lcp_types
[type
], qp
);
262 case 5: // Magic-Number
264 u32 magicno
= ntohl(*(u32
*)(o
+ 2));
265 log(4, 0, 0, 0, " %s %x\n", lcp_types
[type
], magicno
);
268 case 7: // Protocol-Field-Compression
270 u32 pfc
= ntohl(*(u32
*)(o
+ 2));
271 log(4, 0, 0, 0, " %s %x\n", lcp_types
[type
], pfc
);
274 case 8: // Address-And-Control-Field-Compression
276 u32 afc
= ntohl(*(u32
*)(o
+ 2));
277 log(4, 0, 0, 0, " %s %x\n", lcp_types
[type
], afc
);
281 log(2, 0, 0, 0, " Unknown PPP LCP Option type %d\n", type
);
289 // Process LCP messages
290 void processlcp(tunnelidt t
, sessionidt s
, u8
* p
, u16 l
)
296 STAT(call_processlcp
);
298 log_hex(5, "LCP", p
, l
);
301 log(1, session
[s
].ip
, s
, t
, "Short LCP %d bytes", l
);
302 STAT(tunnel_rx_errors
);
307 log(3, session
[s
].ip
, s
, t
, "LCP: Discarding ConfigAck\n");
309 else if (*p
== ConfigReq
)
311 signed int x
= l
- 4;
314 log(3, session
[s
].ip
, s
, t
, "LCP: ConfigReq (%d bytes)...\n", l
);
321 if (length
== 0 || type
== 0) break;
324 case 1: // Maximum-Receive-Unit
325 session
[s
].mru
= ntohs(*(u16
*)(o
+ 2));
327 case 3: // Authentication-Protocol
329 int proto
= ntohs(*(u16
*)(o
+ 2));
332 log(2, session
[s
].ip
, s
, t
, " Remote end is trying to do CHAP. Rejecting it.\n");
336 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
338 log(2, session
[s
].ip
, s
, t
, " Failed to send packet.\n");
343 memcpy(q
, o
, length
);
344 *(u16
*)(q
+= 2) = htons(0xC023); // NAK -> Use PAP instead
349 case 5: // Magic-Number
351 // u32 magicno = ntohl(*(u32 *)(o + 2));
354 case 4: // Quality-Protocol
356 // u32 qp = ntohl(*(u32 *)(o + 2));
359 case 7: // Protocol-Field-Compression
361 // u32 pfc = ntohl(*(u32 *)(o + 2));
364 case 8: // Address-And-Control-Field-Compression
366 // u32 afc = ntohl(*(u32 *)(o + 2));
370 log(2, session
[s
].ip
, s
, t
, " Unknown PPP LCP Option type %d\n", type
);
379 // Send back a ConfigAck
380 log(3, session
[s
].ip
, s
, t
, "ConfigReq accepted, sending as Ack\n");
381 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
383 log(3, session
[s
].ip
, s
, t
, " failed to create packet.\n");
387 tunnelsend(b
, l
+ (q
- b
), t
);
391 // Already built a ConfigNak... send it
392 log(3, session
[s
].ip
, s
, t
, "Sending ConfigNak\n");
393 tunnelsend(b
, l
+ (q
- b
), t
);
395 log(3, session
[s
].ip
, s
, t
, "Sending ConfigReq, requesting PAP login\n");
396 q
= makeppp(b
, sizeof(b
), NULL
, 0, t
, s
, PPPLCP
);
400 *(u16
*)(q
+= 2) = htons(0xC023);
401 tunnelsend(b
, l
+ (q
- b
), t
);
404 else if (*p
== ConfigNak
)
406 log(1, session
[s
].ip
, s
, t
, "Remote end sent a ConfigNak. Ignoring\n");
410 else if (*p
== TerminateReq
)
412 *p
= TerminateAck
; // close
413 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
415 log(3, session
[s
].ip
, s
, t
, "Failed to create PPP packet in processlcp.\n");
418 log(3, session
[s
].ip
, s
, t
, "LCP: Received TerminateReq. Sending TerminateAck\n");
419 sessionshutdown(s
, "Remote end closed connection.");
420 tunnelsend(b
, l
+ (q
- b
), t
); // send it
422 else if (*p
== TerminateReq
)
424 sessionshutdown(s
, "Remote end closed connection.");
426 else if (*p
== EchoReq
)
428 *p
= EchoReply
; // reply
429 *(u32
*) (p
+ 4) = htonl(session
[s
].magic
); // our magic number
430 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPLCP
);
432 log(3, session
[s
].ip
, s
, t
, " failed to send EchoReply.\n");
435 log(5, session
[s
].ip
, s
, t
, "LCP: Received EchoReq. Sending EchoReply\n");
436 tunnelsend(b
, l
+ (q
- b
), t
); // send it
438 else if (*p
== EchoReply
)
440 // Ignore it, last_packet time is set earlier than this.
444 log(1, session
[s
].ip
, s
, t
, "Unexpected LCP code %d\n", *p
);
445 STAT(tunnel_rx_errors
);
450 // Process IPCP messages
451 void processipcp(tunnelidt t
, sessionidt s
, u8
* p
, u16 l
)
454 STAT(call_processipcp
);
456 log_hex(5, "IPCP", p
, l
);
459 log(1, 0, s
, t
, "Short IPCP %d bytes", l
);
460 STAT(tunnel_rx_errors
);
464 { // happy with our IPCP
465 u16 r
= session
[s
].radius
;
466 if ((!r
|| radius
[r
].state
== RADIUSIPCP
) && !session
[s
].walled_garden
) {
470 radiussend(r
, RADIUSSTART
); // send radius start, having got IPCP at last
472 session
[s
].flags
|= SF_IPCP_ACKED
;
478 log(1, 0, s
, t
, "Unexpected IPCP code %d\n", *p
);
479 STAT(tunnel_rx_errors
);
482 if (ntohs(*(u16
*) (p
+ 2)) > l
)
484 log(1, 0, s
, t
, "Length mismatch IPCP %d/%d\n", ntohs(*(u16
*) (p
+ 2)), l
);
485 STAT(tunnel_rx_errors
);
490 log(3, 0, s
, t
, "Waiting on radius reply\n");
491 return ; // have to wait on RADIUS reply
493 // form a config reply quoting the IP in the session
501 while (q
< i
&& q
[1])
503 if (*q
!= 0x81 && *q
!= 0x83 && *q
!= 3)
511 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPIPCP
);
513 log(2, 0, s
, t
, "Failed to send IPCP.\n");
518 while (p
< i
&& p
[1])
520 if (*p
!= 0x81 && *p
!= 0x83 && *p
!= 3)
522 log(2, 0, s
, t
, "IPCP reject %d\n", *p
);
523 memcpy(q
+ n
, p
, p
[1]);
528 *(u16
*) (q
+ 2) = htons(n
);
529 tunnelsend(b
, n
+ (q
- b
), t
); // send it
534 i
= findppp(p
, 0x81); // Primary DNS address
537 if (*(u32
*) (i
+ 2) != htonl(session
[s
].dns1
))
539 *(u32
*) (i
+ 2) = htonl(session
[s
].dns1
);
543 i
= findppp(p
, 0x83); // Secondary DNS address (TBA, is it)
546 if (*(u32
*) (i
+ 2) != htonl(session
[s
].dns2
))
548 *(u32
*) (i
+ 2) = htonl(session
[s
].dns2
);
552 i
= findppp(p
, 3); // IP address
555 log(1, 0, s
, t
, "No IP in IPCP request\n");
556 STAT(tunnel_rx_errors
);
559 if (*(u32
*) (i
+ 2) != htonl(session
[s
].ip
))
561 *(u32
*) (i
+ 2) = htonl(session
[s
].ip
);
564 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPIPCP
);
566 log(2, 0, s
, t
, " Failed to send IPCP packet.\n");
569 tunnelsend(b
, l
+ (q
- b
), t
); // send it
574 // process IP packet received
576 // This MUST be called with at least 4 byte behind 'p'.
577 // (i.e. this routine writes to p[-4]).
578 void processipin(tunnelidt t
, sessionidt s
, u8
* p
, u16 l
)
583 STAT(call_processipin
);
585 log_hex(5, "IP", p
, l
);
587 ip
= ntohl(*(u32
*)(p
+ 12));
591 log(1, ip
, s
, t
, "IP packet too long %d\n", l
);
592 STAT(tunnel_rx_errors
);
596 // no spoof (do sessionbyip to handled statically routed subnets)
597 if (ip
!= session
[s
].ip
&& sessionbyip(htonl(ip
)) != s
)
599 log(5, ip
, s
, t
, "Dropping packet with spoofed IP %s\n", inet_toa(htonl(ip
)));
603 // Add on the tun header
605 *(u32
*)p
= htonl(0x00000800);
608 if (session
[s
].tbf_in
&& !config
->cluster_iam_master
) { // Are we throttled and a slave?
609 master_throttle_packet(session
[s
].tbf_in
, p
, l
); // Pass it to the master for handling.
613 session
[s
].cin
+= l
- 4;
614 session
[s
].total_cin
+= l
- 4;
615 sess_count
[s
].cin
+= l
- 4;
620 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
622 // Snooping this session, send it to ASIO
623 snoop_send_packet(p
, l
, session
[s
].snoop_ip
, session
[s
].snoop_port
);
625 STAT(tap_tx_packets
);
626 INC_STAT(tap_tx_bytes
, l
);
628 if (session
[s
].tbf_in
&& config
->cluster_iam_master
) { // Are we throttled and a master?? actually handle the throttled packets.
629 tbf_queue_packet(session
[s
].tbf_in
, p
, l
);
634 if (tun_write(p
, l
) < 0)
637 log(0, 0, s
, t
, "Error writing %d bytes to TAP device: %s (tapfd=%d, p=%p)\n",
638 l
, strerror(errno
), tapfd
, p
);
644 // Helper routine for the TBF filters.
645 // Used to send queued data in from the user.
647 void send_ipin(sessionidt s
, u8
*buf
, int len
)
649 log_hex(5, "IP in throttled", buf
, len
);
650 if (write(tapfd
, buf
, len
) < 0)
653 log(0, 0, 0, 0, "Error writing %d bytes to TAP device: %s (tapfd=%d, p=%p)\n",
654 len
, strerror(errno
), tapfd
, buf
);
657 // Increment packet counters
658 session
[s
].cin
+= len
- 4;
659 session
[s
].total_cin
+= len
- 4;
660 sess_count
[s
].cin
+= len
- 4;
667 // Process LCP messages
668 void processccp(tunnelidt t
, sessionidt s
, u8
* p
, u16 l
)
671 STAT(call_processccp
);
673 log_hex(5, "CCP", p
, l
);
674 if (l
< 2 || (*p
!= ConfigReq
&& *p
!= TerminateReq
))
676 log(1, 0, s
, t
, "Unexpecetd CCP request code %d\n", *p
);
677 STAT(tunnel_rx_errors
);
688 *p
= ConfigAck
; // accept no compression
692 *p
= ConfigRej
; // reject
696 *p
= TerminateAck
; // close
697 q
= makeppp(b
, sizeof(b
), p
, l
, t
, s
, PPPCCP
);
699 log(1,0,0,0, "Failed to send CCP packet.\n");
702 tunnelsend(b
, l
+ (q
- b
), t
); // send it
706 // send a CHAP PP packet
707 void sendchap(tunnelidt t
, sessionidt s
)
710 u16 r
= session
[s
].radius
;
717 log(1, 0, s
, t
, "No RADIUS to send challenge\n");
718 STAT(tunnel_tx_errors
);
721 log(1, 0, s
, t
, "Send CHAP challenge\n");
724 for (n
= 0; n
< 15; n
++)
725 radius
[r
].auth
[n
] = rand();
727 radius
[r
].chap
= 1; // CHAP not PAP
729 if (radius
[r
].state
!= RADIUSCHAP
)
731 radius
[r
].state
= RADIUSCHAP
;
732 radius
[r
].retry
= backoff(radius
[r
].try++);
733 if (radius
[r
].try > 5)
735 sessionshutdown(s
, "Timeout CHAP");
736 STAT(tunnel_tx_errors
);
739 q
= makeppp(b
, sizeof(b
), 0, 0, t
, s
, PPPCHAP
);
741 log(1, 0, s
, t
, "failed to send CHAP challenge.\n");
745 q
[1] = radius
[r
].id
; // ID
747 memcpy(q
+ 5, radius
[r
].auth
, 16); // challenge
748 strcpy(q
+ 21, hostname
); // our name
749 *(u16
*) (q
+ 2) = htons(strlen(hostname
) + 21); // length
750 tunnelsend(b
, strlen(hostname
) + 21 + (q
- b
), t
); // send it
753 // fill in a L2TP message with a PPP frame,
754 // copies existing PPP message and changes magic number if seen
755 // returns start of PPP frame
756 u8
*makeppp(u8
* b
, int size
, u8
* p
, int l
, tunnelidt t
, sessionidt s
, u16 mtype
)
760 return NULL
; // Need more space than this!!
762 *(u16
*) (b
+ 0) = htons(0x0002); // L2TP with no options
763 *(u16
*) (b
+ 2) = htons(tunnel
[t
].far
); // tunnel
764 *(u16
*) (b
+ 4) = htons(session
[s
].far
); // session
766 if (mtype
!= PPPLCP
&& !(session
[s
].l2tp_flags
& SESSIONACFC
))
768 *(u16
*) b
= htons(0xFF03); // HDLC header
771 if (mtype
< 0x100 && session
[s
].l2tp_flags
& SESSIONPFC
)
775 *(u16
*) b
= htons(mtype
);
780 log(3,0,0,0, "Would have overflowed the buffer in makeppp: size %d, len %d.\n", size
, l
);
781 return NULL
; // Run out of room to hold the packet!
788 // find a PPP option, returns point to option, or 0 if not found
789 u8
*findppp(u8
* b
, u8 mtype
)
791 u16 l
= ntohs(*(u16
*) (b
+ 2));
798 if (l
< b
[1] || !b
[1])
808 // Send initial LCP ConfigReq
809 void initlcp(tunnelidt t
, sessionidt s
)
811 char b
[500] = {0}, *q
;
813 q
= makeppp(b
, sizeof(b
), NULL
, 0, t
, s
, PPPLCP
);
815 log(1, 0, s
, t
, "Failed to send LCP ConfigReq.\n");
818 log(4, 0, s
, t
, "Sending LCP ConfigReq for PAP\n");
820 *(u8
*)(q
+ 1) = (time_now
% 255) + 1; // ID
821 *(u16
*)(q
+ 2) = htons(8); // Length
824 *(u16
*)(q
+ 6) = htons(0xC023); // PAP
825 tunnelsend(b
, 12 + 8, t
);