bundle.c revision 36368
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.7 1998/05/25 02:22:30 brian Exp $ 27 */ 28 29#include <sys/types.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, "tun: fdset(r) %d\n", 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 tun: %s\n", strerror(errno)); 571 return; 572 } 573 n -= sizeof tun - sizeof tun.data; 574 if (n <= 0) { 575 log_Printf(LogERROR, "read from tun: Only %d bytes read\n", 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 656 657struct bundle * 658bundle_Create(const char *prefix, int type) 659{ 660 int s, enoentcount, err; 661 struct ifreq ifrq; 662 static struct bundle bundle; /* there can be only one */ 663 664 if (bundle.ifp.Name != NULL) { /* Already allocated ! */ 665 log_Printf(LogERROR, "bundle_Create: There's only one BUNDLE !\n"); 666 return NULL; 667 } 668 669 err = ENOENT; 670 enoentcount = 0; 671 for (bundle.unit = 0; ; bundle.unit++) { 672 snprintf(bundle.dev.Name, sizeof bundle.dev.Name, "%s%d", 673 prefix, bundle.unit); 674 bundle.dev.fd = ID0open(bundle.dev.Name, O_RDWR); 675 if (bundle.dev.fd >= 0) 676 break; 677 else if (errno == ENXIO) { 678 err = errno; 679 break; 680 } else if (errno == ENOENT) { 681 if (++enoentcount > 2) 682 break; 683 } else 684 err = errno; 685 } 686 687 if (bundle.dev.fd < 0) { 688 log_Printf(LogWARN, "No available tunnel devices found (%s).\n", 689 strerror(err)); 690 return NULL; 691 } 692 693 log_SetTun(bundle.unit); 694 695 s = socket(AF_INET, SOCK_DGRAM, 0); 696 if (s < 0) { 697 log_Printf(LogERROR, "bundle_Create: socket(): %s\n", strerror(errno)); 698 close(bundle.dev.fd); 699 return NULL; 700 } 701 702 bundle.ifp.Name = strrchr(bundle.dev.Name, '/'); 703 if (bundle.ifp.Name == NULL) 704 bundle.ifp.Name = bundle.dev.Name; 705 else 706 bundle.ifp.Name++; 707 708 /* 709 * Now, bring up the interface. 710 */ 711 memset(&ifrq, '\0', sizeof ifrq); 712 strncpy(ifrq.ifr_name, bundle.ifp.Name, sizeof ifrq.ifr_name - 1); 713 ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0'; 714 if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) { 715 log_Printf(LogERROR, "OpenTunnel: ioctl(SIOCGIFFLAGS): %s\n", 716 strerror(errno)); 717 close(s); 718 close(bundle.dev.fd); 719 bundle.ifp.Name = NULL; 720 return NULL; 721 } 722 ifrq.ifr_flags |= IFF_UP; 723 if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) { 724 log_Printf(LogERROR, "OpenTunnel: ioctl(SIOCSIFFLAGS): %s\n", 725 strerror(errno)); 726 close(s); 727 close(bundle.dev.fd); 728 bundle.ifp.Name = NULL; 729 return NULL; 730 } 731 732 close(s); 733 734 if ((bundle.ifp.Index = GetIfIndex(bundle.ifp.Name)) < 0) { 735 log_Printf(LogERROR, "OpenTunnel: Can't find interface index.\n"); 736 close(bundle.dev.fd); 737 bundle.ifp.Name = NULL; 738 return NULL; 739 } 740 log_Printf(LogPHASE, "Using interface: %s\n", bundle.ifp.Name); 741 742 bundle.ifp.Speed = 0; 743 744 bundle.routing_seq = 0; 745 bundle.phase = PHASE_DEAD; 746 bundle.CleaningUp = 0; 747 748 bundle.fsm.LayerStart = bundle_LayerStart; 749 bundle.fsm.LayerUp = bundle_LayerUp; 750 bundle.fsm.LayerDown = bundle_LayerDown; 751 bundle.fsm.LayerFinish = bundle_LayerFinish; 752 bundle.fsm.object = &bundle; 753 754 bundle.cfg.idle_timeout = NCP_IDLE_TIMEOUT; 755 *bundle.cfg.auth.name = '\0'; 756 *bundle.cfg.auth.key = '\0'; 757 bundle.cfg.opt = OPT_SROUTES | OPT_IDCHECK | OPT_LOOPBACK | 758 OPT_THROUGHPUT | OPT_UTMP; 759 *bundle.cfg.label = '\0'; 760 bundle.cfg.mtu = DEF_MTU; 761 bundle.cfg.autoload.max.packets = 0; 762 bundle.cfg.autoload.max.timeout = 0; 763 bundle.cfg.autoload.min.packets = 0; 764 bundle.cfg.autoload.min.timeout = 0; 765 bundle.phys_type = type; 766 767 bundle.links = datalink_Create("deflink", &bundle, type); 768 if (bundle.links == NULL) { 769 log_Printf(LogERROR, "Cannot create data link: %s\n", strerror(errno)); 770 close(bundle.dev.fd); 771 bundle.ifp.Name = NULL; 772 return NULL; 773 } 774 775 bundle.desc.type = BUNDLE_DESCRIPTOR; 776 bundle.desc.UpdateSet = bundle_UpdateSet; 777 bundle.desc.IsSet = bundle_IsSet; 778 bundle.desc.Read = bundle_DescriptorRead; 779 bundle.desc.Write = bundle_DescriptorWrite; 780 781 mp_Init(&bundle.ncp.mp, &bundle); 782 783 /* Send over the first physical link by default */ 784 ipcp_Init(&bundle.ncp.ipcp, &bundle, &bundle.links->physical->link, 785 &bundle.fsm); 786 787 memset(&bundle.filter, '\0', sizeof bundle.filter); 788 bundle.filter.in.fragok = bundle.filter.in.logok = 1; 789 bundle.filter.in.name = "IN"; 790 bundle.filter.out.fragok = bundle.filter.out.logok = 1; 791 bundle.filter.out.name = "OUT"; 792 bundle.filter.dial.name = "DIAL"; 793 bundle.filter.dial.logok = 1; 794 bundle.filter.alive.name = "ALIVE"; 795 bundle.filter.alive.logok = 1; 796 memset(&bundle.idle.timer, '\0', sizeof bundle.idle.timer); 797 bundle.idle.done = 0; 798 bundle.notify.fd = -1; 799 memset(&bundle.autoload.timer, '\0', sizeof bundle.autoload.timer); 800 bundle.autoload.done = 0; 801 bundle.autoload.running = 0; 802 803 /* Clean out any leftover crud */ 804 bundle_CleanInterface(&bundle); 805 806 return &bundle; 807} 808 809static void 810bundle_DownInterface(struct bundle *bundle) 811{ 812 struct ifreq ifrq; 813 int s; 814 815 route_IfDelete(bundle, 1); 816 817 s = ID0socket(AF_INET, SOCK_DGRAM, 0); 818 if (s < 0) { 819 log_Printf(LogERROR, "bundle_DownInterface: socket: %s\n", strerror(errno)); 820 return; 821 } 822 823 memset(&ifrq, '\0', sizeof ifrq); 824 strncpy(ifrq.ifr_name, bundle->ifp.Name, sizeof ifrq.ifr_name - 1); 825 ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0'; 826 if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) { 827 log_Printf(LogERROR, "bundle_DownInterface: ioctl(SIOCGIFFLAGS): %s\n", 828 strerror(errno)); 829 close(s); 830 return; 831 } 832 ifrq.ifr_flags &= ~IFF_UP; 833 if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) { 834 log_Printf(LogERROR, "bundle_DownInterface: ioctl(SIOCSIFFLAGS): %s\n", 835 strerror(errno)); 836 close(s); 837 return; 838 } 839 close(s); 840} 841 842void 843bundle_Destroy(struct bundle *bundle) 844{ 845 struct datalink *dl; 846 847 /* 848 * Clean up the interface. We don't need to timer_Stop()s, mp_Down(), 849 * ipcp_CleanInterface() and bundle_DownInterface() unless we're getting 850 * out under exceptional conditions such as a descriptor exception. 851 */ 852 timer_Stop(&bundle->idle.timer); 853 timer_Stop(&bundle->autoload.timer); 854 mp_Down(&bundle->ncp.mp); 855 ipcp_CleanInterface(&bundle->ncp.ipcp); 856 bundle_DownInterface(bundle); 857 858 /* Again, these are all DATALINK_CLOSED unless we're abending */ 859 dl = bundle->links; 860 while (dl) 861 dl = datalink_Destroy(dl); 862 863 /* In case we never made PHASE_NETWORK */ 864 bundle_Notify(bundle, EX_ERRDEAD); 865 866 bundle->ifp.Name = NULL; 867} 868 869struct rtmsg { 870 struct rt_msghdr m_rtm; 871 char m_space[64]; 872}; 873 874int 875bundle_SetRoute(struct bundle *bundle, int cmd, struct in_addr dst, 876 struct in_addr gateway, struct in_addr mask, int bang) 877{ 878 struct rtmsg rtmes; 879 int s, nb, wb; 880 char *cp; 881 const char *cmdstr; 882 struct sockaddr_in rtdata; 883 int result = 1; 884 885 if (bang) 886 cmdstr = (cmd == RTM_ADD ? "Add!" : "Delete!"); 887 else 888 cmdstr = (cmd == RTM_ADD ? "Add" : "Delete"); 889 s = ID0socket(PF_ROUTE, SOCK_RAW, 0); 890 if (s < 0) { 891 log_Printf(LogERROR, "bundle_SetRoute: socket(): %s\n", strerror(errno)); 892 return result; 893 } 894 memset(&rtmes, '\0', sizeof rtmes); 895 rtmes.m_rtm.rtm_version = RTM_VERSION; 896 rtmes.m_rtm.rtm_type = cmd; 897 rtmes.m_rtm.rtm_addrs = RTA_DST; 898 rtmes.m_rtm.rtm_seq = ++bundle->routing_seq; 899 rtmes.m_rtm.rtm_pid = getpid(); 900 rtmes.m_rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC; 901 902 memset(&rtdata, '\0', sizeof rtdata); 903 rtdata.sin_len = sizeof rtdata; 904 rtdata.sin_family = AF_INET; 905 rtdata.sin_port = 0; 906 rtdata.sin_addr = dst; 907 908 cp = rtmes.m_space; 909 memcpy(cp, &rtdata, rtdata.sin_len); 910 cp += rtdata.sin_len; 911 if (cmd == RTM_ADD) { 912 if (gateway.s_addr == INADDR_ANY) { 913 /* Add a route through the interface */ 914 struct sockaddr_dl dl; 915 const char *iname; 916 int ilen; 917 918 iname = Index2Nam(bundle->ifp.Index); 919 ilen = strlen(iname); 920 dl.sdl_len = sizeof dl - sizeof dl.sdl_data + ilen; 921 dl.sdl_family = AF_LINK; 922 dl.sdl_index = bundle->ifp.Index; 923 dl.sdl_type = 0; 924 dl.sdl_nlen = ilen; 925 dl.sdl_alen = 0; 926 dl.sdl_slen = 0; 927 strncpy(dl.sdl_data, iname, sizeof dl.sdl_data); 928 memcpy(cp, &dl, dl.sdl_len); 929 cp += dl.sdl_len; 930 rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY; 931 } else { 932 rtdata.sin_addr = gateway; 933 memcpy(cp, &rtdata, rtdata.sin_len); 934 cp += rtdata.sin_len; 935 rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY; 936 } 937 } 938 939 if (dst.s_addr == INADDR_ANY) 940 mask.s_addr = INADDR_ANY; 941 942 if (cmd == RTM_ADD || dst.s_addr == INADDR_ANY) { 943 rtdata.sin_addr = mask; 944 memcpy(cp, &rtdata, rtdata.sin_len); 945 cp += rtdata.sin_len; 946 rtmes.m_rtm.rtm_addrs |= RTA_NETMASK; 947 } 948 949 nb = cp - (char *) &rtmes; 950 rtmes.m_rtm.rtm_msglen = nb; 951 wb = ID0write(s, &rtmes, nb); 952 if (wb < 0) { 953 log_Printf(LogTCPIP, "bundle_SetRoute failure:\n"); 954 log_Printf(LogTCPIP, "bundle_SetRoute: Cmd = %s\n", cmdstr); 955 log_Printf(LogTCPIP, "bundle_SetRoute: Dst = %s\n", inet_ntoa(dst)); 956 log_Printf(LogTCPIP, "bundle_SetRoute: Gateway = %s\n", inet_ntoa(gateway)); 957 log_Printf(LogTCPIP, "bundle_SetRoute: Mask = %s\n", inet_ntoa(mask)); 958failed: 959 if (cmd == RTM_ADD && (rtmes.m_rtm.rtm_errno == EEXIST || 960 (rtmes.m_rtm.rtm_errno == 0 && errno == EEXIST))) { 961 if (!bang) { 962 log_Printf(LogWARN, "Add route failed: %s already exists\n", 963 inet_ntoa(dst)); 964 result = 0; /* Don't add to our dynamic list */ 965 } else { 966 rtmes.m_rtm.rtm_type = cmd = RTM_CHANGE; 967 if ((wb = ID0write(s, &rtmes, nb)) < 0) 968 goto failed; 969 } 970 } else if (cmd == RTM_DELETE && 971 (rtmes.m_rtm.rtm_errno == ESRCH || 972 (rtmes.m_rtm.rtm_errno == 0 && errno == ESRCH))) { 973 if (!bang) 974 log_Printf(LogWARN, "Del route failed: %s: Non-existent\n", 975 inet_ntoa(dst)); 976 } else if (rtmes.m_rtm.rtm_errno == 0) 977 log_Printf(LogWARN, "%s route failed: %s: errno: %s\n", cmdstr, 978 inet_ntoa(dst), strerror(errno)); 979 else 980 log_Printf(LogWARN, "%s route failed: %s: %s\n", 981 cmdstr, inet_ntoa(dst), strerror(rtmes.m_rtm.rtm_errno)); 982 } 983 log_Printf(LogDEBUG, "wrote %d: cmd = %s, dst = %x, gateway = %x\n", 984 wb, cmdstr, (unsigned)dst.s_addr, (unsigned)gateway.s_addr); 985 close(s); 986 987 return result; 988} 989 990void 991bundle_LinkClosed(struct bundle *bundle, struct datalink *dl) 992{ 993 /* 994 * Our datalink has closed. 995 * CleanDatalinks() (called from DoLoop()) will remove closed 996 * 1OFF and DIRECT links. 997 * If it's the last data link, enter phase DEAD. 998 * 999 * NOTE: dl may not be in our list (bundle_SendDatalink()) ! 1000 */ 1001 1002 struct datalink *odl; 1003 int other_links; 1004 1005 other_links = 0; 1006 for (odl = bundle->links; odl; odl = odl->next) 1007 if (odl != dl && odl->state != DATALINK_CLOSED) 1008 other_links++; 1009 1010 if (!other_links) { 1011 if (dl->physical->type != PHYS_DEMAND) /* Not in -auto mode */ 1012 bundle_DownInterface(bundle); 1013 if (bundle->ncp.ipcp.fsm.state > ST_CLOSED || 1014 bundle->ncp.ipcp.fsm.state == ST_STARTING) { 1015 fsm_Down(&bundle->ncp.ipcp.fsm); 1016 fsm_Close(&bundle->ncp.ipcp.fsm); /* ST_INITIAL please */ 1017 } 1018 bundle_NewPhase(bundle, PHASE_DEAD); 1019 bundle_StopIdleTimer(bundle); 1020 bundle_StopAutoLoadTimer(bundle); 1021 bundle->autoload.running = 0; 1022 } else 1023 bundle->autoload.running = 1; 1024} 1025 1026void 1027bundle_Open(struct bundle *bundle, const char *name, int mask) 1028{ 1029 /* 1030 * Please open the given datalink, or all if name == NULL 1031 */ 1032 struct datalink *dl; 1033 1034 timer_Stop(&bundle->autoload.timer); 1035 for (dl = bundle->links; dl; dl = dl->next) 1036 if (name == NULL || !strcasecmp(dl->name, name)) { 1037 if (dl->state == DATALINK_CLOSED && (mask & dl->physical->type)) { 1038 datalink_Up(dl, 1, 1); 1039 if (mask == PHYS_DEMAND) 1040 /* Only one DEMAND link at a time (see the AutoLoad timer) */ 1041 break; 1042 } 1043 if (name != NULL) 1044 break; 1045 } 1046} 1047 1048struct datalink * 1049bundle2datalink(struct bundle *bundle, const char *name) 1050{ 1051 struct datalink *dl; 1052 1053 if (name != NULL) { 1054 for (dl = bundle->links; dl; dl = dl->next) 1055 if (!strcasecmp(dl->name, name)) 1056 return dl; 1057 } else if (bundle->links && !bundle->links->next) 1058 return bundle->links; 1059 1060 return NULL; 1061} 1062 1063int 1064bundle_FillQueues(struct bundle *bundle) 1065{ 1066 int total; 1067 1068 if (bundle->ncp.mp.active) 1069 total = mp_FillQueues(bundle); 1070 else { 1071 struct datalink *dl; 1072 int add; 1073 1074 for (total = 0, dl = bundle->links; dl; dl = dl->next) 1075 if (dl->state == DATALINK_OPEN) { 1076 add = link_QueueLen(&dl->physical->link); 1077 if (add == 0 && dl->physical->out == NULL) 1078 add = ip_FlushPacket(&dl->physical->link, bundle); 1079 total += add; 1080 } 1081 } 1082 1083 return total + ip_QueueLen(); 1084} 1085 1086int 1087bundle_ShowLinks(struct cmdargs const *arg) 1088{ 1089 struct datalink *dl; 1090 1091 for (dl = arg->bundle->links; dl; dl = dl->next) { 1092 prompt_Printf(arg->prompt, "Name: %s [%s, %s]", 1093 dl->name, mode2Nam(dl->physical->type), datalink_State(dl)); 1094 if (dl->physical->link.throughput.rolling && dl->state == DATALINK_OPEN) 1095 prompt_Printf(arg->prompt, " weight %d, %d bytes/sec", 1096 dl->mp.weight, 1097 dl->physical->link.throughput.OctetsPerSecond); 1098 prompt_Printf(arg->prompt, "\n"); 1099 } 1100 1101 return 0; 1102} 1103 1104static const char * 1105optval(struct bundle *bundle, int bit) 1106{ 1107 return (bundle->cfg.opt & bit) ? "enabled" : "disabled"; 1108} 1109 1110int 1111bundle_ShowStatus(struct cmdargs const *arg) 1112{ 1113 int remaining; 1114 1115 prompt_Printf(arg->prompt, "Phase %s\n", bundle_PhaseName(arg->bundle)); 1116 prompt_Printf(arg->prompt, " Device: %s\n", arg->bundle->dev.Name); 1117 prompt_Printf(arg->prompt, " Interface: %s @ %lubps\n", 1118 arg->bundle->ifp.Name, arg->bundle->ifp.Speed); 1119 1120 prompt_Printf(arg->prompt, "\nDefaults:\n"); 1121 prompt_Printf(arg->prompt, " Label: %s\n", arg->bundle->cfg.label); 1122 prompt_Printf(arg->prompt, " Auth name: %s\n", 1123 arg->bundle->cfg.auth.name); 1124 prompt_Printf(arg->prompt, " Auto Load: Up after %ds of >= %d packets\n", 1125 arg->bundle->cfg.autoload.max.timeout, 1126 arg->bundle->cfg.autoload.max.packets); 1127 prompt_Printf(arg->prompt, " Down after %ds of <= %d" 1128 " packets\n", arg->bundle->cfg.autoload.min.timeout, 1129 arg->bundle->cfg.autoload.min.packets); 1130 if (arg->bundle->autoload.timer.state == TIMER_RUNNING) 1131 prompt_Printf(arg->prompt, " %ds remaining 'till " 1132 "a link comes %s\n", 1133 bundle_RemainingAutoLoadTime(arg->bundle), 1134 arg->bundle->autoload.comingup ? "up" : "down"); 1135 else 1136 prompt_Printf(arg->prompt, " %srunning with %d" 1137 " packets queued\n", arg->bundle->autoload.running ? 1138 "" : "not ", ip_QueueLen()); 1139 1140 prompt_Printf(arg->prompt, " Idle Timer: "); 1141 if (arg->bundle->cfg.idle_timeout) { 1142 prompt_Printf(arg->prompt, "%ds", arg->bundle->cfg.idle_timeout); 1143 remaining = bundle_RemainingIdleTime(arg->bundle); 1144 if (remaining != -1) 1145 prompt_Printf(arg->prompt, " (%ds remaining)", remaining); 1146 prompt_Printf(arg->prompt, "\n"); 1147 } else 1148 prompt_Printf(arg->prompt, "disabled\n"); 1149 prompt_Printf(arg->prompt, " MTU: "); 1150 if (arg->bundle->cfg.mtu) 1151 prompt_Printf(arg->prompt, "%d\n", arg->bundle->cfg.mtu); 1152 else 1153 prompt_Printf(arg->prompt, "unspecified\n"); 1154 1155 prompt_Printf(arg->prompt, " Sticky Routes: %s\n", 1156 optval(arg->bundle, OPT_SROUTES)); 1157 prompt_Printf(arg->prompt, " ID check: %s\n", 1158 optval(arg->bundle, OPT_IDCHECK)); 1159 prompt_Printf(arg->prompt, " Loopback: %s\n", 1160 optval(arg->bundle, OPT_LOOPBACK)); 1161 prompt_Printf(arg->prompt, " PasswdAuth: %s\n", 1162 optval(arg->bundle, OPT_PASSWDAUTH)); 1163 prompt_Printf(arg->prompt, " Proxy: %s\n", 1164 optval(arg->bundle, OPT_PROXY)); 1165 prompt_Printf(arg->prompt, " Throughput: %s\n", 1166 optval(arg->bundle, OPT_THROUGHPUT)); 1167 prompt_Printf(arg->prompt, " Utmp Logging: %s\n", 1168 optval(arg->bundle, OPT_UTMP)); 1169 1170 return 0; 1171} 1172 1173static void 1174bundle_IdleTimeout(void *v) 1175{ 1176 struct bundle *bundle = (struct bundle *)v; 1177 1178 log_Printf(LogPHASE, "Idle timer expired.\n"); 1179 bundle_StopIdleTimer(bundle); 1180 bundle_Close(bundle, NULL, 1); 1181} 1182 1183/* 1184 * Start Idle timer. If timeout is reached, we call bundle_Close() to 1185 * close LCP and link. 1186 */ 1187void 1188bundle_StartIdleTimer(struct bundle *bundle) 1189{ 1190 timer_Stop(&bundle->idle.timer); 1191 if ((bundle->phys_type & (PHYS_DEDICATED|PHYS_PERM)) != bundle->phys_type && 1192 bundle->cfg.idle_timeout) { 1193 bundle->idle.timer.func = bundle_IdleTimeout; 1194 bundle->idle.timer.name = "idle"; 1195 bundle->idle.timer.load = bundle->cfg.idle_timeout * SECTICKS; 1196 bundle->idle.timer.arg = bundle; 1197 timer_Start(&bundle->idle.timer); 1198 bundle->idle.done = time(NULL) + bundle->cfg.idle_timeout; 1199 } 1200} 1201 1202void 1203bundle_SetIdleTimer(struct bundle *bundle, int value) 1204{ 1205 bundle->cfg.idle_timeout = value; 1206 if (bundle_LinkIsUp(bundle)) 1207 bundle_StartIdleTimer(bundle); 1208} 1209 1210void 1211bundle_StopIdleTimer(struct bundle *bundle) 1212{ 1213 timer_Stop(&bundle->idle.timer); 1214 bundle->idle.done = 0; 1215} 1216 1217static int 1218bundle_RemainingIdleTime(struct bundle *bundle) 1219{ 1220 if (bundle->idle.done) 1221 return bundle->idle.done - time(NULL); 1222 return -1; 1223} 1224 1225int 1226bundle_IsDead(struct bundle *bundle) 1227{ 1228 return !bundle->links || (bundle->phase == PHASE_DEAD && bundle->CleaningUp); 1229} 1230 1231static void 1232bundle_LinkAdded(struct bundle *bundle, struct datalink *dl) 1233{ 1234 bundle->phys_type |= dl->physical->type; 1235 if (dl->physical->type == PHYS_DEMAND && 1236 bundle->autoload.timer.state == TIMER_STOPPED && 1237 bundle->phase == PHASE_NETWORK) 1238 bundle->autoload.running = 1; 1239} 1240 1241static void 1242bundle_LinksRemoved(struct bundle *bundle) 1243{ 1244 struct datalink *dl; 1245 1246 bundle->phys_type = 0; 1247 for (dl = bundle->links; dl; dl = dl->next) 1248 bundle_LinkAdded(bundle, dl); 1249 1250 if ((bundle->phys_type & (PHYS_DEDICATED|PHYS_PERM)) == bundle->phys_type) 1251 timer_Stop(&bundle->idle.timer); 1252} 1253 1254static struct datalink * 1255bundle_DatalinkLinkout(struct bundle *bundle, struct datalink *dl) 1256{ 1257 struct datalink **dlp; 1258 1259 for (dlp = &bundle->links; *dlp; dlp = &(*dlp)->next) 1260 if (*dlp == dl) { 1261 *dlp = dl->next; 1262 dl->next = NULL; 1263 bundle_LinksRemoved(bundle); 1264 return dl; 1265 } 1266 1267 return NULL; 1268} 1269 1270static void 1271bundle_DatalinkLinkin(struct bundle *bundle, struct datalink *dl) 1272{ 1273 struct datalink **dlp = &bundle->links; 1274 1275 while (*dlp) 1276 dlp = &(*dlp)->next; 1277 1278 *dlp = dl; 1279 dl->next = NULL; 1280 1281 bundle_LinkAdded(bundle, dl); 1282} 1283 1284void 1285bundle_CleanDatalinks(struct bundle *bundle) 1286{ 1287 struct datalink **dlp = &bundle->links; 1288 int found = 0; 1289 1290 while (*dlp) 1291 if ((*dlp)->state == DATALINK_CLOSED && 1292 (*dlp)->physical->type & (PHYS_DIRECT|PHYS_1OFF)) { 1293 *dlp = datalink_Destroy(*dlp); 1294 found++; 1295 } else 1296 dlp = &(*dlp)->next; 1297 1298 if (found) 1299 bundle_LinksRemoved(bundle); 1300} 1301 1302int 1303bundle_DatalinkClone(struct bundle *bundle, struct datalink *dl, 1304 const char *name) 1305{ 1306 if (bundle2datalink(bundle, name)) { 1307 log_Printf(LogWARN, "Clone: %s: name already exists\n", name); 1308 return 0; 1309 } 1310 1311 bundle_DatalinkLinkin(bundle, datalink_Clone(dl, name)); 1312 return 1; 1313} 1314 1315void 1316bundle_DatalinkRemove(struct bundle *bundle, struct datalink *dl) 1317{ 1318 dl = bundle_DatalinkLinkout(bundle, dl); 1319 if (dl) 1320 datalink_Destroy(dl); 1321} 1322 1323void 1324bundle_SetLabel(struct bundle *bundle, const char *label) 1325{ 1326 if (label) 1327 strncpy(bundle->cfg.label, label, sizeof bundle->cfg.label - 1); 1328 else 1329 *bundle->cfg.label = '\0'; 1330} 1331 1332const char * 1333bundle_GetLabel(struct bundle *bundle) 1334{ 1335 return *bundle->cfg.label ? bundle->cfg.label : NULL; 1336} 1337 1338void 1339bundle_ReceiveDatalink(struct bundle *bundle, int s, struct sockaddr_un *sun) 1340{ 1341 char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int)]; 1342 struct cmsghdr *cmsg = (struct cmsghdr *)cmsgbuf; 1343 struct msghdr msg; 1344 struct iovec iov[SCATTER_SEGMENTS]; 1345 struct datalink *dl; 1346 int niov, link_fd, expect, f; 1347 1348 log_Printf(LogPHASE, "Receiving datalink\n"); 1349 1350 /* Create our scatter/gather array */ 1351 niov = 1; 1352 iov[0].iov_len = strlen(Version) + 1; 1353 iov[0].iov_base = (char *)malloc(iov[0].iov_len); 1354 if (datalink2iov(NULL, iov, &niov, sizeof iov / sizeof *iov) == -1) { 1355 close(s); 1356 return; 1357 } 1358 1359 for (f = expect = 0; f < niov; f++) 1360 expect += iov[f].iov_len; 1361 1362 /* Set up our message */ 1363 cmsg->cmsg_len = sizeof cmsgbuf; 1364 cmsg->cmsg_level = SOL_SOCKET; 1365 cmsg->cmsg_type = 0; 1366 1367 memset(&msg, '\0', sizeof msg); 1368 msg.msg_name = (caddr_t)sun; 1369 msg.msg_namelen = sizeof *sun; 1370 msg.msg_iov = iov; 1371 msg.msg_iovlen = niov; 1372 msg.msg_control = cmsgbuf; 1373 msg.msg_controllen = sizeof cmsgbuf; 1374 1375 log_Printf(LogDEBUG, "Expecting %d scatter/gather bytes\n", expect); 1376 f = expect + 100; 1377 setsockopt(s, SOL_SOCKET, SO_RCVBUF, &f, sizeof f); 1378 if ((f = recvmsg(s, &msg, MSG_WAITALL)) != expect) { 1379 if (f == -1) 1380 log_Printf(LogERROR, "Failed recvmsg: %s\n", strerror(errno)); 1381 else 1382 log_Printf(LogERROR, "Failed recvmsg: Got %d, not %d\n", f, expect); 1383 while (niov--) 1384 free(iov[niov].iov_base); 1385 close(s); 1386 return; 1387 } 1388 1389 write(s, "!", 1); /* ACK */ 1390 1391 if (cmsg->cmsg_type == SCM_RIGHTS) { 1392 /* We've successfully received an open file descriptor through our socket */ 1393 log_Printf(LogDEBUG, "Receiving non-tty device\n"); 1394 link_fd = *(int *)CMSG_DATA(cmsg); 1395 } else { 1396 /* It's a ``controlling'' tty device via CATPROG */ 1397 log_Printf(LogDEBUG, "Receiving tty device\n"); 1398 link_fd = dup(s); 1399 fcntl(link_fd, F_SETFL, fcntl(link_fd, F_GETFL, 0) | O_NONBLOCK); 1400 } 1401 1402 if (strncmp(Version, iov[0].iov_base, iov[0].iov_len)) { 1403 log_Printf(LogWARN, "Cannot receive datalink, incorrect version" 1404 " (\"%.*s\", not \"%s\")\n", (int)iov[0].iov_len, 1405 iov[0].iov_base, Version); 1406 close(link_fd); 1407 while (niov--) 1408 free(iov[niov].iov_base); 1409 return; 1410 } 1411 1412 niov = 1; 1413 dl = iov2datalink(bundle, iov, &niov, sizeof iov / sizeof *iov, link_fd); 1414 if (dl) { 1415 bundle_DatalinkLinkin(bundle, dl); 1416 datalink_AuthOk(dl); 1417 } else 1418 close(link_fd); 1419 1420 free(iov[0].iov_base); 1421 close(s); 1422} 1423 1424void 1425bundle_SendDatalink(struct datalink *dl, int s, struct sockaddr_un *sun) 1426{ 1427 char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int)], ack; 1428 struct cmsghdr *cmsg = (struct cmsghdr *)cmsgbuf; 1429 struct msghdr msg; 1430 struct iovec iov[SCATTER_SEGMENTS]; 1431 int niov, link_fd, f, expect; 1432 1433 log_Printf(LogPHASE, "Transmitting datalink %s\n", dl->name); 1434 1435 bundle_LinkClosed(dl->bundle, dl); 1436 bundle_DatalinkLinkout(dl->bundle, dl); 1437 1438 /* Build our scatter/gather array */ 1439 iov[0].iov_len = strlen(Version) + 1; 1440 iov[0].iov_base = strdup(Version); 1441 niov = 1; 1442 1443 link_fd = datalink2iov(dl, iov, &niov, sizeof iov / sizeof *iov); 1444 1445 if (link_fd != -1) { 1446 memset(&msg, '\0', sizeof msg); 1447 1448 msg.msg_name = (caddr_t)sun; 1449 msg.msg_namelen = sizeof *sun; 1450 msg.msg_iov = iov; 1451 msg.msg_iovlen = niov; 1452 1453 if (tcgetpgrp(link_fd) == getpgrp()) { 1454 /* 1455 * We can't transfer this tty descriptor. If we do, then once the 1456 * session leader exits, the descriptor becomes unusable by the 1457 * other ppp process. Instead, we'll fork() two `/bin/cat' 1458 * processes..... 1459 */ 1460 msg.msg_control = NULL; 1461 msg.msg_controllen = 0; 1462 } else { 1463 cmsg->cmsg_len = sizeof cmsgbuf; 1464 cmsg->cmsg_level = SOL_SOCKET; 1465 cmsg->cmsg_type = SCM_RIGHTS; 1466 *(int *)CMSG_DATA(cmsg) = link_fd; 1467 msg.msg_control = cmsgbuf; 1468 msg.msg_controllen = sizeof cmsgbuf; 1469 } 1470 1471 for (f = expect = 0; f < niov; f++) 1472 expect += iov[f].iov_len; 1473 1474 log_Printf(LogDEBUG, "Sending %d bytes in scatter/gather array\n", expect); 1475 1476 f = expect + SOCKET_OVERHEAD; 1477 setsockopt(s, SOL_SOCKET, SO_SNDBUF, &f, sizeof f); 1478 if (sendmsg(s, &msg, 0) == -1) 1479 log_Printf(LogERROR, "Failed sendmsg: %s\n", strerror(errno)); 1480 /* We must get the ACK before closing the descriptor ! */ 1481 read(s, &ack, 1); 1482 1483 if (tcgetpgrp(link_fd) == getpgrp()) { 1484 /* We use `/bin/cat' to keep the tty session id */ 1485 pid_t pid; 1486 int status, len, fd; 1487 char name[50], *tname; 1488 1489 tname = ttyname(link_fd); 1490 len = strlen(_PATH_DEV); 1491 if (!strncmp(tname, _PATH_DEV, len)) 1492 tname += len; 1493 1494 log_Printf(LogPHASE, "%s: Using twin %s invocations\n", tname, CATPROG); 1495 1496 switch ((pid = fork())) { 1497 case -1: 1498 log_Printf(LogERROR, "fork: %s\n", strerror(errno)); 1499 break; 1500 case 0: 1501 if (fork()) /* Don't want to belong to the parent any more */ 1502 exit(0); 1503 setsid(); 1504 log_Printf(LogPHASE, "%d: Continuing without controlling terminal\n", 1505 (int)getpid()); 1506 break; 1507 default: 1508 /* Parent does the execs .... */ 1509 timer_TermService(); 1510 waitpid(pid, &status, 0); 1511 1512 fcntl(3, F_SETFD, 1); /* Set close-on-exec flag */ 1513 fcntl(s, F_SETFL, fcntl(s, F_GETFL, 0) & ~O_NONBLOCK); 1514 fcntl(link_fd, F_SETFL, fcntl(link_fd, F_GETFL, 0) & ~O_NONBLOCK); 1515 s = fcntl(s, F_DUPFD, 3); 1516 link_fd = fcntl(link_fd, F_DUPFD, 3); 1517 dup2(open(_PATH_DEVNULL, O_WRONLY|O_APPEND), STDERR_FILENO); 1518 1519 setuid(geteuid()); 1520 1521 switch (fork()) { 1522 case -1: 1523 _exit(0); 1524 break; 1525 case 0: 1526 dup2(link_fd, STDIN_FILENO); 1527 dup2(s, STDOUT_FILENO); 1528 snprintf(name, sizeof name, "%s <- %s", dl->name, tname); 1529 break; 1530 default: 1531 dup2(s, STDIN_FILENO); 1532 dup2(link_fd, STDOUT_FILENO); 1533 snprintf(name, sizeof name, "%s -> %s", dl->name, tname); 1534 break; 1535 } 1536 signal(SIGPIPE, SIG_DFL); 1537 signal(SIGALRM, SIG_DFL); 1538 for (fd = getdtablesize(); fd > 2; fd--) 1539 close(fd); 1540 execl(CATPROG, name, NULL); 1541 _exit(0); 1542 break; 1543 } 1544 } 1545 close(s); 1546 close(link_fd); 1547 } 1548 1549 while (niov--) 1550 free(iov[niov].iov_base); 1551} 1552 1553int 1554bundle_RenameDatalink(struct bundle *bundle, struct datalink *ndl, 1555 const char *name) 1556{ 1557 struct datalink *dl; 1558 1559 if (!strcasecmp(ndl->name, name)) 1560 return 1; 1561 1562 for (dl = bundle->links; dl; dl = dl->next) 1563 if (!strcasecmp(dl->name, name)) 1564 return 0; 1565 1566 datalink_Rename(ndl, name); 1567 return 1; 1568} 1569 1570int 1571bundle_SetMode(struct bundle *bundle, struct datalink *dl, int mode) 1572{ 1573 int omode; 1574 1575 omode = dl->physical->type; 1576 if (omode == mode) 1577 return 1; 1578 1579 if (mode == PHYS_DEMAND && !(bundle->phys_type & PHYS_DEMAND)) 1580 /* Changing to demand-dial mode */ 1581 if (bundle->ncp.ipcp.peer_ip.s_addr == INADDR_ANY) { 1582 log_Printf(LogWARN, "You must `set ifaddr' before changing mode to %s\n", 1583 mode2Nam(mode)); 1584 return 0; 1585 } 1586 1587 if (!datalink_SetMode(dl, mode)) 1588 return 0; 1589 1590 if (mode == PHYS_DEMAND && !(bundle->phys_type & PHYS_DEMAND)) 1591 ipcp_InterfaceUp(&bundle->ncp.ipcp); 1592 1593 /* Regenerate phys_type and adjust autoload & idle timers */ 1594 bundle_LinksRemoved(bundle); 1595 1596 if (omode == PHYS_DEMAND && !(bundle->phys_type & PHYS_DEMAND)) 1597 /* Changing from demand-dial mode */ 1598 ipcp_CleanInterface(&bundle->ncp.ipcp); 1599 1600 return 1; 1601} 1602