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 <net/netfilter/nf_tproxy.h>
37 #include <linux/netfilter/xt_TPROXY.h>
40 tproxy_tg4(struct net
*net
, struct sk_buff
*skb
, __be32 laddr
, __be16 lport
,
41 u_int32_t mark_mask
, u_int32_t mark_value
)
43 const struct iphdr
*iph
= ip_hdr(skb
);
44 struct udphdr _hdr
, *hp
;
47 hp
= skb_header_pointer(skb
, ip_hdrlen(skb
), sizeof(_hdr
), &_hdr
);
51 /* check if there's an ongoing connection on the packet
52 * addresses, this happens if the redirect already happened
53 * and the current packet belongs to an already established
55 sk
= nf_tproxy_get_sock_v4(net
, skb
, iph
->protocol
,
56 iph
->saddr
, iph
->daddr
,
58 skb
->dev
, NF_TPROXY_LOOKUP_ESTABLISHED
);
60 laddr
= nf_tproxy_laddr4(skb
, laddr
, iph
->daddr
);
64 /* UDP has no TCP_TIME_WAIT state, so we never enter here */
65 if (sk
&& sk
->sk_state
== TCP_TIME_WAIT
)
66 /* reopening a TIME_WAIT connection needs special handling */
67 sk
= nf_tproxy_handle_time_wait4(net
, skb
, laddr
, lport
, sk
);
69 /* no, there's no established connection, check if
70 * there's a listener on the redirected addr/port */
71 sk
= nf_tproxy_get_sock_v4(net
, skb
, iph
->protocol
,
74 skb
->dev
, NF_TPROXY_LOOKUP_LISTENER
);
76 /* NOTE: assign_sock consumes our sk reference */
77 if (sk
&& nf_tproxy_sk_is_transparent(sk
)) {
78 /* This should be in a separate target, but we don't do multiple
79 targets on the same rule yet */
80 skb
->mark
= (skb
->mark
& ~mark_mask
) ^ mark_value
;
82 pr_debug("redirecting: proto %hhu %pI4:%hu -> %pI4:%hu, mark: %x\n",
83 iph
->protocol
, &iph
->daddr
, ntohs(hp
->dest
),
84 &laddr
, ntohs(lport
), skb
->mark
);
86 nf_tproxy_assign_sock(skb
, sk
);
90 pr_debug("no socket, dropping: proto %hhu %pI4:%hu -> %pI4:%hu, mark: %x\n",
91 iph
->protocol
, &iph
->saddr
, ntohs(hp
->source
),
92 &iph
->daddr
, ntohs(hp
->dest
), skb
->mark
);
97 tproxy_tg4_v0(struct sk_buff
*skb
, const struct xt_action_param
*par
)
99 const struct xt_tproxy_target_info
*tgi
= par
->targinfo
;
101 return tproxy_tg4(xt_net(par
), skb
, tgi
->laddr
, tgi
->lport
,
102 tgi
->mark_mask
, tgi
->mark_value
);
106 tproxy_tg4_v1(struct sk_buff
*skb
, const struct xt_action_param
*par
)
108 const struct xt_tproxy_target_info_v1
*tgi
= par
->targinfo
;
110 return tproxy_tg4(xt_net(par
), skb
, tgi
->laddr
.ip
, tgi
->lport
,
111 tgi
->mark_mask
, tgi
->mark_value
);
114 #ifdef XT_TPROXY_HAVE_IPV6
117 tproxy_tg6_v1(struct sk_buff
*skb
, const struct xt_action_param
*par
)
119 const struct ipv6hdr
*iph
= ipv6_hdr(skb
);
120 const struct xt_tproxy_target_info_v1
*tgi
= par
->targinfo
;
121 struct udphdr _hdr
, *hp
;
123 const struct in6_addr
*laddr
;
128 tproto
= ipv6_find_hdr(skb
, &thoff
, -1, NULL
, NULL
);
130 pr_debug("unable to find transport header in IPv6 packet, dropping\n");
134 hp
= skb_header_pointer(skb
, thoff
, sizeof(_hdr
), &_hdr
);
136 pr_debug("unable to grab transport header contents in IPv6 packet, dropping\n");
140 /* check if there's an ongoing connection on the packet
141 * addresses, this happens if the redirect already happened
142 * and the current packet belongs to an already established
144 sk
= nf_tproxy_get_sock_v6(xt_net(par
), skb
, thoff
, tproto
,
145 &iph
->saddr
, &iph
->daddr
,
146 hp
->source
, hp
->dest
,
147 xt_in(par
), NF_TPROXY_LOOKUP_ESTABLISHED
);
149 laddr
= nf_tproxy_laddr6(skb
, &tgi
->laddr
.in6
, &iph
->daddr
);
150 lport
= tgi
->lport
? tgi
->lport
: hp
->dest
;
152 /* UDP has no TCP_TIME_WAIT state, so we never enter here */
153 if (sk
&& sk
->sk_state
== TCP_TIME_WAIT
) {
154 const struct xt_tproxy_target_info_v1
*tgi
= par
->targinfo
;
155 /* reopening a TIME_WAIT connection needs special handling */
156 sk
= nf_tproxy_handle_time_wait6(skb
, tproto
, thoff
,
163 /* no there's no established connection, check if
164 * there's a listener on the redirected addr/port */
165 sk
= nf_tproxy_get_sock_v6(xt_net(par
), skb
, thoff
,
166 tproto
, &iph
->saddr
, laddr
,
168 xt_in(par
), NF_TPROXY_LOOKUP_LISTENER
);
170 /* NOTE: assign_sock consumes our sk reference */
171 if (sk
&& nf_tproxy_sk_is_transparent(sk
)) {
172 /* This should be in a separate target, but we don't do multiple
173 targets on the same rule yet */
174 skb
->mark
= (skb
->mark
& ~tgi
->mark_mask
) ^ tgi
->mark_value
;
176 pr_debug("redirecting: proto %hhu %pI6:%hu -> %pI6:%hu, mark: %x\n",
177 tproto
, &iph
->saddr
, ntohs(hp
->source
),
178 laddr
, ntohs(lport
), skb
->mark
);
180 nf_tproxy_assign_sock(skb
, sk
);
184 pr_debug("no socket, dropping: proto %hhu %pI6:%hu -> %pI6:%hu, mark: %x\n",
185 tproto
, &iph
->saddr
, ntohs(hp
->source
),
186 &iph
->daddr
, ntohs(hp
->dest
), skb
->mark
);
191 static int tproxy_tg6_check(const struct xt_tgchk_param
*par
)
193 const struct ip6t_ip6
*i
= par
->entryinfo
;
196 err
= nf_defrag_ipv6_enable(par
->net
);
200 if ((i
->proto
== IPPROTO_TCP
|| i
->proto
== IPPROTO_UDP
) &&
201 !(i
->invflags
& IP6T_INV_PROTO
))
204 pr_info_ratelimited("Can be used only with -p tcp or -p udp\n");
209 static int tproxy_tg4_check(const struct xt_tgchk_param
*par
)
211 const struct ipt_ip
*i
= par
->entryinfo
;
214 err
= nf_defrag_ipv4_enable(par
->net
);
218 if ((i
->proto
== IPPROTO_TCP
|| i
->proto
== IPPROTO_UDP
)
219 && !(i
->invflags
& IPT_INV_PROTO
))
222 pr_info_ratelimited("Can be used only with -p tcp or -p udp\n");
226 static struct xt_target tproxy_tg_reg
[] __read_mostly
= {
229 .family
= NFPROTO_IPV4
,
231 .target
= tproxy_tg4_v0
,
233 .targetsize
= sizeof(struct xt_tproxy_target_info
),
234 .checkentry
= tproxy_tg4_check
,
235 .hooks
= 1 << NF_INET_PRE_ROUTING
,
240 .family
= NFPROTO_IPV4
,
242 .target
= tproxy_tg4_v1
,
244 .targetsize
= sizeof(struct xt_tproxy_target_info_v1
),
245 .checkentry
= tproxy_tg4_check
,
246 .hooks
= 1 << NF_INET_PRE_ROUTING
,
249 #ifdef XT_TPROXY_HAVE_IPV6
252 .family
= NFPROTO_IPV6
,
254 .target
= tproxy_tg6_v1
,
256 .targetsize
= sizeof(struct xt_tproxy_target_info_v1
),
257 .checkentry
= tproxy_tg6_check
,
258 .hooks
= 1 << NF_INET_PRE_ROUTING
,
265 static int __init
tproxy_tg_init(void)
267 return xt_register_targets(tproxy_tg_reg
, ARRAY_SIZE(tproxy_tg_reg
));
270 static void __exit
tproxy_tg_exit(void)
272 xt_unregister_targets(tproxy_tg_reg
, ARRAY_SIZE(tproxy_tg_reg
));
275 module_init(tproxy_tg_init
);
276 module_exit(tproxy_tg_exit
);
277 MODULE_LICENSE("GPL");
278 MODULE_AUTHOR("Balazs Scheidler, Krisztian Kovacs");
279 MODULE_DESCRIPTION("Netfilter transparent proxy (TPROXY) target module.");
280 MODULE_ALIAS("ipt_TPROXY");
281 MODULE_ALIAS("ip6t_TPROXY");