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>
62 #include <sys/thread2.h>
66 #include <netgraph/ng_message.h>
67 #include <netgraph/netgraph.h>
68 #include <netgraph/ng_parse.h>
69 #include "ng_ksocket.h"
71 #include <netinet/in.h>
72 #include <netproto/atalk/at.h>
74 #define OFFSETOF(s, e) ((char *)&((s *)0)->e - (char *)((s *)0))
75 #define SADATA_OFFSET (OFFSETOF(struct sockaddr, sa_data))
77 /* Node private data */
78 struct ng_ksocket_private
{
82 LIST_HEAD(, ng_ksocket_private
) embryos
;
83 LIST_ENTRY(ng_ksocket_private
) siblings
;
85 u_int32_t response_token
;
86 char response_addr
[NG_PATHSIZ
];
88 typedef struct ng_ksocket_private
*priv_p
;
90 /* Flags for priv_p */
91 #define KSF_CONNECTING 0x00000001 /* Waiting for connection complete */
92 #define KSF_ACCEPTING 0x00000002 /* Waiting for accept complete */
93 #define KSF_EOFSEEN 0x00000004 /* Have sent 0-length EOF mbuf */
94 #define KSF_CLONED 0x00000008 /* Cloned from an accepting socket */
95 #define KSF_EMBRYONIC 0x00000010 /* Cloned node with no hooks yet */
96 #define KSF_SENDING 0x00000020 /* Sending on socket */
98 /* Internal commands which we send to ourselves */
99 #define NGM_KSOCKET_INTERNAL_COOKIE (NGM_KSOCKET_COOKIE + 1)
102 NGM_KSOCKET_INTERNAL_UPCALL
= 1
105 /* Netgraph node methods */
106 static ng_constructor_t ng_ksocket_constructor
;
107 static ng_rcvmsg_t ng_ksocket_rcvmsg
;
108 static ng_shutdown_t ng_ksocket_rmnode
;
109 static ng_newhook_t ng_ksocket_newhook
;
110 static ng_rcvdata_t ng_ksocket_rcvdata
;
111 static ng_disconnect_t ng_ksocket_disconnect
;
113 /* Alias structure */
114 struct ng_ksocket_alias
{
120 /* Protocol family aliases */
121 static const struct ng_ksocket_alias ng_ksocket_families
[] = {
122 { "local", PF_LOCAL
},
124 { "inet6", PF_INET6
},
125 { "atalk", PF_APPLETALK
},
131 /* Socket type aliases */
132 static const struct ng_ksocket_alias ng_ksocket_types
[] = {
133 { "stream", SOCK_STREAM
},
134 { "dgram", SOCK_DGRAM
},
137 { "seqpacket", SOCK_SEQPACKET
},
141 /* Protocol aliases */
142 static const struct ng_ksocket_alias ng_ksocket_protos
[] = {
143 { "ip", IPPROTO_IP
, PF_INET
},
144 { "raw", IPPROTO_RAW
, PF_INET
},
145 { "icmp", IPPROTO_ICMP
, PF_INET
},
146 { "igmp", IPPROTO_IGMP
, PF_INET
},
147 { "tcp", IPPROTO_TCP
, PF_INET
},
148 { "udp", IPPROTO_UDP
, PF_INET
},
149 { "gre", IPPROTO_GRE
, PF_INET
},
150 { "esp", IPPROTO_ESP
, PF_INET
},
151 { "ah", IPPROTO_AH
, PF_INET
},
152 { "swipe", IPPROTO_SWIPE
, PF_INET
},
153 { "encap", IPPROTO_ENCAP
, PF_INET
},
154 { "divert", IPPROTO_DIVERT
, PF_INET
},
155 { "pim", IPPROTO_PIM
, PF_INET
},
156 { "ddp", ATPROTO_DDP
, PF_APPLETALK
},
157 { "aarp", ATPROTO_AARP
, PF_APPLETALK
},
161 /* Helper functions */
162 static int ng_ksocket_check_accept(priv_p
);
163 static void ng_ksocket_finish_accept(priv_p
, struct ng_mesg
**);
164 static void ng_ksocket_incoming(struct socket
*so
, void *arg
, int waitflag
);
165 static int ng_ksocket_parse(const struct ng_ksocket_alias
*aliases
,
166 const char *s
, int family
);
168 /************************************************************************
169 STRUCT SOCKADDR PARSE TYPE
170 ************************************************************************/
172 /* Get the length of the data portion of a generic struct sockaddr */
174 ng_parse_generic_sockdata_getLength(const struct ng_parse_type
*type
,
175 const u_char
*start
, const u_char
*buf
)
177 const struct sockaddr
*sa
;
179 sa
= (const struct sockaddr
*)(buf
- SADATA_OFFSET
);
180 return (sa
->sa_len
< SADATA_OFFSET
) ? 0 : sa
->sa_len
- SADATA_OFFSET
;
183 /* Type for the variable length data portion of a generic struct sockaddr */
184 static const struct ng_parse_type ng_ksocket_generic_sockdata_type
= {
185 &ng_parse_bytearray_type
,
186 &ng_parse_generic_sockdata_getLength
189 /* Type for a generic struct sockaddr */
190 static const struct ng_parse_struct_field
191 ng_parse_generic_sockaddr_type_fields
[] = {
192 { "len", &ng_parse_uint8_type
},
193 { "family", &ng_parse_uint8_type
},
194 { "data", &ng_ksocket_generic_sockdata_type
},
197 static const struct ng_parse_type ng_ksocket_generic_sockaddr_type
= {
198 &ng_parse_struct_type
,
199 &ng_parse_generic_sockaddr_type_fields
202 /* Convert a struct sockaddr from ASCII to binary. If its a protocol
203 family that we specially handle, do that, otherwise defer to the
204 generic parse type ng_ksocket_generic_sockaddr_type. */
206 ng_ksocket_sockaddr_parse(const struct ng_parse_type
*type
,
207 const char *s
, int *off
, const u_char
*const start
,
208 u_char
*const buf
, int *buflen
)
210 struct sockaddr
*const sa
= (struct sockaddr
*)buf
;
211 enum ng_parse_token tok
;
216 /* If next token is a left curly brace, use generic parse type */
217 if ((tok
= ng_parse_get_token(s
, off
, &len
)) == T_LBRACE
) {
218 return (*ng_ksocket_generic_sockaddr_type
.supertype
->parse
)
219 (&ng_ksocket_generic_sockaddr_type
,
220 s
, off
, start
, buf
, buflen
);
223 /* Get socket address family followed by a slash */
224 while (isspace(s
[*off
]))
226 if ((t
= index(s
+ *off
, '/')) == NULL
)
228 if ((len
= t
- (s
+ *off
)) > sizeof(fambuf
) - 1)
230 strncpy(fambuf
, s
+ *off
, len
);
233 if ((family
= ng_ksocket_parse(ng_ksocket_families
, fambuf
, 0)) == -1)
237 if (*buflen
< SADATA_OFFSET
)
239 sa
->sa_family
= family
;
241 /* Set family-specific data and length */
242 switch (sa
->sa_family
) {
243 case PF_LOCAL
: /* Get pathname */
245 const int pathoff
= OFFSETOF(struct sockaddr_un
, sun_path
);
246 struct sockaddr_un
*const sun
= (struct sockaddr_un
*)sa
;
250 if ((path
= ng_get_string_token(s
, off
, &toklen
)) == NULL
)
252 pathlen
= strlen(path
);
253 if (pathlen
> SOCK_MAXADDRLEN
) {
254 FREE(path
, M_NETGRAPH
);
257 if (*buflen
< pathoff
+ pathlen
) {
258 FREE(path
, M_NETGRAPH
);
262 bcopy(path
, sun
->sun_path
, pathlen
);
263 sun
->sun_len
= pathoff
+ pathlen
;
264 FREE(path
, M_NETGRAPH
);
268 case PF_INET
: /* Get an IP address with optional port */
270 struct sockaddr_in
*const sin
= (struct sockaddr_in
*)sa
;
273 /* Parse this: <ipaddress>[:port] */
274 for (i
= 0; i
< 4; i
++) {
278 val
= strtoul(s
+ *off
, &eptr
, 10);
279 if (val
> 0xff || eptr
== s
+ *off
)
281 *off
+= (eptr
- (s
+ *off
));
282 ((u_char
*)&sin
->sin_addr
)[i
] = (u_char
)val
;
287 } else if (s
[*off
] == ':') {
289 val
= strtoul(s
+ *off
, &eptr
, 10);
290 if (val
> 0xffff || eptr
== s
+ *off
)
292 *off
+= (eptr
- (s
+ *off
));
293 sin
->sin_port
= htons(val
);
297 bzero(&sin
->sin_zero
, sizeof(sin
->sin_zero
));
298 sin
->sin_len
= sizeof(*sin
);
303 case PF_APPLETALK
: /* XXX implement these someday */
313 *buflen
= sa
->sa_len
;
317 /* Convert a struct sockaddr from binary to ASCII */
319 ng_ksocket_sockaddr_unparse(const struct ng_parse_type
*type
,
320 const u_char
*data
, int *off
, char *cbuf
, int cbuflen
)
322 const struct sockaddr
*sa
= (const struct sockaddr
*)(data
+ *off
);
325 /* Output socket address, either in special or generic format */
326 switch (sa
->sa_family
) {
329 const int pathoff
= OFFSETOF(struct sockaddr_un
, sun_path
);
330 const struct sockaddr_un
*sun
= (const struct sockaddr_un
*)sa
;
331 const int pathlen
= sun
->sun_len
- pathoff
;
332 char pathbuf
[SOCK_MAXADDRLEN
+ 1];
335 bcopy(sun
->sun_path
, pathbuf
, pathlen
);
336 pathbuf
[pathlen
] = '\0';
337 if ((pathtoken
= ng_encode_string(pathbuf
)) == NULL
)
339 slen
+= ksnprintf(cbuf
, cbuflen
, "local/%s", pathtoken
);
340 FREE(pathtoken
, M_NETGRAPH
);
343 *off
+= sun
->sun_len
;
349 const struct sockaddr_in
*sin
= (const struct sockaddr_in
*)sa
;
351 slen
+= ksnprintf(cbuf
, cbuflen
, "inet/%d.%d.%d.%d",
352 ((const u_char
*)&sin
->sin_addr
)[0],
353 ((const u_char
*)&sin
->sin_addr
)[1],
354 ((const u_char
*)&sin
->sin_addr
)[2],
355 ((const u_char
*)&sin
->sin_addr
)[3]);
356 if (sin
->sin_port
!= 0) {
357 slen
+= ksnprintf(cbuf
+ strlen(cbuf
),
358 cbuflen
- strlen(cbuf
), ":%d",
359 (u_int
)ntohs(sin
->sin_port
));
363 *off
+= sizeof(*sin
);
368 case PF_APPLETALK
: /* XXX implement these someday */
374 return (*ng_ksocket_generic_sockaddr_type
.supertype
->unparse
)
375 (&ng_ksocket_generic_sockaddr_type
,
376 data
, off
, cbuf
, cbuflen
);
380 /* Parse type for struct sockaddr */
381 static const struct ng_parse_type ng_ksocket_sockaddr_type
= {
385 &ng_ksocket_sockaddr_parse
,
386 &ng_ksocket_sockaddr_unparse
,
387 NULL
/* no such thing as a default struct sockaddr */
390 /************************************************************************
391 STRUCT NG_KSOCKET_SOCKOPT PARSE TYPE
392 ************************************************************************/
394 /* Get length of the struct ng_ksocket_sockopt value field, which is the
395 just the excess of the message argument portion over the length of
396 the struct ng_ksocket_sockopt. */
398 ng_parse_sockoptval_getLength(const struct ng_parse_type
*type
,
399 const u_char
*start
, const u_char
*buf
)
401 static const int offset
= OFFSETOF(struct ng_ksocket_sockopt
, value
);
402 const struct ng_ksocket_sockopt
*sopt
;
403 const struct ng_mesg
*msg
;
405 sopt
= (const struct ng_ksocket_sockopt
*)(buf
- offset
);
406 msg
= (const struct ng_mesg
*)((const u_char
*)sopt
- sizeof(*msg
));
407 return msg
->header
.arglen
- sizeof(*sopt
);
410 /* Parse type for the option value part of a struct ng_ksocket_sockopt
411 XXX Eventually, we should handle the different socket options specially.
412 XXX This would avoid byte order problems, eg an integer value of 1 is
413 XXX going to be "[1]" for little endian or "[3=1]" for big endian. */
414 static const struct ng_parse_type ng_ksocket_sockoptval_type
= {
415 &ng_parse_bytearray_type
,
416 &ng_parse_sockoptval_getLength
419 /* Parse type for struct ng_ksocket_sockopt */
420 static const struct ng_parse_struct_field ng_ksocket_sockopt_type_fields
[]
421 = NG_KSOCKET_SOCKOPT_INFO(&ng_ksocket_sockoptval_type
);
422 static const struct ng_parse_type ng_ksocket_sockopt_type
= {
423 &ng_parse_struct_type
,
424 &ng_ksocket_sockopt_type_fields
427 /* Parse type for struct ng_ksocket_accept */
428 static const struct ng_parse_struct_field ng_ksocket_accept_type_fields
[]
429 = NGM_KSOCKET_ACCEPT_INFO
;
430 static const struct ng_parse_type ng_ksocket_accept_type
= {
431 &ng_parse_struct_type
,
432 &ng_ksocket_accept_type_fields
435 /* List of commands and how to convert arguments to/from ASCII */
436 static const struct ng_cmdlist ng_ksocket_cmds
[] = {
441 &ng_ksocket_sockaddr_type
,
448 &ng_parse_int32_type
,
456 &ng_ksocket_accept_type
462 &ng_ksocket_sockaddr_type
,
470 &ng_ksocket_sockaddr_type
474 NGM_KSOCKET_GETPEERNAME
,
477 &ng_ksocket_sockaddr_type
483 &ng_ksocket_sockopt_type
,
490 &ng_ksocket_sockopt_type
,
491 &ng_ksocket_sockopt_type
494 /* Internal commands */
496 NGM_KSOCKET_INTERNAL_COOKIE
,
497 NGM_KSOCKET_INTERNAL_UPCALL
,
505 /* Node type descriptor */
506 static struct ng_type ng_ksocket_typestruct
= {
508 NG_KSOCKET_NODE_TYPE
,
510 ng_ksocket_constructor
,
518 ng_ksocket_disconnect
,
521 NETGRAPH_INIT(ksocket
, &ng_ksocket_typestruct
);
523 #define ERROUT(x) do { error = (x); goto done; } while (0)
525 /************************************************************************
527 ************************************************************************/
530 * Node type constructor
533 ng_ksocket_constructor(node_p
*nodep
)
538 /* Allocate private structure */
539 MALLOC(priv
, priv_p
, sizeof(*priv
), M_NETGRAPH
, M_NOWAIT
| M_ZERO
);
543 /* Call generic node constructor */
544 if ((error
= ng_make_node_common(&ng_ksocket_typestruct
, nodep
))) {
545 FREE(priv
, M_NETGRAPH
);
548 (*nodep
)->private = priv
;
550 LIST_INIT(&priv
->embryos
);
557 * Give our OK for a hook to be added. The hook name is of the
558 * form "<family>/<type>/<proto>" where the three components may
559 * be decimal numbers or else aliases from the above lists.
561 * Connecting a hook amounts to opening the socket. Disconnecting
562 * the hook closes the socket and destroys the node as well.
565 ng_ksocket_newhook(node_p node
, hook_p hook
, const char *name0
)
567 struct thread
*td
= curthread
->td_proc
? curthread
: &thread0
; /* XXX broken */
568 const priv_p priv
= node
->private;
570 char *s1
, *s2
, name
[NG_HOOKSIZ
];
571 int family
, type
, protocol
, error
;
573 /* Check if we're already connected */
574 if (priv
->hook
!= NULL
)
577 if (priv
->flags
& KSF_CLONED
) {
578 if (priv
->flags
& KSF_EMBRYONIC
) {
579 /* Remove ourselves from our parent's embryo list */
580 LIST_REMOVE(priv
, siblings
);
581 priv
->flags
&= ~KSF_EMBRYONIC
;
584 /* Extract family, type, and protocol from hook name */
585 ksnprintf(name
, sizeof(name
), "%s", name0
);
587 if ((s2
= index(s1
, '/')) == NULL
)
590 family
= ng_ksocket_parse(ng_ksocket_families
, s1
, 0);
594 if ((s2
= index(s1
, '/')) == NULL
)
597 type
= ng_ksocket_parse(ng_ksocket_types
, s1
, 0);
601 protocol
= ng_ksocket_parse(ng_ksocket_protos
, s1
, family
);
605 /* Create the socket */
606 error
= socreate(family
, &priv
->so
, type
, protocol
, td
);
610 /* XXX call soreserve() ? */
612 /* Add our hook for incoming data and other events */
613 priv
->so
->so_upcallarg
= (caddr_t
)node
;
614 priv
->so
->so_upcall
= ng_ksocket_incoming
;
615 priv
->so
->so_rcv
.ssb_flags
|= SSB_UPCALL
;
616 priv
->so
->so_snd
.ssb_flags
|= SSB_UPCALL
;
623 * On a cloned socket we may have already received one or more
624 * upcalls which we couldn't handle without a hook. Handle
625 * those now. We cannot call the upcall function directly
626 * from here, because until this function has returned our
627 * hook isn't connected. So we queue a message to ourselves
628 * which will cause the upcall function to be called a bit
631 if (priv
->flags
& KSF_CLONED
) {
632 NG_MKMESSAGE(msg
, NGM_KSOCKET_INTERNAL_COOKIE
,
633 NGM_KSOCKET_INTERNAL_UPCALL
, 0, M_NOWAIT
);
635 ng_queue_msg(node
, msg
, ".:");
642 * Receive a control message
645 ng_ksocket_rcvmsg(node_p node
, struct ng_mesg
*msg
,
646 const char *raddr
, struct ng_mesg
**rptr
)
648 struct thread
*td
= curthread
->td_proc
? curthread
: &thread0
; /* XXX broken */
649 const priv_p priv
= node
->private;
650 struct socket
*const so
= priv
->so
;
651 struct ng_mesg
*resp
= NULL
;
654 switch (msg
->header
.typecookie
) {
655 case NGM_KSOCKET_COOKIE
:
656 switch (msg
->header
.cmd
) {
657 case NGM_KSOCKET_BIND
:
659 struct sockaddr
*const sa
660 = (struct sockaddr
*)msg
->data
;
663 if (msg
->header
.arglen
< SADATA_OFFSET
664 || msg
->header
.arglen
< sa
->sa_len
)
670 error
= sobind(so
, sa
, td
);
673 case NGM_KSOCKET_LISTEN
:
676 if (msg
->header
.arglen
!= sizeof(int32_t))
682 error
= solisten(so
, *((int32_t *)msg
->data
), td
);
686 case NGM_KSOCKET_ACCEPT
:
689 if (msg
->header
.arglen
!= 0)
694 /* Make sure the socket is capable of accepting */
695 if (!(so
->so_options
& SO_ACCEPTCONN
))
697 if (priv
->flags
& KSF_ACCEPTING
)
700 error
= ng_ksocket_check_accept(priv
);
701 if (error
!= 0 && error
!= EWOULDBLOCK
)
705 * If a connection is already complete, take it.
706 * Otherwise let the upcall function deal with
707 * the connection when it comes in.
709 priv
->response_token
= msg
->header
.token
;
710 strcpy(priv
->response_addr
, raddr
);
712 ng_ksocket_finish_accept(priv
,
713 rptr
!= NULL
? &resp
: NULL
);
715 priv
->flags
|= KSF_ACCEPTING
;
719 case NGM_KSOCKET_CONNECT
:
721 struct sockaddr
*const sa
722 = (struct sockaddr
*)msg
->data
;
725 if (msg
->header
.arglen
< SADATA_OFFSET
726 || msg
->header
.arglen
< sa
->sa_len
)
732 if ((so
->so_state
& SS_ISCONNECTING
) != 0)
734 if ((error
= soconnect(so
, sa
, td
)) != 0) {
735 so
->so_state
&= ~SS_ISCONNECTING
;
738 if ((so
->so_state
& SS_ISCONNECTING
) != 0) {
739 /* We will notify the sender when we connect */
740 priv
->response_token
= msg
->header
.token
;
741 strcpy(priv
->response_addr
, raddr
);
742 priv
->flags
|= KSF_CONNECTING
;
748 case NGM_KSOCKET_GETNAME
:
749 case NGM_KSOCKET_GETPEERNAME
:
751 struct sockaddr
*sa
= NULL
;
755 if (msg
->header
.arglen
!= 0)
761 if (msg
->header
.cmd
== NGM_KSOCKET_GETPEERNAME
) {
763 & (SS_ISCONNECTED
|SS_ISCONFIRMING
)) == 0)
765 error
= so_pru_peeraddr(so
, &sa
);
767 error
= so_pru_sockaddr(so
, &sa
);
769 /* Get local or peer address */
772 len
= (sa
== NULL
) ? 0 : sa
->sa_len
;
774 /* Send it back in a response */
775 NG_MKRESPONSE(resp
, msg
, len
, M_NOWAIT
);
780 bcopy(sa
, resp
->data
, len
);
789 case NGM_KSOCKET_GETOPT
:
791 struct ng_ksocket_sockopt
*ksopt
=
792 (struct ng_ksocket_sockopt
*)msg
->data
;
796 if (msg
->header
.arglen
!= sizeof(*ksopt
))
801 /* Get response with room for option value */
802 NG_MKRESPONSE(resp
, msg
, sizeof(*ksopt
)
803 + NG_KSOCKET_MAX_OPTLEN
, M_NOWAIT
);
807 /* Get socket option, and put value in the response */
808 sopt
.sopt_dir
= SOPT_GET
;
809 sopt
.sopt_level
= ksopt
->level
;
810 sopt
.sopt_name
= ksopt
->name
;
812 sopt
.sopt_valsize
= NG_KSOCKET_MAX_OPTLEN
;
813 ksopt
= (struct ng_ksocket_sockopt
*)resp
->data
;
814 sopt
.sopt_val
= ksopt
->value
;
815 if ((error
= sogetopt(so
, &sopt
)) != 0) {
816 FREE(resp
, M_NETGRAPH
);
820 /* Set actual value length */
821 resp
->header
.arglen
= sizeof(*ksopt
)
826 case NGM_KSOCKET_SETOPT
:
828 struct ng_ksocket_sockopt
*const ksopt
=
829 (struct ng_ksocket_sockopt
*)msg
->data
;
830 const int valsize
= msg
->header
.arglen
- sizeof(*ksopt
);
839 /* Set socket option */
840 sopt
.sopt_dir
= SOPT_SET
;
841 sopt
.sopt_level
= ksopt
->level
;
842 sopt
.sopt_name
= ksopt
->name
;
843 sopt
.sopt_val
= ksopt
->value
;
844 sopt
.sopt_valsize
= valsize
;
846 error
= sosetopt(so
, &sopt
);
855 case NGM_KSOCKET_INTERNAL_COOKIE
:
856 switch (msg
->header
.cmd
) {
857 case NGM_KSOCKET_INTERNAL_UPCALL
:
860 (*priv
->so
->so_upcall
)(so
, so
->so_upcallarg
, M_NOWAIT
);
874 FREE(resp
, M_NETGRAPH
);
877 FREE(msg
, M_NETGRAPH
);
882 * Receive incoming data on our hook. Send it out the socket.
885 ng_ksocket_rcvdata(hook_p hook
, struct mbuf
*m
, meta_p meta
)
887 struct thread
*td
= curthread
->td_proc
? curthread
: &thread0
; /* XXX broken */
888 const node_p node
= hook
->node
;
889 const priv_p priv
= node
->private;
890 struct socket
*const so
= priv
->so
;
891 struct sockaddr
*sa
= NULL
;
894 /* Avoid reentrantly sending on the socket */
895 if ((priv
->flags
& KSF_SENDING
) != 0) {
896 NG_FREE_DATA(m
, meta
);
900 /* If any meta info, look for peer socket address */
902 struct meta_field_header
*field
;
904 /* Look for peer socket address */
905 for (field
= &meta
->options
[0];
906 (caddr_t
)field
< (caddr_t
)meta
+ meta
->used_len
;
907 field
= (struct meta_field_header
*)
908 ((caddr_t
)field
+ field
->len
)) {
909 if (field
->cookie
!= NGM_KSOCKET_COOKIE
910 || field
->type
!= NG_KSOCKET_META_SOCKADDR
)
912 sa
= (struct sockaddr
*)field
->data
;
918 priv
->flags
|= KSF_SENDING
;
919 error
= so_pru_sosend(so
, sa
, NULL
, m
, NULL
, 0, td
);
920 priv
->flags
&= ~KSF_SENDING
;
922 /* Clean up and exit */
931 ng_ksocket_rmnode(node_p node
)
933 const priv_p priv
= node
->private;
936 /* Close our socket (if any) */
937 if (priv
->so
!= NULL
) {
938 priv
->so
->so_upcall
= NULL
;
939 priv
->so
->so_rcv
.ssb_flags
&= ~SSB_UPCALL
;
940 priv
->so
->so_snd
.ssb_flags
&= ~SSB_UPCALL
;
941 soclose(priv
->so
, FNONBLOCK
);
945 /* If we are an embryo, take ourselves out of the parent's list */
946 if (priv
->flags
& KSF_EMBRYONIC
) {
947 LIST_REMOVE(priv
, siblings
);
948 priv
->flags
&= ~KSF_EMBRYONIC
;
951 /* Remove any embryonic children we have */
952 while (!LIST_EMPTY(&priv
->embryos
)) {
953 embryo
= LIST_FIRST(&priv
->embryos
);
954 ng_rmnode(embryo
->node
);
957 /* Take down netgraph node */
958 node
->flags
|= NG_INVALID
;
961 bzero(priv
, sizeof(*priv
));
962 FREE(priv
, M_NETGRAPH
);
963 node
->private = NULL
;
964 ng_unref(node
); /* let the node escape */
972 ng_ksocket_disconnect(hook_p hook
)
974 KASSERT(hook
->node
->numhooks
== 0,
975 ("%s: numhooks=%d?", __func__
, hook
->node
->numhooks
));
976 ng_rmnode(hook
->node
);
980 /************************************************************************
982 ************************************************************************/
985 * When incoming data is appended to the socket, we get notified here.
986 * This is also called whenever a significant event occurs for the socket.
989 ng_ksocket_incoming(struct socket
*so
, void *arg
, int waitflag
)
991 const node_p node
= arg
;
992 const priv_p priv
= node
->private;
993 struct ng_mesg
*response
;
999 if ((node
->flags
& NG_INVALID
) != 0) {
1003 KASSERT(so
== priv
->so
, ("%s: wrong socket", __func__
));
1005 /* Check whether a pending connect operation has completed */
1006 if (priv
->flags
& KSF_CONNECTING
) {
1007 if ((error
= so
->so_error
) != 0) {
1009 so
->so_state
&= ~SS_ISCONNECTING
;
1011 if (!(so
->so_state
& SS_ISCONNECTING
)) {
1012 NG_MKMESSAGE(response
, NGM_KSOCKET_COOKIE
,
1013 NGM_KSOCKET_CONNECT
, sizeof(int32_t), waitflag
);
1014 if (response
!= NULL
) {
1015 response
->header
.flags
|= NGF_RESP
;
1016 response
->header
.token
= priv
->response_token
;
1017 *(int32_t *)response
->data
= error
;
1019 * XXX We use ng_queue_msg here because we are
1020 * being called from deep in the bowels of the TCP
1021 * stack. Is this right, or should we let the
1022 * receiver of the message worry about that?
1024 ng_queue_msg(node
, response
,
1025 priv
->response_addr
);
1027 priv
->flags
&= ~KSF_CONNECTING
;
1031 /* Check whether a pending accept operation has completed */
1032 if (priv
->flags
& KSF_ACCEPTING
) {
1033 error
= ng_ksocket_check_accept(priv
);
1034 if (error
!= EWOULDBLOCK
)
1035 priv
->flags
&= ~KSF_ACCEPTING
;
1037 ng_ksocket_finish_accept(priv
, NULL
);
1041 * If we don't have a hook, we must handle data events later. When
1042 * the hook gets created and is connected, this upcall function
1043 * will be called again.
1045 if (priv
->hook
== NULL
) {
1050 /* Read and forward available mbuf's */
1052 struct sockaddr
*sa
= NULL
;
1058 sbinit(&sio
, 1000000000);
1059 flags
= MSG_DONTWAIT
;
1061 /* Try to get next packet from socket */
1062 error
= so_pru_soreceive(so
,
1063 ((so
->so_state
& SS_ISCONNECTED
) ? NULL
: &sa
),
1064 NULL
, &sio
, NULL
, &flags
);
1068 /* See if we got anything */
1069 if (sio
.sb_mb
== NULL
) {
1075 /* Don't trust the various socket layers to get the
1076 packet header and length correct (eg. kern/15175) */
1077 sio
.sb_mb
->m_pkthdr
.len
= 0;
1078 for (n
= sio
.sb_mb
; n
!= NULL
; n
= n
->m_next
)
1079 sio
.sb_mb
->m_pkthdr
.len
+= n
->m_len
;
1081 /* Put peer's socket address (if any) into a meta info blob */
1083 struct meta_field_header
*mhead
;
1086 len
= sizeof(*meta
) + sizeof(*mhead
) + sa
->sa_len
;
1087 MALLOC(meta
, meta_p
, len
, M_NETGRAPH
, M_NOWAIT
);
1092 mhead
= &meta
->options
[0];
1093 bzero(meta
, sizeof(*meta
));
1094 bzero(mhead
, sizeof(*mhead
));
1095 meta
->allocated_len
= len
;
1096 meta
->used_len
= len
;
1097 mhead
->cookie
= NGM_KSOCKET_COOKIE
;
1098 mhead
->type
= NG_KSOCKET_META_SOCKADDR
;
1099 mhead
->len
= sizeof(*mhead
) + sa
->sa_len
;
1100 bcopy(sa
, mhead
->data
, sa
->sa_len
);
1103 sendit
: /* Forward data with optional peer sockaddr as meta info */
1104 NG_SEND_DATA(error
, priv
->hook
, sio
.sb_mb
, meta
);
1108 * If the peer has closed the connection, forward a 0-length mbuf
1109 * to indicate end-of-file.
1111 if (so
->so_state
& SS_CANTRCVMORE
&& !(priv
->flags
& KSF_EOFSEEN
)) {
1114 MGETHDR(m
, waitflag
, MT_DATA
);
1116 m
->m_len
= m
->m_pkthdr
.len
= 0;
1117 NG_SEND_DATA_ONLY(error
, priv
->hook
, m
);
1119 priv
->flags
|= KSF_EOFSEEN
;
1126 * Check for a completed incoming connection and return 0 if one is found.
1127 * Otherwise return the appropriate error code.
1130 ng_ksocket_check_accept(priv_p priv
)
1132 struct socket
*const head
= priv
->so
;
1135 if ((error
= head
->so_error
) != 0) {
1139 if (TAILQ_EMPTY(&head
->so_comp
)) {
1140 if (head
->so_state
& SS_CANTRCVMORE
)
1141 return ECONNABORTED
;
1148 * Handle the first completed incoming connection, assumed to be already
1149 * on the socket's so_comp queue.
1152 ng_ksocket_finish_accept(priv_p priv
, struct ng_mesg
**rptr
)
1154 struct socket
*const head
= priv
->so
;
1156 struct sockaddr
*sa
= NULL
;
1157 struct ng_mesg
*resp
;
1158 struct ng_ksocket_accept
*resp_data
;
1163 so
= TAILQ_FIRST(&head
->so_comp
);
1164 if (so
== NULL
) /* Should never happen */
1166 TAILQ_REMOVE(&head
->so_comp
, so
, so_list
);
1169 /* XXX KNOTE(&head->so_rcv.ssb_sel.si_note, 0); */
1171 so
->so_state
&= ~SS_COMP
;
1176 len
= OFFSETOF(struct ng_ksocket_accept
, addr
);
1180 NG_MKMESSAGE(resp
, NGM_KSOCKET_COOKIE
, NGM_KSOCKET_ACCEPT
, len
,
1183 soclose(so
, FNONBLOCK
);
1186 resp
->header
.flags
|= NGF_RESP
;
1187 resp
->header
.token
= priv
->response_token
;
1189 /* Clone a ksocket node to wrap the new socket */
1190 if (ng_ksocket_constructor(&node2
) != 0) {
1191 FREE(resp
, M_NETGRAPH
);
1192 soclose(so
, FNONBLOCK
);
1195 priv2
= (priv_p
)node2
->private;
1197 priv2
->flags
|= KSF_CLONED
| KSF_EMBRYONIC
;
1200 * Insert the cloned node into a list of embryonic children
1201 * on the parent node. When a hook is created on the cloned
1202 * node it will be removed from this list. When the parent
1203 * is destroyed it will destroy any embryonic children it has.
1205 LIST_INSERT_HEAD(&priv
->embryos
, priv2
, siblings
);
1207 so
->so_upcallarg
= (caddr_t
)node2
;
1208 so
->so_upcall
= ng_ksocket_incoming
;
1209 so
->so_rcv
.ssb_flags
|= SSB_UPCALL
;
1210 so
->so_snd
.ssb_flags
|= SSB_UPCALL
;
1212 /* Fill in the response data and send it or return it to the caller */
1213 resp_data
= (struct ng_ksocket_accept
*)resp
->data
;
1214 resp_data
->nodeid
= node2
->ID
;
1216 bcopy(sa
, &resp_data
->addr
, sa
->sa_len
);
1220 ng_queue_msg(priv
->node
, resp
, priv
->response_addr
);
1228 * Parse out either an integer value or an alias.
1231 ng_ksocket_parse(const struct ng_ksocket_alias
*aliases
,
1232 const char *s
, int family
)
1238 for (k
= 0; aliases
[k
].name
!= NULL
; k
++) {
1239 if (strcmp(s
, aliases
[k
].name
) == 0
1240 && aliases
[k
].family
== family
)
1241 return aliases
[k
].value
;
1244 /* Try parsing as a number */
1245 val
= (int)strtoul(s
, &eptr
, 10);
1246 if (val
< 0 || *eptr
!= '\0')