]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - usr.sbin/ppp/ether.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.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 unsigned
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 : (ssize_t)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 __unused, 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
234   while (1) {
235     zerofdset(r);
236     FD_SET(dev->cs, r);
237     t.tv_sec = t.tv_usec = 0;
238     ret = select(dev->cs + 1, r, NULL, NULL, &t);
239
240     if (ret <= 0)
241       break;
242
243     if (NgRecvMsg(dev->cs, rep, sizeof msgbuf, NULL) <= 0)
244       break;
245
246     if (rep->header.version != NG_VERSION) {
247       log_Printf(LogWARN, "%ld: Unexpected netgraph version, expected %ld\n",
248                  (long)rep->header.version, (long)NG_VERSION);
249       break;
250     }
251
252     if (rep->header.typecookie != NGM_PPPOE_COOKIE) {
253       log_Printf(LogWARN, "%ld: Unexpected netgraph cookie, expected %ld\n",
254                  (long)rep->header.typecookie, (long)NGM_PPPOE_COOKIE);
255       break;
256     }
257
258     asciilen = 0;
259     switch (rep->header.cmd) {
260       case NGM_PPPOE_SET_FLAG:  msg = "SET_FLAG";       break;
261       case NGM_PPPOE_CONNECT:   msg = "CONNECT";        break;
262       case NGM_PPPOE_LISTEN:    msg = "LISTEN";         break;
263       case NGM_PPPOE_OFFER:     msg = "OFFER";          break;
264       case NGM_PPPOE_SUCCESS:   msg = "SUCCESS";        break;
265       case NGM_PPPOE_FAIL:      msg = "FAIL";           break;
266       case NGM_PPPOE_CLOSE:     msg = "CLOSE";          break;
267       case NGM_PPPOE_GET_STATUS:        msg = "GET_STATUS";     break;
268       case NGM_PPPOE_ACNAME:
269         msg = "ACNAME";
270         if (setenv("ACNAME", sts->hook, 1) != 0)
271           log_Printf(LogWARN, "setenv: cannot set ACNAME=%s: %m", sts->hook);
272         asciilen = rep->header.arglen;
273         break;
274       case NGM_PPPOE_SESSIONID:
275         msg = "SESSIONID";
276         snprintf(sessionid, sizeof sessionid, "%04x", *(u_int16_t *)sts);
277         if (setenv("SESSIONID", sessionid, 1) != 0)
278           syslog(LOG_WARNING, "setenv: cannot set SESSIONID=%s: %m",
279                  sessionid);
280         /* Use this in preference to our interface index */
281         slot = strtoul(sessionid, &end, 16);
282         if (end != sessionid && *end == '\0')
283             dev->slot = slot;
284         break;
285       default:
286         snprintf(unknown, sizeof unknown, "<%d>", (int)rep->header.cmd);
287         msg = unknown;
288         break;
289     }
290
291     if (asciilen)
292       log_Printf(LogPHASE, "Received NGM_PPPOE_%s (hook \"%.*s\")\n",
293                  msg, asciilen, sts->hook);
294     else
295       log_Printf(LogPHASE, "Received NGM_PPPOE_%s\n", msg);
296
297     switch (rep->header.cmd) {
298       case NGM_PPPOE_SUCCESS:
299         dev->connected = CARRIER_OK;
300         break;
301       case NGM_PPPOE_FAIL:
302       case NGM_PPPOE_CLOSE:
303         dev->connected = CARRIER_LOST;
304         break;
305     }
306   }
307   free(r);
308 }
309
310 static int
311 ether_AwaitCarrier(struct physical *p)
312 {
313   struct etherdevice *dev = device2ether(p->handler);
314
315   if (dev->connected != CARRIER_OK && !dev->timeout--)
316     dev->connected = CARRIER_LOST;
317   else if (dev->connected == CARRIER_PENDING)
318     ether_MessageIn(dev);
319
320   return dev->connected;
321 }
322
323 static const struct device baseetherdevice = {
324   ETHER_DEVICE,
325   "ether",
326   1492,
327   { CD_REQUIRED, DEF_ETHERCDDELAY },
328   ether_AwaitCarrier,
329   ether_RemoveFromSet,
330   NULL,
331   NULL,
332   NULL,
333   NULL,
334   NULL,
335   ether_Free,
336   ether_Read,
337   ether_Write,
338   ether_device2iov,
339   NULL,
340   ether_OpenInfo,
341   ether_Slot
342 };
343
344 struct device *
345 ether_iov2device(int type, struct physical *p, struct iovec *iov, int *niov,
346                  int maxiov __unused, int *auxfd, int *nauxfd)
347 {
348   if (type == ETHER_DEVICE) {
349     struct etherdevice *dev = (struct etherdevice *)iov[(*niov)++].iov_base;
350
351     dev = realloc(dev, sizeof *dev);    /* Reduce to the correct size */
352     if (dev == NULL) {
353       log_Printf(LogALERT, "Failed to allocate memory: %d\n",
354                  (int)(sizeof *dev));
355       AbortProgram(EX_OSERR);
356     }
357
358     if (*nauxfd) {
359       dev->cs = *auxfd;
360       (*nauxfd)--;
361     } else
362       dev->cs = -1;
363
364     /* Refresh function pointers etc */
365     memcpy(&dev->dev, &baseetherdevice, sizeof dev->dev);
366
367     physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNCNOACF);
368     return &dev->dev;
369   }
370
371   return NULL;
372 }
373
374 static int
375 ether_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
376 {
377   struct physical *p = descriptor2physical(d);
378   struct etherdevice *dev = device2ether(p->handler);
379   int result;
380
381   if (r && dev->cs >= 0) {
382     FD_SET(dev->cs, r);
383     log_Printf(LogTIMER, "%s(ctrl): fdset(r) %d\n", p->link.name, dev->cs);
384     result = 1;
385   } else
386     result = 0;
387
388   result += physical_doUpdateSet(d, r, w, e, n, 0);
389
390   return result;
391 }
392
393 static int
394 ether_IsSet(struct fdescriptor *d, const fd_set *fdset)
395 {
396   struct physical *p = descriptor2physical(d);
397   struct etherdevice *dev = device2ether(p->handler);
398   int result;
399
400   result = dev->cs >= 0 && FD_ISSET(dev->cs, fdset);
401   result += physical_IsSet(d, fdset);
402
403   return result;
404 }
405
406 static void
407 ether_DescriptorRead(struct fdescriptor *d, struct bundle *bundle,
408                      const fd_set *fdset)
409 {
410   struct physical *p = descriptor2physical(d);
411   struct etherdevice *dev = device2ether(p->handler);
412
413   if (dev->cs >= 0 && FD_ISSET(dev->cs, fdset)) {
414     ether_MessageIn(dev);
415     if (dev->connected == CARRIER_LOST) {
416       log_Printf(LogPHASE, "%s: Device disconnected\n", p->link.name);
417       datalink_Down(p->dl, CLOSE_NORMAL);
418       return;
419     }
420   }
421
422   if (physical_IsSet(d, fdset))
423     physical_DescriptorRead(d, bundle, fdset);
424 }
425
426 static struct device *
427 ether_Abandon(struct etherdevice *dev, struct physical *p)
428 {
429   /* Abandon our node construction */
430   close(dev->cs);
431   close(p->fd);
432   p->fd = -2;   /* Nobody else need try.. */
433   free(dev);
434
435   return NULL;
436 }
437
438 struct device *
439 ether_Create(struct physical *p)
440 {
441   u_char rbuf[2048];
442   struct etherdevice *dev;
443   struct ng_mesg *resp;
444   const struct hooklist *hlist;
445   const struct nodeinfo *ninfo;
446   char *path, *sessionid;
447   const char *mode;
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     snprintf(connectpath, sizeof connectpath, ".:%s", dev->hook);
627
628     /* Configure node to 3Com mode if needed */
629     if (p->cfg.pppoe_configured) {
630       mode = p->cfg.nonstandard_pppoe ? NG_PPPOE_NONSTANDARD : NG_PPPOE_STANDARD;
631       if (NgSendMsg(dev->cs, connectpath, NGM_PPPOE_COOKIE,
632                 NGM_PPPOE_SETMODE, mode, strlen(mode) + 1) == -1) {
633         log_Printf(LogWARN, "``%s'': Cannot configure netgraph node: %s\n",
634                  connectpath, strerror(errno));
635         return ether_Abandon(dev, p);
636       }
637     }
638
639     /* And finally, request a connection to the given provider */
640
641     data = (struct ngpppoe_init_data *)alloca(sizeof *data + providerlen);
642     snprintf(data->hook, sizeof data->hook, "%s", dev->hook);
643     memcpy(data->data, provider, providerlen);
644     data->data_len = providerlen;
645
646     log_Printf(LogDEBUG, "Sending PPPOE_CONNECT to %s\n", connectpath);
647     if (NgSendMsg(dev->cs, connectpath, NGM_PPPOE_COOKIE,
648                   NGM_PPPOE_CONNECT, data, sizeof *data + providerlen) == -1) {
649       log_Printf(LogWARN, "``%s'': Cannot start netgraph node: %s\n",
650                  connectpath, strerror(errno));
651       return ether_Abandon(dev, p);
652     }
653
654     /* Hook things up so that we monitor dev->cs */
655     p->desc.UpdateSet = ether_UpdateSet;
656     p->desc.IsSet = ether_IsSet;
657     p->desc.Read = ether_DescriptorRead;
658
659     memcpy(&dev->dev, &baseetherdevice, sizeof dev->dev);
660     switch (p->cfg.cd.necessity) {
661       case CD_VARIABLE:
662         dev->dev.cd.delay = p->cfg.cd.delay;
663         break;
664       case CD_REQUIRED:
665         dev->dev.cd = p->cfg.cd;
666         break;
667       case CD_NOTREQUIRED:
668         log_Printf(LogWARN, "%s: Carrier must be set, using ``set cd %d!''\n",
669                    p->link.name, dev->dev.cd.delay);
670       case CD_DEFAULT:
671         break;
672     }
673
674     dev->timeout = dev->dev.cd.delay;
675     dev->connected = CARRIER_PENDING;
676     /* This will be overridden by our session id - if provided by netgraph */
677     dev->slot = GetIfIndex(path);
678   } else {
679     /* See if we're a netgraph socket */
680     struct stat st;
681
682     if (fstat(p->fd, &st) != -1 && (st.st_mode & S_IFSOCK)) {
683       struct sockaddr_storage ssock;
684       struct sockaddr *sock = (struct sockaddr *)&ssock;
685       int sz;
686
687       sz = sizeof ssock;
688       if (getsockname(p->fd, sock, &sz) == -1) {
689         log_Printf(LogPHASE, "%s: Link is a closed socket !\n", p->link.name);
690         close(p->fd);
691         p->fd = -1;
692         return NULL;
693       }
694
695       if (sock->sa_family == AF_NETGRAPH) {
696         /*
697          * It's a netgraph node... We can't determine hook names etc, so we
698          * stay pretty impartial....
699          */
700         log_Printf(LogPHASE, "%s: Link is a netgraph node\n", p->link.name);
701
702         if ((dev = malloc(sizeof *dev)) == NULL) {
703           log_Printf(LogWARN, "%s: Cannot allocate an ether device: %s\n",
704                      p->link.name, strerror(errno));
705           return NULL;
706         }
707
708         memcpy(&dev->dev, &baseetherdevice, sizeof dev->dev);
709         dev->cs = -1;
710         dev->timeout = 0;
711         dev->connected = CARRIER_OK;
712         *dev->hook = '\0';
713
714         /*
715          * If we're being envoked from pppoed(8), we may have a SESSIONID
716          * set in the environment.  If so, use it as the slot
717          */
718         if ((sessionid = getenv("SESSIONID")) != NULL) {
719           char *end;
720           u_long slot;
721
722           slot = strtoul(sessionid, &end, 16);
723           dev->slot = end != sessionid && *end == '\0' ? slot : 0;
724         } else
725           dev->slot = 0;
726       }
727     }
728   }
729
730   if (dev) {
731     physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNCNOACF);
732     return &dev->dev;
733   }
734
735   return NULL;
736 }