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