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 * 37cb3c7a5dSArchie Cobbs * Author: Archie Cobbs <archie@whistle.com> 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 { 164f8307e12SArchie Cobbs { "len", &ng_parse_int8_type }, 165f8307e12SArchie Cobbs { "family", &ng_parse_int8_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 223f8307e12SArchie Cobbs if ((path = ng_get_string_token(s, off, &toklen)) == 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); 309f8307e12SArchie Cobbs pathbuf[pathlen] = '\0'; 310f8307e12SArchie Cobbs if ((pathtoken = ng_encode_string(pathbuf)) == NULL) 311f8307e12SArchie Cobbs return (ENOMEM); 312f8307e12SArchie Cobbs slen += snprintf(cbuf, cbuflen, "local/%s", pathtoken); 313f8307e12SArchie Cobbs FREE(pathtoken, M_NETGRAPH); 314f8307e12SArchie Cobbs if (slen >= cbuflen) 315f8307e12SArchie Cobbs return (ERANGE); 316f8307e12SArchie Cobbs *off += sun->sun_len; 317f8307e12SArchie Cobbs return (0); 318f8307e12SArchie Cobbs } 319f8307e12SArchie Cobbs 320f8307e12SArchie Cobbs case PF_INET: 321f8307e12SArchie Cobbs { 322f8307e12SArchie Cobbs const struct sockaddr_in *sin = (const struct sockaddr_in *)sa; 323f8307e12SArchie Cobbs 324f8307e12SArchie Cobbs slen += snprintf(cbuf, cbuflen, "inet/%d.%d.%d.%d", 325f8307e12SArchie Cobbs ((const u_char *)&sin->sin_addr)[0], 326f8307e12SArchie Cobbs ((const u_char *)&sin->sin_addr)[1], 327f8307e12SArchie Cobbs ((const u_char *)&sin->sin_addr)[2], 328f8307e12SArchie Cobbs ((const u_char *)&sin->sin_addr)[3]); 329f8307e12SArchie Cobbs if (sin->sin_port != 0) { 330f8307e12SArchie Cobbs slen += snprintf(cbuf + strlen(cbuf), 331f8307e12SArchie Cobbs cbuflen - strlen(cbuf), ":%d", 332f8307e12SArchie Cobbs (u_int)ntohs(sin->sin_port)); 333f8307e12SArchie Cobbs } 334f8307e12SArchie Cobbs if (slen >= cbuflen) 335f8307e12SArchie Cobbs return (ERANGE); 336f8307e12SArchie Cobbs *off += sizeof(*sin); 337f8307e12SArchie Cobbs return(0); 338f8307e12SArchie Cobbs } 339f8307e12SArchie Cobbs 340f8307e12SArchie Cobbs #if 0 341f8307e12SArchie Cobbs case PF_APPLETALK: /* XXX implement these someday */ 342f8307e12SArchie Cobbs case PF_INET6: 343f8307e12SArchie Cobbs case PF_IPX: 344f8307e12SArchie Cobbs #endif 345f8307e12SArchie Cobbs 346f8307e12SArchie Cobbs default: 347f8307e12SArchie Cobbs return (*ng_ksocket_generic_sockaddr_type.supertype->unparse) 348f8307e12SArchie Cobbs (&ng_ksocket_generic_sockaddr_type, 349f8307e12SArchie Cobbs data, off, cbuf, cbuflen); 350f8307e12SArchie Cobbs } 351f8307e12SArchie Cobbs } 352f8307e12SArchie Cobbs 353f8307e12SArchie Cobbs /* Parse type for struct sockaddr */ 354f8307e12SArchie Cobbs static const struct ng_parse_type ng_ksocket_sockaddr_type = { 355f8307e12SArchie Cobbs NULL, 356f8307e12SArchie Cobbs NULL, 357f8307e12SArchie Cobbs NULL, 358f8307e12SArchie Cobbs &ng_ksocket_sockaddr_parse, 359f8307e12SArchie Cobbs &ng_ksocket_sockaddr_unparse, 360f8307e12SArchie Cobbs NULL /* no such thing as a default struct sockaddr */ 361f8307e12SArchie Cobbs }; 362f8307e12SArchie Cobbs 363f8307e12SArchie Cobbs /************************************************************************ 364f8307e12SArchie Cobbs STRUCT NG_KSOCKET_SOCKOPT PARSE TYPE 365f8307e12SArchie Cobbs ************************************************************************/ 366f8307e12SArchie Cobbs 367f8307e12SArchie Cobbs /* Get length of the struct ng_ksocket_sockopt value field, which is the 368f8307e12SArchie Cobbs just the excess of the message argument portion over the length of 369f8307e12SArchie Cobbs the struct ng_ksocket_sockopt. */ 370f8307e12SArchie Cobbs static int 371f8307e12SArchie Cobbs ng_parse_sockoptval_getLength(const struct ng_parse_type *type, 372f8307e12SArchie Cobbs const u_char *start, const u_char *buf) 373f8307e12SArchie Cobbs { 374f8307e12SArchie Cobbs static const int offset = OFFSETOF(struct ng_ksocket_sockopt, value); 375f8307e12SArchie Cobbs const struct ng_ksocket_sockopt *sopt; 376f8307e12SArchie Cobbs const struct ng_mesg *msg; 377f8307e12SArchie Cobbs 378f8307e12SArchie Cobbs sopt = (const struct ng_ksocket_sockopt *)(buf - offset); 379f8307e12SArchie Cobbs msg = (const struct ng_mesg *)((const u_char *)sopt - sizeof(*msg)); 380f8307e12SArchie Cobbs return msg->header.arglen - sizeof(*sopt); 381f8307e12SArchie Cobbs } 382f8307e12SArchie Cobbs 383f8307e12SArchie Cobbs /* Parse type for the option value part of a struct ng_ksocket_sockopt 384f8307e12SArchie Cobbs XXX Eventually, we should handle the different socket options specially. 385f8307e12SArchie Cobbs XXX This would avoid byte order problems, eg an integer value of 1 is 386f8307e12SArchie Cobbs XXX going to be "[1]" for little endian or "[3=1]" for big endian. */ 387f8307e12SArchie Cobbs static const struct ng_parse_type ng_ksocket_sockoptval_type = { 388f8307e12SArchie Cobbs &ng_parse_bytearray_type, 389f8307e12SArchie Cobbs &ng_parse_sockoptval_getLength 390f8307e12SArchie Cobbs }; 391f8307e12SArchie Cobbs 392f8307e12SArchie Cobbs /* Parse type for struct ng_ksocket_sockopt */ 393f8307e12SArchie Cobbs static const struct ng_parse_struct_info ng_ksocket_sockopt_type_info 394f8307e12SArchie Cobbs = NG_KSOCKET_SOCKOPT_INFO(&ng_ksocket_sockoptval_type); 395f8307e12SArchie Cobbs static const struct ng_parse_type ng_ksocket_sockopt_type = { 396f8307e12SArchie Cobbs &ng_parse_struct_type, 397f8307e12SArchie Cobbs &ng_ksocket_sockopt_type_info, 398f8307e12SArchie Cobbs }; 399f8307e12SArchie Cobbs 400f8307e12SArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */ 401f8307e12SArchie Cobbs static const struct ng_cmdlist ng_ksocket_cmds[] = { 402f8307e12SArchie Cobbs { 403f8307e12SArchie Cobbs NGM_KSOCKET_COOKIE, 404f8307e12SArchie Cobbs NGM_KSOCKET_BIND, 405f8307e12SArchie Cobbs "bind", 406f8307e12SArchie Cobbs &ng_ksocket_sockaddr_type, 407f8307e12SArchie Cobbs NULL 408f8307e12SArchie Cobbs }, 409f8307e12SArchie Cobbs { 410f8307e12SArchie Cobbs NGM_KSOCKET_COOKIE, 411f8307e12SArchie Cobbs NGM_KSOCKET_LISTEN, 412f8307e12SArchie Cobbs "listen", 413f8307e12SArchie Cobbs &ng_parse_int32_type, 414f8307e12SArchie Cobbs NULL 415f8307e12SArchie Cobbs }, 416f8307e12SArchie Cobbs { 417f8307e12SArchie Cobbs NGM_KSOCKET_COOKIE, 418f8307e12SArchie Cobbs NGM_KSOCKET_ACCEPT, 419f8307e12SArchie Cobbs "accept", 420f8307e12SArchie Cobbs NULL, 421f8307e12SArchie Cobbs &ng_ksocket_sockaddr_type 422f8307e12SArchie Cobbs }, 423f8307e12SArchie Cobbs { 424f8307e12SArchie Cobbs NGM_KSOCKET_COOKIE, 425f8307e12SArchie Cobbs NGM_KSOCKET_CONNECT, 426f8307e12SArchie Cobbs "connect", 427f8307e12SArchie Cobbs &ng_ksocket_sockaddr_type, 428f8307e12SArchie Cobbs NULL 429f8307e12SArchie Cobbs }, 430f8307e12SArchie Cobbs { 431f8307e12SArchie Cobbs NGM_KSOCKET_COOKIE, 432f8307e12SArchie Cobbs NGM_KSOCKET_GETNAME, 433f8307e12SArchie Cobbs "getname", 434f8307e12SArchie Cobbs NULL, 435f8307e12SArchie Cobbs &ng_ksocket_sockaddr_type 436f8307e12SArchie Cobbs }, 437f8307e12SArchie Cobbs { 438f8307e12SArchie Cobbs NGM_KSOCKET_COOKIE, 439f8307e12SArchie Cobbs NGM_KSOCKET_GETPEERNAME, 440f8307e12SArchie Cobbs "getpeername", 441f8307e12SArchie Cobbs NULL, 442f8307e12SArchie Cobbs &ng_ksocket_sockaddr_type 443f8307e12SArchie Cobbs }, 444f8307e12SArchie Cobbs { 445f8307e12SArchie Cobbs NGM_KSOCKET_COOKIE, 446f8307e12SArchie Cobbs NGM_KSOCKET_SETOPT, 447f8307e12SArchie Cobbs "setopt", 448f8307e12SArchie Cobbs &ng_ksocket_sockopt_type, 449f8307e12SArchie Cobbs NULL 450f8307e12SArchie Cobbs }, 451f8307e12SArchie Cobbs { 452f8307e12SArchie Cobbs NGM_KSOCKET_COOKIE, 453f8307e12SArchie Cobbs NGM_KSOCKET_GETOPT, 454f8307e12SArchie Cobbs "getopt", 455f8307e12SArchie Cobbs &ng_ksocket_sockopt_type, 456f8307e12SArchie Cobbs &ng_ksocket_sockopt_type 457f8307e12SArchie Cobbs }, 458f8307e12SArchie Cobbs { 0 } 459f8307e12SArchie Cobbs }; 460f8307e12SArchie Cobbs 461f8307e12SArchie Cobbs /* Node type descriptor */ 462f8307e12SArchie Cobbs static struct ng_type ng_ksocket_typestruct = { 463f8307e12SArchie Cobbs NG_VERSION, 464f8307e12SArchie Cobbs NG_KSOCKET_NODE_TYPE, 465f8307e12SArchie Cobbs NULL, 466f8307e12SArchie Cobbs ng_ksocket_constructor, 467f8307e12SArchie Cobbs ng_ksocket_rcvmsg, 468f8307e12SArchie Cobbs ng_ksocket_rmnode, 469f8307e12SArchie Cobbs ng_ksocket_newhook, 470f8307e12SArchie Cobbs NULL, 471f8307e12SArchie Cobbs NULL, 472f8307e12SArchie Cobbs ng_ksocket_rcvdata, 473f8307e12SArchie Cobbs ng_ksocket_rcvdata, 474f8307e12SArchie Cobbs ng_ksocket_disconnect, 475f8307e12SArchie Cobbs ng_ksocket_cmds 476f8307e12SArchie Cobbs }; 477f8307e12SArchie Cobbs NETGRAPH_INIT(ksocket, &ng_ksocket_typestruct); 478f8307e12SArchie Cobbs 479cb3c7a5dSArchie Cobbs #define ERROUT(x) do { error = (x); goto done; } while (0) 480cb3c7a5dSArchie Cobbs 481cb3c7a5dSArchie Cobbs /************************************************************************ 482cb3c7a5dSArchie Cobbs NETGRAPH NODE STUFF 483cb3c7a5dSArchie Cobbs ************************************************************************/ 484cb3c7a5dSArchie Cobbs 485cb3c7a5dSArchie Cobbs /* 486cb3c7a5dSArchie Cobbs * Node type constructor 487cb3c7a5dSArchie Cobbs */ 488cb3c7a5dSArchie Cobbs static int 489cb3c7a5dSArchie Cobbs ng_ksocket_constructor(node_p *nodep) 490cb3c7a5dSArchie Cobbs { 491cb3c7a5dSArchie Cobbs priv_p priv; 492cb3c7a5dSArchie Cobbs int error; 493cb3c7a5dSArchie Cobbs 494cb3c7a5dSArchie Cobbs /* Allocate private structure */ 495cb3c7a5dSArchie Cobbs MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_WAITOK); 496cb3c7a5dSArchie Cobbs if (priv == NULL) 497cb3c7a5dSArchie Cobbs return (ENOMEM); 498cb3c7a5dSArchie Cobbs bzero(priv, sizeof(*priv)); 499cb3c7a5dSArchie Cobbs 500cb3c7a5dSArchie Cobbs /* Call generic node constructor */ 501cb3c7a5dSArchie Cobbs if ((error = ng_make_node_common(&ng_ksocket_typestruct, nodep))) { 502cb3c7a5dSArchie Cobbs FREE(priv, M_NETGRAPH); 503cb3c7a5dSArchie Cobbs return (error); 504cb3c7a5dSArchie Cobbs } 505cb3c7a5dSArchie Cobbs (*nodep)->private = priv; 506cb3c7a5dSArchie Cobbs 507cb3c7a5dSArchie Cobbs /* Done */ 508cb3c7a5dSArchie Cobbs return (0); 509cb3c7a5dSArchie Cobbs } 510cb3c7a5dSArchie Cobbs 511cb3c7a5dSArchie Cobbs /* 512cb3c7a5dSArchie Cobbs * Give our OK for a hook to be added. The hook name is of the 513cb3c7a5dSArchie Cobbs * form "<family>:<type>:<proto>" where the three components may 514cb3c7a5dSArchie Cobbs * be decimal numbers or else aliases from the above lists. 515cb3c7a5dSArchie Cobbs * 516cb3c7a5dSArchie Cobbs * Connecting a hook amounts to opening the socket. Disconnecting 517cb3c7a5dSArchie Cobbs * the hook closes the socket and destroys the node as well. 518cb3c7a5dSArchie Cobbs */ 519cb3c7a5dSArchie Cobbs static int 520cb3c7a5dSArchie Cobbs ng_ksocket_newhook(node_p node, hook_p hook, const char *name0) 521cb3c7a5dSArchie Cobbs { 522f8307e12SArchie Cobbs struct proc *p = curproc ? curproc : &proc0; /* XXX broken */ 523cb3c7a5dSArchie Cobbs const priv_p priv = node->private; 524cb3c7a5dSArchie Cobbs char *s1, *s2, name[NG_HOOKLEN+1]; 525cb3c7a5dSArchie Cobbs int family, type, protocol, error; 526cb3c7a5dSArchie Cobbs 527cb3c7a5dSArchie Cobbs /* Check if we're already connected */ 528cb3c7a5dSArchie Cobbs if (priv->hook != NULL) 529cb3c7a5dSArchie Cobbs return (EISCONN); 530cb3c7a5dSArchie Cobbs 531cb3c7a5dSArchie Cobbs /* Extract family, type, and protocol from hook name */ 532cb3c7a5dSArchie Cobbs snprintf(name, sizeof(name), "%s", name0); 533cb3c7a5dSArchie Cobbs s1 = name; 534cb3c7a5dSArchie Cobbs if ((s2 = index(s1, '/')) == NULL) 535cb3c7a5dSArchie Cobbs return (EINVAL); 536cb3c7a5dSArchie Cobbs *s2++ = '\0'; 537cb3c7a5dSArchie Cobbs if ((family = ng_ksocket_parse(ng_ksocket_families, s1, 0)) == -1) 538cb3c7a5dSArchie Cobbs return (EINVAL); 539cb3c7a5dSArchie Cobbs s1 = s2; 540cb3c7a5dSArchie Cobbs if ((s2 = index(s1, '/')) == NULL) 541cb3c7a5dSArchie Cobbs return (EINVAL); 542cb3c7a5dSArchie Cobbs *s2++ = '\0'; 543cb3c7a5dSArchie Cobbs if ((type = ng_ksocket_parse(ng_ksocket_types, s1, 0)) == -1) 544cb3c7a5dSArchie Cobbs return (EINVAL); 545cb3c7a5dSArchie Cobbs s1 = s2; 546cb3c7a5dSArchie Cobbs if ((protocol = ng_ksocket_parse(ng_ksocket_protos, s1, family)) == -1) 547cb3c7a5dSArchie Cobbs return (EINVAL); 548cb3c7a5dSArchie Cobbs 549cb3c7a5dSArchie Cobbs /* Create the socket */ 550cb3c7a5dSArchie Cobbs if ((error = socreate(family, &priv->so, type, protocol, p)) != 0) 551cb3c7a5dSArchie Cobbs return (error); 552cb3c7a5dSArchie Cobbs 553cb3c7a5dSArchie Cobbs /* XXX call soreserve() ? */ 554cb3c7a5dSArchie Cobbs 555cb3c7a5dSArchie Cobbs /* Add our hook for incoming data */ 556cb3c7a5dSArchie Cobbs priv->so->so_upcallarg = (caddr_t)node; 557cb3c7a5dSArchie Cobbs priv->so->so_upcall = ng_ksocket_incoming; 558cb3c7a5dSArchie Cobbs priv->so->so_rcv.sb_flags |= SB_UPCALL; 559cb3c7a5dSArchie Cobbs 560cb3c7a5dSArchie Cobbs /* OK */ 561cb3c7a5dSArchie Cobbs priv->hook = hook; 562cb3c7a5dSArchie Cobbs return (0); 563cb3c7a5dSArchie Cobbs } 564cb3c7a5dSArchie Cobbs 565cb3c7a5dSArchie Cobbs /* 566cb3c7a5dSArchie Cobbs * Receive a control message 567cb3c7a5dSArchie Cobbs */ 568cb3c7a5dSArchie Cobbs static int 569cb3c7a5dSArchie Cobbs ng_ksocket_rcvmsg(node_p node, struct ng_mesg *msg, 570a4ec03cfSJulian Elischer const char *raddr, struct ng_mesg **rptr, hook_p lasthook) 571cb3c7a5dSArchie Cobbs { 572f8307e12SArchie Cobbs struct proc *p = curproc ? curproc : &proc0; /* XXX broken */ 573cb3c7a5dSArchie Cobbs const priv_p priv = node->private; 574f8307e12SArchie Cobbs struct socket *const so = priv->so; 575cb3c7a5dSArchie Cobbs struct ng_mesg *resp = NULL; 576cb3c7a5dSArchie Cobbs int error = 0; 577cb3c7a5dSArchie Cobbs 578cb3c7a5dSArchie Cobbs switch (msg->header.typecookie) { 579cb3c7a5dSArchie Cobbs case NGM_KSOCKET_COOKIE: 580cb3c7a5dSArchie Cobbs switch (msg->header.cmd) { 581cb3c7a5dSArchie Cobbs case NGM_KSOCKET_BIND: 582cb3c7a5dSArchie Cobbs { 583f8307e12SArchie Cobbs struct sockaddr *const sa 584f8307e12SArchie Cobbs = (struct sockaddr *)msg->data; 585cb3c7a5dSArchie Cobbs 586f8307e12SArchie Cobbs /* Sanity check */ 587f8307e12SArchie Cobbs if (msg->header.arglen < SADATA_OFFSET 588f8307e12SArchie Cobbs || msg->header.arglen < sa->sa_len) 589f8307e12SArchie Cobbs ERROUT(EINVAL); 590f8307e12SArchie Cobbs if (so == NULL) 591f8307e12SArchie Cobbs ERROUT(ENXIO); 592cb3c7a5dSArchie Cobbs 593f8307e12SArchie Cobbs /* Bind */ 594cb3c7a5dSArchie Cobbs error = sobind(so, sa, p); 595cb3c7a5dSArchie Cobbs break; 596cb3c7a5dSArchie Cobbs } 597cb3c7a5dSArchie Cobbs case NGM_KSOCKET_LISTEN: 598cb3c7a5dSArchie Cobbs { 599f8307e12SArchie Cobbs /* Sanity check */ 600cb3c7a5dSArchie Cobbs if (msg->header.arglen != sizeof(int)) 601cb3c7a5dSArchie Cobbs ERROUT(EINVAL); 602f8307e12SArchie Cobbs if (so == NULL) 603f8307e12SArchie Cobbs ERROUT(ENXIO); 604cb3c7a5dSArchie Cobbs 605f8307e12SArchie Cobbs /* Listen */ 606f8307e12SArchie Cobbs if ((error = solisten(so, *((int *)msg->data), p)) != 0) 607cb3c7a5dSArchie Cobbs break; 608cb3c7a5dSArchie Cobbs 609cb3c7a5dSArchie Cobbs /* Notify sender when we get a connection attempt */ 610cb3c7a5dSArchie Cobbs /* XXX implement me */ 611f8307e12SArchie Cobbs error = ENODEV; 612cb3c7a5dSArchie Cobbs break; 613cb3c7a5dSArchie Cobbs } 614cb3c7a5dSArchie Cobbs 615cb3c7a5dSArchie Cobbs case NGM_KSOCKET_ACCEPT: 616cb3c7a5dSArchie Cobbs { 617f8307e12SArchie Cobbs /* Sanity check */ 618f8307e12SArchie Cobbs if (msg->header.arglen != 0) 619f8307e12SArchie Cobbs ERROUT(EINVAL); 620f8307e12SArchie Cobbs if (so == NULL) 621f8307e12SArchie Cobbs ERROUT(ENXIO); 622f8307e12SArchie Cobbs 623f8307e12SArchie Cobbs /* Accept on the socket in a non-blocking way */ 624f8307e12SArchie Cobbs /* Create a new ksocket node for the new connection */ 625f8307e12SArchie Cobbs /* Return a response with the peer's sockaddr and 626f8307e12SArchie Cobbs the absolute name of the newly created node */ 627f8307e12SArchie Cobbs 628f8307e12SArchie Cobbs /* XXX implement me */ 629f8307e12SArchie Cobbs 630f8307e12SArchie Cobbs error = ENODEV; 631cb3c7a5dSArchie Cobbs break; 632cb3c7a5dSArchie Cobbs } 633cb3c7a5dSArchie Cobbs 634cb3c7a5dSArchie Cobbs case NGM_KSOCKET_CONNECT: 635cb3c7a5dSArchie Cobbs { 636f8307e12SArchie Cobbs struct sockaddr *const sa 637f8307e12SArchie Cobbs = (struct sockaddr *)msg->data; 638cb3c7a5dSArchie Cobbs 639f8307e12SArchie Cobbs /* Sanity check */ 640f8307e12SArchie Cobbs if (msg->header.arglen < SADATA_OFFSET 641f8307e12SArchie Cobbs || msg->header.arglen < sa->sa_len) 642f8307e12SArchie Cobbs ERROUT(EINVAL); 643f8307e12SArchie Cobbs if (so == NULL) 644f8307e12SArchie Cobbs ERROUT(ENXIO); 645cb3c7a5dSArchie Cobbs 646cb3c7a5dSArchie Cobbs /* Do connect */ 647cb3c7a5dSArchie Cobbs if ((so->so_state & SS_ISCONNECTING) != 0) 648cb3c7a5dSArchie Cobbs ERROUT(EALREADY); 649cb3c7a5dSArchie Cobbs if ((error = soconnect(so, sa, p)) != 0) { 650cb3c7a5dSArchie Cobbs so->so_state &= ~SS_ISCONNECTING; 651cb3c7a5dSArchie Cobbs ERROUT(error); 652cb3c7a5dSArchie Cobbs } 653cb3c7a5dSArchie Cobbs if ((so->so_state & SS_ISCONNECTING) != 0) 654cb3c7a5dSArchie Cobbs /* Notify sender when we connect */ 655cb3c7a5dSArchie Cobbs /* XXX implement me */ 656cb3c7a5dSArchie Cobbs ERROUT(EINPROGRESS); 657cb3c7a5dSArchie Cobbs break; 658cb3c7a5dSArchie Cobbs } 659cb3c7a5dSArchie Cobbs 660cb3c7a5dSArchie Cobbs case NGM_KSOCKET_GETNAME: 661cb3c7a5dSArchie Cobbs case NGM_KSOCKET_GETPEERNAME: 662cb3c7a5dSArchie Cobbs { 663f8307e12SArchie Cobbs int (*func)(struct socket *so, struct sockaddr **nam); 664f8307e12SArchie Cobbs struct sockaddr *sa = NULL; 665f8307e12SArchie Cobbs int len; 666f8307e12SArchie Cobbs 667f8307e12SArchie Cobbs /* Sanity check */ 668f8307e12SArchie Cobbs if (msg->header.arglen != 0) 669f8307e12SArchie Cobbs ERROUT(EINVAL); 670f8307e12SArchie Cobbs if (so == NULL) 671f8307e12SArchie Cobbs ERROUT(ENXIO); 672f8307e12SArchie Cobbs 673f8307e12SArchie Cobbs /* Get function */ 674f8307e12SArchie Cobbs if (msg->header.cmd == NGM_KSOCKET_GETPEERNAME) { 675f8307e12SArchie Cobbs if ((so->so_state 676f8307e12SArchie Cobbs & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) 677f8307e12SArchie Cobbs ERROUT(ENOTCONN); 678f8307e12SArchie Cobbs func = so->so_proto->pr_usrreqs->pru_peeraddr; 679f8307e12SArchie Cobbs } else 680f8307e12SArchie Cobbs func = so->so_proto->pr_usrreqs->pru_sockaddr; 681f8307e12SArchie Cobbs 682f8307e12SArchie Cobbs /* Get local or peer address */ 683f8307e12SArchie Cobbs if ((error = (*func)(so, &sa)) != 0) 684f8307e12SArchie Cobbs goto bail; 685f8307e12SArchie Cobbs len = (sa == NULL) ? 0 : sa->sa_len; 686f8307e12SArchie Cobbs 687f8307e12SArchie Cobbs /* Send it back in a response */ 688f8307e12SArchie Cobbs NG_MKRESPONSE(resp, msg, len, M_NOWAIT); 689f8307e12SArchie Cobbs if (resp == NULL) { 690f8307e12SArchie Cobbs error = ENOMEM; 691f8307e12SArchie Cobbs goto bail; 692f8307e12SArchie Cobbs } 693f8307e12SArchie Cobbs bcopy(sa, resp->data, len); 694f8307e12SArchie Cobbs 695f8307e12SArchie Cobbs bail: 696f8307e12SArchie Cobbs /* Cleanup */ 697f8307e12SArchie Cobbs if (sa != NULL) 698f8307e12SArchie Cobbs FREE(sa, M_SONAME); 699cb3c7a5dSArchie Cobbs break; 700cb3c7a5dSArchie Cobbs } 701cb3c7a5dSArchie Cobbs 702cb3c7a5dSArchie Cobbs case NGM_KSOCKET_GETOPT: 703cb3c7a5dSArchie Cobbs { 704f8307e12SArchie Cobbs struct ng_ksocket_sockopt *ksopt = 705f8307e12SArchie Cobbs (struct ng_ksocket_sockopt *)msg->data; 706f8307e12SArchie Cobbs struct sockopt sopt; 707f8307e12SArchie Cobbs 708f8307e12SArchie Cobbs /* Sanity check */ 709f8307e12SArchie Cobbs if (msg->header.arglen != sizeof(*ksopt)) 710f8307e12SArchie Cobbs ERROUT(EINVAL); 711f8307e12SArchie Cobbs if (so == NULL) 712f8307e12SArchie Cobbs ERROUT(ENXIO); 713f8307e12SArchie Cobbs 714f8307e12SArchie Cobbs /* Get response with room for option value */ 715f8307e12SArchie Cobbs NG_MKRESPONSE(resp, msg, sizeof(*ksopt) 716f8307e12SArchie Cobbs + NG_KSOCKET_MAX_OPTLEN, M_NOWAIT); 717f8307e12SArchie Cobbs if (resp == NULL) 718f8307e12SArchie Cobbs ERROUT(ENOMEM); 719f8307e12SArchie Cobbs 720f8307e12SArchie Cobbs /* Get socket option, and put value in the response */ 721f8307e12SArchie Cobbs sopt.sopt_dir = SOPT_GET; 722f8307e12SArchie Cobbs sopt.sopt_level = ksopt->level; 723f8307e12SArchie Cobbs sopt.sopt_name = ksopt->name; 724f8307e12SArchie Cobbs sopt.sopt_p = p; 725f8307e12SArchie Cobbs sopt.sopt_valsize = NG_KSOCKET_MAX_OPTLEN; 726f8307e12SArchie Cobbs ksopt = (struct ng_ksocket_sockopt *)resp->data; 727f8307e12SArchie Cobbs sopt.sopt_val = ksopt->value; 728f8307e12SArchie Cobbs if ((error = sogetopt(so, &sopt)) != 0) { 729f8307e12SArchie Cobbs FREE(resp, M_NETGRAPH); 730f8307e12SArchie Cobbs break; 731f8307e12SArchie Cobbs } 732f8307e12SArchie Cobbs 733f8307e12SArchie Cobbs /* Set actual value length */ 734f8307e12SArchie Cobbs resp->header.arglen = sizeof(*ksopt) 735f8307e12SArchie Cobbs + sopt.sopt_valsize; 736cb3c7a5dSArchie Cobbs break; 737cb3c7a5dSArchie Cobbs } 738cb3c7a5dSArchie Cobbs 739cb3c7a5dSArchie Cobbs case NGM_KSOCKET_SETOPT: 740cb3c7a5dSArchie Cobbs { 741f8307e12SArchie Cobbs struct ng_ksocket_sockopt *const ksopt = 742f8307e12SArchie Cobbs (struct ng_ksocket_sockopt *)msg->data; 743f8307e12SArchie Cobbs const int valsize = msg->header.arglen - sizeof(*ksopt); 744f8307e12SArchie Cobbs struct sockopt sopt; 745f8307e12SArchie Cobbs 746f8307e12SArchie Cobbs /* Sanity check */ 747f8307e12SArchie Cobbs if (valsize < 0) 748f8307e12SArchie Cobbs ERROUT(EINVAL); 749f8307e12SArchie Cobbs if (so == NULL) 750f8307e12SArchie Cobbs ERROUT(ENXIO); 751f8307e12SArchie Cobbs 752f8307e12SArchie Cobbs /* Set socket option */ 753f8307e12SArchie Cobbs sopt.sopt_dir = SOPT_SET; 754f8307e12SArchie Cobbs sopt.sopt_level = ksopt->level; 755f8307e12SArchie Cobbs sopt.sopt_name = ksopt->name; 756f8307e12SArchie Cobbs sopt.sopt_val = ksopt->value; 757f8307e12SArchie Cobbs sopt.sopt_valsize = valsize; 758f8307e12SArchie Cobbs sopt.sopt_p = p; 759f8307e12SArchie Cobbs error = sosetopt(so, &sopt); 760cb3c7a5dSArchie Cobbs break; 761cb3c7a5dSArchie Cobbs } 762cb3c7a5dSArchie Cobbs 763cb3c7a5dSArchie Cobbs default: 764cb3c7a5dSArchie Cobbs error = EINVAL; 765cb3c7a5dSArchie Cobbs break; 766cb3c7a5dSArchie Cobbs } 767cb3c7a5dSArchie Cobbs break; 768cb3c7a5dSArchie Cobbs default: 769cb3c7a5dSArchie Cobbs error = EINVAL; 770cb3c7a5dSArchie Cobbs break; 771cb3c7a5dSArchie Cobbs } 772cb3c7a5dSArchie Cobbs if (rptr) 773cb3c7a5dSArchie Cobbs *rptr = resp; 774cb3c7a5dSArchie Cobbs else if (resp) 775cb3c7a5dSArchie Cobbs FREE(resp, M_NETGRAPH); 776cb3c7a5dSArchie Cobbs 777cb3c7a5dSArchie Cobbs done: 778cb3c7a5dSArchie Cobbs FREE(msg, M_NETGRAPH); 779cb3c7a5dSArchie Cobbs return (error); 780cb3c7a5dSArchie Cobbs } 781cb3c7a5dSArchie Cobbs 782cb3c7a5dSArchie Cobbs /* 783cb3c7a5dSArchie Cobbs * Receive incoming data on our hook. Send it out the socket. 784cb3c7a5dSArchie Cobbs */ 785cb3c7a5dSArchie Cobbs static int 786a4ec03cfSJulian Elischer ng_ksocket_rcvdata(hook_p hook, struct mbuf *m, meta_p meta, 787a4ec03cfSJulian Elischer struct mbuf **ret_m, meta_p *ret_meta) 788cb3c7a5dSArchie Cobbs { 789f8307e12SArchie Cobbs struct proc *p = curproc ? curproc : &proc0; /* XXX broken */ 790cb3c7a5dSArchie Cobbs const node_p node = hook->node; 791cb3c7a5dSArchie Cobbs const priv_p priv = node->private; 792cb3c7a5dSArchie Cobbs struct socket *const so = priv->so; 793cb3c7a5dSArchie Cobbs int error; 794cb3c7a5dSArchie Cobbs 795cb3c7a5dSArchie Cobbs NG_FREE_META(meta); 796cb3c7a5dSArchie Cobbs error = (*so->so_proto->pr_usrreqs->pru_sosend)(so, 0, 0, m, 0, 0, p); 797cb3c7a5dSArchie Cobbs return (error); 798cb3c7a5dSArchie Cobbs } 799cb3c7a5dSArchie Cobbs 800cb3c7a5dSArchie Cobbs /* 801cb3c7a5dSArchie Cobbs * Destroy node 802cb3c7a5dSArchie Cobbs */ 803cb3c7a5dSArchie Cobbs static int 804cb3c7a5dSArchie Cobbs ng_ksocket_rmnode(node_p node) 805cb3c7a5dSArchie Cobbs { 806cb3c7a5dSArchie Cobbs const priv_p priv = node->private; 807cb3c7a5dSArchie Cobbs 80819bff684SArchie Cobbs /* Close our socket (if any) */ 80919bff684SArchie Cobbs if (priv->so != NULL) { 810f8307e12SArchie Cobbs 811f8307e12SArchie Cobbs priv->so->so_upcall = NULL; 812f8307e12SArchie Cobbs priv->so->so_rcv.sb_flags &= ~SB_UPCALL; 81319bff684SArchie Cobbs soclose(priv->so); 81419bff684SArchie Cobbs priv->so = NULL; 81519bff684SArchie Cobbs } 81619bff684SArchie Cobbs 817cb3c7a5dSArchie Cobbs /* Take down netgraph node */ 818cb3c7a5dSArchie Cobbs node->flags |= NG_INVALID; 819cb3c7a5dSArchie Cobbs ng_cutlinks(node); 820cb3c7a5dSArchie Cobbs ng_unname(node); 821cb3c7a5dSArchie Cobbs bzero(priv, sizeof(*priv)); 822cb3c7a5dSArchie Cobbs FREE(priv, M_NETGRAPH); 823cb3c7a5dSArchie Cobbs node->private = NULL; 824cb3c7a5dSArchie Cobbs ng_unref(node); /* let the node escape */ 825cb3c7a5dSArchie Cobbs return (0); 826cb3c7a5dSArchie Cobbs } 827cb3c7a5dSArchie Cobbs 828cb3c7a5dSArchie Cobbs /* 829cb3c7a5dSArchie Cobbs * Hook disconnection 830cb3c7a5dSArchie Cobbs */ 831cb3c7a5dSArchie Cobbs static int 832cb3c7a5dSArchie Cobbs ng_ksocket_disconnect(hook_p hook) 833cb3c7a5dSArchie Cobbs { 83419bff684SArchie Cobbs KASSERT(hook->node->numhooks == 0, 83519bff684SArchie Cobbs ("%s: numhooks=%d?", __FUNCTION__, hook->node->numhooks)); 836cb3c7a5dSArchie Cobbs ng_rmnode(hook->node); 837cb3c7a5dSArchie Cobbs return (0); 838cb3c7a5dSArchie Cobbs } 839cb3c7a5dSArchie Cobbs 840cb3c7a5dSArchie Cobbs /************************************************************************ 841cb3c7a5dSArchie Cobbs HELPER STUFF 842cb3c7a5dSArchie Cobbs ************************************************************************/ 843cb3c7a5dSArchie Cobbs 844cb3c7a5dSArchie Cobbs /* 845cb3c7a5dSArchie Cobbs * When incoming data is appended to the socket, we get notified here. 846cb3c7a5dSArchie Cobbs */ 847cb3c7a5dSArchie Cobbs static void 848cb3c7a5dSArchie Cobbs ng_ksocket_incoming(struct socket *so, void *arg, int waitflag) 849cb3c7a5dSArchie Cobbs { 850cb3c7a5dSArchie Cobbs const node_p node = arg; 851cb3c7a5dSArchie Cobbs const priv_p priv = node->private; 852cb3c7a5dSArchie Cobbs meta_p meta = NULL; 853cb3c7a5dSArchie Cobbs struct sockaddr *nam; 854cb3c7a5dSArchie Cobbs struct mbuf *m; 855cb3c7a5dSArchie Cobbs struct uio auio; 85619bff684SArchie Cobbs int s, flags, error; 85719bff684SArchie Cobbs 85819bff684SArchie Cobbs s = splnet(); 859cb3c7a5dSArchie Cobbs 860cb3c7a5dSArchie Cobbs /* Sanity check */ 86119bff684SArchie Cobbs if ((node->flags & NG_INVALID) != 0) { 86219bff684SArchie Cobbs splx(s); 863cb3c7a5dSArchie Cobbs return; 86419bff684SArchie Cobbs } 865cb3c7a5dSArchie Cobbs KASSERT(so == priv->so, ("%s: wrong socket", __FUNCTION__)); 866cb3c7a5dSArchie Cobbs KASSERT(priv->hook != NULL, ("%s: no hook", __FUNCTION__)); 867cb3c7a5dSArchie Cobbs 868cb3c7a5dSArchie Cobbs /* Read and forward available mbuf's */ 869cb3c7a5dSArchie Cobbs auio.uio_procp = NULL; 870cb3c7a5dSArchie Cobbs auio.uio_resid = 1000000000; 871cb3c7a5dSArchie Cobbs flags = MSG_DONTWAIT; 872cb3c7a5dSArchie Cobbs do { 873cb3c7a5dSArchie Cobbs if ((error = (*so->so_proto->pr_usrreqs->pru_soreceive) 874f8307e12SArchie Cobbs (so, &nam, &auio, &m, (struct mbuf **)0, &flags)) == 0 875f8307e12SArchie Cobbs && m != NULL) { 876f8307e12SArchie Cobbs struct mbuf *n; 877f8307e12SArchie Cobbs 878f8307e12SArchie Cobbs /* Don't trust the various socket layers to get the 879f8307e12SArchie Cobbs packet header and length correct (eg. kern/15175) */ 880f8307e12SArchie Cobbs for (n = m, m->m_pkthdr.len = 0; n; n = n->m_next) 881f8307e12SArchie Cobbs m->m_pkthdr.len += n->m_len; 882cb3c7a5dSArchie Cobbs NG_SEND_DATA(error, priv->hook, m, meta); 883f8307e12SArchie Cobbs } 884cb3c7a5dSArchie Cobbs } while (error == 0 && m != NULL); 88519bff684SArchie Cobbs splx(s); 886cb3c7a5dSArchie Cobbs } 887cb3c7a5dSArchie Cobbs 888cb3c7a5dSArchie Cobbs /* 889cb3c7a5dSArchie Cobbs * Parse out either an integer value or an alias. 890cb3c7a5dSArchie Cobbs */ 891cb3c7a5dSArchie Cobbs static int 892cb3c7a5dSArchie Cobbs ng_ksocket_parse(const struct ng_ksocket_alias *aliases, 893cb3c7a5dSArchie Cobbs const char *s, int family) 894cb3c7a5dSArchie Cobbs { 895cb3c7a5dSArchie Cobbs int k, val; 89625792ef3SArchie Cobbs char *eptr; 897cb3c7a5dSArchie Cobbs 898cb3c7a5dSArchie Cobbs /* Try aliases */ 899cb3c7a5dSArchie Cobbs for (k = 0; aliases[k].name != NULL; k++) { 900cb3c7a5dSArchie Cobbs if (strcmp(s, aliases[k].name) == 0 901cb3c7a5dSArchie Cobbs && aliases[k].family == family) 902cb3c7a5dSArchie Cobbs return aliases[k].value; 903cb3c7a5dSArchie Cobbs } 904cb3c7a5dSArchie Cobbs 905cb3c7a5dSArchie Cobbs /* Try parsing as a number */ 906cb3c7a5dSArchie Cobbs val = (int)strtoul(s, &eptr, 10); 907f8307e12SArchie Cobbs if (val < 0 || *eptr != '\0') 908cb3c7a5dSArchie Cobbs return (-1); 909cb3c7a5dSArchie Cobbs return (val); 910cb3c7a5dSArchie Cobbs } 911cb3c7a5dSArchie Cobbs 912