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