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 <[email protected]>
39 *
40 * $FreeBSD$
41 * $Whistle: ng_socket.c,v 1.28 1999/11/01 09:24:52 julian Exp $
42 */
43
44 /*
45 * Netgraph socket nodes
46 *
47 * There are two types of netgraph sockets, control and data.
48 * Control sockets have a netgraph node, but data sockets are
49 * parasitic on control sockets, and have no node of their own.
50 */
51
52 #include <sys/param.h>
53 #include <sys/domain.h>
54 #include <sys/hash.h>
55 #include <sys/kernel.h>
56 #include <sys/linker.h>
57 #include <sys/lock.h>
58 #include <sys/malloc.h>
59 #include <sys/mbuf.h>
60 #include <sys/mutex.h>
61 #include <sys/proc.h>
62 #include <sys/epoch.h>
63 #include <sys/priv.h>
64 #include <sys/protosw.h>
65 #include <sys/queue.h>
66 #include <sys/socket.h>
67 #include <sys/socketvar.h>
68 #include <sys/syscallsubr.h>
69 #include <sys/sysctl.h>
70
71 #include <net/vnet.h>
72
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 static MALLOC_DEFINE(M_NETGRAPH_PATH, "netgraph_path", "netgraph path info");
80 static 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_findhook_t ngs_findhook;
119 static ng_rcvdata_t ngs_rcvdata;
120 static ng_disconnect_t ngs_disconnect;
121
122 /* Internal methods */
123 static int ng_attach_data(struct socket *so);
124 static int ng_attach_cntl(struct socket *so);
125 static int ng_attach_common(struct socket *so, int type);
126 static void ng_detach_common(struct ngpcb *pcbp, int type);
127 static void ng_socket_free_priv(struct ngsock *priv);
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 void ng_socket_item_applied(void *context, int error);
133
134 /* Netgraph type descriptor */
135 static struct ng_type typestruct = {
136 .version = NG_ABI_VERSION,
137 .name = NG_SOCKET_NODE_TYPE,
138 .mod_event = ngs_mod_event,
139 .constructor = ngs_constructor,
140 .rcvmsg = ngs_rcvmsg,
141 .shutdown = ngs_shutdown,
142 .newhook = ngs_newhook,
143 .connect = ngs_connect,
144 .findhook = ngs_findhook,
145 .rcvdata = ngs_rcvdata,
146 .disconnect = ngs_disconnect,
147 };
148 NETGRAPH_INIT_ORDERED(socket, &typestruct, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
149
150 /* Buffer space */
151 static u_long ngpdg_sendspace = 20 * 1024; /* really max datagram size */
152 SYSCTL_ULONG(_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_ULONG(_net_graph, OID_AUTO, recvspace, CTLFLAG_RW,
156 &ngpdg_recvspace , 0, "Maximum space for incoming Netgraph datagrams");
157
158 /* List of all sockets (for netstat -f netgraph) */
159 static LIST_HEAD(, ngpcb) ngsocklist;
160
161 static struct mtx ngsocketlist_mtx;
162
163 #define sotongpcb(so) ((struct ngpcb *)(so)->so_pcb)
164
165 /* If getting unexplained errors returned, set this to "kdb_enter("X"); */
166 #ifndef TRAP_ERROR
167 #define TRAP_ERROR
168 #endif
169
170 struct hookpriv {
171 LIST_ENTRY(hookpriv) next;
172 hook_p hook;
173 };
174 LIST_HEAD(ngshash, hookpriv);
175
176 /* Per-node private data */
177 struct ngsock {
178 struct ng_node *node; /* the associated netgraph node */
179 struct ngpcb *datasock; /* optional data socket */
180 struct ngpcb *ctlsock; /* optional control socket */
181 struct ngshash *hash; /* hash for hook names */
182 u_long hmask; /* hash mask */
183 int flags;
184 int refs;
185 struct mtx mtx; /* mtx to wait on */
186 int error; /* place to store error */
187 };
188
189 #define NGS_FLAG_NOLINGER 1 /* close with last hook */
190
191 /***************************************************************
192 Control sockets
193 ***************************************************************/
194
195 static int
ngc_attach(struct socket * so,int proto,struct thread * td)196 ngc_attach(struct socket *so, int proto, struct thread *td)
197 {
198 struct ngpcb *const pcbp = sotongpcb(so);
199 int error;
200
201 error = priv_check(td, PRIV_NETGRAPH_CONTROL);
202 if (error)
203 return (error);
204 if (pcbp != NULL)
205 return (EISCONN);
206 return (ng_attach_cntl(so));
207 }
208
209 static void
ngc_detach(struct socket * so)210 ngc_detach(struct socket *so)
211 {
212 struct ngpcb *const pcbp = sotongpcb(so);
213
214 KASSERT(pcbp != NULL, ("ngc_detach: pcbp == NULL"));
215 ng_detach_common(pcbp, NG_CONTROL);
216 }
217
218 static int
ngc_send(struct socket * so,int flags,struct mbuf * m,struct sockaddr * addr,struct mbuf * control,struct thread * td)219 ngc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
220 struct mbuf *control, struct thread *td)
221 {
222 struct ngpcb *const pcbp = sotongpcb(so);
223 struct ngsock *const priv = NG_NODE_PRIVATE(pcbp->sockdata->node);
224 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
225 struct ng_mesg *msg;
226 struct mbuf *m0;
227 item_p item;
228 char *path = NULL;
229 int len, error = 0;
230 struct ng_apply_info apply;
231
232 if (control) {
233 error = EINVAL;
234 goto release;
235 }
236
237 /* Require destination as there may be >= 1 hooks on this node. */
238 if (addr == NULL) {
239 error = EDESTADDRREQ;
240 goto release;
241 }
242
243 /*
244 * Allocate an expendable buffer for the path, chop off
245 * the sockaddr header, and make sure it's NUL terminated.
246 */
247 len = sap->sg_len - 2;
248 path = malloc(len + 1, M_NETGRAPH_PATH, M_WAITOK);
249 bcopy(sap->sg_data, path, len);
250 path[len] = '\0';
251
252 /*
253 * Move the actual message out of mbufs into a linear buffer.
254 * Start by adding up the size of the data. (could use mh_len?)
255 */
256 for (len = 0, m0 = m; m0 != NULL; m0 = m0->m_next)
257 len += m0->m_len;
258
259 /*
260 * Move the data into a linear buffer as well.
261 * Messages are not delivered in mbufs.
262 */
263 msg = malloc(len + 1, M_NETGRAPH_MSG, M_WAITOK);
264 m_copydata(m, 0, len, (char *)msg);
265
266 if (msg->header.version != NG_VERSION) {
267 free(msg, M_NETGRAPH_MSG);
268 error = EINVAL;
269 goto release;
270 }
271
272 /*
273 * Hack alert!
274 * We look into the message and if it mkpeers a node of unknown type, we
275 * try to load it. We need to do this now, in syscall thread, because if
276 * message gets queued and applied later we will get panic.
277 */
278 if (msg->header.typecookie == NGM_GENERIC_COOKIE &&
279 msg->header.cmd == NGM_MKPEER) {
280 struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) msg->data;
281
282 if (ng_findtype(mkp->type) == NULL) {
283 #ifndef FSTACK
284 char filename[NG_TYPESIZ + 3];
285 int fileid;
286
287 /* Not found, try to load it as a loadable module. */
288 snprintf(filename, sizeof(filename), "ng_%s",
289 mkp->type);
290 error = kern_kldload(curthread, filename, &fileid);
291 if (error != 0) {
292 free(msg, M_NETGRAPH_MSG);
293 goto release;
294 }
295
296 /* See if type has been loaded successfully. */
297 if (ng_findtype(mkp->type) == NULL) {
298 free(msg, M_NETGRAPH_MSG);
299 (void)kern_kldunload(curthread, fileid,
300 LINKER_UNLOAD_NORMAL);
301 error = ENXIO;
302 goto release;
303 }
304 #else
305 error = ENOENT;
306 goto release;
307 #endif
308 }
309 }
310
311 item = ng_package_msg(msg, NG_WAITOK);
312 if ((error = ng_address_path((pcbp->sockdata->node), item, path, 0))
313 != 0) {
314 #ifdef TRACE_MESSAGES
315 printf("ng_address_path: errx=%d\n", error);
316 #endif
317 goto release;
318 }
319
320 #ifdef TRACE_MESSAGES
321 printf("[%x]:<---------[socket]: c=<%d>cmd=%x(%s) f=%x #%d (%s)\n",
322 item->el_dest->nd_ID,
323 msg->header.typecookie,
324 msg->header.cmd,
325 msg->header.cmdstr,
326 msg->header.flags,
327 msg->header.token,
328 item->el_dest->nd_type->name);
329 #endif
330 SAVE_LINE(item);
331 /*
332 * We do not want to return from syscall until the item
333 * is processed by destination node. We register callback
334 * on the item, which will update priv->error when item
335 * was applied.
336 * If ng_snd_item() has queued item, we sleep until
337 * callback wakes us up.
338 */
339 bzero(&apply, sizeof(apply));
340 apply.apply = ng_socket_item_applied;
341 apply.context = priv;
342 item->apply = &apply;
343 priv->error = -1;
344
345 error = ng_snd_item(item, 0);
346
347 mtx_lock(&priv->mtx);
348 if (priv->error == -1)
349 msleep(priv, &priv->mtx, 0, "ngsock", 0);
350 mtx_unlock(&priv->mtx);
351 KASSERT(priv->error != -1,
352 ("ng_socket: priv->error wasn't updated"));
353 error = priv->error;
354
355 release:
356 if (path != NULL)
357 free(path, M_NETGRAPH_PATH);
358 if (control != NULL)
359 m_freem(control);
360 if (m != NULL)
361 m_freem(m);
362 return (error);
363 }
364
365 static int
ngc_bind(struct socket * so,struct sockaddr * nam,struct thread * td)366 ngc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
367 {
368 struct ngpcb *const pcbp = sotongpcb(so);
369
370 if (pcbp == NULL)
371 return (EINVAL);
372 return (ng_bind(nam, pcbp));
373 }
374
375 static int
ngc_connect(struct socket * so,struct sockaddr * nam,struct thread * td)376 ngc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
377 {
378 /*
379 * At this time refuse to do this.. it used to
380 * do something but it was undocumented and not used.
381 */
382 printf("program tried to connect control socket to remote node\n");
383 return (EINVAL);
384 }
385
386 /***************************************************************
387 Data sockets
388 ***************************************************************/
389
390 static int
ngd_attach(struct socket * so,int proto,struct thread * td)391 ngd_attach(struct socket *so, int proto, struct thread *td)
392 {
393 struct ngpcb *const pcbp = sotongpcb(so);
394
395 if (pcbp != NULL)
396 return (EISCONN);
397 return (ng_attach_data(so));
398 }
399
400 static void
ngd_detach(struct socket * so)401 ngd_detach(struct socket *so)
402 {
403 struct ngpcb *const pcbp = sotongpcb(so);
404
405 KASSERT(pcbp != NULL, ("ngd_detach: pcbp == NULL"));
406 ng_detach_common(pcbp, NG_DATA);
407 }
408
409 static int
ngd_send(struct socket * so,int flags,struct mbuf * m,struct sockaddr * addr,struct mbuf * control,struct thread * td)410 ngd_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
411 struct mbuf *control, struct thread *td)
412 {
413 struct epoch_tracker et;
414 struct ngpcb *const pcbp = sotongpcb(so);
415 struct sockaddr_ng *const sap = (struct sockaddr_ng *) addr;
416 int len, error;
417 hook_p hook = NULL;
418 item_p item;
419 char hookname[NG_HOOKSIZ];
420
421 if ((pcbp == NULL) || (control != NULL)) {
422 error = EINVAL;
423 goto release;
424 }
425 if (pcbp->sockdata == NULL) {
426 error = ENOTCONN;
427 goto release;
428 }
429
430 if (sap == NULL)
431 len = 0; /* Make compiler happy. */
432 else
433 len = sap->sg_len - 2;
434
435 /*
436 * If the user used any of these ways to not specify an address
437 * then handle specially.
438 */
439 if ((sap == NULL) || (len <= 0) || (*sap->sg_data == '\0')) {
440 if (NG_NODE_NUMHOOKS(pcbp->sockdata->node) != 1) {
441 error = EDESTADDRREQ;
442 goto release;
443 }
444 /*
445 * If exactly one hook exists, just use it.
446 * Special case to allow write(2) to work on an ng_socket.
447 */
448 hook = LIST_FIRST(&pcbp->sockdata->node->nd_hooks);
449 } else {
450 if (len >= NG_HOOKSIZ) {
451 error = EINVAL;
452 goto release;
453 }
454
455 /*
456 * chop off the sockaddr header, and make sure it's NUL
457 * terminated
458 */
459 bcopy(sap->sg_data, hookname, len);
460 hookname[len] = '\0';
461
462 /* Find the correct hook from 'hookname' */
463 hook = ng_findhook(pcbp->sockdata->node, hookname);
464 if (hook == NULL) {
465 error = EHOSTUNREACH;
466 goto release;
467 }
468 }
469
470 /* Send data. */
471 item = ng_package_data(m, NG_WAITOK);
472 m = NULL;
473 NET_EPOCH_ENTER(et);
474 NG_FWD_ITEM_HOOK(error, item, hook);
475 NET_EPOCH_EXIT(et);
476
477 release:
478 if (control != NULL)
479 m_freem(control);
480 if (m != NULL)
481 m_freem(m);
482 return (error);
483 }
484
485 static int
ngd_connect(struct socket * so,struct sockaddr * nam,struct thread * td)486 ngd_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
487 {
488 struct ngpcb *const pcbp = sotongpcb(so);
489
490 if (pcbp == NULL)
491 return (EINVAL);
492 return (ng_connect_data(nam, pcbp));
493 }
494
495 /*
496 * Used for both data and control sockets
497 */
498 static int
ng_getsockaddr(struct socket * so,struct sockaddr ** addr)499 ng_getsockaddr(struct socket *so, struct sockaddr **addr)
500 {
501 struct ngpcb *pcbp;
502 struct sockaddr_ng *sg;
503 int sg_len;
504 int error = 0;
505
506 pcbp = sotongpcb(so);
507 if ((pcbp == NULL) || (pcbp->sockdata == NULL))
508 /* XXXGL: can this still happen? */
509 return (EINVAL);
510
511 sg_len = sizeof(struct sockaddr_ng) + NG_NODESIZ -
512 sizeof(sg->sg_data);
513 sg = malloc(sg_len, M_SONAME, M_WAITOK | M_ZERO);
514
515 mtx_lock(&pcbp->sockdata->mtx);
516 if (pcbp->sockdata->node != NULL) {
517 node_p node = pcbp->sockdata->node;
518
519 if (NG_NODE_HAS_NAME(node))
520 bcopy(NG_NODE_NAME(node), sg->sg_data,
521 strlen(NG_NODE_NAME(node)));
522 mtx_unlock(&pcbp->sockdata->mtx);
523
524 sg->sg_len = sg_len;
525 sg->sg_family = AF_NETGRAPH;
526 *addr = (struct sockaddr *)sg;
527 } else {
528 mtx_unlock(&pcbp->sockdata->mtx);
529 free(sg, M_SONAME);
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
ng_attach_cntl(struct socket * so)543 ng_attach_cntl(struct socket *so)
544 {
545 struct ngsock *priv;
546 struct ngpcb *pcbp;
547 node_p node;
548 int error;
549
550 /* Setup protocol control block */
551 if ((error = ng_attach_common(so, NG_CONTROL)) != 0)
552 return (error);
553 pcbp = sotongpcb(so);
554
555 /* Make the generic node components */
556 if ((error = ng_make_node_common(&typestruct, &node)) != 0) {
557 ng_detach_common(pcbp, NG_CONTROL);
558 return (error);
559 }
560
561 /*
562 * Allocate node private info and hash. We start
563 * with 16 hash entries, however we may grow later
564 * in ngs_newhook(). We can't predict how much hooks
565 * does this node plan to have.
566 */
567 priv = malloc(sizeof(*priv), M_NETGRAPH_SOCK, M_WAITOK | M_ZERO);
568 priv->hash = hashinit(16, M_NETGRAPH_SOCK, &priv->hmask);
569
570 /* Initialize mutex. */
571 mtx_init(&priv->mtx, "ng_socket", NULL, MTX_DEF);
572
573 /* Link the pcb the private data. */
574 priv->ctlsock = pcbp;
575 pcbp->sockdata = priv;
576 priv->refs++;
577 priv->node = node;
578 pcbp->node_id = node->nd_ID; /* hint for netstat(1) */
579
580 /* Link the node and the private data. */
581 NG_NODE_SET_PRIVATE(priv->node, priv);
582 NG_NODE_REF(priv->node);
583 priv->refs++;
584
585 return (0);
586 }
587
588 static int
ng_attach_data(struct socket * so)589 ng_attach_data(struct socket *so)
590 {
591 return (ng_attach_common(so, NG_DATA));
592 }
593
594 /*
595 * Set up a socket protocol control block.
596 * This code is shared between control and data sockets.
597 */
598 static int
ng_attach_common(struct socket * so,int type)599 ng_attach_common(struct socket *so, int type)
600 {
601 struct ngpcb *pcbp;
602 int error;
603
604 /* Standard socket setup stuff. */
605 error = soreserve(so, ngpdg_sendspace, ngpdg_recvspace);
606 if (error)
607 return (error);
608
609 /* Allocate the pcb. */
610 pcbp = malloc(sizeof(struct ngpcb), M_PCB, M_WAITOK | M_ZERO);
611 pcbp->type = type;
612
613 /* Link the pcb and the socket. */
614 so->so_pcb = (caddr_t)pcbp;
615 pcbp->ng_socket = so;
616
617 /* Add the socket to linked list */
618 mtx_lock(&ngsocketlist_mtx);
619 LIST_INSERT_HEAD(&ngsocklist, pcbp, socks);
620 mtx_unlock(&ngsocketlist_mtx);
621 return (0);
622 }
623
624 /*
625 * Disassociate the socket from it's protocol specific
626 * partner. If it's attached to a node's private data structure,
627 * then unlink from that too. If we were the last socket attached to it,
628 * then shut down the entire node. Shared code for control and data sockets.
629 */
630 static void
ng_detach_common(struct ngpcb * pcbp,int which)631 ng_detach_common(struct ngpcb *pcbp, int which)
632 {
633 struct ngsock *priv = pcbp->sockdata;
634
635 if (priv != NULL) {
636 mtx_lock(&priv->mtx);
637
638 switch (which) {
639 case NG_CONTROL:
640 priv->ctlsock = NULL;
641 break;
642 case NG_DATA:
643 priv->datasock = NULL;
644 break;
645 default:
646 panic("%s", __func__);
647 }
648 pcbp->sockdata = NULL;
649 pcbp->node_id = 0;
650
651 ng_socket_free_priv(priv);
652 }
653
654 pcbp->ng_socket->so_pcb = NULL;
655 mtx_lock(&ngsocketlist_mtx);
656 LIST_REMOVE(pcbp, socks);
657 mtx_unlock(&ngsocketlist_mtx);
658 free(pcbp, M_PCB);
659 }
660
661 /*
662 * Remove a reference from node private data.
663 */
664 static void
ng_socket_free_priv(struct ngsock * priv)665 ng_socket_free_priv(struct ngsock *priv)
666 {
667 mtx_assert(&priv->mtx, MA_OWNED);
668
669 priv->refs--;
670
671 if (priv->refs == 0) {
672 mtx_destroy(&priv->mtx);
673 hashdestroy(priv->hash, M_NETGRAPH_SOCK, priv->hmask);
674 free(priv, M_NETGRAPH_SOCK);
675 return;
676 }
677
678 if ((priv->refs == 1) && (priv->node != NULL)) {
679 node_p node = priv->node;
680
681 priv->node = NULL;
682 mtx_unlock(&priv->mtx);
683 NG_NODE_UNREF(node);
684 ng_rmnode_self(node);
685 } else
686 mtx_unlock(&priv->mtx);
687 }
688
689 /*
690 * Connect the data socket to a named control socket node.
691 */
692 static int
ng_connect_data(struct sockaddr * nam,struct ngpcb * pcbp)693 ng_connect_data(struct sockaddr *nam, struct ngpcb *pcbp)
694 {
695 struct sockaddr_ng *sap;
696 node_p farnode;
697 struct ngsock *priv;
698 int error;
699 item_p item;
700
701 /* If we are already connected, don't do it again. */
702 if (pcbp->sockdata != NULL)
703 return (EISCONN);
704
705 /*
706 * Find the target (victim) and check it doesn't already have
707 * a data socket. Also check it is a 'socket' type node.
708 * Use ng_package_data() and ng_address_path() to do this.
709 */
710
711 sap = (struct sockaddr_ng *) nam;
712 /* The item will hold the node reference. */
713 item = ng_package_data(NULL, NG_WAITOK);
714
715 if ((error = ng_address_path(NULL, item, sap->sg_data, 0)))
716 return (error); /* item is freed on failure */
717
718 /*
719 * Extract node from item and free item. Remember we now have
720 * a reference on the node. The item holds it for us.
721 * when we free the item we release the reference.
722 */
723 farnode = item->el_dest; /* shortcut */
724 if (strcmp(farnode->nd_type->name, NG_SOCKET_NODE_TYPE) != 0) {
725 NG_FREE_ITEM(item); /* drop the reference to the node */
726 return (EINVAL);
727 }
728 priv = NG_NODE_PRIVATE(farnode);
729 if (priv->datasock != NULL) {
730 NG_FREE_ITEM(item); /* drop the reference to the node */
731 return (EADDRINUSE);
732 }
733
734 /*
735 * Link the PCB and the private data struct. and note the extra
736 * reference. Drop the extra reference on the node.
737 */
738 mtx_lock(&priv->mtx);
739 priv->datasock = pcbp;
740 pcbp->sockdata = priv;
741 pcbp->node_id = priv->node->nd_ID; /* hint for netstat(1) */
742 priv->refs++;
743 mtx_unlock(&priv->mtx);
744 NG_FREE_ITEM(item); /* drop the reference to the node */
745 return (0);
746 }
747
748 /*
749 * Binding a socket means giving the corresponding node a name
750 */
751 static int
ng_bind(struct sockaddr * nam,struct ngpcb * pcbp)752 ng_bind(struct sockaddr *nam, struct ngpcb *pcbp)
753 {
754 struct ngsock *const priv = pcbp->sockdata;
755 struct sockaddr_ng *const sap = (struct sockaddr_ng *) nam;
756
757 if (priv == NULL) {
758 TRAP_ERROR;
759 return (EINVAL);
760 }
761 if ((sap->sg_len < 4) || (sap->sg_len > (NG_NODESIZ + 2)) ||
762 (sap->sg_data[0] == '\0') ||
763 (sap->sg_data[sap->sg_len - 3] != '\0')) {
764 TRAP_ERROR;
765 return (EINVAL);
766 }
767 return (ng_name_node(priv->node, sap->sg_data));
768 }
769
770 /***************************************************************
771 Netgraph node
772 ***************************************************************/
773
774 /*
775 * You can only create new nodes from the socket end of things.
776 */
777 static int
ngs_constructor(node_p nodep)778 ngs_constructor(node_p nodep)
779 {
780 return (EINVAL);
781 }
782
783 static void
ngs_rehash(node_p node)784 ngs_rehash(node_p node)
785 {
786 struct ngsock *priv = NG_NODE_PRIVATE(node);
787 struct ngshash *new;
788 struct hookpriv *hp;
789 hook_p hook;
790 uint32_t h;
791 u_long hmask;
792
793 new = hashinit_flags((priv->hmask + 1) * 2, M_NETGRAPH_SOCK, &hmask,
794 HASH_NOWAIT);
795 if (new == NULL)
796 return;
797
798 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
799 hp = NG_HOOK_PRIVATE(hook);
800 #ifdef INVARIANTS
801 LIST_REMOVE(hp, next);
802 #endif
803 h = hash32_str(NG_HOOK_NAME(hook), HASHINIT) & hmask;
804 LIST_INSERT_HEAD(&new[h], hp, next);
805 }
806
807 hashdestroy(priv->hash, M_NETGRAPH_SOCK, priv->hmask);
808 priv->hash = new;
809 priv->hmask = hmask;
810 }
811
812 /*
813 * We allow any hook to be connected to the node.
814 * There is no per-hook private information though.
815 */
816 static int
ngs_newhook(node_p node,hook_p hook,const char * name)817 ngs_newhook(node_p node, hook_p hook, const char *name)
818 {
819 struct ngsock *const priv = NG_NODE_PRIVATE(node);
820 struct hookpriv *hp;
821 uint32_t h;
822
823 hp = malloc(sizeof(*hp), M_NETGRAPH_SOCK, M_NOWAIT);
824 if (hp == NULL)
825 return (ENOMEM);
826 if (node->nd_numhooks * 2 > priv->hmask)
827 ngs_rehash(node);
828 hp->hook = hook;
829 h = hash32_str(name, HASHINIT) & priv->hmask;
830 LIST_INSERT_HEAD(&priv->hash[h], hp, next);
831 NG_HOOK_SET_PRIVATE(hook, hp);
832
833 return (0);
834 }
835
836 /*
837 * If only one hook, allow read(2) and write(2) to work.
838 */
839 static int
ngs_connect(hook_p hook)840 ngs_connect(hook_p hook)
841 {
842 node_p node = NG_HOOK_NODE(hook);
843 struct ngsock *priv = NG_NODE_PRIVATE(node);
844
845 if ((priv->datasock) && (priv->datasock->ng_socket)) {
846 if (NG_NODE_NUMHOOKS(node) == 1)
847 priv->datasock->ng_socket->so_state |= SS_ISCONNECTED;
848 else
849 priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED;
850 }
851 return (0);
852 }
853
854 /* Look up hook by name */
855 static hook_p
ngs_findhook(node_p node,const char * name)856 ngs_findhook(node_p node, const char *name)
857 {
858 struct ngsock *priv = NG_NODE_PRIVATE(node);
859 struct hookpriv *hp;
860 uint32_t h;
861
862 /*
863 * Microoptimisation for an ng_socket with
864 * a single hook, which is a common case.
865 */
866 if (node->nd_numhooks == 1) {
867 hook_p hook;
868
869 hook = LIST_FIRST(&node->nd_hooks);
870
871 if (strcmp(NG_HOOK_NAME(hook), name) == 0)
872 return (hook);
873 else
874 return (NULL);
875 }
876
877 h = hash32_str(name, HASHINIT) & priv->hmask;
878
879 LIST_FOREACH(hp, &priv->hash[h], next)
880 if (strcmp(NG_HOOK_NAME(hp->hook), name) == 0)
881 return (hp->hook);
882
883 return (NULL);
884 }
885
886 /*
887 * Incoming messages get passed up to the control socket.
888 * Unless they are for us specifically (socket_type)
889 */
890 static int
ngs_rcvmsg(node_p node,item_p item,hook_p lasthook)891 ngs_rcvmsg(node_p node, item_p item, hook_p lasthook)
892 {
893 struct ngsock *const priv = NG_NODE_PRIVATE(node);
894 struct ngpcb *pcbp;
895 struct socket *so;
896 struct sockaddr_ng addr;
897 struct ng_mesg *msg;
898 struct mbuf *m;
899 ng_ID_t retaddr = NGI_RETADDR(item);
900 int addrlen;
901 int error = 0;
902
903 NGI_GET_MSG(item, msg);
904 NG_FREE_ITEM(item);
905
906 /*
907 * Grab priv->mtx here to prevent destroying of control socket
908 * after checking that priv->ctlsock is not NULL.
909 */
910 mtx_lock(&priv->mtx);
911 pcbp = priv->ctlsock;
912
913 /*
914 * Only allow mesgs to be passed if we have the control socket.
915 * Data sockets can only support the generic messages.
916 */
917 if (pcbp == NULL) {
918 mtx_unlock(&priv->mtx);
919 TRAP_ERROR;
920 NG_FREE_MSG(msg);
921 return (EINVAL);
922 }
923 so = pcbp->ng_socket;
924 SOCKBUF_LOCK(&so->so_rcv);
925
926 /* As long as the race is handled, priv->mtx may be unlocked now. */
927 mtx_unlock(&priv->mtx);
928
929 #ifdef TRACE_MESSAGES
930 printf("[%x]:---------->[socket]: c=<%d>cmd=%x(%s) f=%x #%d\n",
931 retaddr,
932 msg->header.typecookie,
933 msg->header.cmd,
934 msg->header.cmdstr,
935 msg->header.flags,
936 msg->header.token);
937 #endif
938
939 if (msg->header.typecookie == NGM_SOCKET_COOKIE) {
940 switch (msg->header.cmd) {
941 case NGM_SOCK_CMD_NOLINGER:
942 priv->flags |= NGS_FLAG_NOLINGER;
943 break;
944 case NGM_SOCK_CMD_LINGER:
945 priv->flags &= ~NGS_FLAG_NOLINGER;
946 break;
947 default:
948 error = EINVAL; /* unknown command */
949 }
950 SOCKBUF_UNLOCK(&so->so_rcv);
951
952 /* Free the message and return. */
953 NG_FREE_MSG(msg);
954 return (error);
955 }
956
957 /* Get the return address into a sockaddr. */
958 bzero(&addr, sizeof(addr));
959 addr.sg_len = sizeof(addr);
960 addr.sg_family = AF_NETGRAPH;
961 addrlen = snprintf((char *)&addr.sg_data, sizeof(addr.sg_data),
962 "[%x]:", retaddr);
963 if (addrlen < 0 || addrlen > sizeof(addr.sg_data)) {
964 SOCKBUF_UNLOCK(&so->so_rcv);
965 printf("%s: snprintf([%x]) failed - %d\n", __func__, retaddr,
966 addrlen);
967 NG_FREE_MSG(msg);
968 return (EINVAL);
969 }
970
971 /* Copy the message itself into an mbuf chain. */
972 m = m_devget((caddr_t)msg, sizeof(struct ng_mesg) + msg->header.arglen,
973 0, NULL, NULL);
974
975 /*
976 * Here we free the message. We need to do that
977 * regardless of whether we got mbufs.
978 */
979 NG_FREE_MSG(msg);
980
981 if (m == NULL) {
982 SOCKBUF_UNLOCK(&so->so_rcv);
983 TRAP_ERROR;
984 return (ENOBUFS);
985 }
986
987 /* Send it up to the socket. */
988 if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)&addr, m,
989 NULL) == 0) {
990 SOCKBUF_UNLOCK(&so->so_rcv);
991 TRAP_ERROR;
992 m_freem(m);
993 return (ENOBUFS);
994 }
995
996 /* sorwakeup_locked () releases the lock internally. */
997 sorwakeup_locked(so);
998
999 return (error);
1000 }
1001
1002 /*
1003 * Receive data on a hook
1004 */
1005 static int
ngs_rcvdata(hook_p hook,item_p item)1006 ngs_rcvdata(hook_p hook, item_p item)
1007 {
1008 struct ngsock *const priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
1009 struct ngpcb *const pcbp = priv->datasock;
1010 struct socket *so;
1011 struct sockaddr_ng *addr;
1012 char *addrbuf[NG_HOOKSIZ + 4];
1013 int addrlen;
1014 struct mbuf *m;
1015
1016 NGI_GET_M(item, m);
1017 NG_FREE_ITEM(item);
1018
1019 /* If there is no data socket, black-hole it. */
1020 if (pcbp == NULL) {
1021 NG_FREE_M(m);
1022 return (0);
1023 }
1024 so = pcbp->ng_socket;
1025
1026 /* Get the return address into a sockaddr. */
1027 addrlen = strlen(NG_HOOK_NAME(hook)); /* <= NG_HOOKSIZ - 1 */
1028 addr = (struct sockaddr_ng *) addrbuf;
1029 addr->sg_len = addrlen + 3;
1030 addr->sg_family = AF_NETGRAPH;
1031 bcopy(NG_HOOK_NAME(hook), addr->sg_data, addrlen);
1032 addr->sg_data[addrlen] = '\0';
1033
1034 /* Try to tell the socket which hook it came in on. */
1035 SOCKBUF_LOCK(&so->so_rcv);
1036 if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)addr, m,
1037 NULL) == 0) {
1038 SOCKBUF_UNLOCK(&so->so_rcv);
1039 m_freem(m);
1040 TRAP_ERROR;
1041 return (ENOBUFS);
1042 }
1043
1044 /* sorwakeup_locked () releases the lock internally. */
1045 sorwakeup_locked(so);
1046 return (0);
1047 }
1048
1049 /*
1050 * Hook disconnection
1051 *
1052 * For this type, removal of the last link destroys the node
1053 * if the NOLINGER flag is set.
1054 */
1055 static int
ngs_disconnect(hook_p hook)1056 ngs_disconnect(hook_p hook)
1057 {
1058 node_p node = NG_HOOK_NODE(hook);
1059 struct ngsock *const priv = NG_NODE_PRIVATE(node);
1060 struct hookpriv *hp = NG_HOOK_PRIVATE(hook);
1061
1062 LIST_REMOVE(hp, next);
1063 free(hp, M_NETGRAPH_SOCK);
1064
1065 if ((priv->datasock) && (priv->datasock->ng_socket)) {
1066 if (NG_NODE_NUMHOOKS(node) == 1)
1067 priv->datasock->ng_socket->so_state |= SS_ISCONNECTED;
1068 else
1069 priv->datasock->ng_socket->so_state &= ~SS_ISCONNECTED;
1070 }
1071
1072 if ((priv->flags & NGS_FLAG_NOLINGER) &&
1073 (NG_NODE_NUMHOOKS(node) == 0) && (NG_NODE_IS_VALID(node)))
1074 ng_rmnode_self(node);
1075
1076 return (0);
1077 }
1078
1079 /*
1080 * Do local shutdown processing.
1081 * In this case, that involves making sure the socket
1082 * knows we should be shutting down.
1083 */
1084 static int
ngs_shutdown(node_p node)1085 ngs_shutdown(node_p node)
1086 {
1087 struct ngsock *const priv = NG_NODE_PRIVATE(node);
1088 struct ngpcb *dpcbp, *pcbp;
1089
1090 mtx_lock(&priv->mtx);
1091 dpcbp = priv->datasock;
1092 pcbp = priv->ctlsock;
1093
1094 if (dpcbp != NULL)
1095 soisdisconnected(dpcbp->ng_socket);
1096
1097 if (pcbp != NULL)
1098 soisdisconnected(pcbp->ng_socket);
1099
1100 priv->node = NULL;
1101 NG_NODE_SET_PRIVATE(node, NULL);
1102 ng_socket_free_priv(priv);
1103
1104 NG_NODE_UNREF(node);
1105 return (0);
1106 }
1107
1108 static void
ng_socket_item_applied(void * context,int error)1109 ng_socket_item_applied(void *context, int error)
1110 {
1111 struct ngsock *const priv = (struct ngsock *)context;
1112
1113 mtx_lock(&priv->mtx);
1114 priv->error = error;
1115 wakeup(priv);
1116 mtx_unlock(&priv->mtx);
1117
1118 }
1119
1120 static int
dummy_disconnect(struct socket * so)1121 dummy_disconnect(struct socket *so)
1122 {
1123 return (0);
1124 }
1125 /*
1126 * Control and data socket type descriptors
1127 *
1128 * XXXRW: Perhaps _close should do something?
1129 */
1130
1131 static struct pr_usrreqs ngc_usrreqs = {
1132 .pru_abort = NULL,
1133 .pru_attach = ngc_attach,
1134 .pru_bind = ngc_bind,
1135 .pru_connect = ngc_connect,
1136 .pru_detach = ngc_detach,
1137 .pru_disconnect = dummy_disconnect,
1138 .pru_peeraddr = NULL,
1139 .pru_send = ngc_send,
1140 .pru_shutdown = NULL,
1141 .pru_sockaddr = ng_getsockaddr,
1142 .pru_close = NULL,
1143 };
1144
1145 static struct pr_usrreqs ngd_usrreqs = {
1146 .pru_abort = NULL,
1147 .pru_attach = ngd_attach,
1148 .pru_bind = NULL,
1149 .pru_connect = ngd_connect,
1150 .pru_detach = ngd_detach,
1151 .pru_disconnect = dummy_disconnect,
1152 .pru_peeraddr = NULL,
1153 .pru_send = ngd_send,
1154 .pru_shutdown = NULL,
1155 .pru_sockaddr = ng_getsockaddr,
1156 .pru_close = NULL,
1157 };
1158
1159 /*
1160 * Definitions of protocols supported in the NETGRAPH domain.
1161 */
1162
1163 extern struct domain ngdomain; /* stop compiler warnings */
1164
1165 static struct protosw ngsw[] = {
1166 {
1167 .pr_type = SOCK_DGRAM,
1168 .pr_domain = &ngdomain,
1169 .pr_protocol = NG_CONTROL,
1170 .pr_flags = PR_ATOMIC | PR_ADDR /* | PR_RIGHTS */,
1171 .pr_usrreqs = &ngc_usrreqs
1172 },
1173 {
1174 .pr_type = SOCK_DGRAM,
1175 .pr_domain = &ngdomain,
1176 .pr_protocol = NG_DATA,
1177 .pr_flags = PR_ATOMIC | PR_ADDR,
1178 .pr_usrreqs = &ngd_usrreqs
1179 }
1180 };
1181
1182 struct domain ngdomain = {
1183 .dom_family = AF_NETGRAPH,
1184 .dom_name = "netgraph",
1185 .dom_protosw = ngsw,
1186 .dom_protoswNPROTOSW = &ngsw[nitems(ngsw)]
1187 };
1188
1189 /*
1190 * Handle loading and unloading for this node type.
1191 * This is to handle auxiliary linkages (e.g protocol domain addition).
1192 */
1193 static int
ngs_mod_event(module_t mod,int event,void * data)1194 ngs_mod_event(module_t mod, int event, void *data)
1195 {
1196 int error = 0;
1197
1198 switch (event) {
1199 case MOD_LOAD:
1200 mtx_init(&ngsocketlist_mtx, "ng_socketlist", NULL, MTX_DEF);
1201 break;
1202 case MOD_UNLOAD:
1203 /* Ensure there are no open netgraph sockets. */
1204 if (!LIST_EMPTY(&ngsocklist)) {
1205 error = EBUSY;
1206 break;
1207 }
1208 #ifdef NOTYET
1209 /* Unregister protocol domain XXX can't do this yet.. */
1210 #endif
1211 error = EBUSY;
1212 break;
1213 default:
1214 error = EOPNOTSUPP;
1215 break;
1216 }
1217 return (error);
1218 }
1219
1220 VNET_DOMAIN_SET(ng);
1221
1222 SYSCTL_INT(_net_graph, OID_AUTO, family, CTLFLAG_RD, SYSCTL_NULL_INT_PTR, AF_NETGRAPH, "");
1223 static SYSCTL_NODE(_net_graph, OID_AUTO, data, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1224 "DATA");
1225 SYSCTL_INT(_net_graph_data, OID_AUTO, proto, CTLFLAG_RD, SYSCTL_NULL_INT_PTR, NG_DATA, "");
1226 static SYSCTL_NODE(_net_graph, OID_AUTO, control, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1227 "CONTROL");
1228 SYSCTL_INT(_net_graph_control, OID_AUTO, proto, CTLFLAG_RD, SYSCTL_NULL_INT_PTR, NG_CONTROL, "");
1229