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