GNU Linux-libre 4.14.290-gnu1
[releases.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/mm.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
66 /* max hooks per family/hooknum */
67 #define MAX_HOOK_COUNT          1024
68
69 #define nf_entry_dereference(e) \
70         rcu_dereference_protected(e, lockdep_is_held(&nf_hook_mutex))
71
72 static struct nf_hook_entries *allocate_hook_entries_size(u16 num)
73 {
74         struct nf_hook_entries *e;
75         size_t alloc = sizeof(*e) +
76                        sizeof(struct nf_hook_entry) * num +
77                        sizeof(struct nf_hook_ops *) * num;
78
79         if (num == 0)
80                 return NULL;
81
82         e = kvzalloc(alloc, GFP_KERNEL);
83         if (e)
84                 e->num_hook_entries = num;
85         return e;
86 }
87
88 static unsigned int accept_all(void *priv,
89                                struct sk_buff *skb,
90                                const struct nf_hook_state *state)
91 {
92         return NF_ACCEPT; /* ACCEPT makes nf_hook_slow call next hook */
93 }
94
95 static const struct nf_hook_ops dummy_ops = {
96         .hook = accept_all,
97         .priority = INT_MIN,
98 };
99
100 static struct nf_hook_entries *
101 nf_hook_entries_grow(const struct nf_hook_entries *old,
102                      const struct nf_hook_ops *reg)
103 {
104         unsigned int i, alloc_entries, nhooks, old_entries;
105         struct nf_hook_ops **orig_ops = NULL;
106         struct nf_hook_ops **new_ops;
107         struct nf_hook_entries *new;
108         bool inserted = false;
109
110         alloc_entries = 1;
111         old_entries = old ? old->num_hook_entries : 0;
112
113         if (old) {
114                 orig_ops = nf_hook_entries_get_hook_ops(old);
115
116                 for (i = 0; i < old_entries; i++) {
117                         if (orig_ops[i] != &dummy_ops)
118                                 alloc_entries++;
119                 }
120         }
121
122         if (alloc_entries > MAX_HOOK_COUNT)
123                 return ERR_PTR(-E2BIG);
124
125         new = allocate_hook_entries_size(alloc_entries);
126         if (!new)
127                 return ERR_PTR(-ENOMEM);
128
129         new_ops = nf_hook_entries_get_hook_ops(new);
130
131         i = 0;
132         nhooks = 0;
133         while (i < old_entries) {
134                 if (orig_ops[i] == &dummy_ops) {
135                         ++i;
136                         continue;
137                 }
138                 if (inserted || reg->priority > orig_ops[i]->priority) {
139                         new_ops[nhooks] = (void *)orig_ops[i];
140                         new->hooks[nhooks] = old->hooks[i];
141                         i++;
142                 } else {
143                         new_ops[nhooks] = (void *)reg;
144                         new->hooks[nhooks].hook = reg->hook;
145                         new->hooks[nhooks].priv = reg->priv;
146                         inserted = true;
147                 }
148                 nhooks++;
149         }
150
151         if (!inserted) {
152                 new_ops[nhooks] = (void *)reg;
153                 new->hooks[nhooks].hook = reg->hook;
154                 new->hooks[nhooks].priv = reg->priv;
155         }
156
157         return new;
158 }
159
160 static void hooks_validate(const struct nf_hook_entries *hooks)
161 {
162 #ifdef CONFIG_DEBUG_KERNEL
163         struct nf_hook_ops **orig_ops;
164         int prio = INT_MIN;
165         size_t i = 0;
166
167         orig_ops = nf_hook_entries_get_hook_ops(hooks);
168
169         for (i = 0; i < hooks->num_hook_entries; i++) {
170                 if (orig_ops[i] == &dummy_ops)
171                         continue;
172
173                 WARN_ON(orig_ops[i]->priority < prio);
174
175                 if (orig_ops[i]->priority > prio)
176                         prio = orig_ops[i]->priority;
177         }
178 #endif
179 }
180
181 /*
182  * __nf_hook_entries_try_shrink - try to shrink hook array
183  *
184  * @pp -- location of hook blob
185  *
186  * Hook unregistration must always succeed, so to-be-removed hooks
187  * are replaced by a dummy one that will just move to next hook.
188  *
189  * This counts the current dummy hooks, attempts to allocate new blob,
190  * copies the live hooks, then replaces and discards old one.
191  *
192  * return values:
193  *
194  * Returns address to free, or NULL.
195  */
196 static void *__nf_hook_entries_try_shrink(struct nf_hook_entries __rcu **pp)
197 {
198         struct nf_hook_entries *old, *new = NULL;
199         unsigned int i, j, skip = 0, hook_entries;
200         struct nf_hook_ops **orig_ops;
201         struct nf_hook_ops **new_ops;
202
203         old = nf_entry_dereference(*pp);
204         if (WARN_ON_ONCE(!old))
205                 return NULL;
206
207         orig_ops = nf_hook_entries_get_hook_ops(old);
208         for (i = 0; i < old->num_hook_entries; i++) {
209                 if (orig_ops[i] == &dummy_ops)
210                         skip++;
211         }
212
213         /* if skip == hook_entries all hooks have been removed */
214         hook_entries = old->num_hook_entries;
215         if (skip == hook_entries)
216                 goto out_assign;
217
218         if (skip == 0)
219                 return NULL;
220
221         hook_entries -= skip;
222         new = allocate_hook_entries_size(hook_entries);
223         if (!new)
224                 return NULL;
225
226         new_ops = nf_hook_entries_get_hook_ops(new);
227         for (i = 0, j = 0; i < old->num_hook_entries; i++) {
228                 if (orig_ops[i] == &dummy_ops)
229                         continue;
230                 new->hooks[j] = old->hooks[i];
231                 new_ops[j] = (void *)orig_ops[i];
232                 j++;
233         }
234         hooks_validate(new);
235 out_assign:
236         rcu_assign_pointer(*pp, new);
237         return old;
238 }
239
240 static struct nf_hook_entries __rcu **nf_hook_entry_head(struct net *net, const struct nf_hook_ops *reg)
241 {
242         if (reg->pf != NFPROTO_NETDEV)
243                 return net->nf.hooks[reg->pf]+reg->hooknum;
244
245 #ifdef CONFIG_NETFILTER_INGRESS
246         if (reg->hooknum == NF_NETDEV_INGRESS) {
247                 if (reg->dev && dev_net(reg->dev) == net)
248                         return &reg->dev->nf_hooks_ingress;
249         }
250 #endif
251         WARN_ON_ONCE(1);
252         return NULL;
253 }
254
255 int nf_register_net_hook(struct net *net, const struct nf_hook_ops *reg)
256 {
257         struct nf_hook_entries *p, *new_hooks;
258         struct nf_hook_entries __rcu **pp;
259
260         if (reg->pf == NFPROTO_NETDEV) {
261 #ifndef CONFIG_NETFILTER_INGRESS
262                 if (reg->hooknum == NF_NETDEV_INGRESS)
263                         return -EOPNOTSUPP;
264 #endif
265                 if (reg->hooknum != NF_NETDEV_INGRESS ||
266                     !reg->dev || dev_net(reg->dev) != net)
267                         return -EINVAL;
268         }
269
270         pp = nf_hook_entry_head(net, reg);
271         if (!pp)
272                 return -EINVAL;
273
274         mutex_lock(&nf_hook_mutex);
275
276         p = nf_entry_dereference(*pp);
277         new_hooks = nf_hook_entries_grow(p, reg);
278
279         if (!IS_ERR(new_hooks)) {
280                 hooks_validate(new_hooks);
281                 rcu_assign_pointer(*pp, new_hooks);
282         }
283
284         mutex_unlock(&nf_hook_mutex);
285         if (IS_ERR(new_hooks))
286                 return PTR_ERR(new_hooks);
287
288 #ifdef CONFIG_NETFILTER_INGRESS
289         if (reg->pf == NFPROTO_NETDEV && reg->hooknum == NF_NETDEV_INGRESS)
290                 net_inc_ingress_queue();
291 #endif
292 #ifdef HAVE_JUMP_LABEL
293         static_key_slow_inc(&nf_hooks_needed[reg->pf][reg->hooknum]);
294 #endif
295         synchronize_net();
296         BUG_ON(p == new_hooks);
297         kvfree(p);
298         return 0;
299 }
300 EXPORT_SYMBOL(nf_register_net_hook);
301
302 /*
303  * __nf_unregister_net_hook - remove a hook from blob
304  *
305  * @oldp: current address of hook blob
306  * @unreg: hook to unregister
307  *
308  * This cannot fail, hook unregistration must always succeed.
309  * Therefore replace the to-be-removed hook with a dummy hook.
310  */
311 static void __nf_unregister_net_hook(struct nf_hook_entries *old,
312                                      const struct nf_hook_ops *unreg)
313 {
314         struct nf_hook_ops **orig_ops;
315         bool found = false;
316         unsigned int i;
317
318         orig_ops = nf_hook_entries_get_hook_ops(old);
319         for (i = 0; i < old->num_hook_entries; i++) {
320                 if (orig_ops[i] != unreg)
321                         continue;
322                 WRITE_ONCE(old->hooks[i].hook, accept_all);
323                 WRITE_ONCE(orig_ops[i], &dummy_ops);
324                 found = true;
325                 break;
326         }
327
328         if (found) {
329 #ifdef CONFIG_NETFILTER_INGRESS
330                 if (unreg->pf == NFPROTO_NETDEV && unreg->hooknum == NF_NETDEV_INGRESS)
331                         net_dec_ingress_queue();
332 #endif
333 #ifdef HAVE_JUMP_LABEL
334                 static_key_slow_dec(&nf_hooks_needed[unreg->pf][unreg->hooknum]);
335 #endif
336         } else {
337                 WARN_ONCE(1, "hook not found, pf %d num %d", unreg->pf, unreg->hooknum);
338         }
339 }
340
341 void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *reg)
342 {
343         struct nf_hook_entries __rcu **pp;
344         struct nf_hook_entries *p;
345         unsigned int nfq;
346
347         pp = nf_hook_entry_head(net, reg);
348         if (!pp)
349                 return;
350
351         mutex_lock(&nf_hook_mutex);
352
353         p = nf_entry_dereference(*pp);
354         if (WARN_ON_ONCE(!p)) {
355                 mutex_unlock(&nf_hook_mutex);
356                 return;
357         }
358
359         __nf_unregister_net_hook(p, reg);
360
361         p = __nf_hook_entries_try_shrink(pp);
362         mutex_unlock(&nf_hook_mutex);
363         if (!p)
364                 return;
365
366         synchronize_net();
367
368         /* other cpu might still process nfqueue verdict that used reg */
369         nfq = nf_queue_nf_hook_drop(net);
370         if (nfq)
371                 synchronize_net();
372         kvfree(p);
373 }
374 EXPORT_SYMBOL(nf_unregister_net_hook);
375
376 int nf_register_net_hooks(struct net *net, const struct nf_hook_ops *reg,
377                           unsigned int n)
378 {
379         unsigned int i;
380         int err = 0;
381
382         for (i = 0; i < n; i++) {
383                 err = nf_register_net_hook(net, &reg[i]);
384                 if (err)
385                         goto err;
386         }
387         return err;
388
389 err:
390         if (i > 0)
391                 nf_unregister_net_hooks(net, reg, i);
392         return err;
393 }
394 EXPORT_SYMBOL(nf_register_net_hooks);
395
396 void nf_unregister_net_hooks(struct net *net, const struct nf_hook_ops *reg,
397                              unsigned int hookcount)
398 {
399         struct nf_hook_entries *to_free[16], *p;
400         struct nf_hook_entries __rcu **pp;
401         unsigned int i, j, n;
402
403         mutex_lock(&nf_hook_mutex);
404         for (i = 0; i < hookcount; i++) {
405                 pp = nf_hook_entry_head(net, &reg[i]);
406                 if (!pp)
407                         continue;
408
409                 p = nf_entry_dereference(*pp);
410                 if (WARN_ON_ONCE(!p))
411                         continue;
412                 __nf_unregister_net_hook(p, &reg[i]);
413         }
414         mutex_unlock(&nf_hook_mutex);
415
416         do {
417                 n = min_t(unsigned int, hookcount, ARRAY_SIZE(to_free));
418
419                 mutex_lock(&nf_hook_mutex);
420
421                 for (i = 0, j = 0; i < hookcount && j < n; i++) {
422                         pp = nf_hook_entry_head(net, &reg[i]);
423                         if (!pp)
424                                 continue;
425
426                         p = nf_entry_dereference(*pp);
427                         if (!p)
428                                 continue;
429
430                         to_free[j] = __nf_hook_entries_try_shrink(pp);
431                         if (to_free[j])
432                                 ++j;
433                 }
434
435                 mutex_unlock(&nf_hook_mutex);
436
437                 if (j) {
438                         unsigned int nfq;
439
440                         synchronize_net();
441
442                         /* need 2nd synchronize_net() if nfqueue is used, skb
443                          * can get reinjected right before nf_queue_hook_drop()
444                          */
445                         nfq = nf_queue_nf_hook_drop(net);
446                         if (nfq)
447                                 synchronize_net();
448
449                         for (i = 0; i < j; i++)
450                                 kvfree(to_free[i]);
451                 }
452
453                 reg += n;
454                 hookcount -= n;
455         } while (hookcount > 0);
456 }
457 EXPORT_SYMBOL(nf_unregister_net_hooks);
458
459 /* Returns 1 if okfn() needs to be executed by the caller,
460  * -EPERM for NF_DROP, 0 otherwise.  Caller must hold rcu_read_lock. */
461 int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state,
462                  const struct nf_hook_entries *e, unsigned int s)
463 {
464         unsigned int verdict;
465         int ret;
466
467         for (; s < e->num_hook_entries; s++) {
468                 verdict = nf_hook_entry_hookfn(&e->hooks[s], skb, state);
469                 switch (verdict & NF_VERDICT_MASK) {
470                 case NF_ACCEPT:
471                         break;
472                 case NF_DROP:
473                         kfree_skb(skb);
474                         ret = NF_DROP_GETERR(verdict);
475                         if (ret == 0)
476                                 ret = -EPERM;
477                         return ret;
478                 case NF_QUEUE:
479                         ret = nf_queue(skb, state, e, s, verdict);
480                         if (ret == 1)
481                                 continue;
482                         return ret;
483                 default:
484                         /* Implicit handling for NF_STOLEN, as well as any other
485                          * non conventional verdicts.
486                          */
487                         return 0;
488                 }
489         }
490
491         return 1;
492 }
493 EXPORT_SYMBOL(nf_hook_slow);
494
495
496 int skb_make_writable(struct sk_buff *skb, unsigned int writable_len)
497 {
498         if (writable_len > skb->len)
499                 return 0;
500
501         /* Not exclusive use of packet?  Must copy. */
502         if (!skb_cloned(skb)) {
503                 if (writable_len <= skb_headlen(skb))
504                         return 1;
505         } else if (skb_clone_writable(skb, writable_len))
506                 return 1;
507
508         if (writable_len <= skb_headlen(skb))
509                 writable_len = 0;
510         else
511                 writable_len -= skb_headlen(skb);
512
513         return !!__pskb_pull_tail(skb, writable_len);
514 }
515 EXPORT_SYMBOL(skb_make_writable);
516
517 /* This needs to be compiled in any case to avoid dependencies between the
518  * nfnetlink_queue code and nf_conntrack.
519  */
520 struct nfnl_ct_hook __rcu *nfnl_ct_hook __read_mostly;
521 EXPORT_SYMBOL_GPL(nfnl_ct_hook);
522
523 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
524 /* This does not belong here, but locally generated errors need it if connection
525    tracking in use: without this, connection may not be in hash table, and hence
526    manufactured ICMP or RST packets will not be associated with it. */
527 void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *)
528                 __rcu __read_mostly;
529 EXPORT_SYMBOL(ip_ct_attach);
530
531 void nf_ct_attach(struct sk_buff *new, const struct sk_buff *skb)
532 {
533         void (*attach)(struct sk_buff *, const struct sk_buff *);
534
535         if (skb->_nfct) {
536                 rcu_read_lock();
537                 attach = rcu_dereference(ip_ct_attach);
538                 if (attach)
539                         attach(new, skb);
540                 rcu_read_unlock();
541         }
542 }
543 EXPORT_SYMBOL(nf_ct_attach);
544
545 void (*nf_ct_destroy)(struct nf_conntrack *) __rcu __read_mostly;
546 EXPORT_SYMBOL(nf_ct_destroy);
547
548 void nf_conntrack_destroy(struct nf_conntrack *nfct)
549 {
550         void (*destroy)(struct nf_conntrack *);
551
552         rcu_read_lock();
553         destroy = rcu_dereference(nf_ct_destroy);
554         BUG_ON(destroy == NULL);
555         destroy(nfct);
556         rcu_read_unlock();
557 }
558 EXPORT_SYMBOL(nf_conntrack_destroy);
559
560 /* Built-in default zone used e.g. by modules. */
561 const struct nf_conntrack_zone nf_ct_zone_dflt = {
562         .id     = NF_CT_DEFAULT_ZONE_ID,
563         .dir    = NF_CT_DEFAULT_ZONE_DIR,
564 };
565 EXPORT_SYMBOL_GPL(nf_ct_zone_dflt);
566 #endif /* CONFIG_NF_CONNTRACK */
567
568 #ifdef CONFIG_NF_NAT_NEEDED
569 void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *);
570 EXPORT_SYMBOL(nf_nat_decode_session_hook);
571 #endif
572
573 static int __net_init netfilter_net_init(struct net *net)
574 {
575         int i, h;
576
577         for (i = 0; i < ARRAY_SIZE(net->nf.hooks); i++) {
578                 for (h = 0; h < NF_MAX_HOOKS; h++)
579                         RCU_INIT_POINTER(net->nf.hooks[i][h], NULL);
580         }
581
582 #ifdef CONFIG_PROC_FS
583         net->nf.proc_netfilter = proc_net_mkdir(net, "netfilter",
584                                                 net->proc_net);
585         if (!net->nf.proc_netfilter) {
586                 if (!net_eq(net, &init_net))
587                         pr_err("cannot create netfilter proc entry");
588
589                 return -ENOMEM;
590         }
591 #endif
592
593         return 0;
594 }
595
596 static void __net_exit netfilter_net_exit(struct net *net)
597 {
598         remove_proc_entry("netfilter", net->proc_net);
599 }
600
601 static struct pernet_operations netfilter_net_ops = {
602         .init = netfilter_net_init,
603         .exit = netfilter_net_exit,
604 };
605
606 int __init netfilter_init(void)
607 {
608         int ret;
609
610         ret = register_pernet_subsys(&netfilter_net_ops);
611         if (ret < 0)
612                 goto err;
613
614         ret = netfilter_log_init();
615         if (ret < 0)
616                 goto err_pernet;
617
618         return 0;
619 err_pernet:
620         unregister_pernet_subsys(&netfilter_net_ops);
621 err:
622         return ret;
623 }