5 * Copyright (c) 1996-1999 Whistle Communications, Inc.
8 * Subject to the following obligations and disclaimer of warranty, use and
9 * redistribution of this software, in source or object code forms, with or
10 * without modifications are expressly permitted by Whistle Communications;
11 * provided, however, that:
12 * 1. Any and all reproductions of the source or object code must include the
13 * copyright notice above and the following disclaimer of warranties; and
14 * 2. No rights are granted, in any manner or form, to use Whistle
15 * Communications, Inc. trademarks, including the mark "WHISTLE
16 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17 * such appears in the above copyright notice or in the software.
19 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
37 * Author: Archie Cobbs <archie@freebsd.org>
39 * $FreeBSD: src/sys/netgraph/ng_ksocket.c,v 1.5.2.14 2003/08/24 08:24:38 hsu Exp $
40 * $DragonFly: src/sys/netgraph/ksocket/ng_ksocket.c,v 1.16 2008/01/05 14:02:39 swildner Exp $
41 * $Whistle: ng_ksocket.c,v 1.1 1999/11/16 20:04:40 archie Exp $
45 * Kernel socket node type. This node type is basically a kernel-mode
46 * version of a socket... kindof like the reverse of the socket node type.
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
54 #include <sys/malloc.h>
55 #include <sys/ctype.h>
56 #include <sys/protosw.h>
57 #include <sys/errno.h>
58 #include <sys/fcntl.h>
59 #include <sys/socket.h>
60 #include <sys/socketvar.h>
61 #include <sys/socketops.h>
65 #include <sys/thread2.h>
66 #include <sys/socketvar2.h>
68 #include <netgraph/ng_message.h>
69 #include <netgraph/netgraph.h>
70 #include <netgraph/ng_parse.h>
71 #include "ng_ksocket.h"
73 #include <netinet/in.h>
74 #include <netproto/atalk/at.h>
76 #define OFFSETOF(s, e) ((char *)&((s *)0)->e - (char *)((s *)0))
77 #define SADATA_OFFSET (OFFSETOF(struct sockaddr, sa_data))
79 /* Node private data */
80 struct ng_ksocket_private {
84 LIST_HEAD(, ng_ksocket_private) embryos;
85 LIST_ENTRY(ng_ksocket_private) siblings;
87 u_int32_t response_token;
88 char response_addr[NG_PATHSIZ];
90 typedef struct ng_ksocket_private *priv_p;
92 /* Flags for priv_p */
93 #define KSF_CONNECTING 0x00000001 /* Waiting for connection complete */
94 #define KSF_ACCEPTING 0x00000002 /* Waiting for accept complete */
95 #define KSF_EOFSEEN 0x00000004 /* Have sent 0-length EOF mbuf */
96 #define KSF_CLONED 0x00000008 /* Cloned from an accepting socket */
97 #define KSF_EMBRYONIC 0x00000010 /* Cloned node with no hooks yet */
98 #define KSF_SENDING 0x00000020 /* Sending on socket */
100 /* Internal commands which we send to ourselves */
101 #define NGM_KSOCKET_INTERNAL_COOKIE (NGM_KSOCKET_COOKIE + 1)
104 NGM_KSOCKET_INTERNAL_UPCALL = 1
107 /* Netgraph node methods */
108 static ng_constructor_t ng_ksocket_constructor;
109 static ng_rcvmsg_t ng_ksocket_rcvmsg;
110 static ng_shutdown_t ng_ksocket_rmnode;
111 static ng_newhook_t ng_ksocket_newhook;
112 static ng_rcvdata_t ng_ksocket_rcvdata;
113 static ng_disconnect_t ng_ksocket_disconnect;
115 /* Alias structure */
116 struct ng_ksocket_alias {
122 /* Protocol family aliases */
123 static const struct ng_ksocket_alias ng_ksocket_families[] = {
124 { "local", PF_LOCAL },
126 { "inet6", PF_INET6 },
127 { "atalk", PF_APPLETALK },
133 /* Socket type aliases */
134 static const struct ng_ksocket_alias ng_ksocket_types[] = {
135 { "stream", SOCK_STREAM },
136 { "dgram", SOCK_DGRAM },
139 { "seqpacket", SOCK_SEQPACKET },
143 /* Protocol aliases */
144 static const struct ng_ksocket_alias ng_ksocket_protos[] = {
145 { "ip", IPPROTO_IP, PF_INET },
146 { "raw", IPPROTO_RAW, PF_INET },
147 { "icmp", IPPROTO_ICMP, PF_INET },
148 { "igmp", IPPROTO_IGMP, PF_INET },
149 { "tcp", IPPROTO_TCP, PF_INET },
150 { "udp", IPPROTO_UDP, PF_INET },
151 { "gre", IPPROTO_GRE, PF_INET },
152 { "esp", IPPROTO_ESP, PF_INET },
153 { "ah", IPPROTO_AH, PF_INET },
154 { "swipe", IPPROTO_SWIPE, PF_INET },
155 { "encap", IPPROTO_ENCAP, PF_INET },
156 { "divert", IPPROTO_DIVERT, PF_INET },
157 { "pim", IPPROTO_PIM, PF_INET },
158 { "ddp", ATPROTO_DDP, PF_APPLETALK },
159 { "aarp", ATPROTO_AARP, PF_APPLETALK },
163 /* Helper functions */
164 static int ng_ksocket_check_accept(priv_p);
165 static void ng_ksocket_finish_accept(priv_p, struct ng_mesg **);
166 static void ng_ksocket_incoming(struct socket *so, void *arg, int waitflag);
167 static int ng_ksocket_parse(const struct ng_ksocket_alias *aliases,
168 const char *s, int family);
170 /************************************************************************
171 STRUCT SOCKADDR PARSE TYPE
172 ************************************************************************/
174 /* Get the length of the data portion of a generic struct sockaddr */
176 ng_parse_generic_sockdata_getLength(const struct ng_parse_type *type,
177 const u_char *start, const u_char *buf)
179 const struct sockaddr *sa;
181 sa = (const struct sockaddr *)(buf - SADATA_OFFSET);
182 return (sa->sa_len < SADATA_OFFSET) ? 0 : sa->sa_len - SADATA_OFFSET;
185 /* Type for the variable length data portion of a generic struct sockaddr */
186 static const struct ng_parse_type ng_ksocket_generic_sockdata_type = {
187 &ng_parse_bytearray_type,
188 &ng_parse_generic_sockdata_getLength
191 /* Type for a generic struct sockaddr */
192 static const struct ng_parse_struct_field
193 ng_parse_generic_sockaddr_type_fields[] = {
194 { "len", &ng_parse_uint8_type },
195 { "family", &ng_parse_uint8_type },
196 { "data", &ng_ksocket_generic_sockdata_type },
199 static const struct ng_parse_type ng_ksocket_generic_sockaddr_type = {
200 &ng_parse_struct_type,
201 &ng_parse_generic_sockaddr_type_fields
204 /* Convert a struct sockaddr from ASCII to binary. If its a protocol
205 family that we specially handle, do that, otherwise defer to the
206 generic parse type ng_ksocket_generic_sockaddr_type. */
208 ng_ksocket_sockaddr_parse(const struct ng_parse_type *type,
209 const char *s, int *off, const u_char *const start,
210 u_char *const buf, int *buflen)
212 struct sockaddr *const sa = (struct sockaddr *)buf;
213 enum ng_parse_token tok;
218 /* If next token is a left curly brace, use generic parse type */
219 if ((tok = ng_parse_get_token(s, off, &len)) == T_LBRACE) {
220 return (*ng_ksocket_generic_sockaddr_type.supertype->parse)
221 (&ng_ksocket_generic_sockaddr_type,
222 s, off, start, buf, buflen);
225 /* Get socket address family followed by a slash */
226 while (isspace(s[*off]))
228 if ((t = index(s + *off, '/')) == NULL)
230 if ((len = t - (s + *off)) > sizeof(fambuf) - 1)
232 strncpy(fambuf, s + *off, len);
235 if ((family = ng_ksocket_parse(ng_ksocket_families, fambuf, 0)) == -1)
239 if (*buflen < SADATA_OFFSET)
241 sa->sa_family = family;
243 /* Set family-specific data and length */
244 switch (sa->sa_family) {
245 case PF_LOCAL: /* Get pathname */
247 const int pathoff = OFFSETOF(struct sockaddr_un, sun_path);
248 struct sockaddr_un *const sun = (struct sockaddr_un *)sa;
252 if ((path = ng_get_string_token(s, off, &toklen)) == NULL)
254 pathlen = strlen(path);
255 if (pathlen > SOCK_MAXADDRLEN) {
256 FREE(path, M_NETGRAPH);
259 if (*buflen < pathoff + pathlen) {
260 FREE(path, M_NETGRAPH);
264 bcopy(path, sun->sun_path, pathlen);
265 sun->sun_len = pathoff + pathlen;
266 FREE(path, M_NETGRAPH);
270 case PF_INET: /* Get an IP address with optional port */
272 struct sockaddr_in *const sin = (struct sockaddr_in *)sa;
275 /* Parse this: <ipaddress>[:port] */
276 for (i = 0; i < 4; i++) {
280 val = strtoul(s + *off, &eptr, 10);
281 if (val > 0xff || eptr == s + *off)
283 *off += (eptr - (s + *off));
284 ((u_char *)&sin->sin_addr)[i] = (u_char)val;
289 } else if (s[*off] == ':') {
291 val = strtoul(s + *off, &eptr, 10);
292 if (val > 0xffff || eptr == s + *off)
294 *off += (eptr - (s + *off));
295 sin->sin_port = htons(val);
299 bzero(&sin->sin_zero, sizeof(sin->sin_zero));
300 sin->sin_len = sizeof(*sin);
305 case PF_APPLETALK: /* XXX implement these someday */
315 *buflen = sa->sa_len;
319 /* Convert a struct sockaddr from binary to ASCII */
321 ng_ksocket_sockaddr_unparse(const struct ng_parse_type *type,
322 const u_char *data, int *off, char *cbuf, int cbuflen)
324 const struct sockaddr *sa = (const struct sockaddr *)(data + *off);
327 /* Output socket address, either in special or generic format */
328 switch (sa->sa_family) {
331 const int pathoff = OFFSETOF(struct sockaddr_un, sun_path);
332 const struct sockaddr_un *sun = (const struct sockaddr_un *)sa;
333 const int pathlen = sun->sun_len - pathoff;
334 char pathbuf[SOCK_MAXADDRLEN + 1];
337 bcopy(sun->sun_path, pathbuf, pathlen);
338 pathbuf[pathlen] = '\0';
339 if ((pathtoken = ng_encode_string(pathbuf)) == NULL)
341 slen += ksnprintf(cbuf, cbuflen, "local/%s", pathtoken);
342 FREE(pathtoken, M_NETGRAPH);
345 *off += sun->sun_len;
351 const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
353 slen += ksnprintf(cbuf, cbuflen, "inet/%d.%d.%d.%d",
354 ((const u_char *)&sin->sin_addr)[0],
355 ((const u_char *)&sin->sin_addr)[1],
356 ((const u_char *)&sin->sin_addr)[2],
357 ((const u_char *)&sin->sin_addr)[3]);
358 if (sin->sin_port != 0) {
359 slen += ksnprintf(cbuf + strlen(cbuf),
360 cbuflen - strlen(cbuf), ":%d",
361 (u_int)ntohs(sin->sin_port));
365 *off += sizeof(*sin);
370 case PF_APPLETALK: /* XXX implement these someday */
376 return (*ng_ksocket_generic_sockaddr_type.supertype->unparse)
377 (&ng_ksocket_generic_sockaddr_type,
378 data, off, cbuf, cbuflen);
382 /* Parse type for struct sockaddr */
383 static const struct ng_parse_type ng_ksocket_sockaddr_type = {
387 &ng_ksocket_sockaddr_parse,
388 &ng_ksocket_sockaddr_unparse,
389 NULL /* no such thing as a default struct sockaddr */
392 /************************************************************************
393 STRUCT NG_KSOCKET_SOCKOPT PARSE TYPE
394 ************************************************************************/
396 /* Get length of the struct ng_ksocket_sockopt value field, which is the
397 just the excess of the message argument portion over the length of
398 the struct ng_ksocket_sockopt. */
400 ng_parse_sockoptval_getLength(const struct ng_parse_type *type,
401 const u_char *start, const u_char *buf)
403 static const int offset = OFFSETOF(struct ng_ksocket_sockopt, value);
404 const struct ng_ksocket_sockopt *sopt;
405 const struct ng_mesg *msg;
407 sopt = (const struct ng_ksocket_sockopt *)(buf - offset);
408 msg = (const struct ng_mesg *)((const u_char *)sopt - sizeof(*msg));
409 return msg->header.arglen - sizeof(*sopt);
412 /* Parse type for the option value part of a struct ng_ksocket_sockopt
413 XXX Eventually, we should handle the different socket options specially.
414 XXX This would avoid byte order problems, eg an integer value of 1 is
415 XXX going to be "[1]" for little endian or "[3=1]" for big endian. */
416 static const struct ng_parse_type ng_ksocket_sockoptval_type = {
417 &ng_parse_bytearray_type,
418 &ng_parse_sockoptval_getLength
421 /* Parse type for struct ng_ksocket_sockopt */
422 static const struct ng_parse_struct_field ng_ksocket_sockopt_type_fields[]
423 = NG_KSOCKET_SOCKOPT_INFO(&ng_ksocket_sockoptval_type);
424 static const struct ng_parse_type ng_ksocket_sockopt_type = {
425 &ng_parse_struct_type,
426 &ng_ksocket_sockopt_type_fields
429 /* Parse type for struct ng_ksocket_accept */
430 static const struct ng_parse_struct_field ng_ksocket_accept_type_fields[]
431 = NGM_KSOCKET_ACCEPT_INFO;
432 static const struct ng_parse_type ng_ksocket_accept_type = {
433 &ng_parse_struct_type,
434 &ng_ksocket_accept_type_fields
437 /* List of commands and how to convert arguments to/from ASCII */
438 static const struct ng_cmdlist ng_ksocket_cmds[] = {
443 &ng_ksocket_sockaddr_type,
450 &ng_parse_int32_type,
458 &ng_ksocket_accept_type
464 &ng_ksocket_sockaddr_type,
472 &ng_ksocket_sockaddr_type
476 NGM_KSOCKET_GETPEERNAME,
479 &ng_ksocket_sockaddr_type
485 &ng_ksocket_sockopt_type,
492 &ng_ksocket_sockopt_type,
493 &ng_ksocket_sockopt_type
496 /* Internal commands */
498 NGM_KSOCKET_INTERNAL_COOKIE,
499 NGM_KSOCKET_INTERNAL_UPCALL,
507 /* Node type descriptor */
508 static struct ng_type ng_ksocket_typestruct = {
510 NG_KSOCKET_NODE_TYPE,
512 ng_ksocket_constructor,
520 ng_ksocket_disconnect,
523 NETGRAPH_INIT(ksocket, &ng_ksocket_typestruct);
525 #define ERROUT(x) do { error = (x); goto done; } while (0)
527 /************************************************************************
529 ************************************************************************/
532 * Node type constructor
535 ng_ksocket_constructor(node_p *nodep)
540 /* Allocate private structure */
541 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
545 /* Call generic node constructor */
546 if ((error = ng_make_node_common(&ng_ksocket_typestruct, nodep))) {
547 FREE(priv, M_NETGRAPH);
550 (*nodep)->private = priv;
552 LIST_INIT(&priv->embryos);
559 * Give our OK for a hook to be added. The hook name is of the
560 * form "<family>/<type>/<proto>" where the three components may
561 * be decimal numbers or else aliases from the above lists.
563 * Connecting a hook amounts to opening the socket. Disconnecting
564 * the hook closes the socket and destroys the node as well.
567 ng_ksocket_newhook(node_p node, hook_p hook, const char *name0)
569 struct thread *td = curthread->td_proc ? curthread : &thread0; /* XXX broken */
570 const priv_p priv = node->private;
572 char *s1, *s2, name[NG_HOOKSIZ];
573 int family, type, protocol, error;
575 /* Check if we're already connected */
576 if (priv->hook != NULL)
579 if (priv->flags & KSF_CLONED) {
580 if (priv->flags & KSF_EMBRYONIC) {
581 /* Remove ourselves from our parent's embryo list */
582 LIST_REMOVE(priv, siblings);
583 priv->flags &= ~KSF_EMBRYONIC;
586 /* Extract family, type, and protocol from hook name */
587 ksnprintf(name, sizeof(name), "%s", name0);
589 if ((s2 = index(s1, '/')) == NULL)
592 family = ng_ksocket_parse(ng_ksocket_families, s1, 0);
596 if ((s2 = index(s1, '/')) == NULL)
599 type = ng_ksocket_parse(ng_ksocket_types, s1, 0);
603 protocol = ng_ksocket_parse(ng_ksocket_protos, s1, family);
607 /* Create the socket */
608 error = socreate(family, &priv->so, type, protocol, td);
612 /* XXX call soreserve() ? */
614 /* Add our hook for incoming data and other events */
615 priv->so->so_upcallarg = (caddr_t)node;
616 priv->so->so_upcall = ng_ksocket_incoming;
617 atomic_set_int(&priv->so->so_rcv.ssb_flags, SSB_UPCALL);
618 atomic_set_int(&priv->so->so_snd.ssb_flags, SSB_UPCALL);
625 * On a cloned socket we may have already received one or more
626 * upcalls which we couldn't handle without a hook. Handle
627 * those now. We cannot call the upcall function directly
628 * from here, because until this function has returned our
629 * hook isn't connected. So we queue a message to ourselves
630 * which will cause the upcall function to be called a bit
633 if (priv->flags & KSF_CLONED) {
634 NG_MKMESSAGE(msg, NGM_KSOCKET_INTERNAL_COOKIE,
635 NGM_KSOCKET_INTERNAL_UPCALL, 0, M_NOWAIT);
637 ng_queue_msg(node, msg, ".:");
644 * Receive a control message
647 ng_ksocket_rcvmsg(node_p node, struct ng_mesg *msg,
648 const char *raddr, struct ng_mesg **rptr)
650 struct thread *td = curthread->td_proc ? curthread : &thread0; /* XXX broken */
651 const priv_p priv = node->private;
652 struct socket *const so = priv->so;
653 struct ng_mesg *resp = NULL;
656 switch (msg->header.typecookie) {
657 case NGM_KSOCKET_COOKIE:
658 switch (msg->header.cmd) {
659 case NGM_KSOCKET_BIND:
661 struct sockaddr *const sa
662 = (struct sockaddr *)msg->data;
665 if (msg->header.arglen < SADATA_OFFSET
666 || msg->header.arglen < sa->sa_len)
672 error = sobind(so, sa, td);
675 case NGM_KSOCKET_LISTEN:
678 if (msg->header.arglen != sizeof(int32_t))
684 error = solisten(so, *((int32_t *)msg->data), td);
688 case NGM_KSOCKET_ACCEPT:
691 if (msg->header.arglen != 0)
696 /* Make sure the socket is capable of accepting */
697 if (!(so->so_options & SO_ACCEPTCONN))
699 if (priv->flags & KSF_ACCEPTING)
702 error = ng_ksocket_check_accept(priv);
703 if (error != 0 && error != EWOULDBLOCK)
707 * If a connection is already complete, take it.
708 * Otherwise let the upcall function deal with
709 * the connection when it comes in.
711 priv->response_token = msg->header.token;
712 strcpy(priv->response_addr, raddr);
714 ng_ksocket_finish_accept(priv,
715 rptr != NULL ? &resp : NULL);
717 priv->flags |= KSF_ACCEPTING;
721 case NGM_KSOCKET_CONNECT:
723 struct sockaddr *const sa
724 = (struct sockaddr *)msg->data;
727 if (msg->header.arglen < SADATA_OFFSET
728 || msg->header.arglen < sa->sa_len)
734 if ((so->so_state & SS_ISCONNECTING) != 0)
736 if ((error = soconnect(so, sa, td)) != 0) {
737 soclrstate(so, SS_ISCONNECTING);
740 if ((so->so_state & SS_ISCONNECTING) != 0) {
741 /* We will notify the sender when we connect */
742 priv->response_token = msg->header.token;
743 strcpy(priv->response_addr, raddr);
744 priv->flags |= KSF_CONNECTING;
750 case NGM_KSOCKET_GETNAME:
751 case NGM_KSOCKET_GETPEERNAME:
753 struct sockaddr *sa = NULL;
757 if (msg->header.arglen != 0)
763 if (msg->header.cmd == NGM_KSOCKET_GETPEERNAME) {
765 & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0)
767 error = so_pru_peeraddr(so, &sa);
769 error = so_pru_sockaddr(so, &sa);
771 /* Get local or peer address */
774 len = (sa == NULL) ? 0 : sa->sa_len;
776 /* Send it back in a response */
777 NG_MKRESPONSE(resp, msg, len, M_NOWAIT);
782 bcopy(sa, resp->data, len);
791 case NGM_KSOCKET_GETOPT:
793 struct ng_ksocket_sockopt *ksopt =
794 (struct ng_ksocket_sockopt *)msg->data;
798 if (msg->header.arglen != sizeof(*ksopt))
803 /* Get response with room for option value */
804 NG_MKRESPONSE(resp, msg, sizeof(*ksopt)
805 + NG_KSOCKET_MAX_OPTLEN, M_NOWAIT);
809 /* Get socket option, and put value in the response */
810 sopt.sopt_dir = SOPT_GET;
811 sopt.sopt_level = ksopt->level;
812 sopt.sopt_name = ksopt->name;
814 sopt.sopt_valsize = NG_KSOCKET_MAX_OPTLEN;
815 ksopt = (struct ng_ksocket_sockopt *)resp->data;
816 sopt.sopt_val = ksopt->value;
817 if ((error = sogetopt(so, &sopt)) != 0) {
818 FREE(resp, M_NETGRAPH);
822 /* Set actual value length */
823 resp->header.arglen = sizeof(*ksopt)
828 case NGM_KSOCKET_SETOPT:
830 struct ng_ksocket_sockopt *const ksopt =
831 (struct ng_ksocket_sockopt *)msg->data;
832 const int valsize = msg->header.arglen - sizeof(*ksopt);
841 /* Set socket option */
842 sopt.sopt_dir = SOPT_SET;
843 sopt.sopt_level = ksopt->level;
844 sopt.sopt_name = ksopt->name;
845 sopt.sopt_val = ksopt->value;
846 sopt.sopt_valsize = valsize;
848 error = sosetopt(so, &sopt);
857 case NGM_KSOCKET_INTERNAL_COOKIE:
858 switch (msg->header.cmd) {
859 case NGM_KSOCKET_INTERNAL_UPCALL:
862 (*priv->so->so_upcall)(so, so->so_upcallarg, M_NOWAIT);
876 FREE(resp, M_NETGRAPH);
879 FREE(msg, M_NETGRAPH);
884 * Receive incoming data on our hook. Send it out the socket.
887 ng_ksocket_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
889 struct thread *td = curthread->td_proc ? curthread : &thread0; /* XXX broken */
890 const node_p node = hook->node;
891 const priv_p priv = node->private;
892 struct socket *const so = priv->so;
893 struct sockaddr *sa = NULL;
896 /* Avoid reentrantly sending on the socket */
897 if ((priv->flags & KSF_SENDING) != 0) {
898 NG_FREE_DATA(m, meta);
902 /* If any meta info, look for peer socket address */
904 struct meta_field_header *field;
906 /* Look for peer socket address */
907 for (field = &meta->options[0];
908 (caddr_t)field < (caddr_t)meta + meta->used_len;
909 field = (struct meta_field_header *)
910 ((caddr_t)field + field->len)) {
911 if (field->cookie != NGM_KSOCKET_COOKIE
912 || field->type != NG_KSOCKET_META_SOCKADDR)
914 sa = (struct sockaddr *)field->data;
920 priv->flags |= KSF_SENDING;
921 error = so_pru_sosend(so, sa, NULL, m, NULL, 0, td);
922 priv->flags &= ~KSF_SENDING;
924 /* Clean up and exit */
933 ng_ksocket_rmnode(node_p node)
935 const priv_p priv = node->private;
938 /* Close our socket (if any) */
939 if (priv->so != NULL) {
940 priv->so->so_upcall = NULL;
941 atomic_clear_int(&priv->so->so_rcv.ssb_flags, SSB_UPCALL);
942 atomic_clear_int(&priv->so->so_snd.ssb_flags, SSB_UPCALL);
943 soclose(priv->so, FNONBLOCK);
947 /* If we are an embryo, take ourselves out of the parent's list */
948 if (priv->flags & KSF_EMBRYONIC) {
949 LIST_REMOVE(priv, siblings);
950 priv->flags &= ~KSF_EMBRYONIC;
953 /* Remove any embryonic children we have */
954 while (!LIST_EMPTY(&priv->embryos)) {
955 embryo = LIST_FIRST(&priv->embryos);
956 ng_rmnode(embryo->node);
959 /* Take down netgraph node */
960 node->flags |= NG_INVALID;
963 bzero(priv, sizeof(*priv));
964 FREE(priv, M_NETGRAPH);
965 node->private = NULL;
966 ng_unref(node); /* let the node escape */
974 ng_ksocket_disconnect(hook_p hook)
976 KASSERT(hook->node->numhooks == 0,
977 ("%s: numhooks=%d?", __func__, hook->node->numhooks));
978 ng_rmnode(hook->node);
982 /************************************************************************
984 ************************************************************************/
987 * When incoming data is appended to the socket, we get notified here.
988 * This is also called whenever a significant event occurs for the socket.
991 ng_ksocket_incoming(struct socket *so, void *arg, int waitflag)
993 const node_p node = arg;
994 const priv_p priv = node->private;
995 struct ng_mesg *response;
1001 if ((node->flags & NG_INVALID) != 0) {
1005 KASSERT(so == priv->so, ("%s: wrong socket", __func__));
1007 /* Check whether a pending connect operation has completed */
1008 if (priv->flags & KSF_CONNECTING) {
1009 if ((error = so->so_error) != 0) {
1011 soclrstate(so, SS_ISCONNECTING);
1013 if (!(so->so_state & SS_ISCONNECTING)) {
1014 NG_MKMESSAGE(response, NGM_KSOCKET_COOKIE,
1015 NGM_KSOCKET_CONNECT, sizeof(int32_t), waitflag);
1016 if (response != NULL) {
1017 response->header.flags |= NGF_RESP;
1018 response->header.token = priv->response_token;
1019 *(int32_t *)response->data = error;
1021 * XXX We use ng_queue_msg here because we are
1022 * being called from deep in the bowels of the TCP
1023 * stack. Is this right, or should we let the
1024 * receiver of the message worry about that?
1026 ng_queue_msg(node, response,
1027 priv->response_addr);
1029 priv->flags &= ~KSF_CONNECTING;
1033 /* Check whether a pending accept operation has completed */
1034 if (priv->flags & KSF_ACCEPTING) {
1035 error = ng_ksocket_check_accept(priv);
1036 if (error != EWOULDBLOCK)
1037 priv->flags &= ~KSF_ACCEPTING;
1039 ng_ksocket_finish_accept(priv, NULL);
1043 * If we don't have a hook, we must handle data events later. When
1044 * the hook gets created and is connected, this upcall function
1045 * will be called again.
1047 if (priv->hook == NULL) {
1052 /* Read and forward available mbuf's */
1054 struct sockaddr *sa = NULL;
1060 sbinit(&sio, 1000000000);
1061 flags = MSG_DONTWAIT;
1063 /* Try to get next packet from socket */
1064 error = so_pru_soreceive(so,
1065 ((so->so_state & SS_ISCONNECTED) ? NULL : &sa),
1066 NULL, &sio, NULL, &flags);
1070 /* See if we got anything */
1071 if (sio.sb_mb == NULL) {
1077 /* Don't trust the various socket layers to get the
1078 packet header and length correct (eg. kern/15175) */
1079 sio.sb_mb->m_pkthdr.len = 0;
1080 for (n = sio.sb_mb; n != NULL; n = n->m_next)
1081 sio.sb_mb->m_pkthdr.len += n->m_len;
1083 /* Put peer's socket address (if any) into a meta info blob */
1085 struct meta_field_header *mhead;
1088 len = sizeof(*meta) + sizeof(*mhead) + sa->sa_len;
1089 MALLOC(meta, meta_p, len, M_NETGRAPH, M_NOWAIT);
1094 mhead = &meta->options[0];
1095 bzero(meta, sizeof(*meta));
1096 bzero(mhead, sizeof(*mhead));
1097 meta->allocated_len = len;
1098 meta->used_len = len;
1099 mhead->cookie = NGM_KSOCKET_COOKIE;
1100 mhead->type = NG_KSOCKET_META_SOCKADDR;
1101 mhead->len = sizeof(*mhead) + sa->sa_len;
1102 bcopy(sa, mhead->data, sa->sa_len);
1105 sendit: /* Forward data with optional peer sockaddr as meta info */
1106 NG_SEND_DATA(error, priv->hook, sio.sb_mb, meta);
1110 * If the peer has closed the connection, forward a 0-length mbuf
1111 * to indicate end-of-file.
1113 if (so->so_state & SS_CANTRCVMORE && !(priv->flags & KSF_EOFSEEN)) {
1116 MGETHDR(m, waitflag, MT_DATA);
1118 m->m_len = m->m_pkthdr.len = 0;
1119 NG_SEND_DATA_ONLY(error, priv->hook, m);
1121 priv->flags |= KSF_EOFSEEN;
1128 * Check for a completed incoming connection and return 0 if one is found.
1129 * Otherwise return the appropriate error code.
1132 ng_ksocket_check_accept(priv_p priv)
1134 struct socket *const head = priv->so;
1137 lwkt_gettoken(&head->so_rcv.ssb_token);
1138 if ((error = head->so_error) != 0) {
1140 lwkt_reltoken(&head->so_rcv.ssb_token);
1143 if (TAILQ_EMPTY(&head->so_comp)) {
1144 if (head->so_state & SS_CANTRCVMORE)
1145 error = ECONNABORTED;
1147 error = EWOULDBLOCK;
1149 lwkt_reltoken(&head->so_rcv.ssb_token);
1154 * Handle the first completed incoming connection, assumed to be already
1155 * on the socket's so_comp queue.
1158 ng_ksocket_finish_accept(priv_p priv, struct ng_mesg **rptr)
1160 struct socket *const head = priv->so;
1162 struct sockaddr *sa = NULL;
1163 struct ng_mesg *resp;
1164 struct ng_ksocket_accept *resp_data;
1169 lwkt_gettoken(&head->so_rcv.ssb_token);
1170 so = TAILQ_FIRST(&head->so_comp);
1171 if (so == NULL) { /* Should never happen */
1172 lwkt_reltoken(&head->so_rcv.ssb_token);
1175 TAILQ_REMOVE(&head->so_comp, so, so_list);
1177 soclrstate(so, SS_COMP);
1181 lwkt_reltoken(&head->so_rcv.ssb_token);
1183 /* XXX KNOTE(&head->so_rcv.ssb_sel.si_note, 0); */
1187 len = OFFSETOF(struct ng_ksocket_accept, addr);
1191 NG_MKMESSAGE(resp, NGM_KSOCKET_COOKIE, NGM_KSOCKET_ACCEPT, len,
1194 soclose(so, FNONBLOCK);
1197 resp->header.flags |= NGF_RESP;
1198 resp->header.token = priv->response_token;
1200 /* Clone a ksocket node to wrap the new socket */
1201 if (ng_ksocket_constructor(&node2) != 0) {
1202 FREE(resp, M_NETGRAPH);
1203 soclose(so, FNONBLOCK);
1206 priv2 = (priv_p)node2->private;
1208 priv2->flags |= KSF_CLONED | KSF_EMBRYONIC;
1211 * Insert the cloned node into a list of embryonic children
1212 * on the parent node. When a hook is created on the cloned
1213 * node it will be removed from this list. When the parent
1214 * is destroyed it will destroy any embryonic children it has.
1216 LIST_INSERT_HEAD(&priv->embryos, priv2, siblings);
1218 so->so_upcallarg = (caddr_t)node2;
1219 so->so_upcall = ng_ksocket_incoming;
1220 atomic_set_int(&so->so_rcv.ssb_flags, SSB_UPCALL);
1221 atomic_set_int(&so->so_snd.ssb_flags, SSB_UPCALL);
1223 /* Fill in the response data and send it or return it to the caller */
1224 resp_data = (struct ng_ksocket_accept *)resp->data;
1225 resp_data->nodeid = node2->ID;
1227 bcopy(sa, &resp_data->addr, sa->sa_len);
1231 ng_queue_msg(priv->node, resp, priv->response_addr);
1239 * Parse out either an integer value or an alias.
1242 ng_ksocket_parse(const struct ng_ksocket_alias *aliases,
1243 const char *s, int family)
1249 for (k = 0; aliases[k].name != NULL; k++) {
1250 if (strcmp(s, aliases[k].name) == 0
1251 && aliases[k].family == family)
1252 return aliases[k].value;
1255 /* Try parsing as a number */
1256 val = (int)strtoul(s, &eptr, 10);
1257 if (val < 0 || *eptr != '\0')