xt_tcpmss.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* Kernel module to match TCP MSS values. */
  2. /* Copyright (C) 2000 Marc Boucher <marc@mbsi.ca>
  3. * Portions (C) 2005 by Harald Welte <laforge@netfilter.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/skbuff.h>
  11. #include <net/tcp.h>
  12. #include <linux/netfilter/xt_tcpmss.h>
  13. #include <linux/netfilter/x_tables.h>
  14. #include <linux/netfilter_ipv4/ip_tables.h>
  15. #include <linux/netfilter_ipv6/ip6_tables.h>
  16. MODULE_LICENSE("GPL");
  17. MODULE_AUTHOR("Marc Boucher <marc@mbsi.ca>");
  18. MODULE_DESCRIPTION("Xtables: TCP MSS match");
  19. MODULE_ALIAS("ipt_tcpmss");
  20. MODULE_ALIAS("ip6t_tcpmss");
  21. static bool
  22. tcpmss_mt(const struct sk_buff *skb, struct xt_action_param *par)
  23. {
  24. const struct xt_tcpmss_match_info *info = par->matchinfo;
  25. const struct tcphdr *th;
  26. struct tcphdr _tcph;
  27. /* tcp.doff is only 4 bits, ie. max 15 * 4 bytes */
  28. const u_int8_t *op;
  29. u8 _opt[15 * 4 - sizeof(_tcph)];
  30. unsigned int i, optlen;
  31. /* If we don't have the whole header, drop packet. */
  32. th = skb_header_pointer(skb, par->thoff, sizeof(_tcph), &_tcph);
  33. if (th == NULL)
  34. goto dropit;
  35. /* Malformed. */
  36. if (th->doff*4 < sizeof(*th))
  37. goto dropit;
  38. optlen = th->doff*4 - sizeof(*th);
  39. if (!optlen)
  40. goto out;
  41. /* Truncated options. */
  42. op = skb_header_pointer(skb, par->thoff + sizeof(*th), optlen, _opt);
  43. if (op == NULL)
  44. goto dropit;
  45. for (i = 0; i < optlen; ) {
  46. if (op[i] == TCPOPT_MSS
  47. && (optlen - i) >= TCPOLEN_MSS
  48. && op[i+1] == TCPOLEN_MSS) {
  49. u_int16_t mssval;
  50. mssval = (op[i+2] << 8) | op[i+3];
  51. return (mssval >= info->mss_min &&
  52. mssval <= info->mss_max) ^ info->invert;
  53. }
  54. if (op[i] < 2)
  55. i++;
  56. else
  57. i += op[i+1] ? : 1;
  58. }
  59. out:
  60. return info->invert;
  61. dropit:
  62. par->hotdrop = true;
  63. return false;
  64. }
  65. static struct xt_match tcpmss_mt_reg[] __read_mostly = {
  66. {
  67. .name = "tcpmss",
  68. .family = NFPROTO_IPV4,
  69. .match = tcpmss_mt,
  70. .matchsize = sizeof(struct xt_tcpmss_match_info),
  71. .proto = IPPROTO_TCP,
  72. .me = THIS_MODULE,
  73. },
  74. {
  75. .name = "tcpmss",
  76. .family = NFPROTO_IPV6,
  77. .match = tcpmss_mt,
  78. .matchsize = sizeof(struct xt_tcpmss_match_info),
  79. .proto = IPPROTO_TCP,
  80. .me = THIS_MODULE,
  81. },
  82. };
  83. static int __init tcpmss_mt_init(void)
  84. {
  85. return xt_register_matches(tcpmss_mt_reg, ARRAY_SIZE(tcpmss_mt_reg));
  86. }
  87. static void __exit tcpmss_mt_exit(void)
  88. {
  89. xt_unregister_matches(tcpmss_mt_reg, ARRAY_SIZE(tcpmss_mt_reg));
  90. }
  91. module_init(tcpmss_mt_init);
  92. module_exit(tcpmss_mt_exit);