1 /*- 2 * Copyright (c) 2000 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 <netinet/in_systm.h> 38 #include <netinet/ip.h> 39 #include <netgraph/ng_ether.h> 40 #include <netgraph/ng_message.h> 41 #include <netgraph/ng_socket.h> 42 43 #include <errno.h> 44 #include <stdio.h> 45 #include <stdlib.h> 46 #include <string.h> 47 #include <sysexits.h> 48 #include <sys/fcntl.h> 49 #include <sys/uio.h> 50 #include <termios.h> 51 #include <sys/time.h> 52 #include <unistd.h> 53 54 #include "layer.h" 55 #include "defs.h" 56 #include "mbuf.h" 57 #include "log.h" 58 #include "timer.h" 59 #include "lqr.h" 60 #include "hdlc.h" 61 #include "throughput.h" 62 #include "fsm.h" 63 #include "lcp.h" 64 #include "ccp.h" 65 #include "link.h" 66 #include "async.h" 67 #include "descriptor.h" 68 #include "physical.h" 69 #include "main.h" 70 #include "mp.h" 71 #include "chat.h" 72 #include "auth.h" 73 #include "chap.h" 74 #include "cbcp.h" 75 #include "datalink.h" 76 #include "slcompress.h" 77 #include "iplist.h" 78 #include "ncpaddr.h" 79 #include "ipcp.h" 80 #include "ipv6cp.h" 81 #include "ncp.h" 82 #include "filter.h" 83 #ifndef NORADIUS 84 #include "radius.h" 85 #endif 86 #include "bundle.h" 87 #include "id.h" 88 #include "netgraph.h" 89 90 91 struct ngdevice { 92 struct device dev; /* What struct physical knows about */ 93 int cs; /* Control socket */ 94 char hook[NG_HOOKSIZ]; /* Our socket node hook */ 95 }; 96 97 #define device2ng(d) ((d)->type == NG_DEVICE ? (struct ngdevice *)d : NULL) 98 #define NG_MSGBUFSZ 4096 99 #define NETGRAPH_PREFIX "netgraph:" 100 101 int 102 ng_DeviceSize(void) 103 { 104 return sizeof(struct ngdevice); 105 } 106 107 static int 108 ng_MessageOut(struct ngdevice *dev, struct physical *p, const char *data) 109 { 110 char path[NG_PATHSIZ]; 111 int len, pos, dpos; 112 char *fmt; 113 114 /* 115 * We expect a node path, one or more spaces, a command, one or more 116 * spaces and an ascii netgraph structure. 117 */ 118 data += strspn(data, " \t"); 119 len = strcspn(data, " \t"); 120 if (len >= sizeof path) { 121 log_Printf(LogWARN, "%s: %.*s: Node path too long\n", 122 dev->dev.name, len, data); 123 return 0; 124 } 125 memcpy(path, data, len); 126 path[len] = '\0'; 127 data += len; 128 129 data += strspn(data, " \t"); 130 len = strcspn(data, " \t"); 131 for (pos = len; pos >= 0; pos--) 132 if (data[pos] == '%') 133 len++; 134 if ((fmt = alloca(len + 4)) == NULL) { 135 log_Printf(LogWARN, "%s: alloca(%d) failure... %s\n", 136 dev->dev.name, len + 4, strerror(errno)); 137 return 0; 138 } 139 140 /* 141 * This is probably a waste of time, but we really don't want to end 142 * up stuffing unexpected % escapes into the kernel.... 143 */ 144 for (pos = dpos = 0; pos < len;) { 145 if (data[dpos] == '%') 146 fmt[pos++] = '%'; 147 fmt[pos++] = data[dpos++]; 148 } 149 strcpy(fmt + pos, " %s"); 150 data += dpos; 151 152 data += strspn(data, " \t"); 153 if (NgSendAsciiMsg(dev->cs, path, fmt, data) < 0) { 154 log_Printf(LogDEBUG, "%s: NgSendAsciiMsg (to %s): \"%s\", \"%s\": %s\n", 155 dev->dev.name, path, fmt, data, strerror(errno)); 156 return 0; 157 } 158 159 return 1; 160 } 161 162 /* 163 * Get a netgraph message 164 */ 165 static ssize_t 166 ng_MessageIn(struct physical *p, char *buf, size_t sz) 167 { 168 char msgbuf[sizeof(struct ng_mesg) * 2 + NG_MSGBUFSZ]; 169 struct ngdevice *dev = device2ng(p->handler); 170 struct ng_mesg *rep = (struct ng_mesg *)msgbuf; 171 char path[NG_PATHSIZ]; 172 int len; 173 174 #ifdef BROKEN_SELECT 175 struct timeval t; 176 fd_set *r; 177 int ret; 178 179 if (dev->cs < 0) 180 return 0; 181 182 if ((r = mkfdset()) == NULL) { 183 log_Printf(LogERROR, "DoLoop: Cannot create fd_set\n"); 184 return -1; 185 } 186 zerofdset(r); 187 FD_SET(dev->cs, r); 188 t.tv_sec = t.tv_usec = 0; 189 ret = select(dev->cs + 1, r, NULL, NULL, &t); 190 free(r); 191 192 if (ret <= 0) 193 return 0; 194 #endif 195 196 if (NgRecvAsciiMsg(dev->cs, rep, sizeof msgbuf, path)) { 197 log_Printf(LogWARN, "%s: NgRecvAsciiMsg: %s\n", 198 dev->dev.name, strerror(errno)); 199 return -1; 200 } 201 202 /* XXX: Should we check rep->header.version ? */ 203 204 if (sz == 0) 205 log_Printf(LogWARN, "%s: Unexpected message: %s\n", dev->dev.name, 206 rep->header.cmdstr); 207 else { 208 log_Printf(LogDEBUG, "%s: Received message: %s\n", dev->dev.name, 209 rep->header.cmdstr); 210 len = strlen(rep->header.cmdstr); 211 if (sz > len) 212 sz = len; 213 memcpy(buf, rep->header.cmdstr, sz); 214 } 215 216 return sz; 217 } 218 219 static ssize_t 220 ng_Write(struct physical *p, const void *v, size_t n) 221 { 222 struct ngdevice *dev = device2ng(p->handler); 223 224 switch (p->dl->state) { 225 case DATALINK_DIAL: 226 case DATALINK_LOGIN: 227 return ng_MessageOut(dev, p, v) ? n : -1; 228 } 229 return NgSendData(p->fd, dev->hook, v, n) == -1 ? -1 : n; 230 } 231 232 static ssize_t 233 ng_Read(struct physical *p, void *v, size_t n) 234 { 235 char hook[NG_HOOKSIZ]; 236 237 log_Printf(LogDEBUG, "ng_Read\n"); 238 switch (p->dl->state) { 239 case DATALINK_DIAL: 240 case DATALINK_LOGIN: 241 return ng_MessageIn(p, v, n); 242 } 243 244 return NgRecvData(p->fd, v, n, hook); 245 } 246 247 static int 248 ng_RemoveFromSet(struct physical *p, fd_set *r, fd_set *w, fd_set *e) 249 { 250 struct ngdevice *dev = device2ng(p->handler); 251 int result; 252 253 if (r && dev->cs >= 0 && FD_ISSET(dev->cs, r)) { 254 FD_CLR(dev->cs, r); 255 log_Printf(LogTIMER, "%s: fdunset(ctrl) %d\n", p->link.name, dev->cs); 256 result = 1; 257 } else 258 result = 0; 259 260 /* Careful... physical_RemoveFromSet() called us ! */ 261 262 p->handler->removefromset = NULL; 263 result += physical_RemoveFromSet(p, r, w, e); 264 p->handler->removefromset = ng_RemoveFromSet; 265 266 return result; 267 } 268 269 static void 270 ng_Free(struct physical *p) 271 { 272 struct ngdevice *dev = device2ng(p->handler); 273 274 physical_SetDescriptor(p); 275 if (dev->cs != -1) 276 close(dev->cs); 277 free(dev); 278 } 279 280 static void 281 ng_device2iov(struct device *d, struct iovec *iov, int *niov, 282 int maxiov, int *auxfd, int *nauxfd) 283 { 284 struct ngdevice *dev = device2ng(d); 285 int sz = physical_MaxDeviceSize(); 286 287 iov[*niov].iov_base = realloc(d, sz); 288 if (iov[*niov].iov_base == NULL) { 289 log_Printf(LogALERT, "Failed to allocate memory: %d\n", sz); 290 AbortProgram(EX_OSERR); 291 } 292 iov[*niov].iov_len = sz; 293 (*niov)++; 294 295 *auxfd = dev->cs; 296 (*nauxfd)++; 297 } 298 299 static const struct device basengdevice = { 300 NG_DEVICE, 301 "netgraph", 302 0, 303 { CD_REQUIRED, DEF_NGCDDELAY }, 304 NULL, 305 ng_RemoveFromSet, 306 NULL, 307 NULL, 308 NULL, 309 NULL, 310 NULL, 311 ng_Free, 312 ng_Read, 313 ng_Write, 314 ng_device2iov, 315 NULL, 316 NULL, 317 NULL 318 }; 319 320 struct device * 321 ng_iov2device(int type, struct physical *p, struct iovec *iov, int *niov, 322 int maxiov, int *auxfd, int *nauxfd) 323 { 324 if (type == NG_DEVICE) { 325 struct ngdevice *dev = (struct ngdevice *)iov[(*niov)++].iov_base; 326 327 dev = realloc(dev, sizeof *dev); /* Reduce to the correct size */ 328 if (dev == NULL) { 329 log_Printf(LogALERT, "Failed to allocate memory: %d\n", 330 (int)(sizeof *dev)); 331 AbortProgram(EX_OSERR); 332 } 333 334 if (*nauxfd) { 335 dev->cs = *auxfd; 336 (*nauxfd)--; 337 } else 338 dev->cs = -1; 339 340 /* Refresh function pointers etc */ 341 memcpy(&dev->dev, &basengdevice, sizeof dev->dev); 342 343 /* XXX: Are netgraph always synchronous ? */ 344 physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNCNOACF); 345 return &dev->dev; 346 } 347 348 return NULL; 349 } 350 351 static int 352 ng_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n) 353 { 354 struct physical *p = descriptor2physical(d); 355 struct ngdevice *dev = device2ng(p->handler); 356 int result; 357 358 switch (p->dl->state) { 359 case DATALINK_DIAL: 360 case DATALINK_LOGIN: 361 if (r) { 362 FD_SET(dev->cs, r); 363 log_Printf(LogTIMER, "%s(ctrl): fdset(r) %d\n", p->link.name, dev->cs); 364 result = 1; 365 } else 366 result = 0; 367 break; 368 369 default: 370 result = physical_doUpdateSet(d, r, w, e, n, 0); 371 break; 372 } 373 374 return result; 375 } 376 377 static int 378 ng_IsSet(struct fdescriptor *d, const fd_set *fdset) 379 { 380 struct physical *p = descriptor2physical(d); 381 struct ngdevice *dev = device2ng(p->handler); 382 int result; 383 384 result = dev->cs >= 0 && FD_ISSET(dev->cs, fdset); 385 result += physical_IsSet(d, fdset); 386 387 return result; 388 } 389 390 static void 391 ng_DescriptorRead(struct fdescriptor *d, struct bundle *bundle, 392 const fd_set *fdset) 393 { 394 struct physical *p = descriptor2physical(d); 395 struct ngdevice *dev = device2ng(p->handler); 396 397 if (dev->cs >= 0 && FD_ISSET(dev->cs, fdset)) 398 ng_MessageIn(p, NULL, 0); 399 400 if (physical_IsSet(d, fdset)) 401 physical_DescriptorRead(d, bundle, fdset); 402 } 403 404 static struct device * 405 ng_Abandon(struct ngdevice *dev, struct physical *p) 406 { 407 /* Abandon our node construction */ 408 close(dev->cs); 409 close(p->fd); 410 p->fd = -2; /* Nobody else need try.. */ 411 free(dev); 412 413 return NULL; 414 } 415 416 417 /* 418 * Populate the ``word'' (of size ``sz'') named ``what'' from ``from'' 419 * ending with any character from ``sep''. Point ``endp'' at the next 420 * word. 421 */ 422 423 #define GETSEGMENT(what, from, sep, endp) \ 424 getsegment(#what, (what), sizeof(what), from, sep, endp) 425 426 static int 427 getsegment(const char *what, char *word, size_t sz, const char *from, 428 const char *sep, const char **endp) 429 { 430 int len; 431 432 if ((len = strcspn(from, sep)) == 0) { 433 log_Printf(LogWARN, "%s name should not be empty !\n", what); 434 return 0; 435 } 436 437 if (len >= sz) { 438 log_Printf(LogWARN, "%s name too long, max %d !\n", what, sz - 1); 439 return 0; 440 } 441 442 strncpy(word, from, len); 443 word[len] = '\0'; 444 445 *endp = from + len; 446 *endp += strspn(*endp, sep); 447 448 return 1; 449 } 450 451 struct device * 452 ng_Create(struct physical *p) 453 { 454 struct sockaddr_ng ngsock; 455 u_char rbuf[2048]; 456 struct sockaddr *sock = (struct sockaddr *)&ngsock; 457 const struct hooklist *hlist; 458 const struct nodeinfo *ninfo; 459 const struct linkinfo *nlink; 460 struct ngdevice *dev; 461 struct ng_mesg *resp; 462 struct ngm_mkpeer mkp; 463 struct ngm_connect ngc; 464 const char *devp, *endp; 465 char lasthook[NG_HOOKSIZ]; 466 char hook[NG_HOOKSIZ]; 467 char nodetype[NG_TYPESIZ + NG_NODESIZ]; 468 char modname[NG_TYPESIZ + 3]; 469 char path[NG_PATHSIZ]; 470 char *nodename; 471 int len, sz, done, f; 472 473 dev = NULL; 474 if (p->fd < 0 && !strncasecmp(p->name.full, NETGRAPH_PREFIX, 475 sizeof NETGRAPH_PREFIX - 1)) { 476 p->fd--; /* We own the device - change fd */ 477 478 if ((dev = malloc(sizeof *dev)) == NULL) 479 return NULL; 480 481 loadmodules(LOAD_VERBOSLY, "netgraph", "ng_socket", NULL); 482 483 /* Create a socket node */ 484 if (ID0NgMkSockNode(NULL, &dev->cs, &p->fd) == -1) { 485 log_Printf(LogWARN, "Cannot create netgraph socket node: %s\n", 486 strerror(errno)); 487 free(dev); 488 p->fd = -2; 489 return NULL; 490 } 491 492 devp = p->name.full + sizeof NETGRAPH_PREFIX - 1; 493 *lasthook = *path = '\0'; 494 log_Printf(LogDEBUG, "%s: Opening netgraph device \"%s\"\n", 495 p->link.name, devp); 496 done = 0; 497 498 while (*devp != '\0' && !done) { 499 if (*devp != '[') { 500 if (*lasthook == '\0') { 501 log_Printf(LogWARN, "%s: Netgraph devices must start with" 502 " [nodetype:nodename]\n", p->link.name); 503 return ng_Abandon(dev, p); 504 } 505 506 /* Get the hook name of the new node */ 507 if (!GETSEGMENT(hook, devp, ".[", &endp)) 508 return ng_Abandon(dev, p); 509 log_Printf(LogDEBUG, "%s: Got hook \"%s\"\n", p->link.name, hook); 510 devp = endp; 511 if (*devp == '\0') { 512 log_Printf(LogWARN, "%s: Netgraph device must not end with a second" 513 " hook\n", p->link.name); 514 return ng_Abandon(dev, p); 515 } 516 if (devp[-1] != '[') { 517 log_Printf(LogWARN, "%s: Expected a [nodetype:nodename] at device" 518 " pos %d\n", p->link.name, devp - p->link.name - 1); 519 return ng_Abandon(dev, p); 520 } 521 } else { 522 /* Use lasthook as the hook name */ 523 strcpy(hook, lasthook); 524 devp++; 525 } 526 527 /* We've got ``lasthook'' and ``hook'', get the node type */ 528 if (!GETSEGMENT(nodetype, devp, "]", &endp)) 529 return ng_Abandon(dev, p); 530 log_Printf(LogDEBUG, "%s: Got node \"%s\"\n", p->link.name, nodetype); 531 532 if ((nodename = strchr(nodetype, ':')) != NULL) { 533 *nodename++ = '\0'; 534 if (*nodename == '\0' && *nodetype == '\0') { 535 log_Printf(LogWARN, "%s: Empty [nodetype:nodename] at device" 536 " pos %d\n", p->link.name, devp - p->link.name - 1); 537 return ng_Abandon(dev, p); 538 } 539 } 540 541 /* Ignore optional colons after nodes */ 542 devp = *endp == ':' ? endp + 1 : endp; 543 if (*devp == '.') 544 devp++; 545 546 if (*lasthook == '\0') { 547 /* This is the first node in the chain */ 548 if (nodename == NULL || *nodename == '\0') { 549 log_Printf(LogWARN, "%s: %s: No initial device nodename\n", 550 p->link.name, devp); 551 return ng_Abandon(dev, p); 552 } 553 554 if (*nodetype != '\0') { 555 /* Attempt to load the module */ 556 snprintf(modname, sizeof modname, "ng_%s", nodetype); 557 log_Printf(LogDEBUG, "%s: Attempting to load %s.ko\n", 558 p->link.name, modname); 559 loadmodules(LOAD_QUIETLY, modname, NULL); 560 } 561 562 snprintf(path, sizeof path, "%s:", nodename); 563 /* XXX: If we have a node type, ensure it's correct */ 564 } else { 565 /* 566 * Ask for a list of hooks attached to the previous node. If we 567 * find the one we're interested in, and if it's connected to a 568 * node of the right type using the correct hook, use that. 569 * If we find the hook connected to something else, fail. 570 * If we find no match, mkpeer the new node. 571 */ 572 if (*nodetype == '\0') { 573 log_Printf(LogWARN, "%s: Nodetype missing at device offset %d\n", 574 p->link.name, 575 devp - p->name.full + sizeof NETGRAPH_PREFIX - 1); 576 return ng_Abandon(dev, p); 577 } 578 579 /* Get a list of node hooks */ 580 if (NgSendMsg(dev->cs, path, NGM_GENERIC_COOKIE, NGM_LISTHOOKS, 581 NULL, 0) < 0) { 582 log_Printf(LogWARN, "%s: %s Cannot send a LISTHOOOKS message: %s\n", 583 p->link.name, path, strerror(errno)); 584 return ng_Abandon(dev, p); 585 } 586 587 /* Get our list back */ 588 resp = (struct ng_mesg *)rbuf; 589 if (NgRecvMsg(dev->cs, resp, sizeof rbuf, NULL) <= 0) { 590 log_Printf(LogWARN, "%s: Cannot get netgraph response: %s\n", 591 p->link.name, strerror(errno)); 592 return ng_Abandon(dev, p); 593 } 594 595 hlist = (const struct hooklist *)resp->data; 596 ninfo = &hlist->nodeinfo; 597 598 log_Printf(LogDEBUG, "List of netgraph node ``%s'' (id %x) hooks:\n", 599 path, ninfo->id); 600 601 /* look for a hook already attached. */ 602 for (f = 0; f < ninfo->hooks; f++) { 603 nlink = &hlist->link[f]; 604 605 log_Printf(LogDEBUG, " Found %s -> %s (type %s)\n", nlink->ourhook, 606 nlink->peerhook, nlink->nodeinfo.type); 607 608 if (!strcmp(nlink->ourhook, lasthook)) { 609 if (strcmp(nlink->peerhook, hook) || 610 strcmp(nlink->nodeinfo.type, nodetype)) { 611 log_Printf(LogWARN, "%s: hook %s:%s is already in use\n", 612 p->link.name, nlink->ourhook, path); 613 return ng_Abandon(dev, p); 614 } 615 /* The node is already hooked up nicely.... reuse it */ 616 break; 617 } 618 } 619 620 if (f == ninfo->hooks) { 621 /* Attempt to load the module */ 622 snprintf(modname, sizeof modname, "ng_%s", nodetype); 623 log_Printf(LogDEBUG, "%s: Attempting to load %s.ko\n", 624 p->link.name, modname); 625 loadmodules(LOAD_QUIETLY, modname, NULL); 626 627 /* Create (mkpeer) the new node */ 628 629 snprintf(mkp.type, sizeof mkp.type, "%s", nodetype); 630 snprintf(mkp.ourhook, sizeof mkp.ourhook, "%s", lasthook); 631 snprintf(mkp.peerhook, sizeof mkp.peerhook, "%s", hook); 632 633 log_Printf(LogDEBUG, "%s: Doing MKPEER %s%s -> %s (type %s)\n", 634 p->link.name, path, mkp.ourhook, mkp.peerhook, nodetype); 635 636 if (NgSendMsg(dev->cs, path, NGM_GENERIC_COOKIE, 637 NGM_MKPEER, &mkp, sizeof mkp) < 0) { 638 log_Printf(LogWARN, "%s Cannot create %s netgraph node: %s\n", 639 path, nodetype, strerror(errno)); 640 return ng_Abandon(dev, p); 641 } 642 } 643 len = strlen(path); 644 snprintf(path + len, sizeof path - len, "%s%s", 645 path[len - 1] == ':' ? "" : ".", lasthook); 646 } 647 648 /* Get a list of node hooks */ 649 if (NgSendMsg(dev->cs, path, NGM_GENERIC_COOKIE, NGM_LISTHOOKS, 650 NULL, 0) < 0) { 651 log_Printf(LogWARN, "%s: %s Cannot send a LISTHOOOKS message: %s\n", 652 p->link.name, path, strerror(errno)); 653 return ng_Abandon(dev, p); 654 } 655 656 /* Get our list back */ 657 resp = (struct ng_mesg *)rbuf; 658 if (NgRecvMsg(dev->cs, resp, sizeof rbuf, NULL) <= 0) { 659 log_Printf(LogWARN, "%s: Cannot get netgraph response: %s\n", 660 p->link.name, strerror(errno)); 661 return ng_Abandon(dev, p); 662 } 663 664 hlist = (const struct hooklist *)resp->data; 665 ninfo = &hlist->nodeinfo; 666 667 if (*lasthook != '\0' && nodename != NULL && *nodename != '\0' && 668 strcmp(ninfo->name, nodename) && 669 NgNameNode(dev->cs, path, "%s", nodename) < 0) { 670 log_Printf(LogWARN, "%s: %s: Cannot name netgraph node: %s\n", 671 p->link.name, path, strerror(errno)); 672 return ng_Abandon(dev, p); 673 } 674 675 if (!GETSEGMENT(lasthook, devp, " \t.[", &endp)) 676 return ng_Abandon(dev, p); 677 log_Printf(LogDEBUG, "%s: Got hook \"%s\"\n", p->link.name, lasthook); 678 679 len = strlen(lasthook); 680 done = strchr(" \t", devp[len]) ? 1 : 0; 681 devp = endp; 682 683 if (*devp != '\0') { 684 if (devp[-1] == '[') 685 devp--; 686 } /* else should moan about devp[-1] being '[' ? */ 687 } 688 689 snprintf(dev->hook, sizeof dev->hook, "%s", lasthook); 690 691 /* Connect the node to our socket node */ 692 snprintf(ngc.path, sizeof ngc.path, "%s", path); 693 snprintf(ngc.ourhook, sizeof ngc.ourhook, "%s", dev->hook); 694 memcpy(ngc.peerhook, ngc.ourhook, sizeof ngc.peerhook); 695 696 log_Printf(LogDEBUG, "Connecting netgraph socket .:%s -> %s.%s\n", 697 ngc.ourhook, ngc.path, ngc.peerhook); 698 if (NgSendMsg(dev->cs, ".:", NGM_GENERIC_COOKIE, 699 NGM_CONNECT, &ngc, sizeof ngc) < 0) { 700 log_Printf(LogWARN, "Cannot connect %s and socket netgraph " 701 "nodes: %s\n", path, strerror(errno)); 702 return ng_Abandon(dev, p); 703 } 704 705 /* Hook things up so that we monitor dev->cs */ 706 p->desc.UpdateSet = ng_UpdateSet; 707 p->desc.IsSet = ng_IsSet; 708 p->desc.Read = ng_DescriptorRead; 709 710 memcpy(&dev->dev, &basengdevice, sizeof dev->dev); 711 712 } else { 713 /* See if we're a netgraph socket */ 714 715 sz = sizeof ngsock; 716 if (getsockname(p->fd, sock, &sz) != -1 && sock->sa_family == AF_NETGRAPH) { 717 /* 718 * It's a netgraph node... We can't determine hook names etc, so we 719 * stay pretty impartial.... 720 */ 721 log_Printf(LogPHASE, "%s: Link is a netgraph node\n", p->link.name); 722 723 if ((dev = malloc(sizeof *dev)) == NULL) { 724 log_Printf(LogWARN, "%s: Cannot allocate an ether device: %s\n", 725 p->link.name, strerror(errno)); 726 return NULL; 727 } 728 729 memcpy(&dev->dev, &basengdevice, sizeof dev->dev); 730 dev->cs = -1; 731 *dev->hook = '\0'; 732 } 733 } 734 735 if (dev) { 736 physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNCNOACF); 737 return &dev->dev; 738 } 739 740 return NULL; 741 } 742