blob: 640678f47a2ad5420a869e4fbcd63bd677297c2c [file] [log] [blame]
KOVACS Krisztiane8439272008-10-08 11:35:12 +02001/*
2 * Transparent proxy support for Linux/iptables
3 *
Balazs Scheidler6ad78892010-10-21 16:17:26 +02004 * Copyright (c) 2006-2010 BalaBit IT Ltd.
KOVACS Krisztiane8439272008-10-08 11:35:12 +02005 * 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 */
Jan Engelhardtff67e4e2010-03-19 21:08:16 +010012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
KOVACS Krisztiane8439272008-10-08 11:35:12 +020013#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/inet_sock.h>
Balazs Scheidlercc6eb432010-10-21 16:21:10 +020019#include <linux/inetdevice.h>
KOVACS Krisztiane8439272008-10-08 11:35:12 +020020#include <linux/netfilter/x_tables.h>
21#include <linux/netfilter_ipv4/ip_tables.h>
KOVACS Krisztiane8439272008-10-08 11:35:12 +020022
23#include <net/netfilter/ipv4/nf_defrag_ipv4.h>
KOVACS Krisztianf6318e52010-10-24 23:38:32 +000024
25#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
26#define XT_TPROXY_HAVE_IPV6 1
Balazs Scheidlercc6eb432010-10-21 16:21:10 +020027#include <net/if_inet6.h>
28#include <net/addrconf.h>
29#include <linux/netfilter_ipv6/ip6_tables.h>
Balazs Scheidler6ad78892010-10-21 16:17:26 +020030#include <net/netfilter/ipv6/nf_defrag_ipv6.h>
Balazs Scheidlercc6eb432010-10-21 16:21:10 +020031#endif
32
KOVACS Krisztiane8439272008-10-08 11:35:12 +020033#include <net/netfilter/nf_tproxy_core.h>
Balazs Scheidlercc6eb432010-10-21 16:21:10 +020034#include <linux/netfilter/xt_TPROXY.h>
35
36static inline __be32
37tproxy_laddr4(struct sk_buff *skb, __be32 user_laddr, __be32 daddr)
38{
39 struct in_device *indev;
40 __be32 laddr;
41
42 if (user_laddr)
43 return user_laddr;
44
45 laddr = 0;
46 rcu_read_lock();
47 indev = __in_dev_get_rcu(skb->dev);
48 for_primary_ifa(indev) {
49 laddr = ifa->ifa_local;
50 break;
51 } endfor_ifa(indev);
52 rcu_read_unlock();
53
54 return laddr ? laddr : daddr;
55}
KOVACS Krisztiane8439272008-10-08 11:35:12 +020056
Balazs Scheidler106e4c22010-10-21 12:45:14 +020057/**
Balazs Scheidler6ad78892010-10-21 16:17:26 +020058 * tproxy_handle_time_wait4() - handle IPv4 TCP TIME_WAIT reopen redirections
Balazs Scheidler106e4c22010-10-21 12:45:14 +020059 * @skb: The skb being processed.
Balazs Scheidler6ad78892010-10-21 16:17:26 +020060 * @laddr: IPv4 address to redirect to or zero.
61 * @lport: TCP port to redirect to or zero.
Balazs Scheidler106e4c22010-10-21 12:45:14 +020062 * @sk: The TIME_WAIT TCP socket found by the lookup.
63 *
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
67 * socket present.
68 *
Balazs Scheidler6ad78892010-10-21 16:17:26 +020069 * tproxy_handle_time_wait4() consumes the socket reference passed in.
Balazs Scheidler106e4c22010-10-21 12:45:14 +020070 *
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.
73 */
74static struct sock *
Balazs Scheidler6ad78892010-10-21 16:17:26 +020075tproxy_handle_time_wait4(struct sk_buff *skb, __be32 laddr, __be16 lport,
76 struct sock *sk)
Balazs Scheidler106e4c22010-10-21 12:45:14 +020077{
78 const struct iphdr *iph = ip_hdr(skb);
Balazs Scheidler106e4c22010-10-21 12:45:14 +020079 struct tcphdr _hdr, *hp;
80
81 hp = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_hdr), &_hdr);
82 if (hp == NULL) {
83 inet_twsk_put(inet_twsk(sk));
84 return NULL;
85 }
86
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 */
90 struct sock *sk2;
91
92 sk2 = nf_tproxy_get_sock_v4(dev_net(skb->dev), iph->protocol,
Balazs Scheidler6ad78892010-10-21 16:17:26 +020093 iph->saddr, laddr ? laddr : iph->daddr,
94 hp->source, lport ? lport : hp->dest,
95 skb->dev, NFT_LOOKUP_LISTENER);
Balazs Scheidler106e4c22010-10-21 12:45:14 +020096 if (sk2) {
Balazs Scheidler6ad78892010-10-21 16:17:26 +020097 inet_twsk_deschedule(inet_twsk(sk), &tcp_death_row);
98 inet_twsk_put(inet_twsk(sk));
99 sk = sk2;
100 }
101 }
102
103 return sk;
104}
105
Balazs Scheidlercc6eb432010-10-21 16:21:10 +0200106static unsigned int
107tproxy_tg4(struct sk_buff *skb, __be32 laddr, __be16 lport,
108 u_int32_t mark_mask, u_int32_t mark_value)
109{
110 const struct iphdr *iph = ip_hdr(skb);
111 struct udphdr _hdr, *hp;
112 struct sock *sk;
113
114 hp = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_hdr), &_hdr);
115 if (hp == NULL)
116 return NF_DROP;
117
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
121 * connection */
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);
126
127 laddr = tproxy_laddr4(skb, laddr, iph->daddr);
128 if (!lport)
129 lport = hp->dest;
130
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);
135 else if (!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,
139 iph->saddr, laddr,
140 hp->source, lport,
141 skb->dev, NFT_LOOKUP_LISTENER);
142
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;
148
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);
152 return NF_ACCEPT;
153 }
154
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);
158 return NF_DROP;
159}
160
161static unsigned int
162tproxy_tg4_v0(struct sk_buff *skb, const struct xt_action_param *par)
163{
164 const struct xt_tproxy_target_info *tgi = par->targinfo;
165
166 return tproxy_tg4(skb, tgi->laddr, tgi->lport, tgi->mark_mask, tgi->mark_value);
167}
168
169static unsigned int
170tproxy_tg4_v1(struct sk_buff *skb, const struct xt_action_param *par)
171{
172 const struct xt_tproxy_target_info_v1 *tgi = par->targinfo;
173
174 return tproxy_tg4(skb, tgi->laddr.ip, tgi->lport, tgi->mark_mask, tgi->mark_value);
175}
176
KOVACS Krisztianf6318e52010-10-24 23:38:32 +0000177#ifdef XT_TPROXY_HAVE_IPV6
Balazs Scheidlercc6eb432010-10-21 16:21:10 +0200178
179static inline const struct in6_addr *
180tproxy_laddr6(struct sk_buff *skb, const struct in6_addr *user_laddr,
181 const struct in6_addr *daddr)
182{
183 struct inet6_dev *indev;
184 struct inet6_ifaddr *ifa;
185 struct in6_addr *laddr;
186
187 if (!ipv6_addr_any(user_laddr))
188 return user_laddr;
189 laddr = NULL;
190
191 rcu_read_lock();
192 indev = __in6_dev_get(skb->dev);
193 if (indev)
194 list_for_each_entry(ifa, &indev->addr_list, if_list) {
195 if (ifa->flags & (IFA_F_TENTATIVE | IFA_F_DEPRECATED))
196 continue;
197
198 laddr = &ifa->addr;
199 break;
200 }
201 rcu_read_unlock();
202
203 return laddr ? laddr : daddr;
204}
205
Balazs Scheidler6ad78892010-10-21 16:17:26 +0200206/**
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.
213 *
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
217 * socket present.
218 *
219 * tproxy_handle_time_wait6() consumes the socket reference passed in.
220 *
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.
223 */
224static struct sock *
225tproxy_handle_time_wait6(struct sk_buff *skb, int tproto, int thoff,
226 const struct xt_action_param *par,
227 struct sock *sk)
228{
229 const struct ipv6hdr *iph = ipv6_hdr(skb);
230 struct tcphdr _hdr, *hp;
231 const struct xt_tproxy_target_info_v1 *tgi = par->targinfo;
232
233 hp = skb_header_pointer(skb, thoff, sizeof(_hdr), &_hdr);
234 if (hp == NULL) {
235 inet_twsk_put(inet_twsk(sk));
236 return NULL;
237 }
238
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 */
242 struct sock *sk2;
243
244 sk2 = nf_tproxy_get_sock_v6(dev_net(skb->dev), tproto,
245 &iph->saddr,
Balazs Scheidlercc6eb432010-10-21 16:21:10 +0200246 tproxy_laddr6(skb, &tgi->laddr.in6, &iph->daddr),
Balazs Scheidler6ad78892010-10-21 16:17:26 +0200247 hp->source,
248 tgi->lport ? tgi->lport : hp->dest,
249 skb->dev, NFT_LOOKUP_LISTENER);
250 if (sk2) {
Balazs Scheidler106e4c22010-10-21 12:45:14 +0200251 inet_twsk_deschedule(inet_twsk(sk), &tcp_death_row);
252 inet_twsk_put(inet_twsk(sk));
253 sk = sk2;
254 }
255 }
256
257 return sk;
258}
259
KOVACS Krisztiane8439272008-10-08 11:35:12 +0200260static unsigned int
Balazs Scheidler6ad78892010-10-21 16:17:26 +0200261tproxy_tg6_v1(struct sk_buff *skb, const struct xt_action_param *par)
262{
263 const struct ipv6hdr *iph = ipv6_hdr(skb);
264 const struct xt_tproxy_target_info_v1 *tgi = par->targinfo;
265 struct udphdr _hdr, *hp;
266 struct sock *sk;
Balazs Scheidlercc6eb432010-10-21 16:21:10 +0200267 const struct in6_addr *laddr;
268 __be16 lport;
Balazs Scheidler6ad78892010-10-21 16:17:26 +0200269 int thoff;
270 int tproto;
271
272 tproto = ipv6_find_hdr(skb, &thoff, -1, NULL);
273 if (tproto < 0) {
274 pr_debug("unable to find transport header in IPv6 packet, dropping\n");
275 return NF_DROP;
276 }
277
278 hp = skb_header_pointer(skb, thoff, sizeof(_hdr), &_hdr);
279 if (hp == NULL) {
280 pr_debug("unable to grab transport header contents in IPv6 packet, dropping\n");
281 return NF_DROP;
282 }
283
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
287 * connection */
288 sk = nf_tproxy_get_sock_v6(dev_net(skb->dev), tproto,
289 &iph->saddr, &iph->daddr,
290 hp->source, hp->dest,
Balazs Scheidler106e4c22010-10-21 12:45:14 +0200291 par->in, NFT_LOOKUP_ESTABLISHED);
292
Balazs Scheidlercc6eb432010-10-21 16:21:10 +0200293 laddr = tproxy_laddr6(skb, &tgi->laddr.in6, &iph->daddr);
294 lport = tgi->lport ? tgi->lport : hp->dest;
295
Balazs Scheidler106e4c22010-10-21 12:45:14 +0200296 /* UDP has no TCP_TIME_WAIT state, so we never enter here */
297 if (sk && sk->sk_state == TCP_TIME_WAIT)
Balazs Scheidler6ad78892010-10-21 16:17:26 +0200298 /* reopening a TIME_WAIT connection needs special handling */
299 sk = tproxy_handle_time_wait6(skb, tproto, thoff, par, sk);
Balazs Scheidler106e4c22010-10-21 12:45:14 +0200300 else if (!sk)
Balazs Scheidler6ad78892010-10-21 16:17:26 +0200301 /* 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,
Balazs Scheidlercc6eb432010-10-21 16:21:10 +0200304 &iph->saddr, laddr,
305 hp->source, lport,
Balazs Scheidler106e4c22010-10-21 12:45:14 +0200306 par->in, NFT_LOOKUP_LISTENER);
KOVACS Krisztiane8439272008-10-08 11:35:12 +0200307
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;
313
Balazs Scheidler6ad78892010-10-21 16:17:26 +0200314 pr_debug("redirecting: proto %hhu %pI6:%hu -> %pI6:%hu, mark: %x\n",
Balazs Scheidlercc6eb432010-10-21 16:21:10 +0200315 tproto, &iph->saddr, ntohs(hp->source),
316 laddr, ntohs(lport), skb->mark);
KOVACS Krisztiane8439272008-10-08 11:35:12 +0200317 return NF_ACCEPT;
318 }
319
Balazs Scheidler6ad78892010-10-21 16:17:26 +0200320 pr_debug("no socket, dropping: proto %hhu %pI6:%hu -> %pI6:%hu, mark: %x\n",
Balazs Scheidlercc6eb432010-10-21 16:21:10 +0200321 tproto, &iph->saddr, ntohs(hp->source),
322 &iph->daddr, ntohs(hp->dest), skb->mark);
323
KOVACS Krisztiane8439272008-10-08 11:35:12 +0200324 return NF_DROP;
325}
326
Balazs Scheidler6ad78892010-10-21 16:17:26 +0200327static int tproxy_tg6_check(const struct xt_tgchk_param *par)
328{
329 const struct ip6t_ip6 *i = par->entryinfo;
330
331 if ((i->proto == IPPROTO_TCP || i->proto == IPPROTO_UDP)
332 && !(i->flags & IP6T_INV_PROTO))
333 return 0;
334
335 pr_info("Can be used only in combination with "
336 "either -p tcp or -p udp\n");
337 return -EINVAL;
338}
339#endif
340
341static int tproxy_tg4_check(const struct xt_tgchk_param *par)
KOVACS Krisztiane8439272008-10-08 11:35:12 +0200342{
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200343 const struct ipt_ip *i = par->entryinfo;
KOVACS Krisztiane8439272008-10-08 11:35:12 +0200344
345 if ((i->proto == IPPROTO_TCP || i->proto == IPPROTO_UDP)
346 && !(i->invflags & IPT_INV_PROTO))
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100347 return 0;
KOVACS Krisztiane8439272008-10-08 11:35:12 +0200348
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100349 pr_info("Can be used only in combination with "
KOVACS Krisztiane8439272008-10-08 11:35:12 +0200350 "either -p tcp or -p udp\n");
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100351 return -EINVAL;
KOVACS Krisztiane8439272008-10-08 11:35:12 +0200352}
353
Balazs Scheidler6ad78892010-10-21 16:17:26 +0200354static struct xt_target tproxy_tg_reg[] __read_mostly = {
355 {
356 .name = "TPROXY",
357 .family = NFPROTO_IPV4,
358 .table = "mangle",
359 .target = tproxy_tg4_v0,
360 .revision = 0,
361 .targetsize = sizeof(struct xt_tproxy_target_info),
362 .checkentry = tproxy_tg4_check,
363 .hooks = 1 << NF_INET_PRE_ROUTING,
364 .me = THIS_MODULE,
365 },
366 {
367 .name = "TPROXY",
368 .family = NFPROTO_IPV4,
369 .table = "mangle",
370 .target = tproxy_tg4_v1,
371 .revision = 1,
372 .targetsize = sizeof(struct xt_tproxy_target_info_v1),
373 .checkentry = tproxy_tg4_check,
374 .hooks = 1 << NF_INET_PRE_ROUTING,
375 .me = THIS_MODULE,
376 },
KOVACS Krisztianf6318e52010-10-24 23:38:32 +0000377#ifdef XT_TPROXY_HAVE_IPV6
Balazs Scheidler6ad78892010-10-21 16:17:26 +0200378 {
379 .name = "TPROXY",
380 .family = NFPROTO_IPV6,
381 .table = "mangle",
382 .target = tproxy_tg6_v1,
383 .revision = 1,
384 .targetsize = sizeof(struct xt_tproxy_target_info_v1),
385 .checkentry = tproxy_tg6_check,
386 .hooks = 1 << NF_INET_PRE_ROUTING,
387 .me = THIS_MODULE,
388 },
389#endif
390
KOVACS Krisztiane8439272008-10-08 11:35:12 +0200391};
392
393static int __init tproxy_tg_init(void)
394{
395 nf_defrag_ipv4_enable();
KOVACS Krisztianf6318e52010-10-24 23:38:32 +0000396#ifdef XT_TPROXY_HAVE_IPV6
Balazs Scheidler6ad78892010-10-21 16:17:26 +0200397 nf_defrag_ipv6_enable();
398#endif
399
400 return xt_register_targets(tproxy_tg_reg, ARRAY_SIZE(tproxy_tg_reg));
KOVACS Krisztiane8439272008-10-08 11:35:12 +0200401}
402
403static void __exit tproxy_tg_exit(void)
404{
Balazs Scheidler6ad78892010-10-21 16:17:26 +0200405 xt_unregister_targets(tproxy_tg_reg, ARRAY_SIZE(tproxy_tg_reg));
KOVACS Krisztiane8439272008-10-08 11:35:12 +0200406}
407
408module_init(tproxy_tg_init);
409module_exit(tproxy_tg_exit);
410MODULE_LICENSE("GPL");
Balazs Scheidler6ad78892010-10-21 16:17:26 +0200411MODULE_AUTHOR("Balazs Scheidler, Krisztian Kovacs");
KOVACS Krisztiane8439272008-10-08 11:35:12 +0200412MODULE_DESCRIPTION("Netfilter transparent proxy (TPROXY) target module.");
413MODULE_ALIAS("ipt_TPROXY");
Balazs Scheidler6ad78892010-10-21 16:17:26 +0200414MODULE_ALIAS("ip6t_TPROXY");