GNU Linux-libre 4.14.266-gnu1
[releases.git] / net / ipv6 / netfilter / nf_nat_masquerade_ipv6.c
1 /*
2  * Copyright (c) 2011 Patrick McHardy <kaber@trash.net>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * Based on Rusty Russell's IPv6 MASQUERADE target. Development of IPv6
9  * NAT funded by Astaro.
10  */
11
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/atomic.h>
15 #include <linux/netdevice.h>
16 #include <linux/ipv6.h>
17 #include <linux/netfilter.h>
18 #include <linux/netfilter_ipv6.h>
19 #include <net/netfilter/nf_nat.h>
20 #include <net/addrconf.h>
21 #include <net/ipv6.h>
22 #include <net/netfilter/ipv6/nf_nat_masquerade.h>
23
24 #define MAX_WORK_COUNT  16
25
26 static atomic_t v6_worker_count;
27
28 unsigned int
29 nf_nat_masquerade_ipv6(struct sk_buff *skb, const struct nf_nat_range *range,
30                        const struct net_device *out)
31 {
32         enum ip_conntrack_info ctinfo;
33         struct nf_conn_nat *nat;
34         struct in6_addr src;
35         struct nf_conn *ct;
36         struct nf_nat_range newrange;
37
38         ct = nf_ct_get(skb, &ctinfo);
39         WARN_ON(!(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED ||
40                          ctinfo == IP_CT_RELATED_REPLY)));
41
42         if (ipv6_dev_get_saddr(nf_ct_net(ct), out,
43                                &ipv6_hdr(skb)->daddr, 0, &src) < 0)
44                 return NF_DROP;
45
46         nat = nf_ct_nat_ext_add(ct);
47         if (nat)
48                 nat->masq_index = out->ifindex;
49
50         newrange.flags          = range->flags | NF_NAT_RANGE_MAP_IPS;
51         newrange.min_addr.in6   = src;
52         newrange.max_addr.in6   = src;
53         newrange.min_proto      = range->min_proto;
54         newrange.max_proto      = range->max_proto;
55
56         return nf_nat_setup_info(ct, &newrange, NF_NAT_MANIP_SRC);
57 }
58 EXPORT_SYMBOL_GPL(nf_nat_masquerade_ipv6);
59
60 static int device_cmp(struct nf_conn *ct, void *ifindex)
61 {
62         const struct nf_conn_nat *nat = nfct_nat(ct);
63
64         if (!nat)
65                 return 0;
66         if (nf_ct_l3num(ct) != NFPROTO_IPV6)
67                 return 0;
68         return nat->masq_index == (int)(long)ifindex;
69 }
70
71 static int masq_device_event(struct notifier_block *this,
72                              unsigned long event, void *ptr)
73 {
74         const struct net_device *dev = netdev_notifier_info_to_dev(ptr);
75         struct net *net = dev_net(dev);
76
77         if (event == NETDEV_DOWN)
78                 nf_ct_iterate_cleanup_net(net, device_cmp,
79                                           (void *)(long)dev->ifindex, 0, 0);
80
81         return NOTIFY_DONE;
82 }
83
84 static struct notifier_block masq_dev_notifier = {
85         .notifier_call  = masq_device_event,
86 };
87
88 struct masq_dev_work {
89         struct work_struct work;
90         struct net *net;
91         struct in6_addr addr;
92         int ifindex;
93 };
94
95 static int inet_cmp(struct nf_conn *ct, void *work)
96 {
97         struct masq_dev_work *w = (struct masq_dev_work *)work;
98         struct nf_conntrack_tuple *tuple;
99
100         if (!device_cmp(ct, (void *)(long)w->ifindex))
101                 return 0;
102
103         tuple = &ct->tuplehash[IP_CT_DIR_REPLY].tuple;
104
105         return ipv6_addr_equal(&w->addr, &tuple->dst.u3.in6);
106 }
107
108 static void iterate_cleanup_work(struct work_struct *work)
109 {
110         struct masq_dev_work *w;
111
112         w = container_of(work, struct masq_dev_work, work);
113
114         nf_ct_iterate_cleanup_net(w->net, inet_cmp, (void *)w, 0, 0);
115
116         put_net(w->net);
117         kfree(w);
118         atomic_dec(&v6_worker_count);
119         module_put(THIS_MODULE);
120 }
121
122 /* ipv6 inet notifier is an atomic notifier, i.e. we cannot
123  * schedule.
124  *
125  * Unfortunately, nf_ct_iterate_cleanup_net can run for a long
126  * time if there are lots of conntracks and the system
127  * handles high softirq load, so it frequently calls cond_resched
128  * while iterating the conntrack table.
129  *
130  * So we defer nf_ct_iterate_cleanup_net walk to the system workqueue.
131  *
132  * As we can have 'a lot' of inet_events (depending on amount
133  * of ipv6 addresses being deleted), we also need to add an upper
134  * limit to the number of queued work items.
135  */
136 static int masq_inet_event(struct notifier_block *this,
137                            unsigned long event, void *ptr)
138 {
139         struct inet6_ifaddr *ifa = ptr;
140         const struct net_device *dev;
141         struct masq_dev_work *w;
142         struct net *net;
143
144         if (event != NETDEV_DOWN ||
145             atomic_read(&v6_worker_count) >= MAX_WORK_COUNT)
146                 return NOTIFY_DONE;
147
148         dev = ifa->idev->dev;
149         net = maybe_get_net(dev_net(dev));
150         if (!net)
151                 return NOTIFY_DONE;
152
153         if (!try_module_get(THIS_MODULE))
154                 goto err_module;
155
156         w = kmalloc(sizeof(*w), GFP_ATOMIC);
157         if (w) {
158                 atomic_inc(&v6_worker_count);
159
160                 INIT_WORK(&w->work, iterate_cleanup_work);
161                 w->ifindex = dev->ifindex;
162                 w->net = net;
163                 w->addr = ifa->addr;
164                 schedule_work(&w->work);
165
166                 return NOTIFY_DONE;
167         }
168
169         module_put(THIS_MODULE);
170  err_module:
171         put_net(net);
172         return NOTIFY_DONE;
173 }
174
175 static struct notifier_block masq_inet_notifier = {
176         .notifier_call  = masq_inet_event,
177 };
178
179 static atomic_t masquerade_notifier_refcount = ATOMIC_INIT(0);
180
181 void nf_nat_masquerade_ipv6_register_notifier(void)
182 {
183         /* check if the notifier is already set */
184         if (atomic_inc_return(&masquerade_notifier_refcount) > 1)
185                 return;
186
187         register_netdevice_notifier(&masq_dev_notifier);
188         register_inet6addr_notifier(&masq_inet_notifier);
189 }
190 EXPORT_SYMBOL_GPL(nf_nat_masquerade_ipv6_register_notifier);
191
192 void nf_nat_masquerade_ipv6_unregister_notifier(void)
193 {
194         /* check if the notifier still has clients */
195         if (atomic_dec_return(&masquerade_notifier_refcount) > 0)
196                 return;
197
198         unregister_inet6addr_notifier(&masq_inet_notifier);
199         unregister_netdevice_notifier(&masq_dev_notifier);
200 }
201 EXPORT_SYMBOL_GPL(nf_nat_masquerade_ipv6_unregister_notifier);
202
203 MODULE_LICENSE("GPL");
204 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");