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