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