xt_helper.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* iptables module to match on related connections */
  2. /*
  3. * (C) 2001 Martin Josefsson <gandalf@wlug.westbo.se>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/module.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/netfilter.h>
  13. #include <net/netfilter/nf_conntrack.h>
  14. #include <net/netfilter/nf_conntrack_core.h>
  15. #include <net/netfilter/nf_conntrack_helper.h>
  16. #include <linux/netfilter/x_tables.h>
  17. #include <linux/netfilter/xt_helper.h>
  18. MODULE_LICENSE("GPL");
  19. MODULE_AUTHOR("Martin Josefsson <gandalf@netfilter.org>");
  20. MODULE_DESCRIPTION("Xtables: Related connection matching");
  21. MODULE_ALIAS("ipt_helper");
  22. MODULE_ALIAS("ip6t_helper");
  23. static bool
  24. helper_mt(const struct sk_buff *skb, struct xt_action_param *par)
  25. {
  26. const struct xt_helper_info *info = par->matchinfo;
  27. const struct nf_conn *ct;
  28. const struct nf_conn_help *master_help;
  29. const struct nf_conntrack_helper *helper;
  30. enum ip_conntrack_info ctinfo;
  31. bool ret = info->invert;
  32. ct = nf_ct_get(skb, &ctinfo);
  33. if (!ct || !ct->master)
  34. return ret;
  35. master_help = nfct_help(ct->master);
  36. if (!master_help)
  37. return ret;
  38. /* rcu_read_lock()ed by nf_hook_slow */
  39. helper = rcu_dereference(master_help->helper);
  40. if (!helper)
  41. return ret;
  42. if (info->name[0] == '\0')
  43. ret = !ret;
  44. else
  45. ret ^= !strncmp(helper->name, info->name,
  46. strlen(helper->name));
  47. return ret;
  48. }
  49. static int helper_mt_check(const struct xt_mtchk_param *par)
  50. {
  51. struct xt_helper_info *info = par->matchinfo;
  52. int ret;
  53. ret = nf_ct_l3proto_try_module_get(par->family);
  54. if (ret < 0) {
  55. pr_info("cannot load conntrack support for proto=%u\n",
  56. par->family);
  57. return ret;
  58. }
  59. info->name[29] = '\0';
  60. return 0;
  61. }
  62. static void helper_mt_destroy(const struct xt_mtdtor_param *par)
  63. {
  64. nf_ct_l3proto_module_put(par->family);
  65. }
  66. static struct xt_match helper_mt_reg __read_mostly = {
  67. .name = "helper",
  68. .revision = 0,
  69. .family = NFPROTO_UNSPEC,
  70. .checkentry = helper_mt_check,
  71. .match = helper_mt,
  72. .destroy = helper_mt_destroy,
  73. .matchsize = sizeof(struct xt_helper_info),
  74. .me = THIS_MODULE,
  75. };
  76. static int __init helper_mt_init(void)
  77. {
  78. return xt_register_match(&helper_mt_reg);
  79. }
  80. static void __exit helper_mt_exit(void)
  81. {
  82. xt_unregister_match(&helper_mt_reg);
  83. }
  84. module_init(helper_mt_init);
  85. module_exit(helper_mt_exit);