2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1999 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>
40 #include <net/route.h>
41 #include <netinet/in_systm.h>
42 #include <netinet/ip.h>
43 #include <netgraph/ng_ether.h>
44 #include <netgraph/ng_message.h>
45 #include <netgraph/ng_pppoe.h>
46 #include <netgraph/ng_socket.h>
53 #include <sys/fcntl.h>
68 #include "throughput.h"
74 #include "descriptor.h"
83 #include "slcompress.h"
101 #define PPPOE_NODE_TYPE_LEN (sizeof NG_PPPOE_NODE_TYPE - 1) /* "PPPoE" */
104 struct device dev; /* What struct physical knows about */
105 int cs; /* Control socket */
106 int connected; /* Are we connected yet ? */
107 int timeout; /* Seconds attempting to connect */
108 char hook[sizeof TUN_NAME + 11]; /* Our socket node hook */
109 u_int32_t slot; /* ifindex << 24 | unit */
112 #define device2ether(d) \
113 ((d)->type == ETHER_DEVICE ? (struct etherdevice *)d : NULL)
116 ether_DeviceSize(void)
118 return sizeof(struct etherdevice);
122 ether_Write(struct physical *p, const void *v, size_t n)
124 struct etherdevice *dev = device2ether(p->handler);
126 return NgSendData(p->fd, dev->hook, v, n) == -1 ? -1 : (ssize_t)n;
130 ether_Read(struct physical *p, void *v, size_t n)
132 char hook[sizeof TUN_NAME + 11];
134 return NgRecvData(p->fd, v, n, hook);
138 ether_RemoveFromSet(struct physical *p, fd_set *r, fd_set *w, fd_set *e)
140 struct etherdevice *dev = device2ether(p->handler);
143 if (r && dev->cs >= 0 && FD_ISSET(dev->cs, r)) {
145 log_Printf(LogTIMER, "%s: fdunset(ctrl) %d\n", p->link.name, dev->cs);
150 /* Careful... physical_RemoveFromSet() called us ! */
152 p->handler->removefromset = NULL;
153 result += physical_RemoveFromSet(p, r, w, e);
154 p->handler->removefromset = ether_RemoveFromSet;
160 ether_Free(struct physical *p)
162 struct etherdevice *dev = device2ether(p->handler);
164 physical_SetDescriptor(p);
171 ether_OpenInfo(struct physical *p)
173 struct etherdevice *dev = device2ether(p->handler);
175 switch (dev->connected) {
176 case CARRIER_PENDING:
177 return "negotiating";
179 return "established";
182 return "disconnected";
186 ether_Slot(struct physical *p)
188 struct etherdevice *dev = device2ether(p->handler);
195 ether_device2iov(struct device *d, struct iovec *iov, int *niov,
196 int maxiov __unused, int *auxfd, int *nauxfd)
198 struct etherdevice *dev;
199 int sz = physical_MaxDeviceSize();
201 iov[*niov].iov_base = d = realloc(d, sz);
203 log_Printf(LogALERT, "Failed to allocate memory: %d\n", sz);
204 AbortProgram(EX_OSERR);
206 iov[*niov].iov_len = sz;
209 dev = device2ether(d);
217 ether_MessageIn(struct etherdevice *dev)
219 char msgbuf[sizeof(struct ng_mesg) + sizeof(struct ngpppoe_sts)];
220 struct ng_mesg *rep = (struct ng_mesg *)msgbuf;
221 struct ngpppoe_sts *sts = (struct ngpppoe_sts *)(msgbuf + sizeof *rep);
222 char *end, unknown[14], sessionid[5];
232 if ((r = mkfdset()) == NULL) {
233 log_Printf(LogERROR, "DoLoop: Cannot create fd_set\n");
240 t.tv_sec = t.tv_usec = 0;
241 ret = select(dev->cs + 1, r, NULL, NULL, &t);
246 if (NgRecvMsg(dev->cs, rep, sizeof msgbuf, NULL) <= 0)
249 if (rep->header.version != NG_VERSION) {
250 log_Printf(LogWARN, "%ld: Unexpected netgraph version, expected %ld\n",
251 (long)rep->header.version, (long)NG_VERSION);
255 if (rep->header.typecookie != NGM_PPPOE_COOKIE) {
256 log_Printf(LogWARN, "%ld: Unexpected netgraph cookie, expected %ld\n",
257 (long)rep->header.typecookie, (long)NGM_PPPOE_COOKIE);
262 switch (rep->header.cmd) {
263 case NGM_PPPOE_SET_FLAG: msg = "SET_FLAG"; break;
264 case NGM_PPPOE_CONNECT: msg = "CONNECT"; break;
265 case NGM_PPPOE_LISTEN: msg = "LISTEN"; break;
266 case NGM_PPPOE_OFFER: msg = "OFFER"; break;
267 case NGM_PPPOE_SUCCESS: msg = "SUCCESS"; break;
268 case NGM_PPPOE_FAIL: msg = "FAIL"; break;
269 case NGM_PPPOE_CLOSE: msg = "CLOSE"; break;
270 case NGM_PPPOE_GET_STATUS: msg = "GET_STATUS"; break;
271 case NGM_PPPOE_ACNAME:
273 if (setenv("ACNAME", sts->hook, 1) != 0)
274 log_Printf(LogWARN, "setenv: cannot set ACNAME=%s: %m", sts->hook);
275 asciilen = rep->header.arglen;
277 case NGM_PPPOE_SESSIONID:
279 snprintf(sessionid, sizeof sessionid, "%04x", *(u_int16_t *)sts);
280 if (setenv("SESSIONID", sessionid, 1) != 0)
281 syslog(LOG_WARNING, "setenv: cannot set SESSIONID=%s: %m",
283 /* Use this in preference to our interface index */
284 slot = strtoul(sessionid, &end, 16);
285 if (end != sessionid && *end == '\0')
289 snprintf(unknown, sizeof unknown, "<%d>", (int)rep->header.cmd);
295 log_Printf(LogPHASE, "Received NGM_PPPOE_%s (hook \"%.*s\")\n",
296 msg, asciilen, sts->hook);
298 log_Printf(LogPHASE, "Received NGM_PPPOE_%s\n", msg);
300 switch (rep->header.cmd) {
301 case NGM_PPPOE_SUCCESS:
302 dev->connected = CARRIER_OK;
305 case NGM_PPPOE_CLOSE:
306 dev->connected = CARRIER_LOST;
314 ether_AwaitCarrier(struct physical *p)
316 struct etherdevice *dev = device2ether(p->handler);
318 if (dev->connected != CARRIER_OK && !dev->timeout--)
319 dev->connected = CARRIER_LOST;
320 else if (dev->connected == CARRIER_PENDING)
321 ether_MessageIn(dev);
323 return dev->connected;
326 static const struct device baseetherdevice = {
330 { CD_REQUIRED, DEF_ETHERCDDELAY },
348 ether_iov2device(int type, struct physical *p, struct iovec *iov, int *niov,
349 int maxiov __unused, int *auxfd, int *nauxfd)
351 if (type == ETHER_DEVICE) {
352 struct etherdevice *dev = (struct etherdevice *)iov[(*niov)++].iov_base;
354 dev = realloc(dev, sizeof *dev); /* Reduce to the correct size */
356 log_Printf(LogALERT, "Failed to allocate memory: %d\n",
358 AbortProgram(EX_OSERR);
367 /* Refresh function pointers etc */
368 memcpy(&dev->dev, &baseetherdevice, sizeof dev->dev);
370 physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNCNOACF);
378 ether_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
380 struct physical *p = descriptor2physical(d);
381 struct etherdevice *dev = device2ether(p->handler);
384 if (r && dev->cs >= 0) {
386 log_Printf(LogTIMER, "%s(ctrl): fdset(r) %d\n", p->link.name, dev->cs);
391 result += physical_doUpdateSet(d, r, w, e, n, 0);
397 ether_IsSet(struct fdescriptor *d, const fd_set *fdset)
399 struct physical *p = descriptor2physical(d);
400 struct etherdevice *dev = device2ether(p->handler);
403 result = dev->cs >= 0 && FD_ISSET(dev->cs, fdset);
404 result += physical_IsSet(d, fdset);
410 ether_DescriptorRead(struct fdescriptor *d, struct bundle *bundle,
413 struct physical *p = descriptor2physical(d);
414 struct etherdevice *dev = device2ether(p->handler);
416 if (dev->cs >= 0 && FD_ISSET(dev->cs, fdset)) {
417 ether_MessageIn(dev);
418 if (dev->connected == CARRIER_LOST) {
419 log_Printf(LogPHASE, "%s: Device disconnected\n", p->link.name);
420 datalink_Down(p->dl, CLOSE_NORMAL);
425 if (physical_IsSet(d, fdset))
426 physical_DescriptorRead(d, bundle, fdset);
429 static struct device *
430 ether_Abandon(struct etherdevice *dev, struct physical *p)
432 /* Abandon our node construction */
435 p->fd = -2; /* Nobody else need try.. */
442 ether_Create(struct physical *p)
445 struct etherdevice *dev;
446 struct ng_mesg *resp;
447 const struct hooklist *hlist;
448 const struct nodeinfo *ninfo;
449 char *path, *sessionid;
457 if (p->fd < 0 && !strncasecmp(p->name.full, NG_PPPOE_NODE_TYPE,
458 PPPOE_NODE_TYPE_LEN) &&
459 p->name.full[PPPOE_NODE_TYPE_LEN] == ':') {
460 const struct linkinfo *nlink;
461 struct ngpppoe_init_data *data;
462 struct ngm_mkpeer mkp;
463 struct ngm_connect ngc;
464 const char *iface, *provider;
467 char connectpath[sizeof dev->hook + 2]; /* .:<hook> */
469 p->fd--; /* We own the device - change fd */
471 loadmodules(LOAD_VERBOSLY, "netgraph", "ng_ether", "ng_pppoe", "ng_socket",
474 if ((dev = malloc(sizeof *dev)) == NULL)
477 iface = p->name.full + PPPOE_NODE_TYPE_LEN + 1;
479 provider = strchr(iface, ':');
481 ifacelen = provider - iface;
483 providerlen = strlen(provider);
485 ifacelen = strlen(iface);
491 * We're going to do this (where tunN is our tunnel device):
495 * | <iface> | dev->cs
501 * .---------. .-----------.
502 * | pppoe | | socket |
503 * | <iface> |(tunN)<---->(tunN)| <unnamed> |
504 * `--------- `-----------'
511 /* Create a socket node */
512 if (ID0NgMkSockNode(NULL, &dev->cs, &p->fd) == -1) {
513 log_Printf(LogWARN, "Cannot create netgraph socket node: %s\n",
521 * Ask for a list of hooks attached to the "ether" node. This node should
522 * magically exist as a way of hooking stuff onto an ethernet device
524 path = (char *)alloca(ifacelen + 2);
525 sprintf(path, "%.*s:", (int)ifacelen, iface);
526 if (NgSendMsg(dev->cs, path, NGM_GENERIC_COOKIE, NGM_LISTHOOKS,
528 log_Printf(LogWARN, "%s Cannot send a netgraph message: %s\n",
529 path, strerror(errno));
530 return ether_Abandon(dev, p);
533 /* Get our list back */
534 resp = (struct ng_mesg *)rbuf;
535 if (NgRecvMsg(dev->cs, resp, sizeof rbuf, NULL) <= 0) {
536 log_Printf(LogWARN, "Cannot get netgraph response: %s\n",
538 return ether_Abandon(dev, p);
541 hlist = (const struct hooklist *)resp->data;
542 ninfo = &hlist->nodeinfo;
544 /* Make sure we've got the right type of node */
545 if (strncmp(ninfo->type, NG_ETHER_NODE_TYPE,
546 sizeof NG_ETHER_NODE_TYPE - 1)) {
547 log_Printf(LogWARN, "%s Unexpected node type ``%s'' (wanted ``"
548 NG_ETHER_NODE_TYPE "'')\n", path, ninfo->type);
549 return ether_Abandon(dev, p);
552 log_Printf(LogDEBUG, "List of netgraph node ``%s'' (id %x) hooks:\n",
555 /* look for a hook already attached. */
556 for (f = 0; f < ninfo->hooks; f++) {
557 nlink = &hlist->link[f];
559 log_Printf(LogDEBUG, " Found %s -> %s\n", nlink->ourhook,
562 if (!strcmp(nlink->ourhook, NG_ETHER_HOOK_ORPHAN) ||
563 !strcmp(nlink->ourhook, NG_ETHER_HOOK_DIVERT)) {
565 * Something is using the data coming out of this ``ether'' node.
566 * If it's a PPPoE node, we use that node, otherwise we complain that
567 * someone else is using the node.
569 if (!strcmp(nlink->nodeinfo.type, NG_PPPOE_NODE_TYPE))
570 /* Use this PPPoE node ! */
571 snprintf(ngc.path, sizeof ngc.path, "[%x]:", nlink->nodeinfo.id);
573 log_Printf(LogWARN, "%s Node type ``%s'' is currently active\n",
574 path, nlink->nodeinfo.type);
575 return ether_Abandon(dev, p);
581 if (f == ninfo->hooks) {
583 * Create a new ``PPPoE'' node connected to the ``ether'' node using
584 * the ``orphan'' and ``ethernet'' hooks
586 snprintf(mkp.type, sizeof mkp.type, "%s", NG_PPPOE_NODE_TYPE);
587 snprintf(mkp.ourhook, sizeof mkp.ourhook, "%s", NG_ETHER_HOOK_ORPHAN);
588 snprintf(mkp.peerhook, sizeof mkp.peerhook, "%s", NG_PPPOE_HOOK_ETHERNET);
589 snprintf(etherid, sizeof etherid, "[%x]:", ninfo->id);
591 log_Printf(LogDEBUG, "Creating PPPoE netgraph node %s%s -> %s\n",
592 etherid, mkp.ourhook, mkp.peerhook);
594 if (NgSendMsg(dev->cs, etherid, NGM_GENERIC_COOKIE,
595 NGM_MKPEER, &mkp, sizeof mkp) < 0) {
596 log_Printf(LogWARN, "%s Cannot create PPPoE netgraph node: %s\n",
597 etherid, strerror(errno));
598 return ether_Abandon(dev, p);
601 snprintf(ngc.path, sizeof ngc.path, "%s%s", path, NG_ETHER_HOOK_ORPHAN);
604 snprintf(dev->hook, sizeof dev->hook, "%s%d",
605 TUN_NAME, p->dl->bundle->unit);
608 * Connect the PPPoE node to our socket node.
609 * ngc.path has already been set up
611 snprintf(ngc.ourhook, sizeof ngc.ourhook, "%s", dev->hook);
612 memcpy(ngc.peerhook, ngc.ourhook, sizeof ngc.peerhook);
614 log_Printf(LogDEBUG, "Connecting netgraph socket .:%s -> %s:%s\n",
615 ngc.ourhook, ngc.path, ngc.peerhook);
616 if (NgSendMsg(dev->cs, ".:", NGM_GENERIC_COOKIE,
617 NGM_CONNECT, &ngc, sizeof ngc) < 0) {
618 log_Printf(LogWARN, "Cannot connect PPPoE and socket netgraph "
619 "nodes: %s\n", strerror(errno));
620 return ether_Abandon(dev, p);
623 /* Bring the Ethernet interface up */
624 path[ifacelen] = '\0'; /* Remove the trailing ':' */
625 if (!iface_SetFlags(path, IFF_UP))
626 log_Printf(LogWARN, "%s: Failed to set the IFF_UP flag on %s\n",
629 snprintf(connectpath, sizeof connectpath, ".:%s", dev->hook);
631 /* Configure node to 3Com mode if needed */
632 if (p->cfg.pppoe_configured) {
633 mode = p->cfg.nonstandard_pppoe ? NG_PPPOE_NONSTANDARD : NG_PPPOE_STANDARD;
634 if (NgSendMsg(dev->cs, connectpath, NGM_PPPOE_COOKIE,
635 NGM_PPPOE_SETMODE, mode, strlen(mode) + 1) == -1) {
636 log_Printf(LogWARN, "``%s'': Cannot configure netgraph node: %s\n",
637 connectpath, strerror(errno));
638 return ether_Abandon(dev, p);
642 /* And finally, request a connection to the given provider */
644 data = (struct ngpppoe_init_data *)alloca(sizeof *data + providerlen);
645 snprintf(data->hook, sizeof data->hook, "%s", dev->hook);
646 memcpy(data->data, provider, providerlen);
647 data->data_len = providerlen;
649 log_Printf(LogDEBUG, "Sending PPPOE_CONNECT to %s\n", connectpath);
650 if (NgSendMsg(dev->cs, connectpath, NGM_PPPOE_COOKIE,
651 NGM_PPPOE_CONNECT, data, sizeof *data + providerlen) == -1) {
652 log_Printf(LogWARN, "``%s'': Cannot start netgraph node: %s\n",
653 connectpath, strerror(errno));
654 return ether_Abandon(dev, p);
657 /* Hook things up so that we monitor dev->cs */
658 p->desc.UpdateSet = ether_UpdateSet;
659 p->desc.IsSet = ether_IsSet;
660 p->desc.Read = ether_DescriptorRead;
662 memcpy(&dev->dev, &baseetherdevice, sizeof dev->dev);
663 switch (p->cfg.cd.necessity) {
665 dev->dev.cd.delay = p->cfg.cd.delay;
668 dev->dev.cd = p->cfg.cd;
671 log_Printf(LogWARN, "%s: Carrier must be set, using ``set cd %d!''\n",
672 p->link.name, dev->dev.cd.delay);
677 dev->timeout = dev->dev.cd.delay;
678 dev->connected = CARRIER_PENDING;
679 /* This will be overridden by our session id - if provided by netgraph */
680 dev->slot = GetIfIndex(path);
682 /* See if we're a netgraph socket */
685 if (fstat(p->fd, &st) != -1 && (st.st_mode & S_IFSOCK)) {
686 struct sockaddr_storage ssock;
687 struct sockaddr *sock = (struct sockaddr *)&ssock;
691 if (getsockname(p->fd, sock, &sz) == -1) {
692 log_Printf(LogPHASE, "%s: Link is a closed socket !\n", p->link.name);
698 if (sock->sa_family == AF_NETGRAPH) {
700 * It's a netgraph node... We can't determine hook names etc, so we
701 * stay pretty impartial....
703 log_Printf(LogPHASE, "%s: Link is a netgraph node\n", p->link.name);
705 if ((dev = malloc(sizeof *dev)) == NULL) {
706 log_Printf(LogWARN, "%s: Cannot allocate an ether device: %s\n",
707 p->link.name, strerror(errno));
711 memcpy(&dev->dev, &baseetherdevice, sizeof dev->dev);
714 dev->connected = CARRIER_OK;
718 * If we're being envoked from pppoed(8), we may have a SESSIONID
719 * set in the environment. If so, use it as the slot
721 if ((sessionid = getenv("SESSIONID")) != NULL) {
725 slot = strtoul(sessionid, &end, 16);
726 dev->slot = end != sessionid && *end == '\0' ? slot : 0;
734 physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNCNOACF);