xref: /freebsd/usr.sbin/ppp/ether.c (revision ce4946daa5ce852d28008dac492029500ab2ee95)
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 "ipcp.h"
87 #include "filter.h"
88 #ifndef NORADIUS
89 #include "radius.h"
90 #endif
91 #include "bundle.h"
92 #include "id.h"
93 #include "iface.h"
94 #include "ether.h"
95 
96 
97 #define PPPOE_NODE_TYPE_LEN (sizeof NG_PPPOE_NODE_TYPE - 1) /* "PPPoE" */
98 
99 struct etherdevice {
100   struct device dev;			/* What struct physical knows about */
101   int cs;				/* Control socket */
102   int connected;			/* Are we connected yet ? */
103   int timeout;				/* Seconds attempting to connect */
104   char hook[sizeof TUN_NAME + 11];	/* Our socket node hook */
105 };
106 
107 #define device2ether(d) \
108   ((d)->type == ETHER_DEVICE ? (struct etherdevice *)d : NULL)
109 
110 int
111 ether_DeviceSize(void)
112 {
113   return sizeof(struct etherdevice);
114 }
115 
116 static ssize_t
117 ether_Write(struct physical *p, const void *v, size_t n)
118 {
119   struct etherdevice *dev = device2ether(p->handler);
120 
121   return NgSendData(p->fd, dev->hook, v, n) == -1 ? -1 : n;
122 }
123 
124 static ssize_t
125 ether_Read(struct physical *p, void *v, size_t n)
126 {
127   char hook[sizeof TUN_NAME + 11];
128 
129   return NgRecvData(p->fd, v, n, hook);
130 }
131 
132 static int
133 ether_RemoveFromSet(struct physical *p, fd_set *r, fd_set *w, fd_set *e)
134 {
135   struct etherdevice *dev = device2ether(p->handler);
136   int result;
137 
138   if (r && dev->cs >= 0 && FD_ISSET(dev->cs, r)) {
139     FD_CLR(dev->cs, r);
140     log_Printf(LogTIMER, "%s: fdunset(ctrl) %d\n", p->link.name, dev->cs);
141     result = 1;
142   } else
143     result = 0;
144 
145   /* Careful... physical_RemoveFromSet() called us ! */
146 
147   p->handler->removefromset = NULL;
148   result += physical_RemoveFromSet(p, r, w, e);
149   p->handler->removefromset = ether_RemoveFromSet;
150 
151   return result;
152 }
153 
154 static void
155 ether_Free(struct physical *p)
156 {
157   struct etherdevice *dev = device2ether(p->handler);
158 
159   physical_SetDescriptor(p);
160   if (dev->cs != -1)
161     close(dev->cs);
162   free(dev);
163 }
164 
165 static const char *
166 ether_OpenInfo(struct physical *p)
167 {
168   struct etherdevice *dev = device2ether(p->handler);
169 
170   switch (dev->connected) {
171     case CARRIER_PENDING:
172       return "negotiating";
173     case CARRIER_OK:
174       return "established";
175   }
176 
177   return "disconnected";
178 }
179 
180 static void
181 ether_device2iov(struct device *d, struct iovec *iov, int *niov,
182                  int maxiov, int *auxfd, int *nauxfd)
183 {
184   struct etherdevice *dev = device2ether(d);
185   int sz = physical_MaxDeviceSize();
186 
187   iov[*niov].iov_base = realloc(d, sz);
188   if (iov[*niov].iov_base == NULL) {
189     log_Printf(LogALERT, "Failed to allocate memory: %d\n", sz);
190     AbortProgram(EX_OSERR);
191   }
192   iov[*niov].iov_len = sz;
193   (*niov)++;
194 
195   if (dev->cs >= 0) {
196     *auxfd = dev->cs;
197     (*nauxfd)++;
198   }
199 }
200 
201 static void
202 ether_MessageIn(struct etherdevice *dev)
203 {
204   char msgbuf[sizeof(struct ng_mesg) + sizeof(struct ngpppoe_sts)];
205   struct ng_mesg *rep = (struct ng_mesg *)msgbuf;
206   struct ngpppoe_sts *sts = (struct ngpppoe_sts *)(msgbuf + sizeof *rep);
207   char unknown[14];
208   const char *msg;
209   struct timeval t;
210   fd_set *r;
211   int ret;
212 
213   if (dev->cs < 0)
214     return;
215 
216   if ((r = mkfdset()) == NULL) {
217     log_Printf(LogERROR, "DoLoop: Cannot create fd_set\n");
218     return;
219   }
220   zerofdset(r);
221   FD_SET(dev->cs, r);
222   t.tv_sec = t.tv_usec = 0;
223   ret = select(dev->cs + 1, r, NULL, NULL, &t);
224   free(r);
225 
226   if (ret <= 0)
227     return;
228 
229   if (NgRecvMsg(dev->cs, rep, sizeof msgbuf, NULL) < 0)
230     return;
231 
232   if (rep->header.version != NG_VERSION) {
233     log_Printf(LogWARN, "%ld: Unexpected netgraph version, expected %ld\n",
234                (long)rep->header.version, (long)NG_VERSION);
235     return;
236   }
237 
238   if (rep->header.typecookie != NGM_PPPOE_COOKIE) {
239     log_Printf(LogWARN, "%ld: Unexpected netgraph cookie, expected %ld\n",
240                (long)rep->header.typecookie, (long)NGM_PPPOE_COOKIE);
241     return;
242   }
243 
244   switch (rep->header.cmd) {
245     case NGM_PPPOE_SET_FLAG:	msg = "SET_FLAG";	break;
246     case NGM_PPPOE_CONNECT:	msg = "CONNECT";	break;
247     case NGM_PPPOE_LISTEN:	msg = "LISTEN";		break;
248     case NGM_PPPOE_OFFER:	msg = "OFFER";		break;
249     case NGM_PPPOE_SUCCESS:	msg = "SUCCESS";	break;
250     case NGM_PPPOE_FAIL:	msg = "FAIL";		break;
251     case NGM_PPPOE_CLOSE:	msg = "CLOSE";		break;
252     case NGM_PPPOE_GET_STATUS:	msg = "GET_STATUS";	break;
253     default:
254       snprintf(unknown, sizeof unknown, "<%d>", (int)rep->header.cmd);
255       msg = unknown;
256       break;
257   }
258 
259   log_Printf(LogPHASE, "Received NGM_PPPOE_%s (hook \"%s\")\n", msg, sts->hook);
260 
261   switch (rep->header.cmd) {
262     case NGM_PPPOE_SUCCESS:
263       dev->connected = CARRIER_OK;
264       break;
265     case NGM_PPPOE_FAIL:
266     case NGM_PPPOE_CLOSE:
267       dev->connected = CARRIER_LOST;
268       break;
269   }
270 }
271 
272 static int
273 ether_AwaitCarrier(struct physical *p)
274 {
275   struct etherdevice *dev = device2ether(p->handler);
276 
277   if (dev->connected != CARRIER_OK && !dev->timeout--)
278     dev->connected = CARRIER_LOST;
279   else if (dev->connected == CARRIER_PENDING)
280     ether_MessageIn(dev);
281 
282   return dev->connected;
283 }
284 
285 static const struct device baseetherdevice = {
286   ETHER_DEVICE,
287   "ether",
288   { CD_REQUIRED, DEF_ETHERCDDELAY },
289   ether_AwaitCarrier,
290   ether_RemoveFromSet,
291   NULL,
292   NULL,
293   NULL,
294   NULL,
295   ether_Free,
296   ether_Read,
297   ether_Write,
298   ether_device2iov,
299   NULL,
300   ether_OpenInfo
301 };
302 
303 struct device *
304 ether_iov2device(int type, struct physical *p, struct iovec *iov, int *niov,
305                  int maxiov, int *auxfd, int *nauxfd)
306 {
307   if (type == ETHER_DEVICE) {
308     struct etherdevice *dev = (struct etherdevice *)iov[(*niov)++].iov_base;
309 
310     dev = realloc(dev, sizeof *dev);	/* Reduce to the correct size */
311     if (dev == NULL) {
312       log_Printf(LogALERT, "Failed to allocate memory: %d\n",
313                  (int)(sizeof *dev));
314       AbortProgram(EX_OSERR);
315     }
316 
317     if (*nauxfd) {
318       dev->cs = *auxfd;
319       (*nauxfd)--;
320     } else
321       dev->cs = -1;
322 
323     /* Refresh function pointers etc */
324     memcpy(&dev->dev, &baseetherdevice, sizeof dev->dev);
325 
326     physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNCNOACF);
327     return &dev->dev;
328   }
329 
330   return NULL;
331 }
332 
333 static int
334 ether_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
335 {
336   struct physical *p = descriptor2physical(d);
337   struct etherdevice *dev = device2ether(p->handler);
338   int result;
339 
340   if (r && dev->cs >= 0) {
341     FD_SET(dev->cs, r);
342     log_Printf(LogTIMER, "%s(ctrl): fdset(r) %d\n", p->link.name, dev->cs);
343     result = 1;
344   } else
345     result = 0;
346 
347   result += physical_doUpdateSet(d, r, w, e, n, 0);
348 
349   return result;
350 }
351 
352 static int
353 ether_IsSet(struct fdescriptor *d, const fd_set *fdset)
354 {
355   struct physical *p = descriptor2physical(d);
356   struct etherdevice *dev = device2ether(p->handler);
357   int result;
358 
359   result = dev->cs >= 0 && FD_ISSET(dev->cs, fdset);
360   result += physical_IsSet(d, fdset);
361 
362   return result;
363 }
364 
365 static void
366 ether_DescriptorRead(struct fdescriptor *d, struct bundle *bundle,
367                      const fd_set *fdset)
368 {
369   struct physical *p = descriptor2physical(d);
370   struct etherdevice *dev = device2ether(p->handler);
371 
372   if (dev->cs >= 0 && FD_ISSET(dev->cs, fdset)) {
373     ether_MessageIn(dev);
374     if (dev->connected == CARRIER_LOST) {
375       log_Printf(LogPHASE, "%s: Device disconnected\n", p->link.name);
376       datalink_Down(p->dl, CLOSE_NORMAL);
377       return;
378     }
379   }
380 
381   if (physical_IsSet(d, fdset))
382     physical_DescriptorRead(d, bundle, fdset);
383 }
384 
385 static struct device *
386 ether_Abandon(struct etherdevice *dev, struct physical *p)
387 {
388   /* Abandon our node construction */
389   close(dev->cs);
390   close(p->fd);
391   p->fd = -2;	/* Nobody else need try.. */
392   free(dev);
393 
394   return NULL;
395 }
396 
397 struct device *
398 ether_Create(struct physical *p)
399 {
400   u_char rbuf[2048];
401   struct etherdevice *dev;
402   struct ng_mesg *resp;
403   const struct hooklist *hlist;
404   const struct nodeinfo *ninfo;
405   char *path;
406   int ifacelen, f;
407 
408   dev = NULL;
409   path = NULL;
410   ifacelen = 0;
411   if (p->fd < 0 && !strncasecmp(p->name.full, NG_PPPOE_NODE_TYPE,
412                                 PPPOE_NODE_TYPE_LEN) &&
413       p->name.full[PPPOE_NODE_TYPE_LEN] == ':') {
414     const struct linkinfo *nlink;
415     struct ngpppoe_init_data *data;
416     struct ngm_mkpeer mkp;
417     struct ngm_connect ngc;
418     const char *iface, *provider;
419     char etherid[12];
420     int providerlen;
421     char connectpath[sizeof dev->hook + 2];	/* .:<hook> */
422 
423     p->fd--;				/* We own the device - change fd */
424 
425 #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
426     if (modfind("netgraph") == -1 && ID0kldload("netgraph") == -1) {
427       log_Printf(LogWARN, "kldload: netgraph: %s\n", strerror(errno));
428       return NULL;
429     }
430 
431     if (modfind("ng_ether") == -1 && ID0kldload("ng_ether") == -1)
432       /*
433        * Don't treat this as an error as older kernels have this stuff
434        * built in as part of the netgraph node itself.
435        */
436       log_Printf(LogWARN, "kldload: ng_ether: %s\n", strerror(errno));
437 
438     if (modfind("ng_pppoe") == -1 && ID0kldload("ng_pppoe") == -1) {
439       log_Printf(LogWARN, "kldload: ng_pppoe: %s\n", strerror(errno));
440       return NULL;
441     }
442 
443     if (modfind("ng_socket") == -1 && ID0kldload("ng_socket") == -1) {
444       log_Printf(LogWARN, "kldload: ng_socket: %s\n", strerror(errno));
445       return NULL;
446     }
447 #endif
448 
449     if ((dev = malloc(sizeof *dev)) == NULL)
450       return NULL;
451 
452     iface = p->name.full + PPPOE_NODE_TYPE_LEN + 1;
453 
454     provider = strchr(iface, ':');
455     if (provider) {
456       ifacelen = provider - iface;
457       provider++;
458       providerlen = strlen(provider);
459     } else {
460       ifacelen = strlen(iface);
461       provider = "";
462       providerlen = 0;
463     }
464 
465     /*
466      * We're going to do this (where tunN is our tunnel device):
467      *
468      * .---------.
469      * |  ether  |
470      * | <iface> |                         dev->cs
471      * `---------'                           |
472      *  (orphan)                     p->fd   |
473      *     |                           |     |
474      *     |                           |     |
475      * (ethernet)                      |     |
476      * .---------.                  .-----------.
477      * |  pppoe  |                  |  socket   |
478      * | <iface> |(tunN)<---->(tunN)| <unnamed> |
479      * `---------                   `-----------'
480      *   (tunX)
481      *     ^
482      *     |
483      *     `--->(tunX)
484      */
485 
486     /* Create a socket node */
487     if (ID0NgMkSockNode(NULL, &dev->cs, &p->fd) == -1) {
488       log_Printf(LogWARN, "Cannot create netgraph socket node: %s\n",
489                  strerror(errno));
490       free(dev);
491       return NULL;
492     }
493 
494     /*
495      * Ask for a list of hooks attached to the "ether" node.  This node should
496      * magically exist as a way of hooking stuff onto an ethernet device
497      */
498     path = (char *)alloca(ifacelen + 2);
499     sprintf(path, "%.*s:", ifacelen, iface);
500     if (NgSendMsg(dev->cs, path, NGM_GENERIC_COOKIE, NGM_LISTHOOKS,
501                   NULL, 0) < 0) {
502       log_Printf(LogWARN, "%s Cannot send a netgraph message: %s\n",
503                  path, strerror(errno));
504       return ether_Abandon(dev, p);
505     }
506 
507     /* Get our list back */
508     resp = (struct ng_mesg *)rbuf;
509     if (NgRecvMsg(dev->cs, resp, sizeof rbuf, NULL) < 0) {
510       log_Printf(LogWARN, "Cannot get netgraph response: %s\n",
511                  strerror(errno));
512       return ether_Abandon(dev, p);
513     }
514 
515     hlist = (const struct hooklist *)resp->data;
516     ninfo = &hlist->nodeinfo;
517 
518     /* Make sure we've got the right type of node */
519     if (strncmp(ninfo->type, NG_ETHER_NODE_TYPE,
520                 sizeof NG_ETHER_NODE_TYPE - 1)) {
521       log_Printf(LogWARN, "%s Unexpected node type ``%s'' (wanted ``"
522                  NG_ETHER_NODE_TYPE "'')\n", path, ninfo->type);
523       return ether_Abandon(dev, p);
524     }
525 
526     log_Printf(LogDEBUG, "List of netgraph node ``%s'' (id %x) hooks:\n",
527                path, ninfo->id);
528 
529     /* look for a hook already attached.  */
530     for (f = 0; f < ninfo->hooks; f++) {
531       nlink = &hlist->link[f];
532 
533       log_Printf(LogDEBUG, "  Found %s -> %s\n", nlink->ourhook,
534                  nlink->peerhook);
535 
536       if (!strcmp(nlink->ourhook, NG_ETHER_HOOK_ORPHAN) ||
537           !strcmp(nlink->ourhook, NG_ETHER_HOOK_DIVERT)) {
538         /*
539          * Something is using the data coming out of this ``ether'' node.
540          * If it's a PPPoE node, we use that node, otherwise we complain that
541          * someone else is using the node.
542          */
543         if (!strcmp(nlink->nodeinfo.type, NG_PPPOE_NODE_TYPE))
544           /* Use this PPPoE node ! */
545           snprintf(ngc.path, sizeof ngc.path, "[%x]:", nlink->nodeinfo.id);
546         else {
547           log_Printf(LogWARN, "%s Node type ``%s'' is currently active\n",
548                      path, nlink->nodeinfo.type);
549           return ether_Abandon(dev, p);
550         }
551         break;
552       }
553     }
554 
555     if (f == ninfo->hooks) {
556       /*
557        * Create a new ``PPPoE'' node connected to the ``ether'' node using
558        * the magic ``orphan'' and ``ethernet'' hooks
559        */
560       snprintf(mkp.type, sizeof mkp.type, "%s", NG_PPPOE_NODE_TYPE);
561       snprintf(mkp.ourhook, sizeof mkp.ourhook, "%s", NG_ETHER_HOOK_ORPHAN);
562       snprintf(mkp.peerhook, sizeof mkp.peerhook, "%s", NG_PPPOE_HOOK_ETHERNET);
563       snprintf(etherid, sizeof etherid, "[%x]:", ninfo->id);
564 
565       log_Printf(LogDEBUG, "Creating PPPoE netgraph node %s%s -> %s\n",
566                  etherid, mkp.ourhook, mkp.peerhook);
567 
568       if (NgSendMsg(dev->cs, etherid, NGM_GENERIC_COOKIE,
569                     NGM_MKPEER, &mkp, sizeof mkp) < 0) {
570         log_Printf(LogWARN, "%s Cannot create PPPoE netgraph node: %s\n",
571                    etherid, strerror(errno));
572         return ether_Abandon(dev, p);
573       }
574 
575       snprintf(ngc.path, sizeof ngc.path, "%s%s", path, NG_ETHER_HOOK_ORPHAN);
576     }
577 
578     snprintf(dev->hook, sizeof dev->hook, "%s%d",
579              TUN_NAME, p->dl->bundle->unit);
580 
581     /*
582      * Connect the PPPoE node to our socket node.
583      * ngc.path has already been set up
584      */
585     snprintf(ngc.ourhook, sizeof ngc.ourhook, "%s", dev->hook);
586     memcpy(ngc.peerhook, ngc.ourhook, sizeof ngc.peerhook);
587 
588     log_Printf(LogDEBUG, "Connecting netgraph socket .:%s -> %s:%s\n",
589                ngc.ourhook, ngc.path, ngc.peerhook);
590     if (NgSendMsg(dev->cs, ".:", NGM_GENERIC_COOKIE,
591                   NGM_CONNECT, &ngc, sizeof ngc) < 0) {
592       log_Printf(LogWARN, "Cannot connect PPPoE and socket netgraph "
593                  "nodes: %s\n", strerror(errno));
594       return ether_Abandon(dev, p);
595     }
596 
597     /* And finally, request a connection to the given provider */
598 
599     data = (struct ngpppoe_init_data *)alloca(sizeof *data + providerlen);
600     snprintf(data->hook, sizeof data->hook, "%s", dev->hook);
601     memcpy(data->data, provider, providerlen);
602     data->data_len = providerlen;
603 
604     snprintf(connectpath, sizeof connectpath, ".:%s", dev->hook);
605     log_Printf(LogDEBUG, "Sending PPPOE_CONNECT to %s\n", connectpath);
606     if (NgSendMsg(dev->cs, connectpath, NGM_PPPOE_COOKIE,
607                   NGM_PPPOE_CONNECT, data, sizeof *data + providerlen) == -1) {
608       log_Printf(LogWARN, "``%s'': Cannot start netgraph node: %s\n",
609                  connectpath, strerror(errno));
610       return ether_Abandon(dev, p);
611     }
612 
613     /* Hook things up so that we monitor dev->cs */
614     p->desc.UpdateSet = ether_UpdateSet;
615     p->desc.IsSet = ether_IsSet;
616     p->desc.Read = ether_DescriptorRead;
617 
618     memcpy(&dev->dev, &baseetherdevice, sizeof dev->dev);
619     switch (p->cfg.cd.necessity) {
620       case CD_VARIABLE:
621         dev->dev.cd.delay = p->cfg.cd.delay;
622         break;
623       case CD_REQUIRED:
624         dev->dev.cd = p->cfg.cd;
625         break;
626       case CD_NOTREQUIRED:
627         log_Printf(LogWARN, "%s: Carrier must be set, using ``set cd %d!''\n",
628                    p->link.name, dev->dev.cd.delay);
629       case CD_DEFAULT:
630         break;
631     }
632 
633     dev->timeout = dev->dev.cd.delay;
634     dev->connected = CARRIER_PENDING;
635 
636   } else {
637     /* See if we're a netgraph socket */
638     struct stat st;
639 
640     if (fstat(p->fd, &st) != -1 && (st.st_mode & S_IFSOCK)) {
641       struct sockaddr_storage ssock;
642       struct sockaddr *sock = (struct sockaddr *)&ssock;
643       int sz;
644 
645       sz = sizeof ssock;
646       if (getsockname(p->fd, sock, &sz) == -1) {
647         log_Printf(LogPHASE, "%s: Link is a closed socket !\n", p->link.name);
648         close(p->fd);
649         p->fd = -1;
650         return NULL;
651       }
652 
653       if (sock->sa_family == AF_NETGRAPH) {
654         /*
655          * It's a netgraph node... We can't determine hook names etc, so we
656          * stay pretty impartial....
657          */
658         log_Printf(LogPHASE, "%s: Link is a netgraph node\n", p->link.name);
659 
660         if ((dev = malloc(sizeof *dev)) == NULL) {
661           log_Printf(LogWARN, "%s: Cannot allocate an ether device: %s\n",
662                      p->link.name, strerror(errno));
663           return NULL;
664         }
665 
666         memcpy(&dev->dev, &baseetherdevice, sizeof dev->dev);
667         dev->cs = -1;
668         dev->timeout = 0;
669         dev->connected = CARRIER_OK;
670         *dev->hook = '\0';
671       }
672     }
673   }
674 
675   if (dev) {
676     physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNCNOACF);
677 
678     /* Moan about (and fix) invalid LCP configurations */
679     if (p->link.lcp.cfg.mru > 1492) {
680       log_Printf(LogWARN, "%s: Reducing MRU to 1492\n", p->link.name);
681       p->link.lcp.cfg.mru = 1492;
682     }
683     if (p->dl->bundle->cfg.mtu > 1492) {
684       log_Printf(LogWARN, "%s: Reducing MTU to 1492\n", p->link.name);
685       p->dl->bundle->cfg.mtu = 1492;
686     }
687 
688     if (path != NULL) {
689       /* Mark the interface as UP if it's not already */
690 
691       path[ifacelen] = '\0';		/* Remove the trailing ':' */
692       if (!iface_SetFlags(path, IFF_UP))
693         log_Printf(LogWARN, "%s: Failed to set the IFF_UP flag on %s\n",
694                    p->link.name, path);
695     }
696 
697     return &dev->dev;
698   }
699 
700   return NULL;
701 }
702