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