xt_esp.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Kernel module to match ESP parameters. */
  3. /* (C) 1999-2000 Yon Uriarte <yon@astaro.de>
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <linux/module.h>
  7. #include <linux/skbuff.h>
  8. #include <linux/in.h>
  9. #include <linux/ip.h>
  10. #include <linux/netfilter/xt_esp.h>
  11. #include <linux/netfilter/x_tables.h>
  12. #include <linux/netfilter_ipv4/ip_tables.h>
  13. #include <linux/netfilter_ipv6/ip6_tables.h>
  14. MODULE_LICENSE("GPL");
  15. MODULE_AUTHOR("Yon Uriarte <yon@astaro.de>");
  16. MODULE_DESCRIPTION("Xtables: IPsec-ESP packet match");
  17. MODULE_ALIAS("ipt_esp");
  18. MODULE_ALIAS("ip6t_esp");
  19. /* Returns 1 if the spi is matched by the range, 0 otherwise */
  20. static inline bool
  21. spi_match(u_int32_t min, u_int32_t max, u_int32_t spi, bool invert)
  22. {
  23. bool r;
  24. pr_debug("spi_match:%c 0x%x <= 0x%x <= 0x%x\n",
  25. invert ? '!' : ' ', min, spi, max);
  26. r = (spi >= min && spi <= max) ^ invert;
  27. pr_debug(" result %s\n", r ? "PASS" : "FAILED");
  28. return r;
  29. }
  30. static bool esp_mt(const struct sk_buff *skb, struct xt_action_param *par)
  31. {
  32. const struct ip_esp_hdr *eh;
  33. struct ip_esp_hdr _esp;
  34. const struct xt_esp *espinfo = par->matchinfo;
  35. /* Must not be a fragment. */
  36. if (par->fragoff != 0)
  37. return false;
  38. eh = skb_header_pointer(skb, par->thoff, sizeof(_esp), &_esp);
  39. if (eh == NULL) {
  40. /* We've been asked to examine this packet, and we
  41. * can't. Hence, no choice but to drop.
  42. */
  43. pr_debug("Dropping evil ESP tinygram.\n");
  44. par->hotdrop = true;
  45. return false;
  46. }
  47. return spi_match(espinfo->spis[0], espinfo->spis[1], ntohl(eh->spi),
  48. !!(espinfo->invflags & XT_ESP_INV_SPI));
  49. }
  50. static int esp_mt_check(const struct xt_mtchk_param *par)
  51. {
  52. const struct xt_esp *espinfo = par->matchinfo;
  53. if (espinfo->invflags & ~XT_ESP_INV_MASK) {
  54. pr_debug("unknown flags %X\n", espinfo->invflags);
  55. return -EINVAL;
  56. }
  57. return 0;
  58. }
  59. static struct xt_match esp_mt_reg[] __read_mostly = {
  60. {
  61. .name = "esp",
  62. .family = NFPROTO_IPV4,
  63. .checkentry = esp_mt_check,
  64. .match = esp_mt,
  65. .matchsize = sizeof(struct xt_esp),
  66. .proto = IPPROTO_ESP,
  67. .me = THIS_MODULE,
  68. },
  69. {
  70. .name = "esp",
  71. .family = NFPROTO_IPV6,
  72. .checkentry = esp_mt_check,
  73. .match = esp_mt,
  74. .matchsize = sizeof(struct xt_esp),
  75. .proto = IPPROTO_ESP,
  76. .me = THIS_MODULE,
  77. },
  78. };
  79. static int __init esp_mt_init(void)
  80. {
  81. return xt_register_matches(esp_mt_reg, ARRAY_SIZE(esp_mt_reg));
  82. }
  83. static void __exit esp_mt_exit(void)
  84. {
  85. xt_unregister_matches(esp_mt_reg, ARRAY_SIZE(esp_mt_reg));
  86. }
  87. module_init(esp_mt_init);
  88. module_exit(esp_mt_exit);