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