]>
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 | ||
28 | #if 0 | |
29 | #define DEBUGP printk | |
30 | #else | |
31 | #define DEBUGP(format, args...) | |
32 | #endif | |
33 | ||
34 | /* FIXME: Take multiple ranges --RR */ | |
e1931b78 | 35 | static bool |
1da177e4 | 36 | redirect_check(const char *tablename, |
2e4e6a17 | 37 | const void *e, |
c4986734 | 38 | const struct xt_target *target, |
1da177e4 | 39 | void *targinfo, |
1da177e4 LT |
40 | unsigned int hook_mask) |
41 | { | |
587aa641 | 42 | const struct nf_nat_multi_range_compat *mr = targinfo; |
1da177e4 | 43 | |
1da177e4 LT |
44 | if (mr->range[0].flags & IP_NAT_RANGE_MAP_IPS) { |
45 | DEBUGP("redirect_check: bad MAP_IPS.\n"); | |
e1931b78 | 46 | return false; |
1da177e4 LT |
47 | } |
48 | if (mr->rangesize != 1) { | |
49 | DEBUGP("redirect_check: bad rangesize %u.\n", mr->rangesize); | |
e1931b78 | 50 | return false; |
1da177e4 | 51 | } |
e1931b78 | 52 | return true; |
1da177e4 LT |
53 | } |
54 | ||
55 | static unsigned int | |
56 | redirect_target(struct sk_buff **pskb, | |
57 | const struct net_device *in, | |
58 | const struct net_device *out, | |
59 | unsigned int hooknum, | |
c4986734 | 60 | const struct xt_target *target, |
fe1cb108 | 61 | const void *targinfo) |
1da177e4 | 62 | { |
587aa641 | 63 | struct nf_conn *ct; |
1da177e4 | 64 | enum ip_conntrack_info ctinfo; |
a144ea4b | 65 | __be32 newdst; |
587aa641 PM |
66 | const struct nf_nat_multi_range_compat *mr = targinfo; |
67 | struct nf_nat_range newrange; | |
1da177e4 | 68 | |
587aa641 | 69 | NF_CT_ASSERT(hooknum == NF_IP_PRE_ROUTING |
1da177e4 LT |
70 | || hooknum == NF_IP_LOCAL_OUT); |
71 | ||
587aa641 PM |
72 | ct = nf_ct_get(*pskb, &ctinfo); |
73 | NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED)); | |
1da177e4 LT |
74 | |
75 | /* Local packets: make them go to loopback */ | |
76 | if (hooknum == NF_IP_LOCAL_OUT) | |
77 | newdst = htonl(0x7F000001); | |
78 | else { | |
79 | struct in_device *indev; | |
cd0bf2d7 | 80 | struct in_ifaddr *ifa; |
1da177e4 | 81 | |
cd0bf2d7 | 82 | newdst = 0; |
e905a9ed | 83 | |
cd0bf2d7 | 84 | rcu_read_lock(); |
e5ed6399 | 85 | indev = __in_dev_get_rcu((*pskb)->dev); |
cd0bf2d7 PM |
86 | if (indev && (ifa = indev->ifa_list)) |
87 | newdst = ifa->ifa_local; | |
88 | rcu_read_unlock(); | |
1da177e4 | 89 | |
cd0bf2d7 PM |
90 | if (!newdst) |
91 | return NF_DROP; | |
1da177e4 LT |
92 | } |
93 | ||
94 | /* Transfer from original range. */ | |
587aa641 | 95 | newrange = ((struct nf_nat_range) |
1da177e4 LT |
96 | { mr->range[0].flags | IP_NAT_RANGE_MAP_IPS, |
97 | newdst, newdst, | |
98 | mr->range[0].min, mr->range[0].max }); | |
99 | ||
100 | /* Hand modified range to generic setup. */ | |
587aa641 | 101 | return nf_nat_setup_info(ct, &newrange, hooknum); |
1da177e4 LT |
102 | } |
103 | ||
6709dbbb | 104 | static struct xt_target redirect_reg = { |
1da177e4 | 105 | .name = "REDIRECT", |
6709dbbb | 106 | .family = AF_INET, |
1da177e4 | 107 | .target = redirect_target, |
587aa641 | 108 | .targetsize = sizeof(struct nf_nat_multi_range_compat), |
1d5cd909 PM |
109 | .table = "nat", |
110 | .hooks = (1 << NF_IP_PRE_ROUTING) | (1 << NF_IP_LOCAL_OUT), | |
1da177e4 LT |
111 | .checkentry = redirect_check, |
112 | .me = THIS_MODULE, | |
113 | }; | |
114 | ||
65b4b4e8 | 115 | static int __init ipt_redirect_init(void) |
1da177e4 | 116 | { |
6709dbbb | 117 | return xt_register_target(&redirect_reg); |
1da177e4 LT |
118 | } |
119 | ||
65b4b4e8 | 120 | static void __exit ipt_redirect_fini(void) |
1da177e4 | 121 | { |
6709dbbb | 122 | xt_unregister_target(&redirect_reg); |
1da177e4 LT |
123 | } |
124 | ||
65b4b4e8 AM |
125 | module_init(ipt_redirect_init); |
126 | module_exit(ipt_redirect_fini); |