ip6t_ah.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* Kernel module to match AH parameters. */
  2. /* (C) 2001-2002 Andras Kis-Szabo <kisza@sch.bme.hu>
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/module.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/ip.h>
  12. #include <linux/ipv6.h>
  13. #include <linux/types.h>
  14. #include <net/checksum.h>
  15. #include <net/ipv6.h>
  16. #include <linux/netfilter/x_tables.h>
  17. #include <linux/netfilter_ipv6/ip6_tables.h>
  18. #include <linux/netfilter_ipv6/ip6t_ah.h>
  19. MODULE_LICENSE("GPL");
  20. MODULE_DESCRIPTION("Xtables: IPv6 IPsec-AH match");
  21. MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>");
  22. /* Returns 1 if the spi is matched by the range, 0 otherwise */
  23. static inline bool
  24. spi_match(u_int32_t min, u_int32_t max, u_int32_t spi, bool invert)
  25. {
  26. bool r;
  27. pr_debug("spi_match:%c 0x%x <= 0x%x <= 0x%x\n",
  28. invert ? '!' : ' ', min, spi, max);
  29. r = (spi >= min && spi <= max) ^ invert;
  30. pr_debug(" result %s\n", r ? "PASS" : "FAILED");
  31. return r;
  32. }
  33. static bool ah_mt6(const struct sk_buff *skb, struct xt_action_param *par)
  34. {
  35. struct ip_auth_hdr _ah;
  36. const struct ip_auth_hdr *ah;
  37. const struct ip6t_ah *ahinfo = par->matchinfo;
  38. unsigned int ptr = 0;
  39. unsigned int hdrlen = 0;
  40. int err;
  41. err = ipv6_find_hdr(skb, &ptr, NEXTHDR_AUTH, NULL, NULL);
  42. if (err < 0) {
  43. if (err != -ENOENT)
  44. par->hotdrop = true;
  45. return false;
  46. }
  47. ah = skb_header_pointer(skb, ptr, sizeof(_ah), &_ah);
  48. if (ah == NULL) {
  49. par->hotdrop = true;
  50. return false;
  51. }
  52. hdrlen = (ah->hdrlen + 2) << 2;
  53. pr_debug("IPv6 AH LEN %u %u ", hdrlen, ah->hdrlen);
  54. pr_debug("RES %04X ", ah->reserved);
  55. pr_debug("SPI %u %08X\n", ntohl(ah->spi), ntohl(ah->spi));
  56. pr_debug("IPv6 AH spi %02X ",
  57. spi_match(ahinfo->spis[0], ahinfo->spis[1],
  58. ntohl(ah->spi),
  59. !!(ahinfo->invflags & IP6T_AH_INV_SPI)));
  60. pr_debug("len %02X %04X %02X ",
  61. ahinfo->hdrlen, hdrlen,
  62. (!ahinfo->hdrlen ||
  63. (ahinfo->hdrlen == hdrlen) ^
  64. !!(ahinfo->invflags & IP6T_AH_INV_LEN)));
  65. pr_debug("res %02X %04X %02X\n",
  66. ahinfo->hdrres, ah->reserved,
  67. !(ahinfo->hdrres && ah->reserved));
  68. return (ah != NULL) &&
  69. spi_match(ahinfo->spis[0], ahinfo->spis[1],
  70. ntohl(ah->spi),
  71. !!(ahinfo->invflags & IP6T_AH_INV_SPI)) &&
  72. (!ahinfo->hdrlen ||
  73. (ahinfo->hdrlen == hdrlen) ^
  74. !!(ahinfo->invflags & IP6T_AH_INV_LEN)) &&
  75. !(ahinfo->hdrres && ah->reserved);
  76. }
  77. static int ah_mt6_check(const struct xt_mtchk_param *par)
  78. {
  79. const struct ip6t_ah *ahinfo = par->matchinfo;
  80. if (ahinfo->invflags & ~IP6T_AH_INV_MASK) {
  81. pr_debug("unknown flags %X\n", ahinfo->invflags);
  82. return -EINVAL;
  83. }
  84. return 0;
  85. }
  86. static struct xt_match ah_mt6_reg __read_mostly = {
  87. .name = "ah",
  88. .family = NFPROTO_IPV6,
  89. .match = ah_mt6,
  90. .matchsize = sizeof(struct ip6t_ah),
  91. .checkentry = ah_mt6_check,
  92. .me = THIS_MODULE,
  93. };
  94. static int __init ah_mt6_init(void)
  95. {
  96. return xt_register_match(&ah_mt6_reg);
  97. }
  98. static void __exit ah_mt6_exit(void)
  99. {
  100. xt_unregister_match(&ah_mt6_reg);
  101. }
  102. module_init(ah_mt6_init);
  103. module_exit(ah_mt6_exit);