1cb3c7a5dSArchie Cobbs /* 2cb3c7a5dSArchie Cobbs * ng_ksocket.c 3c398230bSWarner Losh */ 4c398230bSWarner Losh 5c398230bSWarner Losh /*- 6cb3c7a5dSArchie Cobbs * Copyright (c) 1996-1999 Whistle Communications, Inc. 7cb3c7a5dSArchie Cobbs * All rights reserved. 8cb3c7a5dSArchie Cobbs * 9cb3c7a5dSArchie Cobbs * Subject to the following obligations and disclaimer of warranty, use and 10cb3c7a5dSArchie Cobbs * redistribution of this software, in source or object code forms, with or 11cb3c7a5dSArchie Cobbs * without modifications are expressly permitted by Whistle Communications; 12cb3c7a5dSArchie Cobbs * provided, however, that: 13cb3c7a5dSArchie Cobbs * 1. Any and all reproductions of the source or object code must include the 14cb3c7a5dSArchie Cobbs * copyright notice above and the following disclaimer of warranties; and 15cb3c7a5dSArchie Cobbs * 2. No rights are granted, in any manner or form, to use Whistle 16cb3c7a5dSArchie Cobbs * Communications, Inc. trademarks, including the mark "WHISTLE 17cb3c7a5dSArchie Cobbs * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 18cb3c7a5dSArchie Cobbs * such appears in the above copyright notice or in the software. 19cb3c7a5dSArchie Cobbs * 20cb3c7a5dSArchie Cobbs * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 21cb3c7a5dSArchie Cobbs * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 22cb3c7a5dSArchie Cobbs * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 23cb3c7a5dSArchie Cobbs * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 24cb3c7a5dSArchie Cobbs * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 25cb3c7a5dSArchie Cobbs * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 26cb3c7a5dSArchie Cobbs * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 27cb3c7a5dSArchie Cobbs * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 28cb3c7a5dSArchie Cobbs * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 29cb3c7a5dSArchie Cobbs * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 30cb3c7a5dSArchie Cobbs * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 31cb3c7a5dSArchie Cobbs * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 32cb3c7a5dSArchie Cobbs * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 33cb3c7a5dSArchie Cobbs * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34cb3c7a5dSArchie Cobbs * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 35cb3c7a5dSArchie Cobbs * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 36cb3c7a5dSArchie Cobbs * OF SUCH DAMAGE. 37cb3c7a5dSArchie Cobbs * 38cc3bbd68SJulian Elischer * Author: Archie Cobbs <archie@freebsd.org> 39cb3c7a5dSArchie Cobbs * 40cb3c7a5dSArchie Cobbs * $FreeBSD$ 41cb3c7a5dSArchie Cobbs * $Whistle: ng_ksocket.c,v 1.1 1999/11/16 20:04:40 archie Exp $ 42cb3c7a5dSArchie Cobbs */ 43cb3c7a5dSArchie Cobbs 44cb3c7a5dSArchie Cobbs /* 45cb3c7a5dSArchie Cobbs * Kernel socket node type. This node type is basically a kernel-mode 46cb3c7a5dSArchie Cobbs * version of a socket... kindof like the reverse of the socket node type. 47cb3c7a5dSArchie Cobbs */ 48cb3c7a5dSArchie Cobbs 49cb3c7a5dSArchie Cobbs #include <sys/param.h> 50cb3c7a5dSArchie Cobbs #include <sys/systm.h> 51cb3c7a5dSArchie Cobbs #include <sys/kernel.h> 52cb3c7a5dSArchie Cobbs #include <sys/mbuf.h> 53cb3c7a5dSArchie Cobbs #include <sys/proc.h> 54cb3c7a5dSArchie Cobbs #include <sys/malloc.h> 55f8307e12SArchie Cobbs #include <sys/ctype.h> 56cb3c7a5dSArchie Cobbs #include <sys/protosw.h> 57cb3c7a5dSArchie Cobbs #include <sys/errno.h> 58cb3c7a5dSArchie Cobbs #include <sys/socket.h> 59cb3c7a5dSArchie Cobbs #include <sys/socketvar.h> 60cb3c7a5dSArchie Cobbs #include <sys/uio.h> 61f8307e12SArchie Cobbs #include <sys/un.h> 62cb3c7a5dSArchie Cobbs 63cb3c7a5dSArchie Cobbs #include <netgraph/ng_message.h> 64cb3c7a5dSArchie Cobbs #include <netgraph/netgraph.h> 65f8307e12SArchie Cobbs #include <netgraph/ng_parse.h> 66cb3c7a5dSArchie Cobbs #include <netgraph/ng_ksocket.h> 67cb3c7a5dSArchie Cobbs 68cb3c7a5dSArchie Cobbs #include <netinet/in.h> 69cb3c7a5dSArchie Cobbs #include <netatalk/at.h> 70cb3c7a5dSArchie Cobbs 719c8c302fSJulian Elischer #ifdef NG_SEPARATE_MALLOC 729c8c302fSJulian Elischer MALLOC_DEFINE(M_NETGRAPH_KSOCKET, "netgraph_ksock", "netgraph ksock node "); 739c8c302fSJulian Elischer #else 749c8c302fSJulian Elischer #define M_NETGRAPH_KSOCKET M_NETGRAPH 759c8c302fSJulian Elischer #endif 769c8c302fSJulian Elischer 77f8307e12SArchie Cobbs #define OFFSETOF(s, e) ((char *)&((s *)0)->e - (char *)((s *)0)) 78f8307e12SArchie Cobbs #define SADATA_OFFSET (OFFSETOF(struct sockaddr, sa_data)) 79f8307e12SArchie Cobbs 80cb3c7a5dSArchie Cobbs /* Node private data */ 8119bff684SArchie Cobbs struct ng_ksocket_private { 82f97e0a07SJulian Elischer node_p node; 83cb3c7a5dSArchie Cobbs hook_p hook; 84cb3c7a5dSArchie Cobbs struct socket *so; 85f97e0a07SJulian Elischer LIST_HEAD(, ng_ksocket_private) embryos; 86f97e0a07SJulian Elischer LIST_ENTRY(ng_ksocket_private) siblings; 87f97e0a07SJulian Elischer u_int32_t flags; 88f97e0a07SJulian Elischer u_int32_t response_token; 89f97e0a07SJulian Elischer ng_ID_t response_addr; 90cb3c7a5dSArchie Cobbs }; 9119bff684SArchie Cobbs typedef struct ng_ksocket_private *priv_p; 92cb3c7a5dSArchie Cobbs 93f97e0a07SJulian Elischer /* Flags for priv_p */ 94f97e0a07SJulian Elischer #define KSF_CONNECTING 0x00000001 /* Waiting for connection complete */ 95f97e0a07SJulian Elischer #define KSF_ACCEPTING 0x00000002 /* Waiting for accept complete */ 96f97e0a07SJulian Elischer #define KSF_EOFSEEN 0x00000004 /* Have sent 0-length EOF mbuf */ 97f97e0a07SJulian Elischer #define KSF_CLONED 0x00000008 /* Cloned from an accepting socket */ 98f97e0a07SJulian Elischer #define KSF_EMBRYONIC 0x00000010 /* Cloned node with no hooks yet */ 99dc9c2e01SArchie Cobbs #define KSF_SENDING 0x00000020 /* Sending on socket */ 100f97e0a07SJulian Elischer 101cb3c7a5dSArchie Cobbs /* Netgraph node methods */ 102cb3c7a5dSArchie Cobbs static ng_constructor_t ng_ksocket_constructor; 103cb3c7a5dSArchie Cobbs static ng_rcvmsg_t ng_ksocket_rcvmsg; 104069154d5SJulian Elischer static ng_shutdown_t ng_ksocket_shutdown; 105cb3c7a5dSArchie Cobbs static ng_newhook_t ng_ksocket_newhook; 106cb3c7a5dSArchie Cobbs static ng_rcvdata_t ng_ksocket_rcvdata; 107f97e0a07SJulian Elischer static ng_connect_t ng_ksocket_connect; 108cb3c7a5dSArchie Cobbs static ng_disconnect_t ng_ksocket_disconnect; 109cb3c7a5dSArchie Cobbs 110cb3c7a5dSArchie Cobbs /* Alias structure */ 111cb3c7a5dSArchie Cobbs struct ng_ksocket_alias { 112cb3c7a5dSArchie Cobbs const char *name; 113cb3c7a5dSArchie Cobbs const int value; 114cb3c7a5dSArchie Cobbs const int family; 115cb3c7a5dSArchie Cobbs }; 116cb3c7a5dSArchie Cobbs 117cb3c7a5dSArchie Cobbs /* Protocol family aliases */ 118cb3c7a5dSArchie Cobbs static const struct ng_ksocket_alias ng_ksocket_families[] = { 119cb3c7a5dSArchie Cobbs { "local", PF_LOCAL }, 120cb3c7a5dSArchie Cobbs { "inet", PF_INET }, 121cb3c7a5dSArchie Cobbs { "inet6", PF_INET6 }, 122cb3c7a5dSArchie Cobbs { "atalk", PF_APPLETALK }, 123cb3c7a5dSArchie Cobbs { "ipx", PF_IPX }, 124cb3c7a5dSArchie Cobbs { "atm", PF_ATM }, 125cb3c7a5dSArchie Cobbs { NULL, -1 }, 126cb3c7a5dSArchie Cobbs }; 127cb3c7a5dSArchie Cobbs 128cb3c7a5dSArchie Cobbs /* Socket type aliases */ 129cb3c7a5dSArchie Cobbs static const struct ng_ksocket_alias ng_ksocket_types[] = { 130cb3c7a5dSArchie Cobbs { "stream", SOCK_STREAM }, 131cb3c7a5dSArchie Cobbs { "dgram", SOCK_DGRAM }, 132cb3c7a5dSArchie Cobbs { "raw", SOCK_RAW }, 133cb3c7a5dSArchie Cobbs { "rdm", SOCK_RDM }, 134cb3c7a5dSArchie Cobbs { "seqpacket", SOCK_SEQPACKET }, 135cb3c7a5dSArchie Cobbs { NULL, -1 }, 136cb3c7a5dSArchie Cobbs }; 137cb3c7a5dSArchie Cobbs 138cb3c7a5dSArchie Cobbs /* Protocol aliases */ 139cb3c7a5dSArchie Cobbs static const struct ng_ksocket_alias ng_ksocket_protos[] = { 140cb3c7a5dSArchie Cobbs { "ip", IPPROTO_IP, PF_INET }, 141129bc895SArchie Cobbs { "raw", IPPROTO_RAW, PF_INET }, 142cb3c7a5dSArchie Cobbs { "icmp", IPPROTO_ICMP, PF_INET }, 143cb3c7a5dSArchie Cobbs { "igmp", IPPROTO_IGMP, PF_INET }, 144cb3c7a5dSArchie Cobbs { "tcp", IPPROTO_TCP, PF_INET }, 145cb3c7a5dSArchie Cobbs { "udp", IPPROTO_UDP, PF_INET }, 146cb3c7a5dSArchie Cobbs { "gre", IPPROTO_GRE, PF_INET }, 147cb3c7a5dSArchie Cobbs { "esp", IPPROTO_ESP, PF_INET }, 148cb3c7a5dSArchie Cobbs { "ah", IPPROTO_AH, PF_INET }, 149cb3c7a5dSArchie Cobbs { "swipe", IPPROTO_SWIPE, PF_INET }, 150cb3c7a5dSArchie Cobbs { "encap", IPPROTO_ENCAP, PF_INET }, 151cb3c7a5dSArchie Cobbs { "divert", IPPROTO_DIVERT, PF_INET }, 15233583c6fSJeffrey Hsu { "pim", IPPROTO_PIM, PF_INET }, 153cb3c7a5dSArchie Cobbs { "ddp", ATPROTO_DDP, PF_APPLETALK }, 154cb3c7a5dSArchie Cobbs { "aarp", ATPROTO_AARP, PF_APPLETALK }, 155cb3c7a5dSArchie Cobbs { NULL, -1 }, 156cb3c7a5dSArchie Cobbs }; 157cb3c7a5dSArchie Cobbs 158f8307e12SArchie Cobbs /* Helper functions */ 159f97e0a07SJulian Elischer static int ng_ksocket_check_accept(priv_p); 160f97e0a07SJulian Elischer static void ng_ksocket_finish_accept(priv_p); 161f8307e12SArchie Cobbs static void ng_ksocket_incoming(struct socket *so, void *arg, int waitflag); 162f8307e12SArchie Cobbs static int ng_ksocket_parse(const struct ng_ksocket_alias *aliases, 163f8307e12SArchie Cobbs const char *s, int family); 164f97e0a07SJulian Elischer static void ng_ksocket_incoming2(node_p node, hook_p hook, 165f97e0a07SJulian Elischer void *arg1, int waitflag); 166f8307e12SArchie Cobbs 167f8307e12SArchie Cobbs /************************************************************************ 168f8307e12SArchie Cobbs STRUCT SOCKADDR PARSE TYPE 169f8307e12SArchie Cobbs ************************************************************************/ 170f8307e12SArchie Cobbs 171f8307e12SArchie Cobbs /* Get the length of the data portion of a generic struct sockaddr */ 172f8307e12SArchie Cobbs static int 173f8307e12SArchie Cobbs ng_parse_generic_sockdata_getLength(const struct ng_parse_type *type, 174f8307e12SArchie Cobbs const u_char *start, const u_char *buf) 175f8307e12SArchie Cobbs { 176f8307e12SArchie Cobbs const struct sockaddr *sa; 177f8307e12SArchie Cobbs 178f8307e12SArchie Cobbs sa = (const struct sockaddr *)(buf - SADATA_OFFSET); 1791baeddb8SArchie Cobbs return (sa->sa_len < SADATA_OFFSET) ? 0 : sa->sa_len - SADATA_OFFSET; 180f8307e12SArchie Cobbs } 181f8307e12SArchie Cobbs 182f8307e12SArchie Cobbs /* Type for the variable length data portion of a generic struct sockaddr */ 183f8307e12SArchie Cobbs static const struct ng_parse_type ng_ksocket_generic_sockdata_type = { 184f8307e12SArchie Cobbs &ng_parse_bytearray_type, 185f8307e12SArchie Cobbs &ng_parse_generic_sockdata_getLength 186f8307e12SArchie Cobbs }; 187f8307e12SArchie Cobbs 188f8307e12SArchie Cobbs /* Type for a generic struct sockaddr */ 189f0184ff8SArchie Cobbs static const struct ng_parse_struct_field 190f0184ff8SArchie Cobbs ng_parse_generic_sockaddr_type_fields[] = { 19157b57be3SArchie Cobbs { "len", &ng_parse_uint8_type }, 19257b57be3SArchie Cobbs { "family", &ng_parse_uint8_type }, 193f8307e12SArchie Cobbs { "data", &ng_ksocket_generic_sockdata_type }, 194f8307e12SArchie Cobbs { NULL } 195f8307e12SArchie Cobbs }; 196f8307e12SArchie Cobbs static const struct ng_parse_type ng_ksocket_generic_sockaddr_type = { 197f8307e12SArchie Cobbs &ng_parse_struct_type, 198f0184ff8SArchie Cobbs &ng_parse_generic_sockaddr_type_fields 199f8307e12SArchie Cobbs }; 200f8307e12SArchie Cobbs 201f8307e12SArchie Cobbs /* Convert a struct sockaddr from ASCII to binary. If its a protocol 202f8307e12SArchie Cobbs family that we specially handle, do that, otherwise defer to the 203f8307e12SArchie Cobbs generic parse type ng_ksocket_generic_sockaddr_type. */ 204f8307e12SArchie Cobbs static int 205f8307e12SArchie Cobbs ng_ksocket_sockaddr_parse(const struct ng_parse_type *type, 206f8307e12SArchie Cobbs const char *s, int *off, const u_char *const start, 207f8307e12SArchie Cobbs u_char *const buf, int *buflen) 208f8307e12SArchie Cobbs { 209f8307e12SArchie Cobbs struct sockaddr *const sa = (struct sockaddr *)buf; 210f8307e12SArchie Cobbs enum ng_parse_token tok; 211f8307e12SArchie Cobbs char fambuf[32]; 212f8307e12SArchie Cobbs int family, len; 213f8307e12SArchie Cobbs char *t; 214f8307e12SArchie Cobbs 215f8307e12SArchie Cobbs /* If next token is a left curly brace, use generic parse type */ 216f8307e12SArchie Cobbs if ((tok = ng_parse_get_token(s, off, &len)) == T_LBRACE) { 217f8307e12SArchie Cobbs return (*ng_ksocket_generic_sockaddr_type.supertype->parse) 218f8307e12SArchie Cobbs (&ng_ksocket_generic_sockaddr_type, 219f8307e12SArchie Cobbs s, off, start, buf, buflen); 220f8307e12SArchie Cobbs } 221f8307e12SArchie Cobbs 222f8307e12SArchie Cobbs /* Get socket address family followed by a slash */ 223f8307e12SArchie Cobbs while (isspace(s[*off])) 224f8307e12SArchie Cobbs (*off)++; 225f8307e12SArchie Cobbs if ((t = index(s + *off, '/')) == NULL) 226f8307e12SArchie Cobbs return (EINVAL); 227f8307e12SArchie Cobbs if ((len = t - (s + *off)) > sizeof(fambuf) - 1) 228f8307e12SArchie Cobbs return (EINVAL); 229f8307e12SArchie Cobbs strncpy(fambuf, s + *off, len); 230f8307e12SArchie Cobbs fambuf[len] = '\0'; 231f8307e12SArchie Cobbs *off += len + 1; 232f8307e12SArchie Cobbs if ((family = ng_ksocket_parse(ng_ksocket_families, fambuf, 0)) == -1) 233f8307e12SArchie Cobbs return (EINVAL); 234f8307e12SArchie Cobbs 235f8307e12SArchie Cobbs /* Set family */ 236f8307e12SArchie Cobbs if (*buflen < SADATA_OFFSET) 237f8307e12SArchie Cobbs return (ERANGE); 238f8307e12SArchie Cobbs sa->sa_family = family; 239f8307e12SArchie Cobbs 240f8307e12SArchie Cobbs /* Set family-specific data and length */ 241f8307e12SArchie Cobbs switch (sa->sa_family) { 242f8307e12SArchie Cobbs case PF_LOCAL: /* Get pathname */ 243f8307e12SArchie Cobbs { 244f8307e12SArchie Cobbs const int pathoff = OFFSETOF(struct sockaddr_un, sun_path); 245f8307e12SArchie Cobbs struct sockaddr_un *const sun = (struct sockaddr_un *)sa; 246f8307e12SArchie Cobbs int toklen, pathlen; 247f8307e12SArchie Cobbs char *path; 248f8307e12SArchie Cobbs 24927121ab1SBrian Somers if ((path = ng_get_string_token(s, off, &toklen, NULL)) == NULL) 250f8307e12SArchie Cobbs return (EINVAL); 251f8307e12SArchie Cobbs pathlen = strlen(path); 252f8307e12SArchie Cobbs if (pathlen > SOCK_MAXADDRLEN) { 2539c8c302fSJulian Elischer FREE(path, M_NETGRAPH_KSOCKET); 254f8307e12SArchie Cobbs return (E2BIG); 255f8307e12SArchie Cobbs } 256f8307e12SArchie Cobbs if (*buflen < pathoff + pathlen) { 2579c8c302fSJulian Elischer FREE(path, M_NETGRAPH_KSOCKET); 258f8307e12SArchie Cobbs return (ERANGE); 259f8307e12SArchie Cobbs } 260f8307e12SArchie Cobbs *off += toklen; 261f8307e12SArchie Cobbs bcopy(path, sun->sun_path, pathlen); 262f8307e12SArchie Cobbs sun->sun_len = pathoff + pathlen; 2639c8c302fSJulian Elischer FREE(path, M_NETGRAPH_KSOCKET); 264f8307e12SArchie Cobbs break; 265f8307e12SArchie Cobbs } 266f8307e12SArchie Cobbs 267f8307e12SArchie Cobbs case PF_INET: /* Get an IP address with optional port */ 268f8307e12SArchie Cobbs { 269f8307e12SArchie Cobbs struct sockaddr_in *const sin = (struct sockaddr_in *)sa; 270f8307e12SArchie Cobbs int i; 271f8307e12SArchie Cobbs 272f8307e12SArchie Cobbs /* Parse this: <ipaddress>[:port] */ 273f8307e12SArchie Cobbs for (i = 0; i < 4; i++) { 274f8307e12SArchie Cobbs u_long val; 275f8307e12SArchie Cobbs char *eptr; 276f8307e12SArchie Cobbs 277f8307e12SArchie Cobbs val = strtoul(s + *off, &eptr, 10); 278f8307e12SArchie Cobbs if (val > 0xff || eptr == s + *off) 279f8307e12SArchie Cobbs return (EINVAL); 280f8307e12SArchie Cobbs *off += (eptr - (s + *off)); 281f8307e12SArchie Cobbs ((u_char *)&sin->sin_addr)[i] = (u_char)val; 282f8307e12SArchie Cobbs if (i < 3) { 283f8307e12SArchie Cobbs if (s[*off] != '.') 284f8307e12SArchie Cobbs return (EINVAL); 285f8307e12SArchie Cobbs (*off)++; 286f8307e12SArchie Cobbs } else if (s[*off] == ':') { 287f8307e12SArchie Cobbs (*off)++; 288f8307e12SArchie Cobbs val = strtoul(s + *off, &eptr, 10); 289f8307e12SArchie Cobbs if (val > 0xffff || eptr == s + *off) 290f8307e12SArchie Cobbs return (EINVAL); 291f8307e12SArchie Cobbs *off += (eptr - (s + *off)); 292f8307e12SArchie Cobbs sin->sin_port = htons(val); 293f8307e12SArchie Cobbs } else 294f8307e12SArchie Cobbs sin->sin_port = 0; 295f8307e12SArchie Cobbs } 296f8307e12SArchie Cobbs bzero(&sin->sin_zero, sizeof(sin->sin_zero)); 297f8307e12SArchie Cobbs sin->sin_len = sizeof(*sin); 298f8307e12SArchie Cobbs break; 299f8307e12SArchie Cobbs } 300f8307e12SArchie Cobbs 301f8307e12SArchie Cobbs #if 0 302f8307e12SArchie Cobbs case PF_APPLETALK: /* XXX implement these someday */ 303f8307e12SArchie Cobbs case PF_INET6: 304f8307e12SArchie Cobbs case PF_IPX: 305f8307e12SArchie Cobbs #endif 306f8307e12SArchie Cobbs 307f8307e12SArchie Cobbs default: 308f8307e12SArchie Cobbs return (EINVAL); 309f8307e12SArchie Cobbs } 310f8307e12SArchie Cobbs 311f8307e12SArchie Cobbs /* Done */ 312f8307e12SArchie Cobbs *buflen = sa->sa_len; 313f8307e12SArchie Cobbs return (0); 314f8307e12SArchie Cobbs } 315f8307e12SArchie Cobbs 316f8307e12SArchie Cobbs /* Convert a struct sockaddr from binary to ASCII */ 317f8307e12SArchie Cobbs static int 318f8307e12SArchie Cobbs ng_ksocket_sockaddr_unparse(const struct ng_parse_type *type, 319f8307e12SArchie Cobbs const u_char *data, int *off, char *cbuf, int cbuflen) 320f8307e12SArchie Cobbs { 321f8307e12SArchie Cobbs const struct sockaddr *sa = (const struct sockaddr *)(data + *off); 322f8307e12SArchie Cobbs int slen = 0; 323f8307e12SArchie Cobbs 324f8307e12SArchie Cobbs /* Output socket address, either in special or generic format */ 325f8307e12SArchie Cobbs switch (sa->sa_family) { 326f8307e12SArchie Cobbs case PF_LOCAL: 327f8307e12SArchie Cobbs { 328f8307e12SArchie Cobbs const int pathoff = OFFSETOF(struct sockaddr_un, sun_path); 329f8307e12SArchie Cobbs const struct sockaddr_un *sun = (const struct sockaddr_un *)sa; 330f8307e12SArchie Cobbs const int pathlen = sun->sun_len - pathoff; 331f8307e12SArchie Cobbs char pathbuf[SOCK_MAXADDRLEN + 1]; 332f8307e12SArchie Cobbs char *pathtoken; 333f8307e12SArchie Cobbs 334f8307e12SArchie Cobbs bcopy(sun->sun_path, pathbuf, pathlen); 33527121ab1SBrian Somers if ((pathtoken = ng_encode_string(pathbuf, pathlen)) == NULL) 336f8307e12SArchie Cobbs return (ENOMEM); 337f8307e12SArchie Cobbs slen += snprintf(cbuf, cbuflen, "local/%s", pathtoken); 3389c8c302fSJulian Elischer FREE(pathtoken, M_NETGRAPH_KSOCKET); 339f8307e12SArchie Cobbs if (slen >= cbuflen) 340f8307e12SArchie Cobbs return (ERANGE); 341f8307e12SArchie Cobbs *off += sun->sun_len; 342f8307e12SArchie Cobbs return (0); 343f8307e12SArchie Cobbs } 344f8307e12SArchie Cobbs 345f8307e12SArchie Cobbs case PF_INET: 346f8307e12SArchie Cobbs { 347f8307e12SArchie Cobbs const struct sockaddr_in *sin = (const struct sockaddr_in *)sa; 348f8307e12SArchie Cobbs 349f8307e12SArchie Cobbs slen += snprintf(cbuf, cbuflen, "inet/%d.%d.%d.%d", 350f8307e12SArchie Cobbs ((const u_char *)&sin->sin_addr)[0], 351f8307e12SArchie Cobbs ((const u_char *)&sin->sin_addr)[1], 352f8307e12SArchie Cobbs ((const u_char *)&sin->sin_addr)[2], 353f8307e12SArchie Cobbs ((const u_char *)&sin->sin_addr)[3]); 354f8307e12SArchie Cobbs if (sin->sin_port != 0) { 355f8307e12SArchie Cobbs slen += snprintf(cbuf + strlen(cbuf), 356f8307e12SArchie Cobbs cbuflen - strlen(cbuf), ":%d", 357f8307e12SArchie Cobbs (u_int)ntohs(sin->sin_port)); 358f8307e12SArchie Cobbs } 359f8307e12SArchie Cobbs if (slen >= cbuflen) 360f8307e12SArchie Cobbs return (ERANGE); 361f8307e12SArchie Cobbs *off += sizeof(*sin); 362f8307e12SArchie Cobbs return(0); 363f8307e12SArchie Cobbs } 364f8307e12SArchie Cobbs 365f8307e12SArchie Cobbs #if 0 366f8307e12SArchie Cobbs case PF_APPLETALK: /* XXX implement these someday */ 367f8307e12SArchie Cobbs case PF_INET6: 368f8307e12SArchie Cobbs case PF_IPX: 369f8307e12SArchie Cobbs #endif 370f8307e12SArchie Cobbs 371f8307e12SArchie Cobbs default: 372f8307e12SArchie Cobbs return (*ng_ksocket_generic_sockaddr_type.supertype->unparse) 373f8307e12SArchie Cobbs (&ng_ksocket_generic_sockaddr_type, 374f8307e12SArchie Cobbs data, off, cbuf, cbuflen); 375f8307e12SArchie Cobbs } 376f8307e12SArchie Cobbs } 377f8307e12SArchie Cobbs 378f8307e12SArchie Cobbs /* Parse type for struct sockaddr */ 379f8307e12SArchie Cobbs static const struct ng_parse_type ng_ksocket_sockaddr_type = { 380f8307e12SArchie Cobbs NULL, 381f8307e12SArchie Cobbs NULL, 382f8307e12SArchie Cobbs NULL, 383f8307e12SArchie Cobbs &ng_ksocket_sockaddr_parse, 384f8307e12SArchie Cobbs &ng_ksocket_sockaddr_unparse, 385f8307e12SArchie Cobbs NULL /* no such thing as a default struct sockaddr */ 386f8307e12SArchie Cobbs }; 387f8307e12SArchie Cobbs 388f8307e12SArchie Cobbs /************************************************************************ 389f8307e12SArchie Cobbs STRUCT NG_KSOCKET_SOCKOPT PARSE TYPE 390f8307e12SArchie Cobbs ************************************************************************/ 391f8307e12SArchie Cobbs 392f8307e12SArchie Cobbs /* Get length of the struct ng_ksocket_sockopt value field, which is the 393f8307e12SArchie Cobbs just the excess of the message argument portion over the length of 394f8307e12SArchie Cobbs the struct ng_ksocket_sockopt. */ 395f8307e12SArchie Cobbs static int 396f8307e12SArchie Cobbs ng_parse_sockoptval_getLength(const struct ng_parse_type *type, 397f8307e12SArchie Cobbs const u_char *start, const u_char *buf) 398f8307e12SArchie Cobbs { 399f8307e12SArchie Cobbs static const int offset = OFFSETOF(struct ng_ksocket_sockopt, value); 400f8307e12SArchie Cobbs const struct ng_ksocket_sockopt *sopt; 401f8307e12SArchie Cobbs const struct ng_mesg *msg; 402f8307e12SArchie Cobbs 403f8307e12SArchie Cobbs sopt = (const struct ng_ksocket_sockopt *)(buf - offset); 404f8307e12SArchie Cobbs msg = (const struct ng_mesg *)((const u_char *)sopt - sizeof(*msg)); 405f8307e12SArchie Cobbs return msg->header.arglen - sizeof(*sopt); 406f8307e12SArchie Cobbs } 407f8307e12SArchie Cobbs 408f8307e12SArchie Cobbs /* Parse type for the option value part of a struct ng_ksocket_sockopt 409f8307e12SArchie Cobbs XXX Eventually, we should handle the different socket options specially. 410f8307e12SArchie Cobbs XXX This would avoid byte order problems, eg an integer value of 1 is 411f8307e12SArchie Cobbs XXX going to be "[1]" for little endian or "[3=1]" for big endian. */ 412f8307e12SArchie Cobbs static const struct ng_parse_type ng_ksocket_sockoptval_type = { 413f8307e12SArchie Cobbs &ng_parse_bytearray_type, 414f8307e12SArchie Cobbs &ng_parse_sockoptval_getLength 415f8307e12SArchie Cobbs }; 416f8307e12SArchie Cobbs 417f8307e12SArchie Cobbs /* Parse type for struct ng_ksocket_sockopt */ 418f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_ksocket_sockopt_type_fields[] 419f8307e12SArchie Cobbs = NG_KSOCKET_SOCKOPT_INFO(&ng_ksocket_sockoptval_type); 420f8307e12SArchie Cobbs static const struct ng_parse_type ng_ksocket_sockopt_type = { 421f8307e12SArchie Cobbs &ng_parse_struct_type, 422f0184ff8SArchie Cobbs &ng_ksocket_sockopt_type_fields 423f8307e12SArchie Cobbs }; 424f8307e12SArchie Cobbs 425f97e0a07SJulian Elischer /* Parse type for struct ng_ksocket_accept */ 426f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_ksocket_accept_type_fields[] 427f97e0a07SJulian Elischer = NGM_KSOCKET_ACCEPT_INFO; 428f97e0a07SJulian Elischer static const struct ng_parse_type ng_ksocket_accept_type = { 429f97e0a07SJulian Elischer &ng_parse_struct_type, 430f0184ff8SArchie Cobbs &ng_ksocket_accept_type_fields 431f97e0a07SJulian Elischer }; 432f97e0a07SJulian Elischer 433f8307e12SArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */ 434f8307e12SArchie Cobbs static const struct ng_cmdlist ng_ksocket_cmds[] = { 435f8307e12SArchie Cobbs { 436f8307e12SArchie Cobbs NGM_KSOCKET_COOKIE, 437f8307e12SArchie Cobbs NGM_KSOCKET_BIND, 438f8307e12SArchie Cobbs "bind", 439f8307e12SArchie Cobbs &ng_ksocket_sockaddr_type, 440f8307e12SArchie Cobbs NULL 441f8307e12SArchie Cobbs }, 442f8307e12SArchie Cobbs { 443f8307e12SArchie Cobbs NGM_KSOCKET_COOKIE, 444f8307e12SArchie Cobbs NGM_KSOCKET_LISTEN, 445f8307e12SArchie Cobbs "listen", 446f8307e12SArchie Cobbs &ng_parse_int32_type, 447f8307e12SArchie Cobbs NULL 448f8307e12SArchie Cobbs }, 449f8307e12SArchie Cobbs { 450f8307e12SArchie Cobbs NGM_KSOCKET_COOKIE, 451f8307e12SArchie Cobbs NGM_KSOCKET_ACCEPT, 452f8307e12SArchie Cobbs "accept", 453f8307e12SArchie Cobbs NULL, 454f97e0a07SJulian Elischer &ng_ksocket_accept_type 455f8307e12SArchie Cobbs }, 456f8307e12SArchie Cobbs { 457f8307e12SArchie Cobbs NGM_KSOCKET_COOKIE, 458f8307e12SArchie Cobbs NGM_KSOCKET_CONNECT, 459f8307e12SArchie Cobbs "connect", 460f8307e12SArchie Cobbs &ng_ksocket_sockaddr_type, 461f97e0a07SJulian Elischer &ng_parse_int32_type 462f8307e12SArchie Cobbs }, 463f8307e12SArchie Cobbs { 464f8307e12SArchie Cobbs NGM_KSOCKET_COOKIE, 465f8307e12SArchie Cobbs NGM_KSOCKET_GETNAME, 466f8307e12SArchie Cobbs "getname", 467f8307e12SArchie Cobbs NULL, 468f8307e12SArchie Cobbs &ng_ksocket_sockaddr_type 469f8307e12SArchie Cobbs }, 470f8307e12SArchie Cobbs { 471f8307e12SArchie Cobbs NGM_KSOCKET_COOKIE, 472f8307e12SArchie Cobbs NGM_KSOCKET_GETPEERNAME, 473f8307e12SArchie Cobbs "getpeername", 474f8307e12SArchie Cobbs NULL, 475f8307e12SArchie Cobbs &ng_ksocket_sockaddr_type 476f8307e12SArchie Cobbs }, 477f8307e12SArchie Cobbs { 478f8307e12SArchie Cobbs NGM_KSOCKET_COOKIE, 479f8307e12SArchie Cobbs NGM_KSOCKET_SETOPT, 480f8307e12SArchie Cobbs "setopt", 481f8307e12SArchie Cobbs &ng_ksocket_sockopt_type, 482f8307e12SArchie Cobbs NULL 483f8307e12SArchie Cobbs }, 484f8307e12SArchie Cobbs { 485f8307e12SArchie Cobbs NGM_KSOCKET_COOKIE, 486f8307e12SArchie Cobbs NGM_KSOCKET_GETOPT, 487f8307e12SArchie Cobbs "getopt", 488f8307e12SArchie Cobbs &ng_ksocket_sockopt_type, 489f8307e12SArchie Cobbs &ng_ksocket_sockopt_type 490f8307e12SArchie Cobbs }, 491f8307e12SArchie Cobbs { 0 } 492f8307e12SArchie Cobbs }; 493f8307e12SArchie Cobbs 494f8307e12SArchie Cobbs /* Node type descriptor */ 495f8307e12SArchie Cobbs static struct ng_type ng_ksocket_typestruct = { 496f8aae777SJulian Elischer .version = NG_ABI_VERSION, 497f8aae777SJulian Elischer .name = NG_KSOCKET_NODE_TYPE, 498f8aae777SJulian Elischer .constructor = ng_ksocket_constructor, 499f8aae777SJulian Elischer .rcvmsg = ng_ksocket_rcvmsg, 500f8aae777SJulian Elischer .shutdown = ng_ksocket_shutdown, 501f8aae777SJulian Elischer .newhook = ng_ksocket_newhook, 502f8aae777SJulian Elischer .connect = ng_ksocket_connect, 503f8aae777SJulian Elischer .rcvdata = ng_ksocket_rcvdata, 504f8aae777SJulian Elischer .disconnect = ng_ksocket_disconnect, 505f8aae777SJulian Elischer .cmdlist = ng_ksocket_cmds, 506f8307e12SArchie Cobbs }; 507f8307e12SArchie Cobbs NETGRAPH_INIT(ksocket, &ng_ksocket_typestruct); 508f8307e12SArchie Cobbs 5094cc20ab1SSeigo Tanimura #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 { 55142ec1da4SRobert Watson struct thread *td = curthread; /* XXX broken */ 55230400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 55387e2c66aSHartmut Brandt char *s1, *s2, name[NG_HOOKSIZ]; 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 */ 5899c4d63daSRobert Watson error = socreate(family, &priv->so, type, protocol, 590a854ed98SJohn Baldwin td->td_ucred, td); 591f97e0a07SJulian Elischer if (error != 0) 592cb3c7a5dSArchie Cobbs return (error); 593cb3c7a5dSArchie Cobbs 594cb3c7a5dSArchie Cobbs /* XXX call soreserve() ? */ 595cb3c7a5dSArchie Cobbs 596f97e0a07SJulian Elischer } 597cb3c7a5dSArchie Cobbs 598cb3c7a5dSArchie Cobbs /* OK */ 599cb3c7a5dSArchie Cobbs priv->hook = hook; 600cb3c7a5dSArchie Cobbs return(0); 601cb3c7a5dSArchie Cobbs } 602cb3c7a5dSArchie Cobbs 603f97e0a07SJulian Elischer static int 604f97e0a07SJulian Elischer ng_ksocket_connect(hook_p hook) 605f97e0a07SJulian Elischer { 606f97e0a07SJulian Elischer node_p node = NG_HOOK_NODE(hook); 607f97e0a07SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 608f97e0a07SJulian Elischer struct socket *const so = priv->so; 609f97e0a07SJulian Elischer 610f97e0a07SJulian Elischer /* Add our hook for incoming data and other events */ 611f97e0a07SJulian Elischer priv->so->so_upcallarg = (caddr_t)node; 612f97e0a07SJulian Elischer priv->so->so_upcall = ng_ksocket_incoming; 6139535efc0SRobert Watson SOCKBUF_LOCK(&priv->so->so_rcv); 614f97e0a07SJulian Elischer priv->so->so_rcv.sb_flags |= SB_UPCALL; 6159535efc0SRobert Watson SOCKBUF_UNLOCK(&priv->so->so_rcv); 6169535efc0SRobert Watson SOCKBUF_LOCK(&priv->so->so_snd); 617f97e0a07SJulian Elischer priv->so->so_snd.sb_flags |= SB_UPCALL; 6189535efc0SRobert Watson SOCKBUF_UNLOCK(&priv->so->so_snd); 6199535efc0SRobert Watson SOCK_LOCK(priv->so); 620f97e0a07SJulian Elischer priv->so->so_state |= SS_NBIO; 6219535efc0SRobert Watson SOCK_UNLOCK(priv->so); 622f97e0a07SJulian Elischer /* 623f97e0a07SJulian Elischer * --Original comment-- 624f97e0a07SJulian Elischer * On a cloned socket we may have already received one or more 625f97e0a07SJulian Elischer * upcalls which we couldn't handle without a hook. Handle 626f97e0a07SJulian Elischer * those now. 627f97e0a07SJulian Elischer * We cannot call the upcall function directly 628f97e0a07SJulian Elischer * from here, because until this function has returned our 629f97e0a07SJulian Elischer * hook isn't connected. 630f97e0a07SJulian Elischer * 631f97e0a07SJulian Elischer * ---meta comment for -current --- 632f97e0a07SJulian Elischer * XXX This is dubius. 633f97e0a07SJulian Elischer * Upcalls between the time that the hook was 634f97e0a07SJulian Elischer * first created and now (on another processesor) will 635f97e0a07SJulian Elischer * be earlier on the queue than the request to finalise the hook. 636f97e0a07SJulian Elischer * By the time the hook is finalised, 637f97e0a07SJulian Elischer * The queued upcalls will have happenned and the code 638f97e0a07SJulian Elischer * will have discarded them because of a lack of a hook. 639f97e0a07SJulian Elischer * (socket not open). 640f97e0a07SJulian Elischer * 641f97e0a07SJulian Elischer * This is a bad byproduct of the complicated way in which hooks 642f97e0a07SJulian Elischer * are now created (3 daisy chained async events). 643f97e0a07SJulian Elischer * 644f97e0a07SJulian Elischer * Since we are a netgraph operation 645f97e0a07SJulian Elischer * We know that we hold a lock on this node. This forces the 646f97e0a07SJulian Elischer * request we make below to be queued rather than implemented 647f97e0a07SJulian Elischer * immediatly which will cause the upcall function to be called a bit 648f97e0a07SJulian Elischer * later. 649f97e0a07SJulian Elischer * However, as we will run any waiting queued operations immediatly 650f97e0a07SJulian Elischer * after doing this one, if we have not finalised the other end 651f97e0a07SJulian Elischer * of the hook, those queued operations will fail. 652f97e0a07SJulian Elischer */ 653f97e0a07SJulian Elischer if (priv->flags & KSF_CLONED) { 654f97e0a07SJulian Elischer ng_send_fn(node, NULL, &ng_ksocket_incoming2, so, M_NOWAIT); 655f97e0a07SJulian Elischer } 656f97e0a07SJulian Elischer 657f97e0a07SJulian Elischer return (0); 658f97e0a07SJulian Elischer } 659f97e0a07SJulian Elischer 660cb3c7a5dSArchie Cobbs /* 661cb3c7a5dSArchie Cobbs * Receive a control message 662cb3c7a5dSArchie Cobbs */ 663cb3c7a5dSArchie Cobbs static int 664069154d5SJulian Elischer ng_ksocket_rcvmsg(node_p node, item_p item, hook_p lasthook) 665cb3c7a5dSArchie Cobbs { 66642ec1da4SRobert Watson struct thread *td = curthread; /* XXX broken */ 66730400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 668f8307e12SArchie Cobbs struct socket *const so = priv->so; 669cb3c7a5dSArchie Cobbs struct ng_mesg *resp = NULL; 670cb3c7a5dSArchie Cobbs int error = 0; 671069154d5SJulian Elischer struct ng_mesg *msg; 672f97e0a07SJulian Elischer ng_ID_t raddr; 673cb3c7a5dSArchie Cobbs 674069154d5SJulian Elischer NGI_GET_MSG(item, msg); 675cb3c7a5dSArchie Cobbs switch (msg->header.typecookie) { 676cb3c7a5dSArchie Cobbs case NGM_KSOCKET_COOKIE: 677cb3c7a5dSArchie Cobbs switch (msg->header.cmd) { 678cb3c7a5dSArchie Cobbs case NGM_KSOCKET_BIND: 679cb3c7a5dSArchie Cobbs { 680f8307e12SArchie Cobbs struct sockaddr *const sa 681f8307e12SArchie Cobbs = (struct sockaddr *)msg->data; 682cb3c7a5dSArchie Cobbs 683f8307e12SArchie Cobbs /* Sanity check */ 684f8307e12SArchie Cobbs if (msg->header.arglen < SADATA_OFFSET 685f8307e12SArchie Cobbs || msg->header.arglen < sa->sa_len) 686f8307e12SArchie Cobbs ERROUT(EINVAL); 687f8307e12SArchie Cobbs if (so == NULL) 688f8307e12SArchie Cobbs ERROUT(ENXIO); 689cb3c7a5dSArchie Cobbs 690f8307e12SArchie Cobbs /* Bind */ 691b40ce416SJulian Elischer error = sobind(so, sa, td); 692cb3c7a5dSArchie Cobbs break; 693cb3c7a5dSArchie Cobbs } 694cb3c7a5dSArchie Cobbs case NGM_KSOCKET_LISTEN: 695cb3c7a5dSArchie Cobbs { 696f8307e12SArchie Cobbs /* Sanity check */ 697f97e0a07SJulian Elischer if (msg->header.arglen != sizeof(int32_t)) 698cb3c7a5dSArchie Cobbs ERROUT(EINVAL); 699f8307e12SArchie Cobbs if (so == NULL) 700f8307e12SArchie Cobbs ERROUT(ENXIO); 701cb3c7a5dSArchie Cobbs 702f8307e12SArchie Cobbs /* Listen */ 703b40ce416SJulian Elischer error = solisten(so, *((int32_t *)msg->data), td); 704cb3c7a5dSArchie Cobbs break; 705cb3c7a5dSArchie Cobbs } 706cb3c7a5dSArchie Cobbs 707cb3c7a5dSArchie Cobbs case NGM_KSOCKET_ACCEPT: 708cb3c7a5dSArchie Cobbs { 709f8307e12SArchie Cobbs /* Sanity check */ 710f8307e12SArchie Cobbs if (msg->header.arglen != 0) 711f8307e12SArchie Cobbs ERROUT(EINVAL); 712f8307e12SArchie Cobbs if (so == NULL) 713f8307e12SArchie Cobbs ERROUT(ENXIO); 714f8307e12SArchie Cobbs 715f97e0a07SJulian Elischer /* Make sure the socket is capable of accepting */ 716f97e0a07SJulian Elischer if (!(so->so_options & SO_ACCEPTCONN)) 7174cc20ab1SSeigo Tanimura ERROUT(EINVAL); 718f97e0a07SJulian Elischer if (priv->flags & KSF_ACCEPTING) 719f97e0a07SJulian Elischer ERROUT(EALREADY); 720f8307e12SArchie Cobbs 721f97e0a07SJulian Elischer error = ng_ksocket_check_accept(priv); 722f97e0a07SJulian Elischer if (error != 0 && error != EWOULDBLOCK) 723f97e0a07SJulian Elischer ERROUT(error); 724f8307e12SArchie Cobbs 725f97e0a07SJulian Elischer /* 726f97e0a07SJulian Elischer * If a connection is already complete, take it. 727f97e0a07SJulian Elischer * Otherwise let the upcall function deal with 728f97e0a07SJulian Elischer * the connection when it comes in. 729f97e0a07SJulian Elischer */ 730f97e0a07SJulian Elischer priv->response_token = msg->header.token; 731a7d83226SBenno Rice raddr = priv->response_addr = NGI_RETADDR(item); 732f97e0a07SJulian Elischer if (error == 0) { 733f97e0a07SJulian Elischer ng_ksocket_finish_accept(priv); 734f97e0a07SJulian Elischer } else 735f97e0a07SJulian Elischer priv->flags |= KSF_ACCEPTING; 736cb3c7a5dSArchie Cobbs break; 737cb3c7a5dSArchie Cobbs } 738cb3c7a5dSArchie Cobbs 739cb3c7a5dSArchie Cobbs case NGM_KSOCKET_CONNECT: 740cb3c7a5dSArchie Cobbs { 741f8307e12SArchie Cobbs struct sockaddr *const sa 742f8307e12SArchie Cobbs = (struct sockaddr *)msg->data; 743cb3c7a5dSArchie Cobbs 744f8307e12SArchie Cobbs /* Sanity check */ 745f8307e12SArchie Cobbs if (msg->header.arglen < SADATA_OFFSET 746f8307e12SArchie Cobbs || msg->header.arglen < sa->sa_len) 747f8307e12SArchie Cobbs ERROUT(EINVAL); 748f8307e12SArchie Cobbs if (so == NULL) 749f8307e12SArchie Cobbs ERROUT(ENXIO); 750cb3c7a5dSArchie Cobbs 751cb3c7a5dSArchie Cobbs /* Do connect */ 752cb3c7a5dSArchie Cobbs if ((so->so_state & SS_ISCONNECTING) != 0) 7534cc20ab1SSeigo Tanimura ERROUT(EALREADY); 754b40ce416SJulian Elischer if ((error = soconnect(so, sa, td)) != 0) { 755cb3c7a5dSArchie Cobbs so->so_state &= ~SS_ISCONNECTING; 7564cc20ab1SSeigo Tanimura ERROUT(error); 757cb3c7a5dSArchie Cobbs } 7587d780740SArchie Cobbs if ((so->so_state & SS_ISCONNECTING) != 0) { 759f97e0a07SJulian Elischer /* We will notify the sender when we connect */ 760f97e0a07SJulian Elischer priv->response_token = msg->header.token; 761a7d83226SBenno Rice raddr = priv->response_addr = NGI_RETADDR(item); 762f97e0a07SJulian Elischer priv->flags |= KSF_CONNECTING; 7634cc20ab1SSeigo Tanimura ERROUT(EINPROGRESS); 7647d780740SArchie Cobbs } 765cb3c7a5dSArchie Cobbs break; 766cb3c7a5dSArchie Cobbs } 767cb3c7a5dSArchie Cobbs 768cb3c7a5dSArchie Cobbs case NGM_KSOCKET_GETNAME: 769cb3c7a5dSArchie Cobbs case NGM_KSOCKET_GETPEERNAME: 770cb3c7a5dSArchie Cobbs { 771f8307e12SArchie Cobbs int (*func)(struct socket *so, struct sockaddr **nam); 772f8307e12SArchie Cobbs struct sockaddr *sa = NULL; 773f8307e12SArchie Cobbs int len; 774f8307e12SArchie Cobbs 775f8307e12SArchie Cobbs /* Sanity check */ 776f8307e12SArchie Cobbs if (msg->header.arglen != 0) 777f8307e12SArchie Cobbs ERROUT(EINVAL); 778f8307e12SArchie Cobbs if (so == NULL) 779f8307e12SArchie Cobbs ERROUT(ENXIO); 780f8307e12SArchie Cobbs 781f8307e12SArchie Cobbs /* Get function */ 782f8307e12SArchie Cobbs if (msg->header.cmd == NGM_KSOCKET_GETPEERNAME) { 783f8307e12SArchie Cobbs if ((so->so_state 784f8307e12SArchie Cobbs & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) 7854cc20ab1SSeigo Tanimura ERROUT(ENOTCONN); 786f8307e12SArchie Cobbs func = so->so_proto->pr_usrreqs->pru_peeraddr; 787f8307e12SArchie Cobbs } else 788f8307e12SArchie Cobbs func = so->so_proto->pr_usrreqs->pru_sockaddr; 789f8307e12SArchie Cobbs 790f8307e12SArchie Cobbs /* Get local or peer address */ 791f8307e12SArchie Cobbs if ((error = (*func)(so, &sa)) != 0) 792f8307e12SArchie Cobbs goto bail; 793f8307e12SArchie Cobbs len = (sa == NULL) ? 0 : sa->sa_len; 794f8307e12SArchie Cobbs 795f8307e12SArchie Cobbs /* Send it back in a response */ 796f8307e12SArchie Cobbs NG_MKRESPONSE(resp, msg, len, M_NOWAIT); 797f8307e12SArchie Cobbs if (resp == NULL) { 798f8307e12SArchie Cobbs error = ENOMEM; 799f8307e12SArchie Cobbs goto bail; 800f8307e12SArchie Cobbs } 801f8307e12SArchie Cobbs bcopy(sa, resp->data, len); 802f8307e12SArchie Cobbs 803f8307e12SArchie Cobbs bail: 804f8307e12SArchie Cobbs /* Cleanup */ 805f8307e12SArchie Cobbs if (sa != NULL) 806f8307e12SArchie Cobbs FREE(sa, M_SONAME); 807cb3c7a5dSArchie Cobbs break; 808cb3c7a5dSArchie Cobbs } 809cb3c7a5dSArchie Cobbs 810cb3c7a5dSArchie Cobbs case NGM_KSOCKET_GETOPT: 811cb3c7a5dSArchie Cobbs { 812f8307e12SArchie Cobbs struct ng_ksocket_sockopt *ksopt = 813f8307e12SArchie Cobbs (struct ng_ksocket_sockopt *)msg->data; 814f8307e12SArchie Cobbs struct sockopt sopt; 815f8307e12SArchie Cobbs 816f8307e12SArchie Cobbs /* Sanity check */ 817f8307e12SArchie Cobbs if (msg->header.arglen != sizeof(*ksopt)) 818f8307e12SArchie Cobbs ERROUT(EINVAL); 819f8307e12SArchie Cobbs if (so == NULL) 820f8307e12SArchie Cobbs ERROUT(ENXIO); 821f8307e12SArchie Cobbs 822f8307e12SArchie Cobbs /* Get response with room for option value */ 823f8307e12SArchie Cobbs NG_MKRESPONSE(resp, msg, sizeof(*ksopt) 824f8307e12SArchie Cobbs + NG_KSOCKET_MAX_OPTLEN, M_NOWAIT); 825f8307e12SArchie Cobbs if (resp == NULL) 826f8307e12SArchie Cobbs ERROUT(ENOMEM); 827f8307e12SArchie Cobbs 828f8307e12SArchie Cobbs /* Get socket option, and put value in the response */ 829f8307e12SArchie Cobbs sopt.sopt_dir = SOPT_GET; 830f8307e12SArchie Cobbs sopt.sopt_level = ksopt->level; 831f8307e12SArchie Cobbs sopt.sopt_name = ksopt->name; 832b40ce416SJulian Elischer sopt.sopt_td = NULL; 833f8307e12SArchie Cobbs sopt.sopt_valsize = NG_KSOCKET_MAX_OPTLEN; 834f8307e12SArchie Cobbs ksopt = (struct ng_ksocket_sockopt *)resp->data; 835f8307e12SArchie Cobbs sopt.sopt_val = ksopt->value; 836f8307e12SArchie Cobbs if ((error = sogetopt(so, &sopt)) != 0) { 837069154d5SJulian Elischer NG_FREE_MSG(resp); 838f8307e12SArchie Cobbs break; 839f8307e12SArchie Cobbs } 840f8307e12SArchie Cobbs 841f8307e12SArchie Cobbs /* Set actual value length */ 842f8307e12SArchie Cobbs resp->header.arglen = sizeof(*ksopt) 843f8307e12SArchie Cobbs + sopt.sopt_valsize; 844cb3c7a5dSArchie Cobbs break; 845cb3c7a5dSArchie Cobbs } 846cb3c7a5dSArchie Cobbs 847cb3c7a5dSArchie Cobbs case NGM_KSOCKET_SETOPT: 848cb3c7a5dSArchie Cobbs { 849f8307e12SArchie Cobbs struct ng_ksocket_sockopt *const ksopt = 850f8307e12SArchie Cobbs (struct ng_ksocket_sockopt *)msg->data; 851f8307e12SArchie Cobbs const int valsize = msg->header.arglen - sizeof(*ksopt); 852f8307e12SArchie Cobbs struct sockopt sopt; 853f8307e12SArchie Cobbs 854f8307e12SArchie Cobbs /* Sanity check */ 855f8307e12SArchie Cobbs if (valsize < 0) 856f8307e12SArchie Cobbs ERROUT(EINVAL); 857f8307e12SArchie Cobbs if (so == NULL) 858f8307e12SArchie Cobbs ERROUT(ENXIO); 859f8307e12SArchie Cobbs 860f8307e12SArchie Cobbs /* Set socket option */ 861f8307e12SArchie Cobbs sopt.sopt_dir = SOPT_SET; 862f8307e12SArchie Cobbs sopt.sopt_level = ksopt->level; 863f8307e12SArchie Cobbs sopt.sopt_name = ksopt->name; 864f8307e12SArchie Cobbs sopt.sopt_val = ksopt->value; 865f8307e12SArchie Cobbs sopt.sopt_valsize = valsize; 866b40ce416SJulian Elischer sopt.sopt_td = NULL; 867f8307e12SArchie Cobbs error = sosetopt(so, &sopt); 868cb3c7a5dSArchie Cobbs break; 869cb3c7a5dSArchie Cobbs } 870cb3c7a5dSArchie Cobbs 871cb3c7a5dSArchie Cobbs default: 872cb3c7a5dSArchie Cobbs error = EINVAL; 873cb3c7a5dSArchie Cobbs break; 874cb3c7a5dSArchie Cobbs } 875cb3c7a5dSArchie Cobbs break; 876cb3c7a5dSArchie Cobbs default: 877cb3c7a5dSArchie Cobbs error = EINVAL; 878cb3c7a5dSArchie Cobbs break; 879cb3c7a5dSArchie Cobbs } 880cb3c7a5dSArchie Cobbs done: 881069154d5SJulian Elischer NG_RESPOND_MSG(error, node, item, resp); 882069154d5SJulian Elischer NG_FREE_MSG(msg); 883cb3c7a5dSArchie Cobbs return (error); 884cb3c7a5dSArchie Cobbs } 885cb3c7a5dSArchie Cobbs 886cb3c7a5dSArchie Cobbs /* 887cb3c7a5dSArchie Cobbs * Receive incoming data on our hook. Send it out the socket. 888cb3c7a5dSArchie Cobbs */ 889cb3c7a5dSArchie Cobbs static int 890069154d5SJulian Elischer ng_ksocket_rcvdata(hook_p hook, item_p item) 891cb3c7a5dSArchie Cobbs { 89242ec1da4SRobert Watson struct thread *td = curthread; /* XXX broken */ 89330400f03SJulian Elischer const node_p node = NG_HOOK_NODE(hook); 89430400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 895cb3c7a5dSArchie Cobbs struct socket *const so = priv->so; 89619ff9e5fSArchie Cobbs struct sockaddr *sa = NULL; 897cb3c7a5dSArchie Cobbs int error; 898069154d5SJulian Elischer struct mbuf *m; 899327b288eSJulian Elischer struct sa_tag *stag; 900cb3c7a5dSArchie Cobbs 901dc9c2e01SArchie Cobbs /* Avoid reentrantly sending on the socket */ 902dc9c2e01SArchie Cobbs if ((priv->flags & KSF_SENDING) != 0) { 903dc9c2e01SArchie Cobbs NG_FREE_ITEM(item); 904dc9c2e01SArchie Cobbs return (EDEADLK); 905dc9c2e01SArchie Cobbs } 906dc9c2e01SArchie Cobbs 907327b288eSJulian Elischer /* Extract data */ 908069154d5SJulian Elischer NGI_GET_M(item, m); 909069154d5SJulian Elischer NG_FREE_ITEM(item); 91019ff9e5fSArchie Cobbs 911327b288eSJulian Elischer /* Look if socket address is stored in packet tags */ 912327b288eSJulian Elischer if ((stag = (struct sa_tag *)m_tag_locate(m, NGM_KSOCKET_COOKIE, 913327b288eSJulian Elischer NG_KSOCKET_TAG_SOCKADDR, NULL)) != NULL) 914327b288eSJulian Elischer sa = &stag->sa; 91519ff9e5fSArchie Cobbs 91619ff9e5fSArchie Cobbs /* Send packet */ 917dc9c2e01SArchie Cobbs priv->flags |= KSF_SENDING; 91819ff9e5fSArchie Cobbs error = (*so->so_proto->pr_usrreqs->pru_sosend)(so, sa, 0, m, 0, 0, td); 919dc9c2e01SArchie Cobbs priv->flags &= ~KSF_SENDING; 92019ff9e5fSArchie Cobbs 921cb3c7a5dSArchie Cobbs return (error); 922cb3c7a5dSArchie Cobbs } 923cb3c7a5dSArchie Cobbs 924cb3c7a5dSArchie Cobbs /* 925cb3c7a5dSArchie Cobbs * Destroy node 926cb3c7a5dSArchie Cobbs */ 927cb3c7a5dSArchie Cobbs static int 928069154d5SJulian Elischer ng_ksocket_shutdown(node_p node) 929cb3c7a5dSArchie Cobbs { 93030400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 931f97e0a07SJulian Elischer priv_p embryo; 932cb3c7a5dSArchie Cobbs 93319bff684SArchie Cobbs /* Close our socket (if any) */ 93419bff684SArchie Cobbs if (priv->so != NULL) { 935f8307e12SArchie Cobbs priv->so->so_upcall = NULL; 9369535efc0SRobert Watson SOCKBUF_LOCK(&priv->so->so_rcv); 937f8307e12SArchie Cobbs priv->so->so_rcv.sb_flags &= ~SB_UPCALL; 9389535efc0SRobert Watson SOCKBUF_UNLOCK(&priv->so->so_rcv); 9399535efc0SRobert Watson SOCKBUF_LOCK(&priv->so->so_snd); 940f97e0a07SJulian Elischer priv->so->so_snd.sb_flags &= ~SB_UPCALL; 9419535efc0SRobert Watson SOCKBUF_UNLOCK(&priv->so->so_snd); 94219bff684SArchie Cobbs soclose(priv->so); 94319bff684SArchie Cobbs priv->so = NULL; 94419bff684SArchie Cobbs } 94519bff684SArchie Cobbs 946f97e0a07SJulian Elischer /* If we are an embryo, take ourselves out of the parent's list */ 947f97e0a07SJulian Elischer if (priv->flags & KSF_EMBRYONIC) { 948f97e0a07SJulian Elischer LIST_REMOVE(priv, siblings); 949f97e0a07SJulian Elischer priv->flags &= ~KSF_EMBRYONIC; 950f97e0a07SJulian Elischer } 951f97e0a07SJulian Elischer 952f97e0a07SJulian Elischer /* Remove any embryonic children we have */ 953f97e0a07SJulian Elischer while (!LIST_EMPTY(&priv->embryos)) { 954f97e0a07SJulian Elischer embryo = LIST_FIRST(&priv->embryos); 955f97e0a07SJulian Elischer ng_rmnode_self(embryo->node); 956f97e0a07SJulian Elischer } 957f97e0a07SJulian Elischer 958cb3c7a5dSArchie Cobbs /* Take down netgraph node */ 959cb3c7a5dSArchie Cobbs bzero(priv, sizeof(*priv)); 9609c8c302fSJulian Elischer FREE(priv, M_NETGRAPH_KSOCKET); 96130400f03SJulian Elischer NG_NODE_SET_PRIVATE(node, NULL); 96230400f03SJulian Elischer NG_NODE_UNREF(node); /* let the node escape */ 963cb3c7a5dSArchie Cobbs return (0); 964cb3c7a5dSArchie Cobbs } 965cb3c7a5dSArchie Cobbs 966cb3c7a5dSArchie Cobbs /* 967cb3c7a5dSArchie Cobbs * Hook disconnection 968cb3c7a5dSArchie Cobbs */ 969cb3c7a5dSArchie Cobbs static int 970cb3c7a5dSArchie Cobbs ng_ksocket_disconnect(hook_p hook) 971cb3c7a5dSArchie Cobbs { 97230400f03SJulian Elischer KASSERT(NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0, 9736e551fb6SDavid E. O'Brien ("%s: numhooks=%d?", __func__, 97493caaaa7SArchie Cobbs NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)))); 97530400f03SJulian Elischer if (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))) 97630400f03SJulian Elischer ng_rmnode_self(NG_HOOK_NODE(hook)); 977cb3c7a5dSArchie Cobbs return (0); 978cb3c7a5dSArchie Cobbs } 979cb3c7a5dSArchie Cobbs 980cb3c7a5dSArchie Cobbs /************************************************************************ 981cb3c7a5dSArchie Cobbs HELPER STUFF 982cb3c7a5dSArchie Cobbs ************************************************************************/ 983cb3c7a5dSArchie Cobbs /* 984f97e0a07SJulian Elischer * You should no-longer "just call" a netgraph node function 985f97e0a07SJulian Elischer * from an external asynchronous event. 986f97e0a07SJulian Elischer * This is because in doing so you are ignoring the locking on the netgraph 987f97e0a07SJulian Elischer * nodes. Instead call your function via 988f97e0a07SJulian Elischer * "int ng_send_fn(node_p node, hook_p hook, ng_item_fn *fn, 989f97e0a07SJulian Elischer * void *arg1, int arg2);" 990f97e0a07SJulian Elischer * this will call the function you chose, but will first do all the 991f97e0a07SJulian Elischer * locking rigmarole. Your function MAY only be called at some distant future 992f97e0a07SJulian Elischer * time (several millisecs away) so don't give it any arguments 993f97e0a07SJulian Elischer * that may be revoked soon (e.g. on your stack). 994f97e0a07SJulian Elischer * In this case even the 'so' argument is doubtful. 995f97e0a07SJulian Elischer * While the function request is being processed the node 996f97e0a07SJulian Elischer * has an extra reference and as such will not disappear until 997f97e0a07SJulian Elischer * the request has at least been done, but the 'so' may not be so lucky. 998f97e0a07SJulian Elischer * handle this by checking the validity of the node in the target function 999f97e0a07SJulian Elischer * before dereferencing the socket pointer. 1000cb3c7a5dSArchie Cobbs */ 1001f97e0a07SJulian Elischer 1002cb3c7a5dSArchie Cobbs static void 1003cb3c7a5dSArchie Cobbs ng_ksocket_incoming(struct socket *so, void *arg, int waitflag) 1004cb3c7a5dSArchie Cobbs { 1005cb3c7a5dSArchie Cobbs const node_p node = arg; 1006f97e0a07SJulian Elischer 1007f97e0a07SJulian Elischer ng_send_fn(node, NULL, &ng_ksocket_incoming2, so, waitflag); 1008f97e0a07SJulian Elischer } 1009f97e0a07SJulian Elischer 1010f97e0a07SJulian Elischer 1011f97e0a07SJulian Elischer /* 1012f97e0a07SJulian Elischer * When incoming data is appended to the socket, we get notified here. 1013f97e0a07SJulian Elischer * This is also called whenever a significant event occurs for the socket. 1014f97e0a07SJulian Elischer * We know that HOOK is NULL. Because of how we were called we know we have a 1015f97e0a07SJulian Elischer * lock on this node an are participating inthe netgraph locking. 1016f97e0a07SJulian Elischer * Our original caller may have queued this even some time ago and 1017f97e0a07SJulian Elischer * we cannot trust that he even still exists. The node however is being 1018f97e0a07SJulian Elischer * held with a reference by the queueing code, at least until we finish, 1019f97e0a07SJulian Elischer * even if it has been zapped, so first check it's validiy 1020f97e0a07SJulian Elischer * before we trust the socket (which was derived from it). 1021f97e0a07SJulian Elischer */ 1022f97e0a07SJulian Elischer static void 1023f97e0a07SJulian Elischer ng_ksocket_incoming2(node_p node, hook_p hook, void *arg1, int waitflag) 1024f97e0a07SJulian Elischer { 1025f97e0a07SJulian Elischer struct socket *so = arg1; 102630400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 1027cb3c7a5dSArchie Cobbs struct mbuf *m; 1028f97e0a07SJulian Elischer struct ng_mesg *response; 1029cb3c7a5dSArchie Cobbs struct uio auio; 103019bff684SArchie Cobbs int s, flags, error; 103119bff684SArchie Cobbs 103219bff684SArchie Cobbs s = splnet(); 1033cb3c7a5dSArchie Cobbs 1034cb3c7a5dSArchie Cobbs /* Sanity check */ 103530400f03SJulian Elischer if (NG_NODE_NOT_VALID(node)) { 103619bff684SArchie Cobbs splx(s); 1037cb3c7a5dSArchie Cobbs return; 103819bff684SArchie Cobbs } 1039f97e0a07SJulian Elischer /* so = priv->so; *//* XXX could have derived this like so */ 10406e551fb6SDavid E. O'Brien KASSERT(so == priv->so, ("%s: wrong socket", __func__)); 1041f97e0a07SJulian Elischer 1042f97e0a07SJulian Elischer /* Check whether a pending connect operation has completed */ 1043f97e0a07SJulian Elischer if (priv->flags & KSF_CONNECTING) { 1044f97e0a07SJulian Elischer if ((error = so->so_error) != 0) { 1045f97e0a07SJulian Elischer so->so_error = 0; 1046f97e0a07SJulian Elischer so->so_state &= ~SS_ISCONNECTING; 1047f97e0a07SJulian Elischer } 1048f97e0a07SJulian Elischer if (!(so->so_state & SS_ISCONNECTING)) { 1049f97e0a07SJulian Elischer NG_MKMESSAGE(response, NGM_KSOCKET_COOKIE, 1050f97e0a07SJulian Elischer NGM_KSOCKET_CONNECT, sizeof(int32_t), waitflag); 1051f97e0a07SJulian Elischer if (response != NULL) { 1052f97e0a07SJulian Elischer response->header.flags |= NGF_RESP; 1053f97e0a07SJulian Elischer response->header.token = priv->response_token; 1054f97e0a07SJulian Elischer *(int32_t *)response->data = error; 1055f97e0a07SJulian Elischer /* 1056f97e0a07SJulian Elischer * send an async "response" message 1057f97e0a07SJulian Elischer * to the node that set us up 1058f97e0a07SJulian Elischer * (if it still exists) 1059f97e0a07SJulian Elischer */ 1060facfd889SArchie Cobbs NG_SEND_MSG_ID(error, node, 1061facfd889SArchie Cobbs response, priv->response_addr, 0); 1062f97e0a07SJulian Elischer } 1063f97e0a07SJulian Elischer priv->flags &= ~KSF_CONNECTING; 10644cc20ab1SSeigo Tanimura } 1065f97e0a07SJulian Elischer } 1066f97e0a07SJulian Elischer 1067f97e0a07SJulian Elischer /* Check whether a pending accept operation has completed */ 1068f97e0a07SJulian Elischer if (priv->flags & KSF_ACCEPTING) { 1069f97e0a07SJulian Elischer error = ng_ksocket_check_accept(priv); 1070f97e0a07SJulian Elischer if (error != EWOULDBLOCK) 1071f97e0a07SJulian Elischer priv->flags &= ~KSF_ACCEPTING; 1072f97e0a07SJulian Elischer if (error == 0) 1073f97e0a07SJulian Elischer ng_ksocket_finish_accept(priv); 1074f97e0a07SJulian Elischer } 1075f97e0a07SJulian Elischer 1076f97e0a07SJulian Elischer /* 1077f97e0a07SJulian Elischer * If we don't have a hook, we must handle data events later. When 1078f97e0a07SJulian Elischer * the hook gets created and is connected, this upcall function 1079f97e0a07SJulian Elischer * will be called again. 1080f97e0a07SJulian Elischer */ 1081f97e0a07SJulian Elischer if (priv->hook == NULL) { 1082f97e0a07SJulian Elischer splx(s); 1083f97e0a07SJulian Elischer return; 1084f97e0a07SJulian Elischer } 1085cb3c7a5dSArchie Cobbs 1086cb3c7a5dSArchie Cobbs /* Read and forward available mbuf's */ 1087b40ce416SJulian Elischer auio.uio_td = NULL; 1088cb3c7a5dSArchie Cobbs auio.uio_resid = 1000000000; 1089cb3c7a5dSArchie Cobbs flags = MSG_DONTWAIT; 109019ff9e5fSArchie Cobbs while (1) { 109119ff9e5fSArchie Cobbs struct sockaddr *sa = NULL; 1092f8307e12SArchie Cobbs struct mbuf *n; 1093f8307e12SArchie Cobbs 109419ff9e5fSArchie Cobbs /* Try to get next packet from socket */ 109519ff9e5fSArchie Cobbs if ((error = (*so->so_proto->pr_usrreqs->pru_soreceive) 10964cc20ab1SSeigo Tanimura (so, (so->so_state & SS_ISCONNECTED) ? NULL : &sa, 109719ff9e5fSArchie Cobbs &auio, &m, (struct mbuf **)0, &flags)) != 0) 109819ff9e5fSArchie Cobbs break; 109919ff9e5fSArchie Cobbs 110019ff9e5fSArchie Cobbs /* See if we got anything */ 110119ff9e5fSArchie Cobbs if (m == NULL) { 110219ff9e5fSArchie Cobbs if (sa != NULL) 110319ff9e5fSArchie Cobbs FREE(sa, M_SONAME); 110419ff9e5fSArchie Cobbs break; 110519ff9e5fSArchie Cobbs } 110619ff9e5fSArchie Cobbs 1107f8307e12SArchie Cobbs /* Don't trust the various socket layers to get the 1108f8307e12SArchie Cobbs packet header and length correct (eg. kern/15175) */ 110919ff9e5fSArchie Cobbs for (n = m, m->m_pkthdr.len = 0; n != NULL; n = n->m_next) 1110f8307e12SArchie Cobbs m->m_pkthdr.len += n->m_len; 111119ff9e5fSArchie Cobbs 1112327b288eSJulian Elischer /* Put peer's socket address (if any) into a tag */ 111319ff9e5fSArchie Cobbs if (sa != NULL) { 1114327b288eSJulian Elischer struct sa_tag *stag; 111519ff9e5fSArchie Cobbs 1116327b288eSJulian Elischer stag = (struct sa_tag *)m_tag_alloc(NGM_KSOCKET_COOKIE, 1117327b288eSJulian Elischer NG_KSOCKET_TAG_SOCKADDR, sa->sa_len, M_NOWAIT); 1118327b288eSJulian Elischer if (stag == NULL) { 111919ff9e5fSArchie Cobbs FREE(sa, M_SONAME); 112019ff9e5fSArchie Cobbs goto sendit; 1121f8307e12SArchie Cobbs } 1122327b288eSJulian Elischer bcopy(sa, &stag->sa, sa->sa_len); 112319ff9e5fSArchie Cobbs FREE(sa, M_SONAME); 1124327b288eSJulian Elischer m_tag_prepend(m, &stag->tag); 112519ff9e5fSArchie Cobbs } 112619ff9e5fSArchie Cobbs 1127327b288eSJulian Elischer sendit: /* Forward data with optional peer sockaddr as packet tag */ 1128327b288eSJulian Elischer NG_SEND_DATA_ONLY(error, priv->hook, m); 112919ff9e5fSArchie Cobbs } 1130f97e0a07SJulian Elischer 1131f97e0a07SJulian Elischer /* 1132f97e0a07SJulian Elischer * If the peer has closed the connection, forward a 0-length mbuf 1133f97e0a07SJulian Elischer * to indicate end-of-file. 1134f97e0a07SJulian Elischer */ 1135c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE && !(priv->flags & KSF_EOFSEEN)) { 1136f97e0a07SJulian Elischer MGETHDR(m, waitflag, MT_DATA); 1137f97e0a07SJulian Elischer if (m != NULL) { 1138f97e0a07SJulian Elischer m->m_len = m->m_pkthdr.len = 0; 1139f97e0a07SJulian Elischer NG_SEND_DATA_ONLY(error, priv->hook, m); 1140f97e0a07SJulian Elischer } 1141f97e0a07SJulian Elischer priv->flags |= KSF_EOFSEEN; 11424cc20ab1SSeigo Tanimura } 114319bff684SArchie Cobbs splx(s); 1144cb3c7a5dSArchie Cobbs } 1145cb3c7a5dSArchie Cobbs 1146cb3c7a5dSArchie Cobbs /* 1147f97e0a07SJulian Elischer * Check for a completed incoming connection and return 0 if one is found. 1148f97e0a07SJulian Elischer * Otherwise return the appropriate error code. 1149f97e0a07SJulian Elischer */ 1150f97e0a07SJulian Elischer static int 1151f97e0a07SJulian Elischer ng_ksocket_check_accept(priv_p priv) 1152f97e0a07SJulian Elischer { 1153f97e0a07SJulian Elischer struct socket *const head = priv->so; 1154f97e0a07SJulian Elischer int error; 1155f97e0a07SJulian Elischer 1156f97e0a07SJulian Elischer if ((error = head->so_error) != 0) { 1157f97e0a07SJulian Elischer head->so_error = 0; 1158f97e0a07SJulian Elischer return error; 1159f97e0a07SJulian Elischer } 11602658b3bbSRobert Watson /* Unlocked read. */ 1161f97e0a07SJulian Elischer if (TAILQ_EMPTY(&head->so_comp)) { 1162c0b99ffaSRobert Watson if (head->so_rcv.sb_state & SBS_CANTRCVMORE) 1163f97e0a07SJulian Elischer return ECONNABORTED; 1164f97e0a07SJulian Elischer return EWOULDBLOCK; 1165f97e0a07SJulian Elischer } 1166f97e0a07SJulian Elischer return 0; 1167f97e0a07SJulian Elischer } 1168f97e0a07SJulian Elischer 1169f97e0a07SJulian Elischer /* 1170f97e0a07SJulian Elischer * Handle the first completed incoming connection, assumed to be already 1171f97e0a07SJulian Elischer * on the socket's so_comp queue. 1172f97e0a07SJulian Elischer */ 1173f97e0a07SJulian Elischer static void 1174f97e0a07SJulian Elischer ng_ksocket_finish_accept(priv_p priv) 1175f97e0a07SJulian Elischer { 1176f97e0a07SJulian Elischer struct socket *const head = priv->so; 1177f97e0a07SJulian Elischer struct socket *so; 1178f97e0a07SJulian Elischer struct sockaddr *sa = NULL; 1179f97e0a07SJulian Elischer struct ng_mesg *resp; 1180f97e0a07SJulian Elischer struct ng_ksocket_accept *resp_data; 1181f97e0a07SJulian Elischer node_p node; 1182f97e0a07SJulian Elischer priv_p priv2; 1183f97e0a07SJulian Elischer int len; 1184f97e0a07SJulian Elischer int error; 1185f97e0a07SJulian Elischer 11862658b3bbSRobert Watson ACCEPT_LOCK(); 1187f97e0a07SJulian Elischer so = TAILQ_FIRST(&head->so_comp); 11882658b3bbSRobert Watson if (so == NULL) { /* Should never happen */ 11892658b3bbSRobert Watson ACCEPT_UNLOCK(); 1190f97e0a07SJulian Elischer return; 11912658b3bbSRobert Watson } 1192f97e0a07SJulian Elischer TAILQ_REMOVE(&head->so_comp, so, so_list); 1193f97e0a07SJulian Elischer head->so_qlen--; 11942658b3bbSRobert Watson so->so_qstate &= ~SQ_COMP; 11952658b3bbSRobert Watson so->so_head = NULL; 1196395a08c9SRobert Watson SOCK_LOCK(so); 11972658b3bbSRobert Watson soref(so); 11982658b3bbSRobert Watson so->so_state |= SS_NBIO; 1199395a08c9SRobert Watson SOCK_UNLOCK(so); 12002658b3bbSRobert Watson ACCEPT_UNLOCK(); 1201f97e0a07SJulian Elischer 1202f97e0a07SJulian Elischer /* XXX KNOTE(&head->so_rcv.sb_sel.si_note, 0); */ 1203f97e0a07SJulian Elischer 1204f97e0a07SJulian Elischer soaccept(so, &sa); 1205f97e0a07SJulian Elischer 1206f97e0a07SJulian Elischer len = OFFSETOF(struct ng_ksocket_accept, addr); 1207f97e0a07SJulian Elischer if (sa != NULL) 1208f97e0a07SJulian Elischer len += sa->sa_len; 1209f97e0a07SJulian Elischer 1210f97e0a07SJulian Elischer NG_MKMESSAGE(resp, NGM_KSOCKET_COOKIE, NGM_KSOCKET_ACCEPT, len, 1211f97e0a07SJulian Elischer M_NOWAIT); 1212f97e0a07SJulian Elischer if (resp == NULL) { 1213f97e0a07SJulian Elischer soclose(so); 1214f97e0a07SJulian Elischer goto out; 1215f97e0a07SJulian Elischer } 1216f97e0a07SJulian Elischer resp->header.flags |= NGF_RESP; 1217f97e0a07SJulian Elischer resp->header.token = priv->response_token; 1218f97e0a07SJulian Elischer 1219f97e0a07SJulian Elischer /* Clone a ksocket node to wrap the new socket */ 1220f97e0a07SJulian Elischer error = ng_make_node_common(&ng_ksocket_typestruct, &node); 1221f97e0a07SJulian Elischer if (error) { 1222f97e0a07SJulian Elischer FREE(resp, M_NETGRAPH); 1223f97e0a07SJulian Elischer soclose(so); 1224f97e0a07SJulian Elischer goto out; 1225f97e0a07SJulian Elischer } 1226f97e0a07SJulian Elischer 1227f97e0a07SJulian Elischer if (ng_ksocket_constructor(node) != 0) { 1228f97e0a07SJulian Elischer NG_NODE_UNREF(node); 1229f97e0a07SJulian Elischer FREE(resp, M_NETGRAPH); 1230f97e0a07SJulian Elischer soclose(so); 1231f97e0a07SJulian Elischer goto out; 1232f97e0a07SJulian Elischer } 1233f97e0a07SJulian Elischer 1234f97e0a07SJulian Elischer priv2 = NG_NODE_PRIVATE(node); 1235f97e0a07SJulian Elischer priv2->so = so; 1236f97e0a07SJulian Elischer priv2->flags |= KSF_CLONED | KSF_EMBRYONIC; 1237f97e0a07SJulian Elischer 1238f97e0a07SJulian Elischer /* 1239f97e0a07SJulian Elischer * Insert the cloned node into a list of embryonic children 1240f97e0a07SJulian Elischer * on the parent node. When a hook is created on the cloned 1241f97e0a07SJulian Elischer * node it will be removed from this list. When the parent 1242f97e0a07SJulian Elischer * is destroyed it will destroy any embryonic children it has. 1243f97e0a07SJulian Elischer */ 1244f97e0a07SJulian Elischer LIST_INSERT_HEAD(&priv->embryos, priv2, siblings); 1245f97e0a07SJulian Elischer 1246f97e0a07SJulian Elischer so->so_upcallarg = (caddr_t)node; 1247f97e0a07SJulian Elischer so->so_upcall = ng_ksocket_incoming; 12489535efc0SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1249f97e0a07SJulian Elischer so->so_rcv.sb_flags |= SB_UPCALL; 12509535efc0SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 12519535efc0SRobert Watson SOCKBUF_LOCK(&so->so_snd); 1252f97e0a07SJulian Elischer so->so_snd.sb_flags |= SB_UPCALL; 12539535efc0SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 1254f97e0a07SJulian Elischer 1255f97e0a07SJulian Elischer /* Fill in the response data and send it or return it to the caller */ 1256f97e0a07SJulian Elischer resp_data = (struct ng_ksocket_accept *)resp->data; 1257f97e0a07SJulian Elischer resp_data->nodeid = NG_NODE_ID(node); 1258f97e0a07SJulian Elischer if (sa != NULL) 1259f97e0a07SJulian Elischer bcopy(sa, &resp_data->addr, sa->sa_len); 1260facfd889SArchie Cobbs NG_SEND_MSG_ID(error, node, resp, priv->response_addr, 0); 1261f97e0a07SJulian Elischer 1262f97e0a07SJulian Elischer out: 1263f97e0a07SJulian Elischer if (sa != NULL) 1264f97e0a07SJulian Elischer FREE(sa, M_SONAME); 1265f97e0a07SJulian Elischer } 1266f97e0a07SJulian Elischer 1267f97e0a07SJulian Elischer /* 1268cb3c7a5dSArchie Cobbs * Parse out either an integer value or an alias. 1269cb3c7a5dSArchie Cobbs */ 1270cb3c7a5dSArchie Cobbs static int 1271cb3c7a5dSArchie Cobbs ng_ksocket_parse(const struct ng_ksocket_alias *aliases, 1272cb3c7a5dSArchie Cobbs const char *s, int family) 1273cb3c7a5dSArchie Cobbs { 1274cb3c7a5dSArchie Cobbs int k, val; 127525792ef3SArchie Cobbs char *eptr; 1276cb3c7a5dSArchie Cobbs 1277cb3c7a5dSArchie Cobbs /* Try aliases */ 1278cb3c7a5dSArchie Cobbs for (k = 0; aliases[k].name != NULL; k++) { 1279cb3c7a5dSArchie Cobbs if (strcmp(s, aliases[k].name) == 0 1280cb3c7a5dSArchie Cobbs && aliases[k].family == family) 1281cb3c7a5dSArchie Cobbs return aliases[k].value; 1282cb3c7a5dSArchie Cobbs } 1283cb3c7a5dSArchie Cobbs 1284cb3c7a5dSArchie Cobbs /* Try parsing as a number */ 1285cb3c7a5dSArchie Cobbs val = (int)strtoul(s, &eptr, 10); 1286f8307e12SArchie Cobbs if (val < 0 || *eptr != '\0') 1287cb3c7a5dSArchie Cobbs return (-1); 1288cb3c7a5dSArchie Cobbs return (val); 1289cb3c7a5dSArchie Cobbs } 1290cb3c7a5dSArchie Cobbs 1291