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