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