1c398230bSWarner Losh /*- 2fe267a55SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause AND BSD-2-Clause-FreeBSD 3fe267a55SPedro F. Giffuni * 4a9b3dca5SArchie Cobbs * Copyright (c) 1996-2000 Whistle Communications, Inc. 54cf49a43SJulian Elischer * All rights reserved. 64cf49a43SJulian Elischer * 74cf49a43SJulian Elischer * Subject to the following obligations and disclaimer of warranty, use and 84cf49a43SJulian Elischer * redistribution of this software, in source or object code forms, with or 94cf49a43SJulian Elischer * without modifications are expressly permitted by Whistle Communications; 104cf49a43SJulian Elischer * provided, however, that: 114cf49a43SJulian Elischer * 1. Any and all reproductions of the source or object code must include the 124cf49a43SJulian Elischer * copyright notice above and the following disclaimer of warranties; and 134cf49a43SJulian Elischer * 2. No rights are granted, in any manner or form, to use Whistle 144cf49a43SJulian Elischer * Communications, Inc. trademarks, including the mark "WHISTLE 154cf49a43SJulian Elischer * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 164cf49a43SJulian Elischer * such appears in the above copyright notice or in the software. 174cf49a43SJulian Elischer * 184cf49a43SJulian Elischer * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 194cf49a43SJulian Elischer * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 204cf49a43SJulian Elischer * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 214cf49a43SJulian Elischer * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 224cf49a43SJulian Elischer * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 234cf49a43SJulian Elischer * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 244cf49a43SJulian Elischer * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 254cf49a43SJulian Elischer * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 264cf49a43SJulian Elischer * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 274cf49a43SJulian Elischer * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 284cf49a43SJulian Elischer * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 294cf49a43SJulian Elischer * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 304cf49a43SJulian Elischer * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 314cf49a43SJulian Elischer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 324cf49a43SJulian Elischer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 334cf49a43SJulian Elischer * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 344cf49a43SJulian Elischer * OF SUCH DAMAGE. 354cf49a43SJulian Elischer * 36164b576eSGleb Smirnoff * Copyright (c) 2007 Alexander Motin <mav@alkar.net> 37164b576eSGleb Smirnoff * All rights reserved. 38164b576eSGleb Smirnoff * 39164b576eSGleb Smirnoff * Redistribution and use in source and binary forms, with or without 40164b576eSGleb Smirnoff * modification, are permitted provided that the following conditions 41164b576eSGleb Smirnoff * are met: 42164b576eSGleb Smirnoff * 1. Redistributions of source code must retain the above copyright 43164b576eSGleb Smirnoff * notice unmodified, this list of conditions, and the following 44164b576eSGleb Smirnoff * disclaimer. 45164b576eSGleb Smirnoff * 2. Redistributions in binary form must reproduce the above copyright 46164b576eSGleb Smirnoff * notice, this list of conditions and the following disclaimer in the 47164b576eSGleb Smirnoff * documentation and/or other materials provided with the distribution. 48164b576eSGleb Smirnoff * 49164b576eSGleb Smirnoff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 50164b576eSGleb Smirnoff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 51164b576eSGleb Smirnoff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 52164b576eSGleb Smirnoff * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 53164b576eSGleb Smirnoff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 54164b576eSGleb Smirnoff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 55164b576eSGleb Smirnoff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 56164b576eSGleb Smirnoff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 57164b576eSGleb Smirnoff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 58164b576eSGleb Smirnoff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 59164b576eSGleb Smirnoff * SUCH DAMAGE. 60164b576eSGleb Smirnoff * 61164b576eSGleb Smirnoff * Authors: Archie Cobbs <archie@freebsd.org>, Alexander Motin <mav@alkar.net> 624cf49a43SJulian Elischer * 634cf49a43SJulian Elischer * $FreeBSD$ 6474f5c6aaSJulian Elischer * $Whistle: ng_ppp.c,v 1.24 1999/11/01 09:24:52 julian Exp $ 654cf49a43SJulian Elischer */ 664cf49a43SJulian Elischer 674cf49a43SJulian Elischer /* 68164b576eSGleb Smirnoff * PPP node type data-flow. 69164b576eSGleb Smirnoff * 70164b576eSGleb Smirnoff * hook xmit layer recv hook 71164b576eSGleb Smirnoff * ------------------------------------ 72164b576eSGleb Smirnoff * inet -> -> inet 73164b576eSGleb Smirnoff * ipv6 -> -> ipv6 74164b576eSGleb Smirnoff * ipx -> proto -> ipx 75164b576eSGleb Smirnoff * atalk -> -> atalk 76164b576eSGleb Smirnoff * bypass -> -> bypass 77164b576eSGleb Smirnoff * -hcomp_xmit()----------proto_recv()- 78164b576eSGleb Smirnoff * vjc_ip <- <- vjc_ip 79164b576eSGleb Smirnoff * vjc_comp -> header compression -> vjc_comp 80164b576eSGleb Smirnoff * vjc_uncomp -> -> vjc_uncomp 8139c14742SGleb Smirnoff * vjc_vjip -> 82164b576eSGleb Smirnoff * -comp_xmit()-----------hcomp_recv()- 83164b576eSGleb Smirnoff * compress <- compression <- decompress 84164b576eSGleb Smirnoff * compress -> -> decompress 85164b576eSGleb Smirnoff * -crypt_xmit()-----------comp_recv()- 86164b576eSGleb Smirnoff * encrypt <- encryption <- decrypt 87164b576eSGleb Smirnoff * encrypt -> -> decrypt 88164b576eSGleb Smirnoff * -ml_xmit()-------------crypt_recv()- 89164b576eSGleb Smirnoff * multilink 90164b576eSGleb Smirnoff * -link_xmit()--------------ml_recv()- 91164b576eSGleb Smirnoff * linkX <- link <- linkX 92164b576eSGleb Smirnoff * 934cf49a43SJulian Elischer */ 944cf49a43SJulian Elischer 954cf49a43SJulian Elischer #include <sys/param.h> 964cf49a43SJulian Elischer #include <sys/systm.h> 974cf49a43SJulian Elischer #include <sys/kernel.h> 98104a9b7eSAlexander Kabaev #include <sys/limits.h> 99a9b3dca5SArchie Cobbs #include <sys/time.h> 1004cf49a43SJulian Elischer #include <sys/mbuf.h> 1014cf49a43SJulian Elischer #include <sys/malloc.h> 10239228864SAlexander Motin #include <sys/endian.h> 1034cf49a43SJulian Elischer #include <sys/errno.h> 1045b664c7cSPoul-Henning Kamp #include <sys/ctype.h> 1054cf49a43SJulian Elischer 1064cf49a43SJulian Elischer #include <netgraph/ng_message.h> 1074cf49a43SJulian Elischer #include <netgraph/netgraph.h> 108f8307e12SArchie Cobbs #include <netgraph/ng_parse.h> 1094cf49a43SJulian Elischer #include <netgraph/ng_ppp.h> 1103949bee8SArchie Cobbs #include <netgraph/ng_vjc.h> 1114cf49a43SJulian Elischer 1129c8c302fSJulian Elischer #ifdef NG_SEPARATE_MALLOC 113d745c852SEd Schouten static MALLOC_DEFINE(M_NETGRAPH_PPP, "netgraph_ppp", "netgraph ppp node"); 1149c8c302fSJulian Elischer #else 1159c8c302fSJulian Elischer #define M_NETGRAPH_PPP M_NETGRAPH 1169c8c302fSJulian Elischer #endif 1179c8c302fSJulian Elischer 1184cf49a43SJulian Elischer #define PROT_VALID(p) (((p) & 0x0101) == 0x0001) 1192b70adcbSArchie Cobbs #define PROT_COMPRESSABLE(p) (((p) & 0xff00) == 0x0000) 1204cf49a43SJulian Elischer 1213949bee8SArchie Cobbs /* Some PPP protocol numbers we're interested in */ 122164b576eSGleb Smirnoff #define PROT_ATALK 0x0029 1233949bee8SArchie Cobbs #define PROT_COMPD 0x00fd 1243949bee8SArchie Cobbs #define PROT_CRYPTD 0x0053 1253949bee8SArchie Cobbs #define PROT_IP 0x0021 126a9b3dca5SArchie Cobbs #define PROT_IPV6 0x0057 1272b70adcbSArchie Cobbs #define PROT_IPX 0x002b 128d690a6e7SArchie Cobbs #define PROT_LCP 0xc021 1293949bee8SArchie Cobbs #define PROT_MP 0x003d 1303949bee8SArchie Cobbs #define PROT_VJCOMP 0x002d 1313949bee8SArchie Cobbs #define PROT_VJUNCOMP 0x002f 1323949bee8SArchie Cobbs 1333949bee8SArchie Cobbs /* Multilink PPP definitions */ 1343949bee8SArchie Cobbs #define MP_INITIAL_SEQ 0 /* per RFC 1990 */ 1353949bee8SArchie Cobbs #define MP_MIN_LINK_MRU 32 1363949bee8SArchie Cobbs 1373949bee8SArchie Cobbs #define MP_SHORT_SEQ_MASK 0x00000fff /* short seq # mask */ 1383949bee8SArchie Cobbs #define MP_SHORT_SEQ_HIBIT 0x00000800 /* short seq # high bit */ 1393949bee8SArchie Cobbs #define MP_SHORT_FIRST_FLAG 0x00008000 /* first fragment in frame */ 1403949bee8SArchie Cobbs #define MP_SHORT_LAST_FLAG 0x00004000 /* last fragment in frame */ 1413949bee8SArchie Cobbs 1423949bee8SArchie Cobbs #define MP_LONG_SEQ_MASK 0x00ffffff /* long seq # mask */ 1433949bee8SArchie Cobbs #define MP_LONG_SEQ_HIBIT 0x00800000 /* long seq # high bit */ 1443949bee8SArchie Cobbs #define MP_LONG_FIRST_FLAG 0x80000000 /* first fragment in frame */ 1453949bee8SArchie Cobbs #define MP_LONG_LAST_FLAG 0x40000000 /* last fragment in frame */ 1463949bee8SArchie Cobbs 1476f16db81SArchie Cobbs #define MP_NOSEQ 0x7fffffff /* impossible sequence number */ 1483949bee8SArchie Cobbs 1493949bee8SArchie Cobbs /* Sign extension of MP sequence numbers */ 1503949bee8SArchie Cobbs #define MP_SHORT_EXTEND(s) (((s) & MP_SHORT_SEQ_HIBIT) ? \ 1516f16db81SArchie Cobbs ((s) | ~MP_SHORT_SEQ_MASK) \ 1526f16db81SArchie Cobbs : ((s) & MP_SHORT_SEQ_MASK)) 1533949bee8SArchie Cobbs #define MP_LONG_EXTEND(s) (((s) & MP_LONG_SEQ_HIBIT) ? \ 1546f16db81SArchie Cobbs ((s) | ~MP_LONG_SEQ_MASK) \ 1556f16db81SArchie Cobbs : ((s) & MP_LONG_SEQ_MASK)) 1563949bee8SArchie Cobbs 157053359b7SPedro F. Giffuni /* Comparison of MP sequence numbers. Note: all sequence numbers 1586f16db81SArchie Cobbs except priv->xseq are stored with the sign bit extended. */ 1596f16db81SArchie Cobbs #define MP_SHORT_SEQ_DIFF(x,y) MP_SHORT_EXTEND((x) - (y)) 1606f16db81SArchie Cobbs #define MP_LONG_SEQ_DIFF(x,y) MP_LONG_EXTEND((x) - (y)) 1613949bee8SArchie Cobbs 1626f16db81SArchie Cobbs #define MP_RECV_SEQ_DIFF(priv,x,y) \ 1636f16db81SArchie Cobbs ((priv)->conf.recvShortSeq ? \ 1643949bee8SArchie Cobbs MP_SHORT_SEQ_DIFF((x), (y)) : \ 1653949bee8SArchie Cobbs MP_LONG_SEQ_DIFF((x), (y))) 1663949bee8SArchie Cobbs 1676f16db81SArchie Cobbs /* Increment receive sequence number */ 1686f16db81SArchie Cobbs #define MP_NEXT_RECV_SEQ(priv,seq) \ 169b5a60ddbSArchie Cobbs ((priv)->conf.recvShortSeq ? \ 170b5a60ddbSArchie Cobbs MP_SHORT_EXTEND((seq) + 1) : \ 171b5a60ddbSArchie Cobbs MP_LONG_EXTEND((seq) + 1)) 172a9b3dca5SArchie Cobbs 173164b576eSGleb Smirnoff /* Don't fragment transmitted packets to parts smaller than this */ 174164b576eSGleb Smirnoff #define MP_MIN_FRAG_LEN 32 175a9b3dca5SArchie Cobbs 176a9b3dca5SArchie Cobbs /* Maximum fragment reasssembly queue length */ 177a9b3dca5SArchie Cobbs #define MP_MAX_QUEUE_LEN 128 178a9b3dca5SArchie Cobbs 179a9b3dca5SArchie Cobbs /* Fragment queue scanner period */ 180a9b3dca5SArchie Cobbs #define MP_FRAGTIMER_INTERVAL (hz/2) 181a9b3dca5SArchie Cobbs 182fd58342cSAlexander Motin /* Average link overhead. XXX: Should be given by user-level */ 183fd58342cSAlexander Motin #define MP_AVERAGE_LINK_OVERHEAD 16 184fd58342cSAlexander Motin 185164b576eSGleb Smirnoff /* Keep this equal to ng_ppp_hook_names lower! */ 186164b576eSGleb Smirnoff #define HOOK_INDEX_MAX 13 187164b576eSGleb Smirnoff 1883949bee8SArchie Cobbs /* We store incoming fragments this way */ 1893949bee8SArchie Cobbs struct ng_ppp_frag { 190a9b3dca5SArchie Cobbs int seq; /* fragment seq# */ 191164b576eSGleb Smirnoff uint8_t first; /* First in packet? */ 192164b576eSGleb Smirnoff uint8_t last; /* Last in packet? */ 193a9b3dca5SArchie Cobbs struct timeval timestamp; /* time of reception */ 194a9b3dca5SArchie Cobbs struct mbuf *data; /* Fragment data */ 1955f90cac7SKirk McKusick TAILQ_ENTRY(ng_ppp_frag) f_qent; /* Fragment queue */ 1963949bee8SArchie Cobbs }; 1973949bee8SArchie Cobbs 198a9b3dca5SArchie Cobbs /* Per-link private information */ 199a9b3dca5SArchie Cobbs struct ng_ppp_link { 200a9b3dca5SArchie Cobbs struct ng_ppp_link_conf conf; /* link configuration */ 201d6fe462aSAlexander Motin struct ng_ppp_link_stat64 stats; /* link stats */ 202a9b3dca5SArchie Cobbs hook_p hook; /* connection to link data */ 2036f16db81SArchie Cobbs int32_t seq; /* highest rec'd seq# - MSEQ */ 204164b576eSGleb Smirnoff uint32_t latency; /* calculated link latency */ 205164b576eSGleb Smirnoff struct timeval lastWrite; /* time of last write for MP */ 206164b576eSGleb Smirnoff int bytesInQueue; /* bytes in the output queue for MP */ 207a9b3dca5SArchie Cobbs }; 208a9b3dca5SArchie Cobbs 209a9b3dca5SArchie Cobbs /* Total per-node private information */ 21062838faeSArchie Cobbs struct ng_ppp_private { 211a9b3dca5SArchie Cobbs struct ng_ppp_bund_conf conf; /* bundle config */ 212d6fe462aSAlexander Motin struct ng_ppp_link_stat64 bundleStats; /* bundle stats */ 213a9b3dca5SArchie Cobbs struct ng_ppp_link links[NG_PPP_MAX_LINKS];/* per-link info */ 2146f16db81SArchie Cobbs int32_t xseq; /* next out MP seq # */ 2156f16db81SArchie Cobbs int32_t mseq; /* min links[i].seq */ 216053359b7SPedro F. Giffuni uint16_t activeLinks[NG_PPP_MAX_LINKS]; /* indices */ 217164b576eSGleb Smirnoff uint16_t numActiveLinks; /* how many links up */ 218164b576eSGleb Smirnoff uint16_t lastLink; /* for round robin */ 219164b576eSGleb Smirnoff uint8_t vjCompHooked; /* VJ comp hooked up? */ 220164b576eSGleb Smirnoff uint8_t allLinksEqual; /* all xmit the same? */ 221a9b3dca5SArchie Cobbs hook_p hooks[HOOK_INDEX_MAX]; /* non-link hooks */ 22293caf2e2SAlexander Motin struct ng_ppp_frag fragsmem[MP_MAX_QUEUE_LEN]; /* fragments storage */ 2235f90cac7SKirk McKusick TAILQ_HEAD(ng_ppp_fraglist, ng_ppp_frag) /* fragment queue */ 224a9b3dca5SArchie Cobbs frags; 22593caf2e2SAlexander Motin TAILQ_HEAD(ng_ppp_fragfreelist, ng_ppp_frag) /* free fragment queue */ 22693caf2e2SAlexander Motin fragsfree; 2279d952bd3SGleb Smirnoff struct callout fragTimer; /* fraq queue check */ 228e89c1507SAlexander Motin struct mtx rmtx; /* recv mutex */ 229e89c1507SAlexander Motin struct mtx xmtx; /* xmit mutex */ 2304cf49a43SJulian Elischer }; 23162838faeSArchie Cobbs typedef struct ng_ppp_private *priv_p; 2324cf49a43SJulian Elischer 2334cf49a43SJulian Elischer /* Netgraph node methods */ 23474f5c6aaSJulian Elischer static ng_constructor_t ng_ppp_constructor; 23574f5c6aaSJulian Elischer static ng_rcvmsg_t ng_ppp_rcvmsg; 236069154d5SJulian Elischer static ng_shutdown_t ng_ppp_shutdown; 23774f5c6aaSJulian Elischer static ng_newhook_t ng_ppp_newhook; 23874f5c6aaSJulian Elischer static ng_rcvdata_t ng_ppp_rcvdata; 23974f5c6aaSJulian Elischer static ng_disconnect_t ng_ppp_disconnect; 2404cf49a43SJulian Elischer 241164b576eSGleb Smirnoff static ng_rcvdata_t ng_ppp_rcvdata_inet; 24235853c2cSGleb Smirnoff static ng_rcvdata_t ng_ppp_rcvdata_inet_fast; 243164b576eSGleb Smirnoff static ng_rcvdata_t ng_ppp_rcvdata_ipv6; 244164b576eSGleb Smirnoff static ng_rcvdata_t ng_ppp_rcvdata_ipx; 245164b576eSGleb Smirnoff static ng_rcvdata_t ng_ppp_rcvdata_atalk; 246164b576eSGleb Smirnoff static ng_rcvdata_t ng_ppp_rcvdata_bypass; 247164b576eSGleb Smirnoff 248164b576eSGleb Smirnoff static ng_rcvdata_t ng_ppp_rcvdata_vjc_ip; 249164b576eSGleb Smirnoff static ng_rcvdata_t ng_ppp_rcvdata_vjc_comp; 250164b576eSGleb Smirnoff static ng_rcvdata_t ng_ppp_rcvdata_vjc_uncomp; 251164b576eSGleb Smirnoff static ng_rcvdata_t ng_ppp_rcvdata_vjc_vjip; 252164b576eSGleb Smirnoff 253164b576eSGleb Smirnoff static ng_rcvdata_t ng_ppp_rcvdata_compress; 254164b576eSGleb Smirnoff static ng_rcvdata_t ng_ppp_rcvdata_decompress; 255164b576eSGleb Smirnoff 256164b576eSGleb Smirnoff static ng_rcvdata_t ng_ppp_rcvdata_encrypt; 257164b576eSGleb Smirnoff static ng_rcvdata_t ng_ppp_rcvdata_decrypt; 258164b576eSGleb Smirnoff 259053359b7SPedro F. Giffuni /* We use integer indices to refer to the non-link hooks. */ 260164b576eSGleb Smirnoff static const struct { 261164b576eSGleb Smirnoff char *const name; 262164b576eSGleb Smirnoff ng_rcvdata_t *fn; 263164b576eSGleb Smirnoff } ng_ppp_hook_names[] = { 264164b576eSGleb Smirnoff #define HOOK_INDEX_ATALK 0 265164b576eSGleb Smirnoff { NG_PPP_HOOK_ATALK, ng_ppp_rcvdata_atalk }, 266164b576eSGleb Smirnoff #define HOOK_INDEX_BYPASS 1 267164b576eSGleb Smirnoff { NG_PPP_HOOK_BYPASS, ng_ppp_rcvdata_bypass }, 268164b576eSGleb Smirnoff #define HOOK_INDEX_COMPRESS 2 269164b576eSGleb Smirnoff { NG_PPP_HOOK_COMPRESS, ng_ppp_rcvdata_compress }, 270164b576eSGleb Smirnoff #define HOOK_INDEX_ENCRYPT 3 271164b576eSGleb Smirnoff { NG_PPP_HOOK_ENCRYPT, ng_ppp_rcvdata_encrypt }, 272164b576eSGleb Smirnoff #define HOOK_INDEX_DECOMPRESS 4 273164b576eSGleb Smirnoff { NG_PPP_HOOK_DECOMPRESS, ng_ppp_rcvdata_decompress }, 274164b576eSGleb Smirnoff #define HOOK_INDEX_DECRYPT 5 275164b576eSGleb Smirnoff { NG_PPP_HOOK_DECRYPT, ng_ppp_rcvdata_decrypt }, 276164b576eSGleb Smirnoff #define HOOK_INDEX_INET 6 277164b576eSGleb Smirnoff { NG_PPP_HOOK_INET, ng_ppp_rcvdata_inet }, 278164b576eSGleb Smirnoff #define HOOK_INDEX_IPX 7 279164b576eSGleb Smirnoff { NG_PPP_HOOK_IPX, ng_ppp_rcvdata_ipx }, 280164b576eSGleb Smirnoff #define HOOK_INDEX_VJC_COMP 8 281164b576eSGleb Smirnoff { NG_PPP_HOOK_VJC_COMP, ng_ppp_rcvdata_vjc_comp }, 282164b576eSGleb Smirnoff #define HOOK_INDEX_VJC_IP 9 283164b576eSGleb Smirnoff { NG_PPP_HOOK_VJC_IP, ng_ppp_rcvdata_vjc_ip }, 284164b576eSGleb Smirnoff #define HOOK_INDEX_VJC_UNCOMP 10 285164b576eSGleb Smirnoff { NG_PPP_HOOK_VJC_UNCOMP, ng_ppp_rcvdata_vjc_uncomp }, 286164b576eSGleb Smirnoff #define HOOK_INDEX_VJC_VJIP 11 287164b576eSGleb Smirnoff { NG_PPP_HOOK_VJC_VJIP, ng_ppp_rcvdata_vjc_vjip }, 288164b576eSGleb Smirnoff #define HOOK_INDEX_IPV6 12 289164b576eSGleb Smirnoff { NG_PPP_HOOK_IPV6, ng_ppp_rcvdata_ipv6 }, 290164b576eSGleb Smirnoff { NULL, NULL } 291164b576eSGleb Smirnoff }; 292164b576eSGleb Smirnoff 2933949bee8SArchie Cobbs /* Helper functions */ 294164b576eSGleb Smirnoff static int ng_ppp_proto_recv(node_p node, item_p item, uint16_t proto, 295164b576eSGleb Smirnoff uint16_t linkNum); 296164b576eSGleb Smirnoff static int ng_ppp_hcomp_xmit(node_p node, item_p item, uint16_t proto); 297164b576eSGleb Smirnoff static int ng_ppp_hcomp_recv(node_p node, item_p item, uint16_t proto, 298164b576eSGleb Smirnoff uint16_t linkNum); 299164b576eSGleb Smirnoff static int ng_ppp_comp_xmit(node_p node, item_p item, uint16_t proto); 300164b576eSGleb Smirnoff static int ng_ppp_comp_recv(node_p node, item_p item, uint16_t proto, 301164b576eSGleb Smirnoff uint16_t linkNum); 302164b576eSGleb Smirnoff static int ng_ppp_crypt_xmit(node_p node, item_p item, uint16_t proto); 303164b576eSGleb Smirnoff static int ng_ppp_crypt_recv(node_p node, item_p item, uint16_t proto, 304164b576eSGleb Smirnoff uint16_t linkNum); 305164b576eSGleb Smirnoff static int ng_ppp_mp_xmit(node_p node, item_p item, uint16_t proto); 306164b576eSGleb Smirnoff static int ng_ppp_mp_recv(node_p node, item_p item, uint16_t proto, 307164b576eSGleb Smirnoff uint16_t linkNum); 308164b576eSGleb Smirnoff static int ng_ppp_link_xmit(node_p node, item_p item, uint16_t proto, 309e89c1507SAlexander Motin uint16_t linkNum, int plen); 310164b576eSGleb Smirnoff 31139c14742SGleb Smirnoff static int ng_ppp_bypass(node_p node, item_p item, uint16_t proto, 31239c14742SGleb Smirnoff uint16_t linkNum); 31339c14742SGleb Smirnoff 3148e8f114eSAlexander Motin static void ng_ppp_bump_mseq(node_p node, int32_t new_mseq); 3158e8f114eSAlexander Motin static int ng_ppp_frag_drop(node_p node); 316a9b3dca5SArchie Cobbs static int ng_ppp_check_packet(node_p node); 3173ca24c28SJulian Elischer static void ng_ppp_get_packet(node_p node, struct mbuf **mp); 3185716b3f1SAlexander Motin static int ng_ppp_frag_process(node_p node, item_p oitem); 319a9b3dca5SArchie Cobbs static int ng_ppp_frag_trim(node_p node); 3209d952bd3SGleb Smirnoff static void ng_ppp_frag_timeout(node_p node, hook_p hook, void *arg1, 3219d952bd3SGleb Smirnoff int arg2); 322a9b3dca5SArchie Cobbs static void ng_ppp_frag_checkstale(node_p node); 323a9b3dca5SArchie Cobbs static void ng_ppp_frag_reset(node_p node); 3243949bee8SArchie Cobbs static void ng_ppp_mp_strategy(node_p node, int len, int *distrib); 3258250de83SGleb Smirnoff static int ng_ppp_intcmp(void *latency, const void *v1, const void *v2); 326164b576eSGleb Smirnoff static struct mbuf *ng_ppp_addproto(struct mbuf *m, uint16_t proto, int compOK); 327164b576eSGleb Smirnoff static struct mbuf *ng_ppp_cutproto(struct mbuf *m, uint16_t *proto); 328d690a6e7SArchie Cobbs static struct mbuf *ng_ppp_prepend(struct mbuf *m, const void *buf, int len); 3293949bee8SArchie Cobbs static int ng_ppp_config_valid(node_p node, 330a9b3dca5SArchie Cobbs const struct ng_ppp_node_conf *newConf); 3313949bee8SArchie Cobbs static void ng_ppp_update(node_p node, int newConf); 332a9b3dca5SArchie Cobbs static void ng_ppp_start_frag_timer(node_p node); 333a9b3dca5SArchie Cobbs static void ng_ppp_stop_frag_timer(node_p node); 3344cf49a43SJulian Elischer 3356f16db81SArchie Cobbs /* Parse type for struct ng_ppp_mp_state_type */ 3366f16db81SArchie Cobbs static const struct ng_parse_fixedarray_info ng_ppp_rseq_array_info = { 3376f16db81SArchie Cobbs &ng_parse_hint32_type, 3386f16db81SArchie Cobbs NG_PPP_MAX_LINKS 3396f16db81SArchie Cobbs }; 3406f16db81SArchie Cobbs static const struct ng_parse_type ng_ppp_rseq_array_type = { 3416f16db81SArchie Cobbs &ng_parse_fixedarray_type, 3426f16db81SArchie Cobbs &ng_ppp_rseq_array_info, 3436f16db81SArchie Cobbs }; 344f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_ppp_mp_state_type_fields[] 3456f16db81SArchie Cobbs = NG_PPP_MP_STATE_TYPE_INFO(&ng_ppp_rseq_array_type); 3466f16db81SArchie Cobbs static const struct ng_parse_type ng_ppp_mp_state_type = { 3476f16db81SArchie Cobbs &ng_parse_struct_type, 348f0184ff8SArchie Cobbs &ng_ppp_mp_state_type_fields 3496f16db81SArchie Cobbs }; 3506f16db81SArchie Cobbs 351a9b3dca5SArchie Cobbs /* Parse type for struct ng_ppp_link_conf */ 352f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_ppp_link_type_fields[] 353f0184ff8SArchie Cobbs = NG_PPP_LINK_TYPE_INFO; 354f8307e12SArchie Cobbs static const struct ng_parse_type ng_ppp_link_type = { 355f8307e12SArchie Cobbs &ng_parse_struct_type, 356f0184ff8SArchie Cobbs &ng_ppp_link_type_fields 357f8307e12SArchie Cobbs }; 358f8307e12SArchie Cobbs 359a9b3dca5SArchie Cobbs /* Parse type for struct ng_ppp_bund_conf */ 360f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_ppp_bund_type_fields[] 361f0184ff8SArchie Cobbs = NG_PPP_BUND_TYPE_INFO; 362a9b3dca5SArchie Cobbs static const struct ng_parse_type ng_ppp_bund_type = { 363a9b3dca5SArchie Cobbs &ng_parse_struct_type, 364f0184ff8SArchie Cobbs &ng_ppp_bund_type_fields 365a9b3dca5SArchie Cobbs }; 366a9b3dca5SArchie Cobbs 367a9b3dca5SArchie Cobbs /* Parse type for struct ng_ppp_node_conf */ 3686f16db81SArchie Cobbs static const struct ng_parse_fixedarray_info ng_ppp_array_info = { 369f8307e12SArchie Cobbs &ng_ppp_link_type, 370f8307e12SArchie Cobbs NG_PPP_MAX_LINKS 371f8307e12SArchie Cobbs }; 372f8307e12SArchie Cobbs static const struct ng_parse_type ng_ppp_link_array_type = { 373f8307e12SArchie Cobbs &ng_parse_fixedarray_type, 374f8307e12SArchie Cobbs &ng_ppp_array_info, 375f8307e12SArchie Cobbs }; 376f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_ppp_conf_type_fields[] 377a9b3dca5SArchie Cobbs = NG_PPP_CONFIG_TYPE_INFO(&ng_ppp_bund_type, &ng_ppp_link_array_type); 378a9b3dca5SArchie Cobbs static const struct ng_parse_type ng_ppp_conf_type = { 379f8307e12SArchie Cobbs &ng_parse_struct_type, 380f0184ff8SArchie Cobbs &ng_ppp_conf_type_fields 381f8307e12SArchie Cobbs }; 382f8307e12SArchie Cobbs 383f8307e12SArchie Cobbs /* Parse type for struct ng_ppp_link_stat */ 384f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_ppp_stats_type_fields[] 385f0184ff8SArchie Cobbs = NG_PPP_STATS_TYPE_INFO; 386f8307e12SArchie Cobbs static const struct ng_parse_type ng_ppp_stats_type = { 387f8307e12SArchie Cobbs &ng_parse_struct_type, 388f0184ff8SArchie Cobbs &ng_ppp_stats_type_fields 389f8307e12SArchie Cobbs }; 390f8307e12SArchie Cobbs 391d6fe462aSAlexander Motin /* Parse type for struct ng_ppp_link_stat64 */ 392d6fe462aSAlexander Motin static const struct ng_parse_struct_field ng_ppp_stats64_type_fields[] 393d6fe462aSAlexander Motin = NG_PPP_STATS64_TYPE_INFO; 394d6fe462aSAlexander Motin static const struct ng_parse_type ng_ppp_stats64_type = { 395d6fe462aSAlexander Motin &ng_parse_struct_type, 396d6fe462aSAlexander Motin &ng_ppp_stats64_type_fields 397d6fe462aSAlexander Motin }; 398d6fe462aSAlexander Motin 399f8307e12SArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */ 400f8307e12SArchie Cobbs static const struct ng_cmdlist ng_ppp_cmds[] = { 401f8307e12SArchie Cobbs { 402f8307e12SArchie Cobbs NGM_PPP_COOKIE, 403f8307e12SArchie Cobbs NGM_PPP_SET_CONFIG, 404f8307e12SArchie Cobbs "setconfig", 405a9b3dca5SArchie Cobbs &ng_ppp_conf_type, 406f8307e12SArchie Cobbs NULL 407f8307e12SArchie Cobbs }, 408f8307e12SArchie Cobbs { 409f8307e12SArchie Cobbs NGM_PPP_COOKIE, 410f8307e12SArchie Cobbs NGM_PPP_GET_CONFIG, 411f8307e12SArchie Cobbs "getconfig", 412f8307e12SArchie Cobbs NULL, 413a9b3dca5SArchie Cobbs &ng_ppp_conf_type 414f8307e12SArchie Cobbs }, 415f8307e12SArchie Cobbs { 416f8307e12SArchie Cobbs NGM_PPP_COOKIE, 4176f16db81SArchie Cobbs NGM_PPP_GET_MP_STATE, 4186f16db81SArchie Cobbs "getmpstate", 4196f16db81SArchie Cobbs NULL, 4206f16db81SArchie Cobbs &ng_ppp_mp_state_type 4216f16db81SArchie Cobbs }, 4226f16db81SArchie Cobbs { 4236f16db81SArchie Cobbs NGM_PPP_COOKIE, 424f8307e12SArchie Cobbs NGM_PPP_GET_LINK_STATS, 425f8307e12SArchie Cobbs "getstats", 426f8307e12SArchie Cobbs &ng_parse_int16_type, 427f8307e12SArchie Cobbs &ng_ppp_stats_type 428f8307e12SArchie Cobbs }, 429f8307e12SArchie Cobbs { 430f8307e12SArchie Cobbs NGM_PPP_COOKIE, 431f8307e12SArchie Cobbs NGM_PPP_CLR_LINK_STATS, 432f8307e12SArchie Cobbs "clrstats", 433f8307e12SArchie Cobbs &ng_parse_int16_type, 434f8307e12SArchie Cobbs NULL 435f8307e12SArchie Cobbs }, 436f8307e12SArchie Cobbs { 437f8307e12SArchie Cobbs NGM_PPP_COOKIE, 438f8307e12SArchie Cobbs NGM_PPP_GETCLR_LINK_STATS, 439f8307e12SArchie Cobbs "getclrstats", 440f8307e12SArchie Cobbs &ng_parse_int16_type, 441f8307e12SArchie Cobbs &ng_ppp_stats_type 442f8307e12SArchie Cobbs }, 443d6fe462aSAlexander Motin { 444d6fe462aSAlexander Motin NGM_PPP_COOKIE, 445d6fe462aSAlexander Motin NGM_PPP_GET_LINK_STATS64, 446d6fe462aSAlexander Motin "getstats64", 447d6fe462aSAlexander Motin &ng_parse_int16_type, 448d6fe462aSAlexander Motin &ng_ppp_stats64_type 449d6fe462aSAlexander Motin }, 450d6fe462aSAlexander Motin { 451d6fe462aSAlexander Motin NGM_PPP_COOKIE, 452d6fe462aSAlexander Motin NGM_PPP_GETCLR_LINK_STATS64, 453d6fe462aSAlexander Motin "getclrstats64", 454d6fe462aSAlexander Motin &ng_parse_int16_type, 455d6fe462aSAlexander Motin &ng_ppp_stats64_type 456d6fe462aSAlexander Motin }, 457f8307e12SArchie Cobbs { 0 } 458f8307e12SArchie Cobbs }; 459f8307e12SArchie Cobbs 4604cf49a43SJulian Elischer /* Node type descriptor */ 4613949bee8SArchie Cobbs static struct ng_type ng_ppp_typestruct = { 462f8aae777SJulian Elischer .version = NG_ABI_VERSION, 463f8aae777SJulian Elischer .name = NG_PPP_NODE_TYPE, 464f8aae777SJulian Elischer .constructor = ng_ppp_constructor, 465f8aae777SJulian Elischer .rcvmsg = ng_ppp_rcvmsg, 466f8aae777SJulian Elischer .shutdown = ng_ppp_shutdown, 467f8aae777SJulian Elischer .newhook = ng_ppp_newhook, 468f8aae777SJulian Elischer .rcvdata = ng_ppp_rcvdata, 469f8aae777SJulian Elischer .disconnect = ng_ppp_disconnect, 470f8aae777SJulian Elischer .cmdlist = ng_ppp_cmds, 4714cf49a43SJulian Elischer }; 4723949bee8SArchie Cobbs NETGRAPH_INIT(ppp, &ng_ppp_typestruct); 4734cf49a43SJulian Elischer 474d690a6e7SArchie Cobbs /* Address and control field header */ 475164b576eSGleb Smirnoff static const uint8_t ng_ppp_acf[2] = { 0xff, 0x03 }; 476d690a6e7SArchie Cobbs 477a9b3dca5SArchie Cobbs /* Maximum time we'll let a complete incoming packet sit in the queue */ 478a9b3dca5SArchie Cobbs static const struct timeval ng_ppp_max_staleness = { 2, 0 }; /* 2 seconds */ 479a9b3dca5SArchie Cobbs 4804cf49a43SJulian Elischer #define ERROUT(x) do { error = (x); goto done; } while (0) 4814cf49a43SJulian Elischer 4824cf49a43SJulian Elischer /************************************************************************ 4834cf49a43SJulian Elischer NETGRAPH NODE STUFF 4844cf49a43SJulian Elischer ************************************************************************/ 4854cf49a43SJulian Elischer 4864cf49a43SJulian Elischer /* 4873949bee8SArchie Cobbs * Node type constructor 4884cf49a43SJulian Elischer */ 4894cf49a43SJulian Elischer static int 490069154d5SJulian Elischer ng_ppp_constructor(node_p node) 4914cf49a43SJulian Elischer { 4924cf49a43SJulian Elischer priv_p priv; 493069154d5SJulian Elischer int i; 4944cf49a43SJulian Elischer 4954cf49a43SJulian Elischer /* Allocate private structure */ 496674d86bfSGleb Smirnoff priv = malloc(sizeof(*priv), M_NETGRAPH_PPP, M_WAITOK | M_ZERO); 4974cf49a43SJulian Elischer 49830400f03SJulian Elischer NG_NODE_SET_PRIVATE(node, priv); 4994cf49a43SJulian Elischer 5003949bee8SArchie Cobbs /* Initialize state */ 5015f90cac7SKirk McKusick TAILQ_INIT(&priv->frags); 50293caf2e2SAlexander Motin TAILQ_INIT(&priv->fragsfree); 50393caf2e2SAlexander Motin for (i = 0; i < MP_MAX_QUEUE_LEN; i++) 50493caf2e2SAlexander Motin TAILQ_INSERT_TAIL(&priv->fragsfree, &priv->fragsmem[i], f_qent); 505a9b3dca5SArchie Cobbs for (i = 0; i < NG_PPP_MAX_LINKS; i++) 506a9b3dca5SArchie Cobbs priv->links[i].seq = MP_NOSEQ; 5079d952bd3SGleb Smirnoff ng_callout_init(&priv->fragTimer); 5083949bee8SArchie Cobbs 509e89c1507SAlexander Motin mtx_init(&priv->rmtx, "ng_ppp_recv", NULL, MTX_DEF); 510e89c1507SAlexander Motin mtx_init(&priv->xmtx, "ng_ppp_xmit", NULL, MTX_DEF); 511e89c1507SAlexander Motin 5124cf49a43SJulian Elischer /* Done */ 5134cf49a43SJulian Elischer return (0); 5144cf49a43SJulian Elischer } 5154cf49a43SJulian Elischer 5164cf49a43SJulian Elischer /* 5174cf49a43SJulian Elischer * Give our OK for a hook to be added 5184cf49a43SJulian Elischer */ 5194cf49a43SJulian Elischer static int 5204cf49a43SJulian Elischer ng_ppp_newhook(node_p node, hook_p hook, const char *name) 5214cf49a43SJulian Elischer { 52230400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 5233949bee8SArchie Cobbs hook_p *hookPtr = NULL; 524164b576eSGleb Smirnoff int linkNum = -1; 5253949bee8SArchie Cobbs int hookIndex = -1; 5264cf49a43SJulian Elischer 5273949bee8SArchie Cobbs /* Figure out which hook it is */ 5283949bee8SArchie Cobbs if (strncmp(name, NG_PPP_HOOK_LINK_PREFIX, /* a link hook? */ 5293949bee8SArchie Cobbs strlen(NG_PPP_HOOK_LINK_PREFIX)) == 0) { 53025792ef3SArchie Cobbs const char *cp; 53125792ef3SArchie Cobbs char *eptr; 5323949bee8SArchie Cobbs 5332b70adcbSArchie Cobbs cp = name + strlen(NG_PPP_HOOK_LINK_PREFIX); 5342b70adcbSArchie Cobbs if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0')) 5354cf49a43SJulian Elischer return (EINVAL); 5362b70adcbSArchie Cobbs linkNum = (int)strtoul(cp, &eptr, 10); 5372b70adcbSArchie Cobbs if (*eptr != '\0' || linkNum < 0 || linkNum >= NG_PPP_MAX_LINKS) 5383949bee8SArchie Cobbs return (EINVAL); 539a9b3dca5SArchie Cobbs hookPtr = &priv->links[linkNum].hook; 5403949bee8SArchie Cobbs hookIndex = ~linkNum; 541164b576eSGleb Smirnoff 542164b576eSGleb Smirnoff /* See if hook is already connected. */ 543164b576eSGleb Smirnoff if (*hookPtr != NULL) 544164b576eSGleb Smirnoff return (EISCONN); 545164b576eSGleb Smirnoff 546164b576eSGleb Smirnoff /* Disallow more than one link unless multilink is enabled. */ 547164b576eSGleb Smirnoff if (priv->links[linkNum].conf.enableLink && 548164b576eSGleb Smirnoff !priv->conf.enableMultilink && priv->numActiveLinks >= 1) 549164b576eSGleb Smirnoff return (ENODEV); 550164b576eSGleb Smirnoff 5513949bee8SArchie Cobbs } else { /* must be a non-link hook */ 5523949bee8SArchie Cobbs int i; 5534cf49a43SJulian Elischer 554164b576eSGleb Smirnoff for (i = 0; ng_ppp_hook_names[i].name != NULL; i++) { 555164b576eSGleb Smirnoff if (strcmp(name, ng_ppp_hook_names[i].name) == 0) { 5563949bee8SArchie Cobbs hookPtr = &priv->hooks[i]; 5573949bee8SArchie Cobbs hookIndex = i; 5583949bee8SArchie Cobbs break; 5593949bee8SArchie Cobbs } 5603949bee8SArchie Cobbs } 561164b576eSGleb Smirnoff if (ng_ppp_hook_names[i].name == NULL) 5623949bee8SArchie Cobbs return (EINVAL); /* no such hook */ 5633949bee8SArchie Cobbs 5643949bee8SArchie Cobbs /* See if hook is already connected */ 5653949bee8SArchie Cobbs if (*hookPtr != NULL) 5664cf49a43SJulian Elischer return (EISCONN); 5674cf49a43SJulian Elischer 568164b576eSGleb Smirnoff /* Every non-linkX hook have it's own function. */ 569164b576eSGleb Smirnoff NG_HOOK_SET_RCVDATA(hook, ng_ppp_hook_names[i].fn); 570164b576eSGleb Smirnoff } 5714cf49a43SJulian Elischer 5724cf49a43SJulian Elischer /* OK */ 5733949bee8SArchie Cobbs *hookPtr = hook; 5748214d60eSJohn Baldwin NG_HOOK_SET_PRIVATE(hook, (void *)(intptr_t)hookIndex); 5753949bee8SArchie Cobbs ng_ppp_update(node, 0); 5764cf49a43SJulian Elischer return (0); 5774cf49a43SJulian Elischer } 5784cf49a43SJulian Elischer 5794cf49a43SJulian Elischer /* 5804cf49a43SJulian Elischer * Receive a control message 5814cf49a43SJulian Elischer */ 5824cf49a43SJulian Elischer static int 583069154d5SJulian Elischer ng_ppp_rcvmsg(node_p node, item_p item, hook_p lasthook) 5844cf49a43SJulian Elischer { 58530400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 5864cf49a43SJulian Elischer struct ng_mesg *resp = NULL; 5874cf49a43SJulian Elischer int error = 0; 588069154d5SJulian Elischer struct ng_mesg *msg; 5894cf49a43SJulian Elischer 590069154d5SJulian Elischer NGI_GET_MSG(item, msg); 5914cf49a43SJulian Elischer switch (msg->header.typecookie) { 5924cf49a43SJulian Elischer case NGM_PPP_COOKIE: 5934cf49a43SJulian Elischer switch (msg->header.cmd) { 5943949bee8SArchie Cobbs case NGM_PPP_SET_CONFIG: 5953949bee8SArchie Cobbs { 596a9b3dca5SArchie Cobbs struct ng_ppp_node_conf *const conf = 597a9b3dca5SArchie Cobbs (struct ng_ppp_node_conf *)msg->data; 598a9b3dca5SArchie Cobbs int i; 5993949bee8SArchie Cobbs 6003949bee8SArchie Cobbs /* Check for invalid or illegal config */ 601a9b3dca5SArchie Cobbs if (msg->header.arglen != sizeof(*conf)) 6024cf49a43SJulian Elischer ERROUT(EINVAL); 603a9b3dca5SArchie Cobbs if (!ng_ppp_config_valid(node, conf)) 6043949bee8SArchie Cobbs ERROUT(EINVAL); 605a9b3dca5SArchie Cobbs 606a9b3dca5SArchie Cobbs /* Copy config */ 607a9b3dca5SArchie Cobbs priv->conf = conf->bund; 608a9b3dca5SArchie Cobbs for (i = 0; i < NG_PPP_MAX_LINKS; i++) 609a9b3dca5SArchie Cobbs priv->links[i].conf = conf->links[i]; 6103949bee8SArchie Cobbs ng_ppp_update(node, 1); 6114cf49a43SJulian Elischer break; 6123949bee8SArchie Cobbs } 6133949bee8SArchie Cobbs case NGM_PPP_GET_CONFIG: 614a9b3dca5SArchie Cobbs { 615a9b3dca5SArchie Cobbs struct ng_ppp_node_conf *conf; 616a9b3dca5SArchie Cobbs int i; 617a9b3dca5SArchie Cobbs 618a9b3dca5SArchie Cobbs NG_MKRESPONSE(resp, msg, sizeof(*conf), M_NOWAIT); 6194cf49a43SJulian Elischer if (resp == NULL) 6204cf49a43SJulian Elischer ERROUT(ENOMEM); 621a9b3dca5SArchie Cobbs conf = (struct ng_ppp_node_conf *)resp->data; 622a9b3dca5SArchie Cobbs conf->bund = priv->conf; 623a9b3dca5SArchie Cobbs for (i = 0; i < NG_PPP_MAX_LINKS; i++) 624a9b3dca5SArchie Cobbs conf->links[i] = priv->links[i].conf; 6254cf49a43SJulian Elischer break; 626a9b3dca5SArchie Cobbs } 6276f16db81SArchie Cobbs case NGM_PPP_GET_MP_STATE: 6286f16db81SArchie Cobbs { 6296f16db81SArchie Cobbs struct ng_ppp_mp_state *info; 6306f16db81SArchie Cobbs int i; 6316f16db81SArchie Cobbs 6326f16db81SArchie Cobbs NG_MKRESPONSE(resp, msg, sizeof(*info), M_NOWAIT); 6336f16db81SArchie Cobbs if (resp == NULL) 6346f16db81SArchie Cobbs ERROUT(ENOMEM); 6356f16db81SArchie Cobbs info = (struct ng_ppp_mp_state *)resp->data; 6366f16db81SArchie Cobbs bzero(info, sizeof(*info)); 6376f16db81SArchie Cobbs for (i = 0; i < NG_PPP_MAX_LINKS; i++) { 6386f16db81SArchie Cobbs if (priv->links[i].seq != MP_NOSEQ) 6396f16db81SArchie Cobbs info->rseq[i] = priv->links[i].seq; 6406f16db81SArchie Cobbs } 6416f16db81SArchie Cobbs info->mseq = priv->mseq; 6426f16db81SArchie Cobbs info->xseq = priv->xseq; 6436f16db81SArchie Cobbs break; 6446f16db81SArchie Cobbs } 6453949bee8SArchie Cobbs case NGM_PPP_GET_LINK_STATS: 6463949bee8SArchie Cobbs case NGM_PPP_CLR_LINK_STATS: 647e149c4e2SArchie Cobbs case NGM_PPP_GETCLR_LINK_STATS: 648d6fe462aSAlexander Motin case NGM_PPP_GET_LINK_STATS64: 649d6fe462aSAlexander Motin case NGM_PPP_GETCLR_LINK_STATS64: 6503949bee8SArchie Cobbs { 651d6fe462aSAlexander Motin struct ng_ppp_link_stat64 *stats; 652164b576eSGleb Smirnoff uint16_t linkNum; 6533949bee8SArchie Cobbs 654d6fe462aSAlexander Motin /* Process request. */ 655164b576eSGleb Smirnoff if (msg->header.arglen != sizeof(uint16_t)) 6563949bee8SArchie Cobbs ERROUT(EINVAL); 657164b576eSGleb Smirnoff linkNum = *((uint16_t *) msg->data); 6583949bee8SArchie Cobbs if (linkNum >= NG_PPP_MAX_LINKS 6593949bee8SArchie Cobbs && linkNum != NG_PPP_BUNDLE_LINKNUM) 6603949bee8SArchie Cobbs ERROUT(EINVAL); 6613949bee8SArchie Cobbs stats = (linkNum == NG_PPP_BUNDLE_LINKNUM) ? 662a9b3dca5SArchie Cobbs &priv->bundleStats : &priv->links[linkNum].stats; 663d6fe462aSAlexander Motin 664d6fe462aSAlexander Motin /* Make 64bit reply. */ 665d6fe462aSAlexander Motin if (msg->header.cmd == NGM_PPP_GET_LINK_STATS64 || 666d6fe462aSAlexander Motin msg->header.cmd == NGM_PPP_GETCLR_LINK_STATS64) { 667d6fe462aSAlexander Motin NG_MKRESPONSE(resp, msg, 668d6fe462aSAlexander Motin sizeof(struct ng_ppp_link_stat64), M_NOWAIT); 669d6fe462aSAlexander Motin if (resp == NULL) 670d6fe462aSAlexander Motin ERROUT(ENOMEM); 671d6fe462aSAlexander Motin bcopy(stats, resp->data, sizeof(*stats)); 672d6fe462aSAlexander Motin } else 673d6fe462aSAlexander Motin /* Make 32bit reply. */ 674d6fe462aSAlexander Motin if (msg->header.cmd == NGM_PPP_GET_LINK_STATS || 675d6fe462aSAlexander Motin msg->header.cmd == NGM_PPP_GETCLR_LINK_STATS) { 676d6fe462aSAlexander Motin struct ng_ppp_link_stat *rs; 6773949bee8SArchie Cobbs NG_MKRESPONSE(resp, msg, 6783949bee8SArchie Cobbs sizeof(struct ng_ppp_link_stat), M_NOWAIT); 6793949bee8SArchie Cobbs if (resp == NULL) 6803949bee8SArchie Cobbs ERROUT(ENOMEM); 681d6fe462aSAlexander Motin rs = (struct ng_ppp_link_stat *)resp->data; 682d6fe462aSAlexander Motin /* Truncate 64->32 bits. */ 683d6fe462aSAlexander Motin rs->xmitFrames = stats->xmitFrames; 684d6fe462aSAlexander Motin rs->xmitOctets = stats->xmitOctets; 685d6fe462aSAlexander Motin rs->recvFrames = stats->recvFrames; 686d6fe462aSAlexander Motin rs->recvOctets = stats->recvOctets; 687d6fe462aSAlexander Motin rs->badProtos = stats->badProtos; 688d6fe462aSAlexander Motin rs->runts = stats->runts; 689d6fe462aSAlexander Motin rs->dupFragments = stats->dupFragments; 690d6fe462aSAlexander Motin rs->dropFragments = stats->dropFragments; 691e149c4e2SArchie Cobbs } 692d6fe462aSAlexander Motin /* Clear stats. */ 693d6fe462aSAlexander Motin if (msg->header.cmd != NGM_PPP_GET_LINK_STATS && 694d6fe462aSAlexander Motin msg->header.cmd != NGM_PPP_GET_LINK_STATS64) 6953949bee8SArchie Cobbs bzero(stats, sizeof(*stats)); 6964cf49a43SJulian Elischer break; 6973949bee8SArchie Cobbs } 6984cf49a43SJulian Elischer default: 6994cf49a43SJulian Elischer error = EINVAL; 7004cf49a43SJulian Elischer break; 7014cf49a43SJulian Elischer } 7024cf49a43SJulian Elischer break; 7033949bee8SArchie Cobbs case NGM_VJC_COOKIE: 7043949bee8SArchie Cobbs { 705069154d5SJulian Elischer /* 706069154d5SJulian Elischer * Forward it to the vjc node. leave the 707069154d5SJulian Elischer * old return address alone. 70830400f03SJulian Elischer * If we have no hook, let NG_RESPOND_MSG 70930400f03SJulian Elischer * clean up any remaining resources. 71030400f03SJulian Elischer * Because we have no resp, the item will be freed 71130400f03SJulian Elischer * along with anything it references. Don't 71230400f03SJulian Elischer * let msg be freed twice. 713069154d5SJulian Elischer */ 714069154d5SJulian Elischer NGI_MSG(item) = msg; /* put it back in the item */ 71530400f03SJulian Elischer msg = NULL; 716164b576eSGleb Smirnoff if ((lasthook = priv->hooks[HOOK_INDEX_VJC_IP])) { 71730400f03SJulian Elischer NG_FWD_ITEM_HOOK(error, item, lasthook); 718069154d5SJulian Elischer } 719069154d5SJulian Elischer return (error); 7203949bee8SArchie Cobbs } 7214cf49a43SJulian Elischer default: 7224cf49a43SJulian Elischer error = EINVAL; 7234cf49a43SJulian Elischer break; 7244cf49a43SJulian Elischer } 7254cf49a43SJulian Elischer done: 726069154d5SJulian Elischer NG_RESPOND_MSG(error, node, item, resp); 727069154d5SJulian Elischer NG_FREE_MSG(msg); 7284cf49a43SJulian Elischer return (error); 7294cf49a43SJulian Elischer } 7304cf49a43SJulian Elischer 7314cf49a43SJulian Elischer /* 7324cf49a43SJulian Elischer * Destroy node 7334cf49a43SJulian Elischer */ 7344cf49a43SJulian Elischer static int 735069154d5SJulian Elischer ng_ppp_shutdown(node_p node) 7364cf49a43SJulian Elischer { 73730400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 7384cf49a43SJulian Elischer 739a9b3dca5SArchie Cobbs /* Stop fragment queue timer */ 740a9b3dca5SArchie Cobbs ng_ppp_stop_frag_timer(node); 741a9b3dca5SArchie Cobbs 7424cf49a43SJulian Elischer /* Take down netgraph node */ 743a9b3dca5SArchie Cobbs ng_ppp_frag_reset(node); 744e89c1507SAlexander Motin mtx_destroy(&priv->rmtx); 745e89c1507SAlexander Motin mtx_destroy(&priv->xmtx); 7464cf49a43SJulian Elischer bzero(priv, sizeof(*priv)); 7471ede983cSDag-Erling Smørgrav free(priv, M_NETGRAPH_PPP); 74830400f03SJulian Elischer NG_NODE_SET_PRIVATE(node, NULL); 74930400f03SJulian Elischer NG_NODE_UNREF(node); /* let the node escape */ 7504cf49a43SJulian Elischer return (0); 7514cf49a43SJulian Elischer } 7524cf49a43SJulian Elischer 7534cf49a43SJulian Elischer /* 7544cf49a43SJulian Elischer * Hook disconnection 7554cf49a43SJulian Elischer */ 7564cf49a43SJulian Elischer static int 7574cf49a43SJulian Elischer ng_ppp_disconnect(hook_p hook) 7584cf49a43SJulian Elischer { 75930400f03SJulian Elischer const node_p node = NG_HOOK_NODE(hook); 76030400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 7618214d60eSJohn Baldwin const int index = (intptr_t)NG_HOOK_PRIVATE(hook); 76262838faeSArchie Cobbs 76362838faeSArchie Cobbs /* Zero out hook pointer */ 76462838faeSArchie Cobbs if (index < 0) 765a9b3dca5SArchie Cobbs priv->links[~index].hook = NULL; 76662838faeSArchie Cobbs else 76762838faeSArchie Cobbs priv->hooks[index] = NULL; 76862838faeSArchie Cobbs 769164b576eSGleb Smirnoff /* Update derived info (or go away if no hooks left). */ 770164b576eSGleb Smirnoff if (NG_NODE_NUMHOOKS(node) > 0) 77162838faeSArchie Cobbs ng_ppp_update(node, 0); 772164b576eSGleb Smirnoff else if (NG_NODE_IS_VALID(node)) 773069154d5SJulian Elischer ng_rmnode_self(node); 774164b576eSGleb Smirnoff 7754cf49a43SJulian Elischer return (0); 7764cf49a43SJulian Elischer } 7774cf49a43SJulian Elischer 778164b576eSGleb Smirnoff /* 779164b576eSGleb Smirnoff * Proto layer 780164b576eSGleb Smirnoff */ 7814cf49a43SJulian Elischer 7824cf49a43SJulian Elischer /* 783164b576eSGleb Smirnoff * Receive data on a hook inet. 7844cf49a43SJulian Elischer */ 7854cf49a43SJulian Elischer static int 786164b576eSGleb Smirnoff ng_ppp_rcvdata_inet(hook_p hook, item_p item) 787164b576eSGleb Smirnoff { 788164b576eSGleb Smirnoff const node_p node = NG_HOOK_NODE(hook); 789164b576eSGleb Smirnoff const priv_p priv = NG_NODE_PRIVATE(node); 790164b576eSGleb Smirnoff 791164b576eSGleb Smirnoff if (!priv->conf.enableIP) { 792164b576eSGleb Smirnoff NG_FREE_ITEM(item); 793164b576eSGleb Smirnoff return (ENXIO); 794164b576eSGleb Smirnoff } 795164b576eSGleb Smirnoff return (ng_ppp_hcomp_xmit(NG_HOOK_NODE(hook), item, PROT_IP)); 796164b576eSGleb Smirnoff } 797164b576eSGleb Smirnoff 798164b576eSGleb Smirnoff /* 79935853c2cSGleb Smirnoff * Receive data on a hook inet and pass it directly to first link. 80035853c2cSGleb Smirnoff */ 80135853c2cSGleb Smirnoff static int 80235853c2cSGleb Smirnoff ng_ppp_rcvdata_inet_fast(hook_p hook, item_p item) 80335853c2cSGleb Smirnoff { 80435853c2cSGleb Smirnoff const node_p node = NG_HOOK_NODE(hook); 80535853c2cSGleb Smirnoff const priv_p priv = NG_NODE_PRIVATE(node); 80635853c2cSGleb Smirnoff 80735853c2cSGleb Smirnoff return (ng_ppp_link_xmit(node, item, PROT_IP, priv->activeLinks[0], 80835853c2cSGleb Smirnoff NGI_M(item)->m_pkthdr.len)); 80935853c2cSGleb Smirnoff } 81035853c2cSGleb Smirnoff 81135853c2cSGleb Smirnoff /* 812164b576eSGleb Smirnoff * Receive data on a hook ipv6. 813164b576eSGleb Smirnoff */ 814164b576eSGleb Smirnoff static int 815164b576eSGleb Smirnoff ng_ppp_rcvdata_ipv6(hook_p hook, item_p item) 816164b576eSGleb Smirnoff { 817164b576eSGleb Smirnoff const node_p node = NG_HOOK_NODE(hook); 818164b576eSGleb Smirnoff const priv_p priv = NG_NODE_PRIVATE(node); 819164b576eSGleb Smirnoff 820164b576eSGleb Smirnoff if (!priv->conf.enableIPv6) { 821164b576eSGleb Smirnoff NG_FREE_ITEM(item); 822164b576eSGleb Smirnoff return (ENXIO); 823164b576eSGleb Smirnoff } 824164b576eSGleb Smirnoff return (ng_ppp_hcomp_xmit(NG_HOOK_NODE(hook), item, PROT_IPV6)); 825164b576eSGleb Smirnoff } 826164b576eSGleb Smirnoff 827164b576eSGleb Smirnoff /* 828164b576eSGleb Smirnoff * Receive data on a hook atalk. 829164b576eSGleb Smirnoff */ 830164b576eSGleb Smirnoff static int 831164b576eSGleb Smirnoff ng_ppp_rcvdata_atalk(hook_p hook, item_p item) 832164b576eSGleb Smirnoff { 833164b576eSGleb Smirnoff const node_p node = NG_HOOK_NODE(hook); 834164b576eSGleb Smirnoff const priv_p priv = NG_NODE_PRIVATE(node); 835164b576eSGleb Smirnoff 836164b576eSGleb Smirnoff if (!priv->conf.enableAtalk) { 837164b576eSGleb Smirnoff NG_FREE_ITEM(item); 838164b576eSGleb Smirnoff return (ENXIO); 839164b576eSGleb Smirnoff } 840164b576eSGleb Smirnoff return (ng_ppp_hcomp_xmit(NG_HOOK_NODE(hook), item, PROT_ATALK)); 841164b576eSGleb Smirnoff } 842164b576eSGleb Smirnoff 843164b576eSGleb Smirnoff /* 844164b576eSGleb Smirnoff * Receive data on a hook ipx 845164b576eSGleb Smirnoff */ 846164b576eSGleb Smirnoff static int 847164b576eSGleb Smirnoff ng_ppp_rcvdata_ipx(hook_p hook, item_p item) 848164b576eSGleb Smirnoff { 849164b576eSGleb Smirnoff const node_p node = NG_HOOK_NODE(hook); 850164b576eSGleb Smirnoff const priv_p priv = NG_NODE_PRIVATE(node); 851164b576eSGleb Smirnoff 852164b576eSGleb Smirnoff if (!priv->conf.enableIPX) { 853164b576eSGleb Smirnoff NG_FREE_ITEM(item); 854164b576eSGleb Smirnoff return (ENXIO); 855164b576eSGleb Smirnoff } 856164b576eSGleb Smirnoff return (ng_ppp_hcomp_xmit(NG_HOOK_NODE(hook), item, PROT_IPX)); 857164b576eSGleb Smirnoff } 858164b576eSGleb Smirnoff 859164b576eSGleb Smirnoff /* 860164b576eSGleb Smirnoff * Receive data on a hook bypass 861164b576eSGleb Smirnoff */ 862164b576eSGleb Smirnoff static int 863164b576eSGleb Smirnoff ng_ppp_rcvdata_bypass(hook_p hook, item_p item) 864164b576eSGleb Smirnoff { 865164b576eSGleb Smirnoff uint16_t linkNum; 866164b576eSGleb Smirnoff uint16_t proto; 867164b576eSGleb Smirnoff struct mbuf *m; 868164b576eSGleb Smirnoff 869164b576eSGleb Smirnoff NGI_GET_M(item, m); 870164b576eSGleb Smirnoff if (m->m_pkthdr.len < 4) { 871164b576eSGleb Smirnoff NG_FREE_ITEM(item); 872164b576eSGleb Smirnoff return (EINVAL); 873164b576eSGleb Smirnoff } 874164b576eSGleb Smirnoff if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) { 875164b576eSGleb Smirnoff NG_FREE_ITEM(item); 876164b576eSGleb Smirnoff return (ENOBUFS); 877164b576eSGleb Smirnoff } 87839228864SAlexander Motin linkNum = be16dec(mtod(m, uint8_t *)); 87939228864SAlexander Motin proto = be16dec(mtod(m, uint8_t *) + 2); 880164b576eSGleb Smirnoff m_adj(m, 4); 881164b576eSGleb Smirnoff NGI_M(item) = m; 882164b576eSGleb Smirnoff 883164b576eSGleb Smirnoff if (linkNum == NG_PPP_BUNDLE_LINKNUM) 884164b576eSGleb Smirnoff return (ng_ppp_hcomp_xmit(NG_HOOK_NODE(hook), item, proto)); 885164b576eSGleb Smirnoff else 886164b576eSGleb Smirnoff return (ng_ppp_link_xmit(NG_HOOK_NODE(hook), item, proto, 887e89c1507SAlexander Motin linkNum, 0)); 888164b576eSGleb Smirnoff } 889164b576eSGleb Smirnoff 890164b576eSGleb Smirnoff static int 89139c14742SGleb Smirnoff ng_ppp_bypass(node_p node, item_p item, uint16_t proto, uint16_t linkNum) 89239c14742SGleb Smirnoff { 89339c14742SGleb Smirnoff const priv_p priv = NG_NODE_PRIVATE(node); 89439c14742SGleb Smirnoff uint16_t hdr[2]; 89539c14742SGleb Smirnoff struct mbuf *m; 89639c14742SGleb Smirnoff int error; 89739c14742SGleb Smirnoff 89839c14742SGleb Smirnoff if (priv->hooks[HOOK_INDEX_BYPASS] == NULL) { 89939c14742SGleb Smirnoff NG_FREE_ITEM(item); 90039c14742SGleb Smirnoff return (ENXIO); 90139c14742SGleb Smirnoff } 90239c14742SGleb Smirnoff 90339c14742SGleb Smirnoff /* Add 4-byte bypass header. */ 90439c14742SGleb Smirnoff hdr[0] = htons(linkNum); 90539c14742SGleb Smirnoff hdr[1] = htons(proto); 90639c14742SGleb Smirnoff 90739c14742SGleb Smirnoff NGI_GET_M(item, m); 90839c14742SGleb Smirnoff if ((m = ng_ppp_prepend(m, &hdr, 4)) == NULL) { 90939c14742SGleb Smirnoff NG_FREE_ITEM(item); 91039c14742SGleb Smirnoff return (ENOBUFS); 91139c14742SGleb Smirnoff } 91239c14742SGleb Smirnoff NGI_M(item) = m; 91339c14742SGleb Smirnoff 91439c14742SGleb Smirnoff /* Send packet out hook. */ 91539c14742SGleb Smirnoff NG_FWD_ITEM_HOOK(error, item, priv->hooks[HOOK_INDEX_BYPASS]); 91639c14742SGleb Smirnoff return (error); 91739c14742SGleb Smirnoff } 91839c14742SGleb Smirnoff 91939c14742SGleb Smirnoff static int 920164b576eSGleb Smirnoff ng_ppp_proto_recv(node_p node, item_p item, uint16_t proto, uint16_t linkNum) 9214cf49a43SJulian Elischer { 92230400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 9233949bee8SArchie Cobbs hook_p outHook = NULL; 924164b576eSGleb Smirnoff int error; 925d6b013b5SAlexander Motin #ifdef ALIGNED_POINTER 926d6b013b5SAlexander Motin struct mbuf *m, *n; 9274cf49a43SJulian Elischer 928d6b013b5SAlexander Motin NGI_GET_M(item, m); 929d6b013b5SAlexander Motin if (!ALIGNED_POINTER(mtod(m, caddr_t), uint32_t)) { 930d6b013b5SAlexander Motin n = m_defrag(m, M_NOWAIT); 931d6b013b5SAlexander Motin if (n == NULL) { 932d6b013b5SAlexander Motin m_freem(m); 933d6b013b5SAlexander Motin NG_FREE_ITEM(item); 934d6b013b5SAlexander Motin return (ENOBUFS); 935d6b013b5SAlexander Motin } 936d6b013b5SAlexander Motin m = n; 937d6b013b5SAlexander Motin } 938d6b013b5SAlexander Motin NGI_M(item) = m; 939d6b013b5SAlexander Motin #endif /* ALIGNED_POINTER */ 940ccb07cc3SGleb Smirnoff switch (proto) { 9413949bee8SArchie Cobbs case PROT_IP: 9423949bee8SArchie Cobbs if (priv->conf.enableIP) 9433949bee8SArchie Cobbs outHook = priv->hooks[HOOK_INDEX_INET]; 9443949bee8SArchie Cobbs break; 945a9b3dca5SArchie Cobbs case PROT_IPV6: 946a9b3dca5SArchie Cobbs if (priv->conf.enableIPv6) 947a9b3dca5SArchie Cobbs outHook = priv->hooks[HOOK_INDEX_IPV6]; 948a9b3dca5SArchie Cobbs break; 949164b576eSGleb Smirnoff case PROT_ATALK: 950164b576eSGleb Smirnoff if (priv->conf.enableAtalk) 951164b576eSGleb Smirnoff outHook = priv->hooks[HOOK_INDEX_ATALK]; 952164b576eSGleb Smirnoff break; 953164b576eSGleb Smirnoff case PROT_IPX: 954164b576eSGleb Smirnoff if (priv->conf.enableIPX) 955164b576eSGleb Smirnoff outHook = priv->hooks[HOOK_INDEX_IPX]; 956164b576eSGleb Smirnoff break; 957ccb07cc3SGleb Smirnoff } 9583949bee8SArchie Cobbs 95939c14742SGleb Smirnoff if (outHook == NULL) 96039c14742SGleb Smirnoff return (ng_ppp_bypass(node, item, proto, linkNum)); 961d690a6e7SArchie Cobbs 962164b576eSGleb Smirnoff /* Send packet out hook. */ 963164b576eSGleb Smirnoff NG_FWD_ITEM_HOOK(error, item, outHook); 9643949bee8SArchie Cobbs return (error); 9654cf49a43SJulian Elischer } 9664cf49a43SJulian Elischer 9674cf49a43SJulian Elischer /* 968164b576eSGleb Smirnoff * Header compression layer 969164b576eSGleb Smirnoff */ 970164b576eSGleb Smirnoff 971164b576eSGleb Smirnoff static int 972164b576eSGleb Smirnoff ng_ppp_hcomp_xmit(node_p node, item_p item, uint16_t proto) 973164b576eSGleb Smirnoff { 974164b576eSGleb Smirnoff const priv_p priv = NG_NODE_PRIVATE(node); 975164b576eSGleb Smirnoff 976164b576eSGleb Smirnoff if (proto == PROT_IP && 977164b576eSGleb Smirnoff priv->conf.enableVJCompression && 978164b576eSGleb Smirnoff priv->vjCompHooked) { 979164b576eSGleb Smirnoff int error; 980164b576eSGleb Smirnoff 981164b576eSGleb Smirnoff /* Send packet out hook. */ 982164b576eSGleb Smirnoff NG_FWD_ITEM_HOOK(error, item, priv->hooks[HOOK_INDEX_VJC_IP]); 983164b576eSGleb Smirnoff return (error); 984164b576eSGleb Smirnoff } 985164b576eSGleb Smirnoff 986164b576eSGleb Smirnoff return (ng_ppp_comp_xmit(node, item, proto)); 987164b576eSGleb Smirnoff } 988164b576eSGleb Smirnoff 989164b576eSGleb Smirnoff /* 990164b576eSGleb Smirnoff * Receive data on a hook vjc_comp. 9914cf49a43SJulian Elischer */ 9923949bee8SArchie Cobbs static int 993164b576eSGleb Smirnoff ng_ppp_rcvdata_vjc_comp(hook_p hook, item_p item) 994164b576eSGleb Smirnoff { 995164b576eSGleb Smirnoff const node_p node = NG_HOOK_NODE(hook); 996164b576eSGleb Smirnoff const priv_p priv = NG_NODE_PRIVATE(node); 997164b576eSGleb Smirnoff 998164b576eSGleb Smirnoff if (!priv->conf.enableVJCompression) { 999164b576eSGleb Smirnoff NG_FREE_ITEM(item); 1000164b576eSGleb Smirnoff return (ENXIO); 1001164b576eSGleb Smirnoff } 1002164b576eSGleb Smirnoff return (ng_ppp_comp_xmit(node, item, PROT_VJCOMP)); 1003164b576eSGleb Smirnoff } 1004164b576eSGleb Smirnoff 1005164b576eSGleb Smirnoff /* 1006164b576eSGleb Smirnoff * Receive data on a hook vjc_uncomp. 1007164b576eSGleb Smirnoff */ 1008164b576eSGleb Smirnoff static int 1009164b576eSGleb Smirnoff ng_ppp_rcvdata_vjc_uncomp(hook_p hook, item_p item) 1010164b576eSGleb Smirnoff { 1011164b576eSGleb Smirnoff const node_p node = NG_HOOK_NODE(hook); 1012164b576eSGleb Smirnoff const priv_p priv = NG_NODE_PRIVATE(node); 1013164b576eSGleb Smirnoff 1014164b576eSGleb Smirnoff if (!priv->conf.enableVJCompression) { 1015164b576eSGleb Smirnoff NG_FREE_ITEM(item); 1016164b576eSGleb Smirnoff return (ENXIO); 1017164b576eSGleb Smirnoff } 1018164b576eSGleb Smirnoff return (ng_ppp_comp_xmit(node, item, PROT_VJUNCOMP)); 1019164b576eSGleb Smirnoff } 1020164b576eSGleb Smirnoff 1021164b576eSGleb Smirnoff /* 1022164b576eSGleb Smirnoff * Receive data on a hook vjc_vjip. 1023164b576eSGleb Smirnoff */ 1024164b576eSGleb Smirnoff static int 1025164b576eSGleb Smirnoff ng_ppp_rcvdata_vjc_vjip(hook_p hook, item_p item) 1026164b576eSGleb Smirnoff { 1027164b576eSGleb Smirnoff const node_p node = NG_HOOK_NODE(hook); 1028164b576eSGleb Smirnoff const priv_p priv = NG_NODE_PRIVATE(node); 1029164b576eSGleb Smirnoff 1030164b576eSGleb Smirnoff if (!priv->conf.enableVJCompression) { 1031164b576eSGleb Smirnoff NG_FREE_ITEM(item); 1032164b576eSGleb Smirnoff return (ENXIO); 1033164b576eSGleb Smirnoff } 1034164b576eSGleb Smirnoff return (ng_ppp_comp_xmit(node, item, PROT_IP)); 1035164b576eSGleb Smirnoff } 1036164b576eSGleb Smirnoff 1037164b576eSGleb Smirnoff static int 1038164b576eSGleb Smirnoff ng_ppp_hcomp_recv(node_p node, item_p item, uint16_t proto, uint16_t linkNum) 1039164b576eSGleb Smirnoff { 1040164b576eSGleb Smirnoff const priv_p priv = NG_NODE_PRIVATE(node); 1041164b576eSGleb Smirnoff 1042164b576eSGleb Smirnoff if (priv->conf.enableVJDecompression && priv->vjCompHooked) { 1043164b576eSGleb Smirnoff hook_p outHook = NULL; 1044164b576eSGleb Smirnoff 1045164b576eSGleb Smirnoff switch (proto) { 1046164b576eSGleb Smirnoff case PROT_VJCOMP: 1047164b576eSGleb Smirnoff outHook = priv->hooks[HOOK_INDEX_VJC_COMP]; 1048164b576eSGleb Smirnoff break; 1049164b576eSGleb Smirnoff case PROT_VJUNCOMP: 1050164b576eSGleb Smirnoff outHook = priv->hooks[HOOK_INDEX_VJC_UNCOMP]; 1051164b576eSGleb Smirnoff break; 1052164b576eSGleb Smirnoff } 1053164b576eSGleb Smirnoff 1054164b576eSGleb Smirnoff if (outHook) { 105539c14742SGleb Smirnoff int error; 105639c14742SGleb Smirnoff 1057164b576eSGleb Smirnoff /* Send packet out hook. */ 1058164b576eSGleb Smirnoff NG_FWD_ITEM_HOOK(error, item, outHook); 1059164b576eSGleb Smirnoff return (error); 1060164b576eSGleb Smirnoff } 1061164b576eSGleb Smirnoff } 1062164b576eSGleb Smirnoff 1063164b576eSGleb Smirnoff return (ng_ppp_proto_recv(node, item, proto, linkNum)); 1064164b576eSGleb Smirnoff } 1065164b576eSGleb Smirnoff 1066164b576eSGleb Smirnoff /* 1067164b576eSGleb Smirnoff * Receive data on a hook vjc_ip. 1068164b576eSGleb Smirnoff */ 1069164b576eSGleb Smirnoff static int 1070164b576eSGleb Smirnoff ng_ppp_rcvdata_vjc_ip(hook_p hook, item_p item) 1071164b576eSGleb Smirnoff { 1072164b576eSGleb Smirnoff const node_p node = NG_HOOK_NODE(hook); 1073164b576eSGleb Smirnoff const priv_p priv = NG_NODE_PRIVATE(node); 1074164b576eSGleb Smirnoff 10756d67c50bSAlexander Motin if (!priv->conf.enableVJDecompression) { 1076164b576eSGleb Smirnoff NG_FREE_ITEM(item); 1077164b576eSGleb Smirnoff return (ENXIO); 1078164b576eSGleb Smirnoff } 1079164b576eSGleb Smirnoff return (ng_ppp_proto_recv(node, item, PROT_IP, NG_PPP_BUNDLE_LINKNUM)); 1080164b576eSGleb Smirnoff } 1081164b576eSGleb Smirnoff 1082164b576eSGleb Smirnoff /* 1083164b576eSGleb Smirnoff * Compression layer 1084164b576eSGleb Smirnoff */ 1085164b576eSGleb Smirnoff 1086164b576eSGleb Smirnoff static int 1087164b576eSGleb Smirnoff ng_ppp_comp_xmit(node_p node, item_p item, uint16_t proto) 1088164b576eSGleb Smirnoff { 1089164b576eSGleb Smirnoff const priv_p priv = NG_NODE_PRIVATE(node); 1090164b576eSGleb Smirnoff 1091164b576eSGleb Smirnoff if (priv->conf.enableCompression && 1092164b576eSGleb Smirnoff proto < 0x4000 && 1093164b576eSGleb Smirnoff proto != PROT_COMPD && 1094164b576eSGleb Smirnoff proto != PROT_CRYPTD && 1095164b576eSGleb Smirnoff priv->hooks[HOOK_INDEX_COMPRESS] != NULL) { 1096164b576eSGleb Smirnoff struct mbuf *m; 1097164b576eSGleb Smirnoff int error; 1098164b576eSGleb Smirnoff 1099164b576eSGleb Smirnoff NGI_GET_M(item, m); 1100164b576eSGleb Smirnoff if ((m = ng_ppp_addproto(m, proto, 0)) == NULL) { 1101164b576eSGleb Smirnoff NG_FREE_ITEM(item); 1102164b576eSGleb Smirnoff return (ENOBUFS); 1103164b576eSGleb Smirnoff } 1104164b576eSGleb Smirnoff NGI_M(item) = m; 1105164b576eSGleb Smirnoff 1106164b576eSGleb Smirnoff /* Send packet out hook. */ 1107164b576eSGleb Smirnoff NG_FWD_ITEM_HOOK(error, item, priv->hooks[HOOK_INDEX_COMPRESS]); 1108164b576eSGleb Smirnoff return (error); 1109164b576eSGleb Smirnoff } 1110164b576eSGleb Smirnoff 1111164b576eSGleb Smirnoff return (ng_ppp_crypt_xmit(node, item, proto)); 1112164b576eSGleb Smirnoff } 1113164b576eSGleb Smirnoff 1114164b576eSGleb Smirnoff /* 1115164b576eSGleb Smirnoff * Receive data on a hook compress. 1116164b576eSGleb Smirnoff */ 1117164b576eSGleb Smirnoff static int 1118164b576eSGleb Smirnoff ng_ppp_rcvdata_compress(hook_p hook, item_p item) 1119164b576eSGleb Smirnoff { 1120164b576eSGleb Smirnoff const node_p node = NG_HOOK_NODE(hook); 1121164b576eSGleb Smirnoff const priv_p priv = NG_NODE_PRIVATE(node); 1122164b576eSGleb Smirnoff uint16_t proto; 1123164b576eSGleb Smirnoff 1124164b576eSGleb Smirnoff switch (priv->conf.enableCompression) { 1125164b576eSGleb Smirnoff case NG_PPP_COMPRESS_NONE: 1126164b576eSGleb Smirnoff NG_FREE_ITEM(item); 1127164b576eSGleb Smirnoff return (ENXIO); 1128164b576eSGleb Smirnoff case NG_PPP_COMPRESS_FULL: 1129164b576eSGleb Smirnoff { 1130164b576eSGleb Smirnoff struct mbuf *m; 1131164b576eSGleb Smirnoff 1132164b576eSGleb Smirnoff NGI_GET_M(item, m); 1133164b576eSGleb Smirnoff if ((m = ng_ppp_cutproto(m, &proto)) == NULL) { 1134164b576eSGleb Smirnoff NG_FREE_ITEM(item); 1135164b576eSGleb Smirnoff return (EIO); 1136164b576eSGleb Smirnoff } 1137164b576eSGleb Smirnoff NGI_M(item) = m; 1138164b576eSGleb Smirnoff if (!PROT_VALID(proto)) { 1139164b576eSGleb Smirnoff NG_FREE_ITEM(item); 1140164b576eSGleb Smirnoff return (EIO); 1141164b576eSGleb Smirnoff } 1142164b576eSGleb Smirnoff } 1143164b576eSGleb Smirnoff break; 1144164b576eSGleb Smirnoff default: 1145164b576eSGleb Smirnoff proto = PROT_COMPD; 1146164b576eSGleb Smirnoff break; 1147164b576eSGleb Smirnoff } 1148164b576eSGleb Smirnoff return (ng_ppp_crypt_xmit(node, item, proto)); 1149164b576eSGleb Smirnoff } 1150164b576eSGleb Smirnoff 1151164b576eSGleb Smirnoff static int 1152164b576eSGleb Smirnoff ng_ppp_comp_recv(node_p node, item_p item, uint16_t proto, uint16_t linkNum) 1153164b576eSGleb Smirnoff { 1154164b576eSGleb Smirnoff const priv_p priv = NG_NODE_PRIVATE(node); 1155164b576eSGleb Smirnoff 1156164b576eSGleb Smirnoff if (proto < 0x4000 && 1157164b576eSGleb Smirnoff ((proto == PROT_COMPD && priv->conf.enableDecompression) || 1158164b576eSGleb Smirnoff priv->conf.enableDecompression == NG_PPP_DECOMPRESS_FULL) && 1159164b576eSGleb Smirnoff priv->hooks[HOOK_INDEX_DECOMPRESS] != NULL) { 1160164b576eSGleb Smirnoff int error; 1161164b576eSGleb Smirnoff 1162164b576eSGleb Smirnoff if (priv->conf.enableDecompression == NG_PPP_DECOMPRESS_FULL) { 1163164b576eSGleb Smirnoff struct mbuf *m; 1164164b576eSGleb Smirnoff NGI_GET_M(item, m); 1165164b576eSGleb Smirnoff if ((m = ng_ppp_addproto(m, proto, 0)) == NULL) { 1166164b576eSGleb Smirnoff NG_FREE_ITEM(item); 1167164b576eSGleb Smirnoff return (EIO); 1168164b576eSGleb Smirnoff } 1169164b576eSGleb Smirnoff NGI_M(item) = m; 1170164b576eSGleb Smirnoff } 1171164b576eSGleb Smirnoff 1172164b576eSGleb Smirnoff /* Send packet out hook. */ 1173164b576eSGleb Smirnoff NG_FWD_ITEM_HOOK(error, item, 1174164b576eSGleb Smirnoff priv->hooks[HOOK_INDEX_DECOMPRESS]); 1175164b576eSGleb Smirnoff return (error); 117639c14742SGleb Smirnoff } else if (proto == PROT_COMPD) { 117739c14742SGleb Smirnoff /* Disabled protos MUST be silently discarded, but 117839c14742SGleb Smirnoff * unsupported MUST not. Let user-level decide this. */ 117939c14742SGleb Smirnoff return (ng_ppp_bypass(node, item, proto, linkNum)); 1180164b576eSGleb Smirnoff } 1181164b576eSGleb Smirnoff 1182164b576eSGleb Smirnoff return (ng_ppp_hcomp_recv(node, item, proto, linkNum)); 1183164b576eSGleb Smirnoff } 1184164b576eSGleb Smirnoff 1185164b576eSGleb Smirnoff /* 1186164b576eSGleb Smirnoff * Receive data on a hook decompress. 1187164b576eSGleb Smirnoff */ 1188164b576eSGleb Smirnoff static int 1189164b576eSGleb Smirnoff ng_ppp_rcvdata_decompress(hook_p hook, item_p item) 1190164b576eSGleb Smirnoff { 1191164b576eSGleb Smirnoff const node_p node = NG_HOOK_NODE(hook); 1192164b576eSGleb Smirnoff const priv_p priv = NG_NODE_PRIVATE(node); 1193164b576eSGleb Smirnoff uint16_t proto; 1194164b576eSGleb Smirnoff struct mbuf *m; 1195164b576eSGleb Smirnoff 1196164b576eSGleb Smirnoff if (!priv->conf.enableDecompression) { 1197164b576eSGleb Smirnoff NG_FREE_ITEM(item); 1198164b576eSGleb Smirnoff return (ENXIO); 1199164b576eSGleb Smirnoff } 1200164b576eSGleb Smirnoff NGI_GET_M(item, m); 1201164b576eSGleb Smirnoff if ((m = ng_ppp_cutproto(m, &proto)) == NULL) { 1202164b576eSGleb Smirnoff NG_FREE_ITEM(item); 1203164b576eSGleb Smirnoff return (EIO); 1204164b576eSGleb Smirnoff } 1205164b576eSGleb Smirnoff NGI_M(item) = m; 1206164b576eSGleb Smirnoff if (!PROT_VALID(proto)) { 1207164b576eSGleb Smirnoff priv->bundleStats.badProtos++; 1208164b576eSGleb Smirnoff NG_FREE_ITEM(item); 1209164b576eSGleb Smirnoff return (EIO); 1210164b576eSGleb Smirnoff } 1211164b576eSGleb Smirnoff return (ng_ppp_hcomp_recv(node, item, proto, NG_PPP_BUNDLE_LINKNUM)); 1212164b576eSGleb Smirnoff } 1213164b576eSGleb Smirnoff 1214164b576eSGleb Smirnoff /* 1215164b576eSGleb Smirnoff * Encryption layer 1216164b576eSGleb Smirnoff */ 1217164b576eSGleb Smirnoff 1218164b576eSGleb Smirnoff static int 1219164b576eSGleb Smirnoff ng_ppp_crypt_xmit(node_p node, item_p item, uint16_t proto) 1220164b576eSGleb Smirnoff { 1221164b576eSGleb Smirnoff const priv_p priv = NG_NODE_PRIVATE(node); 1222164b576eSGleb Smirnoff 1223164b576eSGleb Smirnoff if (priv->conf.enableEncryption && 1224164b576eSGleb Smirnoff proto < 0x4000 && 1225164b576eSGleb Smirnoff proto != PROT_CRYPTD && 1226164b576eSGleb Smirnoff priv->hooks[HOOK_INDEX_ENCRYPT] != NULL) { 1227164b576eSGleb Smirnoff struct mbuf *m; 1228164b576eSGleb Smirnoff int error; 1229164b576eSGleb Smirnoff 1230164b576eSGleb Smirnoff NGI_GET_M(item, m); 1231164b576eSGleb Smirnoff if ((m = ng_ppp_addproto(m, proto, 0)) == NULL) { 1232164b576eSGleb Smirnoff NG_FREE_ITEM(item); 1233164b576eSGleb Smirnoff return (ENOBUFS); 1234164b576eSGleb Smirnoff } 1235164b576eSGleb Smirnoff NGI_M(item) = m; 1236164b576eSGleb Smirnoff 1237164b576eSGleb Smirnoff /* Send packet out hook. */ 1238164b576eSGleb Smirnoff NG_FWD_ITEM_HOOK(error, item, priv->hooks[HOOK_INDEX_ENCRYPT]); 1239164b576eSGleb Smirnoff return (error); 1240164b576eSGleb Smirnoff } 1241164b576eSGleb Smirnoff 1242164b576eSGleb Smirnoff return (ng_ppp_mp_xmit(node, item, proto)); 1243164b576eSGleb Smirnoff } 1244164b576eSGleb Smirnoff 1245164b576eSGleb Smirnoff /* 1246164b576eSGleb Smirnoff * Receive data on a hook encrypt. 1247164b576eSGleb Smirnoff */ 1248164b576eSGleb Smirnoff static int 1249164b576eSGleb Smirnoff ng_ppp_rcvdata_encrypt(hook_p hook, item_p item) 1250164b576eSGleb Smirnoff { 1251164b576eSGleb Smirnoff const node_p node = NG_HOOK_NODE(hook); 1252164b576eSGleb Smirnoff const priv_p priv = NG_NODE_PRIVATE(node); 1253164b576eSGleb Smirnoff 1254164b576eSGleb Smirnoff if (!priv->conf.enableEncryption) { 1255164b576eSGleb Smirnoff NG_FREE_ITEM(item); 1256164b576eSGleb Smirnoff return (ENXIO); 1257164b576eSGleb Smirnoff } 1258164b576eSGleb Smirnoff return (ng_ppp_mp_xmit(node, item, PROT_CRYPTD)); 1259164b576eSGleb Smirnoff } 1260164b576eSGleb Smirnoff 1261164b576eSGleb Smirnoff static int 1262164b576eSGleb Smirnoff ng_ppp_crypt_recv(node_p node, item_p item, uint16_t proto, uint16_t linkNum) 1263164b576eSGleb Smirnoff { 1264164b576eSGleb Smirnoff const priv_p priv = NG_NODE_PRIVATE(node); 1265164b576eSGleb Smirnoff 126639c14742SGleb Smirnoff if (proto == PROT_CRYPTD) { 126739c14742SGleb Smirnoff if (priv->conf.enableDecryption && 1268164b576eSGleb Smirnoff priv->hooks[HOOK_INDEX_DECRYPT] != NULL) { 1269164b576eSGleb Smirnoff int error; 1270164b576eSGleb Smirnoff 1271164b576eSGleb Smirnoff /* Send packet out hook. */ 127239c14742SGleb Smirnoff NG_FWD_ITEM_HOOK(error, item, 127339c14742SGleb Smirnoff priv->hooks[HOOK_INDEX_DECRYPT]); 1274164b576eSGleb Smirnoff return (error); 127539c14742SGleb Smirnoff } else { 127639c14742SGleb Smirnoff /* Disabled protos MUST be silently discarded, but 127739c14742SGleb Smirnoff * unsupported MUST not. Let user-level decide this. */ 127839c14742SGleb Smirnoff return (ng_ppp_bypass(node, item, proto, linkNum)); 127939c14742SGleb Smirnoff } 1280164b576eSGleb Smirnoff } 1281164b576eSGleb Smirnoff 1282164b576eSGleb Smirnoff return (ng_ppp_comp_recv(node, item, proto, linkNum)); 1283164b576eSGleb Smirnoff } 1284164b576eSGleb Smirnoff 1285164b576eSGleb Smirnoff /* 1286164b576eSGleb Smirnoff * Receive data on a hook decrypt. 1287164b576eSGleb Smirnoff */ 1288164b576eSGleb Smirnoff static int 1289164b576eSGleb Smirnoff ng_ppp_rcvdata_decrypt(hook_p hook, item_p item) 1290164b576eSGleb Smirnoff { 1291164b576eSGleb Smirnoff const node_p node = NG_HOOK_NODE(hook); 1292164b576eSGleb Smirnoff const priv_p priv = NG_NODE_PRIVATE(node); 1293164b576eSGleb Smirnoff uint16_t proto; 1294164b576eSGleb Smirnoff struct mbuf *m; 1295164b576eSGleb Smirnoff 1296164b576eSGleb Smirnoff if (!priv->conf.enableDecryption) { 1297164b576eSGleb Smirnoff NG_FREE_ITEM(item); 1298164b576eSGleb Smirnoff return (ENXIO); 1299164b576eSGleb Smirnoff } 1300164b576eSGleb Smirnoff NGI_GET_M(item, m); 1301164b576eSGleb Smirnoff if ((m = ng_ppp_cutproto(m, &proto)) == NULL) { 1302164b576eSGleb Smirnoff NG_FREE_ITEM(item); 1303164b576eSGleb Smirnoff return (EIO); 1304164b576eSGleb Smirnoff } 1305164b576eSGleb Smirnoff NGI_M(item) = m; 1306164b576eSGleb Smirnoff if (!PROT_VALID(proto)) { 1307164b576eSGleb Smirnoff priv->bundleStats.badProtos++; 1308164b576eSGleb Smirnoff NG_FREE_ITEM(item); 1309164b576eSGleb Smirnoff return (EIO); 1310164b576eSGleb Smirnoff } 1311164b576eSGleb Smirnoff return (ng_ppp_comp_recv(node, item, proto, NG_PPP_BUNDLE_LINKNUM)); 1312164b576eSGleb Smirnoff } 1313164b576eSGleb Smirnoff 1314164b576eSGleb Smirnoff /* 1315164b576eSGleb Smirnoff * Link layer 1316164b576eSGleb Smirnoff */ 1317164b576eSGleb Smirnoff 1318164b576eSGleb Smirnoff static int 1319e89c1507SAlexander Motin ng_ppp_link_xmit(node_p node, item_p item, uint16_t proto, uint16_t linkNum, int plen) 13204cf49a43SJulian Elischer { 132130400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 1322a9b3dca5SArchie Cobbs struct ng_ppp_link *link; 1323e149c4e2SArchie Cobbs int len, error; 1324069154d5SJulian Elischer struct mbuf *m; 1325164b576eSGleb Smirnoff uint16_t mru; 13264cf49a43SJulian Elischer 1327164b576eSGleb Smirnoff /* Check if link correct. */ 1328164b576eSGleb Smirnoff if (linkNum >= NG_PPP_MAX_LINKS) { 1329e89c1507SAlexander Motin ERROUT(ENETDOWN); 13303949bee8SArchie Cobbs } 1331164b576eSGleb Smirnoff 1332164b576eSGleb Smirnoff /* Get link pointer (optimization). */ 1333164b576eSGleb Smirnoff link = &priv->links[linkNum]; 1334164b576eSGleb Smirnoff 1335164b576eSGleb Smirnoff /* Check link status (if real). */ 1336164b576eSGleb Smirnoff if (link->hook == NULL) { 1337e89c1507SAlexander Motin ERROUT(ENETDOWN); 13380e11d0f3SArchie Cobbs } 13393949bee8SArchie Cobbs 1340164b576eSGleb Smirnoff /* Extract mbuf. */ 1341164b576eSGleb Smirnoff NGI_GET_M(item, m); 1342164b576eSGleb Smirnoff 1343164b576eSGleb Smirnoff /* Check peer's MRU for this link. */ 1344164b576eSGleb Smirnoff mru = link->conf.mru; 1345931c828aSArchie Cobbs if (mru != 0 && m->m_pkthdr.len > mru) { 1346931c828aSArchie Cobbs NG_FREE_M(m); 1347e89c1507SAlexander Motin ERROUT(EMSGSIZE); 1348931c828aSArchie Cobbs } 1349931c828aSArchie Cobbs 1350164b576eSGleb Smirnoff /* Prepend protocol number, possibly compressed. */ 1351164b576eSGleb Smirnoff if ((m = ng_ppp_addproto(m, proto, link->conf.enableProtoComp)) == 1352164b576eSGleb Smirnoff NULL) { 1353e89c1507SAlexander Motin ERROUT(ENOBUFS); 1354d690a6e7SArchie Cobbs } 1355d690a6e7SArchie Cobbs 1356164b576eSGleb Smirnoff /* Prepend address and control field (unless compressed). */ 1357a9b3dca5SArchie Cobbs if (proto == PROT_LCP || !link->conf.enableACFComp) { 1358e89c1507SAlexander Motin if ((m = ng_ppp_prepend(m, &ng_ppp_acf, 2)) == NULL) 1359e89c1507SAlexander Motin ERROUT(ENOBUFS); 1360d690a6e7SArchie Cobbs } 1361d690a6e7SArchie Cobbs 1362164b576eSGleb Smirnoff /* Deliver frame. */ 1363e149c4e2SArchie Cobbs len = m->m_pkthdr.len; 1364069154d5SJulian Elischer NG_FWD_NEW_DATA(error, item, link->hook, m); 1365fb1fc8abSArchie Cobbs 1366e89c1507SAlexander Motin mtx_lock(&priv->xmtx); 1367e89c1507SAlexander Motin 1368e89c1507SAlexander Motin /* Update link stats. */ 1369a9b3dca5SArchie Cobbs link->stats.xmitFrames++; 1370a9b3dca5SArchie Cobbs link->stats.xmitOctets += len; 1371164b576eSGleb Smirnoff 1372e89c1507SAlexander Motin /* Update bundle stats. */ 1373e89c1507SAlexander Motin if (plen > 0) { 1374e89c1507SAlexander Motin priv->bundleStats.xmitFrames++; 1375e89c1507SAlexander Motin priv->bundleStats.xmitOctets += plen; 1376e89c1507SAlexander Motin } 1377e89c1507SAlexander Motin 1378e89c1507SAlexander Motin /* Update 'bytes in queue' counter. */ 1379e89c1507SAlexander Motin if (error == 0) { 1380164b576eSGleb Smirnoff /* bytesInQueue and lastWrite required only for mp_strategy. */ 1381c35e19c4SAlexander Motin if (priv->conf.enableMultilink && !priv->allLinksEqual && 1382c35e19c4SAlexander Motin !priv->conf.enableRoundRobin) { 1383fd58342cSAlexander Motin /* If queue was empty, then mark this time. */ 1384fd58342cSAlexander Motin if (link->bytesInQueue == 0) 1385a9b3dca5SArchie Cobbs getmicrouptime(&link->lastWrite); 1386fd58342cSAlexander Motin link->bytesInQueue += len + MP_AVERAGE_LINK_OVERHEAD; 1387fd58342cSAlexander Motin /* Limit max queue length to 50 pkts. BW can be defined 1388fd58342cSAlexander Motin incorrectly and link may not signal overload. */ 1389fd58342cSAlexander Motin if (link->bytesInQueue > 50 * 1600) 1390fd58342cSAlexander Motin link->bytesInQueue = 50 * 1600; 1391fb1fc8abSArchie Cobbs } 13923949bee8SArchie Cobbs } 1393e89c1507SAlexander Motin mtx_unlock(&priv->xmtx); 1394e89c1507SAlexander Motin return (error); 1395e89c1507SAlexander Motin 1396e89c1507SAlexander Motin done: 1397e89c1507SAlexander Motin NG_FREE_ITEM(item); 1398164b576eSGleb Smirnoff return (error); 1399164b576eSGleb Smirnoff } 1400164b576eSGleb Smirnoff 1401164b576eSGleb Smirnoff /* 1402164b576eSGleb Smirnoff * Receive data on a hook linkX. 1403164b576eSGleb Smirnoff */ 1404164b576eSGleb Smirnoff static int 1405164b576eSGleb Smirnoff ng_ppp_rcvdata(hook_p hook, item_p item) 1406164b576eSGleb Smirnoff { 1407164b576eSGleb Smirnoff const node_p node = NG_HOOK_NODE(hook); 1408164b576eSGleb Smirnoff const priv_p priv = NG_NODE_PRIVATE(node); 1409164b576eSGleb Smirnoff const int index = (intptr_t)NG_HOOK_PRIVATE(hook); 1410164b576eSGleb Smirnoff const uint16_t linkNum = (uint16_t)~index; 1411164b576eSGleb Smirnoff struct ng_ppp_link * const link = &priv->links[linkNum]; 1412164b576eSGleb Smirnoff uint16_t proto; 1413164b576eSGleb Smirnoff struct mbuf *m; 1414e89c1507SAlexander Motin int error = 0; 1415164b576eSGleb Smirnoff 141633eb7cb0SMatt Jacob KASSERT(linkNum < NG_PPP_MAX_LINKS, 1417164b576eSGleb Smirnoff ("%s: bogus index 0x%x", __func__, index)); 1418164b576eSGleb Smirnoff 1419164b576eSGleb Smirnoff NGI_GET_M(item, m); 1420164b576eSGleb Smirnoff 1421e89c1507SAlexander Motin mtx_lock(&priv->rmtx); 1422e89c1507SAlexander Motin 1423164b576eSGleb Smirnoff /* Stats */ 1424164b576eSGleb Smirnoff link->stats.recvFrames++; 1425164b576eSGleb Smirnoff link->stats.recvOctets += m->m_pkthdr.len; 1426164b576eSGleb Smirnoff 1427164b576eSGleb Smirnoff /* Strip address and control fields, if present. */ 1428e89c1507SAlexander Motin if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) 1429e89c1507SAlexander Motin ERROUT(ENOBUFS); 1430b4d0be22SAlexander Motin if (mtod(m, uint8_t *)[0] == 0xff && 1431b4d0be22SAlexander Motin mtod(m, uint8_t *)[1] == 0x03) 1432164b576eSGleb Smirnoff m_adj(m, 2); 1433164b576eSGleb Smirnoff 1434e89c1507SAlexander Motin /* Get protocol number */ 1435e89c1507SAlexander Motin if ((m = ng_ppp_cutproto(m, &proto)) == NULL) 1436e89c1507SAlexander Motin ERROUT(ENOBUFS); 1437164b576eSGleb Smirnoff NGI_M(item) = m; /* Put changed m back into item. */ 1438164b576eSGleb Smirnoff 1439164b576eSGleb Smirnoff if (!PROT_VALID(proto)) { 1440164b576eSGleb Smirnoff link->stats.badProtos++; 1441e89c1507SAlexander Motin ERROUT(EIO); 1442164b576eSGleb Smirnoff } 1443164b576eSGleb Smirnoff 144439c14742SGleb Smirnoff /* LCP packets must go directly to bypass. */ 1445e89c1507SAlexander Motin if (proto >= 0xB000) { 1446e89c1507SAlexander Motin mtx_unlock(&priv->rmtx); 144739c14742SGleb Smirnoff return (ng_ppp_bypass(node, item, proto, linkNum)); 144839c14742SGleb Smirnoff } 144939c14742SGleb Smirnoff 1450e89c1507SAlexander Motin /* Other packets are denied on a disabled link. */ 1451e89c1507SAlexander Motin if (!link->conf.enableLink) 1452e89c1507SAlexander Motin ERROUT(ENXIO); 1453e89c1507SAlexander Motin 1454e89c1507SAlexander Motin /* Proceed to multilink layer. Mutex will be unlocked inside. */ 1455e89c1507SAlexander Motin error = ng_ppp_mp_recv(node, item, proto, linkNum); 1456e89c1507SAlexander Motin mtx_assert(&priv->rmtx, MA_NOTOWNED); 1457e89c1507SAlexander Motin return (error); 1458e89c1507SAlexander Motin 1459e89c1507SAlexander Motin done: 1460e89c1507SAlexander Motin mtx_unlock(&priv->rmtx); 1461e89c1507SAlexander Motin NG_FREE_ITEM(item); 1462e89c1507SAlexander Motin return (error); 1463164b576eSGleb Smirnoff } 1464164b576eSGleb Smirnoff 1465164b576eSGleb Smirnoff /* 1466164b576eSGleb Smirnoff * Multilink layer 1467164b576eSGleb Smirnoff */ 14683949bee8SArchie Cobbs 14693949bee8SArchie Cobbs /* 14703949bee8SArchie Cobbs * Handle an incoming multi-link fragment 1471a9b3dca5SArchie Cobbs * 1472a9b3dca5SArchie Cobbs * The fragment reassembly algorithm is somewhat complex. This is mainly 1473a9b3dca5SArchie Cobbs * because we are required not to reorder the reconstructed packets, yet 1474a9b3dca5SArchie Cobbs * fragments are only guaranteed to arrive in order on a per-link basis. 1475a9b3dca5SArchie Cobbs * In other words, when we have a complete packet ready, but the previous 1476a9b3dca5SArchie Cobbs * packet is still incomplete, we have to decide between delivering the 1477a9b3dca5SArchie Cobbs * complete packet and throwing away the incomplete one, or waiting to 1478a9b3dca5SArchie Cobbs * see if the remainder of the incomplete one arrives, at which time we 1479a9b3dca5SArchie Cobbs * can deliver both packets, in order. 1480a9b3dca5SArchie Cobbs * 1481a9b3dca5SArchie Cobbs * This problem is exacerbated by "sequence number slew", which is when 1482a9b3dca5SArchie Cobbs * the sequence numbers coming in from different links are far apart from 1483a9b3dca5SArchie Cobbs * each other. In particular, certain unnamed equipment (*cough* Ascend) 1484a9b3dca5SArchie Cobbs * has been seen to generate sequence number slew of up to 10 on an ISDN 1485a9b3dca5SArchie Cobbs * 2B-channel MP link. There is nothing invalid about sequence number slew 1486a9b3dca5SArchie Cobbs * but it makes the reasssembly process have to work harder. 1487a9b3dca5SArchie Cobbs * 1488a9b3dca5SArchie Cobbs * However, the peer is required to transmit fragments in order on each 1489a9b3dca5SArchie Cobbs * link. That means if we define MSEQ as the minimum over all links of 1490a9b3dca5SArchie Cobbs * the highest sequence number received on that link, then we can always 1491a9b3dca5SArchie Cobbs * give up any hope of receiving a fragment with sequence number < MSEQ in 1492a9b3dca5SArchie Cobbs * the future (all of this using 'wraparound' sequence number space). 1493a9b3dca5SArchie Cobbs * Therefore we can always immediately throw away incomplete packets 1494a9b3dca5SArchie Cobbs * missing fragments with sequence numbers < MSEQ. 1495a9b3dca5SArchie Cobbs * 1496a9b3dca5SArchie Cobbs * Here is an overview of our algorithm: 1497a9b3dca5SArchie Cobbs * 1498a9b3dca5SArchie Cobbs * o Received fragments are inserted into a queue, for which we 1499a9b3dca5SArchie Cobbs * maintain these invariants between calls to this function: 1500a9b3dca5SArchie Cobbs * 1501a9b3dca5SArchie Cobbs * - Fragments are ordered in the queue by sequence number 1502a9b3dca5SArchie Cobbs * - If a complete packet is at the head of the queue, then 1503a9b3dca5SArchie Cobbs * the first fragment in the packet has seq# > MSEQ + 1 1504a9b3dca5SArchie Cobbs * (otherwise, we could deliver it immediately) 1505a9b3dca5SArchie Cobbs * - If any fragments have seq# < MSEQ, then they are necessarily 1506a9b3dca5SArchie Cobbs * part of a packet whose missing seq#'s are all > MSEQ (otherwise, 1507a9b3dca5SArchie Cobbs * we can throw them away because they'll never be completed) 1508a9b3dca5SArchie Cobbs * - The queue contains at most MP_MAX_QUEUE_LEN fragments 1509a9b3dca5SArchie Cobbs * 1510a9b3dca5SArchie Cobbs * o We have a periodic timer that checks the queue for the first 1511a9b3dca5SArchie Cobbs * complete packet that has been sitting in the queue "too long". 1512a9b3dca5SArchie Cobbs * When one is detected, all previous (incomplete) fragments are 1513a9b3dca5SArchie Cobbs * discarded, their missing fragments are declared lost and MSEQ 1514a9b3dca5SArchie Cobbs * is increased. 1515a9b3dca5SArchie Cobbs * 1516053359b7SPedro F. Giffuni * o If we receive a fragment with seq# < MSEQ, we throw it away 1517*0a8159d8SGordon Bergling * because we've already declared it lost. 1518a9b3dca5SArchie Cobbs * 1519a9b3dca5SArchie Cobbs * This assumes linkNum != NG_PPP_BUNDLE_LINKNUM. 15203949bee8SArchie Cobbs */ 15213949bee8SArchie Cobbs static int 1522164b576eSGleb Smirnoff ng_ppp_mp_recv(node_p node, item_p item, uint16_t proto, uint16_t linkNum) 15233949bee8SArchie Cobbs { 152430400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 1525a9b3dca5SArchie Cobbs struct ng_ppp_link *const link = &priv->links[linkNum]; 152693caf2e2SAlexander Motin struct ng_ppp_frag *frag; 1527a9b3dca5SArchie Cobbs struct ng_ppp_frag *qent; 1528a9b3dca5SArchie Cobbs int i, diff, inserted; 1529069154d5SJulian Elischer struct mbuf *m; 1530e89c1507SAlexander Motin int error = 0; 15313949bee8SArchie Cobbs 1532e89c1507SAlexander Motin if ((!priv->conf.enableMultilink) || proto != PROT_MP) { 1533e89c1507SAlexander Motin /* Stats */ 1534e89c1507SAlexander Motin priv->bundleStats.recvFrames++; 1535e89c1507SAlexander Motin priv->bundleStats.recvOctets += NGI_M(item)->m_pkthdr.len; 1536e89c1507SAlexander Motin 1537e89c1507SAlexander Motin mtx_unlock(&priv->rmtx); 1538164b576eSGleb Smirnoff return (ng_ppp_crypt_recv(node, item, proto, linkNum)); 1539e89c1507SAlexander Motin } 1540164b576eSGleb Smirnoff 1541069154d5SJulian Elischer NGI_GET_M(item, m); 1542b4c44c30SArchie Cobbs 154393caf2e2SAlexander Motin /* Get a new frag struct from the free queue */ 154493caf2e2SAlexander Motin if ((frag = TAILQ_FIRST(&priv->fragsfree)) == NULL) { 154593caf2e2SAlexander Motin printf("No free fragments headers in ng_ppp!\n"); 154693caf2e2SAlexander Motin NG_FREE_M(m); 154793caf2e2SAlexander Motin goto process; 154893caf2e2SAlexander Motin } 154993caf2e2SAlexander Motin 15503949bee8SArchie Cobbs /* Extract fragment information from MP header */ 15513949bee8SArchie Cobbs if (priv->conf.recvShortSeq) { 1552164b576eSGleb Smirnoff uint16_t shdr; 15533949bee8SArchie Cobbs 15543949bee8SArchie Cobbs if (m->m_pkthdr.len < 2) { 1555a9b3dca5SArchie Cobbs link->stats.runts++; 1556069154d5SJulian Elischer NG_FREE_M(m); 1557e89c1507SAlexander Motin ERROUT(EINVAL); 15583949bee8SArchie Cobbs } 15593ca24c28SJulian Elischer if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) 1560e89c1507SAlexander Motin ERROUT(ENOBUFS); 15613ca24c28SJulian Elischer 156239228864SAlexander Motin shdr = be16dec(mtod(m, void *)); 15636f16db81SArchie Cobbs frag->seq = MP_SHORT_EXTEND(shdr); 15643949bee8SArchie Cobbs frag->first = (shdr & MP_SHORT_FIRST_FLAG) != 0; 15653949bee8SArchie Cobbs frag->last = (shdr & MP_SHORT_LAST_FLAG) != 0; 1566a9b3dca5SArchie Cobbs diff = MP_SHORT_SEQ_DIFF(frag->seq, priv->mseq); 15673949bee8SArchie Cobbs m_adj(m, 2); 15683949bee8SArchie Cobbs } else { 1569164b576eSGleb Smirnoff uint32_t lhdr; 15703949bee8SArchie Cobbs 15713949bee8SArchie Cobbs if (m->m_pkthdr.len < 4) { 1572a9b3dca5SArchie Cobbs link->stats.runts++; 1573069154d5SJulian Elischer NG_FREE_M(m); 1574e89c1507SAlexander Motin ERROUT(EINVAL); 15753949bee8SArchie Cobbs } 15763ca24c28SJulian Elischer if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) 1577e89c1507SAlexander Motin ERROUT(ENOBUFS); 15783ca24c28SJulian Elischer 157939228864SAlexander Motin lhdr = be32dec(mtod(m, void *)); 15806f16db81SArchie Cobbs frag->seq = MP_LONG_EXTEND(lhdr); 15813949bee8SArchie Cobbs frag->first = (lhdr & MP_LONG_FIRST_FLAG) != 0; 15823949bee8SArchie Cobbs frag->last = (lhdr & MP_LONG_LAST_FLAG) != 0; 1583a9b3dca5SArchie Cobbs diff = MP_LONG_SEQ_DIFF(frag->seq, priv->mseq); 15843949bee8SArchie Cobbs m_adj(m, 4); 15853949bee8SArchie Cobbs } 15863949bee8SArchie Cobbs frag->data = m; 1587a9b3dca5SArchie Cobbs getmicrouptime(&frag->timestamp); 15883949bee8SArchie Cobbs 1589a9b3dca5SArchie Cobbs /* If sequence number is < MSEQ, we've already declared this 1590a9b3dca5SArchie Cobbs fragment as lost, so we have no choice now but to drop it */ 1591a9b3dca5SArchie Cobbs if (diff < 0) { 1592a9b3dca5SArchie Cobbs link->stats.dropFragments++; 1593069154d5SJulian Elischer NG_FREE_M(m); 1594e89c1507SAlexander Motin ERROUT(0); 1595a9b3dca5SArchie Cobbs } 15963949bee8SArchie Cobbs 1597a9b3dca5SArchie Cobbs /* Update highest received sequence number on this link and MSEQ */ 1598a9b3dca5SArchie Cobbs priv->mseq = link->seq = frag->seq; 1599a9b3dca5SArchie Cobbs for (i = 0; i < priv->numActiveLinks; i++) { 1600a9b3dca5SArchie Cobbs struct ng_ppp_link *const alink = 1601a9b3dca5SArchie Cobbs &priv->links[priv->activeLinks[i]]; 1602a9b3dca5SArchie Cobbs 16036f16db81SArchie Cobbs if (MP_RECV_SEQ_DIFF(priv, alink->seq, priv->mseq) < 0) 1604a9b3dca5SArchie Cobbs priv->mseq = alink->seq; 1605a9b3dca5SArchie Cobbs } 16063949bee8SArchie Cobbs 160793caf2e2SAlexander Motin /* Remove frag struct from free queue. */ 160893caf2e2SAlexander Motin TAILQ_REMOVE(&priv->fragsfree, frag, f_qent); 16093949bee8SArchie Cobbs 1610a9b3dca5SArchie Cobbs /* Add fragment to queue, which is sorted by sequence number */ 16111e7a9f72SArchie Cobbs inserted = 0; 16125f90cac7SKirk McKusick TAILQ_FOREACH_REVERSE(qent, &priv->frags, ng_ppp_fraglist, f_qent) { 16136f16db81SArchie Cobbs diff = MP_RECV_SEQ_DIFF(priv, frag->seq, qent->seq); 16143949bee8SArchie Cobbs if (diff > 0) { 16155f90cac7SKirk McKusick TAILQ_INSERT_AFTER(&priv->frags, qent, frag, f_qent); 16161e7a9f72SArchie Cobbs inserted = 1; 16173949bee8SArchie Cobbs break; 16183949bee8SArchie Cobbs } else if (diff == 0) { /* should never happen! */ 1619a9b3dca5SArchie Cobbs link->stats.dupFragments++; 1620069154d5SJulian Elischer NG_FREE_M(frag->data); 162193caf2e2SAlexander Motin TAILQ_INSERT_HEAD(&priv->fragsfree, frag, f_qent); 1622e89c1507SAlexander Motin ERROUT(EINVAL); 16233949bee8SArchie Cobbs } 16243949bee8SArchie Cobbs } 16251e7a9f72SArchie Cobbs if (!inserted) 16265f90cac7SKirk McKusick TAILQ_INSERT_HEAD(&priv->frags, frag, f_qent); 16273949bee8SArchie Cobbs 1628e89c1507SAlexander Motin process: 1629a9b3dca5SArchie Cobbs /* Process the queue */ 1630e89c1507SAlexander Motin /* NOTE: rmtx will be unlocked for sending time! */ 16315716b3f1SAlexander Motin error = ng_ppp_frag_process(node, item); 16325716b3f1SAlexander Motin mtx_unlock(&priv->rmtx); 16335716b3f1SAlexander Motin return (error); 1634e89c1507SAlexander Motin 1635e89c1507SAlexander Motin done: 1636e89c1507SAlexander Motin mtx_unlock(&priv->rmtx); 16375716b3f1SAlexander Motin NG_FREE_ITEM(item); 1638e89c1507SAlexander Motin return (error); 16393949bee8SArchie Cobbs } 16401e7a9f72SArchie Cobbs 1641164b576eSGleb Smirnoff /************************************************************************ 1642164b576eSGleb Smirnoff HELPER STUFF 1643164b576eSGleb Smirnoff ************************************************************************/ 1644164b576eSGleb Smirnoff 1645a9b3dca5SArchie Cobbs /* 16468e8f114eSAlexander Motin * If new mseq > current then set it and update all active links 16478e8f114eSAlexander Motin */ 16488e8f114eSAlexander Motin static void 16498e8f114eSAlexander Motin ng_ppp_bump_mseq(node_p node, int32_t new_mseq) 16508e8f114eSAlexander Motin { 16518e8f114eSAlexander Motin const priv_p priv = NG_NODE_PRIVATE(node); 16528e8f114eSAlexander Motin int i; 16538e8f114eSAlexander Motin 16548e8f114eSAlexander Motin if (MP_RECV_SEQ_DIFF(priv, priv->mseq, new_mseq) < 0) { 16558e8f114eSAlexander Motin priv->mseq = new_mseq; 16568e8f114eSAlexander Motin for (i = 0; i < priv->numActiveLinks; i++) { 16578e8f114eSAlexander Motin struct ng_ppp_link *const alink = 16588e8f114eSAlexander Motin &priv->links[priv->activeLinks[i]]; 16598e8f114eSAlexander Motin 16608e8f114eSAlexander Motin if (MP_RECV_SEQ_DIFF(priv, 16618e8f114eSAlexander Motin alink->seq, new_mseq) < 0) 16628e8f114eSAlexander Motin alink->seq = new_mseq; 16638e8f114eSAlexander Motin } 16648e8f114eSAlexander Motin } 16658e8f114eSAlexander Motin } 16668e8f114eSAlexander Motin 16678e8f114eSAlexander Motin /* 1668a9b3dca5SArchie Cobbs * Examine our list of fragments, and determine if there is a 1669a9b3dca5SArchie Cobbs * complete and deliverable packet at the head of the list. 1670a9b3dca5SArchie Cobbs * Return 1 if so, zero otherwise. 1671a9b3dca5SArchie Cobbs */ 1672a9b3dca5SArchie Cobbs static int 1673a9b3dca5SArchie Cobbs ng_ppp_check_packet(node_p node) 1674a9b3dca5SArchie Cobbs { 167530400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 1676a9b3dca5SArchie Cobbs struct ng_ppp_frag *qent, *qnext; 16773949bee8SArchie Cobbs 1678a9b3dca5SArchie Cobbs /* Check for empty queue */ 16795f90cac7SKirk McKusick if (TAILQ_EMPTY(&priv->frags)) 1680a9b3dca5SArchie Cobbs return (0); 1681a9b3dca5SArchie Cobbs 1682a9b3dca5SArchie Cobbs /* Check first fragment is the start of a deliverable packet */ 16835f90cac7SKirk McKusick qent = TAILQ_FIRST(&priv->frags); 16846f16db81SArchie Cobbs if (!qent->first || MP_RECV_SEQ_DIFF(priv, qent->seq, priv->mseq) > 1) 1685a9b3dca5SArchie Cobbs return (0); 1686a9b3dca5SArchie Cobbs 1687a9b3dca5SArchie Cobbs /* Check that all the fragments are there */ 1688a9b3dca5SArchie Cobbs while (!qent->last) { 16895f90cac7SKirk McKusick qnext = TAILQ_NEXT(qent, f_qent); 16905f90cac7SKirk McKusick if (qnext == NULL) /* end of queue */ 1691a9b3dca5SArchie Cobbs return (0); 16926f16db81SArchie Cobbs if (qnext->seq != MP_NEXT_RECV_SEQ(priv, qent->seq)) 1693a9b3dca5SArchie Cobbs return (0); 1694a9b3dca5SArchie Cobbs qent = qnext; 1695a9b3dca5SArchie Cobbs } 1696a9b3dca5SArchie Cobbs 1697a9b3dca5SArchie Cobbs /* Got one */ 1698a9b3dca5SArchie Cobbs return (1); 1699a9b3dca5SArchie Cobbs } 1700a9b3dca5SArchie Cobbs 1701a9b3dca5SArchie Cobbs /* 1702a9b3dca5SArchie Cobbs * Pull a completed packet off the head of the incoming fragment queue. 1703a9b3dca5SArchie Cobbs * This assumes there is a completed packet there to pull off. 1704a9b3dca5SArchie Cobbs */ 1705a9b3dca5SArchie Cobbs static void 17063ca24c28SJulian Elischer ng_ppp_get_packet(node_p node, struct mbuf **mp) 1707a9b3dca5SArchie Cobbs { 170830400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 1709a9b3dca5SArchie Cobbs struct ng_ppp_frag *qent, *qnext; 1710a9b3dca5SArchie Cobbs struct mbuf *m = NULL, *tail; 1711a9b3dca5SArchie Cobbs 17125f90cac7SKirk McKusick qent = TAILQ_FIRST(&priv->frags); 17135f90cac7SKirk McKusick KASSERT(!TAILQ_EMPTY(&priv->frags) && qent->first, 17146e551fb6SDavid E. O'Brien ("%s: no packet", __func__)); 1715a9b3dca5SArchie Cobbs for (tail = NULL; qent != NULL; qent = qnext) { 17165f90cac7SKirk McKusick qnext = TAILQ_NEXT(qent, f_qent); 17175f90cac7SKirk McKusick KASSERT(!TAILQ_EMPTY(&priv->frags), 17186e551fb6SDavid E. O'Brien ("%s: empty q", __func__)); 17195f90cac7SKirk McKusick TAILQ_REMOVE(&priv->frags, qent, f_qent); 17203ca24c28SJulian Elischer if (tail == NULL) 17213949bee8SArchie Cobbs tail = m = qent->data; 17223ca24c28SJulian Elischer else { 17233949bee8SArchie Cobbs m->m_pkthdr.len += qent->data->m_pkthdr.len; 17243949bee8SArchie Cobbs tail->m_next = qent->data; 17253949bee8SArchie Cobbs } 17263949bee8SArchie Cobbs while (tail->m_next != NULL) 17273949bee8SArchie Cobbs tail = tail->m_next; 17288e8f114eSAlexander Motin if (qent->last) { 17293949bee8SArchie Cobbs qnext = NULL; 17308e8f114eSAlexander Motin /* Bump MSEQ if necessary */ 17318e8f114eSAlexander Motin ng_ppp_bump_mseq(node, qent->seq); 17328e8f114eSAlexander Motin } 173393caf2e2SAlexander Motin TAILQ_INSERT_HEAD(&priv->fragsfree, qent, f_qent); 1734a9b3dca5SArchie Cobbs } 1735a9b3dca5SArchie Cobbs *mp = m; 17363949bee8SArchie Cobbs } 17373949bee8SArchie Cobbs 1738a9b3dca5SArchie Cobbs /* 1739a9b3dca5SArchie Cobbs * Trim fragments from the queue whose packets can never be completed. 1740a9b3dca5SArchie Cobbs * This assumes a complete packet is NOT at the beginning of the queue. 1741a9b3dca5SArchie Cobbs * Returns 1 if fragments were removed, zero otherwise. 1742a9b3dca5SArchie Cobbs */ 1743a9b3dca5SArchie Cobbs static int 1744a9b3dca5SArchie Cobbs ng_ppp_frag_trim(node_p node) 1745a9b3dca5SArchie Cobbs { 174630400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 1747a9b3dca5SArchie Cobbs struct ng_ppp_frag *qent, *qnext = NULL; 1748a9b3dca5SArchie Cobbs int removed = 0; 1749a9b3dca5SArchie Cobbs 1750a9b3dca5SArchie Cobbs /* Scan for "dead" fragments and remove them */ 1751a9b3dca5SArchie Cobbs while (1) { 1752a9b3dca5SArchie Cobbs int dead = 0; 1753a9b3dca5SArchie Cobbs 1754a9b3dca5SArchie Cobbs /* If queue is empty, we're done */ 17555f90cac7SKirk McKusick if (TAILQ_EMPTY(&priv->frags)) 17563949bee8SArchie Cobbs break; 1757a9b3dca5SArchie Cobbs 1758a9b3dca5SArchie Cobbs /* Determine whether first fragment can ever be completed */ 17595f90cac7SKirk McKusick TAILQ_FOREACH(qent, &priv->frags, f_qent) { 17606f16db81SArchie Cobbs if (MP_RECV_SEQ_DIFF(priv, qent->seq, priv->mseq) >= 0) 1761a9b3dca5SArchie Cobbs break; 17625f90cac7SKirk McKusick qnext = TAILQ_NEXT(qent, f_qent); 17635f90cac7SKirk McKusick KASSERT(qnext != NULL, 17646e551fb6SDavid E. O'Brien ("%s: last frag < MSEQ?", __func__)); 17656f16db81SArchie Cobbs if (qnext->seq != MP_NEXT_RECV_SEQ(priv, qent->seq) 1766a9b3dca5SArchie Cobbs || qent->last || qnext->first) { 1767a9b3dca5SArchie Cobbs dead = 1; 1768a9b3dca5SArchie Cobbs break; 1769a9b3dca5SArchie Cobbs } 1770a9b3dca5SArchie Cobbs } 1771a9b3dca5SArchie Cobbs if (!dead) 1772a9b3dca5SArchie Cobbs break; 1773a9b3dca5SArchie Cobbs 1774a9b3dca5SArchie Cobbs /* Remove fragment and all others in the same packet */ 17755f90cac7SKirk McKusick while ((qent = TAILQ_FIRST(&priv->frags)) != qnext) { 17765f90cac7SKirk McKusick KASSERT(!TAILQ_EMPTY(&priv->frags), 17776e551fb6SDavid E. O'Brien ("%s: empty q", __func__)); 1778a9b3dca5SArchie Cobbs priv->bundleStats.dropFragments++; 17795f90cac7SKirk McKusick TAILQ_REMOVE(&priv->frags, qent, f_qent); 1780069154d5SJulian Elischer NG_FREE_M(qent->data); 178193caf2e2SAlexander Motin TAILQ_INSERT_HEAD(&priv->fragsfree, qent, f_qent); 1782a9b3dca5SArchie Cobbs removed = 1; 1783a9b3dca5SArchie Cobbs } 1784a9b3dca5SArchie Cobbs } 1785a9b3dca5SArchie Cobbs return (removed); 17863949bee8SArchie Cobbs } 17873949bee8SArchie Cobbs 1788a9b3dca5SArchie Cobbs /* 17898e8f114eSAlexander Motin * Drop fragments on queue overflow. 17908e8f114eSAlexander Motin * Returns 1 if fragments were removed, zero otherwise. 17918e8f114eSAlexander Motin */ 17928e8f114eSAlexander Motin static int 17938e8f114eSAlexander Motin ng_ppp_frag_drop(node_p node) 17948e8f114eSAlexander Motin { 17958e8f114eSAlexander Motin const priv_p priv = NG_NODE_PRIVATE(node); 17968e8f114eSAlexander Motin 17978e8f114eSAlexander Motin /* Check queue length */ 179893caf2e2SAlexander Motin if (TAILQ_EMPTY(&priv->fragsfree)) { 17998e8f114eSAlexander Motin struct ng_ppp_frag *qent; 18008e8f114eSAlexander Motin 18018e8f114eSAlexander Motin /* Get oldest fragment */ 18028e8f114eSAlexander Motin KASSERT(!TAILQ_EMPTY(&priv->frags), 18038e8f114eSAlexander Motin ("%s: empty q", __func__)); 18048e8f114eSAlexander Motin qent = TAILQ_FIRST(&priv->frags); 18058e8f114eSAlexander Motin 18068e8f114eSAlexander Motin /* Bump MSEQ if necessary */ 18078e8f114eSAlexander Motin ng_ppp_bump_mseq(node, qent->seq); 18088e8f114eSAlexander Motin 18098e8f114eSAlexander Motin /* Drop it */ 18108e8f114eSAlexander Motin priv->bundleStats.dropFragments++; 18118e8f114eSAlexander Motin TAILQ_REMOVE(&priv->frags, qent, f_qent); 18128e8f114eSAlexander Motin NG_FREE_M(qent->data); 181393caf2e2SAlexander Motin TAILQ_INSERT_HEAD(&priv->fragsfree, qent, f_qent); 18148e8f114eSAlexander Motin 18158e8f114eSAlexander Motin return (1); 18168e8f114eSAlexander Motin } 18178e8f114eSAlexander Motin return (0); 18188e8f114eSAlexander Motin } 18198e8f114eSAlexander Motin 18208e8f114eSAlexander Motin /* 1821a9b3dca5SArchie Cobbs * Run the queue, restoring the queue invariants 1822a9b3dca5SArchie Cobbs */ 1823a9b3dca5SArchie Cobbs static int 18245716b3f1SAlexander Motin ng_ppp_frag_process(node_p node, item_p oitem) 1825a9b3dca5SArchie Cobbs { 182630400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 1827a9b3dca5SArchie Cobbs struct mbuf *m; 1828069154d5SJulian Elischer item_p item; 1829164b576eSGleb Smirnoff uint16_t proto; 1830a9b3dca5SArchie Cobbs 18318e8f114eSAlexander Motin do { 1832a9b3dca5SArchie Cobbs /* Deliver any deliverable packets */ 1833a9b3dca5SArchie Cobbs while (ng_ppp_check_packet(node)) { 18343ca24c28SJulian Elischer ng_ppp_get_packet(node, &m); 1835164b576eSGleb Smirnoff if ((m = ng_ppp_cutproto(m, &proto)) == NULL) 1836164b576eSGleb Smirnoff continue; 1837164b576eSGleb Smirnoff if (!PROT_VALID(proto)) { 1838164b576eSGleb Smirnoff priv->bundleStats.badProtos++; 1839164b576eSGleb Smirnoff NG_FREE_M(m); 1840164b576eSGleb Smirnoff continue; 1841164b576eSGleb Smirnoff } 18425716b3f1SAlexander Motin if (oitem) { /* If original item present - reuse it. */ 18435716b3f1SAlexander Motin item = oitem; 18445716b3f1SAlexander Motin oitem = NULL; 18455716b3f1SAlexander Motin NGI_M(item) = m; 18465716b3f1SAlexander Motin } else { 18475716b3f1SAlexander Motin item = ng_package_data(m, NG_NOFLAGS); 18485716b3f1SAlexander Motin } 18495716b3f1SAlexander Motin if (item != NULL) { 1850e89c1507SAlexander Motin /* Stats */ 1851e89c1507SAlexander Motin priv->bundleStats.recvFrames++; 1852e89c1507SAlexander Motin priv->bundleStats.recvOctets += 1853e89c1507SAlexander Motin NGI_M(item)->m_pkthdr.len; 1854e89c1507SAlexander Motin 1855e89c1507SAlexander Motin /* Drop mutex for the sending time. 1856e89c1507SAlexander Motin * Priv may change, but we are ready! 1857e89c1507SAlexander Motin */ 1858e89c1507SAlexander Motin mtx_unlock(&priv->rmtx); 1859164b576eSGleb Smirnoff ng_ppp_crypt_recv(node, item, proto, 1860164b576eSGleb Smirnoff NG_PPP_BUNDLE_LINKNUM); 1861e89c1507SAlexander Motin mtx_lock(&priv->rmtx); 1862e89c1507SAlexander Motin } 1863a9b3dca5SArchie Cobbs } 1864a9b3dca5SArchie Cobbs /* Delete dead fragments and try again */ 18658e8f114eSAlexander Motin } while (ng_ppp_frag_trim(node) || ng_ppp_frag_drop(node)); 1866a9b3dca5SArchie Cobbs 18675716b3f1SAlexander Motin /* If we haven't reused original item - free it. */ 18685716b3f1SAlexander Motin if (oitem) NG_FREE_ITEM(oitem); 18695716b3f1SAlexander Motin 1870a9b3dca5SArchie Cobbs /* Done */ 1871a9b3dca5SArchie Cobbs return (0); 1872a9b3dca5SArchie Cobbs } 1873a9b3dca5SArchie Cobbs 1874a9b3dca5SArchie Cobbs /* 1875a9b3dca5SArchie Cobbs * Check for 'stale' completed packets that need to be delivered 1876a9b3dca5SArchie Cobbs * 1877a9b3dca5SArchie Cobbs * If a link goes down or has a temporary failure, MSEQ can get 1878a9b3dca5SArchie Cobbs * "stuck", because no new incoming fragments appear on that link. 1879a9b3dca5SArchie Cobbs * This can cause completed packets to never get delivered if 1880a9b3dca5SArchie Cobbs * their sequence numbers are all > MSEQ + 1. 1881a9b3dca5SArchie Cobbs * 1882a9b3dca5SArchie Cobbs * This routine checks how long all of the completed packets have 1883a9b3dca5SArchie Cobbs * been sitting in the queue, and if too long, removes fragments 1884a9b3dca5SArchie Cobbs * from the queue and increments MSEQ to allow them to be delivered. 1885a9b3dca5SArchie Cobbs */ 1886a9b3dca5SArchie Cobbs static void 1887a9b3dca5SArchie Cobbs ng_ppp_frag_checkstale(node_p node) 1888a9b3dca5SArchie Cobbs { 188930400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 1890a9b3dca5SArchie Cobbs struct ng_ppp_frag *qent, *beg, *end; 1891a9b3dca5SArchie Cobbs struct timeval now, age; 1892a9b3dca5SArchie Cobbs struct mbuf *m; 18938e8f114eSAlexander Motin int seq; 1894069154d5SJulian Elischer item_p item; 18952b2a8188SArchie Cobbs int endseq; 1896164b576eSGleb Smirnoff uint16_t proto; 1897a9b3dca5SArchie Cobbs 1898a9b3dca5SArchie Cobbs now.tv_sec = 0; /* uninitialized state */ 1899a9b3dca5SArchie Cobbs while (1) { 1900a9b3dca5SArchie Cobbs /* If queue is empty, we're done */ 19015f90cac7SKirk McKusick if (TAILQ_EMPTY(&priv->frags)) 1902a9b3dca5SArchie Cobbs break; 1903a9b3dca5SArchie Cobbs 1904a9b3dca5SArchie Cobbs /* Find the first complete packet in the queue */ 1905a9b3dca5SArchie Cobbs beg = end = NULL; 19065f90cac7SKirk McKusick seq = TAILQ_FIRST(&priv->frags)->seq; 19075f90cac7SKirk McKusick TAILQ_FOREACH(qent, &priv->frags, f_qent) { 1908a9b3dca5SArchie Cobbs if (qent->first) 1909a9b3dca5SArchie Cobbs beg = qent; 1910a9b3dca5SArchie Cobbs else if (qent->seq != seq) 1911a9b3dca5SArchie Cobbs beg = NULL; 1912a9b3dca5SArchie Cobbs if (beg != NULL && qent->last) { 1913a9b3dca5SArchie Cobbs end = qent; 1914a9b3dca5SArchie Cobbs break; 1915a9b3dca5SArchie Cobbs } 19166f16db81SArchie Cobbs seq = MP_NEXT_RECV_SEQ(priv, seq); 1917a9b3dca5SArchie Cobbs } 1918a9b3dca5SArchie Cobbs 1919a9b3dca5SArchie Cobbs /* If none found, exit */ 1920a9b3dca5SArchie Cobbs if (end == NULL) 1921a9b3dca5SArchie Cobbs break; 1922a9b3dca5SArchie Cobbs 1923a9b3dca5SArchie Cobbs /* Get current time (we assume we've been up for >= 1 second) */ 1924a9b3dca5SArchie Cobbs if (now.tv_sec == 0) 1925a9b3dca5SArchie Cobbs getmicrouptime(&now); 1926a9b3dca5SArchie Cobbs 1927a9b3dca5SArchie Cobbs /* Check if packet has been queued too long */ 1928a9b3dca5SArchie Cobbs age = now; 1929a9b3dca5SArchie Cobbs timevalsub(&age, &beg->timestamp); 1930a9b3dca5SArchie Cobbs if (timevalcmp(&age, &ng_ppp_max_staleness, < )) 1931a9b3dca5SArchie Cobbs break; 1932a9b3dca5SArchie Cobbs 1933a9b3dca5SArchie Cobbs /* Throw away junk fragments in front of the completed packet */ 19345f90cac7SKirk McKusick while ((qent = TAILQ_FIRST(&priv->frags)) != beg) { 19355f90cac7SKirk McKusick KASSERT(!TAILQ_EMPTY(&priv->frags), 19366e551fb6SDavid E. O'Brien ("%s: empty q", __func__)); 1937a9b3dca5SArchie Cobbs priv->bundleStats.dropFragments++; 19385f90cac7SKirk McKusick TAILQ_REMOVE(&priv->frags, qent, f_qent); 1939069154d5SJulian Elischer NG_FREE_M(qent->data); 194093caf2e2SAlexander Motin TAILQ_INSERT_HEAD(&priv->fragsfree, qent, f_qent); 1941a9b3dca5SArchie Cobbs } 1942a9b3dca5SArchie Cobbs 1943a9b3dca5SArchie Cobbs /* Extract completed packet */ 19442b2a8188SArchie Cobbs endseq = end->seq; 19453ca24c28SJulian Elischer ng_ppp_get_packet(node, &m); 1946a9b3dca5SArchie Cobbs 1947164b576eSGleb Smirnoff if ((m = ng_ppp_cutproto(m, &proto)) == NULL) 1948164b576eSGleb Smirnoff continue; 1949164b576eSGleb Smirnoff if (!PROT_VALID(proto)) { 1950164b576eSGleb Smirnoff priv->bundleStats.badProtos++; 1951164b576eSGleb Smirnoff NG_FREE_M(m); 1952164b576eSGleb Smirnoff continue; 1953164b576eSGleb Smirnoff } 1954164b576eSGleb Smirnoff 1955a9b3dca5SArchie Cobbs /* Deliver packet */ 1956e89c1507SAlexander Motin if ((item = ng_package_data(m, NG_NOFLAGS)) != NULL) { 1957e89c1507SAlexander Motin /* Stats */ 1958e89c1507SAlexander Motin priv->bundleStats.recvFrames++; 1959e89c1507SAlexander Motin priv->bundleStats.recvOctets += NGI_M(item)->m_pkthdr.len; 1960e89c1507SAlexander Motin 1961164b576eSGleb Smirnoff ng_ppp_crypt_recv(node, item, proto, 1962164b576eSGleb Smirnoff NG_PPP_BUNDLE_LINKNUM); 1963a9b3dca5SArchie Cobbs } 1964a9b3dca5SArchie Cobbs } 1965e89c1507SAlexander Motin } 1966a9b3dca5SArchie Cobbs 1967a9b3dca5SArchie Cobbs /* 1968a9b3dca5SArchie Cobbs * Periodically call ng_ppp_frag_checkstale() 1969a9b3dca5SArchie Cobbs */ 1970a9b3dca5SArchie Cobbs static void 19719d952bd3SGleb Smirnoff ng_ppp_frag_timeout(node_p node, hook_p hook, void *arg1, int arg2) 1972a9b3dca5SArchie Cobbs { 19739d952bd3SGleb Smirnoff /* XXX: is this needed? */ 19749d952bd3SGleb Smirnoff if (NG_NODE_NOT_VALID(node)) 1975a9b3dca5SArchie Cobbs return; 1976a9b3dca5SArchie Cobbs 1977a9b3dca5SArchie Cobbs /* Scan the fragment queue */ 1978a9b3dca5SArchie Cobbs ng_ppp_frag_checkstale(node); 19799d952bd3SGleb Smirnoff 19809d952bd3SGleb Smirnoff /* Start timer again */ 19819d952bd3SGleb Smirnoff ng_ppp_start_frag_timer(node); 19823949bee8SArchie Cobbs } 19833949bee8SArchie Cobbs 19843949bee8SArchie Cobbs /* 19853949bee8SArchie Cobbs * Deliver a frame out on the bundle, i.e., figure out how to fragment 19863949bee8SArchie Cobbs * the frame across the individual PPP links and do so. 19873949bee8SArchie Cobbs */ 19883949bee8SArchie Cobbs static int 1989164b576eSGleb Smirnoff ng_ppp_mp_xmit(node_p node, item_p item, uint16_t proto) 19903949bee8SArchie Cobbs { 199130400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 1992931c828aSArchie Cobbs const int hdr_len = priv->conf.xmitShortSeq ? 2 : 4; 19933949bee8SArchie Cobbs int distrib[NG_PPP_MAX_LINKS]; 19943949bee8SArchie Cobbs int firstFragment; 19953949bee8SArchie Cobbs int activeLinkNum; 1996164b576eSGleb Smirnoff struct mbuf *m; 19979c10d113SAlexander Motin int plen; 1998e89c1507SAlexander Motin int frags; 1999e89c1507SAlexander Motin int32_t seq; 20003949bee8SArchie Cobbs 20013949bee8SArchie Cobbs /* At least one link must be active */ 20023949bee8SArchie Cobbs if (priv->numActiveLinks == 0) { 2003164b576eSGleb Smirnoff NG_FREE_ITEM(item); 20043949bee8SArchie Cobbs return (ENETDOWN); 20053949bee8SArchie Cobbs } 20063949bee8SArchie Cobbs 2007e89c1507SAlexander Motin /* Save length for later stats. */ 20089c10d113SAlexander Motin plen = NGI_M(item)->m_pkthdr.len; 2009164b576eSGleb Smirnoff 2010e89c1507SAlexander Motin if (!priv->conf.enableMultilink) { 2011164b576eSGleb Smirnoff return (ng_ppp_link_xmit(node, item, proto, 20129c10d113SAlexander Motin priv->activeLinks[0], plen)); 2013e89c1507SAlexander Motin } 2014164b576eSGleb Smirnoff 2015aa559f63SAlexander Motin /* Check peer's MRRU for this bundle. */ 2016aa559f63SAlexander Motin if (plen > priv->conf.mrru) { 2017aa559f63SAlexander Motin NG_FREE_ITEM(item); 2018aa559f63SAlexander Motin return (EMSGSIZE); 2019aa559f63SAlexander Motin } 2020aa559f63SAlexander Motin 2021164b576eSGleb Smirnoff /* Extract mbuf. */ 2022164b576eSGleb Smirnoff NGI_GET_M(item, m); 2023164b576eSGleb Smirnoff 2024164b576eSGleb Smirnoff /* Prepend protocol number, possibly compressed. */ 2025f9ec6990SAlexander Motin if ((m = ng_ppp_addproto(m, proto, 1)) == NULL) { 2026f9ec6990SAlexander Motin NG_FREE_ITEM(item); 2027164b576eSGleb Smirnoff return (ENOBUFS); 2028f9ec6990SAlexander Motin } 2029164b576eSGleb Smirnoff 2030ccffcb51SAlexander Motin /* Clear distribution plan */ 20313949bee8SArchie Cobbs bzero(&distrib, priv->numActiveLinks * sizeof(distrib[0])); 2032ccffcb51SAlexander Motin 2033e89c1507SAlexander Motin mtx_lock(&priv->xmtx); 2034e89c1507SAlexander Motin 2035ccffcb51SAlexander Motin /* Round-robin strategy */ 2036ccffcb51SAlexander Motin if (priv->conf.enableRoundRobin) { 2037ccffcb51SAlexander Motin activeLinkNum = priv->lastLink++ % priv->numActiveLinks; 20383949bee8SArchie Cobbs distrib[activeLinkNum] = m->m_pkthdr.len; 20393949bee8SArchie Cobbs goto deliver; 20403949bee8SArchie Cobbs } 20413949bee8SArchie Cobbs 20423949bee8SArchie Cobbs /* Strategy when all links are equivalent (optimize the common case) */ 20433949bee8SArchie Cobbs if (priv->allLinksEqual) { 2044ccffcb51SAlexander Motin int numFrags, fraction, remain; 2045ccffcb51SAlexander Motin int i; 20463949bee8SArchie Cobbs 2047ccffcb51SAlexander Motin /* Calculate optimal fragment count */ 2048ccffcb51SAlexander Motin numFrags = priv->numActiveLinks; 2049ccffcb51SAlexander Motin if (numFrags > m->m_pkthdr.len / MP_MIN_FRAG_LEN) 2050ccffcb51SAlexander Motin numFrags = m->m_pkthdr.len / MP_MIN_FRAG_LEN; 2051ccffcb51SAlexander Motin if (numFrags == 0) 2052ccffcb51SAlexander Motin numFrags = 1; 2053ccffcb51SAlexander Motin 2054ccffcb51SAlexander Motin fraction = m->m_pkthdr.len / numFrags; 2055ccffcb51SAlexander Motin remain = m->m_pkthdr.len - (fraction * numFrags); 2056ccffcb51SAlexander Motin 2057ccffcb51SAlexander Motin /* Assign distribution */ 2058ccffcb51SAlexander Motin for (i = 0; i < numFrags; i++) { 20593949bee8SArchie Cobbs distrib[priv->lastLink++ % priv->numActiveLinks] 2060ccffcb51SAlexander Motin = fraction + (((remain--) > 0)?1:0); 20613949bee8SArchie Cobbs } 20623949bee8SArchie Cobbs goto deliver; 20633949bee8SArchie Cobbs } 20643949bee8SArchie Cobbs 20653949bee8SArchie Cobbs /* Strategy when all links are not equivalent */ 20663949bee8SArchie Cobbs ng_ppp_mp_strategy(node, m->m_pkthdr.len, distrib); 20673949bee8SArchie Cobbs 20683949bee8SArchie Cobbs deliver: 2069e89c1507SAlexander Motin /* Estimate fragments count */ 2070e89c1507SAlexander Motin frags = 0; 2071e89c1507SAlexander Motin for (activeLinkNum = priv->numActiveLinks - 1; 2072e89c1507SAlexander Motin activeLinkNum >= 0; activeLinkNum--) { 2073e89c1507SAlexander Motin const uint16_t linkNum = priv->activeLinks[activeLinkNum]; 2074e89c1507SAlexander Motin struct ng_ppp_link *const link = &priv->links[linkNum]; 2075e89c1507SAlexander Motin 2076e89c1507SAlexander Motin frags += (distrib[activeLinkNum] + link->conf.mru - hdr_len - 1) / 2077e89c1507SAlexander Motin (link->conf.mru - hdr_len); 2078e89c1507SAlexander Motin } 2079e89c1507SAlexander Motin 2080e89c1507SAlexander Motin /* Get out initial sequence number */ 2081e89c1507SAlexander Motin seq = priv->xseq; 2082e89c1507SAlexander Motin 2083e89c1507SAlexander Motin /* Update next sequence number */ 2084e89c1507SAlexander Motin if (priv->conf.xmitShortSeq) { 2085e89c1507SAlexander Motin priv->xseq = (seq + frags) & MP_SHORT_SEQ_MASK; 2086e89c1507SAlexander Motin } else { 2087e89c1507SAlexander Motin priv->xseq = (seq + frags) & MP_LONG_SEQ_MASK; 2088e89c1507SAlexander Motin } 2089e89c1507SAlexander Motin 2090e89c1507SAlexander Motin mtx_unlock(&priv->xmtx); 2091e89c1507SAlexander Motin 20923949bee8SArchie Cobbs /* Send alloted portions of frame out on the link(s) */ 20933949bee8SArchie Cobbs for (firstFragment = 1, activeLinkNum = priv->numActiveLinks - 1; 20943949bee8SArchie Cobbs activeLinkNum >= 0; activeLinkNum--) { 2095164b576eSGleb Smirnoff const uint16_t linkNum = priv->activeLinks[activeLinkNum]; 2096a9b3dca5SArchie Cobbs struct ng_ppp_link *const link = &priv->links[linkNum]; 20973949bee8SArchie Cobbs 20983949bee8SArchie Cobbs /* Deliver fragment(s) out the next link */ 20993949bee8SArchie Cobbs for ( ; distrib[activeLinkNum] > 0; firstFragment = 0) { 21003949bee8SArchie Cobbs int len, lastFragment, error; 21013949bee8SArchie Cobbs struct mbuf *m2; 21023949bee8SArchie Cobbs 21033949bee8SArchie Cobbs /* Calculate fragment length; don't exceed link MTU */ 21043949bee8SArchie Cobbs len = distrib[activeLinkNum]; 2105931c828aSArchie Cobbs if (len > link->conf.mru - hdr_len) 2106931c828aSArchie Cobbs len = link->conf.mru - hdr_len; 21073949bee8SArchie Cobbs distrib[activeLinkNum] -= len; 21083949bee8SArchie Cobbs lastFragment = (len == m->m_pkthdr.len); 21093949bee8SArchie Cobbs 21103949bee8SArchie Cobbs /* Split off next fragment as "m2" */ 21113949bee8SArchie Cobbs m2 = m; 21123949bee8SArchie Cobbs if (!lastFragment) { 2113eb1b1807SGleb Smirnoff struct mbuf *n = m_split(m, len, M_NOWAIT); 21143949bee8SArchie Cobbs 21153949bee8SArchie Cobbs if (n == NULL) { 2116069154d5SJulian Elischer NG_FREE_M(m); 2117f9ec6990SAlexander Motin if (firstFragment) 2118f9ec6990SAlexander Motin NG_FREE_ITEM(item); 21193949bee8SArchie Cobbs return (ENOMEM); 21203949bee8SArchie Cobbs } 2121eb1b1807SGleb Smirnoff m_tag_copy_chain(n, m, M_NOWAIT); 21223949bee8SArchie Cobbs m = n; 21233949bee8SArchie Cobbs } 21243949bee8SArchie Cobbs 21253949bee8SArchie Cobbs /* Prepend MP header */ 21263949bee8SArchie Cobbs if (priv->conf.xmitShortSeq) { 2127164b576eSGleb Smirnoff uint16_t shdr; 21283949bee8SArchie Cobbs 2129e89c1507SAlexander Motin shdr = seq; 2130e89c1507SAlexander Motin seq = (seq + 1) & MP_SHORT_SEQ_MASK; 21313949bee8SArchie Cobbs if (firstFragment) 21323949bee8SArchie Cobbs shdr |= MP_SHORT_FIRST_FLAG; 21333949bee8SArchie Cobbs if (lastFragment) 21343949bee8SArchie Cobbs shdr |= MP_SHORT_LAST_FLAG; 2135d690a6e7SArchie Cobbs shdr = htons(shdr); 2136d690a6e7SArchie Cobbs m2 = ng_ppp_prepend(m2, &shdr, 2); 21373949bee8SArchie Cobbs } else { 2138164b576eSGleb Smirnoff uint32_t lhdr; 21393949bee8SArchie Cobbs 2140e89c1507SAlexander Motin lhdr = seq; 2141e89c1507SAlexander Motin seq = (seq + 1) & MP_LONG_SEQ_MASK; 21423949bee8SArchie Cobbs if (firstFragment) 21433949bee8SArchie Cobbs lhdr |= MP_LONG_FIRST_FLAG; 21443949bee8SArchie Cobbs if (lastFragment) 21453949bee8SArchie Cobbs lhdr |= MP_LONG_LAST_FLAG; 2146d690a6e7SArchie Cobbs lhdr = htonl(lhdr); 2147d690a6e7SArchie Cobbs m2 = ng_ppp_prepend(m2, &lhdr, 4); 21483949bee8SArchie Cobbs } 21493949bee8SArchie Cobbs if (m2 == NULL) { 21503949bee8SArchie Cobbs if (!lastFragment) 21513949bee8SArchie Cobbs m_freem(m); 2152f9ec6990SAlexander Motin if (firstFragment) 2153f9ec6990SAlexander Motin NG_FREE_ITEM(item); 21543949bee8SArchie Cobbs return (ENOBUFS); 21553949bee8SArchie Cobbs } 21563949bee8SArchie Cobbs 21573949bee8SArchie Cobbs /* Send fragment */ 2158f9ec6990SAlexander Motin if (firstFragment) { 2159f9ec6990SAlexander Motin NGI_M(item) = m2; /* Reuse original item. */ 2160f9ec6990SAlexander Motin } else { 2161f9ec6990SAlexander Motin item = ng_package_data(m2, NG_NOFLAGS); 2162f9ec6990SAlexander Motin } 2163f9ec6990SAlexander Motin if (item != NULL) { 2164164b576eSGleb Smirnoff error = ng_ppp_link_xmit(node, item, PROT_MP, 21659c10d113SAlexander Motin linkNum, (firstFragment?plen:0)); 21663949bee8SArchie Cobbs if (error != 0) { 21673ca24c28SJulian Elischer if (!lastFragment) 2168069154d5SJulian Elischer NG_FREE_M(m); 21693949bee8SArchie Cobbs return (error); 21703949bee8SArchie Cobbs } 21713949bee8SArchie Cobbs } 21723949bee8SArchie Cobbs } 2173d0784b47SGleb Smirnoff } 21743949bee8SArchie Cobbs 21753949bee8SArchie Cobbs /* Done */ 21763949bee8SArchie Cobbs return (0); 21773949bee8SArchie Cobbs } 21783949bee8SArchie Cobbs 21793949bee8SArchie Cobbs /* 21803949bee8SArchie Cobbs * Computing the optimal fragmentation 21813949bee8SArchie Cobbs * ----------------------------------- 21823949bee8SArchie Cobbs * 21833949bee8SArchie Cobbs * This routine tries to compute the optimal fragmentation pattern based 21843949bee8SArchie Cobbs * on each link's latency, bandwidth, and calculated additional latency. 21853949bee8SArchie Cobbs * The latter quantity is the additional latency caused by previously 21863949bee8SArchie Cobbs * written data that has not been transmitted yet. 21873949bee8SArchie Cobbs * 21883949bee8SArchie Cobbs * This algorithm is only useful when not all of the links have the 21893949bee8SArchie Cobbs * same latency and bandwidth values. 21903949bee8SArchie Cobbs * 21913949bee8SArchie Cobbs * The essential idea is to make the last bit of each fragment of the 21923949bee8SArchie Cobbs * frame arrive at the opposite end at the exact same time. This greedy 21933949bee8SArchie Cobbs * algorithm is optimal, in that no other scheduling could result in any 21943949bee8SArchie Cobbs * packet arriving any sooner unless packets are delivered out of order. 21953949bee8SArchie Cobbs * 21963949bee8SArchie Cobbs * Suppose link i has bandwidth b_i (in tens of bytes per milisecond) and 21973949bee8SArchie Cobbs * latency l_i (in miliseconds). Consider the function function f_i(t) 21983949bee8SArchie Cobbs * which is equal to the number of bytes that will have arrived at 21993949bee8SArchie Cobbs * the peer after t miliseconds if we start writing continuously at 22003949bee8SArchie Cobbs * time t = 0. Then f_i(t) = b_i * (t - l_i) = ((b_i * t) - (l_i * b_i). 22013949bee8SArchie Cobbs * That is, f_i(t) is a line with slope b_i and y-intersect -(l_i * b_i). 22023949bee8SArchie Cobbs * Note that the y-intersect is always <= zero because latency can't be 22033949bee8SArchie Cobbs * negative. Note also that really the function is f_i(t) except when 22043949bee8SArchie Cobbs * f_i(t) is negative, in which case the function is zero. To take 22053949bee8SArchie Cobbs * care of this, let Q_i(t) = { if (f_i(t) > 0) return 1; else return 0; }. 22063949bee8SArchie Cobbs * So the actual number of bytes that will have arrived at the peer after 22073949bee8SArchie Cobbs * t miliseconds is f_i(t) * Q_i(t). 22083949bee8SArchie Cobbs * 22093949bee8SArchie Cobbs * At any given time, each link has some additional latency a_i >= 0 22103949bee8SArchie Cobbs * due to previously written fragment(s) which are still in the queue. 22113949bee8SArchie Cobbs * This value is easily computed from the time since last transmission, 22123949bee8SArchie Cobbs * the previous latency value, the number of bytes written, and the 22133949bee8SArchie Cobbs * link's bandwidth. 22143949bee8SArchie Cobbs * 22153949bee8SArchie Cobbs * Assume that l_i includes any a_i already, and that the links are 22163949bee8SArchie Cobbs * sorted by latency, so that l_i <= l_{i+1}. 22173949bee8SArchie Cobbs * 22183949bee8SArchie Cobbs * Let N be the total number of bytes in the current frame we are sending. 22193949bee8SArchie Cobbs * 22203949bee8SArchie Cobbs * Suppose we were to start writing bytes at time t = 0 on all links 22213949bee8SArchie Cobbs * simultaneously, which is the most we can possibly do. Then let 22223949bee8SArchie Cobbs * F(t) be equal to the total number of bytes received by the peer 22233949bee8SArchie Cobbs * after t miliseconds. Then F(t) = Sum_i (f_i(t) * Q_i(t)). 22243949bee8SArchie Cobbs * 22253949bee8SArchie Cobbs * Our goal is simply this: fragment the frame across the links such 22263949bee8SArchie Cobbs * that the peer is able to reconstruct the completed frame as soon as 22273949bee8SArchie Cobbs * possible, i.e., at the least possible value of t. Call this value t_0. 22283949bee8SArchie Cobbs * 22293949bee8SArchie Cobbs * Then it follows that F(t_0) = N. Our strategy is first to find the value 22303949bee8SArchie Cobbs * of t_0, and then deduce how many bytes to write to each link. 22313949bee8SArchie Cobbs * 22323949bee8SArchie Cobbs * Rewriting F(t_0): 22333949bee8SArchie Cobbs * 22343949bee8SArchie Cobbs * t_0 = ( N + Sum_i ( l_i * b_i * Q_i(t_0) ) ) / Sum_i ( b_i * Q_i(t_0) ) 22353949bee8SArchie Cobbs * 22363949bee8SArchie Cobbs * Now, we note that Q_i(t) is constant for l_i <= t <= l_{i+1}. t_0 will 22373949bee8SArchie Cobbs * lie in one of these ranges. To find it, we just need to find the i such 22383949bee8SArchie Cobbs * that F(l_i) <= N <= F(l_{i+1}). Then we compute all the constant values 22393949bee8SArchie Cobbs * for Q_i() in this range, plug in the remaining values, solving for t_0. 22403949bee8SArchie Cobbs * 22413949bee8SArchie Cobbs * Once t_0 is known, then the number of bytes to send on link i is 22423949bee8SArchie Cobbs * just f_i(t_0) * Q_i(t_0). 22433949bee8SArchie Cobbs * 22443949bee8SArchie Cobbs * In other words, we start allocating bytes to the links one at a time. 22453949bee8SArchie Cobbs * We keep adding links until the frame is completely sent. Some links 22463949bee8SArchie Cobbs * may not get any bytes because their latency is too high. 22473949bee8SArchie Cobbs * 22483949bee8SArchie Cobbs * Is all this work really worth the trouble? Depends on the situation. 22493949bee8SArchie Cobbs * The bigger the ratio of computer speed to link speed, and the more 22503949bee8SArchie Cobbs * important total bundle latency is (e.g., for interactive response time), 22513949bee8SArchie Cobbs * the more it's worth it. There is however the cost of calling this 22523949bee8SArchie Cobbs * function for every frame. The running time is O(n^2) where n is the 22533949bee8SArchie Cobbs * number of links that receive a non-zero number of bytes. 22543949bee8SArchie Cobbs * 22553949bee8SArchie Cobbs * Since latency is measured in miliseconds, the "resolution" of this 22563949bee8SArchie Cobbs * algorithm is one milisecond. 22573949bee8SArchie Cobbs * 22583949bee8SArchie Cobbs * To avoid this algorithm altogether, configure all links to have the 22593949bee8SArchie Cobbs * same latency and bandwidth. 22603949bee8SArchie Cobbs */ 22613949bee8SArchie Cobbs static void 22623949bee8SArchie Cobbs ng_ppp_mp_strategy(node_p node, int len, int *distrib) 22633949bee8SArchie Cobbs { 226430400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 22653949bee8SArchie Cobbs int latency[NG_PPP_MAX_LINKS]; 22663949bee8SArchie Cobbs int sortByLatency[NG_PPP_MAX_LINKS]; 2267a9b3dca5SArchie Cobbs int activeLinkNum; 22683949bee8SArchie Cobbs int t0, total, topSum, botSum; 22693949bee8SArchie Cobbs struct timeval now; 22703949bee8SArchie Cobbs int i, numFragments; 22713949bee8SArchie Cobbs 22723949bee8SArchie Cobbs /* If only one link, this gets real easy */ 22733949bee8SArchie Cobbs if (priv->numActiveLinks == 1) { 22743949bee8SArchie Cobbs distrib[0] = len; 22753949bee8SArchie Cobbs return; 22763949bee8SArchie Cobbs } 22773949bee8SArchie Cobbs 22783949bee8SArchie Cobbs /* Get current time */ 2279a9b3dca5SArchie Cobbs getmicrouptime(&now); 22803949bee8SArchie Cobbs 22813949bee8SArchie Cobbs /* Compute latencies for each link at this point in time */ 22823949bee8SArchie Cobbs for (activeLinkNum = 0; 22833949bee8SArchie Cobbs activeLinkNum < priv->numActiveLinks; activeLinkNum++) { 2284a9b3dca5SArchie Cobbs struct ng_ppp_link *alink; 22853949bee8SArchie Cobbs struct timeval diff; 22863949bee8SArchie Cobbs int xmitBytes; 22873949bee8SArchie Cobbs 22883949bee8SArchie Cobbs /* Start with base latency value */ 2289a9b3dca5SArchie Cobbs alink = &priv->links[priv->activeLinks[activeLinkNum]]; 229049728ffcSBjoern A. Zeeb latency[activeLinkNum] = alink->latency; 22913949bee8SArchie Cobbs sortByLatency[activeLinkNum] = activeLinkNum; /* see below */ 22923949bee8SArchie Cobbs 22933949bee8SArchie Cobbs /* Any additional latency? */ 2294a9b3dca5SArchie Cobbs if (alink->bytesInQueue == 0) 22953949bee8SArchie Cobbs continue; 22963949bee8SArchie Cobbs 22973949bee8SArchie Cobbs /* Compute time delta since last write */ 22983949bee8SArchie Cobbs diff = now; 2299a9b3dca5SArchie Cobbs timevalsub(&diff, &alink->lastWrite); 2300fd58342cSAlexander Motin 2301fd58342cSAlexander Motin /* alink->bytesInQueue will be changed, mark change time. */ 2302fd58342cSAlexander Motin alink->lastWrite = now; 2303fd58342cSAlexander Motin 23043949bee8SArchie Cobbs if (now.tv_sec < 0 || diff.tv_sec >= 10) { /* sanity */ 2305a9b3dca5SArchie Cobbs alink->bytesInQueue = 0; 23063949bee8SArchie Cobbs continue; 23073949bee8SArchie Cobbs } 23083949bee8SArchie Cobbs 23093949bee8SArchie Cobbs /* How many bytes could have transmitted since last write? */ 2310fd58342cSAlexander Motin xmitBytes = (alink->conf.bandwidth * 10 * diff.tv_sec) 2311a9b3dca5SArchie Cobbs + (alink->conf.bandwidth * (diff.tv_usec / 1000)) / 100; 2312a9b3dca5SArchie Cobbs alink->bytesInQueue -= xmitBytes; 2313a9b3dca5SArchie Cobbs if (alink->bytesInQueue < 0) 2314a9b3dca5SArchie Cobbs alink->bytesInQueue = 0; 23153949bee8SArchie Cobbs else 23163949bee8SArchie Cobbs latency[activeLinkNum] += 2317a9b3dca5SArchie Cobbs (100 * alink->bytesInQueue) / alink->conf.bandwidth; 23183949bee8SArchie Cobbs } 23193949bee8SArchie Cobbs 2320a9b3dca5SArchie Cobbs /* Sort active links by latency */ 23218250de83SGleb Smirnoff qsort_r(sortByLatency, 23228250de83SGleb Smirnoff priv->numActiveLinks, sizeof(*sortByLatency), latency, ng_ppp_intcmp); 23233949bee8SArchie Cobbs 23243949bee8SArchie Cobbs /* Find the interval we need (add links in sortByLatency[] order) */ 23253949bee8SArchie Cobbs for (numFragments = 1; 23263949bee8SArchie Cobbs numFragments < priv->numActiveLinks; numFragments++) { 23273949bee8SArchie Cobbs for (total = i = 0; i < numFragments; i++) { 23283949bee8SArchie Cobbs int flowTime; 23293949bee8SArchie Cobbs 23303949bee8SArchie Cobbs flowTime = latency[sortByLatency[numFragments]] 23313949bee8SArchie Cobbs - latency[sortByLatency[i]]; 2332a9b3dca5SArchie Cobbs total += ((flowTime * priv->links[ 2333a9b3dca5SArchie Cobbs priv->activeLinks[sortByLatency[i]]].conf.bandwidth) 23343949bee8SArchie Cobbs + 99) / 100; 23353949bee8SArchie Cobbs } 23363949bee8SArchie Cobbs if (total >= len) 23373949bee8SArchie Cobbs break; 23383949bee8SArchie Cobbs } 23393949bee8SArchie Cobbs 23403949bee8SArchie Cobbs /* Solve for t_0 in that interval */ 23413949bee8SArchie Cobbs for (topSum = botSum = i = 0; i < numFragments; i++) { 2342a9b3dca5SArchie Cobbs int bw = priv->links[ 2343a9b3dca5SArchie Cobbs priv->activeLinks[sortByLatency[i]]].conf.bandwidth; 23443949bee8SArchie Cobbs 23453949bee8SArchie Cobbs topSum += latency[sortByLatency[i]] * bw; /* / 100 */ 23463949bee8SArchie Cobbs botSum += bw; /* / 100 */ 23473949bee8SArchie Cobbs } 23483949bee8SArchie Cobbs t0 = ((len * 100) + topSum + botSum / 2) / botSum; 23493949bee8SArchie Cobbs 23503949bee8SArchie Cobbs /* Compute f_i(t_0) all i */ 23513949bee8SArchie Cobbs for (total = i = 0; i < numFragments; i++) { 2352a9b3dca5SArchie Cobbs int bw = priv->links[ 2353a9b3dca5SArchie Cobbs priv->activeLinks[sortByLatency[i]]].conf.bandwidth; 23543949bee8SArchie Cobbs 23553949bee8SArchie Cobbs distrib[sortByLatency[i]] = 23563949bee8SArchie Cobbs (bw * (t0 - latency[sortByLatency[i]]) + 50) / 100; 23573949bee8SArchie Cobbs total += distrib[sortByLatency[i]]; 23583949bee8SArchie Cobbs } 23593949bee8SArchie Cobbs 2360fb1fc8abSArchie Cobbs /* Deal with any rounding error */ 2361fb1fc8abSArchie Cobbs if (total < len) { 2362a9b3dca5SArchie Cobbs struct ng_ppp_link *fastLink = 2363a9b3dca5SArchie Cobbs &priv->links[priv->activeLinks[sortByLatency[0]]]; 23643949bee8SArchie Cobbs int fast = 0; 23653949bee8SArchie Cobbs 2366fb1fc8abSArchie Cobbs /* Find the fastest link */ 23673949bee8SArchie Cobbs for (i = 1; i < numFragments; i++) { 2368a9b3dca5SArchie Cobbs struct ng_ppp_link *const link = 2369a9b3dca5SArchie Cobbs &priv->links[priv->activeLinks[sortByLatency[i]]]; 2370a9b3dca5SArchie Cobbs 2371a9b3dca5SArchie Cobbs if (link->conf.bandwidth > fastLink->conf.bandwidth) { 23723949bee8SArchie Cobbs fast = i; 2373a9b3dca5SArchie Cobbs fastLink = link; 2374a9b3dca5SArchie Cobbs } 23753949bee8SArchie Cobbs } 23763949bee8SArchie Cobbs distrib[sortByLatency[fast]] += len - total; 2377fb1fc8abSArchie Cobbs } else while (total > len) { 2378a9b3dca5SArchie Cobbs struct ng_ppp_link *slowLink = 2379a9b3dca5SArchie Cobbs &priv->links[priv->activeLinks[sortByLatency[0]]]; 2380fb1fc8abSArchie Cobbs int delta, slow = 0; 23813949bee8SArchie Cobbs 2382fb1fc8abSArchie Cobbs /* Find the slowest link that still has bytes to remove */ 2383fb1fc8abSArchie Cobbs for (i = 1; i < numFragments; i++) { 2384a9b3dca5SArchie Cobbs struct ng_ppp_link *const link = 2385a9b3dca5SArchie Cobbs &priv->links[priv->activeLinks[sortByLatency[i]]]; 2386a9b3dca5SArchie Cobbs 2387b369bd54SGleb Smirnoff if (distrib[sortByLatency[slow]] == 0 || 2388b369bd54SGleb Smirnoff (distrib[sortByLatency[i]] > 0 && 2389b369bd54SGleb Smirnoff link->conf.bandwidth < slowLink->conf.bandwidth)) { 2390fb1fc8abSArchie Cobbs slow = i; 2391a9b3dca5SArchie Cobbs slowLink = link; 2392a9b3dca5SArchie Cobbs } 2393fb1fc8abSArchie Cobbs } 2394fb1fc8abSArchie Cobbs delta = total - len; 2395fb1fc8abSArchie Cobbs if (delta > distrib[sortByLatency[slow]]) 2396fb1fc8abSArchie Cobbs delta = distrib[sortByLatency[slow]]; 2397fb1fc8abSArchie Cobbs distrib[sortByLatency[slow]] -= delta; 2398fb1fc8abSArchie Cobbs total -= delta; 23993949bee8SArchie Cobbs } 24003949bee8SArchie Cobbs } 24013949bee8SArchie Cobbs 24023949bee8SArchie Cobbs /* 24033949bee8SArchie Cobbs * Compare two integers 24043949bee8SArchie Cobbs */ 24053949bee8SArchie Cobbs static int 24068250de83SGleb Smirnoff ng_ppp_intcmp(void *latency, const void *v1, const void *v2) 24073949bee8SArchie Cobbs { 24083949bee8SArchie Cobbs const int index1 = *((const int *) v1); 24093949bee8SArchie Cobbs const int index2 = *((const int *) v2); 24103949bee8SArchie Cobbs 24118250de83SGleb Smirnoff return ((int *)latency)[index1] - ((int *)latency)[index2]; 24123949bee8SArchie Cobbs } 24133949bee8SArchie Cobbs 24143949bee8SArchie Cobbs /* 24153949bee8SArchie Cobbs * Prepend a possibly compressed PPP protocol number in front of a frame 24163949bee8SArchie Cobbs */ 24173949bee8SArchie Cobbs static struct mbuf * 2418164b576eSGleb Smirnoff ng_ppp_addproto(struct mbuf *m, uint16_t proto, int compOK) 24193949bee8SArchie Cobbs { 2420d690a6e7SArchie Cobbs if (compOK && PROT_COMPRESSABLE(proto)) { 2421164b576eSGleb Smirnoff uint8_t pbyte = (uint8_t)proto; 24223949bee8SArchie Cobbs 2423d690a6e7SArchie Cobbs return ng_ppp_prepend(m, &pbyte, 1); 2424d690a6e7SArchie Cobbs } else { 2425164b576eSGleb Smirnoff uint16_t pword = htons((uint16_t)proto); 2426d690a6e7SArchie Cobbs 2427d690a6e7SArchie Cobbs return ng_ppp_prepend(m, &pword, 2); 2428d690a6e7SArchie Cobbs } 2429d690a6e7SArchie Cobbs } 2430d690a6e7SArchie Cobbs 2431d690a6e7SArchie Cobbs /* 2432164b576eSGleb Smirnoff * Cut a possibly compressed PPP protocol number from the front of a frame. 2433164b576eSGleb Smirnoff */ 2434164b576eSGleb Smirnoff static struct mbuf * 2435164b576eSGleb Smirnoff ng_ppp_cutproto(struct mbuf *m, uint16_t *proto) 2436164b576eSGleb Smirnoff { 2437164b576eSGleb Smirnoff 2438164b576eSGleb Smirnoff *proto = 0; 2439164b576eSGleb Smirnoff if (m->m_len < 1 && (m = m_pullup(m, 1)) == NULL) 2440164b576eSGleb Smirnoff return (NULL); 2441164b576eSGleb Smirnoff 2442164b576eSGleb Smirnoff *proto = *mtod(m, uint8_t *); 2443164b576eSGleb Smirnoff m_adj(m, 1); 2444164b576eSGleb Smirnoff 2445164b576eSGleb Smirnoff if (!PROT_VALID(*proto)) { 2446164b576eSGleb Smirnoff if (m->m_len < 1 && (m = m_pullup(m, 1)) == NULL) 2447164b576eSGleb Smirnoff return (NULL); 2448164b576eSGleb Smirnoff 2449164b576eSGleb Smirnoff *proto = (*proto << 8) + *mtod(m, uint8_t *); 2450164b576eSGleb Smirnoff m_adj(m, 1); 2451164b576eSGleb Smirnoff } 2452164b576eSGleb Smirnoff 2453164b576eSGleb Smirnoff return (m); 2454164b576eSGleb Smirnoff } 2455164b576eSGleb Smirnoff 2456164b576eSGleb Smirnoff /* 2457164b576eSGleb Smirnoff * Prepend some bytes to an mbuf. 2458d690a6e7SArchie Cobbs */ 2459d690a6e7SArchie Cobbs static struct mbuf * 2460d690a6e7SArchie Cobbs ng_ppp_prepend(struct mbuf *m, const void *buf, int len) 2461d690a6e7SArchie Cobbs { 2462eb1b1807SGleb Smirnoff M_PREPEND(m, len, M_NOWAIT); 2463d690a6e7SArchie Cobbs if (m == NULL || (m->m_len < len && (m = m_pullup(m, len)) == NULL)) 24644cf49a43SJulian Elischer return (NULL); 2465164b576eSGleb Smirnoff bcopy(buf, mtod(m, uint8_t *), len); 24663949bee8SArchie Cobbs return (m); 24673949bee8SArchie Cobbs } 24683949bee8SArchie Cobbs 24693949bee8SArchie Cobbs /* 24703949bee8SArchie Cobbs * Update private information that is derived from other private information 24713949bee8SArchie Cobbs */ 24723949bee8SArchie Cobbs static void 24733949bee8SArchie Cobbs ng_ppp_update(node_p node, int newConf) 24743949bee8SArchie Cobbs { 247530400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 24763949bee8SArchie Cobbs int i; 24773949bee8SArchie Cobbs 24783949bee8SArchie Cobbs /* Update active status for VJ Compression */ 24793949bee8SArchie Cobbs priv->vjCompHooked = priv->hooks[HOOK_INDEX_VJC_IP] != NULL 24803949bee8SArchie Cobbs && priv->hooks[HOOK_INDEX_VJC_COMP] != NULL 24813949bee8SArchie Cobbs && priv->hooks[HOOK_INDEX_VJC_UNCOMP] != NULL 24823949bee8SArchie Cobbs && priv->hooks[HOOK_INDEX_VJC_VJIP] != NULL; 24833949bee8SArchie Cobbs 24843949bee8SArchie Cobbs /* Increase latency for each link an amount equal to one MP header */ 24853949bee8SArchie Cobbs if (newConf) { 24863949bee8SArchie Cobbs for (i = 0; i < NG_PPP_MAX_LINKS; i++) { 24873949bee8SArchie Cobbs int hdrBytes; 24883949bee8SArchie Cobbs 2489fd58342cSAlexander Motin if (priv->links[i].conf.bandwidth == 0) 2490fd58342cSAlexander Motin continue; 2491fd58342cSAlexander Motin 2492fd58342cSAlexander Motin hdrBytes = MP_AVERAGE_LINK_OVERHEAD 2493fd58342cSAlexander Motin + (priv->links[i].conf.enableACFComp ? 0 : 2) 2494a9b3dca5SArchie Cobbs + (priv->links[i].conf.enableProtoComp ? 1 : 2) 24953949bee8SArchie Cobbs + (priv->conf.xmitShortSeq ? 2 : 4); 249649728ffcSBjoern A. Zeeb priv->links[i].latency = 249749728ffcSBjoern A. Zeeb priv->links[i].conf.latency + 2498fd58342cSAlexander Motin (hdrBytes / priv->links[i].conf.bandwidth + 50) / 100; 24993949bee8SArchie Cobbs } 25003949bee8SArchie Cobbs } 25013949bee8SArchie Cobbs 25023949bee8SArchie Cobbs /* Update list of active links */ 25033949bee8SArchie Cobbs bzero(&priv->activeLinks, sizeof(priv->activeLinks)); 25043949bee8SArchie Cobbs priv->numActiveLinks = 0; 25053949bee8SArchie Cobbs priv->allLinksEqual = 1; 25063949bee8SArchie Cobbs for (i = 0; i < NG_PPP_MAX_LINKS; i++) { 2507a9b3dca5SArchie Cobbs struct ng_ppp_link *const link = &priv->links[i]; 25080e11d0f3SArchie Cobbs 2509a9b3dca5SArchie Cobbs /* Is link active? */ 2510a9b3dca5SArchie Cobbs if (link->conf.enableLink && link->hook != NULL) { 2511a9b3dca5SArchie Cobbs struct ng_ppp_link *link0; 2512a9b3dca5SArchie Cobbs 2513a9b3dca5SArchie Cobbs /* Add link to list of active links */ 25143949bee8SArchie Cobbs priv->activeLinks[priv->numActiveLinks++] = i; 2515a9b3dca5SArchie Cobbs link0 = &priv->links[priv->activeLinks[0]]; 2516a9b3dca5SArchie Cobbs 2517a9b3dca5SArchie Cobbs /* Determine if all links are still equal */ 251849728ffcSBjoern A. Zeeb if (link->latency != link0->latency 2519a9b3dca5SArchie Cobbs || link->conf.bandwidth != link0->conf.bandwidth) 25203949bee8SArchie Cobbs priv->allLinksEqual = 0; 2521a9b3dca5SArchie Cobbs 2522a9b3dca5SArchie Cobbs /* Initialize rec'd sequence number */ 2523a9b3dca5SArchie Cobbs if (link->seq == MP_NOSEQ) { 2524a9b3dca5SArchie Cobbs link->seq = (link == link0) ? 2525a9b3dca5SArchie Cobbs MP_INITIAL_SEQ : link0->seq; 25263949bee8SArchie Cobbs } 2527a9b3dca5SArchie Cobbs } else 2528a9b3dca5SArchie Cobbs link->seq = MP_NOSEQ; 25293949bee8SArchie Cobbs } 25303949bee8SArchie Cobbs 2531a9b3dca5SArchie Cobbs /* Update MP state as multi-link is active or not */ 2532a9b3dca5SArchie Cobbs if (priv->conf.enableMultilink && priv->numActiveLinks > 0) 2533a9b3dca5SArchie Cobbs ng_ppp_start_frag_timer(node); 2534a9b3dca5SArchie Cobbs else { 2535a9b3dca5SArchie Cobbs ng_ppp_stop_frag_timer(node); 2536a9b3dca5SArchie Cobbs ng_ppp_frag_reset(node); 2537a9b3dca5SArchie Cobbs priv->xseq = MP_INITIAL_SEQ; 2538a9b3dca5SArchie Cobbs priv->mseq = MP_INITIAL_SEQ; 2539a9b3dca5SArchie Cobbs for (i = 0; i < NG_PPP_MAX_LINKS; i++) { 2540a9b3dca5SArchie Cobbs struct ng_ppp_link *const link = &priv->links[i]; 2541a9b3dca5SArchie Cobbs 2542a9b3dca5SArchie Cobbs bzero(&link->lastWrite, sizeof(link->lastWrite)); 2543a9b3dca5SArchie Cobbs link->bytesInQueue = 0; 2544a9b3dca5SArchie Cobbs link->seq = MP_NOSEQ; 2545a9b3dca5SArchie Cobbs } 25463949bee8SArchie Cobbs } 254735853c2cSGleb Smirnoff 254835853c2cSGleb Smirnoff if (priv->hooks[HOOK_INDEX_INET] != NULL) { 254935853c2cSGleb Smirnoff if (priv->conf.enableIP == 1 && 255035853c2cSGleb Smirnoff priv->numActiveLinks == 1 && 255135853c2cSGleb Smirnoff priv->conf.enableMultilink == 0 && 255235853c2cSGleb Smirnoff priv->conf.enableCompression == 0 && 255335853c2cSGleb Smirnoff priv->conf.enableEncryption == 0 && 255435853c2cSGleb Smirnoff priv->conf.enableVJCompression == 0) 255535853c2cSGleb Smirnoff NG_HOOK_SET_RCVDATA(priv->hooks[HOOK_INDEX_INET], 255635853c2cSGleb Smirnoff ng_ppp_rcvdata_inet_fast); 255735853c2cSGleb Smirnoff else 255835853c2cSGleb Smirnoff NG_HOOK_SET_RCVDATA(priv->hooks[HOOK_INDEX_INET], 255935853c2cSGleb Smirnoff ng_ppp_rcvdata_inet); 256035853c2cSGleb Smirnoff } 25613949bee8SArchie Cobbs } 25623949bee8SArchie Cobbs 25633949bee8SArchie Cobbs /* 25643949bee8SArchie Cobbs * Determine if a new configuration would represent a valid change 25653949bee8SArchie Cobbs * from the current configuration and link activity status. 25663949bee8SArchie Cobbs */ 25673949bee8SArchie Cobbs static int 2568a9b3dca5SArchie Cobbs ng_ppp_config_valid(node_p node, const struct ng_ppp_node_conf *newConf) 25693949bee8SArchie Cobbs { 257030400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 25713949bee8SArchie Cobbs int i, newNumLinksActive; 25723949bee8SArchie Cobbs 25733949bee8SArchie Cobbs /* Check per-link config and count how many links would be active */ 25743949bee8SArchie Cobbs for (newNumLinksActive = i = 0; i < NG_PPP_MAX_LINKS; i++) { 2575a9b3dca5SArchie Cobbs if (newConf->links[i].enableLink && priv->links[i].hook != NULL) 2576e149c4e2SArchie Cobbs newNumLinksActive++; 2577e149c4e2SArchie Cobbs if (!newConf->links[i].enableLink) 2578e149c4e2SArchie Cobbs continue; 25793949bee8SArchie Cobbs if (newConf->links[i].mru < MP_MIN_LINK_MRU) 25803949bee8SArchie Cobbs return (0); 25813949bee8SArchie Cobbs if (newConf->links[i].bandwidth == 0) 25823949bee8SArchie Cobbs return (0); 25833949bee8SArchie Cobbs if (newConf->links[i].bandwidth > NG_PPP_MAX_BANDWIDTH) 25843949bee8SArchie Cobbs return (0); 25853949bee8SArchie Cobbs if (newConf->links[i].latency > NG_PPP_MAX_LATENCY) 25863949bee8SArchie Cobbs return (0); 25873949bee8SArchie Cobbs } 25883949bee8SArchie Cobbs 25893949bee8SArchie Cobbs /* Disallow changes to multi-link configuration while MP is active */ 25903949bee8SArchie Cobbs if (priv->numActiveLinks > 0 && newNumLinksActive > 0) { 2591a9b3dca5SArchie Cobbs if (!priv->conf.enableMultilink 2592a9b3dca5SArchie Cobbs != !newConf->bund.enableMultilink 2593a9b3dca5SArchie Cobbs || !priv->conf.xmitShortSeq != !newConf->bund.xmitShortSeq 2594a9b3dca5SArchie Cobbs || !priv->conf.recvShortSeq != !newConf->bund.recvShortSeq) 25953949bee8SArchie Cobbs return (0); 25963949bee8SArchie Cobbs } 25973949bee8SArchie Cobbs 2598e149c4e2SArchie Cobbs /* At most one link can be active unless multi-link is enabled */ 2599a9b3dca5SArchie Cobbs if (!newConf->bund.enableMultilink && newNumLinksActive > 1) 2600e149c4e2SArchie Cobbs return (0); 2601e149c4e2SArchie Cobbs 2602e149c4e2SArchie Cobbs /* Configuration change would be valid */ 26033949bee8SArchie Cobbs return (1); 26043949bee8SArchie Cobbs } 26053949bee8SArchie Cobbs 26063949bee8SArchie Cobbs /* 26073949bee8SArchie Cobbs * Free all entries in the fragment queue 26083949bee8SArchie Cobbs */ 26093949bee8SArchie Cobbs static void 2610a9b3dca5SArchie Cobbs ng_ppp_frag_reset(node_p node) 26113949bee8SArchie Cobbs { 261230400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 26131e7a9f72SArchie Cobbs struct ng_ppp_frag *qent, *qnext; 26143949bee8SArchie Cobbs 26155f90cac7SKirk McKusick for (qent = TAILQ_FIRST(&priv->frags); qent; qent = qnext) { 26165f90cac7SKirk McKusick qnext = TAILQ_NEXT(qent, f_qent); 2617069154d5SJulian Elischer NG_FREE_M(qent->data); 261893caf2e2SAlexander Motin TAILQ_INSERT_HEAD(&priv->fragsfree, qent, f_qent); 26193949bee8SArchie Cobbs } 26205f90cac7SKirk McKusick TAILQ_INIT(&priv->frags); 2621a9b3dca5SArchie Cobbs } 2622a9b3dca5SArchie Cobbs 2623a9b3dca5SArchie Cobbs /* 2624a9b3dca5SArchie Cobbs * Start fragment queue timer 2625a9b3dca5SArchie Cobbs */ 2626a9b3dca5SArchie Cobbs static void 2627a9b3dca5SArchie Cobbs ng_ppp_start_frag_timer(node_p node) 2628a9b3dca5SArchie Cobbs { 262930400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 2630a9b3dca5SArchie Cobbs 2631dc2f4d7fSGleb Smirnoff if (!(callout_pending(&priv->fragTimer))) 26329d952bd3SGleb Smirnoff ng_callout(&priv->fragTimer, node, NULL, MP_FRAGTIMER_INTERVAL, 26339d952bd3SGleb Smirnoff ng_ppp_frag_timeout, NULL, 0); 2634a9b3dca5SArchie Cobbs } 2635a9b3dca5SArchie Cobbs 2636a9b3dca5SArchie Cobbs /* 2637a9b3dca5SArchie Cobbs * Stop fragment queue timer 2638a9b3dca5SArchie Cobbs */ 2639a9b3dca5SArchie Cobbs static void 2640a9b3dca5SArchie Cobbs ng_ppp_stop_frag_timer(node_p node) 2641a9b3dca5SArchie Cobbs { 264230400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 2643a9b3dca5SArchie Cobbs 2644dc2f4d7fSGleb Smirnoff if (callout_pending(&priv->fragTimer)) 26459d952bd3SGleb Smirnoff ng_uncallout(&priv->fragTimer, node); 2646a9b3dca5SArchie Cobbs } 2647