1 /* Shared library add-on to iptables to add tcp MSS matching support. */
9 #include <linux/netfilter/xt_tcpmss.h>
11 /* Function which prints out usage message. */
12 static void tcpmss_help(void)
15 "tcpmss match options:\n"
16 "[!] --mss value[:value] Match TCP MSS range.\n"
17 " (only valid for TCP SYN or SYN/ACK packets)\n");
20 static const struct option tcpmss_opts[] = {
21 { "mss", 1, NULL, '1' },
26 parse_tcp_mssvalue(const char *mssvalue)
28 unsigned int mssvaluenum;
30 if (string_to_number(mssvalue, 0, 65535, &mssvaluenum) != -1)
31 return (u_int16_t)mssvaluenum;
33 exit_error(PARAMETER_PROBLEM,
34 "Invalid mss `%s' specified", mssvalue);
38 parse_tcp_mssvalues(const char *mssvaluestring,
39 u_int16_t *mss_min, u_int16_t *mss_max)
44 buffer = strdup(mssvaluestring);
45 if ((cp = strchr(buffer, ':')) == NULL)
46 *mss_min = *mss_max = parse_tcp_mssvalue(buffer);
51 *mss_min = buffer[0] ? parse_tcp_mssvalue(buffer) : 0;
52 *mss_max = cp[0] ? parse_tcp_mssvalue(cp) : 0xFFFF;
57 /* Function which parses command options; returns true if it
60 tcpmss_parse(int c, char **argv, int invert, unsigned int *flags,
61 const void *entry, struct xt_entry_match **match)
63 struct xt_tcpmss_match_info *mssinfo =
64 (struct xt_tcpmss_match_info *)(*match)->data;
69 exit_error(PARAMETER_PROBLEM,
70 "Only one `--mss' allowed");
71 check_inverse(optarg, &invert, &optind, 0);
72 parse_tcp_mssvalues(argv[optind-1],
73 &mssinfo->mss_min, &mssinfo->mss_max);
85 print_tcpmss(u_int16_t mss_min, u_int16_t mss_max, int invert, int numeric)
90 if (mss_min == mss_max)
91 printf("%u ", mss_min);
93 printf("%u:%u ", mss_min, mss_max);
96 /* Final check; must have specified --mss. */
97 static void tcpmss_check(unsigned int flags)
100 exit_error(PARAMETER_PROBLEM,
101 "tcpmss match: You must specify `--mss'");
104 /* Prints out the matchinfo. */
106 tcpmss_print(const void *ip, const struct xt_entry_match *match, int numeric)
108 const struct xt_tcpmss_match_info *mssinfo =
109 (const struct xt_tcpmss_match_info *)match->data;
111 printf("tcpmss match ");
112 print_tcpmss(mssinfo->mss_min, mssinfo->mss_max,
113 mssinfo->invert, numeric);
116 /* Saves the union ipt_matchinfo in parsable form to stdout. */
117 static void tcpmss_save(const void *ip, const struct xt_entry_match *match)
119 const struct xt_tcpmss_match_info *mssinfo =
120 (const struct xt_tcpmss_match_info *)match->data;
123 print_tcpmss(mssinfo->mss_min, mssinfo->mss_max,
127 static struct xtables_match tcpmss_match = {
130 .version = XTABLES_VERSION,
131 .size = XT_ALIGN(sizeof(struct xt_tcpmss_match_info)),
132 .userspacesize = XT_ALIGN(sizeof(struct xt_tcpmss_match_info)),
134 .parse = tcpmss_parse,
135 .final_check = tcpmss_check,
136 .print = tcpmss_print,
138 .extra_opts = tcpmss_opts,
141 static struct xtables_match tcpmss_match6 = {
144 .version = XTABLES_VERSION,
145 .size = XT_ALIGN(sizeof(struct xt_tcpmss_match_info)),
146 .userspacesize = XT_ALIGN(sizeof(struct xt_tcpmss_match_info)),
148 .parse = tcpmss_parse,
149 .final_check = tcpmss_check,
150 .print = tcpmss_print,
152 .extra_opts = tcpmss_opts,
157 xtables_register_match(&tcpmss_match);
158 xtables_register_match(&tcpmss_match6);