xref: /freebsd/sys/netgraph/ng_socket.c (revision 10534a60e8a62e6e70320bfa2e7fbe5ffe5ee4ba)
1 
2 /*
3  * ng_socket.c
4  *
5  * Copyright (c) 1996-1999 Whistle Communications, Inc.
6  * All rights reserved.
7  *
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.
18  *
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
35  * OF SUCH DAMAGE.
36  *
37  * Author: Julian Elischer <julian@whistle.com>
38  *
39  * $FreeBSD$
40  * $Whistle: ng_socket.c,v 1.28 1999/11/01 09:24:52 julian Exp $
41  */
42 
43 /*
44  * Netgraph socket nodes
45  *
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.
49  */
50 
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/proc.h>
54 #include <sys/domain.h>
55 #include <sys/errno.h>
56 #include <sys/kernel.h>
57 #include <sys/file.h>
58 #include <sys/filedesc.h>
59 #include <sys/malloc.h>
60 #include <sys/queue.h>
61 #include <sys/mbuf.h>
62 #include <sys/protosw.h>
63 #include <sys/socket.h>
64 #include <sys/socketvar.h>
65 #include <sys/sysctl.h>
66 #ifdef NOTYET
67 #include <sys/vnode.h>
68 #endif
69 #include <netgraph/ng_message.h>
70 #include <netgraph/netgraph.h>
71 #include <netgraph/ng_socketvar.h>
72 #include <netgraph/ng_socket.h>
73 
74 /*
75  * It's Ascii-art time!
76  *   +-------------+   +-------------+
77  *   |socket  (ctl)|   |socket (data)|
78  *   +-------------+   +-------------+
79  *          ^                 ^
80  *          |                 |
81  *          v                 v
82  *    +-----------+     +-----------+
83  *    |pcb   (ctl)|     |pcb  (data)|
84  *    +-----------+     +-----------+
85  *          ^                 ^
86  *          |                 |
87  *          v                 v
88  *      +--------------------------+
89  *      |   Socket type private    |
90  *      |       data               |
91  *      +--------------------------+
92  *                   ^
93  *                   |
94  *                   v
95  *           +----------------+
96  *           | struct ng_node |
97  *           +----------------+
98  */
99 
100 /* Netgraph node methods */
101 static ng_constructor_t	ngs_constructor;
102 static ng_rcvmsg_t	ngs_rcvmsg;
103 static ng_shutdown_t	ngs_rmnode;
104 static ng_newhook_t	ngs_newhook;
105 static ng_rcvdata_t	ngs_rcvdata;
106 static ng_disconnect_t	ngs_disconnect;
107 
108 /* Internal methods */
109 static int	ng_attach_data(struct socket *so);
110 static int	ng_attach_cntl(struct socket *so);
111 static int	ng_attach_common(struct socket *so, int type);
112 static void	ng_detach_common(struct ngpcb *pcbp, int type);
113 /*static int	ng_internalize(struct mbuf *m, struct proc *p); */
114 
115 static int	ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp);
116 static int	ng_connect_cntl(struct sockaddr *nam, struct ngpcb *pcbp);
117 static int	ng_bind(struct sockaddr *nam, struct ngpcb *pcbp);
118 
119 static int	ngs_mod_event(module_t mod, int event, void *data);
120 static int	ship_msg(struct ngpcb *pcbp, struct ng_mesg *msg,
121 			struct sockaddr_ng *addr);
122 
123 /* Netgraph type descriptor */
124 static struct ng_type typestruct = {
125 	NG_VERSION,
126 	NG_SOCKET_NODE_TYPE,
127 	ngs_mod_event,
128 	ngs_constructor,
129 	ngs_rcvmsg,
130 	ngs_rmnode,
131 	ngs_newhook,
132 	NULL,
133 	NULL,
134 	ngs_rcvdata,
135 	ngs_rcvdata,
136 	ngs_disconnect
137 };
138 NETGRAPH_INIT(socket, &typestruct);
139 
140 /* Buffer space */
141 static u_long ngpdg_sendspace = 2 * 1024;	/* really max datagram size */
142 static u_long ngpdg_recvspace = 20 * 1024;
143 
144 /* List of all sockets */
145 LIST_HEAD(, ngpcb) ngsocklist;
146 
147 #define sotongpcb(so) ((struct ngpcb *)so->so_pcb)
148 
149 /* If getting unexplained errors returned, set this to "Debugger("X"); */
150 #ifndef TRAP_ERROR
151 #define TRAP_ERROR
152 #endif
153 
154 /***************************************************************
155 	Control sockets
156 ***************************************************************/
157 
158 static int
159 ngc_attach(struct socket *so, int proto, struct proc *p)
160 {
161 	struct ngpcb *const pcbp = sotongpcb(so);
162 
163 	if (pcbp != NULL)
164 		return (EISCONN);
165 	return (ng_attach_cntl(so));
166 }
167 
168 static int
169 ngc_detach(struct socket *so)
170 {
171 	struct ngpcb *const pcbp = sotongpcb(so);
172 
173 	if (pcbp == NULL)
174 		return (EINVAL);
175 	ng_detach_common(pcbp, NG_CONTROL);
176 	return (0);
177 }
178 
179 static int
180 ngc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
181 	 struct mbuf *control, struct proc *p)
182 {
183 	struct ngpcb *const pcbp = sotongpcb(so);
184 	struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
185 	struct ng_mesg *resp;
186 	struct mbuf *m0;
187 	char *msg, *path = NULL;
188 	int len, error = 0;
189 
190 	if (pcbp == NULL) {
191 		error = EINVAL;
192 		goto release;
193 	}
194 #ifdef	NOTYET
195 	if (control && (error = ng_internalize(control, p))) {
196 		if (pcbp->sockdata == NULL) {
197 			error = ENOTCONN;
198 			goto release;
199 		}
200 	}
201 #else	/* NOTYET */
202 	if (control) {
203 		error = EINVAL;
204 		goto release;
205 	}
206 #endif	/* NOTYET */
207 
208 	/* Require destination as there may be >= 1 hooks on this node */
209 	if (addr == NULL) {
210 		error = EDESTADDRREQ;
211 		goto release;
212 	}
213 
214 	/* Allocate an expendable buffer for the path, chop off
215 	 * the sockaddr header, and make sure it's NUL terminated */
216 	len = sap->sg_len - 2;
217 	MALLOC(path, char *, len + 1, M_NETGRAPH, M_WAITOK);
218 	if (path == NULL) {
219 		error = ENOMEM;
220 		goto release;
221 	}
222 	bcopy(sap->sg_data, path, len);
223 	path[len] = '\0';
224 
225 	/* Move the actual message out of mbufs into a linear buffer.
226 	 * Start by adding up the size of the data. (could use mh_len?) */
227 	for (len = 0, m0 = m; m0 != NULL; m0 = m0->m_next)
228 		len += m0->m_len;
229 
230 	/* Move the data into a linear buffer as well. Messages are not
231 	 * delivered in mbufs. */
232 	MALLOC(msg, char *, len + 1, M_NETGRAPH, M_WAITOK);
233 	if (msg == NULL) {
234 		error = ENOMEM;
235 		goto release;
236 	}
237 	m_copydata(m, 0, len, msg);
238 
239 	/* The callee will free the msg when done. The addr is our business. */
240 	error = ng_send_msg(pcbp->sockdata->node,
241 			    (struct ng_mesg *) msg, path, &resp);
242 
243 	/* If the callee responded with a synchronous response, then put it
244 	 * back on the receive side of the socket; sap is source address. */
245 	if (error == 0 && resp != NULL)
246 		error = ship_msg(pcbp, resp, sap);
247 
248 release:
249 	if (path != NULL)
250 		FREE(path, M_NETGRAPH);
251 	if (control != NULL)
252 		m_freem(control);
253 	if (m != NULL)
254 		m_freem(m);
255 	return (error);
256 }
257 
258 static int
259 ngc_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
260 {
261 	struct ngpcb *const pcbp = sotongpcb(so);
262 
263 	if (pcbp == 0)
264 		return (EINVAL);
265 	return (ng_bind(nam, pcbp));
266 }
267 
268 static int
269 ngc_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
270 {
271 	struct ngpcb *const pcbp = sotongpcb(so);
272 
273 	if (pcbp == 0)
274 		return (EINVAL);
275 	return (ng_connect_cntl(nam, pcbp));
276 }
277 
278 /***************************************************************
279 	Data sockets
280 ***************************************************************/
281 
282 static int
283 ngd_attach(struct socket *so, int proto, struct proc *p)
284 {
285 	struct ngpcb *const pcbp = sotongpcb(so);
286 
287 	if (pcbp != NULL)
288 		return (EISCONN);
289 	return (ng_attach_data(so));
290 }
291 
292 static int
293 ngd_detach(struct socket *so)
294 {
295 	struct ngpcb *const pcbp = sotongpcb(so);
296 
297 	if (pcbp == NULL)
298 		return (EINVAL);
299 	ng_detach_common(pcbp, NG_DATA);
300 	return (0);
301 }
302 
303 static int
304 ngd_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
305 	 struct mbuf *control, struct proc *p)
306 {
307 	struct ngpcb *const pcbp = sotongpcb(so);
308 	struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
309 	char   *hookname = NULL;
310 	meta_p  mp = NULL;
311 	int     len, error;
312 	hook_p  hook;
313 
314 	if ((pcbp == NULL) || (control != NULL)) {
315 		error = EINVAL;
316 		goto release;
317 	}
318 	if (pcbp->sockdata == NULL) {
319 		error = ENOTCONN;
320 		goto release;
321 	}
322 	if (addr == NULL) {
323 		error = EDESTADDRREQ;
324 		goto release;
325 	}
326 
327 	/* Allocate an expendable buffer for the hook name, chop off
328 	 * the sockaddr header, and make sure it's NUL terminated */
329 	len = sap->sg_len - 2;
330 	MALLOC(hookname, char *, len + 1, M_NETGRAPH, M_WAITOK);
331 	if (hookname == NULL) {
332 		error = ENOMEM;
333 		goto release;
334 	}
335 	bcopy(sap->sg_data, hookname, len);
336 	hookname[len] = '\0';
337 
338 	/* Find the correct hook from 'hookname' */
339 	LIST_FOREACH(hook, &pcbp->sockdata->node->hooks, hooks) {
340 		if (strcmp(hookname, hook->name) == 0)
341 			break;
342 	}
343 
344 	/* Send data (OK if hook is NULL) */
345 	NG_SEND_DATA(error, hook, m, mp);	/* makes m NULL */
346 
347 release:
348 	if (hookname != NULL)
349 		FREE(hookname, M_NETGRAPH);
350 	if (control != NULL)
351 		m_freem(control);
352 	if (m != NULL)
353 		m_freem(m);
354 	return (error);
355 }
356 
357 static int
358 ngd_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
359 {
360 	struct ngpcb *const pcbp = sotongpcb(so);
361 
362 	if (pcbp == 0)
363 		return (EINVAL);
364 	return (ng_connect_data(nam, pcbp));
365 }
366 
367 /*
368  * Used for both data and control sockets
369  */
370 static int
371 ng_setsockaddr(struct socket *so, struct sockaddr **addr)
372 {
373 	struct ngpcb *pcbp;
374 	struct sockaddr_ng *sg;
375 	int sg_len, namelen, s;
376 
377 	/* Why isn't sg_data a `char[1]' ? :-( */
378 	sg_len = sizeof(struct sockaddr_ng) - sizeof(sg->sg_data) + 1;
379 
380 	s = splnet();
381 	pcbp = sotongpcb(so);
382 	if (pcbp == 0) {
383 		splx(s);
384 		return (EINVAL);
385 	}
386 
387 	namelen = 0;		/* silence compiler ! */
388 
389 	if (pcbp->sockdata->node->name != NULL)
390 		sg_len += namelen = strlen(pcbp->sockdata->node->name);
391 
392 	MALLOC(sg, struct sockaddr_ng *, sg_len, M_SONAME, M_WAITOK);
393 	bzero(sg, sg_len);
394 
395 	if (pcbp->sockdata->node->name != NULL)
396 		bcopy(pcbp->sockdata->node->name, sg->sg_data, namelen);
397 	splx(s);
398 
399 	sg->sg_len = sg_len;
400 	sg->sg_family = AF_NETGRAPH;
401 	*addr = (struct sockaddr *)sg;
402 
403 	return (0);
404 }
405 
406 /*
407  * Attach a socket to it's protocol specific partner.
408  * For a control socket, actually create a netgraph node and attach
409  * to it as well.
410  */
411 
412 static int
413 ng_attach_cntl(struct socket *so)
414 {
415 	struct ngsock *privdata;
416 	struct ngpcb *pcbp;
417 	int error;
418 
419 	/* Setup protocol control block */
420 	if ((error = ng_attach_common(so, NG_CONTROL)) != 0)
421 		return (error);
422 	pcbp = (struct ngpcb *) so->so_pcb;
423 
424 	/* Allocate node private info */
425 	MALLOC(privdata, struct ngsock *,
426 	    sizeof(*privdata), M_NETGRAPH, M_WAITOK);
427 	if (privdata == NULL) {
428 		ng_detach_common(pcbp, NG_CONTROL);
429 		return (ENOMEM);
430 	}
431 	bzero(privdata, sizeof(*privdata));
432 
433 	/* Make the generic node components */
434 	if ((error = ng_make_node_common(&typestruct, &privdata->node)) != 0) {
435 		FREE(privdata, M_NETGRAPH);
436 		ng_detach_common(pcbp, NG_CONTROL);
437 		return (error);
438 	}
439 	privdata->node->private = privdata;
440 
441 	/* Link the pcb and the node private data */
442 	privdata->ctlsock = pcbp;
443 	pcbp->sockdata = privdata;
444 	privdata->refs++;
445 	return (0);
446 }
447 
448 static int
449 ng_attach_data(struct socket *so)
450 {
451 	return(ng_attach_common(so, NG_DATA));
452 }
453 
454 /*
455  * Set up a socket protocol control block.
456  * This code is shared between control and data sockets.
457  */
458 static int
459 ng_attach_common(struct socket *so, int type)
460 {
461 	struct ngpcb *pcbp;
462 	int error;
463 
464 	/* Standard socket setup stuff */
465 	error = soreserve(so, ngpdg_sendspace, ngpdg_recvspace);
466 	if (error)
467 		return (error);
468 
469 	/* Allocate the pcb */
470 	MALLOC(pcbp, struct ngpcb *, sizeof(*pcbp), M_PCB, M_WAITOK);
471 	if (pcbp == NULL)
472 		return (ENOMEM);
473 	bzero(pcbp, sizeof(*pcbp));
474 	pcbp->type = type;
475 
476 	/* Link the pcb and the socket */
477 	so->so_pcb = (caddr_t) pcbp;
478 	pcbp->ng_socket = so;
479 
480 	/* Add the socket to linked list */
481 	LIST_INSERT_HEAD(&ngsocklist, pcbp, socks);
482 	return (0);
483 }
484 
485 /*
486  * Disassociate the socket from it's protocol specific
487  * partner. If it's attached to a node's private data structure,
488  * then unlink from that too. If we were the last socket attached to it,
489  * then shut down the entire node. Shared code for control and data sockets.
490  */
491 static void
492 ng_detach_common(struct ngpcb *pcbp, int which)
493 {
494 	struct ngsock *sockdata;
495 
496 	if (pcbp->sockdata) {
497 		sockdata = pcbp->sockdata;
498 		pcbp->sockdata = NULL;
499 		switch (which) {
500 		case NG_CONTROL:
501 			sockdata->ctlsock = NULL;
502 			break;
503 		case NG_DATA:
504 			sockdata->datasock = NULL;
505 			break;
506 		default:
507 			panic(__FUNCTION__);
508 		}
509 		if ((--sockdata->refs == 0) && (sockdata->node != NULL))
510 			ng_rmnode(sockdata->node);
511 	}
512 	pcbp->ng_socket->so_pcb = NULL;
513 	pcbp->ng_socket = NULL;
514 	LIST_REMOVE(pcbp, socks);
515 	FREE(pcbp, M_PCB);
516 }
517 
518 #ifdef NOTYET
519 /*
520  * File descriptors can be passed into a AF_NETGRAPH socket.
521  * Note, that file descriptors cannot be passed OUT.
522  * Only character device descriptors are accepted.
523  * Character devices are useful to connect a graph to a device,
524  * which after all is the purpose of this whole system.
525  */
526 static int
527 ng_internalize(struct mbuf *control, struct proc *p)
528 {
529 	struct filedesc *fdp = p->p_fd;
530 	struct cmsghdr *cm = mtod(control, struct cmsghdr *);
531 	struct file *fp;
532 	struct vnode *vn;
533 	int oldfds;
534 	int fd;
535 
536 	if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
537 	    cm->cmsg_len != control->m_len) {
538 		TRAP_ERROR;
539 		return (EINVAL);
540 	}
541 
542 	/* Check there is only one FD. XXX what would more than one signify? */
543 	oldfds = (cm->cmsg_len - sizeof(*cm)) / sizeof(int);
544 	if (oldfds != 1) {
545 		TRAP_ERROR;
546 		return (EINVAL);
547 	}
548 
549 	/* Check that the FD given is legit. and change it to a pointer to a
550 	 * struct file. */
551 	fd = *(int *) (cm + 1);
552 	if ((unsigned) fd >= fdp->fd_nfiles
553 	    || (fp = fdp->fd_ofiles[fd]) == NULL) {
554 		return (EBADF);
555 	}
556 
557 	/* Depending on what kind of resource it is, act differently. For
558 	 * devices, we treat it as a file. For a AF_NETGRAPH socket,
559 	 * shortcut straight to the node. */
560 	switch (fp->f_type) {
561 	case DTYPE_VNODE:
562 		vn = (struct vnode *) fp->f_data;
563 		if (vn && (vn->v_type == VCHR)) {
564 			/* for a VCHR, actually reference the FILE */
565 			fp->f_count++;
566 			/* XXX then what :) */
567 			/* how to pass on to other modules? */
568 		} else {
569 			TRAP_ERROR;
570 			return (EINVAL);
571 		}
572 		break;
573 	default:
574 		TRAP_ERROR;
575 		return (EINVAL);
576 	}
577 	return (0);
578 }
579 #endif	/* NOTYET */
580 
581 /*
582  * Connect the data socket to a named control socket node.
583  */
584 static int
585 ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp)
586 {
587 	struct sockaddr_ng *sap;
588 	node_p farnode;
589 	struct ngsock *sockdata;
590 	int error;
591 
592 	/* If we are already connected, don't do it again */
593 	if (pcbp->sockdata != NULL)
594 		return (EISCONN);
595 
596 	/* Find the target (victim) and check it doesn't already have a data
597 	 * socket. Also check it is a 'socket' type node. */
598 	sap = (struct sockaddr_ng *) nam;
599 	if ((error = ng_path2node(NULL, sap->sg_data, &farnode, NULL)))
600 		return (error);
601 
602 	if (strcmp(farnode->type->name, NG_SOCKET_NODE_TYPE) != 0)
603 		return (EINVAL);
604 	sockdata = farnode->private;
605 	if (sockdata->datasock != NULL)
606 		return (EADDRINUSE);
607 
608 	/* Link the PCB and the private data struct. and note the extra
609 	 * reference */
610 	sockdata->datasock = pcbp;
611 	pcbp->sockdata = sockdata;
612 	sockdata->refs++;
613 	return (0);
614 }
615 
616 /*
617  * Connect the existing control socket node to a named node:hook.
618  * The hook we use on this end is the same name as the remote node name.
619  */
620 static int
621 ng_connect_cntl(struct sockaddr *nam, struct ngpcb *pcbp)
622 {
623 	struct ngsock *const sockdata = pcbp->sockdata;
624 	struct sockaddr_ng *sap;
625 	char *node, *hook;
626 	node_p farnode;
627 	int rtn, error;
628 
629 	sap = (struct sockaddr_ng *) nam;
630 	rtn = ng_path_parse(sap->sg_data, &node, NULL, &hook);
631 	if (rtn < 0 || node == NULL || hook == NULL) {
632 		TRAP_ERROR;
633 		return (EINVAL);
634 	}
635 	farnode = ng_findname(sockdata->node, node);
636 	if (farnode == NULL) {
637 		TRAP_ERROR;
638 		return (EADDRNOTAVAIL);
639 	}
640 
641 	/* Connect, using a hook name the same as the far node name. */
642 	error = ng_con_nodes(sockdata->node, node, farnode, hook);
643 	return error;
644 }
645 
646 /*
647  * Binding a socket means giving the corresponding node a name
648  */
649 static int
650 ng_bind(struct sockaddr *nam, struct ngpcb *pcbp)
651 {
652 	struct ngsock *const sockdata = pcbp->sockdata;
653 	struct sockaddr_ng *const sap = (struct sockaddr_ng *) nam;
654 
655 	if (sockdata == NULL) {
656 		TRAP_ERROR;
657 		return (EINVAL);
658 	}
659 	if (sap->sg_len < 3 || sap->sg_data[sap->sg_len - 3] != '\0') {
660 		TRAP_ERROR;
661 		return (EINVAL);
662 	}
663 	return (ng_name_node(sockdata->node, sap->sg_data));
664 }
665 
666 /*
667  * Take a message and pass it up to the control socket associated
668  * with the node.
669  */
670 static int
671 ship_msg(struct ngpcb *pcbp, struct ng_mesg *msg, struct sockaddr_ng *addr)
672 {
673 	struct socket *const so = pcbp->ng_socket;
674 	struct mbuf *mdata;
675 	int msglen;
676 
677 	/* Copy the message itself into an mbuf chain */
678 	msglen = sizeof(struct ng_mesg) + msg->header.arglen;
679 	mdata = m_devget((caddr_t) msg, msglen, 0, NULL, NULL);
680 
681 	/* Here we free the message, as we are the end of the line.
682 	 * We need to do that regardless of whether we got mbufs. */
683 	FREE(msg, M_NETGRAPH);
684 
685 	if (mdata == NULL) {
686 		TRAP_ERROR;
687 		return (ENOBUFS);
688 	}
689 
690 	/* Send it up to the socket */
691 	if (sbappendaddr(&so->so_rcv,
692 	    (struct sockaddr *) addr, mdata, NULL) == 0) {
693 		TRAP_ERROR;
694 		m_freem(mdata);
695 		return (ENOBUFS);
696 	}
697 	sorwakeup(so);
698 	return (0);
699 }
700 
701 /*
702  * You can only create new nodes from the socket end of things.
703  */
704 static int
705 ngs_constructor(node_p *nodep)
706 {
707 	return (EINVAL);
708 }
709 
710 /*
711  * We allow any hook to be connected to the node.
712  * There is no per-hook private information though.
713  */
714 static int
715 ngs_newhook(node_p node, hook_p hook, const char *name)
716 {
717 	hook->private = node->private;
718 	return (0);
719 }
720 
721 /*
722  * Incoming messages get passed up to the control socket.
723  * Unless they are for us specifically (socket_type)
724  */
725 static int
726 ngs_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr,
727 	   struct ng_mesg **resp)
728 {
729 	struct ngsock *const sockdata = node->private;
730 	struct ngpcb *const pcbp = sockdata->ctlsock;
731 	struct sockaddr_ng *addr;
732 	int addrlen;
733 	int error = 0;
734 
735 	/* Only allow mesgs to be passed if we have the control socket.
736 	 * Data sockets can only support the generic messages. */
737 	if (pcbp == NULL) {
738 		TRAP_ERROR;
739 		return (EINVAL);
740 	}
741 
742 	if (msg->header.typecookie == NGM_SOCKET_COOKIE) {
743 		switch (msg->header.cmd) {
744 		case NGM_SOCK_CMD_NOLINGER:
745 			sockdata->flags |= NGS_FLAG_NOLINGER;
746 			break;
747 		case NGM_SOCK_CMD_LINGER:
748 			sockdata->flags &= ~NGS_FLAG_NOLINGER;
749 			break;
750 		default:
751 			error = EINVAL;		/* unknown command */
752 		}
753 		/* Free the message and return */
754 		FREE(msg, M_NETGRAPH);
755 		return(error);
756 
757 	}
758 	/* Get the return address into a sockaddr */
759 	if ((retaddr == NULL) || (*retaddr == '\0'))
760 		retaddr = "";
761 	addrlen = strlen(retaddr);
762 	MALLOC(addr, struct sockaddr_ng *, addrlen + 4, M_NETGRAPH, M_NOWAIT);
763 	if (addr == NULL) {
764 		TRAP_ERROR;
765 		return (ENOMEM);
766 	}
767 	addr->sg_len = addrlen + 3;
768 	addr->sg_family = AF_NETGRAPH;
769 	bcopy(retaddr, addr->sg_data, addrlen);
770 	addr->sg_data[addrlen] = '\0';
771 
772 	/* Send it up */
773 	error = ship_msg(pcbp, msg, addr);
774 	FREE(addr, M_NETGRAPH);
775 	return (error);
776 }
777 
778 /*
779  * Receive data on a hook
780  */
781 static int
782 ngs_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
783 {
784 	struct ngsock *const sockdata = hook->node->private;
785 	struct ngpcb *const pcbp = sockdata->datasock;
786 	struct socket *so;
787 	struct sockaddr_ng *addr;
788 	char *addrbuf[NG_HOOKLEN + 1 + 4];
789 	int addrlen;
790 
791 	/* If there is no data socket, black-hole it */
792 	if (pcbp == NULL) {
793 		NG_FREE_DATA(m, meta);
794 		return (0);
795 	}
796 	so = pcbp->ng_socket;
797 
798 	/* Get the return address into a sockaddr. */
799 	addrlen = strlen(hook->name);	/* <= NG_HOOKLEN */
800 	addr = (struct sockaddr_ng *) addrbuf;
801 	addr->sg_len = addrlen + 3;
802 	addr->sg_family = AF_NETGRAPH;
803 	bcopy(hook->name, addr->sg_data, addrlen);
804 	addr->sg_data[addrlen] = '\0';
805 
806 	/* We have no use for the meta data, free/clear it now. */
807 	NG_FREE_META(meta);
808 
809 	/* Try to tell the socket which hook it came in on */
810 	if (sbappendaddr(&so->so_rcv, (struct sockaddr *) addr, m, NULL) == 0) {
811 		m_freem(m);
812 		TRAP_ERROR;
813 		return (ENOBUFS);
814 	}
815 	sorwakeup(so);
816 	return (0);
817 }
818 
819 /*
820  * Dook disconnection
821  *
822  * For this type, removal of the last link destroys the node
823  * if the NOLINGER flag is set.
824  */
825 static int
826 ngs_disconnect(hook_p hook)
827 {
828 	struct ngsock *const sockdata = hook->node->private;
829 
830 	if ((sockdata->flags & NGS_FLAG_NOLINGER )
831 	&& (hook->node->numhooks == 0)) {
832 		ng_rmnode(hook->node);
833 	}
834 	return (0);
835 }
836 
837 /*
838  * Do local shutdown processing.
839  * In this case, that involves making sure the socket
840  * knows we should be shutting down.
841  */
842 static int
843 ngs_rmnode(node_p node)
844 {
845 	struct ngsock *const sockdata = node->private;
846 	struct ngpcb *const dpcbp = sockdata->datasock;
847 	struct ngpcb *const pcbp = sockdata->ctlsock;
848 
849 	ng_cutlinks(node);
850 	ng_unname(node);
851 
852 	if (dpcbp != NULL) {
853 		soisdisconnected(dpcbp->ng_socket);
854 		dpcbp->sockdata = NULL;
855 		sockdata->datasock = NULL;
856 		sockdata->refs--;
857 	}
858 	if (pcbp != NULL) {
859 		soisdisconnected(pcbp->ng_socket);
860 		pcbp->sockdata = NULL;
861 		sockdata->ctlsock = NULL;
862 		sockdata->refs--;
863 	}
864 	node->private = NULL;
865 	ng_unref(node);
866 	FREE(sockdata, M_NETGRAPH);
867 	return (0);
868 }
869 
870 /*
871  * Control and data socket type descriptors
872  */
873 
874 static struct pr_usrreqs ngc_usrreqs = {
875 	NULL,			/* abort */
876 	pru_accept_notsupp,
877 	ngc_attach,
878 	ngc_bind,
879 	ngc_connect,
880 	pru_connect2_notsupp,
881 	pru_control_notsupp,
882 	ngc_detach,
883 	NULL,			/* disconnect */
884 	pru_listen_notsupp,
885 	NULL,			/* setpeeraddr */
886 	pru_rcvd_notsupp,
887 	pru_rcvoob_notsupp,
888 	ngc_send,
889 	pru_sense_null,
890 	NULL,			/* shutdown */
891 	ng_setsockaddr,
892 	sosend,
893 	soreceive,
894 	sopoll
895 };
896 
897 static struct pr_usrreqs ngd_usrreqs = {
898 	NULL,			/* abort */
899 	pru_accept_notsupp,
900 	ngd_attach,
901 	NULL,			/* bind */
902 	ngd_connect,
903 	pru_connect2_notsupp,
904 	pru_control_notsupp,
905 	ngd_detach,
906 	NULL,			/* disconnect */
907 	pru_listen_notsupp,
908 	NULL,			/* setpeeraddr */
909 	pru_rcvd_notsupp,
910 	pru_rcvoob_notsupp,
911 	ngd_send,
912 	pru_sense_null,
913 	NULL,			/* shutdown */
914 	ng_setsockaddr,
915 	sosend,
916 	soreceive,
917 	sopoll
918 };
919 
920 /*
921  * Definitions of protocols supported in the NETGRAPH domain.
922  */
923 
924 extern struct domain ngdomain;		/* stop compiler warnings */
925 
926 static struct protosw ngsw[] = {
927 	{
928 		SOCK_DGRAM,
929 		&ngdomain,
930 		NG_CONTROL,
931 		PR_ATOMIC | PR_ADDR /* | PR_RIGHTS */,
932 		0, 0, 0, 0,
933 		NULL,
934 		0, 0, 0, 0,
935 		&ngc_usrreqs
936 	},
937 	{
938 		SOCK_DGRAM,
939 		&ngdomain,
940 		NG_DATA,
941 		PR_ATOMIC | PR_ADDR,
942 		0, 0, 0, 0,
943 		NULL,
944 		0, 0, 0, 0,
945 		&ngd_usrreqs
946 	}
947 };
948 
949 struct domain ngdomain = {
950 	AF_NETGRAPH,
951 	"netgraph",
952 	0,
953 	NULL,
954 	NULL,
955 	ngsw,
956 	&ngsw[sizeof(ngsw) / sizeof(ngsw[0])],
957 	0,
958 	NULL,
959 	0,
960 	0
961 };
962 
963 /*
964  * Handle loading and unloading for this node type
965  * This is to handle auxiliary linkages (e.g protocol domain addition).
966  */
967 static int
968 ngs_mod_event(module_t mod, int event, void *data)
969 {
970 	int error = 0;
971 
972 	switch (event) {
973 	case MOD_LOAD:
974 		/* Register protocol domain */
975 		net_add_domain(&ngdomain);
976 		break;
977 	case MOD_UNLOAD:
978 		/* Insure there are no open netgraph sockets */
979 		if (!LIST_EMPTY(&ngsocklist)) {
980 			error = EBUSY;
981 			break;
982 		}
983 
984 #ifdef NOTYET
985 		/* Unregister protocol domain XXX can't do this yet.. */
986 		if ((error = net_rm_domain(&ngdomain)) != 0)
987 			break;
988 #else
989 		error = EBUSY;
990 #endif
991 		break;
992 	default:
993 		error = EOPNOTSUPP;
994 		break;
995 	}
996 	return (error);
997 }
998 
999 SYSCTL_NODE(_net, AF_NETGRAPH, graph, CTLFLAG_RW, 0, "netgraph Family");
1000 SYSCTL_INT(_net_graph, OID_AUTO, family, CTLFLAG_RD, 0, AF_NETGRAPH, "");
1001 SYSCTL_NODE(_net_graph, OID_AUTO, data, CTLFLAG_RW, 0, "DATA");
1002 SYSCTL_INT(_net_graph_data, OID_AUTO, proto, CTLFLAG_RD, 0, NG_DATA, "");
1003 SYSCTL_NODE(_net_graph, OID_AUTO, control, CTLFLAG_RW, 0, "CONTROL");
1004 SYSCTL_INT(_net_graph_control, OID_AUTO, proto, CTLFLAG_RD, 0, NG_CONTROL, "");
1005 
1006