libxt_TCPMSS.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* Shared library add-on to iptables to add TCPMSS target support.
  2. *
  3. * Copyright (c) 2000 Marc Boucher
  4. */
  5. #include "config.h"
  6. #include <stdio.h>
  7. #include <xtables.h>
  8. #include <netinet/ip.h>
  9. #include <linux/netfilter/xt_TCPMSS.h>
  10. enum {
  11. O_SET_MSS = 0,
  12. O_CLAMP_MSS,
  13. };
  14. struct mssinfo {
  15. struct xt_entry_target t;
  16. struct xt_tcpmss_info mss;
  17. };
  18. static void __TCPMSS_help(int hdrsize)
  19. {
  20. printf(
  21. "TCPMSS target mutually-exclusive options:\n"
  22. " --set-mss value explicitly set MSS option to specified value\n"
  23. " --clamp-mss-to-pmtu automatically clamp MSS value to (path_MTU - %d)\n",
  24. hdrsize);
  25. }
  26. static void TCPMSS_help(void)
  27. {
  28. __TCPMSS_help(sizeof(struct iphdr));
  29. }
  30. static void TCPMSS_help6(void)
  31. {
  32. __TCPMSS_help(SIZEOF_STRUCT_IP6_HDR);
  33. }
  34. static const struct xt_option_entry TCPMSS4_opts[] = {
  35. {.name = "set-mss", .id = O_SET_MSS, .type = XTTYPE_UINT16,
  36. .min = 0, .max = UINT16_MAX - sizeof(struct iphdr),
  37. .flags = XTOPT_PUT, XTOPT_POINTER(struct xt_tcpmss_info, mss)},
  38. {.name = "clamp-mss-to-pmtu", .id = O_CLAMP_MSS, .type = XTTYPE_NONE},
  39. XTOPT_TABLEEND,
  40. };
  41. static const struct xt_option_entry TCPMSS6_opts[] = {
  42. {.name = "set-mss", .id = O_SET_MSS, .type = XTTYPE_UINT16,
  43. .min = 0, .max = UINT16_MAX - SIZEOF_STRUCT_IP6_HDR,
  44. .flags = XTOPT_PUT, XTOPT_POINTER(struct xt_tcpmss_info, mss)},
  45. {.name = "clamp-mss-to-pmtu", .id = O_CLAMP_MSS, .type = XTTYPE_NONE},
  46. XTOPT_TABLEEND,
  47. };
  48. static void TCPMSS_parse(struct xt_option_call *cb)
  49. {
  50. struct xt_tcpmss_info *mssinfo = cb->data;
  51. xtables_option_parse(cb);
  52. if (cb->entry->id == O_CLAMP_MSS)
  53. mssinfo->mss = XT_TCPMSS_CLAMP_PMTU;
  54. }
  55. static void TCPMSS_check(struct xt_fcheck_call *cb)
  56. {
  57. if (cb->xflags == 0)
  58. xtables_error(PARAMETER_PROBLEM,
  59. "TCPMSS target: At least one parameter is required");
  60. }
  61. static void TCPMSS_print(const void *ip, const struct xt_entry_target *target,
  62. int numeric)
  63. {
  64. const struct xt_tcpmss_info *mssinfo =
  65. (const struct xt_tcpmss_info *)target->data;
  66. if(mssinfo->mss == XT_TCPMSS_CLAMP_PMTU)
  67. printf(" TCPMSS clamp to PMTU");
  68. else
  69. printf(" TCPMSS set %u", mssinfo->mss);
  70. }
  71. static void TCPMSS_save(const void *ip, const struct xt_entry_target *target)
  72. {
  73. const struct xt_tcpmss_info *mssinfo =
  74. (const struct xt_tcpmss_info *)target->data;
  75. if(mssinfo->mss == XT_TCPMSS_CLAMP_PMTU)
  76. printf(" --clamp-mss-to-pmtu");
  77. else
  78. printf(" --set-mss %u", mssinfo->mss);
  79. }
  80. static struct xtables_target tcpmss_tg_reg[] = {
  81. {
  82. .family = NFPROTO_IPV4,
  83. .name = "TCPMSS",
  84. .version = XTABLES_VERSION,
  85. .size = XT_ALIGN(sizeof(struct xt_tcpmss_info)),
  86. .userspacesize = XT_ALIGN(sizeof(struct xt_tcpmss_info)),
  87. .help = TCPMSS_help,
  88. .print = TCPMSS_print,
  89. .save = TCPMSS_save,
  90. .x6_parse = TCPMSS_parse,
  91. .x6_fcheck = TCPMSS_check,
  92. .x6_options = TCPMSS4_opts,
  93. },
  94. {
  95. .family = NFPROTO_IPV6,
  96. .name = "TCPMSS",
  97. .version = XTABLES_VERSION,
  98. .size = XT_ALIGN(sizeof(struct xt_tcpmss_info)),
  99. .userspacesize = XT_ALIGN(sizeof(struct xt_tcpmss_info)),
  100. .help = TCPMSS_help6,
  101. .print = TCPMSS_print,
  102. .save = TCPMSS_save,
  103. .x6_parse = TCPMSS_parse,
  104. .x6_fcheck = TCPMSS_check,
  105. .x6_options = TCPMSS6_opts,
  106. },
  107. };
  108. void _init(void)
  109. {
  110. xtables_register_targets(tcpmss_tg_reg, ARRAY_SIZE(tcpmss_tg_reg));
  111. }