1 /* Masquerade. Simple mapping which alters range to a local IP address
2 (depending on route). */
4 /* (C) 1999-2001 Paul `Rusty' Russell
5 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
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.
12 #include <linux/config.h>
13 #include <linux/types.h>
14 #include <linux/inetdevice.h>
16 #include <linux/timer.h>
17 #include <linux/module.h>
18 #include <linux/netfilter.h>
19 #include <net/protocol.h>
21 #include <net/checksum.h>
22 #include <net/route.h>
23 #include <linux/netfilter_ipv4.h>
24 #include <linux/netfilter_ipv4/ip_nat_rule.h>
25 #include <linux/netfilter_ipv4/ip_tables.h>
27 MODULE_LICENSE("GPL");
28 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
29 MODULE_DESCRIPTION("iptables MASQUERADE target module");
34 #define DEBUGP(format, args...)
37 /* Lock protects masq region inside conntrack */
38 static DEFINE_RWLOCK(masq_lock
);
40 /* FIXME: Multiple targets. --RR */
42 masquerade_check(const char *tablename
,
43 const struct ipt_entry
*e
,
45 unsigned int targinfosize
,
46 unsigned int hook_mask
)
48 const struct ip_nat_multi_range_compat
*mr
= targinfo
;
50 if (strcmp(tablename
, "nat") != 0) {
51 DEBUGP("masquerade_check: bad table `%s'.\n", tablename
);
54 if (targinfosize
!= IPT_ALIGN(sizeof(*mr
))) {
55 DEBUGP("masquerade_check: size %u != %u.\n",
56 targinfosize
, sizeof(*mr
));
59 if (hook_mask
& ~(1 << NF_IP_POST_ROUTING
)) {
60 DEBUGP("masquerade_check: bad hooks %x.\n", hook_mask
);
63 if (mr
->range
[0].flags
& IP_NAT_RANGE_MAP_IPS
) {
64 DEBUGP("masquerade_check: bad MAP_IPS.\n");
67 if (mr
->rangesize
!= 1) {
68 DEBUGP("masquerade_check: bad rangesize %u.\n", mr
->rangesize
);
75 masquerade_target(struct sk_buff
**pskb
,
76 const struct net_device
*in
,
77 const struct net_device
*out
,
82 struct ip_conntrack
*ct
;
83 enum ip_conntrack_info ctinfo
;
84 const struct ip_nat_multi_range_compat
*mr
;
85 struct ip_nat_range newrange
;
89 IP_NF_ASSERT(hooknum
== NF_IP_POST_ROUTING
);
91 ct
= ip_conntrack_get(*pskb
, &ctinfo
);
92 IP_NF_ASSERT(ct
&& (ctinfo
== IP_CT_NEW
|| ctinfo
== IP_CT_RELATED
93 || ctinfo
== IP_CT_RELATED
+ IP_CT_IS_REPLY
));
95 /* Source address is 0.0.0.0 - locally generated packet that is
96 * probably not supposed to be masqueraded.
98 if (ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.ip
== 0)
102 rt
= (struct rtable
*)(*pskb
)->dst
;
103 newsrc
= inet_select_addr(out
, rt
->rt_gateway
, RT_SCOPE_UNIVERSE
);
105 printk("MASQUERADE: %s ate my IP address\n", out
->name
);
109 write_lock_bh(&masq_lock
);
110 ct
->nat
.masq_index
= out
->ifindex
;
111 write_unlock_bh(&masq_lock
);
113 /* Transfer from original range. */
114 newrange
= ((struct ip_nat_range
)
115 { mr
->range
[0].flags
| IP_NAT_RANGE_MAP_IPS
,
117 mr
->range
[0].min
, mr
->range
[0].max
});
119 /* Hand modified range to generic setup. */
120 return ip_nat_setup_info(ct
, &newrange
, hooknum
);
124 device_cmp(struct ip_conntrack
*i
, void *ifindex
)
128 read_lock_bh(&masq_lock
);
129 ret
= (i
->nat
.masq_index
== (int)(long)ifindex
);
130 read_unlock_bh(&masq_lock
);
135 static int masq_device_event(struct notifier_block
*this,
139 struct net_device
*dev
= ptr
;
141 if (event
== NETDEV_DOWN
) {
142 /* Device was downed. Search entire table for
143 conntracks which were associated with that device,
145 IP_NF_ASSERT(dev
->ifindex
!= 0);
147 ip_ct_iterate_cleanup(device_cmp
, (void *)(long)dev
->ifindex
);
153 static int masq_inet_event(struct notifier_block
*this,
157 struct net_device
*dev
= ((struct in_ifaddr
*)ptr
)->ifa_dev
->dev
;
159 if (event
== NETDEV_DOWN
) {
160 /* IP address was deleted. Search entire table for
161 conntracks which were associated with that device,
163 IP_NF_ASSERT(dev
->ifindex
!= 0);
165 ip_ct_iterate_cleanup(device_cmp
, (void *)(long)dev
->ifindex
);
171 static struct notifier_block masq_dev_notifier
= {
172 .notifier_call
= masq_device_event
,
175 static struct notifier_block masq_inet_notifier
= {
176 .notifier_call
= masq_inet_event
,
179 static struct ipt_target masquerade
= {
180 .name
= "MASQUERADE",
181 .target
= masquerade_target
,
182 .checkentry
= masquerade_check
,
186 static int __init
init(void)
190 ret
= ipt_register_target(&masquerade
);
193 /* Register for device down reports */
194 register_netdevice_notifier(&masq_dev_notifier
);
195 /* Register IP address change reports */
196 register_inetaddr_notifier(&masq_inet_notifier
);
202 static void __exit
fini(void)
204 ipt_unregister_target(&masquerade
);
205 unregister_netdevice_notifier(&masq_dev_notifier
);
206 unregister_inetaddr_notifier(&masq_inet_notifier
);