]>
Commit | Line | Data |
---|---|---|
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 <[email protected]> | |
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 | ||
12 | #include <linux/config.h> | |
13 | #include <linux/types.h> | |
14c85021 | 14 | #include <linux/inetdevice.h> |
1da177e4 LT |
15 | #include <linux/ip.h> |
16 | #include <linux/timer.h> | |
17 | #include <linux/module.h> | |
18 | #include <linux/netfilter.h> | |
19 | #include <net/protocol.h> | |
20 | #include <net/ip.h> | |
21 | #include <net/checksum.h> | |
14c85021 | 22 | #include <net/route.h> |
1da177e4 LT |
23 | #include <linux/netfilter_ipv4.h> |
24 | #include <linux/netfilter_ipv4/ip_nat_rule.h> | |
25 | #include <linux/netfilter_ipv4/ip_tables.h> | |
26 | ||
27 | MODULE_LICENSE("GPL"); | |
28 | MODULE_AUTHOR("Netfilter Core Team <[email protected]>"); | |
29 | MODULE_DESCRIPTION("iptables MASQUERADE target module"); | |
30 | ||
31 | #if 0 | |
32 | #define DEBUGP printk | |
33 | #else | |
34 | #define DEBUGP(format, args...) | |
35 | #endif | |
36 | ||
37 | /* Lock protects masq region inside conntrack */ | |
e45b1be8 | 38 | static DEFINE_RWLOCK(masq_lock); |
1da177e4 LT |
39 | |
40 | /* FIXME: Multiple targets. --RR */ | |
41 | static int | |
42 | masquerade_check(const char *tablename, | |
43 | const struct ipt_entry *e, | |
44 | void *targinfo, | |
45 | unsigned int targinfosize, | |
46 | unsigned int hook_mask) | |
47 | { | |
48 | const struct ip_nat_multi_range_compat *mr = targinfo; | |
49 | ||
50 | if (strcmp(tablename, "nat") != 0) { | |
51 | DEBUGP("masquerade_check: bad table `%s'.\n", tablename); | |
52 | return 0; | |
53 | } | |
54 | if (targinfosize != IPT_ALIGN(sizeof(*mr))) { | |
55 | DEBUGP("masquerade_check: size %u != %u.\n", | |
56 | targinfosize, sizeof(*mr)); | |
57 | return 0; | |
58 | } | |
59 | if (hook_mask & ~(1 << NF_IP_POST_ROUTING)) { | |
60 | DEBUGP("masquerade_check: bad hooks %x.\n", hook_mask); | |
61 | return 0; | |
62 | } | |
63 | if (mr->range[0].flags & IP_NAT_RANGE_MAP_IPS) { | |
64 | DEBUGP("masquerade_check: bad MAP_IPS.\n"); | |
65 | return 0; | |
66 | } | |
67 | if (mr->rangesize != 1) { | |
68 | DEBUGP("masquerade_check: bad rangesize %u.\n", mr->rangesize); | |
69 | return 0; | |
70 | } | |
71 | return 1; | |
72 | } | |
73 | ||
74 | static unsigned int | |
75 | masquerade_target(struct sk_buff **pskb, | |
76 | const struct net_device *in, | |
77 | const struct net_device *out, | |
78 | unsigned int hooknum, | |
79 | const void *targinfo, | |
80 | void *userinfo) | |
81 | { | |
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; | |
86 | struct rtable *rt; | |
87 | u_int32_t newsrc; | |
88 | ||
89 | IP_NF_ASSERT(hooknum == NF_IP_POST_ROUTING); | |
90 | ||
1da177e4 LT |
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)); | |
94 | ||
adcb5ad1 PM |
95 | /* Source address is 0.0.0.0 - locally generated packet that is |
96 | * probably not supposed to be masqueraded. | |
97 | */ | |
98 | if (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip == 0) | |
99 | return NF_ACCEPT; | |
100 | ||
1da177e4 LT |
101 | mr = targinfo; |
102 | rt = (struct rtable *)(*pskb)->dst; | |
103 | newsrc = inet_select_addr(out, rt->rt_gateway, RT_SCOPE_UNIVERSE); | |
104 | if (!newsrc) { | |
105 | printk("MASQUERADE: %s ate my IP address\n", out->name); | |
106 | return NF_DROP; | |
107 | } | |
108 | ||
e45b1be8 | 109 | write_lock_bh(&masq_lock); |
1da177e4 | 110 | ct->nat.masq_index = out->ifindex; |
e45b1be8 | 111 | write_unlock_bh(&masq_lock); |
1da177e4 LT |
112 | |
113 | /* Transfer from original range. */ | |
114 | newrange = ((struct ip_nat_range) | |
115 | { mr->range[0].flags | IP_NAT_RANGE_MAP_IPS, | |
116 | newsrc, newsrc, | |
117 | mr->range[0].min, mr->range[0].max }); | |
118 | ||
119 | /* Hand modified range to generic setup. */ | |
120 | return ip_nat_setup_info(ct, &newrange, hooknum); | |
121 | } | |
122 | ||
123 | static inline int | |
124 | device_cmp(struct ip_conntrack *i, void *ifindex) | |
125 | { | |
126 | int ret; | |
127 | ||
e45b1be8 | 128 | read_lock_bh(&masq_lock); |
1da177e4 | 129 | ret = (i->nat.masq_index == (int)(long)ifindex); |
e45b1be8 | 130 | read_unlock_bh(&masq_lock); |
1da177e4 LT |
131 | |
132 | return ret; | |
133 | } | |
134 | ||
135 | static int masq_device_event(struct notifier_block *this, | |
136 | unsigned long event, | |
137 | void *ptr) | |
138 | { | |
139 | struct net_device *dev = ptr; | |
140 | ||
141 | if (event == NETDEV_DOWN) { | |
142 | /* Device was downed. Search entire table for | |
143 | conntracks which were associated with that device, | |
144 | and forget them. */ | |
145 | IP_NF_ASSERT(dev->ifindex != 0); | |
146 | ||
147 | ip_ct_iterate_cleanup(device_cmp, (void *)(long)dev->ifindex); | |
148 | } | |
149 | ||
150 | return NOTIFY_DONE; | |
151 | } | |
152 | ||
153 | static int masq_inet_event(struct notifier_block *this, | |
154 | unsigned long event, | |
155 | void *ptr) | |
156 | { | |
157 | struct net_device *dev = ((struct in_ifaddr *)ptr)->ifa_dev->dev; | |
158 | ||
159 | if (event == NETDEV_DOWN) { | |
160 | /* IP address was deleted. Search entire table for | |
161 | conntracks which were associated with that device, | |
162 | and forget them. */ | |
163 | IP_NF_ASSERT(dev->ifindex != 0); | |
164 | ||
165 | ip_ct_iterate_cleanup(device_cmp, (void *)(long)dev->ifindex); | |
166 | } | |
167 | ||
168 | return NOTIFY_DONE; | |
169 | } | |
170 | ||
171 | static struct notifier_block masq_dev_notifier = { | |
172 | .notifier_call = masq_device_event, | |
173 | }; | |
174 | ||
175 | static struct notifier_block masq_inet_notifier = { | |
176 | .notifier_call = masq_inet_event, | |
177 | }; | |
178 | ||
179 | static struct ipt_target masquerade = { | |
180 | .name = "MASQUERADE", | |
181 | .target = masquerade_target, | |
182 | .checkentry = masquerade_check, | |
183 | .me = THIS_MODULE, | |
184 | }; | |
185 | ||
186 | static int __init init(void) | |
187 | { | |
188 | int ret; | |
189 | ||
190 | ret = ipt_register_target(&masquerade); | |
191 | ||
192 | if (ret == 0) { | |
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); | |
197 | } | |
198 | ||
199 | return ret; | |
200 | } | |
201 | ||
202 | static void __exit fini(void) | |
203 | { | |
204 | ipt_unregister_target(&masquerade); | |
205 | unregister_netdevice_notifier(&masq_dev_notifier); | |
206 | unregister_inetaddr_notifier(&masq_inet_notifier); | |
207 | } | |
208 | ||
209 | module_init(init); | |
210 | module_exit(fini); |