Index: stable/8/sys/amd64/include/xen =================================================================== --- stable/8/sys/amd64/include/xen (revision 206660) +++ stable/8/sys/amd64/include/xen (revision 206661) Property changes on: stable/8/sys/amd64/include/xen ___________________________________________________________________ Modified: svn:mergeinfo ## -0,0 +0,1 ## Merged /head/sys/amd64/include/xen:r206021,206032,206049-206050 Index: stable/8/sys/cddl/contrib/opensolaris =================================================================== --- stable/8/sys/cddl/contrib/opensolaris (revision 206660) +++ stable/8/sys/cddl/contrib/opensolaris (revision 206661) Property changes on: stable/8/sys/cddl/contrib/opensolaris ___________________________________________________________________ Modified: svn:mergeinfo ## -0,0 +0,1 ## Merged /head/sys/cddl/contrib/opensolaris:r206021,206032,206049-206050 Index: stable/8/sys/contrib/dev/acpica =================================================================== --- stable/8/sys/contrib/dev/acpica (revision 206660) +++ stable/8/sys/contrib/dev/acpica (revision 206661) Property changes on: stable/8/sys/contrib/dev/acpica ___________________________________________________________________ Modified: svn:mergeinfo ## -0,0 +0,1 ## Merged /head/sys/contrib/dev/acpica:r206021,206032,206049-206050 Index: stable/8/sys/contrib/pf =================================================================== --- stable/8/sys/contrib/pf (revision 206660) +++ stable/8/sys/contrib/pf (revision 206661) Property changes on: stable/8/sys/contrib/pf ___________________________________________________________________ Modified: svn:mergeinfo ## -0,0 +0,1 ## Merged /head/sys/contrib/pf:r206021,206032,206049-206050 Index: stable/8/sys/dev/xen/xenpci =================================================================== --- stable/8/sys/dev/xen/xenpci (revision 206660) +++ stable/8/sys/dev/xen/xenpci (revision 206661) Property changes on: stable/8/sys/dev/xen/xenpci ___________________________________________________________________ Modified: svn:mergeinfo ## -0,0 +0,1 ## Merged /head/sys/dev/xen/xenpci:r206021,206032,206049-206050 Index: stable/8/sys/netgraph/ng_deflate.c =================================================================== --- stable/8/sys/netgraph/ng_deflate.c (revision 206660) +++ stable/8/sys/netgraph/ng_deflate.c (revision 206661) @@ -1,697 +1,698 @@ /*- * Copyright (c) 2006 Alexander Motin * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice unmodified, this list of conditions, and the following * disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ /* * Deflate PPP compression netgraph node type. */ #include #include #include #include #include +#include #include #include #include #include #include #include #include #include "opt_netgraph.h" MALLOC_DEFINE(M_NETGRAPH_DEFLATE, "netgraph_deflate", "netgraph deflate node "); /* DEFLATE header length */ #define DEFLATE_HDRLEN 2 #define PROT_COMPD 0x00fd #define DEFLATE_BUF_SIZE 4096 /* Node private data */ struct ng_deflate_private { struct ng_deflate_config cfg; /* configuration */ u_char inbuf[DEFLATE_BUF_SIZE]; /* input buffer */ u_char outbuf[DEFLATE_BUF_SIZE]; /* output buffer */ z_stream cx; /* compression context */ struct ng_deflate_stats stats; /* statistics */ ng_ID_t ctrlnode; /* path to controlling node */ uint16_t seqnum; /* sequence number */ u_char compress; /* compress/decompress flag */ }; typedef struct ng_deflate_private *priv_p; /* Netgraph node methods */ static ng_constructor_t ng_deflate_constructor; static ng_rcvmsg_t ng_deflate_rcvmsg; static ng_shutdown_t ng_deflate_shutdown; static ng_newhook_t ng_deflate_newhook; static ng_rcvdata_t ng_deflate_rcvdata; static ng_disconnect_t ng_deflate_disconnect; /* Helper functions */ static void *z_alloc(void *, u_int items, u_int size); static void z_free(void *, void *ptr); static int ng_deflate_compress(node_p node, struct mbuf *m, struct mbuf **resultp); static int ng_deflate_decompress(node_p node, struct mbuf *m, struct mbuf **resultp); static void ng_deflate_reset_req(node_p node); /* Parse type for struct ng_deflate_config. */ static const struct ng_parse_struct_field ng_deflate_config_type_fields[] = NG_DEFLATE_CONFIG_INFO; static const struct ng_parse_type ng_deflate_config_type = { &ng_parse_struct_type, ng_deflate_config_type_fields }; /* Parse type for struct ng_deflate_stat. */ static const struct ng_parse_struct_field ng_deflate_stats_type_fields[] = NG_DEFLATE_STATS_INFO; static const struct ng_parse_type ng_deflate_stat_type = { &ng_parse_struct_type, ng_deflate_stats_type_fields }; /* List of commands and how to convert arguments to/from ASCII. */ static const struct ng_cmdlist ng_deflate_cmds[] = { { NGM_DEFLATE_COOKIE, NGM_DEFLATE_CONFIG, "config", &ng_deflate_config_type, NULL }, { NGM_DEFLATE_COOKIE, NGM_DEFLATE_RESETREQ, "resetreq", NULL, NULL }, { NGM_DEFLATE_COOKIE, NGM_DEFLATE_GET_STATS, "getstats", NULL, &ng_deflate_stat_type }, { NGM_DEFLATE_COOKIE, NGM_DEFLATE_CLR_STATS, "clrstats", NULL, NULL }, { NGM_DEFLATE_COOKIE, NGM_DEFLATE_GETCLR_STATS, "getclrstats", NULL, &ng_deflate_stat_type }, { 0 } }; /* Node type descriptor */ static struct ng_type ng_deflate_typestruct = { .version = NG_ABI_VERSION, .name = NG_DEFLATE_NODE_TYPE, .constructor = ng_deflate_constructor, .rcvmsg = ng_deflate_rcvmsg, .shutdown = ng_deflate_shutdown, .newhook = ng_deflate_newhook, .rcvdata = ng_deflate_rcvdata, .disconnect = ng_deflate_disconnect, .cmdlist = ng_deflate_cmds, }; NETGRAPH_INIT(deflate, &ng_deflate_typestruct); /* Depend on separate zlib module. */ MODULE_DEPEND(ng_deflate, zlib, 1, 1, 1); #define ERROUT(x) do { error = (x); goto done; } while (0) /************************************************************************ NETGRAPH NODE STUFF ************************************************************************/ /* * Node type constructor */ static int ng_deflate_constructor(node_p node) { priv_p priv; /* Allocate private structure. */ priv = malloc(sizeof(*priv), M_NETGRAPH_DEFLATE, M_WAITOK | M_ZERO); NG_NODE_SET_PRIVATE(node, priv); /* This node is not thread safe. */ NG_NODE_FORCE_WRITER(node); /* Done */ return (0); } /* * Give our OK for a hook to be added. */ static int ng_deflate_newhook(node_p node, hook_p hook, const char *name) { const priv_p priv = NG_NODE_PRIVATE(node); if (NG_NODE_NUMHOOKS(node) > 0) return (EINVAL); if (strcmp(name, NG_DEFLATE_HOOK_COMP) == 0) priv->compress = 1; else if (strcmp(name, NG_DEFLATE_HOOK_DECOMP) == 0) priv->compress = 0; else return (EINVAL); return (0); } /* * Receive a control message */ static int ng_deflate_rcvmsg(node_p node, item_p item, hook_p lasthook) { const priv_p priv = NG_NODE_PRIVATE(node); struct ng_mesg *resp = NULL; int error = 0; struct ng_mesg *msg; NGI_GET_MSG(item, msg); if (msg->header.typecookie != NGM_DEFLATE_COOKIE) ERROUT(EINVAL); switch (msg->header.cmd) { case NGM_DEFLATE_CONFIG: { struct ng_deflate_config *const cfg = (struct ng_deflate_config *)msg->data; /* Check configuration. */ if (msg->header.arglen != sizeof(*cfg)) ERROUT(EINVAL); if (cfg->enable) { if (cfg->windowBits < 8 || cfg->windowBits > 15) ERROUT(EINVAL); } else cfg->windowBits = 0; /* Clear previous state. */ if (priv->cfg.enable) { if (priv->compress) deflateEnd(&priv->cx); else inflateEnd(&priv->cx); priv->cfg.enable = 0; } /* Configuration is OK, reset to it. */ priv->cfg = *cfg; if (priv->cfg.enable) { priv->cx.next_in = NULL; priv->cx.zalloc = z_alloc; priv->cx.zfree = z_free; int res; if (priv->compress) { if ((res = deflateInit2(&priv->cx, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -cfg->windowBits, 8, Z_DEFAULT_STRATEGY)) != Z_OK) { log(LOG_NOTICE, "deflateInit2: error %d, %s\n", res, priv->cx.msg); priv->cfg.enable = 0; ERROUT(ENOMEM); } } else { if ((res = inflateInit2(&priv->cx, -cfg->windowBits)) != Z_OK) { log(LOG_NOTICE, "inflateInit2: error %d, %s\n", res, priv->cx.msg); priv->cfg.enable = 0; ERROUT(ENOMEM); } } } /* Initialize other state. */ priv->seqnum = 0; /* Save return address so we can send reset-req's */ priv->ctrlnode = NGI_RETADDR(item); break; } case NGM_DEFLATE_RESETREQ: ng_deflate_reset_req(node); break; case NGM_DEFLATE_GET_STATS: case NGM_DEFLATE_CLR_STATS: case NGM_DEFLATE_GETCLR_STATS: /* Create response if requested. */ if (msg->header.cmd != NGM_DEFLATE_CLR_STATS) { NG_MKRESPONSE(resp, msg, sizeof(struct ng_deflate_stats), M_NOWAIT); if (resp == NULL) ERROUT(ENOMEM); bcopy(&priv->stats, resp->data, sizeof(struct ng_deflate_stats)); } /* Clear stats if requested. */ if (msg->header.cmd != NGM_DEFLATE_GET_STATS) bzero(&priv->stats, sizeof(struct ng_deflate_stats)); break; default: error = EINVAL; break; } done: NG_RESPOND_MSG(error, node, item, resp); NG_FREE_MSG(msg); return (error); } /* * Receive incoming data on our hook. */ static int ng_deflate_rcvdata(hook_p hook, item_p item) { const node_p node = NG_HOOK_NODE(hook); const priv_p priv = NG_NODE_PRIVATE(node); struct mbuf *m, *out; int error; if (!priv->cfg.enable) { NG_FREE_ITEM(item); return (ENXIO); } NGI_GET_M(item, m); /* Compress */ if (priv->compress) { if ((error = ng_deflate_compress(node, m, &out)) != 0) { NG_FREE_ITEM(item); log(LOG_NOTICE, "%s: error: %d\n", __func__, error); return (error); } } else { /* Decompress */ if ((error = ng_deflate_decompress(node, m, &out)) != 0) { NG_FREE_ITEM(item); log(LOG_NOTICE, "%s: error: %d\n", __func__, error); if (priv->ctrlnode != 0) { struct ng_mesg *msg; /* Need to send a reset-request. */ NG_MKMESSAGE(msg, NGM_DEFLATE_COOKIE, NGM_DEFLATE_RESETREQ, 0, M_NOWAIT); if (msg == NULL) return (error); NG_SEND_MSG_ID(error, node, msg, priv->ctrlnode, 0); } return (error); } } NG_FWD_NEW_DATA(error, item, hook, out); return (error); } /* * Destroy node. */ static int ng_deflate_shutdown(node_p node) { const priv_p priv = NG_NODE_PRIVATE(node); /* Take down netgraph node. */ if (priv->cfg.enable) { if (priv->compress) deflateEnd(&priv->cx); else inflateEnd(&priv->cx); } free(priv, M_NETGRAPH_DEFLATE); NG_NODE_SET_PRIVATE(node, NULL); NG_NODE_UNREF(node); /* let the node escape */ return (0); } /* * Hook disconnection */ static int ng_deflate_disconnect(hook_p hook) { const node_p node = NG_HOOK_NODE(hook); const priv_p priv = NG_NODE_PRIVATE(node); if (priv->cfg.enable) { if (priv->compress) deflateEnd(&priv->cx); else inflateEnd(&priv->cx); priv->cfg.enable = 0; } /* Go away if no longer connected. */ if ((NG_NODE_NUMHOOKS(node) == 0) && NG_NODE_IS_VALID(node)) ng_rmnode_self(node); return (0); } /************************************************************************ HELPER STUFF ************************************************************************/ /* * Space allocation and freeing routines for use by zlib routines. */ static void * z_alloc(void *notused, u_int items, u_int size) { return (malloc(items * size, M_NETGRAPH_DEFLATE, M_NOWAIT)); } static void z_free(void *notused, void *ptr) { free(ptr, M_NETGRAPH_DEFLATE); } /* * Compress/encrypt a packet and put the result in a new mbuf at *resultp. * The original mbuf is not free'd. */ static int ng_deflate_compress(node_p node, struct mbuf *m, struct mbuf **resultp) { const priv_p priv = NG_NODE_PRIVATE(node); int outlen, inlen; int rtn; /* Initialize. */ *resultp = NULL; inlen = m->m_pkthdr.len; priv->stats.FramesPlain++; priv->stats.InOctets+=inlen; if (inlen > DEFLATE_BUF_SIZE) { priv->stats.Errors++; NG_FREE_M(m); return (ENOMEM); } /* We must own the mbuf chain exclusively to modify it. */ m = m_unshare(m, M_DONTWAIT); if (m == NULL) { priv->stats.Errors++; return (ENOMEM); } /* Work with contiguous regions of memory. */ m_copydata(m, 0, inlen, (caddr_t)priv->inbuf); outlen = DEFLATE_BUF_SIZE; /* Compress "inbuf" into "outbuf". */ /* Prepare to compress. */ if (priv->inbuf[0] != 0) { priv->cx.next_in = priv->inbuf; priv->cx.avail_in = inlen; } else { priv->cx.next_in = priv->inbuf + 1; /* compress protocol */ priv->cx.avail_in = inlen - 1; } priv->cx.next_out = priv->outbuf + 2 + DEFLATE_HDRLEN; priv->cx.avail_out = outlen - 2 - DEFLATE_HDRLEN; /* Compress. */ rtn = deflate(&priv->cx, Z_PACKET_FLUSH); /* Check return value. */ if (rtn != Z_OK) { priv->stats.Errors++; log(LOG_NOTICE, "ng_deflate: compression error: %d (%s)\n", rtn, priv->cx.msg); NG_FREE_M(m); return (EINVAL); } /* Calculate resulting size. */ outlen -= priv->cx.avail_out; /* If we can't compress this packet, send it as-is. */ if (outlen > inlen) { /* Return original packet uncompressed. */ *resultp = m; priv->stats.FramesUncomp++; priv->stats.OutOctets+=inlen; } else { /* Install header. */ - ((u_int16_t *)priv->outbuf)[0] = htons(PROT_COMPD); - ((u_int16_t *)priv->outbuf)[1] = htons(priv->seqnum); + be16enc(priv->outbuf, PROT_COMPD); + be16enc(priv->outbuf + 2, priv->seqnum); /* Return packet in an mbuf. */ m_copyback(m, 0, outlen, (caddr_t)priv->outbuf); if (m->m_pkthdr.len < outlen) { m_freem(m); priv->stats.Errors++; return (ENOMEM); } else if (outlen < m->m_pkthdr.len) m_adj(m, outlen - m->m_pkthdr.len); *resultp = m; priv->stats.FramesComp++; priv->stats.OutOctets+=outlen; } /* Update sequence number. */ priv->seqnum++; return (0); } /* * Decompress/decrypt packet and put the result in a new mbuf at *resultp. * The original mbuf is not free'd. */ static int ng_deflate_decompress(node_p node, struct mbuf *m, struct mbuf **resultp) { const priv_p priv = NG_NODE_PRIVATE(node); int outlen, inlen; int rtn; uint16_t proto; int offset; uint16_t rseqnum; /* Initialize. */ *resultp = NULL; inlen = m->m_pkthdr.len; if (inlen > DEFLATE_BUF_SIZE) { priv->stats.Errors++; NG_FREE_M(m); priv->seqnum = 0; return (ENOMEM); } /* We must own the mbuf chain exclusively to modify it. */ m = m_unshare(m, M_DONTWAIT); if (m == NULL) { priv->stats.Errors++; return (ENOMEM); } /* Work with contiguous regions of memory. */ m_copydata(m, 0, inlen, (caddr_t)priv->inbuf); /* Separate proto. */ if ((priv->inbuf[0] & 0x01) != 0) { proto = priv->inbuf[0]; offset = 1; } else { - proto = ntohs(((uint16_t *)priv->inbuf)[0]); + proto = be16dec(priv->inbuf); offset = 2; } priv->stats.InOctets += inlen; /* Packet is compressed, so decompress. */ if (proto == PROT_COMPD) { priv->stats.FramesComp++; /* Check sequence number. */ - rseqnum = ntohs(((uint16_t *)(priv->inbuf + offset))[0]); + rseqnum = be16dec(priv->inbuf + offset); offset += 2; if (rseqnum != priv->seqnum) { priv->stats.Errors++; log(LOG_NOTICE, "ng_deflate: wrong sequence: %u " "instead of %u\n", rseqnum, priv->seqnum); NG_FREE_M(m); priv->seqnum = 0; return (EPIPE); } outlen = DEFLATE_BUF_SIZE; /* Decompress "inbuf" into "outbuf". */ /* Prepare to decompress. */ priv->cx.next_in = priv->inbuf + offset; priv->cx.avail_in = inlen - offset; /* Reserve space for protocol decompression. */ priv->cx.next_out = priv->outbuf + 1; priv->cx.avail_out = outlen - 1; /* Decompress. */ rtn = inflate(&priv->cx, Z_PACKET_FLUSH); /* Check return value. */ if (rtn != Z_OK && rtn != Z_STREAM_END) { priv->stats.Errors++; NG_FREE_M(m); priv->seqnum = 0; log(LOG_NOTICE, "%s: decompression error: %d (%s)\n", __func__, rtn, priv->cx.msg); switch (rtn) { case Z_MEM_ERROR: return (ENOMEM); case Z_DATA_ERROR: return (EIO); default: return (EINVAL); } } /* Calculate resulting size. */ outlen -= priv->cx.avail_out; /* Decompress protocol. */ if ((priv->outbuf[1] & 0x01) != 0) { priv->outbuf[0] = 0; /* Return packet in an mbuf. */ m_copyback(m, 0, outlen, (caddr_t)priv->outbuf); } else { outlen--; /* Return packet in an mbuf. */ m_copyback(m, 0, outlen, (caddr_t)(priv->outbuf + 1)); } if (m->m_pkthdr.len < outlen) { m_freem(m); priv->stats.Errors++; priv->seqnum = 0; return (ENOMEM); } else if (outlen < m->m_pkthdr.len) m_adj(m, outlen - m->m_pkthdr.len); *resultp = m; priv->stats.FramesPlain++; priv->stats.OutOctets+=outlen; } else { /* Packet is not compressed, just update dictionary. */ priv->stats.FramesUncomp++; if (priv->inbuf[0] == 0) { priv->cx.next_in = priv->inbuf + 1; /* compress protocol */ priv->cx.avail_in = inlen - 1; } else { priv->cx.next_in = priv->inbuf; priv->cx.avail_in = inlen; } rtn = inflateIncomp(&priv->cx); /* Check return value */ if (rtn != Z_OK) { priv->stats.Errors++; log(LOG_NOTICE, "%s: inflateIncomp error: %d (%s)\n", __func__, rtn, priv->cx.msg); NG_FREE_M(m); priv->seqnum = 0; return (EINVAL); } *resultp = m; priv->stats.FramesPlain++; priv->stats.OutOctets += inlen; } /* Update sequence number. */ priv->seqnum++; return (0); } /* * The peer has sent us a CCP ResetRequest, so reset our transmit state. */ static void ng_deflate_reset_req(node_p node) { const priv_p priv = NG_NODE_PRIVATE(node); priv->seqnum = 0; if (priv->cfg.enable) { if (priv->compress) deflateReset(&priv->cx); else inflateReset(&priv->cx); } } Index: stable/8/sys/netgraph/ng_mppc.c =================================================================== --- stable/8/sys/netgraph/ng_mppc.c (revision 206660) +++ stable/8/sys/netgraph/ng_mppc.c (revision 206661) @@ -1,886 +1,886 @@ /* * ng_mppc.c */ /*- * Copyright (c) 1996-2000 Whistle Communications, Inc. * All rights reserved. * * Subject to the following obligations and disclaimer of warranty, use and * redistribution of this software, in source or object code forms, with or * without modifications are expressly permitted by Whistle Communications; * provided, however, that: * 1. Any and all reproductions of the source or object code must include the * copyright notice above and the following disclaimer of warranties; and * 2. No rights are granted, in any manner or form, to use Whistle * Communications, Inc. trademarks, including the mark "WHISTLE * COMMUNICATIONS" on advertising, endorsements, or otherwise except as * such appears in the above copyright notice or in the software. * * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY * OF SUCH DAMAGE. * * Author: Archie Cobbs * * $Whistle: ng_mppc.c,v 1.4 1999/11/25 00:10:12 archie Exp $ * $FreeBSD$ */ /* * Microsoft PPP compression (MPPC) and encryption (MPPE) netgraph node type. * * You must define one or both of the NETGRAPH_MPPC_COMPRESSION and/or * NETGRAPH_MPPC_ENCRYPTION options for this node type to be useful. */ #include #include #include #include #include +#include #include #include #include #include #include #include "opt_netgraph.h" #if !defined(NETGRAPH_MPPC_COMPRESSION) && !defined(NETGRAPH_MPPC_ENCRYPTION) #ifdef KLD_MODULE /* XXX NETGRAPH_MPPC_COMPRESSION isn't functional yet */ #define NETGRAPH_MPPC_ENCRYPTION #else /* This case is indicative of an error in sys/conf files */ #error Need either NETGRAPH_MPPC_COMPRESSION or NETGRAPH_MPPC_ENCRYPTION #endif #endif #ifdef NG_SEPARATE_MALLOC MALLOC_DEFINE(M_NETGRAPH_MPPC, "netgraph_mppc", "netgraph mppc node "); #else #define M_NETGRAPH_MPPC M_NETGRAPH #endif #ifdef NETGRAPH_MPPC_COMPRESSION /* XXX this file doesn't exist yet, but hopefully someday it will... */ #include #endif #ifdef NETGRAPH_MPPC_ENCRYPTION #include #endif #include /* Decompression blowup */ #define MPPC_DECOMP_BUFSIZE 8092 /* allocate buffer this big */ #define MPPC_DECOMP_SAFETY 100 /* plus this much margin */ /* MPPC/MPPE header length */ #define MPPC_HDRLEN 2 /* Key length */ #define KEYLEN(b) (((b) & MPPE_128) ? 16 : 8) /* * When packets are lost with MPPE, we may have to re-key arbitrarily * many times to 'catch up' to the new jumped-ahead sequence number. * Since this can be expensive, we pose a limit on how many re-keyings * we will do at one time to avoid a possible D.O.S. vulnerability. * This should instead be a configurable parameter. */ #define MPPE_MAX_REKEY 1000 /* MPPC packet header bits */ #define MPPC_FLAG_FLUSHED 0x8000 /* xmitter reset state */ #define MPPC_FLAG_RESTART 0x4000 /* compress history restart */ #define MPPC_FLAG_COMPRESSED 0x2000 /* packet is compresed */ #define MPPC_FLAG_ENCRYPTED 0x1000 /* packet is encrypted */ #define MPPC_CCOUNT_MASK 0x0fff /* sequence number mask */ #define MPPC_CCOUNT_INC(d) ((d) = (((d) + 1) & MPPC_CCOUNT_MASK)) #define MPPE_UPDATE_MASK 0xff /* coherency count when we're */ #define MPPE_UPDATE_FLAG 0xff /* supposed to update key */ #define MPPC_COMP_OK 0x05 #define MPPC_DECOMP_OK 0x05 /* Per direction info */ struct ng_mppc_dir { struct ng_mppc_config cfg; /* configuration */ hook_p hook; /* netgraph hook */ u_int16_t cc:12; /* coherency count */ u_char flushed; /* clean history (xmit only) */ #ifdef NETGRAPH_MPPC_COMPRESSION u_char *history; /* compression history */ #endif #ifdef NETGRAPH_MPPC_ENCRYPTION u_char key[MPPE_KEY_LEN]; /* session key */ struct rc4_state rc4; /* rc4 state */ #endif }; /* Node private data */ struct ng_mppc_private { struct ng_mppc_dir xmit; /* compress/encrypt config */ struct ng_mppc_dir recv; /* decompress/decrypt config */ ng_ID_t ctrlnode; /* path to controlling node */ }; typedef struct ng_mppc_private *priv_p; /* Netgraph node methods */ static ng_constructor_t ng_mppc_constructor; static ng_rcvmsg_t ng_mppc_rcvmsg; static ng_shutdown_t ng_mppc_shutdown; static ng_newhook_t ng_mppc_newhook; static ng_rcvdata_t ng_mppc_rcvdata; static ng_disconnect_t ng_mppc_disconnect; /* Helper functions */ static int ng_mppc_compress(node_p node, struct mbuf **datap); static int ng_mppc_decompress(node_p node, struct mbuf **datap); #ifdef NETGRAPH_MPPC_ENCRYPTION static void ng_mppc_getkey(const u_char *h, u_char *h2, int len); static void ng_mppc_updatekey(u_int32_t bits, u_char *key0, u_char *key, struct rc4_state *rc4); #endif static void ng_mppc_reset_req(node_p node); /* Node type descriptor */ static struct ng_type ng_mppc_typestruct = { .version = NG_ABI_VERSION, .name = NG_MPPC_NODE_TYPE, .constructor = ng_mppc_constructor, .rcvmsg = ng_mppc_rcvmsg, .shutdown = ng_mppc_shutdown, .newhook = ng_mppc_newhook, .rcvdata = ng_mppc_rcvdata, .disconnect = ng_mppc_disconnect, }; NETGRAPH_INIT(mppc, &ng_mppc_typestruct); #ifdef NETGRAPH_MPPC_ENCRYPTION /* Depend on separate rc4 module */ MODULE_DEPEND(ng_mppc, rc4, 1, 1, 1); #endif /* Fixed bit pattern to weaken keysize down to 40 or 56 bits */ static const u_char ng_mppe_weakenkey[3] = { 0xd1, 0x26, 0x9e }; #define ERROUT(x) do { error = (x); goto done; } while (0) /************************************************************************ NETGRAPH NODE STUFF ************************************************************************/ /* * Node type constructor */ static int ng_mppc_constructor(node_p node) { priv_p priv; /* Allocate private structure */ priv = malloc(sizeof(*priv), M_NETGRAPH_MPPC, M_NOWAIT | M_ZERO); if (priv == NULL) return (ENOMEM); NG_NODE_SET_PRIVATE(node, priv); /* This node is not thread safe. */ NG_NODE_FORCE_WRITER(node); /* Done */ return (0); } /* * Give our OK for a hook to be added */ static int ng_mppc_newhook(node_p node, hook_p hook, const char *name) { const priv_p priv = NG_NODE_PRIVATE(node); hook_p *hookPtr; /* Check hook name */ if (strcmp(name, NG_MPPC_HOOK_COMP) == 0) hookPtr = &priv->xmit.hook; else if (strcmp(name, NG_MPPC_HOOK_DECOMP) == 0) hookPtr = &priv->recv.hook; else return (EINVAL); /* See if already connected */ if (*hookPtr != NULL) return (EISCONN); /* OK */ *hookPtr = hook; return (0); } /* * Receive a control message */ static int ng_mppc_rcvmsg(node_p node, item_p item, hook_p lasthook) { const priv_p priv = NG_NODE_PRIVATE(node); struct ng_mesg *resp = NULL; int error = 0; struct ng_mesg *msg; NGI_GET_MSG(item, msg); switch (msg->header.typecookie) { case NGM_MPPC_COOKIE: switch (msg->header.cmd) { case NGM_MPPC_CONFIG_COMP: case NGM_MPPC_CONFIG_DECOMP: { struct ng_mppc_config *const cfg = (struct ng_mppc_config *)msg->data; const int isComp = msg->header.cmd == NGM_MPPC_CONFIG_COMP; struct ng_mppc_dir *const d = isComp ? &priv->xmit : &priv->recv; /* Check configuration */ if (msg->header.arglen != sizeof(*cfg)) ERROUT(EINVAL); if (cfg->enable) { if ((cfg->bits & ~MPPC_VALID_BITS) != 0) ERROUT(EINVAL); #ifndef NETGRAPH_MPPC_COMPRESSION if ((cfg->bits & MPPC_BIT) != 0) ERROUT(EPROTONOSUPPORT); #endif #ifndef NETGRAPH_MPPC_ENCRYPTION if ((cfg->bits & MPPE_BITS) != 0) ERROUT(EPROTONOSUPPORT); #endif } else cfg->bits = 0; /* Save return address so we can send reset-req's */ if (!isComp) priv->ctrlnode = NGI_RETADDR(item); /* Configuration is OK, reset to it */ d->cfg = *cfg; #ifdef NETGRAPH_MPPC_COMPRESSION /* Initialize state buffers for compression */ if (d->history != NULL) { free(d->history, M_NETGRAPH_MPPC); d->history = NULL; } if ((cfg->bits & MPPC_BIT) != 0) { d->history = malloc(isComp ? MPPC_SizeOfCompressionHistory() : MPPC_SizeOfDecompressionHistory(), M_NETGRAPH_MPPC, M_NOWAIT); if (d->history == NULL) ERROUT(ENOMEM); if (isComp) MPPC_InitCompressionHistory(d->history); else { MPPC_InitDecompressionHistory( d->history); } } #endif #ifdef NETGRAPH_MPPC_ENCRYPTION /* Generate initial session keys for encryption */ if ((cfg->bits & MPPE_BITS) != 0) { const int keylen = KEYLEN(cfg->bits); bcopy(cfg->startkey, d->key, keylen); ng_mppc_getkey(cfg->startkey, d->key, keylen); if ((cfg->bits & MPPE_40) != 0) bcopy(&ng_mppe_weakenkey, d->key, 3); else if ((cfg->bits & MPPE_56) != 0) bcopy(&ng_mppe_weakenkey, d->key, 1); rc4_init(&d->rc4, d->key, keylen); } #endif /* Initialize other state */ d->cc = 0; d->flushed = 0; break; } case NGM_MPPC_RESETREQ: ng_mppc_reset_req(node); break; default: error = EINVAL; break; } break; default: error = EINVAL; break; } done: NG_RESPOND_MSG(error, node, item, resp); NG_FREE_MSG(msg); return (error); } /* * Receive incoming data on our hook. */ static int ng_mppc_rcvdata(hook_p hook, item_p item) { const node_p node = NG_HOOK_NODE(hook); const priv_p priv = NG_NODE_PRIVATE(node); int error; struct mbuf *m; NGI_GET_M(item, m); /* Compress and/or encrypt */ if (hook == priv->xmit.hook) { if (!priv->xmit.cfg.enable) { NG_FREE_M(m); NG_FREE_ITEM(item); return (ENXIO); } if ((error = ng_mppc_compress(node, &m)) != 0) { NG_FREE_ITEM(item); return(error); } NG_FWD_NEW_DATA(error, item, priv->xmit.hook, m); return (error); } /* Decompress and/or decrypt */ if (hook == priv->recv.hook) { if (!priv->recv.cfg.enable) { NG_FREE_M(m); NG_FREE_ITEM(item); return (ENXIO); } if ((error = ng_mppc_decompress(node, &m)) != 0) { NG_FREE_ITEM(item); if (error == EINVAL && priv->ctrlnode != 0) { struct ng_mesg *msg; /* Need to send a reset-request */ NG_MKMESSAGE(msg, NGM_MPPC_COOKIE, NGM_MPPC_RESETREQ, 0, M_NOWAIT); if (msg == NULL) return (error); NG_SEND_MSG_ID(error, node, msg, priv->ctrlnode, 0); } return (error); } NG_FWD_NEW_DATA(error, item, priv->recv.hook, m); return (error); } /* Oops */ panic("%s: unknown hook", __func__); #ifdef RESTARTABLE_PANICS return (EINVAL); #endif } /* * Destroy node */ static int ng_mppc_shutdown(node_p node) { const priv_p priv = NG_NODE_PRIVATE(node); /* Take down netgraph node */ #ifdef NETGRAPH_MPPC_COMPRESSION if (priv->xmit.history != NULL) free(priv->xmit.history, M_NETGRAPH_MPPC); if (priv->recv.history != NULL) free(priv->recv.history, M_NETGRAPH_MPPC); #endif bzero(priv, sizeof(*priv)); free(priv, M_NETGRAPH_MPPC); NG_NODE_SET_PRIVATE(node, NULL); NG_NODE_UNREF(node); /* let the node escape */ return (0); } /* * Hook disconnection */ static int ng_mppc_disconnect(hook_p hook) { const node_p node = NG_HOOK_NODE(hook); const priv_p priv = NG_NODE_PRIVATE(node); /* Zero out hook pointer */ if (hook == priv->xmit.hook) priv->xmit.hook = NULL; if (hook == priv->recv.hook) priv->recv.hook = NULL; /* Go away if no longer connected */ if ((NG_NODE_NUMHOOKS(node) == 0) && NG_NODE_IS_VALID(node)) ng_rmnode_self(node); return (0); } /************************************************************************ HELPER STUFF ************************************************************************/ /* * Compress/encrypt a packet and put the result in a new mbuf at *resultp. * The original mbuf is not free'd. */ static int ng_mppc_compress(node_p node, struct mbuf **datap) { const priv_p priv = NG_NODE_PRIVATE(node); struct ng_mppc_dir *const d = &priv->xmit; u_int16_t header; struct mbuf *m = *datap; /* We must own the mbuf chain exclusively to modify it. */ m = m_unshare(m, M_DONTWAIT); if (m == NULL) return (ENOMEM); /* Initialize */ header = d->cc; /* Always set the flushed bit in stateless mode */ if (d->flushed || ((d->cfg.bits & MPPE_STATELESS) != 0)) { header |= MPPC_FLAG_FLUSHED; d->flushed = 0; } /* Compress packet (if compression enabled) */ #ifdef NETGRAPH_MPPC_COMPRESSION if ((d->cfg.bits & MPPC_BIT) != 0) { u_short flags = MPPC_MANDATORY_COMPRESS_FLAGS; u_char *inbuf, *outbuf; int outlen, inlen, ina; u_char *source, *dest; u_long sourceCnt, destCnt; int rtn; /* Work with contiguous regions of memory. */ inlen = m->m_pkthdr.len; if (m->m_next == NULL) { inbuf = mtod(m, u_char *); ina = 0; } else { inbuf = malloc(inlen, M_NETGRAPH_MPPC, M_NOWAIT); if (inbuf == NULL) goto err1; m_copydata(m, 0, inlen, (caddr_t)inbuf); ina = 1; } outlen = MPPC_MAX_BLOWUP(inlen); outbuf = malloc(outlen, M_NETGRAPH_MPPC, M_NOWAIT); if (outbuf == NULL) { if (ina) free(inbuf, M_NETGRAPH_MPPC); err1: m_freem(m); MPPC_InitCompressionHistory(d->history); d->flushed = 1; return (ENOMEM); } /* Prepare to compress */ source = inbuf; sourceCnt = inlen; dest = outbuf; destCnt = outlen; if ((d->cfg.bits & MPPE_STATELESS) == 0) flags |= MPPC_SAVE_HISTORY; /* Compress */ rtn = MPPC_Compress(&source, &dest, &sourceCnt, &destCnt, d->history, flags, 0); /* Check return value */ KASSERT(rtn != MPPC_INVALID, ("%s: invalid", __func__)); if ((rtn & MPPC_EXPANDED) == 0 && (rtn & MPPC_COMP_OK) == MPPC_COMP_OK) { outlen -= destCnt; header |= MPPC_FLAG_COMPRESSED; if ((rtn & MPPC_RESTART_HISTORY) != 0) header |= MPPC_FLAG_RESTART; /* Replace m by the compresed one. */ m_copyback(m, 0, outlen, (caddr_t)outbuf); if (m->m_pkthdr.len < outlen) { m_freem(m); m = NULL; } else if (outlen < m->m_pkthdr.len) m_adj(m, outlen - m->m_pkthdr.len); } d->flushed = (rtn & MPPC_EXPANDED) != 0 || (flags & MPPC_SAVE_HISTORY) == 0; if (ina) free(inbuf, M_NETGRAPH_MPPC); free(outbuf, M_NETGRAPH_MPPC); /* Check mbuf chain reload result. */ if (m == NULL) { if (!d->flushed) { MPPC_InitCompressionHistory(d->history); d->flushed = 1; } return (ENOMEM); } } #endif /* Now encrypt packet (if encryption enabled) */ #ifdef NETGRAPH_MPPC_ENCRYPTION if ((d->cfg.bits & MPPE_BITS) != 0) { struct mbuf *m1; /* Set header bits */ header |= MPPC_FLAG_ENCRYPTED; /* Update key if it's time */ if ((d->cfg.bits & MPPE_STATELESS) != 0 || (d->cc & MPPE_UPDATE_MASK) == MPPE_UPDATE_FLAG) { ng_mppc_updatekey(d->cfg.bits, d->cfg.startkey, d->key, &d->rc4); } else if ((header & MPPC_FLAG_FLUSHED) != 0) { /* Need to reset key if we say we did and ng_mppc_updatekey wasn't called to do it also. */ rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits)); } /* Encrypt packet */ m1 = m; while (m1) { rc4_crypt(&d->rc4, mtod(m1, u_char *), mtod(m1, u_char *), m1->m_len); m1 = m1->m_next; } } #endif /* Update coherency count for next time (12 bit arithmetic) */ MPPC_CCOUNT_INC(d->cc); /* Install header */ M_PREPEND(m, MPPC_HDRLEN, M_DONTWAIT); if (m != NULL) - *(mtod(m, uint16_t *)) = htons(header); + be16enc(mtod(m, void *), header); *datap = m; return (*datap == NULL ? ENOBUFS : 0); } /* * Decompress/decrypt packet and put the result in a new mbuf at *resultp. * The original mbuf is not free'd. */ static int ng_mppc_decompress(node_p node, struct mbuf **datap) { const priv_p priv = NG_NODE_PRIVATE(node); struct ng_mppc_dir *const d = &priv->recv; u_int16_t header, cc; u_int numLost; struct mbuf *m = *datap; /* We must own the mbuf chain exclusively to modify it. */ m = m_unshare(m, M_DONTWAIT); if (m == NULL) return (ENOMEM); /* Pull off header */ if (m->m_pkthdr.len < MPPC_HDRLEN) { m_freem(m); return (EINVAL); } - m_copydata(m, 0, MPPC_HDRLEN, (caddr_t)&header); - header = ntohs(header); + header = be16dec(mtod(m, void *)); cc = (header & MPPC_CCOUNT_MASK); m_adj(m, MPPC_HDRLEN); /* Check for an unexpected jump in the sequence number */ numLost = ((cc - d->cc) & MPPC_CCOUNT_MASK); /* If flushed bit set, we can always handle packet */ if ((header & MPPC_FLAG_FLUSHED) != 0) { #ifdef NETGRAPH_MPPC_COMPRESSION if (d->history != NULL) MPPC_InitDecompressionHistory(d->history); #endif #ifdef NETGRAPH_MPPC_ENCRYPTION if ((d->cfg.bits & MPPE_BITS) != 0) { u_int rekey; /* How many times are we going to have to re-key? */ rekey = ((d->cfg.bits & MPPE_STATELESS) != 0) ? numLost : (numLost / (MPPE_UPDATE_MASK + 1)); if (rekey > MPPE_MAX_REKEY) { log(LOG_ERR, "%s: too many (%d) packets" " dropped, disabling node %p!", __func__, numLost, node); priv->recv.cfg.enable = 0; goto failed; } /* Re-key as necessary to catch up to peer */ while (d->cc != cc) { if ((d->cfg.bits & MPPE_STATELESS) != 0 || (d->cc & MPPE_UPDATE_MASK) == MPPE_UPDATE_FLAG) { ng_mppc_updatekey(d->cfg.bits, d->cfg.startkey, d->key, &d->rc4); } MPPC_CCOUNT_INC(d->cc); } /* Reset key (except in stateless mode, see below) */ if ((d->cfg.bits & MPPE_STATELESS) == 0) rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits)); } #endif d->cc = cc; /* skip over lost seq numbers */ numLost = 0; /* act like no packets were lost */ } /* Can't decode non-sequential packets without a flushed bit */ if (numLost != 0) goto failed; /* Decrypt packet */ if ((header & MPPC_FLAG_ENCRYPTED) != 0) { #ifdef NETGRAPH_MPPC_ENCRYPTION struct mbuf *m1; #endif /* Are we not expecting encryption? */ if ((d->cfg.bits & MPPE_BITS) == 0) { log(LOG_ERR, "%s: rec'd unexpectedly %s packet", __func__, "encrypted"); goto failed; } #ifdef NETGRAPH_MPPC_ENCRYPTION /* Update key if it's time (always in stateless mode) */ if ((d->cfg.bits & MPPE_STATELESS) != 0 || (d->cc & MPPE_UPDATE_MASK) == MPPE_UPDATE_FLAG) { ng_mppc_updatekey(d->cfg.bits, d->cfg.startkey, d->key, &d->rc4); } /* Decrypt packet */ m1 = m; while (m1 != NULL) { rc4_crypt(&d->rc4, mtod(m1, u_char *), mtod(m1, u_char *), m1->m_len); m1 = m1->m_next; } #endif } else { /* Are we expecting encryption? */ if ((d->cfg.bits & MPPE_BITS) != 0) { log(LOG_ERR, "%s: rec'd unexpectedly %s packet", __func__, "unencrypted"); goto failed; } } /* Update coherency count for next time (12 bit arithmetic) */ MPPC_CCOUNT_INC(d->cc); /* Check for unexpected compressed packet */ if ((header & MPPC_FLAG_COMPRESSED) != 0 && (d->cfg.bits & MPPC_BIT) == 0) { log(LOG_ERR, "%s: rec'd unexpectedly %s packet", __func__, "compressed"); failed: m_freem(m); return (EINVAL); } #ifdef NETGRAPH_MPPC_COMPRESSION /* Decompress packet */ if ((header & MPPC_FLAG_COMPRESSED) != 0) { int flags = MPPC_MANDATORY_DECOMPRESS_FLAGS; u_char *inbuf, *outbuf; int inlen, outlen, ina; u_char *source, *dest; u_long sourceCnt, destCnt; int rtn; /* Copy payload into a contiguous region of memory. */ inlen = m->m_pkthdr.len; if (m->m_next == NULL) { inbuf = mtod(m, u_char *); ina = 0; } else { inbuf = malloc(inlen, M_NETGRAPH_MPPC, M_NOWAIT); if (inbuf == NULL) { m_freem(m); return (ENOMEM); } m_copydata(m, 0, inlen, (caddr_t)inbuf); ina = 1; } /* Allocate a buffer for decompressed data */ outbuf = malloc(MPPC_DECOMP_BUFSIZE + MPPC_DECOMP_SAFETY, M_NETGRAPH_MPPC, M_NOWAIT); if (outbuf == NULL) { m_freem(m); if (ina) free(inbuf, M_NETGRAPH_MPPC); return (ENOMEM); } outlen = MPPC_DECOMP_BUFSIZE; /* Prepare to decompress */ source = inbuf; sourceCnt = inlen; dest = outbuf; destCnt = outlen; if ((header & MPPC_FLAG_RESTART) != 0) flags |= MPPC_RESTART_HISTORY; /* Decompress */ rtn = MPPC_Decompress(&source, &dest, &sourceCnt, &destCnt, d->history, flags); /* Check return value */ KASSERT(rtn != MPPC_INVALID, ("%s: invalid", __func__)); if ((rtn & MPPC_DEST_EXHAUSTED) != 0 || (rtn & MPPC_DECOMP_OK) != MPPC_DECOMP_OK) { log(LOG_ERR, "%s: decomp returned 0x%x", __func__, rtn); if (ina) free(inbuf, M_NETGRAPH_MPPC); free(outbuf, M_NETGRAPH_MPPC); goto failed; } /* Replace compressed data with decompressed data */ if (ina) free(inbuf, M_NETGRAPH_MPPC); outlen -= destCnt; m_copyback(m, 0, outlen, (caddr_t)outbuf); if (m->m_pkthdr.len < outlen) { m_freem(m); m = NULL; } else if (outlen < m->m_pkthdr.len) m_adj(m, outlen - m->m_pkthdr.len); free(outbuf, M_NETGRAPH_MPPC); } #endif /* Return result in an mbuf */ *datap = m; return (*datap == NULL ? ENOBUFS : 0); } /* * The peer has sent us a CCP ResetRequest, so reset our transmit state. */ static void ng_mppc_reset_req(node_p node) { const priv_p priv = NG_NODE_PRIVATE(node); struct ng_mppc_dir *const d = &priv->xmit; #ifdef NETGRAPH_MPPC_COMPRESSION if (d->history != NULL) MPPC_InitCompressionHistory(d->history); #endif #ifdef NETGRAPH_MPPC_ENCRYPTION if ((d->cfg.bits & MPPE_STATELESS) == 0) rc4_init(&d->rc4, d->key, KEYLEN(d->cfg.bits)); #endif d->flushed = 1; } #ifdef NETGRAPH_MPPC_ENCRYPTION /* * Generate a new encryption key */ static void ng_mppc_getkey(const u_char *h, u_char *h2, int len) { static const u_char pad1[40] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; static const u_char pad2[40] = { 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2 }; u_char hash[20]; SHA1_CTX c; SHA1Init(&c); SHA1Update(&c, h, len); SHA1Update(&c, pad1, sizeof(pad1)); SHA1Update(&c, h2, len); SHA1Update(&c, pad2, sizeof(pad2)); SHA1Final(hash, &c); bcopy(hash, h2, len); } /* * Update the encryption key */ static void ng_mppc_updatekey(u_int32_t bits, u_char *key0, u_char *key, struct rc4_state *rc4) { const int keylen = KEYLEN(bits); ng_mppc_getkey(key0, key, keylen); rc4_init(rc4, key, keylen); rc4_crypt(rc4, key, key, keylen); if ((bits & MPPE_40) != 0) bcopy(&ng_mppe_weakenkey, key, 3); else if ((bits & MPPE_56) != 0) bcopy(&ng_mppe_weakenkey, key, 1); rc4_init(rc4, key, keylen); } #endif Index: stable/8/sys/netgraph/ng_ppp.c =================================================================== --- stable/8/sys/netgraph/ng_ppp.c (revision 206660) +++ stable/8/sys/netgraph/ng_ppp.c (revision 206661) @@ -1,2619 +1,2620 @@ /*- * Copyright (c) 1996-2000 Whistle Communications, Inc. * All rights reserved. * * Subject to the following obligations and disclaimer of warranty, use and * redistribution of this software, in source or object code forms, with or * without modifications are expressly permitted by Whistle Communications; * provided, however, that: * 1. Any and all reproductions of the source or object code must include the * copyright notice above and the following disclaimer of warranties; and * 2. No rights are granted, in any manner or form, to use Whistle * Communications, Inc. trademarks, including the mark "WHISTLE * COMMUNICATIONS" on advertising, endorsements, or otherwise except as * such appears in the above copyright notice or in the software. * * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY * OF SUCH DAMAGE. * * Copyright (c) 2007 Alexander Motin * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice unmodified, this list of conditions, and the following * disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * Authors: Archie Cobbs , Alexander Motin * * $FreeBSD$ * $Whistle: ng_ppp.c,v 1.24 1999/11/01 09:24:52 julian Exp $ */ /* * PPP node type data-flow. * * hook xmit layer recv hook * ------------------------------------ * inet -> -> inet * ipv6 -> -> ipv6 * ipx -> proto -> ipx * atalk -> -> atalk * bypass -> -> bypass * -hcomp_xmit()----------proto_recv()- * vjc_ip <- <- vjc_ip * vjc_comp -> header compression -> vjc_comp * vjc_uncomp -> -> vjc_uncomp * vjc_vjip -> * -comp_xmit()-----------hcomp_recv()- * compress <- compression <- decompress * compress -> -> decompress * -crypt_xmit()-----------comp_recv()- * encrypt <- encryption <- decrypt * encrypt -> -> decrypt * -ml_xmit()-------------crypt_recv()- * multilink * -link_xmit()--------------ml_recv()- * linkX <- link <- linkX * */ #include #include #include #include #include #include #include +#include #include #include #include #include #include #include #include #ifdef NG_SEPARATE_MALLOC MALLOC_DEFINE(M_NETGRAPH_PPP, "netgraph_ppp", "netgraph ppp node"); #else #define M_NETGRAPH_PPP M_NETGRAPH #endif #define PROT_VALID(p) (((p) & 0x0101) == 0x0001) #define PROT_COMPRESSABLE(p) (((p) & 0xff00) == 0x0000) /* Some PPP protocol numbers we're interested in */ #define PROT_ATALK 0x0029 #define PROT_COMPD 0x00fd #define PROT_CRYPTD 0x0053 #define PROT_IP 0x0021 #define PROT_IPV6 0x0057 #define PROT_IPX 0x002b #define PROT_LCP 0xc021 #define PROT_MP 0x003d #define PROT_VJCOMP 0x002d #define PROT_VJUNCOMP 0x002f /* Multilink PPP definitions */ #define MP_INITIAL_SEQ 0 /* per RFC 1990 */ #define MP_MIN_LINK_MRU 32 #define MP_SHORT_SEQ_MASK 0x00000fff /* short seq # mask */ #define MP_SHORT_SEQ_HIBIT 0x00000800 /* short seq # high bit */ #define MP_SHORT_FIRST_FLAG 0x00008000 /* first fragment in frame */ #define MP_SHORT_LAST_FLAG 0x00004000 /* last fragment in frame */ #define MP_LONG_SEQ_MASK 0x00ffffff /* long seq # mask */ #define MP_LONG_SEQ_HIBIT 0x00800000 /* long seq # high bit */ #define MP_LONG_FIRST_FLAG 0x80000000 /* first fragment in frame */ #define MP_LONG_LAST_FLAG 0x40000000 /* last fragment in frame */ #define MP_NOSEQ 0x7fffffff /* impossible sequence number */ /* Sign extension of MP sequence numbers */ #define MP_SHORT_EXTEND(s) (((s) & MP_SHORT_SEQ_HIBIT) ? \ ((s) | ~MP_SHORT_SEQ_MASK) \ : ((s) & MP_SHORT_SEQ_MASK)) #define MP_LONG_EXTEND(s) (((s) & MP_LONG_SEQ_HIBIT) ? \ ((s) | ~MP_LONG_SEQ_MASK) \ : ((s) & MP_LONG_SEQ_MASK)) /* Comparision of MP sequence numbers. Note: all sequence numbers except priv->xseq are stored with the sign bit extended. */ #define MP_SHORT_SEQ_DIFF(x,y) MP_SHORT_EXTEND((x) - (y)) #define MP_LONG_SEQ_DIFF(x,y) MP_LONG_EXTEND((x) - (y)) #define MP_RECV_SEQ_DIFF(priv,x,y) \ ((priv)->conf.recvShortSeq ? \ MP_SHORT_SEQ_DIFF((x), (y)) : \ MP_LONG_SEQ_DIFF((x), (y))) /* Increment receive sequence number */ #define MP_NEXT_RECV_SEQ(priv,seq) \ ((priv)->conf.recvShortSeq ? \ MP_SHORT_EXTEND((seq) + 1) : \ MP_LONG_EXTEND((seq) + 1)) /* Don't fragment transmitted packets to parts smaller than this */ #define MP_MIN_FRAG_LEN 32 /* Maximum fragment reasssembly queue length */ #define MP_MAX_QUEUE_LEN 128 /* Fragment queue scanner period */ #define MP_FRAGTIMER_INTERVAL (hz/2) /* Average link overhead. XXX: Should be given by user-level */ #define MP_AVERAGE_LINK_OVERHEAD 16 /* Keep this equal to ng_ppp_hook_names lower! */ #define HOOK_INDEX_MAX 13 /* We store incoming fragments this way */ struct ng_ppp_frag { int seq; /* fragment seq# */ uint8_t first; /* First in packet? */ uint8_t last; /* Last in packet? */ struct timeval timestamp; /* time of reception */ struct mbuf *data; /* Fragment data */ TAILQ_ENTRY(ng_ppp_frag) f_qent; /* Fragment queue */ }; /* Per-link private information */ struct ng_ppp_link { struct ng_ppp_link_conf conf; /* link configuration */ struct ng_ppp_link_stat64 stats; /* link stats */ hook_p hook; /* connection to link data */ int32_t seq; /* highest rec'd seq# - MSEQ */ uint32_t latency; /* calculated link latency */ struct timeval lastWrite; /* time of last write for MP */ int bytesInQueue; /* bytes in the output queue for MP */ }; /* Total per-node private information */ struct ng_ppp_private { struct ng_ppp_bund_conf conf; /* bundle config */ struct ng_ppp_link_stat64 bundleStats; /* bundle stats */ struct ng_ppp_link links[NG_PPP_MAX_LINKS];/* per-link info */ int32_t xseq; /* next out MP seq # */ int32_t mseq; /* min links[i].seq */ uint16_t activeLinks[NG_PPP_MAX_LINKS]; /* indicies */ uint16_t numActiveLinks; /* how many links up */ uint16_t lastLink; /* for round robin */ uint8_t vjCompHooked; /* VJ comp hooked up? */ uint8_t allLinksEqual; /* all xmit the same? */ hook_p hooks[HOOK_INDEX_MAX]; /* non-link hooks */ struct ng_ppp_frag fragsmem[MP_MAX_QUEUE_LEN]; /* fragments storage */ TAILQ_HEAD(ng_ppp_fraglist, ng_ppp_frag) /* fragment queue */ frags; TAILQ_HEAD(ng_ppp_fragfreelist, ng_ppp_frag) /* free fragment queue */ fragsfree; struct callout fragTimer; /* fraq queue check */ struct mtx rmtx; /* recv mutex */ struct mtx xmtx; /* xmit mutex */ }; typedef struct ng_ppp_private *priv_p; /* Netgraph node methods */ static ng_constructor_t ng_ppp_constructor; static ng_rcvmsg_t ng_ppp_rcvmsg; static ng_shutdown_t ng_ppp_shutdown; static ng_newhook_t ng_ppp_newhook; static ng_rcvdata_t ng_ppp_rcvdata; static ng_disconnect_t ng_ppp_disconnect; static ng_rcvdata_t ng_ppp_rcvdata_inet; static ng_rcvdata_t ng_ppp_rcvdata_ipv6; static ng_rcvdata_t ng_ppp_rcvdata_ipx; static ng_rcvdata_t ng_ppp_rcvdata_atalk; static ng_rcvdata_t ng_ppp_rcvdata_bypass; static ng_rcvdata_t ng_ppp_rcvdata_vjc_ip; static ng_rcvdata_t ng_ppp_rcvdata_vjc_comp; static ng_rcvdata_t ng_ppp_rcvdata_vjc_uncomp; static ng_rcvdata_t ng_ppp_rcvdata_vjc_vjip; static ng_rcvdata_t ng_ppp_rcvdata_compress; static ng_rcvdata_t ng_ppp_rcvdata_decompress; static ng_rcvdata_t ng_ppp_rcvdata_encrypt; static ng_rcvdata_t ng_ppp_rcvdata_decrypt; /* We use integer indicies to refer to the non-link hooks. */ static const struct { char *const name; ng_rcvdata_t *fn; } ng_ppp_hook_names[] = { #define HOOK_INDEX_ATALK 0 { NG_PPP_HOOK_ATALK, ng_ppp_rcvdata_atalk }, #define HOOK_INDEX_BYPASS 1 { NG_PPP_HOOK_BYPASS, ng_ppp_rcvdata_bypass }, #define HOOK_INDEX_COMPRESS 2 { NG_PPP_HOOK_COMPRESS, ng_ppp_rcvdata_compress }, #define HOOK_INDEX_ENCRYPT 3 { NG_PPP_HOOK_ENCRYPT, ng_ppp_rcvdata_encrypt }, #define HOOK_INDEX_DECOMPRESS 4 { NG_PPP_HOOK_DECOMPRESS, ng_ppp_rcvdata_decompress }, #define HOOK_INDEX_DECRYPT 5 { NG_PPP_HOOK_DECRYPT, ng_ppp_rcvdata_decrypt }, #define HOOK_INDEX_INET 6 { NG_PPP_HOOK_INET, ng_ppp_rcvdata_inet }, #define HOOK_INDEX_IPX 7 { NG_PPP_HOOK_IPX, ng_ppp_rcvdata_ipx }, #define HOOK_INDEX_VJC_COMP 8 { NG_PPP_HOOK_VJC_COMP, ng_ppp_rcvdata_vjc_comp }, #define HOOK_INDEX_VJC_IP 9 { NG_PPP_HOOK_VJC_IP, ng_ppp_rcvdata_vjc_ip }, #define HOOK_INDEX_VJC_UNCOMP 10 { NG_PPP_HOOK_VJC_UNCOMP, ng_ppp_rcvdata_vjc_uncomp }, #define HOOK_INDEX_VJC_VJIP 11 { NG_PPP_HOOK_VJC_VJIP, ng_ppp_rcvdata_vjc_vjip }, #define HOOK_INDEX_IPV6 12 { NG_PPP_HOOK_IPV6, ng_ppp_rcvdata_ipv6 }, { NULL, NULL } }; /* Helper functions */ static int ng_ppp_proto_recv(node_p node, item_p item, uint16_t proto, uint16_t linkNum); static int ng_ppp_hcomp_xmit(node_p node, item_p item, uint16_t proto); static int ng_ppp_hcomp_recv(node_p node, item_p item, uint16_t proto, uint16_t linkNum); static int ng_ppp_comp_xmit(node_p node, item_p item, uint16_t proto); static int ng_ppp_comp_recv(node_p node, item_p item, uint16_t proto, uint16_t linkNum); static int ng_ppp_crypt_xmit(node_p node, item_p item, uint16_t proto); static int ng_ppp_crypt_recv(node_p node, item_p item, uint16_t proto, uint16_t linkNum); static int ng_ppp_mp_xmit(node_p node, item_p item, uint16_t proto); static int ng_ppp_mp_recv(node_p node, item_p item, uint16_t proto, uint16_t linkNum); static int ng_ppp_link_xmit(node_p node, item_p item, uint16_t proto, uint16_t linkNum, int plen); static int ng_ppp_bypass(node_p node, item_p item, uint16_t proto, uint16_t linkNum); static void ng_ppp_bump_mseq(node_p node, int32_t new_mseq); static int ng_ppp_frag_drop(node_p node); static int ng_ppp_check_packet(node_p node); static void ng_ppp_get_packet(node_p node, struct mbuf **mp); static int ng_ppp_frag_process(node_p node, item_p oitem); static int ng_ppp_frag_trim(node_p node); static void ng_ppp_frag_timeout(node_p node, hook_p hook, void *arg1, int arg2); static void ng_ppp_frag_checkstale(node_p node); static void ng_ppp_frag_reset(node_p node); static void ng_ppp_mp_strategy(node_p node, int len, int *distrib); static int ng_ppp_intcmp(void *latency, const void *v1, const void *v2); static struct mbuf *ng_ppp_addproto(struct mbuf *m, uint16_t proto, int compOK); static struct mbuf *ng_ppp_cutproto(struct mbuf *m, uint16_t *proto); static struct mbuf *ng_ppp_prepend(struct mbuf *m, const void *buf, int len); static int ng_ppp_config_valid(node_p node, const struct ng_ppp_node_conf *newConf); static void ng_ppp_update(node_p node, int newConf); static void ng_ppp_start_frag_timer(node_p node); static void ng_ppp_stop_frag_timer(node_p node); /* Parse type for struct ng_ppp_mp_state_type */ static const struct ng_parse_fixedarray_info ng_ppp_rseq_array_info = { &ng_parse_hint32_type, NG_PPP_MAX_LINKS }; static const struct ng_parse_type ng_ppp_rseq_array_type = { &ng_parse_fixedarray_type, &ng_ppp_rseq_array_info, }; static const struct ng_parse_struct_field ng_ppp_mp_state_type_fields[] = NG_PPP_MP_STATE_TYPE_INFO(&ng_ppp_rseq_array_type); static const struct ng_parse_type ng_ppp_mp_state_type = { &ng_parse_struct_type, &ng_ppp_mp_state_type_fields }; /* Parse type for struct ng_ppp_link_conf */ static const struct ng_parse_struct_field ng_ppp_link_type_fields[] = NG_PPP_LINK_TYPE_INFO; static const struct ng_parse_type ng_ppp_link_type = { &ng_parse_struct_type, &ng_ppp_link_type_fields }; /* Parse type for struct ng_ppp_bund_conf */ static const struct ng_parse_struct_field ng_ppp_bund_type_fields[] = NG_PPP_BUND_TYPE_INFO; static const struct ng_parse_type ng_ppp_bund_type = { &ng_parse_struct_type, &ng_ppp_bund_type_fields }; /* Parse type for struct ng_ppp_node_conf */ static const struct ng_parse_fixedarray_info ng_ppp_array_info = { &ng_ppp_link_type, NG_PPP_MAX_LINKS }; static const struct ng_parse_type ng_ppp_link_array_type = { &ng_parse_fixedarray_type, &ng_ppp_array_info, }; static const struct ng_parse_struct_field ng_ppp_conf_type_fields[] = NG_PPP_CONFIG_TYPE_INFO(&ng_ppp_bund_type, &ng_ppp_link_array_type); static const struct ng_parse_type ng_ppp_conf_type = { &ng_parse_struct_type, &ng_ppp_conf_type_fields }; /* Parse type for struct ng_ppp_link_stat */ static const struct ng_parse_struct_field ng_ppp_stats_type_fields[] = NG_PPP_STATS_TYPE_INFO; static const struct ng_parse_type ng_ppp_stats_type = { &ng_parse_struct_type, &ng_ppp_stats_type_fields }; /* Parse type for struct ng_ppp_link_stat64 */ static const struct ng_parse_struct_field ng_ppp_stats64_type_fields[] = NG_PPP_STATS64_TYPE_INFO; static const struct ng_parse_type ng_ppp_stats64_type = { &ng_parse_struct_type, &ng_ppp_stats64_type_fields }; /* List of commands and how to convert arguments to/from ASCII */ static const struct ng_cmdlist ng_ppp_cmds[] = { { NGM_PPP_COOKIE, NGM_PPP_SET_CONFIG, "setconfig", &ng_ppp_conf_type, NULL }, { NGM_PPP_COOKIE, NGM_PPP_GET_CONFIG, "getconfig", NULL, &ng_ppp_conf_type }, { NGM_PPP_COOKIE, NGM_PPP_GET_MP_STATE, "getmpstate", NULL, &ng_ppp_mp_state_type }, { NGM_PPP_COOKIE, NGM_PPP_GET_LINK_STATS, "getstats", &ng_parse_int16_type, &ng_ppp_stats_type }, { NGM_PPP_COOKIE, NGM_PPP_CLR_LINK_STATS, "clrstats", &ng_parse_int16_type, NULL }, { NGM_PPP_COOKIE, NGM_PPP_GETCLR_LINK_STATS, "getclrstats", &ng_parse_int16_type, &ng_ppp_stats_type }, { NGM_PPP_COOKIE, NGM_PPP_GET_LINK_STATS64, "getstats64", &ng_parse_int16_type, &ng_ppp_stats64_type }, { NGM_PPP_COOKIE, NGM_PPP_GETCLR_LINK_STATS64, "getclrstats64", &ng_parse_int16_type, &ng_ppp_stats64_type }, { 0 } }; /* Node type descriptor */ static struct ng_type ng_ppp_typestruct = { .version = NG_ABI_VERSION, .name = NG_PPP_NODE_TYPE, .constructor = ng_ppp_constructor, .rcvmsg = ng_ppp_rcvmsg, .shutdown = ng_ppp_shutdown, .newhook = ng_ppp_newhook, .rcvdata = ng_ppp_rcvdata, .disconnect = ng_ppp_disconnect, .cmdlist = ng_ppp_cmds, }; NETGRAPH_INIT(ppp, &ng_ppp_typestruct); /* Address and control field header */ static const uint8_t ng_ppp_acf[2] = { 0xff, 0x03 }; /* Maximum time we'll let a complete incoming packet sit in the queue */ static const struct timeval ng_ppp_max_staleness = { 2, 0 }; /* 2 seconds */ #define ERROUT(x) do { error = (x); goto done; } while (0) /************************************************************************ NETGRAPH NODE STUFF ************************************************************************/ /* * Node type constructor */ static int ng_ppp_constructor(node_p node) { priv_p priv; int i; /* Allocate private structure */ priv = malloc(sizeof(*priv), M_NETGRAPH_PPP, M_NOWAIT | M_ZERO); if (priv == NULL) return (ENOMEM); NG_NODE_SET_PRIVATE(node, priv); /* Initialize state */ TAILQ_INIT(&priv->frags); TAILQ_INIT(&priv->fragsfree); for (i = 0; i < MP_MAX_QUEUE_LEN; i++) TAILQ_INSERT_TAIL(&priv->fragsfree, &priv->fragsmem[i], f_qent); for (i = 0; i < NG_PPP_MAX_LINKS; i++) priv->links[i].seq = MP_NOSEQ; ng_callout_init(&priv->fragTimer); mtx_init(&priv->rmtx, "ng_ppp_recv", NULL, MTX_DEF); mtx_init(&priv->xmtx, "ng_ppp_xmit", NULL, MTX_DEF); /* Done */ return (0); } /* * Give our OK for a hook to be added */ static int ng_ppp_newhook(node_p node, hook_p hook, const char *name) { const priv_p priv = NG_NODE_PRIVATE(node); hook_p *hookPtr = NULL; int linkNum = -1; int hookIndex = -1; /* Figure out which hook it is */ if (strncmp(name, NG_PPP_HOOK_LINK_PREFIX, /* a link hook? */ strlen(NG_PPP_HOOK_LINK_PREFIX)) == 0) { const char *cp; char *eptr; cp = name + strlen(NG_PPP_HOOK_LINK_PREFIX); if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0')) return (EINVAL); linkNum = (int)strtoul(cp, &eptr, 10); if (*eptr != '\0' || linkNum < 0 || linkNum >= NG_PPP_MAX_LINKS) return (EINVAL); hookPtr = &priv->links[linkNum].hook; hookIndex = ~linkNum; /* See if hook is already connected. */ if (*hookPtr != NULL) return (EISCONN); /* Disallow more than one link unless multilink is enabled. */ if (priv->links[linkNum].conf.enableLink && !priv->conf.enableMultilink && priv->numActiveLinks >= 1) return (ENODEV); } else { /* must be a non-link hook */ int i; for (i = 0; ng_ppp_hook_names[i].name != NULL; i++) { if (strcmp(name, ng_ppp_hook_names[i].name) == 0) { hookPtr = &priv->hooks[i]; hookIndex = i; break; } } if (ng_ppp_hook_names[i].name == NULL) return (EINVAL); /* no such hook */ /* See if hook is already connected */ if (*hookPtr != NULL) return (EISCONN); /* Every non-linkX hook have it's own function. */ NG_HOOK_SET_RCVDATA(hook, ng_ppp_hook_names[i].fn); } /* OK */ *hookPtr = hook; NG_HOOK_SET_PRIVATE(hook, (void *)(intptr_t)hookIndex); ng_ppp_update(node, 0); return (0); } /* * Receive a control message */ static int ng_ppp_rcvmsg(node_p node, item_p item, hook_p lasthook) { const priv_p priv = NG_NODE_PRIVATE(node); struct ng_mesg *resp = NULL; int error = 0; struct ng_mesg *msg; NGI_GET_MSG(item, msg); switch (msg->header.typecookie) { case NGM_PPP_COOKIE: switch (msg->header.cmd) { case NGM_PPP_SET_CONFIG: { struct ng_ppp_node_conf *const conf = (struct ng_ppp_node_conf *)msg->data; int i; /* Check for invalid or illegal config */ if (msg->header.arglen != sizeof(*conf)) ERROUT(EINVAL); if (!ng_ppp_config_valid(node, conf)) ERROUT(EINVAL); /* Copy config */ priv->conf = conf->bund; for (i = 0; i < NG_PPP_MAX_LINKS; i++) priv->links[i].conf = conf->links[i]; ng_ppp_update(node, 1); break; } case NGM_PPP_GET_CONFIG: { struct ng_ppp_node_conf *conf; int i; NG_MKRESPONSE(resp, msg, sizeof(*conf), M_NOWAIT); if (resp == NULL) ERROUT(ENOMEM); conf = (struct ng_ppp_node_conf *)resp->data; conf->bund = priv->conf; for (i = 0; i < NG_PPP_MAX_LINKS; i++) conf->links[i] = priv->links[i].conf; break; } case NGM_PPP_GET_MP_STATE: { struct ng_ppp_mp_state *info; int i; NG_MKRESPONSE(resp, msg, sizeof(*info), M_NOWAIT); if (resp == NULL) ERROUT(ENOMEM); info = (struct ng_ppp_mp_state *)resp->data; bzero(info, sizeof(*info)); for (i = 0; i < NG_PPP_MAX_LINKS; i++) { if (priv->links[i].seq != MP_NOSEQ) info->rseq[i] = priv->links[i].seq; } info->mseq = priv->mseq; info->xseq = priv->xseq; break; } case NGM_PPP_GET_LINK_STATS: case NGM_PPP_CLR_LINK_STATS: case NGM_PPP_GETCLR_LINK_STATS: case NGM_PPP_GET_LINK_STATS64: case NGM_PPP_GETCLR_LINK_STATS64: { struct ng_ppp_link_stat64 *stats; uint16_t linkNum; /* Process request. */ if (msg->header.arglen != sizeof(uint16_t)) ERROUT(EINVAL); linkNum = *((uint16_t *) msg->data); if (linkNum >= NG_PPP_MAX_LINKS && linkNum != NG_PPP_BUNDLE_LINKNUM) ERROUT(EINVAL); stats = (linkNum == NG_PPP_BUNDLE_LINKNUM) ? &priv->bundleStats : &priv->links[linkNum].stats; /* Make 64bit reply. */ if (msg->header.cmd == NGM_PPP_GET_LINK_STATS64 || msg->header.cmd == NGM_PPP_GETCLR_LINK_STATS64) { NG_MKRESPONSE(resp, msg, sizeof(struct ng_ppp_link_stat64), M_NOWAIT); if (resp == NULL) ERROUT(ENOMEM); bcopy(stats, resp->data, sizeof(*stats)); } else /* Make 32bit reply. */ if (msg->header.cmd == NGM_PPP_GET_LINK_STATS || msg->header.cmd == NGM_PPP_GETCLR_LINK_STATS) { struct ng_ppp_link_stat *rs; NG_MKRESPONSE(resp, msg, sizeof(struct ng_ppp_link_stat), M_NOWAIT); if (resp == NULL) ERROUT(ENOMEM); rs = (struct ng_ppp_link_stat *)resp->data; /* Truncate 64->32 bits. */ rs->xmitFrames = stats->xmitFrames; rs->xmitOctets = stats->xmitOctets; rs->recvFrames = stats->recvFrames; rs->recvOctets = stats->recvOctets; rs->badProtos = stats->badProtos; rs->runts = stats->runts; rs->dupFragments = stats->dupFragments; rs->dropFragments = stats->dropFragments; } /* Clear stats. */ if (msg->header.cmd != NGM_PPP_GET_LINK_STATS && msg->header.cmd != NGM_PPP_GET_LINK_STATS64) bzero(stats, sizeof(*stats)); break; } default: error = EINVAL; break; } break; case NGM_VJC_COOKIE: { /* * Forward it to the vjc node. leave the * old return address alone. * If we have no hook, let NG_RESPOND_MSG * clean up any remaining resources. * Because we have no resp, the item will be freed * along with anything it references. Don't * let msg be freed twice. */ NGI_MSG(item) = msg; /* put it back in the item */ msg = NULL; if ((lasthook = priv->hooks[HOOK_INDEX_VJC_IP])) { NG_FWD_ITEM_HOOK(error, item, lasthook); } return (error); } default: error = EINVAL; break; } done: NG_RESPOND_MSG(error, node, item, resp); NG_FREE_MSG(msg); return (error); } /* * Destroy node */ static int ng_ppp_shutdown(node_p node) { const priv_p priv = NG_NODE_PRIVATE(node); /* Stop fragment queue timer */ ng_ppp_stop_frag_timer(node); /* Take down netgraph node */ ng_ppp_frag_reset(node); mtx_destroy(&priv->rmtx); mtx_destroy(&priv->xmtx); bzero(priv, sizeof(*priv)); free(priv, M_NETGRAPH_PPP); NG_NODE_SET_PRIVATE(node, NULL); NG_NODE_UNREF(node); /* let the node escape */ return (0); } /* * Hook disconnection */ static int ng_ppp_disconnect(hook_p hook) { const node_p node = NG_HOOK_NODE(hook); const priv_p priv = NG_NODE_PRIVATE(node); const int index = (intptr_t)NG_HOOK_PRIVATE(hook); /* Zero out hook pointer */ if (index < 0) priv->links[~index].hook = NULL; else priv->hooks[index] = NULL; /* Update derived info (or go away if no hooks left). */ if (NG_NODE_NUMHOOKS(node) > 0) ng_ppp_update(node, 0); else if (NG_NODE_IS_VALID(node)) ng_rmnode_self(node); return (0); } /* * Proto layer */ /* * Receive data on a hook inet. */ static int ng_ppp_rcvdata_inet(hook_p hook, item_p item) { const node_p node = NG_HOOK_NODE(hook); const priv_p priv = NG_NODE_PRIVATE(node); if (!priv->conf.enableIP) { NG_FREE_ITEM(item); return (ENXIO); } return (ng_ppp_hcomp_xmit(NG_HOOK_NODE(hook), item, PROT_IP)); } /* * Receive data on a hook ipv6. */ static int ng_ppp_rcvdata_ipv6(hook_p hook, item_p item) { const node_p node = NG_HOOK_NODE(hook); const priv_p priv = NG_NODE_PRIVATE(node); if (!priv->conf.enableIPv6) { NG_FREE_ITEM(item); return (ENXIO); } return (ng_ppp_hcomp_xmit(NG_HOOK_NODE(hook), item, PROT_IPV6)); } /* * Receive data on a hook atalk. */ static int ng_ppp_rcvdata_atalk(hook_p hook, item_p item) { const node_p node = NG_HOOK_NODE(hook); const priv_p priv = NG_NODE_PRIVATE(node); if (!priv->conf.enableAtalk) { NG_FREE_ITEM(item); return (ENXIO); } return (ng_ppp_hcomp_xmit(NG_HOOK_NODE(hook), item, PROT_ATALK)); } /* * Receive data on a hook ipx */ static int ng_ppp_rcvdata_ipx(hook_p hook, item_p item) { const node_p node = NG_HOOK_NODE(hook); const priv_p priv = NG_NODE_PRIVATE(node); if (!priv->conf.enableIPX) { NG_FREE_ITEM(item); return (ENXIO); } return (ng_ppp_hcomp_xmit(NG_HOOK_NODE(hook), item, PROT_IPX)); } /* * Receive data on a hook bypass */ static int ng_ppp_rcvdata_bypass(hook_p hook, item_p item) { uint16_t linkNum; uint16_t proto; struct mbuf *m; NGI_GET_M(item, m); if (m->m_pkthdr.len < 4) { NG_FREE_ITEM(item); return (EINVAL); } if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) { NG_FREE_ITEM(item); return (ENOBUFS); } - linkNum = ntohs(mtod(m, uint16_t *)[0]); - proto = ntohs(mtod(m, uint16_t *)[1]); + linkNum = be16dec(mtod(m, uint8_t *)); + proto = be16dec(mtod(m, uint8_t *) + 2); m_adj(m, 4); NGI_M(item) = m; if (linkNum == NG_PPP_BUNDLE_LINKNUM) return (ng_ppp_hcomp_xmit(NG_HOOK_NODE(hook), item, proto)); else return (ng_ppp_link_xmit(NG_HOOK_NODE(hook), item, proto, linkNum, 0)); } static int ng_ppp_bypass(node_p node, item_p item, uint16_t proto, uint16_t linkNum) { const priv_p priv = NG_NODE_PRIVATE(node); uint16_t hdr[2]; struct mbuf *m; int error; if (priv->hooks[HOOK_INDEX_BYPASS] == NULL) { NG_FREE_ITEM(item); return (ENXIO); } /* Add 4-byte bypass header. */ hdr[0] = htons(linkNum); hdr[1] = htons(proto); NGI_GET_M(item, m); if ((m = ng_ppp_prepend(m, &hdr, 4)) == NULL) { NG_FREE_ITEM(item); return (ENOBUFS); } NGI_M(item) = m; /* Send packet out hook. */ NG_FWD_ITEM_HOOK(error, item, priv->hooks[HOOK_INDEX_BYPASS]); return (error); } static int ng_ppp_proto_recv(node_p node, item_p item, uint16_t proto, uint16_t linkNum) { const priv_p priv = NG_NODE_PRIVATE(node); hook_p outHook = NULL; int error; #ifdef ALIGNED_POINTER struct mbuf *m, *n; NGI_GET_M(item, m); if (!ALIGNED_POINTER(mtod(m, caddr_t), uint32_t)) { n = m_defrag(m, M_NOWAIT); if (n == NULL) { m_freem(m); NG_FREE_ITEM(item); return (ENOBUFS); } m = n; } NGI_M(item) = m; #endif /* ALIGNED_POINTER */ switch (proto) { case PROT_IP: if (priv->conf.enableIP) outHook = priv->hooks[HOOK_INDEX_INET]; break; case PROT_IPV6: if (priv->conf.enableIPv6) outHook = priv->hooks[HOOK_INDEX_IPV6]; break; case PROT_ATALK: if (priv->conf.enableAtalk) outHook = priv->hooks[HOOK_INDEX_ATALK]; break; case PROT_IPX: if (priv->conf.enableIPX) outHook = priv->hooks[HOOK_INDEX_IPX]; break; } if (outHook == NULL) return (ng_ppp_bypass(node, item, proto, linkNum)); /* Send packet out hook. */ NG_FWD_ITEM_HOOK(error, item, outHook); return (error); } /* * Header compression layer */ static int ng_ppp_hcomp_xmit(node_p node, item_p item, uint16_t proto) { const priv_p priv = NG_NODE_PRIVATE(node); if (proto == PROT_IP && priv->conf.enableVJCompression && priv->vjCompHooked) { int error; /* Send packet out hook. */ NG_FWD_ITEM_HOOK(error, item, priv->hooks[HOOK_INDEX_VJC_IP]); return (error); } return (ng_ppp_comp_xmit(node, item, proto)); } /* * Receive data on a hook vjc_comp. */ static int ng_ppp_rcvdata_vjc_comp(hook_p hook, item_p item) { const node_p node = NG_HOOK_NODE(hook); const priv_p priv = NG_NODE_PRIVATE(node); if (!priv->conf.enableVJCompression) { NG_FREE_ITEM(item); return (ENXIO); } return (ng_ppp_comp_xmit(node, item, PROT_VJCOMP)); } /* * Receive data on a hook vjc_uncomp. */ static int ng_ppp_rcvdata_vjc_uncomp(hook_p hook, item_p item) { const node_p node = NG_HOOK_NODE(hook); const priv_p priv = NG_NODE_PRIVATE(node); if (!priv->conf.enableVJCompression) { NG_FREE_ITEM(item); return (ENXIO); } return (ng_ppp_comp_xmit(node, item, PROT_VJUNCOMP)); } /* * Receive data on a hook vjc_vjip. */ static int ng_ppp_rcvdata_vjc_vjip(hook_p hook, item_p item) { const node_p node = NG_HOOK_NODE(hook); const priv_p priv = NG_NODE_PRIVATE(node); if (!priv->conf.enableVJCompression) { NG_FREE_ITEM(item); return (ENXIO); } return (ng_ppp_comp_xmit(node, item, PROT_IP)); } static int ng_ppp_hcomp_recv(node_p node, item_p item, uint16_t proto, uint16_t linkNum) { const priv_p priv = NG_NODE_PRIVATE(node); if (priv->conf.enableVJDecompression && priv->vjCompHooked) { hook_p outHook = NULL; switch (proto) { case PROT_VJCOMP: outHook = priv->hooks[HOOK_INDEX_VJC_COMP]; break; case PROT_VJUNCOMP: outHook = priv->hooks[HOOK_INDEX_VJC_UNCOMP]; break; } if (outHook) { int error; /* Send packet out hook. */ NG_FWD_ITEM_HOOK(error, item, outHook); return (error); } } return (ng_ppp_proto_recv(node, item, proto, linkNum)); } /* * Receive data on a hook vjc_ip. */ static int ng_ppp_rcvdata_vjc_ip(hook_p hook, item_p item) { const node_p node = NG_HOOK_NODE(hook); const priv_p priv = NG_NODE_PRIVATE(node); if (!priv->conf.enableVJDecompression) { NG_FREE_ITEM(item); return (ENXIO); } return (ng_ppp_proto_recv(node, item, PROT_IP, NG_PPP_BUNDLE_LINKNUM)); } /* * Compression layer */ static int ng_ppp_comp_xmit(node_p node, item_p item, uint16_t proto) { const priv_p priv = NG_NODE_PRIVATE(node); if (priv->conf.enableCompression && proto < 0x4000 && proto != PROT_COMPD && proto != PROT_CRYPTD && priv->hooks[HOOK_INDEX_COMPRESS] != NULL) { struct mbuf *m; int error; NGI_GET_M(item, m); if ((m = ng_ppp_addproto(m, proto, 0)) == NULL) { NG_FREE_ITEM(item); return (ENOBUFS); } NGI_M(item) = m; /* Send packet out hook. */ NG_FWD_ITEM_HOOK(error, item, priv->hooks[HOOK_INDEX_COMPRESS]); return (error); } return (ng_ppp_crypt_xmit(node, item, proto)); } /* * Receive data on a hook compress. */ static int ng_ppp_rcvdata_compress(hook_p hook, item_p item) { const node_p node = NG_HOOK_NODE(hook); const priv_p priv = NG_NODE_PRIVATE(node); uint16_t proto; switch (priv->conf.enableCompression) { case NG_PPP_COMPRESS_NONE: NG_FREE_ITEM(item); return (ENXIO); case NG_PPP_COMPRESS_FULL: { struct mbuf *m; NGI_GET_M(item, m); if ((m = ng_ppp_cutproto(m, &proto)) == NULL) { NG_FREE_ITEM(item); return (EIO); } NGI_M(item) = m; if (!PROT_VALID(proto)) { NG_FREE_ITEM(item); return (EIO); } } break; default: proto = PROT_COMPD; break; } return (ng_ppp_crypt_xmit(node, item, proto)); } static int ng_ppp_comp_recv(node_p node, item_p item, uint16_t proto, uint16_t linkNum) { const priv_p priv = NG_NODE_PRIVATE(node); if (proto < 0x4000 && ((proto == PROT_COMPD && priv->conf.enableDecompression) || priv->conf.enableDecompression == NG_PPP_DECOMPRESS_FULL) && priv->hooks[HOOK_INDEX_DECOMPRESS] != NULL) { int error; if (priv->conf.enableDecompression == NG_PPP_DECOMPRESS_FULL) { struct mbuf *m; NGI_GET_M(item, m); if ((m = ng_ppp_addproto(m, proto, 0)) == NULL) { NG_FREE_ITEM(item); return (EIO); } NGI_M(item) = m; } /* Send packet out hook. */ NG_FWD_ITEM_HOOK(error, item, priv->hooks[HOOK_INDEX_DECOMPRESS]); return (error); } else if (proto == PROT_COMPD) { /* Disabled protos MUST be silently discarded, but * unsupported MUST not. Let user-level decide this. */ return (ng_ppp_bypass(node, item, proto, linkNum)); } return (ng_ppp_hcomp_recv(node, item, proto, linkNum)); } /* * Receive data on a hook decompress. */ static int ng_ppp_rcvdata_decompress(hook_p hook, item_p item) { const node_p node = NG_HOOK_NODE(hook); const priv_p priv = NG_NODE_PRIVATE(node); uint16_t proto; struct mbuf *m; if (!priv->conf.enableDecompression) { NG_FREE_ITEM(item); return (ENXIO); } NGI_GET_M(item, m); if ((m = ng_ppp_cutproto(m, &proto)) == NULL) { NG_FREE_ITEM(item); return (EIO); } NGI_M(item) = m; if (!PROT_VALID(proto)) { priv->bundleStats.badProtos++; NG_FREE_ITEM(item); return (EIO); } return (ng_ppp_hcomp_recv(node, item, proto, NG_PPP_BUNDLE_LINKNUM)); } /* * Encryption layer */ static int ng_ppp_crypt_xmit(node_p node, item_p item, uint16_t proto) { const priv_p priv = NG_NODE_PRIVATE(node); if (priv->conf.enableEncryption && proto < 0x4000 && proto != PROT_CRYPTD && priv->hooks[HOOK_INDEX_ENCRYPT] != NULL) { struct mbuf *m; int error; NGI_GET_M(item, m); if ((m = ng_ppp_addproto(m, proto, 0)) == NULL) { NG_FREE_ITEM(item); return (ENOBUFS); } NGI_M(item) = m; /* Send packet out hook. */ NG_FWD_ITEM_HOOK(error, item, priv->hooks[HOOK_INDEX_ENCRYPT]); return (error); } return (ng_ppp_mp_xmit(node, item, proto)); } /* * Receive data on a hook encrypt. */ static int ng_ppp_rcvdata_encrypt(hook_p hook, item_p item) { const node_p node = NG_HOOK_NODE(hook); const priv_p priv = NG_NODE_PRIVATE(node); if (!priv->conf.enableEncryption) { NG_FREE_ITEM(item); return (ENXIO); } return (ng_ppp_mp_xmit(node, item, PROT_CRYPTD)); } static int ng_ppp_crypt_recv(node_p node, item_p item, uint16_t proto, uint16_t linkNum) { const priv_p priv = NG_NODE_PRIVATE(node); if (proto == PROT_CRYPTD) { if (priv->conf.enableDecryption && priv->hooks[HOOK_INDEX_DECRYPT] != NULL) { int error; /* Send packet out hook. */ NG_FWD_ITEM_HOOK(error, item, priv->hooks[HOOK_INDEX_DECRYPT]); return (error); } else { /* Disabled protos MUST be silently discarded, but * unsupported MUST not. Let user-level decide this. */ return (ng_ppp_bypass(node, item, proto, linkNum)); } } return (ng_ppp_comp_recv(node, item, proto, linkNum)); } /* * Receive data on a hook decrypt. */ static int ng_ppp_rcvdata_decrypt(hook_p hook, item_p item) { const node_p node = NG_HOOK_NODE(hook); const priv_p priv = NG_NODE_PRIVATE(node); uint16_t proto; struct mbuf *m; if (!priv->conf.enableDecryption) { NG_FREE_ITEM(item); return (ENXIO); } NGI_GET_M(item, m); if ((m = ng_ppp_cutproto(m, &proto)) == NULL) { NG_FREE_ITEM(item); return (EIO); } NGI_M(item) = m; if (!PROT_VALID(proto)) { priv->bundleStats.badProtos++; NG_FREE_ITEM(item); return (EIO); } return (ng_ppp_comp_recv(node, item, proto, NG_PPP_BUNDLE_LINKNUM)); } /* * Link layer */ static int ng_ppp_link_xmit(node_p node, item_p item, uint16_t proto, uint16_t linkNum, int plen) { const priv_p priv = NG_NODE_PRIVATE(node); struct ng_ppp_link *link; int len, error; struct mbuf *m; uint16_t mru; /* Check if link correct. */ if (linkNum >= NG_PPP_MAX_LINKS) { ERROUT(ENETDOWN); } /* Get link pointer (optimization). */ link = &priv->links[linkNum]; /* Check link status (if real). */ if (link->hook == NULL) { ERROUT(ENETDOWN); } /* Extract mbuf. */ NGI_GET_M(item, m); /* Check peer's MRU for this link. */ mru = link->conf.mru; if (mru != 0 && m->m_pkthdr.len > mru) { NG_FREE_M(m); ERROUT(EMSGSIZE); } /* Prepend protocol number, possibly compressed. */ if ((m = ng_ppp_addproto(m, proto, link->conf.enableProtoComp)) == NULL) { ERROUT(ENOBUFS); } /* Prepend address and control field (unless compressed). */ if (proto == PROT_LCP || !link->conf.enableACFComp) { if ((m = ng_ppp_prepend(m, &ng_ppp_acf, 2)) == NULL) ERROUT(ENOBUFS); } /* Deliver frame. */ len = m->m_pkthdr.len; NG_FWD_NEW_DATA(error, item, link->hook, m); mtx_lock(&priv->xmtx); /* Update link stats. */ link->stats.xmitFrames++; link->stats.xmitOctets += len; /* Update bundle stats. */ if (plen > 0) { priv->bundleStats.xmitFrames++; priv->bundleStats.xmitOctets += plen; } /* Update 'bytes in queue' counter. */ if (error == 0) { /* bytesInQueue and lastWrite required only for mp_strategy. */ if (priv->conf.enableMultilink && !priv->allLinksEqual && !priv->conf.enableRoundRobin) { /* If queue was empty, then mark this time. */ if (link->bytesInQueue == 0) getmicrouptime(&link->lastWrite); link->bytesInQueue += len + MP_AVERAGE_LINK_OVERHEAD; /* Limit max queue length to 50 pkts. BW can be defined incorrectly and link may not signal overload. */ if (link->bytesInQueue > 50 * 1600) link->bytesInQueue = 50 * 1600; } } mtx_unlock(&priv->xmtx); return (error); done: NG_FREE_ITEM(item); return (error); } /* * Receive data on a hook linkX. */ static int ng_ppp_rcvdata(hook_p hook, item_p item) { const node_p node = NG_HOOK_NODE(hook); const priv_p priv = NG_NODE_PRIVATE(node); const int index = (intptr_t)NG_HOOK_PRIVATE(hook); const uint16_t linkNum = (uint16_t)~index; struct ng_ppp_link * const link = &priv->links[linkNum]; uint16_t proto; struct mbuf *m; int error = 0; KASSERT(linkNum < NG_PPP_MAX_LINKS, ("%s: bogus index 0x%x", __func__, index)); NGI_GET_M(item, m); mtx_lock(&priv->rmtx); /* Stats */ link->stats.recvFrames++; link->stats.recvOctets += m->m_pkthdr.len; /* Strip address and control fields, if present. */ if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) ERROUT(ENOBUFS); if (mtod(m, uint8_t *)[0] == 0xff && mtod(m, uint8_t *)[1] == 0x03) m_adj(m, 2); /* Get protocol number */ if ((m = ng_ppp_cutproto(m, &proto)) == NULL) ERROUT(ENOBUFS); NGI_M(item) = m; /* Put changed m back into item. */ if (!PROT_VALID(proto)) { link->stats.badProtos++; ERROUT(EIO); } /* LCP packets must go directly to bypass. */ if (proto >= 0xB000) { mtx_unlock(&priv->rmtx); return (ng_ppp_bypass(node, item, proto, linkNum)); } /* Other packets are denied on a disabled link. */ if (!link->conf.enableLink) ERROUT(ENXIO); /* Proceed to multilink layer. Mutex will be unlocked inside. */ error = ng_ppp_mp_recv(node, item, proto, linkNum); mtx_assert(&priv->rmtx, MA_NOTOWNED); return (error); done: mtx_unlock(&priv->rmtx); NG_FREE_ITEM(item); return (error); } /* * Multilink layer */ /* * Handle an incoming multi-link fragment * * The fragment reassembly algorithm is somewhat complex. This is mainly * because we are required not to reorder the reconstructed packets, yet * fragments are only guaranteed to arrive in order on a per-link basis. * In other words, when we have a complete packet ready, but the previous * packet is still incomplete, we have to decide between delivering the * complete packet and throwing away the incomplete one, or waiting to * see if the remainder of the incomplete one arrives, at which time we * can deliver both packets, in order. * * This problem is exacerbated by "sequence number slew", which is when * the sequence numbers coming in from different links are far apart from * each other. In particular, certain unnamed equipment (*cough* Ascend) * has been seen to generate sequence number slew of up to 10 on an ISDN * 2B-channel MP link. There is nothing invalid about sequence number slew * but it makes the reasssembly process have to work harder. * * However, the peer is required to transmit fragments in order on each * link. That means if we define MSEQ as the minimum over all links of * the highest sequence number received on that link, then we can always * give up any hope of receiving a fragment with sequence number < MSEQ in * the future (all of this using 'wraparound' sequence number space). * Therefore we can always immediately throw away incomplete packets * missing fragments with sequence numbers < MSEQ. * * Here is an overview of our algorithm: * * o Received fragments are inserted into a queue, for which we * maintain these invariants between calls to this function: * * - Fragments are ordered in the queue by sequence number * - If a complete packet is at the head of the queue, then * the first fragment in the packet has seq# > MSEQ + 1 * (otherwise, we could deliver it immediately) * - If any fragments have seq# < MSEQ, then they are necessarily * part of a packet whose missing seq#'s are all > MSEQ (otherwise, * we can throw them away because they'll never be completed) * - The queue contains at most MP_MAX_QUEUE_LEN fragments * * o We have a periodic timer that checks the queue for the first * complete packet that has been sitting in the queue "too long". * When one is detected, all previous (incomplete) fragments are * discarded, their missing fragments are declared lost and MSEQ * is increased. * * o If we recieve a fragment with seq# < MSEQ, we throw it away * because we've already delcared it lost. * * This assumes linkNum != NG_PPP_BUNDLE_LINKNUM. */ static int ng_ppp_mp_recv(node_p node, item_p item, uint16_t proto, uint16_t linkNum) { const priv_p priv = NG_NODE_PRIVATE(node); struct ng_ppp_link *const link = &priv->links[linkNum]; struct ng_ppp_frag *frag; struct ng_ppp_frag *qent; int i, diff, inserted; struct mbuf *m; int error = 0; if ((!priv->conf.enableMultilink) || proto != PROT_MP) { /* Stats */ priv->bundleStats.recvFrames++; priv->bundleStats.recvOctets += NGI_M(item)->m_pkthdr.len; mtx_unlock(&priv->rmtx); return (ng_ppp_crypt_recv(node, item, proto, linkNum)); } NGI_GET_M(item, m); /* Get a new frag struct from the free queue */ if ((frag = TAILQ_FIRST(&priv->fragsfree)) == NULL) { printf("No free fragments headers in ng_ppp!\n"); NG_FREE_M(m); goto process; } /* Extract fragment information from MP header */ if (priv->conf.recvShortSeq) { uint16_t shdr; if (m->m_pkthdr.len < 2) { link->stats.runts++; NG_FREE_M(m); ERROUT(EINVAL); } if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) ERROUT(ENOBUFS); - shdr = ntohs(*mtod(m, uint16_t *)); + shdr = be16dec(mtod(m, void *)); frag->seq = MP_SHORT_EXTEND(shdr); frag->first = (shdr & MP_SHORT_FIRST_FLAG) != 0; frag->last = (shdr & MP_SHORT_LAST_FLAG) != 0; diff = MP_SHORT_SEQ_DIFF(frag->seq, priv->mseq); m_adj(m, 2); } else { uint32_t lhdr; if (m->m_pkthdr.len < 4) { link->stats.runts++; NG_FREE_M(m); ERROUT(EINVAL); } if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) ERROUT(ENOBUFS); - lhdr = ntohl(*mtod(m, uint32_t *)); + lhdr = be32dec(mtod(m, void *)); frag->seq = MP_LONG_EXTEND(lhdr); frag->first = (lhdr & MP_LONG_FIRST_FLAG) != 0; frag->last = (lhdr & MP_LONG_LAST_FLAG) != 0; diff = MP_LONG_SEQ_DIFF(frag->seq, priv->mseq); m_adj(m, 4); } frag->data = m; getmicrouptime(&frag->timestamp); /* If sequence number is < MSEQ, we've already declared this fragment as lost, so we have no choice now but to drop it */ if (diff < 0) { link->stats.dropFragments++; NG_FREE_M(m); ERROUT(0); } /* Update highest received sequence number on this link and MSEQ */ priv->mseq = link->seq = frag->seq; for (i = 0; i < priv->numActiveLinks; i++) { struct ng_ppp_link *const alink = &priv->links[priv->activeLinks[i]]; if (MP_RECV_SEQ_DIFF(priv, alink->seq, priv->mseq) < 0) priv->mseq = alink->seq; } /* Remove frag struct from free queue. */ TAILQ_REMOVE(&priv->fragsfree, frag, f_qent); /* Add fragment to queue, which is sorted by sequence number */ inserted = 0; TAILQ_FOREACH_REVERSE(qent, &priv->frags, ng_ppp_fraglist, f_qent) { diff = MP_RECV_SEQ_DIFF(priv, frag->seq, qent->seq); if (diff > 0) { TAILQ_INSERT_AFTER(&priv->frags, qent, frag, f_qent); inserted = 1; break; } else if (diff == 0) { /* should never happen! */ link->stats.dupFragments++; NG_FREE_M(frag->data); TAILQ_INSERT_HEAD(&priv->fragsfree, frag, f_qent); ERROUT(EINVAL); } } if (!inserted) TAILQ_INSERT_HEAD(&priv->frags, frag, f_qent); process: /* Process the queue */ /* NOTE: rmtx will be unlocked for sending time! */ error = ng_ppp_frag_process(node, item); mtx_unlock(&priv->rmtx); return (error); done: mtx_unlock(&priv->rmtx); NG_FREE_ITEM(item); return (error); } /************************************************************************ HELPER STUFF ************************************************************************/ /* * If new mseq > current then set it and update all active links */ static void ng_ppp_bump_mseq(node_p node, int32_t new_mseq) { const priv_p priv = NG_NODE_PRIVATE(node); int i; if (MP_RECV_SEQ_DIFF(priv, priv->mseq, new_mseq) < 0) { priv->mseq = new_mseq; for (i = 0; i < priv->numActiveLinks; i++) { struct ng_ppp_link *const alink = &priv->links[priv->activeLinks[i]]; if (MP_RECV_SEQ_DIFF(priv, alink->seq, new_mseq) < 0) alink->seq = new_mseq; } } } /* * Examine our list of fragments, and determine if there is a * complete and deliverable packet at the head of the list. * Return 1 if so, zero otherwise. */ static int ng_ppp_check_packet(node_p node) { const priv_p priv = NG_NODE_PRIVATE(node); struct ng_ppp_frag *qent, *qnext; /* Check for empty queue */ if (TAILQ_EMPTY(&priv->frags)) return (0); /* Check first fragment is the start of a deliverable packet */ qent = TAILQ_FIRST(&priv->frags); if (!qent->first || MP_RECV_SEQ_DIFF(priv, qent->seq, priv->mseq) > 1) return (0); /* Check that all the fragments are there */ while (!qent->last) { qnext = TAILQ_NEXT(qent, f_qent); if (qnext == NULL) /* end of queue */ return (0); if (qnext->seq != MP_NEXT_RECV_SEQ(priv, qent->seq)) return (0); qent = qnext; } /* Got one */ return (1); } /* * Pull a completed packet off the head of the incoming fragment queue. * This assumes there is a completed packet there to pull off. */ static void ng_ppp_get_packet(node_p node, struct mbuf **mp) { const priv_p priv = NG_NODE_PRIVATE(node); struct ng_ppp_frag *qent, *qnext; struct mbuf *m = NULL, *tail; qent = TAILQ_FIRST(&priv->frags); KASSERT(!TAILQ_EMPTY(&priv->frags) && qent->first, ("%s: no packet", __func__)); for (tail = NULL; qent != NULL; qent = qnext) { qnext = TAILQ_NEXT(qent, f_qent); KASSERT(!TAILQ_EMPTY(&priv->frags), ("%s: empty q", __func__)); TAILQ_REMOVE(&priv->frags, qent, f_qent); if (tail == NULL) tail = m = qent->data; else { m->m_pkthdr.len += qent->data->m_pkthdr.len; tail->m_next = qent->data; } while (tail->m_next != NULL) tail = tail->m_next; if (qent->last) { qnext = NULL; /* Bump MSEQ if necessary */ ng_ppp_bump_mseq(node, qent->seq); } TAILQ_INSERT_HEAD(&priv->fragsfree, qent, f_qent); } *mp = m; } /* * Trim fragments from the queue whose packets can never be completed. * This assumes a complete packet is NOT at the beginning of the queue. * Returns 1 if fragments were removed, zero otherwise. */ static int ng_ppp_frag_trim(node_p node) { const priv_p priv = NG_NODE_PRIVATE(node); struct ng_ppp_frag *qent, *qnext = NULL; int removed = 0; /* Scan for "dead" fragments and remove them */ while (1) { int dead = 0; /* If queue is empty, we're done */ if (TAILQ_EMPTY(&priv->frags)) break; /* Determine whether first fragment can ever be completed */ TAILQ_FOREACH(qent, &priv->frags, f_qent) { if (MP_RECV_SEQ_DIFF(priv, qent->seq, priv->mseq) >= 0) break; qnext = TAILQ_NEXT(qent, f_qent); KASSERT(qnext != NULL, ("%s: last frag < MSEQ?", __func__)); if (qnext->seq != MP_NEXT_RECV_SEQ(priv, qent->seq) || qent->last || qnext->first) { dead = 1; break; } } if (!dead) break; /* Remove fragment and all others in the same packet */ while ((qent = TAILQ_FIRST(&priv->frags)) != qnext) { KASSERT(!TAILQ_EMPTY(&priv->frags), ("%s: empty q", __func__)); priv->bundleStats.dropFragments++; TAILQ_REMOVE(&priv->frags, qent, f_qent); NG_FREE_M(qent->data); TAILQ_INSERT_HEAD(&priv->fragsfree, qent, f_qent); removed = 1; } } return (removed); } /* * Drop fragments on queue overflow. * Returns 1 if fragments were removed, zero otherwise. */ static int ng_ppp_frag_drop(node_p node) { const priv_p priv = NG_NODE_PRIVATE(node); /* Check queue length */ if (TAILQ_EMPTY(&priv->fragsfree)) { struct ng_ppp_frag *qent; /* Get oldest fragment */ KASSERT(!TAILQ_EMPTY(&priv->frags), ("%s: empty q", __func__)); qent = TAILQ_FIRST(&priv->frags); /* Bump MSEQ if necessary */ ng_ppp_bump_mseq(node, qent->seq); /* Drop it */ priv->bundleStats.dropFragments++; TAILQ_REMOVE(&priv->frags, qent, f_qent); NG_FREE_M(qent->data); TAILQ_INSERT_HEAD(&priv->fragsfree, qent, f_qent); return (1); } return (0); } /* * Run the queue, restoring the queue invariants */ static int ng_ppp_frag_process(node_p node, item_p oitem) { const priv_p priv = NG_NODE_PRIVATE(node); struct mbuf *m; item_p item; uint16_t proto; do { /* Deliver any deliverable packets */ while (ng_ppp_check_packet(node)) { ng_ppp_get_packet(node, &m); if ((m = ng_ppp_cutproto(m, &proto)) == NULL) continue; if (!PROT_VALID(proto)) { priv->bundleStats.badProtos++; NG_FREE_M(m); continue; } if (oitem) { /* If original item present - reuse it. */ item = oitem; oitem = NULL; NGI_M(item) = m; } else { item = ng_package_data(m, NG_NOFLAGS); } if (item != NULL) { /* Stats */ priv->bundleStats.recvFrames++; priv->bundleStats.recvOctets += NGI_M(item)->m_pkthdr.len; /* Drop mutex for the sending time. * Priv may change, but we are ready! */ mtx_unlock(&priv->rmtx); ng_ppp_crypt_recv(node, item, proto, NG_PPP_BUNDLE_LINKNUM); mtx_lock(&priv->rmtx); } } /* Delete dead fragments and try again */ } while (ng_ppp_frag_trim(node) || ng_ppp_frag_drop(node)); /* If we haven't reused original item - free it. */ if (oitem) NG_FREE_ITEM(oitem); /* Done */ return (0); } /* * Check for 'stale' completed packets that need to be delivered * * If a link goes down or has a temporary failure, MSEQ can get * "stuck", because no new incoming fragments appear on that link. * This can cause completed packets to never get delivered if * their sequence numbers are all > MSEQ + 1. * * This routine checks how long all of the completed packets have * been sitting in the queue, and if too long, removes fragments * from the queue and increments MSEQ to allow them to be delivered. */ static void ng_ppp_frag_checkstale(node_p node) { const priv_p priv = NG_NODE_PRIVATE(node); struct ng_ppp_frag *qent, *beg, *end; struct timeval now, age; struct mbuf *m; int seq; item_p item; int endseq; uint16_t proto; now.tv_sec = 0; /* uninitialized state */ while (1) { /* If queue is empty, we're done */ if (TAILQ_EMPTY(&priv->frags)) break; /* Find the first complete packet in the queue */ beg = end = NULL; seq = TAILQ_FIRST(&priv->frags)->seq; TAILQ_FOREACH(qent, &priv->frags, f_qent) { if (qent->first) beg = qent; else if (qent->seq != seq) beg = NULL; if (beg != NULL && qent->last) { end = qent; break; } seq = MP_NEXT_RECV_SEQ(priv, seq); } /* If none found, exit */ if (end == NULL) break; /* Get current time (we assume we've been up for >= 1 second) */ if (now.tv_sec == 0) getmicrouptime(&now); /* Check if packet has been queued too long */ age = now; timevalsub(&age, &beg->timestamp); if (timevalcmp(&age, &ng_ppp_max_staleness, < )) break; /* Throw away junk fragments in front of the completed packet */ while ((qent = TAILQ_FIRST(&priv->frags)) != beg) { KASSERT(!TAILQ_EMPTY(&priv->frags), ("%s: empty q", __func__)); priv->bundleStats.dropFragments++; TAILQ_REMOVE(&priv->frags, qent, f_qent); NG_FREE_M(qent->data); TAILQ_INSERT_HEAD(&priv->fragsfree, qent, f_qent); } /* Extract completed packet */ endseq = end->seq; ng_ppp_get_packet(node, &m); if ((m = ng_ppp_cutproto(m, &proto)) == NULL) continue; if (!PROT_VALID(proto)) { priv->bundleStats.badProtos++; NG_FREE_M(m); continue; } /* Deliver packet */ if ((item = ng_package_data(m, NG_NOFLAGS)) != NULL) { /* Stats */ priv->bundleStats.recvFrames++; priv->bundleStats.recvOctets += NGI_M(item)->m_pkthdr.len; ng_ppp_crypt_recv(node, item, proto, NG_PPP_BUNDLE_LINKNUM); } } } /* * Periodically call ng_ppp_frag_checkstale() */ static void ng_ppp_frag_timeout(node_p node, hook_p hook, void *arg1, int arg2) { /* XXX: is this needed? */ if (NG_NODE_NOT_VALID(node)) return; /* Scan the fragment queue */ ng_ppp_frag_checkstale(node); /* Start timer again */ ng_ppp_start_frag_timer(node); } /* * Deliver a frame out on the bundle, i.e., figure out how to fragment * the frame across the individual PPP links and do so. */ static int ng_ppp_mp_xmit(node_p node, item_p item, uint16_t proto) { const priv_p priv = NG_NODE_PRIVATE(node); const int hdr_len = priv->conf.xmitShortSeq ? 2 : 4; int distrib[NG_PPP_MAX_LINKS]; int firstFragment; int activeLinkNum; struct mbuf *m; int plen; int frags; int32_t seq; /* At least one link must be active */ if (priv->numActiveLinks == 0) { NG_FREE_ITEM(item); return (ENETDOWN); } /* Save length for later stats. */ plen = NGI_M(item)->m_pkthdr.len; if (!priv->conf.enableMultilink) { return (ng_ppp_link_xmit(node, item, proto, priv->activeLinks[0], plen)); } /* Check peer's MRRU for this bundle. */ if (plen > priv->conf.mrru) { NG_FREE_ITEM(item); return (EMSGSIZE); } /* Extract mbuf. */ NGI_GET_M(item, m); /* Prepend protocol number, possibly compressed. */ if ((m = ng_ppp_addproto(m, proto, 1)) == NULL) { NG_FREE_ITEM(item); return (ENOBUFS); } /* Clear distribution plan */ bzero(&distrib, priv->numActiveLinks * sizeof(distrib[0])); mtx_lock(&priv->xmtx); /* Round-robin strategy */ if (priv->conf.enableRoundRobin) { activeLinkNum = priv->lastLink++ % priv->numActiveLinks; distrib[activeLinkNum] = m->m_pkthdr.len; goto deliver; } /* Strategy when all links are equivalent (optimize the common case) */ if (priv->allLinksEqual) { int numFrags, fraction, remain; int i; /* Calculate optimal fragment count */ numFrags = priv->numActiveLinks; if (numFrags > m->m_pkthdr.len / MP_MIN_FRAG_LEN) numFrags = m->m_pkthdr.len / MP_MIN_FRAG_LEN; if (numFrags == 0) numFrags = 1; fraction = m->m_pkthdr.len / numFrags; remain = m->m_pkthdr.len - (fraction * numFrags); /* Assign distribution */ for (i = 0; i < numFrags; i++) { distrib[priv->lastLink++ % priv->numActiveLinks] = fraction + (((remain--) > 0)?1:0); } goto deliver; } /* Strategy when all links are not equivalent */ ng_ppp_mp_strategy(node, m->m_pkthdr.len, distrib); deliver: /* Estimate fragments count */ frags = 0; for (activeLinkNum = priv->numActiveLinks - 1; activeLinkNum >= 0; activeLinkNum--) { const uint16_t linkNum = priv->activeLinks[activeLinkNum]; struct ng_ppp_link *const link = &priv->links[linkNum]; frags += (distrib[activeLinkNum] + link->conf.mru - hdr_len - 1) / (link->conf.mru - hdr_len); } /* Get out initial sequence number */ seq = priv->xseq; /* Update next sequence number */ if (priv->conf.xmitShortSeq) { priv->xseq = (seq + frags) & MP_SHORT_SEQ_MASK; } else { priv->xseq = (seq + frags) & MP_LONG_SEQ_MASK; } mtx_unlock(&priv->xmtx); /* Send alloted portions of frame out on the link(s) */ for (firstFragment = 1, activeLinkNum = priv->numActiveLinks - 1; activeLinkNum >= 0; activeLinkNum--) { const uint16_t linkNum = priv->activeLinks[activeLinkNum]; struct ng_ppp_link *const link = &priv->links[linkNum]; /* Deliver fragment(s) out the next link */ for ( ; distrib[activeLinkNum] > 0; firstFragment = 0) { int len, lastFragment, error; struct mbuf *m2; /* Calculate fragment length; don't exceed link MTU */ len = distrib[activeLinkNum]; if (len > link->conf.mru - hdr_len) len = link->conf.mru - hdr_len; distrib[activeLinkNum] -= len; lastFragment = (len == m->m_pkthdr.len); /* Split off next fragment as "m2" */ m2 = m; if (!lastFragment) { struct mbuf *n = m_split(m, len, M_DONTWAIT); if (n == NULL) { NG_FREE_M(m); if (firstFragment) NG_FREE_ITEM(item); return (ENOMEM); } m_tag_copy_chain(n, m, M_DONTWAIT); m = n; } /* Prepend MP header */ if (priv->conf.xmitShortSeq) { uint16_t shdr; shdr = seq; seq = (seq + 1) & MP_SHORT_SEQ_MASK; if (firstFragment) shdr |= MP_SHORT_FIRST_FLAG; if (lastFragment) shdr |= MP_SHORT_LAST_FLAG; shdr = htons(shdr); m2 = ng_ppp_prepend(m2, &shdr, 2); } else { uint32_t lhdr; lhdr = seq; seq = (seq + 1) & MP_LONG_SEQ_MASK; if (firstFragment) lhdr |= MP_LONG_FIRST_FLAG; if (lastFragment) lhdr |= MP_LONG_LAST_FLAG; lhdr = htonl(lhdr); m2 = ng_ppp_prepend(m2, &lhdr, 4); } if (m2 == NULL) { if (!lastFragment) m_freem(m); if (firstFragment) NG_FREE_ITEM(item); return (ENOBUFS); } /* Send fragment */ if (firstFragment) { NGI_M(item) = m2; /* Reuse original item. */ } else { item = ng_package_data(m2, NG_NOFLAGS); } if (item != NULL) { error = ng_ppp_link_xmit(node, item, PROT_MP, linkNum, (firstFragment?plen:0)); if (error != 0) { if (!lastFragment) NG_FREE_M(m); return (error); } } } } /* Done */ return (0); } /* * Computing the optimal fragmentation * ----------------------------------- * * This routine tries to compute the optimal fragmentation pattern based * on each link's latency, bandwidth, and calculated additional latency. * The latter quantity is the additional latency caused by previously * written data that has not been transmitted yet. * * This algorithm is only useful when not all of the links have the * same latency and bandwidth values. * * The essential idea is to make the last bit of each fragment of the * frame arrive at the opposite end at the exact same time. This greedy * algorithm is optimal, in that no other scheduling could result in any * packet arriving any sooner unless packets are delivered out of order. * * Suppose link i has bandwidth b_i (in tens of bytes per milisecond) and * latency l_i (in miliseconds). Consider the function function f_i(t) * which is equal to the number of bytes that will have arrived at * the peer after t miliseconds if we start writing continuously at * time t = 0. Then f_i(t) = b_i * (t - l_i) = ((b_i * t) - (l_i * b_i). * That is, f_i(t) is a line with slope b_i and y-intersect -(l_i * b_i). * Note that the y-intersect is always <= zero because latency can't be * negative. Note also that really the function is f_i(t) except when * f_i(t) is negative, in which case the function is zero. To take * care of this, let Q_i(t) = { if (f_i(t) > 0) return 1; else return 0; }. * So the actual number of bytes that will have arrived at the peer after * t miliseconds is f_i(t) * Q_i(t). * * At any given time, each link has some additional latency a_i >= 0 * due to previously written fragment(s) which are still in the queue. * This value is easily computed from the time since last transmission, * the previous latency value, the number of bytes written, and the * link's bandwidth. * * Assume that l_i includes any a_i already, and that the links are * sorted by latency, so that l_i <= l_{i+1}. * * Let N be the total number of bytes in the current frame we are sending. * * Suppose we were to start writing bytes at time t = 0 on all links * simultaneously, which is the most we can possibly do. Then let * F(t) be equal to the total number of bytes received by the peer * after t miliseconds. Then F(t) = Sum_i (f_i(t) * Q_i(t)). * * Our goal is simply this: fragment the frame across the links such * that the peer is able to reconstruct the completed frame as soon as * possible, i.e., at the least possible value of t. Call this value t_0. * * Then it follows that F(t_0) = N. Our strategy is first to find the value * of t_0, and then deduce how many bytes to write to each link. * * Rewriting F(t_0): * * t_0 = ( N + Sum_i ( l_i * b_i * Q_i(t_0) ) ) / Sum_i ( b_i * Q_i(t_0) ) * * Now, we note that Q_i(t) is constant for l_i <= t <= l_{i+1}. t_0 will * lie in one of these ranges. To find it, we just need to find the i such * that F(l_i) <= N <= F(l_{i+1}). Then we compute all the constant values * for Q_i() in this range, plug in the remaining values, solving for t_0. * * Once t_0 is known, then the number of bytes to send on link i is * just f_i(t_0) * Q_i(t_0). * * In other words, we start allocating bytes to the links one at a time. * We keep adding links until the frame is completely sent. Some links * may not get any bytes because their latency is too high. * * Is all this work really worth the trouble? Depends on the situation. * The bigger the ratio of computer speed to link speed, and the more * important total bundle latency is (e.g., for interactive response time), * the more it's worth it. There is however the cost of calling this * function for every frame. The running time is O(n^2) where n is the * number of links that receive a non-zero number of bytes. * * Since latency is measured in miliseconds, the "resolution" of this * algorithm is one milisecond. * * To avoid this algorithm altogether, configure all links to have the * same latency and bandwidth. */ static void ng_ppp_mp_strategy(node_p node, int len, int *distrib) { const priv_p priv = NG_NODE_PRIVATE(node); int latency[NG_PPP_MAX_LINKS]; int sortByLatency[NG_PPP_MAX_LINKS]; int activeLinkNum; int t0, total, topSum, botSum; struct timeval now; int i, numFragments; /* If only one link, this gets real easy */ if (priv->numActiveLinks == 1) { distrib[0] = len; return; } /* Get current time */ getmicrouptime(&now); /* Compute latencies for each link at this point in time */ for (activeLinkNum = 0; activeLinkNum < priv->numActiveLinks; activeLinkNum++) { struct ng_ppp_link *alink; struct timeval diff; int xmitBytes; /* Start with base latency value */ alink = &priv->links[priv->activeLinks[activeLinkNum]]; latency[activeLinkNum] = alink->latency; sortByLatency[activeLinkNum] = activeLinkNum; /* see below */ /* Any additional latency? */ if (alink->bytesInQueue == 0) continue; /* Compute time delta since last write */ diff = now; timevalsub(&diff, &alink->lastWrite); /* alink->bytesInQueue will be changed, mark change time. */ alink->lastWrite = now; if (now.tv_sec < 0 || diff.tv_sec >= 10) { /* sanity */ alink->bytesInQueue = 0; continue; } /* How many bytes could have transmitted since last write? */ xmitBytes = (alink->conf.bandwidth * 10 * diff.tv_sec) + (alink->conf.bandwidth * (diff.tv_usec / 1000)) / 100; alink->bytesInQueue -= xmitBytes; if (alink->bytesInQueue < 0) alink->bytesInQueue = 0; else latency[activeLinkNum] += (100 * alink->bytesInQueue) / alink->conf.bandwidth; } /* Sort active links by latency */ qsort_r(sortByLatency, priv->numActiveLinks, sizeof(*sortByLatency), latency, ng_ppp_intcmp); /* Find the interval we need (add links in sortByLatency[] order) */ for (numFragments = 1; numFragments < priv->numActiveLinks; numFragments++) { for (total = i = 0; i < numFragments; i++) { int flowTime; flowTime = latency[sortByLatency[numFragments]] - latency[sortByLatency[i]]; total += ((flowTime * priv->links[ priv->activeLinks[sortByLatency[i]]].conf.bandwidth) + 99) / 100; } if (total >= len) break; } /* Solve for t_0 in that interval */ for (topSum = botSum = i = 0; i < numFragments; i++) { int bw = priv->links[ priv->activeLinks[sortByLatency[i]]].conf.bandwidth; topSum += latency[sortByLatency[i]] * bw; /* / 100 */ botSum += bw; /* / 100 */ } t0 = ((len * 100) + topSum + botSum / 2) / botSum; /* Compute f_i(t_0) all i */ for (total = i = 0; i < numFragments; i++) { int bw = priv->links[ priv->activeLinks[sortByLatency[i]]].conf.bandwidth; distrib[sortByLatency[i]] = (bw * (t0 - latency[sortByLatency[i]]) + 50) / 100; total += distrib[sortByLatency[i]]; } /* Deal with any rounding error */ if (total < len) { struct ng_ppp_link *fastLink = &priv->links[priv->activeLinks[sortByLatency[0]]]; int fast = 0; /* Find the fastest link */ for (i = 1; i < numFragments; i++) { struct ng_ppp_link *const link = &priv->links[priv->activeLinks[sortByLatency[i]]]; if (link->conf.bandwidth > fastLink->conf.bandwidth) { fast = i; fastLink = link; } } distrib[sortByLatency[fast]] += len - total; } else while (total > len) { struct ng_ppp_link *slowLink = &priv->links[priv->activeLinks[sortByLatency[0]]]; int delta, slow = 0; /* Find the slowest link that still has bytes to remove */ for (i = 1; i < numFragments; i++) { struct ng_ppp_link *const link = &priv->links[priv->activeLinks[sortByLatency[i]]]; if (distrib[sortByLatency[slow]] == 0 || (distrib[sortByLatency[i]] > 0 && link->conf.bandwidth < slowLink->conf.bandwidth)) { slow = i; slowLink = link; } } delta = total - len; if (delta > distrib[sortByLatency[slow]]) delta = distrib[sortByLatency[slow]]; distrib[sortByLatency[slow]] -= delta; total -= delta; } } /* * Compare two integers */ static int ng_ppp_intcmp(void *latency, const void *v1, const void *v2) { const int index1 = *((const int *) v1); const int index2 = *((const int *) v2); return ((int *)latency)[index1] - ((int *)latency)[index2]; } /* * Prepend a possibly compressed PPP protocol number in front of a frame */ static struct mbuf * ng_ppp_addproto(struct mbuf *m, uint16_t proto, int compOK) { if (compOK && PROT_COMPRESSABLE(proto)) { uint8_t pbyte = (uint8_t)proto; return ng_ppp_prepend(m, &pbyte, 1); } else { uint16_t pword = htons((uint16_t)proto); return ng_ppp_prepend(m, &pword, 2); } } /* * Cut a possibly compressed PPP protocol number from the front of a frame. */ static struct mbuf * ng_ppp_cutproto(struct mbuf *m, uint16_t *proto) { *proto = 0; if (m->m_len < 1 && (m = m_pullup(m, 1)) == NULL) return (NULL); *proto = *mtod(m, uint8_t *); m_adj(m, 1); if (!PROT_VALID(*proto)) { if (m->m_len < 1 && (m = m_pullup(m, 1)) == NULL) return (NULL); *proto = (*proto << 8) + *mtod(m, uint8_t *); m_adj(m, 1); } return (m); } /* * Prepend some bytes to an mbuf. */ static struct mbuf * ng_ppp_prepend(struct mbuf *m, const void *buf, int len) { M_PREPEND(m, len, M_DONTWAIT); if (m == NULL || (m->m_len < len && (m = m_pullup(m, len)) == NULL)) return (NULL); bcopy(buf, mtod(m, uint8_t *), len); return (m); } /* * Update private information that is derived from other private information */ static void ng_ppp_update(node_p node, int newConf) { const priv_p priv = NG_NODE_PRIVATE(node); int i; /* Update active status for VJ Compression */ priv->vjCompHooked = priv->hooks[HOOK_INDEX_VJC_IP] != NULL && priv->hooks[HOOK_INDEX_VJC_COMP] != NULL && priv->hooks[HOOK_INDEX_VJC_UNCOMP] != NULL && priv->hooks[HOOK_INDEX_VJC_VJIP] != NULL; /* Increase latency for each link an amount equal to one MP header */ if (newConf) { for (i = 0; i < NG_PPP_MAX_LINKS; i++) { int hdrBytes; if (priv->links[i].conf.bandwidth == 0) continue; hdrBytes = MP_AVERAGE_LINK_OVERHEAD + (priv->links[i].conf.enableACFComp ? 0 : 2) + (priv->links[i].conf.enableProtoComp ? 1 : 2) + (priv->conf.xmitShortSeq ? 2 : 4); priv->links[i].latency = priv->links[i].conf.latency + (hdrBytes / priv->links[i].conf.bandwidth + 50) / 100; } } /* Update list of active links */ bzero(&priv->activeLinks, sizeof(priv->activeLinks)); priv->numActiveLinks = 0; priv->allLinksEqual = 1; for (i = 0; i < NG_PPP_MAX_LINKS; i++) { struct ng_ppp_link *const link = &priv->links[i]; /* Is link active? */ if (link->conf.enableLink && link->hook != NULL) { struct ng_ppp_link *link0; /* Add link to list of active links */ priv->activeLinks[priv->numActiveLinks++] = i; link0 = &priv->links[priv->activeLinks[0]]; /* Determine if all links are still equal */ if (link->latency != link0->latency || link->conf.bandwidth != link0->conf.bandwidth) priv->allLinksEqual = 0; /* Initialize rec'd sequence number */ if (link->seq == MP_NOSEQ) { link->seq = (link == link0) ? MP_INITIAL_SEQ : link0->seq; } } else link->seq = MP_NOSEQ; } /* Update MP state as multi-link is active or not */ if (priv->conf.enableMultilink && priv->numActiveLinks > 0) ng_ppp_start_frag_timer(node); else { ng_ppp_stop_frag_timer(node); ng_ppp_frag_reset(node); priv->xseq = MP_INITIAL_SEQ; priv->mseq = MP_INITIAL_SEQ; for (i = 0; i < NG_PPP_MAX_LINKS; i++) { struct ng_ppp_link *const link = &priv->links[i]; bzero(&link->lastWrite, sizeof(link->lastWrite)); link->bytesInQueue = 0; link->seq = MP_NOSEQ; } } } /* * Determine if a new configuration would represent a valid change * from the current configuration and link activity status. */ static int ng_ppp_config_valid(node_p node, const struct ng_ppp_node_conf *newConf) { const priv_p priv = NG_NODE_PRIVATE(node); int i, newNumLinksActive; /* Check per-link config and count how many links would be active */ for (newNumLinksActive = i = 0; i < NG_PPP_MAX_LINKS; i++) { if (newConf->links[i].enableLink && priv->links[i].hook != NULL) newNumLinksActive++; if (!newConf->links[i].enableLink) continue; if (newConf->links[i].mru < MP_MIN_LINK_MRU) return (0); if (newConf->links[i].bandwidth == 0) return (0); if (newConf->links[i].bandwidth > NG_PPP_MAX_BANDWIDTH) return (0); if (newConf->links[i].latency > NG_PPP_MAX_LATENCY) return (0); } /* Disallow changes to multi-link configuration while MP is active */ if (priv->numActiveLinks > 0 && newNumLinksActive > 0) { if (!priv->conf.enableMultilink != !newConf->bund.enableMultilink || !priv->conf.xmitShortSeq != !newConf->bund.xmitShortSeq || !priv->conf.recvShortSeq != !newConf->bund.recvShortSeq) return (0); } /* At most one link can be active unless multi-link is enabled */ if (!newConf->bund.enableMultilink && newNumLinksActive > 1) return (0); /* Configuration change would be valid */ return (1); } /* * Free all entries in the fragment queue */ static void ng_ppp_frag_reset(node_p node) { const priv_p priv = NG_NODE_PRIVATE(node); struct ng_ppp_frag *qent, *qnext; for (qent = TAILQ_FIRST(&priv->frags); qent; qent = qnext) { qnext = TAILQ_NEXT(qent, f_qent); NG_FREE_M(qent->data); TAILQ_INSERT_HEAD(&priv->fragsfree, qent, f_qent); } TAILQ_INIT(&priv->frags); } /* * Start fragment queue timer */ static void ng_ppp_start_frag_timer(node_p node) { const priv_p priv = NG_NODE_PRIVATE(node); if (!(callout_pending(&priv->fragTimer))) ng_callout(&priv->fragTimer, node, NULL, MP_FRAGTIMER_INTERVAL, ng_ppp_frag_timeout, NULL, 0); } /* * Stop fragment queue timer */ static void ng_ppp_stop_frag_timer(node_p node) { const priv_p priv = NG_NODE_PRIVATE(node); if (callout_pending(&priv->fragTimer)) ng_uncallout(&priv->fragTimer, node); } Index: stable/8/sys/netgraph/ng_pptpgre.c =================================================================== --- stable/8/sys/netgraph/ng_pptpgre.c (revision 206660) +++ stable/8/sys/netgraph/ng_pptpgre.c (revision 206661) @@ -1,985 +1,985 @@ /* * ng_pptpgre.c */ /*- * Copyright (c) 1996-1999 Whistle Communications, Inc. * All rights reserved. * * Subject to the following obligations and disclaimer of warranty, use and * redistribution of this software, in source or object code forms, with or * without modifications are expressly permitted by Whistle Communications; * provided, however, that: * 1. Any and all reproductions of the source or object code must include the * copyright notice above and the following disclaimer of warranties; and * 2. No rights are granted, in any manner or form, to use Whistle * Communications, Inc. trademarks, including the mark "WHISTLE * COMMUNICATIONS" on advertising, endorsements, or otherwise except as * such appears in the above copyright notice or in the software. * * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY * OF SUCH DAMAGE. * * Author: Archie Cobbs * * $FreeBSD$ * $Whistle: ng_pptpgre.c,v 1.7 1999/12/08 00:10:06 archie Exp $ */ /* * PPTP/GRE netgraph node type. * * This node type does the GRE encapsulation as specified for the PPTP * protocol (RFC 2637, section 4). This includes sequencing and * retransmission of frames, but not the actual packet delivery nor * any of the TCP control stream protocol. * * The "upper" hook of this node is suitable for attaching to a "ppp" * node link hook. The "lower" hook of this node is suitable for attaching * to a "ksocket" node on hook "inet/raw/gre". */ #include #include #include #include #include #include #include #include +#include #include #include #include #include #include #include #include #include /* GRE packet format, as used by PPTP */ struct greheader { #if BYTE_ORDER == LITTLE_ENDIAN u_char recursion:3; /* recursion control */ u_char ssr:1; /* strict source route */ u_char hasSeq:1; /* sequence number present */ u_char hasKey:1; /* key present */ u_char hasRoute:1; /* routing present */ u_char hasSum:1; /* checksum present */ u_char vers:3; /* version */ u_char flags:4; /* flags */ u_char hasAck:1; /* acknowlege number present */ #elif BYTE_ORDER == BIG_ENDIAN u_char hasSum:1; /* checksum present */ u_char hasRoute:1; /* routing present */ u_char hasKey:1; /* key present */ u_char hasSeq:1; /* sequence number present */ u_char ssr:1; /* strict source route */ u_char recursion:3; /* recursion control */ u_char hasAck:1; /* acknowlege number present */ u_char flags:4; /* flags */ u_char vers:3; /* version */ #else #error BYTE_ORDER is not defined properly #endif u_int16_t proto; /* protocol (ethertype) */ u_int16_t length; /* payload length */ u_int16_t cid; /* call id */ u_int32_t data[0]; /* opt. seq, ack, then data */ }; /* The PPTP protocol ID used in the GRE 'proto' field */ #define PPTP_GRE_PROTO 0x880b /* Bits that must be set a certain way in all PPTP/GRE packets */ #define PPTP_INIT_VALUE ((0x2001 << 16) | PPTP_GRE_PROTO) #define PPTP_INIT_MASK 0xef7fffff /* Min and max packet length */ #define PPTP_MAX_PAYLOAD (0xffff - sizeof(struct greheader) - 8) /* All times are scaled by this (PPTP_TIME_SCALE time units = 1 sec.) */ #define PPTP_TIME_SCALE 1024 /* milliseconds */ typedef u_int64_t pptptime_t; /* Acknowledgment timeout parameters and functions */ #define PPTP_XMIT_WIN 16 /* max xmit window */ #define PPTP_MIN_TIMEOUT (PPTP_TIME_SCALE / 83) /* 12 milliseconds */ #define PPTP_MAX_TIMEOUT (3 * PPTP_TIME_SCALE) /* 3 seconds */ /* When we recieve a packet, we wait to see if there's an outgoing packet we can piggy-back the ACK off of. These parameters determine the mimimum and maxmimum length of time we're willing to wait in order to do that. These have no effect unless "enableDelayedAck" is turned on. */ #define PPTP_MIN_ACK_DELAY (PPTP_TIME_SCALE / 500) /* 2 milliseconds */ #define PPTP_MAX_ACK_DELAY (PPTP_TIME_SCALE / 2) /* 500 milliseconds */ /* See RFC 2637 section 4.4 */ #define PPTP_ACK_ALPHA(x) (((x) + 4) >> 3) /* alpha = 0.125 */ #define PPTP_ACK_BETA(x) (((x) + 2) >> 2) /* beta = 0.25 */ #define PPTP_ACK_CHI(x) ((x) << 2) /* chi = 4 */ #define PPTP_ACK_DELTA(x) ((x) << 1) /* delta = 2 */ #define PPTP_SEQ_DIFF(x,y) ((int32_t)(x) - (int32_t)(y)) #define SESSHASHSIZE 0x0020 #define SESSHASH(x) (((x) ^ ((x) >> 8)) & (SESSHASHSIZE - 1)) /* We keep packet retransmit and acknowlegement state in this struct */ struct ng_pptpgre_sess { node_p node; /* this node pointer */ hook_p hook; /* hook to upper layers */ struct ng_pptpgre_conf conf; /* configuration info */ struct mtx mtx; /* session mutex */ u_int32_t recvSeq; /* last seq # we rcv'd */ u_int32_t xmitSeq; /* last seq # we sent */ u_int32_t recvAck; /* last seq # peer ack'd */ u_int32_t xmitAck; /* last seq # we ack'd */ int32_t ato; /* adaptive time-out value */ int32_t rtt; /* round trip time estimate */ int32_t dev; /* deviation estimate */ u_int16_t xmitWin; /* size of xmit window */ struct callout sackTimer; /* send ack timer */ struct callout rackTimer; /* recv ack timer */ u_int32_t winAck; /* seq when xmitWin will grow */ pptptime_t timeSent[PPTP_XMIT_WIN]; LIST_ENTRY(ng_pptpgre_sess) sessions; }; typedef struct ng_pptpgre_sess *hpriv_p; /* Node private data */ struct ng_pptpgre_private { hook_p upper; /* hook to upper layers */ hook_p lower; /* hook to lower layers */ struct ng_pptpgre_sess uppersess; /* default session for compat */ LIST_HEAD(, ng_pptpgre_sess) sesshash[SESSHASHSIZE]; struct ng_pptpgre_stats stats; /* node statistics */ }; typedef struct ng_pptpgre_private *priv_p; /* Netgraph node methods */ static ng_constructor_t ng_pptpgre_constructor; static ng_rcvmsg_t ng_pptpgre_rcvmsg; static ng_shutdown_t ng_pptpgre_shutdown; static ng_newhook_t ng_pptpgre_newhook; static ng_rcvdata_t ng_pptpgre_rcvdata; static ng_rcvdata_t ng_pptpgre_rcvdata_lower; static ng_disconnect_t ng_pptpgre_disconnect; /* Helper functions */ static int ng_pptpgre_xmit(hpriv_p hpriv, item_p item); static void ng_pptpgre_start_send_ack_timer(hpriv_p hpriv); static void ng_pptpgre_start_recv_ack_timer(hpriv_p hpriv); static void ng_pptpgre_recv_ack_timeout(node_p node, hook_p hook, void *arg1, int arg2); static void ng_pptpgre_send_ack_timeout(node_p node, hook_p hook, void *arg1, int arg2); static hpriv_p ng_pptpgre_find_session(priv_p privp, u_int16_t cid); static void ng_pptpgre_reset(hpriv_p hpriv); static pptptime_t ng_pptpgre_time(void); /* Parse type for struct ng_pptpgre_conf */ static const struct ng_parse_struct_field ng_pptpgre_conf_type_fields[] = NG_PPTPGRE_CONF_TYPE_INFO; static const struct ng_parse_type ng_pptpgre_conf_type = { &ng_parse_struct_type, &ng_pptpgre_conf_type_fields, }; /* Parse type for struct ng_pptpgre_stats */ static const struct ng_parse_struct_field ng_pptpgre_stats_type_fields[] = NG_PPTPGRE_STATS_TYPE_INFO; static const struct ng_parse_type ng_pptp_stats_type = { &ng_parse_struct_type, &ng_pptpgre_stats_type_fields }; /* List of commands and how to convert arguments to/from ASCII */ static const struct ng_cmdlist ng_pptpgre_cmdlist[] = { { NGM_PPTPGRE_COOKIE, NGM_PPTPGRE_SET_CONFIG, "setconfig", &ng_pptpgre_conf_type, NULL }, { NGM_PPTPGRE_COOKIE, NGM_PPTPGRE_GET_CONFIG, "getconfig", &ng_parse_hint16_type, &ng_pptpgre_conf_type }, { NGM_PPTPGRE_COOKIE, NGM_PPTPGRE_GET_STATS, "getstats", NULL, &ng_pptp_stats_type }, { NGM_PPTPGRE_COOKIE, NGM_PPTPGRE_CLR_STATS, "clrstats", NULL, NULL }, { NGM_PPTPGRE_COOKIE, NGM_PPTPGRE_GETCLR_STATS, "getclrstats", NULL, &ng_pptp_stats_type }, { 0 } }; /* Node type descriptor */ static struct ng_type ng_pptpgre_typestruct = { .version = NG_ABI_VERSION, .name = NG_PPTPGRE_NODE_TYPE, .constructor = ng_pptpgre_constructor, .rcvmsg = ng_pptpgre_rcvmsg, .shutdown = ng_pptpgre_shutdown, .newhook = ng_pptpgre_newhook, .rcvdata = ng_pptpgre_rcvdata, .disconnect = ng_pptpgre_disconnect, .cmdlist = ng_pptpgre_cmdlist, }; NETGRAPH_INIT(pptpgre, &ng_pptpgre_typestruct); #define ERROUT(x) do { error = (x); goto done; } while (0) /************************************************************************ NETGRAPH NODE STUFF ************************************************************************/ /* * Node type constructor */ static int ng_pptpgre_constructor(node_p node) { priv_p priv; int i; /* Allocate private structure */ priv = malloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO); if (priv == NULL) return (ENOMEM); NG_NODE_SET_PRIVATE(node, priv); /* Initialize state */ mtx_init(&priv->uppersess.mtx, "ng_pptp", NULL, MTX_DEF); ng_callout_init(&priv->uppersess.sackTimer); ng_callout_init(&priv->uppersess.rackTimer); priv->uppersess.node = node; for (i = 0; i < SESSHASHSIZE; i++) LIST_INIT(&priv->sesshash[i]); LIST_INSERT_HEAD(&priv->sesshash[0], &priv->uppersess, sessions); /* Done */ return (0); } /* * Give our OK for a hook to be added. */ static int ng_pptpgre_newhook(node_p node, hook_p hook, const char *name) { const priv_p priv = NG_NODE_PRIVATE(node); /* Check hook name */ if (strcmp(name, NG_PPTPGRE_HOOK_UPPER) == 0) { priv->upper = hook; priv->uppersess.hook = hook; NG_HOOK_SET_PRIVATE(hook, &priv->uppersess); } else if (strcmp(name, NG_PPTPGRE_HOOK_LOWER) == 0) { priv->lower = hook; NG_HOOK_SET_RCVDATA(hook, ng_pptpgre_rcvdata_lower); } else { static const char hexdig[16] = "0123456789abcdef"; const char *hex; hpriv_p hpriv; int i, j; uint16_t cid, hash; /* Parse hook name to get session ID */ if (strncmp(name, NG_PPTPGRE_HOOK_SESSION_P, sizeof(NG_PPTPGRE_HOOK_SESSION_P) - 1) != 0) return (EINVAL); hex = name + sizeof(NG_PPTPGRE_HOOK_SESSION_P) - 1; for (cid = i = 0; i < 4; i++) { for (j = 0; j < 16 && hex[i] != hexdig[j]; j++); if (j == 16) return (EINVAL); cid = (cid << 4) | j; } if (hex[i] != '\0') return (EINVAL); hpriv = malloc(sizeof(*hpriv), M_NETGRAPH, M_NOWAIT | M_ZERO); if (hpriv == NULL) return (ENOMEM); /* Initialize state */ mtx_init(&hpriv->mtx, "ng_pptp", NULL, MTX_DEF); ng_callout_init(&hpriv->sackTimer); ng_callout_init(&hpriv->rackTimer); hpriv->conf.cid = cid; hpriv->node = node; hpriv->hook = hook; NG_HOOK_SET_PRIVATE(hook, hpriv); hash = SESSHASH(cid); LIST_INSERT_HEAD(&priv->sesshash[hash], hpriv, sessions); } return (0); } /* * Receive a control message. */ static int ng_pptpgre_rcvmsg(node_p node, item_p item, hook_p lasthook) { const priv_p priv = NG_NODE_PRIVATE(node); struct ng_mesg *resp = NULL; int error = 0; struct ng_mesg *msg; NGI_GET_MSG(item, msg); switch (msg->header.typecookie) { case NGM_PPTPGRE_COOKIE: switch (msg->header.cmd) { case NGM_PPTPGRE_SET_CONFIG: { struct ng_pptpgre_conf *const newConf = (struct ng_pptpgre_conf *) msg->data; hpriv_p hpriv; uint16_t hash; /* Check for invalid or illegal config */ if (msg->header.arglen != sizeof(*newConf)) ERROUT(EINVAL); /* Try to find session by cid. */ hpriv = ng_pptpgre_find_session(priv, newConf->cid); /* If not present - use upper. */ if (hpriv == NULL) { hpriv = &priv->uppersess; LIST_REMOVE(hpriv, sessions); hash = SESSHASH(newConf->cid); LIST_INSERT_HEAD(&priv->sesshash[hash], hpriv, sessions); } ng_pptpgre_reset(hpriv); /* reset on configure */ hpriv->conf = *newConf; break; } case NGM_PPTPGRE_GET_CONFIG: { hpriv_p hpriv; if (msg->header.arglen == 2) { /* Try to find session by cid. */ hpriv = ng_pptpgre_find_session(priv, *((uint16_t *)msg->data)); if (hpriv == NULL) ERROUT(EINVAL); } else if (msg->header.arglen == 0) { /* Use upper. */ hpriv = &priv->uppersess; } else ERROUT(EINVAL); NG_MKRESPONSE(resp, msg, sizeof(hpriv->conf), M_NOWAIT); if (resp == NULL) ERROUT(ENOMEM); bcopy(&hpriv->conf, resp->data, sizeof(hpriv->conf)); break; } case NGM_PPTPGRE_GET_STATS: case NGM_PPTPGRE_CLR_STATS: case NGM_PPTPGRE_GETCLR_STATS: { if (msg->header.cmd != NGM_PPTPGRE_CLR_STATS) { NG_MKRESPONSE(resp, msg, sizeof(priv->stats), M_NOWAIT); if (resp == NULL) ERROUT(ENOMEM); bcopy(&priv->stats, resp->data, sizeof(priv->stats)); } if (msg->header.cmd != NGM_PPTPGRE_GET_STATS) bzero(&priv->stats, sizeof(priv->stats)); break; } default: error = EINVAL; break; } break; default: error = EINVAL; break; } done: NG_RESPOND_MSG(error, node, item, resp); NG_FREE_MSG(msg); return (error); } /* * Receive incoming data on a hook. */ static int ng_pptpgre_rcvdata(hook_p hook, item_p item) { const hpriv_p hpriv = NG_HOOK_PRIVATE(hook); int rval; /* If not configured, reject */ if (!hpriv->conf.enabled) { NG_FREE_ITEM(item); return (ENXIO); } mtx_lock(&hpriv->mtx); rval = ng_pptpgre_xmit(hpriv, item); mtx_assert(&hpriv->mtx, MA_NOTOWNED); return (rval); } /* * Hook disconnection */ static int ng_pptpgre_disconnect(hook_p hook) { const node_p node = NG_HOOK_NODE(hook); const priv_p priv = NG_NODE_PRIVATE(node); const hpriv_p hpriv = NG_HOOK_PRIVATE(hook); /* Zero out hook pointer */ if (hook == priv->upper) { priv->upper = NULL; priv->uppersess.hook = NULL; } else if (hook == priv->lower) { priv->lower = NULL; } else { /* Reset node (stops timers) */ ng_pptpgre_reset(hpriv); LIST_REMOVE(hpriv, sessions); mtx_destroy(&hpriv->mtx); free(hpriv, M_NETGRAPH); } /* Go away if no longer connected to anything */ if ((NG_NODE_NUMHOOKS(node) == 0) && (NG_NODE_IS_VALID(node))) ng_rmnode_self(node); return (0); } /* * Destroy node */ static int ng_pptpgre_shutdown(node_p node) { const priv_p priv = NG_NODE_PRIVATE(node); /* Reset node (stops timers) */ ng_pptpgre_reset(&priv->uppersess); LIST_REMOVE(&priv->uppersess, sessions); mtx_destroy(&priv->uppersess.mtx); free(priv, M_NETGRAPH); /* Decrement ref count */ NG_NODE_UNREF(node); return (0); } /************************************************************************* TRANSMIT AND RECEIVE FUNCTIONS *************************************************************************/ /* * Transmit an outgoing frame, or just an ack if m is NULL. */ static int ng_pptpgre_xmit(hpriv_p hpriv, item_p item) { const priv_p priv = NG_NODE_PRIVATE(hpriv->node); u_char buf[sizeof(struct greheader) + 2 * sizeof(u_int32_t)]; struct greheader *const gre = (struct greheader *)buf; int grelen, error; struct mbuf *m; mtx_assert(&hpriv->mtx, MA_OWNED); if (item) { NGI_GET_M(item, m); } else { m = NULL; } /* Check if there's data */ if (m != NULL) { /* Check if windowing is enabled */ if (hpriv->conf.enableWindowing) { /* Is our transmit window full? */ if ((u_int32_t)PPTP_SEQ_DIFF(hpriv->xmitSeq, hpriv->recvAck) >= hpriv->xmitWin) { priv->stats.xmitDrops++; ERROUT(ENOBUFS); } } /* Sanity check frame length */ if (m != NULL && m->m_pkthdr.len > PPTP_MAX_PAYLOAD) { priv->stats.xmitTooBig++; ERROUT(EMSGSIZE); } } else { priv->stats.xmitLoneAcks++; } /* Build GRE header */ - ((u_int32_t *)gre)[0] = htonl(PPTP_INIT_VALUE); - gre->length = (m != NULL) ? htons((u_short)m->m_pkthdr.len) : 0; - gre->cid = htons(hpriv->conf.peerCid); + be32enc(gre, PPTP_INIT_VALUE); + be16enc(&gre->length, (m != NULL) ? m->m_pkthdr.len : 0); + be16enc(&gre->cid, hpriv->conf.peerCid); /* Include sequence number if packet contains any data */ if (m != NULL) { gre->hasSeq = 1; if (hpriv->conf.enableWindowing) { hpriv->timeSent[hpriv->xmitSeq - hpriv->recvAck] = ng_pptpgre_time(); } hpriv->xmitSeq++; - gre->data[0] = htonl(hpriv->xmitSeq); + be32enc(&gre->data[0], hpriv->xmitSeq); } /* Include acknowledgement (and stop send ack timer) if needed */ if (hpriv->conf.enableAlwaysAck || hpriv->xmitAck != hpriv->recvSeq) { gre->hasAck = 1; - gre->data[gre->hasSeq] = htonl(hpriv->recvSeq); + be32enc(&gre->data[gre->hasSeq], hpriv->recvSeq); hpriv->xmitAck = hpriv->recvSeq; if (hpriv->conf.enableDelayedAck) ng_uncallout(&hpriv->sackTimer, hpriv->node); } /* Prepend GRE header to outgoing frame */ grelen = sizeof(*gre) + sizeof(u_int32_t) * (gre->hasSeq + gre->hasAck); if (m == NULL) { MGETHDR(m, M_DONTWAIT, MT_DATA); if (m == NULL) { priv->stats.memoryFailures++; ERROUT(ENOBUFS); } m->m_len = m->m_pkthdr.len = grelen; m->m_pkthdr.rcvif = NULL; } else { M_PREPEND(m, grelen, M_DONTWAIT); if (m == NULL || (m->m_len < grelen && (m = m_pullup(m, grelen)) == NULL)) { priv->stats.memoryFailures++; ERROUT(ENOBUFS); } } bcopy(gre, mtod(m, u_char *), grelen); /* Update stats */ priv->stats.xmitPackets++; priv->stats.xmitOctets += m->m_pkthdr.len; /* * XXX: we should reset timer only after an item has been sent * successfully. */ if (hpriv->conf.enableWindowing && gre->hasSeq && hpriv->xmitSeq == hpriv->recvAck + 1) ng_pptpgre_start_recv_ack_timer(hpriv); mtx_unlock(&hpriv->mtx); /* Deliver packet */ if (item) { NG_FWD_NEW_DATA(error, item, priv->lower, m); } else { NG_SEND_DATA_ONLY(error, priv->lower, m); } return (error); done: mtx_unlock(&hpriv->mtx); NG_FREE_M(m); if (item) NG_FREE_ITEM(item); return (error); } /* * Handle an incoming packet. The packet includes the IP header. */ static int ng_pptpgre_rcvdata_lower(hook_p hook, item_p item) { hpriv_p hpriv; node_p node = NG_HOOK_NODE(hook); const priv_p priv = NG_NODE_PRIVATE(node); int iphlen, grelen, extralen; const struct greheader *gre; const struct ip *ip; int error = 0; struct mbuf *m; NGI_GET_M(item, m); /* Update stats */ priv->stats.recvPackets++; priv->stats.recvOctets += m->m_pkthdr.len; /* Sanity check packet length */ if (m->m_pkthdr.len < sizeof(*ip) + sizeof(*gre)) { priv->stats.recvRunts++; ERROUT(EINVAL); } /* Safely pull up the complete IP+GRE headers */ if (m->m_len < sizeof(*ip) + sizeof(*gre) && (m = m_pullup(m, sizeof(*ip) + sizeof(*gre))) == NULL) { priv->stats.memoryFailures++; ERROUT(ENOBUFS); } ip = mtod(m, const struct ip *); iphlen = ip->ip_hl << 2; if (m->m_len < iphlen + sizeof(*gre)) { if ((m = m_pullup(m, iphlen + sizeof(*gre))) == NULL) { priv->stats.memoryFailures++; ERROUT(ENOBUFS); } ip = mtod(m, const struct ip *); } gre = (const struct greheader *)((const u_char *)ip + iphlen); grelen = sizeof(*gre) + sizeof(u_int32_t) * (gre->hasSeq + gre->hasAck); if (m->m_pkthdr.len < iphlen + grelen) { priv->stats.recvRunts++; ERROUT(EINVAL); } if (m->m_len < iphlen + grelen) { if ((m = m_pullup(m, iphlen + grelen)) == NULL) { priv->stats.memoryFailures++; ERROUT(ENOBUFS); } ip = mtod(m, const struct ip *); gre = (const struct greheader *)((const u_char *)ip + iphlen); } /* Sanity check packet length and GRE header bits */ extralen = m->m_pkthdr.len - - (iphlen + grelen + gre->hasSeq * (u_int16_t)ntohs(gre->length)); + - (iphlen + grelen + gre->hasSeq * be16dec(&gre->length)); if (extralen < 0) { priv->stats.recvBadGRE++; ERROUT(EINVAL); } - if ((ntohl(*((const u_int32_t *)gre)) & PPTP_INIT_MASK) - != PPTP_INIT_VALUE) { + if ((be32dec(gre) & PPTP_INIT_MASK) != PPTP_INIT_VALUE) { priv->stats.recvBadGRE++; ERROUT(EINVAL); } - hpriv = ng_pptpgre_find_session(priv, ntohs(gre->cid)); + hpriv = ng_pptpgre_find_session(priv, be16dec(&gre->cid)); if (hpriv == NULL || hpriv->hook == NULL || !hpriv->conf.enabled) { priv->stats.recvBadCID++; ERROUT(EINVAL); } mtx_lock(&hpriv->mtx); /* Look for peer ack */ if (gre->hasAck) { - const u_int32_t ack = ntohl(gre->data[gre->hasSeq]); + const u_int32_t ack = be32dec(&gre->data[gre->hasSeq]); const int index = ack - hpriv->recvAck - 1; long sample; long diff; /* Sanity check ack value */ if (PPTP_SEQ_DIFF(ack, hpriv->xmitSeq) > 0) { priv->stats.recvBadAcks++; goto badAck; /* we never sent it! */ } if (PPTP_SEQ_DIFF(ack, hpriv->recvAck) <= 0) goto badAck; /* ack already timed out */ hpriv->recvAck = ack; /* Update adaptive timeout stuff */ if (hpriv->conf.enableWindowing) { sample = ng_pptpgre_time() - hpriv->timeSent[index]; diff = sample - hpriv->rtt; hpriv->rtt += PPTP_ACK_ALPHA(diff); if (diff < 0) diff = -diff; hpriv->dev += PPTP_ACK_BETA(diff - hpriv->dev); /* +2 to compensate low precision of int math */ hpriv->ato = hpriv->rtt + PPTP_ACK_CHI(hpriv->dev + 2); if (hpriv->ato > PPTP_MAX_TIMEOUT) hpriv->ato = PPTP_MAX_TIMEOUT; else if (hpriv->ato < PPTP_MIN_TIMEOUT) hpriv->ato = PPTP_MIN_TIMEOUT; /* Shift packet transmit times in our transmit window */ bcopy(hpriv->timeSent + index + 1, hpriv->timeSent, sizeof(*hpriv->timeSent) * (PPTP_XMIT_WIN - (index + 1))); /* If we sent an entire window, increase window size */ if (PPTP_SEQ_DIFF(ack, hpriv->winAck) >= 0 && hpriv->xmitWin < PPTP_XMIT_WIN) { hpriv->xmitWin++; hpriv->winAck = ack + hpriv->xmitWin; } /* Stop/(re)start receive ACK timer as necessary */ ng_uncallout(&hpriv->rackTimer, hpriv->node); if (hpriv->recvAck != hpriv->xmitSeq) ng_pptpgre_start_recv_ack_timer(hpriv); } } badAck: /* See if frame contains any data */ if (gre->hasSeq) { - const u_int32_t seq = ntohl(gre->data[0]); + const u_int32_t seq = be32dec(&gre->data[0]); /* Sanity check sequence number */ if (PPTP_SEQ_DIFF(seq, hpriv->recvSeq) <= 0) { if (seq == hpriv->recvSeq) priv->stats.recvDuplicates++; else priv->stats.recvOutOfOrder++; mtx_unlock(&hpriv->mtx); ERROUT(EINVAL); } hpriv->recvSeq = seq; /* We need to acknowledge this packet; do it soon... */ if (!(callout_pending(&hpriv->sackTimer))) { /* If delayed ACK is disabled, send it now */ if (!hpriv->conf.enableDelayedAck) { /* ack now */ ng_pptpgre_xmit(hpriv, NULL); /* ng_pptpgre_xmit() drops the mutex */ } else { /* ack later */ ng_pptpgre_start_send_ack_timer(hpriv); mtx_unlock(&hpriv->mtx); } } else mtx_unlock(&hpriv->mtx); /* Trim mbuf down to internal payload */ m_adj(m, iphlen + grelen); if (extralen > 0) m_adj(m, -extralen); mtx_assert(&hpriv->mtx, MA_NOTOWNED); /* Deliver frame to upper layers */ NG_FWD_NEW_DATA(error, item, hpriv->hook, m); } else { priv->stats.recvLoneAcks++; mtx_unlock(&hpriv->mtx); NG_FREE_ITEM(item); NG_FREE_M(m); /* no data to deliver */ } return (error); done: NG_FREE_ITEM(item); NG_FREE_M(m); return (error); } /************************************************************************* TIMER RELATED FUNCTIONS *************************************************************************/ /* * Start a timer for the peer's acknowledging our oldest unacknowledged * sequence number. If we get an ack for this sequence number before * the timer goes off, we cancel the timer. Resets currently running * recv ack timer, if any. */ static void ng_pptpgre_start_recv_ack_timer(hpriv_p hpriv) { int remain, ticks; /* Compute how long until oldest unack'd packet times out, and reset the timer to that time. */ remain = (hpriv->timeSent[0] + hpriv->ato) - ng_pptpgre_time(); if (remain < 0) remain = 0; /* Be conservative: timeout can happen up to 1 tick early */ ticks = (((remain * hz) + PPTP_TIME_SCALE - 1) / PPTP_TIME_SCALE) + 1; ng_callout(&hpriv->rackTimer, hpriv->node, hpriv->hook, ticks, ng_pptpgre_recv_ack_timeout, hpriv, 0); } /* * The peer has failed to acknowledge the oldest unacknowledged sequence * number within the time allotted. Update our adaptive timeout parameters * and reset/restart the recv ack timer. */ static void ng_pptpgre_recv_ack_timeout(node_p node, hook_p hook, void *arg1, int arg2) { const priv_p priv = NG_NODE_PRIVATE(node); const hpriv_p hpriv = arg1; /* Update adaptive timeout stuff */ priv->stats.recvAckTimeouts++; hpriv->rtt = PPTP_ACK_DELTA(hpriv->rtt) + 1; /* +1 to avoid delta*0 case */ hpriv->ato = hpriv->rtt + PPTP_ACK_CHI(hpriv->dev); if (hpriv->ato > PPTP_MAX_TIMEOUT) hpriv->ato = PPTP_MAX_TIMEOUT; else if (hpriv->ato < PPTP_MIN_TIMEOUT) hpriv->ato = PPTP_MIN_TIMEOUT; /* Reset ack and sliding window */ hpriv->recvAck = hpriv->xmitSeq; /* pretend we got the ack */ hpriv->xmitWin = (hpriv->xmitWin + 1) / 2; /* shrink transmit window */ hpriv->winAck = hpriv->recvAck + hpriv->xmitWin; /* reset win expand time */ } /* * Start the send ack timer. This assumes the timer is not * already running. */ static void ng_pptpgre_start_send_ack_timer(hpriv_p hpriv) { int ackTimeout, ticks; /* Take 1/4 of the estimated round trip time */ ackTimeout = (hpriv->rtt >> 2); if (ackTimeout < PPTP_MIN_ACK_DELAY) ackTimeout = PPTP_MIN_ACK_DELAY; else if (ackTimeout > PPTP_MAX_ACK_DELAY) ackTimeout = PPTP_MAX_ACK_DELAY; /* Be conservative: timeout can happen up to 1 tick early */ ticks = (((ackTimeout * hz) + PPTP_TIME_SCALE - 1) / PPTP_TIME_SCALE); ng_callout(&hpriv->sackTimer, hpriv->node, hpriv->hook, ticks, ng_pptpgre_send_ack_timeout, hpriv, 0); } /* * We've waited as long as we're willing to wait before sending an * acknowledgement to the peer for received frames. We had hoped to * be able to piggy back our acknowledgement on an outgoing data frame, * but apparently there haven't been any since. So send the ack now. */ static void ng_pptpgre_send_ack_timeout(node_p node, hook_p hook, void *arg1, int arg2) { const hpriv_p hpriv = arg1; mtx_lock(&hpriv->mtx); /* Send a frame with an ack but no payload */ ng_pptpgre_xmit(hpriv, NULL); mtx_assert(&hpriv->mtx, MA_NOTOWNED); } /************************************************************************* MISC FUNCTIONS *************************************************************************/ /* * Find the hook with a given session ID. */ static hpriv_p ng_pptpgre_find_session(priv_p privp, u_int16_t cid) { uint16_t hash = SESSHASH(cid); hpriv_p hpriv = NULL; LIST_FOREACH(hpriv, &privp->sesshash[hash], sessions) { if (hpriv->conf.cid == cid) break; } return (hpriv); } /* * Reset state (must be called with lock held or from writer) */ static void ng_pptpgre_reset(hpriv_p hpriv) { /* Reset adaptive timeout state */ hpriv->ato = PPTP_MAX_TIMEOUT; hpriv->rtt = PPTP_TIME_SCALE / 10; if (hpriv->conf.peerPpd > 1) /* ppd = 0 treat as = 1 */ hpriv->rtt *= hpriv->conf.peerPpd; hpriv->dev = 0; hpriv->xmitWin = (hpriv->conf.recvWin + 1) / 2; if (hpriv->xmitWin < 2) /* often the first packet is lost */ hpriv->xmitWin = 2; /* because the peer isn't ready */ else if (hpriv->xmitWin > PPTP_XMIT_WIN) hpriv->xmitWin = PPTP_XMIT_WIN; hpriv->winAck = hpriv->xmitWin; /* Reset sequence numbers */ hpriv->recvSeq = ~0; hpriv->recvAck = ~0; hpriv->xmitSeq = ~0; hpriv->xmitAck = ~0; /* Stop timers */ ng_uncallout(&hpriv->sackTimer, hpriv->node); ng_uncallout(&hpriv->rackTimer, hpriv->node); } /* * Return the current time scaled & translated to our internally used format. */ static pptptime_t ng_pptpgre_time(void) { struct timeval tv; pptptime_t t; microuptime(&tv); t = (pptptime_t)tv.tv_sec * PPTP_TIME_SCALE; t += tv.tv_usec / (1000000 / PPTP_TIME_SCALE); return(t); } Index: stable/8/sys/netgraph/ng_tcpmss.c =================================================================== --- stable/8/sys/netgraph/ng_tcpmss.c (revision 206660) +++ stable/8/sys/netgraph/ng_tcpmss.c (revision 206661) @@ -1,443 +1,446 @@ /*- * ng_tcpmss.c * * Copyright (c) 2004, Alexey Popov * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice unmodified, this list of conditions, and the following * disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * This software includes fragments of the following programs: * tcpmssd Ruslan Ermilov * * $FreeBSD$ */ /* * This node is netgraph tool for workaround of PMTUD problem. It acts * like filter for IP packets. If configured, it reduces MSS of TCP SYN * packets. * * Configuration can be done by sending NGM_TCPMSS_CONFIG message. The * message sets filter for incoming packets on hook 'inHook'. Packet's * TCP MSS field is lowered to 'maxMSS' parameter and resulting packet * is sent to 'outHook'. * * XXX: statistics are updated not atomically, so they may broke on SMP. */ #include #include +#include #include #include #include #include #include #include #include #include #include #include #include #include /* Per hook info. */ typedef struct { hook_p outHook; struct ng_tcpmss_hookstat stats; } *hpriv_p; /* Netgraph methods. */ static ng_constructor_t ng_tcpmss_constructor; static ng_rcvmsg_t ng_tcpmss_rcvmsg; static ng_newhook_t ng_tcpmss_newhook; static ng_rcvdata_t ng_tcpmss_rcvdata; static ng_disconnect_t ng_tcpmss_disconnect; static int correct_mss(struct tcphdr *, int, uint16_t, int); /* Parse type for struct ng_tcpmss_hookstat. */ static const struct ng_parse_struct_field ng_tcpmss_hookstat_type_fields[] = NG_TCPMSS_HOOKSTAT_INFO; static const struct ng_parse_type ng_tcpmss_hookstat_type = { &ng_parse_struct_type, &ng_tcpmss_hookstat_type_fields }; /* Parse type for struct ng_tcpmss_config. */ static const struct ng_parse_struct_field ng_tcpmss_config_type_fields[] = NG_TCPMSS_CONFIG_INFO; static const struct ng_parse_type ng_tcpmss_config_type = { &ng_parse_struct_type, ng_tcpmss_config_type_fields }; /* List of commands and how to convert arguments to/from ASCII. */ static const struct ng_cmdlist ng_tcpmss_cmds[] = { { NGM_TCPMSS_COOKIE, NGM_TCPMSS_GET_STATS, "getstats", &ng_parse_hookbuf_type, &ng_tcpmss_hookstat_type }, { NGM_TCPMSS_COOKIE, NGM_TCPMSS_CLR_STATS, "clrstats", &ng_parse_hookbuf_type, NULL }, { NGM_TCPMSS_COOKIE, NGM_TCPMSS_GETCLR_STATS, "getclrstats", &ng_parse_hookbuf_type, &ng_tcpmss_hookstat_type }, { NGM_TCPMSS_COOKIE, NGM_TCPMSS_CONFIG, "config", &ng_tcpmss_config_type, NULL }, { 0 } }; /* Netgraph type descriptor. */ static struct ng_type ng_tcpmss_typestruct = { .version = NG_ABI_VERSION, .name = NG_TCPMSS_NODE_TYPE, .constructor = ng_tcpmss_constructor, .rcvmsg = ng_tcpmss_rcvmsg, .newhook = ng_tcpmss_newhook, .rcvdata = ng_tcpmss_rcvdata, .disconnect = ng_tcpmss_disconnect, .cmdlist = ng_tcpmss_cmds, }; NETGRAPH_INIT(tcpmss, &ng_tcpmss_typestruct); #define ERROUT(x) { error = (x); goto done; } /* * Node constructor. No special actions required. */ static int ng_tcpmss_constructor(node_p node) { return (0); } /* * Add a hook. Any unique name is OK. */ static int ng_tcpmss_newhook(node_p node, hook_p hook, const char *name) { hpriv_p priv; priv = malloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO); if (priv == NULL) return (ENOMEM); NG_HOOK_SET_PRIVATE(hook, priv); return (0); } /* * Receive a control message. */ static int ng_tcpmss_rcvmsg (node_p node, item_p item, hook_p lasthook) { struct ng_mesg *msg, *resp = NULL; int error = 0; NGI_GET_MSG(item, msg); switch (msg->header.typecookie) { case NGM_TCPMSS_COOKIE: switch (msg->header.cmd) { case NGM_TCPMSS_GET_STATS: case NGM_TCPMSS_CLR_STATS: case NGM_TCPMSS_GETCLR_STATS: { hook_p hook; hpriv_p priv; /* Check that message is long enough. */ if (msg->header.arglen != NG_HOOKSIZ) ERROUT(EINVAL); /* Find this hook. */ hook = ng_findhook(node, (char *)msg->data); if (hook == NULL) ERROUT(ENOENT); priv = NG_HOOK_PRIVATE(hook); /* Create response. */ if (msg->header.cmd != NGM_TCPMSS_CLR_STATS) { NG_MKRESPONSE(resp, msg, sizeof(struct ng_tcpmss_hookstat), M_NOWAIT); if (resp == NULL) ERROUT(ENOMEM); bcopy(&priv->stats, resp->data, sizeof(struct ng_tcpmss_hookstat)); } if (msg->header.cmd != NGM_TCPMSS_GET_STATS) bzero(&priv->stats, sizeof(struct ng_tcpmss_hookstat)); break; } case NGM_TCPMSS_CONFIG: { struct ng_tcpmss_config *set; hook_p in, out; hpriv_p priv; /* Check that message is long enough. */ if (msg->header.arglen != sizeof(struct ng_tcpmss_config)) ERROUT(EINVAL); set = (struct ng_tcpmss_config *)msg->data; in = ng_findhook(node, set->inHook); out = ng_findhook(node, set->outHook); if (in == NULL || out == NULL) ERROUT(ENOENT); /* Configure MSS hack. */ priv = NG_HOOK_PRIVATE(in); priv->outHook = out; priv->stats.maxMSS = set->maxMSS; break; } default: error = EINVAL; break; } break; default: error = EINVAL; break; } done: NG_RESPOND_MSG(error, node, item, resp); NG_FREE_MSG(msg); return (error); } /* * Receive data on a hook, and hack MSS. * */ static int ng_tcpmss_rcvdata(hook_p hook, item_p item) { hpriv_p priv = NG_HOOK_PRIVATE(hook); struct mbuf *m = NULL; struct ip *ip; struct tcphdr *tcp; int iphlen, tcphlen, pktlen; int pullup_len = 0; int error = 0; /* Drop packets if filter is not configured on this hook. */ if (priv->outHook == NULL) goto done; NGI_GET_M(item, m); /* Update stats on incoming hook. */ pktlen = m->m_pkthdr.len; priv->stats.Octets += pktlen; priv->stats.Packets++; /* Check whether we configured to fix MSS. */ if (priv->stats.maxMSS == 0) goto send; #define M_CHECK(length) do { \ pullup_len += length; \ if ((m)->m_pkthdr.len < pullup_len) \ goto send; \ if ((m)->m_len < pullup_len && \ (((m) = m_pullup((m), pullup_len)) == NULL)) \ ERROUT(ENOBUFS); \ } while (0) /* Check mbuf packet size and arrange for IP header. */ M_CHECK(sizeof(struct ip)); ip = mtod(m, struct ip *); /* Check IP version. */ if (ip->ip_v != IPVERSION) ERROUT(EINVAL); /* Check IP header length. */ iphlen = ip->ip_hl << 2; if (iphlen < sizeof(struct ip) || iphlen > pktlen ) ERROUT(EINVAL); /* Check if it is TCP. */ if (!(ip->ip_p == IPPROTO_TCP)) goto send; /* Check mbuf packet size and arrange for IP+TCP header */ M_CHECK(iphlen - sizeof(struct ip) + sizeof(struct tcphdr)); ip = mtod(m, struct ip *); tcp = (struct tcphdr *)((caddr_t )ip + iphlen); /* Check TCP header length. */ tcphlen = tcp->th_off << 2; if (tcphlen < sizeof(struct tcphdr) || tcphlen > pktlen - iphlen) ERROUT(EINVAL); /* Check SYN packet and has options. */ if (!(tcp->th_flags & TH_SYN) || tcphlen == sizeof(struct tcphdr)) goto send; /* Update SYN stats. */ priv->stats.SYNPkts++; M_CHECK(tcphlen - sizeof(struct tcphdr)); ip = mtod(m, struct ip *); tcp = (struct tcphdr *)((caddr_t )ip + iphlen); #undef M_CHECK /* Fix MSS and update stats. */ if (correct_mss(tcp, tcphlen, priv->stats.maxMSS, m->m_pkthdr.csum_flags)) priv->stats.FixedPkts++; send: /* Deliver frame out destination hook. */ NG_FWD_NEW_DATA(error, item, priv->outHook, m); return (error); done: NG_FREE_ITEM(item); NG_FREE_M(m); return (error); } /* * Hook disconnection. * We must check all hooks, since they may reference this one. */ static int ng_tcpmss_disconnect(hook_p hook) { node_p node = NG_HOOK_NODE(hook); hook_p hook2; LIST_FOREACH(hook2, &node->nd_hooks, hk_hooks) { hpriv_p priv = NG_HOOK_PRIVATE(hook2); if (priv->outHook == hook) priv->outHook = NULL; } free(NG_HOOK_PRIVATE(hook), M_NETGRAPH); if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) ng_rmnode_self(NG_HOOK_NODE(hook)); return (0); } /* * Code from tcpmssd. */ /*- * The following macro is used to update an * internet checksum. "acc" is a 32-bit * accumulation of all the changes to the * checksum (adding in old 16-bit words and * subtracting out new words), and "cksum" * is the checksum value to be updated. */ #define TCPMSS_ADJUST_CHECKSUM(acc, cksum) do { \ acc += cksum; \ if (acc < 0) { \ acc = -acc; \ acc = (acc >> 16) + (acc & 0xffff); \ acc += acc >> 16; \ cksum = (u_short) ~acc; \ } else { \ acc = (acc >> 16) + (acc & 0xffff); \ acc += acc >> 16; \ cksum = (u_short) acc; \ } \ } while (0); static int correct_mss(struct tcphdr *tc, int hlen, uint16_t maxmss, int flags) { int olen, optlen; u_char *opt; - uint16_t *mss; int accumulate; int res = 0; + uint16_t sum; for (olen = hlen - sizeof(struct tcphdr), opt = (u_char *)(tc + 1); olen > 0; olen -= optlen, opt += optlen) { if (*opt == TCPOPT_EOL) break; else if (*opt == TCPOPT_NOP) optlen = 1; else { optlen = *(opt + 1); if (optlen <= 0 || optlen > olen) break; if (*opt == TCPOPT_MAXSEG) { if (optlen != TCPOLEN_MAXSEG) continue; - mss = (uint16_t *)(opt + 2); - if (ntohs(*mss) > maxmss) { - accumulate = *mss; - *mss = htons(maxmss); - accumulate -= *mss; - if ((flags & CSUM_TCP) == 0) - TCPMSS_ADJUST_CHECKSUM(accumulate, tc->th_sum); + accumulate = be16dec(opt + 2); + if (accumulate > maxmss) { + if ((flags & CSUM_TCP) == 0) { + accumulate -= maxmss; + sum = be16dec(&tc->th_sum); + TCPMSS_ADJUST_CHECKSUM(accumulate, sum); + be16enc(&tc->th_sum, sum); + } + be16enc(opt + 2, maxmss); res = 1; } } } } return (res); } Index: stable/8/sys =================================================================== --- stable/8/sys (revision 206660) +++ stable/8/sys (revision 206661) Property changes on: stable/8/sys ___________________________________________________________________ Modified: svn:mergeinfo ## -0,0 +0,1 ## Merged /head/sys:r206021,206032,206049-206050