xt_ipcomp.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* Kernel module to match IPComp parameters for IPv4 and IPv6
  2. *
  3. * Copyright (C) 2013 WindRiver
  4. *
  5. * Author:
  6. * Fan Du <fan.du@windriver.com>
  7. *
  8. * Based on:
  9. * net/netfilter/xt_esp.c
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/in.h>
  18. #include <linux/module.h>
  19. #include <linux/skbuff.h>
  20. #include <linux/ip.h>
  21. #include <linux/netfilter/xt_ipcomp.h>
  22. #include <linux/netfilter/x_tables.h>
  23. MODULE_LICENSE("GPL");
  24. MODULE_AUTHOR("Fan Du <fan.du@windriver.com>");
  25. MODULE_DESCRIPTION("Xtables: IPv4/6 IPsec-IPComp SPI match");
  26. /* Returns 1 if the spi is matched by the range, 0 otherwise */
  27. static inline bool
  28. spi_match(u_int32_t min, u_int32_t max, u_int32_t spi, bool invert)
  29. {
  30. bool r;
  31. pr_debug("spi_match:%c 0x%x <= 0x%x <= 0x%x\n",
  32. invert ? '!' : ' ', min, spi, max);
  33. r = (spi >= min && spi <= max) ^ invert;
  34. pr_debug(" result %s\n", r ? "PASS" : "FAILED");
  35. return r;
  36. }
  37. static bool comp_mt(const struct sk_buff *skb, struct xt_action_param *par)
  38. {
  39. struct ip_comp_hdr _comphdr;
  40. const struct ip_comp_hdr *chdr;
  41. const struct xt_ipcomp *compinfo = par->matchinfo;
  42. /* Must not be a fragment. */
  43. if (par->fragoff != 0)
  44. return false;
  45. chdr = skb_header_pointer(skb, par->thoff, sizeof(_comphdr), &_comphdr);
  46. if (chdr == NULL) {
  47. /* We've been asked to examine this packet, and we
  48. * can't. Hence, no choice but to drop.
  49. */
  50. pr_debug("Dropping evil IPComp tinygram.\n");
  51. par->hotdrop = true;
  52. return 0;
  53. }
  54. return spi_match(compinfo->spis[0], compinfo->spis[1],
  55. ntohs(chdr->cpi),
  56. !!(compinfo->invflags & XT_IPCOMP_INV_SPI));
  57. }
  58. static int comp_mt_check(const struct xt_mtchk_param *par)
  59. {
  60. const struct xt_ipcomp *compinfo = par->matchinfo;
  61. /* Must specify no unknown invflags */
  62. if (compinfo->invflags & ~XT_IPCOMP_INV_MASK) {
  63. pr_err("unknown flags %X\n", compinfo->invflags);
  64. return -EINVAL;
  65. }
  66. return 0;
  67. }
  68. static struct xt_match comp_mt_reg[] __read_mostly = {
  69. {
  70. .name = "ipcomp",
  71. .family = NFPROTO_IPV4,
  72. .match = comp_mt,
  73. .matchsize = sizeof(struct xt_ipcomp),
  74. .proto = IPPROTO_COMP,
  75. .checkentry = comp_mt_check,
  76. .me = THIS_MODULE,
  77. },
  78. {
  79. .name = "ipcomp",
  80. .family = NFPROTO_IPV6,
  81. .match = comp_mt,
  82. .matchsize = sizeof(struct xt_ipcomp),
  83. .proto = IPPROTO_COMP,
  84. .checkentry = comp_mt_check,
  85. .me = THIS_MODULE,
  86. },
  87. };
  88. static int __init comp_mt_init(void)
  89. {
  90. return xt_register_matches(comp_mt_reg, ARRAY_SIZE(comp_mt_reg));
  91. }
  92. static void __exit comp_mt_exit(void)
  93. {
  94. xt_unregister_matches(comp_mt_reg, ARRAY_SIZE(comp_mt_reg));
  95. }
  96. module_init(comp_mt_init);
  97. module_exit(comp_mt_exit);