1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/types.h>
4 #include <linux/atomic.h>
5 #include <linux/inetdevice.h>
6 #include <linux/netfilter.h>
7 #include <linux/netfilter_ipv4.h>
8 #include <linux/netfilter_ipv6.h>
10 #include <net/netfilter/nf_nat_masquerade.h>
12 struct masq_dev_work {
13 struct work_struct work;
15 netns_tracker ns_tracker;
16 union nf_inet_addr addr;
18 int (*iter)(struct nf_conn *i, void *data);
21 #define MAX_MASQ_WORKER_COUNT 16
23 static DEFINE_MUTEX(masq_mutex);
24 static unsigned int masq_refcnt __read_mostly;
25 static atomic_t masq_worker_count __read_mostly;
28 nf_nat_masquerade_ipv4(struct sk_buff *skb, unsigned int hooknum,
29 const struct nf_nat_range2 *range,
30 const struct net_device *out)
33 struct nf_conn_nat *nat;
34 enum ip_conntrack_info ctinfo;
35 struct nf_nat_range2 newrange;
36 const struct rtable *rt;
39 WARN_ON(hooknum != NF_INET_POST_ROUTING);
41 ct = nf_ct_get(skb, &ctinfo);
43 WARN_ON(!(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED ||
44 ctinfo == IP_CT_RELATED_REPLY)));
46 /* Source address is 0.0.0.0 - locally generated packet that is
47 * probably not supposed to be masqueraded.
49 if (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip == 0)
53 nh = rt_nexthop(rt, ip_hdr(skb)->daddr);
54 newsrc = inet_select_addr(out, nh, RT_SCOPE_UNIVERSE);
56 pr_info("%s ate my IP address\n", out->name);
60 nat = nf_ct_nat_ext_add(ct);
62 nat->masq_index = out->ifindex;
64 /* Transfer from original range. */
65 memset(&newrange.min_addr, 0, sizeof(newrange.min_addr));
66 memset(&newrange.max_addr, 0, sizeof(newrange.max_addr));
67 newrange.flags = range->flags | NF_NAT_RANGE_MAP_IPS;
68 newrange.min_addr.ip = newsrc;
69 newrange.max_addr.ip = newsrc;
70 newrange.min_proto = range->min_proto;
71 newrange.max_proto = range->max_proto;
73 /* Hand modified range to generic setup. */
74 return nf_nat_setup_info(ct, &newrange, NF_NAT_MANIP_SRC);
76 EXPORT_SYMBOL_GPL(nf_nat_masquerade_ipv4);
78 static void iterate_cleanup_work(struct work_struct *work)
80 struct masq_dev_work *w;
82 w = container_of(work, struct masq_dev_work, work);
84 nf_ct_iterate_cleanup_net(w->net, w->iter, (void *)w, 0, 0);
86 put_net_track(w->net, &w->ns_tracker);
88 atomic_dec(&masq_worker_count);
89 module_put(THIS_MODULE);
92 /* Iterate conntrack table in the background and remove conntrack entries
93 * that use the device/address being removed.
95 * In case too many work items have been queued already or memory allocation
96 * fails iteration is skipped, conntrack entries will time out eventually.
98 static void nf_nat_masq_schedule(struct net *net, union nf_inet_addr *addr,
100 int (*iter)(struct nf_conn *i, void *data),
103 struct masq_dev_work *w;
105 if (atomic_read(&masq_worker_count) > MAX_MASQ_WORKER_COUNT)
108 net = maybe_get_net(net);
112 if (!try_module_get(THIS_MODULE))
115 w = kzalloc(sizeof(*w), gfp_flags);
117 /* We can overshoot MAX_MASQ_WORKER_COUNT, no big deal */
118 atomic_inc(&masq_worker_count);
120 INIT_WORK(&w->work, iterate_cleanup_work);
121 w->ifindex = ifindex;
123 netns_tracker_alloc(net, &w->ns_tracker, gfp_flags);
127 schedule_work(&w->work);
131 module_put(THIS_MODULE);
136 static int device_cmp(struct nf_conn *i, void *arg)
138 const struct nf_conn_nat *nat = nfct_nat(i);
139 const struct masq_dev_work *w = arg;
143 return nat->masq_index == w->ifindex;
146 static int masq_device_event(struct notifier_block *this,
150 const struct net_device *dev = netdev_notifier_info_to_dev(ptr);
151 struct net *net = dev_net(dev);
153 if (event == NETDEV_DOWN) {
154 /* Device was downed. Search entire table for
155 * conntracks which were associated with that device,
159 nf_nat_masq_schedule(net, NULL, dev->ifindex,
160 device_cmp, GFP_KERNEL);
166 static int inet_cmp(struct nf_conn *ct, void *ptr)
168 struct nf_conntrack_tuple *tuple;
169 struct masq_dev_work *w = ptr;
171 if (!device_cmp(ct, ptr))
174 tuple = &ct->tuplehash[IP_CT_DIR_REPLY].tuple;
176 return nf_inet_addr_cmp(&w->addr, &tuple->dst.u3);
179 static int masq_inet_event(struct notifier_block *this,
183 const struct in_ifaddr *ifa = ptr;
184 const struct in_device *idev;
185 const struct net_device *dev;
186 union nf_inet_addr addr;
188 if (event != NETDEV_DOWN)
191 /* The masq_dev_notifier will catch the case of the device going
192 * down. So if the inetdev is dead and being destroyed we have
193 * no work to do. Otherwise this is an individual address removal
194 * and we have to perform the flush.
200 memset(&addr, 0, sizeof(addr));
202 addr.ip = ifa->ifa_address;
205 nf_nat_masq_schedule(dev_net(idev->dev), &addr, dev->ifindex,
206 inet_cmp, GFP_KERNEL);
211 static struct notifier_block masq_dev_notifier = {
212 .notifier_call = masq_device_event,
215 static struct notifier_block masq_inet_notifier = {
216 .notifier_call = masq_inet_event,
219 #if IS_ENABLED(CONFIG_IPV6)
221 nat_ipv6_dev_get_saddr(struct net *net, const struct net_device *dev,
222 const struct in6_addr *daddr, unsigned int srcprefs,
223 struct in6_addr *saddr)
225 #ifdef CONFIG_IPV6_MODULE
226 const struct nf_ipv6_ops *v6_ops = nf_get_ipv6_ops();
229 return -EHOSTUNREACH;
231 return v6_ops->dev_get_saddr(net, dev, daddr, srcprefs, saddr);
233 return ipv6_dev_get_saddr(net, dev, daddr, srcprefs, saddr);
238 nf_nat_masquerade_ipv6(struct sk_buff *skb, const struct nf_nat_range2 *range,
239 const struct net_device *out)
241 enum ip_conntrack_info ctinfo;
242 struct nf_conn_nat *nat;
245 struct nf_nat_range2 newrange;
247 ct = nf_ct_get(skb, &ctinfo);
248 WARN_ON(!(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED ||
249 ctinfo == IP_CT_RELATED_REPLY)));
251 if (nat_ipv6_dev_get_saddr(nf_ct_net(ct), out,
252 &ipv6_hdr(skb)->daddr, 0, &src) < 0)
255 nat = nf_ct_nat_ext_add(ct);
257 nat->masq_index = out->ifindex;
259 newrange.flags = range->flags | NF_NAT_RANGE_MAP_IPS;
260 newrange.min_addr.in6 = src;
261 newrange.max_addr.in6 = src;
262 newrange.min_proto = range->min_proto;
263 newrange.max_proto = range->max_proto;
265 return nf_nat_setup_info(ct, &newrange, NF_NAT_MANIP_SRC);
267 EXPORT_SYMBOL_GPL(nf_nat_masquerade_ipv6);
269 /* atomic notifier; can't call nf_ct_iterate_cleanup_net (it can sleep).
271 * Defer it to the system workqueue.
273 * As we can have 'a lot' of inet_events (depending on amount of ipv6
274 * addresses being deleted), we also need to limit work item queue.
276 static int masq_inet6_event(struct notifier_block *this,
277 unsigned long event, void *ptr)
279 struct inet6_ifaddr *ifa = ptr;
280 const struct net_device *dev;
281 union nf_inet_addr addr;
283 if (event != NETDEV_DOWN)
286 dev = ifa->idev->dev;
288 memset(&addr, 0, sizeof(addr));
290 addr.in6 = ifa->addr;
292 nf_nat_masq_schedule(dev_net(dev), &addr, dev->ifindex, inet_cmp,
297 static struct notifier_block masq_inet6_notifier = {
298 .notifier_call = masq_inet6_event,
301 static int nf_nat_masquerade_ipv6_register_notifier(void)
303 return register_inet6addr_notifier(&masq_inet6_notifier);
306 static inline int nf_nat_masquerade_ipv6_register_notifier(void) { return 0; }
309 int nf_nat_masquerade_inet_register_notifiers(void)
313 mutex_lock(&masq_mutex);
314 if (WARN_ON_ONCE(masq_refcnt == UINT_MAX)) {
319 /* check if the notifier was already set */
320 if (++masq_refcnt > 1)
323 /* Register for device down reports */
324 ret = register_netdevice_notifier(&masq_dev_notifier);
327 /* Register IP address change reports */
328 ret = register_inetaddr_notifier(&masq_inet_notifier);
332 ret = nf_nat_masquerade_ipv6_register_notifier();
336 mutex_unlock(&masq_mutex);
339 unregister_inetaddr_notifier(&masq_inet_notifier);
341 unregister_netdevice_notifier(&masq_dev_notifier);
345 mutex_unlock(&masq_mutex);
348 EXPORT_SYMBOL_GPL(nf_nat_masquerade_inet_register_notifiers);
350 void nf_nat_masquerade_inet_unregister_notifiers(void)
352 mutex_lock(&masq_mutex);
353 /* check if the notifiers still have clients */
354 if (--masq_refcnt > 0)
357 unregister_netdevice_notifier(&masq_dev_notifier);
358 unregister_inetaddr_notifier(&masq_inet_notifier);
359 #if IS_ENABLED(CONFIG_IPV6)
360 unregister_inet6addr_notifier(&masq_inet6_notifier);
363 mutex_unlock(&masq_mutex);
365 EXPORT_SYMBOL_GPL(nf_nat_masquerade_inet_unregister_notifiers);