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
11 enum {
12 O_SET_MSS = 0,
13 O_CLAMP_MSS,
14 };
15
16 struct mssinfo {
17 struct xt_entry_target t;
18 struct xt_tcpmss_info mss;
19 };
20
__TCPMSS_help(int hdrsize)21 static void __TCPMSS_help(int hdrsize)
22 {
23 printf(
24 "TCPMSS target mutually-exclusive options:\n"
25 " --set-mss value explicitly set MSS option to specified value\n"
26 " --clamp-mss-to-pmtu automatically clamp MSS value to (path_MTU - %d)\n",
27 hdrsize);
28 }
29
TCPMSS_help(void)30 static void TCPMSS_help(void)
31 {
32 __TCPMSS_help(sizeof(struct iphdr));
33 }
34
TCPMSS_help6(void)35 static void TCPMSS_help6(void)
36 {
37 __TCPMSS_help(SIZEOF_STRUCT_IP6_HDR);
38 }
39
40 static const struct xt_option_entry TCPMSS4_opts[] = {
41 {.name = "set-mss", .id = O_SET_MSS, .type = XTTYPE_UINT16,
42 .min = 0, .max = UINT16_MAX - sizeof(struct iphdr),
43 .flags = XTOPT_PUT, XTOPT_POINTER(struct xt_tcpmss_info, mss)},
44 {.name = "clamp-mss-to-pmtu", .id = O_CLAMP_MSS, .type = XTTYPE_NONE},
45 XTOPT_TABLEEND,
46 };
47
48 static const struct xt_option_entry TCPMSS6_opts[] = {
49 {.name = "set-mss", .id = O_SET_MSS, .type = XTTYPE_UINT16,
50 .min = 0, .max = UINT16_MAX - SIZEOF_STRUCT_IP6_HDR,
51 .flags = XTOPT_PUT, XTOPT_POINTER(struct xt_tcpmss_info, mss)},
52 {.name = "clamp-mss-to-pmtu", .id = O_CLAMP_MSS, .type = XTTYPE_NONE},
53 XTOPT_TABLEEND,
54 };
55
TCPMSS_parse(struct xt_option_call * cb)56 static void TCPMSS_parse(struct xt_option_call *cb)
57 {
58 struct xt_tcpmss_info *mssinfo = cb->data;
59
60 xtables_option_parse(cb);
61 if (cb->entry->id == O_CLAMP_MSS)
62 mssinfo->mss = XT_TCPMSS_CLAMP_PMTU;
63 }
64
TCPMSS_check(struct xt_fcheck_call * cb)65 static void TCPMSS_check(struct xt_fcheck_call *cb)
66 {
67 if (cb->xflags == 0)
68 xtables_error(PARAMETER_PROBLEM,
69 "TCPMSS target: At least one parameter is required");
70 }
71
TCPMSS_print(const void * ip,const struct xt_entry_target * target,int numeric)72 static void TCPMSS_print(const void *ip, const struct xt_entry_target *target,
73 int numeric)
74 {
75 const struct xt_tcpmss_info *mssinfo =
76 (const struct xt_tcpmss_info *)target->data;
77 if(mssinfo->mss == XT_TCPMSS_CLAMP_PMTU)
78 printf(" TCPMSS clamp to PMTU");
79 else
80 printf(" TCPMSS set %u", mssinfo->mss);
81 }
82
TCPMSS_save(const void * ip,const struct xt_entry_target * target)83 static void TCPMSS_save(const void *ip, const struct xt_entry_target *target)
84 {
85 const struct xt_tcpmss_info *mssinfo =
86 (const struct xt_tcpmss_info *)target->data;
87
88 if(mssinfo->mss == XT_TCPMSS_CLAMP_PMTU)
89 printf(" --clamp-mss-to-pmtu");
90 else
91 printf(" --set-mss %u", mssinfo->mss);
92 }
93
TCPMSS_xlate(struct xt_xlate * xl,const struct xt_xlate_tg_params * params)94 static int TCPMSS_xlate(struct xt_xlate *xl,
95 const struct xt_xlate_tg_params *params)
96 {
97 const struct xt_tcpmss_info *mssinfo =
98 (const struct xt_tcpmss_info *)params->target->data;
99 if (mssinfo->mss == XT_TCPMSS_CLAMP_PMTU)
100 xt_xlate_add(xl, "tcp option maxseg size set rt mtu");
101 else
102 xt_xlate_add(xl, "tcp option maxseg size set %d", mssinfo->mss);
103
104 return 1;
105 }
106
107 static struct xtables_target tcpmss_tg_reg[] = {
108 {
109 .family = NFPROTO_IPV4,
110 .name = "TCPMSS",
111 .version = XTABLES_VERSION,
112 .size = XT_ALIGN(sizeof(struct xt_tcpmss_info)),
113 .userspacesize = XT_ALIGN(sizeof(struct xt_tcpmss_info)),
114 .help = TCPMSS_help,
115 .print = TCPMSS_print,
116 .save = TCPMSS_save,
117 .x6_parse = TCPMSS_parse,
118 .x6_fcheck = TCPMSS_check,
119 .x6_options = TCPMSS4_opts,
120 .xlate = TCPMSS_xlate,
121 },
122 {
123 .family = NFPROTO_IPV6,
124 .name = "TCPMSS",
125 .version = XTABLES_VERSION,
126 .size = XT_ALIGN(sizeof(struct xt_tcpmss_info)),
127 .userspacesize = XT_ALIGN(sizeof(struct xt_tcpmss_info)),
128 .help = TCPMSS_help6,
129 .print = TCPMSS_print,
130 .save = TCPMSS_save,
131 .x6_parse = TCPMSS_parse,
132 .x6_fcheck = TCPMSS_check,
133 .x6_options = TCPMSS6_opts,
134 },
135 };
136
_init(void)137 void _init(void)
138 {
139 xtables_register_targets(tcpmss_tg_reg, ARRAY_SIZE(tcpmss_tg_reg));
140 }
141