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