xt_state.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* Kernel module to match connection tracking information. */
  2. /* (C) 1999-2001 Paul `Rusty' Russell
  3. * (C) 2002-2005 Netfilter Core Team <coreteam@netfilter.org>
  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. #include <linux/module.h>
  10. #include <linux/skbuff.h>
  11. #include <net/netfilter/nf_conntrack_compat.h>
  12. #include <linux/netfilter/x_tables.h>
  13. #include <linux/netfilter/xt_state.h>
  14. MODULE_LICENSE("GPL");
  15. MODULE_AUTHOR("Rusty Russell <rusty@rustcorp.com.au>");
  16. MODULE_DESCRIPTION("ip[6]_tables connection tracking state match module");
  17. MODULE_ALIAS("ipt_state");
  18. MODULE_ALIAS("ip6t_state");
  19. static int
  20. match(const struct sk_buff *skb,
  21. const struct net_device *in,
  22. const struct net_device *out,
  23. const struct xt_match *match,
  24. const void *matchinfo,
  25. int offset,
  26. unsigned int protoff,
  27. int *hotdrop)
  28. {
  29. const struct xt_state_info *sinfo = matchinfo;
  30. enum ip_conntrack_info ctinfo;
  31. unsigned int statebit;
  32. if (nf_ct_is_untracked(skb))
  33. statebit = XT_STATE_UNTRACKED;
  34. else if (!nf_ct_get_ctinfo(skb, &ctinfo))
  35. statebit = XT_STATE_INVALID;
  36. else
  37. statebit = XT_STATE_BIT(ctinfo);
  38. return (sinfo->statemask & statebit);
  39. }
  40. static int check(const char *tablename,
  41. const void *inf,
  42. const struct xt_match *match,
  43. void *matchinfo,
  44. unsigned int hook_mask)
  45. {
  46. if (nf_ct_l3proto_try_module_get(match->family) < 0) {
  47. printk(KERN_WARNING "can't load conntrack support for "
  48. "proto=%d\n", match->family);
  49. return 0;
  50. }
  51. return 1;
  52. }
  53. static void
  54. destroy(const struct xt_match *match, void *matchinfo)
  55. {
  56. nf_ct_l3proto_module_put(match->family);
  57. }
  58. static struct xt_match xt_state_match[] = {
  59. {
  60. .name = "state",
  61. .family = AF_INET,
  62. .checkentry = check,
  63. .match = match,
  64. .destroy = destroy,
  65. .matchsize = sizeof(struct xt_state_info),
  66. .me = THIS_MODULE,
  67. },
  68. {
  69. .name = "state",
  70. .family = AF_INET6,
  71. .checkentry = check,
  72. .match = match,
  73. .destroy = destroy,
  74. .matchsize = sizeof(struct xt_state_info),
  75. .me = THIS_MODULE,
  76. },
  77. };
  78. static int __init xt_state_init(void)
  79. {
  80. return xt_register_matches(xt_state_match, ARRAY_SIZE(xt_state_match));
  81. }
  82. static void __exit xt_state_fini(void)
  83. {
  84. xt_unregister_matches(xt_state_match, ARRAY_SIZE(xt_state_match));
  85. }
  86. module_init(xt_state_init);
  87. module_exit(xt_state_fini);