xref: /aosp_15_r20/external/iptables/extensions/libxt_SYNPROXY.c (revision a71a954618bbadd4a345637e5edcf36eec826889)
1 
2 /*
3  * Copyright (c) 2013 Patrick McHardy <[email protected]>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  */
9 
10 #include <stdbool.h>
11 #include <stdio.h>
12 #include <xtables.h>
13 #include <linux/netfilter/xt_SYNPROXY.h>
14 
15 enum {
16 	O_SACK_PERM = 0,
17 	O_TIMESTAMP,
18 	O_WSCALE,
19 	O_MSS,
20 	O_ECN,
21 };
22 
SYNPROXY_help(void)23 static void SYNPROXY_help(void)
24 {
25 	printf(
26 "SYNPROXY target options:\n"
27 "  --sack-perm                        Set SACK_PERM\n"
28 "  --timestamp                        Set TIMESTAMP\n"
29 "  --wscale value                     Set window scaling factor\n"
30 "  --mss value                        Set MSS value\n"
31 "  --ecn                              Set ECN\n");
32 }
33 
34 static const struct xt_option_entry SYNPROXY_opts[] = {
35 	{.name = "sack-perm", .id = O_SACK_PERM, .type = XTTYPE_NONE, },
36 	{.name = "timestamp", .id = O_TIMESTAMP, .type = XTTYPE_NONE, },
37 	{.name = "wscale",    .id = O_WSCALE,    .type = XTTYPE_UINT32, },
38 	{.name = "mss",       .id = O_MSS,       .type = XTTYPE_UINT32, },
39 	{.name = "ecn",       .id = O_ECN,	 .type = XTTYPE_NONE, },
40 	XTOPT_TABLEEND,
41 };
42 
SYNPROXY_parse(struct xt_option_call * cb)43 static void SYNPROXY_parse(struct xt_option_call *cb)
44 {
45 	struct xt_synproxy_info *info = cb->data;
46 
47 	xtables_option_parse(cb);
48 	switch (cb->entry->id) {
49 	case O_SACK_PERM:
50 		info->options |= XT_SYNPROXY_OPT_SACK_PERM;
51 		break;
52 	case O_TIMESTAMP:
53 		info->options |= XT_SYNPROXY_OPT_TIMESTAMP;
54 		break;
55 	case O_WSCALE:
56 		info->options |= XT_SYNPROXY_OPT_WSCALE;
57 		info->wscale = cb->val.u32;
58 		break;
59 	case O_MSS:
60 		info->options |= XT_SYNPROXY_OPT_MSS;
61 		info->mss = cb->val.u32;
62 		break;
63 	case O_ECN:
64 		info->options |= XT_SYNPROXY_OPT_ECN;
65 		break;
66 	}
67 }
68 
SYNPROXY_check(struct xt_fcheck_call * cb)69 static void SYNPROXY_check(struct xt_fcheck_call *cb)
70 {
71 }
72 
SYNPROXY_print(const void * ip,const struct xt_entry_target * target,int numeric)73 static void SYNPROXY_print(const void *ip, const struct xt_entry_target *target,
74                            int numeric)
75 {
76 	const struct xt_synproxy_info *info =
77 		(const struct xt_synproxy_info *)target->data;
78 
79 	printf(" SYNPROXY ");
80 	if (info->options & XT_SYNPROXY_OPT_SACK_PERM)
81 		printf("sack-perm ");
82 	if (info->options & XT_SYNPROXY_OPT_TIMESTAMP)
83 		printf("timestamp ");
84 	if (info->options & XT_SYNPROXY_OPT_WSCALE)
85 		printf("wscale %u ", info->wscale);
86 	if (info->options & XT_SYNPROXY_OPT_MSS)
87 		printf("mss %u ", info->mss);
88 	if (info->options & XT_SYNPROXY_OPT_ECN)
89 		printf("ecn ");
90 }
91 
SYNPROXY_save(const void * ip,const struct xt_entry_target * target)92 static void SYNPROXY_save(const void *ip, const struct xt_entry_target *target)
93 {
94 	const struct xt_synproxy_info *info =
95 		(const struct xt_synproxy_info *)target->data;
96 
97 	if (info->options & XT_SYNPROXY_OPT_SACK_PERM)
98 		printf(" --sack-perm");
99 	if (info->options & XT_SYNPROXY_OPT_TIMESTAMP)
100 		printf(" --timestamp");
101 	if (info->options & XT_SYNPROXY_OPT_WSCALE)
102 		printf(" --wscale %u", info->wscale);
103 	if (info->options & XT_SYNPROXY_OPT_MSS)
104 		printf(" --mss %u", info->mss);
105 	if (info->options & XT_SYNPROXY_OPT_ECN)
106 		printf(" --ecn");
107 }
108 
SYNPROXY_xlate(struct xt_xlate * xl,const struct xt_xlate_tg_params * params)109 static int SYNPROXY_xlate(struct xt_xlate *xl,
110 		          const struct xt_xlate_tg_params *params)
111 {
112 	const struct xt_synproxy_info *info =
113 		(const struct xt_synproxy_info *)params->target->data;
114 
115 	xt_xlate_add(xl, "synproxy ");
116 
117 	if (info->options & XT_SYNPROXY_OPT_SACK_PERM)
118 		xt_xlate_add(xl, "sack-perm ");
119 	if (info->options & XT_SYNPROXY_OPT_TIMESTAMP)
120 		xt_xlate_add(xl, "timestamp ");
121 	if (info->options & XT_SYNPROXY_OPT_WSCALE)
122 		xt_xlate_add(xl, "wscale %u ", info->wscale);
123 	if (info->options & XT_SYNPROXY_OPT_MSS)
124 		xt_xlate_add(xl, "mss %u ", info->mss);
125 	if (info->options & XT_SYNPROXY_OPT_ECN)
126 		xt_xlate_add(xl, "ecn ");
127 
128 	return 1;
129 }
130 
131 static struct xtables_target synproxy_tg_reg = {
132 	.family        = NFPROTO_UNSPEC,
133 	.name          = "SYNPROXY",
134 	.version       = XTABLES_VERSION,
135 	.revision      = 0,
136 	.size          = XT_ALIGN(sizeof(struct xt_synproxy_info)),
137 	.userspacesize = XT_ALIGN(sizeof(struct xt_synproxy_info)),
138 	.help          = SYNPROXY_help,
139 	.print         = SYNPROXY_print,
140 	.save          = SYNPROXY_save,
141 	.x6_parse      = SYNPROXY_parse,
142 	.x6_fcheck     = SYNPROXY_check,
143 	.x6_options    = SYNPROXY_opts,
144 	.xlate         = SYNPROXY_xlate,
145 };
146 
_init(void)147 void _init(void)
148 {
149 	xtables_register_target(&synproxy_tg_reg);
150 }
151