1 /*
2 * ng_socket.c
3 */
4
5 /*-
6 * Copyright (c) 1996-1999 Whistle Communications, Inc.
7 * All rights reserved.
8 *
9 * Subject to the following obligations and disclaimer of warranty, use and
10 * redistribution of this software, in source or object code forms, with or
11 * without modifications are expressly permitted by Whistle Communications;
12 * provided, however, that:
13 * 1. Any and all reproductions of the source or object code must include the
14 * copyright notice above and the following disclaimer of warranties; and
15 * 2. No rights are granted, in any manner or form, to use Whistle
16 * Communications, Inc. trademarks, including the mark "WHISTLE
17 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
18 * such appears in the above copyright notice or in the software.
19 *
20 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
21 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
22 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
23 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
25 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
26 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
27 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
28 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
29 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
36 * OF SUCH DAMAGE.
37 *
38 * Author: Julian Elischer <julian@freebsd.org>
39 *
40 * $FreeBSD$
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/domain.h>
54 #include <sys/errno.h>
55 #include <sys/kdb.h>
56 #include <sys/kernel.h>
57 #include <sys/linker.h>
58 #include <sys/lock.h>
59 #include <sys/malloc.h>
60 #include <sys/mbuf.h>
61 #include <sys/mutex.h>
62 #include <sys/protosw.h>
63 #include <sys/queue.h>
64 #include <sys/signalvar.h>
65 #include <sys/socket.h>
66 #include <sys/socketvar.h>
67 #include <sys/sx.h>
68 #include <sys/sysctl.h>
69 #include <sys/systm.h>
70 #ifdef NOTYET
71 #include <sys/vnode.h>
72 #endif
73 #include <netgraph/ng_message.h>
74 #include <netgraph/netgraph.h>
75 #include <netgraph/ng_socketvar.h>
76 #include <netgraph/ng_socket.h>
77
78 #ifdef NG_SEPARATE_MALLOC
79 MALLOC_DEFINE(M_NETGRAPH_PATH, "netgraph_path", "netgraph path info ");
80 MALLOC_DEFINE(M_NETGRAPH_SOCK, "netgraph_sock", "netgraph socket info ");
81 #else
82 #define M_NETGRAPH_PATH M_NETGRAPH
83 #define M_NETGRAPH_SOCK M_NETGRAPH
84 #endif
85
86 /*
87 * It's Ascii-art time!
88 * +-------------+ +-------------+
89 * |socket (ctl)| |socket (data)|
90 * +-------------+ +-------------+
91 * ^ ^
92 * | |
93 * v v
94 * +-----------+ +-----------+
95 * |pcb (ctl)| |pcb (data)|
96 * +-----------+ +-----------+
97 * ^ ^
98 * | |
99 * v v
100 * +--------------------------+
101 * | Socket type private |
102 * | data |
103 * +--------------------------+
104 * ^
105 * |
106 * v
107 * +----------------+
108 * | struct ng_node |
109 * +----------------+
110 */
111
112 /* Netgraph node methods */
113 static ng_constructor_t ngs_constructor;
114 static ng_rcvmsg_t ngs_rcvmsg;
115 static ng_shutdown_t ngs_shutdown;
116 static ng_newhook_t ngs_newhook;
117 static ng_connect_t ngs_connect;
118 static ng_rcvdata_t ngs_rcvdata;
119 static ng_disconnect_t ngs_disconnect;
120
121 /* Internal methods */
122 static int ng_attach_data(struct socket *so);
123 static int ng_attach_cntl(struct socket *so);
124 static int ng_attach_common(struct socket *so, int type);
125 static void ng_detach_common(struct ngpcb *pcbp, int type);
126 static void ng_socket_free_priv(struct ngsock *priv);
127 /*static int ng_internalize(struct mbuf *m, struct thread *p); */
128
129 static int ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp);
130 static int ng_bind(struct sockaddr *nam, struct ngpcb *pcbp);
131
132 static int ngs_mod_event(module_t mod, int event, void *data);
133 static int ship_msg(struct ngpcb *pcbp, struct ng_mesg *msg,
134 struct sockaddr_ng *addr);
135 static void ng_socket_item_applied(void *context, int error);
136
137 /* Netgraph type descriptor */
138 static struct ng_type typestruct = {
139 .version = NG_ABI_VERSION,
140 .name = NG_SOCKET_NODE_TYPE,
141 .mod_event = ngs_mod_event,
142 .constructor = ngs_constructor,
143 .rcvmsg = ngs_rcvmsg,
144 .shutdown = ngs_shutdown,
145 .newhook = ngs_newhook,
146 .connect = ngs_connect,
147 .rcvdata = ngs_rcvdata,
148 .disconnect = ngs_disconnect,
149 };
150 NETGRAPH_INIT_ORDERED(socket, &typestruct, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
151
152 /* Buffer space */
153 static u_long ngpdg_sendspace = 20 * 1024; /* really max datagram size */
154 SYSCTL_INT(_net_graph, OID_AUTO, maxdgram, CTLFLAG_RW,
155 &ngpdg_sendspace , 0, "Maximum outgoing Netgraph datagram size");
156 static u_long ngpdg_recvspace = 20 * 1024;
157 SYSCTL_INT(_net_graph, OID_AUTO, recvspace, CTLFLAG_RW,
158 &ngpdg_recvspace , 0, "Maximum space for incoming Netgraph datagrams");
159
160 /* List of all sockets */
161 static LIST_HEAD(, ngpcb) ngsocklist;
162
163 static struct mtx ngsocketlist_mtx;
164
165 #define sotongpcb(so) ((struct ngpcb *)(so)->so_pcb)
166
167 /* If getting unexplained errors returned, set this to "kdb_enter("X"); */
168 #ifndef TRAP_ERROR
169 #define TRAP_ERROR
170 #endif
171
172 /***************************************************************
173 Control sockets
174 ***************************************************************/
175
176 static int
177 ngc_attach(struct socket *so, int proto, struct thread *td)
178 {
179 struct ngpcb *const pcbp = sotongpcb(so);
180
181 if (suser(td))
182 return (EPERM);
183 if (pcbp != NULL)
184 return (EISCONN);
185 return (ng_attach_cntl(so));
186 }
187
188 static int
189 ngc_detach(struct socket *so)
190 {
191 struct ngpcb *const pcbp = sotongpcb(so);
192
193 if (pcbp == NULL)
194 return (EINVAL);
195 ng_detach_common(pcbp, NG_CONTROL);
196 return (0);
197 }
198
199 static int
200 ngc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
201 struct mbuf *control, struct thread *td)
202 {
203 struct ngpcb *const pcbp = sotongpcb(so);
204 struct ngsock *const priv = NG_NODE_PRIVATE(pcbp->sockdata->node);
205 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
206 struct ng_mesg *msg;
207 struct mbuf *m0;
208 item_p item;
209 char *path = NULL;
210 int len, error = 0;
211 struct ng_apply_info apply;
212
213 #ifdef NOTYET
214 if (control && (error = ng_internalize(control, td))) {
215 if (pcbp->sockdata == NULL) {
216 error = ENOTCONN;
217 goto release;
218 }
219 }
220 #else /* NOTYET */
221 if (control) {
222 error = EINVAL;
223 goto release;
224 }
225 #endif /* NOTYET */
226
227 /* Require destination as there may be >= 1 hooks on this node */
228 if (addr == NULL) {
229 error = EDESTADDRREQ;
230 goto release;
231 }
232
233 /* Allocate an expendable buffer for the path, chop off
234 * the sockaddr header, and make sure it's NUL terminated */
235 len = sap->sg_len - 2;
236 MALLOC(path, char *, len + 1, M_NETGRAPH_PATH, M_WAITOK);
237 if (path == NULL) {
238 error = ENOMEM;
239 goto release;
240 }
241 bcopy(sap->sg_data, path, len);
242 path[len] = '\0';
243
244 /* Move the actual message out of mbufs into a linear buffer.
245 * Start by adding up the size of the data. (could use mh_len?) */
246 for (len = 0, m0 = m; m0 != NULL; m0 = m0->m_next)
247 len += m0->m_len;
248
249 /* Move the data into a linear buffer as well. Messages are not
250 * delivered in mbufs. */
251 MALLOC(msg, struct ng_mesg *, len + 1, M_NETGRAPH_MSG, M_WAITOK);
252 if (msg == NULL) {
253 error = ENOMEM;
254 goto release;
255 }
256 m_copydata(m, 0, len, (char *)msg);
257
258 if (msg->header.version != NG_VERSION) {
259 FREE(msg, M_NETGRAPH_MSG);
260 error = EINVAL;
261 goto release;
262 }
263
264 /*
265 * Hack alert!
266 * We look into the message and if it mkpeers a node of unknown type, we
267 * try to load it. We need to do this now, in syscall thread, because if
268 * message gets queued and applied later we will get panic.
269 */
270 if (msg->header.typecookie == NGM_GENERIC_COOKIE &&
271 msg->header.cmd == NGM_MKPEER) {
272 struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data;
273 struct ng_type *type;
274
275 if ((type = ng_findtype(mkp->type)) == NULL) {
276 char filename[NG_TYPESIZ + 3];
277 linker_file_t lf;
278
279 /* Not found, try to load it as a loadable module. */
280 snprintf(filename, sizeof(filename), "ng_%s",
281 mkp->type);
282 mtx_lock(&Giant);
283 error = linker_load_module(NULL, filename, NULL, NULL,
284 &lf);
285 mtx_unlock(&Giant);
286 if (error != 0) {
287 FREE(msg, M_NETGRAPH_MSG);
288 goto release;
289 }
290 lf->userrefs++;
291
292 /* See if type has been loaded successfully. */
293 if ((type = ng_findtype(mkp->type)) == NULL) {
294 FREE(msg, M_NETGRAPH_MSG);
295 error = ENXIO;
296 goto release;
297 }
298 }
299 }
300
301 if ((item = ng_package_msg(msg, M_WAITOK)) == NULL) {
302 error = ENOMEM;
303 #ifdef TRACE_MESSAGES
304 printf("ng_package_msg: err=%d\n", error);
305 #endif
306 goto release;
307 }
308 if ((error = ng_address_path((pcbp->sockdata->node), item,
309 path, 0)) != 0) {
310 #ifdef TRACE_MESSAGES
311 printf("ng_address_path: errx=%d\n", error);
312 #endif
313 goto release;
314 }
315
316 #ifdef TRACE_MESSAGES
317 printf("[%x]:<---------[socket]: c=<%d>cmd=%x(%s) f=%x #%d (%s)\n",
318 item->el_dest->nd_ID,
319 msg->header.typecookie,
320 msg->header.cmd,
321 msg->header.cmdstr,
322 msg->header.flags,
323 msg->header.token,
324 item->el_dest->nd_type->name);
325 #endif
326 SAVE_LINE(item);
327 /*
328 * We do not want to return from syscall until the item
329 * is processed by destination node. We register callback
330 * on the item, which will update priv->error when item
331 * was applied.
332 * If ng_snd_item() has queued item, we sleep until
333 * callback wakes us up.
334 */
335 bzero(&apply, sizeof(apply));
336 apply.apply = ng_socket_item_applied;
337 apply.context = priv;
338 item->apply = &apply;
339 priv->error = -1;
340
341 error = ng_snd_item(item, 0);
342
343 mtx_lock(&priv->mtx);
344 if (priv->error == -1)
345 msleep(priv, &priv->mtx, 0, "ngsock", 0);
346 mtx_unlock(&priv->mtx);
347 KASSERT(priv->error != -1,
348 ("ng_socket: priv->error wasn't updated"));
349 error = priv->error;
350
351 release:
352 if (path != NULL)
353 FREE(path, M_NETGRAPH_PATH);
354 if (control != NULL)
355 m_freem(control);
356 if (m != NULL)
357 m_freem(m);
358 return (error);
359 }
360
361 static int
362 ngc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
363 {
364 struct ngpcb *const pcbp = sotongpcb(so);
365
366 if (pcbp == 0)
367 return (EINVAL);
368 return (ng_bind(nam, pcbp));
369 }
370
371 static int
372 ngc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
373 {
374 printf(" program tried to connect control socket to remote node\n ");
375 /*
376 * At this time refuse to do this.. it used to
377 * do something but it was undocumented and not used.
378 */
379 return (EINVAL);
380 }
381
382 /***************************************************************
383 Data sockets
384 ***************************************************************/
385
386 static int
387 ngd_attach(struct socket *so, int proto, struct thread *td)
388 {
389 struct ngpcb *const pcbp = sotongpcb(so);
390
391 if (pcbp != NULL)
392 return (EISCONN);
393 return (ng_attach_data(so));
394 }
395
396 static int
397 ngd_detach(struct socket *so)
398 {
399 struct ngpcb *const pcbp = sotongpcb(so);
400
401 if (pcbp == NULL)
402 return (EINVAL);
403 ng_detach_common(pcbp, NG_DATA);
404 return (0);
405 }
406
407 static int
408 ngd_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
409 struct mbuf *control, struct thread *td)
410 {
411 struct ngpcb *const pcbp = sotongpcb(so);
412 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
413 int len, error;
414 hook_p hook = NULL;
415 char hookname[NG_HOOKSIZ];
416
417 if ((pcbp == NULL) || (control != NULL)) {
418 error = EINVAL;
419 goto release;
420 }
421 if (pcbp->sockdata == NULL) {
422 error = ENOTCONN;
423 goto release;
424 }
425
426 if (sap == NULL)
427 len = 0; /* Make compiler happy. */
428 else
429 len = sap->sg_len - 2;
430
431 /*
432 * If the user used any of these ways to not specify an address
433 * then handle specially.
434 */
435 if ((sap == NULL) || (len <= 0) || (*sap->sg_data == '\0')) {
436 if (NG_NODE_NUMHOOKS(pcbp->sockdata->node) != 1) {
437 error = EDESTADDRREQ;
438 goto release;
439 }
440 /*
441 * if exactly one hook exists, just use it.
442 * Special case to allow write(2) to work on an ng_socket.
443 */
444 hook = LIST_FIRST(&pcbp->sockdata->node->nd_hooks);
445 } else {
446 if (len >= NG_HOOKSIZ) {
447 error = EINVAL;
448 goto release;
449 }
450
451 /*
452 * chop off the sockaddr header, and make sure it's NUL
453 * terminated
454 */
455 bcopy(sap->sg_data, hookname, len);
456 hookname[len] = '\0';
457
458 /* Find the correct hook from 'hookname' */
459 LIST_FOREACH(hook, &pcbp->sockdata->node->nd_hooks, hk_hooks) {
460 if (strcmp(hookname, NG_HOOK_NAME(hook)) == 0) {
461 break;
462 }
463 }
464 if (hook == NULL) {
465 error = EHOSTUNREACH;
466 }
467 }
468
469 /* Send data (OK if hook is NULL) */
470 NG_SEND_DATA_ONLY(error, hook, m); /* makes m NULL */
471
472 release:
473 if (control != NULL)
474 m_freem(control);
475 if (m != NULL)
476 m_freem(m);
477 return (error);
478 }
479
480 static int
481 ngd_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
482 {
483 struct ngpcb *const pcbp = sotongpcb(so);
484
485 if (pcbp == 0)
486 return (EINVAL);
487 return (ng_connect_data(nam, pcbp));
488 }
489
490 /*
491 * Used for both data and control sockets
492 */
493 static int
494 ng_setsockaddr(struct socket *so, struct sockaddr **addr)
495 {
496 struct ngpcb *pcbp;
497 struct sockaddr_ng *sg;
498 int sg_len;
499 int error = 0;
500
501 /* Why isn't sg_data a `char[1]' ? :-( */
502 sg_len = sizeof(struct sockaddr_ng) - sizeof(sg->sg_data) + 1;
503
504 pcbp = sotongpcb(so);
505 if ((pcbp == NULL) || (pcbp->sockdata == NULL))
506 /* XXXGL: can this still happen? */
507 return (EINVAL);
508
509 mtx_lock(&pcbp->sockdata->mtx);
510 if (pcbp->sockdata->node != NULL) {
511 node_p node = pcbp->sockdata->node;
512 int namelen = 0; /* silence compiler! */
513
514 if (NG_NODE_HAS_NAME(node))
515 sg_len += namelen = strlen(NG_NODE_NAME(node));
516
517 sg = malloc(sg_len, M_SONAME, M_WAITOK | M_ZERO);
518
519 if (NG_NODE_HAS_NAME(node))
520 bcopy(NG_NODE_NAME(node), sg->sg_data, namelen);
521
522 sg->sg_len = sg_len;
523 sg->sg_family = AF_NETGRAPH;
524 *addr = (struct sockaddr *)sg;
525 mtx_unlock(&pcbp->sockdata->mtx);
526 } else {
527 mtx_unlock(&pcbp->sockdata->mtx);
528 error = EINVAL;
529 }
530
531 return (error);
532 }
533
534 /*
535 * Attach a socket to it's protocol specific partner.
536 * For a control socket, actually create a netgraph node and attach
537 * to it as well.
538 */
539
540 static int
541 ng_attach_cntl(struct socket *so)
542 {
543 struct ngsock *priv;
544 struct ngpcb *pcbp;
545 int error;
546
547 /* Allocate node private info */
548 MALLOC(priv, struct ngsock *,
549 sizeof(*priv), M_NETGRAPH_SOCK, M_WAITOK | M_ZERO);
550 if (priv == NULL)
551 return (ENOMEM);
552
553 /* Setup protocol control block */
554 if ((error = ng_attach_common(so, NG_CONTROL)) != 0) {
555 FREE(priv, M_NETGRAPH_SOCK);
556 return (error);
557 }
558 pcbp = sotongpcb(so);
559
560 /* Link the pcb the private data. */
561 priv->ctlsock = pcbp;
562 pcbp->sockdata = priv;
563 priv->refs++;
564
565 /* Initialize mutex. */
566 mtx_init(&priv->mtx, "ng_socket", NULL, MTX_DEF);
567
568 /* Make the generic node components */
569 if ((error = ng_make_node_common(&typestruct, &priv->node)) != 0) {
570 FREE(priv, M_NETGRAPH_SOCK);
571 ng_detach_common(pcbp, NG_CONTROL);
572 return (error);
573 }
574
575 /* Link the node and the private data. */
576 NG_NODE_SET_PRIVATE(priv->node, priv);
577 NG_NODE_REF(priv->node);
578 priv->refs++;
579
580 return (0);
581 }
582
583 static int
584 ng_attach_data(struct socket *so)
585 {
586 return(ng_attach_common(so, NG_DATA));
587 }
588
589 /*
590 * Set up a socket protocol control block.
591 * This code is shared between control and data sockets.
592 */
593 static int
594 ng_attach_common(struct socket *so, int type)
595 {
596 struct ngpcb *pcbp;
597 int error;
598
599 /* Standard socket setup stuff */
600 error = soreserve(so, ngpdg_sendspace, ngpdg_recvspace);
601 if (error)
602 return (error);
603
604 /* Allocate the pcb */
605 MALLOC(pcbp, struct ngpcb *, sizeof(*pcbp), M_PCB, M_WAITOK | M_ZERO);
606 if (pcbp == NULL)
607 return (ENOMEM);
608 pcbp->type = type;
609
610 /* Link the pcb and the socket */
611 so->so_pcb = (caddr_t) pcbp;
612 pcbp->ng_socket = so;
613
614 /* Add the socket to linked list */
615 mtx_lock(&ngsocketlist_mtx);
616 LIST_INSERT_HEAD(&ngsocklist, pcbp, socks);
617 mtx_unlock(&ngsocketlist_mtx);
618 return (0);
619 }
620
621 /*
622 * Disassociate the socket from it's protocol specific
623 * partner. If it's attached to a node's private data structure,
624 * then unlink from that too. If we were the last socket attached to it,
625 * then shut down the entire node. Shared code for control and data sockets.
626 */
627 static void
628 ng_detach_common(struct ngpcb *pcbp, int which)
629 {
630 struct ngsock *priv = pcbp->sockdata;
631
632 if (priv != NULL) {
633 mtx_lock(&priv->mtx);
634
635 switch (which) {
636 case NG_CONTROL:
637 priv->ctlsock = NULL;
638 break;
639 case NG_DATA:
640 priv->datasock = NULL;
641 break;
642 default:
643 panic(__func__);
644 }
645 pcbp->sockdata = NULL;
646
647 ng_socket_free_priv(priv);
648 }
649
650 pcbp->ng_socket->so_pcb = NULL;
651 mtx_lock(&ngsocketlist_mtx);
652 LIST_REMOVE(pcbp, socks);
653 mtx_unlock(&ngsocketlist_mtx);
654 FREE(pcbp, M_PCB);
655 }
656
657 /*
658 * Remove a reference from node private data.
659 */
660 static void
661 ng_socket_free_priv(struct ngsock *priv)
662 {
663 mtx_assert(&priv->mtx, MA_OWNED);
664
665 priv->refs--;
666
667 if (priv->refs == 0) {
668 mtx_destroy(&priv->mtx);
669 FREE(priv, M_NETGRAPH_SOCK);
670 return;
671 }
672
673 if ((priv->refs == 1) && (priv->node != NULL)) {
674 node_p node = priv->node;
675
676 priv->node = NULL;
677 mtx_unlock(&priv->mtx);
678 NG_NODE_UNREF(node);
679 ng_rmnode_self(node);
680 } else
681 mtx_unlock(&priv->mtx);
682 }
683
684 #ifdef NOTYET
685 /*
686 * File descriptors can be passed into an AF_NETGRAPH socket.
687 * Note, that file descriptors cannot be passed OUT.
688 * Only character device descriptors are accepted.
689 * Character devices are useful to connect a graph to a device,
690 * which after all is the purpose of this whole system.
691 */
692 static int
693 ng_internalize(struct mbuf *control, struct thread *td)
694 {
695 const struct cmsghdr *cm = mtod(control, const struct cmsghdr *);
696 struct file *fp;
697 struct vnode *vn;
698 int oldfds;
699 int fd;
700
701 if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
702 cm->cmsg_len != control->m_len) {
703 TRAP_ERROR;
704 return (EINVAL);
705 }
706
707 /* Check there is only one FD. XXX what would more than one signify? */
708 oldfds = ((caddr_t)cm + cm->cmsg_len - (caddr_t)data) / sizeof (int);
709 if (oldfds != 1) {
710 TRAP_ERROR;
711 return (EINVAL);
712 }
713
714 /* Check that the FD given is legit. and change it to a pointer to a
715 * struct file. */
716 fd = CMSG_DATA(cm);
717 if ((error = fget(td, fd, &fp)) != 0)
718 return (error);
719
720 /* Depending on what kind of resource it is, act differently. For
721 * devices, we treat it as a file. For an AF_NETGRAPH socket,
722 * shortcut straight to the node. */
723 switch (fp->f_type) {
724 case DTYPE_VNODE:
725 vn = fp->f_data;
726 if (vn && (vn->v_type == VCHR)) {
727 /* for a VCHR, actually reference the FILE */
728 fp->f_count++;
729 /* XXX then what :) */
730 /* how to pass on to other modules? */
731 } else {
732 fdrop(fp, td);
733 TRAP_ERROR;
734 return (EINVAL);
735 }
736 break;
737 default:
738 fdrop(fp, td);
739 TRAP_ERROR;
740 return (EINVAL);
741 }
742 fdrop(fp, td);
743 return (0);
744 }
745 #endif /* NOTYET */
746
747 /*
748 * Connect the data socket to a named control socket node.
749 */
750 static int
751 ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp)
752 {
753 struct sockaddr_ng *sap;
754 node_p farnode;
755 struct ngsock *priv;
756 int error;
757 item_p item;
758
759 /* If we are already connected, don't do it again */
760 if (pcbp->sockdata != NULL)
761 return (EISCONN);
762
763 /* Find the target (victim) and check it doesn't already have a data
764 * socket. Also check it is a 'socket' type node.
765 * Use ng_package_data() and address_path() to do this.
766 */
767
768 sap = (struct sockaddr_ng *) nam;
769 /* The item will hold the node reference */
770 item = ng_package_data(NULL, NG_WAITOK);
771 if (item == NULL) {
772 return (ENOMEM);
773 }
774 if ((error = ng_address_path(NULL, item, sap->sg_data, 0)))
775 return (error); /* item is freed on failure */
776
777 /*
778 * Extract node from item and free item. Remember we now have
779 * a reference on the node. The item holds it for us.
780 * when we free the item we release the reference.
781 */
782 farnode = item->el_dest; /* shortcut */
783 if (strcmp(farnode->nd_type->name, NG_SOCKET_NODE_TYPE) != 0) {
784 NG_FREE_ITEM(item); /* drop the reference to the node */
785 return (EINVAL);
786 }
787 priv = NG_NODE_PRIVATE(farnode);
788 if (priv->datasock != NULL) {
789 NG_FREE_ITEM(item); /* drop the reference to the node */
790 return (EADDRINUSE);
791 }
792
793 /*
794 * Link the PCB and the private data struct. and note the extra
795 * reference. Drop the extra reference on the node.
796 */
797 mtx_lock(&priv->mtx);
798 priv->datasock = pcbp;
799 pcbp->sockdata = priv;
800 priv->refs++;
801 mtx_unlock(&priv->mtx);
802 NG_FREE_ITEM(item); /* drop the reference to the node */
803 return (0);
804 }
805
806 /*
807 * Binding a socket means giving the corresponding node a name
808 */
809 static int
810 ng_bind(struct sockaddr *nam, struct ngpcb *pcbp)
811 {
812 struct ngsock *const priv = pcbp->sockdata;
813 struct sockaddr_ng *const sap = (struct sockaddr_ng *) nam;
814
815 if (priv == NULL) {
816 TRAP_ERROR;
817 return (EINVAL);
818 }
819 if ((sap->sg_len < 4)
820 || (sap->sg_len > (NG_NODESIZ + 2))
821 || (sap->sg_data[0] == '\0')
822 || (sap->sg_data[sap->sg_len - 3] != '\0')) {
823 TRAP_ERROR;
824 return (EINVAL);
825 }
826 return (ng_name_node(priv->node, sap->sg_data));
827 }
828
829 /*
830 * Take a message and pass it up to the control socket associated
831 * with the node.
832 */
833 static int
834 ship_msg(struct ngpcb *pcbp, struct ng_mesg *msg, struct sockaddr_ng *addr)
835 {
836 struct socket *const so = pcbp->ng_socket;
837 struct mbuf *mdata;
838 int msglen;
839 int error = 0;
840
841 /* Copy the message itself into an mbuf chain */
842 msglen = sizeof(struct ng_mesg) + msg->header.arglen;
843 mdata = m_devget((caddr_t) msg, msglen, 0, NULL, NULL);
844
845 /* Here we free the message, as we are the end of the line.
846 * We need to do that regardless of whether we got mbufs. */
847 NG_FREE_MSG(msg);
848
849 if (mdata == NULL) {
850 TRAP_ERROR;
851 return (ENOBUFS);
852 }
853
854 /* Send it up to the socket */
855 if (sbappendaddr(&so->so_rcv,
856 (struct sockaddr *) addr, mdata, NULL) == 0) {
857 TRAP_ERROR;
858 m_freem(mdata);
859 return (ENOBUFS);
860 }
861 sorwakeup(so);
862 return (error);
863 }
864
865 /***************************************************************
866 Netgraph node
867 ***************************************************************/
868
869 /*
870 * You can only create new nodes from the socket end of things.
871 */
872 static int
873 ngs_constructor(node_p nodep)
874 {
875 return (EINVAL);
876 }
877
878 /*
879 * We allow any hook to be connected to the node.
880 * There is no per-hook private information though.
881 */
882 static int
883 ngs_newhook(node_p node, hook_p hook, const char *name)
884 {
885 NG_HOOK_SET_PRIVATE(hook, NG_NODE_PRIVATE(node));
886 return (0);
887 }
888
889 /*
890 * if only one hook, allow read(2) and write(2) to work.
891 */
892 static int
893 ngs_connect(hook_p hook)
894 {
895 node_p node = NG_HOOK_NODE(hook);
896 struct ngsock *priv = NG_NODE_PRIVATE(node);
897
898 if ((priv->datasock)
899 && (priv->datasock->ng_socket)) {
900 if (NG_NODE_NUMHOOKS(node) == 1) {
901 priv->datasock->ng_socket->so_state |= SS_ISCONNECTED;
902 } else {
903 priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED;
904 }
905 }
906 return (0);
907 }
908
909 /*
910 * Incoming messages get passed up to the control socket.
911 * Unless they are for us specifically (socket_type)
912 */
913 static int
914 ngs_rcvmsg(node_p node, item_p item, hook_p lasthook)
915 {
916 struct ngsock *const priv = NG_NODE_PRIVATE(node);
917 struct ngpcb *const pcbp = priv->ctlsock;
918 struct sockaddr_ng *addr;
919 int addrlen;
920 int error = 0;
921 struct ng_mesg *msg;
922 ng_ID_t retaddr = NGI_RETADDR(item);
923 char retabuf[32];
924
925 NGI_GET_MSG(item, msg);
926 NG_FREE_ITEM(item); /* we have all we need */
927
928 /* Only allow mesgs to be passed if we have the control socket.
929 * Data sockets can only support the generic messages. */
930 if (pcbp == NULL) {
931 TRAP_ERROR;
932 return (EINVAL);
933 }
934
935 #ifdef TRACE_MESSAGES
936 printf("[%x]:---------->[socket]: c=<%d>cmd=%x(%s) f=%x #%d\n",
937 retaddr,
938 msg->header.typecookie,
939 msg->header.cmd,
940 msg->header.cmdstr,
941 msg->header.flags,
942 msg->header.token);
943 #endif
944
945 if (msg->header.typecookie == NGM_SOCKET_COOKIE) {
946 switch (msg->header.cmd) {
947 case NGM_SOCK_CMD_NOLINGER:
948 priv->flags |= NGS_FLAG_NOLINGER;
949 break;
950 case NGM_SOCK_CMD_LINGER:
951 priv->flags &= ~NGS_FLAG_NOLINGER;
952 break;
953 default:
954 error = EINVAL; /* unknown command */
955 }
956 /* Free the message and return */
957 NG_FREE_MSG(msg);
958 return(error);
959
960 }
961 /* Get the return address into a sockaddr */
962 sprintf(retabuf,"[%x]:", retaddr);
963 addrlen = strlen(retabuf);
964 MALLOC(addr, struct sockaddr_ng *, addrlen + 4, M_NETGRAPH_PATH, M_NOWAIT);
965 if (addr == NULL) {
966 TRAP_ERROR;
967 return (ENOMEM);
968 }
969 addr->sg_len = addrlen + 3;
970 addr->sg_family = AF_NETGRAPH;
971 bcopy(retabuf, addr->sg_data, addrlen);
972 addr->sg_data[addrlen] = '\0';
973
974 /* Send it up */
975 error = ship_msg(pcbp, msg, addr);
976 FREE(addr, M_NETGRAPH_PATH);
977 return (error);
978 }
979
980 /*
981 * Receive data on a hook
982 */
983 static int
984 ngs_rcvdata(hook_p hook, item_p item)
985 {
986 struct ngsock *const priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
987 struct ngpcb *const pcbp = priv->datasock;
988 struct socket *so;
989 struct sockaddr_ng *addr;
990 char *addrbuf[NG_HOOKSIZ + 4];
991 int addrlen;
992 struct mbuf *m;
993
994 NGI_GET_M(item, m);
995 NG_FREE_ITEM(item);
996 /* If there is no data socket, black-hole it */
997 if (pcbp == NULL) {
998 NG_FREE_M(m);
999 return (0);
1000 }
1001 so = pcbp->ng_socket;
1002
1003 /* Get the return address into a sockaddr. */
1004 addrlen = strlen(NG_HOOK_NAME(hook)); /* <= NG_HOOKSIZ - 1 */
1005 addr = (struct sockaddr_ng *) addrbuf;
1006 addr->sg_len = addrlen + 3;
1007 addr->sg_family = AF_NETGRAPH;
1008 bcopy(NG_HOOK_NAME(hook), addr->sg_data, addrlen);
1009 addr->sg_data[addrlen] = '\0';
1010
1011 /* Try to tell the socket which hook it came in on */
1012 if (sbappendaddr(&so->so_rcv, (struct sockaddr *) addr, m, NULL) == 0) {
1013 m_freem(m);
1014 TRAP_ERROR;
1015 return (ENOBUFS);
1016 }
1017 sorwakeup(so);
1018 return (0);
1019 }
1020
1021 /*
1022 * Hook disconnection
1023 *
1024 * For this type, removal of the last link destroys the node
1025 * if the NOLINGER flag is set.
1026 */
1027 static int
1028 ngs_disconnect(hook_p hook)
1029 {
1030 node_p node = NG_HOOK_NODE(hook);
1031 struct ngsock *const priv = NG_NODE_PRIVATE(node);
1032
1033 if ((priv->datasock)
1034 && (priv->datasock->ng_socket)) {
1035 if (NG_NODE_NUMHOOKS(node) == 1) {
1036 priv->datasock->ng_socket->so_state |= SS_ISCONNECTED;
1037 } else {
1038 priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED;
1039 }
1040 }
1041
1042 if ((priv->flags & NGS_FLAG_NOLINGER )
1043 && (NG_NODE_NUMHOOKS(node) == 0)
1044 && (NG_NODE_IS_VALID(node))) {
1045 ng_rmnode_self(node);
1046 }
1047 return (0);
1048 }
1049
1050 /*
1051 * Do local shutdown processing.
1052 * In this case, that involves making sure the socket
1053 * knows we should be shutting down.
1054 */
1055 static int
1056 ngs_shutdown(node_p node)
1057 {
1058 struct ngsock *const priv = NG_NODE_PRIVATE(node);
1059 struct ngpcb *const dpcbp = priv->datasock;
1060 struct ngpcb *const pcbp = priv->ctlsock;
1061
1062 if (dpcbp != NULL)
1063 soisdisconnected(dpcbp->ng_socket);
1064
1065 if (pcbp != NULL)
1066 soisdisconnected(pcbp->ng_socket);
1067
1068 mtx_lock(&priv->mtx);
1069 priv->node = NULL;
1070 NG_NODE_SET_PRIVATE(node, NULL);
1071 ng_socket_free_priv(priv);
1072
1073 NG_NODE_UNREF(node);
1074 return (0);
1075 }
1076
1077 static void
1078 ng_socket_item_applied(void *context, int error)
1079 {
1080 struct ngsock *const priv = (struct ngsock *)context;
1081
1082 mtx_lock(&priv->mtx);
1083 priv->error = error;
1084 wakeup(priv);
1085 mtx_unlock(&priv->mtx);
1086
1087 }
1088
1089 static int
1090 dummy_disconnect(struct socket *so)
1091 {
1092 return (0);
1093 }
1094 /*
1095 * Control and data socket type descriptors
1096 */
1097
1098 static struct pr_usrreqs ngc_usrreqs = {
1099 .pru_abort = NULL,
1100 .pru_attach = ngc_attach,
1101 .pru_bind = ngc_bind,
1102 .pru_connect = ngc_connect,
1103 .pru_detach = ngc_detach,
1104 .pru_disconnect = dummy_disconnect,
1105 .pru_peeraddr = NULL,
1106 .pru_send = ngc_send,
1107 .pru_shutdown = NULL,
1108 .pru_sockaddr = ng_setsockaddr,
1109 };
1110
1111 static struct pr_usrreqs ngd_usrreqs = {
1112 .pru_abort = NULL,
1113 .pru_attach = ngd_attach,
1114 .pru_bind = NULL,
1115 .pru_connect = ngd_connect,
1116 .pru_detach = ngd_detach,
1117 .pru_disconnect = dummy_disconnect,
1118 .pru_peeraddr = NULL,
1119 .pru_send = ngd_send,
1120 .pru_shutdown = NULL,
1121 .pru_sockaddr = ng_setsockaddr,
1122 };
1123
1124 /*
1125 * Definitions of protocols supported in the NETGRAPH domain.
1126 */
1127
1128 extern struct domain ngdomain; /* stop compiler warnings */
1129
1130 static struct protosw ngsw[] = {
1131 {
1132 .pr_type = SOCK_DGRAM,
1133 .pr_domain = &ngdomain,
1134 .pr_protocol = NG_CONTROL,
1135 .pr_flags = PR_ATOMIC | PR_ADDR /* | PR_RIGHTS */,
1136 .pr_usrreqs = &ngc_usrreqs
1137 },
1138 {
1139 .pr_type = SOCK_DGRAM,
1140 .pr_domain = &ngdomain,
1141 .pr_protocol = NG_DATA,
1142 .pr_flags = PR_ATOMIC | PR_ADDR,
1143 .pr_usrreqs = &ngd_usrreqs
1144 }
1145 };
1146
1147 struct domain ngdomain = {
1148 .dom_family = AF_NETGRAPH,
1149 .dom_name = "netgraph",
1150 .dom_protosw = ngsw,
1151 .dom_protoswNPROTOSW = &ngsw[sizeof(ngsw) / sizeof(ngsw[0])]
1152 };
1153
1154 /*
1155 * Handle loading and unloading for this node type
1156 * This is to handle auxiliary linkages (e.g protocol domain addition).
1157 */
1158 static int
1159 ngs_mod_event(module_t mod, int event, void *data)
1160 {
1161 int error = 0;
1162
1163 switch (event) {
1164 case MOD_LOAD:
1165 mtx_init(&ngsocketlist_mtx, "ng_socketlist", NULL, MTX_DEF);
1166 /* Register protocol domain */
1167 net_add_domain(&ngdomain);
1168 break;
1169 case MOD_UNLOAD:
1170 /* Insure there are no open netgraph sockets */
1171 if (!LIST_EMPTY(&ngsocklist)) {
1172 error = EBUSY;
1173 break;
1174 }
1175
1176 #ifdef NOTYET
1177 if ((LIST_EMPTY(&ngsocklist)) && (typestruct.refs == 0)) {
1178 /* Unregister protocol domain XXX can't do this yet.. */
1179 if ((error = net_rm_domain(&ngdomain)) != 0)
1180 break;
1181 mtx_destroy(&ngsocketlist_mtx);
1182 } else
1183 #endif
1184 error = EBUSY;
1185 break;
1186 default:
1187 error = EOPNOTSUPP;
1188 break;
1189 }
1190 return (error);
1191 }
1192
1193 SYSCTL_INT(_net_graph, OID_AUTO, family, CTLFLAG_RD, 0, AF_NETGRAPH, "");
1194 SYSCTL_NODE(_net_graph, OID_AUTO, data, CTLFLAG_RW, 0, "DATA");
1195 SYSCTL_INT(_net_graph_data, OID_AUTO, proto, CTLFLAG_RD, 0, NG_DATA, "");
1196 SYSCTL_NODE(_net_graph, OID_AUTO, control, CTLFLAG_RW, 0, "CONTROL");
1197 SYSCTL_INT(_net_graph_control, OID_AUTO, proto, CTLFLAG_RD, 0, NG_CONTROL, "");
1198
Cache object: 5982a676dbcd10dda4262a50262c4068
|