xt_esp.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* Kernel module to match ESP parameters. */
  2. /* (C) 1999-2000 Yon Uriarte <yon@astaro.de>
  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/in.h>
  12. #include <linux/ip.h>
  13. #include <linux/netfilter/xt_esp.h>
  14. #include <linux/netfilter/x_tables.h>
  15. #include <linux/netfilter_ipv4/ip_tables.h>
  16. #include <linux/netfilter_ipv6/ip6_tables.h>
  17. MODULE_LICENSE("GPL");
  18. MODULE_AUTHOR("Yon Uriarte <yon@astaro.de>");
  19. MODULE_DESCRIPTION("Xtables: IPsec-ESP packet match");
  20. MODULE_ALIAS("ipt_esp");
  21. MODULE_ALIAS("ip6t_esp");
  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 esp_mt(const struct sk_buff *skb, struct xt_action_param *par)
  34. {
  35. const struct ip_esp_hdr *eh;
  36. struct ip_esp_hdr _esp;
  37. const struct xt_esp *espinfo = par->matchinfo;
  38. /* Must not be a fragment. */
  39. if (par->fragoff != 0)
  40. return false;
  41. eh = skb_header_pointer(skb, par->thoff, sizeof(_esp), &_esp);
  42. if (eh == NULL) {
  43. /* We've been asked to examine this packet, and we
  44. * can't. Hence, no choice but to drop.
  45. */
  46. pr_debug("Dropping evil ESP tinygram.\n");
  47. par->hotdrop = true;
  48. return false;
  49. }
  50. return spi_match(espinfo->spis[0], espinfo->spis[1], ntohl(eh->spi),
  51. !!(espinfo->invflags & XT_ESP_INV_SPI));
  52. }
  53. static int esp_mt_check(const struct xt_mtchk_param *par)
  54. {
  55. const struct xt_esp *espinfo = par->matchinfo;
  56. if (espinfo->invflags & ~XT_ESP_INV_MASK) {
  57. pr_debug("unknown flags %X\n", espinfo->invflags);
  58. return -EINVAL;
  59. }
  60. return 0;
  61. }
  62. static struct xt_match esp_mt_reg[] __read_mostly = {
  63. {
  64. .name = "esp",
  65. .family = NFPROTO_IPV4,
  66. .checkentry = esp_mt_check,
  67. .match = esp_mt,
  68. .matchsize = sizeof(struct xt_esp),
  69. .proto = IPPROTO_ESP,
  70. .me = THIS_MODULE,
  71. },
  72. {
  73. .name = "esp",
  74. .family = NFPROTO_IPV6,
  75. .checkentry = esp_mt_check,
  76. .match = esp_mt,
  77. .matchsize = sizeof(struct xt_esp),
  78. .proto = IPPROTO_ESP,
  79. .me = THIS_MODULE,
  80. },
  81. };
  82. static int __init esp_mt_init(void)
  83. {
  84. return xt_register_matches(esp_mt_reg, ARRAY_SIZE(esp_mt_reg));
  85. }
  86. static void __exit esp_mt_exit(void)
  87. {
  88. xt_unregister_matches(esp_mt_reg, ARRAY_SIZE(esp_mt_reg));
  89. }
  90. module_init(esp_mt_init);
  91. module_exit(esp_mt_exit);