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>
15 #define DEBUGP(format, args...)
17 /* Lock protects masq region inside conntrack */
18 DECLARE_RWLOCK(masq_lock
);
21 masquerade_check(const char *tablename
,
22 const struct ipt_entry
*e
,
24 unsigned int targinfosize
,
25 unsigned int hook_mask
)
27 const struct ip_nat_multi_range
*mr
= targinfo
;
29 if (strcmp(tablename
, "nat") != 0) {
30 DEBUGP("masquerade_check: bad table `%s'.\n", table
);
33 if (targinfosize
!= IPT_ALIGN(sizeof(*mr
))) {
34 DEBUGP("masquerade_check: size %u != %u.\n",
35 targinfosize
, sizeof(*mr
));
38 if (hook_mask
& ~(1 << NF_IP_POST_ROUTING
)) {
39 DEBUGP("masquerade_check: bad hooks %x.\n", hook_mask
);
42 if (mr
->range
[0].flags
& IP_NAT_RANGE_MAP_IPS
) {
43 DEBUGP("masquerade_check: bad MAP_IPS.\n");
46 if (mr
->rangesize
!= 1) {
47 DEBUGP("masquerade_check: bad rangesize %u.\n", mr
->rangesize
);
54 masquerade_target(struct sk_buff
**pskb
,
56 const struct net_device
*in
,
57 const struct net_device
*out
,
61 struct ip_conntrack
*ct
;
62 enum ip_conntrack_info ctinfo
;
63 const struct ip_nat_multi_range
*mr
;
64 struct ip_nat_multi_range newrange
;
69 IP_NF_ASSERT(hooknum
== NF_IP_POST_ROUTING
);
74 ct
= ip_conntrack_get(*pskb
, &ctinfo
);
75 IP_NF_ASSERT(ct
&& (ctinfo
== IP_CT_NEW
76 || ctinfo
== IP_CT_RELATED
));
80 key
.dst
= (*pskb
)->nh
.iph
->daddr
;
81 key
.src
= 0; /* Unknown: that's what we're trying to establish */
82 key
.tos
= RT_TOS((*pskb
)->nh
.iph
->tos
)|RTO_CONN
;
83 key
.oif
= out
->ifindex
;
84 #ifdef CONFIG_IP_ROUTE_FWMARK
85 key
.fwmark
= (*pskb
)->nfmark
;
87 if (ip_route_output_key(&rt
, &key
) != 0) {
88 /* Shouldn't happen */
89 printk("MASQUERADE: No route: Rusty's brain broke!\n");
94 DEBUGP("newsrc = %u.%u.%u.%u\n", NIPQUAD(newsrc
));
97 WRITE_LOCK(&masq_lock
);
98 ct
->nat
.masq_index
= out
->ifindex
;
99 WRITE_UNLOCK(&masq_lock
);
101 /* Transfer from original range. */
102 newrange
= ((struct ip_nat_multi_range
)
103 { 1, { { mr
->range
[0].flags
| IP_NAT_RANGE_MAP_IPS
,
105 mr
->range
[0].min
, mr
->range
[0].max
} } });
107 /* Hand modified range to generic setup. */
108 return ip_nat_setup_info(ct
, &newrange
, hooknum
);
112 device_cmp(const struct ip_conntrack
*i
, void *ifindex
)
116 READ_LOCK(&masq_lock
);
117 ret
= (i
->nat
.masq_index
== (int)(long)ifindex
);
118 READ_UNLOCK(&masq_lock
);
123 static int masq_device_event(struct notifier_block
*this,
127 struct net_device
*dev
= ptr
;
129 if (event
== NETDEV_DOWN
) {
130 /* Device was downed. Search entire table for
131 conntracks which were associated with that device,
133 IP_NF_ASSERT(dev
->ifindex
!= 0);
135 ip_ct_selective_cleanup(device_cmp
, (void *)(long)dev
->ifindex
);
141 static int masq_inet_event(struct notifier_block
*this,
145 struct net_device
*dev
= ((struct in_ifaddr
*)ptr
)->ifa_dev
->dev
;
147 if (event
== NETDEV_DOWN
) {
148 /* IP address was deleted. Search entire table for
149 conntracks which were associated with that device,
151 IP_NF_ASSERT(dev
->ifindex
!= 0);
153 ip_ct_selective_cleanup(device_cmp
, (void *)(long)dev
->ifindex
);
159 static struct notifier_block masq_dev_notifier
= {
165 static struct notifier_block masq_inet_notifier
= {
171 static struct ipt_target masquerade
172 = { { NULL
, NULL
}, "MASQUERADE", masquerade_target
, masquerade_check
, NULL
,
175 static int __init
init(void)
179 ret
= ipt_register_target(&masquerade
);
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
);
191 static void __exit
fini(void)
193 ipt_unregister_target(&masquerade
);
194 unregister_netdevice_notifier(&masq_dev_notifier
);
195 unregister_inetaddr_notifier(&masq_inet_notifier
);
200 MODULE_LICENSE("GPL");
202 EXPORT_SYMBOL(masq_lock
);