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