netfilter: x_tables: use NFPROTO_* in extensions
[pandora-kernel.git] / net / ipv4 / netfilter / ipt_REDIRECT.c
1 /* Redirect.  Simple mapping which alters dst to a local IP address. */
2 /* (C) 1999-2001 Paul `Rusty' Russell
3  * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  */
9
10 #include <linux/types.h>
11 #include <linux/ip.h>
12 #include <linux/timer.h>
13 #include <linux/module.h>
14 #include <linux/netfilter.h>
15 #include <linux/netdevice.h>
16 #include <linux/if.h>
17 #include <linux/inetdevice.h>
18 #include <net/protocol.h>
19 #include <net/checksum.h>
20 #include <linux/netfilter_ipv4.h>
21 #include <linux/netfilter/x_tables.h>
22 #include <net/netfilter/nf_nat_rule.h>
23
24 MODULE_LICENSE("GPL");
25 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
26 MODULE_DESCRIPTION("Xtables: Connection redirection to localhost");
27
28 /* FIXME: Take multiple ranges --RR */
29 static bool
30 redirect_tg_check(const char *tablename, const void *e,
31                   const struct xt_target *target, void *targinfo,
32                   unsigned int hook_mask)
33 {
34         const struct nf_nat_multi_range_compat *mr = targinfo;
35
36         if (mr->range[0].flags & IP_NAT_RANGE_MAP_IPS) {
37                 pr_debug("redirect_check: bad MAP_IPS.\n");
38                 return false;
39         }
40         if (mr->rangesize != 1) {
41                 pr_debug("redirect_check: bad rangesize %u.\n", mr->rangesize);
42                 return false;
43         }
44         return true;
45 }
46
47 static unsigned int
48 redirect_tg(struct sk_buff *skb, const struct net_device *in,
49             const struct net_device *out, unsigned int hooknum,
50             const struct xt_target *target, const void *targinfo)
51 {
52         struct nf_conn *ct;
53         enum ip_conntrack_info ctinfo;
54         __be32 newdst;
55         const struct nf_nat_multi_range_compat *mr = targinfo;
56         struct nf_nat_range newrange;
57
58         NF_CT_ASSERT(hooknum == NF_INET_PRE_ROUTING
59                      || hooknum == NF_INET_LOCAL_OUT);
60
61         ct = nf_ct_get(skb, &ctinfo);
62         NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED));
63
64         /* Local packets: make them go to loopback */
65         if (hooknum == NF_INET_LOCAL_OUT)
66                 newdst = htonl(0x7F000001);
67         else {
68                 struct in_device *indev;
69                 struct in_ifaddr *ifa;
70
71                 newdst = 0;
72
73                 rcu_read_lock();
74                 indev = __in_dev_get_rcu(skb->dev);
75                 if (indev && (ifa = indev->ifa_list))
76                         newdst = ifa->ifa_local;
77                 rcu_read_unlock();
78
79                 if (!newdst)
80                         return NF_DROP;
81         }
82
83         /* Transfer from original range. */
84         newrange = ((struct nf_nat_range)
85                 { mr->range[0].flags | IP_NAT_RANGE_MAP_IPS,
86                   newdst, newdst,
87                   mr->range[0].min, mr->range[0].max });
88
89         /* Hand modified range to generic setup. */
90         return nf_nat_setup_info(ct, &newrange, IP_NAT_MANIP_DST);
91 }
92
93 static struct xt_target redirect_tg_reg __read_mostly = {
94         .name           = "REDIRECT",
95         .family         = NFPROTO_IPV4,
96         .target         = redirect_tg,
97         .targetsize     = sizeof(struct nf_nat_multi_range_compat),
98         .table          = "nat",
99         .hooks          = (1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_OUT),
100         .checkentry     = redirect_tg_check,
101         .me             = THIS_MODULE,
102 };
103
104 static int __init redirect_tg_init(void)
105 {
106         return xt_register_target(&redirect_tg_reg);
107 }
108
109 static void __exit redirect_tg_exit(void)
110 {
111         xt_unregister_target(&redirect_tg_reg);
112 }
113
114 module_init(redirect_tg_init);
115 module_exit(redirect_tg_exit);