xref: /freebsd/sys/netgraph/ng_tcpmss.c (revision bc12a09303d2aee39ef641250ea7852df2074b0a)
1adf284a2SGleb Smirnoff /*-
2adf284a2SGleb Smirnoff  * ng_tcpmss.c
3adf284a2SGleb Smirnoff  *
4adf284a2SGleb Smirnoff  * Copyright (c) 2004, Alexey Popov <lollypop@flexuser.ru>
5adf284a2SGleb Smirnoff  * All rights reserved.
6adf284a2SGleb Smirnoff  *
7adf284a2SGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
8adf284a2SGleb Smirnoff  * modification, are permitted provided that the following conditions
9adf284a2SGleb Smirnoff  * are met:
10adf284a2SGleb Smirnoff  * 1. Redistributions of source code must retain the above copyright
11adf284a2SGleb Smirnoff  *    notice unmodified, this list of conditions, and the following
12adf284a2SGleb Smirnoff  *    disclaimer.
13adf284a2SGleb Smirnoff  * 2. Redistributions in binary form must reproduce the above copyright
14adf284a2SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer in the
15adf284a2SGleb Smirnoff  *    documentation and/or other materials provided with the distribution.
16adf284a2SGleb Smirnoff  *
17adf284a2SGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18adf284a2SGleb Smirnoff  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19adf284a2SGleb Smirnoff  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20adf284a2SGleb Smirnoff  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21adf284a2SGleb Smirnoff  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22adf284a2SGleb Smirnoff  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23adf284a2SGleb Smirnoff  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24adf284a2SGleb Smirnoff  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25adf284a2SGleb Smirnoff  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26adf284a2SGleb Smirnoff  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27adf284a2SGleb Smirnoff  * SUCH DAMAGE.
28adf284a2SGleb Smirnoff  *
29adf284a2SGleb Smirnoff  * This software includes fragments of the following programs:
30adf284a2SGleb Smirnoff  *	tcpmssd		Ruslan Ermilov <ru@FreeBSD.org>
31adf284a2SGleb Smirnoff  *
32adf284a2SGleb Smirnoff  * $FreeBSD$
33adf284a2SGleb Smirnoff  */
34adf284a2SGleb Smirnoff 
35adf284a2SGleb Smirnoff /*
36adf284a2SGleb Smirnoff  * This node is netgraph tool for workaround of PMTUD problem. It acts
37adf284a2SGleb Smirnoff  * like filter for IP packets. If configured, it reduces MSS of TCP SYN
38adf284a2SGleb Smirnoff  * packets.
39adf284a2SGleb Smirnoff  *
40adf284a2SGleb Smirnoff  * Configuration can be done by sending NGM_TCPMSS_CONFIG message. The
41adf284a2SGleb Smirnoff  * message sets filter for incoming packets on hook 'inHook'. Packet's
42adf284a2SGleb Smirnoff  * TCP MSS field is lowered to 'maxMSS' parameter and resulting packet
43adf284a2SGleb Smirnoff  * is sent to 'outHook'.
44adf284a2SGleb Smirnoff  *
45adf284a2SGleb Smirnoff  * XXX: statistics are updated not atomically, so they may broke on SMP.
46adf284a2SGleb Smirnoff  */
47adf284a2SGleb Smirnoff 
48adf284a2SGleb Smirnoff #include <sys/param.h>
49adf284a2SGleb Smirnoff #include <sys/systm.h>
50adf284a2SGleb Smirnoff #include <sys/errno.h>
51adf284a2SGleb Smirnoff #include <sys/kernel.h>
52adf284a2SGleb Smirnoff #include <sys/malloc.h>
53adf284a2SGleb Smirnoff #include <sys/mbuf.h>
54adf284a2SGleb Smirnoff 
55adf284a2SGleb Smirnoff #include <netinet/in.h>
56adf284a2SGleb Smirnoff #include <netinet/in_systm.h>
57adf284a2SGleb Smirnoff #include <netinet/ip.h>
58adf284a2SGleb Smirnoff #include <netinet/tcp.h>
59adf284a2SGleb Smirnoff 
60adf284a2SGleb Smirnoff #include <netgraph/ng_message.h>
61adf284a2SGleb Smirnoff #include <netgraph/netgraph.h>
62adf284a2SGleb Smirnoff #include <netgraph/ng_parse.h>
63adf284a2SGleb Smirnoff #include <netgraph/ng_tcpmss.h>
64adf284a2SGleb Smirnoff 
65adf284a2SGleb Smirnoff /* Per hook info. */
66adf284a2SGleb Smirnoff typedef struct {
67adf284a2SGleb Smirnoff 	hook_p				outHook;
68adf284a2SGleb Smirnoff 	struct ng_tcpmss_hookstat	stats;
69adf284a2SGleb Smirnoff } *hpriv_p;
70adf284a2SGleb Smirnoff 
71adf284a2SGleb Smirnoff /* Netgraph methods. */
72adf284a2SGleb Smirnoff static ng_constructor_t	ng_tcpmss_constructor;
73adf284a2SGleb Smirnoff static ng_rcvmsg_t	ng_tcpmss_rcvmsg;
74adf284a2SGleb Smirnoff static ng_newhook_t	ng_tcpmss_newhook;
75adf284a2SGleb Smirnoff static ng_rcvdata_t	ng_tcpmss_rcvdata;
76adf284a2SGleb Smirnoff static ng_disconnect_t	ng_tcpmss_disconnect;
77adf284a2SGleb Smirnoff 
78adf284a2SGleb Smirnoff static int correct_mss(struct tcphdr *, int, uint16_t, int);
79adf284a2SGleb Smirnoff 
80adf284a2SGleb Smirnoff /* Parse type for struct ng_tcpmss_hookstat. */
81adf284a2SGleb Smirnoff static const struct ng_parse_struct_field ng_tcpmss_hookstat_type_fields[]
82adf284a2SGleb Smirnoff 	= NG_TCPMSS_HOOKSTAT_INFO;
83adf284a2SGleb Smirnoff static const struct ng_parse_type ng_tcpmss_hookstat_type = {
84adf284a2SGleb Smirnoff 	&ng_parse_struct_type,
85adf284a2SGleb Smirnoff 	&ng_tcpmss_hookstat_type_fields
86adf284a2SGleb Smirnoff };
87adf284a2SGleb Smirnoff 
88adf284a2SGleb Smirnoff /* Parse type for struct ng_tcpmss_config. */
89adf284a2SGleb Smirnoff static const struct ng_parse_struct_field ng_tcpmss_config_type_fields[]
90adf284a2SGleb Smirnoff 	= NG_TCPMSS_CONFIG_INFO;
91adf284a2SGleb Smirnoff static const struct ng_parse_type ng_tcpmss_config_type = {
92adf284a2SGleb Smirnoff 	&ng_parse_struct_type,
93adf284a2SGleb Smirnoff 	ng_tcpmss_config_type_fields
94adf284a2SGleb Smirnoff };
95adf284a2SGleb Smirnoff 
96adf284a2SGleb Smirnoff /* List of commands and how to convert arguments to/from ASCII. */
97adf284a2SGleb Smirnoff static const struct ng_cmdlist ng_tcpmss_cmds[] = {
98adf284a2SGleb Smirnoff 	{
99adf284a2SGleb Smirnoff 	  NGM_TCPMSS_COOKIE,
100adf284a2SGleb Smirnoff 	  NGM_TCPMSS_GET_STATS,
101adf284a2SGleb Smirnoff 	  "getstats",
102adf284a2SGleb Smirnoff 	  &ng_parse_hookbuf_type,
103adf284a2SGleb Smirnoff 	  &ng_tcpmss_hookstat_type
104adf284a2SGleb Smirnoff 	},
105adf284a2SGleb Smirnoff 	{
106adf284a2SGleb Smirnoff 	  NGM_TCPMSS_COOKIE,
107adf284a2SGleb Smirnoff 	  NGM_TCPMSS_CLR_STATS,
108adf284a2SGleb Smirnoff 	  "clrstats",
109adf284a2SGleb Smirnoff 	  &ng_parse_hookbuf_type,
110adf284a2SGleb Smirnoff 	  NULL
111adf284a2SGleb Smirnoff 	},
112adf284a2SGleb Smirnoff 	{
113adf284a2SGleb Smirnoff 	  NGM_TCPMSS_COOKIE,
114adf284a2SGleb Smirnoff 	  NGM_TCPMSS_GETCLR_STATS,
115adf284a2SGleb Smirnoff 	  "getclrstats",
116adf284a2SGleb Smirnoff 	  &ng_parse_hookbuf_type,
117adf284a2SGleb Smirnoff 	  &ng_tcpmss_hookstat_type
118adf284a2SGleb Smirnoff 	},
119adf284a2SGleb Smirnoff 	{
120adf284a2SGleb Smirnoff 	  NGM_TCPMSS_COOKIE,
121adf284a2SGleb Smirnoff 	  NGM_TCPMSS_CONFIG,
122adf284a2SGleb Smirnoff 	  "config",
123adf284a2SGleb Smirnoff 	  &ng_tcpmss_config_type,
124adf284a2SGleb Smirnoff 	  NULL
125adf284a2SGleb Smirnoff 	},
126adf284a2SGleb Smirnoff 	{ 0 }
127adf284a2SGleb Smirnoff };
128adf284a2SGleb Smirnoff 
129adf284a2SGleb Smirnoff /* Netgraph type descriptor. */
130adf284a2SGleb Smirnoff static struct ng_type ng_tcpmss_typestruct = {
131adf284a2SGleb Smirnoff 	.version =	NG_ABI_VERSION,
132adf284a2SGleb Smirnoff 	.name =		NG_TCPMSS_NODE_TYPE,
133adf284a2SGleb Smirnoff 	.constructor =	ng_tcpmss_constructor,
134adf284a2SGleb Smirnoff 	.rcvmsg =	ng_tcpmss_rcvmsg,
135adf284a2SGleb Smirnoff 	.newhook =	ng_tcpmss_newhook,
136adf284a2SGleb Smirnoff 	.rcvdata =	ng_tcpmss_rcvdata,
137adf284a2SGleb Smirnoff 	.disconnect =	ng_tcpmss_disconnect,
138adf284a2SGleb Smirnoff 	.cmdlist =	ng_tcpmss_cmds,
139adf284a2SGleb Smirnoff };
140adf284a2SGleb Smirnoff 
141adf284a2SGleb Smirnoff NETGRAPH_INIT(tcpmss, &ng_tcpmss_typestruct);
142adf284a2SGleb Smirnoff 
143adf284a2SGleb Smirnoff #define	ERROUT(x)	{ error = (x); goto done; }
144adf284a2SGleb Smirnoff 
145adf284a2SGleb Smirnoff /*
146adf284a2SGleb Smirnoff  * Node constructor. No special actions required.
147adf284a2SGleb Smirnoff  */
148adf284a2SGleb Smirnoff static int
149adf284a2SGleb Smirnoff ng_tcpmss_constructor(node_p node)
150adf284a2SGleb Smirnoff {
151adf284a2SGleb Smirnoff 	return (0);
152adf284a2SGleb Smirnoff }
153adf284a2SGleb Smirnoff 
154adf284a2SGleb Smirnoff /*
155adf284a2SGleb Smirnoff  * Add a hook. Any unique name is OK.
156adf284a2SGleb Smirnoff  */
157adf284a2SGleb Smirnoff static int
158adf284a2SGleb Smirnoff ng_tcpmss_newhook(node_p node, hook_p hook, const char *name)
159adf284a2SGleb Smirnoff {
160adf284a2SGleb Smirnoff 	hpriv_p priv;
161adf284a2SGleb Smirnoff 
162adf284a2SGleb Smirnoff 	MALLOC(priv, hpriv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
163adf284a2SGleb Smirnoff 	if (priv == NULL)
164adf284a2SGleb Smirnoff 		return (ENOMEM);
165adf284a2SGleb Smirnoff 
166adf284a2SGleb Smirnoff 	NG_HOOK_SET_PRIVATE(hook, priv);
167adf284a2SGleb Smirnoff 
168adf284a2SGleb Smirnoff 	return (0);
169adf284a2SGleb Smirnoff }
170adf284a2SGleb Smirnoff 
171adf284a2SGleb Smirnoff /*
172adf284a2SGleb Smirnoff  * Receive a control message.
173adf284a2SGleb Smirnoff  */
174adf284a2SGleb Smirnoff static int
175adf284a2SGleb Smirnoff ng_tcpmss_rcvmsg
176adf284a2SGleb Smirnoff (node_p node, item_p item, hook_p lasthook)
177adf284a2SGleb Smirnoff {
178adf284a2SGleb Smirnoff 	struct ng_mesg *msg, *resp = NULL;
179adf284a2SGleb Smirnoff 	int error = 0;
180adf284a2SGleb Smirnoff 
181adf284a2SGleb Smirnoff 	NGI_GET_MSG(item, msg);
182adf284a2SGleb Smirnoff 
183adf284a2SGleb Smirnoff 	switch (msg->header.typecookie) {
184adf284a2SGleb Smirnoff 	case NGM_TCPMSS_COOKIE:
185adf284a2SGleb Smirnoff 		switch (msg->header.cmd) {
186adf284a2SGleb Smirnoff 		case NGM_TCPMSS_GET_STATS:
187adf284a2SGleb Smirnoff 		case NGM_TCPMSS_CLR_STATS:
188adf284a2SGleb Smirnoff 		case NGM_TCPMSS_GETCLR_STATS:
189adf284a2SGleb Smirnoff 		    {
190adf284a2SGleb Smirnoff 			hook_p hook;
191adf284a2SGleb Smirnoff 			hpriv_p priv;
192adf284a2SGleb Smirnoff 
193adf284a2SGleb Smirnoff 			/* Check that message is long enough. */
194adf284a2SGleb Smirnoff 			if (msg->header.arglen != NG_HOOKSIZ)
195adf284a2SGleb Smirnoff 				ERROUT(EINVAL);
196adf284a2SGleb Smirnoff 
197adf284a2SGleb Smirnoff 			/* Find this hook. */
198adf284a2SGleb Smirnoff 			hook = ng_findhook(node, (char *)msg->data);
199adf284a2SGleb Smirnoff 			if (hook == NULL)
200adf284a2SGleb Smirnoff 				ERROUT(ENOENT);
201adf284a2SGleb Smirnoff 
202adf284a2SGleb Smirnoff 			priv = NG_HOOK_PRIVATE(hook);
203adf284a2SGleb Smirnoff 
204adf284a2SGleb Smirnoff 			/* Create response. */
205adf284a2SGleb Smirnoff 			if (msg->header.cmd != NGM_TCPMSS_CLR_STATS) {
206adf284a2SGleb Smirnoff 				NG_MKRESPONSE(resp, msg,
207adf284a2SGleb Smirnoff 				    sizeof(struct ng_tcpmss_hookstat), M_NOWAIT);
208adf284a2SGleb Smirnoff 				if (resp == NULL)
209adf284a2SGleb Smirnoff 					ERROUT(ENOMEM);
210adf284a2SGleb Smirnoff 				bcopy(&priv->stats, resp->data,
211adf284a2SGleb Smirnoff 				    sizeof(struct ng_tcpmss_hookstat));
212adf284a2SGleb Smirnoff 			}
213adf284a2SGleb Smirnoff 
214adf284a2SGleb Smirnoff 			if (msg->header.cmd != NGM_TCPMSS_GET_STATS)
215adf284a2SGleb Smirnoff 				bzero(&priv->stats,
216adf284a2SGleb Smirnoff 				    sizeof(struct ng_tcpmss_hookstat));
217adf284a2SGleb Smirnoff 			break;
218adf284a2SGleb Smirnoff 		    }
219adf284a2SGleb Smirnoff 		case NGM_TCPMSS_CONFIG:
220adf284a2SGleb Smirnoff 		    {
221adf284a2SGleb Smirnoff 			struct ng_tcpmss_config *set;
222adf284a2SGleb Smirnoff 			hook_p in, out;
223adf284a2SGleb Smirnoff 			hpriv_p priv;
224adf284a2SGleb Smirnoff 
225adf284a2SGleb Smirnoff 			/* Check that message is long enough. */
226adf284a2SGleb Smirnoff 			if (msg->header.arglen !=
227adf284a2SGleb Smirnoff 			    sizeof(struct ng_tcpmss_config))
228adf284a2SGleb Smirnoff 				ERROUT(EINVAL);
229adf284a2SGleb Smirnoff 
230adf284a2SGleb Smirnoff 			set = (struct ng_tcpmss_config *)msg->data;
231adf284a2SGleb Smirnoff 			in = ng_findhook(node, set->inHook);
232adf284a2SGleb Smirnoff 			out = ng_findhook(node, set->outHook);
233adf284a2SGleb Smirnoff 			if (in == NULL || out == NULL)
234adf284a2SGleb Smirnoff 				ERROUT(ENOENT);
235adf284a2SGleb Smirnoff 
236adf284a2SGleb Smirnoff 			/* Configure MSS hack. */
237adf284a2SGleb Smirnoff 			priv = NG_HOOK_PRIVATE(in);
238adf284a2SGleb Smirnoff 			priv->outHook = out;
239adf284a2SGleb Smirnoff 			priv->stats.maxMSS = set->maxMSS;
240adf284a2SGleb Smirnoff 
241adf284a2SGleb Smirnoff 			break;
242adf284a2SGleb Smirnoff  		    }
243adf284a2SGleb Smirnoff 		default:
244adf284a2SGleb Smirnoff 			error = EINVAL;
245adf284a2SGleb Smirnoff 			break;
246adf284a2SGleb Smirnoff 		}
247adf284a2SGleb Smirnoff 		break;
248adf284a2SGleb Smirnoff 	default:
249adf284a2SGleb Smirnoff 		error = EINVAL;
250adf284a2SGleb Smirnoff 		break;
251adf284a2SGleb Smirnoff 	}
252adf284a2SGleb Smirnoff 
253adf284a2SGleb Smirnoff done:
254adf284a2SGleb Smirnoff 	NG_RESPOND_MSG(error, node, item, resp);
255adf284a2SGleb Smirnoff 	NG_FREE_MSG(msg);
256adf284a2SGleb Smirnoff 
257adf284a2SGleb Smirnoff 	return (error);
258adf284a2SGleb Smirnoff }
259adf284a2SGleb Smirnoff 
260adf284a2SGleb Smirnoff /*
261adf284a2SGleb Smirnoff  * Receive data on a hook, and hack MSS.
262adf284a2SGleb Smirnoff  *
263adf284a2SGleb Smirnoff  */
264adf284a2SGleb Smirnoff static int
265adf284a2SGleb Smirnoff ng_tcpmss_rcvdata(hook_p hook, item_p item)
266adf284a2SGleb Smirnoff {
267adf284a2SGleb Smirnoff 	hpriv_p priv = NG_HOOK_PRIVATE(hook);
268adf284a2SGleb Smirnoff 	struct mbuf *m = NULL;
269adf284a2SGleb Smirnoff 	struct ip *ip;
270adf284a2SGleb Smirnoff 	struct tcphdr *tcp;
271adf284a2SGleb Smirnoff 	int iphlen, tcphlen, pktlen;
272adf284a2SGleb Smirnoff 	int pullup_len = 0;
273adf284a2SGleb Smirnoff 	int error = 0;
274adf284a2SGleb Smirnoff 
275adf284a2SGleb Smirnoff 	/* Drop packets if filter is not configured on this hook. */
276adf284a2SGleb Smirnoff 	if (priv->outHook == NULL)
277adf284a2SGleb Smirnoff 		goto done;
278adf284a2SGleb Smirnoff 
279adf284a2SGleb Smirnoff 	NGI_GET_M(item, m);
280adf284a2SGleb Smirnoff 
281adf284a2SGleb Smirnoff 	/* Update stats on incoming hook. */
282adf284a2SGleb Smirnoff 	pktlen = m->m_pkthdr.len;
283adf284a2SGleb Smirnoff 	priv->stats.Octets += pktlen;
284adf284a2SGleb Smirnoff 	priv->stats.Packets++;
285adf284a2SGleb Smirnoff 
286adf284a2SGleb Smirnoff 	/* Check whether we configured to fix MSS. */
287adf284a2SGleb Smirnoff 	if (priv->stats.maxMSS == 0)
288adf284a2SGleb Smirnoff 		goto send;
289adf284a2SGleb Smirnoff 
290adf284a2SGleb Smirnoff #define	M_CHECK(length) do {					\
291adf284a2SGleb Smirnoff 	pullup_len += length;					\
292e6da342bSGleb Smirnoff 	if ((m)->m_pkthdr.len < pullup_len)			\
293adf284a2SGleb Smirnoff 		goto send;					\
294e6da342bSGleb Smirnoff 	if ((m)->m_len < pullup_len &&				\
295e6da342bSGleb Smirnoff 	   (((m) = m_pullup((m), pullup_len)) == NULL))		\
296adf284a2SGleb Smirnoff 		ERROUT(ENOBUFS);				\
297adf284a2SGleb Smirnoff 	} while (0)
298adf284a2SGleb Smirnoff 
299adf284a2SGleb Smirnoff 	/* Check mbuf packet size and arrange for IP header. */
300adf284a2SGleb Smirnoff 	M_CHECK(sizeof(struct ip));
301adf284a2SGleb Smirnoff 	ip = mtod(m, struct ip *);
302adf284a2SGleb Smirnoff 
303adf284a2SGleb Smirnoff 	/* Check IP version. */
304adf284a2SGleb Smirnoff 	if (ip->ip_v != IPVERSION)
305adf284a2SGleb Smirnoff 		ERROUT(EINVAL);
306adf284a2SGleb Smirnoff 
307adf284a2SGleb Smirnoff 	/* Check IP header length. */
308adf284a2SGleb Smirnoff 	iphlen = ip->ip_hl << 2;
309adf284a2SGleb Smirnoff 	if (iphlen < sizeof(struct ip) || iphlen > pktlen )
310adf284a2SGleb Smirnoff 		ERROUT(EINVAL);
311adf284a2SGleb Smirnoff 
312adf284a2SGleb Smirnoff         /* Check if it is TCP. */
313adf284a2SGleb Smirnoff 	if (!(ip->ip_p == IPPROTO_TCP))
314adf284a2SGleb Smirnoff 		goto send;
315adf284a2SGleb Smirnoff 
316adf284a2SGleb Smirnoff 	/* Check mbuf packet size and arrange for IP+TCP header */
317e6da342bSGleb Smirnoff 	M_CHECK(iphlen - sizeof(struct ip) + sizeof(struct tcphdr));
318bc12a093SGleb Smirnoff 	ip = mtod(m, struct ip *);
319adf284a2SGleb Smirnoff 	tcp = (struct tcphdr *)((caddr_t )ip + iphlen);
320adf284a2SGleb Smirnoff 
321adf284a2SGleb Smirnoff 	/* Check TCP header length. */
322adf284a2SGleb Smirnoff 	tcphlen = tcp->th_off << 2;
323adf284a2SGleb Smirnoff 	if (tcphlen < sizeof(struct tcphdr) || tcphlen > pktlen - iphlen)
324adf284a2SGleb Smirnoff 		ERROUT(EINVAL);
325adf284a2SGleb Smirnoff 
326adf284a2SGleb Smirnoff 	/* Check SYN packet and has options. */
327adf284a2SGleb Smirnoff 	if (!(tcp->th_flags & TH_SYN) || tcphlen == sizeof(struct tcphdr))
328adf284a2SGleb Smirnoff 		goto send;
329adf284a2SGleb Smirnoff 
330adf284a2SGleb Smirnoff 	/* Update SYN stats. */
331adf284a2SGleb Smirnoff 	priv->stats.SYNPkts++;
332adf284a2SGleb Smirnoff 
333e6da342bSGleb Smirnoff 	M_CHECK(tcphlen - sizeof(struct tcphdr));
334bc12a093SGleb Smirnoff 	ip = mtod(m, struct ip *);
335bc12a093SGleb Smirnoff 	tcp = (struct tcphdr *)((caddr_t )ip + iphlen);
336adf284a2SGleb Smirnoff 
337adf284a2SGleb Smirnoff #undef	M_CHECK
338adf284a2SGleb Smirnoff 
339adf284a2SGleb Smirnoff 	/* Fix MSS and update stats. */
340adf284a2SGleb Smirnoff 	if (correct_mss(tcp, tcphlen, priv->stats.maxMSS,
341adf284a2SGleb Smirnoff 	    m->m_pkthdr.csum_flags))
342adf284a2SGleb Smirnoff 		priv->stats.FixedPkts++;
343adf284a2SGleb Smirnoff 
344adf284a2SGleb Smirnoff send:
345adf284a2SGleb Smirnoff 	/* Deliver frame out destination hook. */
346e6da342bSGleb Smirnoff 	NG_FWD_NEW_DATA(error, item, priv->outHook, m);
347adf284a2SGleb Smirnoff 
348adf284a2SGleb Smirnoff 	return (error);
349adf284a2SGleb Smirnoff 
350adf284a2SGleb Smirnoff done:
351adf284a2SGleb Smirnoff 	NG_FREE_ITEM(item);
352adf284a2SGleb Smirnoff 	NG_FREE_M(m);
353adf284a2SGleb Smirnoff 
354adf284a2SGleb Smirnoff 	return (error);
355adf284a2SGleb Smirnoff }
356adf284a2SGleb Smirnoff 
357adf284a2SGleb Smirnoff /*
358adf284a2SGleb Smirnoff  * Hook disconnection.
359adf284a2SGleb Smirnoff  * We must check all hooks, since they may reference this one.
360adf284a2SGleb Smirnoff  */
361adf284a2SGleb Smirnoff static int
362adf284a2SGleb Smirnoff ng_tcpmss_disconnect(hook_p hook)
363adf284a2SGleb Smirnoff {
364adf284a2SGleb Smirnoff 	node_p node = NG_HOOK_NODE(hook);
365adf284a2SGleb Smirnoff 	hook_p hook2;
366adf284a2SGleb Smirnoff 
367adf284a2SGleb Smirnoff 	LIST_FOREACH(hook2, &node->nd_hooks, hk_hooks) {
368adf284a2SGleb Smirnoff 		hpriv_p priv = NG_HOOK_PRIVATE(hook2);
369adf284a2SGleb Smirnoff 
370adf284a2SGleb Smirnoff 		if (priv->outHook == hook)
371adf284a2SGleb Smirnoff 			priv->outHook = NULL;
372adf284a2SGleb Smirnoff 	}
373adf284a2SGleb Smirnoff 
374df7e759cSGleb Smirnoff 	FREE(NG_HOOK_PRIVATE(hook), M_NETGRAPH);
375df7e759cSGleb Smirnoff 
376adf284a2SGleb Smirnoff 	if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
377adf284a2SGleb Smirnoff 		ng_rmnode_self(NG_HOOK_NODE(hook));
378adf284a2SGleb Smirnoff 
379adf284a2SGleb Smirnoff 	return (0);
380adf284a2SGleb Smirnoff }
381adf284a2SGleb Smirnoff 
382adf284a2SGleb Smirnoff /*
383adf284a2SGleb Smirnoff  * Code from tcpmssd.
384adf284a2SGleb Smirnoff  */
385adf284a2SGleb Smirnoff 
386adf284a2SGleb Smirnoff /*-
387adf284a2SGleb Smirnoff  * The following macro is used to update an
388adf284a2SGleb Smirnoff  * internet checksum.  "acc" is a 32-bit
389adf284a2SGleb Smirnoff  * accumulation of all the changes to the
390adf284a2SGleb Smirnoff  * checksum (adding in old 16-bit words and
391adf284a2SGleb Smirnoff  * subtracting out new words), and "cksum"
392adf284a2SGleb Smirnoff  * is the checksum value to be updated.
393adf284a2SGleb Smirnoff  */
394adf284a2SGleb Smirnoff #define TCPMSS_ADJUST_CHECKSUM(acc, cksum) do {		\
395adf284a2SGleb Smirnoff 	acc += cksum;					\
396adf284a2SGleb Smirnoff 	if (acc < 0) {					\
397adf284a2SGleb Smirnoff 		acc = -acc;				\
398adf284a2SGleb Smirnoff 		acc = (acc >> 16) + (acc & 0xffff);	\
399adf284a2SGleb Smirnoff 		acc += acc >> 16;			\
400adf284a2SGleb Smirnoff 		cksum = (u_short) ~acc;			\
401adf284a2SGleb Smirnoff 	} else {					\
402adf284a2SGleb Smirnoff 		acc = (acc >> 16) + (acc & 0xffff);	\
403adf284a2SGleb Smirnoff 		acc += acc >> 16;			\
404adf284a2SGleb Smirnoff 		cksum = (u_short) acc;			\
405adf284a2SGleb Smirnoff 	}						\
406adf284a2SGleb Smirnoff } while (0);
407adf284a2SGleb Smirnoff 
408adf284a2SGleb Smirnoff static int
409adf284a2SGleb Smirnoff correct_mss(struct tcphdr *tc, int hlen, uint16_t maxmss, int flags)
410adf284a2SGleb Smirnoff {
411adf284a2SGleb Smirnoff 	int olen, optlen;
412adf284a2SGleb Smirnoff 	u_char *opt;
413adf284a2SGleb Smirnoff 	uint16_t *mss;
414adf284a2SGleb Smirnoff 	int accumulate;
415adf284a2SGleb Smirnoff 	int res = 0;
416adf284a2SGleb Smirnoff 
417adf284a2SGleb Smirnoff 	for (olen = hlen - sizeof(struct tcphdr), opt = (u_char *)(tc + 1);
418adf284a2SGleb Smirnoff 	     olen > 0; olen -= optlen, opt += optlen) {
419adf284a2SGleb Smirnoff 		if (*opt == TCPOPT_EOL)
420adf284a2SGleb Smirnoff 			break;
421adf284a2SGleb Smirnoff 		else if (*opt == TCPOPT_NOP)
422adf284a2SGleb Smirnoff 			optlen = 1;
423adf284a2SGleb Smirnoff 		else {
424adf284a2SGleb Smirnoff 			optlen = *(opt + 1);
425adf284a2SGleb Smirnoff 			if (optlen <= 0 || optlen > olen)
426adf284a2SGleb Smirnoff 				break;
427adf284a2SGleb Smirnoff 			if (*opt == TCPOPT_MAXSEG) {
428adf284a2SGleb Smirnoff 				if (optlen != TCPOLEN_MAXSEG)
429adf284a2SGleb Smirnoff 					continue;
430adf284a2SGleb Smirnoff 				mss = (uint16_t *)(opt + 2);
431adf284a2SGleb Smirnoff 				if (ntohs(*mss) > maxmss) {
432adf284a2SGleb Smirnoff 					accumulate = *mss;
433adf284a2SGleb Smirnoff 					*mss = htons(maxmss);
434adf284a2SGleb Smirnoff 					accumulate -= *mss;
435adf284a2SGleb Smirnoff 					if ((flags & CSUM_TCP) == 0)
436adf284a2SGleb Smirnoff 						TCPMSS_ADJUST_CHECKSUM(accumulate, tc->th_sum);
437adf284a2SGleb Smirnoff 					res = 1;
438adf284a2SGleb Smirnoff 				}
439adf284a2SGleb Smirnoff 			}
440adf284a2SGleb Smirnoff 		}
441adf284a2SGleb Smirnoff 	}
442adf284a2SGleb Smirnoff 	return (res);
443adf284a2SGleb Smirnoff }
444