1 /*- 2 * Copyright (c) 1998 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 * $Id: bundle.c,v 1.33 1998/08/25 17:48:42 brian Exp $ 27 */ 28 29 #include <sys/param.h> 30 #include <sys/socket.h> 31 #include <netinet/in.h> 32 #include <net/if.h> 33 #include <arpa/inet.h> 34 #include <net/route.h> 35 #include <net/if_dl.h> 36 #include <netinet/in_systm.h> 37 #include <netinet/ip.h> 38 #include <sys/un.h> 39 40 #ifndef NOALIAS 41 #include <alias.h> 42 #endif 43 #include <errno.h> 44 #include <fcntl.h> 45 #include <paths.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <sys/ioctl.h> 50 #include <sys/uio.h> 51 #include <sys/wait.h> 52 #include <termios.h> 53 #include <unistd.h> 54 55 #include "defs.h" 56 #include "command.h" 57 #include "mbuf.h" 58 #include "log.h" 59 #include "id.h" 60 #include "timer.h" 61 #include "fsm.h" 62 #include "iplist.h" 63 #include "lqr.h" 64 #include "hdlc.h" 65 #include "throughput.h" 66 #include "slcompress.h" 67 #include "ipcp.h" 68 #include "filter.h" 69 #include "descriptor.h" 70 #include "route.h" 71 #include "lcp.h" 72 #include "ccp.h" 73 #include "link.h" 74 #include "mp.h" 75 #include "bundle.h" 76 #include "async.h" 77 #include "physical.h" 78 #include "modem.h" 79 #include "auth.h" 80 #include "lcpproto.h" 81 #include "chap.h" 82 #include "tun.h" 83 #include "prompt.h" 84 #include "chat.h" 85 #include "cbcp.h" 86 #include "datalink.h" 87 #include "ip.h" 88 89 #define SCATTER_SEGMENTS 4 /* version, datalink, name, physical */ 90 #define SOCKET_OVERHEAD 100 /* additional buffer space for large */ 91 /* {recv,send}msg() calls */ 92 93 static int bundle_RemainingIdleTime(struct bundle *); 94 static int bundle_RemainingAutoLoadTime(struct bundle *); 95 96 static const char *PhaseNames[] = { 97 "Dead", "Establish", "Authenticate", "Network", "Terminate" 98 }; 99 100 const char * 101 bundle_PhaseName(struct bundle *bundle) 102 { 103 return bundle->phase <= PHASE_TERMINATE ? 104 PhaseNames[bundle->phase] : "unknown"; 105 } 106 107 void 108 bundle_NewPhase(struct bundle *bundle, u_int new) 109 { 110 if (new == bundle->phase) 111 return; 112 113 if (new <= PHASE_TERMINATE) 114 log_Printf(LogPHASE, "bundle: %s\n", PhaseNames[new]); 115 116 switch (new) { 117 case PHASE_DEAD: 118 log_DisplayPrompts(); 119 bundle->phase = new; 120 break; 121 122 case PHASE_ESTABLISH: 123 bundle->phase = new; 124 break; 125 126 case PHASE_AUTHENTICATE: 127 bundle->phase = new; 128 log_DisplayPrompts(); 129 break; 130 131 case PHASE_NETWORK: 132 ipcp_Setup(&bundle->ncp.ipcp); 133 fsm_Up(&bundle->ncp.ipcp.fsm); 134 fsm_Open(&bundle->ncp.ipcp.fsm); 135 bundle->phase = new; 136 log_DisplayPrompts(); 137 break; 138 139 case PHASE_TERMINATE: 140 bundle->phase = new; 141 mp_Down(&bundle->ncp.mp); 142 log_DisplayPrompts(); 143 break; 144 } 145 } 146 147 static int 148 bundle_CleanInterface(const struct bundle *bundle) 149 { 150 int s; 151 struct ifreq ifrq; 152 struct ifaliasreq ifra; 153 154 s = ID0socket(AF_INET, SOCK_DGRAM, 0); 155 if (s < 0) { 156 log_Printf(LogERROR, "bundle_CleanInterface: socket(): %s\n", 157 strerror(errno)); 158 return (-1); 159 } 160 strncpy(ifrq.ifr_name, bundle->ifp.Name, sizeof ifrq.ifr_name - 1); 161 ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0'; 162 while (ID0ioctl(s, SIOCGIFADDR, &ifrq) == 0) { 163 memset(&ifra.ifra_mask, '\0', sizeof ifra.ifra_mask); 164 strncpy(ifra.ifra_name, bundle->ifp.Name, sizeof ifra.ifra_name - 1); 165 ifra.ifra_name[sizeof ifra.ifra_name - 1] = '\0'; 166 ifra.ifra_addr = ifrq.ifr_addr; 167 if (ID0ioctl(s, SIOCGIFDSTADDR, &ifrq) < 0) { 168 if (ifra.ifra_addr.sa_family == AF_INET) 169 log_Printf(LogERROR, "Can't get dst for %s on %s !\n", 170 inet_ntoa(((struct sockaddr_in *)&ifra.ifra_addr)->sin_addr), 171 bundle->ifp.Name); 172 close(s); 173 return 0; 174 } 175 ifra.ifra_broadaddr = ifrq.ifr_dstaddr; 176 if (ID0ioctl(s, SIOCDIFADDR, &ifra) < 0) { 177 if (ifra.ifra_addr.sa_family == AF_INET) 178 log_Printf(LogERROR, "Can't delete %s address on %s !\n", 179 inet_ntoa(((struct sockaddr_in *)&ifra.ifra_addr)->sin_addr), 180 bundle->ifp.Name); 181 close(s); 182 return 0; 183 } 184 } 185 close(s); 186 187 return 1; 188 } 189 190 static void 191 bundle_LayerStart(void *v, struct fsm *fp) 192 { 193 /* The given FSM is about to start up ! */ 194 } 195 196 197 static void 198 bundle_Notify(struct bundle *bundle, char c) 199 { 200 if (bundle->notify.fd != -1) { 201 if (write(bundle->notify.fd, &c, 1) == 1) 202 log_Printf(LogPHASE, "Parent notified of success.\n"); 203 else 204 log_Printf(LogPHASE, "Failed to notify parent of success.\n"); 205 close(bundle->notify.fd); 206 bundle->notify.fd = -1; 207 } 208 } 209 210 static void 211 bundle_ClearQueues(void *v) 212 { 213 struct bundle *bundle = (struct bundle *)v; 214 struct datalink *dl; 215 216 log_Printf(LogPHASE, "Clearing choked output queue\n"); 217 timer_Stop(&bundle->choked.timer); 218 219 /* 220 * Emergency time: 221 * 222 * We've had a full queue for PACKET_DEL_SECS seconds without being 223 * able to get rid of any of the packets. We've probably given up 224 * on the redials at this point, and the queued data has almost 225 * definitely been timed out by the layer above. As this is preventing 226 * us from reading the TUN_NAME device (we don't want to buffer stuff 227 * indefinitely), we may as well nuke this data and start with a clean 228 * slate ! 229 * 230 * Unfortunately, this has the side effect of shafting any compression 231 * dictionaries in use (causing the relevant RESET_REQ/RESET_ACK). 232 */ 233 234 ip_DeleteQueue(&bundle->ncp.ipcp); 235 mp_DeleteQueue(&bundle->ncp.mp); 236 for (dl = bundle->links; dl; dl = dl->next) 237 physical_DeleteQueue(dl->physical); 238 } 239 240 static void 241 bundle_AutoLoadTimeout(void *v) 242 { 243 struct bundle *bundle = (struct bundle *)v; 244 245 if (bundle->autoload.comingup) { 246 log_Printf(LogPHASE, "autoload: Another link is required\n"); 247 /* bundle_Open() stops the timer */ 248 bundle_Open(bundle, NULL, PHYS_AUTO, 0); 249 } else { 250 struct datalink *dl, *last; 251 252 timer_Stop(&bundle->autoload.timer); 253 for (last = NULL, dl = bundle->links; dl; dl = dl->next) 254 if (dl->physical->type == PHYS_AUTO && dl->state == DATALINK_OPEN) 255 last = dl; 256 257 if (last) 258 datalink_Close(last, CLOSE_STAYDOWN); 259 } 260 } 261 262 static void 263 bundle_StartAutoLoadTimer(struct bundle *bundle, int up) 264 { 265 struct datalink *dl; 266 267 timer_Stop(&bundle->autoload.timer); 268 269 if (bundle->CleaningUp || bundle->phase != PHASE_NETWORK) { 270 dl = NULL; 271 bundle->autoload.running = 0; 272 } else if (up) { 273 for (dl = bundle->links; dl; dl = dl->next) 274 if (dl->state == DATALINK_CLOSED && dl->physical->type == PHYS_AUTO) { 275 if (bundle->cfg.autoload.max.timeout) { 276 bundle->autoload.timer.func = bundle_AutoLoadTimeout; 277 bundle->autoload.timer.name = "autoload up"; 278 bundle->autoload.timer.load = 279 bundle->cfg.autoload.max.timeout * SECTICKS; 280 bundle->autoload.timer.arg = bundle; 281 timer_Start(&bundle->autoload.timer); 282 bundle->autoload.done = time(NULL) + bundle->cfg.autoload.max.timeout; 283 } else 284 bundle_AutoLoadTimeout(bundle); 285 break; 286 } 287 bundle->autoload.running = (dl || bundle->cfg.autoload.min.timeout) ? 1 : 0; 288 } else { 289 int nlinks; 290 struct datalink *adl; 291 292 for (nlinks = 0, adl = NULL, dl = bundle->links; dl; dl = dl->next) 293 if (dl->state == DATALINK_OPEN) { 294 if (dl->physical->type == PHYS_AUTO) 295 adl = dl; 296 if (++nlinks > 1 && adl) { 297 if (bundle->cfg.autoload.min.timeout) { 298 bundle->autoload.timer.func = bundle_AutoLoadTimeout; 299 bundle->autoload.timer.name = "autoload down"; 300 bundle->autoload.timer.load = 301 bundle->cfg.autoload.min.timeout * SECTICKS; 302 bundle->autoload.timer.arg = bundle; 303 timer_Start(&bundle->autoload.timer); 304 bundle->autoload.done = 305 time(NULL) + bundle->cfg.autoload.min.timeout; 306 } 307 break; 308 } 309 } 310 311 bundle->autoload.running = 1; 312 } 313 314 bundle->autoload.comingup = up ? 1 : 0; 315 } 316 317 static void 318 bundle_StopAutoLoadTimer(struct bundle *bundle) 319 { 320 timer_Stop(&bundle->autoload.timer); 321 bundle->autoload.done = 0; 322 } 323 324 static int 325 bundle_RemainingAutoLoadTime(struct bundle *bundle) 326 { 327 if (bundle->autoload.done) 328 return bundle->autoload.done - time(NULL); 329 return -1; 330 } 331 332 static void 333 bundle_LinkAdded(struct bundle *bundle, struct datalink *dl) 334 { 335 bundle->phys_type.all |= dl->physical->type; 336 if (dl->state == DATALINK_OPEN) 337 bundle->phys_type.open |= dl->physical->type; 338 339 /* Note: We only re-add links that are DATALINK_OPEN */ 340 if (dl->physical->type == PHYS_AUTO && 341 bundle->autoload.timer.state == TIMER_STOPPED && 342 dl->state != DATALINK_OPEN && 343 bundle->phase == PHASE_NETWORK) 344 bundle->autoload.running = 1; 345 346 if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) 347 != bundle->phys_type.open && bundle->idle.timer.state == TIMER_STOPPED) 348 /* We may need to start our idle timer */ 349 bundle_StartIdleTimer(bundle); 350 } 351 352 void 353 bundle_LinksRemoved(struct bundle *bundle) 354 { 355 struct datalink *dl; 356 357 bundle->phys_type.all = bundle->phys_type.open = 0; 358 for (dl = bundle->links; dl; dl = dl->next) 359 bundle_LinkAdded(bundle, dl); 360 361 if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) 362 == bundle->phys_type.open) 363 bundle_StopIdleTimer(bundle); 364 } 365 366 static void 367 bundle_LayerUp(void *v, struct fsm *fp) 368 { 369 /* 370 * The given fsm is now up 371 * If it's an LCP, adjust our phys_mode.open value. 372 * If it's an LCP set our mtu (if we're multilink, add up the link 373 * speeds and set the MRRU) and start our autoload timer. 374 * If it's an NCP, tell our -background parent to go away. 375 * If it's the first NCP, start the idle timer. 376 */ 377 struct bundle *bundle = (struct bundle *)v; 378 379 if (fp->proto == PROTO_LCP) { 380 struct physical *p = link2physical(fp->link); 381 382 bundle_LinkAdded(bundle, p->dl); 383 if (bundle->ncp.mp.active) { 384 struct datalink *dl; 385 386 bundle->ifp.Speed = 0; 387 for (dl = bundle->links; dl; dl = dl->next) 388 if (dl->state == DATALINK_OPEN) 389 bundle->ifp.Speed += modem_Speed(dl->physical); 390 tun_configure(bundle, bundle->ncp.mp.peer_mrru); 391 bundle->autoload.running = 1; 392 } else { 393 bundle->ifp.Speed = modem_Speed(p); 394 tun_configure(bundle, fsm2lcp(fp)->his_mru); 395 } 396 } else if (fp->proto == PROTO_IPCP) { 397 bundle_StartIdleTimer(bundle); 398 bundle_Notify(bundle, EX_NORMAL); 399 } 400 } 401 402 static void 403 bundle_LayerDown(void *v, struct fsm *fp) 404 { 405 /* 406 * The given FSM has been told to come down. 407 * If it's our last NCP, stop the idle timer. 408 * If it's an LCP, adjust our phys_type.open value and any timers. 409 * If it's an LCP and we're in multilink mode, adjust our tun 410 * speed and make sure our minimum sequence number is adjusted. 411 */ 412 413 struct bundle *bundle = (struct bundle *)v; 414 415 if (fp->proto == PROTO_IPCP) 416 bundle_StopIdleTimer(bundle); 417 else if (fp->proto == PROTO_LCP) { 418 bundle_LinksRemoved(bundle); /* adjust timers & phys_type values */ 419 if (bundle->ncp.mp.active) { 420 struct datalink *dl; 421 struct datalink *lost; 422 423 bundle->ifp.Speed = 0; 424 lost = NULL; 425 for (dl = bundle->links; dl; dl = dl->next) 426 if (fp == &dl->physical->link.lcp.fsm) 427 lost = dl; 428 else if (dl->state == DATALINK_OPEN) 429 bundle->ifp.Speed += modem_Speed(dl->physical); 430 431 if (bundle->ifp.Speed) 432 /* Don't configure down to a speed of 0 */ 433 tun_configure(bundle, bundle->ncp.mp.link.lcp.his_mru); 434 435 if (lost) 436 mp_LinkLost(&bundle->ncp.mp, lost); 437 else 438 log_Printf(LogALERT, "Oops, lost an unrecognised datalink (%s) !\n", 439 fp->link->name); 440 } 441 } 442 } 443 444 static void 445 bundle_LayerFinish(void *v, struct fsm *fp) 446 { 447 /* The given fsm is now down (fp cannot be NULL) 448 * 449 * If it's the last LCP, fsm_Down all NCPs 450 * If it's the last NCP, fsm_Close all LCPs 451 */ 452 453 struct bundle *bundle = (struct bundle *)v; 454 struct datalink *dl; 455 456 if (fp->proto == PROTO_IPCP) { 457 if (bundle_Phase(bundle) != PHASE_DEAD) 458 bundle_NewPhase(bundle, PHASE_TERMINATE); 459 for (dl = bundle->links; dl; dl = dl->next) 460 datalink_Close(dl, CLOSE_NORMAL); 461 fsm2initial(fp); 462 } else if (fp->proto == PROTO_LCP) { 463 int others_active; 464 465 others_active = 0; 466 for (dl = bundle->links; dl; dl = dl->next) 467 if (fp != &dl->physical->link.lcp.fsm && 468 dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) 469 others_active++; 470 471 if (!others_active) 472 fsm2initial(&bundle->ncp.ipcp.fsm); 473 } 474 } 475 476 int 477 bundle_LinkIsUp(const struct bundle *bundle) 478 { 479 return bundle->ncp.ipcp.fsm.state == ST_OPENED; 480 } 481 482 void 483 bundle_Close(struct bundle *bundle, const char *name, int how) 484 { 485 /* 486 * Please close the given datalink. 487 * If name == NULL or name is the last datalink, fsm_Close all NCPs 488 * (except our MP) 489 * If it isn't the last datalink, just Close that datalink. 490 */ 491 492 struct datalink *dl, *this_dl; 493 int others_active; 494 495 others_active = 0; 496 this_dl = NULL; 497 498 for (dl = bundle->links; dl; dl = dl->next) { 499 if (name && !strcasecmp(name, dl->name)) 500 this_dl = dl; 501 if (name == NULL || this_dl == dl) { 502 switch (how) { 503 case CLOSE_LCP: 504 datalink_DontHangup(dl); 505 /* fall through */ 506 case CLOSE_STAYDOWN: 507 datalink_StayDown(dl); 508 break; 509 } 510 } else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) 511 others_active++; 512 } 513 514 if (name && this_dl == NULL) { 515 log_Printf(LogWARN, "%s: Invalid datalink name\n", name); 516 return; 517 } 518 519 if (!others_active) { 520 bundle_StopIdleTimer(bundle); 521 bundle_StopAutoLoadTimer(bundle); 522 if (bundle->ncp.ipcp.fsm.state > ST_CLOSED || 523 bundle->ncp.ipcp.fsm.state == ST_STARTING) 524 fsm_Close(&bundle->ncp.ipcp.fsm); 525 else { 526 fsm2initial(&bundle->ncp.ipcp.fsm); 527 for (dl = bundle->links; dl; dl = dl->next) 528 datalink_Close(dl, how); 529 } 530 } else if (this_dl && this_dl->state != DATALINK_CLOSED && 531 this_dl->state != DATALINK_HANGUP) 532 datalink_Close(this_dl, how); 533 } 534 535 void 536 bundle_Down(struct bundle *bundle, int how) 537 { 538 struct datalink *dl; 539 540 for (dl = bundle->links; dl; dl = dl->next) 541 datalink_Down(dl, how); 542 } 543 544 static int 545 bundle_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n) 546 { 547 struct bundle *bundle = descriptor2bundle(d); 548 struct datalink *dl; 549 int result, want, queued, nlinks; 550 551 result = 0; 552 553 /* If there are aren't many packets queued, look for some more. */ 554 for (nlinks = 0, dl = bundle->links; dl; dl = dl->next) 555 nlinks++; 556 557 if (nlinks) { 558 queued = r ? bundle_FillQueues(bundle) : ip_QueueLen(&bundle->ncp.ipcp); 559 if (bundle->autoload.running) { 560 if (queued < bundle->cfg.autoload.max.packets) { 561 if (queued > bundle->cfg.autoload.min.packets) 562 bundle_StopAutoLoadTimer(bundle); 563 else if (bundle->autoload.timer.state != TIMER_RUNNING || 564 bundle->autoload.comingup) 565 bundle_StartAutoLoadTimer(bundle, 0); 566 } else if (bundle->autoload.timer.state != TIMER_RUNNING || 567 !bundle->autoload.comingup) 568 bundle_StartAutoLoadTimer(bundle, 1); 569 } 570 571 if (r && (bundle->phase == PHASE_NETWORK || 572 bundle->phys_type.all & PHYS_AUTO)) { 573 /* enough surplus so that we can tell if we're getting swamped */ 574 want = bundle->cfg.autoload.max.packets + nlinks * 2; 575 /* but at least 20 packets ! */ 576 if (want < 20) 577 want = 20; 578 if (queued < want) { 579 /* Not enough - select() for more */ 580 if (bundle->choked.timer.state == TIMER_RUNNING) 581 timer_Stop(&bundle->choked.timer); /* Not needed any more */ 582 FD_SET(bundle->dev.fd, r); 583 if (*n < bundle->dev.fd + 1) 584 *n = bundle->dev.fd + 1; 585 log_Printf(LogTIMER, "%s: fdset(r) %d\n", TUN_NAME, bundle->dev.fd); 586 result++; 587 } else if (bundle->choked.timer.state == TIMER_STOPPED) { 588 bundle->choked.timer.func = bundle_ClearQueues; 589 bundle->choked.timer.name = "output choke"; 590 bundle->choked.timer.load = bundle->cfg.choked.timeout * SECTICKS; 591 bundle->choked.timer.arg = bundle; 592 timer_Start(&bundle->choked.timer); 593 } 594 } 595 } 596 597 /* Which links need a select() ? */ 598 for (dl = bundle->links; dl; dl = dl->next) 599 result += descriptor_UpdateSet(&dl->desc, r, w, e, n); 600 601 /* 602 * This *MUST* be called after the datalink UpdateSet()s as it 603 * might be ``holding'' one of the datalinks (death-row) and 604 * wants to be able to de-select() it from the descriptor set. 605 */ 606 result += descriptor_UpdateSet(&bundle->ncp.mp.server.desc, r, w, e, n); 607 608 return result; 609 } 610 611 static int 612 bundle_IsSet(struct descriptor *d, const fd_set *fdset) 613 { 614 struct bundle *bundle = descriptor2bundle(d); 615 struct datalink *dl; 616 617 for (dl = bundle->links; dl; dl = dl->next) 618 if (descriptor_IsSet(&dl->desc, fdset)) 619 return 1; 620 621 if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 622 return 1; 623 624 return FD_ISSET(bundle->dev.fd, fdset); 625 } 626 627 static void 628 bundle_DescriptorRead(struct descriptor *d, struct bundle *bundle, 629 const fd_set *fdset) 630 { 631 struct datalink *dl; 632 633 if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 634 descriptor_Read(&bundle->ncp.mp.server.desc, bundle, fdset); 635 636 for (dl = bundle->links; dl; dl = dl->next) 637 if (descriptor_IsSet(&dl->desc, fdset)) 638 descriptor_Read(&dl->desc, bundle, fdset); 639 640 if (FD_ISSET(bundle->dev.fd, fdset)) { 641 struct tun_data tun; 642 int n, pri; 643 644 /* something to read from tun */ 645 n = read(bundle->dev.fd, &tun, sizeof tun); 646 if (n < 0) { 647 log_Printf(LogWARN, "read from %s: %s\n", TUN_NAME, strerror(errno)); 648 return; 649 } 650 n -= sizeof tun - sizeof tun.data; 651 if (n <= 0) { 652 log_Printf(LogERROR, "read from %s: Only %d bytes read ?\n", TUN_NAME, n); 653 return; 654 } 655 if (!tun_check_header(tun, AF_INET)) 656 return; 657 658 if (((struct ip *)tun.data)->ip_dst.s_addr == 659 bundle->ncp.ipcp.my_ip.s_addr) { 660 /* we've been asked to send something addressed *to* us :( */ 661 if (Enabled(bundle, OPT_LOOPBACK)) { 662 pri = PacketCheck(bundle, tun.data, n, &bundle->filter.in); 663 if (pri >= 0) { 664 struct mbuf *bp; 665 666 #ifndef NOALIAS 667 if (bundle->AliasEnabled) { 668 PacketAliasIn(tun.data, sizeof tun.data); 669 n = ntohs(((struct ip *)tun.data)->ip_len); 670 } 671 #endif 672 bp = mbuf_Alloc(n, MB_IPIN); 673 memcpy(MBUF_CTOP(bp), tun.data, n); 674 ip_Input(bundle, bp); 675 log_Printf(LogDEBUG, "Looped back packet addressed to myself\n"); 676 } 677 return; 678 } else 679 log_Printf(LogDEBUG, "Oops - forwarding packet addressed to myself\n"); 680 } 681 682 /* 683 * Process on-demand dialup. Output packets are queued within tunnel 684 * device until IPCP is opened. 685 */ 686 687 if (bundle_Phase(bundle) == PHASE_DEAD) { 688 /* 689 * Note, we must be in AUTO mode :-/ otherwise our interface should 690 * *not* be UP and we can't receive data 691 */ 692 if ((pri = PacketCheck(bundle, tun.data, n, &bundle->filter.dial)) >= 0) 693 bundle_Open(bundle, NULL, PHYS_AUTO, 0); 694 else 695 /* 696 * Drop the packet. If we were to queue it, we'd just end up with 697 * a pile of timed-out data in our output queue by the time we get 698 * around to actually dialing. We'd also prematurely reach the 699 * threshold at which we stop select()ing to read() the tun 700 * device - breaking auto-dial. 701 */ 702 return; 703 } 704 705 pri = PacketCheck(bundle, tun.data, n, &bundle->filter.out); 706 if (pri >= 0) { 707 #ifndef NOALIAS 708 if (bundle->AliasEnabled) { 709 PacketAliasOut(tun.data, sizeof tun.data); 710 n = ntohs(((struct ip *)tun.data)->ip_len); 711 } 712 #endif 713 ip_Enqueue(&bundle->ncp.ipcp, pri, tun.data, n); 714 } 715 } 716 } 717 718 static int 719 bundle_DescriptorWrite(struct descriptor *d, struct bundle *bundle, 720 const fd_set *fdset) 721 { 722 struct datalink *dl; 723 int result = 0; 724 725 /* This is not actually necessary as struct mpserver doesn't Write() */ 726 if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 727 descriptor_Write(&bundle->ncp.mp.server.desc, bundle, fdset); 728 729 for (dl = bundle->links; dl; dl = dl->next) 730 if (descriptor_IsSet(&dl->desc, fdset)) 731 result += descriptor_Write(&dl->desc, bundle, fdset); 732 733 return result; 734 } 735 736 void 737 bundle_LockTun(struct bundle *bundle) 738 { 739 FILE *lockfile; 740 char pidfile[MAXPATHLEN]; 741 742 snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit); 743 lockfile = ID0fopen(pidfile, "w"); 744 if (lockfile != NULL) { 745 fprintf(lockfile, "%d\n", (int)getpid()); 746 fclose(lockfile); 747 } 748 #ifndef RELEASE_CRUNCH 749 else 750 log_Printf(LogERROR, "Warning: Can't create %s: %s\n", 751 pidfile, strerror(errno)); 752 #endif 753 } 754 755 static void 756 bundle_UnlockTun(struct bundle *bundle) 757 { 758 char pidfile[MAXPATHLEN]; 759 760 snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit); 761 ID0unlink(pidfile); 762 } 763 764 struct bundle * 765 bundle_Create(const char *prefix, int type, const char **argv) 766 { 767 int s, enoentcount, err; 768 struct ifreq ifrq; 769 static struct bundle bundle; /* there can be only one */ 770 771 if (bundle.ifp.Name != NULL) { /* Already allocated ! */ 772 log_Printf(LogALERT, "bundle_Create: There's only one BUNDLE !\n"); 773 return NULL; 774 } 775 776 err = ENOENT; 777 enoentcount = 0; 778 for (bundle.unit = 0; ; bundle.unit++) { 779 snprintf(bundle.dev.Name, sizeof bundle.dev.Name, "%s%d", 780 prefix, bundle.unit); 781 bundle.dev.fd = ID0open(bundle.dev.Name, O_RDWR); 782 if (bundle.dev.fd >= 0) 783 break; 784 else if (errno == ENXIO) { 785 err = errno; 786 break; 787 } else if (errno == ENOENT) { 788 if (++enoentcount > 2) 789 break; 790 } else 791 err = errno; 792 } 793 794 if (bundle.dev.fd < 0) { 795 log_Printf(LogWARN, "No available tunnel devices found (%s).\n", 796 strerror(err)); 797 return NULL; 798 } 799 800 log_SetTun(bundle.unit); 801 bundle.argv = argv; 802 803 s = socket(AF_INET, SOCK_DGRAM, 0); 804 if (s < 0) { 805 log_Printf(LogERROR, "bundle_Create: socket(): %s\n", strerror(errno)); 806 close(bundle.dev.fd); 807 return NULL; 808 } 809 810 bundle.ifp.Name = strrchr(bundle.dev.Name, '/'); 811 if (bundle.ifp.Name == NULL) 812 bundle.ifp.Name = bundle.dev.Name; 813 else 814 bundle.ifp.Name++; 815 816 /* 817 * Now, bring up the interface. 818 */ 819 memset(&ifrq, '\0', sizeof ifrq); 820 strncpy(ifrq.ifr_name, bundle.ifp.Name, sizeof ifrq.ifr_name - 1); 821 ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0'; 822 if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) { 823 log_Printf(LogERROR, "bundle_Create: ioctl(SIOCGIFFLAGS): %s\n", 824 strerror(errno)); 825 close(s); 826 close(bundle.dev.fd); 827 bundle.ifp.Name = NULL; 828 return NULL; 829 } 830 ifrq.ifr_flags |= IFF_UP; 831 if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) { 832 log_Printf(LogERROR, "bundle_Create: ioctl(SIOCSIFFLAGS): %s\n", 833 strerror(errno)); 834 close(s); 835 close(bundle.dev.fd); 836 bundle.ifp.Name = NULL; 837 return NULL; 838 } 839 840 close(s); 841 842 if ((bundle.ifp.Index = GetIfIndex(bundle.ifp.Name)) < 0) { 843 log_Printf(LogERROR, "Can't find interface index.\n"); 844 close(bundle.dev.fd); 845 bundle.ifp.Name = NULL; 846 return NULL; 847 } 848 log_Printf(LogPHASE, "Using interface: %s\n", bundle.ifp.Name); 849 850 bundle.ifp.Speed = 0; 851 852 bundle.routing_seq = 0; 853 bundle.phase = PHASE_DEAD; 854 bundle.CleaningUp = 0; 855 bundle.AliasEnabled = 0; 856 857 bundle.fsm.LayerStart = bundle_LayerStart; 858 bundle.fsm.LayerUp = bundle_LayerUp; 859 bundle.fsm.LayerDown = bundle_LayerDown; 860 bundle.fsm.LayerFinish = bundle_LayerFinish; 861 bundle.fsm.object = &bundle; 862 863 bundle.cfg.idle_timeout = NCP_IDLE_TIMEOUT; 864 *bundle.cfg.auth.name = '\0'; 865 *bundle.cfg.auth.key = '\0'; 866 bundle.cfg.opt = OPT_SROUTES | OPT_IDCHECK | OPT_LOOPBACK | 867 OPT_THROUGHPUT | OPT_UTMP; 868 *bundle.cfg.label = '\0'; 869 bundle.cfg.mtu = DEF_MTU; 870 bundle.cfg.autoload.max.packets = 0; 871 bundle.cfg.autoload.max.timeout = 0; 872 bundle.cfg.autoload.min.packets = 0; 873 bundle.cfg.autoload.min.timeout = 0; 874 bundle.cfg.choked.timeout = CHOKED_TIMEOUT; 875 bundle.phys_type.all = type; 876 bundle.phys_type.open = 0; 877 878 bundle.links = datalink_Create("deflink", &bundle, type); 879 if (bundle.links == NULL) { 880 log_Printf(LogALERT, "Cannot create data link: %s\n", strerror(errno)); 881 close(bundle.dev.fd); 882 bundle.ifp.Name = NULL; 883 return NULL; 884 } 885 886 bundle.desc.type = BUNDLE_DESCRIPTOR; 887 bundle.desc.UpdateSet = bundle_UpdateSet; 888 bundle.desc.IsSet = bundle_IsSet; 889 bundle.desc.Read = bundle_DescriptorRead; 890 bundle.desc.Write = bundle_DescriptorWrite; 891 892 mp_Init(&bundle.ncp.mp, &bundle); 893 894 /* Send over the first physical link by default */ 895 ipcp_Init(&bundle.ncp.ipcp, &bundle, &bundle.links->physical->link, 896 &bundle.fsm); 897 898 memset(&bundle.filter, '\0', sizeof bundle.filter); 899 bundle.filter.in.fragok = bundle.filter.in.logok = 1; 900 bundle.filter.in.name = "IN"; 901 bundle.filter.out.fragok = bundle.filter.out.logok = 1; 902 bundle.filter.out.name = "OUT"; 903 bundle.filter.dial.name = "DIAL"; 904 bundle.filter.dial.logok = 1; 905 bundle.filter.alive.name = "ALIVE"; 906 bundle.filter.alive.logok = 1; 907 memset(&bundle.idle.timer, '\0', sizeof bundle.idle.timer); 908 bundle.idle.done = 0; 909 bundle.notify.fd = -1; 910 memset(&bundle.autoload.timer, '\0', sizeof bundle.autoload.timer); 911 bundle.autoload.done = 0; 912 bundle.autoload.running = 0; 913 memset(&bundle.choked.timer, '\0', sizeof bundle.choked.timer); 914 915 /* Clean out any leftover crud */ 916 bundle_CleanInterface(&bundle); 917 918 bundle_LockTun(&bundle); 919 920 return &bundle; 921 } 922 923 static void 924 bundle_DownInterface(struct bundle *bundle) 925 { 926 struct ifreq ifrq; 927 int s; 928 929 route_IfDelete(bundle, 1); 930 931 s = ID0socket(AF_INET, SOCK_DGRAM, 0); 932 if (s < 0) { 933 log_Printf(LogERROR, "bundle_DownInterface: socket: %s\n", strerror(errno)); 934 return; 935 } 936 937 memset(&ifrq, '\0', sizeof ifrq); 938 strncpy(ifrq.ifr_name, bundle->ifp.Name, sizeof ifrq.ifr_name - 1); 939 ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0'; 940 if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) { 941 log_Printf(LogERROR, "bundle_DownInterface: ioctl(SIOCGIFFLAGS): %s\n", 942 strerror(errno)); 943 close(s); 944 return; 945 } 946 ifrq.ifr_flags &= ~IFF_UP; 947 if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) { 948 log_Printf(LogERROR, "bundle_DownInterface: ioctl(SIOCSIFFLAGS): %s\n", 949 strerror(errno)); 950 close(s); 951 return; 952 } 953 close(s); 954 } 955 956 void 957 bundle_Destroy(struct bundle *bundle) 958 { 959 struct datalink *dl; 960 961 /* 962 * Clean up the interface. We don't need to timer_Stop()s, mp_Down(), 963 * ipcp_CleanInterface() and bundle_DownInterface() unless we're getting 964 * out under exceptional conditions such as a descriptor exception. 965 */ 966 timer_Stop(&bundle->idle.timer); 967 timer_Stop(&bundle->choked.timer); 968 timer_Stop(&bundle->autoload.timer); 969 mp_Down(&bundle->ncp.mp); 970 ipcp_CleanInterface(&bundle->ncp.ipcp); 971 bundle_DownInterface(bundle); 972 973 /* Again, these are all DATALINK_CLOSED unless we're abending */ 974 dl = bundle->links; 975 while (dl) 976 dl = datalink_Destroy(dl); 977 978 close(bundle->dev.fd); 979 bundle_UnlockTun(bundle); 980 981 /* In case we never made PHASE_NETWORK */ 982 bundle_Notify(bundle, EX_ERRDEAD); 983 984 bundle->ifp.Name = NULL; 985 } 986 987 struct rtmsg { 988 struct rt_msghdr m_rtm; 989 char m_space[64]; 990 }; 991 992 int 993 bundle_SetRoute(struct bundle *bundle, int cmd, struct in_addr dst, 994 struct in_addr gateway, struct in_addr mask, int bang, int ssh) 995 { 996 struct rtmsg rtmes; 997 int s, nb, wb; 998 char *cp; 999 const char *cmdstr; 1000 struct sockaddr_in rtdata; 1001 int result = 1; 1002 1003 if (bang) 1004 cmdstr = (cmd == RTM_ADD ? "Add!" : "Delete!"); 1005 else 1006 cmdstr = (cmd == RTM_ADD ? "Add" : "Delete"); 1007 s = ID0socket(PF_ROUTE, SOCK_RAW, 0); 1008 if (s < 0) { 1009 log_Printf(LogERROR, "bundle_SetRoute: socket(): %s\n", strerror(errno)); 1010 return result; 1011 } 1012 memset(&rtmes, '\0', sizeof rtmes); 1013 rtmes.m_rtm.rtm_version = RTM_VERSION; 1014 rtmes.m_rtm.rtm_type = cmd; 1015 rtmes.m_rtm.rtm_addrs = RTA_DST; 1016 rtmes.m_rtm.rtm_seq = ++bundle->routing_seq; 1017 rtmes.m_rtm.rtm_pid = getpid(); 1018 rtmes.m_rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC; 1019 1020 memset(&rtdata, '\0', sizeof rtdata); 1021 rtdata.sin_len = sizeof rtdata; 1022 rtdata.sin_family = AF_INET; 1023 rtdata.sin_port = 0; 1024 rtdata.sin_addr = dst; 1025 1026 cp = rtmes.m_space; 1027 memcpy(cp, &rtdata, rtdata.sin_len); 1028 cp += rtdata.sin_len; 1029 if (cmd == RTM_ADD) { 1030 if (gateway.s_addr == INADDR_ANY) { 1031 /* Add a route through the interface */ 1032 struct sockaddr_dl dl; 1033 const char *iname; 1034 int ilen; 1035 1036 iname = Index2Nam(bundle->ifp.Index); 1037 ilen = strlen(iname); 1038 dl.sdl_len = sizeof dl - sizeof dl.sdl_data + ilen; 1039 dl.sdl_family = AF_LINK; 1040 dl.sdl_index = bundle->ifp.Index; 1041 dl.sdl_type = 0; 1042 dl.sdl_nlen = ilen; 1043 dl.sdl_alen = 0; 1044 dl.sdl_slen = 0; 1045 strncpy(dl.sdl_data, iname, sizeof dl.sdl_data); 1046 memcpy(cp, &dl, dl.sdl_len); 1047 cp += dl.sdl_len; 1048 rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY; 1049 } else { 1050 rtdata.sin_addr = gateway; 1051 memcpy(cp, &rtdata, rtdata.sin_len); 1052 cp += rtdata.sin_len; 1053 rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY; 1054 } 1055 } 1056 1057 if (dst.s_addr == INADDR_ANY) 1058 mask.s_addr = INADDR_ANY; 1059 1060 if (cmd == RTM_ADD || dst.s_addr == INADDR_ANY) { 1061 rtdata.sin_addr = mask; 1062 memcpy(cp, &rtdata, rtdata.sin_len); 1063 cp += rtdata.sin_len; 1064 rtmes.m_rtm.rtm_addrs |= RTA_NETMASK; 1065 } 1066 1067 nb = cp - (char *) &rtmes; 1068 rtmes.m_rtm.rtm_msglen = nb; 1069 wb = ID0write(s, &rtmes, nb); 1070 if (wb < 0) { 1071 log_Printf(LogTCPIP, "bundle_SetRoute failure:\n"); 1072 log_Printf(LogTCPIP, "bundle_SetRoute: Cmd = %s\n", cmdstr); 1073 log_Printf(LogTCPIP, "bundle_SetRoute: Dst = %s\n", inet_ntoa(dst)); 1074 log_Printf(LogTCPIP, "bundle_SetRoute: Gateway = %s\n", inet_ntoa(gateway)); 1075 log_Printf(LogTCPIP, "bundle_SetRoute: Mask = %s\n", inet_ntoa(mask)); 1076 failed: 1077 if (cmd == RTM_ADD && (rtmes.m_rtm.rtm_errno == EEXIST || 1078 (rtmes.m_rtm.rtm_errno == 0 && errno == EEXIST))) { 1079 if (!bang) { 1080 log_Printf(LogWARN, "Add route failed: %s already exists\n", 1081 inet_ntoa(dst)); 1082 result = 0; /* Don't add to our dynamic list */ 1083 } else { 1084 rtmes.m_rtm.rtm_type = cmd = RTM_CHANGE; 1085 if ((wb = ID0write(s, &rtmes, nb)) < 0) 1086 goto failed; 1087 } 1088 } else if (cmd == RTM_DELETE && 1089 (rtmes.m_rtm.rtm_errno == ESRCH || 1090 (rtmes.m_rtm.rtm_errno == 0 && errno == ESRCH))) { 1091 if (!bang) 1092 log_Printf(LogWARN, "Del route failed: %s: Non-existent\n", 1093 inet_ntoa(dst)); 1094 } else if (rtmes.m_rtm.rtm_errno == 0) { 1095 if (!ssh || errno != ENETUNREACH) 1096 log_Printf(LogWARN, "%s route failed: %s: errno: %s\n", cmdstr, 1097 inet_ntoa(dst), strerror(errno)); 1098 } else 1099 log_Printf(LogWARN, "%s route failed: %s: %s\n", 1100 cmdstr, inet_ntoa(dst), strerror(rtmes.m_rtm.rtm_errno)); 1101 } 1102 log_Printf(LogDEBUG, "wrote %d: cmd = %s, dst = %x, gateway = %x\n", 1103 wb, cmdstr, (unsigned)dst.s_addr, (unsigned)gateway.s_addr); 1104 close(s); 1105 1106 return result; 1107 } 1108 1109 void 1110 bundle_LinkClosed(struct bundle *bundle, struct datalink *dl) 1111 { 1112 /* 1113 * Our datalink has closed. 1114 * CleanDatalinks() (called from DoLoop()) will remove closed 1115 * BACKGROUND and DIRECT links. 1116 * If it's the last data link, enter phase DEAD. 1117 * 1118 * NOTE: dl may not be in our list (bundle_SendDatalink()) ! 1119 */ 1120 1121 struct datalink *odl; 1122 int other_links; 1123 1124 log_SetTtyCommandMode(dl); 1125 1126 other_links = 0; 1127 for (odl = bundle->links; odl; odl = odl->next) 1128 if (odl != dl && odl->state != DATALINK_CLOSED) 1129 other_links++; 1130 1131 if (!other_links) { 1132 if (dl->physical->type != PHYS_AUTO) /* Not in -auto mode */ 1133 bundle_DownInterface(bundle); 1134 fsm2initial(&bundle->ncp.ipcp.fsm); 1135 bundle_NewPhase(bundle, PHASE_DEAD); 1136 bundle_StopIdleTimer(bundle); 1137 bundle_StopAutoLoadTimer(bundle); 1138 bundle->autoload.running = 0; 1139 } else 1140 bundle->autoload.running = 1; 1141 } 1142 1143 void 1144 bundle_Open(struct bundle *bundle, const char *name, int mask, int force) 1145 { 1146 /* 1147 * Please open the given datalink, or all if name == NULL 1148 */ 1149 struct datalink *dl; 1150 1151 timer_Stop(&bundle->autoload.timer); 1152 for (dl = bundle->links; dl; dl = dl->next) 1153 if (name == NULL || !strcasecmp(dl->name, name)) { 1154 if ((mask & dl->physical->type) && 1155 (dl->state == DATALINK_CLOSED || 1156 (force && dl->state == DATALINK_OPENING && 1157 dl->dial_timer.state == TIMER_RUNNING))) { 1158 if (force) 1159 timer_Stop(&dl->dial_timer); 1160 datalink_Up(dl, 1, 1); 1161 if (mask == PHYS_AUTO) 1162 /* Only one AUTO link at a time (see the AutoLoad timer) */ 1163 break; 1164 } 1165 if (name != NULL) 1166 break; 1167 } 1168 } 1169 1170 struct datalink * 1171 bundle2datalink(struct bundle *bundle, const char *name) 1172 { 1173 struct datalink *dl; 1174 1175 if (name != NULL) { 1176 for (dl = bundle->links; dl; dl = dl->next) 1177 if (!strcasecmp(dl->name, name)) 1178 return dl; 1179 } else if (bundle->links && !bundle->links->next) 1180 return bundle->links; 1181 1182 return NULL; 1183 } 1184 1185 int 1186 bundle_FillQueues(struct bundle *bundle) 1187 { 1188 int total; 1189 1190 if (bundle->ncp.mp.active) 1191 total = mp_FillQueues(bundle); 1192 else { 1193 struct datalink *dl; 1194 int add; 1195 1196 for (total = 0, dl = bundle->links; dl; dl = dl->next) 1197 if (dl->state == DATALINK_OPEN) { 1198 add = link_QueueLen(&dl->physical->link); 1199 if (add == 0 && dl->physical->out == NULL) 1200 add = ip_FlushPacket(&dl->physical->link, bundle); 1201 total += add; 1202 } 1203 } 1204 1205 return total + ip_QueueLen(&bundle->ncp.ipcp); 1206 } 1207 1208 int 1209 bundle_ShowLinks(struct cmdargs const *arg) 1210 { 1211 struct datalink *dl; 1212 1213 for (dl = arg->bundle->links; dl; dl = dl->next) { 1214 prompt_Printf(arg->prompt, "Name: %s [%s, %s]", 1215 dl->name, mode2Nam(dl->physical->type), datalink_State(dl)); 1216 if (dl->physical->link.throughput.rolling && dl->state == DATALINK_OPEN) 1217 prompt_Printf(arg->prompt, " weight %d, %d bytes/sec", 1218 dl->mp.weight, 1219 dl->physical->link.throughput.OctetsPerSecond); 1220 prompt_Printf(arg->prompt, "\n"); 1221 } 1222 1223 return 0; 1224 } 1225 1226 static const char * 1227 optval(struct bundle *bundle, int bit) 1228 { 1229 return (bundle->cfg.opt & bit) ? "enabled" : "disabled"; 1230 } 1231 1232 int 1233 bundle_ShowStatus(struct cmdargs const *arg) 1234 { 1235 int remaining; 1236 1237 prompt_Printf(arg->prompt, "Phase %s\n", bundle_PhaseName(arg->bundle)); 1238 prompt_Printf(arg->prompt, " Device: %s\n", arg->bundle->dev.Name); 1239 prompt_Printf(arg->prompt, " Interface: %s @ %lubps\n", 1240 arg->bundle->ifp.Name, arg->bundle->ifp.Speed); 1241 1242 prompt_Printf(arg->prompt, "\nDefaults:\n"); 1243 prompt_Printf(arg->prompt, " Label: %s\n", arg->bundle->cfg.label); 1244 prompt_Printf(arg->prompt, " Auth name: %s\n", 1245 arg->bundle->cfg.auth.name); 1246 prompt_Printf(arg->prompt, " Auto Load: Up after %ds of >= %d packets\n", 1247 arg->bundle->cfg.autoload.max.timeout, 1248 arg->bundle->cfg.autoload.max.packets); 1249 prompt_Printf(arg->prompt, " Down after %ds of <= %d" 1250 " packets\n", arg->bundle->cfg.autoload.min.timeout, 1251 arg->bundle->cfg.autoload.min.packets); 1252 if (arg->bundle->autoload.timer.state == TIMER_RUNNING) 1253 prompt_Printf(arg->prompt, " %ds remaining 'till " 1254 "a link comes %s\n", 1255 bundle_RemainingAutoLoadTime(arg->bundle), 1256 arg->bundle->autoload.comingup ? "up" : "down"); 1257 else 1258 prompt_Printf(arg->prompt, " %srunning with %d" 1259 " packets queued\n", arg->bundle->autoload.running ? 1260 "" : "not ", ip_QueueLen(&arg->bundle->ncp.ipcp)); 1261 1262 prompt_Printf(arg->prompt, " Choked Timer: %ds\n", 1263 arg->bundle->cfg.choked.timeout); 1264 prompt_Printf(arg->prompt, " Idle Timer: "); 1265 if (arg->bundle->cfg.idle_timeout) { 1266 prompt_Printf(arg->prompt, "%ds", arg->bundle->cfg.idle_timeout); 1267 remaining = bundle_RemainingIdleTime(arg->bundle); 1268 if (remaining != -1) 1269 prompt_Printf(arg->prompt, " (%ds remaining)", remaining); 1270 prompt_Printf(arg->prompt, "\n"); 1271 } else 1272 prompt_Printf(arg->prompt, "disabled\n"); 1273 prompt_Printf(arg->prompt, " MTU: "); 1274 if (arg->bundle->cfg.mtu) 1275 prompt_Printf(arg->prompt, "%d\n", arg->bundle->cfg.mtu); 1276 else 1277 prompt_Printf(arg->prompt, "unspecified\n"); 1278 1279 prompt_Printf(arg->prompt, " Sticky Routes: %s\n", 1280 optval(arg->bundle, OPT_SROUTES)); 1281 prompt_Printf(arg->prompt, " ID check: %s\n", 1282 optval(arg->bundle, OPT_IDCHECK)); 1283 prompt_Printf(arg->prompt, " Loopback: %s\n", 1284 optval(arg->bundle, OPT_LOOPBACK)); 1285 prompt_Printf(arg->prompt, " PasswdAuth: %s\n", 1286 optval(arg->bundle, OPT_PASSWDAUTH)); 1287 prompt_Printf(arg->prompt, " Proxy: %s\n", 1288 optval(arg->bundle, OPT_PROXY)); 1289 prompt_Printf(arg->prompt, " Throughput: %s\n", 1290 optval(arg->bundle, OPT_THROUGHPUT)); 1291 prompt_Printf(arg->prompt, " Utmp Logging: %s\n", 1292 optval(arg->bundle, OPT_UTMP)); 1293 1294 return 0; 1295 } 1296 1297 static void 1298 bundle_IdleTimeout(void *v) 1299 { 1300 struct bundle *bundle = (struct bundle *)v; 1301 1302 log_Printf(LogPHASE, "Idle timer expired.\n"); 1303 bundle_StopIdleTimer(bundle); 1304 bundle_Close(bundle, NULL, CLOSE_STAYDOWN); 1305 } 1306 1307 /* 1308 * Start Idle timer. If timeout is reached, we call bundle_Close() to 1309 * close LCP and link. 1310 */ 1311 void 1312 bundle_StartIdleTimer(struct bundle *bundle) 1313 { 1314 timer_Stop(&bundle->idle.timer); 1315 if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) != 1316 bundle->phys_type.open && bundle->cfg.idle_timeout) { 1317 bundle->idle.timer.func = bundle_IdleTimeout; 1318 bundle->idle.timer.name = "idle"; 1319 bundle->idle.timer.load = bundle->cfg.idle_timeout * SECTICKS; 1320 bundle->idle.timer.arg = bundle; 1321 timer_Start(&bundle->idle.timer); 1322 bundle->idle.done = time(NULL) + bundle->cfg.idle_timeout; 1323 } 1324 } 1325 1326 void 1327 bundle_SetIdleTimer(struct bundle *bundle, int value) 1328 { 1329 bundle->cfg.idle_timeout = value; 1330 if (bundle_LinkIsUp(bundle)) 1331 bundle_StartIdleTimer(bundle); 1332 } 1333 1334 void 1335 bundle_StopIdleTimer(struct bundle *bundle) 1336 { 1337 timer_Stop(&bundle->idle.timer); 1338 bundle->idle.done = 0; 1339 } 1340 1341 static int 1342 bundle_RemainingIdleTime(struct bundle *bundle) 1343 { 1344 if (bundle->idle.done) 1345 return bundle->idle.done - time(NULL); 1346 return -1; 1347 } 1348 1349 int 1350 bundle_IsDead(struct bundle *bundle) 1351 { 1352 return !bundle->links || (bundle->phase == PHASE_DEAD && bundle->CleaningUp); 1353 } 1354 1355 static struct datalink * 1356 bundle_DatalinkLinkout(struct bundle *bundle, struct datalink *dl) 1357 { 1358 struct datalink **dlp; 1359 1360 for (dlp = &bundle->links; *dlp; dlp = &(*dlp)->next) 1361 if (*dlp == dl) { 1362 *dlp = dl->next; 1363 dl->next = NULL; 1364 bundle_LinksRemoved(bundle); 1365 return dl; 1366 } 1367 1368 return NULL; 1369 } 1370 1371 static void 1372 bundle_DatalinkLinkin(struct bundle *bundle, struct datalink *dl) 1373 { 1374 struct datalink **dlp = &bundle->links; 1375 1376 while (*dlp) 1377 dlp = &(*dlp)->next; 1378 1379 *dlp = dl; 1380 dl->next = NULL; 1381 1382 bundle_LinkAdded(bundle, dl); 1383 } 1384 1385 void 1386 bundle_CleanDatalinks(struct bundle *bundle) 1387 { 1388 struct datalink **dlp = &bundle->links; 1389 int found = 0; 1390 1391 while (*dlp) 1392 if ((*dlp)->state == DATALINK_CLOSED && 1393 (*dlp)->physical->type & (PHYS_DIRECT|PHYS_BACKGROUND)) { 1394 *dlp = datalink_Destroy(*dlp); 1395 found++; 1396 } else 1397 dlp = &(*dlp)->next; 1398 1399 if (found) 1400 bundle_LinksRemoved(bundle); 1401 } 1402 1403 int 1404 bundle_DatalinkClone(struct bundle *bundle, struct datalink *dl, 1405 const char *name) 1406 { 1407 if (bundle2datalink(bundle, name)) { 1408 log_Printf(LogWARN, "Clone: %s: name already exists\n", name); 1409 return 0; 1410 } 1411 1412 bundle_DatalinkLinkin(bundle, datalink_Clone(dl, name)); 1413 return 1; 1414 } 1415 1416 void 1417 bundle_DatalinkRemove(struct bundle *bundle, struct datalink *dl) 1418 { 1419 dl = bundle_DatalinkLinkout(bundle, dl); 1420 if (dl) 1421 datalink_Destroy(dl); 1422 } 1423 1424 void 1425 bundle_SetLabel(struct bundle *bundle, const char *label) 1426 { 1427 if (label) 1428 strncpy(bundle->cfg.label, label, sizeof bundle->cfg.label - 1); 1429 else 1430 *bundle->cfg.label = '\0'; 1431 } 1432 1433 const char * 1434 bundle_GetLabel(struct bundle *bundle) 1435 { 1436 return *bundle->cfg.label ? bundle->cfg.label : NULL; 1437 } 1438 1439 void 1440 bundle_ReceiveDatalink(struct bundle *bundle, int s, struct sockaddr_un *sun) 1441 { 1442 char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int)]; 1443 struct cmsghdr *cmsg = (struct cmsghdr *)cmsgbuf; 1444 struct msghdr msg; 1445 struct iovec iov[SCATTER_SEGMENTS]; 1446 struct datalink *dl; 1447 int niov, link_fd, expect, f; 1448 pid_t pid; 1449 1450 log_Printf(LogPHASE, "Receiving datalink\n"); 1451 1452 /* Create our scatter/gather array */ 1453 niov = 1; 1454 iov[0].iov_len = strlen(Version) + 1; 1455 iov[0].iov_base = (char *)malloc(iov[0].iov_len); 1456 if (datalink2iov(NULL, iov, &niov, sizeof iov / sizeof *iov, 0) == -1) { 1457 close(s); 1458 return; 1459 } 1460 1461 pid = getpid(); 1462 write(s, &pid, sizeof pid); 1463 1464 for (f = expect = 0; f < niov; f++) 1465 expect += iov[f].iov_len; 1466 1467 /* Set up our message */ 1468 cmsg->cmsg_len = sizeof cmsgbuf; 1469 cmsg->cmsg_level = SOL_SOCKET; 1470 cmsg->cmsg_type = 0; 1471 1472 memset(&msg, '\0', sizeof msg); 1473 msg.msg_name = (caddr_t)sun; 1474 msg.msg_namelen = sizeof *sun; 1475 msg.msg_iov = iov; 1476 msg.msg_iovlen = niov; 1477 msg.msg_control = cmsgbuf; 1478 msg.msg_controllen = sizeof cmsgbuf; 1479 1480 log_Printf(LogDEBUG, "Expecting %d scatter/gather bytes\n", expect); 1481 f = expect + 100; 1482 setsockopt(s, SOL_SOCKET, SO_RCVBUF, &f, sizeof f); 1483 if ((f = recvmsg(s, &msg, MSG_WAITALL)) != expect) { 1484 if (f == -1) 1485 log_Printf(LogERROR, "Failed recvmsg: %s\n", strerror(errno)); 1486 else 1487 log_Printf(LogERROR, "Failed recvmsg: Got %d, not %d\n", f, expect); 1488 while (niov--) 1489 free(iov[niov].iov_base); 1490 close(s); 1491 return; 1492 } 1493 1494 write(s, "!", 1); /* ACK */ 1495 close(s); 1496 1497 if (cmsg->cmsg_type != SCM_RIGHTS) { 1498 log_Printf(LogERROR, "Recvmsg: no descriptor received !\n"); 1499 while (niov--) 1500 free(iov[niov].iov_base); 1501 return; 1502 } 1503 1504 /* We've successfully received an open file descriptor through our socket */ 1505 log_Printf(LogDEBUG, "Receiving device descriptor\n"); 1506 link_fd = *(int *)CMSG_DATA(cmsg); 1507 1508 if (strncmp(Version, iov[0].iov_base, iov[0].iov_len)) { 1509 log_Printf(LogWARN, "Cannot receive datalink, incorrect version" 1510 " (\"%.*s\", not \"%s\")\n", (int)iov[0].iov_len, 1511 (char *)iov[0].iov_base, Version); 1512 close(link_fd); 1513 while (niov--) 1514 free(iov[niov].iov_base); 1515 return; 1516 } 1517 1518 niov = 1; 1519 dl = iov2datalink(bundle, iov, &niov, sizeof iov / sizeof *iov, link_fd); 1520 if (dl) { 1521 bundle_DatalinkLinkin(bundle, dl); 1522 datalink_AuthOk(dl); 1523 } else 1524 close(link_fd); 1525 1526 free(iov[0].iov_base); 1527 } 1528 1529 void 1530 bundle_SendDatalink(struct datalink *dl, int s, struct sockaddr_un *sun) 1531 { 1532 char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int)], ack; 1533 struct cmsghdr *cmsg = (struct cmsghdr *)cmsgbuf; 1534 struct msghdr msg; 1535 struct iovec iov[SCATTER_SEGMENTS]; 1536 int niov, link_fd, f, expect, newsid; 1537 pid_t newpid; 1538 1539 log_Printf(LogPHASE, "Transmitting datalink %s\n", dl->name); 1540 1541 bundle_LinkClosed(dl->bundle, dl); 1542 bundle_DatalinkLinkout(dl->bundle, dl); 1543 1544 /* Build our scatter/gather array */ 1545 iov[0].iov_len = strlen(Version) + 1; 1546 iov[0].iov_base = strdup(Version); 1547 niov = 1; 1548 1549 read(s, &newpid, sizeof newpid); 1550 link_fd = datalink2iov(dl, iov, &niov, sizeof iov / sizeof *iov, newpid); 1551 1552 if (link_fd != -1) { 1553 memset(&msg, '\0', sizeof msg); 1554 1555 msg.msg_name = (caddr_t)sun; 1556 msg.msg_namelen = sizeof *sun; 1557 msg.msg_iov = iov; 1558 msg.msg_iovlen = niov; 1559 1560 cmsg->cmsg_len = sizeof cmsgbuf; 1561 cmsg->cmsg_level = SOL_SOCKET; 1562 cmsg->cmsg_type = SCM_RIGHTS; 1563 *(int *)CMSG_DATA(cmsg) = link_fd; 1564 msg.msg_control = cmsgbuf; 1565 msg.msg_controllen = sizeof cmsgbuf; 1566 1567 for (f = expect = 0; f < niov; f++) 1568 expect += iov[f].iov_len; 1569 1570 log_Printf(LogDEBUG, "Sending %d bytes in scatter/gather array\n", expect); 1571 1572 f = expect + SOCKET_OVERHEAD; 1573 setsockopt(s, SOL_SOCKET, SO_SNDBUF, &f, sizeof f); 1574 if (sendmsg(s, &msg, 0) == -1) 1575 log_Printf(LogERROR, "Failed sendmsg: %s\n", strerror(errno)); 1576 /* We must get the ACK before closing the descriptor ! */ 1577 read(s, &ack, 1); 1578 1579 newsid = tcgetpgrp(link_fd) == getpgrp(); 1580 close(link_fd); 1581 if (newsid) 1582 bundle_setsid(dl->bundle, 1); 1583 } 1584 close(s); 1585 1586 while (niov--) 1587 free(iov[niov].iov_base); 1588 } 1589 1590 int 1591 bundle_RenameDatalink(struct bundle *bundle, struct datalink *ndl, 1592 const char *name) 1593 { 1594 struct datalink *dl; 1595 1596 if (!strcasecmp(ndl->name, name)) 1597 return 1; 1598 1599 for (dl = bundle->links; dl; dl = dl->next) 1600 if (!strcasecmp(dl->name, name)) 1601 return 0; 1602 1603 datalink_Rename(ndl, name); 1604 return 1; 1605 } 1606 1607 int 1608 bundle_SetMode(struct bundle *bundle, struct datalink *dl, int mode) 1609 { 1610 int omode; 1611 1612 omode = dl->physical->type; 1613 if (omode == mode) 1614 return 1; 1615 1616 if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO)) 1617 /* First auto link */ 1618 if (bundle->ncp.ipcp.peer_ip.s_addr == INADDR_ANY) { 1619 log_Printf(LogWARN, "You must `set ifaddr' or `open' before" 1620 " changing mode to %s\n", mode2Nam(mode)); 1621 return 0; 1622 } 1623 1624 if (!datalink_SetMode(dl, mode)) 1625 return 0; 1626 1627 if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO) && 1628 bundle->phase != PHASE_NETWORK) 1629 /* First auto link, we need an interface */ 1630 ipcp_InterfaceUp(&bundle->ncp.ipcp); 1631 1632 /* Regenerate phys_type and adjust autoload & idle timers */ 1633 bundle_LinksRemoved(bundle); 1634 1635 if (omode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO) && 1636 bundle->phase != PHASE_NETWORK) 1637 /* No auto links left */ 1638 ipcp_CleanInterface(&bundle->ncp.ipcp); 1639 1640 return 1; 1641 } 1642 1643 void 1644 bundle_setsid(struct bundle *bundle, int holdsession) 1645 { 1646 /* 1647 * Lose the current session. This means getting rid of our pid 1648 * too so that the tty device will really go away, and any getty 1649 * etc will be allowed to restart. 1650 */ 1651 pid_t pid, orig; 1652 int fds[2]; 1653 char done; 1654 struct datalink *dl; 1655 1656 orig = getpid(); 1657 if (pipe(fds) == -1) { 1658 log_Printf(LogERROR, "pipe: %s\n", strerror(errno)); 1659 return; 1660 } 1661 switch ((pid = fork())) { 1662 case -1: 1663 log_Printf(LogERROR, "fork: %s\n", strerror(errno)); 1664 close(fds[0]); 1665 close(fds[1]); 1666 return; 1667 case 0: 1668 close(fds[0]); 1669 read(fds[1], &done, 1); /* uu_locks are mine ! */ 1670 close(fds[1]); 1671 if (pipe(fds) == -1) { 1672 log_Printf(LogERROR, "pipe(2): %s\n", strerror(errno)); 1673 return; 1674 } 1675 switch ((pid = fork())) { 1676 case -1: 1677 log_Printf(LogERROR, "fork(2): %s\n", strerror(errno)); 1678 close(fds[0]); 1679 close(fds[1]); 1680 return; 1681 case 0: 1682 close(fds[0]); 1683 bundle_LockTun(bundle); /* update pid */ 1684 read(fds[1], &done, 1); /* uu_locks are mine ! */ 1685 close(fds[1]); 1686 setsid(); 1687 log_Printf(LogPHASE, "%d -> %d: %s session control\n", 1688 (int)orig, (int)getpid(), 1689 holdsession ? "Passed" : "Dropped"); 1690 timer_InitService(); 1691 break; 1692 default: 1693 close(fds[1]); 1694 /* Give away all our modem locks (to the final process) */ 1695 for (dl = bundle->links; dl; dl = dl->next) 1696 if (dl->state != DATALINK_CLOSED) 1697 modem_ChangedPid(dl->physical, pid); 1698 write(fds[0], "!", 1); /* done */ 1699 close(fds[0]); 1700 exit(0); 1701 break; 1702 } 1703 break; 1704 default: 1705 close(fds[1]); 1706 /* Give away all our modem locks (to the intermediate process) */ 1707 for (dl = bundle->links; dl; dl = dl->next) 1708 if (dl->state != DATALINK_CLOSED) 1709 modem_ChangedPid(dl->physical, pid); 1710 write(fds[0], "!", 1); /* done */ 1711 close(fds[0]); 1712 if (holdsession) { 1713 int fd, status; 1714 1715 timer_TermService(); 1716 signal(SIGPIPE, SIG_DFL); 1717 signal(SIGALRM, SIG_DFL); 1718 signal(SIGHUP, SIG_DFL); 1719 signal(SIGTERM, SIG_DFL); 1720 signal(SIGINT, SIG_DFL); 1721 signal(SIGQUIT, SIG_DFL); 1722 for (fd = getdtablesize(); fd >= 0; fd--) 1723 close(fd); 1724 setuid(geteuid()); 1725 /* 1726 * Reap the intermediate process. As we're not exiting but the 1727 * intermediate is, we don't want it to become defunct. 1728 */ 1729 waitpid(pid, &status, 0); 1730 /* Tweak our process arguments.... */ 1731 bundle->argv[0] = "session owner"; 1732 bundle->argv[1] = NULL; 1733 /* 1734 * Hang around for a HUP. This should happen as soon as the 1735 * ppp that we passed our ctty descriptor to closes it. 1736 * NOTE: If this process dies, the passed descriptor becomes 1737 * invalid and will give a select() error by setting one 1738 * of the error fds, aborting the other ppp. We don't 1739 * want that to happen ! 1740 */ 1741 pause(); 1742 } 1743 exit(0); 1744 break; 1745 } 1746 } 1747