nf_conntrack_extend.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. static struct nf_ct_ext_type __rcu *nf_ct_ext_types[NF_CT_EXT_NUM];
  16. static DEFINE_MUTEX(nf_ct_ext_type_mutex);
  17. #define NF_CT_EXT_PREALLOC 128u /* conntrack events are on by default */
  18. void nf_ct_ext_destroy(struct nf_conn *ct)
  19. {
  20. unsigned int i;
  21. struct nf_ct_ext_type *t;
  22. for (i = 0; i < NF_CT_EXT_NUM; i++) {
  23. rcu_read_lock();
  24. t = rcu_dereference(nf_ct_ext_types[i]);
  25. /* Here the nf_ct_ext_type might have been unregisterd.
  26. * I.e., it has responsible to cleanup private
  27. * area in all conntracks when it is unregisterd.
  28. */
  29. if (t && t->destroy)
  30. t->destroy(ct);
  31. rcu_read_unlock();
  32. }
  33. kfree(ct->ext);
  34. }
  35. void *nf_ct_ext_add(struct nf_conn *ct, enum nf_ct_ext_id id, gfp_t gfp)
  36. {
  37. unsigned int newlen, newoff, oldlen, alloc;
  38. struct nf_ct_ext_type *t;
  39. struct nf_ct_ext *new;
  40. /* Conntrack must not be confirmed to avoid races on reallocation. */
  41. WARN_ON(nf_ct_is_confirmed(ct));
  42. if (ct->ext) {
  43. const struct nf_ct_ext *old = ct->ext;
  44. if (__nf_ct_ext_exist(old, id))
  45. return NULL;
  46. oldlen = old->len;
  47. } else {
  48. oldlen = sizeof(*new);
  49. }
  50. rcu_read_lock();
  51. t = rcu_dereference(nf_ct_ext_types[id]);
  52. if (!t) {
  53. rcu_read_unlock();
  54. return NULL;
  55. }
  56. newoff = ALIGN(oldlen, t->align);
  57. newlen = newoff + t->len;
  58. rcu_read_unlock();
  59. alloc = max(newlen, NF_CT_EXT_PREALLOC);
  60. new = krealloc(ct->ext, alloc, gfp);
  61. if (!new)
  62. return NULL;
  63. if (!ct->ext)
  64. memset(new->offset, 0, sizeof(new->offset));
  65. new->offset[id] = newoff;
  66. new->len = newlen;
  67. memset((void *)new + newoff, 0, newlen - newoff);
  68. ct->ext = new;
  69. return (void *)new + newoff;
  70. }
  71. EXPORT_SYMBOL(nf_ct_ext_add);
  72. /* This MUST be called in process context. */
  73. int nf_ct_extend_register(const struct nf_ct_ext_type *type)
  74. {
  75. int ret = 0;
  76. mutex_lock(&nf_ct_ext_type_mutex);
  77. if (nf_ct_ext_types[type->id]) {
  78. ret = -EBUSY;
  79. goto out;
  80. }
  81. rcu_assign_pointer(nf_ct_ext_types[type->id], type);
  82. out:
  83. mutex_unlock(&nf_ct_ext_type_mutex);
  84. return ret;
  85. }
  86. EXPORT_SYMBOL_GPL(nf_ct_extend_register);
  87. /* This MUST be called in process context. */
  88. void nf_ct_extend_unregister(const struct nf_ct_ext_type *type)
  89. {
  90. mutex_lock(&nf_ct_ext_type_mutex);
  91. RCU_INIT_POINTER(nf_ct_ext_types[type->id], NULL);
  92. mutex_unlock(&nf_ct_ext_type_mutex);
  93. synchronize_rcu();
  94. }
  95. EXPORT_SYMBOL_GPL(nf_ct_extend_unregister);