ether.c revision 52942
1/*-
2 * Copyright (c) 1999 Brian Somers <brian@Awfulhak.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/usr.sbin/ppp/ether.c 52942 1999-11-06 22:50:59Z brian $
27 */
28
29#include <sys/param.h>
30#include <sys/socket.h>
31#include <sys/un.h>
32#include <netinet/in.h>
33#include <arpa/inet.h>
34#include <netdb.h>
35#include <netgraph.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>
43
44#include <errno.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48#include <sysexits.h>
49#include <sys/fcntl.h>
50#if defined(__FreeBSD__) && !defined(NOKLDLOAD)
51#include <sys/linker.h>
52#endif
53#include <sys/uio.h>
54#include <termios.h>
55#ifndef NONBLOCK_FIXED
56#include <sys/time.h>
57#endif
58#include <unistd.h>
59
60#include "layer.h"
61#include "defs.h"
62#include "mbuf.h"
63#include "log.h"
64#include "timer.h"
65#include "lqr.h"
66#include "hdlc.h"
67#include "throughput.h"
68#include "fsm.h"
69#include "lcp.h"
70#include "ccp.h"
71#include "link.h"
72#include "async.h"
73#include "descriptor.h"
74#include "physical.h"
75#include "main.h"
76#include "mp.h"
77#include "chat.h"
78#include "auth.h"
79#include "chap.h"
80#include "cbcp.h"
81#include "datalink.h"
82#include "slcompress.h"
83#include "iplist.h"
84#include "ipcp.h"
85#include "filter.h"
86#ifndef NORADIUS
87#include "radius.h"
88#endif
89#include "bundle.h"
90#include "id.h"
91#include "ether.h"
92
93
94#define PPPOE_NODE_TYPE_LEN (sizeof NG_PPPOE_NODE_TYPE - 1) /* "PPPoE" */
95
96struct etherdevice {
97  struct device dev;			/* What struct physical knows about */
98  int cs;				/* Control socket */
99  int connected;			/* Are we connected yet ? */
100  int timeout;				/* Seconds attempting to connect */
101  char hook[sizeof TUN_NAME + 11];	/* Our socket node hook */
102};
103
104#define device2ether(d) \
105  ((d)->type == ETHER_DEVICE ? (struct etherdevice *)d : NULL)
106
107int
108ether_DeviceSize(void)
109{
110  return sizeof(struct etherdevice);
111}
112
113static ssize_t
114ether_Write(struct physical *p, const void *v, size_t n)
115{
116  struct etherdevice *dev = device2ether(p->handler);
117
118  return NgSendData(p->fd, dev->hook, v, n) == -1 ? -1 : n;
119}
120
121static ssize_t
122ether_Read(struct physical *p, void *v, size_t n)
123{
124  char hook[sizeof TUN_NAME + 11];
125
126  return NgRecvData(p->fd, v, n, hook);
127}
128
129static int
130ether_RemoveFromSet(struct physical *p, fd_set *r, fd_set *w, fd_set *e)
131{
132  struct etherdevice *dev = device2ether(p->handler);
133  int result;
134
135  if (r && dev->cs >= 0 && FD_ISSET(dev->cs, r)) {
136    FD_CLR(dev->cs, r);
137    log_Printf(LogTIMER, "%s: fdunset(ctrl) %d\n", p->link.name, dev->cs);
138    result = 1;
139  } else
140    result = 0;
141
142  /* Careful... physical_RemoveFromSet() called us ! */
143
144  p->handler->removefromset = NULL;
145  result += physical_RemoveFromSet(p, r, w, e);
146  p->handler->removefromset = ether_RemoveFromSet;
147
148  return result;
149}
150
151static void
152ether_Free(struct physical *p)
153{
154  struct etherdevice *dev = device2ether(p->handler);
155
156  physical_SetDescriptor(p);
157  if (dev->cs != -1)
158    close(dev->cs);
159  free(dev);
160}
161
162static const char *
163ether_OpenInfo(struct physical *p)
164{
165  struct etherdevice *dev = device2ether(p->handler);
166
167  switch (dev->connected) {
168    case CARRIER_PENDING:
169      return "negotiating";
170    case CARRIER_OK:
171      return "established";
172  }
173
174  return "disconnected";
175}
176
177static void
178ether_device2iov(struct device *d, struct iovec *iov, int *niov,
179                 int maxiov, int *auxfd, int *nauxfd, pid_t newpid)
180{
181  struct etherdevice *dev = device2ether(d);
182  int sz = physical_MaxDeviceSize();
183
184  iov[*niov].iov_base = realloc(d, sz);
185  if (iov[*niov].iov_base == NULL) {
186    log_Printf(LogALERT, "Failed to allocate memory: %d\n", sz);
187    AbortProgram(EX_OSERR);
188  }
189  iov[*niov].iov_len = sz;
190  (*niov)++;
191
192  if (dev->cs >= 0) {
193    *auxfd = dev->cs;
194    (*nauxfd)++;
195  }
196}
197
198static void
199ether_MessageIn(struct etherdevice *dev)
200{
201  char msgbuf[sizeof(struct ng_mesg) + sizeof(struct ngpppoe_sts)];
202  struct ng_mesg *rep = (struct ng_mesg *)msgbuf;
203  struct ngpppoe_sts *sts = (struct ngpppoe_sts *)(msgbuf + sizeof *rep);
204  char unknown[14];
205  const char *msg;
206#ifndef NONBLOCK_FIXED
207  struct timeval t;
208  fd_set r;
209#endif
210
211  if (dev->cs < 0)
212    return;
213
214#ifndef NONBLOCK_FIXED
215  FD_ZERO(&r);
216  FD_SET(dev->cs, &r);
217  t.tv_sec = t.tv_usec = 0;
218  if (select(dev->cs + 1, &r, NULL, NULL, &t) <= 0)
219    return;
220#endif
221
222  if (NgRecvMsg(dev->cs, rep, sizeof msgbuf, NULL) < 0)
223    return;
224
225  if (rep->header.version != NG_VERSION) {
226    log_Printf(LogWARN, "%ld: Unexpected netgraph version, expected %ld\n",
227               (long)rep->header.version, (long)NG_VERSION);
228    return;
229  }
230
231  if (rep->header.typecookie != NGM_PPPOE_COOKIE) {
232    log_Printf(LogWARN, "%ld: Unexpected netgraph cookie, expected %ld\n",
233               (long)rep->header.typecookie, (long)NGM_PPPOE_COOKIE);
234    return;
235  }
236
237  switch (rep->header.cmd) {
238    case NGM_PPPOE_SET_FLAG:	msg = "SET_FLAG";	break;
239    case NGM_PPPOE_CONNECT:	msg = "CONNECT";	break;
240    case NGM_PPPOE_LISTEN:	msg = "LISTEN";		break;
241    case NGM_PPPOE_OFFER:	msg = "OFFER";		break;
242    case NGM_PPPOE_SUCCESS:	msg = "SUCCESS";	break;
243    case NGM_PPPOE_FAIL:	msg = "FAIL";		break;
244    case NGM_PPPOE_CLOSE:	msg = "CLOSE";		break;
245    case NGM_PPPOE_GET_STATUS:	msg = "GET_STATUS";	break;
246    default:
247      snprintf(unknown, sizeof unknown, "<%d>", (int)rep->header.cmd);
248      msg = unknown;
249      break;
250  }
251
252  log_Printf(LogPHASE, "Received NGM_PPPOE_%s (hook \"%s\")\n", msg, sts->hook);
253
254  switch (rep->header.cmd) {
255    case NGM_PPPOE_SUCCESS:
256      dev->connected = CARRIER_OK;
257      break;
258    case NGM_PPPOE_FAIL:
259    case NGM_PPPOE_CLOSE:
260      dev->connected = CARRIER_LOST;
261      break;
262  }
263}
264
265static int
266ether_AwaitCarrier(struct physical *p)
267{
268  struct etherdevice *dev = device2ether(p->handler);
269
270  if (!dev->timeout--)
271    dev->connected = CARRIER_LOST;
272  else if (dev->connected == CARRIER_PENDING)
273    ether_MessageIn(dev);
274
275  return dev->connected;
276}
277
278static const struct device baseetherdevice = {
279  ETHER_DEVICE,
280  "ether",
281  ether_AwaitCarrier,
282  ether_RemoveFromSet,
283  NULL,
284  NULL,
285  NULL,
286  NULL,
287  ether_Free,
288  ether_Read,
289  ether_Write,
290  ether_device2iov,
291  NULL,
292  ether_OpenInfo
293};
294
295struct device *
296ether_iov2device(int type, struct physical *p, struct iovec *iov, int *niov,
297                 int maxiov, int *auxfd, int *nauxfd)
298{
299  if (type == ETHER_DEVICE) {
300    struct etherdevice *dev = (struct etherdevice *)iov[(*niov)++].iov_base;
301
302    dev = realloc(dev, sizeof *dev);	/* Reduce to the correct size */
303    if (dev == NULL) {
304      log_Printf(LogALERT, "Failed to allocate memory: %d\n",
305                 (int)(sizeof *dev));
306      AbortProgram(EX_OSERR);
307    }
308
309    if (*nauxfd) {
310      dev->cs = *auxfd;
311      (*nauxfd)--;
312    } else
313      dev->cs = -1;
314
315    /* Refresh function pointers etc */
316    memcpy(&dev->dev, &baseetherdevice, sizeof dev->dev);
317
318    physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNCNOACF);
319    return &dev->dev;
320  }
321
322  return NULL;
323}
324
325static int
326ether_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
327{
328  struct physical *p = descriptor2physical(d);
329  struct etherdevice *dev = device2ether(p->handler);
330  int result;
331
332  if (r && dev->cs >= 0) {
333    FD_SET(dev->cs, r);
334    log_Printf(LogTIMER, "%s(ctrl): fdset(r) %d\n", p->link.name, dev->cs);
335    result = 1;
336  } else
337    result = 0;
338
339  result += physical_doUpdateSet(d, r, w, e, n, 0);
340
341  return result;
342}
343
344static int
345ether_IsSet(struct descriptor *d, const fd_set *fdset)
346{
347  struct physical *p = descriptor2physical(d);
348  struct etherdevice *dev = device2ether(p->handler);
349  int result;
350
351  result = dev->cs >= 0 && FD_ISSET(dev->cs, fdset);
352  result += physical_IsSet(d, fdset);
353
354  return result;
355}
356
357static void
358ether_DescriptorRead(struct descriptor *d, struct bundle *bundle,
359                     const fd_set *fdset)
360{
361  struct physical *p = descriptor2physical(d);
362  struct etherdevice *dev = device2ether(p->handler);
363
364  if (dev->cs >= 0 && FD_ISSET(dev->cs, fdset)) {
365    ether_MessageIn(dev);
366    if (dev->connected == CARRIER_LOST) {
367      log_Printf(LogPHASE, "%s: Device disconnected\n", p->link.name);
368      datalink_Down(p->dl, CLOSE_NORMAL);
369      return;
370    }
371  }
372
373  if (physical_IsSet(d, fdset))
374    physical_DescriptorRead(d, bundle, fdset);
375}
376
377static struct device *
378ether_Abandon(struct etherdevice *dev, struct physical *p)
379{
380  /* Abandon our node construction */
381  close(dev->cs);
382  close(p->fd);
383  p->fd = -2;	/* Nobody else need try.. */
384  free(dev);
385
386  return NULL;
387}
388
389struct device *
390ether_Create(struct physical *p)
391{
392  u_char rbuf[2048];
393  struct etherdevice *dev;
394  struct ng_mesg *resp;
395  const struct hooklist *hlist;
396  const struct nodeinfo *ninfo;
397  int f;
398
399  dev = NULL;
400  if (p->fd < 0 && !strncasecmp(p->name.full, NG_PPPOE_NODE_TYPE,
401                                PPPOE_NODE_TYPE_LEN) &&
402      p->name.full[PPPOE_NODE_TYPE_LEN] == ':') {
403    const struct linkinfo *nlink;
404    struct ngpppoe_init_data *data;
405    struct ngm_mkpeer mkp;
406    struct ngm_connect ngc;
407    const char *iface, *provider;
408    char *path, etherid[12];
409    int ifacelen, providerlen, oldflag;
410    char connectpath[sizeof dev->hook + 2];	/* .:<hook> */
411
412#ifdef KLDSYM_LOOKUP
413    /* First make sure we've got the right code loaded */
414    char basesym[] = "ng_make_node", socksym[] = "ngdomain";
415    struct kld_sym_lookup baselookup = { sizeof baselookup, basesym, 0, 0 };
416    struct kld_sym_lookup socklookup = { sizeof socklookup, socksym, 0, 0 };
417#endif
418
419    p->fd--;				/* We own the device - change fd */
420
421#ifdef KLDSYM_LOOKUP
422    if (kldsym(0, KLDSYM_LOOKUP, &baselookup) == -1) {
423      log_Printf(LogWARN, "Can't run without options NETGRAPH in the kernel\n");
424      return NULL;
425    }
426
427    if (kldsym(0, KLDSYM_LOOKUP, &socklookup) == -1 &&
428        ID0kldload("ng_socket") == -1) {
429      log_Printf(LogWARN, "kldload: ng_socket: %s\n", strerror(errno));
430      return NULL;
431    }
432#endif
433
434    if ((dev = malloc(sizeof *dev)) == NULL)
435      return NULL;
436
437    iface = p->name.full + PPPOE_NODE_TYPE_LEN + 1;
438
439    provider = strchr(iface, ':');
440    if (provider) {
441      ifacelen = provider - iface;
442      provider++;
443      providerlen = strlen(provider);
444    } else {
445      ifacelen = strlen(iface);
446      provider = "";
447      providerlen = 0;
448    }
449
450    /*
451     * We're going to do this (where tunN is our tunnel device):
452     *
453     * .---------.
454     * |  ether  |
455     * | <iface> |                         dev->cs
456     * `---------'                           |
457     *  (orphan)                     p->fd   |
458     *     |                           |     |
459     *     |                           |     |
460     * (ethernet)                      |     |
461     * .---------.                  .-----------.
462     * |  pppoe  |                  |  socket   |
463     * | <iface> |(tunN)<---->(tunN)| <unnamed> |
464     * `---------                   `-----------'
465     *   (tunX)
466     *     ^
467     *     |
468     *     `--->(tunX)
469     */
470
471    /* Create a socket node */
472    if (NgMkSockNode(NULL, &dev->cs, &p->fd) == -1) {
473      log_Printf(LogWARN, "Cannot create netgraph socket node: %s\n",
474                 strerror(errno));
475      free(dev);
476      return NULL;
477    }
478
479    /*
480     * Ask for a list of hooks attached to the "ether" node.  This node should
481     * magically exist as a way of hooking stuff onto an ethernet device
482     */
483    path = (char *)alloca(ifacelen + 2);
484    sprintf(path, "%.*s:", ifacelen, iface);
485    if (NgSendMsg(dev->cs, path, NGM_GENERIC_COOKIE, NGM_LISTHOOKS,
486                  NULL, 0) < 0) {
487      log_Printf(LogWARN, "%s Cannot send a netgraph message: %s\n",
488                 path, strerror(errno));
489      return ether_Abandon(dev, p);
490    }
491
492    /* Get our list back */
493    resp = (struct ng_mesg *)rbuf;
494    if (NgRecvMsg(dev->cs, resp, sizeof rbuf, NULL) < 0) {
495      log_Printf(LogWARN, "Cannot get netgraph response: %s\n",
496                 strerror(errno));
497      return ether_Abandon(dev, p);
498    }
499
500    hlist = (const struct hooklist *)resp->data;
501    ninfo = &hlist->nodeinfo;
502
503    /* Make sure we've got the right type of node */
504    if (strncmp(ninfo->type, NG_ETHER_NODE_TYPE,
505                sizeof NG_ETHER_NODE_TYPE - 1)) {
506      log_Printf(LogWARN, "%s Unexpected node type ``%s'' (wanted ``"
507                 NG_ETHER_NODE_TYPE "'')\n", path, ninfo->type);
508      return ether_Abandon(dev, p);
509    }
510
511    log_Printf(LogDEBUG, "List of netgraph node ``%s'' (id %08x) hooks:\n",
512               path, ninfo->id);
513
514    /* look for a hook already attached.  */
515    for (f = 0; f < ninfo->hooks; f++) {
516      nlink = &hlist->link[f];
517
518      log_Printf(LogDEBUG, "  Found %s -> %s\n", nlink->ourhook,
519                 nlink->peerhook);
520
521      if (!strcmp(nlink->ourhook, NG_ETHER_HOOK_ORPHAN) ||
522          !strcmp(nlink->ourhook, NG_ETHER_HOOK_DIVERT)) {
523        /*
524         * Something is using the data coming out of this ``ether'' node.
525         * If it's a PPPoE node, we use that node, otherwise we complain that
526         * someone else is using the node.
527         */
528        if (!strcmp(nlink->nodeinfo.type, NG_PPPOE_NODE_TYPE))
529          /* Use this PPPoE node ! */
530          snprintf(ngc.path, sizeof ngc.path, "[%08x]:", nlink->nodeinfo.id);
531        else {
532          log_Printf(LogWARN, "%s Node type ``%s'' is currently active\n",
533                     path, nlink->nodeinfo.type);
534          return ether_Abandon(dev, p);
535        }
536        break;
537      }
538    }
539
540    if (f == ninfo->hooks) {
541      /*
542       * Create a new ``PPPoE'' node connected to the ``ether'' node using
543       * the magic ``orphan'' and ``ethernet'' hooks
544       */
545      snprintf(mkp.type, sizeof mkp.type, "%s", NG_PPPOE_NODE_TYPE);
546      snprintf(mkp.ourhook, sizeof mkp.ourhook, "%s", NG_ETHER_HOOK_ORPHAN);
547      snprintf(mkp.peerhook, sizeof mkp.peerhook, "%s", NG_PPPOE_HOOK_ETHERNET);
548      snprintf(etherid, sizeof etherid, "[%08x]:", ninfo->id);
549
550      log_Printf(LogDEBUG, "Creating PPPoE netgraph node %s%s -> %s\n",
551                 etherid, mkp.ourhook, mkp.peerhook);
552
553      if (NgSendMsg(dev->cs, etherid, NGM_GENERIC_COOKIE,
554                    NGM_MKPEER, &mkp, sizeof mkp) < 0) {
555        log_Printf(LogWARN, "%s Cannot create PPPoE netgraph node: %s\n",
556                   etherid, strerror(errno));
557        return ether_Abandon(dev, p);
558      }
559
560      snprintf(ngc.path, sizeof ngc.path, "%s%s", path, NG_ETHER_HOOK_ORPHAN);
561    }
562
563    snprintf(dev->hook, sizeof dev->hook, "%s%d",
564             TUN_NAME, p->dl->bundle->unit);
565
566    /*
567     * Connect the PPPoE node to our socket node.
568     * ngc.path has already been set up
569     */
570    snprintf(ngc.ourhook, sizeof ngc.ourhook, "%s", dev->hook);
571    memcpy(ngc.peerhook, ngc.ourhook, sizeof ngc.peerhook);
572
573    log_Printf(LogDEBUG, "Connecting netgraph socket .:%s -> %s:%s\n",
574               ngc.ourhook, ngc.path, ngc.peerhook);
575    if (NgSendMsg(dev->cs, ".:", NGM_GENERIC_COOKIE,
576                  NGM_CONNECT, &ngc, sizeof ngc) < 0) {
577      log_Printf(LogWARN, "Cannot connect PPPoE and socket netgraph "
578                 "nodes: %s\n", strerror(errno));
579      return ether_Abandon(dev, p);
580    }
581
582    /* And finally, request a connection to the given provider */
583
584    data = (struct ngpppoe_init_data *)alloca(sizeof *data + providerlen + 1);
585
586    snprintf(data->hook, sizeof data->hook, "%s", dev->hook);
587    strcpy(data->data, provider);
588    data->data_len = providerlen;
589
590    snprintf(connectpath, sizeof connectpath, ".:%s", dev->hook);
591    log_Printf(LogDEBUG, "Sending PPPOE_CONNECT to %s\n", connectpath);
592    if (NgSendMsg(dev->cs, connectpath, NGM_PPPOE_COOKIE,
593                  NGM_PPPOE_CONNECT, data, sizeof *data + providerlen) == -1) {
594      log_Printf(LogWARN, "``%s'': Cannot start netgraph node: %s\n",
595                 connectpath, strerror(errno));
596      return ether_Abandon(dev, p);
597    }
598
599    /*
600     * Now make our control socket non-blocking so that we can read()
601     * without having to select()
602     *
603     * XXX: Does this work (#define NONBLOCK_FIXED) ?
604     */
605    oldflag = fcntl(dev->cs, F_GETFL, 0);
606    if (oldflag < 0) {
607      log_Printf(LogWARN, "%s: Open: Cannot get physical flags: %s\n",
608                 p->link.name, strerror(errno));
609      return ether_Abandon(dev, p);
610    } else
611      fcntl(dev->cs, F_SETFL, oldflag & ~O_NONBLOCK);
612
613    dev->timeout = p->cfg.cd.delay;
614    dev->connected = CARRIER_PENDING;
615  } else {
616    /* See if we're a netgraph socket */
617    struct sockaddr_ng ngsock;
618    struct sockaddr *sock = (struct sockaddr *)&ngsock;
619    int sz;
620
621    sz = sizeof ngsock;
622    if (getsockname(p->fd, sock, &sz) != -1 && sock->sa_family == AF_NETGRAPH) {
623      /*
624       * It's a netgraph node... determine the hook name and set things up
625       */
626
627      if (NgSendMsg(dev->cs, ".", NGM_GENERIC_COOKIE, NGM_LISTHOOKS,
628                    NULL, 0) < 0) {
629        log_Printf(LogWARN, "Cannot send a netgraph message to stdin: %s\n",
630                   strerror(errno));
631        close(p->fd);
632        p->fd = -1;
633        return NULL;
634      }
635
636      /* Get our list back */
637      resp = (struct ng_mesg *)rbuf;
638      if (NgRecvMsg(dev->cs, resp, sizeof rbuf, NULL) < 0) {
639        log_Printf(LogWARN, "Cannot get netgraph response: %s\n",
640                   strerror(errno));
641        close(p->fd);
642        p->fd = -1;
643        return NULL;
644      }
645
646      hlist = (const struct hooklist *)resp->data;
647      ninfo = &hlist->nodeinfo;
648
649      /*
650       * Make sure we've got the right type of node...
651       * Can it be anything else ?
652       */
653      if (strncmp(ninfo->type, NG_SOCKET_NODE_TYPE,
654                  sizeof NG_SOCKET_NODE_TYPE - 1)) {
655        log_Printf(LogWARN, "Unexpected netgraph node type ``%s'' (wanted ``"
656                   NG_SOCKET_NODE_TYPE "'')\n", ninfo->type);
657        close(p->fd);
658        p->fd = -1;
659        return NULL;
660      }
661
662      if (ninfo->hooks != 1) {
663        log_Printf(LogWARN, "Can't handle netgraph node with %d hooks\n",
664                   ninfo->hooks);
665        close(p->fd);
666        p->fd = -1;
667        return NULL;
668      }
669
670      /* Looks good.... lets allocate a device structure */
671      if ((dev = malloc(sizeof *dev)) == NULL) {
672        log_Printf(LogWARN, "%s: Cannot allocate an ether device: %s\n",
673                   p->link.name, strerror(errno));
674        return NULL;
675      }
676
677      dev->cs = -1;
678      dev->timeout = 0;
679      dev->connected = CARRIER_OK;
680      strncpy(dev->hook, hlist->link->ourhook, sizeof dev->hook - 1);
681      dev->hook[sizeof dev->hook - 1] = '\0';
682
683      log_Printf(LogDEBUG, "Using netgraph hook ``.:%s'' -> [%08x]:%s\n",
684                 dev->hook, hlist->link->nodeinfo.id, hlist->link->peerhook);
685    }
686  }
687
688  if (dev) {
689    memcpy(&dev->dev, &baseetherdevice, sizeof dev->dev);
690
691    /* Hook things up so that we monitor dev->cs */
692    p->desc.UpdateSet = ether_UpdateSet;
693    p->desc.IsSet = ether_IsSet;
694    p->desc.Read = ether_DescriptorRead;
695
696    physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNCNOACF);
697
698    /* Moan about (and fix) invalid LCP configurations */
699    if (p->link.lcp.cfg.mru > 1492) {
700      log_Printf(LogWARN, "%s: Reducing MRU to 1492\n", p->link.name);
701      p->link.lcp.cfg.mru = 1492;
702    }
703    if (p->dl->bundle->cfg.mtu > 1492) {
704      log_Printf(LogWARN, "%s: Reducing MTU to 1492\n", p->link.name);
705      p->dl->bundle->cfg.mtu = 1492;
706    }
707
708    return &dev->dev;
709  }
710
711  return NULL;
712}
713