1 /* Masquerade. Simple mapping which alters range to a local IP address
2 (depending on route). */
3 #include <linux/config.h>
4 #include <linux/types.h>
6 #include <linux/timer.h>
7 #include <linux/module.h>
8 #include <linux/netfilter.h>
9 #include <net/protocol.h>
10 #include <net/checksum.h>
11 #include <linux/netfilter_ipv4.h>
12 #include <linux/netfilter_ipv4/ip_nat_rule.h>
13 #include <linux/netfilter_ipv4/ip_tables.h>
18 #define DEBUGP(format, args...)
21 /* Lock protects masq region inside conntrack */
22 static DECLARE_RWLOCK(masq_lock);
24 /* FIXME: Multiple targets. --RR */
26 masquerade_check(const char *tablename,
27 const struct ipt_entry *e,
29 unsigned int targinfosize,
30 unsigned int hook_mask)
32 const struct ip_nat_multi_range *mr = targinfo;
34 if (strcmp(tablename, "nat") != 0) {
35 DEBUGP("masquerade_check: bad table `%s'.\n", tablename);
38 if (targinfosize != IPT_ALIGN(sizeof(*mr))) {
39 DEBUGP("masquerade_check: size %u != %u.\n",
40 targinfosize, sizeof(*mr));
43 if (hook_mask & ~(1 << NF_IP_POST_ROUTING)) {
44 DEBUGP("masquerade_check: bad hooks %x.\n", hook_mask);
47 if (mr->range[0].flags & IP_NAT_RANGE_MAP_IPS) {
48 DEBUGP("masquerade_check: bad MAP_IPS.\n");
51 if (mr->rangesize != 1) {
52 DEBUGP("masquerade_check: bad rangesize %u.\n", mr->rangesize);
59 masquerade_target(struct sk_buff **pskb,
61 const struct net_device *in,
62 const struct net_device *out,
66 struct ip_conntrack *ct;
67 enum ip_conntrack_info ctinfo;
68 const struct ip_nat_multi_range *mr;
69 struct ip_nat_multi_range newrange;
74 IP_NF_ASSERT(hooknum == NF_IP_POST_ROUTING);
76 /* FIXME: For the moment, don't do local packets, breaks
77 testsuite for 2.3.49 --RR */
81 ct = ip_conntrack_get(*pskb, &ctinfo);
82 IP_NF_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED
83 || ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY));
87 key.dst = (*pskb)->nh.iph->daddr;
88 key.src = 0; /* Unknown: that's what we're trying to establish */
89 key.tos = RT_TOS((*pskb)->nh.iph->tos)|RTO_CONN;
91 #ifdef CONFIG_IP_ROUTE_FWMARK
92 key.fwmark = (*pskb)->nfmark;
94 if (ip_route_output_key(&rt, &key) != 0) {
95 /* Funky routing can do this. */
98 " No route: Rusty's brain broke!\n");
101 if (rt->u.dst.dev != out) {
104 " Route sent us somewhere else.\n");
110 DEBUGP("newsrc = %u.%u.%u.%u\n", NIPQUAD(newsrc));
113 WRITE_LOCK(&masq_lock);
114 ct->nat.masq_index = out->ifindex;
115 WRITE_UNLOCK(&masq_lock);
117 /* Transfer from original range. */
118 newrange = ((struct ip_nat_multi_range)
119 { 1, { { mr->range[0].flags | IP_NAT_RANGE_MAP_IPS,
121 mr->range[0].min, mr->range[0].max } } });
123 /* Hand modified range to generic setup. */
124 return ip_nat_setup_info(ct, &newrange, hooknum);
128 device_cmp(struct ip_conntrack *i, void *ifindex)
132 READ_LOCK(&masq_lock);
133 ret = (i->nat.masq_index == (int)(long)ifindex);
134 READ_UNLOCK(&masq_lock);
139 static int masq_device_event(struct notifier_block *this,
143 struct net_device *dev = ptr;
145 if (event == NETDEV_DOWN) {
146 /* Device was downed. Search entire table for
147 conntracks which were associated with that device,
149 IP_NF_ASSERT(dev->ifindex != 0);
151 ip_ct_iterate_cleanup(device_cmp, (void *)(long)dev->ifindex);
158 static int masq_inet_event(struct notifier_block *this,
162 struct net_device *dev = ((struct in_ifaddr *)ptr)->ifa_dev->dev;
164 if (event == NETDEV_DOWN) {
165 /* IP address was deleted. Search entire table for
166 conntracks which were associated with that device,
168 IP_NF_ASSERT(dev->ifindex != 0);
170 ip_ct_iterate_cleanup(device_cmp, (void *)(long)dev->ifindex);
176 static struct notifier_block masq_dev_notifier = {
177 .notifier_call = masq_device_event,
180 static struct notifier_block masq_inet_notifier = {
181 .notifier_call = masq_inet_event
184 static struct ipt_target masquerade
185 = { { NULL, NULL }, "MASQUERADE", masquerade_target, masquerade_check, NULL,
188 static int __init init(void)
192 ret = ipt_register_target(&masquerade);
195 /* Register for device down reports */
196 register_netdevice_notifier(&masq_dev_notifier);
197 /* Register IP address change reports */
198 register_inetaddr_notifier(&masq_inet_notifier);
204 static void __exit fini(void)
206 ipt_unregister_target(&masquerade);
207 unregister_netdevice_notifier(&masq_dev_notifier);
208 unregister_inetaddr_notifier(&masq_inet_notifier);
213 MODULE_LICENSE("GPL");