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: Julian Elischer <julian@freebsd.org>
39 * $FreeBSD: src/sys/netgraph/ng_socket.c,v 1.11.2.6 2002/07/02 22:17:18 archie Exp $
40 * $Whistle: ng_socket.c,v 1.28 1999/11/01 09:24:52 julian Exp $
44 * Netgraph socket nodes
46 * There are two types of netgraph sockets, control and data.
47 * Control sockets have a netgraph node, but data sockets are
48 * parasitic on control sockets, and have no node of their own.
51 #include <sys/param.h>
52 #include <sys/systm.h>
55 #include <sys/domain.h>
56 #include <sys/errno.h>
57 #include <sys/kernel.h>
58 #include <sys/filedesc.h>
59 #include <sys/malloc.h>
60 #include <sys/queue.h>
62 #include <sys/protosw.h>
63 #include <sys/socket.h>
64 #include <sys/socketvar.h>
65 #include <sys/sysctl.h>
67 #include <sys/vnode.h>
70 #include <sys/thread2.h>
71 #include <sys/socketvar2.h>
72 #include <sys/msgport2.h>
74 #include <netgraph/ng_message.h>
75 #include <netgraph/netgraph.h>
76 #include "ng_socketvar.h"
77 #include "ng_socket.h"
80 * It's Ascii-art time!
81 * +-------------+ +-------------+
82 * |socket (ctl)| |socket (data)|
83 * +-------------+ +-------------+
87 * +-----------+ +-----------+
88 * |pcb (ctl)| |pcb (data)|
89 * +-----------+ +-----------+
93 * +--------------------------+
94 * | Socket type private |
96 * +--------------------------+
105 /* Netgraph node methods */
106 static ng_constructor_t ngs_constructor
;
107 static ng_rcvmsg_t ngs_rcvmsg
;
108 static ng_shutdown_t ngs_rmnode
;
109 static ng_newhook_t ngs_newhook
;
110 static ng_rcvdata_t ngs_rcvdata
;
111 static ng_disconnect_t ngs_disconnect
;
113 /* Internal methods */
114 static int ng_attach_data(struct socket
*so
);
115 static int ng_attach_cntl(struct socket
*so
);
116 static int ng_attach_common(struct socket
*so
, int type
);
117 static void ng_detach_common(struct ngpcb
*pcbp
, int type
);
118 /*static int ng_internalize(struct mbuf *m, struct thread *td); */
120 static int ng_connect_data(struct sockaddr
*nam
, struct ngpcb
*pcbp
);
121 static int ng_connect_cntl(struct sockaddr
*nam
, struct ngpcb
*pcbp
);
122 static int ng_bind(struct sockaddr
*nam
, struct ngpcb
*pcbp
);
124 static int ngs_mod_event(module_t mod
, int event
, void *data
);
125 static int ship_msg(struct ngpcb
*pcbp
, struct ng_mesg
*msg
,
126 struct sockaddr_ng
*addr
);
128 /* Netgraph type descriptor */
129 static struct ng_type typestruct
= {
144 NETGRAPH_INIT(socket
, &typestruct
);
147 static u_long ngpdg_sendspace
= 20 * 1024; /* really max datagram size */
148 static u_long ngpdg_recvspace
= 20 * 1024;
150 /* List of all sockets */
151 LIST_HEAD(, ngpcb
) ngsocklist
;
153 #define sotongpcb(so) ((struct ngpcb *)(so)->so_pcb)
155 /* If getting unexplained errors returned, set this to "Debugger("X"); */
160 /***************************************************************
162 ***************************************************************/
165 ngc_attach(netmsg_t msg
)
167 struct socket
*so
= msg
->attach
.base
.nm_so
;
168 struct pru_attach_info
*ai
= msg
->attach
.nm_ai
;
169 struct ngpcb
*const pcbp
= sotongpcb(so
);
172 if (caps_priv_check(ai
->p_ucred
,
173 SYSCAP_RESTRICTEDROOT
| __SYSCAP_NULLCRED
) != 0)
177 else if (pcbp
!= NULL
)
180 error
= ng_attach_cntl(so
);
181 lwkt_replymsg(&msg
->attach
.base
.lmsg
, error
);
185 ngc_detach(netmsg_t msg
)
187 struct socket
*so
= msg
->detach
.base
.nm_so
;
188 struct ngpcb
*const pcbp
= sotongpcb(so
);
192 ng_detach_common(pcbp
, NG_CONTROL
);
197 lwkt_replymsg(&msg
->detach
.base
.lmsg
, error
);
201 ngc_send(netmsg_t msg
)
203 struct socket
*so
= msg
->send
.base
.nm_so
;
204 struct mbuf
*m
= msg
->send
.nm_m
;
205 struct sockaddr
*addr
= msg
->send
.nm_addr
;
206 struct mbuf
*control
= msg
->send
.nm_control
;
207 struct ngpcb
*const pcbp
= sotongpcb(so
);
208 struct sockaddr_ng
*const sap
= (struct sockaddr_ng
*) addr
;
209 struct ng_mesg
*resp
;
211 char *xmsg
, *path
= NULL
;
220 if (control
&& (error
= ng_internalize(control
, p
))) {
221 if (pcbp
->sockdata
== NULL
) {
233 /* Require destination as there may be >= 1 hooks on this node */
235 error
= EDESTADDRREQ
;
239 /* Allocate an expendable buffer for the path, chop off
240 * the sockaddr header, and make sure it's NUL terminated */
241 len
= sap
->sg_len
- 2;
242 path
= kmalloc(len
+ 1, M_NETGRAPH
, M_WAITOK
);
243 bcopy(sap
->sg_data
, path
, len
);
246 /* Move the actual message out of mbufs into a linear buffer.
247 * Start by adding up the size of the data. (could use mh_len?) */
248 for (len
= 0, m0
= m
; m0
!= NULL
; m0
= m0
->m_next
)
251 /* Move the data into a linear buffer as well. Messages are not
252 * delivered in mbufs. */
253 xmsg
= kmalloc(len
+ 1, M_NETGRAPH
, M_WAITOK
);
254 m_copydata(m
, 0, len
, xmsg
);
256 /* The callee will free the xmsg when done. The addr is our business. */
257 error
= ng_send_msg(pcbp
->sockdata
->node
,
258 (struct ng_mesg
*) xmsg
, path
, &resp
);
260 /* If the callee responded with a synchronous response, then put it
261 * back on the receive side of the socket; sap is source address. */
262 if (error
== 0 && resp
!= NULL
)
263 error
= ship_msg(pcbp
, resp
, sap
);
267 kfree(path
, M_NETGRAPH
);
272 lwkt_replymsg(&msg
->send
.base
.lmsg
, error
);
276 ngc_bind(netmsg_t msg
)
278 struct socket
*so
= msg
->bind
.base
.nm_so
;
279 struct sockaddr
*nam
= msg
->bind
.nm_nam
;
280 struct ngpcb
*const pcbp
= sotongpcb(so
);
284 error
= ng_bind(nam
, pcbp
);
287 lwkt_replymsg(&msg
->bind
.base
.lmsg
, error
);
291 ngc_connect(netmsg_t msg
)
293 struct socket
*so
= msg
->connect
.base
.nm_so
;
294 struct sockaddr
*nam
= msg
->connect
.nm_nam
;
295 struct ngpcb
*const pcbp
= sotongpcb(so
);
299 error
= ng_connect_cntl(nam
, pcbp
);
302 lwkt_replymsg(&msg
->connect
.base
.lmsg
, error
);
305 /***************************************************************
307 ***************************************************************/
310 ngd_attach(netmsg_t msg
)
312 struct socket
*so
= msg
->attach
.base
.nm_so
;
313 struct ngpcb
*const pcbp
= sotongpcb(so
);
319 error
= ng_attach_data(so
);
320 lwkt_replymsg(&msg
->connect
.base
.lmsg
, error
);
324 ngd_detach(netmsg_t msg
)
326 struct socket
*so
= msg
->detach
.base
.nm_so
;
327 struct ngpcb
*const pcbp
= sotongpcb(so
);
331 ng_detach_common(pcbp
, NG_DATA
);
336 lwkt_replymsg(&msg
->detach
.base
.lmsg
, error
);
340 ngd_send(netmsg_t msg
)
342 struct socket
*so
= msg
->send
.base
.nm_so
;
343 struct mbuf
*m
= msg
->send
.nm_m
;
344 struct sockaddr
*addr
= msg
->send
.nm_addr
;
345 struct mbuf
*control
= msg
->send
.nm_control
;
346 struct ngpcb
*const pcbp
= sotongpcb(so
);
347 struct sockaddr_ng
*const sap
= (struct sockaddr_ng
*) addr
;
351 char hookname
[NG_HOOKSIZ
];
353 if ((pcbp
== NULL
) || (control
!= NULL
)) {
357 if (pcbp
->sockdata
== NULL
) {
362 * If the user used any of these ways to not specify an address
363 * then handle specially.
366 || ((len
= sap
->sg_len
- 2) <= 0)
367 || (*sap
->sg_data
== '\0')) {
368 if (pcbp
->sockdata
->node
->numhooks
!= 1) {
369 error
= EDESTADDRREQ
;
373 * if exactly one hook exists, just use it.
374 * Special case to allow write(2) to work on an ng_socket.
376 hook
= LIST_FIRST(&pcbp
->sockdata
->node
->hooks
);
378 if (len
>= NG_HOOKSIZ
) {
384 * chop off the sockaddr header, and make sure it's NUL
387 bcopy(sap
->sg_data
, hookname
, len
);
388 hookname
[len
] = '\0';
390 /* Find the correct hook from 'hookname' */
391 LIST_FOREACH(hook
, &pcbp
->sockdata
->node
->hooks
, hooks
) {
392 if (strcmp(hookname
, hook
->name
) == 0)
396 error
= EHOSTUNREACH
;
399 /* Send data (OK if hook is NULL) */
400 NG_SEND_DATA(error
, hook
, m
, mp
); /* makes m NULL */
407 lwkt_replymsg(&msg
->send
.base
.lmsg
, error
);
411 ngd_connect(netmsg_t msg
)
413 struct socket
*so
= msg
->connect
.base
.nm_so
;
414 struct sockaddr
*nam
= msg
->connect
.nm_nam
;
415 struct ngpcb
*const pcbp
= sotongpcb(so
);
419 error
= ng_connect_data(nam
, pcbp
);
423 lwkt_replymsg(&msg
->connect
.base
.lmsg
, error
);
427 * Used for both data and control sockets
430 ng_setsockaddr(netmsg_t msg
)
432 struct socket
*so
= msg
->sockaddr
.base
.nm_so
;
433 struct sockaddr
**addr
= msg
->sockaddr
.nm_nam
;
435 struct sockaddr_ng
*sg
;
439 /* Why isn't sg_data a `char[1]' ? :-( */
440 sg_len
= sizeof(struct sockaddr_ng
) - sizeof(sg
->sg_data
) + 1;
443 pcbp
= sotongpcb(so
);
444 if ((pcbp
== NULL
) || (pcbp
->sockdata
== NULL
)) {
450 namelen
= 0; /* silence compiler ! */
452 if (pcbp
->sockdata
->node
->name
!= NULL
)
453 sg_len
+= namelen
= strlen(pcbp
->sockdata
->node
->name
);
455 sg
= kmalloc(sg_len
, M_SONAME
, M_WAITOK
| M_ZERO
);
457 if (pcbp
->sockdata
->node
->name
!= NULL
)
458 bcopy(pcbp
->sockdata
->node
->name
, sg
->sg_data
, namelen
);
462 sg
->sg_family
= AF_NETGRAPH
;
463 *addr
= (struct sockaddr
*)sg
;
466 lwkt_replymsg(&msg
->sockaddr
.base
.lmsg
, error
);
470 * Attach a socket to it's protocol specific partner.
471 * For a control socket, actually create a netgraph node and attach
476 ng_attach_cntl(struct socket
*so
)
478 struct ngsock
*privdata
;
482 /* Setup protocol control block */
483 if ((error
= ng_attach_common(so
, NG_CONTROL
)) != 0)
485 pcbp
= sotongpcb(so
);
487 /* Allocate node private info */
488 privdata
= kmalloc(sizeof(*privdata
), M_NETGRAPH
, M_WAITOK
| M_ZERO
);
490 /* Make the generic node components */
491 if ((error
= ng_make_node_common(&typestruct
, &privdata
->node
)) != 0) {
492 kfree(privdata
, M_NETGRAPH
);
493 ng_detach_common(pcbp
, NG_CONTROL
);
496 privdata
->node
->private = privdata
;
498 /* Link the pcb and the node private data */
499 privdata
->ctlsock
= pcbp
;
500 pcbp
->sockdata
= privdata
;
506 ng_attach_data(struct socket
*so
)
508 return(ng_attach_common(so
, NG_DATA
));
512 * Set up a socket protocol control block.
513 * This code is shared between control and data sockets.
516 ng_attach_common(struct socket
*so
, int type
)
521 /* Standard socket setup stuff */
522 error
= soreserve(so
, ngpdg_sendspace
, ngpdg_recvspace
, NULL
);
526 /* Allocate the pcb */
527 pcbp
= kmalloc(sizeof(*pcbp
), M_PCB
, M_WAITOK
| M_ZERO
);
530 /* Link the pcb and the socket */
532 so
->so_pcb
= (caddr_t
) pcbp
;
533 pcbp
->ng_socket
= so
;
535 /* Add the socket to linked list */
536 LIST_INSERT_HEAD(&ngsocklist
, pcbp
, socks
);
541 * Disassociate the socket from it's protocol specific
542 * partner. If it's attached to a node's private data structure,
543 * then unlink from that too. If we were the last socket attached to it,
544 * then shut down the entire node. Shared code for control and data sockets.
547 ng_detach_common(struct ngpcb
*pcbp
, int which
)
549 struct ngsock
*sockdata
;
552 if (pcbp
->sockdata
) {
553 sockdata
= pcbp
->sockdata
;
554 pcbp
->sockdata
= NULL
;
557 sockdata
->ctlsock
= NULL
;
560 sockdata
->datasock
= NULL
;
565 if ((--sockdata
->refs
== 0) && (sockdata
->node
!= NULL
))
566 ng_rmnode(sockdata
->node
);
568 so
= pcbp
->ng_socket
;
570 pcbp
->ng_socket
= NULL
;
571 sofree(so
); /* remove pcb ref */
573 LIST_REMOVE(pcbp
, socks
);
579 * File descriptors can be passed into a AF_NETGRAPH socket.
580 * Note, that file descriptors cannot be passed OUT.
581 * Only character device descriptors are accepted.
582 * Character devices are useful to connect a graph to a device,
583 * which after all is the purpose of this whole system.
586 ng_internalize(struct mbuf
*control
, struct thread
*td
)
588 struct filedesc
*fdp
= p
->p_fd
;
589 const struct cmsghdr
*cm
= mtod(control
, const struct cmsghdr
*);
595 if (cm
->cmsg_type
!= SCM_RIGHTS
|| cm
->cmsg_level
!= SOL_SOCKET
||
596 cm
->cmsg_len
!= control
->m_len
) {
601 /* Check there is only one FD. XXX what would more than one signify? */
602 oldfds
= (cm
->cmsg_len
- sizeof(*cm
)) / sizeof(int);
608 /* Check that the FD given is legit. and change it to a pointer to a
610 fd
= *(int *) (cm
+ 1);
611 if ((unsigned) fd
>= fdp
->fd_nfiles
612 || (fp
= fdp
->fd_files
[fd
].fp
) == NULL
) {
616 /* Depending on what kind of resource it is, act differently. For
617 * devices, we treat it as a file. For a AF_NETGRAPH socket,
618 * shortcut straight to the node. */
619 switch (fp
->f_type
) {
621 vn
= (struct vnode
*) fp
->f_data
;
622 if (vn
&& (vn
->v_type
== VCHR
)) {
623 /* for a VCHR, actually reference the FILE */
625 /* XXX then what :) */
626 /* how to pass on to other modules? */
641 * Connect the data socket to a named control socket node.
644 ng_connect_data(struct sockaddr
*nam
, struct ngpcb
*pcbp
)
646 struct sockaddr_ng
*sap
;
648 struct ngsock
*sockdata
;
651 /* If we are already connected, don't do it again */
652 if (pcbp
->sockdata
!= NULL
)
655 /* Find the target (victim) and check it doesn't already have a data
656 * socket. Also check it is a 'socket' type node. */
657 sap
= (struct sockaddr_ng
*) nam
;
658 if ((error
= ng_path2node(NULL
, sap
->sg_data
, &farnode
, NULL
)))
661 if (strcmp(farnode
->type
->name
, NG_SOCKET_NODE_TYPE
) != 0)
663 sockdata
= farnode
->private;
664 if (sockdata
->datasock
!= NULL
)
667 /* Link the PCB and the private data struct. and note the extra
669 sockdata
->datasock
= pcbp
;
670 pcbp
->sockdata
= sockdata
;
676 * Connect the existing control socket node to a named node:hook.
677 * The hook we use on this end is the same name as the remote node name.
680 ng_connect_cntl(struct sockaddr
*nam
, struct ngpcb
*pcbp
)
682 struct ngsock
*const sockdata
= pcbp
->sockdata
;
683 struct sockaddr_ng
*sap
;
688 sap
= (struct sockaddr_ng
*) nam
;
689 rtn
= ng_path_parse(sap
->sg_data
, &node
, NULL
, &hook
);
690 if (rtn
< 0 || node
== NULL
|| hook
== NULL
) {
694 farnode
= ng_findname(sockdata
->node
, node
);
695 if (farnode
== NULL
) {
697 return (EADDRNOTAVAIL
);
700 /* Connect, using a hook name the same as the far node name. */
701 error
= ng_con_nodes(sockdata
->node
, node
, farnode
, hook
);
706 * Binding a socket means giving the corresponding node a name
709 ng_bind(struct sockaddr
*nam
, struct ngpcb
*pcbp
)
711 struct ngsock
*const sockdata
= pcbp
->sockdata
;
712 struct sockaddr_ng
*const sap
= (struct sockaddr_ng
*) nam
;
714 if (sockdata
== NULL
) {
718 if (sap
->sg_len
< 3 || sap
->sg_data
[sap
->sg_len
- 3] != '\0') {
722 return (ng_name_node(sockdata
->node
, sap
->sg_data
));
726 * Take a message and pass it up to the control socket associated
730 ship_msg(struct ngpcb
*pcbp
, struct ng_mesg
*msg
, struct sockaddr_ng
*addr
)
732 struct socket
*const so
= pcbp
->ng_socket
;
736 /* Copy the message itself into an mbuf chain */
737 msglen
= sizeof(struct ng_mesg
) + msg
->header
.arglen
;
738 mdata
= m_devget(msg
, msglen
, 0, NULL
);
740 /* Here we free the message, as we are the end of the line.
741 * We need to do that regardless of whether we got mbufs. */
742 kfree(msg
, M_NETGRAPH
);
749 /* Send it up to the socket */
750 lwkt_gettoken(&so
->so_rcv
.ssb_token
);
751 if (ssb_appendaddr(&so
->so_rcv
,
752 (struct sockaddr
*) addr
, mdata
, NULL
) == 0) {
754 lwkt_reltoken(&so
->so_rcv
.ssb_token
);
760 lwkt_reltoken(&so
->so_rcv
.ssb_token
);
765 * You can only create new nodes from the socket end of things.
768 ngs_constructor(node_p
*nodep
)
774 * We allow any hook to be connected to the node.
775 * There is no per-hook private information though.
778 ngs_newhook(node_p node
, hook_p hook
, const char *name
)
780 hook
->private = node
->private;
785 * Incoming messages get passed up to the control socket.
786 * Unless they are for us specifically (socket_type)
789 ngs_rcvmsg(node_p node
, struct ng_mesg
*msg
, const char *retaddr
,
790 struct ng_mesg
**resp
)
792 struct ngsock
*const sockdata
= node
->private;
793 struct ngpcb
*const pcbp
= sockdata
->ctlsock
;
794 struct sockaddr_ng
*addr
;
798 /* Only allow mesgs to be passed if we have the control socket.
799 * Data sockets can only support the generic messages. */
805 if (msg
->header
.typecookie
== NGM_SOCKET_COOKIE
) {
806 switch (msg
->header
.cmd
) {
807 case NGM_SOCK_CMD_NOLINGER
:
808 sockdata
->flags
|= NGS_FLAG_NOLINGER
;
810 case NGM_SOCK_CMD_LINGER
:
811 sockdata
->flags
&= ~NGS_FLAG_NOLINGER
;
814 error
= EINVAL
; /* unknown command */
816 /* Free the message and return */
817 kfree(msg
, M_NETGRAPH
);
821 /* Get the return address into a sockaddr */
822 if ((retaddr
== NULL
) || (*retaddr
== '\0'))
824 addrlen
= strlen(retaddr
);
825 addr
= kmalloc(addrlen
+ 4, M_NETGRAPH
, M_NOWAIT
);
830 addr
->sg_len
= addrlen
+ 3;
831 addr
->sg_family
= AF_NETGRAPH
;
832 bcopy(retaddr
, addr
->sg_data
, addrlen
);
833 addr
->sg_data
[addrlen
] = '\0';
836 error
= ship_msg(pcbp
, msg
, addr
);
837 kfree(addr
, M_NETGRAPH
);
842 * Receive data on a hook
845 ngs_rcvdata(hook_p hook
, struct mbuf
*m
, meta_p meta
)
847 struct ngsock
*const sockdata
= hook
->node
->private;
848 struct ngpcb
*const pcbp
= sockdata
->datasock
;
850 struct sockaddr_ng
*addr
;
851 char *addrbuf
[NG_HOOKSIZ
+ 4];
854 /* If there is no data socket, black-hole it */
856 NG_FREE_DATA(m
, meta
);
859 so
= pcbp
->ng_socket
;
861 /* Get the return address into a sockaddr. */
862 addrlen
= strlen(hook
->name
); /* <= NG_HOOKSIZ - 1 */
863 addr
= (struct sockaddr_ng
*) addrbuf
;
864 addr
->sg_len
= addrlen
+ 3;
865 addr
->sg_family
= AF_NETGRAPH
;
866 bcopy(hook
->name
, addr
->sg_data
, addrlen
);
867 addr
->sg_data
[addrlen
] = '\0';
869 /* We have no use for the meta data, free/clear it now. */
872 /* Try to tell the socket which hook it came in on */
873 lwkt_gettoken(&so
->so_rcv
.ssb_token
);
874 if (ssb_appendaddr(&so
->so_rcv
, (struct sockaddr
*) addr
, m
, NULL
) == 0) {
876 lwkt_reltoken(&so
->so_rcv
.ssb_token
);
882 lwkt_reltoken(&so
->so_rcv
.ssb_token
);
889 * For this type, removal of the last link destroys the node
890 * if the NOLINGER flag is set.
893 ngs_disconnect(hook_p hook
)
895 struct ngsock
*const sockdata
= hook
->node
->private;
897 if ((sockdata
->flags
& NGS_FLAG_NOLINGER
)
898 && (hook
->node
->numhooks
== 0)) {
899 ng_rmnode(hook
->node
);
905 * Do local shutdown processing.
906 * In this case, that involves making sure the socket
907 * knows we should be shutting down.
910 ngs_rmnode(node_p node
)
912 struct ngsock
*const sockdata
= node
->private;
913 struct ngpcb
*const dpcbp
= sockdata
->datasock
;
914 struct ngpcb
*const pcbp
= sockdata
->ctlsock
;
920 soisdisconnected(dpcbp
->ng_socket
);
921 dpcbp
->sockdata
= NULL
;
922 sockdata
->datasock
= NULL
;
926 soisdisconnected(pcbp
->ng_socket
);
927 pcbp
->sockdata
= NULL
;
928 sockdata
->ctlsock
= NULL
;
931 node
->private = NULL
;
933 kfree(sockdata
, M_NETGRAPH
);
938 * Control and data socket type descriptors
941 static struct pr_usrreqs ngc_usrreqs
= {
943 .pru_accept
= pr_generic_notsupp
,
944 .pru_attach
= ngc_attach
,
945 .pru_bind
= ngc_bind
,
946 .pru_connect
= ngc_connect
,
947 .pru_connect2
= pr_generic_notsupp
,
948 .pru_control
= pr_generic_notsupp
,
949 .pru_detach
= ngc_detach
,
950 .pru_disconnect
= NULL
,
951 .pru_listen
= pr_generic_notsupp
,
952 .pru_peeraddr
= NULL
,
953 .pru_rcvd
= pr_generic_notsupp
,
954 .pru_rcvoob
= pr_generic_notsupp
,
955 .pru_send
= ngc_send
,
956 .pru_sense
= pru_sense_null
,
957 .pru_shutdown
= NULL
,
958 .pru_sockaddr
= ng_setsockaddr
,
959 .pru_sosend
= sosend
,
960 .pru_soreceive
= soreceive
963 static struct pr_usrreqs ngd_usrreqs
= {
965 .pru_accept
= pr_generic_notsupp
,
966 .pru_attach
= ngd_attach
,
968 .pru_connect
= ngd_connect
,
969 .pru_connect2
= pr_generic_notsupp
,
970 .pru_control
= pr_generic_notsupp
,
971 .pru_detach
= ngd_detach
,
972 .pru_disconnect
= NULL
,
973 .pru_listen
= pr_generic_notsupp
,
974 .pru_peeraddr
= NULL
,
975 .pru_rcvd
= pr_generic_notsupp
,
976 .pru_rcvoob
= pr_generic_notsupp
,
977 .pru_send
= ngd_send
,
978 .pru_sense
= pru_sense_null
,
979 .pru_shutdown
= NULL
,
980 .pru_sockaddr
= ng_setsockaddr
,
981 .pru_sosend
= sosend
,
982 .pru_soreceive
= soreceive
986 * Definitions of protocols supported in the NETGRAPH domain.
989 extern struct domain ngdomain
; /* stop compiler warnings */
991 static struct protosw ngsw
[] = {
993 .pr_type
= SOCK_DGRAM
,
994 .pr_domain
= &ngdomain
,
995 .pr_protocol
= NG_CONTROL
,
996 .pr_flags
= PR_ATOMIC
| PR_ADDR
/* | PR_RIGHTS */,
997 .pr_usrreqs
= &ngc_usrreqs
1000 .pr_type
= SOCK_DGRAM
,
1001 .pr_domain
= &ngdomain
,
1002 .pr_protocol
= NG_DATA
,
1003 .pr_flags
= PR_ATOMIC
| PR_ADDR
,
1004 .pr_usrreqs
= &ngd_usrreqs
1008 struct domain ngdomain
= {
1009 .dom_family
= AF_NETGRAPH
,
1010 .dom_name
= "netgraph",
1011 .dom_protosw
= ngsw
,
1012 .dom_protoswNPROTOSW
= &ngsw
[NELEM(ngsw
)],
1016 * Handle loading and unloading for this node type
1017 * This is to handle auxiliary linkages (e.g protocol domain addition).
1020 ngs_mod_event(module_t mod
, int event
, void *data
)
1026 /* Register protocol domain */
1027 net_add_domain(&ngdomain
);
1030 /* Insure there are no open netgraph sockets */
1031 if (!LIST_EMPTY(&ngsocklist
)) {
1037 /* Unregister protocol domain XXX can't do this yet.. */
1038 if ((error
= net_rm_domain(&ngdomain
)) != 0)
1051 SYSCTL_INT(_net_graph
, OID_AUTO
, family
, CTLFLAG_RD
, 0, AF_NETGRAPH
, "");
1052 SYSCTL_NODE(_net_graph
, OID_AUTO
, data
, CTLFLAG_RW
, 0, "DATA");
1053 SYSCTL_INT(_net_graph_data
, OID_AUTO
, proto
, CTLFLAG_RD
, 0, NG_DATA
, "");
1054 SYSCTL_NODE(_net_graph
, OID_AUTO
, control
, CTLFLAG_RW
, 0, "CONTROL");
1055 SYSCTL_INT(_net_graph_control
, OID_AUTO
, proto
, CTLFLAG_RD
, 0, NG_CONTROL
, "");