Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* Redirect. Simple mapping which alters dst to a local IP address. */ |
2 | /* (C) 1999-2001 Paul `Rusty' Russell | |
5b1158e9 | 3 | * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org> |
1da177e4 LT |
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> | |
6709dbbb | 21 | #include <linux/netfilter/x_tables.h> |
5b1158e9 | 22 | #include <net/netfilter/nf_nat_rule.h> |
1da177e4 LT |
23 | |
24 | MODULE_LICENSE("GPL"); | |
25 | MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>"); | |
26 | MODULE_DESCRIPTION("iptables REDIRECT target module"); | |
27 | ||
1da177e4 | 28 | /* FIXME: Take multiple ranges --RR */ |
e1931b78 | 29 | static bool |
d3c5ee6d JE |
30 | redirect_tg_check(const char *tablename, const void *e, |
31 | const struct xt_target *target, void *targinfo, | |
32 | unsigned int hook_mask) | |
1da177e4 | 33 | { |
587aa641 | 34 | const struct nf_nat_multi_range_compat *mr = targinfo; |
1da177e4 | 35 | |
1da177e4 | 36 | if (mr->range[0].flags & IP_NAT_RANGE_MAP_IPS) { |
0d53778e | 37 | pr_debug("redirect_check: bad MAP_IPS.\n"); |
e1931b78 | 38 | return false; |
1da177e4 LT |
39 | } |
40 | if (mr->rangesize != 1) { | |
0d53778e | 41 | pr_debug("redirect_check: bad rangesize %u.\n", mr->rangesize); |
e1931b78 | 42 | return false; |
1da177e4 | 43 | } |
e1931b78 | 44 | return true; |
1da177e4 LT |
45 | } |
46 | ||
47 | static unsigned int | |
d3c5ee6d JE |
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) | |
1da177e4 | 51 | { |
587aa641 | 52 | struct nf_conn *ct; |
1da177e4 | 53 | enum ip_conntrack_info ctinfo; |
a144ea4b | 54 | __be32 newdst; |
587aa641 PM |
55 | const struct nf_nat_multi_range_compat *mr = targinfo; |
56 | struct nf_nat_range newrange; | |
1da177e4 | 57 | |
6e23ae2a PM |
58 | NF_CT_ASSERT(hooknum == NF_INET_PRE_ROUTING |
59 | || hooknum == NF_INET_LOCAL_OUT); | |
1da177e4 | 60 | |
3db05fea | 61 | ct = nf_ct_get(skb, &ctinfo); |
587aa641 | 62 | NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED)); |
1da177e4 LT |
63 | |
64 | /* Local packets: make them go to loopback */ | |
6e23ae2a | 65 | if (hooknum == NF_INET_LOCAL_OUT) |
1da177e4 LT |
66 | newdst = htonl(0x7F000001); |
67 | else { | |
68 | struct in_device *indev; | |
cd0bf2d7 | 69 | struct in_ifaddr *ifa; |
1da177e4 | 70 | |
cd0bf2d7 | 71 | newdst = 0; |
e905a9ed | 72 | |
cd0bf2d7 | 73 | rcu_read_lock(); |
3db05fea | 74 | indev = __in_dev_get_rcu(skb->dev); |
cd0bf2d7 PM |
75 | if (indev && (ifa = indev->ifa_list)) |
76 | newdst = ifa->ifa_local; | |
77 | rcu_read_unlock(); | |
1da177e4 | 78 | |
cd0bf2d7 PM |
79 | if (!newdst) |
80 | return NF_DROP; | |
1da177e4 LT |
81 | } |
82 | ||
83 | /* Transfer from original range. */ | |
587aa641 | 84 | newrange = ((struct nf_nat_range) |
1da177e4 LT |
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. */ | |
587aa641 | 90 | return nf_nat_setup_info(ct, &newrange, hooknum); |
1da177e4 LT |
91 | } |
92 | ||
d3c5ee6d | 93 | static struct xt_target redirect_tg_reg __read_mostly = { |
1da177e4 | 94 | .name = "REDIRECT", |
6709dbbb | 95 | .family = AF_INET, |
d3c5ee6d | 96 | .target = redirect_tg, |
587aa641 | 97 | .targetsize = sizeof(struct nf_nat_multi_range_compat), |
1d5cd909 | 98 | .table = "nat", |
6e23ae2a | 99 | .hooks = (1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_OUT), |
d3c5ee6d | 100 | .checkentry = redirect_tg_check, |
1da177e4 LT |
101 | .me = THIS_MODULE, |
102 | }; | |
103 | ||
d3c5ee6d | 104 | static int __init redirect_tg_init(void) |
1da177e4 | 105 | { |
d3c5ee6d | 106 | return xt_register_target(&redirect_tg_reg); |
1da177e4 LT |
107 | } |
108 | ||
d3c5ee6d | 109 | static void __exit redirect_tg_exit(void) |
1da177e4 | 110 | { |
d3c5ee6d | 111 | xt_unregister_target(&redirect_tg_reg); |
1da177e4 LT |
112 | } |
113 | ||
d3c5ee6d JE |
114 | module_init(redirect_tg_init); |
115 | module_exit(redirect_tg_exit); |