1cb3c7a5dSArchie Cobbs 2cb3c7a5dSArchie Cobbs /* 3cb3c7a5dSArchie Cobbs * ng_ksocket.c 4cb3c7a5dSArchie Cobbs * 5cb3c7a5dSArchie Cobbs * Copyright (c) 1996-1999 Whistle Communications, Inc. 6cb3c7a5dSArchie Cobbs * All rights reserved. 7cb3c7a5dSArchie Cobbs * 8cb3c7a5dSArchie Cobbs * Subject to the following obligations and disclaimer of warranty, use and 9cb3c7a5dSArchie Cobbs * redistribution of this software, in source or object code forms, with or 10cb3c7a5dSArchie Cobbs * without modifications are expressly permitted by Whistle Communications; 11cb3c7a5dSArchie Cobbs * provided, however, that: 12cb3c7a5dSArchie Cobbs * 1. Any and all reproductions of the source or object code must include the 13cb3c7a5dSArchie Cobbs * copyright notice above and the following disclaimer of warranties; and 14cb3c7a5dSArchie Cobbs * 2. No rights are granted, in any manner or form, to use Whistle 15cb3c7a5dSArchie Cobbs * Communications, Inc. trademarks, including the mark "WHISTLE 16cb3c7a5dSArchie Cobbs * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 17cb3c7a5dSArchie Cobbs * such appears in the above copyright notice or in the software. 18cb3c7a5dSArchie Cobbs * 19cb3c7a5dSArchie Cobbs * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 20cb3c7a5dSArchie Cobbs * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 21cb3c7a5dSArchie Cobbs * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 22cb3c7a5dSArchie Cobbs * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 23cb3c7a5dSArchie Cobbs * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 24cb3c7a5dSArchie Cobbs * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 25cb3c7a5dSArchie Cobbs * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 26cb3c7a5dSArchie Cobbs * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 27cb3c7a5dSArchie Cobbs * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 28cb3c7a5dSArchie Cobbs * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 29cb3c7a5dSArchie Cobbs * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 30cb3c7a5dSArchie Cobbs * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 31cb3c7a5dSArchie Cobbs * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 32cb3c7a5dSArchie Cobbs * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 33cb3c7a5dSArchie Cobbs * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 34cb3c7a5dSArchie Cobbs * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 35cb3c7a5dSArchie Cobbs * OF SUCH DAMAGE. 36cb3c7a5dSArchie Cobbs * 37cc3bbd68SJulian Elischer * Author: Archie Cobbs <archie@freebsd.org> 38cb3c7a5dSArchie Cobbs * 39cb3c7a5dSArchie Cobbs * $FreeBSD$ 40cb3c7a5dSArchie Cobbs * $Whistle: ng_ksocket.c,v 1.1 1999/11/16 20:04:40 archie Exp $ 41cb3c7a5dSArchie Cobbs */ 42cb3c7a5dSArchie Cobbs 43cb3c7a5dSArchie Cobbs /* 44cb3c7a5dSArchie Cobbs * Kernel socket node type. This node type is basically a kernel-mode 45cb3c7a5dSArchie Cobbs * version of a socket... kindof like the reverse of the socket node type. 46cb3c7a5dSArchie Cobbs */ 47cb3c7a5dSArchie Cobbs 48cb3c7a5dSArchie Cobbs #include <sys/param.h> 49cb3c7a5dSArchie Cobbs #include <sys/systm.h> 50cb3c7a5dSArchie Cobbs #include <sys/kernel.h> 51cb3c7a5dSArchie Cobbs #include <sys/mbuf.h> 52cb3c7a5dSArchie Cobbs #include <sys/proc.h> 53cb3c7a5dSArchie Cobbs #include <sys/malloc.h> 54f8307e12SArchie Cobbs #include <sys/ctype.h> 55cb3c7a5dSArchie Cobbs #include <sys/protosw.h> 56cb3c7a5dSArchie Cobbs #include <sys/errno.h> 57cb3c7a5dSArchie Cobbs #include <sys/socket.h> 58cb3c7a5dSArchie Cobbs #include <sys/socketvar.h> 59cb3c7a5dSArchie Cobbs #include <sys/uio.h> 60f8307e12SArchie Cobbs #include <sys/un.h> 61cb3c7a5dSArchie Cobbs 62cb3c7a5dSArchie Cobbs #include <netgraph/ng_message.h> 63cb3c7a5dSArchie Cobbs #include <netgraph/netgraph.h> 64f8307e12SArchie Cobbs #include <netgraph/ng_parse.h> 65cb3c7a5dSArchie Cobbs #include <netgraph/ng_ksocket.h> 66cb3c7a5dSArchie Cobbs 67cb3c7a5dSArchie Cobbs #include <netinet/in.h> 68cb3c7a5dSArchie Cobbs #include <netatalk/at.h> 69cb3c7a5dSArchie Cobbs 709c8c302fSJulian Elischer #ifdef NG_SEPARATE_MALLOC 719c8c302fSJulian Elischer MALLOC_DEFINE(M_NETGRAPH_KSOCKET, "netgraph_ksock", "netgraph ksock node "); 729c8c302fSJulian Elischer #else 739c8c302fSJulian Elischer #define M_NETGRAPH_KSOCKET M_NETGRAPH 749c8c302fSJulian Elischer #endif 759c8c302fSJulian Elischer 76f8307e12SArchie Cobbs #define OFFSETOF(s, e) ((char *)&((s *)0)->e - (char *)((s *)0)) 77f8307e12SArchie Cobbs #define SADATA_OFFSET (OFFSETOF(struct sockaddr, sa_data)) 78f8307e12SArchie Cobbs 79cb3c7a5dSArchie Cobbs /* Node private data */ 8019bff684SArchie Cobbs struct ng_ksocket_private { 81f97e0a07SJulian Elischer node_p node; 82cb3c7a5dSArchie Cobbs hook_p hook; 83cb3c7a5dSArchie Cobbs struct socket *so; 84f97e0a07SJulian Elischer LIST_HEAD(, ng_ksocket_private) embryos; 85f97e0a07SJulian Elischer LIST_ENTRY(ng_ksocket_private) siblings; 86f97e0a07SJulian Elischer u_int32_t flags; 87f97e0a07SJulian Elischer u_int32_t response_token; 88f97e0a07SJulian Elischer ng_ID_t response_addr; 89cb3c7a5dSArchie Cobbs }; 9019bff684SArchie Cobbs typedef struct ng_ksocket_private *priv_p; 91cb3c7a5dSArchie Cobbs 92f97e0a07SJulian Elischer /* Flags for priv_p */ 93f97e0a07SJulian Elischer #define KSF_CONNECTING 0x00000001 /* Waiting for connection complete */ 94f97e0a07SJulian Elischer #define KSF_ACCEPTING 0x00000002 /* Waiting for accept complete */ 95f97e0a07SJulian Elischer #define KSF_EOFSEEN 0x00000004 /* Have sent 0-length EOF mbuf */ 96f97e0a07SJulian Elischer #define KSF_CLONED 0x00000008 /* Cloned from an accepting socket */ 97f97e0a07SJulian Elischer #define KSF_EMBRYONIC 0x00000010 /* Cloned node with no hooks yet */ 98dc9c2e01SArchie Cobbs #define KSF_SENDING 0x00000020 /* Sending on socket */ 99f97e0a07SJulian Elischer 100cb3c7a5dSArchie Cobbs /* Netgraph node methods */ 101cb3c7a5dSArchie Cobbs static ng_constructor_t ng_ksocket_constructor; 102cb3c7a5dSArchie Cobbs static ng_rcvmsg_t ng_ksocket_rcvmsg; 103069154d5SJulian Elischer static ng_shutdown_t ng_ksocket_shutdown; 104cb3c7a5dSArchie Cobbs static ng_newhook_t ng_ksocket_newhook; 105cb3c7a5dSArchie Cobbs static ng_rcvdata_t ng_ksocket_rcvdata; 106f97e0a07SJulian Elischer static ng_connect_t ng_ksocket_connect; 107cb3c7a5dSArchie Cobbs static ng_disconnect_t ng_ksocket_disconnect; 108cb3c7a5dSArchie Cobbs 109cb3c7a5dSArchie Cobbs /* Alias structure */ 110cb3c7a5dSArchie Cobbs struct ng_ksocket_alias { 111cb3c7a5dSArchie Cobbs const char *name; 112cb3c7a5dSArchie Cobbs const int value; 113cb3c7a5dSArchie Cobbs const int family; 114cb3c7a5dSArchie Cobbs }; 115cb3c7a5dSArchie Cobbs 116cb3c7a5dSArchie Cobbs /* Protocol family aliases */ 117cb3c7a5dSArchie Cobbs static const struct ng_ksocket_alias ng_ksocket_families[] = { 118cb3c7a5dSArchie Cobbs { "local", PF_LOCAL }, 119cb3c7a5dSArchie Cobbs { "inet", PF_INET }, 120cb3c7a5dSArchie Cobbs { "inet6", PF_INET6 }, 121cb3c7a5dSArchie Cobbs { "atalk", PF_APPLETALK }, 122cb3c7a5dSArchie Cobbs { "ipx", PF_IPX }, 123cb3c7a5dSArchie Cobbs { "atm", PF_ATM }, 124cb3c7a5dSArchie Cobbs { NULL, -1 }, 125cb3c7a5dSArchie Cobbs }; 126cb3c7a5dSArchie Cobbs 127cb3c7a5dSArchie Cobbs /* Socket type aliases */ 128cb3c7a5dSArchie Cobbs static const struct ng_ksocket_alias ng_ksocket_types[] = { 129cb3c7a5dSArchie Cobbs { "stream", SOCK_STREAM }, 130cb3c7a5dSArchie Cobbs { "dgram", SOCK_DGRAM }, 131cb3c7a5dSArchie Cobbs { "raw", SOCK_RAW }, 132cb3c7a5dSArchie Cobbs { "rdm", SOCK_RDM }, 133cb3c7a5dSArchie Cobbs { "seqpacket", SOCK_SEQPACKET }, 134cb3c7a5dSArchie Cobbs { NULL, -1 }, 135cb3c7a5dSArchie Cobbs }; 136cb3c7a5dSArchie Cobbs 137cb3c7a5dSArchie Cobbs /* Protocol aliases */ 138cb3c7a5dSArchie Cobbs static const struct ng_ksocket_alias ng_ksocket_protos[] = { 139cb3c7a5dSArchie Cobbs { "ip", IPPROTO_IP, PF_INET }, 140129bc895SArchie Cobbs { "raw", IPPROTO_RAW, PF_INET }, 141cb3c7a5dSArchie Cobbs { "icmp", IPPROTO_ICMP, PF_INET }, 142cb3c7a5dSArchie Cobbs { "igmp", IPPROTO_IGMP, PF_INET }, 143cb3c7a5dSArchie Cobbs { "tcp", IPPROTO_TCP, PF_INET }, 144cb3c7a5dSArchie Cobbs { "udp", IPPROTO_UDP, PF_INET }, 145cb3c7a5dSArchie Cobbs { "gre", IPPROTO_GRE, PF_INET }, 146cb3c7a5dSArchie Cobbs { "esp", IPPROTO_ESP, PF_INET }, 147cb3c7a5dSArchie Cobbs { "ah", IPPROTO_AH, PF_INET }, 148cb3c7a5dSArchie Cobbs { "swipe", IPPROTO_SWIPE, PF_INET }, 149cb3c7a5dSArchie Cobbs { "encap", IPPROTO_ENCAP, PF_INET }, 150cb3c7a5dSArchie Cobbs { "divert", IPPROTO_DIVERT, PF_INET }, 151cb3c7a5dSArchie Cobbs { "ddp", ATPROTO_DDP, PF_APPLETALK }, 152cb3c7a5dSArchie Cobbs { "aarp", ATPROTO_AARP, PF_APPLETALK }, 153cb3c7a5dSArchie Cobbs { NULL, -1 }, 154cb3c7a5dSArchie Cobbs }; 155cb3c7a5dSArchie Cobbs 156f8307e12SArchie Cobbs /* Helper functions */ 157f97e0a07SJulian Elischer static int ng_ksocket_check_accept(priv_p); 158f97e0a07SJulian Elischer static void ng_ksocket_finish_accept(priv_p); 159f8307e12SArchie Cobbs static void ng_ksocket_incoming(struct socket *so, void *arg, int waitflag); 160f8307e12SArchie Cobbs static int ng_ksocket_parse(const struct ng_ksocket_alias *aliases, 161f8307e12SArchie Cobbs const char *s, int family); 162f97e0a07SJulian Elischer static void ng_ksocket_incoming2(node_p node, hook_p hook, 163f97e0a07SJulian Elischer void *arg1, int waitflag); 164f8307e12SArchie Cobbs 165f8307e12SArchie Cobbs /************************************************************************ 166f8307e12SArchie Cobbs STRUCT SOCKADDR PARSE TYPE 167f8307e12SArchie Cobbs ************************************************************************/ 168f8307e12SArchie Cobbs 169f8307e12SArchie Cobbs /* Get the length of the data portion of a generic struct sockaddr */ 170f8307e12SArchie Cobbs static int 171f8307e12SArchie Cobbs ng_parse_generic_sockdata_getLength(const struct ng_parse_type *type, 172f8307e12SArchie Cobbs const u_char *start, const u_char *buf) 173f8307e12SArchie Cobbs { 174f8307e12SArchie Cobbs const struct sockaddr *sa; 175f8307e12SArchie Cobbs 176f8307e12SArchie Cobbs sa = (const struct sockaddr *)(buf - SADATA_OFFSET); 1771baeddb8SArchie Cobbs return (sa->sa_len < SADATA_OFFSET) ? 0 : sa->sa_len - SADATA_OFFSET; 178f8307e12SArchie Cobbs } 179f8307e12SArchie Cobbs 180f8307e12SArchie Cobbs /* Type for the variable length data portion of a generic struct sockaddr */ 181f8307e12SArchie Cobbs static const struct ng_parse_type ng_ksocket_generic_sockdata_type = { 182f8307e12SArchie Cobbs &ng_parse_bytearray_type, 183f8307e12SArchie Cobbs &ng_parse_generic_sockdata_getLength 184f8307e12SArchie Cobbs }; 185f8307e12SArchie Cobbs 186f8307e12SArchie Cobbs /* Type for a generic struct sockaddr */ 187f8307e12SArchie Cobbs static const struct ng_parse_struct_info ng_parse_generic_sockaddr_type_info = { 188f8307e12SArchie Cobbs { 18957b57be3SArchie Cobbs { "len", &ng_parse_uint8_type }, 19057b57be3SArchie Cobbs { "family", &ng_parse_uint8_type }, 191f8307e12SArchie Cobbs { "data", &ng_ksocket_generic_sockdata_type }, 192f8307e12SArchie Cobbs { NULL } 193f8307e12SArchie Cobbs } 194f8307e12SArchie Cobbs }; 195f8307e12SArchie Cobbs static const struct ng_parse_type ng_ksocket_generic_sockaddr_type = { 196f8307e12SArchie Cobbs &ng_parse_struct_type, 197f8307e12SArchie Cobbs &ng_parse_generic_sockaddr_type_info 198f8307e12SArchie Cobbs }; 199f8307e12SArchie Cobbs 200f8307e12SArchie Cobbs /* Convert a struct sockaddr from ASCII to binary. If its a protocol 201f8307e12SArchie Cobbs family that we specially handle, do that, otherwise defer to the 202f8307e12SArchie Cobbs generic parse type ng_ksocket_generic_sockaddr_type. */ 203f8307e12SArchie Cobbs static int 204f8307e12SArchie Cobbs ng_ksocket_sockaddr_parse(const struct ng_parse_type *type, 205f8307e12SArchie Cobbs const char *s, int *off, const u_char *const start, 206f8307e12SArchie Cobbs u_char *const buf, int *buflen) 207f8307e12SArchie Cobbs { 208f8307e12SArchie Cobbs struct sockaddr *const sa = (struct sockaddr *)buf; 209f8307e12SArchie Cobbs enum ng_parse_token tok; 210f8307e12SArchie Cobbs char fambuf[32]; 211f8307e12SArchie Cobbs int family, len; 212f8307e12SArchie Cobbs char *t; 213f8307e12SArchie Cobbs 214f8307e12SArchie Cobbs /* If next token is a left curly brace, use generic parse type */ 215f8307e12SArchie Cobbs if ((tok = ng_parse_get_token(s, off, &len)) == T_LBRACE) { 216f8307e12SArchie Cobbs return (*ng_ksocket_generic_sockaddr_type.supertype->parse) 217f8307e12SArchie Cobbs (&ng_ksocket_generic_sockaddr_type, 218f8307e12SArchie Cobbs s, off, start, buf, buflen); 219f8307e12SArchie Cobbs } 220f8307e12SArchie Cobbs 221f8307e12SArchie Cobbs /* Get socket address family followed by a slash */ 222f8307e12SArchie Cobbs while (isspace(s[*off])) 223f8307e12SArchie Cobbs (*off)++; 224f8307e12SArchie Cobbs if ((t = index(s + *off, '/')) == NULL) 225f8307e12SArchie Cobbs return (EINVAL); 226f8307e12SArchie Cobbs if ((len = t - (s + *off)) > sizeof(fambuf) - 1) 227f8307e12SArchie Cobbs return (EINVAL); 228f8307e12SArchie Cobbs strncpy(fambuf, s + *off, len); 229f8307e12SArchie Cobbs fambuf[len] = '\0'; 230f8307e12SArchie Cobbs *off += len + 1; 231f8307e12SArchie Cobbs if ((family = ng_ksocket_parse(ng_ksocket_families, fambuf, 0)) == -1) 232f8307e12SArchie Cobbs return (EINVAL); 233f8307e12SArchie Cobbs 234f8307e12SArchie Cobbs /* Set family */ 235f8307e12SArchie Cobbs if (*buflen < SADATA_OFFSET) 236f8307e12SArchie Cobbs return (ERANGE); 237f8307e12SArchie Cobbs sa->sa_family = family; 238f8307e12SArchie Cobbs 239f8307e12SArchie Cobbs /* Set family-specific data and length */ 240f8307e12SArchie Cobbs switch (sa->sa_family) { 241f8307e12SArchie Cobbs case PF_LOCAL: /* Get pathname */ 242f8307e12SArchie Cobbs { 243f8307e12SArchie Cobbs const int pathoff = OFFSETOF(struct sockaddr_un, sun_path); 244f8307e12SArchie Cobbs struct sockaddr_un *const sun = (struct sockaddr_un *)sa; 245f8307e12SArchie Cobbs int toklen, pathlen; 246f8307e12SArchie Cobbs char *path; 247f8307e12SArchie Cobbs 24827121ab1SBrian Somers if ((path = ng_get_string_token(s, off, &toklen, NULL)) == NULL) 249f8307e12SArchie Cobbs return (EINVAL); 250f8307e12SArchie Cobbs pathlen = strlen(path); 251f8307e12SArchie Cobbs if (pathlen > SOCK_MAXADDRLEN) { 2529c8c302fSJulian Elischer FREE(path, M_NETGRAPH_KSOCKET); 253f8307e12SArchie Cobbs return (E2BIG); 254f8307e12SArchie Cobbs } 255f8307e12SArchie Cobbs if (*buflen < pathoff + pathlen) { 2569c8c302fSJulian Elischer FREE(path, M_NETGRAPH_KSOCKET); 257f8307e12SArchie Cobbs return (ERANGE); 258f8307e12SArchie Cobbs } 259f8307e12SArchie Cobbs *off += toklen; 260f8307e12SArchie Cobbs bcopy(path, sun->sun_path, pathlen); 261f8307e12SArchie Cobbs sun->sun_len = pathoff + pathlen; 2629c8c302fSJulian Elischer FREE(path, M_NETGRAPH_KSOCKET); 263f8307e12SArchie Cobbs break; 264f8307e12SArchie Cobbs } 265f8307e12SArchie Cobbs 266f8307e12SArchie Cobbs case PF_INET: /* Get an IP address with optional port */ 267f8307e12SArchie Cobbs { 268f8307e12SArchie Cobbs struct sockaddr_in *const sin = (struct sockaddr_in *)sa; 269f8307e12SArchie Cobbs int i; 270f8307e12SArchie Cobbs 271f8307e12SArchie Cobbs /* Parse this: <ipaddress>[:port] */ 272f8307e12SArchie Cobbs for (i = 0; i < 4; i++) { 273f8307e12SArchie Cobbs u_long val; 274f8307e12SArchie Cobbs char *eptr; 275f8307e12SArchie Cobbs 276f8307e12SArchie Cobbs val = strtoul(s + *off, &eptr, 10); 277f8307e12SArchie Cobbs if (val > 0xff || eptr == s + *off) 278f8307e12SArchie Cobbs return (EINVAL); 279f8307e12SArchie Cobbs *off += (eptr - (s + *off)); 280f8307e12SArchie Cobbs ((u_char *)&sin->sin_addr)[i] = (u_char)val; 281f8307e12SArchie Cobbs if (i < 3) { 282f8307e12SArchie Cobbs if (s[*off] != '.') 283f8307e12SArchie Cobbs return (EINVAL); 284f8307e12SArchie Cobbs (*off)++; 285f8307e12SArchie Cobbs } else if (s[*off] == ':') { 286f8307e12SArchie Cobbs (*off)++; 287f8307e12SArchie Cobbs val = strtoul(s + *off, &eptr, 10); 288f8307e12SArchie Cobbs if (val > 0xffff || eptr == s + *off) 289f8307e12SArchie Cobbs return (EINVAL); 290f8307e12SArchie Cobbs *off += (eptr - (s + *off)); 291f8307e12SArchie Cobbs sin->sin_port = htons(val); 292f8307e12SArchie Cobbs } else 293f8307e12SArchie Cobbs sin->sin_port = 0; 294f8307e12SArchie Cobbs } 295f8307e12SArchie Cobbs bzero(&sin->sin_zero, sizeof(sin->sin_zero)); 296f8307e12SArchie Cobbs sin->sin_len = sizeof(*sin); 297f8307e12SArchie Cobbs break; 298f8307e12SArchie Cobbs } 299f8307e12SArchie Cobbs 300f8307e12SArchie Cobbs #if 0 301f8307e12SArchie Cobbs case PF_APPLETALK: /* XXX implement these someday */ 302f8307e12SArchie Cobbs case PF_INET6: 303f8307e12SArchie Cobbs case PF_IPX: 304f8307e12SArchie Cobbs #endif 305f8307e12SArchie Cobbs 306f8307e12SArchie Cobbs default: 307f8307e12SArchie Cobbs return (EINVAL); 308f8307e12SArchie Cobbs } 309f8307e12SArchie Cobbs 310f8307e12SArchie Cobbs /* Done */ 311f8307e12SArchie Cobbs *buflen = sa->sa_len; 312f8307e12SArchie Cobbs return (0); 313f8307e12SArchie Cobbs } 314f8307e12SArchie Cobbs 315f8307e12SArchie Cobbs /* Convert a struct sockaddr from binary to ASCII */ 316f8307e12SArchie Cobbs static int 317f8307e12SArchie Cobbs ng_ksocket_sockaddr_unparse(const struct ng_parse_type *type, 318f8307e12SArchie Cobbs const u_char *data, int *off, char *cbuf, int cbuflen) 319f8307e12SArchie Cobbs { 320f8307e12SArchie Cobbs const struct sockaddr *sa = (const struct sockaddr *)(data + *off); 321f8307e12SArchie Cobbs int slen = 0; 322f8307e12SArchie Cobbs 323f8307e12SArchie Cobbs /* Output socket address, either in special or generic format */ 324f8307e12SArchie Cobbs switch (sa->sa_family) { 325f8307e12SArchie Cobbs case PF_LOCAL: 326f8307e12SArchie Cobbs { 327f8307e12SArchie Cobbs const int pathoff = OFFSETOF(struct sockaddr_un, sun_path); 328f8307e12SArchie Cobbs const struct sockaddr_un *sun = (const struct sockaddr_un *)sa; 329f8307e12SArchie Cobbs const int pathlen = sun->sun_len - pathoff; 330f8307e12SArchie Cobbs char pathbuf[SOCK_MAXADDRLEN + 1]; 331f8307e12SArchie Cobbs char *pathtoken; 332f8307e12SArchie Cobbs 333f8307e12SArchie Cobbs bcopy(sun->sun_path, pathbuf, pathlen); 33427121ab1SBrian Somers if ((pathtoken = ng_encode_string(pathbuf, pathlen)) == NULL) 335f8307e12SArchie Cobbs return (ENOMEM); 336f8307e12SArchie Cobbs slen += snprintf(cbuf, cbuflen, "local/%s", pathtoken); 3379c8c302fSJulian Elischer FREE(pathtoken, M_NETGRAPH_KSOCKET); 338f8307e12SArchie Cobbs if (slen >= cbuflen) 339f8307e12SArchie Cobbs return (ERANGE); 340f8307e12SArchie Cobbs *off += sun->sun_len; 341f8307e12SArchie Cobbs return (0); 342f8307e12SArchie Cobbs } 343f8307e12SArchie Cobbs 344f8307e12SArchie Cobbs case PF_INET: 345f8307e12SArchie Cobbs { 346f8307e12SArchie Cobbs const struct sockaddr_in *sin = (const struct sockaddr_in *)sa; 347f8307e12SArchie Cobbs 348f8307e12SArchie Cobbs slen += snprintf(cbuf, cbuflen, "inet/%d.%d.%d.%d", 349f8307e12SArchie Cobbs ((const u_char *)&sin->sin_addr)[0], 350f8307e12SArchie Cobbs ((const u_char *)&sin->sin_addr)[1], 351f8307e12SArchie Cobbs ((const u_char *)&sin->sin_addr)[2], 352f8307e12SArchie Cobbs ((const u_char *)&sin->sin_addr)[3]); 353f8307e12SArchie Cobbs if (sin->sin_port != 0) { 354f8307e12SArchie Cobbs slen += snprintf(cbuf + strlen(cbuf), 355f8307e12SArchie Cobbs cbuflen - strlen(cbuf), ":%d", 356f8307e12SArchie Cobbs (u_int)ntohs(sin->sin_port)); 357f8307e12SArchie Cobbs } 358f8307e12SArchie Cobbs if (slen >= cbuflen) 359f8307e12SArchie Cobbs return (ERANGE); 360f8307e12SArchie Cobbs *off += sizeof(*sin); 361f8307e12SArchie Cobbs return(0); 362f8307e12SArchie Cobbs } 363f8307e12SArchie Cobbs 364f8307e12SArchie Cobbs #if 0 365f8307e12SArchie Cobbs case PF_APPLETALK: /* XXX implement these someday */ 366f8307e12SArchie Cobbs case PF_INET6: 367f8307e12SArchie Cobbs case PF_IPX: 368f8307e12SArchie Cobbs #endif 369f8307e12SArchie Cobbs 370f8307e12SArchie Cobbs default: 371f8307e12SArchie Cobbs return (*ng_ksocket_generic_sockaddr_type.supertype->unparse) 372f8307e12SArchie Cobbs (&ng_ksocket_generic_sockaddr_type, 373f8307e12SArchie Cobbs data, off, cbuf, cbuflen); 374f8307e12SArchie Cobbs } 375f8307e12SArchie Cobbs } 376f8307e12SArchie Cobbs 377f8307e12SArchie Cobbs /* Parse type for struct sockaddr */ 378f8307e12SArchie Cobbs static const struct ng_parse_type ng_ksocket_sockaddr_type = { 379f8307e12SArchie Cobbs NULL, 380f8307e12SArchie Cobbs NULL, 381f8307e12SArchie Cobbs NULL, 382f8307e12SArchie Cobbs &ng_ksocket_sockaddr_parse, 383f8307e12SArchie Cobbs &ng_ksocket_sockaddr_unparse, 384f8307e12SArchie Cobbs NULL /* no such thing as a default struct sockaddr */ 385f8307e12SArchie Cobbs }; 386f8307e12SArchie Cobbs 387f8307e12SArchie Cobbs /************************************************************************ 388f8307e12SArchie Cobbs STRUCT NG_KSOCKET_SOCKOPT PARSE TYPE 389f8307e12SArchie Cobbs ************************************************************************/ 390f8307e12SArchie Cobbs 391f8307e12SArchie Cobbs /* Get length of the struct ng_ksocket_sockopt value field, which is the 392f8307e12SArchie Cobbs just the excess of the message argument portion over the length of 393f8307e12SArchie Cobbs the struct ng_ksocket_sockopt. */ 394f8307e12SArchie Cobbs static int 395f8307e12SArchie Cobbs ng_parse_sockoptval_getLength(const struct ng_parse_type *type, 396f8307e12SArchie Cobbs const u_char *start, const u_char *buf) 397f8307e12SArchie Cobbs { 398f8307e12SArchie Cobbs static const int offset = OFFSETOF(struct ng_ksocket_sockopt, value); 399f8307e12SArchie Cobbs const struct ng_ksocket_sockopt *sopt; 400f8307e12SArchie Cobbs const struct ng_mesg *msg; 401f8307e12SArchie Cobbs 402f8307e12SArchie Cobbs sopt = (const struct ng_ksocket_sockopt *)(buf - offset); 403f8307e12SArchie Cobbs msg = (const struct ng_mesg *)((const u_char *)sopt - sizeof(*msg)); 404f8307e12SArchie Cobbs return msg->header.arglen - sizeof(*sopt); 405f8307e12SArchie Cobbs } 406f8307e12SArchie Cobbs 407f8307e12SArchie Cobbs /* Parse type for the option value part of a struct ng_ksocket_sockopt 408f8307e12SArchie Cobbs XXX Eventually, we should handle the different socket options specially. 409f8307e12SArchie Cobbs XXX This would avoid byte order problems, eg an integer value of 1 is 410f8307e12SArchie Cobbs XXX going to be "[1]" for little endian or "[3=1]" for big endian. */ 411f8307e12SArchie Cobbs static const struct ng_parse_type ng_ksocket_sockoptval_type = { 412f8307e12SArchie Cobbs &ng_parse_bytearray_type, 413f8307e12SArchie Cobbs &ng_parse_sockoptval_getLength 414f8307e12SArchie Cobbs }; 415f8307e12SArchie Cobbs 416f8307e12SArchie Cobbs /* Parse type for struct ng_ksocket_sockopt */ 417f8307e12SArchie Cobbs static const struct ng_parse_struct_info ng_ksocket_sockopt_type_info 418f8307e12SArchie Cobbs = NG_KSOCKET_SOCKOPT_INFO(&ng_ksocket_sockoptval_type); 419f8307e12SArchie Cobbs static const struct ng_parse_type ng_ksocket_sockopt_type = { 420f8307e12SArchie Cobbs &ng_parse_struct_type, 421f8307e12SArchie Cobbs &ng_ksocket_sockopt_type_info, 422f8307e12SArchie Cobbs }; 423f8307e12SArchie Cobbs 424f97e0a07SJulian Elischer /* Parse type for struct ng_ksocket_accept */ 425f97e0a07SJulian Elischer static const struct ng_parse_struct_info ng_ksocket_accept_type_info 426f97e0a07SJulian Elischer = NGM_KSOCKET_ACCEPT_INFO; 427f97e0a07SJulian Elischer static const struct ng_parse_type ng_ksocket_accept_type = { 428f97e0a07SJulian Elischer &ng_parse_struct_type, 429f97e0a07SJulian Elischer &ng_ksocket_accept_type_info 430f97e0a07SJulian Elischer }; 431f97e0a07SJulian Elischer 432f8307e12SArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */ 433f8307e12SArchie Cobbs static const struct ng_cmdlist ng_ksocket_cmds[] = { 434f8307e12SArchie Cobbs { 435f8307e12SArchie Cobbs NGM_KSOCKET_COOKIE, 436f8307e12SArchie Cobbs NGM_KSOCKET_BIND, 437f8307e12SArchie Cobbs "bind", 438f8307e12SArchie Cobbs &ng_ksocket_sockaddr_type, 439f8307e12SArchie Cobbs NULL 440f8307e12SArchie Cobbs }, 441f8307e12SArchie Cobbs { 442f8307e12SArchie Cobbs NGM_KSOCKET_COOKIE, 443f8307e12SArchie Cobbs NGM_KSOCKET_LISTEN, 444f8307e12SArchie Cobbs "listen", 445f8307e12SArchie Cobbs &ng_parse_int32_type, 446f8307e12SArchie Cobbs NULL 447f8307e12SArchie Cobbs }, 448f8307e12SArchie Cobbs { 449f8307e12SArchie Cobbs NGM_KSOCKET_COOKIE, 450f8307e12SArchie Cobbs NGM_KSOCKET_ACCEPT, 451f8307e12SArchie Cobbs "accept", 452f8307e12SArchie Cobbs NULL, 453f97e0a07SJulian Elischer &ng_ksocket_accept_type 454f8307e12SArchie Cobbs }, 455f8307e12SArchie Cobbs { 456f8307e12SArchie Cobbs NGM_KSOCKET_COOKIE, 457f8307e12SArchie Cobbs NGM_KSOCKET_CONNECT, 458f8307e12SArchie Cobbs "connect", 459f8307e12SArchie Cobbs &ng_ksocket_sockaddr_type, 460f97e0a07SJulian Elischer &ng_parse_int32_type 461f8307e12SArchie Cobbs }, 462f8307e12SArchie Cobbs { 463f8307e12SArchie Cobbs NGM_KSOCKET_COOKIE, 464f8307e12SArchie Cobbs NGM_KSOCKET_GETNAME, 465f8307e12SArchie Cobbs "getname", 466f8307e12SArchie Cobbs NULL, 467f8307e12SArchie Cobbs &ng_ksocket_sockaddr_type 468f8307e12SArchie Cobbs }, 469f8307e12SArchie Cobbs { 470f8307e12SArchie Cobbs NGM_KSOCKET_COOKIE, 471f8307e12SArchie Cobbs NGM_KSOCKET_GETPEERNAME, 472f8307e12SArchie Cobbs "getpeername", 473f8307e12SArchie Cobbs NULL, 474f8307e12SArchie Cobbs &ng_ksocket_sockaddr_type 475f8307e12SArchie Cobbs }, 476f8307e12SArchie Cobbs { 477f8307e12SArchie Cobbs NGM_KSOCKET_COOKIE, 478f8307e12SArchie Cobbs NGM_KSOCKET_SETOPT, 479f8307e12SArchie Cobbs "setopt", 480f8307e12SArchie Cobbs &ng_ksocket_sockopt_type, 481f8307e12SArchie Cobbs NULL 482f8307e12SArchie Cobbs }, 483f8307e12SArchie Cobbs { 484f8307e12SArchie Cobbs NGM_KSOCKET_COOKIE, 485f8307e12SArchie Cobbs NGM_KSOCKET_GETOPT, 486f8307e12SArchie Cobbs "getopt", 487f8307e12SArchie Cobbs &ng_ksocket_sockopt_type, 488f8307e12SArchie Cobbs &ng_ksocket_sockopt_type 489f8307e12SArchie Cobbs }, 490f8307e12SArchie Cobbs { 0 } 491f8307e12SArchie Cobbs }; 492f8307e12SArchie Cobbs 493f8307e12SArchie Cobbs /* Node type descriptor */ 494f8307e12SArchie Cobbs static struct ng_type ng_ksocket_typestruct = { 495589f6ed8SJulian Elischer NG_ABI_VERSION, 496f8307e12SArchie Cobbs NG_KSOCKET_NODE_TYPE, 497f8307e12SArchie Cobbs NULL, 498f8307e12SArchie Cobbs ng_ksocket_constructor, 499f8307e12SArchie Cobbs ng_ksocket_rcvmsg, 500069154d5SJulian Elischer ng_ksocket_shutdown, 501f8307e12SArchie Cobbs ng_ksocket_newhook, 502f8307e12SArchie Cobbs NULL, 503f97e0a07SJulian Elischer ng_ksocket_connect, 504f8307e12SArchie Cobbs ng_ksocket_rcvdata, 505f8307e12SArchie Cobbs ng_ksocket_disconnect, 506f8307e12SArchie Cobbs ng_ksocket_cmds 507f8307e12SArchie Cobbs }; 508f8307e12SArchie Cobbs NETGRAPH_INIT(ksocket, &ng_ksocket_typestruct); 509f8307e12SArchie Cobbs 5104cc20ab1SSeigo Tanimura #define ERROUT(x) do { error = (x); goto done; } while (0) 511cb3c7a5dSArchie Cobbs 512cb3c7a5dSArchie Cobbs /************************************************************************ 513cb3c7a5dSArchie Cobbs NETGRAPH NODE STUFF 514cb3c7a5dSArchie Cobbs ************************************************************************/ 515cb3c7a5dSArchie Cobbs 516cb3c7a5dSArchie Cobbs /* 517cb3c7a5dSArchie Cobbs * Node type constructor 518f97e0a07SJulian Elischer * The NODE part is assumed to be all set up. 519f97e0a07SJulian Elischer * There is already a reference to the node for us. 520cb3c7a5dSArchie Cobbs */ 521cb3c7a5dSArchie Cobbs static int 522069154d5SJulian Elischer ng_ksocket_constructor(node_p node) 523cb3c7a5dSArchie Cobbs { 524cb3c7a5dSArchie Cobbs priv_p priv; 525cb3c7a5dSArchie Cobbs 526cb3c7a5dSArchie Cobbs /* Allocate private structure */ 52793caaaa7SArchie Cobbs MALLOC(priv, priv_p, sizeof(*priv), 52893caaaa7SArchie Cobbs M_NETGRAPH_KSOCKET, M_NOWAIT | M_ZERO); 529cb3c7a5dSArchie Cobbs if (priv == NULL) 530cb3c7a5dSArchie Cobbs return (ENOMEM); 531cb3c7a5dSArchie Cobbs 532f97e0a07SJulian Elischer LIST_INIT(&priv->embryos); 533f97e0a07SJulian Elischer /* cross link them */ 534f97e0a07SJulian Elischer priv->node = node; 53530400f03SJulian Elischer NG_NODE_SET_PRIVATE(node, priv); 536cb3c7a5dSArchie Cobbs 537cb3c7a5dSArchie Cobbs /* Done */ 538cb3c7a5dSArchie Cobbs return (0); 539cb3c7a5dSArchie Cobbs } 540cb3c7a5dSArchie Cobbs 541cb3c7a5dSArchie Cobbs /* 542cb3c7a5dSArchie Cobbs * Give our OK for a hook to be added. The hook name is of the 54393caaaa7SArchie Cobbs * form "<family>/<type>/<proto>" where the three components may 544cb3c7a5dSArchie Cobbs * be decimal numbers or else aliases from the above lists. 545cb3c7a5dSArchie Cobbs * 546cb3c7a5dSArchie Cobbs * Connecting a hook amounts to opening the socket. Disconnecting 547cb3c7a5dSArchie Cobbs * the hook closes the socket and destroys the node as well. 548cb3c7a5dSArchie Cobbs */ 549cb3c7a5dSArchie Cobbs static int 550cb3c7a5dSArchie Cobbs ng_ksocket_newhook(node_p node, hook_p hook, const char *name0) 551cb3c7a5dSArchie Cobbs { 552079b7badSJulian Elischer struct thread *td = curthread ? curthread : &thread0; /* XXX broken */ 55330400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 554cb3c7a5dSArchie Cobbs char *s1, *s2, name[NG_HOOKLEN+1]; 555cb3c7a5dSArchie Cobbs int family, type, protocol, error; 556cb3c7a5dSArchie Cobbs 557cb3c7a5dSArchie Cobbs /* Check if we're already connected */ 558cb3c7a5dSArchie Cobbs if (priv->hook != NULL) 559cb3c7a5dSArchie Cobbs return (EISCONN); 560cb3c7a5dSArchie Cobbs 561f97e0a07SJulian Elischer if (priv->flags & KSF_CLONED) { 562f97e0a07SJulian Elischer if (priv->flags & KSF_EMBRYONIC) { 563f97e0a07SJulian Elischer /* Remove ourselves from our parent's embryo list */ 564f97e0a07SJulian Elischer LIST_REMOVE(priv, siblings); 565f97e0a07SJulian Elischer priv->flags &= ~KSF_EMBRYONIC; 566f97e0a07SJulian Elischer } 567f97e0a07SJulian Elischer } else { 568cb3c7a5dSArchie Cobbs /* Extract family, type, and protocol from hook name */ 569cb3c7a5dSArchie Cobbs snprintf(name, sizeof(name), "%s", name0); 570cb3c7a5dSArchie Cobbs s1 = name; 571cb3c7a5dSArchie Cobbs if ((s2 = index(s1, '/')) == NULL) 572cb3c7a5dSArchie Cobbs return (EINVAL); 573cb3c7a5dSArchie Cobbs *s2++ = '\0'; 574f97e0a07SJulian Elischer family = ng_ksocket_parse(ng_ksocket_families, s1, 0); 575f97e0a07SJulian Elischer if (family == -1) 576cb3c7a5dSArchie Cobbs return (EINVAL); 577cb3c7a5dSArchie Cobbs s1 = s2; 578cb3c7a5dSArchie Cobbs if ((s2 = index(s1, '/')) == NULL) 579cb3c7a5dSArchie Cobbs return (EINVAL); 580cb3c7a5dSArchie Cobbs *s2++ = '\0'; 581f97e0a07SJulian Elischer type = ng_ksocket_parse(ng_ksocket_types, s1, 0); 582f97e0a07SJulian Elischer if (type == -1) 583cb3c7a5dSArchie Cobbs return (EINVAL); 584cb3c7a5dSArchie Cobbs s1 = s2; 585f97e0a07SJulian Elischer protocol = ng_ksocket_parse(ng_ksocket_protos, s1, family); 586f97e0a07SJulian Elischer if (protocol == -1) 587cb3c7a5dSArchie Cobbs return (EINVAL); 588cb3c7a5dSArchie Cobbs 589cb3c7a5dSArchie Cobbs /* Create the socket */ 5909c4d63daSRobert Watson error = socreate(family, &priv->so, type, protocol, 591a854ed98SJohn Baldwin td->td_ucred, td); 592f97e0a07SJulian Elischer if (error != 0) 593cb3c7a5dSArchie Cobbs return (error); 594cb3c7a5dSArchie Cobbs 595cb3c7a5dSArchie Cobbs /* XXX call soreserve() ? */ 596cb3c7a5dSArchie Cobbs 597f97e0a07SJulian Elischer } 598cb3c7a5dSArchie Cobbs 599cb3c7a5dSArchie Cobbs /* OK */ 600cb3c7a5dSArchie Cobbs priv->hook = hook; 601cb3c7a5dSArchie Cobbs return(0); 602cb3c7a5dSArchie Cobbs } 603cb3c7a5dSArchie Cobbs 604f97e0a07SJulian Elischer static int 605f97e0a07SJulian Elischer ng_ksocket_connect(hook_p hook) 606f97e0a07SJulian Elischer { 607f97e0a07SJulian Elischer node_p node = NG_HOOK_NODE(hook); 608f97e0a07SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 609f97e0a07SJulian Elischer struct socket *const so = priv->so; 610f97e0a07SJulian Elischer 611f97e0a07SJulian Elischer /* Add our hook for incoming data and other events */ 612f97e0a07SJulian Elischer priv->so->so_upcallarg = (caddr_t)node; 613f97e0a07SJulian Elischer priv->so->so_upcall = ng_ksocket_incoming; 614f97e0a07SJulian Elischer priv->so->so_rcv.sb_flags |= SB_UPCALL; 615f97e0a07SJulian Elischer priv->so->so_snd.sb_flags |= SB_UPCALL; 616f97e0a07SJulian Elischer priv->so->so_state |= SS_NBIO; 617f97e0a07SJulian Elischer /* 618f97e0a07SJulian Elischer * --Original comment-- 619f97e0a07SJulian Elischer * On a cloned socket we may have already received one or more 620f97e0a07SJulian Elischer * upcalls which we couldn't handle without a hook. Handle 621f97e0a07SJulian Elischer * those now. 622f97e0a07SJulian Elischer * We cannot call the upcall function directly 623f97e0a07SJulian Elischer * from here, because until this function has returned our 624f97e0a07SJulian Elischer * hook isn't connected. 625f97e0a07SJulian Elischer * 626f97e0a07SJulian Elischer * ---meta comment for -current --- 627f97e0a07SJulian Elischer * XXX This is dubius. 628f97e0a07SJulian Elischer * Upcalls between the time that the hook was 629f97e0a07SJulian Elischer * first created and now (on another processesor) will 630f97e0a07SJulian Elischer * be earlier on the queue than the request to finalise the hook. 631f97e0a07SJulian Elischer * By the time the hook is finalised, 632f97e0a07SJulian Elischer * The queued upcalls will have happenned and the code 633f97e0a07SJulian Elischer * will have discarded them because of a lack of a hook. 634f97e0a07SJulian Elischer * (socket not open). 635f97e0a07SJulian Elischer * 636f97e0a07SJulian Elischer * This is a bad byproduct of the complicated way in which hooks 637f97e0a07SJulian Elischer * are now created (3 daisy chained async events). 638f97e0a07SJulian Elischer * 639f97e0a07SJulian Elischer * Since we are a netgraph operation 640f97e0a07SJulian Elischer * We know that we hold a lock on this node. This forces the 641f97e0a07SJulian Elischer * request we make below to be queued rather than implemented 642f97e0a07SJulian Elischer * immediatly which will cause the upcall function to be called a bit 643f97e0a07SJulian Elischer * later. 644f97e0a07SJulian Elischer * However, as we will run any waiting queued operations immediatly 645f97e0a07SJulian Elischer * after doing this one, if we have not finalised the other end 646f97e0a07SJulian Elischer * of the hook, those queued operations will fail. 647f97e0a07SJulian Elischer */ 648f97e0a07SJulian Elischer if (priv->flags & KSF_CLONED) { 649f97e0a07SJulian Elischer ng_send_fn(node, NULL, &ng_ksocket_incoming2, so, M_NOWAIT); 650f97e0a07SJulian Elischer } 651f97e0a07SJulian Elischer 652f97e0a07SJulian Elischer return (0); 653f97e0a07SJulian Elischer } 654f97e0a07SJulian Elischer 655cb3c7a5dSArchie Cobbs /* 656cb3c7a5dSArchie Cobbs * Receive a control message 657cb3c7a5dSArchie Cobbs */ 658cb3c7a5dSArchie Cobbs static int 659069154d5SJulian Elischer ng_ksocket_rcvmsg(node_p node, item_p item, hook_p lasthook) 660cb3c7a5dSArchie Cobbs { 661079b7badSJulian Elischer struct thread *td = curthread ? curthread : &thread0; /* XXX broken */ 66230400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 663f8307e12SArchie Cobbs struct socket *const so = priv->so; 664cb3c7a5dSArchie Cobbs struct ng_mesg *resp = NULL; 665cb3c7a5dSArchie Cobbs int error = 0; 666069154d5SJulian Elischer struct ng_mesg *msg; 667f97e0a07SJulian Elischer ng_ID_t raddr; 668cb3c7a5dSArchie Cobbs 669069154d5SJulian Elischer NGI_GET_MSG(item, msg); 670cb3c7a5dSArchie Cobbs switch (msg->header.typecookie) { 671cb3c7a5dSArchie Cobbs case NGM_KSOCKET_COOKIE: 672cb3c7a5dSArchie Cobbs switch (msg->header.cmd) { 673cb3c7a5dSArchie Cobbs case NGM_KSOCKET_BIND: 674cb3c7a5dSArchie Cobbs { 675f8307e12SArchie Cobbs struct sockaddr *const sa 676f8307e12SArchie Cobbs = (struct sockaddr *)msg->data; 677cb3c7a5dSArchie Cobbs 678f8307e12SArchie Cobbs /* Sanity check */ 679f8307e12SArchie Cobbs if (msg->header.arglen < SADATA_OFFSET 680f8307e12SArchie Cobbs || msg->header.arglen < sa->sa_len) 681f8307e12SArchie Cobbs ERROUT(EINVAL); 682f8307e12SArchie Cobbs if (so == NULL) 683f8307e12SArchie Cobbs ERROUT(ENXIO); 684cb3c7a5dSArchie Cobbs 685f8307e12SArchie Cobbs /* Bind */ 686b40ce416SJulian Elischer error = sobind(so, sa, td); 687cb3c7a5dSArchie Cobbs break; 688cb3c7a5dSArchie Cobbs } 689cb3c7a5dSArchie Cobbs case NGM_KSOCKET_LISTEN: 690cb3c7a5dSArchie Cobbs { 691f8307e12SArchie Cobbs /* Sanity check */ 692f97e0a07SJulian Elischer if (msg->header.arglen != sizeof(int32_t)) 693cb3c7a5dSArchie Cobbs ERROUT(EINVAL); 694f8307e12SArchie Cobbs if (so == NULL) 695f8307e12SArchie Cobbs ERROUT(ENXIO); 696cb3c7a5dSArchie Cobbs 697f8307e12SArchie Cobbs /* Listen */ 698b40ce416SJulian Elischer error = solisten(so, *((int32_t *)msg->data), td); 699cb3c7a5dSArchie Cobbs break; 700cb3c7a5dSArchie Cobbs } 701cb3c7a5dSArchie Cobbs 702cb3c7a5dSArchie Cobbs case NGM_KSOCKET_ACCEPT: 703cb3c7a5dSArchie Cobbs { 704f8307e12SArchie Cobbs /* Sanity check */ 705f8307e12SArchie Cobbs if (msg->header.arglen != 0) 706f8307e12SArchie Cobbs ERROUT(EINVAL); 707f8307e12SArchie Cobbs if (so == NULL) 708f8307e12SArchie Cobbs ERROUT(ENXIO); 709f8307e12SArchie Cobbs 710f97e0a07SJulian Elischer /* Make sure the socket is capable of accepting */ 711f97e0a07SJulian Elischer if (!(so->so_options & SO_ACCEPTCONN)) 7124cc20ab1SSeigo Tanimura ERROUT(EINVAL); 713f97e0a07SJulian Elischer if (priv->flags & KSF_ACCEPTING) 714f97e0a07SJulian Elischer ERROUT(EALREADY); 715f8307e12SArchie Cobbs 716f97e0a07SJulian Elischer error = ng_ksocket_check_accept(priv); 717f97e0a07SJulian Elischer if (error != 0 && error != EWOULDBLOCK) 718f97e0a07SJulian Elischer ERROUT(error); 719f8307e12SArchie Cobbs 720f97e0a07SJulian Elischer /* 721f97e0a07SJulian Elischer * If a connection is already complete, take it. 722f97e0a07SJulian Elischer * Otherwise let the upcall function deal with 723f97e0a07SJulian Elischer * the connection when it comes in. 724f97e0a07SJulian Elischer */ 725f97e0a07SJulian Elischer priv->response_token = msg->header.token; 726f97e0a07SJulian Elischer raddr = priv->response_addr; 727f97e0a07SJulian Elischer if (error == 0) { 728f97e0a07SJulian Elischer ng_ksocket_finish_accept(priv); 729f97e0a07SJulian Elischer } else 730f97e0a07SJulian Elischer priv->flags |= KSF_ACCEPTING; 731cb3c7a5dSArchie Cobbs break; 732cb3c7a5dSArchie Cobbs } 733cb3c7a5dSArchie Cobbs 734cb3c7a5dSArchie Cobbs case NGM_KSOCKET_CONNECT: 735cb3c7a5dSArchie Cobbs { 736f8307e12SArchie Cobbs struct sockaddr *const sa 737f8307e12SArchie Cobbs = (struct sockaddr *)msg->data; 738cb3c7a5dSArchie Cobbs 739f8307e12SArchie Cobbs /* Sanity check */ 740f8307e12SArchie Cobbs if (msg->header.arglen < SADATA_OFFSET 741f8307e12SArchie Cobbs || msg->header.arglen < sa->sa_len) 742f8307e12SArchie Cobbs ERROUT(EINVAL); 743f8307e12SArchie Cobbs if (so == NULL) 744f8307e12SArchie Cobbs ERROUT(ENXIO); 745cb3c7a5dSArchie Cobbs 746cb3c7a5dSArchie Cobbs /* Do connect */ 747cb3c7a5dSArchie Cobbs if ((so->so_state & SS_ISCONNECTING) != 0) 7484cc20ab1SSeigo Tanimura ERROUT(EALREADY); 749b40ce416SJulian Elischer if ((error = soconnect(so, sa, td)) != 0) { 750cb3c7a5dSArchie Cobbs so->so_state &= ~SS_ISCONNECTING; 7514cc20ab1SSeigo Tanimura ERROUT(error); 752cb3c7a5dSArchie Cobbs } 7534cc20ab1SSeigo Tanimura if ((so->so_state & SS_ISCONNECTING) != 0) 754f97e0a07SJulian Elischer /* We will notify the sender when we connect */ 755f97e0a07SJulian Elischer priv->response_token = msg->header.token; 756f97e0a07SJulian Elischer raddr = priv->response_addr; 757f97e0a07SJulian Elischer priv->flags |= KSF_CONNECTING; 7584cc20ab1SSeigo Tanimura ERROUT(EINPROGRESS); 759cb3c7a5dSArchie Cobbs break; 760cb3c7a5dSArchie Cobbs } 761cb3c7a5dSArchie Cobbs 762cb3c7a5dSArchie Cobbs case NGM_KSOCKET_GETNAME: 763cb3c7a5dSArchie Cobbs case NGM_KSOCKET_GETPEERNAME: 764cb3c7a5dSArchie Cobbs { 765f8307e12SArchie Cobbs int (*func)(struct socket *so, struct sockaddr **nam); 766f8307e12SArchie Cobbs struct sockaddr *sa = NULL; 767f8307e12SArchie Cobbs int len; 768f8307e12SArchie Cobbs 769f8307e12SArchie Cobbs /* Sanity check */ 770f8307e12SArchie Cobbs if (msg->header.arglen != 0) 771f8307e12SArchie Cobbs ERROUT(EINVAL); 772f8307e12SArchie Cobbs if (so == NULL) 773f8307e12SArchie Cobbs ERROUT(ENXIO); 774f8307e12SArchie Cobbs 775f8307e12SArchie Cobbs /* Get function */ 776f8307e12SArchie Cobbs if (msg->header.cmd == NGM_KSOCKET_GETPEERNAME) { 777f8307e12SArchie Cobbs if ((so->so_state 778f8307e12SArchie Cobbs & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) 7794cc20ab1SSeigo Tanimura ERROUT(ENOTCONN); 780f8307e12SArchie Cobbs func = so->so_proto->pr_usrreqs->pru_peeraddr; 781f8307e12SArchie Cobbs } else 782f8307e12SArchie Cobbs func = so->so_proto->pr_usrreqs->pru_sockaddr; 783f8307e12SArchie Cobbs 784f8307e12SArchie Cobbs /* Get local or peer address */ 785f8307e12SArchie Cobbs if ((error = (*func)(so, &sa)) != 0) 786f8307e12SArchie Cobbs goto bail; 787f8307e12SArchie Cobbs len = (sa == NULL) ? 0 : sa->sa_len; 788f8307e12SArchie Cobbs 789f8307e12SArchie Cobbs /* Send it back in a response */ 790f8307e12SArchie Cobbs NG_MKRESPONSE(resp, msg, len, M_NOWAIT); 791f8307e12SArchie Cobbs if (resp == NULL) { 792f8307e12SArchie Cobbs error = ENOMEM; 793f8307e12SArchie Cobbs goto bail; 794f8307e12SArchie Cobbs } 795f8307e12SArchie Cobbs bcopy(sa, resp->data, len); 796f8307e12SArchie Cobbs 797f8307e12SArchie Cobbs bail: 798f8307e12SArchie Cobbs /* Cleanup */ 799f8307e12SArchie Cobbs if (sa != NULL) 800f8307e12SArchie Cobbs FREE(sa, M_SONAME); 801cb3c7a5dSArchie Cobbs break; 802cb3c7a5dSArchie Cobbs } 803cb3c7a5dSArchie Cobbs 804cb3c7a5dSArchie Cobbs case NGM_KSOCKET_GETOPT: 805cb3c7a5dSArchie Cobbs { 806f8307e12SArchie Cobbs struct ng_ksocket_sockopt *ksopt = 807f8307e12SArchie Cobbs (struct ng_ksocket_sockopt *)msg->data; 808f8307e12SArchie Cobbs struct sockopt sopt; 809f8307e12SArchie Cobbs 810f8307e12SArchie Cobbs /* Sanity check */ 811f8307e12SArchie Cobbs if (msg->header.arglen != sizeof(*ksopt)) 812f8307e12SArchie Cobbs ERROUT(EINVAL); 813f8307e12SArchie Cobbs if (so == NULL) 814f8307e12SArchie Cobbs ERROUT(ENXIO); 815f8307e12SArchie Cobbs 816f8307e12SArchie Cobbs /* Get response with room for option value */ 817f8307e12SArchie Cobbs NG_MKRESPONSE(resp, msg, sizeof(*ksopt) 818f8307e12SArchie Cobbs + NG_KSOCKET_MAX_OPTLEN, M_NOWAIT); 819f8307e12SArchie Cobbs if (resp == NULL) 820f8307e12SArchie Cobbs ERROUT(ENOMEM); 821f8307e12SArchie Cobbs 822f8307e12SArchie Cobbs /* Get socket option, and put value in the response */ 823f8307e12SArchie Cobbs sopt.sopt_dir = SOPT_GET; 824f8307e12SArchie Cobbs sopt.sopt_level = ksopt->level; 825f8307e12SArchie Cobbs sopt.sopt_name = ksopt->name; 826b40ce416SJulian Elischer sopt.sopt_td = NULL; 827f8307e12SArchie Cobbs sopt.sopt_valsize = NG_KSOCKET_MAX_OPTLEN; 828f8307e12SArchie Cobbs ksopt = (struct ng_ksocket_sockopt *)resp->data; 829f8307e12SArchie Cobbs sopt.sopt_val = ksopt->value; 830f8307e12SArchie Cobbs if ((error = sogetopt(so, &sopt)) != 0) { 831069154d5SJulian Elischer NG_FREE_MSG(resp); 832f8307e12SArchie Cobbs break; 833f8307e12SArchie Cobbs } 834f8307e12SArchie Cobbs 835f8307e12SArchie Cobbs /* Set actual value length */ 836f8307e12SArchie Cobbs resp->header.arglen = sizeof(*ksopt) 837f8307e12SArchie Cobbs + sopt.sopt_valsize; 838cb3c7a5dSArchie Cobbs break; 839cb3c7a5dSArchie Cobbs } 840cb3c7a5dSArchie Cobbs 841cb3c7a5dSArchie Cobbs case NGM_KSOCKET_SETOPT: 842cb3c7a5dSArchie Cobbs { 843f8307e12SArchie Cobbs struct ng_ksocket_sockopt *const ksopt = 844f8307e12SArchie Cobbs (struct ng_ksocket_sockopt *)msg->data; 845f8307e12SArchie Cobbs const int valsize = msg->header.arglen - sizeof(*ksopt); 846f8307e12SArchie Cobbs struct sockopt sopt; 847f8307e12SArchie Cobbs 848f8307e12SArchie Cobbs /* Sanity check */ 849f8307e12SArchie Cobbs if (valsize < 0) 850f8307e12SArchie Cobbs ERROUT(EINVAL); 851f8307e12SArchie Cobbs if (so == NULL) 852f8307e12SArchie Cobbs ERROUT(ENXIO); 853f8307e12SArchie Cobbs 854f8307e12SArchie Cobbs /* Set socket option */ 855f8307e12SArchie Cobbs sopt.sopt_dir = SOPT_SET; 856f8307e12SArchie Cobbs sopt.sopt_level = ksopt->level; 857f8307e12SArchie Cobbs sopt.sopt_name = ksopt->name; 858f8307e12SArchie Cobbs sopt.sopt_val = ksopt->value; 859f8307e12SArchie Cobbs sopt.sopt_valsize = valsize; 860b40ce416SJulian Elischer sopt.sopt_td = NULL; 861f8307e12SArchie Cobbs error = sosetopt(so, &sopt); 862cb3c7a5dSArchie Cobbs break; 863cb3c7a5dSArchie Cobbs } 864cb3c7a5dSArchie Cobbs 865cb3c7a5dSArchie Cobbs default: 866cb3c7a5dSArchie Cobbs error = EINVAL; 867cb3c7a5dSArchie Cobbs break; 868cb3c7a5dSArchie Cobbs } 869cb3c7a5dSArchie Cobbs break; 870cb3c7a5dSArchie Cobbs default: 871cb3c7a5dSArchie Cobbs error = EINVAL; 872cb3c7a5dSArchie Cobbs break; 873cb3c7a5dSArchie Cobbs } 874cb3c7a5dSArchie Cobbs done: 875069154d5SJulian Elischer NG_RESPOND_MSG(error, node, item, resp); 876069154d5SJulian Elischer NG_FREE_MSG(msg); 877cb3c7a5dSArchie Cobbs return (error); 878cb3c7a5dSArchie Cobbs } 879cb3c7a5dSArchie Cobbs 880cb3c7a5dSArchie Cobbs /* 881cb3c7a5dSArchie Cobbs * Receive incoming data on our hook. Send it out the socket. 882cb3c7a5dSArchie Cobbs */ 883cb3c7a5dSArchie Cobbs static int 884069154d5SJulian Elischer ng_ksocket_rcvdata(hook_p hook, item_p item) 885cb3c7a5dSArchie Cobbs { 886079b7badSJulian Elischer struct thread *td = curthread ? curthread : &thread0; /* XXX broken */ 88730400f03SJulian Elischer const node_p node = NG_HOOK_NODE(hook); 88830400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 889cb3c7a5dSArchie Cobbs struct socket *const so = priv->so; 89019ff9e5fSArchie Cobbs struct sockaddr *sa = NULL; 89119ff9e5fSArchie Cobbs meta_p meta; 892cb3c7a5dSArchie Cobbs int error; 893069154d5SJulian Elischer struct mbuf *m; 894cb3c7a5dSArchie Cobbs 895dc9c2e01SArchie Cobbs /* Avoid reentrantly sending on the socket */ 896dc9c2e01SArchie Cobbs if ((priv->flags & KSF_SENDING) != 0) { 897dc9c2e01SArchie Cobbs NG_FREE_ITEM(item); 898dc9c2e01SArchie Cobbs return (EDEADLK); 899dc9c2e01SArchie Cobbs } 900dc9c2e01SArchie Cobbs 90119ff9e5fSArchie Cobbs /* Extract data and meta information */ 902069154d5SJulian Elischer NGI_GET_M(item, m); 90319ff9e5fSArchie Cobbs NGI_GET_META(item, meta); 904069154d5SJulian Elischer NG_FREE_ITEM(item); 90519ff9e5fSArchie Cobbs 90619ff9e5fSArchie Cobbs /* If any meta info, look for peer socket address */ 90719ff9e5fSArchie Cobbs if (meta != NULL) { 90819ff9e5fSArchie Cobbs struct meta_field_header *field; 90919ff9e5fSArchie Cobbs 91019ff9e5fSArchie Cobbs /* Look for peer socket address */ 91119ff9e5fSArchie Cobbs for (field = &meta->options[0]; 91219ff9e5fSArchie Cobbs (caddr_t)field < (caddr_t)meta + meta->used_len; 91319ff9e5fSArchie Cobbs field = (struct meta_field_header *) 91419ff9e5fSArchie Cobbs ((caddr_t)field + field->len)) { 91519ff9e5fSArchie Cobbs if (field->cookie != NGM_KSOCKET_COOKIE 91619ff9e5fSArchie Cobbs || field->type != NG_KSOCKET_META_SOCKADDR) 91719ff9e5fSArchie Cobbs continue; 91819ff9e5fSArchie Cobbs sa = (struct sockaddr *)field->data; 91919ff9e5fSArchie Cobbs break; 92019ff9e5fSArchie Cobbs } 92119ff9e5fSArchie Cobbs } 92219ff9e5fSArchie Cobbs 92319ff9e5fSArchie Cobbs /* Send packet */ 924dc9c2e01SArchie Cobbs priv->flags |= KSF_SENDING; 92519ff9e5fSArchie Cobbs error = (*so->so_proto->pr_usrreqs->pru_sosend)(so, sa, 0, m, 0, 0, td); 926dc9c2e01SArchie Cobbs priv->flags &= ~KSF_SENDING; 92719ff9e5fSArchie Cobbs 92819ff9e5fSArchie Cobbs /* Clean up and exit */ 92919ff9e5fSArchie Cobbs NG_FREE_META(meta); 930cb3c7a5dSArchie Cobbs return (error); 931cb3c7a5dSArchie Cobbs } 932cb3c7a5dSArchie Cobbs 933cb3c7a5dSArchie Cobbs /* 934cb3c7a5dSArchie Cobbs * Destroy node 935cb3c7a5dSArchie Cobbs */ 936cb3c7a5dSArchie Cobbs static int 937069154d5SJulian Elischer ng_ksocket_shutdown(node_p node) 938cb3c7a5dSArchie Cobbs { 93930400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 940f97e0a07SJulian Elischer priv_p embryo; 941cb3c7a5dSArchie Cobbs 94219bff684SArchie Cobbs /* Close our socket (if any) */ 94319bff684SArchie Cobbs if (priv->so != NULL) { 944f8307e12SArchie Cobbs priv->so->so_upcall = NULL; 945f8307e12SArchie Cobbs priv->so->so_rcv.sb_flags &= ~SB_UPCALL; 946f97e0a07SJulian Elischer priv->so->so_snd.sb_flags &= ~SB_UPCALL; 94719bff684SArchie Cobbs soclose(priv->so); 94819bff684SArchie Cobbs priv->so = NULL; 94919bff684SArchie Cobbs } 95019bff684SArchie Cobbs 951f97e0a07SJulian Elischer /* If we are an embryo, take ourselves out of the parent's list */ 952f97e0a07SJulian Elischer if (priv->flags & KSF_EMBRYONIC) { 953f97e0a07SJulian Elischer LIST_REMOVE(priv, siblings); 954f97e0a07SJulian Elischer priv->flags &= ~KSF_EMBRYONIC; 955f97e0a07SJulian Elischer } 956f97e0a07SJulian Elischer 957f97e0a07SJulian Elischer /* Remove any embryonic children we have */ 958f97e0a07SJulian Elischer while (!LIST_EMPTY(&priv->embryos)) { 959f97e0a07SJulian Elischer embryo = LIST_FIRST(&priv->embryos); 960f97e0a07SJulian Elischer ng_rmnode_self(embryo->node); 961f97e0a07SJulian Elischer } 962f97e0a07SJulian Elischer 963cb3c7a5dSArchie Cobbs /* Take down netgraph node */ 964cb3c7a5dSArchie Cobbs bzero(priv, sizeof(*priv)); 9659c8c302fSJulian Elischer FREE(priv, M_NETGRAPH_KSOCKET); 96630400f03SJulian Elischer NG_NODE_SET_PRIVATE(node, NULL); 96730400f03SJulian Elischer NG_NODE_UNREF(node); /* let the node escape */ 968cb3c7a5dSArchie Cobbs return (0); 969cb3c7a5dSArchie Cobbs } 970cb3c7a5dSArchie Cobbs 971cb3c7a5dSArchie Cobbs /* 972cb3c7a5dSArchie Cobbs * Hook disconnection 973cb3c7a5dSArchie Cobbs */ 974cb3c7a5dSArchie Cobbs static int 975cb3c7a5dSArchie Cobbs ng_ksocket_disconnect(hook_p hook) 976cb3c7a5dSArchie Cobbs { 97730400f03SJulian Elischer KASSERT(NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0, 9786e551fb6SDavid E. O'Brien ("%s: numhooks=%d?", __func__, 97993caaaa7SArchie Cobbs NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)))); 98030400f03SJulian Elischer if (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))) 98130400f03SJulian Elischer ng_rmnode_self(NG_HOOK_NODE(hook)); 982cb3c7a5dSArchie Cobbs return (0); 983cb3c7a5dSArchie Cobbs } 984cb3c7a5dSArchie Cobbs 985cb3c7a5dSArchie Cobbs /************************************************************************ 986cb3c7a5dSArchie Cobbs HELPER STUFF 987cb3c7a5dSArchie Cobbs ************************************************************************/ 988cb3c7a5dSArchie Cobbs /* 989f97e0a07SJulian Elischer * You should no-longer "just call" a netgraph node function 990f97e0a07SJulian Elischer * from an external asynchronous event. 991f97e0a07SJulian Elischer * This is because in doing so you are ignoring the locking on the netgraph 992f97e0a07SJulian Elischer * nodes. Instead call your function via 993f97e0a07SJulian Elischer * "int ng_send_fn(node_p node, hook_p hook, ng_item_fn *fn, 994f97e0a07SJulian Elischer * void *arg1, int arg2);" 995f97e0a07SJulian Elischer * this will call the function you chose, but will first do all the 996f97e0a07SJulian Elischer * locking rigmarole. Your function MAY only be called at some distant future 997f97e0a07SJulian Elischer * time (several millisecs away) so don't give it any arguments 998f97e0a07SJulian Elischer * that may be revoked soon (e.g. on your stack). 999f97e0a07SJulian Elischer * In this case even the 'so' argument is doubtful. 1000f97e0a07SJulian Elischer * While the function request is being processed the node 1001f97e0a07SJulian Elischer * has an extra reference and as such will not disappear until 1002f97e0a07SJulian Elischer * the request has at least been done, but the 'so' may not be so lucky. 1003f97e0a07SJulian Elischer * handle this by checking the validity of the node in the target function 1004f97e0a07SJulian Elischer * before dereferencing the socket pointer. 1005cb3c7a5dSArchie Cobbs */ 1006f97e0a07SJulian Elischer 1007cb3c7a5dSArchie Cobbs static void 1008cb3c7a5dSArchie Cobbs ng_ksocket_incoming(struct socket *so, void *arg, int waitflag) 1009cb3c7a5dSArchie Cobbs { 1010cb3c7a5dSArchie Cobbs const node_p node = arg; 1011f97e0a07SJulian Elischer 1012f97e0a07SJulian Elischer ng_send_fn(node, NULL, &ng_ksocket_incoming2, so, waitflag); 1013f97e0a07SJulian Elischer } 1014f97e0a07SJulian Elischer 1015f97e0a07SJulian Elischer 1016f97e0a07SJulian Elischer /* 1017f97e0a07SJulian Elischer * When incoming data is appended to the socket, we get notified here. 1018f97e0a07SJulian Elischer * This is also called whenever a significant event occurs for the socket. 1019f97e0a07SJulian Elischer * We know that HOOK is NULL. Because of how we were called we know we have a 1020f97e0a07SJulian Elischer * lock on this node an are participating inthe netgraph locking. 1021f97e0a07SJulian Elischer * Our original caller may have queued this even some time ago and 1022f97e0a07SJulian Elischer * we cannot trust that he even still exists. The node however is being 1023f97e0a07SJulian Elischer * held with a reference by the queueing code, at least until we finish, 1024f97e0a07SJulian Elischer * even if it has been zapped, so first check it's validiy 1025f97e0a07SJulian Elischer * before we trust the socket (which was derived from it). 1026f97e0a07SJulian Elischer */ 1027f97e0a07SJulian Elischer static void 1028f97e0a07SJulian Elischer ng_ksocket_incoming2(node_p node, hook_p hook, void *arg1, int waitflag) 1029f97e0a07SJulian Elischer { 1030f97e0a07SJulian Elischer struct socket *so = arg1; 103130400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 1032cb3c7a5dSArchie Cobbs struct mbuf *m; 1033f97e0a07SJulian Elischer struct ng_mesg *response; 1034cb3c7a5dSArchie Cobbs struct uio auio; 103519bff684SArchie Cobbs int s, flags, error; 103619bff684SArchie Cobbs 103719bff684SArchie Cobbs s = splnet(); 1038cb3c7a5dSArchie Cobbs 1039cb3c7a5dSArchie Cobbs /* Sanity check */ 104030400f03SJulian Elischer if (NG_NODE_NOT_VALID(node)) { 104119bff684SArchie Cobbs splx(s); 1042cb3c7a5dSArchie Cobbs return; 104319bff684SArchie Cobbs } 1044f97e0a07SJulian Elischer /* so = priv->so; *//* XXX could have derived this like so */ 10456e551fb6SDavid E. O'Brien KASSERT(so == priv->so, ("%s: wrong socket", __func__)); 1046f97e0a07SJulian Elischer 1047f97e0a07SJulian Elischer /* Check whether a pending connect operation has completed */ 1048f97e0a07SJulian Elischer if (priv->flags & KSF_CONNECTING) { 1049f97e0a07SJulian Elischer if ((error = so->so_error) != 0) { 1050f97e0a07SJulian Elischer so->so_error = 0; 1051f97e0a07SJulian Elischer so->so_state &= ~SS_ISCONNECTING; 1052f97e0a07SJulian Elischer } 1053f97e0a07SJulian Elischer if (!(so->so_state & SS_ISCONNECTING)) { 1054f97e0a07SJulian Elischer NG_MKMESSAGE(response, NGM_KSOCKET_COOKIE, 1055f97e0a07SJulian Elischer NGM_KSOCKET_CONNECT, sizeof(int32_t), waitflag); 1056f97e0a07SJulian Elischer if (response != NULL) { 1057f97e0a07SJulian Elischer response->header.flags |= NGF_RESP; 1058f97e0a07SJulian Elischer response->header.token = priv->response_token; 1059f97e0a07SJulian Elischer *(int32_t *)response->data = error; 1060f97e0a07SJulian Elischer /* 1061f97e0a07SJulian Elischer * send an async "response" message 1062f97e0a07SJulian Elischer * to the node that set us up 1063f97e0a07SJulian Elischer * (if it still exists) 1064f97e0a07SJulian Elischer */ 1065f97e0a07SJulian Elischer NG_SEND_MSG_ID(error, node, response, 1066f97e0a07SJulian Elischer priv->response_addr, NULL); 1067f97e0a07SJulian Elischer } 1068f97e0a07SJulian Elischer priv->flags &= ~KSF_CONNECTING; 10694cc20ab1SSeigo Tanimura } 1070f97e0a07SJulian Elischer } 1071f97e0a07SJulian Elischer 1072f97e0a07SJulian Elischer /* Check whether a pending accept operation has completed */ 1073f97e0a07SJulian Elischer if (priv->flags & KSF_ACCEPTING) { 1074f97e0a07SJulian Elischer error = ng_ksocket_check_accept(priv); 1075f97e0a07SJulian Elischer if (error != EWOULDBLOCK) 1076f97e0a07SJulian Elischer priv->flags &= ~KSF_ACCEPTING; 1077f97e0a07SJulian Elischer if (error == 0) 1078f97e0a07SJulian Elischer ng_ksocket_finish_accept(priv); 1079f97e0a07SJulian Elischer } 1080f97e0a07SJulian Elischer 1081f97e0a07SJulian Elischer /* 1082f97e0a07SJulian Elischer * If we don't have a hook, we must handle data events later. When 1083f97e0a07SJulian Elischer * the hook gets created and is connected, this upcall function 1084f97e0a07SJulian Elischer * will be called again. 1085f97e0a07SJulian Elischer */ 1086f97e0a07SJulian Elischer if (priv->hook == NULL) { 1087f97e0a07SJulian Elischer splx(s); 1088f97e0a07SJulian Elischer return; 1089f97e0a07SJulian Elischer } 1090cb3c7a5dSArchie Cobbs 1091cb3c7a5dSArchie Cobbs /* Read and forward available mbuf's */ 1092b40ce416SJulian Elischer auio.uio_td = NULL; 1093cb3c7a5dSArchie Cobbs auio.uio_resid = 1000000000; 1094cb3c7a5dSArchie Cobbs flags = MSG_DONTWAIT; 109519ff9e5fSArchie Cobbs while (1) { 109619ff9e5fSArchie Cobbs struct sockaddr *sa = NULL; 109719ff9e5fSArchie Cobbs meta_p meta = NULL; 1098f8307e12SArchie Cobbs struct mbuf *n; 1099f8307e12SArchie Cobbs 110019ff9e5fSArchie Cobbs /* Try to get next packet from socket */ 110119ff9e5fSArchie Cobbs if ((error = (*so->so_proto->pr_usrreqs->pru_soreceive) 11024cc20ab1SSeigo Tanimura (so, (so->so_state & SS_ISCONNECTED) ? NULL : &sa, 110319ff9e5fSArchie Cobbs &auio, &m, (struct mbuf **)0, &flags)) != 0) 110419ff9e5fSArchie Cobbs break; 110519ff9e5fSArchie Cobbs 110619ff9e5fSArchie Cobbs /* See if we got anything */ 110719ff9e5fSArchie Cobbs if (m == NULL) { 110819ff9e5fSArchie Cobbs if (sa != NULL) 110919ff9e5fSArchie Cobbs FREE(sa, M_SONAME); 111019ff9e5fSArchie Cobbs break; 111119ff9e5fSArchie Cobbs } 111219ff9e5fSArchie Cobbs 1113f8307e12SArchie Cobbs /* Don't trust the various socket layers to get the 1114f8307e12SArchie Cobbs packet header and length correct (eg. kern/15175) */ 111519ff9e5fSArchie Cobbs for (n = m, m->m_pkthdr.len = 0; n != NULL; n = n->m_next) 1116f8307e12SArchie Cobbs m->m_pkthdr.len += n->m_len; 111719ff9e5fSArchie Cobbs 111819ff9e5fSArchie Cobbs /* Put peer's socket address (if any) into a meta info blob */ 111919ff9e5fSArchie Cobbs if (sa != NULL) { 112019ff9e5fSArchie Cobbs struct meta_field_header *mhead; 112119ff9e5fSArchie Cobbs u_int len; 112219ff9e5fSArchie Cobbs 112319ff9e5fSArchie Cobbs len = sizeof(*meta) + sizeof(*mhead) + sa->sa_len; 112419ff9e5fSArchie Cobbs MALLOC(meta, meta_p, len, M_NETGRAPH_META, M_NOWAIT); 112519ff9e5fSArchie Cobbs if (meta == NULL) { 112619ff9e5fSArchie Cobbs FREE(sa, M_SONAME); 112719ff9e5fSArchie Cobbs goto sendit; 1128f8307e12SArchie Cobbs } 112919ff9e5fSArchie Cobbs mhead = &meta->options[0]; 113019ff9e5fSArchie Cobbs bzero(meta, sizeof(*meta)); 113119ff9e5fSArchie Cobbs bzero(mhead, sizeof(*mhead)); 113219ff9e5fSArchie Cobbs meta->allocated_len = len; 113319ff9e5fSArchie Cobbs meta->used_len = len; 113419ff9e5fSArchie Cobbs mhead->cookie = NGM_KSOCKET_COOKIE; 113519ff9e5fSArchie Cobbs mhead->type = NG_KSOCKET_META_SOCKADDR; 113619ff9e5fSArchie Cobbs mhead->len = sizeof(*mhead) + sa->sa_len; 113719ff9e5fSArchie Cobbs bcopy(sa, mhead->data, sa->sa_len); 113819ff9e5fSArchie Cobbs FREE(sa, M_SONAME); 113919ff9e5fSArchie Cobbs } 114019ff9e5fSArchie Cobbs 114119ff9e5fSArchie Cobbs sendit: /* Forward data with optional peer sockaddr as meta info */ 114219ff9e5fSArchie Cobbs NG_SEND_DATA(error, priv->hook, m, meta); 114319ff9e5fSArchie Cobbs } 1144f97e0a07SJulian Elischer 1145f97e0a07SJulian Elischer /* 1146f97e0a07SJulian Elischer * If the peer has closed the connection, forward a 0-length mbuf 1147f97e0a07SJulian Elischer * to indicate end-of-file. 1148f97e0a07SJulian Elischer */ 1149f97e0a07SJulian Elischer if (so->so_state & SS_CANTRCVMORE && !(priv->flags & KSF_EOFSEEN)) { 1150f97e0a07SJulian Elischer MGETHDR(m, waitflag, MT_DATA); 1151f97e0a07SJulian Elischer if (m != NULL) { 1152f97e0a07SJulian Elischer m->m_len = m->m_pkthdr.len = 0; 1153f97e0a07SJulian Elischer NG_SEND_DATA_ONLY(error, priv->hook, m); 1154f97e0a07SJulian Elischer } 1155f97e0a07SJulian Elischer priv->flags |= KSF_EOFSEEN; 11564cc20ab1SSeigo Tanimura } 115719bff684SArchie Cobbs splx(s); 1158cb3c7a5dSArchie Cobbs } 1159cb3c7a5dSArchie Cobbs 1160cb3c7a5dSArchie Cobbs /* 1161f97e0a07SJulian Elischer * Check for a completed incoming connection and return 0 if one is found. 1162f97e0a07SJulian Elischer * Otherwise return the appropriate error code. 1163f97e0a07SJulian Elischer */ 1164f97e0a07SJulian Elischer static int 1165f97e0a07SJulian Elischer ng_ksocket_check_accept(priv_p priv) 1166f97e0a07SJulian Elischer { 1167f97e0a07SJulian Elischer struct socket *const head = priv->so; 1168f97e0a07SJulian Elischer int error; 1169f97e0a07SJulian Elischer 1170f97e0a07SJulian Elischer if ((error = head->so_error) != 0) { 1171f97e0a07SJulian Elischer head->so_error = 0; 1172f97e0a07SJulian Elischer return error; 1173f97e0a07SJulian Elischer } 1174f97e0a07SJulian Elischer if (TAILQ_EMPTY(&head->so_comp)) { 11754cc20ab1SSeigo Tanimura if (head->so_state & SS_CANTRCVMORE) 1176f97e0a07SJulian Elischer return ECONNABORTED; 1177f97e0a07SJulian Elischer return EWOULDBLOCK; 1178f97e0a07SJulian Elischer } 1179f97e0a07SJulian Elischer return 0; 1180f97e0a07SJulian Elischer } 1181f97e0a07SJulian Elischer 1182f97e0a07SJulian Elischer /* 1183f97e0a07SJulian Elischer * Handle the first completed incoming connection, assumed to be already 1184f97e0a07SJulian Elischer * on the socket's so_comp queue. 1185f97e0a07SJulian Elischer */ 1186f97e0a07SJulian Elischer static void 1187f97e0a07SJulian Elischer ng_ksocket_finish_accept(priv_p priv) 1188f97e0a07SJulian Elischer { 1189f97e0a07SJulian Elischer struct socket *const head = priv->so; 1190f97e0a07SJulian Elischer struct socket *so; 1191f97e0a07SJulian Elischer struct sockaddr *sa = NULL; 1192f97e0a07SJulian Elischer struct ng_mesg *resp; 1193f97e0a07SJulian Elischer struct ng_ksocket_accept *resp_data; 1194f97e0a07SJulian Elischer node_p node; 1195f97e0a07SJulian Elischer priv_p priv2; 1196f97e0a07SJulian Elischer int len; 1197f97e0a07SJulian Elischer int error; 1198f97e0a07SJulian Elischer 1199f97e0a07SJulian Elischer so = TAILQ_FIRST(&head->so_comp); 1200f97e0a07SJulian Elischer if (so == NULL) /* Should never happen */ 1201f97e0a07SJulian Elischer return; 1202f97e0a07SJulian Elischer TAILQ_REMOVE(&head->so_comp, so, so_list); 1203f97e0a07SJulian Elischer head->so_qlen--; 1204f97e0a07SJulian Elischer 1205f97e0a07SJulian Elischer /* XXX KNOTE(&head->so_rcv.sb_sel.si_note, 0); */ 1206f97e0a07SJulian Elischer 1207f97e0a07SJulian Elischer so->so_state &= ~SS_COMP; 1208f97e0a07SJulian Elischer so->so_state |= SS_NBIO; 1209f97e0a07SJulian Elischer so->so_head = NULL; 1210f97e0a07SJulian Elischer 1211f97e0a07SJulian Elischer soaccept(so, &sa); 1212f97e0a07SJulian Elischer 1213f97e0a07SJulian Elischer len = OFFSETOF(struct ng_ksocket_accept, addr); 1214f97e0a07SJulian Elischer if (sa != NULL) 1215f97e0a07SJulian Elischer len += sa->sa_len; 1216f97e0a07SJulian Elischer 1217f97e0a07SJulian Elischer NG_MKMESSAGE(resp, NGM_KSOCKET_COOKIE, NGM_KSOCKET_ACCEPT, len, 1218f97e0a07SJulian Elischer M_NOWAIT); 1219f97e0a07SJulian Elischer if (resp == NULL) { 1220f97e0a07SJulian Elischer soclose(so); 1221f97e0a07SJulian Elischer goto out; 1222f97e0a07SJulian Elischer } 1223f97e0a07SJulian Elischer resp->header.flags |= NGF_RESP; 1224f97e0a07SJulian Elischer resp->header.token = priv->response_token; 1225f97e0a07SJulian Elischer 1226f97e0a07SJulian Elischer /* Clone a ksocket node to wrap the new socket */ 1227f97e0a07SJulian Elischer error = ng_make_node_common(&ng_ksocket_typestruct, &node); 1228f97e0a07SJulian Elischer if (error) { 1229f97e0a07SJulian Elischer FREE(resp, M_NETGRAPH); 1230f97e0a07SJulian Elischer soclose(so); 1231f97e0a07SJulian Elischer goto out; 1232f97e0a07SJulian Elischer } 1233f97e0a07SJulian Elischer 1234f97e0a07SJulian Elischer if (ng_ksocket_constructor(node) != 0) { 1235f97e0a07SJulian Elischer NG_NODE_UNREF(node); 1236f97e0a07SJulian Elischer FREE(resp, M_NETGRAPH); 1237f97e0a07SJulian Elischer soclose(so); 1238f97e0a07SJulian Elischer goto out; 1239f97e0a07SJulian Elischer } 1240f97e0a07SJulian Elischer 1241f97e0a07SJulian Elischer priv2 = NG_NODE_PRIVATE(node); 1242f97e0a07SJulian Elischer priv2->so = so; 1243f97e0a07SJulian Elischer priv2->flags |= KSF_CLONED | KSF_EMBRYONIC; 1244f97e0a07SJulian Elischer 1245f97e0a07SJulian Elischer /* 1246f97e0a07SJulian Elischer * Insert the cloned node into a list of embryonic children 1247f97e0a07SJulian Elischer * on the parent node. When a hook is created on the cloned 1248f97e0a07SJulian Elischer * node it will be removed from this list. When the parent 1249f97e0a07SJulian Elischer * is destroyed it will destroy any embryonic children it has. 1250f97e0a07SJulian Elischer */ 1251f97e0a07SJulian Elischer LIST_INSERT_HEAD(&priv->embryos, priv2, siblings); 1252f97e0a07SJulian Elischer 1253f97e0a07SJulian Elischer so->so_upcallarg = (caddr_t)node; 1254f97e0a07SJulian Elischer so->so_upcall = ng_ksocket_incoming; 1255f97e0a07SJulian Elischer so->so_rcv.sb_flags |= SB_UPCALL; 1256f97e0a07SJulian Elischer so->so_snd.sb_flags |= SB_UPCALL; 1257f97e0a07SJulian Elischer 1258f97e0a07SJulian Elischer /* Fill in the response data and send it or return it to the caller */ 1259f97e0a07SJulian Elischer resp_data = (struct ng_ksocket_accept *)resp->data; 1260f97e0a07SJulian Elischer resp_data->nodeid = NG_NODE_ID(node); 1261f97e0a07SJulian Elischer if (sa != NULL) 1262f97e0a07SJulian Elischer bcopy(sa, &resp_data->addr, sa->sa_len); 1263f97e0a07SJulian Elischer NG_SEND_MSG_ID(error, node, resp, priv->response_addr, NULL); 1264f97e0a07SJulian Elischer 1265f97e0a07SJulian Elischer out: 1266f97e0a07SJulian Elischer if (sa != NULL) 1267f97e0a07SJulian Elischer FREE(sa, M_SONAME); 1268f97e0a07SJulian Elischer } 1269f97e0a07SJulian Elischer 1270f97e0a07SJulian Elischer /* 1271cb3c7a5dSArchie Cobbs * Parse out either an integer value or an alias. 1272cb3c7a5dSArchie Cobbs */ 1273cb3c7a5dSArchie Cobbs static int 1274cb3c7a5dSArchie Cobbs ng_ksocket_parse(const struct ng_ksocket_alias *aliases, 1275cb3c7a5dSArchie Cobbs const char *s, int family) 1276cb3c7a5dSArchie Cobbs { 1277cb3c7a5dSArchie Cobbs int k, val; 127825792ef3SArchie Cobbs char *eptr; 1279cb3c7a5dSArchie Cobbs 1280cb3c7a5dSArchie Cobbs /* Try aliases */ 1281cb3c7a5dSArchie Cobbs for (k = 0; aliases[k].name != NULL; k++) { 1282cb3c7a5dSArchie Cobbs if (strcmp(s, aliases[k].name) == 0 1283cb3c7a5dSArchie Cobbs && aliases[k].family == family) 1284cb3c7a5dSArchie Cobbs return aliases[k].value; 1285cb3c7a5dSArchie Cobbs } 1286cb3c7a5dSArchie Cobbs 1287cb3c7a5dSArchie Cobbs /* Try parsing as a number */ 1288cb3c7a5dSArchie Cobbs val = (int)strtoul(s, &eptr, 10); 1289f8307e12SArchie Cobbs if (val < 0 || *eptr != '\0') 1290cb3c7a5dSArchie Cobbs return (-1); 1291cb3c7a5dSArchie Cobbs return (val); 1292cb3c7a5dSArchie Cobbs } 1293cb3c7a5dSArchie Cobbs 1294