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