bundle.c revision 60945
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: head/usr.sbin/ppp/bundle.c 60945 2000-05-26 08:26:56Z brian $ 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 104static int bundle_RemainingIdleTime(struct bundle *); 105 106static const char * const PhaseNames[] = { 107 "Dead", "Establish", "Authenticate", "Network", "Terminate" 108}; 109 110const char * 111bundle_PhaseName(struct bundle *bundle) 112{ 113 return bundle->phase <= PHASE_TERMINATE ? 114 PhaseNames[bundle->phase] : "unknown"; 115} 116 117void 118bundle_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 156static void 157bundle_LayerStart(void *v, struct fsm *fp) 158{ 159 /* The given FSM is about to start up ! */ 160} 161 162 163void 164bundle_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 185static void 186bundle_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 215static void 216bundle_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 228void 229bundle_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 245static void 246bundle_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 274static void 275bundle_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 326static void 327bundle_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 347int 348bundle_LinkIsUp(const struct bundle *bundle) 349{ 350 return bundle->ncp.ipcp.fsm.state == ST_OPENED; 351} 352 353void 354bundle_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 405void 406bundle_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 414static size_t 415bundle_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 437static int 438bundle_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 494static int 495bundle_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 515static void 516bundle_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 611static int 612bundle_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 629void 630bundle_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 648static void 649bundle_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 657struct bundle * 658bundle_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 869static void 870bundle_DownInterface(struct bundle *bundle) 871{ 872 route_IfDelete(bundle, 1); 873 iface_ClearFlags(bundle->iface, IFF_UP); 874} 875 876void 877bundle_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 914struct rtmsg { 915 struct rt_msghdr m_rtm; 916 char m_space[64]; 917}; 918 919int 920bundle_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)); 1002failed: 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 1035void 1036bundle_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 1066void 1067bundle_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 dl->state == DATALINK_READY)) { 1081 timer_Stop(&dl->dial.timer); /* We're finished with this */ 1082 datalink_Up(dl, 1, 1); 1083 if (mask & PHYS_AUTO) 1084 break; /* Only one AUTO link at a time */ 1085 } 1086 if (name != NULL) 1087 break; 1088 } 1089} 1090 1091struct datalink * 1092bundle2datalink(struct bundle *bundle, const char *name) 1093{ 1094 struct datalink *dl; 1095 1096 if (name != NULL) { 1097 for (dl = bundle->links; dl; dl = dl->next) 1098 if (!strcasecmp(dl->name, name)) 1099 return dl; 1100 } else if (bundle->links && !bundle->links->next) 1101 return bundle->links; 1102 1103 return NULL; 1104} 1105 1106int 1107bundle_ShowLinks(struct cmdargs const *arg) 1108{ 1109 struct datalink *dl; 1110 struct pppThroughput *t; 1111 int secs; 1112 1113 for (dl = arg->bundle->links; dl; dl = dl->next) { 1114 prompt_Printf(arg->prompt, "Name: %s [%s, %s]", 1115 dl->name, mode2Nam(dl->physical->type), datalink_State(dl)); 1116 if (dl->physical->link.throughput.rolling && dl->state == DATALINK_OPEN) 1117 prompt_Printf(arg->prompt, " bandwidth %d, %llu bps (%llu bytes/sec)", 1118 dl->mp.bandwidth ? dl->mp.bandwidth : 1119 physical_GetSpeed(dl->physical), 1120 dl->physical->link.throughput.OctetsPerSecond * 8, 1121 dl->physical->link.throughput.OctetsPerSecond); 1122 prompt_Printf(arg->prompt, "\n"); 1123 } 1124 1125 t = &arg->bundle->ncp.mp.link.throughput; 1126 secs = t->downtime ? 0 : throughput_uptime(t); 1127 if (secs > t->SamplePeriod) 1128 secs = t->SamplePeriod; 1129 if (secs) 1130 prompt_Printf(arg->prompt, "Currently averaging %llu bps (%llu bytes/sec)" 1131 " over the last %d secs\n", t->OctetsPerSecond * 8, 1132 t->OctetsPerSecond, secs); 1133 1134 return 0; 1135} 1136 1137static const char * 1138optval(struct bundle *bundle, int bit) 1139{ 1140 return (bundle->cfg.opt & bit) ? "enabled" : "disabled"; 1141} 1142 1143int 1144bundle_ShowStatus(struct cmdargs const *arg) 1145{ 1146 int remaining; 1147 1148 prompt_Printf(arg->prompt, "Phase %s\n", bundle_PhaseName(arg->bundle)); 1149 prompt_Printf(arg->prompt, " Device: %s\n", arg->bundle->dev.Name); 1150 prompt_Printf(arg->prompt, " Interface: %s @ %lubps", 1151 arg->bundle->iface->name, arg->bundle->bandwidth); 1152 1153 if (arg->bundle->upat) { 1154 int secs = time(NULL) - arg->bundle->upat; 1155 1156 prompt_Printf(arg->prompt, ", up time %d:%02d:%02d", secs / 3600, 1157 (secs / 60) % 60, secs % 60); 1158 } 1159 1160 prompt_Printf(arg->prompt, "\n\nDefaults:\n"); 1161 prompt_Printf(arg->prompt, " Label: %s\n", arg->bundle->cfg.label); 1162 prompt_Printf(arg->prompt, " Auth name: %s\n", 1163 arg->bundle->cfg.auth.name); 1164 1165 prompt_Printf(arg->prompt, " Choked Timer: %ds\n", 1166 arg->bundle->cfg.choked.timeout); 1167 1168#ifndef NORADIUS 1169 radius_Show(&arg->bundle->radius, arg->prompt); 1170#endif 1171 1172 prompt_Printf(arg->prompt, " Idle Timer: "); 1173 if (arg->bundle->cfg.idle.timeout) { 1174 prompt_Printf(arg->prompt, "%ds", arg->bundle->cfg.idle.timeout); 1175 if (arg->bundle->cfg.idle.min_timeout) 1176 prompt_Printf(arg->prompt, ", min %ds", 1177 arg->bundle->cfg.idle.min_timeout); 1178 remaining = bundle_RemainingIdleTime(arg->bundle); 1179 if (remaining != -1) 1180 prompt_Printf(arg->prompt, " (%ds remaining)", remaining); 1181 prompt_Printf(arg->prompt, "\n"); 1182 } else 1183 prompt_Printf(arg->prompt, "disabled\n"); 1184 prompt_Printf(arg->prompt, " MTU: "); 1185 if (arg->bundle->cfg.mtu) 1186 prompt_Printf(arg->prompt, "%d\n", arg->bundle->cfg.mtu); 1187 else 1188 prompt_Printf(arg->prompt, "unspecified\n"); 1189 1190 prompt_Printf(arg->prompt, " sendpipe: "); 1191 if (arg->bundle->ncp.ipcp.cfg.sendpipe > 0) 1192 prompt_Printf(arg->prompt, "%-20ld", arg->bundle->ncp.ipcp.cfg.sendpipe); 1193 else 1194 prompt_Printf(arg->prompt, "unspecified "); 1195 prompt_Printf(arg->prompt, " recvpipe: "); 1196 if (arg->bundle->ncp.ipcp.cfg.recvpipe > 0) 1197 prompt_Printf(arg->prompt, "%ld\n", arg->bundle->ncp.ipcp.cfg.recvpipe); 1198 else 1199 prompt_Printf(arg->prompt, "unspecified\n"); 1200 1201 prompt_Printf(arg->prompt, " Sticky Routes: %-20.20s", 1202 optval(arg->bundle, OPT_SROUTES)); 1203 prompt_Printf(arg->prompt, " ID check: %s\n", 1204 optval(arg->bundle, OPT_IDCHECK)); 1205 prompt_Printf(arg->prompt, " Keep-Session: %-20.20s", 1206 optval(arg->bundle, OPT_KEEPSESSION)); 1207 prompt_Printf(arg->prompt, " Loopback: %s\n", 1208 optval(arg->bundle, OPT_LOOPBACK)); 1209 prompt_Printf(arg->prompt, " PasswdAuth: %-20.20s", 1210 optval(arg->bundle, OPT_PASSWDAUTH)); 1211 prompt_Printf(arg->prompt, " Proxy: %s\n", 1212 optval(arg->bundle, OPT_PROXY)); 1213 prompt_Printf(arg->prompt, " Proxyall: %-20.20s", 1214 optval(arg->bundle, OPT_PROXYALL)); 1215 prompt_Printf(arg->prompt, " Throughput: %s\n", 1216 optval(arg->bundle, OPT_THROUGHPUT)); 1217 prompt_Printf(arg->prompt, " Utmp Logging: %-20.20s", 1218 optval(arg->bundle, OPT_UTMP)); 1219 prompt_Printf(arg->prompt, " Iface-Alias: %s\n", 1220 optval(arg->bundle, OPT_IFACEALIAS)); 1221 1222 return 0; 1223} 1224 1225static void 1226bundle_IdleTimeout(void *v) 1227{ 1228 struct bundle *bundle = (struct bundle *)v; 1229 1230 log_Printf(LogPHASE, "Idle timer expired\n"); 1231 bundle_StopIdleTimer(bundle); 1232 bundle_Close(bundle, NULL, CLOSE_STAYDOWN); 1233} 1234 1235/* 1236 * Start Idle timer. If timeout is reached, we call bundle_Close() to 1237 * close LCP and link. 1238 */ 1239void 1240bundle_StartIdleTimer(struct bundle *bundle) 1241{ 1242 timer_Stop(&bundle->idle.timer); 1243 if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) != 1244 bundle->phys_type.open && bundle->cfg.idle.timeout) { 1245 int secs; 1246 1247 secs = bundle->cfg.idle.timeout; 1248 if (bundle->cfg.idle.min_timeout > secs && bundle->upat) { 1249 int up = time(NULL) - bundle->upat; 1250 1251 if ((long long)bundle->cfg.idle.min_timeout - up > (long long)secs) 1252 secs = bundle->cfg.idle.min_timeout - up; 1253 } 1254 bundle->idle.timer.func = bundle_IdleTimeout; 1255 bundle->idle.timer.name = "idle"; 1256 bundle->idle.timer.load = secs * SECTICKS; 1257 bundle->idle.timer.arg = bundle; 1258 timer_Start(&bundle->idle.timer); 1259 bundle->idle.done = time(NULL) + secs; 1260 } 1261} 1262 1263void 1264bundle_SetIdleTimer(struct bundle *bundle, int timeout, int min_timeout) 1265{ 1266 bundle->cfg.idle.timeout = timeout; 1267 if (min_timeout >= 0) 1268 bundle->cfg.idle.min_timeout = min_timeout; 1269 if (bundle_LinkIsUp(bundle)) 1270 bundle_StartIdleTimer(bundle); 1271} 1272 1273void 1274bundle_StopIdleTimer(struct bundle *bundle) 1275{ 1276 timer_Stop(&bundle->idle.timer); 1277 bundle->idle.done = 0; 1278} 1279 1280static int 1281bundle_RemainingIdleTime(struct bundle *bundle) 1282{ 1283 if (bundle->idle.done) 1284 return bundle->idle.done - time(NULL); 1285 return -1; 1286} 1287 1288int 1289bundle_IsDead(struct bundle *bundle) 1290{ 1291 return !bundle->links || (bundle->phase == PHASE_DEAD && bundle->CleaningUp); 1292} 1293 1294static struct datalink * 1295bundle_DatalinkLinkout(struct bundle *bundle, struct datalink *dl) 1296{ 1297 struct datalink **dlp; 1298 1299 for (dlp = &bundle->links; *dlp; dlp = &(*dlp)->next) 1300 if (*dlp == dl) { 1301 *dlp = dl->next; 1302 dl->next = NULL; 1303 bundle_LinksRemoved(bundle); 1304 return dl; 1305 } 1306 1307 return NULL; 1308} 1309 1310static void 1311bundle_DatalinkLinkin(struct bundle *bundle, struct datalink *dl) 1312{ 1313 struct datalink **dlp = &bundle->links; 1314 1315 while (*dlp) 1316 dlp = &(*dlp)->next; 1317 1318 *dlp = dl; 1319 dl->next = NULL; 1320 1321 bundle_LinkAdded(bundle, dl); 1322 mp_CheckAutoloadTimer(&bundle->ncp.mp); 1323} 1324 1325void 1326bundle_CleanDatalinks(struct bundle *bundle) 1327{ 1328 struct datalink **dlp = &bundle->links; 1329 int found = 0; 1330 1331 while (*dlp) 1332 if ((*dlp)->state == DATALINK_CLOSED && 1333 (*dlp)->physical->type & 1334 (PHYS_DIRECT|PHYS_BACKGROUND|PHYS_FOREGROUND)) { 1335 *dlp = datalink_Destroy(*dlp); 1336 found++; 1337 } else 1338 dlp = &(*dlp)->next; 1339 1340 if (found) 1341 bundle_LinksRemoved(bundle); 1342} 1343 1344int 1345bundle_DatalinkClone(struct bundle *bundle, struct datalink *dl, 1346 const char *name) 1347{ 1348 if (bundle2datalink(bundle, name)) { 1349 log_Printf(LogWARN, "Clone: %s: name already exists\n", name); 1350 return 0; 1351 } 1352 1353 bundle_DatalinkLinkin(bundle, datalink_Clone(dl, name)); 1354 return 1; 1355} 1356 1357void 1358bundle_DatalinkRemove(struct bundle *bundle, struct datalink *dl) 1359{ 1360 dl = bundle_DatalinkLinkout(bundle, dl); 1361 if (dl) 1362 datalink_Destroy(dl); 1363} 1364 1365void 1366bundle_SetLabel(struct bundle *bundle, const char *label) 1367{ 1368 if (label) 1369 strncpy(bundle->cfg.label, label, sizeof bundle->cfg.label - 1); 1370 else 1371 *bundle->cfg.label = '\0'; 1372} 1373 1374const char * 1375bundle_GetLabel(struct bundle *bundle) 1376{ 1377 return *bundle->cfg.label ? bundle->cfg.label : NULL; 1378} 1379 1380int 1381bundle_LinkSize() 1382{ 1383 struct iovec iov[SCATTER_SEGMENTS]; 1384 int niov, expect, f; 1385 1386 iov[0].iov_len = strlen(Version) + 1; 1387 iov[0].iov_base = NULL; 1388 niov = 1; 1389 if (datalink2iov(NULL, iov, &niov, SCATTER_SEGMENTS, NULL, NULL) == -1) { 1390 log_Printf(LogERROR, "Cannot determine space required for link\n"); 1391 return 0; 1392 } 1393 1394 for (f = expect = 0; f < niov; f++) 1395 expect += iov[f].iov_len; 1396 1397 return expect; 1398} 1399 1400void 1401bundle_ReceiveDatalink(struct bundle *bundle, int s) 1402{ 1403 char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int) * SEND_MAXFD]; 1404 int niov, expect, f, *fd, nfd, onfd, got; 1405 struct iovec iov[SCATTER_SEGMENTS]; 1406 struct cmsghdr *cmsg; 1407 struct msghdr msg; 1408 struct datalink *dl; 1409 pid_t pid; 1410 1411 log_Printf(LogPHASE, "Receiving datalink\n"); 1412 1413 /* 1414 * Create our scatter/gather array - passing NULL gets the space 1415 * allocation requirement rather than actually flattening the 1416 * structures. 1417 */ 1418 iov[0].iov_len = strlen(Version) + 1; 1419 iov[0].iov_base = NULL; 1420 niov = 1; 1421 if (datalink2iov(NULL, iov, &niov, SCATTER_SEGMENTS, NULL, NULL) == -1) { 1422 log_Printf(LogERROR, "Cannot determine space required for link\n"); 1423 return; 1424 } 1425 1426 /* Allocate the scatter/gather array for recvmsg() */ 1427 for (f = expect = 0; f < niov; f++) { 1428 if ((iov[f].iov_base = malloc(iov[f].iov_len)) == NULL) { 1429 log_Printf(LogERROR, "Cannot allocate space to receive link\n"); 1430 return; 1431 } 1432 if (f) 1433 expect += iov[f].iov_len; 1434 } 1435 1436 /* Set up our message */ 1437 cmsg = (struct cmsghdr *)cmsgbuf; 1438 cmsg->cmsg_len = sizeof cmsgbuf; 1439 cmsg->cmsg_level = SOL_SOCKET; 1440 cmsg->cmsg_type = 0; 1441 1442 memset(&msg, '\0', sizeof msg); 1443 msg.msg_name = NULL; 1444 msg.msg_namelen = 0; 1445 msg.msg_iov = iov; 1446 msg.msg_iovlen = 1; /* Only send the version at the first pass */ 1447 msg.msg_control = cmsgbuf; 1448 msg.msg_controllen = sizeof cmsgbuf; 1449 1450 log_Printf(LogDEBUG, "Expecting %u scatter/gather bytes\n", 1451 (unsigned)iov[0].iov_len); 1452 1453 if ((got = recvmsg(s, &msg, MSG_WAITALL)) != iov[0].iov_len) { 1454 if (got == -1) 1455 log_Printf(LogERROR, "Failed recvmsg: %s\n", strerror(errno)); 1456 else 1457 log_Printf(LogERROR, "Failed recvmsg: Got %d, not %u\n", 1458 got, (unsigned)iov[0].iov_len); 1459 while (niov--) 1460 free(iov[niov].iov_base); 1461 return; 1462 } 1463 1464 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) { 1465 log_Printf(LogERROR, "Recvmsg: no descriptors received !\n"); 1466 while (niov--) 1467 free(iov[niov].iov_base); 1468 return; 1469 } 1470 1471 fd = (int *)(cmsg + 1); 1472 nfd = (cmsg->cmsg_len - sizeof *cmsg) / sizeof(int); 1473 1474 if (nfd < 2) { 1475 log_Printf(LogERROR, "Recvmsg: %d descriptor%s received (too few) !\n", 1476 nfd, nfd == 1 ? "" : "s"); 1477 while (nfd--) 1478 close(fd[nfd]); 1479 while (niov--) 1480 free(iov[niov].iov_base); 1481 return; 1482 } 1483 1484 /* 1485 * We've successfully received two or more open file descriptors 1486 * through our socket, plus a version string. Make sure it's the 1487 * correct version, and drop the connection if it's not. 1488 */ 1489 if (strncmp(Version, iov[0].iov_base, iov[0].iov_len)) { 1490 log_Printf(LogWARN, "Cannot receive datalink, incorrect version" 1491 " (\"%.*s\", not \"%s\")\n", (int)iov[0].iov_len, 1492 (char *)iov[0].iov_base, Version); 1493 while (nfd--) 1494 close(fd[nfd]); 1495 while (niov--) 1496 free(iov[niov].iov_base); 1497 return; 1498 } 1499 1500 /* 1501 * Everything looks good. Send the other side our process id so that 1502 * they can transfer lock ownership, and wait for them to send the 1503 * actual link data. 1504 */ 1505 pid = getpid(); 1506 if ((got = write(fd[1], &pid, sizeof pid)) != sizeof pid) { 1507 if (got == -1) 1508 log_Printf(LogERROR, "Failed write: %s\n", strerror(errno)); 1509 else 1510 log_Printf(LogERROR, "Failed write: Got %d, not %d\n", got, 1511 (int)(sizeof pid)); 1512 while (nfd--) 1513 close(fd[nfd]); 1514 while (niov--) 1515 free(iov[niov].iov_base); 1516 return; 1517 } 1518 1519 if ((got = readv(fd[1], iov + 1, niov - 1)) != expect) { 1520 if (got == -1) 1521 log_Printf(LogERROR, "Failed write: %s\n", strerror(errno)); 1522 else 1523 log_Printf(LogERROR, "Failed write: Got %d, not %d\n", got, expect); 1524 while (nfd--) 1525 close(fd[nfd]); 1526 while (niov--) 1527 free(iov[niov].iov_base); 1528 return; 1529 } 1530 close(fd[1]); 1531 1532 onfd = nfd; /* We've got this many in our array */ 1533 nfd -= 2; /* Don't include p->fd and our reply descriptor */ 1534 niov = 1; /* Skip the version id */ 1535 dl = iov2datalink(bundle, iov, &niov, sizeof iov / sizeof *iov, fd[0], 1536 fd + 2, &nfd); 1537 if (dl) { 1538 1539 if (nfd) { 1540 log_Printf(LogERROR, "bundle_ReceiveDatalink: Failed to handle %d " 1541 "auxiliary file descriptors (%d remain)\n", onfd, nfd); 1542 datalink_Destroy(dl); 1543 while (nfd--) 1544 close(fd[onfd--]); 1545 close(fd[0]); 1546 } else { 1547 bundle_DatalinkLinkin(bundle, dl); 1548 datalink_AuthOk(dl); 1549 bundle_CalculateBandwidth(dl->bundle); 1550 } 1551 } else { 1552 while (nfd--) 1553 close(fd[onfd--]); 1554 close(fd[0]); 1555 close(fd[1]); 1556 } 1557 1558 free(iov[0].iov_base); 1559} 1560 1561void 1562bundle_SendDatalink(struct datalink *dl, int s, struct sockaddr_un *sun) 1563{ 1564 char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int) * SEND_MAXFD]; 1565 const char *constlock; 1566 char *lock; 1567 struct cmsghdr *cmsg; 1568 struct msghdr msg; 1569 struct iovec iov[SCATTER_SEGMENTS]; 1570 int niov, f, expect, newsid, fd[SEND_MAXFD], nfd, reply[2], got; 1571 pid_t newpid; 1572 1573 log_Printf(LogPHASE, "Transmitting datalink %s\n", dl->name); 1574 1575 /* Record the base device name for a lock transfer later */ 1576 constlock = physical_LockedDevice(dl->physical); 1577 if (constlock) { 1578 lock = alloca(strlen(constlock) + 1); 1579 strcpy(lock, constlock); 1580 } else 1581 lock = NULL; 1582 1583 bundle_LinkClosed(dl->bundle, dl); 1584 bundle_DatalinkLinkout(dl->bundle, dl); 1585 1586 /* Build our scatter/gather array */ 1587 iov[0].iov_len = strlen(Version) + 1; 1588 iov[0].iov_base = strdup(Version); 1589 niov = 1; 1590 nfd = 0; 1591 1592 fd[0] = datalink2iov(dl, iov, &niov, SCATTER_SEGMENTS, fd + 2, &nfd); 1593 1594 if (fd[0] != -1 && socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, reply) != -1) { 1595 /* 1596 * fd[1] is used to get the peer process id back, then to confirm that 1597 * we've transferred any device locks to that process id. 1598 */ 1599 fd[1] = reply[1]; 1600 1601 nfd += 2; /* Include fd[0] and fd[1] */ 1602 memset(&msg, '\0', sizeof msg); 1603 1604 msg.msg_name = NULL; 1605 msg.msg_namelen = 0; 1606 /* 1607 * Only send the version to start... We used to send the whole lot, but 1608 * this caused problems with our RECVBUF size as a single link is about 1609 * 22k ! This way, we should bump into no limits. 1610 */ 1611 msg.msg_iovlen = 1; 1612 msg.msg_iov = iov; 1613 msg.msg_control = cmsgbuf; 1614 msg.msg_controllen = sizeof *cmsg + sizeof(int) * nfd; 1615 msg.msg_flags = 0; 1616 1617 cmsg = (struct cmsghdr *)cmsgbuf; 1618 cmsg->cmsg_len = msg.msg_controllen; 1619 cmsg->cmsg_level = SOL_SOCKET; 1620 cmsg->cmsg_type = SCM_RIGHTS; 1621 1622 for (f = 0; f < nfd; f++) 1623 *((int *)(cmsg + 1) + f) = fd[f]; 1624 1625 for (f = 1, expect = 0; f < niov; f++) 1626 expect += iov[f].iov_len; 1627 1628 if (setsockopt(reply[0], SOL_SOCKET, SO_SNDBUF, &expect, sizeof(int)) == -1) 1629 log_Printf(LogERROR, "setsockopt(SO_RCVBUF, %d): %s\n", expect, 1630 strerror(errno)); 1631 if (setsockopt(reply[1], SOL_SOCKET, SO_RCVBUF, &expect, sizeof(int)) == -1) 1632 log_Printf(LogERROR, "setsockopt(SO_RCVBUF, %d): %s\n", expect, 1633 strerror(errno)); 1634 1635 log_Printf(LogDEBUG, "Sending %d descriptor%s and %u bytes in scatter" 1636 "/gather array\n", nfd, nfd == 1 ? "" : "s", 1637 (unsigned)iov[0].iov_len); 1638 1639 if ((got = sendmsg(s, &msg, 0)) == -1) 1640 log_Printf(LogERROR, "Failed sendmsg: %s: %s\n", 1641 sun->sun_path, strerror(errno)); 1642 else if (got != iov[0].iov_len) 1643 log_Printf(LogERROR, "%s: Failed initial sendmsg: Only sent %d of %u\n", 1644 sun->sun_path, got, (unsigned)iov[0].iov_len); 1645 else { 1646 /* We must get the ACK before closing the descriptor ! */ 1647 int res; 1648 1649 if ((got = read(reply[0], &newpid, sizeof newpid)) == sizeof newpid) { 1650 log_Printf(LogDEBUG, "Received confirmation from pid %d\n", 1651 (int)newpid); 1652 if (lock && (res = ID0uu_lock_txfr(lock, newpid)) != UU_LOCK_OK) 1653 log_Printf(LogERROR, "uu_lock_txfr: %s\n", uu_lockerr(res)); 1654 1655 log_Printf(LogDEBUG, "Transmitting link (%d bytes)\n", expect); 1656 if ((got = writev(reply[0], iov + 1, niov - 1)) != expect) { 1657 if (got == -1) 1658 log_Printf(LogERROR, "%s: Failed writev: %s\n", 1659 sun->sun_path, strerror(errno)); 1660 else 1661 log_Printf(LogERROR, "%s: Failed writev: Wrote %d of %d\n", 1662 sun->sun_path, got, expect); 1663 } 1664 } else if (got == -1) 1665 log_Printf(LogERROR, "%s: Failed socketpair read: %s\n", 1666 sun->sun_path, strerror(errno)); 1667 else 1668 log_Printf(LogERROR, "%s: Failed socketpair read: Got %d of %d\n", 1669 sun->sun_path, got, (int)(sizeof newpid)); 1670 } 1671 1672 close(reply[0]); 1673 close(reply[1]); 1674 1675 newsid = Enabled(dl->bundle, OPT_KEEPSESSION) || 1676 tcgetpgrp(fd[0]) == getpgrp(); 1677 while (nfd) 1678 close(fd[--nfd]); 1679 if (newsid) 1680 bundle_setsid(dl->bundle, got != -1); 1681 } 1682 close(s); 1683 1684 while (niov--) 1685 free(iov[niov].iov_base); 1686} 1687 1688int 1689bundle_RenameDatalink(struct bundle *bundle, struct datalink *ndl, 1690 const char *name) 1691{ 1692 struct datalink *dl; 1693 1694 if (!strcasecmp(ndl->name, name)) 1695 return 1; 1696 1697 for (dl = bundle->links; dl; dl = dl->next) 1698 if (!strcasecmp(dl->name, name)) 1699 return 0; 1700 1701 datalink_Rename(ndl, name); 1702 return 1; 1703} 1704 1705int 1706bundle_SetMode(struct bundle *bundle, struct datalink *dl, int mode) 1707{ 1708 int omode; 1709 1710 omode = dl->physical->type; 1711 if (omode == mode) 1712 return 1; 1713 1714 if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO)) 1715 /* First auto link */ 1716 if (bundle->ncp.ipcp.peer_ip.s_addr == INADDR_ANY) { 1717 log_Printf(LogWARN, "You must `set ifaddr' or `open' before" 1718 " changing mode to %s\n", mode2Nam(mode)); 1719 return 0; 1720 } 1721 1722 if (!datalink_SetMode(dl, mode)) 1723 return 0; 1724 1725 if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO) && 1726 bundle->phase != PHASE_NETWORK) 1727 /* First auto link, we need an interface */ 1728 ipcp_InterfaceUp(&bundle->ncp.ipcp); 1729 1730 /* Regenerate phys_type and adjust idle timer */ 1731 bundle_LinksRemoved(bundle); 1732 1733 return 1; 1734} 1735 1736void 1737bundle_setsid(struct bundle *bundle, int holdsession) 1738{ 1739 /* 1740 * Lose the current session. This means getting rid of our pid 1741 * too so that the tty device will really go away, and any getty 1742 * etc will be allowed to restart. 1743 */ 1744 pid_t pid, orig; 1745 int fds[2]; 1746 char done; 1747 struct datalink *dl; 1748 1749 if (!holdsession && bundle_IsDead(bundle)) { 1750 /* 1751 * No need to lose our session after all... we're going away anyway 1752 * 1753 * We should really stop the timer and pause if holdsession is set and 1754 * the bundle's dead, but that leaves other resources lying about :-( 1755 */ 1756 return; 1757 } 1758 1759 orig = getpid(); 1760 if (pipe(fds) == -1) { 1761 log_Printf(LogERROR, "pipe: %s\n", strerror(errno)); 1762 return; 1763 } 1764 switch ((pid = fork())) { 1765 case -1: 1766 log_Printf(LogERROR, "fork: %s\n", strerror(errno)); 1767 close(fds[0]); 1768 close(fds[1]); 1769 return; 1770 case 0: 1771 close(fds[1]); 1772 read(fds[0], &done, 1); /* uu_locks are mine ! */ 1773 close(fds[0]); 1774 if (pipe(fds) == -1) { 1775 log_Printf(LogERROR, "pipe(2): %s\n", strerror(errno)); 1776 return; 1777 } 1778 switch ((pid = fork())) { 1779 case -1: 1780 log_Printf(LogERROR, "fork(2): %s\n", strerror(errno)); 1781 close(fds[0]); 1782 close(fds[1]); 1783 return; 1784 case 0: 1785 close(fds[1]); 1786 bundle_LockTun(bundle); /* update pid */ 1787 read(fds[0], &done, 1); /* uu_locks are mine ! */ 1788 close(fds[0]); 1789 setsid(); 1790 bundle_ChangedPID(bundle); 1791 log_Printf(LogDEBUG, "%d -> %d: %s session control\n", 1792 (int)orig, (int)getpid(), 1793 holdsession ? "Passed" : "Dropped"); 1794 timer_InitService(0); /* Start the Timer Service */ 1795 break; 1796 default: 1797 close(fds[0]); 1798 /* Give away all our physical locks (to the final process) */ 1799 for (dl = bundle->links; dl; dl = dl->next) 1800 if (dl->state != DATALINK_CLOSED) 1801 physical_ChangedPid(dl->physical, pid); 1802 write(fds[1], "!", 1); /* done */ 1803 close(fds[1]); 1804 _exit(0); 1805 break; 1806 } 1807 break; 1808 default: 1809 close(fds[0]); 1810 /* Give away all our physical locks (to the intermediate process) */ 1811 for (dl = bundle->links; dl; dl = dl->next) 1812 if (dl->state != DATALINK_CLOSED) 1813 physical_ChangedPid(dl->physical, pid); 1814 write(fds[1], "!", 1); /* done */ 1815 close(fds[1]); 1816 if (holdsession) { 1817 int fd, status; 1818 1819 timer_TermService(); 1820 signal(SIGPIPE, SIG_DFL); 1821 signal(SIGALRM, SIG_DFL); 1822 signal(SIGHUP, SIG_DFL); 1823 signal(SIGTERM, SIG_DFL); 1824 signal(SIGINT, SIG_DFL); 1825 signal(SIGQUIT, SIG_DFL); 1826 for (fd = getdtablesize(); fd >= 0; fd--) 1827 close(fd); 1828 /* 1829 * Reap the intermediate process. As we're not exiting but the 1830 * intermediate is, we don't want it to become defunct. 1831 */ 1832 waitpid(pid, &status, 0); 1833 /* Tweak our process arguments.... */ 1834 ID0setproctitle("session owner"); 1835 setuid(ID0realuid()); 1836 /* 1837 * Hang around for a HUP. This should happen as soon as the 1838 * ppp that we passed our ctty descriptor to closes it. 1839 * NOTE: If this process dies, the passed descriptor becomes 1840 * invalid and will give a select() error by setting one 1841 * of the error fds, aborting the other ppp. We don't 1842 * want that to happen ! 1843 */ 1844 pause(); 1845 } 1846 _exit(0); 1847 break; 1848 } 1849} 1850 1851int 1852bundle_HighestState(struct bundle *bundle) 1853{ 1854 struct datalink *dl; 1855 int result = DATALINK_CLOSED; 1856 1857 for (dl = bundle->links; dl; dl = dl->next) 1858 if (result < dl->state) 1859 result = dl->state; 1860 1861 return result; 1862} 1863 1864int 1865bundle_Exception(struct bundle *bundle, int fd) 1866{ 1867 struct datalink *dl; 1868 1869 for (dl = bundle->links; dl; dl = dl->next) 1870 if (dl->physical->fd == fd) { 1871 datalink_Down(dl, CLOSE_NORMAL); 1872 return 1; 1873 } 1874 1875 return 0; 1876} 1877 1878void 1879bundle_AdjustFilters(struct bundle *bundle, struct in_addr *my_ip, 1880 struct in_addr *peer_ip) 1881{ 1882 filter_AdjustAddr(&bundle->filter.in, my_ip, peer_ip, NULL); 1883 filter_AdjustAddr(&bundle->filter.out, my_ip, peer_ip, NULL); 1884 filter_AdjustAddr(&bundle->filter.dial, my_ip, peer_ip, NULL); 1885 filter_AdjustAddr(&bundle->filter.alive, my_ip, peer_ip, NULL); 1886} 1887 1888void 1889bundle_AdjustDNS(struct bundle *bundle, struct in_addr dns[2]) 1890{ 1891 filter_AdjustAddr(&bundle->filter.in, NULL, NULL, dns); 1892 filter_AdjustAddr(&bundle->filter.out, NULL, NULL, dns); 1893 filter_AdjustAddr(&bundle->filter.dial, NULL, NULL, dns); 1894 filter_AdjustAddr(&bundle->filter.alive, NULL, NULL, dns); 1895} 1896 1897void 1898bundle_CalculateBandwidth(struct bundle *bundle) 1899{ 1900 struct datalink *dl; 1901 int mtu, sp; 1902 1903 bundle->bandwidth = 0; 1904 mtu = 0; 1905 for (dl = bundle->links; dl; dl = dl->next) 1906 if (dl->state == DATALINK_OPEN) { 1907 if ((sp = dl->mp.bandwidth) == 0 && 1908 (sp = physical_GetSpeed(dl->physical)) == 0) 1909 log_Printf(LogDEBUG, "%s: %s: Cannot determine bandwidth\n", 1910 dl->name, dl->physical->name.full); 1911 else 1912 bundle->bandwidth += sp; 1913 if (!bundle->ncp.mp.active) { 1914 mtu = dl->physical->link.lcp.his_mru; 1915 break; 1916 } 1917 } 1918 1919 if(bundle->bandwidth == 0) 1920 bundle->bandwidth = 115200; /* Shrug */ 1921 1922 if (bundle->ncp.mp.active) 1923 mtu = bundle->ncp.mp.peer_mrru; 1924 else if (!mtu) 1925 mtu = 1500; 1926 1927#ifndef NORADIUS 1928 if (bundle->radius.valid && bundle->radius.mtu && bundle->radius.mtu < mtu) { 1929 log_Printf(LogLCP, "Reducing MTU to radius value %lu\n", 1930 bundle->radius.mtu); 1931 mtu = bundle->radius.mtu; 1932 } 1933#endif 1934 1935 tun_configure(bundle, mtu); 1936} 1937 1938void 1939bundle_AutoAdjust(struct bundle *bundle, int percent, int what) 1940{ 1941 struct datalink *dl, *choice, *otherlinkup; 1942 1943 choice = otherlinkup = NULL; 1944 for (dl = bundle->links; dl; dl = dl->next) 1945 if (dl->physical->type == PHYS_AUTO) { 1946 if (dl->state == DATALINK_OPEN) { 1947 if (what == AUTO_DOWN) { 1948 if (choice) 1949 otherlinkup = choice; 1950 choice = dl; 1951 } 1952 } else if (dl->state == DATALINK_CLOSED) { 1953 if (what == AUTO_UP) { 1954 choice = dl; 1955 break; 1956 } 1957 } else { 1958 /* An auto link in an intermediate state - forget it for the moment */ 1959 choice = NULL; 1960 break; 1961 } 1962 } else if (dl->state == DATALINK_OPEN && what == AUTO_DOWN) 1963 otherlinkup = dl; 1964 1965 if (choice) { 1966 if (what == AUTO_UP) { 1967 log_Printf(LogPHASE, "%d%% saturation -> Opening link ``%s''\n", 1968 percent, choice->name); 1969 datalink_Up(choice, 1, 1); 1970 mp_StopAutoloadTimer(&bundle->ncp.mp); 1971 } else if (otherlinkup) { /* Only bring the second-last link down */ 1972 log_Printf(LogPHASE, "%d%% saturation -> Closing link ``%s''\n", 1973 percent, choice->name); 1974 datalink_Close(choice, CLOSE_STAYDOWN); 1975 mp_StopAutoloadTimer(&bundle->ncp.mp); 1976 } 1977 } 1978} 1979 1980int 1981bundle_WantAutoloadTimer(struct bundle *bundle) 1982{ 1983 struct datalink *dl; 1984 int autolink, opened; 1985 1986 if (bundle->phase == PHASE_NETWORK) { 1987 for (autolink = opened = 0, dl = bundle->links; dl; dl = dl->next) 1988 if (dl->physical->type == PHYS_AUTO) { 1989 if (++autolink == 2 || (autolink == 1 && opened)) 1990 /* Two auto links or one auto and one open in NETWORK phase */ 1991 return 1; 1992 } else if (dl->state == DATALINK_OPEN) { 1993 opened++; 1994 if (autolink) 1995 /* One auto and one open link in NETWORK phase */ 1996 return 1; 1997 } 1998 } 1999 2000 return 0; 2001} 2002 2003void 2004bundle_ChangedPID(struct bundle *bundle) 2005{ 2006#ifdef TUNSIFPID 2007 ioctl(bundle->dev.fd, TUNSIFPID, 0); 2008#endif 2009} 2010