xt_length.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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("Xtables: Packet length (Layer3,4,5) match");
  16. MODULE_LICENSE("GPL");
  17. MODULE_ALIAS("ipt_length");
  18. MODULE_ALIAS("ip6t_length");
  19. static bool
  20. length_mt(const struct sk_buff *skb, struct xt_action_param *par)
  21. {
  22. const struct xt_length_info *info = par->matchinfo;
  23. u_int16_t pktlen = ntohs(ip_hdr(skb)->tot_len);
  24. return (pktlen >= info->min && pktlen <= info->max) ^ info->invert;
  25. }
  26. static bool
  27. length_mt6(const struct sk_buff *skb, struct xt_action_param *par)
  28. {
  29. const struct xt_length_info *info = par->matchinfo;
  30. const u_int16_t pktlen = ntohs(ipv6_hdr(skb)->payload_len) +
  31. sizeof(struct ipv6hdr);
  32. return (pktlen >= info->min && pktlen <= info->max) ^ info->invert;
  33. }
  34. static struct xt_match length_mt_reg[] __read_mostly = {
  35. {
  36. .name = "length",
  37. .family = NFPROTO_IPV4,
  38. .match = length_mt,
  39. .matchsize = sizeof(struct xt_length_info),
  40. .me = THIS_MODULE,
  41. },
  42. {
  43. .name = "length",
  44. .family = NFPROTO_IPV6,
  45. .match = length_mt6,
  46. .matchsize = sizeof(struct xt_length_info),
  47. .me = THIS_MODULE,
  48. },
  49. };
  50. static int __init length_mt_init(void)
  51. {
  52. return xt_register_matches(length_mt_reg, ARRAY_SIZE(length_mt_reg));
  53. }
  54. static void __exit length_mt_exit(void)
  55. {
  56. xt_unregister_matches(length_mt_reg, ARRAY_SIZE(length_mt_reg));
  57. }
  58. module_init(length_mt_init);
  59. module_exit(length_mt_exit);