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.13 2003/04/28 20:41:09 archie Exp $
40 * $DragonFly: src/sys/netgraph/ksocket/ng_ksocket.c,v 1.3 2003/06/25 03:56:03 dillon 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/socket.h>
59 #include <sys/socketvar.h>
63 #include <netgraph/ng_message.h>
64 #include <netgraph/netgraph.h>
65 #include <netgraph/ng_parse.h>
66 #include <netgraph/ng_ksocket.h>
68 #include <netinet/in.h>
69 #include <netatalk/at.h>
71 #define OFFSETOF(s, e) ((char *)&((s *)0)->e - (char *)((s *)0))
72 #define SADATA_OFFSET (OFFSETOF(struct sockaddr, sa_data))
74 /* Node private data */
75 struct ng_ksocket_private {
79 LIST_HEAD(, ng_ksocket_private) embryos;
80 LIST_ENTRY(ng_ksocket_private) siblings;
82 u_int32_t response_token;
83 char response_addr[NG_PATHLEN+1];
85 typedef struct ng_ksocket_private *priv_p;
87 /* Flags for priv_p */
88 #define KSF_CONNECTING 0x00000001 /* Waiting for connection complete */
89 #define KSF_ACCEPTING 0x00000002 /* Waiting for accept complete */
90 #define KSF_EOFSEEN 0x00000004 /* Have sent 0-length EOF mbuf */
91 #define KSF_CLONED 0x00000008 /* Cloned from an accepting socket */
92 #define KSF_EMBRYONIC 0x00000010 /* Cloned node with no hooks yet */
93 #define KSF_SENDING 0x00000020 /* Sending on socket */
95 /* Internal commands which we send to ourselves */
96 #define NGM_KSOCKET_INTERNAL_COOKIE (NGM_KSOCKET_COOKIE + 1)
99 NGM_KSOCKET_INTERNAL_UPCALL = 1
102 /* Netgraph node methods */
103 static ng_constructor_t ng_ksocket_constructor;
104 static ng_rcvmsg_t ng_ksocket_rcvmsg;
105 static ng_shutdown_t ng_ksocket_rmnode;
106 static ng_newhook_t ng_ksocket_newhook;
107 static ng_rcvdata_t ng_ksocket_rcvdata;
108 static ng_disconnect_t ng_ksocket_disconnect;
110 /* Alias structure */
111 struct ng_ksocket_alias {
117 /* Protocol family aliases */
118 static const struct ng_ksocket_alias ng_ksocket_families[] = {
119 { "local", PF_LOCAL },
121 { "inet6", PF_INET6 },
122 { "atalk", PF_APPLETALK },
128 /* Socket type aliases */
129 static const struct ng_ksocket_alias ng_ksocket_types[] = {
130 { "stream", SOCK_STREAM },
131 { "dgram", SOCK_DGRAM },
134 { "seqpacket", SOCK_SEQPACKET },
138 /* Protocol aliases */
139 static const struct ng_ksocket_alias ng_ksocket_protos[] = {
140 { "ip", IPPROTO_IP, PF_INET },
141 { "raw", IPPROTO_RAW, PF_INET },
142 { "icmp", IPPROTO_ICMP, PF_INET },
143 { "igmp", IPPROTO_IGMP, PF_INET },
144 { "tcp", IPPROTO_TCP, PF_INET },
145 { "udp", IPPROTO_UDP, PF_INET },
146 { "gre", IPPROTO_GRE, PF_INET },
147 { "esp", IPPROTO_ESP, PF_INET },
148 { "ah", IPPROTO_AH, PF_INET },
149 { "swipe", IPPROTO_SWIPE, PF_INET },
150 { "encap", IPPROTO_ENCAP, PF_INET },
151 { "divert", IPPROTO_DIVERT, PF_INET },
152 { "ddp", ATPROTO_DDP, PF_APPLETALK },
153 { "aarp", ATPROTO_AARP, PF_APPLETALK },
157 /* Helper functions */
158 static int ng_ksocket_check_accept(priv_p);
159 static void ng_ksocket_finish_accept(priv_p, struct ng_mesg **);
160 static void ng_ksocket_incoming(struct socket *so, void *arg, int waitflag);
161 static int ng_ksocket_parse(const struct ng_ksocket_alias *aliases,
162 const char *s, int family);
164 /************************************************************************
165 STRUCT SOCKADDR PARSE TYPE
166 ************************************************************************/
168 /* Get the length of the data portion of a generic struct sockaddr */
170 ng_parse_generic_sockdata_getLength(const struct ng_parse_type *type,
171 const u_char *start, const u_char *buf)
173 const struct sockaddr *sa;
175 sa = (const struct sockaddr *)(buf - SADATA_OFFSET);
176 return (sa->sa_len < SADATA_OFFSET) ? 0 : sa->sa_len - SADATA_OFFSET;
179 /* Type for the variable length data portion of a generic struct sockaddr */
180 static const struct ng_parse_type ng_ksocket_generic_sockdata_type = {
181 &ng_parse_bytearray_type,
182 &ng_parse_generic_sockdata_getLength
185 /* Type for a generic struct sockaddr */
186 static const struct ng_parse_struct_field
187 ng_parse_generic_sockaddr_type_fields[] = {
188 { "len", &ng_parse_uint8_type },
189 { "family", &ng_parse_uint8_type },
190 { "data", &ng_ksocket_generic_sockdata_type },
193 static const struct ng_parse_type ng_ksocket_generic_sockaddr_type = {
194 &ng_parse_struct_type,
195 &ng_parse_generic_sockaddr_type_fields
198 /* Convert a struct sockaddr from ASCII to binary. If its a protocol
199 family that we specially handle, do that, otherwise defer to the
200 generic parse type ng_ksocket_generic_sockaddr_type. */
202 ng_ksocket_sockaddr_parse(const struct ng_parse_type *type,
203 const char *s, int *off, const u_char *const start,
204 u_char *const buf, int *buflen)
206 struct sockaddr *const sa = (struct sockaddr *)buf;
207 enum ng_parse_token tok;
212 /* If next token is a left curly brace, use generic parse type */
213 if ((tok = ng_parse_get_token(s, off, &len)) == T_LBRACE) {
214 return (*ng_ksocket_generic_sockaddr_type.supertype->parse)
215 (&ng_ksocket_generic_sockaddr_type,
216 s, off, start, buf, buflen);
219 /* Get socket address family followed by a slash */
220 while (isspace(s[*off]))
222 if ((t = index(s + *off, '/')) == NULL)
224 if ((len = t - (s + *off)) > sizeof(fambuf) - 1)
226 strncpy(fambuf, s + *off, len);
229 if ((family = ng_ksocket_parse(ng_ksocket_families, fambuf, 0)) == -1)
233 if (*buflen < SADATA_OFFSET)
235 sa->sa_family = family;
237 /* Set family-specific data and length */
238 switch (sa->sa_family) {
239 case PF_LOCAL: /* Get pathname */
241 const int pathoff = OFFSETOF(struct sockaddr_un, sun_path);
242 struct sockaddr_un *const sun = (struct sockaddr_un *)sa;
246 if ((path = ng_get_string_token(s, off, &toklen)) == NULL)
248 pathlen = strlen(path);
249 if (pathlen > SOCK_MAXADDRLEN) {
250 FREE(path, M_NETGRAPH);
253 if (*buflen < pathoff + pathlen) {
254 FREE(path, M_NETGRAPH);
258 bcopy(path, sun->sun_path, pathlen);
259 sun->sun_len = pathoff + pathlen;
260 FREE(path, M_NETGRAPH);
264 case PF_INET: /* Get an IP address with optional port */
266 struct sockaddr_in *const sin = (struct sockaddr_in *)sa;
269 /* Parse this: <ipaddress>[:port] */
270 for (i = 0; i < 4; i++) {
274 val = strtoul(s + *off, &eptr, 10);
275 if (val > 0xff || eptr == s + *off)
277 *off += (eptr - (s + *off));
278 ((u_char *)&sin->sin_addr)[i] = (u_char)val;
283 } else if (s[*off] == ':') {
285 val = strtoul(s + *off, &eptr, 10);
286 if (val > 0xffff || eptr == s + *off)
288 *off += (eptr - (s + *off));
289 sin->sin_port = htons(val);
293 bzero(&sin->sin_zero, sizeof(sin->sin_zero));
294 sin->sin_len = sizeof(*sin);
299 case PF_APPLETALK: /* XXX implement these someday */
309 *buflen = sa->sa_len;
313 /* Convert a struct sockaddr from binary to ASCII */
315 ng_ksocket_sockaddr_unparse(const struct ng_parse_type *type,
316 const u_char *data, int *off, char *cbuf, int cbuflen)
318 const struct sockaddr *sa = (const struct sockaddr *)(data + *off);
321 /* Output socket address, either in special or generic format */
322 switch (sa->sa_family) {
325 const int pathoff = OFFSETOF(struct sockaddr_un, sun_path);
326 const struct sockaddr_un *sun = (const struct sockaddr_un *)sa;
327 const int pathlen = sun->sun_len - pathoff;
328 char pathbuf[SOCK_MAXADDRLEN + 1];
331 bcopy(sun->sun_path, pathbuf, pathlen);
332 pathbuf[pathlen] = '\0';
333 if ((pathtoken = ng_encode_string(pathbuf)) == NULL)
335 slen += snprintf(cbuf, cbuflen, "local/%s", pathtoken);
336 FREE(pathtoken, M_NETGRAPH);
339 *off += sun->sun_len;
345 const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
347 slen += snprintf(cbuf, cbuflen, "inet/%d.%d.%d.%d",
348 ((const u_char *)&sin->sin_addr)[0],
349 ((const u_char *)&sin->sin_addr)[1],
350 ((const u_char *)&sin->sin_addr)[2],
351 ((const u_char *)&sin->sin_addr)[3]);
352 if (sin->sin_port != 0) {
353 slen += snprintf(cbuf + strlen(cbuf),
354 cbuflen - strlen(cbuf), ":%d",
355 (u_int)ntohs(sin->sin_port));
359 *off += sizeof(*sin);
364 case PF_APPLETALK: /* XXX implement these someday */
370 return (*ng_ksocket_generic_sockaddr_type.supertype->unparse)
371 (&ng_ksocket_generic_sockaddr_type,
372 data, off, cbuf, cbuflen);
376 /* Parse type for struct sockaddr */
377 static const struct ng_parse_type ng_ksocket_sockaddr_type = {
381 &ng_ksocket_sockaddr_parse,
382 &ng_ksocket_sockaddr_unparse,
383 NULL /* no such thing as a default struct sockaddr */
386 /************************************************************************
387 STRUCT NG_KSOCKET_SOCKOPT PARSE TYPE
388 ************************************************************************/
390 /* Get length of the struct ng_ksocket_sockopt value field, which is the
391 just the excess of the message argument portion over the length of
392 the struct ng_ksocket_sockopt. */
394 ng_parse_sockoptval_getLength(const struct ng_parse_type *type,
395 const u_char *start, const u_char *buf)
397 static const int offset = OFFSETOF(struct ng_ksocket_sockopt, value);
398 const struct ng_ksocket_sockopt *sopt;
399 const struct ng_mesg *msg;
401 sopt = (const struct ng_ksocket_sockopt *)(buf - offset);
402 msg = (const struct ng_mesg *)((const u_char *)sopt - sizeof(*msg));
403 return msg->header.arglen - sizeof(*sopt);
406 /* Parse type for the option value part of a struct ng_ksocket_sockopt
407 XXX Eventually, we should handle the different socket options specially.
408 XXX This would avoid byte order problems, eg an integer value of 1 is
409 XXX going to be "[1]" for little endian or "[3=1]" for big endian. */
410 static const struct ng_parse_type ng_ksocket_sockoptval_type = {
411 &ng_parse_bytearray_type,
412 &ng_parse_sockoptval_getLength
415 /* Parse type for struct ng_ksocket_sockopt */
416 static const struct ng_parse_struct_field ng_ksocket_sockopt_type_fields[]
417 = NG_KSOCKET_SOCKOPT_INFO(&ng_ksocket_sockoptval_type);
418 static const struct ng_parse_type ng_ksocket_sockopt_type = {
419 &ng_parse_struct_type,
420 &ng_ksocket_sockopt_type_fields
423 /* Parse type for struct ng_ksocket_accept */
424 static const struct ng_parse_struct_field ng_ksocket_accept_type_fields[]
425 = NGM_KSOCKET_ACCEPT_INFO;
426 static const struct ng_parse_type ng_ksocket_accept_type = {
427 &ng_parse_struct_type,
428 &ng_ksocket_accept_type_fields
431 /* List of commands and how to convert arguments to/from ASCII */
432 static const struct ng_cmdlist ng_ksocket_cmds[] = {
437 &ng_ksocket_sockaddr_type,
444 &ng_parse_int32_type,
452 &ng_ksocket_accept_type
458 &ng_ksocket_sockaddr_type,
466 &ng_ksocket_sockaddr_type
470 NGM_KSOCKET_GETPEERNAME,
473 &ng_ksocket_sockaddr_type
479 &ng_ksocket_sockopt_type,
486 &ng_ksocket_sockopt_type,
487 &ng_ksocket_sockopt_type
490 /* Internal commands */
492 NGM_KSOCKET_INTERNAL_COOKIE,
493 NGM_KSOCKET_INTERNAL_UPCALL,
501 /* Node type descriptor */
502 static struct ng_type ng_ksocket_typestruct = {
504 NG_KSOCKET_NODE_TYPE,
506 ng_ksocket_constructor,
514 ng_ksocket_disconnect,
517 NETGRAPH_INIT(ksocket, &ng_ksocket_typestruct);
519 #define ERROUT(x) do { error = (x); goto done; } while (0)
521 /************************************************************************
523 ************************************************************************/
526 * Node type constructor
529 ng_ksocket_constructor(node_p *nodep)
534 /* Allocate private structure */
535 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT);
538 bzero(priv, sizeof(*priv));
540 /* Call generic node constructor */
541 if ((error = ng_make_node_common(&ng_ksocket_typestruct, nodep))) {
542 FREE(priv, M_NETGRAPH);
545 (*nodep)->private = priv;
547 LIST_INIT(&priv->embryos);
554 * Give our OK for a hook to be added. The hook name is of the
555 * form "<family>/<type>/<proto>" where the three components may
556 * be decimal numbers or else aliases from the above lists.
558 * Connecting a hook amounts to opening the socket. Disconnecting
559 * the hook closes the socket and destroys the node as well.
562 ng_ksocket_newhook(node_p node, hook_p hook, const char *name0)
564 struct thread *td = curthread; /* XXX broken */
565 const priv_p priv = node->private;
567 char *s1, *s2, name[NG_HOOKLEN+1];
568 int family, type, protocol, error;
570 /* Check if we're already connected */
571 if (priv->hook != NULL)
574 if (priv->flags & KSF_CLONED) {
575 if (priv->flags & KSF_EMBRYONIC) {
576 /* Remove ourselves from our parent's embryo list */
577 LIST_REMOVE(priv, siblings);
578 priv->flags &= ~KSF_EMBRYONIC;
581 /* Extract family, type, and protocol from hook name */
582 snprintf(name, sizeof(name), "%s", name0);
584 if ((s2 = index(s1, '/')) == NULL)
587 family = ng_ksocket_parse(ng_ksocket_families, s1, 0);
591 if ((s2 = index(s1, '/')) == NULL)
594 type = ng_ksocket_parse(ng_ksocket_types, s1, 0);
598 protocol = ng_ksocket_parse(ng_ksocket_protos, s1, family);
602 /* Create the socket */
603 error = socreate(family, &priv->so, type, protocol, td);
607 /* XXX call soreserve() ? */
609 /* Add our hook for incoming data and other events */
610 priv->so->so_upcallarg = (caddr_t)node;
611 priv->so->so_upcall = ng_ksocket_incoming;
612 priv->so->so_rcv.sb_flags |= SB_UPCALL;
613 priv->so->so_snd.sb_flags |= SB_UPCALL;
614 priv->so->so_state |= SS_NBIO;
621 * On a cloned socket we may have already received one or more
622 * upcalls which we couldn't handle without a hook. Handle
623 * those now. We cannot call the upcall function directly
624 * from here, because until this function has returned our
625 * hook isn't connected. So we queue a message to ourselves
626 * which will cause the upcall function to be called a bit
629 if (priv->flags & KSF_CLONED) {
630 NG_MKMESSAGE(msg, NGM_KSOCKET_INTERNAL_COOKIE,
631 NGM_KSOCKET_INTERNAL_UPCALL, 0, M_NOWAIT);
633 ng_queue_msg(node, msg, ".:");
640 * Receive a control message
643 ng_ksocket_rcvmsg(node_p node, struct ng_mesg *msg,
644 const char *raddr, struct ng_mesg **rptr)
646 struct thread *td = curthread; /* XXX broken */
647 const priv_p priv = node->private;
648 struct socket *const so = priv->so;
649 struct ng_mesg *resp = NULL;
652 switch (msg->header.typecookie) {
653 case NGM_KSOCKET_COOKIE:
654 switch (msg->header.cmd) {
655 case NGM_KSOCKET_BIND:
657 struct sockaddr *const sa
658 = (struct sockaddr *)msg->data;
661 if (msg->header.arglen < SADATA_OFFSET
662 || msg->header.arglen < sa->sa_len)
668 error = sobind(so, sa, td);
671 case NGM_KSOCKET_LISTEN:
674 if (msg->header.arglen != sizeof(int32_t))
680 error = solisten(so, *((int32_t *)msg->data), td);
684 case NGM_KSOCKET_ACCEPT:
687 if (msg->header.arglen != 0)
692 /* Make sure the socket is capable of accepting */
693 if (!(so->so_options & SO_ACCEPTCONN))
695 if (priv->flags & KSF_ACCEPTING)
698 error = ng_ksocket_check_accept(priv);
699 if (error != 0 && error != EWOULDBLOCK)
703 * If a connection is already complete, take it.
704 * Otherwise let the upcall function deal with
705 * the connection when it comes in.
707 priv->response_token = msg->header.token;
708 strcpy(priv->response_addr, raddr);
710 ng_ksocket_finish_accept(priv,
711 rptr != NULL ? &resp : NULL);
713 priv->flags |= KSF_ACCEPTING;
717 case NGM_KSOCKET_CONNECT:
719 struct sockaddr *const sa
720 = (struct sockaddr *)msg->data;
723 if (msg->header.arglen < SADATA_OFFSET
724 || msg->header.arglen < sa->sa_len)
730 if ((so->so_state & SS_ISCONNECTING) != 0)
732 if ((error = soconnect(so, sa, td)) != 0) {
733 so->so_state &= ~SS_ISCONNECTING;
736 if ((so->so_state & SS_ISCONNECTING) != 0) {
737 /* We will notify the sender when we connect */
738 priv->response_token = msg->header.token;
739 strcpy(priv->response_addr, raddr);
740 priv->flags |= KSF_CONNECTING;
746 case NGM_KSOCKET_GETNAME:
747 case NGM_KSOCKET_GETPEERNAME:
749 int (*func)(struct socket *so, struct sockaddr **nam);
750 struct sockaddr *sa = NULL;
754 if (msg->header.arglen != 0)
760 if (msg->header.cmd == NGM_KSOCKET_GETPEERNAME) {
762 & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0)
764 func = so->so_proto->pr_usrreqs->pru_peeraddr;
766 func = so->so_proto->pr_usrreqs->pru_sockaddr;
768 /* Get local or peer address */
769 if ((error = (*func)(so, &sa)) != 0)
771 len = (sa == NULL) ? 0 : sa->sa_len;
773 /* Send it back in a response */
774 NG_MKRESPONSE(resp, msg, len, M_NOWAIT);
779 bcopy(sa, resp->data, len);
788 case NGM_KSOCKET_GETOPT:
790 struct ng_ksocket_sockopt *ksopt =
791 (struct ng_ksocket_sockopt *)msg->data;
795 if (msg->header.arglen != sizeof(*ksopt))
800 /* Get response with room for option value */
801 NG_MKRESPONSE(resp, msg, sizeof(*ksopt)
802 + NG_KSOCKET_MAX_OPTLEN, M_NOWAIT);
806 /* Get socket option, and put value in the response */
807 sopt.sopt_dir = SOPT_GET;
808 sopt.sopt_level = ksopt->level;
809 sopt.sopt_name = ksopt->name;
811 sopt.sopt_valsize = NG_KSOCKET_MAX_OPTLEN;
812 ksopt = (struct ng_ksocket_sockopt *)resp->data;
813 sopt.sopt_val = ksopt->value;
814 if ((error = sogetopt(so, &sopt)) != 0) {
815 FREE(resp, M_NETGRAPH);
819 /* Set actual value length */
820 resp->header.arglen = sizeof(*ksopt)
825 case NGM_KSOCKET_SETOPT:
827 struct ng_ksocket_sockopt *const ksopt =
828 (struct ng_ksocket_sockopt *)msg->data;
829 const int valsize = msg->header.arglen - sizeof(*ksopt);
838 /* Set socket option */
839 sopt.sopt_dir = SOPT_SET;
840 sopt.sopt_level = ksopt->level;
841 sopt.sopt_name = ksopt->name;
842 sopt.sopt_val = ksopt->value;
843 sopt.sopt_valsize = valsize;
845 error = sosetopt(so, &sopt);
854 case NGM_KSOCKET_INTERNAL_COOKIE:
855 switch (msg->header.cmd) {
856 case NGM_KSOCKET_INTERNAL_UPCALL:
859 (*priv->so->so_upcall)(so, so->so_upcallarg, M_NOWAIT);
873 FREE(resp, M_NETGRAPH);
876 FREE(msg, M_NETGRAPH);
881 * Receive incoming data on our hook. Send it out the socket.
884 ng_ksocket_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
886 struct thread *td = curthread; /* XXX broken */
887 const node_p node = hook->node;
888 const priv_p priv = node->private;
889 struct socket *const so = priv->so;
890 struct sockaddr *sa = NULL;
893 /* Avoid reentrantly sending on the socket */
894 if ((priv->flags & KSF_SENDING) != 0) {
895 NG_FREE_DATA(m, meta);
899 /* If any meta info, look for peer socket address */
901 struct meta_field_header *field;
903 /* Look for peer socket address */
904 for (field = &meta->options[0];
905 (caddr_t)field < (caddr_t)meta + meta->used_len;
906 field = (struct meta_field_header *)
907 ((caddr_t)field + field->len)) {
908 if (field->cookie != NGM_KSOCKET_COOKIE
909 || field->type != NG_KSOCKET_META_SOCKADDR)
911 sa = (struct sockaddr *)field->data;
917 priv->flags |= KSF_SENDING;
918 error = (*so->so_proto->pr_usrreqs->pru_sosend)(so, sa, 0, m, 0, 0, td);
919 priv->flags &= ~KSF_SENDING;
921 /* Clean up and exit */
930 ng_ksocket_rmnode(node_p node)
932 const priv_p priv = node->private;
935 /* Close our socket (if any) */
936 if (priv->so != NULL) {
937 priv->so->so_upcall = NULL;
938 priv->so->so_rcv.sb_flags &= ~SB_UPCALL;
939 priv->so->so_snd.sb_flags &= ~SB_UPCALL;
944 /* If we are an embryo, take ourselves out of the parent's list */
945 if (priv->flags & KSF_EMBRYONIC) {
946 LIST_REMOVE(priv, siblings);
947 priv->flags &= ~KSF_EMBRYONIC;
950 /* Remove any embryonic children we have */
951 while (!LIST_EMPTY(&priv->embryos)) {
952 embryo = LIST_FIRST(&priv->embryos);
953 ng_rmnode(embryo->node);
956 /* Take down netgraph node */
957 node->flags |= NG_INVALID;
960 bzero(priv, sizeof(*priv));
961 FREE(priv, M_NETGRAPH);
962 node->private = NULL;
963 ng_unref(node); /* let the node escape */
971 ng_ksocket_disconnect(hook_p hook)
973 KASSERT(hook->node->numhooks == 0,
974 ("%s: numhooks=%d?", __FUNCTION__, hook->node->numhooks));
975 ng_rmnode(hook->node);
979 /************************************************************************
981 ************************************************************************/
984 * When incoming data is appended to the socket, we get notified here.
985 * This is also called whenever a significant event occurs for the socket.
988 ng_ksocket_incoming(struct socket *so, void *arg, int waitflag)
990 const node_p node = arg;
991 const priv_p priv = node->private;
993 struct ng_mesg *response;
1000 if ((node->flags & NG_INVALID) != 0) {
1004 KASSERT(so == priv->so, ("%s: wrong socket", __FUNCTION__));
1006 /* Check whether a pending connect operation has completed */
1007 if (priv->flags & KSF_CONNECTING) {
1008 if ((error = so->so_error) != 0) {
1010 so->so_state &= ~SS_ISCONNECTING;
1012 if (!(so->so_state & SS_ISCONNECTING)) {
1013 NG_MKMESSAGE(response, NGM_KSOCKET_COOKIE,
1014 NGM_KSOCKET_CONNECT, sizeof(int32_t), waitflag);
1015 if (response != NULL) {
1016 response->header.flags |= NGF_RESP;
1017 response->header.token = priv->response_token;
1018 *(int32_t *)response->data = error;
1020 * XXX We use ng_queue_msg here because we are
1021 * being called from deep in the bowels of the TCP
1022 * stack. Is this right, or should we let the
1023 * receiver of the message worry about that?
1025 ng_queue_msg(node, response,
1026 priv->response_addr);
1028 priv->flags &= ~KSF_CONNECTING;
1032 /* Check whether a pending accept operation has completed */
1033 if (priv->flags & KSF_ACCEPTING) {
1034 error = ng_ksocket_check_accept(priv);
1035 if (error != EWOULDBLOCK)
1036 priv->flags &= ~KSF_ACCEPTING;
1038 ng_ksocket_finish_accept(priv, NULL);
1042 * If we don't have a hook, we must handle data events later. When
1043 * the hook gets created and is connected, this upcall function
1044 * will be called again.
1046 if (priv->hook == NULL) {
1051 /* Read and forward available mbuf's */
1053 auio.uio_resid = 1000000000;
1054 flags = MSG_DONTWAIT;
1056 struct sockaddr *sa = NULL;
1060 /* Try to get next packet from socket */
1061 if ((error = (*so->so_proto->pr_usrreqs->pru_soreceive)
1062 (so, (so->so_state & SS_ISCONNECTED) ? NULL : &sa,
1063 &auio, &m, (struct mbuf **)0, &flags)) != 0)
1066 /* See if we got anything */
1073 /* Don't trust the various socket layers to get the
1074 packet header and length correct (eg. kern/15175) */
1075 for (n = m, m->m_pkthdr.len = 0; n != NULL; n = n->m_next)
1076 m->m_pkthdr.len += n->m_len;
1078 /* Put peer's socket address (if any) into a meta info blob */
1080 struct meta_field_header *mhead;
1083 len = sizeof(*meta) + sizeof(*mhead) + sa->sa_len;
1084 MALLOC(meta, meta_p, len, M_NETGRAPH, M_NOWAIT);
1089 mhead = &meta->options[0];
1090 bzero(meta, sizeof(*meta));
1091 bzero(mhead, sizeof(*mhead));
1092 meta->allocated_len = len;
1093 meta->used_len = len;
1094 mhead->cookie = NGM_KSOCKET_COOKIE;
1095 mhead->type = NG_KSOCKET_META_SOCKADDR;
1096 mhead->len = sizeof(*mhead) + sa->sa_len;
1097 bcopy(sa, mhead->data, sa->sa_len);
1100 sendit: /* Forward data with optional peer sockaddr as meta info */
1101 NG_SEND_DATA(error, priv->hook, m, meta);
1105 * If the peer has closed the connection, forward a 0-length mbuf
1106 * to indicate end-of-file.
1108 if (so->so_state & SS_CANTRCVMORE && !(priv->flags & KSF_EOFSEEN)) {
1109 MGETHDR(m, waitflag, MT_DATA);
1111 m->m_len = m->m_pkthdr.len = 0;
1112 NG_SEND_DATA_ONLY(error, priv->hook, m);
1114 priv->flags |= KSF_EOFSEEN;
1121 * Check for a completed incoming connection and return 0 if one is found.
1122 * Otherwise return the appropriate error code.
1125 ng_ksocket_check_accept(priv_p priv)
1127 struct socket *const head = priv->so;
1130 if ((error = head->so_error) != 0) {
1134 if (TAILQ_EMPTY(&head->so_comp)) {
1135 if (head->so_state & SS_CANTRCVMORE)
1136 return ECONNABORTED;
1143 * Handle the first completed incoming connection, assumed to be already
1144 * on the socket's so_comp queue.
1147 ng_ksocket_finish_accept(priv_p priv, struct ng_mesg **rptr)
1149 struct socket *const head = priv->so;
1151 struct sockaddr *sa = NULL;
1152 struct ng_mesg *resp;
1153 struct ng_ksocket_accept *resp_data;
1158 so = TAILQ_FIRST(&head->so_comp);
1159 if (so == NULL) /* Should never happen */
1161 TAILQ_REMOVE(&head->so_comp, so, so_list);
1164 /* XXX KNOTE(&head->so_rcv.sb_sel.si_note, 0); */
1166 so->so_state &= ~SS_COMP;
1167 so->so_state |= SS_NBIO;
1172 len = OFFSETOF(struct ng_ksocket_accept, addr);
1176 NG_MKMESSAGE(resp, NGM_KSOCKET_COOKIE, NGM_KSOCKET_ACCEPT, len,
1182 resp->header.flags |= NGF_RESP;
1183 resp->header.token = priv->response_token;
1185 /* Clone a ksocket node to wrap the new socket */
1186 if (ng_ksocket_constructor(&node2) != 0) {
1187 FREE(resp, M_NETGRAPH);
1191 priv2 = (priv_p)node2->private;
1193 priv2->flags |= KSF_CLONED | KSF_EMBRYONIC;
1196 * Insert the cloned node into a list of embryonic children
1197 * on the parent node. When a hook is created on the cloned
1198 * node it will be removed from this list. When the parent
1199 * is destroyed it will destroy any embryonic children it has.
1201 LIST_INSERT_HEAD(&priv->embryos, priv2, siblings);
1203 so->so_upcallarg = (caddr_t)node2;
1204 so->so_upcall = ng_ksocket_incoming;
1205 so->so_rcv.sb_flags |= SB_UPCALL;
1206 so->so_snd.sb_flags |= SB_UPCALL;
1208 /* Fill in the response data and send it or return it to the caller */
1209 resp_data = (struct ng_ksocket_accept *)resp->data;
1210 resp_data->nodeid = node2->ID;
1212 bcopy(sa, &resp_data->addr, sa->sa_len);
1216 ng_queue_msg(priv->node, resp, priv->response_addr);
1224 * Parse out either an integer value or an alias.
1227 ng_ksocket_parse(const struct ng_ksocket_alias *aliases,
1228 const char *s, int family)
1234 for (k = 0; aliases[k].name != NULL; k++) {
1235 if (strcmp(s, aliases[k].name) == 0
1236 && aliases[k].family == family)
1237 return aliases[k].value;
1240 /* Try parsing as a number */
1241 val = (int)strtoul(s, &eptr, 10);
1242 if (val < 0 || *eptr != '\0')