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