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