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