ether.c revision 62209
152942Sbrian/*-
252942Sbrian * Copyright (c) 1999 Brian Somers <brian@Awfulhak.org>
352942Sbrian * All rights reserved.
452942Sbrian *
552942Sbrian * Redistribution and use in source and binary forms, with or without
652942Sbrian * modification, are permitted provided that the following conditions
752942Sbrian * are met:
852942Sbrian * 1. Redistributions of source code must retain the above copyright
952942Sbrian *    notice, this list of conditions and the following disclaimer.
1052942Sbrian * 2. Redistributions in binary form must reproduce the above copyright
1152942Sbrian *    notice, this list of conditions and the following disclaimer in the
1252942Sbrian *    documentation and/or other materials provided with the distribution.
1352942Sbrian *
1452942Sbrian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1552942Sbrian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1652942Sbrian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1752942Sbrian * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1852942Sbrian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1952942Sbrian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2052942Sbrian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2152942Sbrian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2252942Sbrian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2352942Sbrian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2452942Sbrian * SUCH DAMAGE.
2552942Sbrian *
2652942Sbrian * $FreeBSD: head/usr.sbin/ppp/ether.c 62209 2000-06-28 09:30:30Z brian $
2752942Sbrian */
2852942Sbrian
2952942Sbrian#include <sys/param.h>
3052942Sbrian#include <sys/socket.h>
3152942Sbrian#include <sys/un.h>
3252942Sbrian#include <netinet/in.h>
3352942Sbrian#include <arpa/inet.h>
3452942Sbrian#include <netdb.h>
3552942Sbrian#include <netgraph.h>
3652942Sbrian#include <net/ethernet.h>
3752942Sbrian#include <netinet/in_systm.h>
3852942Sbrian#include <netinet/ip.h>
3952942Sbrian#include <netgraph/ng_ether.h>
4052942Sbrian#include <netgraph/ng_message.h>
4152942Sbrian#include <netgraph/ng_pppoe.h>
4252942Sbrian#include <netgraph/ng_socket.h>
4352942Sbrian
4452942Sbrian#include <errno.h>
4552942Sbrian#include <stdio.h>
4652942Sbrian#include <stdlib.h>
4752942Sbrian#include <string.h>
4852942Sbrian#include <sysexits.h>
4952942Sbrian#include <sys/fcntl.h>
5052942Sbrian#if defined(__FreeBSD__) && !defined(NOKLDLOAD)
5152942Sbrian#include <sys/linker.h>
5253241Sbrian#include <sys/module.h>
5352942Sbrian#endif
5452942Sbrian#include <sys/uio.h>
5552942Sbrian#include <termios.h>
5652942Sbrian#include <sys/time.h>
5752942Sbrian#include <unistd.h>
5852942Sbrian
5952942Sbrian#include "layer.h"
6052942Sbrian#include "defs.h"
6152942Sbrian#include "mbuf.h"
6252942Sbrian#include "log.h"
6352942Sbrian#include "timer.h"
6452942Sbrian#include "lqr.h"
6552942Sbrian#include "hdlc.h"
6652942Sbrian#include "throughput.h"
6752942Sbrian#include "fsm.h"
6852942Sbrian#include "lcp.h"
6952942Sbrian#include "ccp.h"
7052942Sbrian#include "link.h"
7152942Sbrian#include "async.h"
7252942Sbrian#include "descriptor.h"
7352942Sbrian#include "physical.h"
7452942Sbrian#include "main.h"
7552942Sbrian#include "mp.h"
7652942Sbrian#include "chat.h"
7752942Sbrian#include "auth.h"
7852942Sbrian#include "chap.h"
7952942Sbrian#include "cbcp.h"
8052942Sbrian#include "datalink.h"
8152942Sbrian#include "slcompress.h"
8252942Sbrian#include "iplist.h"
8352942Sbrian#include "ipcp.h"
8452942Sbrian#include "filter.h"
8552942Sbrian#ifndef NORADIUS
8652942Sbrian#include "radius.h"
8752942Sbrian#endif
8852942Sbrian#include "bundle.h"
8952942Sbrian#include "id.h"
9052942Sbrian#include "ether.h"
9152942Sbrian
9252942Sbrian
9352942Sbrian#define PPPOE_NODE_TYPE_LEN (sizeof NG_PPPOE_NODE_TYPE - 1) /* "PPPoE" */
9452942Sbrian
9552942Sbrianstruct etherdevice {
9652942Sbrian  struct device dev;			/* What struct physical knows about */
9752942Sbrian  int cs;				/* Control socket */
9852942Sbrian  int connected;			/* Are we connected yet ? */
9952942Sbrian  int timeout;				/* Seconds attempting to connect */
10052942Sbrian  char hook[sizeof TUN_NAME + 11];	/* Our socket node hook */
10152942Sbrian};
10252942Sbrian
10352942Sbrian#define device2ether(d) \
10452942Sbrian  ((d)->type == ETHER_DEVICE ? (struct etherdevice *)d : NULL)
10552942Sbrian
10652942Sbrianint
10752942Sbrianether_DeviceSize(void)
10852942Sbrian{
10952942Sbrian  return sizeof(struct etherdevice);
11052942Sbrian}
11152942Sbrian
11252942Sbrianstatic ssize_t
11352942Sbrianether_Write(struct physical *p, const void *v, size_t n)
11452942Sbrian{
11552942Sbrian  struct etherdevice *dev = device2ether(p->handler);
11652942Sbrian
11752942Sbrian  return NgSendData(p->fd, dev->hook, v, n) == -1 ? -1 : n;
11852942Sbrian}
11952942Sbrian
12052942Sbrianstatic ssize_t
12152942Sbrianether_Read(struct physical *p, void *v, size_t n)
12252942Sbrian{
12352942Sbrian  char hook[sizeof TUN_NAME + 11];
12452942Sbrian
12552942Sbrian  return NgRecvData(p->fd, v, n, hook);
12652942Sbrian}
12752942Sbrian
12852942Sbrianstatic int
12952942Sbrianether_RemoveFromSet(struct physical *p, fd_set *r, fd_set *w, fd_set *e)
13052942Sbrian{
13152942Sbrian  struct etherdevice *dev = device2ether(p->handler);
13252942Sbrian  int result;
13352942Sbrian
13452942Sbrian  if (r && dev->cs >= 0 && FD_ISSET(dev->cs, r)) {
13552942Sbrian    FD_CLR(dev->cs, r);
13652942Sbrian    log_Printf(LogTIMER, "%s: fdunset(ctrl) %d\n", p->link.name, dev->cs);
13752942Sbrian    result = 1;
13852942Sbrian  } else
13952942Sbrian    result = 0;
14052942Sbrian
14152942Sbrian  /* Careful... physical_RemoveFromSet() called us ! */
14252942Sbrian
14352942Sbrian  p->handler->removefromset = NULL;
14452942Sbrian  result += physical_RemoveFromSet(p, r, w, e);
14552942Sbrian  p->handler->removefromset = ether_RemoveFromSet;
14652942Sbrian
14752942Sbrian  return result;
14852942Sbrian}
14952942Sbrian
15052942Sbrianstatic void
15152942Sbrianether_Free(struct physical *p)
15252942Sbrian{
15352942Sbrian  struct etherdevice *dev = device2ether(p->handler);
15452942Sbrian
15552942Sbrian  physical_SetDescriptor(p);
15652942Sbrian  if (dev->cs != -1)
15752942Sbrian    close(dev->cs);
15852942Sbrian  free(dev);
15952942Sbrian}
16052942Sbrian
16152942Sbrianstatic const char *
16252942Sbrianether_OpenInfo(struct physical *p)
16352942Sbrian{
16452942Sbrian  struct etherdevice *dev = device2ether(p->handler);
16552942Sbrian
16652942Sbrian  switch (dev->connected) {
16752942Sbrian    case CARRIER_PENDING:
16852942Sbrian      return "negotiating";
16952942Sbrian    case CARRIER_OK:
17052942Sbrian      return "established";
17152942Sbrian  }
17252942Sbrian
17352942Sbrian  return "disconnected";
17452942Sbrian}
17552942Sbrian
17652942Sbrianstatic void
17752942Sbrianether_device2iov(struct device *d, struct iovec *iov, int *niov,
17853684Sbrian                 int maxiov, int *auxfd, int *nauxfd)
17952942Sbrian{
18052942Sbrian  struct etherdevice *dev = device2ether(d);
18152942Sbrian  int sz = physical_MaxDeviceSize();
18252942Sbrian
18352942Sbrian  iov[*niov].iov_base = realloc(d, sz);
18452942Sbrian  if (iov[*niov].iov_base == NULL) {
18552942Sbrian    log_Printf(LogALERT, "Failed to allocate memory: %d\n", sz);
18652942Sbrian    AbortProgram(EX_OSERR);
18752942Sbrian  }
18852942Sbrian  iov[*niov].iov_len = sz;
18952942Sbrian  (*niov)++;
19052942Sbrian
19152942Sbrian  if (dev->cs >= 0) {
19252942Sbrian    *auxfd = dev->cs;
19352942Sbrian    (*nauxfd)++;
19452942Sbrian  }
19552942Sbrian}
19652942Sbrian
19752942Sbrianstatic void
19852942Sbrianether_MessageIn(struct etherdevice *dev)
19952942Sbrian{
20052942Sbrian  char msgbuf[sizeof(struct ng_mesg) + sizeof(struct ngpppoe_sts)];
20152942Sbrian  struct ng_mesg *rep = (struct ng_mesg *)msgbuf;
20252942Sbrian  struct ngpppoe_sts *sts = (struct ngpppoe_sts *)(msgbuf + sizeof *rep);
20352942Sbrian  char unknown[14];
20452942Sbrian  const char *msg;
20552942Sbrian  struct timeval t;
20652942Sbrian  fd_set r;
20752942Sbrian
20852942Sbrian  if (dev->cs < 0)
20952942Sbrian    return;
21052942Sbrian
21152942Sbrian  FD_ZERO(&r);
21252942Sbrian  FD_SET(dev->cs, &r);
21352942Sbrian  t.tv_sec = t.tv_usec = 0;
21452942Sbrian  if (select(dev->cs + 1, &r, NULL, NULL, &t) <= 0)
21552942Sbrian    return;
21652942Sbrian
21752942Sbrian  if (NgRecvMsg(dev->cs, rep, sizeof msgbuf, NULL) < 0)
21852942Sbrian    return;
21952942Sbrian
22052942Sbrian  if (rep->header.version != NG_VERSION) {
22152942Sbrian    log_Printf(LogWARN, "%ld: Unexpected netgraph version, expected %ld\n",
22252942Sbrian               (long)rep->header.version, (long)NG_VERSION);
22352942Sbrian    return;
22452942Sbrian  }
22552942Sbrian
22652942Sbrian  if (rep->header.typecookie != NGM_PPPOE_COOKIE) {
22752942Sbrian    log_Printf(LogWARN, "%ld: Unexpected netgraph cookie, expected %ld\n",
22852942Sbrian               (long)rep->header.typecookie, (long)NGM_PPPOE_COOKIE);
22952942Sbrian    return;
23052942Sbrian  }
23152942Sbrian
23252942Sbrian  switch (rep->header.cmd) {
23352942Sbrian    case NGM_PPPOE_SET_FLAG:	msg = "SET_FLAG";	break;
23452942Sbrian    case NGM_PPPOE_CONNECT:	msg = "CONNECT";	break;
23552942Sbrian    case NGM_PPPOE_LISTEN:	msg = "LISTEN";		break;
23652942Sbrian    case NGM_PPPOE_OFFER:	msg = "OFFER";		break;
23752942Sbrian    case NGM_PPPOE_SUCCESS:	msg = "SUCCESS";	break;
23852942Sbrian    case NGM_PPPOE_FAIL:	msg = "FAIL";		break;
23952942Sbrian    case NGM_PPPOE_CLOSE:	msg = "CLOSE";		break;
24052942Sbrian    case NGM_PPPOE_GET_STATUS:	msg = "GET_STATUS";	break;
24152942Sbrian    default:
24252942Sbrian      snprintf(unknown, sizeof unknown, "<%d>", (int)rep->header.cmd);
24352942Sbrian      msg = unknown;
24452942Sbrian      break;
24552942Sbrian  }
24652942Sbrian
24752942Sbrian  log_Printf(LogPHASE, "Received NGM_PPPOE_%s (hook \"%s\")\n", msg, sts->hook);
24852942Sbrian
24952942Sbrian  switch (rep->header.cmd) {
25052942Sbrian    case NGM_PPPOE_SUCCESS:
25152942Sbrian      dev->connected = CARRIER_OK;
25252942Sbrian      break;
25352942Sbrian    case NGM_PPPOE_FAIL:
25452942Sbrian    case NGM_PPPOE_CLOSE:
25552942Sbrian      dev->connected = CARRIER_LOST;
25652942Sbrian      break;
25752942Sbrian  }
25852942Sbrian}
25952942Sbrian
26052942Sbrianstatic int
26152942Sbrianether_AwaitCarrier(struct physical *p)
26252942Sbrian{
26352942Sbrian  struct etherdevice *dev = device2ether(p->handler);
26452942Sbrian
26553071Sbrian  if (dev->connected != CARRIER_OK && !dev->timeout--)
26652942Sbrian    dev->connected = CARRIER_LOST;
26752942Sbrian  else if (dev->connected == CARRIER_PENDING)
26852942Sbrian    ether_MessageIn(dev);
26952942Sbrian
27052942Sbrian  return dev->connected;
27152942Sbrian}
27252942Sbrian
27352942Sbrianstatic const struct device baseetherdevice = {
27452942Sbrian  ETHER_DEVICE,
27552942Sbrian  "ether",
27653733Sbrian  { CD_REQUIRED, DEF_ETHERCDDELAY },
27752942Sbrian  ether_AwaitCarrier,
27852942Sbrian  ether_RemoveFromSet,
27952942Sbrian  NULL,
28052942Sbrian  NULL,
28152942Sbrian  NULL,
28252942Sbrian  NULL,
28352942Sbrian  ether_Free,
28452942Sbrian  ether_Read,
28552942Sbrian  ether_Write,
28652942Sbrian  ether_device2iov,
28752942Sbrian  NULL,
28852942Sbrian  ether_OpenInfo
28952942Sbrian};
29052942Sbrian
29152942Sbrianstruct device *
29252942Sbrianether_iov2device(int type, struct physical *p, struct iovec *iov, int *niov,
29352942Sbrian                 int maxiov, int *auxfd, int *nauxfd)
29452942Sbrian{
29552942Sbrian  if (type == ETHER_DEVICE) {
29652942Sbrian    struct etherdevice *dev = (struct etherdevice *)iov[(*niov)++].iov_base;
29752942Sbrian
29852942Sbrian    dev = realloc(dev, sizeof *dev);	/* Reduce to the correct size */
29952942Sbrian    if (dev == NULL) {
30052942Sbrian      log_Printf(LogALERT, "Failed to allocate memory: %d\n",
30152942Sbrian                 (int)(sizeof *dev));
30252942Sbrian      AbortProgram(EX_OSERR);
30352942Sbrian    }
30452942Sbrian
30552942Sbrian    if (*nauxfd) {
30652942Sbrian      dev->cs = *auxfd;
30752942Sbrian      (*nauxfd)--;
30852942Sbrian    } else
30952942Sbrian      dev->cs = -1;
31052942Sbrian
31152942Sbrian    /* Refresh function pointers etc */
31252942Sbrian    memcpy(&dev->dev, &baseetherdevice, sizeof dev->dev);
31352942Sbrian
31452942Sbrian    physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNCNOACF);
31552942Sbrian    return &dev->dev;
31652942Sbrian  }
31752942Sbrian
31852942Sbrian  return NULL;
31952942Sbrian}
32052942Sbrian
32152942Sbrianstatic int
32258028Sbrianether_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
32352942Sbrian{
32452942Sbrian  struct physical *p = descriptor2physical(d);
32552942Sbrian  struct etherdevice *dev = device2ether(p->handler);
32652942Sbrian  int result;
32752942Sbrian
32852942Sbrian  if (r && dev->cs >= 0) {
32952942Sbrian    FD_SET(dev->cs, r);
33052942Sbrian    log_Printf(LogTIMER, "%s(ctrl): fdset(r) %d\n", p->link.name, dev->cs);
33152942Sbrian    result = 1;
33252942Sbrian  } else
33352942Sbrian    result = 0;
33452942Sbrian
33552942Sbrian  result += physical_doUpdateSet(d, r, w, e, n, 0);
33652942Sbrian
33752942Sbrian  return result;
33852942Sbrian}
33952942Sbrian
34052942Sbrianstatic int
34158028Sbrianether_IsSet(struct fdescriptor *d, const fd_set *fdset)
34252942Sbrian{
34352942Sbrian  struct physical *p = descriptor2physical(d);
34452942Sbrian  struct etherdevice *dev = device2ether(p->handler);
34552942Sbrian  int result;
34652942Sbrian
34752942Sbrian  result = dev->cs >= 0 && FD_ISSET(dev->cs, fdset);
34852942Sbrian  result += physical_IsSet(d, fdset);
34952942Sbrian
35052942Sbrian  return result;
35152942Sbrian}
35252942Sbrian
35352942Sbrianstatic void
35458028Sbrianether_DescriptorRead(struct fdescriptor *d, struct bundle *bundle,
35552942Sbrian                     const fd_set *fdset)
35652942Sbrian{
35752942Sbrian  struct physical *p = descriptor2physical(d);
35852942Sbrian  struct etherdevice *dev = device2ether(p->handler);
35952942Sbrian
36052942Sbrian  if (dev->cs >= 0 && FD_ISSET(dev->cs, fdset)) {
36152942Sbrian    ether_MessageIn(dev);
36252942Sbrian    if (dev->connected == CARRIER_LOST) {
36352942Sbrian      log_Printf(LogPHASE, "%s: Device disconnected\n", p->link.name);
36452942Sbrian      datalink_Down(p->dl, CLOSE_NORMAL);
36552942Sbrian      return;
36652942Sbrian    }
36752942Sbrian  }
36852942Sbrian
36952942Sbrian  if (physical_IsSet(d, fdset))
37052942Sbrian    physical_DescriptorRead(d, bundle, fdset);
37152942Sbrian}
37252942Sbrian
37352942Sbrianstatic struct device *
37452942Sbrianether_Abandon(struct etherdevice *dev, struct physical *p)
37552942Sbrian{
37652942Sbrian  /* Abandon our node construction */
37752942Sbrian  close(dev->cs);
37852942Sbrian  close(p->fd);
37952942Sbrian  p->fd = -2;	/* Nobody else need try.. */
38052942Sbrian  free(dev);
38152942Sbrian
38252942Sbrian  return NULL;
38352942Sbrian}
38452942Sbrian
38552942Sbrianstruct device *
38652942Sbrianether_Create(struct physical *p)
38752942Sbrian{
38852942Sbrian  u_char rbuf[2048];
38952942Sbrian  struct etherdevice *dev;
39052942Sbrian  struct ng_mesg *resp;
39152942Sbrian  const struct hooklist *hlist;
39252942Sbrian  const struct nodeinfo *ninfo;
39352942Sbrian  int f;
39452942Sbrian
39552942Sbrian  dev = NULL;
39652942Sbrian  if (p->fd < 0 && !strncasecmp(p->name.full, NG_PPPOE_NODE_TYPE,
39752942Sbrian                                PPPOE_NODE_TYPE_LEN) &&
39852942Sbrian      p->name.full[PPPOE_NODE_TYPE_LEN] == ':') {
39952942Sbrian    const struct linkinfo *nlink;
40052942Sbrian    struct ngpppoe_init_data *data;
40152942Sbrian    struct ngm_mkpeer mkp;
40252942Sbrian    struct ngm_connect ngc;
40352942Sbrian    const char *iface, *provider;
40452942Sbrian    char *path, etherid[12];
40553242Sbrian    int ifacelen, providerlen;
40652942Sbrian    char connectpath[sizeof dev->hook + 2];	/* .:<hook> */
40752942Sbrian
40852942Sbrian    p->fd--;				/* We own the device - change fd */
40952942Sbrian
41053241Sbrian#if defined(__FreeBSD__) && !defined(NOKLDLOAD)
41153241Sbrian    if (modfind("netgraph") == -1) {
41253241Sbrian      log_Printf(LogWARN, "Netgraph is not built into the kernel\n");
41352942Sbrian      return NULL;
41452942Sbrian    }
41552942Sbrian
41662209Sbrian    if (modfind("ng_ether") == -1 && ID0kldload("ng_ether") == -1)
41762209Sbrian      /*
41862209Sbrian       * Don't treat this as an error as older kernels have this stuff
41962209Sbrian       * built in as part of the netgraph node itself.
42062209Sbrian       */
42162209Sbrian      log_Printf(LogWARN, "kldload: ng_ether: %s\n", strerror(errno));
42262209Sbrian
42362209Sbrian    if (modfind("ng_socket") == -1 && ID0kldload("ng_socket") == -1) {
42452942Sbrian      log_Printf(LogWARN, "kldload: ng_socket: %s\n", strerror(errno));
42552942Sbrian      return NULL;
42652942Sbrian    }
42752942Sbrian#endif
42852942Sbrian
42952942Sbrian    if ((dev = malloc(sizeof *dev)) == NULL)
43052942Sbrian      return NULL;
43152942Sbrian
43252942Sbrian    iface = p->name.full + PPPOE_NODE_TYPE_LEN + 1;
43352942Sbrian
43452942Sbrian    provider = strchr(iface, ':');
43552942Sbrian    if (provider) {
43652942Sbrian      ifacelen = provider - iface;
43752942Sbrian      provider++;
43852942Sbrian      providerlen = strlen(provider);
43952942Sbrian    } else {
44052942Sbrian      ifacelen = strlen(iface);
44152942Sbrian      provider = "";
44252942Sbrian      providerlen = 0;
44352942Sbrian    }
44452942Sbrian
44552942Sbrian    /*
44652942Sbrian     * We're going to do this (where tunN is our tunnel device):
44752942Sbrian     *
44852942Sbrian     * .---------.
44952942Sbrian     * |  ether  |
45052942Sbrian     * | <iface> |                         dev->cs
45152942Sbrian     * `---------'                           |
45252942Sbrian     *  (orphan)                     p->fd   |
45352942Sbrian     *     |                           |     |
45452942Sbrian     *     |                           |     |
45552942Sbrian     * (ethernet)                      |     |
45652942Sbrian     * .---------.                  .-----------.
45752942Sbrian     * |  pppoe  |                  |  socket   |
45852942Sbrian     * | <iface> |(tunN)<---->(tunN)| <unnamed> |
45952942Sbrian     * `---------                   `-----------'
46052942Sbrian     *   (tunX)
46152942Sbrian     *     ^
46252942Sbrian     *     |
46352942Sbrian     *     `--->(tunX)
46452942Sbrian     */
46552942Sbrian
46652942Sbrian    /* Create a socket node */
46753535Sbrian    if (ID0NgMkSockNode(NULL, &dev->cs, &p->fd) == -1) {
46852942Sbrian      log_Printf(LogWARN, "Cannot create netgraph socket node: %s\n",
46952942Sbrian                 strerror(errno));
47052942Sbrian      free(dev);
47152942Sbrian      return NULL;
47252942Sbrian    }
47352942Sbrian
47452942Sbrian    /*
47552942Sbrian     * Ask for a list of hooks attached to the "ether" node.  This node should
47652942Sbrian     * magically exist as a way of hooking stuff onto an ethernet device
47752942Sbrian     */
47852942Sbrian    path = (char *)alloca(ifacelen + 2);
47952942Sbrian    sprintf(path, "%.*s:", ifacelen, iface);
48052942Sbrian    if (NgSendMsg(dev->cs, path, NGM_GENERIC_COOKIE, NGM_LISTHOOKS,
48152942Sbrian                  NULL, 0) < 0) {
48252942Sbrian      log_Printf(LogWARN, "%s Cannot send a netgraph message: %s\n",
48352942Sbrian                 path, strerror(errno));
48452942Sbrian      return ether_Abandon(dev, p);
48552942Sbrian    }
48652942Sbrian
48752942Sbrian    /* Get our list back */
48852942Sbrian    resp = (struct ng_mesg *)rbuf;
48952942Sbrian    if (NgRecvMsg(dev->cs, resp, sizeof rbuf, NULL) < 0) {
49052942Sbrian      log_Printf(LogWARN, "Cannot get netgraph response: %s\n",
49152942Sbrian                 strerror(errno));
49252942Sbrian      return ether_Abandon(dev, p);
49352942Sbrian    }
49452942Sbrian
49552942Sbrian    hlist = (const struct hooklist *)resp->data;
49652942Sbrian    ninfo = &hlist->nodeinfo;
49752942Sbrian
49852942Sbrian    /* Make sure we've got the right type of node */
49952942Sbrian    if (strncmp(ninfo->type, NG_ETHER_NODE_TYPE,
50052942Sbrian                sizeof NG_ETHER_NODE_TYPE - 1)) {
50152942Sbrian      log_Printf(LogWARN, "%s Unexpected node type ``%s'' (wanted ``"
50252942Sbrian                 NG_ETHER_NODE_TYPE "'')\n", path, ninfo->type);
50352942Sbrian      return ether_Abandon(dev, p);
50452942Sbrian    }
50552942Sbrian
50652963Sbrian    log_Printf(LogDEBUG, "List of netgraph node ``%s'' (id %x) hooks:\n",
50752942Sbrian               path, ninfo->id);
50852942Sbrian
50952942Sbrian    /* look for a hook already attached.  */
51052942Sbrian    for (f = 0; f < ninfo->hooks; f++) {
51152942Sbrian      nlink = &hlist->link[f];
51252942Sbrian
51352942Sbrian      log_Printf(LogDEBUG, "  Found %s -> %s\n", nlink->ourhook,
51452942Sbrian                 nlink->peerhook);
51552942Sbrian
51652942Sbrian      if (!strcmp(nlink->ourhook, NG_ETHER_HOOK_ORPHAN) ||
51752942Sbrian          !strcmp(nlink->ourhook, NG_ETHER_HOOK_DIVERT)) {
51852942Sbrian        /*
51952942Sbrian         * Something is using the data coming out of this ``ether'' node.
52052942Sbrian         * If it's a PPPoE node, we use that node, otherwise we complain that
52152942Sbrian         * someone else is using the node.
52252942Sbrian         */
52352942Sbrian        if (!strcmp(nlink->nodeinfo.type, NG_PPPOE_NODE_TYPE))
52452942Sbrian          /* Use this PPPoE node ! */
52552963Sbrian          snprintf(ngc.path, sizeof ngc.path, "[%x]:", nlink->nodeinfo.id);
52652942Sbrian        else {
52752942Sbrian          log_Printf(LogWARN, "%s Node type ``%s'' is currently active\n",
52852942Sbrian                     path, nlink->nodeinfo.type);
52952942Sbrian          return ether_Abandon(dev, p);
53052942Sbrian        }
53152942Sbrian        break;
53252942Sbrian      }
53352942Sbrian    }
53452942Sbrian
53552942Sbrian    if (f == ninfo->hooks) {
53652942Sbrian      /*
53752942Sbrian       * Create a new ``PPPoE'' node connected to the ``ether'' node using
53852942Sbrian       * the magic ``orphan'' and ``ethernet'' hooks
53952942Sbrian       */
54052942Sbrian      snprintf(mkp.type, sizeof mkp.type, "%s", NG_PPPOE_NODE_TYPE);
54152942Sbrian      snprintf(mkp.ourhook, sizeof mkp.ourhook, "%s", NG_ETHER_HOOK_ORPHAN);
54252942Sbrian      snprintf(mkp.peerhook, sizeof mkp.peerhook, "%s", NG_PPPOE_HOOK_ETHERNET);
54352963Sbrian      snprintf(etherid, sizeof etherid, "[%x]:", ninfo->id);
54452942Sbrian
54552942Sbrian      log_Printf(LogDEBUG, "Creating PPPoE netgraph node %s%s -> %s\n",
54652942Sbrian                 etherid, mkp.ourhook, mkp.peerhook);
54752942Sbrian
54852942Sbrian      if (NgSendMsg(dev->cs, etherid, NGM_GENERIC_COOKIE,
54952942Sbrian                    NGM_MKPEER, &mkp, sizeof mkp) < 0) {
55052942Sbrian        log_Printf(LogWARN, "%s Cannot create PPPoE netgraph node: %s\n",
55152942Sbrian                   etherid, strerror(errno));
55252942Sbrian        return ether_Abandon(dev, p);
55352942Sbrian      }
55452942Sbrian
55552942Sbrian      snprintf(ngc.path, sizeof ngc.path, "%s%s", path, NG_ETHER_HOOK_ORPHAN);
55652942Sbrian    }
55752942Sbrian
55852942Sbrian    snprintf(dev->hook, sizeof dev->hook, "%s%d",
55952942Sbrian             TUN_NAME, p->dl->bundle->unit);
56052942Sbrian
56152942Sbrian    /*
56252942Sbrian     * Connect the PPPoE node to our socket node.
56352942Sbrian     * ngc.path has already been set up
56452942Sbrian     */
56552942Sbrian    snprintf(ngc.ourhook, sizeof ngc.ourhook, "%s", dev->hook);
56652942Sbrian    memcpy(ngc.peerhook, ngc.ourhook, sizeof ngc.peerhook);
56752942Sbrian
56852942Sbrian    log_Printf(LogDEBUG, "Connecting netgraph socket .:%s -> %s:%s\n",
56952942Sbrian               ngc.ourhook, ngc.path, ngc.peerhook);
57052942Sbrian    if (NgSendMsg(dev->cs, ".:", NGM_GENERIC_COOKIE,
57152942Sbrian                  NGM_CONNECT, &ngc, sizeof ngc) < 0) {
57252942Sbrian      log_Printf(LogWARN, "Cannot connect PPPoE and socket netgraph "
57352942Sbrian                 "nodes: %s\n", strerror(errno));
57452942Sbrian      return ether_Abandon(dev, p);
57552942Sbrian    }
57652942Sbrian
57752942Sbrian    /* And finally, request a connection to the given provider */
57852942Sbrian
57952942Sbrian    data = (struct ngpppoe_init_data *)alloca(sizeof *data + providerlen + 1);
58052942Sbrian
58152942Sbrian    snprintf(data->hook, sizeof data->hook, "%s", dev->hook);
58252942Sbrian    strcpy(data->data, provider);
58352942Sbrian    data->data_len = providerlen;
58452942Sbrian
58552942Sbrian    snprintf(connectpath, sizeof connectpath, ".:%s", dev->hook);
58652942Sbrian    log_Printf(LogDEBUG, "Sending PPPOE_CONNECT to %s\n", connectpath);
58752942Sbrian    if (NgSendMsg(dev->cs, connectpath, NGM_PPPOE_COOKIE,
58852942Sbrian                  NGM_PPPOE_CONNECT, data, sizeof *data + providerlen) == -1) {
58952942Sbrian      log_Printf(LogWARN, "``%s'': Cannot start netgraph node: %s\n",
59052942Sbrian                 connectpath, strerror(errno));
59152942Sbrian      return ether_Abandon(dev, p);
59252942Sbrian    }
59352942Sbrian
59453062Sbrian    /* Hook things up so that we monitor dev->cs */
59553062Sbrian    p->desc.UpdateSet = ether_UpdateSet;
59653062Sbrian    p->desc.IsSet = ether_IsSet;
59753062Sbrian    p->desc.Read = ether_DescriptorRead;
59853733Sbrian
59953733Sbrian    memcpy(&dev->dev, &baseetherdevice, sizeof dev->dev);
60053733Sbrian    switch (p->cfg.cd.necessity) {
60153733Sbrian      case CD_VARIABLE:
60253733Sbrian        dev->dev.cd.delay = p->cfg.cd.delay;
60353733Sbrian        break;
60453733Sbrian      case CD_REQUIRED:
60553733Sbrian        dev->dev.cd = p->cfg.cd;
60653733Sbrian        break;
60753733Sbrian      case CD_NOTREQUIRED:
60853733Sbrian        log_Printf(LogWARN, "%s: Carrier must be set, using ``set cd %d!''\n",
60953733Sbrian                   p->link.name, dev->dev.cd.delay);
61053733Sbrian      case CD_DEFAULT:
61153733Sbrian        break;
61253733Sbrian    }
61353733Sbrian
61453733Sbrian    dev->timeout = dev->dev.cd.delay;
61553733Sbrian    dev->connected = CARRIER_PENDING;
61653733Sbrian
61752942Sbrian  } else {
61852942Sbrian    /* See if we're a netgraph socket */
61952942Sbrian    struct sockaddr_ng ngsock;
62052942Sbrian    struct sockaddr *sock = (struct sockaddr *)&ngsock;
62152942Sbrian    int sz;
62252942Sbrian
62352942Sbrian    sz = sizeof ngsock;
62452942Sbrian    if (getsockname(p->fd, sock, &sz) != -1 && sock->sa_family == AF_NETGRAPH) {
62552942Sbrian      /*
62653062Sbrian       * It's a netgraph node... We can't determine hook names etc, so we
62753062Sbrian       * stay pretty impartial....
62852942Sbrian       */
62953062Sbrian      log_Printf(LogPHASE, "%s: Link is a netgraph node\n", p->link.name);
63052942Sbrian
63152942Sbrian      if ((dev = malloc(sizeof *dev)) == NULL) {
63252942Sbrian        log_Printf(LogWARN, "%s: Cannot allocate an ether device: %s\n",
63352942Sbrian                   p->link.name, strerror(errno));
63452942Sbrian        return NULL;
63552942Sbrian      }
63652942Sbrian
63753733Sbrian      memcpy(&dev->dev, &baseetherdevice, sizeof dev->dev);
63852942Sbrian      dev->cs = -1;
63952942Sbrian      dev->timeout = 0;
64052942Sbrian      dev->connected = CARRIER_OK;
64153062Sbrian      *dev->hook = '\0';
64252942Sbrian    }
64352942Sbrian  }
64452942Sbrian
64552942Sbrian  if (dev) {
64652942Sbrian    physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNCNOACF);
64752942Sbrian
64852942Sbrian    /* Moan about (and fix) invalid LCP configurations */
64952942Sbrian    if (p->link.lcp.cfg.mru > 1492) {
65052942Sbrian      log_Printf(LogWARN, "%s: Reducing MRU to 1492\n", p->link.name);
65152942Sbrian      p->link.lcp.cfg.mru = 1492;
65252942Sbrian    }
65352942Sbrian    if (p->dl->bundle->cfg.mtu > 1492) {
65452942Sbrian      log_Printf(LogWARN, "%s: Reducing MTU to 1492\n", p->link.name);
65552942Sbrian      p->dl->bundle->cfg.mtu = 1492;
65652942Sbrian    }
65752942Sbrian
65852942Sbrian    return &dev->dev;
65952942Sbrian  }
66052942Sbrian
66152942Sbrian  return NULL;
66252942Sbrian}
663