xt_length.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* Kernel module to match packet length. */
  2. /* (C) 1999-2001 James Morris <jmorros@intercode.com.au>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/module.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/ipv6.h>
  11. #include <net/ip.h>
  12. #include <linux/netfilter/xt_length.h>
  13. #include <linux/netfilter/x_tables.h>
  14. MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
  15. MODULE_DESCRIPTION("IP tables packet length matching module");
  16. MODULE_LICENSE("GPL");
  17. MODULE_ALIAS("ipt_length");
  18. MODULE_ALIAS("ip6t_length");
  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_length_info *info = matchinfo;
  30. u_int16_t pktlen = ntohs(skb->nh.iph->tot_len);
  31. return (pktlen >= info->min && pktlen <= info->max) ^ info->invert;
  32. }
  33. static int
  34. match6(const struct sk_buff *skb,
  35. const struct net_device *in,
  36. const struct net_device *out,
  37. const struct xt_match *match,
  38. const void *matchinfo,
  39. int offset,
  40. unsigned int protoff,
  41. int *hotdrop)
  42. {
  43. const struct xt_length_info *info = matchinfo;
  44. u_int16_t pktlen = ntohs(skb->nh.ipv6h->payload_len) + sizeof(struct ipv6hdr);
  45. return (pktlen >= info->min && pktlen <= info->max) ^ info->invert;
  46. }
  47. static struct xt_match xt_length_match[] = {
  48. {
  49. .name = "length",
  50. .family = AF_INET,
  51. .match = match,
  52. .matchsize = sizeof(struct xt_length_info),
  53. .me = THIS_MODULE,
  54. },
  55. {
  56. .name = "length",
  57. .family = AF_INET6,
  58. .match = match6,
  59. .matchsize = sizeof(struct xt_length_info),
  60. .me = THIS_MODULE,
  61. },
  62. };
  63. static int __init xt_length_init(void)
  64. {
  65. return xt_register_matches(xt_length_match,
  66. ARRAY_SIZE(xt_length_match));
  67. }
  68. static void __exit xt_length_fini(void)
  69. {
  70. xt_unregister_matches(xt_length_match, ARRAY_SIZE(xt_length_match));
  71. }
  72. module_init(xt_length_init);
  73. module_exit(xt_length_fini);