Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* Redirect. Simple mapping which alters dst to a local IP address. */ |
2 | /* (C) 1999-2001 Paul `Rusty' Russell | |
3 | * (C) 2002-2004 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_ipv4/ip_nat_rule.h> | |
22 | ||
23 | MODULE_LICENSE("GPL"); | |
24 | MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>"); | |
25 | MODULE_DESCRIPTION("iptables REDIRECT target module"); | |
26 | ||
27 | #if 0 | |
28 | #define DEBUGP printk | |
29 | #else | |
30 | #define DEBUGP(format, args...) | |
31 | #endif | |
32 | ||
33 | /* FIXME: Take multiple ranges --RR */ | |
34 | static int | |
35 | redirect_check(const char *tablename, | |
2e4e6a17 | 36 | const void *e, |
c4986734 | 37 | const struct xt_target *target, |
1da177e4 LT |
38 | void *targinfo, |
39 | unsigned int targinfosize, | |
40 | unsigned int hook_mask) | |
41 | { | |
42 | const struct ip_nat_multi_range_compat *mr = targinfo; | |
43 | ||
1da177e4 LT |
44 | if (mr->range[0].flags & IP_NAT_RANGE_MAP_IPS) { |
45 | DEBUGP("redirect_check: bad MAP_IPS.\n"); | |
46 | return 0; | |
47 | } | |
48 | if (mr->rangesize != 1) { | |
49 | DEBUGP("redirect_check: bad rangesize %u.\n", mr->rangesize); | |
50 | return 0; | |
51 | } | |
52 | return 1; | |
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, |
1da177e4 LT |
61 | const void *targinfo, |
62 | void *userinfo) | |
63 | { | |
64 | struct ip_conntrack *ct; | |
65 | enum ip_conntrack_info ctinfo; | |
66 | u_int32_t newdst; | |
67 | const struct ip_nat_multi_range_compat *mr = targinfo; | |
68 | struct ip_nat_range newrange; | |
69 | ||
70 | IP_NF_ASSERT(hooknum == NF_IP_PRE_ROUTING | |
71 | || hooknum == NF_IP_LOCAL_OUT); | |
72 | ||
73 | ct = ip_conntrack_get(*pskb, &ctinfo); | |
74 | IP_NF_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED)); | |
75 | ||
76 | /* Local packets: make them go to loopback */ | |
77 | if (hooknum == NF_IP_LOCAL_OUT) | |
78 | newdst = htonl(0x7F000001); | |
79 | else { | |
80 | struct in_device *indev; | |
cd0bf2d7 | 81 | struct in_ifaddr *ifa; |
1da177e4 | 82 | |
cd0bf2d7 PM |
83 | newdst = 0; |
84 | ||
85 | rcu_read_lock(); | |
e5ed6399 | 86 | indev = __in_dev_get_rcu((*pskb)->dev); |
cd0bf2d7 PM |
87 | if (indev && (ifa = indev->ifa_list)) |
88 | newdst = ifa->ifa_local; | |
89 | rcu_read_unlock(); | |
1da177e4 | 90 | |
cd0bf2d7 PM |
91 | if (!newdst) |
92 | return NF_DROP; | |
1da177e4 LT |
93 | } |
94 | ||
95 | /* Transfer from original range. */ | |
96 | newrange = ((struct ip_nat_range) | |
97 | { mr->range[0].flags | IP_NAT_RANGE_MAP_IPS, | |
98 | newdst, newdst, | |
99 | mr->range[0].min, mr->range[0].max }); | |
100 | ||
101 | /* Hand modified range to generic setup. */ | |
102 | return ip_nat_setup_info(ct, &newrange, hooknum); | |
103 | } | |
104 | ||
105 | static struct ipt_target redirect_reg = { | |
106 | .name = "REDIRECT", | |
107 | .target = redirect_target, | |
1d5cd909 PM |
108 | .targetsize = sizeof(struct ip_nat_multi_range_compat), |
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 | ||
115 | static int __init init(void) | |
116 | { | |
117 | return ipt_register_target(&redirect_reg); | |
118 | } | |
119 | ||
120 | static void __exit fini(void) | |
121 | { | |
122 | ipt_unregister_target(&redirect_reg); | |
123 | } | |
124 | ||
125 | module_init(init); | |
126 | module_exit(fini); |