xref: /freebsd/usr.sbin/ppp/ether.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1999 Brian Somers <brian@Awfulhak.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
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;
197   int sz = physical_MaxDeviceSize();
198 
199   iov[*niov].iov_base = d = realloc(d, sz);
200   if (d == 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   dev = device2ether(d);
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   const char *mode;
449   size_t ifacelen;
450   unsigned f;
451 
452   dev = NULL;
453   path = NULL;
454   ifacelen = 0;
455   if (p->fd < 0 && !strncasecmp(p->name.full, NG_PPPOE_NODE_TYPE,
456                                 PPPOE_NODE_TYPE_LEN) &&
457       p->name.full[PPPOE_NODE_TYPE_LEN] == ':') {
458     const struct linkinfo *nlink;
459     struct ngpppoe_init_data *data;
460     struct ngm_mkpeer mkp;
461     struct ngm_connect ngc;
462     const char *iface, *provider;
463     char etherid[12];
464     int providerlen;
465     char connectpath[sizeof dev->hook + 2];	/* .:<hook> */
466 
467     p->fd--;				/* We own the device - change fd */
468 
469     loadmodules(LOAD_VERBOSLY, "netgraph", "ng_ether", "ng_pppoe", "ng_socket",
470                 NULL);
471 
472     if ((dev = malloc(sizeof *dev)) == NULL)
473       return NULL;
474 
475     iface = p->name.full + PPPOE_NODE_TYPE_LEN + 1;
476 
477     provider = strchr(iface, ':');
478     if (provider) {
479       ifacelen = provider - iface;
480       provider++;
481       providerlen = strlen(provider);
482     } else {
483       ifacelen = strlen(iface);
484       provider = "";
485       providerlen = 0;
486     }
487 
488     /*
489      * We're going to do this (where tunN is our tunnel device):
490      *
491      * .---------.
492      * |  ether  |
493      * | <iface> |                         dev->cs
494      * `---------'                           |
495      *  (orphan)                     p->fd   |
496      *     |                           |     |
497      *     |                           |     |
498      * (ethernet)                      |     |
499      * .---------.                  .-----------.
500      * |  pppoe  |                  |  socket   |
501      * | <iface> |(tunN)<---->(tunN)| <unnamed> |
502      * `---------                   `-----------'
503      *   (tunX)
504      *     ^
505      *     |
506      *     `--->(tunX)
507      */
508 
509     /* Create a socket node */
510     if (ID0NgMkSockNode(NULL, &dev->cs, &p->fd) == -1) {
511       log_Printf(LogWARN, "Cannot create netgraph socket node: %s\n",
512                  strerror(errno));
513       free(dev);
514       p->fd = -2;
515       return NULL;
516     }
517 
518     /*
519      * Ask for a list of hooks attached to the "ether" node.  This node should
520      * magically exist as a way of hooking stuff onto an ethernet device
521      */
522     path = (char *)alloca(ifacelen + 2);
523     sprintf(path, "%.*s:", (int)ifacelen, iface);
524     if (NgSendMsg(dev->cs, path, NGM_GENERIC_COOKIE, NGM_LISTHOOKS,
525                   NULL, 0) < 0) {
526       log_Printf(LogWARN, "%s Cannot send a netgraph message: %s\n",
527                  path, strerror(errno));
528       return ether_Abandon(dev, p);
529     }
530 
531     /* Get our list back */
532     resp = (struct ng_mesg *)rbuf;
533     if (NgRecvMsg(dev->cs, resp, sizeof rbuf, NULL) <= 0) {
534       log_Printf(LogWARN, "Cannot get netgraph response: %s\n",
535                  strerror(errno));
536       return ether_Abandon(dev, p);
537     }
538 
539     hlist = (const struct hooklist *)resp->data;
540     ninfo = &hlist->nodeinfo;
541 
542     /* Make sure we've got the right type of node */
543     if (strncmp(ninfo->type, NG_ETHER_NODE_TYPE,
544                 sizeof NG_ETHER_NODE_TYPE - 1)) {
545       log_Printf(LogWARN, "%s Unexpected node type ``%s'' (wanted ``"
546                  NG_ETHER_NODE_TYPE "'')\n", path, ninfo->type);
547       return ether_Abandon(dev, p);
548     }
549 
550     log_Printf(LogDEBUG, "List of netgraph node ``%s'' (id %x) hooks:\n",
551                path, ninfo->id);
552 
553     /* look for a hook already attached.  */
554     for (f = 0; f < ninfo->hooks; f++) {
555       nlink = &hlist->link[f];
556 
557       log_Printf(LogDEBUG, "  Found %s -> %s\n", nlink->ourhook,
558                  nlink->peerhook);
559 
560       if (!strcmp(nlink->ourhook, NG_ETHER_HOOK_ORPHAN) ||
561           !strcmp(nlink->ourhook, NG_ETHER_HOOK_DIVERT)) {
562         /*
563          * Something is using the data coming out of this ``ether'' node.
564          * If it's a PPPoE node, we use that node, otherwise we complain that
565          * someone else is using the node.
566          */
567         if (!strcmp(nlink->nodeinfo.type, NG_PPPOE_NODE_TYPE))
568           /* Use this PPPoE node ! */
569           snprintf(ngc.path, sizeof ngc.path, "[%x]:", nlink->nodeinfo.id);
570         else {
571           log_Printf(LogWARN, "%s Node type ``%s'' is currently active\n",
572                      path, nlink->nodeinfo.type);
573           return ether_Abandon(dev, p);
574         }
575         break;
576       }
577     }
578 
579     if (f == ninfo->hooks) {
580       /*
581        * Create a new ``PPPoE'' node connected to the ``ether'' node using
582        * the ``orphan'' and ``ethernet'' hooks
583        */
584       snprintf(mkp.type, sizeof mkp.type, "%s", NG_PPPOE_NODE_TYPE);
585       snprintf(mkp.ourhook, sizeof mkp.ourhook, "%s", NG_ETHER_HOOK_ORPHAN);
586       snprintf(mkp.peerhook, sizeof mkp.peerhook, "%s", NG_PPPOE_HOOK_ETHERNET);
587       snprintf(etherid, sizeof etherid, "[%x]:", ninfo->id);
588 
589       log_Printf(LogDEBUG, "Creating PPPoE netgraph node %s%s -> %s\n",
590                  etherid, mkp.ourhook, mkp.peerhook);
591 
592       if (NgSendMsg(dev->cs, etherid, NGM_GENERIC_COOKIE,
593                     NGM_MKPEER, &mkp, sizeof mkp) < 0) {
594         log_Printf(LogWARN, "%s Cannot create PPPoE netgraph node: %s\n",
595                    etherid, strerror(errno));
596         return ether_Abandon(dev, p);
597       }
598 
599       snprintf(ngc.path, sizeof ngc.path, "%s%s", path, NG_ETHER_HOOK_ORPHAN);
600     }
601 
602     snprintf(dev->hook, sizeof dev->hook, "%s%d",
603              TUN_NAME, p->dl->bundle->unit);
604 
605     /*
606      * Connect the PPPoE node to our socket node.
607      * ngc.path has already been set up
608      */
609     snprintf(ngc.ourhook, sizeof ngc.ourhook, "%s", dev->hook);
610     memcpy(ngc.peerhook, ngc.ourhook, sizeof ngc.peerhook);
611 
612     log_Printf(LogDEBUG, "Connecting netgraph socket .:%s -> %s:%s\n",
613                ngc.ourhook, ngc.path, ngc.peerhook);
614     if (NgSendMsg(dev->cs, ".:", NGM_GENERIC_COOKIE,
615                   NGM_CONNECT, &ngc, sizeof ngc) < 0) {
616       log_Printf(LogWARN, "Cannot connect PPPoE and socket netgraph "
617                  "nodes: %s\n", strerror(errno));
618       return ether_Abandon(dev, p);
619     }
620 
621     /* Bring the Ethernet interface up */
622     path[ifacelen] = '\0';	/* Remove the trailing ':' */
623     if (!iface_SetFlags(path, IFF_UP))
624       log_Printf(LogWARN, "%s: Failed to set the IFF_UP flag on %s\n",
625                  p->link.name, path);
626 
627     snprintf(connectpath, sizeof connectpath, ".:%s", dev->hook);
628 
629     /* Configure node to 3Com mode if needed */
630     if (p->cfg.pppoe_configured) {
631       mode = p->cfg.nonstandard_pppoe ? NG_PPPOE_NONSTANDARD : NG_PPPOE_STANDARD;
632       if (NgSendMsg(dev->cs, connectpath, NGM_PPPOE_COOKIE,
633 		NGM_PPPOE_SETMODE, mode, strlen(mode) + 1) == -1) {
634         log_Printf(LogWARN, "``%s'': Cannot configure netgraph node: %s\n",
635                  connectpath, strerror(errno));
636         return ether_Abandon(dev, p);
637       }
638     }
639 
640     /* And finally, request a connection to the given provider */
641 
642     data = (struct ngpppoe_init_data *)alloca(sizeof *data + providerlen);
643     snprintf(data->hook, sizeof data->hook, "%s", dev->hook);
644     memcpy(data->data, provider, providerlen);
645     data->data_len = providerlen;
646 
647     log_Printf(LogDEBUG, "Sending PPPOE_CONNECT to %s\n", connectpath);
648     if (NgSendMsg(dev->cs, connectpath, NGM_PPPOE_COOKIE,
649                   NGM_PPPOE_CONNECT, data, sizeof *data + providerlen) == -1) {
650       log_Printf(LogWARN, "``%s'': Cannot start netgraph node: %s\n",
651                  connectpath, strerror(errno));
652       return ether_Abandon(dev, p);
653     }
654 
655     /* Hook things up so that we monitor dev->cs */
656     p->desc.UpdateSet = ether_UpdateSet;
657     p->desc.IsSet = ether_IsSet;
658     p->desc.Read = ether_DescriptorRead;
659 
660     memcpy(&dev->dev, &baseetherdevice, sizeof dev->dev);
661     switch (p->cfg.cd.necessity) {
662       case CD_VARIABLE:
663         dev->dev.cd.delay = p->cfg.cd.delay;
664         break;
665       case CD_REQUIRED:
666         dev->dev.cd = p->cfg.cd;
667         break;
668       case CD_NOTREQUIRED:
669         log_Printf(LogWARN, "%s: Carrier must be set, using ``set cd %d!''\n",
670                    p->link.name, dev->dev.cd.delay);
671       case CD_DEFAULT:
672         break;
673     }
674 
675     dev->timeout = dev->dev.cd.delay;
676     dev->connected = CARRIER_PENDING;
677     /* This will be overridden by our session id - if provided by netgraph */
678     dev->slot = GetIfIndex(path);
679   } else {
680     /* See if we're a netgraph socket */
681     struct stat st;
682 
683     if (fstat(p->fd, &st) != -1 && (st.st_mode & S_IFSOCK)) {
684       struct sockaddr_storage ssock;
685       struct sockaddr *sock = (struct sockaddr *)&ssock;
686       int sz;
687 
688       sz = sizeof ssock;
689       if (getsockname(p->fd, sock, &sz) == -1) {
690         log_Printf(LogPHASE, "%s: Link is a closed socket !\n", p->link.name);
691         close(p->fd);
692         p->fd = -1;
693         return NULL;
694       }
695 
696       if (sock->sa_family == AF_NETGRAPH) {
697         /*
698          * It's a netgraph node... We can't determine hook names etc, so we
699          * stay pretty impartial....
700          */
701         log_Printf(LogPHASE, "%s: Link is a netgraph node\n", p->link.name);
702 
703         if ((dev = malloc(sizeof *dev)) == NULL) {
704           log_Printf(LogWARN, "%s: Cannot allocate an ether device: %s\n",
705                      p->link.name, strerror(errno));
706           return NULL;
707         }
708 
709         memcpy(&dev->dev, &baseetherdevice, sizeof dev->dev);
710         dev->cs = -1;
711         dev->timeout = 0;
712         dev->connected = CARRIER_OK;
713         *dev->hook = '\0';
714 
715         /*
716          * If we're being envoked from pppoed(8), we may have a SESSIONID
717          * set in the environment.  If so, use it as the slot
718          */
719         if ((sessionid = getenv("SESSIONID")) != NULL) {
720           char *end;
721           u_long slot;
722 
723           slot = strtoul(sessionid, &end, 16);
724           dev->slot = end != sessionid && *end == '\0' ? slot : 0;
725         } else
726           dev->slot = 0;
727       }
728     }
729   }
730 
731   if (dev) {
732     physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNCNOACF);
733     return &dev->dev;
734   }
735 
736   return NULL;
737 }
738