nf_conntrack_helper.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* Helper handling for netfilter. */
  2. /* (C) 1999-2001 Paul `Rusty' Russell
  3. * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
  4. * (C) 2003,2004 USAGI/WIDE Project <http://www.linux-ipv6.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/types.h>
  11. #include <linux/netfilter.h>
  12. #include <linux/module.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/vmalloc.h>
  15. #include <linux/stddef.h>
  16. #include <linux/slab.h>
  17. #include <linux/random.h>
  18. #include <linux/err.h>
  19. #include <linux/kernel.h>
  20. #include <linux/netdevice.h>
  21. #include <net/netfilter/nf_conntrack.h>
  22. #include <net/netfilter/nf_conntrack_l3proto.h>
  23. #include <net/netfilter/nf_conntrack_l4proto.h>
  24. #include <net/netfilter/nf_conntrack_helper.h>
  25. #include <net/netfilter/nf_conntrack_core.h>
  26. static __read_mostly LIST_HEAD(helpers);
  27. struct nf_conntrack_helper *
  28. __nf_ct_helper_find(const struct nf_conntrack_tuple *tuple)
  29. {
  30. struct nf_conntrack_helper *h;
  31. list_for_each_entry(h, &helpers, list) {
  32. if (nf_ct_tuple_mask_cmp(tuple, &h->tuple, &h->mask))
  33. return h;
  34. }
  35. return NULL;
  36. }
  37. struct nf_conntrack_helper *
  38. nf_ct_helper_find_get( const struct nf_conntrack_tuple *tuple)
  39. {
  40. struct nf_conntrack_helper *helper;
  41. /* need nf_conntrack_lock to assure that helper exists until
  42. * try_module_get() is called */
  43. read_lock_bh(&nf_conntrack_lock);
  44. helper = __nf_ct_helper_find(tuple);
  45. if (helper) {
  46. /* need to increase module usage count to assure helper will
  47. * not go away while the caller is e.g. busy putting a
  48. * conntrack in the hash that uses the helper */
  49. if (!try_module_get(helper->me))
  50. helper = NULL;
  51. }
  52. read_unlock_bh(&nf_conntrack_lock);
  53. return helper;
  54. }
  55. EXPORT_SYMBOL_GPL(nf_ct_helper_find_get);
  56. void nf_ct_helper_put(struct nf_conntrack_helper *helper)
  57. {
  58. module_put(helper->me);
  59. }
  60. EXPORT_SYMBOL_GPL(nf_ct_helper_put);
  61. struct nf_conntrack_helper *
  62. __nf_conntrack_helper_find_byname(const char *name)
  63. {
  64. struct nf_conntrack_helper *h;
  65. list_for_each_entry(h, &helpers, list) {
  66. if (!strcmp(h->name, name))
  67. return h;
  68. }
  69. return NULL;
  70. }
  71. EXPORT_SYMBOL_GPL(__nf_conntrack_helper_find_byname);
  72. static inline int unhelp(struct nf_conntrack_tuple_hash *i,
  73. const struct nf_conntrack_helper *me)
  74. {
  75. struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(i);
  76. struct nf_conn_help *help = nfct_help(ct);
  77. if (help && help->helper == me) {
  78. nf_conntrack_event(IPCT_HELPER, ct);
  79. help->helper = NULL;
  80. }
  81. return 0;
  82. }
  83. int nf_conntrack_helper_register(struct nf_conntrack_helper *me)
  84. {
  85. int size, ret;
  86. BUG_ON(me->timeout == 0);
  87. size = ALIGN(sizeof(struct nf_conn), __alignof__(struct nf_conn_help)) +
  88. sizeof(struct nf_conn_help);
  89. ret = nf_conntrack_register_cache(NF_CT_F_HELP, "nf_conntrack:help",
  90. size);
  91. if (ret < 0) {
  92. printk(KERN_ERR "nf_conntrack_helper_register: Unable to create slab cache for conntracks\n");
  93. return ret;
  94. }
  95. write_lock_bh(&nf_conntrack_lock);
  96. list_add(&me->list, &helpers);
  97. write_unlock_bh(&nf_conntrack_lock);
  98. return 0;
  99. }
  100. EXPORT_SYMBOL_GPL(nf_conntrack_helper_register);
  101. void nf_conntrack_helper_unregister(struct nf_conntrack_helper *me)
  102. {
  103. unsigned int i;
  104. struct nf_conntrack_tuple_hash *h;
  105. struct nf_conntrack_expect *exp, *tmp;
  106. /* Need write lock here, to delete helper. */
  107. write_lock_bh(&nf_conntrack_lock);
  108. list_del(&me->list);
  109. /* Get rid of expectations */
  110. list_for_each_entry_safe(exp, tmp, &nf_conntrack_expect_list, list) {
  111. struct nf_conn_help *help = nfct_help(exp->master);
  112. if ((help->helper == me || exp->helper == me) &&
  113. del_timer(&exp->timeout)) {
  114. nf_ct_unlink_expect(exp);
  115. nf_conntrack_expect_put(exp);
  116. }
  117. }
  118. /* Get rid of expecteds, set helpers to NULL. */
  119. list_for_each_entry(h, &unconfirmed, list)
  120. unhelp(h, me);
  121. for (i = 0; i < nf_conntrack_htable_size; i++) {
  122. list_for_each_entry(h, &nf_conntrack_hash[i], list)
  123. unhelp(h, me);
  124. }
  125. write_unlock_bh(&nf_conntrack_lock);
  126. /* Someone could be still looking at the helper in a bh. */
  127. synchronize_net();
  128. }
  129. EXPORT_SYMBOL_GPL(nf_conntrack_helper_unregister);