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