/net/netfilter/xt_state.c

http://github.com/mirrors/linux · C · 75 lines · 57 code · 13 blank · 5 comment · 5 complexity · 268751a1586936b277e908ac59048412 MD5 · raw file

  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Kernel module to match connection tracking information. */
  3. /* (C) 1999-2001 Paul `Rusty' Russell
  4. * (C) 2002-2005 Netfilter Core Team <coreteam@netfilter.org>
  5. */
  6. #include <linux/module.h>
  7. #include <linux/skbuff.h>
  8. #include <net/netfilter/nf_conntrack.h>
  9. #include <linux/netfilter/x_tables.h>
  10. #include <linux/netfilter/xt_state.h>
  11. MODULE_LICENSE("GPL");
  12. MODULE_AUTHOR("Rusty Russell <rusty@rustcorp.com.au>");
  13. MODULE_DESCRIPTION("ip[6]_tables connection tracking state match module");
  14. MODULE_ALIAS("ipt_state");
  15. MODULE_ALIAS("ip6t_state");
  16. static bool
  17. state_mt(const struct sk_buff *skb, struct xt_action_param *par)
  18. {
  19. const struct xt_state_info *sinfo = par->matchinfo;
  20. enum ip_conntrack_info ctinfo;
  21. unsigned int statebit;
  22. struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
  23. if (ct)
  24. statebit = XT_STATE_BIT(ctinfo);
  25. else if (ctinfo == IP_CT_UNTRACKED)
  26. statebit = XT_STATE_UNTRACKED;
  27. else
  28. statebit = XT_STATE_INVALID;
  29. return (sinfo->statemask & statebit);
  30. }
  31. static int state_mt_check(const struct xt_mtchk_param *par)
  32. {
  33. int ret;
  34. ret = nf_ct_netns_get(par->net, par->family);
  35. if (ret < 0)
  36. pr_info_ratelimited("cannot load conntrack support for proto=%u\n",
  37. par->family);
  38. return ret;
  39. }
  40. static void state_mt_destroy(const struct xt_mtdtor_param *par)
  41. {
  42. nf_ct_netns_put(par->net, par->family);
  43. }
  44. static struct xt_match state_mt_reg __read_mostly = {
  45. .name = "state",
  46. .family = NFPROTO_UNSPEC,
  47. .checkentry = state_mt_check,
  48. .match = state_mt,
  49. .destroy = state_mt_destroy,
  50. .matchsize = sizeof(struct xt_state_info),
  51. .me = THIS_MODULE,
  52. };
  53. static int __init state_mt_init(void)
  54. {
  55. return xt_register_match(&state_mt_reg);
  56. }
  57. static void __exit state_mt_exit(void)
  58. {
  59. xt_unregister_match(&state_mt_reg);
  60. }
  61. module_init(state_mt_init);
  62. module_exit(state_mt_exit);