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