]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/netfilter/nf_conntrack_extend.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 / nf_conntrack_extend.c
CommitLineData
ecfab2c9
YK
1/* Structure dynamic extension infrastructure
2 * Copyright (C) 2004 Rusty Russell IBM Corporation
3 * Copyright (C) 2007 Netfilter Core Team <coreteam@netfilter.org>
4 * Copyright (C) 2007 USAGI/WIDE Project <http://www.linux-ipv6.org>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/mutex.h>
14#include <linux/rcupdate.h>
15#include <linux/slab.h>
16#include <linux/skbuff.h>
17#include <net/netfilter/nf_conntrack_extend.h>
18
0906a372 19static struct nf_ct_ext_type __rcu *nf_ct_ext_types[NF_CT_EXT_NUM];
ecfab2c9 20static DEFINE_MUTEX(nf_ct_ext_type_mutex);
54044b1f 21#define NF_CT_EXT_PREALLOC 128u /* conntrack events are on by default */
ecfab2c9 22
8eeef235 23void nf_ct_ext_destroy(struct nf_conn *ct)
ecfab2c9
YK
24{
25 unsigned int i;
26 struct nf_ct_ext_type *t;
27
28 for (i = 0; i < NF_CT_EXT_NUM; i++) {
ecfab2c9
YK
29 rcu_read_lock();
30 t = rcu_dereference(nf_ct_ext_types[i]);
31
32 /* Here the nf_ct_ext_type might have been unregisterd.
33 * I.e., it has responsible to cleanup private
34 * area in all conntracks when it is unregisterd.
35 */
36 if (t && t->destroy)
37 t->destroy(ct);
38 rcu_read_unlock();
39 }
40}
8eeef235 41EXPORT_SYMBOL(nf_ct_ext_destroy);
ecfab2c9 42
faec865d 43void *nf_ct_ext_add(struct nf_conn *ct, enum nf_ct_ext_id id, gfp_t gfp)
ecfab2c9 44{
22d4536d 45 unsigned int newlen, newoff, oldlen, alloc;
ee92d378 46 struct nf_ct_ext *old, *new;
ecfab2c9
YK
47 struct nf_ct_ext_type *t;
48
55871d04
PM
49 /* Conntrack must not be confirmed to avoid races on reallocation. */
50 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
51
ee92d378 52 old = ct->ext;
ecfab2c9 53
22d4536d
FW
54 if (old) {
55 if (__nf_ct_ext_exist(old, id))
56 return NULL;
57 oldlen = old->len;
58 } else {
59 oldlen = sizeof(*new);
60 }
ecfab2c9
YK
61
62 rcu_read_lock();
63 t = rcu_dereference(nf_ct_ext_types[id]);
9c3f3794
LZ
64 if (!t) {
65 rcu_read_unlock();
66 return NULL;
67 }
ecfab2c9 68
22d4536d 69 newoff = ALIGN(oldlen, t->align);
faec865d 70 newlen = newoff + t->len;
ecfab2c9
YK
71 rcu_read_unlock();
72
22d4536d
FW
73 alloc = max(newlen, NF_CT_EXT_PREALLOC);
74 new = __krealloc(old, alloc, gfp);
31d8519c
PE
75 if (!new)
76 return NULL;
ecfab2c9 77
22d4536d
FW
78 if (!old) {
79 memset(new->offset, 0, sizeof(new->offset));
80 ct->ext = new;
81 } else if (new != old) {
1f8d36a1 82 kfree_rcu(old, rcu);
7c966435 83 rcu_assign_pointer(ct->ext, new);
ecfab2c9
YK
84 }
85
6c64825b
PM
86 new->offset[id] = newoff;
87 new->len = newlen;
88 memset((void *)new + newoff, 0, newlen - newoff);
89 return (void *)new + newoff;
ecfab2c9 90}
faec865d 91EXPORT_SYMBOL(nf_ct_ext_add);
ecfab2c9 92
ecfab2c9 93/* This MUST be called in process context. */
23f671a1 94int nf_ct_extend_register(const struct nf_ct_ext_type *type)
ecfab2c9
YK
95{
96 int ret = 0;
97
98 mutex_lock(&nf_ct_ext_type_mutex);
99 if (nf_ct_ext_types[type->id]) {
100 ret = -EBUSY;
101 goto out;
102 }
103
cf778b00 104 rcu_assign_pointer(nf_ct_ext_types[type->id], type);
ecfab2c9
YK
105out:
106 mutex_unlock(&nf_ct_ext_type_mutex);
107 return ret;
108}
109EXPORT_SYMBOL_GPL(nf_ct_extend_register);
110
111/* This MUST be called in process context. */
23f671a1 112void nf_ct_extend_unregister(const struct nf_ct_ext_type *type)
ecfab2c9
YK
113{
114 mutex_lock(&nf_ct_ext_type_mutex);
a9b3cd7f 115 RCU_INIT_POINTER(nf_ct_ext_types[type->id], NULL);
ecfab2c9 116 mutex_unlock(&nf_ct_ext_type_mutex);
9c3f3794 117 synchronize_rcu();
ecfab2c9
YK
118}
119EXPORT_SYMBOL_GPL(nf_ct_extend_unregister);