xt_ipcomp.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. MODULE_ALIAS("ipt_ipcomp");
  27. MODULE_ALIAS("ip6t_ipcomp");
  28. /* Returns 1 if the spi is matched by the range, 0 otherwise */
  29. static inline bool
  30. spi_match(u_int32_t min, u_int32_t max, u_int32_t spi, bool invert)
  31. {
  32. bool r;
  33. pr_debug("spi_match:%c 0x%x <= 0x%x <= 0x%x\n",
  34. invert ? '!' : ' ', min, spi, max);
  35. r = (spi >= min && spi <= max) ^ invert;
  36. pr_debug(" result %s\n", r ? "PASS" : "FAILED");
  37. return r;
  38. }
  39. static bool comp_mt(const struct sk_buff *skb, struct xt_action_param *par)
  40. {
  41. struct ip_comp_hdr _comphdr;
  42. const struct ip_comp_hdr *chdr;
  43. const struct xt_ipcomp *compinfo = par->matchinfo;
  44. /* Must not be a fragment. */
  45. if (par->fragoff != 0)
  46. return false;
  47. chdr = skb_header_pointer(skb, par->thoff, sizeof(_comphdr), &_comphdr);
  48. if (chdr == NULL) {
  49. /* We've been asked to examine this packet, and we
  50. * can't. Hence, no choice but to drop.
  51. */
  52. pr_debug("Dropping evil IPComp tinygram.\n");
  53. par->hotdrop = true;
  54. return 0;
  55. }
  56. return spi_match(compinfo->spis[0], compinfo->spis[1],
  57. ntohs(chdr->cpi),
  58. !!(compinfo->invflags & XT_IPCOMP_INV_SPI));
  59. }
  60. static int comp_mt_check(const struct xt_mtchk_param *par)
  61. {
  62. const struct xt_ipcomp *compinfo = par->matchinfo;
  63. /* Must specify no unknown invflags */
  64. if (compinfo->invflags & ~XT_IPCOMP_INV_MASK) {
  65. pr_err("unknown flags %X\n", compinfo->invflags);
  66. return -EINVAL;
  67. }
  68. return 0;
  69. }
  70. static struct xt_match comp_mt_reg[] __read_mostly = {
  71. {
  72. .name = "ipcomp",
  73. .family = NFPROTO_IPV4,
  74. .match = comp_mt,
  75. .matchsize = sizeof(struct xt_ipcomp),
  76. .proto = IPPROTO_COMP,
  77. .checkentry = comp_mt_check,
  78. .me = THIS_MODULE,
  79. },
  80. {
  81. .name = "ipcomp",
  82. .family = NFPROTO_IPV6,
  83. .match = comp_mt,
  84. .matchsize = sizeof(struct xt_ipcomp),
  85. .proto = IPPROTO_COMP,
  86. .checkentry = comp_mt_check,
  87. .me = THIS_MODULE,
  88. },
  89. };
  90. static int __init comp_mt_init(void)
  91. {
  92. return xt_register_matches(comp_mt_reg, ARRAY_SIZE(comp_mt_reg));
  93. }
  94. static void __exit comp_mt_exit(void)
  95. {
  96. xt_unregister_matches(comp_mt_reg, ARRAY_SIZE(comp_mt_reg));
  97. }
  98. module_init(comp_mt_init);
  99. module_exit(comp_mt_exit);