Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Transparent proxy support for Linux/iptables
4 *
5 * Copyright (c) 2006-2010 BalaBit IT Ltd.
6 * Author: Balazs Scheidler, Krisztian Kovacs
7 */
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9#include <linux/module.h>
10#include <linux/skbuff.h>
11#include <linux/ip.h>
12#include <net/checksum.h>
13#include <net/udp.h>
14#include <net/tcp.h>
15#include <net/inet_sock.h>
16#include <net/inet_hashtables.h>
17#include <linux/inetdevice.h>
18#include <linux/netfilter/x_tables.h>
19#include <linux/netfilter_ipv4/ip_tables.h>
20
21#include <net/netfilter/ipv4/nf_defrag_ipv4.h>
22
23#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
24#define XT_TPROXY_HAVE_IPV6 1
25#include <net/if_inet6.h>
26#include <net/addrconf.h>
27#include <net/inet6_hashtables.h>
28#include <linux/netfilter_ipv6/ip6_tables.h>
29#include <net/netfilter/ipv6/nf_defrag_ipv6.h>
30#endif
31
32#include <net/netfilter/nf_tproxy.h>
33#include <linux/netfilter/xt_TPROXY.h>
34
35static unsigned int
36tproxy_tg4(struct net *net, struct sk_buff *skb, __be32 laddr, __be16 lport,
37 u_int32_t mark_mask, u_int32_t mark_value)
38{
39 const struct iphdr *iph = ip_hdr(skb);
40 struct udphdr _hdr, *hp;
41 struct sock *sk;
42
43 hp = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_hdr), &_hdr);
44 if (hp == NULL)
45 return NF_DROP;
46
47 /* check if there's an ongoing connection on the packet
48 * addresses, this happens if the redirect already happened
49 * and the current packet belongs to an already established
50 * connection */
51 sk = nf_tproxy_get_sock_v4(net, skb, iph->protocol,
52 iph->saddr, iph->daddr,
53 hp->source, hp->dest,
54 skb->dev, NF_TPROXY_LOOKUP_ESTABLISHED);
55
56 laddr = nf_tproxy_laddr4(skb, laddr, iph->daddr);
57 if (!lport)
58 lport = hp->dest;
59
60 /* UDP has no TCP_TIME_WAIT state, so we never enter here */
61 if (sk && sk->sk_state == TCP_TIME_WAIT)
62 /* reopening a TIME_WAIT connection needs special handling */
63 sk = nf_tproxy_handle_time_wait4(net, skb, laddr, lport, sk);
64 else if (!sk)
65 /* no, there's no established connection, check if
66 * there's a listener on the redirected addr/port */
67 sk = nf_tproxy_get_sock_v4(net, skb, iph->protocol,
68 iph->saddr, laddr,
69 hp->source, lport,
70 skb->dev, NF_TPROXY_LOOKUP_LISTENER);
71
72 /* NOTE: assign_sock consumes our sk reference */
73 if (sk && nf_tproxy_sk_is_transparent(sk)) {
74 /* This should be in a separate target, but we don't do multiple
75 targets on the same rule yet */
76 skb->mark = (skb->mark & ~mark_mask) ^ mark_value;
77
78 pr_debug("redirecting: proto %hhu %pI4:%hu -> %pI4:%hu, mark: %x\n",
79 iph->protocol, &iph->daddr, ntohs(hp->dest),
80 &laddr, ntohs(lport), skb->mark);
81
82 nf_tproxy_assign_sock(skb, sk);
83 return NF_ACCEPT;
84 }
85
86 pr_debug("no socket, dropping: proto %hhu %pI4:%hu -> %pI4:%hu, mark: %x\n",
87 iph->protocol, &iph->saddr, ntohs(hp->source),
88 &iph->daddr, ntohs(hp->dest), skb->mark);
89 return NF_DROP;
90}
91
92static unsigned int
93tproxy_tg4_v0(struct sk_buff *skb, const struct xt_action_param *par)
94{
95 const struct xt_tproxy_target_info *tgi = par->targinfo;
96
97 return tproxy_tg4(xt_net(par), skb, tgi->laddr, tgi->lport,
98 tgi->mark_mask, tgi->mark_value);
99}
100
101static unsigned int
102tproxy_tg4_v1(struct sk_buff *skb, const struct xt_action_param *par)
103{
104 const struct xt_tproxy_target_info_v1 *tgi = par->targinfo;
105
106 return tproxy_tg4(xt_net(par), skb, tgi->laddr.ip, tgi->lport,
107 tgi->mark_mask, tgi->mark_value);
108}
109
110#ifdef XT_TPROXY_HAVE_IPV6
111
112static unsigned int
113tproxy_tg6_v1(struct sk_buff *skb, const struct xt_action_param *par)
114{
115 const struct ipv6hdr *iph = ipv6_hdr(skb);
116 const struct xt_tproxy_target_info_v1 *tgi = par->targinfo;
117 struct udphdr _hdr, *hp;
118 struct sock *sk;
119 const struct in6_addr *laddr;
120 __be16 lport;
121 int thoff = 0;
122 int tproto;
123
124 tproto = ipv6_find_hdr(skb, &thoff, -1, NULL, NULL);
125 if (tproto < 0) {
126 pr_debug("unable to find transport header in IPv6 packet, dropping\n");
127 return NF_DROP;
128 }
129
130 hp = skb_header_pointer(skb, thoff, sizeof(_hdr), &_hdr);
131 if (hp == NULL) {
132 pr_debug("unable to grab transport header contents in IPv6 packet, dropping\n");
133 return NF_DROP;
134 }
135
136 /* check if there's an ongoing connection on the packet
137 * addresses, this happens if the redirect already happened
138 * and the current packet belongs to an already established
139 * connection */
140 sk = nf_tproxy_get_sock_v6(xt_net(par), skb, thoff, tproto,
141 &iph->saddr, &iph->daddr,
142 hp->source, hp->dest,
143 xt_in(par), NF_TPROXY_LOOKUP_ESTABLISHED);
144
145 laddr = nf_tproxy_laddr6(skb, &tgi->laddr.in6, &iph->daddr);
146 lport = tgi->lport ? tgi->lport : hp->dest;
147
148 /* UDP has no TCP_TIME_WAIT state, so we never enter here */
149 if (sk && sk->sk_state == TCP_TIME_WAIT) {
150 const struct xt_tproxy_target_info_v1 *tgi = par->targinfo;
151 /* reopening a TIME_WAIT connection needs special handling */
152 sk = nf_tproxy_handle_time_wait6(skb, tproto, thoff,
153 xt_net(par),
154 &tgi->laddr.in6,
155 tgi->lport,
156 sk);
157 }
158 else if (!sk)
159 /* no there's no established connection, check if
160 * there's a listener on the redirected addr/port */
161 sk = nf_tproxy_get_sock_v6(xt_net(par), skb, thoff,
162 tproto, &iph->saddr, laddr,
163 hp->source, lport,
164 xt_in(par), NF_TPROXY_LOOKUP_LISTENER);
165
166 /* NOTE: assign_sock consumes our sk reference */
167 if (sk && nf_tproxy_sk_is_transparent(sk)) {
168 /* This should be in a separate target, but we don't do multiple
169 targets on the same rule yet */
170 skb->mark = (skb->mark & ~tgi->mark_mask) ^ tgi->mark_value;
171
172 pr_debug("redirecting: proto %hhu %pI6:%hu -> %pI6:%hu, mark: %x\n",
173 tproto, &iph->saddr, ntohs(hp->source),
174 laddr, ntohs(lport), skb->mark);
175
176 nf_tproxy_assign_sock(skb, sk);
177 return NF_ACCEPT;
178 }
179
180 pr_debug("no socket, dropping: proto %hhu %pI6:%hu -> %pI6:%hu, mark: %x\n",
181 tproto, &iph->saddr, ntohs(hp->source),
182 &iph->daddr, ntohs(hp->dest), skb->mark);
183
184 return NF_DROP;
185}
186
187static int tproxy_tg6_check(const struct xt_tgchk_param *par)
188{
189 const struct ip6t_ip6 *i = par->entryinfo;
190 int err;
191
192 err = nf_defrag_ipv6_enable(par->net);
193 if (err)
194 return err;
195
196 if ((i->proto == IPPROTO_TCP || i->proto == IPPROTO_UDP) &&
197 !(i->invflags & IP6T_INV_PROTO))
198 return 0;
199
200 pr_info_ratelimited("Can be used only with -p tcp or -p udp\n");
201 return -EINVAL;
202}
203
204static void tproxy_tg6_destroy(const struct xt_tgdtor_param *par)
205{
206 nf_defrag_ipv6_disable(par->net);
207}
208#endif
209
210static int tproxy_tg4_check(const struct xt_tgchk_param *par)
211{
212 const struct ipt_ip *i = par->entryinfo;
213 int err;
214
215 err = nf_defrag_ipv4_enable(par->net);
216 if (err)
217 return err;
218
219 if ((i->proto == IPPROTO_TCP || i->proto == IPPROTO_UDP)
220 && !(i->invflags & IPT_INV_PROTO))
221 return 0;
222
223 pr_info_ratelimited("Can be used only with -p tcp or -p udp\n");
224 return -EINVAL;
225}
226
227static void tproxy_tg4_destroy(const struct xt_tgdtor_param *par)
228{
229 nf_defrag_ipv4_disable(par->net);
230}
231
232static struct xt_target tproxy_tg_reg[] __read_mostly = {
233 {
234 .name = "TPROXY",
235 .family = NFPROTO_IPV4,
236 .table = "mangle",
237 .target = tproxy_tg4_v0,
238 .revision = 0,
239 .targetsize = sizeof(struct xt_tproxy_target_info),
240 .checkentry = tproxy_tg4_check,
241 .destroy = tproxy_tg4_destroy,
242 .hooks = 1 << NF_INET_PRE_ROUTING,
243 .me = THIS_MODULE,
244 },
245 {
246 .name = "TPROXY",
247 .family = NFPROTO_IPV4,
248 .table = "mangle",
249 .target = tproxy_tg4_v1,
250 .revision = 1,
251 .targetsize = sizeof(struct xt_tproxy_target_info_v1),
252 .checkentry = tproxy_tg4_check,
253 .destroy = tproxy_tg4_destroy,
254 .hooks = 1 << NF_INET_PRE_ROUTING,
255 .me = THIS_MODULE,
256 },
257#ifdef XT_TPROXY_HAVE_IPV6
258 {
259 .name = "TPROXY",
260 .family = NFPROTO_IPV6,
261 .table = "mangle",
262 .target = tproxy_tg6_v1,
263 .revision = 1,
264 .targetsize = sizeof(struct xt_tproxy_target_info_v1),
265 .checkentry = tproxy_tg6_check,
266 .destroy = tproxy_tg6_destroy,
267 .hooks = 1 << NF_INET_PRE_ROUTING,
268 .me = THIS_MODULE,
269 },
270#endif
271
272};
273
274static int __init tproxy_tg_init(void)
275{
276 return xt_register_targets(tproxy_tg_reg, ARRAY_SIZE(tproxy_tg_reg));
277}
278
279static void __exit tproxy_tg_exit(void)
280{
281 xt_unregister_targets(tproxy_tg_reg, ARRAY_SIZE(tproxy_tg_reg));
282}
283
284module_init(tproxy_tg_init);
285module_exit(tproxy_tg_exit);
286MODULE_LICENSE("GPL");
287MODULE_AUTHOR("Balazs Scheidler, Krisztian Kovacs");
288MODULE_DESCRIPTION("Netfilter transparent proxy (TPROXY) target module.");
289MODULE_ALIAS("ipt_TPROXY");
290MODULE_ALIAS("ip6t_TPROXY");
1/*
2 * Transparent proxy support for Linux/iptables
3 *
4 * Copyright (c) 2006-2010 BalaBit IT Ltd.
5 * Author: Balazs Scheidler, Krisztian Kovacs
6 *
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.
10 *
11 */
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13#include <linux/module.h>
14#include <linux/skbuff.h>
15#include <linux/ip.h>
16#include <net/checksum.h>
17#include <net/udp.h>
18#include <net/tcp.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>
24
25#include <net/netfilter/ipv4/nf_defrag_ipv4.h>
26
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>
34#endif
35
36#include <linux/netfilter/xt_TPROXY.h>
37
38enum nf_tproxy_lookup_t {
39 NFT_LOOKUP_LISTENER,
40 NFT_LOOKUP_ESTABLISHED,
41};
42
43static bool tproxy_sk_is_transparent(struct sock *sk)
44{
45 switch (sk->sk_state) {
46 case TCP_TIME_WAIT:
47 if (inet_twsk(sk)->tw_transparent)
48 return true;
49 break;
50 case TCP_NEW_SYN_RECV:
51 if (inet_rsk(inet_reqsk(sk))->no_srccheck)
52 return true;
53 break;
54 default:
55 if (inet_sk(sk)->transparent)
56 return true;
57 }
58
59 sock_gen_put(sk);
60 return false;
61}
62
63static inline __be32
64tproxy_laddr4(struct sk_buff *skb, __be32 user_laddr, __be32 daddr)
65{
66 struct in_device *indev;
67 __be32 laddr;
68
69 if (user_laddr)
70 return user_laddr;
71
72 laddr = 0;
73 indev = __in_dev_get_rcu(skb->dev);
74 for_primary_ifa(indev) {
75 laddr = ifa->ifa_local;
76 break;
77 } endfor_ifa(indev);
78
79 return laddr ? laddr : daddr;
80}
81
82/*
83 * This is used when the user wants to intercept a connection matching
84 * an explicit iptables rule. In this case the sockets are assumed
85 * matching in preference order:
86 *
87 * - match: if there's a fully established connection matching the
88 * _packet_ tuple, it is returned, assuming the redirection
89 * already took place and we process a packet belonging to an
90 * established connection
91 *
92 * - match: if there's a listening socket matching the redirection
93 * (e.g. on-port & on-ip of the connection), it is returned,
94 * regardless if it was bound to 0.0.0.0 or an explicit
95 * address. The reasoning is that if there's an explicit rule, it
96 * does not really matter if the listener is bound to an interface
97 * or to 0. The user already stated that he wants redirection
98 * (since he added the rule).
99 *
100 * Please note that there's an overlap between what a TPROXY target
101 * and a socket match will match. Normally if you have both rules the
102 * "socket" match will be the first one, effectively all packets
103 * belonging to established connections going through that one.
104 */
105static inline struct sock *
106nf_tproxy_get_sock_v4(struct net *net, struct sk_buff *skb, void *hp,
107 const u8 protocol,
108 const __be32 saddr, const __be32 daddr,
109 const __be16 sport, const __be16 dport,
110 const struct net_device *in,
111 const enum nf_tproxy_lookup_t lookup_type)
112{
113 struct sock *sk;
114 struct tcphdr *tcph;
115
116 switch (protocol) {
117 case IPPROTO_TCP:
118 switch (lookup_type) {
119 case NFT_LOOKUP_LISTENER:
120 tcph = hp;
121 sk = inet_lookup_listener(net, &tcp_hashinfo, skb,
122 ip_hdrlen(skb) +
123 __tcp_hdrlen(tcph),
124 saddr, sport,
125 daddr, dport,
126 in->ifindex, 0);
127
128 if (sk && !refcount_inc_not_zero(&sk->sk_refcnt))
129 sk = NULL;
130 /* NOTE: we return listeners even if bound to
131 * 0.0.0.0, those are filtered out in
132 * xt_socket, since xt_TPROXY needs 0 bound
133 * listeners too
134 */
135 break;
136 case NFT_LOOKUP_ESTABLISHED:
137 sk = inet_lookup_established(net, &tcp_hashinfo,
138 saddr, sport, daddr, dport,
139 in->ifindex);
140 break;
141 default:
142 BUG();
143 }
144 break;
145 case IPPROTO_UDP:
146 sk = udp4_lib_lookup(net, saddr, sport, daddr, dport,
147 in->ifindex);
148 if (sk) {
149 int connected = (sk->sk_state == TCP_ESTABLISHED);
150 int wildcard = (inet_sk(sk)->inet_rcv_saddr == 0);
151
152 /* NOTE: we return listeners even if bound to
153 * 0.0.0.0, those are filtered out in
154 * xt_socket, since xt_TPROXY needs 0 bound
155 * listeners too
156 */
157 if ((lookup_type == NFT_LOOKUP_ESTABLISHED && (!connected || wildcard)) ||
158 (lookup_type == NFT_LOOKUP_LISTENER && connected)) {
159 sock_put(sk);
160 sk = NULL;
161 }
162 }
163 break;
164 default:
165 WARN_ON(1);
166 sk = NULL;
167 }
168
169 pr_debug("tproxy socket lookup: proto %u %08x:%u -> %08x:%u, lookup type: %d, sock %p\n",
170 protocol, ntohl(saddr), ntohs(sport), ntohl(daddr), ntohs(dport), lookup_type, sk);
171
172 return sk;
173}
174
175#ifdef XT_TPROXY_HAVE_IPV6
176static inline struct sock *
177nf_tproxy_get_sock_v6(struct net *net, struct sk_buff *skb, int thoff, void *hp,
178 const u8 protocol,
179 const struct in6_addr *saddr, const struct in6_addr *daddr,
180 const __be16 sport, const __be16 dport,
181 const struct net_device *in,
182 const enum nf_tproxy_lookup_t lookup_type)
183{
184 struct sock *sk;
185 struct tcphdr *tcph;
186
187 switch (protocol) {
188 case IPPROTO_TCP:
189 switch (lookup_type) {
190 case NFT_LOOKUP_LISTENER:
191 tcph = hp;
192 sk = inet6_lookup_listener(net, &tcp_hashinfo, skb,
193 thoff + __tcp_hdrlen(tcph),
194 saddr, sport,
195 daddr, ntohs(dport),
196 in->ifindex, 0);
197
198 if (sk && !refcount_inc_not_zero(&sk->sk_refcnt))
199 sk = NULL;
200 /* NOTE: we return listeners even if bound to
201 * 0.0.0.0, those are filtered out in
202 * xt_socket, since xt_TPROXY needs 0 bound
203 * listeners too
204 */
205 break;
206 case NFT_LOOKUP_ESTABLISHED:
207 sk = __inet6_lookup_established(net, &tcp_hashinfo,
208 saddr, sport, daddr, ntohs(dport),
209 in->ifindex, 0);
210 break;
211 default:
212 BUG();
213 }
214 break;
215 case IPPROTO_UDP:
216 sk = udp6_lib_lookup(net, saddr, sport, daddr, dport,
217 in->ifindex);
218 if (sk) {
219 int connected = (sk->sk_state == TCP_ESTABLISHED);
220 int wildcard = ipv6_addr_any(&sk->sk_v6_rcv_saddr);
221
222 /* NOTE: we return listeners even if bound to
223 * 0.0.0.0, those are filtered out in
224 * xt_socket, since xt_TPROXY needs 0 bound
225 * listeners too
226 */
227 if ((lookup_type == NFT_LOOKUP_ESTABLISHED && (!connected || wildcard)) ||
228 (lookup_type == NFT_LOOKUP_LISTENER && connected)) {
229 sock_put(sk);
230 sk = NULL;
231 }
232 }
233 break;
234 default:
235 WARN_ON(1);
236 sk = NULL;
237 }
238
239 pr_debug("tproxy socket lookup: proto %u %pI6:%u -> %pI6:%u, lookup type: %d, sock %p\n",
240 protocol, saddr, ntohs(sport), daddr, ntohs(dport), lookup_type, sk);
241
242 return sk;
243}
244#endif
245
246/**
247 * tproxy_handle_time_wait4 - handle IPv4 TCP TIME_WAIT reopen redirections
248 * @skb: The skb being processed.
249 * @laddr: IPv4 address to redirect to or zero.
250 * @lport: TCP port to redirect to or zero.
251 * @sk: The TIME_WAIT TCP socket found by the lookup.
252 *
253 * We have to handle SYN packets arriving to TIME_WAIT sockets
254 * differently: instead of reopening the connection we should rather
255 * redirect the new connection to the proxy if there's a listener
256 * socket present.
257 *
258 * tproxy_handle_time_wait4() consumes the socket reference passed in.
259 *
260 * Returns the listener socket if there's one, the TIME_WAIT socket if
261 * no such listener is found, or NULL if the TCP header is incomplete.
262 */
263static struct sock *
264tproxy_handle_time_wait4(struct net *net, struct sk_buff *skb,
265 __be32 laddr, __be16 lport, struct sock *sk)
266{
267 const struct iphdr *iph = ip_hdr(skb);
268 struct tcphdr _hdr, *hp;
269
270 hp = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_hdr), &_hdr);
271 if (hp == NULL) {
272 inet_twsk_put(inet_twsk(sk));
273 return NULL;
274 }
275
276 if (hp->syn && !hp->rst && !hp->ack && !hp->fin) {
277 /* SYN to a TIME_WAIT socket, we'd rather redirect it
278 * to a listener socket if there's one */
279 struct sock *sk2;
280
281 sk2 = nf_tproxy_get_sock_v4(net, skb, hp, iph->protocol,
282 iph->saddr, laddr ? laddr : iph->daddr,
283 hp->source, lport ? lport : hp->dest,
284 skb->dev, NFT_LOOKUP_LISTENER);
285 if (sk2) {
286 inet_twsk_deschedule_put(inet_twsk(sk));
287 sk = sk2;
288 }
289 }
290
291 return sk;
292}
293
294/* assign a socket to the skb -- consumes sk */
295static void
296nf_tproxy_assign_sock(struct sk_buff *skb, struct sock *sk)
297{
298 skb_orphan(skb);
299 skb->sk = sk;
300 skb->destructor = sock_edemux;
301}
302
303static unsigned int
304tproxy_tg4(struct net *net, struct sk_buff *skb, __be32 laddr, __be16 lport,
305 u_int32_t mark_mask, u_int32_t mark_value)
306{
307 const struct iphdr *iph = ip_hdr(skb);
308 struct udphdr _hdr, *hp;
309 struct sock *sk;
310
311 hp = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_hdr), &_hdr);
312 if (hp == NULL)
313 return NF_DROP;
314
315 /* check if there's an ongoing connection on the packet
316 * addresses, this happens if the redirect already happened
317 * and the current packet belongs to an already established
318 * connection */
319 sk = nf_tproxy_get_sock_v4(net, skb, hp, iph->protocol,
320 iph->saddr, iph->daddr,
321 hp->source, hp->dest,
322 skb->dev, NFT_LOOKUP_ESTABLISHED);
323
324 laddr = tproxy_laddr4(skb, laddr, iph->daddr);
325 if (!lport)
326 lport = hp->dest;
327
328 /* UDP has no TCP_TIME_WAIT state, so we never enter here */
329 if (sk && sk->sk_state == TCP_TIME_WAIT)
330 /* reopening a TIME_WAIT connection needs special handling */
331 sk = tproxy_handle_time_wait4(net, skb, laddr, lport, sk);
332 else if (!sk)
333 /* no, there's no established connection, check if
334 * there's a listener on the redirected addr/port */
335 sk = nf_tproxy_get_sock_v4(net, skb, hp, iph->protocol,
336 iph->saddr, laddr,
337 hp->source, lport,
338 skb->dev, NFT_LOOKUP_LISTENER);
339
340 /* NOTE: assign_sock consumes our sk reference */
341 if (sk && tproxy_sk_is_transparent(sk)) {
342 /* This should be in a separate target, but we don't do multiple
343 targets on the same rule yet */
344 skb->mark = (skb->mark & ~mark_mask) ^ mark_value;
345
346 pr_debug("redirecting: proto %hhu %pI4:%hu -> %pI4:%hu, mark: %x\n",
347 iph->protocol, &iph->daddr, ntohs(hp->dest),
348 &laddr, ntohs(lport), skb->mark);
349
350 nf_tproxy_assign_sock(skb, sk);
351 return NF_ACCEPT;
352 }
353
354 pr_debug("no socket, dropping: proto %hhu %pI4:%hu -> %pI4:%hu, mark: %x\n",
355 iph->protocol, &iph->saddr, ntohs(hp->source),
356 &iph->daddr, ntohs(hp->dest), skb->mark);
357 return NF_DROP;
358}
359
360static unsigned int
361tproxy_tg4_v0(struct sk_buff *skb, const struct xt_action_param *par)
362{
363 const struct xt_tproxy_target_info *tgi = par->targinfo;
364
365 return tproxy_tg4(xt_net(par), skb, tgi->laddr, tgi->lport,
366 tgi->mark_mask, tgi->mark_value);
367}
368
369static unsigned int
370tproxy_tg4_v1(struct sk_buff *skb, const struct xt_action_param *par)
371{
372 const struct xt_tproxy_target_info_v1 *tgi = par->targinfo;
373
374 return tproxy_tg4(xt_net(par), skb, tgi->laddr.ip, tgi->lport,
375 tgi->mark_mask, tgi->mark_value);
376}
377
378#ifdef XT_TPROXY_HAVE_IPV6
379
380static inline const struct in6_addr *
381tproxy_laddr6(struct sk_buff *skb, const struct in6_addr *user_laddr,
382 const struct in6_addr *daddr)
383{
384 struct inet6_dev *indev;
385 struct inet6_ifaddr *ifa;
386 struct in6_addr *laddr;
387
388 if (!ipv6_addr_any(user_laddr))
389 return user_laddr;
390 laddr = NULL;
391
392 indev = __in6_dev_get(skb->dev);
393 if (indev) {
394 read_lock_bh(&indev->lock);
395 list_for_each_entry(ifa, &indev->addr_list, if_list) {
396 if (ifa->flags & (IFA_F_TENTATIVE | IFA_F_DEPRECATED))
397 continue;
398
399 laddr = &ifa->addr;
400 break;
401 }
402 read_unlock_bh(&indev->lock);
403 }
404
405 return laddr ? laddr : daddr;
406}
407
408/**
409 * tproxy_handle_time_wait6 - handle IPv6 TCP TIME_WAIT reopen redirections
410 * @skb: The skb being processed.
411 * @tproto: Transport protocol.
412 * @thoff: Transport protocol header offset.
413 * @par: Iptables target parameters.
414 * @sk: The TIME_WAIT TCP socket found by the lookup.
415 *
416 * We have to handle SYN packets arriving to TIME_WAIT sockets
417 * differently: instead of reopening the connection we should rather
418 * redirect the new connection to the proxy if there's a listener
419 * socket present.
420 *
421 * tproxy_handle_time_wait6() consumes the socket reference passed in.
422 *
423 * Returns the listener socket if there's one, the TIME_WAIT socket if
424 * no such listener is found, or NULL if the TCP header is incomplete.
425 */
426static struct sock *
427tproxy_handle_time_wait6(struct sk_buff *skb, int tproto, int thoff,
428 const struct xt_action_param *par,
429 struct sock *sk)
430{
431 const struct ipv6hdr *iph = ipv6_hdr(skb);
432 struct tcphdr _hdr, *hp;
433 const struct xt_tproxy_target_info_v1 *tgi = par->targinfo;
434
435 hp = skb_header_pointer(skb, thoff, sizeof(_hdr), &_hdr);
436 if (hp == NULL) {
437 inet_twsk_put(inet_twsk(sk));
438 return NULL;
439 }
440
441 if (hp->syn && !hp->rst && !hp->ack && !hp->fin) {
442 /* SYN to a TIME_WAIT socket, we'd rather redirect it
443 * to a listener socket if there's one */
444 struct sock *sk2;
445
446 sk2 = nf_tproxy_get_sock_v6(xt_net(par), skb, thoff, hp, tproto,
447 &iph->saddr,
448 tproxy_laddr6(skb, &tgi->laddr.in6, &iph->daddr),
449 hp->source,
450 tgi->lport ? tgi->lport : hp->dest,
451 skb->dev, NFT_LOOKUP_LISTENER);
452 if (sk2) {
453 inet_twsk_deschedule_put(inet_twsk(sk));
454 sk = sk2;
455 }
456 }
457
458 return sk;
459}
460
461static unsigned int
462tproxy_tg6_v1(struct sk_buff *skb, const struct xt_action_param *par)
463{
464 const struct ipv6hdr *iph = ipv6_hdr(skb);
465 const struct xt_tproxy_target_info_v1 *tgi = par->targinfo;
466 struct udphdr _hdr, *hp;
467 struct sock *sk;
468 const struct in6_addr *laddr;
469 __be16 lport;
470 int thoff = 0;
471 int tproto;
472
473 tproto = ipv6_find_hdr(skb, &thoff, -1, NULL, NULL);
474 if (tproto < 0) {
475 pr_debug("unable to find transport header in IPv6 packet, dropping\n");
476 return NF_DROP;
477 }
478
479 hp = skb_header_pointer(skb, thoff, sizeof(_hdr), &_hdr);
480 if (hp == NULL) {
481 pr_debug("unable to grab transport header contents in IPv6 packet, dropping\n");
482 return NF_DROP;
483 }
484
485 /* check if there's an ongoing connection on the packet
486 * addresses, this happens if the redirect already happened
487 * and the current packet belongs to an already established
488 * connection */
489 sk = nf_tproxy_get_sock_v6(xt_net(par), skb, thoff, hp, tproto,
490 &iph->saddr, &iph->daddr,
491 hp->source, hp->dest,
492 xt_in(par), NFT_LOOKUP_ESTABLISHED);
493
494 laddr = tproxy_laddr6(skb, &tgi->laddr.in6, &iph->daddr);
495 lport = tgi->lport ? tgi->lport : hp->dest;
496
497 /* UDP has no TCP_TIME_WAIT state, so we never enter here */
498 if (sk && sk->sk_state == TCP_TIME_WAIT)
499 /* reopening a TIME_WAIT connection needs special handling */
500 sk = tproxy_handle_time_wait6(skb, tproto, thoff, par, sk);
501 else if (!sk)
502 /* no there's no established connection, check if
503 * there's a listener on the redirected addr/port */
504 sk = nf_tproxy_get_sock_v6(xt_net(par), skb, thoff, hp,
505 tproto, &iph->saddr, laddr,
506 hp->source, lport,
507 xt_in(par), NFT_LOOKUP_LISTENER);
508
509 /* NOTE: assign_sock consumes our sk reference */
510 if (sk && tproxy_sk_is_transparent(sk)) {
511 /* This should be in a separate target, but we don't do multiple
512 targets on the same rule yet */
513 skb->mark = (skb->mark & ~tgi->mark_mask) ^ tgi->mark_value;
514
515 pr_debug("redirecting: proto %hhu %pI6:%hu -> %pI6:%hu, mark: %x\n",
516 tproto, &iph->saddr, ntohs(hp->source),
517 laddr, ntohs(lport), skb->mark);
518
519 nf_tproxy_assign_sock(skb, sk);
520 return NF_ACCEPT;
521 }
522
523 pr_debug("no socket, dropping: proto %hhu %pI6:%hu -> %pI6:%hu, mark: %x\n",
524 tproto, &iph->saddr, ntohs(hp->source),
525 &iph->daddr, ntohs(hp->dest), skb->mark);
526
527 return NF_DROP;
528}
529
530static int tproxy_tg6_check(const struct xt_tgchk_param *par)
531{
532 const struct ip6t_ip6 *i = par->entryinfo;
533 int err;
534
535 err = nf_defrag_ipv6_enable(par->net);
536 if (err)
537 return err;
538
539 if ((i->proto == IPPROTO_TCP || i->proto == IPPROTO_UDP) &&
540 !(i->invflags & IP6T_INV_PROTO))
541 return 0;
542
543 pr_info_ratelimited("Can be used only with -p tcp or -p udp\n");
544 return -EINVAL;
545}
546#endif
547
548static int tproxy_tg4_check(const struct xt_tgchk_param *par)
549{
550 const struct ipt_ip *i = par->entryinfo;
551 int err;
552
553 err = nf_defrag_ipv4_enable(par->net);
554 if (err)
555 return err;
556
557 if ((i->proto == IPPROTO_TCP || i->proto == IPPROTO_UDP)
558 && !(i->invflags & IPT_INV_PROTO))
559 return 0;
560
561 pr_info_ratelimited("Can be used only with -p tcp or -p udp\n");
562 return -EINVAL;
563}
564
565static struct xt_target tproxy_tg_reg[] __read_mostly = {
566 {
567 .name = "TPROXY",
568 .family = NFPROTO_IPV4,
569 .table = "mangle",
570 .target = tproxy_tg4_v0,
571 .revision = 0,
572 .targetsize = sizeof(struct xt_tproxy_target_info),
573 .checkentry = tproxy_tg4_check,
574 .hooks = 1 << NF_INET_PRE_ROUTING,
575 .me = THIS_MODULE,
576 },
577 {
578 .name = "TPROXY",
579 .family = NFPROTO_IPV4,
580 .table = "mangle",
581 .target = tproxy_tg4_v1,
582 .revision = 1,
583 .targetsize = sizeof(struct xt_tproxy_target_info_v1),
584 .checkentry = tproxy_tg4_check,
585 .hooks = 1 << NF_INET_PRE_ROUTING,
586 .me = THIS_MODULE,
587 },
588#ifdef XT_TPROXY_HAVE_IPV6
589 {
590 .name = "TPROXY",
591 .family = NFPROTO_IPV6,
592 .table = "mangle",
593 .target = tproxy_tg6_v1,
594 .revision = 1,
595 .targetsize = sizeof(struct xt_tproxy_target_info_v1),
596 .checkentry = tproxy_tg6_check,
597 .hooks = 1 << NF_INET_PRE_ROUTING,
598 .me = THIS_MODULE,
599 },
600#endif
601
602};
603
604static int __init tproxy_tg_init(void)
605{
606 return xt_register_targets(tproxy_tg_reg, ARRAY_SIZE(tproxy_tg_reg));
607}
608
609static void __exit tproxy_tg_exit(void)
610{
611 xt_unregister_targets(tproxy_tg_reg, ARRAY_SIZE(tproxy_tg_reg));
612}
613
614module_init(tproxy_tg_init);
615module_exit(tproxy_tg_exit);
616MODULE_LICENSE("GPL");
617MODULE_AUTHOR("Balazs Scheidler, Krisztian Kovacs");
618MODULE_DESCRIPTION("Netfilter transparent proxy (TPROXY) target module.");
619MODULE_ALIAS("ipt_TPROXY");
620MODULE_ALIAS("ip6t_TPROXY");