1 /*- 2 * Copyright (c) 1999 Brian Somers <brian@Awfulhak.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 #include <sys/param.h> 30 #include <sys/socket.h> 31 #include <sys/un.h> 32 #include <netinet/in.h> 33 #include <arpa/inet.h> 34 #include <netdb.h> 35 #include <netgraph.h> 36 #include <net/ethernet.h> 37 #include <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_pppoe.h> 42 #include <netgraph/ng_socket.h> 43 44 #include <errno.h> 45 #include <paths.h> 46 #include <signal.h> 47 #include <stdio.h> 48 #include <stdarg.h> 49 #include <stdlib.h> 50 #include <string.h> 51 #include <sysexits.h> 52 #include <sys/fcntl.h> 53 #ifndef NOKLDLOAD 54 #include <sys/linker.h> 55 #include <sys/module.h> 56 #endif 57 #include <sys/uio.h> 58 #include <sys/wait.h> 59 #include <syslog.h> 60 #include <termios.h> 61 #include <unistd.h> 62 63 64 #define DEFAULT_EXEC_PREFIX "exec /usr/sbin/ppp -direct " 65 66 static int ReceivedSignal; 67 68 static int 69 usage(const char *prog) 70 { 71 fprintf(stderr, "Usage: %s [-Fd] [-P pidfile] [-a name] [-e exec]" 72 " [-p provider] interface\n", prog); 73 return EX_USAGE; 74 } 75 76 static void 77 Farewell(int sig) 78 { 79 ReceivedSignal = sig; 80 signal(sig, SIG_DFL); /* If something makes us block... */ 81 } 82 83 static int 84 ConfigureNode(const char *prog, const char *iface, const char *provider, 85 int cs, int ds, int debug, struct ngm_connect *ngc) 86 { 87 /* 88 * We're going to do this with the passed `ds' & `cs' descriptors: 89 * 90 * .---------. 91 * | ether | 92 * | <iface> | 93 * `---------' 94 * (orphan) ds cs 95 * | | | 96 * | | | 97 * (ethernet) | | 98 * .---------. .-----------. 99 * | pppoe | | socket | 100 * | <iface> |(pppoe-<pid>)<---->(pppoe-<pid>)| <unnamed> | 101 * `--------- `-----------' 102 * (exec-<pid>) 103 * ^ .-----------. .-------------. 104 * | | socket | | ppp -direct | 105 * `--->(exec-<pid>)| <unnamed> |--fd--| provider | 106 * `-----------' `-------------' 107 * 108 * where there are potentially many ppp processes running off of the 109 * same PPPoE node. 110 * The exec-<pid> hook isn't made 'till we Spawn(). 111 */ 112 113 char *epath, *spath; 114 struct ngpppoe_init_data *data; 115 const struct hooklist *hlist; 116 const struct nodeinfo *ninfo; 117 const struct linkinfo *nlink; 118 struct ngm_mkpeer mkp; 119 struct ng_mesg *resp; 120 u_char rbuf[2048]; 121 int f, plen; 122 123 /* 124 * Ask for a list of hooks attached to the "ether" node. This node should 125 * magically exist as a way of hooking stuff onto an ethernet device 126 */ 127 epath = (char *)alloca(strlen(iface) + 2); 128 sprintf(epath, "%s:", iface); 129 130 if (debug) 131 fprintf(stderr, "Sending NGM_LISTHOOKS to %s\n", epath); 132 133 if (NgSendMsg(cs, epath, NGM_GENERIC_COOKIE, NGM_LISTHOOKS, NULL, 0) < 0) { 134 if (errno == ENOENT) 135 fprintf(stderr, "%s Cannot send a netgraph message: Invalid interface\n", 136 epath); 137 else 138 fprintf(stderr, "%s Cannot send a netgraph message: %s\n", 139 epath, strerror(errno)); 140 return EX_UNAVAILABLE; 141 } 142 143 /* Get our list back */ 144 resp = (struct ng_mesg *)rbuf; 145 if (NgRecvMsg(cs, resp, sizeof rbuf, NULL) < 0) { 146 perror("Cannot get netgraph response"); 147 return EX_UNAVAILABLE; 148 } 149 150 hlist = (const struct hooklist *)resp->data; 151 ninfo = &hlist->nodeinfo; 152 153 if (debug) 154 fprintf(stderr, "Got reply from id [%x]: Type %s with %d hooks\n", 155 ninfo->id, ninfo->type, ninfo->hooks); 156 157 /* Make sure we've got the right type of node */ 158 if (strncmp(ninfo->type, NG_ETHER_NODE_TYPE, sizeof NG_ETHER_NODE_TYPE - 1)) { 159 fprintf(stderr, "%s Unexpected node type ``%s'' (wanted ``" 160 NG_ETHER_NODE_TYPE "'')\n", epath, ninfo->type); 161 return EX_DATAERR; 162 } 163 164 /* look for a hook already attached. */ 165 for (f = 0; f < ninfo->hooks; f++) { 166 nlink = &hlist->link[f]; 167 168 if (debug) 169 fprintf(stderr, " Got [%x]:%s -> [%x]:%s\n", ninfo->id, 170 nlink->ourhook, nlink->nodeinfo.id, nlink->peerhook); 171 172 if (!strcmp(nlink->ourhook, NG_ETHER_HOOK_ORPHAN) || 173 !strcmp(nlink->ourhook, NG_ETHER_HOOK_DIVERT)) { 174 /* 175 * Something is using the data coming out of this `ether' node. 176 * If it's a PPPoE node, we use that node, otherwise we complain that 177 * someone else is using the node. 178 */ 179 if (strcmp(nlink->nodeinfo.type, NG_PPPOE_NODE_TYPE)) { 180 fprintf(stderr, "%s Node type %s is currently active\n", 181 epath, nlink->nodeinfo.type); 182 return EX_UNAVAILABLE; 183 } 184 break; 185 } 186 } 187 188 if (f == ninfo->hooks) { 189 /* 190 * Create a new PPPoE node connected to the `ether' node using 191 * the magic `orphan' and `ethernet' hooks 192 */ 193 snprintf(mkp.type, sizeof mkp.type, "%s", NG_PPPOE_NODE_TYPE); 194 snprintf(mkp.ourhook, sizeof mkp.ourhook, "%s", NG_ETHER_HOOK_ORPHAN); 195 snprintf(mkp.peerhook, sizeof mkp.peerhook, "%s", NG_PPPOE_HOOK_ETHERNET); 196 197 if (debug) 198 fprintf(stderr, "Send MKPEER: %s%s -> [type %s]:%s\n", epath, 199 mkp.ourhook, mkp.type, mkp.peerhook); 200 201 if (NgSendMsg(cs, epath, NGM_GENERIC_COOKIE, 202 NGM_MKPEER, &mkp, sizeof mkp) < 0) { 203 fprintf(stderr, "%s Cannot create a peer PPPoE node: %s\n", 204 epath, strerror(errno)); 205 return EX_OSERR; 206 } 207 } 208 209 /* Connect the PPPoE node to our socket node. */ 210 snprintf(ngc->path, sizeof ngc->path, "%s%s", epath, NG_ETHER_HOOK_ORPHAN); 211 snprintf(ngc->ourhook, sizeof ngc->ourhook, "pppoe-%ld", (long)getpid()); 212 memcpy(ngc->peerhook, ngc->ourhook, sizeof ngc->peerhook); 213 214 if (NgSendMsg(cs, ".:", NGM_GENERIC_COOKIE, 215 NGM_CONNECT, ngc, sizeof *ngc) < 0) { 216 perror("Cannot CONNECT PPPoE and socket nodes"); 217 return EX_OSERR; 218 } 219 220 plen = strlen(provider); 221 222 data = (struct ngpppoe_init_data *)alloca(sizeof *data + plen); 223 snprintf(data->hook, sizeof data->hook, "%s", ngc->peerhook); 224 memcpy(data->data, provider, plen); 225 data->data_len = plen; 226 227 spath = (char *)alloca(strlen(ngc->peerhook) + 3); 228 strcpy(spath, ".:"); 229 strcpy(spath + 2, ngc->ourhook); 230 231 if (debug) { 232 if (provider) 233 fprintf(stderr, "Sending PPPOE_LISTEN to %s, provider %s\n", 234 spath, provider); 235 else 236 fprintf(stderr, "Sending PPPOE_LISTEN to %s\n", spath); 237 } 238 239 if (NgSendMsg(cs, spath, NGM_PPPOE_COOKIE, NGM_PPPOE_LISTEN, 240 data, sizeof *data + plen) == -1) { 241 fprintf(stderr, "%s: Cannot LISTEN on netgraph node: %s\n", 242 spath, strerror(errno)); 243 return EX_OSERR; 244 } 245 246 return 0; 247 } 248 249 static void 250 Spawn(const char *prog, const char *acname, const char *provider, 251 const char *exec, 252 struct ngm_connect ngc, int cs, int ds, void *request, int sz, 253 int debug) 254 { 255 char msgbuf[sizeof(struct ng_mesg) + sizeof(struct ngpppoe_sts)]; 256 struct ng_mesg *rep = (struct ng_mesg *)msgbuf; 257 struct ngpppoe_sts *sts = (struct ngpppoe_sts *)(msgbuf + sizeof *rep); 258 struct ngpppoe_init_data *data; 259 char unknown[14], *path; 260 const char *msg; 261 int ret, slen; 262 263 switch ((ret = fork())) { 264 case -1: 265 syslog(LOG_ERR, "fork: %m"); 266 break; 267 268 case 0: 269 switch (fork()) { 270 case 0: 271 break; 272 case -1: 273 _exit(errno); 274 default: 275 _exit(0); 276 } 277 close(cs); 278 close(ds); 279 280 /* Create a new socket node */ 281 if (debug) 282 syslog(LOG_INFO, "Creating a new socket node"); 283 284 if (NgMkSockNode(NULL, &cs, &ds) == -1) { 285 syslog(LOG_ERR, "Cannot create netgraph socket node: %m"); 286 _exit(EX_CANTCREAT); 287 } 288 289 /* Connect the PPPoE node to our new socket node. */ 290 snprintf(ngc.ourhook, sizeof ngc.ourhook, "exec-%ld", (long)getpid()); 291 memcpy(ngc.peerhook, ngc.ourhook, sizeof ngc.peerhook); 292 293 if (debug) 294 syslog(LOG_INFO, "Sending CONNECT from .:%s -> %s.%s", 295 ngc.ourhook, ngc.path, ngc.peerhook); 296 if (NgSendMsg(cs, ".:", NGM_GENERIC_COOKIE, 297 NGM_CONNECT, &ngc, sizeof ngc) < 0) { 298 syslog(LOG_ERR, "Cannot CONNECT PPPoE and socket nodes: %m"); 299 _exit(EX_OSERR); 300 } 301 302 /* 303 * If we tell the socket node not to LINGER, it will go away when 304 * the last hook is removed. 305 */ 306 if (debug) 307 syslog(LOG_INFO, "Sending NGM_SOCK_CMD_NOLINGER to socket"); 308 if (NgSendMsg(cs, ".:", NGM_SOCKET_COOKIE, 309 NGM_SOCK_CMD_NOLINGER, NULL, 0) < 0) { 310 syslog(LOG_ERR, "Cannot send NGM_SOCK_CMD_NOLINGER: %m"); 311 _exit(EX_OSERR); 312 } 313 314 /* Put the PPPoE node into OFFER mode */ 315 slen = strlen(acname); 316 data = (struct ngpppoe_init_data *)alloca(sizeof *data + slen); 317 snprintf(data->hook, sizeof data->hook, "%s", ngc.ourhook); 318 memcpy(data->data, acname, slen); 319 data->data_len = slen; 320 321 path = (char *)alloca(strlen(ngc.ourhook) + 3); 322 strcpy(path, ".:"); 323 strcpy(path + 2, ngc.ourhook); 324 325 syslog(LOG_INFO, "Offering to %s as access concentrator %s", 326 path, acname); 327 if (NgSendMsg(cs, path, NGM_PPPOE_COOKIE, NGM_PPPOE_OFFER, 328 data, sizeof *data + slen) == -1) { 329 syslog(LOG_INFO, "%s: Cannot OFFER on netgraph node: %m", path); 330 _exit(EX_OSERR); 331 } 332 /* If we have a provider code, set it */ 333 if (provider) { 334 slen = strlen(provider); 335 data = (struct ngpppoe_init_data *)alloca(sizeof *data + slen); 336 snprintf(data->hook, sizeof data->hook, "%s", ngc.ourhook); 337 memcpy(data->data, provider, slen); 338 data->data_len = slen; 339 340 syslog(LOG_INFO, "adding to %s as offered service %s", 341 path, acname); 342 if (NgSendMsg(cs, path, NGM_PPPOE_COOKIE, NGM_PPPOE_SERVICE, 343 data, sizeof *data + slen) == -1) { 344 syslog(LOG_INFO, "%s: Cannot add service on netgraph node: %m", path); 345 _exit(EX_OSERR); 346 } 347 } 348 349 /* And send our request data to the waiting node */ 350 if (debug) 351 syslog(LOG_INFO, "Sending original request to %s (%d bytes)", path, sz); 352 if (NgSendData(ds, ngc.ourhook, request, sz) == -1) { 353 syslog(LOG_ERR, "Cannot send original request to %s: %m", path); 354 _exit(EX_OSERR); 355 } 356 357 /* Then wait for a success indication */ 358 359 if (debug) 360 syslog(LOG_INFO, "Waiting for a SUCCESS reply %s", path); 361 362 do { 363 if (NgRecvMsg(cs, rep, sizeof msgbuf, NULL) < 0) { 364 syslog(LOG_ERR, "%s: Cannot receive a message: %m", path); 365 _exit(EX_OSERR); 366 } 367 368 if (rep->header.version != NG_VERSION) { 369 syslog(LOG_ERR, "%ld: Unexpected netgraph version, expected %ld", 370 (long)rep->header.version, (long)NG_VERSION); 371 _exit(EX_PROTOCOL); 372 } 373 374 if (rep->header.typecookie != NGM_PPPOE_COOKIE) { 375 syslog(LOG_INFO, "%ld: Unexpected netgraph cookie, expected %ld", 376 (long)rep->header.typecookie, (long)NGM_PPPOE_COOKIE); 377 continue; 378 } 379 380 switch (rep->header.cmd) { 381 case NGM_PPPOE_SET_FLAG: msg = "SET_FLAG"; break; 382 case NGM_PPPOE_CONNECT: msg = "CONNECT"; break; 383 case NGM_PPPOE_LISTEN: msg = "LISTEN"; break; 384 case NGM_PPPOE_OFFER: msg = "OFFER"; break; 385 case NGM_PPPOE_SUCCESS: msg = "SUCCESS"; break; 386 case NGM_PPPOE_FAIL: msg = "FAIL"; break; 387 case NGM_PPPOE_CLOSE: msg = "CLOSE"; break; 388 case NGM_PPPOE_GET_STATUS: msg = "GET_STATUS"; break; 389 default: 390 snprintf(unknown, sizeof unknown, "<%d>", (int)rep->header.cmd); 391 msg = unknown; 392 break; 393 } 394 395 switch (rep->header.cmd) { 396 case NGM_PPPOE_FAIL: 397 case NGM_PPPOE_CLOSE: 398 syslog(LOG_ERR, "Received NGM_PPPOE_%s (hook \"%s\")", 399 msg, sts->hook); 400 _exit(0); 401 } 402 403 syslog(LOG_INFO, "Received NGM_PPPOE_%s (hook \"%s\")", msg, sts->hook); 404 } while (rep->header.cmd != NGM_PPPOE_SUCCESS); 405 406 dup2(ds, STDIN_FILENO); 407 dup2(ds, STDOUT_FILENO); 408 close(ds); 409 close(cs); 410 411 setsid(); 412 syslog(LOG_INFO, "Executing: %s", exec); 413 execlp(_PATH_BSHELL, _PATH_BSHELL, "-c", exec, (char *)NULL); 414 syslog(LOG_ERR, "execlp failed: %m"); 415 _exit(EX_OSFILE); 416 417 default: 418 wait(&ret); 419 errno = ret; 420 if (errno) 421 syslog(LOG_ERR, "Second fork failed: %m"); 422 break; 423 } 424 } 425 426 #ifndef NOKLDLOAD 427 static int 428 LoadModules(void) 429 { 430 const char *module[] = { "netgraph", "ng_socket", "ng_ether", "ng_pppoe" }; 431 int f; 432 433 for (f = 0; f < sizeof module / sizeof *module; f++) 434 if (modfind(module[f]) == -1 && kldload(module[f]) == -1) { 435 fprintf(stderr, "kldload: %s: %s\n", module[f], strerror(errno)); 436 return 0; 437 } 438 439 return 1; 440 } 441 #endif 442 443 static void 444 nglog(const char *fmt, ...) 445 { 446 char nfmt[256]; 447 va_list ap; 448 449 snprintf(nfmt, sizeof nfmt, "%s: %s", fmt, strerror(errno)); 450 va_start(ap, fmt); 451 vsyslog(LOG_INFO, nfmt, ap); 452 va_end(ap); 453 } 454 455 static void 456 nglogx(const char *fmt, ...) 457 { 458 va_list ap; 459 460 va_start(ap, fmt); 461 vsyslog(LOG_INFO, fmt, ap); 462 va_end(ap); 463 } 464 465 int 466 main(int argc, char **argv) 467 { 468 char hostname[MAXHOSTNAMELEN], *exec, rhook[NG_HOOKLEN + 1]; 469 unsigned char response[1024]; 470 const char *prog, *provider, *acname; 471 struct ngm_connect ngc; 472 int ch, cs, ds, ret, optF, optd, optn, sz, f; 473 const char *pidfile; 474 475 prog = strrchr(argv[0], '/'); 476 prog = prog ? prog + 1 : argv[0]; 477 pidfile = NULL; 478 exec = NULL; 479 acname = NULL; 480 provider = ""; 481 optF = optd = optn = 0; 482 483 while ((ch = getopt(argc, argv, "FP:a:de:n:p:")) != -1) { 484 switch (ch) { 485 case 'F': 486 optF = 1; 487 break; 488 489 case 'P': 490 pidfile = optarg; 491 break; 492 493 case 'a': 494 acname = optarg; 495 break; 496 497 case 'd': 498 optd = 1; 499 break; 500 501 case 'e': 502 exec = optarg; 503 break; 504 505 case 'n': 506 optn = 1; 507 NgSetDebug(atoi(optarg)); 508 break; 509 510 case 'p': 511 provider = optarg; 512 break; 513 514 default: 515 return usage(prog); 516 } 517 } 518 519 if (optind >= argc || optind + 2 < argc) 520 return usage(prog); 521 522 if (exec == NULL) { 523 if (provider == NULL) { 524 fprintf(stderr, "%s: Either a provider or an exec command" 525 " must be given\n", prog); 526 return usage(prog); 527 } 528 exec = (char *)alloca(sizeof DEFAULT_EXEC_PREFIX + strlen(provider)); 529 if (exec == NULL) { 530 fprintf(stderr, "%s: Cannot allocate %d bytes\n", prog, 531 (int)(sizeof DEFAULT_EXEC_PREFIX) + strlen(provider)); 532 return EX_OSERR; 533 } 534 strcpy(exec, DEFAULT_EXEC_PREFIX); 535 strcpy(exec + sizeof DEFAULT_EXEC_PREFIX - 1, provider); 536 } 537 538 if (acname == NULL) { 539 char *dot; 540 541 if (gethostname(hostname, sizeof hostname)) 542 strcpy(hostname, "localhost"); 543 else if ((dot = strchr(hostname, '.'))) 544 *dot = '\0'; 545 546 acname = hostname; 547 } 548 549 #ifndef NOKLDLOAD 550 if (!LoadModules()) 551 return EX_UNAVAILABLE; 552 #endif 553 554 /* Create a socket node */ 555 if (NgMkSockNode(NULL, &cs, &ds) == -1) { 556 perror("Cannot create netgraph socket node"); 557 return EX_CANTCREAT; 558 } 559 560 /* Connect it up (and fill in `ngc') */ 561 if ((ret = ConfigureNode(prog, argv[optind], provider, cs, ds, 562 optd, &ngc)) != 0) { 563 close(cs); 564 close(ds); 565 return ret; 566 } 567 568 if (!optF && daemon(1, 0) == -1) { 569 perror("daemon()"); 570 close(cs); 571 close(ds); 572 return EX_OSERR; 573 } 574 575 576 if (pidfile != NULL) { 577 FILE *fp; 578 579 if ((fp = fopen(pidfile, "w")) == NULL) { 580 perror(pidfile); 581 close(cs); 582 close(ds); 583 return EX_CANTCREAT; 584 } else { 585 fprintf(fp, "%d\n", (int)getpid()); 586 fclose(fp); 587 } 588 } 589 590 openlog(prog, LOG_PID | (optF ? LOG_PERROR : 0), LOG_DAEMON); 591 if (!optF && optn) 592 NgSetErrLog(nglog, nglogx); 593 594 signal(SIGHUP, Farewell); 595 signal(SIGINT, Farewell); 596 signal(SIGQUIT, Farewell); 597 signal(SIGTERM, Farewell); 598 599 while (!ReceivedSignal) { 600 if (*provider) 601 syslog(LOG_INFO, "Listening as provider %s", provider); 602 else 603 syslog(LOG_INFO, "Listening"); 604 605 switch (sz = NgRecvData(ds, response, sizeof response, rhook)) { 606 case -1: 607 syslog(LOG_INFO, "NgRecvData: %m"); 608 break; 609 case 0: 610 syslog(LOG_INFO, "NgRecvData: socket closed"); 611 break; 612 default: 613 if (optd) { 614 char *dbuf, *ptr; 615 616 ptr = dbuf = alloca(sz * 2 + 1); 617 for (f = 0; f < sz; f++, ptr += 2) 618 sprintf(ptr, "%02x", (u_char)response[f]); 619 *ptr = '\0'; 620 syslog(LOG_INFO, "Got %d bytes of data: %s", sz, dbuf); 621 } 622 } 623 if (sz <= 0) { 624 ret = EX_UNAVAILABLE; 625 break; 626 } 627 Spawn(prog, acname, provider, exec, ngc, cs, ds, response, sz, optd); 628 } 629 630 if (pidfile) 631 remove(pidfile); 632 633 if (ReceivedSignal) { 634 syslog(LOG_INFO, "Received signal %d, exiting", ReceivedSignal); 635 636 signal(ReceivedSignal, SIG_DFL); 637 raise(ReceivedSignal); 638 639 /* NOTREACHED */ 640 641 ret = -ReceivedSignal; 642 } 643 644 return ret; 645 } 646