[NETFILTER]: Convert DEBUGP to pr_debug
[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
5b1158e9 5 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
1da177e4
LT
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>
5b1158e9 22#include <net/netfilter/nf_nat_rule.h>
587aa641 23#include <linux/netfilter_ipv4.h>
6709dbbb 24#include <linux/netfilter/x_tables.h>
1da177e4
LT
25
26MODULE_LICENSE("GPL");
27MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
28MODULE_DESCRIPTION("iptables MASQUERADE target module");
29
1da177e4 30/* Lock protects masq region inside conntrack */
e45b1be8 31static DEFINE_RWLOCK(masq_lock);
1da177e4
LT
32
33/* FIXME: Multiple targets. --RR */
e1931b78 34static bool
1da177e4 35masquerade_check(const char *tablename,
2e4e6a17 36 const void *e,
c4986734 37 const struct xt_target *target,
1da177e4 38 void *targinfo,
1da177e4
LT
39 unsigned int hook_mask)
40{
587aa641 41 const struct nf_nat_multi_range_compat *mr = targinfo;
1da177e4 42
1da177e4 43 if (mr->range[0].flags & IP_NAT_RANGE_MAP_IPS) {
0d53778e 44 pr_debug("masquerade_check: bad MAP_IPS.\n");
e1931b78 45 return false;
1da177e4
LT
46 }
47 if (mr->rangesize != 1) {
0d53778e 48 pr_debug("masquerade_check: bad rangesize %u\n", mr->rangesize);
e1931b78 49 return false;
1da177e4 50 }
e1931b78 51 return true;
1da177e4
LT
52}
53
54static unsigned int
55masquerade_target(struct sk_buff **pskb,
56 const struct net_device *in,
57 const struct net_device *out,
58 unsigned int hooknum,
c4986734 59 const struct xt_target *target,
fe1cb108 60 const void *targinfo)
1da177e4 61{
587aa641 62 struct nf_conn *ct;
5b1158e9 63 struct nf_conn_nat *nat;
1da177e4 64 enum ip_conntrack_info ctinfo;
587aa641
PM
65 struct nf_nat_range newrange;
66 const struct nf_nat_multi_range_compat *mr;
a47362a2 67 const struct rtable *rt;
a61ced5d 68 __be32 newsrc;
1da177e4 69
587aa641 70 NF_CT_ASSERT(hooknum == NF_IP_POST_ROUTING);
1da177e4 71
587aa641 72 ct = nf_ct_get(*pskb, &ctinfo);
5b1158e9 73 nat = nfct_nat(ct);
587aa641
PM
74
75 NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED
e905a9ed 76 || ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY));
1da177e4 77
adcb5ad1
PM
78 /* Source address is 0.0.0.0 - locally generated packet that is
79 * probably not supposed to be masqueraded.
80 */
5b1158e9 81 if (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip == 0)
adcb5ad1
PM
82 return NF_ACCEPT;
83
1da177e4
LT
84 mr = targinfo;
85 rt = (struct rtable *)(*pskb)->dst;
86 newsrc = inet_select_addr(out, rt->rt_gateway, RT_SCOPE_UNIVERSE);
87 if (!newsrc) {
88 printk("MASQUERADE: %s ate my IP address\n", out->name);
89 return NF_DROP;
90 }
91
e45b1be8 92 write_lock_bh(&masq_lock);
5b1158e9 93 nat->masq_index = out->ifindex;
e45b1be8 94 write_unlock_bh(&masq_lock);
1da177e4
LT
95
96 /* Transfer from original range. */
587aa641 97 newrange = ((struct nf_nat_range)
1da177e4
LT
98 { mr->range[0].flags | IP_NAT_RANGE_MAP_IPS,
99 newsrc, newsrc,
100 mr->range[0].min, mr->range[0].max });
101
102 /* Hand modified range to generic setup. */
587aa641 103 return nf_nat_setup_info(ct, &newrange, hooknum);
1da177e4
LT
104}
105
170b197c 106static int
587aa641 107device_cmp(struct nf_conn *i, void *ifindex)
1da177e4 108{
a47362a2 109 const struct nf_conn_nat *nat = nfct_nat(i);
587aa641 110 int ret;
bbdc176a
MJ
111
112 if (!nat)
113 return 0;
1da177e4 114
e45b1be8 115 read_lock_bh(&masq_lock);
5b1158e9 116 ret = (nat->masq_index == (int)(long)ifindex);
e45b1be8 117 read_unlock_bh(&masq_lock);
1da177e4
LT
118
119 return ret;
120}
121
122static int masq_device_event(struct notifier_block *this,
123 unsigned long event,
124 void *ptr)
125{
a47362a2 126 const struct net_device *dev = ptr;
1da177e4
LT
127
128 if (event == NETDEV_DOWN) {
129 /* Device was downed. Search entire table for
130 conntracks which were associated with that device,
131 and forget them. */
587aa641 132 NF_CT_ASSERT(dev->ifindex != 0);
1da177e4 133
587aa641 134 nf_ct_iterate_cleanup(device_cmp, (void *)(long)dev->ifindex);
1da177e4
LT
135 }
136
137 return NOTIFY_DONE;
138}
139
140static int masq_inet_event(struct notifier_block *this,
141 unsigned long event,
142 void *ptr)
143{
a47362a2 144 const struct net_device *dev = ((struct in_ifaddr *)ptr)->ifa_dev->dev;
1da177e4
LT
145
146 if (event == NETDEV_DOWN) {
147 /* IP address was deleted. Search entire table for
148 conntracks which were associated with that device,
149 and forget them. */
587aa641 150 NF_CT_ASSERT(dev->ifindex != 0);
1da177e4 151
587aa641 152 nf_ct_iterate_cleanup(device_cmp, (void *)(long)dev->ifindex);
1da177e4
LT
153 }
154
155 return NOTIFY_DONE;
156}
157
158static struct notifier_block masq_dev_notifier = {
159 .notifier_call = masq_device_event,
160};
161
162static struct notifier_block masq_inet_notifier = {
163 .notifier_call = masq_inet_event,
164};
165
9f15c530 166static struct xt_target masquerade __read_mostly = {
1da177e4 167 .name = "MASQUERADE",
6709dbbb 168 .family = AF_INET,
1da177e4 169 .target = masquerade_target,
587aa641 170 .targetsize = sizeof(struct nf_nat_multi_range_compat),
1d5cd909
PM
171 .table = "nat",
172 .hooks = 1 << NF_IP_POST_ROUTING,
1da177e4
LT
173 .checkentry = masquerade_check,
174 .me = THIS_MODULE,
175};
176
65b4b4e8 177static int __init ipt_masquerade_init(void)
1da177e4
LT
178{
179 int ret;
180
6709dbbb 181 ret = xt_register_target(&masquerade);
1da177e4
LT
182
183 if (ret == 0) {
184 /* Register for device down reports */
185 register_netdevice_notifier(&masq_dev_notifier);
186 /* Register IP address change reports */
187 register_inetaddr_notifier(&masq_inet_notifier);
188 }
189
190 return ret;
191}
192
65b4b4e8 193static void __exit ipt_masquerade_fini(void)
1da177e4 194{
6709dbbb 195 xt_unregister_target(&masquerade);
1da177e4 196 unregister_netdevice_notifier(&masq_dev_notifier);
e905a9ed 197 unregister_inetaddr_notifier(&masq_inet_notifier);
1da177e4
LT
198}
199
65b4b4e8
AM
200module_init(ipt_masquerade_init);
201module_exit(ipt_masquerade_fini);
This page took 0.275993 seconds and 5 git commands to generate.