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