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