xref: /aosp_15_r20/external/iptables/extensions/libxt_tcpmss.c (revision a71a954618bbadd4a345637e5edcf36eec826889)
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 
tcpmss_xlate(struct xt_xlate * xl,const struct xt_xlate_mt_params * params)63 static int tcpmss_xlate(struct xt_xlate *xl,
64 			const struct xt_xlate_mt_params *params)
65 {
66 	const struct xt_tcpmss_match_info *info = (void *)params->match->data;
67 
68 	xt_xlate_add(xl, "tcp option maxseg size %s", info->invert ? "!= " : "");
69 
70 	if (info->mss_min == info->mss_max)
71 		xt_xlate_add(xl, "%u", info->mss_min);
72 	else
73 		xt_xlate_add(xl, "%u-%u", info->mss_min, info->mss_max);
74 
75 	return 1;
76 }
77 
78 static struct xtables_match tcpmss_match = {
79 	.family		= NFPROTO_UNSPEC,
80 	.name		= "tcpmss",
81 	.version	= XTABLES_VERSION,
82 	.size		= XT_ALIGN(sizeof(struct xt_tcpmss_match_info)),
83 	.userspacesize	= XT_ALIGN(sizeof(struct xt_tcpmss_match_info)),
84 	.help		= tcpmss_help,
85 	.print		= tcpmss_print,
86 	.save		= tcpmss_save,
87 	.x6_parse	= tcpmss_parse,
88 	.x6_options	= tcpmss_opts,
89 	.xlate		= tcpmss_xlate,
90 };
91 
_init(void)92 void _init(void)
93 {
94 	xtables_register_match(&tcpmss_match);
95 }
96