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