14cf49a43SJulian Elischer 24cf49a43SJulian Elischer /* 34cf49a43SJulian Elischer * ng_ppp.c 44cf49a43SJulian Elischer * 5a9b3dca5SArchie Cobbs * Copyright (c) 1996-2000 Whistle Communications, Inc. 64cf49a43SJulian Elischer * All rights reserved. 74cf49a43SJulian Elischer * 84cf49a43SJulian Elischer * Subject to the following obligations and disclaimer of warranty, use and 94cf49a43SJulian Elischer * redistribution of this software, in source or object code forms, with or 104cf49a43SJulian Elischer * without modifications are expressly permitted by Whistle Communications; 114cf49a43SJulian Elischer * provided, however, that: 124cf49a43SJulian Elischer * 1. Any and all reproductions of the source or object code must include the 134cf49a43SJulian Elischer * copyright notice above and the following disclaimer of warranties; and 144cf49a43SJulian Elischer * 2. No rights are granted, in any manner or form, to use Whistle 154cf49a43SJulian Elischer * Communications, Inc. trademarks, including the mark "WHISTLE 164cf49a43SJulian Elischer * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 174cf49a43SJulian Elischer * such appears in the above copyright notice or in the software. 184cf49a43SJulian Elischer * 194cf49a43SJulian Elischer * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 204cf49a43SJulian Elischer * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 214cf49a43SJulian Elischer * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 224cf49a43SJulian Elischer * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 234cf49a43SJulian Elischer * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 244cf49a43SJulian Elischer * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 254cf49a43SJulian Elischer * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 264cf49a43SJulian Elischer * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 274cf49a43SJulian Elischer * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 284cf49a43SJulian Elischer * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 294cf49a43SJulian Elischer * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 304cf49a43SJulian Elischer * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 314cf49a43SJulian Elischer * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 324cf49a43SJulian Elischer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 334cf49a43SJulian Elischer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 344cf49a43SJulian Elischer * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 354cf49a43SJulian Elischer * OF SUCH DAMAGE. 364cf49a43SJulian Elischer * 376f16db81SArchie Cobbs * Author: Archie Cobbs <archie@freebsd.org> 384cf49a43SJulian Elischer * 394cf49a43SJulian Elischer * $FreeBSD$ 4074f5c6aaSJulian Elischer * $Whistle: ng_ppp.c,v 1.24 1999/11/01 09:24:52 julian Exp $ 414cf49a43SJulian Elischer */ 424cf49a43SJulian Elischer 434cf49a43SJulian Elischer /* 443949bee8SArchie Cobbs * PPP node type. 454cf49a43SJulian Elischer */ 464cf49a43SJulian Elischer 474cf49a43SJulian Elischer #include <sys/param.h> 484cf49a43SJulian Elischer #include <sys/systm.h> 494cf49a43SJulian Elischer #include <sys/kernel.h> 50a9b3dca5SArchie Cobbs #include <sys/time.h> 514cf49a43SJulian Elischer #include <sys/mbuf.h> 524cf49a43SJulian Elischer #include <sys/malloc.h> 534cf49a43SJulian Elischer #include <sys/errno.h> 545b664c7cSPoul-Henning Kamp #include <sys/ctype.h> 554cf49a43SJulian Elischer 56a9b3dca5SArchie Cobbs #include <machine/limits.h> 57a9b3dca5SArchie Cobbs 584cf49a43SJulian Elischer #include <netgraph/ng_message.h> 594cf49a43SJulian Elischer #include <netgraph/netgraph.h> 60f8307e12SArchie Cobbs #include <netgraph/ng_parse.h> 614cf49a43SJulian Elischer #include <netgraph/ng_ppp.h> 623949bee8SArchie Cobbs #include <netgraph/ng_vjc.h> 634cf49a43SJulian Elischer 644cf49a43SJulian Elischer #define PROT_VALID(p) (((p) & 0x0101) == 0x0001) 652b70adcbSArchie Cobbs #define PROT_COMPRESSABLE(p) (((p) & 0xff00) == 0x0000) 664cf49a43SJulian Elischer 673949bee8SArchie Cobbs /* Some PPP protocol numbers we're interested in */ 683949bee8SArchie Cobbs #define PROT_APPLETALK 0x0029 693949bee8SArchie Cobbs #define PROT_COMPD 0x00fd 703949bee8SArchie Cobbs #define PROT_CRYPTD 0x0053 713949bee8SArchie Cobbs #define PROT_IP 0x0021 72a9b3dca5SArchie Cobbs #define PROT_IPV6 0x0057 732b70adcbSArchie Cobbs #define PROT_IPX 0x002b 74d690a6e7SArchie Cobbs #define PROT_LCP 0xc021 753949bee8SArchie Cobbs #define PROT_MP 0x003d 763949bee8SArchie Cobbs #define PROT_VJCOMP 0x002d 773949bee8SArchie Cobbs #define PROT_VJUNCOMP 0x002f 783949bee8SArchie Cobbs 793949bee8SArchie Cobbs /* Multilink PPP definitions */ 803949bee8SArchie Cobbs #define MP_MIN_MRRU 1500 /* per RFC 1990 */ 813949bee8SArchie Cobbs #define MP_INITIAL_SEQ 0 /* per RFC 1990 */ 823949bee8SArchie Cobbs #define MP_MIN_LINK_MRU 32 833949bee8SArchie Cobbs 843949bee8SArchie Cobbs #define MP_SHORT_SEQ_MASK 0x00000fff /* short seq # mask */ 853949bee8SArchie Cobbs #define MP_SHORT_SEQ_HIBIT 0x00000800 /* short seq # high bit */ 863949bee8SArchie Cobbs #define MP_SHORT_FIRST_FLAG 0x00008000 /* first fragment in frame */ 873949bee8SArchie Cobbs #define MP_SHORT_LAST_FLAG 0x00004000 /* last fragment in frame */ 883949bee8SArchie Cobbs 893949bee8SArchie Cobbs #define MP_LONG_SEQ_MASK 0x00ffffff /* long seq # mask */ 903949bee8SArchie Cobbs #define MP_LONG_SEQ_HIBIT 0x00800000 /* long seq # high bit */ 913949bee8SArchie Cobbs #define MP_LONG_FIRST_FLAG 0x80000000 /* first fragment in frame */ 923949bee8SArchie Cobbs #define MP_LONG_LAST_FLAG 0x40000000 /* last fragment in frame */ 933949bee8SArchie Cobbs 946f16db81SArchie Cobbs #define MP_NOSEQ 0x7fffffff /* impossible sequence number */ 953949bee8SArchie Cobbs 963949bee8SArchie Cobbs /* Sign extension of MP sequence numbers */ 973949bee8SArchie Cobbs #define MP_SHORT_EXTEND(s) (((s) & MP_SHORT_SEQ_HIBIT) ? \ 986f16db81SArchie Cobbs ((s) | ~MP_SHORT_SEQ_MASK) \ 996f16db81SArchie Cobbs : ((s) & MP_SHORT_SEQ_MASK)) 1003949bee8SArchie Cobbs #define MP_LONG_EXTEND(s) (((s) & MP_LONG_SEQ_HIBIT) ? \ 1016f16db81SArchie Cobbs ((s) | ~MP_LONG_SEQ_MASK) \ 1026f16db81SArchie Cobbs : ((s) & MP_LONG_SEQ_MASK)) 1033949bee8SArchie Cobbs 1046f16db81SArchie Cobbs /* Comparision of MP sequence numbers. Note: all sequence numbers 1056f16db81SArchie Cobbs except priv->xseq are stored with the sign bit extended. */ 1066f16db81SArchie Cobbs #define MP_SHORT_SEQ_DIFF(x,y) MP_SHORT_EXTEND((x) - (y)) 1076f16db81SArchie Cobbs #define MP_LONG_SEQ_DIFF(x,y) MP_LONG_EXTEND((x) - (y)) 1083949bee8SArchie Cobbs 1096f16db81SArchie Cobbs #define MP_RECV_SEQ_DIFF(priv,x,y) \ 1106f16db81SArchie Cobbs ((priv)->conf.recvShortSeq ? \ 1113949bee8SArchie Cobbs MP_SHORT_SEQ_DIFF((x), (y)) : \ 1123949bee8SArchie Cobbs MP_LONG_SEQ_DIFF((x), (y))) 1133949bee8SArchie Cobbs 1146f16db81SArchie Cobbs /* Increment receive sequence number */ 1156f16db81SArchie Cobbs #define MP_NEXT_RECV_SEQ(priv,seq) \ 1166f16db81SArchie Cobbs (((seq) + 1) & ((priv)->conf.recvShortSeq ? \ 1176f16db81SArchie Cobbs MP_SHORT_SEQ_MASK : MP_LONG_SEQ_MASK)) 118a9b3dca5SArchie Cobbs 119a9b3dca5SArchie Cobbs /* Don't fragment transmitted packets smaller than this */ 120a9b3dca5SArchie Cobbs #define MP_MIN_FRAG_LEN 6 121a9b3dca5SArchie Cobbs 122a9b3dca5SArchie Cobbs /* Maximum fragment reasssembly queue length */ 123a9b3dca5SArchie Cobbs #define MP_MAX_QUEUE_LEN 128 124a9b3dca5SArchie Cobbs 125a9b3dca5SArchie Cobbs /* Fragment queue scanner period */ 126a9b3dca5SArchie Cobbs #define MP_FRAGTIMER_INTERVAL (hz/2) 127a9b3dca5SArchie Cobbs 1283949bee8SArchie Cobbs /* We store incoming fragments this way */ 1293949bee8SArchie Cobbs struct ng_ppp_frag { 130a9b3dca5SArchie Cobbs int seq; /* fragment seq# */ 131a9b3dca5SArchie Cobbs u_char first; /* First in packet? */ 132a9b3dca5SArchie Cobbs u_char last; /* Last in packet? */ 133a9b3dca5SArchie Cobbs struct timeval timestamp; /* time of reception */ 134a9b3dca5SArchie Cobbs struct mbuf *data; /* Fragment data */ 135a9b3dca5SArchie Cobbs meta_p meta; /* Fragment meta */ 136e3975643SJake Burkholder CIRCLEQ_ENTRY(ng_ppp_frag) f_qent; /* Fragment queue */ 1373949bee8SArchie Cobbs }; 1383949bee8SArchie Cobbs 1393949bee8SArchie Cobbs /* We use integer indicies to refer to the non-link hooks */ 1403949bee8SArchie Cobbs static const char *const ng_ppp_hook_names[] = { 1413949bee8SArchie Cobbs NG_PPP_HOOK_ATALK, 1423949bee8SArchie Cobbs #define HOOK_INDEX_ATALK 0 1433949bee8SArchie Cobbs NG_PPP_HOOK_BYPASS, 1443949bee8SArchie Cobbs #define HOOK_INDEX_BYPASS 1 1453949bee8SArchie Cobbs NG_PPP_HOOK_COMPRESS, 1463949bee8SArchie Cobbs #define HOOK_INDEX_COMPRESS 2 1473949bee8SArchie Cobbs NG_PPP_HOOK_ENCRYPT, 1483949bee8SArchie Cobbs #define HOOK_INDEX_ENCRYPT 3 1493949bee8SArchie Cobbs NG_PPP_HOOK_DECOMPRESS, 1503949bee8SArchie Cobbs #define HOOK_INDEX_DECOMPRESS 4 1513949bee8SArchie Cobbs NG_PPP_HOOK_DECRYPT, 1523949bee8SArchie Cobbs #define HOOK_INDEX_DECRYPT 5 1533949bee8SArchie Cobbs NG_PPP_HOOK_INET, 1543949bee8SArchie Cobbs #define HOOK_INDEX_INET 6 1553949bee8SArchie Cobbs NG_PPP_HOOK_IPX, 1563949bee8SArchie Cobbs #define HOOK_INDEX_IPX 7 1573949bee8SArchie Cobbs NG_PPP_HOOK_VJC_COMP, 1583949bee8SArchie Cobbs #define HOOK_INDEX_VJC_COMP 8 1593949bee8SArchie Cobbs NG_PPP_HOOK_VJC_IP, 1603949bee8SArchie Cobbs #define HOOK_INDEX_VJC_IP 9 1613949bee8SArchie Cobbs NG_PPP_HOOK_VJC_UNCOMP, 1623949bee8SArchie Cobbs #define HOOK_INDEX_VJC_UNCOMP 10 1633949bee8SArchie Cobbs NG_PPP_HOOK_VJC_VJIP, 1643949bee8SArchie Cobbs #define HOOK_INDEX_VJC_VJIP 11 165a9b3dca5SArchie Cobbs NG_PPP_HOOK_IPV6, 166a9b3dca5SArchie Cobbs #define HOOK_INDEX_IPV6 12 1673949bee8SArchie Cobbs NULL 168a9b3dca5SArchie Cobbs #define HOOK_INDEX_MAX 13 1693949bee8SArchie Cobbs }; 1703949bee8SArchie Cobbs 1713949bee8SArchie Cobbs /* We store index numbers in the hook private pointer. The HOOK_INDEX() 1723949bee8SArchie Cobbs for a hook is either the index (above) for normal hooks, or the ones 1733949bee8SArchie Cobbs complement of the link number for link hooks. */ 1743949bee8SArchie Cobbs #define HOOK_INDEX(hook) (*((int16_t *) &(hook)->private)) 1754cf49a43SJulian Elischer 176a9b3dca5SArchie Cobbs /* Per-link private information */ 177a9b3dca5SArchie Cobbs struct ng_ppp_link { 178a9b3dca5SArchie Cobbs struct ng_ppp_link_conf conf; /* link configuration */ 179a9b3dca5SArchie Cobbs hook_p hook; /* connection to link data */ 1806f16db81SArchie Cobbs int32_t seq; /* highest rec'd seq# - MSEQ */ 181a9b3dca5SArchie Cobbs struct timeval lastWrite; /* time of last write */ 182a9b3dca5SArchie Cobbs int bytesInQueue; /* bytes in the output queue */ 183a9b3dca5SArchie Cobbs struct ng_ppp_link_stat stats; /* Link stats */ 184a9b3dca5SArchie Cobbs }; 185a9b3dca5SArchie Cobbs 186a9b3dca5SArchie Cobbs /* Total per-node private information */ 18762838faeSArchie Cobbs struct ng_ppp_private { 188a9b3dca5SArchie Cobbs struct ng_ppp_bund_conf conf; /* bundle config */ 189a9b3dca5SArchie Cobbs struct ng_ppp_link_stat bundleStats; /* bundle stats */ 190a9b3dca5SArchie Cobbs struct ng_ppp_link links[NG_PPP_MAX_LINKS];/* per-link info */ 1916f16db81SArchie Cobbs int32_t xseq; /* next out MP seq # */ 1926f16db81SArchie Cobbs int32_t mseq; /* min links[i].seq */ 193a9b3dca5SArchie Cobbs u_char vjCompHooked; /* VJ comp hooked up? */ 194a9b3dca5SArchie Cobbs u_char allLinksEqual; /* all xmit the same? */ 195a9b3dca5SArchie Cobbs u_char timerActive; /* frag timer active? */ 196a9b3dca5SArchie Cobbs u_int numActiveLinks; /* how many links up */ 197a9b3dca5SArchie Cobbs int activeLinks[NG_PPP_MAX_LINKS]; /* indicies */ 1983949bee8SArchie Cobbs u_int lastLink; /* for round robin */ 199a9b3dca5SArchie Cobbs hook_p hooks[HOOK_INDEX_MAX]; /* non-link hooks */ 200e3975643SJake Burkholder CIRCLEQ_HEAD(ng_ppp_fraglist, ng_ppp_frag) /* fragment queue */ 201a9b3dca5SArchie Cobbs frags; 202a9b3dca5SArchie Cobbs int qlen; /* fraq queue length */ 203a9b3dca5SArchie Cobbs struct callout_handle fragTimer; /* fraq queue check */ 2044cf49a43SJulian Elischer }; 20562838faeSArchie Cobbs typedef struct ng_ppp_private *priv_p; 2064cf49a43SJulian Elischer 2074cf49a43SJulian Elischer /* Netgraph node methods */ 20874f5c6aaSJulian Elischer static ng_constructor_t ng_ppp_constructor; 20974f5c6aaSJulian Elischer static ng_rcvmsg_t ng_ppp_rcvmsg; 21074f5c6aaSJulian Elischer static ng_shutdown_t ng_ppp_rmnode; 21174f5c6aaSJulian Elischer static ng_newhook_t ng_ppp_newhook; 21274f5c6aaSJulian Elischer static ng_rcvdata_t ng_ppp_rcvdata; 21374f5c6aaSJulian Elischer static ng_disconnect_t ng_ppp_disconnect; 2144cf49a43SJulian Elischer 2153949bee8SArchie Cobbs /* Helper functions */ 216e149c4e2SArchie Cobbs static int ng_ppp_input(node_p node, int bypass, 217e149c4e2SArchie Cobbs int linkNum, struct mbuf *m, meta_p meta); 218d690a6e7SArchie Cobbs static int ng_ppp_output(node_p node, int bypass, int proto, 219e149c4e2SArchie Cobbs int linkNum, struct mbuf *m, meta_p meta); 220e149c4e2SArchie Cobbs static int ng_ppp_mp_input(node_p node, int linkNum, 221e149c4e2SArchie Cobbs struct mbuf *m, meta_p meta); 222a9b3dca5SArchie Cobbs static int ng_ppp_check_packet(node_p node); 223a9b3dca5SArchie Cobbs static void ng_ppp_get_packet(node_p node, struct mbuf **mp, meta_p *metap); 224a9b3dca5SArchie Cobbs static int ng_ppp_frag_process(node_p node); 225a9b3dca5SArchie Cobbs static int ng_ppp_frag_trim(node_p node); 226a9b3dca5SArchie Cobbs static void ng_ppp_frag_timeout(void *arg); 227a9b3dca5SArchie Cobbs static void ng_ppp_frag_checkstale(node_p node); 228a9b3dca5SArchie Cobbs static void ng_ppp_frag_reset(node_p node); 2293949bee8SArchie Cobbs static int ng_ppp_mp_output(node_p node, struct mbuf *m, meta_p meta); 2303949bee8SArchie Cobbs static void ng_ppp_mp_strategy(node_p node, int len, int *distrib); 2313949bee8SArchie Cobbs static int ng_ppp_intcmp(const void *v1, const void *v2); 2323949bee8SArchie Cobbs static struct mbuf *ng_ppp_addproto(struct mbuf *m, int proto, int compOK); 233d690a6e7SArchie Cobbs static struct mbuf *ng_ppp_prepend(struct mbuf *m, const void *buf, int len); 2343949bee8SArchie Cobbs static int ng_ppp_config_valid(node_p node, 235a9b3dca5SArchie Cobbs const struct ng_ppp_node_conf *newConf); 2363949bee8SArchie Cobbs static void ng_ppp_update(node_p node, int newConf); 237a9b3dca5SArchie Cobbs static void ng_ppp_start_frag_timer(node_p node); 238a9b3dca5SArchie Cobbs static void ng_ppp_stop_frag_timer(node_p node); 2394cf49a43SJulian Elischer 2406f16db81SArchie Cobbs /* Parse type for struct ng_ppp_mp_state_type */ 2416f16db81SArchie Cobbs static const struct ng_parse_fixedarray_info ng_ppp_rseq_array_info = { 2426f16db81SArchie Cobbs &ng_parse_hint32_type, 2436f16db81SArchie Cobbs NG_PPP_MAX_LINKS 2446f16db81SArchie Cobbs }; 2456f16db81SArchie Cobbs static const struct ng_parse_type ng_ppp_rseq_array_type = { 2466f16db81SArchie Cobbs &ng_parse_fixedarray_type, 2476f16db81SArchie Cobbs &ng_ppp_rseq_array_info, 2486f16db81SArchie Cobbs }; 2496f16db81SArchie Cobbs static const struct ng_parse_struct_info ng_ppp_mp_state_type_info 2506f16db81SArchie Cobbs = NG_PPP_MP_STATE_TYPE_INFO(&ng_ppp_rseq_array_type); 2516f16db81SArchie Cobbs static const struct ng_parse_type ng_ppp_mp_state_type = { 2526f16db81SArchie Cobbs &ng_parse_struct_type, 2536f16db81SArchie Cobbs &ng_ppp_mp_state_type_info, 2546f16db81SArchie Cobbs }; 2556f16db81SArchie Cobbs 256a9b3dca5SArchie Cobbs /* Parse type for struct ng_ppp_link_conf */ 257f8307e12SArchie Cobbs static const struct ng_parse_struct_info 258f8307e12SArchie Cobbs ng_ppp_link_type_info = NG_PPP_LINK_TYPE_INFO; 259f8307e12SArchie Cobbs static const struct ng_parse_type ng_ppp_link_type = { 260f8307e12SArchie Cobbs &ng_parse_struct_type, 261f8307e12SArchie Cobbs &ng_ppp_link_type_info, 262f8307e12SArchie Cobbs }; 263f8307e12SArchie Cobbs 264a9b3dca5SArchie Cobbs /* Parse type for struct ng_ppp_bund_conf */ 265a9b3dca5SArchie Cobbs static const struct ng_parse_struct_info 266a9b3dca5SArchie Cobbs ng_ppp_bund_type_info = NG_PPP_BUND_TYPE_INFO; 267a9b3dca5SArchie Cobbs static const struct ng_parse_type ng_ppp_bund_type = { 268a9b3dca5SArchie Cobbs &ng_parse_struct_type, 269a9b3dca5SArchie Cobbs &ng_ppp_bund_type_info, 270a9b3dca5SArchie Cobbs }; 271a9b3dca5SArchie Cobbs 272a9b3dca5SArchie Cobbs /* Parse type for struct ng_ppp_node_conf */ 2736f16db81SArchie Cobbs static const struct ng_parse_fixedarray_info ng_ppp_array_info = { 274f8307e12SArchie Cobbs &ng_ppp_link_type, 275f8307e12SArchie Cobbs NG_PPP_MAX_LINKS 276f8307e12SArchie Cobbs }; 277f8307e12SArchie Cobbs static const struct ng_parse_type ng_ppp_link_array_type = { 278f8307e12SArchie Cobbs &ng_parse_fixedarray_type, 279f8307e12SArchie Cobbs &ng_ppp_array_info, 280f8307e12SArchie Cobbs }; 281a9b3dca5SArchie Cobbs static const struct ng_parse_struct_info ng_ppp_conf_type_info 282a9b3dca5SArchie Cobbs = NG_PPP_CONFIG_TYPE_INFO(&ng_ppp_bund_type, &ng_ppp_link_array_type); 283a9b3dca5SArchie Cobbs static const struct ng_parse_type ng_ppp_conf_type = { 284f8307e12SArchie Cobbs &ng_parse_struct_type, 285a9b3dca5SArchie Cobbs &ng_ppp_conf_type_info 286f8307e12SArchie Cobbs }; 287f8307e12SArchie Cobbs 288f8307e12SArchie Cobbs /* Parse type for struct ng_ppp_link_stat */ 289f8307e12SArchie Cobbs static const struct ng_parse_struct_info 290f8307e12SArchie Cobbs ng_ppp_stats_type_info = NG_PPP_STATS_TYPE_INFO; 291f8307e12SArchie Cobbs static const struct ng_parse_type ng_ppp_stats_type = { 292f8307e12SArchie Cobbs &ng_parse_struct_type, 293f8307e12SArchie Cobbs &ng_ppp_stats_type_info 294f8307e12SArchie Cobbs }; 295f8307e12SArchie Cobbs 296f8307e12SArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */ 297f8307e12SArchie Cobbs static const struct ng_cmdlist ng_ppp_cmds[] = { 298f8307e12SArchie Cobbs { 299f8307e12SArchie Cobbs NGM_PPP_COOKIE, 300f8307e12SArchie Cobbs NGM_PPP_SET_CONFIG, 301f8307e12SArchie Cobbs "setconfig", 302a9b3dca5SArchie Cobbs &ng_ppp_conf_type, 303f8307e12SArchie Cobbs NULL 304f8307e12SArchie Cobbs }, 305f8307e12SArchie Cobbs { 306f8307e12SArchie Cobbs NGM_PPP_COOKIE, 307f8307e12SArchie Cobbs NGM_PPP_GET_CONFIG, 308f8307e12SArchie Cobbs "getconfig", 309f8307e12SArchie Cobbs NULL, 310a9b3dca5SArchie Cobbs &ng_ppp_conf_type 311f8307e12SArchie Cobbs }, 312f8307e12SArchie Cobbs { 313f8307e12SArchie Cobbs NGM_PPP_COOKIE, 3146f16db81SArchie Cobbs NGM_PPP_GET_MP_STATE, 3156f16db81SArchie Cobbs "getmpstate", 3166f16db81SArchie Cobbs NULL, 3176f16db81SArchie Cobbs &ng_ppp_mp_state_type 3186f16db81SArchie Cobbs }, 3196f16db81SArchie Cobbs { 3206f16db81SArchie Cobbs NGM_PPP_COOKIE, 321f8307e12SArchie Cobbs NGM_PPP_GET_LINK_STATS, 322f8307e12SArchie Cobbs "getstats", 323f8307e12SArchie Cobbs &ng_parse_int16_type, 324f8307e12SArchie Cobbs &ng_ppp_stats_type 325f8307e12SArchie Cobbs }, 326f8307e12SArchie Cobbs { 327f8307e12SArchie Cobbs NGM_PPP_COOKIE, 328f8307e12SArchie Cobbs NGM_PPP_CLR_LINK_STATS, 329f8307e12SArchie Cobbs "clrstats", 330f8307e12SArchie Cobbs &ng_parse_int16_type, 331f8307e12SArchie Cobbs NULL 332f8307e12SArchie Cobbs }, 333f8307e12SArchie Cobbs { 334f8307e12SArchie Cobbs NGM_PPP_COOKIE, 335f8307e12SArchie Cobbs NGM_PPP_GETCLR_LINK_STATS, 336f8307e12SArchie Cobbs "getclrstats", 337f8307e12SArchie Cobbs &ng_parse_int16_type, 338f8307e12SArchie Cobbs &ng_ppp_stats_type 339f8307e12SArchie Cobbs }, 340f8307e12SArchie Cobbs { 0 } 341f8307e12SArchie Cobbs }; 342f8307e12SArchie Cobbs 3434cf49a43SJulian Elischer /* Node type descriptor */ 3443949bee8SArchie Cobbs static struct ng_type ng_ppp_typestruct = { 3454cf49a43SJulian Elischer NG_VERSION, 3464cf49a43SJulian Elischer NG_PPP_NODE_TYPE, 3474cf49a43SJulian Elischer NULL, 3484cf49a43SJulian Elischer ng_ppp_constructor, 3494cf49a43SJulian Elischer ng_ppp_rcvmsg, 3504cf49a43SJulian Elischer ng_ppp_rmnode, 3514cf49a43SJulian Elischer ng_ppp_newhook, 3524cf49a43SJulian Elischer NULL, 3534cf49a43SJulian Elischer NULL, 3544cf49a43SJulian Elischer ng_ppp_rcvdata, 3554cf49a43SJulian Elischer ng_ppp_rcvdata, 356f8307e12SArchie Cobbs ng_ppp_disconnect, 357f8307e12SArchie Cobbs ng_ppp_cmds 3584cf49a43SJulian Elischer }; 3593949bee8SArchie Cobbs NETGRAPH_INIT(ppp, &ng_ppp_typestruct); 3604cf49a43SJulian Elischer 3613949bee8SArchie Cobbs static int *compareLatencies; /* hack for ng_ppp_intcmp() */ 3624cf49a43SJulian Elischer 363d690a6e7SArchie Cobbs /* Address and control field header */ 364d690a6e7SArchie Cobbs static const u_char ng_ppp_acf[2] = { 0xff, 0x03 }; 365d690a6e7SArchie Cobbs 366a9b3dca5SArchie Cobbs /* Maximum time we'll let a complete incoming packet sit in the queue */ 367a9b3dca5SArchie Cobbs static const struct timeval ng_ppp_max_staleness = { 2, 0 }; /* 2 seconds */ 368a9b3dca5SArchie Cobbs 3694cf49a43SJulian Elischer #define ERROUT(x) do { error = (x); goto done; } while (0) 3704cf49a43SJulian Elischer 3714cf49a43SJulian Elischer /************************************************************************ 3724cf49a43SJulian Elischer NETGRAPH NODE STUFF 3734cf49a43SJulian Elischer ************************************************************************/ 3744cf49a43SJulian Elischer 3754cf49a43SJulian Elischer /* 3763949bee8SArchie Cobbs * Node type constructor 3774cf49a43SJulian Elischer */ 3784cf49a43SJulian Elischer static int 3794cf49a43SJulian Elischer ng_ppp_constructor(node_p *nodep) 3804cf49a43SJulian Elischer { 3814cf49a43SJulian Elischer priv_p priv; 382a9b3dca5SArchie Cobbs int i, error; 3834cf49a43SJulian Elischer 3844cf49a43SJulian Elischer /* Allocate private structure */ 38565b9a0daSArchie Cobbs MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT); 3864cf49a43SJulian Elischer if (priv == NULL) 3874cf49a43SJulian Elischer return (ENOMEM); 3884cf49a43SJulian Elischer bzero(priv, sizeof(*priv)); 3894cf49a43SJulian Elischer 3904cf49a43SJulian Elischer /* Call generic node constructor */ 3913949bee8SArchie Cobbs if ((error = ng_make_node_common(&ng_ppp_typestruct, nodep))) { 3924cf49a43SJulian Elischer FREE(priv, M_NETGRAPH); 3934cf49a43SJulian Elischer return (error); 3944cf49a43SJulian Elischer } 3954cf49a43SJulian Elischer (*nodep)->private = priv; 3964cf49a43SJulian Elischer 3973949bee8SArchie Cobbs /* Initialize state */ 3983949bee8SArchie Cobbs CIRCLEQ_INIT(&priv->frags); 399a9b3dca5SArchie Cobbs for (i = 0; i < NG_PPP_MAX_LINKS; i++) 400a9b3dca5SArchie Cobbs priv->links[i].seq = MP_NOSEQ; 401a9b3dca5SArchie Cobbs callout_handle_init(&priv->fragTimer); 4023949bee8SArchie Cobbs 4034cf49a43SJulian Elischer /* Done */ 4044cf49a43SJulian Elischer return (0); 4054cf49a43SJulian Elischer } 4064cf49a43SJulian Elischer 4074cf49a43SJulian Elischer /* 4084cf49a43SJulian Elischer * Give our OK for a hook to be added 4094cf49a43SJulian Elischer */ 4104cf49a43SJulian Elischer static int 4114cf49a43SJulian Elischer ng_ppp_newhook(node_p node, hook_p hook, const char *name) 4124cf49a43SJulian Elischer { 4134cf49a43SJulian Elischer const priv_p priv = node->private; 4143949bee8SArchie Cobbs int linkNum = -1; 4153949bee8SArchie Cobbs hook_p *hookPtr = NULL; 4163949bee8SArchie Cobbs int hookIndex = -1; 4174cf49a43SJulian Elischer 4183949bee8SArchie Cobbs /* Figure out which hook it is */ 4193949bee8SArchie Cobbs if (strncmp(name, NG_PPP_HOOK_LINK_PREFIX, /* a link hook? */ 4203949bee8SArchie Cobbs strlen(NG_PPP_HOOK_LINK_PREFIX)) == 0) { 42125792ef3SArchie Cobbs const char *cp; 42225792ef3SArchie Cobbs char *eptr; 4233949bee8SArchie Cobbs 4242b70adcbSArchie Cobbs cp = name + strlen(NG_PPP_HOOK_LINK_PREFIX); 4252b70adcbSArchie Cobbs if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0')) 4264cf49a43SJulian Elischer return (EINVAL); 4272b70adcbSArchie Cobbs linkNum = (int)strtoul(cp, &eptr, 10); 4282b70adcbSArchie Cobbs if (*eptr != '\0' || linkNum < 0 || linkNum >= NG_PPP_MAX_LINKS) 4293949bee8SArchie Cobbs return (EINVAL); 430a9b3dca5SArchie Cobbs hookPtr = &priv->links[linkNum].hook; 4313949bee8SArchie Cobbs hookIndex = ~linkNum; 4323949bee8SArchie Cobbs } else { /* must be a non-link hook */ 4333949bee8SArchie Cobbs int i; 4344cf49a43SJulian Elischer 4353949bee8SArchie Cobbs for (i = 0; ng_ppp_hook_names[i] != NULL; i++) { 4363949bee8SArchie Cobbs if (strcmp(name, ng_ppp_hook_names[i]) == 0) { 4373949bee8SArchie Cobbs hookPtr = &priv->hooks[i]; 4383949bee8SArchie Cobbs hookIndex = i; 4393949bee8SArchie Cobbs break; 4403949bee8SArchie Cobbs } 4413949bee8SArchie Cobbs } 4423949bee8SArchie Cobbs if (ng_ppp_hook_names[i] == NULL) 4433949bee8SArchie Cobbs return (EINVAL); /* no such hook */ 4443949bee8SArchie Cobbs } 4453949bee8SArchie Cobbs 4463949bee8SArchie Cobbs /* See if hook is already connected */ 4473949bee8SArchie Cobbs if (*hookPtr != NULL) 4484cf49a43SJulian Elischer return (EISCONN); 4494cf49a43SJulian Elischer 4503949bee8SArchie Cobbs /* Disallow more than one link unless multilink is enabled */ 451a9b3dca5SArchie Cobbs if (linkNum != -1 && priv->links[linkNum].conf.enableLink 4523949bee8SArchie Cobbs && !priv->conf.enableMultilink && priv->numActiveLinks >= 1) 4533949bee8SArchie Cobbs return (ENODEV); 4544cf49a43SJulian Elischer 4554cf49a43SJulian Elischer /* OK */ 4563949bee8SArchie Cobbs *hookPtr = hook; 4573949bee8SArchie Cobbs HOOK_INDEX(hook) = hookIndex; 4583949bee8SArchie Cobbs ng_ppp_update(node, 0); 4594cf49a43SJulian Elischer return (0); 4604cf49a43SJulian Elischer } 4614cf49a43SJulian Elischer 4624cf49a43SJulian Elischer /* 4634cf49a43SJulian Elischer * Receive a control message 4644cf49a43SJulian Elischer */ 4654cf49a43SJulian Elischer static int 4664cf49a43SJulian Elischer ng_ppp_rcvmsg(node_p node, struct ng_mesg *msg, 467a4ec03cfSJulian Elischer const char *raddr, struct ng_mesg **rptr, hook_p lasthook) 4684cf49a43SJulian Elischer { 4694cf49a43SJulian Elischer const priv_p priv = node->private; 4704cf49a43SJulian Elischer struct ng_mesg *resp = NULL; 4714cf49a43SJulian Elischer int error = 0; 4724cf49a43SJulian Elischer 4734cf49a43SJulian Elischer switch (msg->header.typecookie) { 4744cf49a43SJulian Elischer case NGM_PPP_COOKIE: 4754cf49a43SJulian Elischer switch (msg->header.cmd) { 4763949bee8SArchie Cobbs case NGM_PPP_SET_CONFIG: 4773949bee8SArchie Cobbs { 478a9b3dca5SArchie Cobbs struct ng_ppp_node_conf *const conf = 479a9b3dca5SArchie Cobbs (struct ng_ppp_node_conf *)msg->data; 480a9b3dca5SArchie Cobbs int i; 4813949bee8SArchie Cobbs 4823949bee8SArchie Cobbs /* Check for invalid or illegal config */ 483a9b3dca5SArchie Cobbs if (msg->header.arglen != sizeof(*conf)) 4844cf49a43SJulian Elischer ERROUT(EINVAL); 485a9b3dca5SArchie Cobbs if (!ng_ppp_config_valid(node, conf)) 4863949bee8SArchie Cobbs ERROUT(EINVAL); 487a9b3dca5SArchie Cobbs 488a9b3dca5SArchie Cobbs /* Copy config */ 489a9b3dca5SArchie Cobbs priv->conf = conf->bund; 490a9b3dca5SArchie Cobbs for (i = 0; i < NG_PPP_MAX_LINKS; i++) 491a9b3dca5SArchie Cobbs priv->links[i].conf = conf->links[i]; 4923949bee8SArchie Cobbs ng_ppp_update(node, 1); 4934cf49a43SJulian Elischer break; 4943949bee8SArchie Cobbs } 4953949bee8SArchie Cobbs case NGM_PPP_GET_CONFIG: 496a9b3dca5SArchie Cobbs { 497a9b3dca5SArchie Cobbs struct ng_ppp_node_conf *conf; 498a9b3dca5SArchie Cobbs int i; 499a9b3dca5SArchie Cobbs 500a9b3dca5SArchie Cobbs NG_MKRESPONSE(resp, msg, sizeof(*conf), M_NOWAIT); 5014cf49a43SJulian Elischer if (resp == NULL) 5024cf49a43SJulian Elischer ERROUT(ENOMEM); 503a9b3dca5SArchie Cobbs conf = (struct ng_ppp_node_conf *)resp->data; 504a9b3dca5SArchie Cobbs conf->bund = priv->conf; 505a9b3dca5SArchie Cobbs for (i = 0; i < NG_PPP_MAX_LINKS; i++) 506a9b3dca5SArchie Cobbs conf->links[i] = priv->links[i].conf; 5074cf49a43SJulian Elischer break; 508a9b3dca5SArchie Cobbs } 5096f16db81SArchie Cobbs case NGM_PPP_GET_MP_STATE: 5106f16db81SArchie Cobbs { 5116f16db81SArchie Cobbs struct ng_ppp_mp_state *info; 5126f16db81SArchie Cobbs int i; 5136f16db81SArchie Cobbs 5146f16db81SArchie Cobbs NG_MKRESPONSE(resp, msg, sizeof(*info), M_NOWAIT); 5156f16db81SArchie Cobbs if (resp == NULL) 5166f16db81SArchie Cobbs ERROUT(ENOMEM); 5176f16db81SArchie Cobbs info = (struct ng_ppp_mp_state *)resp->data; 5186f16db81SArchie Cobbs bzero(info, sizeof(*info)); 5196f16db81SArchie Cobbs for (i = 0; i < NG_PPP_MAX_LINKS; i++) { 5206f16db81SArchie Cobbs if (priv->links[i].seq != MP_NOSEQ) 5216f16db81SArchie Cobbs info->rseq[i] = priv->links[i].seq; 5226f16db81SArchie Cobbs } 5236f16db81SArchie Cobbs info->mseq = priv->mseq; 5246f16db81SArchie Cobbs info->xseq = priv->xseq; 5256f16db81SArchie Cobbs break; 5266f16db81SArchie Cobbs } 5273949bee8SArchie Cobbs case NGM_PPP_GET_LINK_STATS: 5283949bee8SArchie Cobbs case NGM_PPP_CLR_LINK_STATS: 529e149c4e2SArchie Cobbs case NGM_PPP_GETCLR_LINK_STATS: 5303949bee8SArchie Cobbs { 5313949bee8SArchie Cobbs struct ng_ppp_link_stat *stats; 5323949bee8SArchie Cobbs u_int16_t linkNum; 5333949bee8SArchie Cobbs 5343949bee8SArchie Cobbs if (msg->header.arglen != sizeof(u_int16_t)) 5353949bee8SArchie Cobbs ERROUT(EINVAL); 5363949bee8SArchie Cobbs linkNum = *((u_int16_t *) msg->data); 5373949bee8SArchie Cobbs if (linkNum >= NG_PPP_MAX_LINKS 5383949bee8SArchie Cobbs && linkNum != NG_PPP_BUNDLE_LINKNUM) 5393949bee8SArchie Cobbs ERROUT(EINVAL); 5403949bee8SArchie Cobbs stats = (linkNum == NG_PPP_BUNDLE_LINKNUM) ? 541a9b3dca5SArchie Cobbs &priv->bundleStats : &priv->links[linkNum].stats; 542e149c4e2SArchie Cobbs if (msg->header.cmd != NGM_PPP_CLR_LINK_STATS) { 5433949bee8SArchie Cobbs NG_MKRESPONSE(resp, msg, 5443949bee8SArchie Cobbs sizeof(struct ng_ppp_link_stat), M_NOWAIT); 5453949bee8SArchie Cobbs if (resp == NULL) 5463949bee8SArchie Cobbs ERROUT(ENOMEM); 5473949bee8SArchie Cobbs bcopy(stats, resp->data, sizeof(*stats)); 548e149c4e2SArchie Cobbs } 549e149c4e2SArchie Cobbs if (msg->header.cmd != NGM_PPP_GET_LINK_STATS) 5503949bee8SArchie Cobbs bzero(stats, sizeof(*stats)); 5514cf49a43SJulian Elischer break; 5523949bee8SArchie Cobbs } 5534cf49a43SJulian Elischer default: 5544cf49a43SJulian Elischer error = EINVAL; 5554cf49a43SJulian Elischer break; 5564cf49a43SJulian Elischer } 5574cf49a43SJulian Elischer break; 5583949bee8SArchie Cobbs case NGM_VJC_COOKIE: 5593949bee8SArchie Cobbs { 5603949bee8SArchie Cobbs char path[NG_PATHLEN + 1]; 5613949bee8SArchie Cobbs node_p origNode; 5623949bee8SArchie Cobbs 563a9b3dca5SArchie Cobbs if ((error = ng_path2node(node, 564a9b3dca5SArchie Cobbs raddr, &origNode, NULL, NULL)) != 0) 5653949bee8SArchie Cobbs ERROUT(error); 5663949bee8SArchie Cobbs snprintf(path, sizeof(path), "[%lx]:%s", 5673949bee8SArchie Cobbs (long)node, NG_PPP_HOOK_VJC_IP); 5683949bee8SArchie Cobbs return ng_send_msg(origNode, msg, path, rptr); 5693949bee8SArchie Cobbs } 5704cf49a43SJulian Elischer default: 5714cf49a43SJulian Elischer error = EINVAL; 5724cf49a43SJulian Elischer break; 5734cf49a43SJulian Elischer } 5744cf49a43SJulian Elischer if (rptr) 5754cf49a43SJulian Elischer *rptr = resp; 5764cf49a43SJulian Elischer else if (resp) 5774cf49a43SJulian Elischer FREE(resp, M_NETGRAPH); 5784cf49a43SJulian Elischer 5794cf49a43SJulian Elischer done: 5804cf49a43SJulian Elischer FREE(msg, M_NETGRAPH); 5814cf49a43SJulian Elischer return (error); 5824cf49a43SJulian Elischer } 5834cf49a43SJulian Elischer 5844cf49a43SJulian Elischer /* 5854cf49a43SJulian Elischer * Receive data on a hook 5864cf49a43SJulian Elischer */ 5874cf49a43SJulian Elischer static int 588a4ec03cfSJulian Elischer ng_ppp_rcvdata(hook_p hook, struct mbuf *m, meta_p meta, 589a4ec03cfSJulian Elischer struct mbuf **ret_m, meta_p *ret_meta) 5904cf49a43SJulian Elischer { 5914cf49a43SJulian Elischer const node_p node = hook->node; 5924cf49a43SJulian Elischer const priv_p priv = node->private; 5933949bee8SArchie Cobbs const int index = HOOK_INDEX(hook); 5943949bee8SArchie Cobbs u_int16_t linkNum = NG_PPP_BUNDLE_LINKNUM; 5953949bee8SArchie Cobbs hook_p outHook = NULL; 5963949bee8SArchie Cobbs int proto = 0, error; 5974cf49a43SJulian Elischer 5983949bee8SArchie Cobbs /* Did it come from a link hook? */ 5993949bee8SArchie Cobbs if (index < 0) { 600a9b3dca5SArchie Cobbs struct ng_ppp_link *link; 6014cf49a43SJulian Elischer 602e149c4e2SArchie Cobbs /* Convert index into a link number */ 6033949bee8SArchie Cobbs linkNum = (u_int16_t)~index; 6043949bee8SArchie Cobbs KASSERT(linkNum < NG_PPP_MAX_LINKS, 6053949bee8SArchie Cobbs ("%s: bogus index 0x%x", __FUNCTION__, index)); 606a9b3dca5SArchie Cobbs link = &priv->links[linkNum]; 6073949bee8SArchie Cobbs 6083949bee8SArchie Cobbs /* Stats */ 609a9b3dca5SArchie Cobbs link->stats.recvFrames++; 610a9b3dca5SArchie Cobbs link->stats.recvOctets += m->m_pkthdr.len; 6113949bee8SArchie Cobbs 612d690a6e7SArchie Cobbs /* Strip address and control fields, if present */ 613d690a6e7SArchie Cobbs if (m->m_pkthdr.len >= 2) { 614d690a6e7SArchie Cobbs if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) { 615d690a6e7SArchie Cobbs NG_FREE_DATA(m, meta); 616d690a6e7SArchie Cobbs return (ENOBUFS); 617d690a6e7SArchie Cobbs } 618d690a6e7SArchie Cobbs if (bcmp(mtod(m, u_char *), &ng_ppp_acf, 2) == 0) 619d690a6e7SArchie Cobbs m_adj(m, 2); 620d690a6e7SArchie Cobbs } 621d690a6e7SArchie Cobbs 622e149c4e2SArchie Cobbs /* Dispatch incoming frame (if not enabled, to bypass) */ 623e149c4e2SArchie Cobbs return ng_ppp_input(node, 624a9b3dca5SArchie Cobbs !link->conf.enableLink, linkNum, m, meta); 6253949bee8SArchie Cobbs } 6263949bee8SArchie Cobbs 6273949bee8SArchie Cobbs /* Get protocol & check if data allowed from this hook */ 6283949bee8SArchie Cobbs switch (index) { 6293949bee8SArchie Cobbs 630e149c4e2SArchie Cobbs /* Outgoing data */ 6313949bee8SArchie Cobbs case HOOK_INDEX_ATALK: 6323949bee8SArchie Cobbs if (!priv->conf.enableAtalk) { 6333949bee8SArchie Cobbs NG_FREE_DATA(m, meta); 6343949bee8SArchie Cobbs return (ENXIO); 6353949bee8SArchie Cobbs } 6363949bee8SArchie Cobbs proto = PROT_APPLETALK; 6373949bee8SArchie Cobbs break; 6383949bee8SArchie Cobbs case HOOK_INDEX_IPX: 6393949bee8SArchie Cobbs if (!priv->conf.enableIPX) { 6403949bee8SArchie Cobbs NG_FREE_DATA(m, meta); 6413949bee8SArchie Cobbs return (ENXIO); 6423949bee8SArchie Cobbs } 6433949bee8SArchie Cobbs proto = PROT_IPX; 6443949bee8SArchie Cobbs break; 645a9b3dca5SArchie Cobbs case HOOK_INDEX_IPV6: 646a9b3dca5SArchie Cobbs if (!priv->conf.enableIPv6) { 647a9b3dca5SArchie Cobbs NG_FREE_DATA(m, meta); 648a9b3dca5SArchie Cobbs return (ENXIO); 649a9b3dca5SArchie Cobbs } 650a9b3dca5SArchie Cobbs proto = PROT_IPV6; 651a9b3dca5SArchie Cobbs break; 6523949bee8SArchie Cobbs case HOOK_INDEX_INET: 6533949bee8SArchie Cobbs case HOOK_INDEX_VJC_VJIP: 6543949bee8SArchie Cobbs if (!priv->conf.enableIP) { 6553949bee8SArchie Cobbs NG_FREE_DATA(m, meta); 6563949bee8SArchie Cobbs return (ENXIO); 6573949bee8SArchie Cobbs } 6583949bee8SArchie Cobbs proto = PROT_IP; 6593949bee8SArchie Cobbs break; 6603949bee8SArchie Cobbs case HOOK_INDEX_VJC_COMP: 6613949bee8SArchie Cobbs if (!priv->conf.enableVJCompression) { 6623949bee8SArchie Cobbs NG_FREE_DATA(m, meta); 6633949bee8SArchie Cobbs return (ENXIO); 6643949bee8SArchie Cobbs } 6653949bee8SArchie Cobbs proto = PROT_VJCOMP; 6663949bee8SArchie Cobbs break; 6673949bee8SArchie Cobbs case HOOK_INDEX_VJC_UNCOMP: 6683949bee8SArchie Cobbs if (!priv->conf.enableVJCompression) { 6693949bee8SArchie Cobbs NG_FREE_DATA(m, meta); 6703949bee8SArchie Cobbs return (ENXIO); 6713949bee8SArchie Cobbs } 6723949bee8SArchie Cobbs proto = PROT_VJUNCOMP; 6733949bee8SArchie Cobbs break; 6743949bee8SArchie Cobbs case HOOK_INDEX_COMPRESS: 6753949bee8SArchie Cobbs if (!priv->conf.enableCompression) { 6763949bee8SArchie Cobbs NG_FREE_DATA(m, meta); 6773949bee8SArchie Cobbs return (ENXIO); 6783949bee8SArchie Cobbs } 6793949bee8SArchie Cobbs proto = PROT_COMPD; 6803949bee8SArchie Cobbs break; 6813949bee8SArchie Cobbs case HOOK_INDEX_ENCRYPT: 6823949bee8SArchie Cobbs if (!priv->conf.enableEncryption) { 6833949bee8SArchie Cobbs NG_FREE_DATA(m, meta); 6843949bee8SArchie Cobbs return (ENXIO); 6853949bee8SArchie Cobbs } 6863949bee8SArchie Cobbs proto = PROT_CRYPTD; 6873949bee8SArchie Cobbs break; 6883949bee8SArchie Cobbs case HOOK_INDEX_BYPASS: 6893949bee8SArchie Cobbs if (m->m_pkthdr.len < 4) { 6904c641908SArchie Cobbs NG_FREE_DATA(m, meta); 6913949bee8SArchie Cobbs return (EINVAL); 6923949bee8SArchie Cobbs } 6933949bee8SArchie Cobbs if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) { 6943949bee8SArchie Cobbs NG_FREE_META(meta); 6953949bee8SArchie Cobbs return (ENOBUFS); 6963949bee8SArchie Cobbs } 6973949bee8SArchie Cobbs linkNum = ntohs(mtod(m, u_int16_t *)[0]); 6983949bee8SArchie Cobbs proto = ntohs(mtod(m, u_int16_t *)[1]); 6993949bee8SArchie Cobbs m_adj(m, 4); 7003949bee8SArchie Cobbs if (linkNum >= NG_PPP_MAX_LINKS 7014c641908SArchie Cobbs && linkNum != NG_PPP_BUNDLE_LINKNUM) { 7024c641908SArchie Cobbs NG_FREE_DATA(m, meta); 7033949bee8SArchie Cobbs return (EINVAL); 7044c641908SArchie Cobbs } 7053949bee8SArchie Cobbs break; 7063949bee8SArchie Cobbs 707e149c4e2SArchie Cobbs /* Incoming data */ 7083949bee8SArchie Cobbs case HOOK_INDEX_VJC_IP: 709e149c4e2SArchie Cobbs if (!priv->conf.enableIP || !priv->conf.enableVJDecompression) { 7103949bee8SArchie Cobbs NG_FREE_DATA(m, meta); 7113949bee8SArchie Cobbs return (ENXIO); 7123949bee8SArchie Cobbs } 7133949bee8SArchie Cobbs break; 7143949bee8SArchie Cobbs case HOOK_INDEX_DECOMPRESS: 7153949bee8SArchie Cobbs if (!priv->conf.enableDecompression) { 7163949bee8SArchie Cobbs NG_FREE_DATA(m, meta); 7173949bee8SArchie Cobbs return (ENXIO); 7183949bee8SArchie Cobbs } 7193949bee8SArchie Cobbs break; 7203949bee8SArchie Cobbs case HOOK_INDEX_DECRYPT: 7213949bee8SArchie Cobbs if (!priv->conf.enableDecryption) { 7223949bee8SArchie Cobbs NG_FREE_DATA(m, meta); 7233949bee8SArchie Cobbs return (ENXIO); 7243949bee8SArchie Cobbs } 7253949bee8SArchie Cobbs break; 7263949bee8SArchie Cobbs default: 727e149c4e2SArchie Cobbs panic("%s: bogus index 0x%x", __FUNCTION__, index); 7283949bee8SArchie Cobbs } 7293949bee8SArchie Cobbs 7303949bee8SArchie Cobbs /* Now figure out what to do with the frame */ 7313949bee8SArchie Cobbs switch (index) { 732e149c4e2SArchie Cobbs 733e149c4e2SArchie Cobbs /* Outgoing data */ 7343949bee8SArchie Cobbs case HOOK_INDEX_INET: 7353949bee8SArchie Cobbs if (priv->conf.enableVJCompression && priv->vjCompHooked) { 7363949bee8SArchie Cobbs outHook = priv->hooks[HOOK_INDEX_VJC_IP]; 7373949bee8SArchie Cobbs break; 7383949bee8SArchie Cobbs } 7393949bee8SArchie Cobbs /* FALLTHROUGH */ 7403949bee8SArchie Cobbs case HOOK_INDEX_ATALK: 741a9b3dca5SArchie Cobbs case HOOK_INDEX_IPV6: 7423949bee8SArchie Cobbs case HOOK_INDEX_IPX: 7433949bee8SArchie Cobbs case HOOK_INDEX_VJC_COMP: 7443949bee8SArchie Cobbs case HOOK_INDEX_VJC_UNCOMP: 7453949bee8SArchie Cobbs case HOOK_INDEX_VJC_VJIP: 7463949bee8SArchie Cobbs if (priv->conf.enableCompression 7473949bee8SArchie Cobbs && priv->hooks[HOOK_INDEX_COMPRESS] != NULL) { 7483949bee8SArchie Cobbs if ((m = ng_ppp_addproto(m, proto, 1)) == NULL) { 7493949bee8SArchie Cobbs NG_FREE_META(meta); 7503949bee8SArchie Cobbs return (ENOBUFS); 7513949bee8SArchie Cobbs } 7523949bee8SArchie Cobbs outHook = priv->hooks[HOOK_INDEX_COMPRESS]; 7533949bee8SArchie Cobbs break; 7543949bee8SArchie Cobbs } 7553949bee8SArchie Cobbs /* FALLTHROUGH */ 7563949bee8SArchie Cobbs case HOOK_INDEX_COMPRESS: 7573949bee8SArchie Cobbs if (priv->conf.enableEncryption 7583949bee8SArchie Cobbs && priv->hooks[HOOK_INDEX_ENCRYPT] != NULL) { 7593949bee8SArchie Cobbs if ((m = ng_ppp_addproto(m, proto, 1)) == NULL) { 7603949bee8SArchie Cobbs NG_FREE_META(meta); 7613949bee8SArchie Cobbs return (ENOBUFS); 7623949bee8SArchie Cobbs } 7633949bee8SArchie Cobbs outHook = priv->hooks[HOOK_INDEX_ENCRYPT]; 7643949bee8SArchie Cobbs break; 7653949bee8SArchie Cobbs } 7663949bee8SArchie Cobbs /* FALLTHROUGH */ 7673949bee8SArchie Cobbs case HOOK_INDEX_ENCRYPT: 7680e11d0f3SArchie Cobbs return ng_ppp_output(node, 0, 769d690a6e7SArchie Cobbs proto, NG_PPP_BUNDLE_LINKNUM, m, meta); 7703949bee8SArchie Cobbs 7710e11d0f3SArchie Cobbs case HOOK_INDEX_BYPASS: 7720e11d0f3SArchie Cobbs return ng_ppp_output(node, 1, proto, linkNum, m, meta); 7730e11d0f3SArchie Cobbs 7743949bee8SArchie Cobbs /* Incoming data */ 7753949bee8SArchie Cobbs case HOOK_INDEX_DECRYPT: 7763949bee8SArchie Cobbs case HOOK_INDEX_DECOMPRESS: 777e149c4e2SArchie Cobbs return ng_ppp_input(node, 0, NG_PPP_BUNDLE_LINKNUM, m, meta); 778e149c4e2SArchie Cobbs 7793949bee8SArchie Cobbs case HOOK_INDEX_VJC_IP: 780e149c4e2SArchie Cobbs outHook = priv->hooks[HOOK_INDEX_INET]; 781e149c4e2SArchie Cobbs break; 7823949bee8SArchie Cobbs } 7833949bee8SArchie Cobbs 7843949bee8SArchie Cobbs /* Send packet out hook */ 785a9b3dca5SArchie Cobbs NG_SEND_DATA_RET(error, outHook, m, meta); 786a9b3dca5SArchie Cobbs if (m != NULL || meta != NULL) 787a9b3dca5SArchie Cobbs return ng_ppp_rcvdata(outHook, m, meta, NULL, NULL); 7884cf49a43SJulian Elischer return (error); 7894cf49a43SJulian Elischer } 7904cf49a43SJulian Elischer 7914cf49a43SJulian Elischer /* 7924cf49a43SJulian Elischer * Destroy node 7934cf49a43SJulian Elischer */ 7944cf49a43SJulian Elischer static int 7954cf49a43SJulian Elischer ng_ppp_rmnode(node_p node) 7964cf49a43SJulian Elischer { 7974cf49a43SJulian Elischer const priv_p priv = node->private; 7984cf49a43SJulian Elischer 799a9b3dca5SArchie Cobbs /* Stop fragment queue timer */ 800a9b3dca5SArchie Cobbs ng_ppp_stop_frag_timer(node); 801a9b3dca5SArchie Cobbs 8024cf49a43SJulian Elischer /* Take down netgraph node */ 8034cf49a43SJulian Elischer node->flags |= NG_INVALID; 8044cf49a43SJulian Elischer ng_cutlinks(node); 8054cf49a43SJulian Elischer ng_unname(node); 806a9b3dca5SArchie Cobbs ng_ppp_frag_reset(node); 8074cf49a43SJulian Elischer bzero(priv, sizeof(*priv)); 8084cf49a43SJulian Elischer FREE(priv, M_NETGRAPH); 8094cf49a43SJulian Elischer node->private = NULL; 8104cf49a43SJulian Elischer ng_unref(node); /* let the node escape */ 8114cf49a43SJulian Elischer return (0); 8124cf49a43SJulian Elischer } 8134cf49a43SJulian Elischer 8144cf49a43SJulian Elischer /* 8154cf49a43SJulian Elischer * Hook disconnection 8164cf49a43SJulian Elischer */ 8174cf49a43SJulian Elischer static int 8184cf49a43SJulian Elischer ng_ppp_disconnect(hook_p hook) 8194cf49a43SJulian Elischer { 82062838faeSArchie Cobbs const node_p node = hook->node; 82162838faeSArchie Cobbs const priv_p priv = node->private; 82262838faeSArchie Cobbs const int index = HOOK_INDEX(hook); 82362838faeSArchie Cobbs 82462838faeSArchie Cobbs /* Zero out hook pointer */ 82562838faeSArchie Cobbs if (index < 0) 826a9b3dca5SArchie Cobbs priv->links[~index].hook = NULL; 82762838faeSArchie Cobbs else 82862838faeSArchie Cobbs priv->hooks[index] = NULL; 82962838faeSArchie Cobbs 83062838faeSArchie Cobbs /* Update derived info (or go away if no hooks left) */ 83162838faeSArchie Cobbs if (node->numhooks > 0) 83262838faeSArchie Cobbs ng_ppp_update(node, 0); 83362838faeSArchie Cobbs else 834a9b3dca5SArchie Cobbs ng_rmnode(node); 8354cf49a43SJulian Elischer return (0); 8364cf49a43SJulian Elischer } 8374cf49a43SJulian Elischer 8384cf49a43SJulian Elischer /************************************************************************ 8394cf49a43SJulian Elischer HELPER STUFF 8404cf49a43SJulian Elischer ************************************************************************/ 8414cf49a43SJulian Elischer 8424cf49a43SJulian Elischer /* 8433949bee8SArchie Cobbs * Handle an incoming frame. Extract the PPP protocol number 8443949bee8SArchie Cobbs * and dispatch accordingly. 8454cf49a43SJulian Elischer */ 8464cf49a43SJulian Elischer static int 847e149c4e2SArchie Cobbs ng_ppp_input(node_p node, int bypass, int linkNum, struct mbuf *m, meta_p meta) 8484cf49a43SJulian Elischer { 8493949bee8SArchie Cobbs const priv_p priv = node->private; 8503949bee8SArchie Cobbs hook_p outHook = NULL; 8513949bee8SArchie Cobbs int proto, error; 8524cf49a43SJulian Elischer 8533949bee8SArchie Cobbs /* Extract protocol number */ 8543949bee8SArchie Cobbs for (proto = 0; !PROT_VALID(proto) && m->m_pkthdr.len > 0; ) { 8553949bee8SArchie Cobbs if (m->m_len < 1 && (m = m_pullup(m, 1)) == NULL) { 8563949bee8SArchie Cobbs NG_FREE_META(meta); 8573949bee8SArchie Cobbs return (ENOBUFS); 8584cf49a43SJulian Elischer } 8593949bee8SArchie Cobbs proto = (proto << 8) + *mtod(m, u_char *); 8603949bee8SArchie Cobbs m_adj(m, 1); 8613949bee8SArchie Cobbs } 8623949bee8SArchie Cobbs if (!PROT_VALID(proto)) { 8633949bee8SArchie Cobbs if (linkNum == NG_PPP_BUNDLE_LINKNUM) 8643949bee8SArchie Cobbs priv->bundleStats.badProtos++; 8653949bee8SArchie Cobbs else 866a9b3dca5SArchie Cobbs priv->links[linkNum].stats.badProtos++; 8673949bee8SArchie Cobbs NG_FREE_DATA(m, meta); 8683949bee8SArchie Cobbs return (EINVAL); 8693949bee8SArchie Cobbs } 8703949bee8SArchie Cobbs 871e149c4e2SArchie Cobbs /* Bypass frame? */ 872e149c4e2SArchie Cobbs if (bypass) 873e149c4e2SArchie Cobbs goto bypass; 874e149c4e2SArchie Cobbs 8753949bee8SArchie Cobbs /* Check protocol */ 8763949bee8SArchie Cobbs switch (proto) { 8773949bee8SArchie Cobbs case PROT_COMPD: 8783949bee8SArchie Cobbs if (priv->conf.enableDecompression) 8793949bee8SArchie Cobbs outHook = priv->hooks[HOOK_INDEX_DECOMPRESS]; 8803949bee8SArchie Cobbs break; 8813949bee8SArchie Cobbs case PROT_CRYPTD: 8823949bee8SArchie Cobbs if (priv->conf.enableDecryption) 8833949bee8SArchie Cobbs outHook = priv->hooks[HOOK_INDEX_DECRYPT]; 8843949bee8SArchie Cobbs break; 8853949bee8SArchie Cobbs case PROT_VJCOMP: 8863949bee8SArchie Cobbs if (priv->conf.enableVJDecompression && priv->vjCompHooked) 8873949bee8SArchie Cobbs outHook = priv->hooks[HOOK_INDEX_VJC_COMP]; 8883949bee8SArchie Cobbs break; 8893949bee8SArchie Cobbs case PROT_VJUNCOMP: 8903949bee8SArchie Cobbs if (priv->conf.enableVJDecompression && priv->vjCompHooked) 8913949bee8SArchie Cobbs outHook = priv->hooks[HOOK_INDEX_VJC_UNCOMP]; 8923949bee8SArchie Cobbs break; 8933949bee8SArchie Cobbs case PROT_MP: 894a9b3dca5SArchie Cobbs if (priv->conf.enableMultilink 895a9b3dca5SArchie Cobbs && linkNum != NG_PPP_BUNDLE_LINKNUM) 8963949bee8SArchie Cobbs return ng_ppp_mp_input(node, linkNum, m, meta); 8973949bee8SArchie Cobbs break; 8983949bee8SArchie Cobbs case PROT_APPLETALK: 8993949bee8SArchie Cobbs if (priv->conf.enableAtalk) 9003949bee8SArchie Cobbs outHook = priv->hooks[HOOK_INDEX_ATALK]; 9013949bee8SArchie Cobbs break; 9023949bee8SArchie Cobbs case PROT_IPX: 9033949bee8SArchie Cobbs if (priv->conf.enableIPX) 9043949bee8SArchie Cobbs outHook = priv->hooks[HOOK_INDEX_IPX]; 9053949bee8SArchie Cobbs break; 9063949bee8SArchie Cobbs case PROT_IP: 9073949bee8SArchie Cobbs if (priv->conf.enableIP) 9083949bee8SArchie Cobbs outHook = priv->hooks[HOOK_INDEX_INET]; 9093949bee8SArchie Cobbs break; 910a9b3dca5SArchie Cobbs case PROT_IPV6: 911a9b3dca5SArchie Cobbs if (priv->conf.enableIPv6) 912a9b3dca5SArchie Cobbs outHook = priv->hooks[HOOK_INDEX_IPV6]; 913a9b3dca5SArchie Cobbs break; 9143949bee8SArchie Cobbs } 9153949bee8SArchie Cobbs 916e149c4e2SArchie Cobbs bypass: 9174c641908SArchie Cobbs /* For unknown/inactive protocols, forward out the bypass hook */ 9183949bee8SArchie Cobbs if (outHook == NULL) { 919d690a6e7SArchie Cobbs u_int16_t hdr[2]; 920d690a6e7SArchie Cobbs 921d690a6e7SArchie Cobbs hdr[0] = htons(linkNum); 922d690a6e7SArchie Cobbs hdr[1] = htons((u_int16_t)proto); 9234c641908SArchie Cobbs if ((m = ng_ppp_prepend(m, &hdr, 4)) == NULL) { 9244c641908SArchie Cobbs NG_FREE_META(meta); 9253949bee8SArchie Cobbs return (ENOBUFS); 9264c641908SArchie Cobbs } 9273949bee8SArchie Cobbs outHook = priv->hooks[HOOK_INDEX_BYPASS]; 9283949bee8SArchie Cobbs } 9293949bee8SArchie Cobbs 9303949bee8SArchie Cobbs /* Forward frame */ 9313949bee8SArchie Cobbs NG_SEND_DATA(error, outHook, m, meta); 9323949bee8SArchie Cobbs return (error); 9334cf49a43SJulian Elischer } 9344cf49a43SJulian Elischer 9354cf49a43SJulian Elischer /* 9363949bee8SArchie Cobbs * Deliver a frame out a link, either a real one or NG_PPP_BUNDLE_LINKNUM 9370e11d0f3SArchie Cobbs * If the link is not enabled then ENXIO is returned, unless "bypass" is != 0. 9384cf49a43SJulian Elischer */ 9393949bee8SArchie Cobbs static int 940d690a6e7SArchie Cobbs ng_ppp_output(node_p node, int bypass, 941d690a6e7SArchie Cobbs int proto, int linkNum, struct mbuf *m, meta_p meta) 9424cf49a43SJulian Elischer { 9433949bee8SArchie Cobbs const priv_p priv = node->private; 944a9b3dca5SArchie Cobbs struct ng_ppp_link *link; 945e149c4e2SArchie Cobbs int len, error; 9464cf49a43SJulian Elischer 947d690a6e7SArchie Cobbs /* If not doing MP, map bundle virtual link to (the only) link */ 948d690a6e7SArchie Cobbs if (linkNum == NG_PPP_BUNDLE_LINKNUM && !priv->conf.enableMultilink) 9493949bee8SArchie Cobbs linkNum = priv->activeLinks[0]; 9503949bee8SArchie Cobbs 951a9b3dca5SArchie Cobbs /* Get link pointer (optimization) */ 952a9b3dca5SArchie Cobbs link = (linkNum != NG_PPP_BUNDLE_LINKNUM) ? 953a9b3dca5SArchie Cobbs &priv->links[linkNum] : NULL; 954a9b3dca5SArchie Cobbs 955d690a6e7SArchie Cobbs /* Check link status (if real) */ 9560e11d0f3SArchie Cobbs if (linkNum != NG_PPP_BUNDLE_LINKNUM) { 957a9b3dca5SArchie Cobbs if (!bypass && !link->conf.enableLink) { 9584c641908SArchie Cobbs NG_FREE_DATA(m, meta); 9593949bee8SArchie Cobbs return (ENXIO); 9604c641908SArchie Cobbs } 961a9b3dca5SArchie Cobbs if (link->hook == NULL) { 9623949bee8SArchie Cobbs NG_FREE_DATA(m, meta); 9633949bee8SArchie Cobbs return (ENETDOWN); 9643949bee8SArchie Cobbs } 9650e11d0f3SArchie Cobbs } 9663949bee8SArchie Cobbs 967d690a6e7SArchie Cobbs /* Prepend protocol number, possibly compressed */ 968d690a6e7SArchie Cobbs if ((m = ng_ppp_addproto(m, proto, 969d690a6e7SArchie Cobbs linkNum == NG_PPP_BUNDLE_LINKNUM 970a9b3dca5SArchie Cobbs || link->conf.enableProtoComp)) == NULL) { 971d690a6e7SArchie Cobbs NG_FREE_META(meta); 972d690a6e7SArchie Cobbs return (ENOBUFS); 973d690a6e7SArchie Cobbs } 974d690a6e7SArchie Cobbs 975d690a6e7SArchie Cobbs /* Special handling for the MP virtual link */ 976d690a6e7SArchie Cobbs if (linkNum == NG_PPP_BUNDLE_LINKNUM) 977d690a6e7SArchie Cobbs return ng_ppp_mp_output(node, m, meta); 978d690a6e7SArchie Cobbs 979d690a6e7SArchie Cobbs /* Prepend address and control field (unless compressed) */ 980a9b3dca5SArchie Cobbs if (proto == PROT_LCP || !link->conf.enableACFComp) { 981d690a6e7SArchie Cobbs if ((m = ng_ppp_prepend(m, &ng_ppp_acf, 2)) == NULL) { 982d690a6e7SArchie Cobbs NG_FREE_META(meta); 983d690a6e7SArchie Cobbs return (ENOBUFS); 984d690a6e7SArchie Cobbs } 985d690a6e7SArchie Cobbs } 986d690a6e7SArchie Cobbs 9873949bee8SArchie Cobbs /* Deliver frame */ 988e149c4e2SArchie Cobbs len = m->m_pkthdr.len; 989a9b3dca5SArchie Cobbs NG_SEND_DATA(error, link->hook, m, meta); 990fb1fc8abSArchie Cobbs 991fb1fc8abSArchie Cobbs /* Update stats and 'bytes in queue' counter */ 992fb1fc8abSArchie Cobbs if (error == 0) { 993a9b3dca5SArchie Cobbs link->stats.xmitFrames++; 994a9b3dca5SArchie Cobbs link->stats.xmitOctets += len; 995a9b3dca5SArchie Cobbs link->bytesInQueue += len; 996a9b3dca5SArchie Cobbs getmicrouptime(&link->lastWrite); 997fb1fc8abSArchie Cobbs } 9983949bee8SArchie Cobbs return error; 9993949bee8SArchie Cobbs } 10003949bee8SArchie Cobbs 10013949bee8SArchie Cobbs /* 10023949bee8SArchie Cobbs * Handle an incoming multi-link fragment 1003a9b3dca5SArchie Cobbs * 1004a9b3dca5SArchie Cobbs * The fragment reassembly algorithm is somewhat complex. This is mainly 1005a9b3dca5SArchie Cobbs * because we are required not to reorder the reconstructed packets, yet 1006a9b3dca5SArchie Cobbs * fragments are only guaranteed to arrive in order on a per-link basis. 1007a9b3dca5SArchie Cobbs * In other words, when we have a complete packet ready, but the previous 1008a9b3dca5SArchie Cobbs * packet is still incomplete, we have to decide between delivering the 1009a9b3dca5SArchie Cobbs * complete packet and throwing away the incomplete one, or waiting to 1010a9b3dca5SArchie Cobbs * see if the remainder of the incomplete one arrives, at which time we 1011a9b3dca5SArchie Cobbs * can deliver both packets, in order. 1012a9b3dca5SArchie Cobbs * 1013a9b3dca5SArchie Cobbs * This problem is exacerbated by "sequence number slew", which is when 1014a9b3dca5SArchie Cobbs * the sequence numbers coming in from different links are far apart from 1015a9b3dca5SArchie Cobbs * each other. In particular, certain unnamed equipment (*cough* Ascend) 1016a9b3dca5SArchie Cobbs * has been seen to generate sequence number slew of up to 10 on an ISDN 1017a9b3dca5SArchie Cobbs * 2B-channel MP link. There is nothing invalid about sequence number slew 1018a9b3dca5SArchie Cobbs * but it makes the reasssembly process have to work harder. 1019a9b3dca5SArchie Cobbs * 1020a9b3dca5SArchie Cobbs * However, the peer is required to transmit fragments in order on each 1021a9b3dca5SArchie Cobbs * link. That means if we define MSEQ as the minimum over all links of 1022a9b3dca5SArchie Cobbs * the highest sequence number received on that link, then we can always 1023a9b3dca5SArchie Cobbs * give up any hope of receiving a fragment with sequence number < MSEQ in 1024a9b3dca5SArchie Cobbs * the future (all of this using 'wraparound' sequence number space). 1025a9b3dca5SArchie Cobbs * Therefore we can always immediately throw away incomplete packets 1026a9b3dca5SArchie Cobbs * missing fragments with sequence numbers < MSEQ. 1027a9b3dca5SArchie Cobbs * 1028a9b3dca5SArchie Cobbs * Here is an overview of our algorithm: 1029a9b3dca5SArchie Cobbs * 1030a9b3dca5SArchie Cobbs * o Received fragments are inserted into a queue, for which we 1031a9b3dca5SArchie Cobbs * maintain these invariants between calls to this function: 1032a9b3dca5SArchie Cobbs * 1033a9b3dca5SArchie Cobbs * - Fragments are ordered in the queue by sequence number 1034a9b3dca5SArchie Cobbs * - If a complete packet is at the head of the queue, then 1035a9b3dca5SArchie Cobbs * the first fragment in the packet has seq# > MSEQ + 1 1036a9b3dca5SArchie Cobbs * (otherwise, we could deliver it immediately) 1037a9b3dca5SArchie Cobbs * - If any fragments have seq# < MSEQ, then they are necessarily 1038a9b3dca5SArchie Cobbs * part of a packet whose missing seq#'s are all > MSEQ (otherwise, 1039a9b3dca5SArchie Cobbs * we can throw them away because they'll never be completed) 1040a9b3dca5SArchie Cobbs * - The queue contains at most MP_MAX_QUEUE_LEN fragments 1041a9b3dca5SArchie Cobbs * 1042a9b3dca5SArchie Cobbs * o We have a periodic timer that checks the queue for the first 1043a9b3dca5SArchie Cobbs * complete packet that has been sitting in the queue "too long". 1044a9b3dca5SArchie Cobbs * When one is detected, all previous (incomplete) fragments are 1045a9b3dca5SArchie Cobbs * discarded, their missing fragments are declared lost and MSEQ 1046a9b3dca5SArchie Cobbs * is increased. 1047a9b3dca5SArchie Cobbs * 1048a9b3dca5SArchie Cobbs * o If we recieve a fragment with seq# < MSEQ, we throw it away 1049a9b3dca5SArchie Cobbs * because we've already delcared it lost. 1050a9b3dca5SArchie Cobbs * 1051a9b3dca5SArchie Cobbs * This assumes linkNum != NG_PPP_BUNDLE_LINKNUM. 10523949bee8SArchie Cobbs */ 10533949bee8SArchie Cobbs static int 10543949bee8SArchie Cobbs ng_ppp_mp_input(node_p node, int linkNum, struct mbuf *m, meta_p meta) 10553949bee8SArchie Cobbs { 10563949bee8SArchie Cobbs const priv_p priv = node->private; 1057a9b3dca5SArchie Cobbs struct ng_ppp_link *const link = &priv->links[linkNum]; 10583949bee8SArchie Cobbs struct ng_ppp_frag frag0, *frag = &frag0; 1059a9b3dca5SArchie Cobbs struct ng_ppp_frag *qent; 1060a9b3dca5SArchie Cobbs int i, diff, inserted; 10613949bee8SArchie Cobbs 1062b4c44c30SArchie Cobbs /* Stats */ 1063b4c44c30SArchie Cobbs priv->bundleStats.recvFrames++; 1064b4c44c30SArchie Cobbs priv->bundleStats.recvOctets += m->m_pkthdr.len; 1065b4c44c30SArchie Cobbs 10663949bee8SArchie Cobbs /* Extract fragment information from MP header */ 10673949bee8SArchie Cobbs if (priv->conf.recvShortSeq) { 10683949bee8SArchie Cobbs u_int16_t shdr; 10693949bee8SArchie Cobbs 10703949bee8SArchie Cobbs if (m->m_pkthdr.len < 2) { 1071a9b3dca5SArchie Cobbs link->stats.runts++; 10723949bee8SArchie Cobbs NG_FREE_DATA(m, meta); 10733949bee8SArchie Cobbs return (EINVAL); 10743949bee8SArchie Cobbs } 10753949bee8SArchie Cobbs if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) { 10763949bee8SArchie Cobbs NG_FREE_META(meta); 10773949bee8SArchie Cobbs return (ENOBUFS); 10783949bee8SArchie Cobbs } 10793949bee8SArchie Cobbs shdr = ntohs(*mtod(m, u_int16_t *)); 10806f16db81SArchie Cobbs frag->seq = MP_SHORT_EXTEND(shdr); 10813949bee8SArchie Cobbs frag->first = (shdr & MP_SHORT_FIRST_FLAG) != 0; 10823949bee8SArchie Cobbs frag->last = (shdr & MP_SHORT_LAST_FLAG) != 0; 1083a9b3dca5SArchie Cobbs diff = MP_SHORT_SEQ_DIFF(frag->seq, priv->mseq); 10843949bee8SArchie Cobbs m_adj(m, 2); 10853949bee8SArchie Cobbs } else { 10863949bee8SArchie Cobbs u_int32_t lhdr; 10873949bee8SArchie Cobbs 10883949bee8SArchie Cobbs if (m->m_pkthdr.len < 4) { 1089a9b3dca5SArchie Cobbs link->stats.runts++; 10903949bee8SArchie Cobbs NG_FREE_DATA(m, meta); 10913949bee8SArchie Cobbs return (EINVAL); 10923949bee8SArchie Cobbs } 10933949bee8SArchie Cobbs if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) { 10943949bee8SArchie Cobbs NG_FREE_META(meta); 10953949bee8SArchie Cobbs return (ENOBUFS); 10963949bee8SArchie Cobbs } 10973949bee8SArchie Cobbs lhdr = ntohl(*mtod(m, u_int32_t *)); 10986f16db81SArchie Cobbs frag->seq = MP_LONG_EXTEND(lhdr); 10993949bee8SArchie Cobbs frag->first = (lhdr & MP_LONG_FIRST_FLAG) != 0; 11003949bee8SArchie Cobbs frag->last = (lhdr & MP_LONG_LAST_FLAG) != 0; 1101a9b3dca5SArchie Cobbs diff = MP_LONG_SEQ_DIFF(frag->seq, priv->mseq); 11023949bee8SArchie Cobbs m_adj(m, 4); 11033949bee8SArchie Cobbs } 11043949bee8SArchie Cobbs frag->data = m; 11053949bee8SArchie Cobbs frag->meta = meta; 1106a9b3dca5SArchie Cobbs getmicrouptime(&frag->timestamp); 11073949bee8SArchie Cobbs 1108a9b3dca5SArchie Cobbs /* If sequence number is < MSEQ, we've already declared this 1109a9b3dca5SArchie Cobbs fragment as lost, so we have no choice now but to drop it */ 1110a9b3dca5SArchie Cobbs if (diff < 0) { 1111a9b3dca5SArchie Cobbs link->stats.dropFragments++; 1112a9b3dca5SArchie Cobbs NG_FREE_DATA(m, meta); 1113a9b3dca5SArchie Cobbs return (0); 1114a9b3dca5SArchie Cobbs } 11153949bee8SArchie Cobbs 1116a9b3dca5SArchie Cobbs /* Update highest received sequence number on this link and MSEQ */ 1117a9b3dca5SArchie Cobbs priv->mseq = link->seq = frag->seq; 1118a9b3dca5SArchie Cobbs for (i = 0; i < priv->numActiveLinks; i++) { 1119a9b3dca5SArchie Cobbs struct ng_ppp_link *const alink = 1120a9b3dca5SArchie Cobbs &priv->links[priv->activeLinks[i]]; 1121a9b3dca5SArchie Cobbs 11226f16db81SArchie Cobbs if (MP_RECV_SEQ_DIFF(priv, alink->seq, priv->mseq) < 0) 1123a9b3dca5SArchie Cobbs priv->mseq = alink->seq; 1124a9b3dca5SArchie Cobbs } 11253949bee8SArchie Cobbs 11263949bee8SArchie Cobbs /* Allocate a new frag struct for the queue */ 11273949bee8SArchie Cobbs MALLOC(frag, struct ng_ppp_frag *, sizeof(*frag), M_NETGRAPH, M_NOWAIT); 11283949bee8SArchie Cobbs if (frag == NULL) { 11293949bee8SArchie Cobbs NG_FREE_DATA(m, meta); 1130a9b3dca5SArchie Cobbs ng_ppp_frag_process(node); 11313949bee8SArchie Cobbs return (ENOMEM); 11323949bee8SArchie Cobbs } 11333949bee8SArchie Cobbs *frag = frag0; 11343949bee8SArchie Cobbs 1135a9b3dca5SArchie Cobbs /* Add fragment to queue, which is sorted by sequence number */ 11361e7a9f72SArchie Cobbs inserted = 0; 1137a9b3dca5SArchie Cobbs CIRCLEQ_FOREACH_REVERSE(qent, &priv->frags, f_qent) { 11386f16db81SArchie Cobbs diff = MP_RECV_SEQ_DIFF(priv, frag->seq, qent->seq); 11393949bee8SArchie Cobbs if (diff > 0) { 1140a9b3dca5SArchie Cobbs CIRCLEQ_INSERT_AFTER(&priv->frags, qent, frag, f_qent); 11411e7a9f72SArchie Cobbs inserted = 1; 11423949bee8SArchie Cobbs break; 11433949bee8SArchie Cobbs } else if (diff == 0) { /* should never happen! */ 1144a9b3dca5SArchie Cobbs link->stats.dupFragments++; 11453949bee8SArchie Cobbs NG_FREE_DATA(frag->data, frag->meta); 11463949bee8SArchie Cobbs FREE(frag, M_NETGRAPH); 11473949bee8SArchie Cobbs return (EINVAL); 11483949bee8SArchie Cobbs } 11493949bee8SArchie Cobbs } 11501e7a9f72SArchie Cobbs if (!inserted) 1151a9b3dca5SArchie Cobbs CIRCLEQ_INSERT_HEAD(&priv->frags, frag, f_qent); 1152a9b3dca5SArchie Cobbs priv->qlen++; 11533949bee8SArchie Cobbs 1154a9b3dca5SArchie Cobbs /* Process the queue */ 1155a9b3dca5SArchie Cobbs return ng_ppp_frag_process(node); 11563949bee8SArchie Cobbs } 11571e7a9f72SArchie Cobbs 1158a9b3dca5SArchie Cobbs /* 1159a9b3dca5SArchie Cobbs * Examine our list of fragments, and determine if there is a 1160a9b3dca5SArchie Cobbs * complete and deliverable packet at the head of the list. 1161a9b3dca5SArchie Cobbs * Return 1 if so, zero otherwise. 1162a9b3dca5SArchie Cobbs */ 1163a9b3dca5SArchie Cobbs static int 1164a9b3dca5SArchie Cobbs ng_ppp_check_packet(node_p node) 1165a9b3dca5SArchie Cobbs { 1166a9b3dca5SArchie Cobbs const priv_p priv = node->private; 1167a9b3dca5SArchie Cobbs struct ng_ppp_frag *qent, *qnext; 11683949bee8SArchie Cobbs 1169a9b3dca5SArchie Cobbs /* Check for empty queue */ 1170a9b3dca5SArchie Cobbs if (CIRCLEQ_EMPTY(&priv->frags)) 1171a9b3dca5SArchie Cobbs return (0); 1172a9b3dca5SArchie Cobbs 1173a9b3dca5SArchie Cobbs /* Check first fragment is the start of a deliverable packet */ 1174a9b3dca5SArchie Cobbs qent = CIRCLEQ_FIRST(&priv->frags); 11756f16db81SArchie Cobbs if (!qent->first || MP_RECV_SEQ_DIFF(priv, qent->seq, priv->mseq) > 1) 1176a9b3dca5SArchie Cobbs return (0); 1177a9b3dca5SArchie Cobbs 1178a9b3dca5SArchie Cobbs /* Check that all the fragments are there */ 1179a9b3dca5SArchie Cobbs while (!qent->last) { 1180a9b3dca5SArchie Cobbs qnext = CIRCLEQ_NEXT(qent, f_qent); 1181a9b3dca5SArchie Cobbs if (qnext == (void *)&priv->frags) /* end of queue */ 1182a9b3dca5SArchie Cobbs return (0); 11836f16db81SArchie Cobbs if (qnext->seq != MP_NEXT_RECV_SEQ(priv, qent->seq)) 1184a9b3dca5SArchie Cobbs return (0); 1185a9b3dca5SArchie Cobbs qent = qnext; 1186a9b3dca5SArchie Cobbs } 1187a9b3dca5SArchie Cobbs 1188a9b3dca5SArchie Cobbs /* Got one */ 1189a9b3dca5SArchie Cobbs return (1); 1190a9b3dca5SArchie Cobbs } 1191a9b3dca5SArchie Cobbs 1192a9b3dca5SArchie Cobbs /* 1193a9b3dca5SArchie Cobbs * Pull a completed packet off the head of the incoming fragment queue. 1194a9b3dca5SArchie Cobbs * This assumes there is a completed packet there to pull off. 1195a9b3dca5SArchie Cobbs */ 1196a9b3dca5SArchie Cobbs static void 1197a9b3dca5SArchie Cobbs ng_ppp_get_packet(node_p node, struct mbuf **mp, meta_p *metap) 1198a9b3dca5SArchie Cobbs { 1199a9b3dca5SArchie Cobbs const priv_p priv = node->private; 1200a9b3dca5SArchie Cobbs struct ng_ppp_frag *qent, *qnext; 1201a9b3dca5SArchie Cobbs struct mbuf *m = NULL, *tail; 1202a9b3dca5SArchie Cobbs 1203a9b3dca5SArchie Cobbs qent = CIRCLEQ_FIRST(&priv->frags); 1204a9b3dca5SArchie Cobbs KASSERT(!CIRCLEQ_EMPTY(&priv->frags) && qent->first, 1205a9b3dca5SArchie Cobbs ("%s: no packet", __FUNCTION__)); 1206a9b3dca5SArchie Cobbs for (tail = NULL; qent != NULL; qent = qnext) { 1207a9b3dca5SArchie Cobbs qnext = CIRCLEQ_NEXT(qent, f_qent); 1208a9b3dca5SArchie Cobbs KASSERT(!CIRCLEQ_EMPTY(&priv->frags), 1209a9b3dca5SArchie Cobbs ("%s: empty q", __FUNCTION__)); 12103949bee8SArchie Cobbs CIRCLEQ_REMOVE(&priv->frags, qent, f_qent); 12113949bee8SArchie Cobbs if (tail == NULL) { 12123949bee8SArchie Cobbs tail = m = qent->data; 1213a9b3dca5SArchie Cobbs *metap = qent->meta; /* inherit first frag's meta */ 12143949bee8SArchie Cobbs } else { 12153949bee8SArchie Cobbs m->m_pkthdr.len += qent->data->m_pkthdr.len; 12163949bee8SArchie Cobbs tail->m_next = qent->data; 1217d690a6e7SArchie Cobbs NG_FREE_META(qent->meta); /* drop other frags' metas */ 12183949bee8SArchie Cobbs } 12193949bee8SArchie Cobbs while (tail->m_next != NULL) 12203949bee8SArchie Cobbs tail = tail->m_next; 1221a9b3dca5SArchie Cobbs if (qent->last) 12223949bee8SArchie Cobbs qnext = NULL; 12233949bee8SArchie Cobbs FREE(qent, M_NETGRAPH); 1224a9b3dca5SArchie Cobbs priv->qlen--; 1225a9b3dca5SArchie Cobbs } 1226a9b3dca5SArchie Cobbs *mp = m; 12273949bee8SArchie Cobbs } 12283949bee8SArchie Cobbs 1229a9b3dca5SArchie Cobbs /* 1230a9b3dca5SArchie Cobbs * Trim fragments from the queue whose packets can never be completed. 1231a9b3dca5SArchie Cobbs * This assumes a complete packet is NOT at the beginning of the queue. 1232a9b3dca5SArchie Cobbs * Returns 1 if fragments were removed, zero otherwise. 1233a9b3dca5SArchie Cobbs */ 1234a9b3dca5SArchie Cobbs static int 1235a9b3dca5SArchie Cobbs ng_ppp_frag_trim(node_p node) 1236a9b3dca5SArchie Cobbs { 1237a9b3dca5SArchie Cobbs const priv_p priv = node->private; 1238a9b3dca5SArchie Cobbs struct ng_ppp_frag *qent, *qnext = NULL; 1239a9b3dca5SArchie Cobbs int removed = 0; 1240a9b3dca5SArchie Cobbs 1241a9b3dca5SArchie Cobbs /* Scan for "dead" fragments and remove them */ 1242a9b3dca5SArchie Cobbs while (1) { 1243a9b3dca5SArchie Cobbs int dead = 0; 1244a9b3dca5SArchie Cobbs 1245a9b3dca5SArchie Cobbs /* If queue is empty, we're done */ 1246a9b3dca5SArchie Cobbs if (CIRCLEQ_EMPTY(&priv->frags)) 12473949bee8SArchie Cobbs break; 1248a9b3dca5SArchie Cobbs 1249a9b3dca5SArchie Cobbs /* Determine whether first fragment can ever be completed */ 1250a9b3dca5SArchie Cobbs CIRCLEQ_FOREACH(qent, &priv->frags, f_qent) { 12516f16db81SArchie Cobbs if (MP_RECV_SEQ_DIFF(priv, qent->seq, priv->mseq) >= 0) 1252a9b3dca5SArchie Cobbs break; 1253a9b3dca5SArchie Cobbs qnext = CIRCLEQ_NEXT(qent, f_qent); 1254a9b3dca5SArchie Cobbs KASSERT(qnext != (void*)&priv->frags, 1255a9b3dca5SArchie Cobbs ("%s: last frag < MSEQ?", __FUNCTION__)); 12566f16db81SArchie Cobbs if (qnext->seq != MP_NEXT_RECV_SEQ(priv, qent->seq) 1257a9b3dca5SArchie Cobbs || qent->last || qnext->first) { 1258a9b3dca5SArchie Cobbs dead = 1; 1259a9b3dca5SArchie Cobbs break; 1260a9b3dca5SArchie Cobbs } 1261a9b3dca5SArchie Cobbs } 1262a9b3dca5SArchie Cobbs if (!dead) 1263a9b3dca5SArchie Cobbs break; 1264a9b3dca5SArchie Cobbs 1265a9b3dca5SArchie Cobbs /* Remove fragment and all others in the same packet */ 1266a9b3dca5SArchie Cobbs while ((qent = CIRCLEQ_FIRST(&priv->frags)) != qnext) { 1267a9b3dca5SArchie Cobbs KASSERT(!CIRCLEQ_EMPTY(&priv->frags), 1268a9b3dca5SArchie Cobbs ("%s: empty q", __FUNCTION__)); 1269a9b3dca5SArchie Cobbs priv->bundleStats.dropFragments++; 12703949bee8SArchie Cobbs CIRCLEQ_REMOVE(&priv->frags, qent, f_qent); 12713949bee8SArchie Cobbs NG_FREE_DATA(qent->data, qent->meta); 12723949bee8SArchie Cobbs FREE(qent, M_NETGRAPH); 1273a9b3dca5SArchie Cobbs priv->qlen--; 1274a9b3dca5SArchie Cobbs removed = 1; 1275a9b3dca5SArchie Cobbs } 1276a9b3dca5SArchie Cobbs } 1277a9b3dca5SArchie Cobbs return (removed); 12783949bee8SArchie Cobbs } 12793949bee8SArchie Cobbs 1280a9b3dca5SArchie Cobbs /* 1281a9b3dca5SArchie Cobbs * Run the queue, restoring the queue invariants 1282a9b3dca5SArchie Cobbs */ 1283a9b3dca5SArchie Cobbs static int 1284a9b3dca5SArchie Cobbs ng_ppp_frag_process(node_p node) 1285a9b3dca5SArchie Cobbs { 1286a9b3dca5SArchie Cobbs const priv_p priv = node->private; 1287a9b3dca5SArchie Cobbs struct mbuf *m; 1288a9b3dca5SArchie Cobbs meta_p meta; 1289a9b3dca5SArchie Cobbs 1290a9b3dca5SArchie Cobbs /* Deliver any deliverable packets */ 1291a9b3dca5SArchie Cobbs while (ng_ppp_check_packet(node)) { 1292a9b3dca5SArchie Cobbs ng_ppp_get_packet(node, &m, &meta); 1293a9b3dca5SArchie Cobbs ng_ppp_input(node, 0, NG_PPP_BUNDLE_LINKNUM, m, meta); 1294a9b3dca5SArchie Cobbs } 1295a9b3dca5SArchie Cobbs 1296a9b3dca5SArchie Cobbs /* Delete dead fragments and try again */ 1297a9b3dca5SArchie Cobbs if (ng_ppp_frag_trim(node)) { 1298a9b3dca5SArchie Cobbs while (ng_ppp_check_packet(node)) { 1299a9b3dca5SArchie Cobbs ng_ppp_get_packet(node, &m, &meta); 1300a9b3dca5SArchie Cobbs ng_ppp_input(node, 0, NG_PPP_BUNDLE_LINKNUM, m, meta); 1301a9b3dca5SArchie Cobbs } 1302a9b3dca5SArchie Cobbs } 1303a9b3dca5SArchie Cobbs 1304a9b3dca5SArchie Cobbs /* Check for stale fragments while we're here */ 1305a9b3dca5SArchie Cobbs ng_ppp_frag_checkstale(node); 1306a9b3dca5SArchie Cobbs 1307a9b3dca5SArchie Cobbs /* Check queue length */ 1308a9b3dca5SArchie Cobbs if (priv->qlen > MP_MAX_QUEUE_LEN) { 1309a9b3dca5SArchie Cobbs struct ng_ppp_frag *qent; 1310a9b3dca5SArchie Cobbs int i; 1311a9b3dca5SArchie Cobbs 1312a9b3dca5SArchie Cobbs /* Get oldest fragment */ 1313a9b3dca5SArchie Cobbs KASSERT(!CIRCLEQ_EMPTY(&priv->frags), 1314a9b3dca5SArchie Cobbs ("%s: empty q", __FUNCTION__)); 1315a9b3dca5SArchie Cobbs qent = CIRCLEQ_FIRST(&priv->frags); 1316a9b3dca5SArchie Cobbs 1317a9b3dca5SArchie Cobbs /* Bump MSEQ if necessary */ 13186f16db81SArchie Cobbs if (MP_RECV_SEQ_DIFF(priv, priv->mseq, qent->seq) < 0) { 1319a9b3dca5SArchie Cobbs priv->mseq = qent->seq; 1320a9b3dca5SArchie Cobbs for (i = 0; i < priv->numActiveLinks; i++) { 1321a9b3dca5SArchie Cobbs struct ng_ppp_link *const alink = 1322a9b3dca5SArchie Cobbs &priv->links[priv->activeLinks[i]]; 1323a9b3dca5SArchie Cobbs 13246f16db81SArchie Cobbs if (MP_RECV_SEQ_DIFF(priv, 1325a9b3dca5SArchie Cobbs alink->seq, priv->mseq) < 0) 1326a9b3dca5SArchie Cobbs alink->seq = priv->mseq; 1327a9b3dca5SArchie Cobbs } 1328a9b3dca5SArchie Cobbs } 1329a9b3dca5SArchie Cobbs 1330a9b3dca5SArchie Cobbs /* Drop it */ 1331a9b3dca5SArchie Cobbs priv->bundleStats.dropFragments++; 1332a9b3dca5SArchie Cobbs CIRCLEQ_REMOVE(&priv->frags, qent, f_qent); 1333a9b3dca5SArchie Cobbs NG_FREE_DATA(qent->data, qent->meta); 1334a9b3dca5SArchie Cobbs FREE(qent, M_NETGRAPH); 1335a9b3dca5SArchie Cobbs priv->qlen--; 1336a9b3dca5SArchie Cobbs 1337a9b3dca5SArchie Cobbs /* Process queue again */ 1338a9b3dca5SArchie Cobbs return ng_ppp_frag_process(node); 1339a9b3dca5SArchie Cobbs } 1340a9b3dca5SArchie Cobbs 1341a9b3dca5SArchie Cobbs /* Done */ 1342a9b3dca5SArchie Cobbs return (0); 1343a9b3dca5SArchie Cobbs } 1344a9b3dca5SArchie Cobbs 1345a9b3dca5SArchie Cobbs /* 1346a9b3dca5SArchie Cobbs * Check for 'stale' completed packets that need to be delivered 1347a9b3dca5SArchie Cobbs * 1348a9b3dca5SArchie Cobbs * If a link goes down or has a temporary failure, MSEQ can get 1349a9b3dca5SArchie Cobbs * "stuck", because no new incoming fragments appear on that link. 1350a9b3dca5SArchie Cobbs * This can cause completed packets to never get delivered if 1351a9b3dca5SArchie Cobbs * their sequence numbers are all > MSEQ + 1. 1352a9b3dca5SArchie Cobbs * 1353a9b3dca5SArchie Cobbs * This routine checks how long all of the completed packets have 1354a9b3dca5SArchie Cobbs * been sitting in the queue, and if too long, removes fragments 1355a9b3dca5SArchie Cobbs * from the queue and increments MSEQ to allow them to be delivered. 1356a9b3dca5SArchie Cobbs */ 1357a9b3dca5SArchie Cobbs static void 1358a9b3dca5SArchie Cobbs ng_ppp_frag_checkstale(node_p node) 1359a9b3dca5SArchie Cobbs { 1360a9b3dca5SArchie Cobbs const priv_p priv = node->private; 1361a9b3dca5SArchie Cobbs struct ng_ppp_frag *qent, *beg, *end; 1362a9b3dca5SArchie Cobbs struct timeval now, age; 1363a9b3dca5SArchie Cobbs struct mbuf *m; 1364a9b3dca5SArchie Cobbs meta_p meta; 1365a9b3dca5SArchie Cobbs int i, seq; 1366a9b3dca5SArchie Cobbs 1367a9b3dca5SArchie Cobbs now.tv_sec = 0; /* uninitialized state */ 1368a9b3dca5SArchie Cobbs while (1) { 1369a9b3dca5SArchie Cobbs 1370a9b3dca5SArchie Cobbs /* If queue is empty, we're done */ 1371a9b3dca5SArchie Cobbs if (CIRCLEQ_EMPTY(&priv->frags)) 1372a9b3dca5SArchie Cobbs break; 1373a9b3dca5SArchie Cobbs 1374a9b3dca5SArchie Cobbs /* Find the first complete packet in the queue */ 1375a9b3dca5SArchie Cobbs beg = end = NULL; 1376a9b3dca5SArchie Cobbs seq = CIRCLEQ_FIRST(&priv->frags)->seq; 1377a9b3dca5SArchie Cobbs CIRCLEQ_FOREACH(qent, &priv->frags, f_qent) { 1378a9b3dca5SArchie Cobbs if (qent->first) 1379a9b3dca5SArchie Cobbs beg = qent; 1380a9b3dca5SArchie Cobbs else if (qent->seq != seq) 1381a9b3dca5SArchie Cobbs beg = NULL; 1382a9b3dca5SArchie Cobbs if (beg != NULL && qent->last) { 1383a9b3dca5SArchie Cobbs end = qent; 1384a9b3dca5SArchie Cobbs break; 1385a9b3dca5SArchie Cobbs } 13866f16db81SArchie Cobbs seq = MP_NEXT_RECV_SEQ(priv, seq); 1387a9b3dca5SArchie Cobbs } 1388a9b3dca5SArchie Cobbs 1389a9b3dca5SArchie Cobbs /* If none found, exit */ 1390a9b3dca5SArchie Cobbs if (end == NULL) 1391a9b3dca5SArchie Cobbs break; 1392a9b3dca5SArchie Cobbs 1393a9b3dca5SArchie Cobbs /* Get current time (we assume we've been up for >= 1 second) */ 1394a9b3dca5SArchie Cobbs if (now.tv_sec == 0) 1395a9b3dca5SArchie Cobbs getmicrouptime(&now); 1396a9b3dca5SArchie Cobbs 1397a9b3dca5SArchie Cobbs /* Check if packet has been queued too long */ 1398a9b3dca5SArchie Cobbs age = now; 1399a9b3dca5SArchie Cobbs timevalsub(&age, &beg->timestamp); 1400a9b3dca5SArchie Cobbs if (timevalcmp(&age, &ng_ppp_max_staleness, < )) 1401a9b3dca5SArchie Cobbs break; 1402a9b3dca5SArchie Cobbs 1403a9b3dca5SArchie Cobbs /* Throw away junk fragments in front of the completed packet */ 1404a9b3dca5SArchie Cobbs while ((qent = CIRCLEQ_FIRST(&priv->frags)) != beg) { 1405a9b3dca5SArchie Cobbs KASSERT(!CIRCLEQ_EMPTY(&priv->frags), 1406a9b3dca5SArchie Cobbs ("%s: empty q", __FUNCTION__)); 1407a9b3dca5SArchie Cobbs priv->bundleStats.dropFragments++; 1408a9b3dca5SArchie Cobbs CIRCLEQ_REMOVE(&priv->frags, qent, f_qent); 1409a9b3dca5SArchie Cobbs NG_FREE_DATA(qent->data, qent->meta); 1410a9b3dca5SArchie Cobbs FREE(qent, M_NETGRAPH); 1411a9b3dca5SArchie Cobbs priv->qlen--; 1412a9b3dca5SArchie Cobbs } 1413a9b3dca5SArchie Cobbs 1414a9b3dca5SArchie Cobbs /* Extract completed packet */ 1415a9b3dca5SArchie Cobbs ng_ppp_get_packet(node, &m, &meta); 1416a9b3dca5SArchie Cobbs 1417a9b3dca5SArchie Cobbs /* Bump MSEQ if necessary */ 14186f16db81SArchie Cobbs if (MP_RECV_SEQ_DIFF(priv, priv->mseq, end->seq) < 0) { 1419a9b3dca5SArchie Cobbs priv->mseq = end->seq; 1420a9b3dca5SArchie Cobbs for (i = 0; i < priv->numActiveLinks; i++) { 1421a9b3dca5SArchie Cobbs struct ng_ppp_link *const alink = 1422a9b3dca5SArchie Cobbs &priv->links[priv->activeLinks[i]]; 1423a9b3dca5SArchie Cobbs 14246f16db81SArchie Cobbs if (MP_RECV_SEQ_DIFF(priv, 1425a9b3dca5SArchie Cobbs alink->seq, priv->mseq) < 0) 1426a9b3dca5SArchie Cobbs alink->seq = priv->mseq; 1427a9b3dca5SArchie Cobbs } 1428a9b3dca5SArchie Cobbs } 1429a9b3dca5SArchie Cobbs 1430a9b3dca5SArchie Cobbs /* Deliver packet */ 1431a9b3dca5SArchie Cobbs ng_ppp_input(node, 0, NG_PPP_BUNDLE_LINKNUM, m, meta); 1432a9b3dca5SArchie Cobbs } 1433a9b3dca5SArchie Cobbs } 1434a9b3dca5SArchie Cobbs 1435a9b3dca5SArchie Cobbs /* 1436a9b3dca5SArchie Cobbs * Periodically call ng_ppp_frag_checkstale() 1437a9b3dca5SArchie Cobbs */ 1438a9b3dca5SArchie Cobbs static void 1439a9b3dca5SArchie Cobbs ng_ppp_frag_timeout(void *arg) 1440a9b3dca5SArchie Cobbs { 1441a9b3dca5SArchie Cobbs const node_p node = arg; 1442a9b3dca5SArchie Cobbs const priv_p priv = node->private; 1443a9b3dca5SArchie Cobbs int s = splnet(); 1444a9b3dca5SArchie Cobbs 1445a9b3dca5SArchie Cobbs /* Handle the race where shutdown happens just before splnet() above */ 1446a9b3dca5SArchie Cobbs if ((node->flags & NG_INVALID) != 0) { 1447a9b3dca5SArchie Cobbs ng_unref(node); 1448a9b3dca5SArchie Cobbs splx(s); 1449a9b3dca5SArchie Cobbs return; 1450a9b3dca5SArchie Cobbs } 1451a9b3dca5SArchie Cobbs 1452a9b3dca5SArchie Cobbs /* Reset timer state after timeout */ 1453a9b3dca5SArchie Cobbs KASSERT(priv->timerActive, ("%s: !timerActive", __FUNCTION__)); 1454a9b3dca5SArchie Cobbs priv->timerActive = 0; 1455a9b3dca5SArchie Cobbs KASSERT(node->refs > 1, ("%s: refs=%d", __FUNCTION__, node->refs)); 1456a9b3dca5SArchie Cobbs ng_unref(node); 1457a9b3dca5SArchie Cobbs 1458a9b3dca5SArchie Cobbs /* Start timer again */ 1459a9b3dca5SArchie Cobbs ng_ppp_start_frag_timer(node); 1460a9b3dca5SArchie Cobbs 1461a9b3dca5SArchie Cobbs /* Scan the fragment queue */ 1462a9b3dca5SArchie Cobbs ng_ppp_frag_checkstale(node); 1463a9b3dca5SArchie Cobbs splx(s); 14643949bee8SArchie Cobbs } 14653949bee8SArchie Cobbs 14663949bee8SArchie Cobbs /* 14673949bee8SArchie Cobbs * Deliver a frame out on the bundle, i.e., figure out how to fragment 14683949bee8SArchie Cobbs * the frame across the individual PPP links and do so. 14693949bee8SArchie Cobbs */ 14703949bee8SArchie Cobbs static int 14713949bee8SArchie Cobbs ng_ppp_mp_output(node_p node, struct mbuf *m, meta_p meta) 14723949bee8SArchie Cobbs { 14733949bee8SArchie Cobbs const priv_p priv = node->private; 14743949bee8SArchie Cobbs int distrib[NG_PPP_MAX_LINKS]; 14753949bee8SArchie Cobbs int firstFragment; 14763949bee8SArchie Cobbs int activeLinkNum; 14773949bee8SArchie Cobbs 14783949bee8SArchie Cobbs /* At least one link must be active */ 14793949bee8SArchie Cobbs if (priv->numActiveLinks == 0) { 14803949bee8SArchie Cobbs NG_FREE_DATA(m, meta); 14813949bee8SArchie Cobbs return (ENETDOWN); 14823949bee8SArchie Cobbs } 14833949bee8SArchie Cobbs 14843949bee8SArchie Cobbs /* Round-robin strategy */ 14853949bee8SArchie Cobbs if (priv->conf.enableRoundRobin || m->m_pkthdr.len < MP_MIN_FRAG_LEN) { 14863949bee8SArchie Cobbs activeLinkNum = priv->lastLink++ % priv->numActiveLinks; 14873949bee8SArchie Cobbs bzero(&distrib, priv->numActiveLinks * sizeof(distrib[0])); 14883949bee8SArchie Cobbs distrib[activeLinkNum] = m->m_pkthdr.len; 14893949bee8SArchie Cobbs goto deliver; 14903949bee8SArchie Cobbs } 14913949bee8SArchie Cobbs 14923949bee8SArchie Cobbs /* Strategy when all links are equivalent (optimize the common case) */ 14933949bee8SArchie Cobbs if (priv->allLinksEqual) { 14943949bee8SArchie Cobbs const int fraction = m->m_pkthdr.len / priv->numActiveLinks; 14953949bee8SArchie Cobbs int i, remain; 14963949bee8SArchie Cobbs 14973949bee8SArchie Cobbs for (i = 0; i < priv->numActiveLinks; i++) 14983949bee8SArchie Cobbs distrib[priv->lastLink++ % priv->numActiveLinks] 14993949bee8SArchie Cobbs = fraction; 15003949bee8SArchie Cobbs remain = m->m_pkthdr.len - (fraction * priv->numActiveLinks); 15013949bee8SArchie Cobbs while (remain > 0) { 15023949bee8SArchie Cobbs distrib[priv->lastLink++ % priv->numActiveLinks]++; 15033949bee8SArchie Cobbs remain--; 15043949bee8SArchie Cobbs } 15053949bee8SArchie Cobbs goto deliver; 15063949bee8SArchie Cobbs } 15073949bee8SArchie Cobbs 15083949bee8SArchie Cobbs /* Strategy when all links are not equivalent */ 15093949bee8SArchie Cobbs ng_ppp_mp_strategy(node, m->m_pkthdr.len, distrib); 15103949bee8SArchie Cobbs 15113949bee8SArchie Cobbs deliver: 15123949bee8SArchie Cobbs /* Update stats */ 15133949bee8SArchie Cobbs priv->bundleStats.xmitFrames++; 15143949bee8SArchie Cobbs priv->bundleStats.xmitOctets += m->m_pkthdr.len; 15153949bee8SArchie Cobbs 15163949bee8SArchie Cobbs /* Send alloted portions of frame out on the link(s) */ 15173949bee8SArchie Cobbs for (firstFragment = 1, activeLinkNum = priv->numActiveLinks - 1; 15183949bee8SArchie Cobbs activeLinkNum >= 0; activeLinkNum--) { 15193949bee8SArchie Cobbs const int linkNum = priv->activeLinks[activeLinkNum]; 1520a9b3dca5SArchie Cobbs struct ng_ppp_link *const link = &priv->links[linkNum]; 15213949bee8SArchie Cobbs 15223949bee8SArchie Cobbs /* Deliver fragment(s) out the next link */ 15233949bee8SArchie Cobbs for ( ; distrib[activeLinkNum] > 0; firstFragment = 0) { 15243949bee8SArchie Cobbs int len, lastFragment, error; 15253949bee8SArchie Cobbs struct mbuf *m2; 15263949bee8SArchie Cobbs meta_p meta2; 15273949bee8SArchie Cobbs 15283949bee8SArchie Cobbs /* Calculate fragment length; don't exceed link MTU */ 15293949bee8SArchie Cobbs len = distrib[activeLinkNum]; 1530a9b3dca5SArchie Cobbs if (len > link->conf.mru) 1531a9b3dca5SArchie Cobbs len = link->conf.mru; 15323949bee8SArchie Cobbs distrib[activeLinkNum] -= len; 15333949bee8SArchie Cobbs lastFragment = (len == m->m_pkthdr.len); 15343949bee8SArchie Cobbs 15353949bee8SArchie Cobbs /* Split off next fragment as "m2" */ 15363949bee8SArchie Cobbs m2 = m; 15373949bee8SArchie Cobbs if (!lastFragment) { 15383949bee8SArchie Cobbs struct mbuf *n = m_split(m, len, M_NOWAIT); 15393949bee8SArchie Cobbs 15403949bee8SArchie Cobbs if (n == NULL) { 15413949bee8SArchie Cobbs NG_FREE_DATA(m, meta); 15423949bee8SArchie Cobbs return (ENOMEM); 15433949bee8SArchie Cobbs } 15443949bee8SArchie Cobbs m = n; 15453949bee8SArchie Cobbs } 15463949bee8SArchie Cobbs 15473949bee8SArchie Cobbs /* Prepend MP header */ 15483949bee8SArchie Cobbs if (priv->conf.xmitShortSeq) { 15493949bee8SArchie Cobbs u_int16_t shdr; 15503949bee8SArchie Cobbs 1551a9b3dca5SArchie Cobbs shdr = priv->xseq; 1552a9b3dca5SArchie Cobbs priv->xseq = 15536f16db81SArchie Cobbs (priv->xseq + 1) & MP_SHORT_SEQ_MASK; 15543949bee8SArchie Cobbs if (firstFragment) 15553949bee8SArchie Cobbs shdr |= MP_SHORT_FIRST_FLAG; 15563949bee8SArchie Cobbs if (lastFragment) 15573949bee8SArchie Cobbs shdr |= MP_SHORT_LAST_FLAG; 1558d690a6e7SArchie Cobbs shdr = htons(shdr); 1559d690a6e7SArchie Cobbs m2 = ng_ppp_prepend(m2, &shdr, 2); 15603949bee8SArchie Cobbs } else { 15613949bee8SArchie Cobbs u_int32_t lhdr; 15623949bee8SArchie Cobbs 1563a9b3dca5SArchie Cobbs lhdr = priv->xseq; 1564a9b3dca5SArchie Cobbs priv->xseq = 15656f16db81SArchie Cobbs (priv->xseq + 1) & MP_LONG_SEQ_MASK; 15663949bee8SArchie Cobbs if (firstFragment) 15673949bee8SArchie Cobbs lhdr |= MP_LONG_FIRST_FLAG; 15683949bee8SArchie Cobbs if (lastFragment) 15693949bee8SArchie Cobbs lhdr |= MP_LONG_LAST_FLAG; 1570d690a6e7SArchie Cobbs lhdr = htonl(lhdr); 1571d690a6e7SArchie Cobbs m2 = ng_ppp_prepend(m2, &lhdr, 4); 15723949bee8SArchie Cobbs } 15733949bee8SArchie Cobbs if (m2 == NULL) { 15743949bee8SArchie Cobbs if (!lastFragment) 15753949bee8SArchie Cobbs m_freem(m); 15763949bee8SArchie Cobbs NG_FREE_META(meta); 15773949bee8SArchie Cobbs return (ENOBUFS); 15783949bee8SArchie Cobbs } 15793949bee8SArchie Cobbs 15803949bee8SArchie Cobbs /* Copy the meta information, if any */ 1581a9b3dca5SArchie Cobbs meta2 = lastFragment ? meta : ng_copy_meta(meta); 15823949bee8SArchie Cobbs 15833949bee8SArchie Cobbs /* Send fragment */ 158462838faeSArchie Cobbs error = ng_ppp_output(node, 0, 158562838faeSArchie Cobbs PROT_MP, linkNum, m2, meta2); 15863949bee8SArchie Cobbs if (error != 0) { 15873949bee8SArchie Cobbs if (!lastFragment) 15883949bee8SArchie Cobbs NG_FREE_DATA(m, meta); 15893949bee8SArchie Cobbs return (error); 15903949bee8SArchie Cobbs } 15913949bee8SArchie Cobbs } 15923949bee8SArchie Cobbs } 15933949bee8SArchie Cobbs 15943949bee8SArchie Cobbs /* Done */ 15953949bee8SArchie Cobbs return (0); 15963949bee8SArchie Cobbs } 15973949bee8SArchie Cobbs 15983949bee8SArchie Cobbs /* 15993949bee8SArchie Cobbs * Computing the optimal fragmentation 16003949bee8SArchie Cobbs * ----------------------------------- 16013949bee8SArchie Cobbs * 16023949bee8SArchie Cobbs * This routine tries to compute the optimal fragmentation pattern based 16033949bee8SArchie Cobbs * on each link's latency, bandwidth, and calculated additional latency. 16043949bee8SArchie Cobbs * The latter quantity is the additional latency caused by previously 16053949bee8SArchie Cobbs * written data that has not been transmitted yet. 16063949bee8SArchie Cobbs * 16073949bee8SArchie Cobbs * This algorithm is only useful when not all of the links have the 16083949bee8SArchie Cobbs * same latency and bandwidth values. 16093949bee8SArchie Cobbs * 16103949bee8SArchie Cobbs * The essential idea is to make the last bit of each fragment of the 16113949bee8SArchie Cobbs * frame arrive at the opposite end at the exact same time. This greedy 16123949bee8SArchie Cobbs * algorithm is optimal, in that no other scheduling could result in any 16133949bee8SArchie Cobbs * packet arriving any sooner unless packets are delivered out of order. 16143949bee8SArchie Cobbs * 16153949bee8SArchie Cobbs * Suppose link i has bandwidth b_i (in tens of bytes per milisecond) and 16163949bee8SArchie Cobbs * latency l_i (in miliseconds). Consider the function function f_i(t) 16173949bee8SArchie Cobbs * which is equal to the number of bytes that will have arrived at 16183949bee8SArchie Cobbs * the peer after t miliseconds if we start writing continuously at 16193949bee8SArchie Cobbs * time t = 0. Then f_i(t) = b_i * (t - l_i) = ((b_i * t) - (l_i * b_i). 16203949bee8SArchie Cobbs * That is, f_i(t) is a line with slope b_i and y-intersect -(l_i * b_i). 16213949bee8SArchie Cobbs * Note that the y-intersect is always <= zero because latency can't be 16223949bee8SArchie Cobbs * negative. Note also that really the function is f_i(t) except when 16233949bee8SArchie Cobbs * f_i(t) is negative, in which case the function is zero. To take 16243949bee8SArchie Cobbs * care of this, let Q_i(t) = { if (f_i(t) > 0) return 1; else return 0; }. 16253949bee8SArchie Cobbs * So the actual number of bytes that will have arrived at the peer after 16263949bee8SArchie Cobbs * t miliseconds is f_i(t) * Q_i(t). 16273949bee8SArchie Cobbs * 16283949bee8SArchie Cobbs * At any given time, each link has some additional latency a_i >= 0 16293949bee8SArchie Cobbs * due to previously written fragment(s) which are still in the queue. 16303949bee8SArchie Cobbs * This value is easily computed from the time since last transmission, 16313949bee8SArchie Cobbs * the previous latency value, the number of bytes written, and the 16323949bee8SArchie Cobbs * link's bandwidth. 16333949bee8SArchie Cobbs * 16343949bee8SArchie Cobbs * Assume that l_i includes any a_i already, and that the links are 16353949bee8SArchie Cobbs * sorted by latency, so that l_i <= l_{i+1}. 16363949bee8SArchie Cobbs * 16373949bee8SArchie Cobbs * Let N be the total number of bytes in the current frame we are sending. 16383949bee8SArchie Cobbs * 16393949bee8SArchie Cobbs * Suppose we were to start writing bytes at time t = 0 on all links 16403949bee8SArchie Cobbs * simultaneously, which is the most we can possibly do. Then let 16413949bee8SArchie Cobbs * F(t) be equal to the total number of bytes received by the peer 16423949bee8SArchie Cobbs * after t miliseconds. Then F(t) = Sum_i (f_i(t) * Q_i(t)). 16433949bee8SArchie Cobbs * 16443949bee8SArchie Cobbs * Our goal is simply this: fragment the frame across the links such 16453949bee8SArchie Cobbs * that the peer is able to reconstruct the completed frame as soon as 16463949bee8SArchie Cobbs * possible, i.e., at the least possible value of t. Call this value t_0. 16473949bee8SArchie Cobbs * 16483949bee8SArchie Cobbs * Then it follows that F(t_0) = N. Our strategy is first to find the value 16493949bee8SArchie Cobbs * of t_0, and then deduce how many bytes to write to each link. 16503949bee8SArchie Cobbs * 16513949bee8SArchie Cobbs * Rewriting F(t_0): 16523949bee8SArchie Cobbs * 16533949bee8SArchie Cobbs * t_0 = ( N + Sum_i ( l_i * b_i * Q_i(t_0) ) ) / Sum_i ( b_i * Q_i(t_0) ) 16543949bee8SArchie Cobbs * 16553949bee8SArchie Cobbs * Now, we note that Q_i(t) is constant for l_i <= t <= l_{i+1}. t_0 will 16563949bee8SArchie Cobbs * lie in one of these ranges. To find it, we just need to find the i such 16573949bee8SArchie Cobbs * that F(l_i) <= N <= F(l_{i+1}). Then we compute all the constant values 16583949bee8SArchie Cobbs * for Q_i() in this range, plug in the remaining values, solving for t_0. 16593949bee8SArchie Cobbs * 16603949bee8SArchie Cobbs * Once t_0 is known, then the number of bytes to send on link i is 16613949bee8SArchie Cobbs * just f_i(t_0) * Q_i(t_0). 16623949bee8SArchie Cobbs * 16633949bee8SArchie Cobbs * In other words, we start allocating bytes to the links one at a time. 16643949bee8SArchie Cobbs * We keep adding links until the frame is completely sent. Some links 16653949bee8SArchie Cobbs * may not get any bytes because their latency is too high. 16663949bee8SArchie Cobbs * 16673949bee8SArchie Cobbs * Is all this work really worth the trouble? Depends on the situation. 16683949bee8SArchie Cobbs * The bigger the ratio of computer speed to link speed, and the more 16693949bee8SArchie Cobbs * important total bundle latency is (e.g., for interactive response time), 16703949bee8SArchie Cobbs * the more it's worth it. There is however the cost of calling this 16713949bee8SArchie Cobbs * function for every frame. The running time is O(n^2) where n is the 16723949bee8SArchie Cobbs * number of links that receive a non-zero number of bytes. 16733949bee8SArchie Cobbs * 16743949bee8SArchie Cobbs * Since latency is measured in miliseconds, the "resolution" of this 16753949bee8SArchie Cobbs * algorithm is one milisecond. 16763949bee8SArchie Cobbs * 16773949bee8SArchie Cobbs * To avoid this algorithm altogether, configure all links to have the 16783949bee8SArchie Cobbs * same latency and bandwidth. 16793949bee8SArchie Cobbs */ 16803949bee8SArchie Cobbs static void 16813949bee8SArchie Cobbs ng_ppp_mp_strategy(node_p node, int len, int *distrib) 16823949bee8SArchie Cobbs { 16833949bee8SArchie Cobbs const priv_p priv = node->private; 16843949bee8SArchie Cobbs int latency[NG_PPP_MAX_LINKS]; 16853949bee8SArchie Cobbs int sortByLatency[NG_PPP_MAX_LINKS]; 1686a9b3dca5SArchie Cobbs int activeLinkNum; 16873949bee8SArchie Cobbs int t0, total, topSum, botSum; 16883949bee8SArchie Cobbs struct timeval now; 16893949bee8SArchie Cobbs int i, numFragments; 16903949bee8SArchie Cobbs 16913949bee8SArchie Cobbs /* If only one link, this gets real easy */ 16923949bee8SArchie Cobbs if (priv->numActiveLinks == 1) { 16933949bee8SArchie Cobbs distrib[0] = len; 16943949bee8SArchie Cobbs return; 16953949bee8SArchie Cobbs } 16963949bee8SArchie Cobbs 16973949bee8SArchie Cobbs /* Get current time */ 1698a9b3dca5SArchie Cobbs getmicrouptime(&now); 16993949bee8SArchie Cobbs 17003949bee8SArchie Cobbs /* Compute latencies for each link at this point in time */ 17013949bee8SArchie Cobbs for (activeLinkNum = 0; 17023949bee8SArchie Cobbs activeLinkNum < priv->numActiveLinks; activeLinkNum++) { 1703a9b3dca5SArchie Cobbs struct ng_ppp_link *alink; 17043949bee8SArchie Cobbs struct timeval diff; 17053949bee8SArchie Cobbs int xmitBytes; 17063949bee8SArchie Cobbs 17073949bee8SArchie Cobbs /* Start with base latency value */ 1708a9b3dca5SArchie Cobbs alink = &priv->links[priv->activeLinks[activeLinkNum]]; 1709a9b3dca5SArchie Cobbs latency[activeLinkNum] = alink->conf.latency; 17103949bee8SArchie Cobbs sortByLatency[activeLinkNum] = activeLinkNum; /* see below */ 17113949bee8SArchie Cobbs 17123949bee8SArchie Cobbs /* Any additional latency? */ 1713a9b3dca5SArchie Cobbs if (alink->bytesInQueue == 0) 17143949bee8SArchie Cobbs continue; 17153949bee8SArchie Cobbs 17163949bee8SArchie Cobbs /* Compute time delta since last write */ 17173949bee8SArchie Cobbs diff = now; 1718a9b3dca5SArchie Cobbs timevalsub(&diff, &alink->lastWrite); 17193949bee8SArchie Cobbs if (now.tv_sec < 0 || diff.tv_sec >= 10) { /* sanity */ 1720a9b3dca5SArchie Cobbs alink->bytesInQueue = 0; 17213949bee8SArchie Cobbs continue; 17223949bee8SArchie Cobbs } 17233949bee8SArchie Cobbs 17243949bee8SArchie Cobbs /* How many bytes could have transmitted since last write? */ 1725a9b3dca5SArchie Cobbs xmitBytes = (alink->conf.bandwidth * diff.tv_sec) 1726a9b3dca5SArchie Cobbs + (alink->conf.bandwidth * (diff.tv_usec / 1000)) / 100; 1727a9b3dca5SArchie Cobbs alink->bytesInQueue -= xmitBytes; 1728a9b3dca5SArchie Cobbs if (alink->bytesInQueue < 0) 1729a9b3dca5SArchie Cobbs alink->bytesInQueue = 0; 17303949bee8SArchie Cobbs else 17313949bee8SArchie Cobbs latency[activeLinkNum] += 1732a9b3dca5SArchie Cobbs (100 * alink->bytesInQueue) / alink->conf.bandwidth; 17333949bee8SArchie Cobbs } 17343949bee8SArchie Cobbs 1735a9b3dca5SArchie Cobbs /* Sort active links by latency */ 17363949bee8SArchie Cobbs compareLatencies = latency; 17373949bee8SArchie Cobbs qsort(sortByLatency, 17383949bee8SArchie Cobbs priv->numActiveLinks, sizeof(*sortByLatency), ng_ppp_intcmp); 17393949bee8SArchie Cobbs compareLatencies = NULL; 17403949bee8SArchie Cobbs 17413949bee8SArchie Cobbs /* Find the interval we need (add links in sortByLatency[] order) */ 17423949bee8SArchie Cobbs for (numFragments = 1; 17433949bee8SArchie Cobbs numFragments < priv->numActiveLinks; numFragments++) { 17443949bee8SArchie Cobbs for (total = i = 0; i < numFragments; i++) { 17453949bee8SArchie Cobbs int flowTime; 17463949bee8SArchie Cobbs 17473949bee8SArchie Cobbs flowTime = latency[sortByLatency[numFragments]] 17483949bee8SArchie Cobbs - latency[sortByLatency[i]]; 1749a9b3dca5SArchie Cobbs total += ((flowTime * priv->links[ 1750a9b3dca5SArchie Cobbs priv->activeLinks[sortByLatency[i]]].conf.bandwidth) 17513949bee8SArchie Cobbs + 99) / 100; 17523949bee8SArchie Cobbs } 17533949bee8SArchie Cobbs if (total >= len) 17543949bee8SArchie Cobbs break; 17553949bee8SArchie Cobbs } 17563949bee8SArchie Cobbs 17573949bee8SArchie Cobbs /* Solve for t_0 in that interval */ 17583949bee8SArchie Cobbs for (topSum = botSum = i = 0; i < numFragments; i++) { 1759a9b3dca5SArchie Cobbs int bw = priv->links[ 1760a9b3dca5SArchie Cobbs priv->activeLinks[sortByLatency[i]]].conf.bandwidth; 17613949bee8SArchie Cobbs 17623949bee8SArchie Cobbs topSum += latency[sortByLatency[i]] * bw; /* / 100 */ 17633949bee8SArchie Cobbs botSum += bw; /* / 100 */ 17643949bee8SArchie Cobbs } 17653949bee8SArchie Cobbs t0 = ((len * 100) + topSum + botSum / 2) / botSum; 17663949bee8SArchie Cobbs 17673949bee8SArchie Cobbs /* Compute f_i(t_0) all i */ 17683949bee8SArchie Cobbs bzero(distrib, priv->numActiveLinks * sizeof(*distrib)); 17693949bee8SArchie Cobbs for (total = i = 0; i < numFragments; i++) { 1770a9b3dca5SArchie Cobbs int bw = priv->links[ 1771a9b3dca5SArchie Cobbs priv->activeLinks[sortByLatency[i]]].conf.bandwidth; 17723949bee8SArchie Cobbs 17733949bee8SArchie Cobbs distrib[sortByLatency[i]] = 17743949bee8SArchie Cobbs (bw * (t0 - latency[sortByLatency[i]]) + 50) / 100; 17753949bee8SArchie Cobbs total += distrib[sortByLatency[i]]; 17763949bee8SArchie Cobbs } 17773949bee8SArchie Cobbs 1778fb1fc8abSArchie Cobbs /* Deal with any rounding error */ 1779fb1fc8abSArchie Cobbs if (total < len) { 1780a9b3dca5SArchie Cobbs struct ng_ppp_link *fastLink = 1781a9b3dca5SArchie Cobbs &priv->links[priv->activeLinks[sortByLatency[0]]]; 17823949bee8SArchie Cobbs int fast = 0; 17833949bee8SArchie Cobbs 1784fb1fc8abSArchie Cobbs /* Find the fastest link */ 17853949bee8SArchie Cobbs for (i = 1; i < numFragments; i++) { 1786a9b3dca5SArchie Cobbs struct ng_ppp_link *const link = 1787a9b3dca5SArchie Cobbs &priv->links[priv->activeLinks[sortByLatency[i]]]; 1788a9b3dca5SArchie Cobbs 1789a9b3dca5SArchie Cobbs if (link->conf.bandwidth > fastLink->conf.bandwidth) { 17903949bee8SArchie Cobbs fast = i; 1791a9b3dca5SArchie Cobbs fastLink = link; 1792a9b3dca5SArchie Cobbs } 17933949bee8SArchie Cobbs } 17943949bee8SArchie Cobbs distrib[sortByLatency[fast]] += len - total; 1795fb1fc8abSArchie Cobbs } else while (total > len) { 1796a9b3dca5SArchie Cobbs struct ng_ppp_link *slowLink = 1797a9b3dca5SArchie Cobbs &priv->links[priv->activeLinks[sortByLatency[0]]]; 1798fb1fc8abSArchie Cobbs int delta, slow = 0; 17993949bee8SArchie Cobbs 1800fb1fc8abSArchie Cobbs /* Find the slowest link that still has bytes to remove */ 1801fb1fc8abSArchie Cobbs for (i = 1; i < numFragments; i++) { 1802a9b3dca5SArchie Cobbs struct ng_ppp_link *const link = 1803a9b3dca5SArchie Cobbs &priv->links[priv->activeLinks[sortByLatency[i]]]; 1804a9b3dca5SArchie Cobbs 1805fb1fc8abSArchie Cobbs if (distrib[sortByLatency[slow]] == 0 1806fb1fc8abSArchie Cobbs || (distrib[sortByLatency[i]] > 0 1807a9b3dca5SArchie Cobbs && link->conf.bandwidth < 1808a9b3dca5SArchie Cobbs slowLink->conf.bandwidth)) { 1809fb1fc8abSArchie Cobbs slow = i; 1810a9b3dca5SArchie Cobbs slowLink = link; 1811a9b3dca5SArchie Cobbs } 1812fb1fc8abSArchie Cobbs } 1813fb1fc8abSArchie Cobbs delta = total - len; 1814fb1fc8abSArchie Cobbs if (delta > distrib[sortByLatency[slow]]) 1815fb1fc8abSArchie Cobbs delta = distrib[sortByLatency[slow]]; 1816fb1fc8abSArchie Cobbs distrib[sortByLatency[slow]] -= delta; 1817fb1fc8abSArchie Cobbs total -= delta; 18183949bee8SArchie Cobbs } 18193949bee8SArchie Cobbs } 18203949bee8SArchie Cobbs 18213949bee8SArchie Cobbs /* 18223949bee8SArchie Cobbs * Compare two integers 18233949bee8SArchie Cobbs */ 18243949bee8SArchie Cobbs static int 18253949bee8SArchie Cobbs ng_ppp_intcmp(const void *v1, const void *v2) 18263949bee8SArchie Cobbs { 18273949bee8SArchie Cobbs const int index1 = *((const int *) v1); 18283949bee8SArchie Cobbs const int index2 = *((const int *) v2); 18293949bee8SArchie Cobbs 18303949bee8SArchie Cobbs return compareLatencies[index1] - compareLatencies[index2]; 18313949bee8SArchie Cobbs } 18323949bee8SArchie Cobbs 18333949bee8SArchie Cobbs /* 18343949bee8SArchie Cobbs * Prepend a possibly compressed PPP protocol number in front of a frame 18353949bee8SArchie Cobbs */ 18363949bee8SArchie Cobbs static struct mbuf * 18373949bee8SArchie Cobbs ng_ppp_addproto(struct mbuf *m, int proto, int compOK) 18383949bee8SArchie Cobbs { 1839d690a6e7SArchie Cobbs if (compOK && PROT_COMPRESSABLE(proto)) { 1840d690a6e7SArchie Cobbs u_char pbyte = (u_char)proto; 18413949bee8SArchie Cobbs 1842d690a6e7SArchie Cobbs return ng_ppp_prepend(m, &pbyte, 1); 1843d690a6e7SArchie Cobbs } else { 1844d690a6e7SArchie Cobbs u_int16_t pword = htons((u_int16_t)proto); 1845d690a6e7SArchie Cobbs 1846d690a6e7SArchie Cobbs return ng_ppp_prepend(m, &pword, 2); 1847d690a6e7SArchie Cobbs } 1848d690a6e7SArchie Cobbs } 1849d690a6e7SArchie Cobbs 1850d690a6e7SArchie Cobbs /* 1851d690a6e7SArchie Cobbs * Prepend some bytes to an mbuf 1852d690a6e7SArchie Cobbs */ 1853d690a6e7SArchie Cobbs static struct mbuf * 1854d690a6e7SArchie Cobbs ng_ppp_prepend(struct mbuf *m, const void *buf, int len) 1855d690a6e7SArchie Cobbs { 1856d690a6e7SArchie Cobbs M_PREPEND(m, len, M_NOWAIT); 1857d690a6e7SArchie Cobbs if (m == NULL || (m->m_len < len && (m = m_pullup(m, len)) == NULL)) 18584cf49a43SJulian Elischer return (NULL); 1859d690a6e7SArchie Cobbs bcopy(buf, mtod(m, u_char *), len); 18603949bee8SArchie Cobbs return (m); 18613949bee8SArchie Cobbs } 18623949bee8SArchie Cobbs 18633949bee8SArchie Cobbs /* 18643949bee8SArchie Cobbs * Update private information that is derived from other private information 18653949bee8SArchie Cobbs */ 18663949bee8SArchie Cobbs static void 18673949bee8SArchie Cobbs ng_ppp_update(node_p node, int newConf) 18683949bee8SArchie Cobbs { 18693949bee8SArchie Cobbs const priv_p priv = node->private; 18703949bee8SArchie Cobbs int i; 18713949bee8SArchie Cobbs 18723949bee8SArchie Cobbs /* Update active status for VJ Compression */ 18733949bee8SArchie Cobbs priv->vjCompHooked = priv->hooks[HOOK_INDEX_VJC_IP] != NULL 18743949bee8SArchie Cobbs && priv->hooks[HOOK_INDEX_VJC_COMP] != NULL 18753949bee8SArchie Cobbs && priv->hooks[HOOK_INDEX_VJC_UNCOMP] != NULL 18763949bee8SArchie Cobbs && priv->hooks[HOOK_INDEX_VJC_VJIP] != NULL; 18773949bee8SArchie Cobbs 18783949bee8SArchie Cobbs /* Increase latency for each link an amount equal to one MP header */ 18793949bee8SArchie Cobbs if (newConf) { 18803949bee8SArchie Cobbs for (i = 0; i < NG_PPP_MAX_LINKS; i++) { 18813949bee8SArchie Cobbs int hdrBytes; 18823949bee8SArchie Cobbs 1883a9b3dca5SArchie Cobbs hdrBytes = (priv->links[i].conf.enableACFComp ? 0 : 2) 1884a9b3dca5SArchie Cobbs + (priv->links[i].conf.enableProtoComp ? 1 : 2) 18853949bee8SArchie Cobbs + (priv->conf.xmitShortSeq ? 2 : 4); 1886a9b3dca5SArchie Cobbs priv->links[i].conf.latency += 1887a9b3dca5SArchie Cobbs ((hdrBytes * priv->links[i].conf.bandwidth) + 50) 18883949bee8SArchie Cobbs / 100; 18893949bee8SArchie Cobbs } 18903949bee8SArchie Cobbs } 18913949bee8SArchie Cobbs 18923949bee8SArchie Cobbs /* Update list of active links */ 18933949bee8SArchie Cobbs bzero(&priv->activeLinks, sizeof(priv->activeLinks)); 18943949bee8SArchie Cobbs priv->numActiveLinks = 0; 18953949bee8SArchie Cobbs priv->allLinksEqual = 1; 18963949bee8SArchie Cobbs for (i = 0; i < NG_PPP_MAX_LINKS; i++) { 1897a9b3dca5SArchie Cobbs struct ng_ppp_link *const link = &priv->links[i]; 18980e11d0f3SArchie Cobbs 1899a9b3dca5SArchie Cobbs /* Is link active? */ 1900a9b3dca5SArchie Cobbs if (link->conf.enableLink && link->hook != NULL) { 1901a9b3dca5SArchie Cobbs struct ng_ppp_link *link0; 1902a9b3dca5SArchie Cobbs 1903a9b3dca5SArchie Cobbs /* Add link to list of active links */ 19043949bee8SArchie Cobbs priv->activeLinks[priv->numActiveLinks++] = i; 1905a9b3dca5SArchie Cobbs link0 = &priv->links[priv->activeLinks[0]]; 1906a9b3dca5SArchie Cobbs 1907a9b3dca5SArchie Cobbs /* Determine if all links are still equal */ 1908a9b3dca5SArchie Cobbs if (link->conf.latency != link0->conf.latency 1909a9b3dca5SArchie Cobbs || link->conf.bandwidth != link0->conf.bandwidth) 19103949bee8SArchie Cobbs priv->allLinksEqual = 0; 1911a9b3dca5SArchie Cobbs 1912a9b3dca5SArchie Cobbs /* Initialize rec'd sequence number */ 1913a9b3dca5SArchie Cobbs if (link->seq == MP_NOSEQ) { 1914a9b3dca5SArchie Cobbs link->seq = (link == link0) ? 1915a9b3dca5SArchie Cobbs MP_INITIAL_SEQ : link0->seq; 19163949bee8SArchie Cobbs } 1917a9b3dca5SArchie Cobbs } else 1918a9b3dca5SArchie Cobbs link->seq = MP_NOSEQ; 19193949bee8SArchie Cobbs } 19203949bee8SArchie Cobbs 1921a9b3dca5SArchie Cobbs /* Update MP state as multi-link is active or not */ 1922a9b3dca5SArchie Cobbs if (priv->conf.enableMultilink && priv->numActiveLinks > 0) 1923a9b3dca5SArchie Cobbs ng_ppp_start_frag_timer(node); 1924a9b3dca5SArchie Cobbs else { 1925a9b3dca5SArchie Cobbs ng_ppp_stop_frag_timer(node); 1926a9b3dca5SArchie Cobbs ng_ppp_frag_reset(node); 1927a9b3dca5SArchie Cobbs priv->xseq = MP_INITIAL_SEQ; 1928a9b3dca5SArchie Cobbs priv->mseq = MP_INITIAL_SEQ; 1929a9b3dca5SArchie Cobbs for (i = 0; i < NG_PPP_MAX_LINKS; i++) { 1930a9b3dca5SArchie Cobbs struct ng_ppp_link *const link = &priv->links[i]; 1931a9b3dca5SArchie Cobbs 1932a9b3dca5SArchie Cobbs bzero(&link->lastWrite, sizeof(link->lastWrite)); 1933a9b3dca5SArchie Cobbs link->bytesInQueue = 0; 1934a9b3dca5SArchie Cobbs link->seq = MP_NOSEQ; 1935a9b3dca5SArchie Cobbs } 19363949bee8SArchie Cobbs } 19373949bee8SArchie Cobbs } 19383949bee8SArchie Cobbs 19393949bee8SArchie Cobbs /* 19403949bee8SArchie Cobbs * Determine if a new configuration would represent a valid change 19413949bee8SArchie Cobbs * from the current configuration and link activity status. 19423949bee8SArchie Cobbs */ 19433949bee8SArchie Cobbs static int 1944a9b3dca5SArchie Cobbs ng_ppp_config_valid(node_p node, const struct ng_ppp_node_conf *newConf) 19453949bee8SArchie Cobbs { 19463949bee8SArchie Cobbs const priv_p priv = node->private; 19473949bee8SArchie Cobbs int i, newNumLinksActive; 19483949bee8SArchie Cobbs 19493949bee8SArchie Cobbs /* Check per-link config and count how many links would be active */ 19503949bee8SArchie Cobbs for (newNumLinksActive = i = 0; i < NG_PPP_MAX_LINKS; i++) { 1951a9b3dca5SArchie Cobbs if (newConf->links[i].enableLink && priv->links[i].hook != NULL) 1952e149c4e2SArchie Cobbs newNumLinksActive++; 1953e149c4e2SArchie Cobbs if (!newConf->links[i].enableLink) 1954e149c4e2SArchie Cobbs continue; 19553949bee8SArchie Cobbs if (newConf->links[i].mru < MP_MIN_LINK_MRU) 19563949bee8SArchie Cobbs return (0); 19573949bee8SArchie Cobbs if (newConf->links[i].bandwidth == 0) 19583949bee8SArchie Cobbs return (0); 19593949bee8SArchie Cobbs if (newConf->links[i].bandwidth > NG_PPP_MAX_BANDWIDTH) 19603949bee8SArchie Cobbs return (0); 19613949bee8SArchie Cobbs if (newConf->links[i].latency > NG_PPP_MAX_LATENCY) 19623949bee8SArchie Cobbs return (0); 19633949bee8SArchie Cobbs } 19643949bee8SArchie Cobbs 19653949bee8SArchie Cobbs /* Check bundle parameters */ 1966a9b3dca5SArchie Cobbs if (newConf->bund.enableMultilink && newConf->bund.mrru < MP_MIN_MRRU) 19673949bee8SArchie Cobbs return (0); 19683949bee8SArchie Cobbs 19693949bee8SArchie Cobbs /* Disallow changes to multi-link configuration while MP is active */ 19703949bee8SArchie Cobbs if (priv->numActiveLinks > 0 && newNumLinksActive > 0) { 1971a9b3dca5SArchie Cobbs if (!priv->conf.enableMultilink 1972a9b3dca5SArchie Cobbs != !newConf->bund.enableMultilink 1973a9b3dca5SArchie Cobbs || !priv->conf.xmitShortSeq != !newConf->bund.xmitShortSeq 1974a9b3dca5SArchie Cobbs || !priv->conf.recvShortSeq != !newConf->bund.recvShortSeq) 19753949bee8SArchie Cobbs return (0); 19763949bee8SArchie Cobbs } 19773949bee8SArchie Cobbs 1978e149c4e2SArchie Cobbs /* At most one link can be active unless multi-link is enabled */ 1979a9b3dca5SArchie Cobbs if (!newConf->bund.enableMultilink && newNumLinksActive > 1) 1980e149c4e2SArchie Cobbs return (0); 1981e149c4e2SArchie Cobbs 1982e149c4e2SArchie Cobbs /* Configuration change would be valid */ 19833949bee8SArchie Cobbs return (1); 19843949bee8SArchie Cobbs } 19853949bee8SArchie Cobbs 19863949bee8SArchie Cobbs /* 19873949bee8SArchie Cobbs * Free all entries in the fragment queue 19883949bee8SArchie Cobbs */ 19893949bee8SArchie Cobbs static void 1990a9b3dca5SArchie Cobbs ng_ppp_frag_reset(node_p node) 19913949bee8SArchie Cobbs { 19923949bee8SArchie Cobbs const priv_p priv = node->private; 19931e7a9f72SArchie Cobbs struct ng_ppp_frag *qent, *qnext; 19943949bee8SArchie Cobbs 19953949bee8SArchie Cobbs for (qent = CIRCLEQ_FIRST(&priv->frags); 19961e7a9f72SArchie Cobbs qent != (void *)&priv->frags; qent = qnext) { 19971e7a9f72SArchie Cobbs qnext = CIRCLEQ_NEXT(qent, f_qent); 19983949bee8SArchie Cobbs NG_FREE_DATA(qent->data, qent->meta); 19993949bee8SArchie Cobbs FREE(qent, M_NETGRAPH); 20003949bee8SArchie Cobbs } 20013949bee8SArchie Cobbs CIRCLEQ_INIT(&priv->frags); 2002a9b3dca5SArchie Cobbs priv->qlen = 0; 2003a9b3dca5SArchie Cobbs } 2004a9b3dca5SArchie Cobbs 2005a9b3dca5SArchie Cobbs /* 2006a9b3dca5SArchie Cobbs * Start fragment queue timer 2007a9b3dca5SArchie Cobbs */ 2008a9b3dca5SArchie Cobbs static void 2009a9b3dca5SArchie Cobbs ng_ppp_start_frag_timer(node_p node) 2010a9b3dca5SArchie Cobbs { 2011a9b3dca5SArchie Cobbs const priv_p priv = node->private; 2012a9b3dca5SArchie Cobbs 2013a9b3dca5SArchie Cobbs if (!priv->timerActive) { 2014a9b3dca5SArchie Cobbs priv->fragTimer = timeout(ng_ppp_frag_timeout, 2015a9b3dca5SArchie Cobbs node, MP_FRAGTIMER_INTERVAL); 2016a9b3dca5SArchie Cobbs priv->timerActive = 1; 2017a9b3dca5SArchie Cobbs node->refs++; 2018a9b3dca5SArchie Cobbs } 2019a9b3dca5SArchie Cobbs } 2020a9b3dca5SArchie Cobbs 2021a9b3dca5SArchie Cobbs /* 2022a9b3dca5SArchie Cobbs * Stop fragment queue timer 2023a9b3dca5SArchie Cobbs */ 2024a9b3dca5SArchie Cobbs static void 2025a9b3dca5SArchie Cobbs ng_ppp_stop_frag_timer(node_p node) 2026a9b3dca5SArchie Cobbs { 2027a9b3dca5SArchie Cobbs const priv_p priv = node->private; 2028a9b3dca5SArchie Cobbs 2029a9b3dca5SArchie Cobbs if (priv->timerActive) { 2030a9b3dca5SArchie Cobbs untimeout(ng_ppp_frag_timeout, node, priv->fragTimer); 2031a9b3dca5SArchie Cobbs priv->timerActive = 0; 2032a9b3dca5SArchie Cobbs KASSERT(node->refs > 1, 2033a9b3dca5SArchie Cobbs ("%s: refs=%d", __FUNCTION__, node->refs)); 2034a9b3dca5SArchie Cobbs ng_unref(node); 2035a9b3dca5SArchie Cobbs } 20364cf49a43SJulian Elischer } 20374cf49a43SJulian Elischer 2038