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>
18 #include <net/inet_sock.h>
19 #include <linux/inetdevice.h>
20 #include <linux/netfilter/x_tables.h>
21 #include <linux/netfilter_ipv4/ip_tables.h>
23 #include <net/netfilter/ipv4/nf_defrag_ipv4.h>
25 #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
26 #define XT_TPROXY_HAVE_IPV6 1
27 #include <net/if_inet6.h>
28 #include <net/addrconf.h>
29 #include <linux/netfilter_ipv6/ip6_tables.h>
30 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
33 #include <net/netfilter/nf_tproxy_core.h>
34 #include <linux/netfilter/xt_TPROXY.h>
37 tproxy_laddr4(struct sk_buff
*skb
, __be32 user_laddr
, __be32 daddr
)
39 struct in_device
*indev
;
47 indev
= __in_dev_get_rcu(skb
->dev
);
48 for_primary_ifa(indev
) {
49 laddr
= ifa
->ifa_local
;
54 return laddr
? laddr
: daddr
;
58 * tproxy_handle_time_wait4() - handle IPv4 TCP TIME_WAIT reopen redirections
59 * @skb: The skb being processed.
60 * @laddr: IPv4 address to redirect to or zero.
61 * @lport: TCP port to redirect to or zero.
62 * @sk: The TIME_WAIT TCP socket found by the lookup.
64 * We have to handle SYN packets arriving to TIME_WAIT sockets
65 * differently: instead of reopening the connection we should rather
66 * redirect the new connection to the proxy if there's a listener
69 * tproxy_handle_time_wait4() consumes the socket reference passed in.
71 * Returns the listener socket if there's one, the TIME_WAIT socket if
72 * no such listener is found, or NULL if the TCP header is incomplete.
75 tproxy_handle_time_wait4(struct sk_buff
*skb
, __be32 laddr
, __be16 lport
,
78 const struct iphdr
*iph
= ip_hdr(skb
);
79 struct tcphdr _hdr
, *hp
;
81 hp
= skb_header_pointer(skb
, ip_hdrlen(skb
), sizeof(_hdr
), &_hdr
);
83 inet_twsk_put(inet_twsk(sk
));
87 if (hp
->syn
&& !hp
->rst
&& !hp
->ack
&& !hp
->fin
) {
88 /* SYN to a TIME_WAIT socket, we'd rather redirect it
89 * to a listener socket if there's one */
92 sk2
= nf_tproxy_get_sock_v4(dev_net(skb
->dev
), iph
->protocol
,
93 iph
->saddr
, laddr
? laddr
: iph
->daddr
,
94 hp
->source
, lport
? lport
: hp
->dest
,
95 skb
->dev
, NFT_LOOKUP_LISTENER
);
97 inet_twsk_deschedule(inet_twsk(sk
), &tcp_death_row
);
98 inet_twsk_put(inet_twsk(sk
));
107 tproxy_tg4(struct sk_buff
*skb
, __be32 laddr
, __be16 lport
,
108 u_int32_t mark_mask
, u_int32_t mark_value
)
110 const struct iphdr
*iph
= ip_hdr(skb
);
111 struct udphdr _hdr
, *hp
;
114 hp
= skb_header_pointer(skb
, ip_hdrlen(skb
), sizeof(_hdr
), &_hdr
);
118 /* check if there's an ongoing connection on the packet
119 * addresses, this happens if the redirect already happened
120 * and the current packet belongs to an already established
122 sk
= nf_tproxy_get_sock_v4(dev_net(skb
->dev
), iph
->protocol
,
123 iph
->saddr
, iph
->daddr
,
124 hp
->source
, hp
->dest
,
125 skb
->dev
, NFT_LOOKUP_ESTABLISHED
);
127 laddr
= tproxy_laddr4(skb
, laddr
, iph
->daddr
);
131 /* UDP has no TCP_TIME_WAIT state, so we never enter here */
132 if (sk
&& sk
->sk_state
== TCP_TIME_WAIT
)
133 /* reopening a TIME_WAIT connection needs special handling */
134 sk
= tproxy_handle_time_wait4(skb
, laddr
, lport
, sk
);
136 /* no, there's no established connection, check if
137 * there's a listener on the redirected addr/port */
138 sk
= nf_tproxy_get_sock_v4(dev_net(skb
->dev
), iph
->protocol
,
141 skb
->dev
, NFT_LOOKUP_LISTENER
);
143 /* NOTE: assign_sock consumes our sk reference */
144 if (sk
&& nf_tproxy_assign_sock(skb
, sk
)) {
145 /* This should be in a separate target, but we don't do multiple
146 targets on the same rule yet */
147 skb
->mark
= (skb
->mark
& ~mark_mask
) ^ mark_value
;
149 pr_debug("redirecting: proto %hhu %pI4:%hu -> %pI4:%hu, mark: %x\n",
150 iph
->protocol
, &iph
->daddr
, ntohs(hp
->dest
),
151 &laddr
, ntohs(lport
), skb
->mark
);
155 pr_debug("no socket, dropping: proto %hhu %pI4:%hu -> %pI4:%hu, mark: %x\n",
156 iph
->protocol
, &iph
->saddr
, ntohs(hp
->source
),
157 &iph
->daddr
, ntohs(hp
->dest
), skb
->mark
);
162 tproxy_tg4_v0(struct sk_buff
*skb
, const struct xt_action_param
*par
)
164 const struct xt_tproxy_target_info
*tgi
= par
->targinfo
;
166 return tproxy_tg4(skb
, tgi
->laddr
, tgi
->lport
, tgi
->mark_mask
, tgi
->mark_value
);
170 tproxy_tg4_v1(struct sk_buff
*skb
, const struct xt_action_param
*par
)
172 const struct xt_tproxy_target_info_v1
*tgi
= par
->targinfo
;
174 return tproxy_tg4(skb
, tgi
->laddr
.ip
, tgi
->lport
, tgi
->mark_mask
, tgi
->mark_value
);
177 #ifdef XT_TPROXY_HAVE_IPV6
179 static inline const struct in6_addr
*
180 tproxy_laddr6(struct sk_buff
*skb
, const struct in6_addr
*user_laddr
,
181 const struct in6_addr
*daddr
)
183 struct inet6_dev
*indev
;
184 struct inet6_ifaddr
*ifa
;
185 struct in6_addr
*laddr
;
187 if (!ipv6_addr_any(user_laddr
))
192 indev
= __in6_dev_get(skb
->dev
);
194 list_for_each_entry(ifa
, &indev
->addr_list
, if_list
) {
195 if (ifa
->flags
& (IFA_F_TENTATIVE
| IFA_F_DEPRECATED
))
203 return laddr
? laddr
: daddr
;
207 * tproxy_handle_time_wait6() - handle IPv6 TCP TIME_WAIT reopen redirections
208 * @skb: The skb being processed.
209 * @tproto: Transport protocol.
210 * @thoff: Transport protocol header offset.
211 * @par: Iptables target parameters.
212 * @sk: The TIME_WAIT TCP socket found by the lookup.
214 * We have to handle SYN packets arriving to TIME_WAIT sockets
215 * differently: instead of reopening the connection we should rather
216 * redirect the new connection to the proxy if there's a listener
219 * tproxy_handle_time_wait6() consumes the socket reference passed in.
221 * Returns the listener socket if there's one, the TIME_WAIT socket if
222 * no such listener is found, or NULL if the TCP header is incomplete.
225 tproxy_handle_time_wait6(struct sk_buff
*skb
, int tproto
, int thoff
,
226 const struct xt_action_param
*par
,
229 const struct ipv6hdr
*iph
= ipv6_hdr(skb
);
230 struct tcphdr _hdr
, *hp
;
231 const struct xt_tproxy_target_info_v1
*tgi
= par
->targinfo
;
233 hp
= skb_header_pointer(skb
, thoff
, sizeof(_hdr
), &_hdr
);
235 inet_twsk_put(inet_twsk(sk
));
239 if (hp
->syn
&& !hp
->rst
&& !hp
->ack
&& !hp
->fin
) {
240 /* SYN to a TIME_WAIT socket, we'd rather redirect it
241 * to a listener socket if there's one */
244 sk2
= nf_tproxy_get_sock_v6(dev_net(skb
->dev
), tproto
,
246 tproxy_laddr6(skb
, &tgi
->laddr
.in6
, &iph
->daddr
),
248 tgi
->lport
? tgi
->lport
: hp
->dest
,
249 skb
->dev
, NFT_LOOKUP_LISTENER
);
251 inet_twsk_deschedule(inet_twsk(sk
), &tcp_death_row
);
252 inet_twsk_put(inet_twsk(sk
));
261 tproxy_tg6_v1(struct sk_buff
*skb
, const struct xt_action_param
*par
)
263 const struct ipv6hdr
*iph
= ipv6_hdr(skb
);
264 const struct xt_tproxy_target_info_v1
*tgi
= par
->targinfo
;
265 struct udphdr _hdr
, *hp
;
267 const struct in6_addr
*laddr
;
272 tproto
= ipv6_find_hdr(skb
, &thoff
, -1, NULL
);
274 pr_debug("unable to find transport header in IPv6 packet, dropping\n");
278 hp
= skb_header_pointer(skb
, thoff
, sizeof(_hdr
), &_hdr
);
280 pr_debug("unable to grab transport header contents in IPv6 packet, dropping\n");
284 /* check if there's an ongoing connection on the packet
285 * addresses, this happens if the redirect already happened
286 * and the current packet belongs to an already established
288 sk
= nf_tproxy_get_sock_v6(dev_net(skb
->dev
), tproto
,
289 &iph
->saddr
, &iph
->daddr
,
290 hp
->source
, hp
->dest
,
291 par
->in
, NFT_LOOKUP_ESTABLISHED
);
293 laddr
= tproxy_laddr6(skb
, &tgi
->laddr
.in6
, &iph
->daddr
);
294 lport
= tgi
->lport
? tgi
->lport
: hp
->dest
;
296 /* UDP has no TCP_TIME_WAIT state, so we never enter here */
297 if (sk
&& sk
->sk_state
== TCP_TIME_WAIT
)
298 /* reopening a TIME_WAIT connection needs special handling */
299 sk
= tproxy_handle_time_wait6(skb
, tproto
, thoff
, par
, sk
);
301 /* no there's no established connection, check if
302 * there's a listener on the redirected addr/port */
303 sk
= nf_tproxy_get_sock_v6(dev_net(skb
->dev
), tproto
,
306 par
->in
, NFT_LOOKUP_LISTENER
);
308 /* NOTE: assign_sock consumes our sk reference */
309 if (sk
&& nf_tproxy_assign_sock(skb
, sk
)) {
310 /* This should be in a separate target, but we don't do multiple
311 targets on the same rule yet */
312 skb
->mark
= (skb
->mark
& ~tgi
->mark_mask
) ^ tgi
->mark_value
;
314 pr_debug("redirecting: proto %hhu %pI6:%hu -> %pI6:%hu, mark: %x\n",
315 tproto
, &iph
->saddr
, ntohs(hp
->source
),
316 laddr
, ntohs(lport
), skb
->mark
);
320 pr_debug("no socket, dropping: proto %hhu %pI6:%hu -> %pI6:%hu, mark: %x\n",
321 tproto
, &iph
->saddr
, ntohs(hp
->source
),
322 &iph
->daddr
, ntohs(hp
->dest
), skb
->mark
);
327 static int tproxy_tg6_check(const struct xt_tgchk_param
*par
)
329 const struct ip6t_ip6
*i
= par
->entryinfo
;
331 if ((i
->proto
== IPPROTO_TCP
|| i
->proto
== IPPROTO_UDP
)
332 && !(i
->flags
& IP6T_INV_PROTO
))
335 pr_info("Can be used only in combination with "
336 "either -p tcp or -p udp\n");
341 static int tproxy_tg4_check(const struct xt_tgchk_param
*par
)
343 const struct ipt_ip
*i
= par
->entryinfo
;
345 if ((i
->proto
== IPPROTO_TCP
|| i
->proto
== IPPROTO_UDP
)
346 && !(i
->invflags
& IPT_INV_PROTO
))
349 pr_info("Can be used only in combination with "
350 "either -p tcp or -p udp\n");
354 static struct xt_target tproxy_tg_reg
[] __read_mostly
= {
357 .family
= NFPROTO_IPV4
,
359 .target
= tproxy_tg4_v0
,
361 .targetsize
= sizeof(struct xt_tproxy_target_info
),
362 .checkentry
= tproxy_tg4_check
,
363 .hooks
= 1 << NF_INET_PRE_ROUTING
,
368 .family
= NFPROTO_IPV4
,
370 .target
= tproxy_tg4_v1
,
372 .targetsize
= sizeof(struct xt_tproxy_target_info_v1
),
373 .checkentry
= tproxy_tg4_check
,
374 .hooks
= 1 << NF_INET_PRE_ROUTING
,
377 #ifdef XT_TPROXY_HAVE_IPV6
380 .family
= NFPROTO_IPV6
,
382 .target
= tproxy_tg6_v1
,
384 .targetsize
= sizeof(struct xt_tproxy_target_info_v1
),
385 .checkentry
= tproxy_tg6_check
,
386 .hooks
= 1 << NF_INET_PRE_ROUTING
,
393 static int __init
tproxy_tg_init(void)
395 nf_defrag_ipv4_enable();
396 #ifdef XT_TPROXY_HAVE_IPV6
397 nf_defrag_ipv6_enable();
400 return xt_register_targets(tproxy_tg_reg
, ARRAY_SIZE(tproxy_tg_reg
));
403 static void __exit
tproxy_tg_exit(void)
405 xt_unregister_targets(tproxy_tg_reg
, ARRAY_SIZE(tproxy_tg_reg
));
408 module_init(tproxy_tg_init
);
409 module_exit(tproxy_tg_exit
);
410 MODULE_LICENSE("GPL");
411 MODULE_AUTHOR("Balazs Scheidler, Krisztian Kovacs");
412 MODULE_DESCRIPTION("Netfilter transparent proxy (TPROXY) target module.");
413 MODULE_ALIAS("ipt_TPROXY");
414 MODULE_ALIAS("ip6t_TPROXY");