1 #include <stdio.h>
2 #include <xtables.h>
3 #include <linux/netfilter/xt_tcpmss.h>
4
5 enum {
6 O_TCPMSS = 0,
7 };
8
tcpmss_help(void)9 static void tcpmss_help(void)
10 {
11 printf(
12 "tcpmss match options:\n"
13 "[!] --mss value[:value] Match TCP MSS range.\n"
14 " (only valid for TCP SYN or SYN/ACK packets)\n");
15 }
16
17 static const struct xt_option_entry tcpmss_opts[] = {
18 {.name = "mss", .id = O_TCPMSS, .type = XTTYPE_UINT16RC,
19 .flags = XTOPT_MAND | XTOPT_INVERT},
20 XTOPT_TABLEEND,
21 };
22
tcpmss_parse(struct xt_option_call * cb)23 static void tcpmss_parse(struct xt_option_call *cb)
24 {
25 struct xt_tcpmss_match_info *mssinfo = cb->data;
26
27 xtables_option_parse(cb);
28 mssinfo->mss_min = cb->val.u16_range[0];
29 mssinfo->mss_max = mssinfo->mss_min;
30 if (cb->nvals == 2) {
31 mssinfo->mss_max = cb->val.u16_range[1];
32 if (mssinfo->mss_max < mssinfo->mss_min)
33 xtables_error(PARAMETER_PROBLEM,
34 "tcpmss: invalid range given");
35 }
36 if (cb->invert)
37 mssinfo->invert = 1;
38 }
39
40 static void
tcpmss_print(const void * ip,const struct xt_entry_match * match,int numeric)41 tcpmss_print(const void *ip, const struct xt_entry_match *match, int numeric)
42 {
43 const struct xt_tcpmss_match_info *info = (void *)match->data;
44
45 printf(" tcpmss match %s", info->invert ? "!" : "");
46 if (info->mss_min == info->mss_max)
47 printf("%u", info->mss_min);
48 else
49 printf("%u:%u", info->mss_min, info->mss_max);
50 }
51
tcpmss_save(const void * ip,const struct xt_entry_match * match)52 static void tcpmss_save(const void *ip, const struct xt_entry_match *match)
53 {
54 const struct xt_tcpmss_match_info *info = (void *)match->data;
55
56 printf("%s --mss ", info->invert ? " !" : "");
57 if (info->mss_min == info->mss_max)
58 printf("%u", info->mss_min);
59 else
60 printf("%u:%u", info->mss_min, info->mss_max);
61 }
62
63 static struct xtables_match tcpmss_match = {
64 .family = NFPROTO_UNSPEC,
65 .name = "tcpmss",
66 .version = XTABLES_VERSION,
67 .size = XT_ALIGN(sizeof(struct xt_tcpmss_match_info)),
68 .userspacesize = XT_ALIGN(sizeof(struct xt_tcpmss_match_info)),
69 .help = tcpmss_help,
70 .print = tcpmss_print,
71 .save = tcpmss_save,
72 .x6_parse = tcpmss_parse,
73 .x6_options = tcpmss_opts,
74 };
75
_init(void)76 void _init(void)
77 {
78 xtables_register_match(&tcpmss_match);
79 }
80