ARM: fixup SMP alternatives in modules
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / netfilter / xt_TPROXY.c
blob640678f47a2ad5420a869e4fbcd63bd677297c2c
1 /*
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>
15 #include <linux/ip.h>
16 #include <net/checksum.h>
17 #include <net/udp.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>
31 #endif
33 #include <net/netfilter/nf_tproxy_core.h>
34 #include <linux/netfilter/xt_TPROXY.h>
36 static inline __be32
37 tproxy_laddr4(struct sk_buff *skb, __be32 user_laddr, __be32 daddr)
39 struct in_device *indev;
40 __be32 laddr;
42 if (user_laddr)
43 return user_laddr;
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();
54 return laddr ? laddr : daddr;
57 /**
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
67 * socket present.
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.
74 static struct sock *
75 tproxy_handle_time_wait4(struct sk_buff *skb, __be32 laddr, __be16 lport,
76 struct sock *sk)
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);
82 if (hp == NULL) {
83 inet_twsk_put(inet_twsk(sk));
84 return NULL;
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;
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);
96 if (sk2) {
97 inet_twsk_deschedule(inet_twsk(sk), &tcp_death_row);
98 inet_twsk_put(inet_twsk(sk));
99 sk = sk2;
103 return sk;
106 static unsigned int
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;
112 struct sock *sk;
114 hp = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_hdr), &_hdr);
115 if (hp == NULL)
116 return NF_DROP;
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);
127 laddr = tproxy_laddr4(skb, laddr, iph->daddr);
128 if (!lport)
129 lport = hp->dest;
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);
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);
152 return NF_ACCEPT;
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;
161 static unsigned int
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);
169 static unsigned int
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))
188 return user_laddr;
189 laddr = NULL;
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;
198 laddr = &ifa->addr;
199 break;
201 rcu_read_unlock();
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
217 * socket present.
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.
224 static struct sock *
225 tproxy_handle_time_wait6(struct sk_buff *skb, int tproto, int thoff,
226 const struct xt_action_param *par,
227 struct sock *sk)
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);
234 if (hp == NULL) {
235 inet_twsk_put(inet_twsk(sk));
236 return NULL;
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;
244 sk2 = nf_tproxy_get_sock_v6(dev_net(skb->dev), tproto,
245 &iph->saddr,
246 tproxy_laddr6(skb, &tgi->laddr.in6, &iph->daddr),
247 hp->source,
248 tgi->lport ? tgi->lport : hp->dest,
249 skb->dev, NFT_LOOKUP_LISTENER);
250 if (sk2) {
251 inet_twsk_deschedule(inet_twsk(sk), &tcp_death_row);
252 inet_twsk_put(inet_twsk(sk));
253 sk = sk2;
257 return sk;
260 static unsigned int
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;
266 struct sock *sk;
267 const struct in6_addr *laddr;
268 __be16 lport;
269 int thoff;
270 int tproto;
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;
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;
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,
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);
300 else if (!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,
304 &iph->saddr, laddr,
305 hp->source, lport,
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);
317 return NF_ACCEPT;
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);
324 return NF_DROP;
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))
333 return 0;
335 pr_info("Can be used only in combination with "
336 "either -p tcp or -p udp\n");
337 return -EINVAL;
339 #endif
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))
347 return 0;
349 pr_info("Can be used only in combination with "
350 "either -p tcp or -p udp\n");
351 return -EINVAL;
354 static struct xt_target tproxy_tg_reg[] __read_mostly = {
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,
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,
377 #ifdef XT_TPROXY_HAVE_IPV6
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,
389 #endif
393 static int __init tproxy_tg_init(void)
395 nf_defrag_ipv4_enable();
396 #ifdef XT_TPROXY_HAVE_IPV6
397 nf_defrag_ipv6_enable();
398 #endif
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");