Merge tag 'trace-fixes-v4.5-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git...
[cascardo/linux.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 in6_addr src;
34         struct nf_conn *ct;
35         struct nf_nat_range newrange;
36
37         ct = nf_ct_get(skb, &ctinfo);
38         NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED ||
39                             ctinfo == IP_CT_RELATED_REPLY));
40
41         if (ipv6_dev_get_saddr(nf_ct_net(ct), out,
42                                &ipv6_hdr(skb)->daddr, 0, &src) < 0)
43                 return NF_DROP;
44
45         nfct_nat(ct)->masq_index = out->ifindex;
46
47         newrange.flags          = range->flags | NF_NAT_RANGE_MAP_IPS;
48         newrange.min_addr.in6   = src;
49         newrange.max_addr.in6   = src;
50         newrange.min_proto      = range->min_proto;
51         newrange.max_proto      = range->max_proto;
52
53         return nf_nat_setup_info(ct, &newrange, NF_NAT_MANIP_SRC);
54 }
55 EXPORT_SYMBOL_GPL(nf_nat_masquerade_ipv6);
56
57 static int device_cmp(struct nf_conn *ct, void *ifindex)
58 {
59         const struct nf_conn_nat *nat = nfct_nat(ct);
60
61         if (!nat)
62                 return 0;
63         if (nf_ct_l3num(ct) != NFPROTO_IPV6)
64                 return 0;
65         return nat->masq_index == (int)(long)ifindex;
66 }
67
68 static int masq_device_event(struct notifier_block *this,
69                              unsigned long event, void *ptr)
70 {
71         const struct net_device *dev = netdev_notifier_info_to_dev(ptr);
72         struct net *net = dev_net(dev);
73
74         if (event == NETDEV_DOWN)
75                 nf_ct_iterate_cleanup(net, device_cmp,
76                                       (void *)(long)dev->ifindex, 0, 0);
77
78         return NOTIFY_DONE;
79 }
80
81 static struct notifier_block masq_dev_notifier = {
82         .notifier_call  = masq_device_event,
83 };
84
85 struct masq_dev_work {
86         struct work_struct work;
87         struct net *net;
88         int ifindex;
89 };
90
91 static void iterate_cleanup_work(struct work_struct *work)
92 {
93         struct masq_dev_work *w;
94         long index;
95
96         w = container_of(work, struct masq_dev_work, work);
97
98         index = w->ifindex;
99         nf_ct_iterate_cleanup(w->net, device_cmp, (void *)index, 0, 0);
100
101         put_net(w->net);
102         kfree(w);
103         atomic_dec(&v6_worker_count);
104         module_put(THIS_MODULE);
105 }
106
107 /* ipv6 inet notifier is an atomic notifier, i.e. we cannot
108  * schedule.
109  *
110  * Unfortunately, nf_ct_iterate_cleanup can run for a long
111  * time if there are lots of conntracks and the system
112  * handles high softirq load, so it frequently calls cond_resched
113  * while iterating the conntrack table.
114  *
115  * So we defer nf_ct_iterate_cleanup walk to the system workqueue.
116  *
117  * As we can have 'a lot' of inet_events (depending on amount
118  * of ipv6 addresses being deleted), we also need to add an upper
119  * limit to the number of queued work items.
120  */
121 static int masq_inet_event(struct notifier_block *this,
122                            unsigned long event, void *ptr)
123 {
124         struct inet6_ifaddr *ifa = ptr;
125         const struct net_device *dev;
126         struct masq_dev_work *w;
127         struct net *net;
128
129         if (event != NETDEV_DOWN ||
130             atomic_read(&v6_worker_count) >= MAX_WORK_COUNT)
131                 return NOTIFY_DONE;
132
133         dev = ifa->idev->dev;
134         net = maybe_get_net(dev_net(dev));
135         if (!net)
136                 return NOTIFY_DONE;
137
138         if (!try_module_get(THIS_MODULE))
139                 goto err_module;
140
141         w = kmalloc(sizeof(*w), GFP_ATOMIC);
142         if (w) {
143                 atomic_inc(&v6_worker_count);
144
145                 INIT_WORK(&w->work, iterate_cleanup_work);
146                 w->ifindex = dev->ifindex;
147                 w->net = net;
148                 schedule_work(&w->work);
149
150                 return NOTIFY_DONE;
151         }
152
153         module_put(THIS_MODULE);
154  err_module:
155         put_net(net);
156         return NOTIFY_DONE;
157 }
158
159 static struct notifier_block masq_inet_notifier = {
160         .notifier_call  = masq_inet_event,
161 };
162
163 static atomic_t masquerade_notifier_refcount = ATOMIC_INIT(0);
164
165 void nf_nat_masquerade_ipv6_register_notifier(void)
166 {
167         /* check if the notifier is already set */
168         if (atomic_inc_return(&masquerade_notifier_refcount) > 1)
169                 return;
170
171         register_netdevice_notifier(&masq_dev_notifier);
172         register_inet6addr_notifier(&masq_inet_notifier);
173 }
174 EXPORT_SYMBOL_GPL(nf_nat_masquerade_ipv6_register_notifier);
175
176 void nf_nat_masquerade_ipv6_unregister_notifier(void)
177 {
178         /* check if the notifier still has clients */
179         if (atomic_dec_return(&masquerade_notifier_refcount) > 0)
180                 return;
181
182         unregister_inet6addr_notifier(&masq_inet_notifier);
183         unregister_netdevice_notifier(&masq_dev_notifier);
184 }
185 EXPORT_SYMBOL_GPL(nf_nat_masquerade_ipv6_unregister_notifier);
186
187 MODULE_LICENSE("GPL");
188 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");