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