2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2000 Brian Somers <brian@Awfulhak.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/param.h>
32 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
38 #include <net/ethernet.h>
39 #include <netinet/in_systm.h>
40 #include <netinet/ip.h>
41 #include <netgraph/ng_ether.h>
42 #include <netgraph/ng_message.h>
43 #include <netgraph/ng_socket.h>
50 #include <sys/fcntl.h>
63 #include "throughput.h"
69 #include "descriptor.h"
78 #include "slcompress.h"
94 struct device dev; /* What struct physical knows about */
95 int cs; /* Control socket */
96 char hook[NG_HOOKSIZ]; /* Our socket node hook */
99 #define device2ng(d) ((d)->type == NG_DEVICE ? (struct ngdevice *)d : NULL)
100 #define NG_MSGBUFSZ 4096
101 #define NETGRAPH_PREFIX "netgraph:"
106 return sizeof(struct ngdevice);
110 ng_MessageOut(struct ngdevice *dev, const char *data)
112 char path[NG_PATHSIZ];
118 * We expect a node path, one or more spaces, a command, one or more
119 * spaces and an ascii netgraph structure.
121 data += strspn(data, " \t");
122 len = strcspn(data, " \t");
123 if (len >= sizeof path) {
124 log_Printf(LogWARN, "%s: %.*s: Node path too long\n",
125 dev->dev.name, len, data);
128 memcpy(path, data, len);
132 data += strspn(data, " \t");
133 len = strcspn(data, " \t");
134 for (pos = len; pos >= 0; pos--)
135 if (data[pos] == '%')
137 if ((fmt = alloca(len + 4)) == NULL) {
138 log_Printf(LogWARN, "%s: alloca(%d) failure... %s\n",
139 dev->dev.name, len + 4, strerror(errno));
144 * This is probably a waste of time, but we really don't want to end
145 * up stuffing unexpected % escapes into the kernel....
147 for (pos = dpos = 0; pos < (int)len;) {
148 if (data[dpos] == '%')
150 fmt[pos++] = data[dpos++];
152 strcpy(fmt + pos, " %s");
155 data += strspn(data, " \t");
156 if (NgSendAsciiMsg(dev->cs, path, fmt, data) < 0) {
157 log_Printf(LogDEBUG, "%s: NgSendAsciiMsg (to %s): \"%s\", \"%s\": %s\n",
158 dev->dev.name, path, fmt, data, strerror(errno));
166 * Get a netgraph message
169 ng_MessageIn(struct physical *p, char *buf, size_t sz)
171 char msgbuf[sizeof(struct ng_mesg) * 2 + NG_MSGBUFSZ];
172 struct ngdevice *dev = device2ng(p->handler);
173 struct ng_mesg *rep = (struct ng_mesg *)msgbuf;
174 char path[NG_PATHSIZ];
185 if ((r = mkfdset()) == NULL) {
186 log_Printf(LogERROR, "DoLoop: Cannot create fd_set\n");
191 t.tv_sec = t.tv_usec = 0;
192 ret = select(dev->cs + 1, r, NULL, NULL, &t);
199 if (NgRecvAsciiMsg(dev->cs, rep, sizeof msgbuf, path)) {
200 log_Printf(LogWARN, "%s: NgRecvAsciiMsg: %s\n",
201 dev->dev.name, strerror(errno));
205 /* XXX: Should we check rep->header.version ? */
208 log_Printf(LogWARN, "%s: Unexpected message: %s\n", dev->dev.name,
211 log_Printf(LogDEBUG, "%s: Received message: %s\n", dev->dev.name,
213 len = strlen(rep->header.cmdstr);
216 memcpy(buf, rep->header.cmdstr, sz);
223 ng_Write(struct physical *p, const void *v, size_t n)
225 struct ngdevice *dev = device2ng(p->handler);
227 switch (p->dl->state) {
230 return ng_MessageOut(dev, v) ? (ssize_t)n : -1;
232 return NgSendData(p->fd, dev->hook, v, n) == -1 ? -1 : (ssize_t)n;
236 ng_Read(struct physical *p, void *v, size_t n)
238 char hook[NG_HOOKSIZ];
240 switch (p->dl->state) {
243 return ng_MessageIn(p, v, n);
246 return NgRecvData(p->fd, v, n, hook);
250 ng_RemoveFromSet(struct physical *p, fd_set *r, fd_set *w, fd_set *e)
252 struct ngdevice *dev = device2ng(p->handler);
255 if (r && dev->cs >= 0 && FD_ISSET(dev->cs, r)) {
257 log_Printf(LogTIMER, "%s: fdunset(ctrl) %d\n", p->link.name, dev->cs);
262 /* Careful... physical_RemoveFromSet() called us ! */
264 p->handler->removefromset = NULL;
265 result += physical_RemoveFromSet(p, r, w, e);
266 p->handler->removefromset = ng_RemoveFromSet;
272 ng_Free(struct physical *p)
274 struct ngdevice *dev = device2ng(p->handler);
276 physical_SetDescriptor(p);
283 ng_device2iov(struct device *d, struct iovec *iov, int *niov,
284 int maxiov __unused, int *auxfd, int *nauxfd)
286 struct ngdevice *dev;
287 int sz = physical_MaxDeviceSize();
289 iov[*niov].iov_base = d = realloc(d, sz);
291 log_Printf(LogALERT, "Failed to allocate memory: %d\n", sz);
292 AbortProgram(EX_OSERR);
294 iov[*niov].iov_len = sz;
302 static const struct device basengdevice = {
306 { CD_REQUIRED, DEF_NGCDDELAY },
324 ng_iov2device(int type, struct physical *p, struct iovec *iov, int *niov,
325 int maxiov __unused, int *auxfd, int *nauxfd)
327 if (type == NG_DEVICE) {
328 struct ngdevice *dev = (struct ngdevice *)iov[(*niov)++].iov_base;
330 dev = realloc(dev, sizeof *dev); /* Reduce to the correct size */
332 log_Printf(LogALERT, "Failed to allocate memory: %d\n",
334 AbortProgram(EX_OSERR);
343 /* Refresh function pointers etc */
344 memcpy(&dev->dev, &basengdevice, sizeof dev->dev);
346 /* XXX: Are netgraph always synchronous ? */
347 physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNCNOACF);
355 ng_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
357 struct physical *p = descriptor2physical(d);
358 struct ngdevice *dev = device2ng(p->handler);
361 switch (p->dl->state) {
366 log_Printf(LogTIMER, "%s(ctrl): fdset(r) %d\n", p->link.name, dev->cs);
373 result = physical_doUpdateSet(d, r, w, e, n, 0);
381 ng_IsSet(struct fdescriptor *d, const fd_set *fdset)
383 struct physical *p = descriptor2physical(d);
384 struct ngdevice *dev = device2ng(p->handler);
387 result = dev->cs >= 0 && FD_ISSET(dev->cs, fdset);
388 result += physical_IsSet(d, fdset);
394 ng_DescriptorRead(struct fdescriptor *d, struct bundle *bundle,
397 struct physical *p = descriptor2physical(d);
398 struct ngdevice *dev = device2ng(p->handler);
400 if (dev->cs >= 0 && FD_ISSET(dev->cs, fdset))
401 ng_MessageIn(p, NULL, 0);
403 if (physical_IsSet(d, fdset))
404 physical_DescriptorRead(d, bundle, fdset);
407 static struct device *
408 ng_Abandon(struct ngdevice *dev, struct physical *p)
410 /* Abandon our node construction */
413 p->fd = -2; /* Nobody else need try.. */
421 * Populate the ``word'' (of size ``sz'') named ``what'' from ``from''
422 * ending with any character from ``sep''. Point ``endp'' at the next
426 #define GETSEGMENT(what, from, sep, endp) \
427 getsegment(#what, (what), sizeof(what), from, sep, endp)
430 getsegment(const char *what, char *word, size_t sz, const char *from,
431 const char *sep, const char **endp)
435 if ((len = strcspn(from, sep)) == 0) {
436 log_Printf(LogWARN, "%s name should not be empty !\n", what);
441 log_Printf(LogWARN, "%s name too long, max %d !\n", what, sz - 1);
445 strncpy(word, from, len);
449 *endp += strspn(*endp, sep);
455 ng_Create(struct physical *p)
457 struct sockaddr_ng ngsock;
459 struct sockaddr *sock = (struct sockaddr *)&ngsock;
460 const struct hooklist *hlist;
461 const struct nodeinfo *ninfo;
462 const struct linkinfo *nlink;
463 struct ngdevice *dev;
464 struct ng_mesg *resp;
465 struct ngm_mkpeer mkp;
466 struct ngm_connect ngc;
467 const char *devp, *endp;
468 char lasthook[NG_HOOKSIZ];
469 char hook[NG_HOOKSIZ];
470 char nodetype[NG_TYPESIZ + NG_NODESIZ];
471 char modname[NG_TYPESIZ + 3];
472 char path[NG_PATHSIZ];
478 if (p->fd < 0 && !strncasecmp(p->name.full, NETGRAPH_PREFIX,
479 sizeof NETGRAPH_PREFIX - 1)) {
480 p->fd--; /* We own the device - change fd */
482 if ((dev = malloc(sizeof *dev)) == NULL)
485 loadmodules(LOAD_VERBOSLY, "netgraph", "ng_socket", NULL);
487 /* Create a socket node */
488 if (ID0NgMkSockNode(NULL, &dev->cs, &p->fd) == -1) {
489 log_Printf(LogWARN, "Cannot create netgraph socket node: %s\n",
496 devp = p->name.full + sizeof NETGRAPH_PREFIX - 1;
497 *lasthook = *path = '\0';
498 log_Printf(LogDEBUG, "%s: Opening netgraph device \"%s\"\n",
502 while (*devp != '\0' && !done) {
504 if (*lasthook == '\0') {
505 log_Printf(LogWARN, "%s: Netgraph devices must start with"
506 " [nodetype:nodename]\n", p->link.name);
507 return ng_Abandon(dev, p);
510 /* Get the hook name of the new node */
511 if (!GETSEGMENT(hook, devp, ".[", &endp))
512 return ng_Abandon(dev, p);
513 log_Printf(LogDEBUG, "%s: Got hook \"%s\"\n", p->link.name, hook);
516 log_Printf(LogWARN, "%s: Netgraph device must not end with a second"
517 " hook\n", p->link.name);
518 return ng_Abandon(dev, p);
520 if (devp[-1] != '[') {
521 log_Printf(LogWARN, "%s: Expected a [nodetype:nodename] at device"
522 " pos %d\n", p->link.name, devp - p->link.name - 1);
523 return ng_Abandon(dev, p);
526 /* Use lasthook as the hook name */
527 strcpy(hook, lasthook);
531 /* We've got ``lasthook'' and ``hook'', get the node type */
532 if (!GETSEGMENT(nodetype, devp, "]", &endp))
533 return ng_Abandon(dev, p);
534 log_Printf(LogDEBUG, "%s: Got node \"%s\"\n", p->link.name, nodetype);
536 if ((nodename = strchr(nodetype, ':')) != NULL) {
538 if (*nodename == '\0' && *nodetype == '\0') {
539 log_Printf(LogWARN, "%s: Empty [nodetype:nodename] at device"
540 " pos %d\n", p->link.name, devp - p->link.name - 1);
541 return ng_Abandon(dev, p);
545 /* Ignore optional colons after nodes */
546 devp = *endp == ':' ? endp + 1 : endp;
550 if (*lasthook == '\0') {
551 /* This is the first node in the chain */
552 if (nodename == NULL || *nodename == '\0') {
553 log_Printf(LogWARN, "%s: %s: No initial device nodename\n",
555 return ng_Abandon(dev, p);
558 if (*nodetype != '\0') {
559 /* Attempt to load the module */
560 snprintf(modname, sizeof modname, "ng_%s", nodetype);
561 log_Printf(LogDEBUG, "%s: Attempting to load %s.ko\n",
562 p->link.name, modname);
563 loadmodules(LOAD_QUIETLY, modname, NULL);
566 snprintf(path, sizeof path, "%s:", nodename);
567 /* XXX: If we have a node type, ensure it's correct */
570 * Ask for a list of hooks attached to the previous node. If we
571 * find the one we're interested in, and if it's connected to a
572 * node of the right type using the correct hook, use that.
573 * If we find the hook connected to something else, fail.
574 * If we find no match, mkpeer the new node.
576 if (*nodetype == '\0') {
577 log_Printf(LogWARN, "%s: Nodetype missing at device offset %d\n",
579 devp - p->name.full + sizeof NETGRAPH_PREFIX - 1);
580 return ng_Abandon(dev, p);
583 /* Get a list of node hooks */
584 if (NgSendMsg(dev->cs, path, NGM_GENERIC_COOKIE, NGM_LISTHOOKS,
586 log_Printf(LogWARN, "%s: %s Cannot send a LISTHOOOKS message: %s\n",
587 p->link.name, path, strerror(errno));
588 return ng_Abandon(dev, p);
591 /* Get our list back */
592 resp = (struct ng_mesg *)rbuf;
593 if (NgRecvMsg(dev->cs, resp, sizeof rbuf, NULL) <= 0) {
594 log_Printf(LogWARN, "%s: Cannot get netgraph response: %s\n",
595 p->link.name, strerror(errno));
596 return ng_Abandon(dev, p);
599 hlist = (const struct hooklist *)resp->data;
600 ninfo = &hlist->nodeinfo;
602 log_Printf(LogDEBUG, "List of netgraph node ``%s'' (id %x) hooks:\n",
605 /* look for a hook already attached. */
606 for (f = 0; f < ninfo->hooks; f++) {
607 nlink = &hlist->link[f];
609 log_Printf(LogDEBUG, " Found %s -> %s (type %s)\n", nlink->ourhook,
610 nlink->peerhook, nlink->nodeinfo.type);
612 if (!strcmp(nlink->ourhook, lasthook)) {
613 if (strcmp(nlink->peerhook, hook) ||
614 strcmp(nlink->nodeinfo.type, nodetype)) {
615 log_Printf(LogWARN, "%s: hook %s:%s is already in use\n",
616 p->link.name, nlink->ourhook, path);
617 return ng_Abandon(dev, p);
619 /* The node is already hooked up nicely.... reuse it */
624 if (f == ninfo->hooks) {
625 /* Attempt to load the module */
626 snprintf(modname, sizeof modname, "ng_%s", nodetype);
627 log_Printf(LogDEBUG, "%s: Attempting to load %s.ko\n",
628 p->link.name, modname);
629 loadmodules(LOAD_QUIETLY, modname, NULL);
631 /* Create (mkpeer) the new node */
633 snprintf(mkp.type, sizeof mkp.type, "%s", nodetype);
634 snprintf(mkp.ourhook, sizeof mkp.ourhook, "%s", lasthook);
635 snprintf(mkp.peerhook, sizeof mkp.peerhook, "%s", hook);
637 log_Printf(LogDEBUG, "%s: Doing MKPEER %s%s -> %s (type %s)\n",
638 p->link.name, path, mkp.ourhook, mkp.peerhook, nodetype);
640 if (NgSendMsg(dev->cs, path, NGM_GENERIC_COOKIE,
641 NGM_MKPEER, &mkp, sizeof mkp) < 0) {
642 log_Printf(LogWARN, "%s Cannot create %s netgraph node: %s\n",
643 path, nodetype, strerror(errno));
644 return ng_Abandon(dev, p);
648 snprintf(path + len, sizeof path - len, "%s%s",
649 path[len - 1] == ':' ? "" : ".", lasthook);
652 /* Get a list of node hooks */
653 if (NgSendMsg(dev->cs, path, NGM_GENERIC_COOKIE, NGM_LISTHOOKS,
655 log_Printf(LogWARN, "%s: %s Cannot send a LISTHOOOKS message: %s\n",
656 p->link.name, path, strerror(errno));
657 return ng_Abandon(dev, p);
660 /* Get our list back */
661 resp = (struct ng_mesg *)rbuf;
662 if (NgRecvMsg(dev->cs, resp, sizeof rbuf, NULL) <= 0) {
663 log_Printf(LogWARN, "%s: Cannot get netgraph response: %s\n",
664 p->link.name, strerror(errno));
665 return ng_Abandon(dev, p);
668 hlist = (const struct hooklist *)resp->data;
669 ninfo = &hlist->nodeinfo;
671 if (*lasthook != '\0' && nodename != NULL && *nodename != '\0' &&
672 strcmp(ninfo->name, nodename) &&
673 NgNameNode(dev->cs, path, "%s", nodename) < 0) {
674 log_Printf(LogWARN, "%s: %s: Cannot name netgraph node: %s\n",
675 p->link.name, path, strerror(errno));
676 return ng_Abandon(dev, p);
679 if (!GETSEGMENT(lasthook, devp, " \t.[", &endp))
680 return ng_Abandon(dev, p);
681 log_Printf(LogDEBUG, "%s: Got hook \"%s\"\n", p->link.name, lasthook);
683 len = strlen(lasthook);
684 done = strchr(" \t", devp[len]) ? 1 : 0;
690 } /* else should moan about devp[-1] being '[' ? */
693 snprintf(dev->hook, sizeof dev->hook, "%s", lasthook);
695 /* Connect the node to our socket node */
696 snprintf(ngc.path, sizeof ngc.path, "%s", path);
697 snprintf(ngc.ourhook, sizeof ngc.ourhook, "%s", dev->hook);
698 memcpy(ngc.peerhook, ngc.ourhook, sizeof ngc.peerhook);
700 log_Printf(LogDEBUG, "Connecting netgraph socket .:%s -> %s.%s\n",
701 ngc.ourhook, ngc.path, ngc.peerhook);
702 if (NgSendMsg(dev->cs, ".:", NGM_GENERIC_COOKIE,
703 NGM_CONNECT, &ngc, sizeof ngc) < 0) {
704 log_Printf(LogWARN, "Cannot connect %s and socket netgraph "
705 "nodes: %s\n", path, strerror(errno));
706 return ng_Abandon(dev, p);
709 /* Hook things up so that we monitor dev->cs */
710 p->desc.UpdateSet = ng_UpdateSet;
711 p->desc.IsSet = ng_IsSet;
712 p->desc.Read = ng_DescriptorRead;
714 memcpy(&dev->dev, &basengdevice, sizeof dev->dev);
717 /* See if we're a netgraph socket */
720 if (getsockname(p->fd, sock, &sz) != -1 && sock->sa_family == AF_NETGRAPH) {
722 * It's a netgraph node... We can't determine hook names etc, so we
723 * stay pretty impartial....
725 log_Printf(LogPHASE, "%s: Link is a netgraph node\n", p->link.name);
727 if ((dev = malloc(sizeof *dev)) == NULL) {
728 log_Printf(LogWARN, "%s: Cannot allocate an ether device: %s\n",
729 p->link.name, strerror(errno));
733 memcpy(&dev->dev, &basengdevice, sizeof dev->dev);
740 physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNCNOACF);