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