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