]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ip/Intercept.cc
2 * DEBUG: section 89 NAT / IP Interception
3 * AUTHOR: Robert Collins
4 * AUTHOR: Amos Jeffries
6 * SQUID Web Proxy Cache http://www.squid-cache.org/
7 * ----------------------------------------------------------
9 * Squid is the result of efforts by numerous individuals from
10 * the Internet community; see the CONTRIBUTORS file for full
11 * details. Many organizations have provided support for Squid's
12 * development; see the SPONSORS file for full details. Squid is
13 * Copyrighted (C) 2001 by the Regents of the University of
14 * California; see the COPYRIGHT file for full details. Squid
15 * incorporates software developed and/or copyrighted by other
16 * sources; see the CREDITS file for full details.
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
34 #include "comm/Connection.h"
36 #include "ip/Intercept.h"
37 #include "src/tools.h"
42 #include <sys/ioctl.h>
44 #if HAVE_NETINET_TCP_H
45 #include <netinet/tcp.h>
52 #elif HAVE_NETINET_IPL_H
53 #include <netinet/ipl.h>
55 #if HAVE_IP_FIL_COMPAT_H
56 #include <ip_fil_compat.h>
57 #elif HAVE_NETINET_IP_FIL_COMPAT_H
58 #include <netinet/ip_fil_compat.h>
59 #elif HAVE_IP_COMPAT_H
60 #include <ip_compat.h>
61 #elif HAVE_NETINET_IP_COMPAT_H
62 #include <netinet/ip_compat.h>
66 #elif HAVE_NETINET_IP_FIL_H
67 #include <netinet/ip_fil.h>
71 #elif HAVE_NETINET_IP_NAT_H
72 #include <netinet/ip_nat.h>
78 #endif /* IPF_TRANSPARENT required headers */
81 #include <sys/socket.h>
82 #include <sys/ioctl.h>
83 #include <sys/fcntl.h>
85 #include <netinet/in.h>
86 #if HAVE_NET_PF_PFVAR_H
87 #include <net/pf/pfvar.h>
88 #endif /* HAVE_NET_PF_PFVAR_H */
90 #include <net/pfvar.h>
91 #endif /* HAVE_NET_PFVAR_H */
92 #endif /* PF_TRANSPARENT required headers */
96 /* must be before including netfilter_ipv4.h */
100 #include <linux/netfilter_ipv4.h>
101 #if HAVE_LINUX_NETFILTER_IPV6_IP6_TABLES_H
102 /* 2013-07-01: Pablo the Netfilter maintainer is rejecting patches
103 * which will enable C++ compilers to build the Netfilter public headers.
104 * We can auto-detect its presence and attempt to use in case he ever
105 * changes his mind or things get cleaned up some other way.
106 * But until then are usually forced to hard-code the getsockopt() code
107 * for IPv6 NAT lookups.
109 #include <linux/netfilter_ipv6/ip6_tables.h>
111 #if !defined(IP6T_SO_ORIGINAL_DST)
112 #define IP6T_SO_ORIGINAL_DST 80 // stolen with prejudice from the above file.
114 #endif /* LINUX_NETFILTER required headers */
116 // single global instance for access by other components.
117 Ip::Intercept
Ip::Interceptor
;
120 Ip::Intercept::StopTransparency(const char *str
)
122 if (transparentActive_
) {
123 debugs(89, DBG_IMPORTANT
, "Stopping full transparency: " << str
);
124 transparentActive_
= 0;
129 Ip::Intercept::StopInterception(const char *str
)
131 if (interceptActive_
) {
132 debugs(89, DBG_IMPORTANT
, "Stopping IP interception: " << str
);
133 interceptActive_
= 0;
138 Ip::Intercept::NetfilterInterception(const Comm::ConnectionPointer
&newConn
, int silent
)
141 struct sockaddr_storage lookup
;
142 socklen_t len
= newConn
->local
.isIPv6() ? sizeof(sockaddr_in6
) : sizeof(sockaddr_in
);
143 newConn
->local
.getSockAddr(lookup
, AF_UNSPEC
);
146 * Try NAT lookup for REDIRECT or DNAT targets. */
147 if ( getsockopt(newConn
->fd
,
148 newConn
->local
.isIPv6() ? IPPROTO_IPV6
: IPPROTO_IP
,
149 newConn
->local
.isIPv6() ? IP6T_SO_ORIGINAL_DST
: SO_ORIGINAL_DST
,
153 debugs(89, DBG_IMPORTANT
, "ERROR: NF getsockopt(ORIGINAL_DST) failed on " << newConn
<< ": " << xstrerror());
154 lastReported_
= squid_curtime
;
156 debugs(89, 9, "address: " << newConn
);
159 newConn
->local
= lookup
;
160 debugs(89, 5, "address NAT: " << newConn
);
168 Ip::Intercept::TproxyTransparent(const Comm::ConnectionPointer
&newConn
, int silent
)
170 #if (LINUX_NETFILTER && defined(IP_TRANSPARENT)) || \
171 (PF_TRANSPARENT && defined(SO_BINDANY)) || \
172 (IPFW_TRANSPARENT && defined(IP_BINDANY))
174 /* Trust the user configured properly. If not no harm done.
175 * We will simply attempt a bind outgoing on our own IP.
177 newConn
->remote
.port(0); // allow random outgoing port to prevent address clashes
178 debugs(89, 5, HERE
<< "address TPROXY: " << newConn
);
186 Ip::Intercept::IpfwInterception(const Comm::ConnectionPointer
&newConn
, int silent
)
189 /* The getsockname() call performed already provided the TCP packet details.
190 * There is no way to identify whether they came from NAT or not.
191 * Trust the user configured properly.
193 debugs(89, 5, HERE
<< "address NAT: " << newConn
);
201 Ip::Intercept::IpfInterception(const Comm::ConnectionPointer
&newConn
, int silent
)
203 #if IPF_TRANSPARENT /* --enable-ipf-transparent */
205 struct natlookup natLookup
;
206 static int natfd
= -1;
209 // all fields must be set to 0
210 memset(&natLookup
, 0, sizeof(natLookup
));
211 // for NAT lookup set local and remote IP:port's
212 natLookup
.nl_inport
= htons(newConn
->local
.port());
213 newConn
->local
.getInAddr(natLookup
.nl_inip
);
214 natLookup
.nl_outport
= htons(newConn
->remote
.port());
215 newConn
->remote
.getInAddr(natLookup
.nl_outip
);
216 // ... and the TCP flag
217 natLookup
.nl_flags
= IPN_TCP
;
223 natfd
= open(IPNAT_NAME
, O_RDONLY
, 0);
225 natfd
= open(IPL_NAT
, O_RDONLY
, 0);
234 debugs(89, DBG_IMPORTANT
, "IPF (IPFilter) NAT open failed: " << xstrerror());
235 lastReported_
= squid_curtime
;
240 #if defined(IPFILTER_VERSION) && (IPFILTER_VERSION >= 4000027)
242 memset(&obj
, 0, sizeof(obj
));
243 obj
.ipfo_rev
= IPFILTER_VERSION
;
244 obj
.ipfo_size
= sizeof(natLookup
);
245 obj
.ipfo_ptr
= &natLookup
;
246 obj
.ipfo_type
= IPFOBJ_NATLOOKUP
;
248 x
= ioctl(natfd
, SIOCGNATL
, &obj
);
251 * IP-Filter changed the type for SIOCGNATL between
252 * 3.3 and 3.4. It also changed the cmd value for
253 * SIOCGNATL, so at least we can detect it. We could
254 * put something in configure and use ifdefs here, but
255 * this seems simpler.
257 static int siocgnatl_cmd
= SIOCGNATL
& 0xff;
258 if (63 == siocgnatl_cmd
) {
259 struct natlookup
*nlp
= &natLookup
;
260 x
= ioctl(natfd
, SIOCGNATL
, &nlp
);
262 x
= ioctl(natfd
, SIOCGNATL
, &natLookup
);
267 if (errno
!= ESRCH
) {
269 debugs(89, DBG_IMPORTANT
, "IPF (IPFilter) NAT lookup failed: ioctl(SIOCGNATL) (v=" << IPFILTER_VERSION
<< "): " << xstrerror());
270 lastReported_
= squid_curtime
;
277 debugs(89, 9, HERE
<< "address: " << newConn
);
280 newConn
->local
= natLookup
.nl_realip
;
281 newConn
->local
.port(ntohs(natLookup
.nl_realport
));
282 debugs(89, 5, HERE
<< "address NAT: " << newConn
);
286 #endif /* --enable-ipf-transparent */
291 Ip::Intercept::PfInterception(const Comm::ConnectionPointer
&newConn
, int silent
)
293 #if PF_TRANSPARENT /* --enable-pf-transparent */
296 /* On recent PF versions the getsockname() call performed already provided
297 * the required TCP packet details.
298 * There is no way to identify whether they came from NAT or not.
300 * Trust the user configured properly.
302 debugs(89, 5, HERE
<< "address NAT divert-to: " << newConn
);
305 #else /* USE_NAT_DEVPF / --with-nat-devpf */
307 struct pfioc_natlook nl
;
308 static int pffd
= -1;
311 pffd
= open("/dev/pf", O_RDONLY
);
315 debugs(89, DBG_IMPORTANT
, HERE
<< "PF open failed: " << xstrerror());
316 lastReported_
= squid_curtime
;
321 memset(&nl
, 0, sizeof(struct pfioc_natlook
));
322 newConn
->remote
.getInAddr(nl
.saddr
.v4
);
323 nl
.sport
= htons(newConn
->remote
.port());
325 newConn
->local
.getInAddr(nl
.daddr
.v4
);
326 nl
.dport
= htons(newConn
->local
.port());
329 nl
.proto
= IPPROTO_TCP
;
330 nl
.direction
= PF_OUT
;
332 if (ioctl(pffd
, DIOCNATLOOK
, &nl
)) {
333 if (errno
!= ENOENT
) {
335 debugs(89, DBG_IMPORTANT
, HERE
<< "PF lookup failed: ioctl(DIOCNATLOOK)");
336 lastReported_
= squid_curtime
;
341 debugs(89, 9, HERE
<< "address: " << newConn
);
344 newConn
->local
= nl
.rdaddr
.v4
;
345 newConn
->local
.port(ntohs(nl
.rdport
));
346 debugs(89, 5, HERE
<< "address NAT: " << newConn
);
349 #endif /* --with-nat-devpf */
350 #endif /* --enable-pf-transparent */
355 Ip::Intercept::Lookup(const Comm::ConnectionPointer
&newConn
, const Comm::ConnectionPointer
&listenConn
)
357 /* --enable-linux-netfilter */
358 /* --enable-ipfw-transparent */
359 /* --enable-ipf-transparent */
360 /* --enable-pf-transparent */
361 #if IPF_TRANSPARENT || LINUX_NETFILTER || IPFW_TRANSPARENT || PF_TRANSPARENT
364 // Crop interception errors down to one per minute.
365 int silent
= (squid_curtime
- lastReported_
> 60 ? 0 : 1);
367 // Show all interception errors.
371 debugs(89, 5, HERE
<< "address BEGIN: me/client= " << newConn
->local
<< ", destination/me= " << newConn
->remote
);
373 newConn
->flags
|= (listenConn
->flags
& (COMM_TRANSPARENT
|COMM_INTERCEPTION
));
375 /* NP: try TPROXY first, its much quieter than NAT when non-matching */
376 if (transparentActive_
&& listenConn
->flags
&COMM_TRANSPARENT
) {
377 if (TproxyTransparent(newConn
, silent
)) return true;
380 if (interceptActive_
&& listenConn
->flags
&COMM_INTERCEPTION
) {
381 /* NAT methods that use sock-opts to return client address */
382 if (NetfilterInterception(newConn
, silent
)) return true;
383 if (IpfwInterception(newConn
, silent
)) return true;
385 /* NAT methods that use ioctl to return client address AND destination address */
386 if (PfInterception(newConn
, silent
)) return true;
387 if (IpfInterception(newConn
, silent
)) return true;
390 #else /* none of the transparent options configured */
391 debugs(89, DBG_IMPORTANT
, "WARNING: transparent proxying not supported");
398 Ip::Intercept::ProbeForTproxy(Ip::Address
&test
)
400 bool doneSuid
= false;
402 #if _SQUID_LINUX_ && defined(IP_TRANSPARENT) // Linux
403 # define soLevel SOL_IP
404 # define soFlag IP_TRANSPARENT
406 #elif defined(SO_BINDANY) // OpenBSD 4.7+ and NetBSD with PF
407 # define soLevel SOL_SOCKET
408 # define soFlag SO_BINDANY
412 #elif defined(IP_BINDANY) // FreeBSD with IPFW
413 # define soLevel IPPROTO_IP
414 # define soFlag IP_BINDANY
420 #if defined(soLevel) && defined(soFlag)
422 debugs(3, 3, "Detect TPROXY support on port " << test
);
427 /* Probe to see if the Kernel TPROXY support is IPv6-enabled */
429 debugs(3, 3, "...Probing for IPv6 TPROXY support.");
431 struct sockaddr_in6 tmp_ip6
;
432 Ip::Address tmp
= "::2";
434 tmp
.getSockAddr(tmp_ip6
);
436 if ( (tmp_sock
= socket(PF_INET6
, SOCK_STREAM
, IPPROTO_TCP
)) >= 0 &&
437 setsockopt(tmp_sock
, soLevel
, soFlag
, (char *)&tos
, sizeof(int)) == 0 &&
438 bind(tmp_sock
, (struct sockaddr
*)&tmp_ip6
, sizeof(struct sockaddr_in6
)) == 0 ) {
440 debugs(3, 3, "IPv6 TPROXY support detected. Using.");
452 if ( test
.isIPv6() && !test
.setIPv4() ) {
453 debugs(3, DBG_CRITICAL
, "TPROXY lacks IPv6 support for " << test
);
459 /* Probe to see if the Kernel TPROXY support is IPv4-enabled (aka present) */
461 debugs(3, 3, "...Probing for IPv4 TPROXY support.");
463 struct sockaddr_in tmp_ip4
;
464 Ip::Address tmp
= "127.0.0.2";
466 tmp
.getSockAddr(tmp_ip4
);
468 if ( (tmp_sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
)) >= 0 &&
469 setsockopt(tmp_sock
, soLevel
, soFlag
, (char *)&tos
, sizeof(int)) == 0 &&
470 bind(tmp_sock
, (struct sockaddr
*)&tmp_ip4
, sizeof(struct sockaddr_in
)) == 0 ) {
472 debugs(3, 3, "IPv4 TPROXY support detected. Using.");
484 debugs(3, 3, "TPROXY setsockopt() not supported on this platform. Disabling TPROXY.");