| Commit | Line | Data |
|---|---|---|
| b06ebda0 MD |
1 | /*- |
| 2 | * Copyright (c) 1996-2000 Whistle Communications, Inc. | |
| 3 | * All rights reserved. | |
| 4 | * | |
| 5 | * Subject to the following obligations and disclaimer of warranty, use and | |
| 6 | * redistribution of this software, in source or object code forms, with or | |
| 7 | * without modifications are expressly permitted by Whistle Communications; | |
| 8 | * provided, however, that: | |
| 9 | * 1. Any and all reproductions of the source or object code must include the | |
| 10 | * copyright notice above and the following disclaimer of warranties; and | |
| 11 | * 2. No rights are granted, in any manner or form, to use Whistle | |
| 12 | * Communications, Inc. trademarks, including the mark "WHISTLE | |
| 13 | * COMMUNICATIONS" on advertising, endorsements, or otherwise except as | |
| 14 | * such appears in the above copyright notice or in the software. | |
| 15 | * | |
| 16 | * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND | |
| 17 | * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO | |
| 18 | * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, | |
| 19 | * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF | |
| 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. | |
| 21 | * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY | |
| 22 | * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS | |
| 23 | * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. | |
| 24 | * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES | |
| 25 | * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING | |
| 26 | * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, | |
| 27 | * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR | |
| 28 | * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY | |
| 29 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 30 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
| 31 | * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY | |
| 32 | * OF SUCH DAMAGE. | |
| 33 | * | |
| 34 | * Copyright (c) 2007 Alexander Motin <mav@alkar.net> | |
| 35 | * All rights reserved. | |
| 36 | * | |
| 37 | * Redistribution and use in source and binary forms, with or without | |
| 38 | * modification, are permitted provided that the following conditions | |
| 39 | * are met: | |
| 40 | * 1. Redistributions of source code must retain the above copyright | |
| 41 | * notice unmodified, this list of conditions, and the following | |
| 42 | * disclaimer. | |
| 43 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 44 | * notice, this list of conditions and the following disclaimer in the | |
| 45 | * documentation and/or other materials provided with the distribution. | |
| 46 | * | |
| 47 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | |
| 48 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 49 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 50 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
| 51 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 52 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
| 53 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 54 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
| 55 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
| 56 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 57 | * SUCH DAMAGE. | |
| 58 | * | |
| 59 | * Authors: Archie Cobbs <archie@freebsd.org>, Alexander Motin <mav@alkar.net> | |
| 60 | * | |
| 61 | * $FreeBSD: src/sys/netgraph/ng_ppp.c,v 1.75 2008/02/06 20:37:34 mav Exp $ | |
| 5a975a3d | 62 | * $DragonFly: src/sys/netgraph7/ng_ppp.c,v 1.2 2008/06/26 23:05:35 dillon Exp $ |
| b06ebda0 MD |
63 | * $Whistle: ng_ppp.c,v 1.24 1999/11/01 09:24:52 julian Exp $ |
| 64 | */ | |
| 65 | ||
| 66 | /* | |
| 67 | * PPP node type data-flow. | |
| 68 | * | |
| 69 | * hook xmit layer recv hook | |
| 70 | * ------------------------------------ | |
| 71 | * inet -> -> inet | |
| 72 | * ipv6 -> -> ipv6 | |
| 73 | * ipx -> proto -> ipx | |
| 74 | * atalk -> -> atalk | |
| 75 | * bypass -> -> bypass | |
| 76 | * -hcomp_xmit()----------proto_recv()- | |
| 77 | * vjc_ip <- <- vjc_ip | |
| 78 | * vjc_comp -> header compression -> vjc_comp | |
| 79 | * vjc_uncomp -> -> vjc_uncomp | |
| 80 | * vjc_vjip -> | |
| 81 | * -comp_xmit()-----------hcomp_recv()- | |
| 82 | * compress <- compression <- decompress | |
| 83 | * compress -> -> decompress | |
| 84 | * -crypt_xmit()-----------comp_recv()- | |
| 85 | * encrypt <- encryption <- decrypt | |
| 86 | * encrypt -> -> decrypt | |
| 87 | * -ml_xmit()-------------crypt_recv()- | |
| 88 | * multilink | |
| 89 | * -link_xmit()--------------ml_recv()- | |
| 90 | * linkX <- link <- linkX | |
| 91 | * | |
| 92 | */ | |
| 93 | ||
| 94 | #include <sys/param.h> | |
| 95 | #include <sys/systm.h> | |
| 96 | #include <sys/kernel.h> | |
| 97 | #include <sys/limits.h> | |
| 98 | #include <sys/time.h> | |
| 99 | #include <sys/mbuf.h> | |
| 100 | #include <sys/malloc.h> | |
| 101 | #include <sys/errno.h> | |
| 102 | #include <sys/ctype.h> | |
| 103 | ||
| 5a975a3d MD |
104 | #include "ng_message.h" |
| 105 | #include "netgraph.h" | |
| 106 | #include "ng_parse.h" | |
| 107 | #include "ng_ppp.h" | |
| 108 | #include "ng_vjc.h" | |
| b06ebda0 MD |
109 | |
| 110 | #ifdef NG_SEPARATE_MALLOC | |
| 111 | MALLOC_DEFINE(M_NETGRAPH_PPP, "netgraph_ppp", "netgraph ppp node"); | |
| 112 | #else | |
| 113 | #define M_NETGRAPH_PPP M_NETGRAPH | |
| 114 | #endif | |
| 115 | ||
| 116 | #define PROT_VALID(p) (((p) & 0x0101) == 0x0001) | |
| 117 | #define PROT_COMPRESSABLE(p) (((p) & 0xff00) == 0x0000) | |
| 118 | ||
| 119 | /* Some PPP protocol numbers we're interested in */ | |
| 120 | #define PROT_ATALK 0x0029 | |
| 121 | #define PROT_COMPD 0x00fd | |
| 122 | #define PROT_CRYPTD 0x0053 | |
| 123 | #define PROT_IP 0x0021 | |
| 124 | #define PROT_IPV6 0x0057 | |
| 125 | #define PROT_IPX 0x002b | |
| 126 | #define PROT_LCP 0xc021 | |
| 127 | #define PROT_MP 0x003d | |
| 128 | #define PROT_VJCOMP 0x002d | |
| 129 | #define PROT_VJUNCOMP 0x002f | |
| 130 | ||
| 131 | /* Multilink PPP definitions */ | |
| 132 | #define MP_MIN_MRRU 1500 /* per RFC 1990 */ | |
| 133 | #define MP_INITIAL_SEQ 0 /* per RFC 1990 */ | |
| 134 | #define MP_MIN_LINK_MRU 32 | |
| 135 | ||
| 136 | #define MP_SHORT_SEQ_MASK 0x00000fff /* short seq # mask */ | |
| 137 | #define MP_SHORT_SEQ_HIBIT 0x00000800 /* short seq # high bit */ | |
| 138 | #define MP_SHORT_FIRST_FLAG 0x00008000 /* first fragment in frame */ | |
| 139 | #define MP_SHORT_LAST_FLAG 0x00004000 /* last fragment in frame */ | |
| 140 | ||
| 141 | #define MP_LONG_SEQ_MASK 0x00ffffff /* long seq # mask */ | |
| 142 | #define MP_LONG_SEQ_HIBIT 0x00800000 /* long seq # high bit */ | |
| 143 | #define MP_LONG_FIRST_FLAG 0x80000000 /* first fragment in frame */ | |
| 144 | #define MP_LONG_LAST_FLAG 0x40000000 /* last fragment in frame */ | |
| 145 | ||
| 146 | #define MP_NOSEQ 0x7fffffff /* impossible sequence number */ | |
| 147 | ||
| 148 | /* Sign extension of MP sequence numbers */ | |
| 149 | #define MP_SHORT_EXTEND(s) (((s) & MP_SHORT_SEQ_HIBIT) ? \ | |
| 150 | ((s) | ~MP_SHORT_SEQ_MASK) \ | |
| 151 | : ((s) & MP_SHORT_SEQ_MASK)) | |
| 152 | #define MP_LONG_EXTEND(s) (((s) & MP_LONG_SEQ_HIBIT) ? \ | |
| 153 | ((s) | ~MP_LONG_SEQ_MASK) \ | |
| 154 | : ((s) & MP_LONG_SEQ_MASK)) | |
| 155 | ||
| 156 | /* Comparision of MP sequence numbers. Note: all sequence numbers | |
| 157 | except priv->xseq are stored with the sign bit extended. */ | |
| 158 | #define MP_SHORT_SEQ_DIFF(x,y) MP_SHORT_EXTEND((x) - (y)) | |
| 159 | #define MP_LONG_SEQ_DIFF(x,y) MP_LONG_EXTEND((x) - (y)) | |
| 160 | ||
| 161 | #define MP_RECV_SEQ_DIFF(priv,x,y) \ | |
| 162 | ((priv)->conf.recvShortSeq ? \ | |
| 163 | MP_SHORT_SEQ_DIFF((x), (y)) : \ | |
| 164 | MP_LONG_SEQ_DIFF((x), (y))) | |
| 165 | ||
| 166 | /* Increment receive sequence number */ | |
| 167 | #define MP_NEXT_RECV_SEQ(priv,seq) \ | |
| 168 | ((priv)->conf.recvShortSeq ? \ | |
| 169 | MP_SHORT_EXTEND((seq) + 1) : \ | |
| 170 | MP_LONG_EXTEND((seq) + 1)) | |
| 171 | ||
| 172 | /* Don't fragment transmitted packets to parts smaller than this */ | |
| 173 | #define MP_MIN_FRAG_LEN 32 | |
| 174 | ||
| 175 | /* Maximum fragment reasssembly queue length */ | |
| 176 | #define MP_MAX_QUEUE_LEN 128 | |
| 177 | ||
| 178 | /* Fragment queue scanner period */ | |
| 179 | #define MP_FRAGTIMER_INTERVAL (hz/2) | |
| 180 | ||
| 181 | /* Average link overhead. XXX: Should be given by user-level */ | |
| 182 | #define MP_AVERAGE_LINK_OVERHEAD 16 | |
| 183 | ||
| 184 | /* Keep this equal to ng_ppp_hook_names lower! */ | |
| 185 | #define HOOK_INDEX_MAX 13 | |
| 186 | ||
| 187 | /* We store incoming fragments this way */ | |
| 188 | struct ng_ppp_frag { | |
| 189 | int seq; /* fragment seq# */ | |
| 190 | uint8_t first; /* First in packet? */ | |
| 191 | uint8_t last; /* Last in packet? */ | |
| 192 | struct timeval timestamp; /* time of reception */ | |
| 193 | struct mbuf *data; /* Fragment data */ | |
| 194 | TAILQ_ENTRY(ng_ppp_frag) f_qent; /* Fragment queue */ | |
| 195 | }; | |
| 196 | ||
| 197 | /* Per-link private information */ | |
| 198 | struct ng_ppp_link { | |
| 199 | struct ng_ppp_link_conf conf; /* link configuration */ | |
| 200 | struct ng_ppp_link_stat64 stats; /* link stats */ | |
| 201 | hook_p hook; /* connection to link data */ | |
| 202 | int32_t seq; /* highest rec'd seq# - MSEQ */ | |
| 203 | uint32_t latency; /* calculated link latency */ | |
| 204 | struct timeval lastWrite; /* time of last write for MP */ | |
| 205 | int bytesInQueue; /* bytes in the output queue for MP */ | |
| 206 | }; | |
| 207 | ||
| 208 | /* Total per-node private information */ | |
| 209 | struct ng_ppp_private { | |
| 210 | struct ng_ppp_bund_conf conf; /* bundle config */ | |
| 211 | struct ng_ppp_link_stat64 bundleStats; /* bundle stats */ | |
| 212 | struct ng_ppp_link links[NG_PPP_MAX_LINKS];/* per-link info */ | |
| 213 | int32_t xseq; /* next out MP seq # */ | |
| 214 | int32_t mseq; /* min links[i].seq */ | |
| 215 | uint16_t activeLinks[NG_PPP_MAX_LINKS]; /* indicies */ | |
| 216 | uint16_t numActiveLinks; /* how many links up */ | |
| 217 | uint16_t lastLink; /* for round robin */ | |
| 218 | uint8_t vjCompHooked; /* VJ comp hooked up? */ | |
| 219 | uint8_t allLinksEqual; /* all xmit the same? */ | |
| 220 | hook_p hooks[HOOK_INDEX_MAX]; /* non-link hooks */ | |
| 221 | struct ng_ppp_frag fragsmem[MP_MAX_QUEUE_LEN]; /* fragments storage */ | |
| 222 | TAILQ_HEAD(ng_ppp_fraglist, ng_ppp_frag) /* fragment queue */ | |
| 223 | frags; | |
| 224 | TAILQ_HEAD(ng_ppp_fragfreelist, ng_ppp_frag) /* free fragment queue */ | |
| 225 | fragsfree; | |
| 226 | struct callout fragTimer; /* fraq queue check */ | |
| 227 | struct mtx rmtx; /* recv mutex */ | |
| 228 | struct mtx xmtx; /* xmit mutex */ | |
| 229 | }; | |
| 230 | typedef struct ng_ppp_private *priv_p; | |
| 231 | ||
| 232 | /* Netgraph node methods */ | |
| 233 | static ng_constructor_t ng_ppp_constructor; | |
| 234 | static ng_rcvmsg_t ng_ppp_rcvmsg; | |
| 235 | static ng_shutdown_t ng_ppp_shutdown; | |
| 236 | static ng_newhook_t ng_ppp_newhook; | |
| 237 | static ng_rcvdata_t ng_ppp_rcvdata; | |
| 238 | static ng_disconnect_t ng_ppp_disconnect; | |
| 239 | ||
| 240 | static ng_rcvdata_t ng_ppp_rcvdata_inet; | |
| 241 | static ng_rcvdata_t ng_ppp_rcvdata_ipv6; | |
| 242 | static ng_rcvdata_t ng_ppp_rcvdata_ipx; | |
| 243 | static ng_rcvdata_t ng_ppp_rcvdata_atalk; | |
| 244 | static ng_rcvdata_t ng_ppp_rcvdata_bypass; | |
| 245 | ||
| 246 | static ng_rcvdata_t ng_ppp_rcvdata_vjc_ip; | |
| 247 | static ng_rcvdata_t ng_ppp_rcvdata_vjc_comp; | |
| 248 | static ng_rcvdata_t ng_ppp_rcvdata_vjc_uncomp; | |
| 249 | static ng_rcvdata_t ng_ppp_rcvdata_vjc_vjip; | |
| 250 | ||
| 251 | static ng_rcvdata_t ng_ppp_rcvdata_compress; | |
| 252 | static ng_rcvdata_t ng_ppp_rcvdata_decompress; | |
| 253 | ||
| 254 | static ng_rcvdata_t ng_ppp_rcvdata_encrypt; | |
| 255 | static ng_rcvdata_t ng_ppp_rcvdata_decrypt; | |
| 256 | ||
| 257 | /* We use integer indicies to refer to the non-link hooks. */ | |
| 258 | static const struct { | |
| 259 | char *const name; | |
| 260 | ng_rcvdata_t *fn; | |
| 261 | } ng_ppp_hook_names[] = { | |
| 262 | #define HOOK_INDEX_ATALK 0 | |
| 263 | { NG_PPP_HOOK_ATALK, ng_ppp_rcvdata_atalk }, | |
| 264 | #define HOOK_INDEX_BYPASS 1 | |
| 265 | { NG_PPP_HOOK_BYPASS, ng_ppp_rcvdata_bypass }, | |
| 266 | #define HOOK_INDEX_COMPRESS 2 | |
| 267 | { NG_PPP_HOOK_COMPRESS, ng_ppp_rcvdata_compress }, | |
| 268 | #define HOOK_INDEX_ENCRYPT 3 | |
| 269 | { NG_PPP_HOOK_ENCRYPT, ng_ppp_rcvdata_encrypt }, | |
| 270 | #define HOOK_INDEX_DECOMPRESS 4 | |
| 271 | { NG_PPP_HOOK_DECOMPRESS, ng_ppp_rcvdata_decompress }, | |
| 272 | #define HOOK_INDEX_DECRYPT 5 | |
| 273 | { NG_PPP_HOOK_DECRYPT, ng_ppp_rcvdata_decrypt }, | |
| 274 | #define HOOK_INDEX_INET 6 | |
| 275 | { NG_PPP_HOOK_INET, ng_ppp_rcvdata_inet }, | |
| 276 | #define HOOK_INDEX_IPX 7 | |
| 277 | { NG_PPP_HOOK_IPX, ng_ppp_rcvdata_ipx }, | |
| 278 | #define HOOK_INDEX_VJC_COMP 8 | |
| 279 | { NG_PPP_HOOK_VJC_COMP, ng_ppp_rcvdata_vjc_comp }, | |
| 280 | #define HOOK_INDEX_VJC_IP 9 | |
| 281 | { NG_PPP_HOOK_VJC_IP, ng_ppp_rcvdata_vjc_ip }, | |
| 282 | #define HOOK_INDEX_VJC_UNCOMP 10 | |
| 283 | { NG_PPP_HOOK_VJC_UNCOMP, ng_ppp_rcvdata_vjc_uncomp }, | |
| 284 | #define HOOK_INDEX_VJC_VJIP 11 | |
| 285 | { NG_PPP_HOOK_VJC_VJIP, ng_ppp_rcvdata_vjc_vjip }, | |
| 286 | #define HOOK_INDEX_IPV6 12 | |
| 287 | { NG_PPP_HOOK_IPV6, ng_ppp_rcvdata_ipv6 }, | |
| 288 | { NULL, NULL } | |
| 289 | }; | |
| 290 | ||
| 291 | /* Helper functions */ | |
| 292 | static int ng_ppp_proto_recv(node_p node, item_p item, uint16_t proto, | |
| 293 | uint16_t linkNum); | |
| 294 | static int ng_ppp_hcomp_xmit(node_p node, item_p item, uint16_t proto); | |
| 295 | static int ng_ppp_hcomp_recv(node_p node, item_p item, uint16_t proto, | |
| 296 | uint16_t linkNum); | |
| 297 | static int ng_ppp_comp_xmit(node_p node, item_p item, uint16_t proto); | |
| 298 | static int ng_ppp_comp_recv(node_p node, item_p item, uint16_t proto, | |
| 299 | uint16_t linkNum); | |
| 300 | static int ng_ppp_crypt_xmit(node_p node, item_p item, uint16_t proto); | |
| 301 | static int ng_ppp_crypt_recv(node_p node, item_p item, uint16_t proto, | |
| 302 | uint16_t linkNum); | |
| 303 | static int ng_ppp_mp_xmit(node_p node, item_p item, uint16_t proto); | |
| 304 | static int ng_ppp_mp_recv(node_p node, item_p item, uint16_t proto, | |
| 305 | uint16_t linkNum); | |
| 306 | static int ng_ppp_link_xmit(node_p node, item_p item, uint16_t proto, | |
| 307 | uint16_t linkNum, int plen); | |
| 308 | ||
| 309 | static int ng_ppp_bypass(node_p node, item_p item, uint16_t proto, | |
| 310 | uint16_t linkNum); | |
| 311 | ||
| 312 | static void ng_ppp_bump_mseq(node_p node, int32_t new_mseq); | |
| 313 | static int ng_ppp_frag_drop(node_p node); | |
| 314 | static int ng_ppp_check_packet(node_p node); | |
| 315 | static void ng_ppp_get_packet(node_p node, struct mbuf **mp); | |
| 316 | static int ng_ppp_frag_process(node_p node, item_p oitem); | |
| 317 | static int ng_ppp_frag_trim(node_p node); | |
| 318 | static void ng_ppp_frag_timeout(node_p node, hook_p hook, void *arg1, | |
| 319 | int arg2); | |
| 320 | static void ng_ppp_frag_checkstale(node_p node); | |
| 321 | static void ng_ppp_frag_reset(node_p node); | |
| 322 | static void ng_ppp_mp_strategy(node_p node, int len, int *distrib); | |
| 323 | static int ng_ppp_intcmp(void *latency, const void *v1, const void *v2); | |
| 324 | static struct mbuf *ng_ppp_addproto(struct mbuf *m, uint16_t proto, int compOK); | |
| 325 | static struct mbuf *ng_ppp_cutproto(struct mbuf *m, uint16_t *proto); | |
| 326 | static struct mbuf *ng_ppp_prepend(struct mbuf *m, const void *buf, int len); | |
| 327 | static int ng_ppp_config_valid(node_p node, | |
| 328 | const struct ng_ppp_node_conf *newConf); | |
| 329 | static void ng_ppp_update(node_p node, int newConf); | |
| 330 | static void ng_ppp_start_frag_timer(node_p node); | |
| 331 | static void ng_ppp_stop_frag_timer(node_p node); | |
| 332 | ||
| 333 | /* Parse type for struct ng_ppp_mp_state_type */ | |
| 334 | static const struct ng_parse_fixedarray_info ng_ppp_rseq_array_info = { | |
| 335 | &ng_parse_hint32_type, | |
| 336 | NG_PPP_MAX_LINKS | |
| 337 | }; | |
| 338 | static const struct ng_parse_type ng_ppp_rseq_array_type = { | |
| 339 | &ng_parse_fixedarray_type, | |
| 340 | &ng_ppp_rseq_array_info, | |
| 341 | }; | |
| 342 | static const struct ng_parse_struct_field ng_ppp_mp_state_type_fields[] | |
| 343 | = NG_PPP_MP_STATE_TYPE_INFO(&ng_ppp_rseq_array_type); | |
| 344 | static const struct ng_parse_type ng_ppp_mp_state_type = { | |
| 345 | &ng_parse_struct_type, | |
| 346 | &ng_ppp_mp_state_type_fields | |
| 347 | }; | |
| 348 | ||
| 349 | /* Parse type for struct ng_ppp_link_conf */ | |
| 350 | static const struct ng_parse_struct_field ng_ppp_link_type_fields[] | |
| 351 | = NG_PPP_LINK_TYPE_INFO; | |
| 352 | static const struct ng_parse_type ng_ppp_link_type = { | |
| 353 | &ng_parse_struct_type, | |
| 354 | &ng_ppp_link_type_fields | |
| 355 | }; | |
| 356 | ||
| 357 | /* Parse type for struct ng_ppp_bund_conf */ | |
| 358 | static const struct ng_parse_struct_field ng_ppp_bund_type_fields[] | |
| 359 | = NG_PPP_BUND_TYPE_INFO; | |
| 360 | static const struct ng_parse_type ng_ppp_bund_type = { | |
| 361 | &ng_parse_struct_type, | |
| 362 | &ng_ppp_bund_type_fields | |
| 363 | }; | |
| 364 | ||
| 365 | /* Parse type for struct ng_ppp_node_conf */ | |
| 366 | static const struct ng_parse_fixedarray_info ng_ppp_array_info = { | |
| 367 | &ng_ppp_link_type, | |
| 368 | NG_PPP_MAX_LINKS | |
| 369 | }; | |
| 370 | static const struct ng_parse_type ng_ppp_link_array_type = { | |
| 371 | &ng_parse_fixedarray_type, | |
| 372 | &ng_ppp_array_info, | |
| 373 | }; | |
| 374 | static const struct ng_parse_struct_field ng_ppp_conf_type_fields[] | |
| 375 | = NG_PPP_CONFIG_TYPE_INFO(&ng_ppp_bund_type, &ng_ppp_link_array_type); | |
| 376 | static const struct ng_parse_type ng_ppp_conf_type = { | |
| 377 | &ng_parse_struct_type, | |
| 378 | &ng_ppp_conf_type_fields | |
| 379 | }; | |
| 380 | ||
| 381 | /* Parse type for struct ng_ppp_link_stat */ | |
| 382 | static const struct ng_parse_struct_field ng_ppp_stats_type_fields[] | |
| 383 | = NG_PPP_STATS_TYPE_INFO; | |
| 384 | static const struct ng_parse_type ng_ppp_stats_type = { | |
| 385 | &ng_parse_struct_type, | |
| 386 | &ng_ppp_stats_type_fields | |
| 387 | }; | |
| 388 | ||
| 389 | /* Parse type for struct ng_ppp_link_stat64 */ | |
| 390 | static const struct ng_parse_struct_field ng_ppp_stats64_type_fields[] | |
| 391 | = NG_PPP_STATS64_TYPE_INFO; | |
| 392 | static const struct ng_parse_type ng_ppp_stats64_type = { | |
| 393 | &ng_parse_struct_type, | |
| 394 | &ng_ppp_stats64_type_fields | |
| 395 | }; | |
| 396 | ||
| 397 | /* List of commands and how to convert arguments to/from ASCII */ | |
| 398 | static const struct ng_cmdlist ng_ppp_cmds[] = { | |
| 399 | { | |
| 400 | NGM_PPP_COOKIE, | |
| 401 | NGM_PPP_SET_CONFIG, | |
| 402 | "setconfig", | |
| 403 | &ng_ppp_conf_type, | |
| 404 | NULL | |
| 405 | }, | |
| 406 | { | |
| 407 | NGM_PPP_COOKIE, | |
| 408 | NGM_PPP_GET_CONFIG, | |
| 409 | "getconfig", | |
| 410 | NULL, | |
| 411 | &ng_ppp_conf_type | |
| 412 | }, | |
| 413 | { | |
| 414 | NGM_PPP_COOKIE, | |
| 415 | NGM_PPP_GET_MP_STATE, | |
| 416 | "getmpstate", | |
| 417 | NULL, | |
| 418 | &ng_ppp_mp_state_type | |
| 419 | }, | |
| 420 | { | |
| 421 | NGM_PPP_COOKIE, | |
| 422 | NGM_PPP_GET_LINK_STATS, | |
| 423 | "getstats", | |
| 424 | &ng_parse_int16_type, | |
| 425 | &ng_ppp_stats_type | |
| 426 | }, | |
| 427 | { | |
| 428 | NGM_PPP_COOKIE, | |
| 429 | NGM_PPP_CLR_LINK_STATS, | |
| 430 | "clrstats", | |
| 431 | &ng_parse_int16_type, | |
| 432 | NULL | |
| 433 | }, | |
| 434 | { | |
| 435 | NGM_PPP_COOKIE, | |
| 436 | NGM_PPP_GETCLR_LINK_STATS, | |
| 437 | "getclrstats", | |
| 438 | &ng_parse_int16_type, | |
| 439 | &ng_ppp_stats_type | |
| 440 | }, | |
| 441 | { | |
| 442 | NGM_PPP_COOKIE, | |
| 443 | NGM_PPP_GET_LINK_STATS64, | |
| 444 | "getstats64", | |
| 445 | &ng_parse_int16_type, | |
| 446 | &ng_ppp_stats64_type | |
| 447 | }, | |
| 448 | { | |
| 449 | NGM_PPP_COOKIE, | |
| 450 | NGM_PPP_GETCLR_LINK_STATS64, | |
| 451 | "getclrstats64", | |
| 452 | &ng_parse_int16_type, | |
| 453 | &ng_ppp_stats64_type | |
| 454 | }, | |
| 455 | { 0 } | |
| 456 | }; | |
| 457 | ||
| 458 | /* Node type descriptor */ | |
| 459 | static struct ng_type ng_ppp_typestruct = { | |
| 460 | .version = NG_ABI_VERSION, | |
| 461 | .name = NG_PPP_NODE_TYPE, | |
| 462 | .constructor = ng_ppp_constructor, | |
| 463 | .rcvmsg = ng_ppp_rcvmsg, | |
| 464 | .shutdown = ng_ppp_shutdown, | |
| 465 | .newhook = ng_ppp_newhook, | |
| 466 | .rcvdata = ng_ppp_rcvdata, | |
| 467 | .disconnect = ng_ppp_disconnect, | |
| 468 | .cmdlist = ng_ppp_cmds, | |
| 469 | }; | |
| 470 | NETGRAPH_INIT(ppp, &ng_ppp_typestruct); | |
| 471 | ||
| 472 | /* Address and control field header */ | |
| 473 | static const uint8_t ng_ppp_acf[2] = { 0xff, 0x03 }; | |
| 474 | ||
| 475 | /* Maximum time we'll let a complete incoming packet sit in the queue */ | |
| 476 | static const struct timeval ng_ppp_max_staleness = { 2, 0 }; /* 2 seconds */ | |
| 477 | ||
| 478 | #define ERROUT(x) do { error = (x); goto done; } while (0) | |
| 479 | ||
| 480 | /************************************************************************ | |
| 481 | NETGRAPH NODE STUFF | |
| 482 | ************************************************************************/ | |
| 483 | ||
| 484 | /* | |
| 485 | * Node type constructor | |
| 486 | */ | |
| 487 | static int | |
| 488 | ng_ppp_constructor(node_p node) | |
| 489 | { | |
| 490 | priv_p priv; | |
| 491 | int i; | |
| 492 | ||
| 493 | /* Allocate private structure */ | |
| fc025606 SW |
494 | priv = kmalloc(sizeof(*priv), M_NETGRAPH_PPP, |
| 495 | M_WAITOK | M_NULLOK | M_ZERO); | |
| b06ebda0 MD |
496 | if (priv == NULL) |
| 497 | return (ENOMEM); | |
| 498 | ||
| 499 | NG_NODE_SET_PRIVATE(node, priv); | |
| 500 | ||
| 501 | /* Initialize state */ | |
| 502 | TAILQ_INIT(&priv->frags); | |
| 503 | TAILQ_INIT(&priv->fragsfree); | |
| 504 | for (i = 0; i < MP_MAX_QUEUE_LEN; i++) | |
| 505 | TAILQ_INSERT_TAIL(&priv->fragsfree, &priv->fragsmem[i], f_qent); | |
| 506 | for (i = 0; i < NG_PPP_MAX_LINKS; i++) | |
| 507 | priv->links[i].seq = MP_NOSEQ; | |
| 508 | ng_callout_init(&priv->fragTimer); | |
| 509 | ||
| 510 | mtx_init(&priv->rmtx, "ng_ppp_recv", NULL, MTX_DEF); | |
| 511 | mtx_init(&priv->xmtx, "ng_ppp_xmit", NULL, MTX_DEF); | |
| 512 | ||
| 513 | /* Done */ | |
| 514 | return (0); | |
| 515 | } | |
| 516 | ||
| 517 | /* | |
| 518 | * Give our OK for a hook to be added | |
| 519 | */ | |
| 520 | static int | |
| 521 | ng_ppp_newhook(node_p node, hook_p hook, const char *name) | |
| 522 | { | |
| 523 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 524 | hook_p *hookPtr = NULL; | |
| 525 | int linkNum = -1; | |
| 526 | int hookIndex = -1; | |
| 527 | ||
| 528 | /* Figure out which hook it is */ | |
| 529 | if (strncmp(name, NG_PPP_HOOK_LINK_PREFIX, /* a link hook? */ | |
| 530 | strlen(NG_PPP_HOOK_LINK_PREFIX)) == 0) { | |
| 531 | const char *cp; | |
| 532 | char *eptr; | |
| 533 | ||
| 534 | cp = name + strlen(NG_PPP_HOOK_LINK_PREFIX); | |
| 535 | if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0')) | |
| 536 | return (EINVAL); | |
| 537 | linkNum = (int)strtoul(cp, &eptr, 10); | |
| 538 | if (*eptr != '\0' || linkNum < 0 || linkNum >= NG_PPP_MAX_LINKS) | |
| 539 | return (EINVAL); | |
| 540 | hookPtr = &priv->links[linkNum].hook; | |
| 541 | hookIndex = ~linkNum; | |
| 542 | ||
| 543 | /* See if hook is already connected. */ | |
| 544 | if (*hookPtr != NULL) | |
| 545 | return (EISCONN); | |
| 546 | ||
| 547 | /* Disallow more than one link unless multilink is enabled. */ | |
| 548 | if (priv->links[linkNum].conf.enableLink && | |
| 549 | !priv->conf.enableMultilink && priv->numActiveLinks >= 1) | |
| 550 | return (ENODEV); | |
| 551 | ||
| 552 | } else { /* must be a non-link hook */ | |
| 553 | int i; | |
| 554 | ||
| 555 | for (i = 0; ng_ppp_hook_names[i].name != NULL; i++) { | |
| 556 | if (strcmp(name, ng_ppp_hook_names[i].name) == 0) { | |
| 557 | hookPtr = &priv->hooks[i]; | |
| 558 | hookIndex = i; | |
| 559 | break; | |
| 560 | } | |
| 561 | } | |
| 562 | if (ng_ppp_hook_names[i].name == NULL) | |
| 563 | return (EINVAL); /* no such hook */ | |
| 564 | ||
| 565 | /* See if hook is already connected */ | |
| 566 | if (*hookPtr != NULL) | |
| 567 | return (EISCONN); | |
| 568 | ||
| 569 | /* Every non-linkX hook have it's own function. */ | |
| 570 | NG_HOOK_SET_RCVDATA(hook, ng_ppp_hook_names[i].fn); | |
| 571 | } | |
| 572 | ||
| 573 | /* OK */ | |
| 574 | *hookPtr = hook; | |
| 575 | NG_HOOK_SET_PRIVATE(hook, (void *)(intptr_t)hookIndex); | |
| 576 | ng_ppp_update(node, 0); | |
| 577 | return (0); | |
| 578 | } | |
| 579 | ||
| 580 | /* | |
| 581 | * Receive a control message | |
| 582 | */ | |
| 583 | static int | |
| 584 | ng_ppp_rcvmsg(node_p node, item_p item, hook_p lasthook) | |
| 585 | { | |
| 586 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 587 | struct ng_mesg *resp = NULL; | |
| 588 | int error = 0; | |
| 589 | struct ng_mesg *msg; | |
| 590 | ||
| 591 | NGI_GET_MSG(item, msg); | |
| 592 | switch (msg->header.typecookie) { | |
| 593 | case NGM_PPP_COOKIE: | |
| 594 | switch (msg->header.cmd) { | |
| 595 | case NGM_PPP_SET_CONFIG: | |
| 596 | { | |
| 597 | struct ng_ppp_node_conf *const conf = | |
| 598 | (struct ng_ppp_node_conf *)msg->data; | |
| 599 | int i; | |
| 600 | ||
| 601 | /* Check for invalid or illegal config */ | |
| 602 | if (msg->header.arglen != sizeof(*conf)) | |
| 603 | ERROUT(EINVAL); | |
| 604 | if (!ng_ppp_config_valid(node, conf)) | |
| 605 | ERROUT(EINVAL); | |
| 606 | ||
| 607 | /* Copy config */ | |
| 608 | priv->conf = conf->bund; | |
| 609 | for (i = 0; i < NG_PPP_MAX_LINKS; i++) | |
| 610 | priv->links[i].conf = conf->links[i]; | |
| 611 | ng_ppp_update(node, 1); | |
| 612 | break; | |
| 613 | } | |
| 614 | case NGM_PPP_GET_CONFIG: | |
| 615 | { | |
| 616 | struct ng_ppp_node_conf *conf; | |
| 617 | int i; | |
| 618 | ||
| 5a975a3d | 619 | NG_MKRESPONSE(resp, msg, sizeof(*conf), M_WAITOK | M_NULLOK); |
| b06ebda0 MD |
620 | if (resp == NULL) |
| 621 | ERROUT(ENOMEM); | |
| 622 | conf = (struct ng_ppp_node_conf *)resp->data; | |
| 623 | conf->bund = priv->conf; | |
| 624 | for (i = 0; i < NG_PPP_MAX_LINKS; i++) | |
| 625 | conf->links[i] = priv->links[i].conf; | |
| 626 | break; | |
| 627 | } | |
| 628 | case NGM_PPP_GET_MP_STATE: | |
| 629 | { | |
| 630 | struct ng_ppp_mp_state *info; | |
| 631 | int i; | |
| 632 | ||
| 5a975a3d | 633 | NG_MKRESPONSE(resp, msg, sizeof(*info), M_WAITOK | M_NULLOK); |
| b06ebda0 MD |
634 | if (resp == NULL) |
| 635 | ERROUT(ENOMEM); | |
| 636 | info = (struct ng_ppp_mp_state *)resp->data; | |
| 637 | bzero(info, sizeof(*info)); | |
| 638 | for (i = 0; i < NG_PPP_MAX_LINKS; i++) { | |
| 639 | if (priv->links[i].seq != MP_NOSEQ) | |
| 640 | info->rseq[i] = priv->links[i].seq; | |
| 641 | } | |
| 642 | info->mseq = priv->mseq; | |
| 643 | info->xseq = priv->xseq; | |
| 644 | break; | |
| 645 | } | |
| 646 | case NGM_PPP_GET_LINK_STATS: | |
| 647 | case NGM_PPP_CLR_LINK_STATS: | |
| 648 | case NGM_PPP_GETCLR_LINK_STATS: | |
| 649 | case NGM_PPP_GET_LINK_STATS64: | |
| 650 | case NGM_PPP_GETCLR_LINK_STATS64: | |
| 651 | { | |
| 652 | struct ng_ppp_link_stat64 *stats; | |
| 653 | uint16_t linkNum; | |
| 654 | ||
| 655 | /* Process request. */ | |
| 656 | if (msg->header.arglen != sizeof(uint16_t)) | |
| 657 | ERROUT(EINVAL); | |
| 658 | linkNum = *((uint16_t *) msg->data); | |
| 659 | if (linkNum >= NG_PPP_MAX_LINKS | |
| 660 | && linkNum != NG_PPP_BUNDLE_LINKNUM) | |
| 661 | ERROUT(EINVAL); | |
| 662 | stats = (linkNum == NG_PPP_BUNDLE_LINKNUM) ? | |
| 663 | &priv->bundleStats : &priv->links[linkNum].stats; | |
| 664 | ||
| 665 | /* Make 64bit reply. */ | |
| 666 | if (msg->header.cmd == NGM_PPP_GET_LINK_STATS64 || | |
| 667 | msg->header.cmd == NGM_PPP_GETCLR_LINK_STATS64) { | |
| 668 | NG_MKRESPONSE(resp, msg, | |
| 5a975a3d | 669 | sizeof(struct ng_ppp_link_stat64), M_WAITOK | M_NULLOK); |
| b06ebda0 MD |
670 | if (resp == NULL) |
| 671 | ERROUT(ENOMEM); | |
| 672 | bcopy(stats, resp->data, sizeof(*stats)); | |
| 673 | } else | |
| 674 | /* Make 32bit reply. */ | |
| 675 | if (msg->header.cmd == NGM_PPP_GET_LINK_STATS || | |
| 676 | msg->header.cmd == NGM_PPP_GETCLR_LINK_STATS) { | |
| 677 | struct ng_ppp_link_stat *rs; | |
| 678 | NG_MKRESPONSE(resp, msg, | |
| 5a975a3d | 679 | sizeof(struct ng_ppp_link_stat), M_WAITOK | M_NULLOK); |
| b06ebda0 MD |
680 | if (resp == NULL) |
| 681 | ERROUT(ENOMEM); | |
| 682 | rs = (struct ng_ppp_link_stat *)resp->data; | |
| 683 | /* Truncate 64->32 bits. */ | |
| 684 | rs->xmitFrames = stats->xmitFrames; | |
| 685 | rs->xmitOctets = stats->xmitOctets; | |
| 686 | rs->recvFrames = stats->recvFrames; | |
| 687 | rs->recvOctets = stats->recvOctets; | |
| 688 | rs->badProtos = stats->badProtos; | |
| 689 | rs->runts = stats->runts; | |
| 690 | rs->dupFragments = stats->dupFragments; | |
| 691 | rs->dropFragments = stats->dropFragments; | |
| 692 | } | |
| 693 | /* Clear stats. */ | |
| 694 | if (msg->header.cmd != NGM_PPP_GET_LINK_STATS && | |
| 695 | msg->header.cmd != NGM_PPP_GET_LINK_STATS64) | |
| 696 | bzero(stats, sizeof(*stats)); | |
| 697 | break; | |
| 698 | } | |
| 699 | default: | |
| 700 | error = EINVAL; | |
| 701 | break; | |
| 702 | } | |
| 703 | break; | |
| 704 | case NGM_VJC_COOKIE: | |
| 705 | { | |
| 706 | /* | |
| 707 | * Forward it to the vjc node. leave the | |
| 708 | * old return address alone. | |
| 709 | * If we have no hook, let NG_RESPOND_MSG | |
| 710 | * clean up any remaining resources. | |
| 711 | * Because we have no resp, the item will be freed | |
| 712 | * along with anything it references. Don't | |
| 713 | * let msg be freed twice. | |
| 714 | */ | |
| 715 | NGI_MSG(item) = msg; /* put it back in the item */ | |
| 716 | msg = NULL; | |
| 717 | if ((lasthook = priv->hooks[HOOK_INDEX_VJC_IP])) { | |
| 718 | NG_FWD_ITEM_HOOK(error, item, lasthook); | |
| 719 | } | |
| 720 | return (error); | |
| 721 | } | |
| 722 | default: | |
| 723 | error = EINVAL; | |
| 724 | break; | |
| 725 | } | |
| 726 | done: | |
| 727 | NG_RESPOND_MSG(error, node, item, resp); | |
| 728 | NG_FREE_MSG(msg); | |
| 729 | return (error); | |
| 730 | } | |
| 731 | ||
| 732 | /* | |
| 733 | * Destroy node | |
| 734 | */ | |
| 735 | static int | |
| 736 | ng_ppp_shutdown(node_p node) | |
| 737 | { | |
| 738 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 739 | ||
| 740 | /* Stop fragment queue timer */ | |
| 741 | ng_ppp_stop_frag_timer(node); | |
| 742 | ||
| 743 | /* Take down netgraph node */ | |
| 744 | ng_ppp_frag_reset(node); | |
| 745 | mtx_destroy(&priv->rmtx); | |
| 746 | mtx_destroy(&priv->xmtx); | |
| 747 | bzero(priv, sizeof(*priv)); | |
| fc025606 | 748 | kfree(priv, M_NETGRAPH_PPP); |
| b06ebda0 MD |
749 | NG_NODE_SET_PRIVATE(node, NULL); |
| 750 | NG_NODE_UNREF(node); /* let the node escape */ | |
| 751 | return (0); | |
| 752 | } | |
| 753 | ||
| 754 | /* | |
| 755 | * Hook disconnection | |
| 756 | */ | |
| 757 | static int | |
| 758 | ng_ppp_disconnect(hook_p hook) | |
| 759 | { | |
| 760 | const node_p node = NG_HOOK_NODE(hook); | |
| 761 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 762 | const int index = (intptr_t)NG_HOOK_PRIVATE(hook); | |
| 763 | ||
| 764 | /* Zero out hook pointer */ | |
| 765 | if (index < 0) | |
| 766 | priv->links[~index].hook = NULL; | |
| 767 | else | |
| 768 | priv->hooks[index] = NULL; | |
| 769 | ||
| 770 | /* Update derived info (or go away if no hooks left). */ | |
| 771 | if (NG_NODE_NUMHOOKS(node) > 0) | |
| 772 | ng_ppp_update(node, 0); | |
| 773 | else if (NG_NODE_IS_VALID(node)) | |
| 774 | ng_rmnode_self(node); | |
| 775 | ||
| 776 | return (0); | |
| 777 | } | |
| 778 | ||
| 779 | /* | |
| 780 | * Proto layer | |
| 781 | */ | |
| 782 | ||
| 783 | /* | |
| 784 | * Receive data on a hook inet. | |
| 785 | */ | |
| 786 | static int | |
| 787 | ng_ppp_rcvdata_inet(hook_p hook, item_p item) | |
| 788 | { | |
| 789 | const node_p node = NG_HOOK_NODE(hook); | |
| 790 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 791 | ||
| 792 | if (!priv->conf.enableIP) { | |
| 793 | NG_FREE_ITEM(item); | |
| 794 | return (ENXIO); | |
| 795 | } | |
| 796 | return (ng_ppp_hcomp_xmit(NG_HOOK_NODE(hook), item, PROT_IP)); | |
| 797 | } | |
| 798 | ||
| 799 | /* | |
| 800 | * Receive data on a hook ipv6. | |
| 801 | */ | |
| 802 | static int | |
| 803 | ng_ppp_rcvdata_ipv6(hook_p hook, item_p item) | |
| 804 | { | |
| 805 | const node_p node = NG_HOOK_NODE(hook); | |
| 806 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 807 | ||
| 808 | if (!priv->conf.enableIPv6) { | |
| 809 | NG_FREE_ITEM(item); | |
| 810 | return (ENXIO); | |
| 811 | } | |
| 812 | return (ng_ppp_hcomp_xmit(NG_HOOK_NODE(hook), item, PROT_IPV6)); | |
| 813 | } | |
| 814 | ||
| 815 | /* | |
| 816 | * Receive data on a hook atalk. | |
| 817 | */ | |
| 818 | static int | |
| 819 | ng_ppp_rcvdata_atalk(hook_p hook, item_p item) | |
| 820 | { | |
| 821 | const node_p node = NG_HOOK_NODE(hook); | |
| 822 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 823 | ||
| 824 | if (!priv->conf.enableAtalk) { | |
| 825 | NG_FREE_ITEM(item); | |
| 826 | return (ENXIO); | |
| 827 | } | |
| 828 | return (ng_ppp_hcomp_xmit(NG_HOOK_NODE(hook), item, PROT_ATALK)); | |
| 829 | } | |
| 830 | ||
| 831 | /* | |
| 832 | * Receive data on a hook ipx | |
| 833 | */ | |
| 834 | static int | |
| 835 | ng_ppp_rcvdata_ipx(hook_p hook, item_p item) | |
| 836 | { | |
| 837 | const node_p node = NG_HOOK_NODE(hook); | |
| 838 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 839 | ||
| 840 | if (!priv->conf.enableIPX) { | |
| 841 | NG_FREE_ITEM(item); | |
| 842 | return (ENXIO); | |
| 843 | } | |
| 844 | return (ng_ppp_hcomp_xmit(NG_HOOK_NODE(hook), item, PROT_IPX)); | |
| 845 | } | |
| 846 | ||
| 847 | /* | |
| 848 | * Receive data on a hook bypass | |
| 849 | */ | |
| 850 | static int | |
| 851 | ng_ppp_rcvdata_bypass(hook_p hook, item_p item) | |
| 852 | { | |
| 853 | uint16_t linkNum; | |
| 854 | uint16_t proto; | |
| 855 | struct mbuf *m; | |
| 856 | ||
| 857 | NGI_GET_M(item, m); | |
| 858 | if (m->m_pkthdr.len < 4) { | |
| 859 | NG_FREE_ITEM(item); | |
| 860 | return (EINVAL); | |
| 861 | } | |
| 862 | if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) { | |
| 863 | NG_FREE_ITEM(item); | |
| 864 | return (ENOBUFS); | |
| 865 | } | |
| 866 | linkNum = ntohs(mtod(m, uint16_t *)[0]); | |
| 867 | proto = ntohs(mtod(m, uint16_t *)[1]); | |
| 868 | m_adj(m, 4); | |
| 869 | NGI_M(item) = m; | |
| 870 | ||
| 871 | if (linkNum == NG_PPP_BUNDLE_LINKNUM) | |
| 872 | return (ng_ppp_hcomp_xmit(NG_HOOK_NODE(hook), item, proto)); | |
| 873 | else | |
| 874 | return (ng_ppp_link_xmit(NG_HOOK_NODE(hook), item, proto, | |
| 875 | linkNum, 0)); | |
| 876 | } | |
| 877 | ||
| 878 | static int | |
| 879 | ng_ppp_bypass(node_p node, item_p item, uint16_t proto, uint16_t linkNum) | |
| 880 | { | |
| 881 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 882 | uint16_t hdr[2]; | |
| 883 | struct mbuf *m; | |
| 884 | int error; | |
| 885 | ||
| 886 | if (priv->hooks[HOOK_INDEX_BYPASS] == NULL) { | |
| 887 | NG_FREE_ITEM(item); | |
| 888 | return (ENXIO); | |
| 889 | } | |
| 890 | ||
| 891 | /* Add 4-byte bypass header. */ | |
| 892 | hdr[0] = htons(linkNum); | |
| 893 | hdr[1] = htons(proto); | |
| 894 | ||
| 895 | NGI_GET_M(item, m); | |
| 896 | if ((m = ng_ppp_prepend(m, &hdr, 4)) == NULL) { | |
| 897 | NG_FREE_ITEM(item); | |
| 898 | return (ENOBUFS); | |
| 899 | } | |
| 900 | NGI_M(item) = m; | |
| 901 | ||
| 902 | /* Send packet out hook. */ | |
| 903 | NG_FWD_ITEM_HOOK(error, item, priv->hooks[HOOK_INDEX_BYPASS]); | |
| 904 | return (error); | |
| 905 | } | |
| 906 | ||
| 907 | static int | |
| 908 | ng_ppp_proto_recv(node_p node, item_p item, uint16_t proto, uint16_t linkNum) | |
| 909 | { | |
| 910 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 911 | hook_p outHook = NULL; | |
| 912 | int error; | |
| 913 | ||
| 914 | switch (proto) { | |
| 915 | case PROT_IP: | |
| 916 | if (priv->conf.enableIP) | |
| 917 | outHook = priv->hooks[HOOK_INDEX_INET]; | |
| 918 | break; | |
| 919 | case PROT_IPV6: | |
| 920 | if (priv->conf.enableIPv6) | |
| 921 | outHook = priv->hooks[HOOK_INDEX_IPV6]; | |
| 922 | break; | |
| 923 | case PROT_ATALK: | |
| 924 | if (priv->conf.enableAtalk) | |
| 925 | outHook = priv->hooks[HOOK_INDEX_ATALK]; | |
| 926 | break; | |
| 927 | case PROT_IPX: | |
| 928 | if (priv->conf.enableIPX) | |
| 929 | outHook = priv->hooks[HOOK_INDEX_IPX]; | |
| 930 | break; | |
| 931 | } | |
| 932 | ||
| 933 | if (outHook == NULL) | |
| 934 | return (ng_ppp_bypass(node, item, proto, linkNum)); | |
| 935 | ||
| 936 | /* Send packet out hook. */ | |
| 937 | NG_FWD_ITEM_HOOK(error, item, outHook); | |
| 938 | return (error); | |
| 939 | } | |
| 940 | ||
| 941 | /* | |
| 942 | * Header compression layer | |
| 943 | */ | |
| 944 | ||
| 945 | static int | |
| 946 | ng_ppp_hcomp_xmit(node_p node, item_p item, uint16_t proto) | |
| 947 | { | |
| 948 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 949 | ||
| 950 | if (proto == PROT_IP && | |
| 951 | priv->conf.enableVJCompression && | |
| 952 | priv->vjCompHooked) { | |
| 953 | int error; | |
| 954 | ||
| 955 | /* Send packet out hook. */ | |
| 956 | NG_FWD_ITEM_HOOK(error, item, priv->hooks[HOOK_INDEX_VJC_IP]); | |
| 957 | return (error); | |
| 958 | } | |
| 959 | ||
| 960 | return (ng_ppp_comp_xmit(node, item, proto)); | |
| 961 | } | |
| 962 | ||
| 963 | /* | |
| 964 | * Receive data on a hook vjc_comp. | |
| 965 | */ | |
| 966 | static int | |
| 967 | ng_ppp_rcvdata_vjc_comp(hook_p hook, item_p item) | |
| 968 | { | |
| 969 | const node_p node = NG_HOOK_NODE(hook); | |
| 970 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 971 | ||
| 972 | if (!priv->conf.enableVJCompression) { | |
| 973 | NG_FREE_ITEM(item); | |
| 974 | return (ENXIO); | |
| 975 | } | |
| 976 | return (ng_ppp_comp_xmit(node, item, PROT_VJCOMP)); | |
| 977 | } | |
| 978 | ||
| 979 | /* | |
| 980 | * Receive data on a hook vjc_uncomp. | |
| 981 | */ | |
| 982 | static int | |
| 983 | ng_ppp_rcvdata_vjc_uncomp(hook_p hook, item_p item) | |
| 984 | { | |
| 985 | const node_p node = NG_HOOK_NODE(hook); | |
| 986 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 987 | ||
| 988 | if (!priv->conf.enableVJCompression) { | |
| 989 | NG_FREE_ITEM(item); | |
| 990 | return (ENXIO); | |
| 991 | } | |
| 992 | return (ng_ppp_comp_xmit(node, item, PROT_VJUNCOMP)); | |
| 993 | } | |
| 994 | ||
| 995 | /* | |
| 996 | * Receive data on a hook vjc_vjip. | |
| 997 | */ | |
| 998 | static int | |
| 999 | ng_ppp_rcvdata_vjc_vjip(hook_p hook, item_p item) | |
| 1000 | { | |
| 1001 | const node_p node = NG_HOOK_NODE(hook); | |
| 1002 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 1003 | ||
| 1004 | if (!priv->conf.enableVJCompression) { | |
| 1005 | NG_FREE_ITEM(item); | |
| 1006 | return (ENXIO); | |
| 1007 | } | |
| 1008 | return (ng_ppp_comp_xmit(node, item, PROT_IP)); | |
| 1009 | } | |
| 1010 | ||
| 1011 | static int | |
| 1012 | ng_ppp_hcomp_recv(node_p node, item_p item, uint16_t proto, uint16_t linkNum) | |
| 1013 | { | |
| 1014 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 1015 | ||
| 1016 | if (priv->conf.enableVJDecompression && priv->vjCompHooked) { | |
| 1017 | hook_p outHook = NULL; | |
| 1018 | ||
| 1019 | switch (proto) { | |
| 1020 | case PROT_VJCOMP: | |
| 1021 | outHook = priv->hooks[HOOK_INDEX_VJC_COMP]; | |
| 1022 | break; | |
| 1023 | case PROT_VJUNCOMP: | |
| 1024 | outHook = priv->hooks[HOOK_INDEX_VJC_UNCOMP]; | |
| 1025 | break; | |
| 1026 | } | |
| 1027 | ||
| 1028 | if (outHook) { | |
| 1029 | int error; | |
| 1030 | ||
| 1031 | /* Send packet out hook. */ | |
| 1032 | NG_FWD_ITEM_HOOK(error, item, outHook); | |
| 1033 | return (error); | |
| 1034 | } | |
| 1035 | } | |
| 1036 | ||
| 1037 | return (ng_ppp_proto_recv(node, item, proto, linkNum)); | |
| 1038 | } | |
| 1039 | ||
| 1040 | /* | |
| 1041 | * Receive data on a hook vjc_ip. | |
| 1042 | */ | |
| 1043 | static int | |
| 1044 | ng_ppp_rcvdata_vjc_ip(hook_p hook, item_p item) | |
| 1045 | { | |
| 1046 | const node_p node = NG_HOOK_NODE(hook); | |
| 1047 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 1048 | ||
| 1049 | if (!priv->conf.enableVJDecompression) { | |
| 1050 | NG_FREE_ITEM(item); | |
| 1051 | return (ENXIO); | |
| 1052 | } | |
| 1053 | return (ng_ppp_proto_recv(node, item, PROT_IP, NG_PPP_BUNDLE_LINKNUM)); | |
| 1054 | } | |
| 1055 | ||
| 1056 | /* | |
| 1057 | * Compression layer | |
| 1058 | */ | |
| 1059 | ||
| 1060 | static int | |
| 1061 | ng_ppp_comp_xmit(node_p node, item_p item, uint16_t proto) | |
| 1062 | { | |
| 1063 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 1064 | ||
| 1065 | if (priv->conf.enableCompression && | |
| 1066 | proto < 0x4000 && | |
| 1067 | proto != PROT_COMPD && | |
| 1068 | proto != PROT_CRYPTD && | |
| 1069 | priv->hooks[HOOK_INDEX_COMPRESS] != NULL) { | |
| 1070 | struct mbuf *m; | |
| 1071 | int error; | |
| 1072 | ||
| 1073 | NGI_GET_M(item, m); | |
| 1074 | if ((m = ng_ppp_addproto(m, proto, 0)) == NULL) { | |
| 1075 | NG_FREE_ITEM(item); | |
| 1076 | return (ENOBUFS); | |
| 1077 | } | |
| 1078 | NGI_M(item) = m; | |
| 1079 | ||
| 1080 | /* Send packet out hook. */ | |
| 1081 | NG_FWD_ITEM_HOOK(error, item, priv->hooks[HOOK_INDEX_COMPRESS]); | |
| 1082 | return (error); | |
| 1083 | } | |
| 1084 | ||
| 1085 | return (ng_ppp_crypt_xmit(node, item, proto)); | |
| 1086 | } | |
| 1087 | ||
| 1088 | /* | |
| 1089 | * Receive data on a hook compress. | |
| 1090 | */ | |
| 1091 | static int | |
| 1092 | ng_ppp_rcvdata_compress(hook_p hook, item_p item) | |
| 1093 | { | |
| 1094 | const node_p node = NG_HOOK_NODE(hook); | |
| 1095 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 1096 | uint16_t proto; | |
| 1097 | ||
| 1098 | switch (priv->conf.enableCompression) { | |
| 1099 | case NG_PPP_COMPRESS_NONE: | |
| 1100 | NG_FREE_ITEM(item); | |
| 1101 | return (ENXIO); | |
| 1102 | case NG_PPP_COMPRESS_FULL: | |
| 1103 | { | |
| 1104 | struct mbuf *m; | |
| 1105 | ||
| 1106 | NGI_GET_M(item, m); | |
| 1107 | if ((m = ng_ppp_cutproto(m, &proto)) == NULL) { | |
| 1108 | NG_FREE_ITEM(item); | |
| 1109 | return (EIO); | |
| 1110 | } | |
| 1111 | NGI_M(item) = m; | |
| 1112 | if (!PROT_VALID(proto)) { | |
| 1113 | NG_FREE_ITEM(item); | |
| 1114 | return (EIO); | |
| 1115 | } | |
| 1116 | } | |
| 1117 | break; | |
| 1118 | default: | |
| 1119 | proto = PROT_COMPD; | |
| 1120 | break; | |
| 1121 | } | |
| 1122 | return (ng_ppp_crypt_xmit(node, item, proto)); | |
| 1123 | } | |
| 1124 | ||
| 1125 | static int | |
| 1126 | ng_ppp_comp_recv(node_p node, item_p item, uint16_t proto, uint16_t linkNum) | |
| 1127 | { | |
| 1128 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 1129 | ||
| 1130 | if (proto < 0x4000 && | |
| 1131 | ((proto == PROT_COMPD && priv->conf.enableDecompression) || | |
| 1132 | priv->conf.enableDecompression == NG_PPP_DECOMPRESS_FULL) && | |
| 1133 | priv->hooks[HOOK_INDEX_DECOMPRESS] != NULL) { | |
| 1134 | int error; | |
| 1135 | ||
| 1136 | if (priv->conf.enableDecompression == NG_PPP_DECOMPRESS_FULL) { | |
| 1137 | struct mbuf *m; | |
| 1138 | NGI_GET_M(item, m); | |
| 1139 | if ((m = ng_ppp_addproto(m, proto, 0)) == NULL) { | |
| 1140 | NG_FREE_ITEM(item); | |
| 1141 | return (EIO); | |
| 1142 | } | |
| 1143 | NGI_M(item) = m; | |
| 1144 | } | |
| 1145 | ||
| 1146 | /* Send packet out hook. */ | |
| 1147 | NG_FWD_ITEM_HOOK(error, item, | |
| 1148 | priv->hooks[HOOK_INDEX_DECOMPRESS]); | |
| 1149 | return (error); | |
| 1150 | } else if (proto == PROT_COMPD) { | |
| 1151 | /* Disabled protos MUST be silently discarded, but | |
| 1152 | * unsupported MUST not. Let user-level decide this. */ | |
| 1153 | return (ng_ppp_bypass(node, item, proto, linkNum)); | |
| 1154 | } | |
| 1155 | ||
| 1156 | return (ng_ppp_hcomp_recv(node, item, proto, linkNum)); | |
| 1157 | } | |
| 1158 | ||
| 1159 | /* | |
| 1160 | * Receive data on a hook decompress. | |
| 1161 | */ | |
| 1162 | static int | |
| 1163 | ng_ppp_rcvdata_decompress(hook_p hook, item_p item) | |
| 1164 | { | |
| 1165 | const node_p node = NG_HOOK_NODE(hook); | |
| 1166 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 1167 | uint16_t proto; | |
| 1168 | struct mbuf *m; | |
| 1169 | ||
| 1170 | if (!priv->conf.enableDecompression) { | |
| 1171 | NG_FREE_ITEM(item); | |
| 1172 | return (ENXIO); | |
| 1173 | } | |
| 1174 | NGI_GET_M(item, m); | |
| 1175 | if ((m = ng_ppp_cutproto(m, &proto)) == NULL) { | |
| 1176 | NG_FREE_ITEM(item); | |
| 1177 | return (EIO); | |
| 1178 | } | |
| 1179 | NGI_M(item) = m; | |
| 1180 | if (!PROT_VALID(proto)) { | |
| 1181 | priv->bundleStats.badProtos++; | |
| 1182 | NG_FREE_ITEM(item); | |
| 1183 | return (EIO); | |
| 1184 | } | |
| 1185 | return (ng_ppp_hcomp_recv(node, item, proto, NG_PPP_BUNDLE_LINKNUM)); | |
| 1186 | } | |
| 1187 | ||
| 1188 | /* | |
| 1189 | * Encryption layer | |
| 1190 | */ | |
| 1191 | ||
| 1192 | static int | |
| 1193 | ng_ppp_crypt_xmit(node_p node, item_p item, uint16_t proto) | |
| 1194 | { | |
| 1195 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 1196 | ||
| 1197 | if (priv->conf.enableEncryption && | |
| 1198 | proto < 0x4000 && | |
| 1199 | proto != PROT_CRYPTD && | |
| 1200 | priv->hooks[HOOK_INDEX_ENCRYPT] != NULL) { | |
| 1201 | struct mbuf *m; | |
| 1202 | int error; | |
| 1203 | ||
| 1204 | NGI_GET_M(item, m); | |
| 1205 | if ((m = ng_ppp_addproto(m, proto, 0)) == NULL) { | |
| 1206 | NG_FREE_ITEM(item); | |
| 1207 | return (ENOBUFS); | |
| 1208 | } | |
| 1209 | NGI_M(item) = m; | |
| 1210 | ||
| 1211 | /* Send packet out hook. */ | |
| 1212 | NG_FWD_ITEM_HOOK(error, item, priv->hooks[HOOK_INDEX_ENCRYPT]); | |
| 1213 | return (error); | |
| 1214 | } | |
| 1215 | ||
| 1216 | return (ng_ppp_mp_xmit(node, item, proto)); | |
| 1217 | } | |
| 1218 | ||
| 1219 | /* | |
| 1220 | * Receive data on a hook encrypt. | |
| 1221 | */ | |
| 1222 | static int | |
| 1223 | ng_ppp_rcvdata_encrypt(hook_p hook, item_p item) | |
| 1224 | { | |
| 1225 | const node_p node = NG_HOOK_NODE(hook); | |
| 1226 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 1227 | ||
| 1228 | if (!priv->conf.enableEncryption) { | |
| 1229 | NG_FREE_ITEM(item); | |
| 1230 | return (ENXIO); | |
| 1231 | } | |
| 1232 | return (ng_ppp_mp_xmit(node, item, PROT_CRYPTD)); | |
| 1233 | } | |
| 1234 | ||
| 1235 | static int | |
| 1236 | ng_ppp_crypt_recv(node_p node, item_p item, uint16_t proto, uint16_t linkNum) | |
| 1237 | { | |
| 1238 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 1239 | ||
| 1240 | if (proto == PROT_CRYPTD) { | |
| 1241 | if (priv->conf.enableDecryption && | |
| 1242 | priv->hooks[HOOK_INDEX_DECRYPT] != NULL) { | |
| 1243 | int error; | |
| 1244 | ||
| 1245 | /* Send packet out hook. */ | |
| 1246 | NG_FWD_ITEM_HOOK(error, item, | |
| 1247 | priv->hooks[HOOK_INDEX_DECRYPT]); | |
| 1248 | return (error); | |
| 1249 | } else { | |
| 1250 | /* Disabled protos MUST be silently discarded, but | |
| 1251 | * unsupported MUST not. Let user-level decide this. */ | |
| 1252 | return (ng_ppp_bypass(node, item, proto, linkNum)); | |
| 1253 | } | |
| 1254 | } | |
| 1255 | ||
| 1256 | return (ng_ppp_comp_recv(node, item, proto, linkNum)); | |
| 1257 | } | |
| 1258 | ||
| 1259 | /* | |
| 1260 | * Receive data on a hook decrypt. | |
| 1261 | */ | |
| 1262 | static int | |
| 1263 | ng_ppp_rcvdata_decrypt(hook_p hook, item_p item) | |
| 1264 | { | |
| 1265 | const node_p node = NG_HOOK_NODE(hook); | |
| 1266 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 1267 | uint16_t proto; | |
| 1268 | struct mbuf *m; | |
| 1269 | ||
| 1270 | if (!priv->conf.enableDecryption) { | |
| 1271 | NG_FREE_ITEM(item); | |
| 1272 | return (ENXIO); | |
| 1273 | } | |
| 1274 | NGI_GET_M(item, m); | |
| 1275 | if ((m = ng_ppp_cutproto(m, &proto)) == NULL) { | |
| 1276 | NG_FREE_ITEM(item); | |
| 1277 | return (EIO); | |
| 1278 | } | |
| 1279 | NGI_M(item) = m; | |
| 1280 | if (!PROT_VALID(proto)) { | |
| 1281 | priv->bundleStats.badProtos++; | |
| 1282 | NG_FREE_ITEM(item); | |
| 1283 | return (EIO); | |
| 1284 | } | |
| 1285 | return (ng_ppp_comp_recv(node, item, proto, NG_PPP_BUNDLE_LINKNUM)); | |
| 1286 | } | |
| 1287 | ||
| 1288 | /* | |
| 1289 | * Link layer | |
| 1290 | */ | |
| 1291 | ||
| 1292 | static int | |
| 1293 | ng_ppp_link_xmit(node_p node, item_p item, uint16_t proto, uint16_t linkNum, int plen) | |
| 1294 | { | |
| 1295 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 1296 | struct ng_ppp_link *link; | |
| 1297 | int len, error; | |
| 1298 | struct mbuf *m; | |
| 1299 | uint16_t mru; | |
| 1300 | ||
| 1301 | /* Check if link correct. */ | |
| 1302 | if (linkNum >= NG_PPP_MAX_LINKS) { | |
| 1303 | ERROUT(ENETDOWN); | |
| 1304 | } | |
| 1305 | ||
| 1306 | /* Get link pointer (optimization). */ | |
| 1307 | link = &priv->links[linkNum]; | |
| 1308 | ||
| 1309 | /* Check link status (if real). */ | |
| 1310 | if (link->hook == NULL) { | |
| 1311 | ERROUT(ENETDOWN); | |
| 1312 | } | |
| 1313 | ||
| 1314 | /* Extract mbuf. */ | |
| 1315 | NGI_GET_M(item, m); | |
| 1316 | ||
| 1317 | /* Check peer's MRU for this link. */ | |
| 1318 | mru = link->conf.mru; | |
| 1319 | if (mru != 0 && m->m_pkthdr.len > mru) { | |
| 1320 | NG_FREE_M(m); | |
| 1321 | ERROUT(EMSGSIZE); | |
| 1322 | } | |
| 1323 | ||
| 1324 | /* Prepend protocol number, possibly compressed. */ | |
| 1325 | if ((m = ng_ppp_addproto(m, proto, link->conf.enableProtoComp)) == | |
| 1326 | NULL) { | |
| 1327 | ERROUT(ENOBUFS); | |
| 1328 | } | |
| 1329 | ||
| 1330 | /* Prepend address and control field (unless compressed). */ | |
| 1331 | if (proto == PROT_LCP || !link->conf.enableACFComp) { | |
| 1332 | if ((m = ng_ppp_prepend(m, &ng_ppp_acf, 2)) == NULL) | |
| 1333 | ERROUT(ENOBUFS); | |
| 1334 | } | |
| 1335 | ||
| 1336 | /* Deliver frame. */ | |
| 1337 | len = m->m_pkthdr.len; | |
| 1338 | NG_FWD_NEW_DATA(error, item, link->hook, m); | |
| 1339 | ||
| 1340 | mtx_lock(&priv->xmtx); | |
| 1341 | ||
| 1342 | /* Update link stats. */ | |
| 1343 | link->stats.xmitFrames++; | |
| 1344 | link->stats.xmitOctets += len; | |
| 1345 | ||
| 1346 | /* Update bundle stats. */ | |
| 1347 | if (plen > 0) { | |
| 1348 | priv->bundleStats.xmitFrames++; | |
| 1349 | priv->bundleStats.xmitOctets += plen; | |
| 1350 | } | |
| 1351 | ||
| 1352 | /* Update 'bytes in queue' counter. */ | |
| 1353 | if (error == 0) { | |
| 1354 | /* bytesInQueue and lastWrite required only for mp_strategy. */ | |
| 1355 | if (priv->conf.enableMultilink && !priv->allLinksEqual && | |
| 1356 | !priv->conf.enableRoundRobin) { | |
| 1357 | /* If queue was empty, then mark this time. */ | |
| 1358 | if (link->bytesInQueue == 0) | |
| 1359 | getmicrouptime(&link->lastWrite); | |
| 1360 | link->bytesInQueue += len + MP_AVERAGE_LINK_OVERHEAD; | |
| 1361 | /* Limit max queue length to 50 pkts. BW can be defined | |
| 1362 | incorrectly and link may not signal overload. */ | |
| 1363 | if (link->bytesInQueue > 50 * 1600) | |
| 1364 | link->bytesInQueue = 50 * 1600; | |
| 1365 | } | |
| 1366 | } | |
| 1367 | mtx_unlock(&priv->xmtx); | |
| 1368 | return (error); | |
| 1369 | ||
| 1370 | done: | |
| 1371 | NG_FREE_ITEM(item); | |
| 1372 | return (error); | |
| 1373 | } | |
| 1374 | ||
| 1375 | /* | |
| 1376 | * Receive data on a hook linkX. | |
| 1377 | */ | |
| 1378 | static int | |
| 1379 | ng_ppp_rcvdata(hook_p hook, item_p item) | |
| 1380 | { | |
| 1381 | const node_p node = NG_HOOK_NODE(hook); | |
| 1382 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 1383 | const int index = (intptr_t)NG_HOOK_PRIVATE(hook); | |
| 1384 | const uint16_t linkNum = (uint16_t)~index; | |
| 1385 | struct ng_ppp_link * const link = &priv->links[linkNum]; | |
| 1386 | uint16_t proto; | |
| 1387 | struct mbuf *m; | |
| 1388 | int error = 0; | |
| 1389 | ||
| 1390 | KASSERT(linkNum < NG_PPP_MAX_LINKS, | |
| 1391 | ("%s: bogus index 0x%x", __func__, index)); | |
| 1392 | ||
| 1393 | NGI_GET_M(item, m); | |
| 1394 | ||
| 1395 | mtx_lock(&priv->rmtx); | |
| 1396 | ||
| 1397 | /* Stats */ | |
| 1398 | link->stats.recvFrames++; | |
| 1399 | link->stats.recvOctets += m->m_pkthdr.len; | |
| 1400 | ||
| 1401 | /* Strip address and control fields, if present. */ | |
| 1402 | if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) | |
| 1403 | ERROUT(ENOBUFS); | |
| 1404 | if (mtod(m, uint8_t *)[0] == 0xff && | |
| 1405 | mtod(m, uint8_t *)[1] == 0x03) | |
| 1406 | m_adj(m, 2); | |
| 1407 | ||
| 1408 | /* Get protocol number */ | |
| 1409 | if ((m = ng_ppp_cutproto(m, &proto)) == NULL) | |
| 1410 | ERROUT(ENOBUFS); | |
| 1411 | NGI_M(item) = m; /* Put changed m back into item. */ | |
| 1412 | ||
| 1413 | if (!PROT_VALID(proto)) { | |
| 1414 | link->stats.badProtos++; | |
| 1415 | ERROUT(EIO); | |
| 1416 | } | |
| 1417 | ||
| 1418 | /* LCP packets must go directly to bypass. */ | |
| 1419 | if (proto >= 0xB000) { | |
| 1420 | mtx_unlock(&priv->rmtx); | |
| 1421 | return (ng_ppp_bypass(node, item, proto, linkNum)); | |
| 1422 | } | |
| 1423 | ||
| 1424 | /* Other packets are denied on a disabled link. */ | |
| 1425 | if (!link->conf.enableLink) | |
| 1426 | ERROUT(ENXIO); | |
| 1427 | ||
| 1428 | /* Proceed to multilink layer. Mutex will be unlocked inside. */ | |
| 1429 | error = ng_ppp_mp_recv(node, item, proto, linkNum); | |
| 1430 | mtx_assert(&priv->rmtx, MA_NOTOWNED); | |
| 1431 | return (error); | |
| 1432 | ||
| 1433 | done: | |
| 1434 | mtx_unlock(&priv->rmtx); | |
| 1435 | NG_FREE_ITEM(item); | |
| 1436 | return (error); | |
| 1437 | } | |
| 1438 | ||
| 1439 | /* | |
| 1440 | * Multilink layer | |
| 1441 | */ | |
| 1442 | ||
| 1443 | /* | |
| 1444 | * Handle an incoming multi-link fragment | |
| 1445 | * | |
| 1446 | * The fragment reassembly algorithm is somewhat complex. This is mainly | |
| 1447 | * because we are required not to reorder the reconstructed packets, yet | |
| 1448 | * fragments are only guaranteed to arrive in order on a per-link basis. | |
| 1449 | * In other words, when we have a complete packet ready, but the previous | |
| 1450 | * packet is still incomplete, we have to decide between delivering the | |
| 1451 | * complete packet and throwing away the incomplete one, or waiting to | |
| 1452 | * see if the remainder of the incomplete one arrives, at which time we | |
| 1453 | * can deliver both packets, in order. | |
| 1454 | * | |
| 1455 | * This problem is exacerbated by "sequence number slew", which is when | |
| 1456 | * the sequence numbers coming in from different links are far apart from | |
| 1457 | * each other. In particular, certain unnamed equipment (*cough* Ascend) | |
| 1458 | * has been seen to generate sequence number slew of up to 10 on an ISDN | |
| 1459 | * 2B-channel MP link. There is nothing invalid about sequence number slew | |
| 1460 | * but it makes the reasssembly process have to work harder. | |
| 1461 | * | |
| 1462 | * However, the peer is required to transmit fragments in order on each | |
| 1463 | * link. That means if we define MSEQ as the minimum over all links of | |
| 1464 | * the highest sequence number received on that link, then we can always | |
| 1465 | * give up any hope of receiving a fragment with sequence number < MSEQ in | |
| 1466 | * the future (all of this using 'wraparound' sequence number space). | |
| 1467 | * Therefore we can always immediately throw away incomplete packets | |
| 1468 | * missing fragments with sequence numbers < MSEQ. | |
| 1469 | * | |
| 1470 | * Here is an overview of our algorithm: | |
| 1471 | * | |
| 1472 | * o Received fragments are inserted into a queue, for which we | |
| 1473 | * maintain these invariants between calls to this function: | |
| 1474 | * | |
| 1475 | * - Fragments are ordered in the queue by sequence number | |
| 1476 | * - If a complete packet is at the head of the queue, then | |
| 1477 | * the first fragment in the packet has seq# > MSEQ + 1 | |
| 1478 | * (otherwise, we could deliver it immediately) | |
| 1479 | * - If any fragments have seq# < MSEQ, then they are necessarily | |
| 1480 | * part of a packet whose missing seq#'s are all > MSEQ (otherwise, | |
| 1481 | * we can throw them away because they'll never be completed) | |
| 1482 | * - The queue contains at most MP_MAX_QUEUE_LEN fragments | |
| 1483 | * | |
| 1484 | * o We have a periodic timer that checks the queue for the first | |
| 1485 | * complete packet that has been sitting in the queue "too long". | |
| 1486 | * When one is detected, all previous (incomplete) fragments are | |
| 1487 | * discarded, their missing fragments are declared lost and MSEQ | |
| 1488 | * is increased. | |
| 1489 | * | |
| 1490 | * o If we recieve a fragment with seq# < MSEQ, we throw it away | |
| 1491 | * because we've already delcared it lost. | |
| 1492 | * | |
| 1493 | * This assumes linkNum != NG_PPP_BUNDLE_LINKNUM. | |
| 1494 | */ | |
| 1495 | static int | |
| 1496 | ng_ppp_mp_recv(node_p node, item_p item, uint16_t proto, uint16_t linkNum) | |
| 1497 | { | |
| 1498 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 1499 | struct ng_ppp_link *const link = &priv->links[linkNum]; | |
| 1500 | struct ng_ppp_frag *frag; | |
| 1501 | struct ng_ppp_frag *qent; | |
| 1502 | int i, diff, inserted; | |
| 1503 | struct mbuf *m; | |
| 1504 | int error = 0; | |
| 1505 | ||
| 1506 | if ((!priv->conf.enableMultilink) || proto != PROT_MP) { | |
| 1507 | /* Stats */ | |
| 1508 | priv->bundleStats.recvFrames++; | |
| 1509 | priv->bundleStats.recvOctets += NGI_M(item)->m_pkthdr.len; | |
| 1510 | ||
| 1511 | mtx_unlock(&priv->rmtx); | |
| 1512 | return (ng_ppp_crypt_recv(node, item, proto, linkNum)); | |
| 1513 | } | |
| 1514 | ||
| 1515 | NGI_GET_M(item, m); | |
| 1516 | ||
| 1517 | /* Get a new frag struct from the free queue */ | |
| 1518 | if ((frag = TAILQ_FIRST(&priv->fragsfree)) == NULL) { | |
| 1519 | printf("No free fragments headers in ng_ppp!\n"); | |
| 1520 | NG_FREE_M(m); | |
| 1521 | goto process; | |
| 1522 | } | |
| 1523 | ||
| 1524 | /* Extract fragment information from MP header */ | |
| 1525 | if (priv->conf.recvShortSeq) { | |
| 1526 | uint16_t shdr; | |
| 1527 | ||
| 1528 | if (m->m_pkthdr.len < 2) { | |
| 1529 | link->stats.runts++; | |
| 1530 | NG_FREE_M(m); | |
| 1531 | ERROUT(EINVAL); | |
| 1532 | } | |
| 1533 | if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) | |
| 1534 | ERROUT(ENOBUFS); | |
| 1535 | ||
| 1536 | shdr = ntohs(*mtod(m, uint16_t *)); | |
| 1537 | frag->seq = MP_SHORT_EXTEND(shdr); | |
| 1538 | frag->first = (shdr & MP_SHORT_FIRST_FLAG) != 0; | |
| 1539 | frag->last = (shdr & MP_SHORT_LAST_FLAG) != 0; | |
| 1540 | diff = MP_SHORT_SEQ_DIFF(frag->seq, priv->mseq); | |
| 1541 | m_adj(m, 2); | |
| 1542 | } else { | |
| 1543 | uint32_t lhdr; | |
| 1544 | ||
| 1545 | if (m->m_pkthdr.len < 4) { | |
| 1546 | link->stats.runts++; | |
| 1547 | NG_FREE_M(m); | |
| 1548 | ERROUT(EINVAL); | |
| 1549 | } | |
| 1550 | if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) | |
| 1551 | ERROUT(ENOBUFS); | |
| 1552 | ||
| 1553 | lhdr = ntohl(*mtod(m, uint32_t *)); | |
| 1554 | frag->seq = MP_LONG_EXTEND(lhdr); | |
| 1555 | frag->first = (lhdr & MP_LONG_FIRST_FLAG) != 0; | |
| 1556 | frag->last = (lhdr & MP_LONG_LAST_FLAG) != 0; | |
| 1557 | diff = MP_LONG_SEQ_DIFF(frag->seq, priv->mseq); | |
| 1558 | m_adj(m, 4); | |
| 1559 | } | |
| 1560 | frag->data = m; | |
| 1561 | getmicrouptime(&frag->timestamp); | |
| 1562 | ||
| 1563 | /* If sequence number is < MSEQ, we've already declared this | |
| 1564 | fragment as lost, so we have no choice now but to drop it */ | |
| 1565 | if (diff < 0) { | |
| 1566 | link->stats.dropFragments++; | |
| 1567 | NG_FREE_M(m); | |
| 1568 | ERROUT(0); | |
| 1569 | } | |
| 1570 | ||
| 1571 | /* Update highest received sequence number on this link and MSEQ */ | |
| 1572 | priv->mseq = link->seq = frag->seq; | |
| 1573 | for (i = 0; i < priv->numActiveLinks; i++) { | |
| 1574 | struct ng_ppp_link *const alink = | |
| 1575 | &priv->links[priv->activeLinks[i]]; | |
| 1576 | ||
| 1577 | if (MP_RECV_SEQ_DIFF(priv, alink->seq, priv->mseq) < 0) | |
| 1578 | priv->mseq = alink->seq; | |
| 1579 | } | |
| 1580 | ||
| 1581 | /* Remove frag struct from free queue. */ | |
| 1582 | TAILQ_REMOVE(&priv->fragsfree, frag, f_qent); | |
| 1583 | ||
| 1584 | /* Add fragment to queue, which is sorted by sequence number */ | |
| 1585 | inserted = 0; | |
| 1586 | TAILQ_FOREACH_REVERSE(qent, &priv->frags, ng_ppp_fraglist, f_qent) { | |
| 1587 | diff = MP_RECV_SEQ_DIFF(priv, frag->seq, qent->seq); | |
| 1588 | if (diff > 0) { | |
| 1589 | TAILQ_INSERT_AFTER(&priv->frags, qent, frag, f_qent); | |
| 1590 | inserted = 1; | |
| 1591 | break; | |
| 1592 | } else if (diff == 0) { /* should never happen! */ | |
| 1593 | link->stats.dupFragments++; | |
| 1594 | NG_FREE_M(frag->data); | |
| 1595 | TAILQ_INSERT_HEAD(&priv->fragsfree, frag, f_qent); | |
| 1596 | ERROUT(EINVAL); | |
| 1597 | } | |
| 1598 | } | |
| 1599 | if (!inserted) | |
| 1600 | TAILQ_INSERT_HEAD(&priv->frags, frag, f_qent); | |
| 1601 | ||
| 1602 | process: | |
| 1603 | /* Process the queue */ | |
| 1604 | /* NOTE: rmtx will be unlocked for sending time! */ | |
| 1605 | error = ng_ppp_frag_process(node, item); | |
| 1606 | mtx_unlock(&priv->rmtx); | |
| 1607 | return (error); | |
| 1608 | ||
| 1609 | done: | |
| 1610 | mtx_unlock(&priv->rmtx); | |
| 1611 | NG_FREE_ITEM(item); | |
| 1612 | return (error); | |
| 1613 | } | |
| 1614 | ||
| 1615 | /************************************************************************ | |
| 1616 | HELPER STUFF | |
| 1617 | ************************************************************************/ | |
| 1618 | ||
| 1619 | /* | |
| 1620 | * If new mseq > current then set it and update all active links | |
| 1621 | */ | |
| 1622 | static void | |
| 1623 | ng_ppp_bump_mseq(node_p node, int32_t new_mseq) | |
| 1624 | { | |
| 1625 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 1626 | int i; | |
| 1627 | ||
| 1628 | if (MP_RECV_SEQ_DIFF(priv, priv->mseq, new_mseq) < 0) { | |
| 1629 | priv->mseq = new_mseq; | |
| 1630 | for (i = 0; i < priv->numActiveLinks; i++) { | |
| 1631 | struct ng_ppp_link *const alink = | |
| 1632 | &priv->links[priv->activeLinks[i]]; | |
| 1633 | ||
| 1634 | if (MP_RECV_SEQ_DIFF(priv, | |
| 1635 | alink->seq, new_mseq) < 0) | |
| 1636 | alink->seq = new_mseq; | |
| 1637 | } | |
| 1638 | } | |
| 1639 | } | |
| 1640 | ||
| 1641 | /* | |
| 1642 | * Examine our list of fragments, and determine if there is a | |
| 1643 | * complete and deliverable packet at the head of the list. | |
| 1644 | * Return 1 if so, zero otherwise. | |
| 1645 | */ | |
| 1646 | static int | |
| 1647 | ng_ppp_check_packet(node_p node) | |
| 1648 | { | |
| 1649 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 1650 | struct ng_ppp_frag *qent, *qnext; | |
| 1651 | ||
| 1652 | /* Check for empty queue */ | |
| 1653 | if (TAILQ_EMPTY(&priv->frags)) | |
| 1654 | return (0); | |
| 1655 | ||
| 1656 | /* Check first fragment is the start of a deliverable packet */ | |
| 1657 | qent = TAILQ_FIRST(&priv->frags); | |
| 1658 | if (!qent->first || MP_RECV_SEQ_DIFF(priv, qent->seq, priv->mseq) > 1) | |
| 1659 | return (0); | |
| 1660 | ||
| 1661 | /* Check that all the fragments are there */ | |
| 1662 | while (!qent->last) { | |
| 1663 | qnext = TAILQ_NEXT(qent, f_qent); | |
| 1664 | if (qnext == NULL) /* end of queue */ | |
| 1665 | return (0); | |
| 1666 | if (qnext->seq != MP_NEXT_RECV_SEQ(priv, qent->seq)) | |
| 1667 | return (0); | |
| 1668 | qent = qnext; | |
| 1669 | } | |
| 1670 | ||
| 1671 | /* Got one */ | |
| 1672 | return (1); | |
| 1673 | } | |
| 1674 | ||
| 1675 | /* | |
| 1676 | * Pull a completed packet off the head of the incoming fragment queue. | |
| 1677 | * This assumes there is a completed packet there to pull off. | |
| 1678 | */ | |
| 1679 | static void | |
| 1680 | ng_ppp_get_packet(node_p node, struct mbuf **mp) | |
| 1681 | { | |
| 1682 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 1683 | struct ng_ppp_frag *qent, *qnext; | |
| 1684 | struct mbuf *m = NULL, *tail; | |
| 1685 | ||
| 1686 | qent = TAILQ_FIRST(&priv->frags); | |
| 1687 | KASSERT(!TAILQ_EMPTY(&priv->frags) && qent->first, | |
| 1688 | ("%s: no packet", __func__)); | |
| 1689 | for (tail = NULL; qent != NULL; qent = qnext) { | |
| 1690 | qnext = TAILQ_NEXT(qent, f_qent); | |
| 1691 | KASSERT(!TAILQ_EMPTY(&priv->frags), | |
| 1692 | ("%s: empty q", __func__)); | |
| 1693 | TAILQ_REMOVE(&priv->frags, qent, f_qent); | |
| 1694 | if (tail == NULL) | |
| 1695 | tail = m = qent->data; | |
| 1696 | else { | |
| 1697 | m->m_pkthdr.len += qent->data->m_pkthdr.len; | |
| 1698 | tail->m_next = qent->data; | |
| 1699 | } | |
| 1700 | while (tail->m_next != NULL) | |
| 1701 | tail = tail->m_next; | |
| 1702 | if (qent->last) { | |
| 1703 | qnext = NULL; | |
| 1704 | /* Bump MSEQ if necessary */ | |
| 1705 | ng_ppp_bump_mseq(node, qent->seq); | |
| 1706 | } | |
| 1707 | TAILQ_INSERT_HEAD(&priv->fragsfree, qent, f_qent); | |
| 1708 | } | |
| 1709 | *mp = m; | |
| 1710 | } | |
| 1711 | ||
| 1712 | /* | |
| 1713 | * Trim fragments from the queue whose packets can never be completed. | |
| 1714 | * This assumes a complete packet is NOT at the beginning of the queue. | |
| 1715 | * Returns 1 if fragments were removed, zero otherwise. | |
| 1716 | */ | |
| 1717 | static int | |
| 1718 | ng_ppp_frag_trim(node_p node) | |
| 1719 | { | |
| 1720 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 1721 | struct ng_ppp_frag *qent, *qnext = NULL; | |
| 1722 | int removed = 0; | |
| 1723 | ||
| 1724 | /* Scan for "dead" fragments and remove them */ | |
| 1725 | while (1) { | |
| 1726 | int dead = 0; | |
| 1727 | ||
| 1728 | /* If queue is empty, we're done */ | |
| 1729 | if (TAILQ_EMPTY(&priv->frags)) | |
| 1730 | break; | |
| 1731 | ||
| 1732 | /* Determine whether first fragment can ever be completed */ | |
| 1733 | TAILQ_FOREACH(qent, &priv->frags, f_qent) { | |
| 1734 | if (MP_RECV_SEQ_DIFF(priv, qent->seq, priv->mseq) >= 0) | |
| 1735 | break; | |
| 1736 | qnext = TAILQ_NEXT(qent, f_qent); | |
| 1737 | KASSERT(qnext != NULL, | |
| 1738 | ("%s: last frag < MSEQ?", __func__)); | |
| 1739 | if (qnext->seq != MP_NEXT_RECV_SEQ(priv, qent->seq) | |
| 1740 | || qent->last || qnext->first) { | |
| 1741 | dead = 1; | |
| 1742 | break; | |
| 1743 | } | |
| 1744 | } | |
| 1745 | if (!dead) | |
| 1746 | break; | |
| 1747 | ||
| 1748 | /* Remove fragment and all others in the same packet */ | |
| 1749 | while ((qent = TAILQ_FIRST(&priv->frags)) != qnext) { | |
| 1750 | KASSERT(!TAILQ_EMPTY(&priv->frags), | |
| 1751 | ("%s: empty q", __func__)); | |
| 1752 | priv->bundleStats.dropFragments++; | |
| 1753 | TAILQ_REMOVE(&priv->frags, qent, f_qent); | |
| 1754 | NG_FREE_M(qent->data); | |
| 1755 | TAILQ_INSERT_HEAD(&priv->fragsfree, qent, f_qent); | |
| 1756 | removed = 1; | |
| 1757 | } | |
| 1758 | } | |
| 1759 | return (removed); | |
| 1760 | } | |
| 1761 | ||
| 1762 | /* | |
| 1763 | * Drop fragments on queue overflow. | |
| 1764 | * Returns 1 if fragments were removed, zero otherwise. | |
| 1765 | */ | |
| 1766 | static int | |
| 1767 | ng_ppp_frag_drop(node_p node) | |
| 1768 | { | |
| 1769 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 1770 | ||
| 1771 | /* Check queue length */ | |
| 1772 | if (TAILQ_EMPTY(&priv->fragsfree)) { | |
| 1773 | struct ng_ppp_frag *qent; | |
| 1774 | ||
| 1775 | /* Get oldest fragment */ | |
| 1776 | KASSERT(!TAILQ_EMPTY(&priv->frags), | |
| 1777 | ("%s: empty q", __func__)); | |
| 1778 | qent = TAILQ_FIRST(&priv->frags); | |
| 1779 | ||
| 1780 | /* Bump MSEQ if necessary */ | |
| 1781 | ng_ppp_bump_mseq(node, qent->seq); | |
| 1782 | ||
| 1783 | /* Drop it */ | |
| 1784 | priv->bundleStats.dropFragments++; | |
| 1785 | TAILQ_REMOVE(&priv->frags, qent, f_qent); | |
| 1786 | NG_FREE_M(qent->data); | |
| 1787 | TAILQ_INSERT_HEAD(&priv->fragsfree, qent, f_qent); | |
| 1788 | ||
| 1789 | return (1); | |
| 1790 | } | |
| 1791 | return (0); | |
| 1792 | } | |
| 1793 | ||
| 1794 | /* | |
| 1795 | * Run the queue, restoring the queue invariants | |
| 1796 | */ | |
| 1797 | static int | |
| 1798 | ng_ppp_frag_process(node_p node, item_p oitem) | |
| 1799 | { | |
| 1800 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 1801 | struct mbuf *m; | |
| 1802 | item_p item; | |
| 1803 | uint16_t proto; | |
| 1804 | ||
| 1805 | do { | |
| 1806 | /* Deliver any deliverable packets */ | |
| 1807 | while (ng_ppp_check_packet(node)) { | |
| 1808 | ng_ppp_get_packet(node, &m); | |
| 1809 | if ((m = ng_ppp_cutproto(m, &proto)) == NULL) | |
| 1810 | continue; | |
| 1811 | if (!PROT_VALID(proto)) { | |
| 1812 | priv->bundleStats.badProtos++; | |
| 1813 | NG_FREE_M(m); | |
| 1814 | continue; | |
| 1815 | } | |
| 1816 | if (oitem) { /* If original item present - reuse it. */ | |
| 1817 | item = oitem; | |
| 1818 | oitem = NULL; | |
| 1819 | NGI_M(item) = m; | |
| 1820 | } else { | |
| 1821 | item = ng_package_data(m, NG_NOFLAGS); | |
| 1822 | } | |
| 1823 | if (item != NULL) { | |
| 1824 | /* Stats */ | |
| 1825 | priv->bundleStats.recvFrames++; | |
| 1826 | priv->bundleStats.recvOctets += | |
| 1827 | NGI_M(item)->m_pkthdr.len; | |
| 1828 | ||
| 1829 | /* Drop mutex for the sending time. | |
| 1830 | * Priv may change, but we are ready! | |
| 1831 | */ | |
| 1832 | mtx_unlock(&priv->rmtx); | |
| 1833 | ng_ppp_crypt_recv(node, item, proto, | |
| 1834 | NG_PPP_BUNDLE_LINKNUM); | |
| 1835 | mtx_lock(&priv->rmtx); | |
| 1836 | } | |
| 1837 | } | |
| 1838 | /* Delete dead fragments and try again */ | |
| 1839 | } while (ng_ppp_frag_trim(node) || ng_ppp_frag_drop(node)); | |
| 1840 | ||
| 1841 | /* If we haven't reused original item - free it. */ | |
| 1842 | if (oitem) NG_FREE_ITEM(oitem); | |
| 1843 | ||
| 1844 | /* Done */ | |
| 1845 | return (0); | |
| 1846 | } | |
| 1847 | ||
| 1848 | /* | |
| 1849 | * Check for 'stale' completed packets that need to be delivered | |
| 1850 | * | |
| 1851 | * If a link goes down or has a temporary failure, MSEQ can get | |
| 1852 | * "stuck", because no new incoming fragments appear on that link. | |
| 1853 | * This can cause completed packets to never get delivered if | |
| 1854 | * their sequence numbers are all > MSEQ + 1. | |
| 1855 | * | |
| 1856 | * This routine checks how long all of the completed packets have | |
| 1857 | * been sitting in the queue, and if too long, removes fragments | |
| 1858 | * from the queue and increments MSEQ to allow them to be delivered. | |
| 1859 | */ | |
| 1860 | static void | |
| 1861 | ng_ppp_frag_checkstale(node_p node) | |
| 1862 | { | |
| 1863 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 1864 | struct ng_ppp_frag *qent, *beg, *end; | |
| 1865 | struct timeval now, age; | |
| 1866 | struct mbuf *m; | |
| 1867 | int seq; | |
| 1868 | item_p item; | |
| 1869 | int endseq; | |
| 1870 | uint16_t proto; | |
| 1871 | ||
| 1872 | now.tv_sec = 0; /* uninitialized state */ | |
| 1873 | while (1) { | |
| 1874 | ||
| 1875 | /* If queue is empty, we're done */ | |
| 1876 | if (TAILQ_EMPTY(&priv->frags)) | |
| 1877 | break; | |
| 1878 | ||
| 1879 | /* Find the first complete packet in the queue */ | |
| 1880 | beg = end = NULL; | |
| 1881 | seq = TAILQ_FIRST(&priv->frags)->seq; | |
| 1882 | TAILQ_FOREACH(qent, &priv->frags, f_qent) { | |
| 1883 | if (qent->first) | |
| 1884 | beg = qent; | |
| 1885 | else if (qent->seq != seq) | |
| 1886 | beg = NULL; | |
| 1887 | if (beg != NULL && qent->last) { | |
| 1888 | end = qent; | |
| 1889 | break; | |
| 1890 | } | |
| 1891 | seq = MP_NEXT_RECV_SEQ(priv, seq); | |
| 1892 | } | |
| 1893 | ||
| 1894 | /* If none found, exit */ | |
| 1895 | if (end == NULL) | |
| 1896 | break; | |
| 1897 | ||
| 1898 | /* Get current time (we assume we've been up for >= 1 second) */ | |
| 1899 | if (now.tv_sec == 0) | |
| 1900 | getmicrouptime(&now); | |
| 1901 | ||
| 1902 | /* Check if packet has been queued too long */ | |
| 1903 | age = now; | |
| 1904 | timevalsub(&age, &beg->timestamp); | |
| 1905 | if (timevalcmp(&age, &ng_ppp_max_staleness, < )) | |
| 1906 | break; | |
| 1907 | ||
| 1908 | /* Throw away junk fragments in front of the completed packet */ | |
| 1909 | while ((qent = TAILQ_FIRST(&priv->frags)) != beg) { | |
| 1910 | KASSERT(!TAILQ_EMPTY(&priv->frags), | |
| 1911 | ("%s: empty q", __func__)); | |
| 1912 | priv->bundleStats.dropFragments++; | |
| 1913 | TAILQ_REMOVE(&priv->frags, qent, f_qent); | |
| 1914 | NG_FREE_M(qent->data); | |
| 1915 | TAILQ_INSERT_HEAD(&priv->fragsfree, qent, f_qent); | |
| 1916 | } | |
| 1917 | ||
| 1918 | /* Extract completed packet */ | |
| 1919 | endseq = end->seq; | |
| 1920 | ng_ppp_get_packet(node, &m); | |
| 1921 | ||
| 1922 | if ((m = ng_ppp_cutproto(m, &proto)) == NULL) | |
| 1923 | continue; | |
| 1924 | if (!PROT_VALID(proto)) { | |
| 1925 | priv->bundleStats.badProtos++; | |
| 1926 | NG_FREE_M(m); | |
| 1927 | continue; | |
| 1928 | } | |
| 1929 | ||
| 1930 | /* Deliver packet */ | |
| 1931 | if ((item = ng_package_data(m, NG_NOFLAGS)) != NULL) { | |
| 1932 | /* Stats */ | |
| 1933 | priv->bundleStats.recvFrames++; | |
| 1934 | priv->bundleStats.recvOctets += NGI_M(item)->m_pkthdr.len; | |
| 1935 | ||
| 1936 | ng_ppp_crypt_recv(node, item, proto, | |
| 1937 | NG_PPP_BUNDLE_LINKNUM); | |
| 1938 | } | |
| 1939 | } | |
| 1940 | } | |
| 1941 | ||
| 1942 | /* | |
| 1943 | * Periodically call ng_ppp_frag_checkstale() | |
| 1944 | */ | |
| 1945 | static void | |
| 1946 | ng_ppp_frag_timeout(node_p node, hook_p hook, void *arg1, int arg2) | |
| 1947 | { | |
| 1948 | /* XXX: is this needed? */ | |
| 1949 | if (NG_NODE_NOT_VALID(node)) | |
| 1950 | return; | |
| 1951 | ||
| 1952 | /* Scan the fragment queue */ | |
| 1953 | ng_ppp_frag_checkstale(node); | |
| 1954 | ||
| 1955 | /* Start timer again */ | |
| 1956 | ng_ppp_start_frag_timer(node); | |
| 1957 | } | |
| 1958 | ||
| 1959 | /* | |
| 1960 | * Deliver a frame out on the bundle, i.e., figure out how to fragment | |
| 1961 | * the frame across the individual PPP links and do so. | |
| 1962 | */ | |
| 1963 | static int | |
| 1964 | ng_ppp_mp_xmit(node_p node, item_p item, uint16_t proto) | |
| 1965 | { | |
| 1966 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 1967 | const int hdr_len = priv->conf.xmitShortSeq ? 2 : 4; | |
| 1968 | int distrib[NG_PPP_MAX_LINKS]; | |
| 1969 | int firstFragment; | |
| 1970 | int activeLinkNum; | |
| 1971 | struct mbuf *m; | |
| 1972 | int plen; | |
| 1973 | int frags; | |
| 1974 | int32_t seq; | |
| 1975 | ||
| 1976 | /* At least one link must be active */ | |
| 1977 | if (priv->numActiveLinks == 0) { | |
| 1978 | NG_FREE_ITEM(item); | |
| 1979 | return (ENETDOWN); | |
| 1980 | } | |
| 1981 | ||
| 1982 | /* Save length for later stats. */ | |
| 1983 | plen = NGI_M(item)->m_pkthdr.len; | |
| 1984 | ||
| 1985 | if (!priv->conf.enableMultilink) { | |
| 1986 | return (ng_ppp_link_xmit(node, item, proto, | |
| 1987 | priv->activeLinks[0], plen)); | |
| 1988 | } | |
| 1989 | ||
| 1990 | /* Extract mbuf. */ | |
| 1991 | NGI_GET_M(item, m); | |
| 1992 | ||
| 1993 | /* Prepend protocol number, possibly compressed. */ | |
| 1994 | if ((m = ng_ppp_addproto(m, proto, 1)) == NULL) { | |
| 1995 | NG_FREE_ITEM(item); | |
| 1996 | return (ENOBUFS); | |
| 1997 | } | |
| 1998 | ||
| 1999 | /* Clear distribution plan */ | |
| 2000 | bzero(&distrib, priv->numActiveLinks * sizeof(distrib[0])); | |
| 2001 | ||
| 2002 | mtx_lock(&priv->xmtx); | |
| 2003 | ||
| 2004 | /* Round-robin strategy */ | |
| 2005 | if (priv->conf.enableRoundRobin) { | |
| 2006 | activeLinkNum = priv->lastLink++ % priv->numActiveLinks; | |
| 2007 | distrib[activeLinkNum] = m->m_pkthdr.len; | |
| 2008 | goto deliver; | |
| 2009 | } | |
| 2010 | ||
| 2011 | /* Strategy when all links are equivalent (optimize the common case) */ | |
| 2012 | if (priv->allLinksEqual) { | |
| 2013 | int numFrags, fraction, remain; | |
| 2014 | int i; | |
| 2015 | ||
| 2016 | /* Calculate optimal fragment count */ | |
| 2017 | numFrags = priv->numActiveLinks; | |
| 2018 | if (numFrags > m->m_pkthdr.len / MP_MIN_FRAG_LEN) | |
| 2019 | numFrags = m->m_pkthdr.len / MP_MIN_FRAG_LEN; | |
| 2020 | if (numFrags == 0) | |
| 2021 | numFrags = 1; | |
| 2022 | ||
| 2023 | fraction = m->m_pkthdr.len / numFrags; | |
| 2024 | remain = m->m_pkthdr.len - (fraction * numFrags); | |
| 2025 | ||
| 2026 | /* Assign distribution */ | |
| 2027 | for (i = 0; i < numFrags; i++) { | |
| 2028 | distrib[priv->lastLink++ % priv->numActiveLinks] | |
| 2029 | = fraction + (((remain--) > 0)?1:0); | |
| 2030 | } | |
| 2031 | goto deliver; | |
| 2032 | } | |
| 2033 | ||
| 2034 | /* Strategy when all links are not equivalent */ | |
| 2035 | ng_ppp_mp_strategy(node, m->m_pkthdr.len, distrib); | |
| 2036 | ||
| 2037 | deliver: | |
| 2038 | /* Estimate fragments count */ | |
| 2039 | frags = 0; | |
| 2040 | for (activeLinkNum = priv->numActiveLinks - 1; | |
| 2041 | activeLinkNum >= 0; activeLinkNum--) { | |
| 2042 | const uint16_t linkNum = priv->activeLinks[activeLinkNum]; | |
| 2043 | struct ng_ppp_link *const link = &priv->links[linkNum]; | |
| 2044 | ||
| 2045 | frags += (distrib[activeLinkNum] + link->conf.mru - hdr_len - 1) / | |
| 2046 | (link->conf.mru - hdr_len); | |
| 2047 | } | |
| 2048 | ||
| 2049 | /* Get out initial sequence number */ | |
| 2050 | seq = priv->xseq; | |
| 2051 | ||
| 2052 | /* Update next sequence number */ | |
| 2053 | if (priv->conf.xmitShortSeq) { | |
| 2054 | priv->xseq = (seq + frags) & MP_SHORT_SEQ_MASK; | |
| 2055 | } else { | |
| 2056 | priv->xseq = (seq + frags) & MP_LONG_SEQ_MASK; | |
| 2057 | } | |
| 2058 | ||
| 2059 | mtx_unlock(&priv->xmtx); | |
| 2060 | ||
| 2061 | /* Send alloted portions of frame out on the link(s) */ | |
| 2062 | for (firstFragment = 1, activeLinkNum = priv->numActiveLinks - 1; | |
| 2063 | activeLinkNum >= 0; activeLinkNum--) { | |
| 2064 | const uint16_t linkNum = priv->activeLinks[activeLinkNum]; | |
| 2065 | struct ng_ppp_link *const link = &priv->links[linkNum]; | |
| 2066 | ||
| 2067 | /* Deliver fragment(s) out the next link */ | |
| 2068 | for ( ; distrib[activeLinkNum] > 0; firstFragment = 0) { | |
| 2069 | int len, lastFragment, error; | |
| 2070 | struct mbuf *m2; | |
| 2071 | ||
| 2072 | /* Calculate fragment length; don't exceed link MTU */ | |
| 2073 | len = distrib[activeLinkNum]; | |
| 2074 | if (len > link->conf.mru - hdr_len) | |
| 2075 | len = link->conf.mru - hdr_len; | |
| 2076 | distrib[activeLinkNum] -= len; | |
| 2077 | lastFragment = (len == m->m_pkthdr.len); | |
| 2078 | ||
| 2079 | /* Split off next fragment as "m2" */ | |
| 2080 | m2 = m; | |
| 2081 | if (!lastFragment) { | |
| 5a975a3d | 2082 | struct mbuf *n = m_split(m, len, MB_DONTWAIT); |
| b06ebda0 MD |
2083 | |
| 2084 | if (n == NULL) { | |
| 2085 | NG_FREE_M(m); | |
| 2086 | if (firstFragment) | |
| 2087 | NG_FREE_ITEM(item); | |
| 2088 | return (ENOMEM); | |
| 2089 | } | |
| 5a975a3d | 2090 | m_tag_copy_chain(n, m, MB_DONTWAIT); |
| b06ebda0 MD |
2091 | m = n; |
| 2092 | } | |
| 2093 | ||
| 2094 | /* Prepend MP header */ | |
| 2095 | if (priv->conf.xmitShortSeq) { | |
| 2096 | uint16_t shdr; | |
| 2097 | ||
| 2098 | shdr = seq; | |
| 2099 | seq = (seq + 1) & MP_SHORT_SEQ_MASK; | |
| 2100 | if (firstFragment) | |
| 2101 | shdr |= MP_SHORT_FIRST_FLAG; | |
| 2102 | if (lastFragment) | |
| 2103 | shdr |= MP_SHORT_LAST_FLAG; | |
| 2104 | shdr = htons(shdr); | |
| 2105 | m2 = ng_ppp_prepend(m2, &shdr, 2); | |
| 2106 | } else { | |
| 2107 | uint32_t lhdr; | |
| 2108 | ||
| 2109 | lhdr = seq; | |
| 2110 | seq = (seq + 1) & MP_LONG_SEQ_MASK; | |
| 2111 | if (firstFragment) | |
| 2112 | lhdr |= MP_LONG_FIRST_FLAG; | |
| 2113 | if (lastFragment) | |
| 2114 | lhdr |= MP_LONG_LAST_FLAG; | |
| 2115 | lhdr = htonl(lhdr); | |
| 2116 | m2 = ng_ppp_prepend(m2, &lhdr, 4); | |
| 2117 | } | |
| 2118 | if (m2 == NULL) { | |
| 2119 | if (!lastFragment) | |
| 2120 | m_freem(m); | |
| 2121 | if (firstFragment) | |
| 2122 | NG_FREE_ITEM(item); | |
| 2123 | return (ENOBUFS); | |
| 2124 | } | |
| 2125 | ||
| 2126 | /* Send fragment */ | |
| 2127 | if (firstFragment) { | |
| 2128 | NGI_M(item) = m2; /* Reuse original item. */ | |
| 2129 | } else { | |
| 2130 | item = ng_package_data(m2, NG_NOFLAGS); | |
| 2131 | } | |
| 2132 | if (item != NULL) { | |
| 2133 | error = ng_ppp_link_xmit(node, item, PROT_MP, | |
| 2134 | linkNum, (firstFragment?plen:0)); | |
| 2135 | if (error != 0) { | |
| 2136 | if (!lastFragment) | |
| 2137 | NG_FREE_M(m); | |
| 2138 | return (error); | |
| 2139 | } | |
| 2140 | } | |
| 2141 | } | |
| 2142 | } | |
| 2143 | ||
| 2144 | /* Done */ | |
| 2145 | return (0); | |
| 2146 | } | |
| 2147 | ||
| 2148 | /* | |
| 2149 | * Computing the optimal fragmentation | |
| 2150 | * ----------------------------------- | |
| 2151 | * | |
| 2152 | * This routine tries to compute the optimal fragmentation pattern based | |
| 2153 | * on each link's latency, bandwidth, and calculated additional latency. | |
| 2154 | * The latter quantity is the additional latency caused by previously | |
| 2155 | * written data that has not been transmitted yet. | |
| 2156 | * | |
| 2157 | * This algorithm is only useful when not all of the links have the | |
| 2158 | * same latency and bandwidth values. | |
| 2159 | * | |
| 2160 | * The essential idea is to make the last bit of each fragment of the | |
| 2161 | * frame arrive at the opposite end at the exact same time. This greedy | |
| 2162 | * algorithm is optimal, in that no other scheduling could result in any | |
| 2163 | * packet arriving any sooner unless packets are delivered out of order. | |
| 2164 | * | |
| 2165 | * Suppose link i has bandwidth b_i (in tens of bytes per milisecond) and | |
| 2166 | * latency l_i (in miliseconds). Consider the function function f_i(t) | |
| 2167 | * which is equal to the number of bytes that will have arrived at | |
| 2168 | * the peer after t miliseconds if we start writing continuously at | |
| 2169 | * time t = 0. Then f_i(t) = b_i * (t - l_i) = ((b_i * t) - (l_i * b_i). | |
| 2170 | * That is, f_i(t) is a line with slope b_i and y-intersect -(l_i * b_i). | |
| 2171 | * Note that the y-intersect is always <= zero because latency can't be | |
| 2172 | * negative. Note also that really the function is f_i(t) except when | |
| 2173 | * f_i(t) is negative, in which case the function is zero. To take | |
| 2174 | * care of this, let Q_i(t) = { if (f_i(t) > 0) return 1; else return 0; }. | |
| 2175 | * So the actual number of bytes that will have arrived at the peer after | |
| 2176 | * t miliseconds is f_i(t) * Q_i(t). | |
| 2177 | * | |
| 2178 | * At any given time, each link has some additional latency a_i >= 0 | |
| 2179 | * due to previously written fragment(s) which are still in the queue. | |
| 2180 | * This value is easily computed from the time since last transmission, | |
| 2181 | * the previous latency value, the number of bytes written, and the | |
| 2182 | * link's bandwidth. | |
| 2183 | * | |
| 2184 | * Assume that l_i includes any a_i already, and that the links are | |
| 2185 | * sorted by latency, so that l_i <= l_{i+1}. | |
| 2186 | * | |
| 2187 | * Let N be the total number of bytes in the current frame we are sending. | |
| 2188 | * | |
| 2189 | * Suppose we were to start writing bytes at time t = 0 on all links | |
| 2190 | * simultaneously, which is the most we can possibly do. Then let | |
| 2191 | * F(t) be equal to the total number of bytes received by the peer | |
| 2192 | * after t miliseconds. Then F(t) = Sum_i (f_i(t) * Q_i(t)). | |
| 2193 | * | |
| 2194 | * Our goal is simply this: fragment the frame across the links such | |
| 2195 | * that the peer is able to reconstruct the completed frame as soon as | |
| 2196 | * possible, i.e., at the least possible value of t. Call this value t_0. | |
| 2197 | * | |
| 2198 | * Then it follows that F(t_0) = N. Our strategy is first to find the value | |
| 2199 | * of t_0, and then deduce how many bytes to write to each link. | |
| 2200 | * | |
| 2201 | * Rewriting F(t_0): | |
| 2202 | * | |
| 2203 | * t_0 = ( N + Sum_i ( l_i * b_i * Q_i(t_0) ) ) / Sum_i ( b_i * Q_i(t_0) ) | |
| 2204 | * | |
| 2205 | * Now, we note that Q_i(t) is constant for l_i <= t <= l_{i+1}. t_0 will | |
| 2206 | * lie in one of these ranges. To find it, we just need to find the i such | |
| 2207 | * that F(l_i) <= N <= F(l_{i+1}). Then we compute all the constant values | |
| 2208 | * for Q_i() in this range, plug in the remaining values, solving for t_0. | |
| 2209 | * | |
| 2210 | * Once t_0 is known, then the number of bytes to send on link i is | |
| 2211 | * just f_i(t_0) * Q_i(t_0). | |
| 2212 | * | |
| 2213 | * In other words, we start allocating bytes to the links one at a time. | |
| 2214 | * We keep adding links until the frame is completely sent. Some links | |
| 2215 | * may not get any bytes because their latency is too high. | |
| 2216 | * | |
| 2217 | * Is all this work really worth the trouble? Depends on the situation. | |
| 2218 | * The bigger the ratio of computer speed to link speed, and the more | |
| 2219 | * important total bundle latency is (e.g., for interactive response time), | |
| 2220 | * the more it's worth it. There is however the cost of calling this | |
| 2221 | * function for every frame. The running time is O(n^2) where n is the | |
| 2222 | * number of links that receive a non-zero number of bytes. | |
| 2223 | * | |
| 2224 | * Since latency is measured in miliseconds, the "resolution" of this | |
| 2225 | * algorithm is one milisecond. | |
| 2226 | * | |
| 2227 | * To avoid this algorithm altogether, configure all links to have the | |
| 2228 | * same latency and bandwidth. | |
| 2229 | */ | |
| 2230 | static void | |
| 2231 | ng_ppp_mp_strategy(node_p node, int len, int *distrib) | |
| 2232 | { | |
| 2233 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 2234 | int latency[NG_PPP_MAX_LINKS]; | |
| 2235 | int sortByLatency[NG_PPP_MAX_LINKS]; | |
| 2236 | int activeLinkNum; | |
| 2237 | int t0, total, topSum, botSum; | |
| 2238 | struct timeval now; | |
| 2239 | int i, numFragments; | |
| 2240 | ||
| 2241 | /* If only one link, this gets real easy */ | |
| 2242 | if (priv->numActiveLinks == 1) { | |
| 2243 | distrib[0] = len; | |
| 2244 | return; | |
| 2245 | } | |
| 2246 | ||
| 2247 | /* Get current time */ | |
| 2248 | getmicrouptime(&now); | |
| 2249 | ||
| 2250 | /* Compute latencies for each link at this point in time */ | |
| 2251 | for (activeLinkNum = 0; | |
| 2252 | activeLinkNum < priv->numActiveLinks; activeLinkNum++) { | |
| 2253 | struct ng_ppp_link *alink; | |
| 2254 | struct timeval diff; | |
| 2255 | int xmitBytes; | |
| 2256 | ||
| 2257 | /* Start with base latency value */ | |
| 2258 | alink = &priv->links[priv->activeLinks[activeLinkNum]]; | |
| 2259 | latency[activeLinkNum] = alink->latency; | |
| 2260 | sortByLatency[activeLinkNum] = activeLinkNum; /* see below */ | |
| 2261 | ||
| 2262 | /* Any additional latency? */ | |
| 2263 | if (alink->bytesInQueue == 0) | |
| 2264 | continue; | |
| 2265 | ||
| 2266 | /* Compute time delta since last write */ | |
| 2267 | diff = now; | |
| 2268 | timevalsub(&diff, &alink->lastWrite); | |
| 2269 | ||
| 2270 | /* alink->bytesInQueue will be changed, mark change time. */ | |
| 2271 | alink->lastWrite = now; | |
| 2272 | ||
| 2273 | if (now.tv_sec < 0 || diff.tv_sec >= 10) { /* sanity */ | |
| 2274 | alink->bytesInQueue = 0; | |
| 2275 | continue; | |
| 2276 | } | |
| 2277 | ||
| 2278 | /* How many bytes could have transmitted since last write? */ | |
| 2279 | xmitBytes = (alink->conf.bandwidth * 10 * diff.tv_sec) | |
| 2280 | + (alink->conf.bandwidth * (diff.tv_usec / 1000)) / 100; | |
| 2281 | alink->bytesInQueue -= xmitBytes; | |
| 2282 | if (alink->bytesInQueue < 0) | |
| 2283 | alink->bytesInQueue = 0; | |
| 2284 | else | |
| 2285 | latency[activeLinkNum] += | |
| 2286 | (100 * alink->bytesInQueue) / alink->conf.bandwidth; | |
| 2287 | } | |
| 2288 | ||
| 2289 | /* Sort active links by latency */ | |
| 2290 | qsort_r(sortByLatency, | |
| 2291 | priv->numActiveLinks, sizeof(*sortByLatency), latency, ng_ppp_intcmp); | |
| 2292 | ||
| 2293 | /* Find the interval we need (add links in sortByLatency[] order) */ | |
| 2294 | for (numFragments = 1; | |
| 2295 | numFragments < priv->numActiveLinks; numFragments++) { | |
| 2296 | for (total = i = 0; i < numFragments; i++) { | |
| 2297 | int flowTime; | |
| 2298 | ||
| 2299 | flowTime = latency[sortByLatency[numFragments]] | |
| 2300 | - latency[sortByLatency[i]]; | |
| 2301 | total += ((flowTime * priv->links[ | |
| 2302 | priv->activeLinks[sortByLatency[i]]].conf.bandwidth) | |
| 2303 | + 99) / 100; | |
| 2304 | } | |
| 2305 | if (total >= len) | |
| 2306 | break; | |
| 2307 | } | |
| 2308 | ||
| 2309 | /* Solve for t_0 in that interval */ | |
| 2310 | for (topSum = botSum = i = 0; i < numFragments; i++) { | |
| 2311 | int bw = priv->links[ | |
| 2312 | priv->activeLinks[sortByLatency[i]]].conf.bandwidth; | |
| 2313 | ||
| 2314 | topSum += latency[sortByLatency[i]] * bw; /* / 100 */ | |
| 2315 | botSum += bw; /* / 100 */ | |
| 2316 | } | |
| 2317 | t0 = ((len * 100) + topSum + botSum / 2) / botSum; | |
| 2318 | ||
| 2319 | /* Compute f_i(t_0) all i */ | |
| 2320 | for (total = i = 0; i < numFragments; i++) { | |
| 2321 | int bw = priv->links[ | |
| 2322 | priv->activeLinks[sortByLatency[i]]].conf.bandwidth; | |
| 2323 | ||
| 2324 | distrib[sortByLatency[i]] = | |
| 2325 | (bw * (t0 - latency[sortByLatency[i]]) + 50) / 100; | |
| 2326 | total += distrib[sortByLatency[i]]; | |
| 2327 | } | |
| 2328 | ||
| 2329 | /* Deal with any rounding error */ | |
| 2330 | if (total < len) { | |
| 2331 | struct ng_ppp_link *fastLink = | |
| 2332 | &priv->links[priv->activeLinks[sortByLatency[0]]]; | |
| 2333 | int fast = 0; | |
| 2334 | ||
| 2335 | /* Find the fastest link */ | |
| 2336 | for (i = 1; i < numFragments; i++) { | |
| 2337 | struct ng_ppp_link *const link = | |
| 2338 | &priv->links[priv->activeLinks[sortByLatency[i]]]; | |
| 2339 | ||
| 2340 | if (link->conf.bandwidth > fastLink->conf.bandwidth) { | |
| 2341 | fast = i; | |
| 2342 | fastLink = link; | |
| 2343 | } | |
| 2344 | } | |
| 2345 | distrib[sortByLatency[fast]] += len - total; | |
| 2346 | } else while (total > len) { | |
| 2347 | struct ng_ppp_link *slowLink = | |
| 2348 | &priv->links[priv->activeLinks[sortByLatency[0]]]; | |
| 2349 | int delta, slow = 0; | |
| 2350 | ||
| 2351 | /* Find the slowest link that still has bytes to remove */ | |
| 2352 | for (i = 1; i < numFragments; i++) { | |
| 2353 | struct ng_ppp_link *const link = | |
| 2354 | &priv->links[priv->activeLinks[sortByLatency[i]]]; | |
| 2355 | ||
| 2356 | if (distrib[sortByLatency[slow]] == 0 | |
| 2357 | || (distrib[sortByLatency[i]] > 0 | |
| 2358 | && link->conf.bandwidth < | |
| 2359 | slowLink->conf.bandwidth)) { | |
| 2360 | slow = i; | |
| 2361 | slowLink = link; | |
| 2362 | } | |
| 2363 | } | |
| 2364 | delta = total - len; | |
| 2365 | if (delta > distrib[sortByLatency[slow]]) | |
| 2366 | delta = distrib[sortByLatency[slow]]; | |
| 2367 | distrib[sortByLatency[slow]] -= delta; | |
| 2368 | total -= delta; | |
| 2369 | } | |
| 2370 | } | |
| 2371 | ||
| 2372 | /* | |
| 2373 | * Compare two integers | |
| 2374 | */ | |
| 2375 | static int | |
| 2376 | ng_ppp_intcmp(void *latency, const void *v1, const void *v2) | |
| 2377 | { | |
| 2378 | const int index1 = *((const int *) v1); | |
| 2379 | const int index2 = *((const int *) v2); | |
| 2380 | ||
| 2381 | return ((int *)latency)[index1] - ((int *)latency)[index2]; | |
| 2382 | } | |
| 2383 | ||
| 2384 | /* | |
| 2385 | * Prepend a possibly compressed PPP protocol number in front of a frame | |
| 2386 | */ | |
| 2387 | static struct mbuf * | |
| 2388 | ng_ppp_addproto(struct mbuf *m, uint16_t proto, int compOK) | |
| 2389 | { | |
| 2390 | if (compOK && PROT_COMPRESSABLE(proto)) { | |
| 2391 | uint8_t pbyte = (uint8_t)proto; | |
| 2392 | ||
| 2393 | return ng_ppp_prepend(m, &pbyte, 1); | |
| 2394 | } else { | |
| 2395 | uint16_t pword = htons((uint16_t)proto); | |
| 2396 | ||
| 2397 | return ng_ppp_prepend(m, &pword, 2); | |
| 2398 | } | |
| 2399 | } | |
| 2400 | ||
| 2401 | /* | |
| 2402 | * Cut a possibly compressed PPP protocol number from the front of a frame. | |
| 2403 | */ | |
| 2404 | static struct mbuf * | |
| 2405 | ng_ppp_cutproto(struct mbuf *m, uint16_t *proto) | |
| 2406 | { | |
| 2407 | ||
| 2408 | *proto = 0; | |
| 2409 | if (m->m_len < 1 && (m = m_pullup(m, 1)) == NULL) | |
| 2410 | return (NULL); | |
| 2411 | ||
| 2412 | *proto = *mtod(m, uint8_t *); | |
| 2413 | m_adj(m, 1); | |
| 2414 | ||
| 2415 | if (!PROT_VALID(*proto)) { | |
| 2416 | if (m->m_len < 1 && (m = m_pullup(m, 1)) == NULL) | |
| 2417 | return (NULL); | |
| 2418 | ||
| 2419 | *proto = (*proto << 8) + *mtod(m, uint8_t *); | |
| 2420 | m_adj(m, 1); | |
| 2421 | } | |
| 2422 | ||
| 2423 | return (m); | |
| 2424 | } | |
| 2425 | ||
| 2426 | /* | |
| 2427 | * Prepend some bytes to an mbuf. | |
| 2428 | */ | |
| 2429 | static struct mbuf * | |
| 2430 | ng_ppp_prepend(struct mbuf *m, const void *buf, int len) | |
| 2431 | { | |
| 5a975a3d | 2432 | M_PREPEND(m, len, MB_DONTWAIT); |
| b06ebda0 MD |
2433 | if (m == NULL || (m->m_len < len && (m = m_pullup(m, len)) == NULL)) |
| 2434 | return (NULL); | |
| 2435 | bcopy(buf, mtod(m, uint8_t *), len); | |
| 2436 | return (m); | |
| 2437 | } | |
| 2438 | ||
| 2439 | /* | |
| 2440 | * Update private information that is derived from other private information | |
| 2441 | */ | |
| 2442 | static void | |
| 2443 | ng_ppp_update(node_p node, int newConf) | |
| 2444 | { | |
| 2445 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 2446 | int i; | |
| 2447 | ||
| 2448 | /* Update active status for VJ Compression */ | |
| 2449 | priv->vjCompHooked = priv->hooks[HOOK_INDEX_VJC_IP] != NULL | |
| 2450 | && priv->hooks[HOOK_INDEX_VJC_COMP] != NULL | |
| 2451 | && priv->hooks[HOOK_INDEX_VJC_UNCOMP] != NULL | |
| 2452 | && priv->hooks[HOOK_INDEX_VJC_VJIP] != NULL; | |
| 2453 | ||
| 2454 | /* Increase latency for each link an amount equal to one MP header */ | |
| 2455 | if (newConf) { | |
| 2456 | for (i = 0; i < NG_PPP_MAX_LINKS; i++) { | |
| 2457 | int hdrBytes; | |
| 2458 | ||
| 2459 | if (priv->links[i].conf.bandwidth == 0) | |
| 2460 | continue; | |
| 2461 | ||
| 2462 | hdrBytes = MP_AVERAGE_LINK_OVERHEAD | |
| 2463 | + (priv->links[i].conf.enableACFComp ? 0 : 2) | |
| 2464 | + (priv->links[i].conf.enableProtoComp ? 1 : 2) | |
| 2465 | + (priv->conf.xmitShortSeq ? 2 : 4); | |
| 2466 | priv->links[i].latency = | |
| 2467 | priv->links[i].conf.latency + | |
| 2468 | (hdrBytes / priv->links[i].conf.bandwidth + 50) / 100; | |
| 2469 | } | |
| 2470 | } | |
| 2471 | ||
| 2472 | /* Update list of active links */ | |
| 2473 | bzero(&priv->activeLinks, sizeof(priv->activeLinks)); | |
| 2474 | priv->numActiveLinks = 0; | |
| 2475 | priv->allLinksEqual = 1; | |
| 2476 | for (i = 0; i < NG_PPP_MAX_LINKS; i++) { | |
| 2477 | struct ng_ppp_link *const link = &priv->links[i]; | |
| 2478 | ||
| 2479 | /* Is link active? */ | |
| 2480 | if (link->conf.enableLink && link->hook != NULL) { | |
| 2481 | struct ng_ppp_link *link0; | |
| 2482 | ||
| 2483 | /* Add link to list of active links */ | |
| 2484 | priv->activeLinks[priv->numActiveLinks++] = i; | |
| 2485 | link0 = &priv->links[priv->activeLinks[0]]; | |
| 2486 | ||
| 2487 | /* Determine if all links are still equal */ | |
| 2488 | if (link->latency != link0->latency | |
| 2489 | || link->conf.bandwidth != link0->conf.bandwidth) | |
| 2490 | priv->allLinksEqual = 0; | |
| 2491 | ||
| 2492 | /* Initialize rec'd sequence number */ | |
| 2493 | if (link->seq == MP_NOSEQ) { | |
| 2494 | link->seq = (link == link0) ? | |
| 2495 | MP_INITIAL_SEQ : link0->seq; | |
| 2496 | } | |
| 2497 | } else | |
| 2498 | link->seq = MP_NOSEQ; | |
| 2499 | } | |
| 2500 | ||
| 2501 | /* Update MP state as multi-link is active or not */ | |
| 2502 | if (priv->conf.enableMultilink && priv->numActiveLinks > 0) | |
| 2503 | ng_ppp_start_frag_timer(node); | |
| 2504 | else { | |
| 2505 | ng_ppp_stop_frag_timer(node); | |
| 2506 | ng_ppp_frag_reset(node); | |
| 2507 | priv->xseq = MP_INITIAL_SEQ; | |
| 2508 | priv->mseq = MP_INITIAL_SEQ; | |
| 2509 | for (i = 0; i < NG_PPP_MAX_LINKS; i++) { | |
| 2510 | struct ng_ppp_link *const link = &priv->links[i]; | |
| 2511 | ||
| 2512 | bzero(&link->lastWrite, sizeof(link->lastWrite)); | |
| 2513 | link->bytesInQueue = 0; | |
| 2514 | link->seq = MP_NOSEQ; | |
| 2515 | } | |
| 2516 | } | |
| 2517 | } | |
| 2518 | ||
| 2519 | /* | |
| 2520 | * Determine if a new configuration would represent a valid change | |
| 2521 | * from the current configuration and link activity status. | |
| 2522 | */ | |
| 2523 | static int | |
| 2524 | ng_ppp_config_valid(node_p node, const struct ng_ppp_node_conf *newConf) | |
| 2525 | { | |
| 2526 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 2527 | int i, newNumLinksActive; | |
| 2528 | ||
| 2529 | /* Check per-link config and count how many links would be active */ | |
| 2530 | for (newNumLinksActive = i = 0; i < NG_PPP_MAX_LINKS; i++) { | |
| 2531 | if (newConf->links[i].enableLink && priv->links[i].hook != NULL) | |
| 2532 | newNumLinksActive++; | |
| 2533 | if (!newConf->links[i].enableLink) | |
| 2534 | continue; | |
| 2535 | if (newConf->links[i].mru < MP_MIN_LINK_MRU) | |
| 2536 | return (0); | |
| 2537 | if (newConf->links[i].bandwidth == 0) | |
| 2538 | return (0); | |
| 2539 | if (newConf->links[i].bandwidth > NG_PPP_MAX_BANDWIDTH) | |
| 2540 | return (0); | |
| 2541 | if (newConf->links[i].latency > NG_PPP_MAX_LATENCY) | |
| 2542 | return (0); | |
| 2543 | } | |
| 2544 | ||
| 2545 | /* Check bundle parameters */ | |
| 2546 | if (newConf->bund.enableMultilink && newConf->bund.mrru < MP_MIN_MRRU) | |
| 2547 | return (0); | |
| 2548 | ||
| 2549 | /* Disallow changes to multi-link configuration while MP is active */ | |
| 2550 | if (priv->numActiveLinks > 0 && newNumLinksActive > 0) { | |
| 2551 | if (!priv->conf.enableMultilink | |
| 2552 | != !newConf->bund.enableMultilink | |
| 2553 | || !priv->conf.xmitShortSeq != !newConf->bund.xmitShortSeq | |
| 2554 | || !priv->conf.recvShortSeq != !newConf->bund.recvShortSeq) | |
| 2555 | return (0); | |
| 2556 | } | |
| 2557 | ||
| 2558 | /* At most one link can be active unless multi-link is enabled */ | |
| 2559 | if (!newConf->bund.enableMultilink && newNumLinksActive > 1) | |
| 2560 | return (0); | |
| 2561 | ||
| 2562 | /* Configuration change would be valid */ | |
| 2563 | return (1); | |
| 2564 | } | |
| 2565 | ||
| 2566 | /* | |
| 2567 | * Free all entries in the fragment queue | |
| 2568 | */ | |
| 2569 | static void | |
| 2570 | ng_ppp_frag_reset(node_p node) | |
| 2571 | { | |
| 2572 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 2573 | struct ng_ppp_frag *qent, *qnext; | |
| 2574 | ||
| 2575 | for (qent = TAILQ_FIRST(&priv->frags); qent; qent = qnext) { | |
| 2576 | qnext = TAILQ_NEXT(qent, f_qent); | |
| 2577 | NG_FREE_M(qent->data); | |
| 2578 | TAILQ_INSERT_HEAD(&priv->fragsfree, qent, f_qent); | |
| 2579 | } | |
| 2580 | TAILQ_INIT(&priv->frags); | |
| 2581 | } | |
| 2582 | ||
| 2583 | /* | |
| 2584 | * Start fragment queue timer | |
| 2585 | */ | |
| 2586 | static void | |
| 2587 | ng_ppp_start_frag_timer(node_p node) | |
| 2588 | { | |
| 2589 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 2590 | ||
| 2591 | if (!(callout_pending(&priv->fragTimer))) | |
| 2592 | ng_callout(&priv->fragTimer, node, NULL, MP_FRAGTIMER_INTERVAL, | |
| 2593 | ng_ppp_frag_timeout, NULL, 0); | |
| 2594 | } | |
| 2595 | ||
| 2596 | /* | |
| 2597 | * Stop fragment queue timer | |
| 2598 | */ | |
| 2599 | static void | |
| 2600 | ng_ppp_stop_frag_timer(node_p node) | |
| 2601 | { | |
| 2602 | const priv_p priv = NG_NODE_PRIVATE(node); | |
| 2603 | ||
| 2604 | if (callout_pending(&priv->fragTimer)) | |
| 2605 | ng_uncallout(&priv->fragTimer, node); | |
| 2606 | } |