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>
37 #include <netinet/in_systm.h>
38 #include <netinet/ip.h>
39 #include <netgraph/ng_ether.h>
40 #include <netgraph/ng_message.h>
41 #include <netgraph/ng_pppoe.h>
42 #include <netgraph/ng_socket.h>
49 #include <sys/fcntl.h>
50 #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
51 #include <sys/linker.h>
52 #include <sys/module.h>
66 #include "throughput.h"
72 #include "descriptor.h"
81 #include "slcompress.h"
93 #define PPPOE_NODE_TYPE_LEN (sizeof NG_PPPOE_NODE_TYPE - 1) /* "PPPoE" */
96 struct device dev; /* What struct physical knows about */
97 int cs; /* Control socket */
98 int connected; /* Are we connected yet ? */
99 int timeout; /* Seconds attempting to connect */
100 char hook[sizeof TUN_NAME + 11]; /* Our socket node hook */
103 #define device2ether(d) \
104 ((d)->type == ETHER_DEVICE ? (struct etherdevice *)d : NULL)
107 ether_DeviceSize(void)
109 return sizeof(struct etherdevice);
113 ether_Write(struct physical *p, const void *v, size_t n)
115 struct etherdevice *dev = device2ether(p->handler);
117 return NgSendData(p->fd, dev->hook, v, n) == -1 ? -1 : n;
121 ether_Read(struct physical *p, void *v, size_t n)
123 char hook[sizeof TUN_NAME + 11];
125 return NgRecvData(p->fd, v, n, hook);
129 ether_RemoveFromSet(struct physical *p, fd_set *r, fd_set *w, fd_set *e)
131 struct etherdevice *dev = device2ether(p->handler);
134 if (r && dev->cs >= 0 && FD_ISSET(dev->cs, r)) {
136 log_Printf(LogTIMER, "%s: fdunset(ctrl) %d\n", p->link.name, dev->cs);
141 /* Careful... physical_RemoveFromSet() called us ! */
143 p->handler->removefromset = NULL;
144 result += physical_RemoveFromSet(p, r, w, e);
145 p->handler->removefromset = ether_RemoveFromSet;
151 ether_Free(struct physical *p)
153 struct etherdevice *dev = device2ether(p->handler);
155 physical_SetDescriptor(p);
162 ether_OpenInfo(struct physical *p)
164 struct etherdevice *dev = device2ether(p->handler);
166 switch (dev->connected) {
167 case CARRIER_PENDING:
168 return "negotiating";
170 return "established";
173 return "disconnected";
177 ether_device2iov(struct device *d, struct iovec *iov, int *niov,
178 int maxiov, int *auxfd, int *nauxfd)
180 struct etherdevice *dev = device2ether(d);
181 int sz = physical_MaxDeviceSize();
183 iov[*niov].iov_base = realloc(d, sz);
184 if (iov[*niov].iov_base == NULL) {
185 log_Printf(LogALERT, "Failed to allocate memory: %d\n", sz);
186 AbortProgram(EX_OSERR);
188 iov[*niov].iov_len = sz;
198 ether_MessageIn(struct etherdevice *dev)
200 char msgbuf[sizeof(struct ng_mesg) + sizeof(struct ngpppoe_sts)];
201 struct ng_mesg *rep = (struct ng_mesg *)msgbuf;
202 struct ngpppoe_sts *sts = (struct ngpppoe_sts *)(msgbuf + sizeof *rep);
213 t.tv_sec = t.tv_usec = 0;
214 if (select(dev->cs + 1, &r, NULL, NULL, &t) <= 0)
217 if (NgRecvMsg(dev->cs, rep, sizeof msgbuf, NULL) < 0)
220 if (rep->header.version != NG_VERSION) {
221 log_Printf(LogWARN, "%ld: Unexpected netgraph version, expected %ld\n",
222 (long)rep->header.version, (long)NG_VERSION);
226 if (rep->header.typecookie != NGM_PPPOE_COOKIE) {
227 log_Printf(LogWARN, "%ld: Unexpected netgraph cookie, expected %ld\n",
228 (long)rep->header.typecookie, (long)NGM_PPPOE_COOKIE);
232 switch (rep->header.cmd) {
233 case NGM_PPPOE_SET_FLAG: msg = "SET_FLAG"; break;
234 case NGM_PPPOE_CONNECT: msg = "CONNECT"; break;
235 case NGM_PPPOE_LISTEN: msg = "LISTEN"; break;
236 case NGM_PPPOE_OFFER: msg = "OFFER"; break;
237 case NGM_PPPOE_SUCCESS: msg = "SUCCESS"; break;
238 case NGM_PPPOE_FAIL: msg = "FAIL"; break;
239 case NGM_PPPOE_CLOSE: msg = "CLOSE"; break;
240 case NGM_PPPOE_GET_STATUS: msg = "GET_STATUS"; break;
242 snprintf(unknown, sizeof unknown, "<%d>", (int)rep->header.cmd);
247 log_Printf(LogPHASE, "Received NGM_PPPOE_%s (hook \"%s\")\n", msg, sts->hook);
249 switch (rep->header.cmd) {
250 case NGM_PPPOE_SUCCESS:
251 dev->connected = CARRIER_OK;
254 case NGM_PPPOE_CLOSE:
255 dev->connected = CARRIER_LOST;
261 ether_AwaitCarrier(struct physical *p)
263 struct etherdevice *dev = device2ether(p->handler);
265 if (dev->connected != CARRIER_OK && !dev->timeout--)
266 dev->connected = CARRIER_LOST;
267 else if (dev->connected == CARRIER_PENDING)
268 ether_MessageIn(dev);
270 return dev->connected;
273 static const struct device baseetherdevice = {
276 { CD_REQUIRED, DEF_ETHERCDDELAY },
292 ether_iov2device(int type, struct physical *p, struct iovec *iov, int *niov,
293 int maxiov, int *auxfd, int *nauxfd)
295 if (type == ETHER_DEVICE) {
296 struct etherdevice *dev = (struct etherdevice *)iov[(*niov)++].iov_base;
298 dev = realloc(dev, sizeof *dev); /* Reduce to the correct size */
300 log_Printf(LogALERT, "Failed to allocate memory: %d\n",
302 AbortProgram(EX_OSERR);
311 /* Refresh function pointers etc */
312 memcpy(&dev->dev, &baseetherdevice, sizeof dev->dev);
314 physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNCNOACF);
322 ether_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
324 struct physical *p = descriptor2physical(d);
325 struct etherdevice *dev = device2ether(p->handler);
328 if (r && dev->cs >= 0) {
330 log_Printf(LogTIMER, "%s(ctrl): fdset(r) %d\n", p->link.name, dev->cs);
335 result += physical_doUpdateSet(d, r, w, e, n, 0);
341 ether_IsSet(struct fdescriptor *d, const fd_set *fdset)
343 struct physical *p = descriptor2physical(d);
344 struct etherdevice *dev = device2ether(p->handler);
347 result = dev->cs >= 0 && FD_ISSET(dev->cs, fdset);
348 result += physical_IsSet(d, fdset);
354 ether_DescriptorRead(struct fdescriptor *d, struct bundle *bundle,
357 struct physical *p = descriptor2physical(d);
358 struct etherdevice *dev = device2ether(p->handler);
360 if (dev->cs >= 0 && FD_ISSET(dev->cs, fdset)) {
361 ether_MessageIn(dev);
362 if (dev->connected == CARRIER_LOST) {
363 log_Printf(LogPHASE, "%s: Device disconnected\n", p->link.name);
364 datalink_Down(p->dl, CLOSE_NORMAL);
369 if (physical_IsSet(d, fdset))
370 physical_DescriptorRead(d, bundle, fdset);
373 static struct device *
374 ether_Abandon(struct etherdevice *dev, struct physical *p)
376 /* Abandon our node construction */
379 p->fd = -2; /* Nobody else need try.. */
386 ether_Create(struct physical *p)
389 struct etherdevice *dev;
390 struct ng_mesg *resp;
391 const struct hooklist *hlist;
392 const struct nodeinfo *ninfo;
396 if (p->fd < 0 && !strncasecmp(p->name.full, NG_PPPOE_NODE_TYPE,
397 PPPOE_NODE_TYPE_LEN) &&
398 p->name.full[PPPOE_NODE_TYPE_LEN] == ':') {
399 const struct linkinfo *nlink;
400 struct ngpppoe_init_data *data;
401 struct ngm_mkpeer mkp;
402 struct ngm_connect ngc;
403 const char *iface, *provider;
404 char *path, etherid[12];
405 int ifacelen, providerlen;
406 char connectpath[sizeof dev->hook + 2]; /* .:<hook> */
408 p->fd--; /* We own the device - change fd */
410 #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
411 if (modfind("netgraph") == -1) {
412 log_Printf(LogWARN, "Netgraph is not built into the kernel\n");
416 if (modfind("ng_socket") == -1 &&
417 ID0kldload("ng_socket") == -1) {
418 log_Printf(LogWARN, "kldload: ng_socket: %s\n", strerror(errno));
423 if ((dev = malloc(sizeof *dev)) == NULL)
426 iface = p->name.full + PPPOE_NODE_TYPE_LEN + 1;
428 provider = strchr(iface, ':');
430 ifacelen = provider - iface;
432 providerlen = strlen(provider);
434 ifacelen = strlen(iface);
440 * We're going to do this (where tunN is our tunnel device):
444 * | <iface> | dev->cs
450 * .---------. .-----------.
451 * | pppoe | | socket |
452 * | <iface> |(tunN)<---->(tunN)| <unnamed> |
453 * `--------- `-----------'
460 /* Create a socket node */
461 if (ID0NgMkSockNode(NULL, &dev->cs, &p->fd) == -1) {
462 log_Printf(LogWARN, "Cannot create netgraph socket node: %s\n",
469 * Ask for a list of hooks attached to the "ether" node. This node should
470 * magically exist as a way of hooking stuff onto an ethernet device
472 path = (char *)alloca(ifacelen + 2);
473 sprintf(path, "%.*s:", ifacelen, iface);
474 if (NgSendMsg(dev->cs, path, NGM_GENERIC_COOKIE, NGM_LISTHOOKS,
476 log_Printf(LogWARN, "%s Cannot send a netgraph message: %s\n",
477 path, strerror(errno));
478 return ether_Abandon(dev, p);
481 /* Get our list back */
482 resp = (struct ng_mesg *)rbuf;
483 if (NgRecvMsg(dev->cs, resp, sizeof rbuf, NULL) < 0) {
484 log_Printf(LogWARN, "Cannot get netgraph response: %s\n",
486 return ether_Abandon(dev, p);
489 hlist = (const struct hooklist *)resp->data;
490 ninfo = &hlist->nodeinfo;
492 /* Make sure we've got the right type of node */
493 if (strncmp(ninfo->type, NG_ETHER_NODE_TYPE,
494 sizeof NG_ETHER_NODE_TYPE - 1)) {
495 log_Printf(LogWARN, "%s Unexpected node type ``%s'' (wanted ``"
496 NG_ETHER_NODE_TYPE "'')\n", path, ninfo->type);
497 return ether_Abandon(dev, p);
500 log_Printf(LogDEBUG, "List of netgraph node ``%s'' (id %x) hooks:\n",
503 /* look for a hook already attached. */
504 for (f = 0; f < ninfo->hooks; f++) {
505 nlink = &hlist->link[f];
507 log_Printf(LogDEBUG, " Found %s -> %s\n", nlink->ourhook,
510 if (!strcmp(nlink->ourhook, NG_ETHER_HOOK_ORPHAN) ||
511 !strcmp(nlink->ourhook, NG_ETHER_HOOK_DIVERT)) {
513 * Something is using the data coming out of this ``ether'' node.
514 * If it's a PPPoE node, we use that node, otherwise we complain that
515 * someone else is using the node.
517 if (!strcmp(nlink->nodeinfo.type, NG_PPPOE_NODE_TYPE))
518 /* Use this PPPoE node ! */
519 snprintf(ngc.path, sizeof ngc.path, "[%x]:", nlink->nodeinfo.id);
521 log_Printf(LogWARN, "%s Node type ``%s'' is currently active\n",
522 path, nlink->nodeinfo.type);
523 return ether_Abandon(dev, p);
529 if (f == ninfo->hooks) {
531 * Create a new ``PPPoE'' node connected to the ``ether'' node using
532 * the magic ``orphan'' and ``ethernet'' hooks
534 snprintf(mkp.type, sizeof mkp.type, "%s", NG_PPPOE_NODE_TYPE);
535 snprintf(mkp.ourhook, sizeof mkp.ourhook, "%s", NG_ETHER_HOOK_ORPHAN);
536 snprintf(mkp.peerhook, sizeof mkp.peerhook, "%s", NG_PPPOE_HOOK_ETHERNET);
537 snprintf(etherid, sizeof etherid, "[%x]:", ninfo->id);
539 log_Printf(LogDEBUG, "Creating PPPoE netgraph node %s%s -> %s\n",
540 etherid, mkp.ourhook, mkp.peerhook);
542 if (NgSendMsg(dev->cs, etherid, NGM_GENERIC_COOKIE,
543 NGM_MKPEER, &mkp, sizeof mkp) < 0) {
544 log_Printf(LogWARN, "%s Cannot create PPPoE netgraph node: %s\n",
545 etherid, strerror(errno));
546 return ether_Abandon(dev, p);
549 snprintf(ngc.path, sizeof ngc.path, "%s%s", path, NG_ETHER_HOOK_ORPHAN);
552 snprintf(dev->hook, sizeof dev->hook, "%s%d",
553 TUN_NAME, p->dl->bundle->unit);
556 * Connect the PPPoE node to our socket node.
557 * ngc.path has already been set up
559 snprintf(ngc.ourhook, sizeof ngc.ourhook, "%s", dev->hook);
560 memcpy(ngc.peerhook, ngc.ourhook, sizeof ngc.peerhook);
562 log_Printf(LogDEBUG, "Connecting netgraph socket .:%s -> %s:%s\n",
563 ngc.ourhook, ngc.path, ngc.peerhook);
564 if (NgSendMsg(dev->cs, ".:", NGM_GENERIC_COOKIE,
565 NGM_CONNECT, &ngc, sizeof ngc) < 0) {
566 log_Printf(LogWARN, "Cannot connect PPPoE and socket netgraph "
567 "nodes: %s\n", strerror(errno));
568 return ether_Abandon(dev, p);
571 /* And finally, request a connection to the given provider */
573 data = (struct ngpppoe_init_data *)alloca(sizeof *data + providerlen + 1);
575 snprintf(data->hook, sizeof data->hook, "%s", dev->hook);
576 strcpy(data->data, provider);
577 data->data_len = providerlen;
579 snprintf(connectpath, sizeof connectpath, ".:%s", dev->hook);
580 log_Printf(LogDEBUG, "Sending PPPOE_CONNECT to %s\n", connectpath);
581 if (NgSendMsg(dev->cs, connectpath, NGM_PPPOE_COOKIE,
582 NGM_PPPOE_CONNECT, data, sizeof *data + providerlen) == -1) {
583 log_Printf(LogWARN, "``%s'': Cannot start netgraph node: %s\n",
584 connectpath, strerror(errno));
585 return ether_Abandon(dev, p);
588 /* Hook things up so that we monitor dev->cs */
589 p->desc.UpdateSet = ether_UpdateSet;
590 p->desc.IsSet = ether_IsSet;
591 p->desc.Read = ether_DescriptorRead;
593 memcpy(&dev->dev, &baseetherdevice, sizeof dev->dev);
594 switch (p->cfg.cd.necessity) {
596 dev->dev.cd.delay = p->cfg.cd.delay;
599 dev->dev.cd = p->cfg.cd;
602 log_Printf(LogWARN, "%s: Carrier must be set, using ``set cd %d!''\n",
603 p->link.name, dev->dev.cd.delay);
608 dev->timeout = dev->dev.cd.delay;
609 dev->connected = CARRIER_PENDING;
612 /* See if we're a netgraph socket */
613 struct sockaddr_ng ngsock;
614 struct sockaddr *sock = (struct sockaddr *)&ngsock;
618 if (getsockname(p->fd, sock, &sz) != -1 && sock->sa_family == AF_NETGRAPH) {
620 * It's a netgraph node... We can't determine hook names etc, so we
621 * stay pretty impartial....
623 log_Printf(LogPHASE, "%s: Link is a netgraph node\n", p->link.name);
625 if ((dev = malloc(sizeof *dev)) == NULL) {
626 log_Printf(LogWARN, "%s: Cannot allocate an ether device: %s\n",
627 p->link.name, strerror(errno));
631 memcpy(&dev->dev, &baseetherdevice, sizeof dev->dev);
634 dev->connected = CARRIER_OK;
640 physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNCNOACF);
642 /* Moan about (and fix) invalid LCP configurations */
643 if (p->link.lcp.cfg.mru > 1492) {
644 log_Printf(LogWARN, "%s: Reducing MRU to 1492\n", p->link.name);
645 p->link.lcp.cfg.mru = 1492;
647 if (p->dl->bundle->cfg.mtu > 1492) {
648 log_Printf(LogWARN, "%s: Reducing MTU to 1492\n", p->link.name);
649 p->dl->bundle->cfg.mtu = 1492;