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