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