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> 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 66adf284a2SGleb Smirnoff /* Per hook info. */ 67adf284a2SGleb Smirnoff typedef struct { 68adf284a2SGleb Smirnoff hook_p outHook; 69adf284a2SGleb Smirnoff struct ng_tcpmss_hookstat stats; 70adf284a2SGleb Smirnoff } *hpriv_p; 71adf284a2SGleb Smirnoff 72adf284a2SGleb Smirnoff /* Netgraph methods. */ 73adf284a2SGleb Smirnoff static ng_constructor_t ng_tcpmss_constructor; 74adf284a2SGleb Smirnoff static ng_rcvmsg_t ng_tcpmss_rcvmsg; 75adf284a2SGleb Smirnoff static ng_newhook_t ng_tcpmss_newhook; 76adf284a2SGleb Smirnoff static ng_rcvdata_t ng_tcpmss_rcvdata; 77adf284a2SGleb Smirnoff static ng_disconnect_t ng_tcpmss_disconnect; 78adf284a2SGleb Smirnoff 79adf284a2SGleb Smirnoff static int correct_mss(struct tcphdr *, int, uint16_t, int); 80adf284a2SGleb Smirnoff 81adf284a2SGleb Smirnoff /* Parse type for struct ng_tcpmss_hookstat. */ 82adf284a2SGleb Smirnoff static const struct ng_parse_struct_field ng_tcpmss_hookstat_type_fields[] 83adf284a2SGleb Smirnoff = NG_TCPMSS_HOOKSTAT_INFO; 84adf284a2SGleb Smirnoff static const struct ng_parse_type ng_tcpmss_hookstat_type = { 85adf284a2SGleb Smirnoff &ng_parse_struct_type, 86adf284a2SGleb Smirnoff &ng_tcpmss_hookstat_type_fields 87adf284a2SGleb Smirnoff }; 88adf284a2SGleb Smirnoff 89adf284a2SGleb Smirnoff /* Parse type for struct ng_tcpmss_config. */ 90adf284a2SGleb Smirnoff static const struct ng_parse_struct_field ng_tcpmss_config_type_fields[] 91adf284a2SGleb Smirnoff = NG_TCPMSS_CONFIG_INFO; 92adf284a2SGleb Smirnoff static const struct ng_parse_type ng_tcpmss_config_type = { 93adf284a2SGleb Smirnoff &ng_parse_struct_type, 94adf284a2SGleb Smirnoff ng_tcpmss_config_type_fields 95adf284a2SGleb Smirnoff }; 96adf284a2SGleb Smirnoff 97adf284a2SGleb Smirnoff /* List of commands and how to convert arguments to/from ASCII. */ 98adf284a2SGleb Smirnoff static const struct ng_cmdlist ng_tcpmss_cmds[] = { 99adf284a2SGleb Smirnoff { 100adf284a2SGleb Smirnoff NGM_TCPMSS_COOKIE, 101adf284a2SGleb Smirnoff NGM_TCPMSS_GET_STATS, 102adf284a2SGleb Smirnoff "getstats", 103adf284a2SGleb Smirnoff &ng_parse_hookbuf_type, 104adf284a2SGleb Smirnoff &ng_tcpmss_hookstat_type 105adf284a2SGleb Smirnoff }, 106adf284a2SGleb Smirnoff { 107adf284a2SGleb Smirnoff NGM_TCPMSS_COOKIE, 108adf284a2SGleb Smirnoff NGM_TCPMSS_CLR_STATS, 109adf284a2SGleb Smirnoff "clrstats", 110adf284a2SGleb Smirnoff &ng_parse_hookbuf_type, 111adf284a2SGleb Smirnoff NULL 112adf284a2SGleb Smirnoff }, 113adf284a2SGleb Smirnoff { 114adf284a2SGleb Smirnoff NGM_TCPMSS_COOKIE, 115adf284a2SGleb Smirnoff NGM_TCPMSS_GETCLR_STATS, 116adf284a2SGleb Smirnoff "getclrstats", 117adf284a2SGleb Smirnoff &ng_parse_hookbuf_type, 118adf284a2SGleb Smirnoff &ng_tcpmss_hookstat_type 119adf284a2SGleb Smirnoff }, 120adf284a2SGleb Smirnoff { 121adf284a2SGleb Smirnoff NGM_TCPMSS_COOKIE, 122adf284a2SGleb Smirnoff NGM_TCPMSS_CONFIG, 123adf284a2SGleb Smirnoff "config", 124adf284a2SGleb Smirnoff &ng_tcpmss_config_type, 125adf284a2SGleb Smirnoff NULL 126adf284a2SGleb Smirnoff }, 127adf284a2SGleb Smirnoff { 0 } 128adf284a2SGleb Smirnoff }; 129adf284a2SGleb Smirnoff 130adf284a2SGleb Smirnoff /* Netgraph type descriptor. */ 131adf284a2SGleb Smirnoff static struct ng_type ng_tcpmss_typestruct = { 132adf284a2SGleb Smirnoff .version = NG_ABI_VERSION, 133adf284a2SGleb Smirnoff .name = NG_TCPMSS_NODE_TYPE, 134adf284a2SGleb Smirnoff .constructor = ng_tcpmss_constructor, 135adf284a2SGleb Smirnoff .rcvmsg = ng_tcpmss_rcvmsg, 136adf284a2SGleb Smirnoff .newhook = ng_tcpmss_newhook, 137adf284a2SGleb Smirnoff .rcvdata = ng_tcpmss_rcvdata, 138adf284a2SGleb Smirnoff .disconnect = ng_tcpmss_disconnect, 139adf284a2SGleb Smirnoff .cmdlist = ng_tcpmss_cmds, 140adf284a2SGleb Smirnoff }; 141adf284a2SGleb Smirnoff 142adf284a2SGleb Smirnoff NETGRAPH_INIT(tcpmss, &ng_tcpmss_typestruct); 143adf284a2SGleb Smirnoff 144adf284a2SGleb Smirnoff #define ERROUT(x) { error = (x); goto done; } 145adf284a2SGleb Smirnoff 146adf284a2SGleb Smirnoff /* 147adf284a2SGleb Smirnoff * Node constructor. No special actions required. 148adf284a2SGleb Smirnoff */ 149adf284a2SGleb Smirnoff static int 150adf284a2SGleb Smirnoff ng_tcpmss_constructor(node_p node) 151adf284a2SGleb Smirnoff { 152adf284a2SGleb Smirnoff return (0); 153adf284a2SGleb Smirnoff } 154adf284a2SGleb Smirnoff 155adf284a2SGleb Smirnoff /* 156adf284a2SGleb Smirnoff * Add a hook. Any unique name is OK. 157adf284a2SGleb Smirnoff */ 158adf284a2SGleb Smirnoff static int 159adf284a2SGleb Smirnoff ng_tcpmss_newhook(node_p node, hook_p hook, const char *name) 160adf284a2SGleb Smirnoff { 161adf284a2SGleb Smirnoff hpriv_p priv; 162adf284a2SGleb Smirnoff 1631ede983cSDag-Erling Smørgrav priv = malloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO); 164adf284a2SGleb Smirnoff if (priv == NULL) 165adf284a2SGleb Smirnoff return (ENOMEM); 166adf284a2SGleb Smirnoff 167adf284a2SGleb Smirnoff NG_HOOK_SET_PRIVATE(hook, priv); 168adf284a2SGleb Smirnoff 169adf284a2SGleb Smirnoff return (0); 170adf284a2SGleb Smirnoff } 171adf284a2SGleb Smirnoff 172adf284a2SGleb Smirnoff /* 173adf284a2SGleb Smirnoff * Receive a control message. 174adf284a2SGleb Smirnoff */ 175adf284a2SGleb Smirnoff static int 176adf284a2SGleb Smirnoff ng_tcpmss_rcvmsg 177adf284a2SGleb Smirnoff (node_p node, item_p item, hook_p lasthook) 178adf284a2SGleb Smirnoff { 179adf284a2SGleb Smirnoff struct ng_mesg *msg, *resp = NULL; 180adf284a2SGleb Smirnoff int error = 0; 181adf284a2SGleb Smirnoff 182adf284a2SGleb Smirnoff NGI_GET_MSG(item, msg); 183adf284a2SGleb Smirnoff 184adf284a2SGleb Smirnoff switch (msg->header.typecookie) { 185adf284a2SGleb Smirnoff case NGM_TCPMSS_COOKIE: 186adf284a2SGleb Smirnoff switch (msg->header.cmd) { 187adf284a2SGleb Smirnoff case NGM_TCPMSS_GET_STATS: 188adf284a2SGleb Smirnoff case NGM_TCPMSS_CLR_STATS: 189adf284a2SGleb Smirnoff case NGM_TCPMSS_GETCLR_STATS: 190adf284a2SGleb Smirnoff { 191adf284a2SGleb Smirnoff hook_p hook; 192adf284a2SGleb Smirnoff hpriv_p priv; 193adf284a2SGleb Smirnoff 194adf284a2SGleb Smirnoff /* Check that message is long enough. */ 195adf284a2SGleb Smirnoff if (msg->header.arglen != NG_HOOKSIZ) 196adf284a2SGleb Smirnoff ERROUT(EINVAL); 197adf284a2SGleb Smirnoff 198adf284a2SGleb Smirnoff /* Find this hook. */ 199adf284a2SGleb Smirnoff hook = ng_findhook(node, (char *)msg->data); 200adf284a2SGleb Smirnoff if (hook == NULL) 201adf284a2SGleb Smirnoff ERROUT(ENOENT); 202adf284a2SGleb Smirnoff 203adf284a2SGleb Smirnoff priv = NG_HOOK_PRIVATE(hook); 204adf284a2SGleb Smirnoff 205adf284a2SGleb Smirnoff /* Create response. */ 206adf284a2SGleb Smirnoff if (msg->header.cmd != NGM_TCPMSS_CLR_STATS) { 207adf284a2SGleb Smirnoff NG_MKRESPONSE(resp, msg, 208adf284a2SGleb Smirnoff sizeof(struct ng_tcpmss_hookstat), M_NOWAIT); 209adf284a2SGleb Smirnoff if (resp == NULL) 210adf284a2SGleb Smirnoff ERROUT(ENOMEM); 211adf284a2SGleb Smirnoff bcopy(&priv->stats, resp->data, 212adf284a2SGleb Smirnoff sizeof(struct ng_tcpmss_hookstat)); 213adf284a2SGleb Smirnoff } 214adf284a2SGleb Smirnoff 215adf284a2SGleb Smirnoff if (msg->header.cmd != NGM_TCPMSS_GET_STATS) 216adf284a2SGleb Smirnoff bzero(&priv->stats, 217adf284a2SGleb Smirnoff sizeof(struct ng_tcpmss_hookstat)); 218adf284a2SGleb Smirnoff break; 219adf284a2SGleb Smirnoff } 220adf284a2SGleb Smirnoff case NGM_TCPMSS_CONFIG: 221adf284a2SGleb Smirnoff { 222adf284a2SGleb Smirnoff struct ng_tcpmss_config *set; 223adf284a2SGleb Smirnoff hook_p in, out; 224adf284a2SGleb Smirnoff hpriv_p priv; 225adf284a2SGleb Smirnoff 226adf284a2SGleb Smirnoff /* Check that message is long enough. */ 227adf284a2SGleb Smirnoff if (msg->header.arglen != 228adf284a2SGleb Smirnoff sizeof(struct ng_tcpmss_config)) 229adf284a2SGleb Smirnoff ERROUT(EINVAL); 230adf284a2SGleb Smirnoff 231adf284a2SGleb Smirnoff set = (struct ng_tcpmss_config *)msg->data; 232adf284a2SGleb Smirnoff in = ng_findhook(node, set->inHook); 233adf284a2SGleb Smirnoff out = ng_findhook(node, set->outHook); 234adf284a2SGleb Smirnoff if (in == NULL || out == NULL) 235adf284a2SGleb Smirnoff ERROUT(ENOENT); 236adf284a2SGleb Smirnoff 237adf284a2SGleb Smirnoff /* Configure MSS hack. */ 238adf284a2SGleb Smirnoff priv = NG_HOOK_PRIVATE(in); 239adf284a2SGleb Smirnoff priv->outHook = out; 240adf284a2SGleb Smirnoff priv->stats.maxMSS = set->maxMSS; 241adf284a2SGleb Smirnoff 242adf284a2SGleb Smirnoff break; 243adf284a2SGleb Smirnoff } 244adf284a2SGleb Smirnoff default: 245adf284a2SGleb Smirnoff error = EINVAL; 246adf284a2SGleb Smirnoff break; 247adf284a2SGleb Smirnoff } 248adf284a2SGleb Smirnoff break; 249adf284a2SGleb Smirnoff default: 250adf284a2SGleb Smirnoff error = EINVAL; 251adf284a2SGleb Smirnoff break; 252adf284a2SGleb Smirnoff } 253adf284a2SGleb Smirnoff 254adf284a2SGleb Smirnoff done: 255adf284a2SGleb Smirnoff NG_RESPOND_MSG(error, node, item, resp); 256adf284a2SGleb Smirnoff NG_FREE_MSG(msg); 257adf284a2SGleb Smirnoff 258adf284a2SGleb Smirnoff return (error); 259adf284a2SGleb Smirnoff } 260adf284a2SGleb Smirnoff 261adf284a2SGleb Smirnoff /* 262adf284a2SGleb Smirnoff * Receive data on a hook, and hack MSS. 263adf284a2SGleb Smirnoff * 264adf284a2SGleb Smirnoff */ 265adf284a2SGleb Smirnoff static int 266adf284a2SGleb Smirnoff ng_tcpmss_rcvdata(hook_p hook, item_p item) 267adf284a2SGleb Smirnoff { 268adf284a2SGleb Smirnoff hpriv_p priv = NG_HOOK_PRIVATE(hook); 269adf284a2SGleb Smirnoff struct mbuf *m = NULL; 270adf284a2SGleb Smirnoff struct ip *ip; 271adf284a2SGleb Smirnoff struct tcphdr *tcp; 272adf284a2SGleb Smirnoff int iphlen, tcphlen, pktlen; 273adf284a2SGleb Smirnoff int pullup_len = 0; 274adf284a2SGleb Smirnoff int error = 0; 275adf284a2SGleb Smirnoff 276adf284a2SGleb Smirnoff /* Drop packets if filter is not configured on this hook. */ 277adf284a2SGleb Smirnoff if (priv->outHook == NULL) 278adf284a2SGleb Smirnoff goto done; 279adf284a2SGleb Smirnoff 280adf284a2SGleb Smirnoff NGI_GET_M(item, m); 281adf284a2SGleb Smirnoff 282adf284a2SGleb Smirnoff /* Update stats on incoming hook. */ 283adf284a2SGleb Smirnoff pktlen = m->m_pkthdr.len; 284adf284a2SGleb Smirnoff priv->stats.Octets += pktlen; 285adf284a2SGleb Smirnoff priv->stats.Packets++; 286adf284a2SGleb Smirnoff 287adf284a2SGleb Smirnoff /* Check whether we configured to fix MSS. */ 288adf284a2SGleb Smirnoff if (priv->stats.maxMSS == 0) 289adf284a2SGleb Smirnoff goto send; 290adf284a2SGleb Smirnoff 291adf284a2SGleb Smirnoff #define M_CHECK(length) do { \ 292adf284a2SGleb Smirnoff pullup_len += length; \ 293e6da342bSGleb Smirnoff if ((m)->m_pkthdr.len < pullup_len) \ 294adf284a2SGleb Smirnoff goto send; \ 295e6da342bSGleb Smirnoff if ((m)->m_len < pullup_len && \ 296e6da342bSGleb Smirnoff (((m) = m_pullup((m), pullup_len)) == NULL)) \ 297adf284a2SGleb Smirnoff ERROUT(ENOBUFS); \ 298adf284a2SGleb Smirnoff } while (0) 299adf284a2SGleb Smirnoff 300adf284a2SGleb Smirnoff /* Check mbuf packet size and arrange for IP header. */ 301adf284a2SGleb Smirnoff M_CHECK(sizeof(struct ip)); 302adf284a2SGleb Smirnoff ip = mtod(m, struct ip *); 303adf284a2SGleb Smirnoff 304adf284a2SGleb Smirnoff /* Check IP version. */ 305adf284a2SGleb Smirnoff if (ip->ip_v != IPVERSION) 306adf284a2SGleb Smirnoff ERROUT(EINVAL); 307adf284a2SGleb Smirnoff 308adf284a2SGleb Smirnoff /* Check IP header length. */ 309adf284a2SGleb Smirnoff iphlen = ip->ip_hl << 2; 310adf284a2SGleb Smirnoff if (iphlen < sizeof(struct ip) || iphlen > pktlen ) 311adf284a2SGleb Smirnoff ERROUT(EINVAL); 312adf284a2SGleb Smirnoff 313adf284a2SGleb Smirnoff /* Check if it is TCP. */ 314adf284a2SGleb Smirnoff if (!(ip->ip_p == IPPROTO_TCP)) 315adf284a2SGleb Smirnoff goto send; 316adf284a2SGleb Smirnoff 317adf284a2SGleb Smirnoff /* Check mbuf packet size and arrange for IP+TCP header */ 318e6da342bSGleb Smirnoff M_CHECK(iphlen - sizeof(struct ip) + sizeof(struct tcphdr)); 319bc12a093SGleb Smirnoff ip = mtod(m, struct ip *); 320adf284a2SGleb Smirnoff tcp = (struct tcphdr *)((caddr_t )ip + iphlen); 321adf284a2SGleb Smirnoff 322adf284a2SGleb Smirnoff /* Check TCP header length. */ 323adf284a2SGleb Smirnoff tcphlen = tcp->th_off << 2; 324adf284a2SGleb Smirnoff if (tcphlen < sizeof(struct tcphdr) || tcphlen > pktlen - iphlen) 325adf284a2SGleb Smirnoff ERROUT(EINVAL); 326adf284a2SGleb Smirnoff 327adf284a2SGleb Smirnoff /* Check SYN packet and has options. */ 328adf284a2SGleb Smirnoff if (!(tcp->th_flags & TH_SYN) || tcphlen == sizeof(struct tcphdr)) 329adf284a2SGleb Smirnoff goto send; 330adf284a2SGleb Smirnoff 331adf284a2SGleb Smirnoff /* Update SYN stats. */ 332adf284a2SGleb Smirnoff priv->stats.SYNPkts++; 333adf284a2SGleb Smirnoff 334e6da342bSGleb Smirnoff M_CHECK(tcphlen - sizeof(struct tcphdr)); 335bc12a093SGleb Smirnoff ip = mtod(m, struct ip *); 336bc12a093SGleb Smirnoff tcp = (struct tcphdr *)((caddr_t )ip + iphlen); 337adf284a2SGleb Smirnoff 338adf284a2SGleb Smirnoff #undef M_CHECK 339adf284a2SGleb Smirnoff 340adf284a2SGleb Smirnoff /* Fix MSS and update stats. */ 341adf284a2SGleb Smirnoff if (correct_mss(tcp, tcphlen, priv->stats.maxMSS, 342adf284a2SGleb Smirnoff m->m_pkthdr.csum_flags)) 343adf284a2SGleb Smirnoff priv->stats.FixedPkts++; 344adf284a2SGleb Smirnoff 345adf284a2SGleb Smirnoff send: 346adf284a2SGleb Smirnoff /* Deliver frame out destination hook. */ 347e6da342bSGleb Smirnoff NG_FWD_NEW_DATA(error, item, priv->outHook, m); 348adf284a2SGleb Smirnoff 349adf284a2SGleb Smirnoff return (error); 350adf284a2SGleb Smirnoff 351adf284a2SGleb Smirnoff done: 352adf284a2SGleb Smirnoff NG_FREE_ITEM(item); 353adf284a2SGleb Smirnoff NG_FREE_M(m); 354adf284a2SGleb Smirnoff 355adf284a2SGleb Smirnoff return (error); 356adf284a2SGleb Smirnoff } 357adf284a2SGleb Smirnoff 358adf284a2SGleb Smirnoff /* 359adf284a2SGleb Smirnoff * Hook disconnection. 360adf284a2SGleb Smirnoff * We must check all hooks, since they may reference this one. 361adf284a2SGleb Smirnoff */ 362adf284a2SGleb Smirnoff static int 363adf284a2SGleb Smirnoff ng_tcpmss_disconnect(hook_p hook) 364adf284a2SGleb Smirnoff { 365adf284a2SGleb Smirnoff node_p node = NG_HOOK_NODE(hook); 366adf284a2SGleb Smirnoff hook_p hook2; 367adf284a2SGleb Smirnoff 368adf284a2SGleb Smirnoff LIST_FOREACH(hook2, &node->nd_hooks, hk_hooks) { 369adf284a2SGleb Smirnoff hpriv_p priv = NG_HOOK_PRIVATE(hook2); 370adf284a2SGleb Smirnoff 371adf284a2SGleb Smirnoff if (priv->outHook == hook) 372adf284a2SGleb Smirnoff priv->outHook = NULL; 373adf284a2SGleb Smirnoff } 374adf284a2SGleb Smirnoff 3751ede983cSDag-Erling Smørgrav free(NG_HOOK_PRIVATE(hook), M_NETGRAPH); 376df7e759cSGleb Smirnoff 377adf284a2SGleb Smirnoff if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) 378adf284a2SGleb Smirnoff ng_rmnode_self(NG_HOOK_NODE(hook)); 379adf284a2SGleb Smirnoff 380adf284a2SGleb Smirnoff return (0); 381adf284a2SGleb Smirnoff } 382adf284a2SGleb Smirnoff 383adf284a2SGleb Smirnoff /* 384adf284a2SGleb Smirnoff * Code from tcpmssd. 385adf284a2SGleb Smirnoff */ 386adf284a2SGleb Smirnoff 387adf284a2SGleb Smirnoff /*- 388adf284a2SGleb Smirnoff * The following macro is used to update an 389adf284a2SGleb Smirnoff * internet checksum. "acc" is a 32-bit 390adf284a2SGleb Smirnoff * accumulation of all the changes to the 391adf284a2SGleb Smirnoff * checksum (adding in old 16-bit words and 392adf284a2SGleb Smirnoff * subtracting out new words), and "cksum" 393adf284a2SGleb Smirnoff * is the checksum value to be updated. 394adf284a2SGleb Smirnoff */ 395adf284a2SGleb Smirnoff #define TCPMSS_ADJUST_CHECKSUM(acc, cksum) do { \ 396adf284a2SGleb Smirnoff acc += cksum; \ 397adf284a2SGleb Smirnoff if (acc < 0) { \ 398adf284a2SGleb Smirnoff acc = -acc; \ 399adf284a2SGleb Smirnoff acc = (acc >> 16) + (acc & 0xffff); \ 400adf284a2SGleb Smirnoff acc += acc >> 16; \ 401adf284a2SGleb Smirnoff cksum = (u_short) ~acc; \ 402adf284a2SGleb Smirnoff } else { \ 403adf284a2SGleb Smirnoff acc = (acc >> 16) + (acc & 0xffff); \ 404adf284a2SGleb Smirnoff acc += acc >> 16; \ 405adf284a2SGleb Smirnoff cksum = (u_short) acc; \ 406adf284a2SGleb Smirnoff } \ 407adf284a2SGleb Smirnoff } while (0); 408adf284a2SGleb Smirnoff 409adf284a2SGleb Smirnoff static int 410adf284a2SGleb Smirnoff correct_mss(struct tcphdr *tc, int hlen, uint16_t maxmss, int flags) 411adf284a2SGleb Smirnoff { 412adf284a2SGleb Smirnoff int olen, optlen; 413adf284a2SGleb Smirnoff u_char *opt; 414adf284a2SGleb Smirnoff int accumulate; 415adf284a2SGleb Smirnoff int res = 0; 4167937d24bSAlexander Motin uint16_t sum; 417adf284a2SGleb Smirnoff 418adf284a2SGleb Smirnoff for (olen = hlen - sizeof(struct tcphdr), opt = (u_char *)(tc + 1); 419adf284a2SGleb Smirnoff olen > 0; olen -= optlen, opt += optlen) { 420adf284a2SGleb Smirnoff if (*opt == TCPOPT_EOL) 421adf284a2SGleb Smirnoff break; 422adf284a2SGleb Smirnoff else if (*opt == TCPOPT_NOP) 423adf284a2SGleb Smirnoff optlen = 1; 424adf284a2SGleb Smirnoff else { 425adf284a2SGleb Smirnoff optlen = *(opt + 1); 426adf284a2SGleb Smirnoff if (optlen <= 0 || optlen > olen) 427adf284a2SGleb Smirnoff break; 428adf284a2SGleb Smirnoff if (*opt == TCPOPT_MAXSEG) { 429adf284a2SGleb Smirnoff if (optlen != TCPOLEN_MAXSEG) 430adf284a2SGleb Smirnoff continue; 4317937d24bSAlexander Motin accumulate = be16dec(opt + 2); 4327937d24bSAlexander Motin if (accumulate > maxmss) { 4337937d24bSAlexander Motin if ((flags & CSUM_TCP) == 0) { 4347937d24bSAlexander Motin accumulate -= maxmss; 4357937d24bSAlexander Motin sum = be16dec(&tc->th_sum); 4367937d24bSAlexander Motin TCPMSS_ADJUST_CHECKSUM(accumulate, sum); 4377937d24bSAlexander Motin be16enc(&tc->th_sum, sum); 4387937d24bSAlexander Motin } 4397937d24bSAlexander Motin be16enc(opt + 2, maxmss); 440adf284a2SGleb Smirnoff res = 1; 441adf284a2SGleb Smirnoff } 442adf284a2SGleb Smirnoff } 443adf284a2SGleb Smirnoff } 444adf284a2SGleb Smirnoff } 445adf284a2SGleb Smirnoff return (res); 446adf284a2SGleb Smirnoff } 447