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