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 { 81cb3c7a5dSArchie Cobbs hook_p hook; 82cb3c7a5dSArchie Cobbs struct socket *so; 83cb3c7a5dSArchie Cobbs }; 8419bff684SArchie Cobbs typedef struct ng_ksocket_private *priv_p; 85cb3c7a5dSArchie Cobbs 86cb3c7a5dSArchie Cobbs /* Netgraph node methods */ 87cb3c7a5dSArchie Cobbs static ng_constructor_t ng_ksocket_constructor; 88cb3c7a5dSArchie Cobbs static ng_rcvmsg_t ng_ksocket_rcvmsg; 89069154d5SJulian Elischer static ng_shutdown_t ng_ksocket_shutdown; 90cb3c7a5dSArchie Cobbs static ng_newhook_t ng_ksocket_newhook; 91cb3c7a5dSArchie Cobbs static ng_rcvdata_t ng_ksocket_rcvdata; 92cb3c7a5dSArchie Cobbs static ng_disconnect_t ng_ksocket_disconnect; 93cb3c7a5dSArchie Cobbs 94cb3c7a5dSArchie Cobbs /* Alias structure */ 95cb3c7a5dSArchie Cobbs struct ng_ksocket_alias { 96cb3c7a5dSArchie Cobbs const char *name; 97cb3c7a5dSArchie Cobbs const int value; 98cb3c7a5dSArchie Cobbs const int family; 99cb3c7a5dSArchie Cobbs }; 100cb3c7a5dSArchie Cobbs 101cb3c7a5dSArchie Cobbs /* Protocol family aliases */ 102cb3c7a5dSArchie Cobbs static const struct ng_ksocket_alias ng_ksocket_families[] = { 103cb3c7a5dSArchie Cobbs { "local", PF_LOCAL }, 104cb3c7a5dSArchie Cobbs { "inet", PF_INET }, 105cb3c7a5dSArchie Cobbs { "inet6", PF_INET6 }, 106cb3c7a5dSArchie Cobbs { "atalk", PF_APPLETALK }, 107cb3c7a5dSArchie Cobbs { "ipx", PF_IPX }, 108cb3c7a5dSArchie Cobbs { "atm", PF_ATM }, 109cb3c7a5dSArchie Cobbs { NULL, -1 }, 110cb3c7a5dSArchie Cobbs }; 111cb3c7a5dSArchie Cobbs 112cb3c7a5dSArchie Cobbs /* Socket type aliases */ 113cb3c7a5dSArchie Cobbs static const struct ng_ksocket_alias ng_ksocket_types[] = { 114cb3c7a5dSArchie Cobbs { "stream", SOCK_STREAM }, 115cb3c7a5dSArchie Cobbs { "dgram", SOCK_DGRAM }, 116cb3c7a5dSArchie Cobbs { "raw", SOCK_RAW }, 117cb3c7a5dSArchie Cobbs { "rdm", SOCK_RDM }, 118cb3c7a5dSArchie Cobbs { "seqpacket", SOCK_SEQPACKET }, 119cb3c7a5dSArchie Cobbs { NULL, -1 }, 120cb3c7a5dSArchie Cobbs }; 121cb3c7a5dSArchie Cobbs 122cb3c7a5dSArchie Cobbs /* Protocol aliases */ 123cb3c7a5dSArchie Cobbs static const struct ng_ksocket_alias ng_ksocket_protos[] = { 124cb3c7a5dSArchie Cobbs { "ip", IPPROTO_IP, PF_INET }, 125cb3c7a5dSArchie Cobbs { "raw", IPPROTO_IP, PF_INET }, 126cb3c7a5dSArchie Cobbs { "icmp", IPPROTO_ICMP, PF_INET }, 127cb3c7a5dSArchie Cobbs { "igmp", IPPROTO_IGMP, PF_INET }, 128cb3c7a5dSArchie Cobbs { "tcp", IPPROTO_TCP, PF_INET }, 129cb3c7a5dSArchie Cobbs { "udp", IPPROTO_UDP, PF_INET }, 130cb3c7a5dSArchie Cobbs { "gre", IPPROTO_GRE, PF_INET }, 131cb3c7a5dSArchie Cobbs { "esp", IPPROTO_ESP, PF_INET }, 132cb3c7a5dSArchie Cobbs { "ah", IPPROTO_AH, PF_INET }, 133cb3c7a5dSArchie Cobbs { "swipe", IPPROTO_SWIPE, PF_INET }, 134cb3c7a5dSArchie Cobbs { "encap", IPPROTO_ENCAP, PF_INET }, 135cb3c7a5dSArchie Cobbs { "divert", IPPROTO_DIVERT, PF_INET }, 136cb3c7a5dSArchie Cobbs { "ddp", ATPROTO_DDP, PF_APPLETALK }, 137cb3c7a5dSArchie Cobbs { "aarp", ATPROTO_AARP, PF_APPLETALK }, 138cb3c7a5dSArchie Cobbs { NULL, -1 }, 139cb3c7a5dSArchie Cobbs }; 140cb3c7a5dSArchie Cobbs 141f8307e12SArchie Cobbs /* Helper functions */ 142f8307e12SArchie Cobbs static void ng_ksocket_incoming(struct socket *so, void *arg, int waitflag); 143f8307e12SArchie Cobbs static int ng_ksocket_parse(const struct ng_ksocket_alias *aliases, 144f8307e12SArchie Cobbs const char *s, int family); 145f8307e12SArchie Cobbs 146f8307e12SArchie Cobbs /************************************************************************ 147f8307e12SArchie Cobbs STRUCT SOCKADDR PARSE TYPE 148f8307e12SArchie Cobbs ************************************************************************/ 149f8307e12SArchie Cobbs 150f8307e12SArchie Cobbs /* Get the length of the data portion of a generic struct sockaddr */ 151f8307e12SArchie Cobbs static int 152f8307e12SArchie Cobbs ng_parse_generic_sockdata_getLength(const struct ng_parse_type *type, 153f8307e12SArchie Cobbs const u_char *start, const u_char *buf) 154f8307e12SArchie Cobbs { 155f8307e12SArchie Cobbs const struct sockaddr *sa; 156f8307e12SArchie Cobbs 157f8307e12SArchie Cobbs sa = (const struct sockaddr *)(buf - SADATA_OFFSET); 1581baeddb8SArchie Cobbs return (sa->sa_len < SADATA_OFFSET) ? 0 : sa->sa_len - SADATA_OFFSET; 159f8307e12SArchie Cobbs } 160f8307e12SArchie Cobbs 161f8307e12SArchie Cobbs /* Type for the variable length data portion of a generic struct sockaddr */ 162f8307e12SArchie Cobbs static const struct ng_parse_type ng_ksocket_generic_sockdata_type = { 163f8307e12SArchie Cobbs &ng_parse_bytearray_type, 164f8307e12SArchie Cobbs &ng_parse_generic_sockdata_getLength 165f8307e12SArchie Cobbs }; 166f8307e12SArchie Cobbs 167f8307e12SArchie Cobbs /* Type for a generic struct sockaddr */ 168f8307e12SArchie Cobbs static const struct ng_parse_struct_info ng_parse_generic_sockaddr_type_info = { 169f8307e12SArchie Cobbs { 17057b57be3SArchie Cobbs { "len", &ng_parse_uint8_type }, 17157b57be3SArchie Cobbs { "family", &ng_parse_uint8_type }, 172f8307e12SArchie Cobbs { "data", &ng_ksocket_generic_sockdata_type }, 173f8307e12SArchie Cobbs { NULL } 174f8307e12SArchie Cobbs } 175f8307e12SArchie Cobbs }; 176f8307e12SArchie Cobbs static const struct ng_parse_type ng_ksocket_generic_sockaddr_type = { 177f8307e12SArchie Cobbs &ng_parse_struct_type, 178f8307e12SArchie Cobbs &ng_parse_generic_sockaddr_type_info 179f8307e12SArchie Cobbs }; 180f8307e12SArchie Cobbs 181f8307e12SArchie Cobbs /* Convert a struct sockaddr from ASCII to binary. If its a protocol 182f8307e12SArchie Cobbs family that we specially handle, do that, otherwise defer to the 183f8307e12SArchie Cobbs generic parse type ng_ksocket_generic_sockaddr_type. */ 184f8307e12SArchie Cobbs static int 185f8307e12SArchie Cobbs ng_ksocket_sockaddr_parse(const struct ng_parse_type *type, 186f8307e12SArchie Cobbs const char *s, int *off, const u_char *const start, 187f8307e12SArchie Cobbs u_char *const buf, int *buflen) 188f8307e12SArchie Cobbs { 189f8307e12SArchie Cobbs struct sockaddr *const sa = (struct sockaddr *)buf; 190f8307e12SArchie Cobbs enum ng_parse_token tok; 191f8307e12SArchie Cobbs char fambuf[32]; 192f8307e12SArchie Cobbs int family, len; 193f8307e12SArchie Cobbs char *t; 194f8307e12SArchie Cobbs 195f8307e12SArchie Cobbs /* If next token is a left curly brace, use generic parse type */ 196f8307e12SArchie Cobbs if ((tok = ng_parse_get_token(s, off, &len)) == T_LBRACE) { 197f8307e12SArchie Cobbs return (*ng_ksocket_generic_sockaddr_type.supertype->parse) 198f8307e12SArchie Cobbs (&ng_ksocket_generic_sockaddr_type, 199f8307e12SArchie Cobbs s, off, start, buf, buflen); 200f8307e12SArchie Cobbs } 201f8307e12SArchie Cobbs 202f8307e12SArchie Cobbs /* Get socket address family followed by a slash */ 203f8307e12SArchie Cobbs while (isspace(s[*off])) 204f8307e12SArchie Cobbs (*off)++; 205f8307e12SArchie Cobbs if ((t = index(s + *off, '/')) == NULL) 206f8307e12SArchie Cobbs return (EINVAL); 207f8307e12SArchie Cobbs if ((len = t - (s + *off)) > sizeof(fambuf) - 1) 208f8307e12SArchie Cobbs return (EINVAL); 209f8307e12SArchie Cobbs strncpy(fambuf, s + *off, len); 210f8307e12SArchie Cobbs fambuf[len] = '\0'; 211f8307e12SArchie Cobbs *off += len + 1; 212f8307e12SArchie Cobbs if ((family = ng_ksocket_parse(ng_ksocket_families, fambuf, 0)) == -1) 213f8307e12SArchie Cobbs return (EINVAL); 214f8307e12SArchie Cobbs 215f8307e12SArchie Cobbs /* Set family */ 216f8307e12SArchie Cobbs if (*buflen < SADATA_OFFSET) 217f8307e12SArchie Cobbs return (ERANGE); 218f8307e12SArchie Cobbs sa->sa_family = family; 219f8307e12SArchie Cobbs 220f8307e12SArchie Cobbs /* Set family-specific data and length */ 221f8307e12SArchie Cobbs switch (sa->sa_family) { 222f8307e12SArchie Cobbs case PF_LOCAL: /* Get pathname */ 223f8307e12SArchie Cobbs { 224f8307e12SArchie Cobbs const int pathoff = OFFSETOF(struct sockaddr_un, sun_path); 225f8307e12SArchie Cobbs struct sockaddr_un *const sun = (struct sockaddr_un *)sa; 226f8307e12SArchie Cobbs int toklen, pathlen; 227f8307e12SArchie Cobbs char *path; 228f8307e12SArchie Cobbs 22927121ab1SBrian Somers if ((path = ng_get_string_token(s, off, &toklen, NULL)) == NULL) 230f8307e12SArchie Cobbs return (EINVAL); 231f8307e12SArchie Cobbs pathlen = strlen(path); 232f8307e12SArchie Cobbs if (pathlen > SOCK_MAXADDRLEN) { 2339c8c302fSJulian Elischer FREE(path, M_NETGRAPH_KSOCKET); 234f8307e12SArchie Cobbs return (E2BIG); 235f8307e12SArchie Cobbs } 236f8307e12SArchie Cobbs if (*buflen < pathoff + pathlen) { 2379c8c302fSJulian Elischer FREE(path, M_NETGRAPH_KSOCKET); 238f8307e12SArchie Cobbs return (ERANGE); 239f8307e12SArchie Cobbs } 240f8307e12SArchie Cobbs *off += toklen; 241f8307e12SArchie Cobbs bcopy(path, sun->sun_path, pathlen); 242f8307e12SArchie Cobbs sun->sun_len = pathoff + pathlen; 2439c8c302fSJulian Elischer FREE(path, M_NETGRAPH_KSOCKET); 244f8307e12SArchie Cobbs break; 245f8307e12SArchie Cobbs } 246f8307e12SArchie Cobbs 247f8307e12SArchie Cobbs case PF_INET: /* Get an IP address with optional port */ 248f8307e12SArchie Cobbs { 249f8307e12SArchie Cobbs struct sockaddr_in *const sin = (struct sockaddr_in *)sa; 250f8307e12SArchie Cobbs int i; 251f8307e12SArchie Cobbs 252f8307e12SArchie Cobbs /* Parse this: <ipaddress>[:port] */ 253f8307e12SArchie Cobbs for (i = 0; i < 4; i++) { 254f8307e12SArchie Cobbs u_long val; 255f8307e12SArchie Cobbs char *eptr; 256f8307e12SArchie Cobbs 257f8307e12SArchie Cobbs val = strtoul(s + *off, &eptr, 10); 258f8307e12SArchie Cobbs if (val > 0xff || eptr == s + *off) 259f8307e12SArchie Cobbs return (EINVAL); 260f8307e12SArchie Cobbs *off += (eptr - (s + *off)); 261f8307e12SArchie Cobbs ((u_char *)&sin->sin_addr)[i] = (u_char)val; 262f8307e12SArchie Cobbs if (i < 3) { 263f8307e12SArchie Cobbs if (s[*off] != '.') 264f8307e12SArchie Cobbs return (EINVAL); 265f8307e12SArchie Cobbs (*off)++; 266f8307e12SArchie Cobbs } else if (s[*off] == ':') { 267f8307e12SArchie Cobbs (*off)++; 268f8307e12SArchie Cobbs val = strtoul(s + *off, &eptr, 10); 269f8307e12SArchie Cobbs if (val > 0xffff || eptr == s + *off) 270f8307e12SArchie Cobbs return (EINVAL); 271f8307e12SArchie Cobbs *off += (eptr - (s + *off)); 272f8307e12SArchie Cobbs sin->sin_port = htons(val); 273f8307e12SArchie Cobbs } else 274f8307e12SArchie Cobbs sin->sin_port = 0; 275f8307e12SArchie Cobbs } 276f8307e12SArchie Cobbs bzero(&sin->sin_zero, sizeof(sin->sin_zero)); 277f8307e12SArchie Cobbs sin->sin_len = sizeof(*sin); 278f8307e12SArchie Cobbs break; 279f8307e12SArchie Cobbs } 280f8307e12SArchie Cobbs 281f8307e12SArchie Cobbs #if 0 282f8307e12SArchie Cobbs case PF_APPLETALK: /* XXX implement these someday */ 283f8307e12SArchie Cobbs case PF_INET6: 284f8307e12SArchie Cobbs case PF_IPX: 285f8307e12SArchie Cobbs #endif 286f8307e12SArchie Cobbs 287f8307e12SArchie Cobbs default: 288f8307e12SArchie Cobbs return (EINVAL); 289f8307e12SArchie Cobbs } 290f8307e12SArchie Cobbs 291f8307e12SArchie Cobbs /* Done */ 292f8307e12SArchie Cobbs *buflen = sa->sa_len; 293f8307e12SArchie Cobbs return (0); 294f8307e12SArchie Cobbs } 295f8307e12SArchie Cobbs 296f8307e12SArchie Cobbs /* Convert a struct sockaddr from binary to ASCII */ 297f8307e12SArchie Cobbs static int 298f8307e12SArchie Cobbs ng_ksocket_sockaddr_unparse(const struct ng_parse_type *type, 299f8307e12SArchie Cobbs const u_char *data, int *off, char *cbuf, int cbuflen) 300f8307e12SArchie Cobbs { 301f8307e12SArchie Cobbs const struct sockaddr *sa = (const struct sockaddr *)(data + *off); 302f8307e12SArchie Cobbs int slen = 0; 303f8307e12SArchie Cobbs 304f8307e12SArchie Cobbs /* Output socket address, either in special or generic format */ 305f8307e12SArchie Cobbs switch (sa->sa_family) { 306f8307e12SArchie Cobbs case PF_LOCAL: 307f8307e12SArchie Cobbs { 308f8307e12SArchie Cobbs const int pathoff = OFFSETOF(struct sockaddr_un, sun_path); 309f8307e12SArchie Cobbs const struct sockaddr_un *sun = (const struct sockaddr_un *)sa; 310f8307e12SArchie Cobbs const int pathlen = sun->sun_len - pathoff; 311f8307e12SArchie Cobbs char pathbuf[SOCK_MAXADDRLEN + 1]; 312f8307e12SArchie Cobbs char *pathtoken; 313f8307e12SArchie Cobbs 314f8307e12SArchie Cobbs bcopy(sun->sun_path, pathbuf, pathlen); 31527121ab1SBrian Somers if ((pathtoken = ng_encode_string(pathbuf, pathlen)) == NULL) 316f8307e12SArchie Cobbs return (ENOMEM); 317f8307e12SArchie Cobbs slen += snprintf(cbuf, cbuflen, "local/%s", pathtoken); 3189c8c302fSJulian Elischer FREE(pathtoken, M_NETGRAPH_KSOCKET); 319f8307e12SArchie Cobbs if (slen >= cbuflen) 320f8307e12SArchie Cobbs return (ERANGE); 321f8307e12SArchie Cobbs *off += sun->sun_len; 322f8307e12SArchie Cobbs return (0); 323f8307e12SArchie Cobbs } 324f8307e12SArchie Cobbs 325f8307e12SArchie Cobbs case PF_INET: 326f8307e12SArchie Cobbs { 327f8307e12SArchie Cobbs const struct sockaddr_in *sin = (const struct sockaddr_in *)sa; 328f8307e12SArchie Cobbs 329f8307e12SArchie Cobbs slen += snprintf(cbuf, cbuflen, "inet/%d.%d.%d.%d", 330f8307e12SArchie Cobbs ((const u_char *)&sin->sin_addr)[0], 331f8307e12SArchie Cobbs ((const u_char *)&sin->sin_addr)[1], 332f8307e12SArchie Cobbs ((const u_char *)&sin->sin_addr)[2], 333f8307e12SArchie Cobbs ((const u_char *)&sin->sin_addr)[3]); 334f8307e12SArchie Cobbs if (sin->sin_port != 0) { 335f8307e12SArchie Cobbs slen += snprintf(cbuf + strlen(cbuf), 336f8307e12SArchie Cobbs cbuflen - strlen(cbuf), ":%d", 337f8307e12SArchie Cobbs (u_int)ntohs(sin->sin_port)); 338f8307e12SArchie Cobbs } 339f8307e12SArchie Cobbs if (slen >= cbuflen) 340f8307e12SArchie Cobbs return (ERANGE); 341f8307e12SArchie Cobbs *off += sizeof(*sin); 342f8307e12SArchie Cobbs return(0); 343f8307e12SArchie Cobbs } 344f8307e12SArchie Cobbs 345f8307e12SArchie Cobbs #if 0 346f8307e12SArchie Cobbs case PF_APPLETALK: /* XXX implement these someday */ 347f8307e12SArchie Cobbs case PF_INET6: 348f8307e12SArchie Cobbs case PF_IPX: 349f8307e12SArchie Cobbs #endif 350f8307e12SArchie Cobbs 351f8307e12SArchie Cobbs default: 352f8307e12SArchie Cobbs return (*ng_ksocket_generic_sockaddr_type.supertype->unparse) 353f8307e12SArchie Cobbs (&ng_ksocket_generic_sockaddr_type, 354f8307e12SArchie Cobbs data, off, cbuf, cbuflen); 355f8307e12SArchie Cobbs } 356f8307e12SArchie Cobbs } 357f8307e12SArchie Cobbs 358f8307e12SArchie Cobbs /* Parse type for struct sockaddr */ 359f8307e12SArchie Cobbs static const struct ng_parse_type ng_ksocket_sockaddr_type = { 360f8307e12SArchie Cobbs NULL, 361f8307e12SArchie Cobbs NULL, 362f8307e12SArchie Cobbs NULL, 363f8307e12SArchie Cobbs &ng_ksocket_sockaddr_parse, 364f8307e12SArchie Cobbs &ng_ksocket_sockaddr_unparse, 365f8307e12SArchie Cobbs NULL /* no such thing as a default struct sockaddr */ 366f8307e12SArchie Cobbs }; 367f8307e12SArchie Cobbs 368f8307e12SArchie Cobbs /************************************************************************ 369f8307e12SArchie Cobbs STRUCT NG_KSOCKET_SOCKOPT PARSE TYPE 370f8307e12SArchie Cobbs ************************************************************************/ 371f8307e12SArchie Cobbs 372f8307e12SArchie Cobbs /* Get length of the struct ng_ksocket_sockopt value field, which is the 373f8307e12SArchie Cobbs just the excess of the message argument portion over the length of 374f8307e12SArchie Cobbs the struct ng_ksocket_sockopt. */ 375f8307e12SArchie Cobbs static int 376f8307e12SArchie Cobbs ng_parse_sockoptval_getLength(const struct ng_parse_type *type, 377f8307e12SArchie Cobbs const u_char *start, const u_char *buf) 378f8307e12SArchie Cobbs { 379f8307e12SArchie Cobbs static const int offset = OFFSETOF(struct ng_ksocket_sockopt, value); 380f8307e12SArchie Cobbs const struct ng_ksocket_sockopt *sopt; 381f8307e12SArchie Cobbs const struct ng_mesg *msg; 382f8307e12SArchie Cobbs 383f8307e12SArchie Cobbs sopt = (const struct ng_ksocket_sockopt *)(buf - offset); 384f8307e12SArchie Cobbs msg = (const struct ng_mesg *)((const u_char *)sopt - sizeof(*msg)); 385f8307e12SArchie Cobbs return msg->header.arglen - sizeof(*sopt); 386f8307e12SArchie Cobbs } 387f8307e12SArchie Cobbs 388f8307e12SArchie Cobbs /* Parse type for the option value part of a struct ng_ksocket_sockopt 389f8307e12SArchie Cobbs XXX Eventually, we should handle the different socket options specially. 390f8307e12SArchie Cobbs XXX This would avoid byte order problems, eg an integer value of 1 is 391f8307e12SArchie Cobbs XXX going to be "[1]" for little endian or "[3=1]" for big endian. */ 392f8307e12SArchie Cobbs static const struct ng_parse_type ng_ksocket_sockoptval_type = { 393f8307e12SArchie Cobbs &ng_parse_bytearray_type, 394f8307e12SArchie Cobbs &ng_parse_sockoptval_getLength 395f8307e12SArchie Cobbs }; 396f8307e12SArchie Cobbs 397f8307e12SArchie Cobbs /* Parse type for struct ng_ksocket_sockopt */ 398f8307e12SArchie Cobbs static const struct ng_parse_struct_info ng_ksocket_sockopt_type_info 399f8307e12SArchie Cobbs = NG_KSOCKET_SOCKOPT_INFO(&ng_ksocket_sockoptval_type); 400f8307e12SArchie Cobbs static const struct ng_parse_type ng_ksocket_sockopt_type = { 401f8307e12SArchie Cobbs &ng_parse_struct_type, 402f8307e12SArchie Cobbs &ng_ksocket_sockopt_type_info, 403f8307e12SArchie Cobbs }; 404f8307e12SArchie Cobbs 405f8307e12SArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */ 406f8307e12SArchie Cobbs static const struct ng_cmdlist ng_ksocket_cmds[] = { 407f8307e12SArchie Cobbs { 408f8307e12SArchie Cobbs NGM_KSOCKET_COOKIE, 409f8307e12SArchie Cobbs NGM_KSOCKET_BIND, 410f8307e12SArchie Cobbs "bind", 411f8307e12SArchie Cobbs &ng_ksocket_sockaddr_type, 412f8307e12SArchie Cobbs NULL 413f8307e12SArchie Cobbs }, 414f8307e12SArchie Cobbs { 415f8307e12SArchie Cobbs NGM_KSOCKET_COOKIE, 416f8307e12SArchie Cobbs NGM_KSOCKET_LISTEN, 417f8307e12SArchie Cobbs "listen", 418f8307e12SArchie Cobbs &ng_parse_int32_type, 419f8307e12SArchie Cobbs NULL 420f8307e12SArchie Cobbs }, 421f8307e12SArchie Cobbs { 422f8307e12SArchie Cobbs NGM_KSOCKET_COOKIE, 423f8307e12SArchie Cobbs NGM_KSOCKET_ACCEPT, 424f8307e12SArchie Cobbs "accept", 425f8307e12SArchie Cobbs NULL, 426f8307e12SArchie Cobbs &ng_ksocket_sockaddr_type 427f8307e12SArchie Cobbs }, 428f8307e12SArchie Cobbs { 429f8307e12SArchie Cobbs NGM_KSOCKET_COOKIE, 430f8307e12SArchie Cobbs NGM_KSOCKET_CONNECT, 431f8307e12SArchie Cobbs "connect", 432f8307e12SArchie Cobbs &ng_ksocket_sockaddr_type, 433f8307e12SArchie Cobbs NULL 434f8307e12SArchie Cobbs }, 435f8307e12SArchie Cobbs { 436f8307e12SArchie Cobbs NGM_KSOCKET_COOKIE, 437f8307e12SArchie Cobbs NGM_KSOCKET_GETNAME, 438f8307e12SArchie Cobbs "getname", 439f8307e12SArchie Cobbs NULL, 440f8307e12SArchie Cobbs &ng_ksocket_sockaddr_type 441f8307e12SArchie Cobbs }, 442f8307e12SArchie Cobbs { 443f8307e12SArchie Cobbs NGM_KSOCKET_COOKIE, 444f8307e12SArchie Cobbs NGM_KSOCKET_GETPEERNAME, 445f8307e12SArchie Cobbs "getpeername", 446f8307e12SArchie Cobbs NULL, 447f8307e12SArchie Cobbs &ng_ksocket_sockaddr_type 448f8307e12SArchie Cobbs }, 449f8307e12SArchie Cobbs { 450f8307e12SArchie Cobbs NGM_KSOCKET_COOKIE, 451f8307e12SArchie Cobbs NGM_KSOCKET_SETOPT, 452f8307e12SArchie Cobbs "setopt", 453f8307e12SArchie Cobbs &ng_ksocket_sockopt_type, 454f8307e12SArchie Cobbs NULL 455f8307e12SArchie Cobbs }, 456f8307e12SArchie Cobbs { 457f8307e12SArchie Cobbs NGM_KSOCKET_COOKIE, 458f8307e12SArchie Cobbs NGM_KSOCKET_GETOPT, 459f8307e12SArchie Cobbs "getopt", 460f8307e12SArchie Cobbs &ng_ksocket_sockopt_type, 461f8307e12SArchie Cobbs &ng_ksocket_sockopt_type 462f8307e12SArchie Cobbs }, 463f8307e12SArchie Cobbs { 0 } 464f8307e12SArchie Cobbs }; 465f8307e12SArchie Cobbs 466f8307e12SArchie Cobbs /* Node type descriptor */ 467f8307e12SArchie Cobbs static struct ng_type ng_ksocket_typestruct = { 468589f6ed8SJulian Elischer NG_ABI_VERSION, 469f8307e12SArchie Cobbs NG_KSOCKET_NODE_TYPE, 470f8307e12SArchie Cobbs NULL, 471f8307e12SArchie Cobbs ng_ksocket_constructor, 472f8307e12SArchie Cobbs ng_ksocket_rcvmsg, 473069154d5SJulian Elischer ng_ksocket_shutdown, 474f8307e12SArchie Cobbs ng_ksocket_newhook, 475f8307e12SArchie Cobbs NULL, 476f8307e12SArchie Cobbs NULL, 477f8307e12SArchie Cobbs ng_ksocket_rcvdata, 478f8307e12SArchie Cobbs ng_ksocket_disconnect, 479f8307e12SArchie Cobbs ng_ksocket_cmds 480f8307e12SArchie Cobbs }; 481f8307e12SArchie Cobbs NETGRAPH_INIT(ksocket, &ng_ksocket_typestruct); 482f8307e12SArchie Cobbs 483cb3c7a5dSArchie Cobbs #define ERROUT(x) do { error = (x); goto done; } while (0) 484cb3c7a5dSArchie Cobbs 485cb3c7a5dSArchie Cobbs /************************************************************************ 486cb3c7a5dSArchie Cobbs NETGRAPH NODE STUFF 487cb3c7a5dSArchie Cobbs ************************************************************************/ 488cb3c7a5dSArchie Cobbs 489cb3c7a5dSArchie Cobbs /* 490cb3c7a5dSArchie Cobbs * Node type constructor 491cb3c7a5dSArchie Cobbs */ 492cb3c7a5dSArchie Cobbs static int 493069154d5SJulian Elischer ng_ksocket_constructor(node_p node) 494cb3c7a5dSArchie Cobbs { 495cb3c7a5dSArchie Cobbs priv_p priv; 496cb3c7a5dSArchie Cobbs 497cb3c7a5dSArchie Cobbs /* Allocate private structure */ 4989c8c302fSJulian Elischer MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH_KSOCKET, M_NOWAIT | M_ZERO); 499cb3c7a5dSArchie Cobbs if (priv == NULL) 500cb3c7a5dSArchie Cobbs return (ENOMEM); 501cb3c7a5dSArchie Cobbs 50230400f03SJulian Elischer NG_NODE_SET_PRIVATE(node, priv); 503cb3c7a5dSArchie Cobbs 504cb3c7a5dSArchie Cobbs /* Done */ 505cb3c7a5dSArchie Cobbs return (0); 506cb3c7a5dSArchie Cobbs } 507cb3c7a5dSArchie Cobbs 508cb3c7a5dSArchie Cobbs /* 509cb3c7a5dSArchie Cobbs * Give our OK for a hook to be added. The hook name is of the 510cb3c7a5dSArchie Cobbs * form "<family>:<type>:<proto>" where the three components may 511cb3c7a5dSArchie Cobbs * be decimal numbers or else aliases from the above lists. 512cb3c7a5dSArchie Cobbs * 513cb3c7a5dSArchie Cobbs * Connecting a hook amounts to opening the socket. Disconnecting 514cb3c7a5dSArchie Cobbs * the hook closes the socket and destroys the node as well. 515cb3c7a5dSArchie Cobbs */ 516cb3c7a5dSArchie Cobbs static int 517cb3c7a5dSArchie Cobbs ng_ksocket_newhook(node_p node, hook_p hook, const char *name0) 518cb3c7a5dSArchie Cobbs { 519f8307e12SArchie Cobbs struct proc *p = curproc ? curproc : &proc0; /* XXX broken */ 52030400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 521cb3c7a5dSArchie Cobbs char *s1, *s2, name[NG_HOOKLEN+1]; 522cb3c7a5dSArchie Cobbs int family, type, protocol, error; 523cb3c7a5dSArchie Cobbs 524cb3c7a5dSArchie Cobbs /* Check if we're already connected */ 525cb3c7a5dSArchie Cobbs if (priv->hook != NULL) 526cb3c7a5dSArchie Cobbs return (EISCONN); 527cb3c7a5dSArchie Cobbs 528cb3c7a5dSArchie Cobbs /* Extract family, type, and protocol from hook name */ 529cb3c7a5dSArchie Cobbs snprintf(name, sizeof(name), "%s", name0); 530cb3c7a5dSArchie Cobbs s1 = name; 531cb3c7a5dSArchie Cobbs if ((s2 = index(s1, '/')) == NULL) 532cb3c7a5dSArchie Cobbs return (EINVAL); 533cb3c7a5dSArchie Cobbs *s2++ = '\0'; 534cb3c7a5dSArchie Cobbs if ((family = ng_ksocket_parse(ng_ksocket_families, s1, 0)) == -1) 535cb3c7a5dSArchie Cobbs return (EINVAL); 536cb3c7a5dSArchie Cobbs s1 = s2; 537cb3c7a5dSArchie Cobbs if ((s2 = index(s1, '/')) == NULL) 538cb3c7a5dSArchie Cobbs return (EINVAL); 539cb3c7a5dSArchie Cobbs *s2++ = '\0'; 540cb3c7a5dSArchie Cobbs if ((type = ng_ksocket_parse(ng_ksocket_types, s1, 0)) == -1) 541cb3c7a5dSArchie Cobbs return (EINVAL); 542cb3c7a5dSArchie Cobbs s1 = s2; 543cb3c7a5dSArchie Cobbs if ((protocol = ng_ksocket_parse(ng_ksocket_protos, s1, family)) == -1) 544cb3c7a5dSArchie Cobbs return (EINVAL); 545cb3c7a5dSArchie Cobbs 546cb3c7a5dSArchie Cobbs /* Create the socket */ 547cb3c7a5dSArchie Cobbs if ((error = socreate(family, &priv->so, type, protocol, p)) != 0) 548cb3c7a5dSArchie Cobbs return (error); 549cb3c7a5dSArchie Cobbs 550cb3c7a5dSArchie Cobbs /* XXX call soreserve() ? */ 551cb3c7a5dSArchie Cobbs 552cb3c7a5dSArchie Cobbs /* Add our hook for incoming data */ 553cb3c7a5dSArchie Cobbs priv->so->so_upcallarg = (caddr_t)node; 554cb3c7a5dSArchie Cobbs priv->so->so_upcall = ng_ksocket_incoming; 555cb3c7a5dSArchie Cobbs priv->so->so_rcv.sb_flags |= SB_UPCALL; 556cb3c7a5dSArchie Cobbs 557cb3c7a5dSArchie Cobbs /* OK */ 558cb3c7a5dSArchie Cobbs priv->hook = hook; 559cb3c7a5dSArchie Cobbs return (0); 560cb3c7a5dSArchie Cobbs } 561cb3c7a5dSArchie Cobbs 562cb3c7a5dSArchie Cobbs /* 563cb3c7a5dSArchie Cobbs * Receive a control message 564cb3c7a5dSArchie Cobbs */ 565cb3c7a5dSArchie Cobbs static int 566069154d5SJulian Elischer ng_ksocket_rcvmsg(node_p node, item_p item, hook_p lasthook) 567cb3c7a5dSArchie Cobbs { 568f8307e12SArchie Cobbs struct proc *p = curproc ? curproc : &proc0; /* XXX broken */ 56930400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 570f8307e12SArchie Cobbs struct socket *const so = priv->so; 571cb3c7a5dSArchie Cobbs struct ng_mesg *resp = NULL; 572cb3c7a5dSArchie Cobbs int error = 0; 573069154d5SJulian Elischer struct ng_mesg *msg; 574cb3c7a5dSArchie Cobbs 575069154d5SJulian Elischer NGI_GET_MSG(item, msg); 576cb3c7a5dSArchie Cobbs switch (msg->header.typecookie) { 577cb3c7a5dSArchie Cobbs case NGM_KSOCKET_COOKIE: 578cb3c7a5dSArchie Cobbs switch (msg->header.cmd) { 579cb3c7a5dSArchie Cobbs case NGM_KSOCKET_BIND: 580cb3c7a5dSArchie Cobbs { 581f8307e12SArchie Cobbs struct sockaddr *const sa 582f8307e12SArchie Cobbs = (struct sockaddr *)msg->data; 583cb3c7a5dSArchie Cobbs 584f8307e12SArchie Cobbs /* Sanity check */ 585f8307e12SArchie Cobbs if (msg->header.arglen < SADATA_OFFSET 586f8307e12SArchie Cobbs || msg->header.arglen < sa->sa_len) 587f8307e12SArchie Cobbs ERROUT(EINVAL); 588f8307e12SArchie Cobbs if (so == NULL) 589f8307e12SArchie Cobbs ERROUT(ENXIO); 590cb3c7a5dSArchie Cobbs 591f8307e12SArchie Cobbs /* Bind */ 592cb3c7a5dSArchie Cobbs error = sobind(so, sa, p); 593cb3c7a5dSArchie Cobbs break; 594cb3c7a5dSArchie Cobbs } 595cb3c7a5dSArchie Cobbs case NGM_KSOCKET_LISTEN: 596cb3c7a5dSArchie Cobbs { 597f8307e12SArchie Cobbs /* Sanity check */ 598cb3c7a5dSArchie Cobbs if (msg->header.arglen != sizeof(int)) 599cb3c7a5dSArchie Cobbs ERROUT(EINVAL); 600f8307e12SArchie Cobbs if (so == NULL) 601f8307e12SArchie Cobbs ERROUT(ENXIO); 602cb3c7a5dSArchie Cobbs 603f8307e12SArchie Cobbs /* Listen */ 604f8307e12SArchie Cobbs if ((error = solisten(so, *((int *)msg->data), p)) != 0) 605cb3c7a5dSArchie Cobbs break; 606cb3c7a5dSArchie Cobbs 607cb3c7a5dSArchie Cobbs /* Notify sender when we get a connection attempt */ 608cb3c7a5dSArchie Cobbs /* XXX implement me */ 609f8307e12SArchie Cobbs error = ENODEV; 610cb3c7a5dSArchie Cobbs break; 611cb3c7a5dSArchie Cobbs } 612cb3c7a5dSArchie Cobbs 613cb3c7a5dSArchie Cobbs case NGM_KSOCKET_ACCEPT: 614cb3c7a5dSArchie Cobbs { 615f8307e12SArchie Cobbs /* Sanity check */ 616f8307e12SArchie Cobbs if (msg->header.arglen != 0) 617f8307e12SArchie Cobbs ERROUT(EINVAL); 618f8307e12SArchie Cobbs if (so == NULL) 619f8307e12SArchie Cobbs ERROUT(ENXIO); 620f8307e12SArchie Cobbs 621f8307e12SArchie Cobbs /* Accept on the socket in a non-blocking way */ 622f8307e12SArchie Cobbs /* Create a new ksocket node for the new connection */ 623f8307e12SArchie Cobbs /* Return a response with the peer's sockaddr and 624f8307e12SArchie Cobbs the absolute name of the newly created node */ 625f8307e12SArchie Cobbs 626f8307e12SArchie Cobbs /* XXX implement me */ 627f8307e12SArchie Cobbs 628f8307e12SArchie Cobbs error = ENODEV; 629cb3c7a5dSArchie Cobbs break; 630cb3c7a5dSArchie Cobbs } 631cb3c7a5dSArchie Cobbs 632cb3c7a5dSArchie Cobbs case NGM_KSOCKET_CONNECT: 633cb3c7a5dSArchie Cobbs { 634f8307e12SArchie Cobbs struct sockaddr *const sa 635f8307e12SArchie Cobbs = (struct sockaddr *)msg->data; 636cb3c7a5dSArchie Cobbs 637f8307e12SArchie Cobbs /* Sanity check */ 638f8307e12SArchie Cobbs if (msg->header.arglen < SADATA_OFFSET 639f8307e12SArchie Cobbs || msg->header.arglen < sa->sa_len) 640f8307e12SArchie Cobbs ERROUT(EINVAL); 641f8307e12SArchie Cobbs if (so == NULL) 642f8307e12SArchie Cobbs ERROUT(ENXIO); 643cb3c7a5dSArchie Cobbs 644cb3c7a5dSArchie Cobbs /* Do connect */ 645cb3c7a5dSArchie Cobbs if ((so->so_state & SS_ISCONNECTING) != 0) 646cb3c7a5dSArchie Cobbs ERROUT(EALREADY); 647cb3c7a5dSArchie Cobbs if ((error = soconnect(so, sa, p)) != 0) { 648cb3c7a5dSArchie Cobbs so->so_state &= ~SS_ISCONNECTING; 649cb3c7a5dSArchie Cobbs ERROUT(error); 650cb3c7a5dSArchie Cobbs } 651cb3c7a5dSArchie Cobbs if ((so->so_state & SS_ISCONNECTING) != 0) 652cb3c7a5dSArchie Cobbs /* Notify sender when we connect */ 653cb3c7a5dSArchie Cobbs /* XXX implement me */ 654cb3c7a5dSArchie Cobbs ERROUT(EINPROGRESS); 655cb3c7a5dSArchie Cobbs break; 656cb3c7a5dSArchie Cobbs } 657cb3c7a5dSArchie Cobbs 658cb3c7a5dSArchie Cobbs case NGM_KSOCKET_GETNAME: 659cb3c7a5dSArchie Cobbs case NGM_KSOCKET_GETPEERNAME: 660cb3c7a5dSArchie Cobbs { 661f8307e12SArchie Cobbs int (*func)(struct socket *so, struct sockaddr **nam); 662f8307e12SArchie Cobbs struct sockaddr *sa = NULL; 663f8307e12SArchie Cobbs int len; 664f8307e12SArchie Cobbs 665f8307e12SArchie Cobbs /* Sanity check */ 666f8307e12SArchie Cobbs if (msg->header.arglen != 0) 667f8307e12SArchie Cobbs ERROUT(EINVAL); 668f8307e12SArchie Cobbs if (so == NULL) 669f8307e12SArchie Cobbs ERROUT(ENXIO); 670f8307e12SArchie Cobbs 671f8307e12SArchie Cobbs /* Get function */ 672f8307e12SArchie Cobbs if (msg->header.cmd == NGM_KSOCKET_GETPEERNAME) { 673f8307e12SArchie Cobbs if ((so->so_state 674f8307e12SArchie Cobbs & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) 675f8307e12SArchie Cobbs ERROUT(ENOTCONN); 676f8307e12SArchie Cobbs func = so->so_proto->pr_usrreqs->pru_peeraddr; 677f8307e12SArchie Cobbs } else 678f8307e12SArchie Cobbs func = so->so_proto->pr_usrreqs->pru_sockaddr; 679f8307e12SArchie Cobbs 680f8307e12SArchie Cobbs /* Get local or peer address */ 681f8307e12SArchie Cobbs if ((error = (*func)(so, &sa)) != 0) 682f8307e12SArchie Cobbs goto bail; 683f8307e12SArchie Cobbs len = (sa == NULL) ? 0 : sa->sa_len; 684f8307e12SArchie Cobbs 685f8307e12SArchie Cobbs /* Send it back in a response */ 686f8307e12SArchie Cobbs NG_MKRESPONSE(resp, msg, len, M_NOWAIT); 687f8307e12SArchie Cobbs if (resp == NULL) { 688f8307e12SArchie Cobbs error = ENOMEM; 689f8307e12SArchie Cobbs goto bail; 690f8307e12SArchie Cobbs } 691f8307e12SArchie Cobbs bcopy(sa, resp->data, len); 692f8307e12SArchie Cobbs 693f8307e12SArchie Cobbs bail: 694f8307e12SArchie Cobbs /* Cleanup */ 695f8307e12SArchie Cobbs if (sa != NULL) 696f8307e12SArchie Cobbs FREE(sa, M_SONAME); 697cb3c7a5dSArchie Cobbs break; 698cb3c7a5dSArchie Cobbs } 699cb3c7a5dSArchie Cobbs 700cb3c7a5dSArchie Cobbs case NGM_KSOCKET_GETOPT: 701cb3c7a5dSArchie Cobbs { 702f8307e12SArchie Cobbs struct ng_ksocket_sockopt *ksopt = 703f8307e12SArchie Cobbs (struct ng_ksocket_sockopt *)msg->data; 704f8307e12SArchie Cobbs struct sockopt sopt; 705f8307e12SArchie Cobbs 706f8307e12SArchie Cobbs /* Sanity check */ 707f8307e12SArchie Cobbs if (msg->header.arglen != sizeof(*ksopt)) 708f8307e12SArchie Cobbs ERROUT(EINVAL); 709f8307e12SArchie Cobbs if (so == NULL) 710f8307e12SArchie Cobbs ERROUT(ENXIO); 711f8307e12SArchie Cobbs 712f8307e12SArchie Cobbs /* Get response with room for option value */ 713f8307e12SArchie Cobbs NG_MKRESPONSE(resp, msg, sizeof(*ksopt) 714f8307e12SArchie Cobbs + NG_KSOCKET_MAX_OPTLEN, M_NOWAIT); 715f8307e12SArchie Cobbs if (resp == NULL) 716f8307e12SArchie Cobbs ERROUT(ENOMEM); 717f8307e12SArchie Cobbs 718f8307e12SArchie Cobbs /* Get socket option, and put value in the response */ 719f8307e12SArchie Cobbs sopt.sopt_dir = SOPT_GET; 720f8307e12SArchie Cobbs sopt.sopt_level = ksopt->level; 721f8307e12SArchie Cobbs sopt.sopt_name = ksopt->name; 722f8307e12SArchie Cobbs sopt.sopt_p = p; 723f8307e12SArchie Cobbs sopt.sopt_valsize = NG_KSOCKET_MAX_OPTLEN; 724f8307e12SArchie Cobbs ksopt = (struct ng_ksocket_sockopt *)resp->data; 725f8307e12SArchie Cobbs sopt.sopt_val = ksopt->value; 726f8307e12SArchie Cobbs if ((error = sogetopt(so, &sopt)) != 0) { 727069154d5SJulian Elischer NG_FREE_MSG(resp); 728f8307e12SArchie Cobbs break; 729f8307e12SArchie Cobbs } 730f8307e12SArchie Cobbs 731f8307e12SArchie Cobbs /* Set actual value length */ 732f8307e12SArchie Cobbs resp->header.arglen = sizeof(*ksopt) 733f8307e12SArchie Cobbs + sopt.sopt_valsize; 734cb3c7a5dSArchie Cobbs break; 735cb3c7a5dSArchie Cobbs } 736cb3c7a5dSArchie Cobbs 737cb3c7a5dSArchie Cobbs case NGM_KSOCKET_SETOPT: 738cb3c7a5dSArchie Cobbs { 739f8307e12SArchie Cobbs struct ng_ksocket_sockopt *const ksopt = 740f8307e12SArchie Cobbs (struct ng_ksocket_sockopt *)msg->data; 741f8307e12SArchie Cobbs const int valsize = msg->header.arglen - sizeof(*ksopt); 742f8307e12SArchie Cobbs struct sockopt sopt; 743f8307e12SArchie Cobbs 744f8307e12SArchie Cobbs /* Sanity check */ 745f8307e12SArchie Cobbs if (valsize < 0) 746f8307e12SArchie Cobbs ERROUT(EINVAL); 747f8307e12SArchie Cobbs if (so == NULL) 748f8307e12SArchie Cobbs ERROUT(ENXIO); 749f8307e12SArchie Cobbs 750f8307e12SArchie Cobbs /* Set socket option */ 751f8307e12SArchie Cobbs sopt.sopt_dir = SOPT_SET; 752f8307e12SArchie Cobbs sopt.sopt_level = ksopt->level; 753f8307e12SArchie Cobbs sopt.sopt_name = ksopt->name; 754f8307e12SArchie Cobbs sopt.sopt_val = ksopt->value; 755f8307e12SArchie Cobbs sopt.sopt_valsize = valsize; 756f8307e12SArchie Cobbs sopt.sopt_p = p; 757f8307e12SArchie Cobbs error = sosetopt(so, &sopt); 758cb3c7a5dSArchie Cobbs break; 759cb3c7a5dSArchie Cobbs } 760cb3c7a5dSArchie Cobbs 761cb3c7a5dSArchie Cobbs default: 762cb3c7a5dSArchie Cobbs error = EINVAL; 763cb3c7a5dSArchie Cobbs break; 764cb3c7a5dSArchie Cobbs } 765cb3c7a5dSArchie Cobbs break; 766cb3c7a5dSArchie Cobbs default: 767cb3c7a5dSArchie Cobbs error = EINVAL; 768cb3c7a5dSArchie Cobbs break; 769cb3c7a5dSArchie Cobbs } 770cb3c7a5dSArchie Cobbs done: 771069154d5SJulian Elischer NG_RESPOND_MSG(error, node, item, resp); 772069154d5SJulian Elischer NG_FREE_MSG(msg); 773cb3c7a5dSArchie Cobbs return (error); 774cb3c7a5dSArchie Cobbs } 775cb3c7a5dSArchie Cobbs 776cb3c7a5dSArchie Cobbs /* 777cb3c7a5dSArchie Cobbs * Receive incoming data on our hook. Send it out the socket. 778cb3c7a5dSArchie Cobbs */ 779cb3c7a5dSArchie Cobbs static int 780069154d5SJulian Elischer ng_ksocket_rcvdata(hook_p hook, item_p item) 781cb3c7a5dSArchie Cobbs { 782f8307e12SArchie Cobbs struct proc *p = curproc ? curproc : &proc0; /* XXX broken */ 78330400f03SJulian Elischer const node_p node = NG_HOOK_NODE(hook); 78430400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 785cb3c7a5dSArchie Cobbs struct socket *const so = priv->so; 786cb3c7a5dSArchie Cobbs int error; 787069154d5SJulian Elischer struct mbuf *m; 788cb3c7a5dSArchie Cobbs 789069154d5SJulian Elischer NGI_GET_M(item, m); 790069154d5SJulian Elischer NG_FREE_ITEM(item); 791cb3c7a5dSArchie Cobbs error = (*so->so_proto->pr_usrreqs->pru_sosend)(so, 0, 0, m, 0, 0, p); 792cb3c7a5dSArchie Cobbs return (error); 793cb3c7a5dSArchie Cobbs } 794cb3c7a5dSArchie Cobbs 795cb3c7a5dSArchie Cobbs /* 796cb3c7a5dSArchie Cobbs * Destroy node 797cb3c7a5dSArchie Cobbs */ 798cb3c7a5dSArchie Cobbs static int 799069154d5SJulian Elischer ng_ksocket_shutdown(node_p node) 800cb3c7a5dSArchie Cobbs { 80130400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 802cb3c7a5dSArchie Cobbs 80319bff684SArchie Cobbs /* Close our socket (if any) */ 80419bff684SArchie Cobbs if (priv->so != NULL) { 805f8307e12SArchie Cobbs 806f8307e12SArchie Cobbs priv->so->so_upcall = NULL; 807f8307e12SArchie Cobbs priv->so->so_rcv.sb_flags &= ~SB_UPCALL; 80819bff684SArchie Cobbs soclose(priv->so); 80919bff684SArchie Cobbs priv->so = NULL; 81019bff684SArchie Cobbs } 81119bff684SArchie Cobbs 812cb3c7a5dSArchie Cobbs /* Take down netgraph node */ 813cb3c7a5dSArchie Cobbs bzero(priv, sizeof(*priv)); 8149c8c302fSJulian Elischer FREE(priv, M_NETGRAPH_KSOCKET); 81530400f03SJulian Elischer NG_NODE_SET_PRIVATE(node, NULL); 81630400f03SJulian Elischer NG_NODE_UNREF(node); /* let the node escape */ 817cb3c7a5dSArchie Cobbs return (0); 818cb3c7a5dSArchie Cobbs } 819cb3c7a5dSArchie Cobbs 820cb3c7a5dSArchie Cobbs /* 821cb3c7a5dSArchie Cobbs * Hook disconnection 822cb3c7a5dSArchie Cobbs */ 823cb3c7a5dSArchie Cobbs static int 824cb3c7a5dSArchie Cobbs ng_ksocket_disconnect(hook_p hook) 825cb3c7a5dSArchie Cobbs { 82630400f03SJulian Elischer KASSERT(NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0, 82730400f03SJulian Elischer ("%s: numhooks=%d?", __FUNCTION__, NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)))); 82830400f03SJulian Elischer if (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))) 82930400f03SJulian Elischer ng_rmnode_self(NG_HOOK_NODE(hook)); 830cb3c7a5dSArchie Cobbs return (0); 831cb3c7a5dSArchie Cobbs } 832cb3c7a5dSArchie Cobbs 833cb3c7a5dSArchie Cobbs /************************************************************************ 834cb3c7a5dSArchie Cobbs HELPER STUFF 835cb3c7a5dSArchie Cobbs ************************************************************************/ 836cb3c7a5dSArchie Cobbs 837cb3c7a5dSArchie Cobbs /* 838cb3c7a5dSArchie Cobbs * When incoming data is appended to the socket, we get notified here. 839cb3c7a5dSArchie Cobbs */ 840cb3c7a5dSArchie Cobbs static void 841cb3c7a5dSArchie Cobbs ng_ksocket_incoming(struct socket *so, void *arg, int waitflag) 842cb3c7a5dSArchie Cobbs { 843cb3c7a5dSArchie Cobbs const node_p node = arg; 84430400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 845cb3c7a5dSArchie Cobbs struct mbuf *m; 846cb3c7a5dSArchie Cobbs struct uio auio; 84719bff684SArchie Cobbs int s, flags, error; 84819bff684SArchie Cobbs 84919bff684SArchie Cobbs s = splnet(); 850cb3c7a5dSArchie Cobbs 851cb3c7a5dSArchie Cobbs /* Sanity check */ 85230400f03SJulian Elischer if (NG_NODE_NOT_VALID(node)) { 85319bff684SArchie Cobbs splx(s); 854cb3c7a5dSArchie Cobbs return; 85519bff684SArchie Cobbs } 856cb3c7a5dSArchie Cobbs KASSERT(so == priv->so, ("%s: wrong socket", __FUNCTION__)); 857cb3c7a5dSArchie Cobbs KASSERT(priv->hook != NULL, ("%s: no hook", __FUNCTION__)); 858cb3c7a5dSArchie Cobbs 859cb3c7a5dSArchie Cobbs /* Read and forward available mbuf's */ 860cb3c7a5dSArchie Cobbs auio.uio_procp = NULL; 861cb3c7a5dSArchie Cobbs auio.uio_resid = 1000000000; 862cb3c7a5dSArchie Cobbs flags = MSG_DONTWAIT; 863cb3c7a5dSArchie Cobbs do { 864cb3c7a5dSArchie Cobbs if ((error = (*so->so_proto->pr_usrreqs->pru_soreceive) 865be731c30SArchie Cobbs (so, (struct sockaddr **)0, &auio, &m, 866be731c30SArchie Cobbs (struct mbuf **)0, &flags)) == 0 867f8307e12SArchie Cobbs && m != NULL) { 868f8307e12SArchie Cobbs struct mbuf *n; 869f8307e12SArchie Cobbs 870f8307e12SArchie Cobbs /* Don't trust the various socket layers to get the 871f8307e12SArchie Cobbs packet header and length correct (eg. kern/15175) */ 872f8307e12SArchie Cobbs for (n = m, m->m_pkthdr.len = 0; n; n = n->m_next) 873f8307e12SArchie Cobbs m->m_pkthdr.len += n->m_len; 874069154d5SJulian Elischer NG_SEND_DATA_ONLY(error, priv->hook, m); 875f8307e12SArchie Cobbs } 876cb3c7a5dSArchie Cobbs } while (error == 0 && m != NULL); 87719bff684SArchie Cobbs splx(s); 878cb3c7a5dSArchie Cobbs } 879cb3c7a5dSArchie Cobbs 880cb3c7a5dSArchie Cobbs /* 881cb3c7a5dSArchie Cobbs * Parse out either an integer value or an alias. 882cb3c7a5dSArchie Cobbs */ 883cb3c7a5dSArchie Cobbs static int 884cb3c7a5dSArchie Cobbs ng_ksocket_parse(const struct ng_ksocket_alias *aliases, 885cb3c7a5dSArchie Cobbs const char *s, int family) 886cb3c7a5dSArchie Cobbs { 887cb3c7a5dSArchie Cobbs int k, val; 88825792ef3SArchie Cobbs char *eptr; 889cb3c7a5dSArchie Cobbs 890cb3c7a5dSArchie Cobbs /* Try aliases */ 891cb3c7a5dSArchie Cobbs for (k = 0; aliases[k].name != NULL; k++) { 892cb3c7a5dSArchie Cobbs if (strcmp(s, aliases[k].name) == 0 893cb3c7a5dSArchie Cobbs && aliases[k].family == family) 894cb3c7a5dSArchie Cobbs return aliases[k].value; 895cb3c7a5dSArchie Cobbs } 896cb3c7a5dSArchie Cobbs 897cb3c7a5dSArchie Cobbs /* Try parsing as a number */ 898cb3c7a5dSArchie Cobbs val = (int)strtoul(s, &eptr, 10); 899f8307e12SArchie Cobbs if (val < 0 || *eptr != '\0') 900cb3c7a5dSArchie Cobbs return (-1); 901cb3c7a5dSArchie Cobbs return (val); 902cb3c7a5dSArchie Cobbs } 903cb3c7a5dSArchie Cobbs 904