xt_length.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Kernel module to match packet length. */
  3. /* (C) 1999-2001 James Morris <jmorros@intercode.com.au>
  4. */
  5. #include <linux/module.h>
  6. #include <linux/skbuff.h>
  7. #include <linux/ipv6.h>
  8. #include <net/ip.h>
  9. #include <linux/netfilter/xt_length.h>
  10. #include <linux/netfilter/x_tables.h>
  11. MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
  12. MODULE_DESCRIPTION("Xtables: Packet length (Layer3,4,5) match");
  13. MODULE_LICENSE("GPL");
  14. MODULE_ALIAS("ipt_length");
  15. MODULE_ALIAS("ip6t_length");
  16. static bool
  17. length_mt(const struct sk_buff *skb, struct xt_action_param *par)
  18. {
  19. const struct xt_length_info *info = par->matchinfo;
  20. u_int16_t pktlen = ntohs(ip_hdr(skb)->tot_len);
  21. return (pktlen >= info->min && pktlen <= info->max) ^ info->invert;
  22. }
  23. static bool
  24. length_mt6(const struct sk_buff *skb, struct xt_action_param *par)
  25. {
  26. const struct xt_length_info *info = par->matchinfo;
  27. const u_int16_t pktlen = ntohs(ipv6_hdr(skb)->payload_len) +
  28. sizeof(struct ipv6hdr);
  29. return (pktlen >= info->min && pktlen <= info->max) ^ info->invert;
  30. }
  31. static struct xt_match length_mt_reg[] __read_mostly = {
  32. {
  33. .name = "length",
  34. .family = NFPROTO_IPV4,
  35. .match = length_mt,
  36. .matchsize = sizeof(struct xt_length_info),
  37. .me = THIS_MODULE,
  38. },
  39. {
  40. .name = "length",
  41. .family = NFPROTO_IPV6,
  42. .match = length_mt6,
  43. .matchsize = sizeof(struct xt_length_info),
  44. .me = THIS_MODULE,
  45. },
  46. };
  47. static int __init length_mt_init(void)
  48. {
  49. return xt_register_matches(length_mt_reg, ARRAY_SIZE(length_mt_reg));
  50. }
  51. static void __exit length_mt_exit(void)
  52. {
  53. xt_unregister_matches(length_mt_reg, ARRAY_SIZE(length_mt_reg));
  54. }
  55. module_init(length_mt_init);
  56. module_exit(length_mt_exit);