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
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>
65 #include "throughput.h"
71 #include "descriptor.h"
80 #include "slcompress.h"
97 #define PPPOE_NODE_TYPE_LEN (sizeof NG_PPPOE_NODE_TYPE - 1) /* "PPPoE" */
100 struct device dev; /* What struct physical knows about */
101 int cs; /* Control socket */
102 int connected; /* Are we connected yet ? */
103 int timeout; /* Seconds attempting to connect */
104 char hook[sizeof TUN_NAME + 11]; /* Our socket node hook */
107 #define device2ether(d) \
108 ((d)->type == ETHER_DEVICE ? (struct etherdevice *)d : NULL)
111 ether_DeviceSize(void)
113 return sizeof(struct etherdevice);
117 ether_Write(struct physical *p, const void *v, size_t n)
119 struct etherdevice *dev = device2ether(p->handler);
121 return NgSendData(p->fd, dev->hook, v, n) == -1 ? -1 : n;
125 ether_Read(struct physical *p, void *v, size_t n)
127 char hook[sizeof TUN_NAME + 11];
129 return NgRecvData(p->fd, v, n, hook);
133 ether_RemoveFromSet(struct physical *p, fd_set *r, fd_set *w, fd_set *e)
135 struct etherdevice *dev = device2ether(p->handler);
138 if (r && dev->cs >= 0 && FD_ISSET(dev->cs, r)) {
140 log_Printf(LogTIMER, "%s: fdunset(ctrl) %d\n", p->link.name, dev->cs);
145 /* Careful... physical_RemoveFromSet() called us ! */
147 p->handler->removefromset = NULL;
148 result += physical_RemoveFromSet(p, r, w, e);
149 p->handler->removefromset = ether_RemoveFromSet;
155 ether_Free(struct physical *p)
157 struct etherdevice *dev = device2ether(p->handler);
159 physical_SetDescriptor(p);
166 ether_OpenInfo(struct physical *p)
168 struct etherdevice *dev = device2ether(p->handler);
170 switch (dev->connected) {
171 case CARRIER_PENDING:
172 return "negotiating";
174 return "established";
177 return "disconnected";
181 ether_device2iov(struct device *d, struct iovec *iov, int *niov,
182 int maxiov, int *auxfd, int *nauxfd)
184 struct etherdevice *dev = device2ether(d);
185 int sz = physical_MaxDeviceSize();
187 iov[*niov].iov_base = realloc(d, sz);
188 if (iov[*niov].iov_base == NULL) {
189 log_Printf(LogALERT, "Failed to allocate memory: %d\n", sz);
190 AbortProgram(EX_OSERR);
192 iov[*niov].iov_len = sz;
202 ether_MessageIn(struct etherdevice *dev)
204 char msgbuf[sizeof(struct ng_mesg) + sizeof(struct ngpppoe_sts)];
205 struct ng_mesg *rep = (struct ng_mesg *)msgbuf;
206 struct ngpppoe_sts *sts = (struct ngpppoe_sts *)(msgbuf + sizeof *rep);
216 if ((r = mkfdset()) == NULL) {
217 log_Printf(LogERROR, "DoLoop: Cannot create fd_set\n");
222 t.tv_sec = t.tv_usec = 0;
223 ret = select(dev->cs + 1, r, NULL, NULL, &t);
229 if (NgRecvMsg(dev->cs, rep, sizeof msgbuf, NULL) <= 0)
232 if (rep->header.version != NG_VERSION) {
233 log_Printf(LogWARN, "%ld: Unexpected netgraph version, expected %ld\n",
234 (long)rep->header.version, (long)NG_VERSION);
238 if (rep->header.typecookie != NGM_PPPOE_COOKIE) {
239 log_Printf(LogWARN, "%ld: Unexpected netgraph cookie, expected %ld\n",
240 (long)rep->header.typecookie, (long)NGM_PPPOE_COOKIE);
244 switch (rep->header.cmd) {
245 case NGM_PPPOE_SET_FLAG: msg = "SET_FLAG"; break;
246 case NGM_PPPOE_CONNECT: msg = "CONNECT"; break;
247 case NGM_PPPOE_LISTEN: msg = "LISTEN"; break;
248 case NGM_PPPOE_OFFER: msg = "OFFER"; break;
249 case NGM_PPPOE_SUCCESS: msg = "SUCCESS"; break;
250 case NGM_PPPOE_FAIL: msg = "FAIL"; break;
251 case NGM_PPPOE_CLOSE: msg = "CLOSE"; break;
252 case NGM_PPPOE_GET_STATUS: msg = "GET_STATUS"; break;
253 case NGM_PPPOE_ACNAME:
255 if (setenv("ACNAME", sts->hook, 1) != 0)
256 log_Printf(LogWARN, "setenv: cannot set ACNAME=%s: %m", sts->hook);
259 snprintf(unknown, sizeof unknown, "<%d>", (int)rep->header.cmd);
264 log_Printf(LogPHASE, "Received NGM_PPPOE_%s (hook \"%s\")\n", msg, sts->hook);
266 switch (rep->header.cmd) {
267 case NGM_PPPOE_SUCCESS:
268 dev->connected = CARRIER_OK;
271 case NGM_PPPOE_CLOSE:
272 dev->connected = CARRIER_LOST;
278 ether_AwaitCarrier(struct physical *p)
280 struct etherdevice *dev = device2ether(p->handler);
282 if (dev->connected != CARRIER_OK && !dev->timeout--)
283 dev->connected = CARRIER_LOST;
284 else if (dev->connected == CARRIER_PENDING)
285 ether_MessageIn(dev);
287 return dev->connected;
290 static const struct device baseetherdevice = {
294 { CD_REQUIRED, DEF_ETHERCDDELAY },
311 ether_iov2device(int type, struct physical *p, struct iovec *iov, int *niov,
312 int maxiov, int *auxfd, int *nauxfd)
314 if (type == ETHER_DEVICE) {
315 struct etherdevice *dev = (struct etherdevice *)iov[(*niov)++].iov_base;
317 dev = realloc(dev, sizeof *dev); /* Reduce to the correct size */
319 log_Printf(LogALERT, "Failed to allocate memory: %d\n",
321 AbortProgram(EX_OSERR);
330 /* Refresh function pointers etc */
331 memcpy(&dev->dev, &baseetherdevice, sizeof dev->dev);
333 physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNCNOACF);
341 ether_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
343 struct physical *p = descriptor2physical(d);
344 struct etherdevice *dev = device2ether(p->handler);
347 if (r && dev->cs >= 0) {
349 log_Printf(LogTIMER, "%s(ctrl): fdset(r) %d\n", p->link.name, dev->cs);
354 result += physical_doUpdateSet(d, r, w, e, n, 0);
360 ether_IsSet(struct fdescriptor *d, const fd_set *fdset)
362 struct physical *p = descriptor2physical(d);
363 struct etherdevice *dev = device2ether(p->handler);
366 result = dev->cs >= 0 && FD_ISSET(dev->cs, fdset);
367 result += physical_IsSet(d, fdset);
373 ether_DescriptorRead(struct fdescriptor *d, struct bundle *bundle,
376 struct physical *p = descriptor2physical(d);
377 struct etherdevice *dev = device2ether(p->handler);
379 if (dev->cs >= 0 && FD_ISSET(dev->cs, fdset)) {
380 ether_MessageIn(dev);
381 if (dev->connected == CARRIER_LOST) {
382 log_Printf(LogPHASE, "%s: Device disconnected\n", p->link.name);
383 datalink_Down(p->dl, CLOSE_NORMAL);
388 if (physical_IsSet(d, fdset))
389 physical_DescriptorRead(d, bundle, fdset);
392 static struct device *
393 ether_Abandon(struct etherdevice *dev, struct physical *p)
395 /* Abandon our node construction */
398 p->fd = -2; /* Nobody else need try.. */
405 ether_Create(struct physical *p)
408 struct etherdevice *dev;
409 struct ng_mesg *resp;
410 const struct hooklist *hlist;
411 const struct nodeinfo *ninfo;
418 if (p->fd < 0 && !strncasecmp(p->name.full, NG_PPPOE_NODE_TYPE,
419 PPPOE_NODE_TYPE_LEN) &&
420 p->name.full[PPPOE_NODE_TYPE_LEN] == ':') {
421 const struct linkinfo *nlink;
422 struct ngpppoe_init_data *data;
423 struct ngm_mkpeer mkp;
424 struct ngm_connect ngc;
425 const char *iface, *provider;
428 char connectpath[sizeof dev->hook + 2]; /* .:<hook> */
430 p->fd--; /* We own the device - change fd */
432 loadmodules(LOAD_VERBOSLY, "netgraph", "ng_ether", "ng_pppoe", "ng_socket",
435 if ((dev = malloc(sizeof *dev)) == NULL)
438 iface = p->name.full + PPPOE_NODE_TYPE_LEN + 1;
440 provider = strchr(iface, ':');
442 ifacelen = provider - iface;
444 providerlen = strlen(provider);
446 ifacelen = strlen(iface);
452 * We're going to do this (where tunN is our tunnel device):
456 * | <iface> | dev->cs
462 * .---------. .-----------.
463 * | pppoe | | socket |
464 * | <iface> |(tunN)<---->(tunN)| <unnamed> |
465 * `--------- `-----------'
472 /* Create a socket node */
473 if (ID0NgMkSockNode(NULL, &dev->cs, &p->fd) == -1) {
474 log_Printf(LogWARN, "Cannot create netgraph socket node: %s\n",
482 * Ask for a list of hooks attached to the "ether" node. This node should
483 * magically exist as a way of hooking stuff onto an ethernet device
485 path = (char *)alloca(ifacelen + 2);
486 sprintf(path, "%.*s:", ifacelen, iface);
487 if (NgSendMsg(dev->cs, path, NGM_GENERIC_COOKIE, NGM_LISTHOOKS,
489 log_Printf(LogWARN, "%s Cannot send a netgraph message: %s\n",
490 path, strerror(errno));
491 return ether_Abandon(dev, p);
494 /* Get our list back */
495 resp = (struct ng_mesg *)rbuf;
496 if (NgRecvMsg(dev->cs, resp, sizeof rbuf, NULL) <= 0) {
497 log_Printf(LogWARN, "Cannot get netgraph response: %s\n",
499 return ether_Abandon(dev, p);
502 hlist = (const struct hooklist *)resp->data;
503 ninfo = &hlist->nodeinfo;
505 /* Make sure we've got the right type of node */
506 if (strncmp(ninfo->type, NG_ETHER_NODE_TYPE,
507 sizeof NG_ETHER_NODE_TYPE - 1)) {
508 log_Printf(LogWARN, "%s Unexpected node type ``%s'' (wanted ``"
509 NG_ETHER_NODE_TYPE "'')\n", path, ninfo->type);
510 return ether_Abandon(dev, p);
513 log_Printf(LogDEBUG, "List of netgraph node ``%s'' (id %x) hooks:\n",
516 /* look for a hook already attached. */
517 for (f = 0; f < ninfo->hooks; f++) {
518 nlink = &hlist->link[f];
520 log_Printf(LogDEBUG, " Found %s -> %s\n", nlink->ourhook,
523 if (!strcmp(nlink->ourhook, NG_ETHER_HOOK_ORPHAN) ||
524 !strcmp(nlink->ourhook, NG_ETHER_HOOK_DIVERT)) {
526 * Something is using the data coming out of this ``ether'' node.
527 * If it's a PPPoE node, we use that node, otherwise we complain that
528 * someone else is using the node.
530 if (!strcmp(nlink->nodeinfo.type, NG_PPPOE_NODE_TYPE))
531 /* Use this PPPoE node ! */
532 snprintf(ngc.path, sizeof ngc.path, "[%x]:", nlink->nodeinfo.id);
534 log_Printf(LogWARN, "%s Node type ``%s'' is currently active\n",
535 path, nlink->nodeinfo.type);
536 return ether_Abandon(dev, p);
542 if (f == ninfo->hooks) {
544 * Create a new ``PPPoE'' node connected to the ``ether'' node using
545 * the ``orphan'' and ``ethernet'' hooks
547 snprintf(mkp.type, sizeof mkp.type, "%s", NG_PPPOE_NODE_TYPE);
548 snprintf(mkp.ourhook, sizeof mkp.ourhook, "%s", NG_ETHER_HOOK_ORPHAN);
549 snprintf(mkp.peerhook, sizeof mkp.peerhook, "%s", NG_PPPOE_HOOK_ETHERNET);
550 snprintf(etherid, sizeof etherid, "[%x]:", ninfo->id);
552 log_Printf(LogDEBUG, "Creating PPPoE netgraph node %s%s -> %s\n",
553 etherid, mkp.ourhook, mkp.peerhook);
555 if (NgSendMsg(dev->cs, etherid, NGM_GENERIC_COOKIE,
556 NGM_MKPEER, &mkp, sizeof mkp) < 0) {
557 log_Printf(LogWARN, "%s Cannot create PPPoE netgraph node: %s\n",
558 etherid, strerror(errno));
559 return ether_Abandon(dev, p);
562 snprintf(ngc.path, sizeof ngc.path, "%s%s", path, NG_ETHER_HOOK_ORPHAN);
565 snprintf(dev->hook, sizeof dev->hook, "%s%d",
566 TUN_NAME, p->dl->bundle->unit);
569 * Connect the PPPoE node to our socket node.
570 * ngc.path has already been set up
572 snprintf(ngc.ourhook, sizeof ngc.ourhook, "%s", dev->hook);
573 memcpy(ngc.peerhook, ngc.ourhook, sizeof ngc.peerhook);
575 log_Printf(LogDEBUG, "Connecting netgraph socket .:%s -> %s:%s\n",
576 ngc.ourhook, ngc.path, ngc.peerhook);
577 if (NgSendMsg(dev->cs, ".:", NGM_GENERIC_COOKIE,
578 NGM_CONNECT, &ngc, sizeof ngc) < 0) {
579 log_Printf(LogWARN, "Cannot connect PPPoE and socket netgraph "
580 "nodes: %s\n", strerror(errno));
581 return ether_Abandon(dev, p);
584 /* Bring the Ethernet interface up */
585 path[ifacelen] = '\0'; /* Remove the trailing ':' */
586 if (!iface_SetFlags(path, IFF_UP))
587 log_Printf(LogWARN, "%s: Failed to set the IFF_UP flag on %s\n",
590 /* And finally, request a connection to the given provider */
592 data = (struct ngpppoe_init_data *)alloca(sizeof *data + providerlen);
593 snprintf(data->hook, sizeof data->hook, "%s", dev->hook);
594 memcpy(data->data, provider, providerlen);
595 data->data_len = providerlen;
597 snprintf(connectpath, sizeof connectpath, ".:%s", dev->hook);
598 log_Printf(LogDEBUG, "Sending PPPOE_CONNECT to %s\n", connectpath);
599 if (NgSendMsg(dev->cs, connectpath, NGM_PPPOE_COOKIE,
600 NGM_PPPOE_CONNECT, data, sizeof *data + providerlen) == -1) {
601 log_Printf(LogWARN, "``%s'': Cannot start netgraph node: %s\n",
602 connectpath, strerror(errno));
603 return ether_Abandon(dev, p);
606 /* Hook things up so that we monitor dev->cs */
607 p->desc.UpdateSet = ether_UpdateSet;
608 p->desc.IsSet = ether_IsSet;
609 p->desc.Read = ether_DescriptorRead;
611 memcpy(&dev->dev, &baseetherdevice, sizeof dev->dev);
612 switch (p->cfg.cd.necessity) {
614 dev->dev.cd.delay = p->cfg.cd.delay;
617 dev->dev.cd = p->cfg.cd;
620 log_Printf(LogWARN, "%s: Carrier must be set, using ``set cd %d!''\n",
621 p->link.name, dev->dev.cd.delay);
626 dev->timeout = dev->dev.cd.delay;
627 dev->connected = CARRIER_PENDING;
630 /* See if we're a netgraph socket */
633 if (fstat(p->fd, &st) != -1 && (st.st_mode & S_IFSOCK)) {
634 struct sockaddr_storage ssock;
635 struct sockaddr *sock = (struct sockaddr *)&ssock;
639 if (getsockname(p->fd, sock, &sz) == -1) {
640 log_Printf(LogPHASE, "%s: Link is a closed socket !\n", p->link.name);
646 if (sock->sa_family == AF_NETGRAPH) {
648 * It's a netgraph node... We can't determine hook names etc, so we
649 * stay pretty impartial....
651 log_Printf(LogPHASE, "%s: Link is a netgraph node\n", p->link.name);
653 if ((dev = malloc(sizeof *dev)) == NULL) {
654 log_Printf(LogWARN, "%s: Cannot allocate an ether device: %s\n",
655 p->link.name, strerror(errno));
659 memcpy(&dev->dev, &baseetherdevice, sizeof dev->dev);
662 dev->connected = CARRIER_OK;
669 physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNCNOACF);