]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/ppp/ether.c
This commit was generated by cvs2svn to compensate for changes in r98841,
[FreeBSD/FreeBSD.git] / usr.sbin / ppp / ether.c
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$
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
101 struct 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
113 int
114 ether_DeviceSize(void)
115 {
116   return sizeof(struct etherdevice);
117 }
118
119 static ssize_t
120 ether_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
127 static ssize_t
128 ether_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
135 static int
136 ether_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
157 static void
158 ether_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
168 static const char *
169 ether_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
183 static int
184 ether_Slot(struct physical *p)
185 {
186   struct etherdevice *dev = device2ether(p->handler);
187
188   return dev->slot;
189 }
190
191
192 static void
193 ether_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
213 static void
214 ether_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 asciilen, 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   asciilen = 0;
258   switch (rep->header.cmd) {
259     case NGM_PPPOE_SET_FLAG:    msg = "SET_FLAG";       break;
260     case NGM_PPPOE_CONNECT:     msg = "CONNECT";        break;
261     case NGM_PPPOE_LISTEN:      msg = "LISTEN";         break;
262     case NGM_PPPOE_OFFER:       msg = "OFFER";          break;
263     case NGM_PPPOE_SUCCESS:     msg = "SUCCESS";        break;
264     case NGM_PPPOE_FAIL:        msg = "FAIL";           break;
265     case NGM_PPPOE_CLOSE:       msg = "CLOSE";          break;
266     case NGM_PPPOE_GET_STATUS:  msg = "GET_STATUS";     break;
267     case NGM_PPPOE_ACNAME:
268       msg = "ACNAME";
269       if (setenv("ACNAME", sts->hook, 1) != 0)
270         log_Printf(LogWARN, "setenv: cannot set ACNAME=%s: %m", sts->hook);
271       asciilen = rep->header.arglen;
272       break;
273     case NGM_PPPOE_SESSIONID:
274       msg = "SESSIONID";
275       snprintf(sessionid, sizeof sessionid, "%04x", *(u_int16_t *)sts);
276       if (setenv("SESSIONID", sessionid, 1) != 0)
277         syslog(LOG_WARNING, "setenv: cannot set SESSIONID=%s: %m",
278                sessionid);
279       /* Use this in preference to our interface index */
280       slot = strtoul(sessionid, &end, 16);
281       if (end != sessionid && *end == '\0')
282           dev->slot = slot;
283       break;
284     default:
285       snprintf(unknown, sizeof unknown, "<%d>", (int)rep->header.cmd);
286       msg = unknown;
287       break;
288   }
289
290   if (asciilen)
291     log_Printf(LogPHASE, "Received NGM_PPPOE_%s (hook \"%.*s\")\n",
292                msg, asciilen, sts->hook);
293   else
294     log_Printf(LogPHASE, "Received NGM_PPPOE_%s\n", msg);
295
296   switch (rep->header.cmd) {
297     case NGM_PPPOE_SUCCESS:
298       dev->connected = CARRIER_OK;
299       break;
300     case NGM_PPPOE_FAIL:
301     case NGM_PPPOE_CLOSE:
302       dev->connected = CARRIER_LOST;
303       break;
304   }
305 }
306
307 static int
308 ether_AwaitCarrier(struct physical *p)
309 {
310   struct etherdevice *dev = device2ether(p->handler);
311
312   if (dev->connected != CARRIER_OK && !dev->timeout--)
313     dev->connected = CARRIER_LOST;
314   else if (dev->connected == CARRIER_PENDING)
315     ether_MessageIn(dev);
316
317   return dev->connected;
318 }
319
320 static const struct device baseetherdevice = {
321   ETHER_DEVICE,
322   "ether",
323   1492,
324   { CD_REQUIRED, DEF_ETHERCDDELAY },
325   ether_AwaitCarrier,
326   ether_RemoveFromSet,
327   NULL,
328   NULL,
329   NULL,
330   NULL,
331   NULL,
332   ether_Free,
333   ether_Read,
334   ether_Write,
335   ether_device2iov,
336   NULL,
337   ether_OpenInfo,
338   ether_Slot
339 };
340
341 struct device *
342 ether_iov2device(int type, struct physical *p, struct iovec *iov, int *niov,
343                  int maxiov, int *auxfd, int *nauxfd)
344 {
345   if (type == ETHER_DEVICE) {
346     struct etherdevice *dev = (struct etherdevice *)iov[(*niov)++].iov_base;
347
348     dev = realloc(dev, sizeof *dev);    /* Reduce to the correct size */
349     if (dev == NULL) {
350       log_Printf(LogALERT, "Failed to allocate memory: %d\n",
351                  (int)(sizeof *dev));
352       AbortProgram(EX_OSERR);
353     }
354
355     if (*nauxfd) {
356       dev->cs = *auxfd;
357       (*nauxfd)--;
358     } else
359       dev->cs = -1;
360
361     /* Refresh function pointers etc */
362     memcpy(&dev->dev, &baseetherdevice, sizeof dev->dev);
363
364     physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNCNOACF);
365     return &dev->dev;
366   }
367
368   return NULL;
369 }
370
371 static int
372 ether_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
373 {
374   struct physical *p = descriptor2physical(d);
375   struct etherdevice *dev = device2ether(p->handler);
376   int result;
377
378   if (r && dev->cs >= 0) {
379     FD_SET(dev->cs, r);
380     log_Printf(LogTIMER, "%s(ctrl): fdset(r) %d\n", p->link.name, dev->cs);
381     result = 1;
382   } else
383     result = 0;
384
385   result += physical_doUpdateSet(d, r, w, e, n, 0);
386
387   return result;
388 }
389
390 static int
391 ether_IsSet(struct fdescriptor *d, const fd_set *fdset)
392 {
393   struct physical *p = descriptor2physical(d);
394   struct etherdevice *dev = device2ether(p->handler);
395   int result;
396
397   result = dev->cs >= 0 && FD_ISSET(dev->cs, fdset);
398   result += physical_IsSet(d, fdset);
399
400   return result;
401 }
402
403 static void
404 ether_DescriptorRead(struct fdescriptor *d, struct bundle *bundle,
405                      const fd_set *fdset)
406 {
407   struct physical *p = descriptor2physical(d);
408   struct etherdevice *dev = device2ether(p->handler);
409
410   if (dev->cs >= 0 && FD_ISSET(dev->cs, fdset)) {
411     ether_MessageIn(dev);
412     if (dev->connected == CARRIER_LOST) {
413       log_Printf(LogPHASE, "%s: Device disconnected\n", p->link.name);
414       datalink_Down(p->dl, CLOSE_NORMAL);
415       return;
416     }
417   }
418
419   if (physical_IsSet(d, fdset))
420     physical_DescriptorRead(d, bundle, fdset);
421 }
422
423 static struct device *
424 ether_Abandon(struct etherdevice *dev, struct physical *p)
425 {
426   /* Abandon our node construction */
427   close(dev->cs);
428   close(p->fd);
429   p->fd = -2;   /* Nobody else need try.. */
430   free(dev);
431
432   return NULL;
433 }
434
435 struct device *
436 ether_Create(struct physical *p)
437 {
438   u_char rbuf[2048];
439   struct etherdevice *dev;
440   struct ng_mesg *resp;
441   const struct hooklist *hlist;
442   const struct nodeinfo *ninfo;
443   char *path, *sessionid;
444   int ifacelen, f;
445
446   dev = NULL;
447   path = NULL;
448   ifacelen = 0;
449   if (p->fd < 0 && !strncasecmp(p->name.full, NG_PPPOE_NODE_TYPE,
450                                 PPPOE_NODE_TYPE_LEN) &&
451       p->name.full[PPPOE_NODE_TYPE_LEN] == ':') {
452     const struct linkinfo *nlink;
453     struct ngpppoe_init_data *data;
454     struct ngm_mkpeer mkp;
455     struct ngm_connect ngc;
456     const char *iface, *provider;
457     char etherid[12];
458     int providerlen;
459     char connectpath[sizeof dev->hook + 2];     /* .:<hook> */
460
461     p->fd--;                            /* We own the device - change fd */
462
463     loadmodules(LOAD_VERBOSLY, "netgraph", "ng_ether", "ng_pppoe", "ng_socket",
464                 NULL);
465
466     if ((dev = malloc(sizeof *dev)) == NULL)
467       return NULL;
468
469     iface = p->name.full + PPPOE_NODE_TYPE_LEN + 1;
470
471     provider = strchr(iface, ':');
472     if (provider) {
473       ifacelen = provider - iface;
474       provider++;
475       providerlen = strlen(provider);
476     } else {
477       ifacelen = strlen(iface);
478       provider = "";
479       providerlen = 0;
480     }
481
482     /*
483      * We're going to do this (where tunN is our tunnel device):
484      *
485      * .---------.
486      * |  ether  |
487      * | <iface> |                         dev->cs
488      * `---------'                           |
489      *  (orphan)                     p->fd   |
490      *     |                           |     |
491      *     |                           |     |
492      * (ethernet)                      |     |
493      * .---------.                  .-----------.
494      * |  pppoe  |                  |  socket   |
495      * | <iface> |(tunN)<---->(tunN)| <unnamed> |
496      * `---------                   `-----------'
497      *   (tunX)
498      *     ^
499      *     |
500      *     `--->(tunX)
501      */
502
503     /* Create a socket node */
504     if (ID0NgMkSockNode(NULL, &dev->cs, &p->fd) == -1) {
505       log_Printf(LogWARN, "Cannot create netgraph socket node: %s\n",
506                  strerror(errno));
507       free(dev);
508       p->fd = -2;
509       return NULL;
510     }
511
512     /*
513      * Ask for a list of hooks attached to the "ether" node.  This node should
514      * magically exist as a way of hooking stuff onto an ethernet device
515      */
516     path = (char *)alloca(ifacelen + 2);
517     sprintf(path, "%.*s:", ifacelen, iface);
518     if (NgSendMsg(dev->cs, path, NGM_GENERIC_COOKIE, NGM_LISTHOOKS,
519                   NULL, 0) < 0) {
520       log_Printf(LogWARN, "%s Cannot send a netgraph message: %s\n",
521                  path, strerror(errno));
522       return ether_Abandon(dev, p);
523     }
524
525     /* Get our list back */
526     resp = (struct ng_mesg *)rbuf;
527     if (NgRecvMsg(dev->cs, resp, sizeof rbuf, NULL) <= 0) {
528       log_Printf(LogWARN, "Cannot get netgraph response: %s\n",
529                  strerror(errno));
530       return ether_Abandon(dev, p);
531     }
532
533     hlist = (const struct hooklist *)resp->data;
534     ninfo = &hlist->nodeinfo;
535
536     /* Make sure we've got the right type of node */
537     if (strncmp(ninfo->type, NG_ETHER_NODE_TYPE,
538                 sizeof NG_ETHER_NODE_TYPE - 1)) {
539       log_Printf(LogWARN, "%s Unexpected node type ``%s'' (wanted ``"
540                  NG_ETHER_NODE_TYPE "'')\n", path, ninfo->type);
541       return ether_Abandon(dev, p);
542     }
543
544     log_Printf(LogDEBUG, "List of netgraph node ``%s'' (id %x) hooks:\n",
545                path, ninfo->id);
546
547     /* look for a hook already attached.  */
548     for (f = 0; f < ninfo->hooks; f++) {
549       nlink = &hlist->link[f];
550
551       log_Printf(LogDEBUG, "  Found %s -> %s\n", nlink->ourhook,
552                  nlink->peerhook);
553
554       if (!strcmp(nlink->ourhook, NG_ETHER_HOOK_ORPHAN) ||
555           !strcmp(nlink->ourhook, NG_ETHER_HOOK_DIVERT)) {
556         /*
557          * Something is using the data coming out of this ``ether'' node.
558          * If it's a PPPoE node, we use that node, otherwise we complain that
559          * someone else is using the node.
560          */
561         if (!strcmp(nlink->nodeinfo.type, NG_PPPOE_NODE_TYPE))
562           /* Use this PPPoE node ! */
563           snprintf(ngc.path, sizeof ngc.path, "[%x]:", nlink->nodeinfo.id);
564         else {
565           log_Printf(LogWARN, "%s Node type ``%s'' is currently active\n",
566                      path, nlink->nodeinfo.type);
567           return ether_Abandon(dev, p);
568         }
569         break;
570       }
571     }
572
573     if (f == ninfo->hooks) {
574       /*
575        * Create a new ``PPPoE'' node connected to the ``ether'' node using
576        * the ``orphan'' and ``ethernet'' hooks
577        */
578       snprintf(mkp.type, sizeof mkp.type, "%s", NG_PPPOE_NODE_TYPE);
579       snprintf(mkp.ourhook, sizeof mkp.ourhook, "%s", NG_ETHER_HOOK_ORPHAN);
580       snprintf(mkp.peerhook, sizeof mkp.peerhook, "%s", NG_PPPOE_HOOK_ETHERNET);
581       snprintf(etherid, sizeof etherid, "[%x]:", ninfo->id);
582
583       log_Printf(LogDEBUG, "Creating PPPoE netgraph node %s%s -> %s\n",
584                  etherid, mkp.ourhook, mkp.peerhook);
585
586       if (NgSendMsg(dev->cs, etherid, NGM_GENERIC_COOKIE,
587                     NGM_MKPEER, &mkp, sizeof mkp) < 0) {
588         log_Printf(LogWARN, "%s Cannot create PPPoE netgraph node: %s\n",
589                    etherid, strerror(errno));
590         return ether_Abandon(dev, p);
591       }
592
593       snprintf(ngc.path, sizeof ngc.path, "%s%s", path, NG_ETHER_HOOK_ORPHAN);
594     }
595
596     snprintf(dev->hook, sizeof dev->hook, "%s%d",
597              TUN_NAME, p->dl->bundle->unit);
598
599     /*
600      * Connect the PPPoE node to our socket node.
601      * ngc.path has already been set up
602      */
603     snprintf(ngc.ourhook, sizeof ngc.ourhook, "%s", dev->hook);
604     memcpy(ngc.peerhook, ngc.ourhook, sizeof ngc.peerhook);
605
606     log_Printf(LogDEBUG, "Connecting netgraph socket .:%s -> %s:%s\n",
607                ngc.ourhook, ngc.path, ngc.peerhook);
608     if (NgSendMsg(dev->cs, ".:", NGM_GENERIC_COOKIE,
609                   NGM_CONNECT, &ngc, sizeof ngc) < 0) {
610       log_Printf(LogWARN, "Cannot connect PPPoE and socket netgraph "
611                  "nodes: %s\n", strerror(errno));
612       return ether_Abandon(dev, p);
613     }
614
615     /* Bring the Ethernet interface up */
616     path[ifacelen] = '\0';      /* Remove the trailing ':' */
617     if (!iface_SetFlags(path, IFF_UP))
618       log_Printf(LogWARN, "%s: Failed to set the IFF_UP flag on %s\n",
619                  p->link.name, path);
620
621     /* And finally, request a connection to the given provider */
622
623     data = (struct ngpppoe_init_data *)alloca(sizeof *data + providerlen);
624     snprintf(data->hook, sizeof data->hook, "%s", dev->hook);
625     memcpy(data->data, provider, providerlen);
626     data->data_len = providerlen;
627
628     snprintf(connectpath, sizeof connectpath, ".:%s", dev->hook);
629     log_Printf(LogDEBUG, "Sending PPPOE_CONNECT to %s\n", connectpath);
630     if (NgSendMsg(dev->cs, connectpath, NGM_PPPOE_COOKIE,
631                   NGM_PPPOE_CONNECT, data, sizeof *data + providerlen) == -1) {
632       log_Printf(LogWARN, "``%s'': Cannot start netgraph node: %s\n",
633                  connectpath, strerror(errno));
634       return ether_Abandon(dev, p);
635     }
636
637     /* Hook things up so that we monitor dev->cs */
638     p->desc.UpdateSet = ether_UpdateSet;
639     p->desc.IsSet = ether_IsSet;
640     p->desc.Read = ether_DescriptorRead;
641
642     memcpy(&dev->dev, &baseetherdevice, sizeof dev->dev);
643     switch (p->cfg.cd.necessity) {
644       case CD_VARIABLE:
645         dev->dev.cd.delay = p->cfg.cd.delay;
646         break;
647       case CD_REQUIRED:
648         dev->dev.cd = p->cfg.cd;
649         break;
650       case CD_NOTREQUIRED:
651         log_Printf(LogWARN, "%s: Carrier must be set, using ``set cd %d!''\n",
652                    p->link.name, dev->dev.cd.delay);
653       case CD_DEFAULT:
654         break;
655     }
656
657     dev->timeout = dev->dev.cd.delay;
658     dev->connected = CARRIER_PENDING;
659     /* This will be overridden by our session id - if provided by netgraph */
660     dev->slot = GetIfIndex(path);
661   } else {
662     /* See if we're a netgraph socket */
663     struct stat st;
664
665     if (fstat(p->fd, &st) != -1 && (st.st_mode & S_IFSOCK)) {
666       struct sockaddr_storage ssock;
667       struct sockaddr *sock = (struct sockaddr *)&ssock;
668       int sz;
669
670       sz = sizeof ssock;
671       if (getsockname(p->fd, sock, &sz) == -1) {
672         log_Printf(LogPHASE, "%s: Link is a closed socket !\n", p->link.name);
673         close(p->fd);
674         p->fd = -1;
675         return NULL;
676       }
677
678       if (sock->sa_family == AF_NETGRAPH) {
679         /*
680          * It's a netgraph node... We can't determine hook names etc, so we
681          * stay pretty impartial....
682          */
683         log_Printf(LogPHASE, "%s: Link is a netgraph node\n", p->link.name);
684
685         if ((dev = malloc(sizeof *dev)) == NULL) {
686           log_Printf(LogWARN, "%s: Cannot allocate an ether device: %s\n",
687                      p->link.name, strerror(errno));
688           return NULL;
689         }
690
691         memcpy(&dev->dev, &baseetherdevice, sizeof dev->dev);
692         dev->cs = -1;
693         dev->timeout = 0;
694         dev->connected = CARRIER_OK;
695         *dev->hook = '\0';
696
697         /*
698          * If we're being envoked from pppoed(8), we may have a SESSIONID
699          * set in the environment.  If so, use it as the slot
700          */
701         if ((sessionid = getenv("SESSIONID")) != NULL) {
702           char *end;
703           u_long slot;
704
705           slot = strtoul(sessionid, &end, 16);
706           dev->slot = end != sessionid && *end == '\0' ? slot : 0;
707         } else
708           dev->slot = 0;
709       }
710     }
711   }
712
713   if (dev) {
714     physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNCNOACF);
715     return &dev->dev;
716   }
717
718   return NULL;
719 }