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 <stdlib.h> 49 #include <string.h> 50 #include <sysexits.h> 51 #include <sys/fcntl.h> 52 #ifndef NOKLDLOAD 53 #include <sys/linker.h> 54 #include <sys/module.h> 55 #endif 56 #include <sys/uio.h> 57 #include <sys/wait.h> 58 #include <syslog.h> 59 #include <termios.h> 60 #include <unistd.h> 61 62 63 #define DEFAULT_EXEC_PREFIX "exec /usr/sbin/ppp -direct " 64 65 static int 66 usage(const char *prog) 67 { 68 fprintf(stderr, "Usage: %s [-Fd] [-P pidfile] [-a name] [-e exec]" 69 " [-p provider] interface\n", prog); 70 return EX_USAGE; 71 } 72 73 const char *pidfile; 74 75 static void 76 Fairwell(int sig) 77 { 78 char buf[] = "Received signal XX, exiting"; 79 80 /* No stdio in a signal handler */ 81 buf[16] = '0' + ((sig / 10) % 10); 82 buf[17] = '0' + (sig % 10); 83 84 syslog(LOG_INFO, buf); 85 86 if (pidfile) 87 remove(pidfile); 88 89 signal(sig, SIG_DFL); 90 raise(sig); 91 } 92 93 static int 94 ConfigureNode(const char *prog, const char *iface, const char *provider, 95 int cs, int ds, int debug, struct ngm_connect *ngc) 96 { 97 /* 98 * We're going to do this with the passed `ds' & `cs' descriptors: 99 * 100 * .---------. 101 * | ether | 102 * | <iface> | 103 * `---------' 104 * (orphan) ds cs 105 * | | | 106 * | | | 107 * (ethernet) | | 108 * .---------. .-----------. 109 * | pppoe | | socket | 110 * | <iface> |(pppoe-<pid>)<---->(pppoe-<pid>)| <unnamed> | 111 * `--------- `-----------' 112 * (exec-<pid>) 113 * ^ .-----------. .-------------. 114 * | | socket | | ppp -direct | 115 * `--->(exec-<pid>)| <unnamed> |--fd--| provider | 116 * `-----------' `-------------' 117 * 118 * where there are potentially many ppp processes running off of the 119 * same PPPoE node. 120 * The exec-<pid> hook isn't made 'till we Spawn(). 121 */ 122 123 char *epath, *spath; 124 struct ngpppoe_init_data *data; 125 const struct hooklist *hlist; 126 const struct nodeinfo *ninfo; 127 const struct linkinfo *nlink; 128 struct ngm_mkpeer mkp; 129 struct ng_mesg *resp; 130 u_char rbuf[2048]; 131 int f, plen; 132 133 /* 134 * Ask for a list of hooks attached to the "ether" node. This node should 135 * magically exist as a way of hooking stuff onto an ethernet device 136 */ 137 epath = (char *)alloca(strlen(iface) + 2); 138 sprintf(epath, "%s:", iface); 139 140 if (debug) 141 fprintf(stderr, "Sending NGM_LISTHOOKS to %s\n", epath); 142 143 if (NgSendMsg(cs, epath, NGM_GENERIC_COOKIE, NGM_LISTHOOKS, NULL, 0) < 0) { 144 if (errno == ENOENT) 145 fprintf(stderr, "%s Cannot send a netgraph message: Invalid interface\n", 146 epath); 147 else 148 fprintf(stderr, "%s Cannot send a netgraph message: %s\n", 149 epath, strerror(errno)); 150 return EX_UNAVAILABLE; 151 } 152 153 /* Get our list back */ 154 resp = (struct ng_mesg *)rbuf; 155 if (NgRecvMsg(cs, resp, sizeof rbuf, NULL) < 0) { 156 perror("Cannot get netgraph response"); 157 return EX_UNAVAILABLE; 158 } 159 160 hlist = (const struct hooklist *)resp->data; 161 ninfo = &hlist->nodeinfo; 162 163 if (debug) 164 fprintf(stderr, "Got reply from id [%x]: Type %s with %d hooks\n", 165 ninfo->id, ninfo->type, ninfo->hooks); 166 167 /* Make sure we've got the right type of node */ 168 if (strncmp(ninfo->type, NG_ETHER_NODE_TYPE, sizeof NG_ETHER_NODE_TYPE - 1)) { 169 fprintf(stderr, "%s Unexpected node type ``%s'' (wanted ``" 170 NG_ETHER_NODE_TYPE "'')\n", epath, ninfo->type); 171 return EX_DATAERR; 172 } 173 174 /* look for a hook already attached. */ 175 for (f = 0; f < ninfo->hooks; f++) { 176 nlink = &hlist->link[f]; 177 178 if (debug) 179 fprintf(stderr, " Got [%x]:%s -> [%x]:%s\n", ninfo->id, 180 nlink->ourhook, nlink->nodeinfo.id, nlink->peerhook); 181 182 if (!strcmp(nlink->ourhook, NG_ETHER_HOOK_ORPHAN) || 183 !strcmp(nlink->ourhook, NG_ETHER_HOOK_DIVERT)) { 184 /* 185 * Something is using the data coming out of this `ether' node. 186 * If it's a PPPoE node, we use that node, otherwise we complain that 187 * someone else is using the node. 188 */ 189 if (strcmp(nlink->nodeinfo.type, NG_PPPOE_NODE_TYPE)) { 190 fprintf(stderr, "%s Node type %s is currently active\n", 191 epath, nlink->nodeinfo.type); 192 return EX_UNAVAILABLE; 193 } 194 break; 195 } 196 } 197 198 if (f == ninfo->hooks) { 199 /* 200 * Create a new PPPoE node connected to the `ether' node using 201 * the magic `orphan' and `ethernet' hooks 202 */ 203 snprintf(mkp.type, sizeof mkp.type, "%s", NG_PPPOE_NODE_TYPE); 204 snprintf(mkp.ourhook, sizeof mkp.ourhook, "%s", NG_ETHER_HOOK_ORPHAN); 205 snprintf(mkp.peerhook, sizeof mkp.peerhook, "%s", NG_PPPOE_HOOK_ETHERNET); 206 207 if (debug) 208 fprintf(stderr, "Send MKPEER: %s%s -> [type %s]:%s\n", epath, 209 mkp.ourhook, mkp.type, mkp.peerhook); 210 211 if (NgSendMsg(cs, epath, NGM_GENERIC_COOKIE, 212 NGM_MKPEER, &mkp, sizeof mkp) < 0) { 213 fprintf(stderr, "%s Cannot create a peer PPPoE node: %s\n", 214 epath, strerror(errno)); 215 return EX_OSERR; 216 } 217 } 218 219 /* Connect the PPPoE node to our socket node. */ 220 snprintf(ngc->path, sizeof ngc->path, "%s%s", epath, NG_ETHER_HOOK_ORPHAN); 221 snprintf(ngc->ourhook, sizeof ngc->ourhook, "pppoe-%ld", (long)getpid()); 222 memcpy(ngc->peerhook, ngc->ourhook, sizeof ngc->peerhook); 223 224 if (NgSendMsg(cs, ".:", NGM_GENERIC_COOKIE, 225 NGM_CONNECT, ngc, sizeof *ngc) < 0) { 226 perror("Cannot CONNECT PPPoE and socket nodes"); 227 return EX_OSERR; 228 } 229 230 plen = strlen(provider); 231 232 data = (struct ngpppoe_init_data *)alloca(sizeof *data + plen + 1); 233 snprintf(data->hook, sizeof data->hook, "%s", ngc->peerhook); 234 strcpy(data->data, provider); 235 data->data_len = plen; 236 237 spath = (char *)alloca(strlen(ngc->peerhook) + 3); 238 strcpy(spath, ".:"); 239 strcpy(spath + 2, ngc->ourhook); 240 241 if (debug) { 242 if (provider) 243 fprintf(stderr, "Sending PPPOE_LISTEN to %s, provider %s\n", 244 spath, provider); 245 else 246 fprintf(stderr, "Sending PPPOE_LISTEN to %s\n", spath); 247 } 248 249 if (NgSendMsg(cs, spath, NGM_PPPOE_COOKIE, NGM_PPPOE_LISTEN, 250 data, sizeof *data + plen) == -1) { 251 fprintf(stderr, "%s: Cannot LISTEN on netgraph node: %s\n", 252 spath, strerror(errno)); 253 return EX_OSERR; 254 } 255 256 return 0; 257 } 258 259 static void 260 Spawn(const char *prog, const char *acname, const char *exec, 261 struct ngm_connect ngc, int cs, int ds, void *request, int sz, 262 int debug) 263 { 264 char msgbuf[sizeof(struct ng_mesg) + sizeof(struct ngpppoe_sts)]; 265 struct ng_mesg *rep = (struct ng_mesg *)msgbuf; 266 struct ngpppoe_sts *sts = (struct ngpppoe_sts *)(msgbuf + sizeof *rep); 267 struct ngpppoe_init_data *data; 268 char unknown[14], *path; 269 const char *msg; 270 int ret, slen; 271 272 switch ((ret = fork())) { 273 case -1: 274 syslog(LOG_ERR, "fork: %m"); 275 break; 276 277 case 0: 278 switch (fork()) { 279 case 0: 280 break; 281 case -1: 282 _exit(errno); 283 default: 284 _exit(0); 285 } 286 close(cs); 287 close(ds); 288 289 /* Create a new socket node */ 290 if (debug) 291 syslog(LOG_INFO, "Creating a new socket node"); 292 293 if (NgMkSockNode(NULL, &cs, &ds) == -1) { 294 syslog(LOG_ERR, "Cannot create netgraph socket node: %m"); 295 _exit(EX_CANTCREAT); 296 } 297 298 /* Connect the PPPoE node to our new socket node. */ 299 snprintf(ngc.ourhook, sizeof ngc.ourhook, "exec-%ld", (long)getpid()); 300 memcpy(ngc.peerhook, ngc.ourhook, sizeof ngc.peerhook); 301 302 if (debug) 303 syslog(LOG_INFO, "Sending CONNECT from .:%s -> %s.%s", 304 ngc.ourhook, ngc.path, ngc.peerhook); 305 if (NgSendMsg(cs, ".:", NGM_GENERIC_COOKIE, 306 NGM_CONNECT, &ngc, sizeof ngc) < 0) { 307 syslog(LOG_ERR, "Cannot CONNECT PPPoE and socket nodes: %m"); 308 _exit(EX_OSERR); 309 } 310 311 /* 312 * If we tell the socket node not to LINGER, it will go away when 313 * the last hook is removed. 314 */ 315 if (debug) 316 syslog(LOG_INFO, "Sending NGM_SOCK_CMD_NOLINGER to socket"); 317 if (NgSendMsg(cs, ".:", NGM_SOCKET_COOKIE, 318 NGM_SOCK_CMD_NOLINGER, NULL, 0) < 0) { 319 syslog(LOG_ERR, "Cannot send NGM_SOCK_CMD_NOLINGER: %m"); 320 _exit(EX_OSERR); 321 } 322 323 /* Put the PPPoE node into OFFER mode */ 324 slen = strlen(acname); 325 data = (struct ngpppoe_init_data *)alloca(sizeof *data + slen + 1); 326 snprintf(data->hook, sizeof data->hook, "%s", ngc.ourhook); 327 strcpy(data->data, acname); 328 data->data_len = slen; 329 330 path = (char *)alloca(strlen(ngc.ourhook) + 3); 331 strcpy(path, ".:"); 332 strcpy(path + 2, ngc.ourhook); 333 334 syslog(LOG_INFO, "Offering to %s as access concentrator %s", 335 path, acname); 336 if (NgSendMsg(cs, path, NGM_PPPOE_COOKIE, NGM_PPPOE_OFFER, 337 data, sizeof *data + slen) == -1) { 338 syslog(LOG_INFO, "%s: Cannot OFFER on netgraph node: %m", path); 339 _exit(EX_OSERR); 340 } 341 342 /* And send our request data to the waiting node */ 343 if (debug) 344 syslog(LOG_INFO, "Sending original request to %s (%d bytes)", path, sz); 345 if (NgSendData(ds, ngc.ourhook, request, sz) == -1) { 346 syslog(LOG_ERR, "Cannot send original request to %s: %m", path); 347 _exit(EX_OSERR); 348 } 349 350 /* Then wait for a success indication */ 351 352 if (debug) 353 syslog(LOG_INFO, "Waiting for a SUCCESS reply %s", path); 354 355 do { 356 if (NgRecvMsg(cs, rep, sizeof msgbuf, NULL) < 0) { 357 syslog(LOG_ERR, "%s: Cannot receive a message: %m", path); 358 _exit(EX_OSERR); 359 } 360 361 if (rep->header.version != NG_VERSION) { 362 syslog(LOG_ERR, "%ld: Unexpected netgraph version, expected %ld", 363 (long)rep->header.version, (long)NG_VERSION); 364 _exit(EX_PROTOCOL); 365 } 366 367 if (rep->header.typecookie != NGM_PPPOE_COOKIE) { 368 syslog(LOG_INFO, "%ld: Unexpected netgraph cookie, expected %ld", 369 (long)rep->header.typecookie, (long)NGM_PPPOE_COOKIE); 370 continue; 371 } 372 373 switch (rep->header.cmd) { 374 case NGM_PPPOE_SET_FLAG: msg = "SET_FLAG"; break; 375 case NGM_PPPOE_CONNECT: msg = "CONNECT"; break; 376 case NGM_PPPOE_LISTEN: msg = "LISTEN"; break; 377 case NGM_PPPOE_OFFER: msg = "OFFER"; break; 378 case NGM_PPPOE_SUCCESS: msg = "SUCCESS"; break; 379 case NGM_PPPOE_FAIL: msg = "FAIL"; break; 380 case NGM_PPPOE_CLOSE: msg = "CLOSE"; break; 381 case NGM_PPPOE_GET_STATUS: msg = "GET_STATUS"; break; 382 default: 383 snprintf(unknown, sizeof unknown, "<%d>", (int)rep->header.cmd); 384 msg = unknown; 385 break; 386 } 387 388 switch (rep->header.cmd) { 389 case NGM_PPPOE_FAIL: 390 case NGM_PPPOE_CLOSE: 391 syslog(LOG_ERR, "Received NGM_PPPOE_%s (hook \"%s\")", 392 msg, sts->hook); 393 _exit(0); 394 } 395 396 syslog(LOG_INFO, "Received NGM_PPPOE_%s (hook \"%s\")", msg, sts->hook); 397 } while (rep->header.cmd != NGM_PPPOE_SUCCESS); 398 399 dup2(ds, STDIN_FILENO); 400 dup2(ds, STDOUT_FILENO); 401 close(ds); 402 close(cs); 403 404 setsid(); 405 syslog(LOG_INFO, "Executing: %s", exec); 406 execlp(_PATH_BSHELL, _PATH_BSHELL, "-c", exec, NULL); 407 syslog(LOG_ERR, "execlp failed: %m"); 408 _exit(EX_OSFILE); 409 410 default: 411 wait(&ret); 412 errno = ret; 413 if (errno) 414 syslog(LOG_ERR, "Second fork failed: %m"); 415 break; 416 } 417 } 418 419 int 420 main(int argc, char **argv) 421 { 422 char hostname[MAXHOSTNAMELEN]; 423 char response[1024], *exec, rhook[NG_HOOKLEN + 1]; 424 const char *prog, *provider, *acname; 425 struct ngm_connect ngc; 426 int ch, cs, ds, ret, optF, optd, sz, f; 427 428 prog = strrchr(argv[0], '/'); 429 prog = prog ? prog + 1 : argv[0]; 430 pidfile = NULL; 431 exec = NULL; 432 acname = NULL; 433 provider = ""; 434 optF = optd = 0; 435 436 while ((ch = getopt(argc, argv, "FP:a:de:p:")) != -1) { 437 switch (ch) { 438 case 'F': 439 optF = 1; 440 break; 441 442 case 'P': 443 pidfile = optarg; 444 break; 445 446 case 'a': 447 acname = optarg; 448 break; 449 450 case 'd': 451 optd = 1; 452 break; 453 454 case 'e': 455 exec = optarg; 456 break; 457 458 case 'p': 459 provider = optarg; 460 break; 461 462 default: 463 return usage(prog); 464 } 465 } 466 467 if (optind >= argc || optind + 2 < argc) 468 return usage(prog); 469 470 if (exec == NULL) { 471 if (provider == NULL) { 472 fprintf(stderr, "%s: Either a provider or an exec command" 473 " must be given\n", prog); 474 return usage(prog); 475 } 476 exec = (char *)alloca(sizeof DEFAULT_EXEC_PREFIX + strlen(provider)); 477 if (exec == NULL) { 478 fprintf(stderr, "%s: Cannot allocate %d bytes\n", prog, 479 (int)(sizeof DEFAULT_EXEC_PREFIX) + strlen(provider)); 480 return EX_OSERR; 481 } 482 strcpy(exec, DEFAULT_EXEC_PREFIX); 483 strcpy(exec + sizeof DEFAULT_EXEC_PREFIX - 1, provider); 484 } 485 486 if (acname == NULL) { 487 char *dot; 488 489 if (gethostname(hostname, sizeof hostname)) 490 strcpy(hostname, "localhost"); 491 else if ((dot = strchr(hostname, '.'))) 492 *dot = '\0'; 493 494 acname = hostname; 495 } 496 497 #ifndef NOKLDLOAD 498 if (modfind("netgraph") == -1) { 499 fputs("Can't run without options NETGRAPH in the kernel\n", stderr); 500 return EX_UNAVAILABLE; 501 } 502 503 if (modfind("ng_socket") == -1 && kldload("ng_socket") == -1) { 504 perror("kldload: ng_socket"); 505 return EX_UNAVAILABLE; 506 } 507 #endif 508 509 /* Create a socket node */ 510 if (NgMkSockNode(NULL, &cs, &ds) == -1) { 511 perror("Cannot create netgraph socket node"); 512 return EX_CANTCREAT; 513 } 514 515 /* Connect it up (and fill in `ngc') */ 516 if ((ret = ConfigureNode(prog, argv[optind], provider, cs, ds, 517 optd, &ngc)) != 0) { 518 close(cs); 519 close(ds); 520 return ret; 521 } 522 523 if (!optF && daemon(1, 0) == -1) { 524 perror("daemon()"); 525 close(cs); 526 close(ds); 527 return EX_OSERR; 528 } 529 530 531 if (pidfile != NULL) { 532 FILE *fp; 533 534 if ((fp = fopen(pidfile, "w")) == NULL) { 535 perror(pidfile); 536 close(cs); 537 close(ds); 538 return EX_CANTCREAT; 539 } else { 540 fprintf(fp, "%d\n", (int)getpid()); 541 fclose(fp); 542 } 543 } 544 545 openlog(prog, LOG_PID | (optF ? LOG_PERROR : 0), LOG_DAEMON); 546 547 signal(SIGHUP, Fairwell); 548 signal(SIGINT, Fairwell); 549 signal(SIGQUIT, Fairwell); 550 signal(SIGTERM, Fairwell); 551 552 while (1) { 553 if (*provider) 554 syslog(LOG_INFO, "Listening as provider %s", provider); 555 else 556 syslog(LOG_INFO, "Listening"); 557 558 switch (sz = NgRecvData(ds, response, sizeof response, rhook)) { 559 case -1: 560 syslog(LOG_INFO, "NgRecvData: %m"); 561 break; 562 case 0: 563 syslog(LOG_INFO, "NgRecvData: socket closed"); 564 break; 565 default: 566 if (optd) { 567 char *dbuf, *ptr; 568 569 ptr = dbuf = alloca(sz * 2 + 1); 570 for (f = 0; f < sz; f++, ptr += 2) 571 sprintf(ptr, "%02x", (u_char)response[f]); 572 *ptr = '\0'; 573 syslog(LOG_INFO, "Got %d bytes of data: %s", sz, dbuf); 574 } 575 } 576 if (sz <= 0) { 577 ret = EX_UNAVAILABLE; 578 break; 579 } 580 Spawn(prog, acname, exec, ngc, cs, ds, response, sz, optd); 581 } 582 583 return ret; 584 } 585