[NETFILTER]: x_tables: remove unused size argument to check/destroy functions
[deliverable/linux.git] / net / ipv4 / netfilter / ipt_MASQUERADE.c
CommitLineData
1da177e4
LT
1/* Masquerade. Simple mapping which alters range to a local IP address
2 (depending on route). */
3
4/* (C) 1999-2001 Paul `Rusty' Russell
5 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
1da177e4 12#include <linux/types.h>
14c85021 13#include <linux/inetdevice.h>
1da177e4
LT
14#include <linux/ip.h>
15#include <linux/timer.h>
16#include <linux/module.h>
17#include <linux/netfilter.h>
18#include <net/protocol.h>
19#include <net/ip.h>
20#include <net/checksum.h>
14c85021 21#include <net/route.h>
1da177e4
LT
22#include <linux/netfilter_ipv4.h>
23#include <linux/netfilter_ipv4/ip_nat_rule.h>
24#include <linux/netfilter_ipv4/ip_tables.h>
25
26MODULE_LICENSE("GPL");
27MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
28MODULE_DESCRIPTION("iptables MASQUERADE target module");
29
30#if 0
31#define DEBUGP printk
32#else
33#define DEBUGP(format, args...)
34#endif
35
36/* Lock protects masq region inside conntrack */
e45b1be8 37static DEFINE_RWLOCK(masq_lock);
1da177e4
LT
38
39/* FIXME: Multiple targets. --RR */
40static int
41masquerade_check(const char *tablename,
2e4e6a17 42 const void *e,
c4986734 43 const struct xt_target *target,
1da177e4 44 void *targinfo,
1da177e4
LT
45 unsigned int hook_mask)
46{
47 const struct ip_nat_multi_range_compat *mr = targinfo;
48
1da177e4
LT
49 if (mr->range[0].flags & IP_NAT_RANGE_MAP_IPS) {
50 DEBUGP("masquerade_check: bad MAP_IPS.\n");
51 return 0;
52 }
53 if (mr->rangesize != 1) {
54 DEBUGP("masquerade_check: bad rangesize %u.\n", mr->rangesize);
55 return 0;
56 }
57 return 1;
58}
59
60static unsigned int
61masquerade_target(struct sk_buff **pskb,
62 const struct net_device *in,
63 const struct net_device *out,
64 unsigned int hooknum,
c4986734 65 const struct xt_target *target,
fe1cb108 66 const void *targinfo)
1da177e4
LT
67{
68 struct ip_conntrack *ct;
69 enum ip_conntrack_info ctinfo;
70 const struct ip_nat_multi_range_compat *mr;
71 struct ip_nat_range newrange;
72 struct rtable *rt;
73 u_int32_t newsrc;
74
75 IP_NF_ASSERT(hooknum == NF_IP_POST_ROUTING);
76
1da177e4
LT
77 ct = ip_conntrack_get(*pskb, &ctinfo);
78 IP_NF_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED
79 || ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY));
80
adcb5ad1
PM
81 /* Source address is 0.0.0.0 - locally generated packet that is
82 * probably not supposed to be masqueraded.
83 */
84 if (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip == 0)
85 return NF_ACCEPT;
86
1da177e4
LT
87 mr = targinfo;
88 rt = (struct rtable *)(*pskb)->dst;
89 newsrc = inet_select_addr(out, rt->rt_gateway, RT_SCOPE_UNIVERSE);
90 if (!newsrc) {
91 printk("MASQUERADE: %s ate my IP address\n", out->name);
92 return NF_DROP;
93 }
94
e45b1be8 95 write_lock_bh(&masq_lock);
1da177e4 96 ct->nat.masq_index = out->ifindex;
e45b1be8 97 write_unlock_bh(&masq_lock);
1da177e4
LT
98
99 /* Transfer from original range. */
100 newrange = ((struct ip_nat_range)
101 { mr->range[0].flags | IP_NAT_RANGE_MAP_IPS,
102 newsrc, newsrc,
103 mr->range[0].min, mr->range[0].max });
104
105 /* Hand modified range to generic setup. */
106 return ip_nat_setup_info(ct, &newrange, hooknum);
107}
108
109static inline int
110device_cmp(struct ip_conntrack *i, void *ifindex)
111{
112 int ret;
113
e45b1be8 114 read_lock_bh(&masq_lock);
1da177e4 115 ret = (i->nat.masq_index == (int)(long)ifindex);
e45b1be8 116 read_unlock_bh(&masq_lock);
1da177e4
LT
117
118 return ret;
119}
120
121static int masq_device_event(struct notifier_block *this,
122 unsigned long event,
123 void *ptr)
124{
125 struct net_device *dev = ptr;
126
127 if (event == NETDEV_DOWN) {
128 /* Device was downed. Search entire table for
129 conntracks which were associated with that device,
130 and forget them. */
131 IP_NF_ASSERT(dev->ifindex != 0);
132
133 ip_ct_iterate_cleanup(device_cmp, (void *)(long)dev->ifindex);
134 }
135
136 return NOTIFY_DONE;
137}
138
139static int masq_inet_event(struct notifier_block *this,
140 unsigned long event,
141 void *ptr)
142{
143 struct net_device *dev = ((struct in_ifaddr *)ptr)->ifa_dev->dev;
144
145 if (event == NETDEV_DOWN) {
146 /* IP address was deleted. Search entire table for
147 conntracks which were associated with that device,
148 and forget them. */
149 IP_NF_ASSERT(dev->ifindex != 0);
150
151 ip_ct_iterate_cleanup(device_cmp, (void *)(long)dev->ifindex);
152 }
153
154 return NOTIFY_DONE;
155}
156
157static struct notifier_block masq_dev_notifier = {
158 .notifier_call = masq_device_event,
159};
160
161static struct notifier_block masq_inet_notifier = {
162 .notifier_call = masq_inet_event,
163};
164
165static struct ipt_target masquerade = {
166 .name = "MASQUERADE",
167 .target = masquerade_target,
1d5cd909
PM
168 .targetsize = sizeof(struct ip_nat_multi_range_compat),
169 .table = "nat",
170 .hooks = 1 << NF_IP_POST_ROUTING,
1da177e4
LT
171 .checkentry = masquerade_check,
172 .me = THIS_MODULE,
173};
174
65b4b4e8 175static int __init ipt_masquerade_init(void)
1da177e4
LT
176{
177 int ret;
178
179 ret = ipt_register_target(&masquerade);
180
181 if (ret == 0) {
182 /* Register for device down reports */
183 register_netdevice_notifier(&masq_dev_notifier);
184 /* Register IP address change reports */
185 register_inetaddr_notifier(&masq_inet_notifier);
186 }
187
188 return ret;
189}
190
65b4b4e8 191static void __exit ipt_masquerade_fini(void)
1da177e4
LT
192{
193 ipt_unregister_target(&masquerade);
194 unregister_netdevice_notifier(&masq_dev_notifier);
195 unregister_inetaddr_notifier(&masq_inet_notifier);
196}
197
65b4b4e8
AM
198module_init(ipt_masquerade_init);
199module_exit(ipt_masquerade_fini);
This page took 0.256928 seconds and 5 git commands to generate.