]>
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> | |
5b1158e9 JK |
21 | #ifdef CONFIG_NF_NAT_NEEDED |
22 | #include <net/netfilter/nf_nat_rule.h> | |
23 | #else | |
1da177e4 | 24 | #include <linux/netfilter_ipv4/ip_nat_rule.h> |
5b1158e9 | 25 | #endif |
1da177e4 LT |
26 | |
27 | MODULE_LICENSE("GPL"); | |
28 | MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>"); | |
29 | MODULE_DESCRIPTION("iptables REDIRECT target module"); | |
30 | ||
31 | #if 0 | |
32 | #define DEBUGP printk | |
33 | #else | |
34 | #define DEBUGP(format, args...) | |
35 | #endif | |
36 | ||
37 | /* FIXME: Take multiple ranges --RR */ | |
38 | static int | |
39 | redirect_check(const char *tablename, | |
2e4e6a17 | 40 | const void *e, |
c4986734 | 41 | const struct xt_target *target, |
1da177e4 | 42 | void *targinfo, |
1da177e4 LT |
43 | unsigned int hook_mask) |
44 | { | |
45 | const struct ip_nat_multi_range_compat *mr = targinfo; | |
46 | ||
1da177e4 LT |
47 | if (mr->range[0].flags & IP_NAT_RANGE_MAP_IPS) { |
48 | DEBUGP("redirect_check: bad MAP_IPS.\n"); | |
49 | return 0; | |
50 | } | |
51 | if (mr->rangesize != 1) { | |
52 | DEBUGP("redirect_check: bad rangesize %u.\n", mr->rangesize); | |
53 | return 0; | |
54 | } | |
55 | return 1; | |
56 | } | |
57 | ||
58 | static unsigned int | |
59 | redirect_target(struct sk_buff **pskb, | |
60 | const struct net_device *in, | |
61 | const struct net_device *out, | |
62 | unsigned int hooknum, | |
c4986734 | 63 | const struct xt_target *target, |
fe1cb108 | 64 | const void *targinfo) |
1da177e4 LT |
65 | { |
66 | struct ip_conntrack *ct; | |
67 | enum ip_conntrack_info ctinfo; | |
a144ea4b | 68 | __be32 newdst; |
1da177e4 LT |
69 | const struct ip_nat_multi_range_compat *mr = targinfo; |
70 | struct ip_nat_range newrange; | |
71 | ||
72 | IP_NF_ASSERT(hooknum == NF_IP_PRE_ROUTING | |
73 | || hooknum == NF_IP_LOCAL_OUT); | |
74 | ||
75 | ct = ip_conntrack_get(*pskb, &ctinfo); | |
76 | IP_NF_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED)); | |
77 | ||
78 | /* Local packets: make them go to loopback */ | |
79 | if (hooknum == NF_IP_LOCAL_OUT) | |
80 | newdst = htonl(0x7F000001); | |
81 | else { | |
82 | struct in_device *indev; | |
cd0bf2d7 | 83 | struct in_ifaddr *ifa; |
1da177e4 | 84 | |
cd0bf2d7 PM |
85 | newdst = 0; |
86 | ||
87 | rcu_read_lock(); | |
e5ed6399 | 88 | indev = __in_dev_get_rcu((*pskb)->dev); |
cd0bf2d7 PM |
89 | if (indev && (ifa = indev->ifa_list)) |
90 | newdst = ifa->ifa_local; | |
91 | rcu_read_unlock(); | |
1da177e4 | 92 | |
cd0bf2d7 PM |
93 | if (!newdst) |
94 | return NF_DROP; | |
1da177e4 LT |
95 | } |
96 | ||
97 | /* Transfer from original range. */ | |
98 | newrange = ((struct ip_nat_range) | |
99 | { mr->range[0].flags | IP_NAT_RANGE_MAP_IPS, | |
100 | newdst, newdst, | |
101 | mr->range[0].min, mr->range[0].max }); | |
102 | ||
103 | /* Hand modified range to generic setup. */ | |
104 | return ip_nat_setup_info(ct, &newrange, hooknum); | |
105 | } | |
106 | ||
107 | static struct ipt_target redirect_reg = { | |
108 | .name = "REDIRECT", | |
109 | .target = redirect_target, | |
1d5cd909 PM |
110 | .targetsize = sizeof(struct ip_nat_multi_range_compat), |
111 | .table = "nat", | |
112 | .hooks = (1 << NF_IP_PRE_ROUTING) | (1 << NF_IP_LOCAL_OUT), | |
1da177e4 LT |
113 | .checkentry = redirect_check, |
114 | .me = THIS_MODULE, | |
115 | }; | |
116 | ||
65b4b4e8 | 117 | static int __init ipt_redirect_init(void) |
1da177e4 LT |
118 | { |
119 | return ipt_register_target(&redirect_reg); | |
120 | } | |
121 | ||
65b4b4e8 | 122 | static void __exit ipt_redirect_fini(void) |
1da177e4 LT |
123 | { |
124 | ipt_unregister_target(&redirect_reg); | |
125 | } | |
126 | ||
65b4b4e8 AM |
127 | module_init(ipt_redirect_init); |
128 | module_exit(ipt_redirect_fini); |