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