2 * Transparent proxy support for Linux/iptables
4 * Copyright (c) 2006-2010 BalaBit IT Ltd.
5 * Author: Balazs Scheidler, Krisztian Kovacs
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/module.h>
14 #include <linux/skbuff.h>
16 #include <net/checksum.h>
19 #include <net/inet_sock.h>
20 #include <net/inet_hashtables.h>
21 #include <linux/inetdevice.h>
22 #include <linux/netfilter/x_tables.h>
23 #include <linux/netfilter_ipv4/ip_tables.h>
25 #include <net/netfilter/ipv4/nf_defrag_ipv4.h>
27 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
28 #define XT_TPROXY_HAVE_IPV6 1
29 #include <net/if_inet6.h>
30 #include <net/addrconf.h>
31 #include <net/inet6_hashtables.h>
32 #include <linux/netfilter_ipv6/ip6_tables.h>
33 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
36 #include <linux/netfilter/xt_TPROXY.h>
38 enum nf_tproxy_lookup_t
{
40 NFT_LOOKUP_ESTABLISHED
,
43 static bool tproxy_sk_is_transparent(struct sock
*sk
)
45 switch (sk
->sk_state
) {
47 if (inet_twsk(sk
)->tw_transparent
)
50 case TCP_NEW_SYN_RECV
:
51 if (inet_rsk(inet_reqsk(sk
))->no_srccheck
)
55 if (inet_sk(sk
)->transparent
)
64 tproxy_laddr4(struct sk_buff
*skb
, __be32 user_laddr
, __be32 daddr
)
66 struct in_device
*indev
;
74 indev
= __in_dev_get_rcu(skb
->dev
);
75 for_primary_ifa(indev
) {
76 laddr
= ifa
->ifa_local
;
81 return laddr
? laddr
: daddr
;
85 * This is used when the user wants to intercept a connection matching
86 * an explicit iptables rule. In this case the sockets are assumed
87 * matching in preference order:
89 * - match: if there's a fully established connection matching the
90 * _packet_ tuple, it is returned, assuming the redirection
91 * already took place and we process a packet belonging to an
92 * established connection
94 * - match: if there's a listening socket matching the redirection
95 * (e.g. on-port & on-ip of the connection), it is returned,
96 * regardless if it was bound to 0.0.0.0 or an explicit
97 * address. The reasoning is that if there's an explicit rule, it
98 * does not really matter if the listener is bound to an interface
99 * or to 0. The user already stated that he wants redirection
100 * (since he added the rule).
102 * Please note that there's an overlap between what a TPROXY target
103 * and a socket match will match. Normally if you have both rules the
104 * "socket" match will be the first one, effectively all packets
105 * belonging to established connections going through that one.
107 static inline struct sock
*
108 nf_tproxy_get_sock_v4(struct net
*net
, struct sk_buff
*skb
, void *hp
,
110 const __be32 saddr
, const __be32 daddr
,
111 const __be16 sport
, const __be16 dport
,
112 const struct net_device
*in
,
113 const enum nf_tproxy_lookup_t lookup_type
)
120 switch (lookup_type
) {
121 case NFT_LOOKUP_LISTENER
:
123 sk
= inet_lookup_listener(net
, &tcp_hashinfo
, skb
,
130 if (sk
&& !atomic_inc_not_zero(&sk
->sk_refcnt
))
132 /* NOTE: we return listeners even if bound to
133 * 0.0.0.0, those are filtered out in
134 * xt_socket, since xt_TPROXY needs 0 bound
138 case NFT_LOOKUP_ESTABLISHED
:
139 sk
= inet_lookup_established(net
, &tcp_hashinfo
,
140 saddr
, sport
, daddr
, dport
,
148 sk
= udp4_lib_lookup(net
, saddr
, sport
, daddr
, dport
,
151 int connected
= (sk
->sk_state
== TCP_ESTABLISHED
);
152 int wildcard
= (inet_sk(sk
)->inet_rcv_saddr
== 0);
154 /* NOTE: we return listeners even if bound to
155 * 0.0.0.0, those are filtered out in
156 * xt_socket, since xt_TPROXY needs 0 bound
159 if ((lookup_type
== NFT_LOOKUP_ESTABLISHED
&& (!connected
|| wildcard
)) ||
160 (lookup_type
== NFT_LOOKUP_LISTENER
&& connected
)) {
171 pr_debug("tproxy socket lookup: proto %u %08x:%u -> %08x:%u, lookup type: %d, sock %p\n",
172 protocol
, ntohl(saddr
), ntohs(sport
), ntohl(daddr
), ntohs(dport
), lookup_type
, sk
);
177 #ifdef XT_TPROXY_HAVE_IPV6
178 static inline struct sock
*
179 nf_tproxy_get_sock_v6(struct net
*net
, struct sk_buff
*skb
, int thoff
, void *hp
,
181 const struct in6_addr
*saddr
, const struct in6_addr
*daddr
,
182 const __be16 sport
, const __be16 dport
,
183 const struct net_device
*in
,
184 const enum nf_tproxy_lookup_t lookup_type
)
191 switch (lookup_type
) {
192 case NFT_LOOKUP_LISTENER
:
194 sk
= inet6_lookup_listener(net
, &tcp_hashinfo
, skb
,
195 thoff
+ __tcp_hdrlen(tcph
),
200 if (sk
&& !atomic_inc_not_zero(&sk
->sk_refcnt
))
202 /* NOTE: we return listeners even if bound to
203 * 0.0.0.0, those are filtered out in
204 * xt_socket, since xt_TPROXY needs 0 bound
208 case NFT_LOOKUP_ESTABLISHED
:
209 sk
= __inet6_lookup_established(net
, &tcp_hashinfo
,
210 saddr
, sport
, daddr
, ntohs(dport
),
218 sk
= udp6_lib_lookup(net
, saddr
, sport
, daddr
, dport
,
221 int connected
= (sk
->sk_state
== TCP_ESTABLISHED
);
222 int wildcard
= ipv6_addr_any(&sk
->sk_v6_rcv_saddr
);
224 /* NOTE: we return listeners even if bound to
225 * 0.0.0.0, those are filtered out in
226 * xt_socket, since xt_TPROXY needs 0 bound
229 if ((lookup_type
== NFT_LOOKUP_ESTABLISHED
&& (!connected
|| wildcard
)) ||
230 (lookup_type
== NFT_LOOKUP_LISTENER
&& connected
)) {
241 pr_debug("tproxy socket lookup: proto %u %pI6:%u -> %pI6:%u, lookup type: %d, sock %p\n",
242 protocol
, saddr
, ntohs(sport
), daddr
, ntohs(dport
), lookup_type
, sk
);
249 * tproxy_handle_time_wait4 - handle IPv4 TCP TIME_WAIT reopen redirections
250 * @skb: The skb being processed.
251 * @laddr: IPv4 address to redirect to or zero.
252 * @lport: TCP port to redirect to or zero.
253 * @sk: The TIME_WAIT TCP socket found by the lookup.
255 * We have to handle SYN packets arriving to TIME_WAIT sockets
256 * differently: instead of reopening the connection we should rather
257 * redirect the new connection to the proxy if there's a listener
260 * tproxy_handle_time_wait4() consumes the socket reference passed in.
262 * Returns the listener socket if there's one, the TIME_WAIT socket if
263 * no such listener is found, or NULL if the TCP header is incomplete.
266 tproxy_handle_time_wait4(struct net
*net
, struct sk_buff
*skb
,
267 __be32 laddr
, __be16 lport
, struct sock
*sk
)
269 const struct iphdr
*iph
= ip_hdr(skb
);
270 struct tcphdr _hdr
, *hp
;
272 hp
= skb_header_pointer(skb
, ip_hdrlen(skb
), sizeof(_hdr
), &_hdr
);
274 inet_twsk_put(inet_twsk(sk
));
278 if (hp
->syn
&& !hp
->rst
&& !hp
->ack
&& !hp
->fin
) {
279 /* SYN to a TIME_WAIT socket, we'd rather redirect it
280 * to a listener socket if there's one */
283 sk2
= nf_tproxy_get_sock_v4(net
, skb
, hp
, iph
->protocol
,
284 iph
->saddr
, laddr
? laddr
: iph
->daddr
,
285 hp
->source
, lport
? lport
: hp
->dest
,
286 skb
->dev
, NFT_LOOKUP_LISTENER
);
288 inet_twsk_deschedule_put(inet_twsk(sk
));
296 /* assign a socket to the skb -- consumes sk */
298 nf_tproxy_assign_sock(struct sk_buff
*skb
, struct sock
*sk
)
302 skb
->destructor
= sock_edemux
;
306 tproxy_tg4(struct net
*net
, struct sk_buff
*skb
, __be32 laddr
, __be16 lport
,
307 u_int32_t mark_mask
, u_int32_t mark_value
)
309 const struct iphdr
*iph
= ip_hdr(skb
);
310 struct udphdr _hdr
, *hp
;
313 hp
= skb_header_pointer(skb
, ip_hdrlen(skb
), sizeof(_hdr
), &_hdr
);
317 /* check if there's an ongoing connection on the packet
318 * addresses, this happens if the redirect already happened
319 * and the current packet belongs to an already established
321 sk
= nf_tproxy_get_sock_v4(net
, skb
, hp
, iph
->protocol
,
322 iph
->saddr
, iph
->daddr
,
323 hp
->source
, hp
->dest
,
324 skb
->dev
, NFT_LOOKUP_ESTABLISHED
);
326 laddr
= tproxy_laddr4(skb
, laddr
, iph
->daddr
);
330 /* UDP has no TCP_TIME_WAIT state, so we never enter here */
331 if (sk
&& sk
->sk_state
== TCP_TIME_WAIT
)
332 /* reopening a TIME_WAIT connection needs special handling */
333 sk
= tproxy_handle_time_wait4(net
, skb
, laddr
, lport
, sk
);
335 /* no, there's no established connection, check if
336 * there's a listener on the redirected addr/port */
337 sk
= nf_tproxy_get_sock_v4(net
, skb
, hp
, iph
->protocol
,
340 skb
->dev
, NFT_LOOKUP_LISTENER
);
342 /* NOTE: assign_sock consumes our sk reference */
343 if (sk
&& tproxy_sk_is_transparent(sk
)) {
344 /* This should be in a separate target, but we don't do multiple
345 targets on the same rule yet */
346 skb
->mark
= (skb
->mark
& ~mark_mask
) ^ mark_value
;
348 pr_debug("redirecting: proto %hhu %pI4:%hu -> %pI4:%hu, mark: %x\n",
349 iph
->protocol
, &iph
->daddr
, ntohs(hp
->dest
),
350 &laddr
, ntohs(lport
), skb
->mark
);
352 nf_tproxy_assign_sock(skb
, sk
);
356 pr_debug("no socket, dropping: proto %hhu %pI4:%hu -> %pI4:%hu, mark: %x\n",
357 iph
->protocol
, &iph
->saddr
, ntohs(hp
->source
),
358 &iph
->daddr
, ntohs(hp
->dest
), skb
->mark
);
363 tproxy_tg4_v0(struct sk_buff
*skb
, const struct xt_action_param
*par
)
365 const struct xt_tproxy_target_info
*tgi
= par
->targinfo
;
367 return tproxy_tg4(xt_net(par
), skb
, tgi
->laddr
, tgi
->lport
,
368 tgi
->mark_mask
, tgi
->mark_value
);
372 tproxy_tg4_v1(struct sk_buff
*skb
, const struct xt_action_param
*par
)
374 const struct xt_tproxy_target_info_v1
*tgi
= par
->targinfo
;
376 return tproxy_tg4(xt_net(par
), skb
, tgi
->laddr
.ip
, tgi
->lport
,
377 tgi
->mark_mask
, tgi
->mark_value
);
380 #ifdef XT_TPROXY_HAVE_IPV6
382 static inline const struct in6_addr
*
383 tproxy_laddr6(struct sk_buff
*skb
, const struct in6_addr
*user_laddr
,
384 const struct in6_addr
*daddr
)
386 struct inet6_dev
*indev
;
387 struct inet6_ifaddr
*ifa
;
388 struct in6_addr
*laddr
;
390 if (!ipv6_addr_any(user_laddr
))
395 indev
= __in6_dev_get(skb
->dev
);
397 list_for_each_entry(ifa
, &indev
->addr_list
, if_list
) {
398 if (ifa
->flags
& (IFA_F_TENTATIVE
| IFA_F_DEPRECATED
))
406 return laddr
? laddr
: daddr
;
410 * tproxy_handle_time_wait6 - handle IPv6 TCP TIME_WAIT reopen redirections
411 * @skb: The skb being processed.
412 * @tproto: Transport protocol.
413 * @thoff: Transport protocol header offset.
414 * @par: Iptables target parameters.
415 * @sk: The TIME_WAIT TCP socket found by the lookup.
417 * We have to handle SYN packets arriving to TIME_WAIT sockets
418 * differently: instead of reopening the connection we should rather
419 * redirect the new connection to the proxy if there's a listener
422 * tproxy_handle_time_wait6() consumes the socket reference passed in.
424 * Returns the listener socket if there's one, the TIME_WAIT socket if
425 * no such listener is found, or NULL if the TCP header is incomplete.
428 tproxy_handle_time_wait6(struct sk_buff
*skb
, int tproto
, int thoff
,
429 const struct xt_action_param
*par
,
432 const struct ipv6hdr
*iph
= ipv6_hdr(skb
);
433 struct tcphdr _hdr
, *hp
;
434 const struct xt_tproxy_target_info_v1
*tgi
= par
->targinfo
;
436 hp
= skb_header_pointer(skb
, thoff
, sizeof(_hdr
), &_hdr
);
438 inet_twsk_put(inet_twsk(sk
));
442 if (hp
->syn
&& !hp
->rst
&& !hp
->ack
&& !hp
->fin
) {
443 /* SYN to a TIME_WAIT socket, we'd rather redirect it
444 * to a listener socket if there's one */
447 sk2
= nf_tproxy_get_sock_v6(xt_net(par
), skb
, thoff
, hp
, tproto
,
449 tproxy_laddr6(skb
, &tgi
->laddr
.in6
, &iph
->daddr
),
451 tgi
->lport
? tgi
->lport
: hp
->dest
,
452 skb
->dev
, NFT_LOOKUP_LISTENER
);
454 inet_twsk_deschedule_put(inet_twsk(sk
));
463 tproxy_tg6_v1(struct sk_buff
*skb
, const struct xt_action_param
*par
)
465 const struct ipv6hdr
*iph
= ipv6_hdr(skb
);
466 const struct xt_tproxy_target_info_v1
*tgi
= par
->targinfo
;
467 struct udphdr _hdr
, *hp
;
469 const struct in6_addr
*laddr
;
474 tproto
= ipv6_find_hdr(skb
, &thoff
, -1, NULL
, NULL
);
476 pr_debug("unable to find transport header in IPv6 packet, dropping\n");
480 hp
= skb_header_pointer(skb
, thoff
, sizeof(_hdr
), &_hdr
);
482 pr_debug("unable to grab transport header contents in IPv6 packet, dropping\n");
486 /* check if there's an ongoing connection on the packet
487 * addresses, this happens if the redirect already happened
488 * and the current packet belongs to an already established
490 sk
= nf_tproxy_get_sock_v6(xt_net(par
), skb
, thoff
, hp
, tproto
,
491 &iph
->saddr
, &iph
->daddr
,
492 hp
->source
, hp
->dest
,
493 xt_in(par
), NFT_LOOKUP_ESTABLISHED
);
495 laddr
= tproxy_laddr6(skb
, &tgi
->laddr
.in6
, &iph
->daddr
);
496 lport
= tgi
->lport
? tgi
->lport
: hp
->dest
;
498 /* UDP has no TCP_TIME_WAIT state, so we never enter here */
499 if (sk
&& sk
->sk_state
== TCP_TIME_WAIT
)
500 /* reopening a TIME_WAIT connection needs special handling */
501 sk
= tproxy_handle_time_wait6(skb
, tproto
, thoff
, par
, sk
);
503 /* no there's no established connection, check if
504 * there's a listener on the redirected addr/port */
505 sk
= nf_tproxy_get_sock_v6(xt_net(par
), skb
, thoff
, hp
,
506 tproto
, &iph
->saddr
, laddr
,
508 xt_in(par
), NFT_LOOKUP_LISTENER
);
510 /* NOTE: assign_sock consumes our sk reference */
511 if (sk
&& tproxy_sk_is_transparent(sk
)) {
512 /* This should be in a separate target, but we don't do multiple
513 targets on the same rule yet */
514 skb
->mark
= (skb
->mark
& ~tgi
->mark_mask
) ^ tgi
->mark_value
;
516 pr_debug("redirecting: proto %hhu %pI6:%hu -> %pI6:%hu, mark: %x\n",
517 tproto
, &iph
->saddr
, ntohs(hp
->source
),
518 laddr
, ntohs(lport
), skb
->mark
);
520 nf_tproxy_assign_sock(skb
, sk
);
524 pr_debug("no socket, dropping: proto %hhu %pI6:%hu -> %pI6:%hu, mark: %x\n",
525 tproto
, &iph
->saddr
, ntohs(hp
->source
),
526 &iph
->daddr
, ntohs(hp
->dest
), skb
->mark
);
531 static int tproxy_tg6_check(const struct xt_tgchk_param
*par
)
533 const struct ip6t_ip6
*i
= par
->entryinfo
;
536 err
= nf_defrag_ipv6_enable(par
->net
);
540 if ((i
->proto
== IPPROTO_TCP
|| i
->proto
== IPPROTO_UDP
) &&
541 !(i
->invflags
& IP6T_INV_PROTO
))
544 pr_info("Can be used only in combination with "
545 "either -p tcp or -p udp\n");
550 static int tproxy_tg4_check(const struct xt_tgchk_param
*par
)
552 const struct ipt_ip
*i
= par
->entryinfo
;
555 err
= nf_defrag_ipv4_enable(par
->net
);
559 if ((i
->proto
== IPPROTO_TCP
|| i
->proto
== IPPROTO_UDP
)
560 && !(i
->invflags
& IPT_INV_PROTO
))
563 pr_info("Can be used only in combination with "
564 "either -p tcp or -p udp\n");
568 static struct xt_target tproxy_tg_reg
[] __read_mostly
= {
571 .family
= NFPROTO_IPV4
,
573 .target
= tproxy_tg4_v0
,
575 .targetsize
= sizeof(struct xt_tproxy_target_info
),
576 .checkentry
= tproxy_tg4_check
,
577 .hooks
= 1 << NF_INET_PRE_ROUTING
,
582 .family
= NFPROTO_IPV4
,
584 .target
= tproxy_tg4_v1
,
586 .targetsize
= sizeof(struct xt_tproxy_target_info_v1
),
587 .checkentry
= tproxy_tg4_check
,
588 .hooks
= 1 << NF_INET_PRE_ROUTING
,
591 #ifdef XT_TPROXY_HAVE_IPV6
594 .family
= NFPROTO_IPV6
,
596 .target
= tproxy_tg6_v1
,
598 .targetsize
= sizeof(struct xt_tproxy_target_info_v1
),
599 .checkentry
= tproxy_tg6_check
,
600 .hooks
= 1 << NF_INET_PRE_ROUTING
,
607 static int __init
tproxy_tg_init(void)
609 return xt_register_targets(tproxy_tg_reg
, ARRAY_SIZE(tproxy_tg_reg
));
612 static void __exit
tproxy_tg_exit(void)
614 xt_unregister_targets(tproxy_tg_reg
, ARRAY_SIZE(tproxy_tg_reg
));
617 module_init(tproxy_tg_init
);
618 module_exit(tproxy_tg_exit
);
619 MODULE_LICENSE("GPL");
620 MODULE_AUTHOR("Balazs Scheidler, Krisztian Kovacs");
621 MODULE_DESCRIPTION("Netfilter transparent proxy (TPROXY) target module.");
622 MODULE_ALIAS("ipt_TPROXY");
623 MODULE_ALIAS("ip6t_TPROXY");