1#include <stdio.h>
2#include <xtables.h>
3#include <linux/netfilter/xt_tcpmss.h>
4
5enum {
6	O_TCPMSS = 0,
7};
8
9static 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
17static 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
23static 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 (cb->invert)
33		mssinfo->invert = 1;
34}
35
36static void
37tcpmss_print(const void *ip, const struct xt_entry_match *match, int numeric)
38{
39	const struct xt_tcpmss_match_info *info = (void *)match->data;
40
41	printf(" tcpmss match %s", info->invert ? "!" : "");
42	if (info->mss_min == info->mss_max)
43		printf("%u", info->mss_min);
44	else
45		printf("%u:%u", info->mss_min, info->mss_max);
46}
47
48static void tcpmss_save(const void *ip, const struct xt_entry_match *match)
49{
50	const struct xt_tcpmss_match_info *info = (void *)match->data;
51
52	printf("%s --mss ", info->invert ? " !" : "");
53	if (info->mss_min == info->mss_max)
54		printf("%u", info->mss_min);
55	else
56		printf("%u:%u", info->mss_min, info->mss_max);
57}
58
59static struct xtables_match tcpmss_match = {
60	.family		= NFPROTO_UNSPEC,
61	.name		= "tcpmss",
62	.version	= XTABLES_VERSION,
63	.size		= XT_ALIGN(sizeof(struct xt_tcpmss_match_info)),
64	.userspacesize	= XT_ALIGN(sizeof(struct xt_tcpmss_match_info)),
65	.help		= tcpmss_help,
66	.print		= tcpmss_print,
67	.save		= tcpmss_save,
68	.x6_parse	= tcpmss_parse,
69	.x6_options	= tcpmss_opts,
70};
71
72void _init(void)
73{
74	xtables_register_match(&tcpmss_match);
75}
76