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