Intercept.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 /* DEBUG: section 89 NAT / IP Interception */
10 
11 // Enable hack to workaround Solaris 10 IPFilter breakage
12 #define BUILDING_SQUID_IP_INTERCEPT_CC 1
13 
14 #include "squid.h"
15 #include "comm/Connection.h"
16 #include "fde.h"
17 #include "ip/Intercept.h"
18 #include "src/tools.h"
19 
20 #include <cerrno>
21 
22 #if IPF_TRANSPARENT
23 
24 #if !defined(IPFILTER_VERSION)
25 #define IPFILTER_VERSION 5000004
26 #endif
27 
28 #if HAVE_SYS_PARAM_H
29 #include <sys/param.h>
30 #endif
31 #if HAVE_SYS_IOCCOM_H
32 #include <sys/ioccom.h>
33 #endif
34 #if HAVE_SYS_IOCTL_H
35 #include <sys/ioctl.h>
36 #endif
37 #if HAVE_NETINET_IP6_H
38 #include <netinet/ip6.h>
39 #endif
40 #if HAVE_NETINET_TCP_H
41 #include <netinet/tcp.h>
42 #endif
43 #if HAVE_NET_IF_H
44 #include <net/if.h>
45 #endif
46 #if HAVE_IPL_H
47 #include <ipl.h>
48 #elif HAVE_NETINET_IPL_H
49 #include <netinet/ipl.h>
50 #endif
51 #if USE_SOLARIS_IPFILTER_MINOR_T_HACK
52 #undef minor_t
53 #endif
54 #if HAVE_IP_FIL_COMPAT_H
55 #include <ip_fil_compat.h>
56 #elif HAVE_NETINET_IP_FIL_COMPAT_H
57 #include <netinet/ip_fil_compat.h>
58 #elif HAVE_IP_COMPAT_H
59 #include <ip_compat.h>
60 #elif HAVE_NETINET_IP_COMPAT_H
61 #include <netinet/ip_compat.h>
62 #endif
63 #if HAVE_IP_FIL_H
64 #include <ip_fil.h>
65 #elif HAVE_NETINET_IP_FIL_H
66 #include <netinet/ip_fil.h>
67 #endif
68 #if HAVE_IP_NAT_H
69 #include <ip_nat.h>
70 #elif HAVE_NETINET_IP_NAT_H
71 #include <netinet/ip_nat.h>
72 #endif
73 
74 #endif /* IPF_TRANSPARENT required headers */
75 
76 #if PF_TRANSPARENT
77 #include <sys/socket.h>
78 #include <sys/ioctl.h>
79 #include <sys/fcntl.h>
80 #include <net/if.h>
81 #include <netinet/in.h>
82 #if HAVE_NET_PF_PFVAR_H
83 #include <net/pf/pfvar.h>
84 #endif /* HAVE_NET_PF_PFVAR_H */
85 #if HAVE_NET_PFVAR_H
86 #include <net/pfvar.h>
87 #endif /* HAVE_NET_PFVAR_H */
88 #endif /* PF_TRANSPARENT required headers */
89 
90 #if LINUX_NETFILTER
91 /* <climits> must be before including netfilter_ipv4.h */
92 #include <climits>
93 #include <linux/if.h>
94 #include <linux/netfilter_ipv4.h>
95 #if HAVE_LINUX_NETFILTER_IPV6_IP6_TABLES_H
96 /* 2013-07-01: Pablo the Netfilter maintainer is rejecting patches
97  * which will enable C++ compilers to build the Netfilter public headers.
98  * We can auto-detect its presence and attempt to use in case he ever
99  * changes his mind or things get cleaned up some other way.
100  * But until then are usually forced to hard-code the getsockopt() code
101  * for IPv6 NAT lookups.
102  */
103 #include <linux/netfilter_ipv6/ip6_tables.h>
104 #endif
105 #if !defined(IP6T_SO_ORIGINAL_DST)
106 #define IP6T_SO_ORIGINAL_DST 80 // stolen with prejudice from the above file.
107 #endif
108 #endif /* LINUX_NETFILTER required headers */
109 
110 // single global instance for access by other components.
112 
113 void
115 {
116  if (transparentActive_) {
117  debugs(89, DBG_IMPORTANT, "Stopping full transparency: " << str);
118  transparentActive_ = 0;
119  }
120 }
121 
122 void
124 {
125  if (interceptActive_) {
126  debugs(89, DBG_IMPORTANT, "Stopping IP interception: " << str);
127  interceptActive_ = 0;
128  }
129 }
130 
131 bool
133 {
134 #if LINUX_NETFILTER
135  struct sockaddr_storage lookup;
136  socklen_t len = newConn->local.isIPv6() ? sizeof(sockaddr_in6) : sizeof(sockaddr_in);
137  newConn->local.getSockAddr(lookup, AF_UNSPEC);
138 
141  if ( getsockopt(newConn->fd,
142  newConn->local.isIPv6() ? IPPROTO_IPV6 : IPPROTO_IP,
143  newConn->local.isIPv6() ? IP6T_SO_ORIGINAL_DST : SO_ORIGINAL_DST,
144  &lookup,
145  &len) != 0) {
146  const auto xerrno = errno;
147  debugs(89, DBG_IMPORTANT, "ERROR: NF getsockopt(ORIGINAL_DST) failed on " << newConn << ": " << xstrerr(xerrno));
148  return false;
149  } else {
150  newConn->local = lookup;
151  debugs(89, 5, "address NAT: " << newConn);
152  return true;
153  }
154 #else
155  (void)newConn;
156 #endif
157  return false;
158 }
159 
160 bool
162 {
163 #if (LINUX_NETFILTER && defined(IP_TRANSPARENT)) || \
164  (PF_TRANSPARENT && defined(SO_BINDANY)) || \
165  (IPFW_TRANSPARENT && defined(IP_BINDANY))
166 
167  /* Trust the user configured properly. If not no harm done.
168  * We will simply attempt a bind outgoing on our own IP.
169  */
170  debugs(89, 5, HERE << "address TPROXY: " << newConn);
171  return true;
172 #else
173  (void)newConn;
174  return false;
175 #endif
176 }
177 
178 bool
180 {
181 #if IPFW_TRANSPARENT
182  /* The getsockname() call performed already provided the TCP packet details.
183  * There is no way to identify whether they came from NAT or not.
184  * Trust the user configured properly.
185  */
186  debugs(89, 5, HERE << "address NAT: " << newConn);
187  return true;
188 #else
189  (void)newConn;
190  return false;
191 #endif
192 }
193 
194 bool
196 {
197 #if IPF_TRANSPARENT /* --enable-ipf-transparent */
198 
199  struct natlookup natLookup;
200  static int natfd = -1;
201  int x;
202 
203  // all fields must be set to 0
204  memset(&natLookup, 0, sizeof(natLookup));
205  // for NAT lookup set local and remote IP:port's
206  if (newConn->remote.isIPv6()) {
207 #if HAVE_STRUCT_NATLOOKUP_NL_INIPADDR_IN6
208  natLookup.nl_v = 6;
209  newConn->local.getInAddr(natLookup.nl_inipaddr.in6);
210  newConn->remote.getInAddr(natLookup.nl_outipaddr.in6);
211  }
212  else {
213  natLookup.nl_v = 4;
214  newConn->local.getInAddr(natLookup.nl_inipaddr.in4);
215  newConn->remote.getInAddr(natLookup.nl_outipaddr.in4);
216  }
217 #else /* HAVE_STRUCT_NATLOOKUP_NL_INIPADDR_IN6 */
218  // warn once every 10 at critical level, then push down a level each repeated event
219  static int warningLevel = DBG_CRITICAL;
220  debugs(89, warningLevel, "Your IPF (IPFilter) NAT does not support IPv6. Please upgrade it.");
221  warningLevel = (warningLevel + 1) % 10;
222  return false;
223  }
224  newConn->local.getInAddr(natLookup.nl_inip);
225  newConn->remote.getInAddr(natLookup.nl_outip);
226 #endif /* HAVE_STRUCT_NATLOOKUP_NL_INIPADDR_IN6 */
227  natLookup.nl_inport = htons(newConn->local.port());
228  natLookup.nl_outport = htons(newConn->remote.port());
229  // ... and the TCP flag
230  natLookup.nl_flags = IPN_TCP;
231 
232  if (natfd < 0) {
233  int save_errno;
234  enter_suid();
235 #ifdef IPNAT_NAME
236  natfd = open(IPNAT_NAME, O_RDONLY, 0);
237 #else
238  natfd = open(IPL_NAT, O_RDONLY, 0);
239 #endif
240  save_errno = errno;
241  leave_suid();
242  errno = save_errno;
243  }
244 
245  if (natfd < 0) {
246  const auto xerrno = errno;
247  debugs(89, DBG_IMPORTANT, "ERROR: IPF (IPFilter) NAT open failed: " << xstrerr(xerrno));
248  return false;
249  }
250 
251 #if defined(IPFILTER_VERSION) && (IPFILTER_VERSION >= 4000027)
252  struct ipfobj obj;
253  memset(&obj, 0, sizeof(obj));
254  obj.ipfo_rev = IPFILTER_VERSION;
255  obj.ipfo_size = sizeof(natLookup);
256  obj.ipfo_ptr = &natLookup;
257  obj.ipfo_type = IPFOBJ_NATLOOKUP;
258 
259  x = ioctl(natfd, SIOCGNATL, &obj);
260 #else
261  /*
262  * IP-Filter changed the type for SIOCGNATL between
263  * 3.3 and 3.4. It also changed the cmd value for
264  * SIOCGNATL, so at least we can detect it. We could
265  * put something in configure and use ifdefs here, but
266  * this seems simpler.
267  */
268  static int siocgnatl_cmd = SIOCGNATL & 0xff;
269  if (63 == siocgnatl_cmd) {
270  struct natlookup *nlp = &natLookup;
271  x = ioctl(natfd, SIOCGNATL, &nlp);
272  } else {
273  x = ioctl(natfd, SIOCGNATL, &natLookup);
274  }
275 
276 #endif /* defined(IPFILTER_VERSION) ... */
277  if (x < 0) {
278  const auto xerrno = errno;
279  if (xerrno != ESRCH) {
280  debugs(89, DBG_IMPORTANT, "ERROR: IPF (IPFilter) NAT lookup failed: ioctl(SIOCGNATL) (v=" << IPFILTER_VERSION << "): " << xstrerr(xerrno));
281  close(natfd);
282  natfd = -1;
283  }
284 
285  debugs(89, 9, HERE << "address: " << newConn);
286  return false;
287  } else {
288 #if HAVE_STRUCT_NATLOOKUP_NL_REALIPADDR_IN6
289  if (newConn->remote.isIPv6())
290  newConn->local = natLookup.nl_realipaddr.in6;
291  else
292  newConn->local = natLookup.nl_realipaddr.in4;
293 #else
294  newConn->local = natLookup.nl_realip;
295 #endif
296  newConn->local.port(ntohs(natLookup.nl_realport));
297  debugs(89, 5, HERE << "address NAT: " << newConn);
298  return true;
299  }
300 
301 #else
302  (void)newConn;
303 #endif /* --enable-ipf-transparent */
304  return false;
305 }
306 
307 bool
309 {
310 #if PF_TRANSPARENT /* --enable-pf-transparent */
311 
312 #if !USE_NAT_DEVPF
313  /* On recent PF versions the getsockname() call performed already provided
314  * the required TCP packet details.
315  * There is no way to identify whether they came from NAT or not.
316  *
317  * Trust the user configured properly.
318  */
319  debugs(89, 5, HERE << "address NAT divert-to: " << newConn);
320  return true;
321 
322 #else /* USE_NAT_DEVPF / --with-nat-devpf */
323 
324  struct pfioc_natlook nl;
325  static int pffd = -1;
326 
327  if (pffd < 0)
328  pffd = open("/dev/pf", O_RDONLY);
329 
330  if (pffd < 0) {
331  const auto xerrno = errno;
332  debugs(89, DBG_IMPORTANT, "ERROR: PF open failed: " << xstrerr(xerrno));
333  return false;
334  }
335 
336  memset(&nl, 0, sizeof(struct pfioc_natlook));
337 
338  if (newConn->remote.isIPv6()) {
339  newConn->remote.getInAddr(nl.saddr.v6);
340  newConn->local.getInAddr(nl.daddr.v6);
341  nl.af = AF_INET6;
342  } else {
343  newConn->remote.getInAddr(nl.saddr.v4);
344  newConn->local.getInAddr(nl.daddr.v4);
345  nl.af = AF_INET;
346  }
347 
348  nl.sport = htons(newConn->remote.port());
349  nl.dport = htons(newConn->local.port());
350 
351  nl.proto = IPPROTO_TCP;
352  nl.direction = PF_OUT;
353 
354  if (ioctl(pffd, DIOCNATLOOK, &nl)) {
355  const auto xerrno = errno;
356  if (xerrno != ENOENT) {
357  debugs(89, DBG_IMPORTANT, "ERROR: PF lookup failed: ioctl(DIOCNATLOOK): " << xstrerr(xerrno));
358  close(pffd);
359  pffd = -1;
360  }
361  debugs(89, 9, HERE << "address: " << newConn);
362  return false;
363  } else {
364  if (newConn->remote.isIPv6())
365  newConn->local = nl.rdaddr.v6;
366  else
367  newConn->local = nl.rdaddr.v4;
368  newConn->local.port(ntohs(nl.rdport));
369  debugs(89, 5, HERE << "address NAT: " << newConn);
370  return true;
371  }
372 #endif /* --with-nat-devpf */
373 #else
374  (void)newConn;
375 #endif /* --enable-pf-transparent */
376  return false;
377 }
378 
379 bool
381 {
382  /* --enable-linux-netfilter */
383  /* --enable-ipfw-transparent */
384  /* --enable-ipf-transparent */
385  /* --enable-pf-transparent */
386 #if IPF_TRANSPARENT || LINUX_NETFILTER || IPFW_TRANSPARENT || PF_TRANSPARENT
387 
388  debugs(89, 5, HERE << "address BEGIN: me/client= " << newConn->local << ", destination/me= " << newConn->remote);
389 
390  newConn->flags |= (listenConn->flags & (COMM_TRANSPARENT|COMM_INTERCEPTION));
391 
392  /* NP: try TPROXY first, its much quieter than NAT when non-matching */
393  if (transparentActive_ && listenConn->flags&COMM_TRANSPARENT) {
394  if (TproxyTransparent(newConn)) return true;
395  }
396 
397  if (interceptActive_ && listenConn->flags&COMM_INTERCEPTION) {
398  /* NAT methods that use sock-opts to return client address */
399  if (NetfilterInterception(newConn)) return true;
400  if (IpfwInterception(newConn)) return true;
401 
402  /* NAT methods that use ioctl to return client address AND destination address */
403  if (PfInterception(newConn)) return true;
404  if (IpfInterception(newConn)) return true;
405  }
406 
407 #else /* none of the transparent options configured */
408  (void)newConn;
409  (void)listenConn;
410  debugs(89, DBG_IMPORTANT, "WARNING: transparent proxying not supported");
411 #endif
412 
413  return false;
414 }
415 
416 bool
418 {
419  bool doneSuid = false;
420 
421 #if _SQUID_LINUX_ && defined(IP_TRANSPARENT) // Linux
422 # define soLevel SOL_IP
423 # define soFlag IP_TRANSPARENT
424 
425 #elif defined(SO_BINDANY) // OpenBSD 4.7+ and NetBSD with PF
426 # define soLevel SOL_SOCKET
427 # define soFlag SO_BINDANY
428  enter_suid();
429  doneSuid = true;
430 
431 #elif defined(IP_BINDANY) // FreeBSD with IPFW
432 # define soLevel IPPROTO_IP
433 # define soFlag IP_BINDANY
434  enter_suid();
435  doneSuid = true;
436 
437 #endif
438 
439 #if defined(soLevel) && defined(soFlag)
440 
441  debugs(3, 3, "Detect TPROXY support on port " << test);
442 
443  int tos = 1;
444  int tmp_sock = -1;
445 
446  /* Probe to see if the Kernel TPROXY support is IPv6-enabled */
447  if (test.isIPv6()) {
448  debugs(3, 3, "...Probing for IPv6 TPROXY support.");
449 
450  struct sockaddr_in6 tmp_ip6;
451  Ip::Address tmp = "::2";
452  tmp.port(0);
453  tmp.getSockAddr(tmp_ip6);
454 
455  if ( (tmp_sock = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP)) >= 0 &&
456  setsockopt(tmp_sock, soLevel, soFlag, (char *)&tos, sizeof(int)) == 0 &&
457  bind(tmp_sock, (struct sockaddr*)&tmp_ip6, sizeof(struct sockaddr_in6)) == 0 ) {
458 
459  debugs(3, 3, "IPv6 TPROXY support detected. Using.");
460  close(tmp_sock);
461  if (doneSuid)
462  leave_suid();
463  return true;
464  }
465  if (tmp_sock >= 0) {
466  close(tmp_sock);
467  tmp_sock = -1;
468  }
469  }
470 
471  if ( test.isIPv6() && !test.setIPv4() ) {
472  debugs(3, DBG_CRITICAL, "TPROXY lacks IPv6 support for " << test );
473  if (doneSuid)
474  leave_suid();
475  return false;
476  }
477 
478  /* Probe to see if the Kernel TPROXY support is IPv4-enabled (aka present) */
479  if (test.isIPv4()) {
480  debugs(3, 3, "...Probing for IPv4 TPROXY support.");
481 
482  struct sockaddr_in tmp_ip4;
483  Ip::Address tmp = "127.0.0.2";
484  tmp.port(0);
485  tmp.getSockAddr(tmp_ip4);
486 
487  if ( (tmp_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) >= 0 &&
488  setsockopt(tmp_sock, soLevel, soFlag, (char *)&tos, sizeof(int)) == 0 &&
489  bind(tmp_sock, (struct sockaddr*)&tmp_ip4, sizeof(struct sockaddr_in)) == 0 ) {
490 
491  debugs(3, 3, "IPv4 TPROXY support detected. Using.");
492  close(tmp_sock);
493  if (doneSuid)
494  leave_suid();
495  return true;
496  }
497  if (tmp_sock >= 0) {
498  close(tmp_sock);
499  }
500  }
501 
502 #else
503  debugs(3, 3, "TPROXY setsockopt() not supported on this platform. Disabling TPROXY.");
504 
505 #endif
506  if (doneSuid)
507  leave_suid();
508  return false;
509 }
510 
const char * xstrerr(int error)
Definition: xstrerror.cc:83
bool TproxyTransparent(const Comm::ConnectionPointer &newConn)
Definition: Intercept.cc:161
void StopTransparency(const char *str)
Definition: Intercept.cc:114
bool NetfilterInterception(const Comm::ConnectionPointer &newConn)
Definition: Intercept.cc:132
bool getInAddr(struct in_addr &) const
Definition: Address.cc:1020
#define DBG_CRITICAL
Definition: Debug.h:40
bool isIPv4() const
Definition: Address.cc:158
#define DBG_IMPORTANT
Definition: Debug.h:41
void StopInterception(const char *str)
Definition: Intercept.cc:123
int socklen_t
Definition: types.h:158
bool IpfwInterception(const Comm::ConnectionPointer &newConn)
Definition: Intercept.cc:179
#define COMM_INTERCEPTION
Definition: Connection.h:51
#define COMM_TRANSPARENT
Definition: Connection.h:50
void leave_suid(void)
Definition: tools.cc:506
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:123
int transparentActive_
Definition: Intercept.h:132
unsigned short port() const
Definition: Address.cc:778
bool isIPv6() const
Definition: Address.cc:164
Ip::Address local
Definition: Connection.h:144
std::ostream & HERE(std::ostream &s)
Definition: Debug.h:152
Ip::Address remote
Definition: Connection.h:147
bool setIPv4()
Definition: Address.cc:224
bool IpfInterception(const Comm::ConnectionPointer &newConn)
Definition: Intercept.cc:195
bool Lookup(const Comm::ConnectionPointer &newConn, const Comm::ConnectionPointer &listenConn)
Definition: Intercept.cc:380
bool ProbeForTproxy(Address &test)
Definition: Intercept.cc:417
Intercept Interceptor
Definition: Intercept.h:145
#define IP6T_SO_ORIGINAL_DST
Definition: Intercept.cc:106
void enter_suid(void)
Definition: tools.cc:577
bool PfInterception(const Comm::ConnectionPointer &newConn)
Definition: Intercept.cc:308
void getSockAddr(struct sockaddr_storage &addr, const int family) const
Definition: Address.cc:924

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors