ipt_ah.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Kernel module to match AH parameters. */
  3. /* (C) 1999-2000 Yon Uriarte <yon@astaro.de>
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <linux/in.h>
  7. #include <linux/module.h>
  8. #include <linux/skbuff.h>
  9. #include <linux/ip.h>
  10. #include <linux/netfilter_ipv4/ipt_ah.h>
  11. #include <linux/netfilter/x_tables.h>
  12. MODULE_LICENSE("GPL");
  13. MODULE_AUTHOR("Yon Uriarte <yon@astaro.de>");
  14. MODULE_DESCRIPTION("Xtables: IPv4 IPsec-AH SPI match");
  15. /* Returns 1 if the spi is matched by the range, 0 otherwise */
  16. static inline bool
  17. spi_match(u_int32_t min, u_int32_t max, u_int32_t spi, bool invert)
  18. {
  19. bool r;
  20. pr_debug("spi_match:%c 0x%x <= 0x%x <= 0x%x\n",
  21. invert ? '!' : ' ', min, spi, max);
  22. r = (spi >= min && spi <= max) ^ invert;
  23. pr_debug(" result %s\n", r ? "PASS" : "FAILED");
  24. return r;
  25. }
  26. static bool ah_mt(const struct sk_buff *skb, struct xt_action_param *par)
  27. {
  28. struct ip_auth_hdr _ahdr;
  29. const struct ip_auth_hdr *ah;
  30. const struct ipt_ah *ahinfo = par->matchinfo;
  31. /* Must not be a fragment. */
  32. if (par->fragoff != 0)
  33. return false;
  34. ah = skb_header_pointer(skb, par->thoff, sizeof(_ahdr), &_ahdr);
  35. if (ah == NULL) {
  36. /* We've been asked to examine this packet, and we
  37. * can't. Hence, no choice but to drop.
  38. */
  39. pr_debug("Dropping evil AH tinygram.\n");
  40. par->hotdrop = true;
  41. return false;
  42. }
  43. return spi_match(ahinfo->spis[0], ahinfo->spis[1],
  44. ntohl(ah->spi),
  45. !!(ahinfo->invflags & IPT_AH_INV_SPI));
  46. }
  47. static int ah_mt_check(const struct xt_mtchk_param *par)
  48. {
  49. const struct ipt_ah *ahinfo = par->matchinfo;
  50. /* Must specify no unknown invflags */
  51. if (ahinfo->invflags & ~IPT_AH_INV_MASK) {
  52. pr_debug("unknown flags %X\n", ahinfo->invflags);
  53. return -EINVAL;
  54. }
  55. return 0;
  56. }
  57. static struct xt_match ah_mt_reg __read_mostly = {
  58. .name = "ah",
  59. .family = NFPROTO_IPV4,
  60. .match = ah_mt,
  61. .matchsize = sizeof(struct ipt_ah),
  62. .proto = IPPROTO_AH,
  63. .checkentry = ah_mt_check,
  64. .me = THIS_MODULE,
  65. };
  66. static int __init ah_mt_init(void)
  67. {
  68. return xt_register_match(&ah_mt_reg);
  69. }
  70. static void __exit ah_mt_exit(void)
  71. {
  72. xt_unregister_match(&ah_mt_reg);
  73. }
  74. module_init(ah_mt_init);
  75. module_exit(ah_mt_exit);