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 * $FreeBSD$ 27 */ 28 29 #include <sys/param.h> 30 #include <sys/socket.h> 31 #include <netinet/in.h> 32 #include <net/if.h> 33 #include <net/if_tun.h> /* For TUNS* ioctls */ 34 #include <net/route.h> 35 #include <netinet/in_systm.h> 36 #include <netinet/ip.h> 37 #include <sys/un.h> 38 39 #include <errno.h> 40 #include <fcntl.h> 41 #ifdef __OpenBSD__ 42 #include <util.h> 43 #else 44 #include <libutil.h> 45 #endif 46 #include <paths.h> 47 #include <stdarg.h> 48 #include <stdio.h> 49 #include <stdlib.h> 50 #include <string.h> 51 #include <sys/uio.h> 52 #include <sys/wait.h> 53 #include <termios.h> 54 #include <unistd.h> 55 56 #include "layer.h" 57 #include "defs.h" 58 #include "command.h" 59 #include "mbuf.h" 60 #include "log.h" 61 #include "id.h" 62 #include "timer.h" 63 #include "fsm.h" 64 #include "iplist.h" 65 #include "lqr.h" 66 #include "hdlc.h" 67 #include "throughput.h" 68 #include "slcompress.h" 69 #include "ncpaddr.h" 70 #include "ip.h" 71 #include "ipcp.h" 72 #include "filter.h" 73 #include "descriptor.h" 74 #include "route.h" 75 #include "lcp.h" 76 #include "ccp.h" 77 #include "link.h" 78 #include "mp.h" 79 #ifndef NORADIUS 80 #include "radius.h" 81 #endif 82 #include "ipv6cp.h" 83 #include "ncp.h" 84 #include "bundle.h" 85 #include "async.h" 86 #include "physical.h" 87 #include "auth.h" 88 #include "proto.h" 89 #include "chap.h" 90 #include "tun.h" 91 #include "prompt.h" 92 #include "chat.h" 93 #include "cbcp.h" 94 #include "datalink.h" 95 #include "iface.h" 96 #include "server.h" 97 #include "probe.h" 98 #ifndef NODES 99 #include "mppe.h" 100 #endif 101 102 #define SCATTER_SEGMENTS 7 /* version, datalink, name, physical, 103 throughput, throughput, device */ 104 105 #define SEND_MAXFD 3 /* Max file descriptors passed through 106 the local domain socket */ 107 108 static int bundle_RemainingIdleTime(struct bundle *); 109 110 static const char * const PhaseNames[] = { 111 "Dead", "Establish", "Authenticate", "Network", "Terminate" 112 }; 113 114 const char * 115 bundle_PhaseName(struct bundle *bundle) 116 { 117 return bundle->phase <= PHASE_TERMINATE ? 118 PhaseNames[bundle->phase] : "unknown"; 119 } 120 121 void 122 bundle_NewPhase(struct bundle *bundle, u_int new) 123 { 124 if (new == bundle->phase) 125 return; 126 127 if (new <= PHASE_TERMINATE) 128 log_Printf(LogPHASE, "bundle: %s\n", PhaseNames[new]); 129 130 switch (new) { 131 case PHASE_DEAD: 132 bundle->phase = new; 133 #ifndef NODES 134 MPPE_MasterKeyValid = 0; 135 #endif 136 log_DisplayPrompts(); 137 break; 138 139 case PHASE_ESTABLISH: 140 bundle->phase = new; 141 break; 142 143 case PHASE_AUTHENTICATE: 144 bundle->phase = new; 145 log_DisplayPrompts(); 146 break; 147 148 case PHASE_NETWORK: 149 if (ncp_fsmStart(&bundle->ncp, bundle)) { 150 bundle->phase = new; 151 log_DisplayPrompts(); 152 } else { 153 log_Printf(LogPHASE, "bundle: All NCPs are disabled\n"); 154 bundle_Close(bundle, NULL, CLOSE_STAYDOWN); 155 } 156 break; 157 158 case PHASE_TERMINATE: 159 bundle->phase = new; 160 mp_Down(&bundle->ncp.mp); 161 log_DisplayPrompts(); 162 break; 163 } 164 } 165 166 static void 167 bundle_LayerStart(void *v __unused, struct fsm *fp __unused) 168 { 169 /* The given FSM is about to start up ! */ 170 } 171 172 173 void 174 bundle_Notify(struct bundle *bundle, char c) 175 { 176 if (bundle->notify.fd != -1) { 177 int ret; 178 179 ret = write(bundle->notify.fd, &c, 1); 180 if (c != EX_REDIAL && c != EX_RECONNECT) { 181 if (ret == 1) 182 log_Printf(LogCHAT, "Parent notified of %s\n", 183 c == EX_NORMAL ? "success" : "failure"); 184 else 185 log_Printf(LogERROR, "Failed to notify parent of success\n"); 186 close(bundle->notify.fd); 187 bundle->notify.fd = -1; 188 } else if (ret == 1) 189 log_Printf(LogCHAT, "Parent notified of %s\n", ex_desc(c)); 190 else 191 log_Printf(LogERROR, "Failed to notify parent of %s\n", ex_desc(c)); 192 } 193 } 194 195 static void 196 bundle_ClearQueues(void *v) 197 { 198 struct bundle *bundle = (struct bundle *)v; 199 struct datalink *dl; 200 201 log_Printf(LogPHASE, "Clearing choked output queue\n"); 202 timer_Stop(&bundle->choked.timer); 203 204 /* 205 * Emergency time: 206 * 207 * We've had a full queue for PACKET_DEL_SECS seconds without being 208 * able to get rid of any of the packets. We've probably given up 209 * on the redials at this point, and the queued data has almost 210 * definitely been timed out by the layer above. As this is preventing 211 * us from reading the TUN_NAME device (we don't want to buffer stuff 212 * indefinitely), we may as well nuke this data and start with a clean 213 * slate ! 214 * 215 * Unfortunately, this has the side effect of shafting any compression 216 * dictionaries in use (causing the relevant RESET_REQ/RESET_ACK). 217 */ 218 219 ncp_DeleteQueues(&bundle->ncp); 220 for (dl = bundle->links; dl; dl = dl->next) 221 physical_DeleteQueue(dl->physical); 222 } 223 224 static void 225 bundle_LinkAdded(struct bundle *bundle, struct datalink *dl) 226 { 227 bundle->phys_type.all |= dl->physical->type; 228 if (dl->state == DATALINK_OPEN) 229 bundle->phys_type.open |= dl->physical->type; 230 231 #ifndef NORADIUS 232 if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) 233 != bundle->phys_type.open && bundle->session.timer.state == TIMER_STOPPED) 234 if (bundle->radius.sessiontime) 235 bundle_StartSessionTimer(bundle, 0); 236 #endif 237 238 if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) 239 != bundle->phys_type.open && bundle->idle.timer.state == TIMER_STOPPED) 240 /* We may need to start our idle timer */ 241 bundle_StartIdleTimer(bundle, 0); 242 } 243 244 void 245 bundle_LinksRemoved(struct bundle *bundle) 246 { 247 struct datalink *dl; 248 249 bundle->phys_type.all = bundle->phys_type.open = 0; 250 for (dl = bundle->links; dl; dl = dl->next) 251 bundle_LinkAdded(bundle, dl); 252 253 bundle_CalculateBandwidth(bundle); 254 mp_CheckAutoloadTimer(&bundle->ncp.mp); 255 256 if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) 257 == bundle->phys_type.open) { 258 #ifndef NORADIUS 259 if (bundle->radius.sessiontime) 260 bundle_StopSessionTimer(bundle); 261 #endif 262 bundle_StopIdleTimer(bundle); 263 } 264 } 265 266 static void 267 bundle_LayerUp(void *v, struct fsm *fp) 268 { 269 /* 270 * The given fsm is now up 271 * If it's an LCP, adjust our phys_mode.open value and check the 272 * autoload timer. 273 * If it's the first NCP, calculate our bandwidth 274 * If it's the first NCP, set our ``upat'' time 275 * If it's the first NCP, start the idle timer. 276 * If it's an NCP, tell our -background parent to go away. 277 * If it's the first NCP, start the autoload timer 278 */ 279 struct bundle *bundle = (struct bundle *)v; 280 281 if (fp->proto == PROTO_LCP) { 282 struct physical *p = link2physical(fp->link); 283 284 bundle_LinkAdded(bundle, p->dl); 285 mp_CheckAutoloadTimer(&bundle->ncp.mp); 286 } else if (isncp(fp->proto)) { 287 if (ncp_LayersOpen(&fp->bundle->ncp) == 1) { 288 bundle_CalculateBandwidth(fp->bundle); 289 time(&bundle->upat); 290 #ifndef NORADIUS 291 if (bundle->radius.sessiontime) 292 bundle_StartSessionTimer(bundle, 0); 293 #endif 294 bundle_StartIdleTimer(bundle, 0); 295 mp_CheckAutoloadTimer(&fp->bundle->ncp.mp); 296 } 297 bundle_Notify(bundle, EX_NORMAL); 298 } else if (fp->proto == PROTO_CCP) 299 bundle_CalculateBandwidth(fp->bundle); /* Against ccp_MTUOverhead */ 300 } 301 302 static void 303 bundle_LayerDown(void *v, struct fsm *fp) 304 { 305 /* 306 * The given FSM has been told to come down. 307 * If it's our last NCP, stop the idle timer. 308 * If it's our last NCP, clear our ``upat'' value. 309 * If it's our last NCP, stop the autoload timer 310 * If it's an LCP, adjust our phys_type.open value and any timers. 311 * If it's an LCP and we're in multilink mode, adjust our tun 312 * If it's the last LCP, down all NCPs 313 * speed and make sure our minimum sequence number is adjusted. 314 */ 315 316 struct bundle *bundle = (struct bundle *)v; 317 318 if (isncp(fp->proto)) { 319 if (ncp_LayersOpen(&fp->bundle->ncp) == 0) { 320 #ifndef NORADIUS 321 if (bundle->radius.sessiontime) 322 bundle_StopSessionTimer(bundle); 323 #endif 324 bundle_StopIdleTimer(bundle); 325 bundle->upat = 0; 326 mp_StopAutoloadTimer(&bundle->ncp.mp); 327 } 328 } else if (fp->proto == PROTO_LCP) { 329 struct datalink *dl; 330 struct datalink *lost; 331 int others_active; 332 333 bundle_LinksRemoved(bundle); /* adjust timers & phys_type values */ 334 335 lost = NULL; 336 others_active = 0; 337 for (dl = bundle->links; dl; dl = dl->next) { 338 if (fp == &dl->physical->link.lcp.fsm) 339 lost = dl; 340 else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) 341 others_active++; 342 } 343 344 if (bundle->ncp.mp.active) { 345 bundle_CalculateBandwidth(bundle); 346 347 if (lost) 348 mp_LinkLost(&bundle->ncp.mp, lost); 349 else 350 log_Printf(LogALERT, "Oops, lost an unrecognised datalink (%s) !\n", 351 fp->link->name); 352 } 353 354 if (!others_active) { 355 /* Down the NCPs. We don't expect to get fsm_Close()d ourself ! */ 356 ncp2initial(&bundle->ncp); 357 mp_Down(&bundle->ncp.mp); 358 } 359 } 360 } 361 362 static void 363 bundle_LayerFinish(void *v, struct fsm *fp) 364 { 365 /* The given fsm is now down (fp cannot be NULL) 366 * 367 * If it's the last NCP, fsm_Close all LCPs 368 * If it's the last NCP, bring any MP layer down 369 */ 370 371 struct bundle *bundle = (struct bundle *)v; 372 struct datalink *dl; 373 374 if (isncp(fp->proto) && !ncp_LayersUnfinished(&bundle->ncp)) { 375 if (bundle_Phase(bundle) != PHASE_DEAD) 376 bundle_NewPhase(bundle, PHASE_TERMINATE); 377 for (dl = bundle->links; dl; dl = dl->next) 378 if (dl->state == DATALINK_OPEN) 379 datalink_Close(dl, CLOSE_STAYDOWN); 380 fsm2initial(fp); 381 mp_Down(&bundle->ncp.mp); 382 } 383 } 384 385 void 386 bundle_Close(struct bundle *bundle, const char *name, int how) 387 { 388 /* 389 * Please close the given datalink. 390 * If name == NULL or name is the last datalink, fsm_Close all NCPs 391 * (except our MP) 392 * If it isn't the last datalink, just Close that datalink. 393 */ 394 395 struct datalink *dl, *this_dl; 396 int others_active; 397 398 others_active = 0; 399 this_dl = NULL; 400 401 for (dl = bundle->links; dl; dl = dl->next) { 402 if (name && !strcasecmp(name, dl->name)) 403 this_dl = dl; 404 if (name == NULL || this_dl == dl) { 405 switch (how) { 406 case CLOSE_LCP: 407 datalink_DontHangup(dl); 408 break; 409 case CLOSE_STAYDOWN: 410 datalink_StayDown(dl); 411 break; 412 } 413 } else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) 414 others_active++; 415 } 416 417 if (name && this_dl == NULL) { 418 log_Printf(LogWARN, "%s: Invalid datalink name\n", name); 419 return; 420 } 421 422 if (!others_active) { 423 #ifndef NORADIUS 424 if (bundle->radius.sessiontime) 425 bundle_StopSessionTimer(bundle); 426 #endif 427 bundle_StopIdleTimer(bundle); 428 if (ncp_LayersUnfinished(&bundle->ncp)) 429 ncp_Close(&bundle->ncp); 430 else { 431 ncp2initial(&bundle->ncp); 432 mp_Down(&bundle->ncp.mp); 433 for (dl = bundle->links; dl; dl = dl->next) 434 datalink_Close(dl, how); 435 } 436 } else if (this_dl && this_dl->state != DATALINK_CLOSED && 437 this_dl->state != DATALINK_HANGUP) 438 datalink_Close(this_dl, how); 439 } 440 441 void 442 bundle_Down(struct bundle *bundle, int how) 443 { 444 struct datalink *dl; 445 446 for (dl = bundle->links; dl; dl = dl->next) 447 datalink_Down(dl, how); 448 } 449 450 static int 451 bundle_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n) 452 { 453 struct bundle *bundle = descriptor2bundle(d); 454 struct datalink *dl; 455 int result, nlinks; 456 u_short ifqueue; 457 size_t queued; 458 459 result = 0; 460 461 /* If there are aren't many packets queued, look for some more. */ 462 for (nlinks = 0, dl = bundle->links; dl; dl = dl->next) 463 nlinks++; 464 465 if (nlinks) { 466 queued = r ? ncp_FillPhysicalQueues(&bundle->ncp, bundle) : 467 ncp_QueueLen(&bundle->ncp); 468 469 if (r && (bundle->phase == PHASE_NETWORK || 470 bundle->phys_type.all & PHYS_AUTO)) { 471 /* enough surplus so that we can tell if we're getting swamped */ 472 ifqueue = nlinks > bundle->cfg.ifqueue ? nlinks : bundle->cfg.ifqueue; 473 if (queued < ifqueue) { 474 /* Not enough - select() for more */ 475 if (bundle->choked.timer.state == TIMER_RUNNING) 476 timer_Stop(&bundle->choked.timer); /* Not needed any more */ 477 FD_SET(bundle->dev.fd, r); 478 if (*n < bundle->dev.fd + 1) 479 *n = bundle->dev.fd + 1; 480 log_Printf(LogTIMER, "%s: fdset(r) %d\n", TUN_NAME, bundle->dev.fd); 481 result++; 482 } else if (bundle->choked.timer.state == TIMER_STOPPED) { 483 bundle->choked.timer.func = bundle_ClearQueues; 484 bundle->choked.timer.name = "output choke"; 485 bundle->choked.timer.load = bundle->cfg.choked.timeout * SECTICKS; 486 bundle->choked.timer.arg = bundle; 487 timer_Start(&bundle->choked.timer); 488 } 489 } 490 } 491 492 #ifndef NORADIUS 493 result += descriptor_UpdateSet(&bundle->radius.desc, r, w, e, n); 494 #endif 495 496 /* Which links need a select() ? */ 497 for (dl = bundle->links; dl; dl = dl->next) 498 result += descriptor_UpdateSet(&dl->desc, r, w, e, n); 499 500 /* 501 * This *MUST* be called after the datalink UpdateSet()s as it 502 * might be ``holding'' one of the datalinks (death-row) and 503 * wants to be able to de-select() it from the descriptor set. 504 */ 505 result += descriptor_UpdateSet(&bundle->ncp.mp.server.desc, r, w, e, n); 506 507 return result; 508 } 509 510 static int 511 bundle_IsSet(struct fdescriptor *d, const fd_set *fdset) 512 { 513 struct bundle *bundle = descriptor2bundle(d); 514 struct datalink *dl; 515 516 for (dl = bundle->links; dl; dl = dl->next) 517 if (descriptor_IsSet(&dl->desc, fdset)) 518 return 1; 519 520 #ifndef NORADIUS 521 if (descriptor_IsSet(&bundle->radius.desc, fdset)) 522 return 1; 523 #endif 524 525 if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 526 return 1; 527 528 return FD_ISSET(bundle->dev.fd, fdset); 529 } 530 531 static void 532 bundle_DescriptorRead(struct fdescriptor *d __unused, struct bundle *bundle, 533 const fd_set *fdset) 534 { 535 struct datalink *dl; 536 unsigned secs; 537 u_int32_t af; 538 539 if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 540 descriptor_Read(&bundle->ncp.mp.server.desc, bundle, fdset); 541 542 for (dl = bundle->links; dl; dl = dl->next) 543 if (descriptor_IsSet(&dl->desc, fdset)) 544 descriptor_Read(&dl->desc, bundle, fdset); 545 546 #ifndef NORADIUS 547 if (descriptor_IsSet(&bundle->radius.desc, fdset)) 548 descriptor_Read(&bundle->radius.desc, bundle, fdset); 549 #endif 550 551 if (FD_ISSET(bundle->dev.fd, fdset)) { 552 struct tun_data tun; 553 int n, pri; 554 u_char *data; 555 size_t sz; 556 557 if (bundle->dev.header) { 558 data = (u_char *)&tun; 559 sz = sizeof tun; 560 } else { 561 data = tun.data; 562 sz = sizeof tun.data; 563 } 564 565 /* something to read from tun */ 566 567 n = read(bundle->dev.fd, data, sz); 568 if (n < 0) { 569 log_Printf(LogWARN, "%s: read: %s\n", bundle->dev.Name, strerror(errno)); 570 return; 571 } 572 573 if (bundle->dev.header) { 574 n -= sz - sizeof tun.data; 575 if (n <= 0) { 576 log_Printf(LogERROR, "%s: read: Got only %d bytes of data !\n", 577 bundle->dev.Name, n); 578 return; 579 } 580 af = ntohl(tun.header.family); 581 #ifndef NOINET6 582 if (af != AF_INET && af != AF_INET6) 583 #else 584 if (af != AF_INET) 585 #endif 586 /* XXX: Should be maintaining drop/family counts ! */ 587 return; 588 } else 589 af = AF_INET; 590 591 if (af == AF_INET && ((struct ip *)tun.data)->ip_dst.s_addr == 592 bundle->ncp.ipcp.my_ip.s_addr) { 593 /* we've been asked to send something addressed *to* us :( */ 594 if (Enabled(bundle, OPT_LOOPBACK)) { 595 pri = PacketCheck(bundle, af, tun.data, n, &bundle->filter.in, 596 NULL, NULL); 597 if (pri >= 0) { 598 n += sz - sizeof tun.data; 599 write(bundle->dev.fd, data, n); 600 log_Printf(LogDEBUG, "Looped back packet addressed to myself\n"); 601 } 602 return; 603 } else 604 log_Printf(LogDEBUG, "Oops - forwarding packet addressed to myself\n"); 605 } 606 607 /* 608 * Process on-demand dialup. Output packets are queued within the tunnel 609 * device until the appropriate NCP is opened. 610 */ 611 612 if (bundle_Phase(bundle) == PHASE_DEAD) { 613 /* 614 * Note, we must be in AUTO mode :-/ otherwise our interface should 615 * *not* be UP and we can't receive data 616 */ 617 pri = PacketCheck(bundle, af, tun.data, n, &bundle->filter.dial, 618 NULL, NULL); 619 if (pri >= 0) 620 bundle_Open(bundle, NULL, PHYS_AUTO, 0); 621 else 622 /* 623 * Drop the packet. If we were to queue it, we'd just end up with 624 * a pile of timed-out data in our output queue by the time we get 625 * around to actually dialing. We'd also prematurely reach the 626 * threshold at which we stop select()ing to read() the tun 627 * device - breaking auto-dial. 628 */ 629 return; 630 } 631 632 secs = 0; 633 pri = PacketCheck(bundle, af, tun.data, n, &bundle->filter.out, 634 NULL, &secs); 635 if (pri >= 0) { 636 /* Prepend the number of seconds timeout given in the filter */ 637 tun.header.timeout = secs; 638 ncp_Enqueue(&bundle->ncp, af, pri, (char *)&tun, n + sizeof tun.header); 639 } 640 } 641 } 642 643 static int 644 bundle_DescriptorWrite(struct fdescriptor *d __unused, struct bundle *bundle, 645 const fd_set *fdset) 646 { 647 struct datalink *dl; 648 int result = 0; 649 650 /* This is not actually necessary as struct mpserver doesn't Write() */ 651 if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 652 if (descriptor_Write(&bundle->ncp.mp.server.desc, bundle, fdset) == 1) 653 result++; 654 655 for (dl = bundle->links; dl; dl = dl->next) 656 if (descriptor_IsSet(&dl->desc, fdset)) 657 switch (descriptor_Write(&dl->desc, bundle, fdset)) { 658 case -1: 659 datalink_ComeDown(dl, CLOSE_NORMAL); 660 break; 661 case 1: 662 result++; 663 } 664 665 return result; 666 } 667 668 void 669 bundle_LockTun(struct bundle *bundle) 670 { 671 FILE *lockfile; 672 char pidfile[PATH_MAX]; 673 674 snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit); 675 lockfile = ID0fopen(pidfile, "w"); 676 if (lockfile != NULL) { 677 fprintf(lockfile, "%d\n", (int)getpid()); 678 fclose(lockfile); 679 } 680 #ifndef RELEASE_CRUNCH 681 else 682 log_Printf(LogERROR, "Warning: Can't create %s: %s\n", 683 pidfile, strerror(errno)); 684 #endif 685 } 686 687 static void 688 bundle_UnlockTun(struct bundle *bundle) 689 { 690 char pidfile[PATH_MAX]; 691 692 snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit); 693 ID0unlink(pidfile); 694 } 695 696 struct bundle * 697 bundle_Create(const char *prefix, int type, int unit) 698 { 699 static struct bundle bundle; /* there can be only one */ 700 int enoentcount, err, minunit, maxunit; 701 const char *ifname; 702 #if defined(__FreeBSD__) && !defined(NOKLDLOAD) 703 int kldtried; 704 #endif 705 #if defined(TUNSIFMODE) || defined(TUNSLMODE) || defined(TUNSIFHEAD) 706 int iff; 707 #endif 708 709 if (bundle.iface != NULL) { /* Already allocated ! */ 710 log_Printf(LogALERT, "bundle_Create: There's only one BUNDLE !\n"); 711 return NULL; 712 } 713 714 if (unit == -1) { 715 minunit = 0; 716 maxunit = -1; 717 } else { 718 minunit = unit; 719 maxunit = unit + 1; 720 } 721 err = ENOENT; 722 enoentcount = 0; 723 #if defined(__FreeBSD__) && !defined(NOKLDLOAD) 724 kldtried = 0; 725 #endif 726 for (bundle.unit = minunit; bundle.unit != maxunit; bundle.unit++) { 727 snprintf(bundle.dev.Name, sizeof bundle.dev.Name, "%s%d", 728 prefix, bundle.unit); 729 bundle.dev.fd = ID0open(bundle.dev.Name, O_RDWR); 730 if (bundle.dev.fd >= 0) 731 break; 732 else if (errno == ENXIO || errno == ENOENT) { 733 #if defined(__FreeBSD__) && !defined(NOKLDLOAD) 734 if (bundle.unit == minunit && !kldtried++) { 735 /* 736 * Attempt to load the tunnel interface KLD if it isn't loaded 737 * already. 738 */ 739 if (loadmodules(LOAD_VERBOSLY, "if_tun", NULL)) 740 bundle.unit--; 741 continue; 742 } 743 #endif 744 if (errno != ENOENT || ++enoentcount > 2) { 745 err = errno; 746 break; 747 } 748 } else 749 err = errno; 750 } 751 752 if (bundle.dev.fd < 0) { 753 if (unit == -1) 754 log_Printf(LogWARN, "No available tunnel devices found (%s)\n", 755 strerror(err)); 756 else 757 log_Printf(LogWARN, "%s%d: %s\n", prefix, unit, strerror(err)); 758 return NULL; 759 } 760 761 log_SetTun(bundle.unit); 762 763 ifname = strrchr(bundle.dev.Name, '/'); 764 if (ifname == NULL) 765 ifname = bundle.dev.Name; 766 else 767 ifname++; 768 769 bundle.iface = iface_Create(ifname); 770 if (bundle.iface == NULL) { 771 close(bundle.dev.fd); 772 return NULL; 773 } 774 775 #ifdef TUNSIFMODE 776 /* Make sure we're POINTOPOINT & IFF_MULTICAST */ 777 iff = IFF_POINTOPOINT | IFF_MULTICAST; 778 if (ID0ioctl(bundle.dev.fd, TUNSIFMODE, &iff) < 0) 779 log_Printf(LogERROR, "bundle_Create: ioctl(TUNSIFMODE): %s\n", 780 strerror(errno)); 781 #endif 782 783 #ifdef TUNSLMODE 784 /* Make sure we're not prepending sockaddrs */ 785 iff = 0; 786 if (ID0ioctl(bundle.dev.fd, TUNSLMODE, &iff) < 0) 787 log_Printf(LogERROR, "bundle_Create: ioctl(TUNSLMODE): %s\n", 788 strerror(errno)); 789 #endif 790 791 #ifdef TUNSIFHEAD 792 /* We want the address family please ! */ 793 iff = 1; 794 if (ID0ioctl(bundle.dev.fd, TUNSIFHEAD, &iff) < 0) { 795 log_Printf(LogERROR, "bundle_Create: ioctl(TUNSIFHEAD): %s\n", 796 strerror(errno)); 797 bundle.dev.header = 0; 798 } else 799 bundle.dev.header = 1; 800 #else 801 #ifdef __OpenBSD__ 802 /* Always present for OpenBSD */ 803 bundle.dev.header = 1; 804 #else 805 /* 806 * If TUNSIFHEAD isn't available and we're not OpenBSD, assume 807 * everything's AF_INET (hopefully the tun device won't pass us 808 * anything else !). 809 */ 810 bundle.dev.header = 0; 811 #endif 812 #endif 813 814 log_Printf(LogPHASE, "Using interface: %s\n", ifname); 815 816 bundle.bandwidth = 0; 817 bundle.routing_seq = 0; 818 bundle.phase = PHASE_DEAD; 819 bundle.CleaningUp = 0; 820 bundle.NatEnabled = 0; 821 822 bundle.fsm.LayerStart = bundle_LayerStart; 823 bundle.fsm.LayerUp = bundle_LayerUp; 824 bundle.fsm.LayerDown = bundle_LayerDown; 825 bundle.fsm.LayerFinish = bundle_LayerFinish; 826 bundle.fsm.object = &bundle; 827 828 bundle.cfg.idle.timeout = NCP_IDLE_TIMEOUT; 829 bundle.cfg.idle.min_timeout = 0; 830 *bundle.cfg.auth.name = '\0'; 831 *bundle.cfg.auth.key = '\0'; 832 bundle.cfg.opt = OPT_IDCHECK | OPT_LOOPBACK | OPT_SROUTES | OPT_TCPMSSFIXUP | 833 OPT_THROUGHPUT | OPT_UTMP; 834 #ifndef NOINET6 835 bundle.cfg.opt |= OPT_IPCP; 836 if (probe.ipv6_available) 837 bundle.cfg.opt |= OPT_IPV6CP; 838 #endif 839 *bundle.cfg.label = '\0'; 840 bundle.cfg.ifqueue = DEF_IFQUEUE; 841 bundle.cfg.choked.timeout = CHOKED_TIMEOUT; 842 bundle.phys_type.all = type; 843 bundle.phys_type.open = 0; 844 bundle.upat = 0; 845 846 bundle.links = datalink_Create("deflink", &bundle, type); 847 if (bundle.links == NULL) { 848 log_Printf(LogALERT, "Cannot create data link: %s\n", strerror(errno)); 849 iface_Destroy(bundle.iface); 850 bundle.iface = NULL; 851 close(bundle.dev.fd); 852 return NULL; 853 } 854 855 bundle.desc.type = BUNDLE_DESCRIPTOR; 856 bundle.desc.UpdateSet = bundle_UpdateSet; 857 bundle.desc.IsSet = bundle_IsSet; 858 bundle.desc.Read = bundle_DescriptorRead; 859 bundle.desc.Write = bundle_DescriptorWrite; 860 861 ncp_Init(&bundle.ncp, &bundle); 862 863 memset(&bundle.filter, '\0', sizeof bundle.filter); 864 bundle.filter.in.fragok = bundle.filter.in.logok = 1; 865 bundle.filter.in.name = "IN"; 866 bundle.filter.out.fragok = bundle.filter.out.logok = 1; 867 bundle.filter.out.name = "OUT"; 868 bundle.filter.dial.name = "DIAL"; 869 bundle.filter.dial.logok = 1; 870 bundle.filter.alive.name = "ALIVE"; 871 bundle.filter.alive.logok = 1; 872 { 873 int i; 874 for (i = 0; i < MAXFILTERS; i++) { 875 bundle.filter.in.rule[i].f_action = A_NONE; 876 bundle.filter.out.rule[i].f_action = A_NONE; 877 bundle.filter.dial.rule[i].f_action = A_NONE; 878 bundle.filter.alive.rule[i].f_action = A_NONE; 879 } 880 } 881 memset(&bundle.idle.timer, '\0', sizeof bundle.idle.timer); 882 bundle.idle.done = 0; 883 bundle.notify.fd = -1; 884 memset(&bundle.choked.timer, '\0', sizeof bundle.choked.timer); 885 #ifndef NORADIUS 886 radius_Init(&bundle.radius); 887 #endif 888 889 /* Clean out any leftover crud */ 890 iface_Clear(bundle.iface, &bundle.ncp, 0, IFACE_CLEAR_ALL); 891 892 bundle_LockTun(&bundle); 893 894 return &bundle; 895 } 896 897 static void 898 bundle_DownInterface(struct bundle *bundle) 899 { 900 route_IfDelete(bundle, 1); 901 iface_ClearFlags(bundle->iface->name, IFF_UP); 902 } 903 904 void 905 bundle_Destroy(struct bundle *bundle) 906 { 907 struct datalink *dl; 908 909 /* 910 * Clean up the interface. We don't really need to do the timer_Stop()s, 911 * mp_Down(), iface_Clear() and bundle_DownInterface() unless we're getting 912 * out under exceptional conditions such as a descriptor exception. 913 */ 914 timer_Stop(&bundle->idle.timer); 915 timer_Stop(&bundle->choked.timer); 916 mp_Down(&bundle->ncp.mp); 917 iface_Clear(bundle->iface, &bundle->ncp, 0, IFACE_CLEAR_ALL); 918 bundle_DownInterface(bundle); 919 920 #ifndef NORADIUS 921 /* Tell the radius server the bad news */ 922 radius_Destroy(&bundle->radius); 923 #endif 924 925 /* Again, these are all DATALINK_CLOSED unless we're abending */ 926 dl = bundle->links; 927 while (dl) 928 dl = datalink_Destroy(dl); 929 930 ncp_Destroy(&bundle->ncp); 931 932 close(bundle->dev.fd); 933 bundle_UnlockTun(bundle); 934 935 /* In case we never made PHASE_NETWORK */ 936 bundle_Notify(bundle, EX_ERRDEAD); 937 938 iface_Destroy(bundle->iface); 939 bundle->iface = NULL; 940 } 941 942 void 943 bundle_LinkClosed(struct bundle *bundle, struct datalink *dl) 944 { 945 /* 946 * Our datalink has closed. 947 * CleanDatalinks() (called from DoLoop()) will remove closed 948 * BACKGROUND, FOREGROUND and DIRECT links. 949 * If it's the last data link, enter phase DEAD. 950 * 951 * NOTE: dl may not be in our list (bundle_SendDatalink()) ! 952 */ 953 954 struct datalink *odl; 955 int other_links; 956 957 log_SetTtyCommandMode(dl); 958 959 other_links = 0; 960 for (odl = bundle->links; odl; odl = odl->next) 961 if (odl != dl && odl->state != DATALINK_CLOSED) 962 other_links++; 963 964 if (!other_links) { 965 if (dl->physical->type != PHYS_AUTO) /* Not in -auto mode */ 966 bundle_DownInterface(bundle); 967 ncp2initial(&bundle->ncp); 968 mp_Down(&bundle->ncp.mp); 969 bundle_NewPhase(bundle, PHASE_DEAD); 970 #ifndef NORADIUS 971 if (bundle->radius.sessiontime) 972 bundle_StopSessionTimer(bundle); 973 #endif 974 bundle_StopIdleTimer(bundle); 975 } 976 } 977 978 void 979 bundle_Open(struct bundle *bundle, const char *name, int mask, int force) 980 { 981 /* 982 * Please open the given datalink, or all if name == NULL 983 */ 984 struct datalink *dl; 985 986 for (dl = bundle->links; dl; dl = dl->next) 987 if (name == NULL || !strcasecmp(dl->name, name)) { 988 if ((mask & dl->physical->type) && 989 (dl->state == DATALINK_CLOSED || 990 (force && dl->state == DATALINK_OPENING && 991 dl->dial.timer.state == TIMER_RUNNING) || 992 dl->state == DATALINK_READY)) { 993 timer_Stop(&dl->dial.timer); /* We're finished with this */ 994 datalink_Up(dl, 1, 1); 995 if (mask & PHYS_AUTO) 996 break; /* Only one AUTO link at a time */ 997 } 998 if (name != NULL) 999 break; 1000 } 1001 } 1002 1003 struct datalink * 1004 bundle2datalink(struct bundle *bundle, const char *name) 1005 { 1006 struct datalink *dl; 1007 1008 if (name != NULL) { 1009 for (dl = bundle->links; dl; dl = dl->next) 1010 if (!strcasecmp(dl->name, name)) 1011 return dl; 1012 } else if (bundle->links && !bundle->links->next) 1013 return bundle->links; 1014 1015 return NULL; 1016 } 1017 1018 int 1019 bundle_ShowLinks(struct cmdargs const *arg) 1020 { 1021 struct datalink *dl; 1022 struct pppThroughput *t; 1023 unsigned long long octets; 1024 int secs; 1025 1026 for (dl = arg->bundle->links; dl; dl = dl->next) { 1027 octets = MAX(dl->physical->link.stats.total.in.OctetsPerSecond, 1028 dl->physical->link.stats.total.out.OctetsPerSecond); 1029 1030 prompt_Printf(arg->prompt, "Name: %s [%s, %s]", 1031 dl->name, mode2Nam(dl->physical->type), datalink_State(dl)); 1032 if (dl->physical->link.stats.total.rolling && dl->state == DATALINK_OPEN) 1033 prompt_Printf(arg->prompt, " bandwidth %d, %llu bps (%llu bytes/sec)", 1034 dl->mp.bandwidth ? dl->mp.bandwidth : 1035 physical_GetSpeed(dl->physical), 1036 octets * 8, octets); 1037 prompt_Printf(arg->prompt, "\n"); 1038 } 1039 1040 t = &arg->bundle->ncp.mp.link.stats.total; 1041 octets = MAX(t->in.OctetsPerSecond, t->out.OctetsPerSecond); 1042 secs = t->downtime ? 0 : throughput_uptime(t); 1043 if (secs > t->SamplePeriod) 1044 secs = t->SamplePeriod; 1045 if (secs) 1046 prompt_Printf(arg->prompt, "Currently averaging %llu bps (%llu bytes/sec)" 1047 " over the last %d secs\n", octets * 8, octets, secs); 1048 1049 return 0; 1050 } 1051 1052 static const char * 1053 optval(struct bundle *bundle, int bit) 1054 { 1055 return (bundle->cfg.opt & bit) ? "enabled" : "disabled"; 1056 } 1057 1058 int 1059 bundle_ShowStatus(struct cmdargs const *arg) 1060 { 1061 int remaining; 1062 1063 prompt_Printf(arg->prompt, "Phase %s\n", bundle_PhaseName(arg->bundle)); 1064 prompt_Printf(arg->prompt, " Device: %s\n", arg->bundle->dev.Name); 1065 prompt_Printf(arg->prompt, " Interface: %s @ %lubps", 1066 arg->bundle->iface->name, arg->bundle->bandwidth); 1067 1068 if (arg->bundle->upat) { 1069 int secs = bundle_Uptime(arg->bundle); 1070 1071 prompt_Printf(arg->prompt, ", up time %d:%02d:%02d", secs / 3600, 1072 (secs / 60) % 60, secs % 60); 1073 } 1074 prompt_Printf(arg->prompt, "\n Queued: %lu of %u\n", 1075 (unsigned long)ncp_QueueLen(&arg->bundle->ncp), 1076 arg->bundle->cfg.ifqueue); 1077 1078 prompt_Printf(arg->prompt, "\nDefaults:\n"); 1079 prompt_Printf(arg->prompt, " Label: %s\n", 1080 arg->bundle->cfg.label); 1081 prompt_Printf(arg->prompt, " Auth name: %s\n", 1082 arg->bundle->cfg.auth.name); 1083 prompt_Printf(arg->prompt, " Diagnostic socket: "); 1084 if (*server.cfg.sockname != '\0') { 1085 prompt_Printf(arg->prompt, "%s", server.cfg.sockname); 1086 if (server.cfg.mask != (mode_t)-1) 1087 prompt_Printf(arg->prompt, ", mask 0%03o", (int)server.cfg.mask); 1088 prompt_Printf(arg->prompt, "%s\n", server.fd == -1 ? " (not open)" : ""); 1089 } else if (server.cfg.port != 0) 1090 prompt_Printf(arg->prompt, "TCP port %d%s\n", server.cfg.port, 1091 server.fd == -1 ? " (not open)" : ""); 1092 else 1093 prompt_Printf(arg->prompt, "none\n"); 1094 1095 prompt_Printf(arg->prompt, " Choked Timer: %us\n", 1096 arg->bundle->cfg.choked.timeout); 1097 1098 #ifndef NORADIUS 1099 radius_Show(&arg->bundle->radius, arg->prompt); 1100 #endif 1101 1102 prompt_Printf(arg->prompt, " Idle Timer: "); 1103 if (arg->bundle->cfg.idle.timeout) { 1104 prompt_Printf(arg->prompt, "%us", arg->bundle->cfg.idle.timeout); 1105 if (arg->bundle->cfg.idle.min_timeout) 1106 prompt_Printf(arg->prompt, ", min %us", 1107 arg->bundle->cfg.idle.min_timeout); 1108 remaining = bundle_RemainingIdleTime(arg->bundle); 1109 if (remaining != -1) 1110 prompt_Printf(arg->prompt, " (%ds remaining)", remaining); 1111 prompt_Printf(arg->prompt, "\n"); 1112 } else 1113 prompt_Printf(arg->prompt, "disabled\n"); 1114 1115 prompt_Printf(arg->prompt, " Filter Decap: %-20.20s", 1116 optval(arg->bundle, OPT_FILTERDECAP)); 1117 prompt_Printf(arg->prompt, " ID check: %s\n", 1118 optval(arg->bundle, OPT_IDCHECK)); 1119 prompt_Printf(arg->prompt, " Iface-Alias: %-20.20s", 1120 optval(arg->bundle, OPT_IFACEALIAS)); 1121 #ifndef NOINET6 1122 prompt_Printf(arg->prompt, " IPCP: %s\n", 1123 optval(arg->bundle, OPT_IPCP)); 1124 prompt_Printf(arg->prompt, " IPV6CP: %-20.20s", 1125 optval(arg->bundle, OPT_IPV6CP)); 1126 #endif 1127 prompt_Printf(arg->prompt, " Keep-Session: %s\n", 1128 optval(arg->bundle, OPT_KEEPSESSION)); 1129 prompt_Printf(arg->prompt, " Loopback: %-20.20s", 1130 optval(arg->bundle, OPT_LOOPBACK)); 1131 prompt_Printf(arg->prompt, " PasswdAuth: %s\n", 1132 optval(arg->bundle, OPT_PASSWDAUTH)); 1133 prompt_Printf(arg->prompt, " Proxy: %-20.20s", 1134 optval(arg->bundle, OPT_PROXY)); 1135 prompt_Printf(arg->prompt, " Proxyall: %s\n", 1136 optval(arg->bundle, OPT_PROXYALL)); 1137 prompt_Printf(arg->prompt, " Sticky Routes: %-20.20s", 1138 optval(arg->bundle, OPT_SROUTES)); 1139 prompt_Printf(arg->prompt, " TCPMSS Fixup: %s\n", 1140 optval(arg->bundle, OPT_TCPMSSFIXUP)); 1141 prompt_Printf(arg->prompt, " Throughput: %-20.20s", 1142 optval(arg->bundle, OPT_THROUGHPUT)); 1143 prompt_Printf(arg->prompt, " Utmp Logging: %s\n", 1144 optval(arg->bundle, OPT_UTMP)); 1145 1146 return 0; 1147 } 1148 1149 static void 1150 bundle_IdleTimeout(void *v) 1151 { 1152 struct bundle *bundle = (struct bundle *)v; 1153 1154 log_Printf(LogPHASE, "Idle timer expired\n"); 1155 bundle_StopIdleTimer(bundle); 1156 bundle_Close(bundle, NULL, CLOSE_STAYDOWN); 1157 } 1158 1159 /* 1160 * Start Idle timer. If timeout is reached, we call bundle_Close() to 1161 * close LCP and link. 1162 */ 1163 void 1164 bundle_StartIdleTimer(struct bundle *bundle, unsigned secs) 1165 { 1166 timer_Stop(&bundle->idle.timer); 1167 if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) != 1168 bundle->phys_type.open && bundle->cfg.idle.timeout) { 1169 time_t now = time(NULL); 1170 1171 if (secs == 0) 1172 secs = bundle->cfg.idle.timeout; 1173 1174 /* We want at least `secs' */ 1175 if (bundle->cfg.idle.min_timeout > secs && bundle->upat) { 1176 unsigned up = now - bundle->upat; 1177 1178 if (bundle->cfg.idle.min_timeout > up && 1179 bundle->cfg.idle.min_timeout - up > (long long)secs) 1180 /* Only increase from the current `remaining' value */ 1181 secs = bundle->cfg.idle.min_timeout - up; 1182 } 1183 bundle->idle.timer.func = bundle_IdleTimeout; 1184 bundle->idle.timer.name = "idle"; 1185 bundle->idle.timer.load = secs * SECTICKS; 1186 bundle->idle.timer.arg = bundle; 1187 timer_Start(&bundle->idle.timer); 1188 bundle->idle.done = now + secs; 1189 } 1190 } 1191 1192 void 1193 bundle_SetIdleTimer(struct bundle *bundle, unsigned timeout, 1194 unsigned min_timeout) 1195 { 1196 bundle->cfg.idle.timeout = timeout; 1197 bundle->cfg.idle.min_timeout = min_timeout; 1198 if (ncp_LayersOpen(&bundle->ncp)) 1199 bundle_StartIdleTimer(bundle, 0); 1200 } 1201 1202 void 1203 bundle_StopIdleTimer(struct bundle *bundle) 1204 { 1205 timer_Stop(&bundle->idle.timer); 1206 bundle->idle.done = 0; 1207 } 1208 1209 static int 1210 bundle_RemainingIdleTime(struct bundle *bundle) 1211 { 1212 if (bundle->idle.done) 1213 return bundle->idle.done - time(NULL); 1214 return -1; 1215 } 1216 1217 #ifndef NORADIUS 1218 1219 static void 1220 bundle_SessionTimeout(void *v) 1221 { 1222 struct bundle *bundle = (struct bundle *)v; 1223 1224 log_Printf(LogPHASE, "Session-Timeout timer expired\n"); 1225 bundle_StopSessionTimer(bundle); 1226 bundle_Close(bundle, NULL, CLOSE_STAYDOWN); 1227 } 1228 1229 void 1230 bundle_StartSessionTimer(struct bundle *bundle, unsigned secs) 1231 { 1232 timer_Stop(&bundle->session.timer); 1233 if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) != 1234 bundle->phys_type.open && bundle->radius.sessiontime) { 1235 time_t now = time(NULL); 1236 1237 if (secs == 0) 1238 secs = bundle->radius.sessiontime; 1239 1240 bundle->session.timer.func = bundle_SessionTimeout; 1241 bundle->session.timer.name = "session"; 1242 bundle->session.timer.load = secs * SECTICKS; 1243 bundle->session.timer.arg = bundle; 1244 timer_Start(&bundle->session.timer); 1245 bundle->session.done = now + secs; 1246 } 1247 } 1248 1249 void 1250 bundle_StopSessionTimer(struct bundle *bundle) 1251 { 1252 timer_Stop(&bundle->session.timer); 1253 bundle->session.done = 0; 1254 } 1255 1256 #endif 1257 1258 int 1259 bundle_IsDead(struct bundle *bundle) 1260 { 1261 return !bundle->links || (bundle->phase == PHASE_DEAD && bundle->CleaningUp); 1262 } 1263 1264 static struct datalink * 1265 bundle_DatalinkLinkout(struct bundle *bundle, struct datalink *dl) 1266 { 1267 struct datalink **dlp; 1268 1269 for (dlp = &bundle->links; *dlp; dlp = &(*dlp)->next) 1270 if (*dlp == dl) { 1271 *dlp = dl->next; 1272 dl->next = NULL; 1273 bundle_LinksRemoved(bundle); 1274 return dl; 1275 } 1276 1277 return NULL; 1278 } 1279 1280 static void 1281 bundle_DatalinkLinkin(struct bundle *bundle, struct datalink *dl) 1282 { 1283 struct datalink **dlp = &bundle->links; 1284 1285 while (*dlp) 1286 dlp = &(*dlp)->next; 1287 1288 *dlp = dl; 1289 dl->next = NULL; 1290 1291 bundle_LinkAdded(bundle, dl); 1292 mp_CheckAutoloadTimer(&bundle->ncp.mp); 1293 } 1294 1295 void 1296 bundle_CleanDatalinks(struct bundle *bundle) 1297 { 1298 struct datalink **dlp = &bundle->links; 1299 int found = 0; 1300 1301 while (*dlp) 1302 if ((*dlp)->state == DATALINK_CLOSED && 1303 (*dlp)->physical->type & 1304 (PHYS_DIRECT|PHYS_BACKGROUND|PHYS_FOREGROUND)) { 1305 *dlp = datalink_Destroy(*dlp); 1306 found++; 1307 } else 1308 dlp = &(*dlp)->next; 1309 1310 if (found) 1311 bundle_LinksRemoved(bundle); 1312 } 1313 1314 int 1315 bundle_DatalinkClone(struct bundle *bundle, struct datalink *dl, 1316 const char *name) 1317 { 1318 if (bundle2datalink(bundle, name)) { 1319 log_Printf(LogWARN, "Clone: %s: name already exists\n", name); 1320 return 0; 1321 } 1322 1323 bundle_DatalinkLinkin(bundle, datalink_Clone(dl, name)); 1324 return 1; 1325 } 1326 1327 void 1328 bundle_DatalinkRemove(struct bundle *bundle, struct datalink *dl) 1329 { 1330 dl = bundle_DatalinkLinkout(bundle, dl); 1331 if (dl) 1332 datalink_Destroy(dl); 1333 } 1334 1335 void 1336 bundle_SetLabel(struct bundle *bundle, const char *label) 1337 { 1338 if (label) 1339 strncpy(bundle->cfg.label, label, sizeof bundle->cfg.label - 1); 1340 else 1341 *bundle->cfg.label = '\0'; 1342 } 1343 1344 const char * 1345 bundle_GetLabel(struct bundle *bundle) 1346 { 1347 return *bundle->cfg.label ? bundle->cfg.label : NULL; 1348 } 1349 1350 int 1351 bundle_LinkSize() 1352 { 1353 struct iovec iov[SCATTER_SEGMENTS]; 1354 int niov, expect, f; 1355 1356 iov[0].iov_len = strlen(Version) + 1; 1357 iov[0].iov_base = NULL; 1358 niov = 1; 1359 if (datalink2iov(NULL, iov, &niov, SCATTER_SEGMENTS, NULL, NULL) == -1) { 1360 log_Printf(LogERROR, "Cannot determine space required for link\n"); 1361 return 0; 1362 } 1363 1364 for (f = expect = 0; f < niov; f++) 1365 expect += iov[f].iov_len; 1366 1367 return expect; 1368 } 1369 1370 void 1371 bundle_ReceiveDatalink(struct bundle *bundle, int s) 1372 { 1373 char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int) * SEND_MAXFD]; 1374 int niov, expect, f, *fd, nfd, onfd; 1375 ssize_t got; 1376 struct iovec iov[SCATTER_SEGMENTS]; 1377 struct cmsghdr *cmsg; 1378 struct msghdr msg; 1379 struct datalink *dl; 1380 pid_t pid; 1381 1382 log_Printf(LogPHASE, "Receiving datalink\n"); 1383 1384 /* 1385 * Create our scatter/gather array - passing NULL gets the space 1386 * allocation requirement rather than actually flattening the 1387 * structures. 1388 */ 1389 iov[0].iov_len = strlen(Version) + 1; 1390 iov[0].iov_base = NULL; 1391 niov = 1; 1392 if (datalink2iov(NULL, iov, &niov, SCATTER_SEGMENTS, NULL, NULL) == -1) { 1393 log_Printf(LogERROR, "Cannot determine space required for link\n"); 1394 return; 1395 } 1396 1397 /* Allocate the scatter/gather array for recvmsg() */ 1398 for (f = expect = 0; f < niov; f++) { 1399 if ((iov[f].iov_base = malloc(iov[f].iov_len)) == NULL) { 1400 log_Printf(LogERROR, "Cannot allocate space to receive link\n"); 1401 return; 1402 } 1403 if (f) 1404 expect += iov[f].iov_len; 1405 } 1406 1407 /* Set up our message */ 1408 cmsg = (struct cmsghdr *)cmsgbuf; 1409 cmsg->cmsg_len = sizeof cmsgbuf; 1410 cmsg->cmsg_level = SOL_SOCKET; 1411 cmsg->cmsg_type = 0; 1412 1413 memset(&msg, '\0', sizeof msg); 1414 msg.msg_name = NULL; 1415 msg.msg_namelen = 0; 1416 msg.msg_iov = iov; 1417 msg.msg_iovlen = 1; /* Only send the version at the first pass */ 1418 msg.msg_control = cmsgbuf; 1419 msg.msg_controllen = sizeof cmsgbuf; 1420 1421 log_Printf(LogDEBUG, "Expecting %u scatter/gather bytes\n", 1422 (unsigned)iov[0].iov_len); 1423 1424 if ((got = recvmsg(s, &msg, MSG_WAITALL)) != (ssize_t)iov[0].iov_len) { 1425 if (got == -1) 1426 log_Printf(LogERROR, "Failed recvmsg: %s\n", strerror(errno)); 1427 else 1428 log_Printf(LogERROR, "Failed recvmsg: Got %zd, not %u\n", 1429 got, (unsigned)iov[0].iov_len); 1430 while (niov--) 1431 free(iov[niov].iov_base); 1432 return; 1433 } 1434 1435 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) { 1436 log_Printf(LogERROR, "Recvmsg: no descriptors received !\n"); 1437 while (niov--) 1438 free(iov[niov].iov_base); 1439 return; 1440 } 1441 1442 fd = (int *)CMSG_DATA(cmsg); 1443 nfd = ((caddr_t)cmsg + cmsg->cmsg_len - (caddr_t)fd) / sizeof(int); 1444 1445 if (nfd < 2) { 1446 log_Printf(LogERROR, "Recvmsg: %d descriptor%s received (too few) !\n", 1447 nfd, nfd == 1 ? "" : "s"); 1448 while (nfd--) 1449 close(fd[nfd]); 1450 while (niov--) 1451 free(iov[niov].iov_base); 1452 return; 1453 } 1454 1455 /* 1456 * We've successfully received two or more open file descriptors 1457 * through our socket, plus a version string. Make sure it's the 1458 * correct version, and drop the connection if it's not. 1459 */ 1460 if (strncmp(Version, iov[0].iov_base, iov[0].iov_len)) { 1461 log_Printf(LogWARN, "Cannot receive datalink, incorrect version" 1462 " (\"%.*s\", not \"%s\")\n", (int)iov[0].iov_len, 1463 (char *)iov[0].iov_base, Version); 1464 while (nfd--) 1465 close(fd[nfd]); 1466 while (niov--) 1467 free(iov[niov].iov_base); 1468 return; 1469 } 1470 1471 /* 1472 * Everything looks good. Send the other side our process id so that 1473 * they can transfer lock ownership, and wait for them to send the 1474 * actual link data. 1475 */ 1476 pid = getpid(); 1477 if ((got = write(fd[1], &pid, sizeof pid)) != sizeof pid) { 1478 if (got == -1) 1479 log_Printf(LogERROR, "Failed write: %s\n", strerror(errno)); 1480 else 1481 log_Printf(LogERROR, "Failed write: Got %zd, not %d\n", got, 1482 (int)(sizeof pid)); 1483 while (nfd--) 1484 close(fd[nfd]); 1485 while (niov--) 1486 free(iov[niov].iov_base); 1487 return; 1488 } 1489 1490 if ((got = readv(fd[1], iov + 1, niov - 1)) != expect) { 1491 if (got == -1) 1492 log_Printf(LogERROR, "Failed write: %s\n", strerror(errno)); 1493 else 1494 log_Printf(LogERROR, "Failed write: Got %zd, not %d\n", got, expect); 1495 while (nfd--) 1496 close(fd[nfd]); 1497 while (niov--) 1498 free(iov[niov].iov_base); 1499 return; 1500 } 1501 close(fd[1]); 1502 1503 onfd = nfd; /* We've got this many in our array */ 1504 nfd -= 2; /* Don't include p->fd and our reply descriptor */ 1505 niov = 1; /* Skip the version id */ 1506 dl = iov2datalink(bundle, iov, &niov, sizeof iov / sizeof *iov, fd[0], 1507 fd + 2, &nfd); 1508 if (dl) { 1509 1510 if (nfd) { 1511 log_Printf(LogERROR, "bundle_ReceiveDatalink: Failed to handle %d " 1512 "auxiliary file descriptors (%d remain)\n", onfd, nfd); 1513 datalink_Destroy(dl); 1514 while (nfd--) 1515 close(fd[onfd--]); 1516 close(fd[0]); 1517 } else { 1518 bundle_DatalinkLinkin(bundle, dl); 1519 datalink_AuthOk(dl); 1520 bundle_CalculateBandwidth(dl->bundle); 1521 } 1522 } else { 1523 while (nfd--) 1524 close(fd[onfd--]); 1525 close(fd[0]); 1526 close(fd[1]); 1527 } 1528 1529 free(iov[0].iov_base); 1530 } 1531 1532 void 1533 bundle_SendDatalink(struct datalink *dl, int s, struct sockaddr_un *sun) 1534 { 1535 char cmsgbuf[CMSG_SPACE(sizeof(int) * SEND_MAXFD)]; 1536 const char *constlock; 1537 char *lock; 1538 struct cmsghdr *cmsg; 1539 struct msghdr msg; 1540 struct iovec iov[SCATTER_SEGMENTS]; 1541 int niov, f, expect, newsid, fd[SEND_MAXFD], nfd, reply[2]; 1542 ssize_t got; 1543 pid_t newpid; 1544 1545 log_Printf(LogPHASE, "Transmitting datalink %s\n", dl->name); 1546 1547 /* Record the base device name for a lock transfer later */ 1548 constlock = physical_LockedDevice(dl->physical); 1549 if (constlock) { 1550 lock = alloca(strlen(constlock) + 1); 1551 strcpy(lock, constlock); 1552 } else 1553 lock = NULL; 1554 1555 bundle_LinkClosed(dl->bundle, dl); 1556 bundle_DatalinkLinkout(dl->bundle, dl); 1557 1558 /* Build our scatter/gather array */ 1559 iov[0].iov_len = strlen(Version) + 1; 1560 iov[0].iov_base = strdup(Version); 1561 niov = 1; 1562 nfd = 0; 1563 1564 fd[0] = datalink2iov(dl, iov, &niov, SCATTER_SEGMENTS, fd + 2, &nfd); 1565 1566 if (fd[0] != -1 && socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, reply) != -1) { 1567 /* 1568 * fd[1] is used to get the peer process id back, then to confirm that 1569 * we've transferred any device locks to that process id. 1570 */ 1571 fd[1] = reply[1]; 1572 1573 nfd += 2; /* Include fd[0] and fd[1] */ 1574 memset(&msg, '\0', sizeof msg); 1575 1576 msg.msg_name = NULL; 1577 msg.msg_namelen = 0; 1578 /* 1579 * Only send the version to start... We used to send the whole lot, but 1580 * this caused problems with our RECVBUF size as a single link is about 1581 * 22k ! This way, we should bump into no limits. 1582 */ 1583 msg.msg_iovlen = 1; 1584 msg.msg_iov = iov; 1585 msg.msg_control = cmsgbuf; 1586 msg.msg_controllen = CMSG_SPACE(sizeof(int) * nfd); 1587 msg.msg_flags = 0; 1588 1589 cmsg = (struct cmsghdr *)cmsgbuf; 1590 cmsg->cmsg_len = msg.msg_controllen; 1591 cmsg->cmsg_level = SOL_SOCKET; 1592 cmsg->cmsg_type = SCM_RIGHTS; 1593 1594 for (f = 0; f < nfd; f++) 1595 *((int *)CMSG_DATA(cmsg) + f) = fd[f]; 1596 1597 for (f = 1, expect = 0; f < niov; f++) 1598 expect += iov[f].iov_len; 1599 1600 if (setsockopt(reply[0], SOL_SOCKET, SO_SNDBUF, &expect, sizeof(int)) == -1) 1601 log_Printf(LogERROR, "setsockopt(SO_RCVBUF, %d): %s\n", expect, 1602 strerror(errno)); 1603 if (setsockopt(reply[1], SOL_SOCKET, SO_RCVBUF, &expect, sizeof(int)) == -1) 1604 log_Printf(LogERROR, "setsockopt(SO_RCVBUF, %d): %s\n", expect, 1605 strerror(errno)); 1606 1607 log_Printf(LogDEBUG, "Sending %d descriptor%s and %u bytes in scatter" 1608 "/gather array\n", nfd, nfd == 1 ? "" : "s", 1609 (unsigned)iov[0].iov_len); 1610 1611 if ((got = sendmsg(s, &msg, 0)) == -1) 1612 log_Printf(LogERROR, "Failed sendmsg: %s: %s\n", 1613 sun->sun_path, strerror(errno)); 1614 else if (got != (ssize_t)iov[0].iov_len) 1615 log_Printf(LogERROR, "%s: Failed initial sendmsg: Only sent %zd of %u\n", 1616 sun->sun_path, got, (unsigned)iov[0].iov_len); 1617 else { 1618 /* We must get the ACK before closing the descriptor ! */ 1619 int res; 1620 1621 if ((got = read(reply[0], &newpid, sizeof newpid)) == sizeof newpid) { 1622 log_Printf(LogDEBUG, "Received confirmation from pid %ld\n", 1623 (long)newpid); 1624 if (lock && (res = ID0uu_lock_txfr(lock, newpid)) != UU_LOCK_OK) 1625 log_Printf(LogERROR, "uu_lock_txfr: %s\n", uu_lockerr(res)); 1626 1627 log_Printf(LogDEBUG, "Transmitting link (%d bytes)\n", expect); 1628 if ((got = writev(reply[0], iov + 1, niov - 1)) != expect) { 1629 if (got == -1) 1630 log_Printf(LogERROR, "%s: Failed writev: %s\n", 1631 sun->sun_path, strerror(errno)); 1632 else 1633 log_Printf(LogERROR, "%s: Failed writev: Wrote %zd of %d\n", 1634 sun->sun_path, got, expect); 1635 } 1636 } else if (got == -1) 1637 log_Printf(LogERROR, "%s: Failed socketpair read: %s\n", 1638 sun->sun_path, strerror(errno)); 1639 else 1640 log_Printf(LogERROR, "%s: Failed socketpair read: Got %zd of %d\n", 1641 sun->sun_path, got, (int)(sizeof newpid)); 1642 } 1643 1644 close(reply[0]); 1645 close(reply[1]); 1646 1647 newsid = Enabled(dl->bundle, OPT_KEEPSESSION) || 1648 tcgetpgrp(fd[0]) == getpgrp(); 1649 while (nfd) 1650 close(fd[--nfd]); 1651 if (newsid) 1652 bundle_setsid(dl->bundle, got != -1); 1653 } 1654 close(s); 1655 1656 while (niov--) 1657 free(iov[niov].iov_base); 1658 } 1659 1660 int 1661 bundle_RenameDatalink(struct bundle *bundle, struct datalink *ndl, 1662 const char *name) 1663 { 1664 struct datalink *dl; 1665 1666 if (!strcasecmp(ndl->name, name)) 1667 return 1; 1668 1669 for (dl = bundle->links; dl; dl = dl->next) 1670 if (!strcasecmp(dl->name, name)) 1671 return 0; 1672 1673 datalink_Rename(ndl, name); 1674 return 1; 1675 } 1676 1677 int 1678 bundle_SetMode(struct bundle *bundle, struct datalink *dl, int mode) 1679 { 1680 int omode; 1681 1682 omode = dl->physical->type; 1683 if (omode == mode) 1684 return 1; 1685 1686 if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO)) 1687 /* First auto link */ 1688 if (bundle->ncp.ipcp.peer_ip.s_addr == INADDR_ANY) { 1689 log_Printf(LogWARN, "You must `set ifaddr' or `open' before" 1690 " changing mode to %s\n", mode2Nam(mode)); 1691 return 0; 1692 } 1693 1694 if (!datalink_SetMode(dl, mode)) 1695 return 0; 1696 1697 if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO) && 1698 bundle->phase != PHASE_NETWORK) 1699 /* First auto link, we need an interface */ 1700 ipcp_InterfaceUp(&bundle->ncp.ipcp); 1701 1702 /* Regenerate phys_type and adjust idle timer */ 1703 bundle_LinksRemoved(bundle); 1704 1705 return 1; 1706 } 1707 1708 void 1709 bundle_setsid(struct bundle *bundle, int holdsession) 1710 { 1711 /* 1712 * Lose the current session. This means getting rid of our pid 1713 * too so that the tty device will really go away, and any getty 1714 * etc will be allowed to restart. 1715 */ 1716 pid_t pid, orig; 1717 int fds[2]; 1718 char done; 1719 struct datalink *dl; 1720 1721 if (!holdsession && bundle_IsDead(bundle)) { 1722 /* 1723 * No need to lose our session after all... we're going away anyway 1724 * 1725 * We should really stop the timer and pause if holdsession is set and 1726 * the bundle's dead, but that leaves other resources lying about :-( 1727 */ 1728 return; 1729 } 1730 1731 orig = getpid(); 1732 if (pipe(fds) == -1) { 1733 log_Printf(LogERROR, "pipe: %s\n", strerror(errno)); 1734 return; 1735 } 1736 switch ((pid = fork())) { 1737 case -1: 1738 log_Printf(LogERROR, "fork: %s\n", strerror(errno)); 1739 close(fds[0]); 1740 close(fds[1]); 1741 return; 1742 case 0: 1743 close(fds[1]); 1744 read(fds[0], &done, 1); /* uu_locks are mine ! */ 1745 close(fds[0]); 1746 if (pipe(fds) == -1) { 1747 log_Printf(LogERROR, "pipe(2): %s\n", strerror(errno)); 1748 return; 1749 } 1750 switch ((pid = fork())) { 1751 case -1: 1752 log_Printf(LogERROR, "fork(2): %s\n", strerror(errno)); 1753 close(fds[0]); 1754 close(fds[1]); 1755 return; 1756 case 0: 1757 close(fds[1]); 1758 bundle_LockTun(bundle); /* update pid */ 1759 read(fds[0], &done, 1); /* uu_locks are mine ! */ 1760 close(fds[0]); 1761 setsid(); 1762 bundle_ChangedPID(bundle); 1763 log_Printf(LogDEBUG, "%ld -> %ld: %s session control\n", 1764 (long)orig, (long)getpid(), 1765 holdsession ? "Passed" : "Dropped"); 1766 timer_InitService(0); /* Start the Timer Service */ 1767 break; 1768 default: 1769 close(fds[0]); 1770 /* Give away all our physical locks (to the final process) */ 1771 for (dl = bundle->links; dl; dl = dl->next) 1772 if (dl->state != DATALINK_CLOSED) 1773 physical_ChangedPid(dl->physical, pid); 1774 write(fds[1], "!", 1); /* done */ 1775 close(fds[1]); 1776 _exit(0); 1777 break; 1778 } 1779 break; 1780 default: 1781 close(fds[0]); 1782 /* Give away all our physical locks (to the intermediate process) */ 1783 for (dl = bundle->links; dl; dl = dl->next) 1784 if (dl->state != DATALINK_CLOSED) 1785 physical_ChangedPid(dl->physical, pid); 1786 write(fds[1], "!", 1); /* done */ 1787 close(fds[1]); 1788 if (holdsession) { 1789 int fd, status; 1790 1791 timer_TermService(); 1792 signal(SIGPIPE, SIG_DFL); 1793 signal(SIGALRM, SIG_DFL); 1794 signal(SIGHUP, SIG_DFL); 1795 signal(SIGTERM, SIG_DFL); 1796 signal(SIGINT, SIG_DFL); 1797 signal(SIGQUIT, SIG_DFL); 1798 for (fd = getdtablesize(); fd >= 0; fd--) 1799 close(fd); 1800 /* 1801 * Reap the intermediate process. As we're not exiting but the 1802 * intermediate is, we don't want it to become defunct. 1803 */ 1804 waitpid(pid, &status, 0); 1805 /* Tweak our process arguments.... */ 1806 SetTitle("session owner"); 1807 #ifndef NOSUID 1808 setuid(ID0realuid()); 1809 #endif 1810 /* 1811 * Hang around for a HUP. This should happen as soon as the 1812 * ppp that we passed our ctty descriptor to closes it. 1813 * NOTE: If this process dies, the passed descriptor becomes 1814 * invalid and will give a select() error by setting one 1815 * of the error fds, aborting the other ppp. We don't 1816 * want that to happen ! 1817 */ 1818 pause(); 1819 } 1820 _exit(0); 1821 break; 1822 } 1823 } 1824 1825 unsigned 1826 bundle_HighestState(struct bundle *bundle) 1827 { 1828 struct datalink *dl; 1829 unsigned result = DATALINK_CLOSED; 1830 1831 for (dl = bundle->links; dl; dl = dl->next) 1832 if (result < dl->state) 1833 result = dl->state; 1834 1835 return result; 1836 } 1837 1838 int 1839 bundle_Exception(struct bundle *bundle, int fd) 1840 { 1841 struct datalink *dl; 1842 1843 for (dl = bundle->links; dl; dl = dl->next) 1844 if (dl->physical->fd == fd) { 1845 datalink_Down(dl, CLOSE_NORMAL); 1846 return 1; 1847 } 1848 1849 return 0; 1850 } 1851 1852 void 1853 bundle_AdjustFilters(struct bundle *bundle, struct ncpaddr *local, 1854 struct ncpaddr *remote) 1855 { 1856 filter_AdjustAddr(&bundle->filter.in, local, remote, NULL); 1857 filter_AdjustAddr(&bundle->filter.out, local, remote, NULL); 1858 filter_AdjustAddr(&bundle->filter.dial, local, remote, NULL); 1859 filter_AdjustAddr(&bundle->filter.alive, local, remote, NULL); 1860 } 1861 1862 void 1863 bundle_AdjustDNS(struct bundle *bundle) 1864 { 1865 struct in_addr *dns = bundle->ncp.ipcp.ns.dns; 1866 1867 filter_AdjustAddr(&bundle->filter.in, NULL, NULL, dns); 1868 filter_AdjustAddr(&bundle->filter.out, NULL, NULL, dns); 1869 filter_AdjustAddr(&bundle->filter.dial, NULL, NULL, dns); 1870 filter_AdjustAddr(&bundle->filter.alive, NULL, NULL, dns); 1871 } 1872 1873 void 1874 bundle_CalculateBandwidth(struct bundle *bundle) 1875 { 1876 struct datalink *dl; 1877 int sp, overhead, maxoverhead; 1878 1879 bundle->bandwidth = 0; 1880 bundle->iface->mtu = 0; 1881 maxoverhead = 0; 1882 1883 for (dl = bundle->links; dl; dl = dl->next) { 1884 overhead = ccp_MTUOverhead(&dl->physical->link.ccp); 1885 if (maxoverhead < overhead) 1886 maxoverhead = overhead; 1887 if (dl->state == DATALINK_OPEN) { 1888 if ((sp = dl->mp.bandwidth) == 0 && 1889 (sp = physical_GetSpeed(dl->physical)) == 0) 1890 log_Printf(LogDEBUG, "%s: %s: Cannot determine bandwidth\n", 1891 dl->name, dl->physical->name.full); 1892 else 1893 bundle->bandwidth += sp; 1894 if (!bundle->ncp.mp.active) { 1895 bundle->iface->mtu = dl->physical->link.lcp.his_mru; 1896 break; 1897 } 1898 } 1899 } 1900 1901 if (bundle->bandwidth == 0) 1902 bundle->bandwidth = 115200; /* Shrug */ 1903 1904 if (bundle->ncp.mp.active) { 1905 bundle->iface->mtu = bundle->ncp.mp.peer_mrru; 1906 overhead = ccp_MTUOverhead(&bundle->ncp.mp.link.ccp); 1907 if (maxoverhead < overhead) 1908 maxoverhead = overhead; 1909 } else if (!bundle->iface->mtu) 1910 bundle->iface->mtu = DEF_MRU; 1911 1912 #ifndef NORADIUS 1913 if (bundle->radius.valid && bundle->radius.mtu && 1914 bundle->radius.mtu < bundle->iface->mtu) { 1915 log_Printf(LogLCP, "Reducing MTU to radius value %lu\n", 1916 bundle->radius.mtu); 1917 bundle->iface->mtu = bundle->radius.mtu; 1918 } 1919 #endif 1920 1921 if (maxoverhead) { 1922 log_Printf(LogLCP, "Reducing MTU from %lu to %lu (CCP requirement)\n", 1923 bundle->iface->mtu, bundle->iface->mtu - maxoverhead); 1924 bundle->iface->mtu -= maxoverhead; 1925 } 1926 1927 tun_configure(bundle); 1928 1929 route_UpdateMTU(bundle); 1930 } 1931 1932 void 1933 bundle_AutoAdjust(struct bundle *bundle, int percent, int what) 1934 { 1935 struct datalink *dl, *choice, *otherlinkup; 1936 1937 choice = otherlinkup = NULL; 1938 for (dl = bundle->links; dl; dl = dl->next) 1939 if (dl->physical->type == PHYS_AUTO) { 1940 if (dl->state == DATALINK_OPEN) { 1941 if (what == AUTO_DOWN) { 1942 if (choice) 1943 otherlinkup = choice; 1944 choice = dl; 1945 } 1946 } else if (dl->state == DATALINK_CLOSED) { 1947 if (what == AUTO_UP) { 1948 choice = dl; 1949 break; 1950 } 1951 } else { 1952 /* An auto link in an intermediate state - forget it for the moment */ 1953 choice = NULL; 1954 break; 1955 } 1956 } else if (dl->state == DATALINK_OPEN && what == AUTO_DOWN) 1957 otherlinkup = dl; 1958 1959 if (choice) { 1960 if (what == AUTO_UP) { 1961 log_Printf(LogPHASE, "%d%% saturation -> Opening link ``%s''\n", 1962 percent, choice->name); 1963 datalink_Up(choice, 1, 1); 1964 mp_CheckAutoloadTimer(&bundle->ncp.mp); 1965 } else if (otherlinkup) { /* Only bring the second-last link down */ 1966 log_Printf(LogPHASE, "%d%% saturation -> Closing link ``%s''\n", 1967 percent, choice->name); 1968 datalink_Close(choice, CLOSE_STAYDOWN); 1969 mp_CheckAutoloadTimer(&bundle->ncp.mp); 1970 } 1971 } 1972 } 1973 1974 int 1975 bundle_WantAutoloadTimer(struct bundle *bundle) 1976 { 1977 struct datalink *dl; 1978 int autolink, opened; 1979 1980 if (bundle->phase == PHASE_NETWORK) { 1981 for (autolink = opened = 0, dl = bundle->links; dl; dl = dl->next) 1982 if (dl->physical->type == PHYS_AUTO) { 1983 if (++autolink == 2 || (autolink == 1 && opened)) 1984 /* Two auto links or one auto and one open in NETWORK phase */ 1985 return 1; 1986 } else if (dl->state == DATALINK_OPEN) { 1987 opened++; 1988 if (autolink) 1989 /* One auto and one open link in NETWORK phase */ 1990 return 1; 1991 } 1992 } 1993 1994 return 0; 1995 } 1996 1997 void 1998 bundle_ChangedPID(struct bundle *bundle) 1999 { 2000 #ifdef TUNSIFPID 2001 ioctl(bundle->dev.fd, TUNSIFPID, 0); 2002 #endif 2003 } 2004 2005 int 2006 bundle_Uptime(struct bundle *bundle) 2007 { 2008 if (bundle->upat) 2009 return time(NULL) - bundle->upat; 2010 2011 return 0; 2012 } 2013