1 /* 2 * ng_ksocket.c 3 */ 4 5 /*- 6 * Copyright (c) 1996-1999 Whistle Communications, Inc. 7 * All rights reserved. 8 * 9 * Subject to the following obligations and disclaimer of warranty, use and 10 * redistribution of this software, in source or object code forms, with or 11 * without modifications are expressly permitted by Whistle Communications; 12 * provided, however, that: 13 * 1. Any and all reproductions of the source or object code must include the 14 * copyright notice above and the following disclaimer of warranties; and 15 * 2. No rights are granted, in any manner or form, to use Whistle 16 * Communications, Inc. trademarks, including the mark "WHISTLE 17 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 18 * such appears in the above copyright notice or in the software. 19 * 20 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 21 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 22 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 23 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 25 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 26 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 27 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 28 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 29 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 30 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 31 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 32 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 35 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 36 * OF SUCH DAMAGE. 37 * 38 * Author: Archie Cobbs <archie@freebsd.org> 39 * 40 * $FreeBSD: src/sys/netgraph/ng_ksocket.c,v 1.61 2008/03/07 21:12:56 mav Exp $ 41 * $Whistle: ng_ksocket.c,v 1.1 1999/11/16 20:04:40 archie Exp $ 42 */ 43 44 /* 45 * Kernel socket node type. This node type is basically a kernel-mode 46 * version of a socket... kindof like the reverse of the socket node type. 47 */ 48 49 #include <sys/param.h> 50 #include <sys/systm.h> 51 #include <sys/kernel.h> 52 #include <sys/mbuf.h> 53 #include <sys/proc.h> 54 #include <sys/malloc.h> 55 #include <sys/ctype.h> 56 #include <sys/protosw.h> 57 #include <sys/errno.h> 58 #include <sys/socket.h> 59 #include <sys/socketops.h> 60 #include <sys/socketvar.h> 61 #include <sys/socketvar2.h> 62 #include <sys/thread2.h> 63 #include <sys/uio.h> 64 #include <sys/un.h> 65 66 #include <netgraph7/ng_message.h> 67 #include <netgraph7/netgraph.h> 68 #include <netgraph7/ng_parse.h> 69 #include "ng_ksocket.h" 70 71 #include <netinet/in.h> 72 73 #ifdef NG_SEPARATE_MALLOC 74 MALLOC_DEFINE(M_NETGRAPH_KSOCKET, "netgraph_ksock", "netgraph ksock node "); 75 #else 76 #define M_NETGRAPH_KSOCKET M_NETGRAPH 77 #endif 78 79 #define OFFSETOF(s, e) ((char *)&((s *)0)->e - (char *)((s *)0)) 80 #define SADATA_OFFSET (OFFSETOF(struct sockaddr, sa_data)) 81 82 /* Node private data */ 83 struct ng_ksocket_private { 84 node_p node; 85 hook_p hook; 86 struct socket *so; 87 int fn_sent; /* FN call on incoming event was sent */ 88 LIST_HEAD(, ng_ksocket_private) embryos; 89 LIST_ENTRY(ng_ksocket_private) siblings; 90 u_int32_t flags; 91 u_int32_t response_token; 92 ng_ID_t response_addr; 93 }; 94 typedef struct ng_ksocket_private *priv_p; 95 96 /* Flags for priv_p */ 97 #define KSF_CONNECTING 0x00000001 /* Waiting for connection complete */ 98 #define KSF_ACCEPTING 0x00000002 /* Waiting for accept complete */ 99 #define KSF_EOFSEEN 0x00000004 /* Have sent 0-length EOF mbuf */ 100 #define KSF_CLONED 0x00000008 /* Cloned from an accepting socket */ 101 #define KSF_EMBRYONIC 0x00000010 /* Cloned node with no hooks yet */ 102 103 /* Netgraph node methods */ 104 static ng_constructor_t ng_ksocket_constructor; 105 static ng_rcvmsg_t ng_ksocket_rcvmsg; 106 static ng_shutdown_t ng_ksocket_shutdown; 107 static ng_newhook_t ng_ksocket_newhook; 108 static ng_rcvdata_t ng_ksocket_rcvdata; 109 static ng_connect_t ng_ksocket_connect; 110 static ng_disconnect_t ng_ksocket_disconnect; 111 112 /* Alias structure */ 113 struct ng_ksocket_alias { 114 const char *name; 115 const int value; 116 const int family; 117 }; 118 119 /* Protocol family aliases */ 120 static const struct ng_ksocket_alias ng_ksocket_families[] = { 121 { "local", PF_LOCAL }, 122 { "inet", PF_INET }, 123 { "inet6", PF_INET6 }, 124 { "ipx", PF_IPX }, 125 { "atm", PF_ATM }, 126 { NULL, -1 }, 127 }; 128 129 /* Socket type aliases */ 130 static const struct ng_ksocket_alias ng_ksocket_types[] = { 131 { "stream", SOCK_STREAM }, 132 { "dgram", SOCK_DGRAM }, 133 { "raw", SOCK_RAW }, 134 { "rdm", SOCK_RDM }, 135 { "seqpacket", SOCK_SEQPACKET }, 136 { NULL, -1 }, 137 }; 138 139 /* Protocol aliases */ 140 static const struct ng_ksocket_alias ng_ksocket_protos[] = { 141 { "ip", IPPROTO_IP, PF_INET }, 142 { "raw", IPPROTO_RAW, PF_INET }, 143 { "icmp", IPPROTO_ICMP, PF_INET }, 144 { "igmp", IPPROTO_IGMP, PF_INET }, 145 { "tcp", IPPROTO_TCP, PF_INET }, 146 { "udp", IPPROTO_UDP, PF_INET }, 147 { "gre", IPPROTO_GRE, PF_INET }, 148 { "esp", IPPROTO_ESP, PF_INET }, 149 { "ah", IPPROTO_AH, PF_INET }, 150 { "swipe", IPPROTO_SWIPE, PF_INET }, 151 { "encap", IPPROTO_ENCAP, PF_INET }, 152 { "divert", IPPROTO_DIVERT, PF_INET }, 153 { "pim", IPPROTO_PIM, PF_INET }, 154 { NULL, -1 }, 155 }; 156 157 /* Helper functions */ 158 static int ng_ksocket_check_accept(priv_p); 159 static void ng_ksocket_finish_accept(priv_p); 160 static void ng_ksocket_incoming(struct socket *so, void *arg, int waitflag); 161 static int ng_ksocket_parse(const struct ng_ksocket_alias *aliases, 162 const char *s, int family); 163 static void ng_ksocket_incoming2(node_p node, hook_p hook, 164 void *arg1, int arg2); 165 166 /************************************************************************ 167 STRUCT SOCKADDR PARSE TYPE 168 ************************************************************************/ 169 170 /* Get the length of the data portion of a generic struct sockaddr */ 171 static int 172 ng_parse_generic_sockdata_getLength(const struct ng_parse_type *type, 173 const u_char *start, const u_char *buf) 174 { 175 const struct sockaddr *sa; 176 177 sa = (const struct sockaddr *)(buf - SADATA_OFFSET); 178 return (sa->sa_len < SADATA_OFFSET) ? 0 : sa->sa_len - SADATA_OFFSET; 179 } 180 181 /* Type for the variable length data portion of a generic struct sockaddr */ 182 static const struct ng_parse_type ng_ksocket_generic_sockdata_type = { 183 &ng_parse_bytearray_type, 184 &ng_parse_generic_sockdata_getLength 185 }; 186 187 /* Type for a generic struct sockaddr */ 188 static const struct ng_parse_struct_field 189 ng_parse_generic_sockaddr_type_fields[] = { 190 { "len", &ng_parse_uint8_type }, 191 { "family", &ng_parse_uint8_type }, 192 { "data", &ng_ksocket_generic_sockdata_type }, 193 { NULL } 194 }; 195 static const struct ng_parse_type ng_ksocket_generic_sockaddr_type = { 196 &ng_parse_struct_type, 197 &ng_parse_generic_sockaddr_type_fields 198 }; 199 200 /* Convert a struct sockaddr from ASCII to binary. If its a protocol 201 family that we specially handle, do that, otherwise defer to the 202 generic parse type ng_ksocket_generic_sockaddr_type. */ 203 static int 204 ng_ksocket_sockaddr_parse(const struct ng_parse_type *type, 205 const char *s, int *off, const u_char *const start, 206 u_char *const buf, int *buflen) 207 { 208 struct sockaddr *const sa = (struct sockaddr *)buf; 209 enum ng_parse_token tok; 210 char fambuf[32]; 211 int family, len; 212 char *t; 213 214 /* If next token is a left curly brace, use generic parse type */ 215 if ((tok = ng_parse_get_token(s, off, &len)) == T_LBRACE) { 216 return (*ng_ksocket_generic_sockaddr_type.supertype->parse) 217 (&ng_ksocket_generic_sockaddr_type, 218 s, off, start, buf, buflen); 219 } 220 221 /* Get socket address family followed by a slash */ 222 while (isspace(s[*off])) 223 (*off)++; 224 if ((t = index(s + *off, '/')) == NULL) 225 return (EINVAL); 226 if ((len = t - (s + *off)) > sizeof(fambuf) - 1) 227 return (EINVAL); 228 strncpy(fambuf, s + *off, len); 229 fambuf[len] = '\0'; 230 *off += len + 1; 231 if ((family = ng_ksocket_parse(ng_ksocket_families, fambuf, 0)) == -1) 232 return (EINVAL); 233 234 /* Set family */ 235 if (*buflen < SADATA_OFFSET) 236 return (ERANGE); 237 sa->sa_family = family; 238 239 /* Set family-specific data and length */ 240 switch (sa->sa_family) { 241 case PF_LOCAL: /* Get pathname */ 242 { 243 const int pathoff = OFFSETOF(struct sockaddr_un, sun_path); 244 struct sockaddr_un *const sun = (struct sockaddr_un *)sa; 245 int toklen, pathlen; 246 char *path; 247 248 if ((path = ng_get_string_token(s, off, &toklen, NULL)) == NULL) 249 return (EINVAL); 250 pathlen = strlen(path); 251 if (pathlen > SOCK_MAXADDRLEN) { 252 kfree(path, M_NETGRAPH_KSOCKET); 253 return (E2BIG); 254 } 255 if (*buflen < pathoff + pathlen) { 256 kfree(path, M_NETGRAPH_KSOCKET); 257 return (ERANGE); 258 } 259 *off += toklen; 260 bcopy(path, sun->sun_path, pathlen); 261 sun->sun_len = pathoff + pathlen; 262 kfree(path, M_NETGRAPH_KSOCKET); 263 break; 264 } 265 266 case PF_INET: /* Get an IP address with optional port */ 267 { 268 struct sockaddr_in *const sin = (struct sockaddr_in *)sa; 269 int i; 270 271 /* Parse this: <ipaddress>[:port] */ 272 for (i = 0; i < 4; i++) { 273 u_long val; 274 char *eptr; 275 276 val = strtoul(s + *off, &eptr, 10); 277 if (val > 0xff || eptr == s + *off) 278 return (EINVAL); 279 *off += (eptr - (s + *off)); 280 ((u_char *)&sin->sin_addr)[i] = (u_char)val; 281 if (i < 3) { 282 if (s[*off] != '.') 283 return (EINVAL); 284 (*off)++; 285 } else if (s[*off] == ':') { 286 (*off)++; 287 val = strtoul(s + *off, &eptr, 10); 288 if (val > 0xffff || eptr == s + *off) 289 return (EINVAL); 290 *off += (eptr - (s + *off)); 291 sin->sin_port = htons(val); 292 } else 293 sin->sin_port = 0; 294 } 295 bzero(&sin->sin_zero, sizeof(sin->sin_zero)); 296 sin->sin_len = sizeof(*sin); 297 break; 298 } 299 300 #if 0 301 case PF_INET6: 302 case PF_IPX: 303 #endif 304 305 default: 306 return (EINVAL); 307 } 308 309 /* Done */ 310 *buflen = sa->sa_len; 311 return (0); 312 } 313 314 /* Convert a struct sockaddr from binary to ASCII */ 315 static int 316 ng_ksocket_sockaddr_unparse(const struct ng_parse_type *type, 317 const u_char *data, int *off, char *cbuf, int cbuflen) 318 { 319 const struct sockaddr *sa = (const struct sockaddr *)(data + *off); 320 int slen = 0; 321 322 /* Output socket address, either in special or generic format */ 323 switch (sa->sa_family) { 324 case PF_LOCAL: 325 { 326 const int pathoff = OFFSETOF(struct sockaddr_un, sun_path); 327 const struct sockaddr_un *sun = (const struct sockaddr_un *)sa; 328 const int pathlen = sun->sun_len - pathoff; 329 char pathbuf[SOCK_MAXADDRLEN + 1]; 330 char *pathtoken; 331 332 bcopy(sun->sun_path, pathbuf, pathlen); 333 if ((pathtoken = ng_encode_string(pathbuf, pathlen)) == NULL) 334 return (ENOMEM); 335 slen += ksnprintf(cbuf, cbuflen, "local/%s", pathtoken); 336 kfree(pathtoken, M_NETGRAPH_KSOCKET); 337 if (slen >= cbuflen) 338 return (ERANGE); 339 *off += sun->sun_len; 340 return (0); 341 } 342 343 case PF_INET: 344 { 345 const struct sockaddr_in *sin = (const struct sockaddr_in *)sa; 346 347 slen += ksnprintf(cbuf, cbuflen, "inet/%d.%d.%d.%d", 348 ((const u_char *)&sin->sin_addr)[0], 349 ((const u_char *)&sin->sin_addr)[1], 350 ((const u_char *)&sin->sin_addr)[2], 351 ((const u_char *)&sin->sin_addr)[3]); 352 if (sin->sin_port != 0) { 353 slen += ksnprintf(cbuf + strlen(cbuf), 354 cbuflen - strlen(cbuf), ":%d", 355 (u_int)ntohs(sin->sin_port)); 356 } 357 if (slen >= cbuflen) 358 return (ERANGE); 359 *off += sizeof(*sin); 360 return(0); 361 } 362 363 #if 0 364 case PF_INET6: 365 case PF_IPX: 366 #endif 367 368 default: 369 return (*ng_ksocket_generic_sockaddr_type.supertype->unparse) 370 (&ng_ksocket_generic_sockaddr_type, 371 data, off, cbuf, cbuflen); 372 } 373 } 374 375 /* Parse type for struct sockaddr */ 376 static const struct ng_parse_type ng_ksocket_sockaddr_type = { 377 NULL, 378 NULL, 379 NULL, 380 &ng_ksocket_sockaddr_parse, 381 &ng_ksocket_sockaddr_unparse, 382 NULL /* no such thing as a default struct sockaddr */ 383 }; 384 385 /************************************************************************ 386 STRUCT NG_KSOCKET_SOCKOPT PARSE TYPE 387 ************************************************************************/ 388 389 /* Get length of the struct ng_ksocket_sockopt value field, which is the 390 just the excess of the message argument portion over the length of 391 the struct ng_ksocket_sockopt. */ 392 static int 393 ng_parse_sockoptval_getLength(const struct ng_parse_type *type, 394 const u_char *start, const u_char *buf) 395 { 396 static const int offset = OFFSETOF(struct ng_ksocket_sockopt, value); 397 const struct ng_ksocket_sockopt *sopt; 398 const struct ng_mesg *msg; 399 400 sopt = (const struct ng_ksocket_sockopt *)(buf - offset); 401 msg = (const struct ng_mesg *)((const u_char *)sopt - sizeof(*msg)); 402 return msg->header.arglen - sizeof(*sopt); 403 } 404 405 /* Parse type for the option value part of a struct ng_ksocket_sockopt 406 XXX Eventually, we should handle the different socket options specially. 407 XXX This would avoid byte order problems, eg an integer value of 1 is 408 XXX going to be "[1]" for little endian or "[3=1]" for big endian. */ 409 static const struct ng_parse_type ng_ksocket_sockoptval_type = { 410 &ng_parse_bytearray_type, 411 &ng_parse_sockoptval_getLength 412 }; 413 414 /* Parse type for struct ng_ksocket_sockopt */ 415 static const struct ng_parse_struct_field ng_ksocket_sockopt_type_fields[] 416 = NG_KSOCKET_SOCKOPT_INFO(&ng_ksocket_sockoptval_type); 417 static const struct ng_parse_type ng_ksocket_sockopt_type = { 418 &ng_parse_struct_type, 419 &ng_ksocket_sockopt_type_fields 420 }; 421 422 /* Parse type for struct ng_ksocket_accept */ 423 static const struct ng_parse_struct_field ng_ksocket_accept_type_fields[] 424 = NGM_KSOCKET_ACCEPT_INFO; 425 static const struct ng_parse_type ng_ksocket_accept_type = { 426 &ng_parse_struct_type, 427 &ng_ksocket_accept_type_fields 428 }; 429 430 /* List of commands and how to convert arguments to/from ASCII */ 431 static const struct ng_cmdlist ng_ksocket_cmds[] = { 432 { 433 NGM_KSOCKET_COOKIE, 434 NGM_KSOCKET_BIND, 435 "bind", 436 &ng_ksocket_sockaddr_type, 437 NULL 438 }, 439 { 440 NGM_KSOCKET_COOKIE, 441 NGM_KSOCKET_LISTEN, 442 "listen", 443 &ng_parse_int32_type, 444 NULL 445 }, 446 { 447 NGM_KSOCKET_COOKIE, 448 NGM_KSOCKET_ACCEPT, 449 "accept", 450 NULL, 451 &ng_ksocket_accept_type 452 }, 453 { 454 NGM_KSOCKET_COOKIE, 455 NGM_KSOCKET_CONNECT, 456 "connect", 457 &ng_ksocket_sockaddr_type, 458 &ng_parse_int32_type 459 }, 460 { 461 NGM_KSOCKET_COOKIE, 462 NGM_KSOCKET_GETNAME, 463 "getname", 464 NULL, 465 &ng_ksocket_sockaddr_type 466 }, 467 { 468 NGM_KSOCKET_COOKIE, 469 NGM_KSOCKET_GETPEERNAME, 470 "getpeername", 471 NULL, 472 &ng_ksocket_sockaddr_type 473 }, 474 { 475 NGM_KSOCKET_COOKIE, 476 NGM_KSOCKET_SETOPT, 477 "setopt", 478 &ng_ksocket_sockopt_type, 479 NULL 480 }, 481 { 482 NGM_KSOCKET_COOKIE, 483 NGM_KSOCKET_GETOPT, 484 "getopt", 485 &ng_ksocket_sockopt_type, 486 &ng_ksocket_sockopt_type 487 }, 488 { 0 } 489 }; 490 491 /* Node type descriptor */ 492 static struct ng_type ng_ksocket_typestruct = { 493 .version = NG_ABI_VERSION, 494 .name = NG_KSOCKET_NODE_TYPE, 495 .constructor = ng_ksocket_constructor, 496 .rcvmsg = ng_ksocket_rcvmsg, 497 .shutdown = ng_ksocket_shutdown, 498 .newhook = ng_ksocket_newhook, 499 .connect = ng_ksocket_connect, 500 .rcvdata = ng_ksocket_rcvdata, 501 .disconnect = ng_ksocket_disconnect, 502 .cmdlist = ng_ksocket_cmds, 503 }; 504 NETGRAPH_INIT(ksocket, &ng_ksocket_typestruct); 505 506 #define ERROUT(x) do { error = (x); goto done; } while (0) 507 508 /************************************************************************ 509 NETGRAPH NODE STUFF 510 ************************************************************************/ 511 512 /* 513 * Node type constructor 514 * The NODE part is assumed to be all set up. 515 * There is already a reference to the node for us. 516 */ 517 static int 518 ng_ksocket_constructor(node_p node) 519 { 520 priv_p priv; 521 522 /* Allocate private structure */ 523 priv = kmalloc(sizeof(*priv), M_NETGRAPH, 524 M_WAITOK | M_NULLOK | M_ZERO); 525 if (priv == NULL) 526 return (ENOMEM); 527 528 LIST_INIT(&priv->embryos); 529 /* cross link them */ 530 priv->node = node; 531 NG_NODE_SET_PRIVATE(node, priv); 532 533 /* Done */ 534 return (0); 535 } 536 537 /* 538 * Give our OK for a hook to be added. The hook name is of the 539 * form "<family>/<type>/<proto>" where the three components may 540 * be decimal numbers or else aliases from the above lists. 541 * 542 * Connecting a hook amounts to opening the socket. Disconnecting 543 * the hook closes the socket and destroys the node as well. 544 */ 545 static int 546 ng_ksocket_newhook(node_p node, hook_p hook, const char *name0) 547 { 548 struct thread *td = curthread->td_proc ? curthread : &thread0; /* XXX broken */ 549 const priv_p priv = NG_NODE_PRIVATE(node); 550 char *s1, *s2, name[NG_HOOKSIZ]; 551 int family, type, protocol, error; 552 553 /* Check if we're already connected */ 554 if (priv->hook != NULL) 555 return (EISCONN); 556 557 if (priv->flags & KSF_CLONED) { 558 if (priv->flags & KSF_EMBRYONIC) { 559 /* Remove ourselves from our parent's embryo list */ 560 LIST_REMOVE(priv, siblings); 561 priv->flags &= ~KSF_EMBRYONIC; 562 } 563 } else { 564 /* Extract family, type, and protocol from hook name */ 565 ksnprintf(name, sizeof(name), "%s", name0); 566 s1 = name; 567 if ((s2 = index(s1, '/')) == NULL) 568 return (EINVAL); 569 *s2++ = '\0'; 570 family = ng_ksocket_parse(ng_ksocket_families, s1, 0); 571 if (family == -1) 572 return (EINVAL); 573 s1 = s2; 574 if ((s2 = index(s1, '/')) == NULL) 575 return (EINVAL); 576 *s2++ = '\0'; 577 type = ng_ksocket_parse(ng_ksocket_types, s1, 0); 578 if (type == -1) 579 return (EINVAL); 580 s1 = s2; 581 protocol = ng_ksocket_parse(ng_ksocket_protos, s1, family); 582 if (protocol == -1) 583 return (EINVAL); 584 585 /* Create the socket */ 586 error = socreate(family, &priv->so, type, protocol, td); 587 if (error != 0) 588 return (error); 589 590 /* XXX call soreserve() ? */ 591 592 } 593 594 /* OK */ 595 priv->hook = hook; 596 597 /* 598 * In case of misconfigured routing a packet may reenter 599 * ksocket node recursively. Decouple stack to avoid possible 600 * panics about sleeping with locks held. 601 */ 602 NG_HOOK_FORCE_QUEUE(hook); 603 604 return(0); 605 } 606 607 static int 608 ng_ksocket_connect(hook_p hook) 609 { 610 node_p node = NG_HOOK_NODE(hook); 611 const priv_p priv = NG_NODE_PRIVATE(node); 612 struct socket *const so = priv->so; 613 614 /* Add our hook for incoming data and other events */ 615 priv->so->so_upcallarg = (caddr_t)node; 616 priv->so->so_upcall = ng_ksocket_incoming; 617 atomic_set_int(&priv->so->so_rcv.ssb_flags, SSB_UPCALL); 618 atomic_set_int(&priv->so->so_snd.ssb_flags, SSB_UPCALL); 619 /* 620 * --Original comment-- 621 * On a cloned socket we may have already received one or more 622 * upcalls which we couldn't handle without a hook. Handle 623 * those now. 624 * We cannot call the upcall function directly 625 * from here, because until this function has returned our 626 * hook isn't connected. 627 * 628 * ---meta comment for -current --- 629 * XXX This is dubius. 630 * Upcalls between the time that the hook was 631 * first created and now (on another processesor) will 632 * be earlier on the queue than the request to finalise the hook. 633 * By the time the hook is finalised, 634 * The queued upcalls will have happenned and the code 635 * will have discarded them because of a lack of a hook. 636 * (socket not open). 637 * 638 * This is a bad byproduct of the complicated way in which hooks 639 * are now created (3 daisy chained async events). 640 * 641 * Since we are a netgraph operation 642 * We know that we hold a lock on this node. This forces the 643 * request we make below to be queued rather than implemented 644 * immediatly which will cause the upcall function to be called a bit 645 * later. 646 * However, as we will run any waiting queued operations immediatly 647 * after doing this one, if we have not finalised the other end 648 * of the hook, those queued operations will fail. 649 */ 650 if (priv->flags & KSF_CLONED) { 651 ng_send_fn(node, NULL, &ng_ksocket_incoming2, so, M_WAITOK | M_NULLOK); 652 } 653 654 return (0); 655 } 656 657 /* 658 * Receive a control message 659 */ 660 static int 661 ng_ksocket_rcvmsg(node_p node, item_p item, hook_p lasthook) 662 { 663 struct thread *td = curthread->td_proc ? curthread : &thread0; /* XXX broken */ 664 const priv_p priv = NG_NODE_PRIVATE(node); 665 struct socket *const so = priv->so; 666 struct ng_mesg *resp = NULL; 667 int error = 0; 668 struct ng_mesg *msg; 669 670 NGI_GET_MSG(item, msg); 671 switch (msg->header.typecookie) { 672 case NGM_KSOCKET_COOKIE: 673 switch (msg->header.cmd) { 674 case NGM_KSOCKET_BIND: 675 { 676 struct sockaddr *const sa 677 = (struct sockaddr *)msg->data; 678 679 /* Sanity check */ 680 if (msg->header.arglen < SADATA_OFFSET 681 || msg->header.arglen < sa->sa_len) 682 ERROUT(EINVAL); 683 if (so == NULL) 684 ERROUT(ENXIO); 685 686 /* Bind */ 687 error = sobind(so, sa, td); 688 break; 689 } 690 case NGM_KSOCKET_LISTEN: 691 { 692 /* Sanity check */ 693 if (msg->header.arglen != sizeof(int32_t)) 694 ERROUT(EINVAL); 695 if (so == NULL) 696 ERROUT(ENXIO); 697 698 /* Listen */ 699 error = solisten(so, *((int32_t *)msg->data), td); 700 break; 701 } 702 703 case NGM_KSOCKET_ACCEPT: 704 { 705 /* Sanity check */ 706 if (msg->header.arglen != 0) 707 ERROUT(EINVAL); 708 if (so == NULL) 709 ERROUT(ENXIO); 710 711 /* Make sure the socket is capable of accepting */ 712 if (!(so->so_options & SO_ACCEPTCONN)) 713 ERROUT(EINVAL); 714 if (priv->flags & KSF_ACCEPTING) 715 ERROUT(EALREADY); 716 717 error = ng_ksocket_check_accept(priv); 718 if (error != 0 && error != EWOULDBLOCK) 719 ERROUT(error); 720 721 /* 722 * If a connection is already complete, take it. 723 * Otherwise let the upcall function deal with 724 * the connection when it comes in. 725 */ 726 priv->response_token = msg->header.token; 727 priv->response_addr = NGI_RETADDR(item); 728 if (error == 0) { 729 ng_ksocket_finish_accept(priv); 730 } else 731 priv->flags |= KSF_ACCEPTING; 732 break; 733 } 734 735 case NGM_KSOCKET_CONNECT: 736 { 737 struct sockaddr *const sa 738 = (struct sockaddr *)msg->data; 739 740 /* Sanity check */ 741 if (msg->header.arglen < SADATA_OFFSET 742 || msg->header.arglen < sa->sa_len) 743 ERROUT(EINVAL); 744 if (so == NULL) 745 ERROUT(ENXIO); 746 747 /* Do connect */ 748 if ((so->so_state & SS_ISCONNECTING) != 0) 749 ERROUT(EALREADY); 750 if ((error = soconnect(so, sa, td, TRUE)) != 0) { 751 soclrstate(so, SS_ISCONNECTING); 752 ERROUT(error); 753 } 754 if ((so->so_state & SS_ISCONNECTING) != 0) { 755 /* We will notify the sender when we connect */ 756 priv->response_token = msg->header.token; 757 priv->response_addr = NGI_RETADDR(item); 758 priv->flags |= KSF_CONNECTING; 759 ERROUT(EINPROGRESS); 760 } 761 break; 762 } 763 764 case NGM_KSOCKET_GETNAME: 765 case NGM_KSOCKET_GETPEERNAME: 766 { 767 struct sockaddr *sa = NULL; 768 int len; 769 770 /* Sanity check */ 771 if (msg->header.arglen != 0) 772 ERROUT(EINVAL); 773 if (so == NULL) 774 ERROUT(ENXIO); 775 776 /* Get function */ 777 if (msg->header.cmd == NGM_KSOCKET_GETPEERNAME) { 778 if ((so->so_state 779 & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) 780 ERROUT(ENOTCONN); 781 error = so_pru_peeraddr(so, &sa); 782 } else { 783 error = so_pru_sockaddr(so, &sa); 784 } 785 786 /* Get local or peer address */ 787 if (error != 0) 788 goto bail; 789 len = (sa == NULL) ? 0 : sa->sa_len; 790 791 /* Send it back in a response */ 792 NG_MKRESPONSE(resp, msg, len, M_WAITOK | M_NULLOK); 793 if (resp == NULL) { 794 error = ENOMEM; 795 goto bail; 796 } 797 bcopy(sa, resp->data, len); 798 799 bail: 800 /* Cleanup */ 801 if (sa != NULL) 802 kfree(sa, M_SONAME); 803 break; 804 } 805 806 case NGM_KSOCKET_GETOPT: 807 { 808 struct ng_ksocket_sockopt *ksopt = 809 (struct ng_ksocket_sockopt *)msg->data; 810 struct sockopt sopt; 811 812 /* Sanity check */ 813 if (msg->header.arglen != sizeof(*ksopt)) 814 ERROUT(EINVAL); 815 if (so == NULL) 816 ERROUT(ENXIO); 817 818 /* Get response with room for option value */ 819 NG_MKRESPONSE(resp, msg, sizeof(*ksopt) 820 + NG_KSOCKET_MAX_OPTLEN, M_WAITOK | M_NULLOK); 821 if (resp == NULL) 822 ERROUT(ENOMEM); 823 824 /* Get socket option, and put value in the response */ 825 sopt.sopt_dir = SOPT_GET; 826 sopt.sopt_level = ksopt->level; 827 sopt.sopt_name = ksopt->name; 828 sopt.sopt_td = NULL; 829 sopt.sopt_valsize = NG_KSOCKET_MAX_OPTLEN; 830 ksopt = (struct ng_ksocket_sockopt *)resp->data; 831 sopt.sopt_val = ksopt->value; 832 if ((error = sogetopt(so, &sopt)) != 0) { 833 NG_FREE_MSG(resp); 834 break; 835 } 836 837 /* Set actual value length */ 838 resp->header.arglen = sizeof(*ksopt) 839 + sopt.sopt_valsize; 840 break; 841 } 842 843 case NGM_KSOCKET_SETOPT: 844 { 845 struct ng_ksocket_sockopt *const ksopt = 846 (struct ng_ksocket_sockopt *)msg->data; 847 const int valsize = msg->header.arglen - sizeof(*ksopt); 848 struct sockopt sopt; 849 850 /* Sanity check */ 851 if (valsize < 0) 852 ERROUT(EINVAL); 853 if (so == NULL) 854 ERROUT(ENXIO); 855 856 /* Set socket option */ 857 sopt.sopt_dir = SOPT_SET; 858 sopt.sopt_level = ksopt->level; 859 sopt.sopt_name = ksopt->name; 860 sopt.sopt_val = ksopt->value; 861 sopt.sopt_valsize = valsize; 862 sopt.sopt_td = NULL; 863 error = sosetopt(so, &sopt); 864 break; 865 } 866 867 default: 868 error = EINVAL; 869 break; 870 } 871 break; 872 default: 873 error = EINVAL; 874 break; 875 } 876 done: 877 NG_RESPOND_MSG(error, node, item, resp); 878 NG_FREE_MSG(msg); 879 return (error); 880 } 881 882 /* 883 * Receive incoming data on our hook. Send it out the socket. 884 */ 885 static int 886 ng_ksocket_rcvdata(hook_p hook, item_p item) 887 { 888 struct thread *td = curthread->td_proc ? curthread : &thread0; /* XXX broken */ 889 const node_p node = NG_HOOK_NODE(hook); 890 const priv_p priv = NG_NODE_PRIVATE(node); 891 struct socket *const so = priv->so; 892 struct sockaddr *sa = NULL; 893 int error; 894 struct mbuf *m; 895 struct sa_tag *stag; 896 897 /* Extract data */ 898 NGI_GET_M(item, m); 899 NG_FREE_ITEM(item); 900 901 /* 902 * Look if socket address is stored in packet tags. 903 * If sockaddr is ours, or provided by a third party (zero id), 904 * then we accept it. 905 */ 906 if (((stag = (struct sa_tag *)m_tag_locate(m, NGM_KSOCKET_COOKIE, 907 NG_KSOCKET_TAG_SOCKADDR, NULL)) != NULL) && 908 (stag->id == NG_NODE_ID(node) || stag->id == 0)) 909 sa = &stag->sa; 910 911 /* Reset specific mbuf flags to prevent addressing problems. */ 912 m->m_flags &= ~(M_BCAST|M_MCAST); 913 914 /* Send packet */ 915 error = sosend(so, sa, 0, m, 0, 0, td); 916 917 return (error); 918 } 919 920 /* 921 * Destroy node 922 */ 923 static int 924 ng_ksocket_shutdown(node_p node) 925 { 926 const priv_p priv = NG_NODE_PRIVATE(node); 927 priv_p embryo; 928 929 /* Close our socket (if any) */ 930 if (priv->so != NULL) { 931 atomic_clear_int(&priv->so->so_rcv.ssb_flags, SSB_UPCALL); 932 atomic_clear_int(&priv->so->so_snd.ssb_flags, SSB_UPCALL); 933 priv->so->so_upcall = NULL; 934 soclose(priv->so, FNONBLOCK); 935 priv->so = NULL; 936 } 937 938 /* If we are an embryo, take ourselves out of the parent's list */ 939 if (priv->flags & KSF_EMBRYONIC) { 940 LIST_REMOVE(priv, siblings); 941 priv->flags &= ~KSF_EMBRYONIC; 942 } 943 944 /* Remove any embryonic children we have */ 945 while (!LIST_EMPTY(&priv->embryos)) { 946 embryo = LIST_FIRST(&priv->embryos); 947 ng_rmnode_self(embryo->node); 948 } 949 950 /* Take down netgraph node */ 951 bzero(priv, sizeof(*priv)); 952 kfree(priv, M_NETGRAPH); 953 NG_NODE_SET_PRIVATE(node, NULL); 954 NG_NODE_UNREF(node); /* let the node escape */ 955 return (0); 956 } 957 958 /* 959 * Hook disconnection 960 */ 961 static int 962 ng_ksocket_disconnect(hook_p hook) 963 { 964 KASSERT(NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0, 965 ("%s: numhooks=%d?", __func__, 966 NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)))); 967 if (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))) 968 ng_rmnode_self(NG_HOOK_NODE(hook)); 969 return (0); 970 } 971 972 /************************************************************************ 973 HELPER STUFF 974 ************************************************************************/ 975 /* 976 * You should not "just call" a netgraph node function from an external 977 * asynchronous event. This is because in doing so you are ignoring the 978 * locking on the netgraph nodes. Instead call your function via ng_send_fn(). 979 * This will call the function you chose, but will first do all the 980 * locking rigmarole. Your function MAY only be called at some distant future 981 * time (several millisecs away) so don't give it any arguments 982 * that may be revoked soon (e.g. on your stack). 983 * 984 * To decouple stack, we use queue version of ng_send_fn(). 985 */ 986 987 static void 988 ng_ksocket_incoming(struct socket *so, void *arg, int waitflag) 989 { 990 const node_p node = arg; 991 const priv_p priv = NG_NODE_PRIVATE(node); 992 int wait = ((waitflag & M_WAITOK) ? NG_WAITOK : 0) | NG_QUEUE; 993 994 /* 995 * Even if node is not locked, as soon as we are called, we assume 996 * it exist and it's private area is valid. With some care we can 997 * access it. Mark node that incoming event for it was sent to 998 * avoid unneded queue trashing. 999 */ 1000 if (atomic_cmpset_int(&priv->fn_sent, 0, 1) && 1001 ng_send_fn1(node, NULL, &ng_ksocket_incoming2, so, 0, wait)) { 1002 atomic_store_rel_int(&priv->fn_sent, 0); 1003 } 1004 } 1005 1006 1007 /* 1008 * When incoming data is appended to the socket, we get notified here. 1009 * This is also called whenever a significant event occurs for the socket. 1010 * Our original caller may have queued this even some time ago and 1011 * we cannot trust that he even still exists. The node however is being 1012 * held with a reference by the queueing code and guarantied to be valid. 1013 */ 1014 static void 1015 ng_ksocket_incoming2(node_p node, hook_p hook, void *arg1, int arg2) 1016 { 1017 struct socket *so = arg1; 1018 const priv_p priv = NG_NODE_PRIVATE(node); 1019 struct ng_mesg *response; 1020 int flags, error; 1021 1022 crit_enter(); 1023 1024 /* so = priv->so; *//* XXX could have derived this like so */ 1025 KASSERT(so == priv->so, ("%s: wrong socket", __func__)); 1026 1027 /* Allow next incoming event to be queued. */ 1028 atomic_store_rel_int(&priv->fn_sent, 0); 1029 1030 /* Check whether a pending connect operation has completed */ 1031 if (priv->flags & KSF_CONNECTING) { 1032 if ((error = so->so_error) != 0) { 1033 so->so_error = 0; 1034 soclrstate(so, SS_ISCONNECTING); 1035 } 1036 if (!(so->so_state & SS_ISCONNECTING)) { 1037 NG_MKMESSAGE(response, NGM_KSOCKET_COOKIE, 1038 NGM_KSOCKET_CONNECT, sizeof(int32_t), M_WAITOK | M_NULLOK); 1039 if (response != NULL) { 1040 response->header.flags |= NGF_RESP; 1041 response->header.token = priv->response_token; 1042 *(int32_t *)response->data = error; 1043 /* 1044 * send an async "response" message 1045 * to the node that set us up 1046 * (if it still exists) 1047 */ 1048 NG_SEND_MSG_ID(error, node, 1049 response, priv->response_addr, 0); 1050 } 1051 priv->flags &= ~KSF_CONNECTING; 1052 } 1053 } 1054 1055 /* Check whether a pending accept operation has completed */ 1056 if (priv->flags & KSF_ACCEPTING) { 1057 error = ng_ksocket_check_accept(priv); 1058 if (error != EWOULDBLOCK) 1059 priv->flags &= ~KSF_ACCEPTING; 1060 if (error == 0) 1061 ng_ksocket_finish_accept(priv); 1062 } 1063 1064 /* 1065 * If we don't have a hook, we must handle data events later. When 1066 * the hook gets created and is connected, this upcall function 1067 * will be called again. 1068 */ 1069 if (priv->hook == NULL) { 1070 crit_exit(); 1071 return; 1072 } 1073 1074 /* Read and forward available mbuf's */ 1075 while (1) { 1076 struct sockaddr *sa = NULL; 1077 struct sockbuf sio; 1078 struct mbuf *n; 1079 1080 sbinit(&sio, 1000000000); 1081 flags = MSG_DONTWAIT; 1082 1083 /* Try to get next packet from socket */ 1084 error = soreceive(so, 1085 ((so->so_state & SS_ISCONNECTED) ? NULL : &sa), 1086 NULL, &sio, NULL, &flags); 1087 if (error) 1088 break; 1089 1090 /* See if we got anything */ 1091 if (sio.sb_mb == NULL) { 1092 if (sa != NULL) 1093 kfree(sa, M_SONAME); 1094 break; 1095 } 1096 1097 /* 1098 * Don't trust the various socket layers to get the 1099 * packet header and length correct (e.g. kern/15175). 1100 * 1101 * Also, do not trust that soreceive() will clear m_nextpkt 1102 * for us (e.g. kern/84952, kern/82413). 1103 */ 1104 sio.sb_mb->m_pkthdr.csum_flags = 0; 1105 sio.sb_mb->m_pkthdr.len = 0; 1106 for (n = sio.sb_mb; n != NULL; n = n->m_next) { 1107 sio.sb_mb->m_pkthdr.len += n->m_len; 1108 n->m_nextpkt = NULL; 1109 } 1110 1111 /* Put peer's socket address (if any) into a tag */ 1112 if (sa != NULL) { 1113 struct sa_tag *stag; 1114 1115 stag = (struct sa_tag *)m_tag_alloc(NGM_KSOCKET_COOKIE, 1116 NG_KSOCKET_TAG_SOCKADDR, sizeof(ng_ID_t) + 1117 sa->sa_len, MB_DONTWAIT); 1118 if (stag == NULL) { 1119 kfree(sa, M_SONAME); 1120 goto sendit; 1121 } 1122 bcopy(sa, &stag->sa, sa->sa_len); 1123 kfree(sa, M_SONAME); 1124 stag->id = NG_NODE_ID(node); 1125 m_tag_prepend(sio.sb_mb, &stag->tag); 1126 } 1127 1128 sendit: /* Forward data with optional peer sockaddr as packet tag */ 1129 NG_SEND_DATA_ONLY(error, priv->hook, sio.sb_mb); 1130 } 1131 1132 /* 1133 * If the peer has closed the connection, forward a 0-length mbuf 1134 * to indicate end-of-file. 1135 */ 1136 if (so->so_state & SS_CANTRCVMORE && !(priv->flags & KSF_EOFSEEN)) { 1137 struct mbuf *m; 1138 1139 MGETHDR(m, MB_DONTWAIT, MT_DATA); 1140 if (m != NULL) { 1141 m->m_len = m->m_pkthdr.len = 0; 1142 NG_SEND_DATA_ONLY(error, priv->hook, m); 1143 } 1144 priv->flags |= KSF_EOFSEEN; 1145 } 1146 crit_exit(); 1147 } 1148 1149 /* 1150 * Check for a completed incoming connection and return 0 if one is found. 1151 * Otherwise return the appropriate error code. 1152 */ 1153 static int 1154 ng_ksocket_check_accept(priv_p priv) 1155 { 1156 struct socket *const head = priv->so; 1157 int error; 1158 1159 if ((error = head->so_error) != 0) { 1160 head->so_error = 0; 1161 return error; 1162 } 1163 /* Unlocked read. */ 1164 if (TAILQ_EMPTY(&head->so_comp)) { 1165 if (head->so_state & SS_CANTRCVMORE) 1166 return ECONNABORTED; 1167 return EWOULDBLOCK; 1168 } 1169 return 0; 1170 } 1171 1172 /* 1173 * Handle the first completed incoming connection, assumed to be already 1174 * on the socket's so_comp queue. 1175 */ 1176 static void 1177 ng_ksocket_finish_accept(priv_p priv) 1178 { 1179 struct socket *const head = priv->so; 1180 struct socket *so; 1181 struct sockaddr *sa = NULL; 1182 struct ng_mesg *resp; 1183 struct ng_ksocket_accept *resp_data; 1184 node_p node; 1185 priv_p priv2; 1186 int len; 1187 int error; 1188 1189 lwkt_getpooltoken(head); 1190 so = TAILQ_FIRST(&head->so_comp); 1191 if (so == NULL) { /* Should never happen */ 1192 lwkt_relpooltoken(head); 1193 return; 1194 } 1195 TAILQ_REMOVE(&head->so_comp, so, so_list); 1196 head->so_qlen--; 1197 soclrstate(so, SS_COMP); 1198 so->so_head = NULL; 1199 soreference(so); 1200 lwkt_relpooltoken(head); 1201 1202 /* XXX KNOTE(&head->so_rcv.ssb_sel.si_note, 0); */ 1203 1204 soaccept(so, &sa); 1205 1206 len = OFFSETOF(struct ng_ksocket_accept, addr); 1207 if (sa != NULL) 1208 len += sa->sa_len; 1209 1210 NG_MKMESSAGE(resp, NGM_KSOCKET_COOKIE, NGM_KSOCKET_ACCEPT, len, 1211 M_WAITOK | M_NULLOK); 1212 if (resp == NULL) { 1213 soclose(so, FNONBLOCK); 1214 goto out; 1215 } 1216 resp->header.flags |= NGF_RESP; 1217 resp->header.token = priv->response_token; 1218 1219 /* Clone a ksocket node to wrap the new socket */ 1220 error = ng_make_node_common(&ng_ksocket_typestruct, &node); 1221 if (error) { 1222 kfree(resp, M_NETGRAPH); 1223 soclose(so, FNONBLOCK); 1224 goto out; 1225 } 1226 1227 if (ng_ksocket_constructor(node) != 0) { 1228 NG_NODE_UNREF(node); 1229 kfree(resp, M_NETGRAPH); 1230 soclose(so, FNONBLOCK); 1231 goto out; 1232 } 1233 1234 priv2 = NG_NODE_PRIVATE(node); 1235 priv2->so = so; 1236 priv2->flags |= KSF_CLONED | KSF_EMBRYONIC; 1237 1238 /* 1239 * Insert the cloned node into a list of embryonic children 1240 * on the parent node. When a hook is created on the cloned 1241 * node it will be removed from this list. When the parent 1242 * is destroyed it will destroy any embryonic children it has. 1243 */ 1244 LIST_INSERT_HEAD(&priv->embryos, priv2, siblings); 1245 1246 so->so_upcallarg = (caddr_t)node; 1247 so->so_upcall = ng_ksocket_incoming; 1248 atomic_set_int(&priv->so->so_rcv.ssb_flags, SSB_UPCALL); 1249 atomic_set_int(&priv->so->so_snd.ssb_flags, SSB_UPCALL); 1250 1251 /* Fill in the response data and send it or return it to the caller */ 1252 resp_data = (struct ng_ksocket_accept *)resp->data; 1253 resp_data->nodeid = NG_NODE_ID(node); 1254 if (sa != NULL) 1255 bcopy(sa, &resp_data->addr, sa->sa_len); 1256 NG_SEND_MSG_ID(error, node, resp, priv->response_addr, 0); 1257 1258 out: 1259 if (sa != NULL) 1260 kfree(sa, M_SONAME); 1261 } 1262 1263 /* 1264 * Parse out either an integer value or an alias. 1265 */ 1266 static int 1267 ng_ksocket_parse(const struct ng_ksocket_alias *aliases, 1268 const char *s, int family) 1269 { 1270 int k, val; 1271 char *eptr; 1272 1273 /* Try aliases */ 1274 for (k = 0; aliases[k].name != NULL; k++) { 1275 if (strcmp(s, aliases[k].name) == 0 1276 && aliases[k].family == family) 1277 return aliases[k].value; 1278 } 1279 1280 /* Try parsing as a number */ 1281 val = (int)strtoul(s, &eptr, 10); 1282 if (val < 0 || *eptr != '\0') 1283 return (-1); 1284 return (val); 1285 } 1286 1287