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