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