Merge branch 'work.misc' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[linux-2.6-microblaze.git] / net / netfilter / nf_conntrack_extend.c
index 008299b..6c605e8 100644 (file)
 
 static struct nf_ct_ext_type __rcu *nf_ct_ext_types[NF_CT_EXT_NUM];
 static DEFINE_MUTEX(nf_ct_ext_type_mutex);
+#define NF_CT_EXT_PREALLOC     128u /* conntrack events are on by default */
 
-void __nf_ct_ext_destroy(struct nf_conn *ct)
+void nf_ct_ext_destroy(struct nf_conn *ct)
 {
        unsigned int i;
        struct nf_ct_ext_type *t;
-       struct nf_ct_ext *ext = ct->ext;
 
        for (i = 0; i < NF_CT_EXT_NUM; i++) {
-               if (!__nf_ct_ext_exist(ext, i))
-                       continue;
-
                rcu_read_lock();
                t = rcu_dereference(nf_ct_ext_types[i]);
 
@@ -41,54 +38,26 @@ void __nf_ct_ext_destroy(struct nf_conn *ct)
                rcu_read_unlock();
        }
 }
-EXPORT_SYMBOL(__nf_ct_ext_destroy);
-
-static void *
-nf_ct_ext_create(struct nf_ct_ext **ext, enum nf_ct_ext_id id,
-                size_t var_alloc_len, gfp_t gfp)
-{
-       unsigned int off, len;
-       struct nf_ct_ext_type *t;
-       size_t alloc_size;
-
-       rcu_read_lock();
-       t = rcu_dereference(nf_ct_ext_types[id]);
-       if (!t) {
-               rcu_read_unlock();
-               return NULL;
-       }
-
-       off = ALIGN(sizeof(struct nf_ct_ext), t->align);
-       len = off + t->len + var_alloc_len;
-       alloc_size = t->alloc_size + var_alloc_len;
-       rcu_read_unlock();
-
-       *ext = kzalloc(alloc_size, gfp);
-       if (!*ext)
-               return NULL;
-
-       (*ext)->offset[id] = off;
-       (*ext)->len = len;
-
-       return (void *)(*ext) + off;
-}
+EXPORT_SYMBOL(nf_ct_ext_destroy);
 
-void *__nf_ct_ext_add_length(struct nf_conn *ct, enum nf_ct_ext_id id,
-                            size_t var_alloc_len, gfp_t gfp)
+void *nf_ct_ext_add(struct nf_conn *ct, enum nf_ct_ext_id id, gfp_t gfp)
 {
+       unsigned int newlen, newoff, oldlen, alloc;
        struct nf_ct_ext *old, *new;
-       int newlen, newoff;
        struct nf_ct_ext_type *t;
 
        /* Conntrack must not be confirmed to avoid races on reallocation. */
        NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
 
        old = ct->ext;
-       if (!old)
-               return nf_ct_ext_create(&ct->ext, id, var_alloc_len, gfp);
 
-       if (__nf_ct_ext_exist(old, id))
-               return NULL;
+       if (old) {
+               if (__nf_ct_ext_exist(old, id))
+                       return NULL;
+               oldlen = old->len;
+       } else {
+               oldlen = sizeof(*new);
+       }
 
        rcu_read_lock();
        t = rcu_dereference(nf_ct_ext_types[id]);
@@ -97,15 +66,19 @@ void *__nf_ct_ext_add_length(struct nf_conn *ct, enum nf_ct_ext_id id,
                return NULL;
        }
 
-       newoff = ALIGN(old->len, t->align);
-       newlen = newoff + t->len + var_alloc_len;
+       newoff = ALIGN(oldlen, t->align);
+       newlen = newoff + t->len;
        rcu_read_unlock();
 
-       new = __krealloc(old, newlen, gfp);
+       alloc = max(newlen, NF_CT_EXT_PREALLOC);
+       new = __krealloc(old, alloc, gfp);
        if (!new)
                return NULL;
 
-       if (new != old) {
+       if (!old) {
+               memset(new->offset, 0, sizeof(new->offset));
+               ct->ext = new;
+       } else if (new != old) {
                kfree_rcu(old, rcu);
                rcu_assign_pointer(ct->ext, new);
        }
@@ -115,45 +88,10 @@ void *__nf_ct_ext_add_length(struct nf_conn *ct, enum nf_ct_ext_id id,
        memset((void *)new + newoff, 0, newlen - newoff);
        return (void *)new + newoff;
 }
-EXPORT_SYMBOL(__nf_ct_ext_add_length);
-
-static void update_alloc_size(struct nf_ct_ext_type *type)
-{
-       int i, j;
-       struct nf_ct_ext_type *t1, *t2;
-       enum nf_ct_ext_id min = 0, max = NF_CT_EXT_NUM - 1;
-
-       /* unnecessary to update all types */
-       if ((type->flags & NF_CT_EXT_F_PREALLOC) == 0) {
-               min = type->id;
-               max = type->id;
-       }
-
-       /* This assumes that extended areas in conntrack for the types
-          whose NF_CT_EXT_F_PREALLOC bit set are allocated in order */
-       for (i = min; i <= max; i++) {
-               t1 = rcu_dereference_protected(nf_ct_ext_types[i],
-                               lockdep_is_held(&nf_ct_ext_type_mutex));
-               if (!t1)
-                       continue;
-
-               t1->alloc_size = ALIGN(sizeof(struct nf_ct_ext), t1->align) +
-                                t1->len;
-               for (j = 0; j < NF_CT_EXT_NUM; j++) {
-                       t2 = rcu_dereference_protected(nf_ct_ext_types[j],
-                               lockdep_is_held(&nf_ct_ext_type_mutex));
-                       if (t2 == NULL || t2 == t1 ||
-                           (t2->flags & NF_CT_EXT_F_PREALLOC) == 0)
-                               continue;
-
-                       t1->alloc_size = ALIGN(t1->alloc_size, t2->align)
-                                        + t2->len;
-               }
-       }
-}
+EXPORT_SYMBOL(nf_ct_ext_add);
 
 /* This MUST be called in process context. */
-int nf_ct_extend_register(struct nf_ct_ext_type *type)
+int nf_ct_extend_register(const struct nf_ct_ext_type *type)
 {
        int ret = 0;
 
@@ -163,12 +101,7 @@ int nf_ct_extend_register(struct nf_ct_ext_type *type)
                goto out;
        }
 
-       /* This ensures that nf_ct_ext_create() can allocate enough area
-          before updating alloc_size */
-       type->alloc_size = ALIGN(sizeof(struct nf_ct_ext), type->align)
-                          + type->len;
        rcu_assign_pointer(nf_ct_ext_types[type->id], type);
-       update_alloc_size(type);
 out:
        mutex_unlock(&nf_ct_ext_type_mutex);
        return ret;
@@ -176,11 +109,10 @@ out:
 EXPORT_SYMBOL_GPL(nf_ct_extend_register);
 
 /* This MUST be called in process context. */
-void nf_ct_extend_unregister(struct nf_ct_ext_type *type)
+void nf_ct_extend_unregister(const struct nf_ct_ext_type *type)
 {
        mutex_lock(&nf_ct_ext_type_mutex);
        RCU_INIT_POINTER(nf_ct_ext_types[type->id], NULL);
-       update_alloc_size(type);
        mutex_unlock(&nf_ct_ext_type_mutex);
        synchronize_rcu();
 }