linux/net/netfilter/nf_conntrack_extend.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/* Structure dynamic extension infrastructure
   3 * Copyright (C) 2004 Rusty Russell IBM Corporation
   4 * Copyright (C) 2007 Netfilter Core Team <coreteam@netfilter.org>
   5 * Copyright (C) 2007 USAGI/WIDE Project <http://www.linux-ipv6.org>
   6 */
   7#include <linux/kernel.h>
   8#include <linux/kmemleak.h>
   9#include <linux/module.h>
  10#include <linux/mutex.h>
  11#include <linux/rcupdate.h>
  12#include <linux/slab.h>
  13#include <linux/skbuff.h>
  14#include <net/netfilter/nf_conntrack_extend.h>
  15
  16static struct nf_ct_ext_type __rcu *nf_ct_ext_types[NF_CT_EXT_NUM];
  17static DEFINE_MUTEX(nf_ct_ext_type_mutex);
  18#define NF_CT_EXT_PREALLOC      128u /* conntrack events are on by default */
  19
  20void nf_ct_ext_destroy(struct nf_conn *ct)
  21{
  22        unsigned int i;
  23        struct nf_ct_ext_type *t;
  24
  25        for (i = 0; i < NF_CT_EXT_NUM; i++) {
  26                rcu_read_lock();
  27                t = rcu_dereference(nf_ct_ext_types[i]);
  28
  29                /* Here the nf_ct_ext_type might have been unregisterd.
  30                 * I.e., it has responsible to cleanup private
  31                 * area in all conntracks when it is unregisterd.
  32                 */
  33                if (t && t->destroy)
  34                        t->destroy(ct);
  35                rcu_read_unlock();
  36        }
  37
  38        kfree(ct->ext);
  39}
  40
  41void *nf_ct_ext_add(struct nf_conn *ct, enum nf_ct_ext_id id, gfp_t gfp)
  42{
  43        unsigned int newlen, newoff, oldlen, alloc;
  44        struct nf_ct_ext_type *t;
  45        struct nf_ct_ext *new;
  46
  47        /* Conntrack must not be confirmed to avoid races on reallocation. */
  48        WARN_ON(nf_ct_is_confirmed(ct));
  49
  50
  51        if (ct->ext) {
  52                const struct nf_ct_ext *old = ct->ext;
  53
  54                if (__nf_ct_ext_exist(old, id))
  55                        return NULL;
  56                oldlen = old->len;
  57        } else {
  58                oldlen = sizeof(*new);
  59        }
  60
  61        rcu_read_lock();
  62        t = rcu_dereference(nf_ct_ext_types[id]);
  63        if (!t) {
  64                rcu_read_unlock();
  65                return NULL;
  66        }
  67
  68        newoff = ALIGN(oldlen, t->align);
  69        newlen = newoff + t->len;
  70        rcu_read_unlock();
  71
  72        alloc = max(newlen, NF_CT_EXT_PREALLOC);
  73        new = krealloc(ct->ext, alloc, gfp);
  74        if (!new)
  75                return NULL;
  76
  77        if (!ct->ext)
  78                memset(new->offset, 0, sizeof(new->offset));
  79
  80        new->offset[id] = newoff;
  81        new->len = newlen;
  82        memset((void *)new + newoff, 0, newlen - newoff);
  83
  84        ct->ext = new;
  85        return (void *)new + newoff;
  86}
  87EXPORT_SYMBOL(nf_ct_ext_add);
  88
  89/* This MUST be called in process context. */
  90int nf_ct_extend_register(const struct nf_ct_ext_type *type)
  91{
  92        int ret = 0;
  93
  94        mutex_lock(&nf_ct_ext_type_mutex);
  95        if (nf_ct_ext_types[type->id]) {
  96                ret = -EBUSY;
  97                goto out;
  98        }
  99
 100        rcu_assign_pointer(nf_ct_ext_types[type->id], type);
 101out:
 102        mutex_unlock(&nf_ct_ext_type_mutex);
 103        return ret;
 104}
 105EXPORT_SYMBOL_GPL(nf_ct_extend_register);
 106
 107/* This MUST be called in process context. */
 108void nf_ct_extend_unregister(const struct nf_ct_ext_type *type)
 109{
 110        mutex_lock(&nf_ct_ext_type_mutex);
 111        RCU_INIT_POINTER(nf_ct_ext_types[type->id], NULL);
 112        mutex_unlock(&nf_ct_ext_type_mutex);
 113        synchronize_rcu();
 114}
 115EXPORT_SYMBOL_GPL(nf_ct_extend_unregister);
 116