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