]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - net/netfilter/core.c
Merge tag 'arc-4.13-rc7-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/vgupt...
[mirror_ubuntu-artful-kernel.git] / net / netfilter / core.c
1 /* netfilter.c: look after the filters for various protocols.
2 * Heavily influenced by the old firewall.c by David Bonn and Alan Cox.
3 *
4 * Thanks to Rob `CmdrTaco' Malda for not influencing this code in any
5 * way.
6 *
7 * Rusty Russell (C)2000 -- This code is GPL.
8 * Patrick McHardy (c) 2006-2012
9 */
10 #include <linux/kernel.h>
11 #include <linux/netfilter.h>
12 #include <net/protocol.h>
13 #include <linux/init.h>
14 #include <linux/skbuff.h>
15 #include <linux/wait.h>
16 #include <linux/module.h>
17 #include <linux/interrupt.h>
18 #include <linux/if.h>
19 #include <linux/netdevice.h>
20 #include <linux/netfilter_ipv6.h>
21 #include <linux/inetdevice.h>
22 #include <linux/proc_fs.h>
23 #include <linux/mutex.h>
24 #include <linux/slab.h>
25 #include <linux/rcupdate.h>
26 #include <net/net_namespace.h>
27 #include <net/sock.h>
28
29 #include "nf_internals.h"
30
31 static DEFINE_MUTEX(afinfo_mutex);
32
33 const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO] __read_mostly;
34 EXPORT_SYMBOL(nf_afinfo);
35 const struct nf_ipv6_ops __rcu *nf_ipv6_ops __read_mostly;
36 EXPORT_SYMBOL_GPL(nf_ipv6_ops);
37
38 DEFINE_PER_CPU(bool, nf_skb_duplicated);
39 EXPORT_SYMBOL_GPL(nf_skb_duplicated);
40
41 int nf_register_afinfo(const struct nf_afinfo *afinfo)
42 {
43 mutex_lock(&afinfo_mutex);
44 RCU_INIT_POINTER(nf_afinfo[afinfo->family], afinfo);
45 mutex_unlock(&afinfo_mutex);
46 return 0;
47 }
48 EXPORT_SYMBOL_GPL(nf_register_afinfo);
49
50 void nf_unregister_afinfo(const struct nf_afinfo *afinfo)
51 {
52 mutex_lock(&afinfo_mutex);
53 RCU_INIT_POINTER(nf_afinfo[afinfo->family], NULL);
54 mutex_unlock(&afinfo_mutex);
55 synchronize_rcu();
56 }
57 EXPORT_SYMBOL_GPL(nf_unregister_afinfo);
58
59 #ifdef HAVE_JUMP_LABEL
60 struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
61 EXPORT_SYMBOL(nf_hooks_needed);
62 #endif
63
64 static DEFINE_MUTEX(nf_hook_mutex);
65 #define nf_entry_dereference(e) \
66 rcu_dereference_protected(e, lockdep_is_held(&nf_hook_mutex))
67
68 static struct nf_hook_entry __rcu **nf_hook_entry_head(struct net *net, const struct nf_hook_ops *reg)
69 {
70 if (reg->pf != NFPROTO_NETDEV)
71 return net->nf.hooks[reg->pf]+reg->hooknum;
72
73 #ifdef CONFIG_NETFILTER_INGRESS
74 if (reg->hooknum == NF_NETDEV_INGRESS) {
75 if (reg->dev && dev_net(reg->dev) == net)
76 return &reg->dev->nf_hooks_ingress;
77 }
78 #endif
79 return NULL;
80 }
81
82 int nf_register_net_hook(struct net *net, const struct nf_hook_ops *reg)
83 {
84 struct nf_hook_entry __rcu **pp;
85 struct nf_hook_entry *entry, *p;
86
87 if (reg->pf == NFPROTO_NETDEV) {
88 #ifndef CONFIG_NETFILTER_INGRESS
89 if (reg->hooknum == NF_NETDEV_INGRESS)
90 return -EOPNOTSUPP;
91 #endif
92 if (reg->hooknum != NF_NETDEV_INGRESS ||
93 !reg->dev || dev_net(reg->dev) != net)
94 return -EINVAL;
95 }
96
97 pp = nf_hook_entry_head(net, reg);
98 if (!pp)
99 return -EINVAL;
100
101 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
102 if (!entry)
103 return -ENOMEM;
104
105 nf_hook_entry_init(entry, reg);
106
107 mutex_lock(&nf_hook_mutex);
108
109 /* Find the spot in the list */
110 for (; (p = nf_entry_dereference(*pp)) != NULL; pp = &p->next) {
111 if (reg->priority < nf_hook_entry_priority(p))
112 break;
113 }
114 rcu_assign_pointer(entry->next, p);
115 rcu_assign_pointer(*pp, entry);
116
117 mutex_unlock(&nf_hook_mutex);
118 #ifdef CONFIG_NETFILTER_INGRESS
119 if (reg->pf == NFPROTO_NETDEV && reg->hooknum == NF_NETDEV_INGRESS)
120 net_inc_ingress_queue();
121 #endif
122 #ifdef HAVE_JUMP_LABEL
123 static_key_slow_inc(&nf_hooks_needed[reg->pf][reg->hooknum]);
124 #endif
125 return 0;
126 }
127 EXPORT_SYMBOL(nf_register_net_hook);
128
129 static struct nf_hook_entry *
130 __nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *reg)
131 {
132 struct nf_hook_entry __rcu **pp;
133 struct nf_hook_entry *p;
134
135 pp = nf_hook_entry_head(net, reg);
136 if (WARN_ON_ONCE(!pp))
137 return NULL;
138
139 mutex_lock(&nf_hook_mutex);
140 for (; (p = nf_entry_dereference(*pp)) != NULL; pp = &p->next) {
141 if (nf_hook_entry_ops(p) == reg) {
142 rcu_assign_pointer(*pp, p->next);
143 break;
144 }
145 }
146 mutex_unlock(&nf_hook_mutex);
147 if (!p) {
148 WARN(1, "nf_unregister_net_hook: hook not found!\n");
149 return NULL;
150 }
151 #ifdef CONFIG_NETFILTER_INGRESS
152 if (reg->pf == NFPROTO_NETDEV && reg->hooknum == NF_NETDEV_INGRESS)
153 net_dec_ingress_queue();
154 #endif
155 #ifdef HAVE_JUMP_LABEL
156 static_key_slow_dec(&nf_hooks_needed[reg->pf][reg->hooknum]);
157 #endif
158
159 return p;
160 }
161
162 void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *reg)
163 {
164 struct nf_hook_entry *p = __nf_unregister_net_hook(net, reg);
165 unsigned int nfq;
166
167 if (!p)
168 return;
169
170 synchronize_net();
171
172 /* other cpu might still process nfqueue verdict that used reg */
173 nfq = nf_queue_nf_hook_drop(net);
174 if (nfq)
175 synchronize_net();
176 kfree(p);
177 }
178 EXPORT_SYMBOL(nf_unregister_net_hook);
179
180 int nf_register_net_hooks(struct net *net, const struct nf_hook_ops *reg,
181 unsigned int n)
182 {
183 unsigned int i;
184 int err = 0;
185
186 for (i = 0; i < n; i++) {
187 err = nf_register_net_hook(net, &reg[i]);
188 if (err)
189 goto err;
190 }
191 return err;
192
193 err:
194 if (i > 0)
195 nf_unregister_net_hooks(net, reg, i);
196 return err;
197 }
198 EXPORT_SYMBOL(nf_register_net_hooks);
199
200 void nf_unregister_net_hooks(struct net *net, const struct nf_hook_ops *reg,
201 unsigned int hookcount)
202 {
203 struct nf_hook_entry *to_free[16];
204 unsigned int i, n, nfq;
205
206 do {
207 n = min_t(unsigned int, hookcount, ARRAY_SIZE(to_free));
208
209 for (i = 0; i < n; i++)
210 to_free[i] = __nf_unregister_net_hook(net, &reg[i]);
211
212 synchronize_net();
213
214 /* need 2nd synchronize_net() if nfqueue is used, skb
215 * can get reinjected right before nf_queue_hook_drop()
216 */
217 nfq = nf_queue_nf_hook_drop(net);
218 if (nfq)
219 synchronize_net();
220
221 for (i = 0; i < n; i++)
222 kfree(to_free[i]);
223
224 reg += n;
225 hookcount -= n;
226 } while (hookcount > 0);
227 }
228 EXPORT_SYMBOL(nf_unregister_net_hooks);
229
230 /* Returns 1 if okfn() needs to be executed by the caller,
231 * -EPERM for NF_DROP, 0 otherwise. Caller must hold rcu_read_lock. */
232 int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state,
233 struct nf_hook_entry *entry)
234 {
235 unsigned int verdict;
236 int ret;
237
238 do {
239 verdict = nf_hook_entry_hookfn(entry, skb, state);
240 switch (verdict & NF_VERDICT_MASK) {
241 case NF_ACCEPT:
242 entry = rcu_dereference(entry->next);
243 break;
244 case NF_DROP:
245 kfree_skb(skb);
246 ret = NF_DROP_GETERR(verdict);
247 if (ret == 0)
248 ret = -EPERM;
249 return ret;
250 case NF_QUEUE:
251 ret = nf_queue(skb, state, &entry, verdict);
252 if (ret == 1 && entry)
253 continue;
254 return ret;
255 default:
256 /* Implicit handling for NF_STOLEN, as well as any other
257 * non conventional verdicts.
258 */
259 return 0;
260 }
261 } while (entry);
262
263 return 1;
264 }
265 EXPORT_SYMBOL(nf_hook_slow);
266
267
268 int skb_make_writable(struct sk_buff *skb, unsigned int writable_len)
269 {
270 if (writable_len > skb->len)
271 return 0;
272
273 /* Not exclusive use of packet? Must copy. */
274 if (!skb_cloned(skb)) {
275 if (writable_len <= skb_headlen(skb))
276 return 1;
277 } else if (skb_clone_writable(skb, writable_len))
278 return 1;
279
280 if (writable_len <= skb_headlen(skb))
281 writable_len = 0;
282 else
283 writable_len -= skb_headlen(skb);
284
285 return !!__pskb_pull_tail(skb, writable_len);
286 }
287 EXPORT_SYMBOL(skb_make_writable);
288
289 /* This needs to be compiled in any case to avoid dependencies between the
290 * nfnetlink_queue code and nf_conntrack.
291 */
292 struct nfnl_ct_hook __rcu *nfnl_ct_hook __read_mostly;
293 EXPORT_SYMBOL_GPL(nfnl_ct_hook);
294
295 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
296 /* This does not belong here, but locally generated errors need it if connection
297 tracking in use: without this, connection may not be in hash table, and hence
298 manufactured ICMP or RST packets will not be associated with it. */
299 void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *)
300 __rcu __read_mostly;
301 EXPORT_SYMBOL(ip_ct_attach);
302
303 void nf_ct_attach(struct sk_buff *new, const struct sk_buff *skb)
304 {
305 void (*attach)(struct sk_buff *, const struct sk_buff *);
306
307 if (skb->_nfct) {
308 rcu_read_lock();
309 attach = rcu_dereference(ip_ct_attach);
310 if (attach)
311 attach(new, skb);
312 rcu_read_unlock();
313 }
314 }
315 EXPORT_SYMBOL(nf_ct_attach);
316
317 void (*nf_ct_destroy)(struct nf_conntrack *) __rcu __read_mostly;
318 EXPORT_SYMBOL(nf_ct_destroy);
319
320 void nf_conntrack_destroy(struct nf_conntrack *nfct)
321 {
322 void (*destroy)(struct nf_conntrack *);
323
324 rcu_read_lock();
325 destroy = rcu_dereference(nf_ct_destroy);
326 BUG_ON(destroy == NULL);
327 destroy(nfct);
328 rcu_read_unlock();
329 }
330 EXPORT_SYMBOL(nf_conntrack_destroy);
331
332 /* Built-in default zone used e.g. by modules. */
333 const struct nf_conntrack_zone nf_ct_zone_dflt = {
334 .id = NF_CT_DEFAULT_ZONE_ID,
335 .dir = NF_CT_DEFAULT_ZONE_DIR,
336 };
337 EXPORT_SYMBOL_GPL(nf_ct_zone_dflt);
338 #endif /* CONFIG_NF_CONNTRACK */
339
340 #ifdef CONFIG_NF_NAT_NEEDED
341 void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *);
342 EXPORT_SYMBOL(nf_nat_decode_session_hook);
343 #endif
344
345 static int __net_init netfilter_net_init(struct net *net)
346 {
347 int i, h;
348
349 for (i = 0; i < ARRAY_SIZE(net->nf.hooks); i++) {
350 for (h = 0; h < NF_MAX_HOOKS; h++)
351 RCU_INIT_POINTER(net->nf.hooks[i][h], NULL);
352 }
353
354 #ifdef CONFIG_PROC_FS
355 net->nf.proc_netfilter = proc_net_mkdir(net, "netfilter",
356 net->proc_net);
357 if (!net->nf.proc_netfilter) {
358 if (!net_eq(net, &init_net))
359 pr_err("cannot create netfilter proc entry");
360
361 return -ENOMEM;
362 }
363 #endif
364
365 return 0;
366 }
367
368 static void __net_exit netfilter_net_exit(struct net *net)
369 {
370 remove_proc_entry("netfilter", net->proc_net);
371 }
372
373 static struct pernet_operations netfilter_net_ops = {
374 .init = netfilter_net_init,
375 .exit = netfilter_net_exit,
376 };
377
378 int __init netfilter_init(void)
379 {
380 int ret;
381
382 ret = register_pernet_subsys(&netfilter_net_ops);
383 if (ret < 0)
384 goto err;
385
386 ret = netfilter_log_init();
387 if (ret < 0)
388 goto err_pernet;
389
390 return 0;
391 err_pernet:
392 unregister_pernet_subsys(&netfilter_net_ops);
393 err:
394 return ret;
395 }