summaryrefslogtreecommitdiffstats
path: root/extensions/libxt_TCPMSS.c
blob: 0d9b200ebc72f4a5232c23cb1d5f4835de8ef515 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* Shared library add-on to iptables to add TCPMSS target support.
 *
 * Copyright (c) 2000 Marc Boucher
*/
#include "config.h"
#include <stdio.h>
#include <xtables.h>
#include <netinet/ip.h>
#include <linux/netfilter/xt_TCPMSS.h>

enum {
	O_SET_MSS = 0,
	O_CLAMP_MSS,
};

struct mssinfo {
	struct xt_entry_target t;
	struct xt_tcpmss_info mss;
};

static void __TCPMSS_help(int hdrsize)
{
	printf(
"TCPMSS target mutually-exclusive options:\n"
"  --set-mss value               explicitly set MSS option to specified value\n"
"  --clamp-mss-to-pmtu           automatically clamp MSS value to (path_MTU - %d)\n",
hdrsize);
}

static void TCPMSS_help(void)
{
	__TCPMSS_help(sizeof(struct iphdr));
}

static void TCPMSS_help6(void)
{
	__TCPMSS_help(SIZEOF_STRUCT_IP6_HDR);
}

static const struct xt_option_entry TCPMSS4_opts[] = {
	{.name = "set-mss", .id = O_SET_MSS, .type = XTTYPE_UINT16,
	 .min = 0, .max = UINT16_MAX - sizeof(struct iphdr),
	 .flags = XTOPT_PUT, XTOPT_POINTER(struct xt_tcpmss_info, mss)},
	{.name = "clamp-mss-to-pmtu", .id = O_CLAMP_MSS, .type = XTTYPE_NONE},
	XTOPT_TABLEEND,
};

static const struct xt_option_entry TCPMSS6_opts[] = {
	{.name = "set-mss", .id = O_SET_MSS, .type = XTTYPE_UINT16,
	 .min = 0, .max = UINT16_MAX - SIZEOF_STRUCT_IP6_HDR,
	 .flags = XTOPT_PUT, XTOPT_POINTER(struct xt_tcpmss_info, mss)},
	{.name = "clamp-mss-to-pmtu", .id = O_CLAMP_MSS, .type = XTTYPE_NONE},
	XTOPT_TABLEEND,
};

static void TCPMSS_parse(struct xt_option_call *cb)
{
	struct xt_tcpmss_info *mssinfo = cb->data;

	xtables_option_parse(cb);
	if (cb->entry->id == O_CLAMP_MSS)
		mssinfo->mss = XT_TCPMSS_CLAMP_PMTU;
}

static void TCPMSS_check(struct xt_fcheck_call *cb)
{
	if (cb->xflags == 0)
		xtables_error(PARAMETER_PROBLEM,
		           "TCPMSS target: At least one parameter is required");
}

static void TCPMSS_print(const void *ip, const struct xt_entry_target *target,
                         int numeric)
{
	const struct xt_tcpmss_info *mssinfo =
		(const struct xt_tcpmss_info *)target->data;
	if(mssinfo->mss == XT_TCPMSS_CLAMP_PMTU)
		printf(" TCPMSS clamp to PMTU");
	else
		printf(" TCPMSS set %u", mssinfo->mss);
}

static void TCPMSS_save(const void *ip, const struct xt_entry_target *target)
{
	const struct xt_tcpmss_info *mssinfo =
		(const struct xt_tcpmss_info *)target->data;

	if(mssinfo->mss == XT_TCPMSS_CLAMP_PMTU)
		printf(" --clamp-mss-to-pmtu");
	else
		printf(" --set-mss %u", mssinfo->mss);
}

static int TCPMSS_xlate(struct xt_xlate *xl,
			const struct xt_xlate_tg_params *params)
{
	const struct xt_tcpmss_info *mssinfo =
		(const struct xt_tcpmss_info *)params->target->data;
	if (mssinfo->mss == XT_TCPMSS_CLAMP_PMTU)
		xt_xlate_add(xl, "tcp option maxseg size set rt mtu");
	else
		xt_xlate_add(xl, "tcp option maxseg size set %d", mssinfo->mss);

	return 1;
}

static struct xtables_target tcpmss_tg_reg[] = {
	{
		.family        = NFPROTO_IPV4,
		.name          = "TCPMSS",
		.version       = XTABLES_VERSION,
		.size          = XT_ALIGN(sizeof(struct xt_tcpmss_info)),
		.userspacesize = XT_ALIGN(sizeof(struct xt_tcpmss_info)),
		.help          = TCPMSS_help,
		.print         = TCPMSS_print,
		.save          = TCPMSS_save,
		.x6_parse      = TCPMSS_parse,
		.x6_fcheck     = TCPMSS_check,
		.x6_options    = TCPMSS4_opts,
		.xlate         = TCPMSS_xlate,
	},
	{
		.family        = NFPROTO_IPV6,
		.name          = "TCPMSS",
		.version       = XTABLES_VERSION,
		.size          = XT_ALIGN(sizeof(struct xt_tcpmss_info)),
		.userspacesize = XT_ALIGN(sizeof(struct xt_tcpmss_info)),
		.help          = TCPMSS_help6,
		.print         = TCPMSS_print,
		.save          = TCPMSS_save,
		.x6_parse      = TCPMSS_parse,
		.x6_fcheck     = TCPMSS_check,
		.x6_options    = TCPMSS6_opts,
	},
};

void _init(void)
{
	xtables_register_targets(tcpmss_tg_reg, ARRAY_SIZE(tcpmss_tg_reg));
}