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