1add85a1dSArchie Cobbs 2add85a1dSArchie Cobbs /* 3add85a1dSArchie Cobbs * ng_pptpgre.c 4add85a1dSArchie Cobbs * 5add85a1dSArchie Cobbs * Copyright (c) 1996-1999 Whistle Communications, Inc. 6add85a1dSArchie Cobbs * All rights reserved. 7add85a1dSArchie Cobbs * 8add85a1dSArchie Cobbs * Subject to the following obligations and disclaimer of warranty, use and 9add85a1dSArchie Cobbs * redistribution of this software, in source or object code forms, with or 10add85a1dSArchie Cobbs * without modifications are expressly permitted by Whistle Communications; 11add85a1dSArchie Cobbs * provided, however, that: 12add85a1dSArchie Cobbs * 1. Any and all reproductions of the source or object code must include the 13add85a1dSArchie Cobbs * copyright notice above and the following disclaimer of warranties; and 14add85a1dSArchie Cobbs * 2. No rights are granted, in any manner or form, to use Whistle 15add85a1dSArchie Cobbs * Communications, Inc. trademarks, including the mark "WHISTLE 16add85a1dSArchie Cobbs * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 17add85a1dSArchie Cobbs * such appears in the above copyright notice or in the software. 18add85a1dSArchie Cobbs * 19add85a1dSArchie Cobbs * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 20add85a1dSArchie Cobbs * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 21add85a1dSArchie Cobbs * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 22add85a1dSArchie Cobbs * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 23add85a1dSArchie Cobbs * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 24add85a1dSArchie Cobbs * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 25add85a1dSArchie Cobbs * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 26add85a1dSArchie Cobbs * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 27add85a1dSArchie Cobbs * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 28add85a1dSArchie Cobbs * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 29add85a1dSArchie Cobbs * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 30add85a1dSArchie Cobbs * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 31add85a1dSArchie Cobbs * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 32add85a1dSArchie Cobbs * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 33add85a1dSArchie Cobbs * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 34add85a1dSArchie Cobbs * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 35add85a1dSArchie Cobbs * OF SUCH DAMAGE. 36add85a1dSArchie Cobbs * 37cc3bbd68SJulian Elischer * Author: Archie Cobbs <archie@freebsd.org> 38add85a1dSArchie Cobbs * 39add85a1dSArchie Cobbs * $FreeBSD$ 40add85a1dSArchie Cobbs * $Whistle: ng_pptpgre.c,v 1.7 1999/12/08 00:10:06 archie Exp $ 41add85a1dSArchie Cobbs */ 42add85a1dSArchie Cobbs 43add85a1dSArchie Cobbs /* 44add85a1dSArchie Cobbs * PPTP/GRE netgraph node type. 45add85a1dSArchie Cobbs * 46add85a1dSArchie Cobbs * This node type does the GRE encapsulation as specified for the PPTP 47add85a1dSArchie Cobbs * protocol (RFC 2637, section 4). This includes sequencing and 48add85a1dSArchie Cobbs * retransmission of frames, but not the actual packet delivery nor 49add85a1dSArchie Cobbs * any of the TCP control stream protocol. 50add85a1dSArchie Cobbs * 51add85a1dSArchie Cobbs * The "upper" hook of this node is suitable for attaching to a "ppp" 52add85a1dSArchie Cobbs * node link hook. The "lower" hook of this node is suitable for attaching 53add85a1dSArchie Cobbs * to a "ksocket" node on hook "inet/raw/gre". 54add85a1dSArchie Cobbs */ 55add85a1dSArchie Cobbs 56add85a1dSArchie Cobbs #include <sys/param.h> 57add85a1dSArchie Cobbs #include <sys/systm.h> 58add85a1dSArchie Cobbs #include <sys/kernel.h> 59add85a1dSArchie Cobbs #include <sys/time.h> 60add85a1dSArchie Cobbs #include <sys/mbuf.h> 61add85a1dSArchie Cobbs #include <sys/malloc.h> 62add85a1dSArchie Cobbs #include <sys/errno.h> 63add85a1dSArchie Cobbs 64add85a1dSArchie Cobbs #include <netinet/in.h> 65add85a1dSArchie Cobbs #include <netinet/in_systm.h> 66add85a1dSArchie Cobbs #include <netinet/ip.h> 67add85a1dSArchie Cobbs 68add85a1dSArchie Cobbs #include <netgraph/ng_message.h> 69add85a1dSArchie Cobbs #include <netgraph/netgraph.h> 70add85a1dSArchie Cobbs #include <netgraph/ng_parse.h> 71add85a1dSArchie Cobbs #include <netgraph/ng_pptpgre.h> 72add85a1dSArchie Cobbs 73add85a1dSArchie Cobbs /* GRE packet format, as used by PPTP */ 74add85a1dSArchie Cobbs struct greheader { 75add85a1dSArchie Cobbs #if BYTE_ORDER == LITTLE_ENDIAN 76add85a1dSArchie Cobbs u_char recursion:3; /* recursion control */ 77add85a1dSArchie Cobbs u_char ssr:1; /* strict source route */ 78add85a1dSArchie Cobbs u_char hasSeq:1; /* sequence number present */ 79add85a1dSArchie Cobbs u_char hasKey:1; /* key present */ 80add85a1dSArchie Cobbs u_char hasRoute:1; /* routing present */ 81add85a1dSArchie Cobbs u_char hasSum:1; /* checksum present */ 82add85a1dSArchie Cobbs u_char vers:3; /* version */ 83add85a1dSArchie Cobbs u_char flags:4; /* flags */ 84add85a1dSArchie Cobbs u_char hasAck:1; /* acknowlege number present */ 85add85a1dSArchie Cobbs #elif BYTE_ORDER == BIG_ENDIAN 86add85a1dSArchie Cobbs u_char hasSum:1; /* checksum present */ 87add85a1dSArchie Cobbs u_char hasRoute:1; /* routing present */ 88add85a1dSArchie Cobbs u_char hasKey:1; /* key present */ 89add85a1dSArchie Cobbs u_char hasSeq:1; /* sequence number present */ 90add85a1dSArchie Cobbs u_char ssr:1; /* strict source route */ 91add85a1dSArchie Cobbs u_char recursion:3; /* recursion control */ 92add85a1dSArchie Cobbs u_char hasAck:1; /* acknowlege number present */ 93add85a1dSArchie Cobbs u_char flags:4; /* flags */ 94add85a1dSArchie Cobbs u_char vers:3; /* version */ 95add85a1dSArchie Cobbs #else 96add85a1dSArchie Cobbs #error BYTE_ORDER is not defined properly 97add85a1dSArchie Cobbs #endif 98add85a1dSArchie Cobbs u_int16_t proto; /* protocol (ethertype) */ 99add85a1dSArchie Cobbs u_int16_t length; /* payload length */ 100add85a1dSArchie Cobbs u_int16_t cid; /* call id */ 101add85a1dSArchie Cobbs u_int32_t data[0]; /* opt. seq, ack, then data */ 102add85a1dSArchie Cobbs }; 103add85a1dSArchie Cobbs 104add85a1dSArchie Cobbs /* The PPTP protocol ID used in the GRE 'proto' field */ 105add85a1dSArchie Cobbs #define PPTP_GRE_PROTO 0x880b 106add85a1dSArchie Cobbs 107add85a1dSArchie Cobbs /* Bits that must be set a certain way in all PPTP/GRE packets */ 108add85a1dSArchie Cobbs #define PPTP_INIT_VALUE ((0x2001 << 16) | PPTP_GRE_PROTO) 109add85a1dSArchie Cobbs #define PPTP_INIT_MASK 0xef7fffff 110add85a1dSArchie Cobbs 111add85a1dSArchie Cobbs /* Min and max packet length */ 112add85a1dSArchie Cobbs #define PPTP_MAX_PAYLOAD (0xffff - sizeof(struct greheader) - 8) 113add85a1dSArchie Cobbs 114add85a1dSArchie Cobbs /* All times are scaled by this (PPTP_TIME_SCALE time units = 1 sec.) */ 115e962a823SArchie Cobbs #define PPTP_TIME_SCALE 1000 /* milliseconds */ 116678f9e33SArchie Cobbs typedef u_int64_t pptptime_t; 117add85a1dSArchie Cobbs 118add85a1dSArchie Cobbs /* Acknowledgment timeout parameters and functions */ 119e962a823SArchie Cobbs #define PPTP_XMIT_WIN 16 /* max xmit window */ 120e962a823SArchie Cobbs #define PPTP_MIN_RTT (PPTP_TIME_SCALE / 10) /* 100 milliseconds */ 121da010626SArchie Cobbs #define PPTP_MIN_TIMEOUT (PPTP_TIME_SCALE / 500) /* 2 milliseconds */ 122add85a1dSArchie Cobbs #define PPTP_MAX_TIMEOUT (10 * PPTP_TIME_SCALE) /* 10 seconds */ 123add85a1dSArchie Cobbs 124da010626SArchie Cobbs /* When we recieve a packet, we wait to see if there's an outgoing packet 125da010626SArchie Cobbs we can piggy-back the ACK off of. These parameters determine the mimimum 126da010626SArchie Cobbs and maxmimum length of time we're willing to wait in order to do that. 127da010626SArchie Cobbs These have no effect unless "enableDelayedAck" is turned on. */ 128da010626SArchie Cobbs #define PPTP_MIN_ACK_DELAY (PPTP_TIME_SCALE / 500) /* 2 milliseconds */ 129da010626SArchie Cobbs #define PPTP_MAX_ACK_DELAY (PPTP_TIME_SCALE / 2) /* 500 milliseconds */ 130da010626SArchie Cobbs 131e962a823SArchie Cobbs /* See RFC 2637 section 4.4 */ 132add85a1dSArchie Cobbs #define PPTP_ACK_ALPHA(x) ((x) >> 3) /* alpha = 0.125 */ 133add85a1dSArchie Cobbs #define PPTP_ACK_BETA(x) ((x) >> 2) /* beta = 0.25 */ 134add85a1dSArchie Cobbs #define PPTP_ACK_CHI(x) ((x) << 2) /* chi = 4 */ 135add85a1dSArchie Cobbs #define PPTP_ACK_DELTA(x) ((x) << 1) /* delta = 2 */ 136add85a1dSArchie Cobbs 1379bee7adfSArchie Cobbs #define PPTP_SEQ_DIFF(x,y) ((int32_t)(x) - (int32_t)(y)) 1389bee7adfSArchie Cobbs 139add85a1dSArchie Cobbs /* We keep packet retransmit and acknowlegement state in this struct */ 140add85a1dSArchie Cobbs struct ng_pptpgre_ackp { 141add85a1dSArchie Cobbs int32_t ato; /* adaptive time-out value */ 142add85a1dSArchie Cobbs int32_t rtt; /* round trip time estimate */ 143add85a1dSArchie Cobbs int32_t dev; /* deviation estimate */ 144add85a1dSArchie Cobbs u_int16_t xmitWin; /* size of xmit window */ 145add85a1dSArchie Cobbs struct callout_handle sackTimer; /* send ack timer */ 146add85a1dSArchie Cobbs struct callout_handle rackTimer; /* recv ack timer */ 1473cd7db22SArchie Cobbs node_p *sackTimerPtr; /* send ack timer pointer */ 1483cd7db22SArchie Cobbs node_p *rackTimerPtr; /* recv ack timer pointer */ 1493cd7db22SArchie Cobbs u_int32_t winAck; /* seq when xmitWin will grow */ 150add85a1dSArchie Cobbs pptptime_t timeSent[PPTP_XMIT_WIN]; 151678f9e33SArchie Cobbs #ifdef DEBUG_RAT 152da010626SArchie Cobbs pptptime_t timerStart; /* when rackTimer started */ 153da010626SArchie Cobbs pptptime_t timerLength; /* rackTimer duration */ 154678f9e33SArchie Cobbs #endif 155add85a1dSArchie Cobbs }; 156add85a1dSArchie Cobbs 157add85a1dSArchie Cobbs /* Node private data */ 158add85a1dSArchie Cobbs struct ng_pptpgre_private { 159add85a1dSArchie Cobbs hook_p upper; /* hook to upper layers */ 160add85a1dSArchie Cobbs hook_p lower; /* hook to lower layers */ 161add85a1dSArchie Cobbs struct ng_pptpgre_conf conf; /* configuration info */ 162add85a1dSArchie Cobbs struct ng_pptpgre_ackp ackp; /* packet transmit ack state */ 163add85a1dSArchie Cobbs u_int32_t recvSeq; /* last seq # we rcv'd */ 164add85a1dSArchie Cobbs u_int32_t xmitSeq; /* last seq # we sent */ 165add85a1dSArchie Cobbs u_int32_t recvAck; /* last seq # peer ack'd */ 166add85a1dSArchie Cobbs u_int32_t xmitAck; /* last seq # we ack'd */ 167add85a1dSArchie Cobbs struct timeval startTime; /* time node was created */ 1689bee7adfSArchie Cobbs struct ng_pptpgre_stats stats; /* node statistics */ 169add85a1dSArchie Cobbs }; 170add85a1dSArchie Cobbs typedef struct ng_pptpgre_private *priv_p; 171add85a1dSArchie Cobbs 172add85a1dSArchie Cobbs /* Netgraph node methods */ 173add85a1dSArchie Cobbs static ng_constructor_t ng_pptpgre_constructor; 174add85a1dSArchie Cobbs static ng_rcvmsg_t ng_pptpgre_rcvmsg; 175069154d5SJulian Elischer static ng_shutdown_t ng_pptpgre_shutdown; 176add85a1dSArchie Cobbs static ng_newhook_t ng_pptpgre_newhook; 177add85a1dSArchie Cobbs static ng_rcvdata_t ng_pptpgre_rcvdata; 178add85a1dSArchie Cobbs static ng_disconnect_t ng_pptpgre_disconnect; 179add85a1dSArchie Cobbs 180add85a1dSArchie Cobbs /* Helper functions */ 181069154d5SJulian Elischer static int ng_pptpgre_xmit(node_p node, item_p item); 182069154d5SJulian Elischer static int ng_pptpgre_recv(node_p node, item_p item); 183da010626SArchie Cobbs static void ng_pptpgre_start_send_ack_timer(node_p node, int ackTimeout); 184add85a1dSArchie Cobbs static void ng_pptpgre_start_recv_ack_timer(node_p node); 185add85a1dSArchie Cobbs static void ng_pptpgre_recv_ack_timeout(void *arg); 186add85a1dSArchie Cobbs static void ng_pptpgre_send_ack_timeout(void *arg); 187add85a1dSArchie Cobbs static void ng_pptpgre_reset(node_p node); 188add85a1dSArchie Cobbs static pptptime_t ng_pptpgre_time(node_p node); 189add85a1dSArchie Cobbs 190add85a1dSArchie Cobbs /* Parse type for struct ng_pptpgre_conf */ 191add85a1dSArchie Cobbs static const struct ng_parse_struct_info 192add85a1dSArchie Cobbs ng_pptpgre_conf_type_info = NG_PPTPGRE_CONF_TYPE_INFO; 193add85a1dSArchie Cobbs static const struct ng_parse_type ng_pptpgre_conf_type = { 194add85a1dSArchie Cobbs &ng_parse_struct_type, 195add85a1dSArchie Cobbs &ng_pptpgre_conf_type_info, 196add85a1dSArchie Cobbs }; 197add85a1dSArchie Cobbs 1989bee7adfSArchie Cobbs /* Parse type for struct ng_pptpgre_stats */ 1999bee7adfSArchie Cobbs static const struct ng_parse_struct_info 2009bee7adfSArchie Cobbs ng_pptpgre_stats_type_info = NG_PPTPGRE_STATS_TYPE_INFO; 2019bee7adfSArchie Cobbs static const struct ng_parse_type ng_pptp_stats_type = { 2029bee7adfSArchie Cobbs &ng_parse_struct_type, 2039bee7adfSArchie Cobbs &ng_pptpgre_stats_type_info 2049bee7adfSArchie Cobbs }; 2059bee7adfSArchie Cobbs 206add85a1dSArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */ 207add85a1dSArchie Cobbs static const struct ng_cmdlist ng_pptpgre_cmdlist[] = { 208add85a1dSArchie Cobbs { 209add85a1dSArchie Cobbs NGM_PPTPGRE_COOKIE, 210add85a1dSArchie Cobbs NGM_PPTPGRE_SET_CONFIG, 211add85a1dSArchie Cobbs "setconfig", 212add85a1dSArchie Cobbs &ng_pptpgre_conf_type, 213add85a1dSArchie Cobbs NULL 214add85a1dSArchie Cobbs }, 215add85a1dSArchie Cobbs { 216add85a1dSArchie Cobbs NGM_PPTPGRE_COOKIE, 217add85a1dSArchie Cobbs NGM_PPTPGRE_GET_CONFIG, 218add85a1dSArchie Cobbs "getconfig", 219add85a1dSArchie Cobbs NULL, 220add85a1dSArchie Cobbs &ng_pptpgre_conf_type 221add85a1dSArchie Cobbs }, 2229bee7adfSArchie Cobbs { 2239bee7adfSArchie Cobbs NGM_PPTPGRE_COOKIE, 2249bee7adfSArchie Cobbs NGM_PPTPGRE_GET_STATS, 2259bee7adfSArchie Cobbs "getstats", 2269bee7adfSArchie Cobbs NULL, 2279bee7adfSArchie Cobbs &ng_pptp_stats_type 2289bee7adfSArchie Cobbs }, 2299bee7adfSArchie Cobbs { 2309bee7adfSArchie Cobbs NGM_PPTPGRE_COOKIE, 2319bee7adfSArchie Cobbs NGM_PPTPGRE_CLR_STATS, 2329bee7adfSArchie Cobbs "clrstats", 2339bee7adfSArchie Cobbs NULL, 2349bee7adfSArchie Cobbs NULL 2359bee7adfSArchie Cobbs }, 2369bee7adfSArchie Cobbs { 2379bee7adfSArchie Cobbs NGM_PPTPGRE_COOKIE, 2389bee7adfSArchie Cobbs NGM_PPTPGRE_GETCLR_STATS, 2399bee7adfSArchie Cobbs "getclrstats", 2409bee7adfSArchie Cobbs NULL, 2419bee7adfSArchie Cobbs &ng_pptp_stats_type 2429bee7adfSArchie Cobbs }, 243add85a1dSArchie Cobbs { 0 } 244add85a1dSArchie Cobbs }; 245add85a1dSArchie Cobbs 246add85a1dSArchie Cobbs /* Node type descriptor */ 247add85a1dSArchie Cobbs static struct ng_type ng_pptpgre_typestruct = { 248589f6ed8SJulian Elischer NG_ABI_VERSION, 249add85a1dSArchie Cobbs NG_PPTPGRE_NODE_TYPE, 250add85a1dSArchie Cobbs NULL, 251add85a1dSArchie Cobbs ng_pptpgre_constructor, 252add85a1dSArchie Cobbs ng_pptpgre_rcvmsg, 253069154d5SJulian Elischer ng_pptpgre_shutdown, 254add85a1dSArchie Cobbs ng_pptpgre_newhook, 255add85a1dSArchie Cobbs NULL, 256add85a1dSArchie Cobbs NULL, 257add85a1dSArchie Cobbs ng_pptpgre_rcvdata, 258add85a1dSArchie Cobbs ng_pptpgre_disconnect, 259add85a1dSArchie Cobbs ng_pptpgre_cmdlist 260add85a1dSArchie Cobbs }; 261add85a1dSArchie Cobbs NETGRAPH_INIT(pptpgre, &ng_pptpgre_typestruct); 262add85a1dSArchie Cobbs 263add85a1dSArchie Cobbs #define ERROUT(x) do { error = (x); goto done; } while (0) 264add85a1dSArchie Cobbs 265add85a1dSArchie Cobbs /************************************************************************ 266add85a1dSArchie Cobbs NETGRAPH NODE STUFF 267add85a1dSArchie Cobbs ************************************************************************/ 268add85a1dSArchie Cobbs 269add85a1dSArchie Cobbs /* 270add85a1dSArchie Cobbs * Node type constructor 271add85a1dSArchie Cobbs */ 272add85a1dSArchie Cobbs static int 273069154d5SJulian Elischer ng_pptpgre_constructor(node_p node) 274add85a1dSArchie Cobbs { 275add85a1dSArchie Cobbs priv_p priv; 276add85a1dSArchie Cobbs 277add85a1dSArchie Cobbs /* Allocate private structure */ 27899cdf4ccSDavid Malone MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO); 279add85a1dSArchie Cobbs if (priv == NULL) 280add85a1dSArchie Cobbs return (ENOMEM); 281add85a1dSArchie Cobbs 28230400f03SJulian Elischer NG_NODE_SET_PRIVATE(node, priv); 283add85a1dSArchie Cobbs 284add85a1dSArchie Cobbs /* Initialize state */ 285add85a1dSArchie Cobbs callout_handle_init(&priv->ackp.sackTimer); 286add85a1dSArchie Cobbs callout_handle_init(&priv->ackp.rackTimer); 287add85a1dSArchie Cobbs 288add85a1dSArchie Cobbs /* Done */ 289add85a1dSArchie Cobbs return (0); 290add85a1dSArchie Cobbs } 291add85a1dSArchie Cobbs 292add85a1dSArchie Cobbs /* 293add85a1dSArchie Cobbs * Give our OK for a hook to be added. 294add85a1dSArchie Cobbs */ 295add85a1dSArchie Cobbs static int 296add85a1dSArchie Cobbs ng_pptpgre_newhook(node_p node, hook_p hook, const char *name) 297add85a1dSArchie Cobbs { 29830400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 299add85a1dSArchie Cobbs hook_p *hookPtr; 300add85a1dSArchie Cobbs 301add85a1dSArchie Cobbs /* Check hook name */ 302add85a1dSArchie Cobbs if (strcmp(name, NG_PPTPGRE_HOOK_UPPER) == 0) 303add85a1dSArchie Cobbs hookPtr = &priv->upper; 304add85a1dSArchie Cobbs else if (strcmp(name, NG_PPTPGRE_HOOK_LOWER) == 0) 305add85a1dSArchie Cobbs hookPtr = &priv->lower; 306add85a1dSArchie Cobbs else 307add85a1dSArchie Cobbs return (EINVAL); 308add85a1dSArchie Cobbs 309add85a1dSArchie Cobbs /* See if already connected */ 310add85a1dSArchie Cobbs if (*hookPtr != NULL) 311add85a1dSArchie Cobbs return (EISCONN); 312add85a1dSArchie Cobbs 313add85a1dSArchie Cobbs /* OK */ 314add85a1dSArchie Cobbs *hookPtr = hook; 315add85a1dSArchie Cobbs return (0); 316add85a1dSArchie Cobbs } 317add85a1dSArchie Cobbs 318add85a1dSArchie Cobbs /* 319add85a1dSArchie Cobbs * Receive a control message. 320add85a1dSArchie Cobbs */ 321add85a1dSArchie Cobbs static int 322069154d5SJulian Elischer ng_pptpgre_rcvmsg(node_p node, item_p item, hook_p lasthook) 323add85a1dSArchie Cobbs { 32430400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 325add85a1dSArchie Cobbs struct ng_mesg *resp = NULL; 326add85a1dSArchie Cobbs int error = 0; 327069154d5SJulian Elischer struct ng_mesg *msg; 328add85a1dSArchie Cobbs 329069154d5SJulian Elischer NGI_GET_MSG(item, msg); 330add85a1dSArchie Cobbs switch (msg->header.typecookie) { 331add85a1dSArchie Cobbs case NGM_PPTPGRE_COOKIE: 332add85a1dSArchie Cobbs switch (msg->header.cmd) { 333add85a1dSArchie Cobbs case NGM_PPTPGRE_SET_CONFIG: 334add85a1dSArchie Cobbs { 335add85a1dSArchie Cobbs struct ng_pptpgre_conf *const newConf = 336add85a1dSArchie Cobbs (struct ng_pptpgre_conf *) msg->data; 337add85a1dSArchie Cobbs 338add85a1dSArchie Cobbs /* Check for invalid or illegal config */ 339add85a1dSArchie Cobbs if (msg->header.arglen != sizeof(*newConf)) 340add85a1dSArchie Cobbs ERROUT(EINVAL); 341add85a1dSArchie Cobbs ng_pptpgre_reset(node); /* reset on configure */ 342add85a1dSArchie Cobbs priv->conf = *newConf; 343add85a1dSArchie Cobbs break; 344add85a1dSArchie Cobbs } 345add85a1dSArchie Cobbs case NGM_PPTPGRE_GET_CONFIG: 346add85a1dSArchie Cobbs NG_MKRESPONSE(resp, msg, sizeof(priv->conf), M_NOWAIT); 347add85a1dSArchie Cobbs if (resp == NULL) 348add85a1dSArchie Cobbs ERROUT(ENOMEM); 349add85a1dSArchie Cobbs bcopy(&priv->conf, resp->data, sizeof(priv->conf)); 350add85a1dSArchie Cobbs break; 3519bee7adfSArchie Cobbs case NGM_PPTPGRE_GET_STATS: 3529bee7adfSArchie Cobbs case NGM_PPTPGRE_CLR_STATS: 3539bee7adfSArchie Cobbs case NGM_PPTPGRE_GETCLR_STATS: 3549bee7adfSArchie Cobbs { 3559bee7adfSArchie Cobbs if (msg->header.cmd != NGM_PPTPGRE_CLR_STATS) { 3569bee7adfSArchie Cobbs NG_MKRESPONSE(resp, msg, 3579bee7adfSArchie Cobbs sizeof(priv->stats), M_NOWAIT); 3589bee7adfSArchie Cobbs if (resp == NULL) 3599bee7adfSArchie Cobbs ERROUT(ENOMEM); 3609bee7adfSArchie Cobbs bcopy(&priv->stats, 3619bee7adfSArchie Cobbs resp->data, sizeof(priv->stats)); 3629bee7adfSArchie Cobbs } 3639bee7adfSArchie Cobbs if (msg->header.cmd != NGM_PPTPGRE_GET_STATS) 3649bee7adfSArchie Cobbs bzero(&priv->stats, sizeof(priv->stats)); 3659bee7adfSArchie Cobbs break; 3669bee7adfSArchie Cobbs } 367add85a1dSArchie Cobbs default: 368add85a1dSArchie Cobbs error = EINVAL; 369add85a1dSArchie Cobbs break; 370add85a1dSArchie Cobbs } 371add85a1dSArchie Cobbs break; 372add85a1dSArchie Cobbs default: 373add85a1dSArchie Cobbs error = EINVAL; 374add85a1dSArchie Cobbs break; 375add85a1dSArchie Cobbs } 376589f6ed8SJulian Elischer done: 377069154d5SJulian Elischer NG_RESPOND_MSG(error, node, item, resp); 378069154d5SJulian Elischer NG_FREE_MSG(msg); 379add85a1dSArchie Cobbs return (error); 380add85a1dSArchie Cobbs } 381add85a1dSArchie Cobbs 382add85a1dSArchie Cobbs /* 383add85a1dSArchie Cobbs * Receive incoming data on a hook. 384add85a1dSArchie Cobbs */ 385add85a1dSArchie Cobbs static int 386069154d5SJulian Elischer ng_pptpgre_rcvdata(hook_p hook, item_p item) 387add85a1dSArchie Cobbs { 38830400f03SJulian Elischer const node_p node = NG_HOOK_NODE(hook); 38930400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 390add85a1dSArchie Cobbs 391add85a1dSArchie Cobbs /* If not configured, reject */ 392add85a1dSArchie Cobbs if (!priv->conf.enabled) { 393069154d5SJulian Elischer NG_FREE_ITEM(item); 394add85a1dSArchie Cobbs return (ENXIO); 395add85a1dSArchie Cobbs } 396add85a1dSArchie Cobbs 397add85a1dSArchie Cobbs /* Treat as xmit or recv data */ 398add85a1dSArchie Cobbs if (hook == priv->upper) 399069154d5SJulian Elischer return ng_pptpgre_xmit(node, item); 400add85a1dSArchie Cobbs if (hook == priv->lower) 401069154d5SJulian Elischer return ng_pptpgre_recv(node, item); 402add85a1dSArchie Cobbs panic("%s: weird hook", __FUNCTION__); 403add85a1dSArchie Cobbs } 404add85a1dSArchie Cobbs 405add85a1dSArchie Cobbs /* 406add85a1dSArchie Cobbs * Destroy node 407add85a1dSArchie Cobbs */ 408add85a1dSArchie Cobbs static int 409069154d5SJulian Elischer ng_pptpgre_shutdown(node_p node) 410add85a1dSArchie Cobbs { 41130400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 412add85a1dSArchie Cobbs 4133cd7db22SArchie Cobbs /* Reset node */ 414add85a1dSArchie Cobbs ng_pptpgre_reset(node); 415add85a1dSArchie Cobbs 416add85a1dSArchie Cobbs /* Take down netgraph node */ 417add85a1dSArchie Cobbs bzero(priv, sizeof(*priv)); 418add85a1dSArchie Cobbs FREE(priv, M_NETGRAPH); 41930400f03SJulian Elischer NG_NODE_SET_PRIVATE(node, NULL); 42030400f03SJulian Elischer NG_NODE_UNREF(node); 421add85a1dSArchie Cobbs return (0); 422add85a1dSArchie Cobbs } 423add85a1dSArchie Cobbs 424add85a1dSArchie Cobbs /* 425add85a1dSArchie Cobbs * Hook disconnection 426add85a1dSArchie Cobbs */ 427add85a1dSArchie Cobbs static int 428add85a1dSArchie Cobbs ng_pptpgre_disconnect(hook_p hook) 429add85a1dSArchie Cobbs { 43030400f03SJulian Elischer const node_p node = NG_HOOK_NODE(hook); 43130400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 432add85a1dSArchie Cobbs 433add85a1dSArchie Cobbs /* Zero out hook pointer */ 434add85a1dSArchie Cobbs if (hook == priv->upper) 435add85a1dSArchie Cobbs priv->upper = NULL; 436add85a1dSArchie Cobbs else if (hook == priv->lower) 437add85a1dSArchie Cobbs priv->lower = NULL; 438add85a1dSArchie Cobbs else 439add85a1dSArchie Cobbs panic("%s: unknown hook", __FUNCTION__); 440add85a1dSArchie Cobbs 441add85a1dSArchie Cobbs /* Go away if no longer connected to anything */ 44230400f03SJulian Elischer if ((NG_NODE_NUMHOOKS(node) == 0) 44330400f03SJulian Elischer && (NG_NODE_IS_VALID(node))) 444069154d5SJulian Elischer ng_rmnode_self(node); 445add85a1dSArchie Cobbs return (0); 446add85a1dSArchie Cobbs } 447add85a1dSArchie Cobbs 448add85a1dSArchie Cobbs /************************************************************************* 449add85a1dSArchie Cobbs TRANSMIT AND RECEIVE FUNCTIONS 450add85a1dSArchie Cobbs *************************************************************************/ 451add85a1dSArchie Cobbs 452add85a1dSArchie Cobbs /* 453add85a1dSArchie Cobbs * Transmit an outgoing frame, or just an ack if m is NULL. 454add85a1dSArchie Cobbs */ 455add85a1dSArchie Cobbs static int 456069154d5SJulian Elischer ng_pptpgre_xmit(node_p node, item_p item) 457add85a1dSArchie Cobbs { 45830400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 459add85a1dSArchie Cobbs struct ng_pptpgre_ackp *const a = &priv->ackp; 460add85a1dSArchie Cobbs u_char buf[sizeof(struct greheader) + 2 * sizeof(u_int32_t)]; 461add85a1dSArchie Cobbs struct greheader *const gre = (struct greheader *)buf; 462add85a1dSArchie Cobbs int grelen, error; 463069154d5SJulian Elischer struct mbuf *m; 464add85a1dSArchie Cobbs 465069154d5SJulian Elischer if (item) { 466069154d5SJulian Elischer NGI_GET_M(item, m); 467069154d5SJulian Elischer } else { 468069154d5SJulian Elischer m = NULL; 469069154d5SJulian Elischer } 4709bee7adfSArchie Cobbs /* Check if there's data */ 4719bee7adfSArchie Cobbs if (m != NULL) { 4729bee7adfSArchie Cobbs 473add85a1dSArchie Cobbs /* Is our transmit window full? */ 4749bee7adfSArchie Cobbs if ((u_int32_t)PPTP_SEQ_DIFF(priv->xmitSeq, priv->recvAck) 4759bee7adfSArchie Cobbs >= a->xmitWin) { 4769bee7adfSArchie Cobbs priv->stats.xmitDrops++; 477069154d5SJulian Elischer NG_FREE_M(m); 478069154d5SJulian Elischer NG_FREE_ITEM(item); 479add85a1dSArchie Cobbs return (ENOBUFS); 480add85a1dSArchie Cobbs } 481add85a1dSArchie Cobbs 482add85a1dSArchie Cobbs /* Sanity check frame length */ 483add85a1dSArchie Cobbs if (m != NULL && m->m_pkthdr.len > PPTP_MAX_PAYLOAD) { 4849bee7adfSArchie Cobbs priv->stats.xmitTooBig++; 485069154d5SJulian Elischer NG_FREE_M(m); 486069154d5SJulian Elischer NG_FREE_ITEM(item); 487add85a1dSArchie Cobbs return (EMSGSIZE); 488add85a1dSArchie Cobbs } 489069154d5SJulian Elischer } else { 4909bee7adfSArchie Cobbs priv->stats.xmitLoneAcks++; 491069154d5SJulian Elischer } 492add85a1dSArchie Cobbs 493add85a1dSArchie Cobbs /* Build GRE header */ 494add85a1dSArchie Cobbs ((u_int32_t *)gre)[0] = htonl(PPTP_INIT_VALUE); 495add85a1dSArchie Cobbs gre->length = (m != NULL) ? htons((u_short)m->m_pkthdr.len) : 0; 496add85a1dSArchie Cobbs gre->cid = htons(priv->conf.peerCid); 497add85a1dSArchie Cobbs 498add85a1dSArchie Cobbs /* Include sequence number if packet contains any data */ 499add85a1dSArchie Cobbs if (m != NULL) { 500add85a1dSArchie Cobbs gre->hasSeq = 1; 501add85a1dSArchie Cobbs a->timeSent[priv->xmitSeq - priv->recvAck] 502add85a1dSArchie Cobbs = ng_pptpgre_time(node); 503add85a1dSArchie Cobbs priv->xmitSeq++; 504add85a1dSArchie Cobbs gre->data[0] = htonl(priv->xmitSeq); 505add85a1dSArchie Cobbs } 506add85a1dSArchie Cobbs 507add85a1dSArchie Cobbs /* Include acknowledgement (and stop send ack timer) if needed */ 508678f9e33SArchie Cobbs if (priv->conf.enableAlwaysAck || priv->xmitAck != priv->recvSeq) { 509add85a1dSArchie Cobbs gre->hasAck = 1; 510678f9e33SArchie Cobbs gre->data[gre->hasSeq] = htonl(priv->recvSeq); 511add85a1dSArchie Cobbs priv->xmitAck = priv->recvSeq; 512034d9dacSArchie Cobbs a->sackTimerPtr = NULL; /* "stop" timer */ 513da010626SArchie Cobbs } 514add85a1dSArchie Cobbs 515add85a1dSArchie Cobbs /* Prepend GRE header to outgoing frame */ 516add85a1dSArchie Cobbs grelen = sizeof(*gre) + sizeof(u_int32_t) * (gre->hasSeq + gre->hasAck); 517add85a1dSArchie Cobbs if (m == NULL) { 518add85a1dSArchie Cobbs MGETHDR(m, M_DONTWAIT, MT_DATA); 519add85a1dSArchie Cobbs if (m == NULL) { 520678f9e33SArchie Cobbs priv->stats.memoryFailures++; 521069154d5SJulian Elischer if (item) 522069154d5SJulian Elischer NG_FREE_ITEM(item); 523add85a1dSArchie Cobbs return (ENOBUFS); 524add85a1dSArchie Cobbs } 525add85a1dSArchie Cobbs m->m_len = m->m_pkthdr.len = grelen; 526add85a1dSArchie Cobbs m->m_pkthdr.rcvif = NULL; 527add85a1dSArchie Cobbs } else { 528add85a1dSArchie Cobbs M_PREPEND(m, grelen, M_NOWAIT); 529add85a1dSArchie Cobbs if (m == NULL || (m->m_len < grelen 530add85a1dSArchie Cobbs && (m = m_pullup(m, grelen)) == NULL)) { 531678f9e33SArchie Cobbs priv->stats.memoryFailures++; 532069154d5SJulian Elischer if (item) 533069154d5SJulian Elischer NG_FREE_ITEM(item); 534add85a1dSArchie Cobbs return (ENOBUFS); 535add85a1dSArchie Cobbs } 536add85a1dSArchie Cobbs } 537add85a1dSArchie Cobbs bcopy(gre, mtod(m, u_char *), grelen); 538add85a1dSArchie Cobbs 5399bee7adfSArchie Cobbs /* Update stats */ 5409bee7adfSArchie Cobbs priv->stats.xmitPackets++; 5419bee7adfSArchie Cobbs priv->stats.xmitOctets += m->m_pkthdr.len; 5429bee7adfSArchie Cobbs 543add85a1dSArchie Cobbs /* Deliver packet */ 544069154d5SJulian Elischer if (item) { 545069154d5SJulian Elischer NG_FWD_NEW_DATA(error, item, priv->lower, m); 546069154d5SJulian Elischer } else { 547069154d5SJulian Elischer NG_SEND_DATA_ONLY(error, priv->lower, m); 548069154d5SJulian Elischer } 549069154d5SJulian Elischer 550678f9e33SArchie Cobbs 551da010626SArchie Cobbs /* Start receive ACK timer if data was sent and not already running */ 552678f9e33SArchie Cobbs if (error == 0 && gre->hasSeq && priv->xmitSeq == priv->recvAck + 1) 553678f9e33SArchie Cobbs ng_pptpgre_start_recv_ack_timer(node); 554add85a1dSArchie Cobbs return (error); 555add85a1dSArchie Cobbs } 556add85a1dSArchie Cobbs 557add85a1dSArchie Cobbs /* 558add85a1dSArchie Cobbs * Handle an incoming packet. The packet includes the IP header. 559add85a1dSArchie Cobbs */ 560add85a1dSArchie Cobbs static int 561069154d5SJulian Elischer ng_pptpgre_recv(node_p node, item_p item) 562add85a1dSArchie Cobbs { 56330400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 564add85a1dSArchie Cobbs int iphlen, grelen, extralen; 565add85a1dSArchie Cobbs struct greheader *gre; 566add85a1dSArchie Cobbs struct ip *ip; 567add85a1dSArchie Cobbs int error = 0; 568069154d5SJulian Elischer struct mbuf *m; 569add85a1dSArchie Cobbs 570069154d5SJulian Elischer NGI_GET_M(item, m); 5719bee7adfSArchie Cobbs /* Update stats */ 5729bee7adfSArchie Cobbs priv->stats.recvPackets++; 5739bee7adfSArchie Cobbs priv->stats.recvOctets += m->m_pkthdr.len; 5749bee7adfSArchie Cobbs 575add85a1dSArchie Cobbs /* Sanity check packet length */ 576add85a1dSArchie Cobbs if (m->m_pkthdr.len < sizeof(*ip) + sizeof(*gre)) { 5779bee7adfSArchie Cobbs priv->stats.recvRunts++; 578add85a1dSArchie Cobbs bad: 579069154d5SJulian Elischer NG_FREE_M(m); 580069154d5SJulian Elischer NG_FREE_ITEM(item); 581add85a1dSArchie Cobbs return (EINVAL); 582add85a1dSArchie Cobbs } 583add85a1dSArchie Cobbs 584add85a1dSArchie Cobbs /* Safely pull up the complete IP+GRE headers */ 585add85a1dSArchie Cobbs if (m->m_len < sizeof(*ip) + sizeof(*gre) 586add85a1dSArchie Cobbs && (m = m_pullup(m, sizeof(*ip) + sizeof(*gre))) == NULL) { 587678f9e33SArchie Cobbs priv->stats.memoryFailures++; 588069154d5SJulian Elischer NG_FREE_ITEM(item); 589add85a1dSArchie Cobbs return (ENOBUFS); 590add85a1dSArchie Cobbs } 591add85a1dSArchie Cobbs ip = mtod(m, struct ip *); 592add85a1dSArchie Cobbs iphlen = ip->ip_hl << 2; 593add85a1dSArchie Cobbs if (m->m_len < iphlen + sizeof(*gre)) { 594add85a1dSArchie Cobbs if ((m = m_pullup(m, iphlen + sizeof(*gre))) == NULL) { 595678f9e33SArchie Cobbs priv->stats.memoryFailures++; 596069154d5SJulian Elischer NG_FREE_ITEM(item); 597add85a1dSArchie Cobbs return (ENOBUFS); 598add85a1dSArchie Cobbs } 599add85a1dSArchie Cobbs ip = mtod(m, struct ip *); 600add85a1dSArchie Cobbs } 601add85a1dSArchie Cobbs gre = (struct greheader *)((u_char *)ip + iphlen); 602add85a1dSArchie Cobbs grelen = sizeof(*gre) + sizeof(u_int32_t) * (gre->hasSeq + gre->hasAck); 6039bee7adfSArchie Cobbs if (m->m_pkthdr.len < iphlen + grelen) { 6049bee7adfSArchie Cobbs priv->stats.recvRunts++; 605add85a1dSArchie Cobbs goto bad; 6069bee7adfSArchie Cobbs } 607add85a1dSArchie Cobbs if (m->m_len < iphlen + grelen) { 608add85a1dSArchie Cobbs if ((m = m_pullup(m, iphlen + grelen)) == NULL) { 609678f9e33SArchie Cobbs priv->stats.memoryFailures++; 610069154d5SJulian Elischer NG_FREE_ITEM(item); 611add85a1dSArchie Cobbs return (ENOBUFS); 612add85a1dSArchie Cobbs } 613add85a1dSArchie Cobbs ip = mtod(m, struct ip *); 614add85a1dSArchie Cobbs gre = (struct greheader *)((u_char *)ip + iphlen); 615add85a1dSArchie Cobbs } 616add85a1dSArchie Cobbs 617add85a1dSArchie Cobbs /* Sanity check packet length and GRE header bits */ 618add85a1dSArchie Cobbs extralen = m->m_pkthdr.len 619add85a1dSArchie Cobbs - (iphlen + grelen + (u_int16_t)ntohs(gre->length)); 6209bee7adfSArchie Cobbs if (extralen < 0) { 6219bee7adfSArchie Cobbs priv->stats.recvBadGRE++; 622add85a1dSArchie Cobbs goto bad; 6239bee7adfSArchie Cobbs } 6249bee7adfSArchie Cobbs if ((ntohl(*((u_int32_t *)gre)) & PPTP_INIT_MASK) != PPTP_INIT_VALUE) { 6259bee7adfSArchie Cobbs priv->stats.recvBadGRE++; 626add85a1dSArchie Cobbs goto bad; 6279bee7adfSArchie Cobbs } 6289bee7adfSArchie Cobbs if (ntohs(gre->cid) != priv->conf.cid) { 6299bee7adfSArchie Cobbs priv->stats.recvBadCID++; 630add85a1dSArchie Cobbs goto bad; 6319bee7adfSArchie Cobbs } 632add85a1dSArchie Cobbs 633add85a1dSArchie Cobbs /* Look for peer ack */ 634add85a1dSArchie Cobbs if (gre->hasAck) { 635add85a1dSArchie Cobbs struct ng_pptpgre_ackp *const a = &priv->ackp; 636add85a1dSArchie Cobbs const u_int32_t ack = ntohl(gre->data[gre->hasSeq]); 637add85a1dSArchie Cobbs const int index = ack - priv->recvAck - 1; 63822dfb9bdSArchie Cobbs long sample; 639add85a1dSArchie Cobbs long diff; 640add85a1dSArchie Cobbs 641add85a1dSArchie Cobbs /* Sanity check ack value */ 6429bee7adfSArchie Cobbs if (PPTP_SEQ_DIFF(ack, priv->xmitSeq) > 0) { 6439bee7adfSArchie Cobbs priv->stats.recvBadAcks++; 6449bee7adfSArchie Cobbs goto badAck; /* we never sent it! */ 6459bee7adfSArchie Cobbs } 6469bee7adfSArchie Cobbs if (PPTP_SEQ_DIFF(ack, priv->recvAck) <= 0) 6479bee7adfSArchie Cobbs goto badAck; /* ack already timed out */ 648add85a1dSArchie Cobbs priv->recvAck = ack; 649add85a1dSArchie Cobbs 650add85a1dSArchie Cobbs /* Update adaptive timeout stuff */ 65122dfb9bdSArchie Cobbs sample = ng_pptpgre_time(node) - a->timeSent[index]; 652add85a1dSArchie Cobbs diff = sample - a->rtt; 653add85a1dSArchie Cobbs a->rtt += PPTP_ACK_ALPHA(diff); 654add85a1dSArchie Cobbs if (diff < 0) 655add85a1dSArchie Cobbs diff = -diff; 656add85a1dSArchie Cobbs a->dev += PPTP_ACK_BETA(diff - a->dev); 657e962a823SArchie Cobbs a->ato = a->rtt + PPTP_ACK_CHI(a->dev); 658add85a1dSArchie Cobbs if (a->ato > PPTP_MAX_TIMEOUT) 659add85a1dSArchie Cobbs a->ato = PPTP_MAX_TIMEOUT; 660e962a823SArchie Cobbs if (a->ato < PPTP_MIN_TIMEOUT) 661e962a823SArchie Cobbs a->ato = PPTP_MIN_TIMEOUT; 662e962a823SArchie Cobbs 663e962a823SArchie Cobbs /* Shift packet transmit times in our transmit window */ 664add85a1dSArchie Cobbs ovbcopy(a->timeSent + index + 1, a->timeSent, 665add85a1dSArchie Cobbs sizeof(*a->timeSent) * (PPTP_XMIT_WIN - (index + 1))); 666e962a823SArchie Cobbs 667e962a823SArchie Cobbs /* If we sent an entire window, increase window size by one */ 6689bee7adfSArchie Cobbs if (PPTP_SEQ_DIFF(ack, a->winAck) >= 0 6699bee7adfSArchie Cobbs && a->xmitWin < PPTP_XMIT_WIN) { 670add85a1dSArchie Cobbs a->xmitWin++; 671add85a1dSArchie Cobbs a->winAck = ack + a->xmitWin; 672add85a1dSArchie Cobbs } 673add85a1dSArchie Cobbs 6749bee7adfSArchie Cobbs /* Stop/(re)start receive ACK timer as necessary */ 675678f9e33SArchie Cobbs a->rackTimerPtr = NULL; 676678f9e33SArchie Cobbs if (priv->recvAck != priv->xmitSeq) 677add85a1dSArchie Cobbs ng_pptpgre_start_recv_ack_timer(node); 678add85a1dSArchie Cobbs } 6799bee7adfSArchie Cobbs badAck: 680add85a1dSArchie Cobbs 681add85a1dSArchie Cobbs /* See if frame contains any data */ 682add85a1dSArchie Cobbs if (gre->hasSeq) { 683add85a1dSArchie Cobbs struct ng_pptpgre_ackp *const a = &priv->ackp; 684add85a1dSArchie Cobbs const u_int32_t seq = ntohl(gre->data[0]); 685add85a1dSArchie Cobbs 686add85a1dSArchie Cobbs /* Sanity check sequence number */ 6879bee7adfSArchie Cobbs if (PPTP_SEQ_DIFF(seq, priv->recvSeq) <= 0) { 6889bee7adfSArchie Cobbs if (seq == priv->recvSeq) 6899bee7adfSArchie Cobbs priv->stats.recvDuplicates++; 6909bee7adfSArchie Cobbs else 6919bee7adfSArchie Cobbs priv->stats.recvOutOfOrder++; 6929bee7adfSArchie Cobbs goto bad; /* out-of-order or dup */ 6939bee7adfSArchie Cobbs } 694add85a1dSArchie Cobbs priv->recvSeq = seq; 695add85a1dSArchie Cobbs 696add85a1dSArchie Cobbs /* We need to acknowledge this packet; do it soon... */ 6973cd7db22SArchie Cobbs if (a->sackTimerPtr == NULL) { 698da010626SArchie Cobbs int maxWait; 699add85a1dSArchie Cobbs 700da010626SArchie Cobbs /* Take 1/4 of the estimated round trip time */ 701da010626SArchie Cobbs maxWait = (a->rtt >> 2); 702add85a1dSArchie Cobbs 703678f9e33SArchie Cobbs /* If delayed ACK is disabled, send it now */ 704da010626SArchie Cobbs if (!priv->conf.enableDelayedAck 705da010626SArchie Cobbs || maxWait < PPTP_MIN_ACK_DELAY) 706069154d5SJulian Elischer ng_pptpgre_xmit(node, NULL); 707add85a1dSArchie Cobbs else { /* send the ack later */ 7083cd7db22SArchie Cobbs if (maxWait > PPTP_MAX_ACK_DELAY) 7093cd7db22SArchie Cobbs maxWait = PPTP_MAX_ACK_DELAY; 7103cd7db22SArchie Cobbs ng_pptpgre_start_send_ack_timer(node, maxWait); 711add85a1dSArchie Cobbs } 712add85a1dSArchie Cobbs } 713add85a1dSArchie Cobbs 714add85a1dSArchie Cobbs /* Trim mbuf down to internal payload */ 715add85a1dSArchie Cobbs m_adj(m, iphlen + grelen); 716add85a1dSArchie Cobbs if (extralen > 0) 717add85a1dSArchie Cobbs m_adj(m, -extralen); 718add85a1dSArchie Cobbs 719add85a1dSArchie Cobbs /* Deliver frame to upper layers */ 720069154d5SJulian Elischer NG_FWD_NEW_DATA(error, item, priv->upper, m); 7219bee7adfSArchie Cobbs } else { 7229bee7adfSArchie Cobbs priv->stats.recvLoneAcks++; 723069154d5SJulian Elischer NG_FREE_ITEM(item); 724069154d5SJulian Elischer NG_FREE_M(m); /* no data to deliver */ 7259bee7adfSArchie Cobbs } 726add85a1dSArchie Cobbs return (error); 727add85a1dSArchie Cobbs } 728add85a1dSArchie Cobbs 729add85a1dSArchie Cobbs /************************************************************************* 730add85a1dSArchie Cobbs TIMER RELATED FUNCTIONS 731add85a1dSArchie Cobbs *************************************************************************/ 732add85a1dSArchie Cobbs 733add85a1dSArchie Cobbs /* 7349bee7adfSArchie Cobbs * Start a timer for the peer's acknowledging our oldest unacknowledged 735add85a1dSArchie Cobbs * sequence number. If we get an ack for this sequence number before 736add85a1dSArchie Cobbs * the timer goes off, we cancel the timer. Resets currently running 737add85a1dSArchie Cobbs * recv ack timer, if any. 738add85a1dSArchie Cobbs */ 739add85a1dSArchie Cobbs static void 740add85a1dSArchie Cobbs ng_pptpgre_start_recv_ack_timer(node_p node) 741add85a1dSArchie Cobbs { 74230400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 743add85a1dSArchie Cobbs struct ng_pptpgre_ackp *const a = &priv->ackp; 744678f9e33SArchie Cobbs int remain, ticks; 745add85a1dSArchie Cobbs 746add85a1dSArchie Cobbs /* Compute how long until oldest unack'd packet times out, 747add85a1dSArchie Cobbs and reset the timer to that time. */ 748da010626SArchie Cobbs KASSERT(a->rackTimerPtr == NULL, ("%s: rackTimer", __FUNCTION__)); 749add85a1dSArchie Cobbs remain = (a->timeSent[0] + a->ato) - ng_pptpgre_time(node); 750add85a1dSArchie Cobbs if (remain < 0) 751add85a1dSArchie Cobbs remain = 0; 752678f9e33SArchie Cobbs #ifdef DEBUG_RAT 753678f9e33SArchie Cobbs a->timerLength = remain; 754678f9e33SArchie Cobbs a->timerStart = ng_pptpgre_time(node); 755678f9e33SArchie Cobbs #endif 7569bee7adfSArchie Cobbs 7573cd7db22SArchie Cobbs /* Start new timer */ 7583cd7db22SArchie Cobbs MALLOC(a->rackTimerPtr, node_p *, sizeof(node_p), M_NETGRAPH, M_NOWAIT); 759678f9e33SArchie Cobbs if (a->rackTimerPtr == NULL) { 760678f9e33SArchie Cobbs priv->stats.memoryFailures++; 7613cd7db22SArchie Cobbs return; /* XXX potential hang here */ 762678f9e33SArchie Cobbs } 7633cd7db22SArchie Cobbs *a->rackTimerPtr = node; /* insures the correct timeout event */ 76430400f03SJulian Elischer NG_NODE_REF(node); 765678f9e33SArchie Cobbs 766678f9e33SArchie Cobbs /* Be conservative: timeout() can return up to 1 tick early */ 767678f9e33SArchie Cobbs ticks = (((remain * hz) + PPTP_TIME_SCALE - 1) / PPTP_TIME_SCALE) + 1; 7683cd7db22SArchie Cobbs a->rackTimer = timeout(ng_pptpgre_recv_ack_timeout, 769678f9e33SArchie Cobbs a->rackTimerPtr, ticks); 770add85a1dSArchie Cobbs } 771add85a1dSArchie Cobbs 772add85a1dSArchie Cobbs /* 773add85a1dSArchie Cobbs * The peer has failed to acknowledge the oldest unacknowledged sequence 774add85a1dSArchie Cobbs * number within the time allotted. Update our adaptive timeout parameters 775add85a1dSArchie Cobbs * and reset/restart the recv ack timer. 776add85a1dSArchie Cobbs */ 777add85a1dSArchie Cobbs static void 778add85a1dSArchie Cobbs ng_pptpgre_recv_ack_timeout(void *arg) 779add85a1dSArchie Cobbs { 7804164c447SArchie Cobbs int s = splnet(); 7813cd7db22SArchie Cobbs const node_p node = *((node_p *)arg); 78230400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 783add85a1dSArchie Cobbs struct ng_pptpgre_ackp *const a = &priv->ackp; 784add85a1dSArchie Cobbs 7853cd7db22SArchie Cobbs /* This complicated stuff is needed to avoid race conditions */ 7863cd7db22SArchie Cobbs FREE(arg, M_NETGRAPH); 78730400f03SJulian Elischer KASSERT(node->nd_refs > 0, ("%s: no nd_refs", __FUNCTION__)); 78830400f03SJulian Elischer if (NG_NODE_NOT_VALID(node)) { /* shutdown race condition */ 78930400f03SJulian Elischer NG_NODE_UNREF(node); 7909bee7adfSArchie Cobbs splx(s); 7919bee7adfSArchie Cobbs return; 7929bee7adfSArchie Cobbs } 79330400f03SJulian Elischer NG_NODE_UNREF(node); 7943cd7db22SArchie Cobbs if (arg != a->rackTimerPtr) { /* timer stopped race condition */ 7953cd7db22SArchie Cobbs splx(s); 7963cd7db22SArchie Cobbs return; 7973cd7db22SArchie Cobbs } 7983cd7db22SArchie Cobbs a->rackTimerPtr = NULL; 7999bee7adfSArchie Cobbs 800add85a1dSArchie Cobbs /* Update adaptive timeout stuff */ 8019bee7adfSArchie Cobbs priv->stats.recvAckTimeouts++; 802add85a1dSArchie Cobbs a->rtt = PPTP_ACK_DELTA(a->rtt); 803add85a1dSArchie Cobbs a->ato = a->rtt + PPTP_ACK_CHI(a->dev); 804add85a1dSArchie Cobbs if (a->ato > PPTP_MAX_TIMEOUT) 805add85a1dSArchie Cobbs a->ato = PPTP_MAX_TIMEOUT; 806e962a823SArchie Cobbs if (a->ato < PPTP_MIN_TIMEOUT) 807e962a823SArchie Cobbs a->ato = PPTP_MIN_TIMEOUT; 808add85a1dSArchie Cobbs 809678f9e33SArchie Cobbs #ifdef DEBUG_RAT 810678f9e33SArchie Cobbs log(LOG_DEBUG, 811678f9e33SArchie Cobbs "RAT now=%d seq=0x%x sent=%d tstart=%d tlen=%d ato=%d\n", 812678f9e33SArchie Cobbs (int)ng_pptpgre_time(node), priv->recvAck + 1, 813678f9e33SArchie Cobbs (int)a->timeSent[0], (int)a->timerStart, (int)a->timerLength, a->ato); 814678f9e33SArchie Cobbs #endif 815678f9e33SArchie Cobbs 816e962a823SArchie Cobbs /* Reset ack and sliding window */ 817e962a823SArchie Cobbs priv->recvAck = priv->xmitSeq; /* pretend we got the ack */ 818e962a823SArchie Cobbs a->xmitWin = (a->xmitWin + 1) / 2; /* shrink transmit window */ 819e962a823SArchie Cobbs a->winAck = priv->recvAck + a->xmitWin; /* reset win expand time */ 8204164c447SArchie Cobbs splx(s); 821add85a1dSArchie Cobbs } 822add85a1dSArchie Cobbs 823add85a1dSArchie Cobbs /* 8249bee7adfSArchie Cobbs * Start the send ack timer. This assumes the timer is not 8259bee7adfSArchie Cobbs * already running. 8269bee7adfSArchie Cobbs */ 8279bee7adfSArchie Cobbs static void 828da010626SArchie Cobbs ng_pptpgre_start_send_ack_timer(node_p node, int ackTimeout) 8299bee7adfSArchie Cobbs { 83030400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 8319bee7adfSArchie Cobbs struct ng_pptpgre_ackp *const a = &priv->ackp; 832678f9e33SArchie Cobbs int ticks; 8339bee7adfSArchie Cobbs 8343cd7db22SArchie Cobbs /* Start new timer */ 8353cd7db22SArchie Cobbs KASSERT(a->sackTimerPtr == NULL, ("%s: sackTimer", __FUNCTION__)); 8363cd7db22SArchie Cobbs MALLOC(a->sackTimerPtr, node_p *, sizeof(node_p), M_NETGRAPH, M_NOWAIT); 837678f9e33SArchie Cobbs if (a->sackTimerPtr == NULL) { 838678f9e33SArchie Cobbs priv->stats.memoryFailures++; 8393cd7db22SArchie Cobbs return; /* XXX potential hang here */ 840678f9e33SArchie Cobbs } 8413cd7db22SArchie Cobbs *a->sackTimerPtr = node; 84230400f03SJulian Elischer NG_NODE_REF(node); 843678f9e33SArchie Cobbs 844678f9e33SArchie Cobbs /* Be conservative: timeout() can return up to 1 tick early */ 845678f9e33SArchie Cobbs ticks = (((ackTimeout * hz) + PPTP_TIME_SCALE - 1) / PPTP_TIME_SCALE); 8463cd7db22SArchie Cobbs a->sackTimer = timeout(ng_pptpgre_send_ack_timeout, 847678f9e33SArchie Cobbs a->sackTimerPtr, ticks); 8489bee7adfSArchie Cobbs } 8499bee7adfSArchie Cobbs 8509bee7adfSArchie Cobbs /* 851add85a1dSArchie Cobbs * We've waited as long as we're willing to wait before sending an 852add85a1dSArchie Cobbs * acknowledgement to the peer for received frames. We had hoped to 853add85a1dSArchie Cobbs * be able to piggy back our acknowledgement on an outgoing data frame, 854add85a1dSArchie Cobbs * but apparently there haven't been any since. So send the ack now. 855add85a1dSArchie Cobbs */ 856add85a1dSArchie Cobbs static void 857add85a1dSArchie Cobbs ng_pptpgre_send_ack_timeout(void *arg) 858add85a1dSArchie Cobbs { 8594164c447SArchie Cobbs int s = splnet(); 8603cd7db22SArchie Cobbs const node_p node = *((node_p *)arg); 86130400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 862add85a1dSArchie Cobbs struct ng_pptpgre_ackp *const a = &priv->ackp; 863add85a1dSArchie Cobbs 8643cd7db22SArchie Cobbs /* This complicated stuff is needed to avoid race conditions */ 8653cd7db22SArchie Cobbs FREE(arg, M_NETGRAPH); 86630400f03SJulian Elischer KASSERT(node->nd_refs > 0, ("%s: no nd_refs", __FUNCTION__)); 86730400f03SJulian Elischer if (NG_NODE_NOT_VALID(node)) { /* shutdown race condition */ 86830400f03SJulian Elischer NG_NODE_UNREF(node); 8699bee7adfSArchie Cobbs splx(s); 8709bee7adfSArchie Cobbs return; 8719bee7adfSArchie Cobbs } 87230400f03SJulian Elischer NG_NODE_UNREF(node); 8733cd7db22SArchie Cobbs if (a->sackTimerPtr != arg) { /* timer stopped race condition */ 8743cd7db22SArchie Cobbs splx(s); 8753cd7db22SArchie Cobbs return; 8763cd7db22SArchie Cobbs } 8773cd7db22SArchie Cobbs a->sackTimerPtr = NULL; 8789bee7adfSArchie Cobbs 8799bee7adfSArchie Cobbs /* Send a frame with an ack but no payload */ 880069154d5SJulian Elischer ng_pptpgre_xmit(node, NULL); 8814164c447SArchie Cobbs splx(s); 882add85a1dSArchie Cobbs } 883add85a1dSArchie Cobbs 884add85a1dSArchie Cobbs /************************************************************************* 885add85a1dSArchie Cobbs MISC FUNCTIONS 886add85a1dSArchie Cobbs *************************************************************************/ 887add85a1dSArchie Cobbs 888add85a1dSArchie Cobbs /* 889add85a1dSArchie Cobbs * Reset state 890add85a1dSArchie Cobbs */ 891add85a1dSArchie Cobbs static void 892add85a1dSArchie Cobbs ng_pptpgre_reset(node_p node) 893add85a1dSArchie Cobbs { 89430400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 895add85a1dSArchie Cobbs struct ng_pptpgre_ackp *const a = &priv->ackp; 896add85a1dSArchie Cobbs 897add85a1dSArchie Cobbs /* Reset adaptive timeout state */ 898add85a1dSArchie Cobbs a->ato = PPTP_MAX_TIMEOUT; 899add85a1dSArchie Cobbs a->rtt = priv->conf.peerPpd * PPTP_TIME_SCALE / 10; /* ppd in 10ths */ 900add85a1dSArchie Cobbs if (a->rtt < PPTP_MIN_RTT) 901add85a1dSArchie Cobbs a->rtt = PPTP_MIN_RTT; 902add85a1dSArchie Cobbs a->dev = 0; 903add85a1dSArchie Cobbs a->xmitWin = (priv->conf.recvWin + 1) / 2; 904e962a823SArchie Cobbs if (a->xmitWin < 2) /* often the first packet is lost */ 905e962a823SArchie Cobbs a->xmitWin = 2; /* because the peer isn't ready */ 906add85a1dSArchie Cobbs if (a->xmitWin > PPTP_XMIT_WIN) 907add85a1dSArchie Cobbs a->xmitWin = PPTP_XMIT_WIN; 908add85a1dSArchie Cobbs a->winAck = a->xmitWin; 909add85a1dSArchie Cobbs 910add85a1dSArchie Cobbs /* Reset sequence numbers */ 9113cd7db22SArchie Cobbs priv->recvSeq = ~0; 9123cd7db22SArchie Cobbs priv->recvAck = ~0; 9133cd7db22SArchie Cobbs priv->xmitSeq = ~0; 9143cd7db22SArchie Cobbs priv->xmitAck = ~0; 915add85a1dSArchie Cobbs 916add85a1dSArchie Cobbs /* Reset start time */ 9179bee7adfSArchie Cobbs getmicrouptime(&priv->startTime); 9189bee7adfSArchie Cobbs 9199bee7adfSArchie Cobbs /* Reset stats */ 9209bee7adfSArchie Cobbs bzero(&priv->stats, sizeof(priv->stats)); 921add85a1dSArchie Cobbs 922034d9dacSArchie Cobbs /* "Stop" timers */ 9233cd7db22SArchie Cobbs a->sackTimerPtr = NULL; 9243cd7db22SArchie Cobbs a->rackTimerPtr = NULL; 925add85a1dSArchie Cobbs } 926add85a1dSArchie Cobbs 927add85a1dSArchie Cobbs /* 928add85a1dSArchie Cobbs * Return the current time scaled & translated to our internally used format. 929add85a1dSArchie Cobbs */ 930add85a1dSArchie Cobbs static pptptime_t 931add85a1dSArchie Cobbs ng_pptpgre_time(node_p node) 932add85a1dSArchie Cobbs { 93330400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 934add85a1dSArchie Cobbs struct timeval tv; 935678f9e33SArchie Cobbs pptptime_t t; 936add85a1dSArchie Cobbs 937678f9e33SArchie Cobbs microuptime(&tv); 938add85a1dSArchie Cobbs if (tv.tv_sec < priv->startTime.tv_sec 939add85a1dSArchie Cobbs || (tv.tv_sec == priv->startTime.tv_sec 940add85a1dSArchie Cobbs && tv.tv_usec < priv->startTime.tv_usec)) 941add85a1dSArchie Cobbs return (0); 942add85a1dSArchie Cobbs timevalsub(&tv, &priv->startTime); 943678f9e33SArchie Cobbs t = (pptptime_t)tv.tv_sec * PPTP_TIME_SCALE; 944678f9e33SArchie Cobbs t += (pptptime_t)tv.tv_usec / (1000000 / PPTP_TIME_SCALE); 945678f9e33SArchie Cobbs return(t); 946add85a1dSArchie Cobbs } 947add85a1dSArchie Cobbs 948