2 * Copyright (c) 1999 Brian Somers <brian@Awfulhak.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/usr.sbin/ppp/ether.c,v 1.9.2.14 2002/09/01 02:12:26 brian Exp $
29 #include <sys/param.h>
30 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
36 #include <net/ethernet.h>
38 #include <net/route.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_pppoe.h>
44 #include <netgraph/ng_socket.h>
51 #include <sys/fcntl.h>
66 #include "throughput.h"
72 #include "descriptor.h"
81 #include "slcompress.h"
99 #define PPPOE_NODE_TYPE_LEN (sizeof NG_PPPOE_NODE_TYPE - 1) /* "PPPoE" */
102 struct device dev; /* What struct physical knows about */
103 int cs; /* Control socket */
104 int connected; /* Are we connected yet ? */
105 int timeout; /* Seconds attempting to connect */
106 char hook[sizeof TUN_NAME + 11]; /* Our socket node hook */
107 u_int32_t slot; /* ifindex << 24 | unit */
110 #define device2ether(d) \
111 ((d)->type == ETHER_DEVICE ? (struct etherdevice *)d : NULL)
114 ether_DeviceSize(void)
116 return sizeof(struct etherdevice);
120 ether_Write(struct physical *p, const void *v, size_t n)
122 struct etherdevice *dev = device2ether(p->handler);
124 return NgSendData(p->fd, dev->hook, v, n) == -1 ? -1 : n;
128 ether_Read(struct physical *p, void *v, size_t n)
130 char hook[sizeof TUN_NAME + 11];
132 return NgRecvData(p->fd, v, n, hook);
136 ether_RemoveFromSet(struct physical *p, fd_set *r, fd_set *w, fd_set *e)
138 struct etherdevice *dev = device2ether(p->handler);
141 if (r && dev->cs >= 0 && FD_ISSET(dev->cs, r)) {
143 log_Printf(LogTIMER, "%s: fdunset(ctrl) %d\n", p->link.name, dev->cs);
148 /* Careful... physical_RemoveFromSet() called us ! */
150 p->handler->removefromset = NULL;
151 result += physical_RemoveFromSet(p, r, w, e);
152 p->handler->removefromset = ether_RemoveFromSet;
158 ether_Free(struct physical *p)
160 struct etherdevice *dev = device2ether(p->handler);
162 physical_SetDescriptor(p);
169 ether_OpenInfo(struct physical *p)
171 struct etherdevice *dev = device2ether(p->handler);
173 switch (dev->connected) {
174 case CARRIER_PENDING:
175 return "negotiating";
177 return "established";
180 return "disconnected";
184 ether_Slot(struct physical *p)
186 struct etherdevice *dev = device2ether(p->handler);
193 ether_device2iov(struct device *d, struct iovec *iov, int *niov,
194 int maxiov, int *auxfd, int *nauxfd)
196 struct etherdevice *dev = device2ether(d);
197 int sz = physical_MaxDeviceSize();
199 iov[*niov].iov_base = realloc(d, sz);
200 if (iov[*niov].iov_base == NULL) {
201 log_Printf(LogALERT, "Failed to allocate memory: %d\n", sz);
202 AbortProgram(EX_OSERR);
204 iov[*niov].iov_len = sz;
214 ether_MessageIn(struct etherdevice *dev)
216 char msgbuf[sizeof(struct ng_mesg) + sizeof(struct ngpppoe_sts)];
217 struct ng_mesg *rep = (struct ng_mesg *)msgbuf;
218 struct ngpppoe_sts *sts = (struct ngpppoe_sts *)(msgbuf + sizeof *rep);
219 char *end, unknown[14], sessionid[5];
229 if ((r = mkfdset()) == NULL) {
230 log_Printf(LogERROR, "DoLoop: Cannot create fd_set\n");
237 t.tv_sec = t.tv_usec = 0;
238 ret = select(dev->cs + 1, r, NULL, NULL, &t);
243 if (NgRecvMsg(dev->cs, rep, sizeof msgbuf, NULL) <= 0)
246 if (rep->header.version != NG_VERSION) {
247 log_Printf(LogWARN, "%ld: Unexpected netgraph version, expected %ld\n",
248 (long)rep->header.version, (long)NG_VERSION);
252 if (rep->header.typecookie != NGM_PPPOE_COOKIE) {
253 log_Printf(LogWARN, "%ld: Unexpected netgraph cookie, expected %ld\n",
254 (long)rep->header.typecookie, (long)NGM_PPPOE_COOKIE);
259 switch (rep->header.cmd) {
260 case NGM_PPPOE_SET_FLAG: msg = "SET_FLAG"; break;
261 case NGM_PPPOE_CONNECT: msg = "CONNECT"; break;
262 case NGM_PPPOE_LISTEN: msg = "LISTEN"; break;
263 case NGM_PPPOE_OFFER: msg = "OFFER"; break;
264 case NGM_PPPOE_SUCCESS: msg = "SUCCESS"; break;
265 case NGM_PPPOE_FAIL: msg = "FAIL"; break;
266 case NGM_PPPOE_CLOSE: msg = "CLOSE"; break;
267 case NGM_PPPOE_GET_STATUS: msg = "GET_STATUS"; break;
268 case NGM_PPPOE_ACNAME:
270 if (setenv("ACNAME", sts->hook, 1) != 0)
271 log_Printf(LogWARN, "setenv: cannot set ACNAME=%s: %m", sts->hook);
272 asciilen = rep->header.arglen;
274 case NGM_PPPOE_SESSIONID:
276 snprintf(sessionid, sizeof sessionid, "%04x", *(u_int16_t *)sts);
277 if (setenv("SESSIONID", sessionid, 1) != 0)
278 syslog(LOG_WARNING, "setenv: cannot set SESSIONID=%s: %m",
280 /* Use this in preference to our interface index */
281 slot = strtoul(sessionid, &end, 16);
282 if (end != sessionid && *end == '\0')
286 snprintf(unknown, sizeof unknown, "<%d>", (int)rep->header.cmd);
292 log_Printf(LogPHASE, "Received NGM_PPPOE_%s (hook \"%.*s\")\n",
293 msg, asciilen, sts->hook);
295 log_Printf(LogPHASE, "Received NGM_PPPOE_%s\n", msg);
297 switch (rep->header.cmd) {
298 case NGM_PPPOE_SUCCESS:
299 dev->connected = CARRIER_OK;
302 case NGM_PPPOE_CLOSE:
303 dev->connected = CARRIER_LOST;
311 ether_AwaitCarrier(struct physical *p)
313 struct etherdevice *dev = device2ether(p->handler);
315 if (dev->connected != CARRIER_OK && !dev->timeout--)
316 dev->connected = CARRIER_LOST;
317 else if (dev->connected == CARRIER_PENDING)
318 ether_MessageIn(dev);
320 return dev->connected;
323 static const struct device baseetherdevice = {
327 { CD_REQUIRED, DEF_ETHERCDDELAY },
345 ether_iov2device(int type, struct physical *p, struct iovec *iov, int *niov,
346 int maxiov, int *auxfd, int *nauxfd)
348 if (type == ETHER_DEVICE) {
349 struct etherdevice *dev = (struct etherdevice *)iov[(*niov)++].iov_base;
351 dev = realloc(dev, sizeof *dev); /* Reduce to the correct size */
353 log_Printf(LogALERT, "Failed to allocate memory: %d\n",
355 AbortProgram(EX_OSERR);
364 /* Refresh function pointers etc */
365 memcpy(&dev->dev, &baseetherdevice, sizeof dev->dev);
367 physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNCNOACF);
375 ether_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
377 struct physical *p = descriptor2physical(d);
378 struct etherdevice *dev = device2ether(p->handler);
381 if (r && dev->cs >= 0) {
383 log_Printf(LogTIMER, "%s(ctrl): fdset(r) %d\n", p->link.name, dev->cs);
388 result += physical_doUpdateSet(d, r, w, e, n, 0);
394 ether_IsSet(struct fdescriptor *d, const fd_set *fdset)
396 struct physical *p = descriptor2physical(d);
397 struct etherdevice *dev = device2ether(p->handler);
400 result = dev->cs >= 0 && FD_ISSET(dev->cs, fdset);
401 result += physical_IsSet(d, fdset);
407 ether_DescriptorRead(struct fdescriptor *d, struct bundle *bundle,
410 struct physical *p = descriptor2physical(d);
411 struct etherdevice *dev = device2ether(p->handler);
413 if (dev->cs >= 0 && FD_ISSET(dev->cs, fdset)) {
414 ether_MessageIn(dev);
415 if (dev->connected == CARRIER_LOST) {
416 log_Printf(LogPHASE, "%s: Device disconnected\n", p->link.name);
417 datalink_Down(p->dl, CLOSE_NORMAL);
422 if (physical_IsSet(d, fdset))
423 physical_DescriptorRead(d, bundle, fdset);
426 static struct device *
427 ether_Abandon(struct etherdevice *dev, struct physical *p)
429 /* Abandon our node construction */
432 p->fd = -2; /* Nobody else need try.. */
439 ether_Create(struct physical *p)
442 struct etherdevice *dev;
443 struct ng_mesg *resp;
444 const struct hooklist *hlist;
445 const struct nodeinfo *ninfo;
446 char *path, *sessionid;
452 if (p->fd < 0 && !strncasecmp(p->name.full, NG_PPPOE_NODE_TYPE,
453 PPPOE_NODE_TYPE_LEN) &&
454 p->name.full[PPPOE_NODE_TYPE_LEN] == ':') {
455 const struct linkinfo *nlink;
456 struct ngpppoe_init_data *data;
457 struct ngm_mkpeer mkp;
458 struct ngm_connect ngc;
459 const char *iface, *provider;
462 char connectpath[sizeof dev->hook + 2]; /* .:<hook> */
464 p->fd--; /* We own the device - change fd */
466 loadmodules(LOAD_VERBOSLY, "netgraph", "ng_ether", "ng_pppoe", "ng_socket",
469 if ((dev = malloc(sizeof *dev)) == NULL)
472 iface = p->name.full + PPPOE_NODE_TYPE_LEN + 1;
474 provider = strchr(iface, ':');
476 ifacelen = provider - iface;
478 providerlen = strlen(provider);
480 ifacelen = strlen(iface);
486 * We're going to do this (where tunN is our tunnel device):
490 * | <iface> | dev->cs
496 * .---------. .-----------.
497 * | pppoe | | socket |
498 * | <iface> |(tunN)<---->(tunN)| <unnamed> |
499 * `--------- `-----------'
506 /* Create a socket node */
507 if (ID0NgMkSockNode(NULL, &dev->cs, &p->fd) == -1) {
508 log_Printf(LogWARN, "Cannot create netgraph socket node: %s\n",
516 * Ask for a list of hooks attached to the "ether" node. This node should
517 * magically exist as a way of hooking stuff onto an ethernet device
519 path = (char *)alloca(ifacelen + 2);
520 sprintf(path, "%.*s:", ifacelen, iface);
521 if (NgSendMsg(dev->cs, path, NGM_GENERIC_COOKIE, NGM_LISTHOOKS,
523 log_Printf(LogWARN, "%s Cannot send a netgraph message: %s\n",
524 path, strerror(errno));
525 return ether_Abandon(dev, p);
528 /* Get our list back */
529 resp = (struct ng_mesg *)rbuf;
530 if (NgRecvMsg(dev->cs, resp, sizeof rbuf, NULL) <= 0) {
531 log_Printf(LogWARN, "Cannot get netgraph response: %s\n",
533 return ether_Abandon(dev, p);
536 hlist = (const struct hooklist *)resp->data;
537 ninfo = &hlist->nodeinfo;
539 /* Make sure we've got the right type of node */
540 if (strncmp(ninfo->type, NG_ETHER_NODE_TYPE,
541 sizeof NG_ETHER_NODE_TYPE - 1)) {
542 log_Printf(LogWARN, "%s Unexpected node type ``%s'' (wanted ``"
543 NG_ETHER_NODE_TYPE "'')\n", path, ninfo->type);
544 return ether_Abandon(dev, p);
547 log_Printf(LogDEBUG, "List of netgraph node ``%s'' (id %x) hooks:\n",
550 /* look for a hook already attached. */
551 for (f = 0; f < ninfo->hooks; f++) {
552 nlink = &hlist->link[f];
554 log_Printf(LogDEBUG, " Found %s -> %s\n", nlink->ourhook,
557 if (!strcmp(nlink->ourhook, NG_ETHER_HOOK_ORPHAN) ||
558 !strcmp(nlink->ourhook, NG_ETHER_HOOK_DIVERT)) {
560 * Something is using the data coming out of this ``ether'' node.
561 * If it's a PPPoE node, we use that node, otherwise we complain that
562 * someone else is using the node.
564 if (!strcmp(nlink->nodeinfo.type, NG_PPPOE_NODE_TYPE))
565 /* Use this PPPoE node ! */
566 snprintf(ngc.path, sizeof ngc.path, "[%x]:", nlink->nodeinfo.id);
568 log_Printf(LogWARN, "%s Node type ``%s'' is currently active\n",
569 path, nlink->nodeinfo.type);
570 return ether_Abandon(dev, p);
576 if (f == ninfo->hooks) {
578 * Create a new ``PPPoE'' node connected to the ``ether'' node using
579 * the ``orphan'' and ``ethernet'' hooks
581 snprintf(mkp.type, sizeof mkp.type, "%s", NG_PPPOE_NODE_TYPE);
582 snprintf(mkp.ourhook, sizeof mkp.ourhook, "%s", NG_ETHER_HOOK_ORPHAN);
583 snprintf(mkp.peerhook, sizeof mkp.peerhook, "%s", NG_PPPOE_HOOK_ETHERNET);
584 snprintf(etherid, sizeof etherid, "[%x]:", ninfo->id);
586 log_Printf(LogDEBUG, "Creating PPPoE netgraph node %s%s -> %s\n",
587 etherid, mkp.ourhook, mkp.peerhook);
589 if (NgSendMsg(dev->cs, etherid, NGM_GENERIC_COOKIE,
590 NGM_MKPEER, &mkp, sizeof mkp) < 0) {
591 log_Printf(LogWARN, "%s Cannot create PPPoE netgraph node: %s\n",
592 etherid, strerror(errno));
593 return ether_Abandon(dev, p);
596 snprintf(ngc.path, sizeof ngc.path, "%s%s", path, NG_ETHER_HOOK_ORPHAN);
599 snprintf(dev->hook, sizeof dev->hook, "%s%d",
600 TUN_NAME, p->dl->bundle->unit);
603 * Connect the PPPoE node to our socket node.
604 * ngc.path has already been set up
606 snprintf(ngc.ourhook, sizeof ngc.ourhook, "%s", dev->hook);
607 memcpy(ngc.peerhook, ngc.ourhook, sizeof ngc.peerhook);
609 log_Printf(LogDEBUG, "Connecting netgraph socket .:%s -> %s:%s\n",
610 ngc.ourhook, ngc.path, ngc.peerhook);
611 if (NgSendMsg(dev->cs, ".:", NGM_GENERIC_COOKIE,
612 NGM_CONNECT, &ngc, sizeof ngc) < 0) {
613 log_Printf(LogWARN, "Cannot connect PPPoE and socket netgraph "
614 "nodes: %s\n", strerror(errno));
615 return ether_Abandon(dev, p);
618 /* Bring the Ethernet interface up */
619 path[ifacelen] = '\0'; /* Remove the trailing ':' */
620 if (!iface_SetFlags(path, IFF_UP))
621 log_Printf(LogWARN, "%s: Failed to set the IFF_UP flag on %s\n",
624 /* And finally, request a connection to the given provider */
626 data = (struct ngpppoe_init_data *)alloca(sizeof *data + providerlen);
627 snprintf(data->hook, sizeof data->hook, "%s", dev->hook);
628 memcpy(data->data, provider, providerlen);
629 data->data_len = providerlen;
631 snprintf(connectpath, sizeof connectpath, ".:%s", dev->hook);
632 log_Printf(LogDEBUG, "Sending PPPOE_CONNECT to %s\n", connectpath);
633 if (NgSendMsg(dev->cs, connectpath, NGM_PPPOE_COOKIE,
634 NGM_PPPOE_CONNECT, data, sizeof *data + providerlen) == -1) {
635 log_Printf(LogWARN, "``%s'': Cannot start netgraph node: %s\n",
636 connectpath, strerror(errno));
637 return ether_Abandon(dev, p);
640 /* Hook things up so that we monitor dev->cs */
641 p->desc.UpdateSet = ether_UpdateSet;
642 p->desc.IsSet = ether_IsSet;
643 p->desc.Read = ether_DescriptorRead;
645 memcpy(&dev->dev, &baseetherdevice, sizeof dev->dev);
646 switch (p->cfg.cd.necessity) {
648 dev->dev.cd.delay = p->cfg.cd.delay;
651 dev->dev.cd = p->cfg.cd;
654 log_Printf(LogWARN, "%s: Carrier must be set, using ``set cd %d!''\n",
655 p->link.name, dev->dev.cd.delay);
660 dev->timeout = dev->dev.cd.delay;
661 dev->connected = CARRIER_PENDING;
662 /* This will be overridden by our session id - if provided by netgraph */
663 dev->slot = GetIfIndex(path);
665 /* See if we're a netgraph socket */
668 if (fstat(p->fd, &st) != -1 && (st.st_mode & S_IFSOCK)) {
669 struct sockaddr_storage ssock;
670 struct sockaddr *sock = (struct sockaddr *)&ssock;
674 if (getsockname(p->fd, sock, &sz) == -1) {
675 log_Printf(LogPHASE, "%s: Link is a closed socket !\n", p->link.name);
681 if (sock->sa_family == AF_NETGRAPH) {
683 * It's a netgraph node... We can't determine hook names etc, so we
684 * stay pretty impartial....
686 log_Printf(LogPHASE, "%s: Link is a netgraph node\n", p->link.name);
688 if ((dev = malloc(sizeof *dev)) == NULL) {
689 log_Printf(LogWARN, "%s: Cannot allocate an ether device: %s\n",
690 p->link.name, strerror(errno));
694 memcpy(&dev->dev, &baseetherdevice, sizeof dev->dev);
697 dev->connected = CARRIER_OK;
701 * If we're being envoked from pppoed(8), we may have a SESSIONID
702 * set in the environment. If so, use it as the slot
704 if ((sessionid = getenv("SESSIONID")) != NULL) {
708 slot = strtoul(sessionid, &end, 16);
709 dev->slot = end != sessionid && *end == '\0' ? slot : 0;
717 physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNCNOACF);