17a6f8720SBrian Somers /*- 27a6f8720SBrian Somers * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org> 37a6f8720SBrian Somers * All rights reserved. 47a6f8720SBrian Somers * 57a6f8720SBrian Somers * Redistribution and use in source and binary forms, with or without 67a6f8720SBrian Somers * modification, are permitted provided that the following conditions 77a6f8720SBrian Somers * are met: 87a6f8720SBrian Somers * 1. Redistributions of source code must retain the above copyright 97a6f8720SBrian Somers * notice, this list of conditions and the following disclaimer. 107a6f8720SBrian Somers * 2. Redistributions in binary form must reproduce the above copyright 117a6f8720SBrian Somers * notice, this list of conditions and the following disclaimer in the 127a6f8720SBrian Somers * documentation and/or other materials provided with the distribution. 137a6f8720SBrian Somers * 147a6f8720SBrian Somers * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 157a6f8720SBrian Somers * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 167a6f8720SBrian Somers * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 177a6f8720SBrian Somers * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 187a6f8720SBrian Somers * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 197a6f8720SBrian Somers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 207a6f8720SBrian Somers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 217a6f8720SBrian Somers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 227a6f8720SBrian Somers * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 237a6f8720SBrian Somers * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 247a6f8720SBrian Somers * SUCH DAMAGE. 257a6f8720SBrian Somers * 26991c2a7bSBrian Somers * $Id: bundle.c,v 1.40 1998/10/29 23:47:10 brian Exp $ 277a6f8720SBrian Somers */ 287a6f8720SBrian Somers 291384bd27SBrian Somers #include <sys/param.h> 307a6f8720SBrian Somers #include <sys/socket.h> 317a6f8720SBrian Somers #include <netinet/in.h> 327a6f8720SBrian Somers #include <net/if.h> 337a6f8720SBrian Somers #include <arpa/inet.h> 347a6f8720SBrian Somers #include <net/route.h> 357a6f8720SBrian Somers #include <net/if_dl.h> 36eaa4df37SBrian Somers #include <netinet/in_systm.h> 37eaa4df37SBrian Somers #include <netinet/ip.h> 381fa665f5SBrian Somers #include <sys/un.h> 397a6f8720SBrian Somers 407a6f8720SBrian Somers #include <errno.h> 417a6f8720SBrian Somers #include <fcntl.h> 4247723d29SBrian Somers #include <paths.h> 437a6f8720SBrian Somers #include <stdio.h> 446f384573SBrian Somers #include <stdlib.h> 457a6f8720SBrian Somers #include <string.h> 467a6f8720SBrian Somers #include <sys/ioctl.h> 4796c9bb21SBrian Somers #include <sys/uio.h> 4854cd8e13SBrian Somers #include <sys/wait.h> 497a6f8720SBrian Somers #include <termios.h> 507a6f8720SBrian Somers #include <unistd.h> 517a6f8720SBrian Somers 521595bacdSBrian Somers #ifndef NOALIAS 531595bacdSBrian Somers #ifdef __OpenBSD__ 541595bacdSBrian Somers #include "alias.h" 551595bacdSBrian Somers #else 561595bacdSBrian Somers #include <alias.h> 571595bacdSBrian Somers #endif 581595bacdSBrian Somers #endif 59c9e11a11SBrian Somers #include "defs.h" 607a6f8720SBrian Somers #include "command.h" 617a6f8720SBrian Somers #include "mbuf.h" 627a6f8720SBrian Somers #include "log.h" 637a6f8720SBrian Somers #include "id.h" 647a6f8720SBrian Somers #include "timer.h" 657a6f8720SBrian Somers #include "fsm.h" 667a6f8720SBrian Somers #include "iplist.h" 67879ed6faSBrian Somers #include "lqr.h" 68455aabc3SBrian Somers #include "hdlc.h" 697a6f8720SBrian Somers #include "throughput.h" 70eaa4df37SBrian Somers #include "slcompress.h" 717a6f8720SBrian Somers #include "ipcp.h" 725ca5389aSBrian Somers #include "filter.h" 732f786681SBrian Somers #include "descriptor.h" 747a6f8720SBrian Somers #include "route.h" 757a6f8720SBrian Somers #include "lcp.h" 767a6f8720SBrian Somers #include "ccp.h" 773b0f8d2eSBrian Somers #include "link.h" 783b0f8d2eSBrian Somers #include "mp.h" 793b0f8d2eSBrian Somers #include "bundle.h" 80455aabc3SBrian Somers #include "async.h" 81455aabc3SBrian Somers #include "physical.h" 822289f246SBrian Somers #include "modem.h" 83455aabc3SBrian Somers #include "auth.h" 84455aabc3SBrian Somers #include "lcpproto.h" 85455aabc3SBrian Somers #include "chap.h" 86455aabc3SBrian Somers #include "tun.h" 8785b542cfSBrian Somers #include "prompt.h" 883006ec67SBrian Somers #include "chat.h" 8992b09558SBrian Somers #include "cbcp.h" 903006ec67SBrian Somers #include "datalink.h" 913006ec67SBrian Somers #include "ip.h" 928fa6ebe4SBrian Somers #include "iface.h" 937a6f8720SBrian Somers 9496c9bb21SBrian Somers #define SCATTER_SEGMENTS 4 /* version, datalink, name, physical */ 9596c9bb21SBrian Somers #define SOCKET_OVERHEAD 100 /* additional buffer space for large */ 9696c9bb21SBrian Somers /* {recv,send}msg() calls */ 9796c9bb21SBrian Somers 9804eaa58cSBrian Somers static int bundle_RemainingIdleTime(struct bundle *); 9904eaa58cSBrian Somers static int bundle_RemainingAutoLoadTime(struct bundle *); 10004eaa58cSBrian Somers 101455aabc3SBrian Somers static const char *PhaseNames[] = { 102455aabc3SBrian Somers "Dead", "Establish", "Authenticate", "Network", "Terminate" 103455aabc3SBrian Somers }; 104455aabc3SBrian Somers 105455aabc3SBrian Somers const char * 106455aabc3SBrian Somers bundle_PhaseName(struct bundle *bundle) 1077a6f8720SBrian Somers { 108455aabc3SBrian Somers return bundle->phase <= PHASE_TERMINATE ? 109455aabc3SBrian Somers PhaseNames[bundle->phase] : "unknown"; 1107a6f8720SBrian Somers } 1117a6f8720SBrian Somers 112455aabc3SBrian Somers void 1135563ebdeSBrian Somers bundle_NewPhase(struct bundle *bundle, u_int new) 114455aabc3SBrian Somers { 115aef795ccSBrian Somers if (new == bundle->phase) 116aef795ccSBrian Somers return; 117aef795ccSBrian Somers 118e2ebb036SBrian Somers if (new <= PHASE_TERMINATE) 119dd7e2610SBrian Somers log_Printf(LogPHASE, "bundle: %s\n", PhaseNames[new]); 1207a6f8720SBrian Somers 121455aabc3SBrian Somers switch (new) { 122455aabc3SBrian Somers case PHASE_DEAD: 1230f2f3eb3SBrian Somers log_DisplayPrompts(); 124455aabc3SBrian Somers bundle->phase = new; 125455aabc3SBrian Somers break; 126455aabc3SBrian Somers 127455aabc3SBrian Somers case PHASE_ESTABLISH: 128455aabc3SBrian Somers bundle->phase = new; 129455aabc3SBrian Somers break; 130455aabc3SBrian Somers 131455aabc3SBrian Somers case PHASE_AUTHENTICATE: 132455aabc3SBrian Somers bundle->phase = new; 1330f2f3eb3SBrian Somers log_DisplayPrompts(); 134455aabc3SBrian Somers break; 135455aabc3SBrian Somers 136455aabc3SBrian Somers case PHASE_NETWORK: 1375828db6dSBrian Somers ipcp_Setup(&bundle->ncp.ipcp); 138dd7e2610SBrian Somers fsm_Up(&bundle->ncp.ipcp.fsm); 139dd7e2610SBrian Somers fsm_Open(&bundle->ncp.ipcp.fsm); 140673903ecSBrian Somers bundle->phase = new; 1410f2f3eb3SBrian Somers log_DisplayPrompts(); 142673903ecSBrian Somers break; 143455aabc3SBrian Somers 144455aabc3SBrian Somers case PHASE_TERMINATE: 145455aabc3SBrian Somers bundle->phase = new; 146673903ecSBrian Somers mp_Down(&bundle->ncp.mp); 1470f2f3eb3SBrian Somers log_DisplayPrompts(); 148455aabc3SBrian Somers break; 1497a6f8720SBrian Somers } 1507a6f8720SBrian Somers } 1517a6f8720SBrian Somers 1526d666775SBrian Somers static void 1536d666775SBrian Somers bundle_LayerStart(void *v, struct fsm *fp) 1547a6f8720SBrian Somers { 1553006ec67SBrian Somers /* The given FSM is about to start up ! */ 1567a6f8720SBrian Somers } 1577a6f8720SBrian Somers 1585cf4388bSBrian Somers 1595cf4388bSBrian Somers static void 1605cf4388bSBrian Somers bundle_Notify(struct bundle *bundle, char c) 1615cf4388bSBrian Somers { 1625cf4388bSBrian Somers if (bundle->notify.fd != -1) { 1635cf4388bSBrian Somers if (write(bundle->notify.fd, &c, 1) == 1) 164dd7e2610SBrian Somers log_Printf(LogPHASE, "Parent notified of success.\n"); 1655cf4388bSBrian Somers else 166dd7e2610SBrian Somers log_Printf(LogPHASE, "Failed to notify parent of success.\n"); 1675cf4388bSBrian Somers close(bundle->notify.fd); 1685cf4388bSBrian Somers bundle->notify.fd = -1; 1695cf4388bSBrian Somers } 1705cf4388bSBrian Somers } 1713b0f8d2eSBrian Somers 1726d666775SBrian Somers static void 1736f8e9f0aSBrian Somers bundle_ClearQueues(void *v) 1746f8e9f0aSBrian Somers { 1756f8e9f0aSBrian Somers struct bundle *bundle = (struct bundle *)v; 1766f8e9f0aSBrian Somers struct datalink *dl; 1776f8e9f0aSBrian Somers 1786f8e9f0aSBrian Somers log_Printf(LogPHASE, "Clearing choked output queue\n"); 1796f8e9f0aSBrian Somers timer_Stop(&bundle->choked.timer); 1806f8e9f0aSBrian Somers 1816f8e9f0aSBrian Somers /* 1826f8e9f0aSBrian Somers * Emergency time: 1836f8e9f0aSBrian Somers * 1846f8e9f0aSBrian Somers * We've had a full queue for PACKET_DEL_SECS seconds without being 1856f8e9f0aSBrian Somers * able to get rid of any of the packets. We've probably given up 1866f8e9f0aSBrian Somers * on the redials at this point, and the queued data has almost 1876f8e9f0aSBrian Somers * definitely been timed out by the layer above. As this is preventing 1886f8e9f0aSBrian Somers * us from reading the TUN_NAME device (we don't want to buffer stuff 1896f8e9f0aSBrian Somers * indefinitely), we may as well nuke this data and start with a clean 1906f8e9f0aSBrian Somers * slate ! 1916f8e9f0aSBrian Somers * 1926f8e9f0aSBrian Somers * Unfortunately, this has the side effect of shafting any compression 1936f8e9f0aSBrian Somers * dictionaries in use (causing the relevant RESET_REQ/RESET_ACK). 1946f8e9f0aSBrian Somers */ 1956f8e9f0aSBrian Somers 1965a72b6edSBrian Somers ip_DeleteQueue(&bundle->ncp.ipcp); 1976f8e9f0aSBrian Somers mp_DeleteQueue(&bundle->ncp.mp); 1986f8e9f0aSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 1996f8e9f0aSBrian Somers physical_DeleteQueue(dl->physical); 2006f8e9f0aSBrian Somers } 2016f8e9f0aSBrian Somers 2026f8e9f0aSBrian Somers static void 20304eaa58cSBrian Somers bundle_AutoLoadTimeout(void *v) 20404eaa58cSBrian Somers { 20504eaa58cSBrian Somers struct bundle *bundle = (struct bundle *)v; 20604eaa58cSBrian Somers 20704eaa58cSBrian Somers if (bundle->autoload.comingup) { 20804eaa58cSBrian Somers log_Printf(LogPHASE, "autoload: Another link is required\n"); 20904eaa58cSBrian Somers /* bundle_Open() stops the timer */ 210ba23f397SBrian Somers bundle_Open(bundle, NULL, PHYS_AUTO, 0); 21104eaa58cSBrian Somers } else { 21204eaa58cSBrian Somers struct datalink *dl, *last; 21304eaa58cSBrian Somers 21404eaa58cSBrian Somers timer_Stop(&bundle->autoload.timer); 21504eaa58cSBrian Somers for (last = NULL, dl = bundle->links; dl; dl = dl->next) 21681358fa3SBrian Somers if (dl->physical->type == PHYS_AUTO && dl->state == DATALINK_OPEN) 21704eaa58cSBrian Somers last = dl; 21804eaa58cSBrian Somers 21904eaa58cSBrian Somers if (last) 2209c81b87dSBrian Somers datalink_Close(last, CLOSE_STAYDOWN); 22104eaa58cSBrian Somers } 22204eaa58cSBrian Somers } 22304eaa58cSBrian Somers 22404eaa58cSBrian Somers static void 22504eaa58cSBrian Somers bundle_StartAutoLoadTimer(struct bundle *bundle, int up) 22604eaa58cSBrian Somers { 22704eaa58cSBrian Somers struct datalink *dl; 22804eaa58cSBrian Somers 22904eaa58cSBrian Somers timer_Stop(&bundle->autoload.timer); 23004eaa58cSBrian Somers 23104eaa58cSBrian Somers if (bundle->CleaningUp || bundle->phase != PHASE_NETWORK) { 23204eaa58cSBrian Somers dl = NULL; 23304eaa58cSBrian Somers bundle->autoload.running = 0; 23404eaa58cSBrian Somers } else if (up) { 23504eaa58cSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 23681358fa3SBrian Somers if (dl->state == DATALINK_CLOSED && dl->physical->type == PHYS_AUTO) { 23704eaa58cSBrian Somers if (bundle->cfg.autoload.max.timeout) { 23804eaa58cSBrian Somers bundle->autoload.timer.func = bundle_AutoLoadTimeout; 23904eaa58cSBrian Somers bundle->autoload.timer.name = "autoload up"; 24004eaa58cSBrian Somers bundle->autoload.timer.load = 24104eaa58cSBrian Somers bundle->cfg.autoload.max.timeout * SECTICKS; 24204eaa58cSBrian Somers bundle->autoload.timer.arg = bundle; 24304eaa58cSBrian Somers timer_Start(&bundle->autoload.timer); 24404eaa58cSBrian Somers bundle->autoload.done = time(NULL) + bundle->cfg.autoload.max.timeout; 24504eaa58cSBrian Somers } else 24604eaa58cSBrian Somers bundle_AutoLoadTimeout(bundle); 24704eaa58cSBrian Somers break; 24804eaa58cSBrian Somers } 24904eaa58cSBrian Somers bundle->autoload.running = (dl || bundle->cfg.autoload.min.timeout) ? 1 : 0; 25004eaa58cSBrian Somers } else { 25104eaa58cSBrian Somers int nlinks; 25204eaa58cSBrian Somers struct datalink *adl; 25304eaa58cSBrian Somers 25404eaa58cSBrian Somers for (nlinks = 0, adl = NULL, dl = bundle->links; dl; dl = dl->next) 25504eaa58cSBrian Somers if (dl->state == DATALINK_OPEN) { 25681358fa3SBrian Somers if (dl->physical->type == PHYS_AUTO) 25704eaa58cSBrian Somers adl = dl; 25804eaa58cSBrian Somers if (++nlinks > 1 && adl) { 25904eaa58cSBrian Somers if (bundle->cfg.autoload.min.timeout) { 26004eaa58cSBrian Somers bundle->autoload.timer.func = bundle_AutoLoadTimeout; 26104eaa58cSBrian Somers bundle->autoload.timer.name = "autoload down"; 26204eaa58cSBrian Somers bundle->autoload.timer.load = 26304eaa58cSBrian Somers bundle->cfg.autoload.min.timeout * SECTICKS; 26404eaa58cSBrian Somers bundle->autoload.timer.arg = bundle; 26504eaa58cSBrian Somers timer_Start(&bundle->autoload.timer); 26604eaa58cSBrian Somers bundle->autoload.done = 26704eaa58cSBrian Somers time(NULL) + bundle->cfg.autoload.min.timeout; 26804eaa58cSBrian Somers } 26904eaa58cSBrian Somers break; 27004eaa58cSBrian Somers } 27104eaa58cSBrian Somers } 27204eaa58cSBrian Somers 27304eaa58cSBrian Somers bundle->autoload.running = 1; 27404eaa58cSBrian Somers } 27504eaa58cSBrian Somers 27604eaa58cSBrian Somers bundle->autoload.comingup = up ? 1 : 0; 27704eaa58cSBrian Somers } 27804eaa58cSBrian Somers 27904eaa58cSBrian Somers static void 28004eaa58cSBrian Somers bundle_StopAutoLoadTimer(struct bundle *bundle) 28104eaa58cSBrian Somers { 28204eaa58cSBrian Somers timer_Stop(&bundle->autoload.timer); 28304eaa58cSBrian Somers bundle->autoload.done = 0; 28404eaa58cSBrian Somers } 28504eaa58cSBrian Somers 28604eaa58cSBrian Somers static int 28704eaa58cSBrian Somers bundle_RemainingAutoLoadTime(struct bundle *bundle) 28804eaa58cSBrian Somers { 28904eaa58cSBrian Somers if (bundle->autoload.done) 29004eaa58cSBrian Somers return bundle->autoload.done - time(NULL); 29104eaa58cSBrian Somers return -1; 29204eaa58cSBrian Somers } 29304eaa58cSBrian Somers 294ff0f9439SBrian Somers static void 295ff0f9439SBrian Somers bundle_LinkAdded(struct bundle *bundle, struct datalink *dl) 296ff0f9439SBrian Somers { 297ff0f9439SBrian Somers bundle->phys_type.all |= dl->physical->type; 298ff0f9439SBrian Somers if (dl->state == DATALINK_OPEN) 299ff0f9439SBrian Somers bundle->phys_type.open |= dl->physical->type; 300ff0f9439SBrian Somers 301ff0f9439SBrian Somers /* Note: We only re-add links that are DATALINK_OPEN */ 302ff0f9439SBrian Somers if (dl->physical->type == PHYS_AUTO && 303ff0f9439SBrian Somers bundle->autoload.timer.state == TIMER_STOPPED && 304ff0f9439SBrian Somers dl->state != DATALINK_OPEN && 305ff0f9439SBrian Somers bundle->phase == PHASE_NETWORK) 306ff0f9439SBrian Somers bundle->autoload.running = 1; 307ff0f9439SBrian Somers 308ff0f9439SBrian Somers if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) 309ff0f9439SBrian Somers != bundle->phys_type.open && bundle->idle.timer.state == TIMER_STOPPED) 310ff0f9439SBrian Somers /* We may need to start our idle timer */ 311ff0f9439SBrian Somers bundle_StartIdleTimer(bundle); 312ff0f9439SBrian Somers } 313ff0f9439SBrian Somers 31492b09558SBrian Somers void 315ff0f9439SBrian Somers bundle_LinksRemoved(struct bundle *bundle) 316ff0f9439SBrian Somers { 317ff0f9439SBrian Somers struct datalink *dl; 318ff0f9439SBrian Somers 319ff0f9439SBrian Somers bundle->phys_type.all = bundle->phys_type.open = 0; 320ff0f9439SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 321ff0f9439SBrian Somers bundle_LinkAdded(bundle, dl); 322ff0f9439SBrian Somers 323ff0f9439SBrian Somers if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) 324ff0f9439SBrian Somers == bundle->phys_type.open) 325ff0f9439SBrian Somers bundle_StopIdleTimer(bundle); 326ff0f9439SBrian Somers } 32704eaa58cSBrian Somers 32804eaa58cSBrian Somers static void 3296f384573SBrian Somers bundle_LayerUp(void *v, struct fsm *fp) 3307a6f8720SBrian Somers { 3313006ec67SBrian Somers /* 3323006ec67SBrian Somers * The given fsm is now up 333ff0f9439SBrian Somers * If it's an LCP, adjust our phys_mode.open value. 33449052c95SBrian Somers * If it's an LCP set our mtu (if we're multilink, add up the link 33504eaa58cSBrian Somers * speeds and set the MRRU) and start our autoload timer. 336565e35e5SBrian Somers * If it's an NCP, tell our -background parent to go away. 3373b0f8d2eSBrian Somers * If it's the first NCP, start the idle timer. 3383006ec67SBrian Somers */ 3396f384573SBrian Somers struct bundle *bundle = (struct bundle *)v; 3406d666775SBrian Somers 3415563ebdeSBrian Somers if (fp->proto == PROTO_LCP) { 342ff0f9439SBrian Somers struct physical *p = link2physical(fp->link); 343ff0f9439SBrian Somers 344ff0f9439SBrian Somers bundle_LinkAdded(bundle, p->dl); 3453b0f8d2eSBrian Somers if (bundle->ncp.mp.active) { 3463b0f8d2eSBrian Somers struct datalink *dl; 3475563ebdeSBrian Somers 3488fa6ebe4SBrian Somers bundle->ifSpeed = 0; 349faefde08SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 350eeab6bf5SBrian Somers if (dl->state == DATALINK_OPEN) 3518fa6ebe4SBrian Somers bundle->ifSpeed += modem_Speed(dl->physical); 352faefde08SBrian Somers tun_configure(bundle, bundle->ncp.mp.peer_mrru); 35304eaa58cSBrian Somers bundle->autoload.running = 1; 354faefde08SBrian Somers } else { 3558fa6ebe4SBrian Somers bundle->ifSpeed = modem_Speed(p); 356faefde08SBrian Somers tun_configure(bundle, fsm2lcp(fp)->his_mru); 357faefde08SBrian Somers } 3583b0f8d2eSBrian Somers } else if (fp->proto == PROTO_IPCP) { 359ab886ad0SBrian Somers bundle_StartIdleTimer(bundle); 3605cf4388bSBrian Somers bundle_Notify(bundle, EX_NORMAL); 3617a6f8720SBrian Somers } 362ab886ad0SBrian Somers } 3637a6f8720SBrian Somers 3646d666775SBrian Somers static void 3656d666775SBrian Somers bundle_LayerDown(void *v, struct fsm *fp) 3666d666775SBrian Somers { 3676d666775SBrian Somers /* 3686d666775SBrian Somers * The given FSM has been told to come down. 369ab886ad0SBrian Somers * If it's our last NCP, stop the idle timer. 370ff0f9439SBrian Somers * If it's an LCP, adjust our phys_type.open value and any timers. 37186b1f0d7SBrian Somers * If it's an LCP and we're in multilink mode, adjust our tun 37286b1f0d7SBrian Somers * speed and make sure our minimum sequence number is adjusted. 3736d666775SBrian Somers */ 374ab886ad0SBrian Somers 375ab886ad0SBrian Somers struct bundle *bundle = (struct bundle *)v; 376ab886ad0SBrian Somers 377078c562eSBrian Somers if (fp->proto == PROTO_IPCP) 378ab886ad0SBrian Somers bundle_StopIdleTimer(bundle); 379ff0f9439SBrian Somers else if (fp->proto == PROTO_LCP) { 380ff0f9439SBrian Somers bundle_LinksRemoved(bundle); /* adjust timers & phys_type values */ 381ff0f9439SBrian Somers if (bundle->ncp.mp.active) { 3823b0f8d2eSBrian Somers struct datalink *dl; 38386b1f0d7SBrian Somers struct datalink *lost; 3843b0f8d2eSBrian Somers 3858fa6ebe4SBrian Somers bundle->ifSpeed = 0; 38686b1f0d7SBrian Somers lost = NULL; 387faefde08SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 38886b1f0d7SBrian Somers if (fp == &dl->physical->link.lcp.fsm) 38986b1f0d7SBrian Somers lost = dl; 39086b1f0d7SBrian Somers else if (dl->state == DATALINK_OPEN) 3918fa6ebe4SBrian Somers bundle->ifSpeed += modem_Speed(dl->physical); 39286b1f0d7SBrian Somers 3938fa6ebe4SBrian Somers if (bundle->ifSpeed) 394faefde08SBrian Somers /* Don't configure down to a speed of 0 */ 395faefde08SBrian Somers tun_configure(bundle, bundle->ncp.mp.link.lcp.his_mru); 39686b1f0d7SBrian Somers 39786b1f0d7SBrian Somers if (lost) 39886b1f0d7SBrian Somers mp_LinkLost(&bundle->ncp.mp, lost); 39986b1f0d7SBrian Somers else 400a33b2ef7SBrian Somers log_Printf(LogALERT, "Oops, lost an unrecognised datalink (%s) !\n", 40186b1f0d7SBrian Somers fp->link->name); 4023b0f8d2eSBrian Somers } 4036d666775SBrian Somers } 404ff0f9439SBrian Somers } 4056d666775SBrian Somers 4066d666775SBrian Somers static void 4076d666775SBrian Somers bundle_LayerFinish(void *v, struct fsm *fp) 4086d666775SBrian Somers { 4096d666775SBrian Somers /* The given fsm is now down (fp cannot be NULL) 4106d666775SBrian Somers * 411dd7e2610SBrian Somers * If it's the last LCP, fsm_Down all NCPs 412dd7e2610SBrian Somers * If it's the last NCP, fsm_Close all LCPs 4136d666775SBrian Somers */ 4146d666775SBrian Somers 4156d666775SBrian Somers struct bundle *bundle = (struct bundle *)v; 4166d666775SBrian Somers struct datalink *dl; 4176d666775SBrian Somers 4183b0f8d2eSBrian Somers if (fp->proto == PROTO_IPCP) { 41926afeaa2SBrian Somers if (bundle_Phase(bundle) != PHASE_DEAD) 42025092092SBrian Somers bundle_NewPhase(bundle, PHASE_TERMINATE); 4216d666775SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 4229c81b87dSBrian Somers datalink_Close(dl, CLOSE_NORMAL); 42309206a6fSBrian Somers fsm2initial(fp); 4243b0f8d2eSBrian Somers } else if (fp->proto == PROTO_LCP) { 4253b0f8d2eSBrian Somers int others_active; 426a611cad6SBrian Somers 4273b0f8d2eSBrian Somers others_active = 0; 4283b0f8d2eSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 4293b0f8d2eSBrian Somers if (fp != &dl->physical->link.lcp.fsm && 4303b0f8d2eSBrian Somers dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) 4313b0f8d2eSBrian Somers others_active++; 4323b0f8d2eSBrian Somers 43309206a6fSBrian Somers if (!others_active) 43409206a6fSBrian Somers fsm2initial(&bundle->ncp.ipcp.fsm); 4353b0f8d2eSBrian Somers } 4363b0f8d2eSBrian Somers } 4376d666775SBrian Somers 4387a6f8720SBrian Somers int 4397a6f8720SBrian Somers bundle_LinkIsUp(const struct bundle *bundle) 4407a6f8720SBrian Somers { 4415828db6dSBrian Somers return bundle->ncp.ipcp.fsm.state == ST_OPENED; 4427a6f8720SBrian Somers } 4437a6f8720SBrian Somers 4447a6f8720SBrian Somers void 4459c81b87dSBrian Somers bundle_Close(struct bundle *bundle, const char *name, int how) 4467a6f8720SBrian Somers { 447455aabc3SBrian Somers /* 4483006ec67SBrian Somers * Please close the given datalink. 449dd7e2610SBrian Somers * If name == NULL or name is the last datalink, fsm_Close all NCPs 4506f384573SBrian Somers * (except our MP) 4513b0f8d2eSBrian Somers * If it isn't the last datalink, just Close that datalink. 452455aabc3SBrian Somers */ 4537a6f8720SBrian Somers 4543b0f8d2eSBrian Somers struct datalink *dl, *this_dl; 4553b0f8d2eSBrian Somers int others_active; 4563006ec67SBrian Somers 4573b0f8d2eSBrian Somers others_active = 0; 4583b0f8d2eSBrian Somers this_dl = NULL; 4593b0f8d2eSBrian Somers 4603b0f8d2eSBrian Somers for (dl = bundle->links; dl; dl = dl->next) { 4613b0f8d2eSBrian Somers if (name && !strcasecmp(name, dl->name)) 4623b0f8d2eSBrian Somers this_dl = dl; 4633b0f8d2eSBrian Somers if (name == NULL || this_dl == dl) { 4649c81b87dSBrian Somers switch (how) { 4659c81b87dSBrian Somers case CLOSE_LCP: 4669c81b87dSBrian Somers datalink_DontHangup(dl); 4679c81b87dSBrian Somers /* fall through */ 4689c81b87dSBrian Somers case CLOSE_STAYDOWN: 4693006ec67SBrian Somers datalink_StayDown(dl); 4709c81b87dSBrian Somers break; 4719c81b87dSBrian Somers } 4723b0f8d2eSBrian Somers } else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) 4733b0f8d2eSBrian Somers others_active++; 4743b0f8d2eSBrian Somers } 4753b0f8d2eSBrian Somers 4763b0f8d2eSBrian Somers if (name && this_dl == NULL) { 477dd7e2610SBrian Somers log_Printf(LogWARN, "%s: Invalid datalink name\n", name); 4783b0f8d2eSBrian Somers return; 4793b0f8d2eSBrian Somers } 4803b0f8d2eSBrian Somers 4813b0f8d2eSBrian Somers if (!others_active) { 48204eaa58cSBrian Somers bundle_StopIdleTimer(bundle); 48304eaa58cSBrian Somers bundle_StopAutoLoadTimer(bundle); 4843b0f8d2eSBrian Somers if (bundle->ncp.ipcp.fsm.state > ST_CLOSED || 4853b0f8d2eSBrian Somers bundle->ncp.ipcp.fsm.state == ST_STARTING) 486dd7e2610SBrian Somers fsm_Close(&bundle->ncp.ipcp.fsm); 4873b0f8d2eSBrian Somers else { 48809206a6fSBrian Somers fsm2initial(&bundle->ncp.ipcp.fsm); 489d345321bSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 4909c81b87dSBrian Somers datalink_Close(dl, how); 4917a6f8720SBrian Somers } 4923b0f8d2eSBrian Somers } else if (this_dl && this_dl->state != DATALINK_CLOSED && 4933b0f8d2eSBrian Somers this_dl->state != DATALINK_HANGUP) 4949c81b87dSBrian Somers datalink_Close(this_dl, how); 495d2fd8d77SBrian Somers } 4967a6f8720SBrian Somers 4971bc9b5baSBrian Somers void 498899011c4SBrian Somers bundle_Down(struct bundle *bundle, int how) 4991bc9b5baSBrian Somers { 5001bc9b5baSBrian Somers struct datalink *dl; 5011bc9b5baSBrian Somers 5021bc9b5baSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 503899011c4SBrian Somers datalink_Down(dl, how); 5041bc9b5baSBrian Somers } 5051bc9b5baSBrian Somers 5062f786681SBrian Somers static int 5072f786681SBrian Somers bundle_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n) 5082f786681SBrian Somers { 5092f786681SBrian Somers struct bundle *bundle = descriptor2bundle(d); 5102f786681SBrian Somers struct datalink *dl; 51104eaa58cSBrian Somers int result, want, queued, nlinks; 5122f786681SBrian Somers 5132f786681SBrian Somers result = 0; 5142f786681SBrian Somers 515078c562eSBrian Somers /* If there are aren't many packets queued, look for some more. */ 51604eaa58cSBrian Somers for (nlinks = 0, dl = bundle->links; dl; dl = dl->next) 51704eaa58cSBrian Somers nlinks++; 51804eaa58cSBrian Somers 51904eaa58cSBrian Somers if (nlinks) { 5205a72b6edSBrian Somers queued = r ? bundle_FillQueues(bundle) : ip_QueueLen(&bundle->ncp.ipcp); 52104eaa58cSBrian Somers if (bundle->autoload.running) { 52204eaa58cSBrian Somers if (queued < bundle->cfg.autoload.max.packets) { 52304eaa58cSBrian Somers if (queued > bundle->cfg.autoload.min.packets) 52404eaa58cSBrian Somers bundle_StopAutoLoadTimer(bundle); 52504eaa58cSBrian Somers else if (bundle->autoload.timer.state != TIMER_RUNNING || 52604eaa58cSBrian Somers bundle->autoload.comingup) 52704eaa58cSBrian Somers bundle_StartAutoLoadTimer(bundle, 0); 52804eaa58cSBrian Somers } else if (bundle->autoload.timer.state != TIMER_RUNNING || 52904eaa58cSBrian Somers !bundle->autoload.comingup) 53004eaa58cSBrian Somers bundle_StartAutoLoadTimer(bundle, 1); 53104eaa58cSBrian Somers } 53204eaa58cSBrian Somers 533ff0f9439SBrian Somers if (r && (bundle->phase == PHASE_NETWORK || 534ff0f9439SBrian Somers bundle->phys_type.all & PHYS_AUTO)) { 53504eaa58cSBrian Somers /* enough surplus so that we can tell if we're getting swamped */ 53604eaa58cSBrian Somers want = bundle->cfg.autoload.max.packets + nlinks * 2; 53704eaa58cSBrian Somers /* but at least 20 packets ! */ 53804eaa58cSBrian Somers if (want < 20) 53904eaa58cSBrian Somers want = 20; 54004eaa58cSBrian Somers if (queued < want) { 54104eaa58cSBrian Somers /* Not enough - select() for more */ 5426f8e9f0aSBrian Somers if (bundle->choked.timer.state == TIMER_RUNNING) 5436f8e9f0aSBrian Somers timer_Stop(&bundle->choked.timer); /* Not needed any more */ 54404eaa58cSBrian Somers FD_SET(bundle->dev.fd, r); 545faefde08SBrian Somers if (*n < bundle->dev.fd + 1) 546faefde08SBrian Somers *n = bundle->dev.fd + 1; 5471384bd27SBrian Somers log_Printf(LogTIMER, "%s: fdset(r) %d\n", TUN_NAME, bundle->dev.fd); 548078c562eSBrian Somers result++; 5496f8e9f0aSBrian Somers } else if (bundle->choked.timer.state == TIMER_STOPPED) { 5506f8e9f0aSBrian Somers bundle->choked.timer.func = bundle_ClearQueues; 5516f8e9f0aSBrian Somers bundle->choked.timer.name = "output choke"; 5526f8e9f0aSBrian Somers bundle->choked.timer.load = bundle->cfg.choked.timeout * SECTICKS; 5536f8e9f0aSBrian Somers bundle->choked.timer.arg = bundle; 5546f8e9f0aSBrian Somers timer_Start(&bundle->choked.timer); 555078c562eSBrian Somers } 55604eaa58cSBrian Somers } 55704eaa58cSBrian Somers } 558078c562eSBrian Somers 55971555108SBrian Somers /* Which links need a select() ? */ 56071555108SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 56171555108SBrian Somers result += descriptor_UpdateSet(&dl->desc, r, w, e, n); 56271555108SBrian Somers 563ea722969SBrian Somers /* 564ea722969SBrian Somers * This *MUST* be called after the datalink UpdateSet()s as it 56504eaa58cSBrian Somers * might be ``holding'' one of the datalinks (death-row) and 56604eaa58cSBrian Somers * wants to be able to de-select() it from the descriptor set. 567ea722969SBrian Somers */ 5680f2f3eb3SBrian Somers result += descriptor_UpdateSet(&bundle->ncp.mp.server.desc, r, w, e, n); 569ea722969SBrian Somers 5702f786681SBrian Somers return result; 5712f786681SBrian Somers } 5722f786681SBrian Somers 5732f786681SBrian Somers static int 5742f786681SBrian Somers bundle_IsSet(struct descriptor *d, const fd_set *fdset) 5752f786681SBrian Somers { 5762f786681SBrian Somers struct bundle *bundle = descriptor2bundle(d); 5772f786681SBrian Somers struct datalink *dl; 5782f786681SBrian Somers 5792f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 5802f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 5812f786681SBrian Somers return 1; 5822f786681SBrian Somers 583332b9de0SBrian Somers if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 584332b9de0SBrian Somers return 1; 585332b9de0SBrian Somers 586faefde08SBrian Somers return FD_ISSET(bundle->dev.fd, fdset); 5872f786681SBrian Somers } 5882f786681SBrian Somers 5892f786681SBrian Somers static void 5902f786681SBrian Somers bundle_DescriptorRead(struct descriptor *d, struct bundle *bundle, 5912f786681SBrian Somers const fd_set *fdset) 5922f786681SBrian Somers { 5932f786681SBrian Somers struct datalink *dl; 5942f786681SBrian Somers 595ea722969SBrian Somers if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 596ea722969SBrian Somers descriptor_Read(&bundle->ncp.mp.server.desc, bundle, fdset); 597ea722969SBrian Somers 5982f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 5992f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 6002f786681SBrian Somers descriptor_Read(&dl->desc, bundle, fdset); 601b6217683SBrian Somers 602faefde08SBrian Somers if (FD_ISSET(bundle->dev.fd, fdset)) { 603078c562eSBrian Somers struct tun_data tun; 604078c562eSBrian Somers int n, pri; 605078c562eSBrian Somers 606078c562eSBrian Somers /* something to read from tun */ 607faefde08SBrian Somers n = read(bundle->dev.fd, &tun, sizeof tun); 608078c562eSBrian Somers if (n < 0) { 609a33b2ef7SBrian Somers log_Printf(LogWARN, "read from %s: %s\n", TUN_NAME, strerror(errno)); 610078c562eSBrian Somers return; 611078c562eSBrian Somers } 612078c562eSBrian Somers n -= sizeof tun - sizeof tun.data; 613078c562eSBrian Somers if (n <= 0) { 614a33b2ef7SBrian Somers log_Printf(LogERROR, "read from %s: Only %d bytes read ?\n", TUN_NAME, n); 615078c562eSBrian Somers return; 616078c562eSBrian Somers } 617078c562eSBrian Somers if (!tun_check_header(tun, AF_INET)) 618078c562eSBrian Somers return; 619078c562eSBrian Somers 620078c562eSBrian Somers if (((struct ip *)tun.data)->ip_dst.s_addr == 621078c562eSBrian Somers bundle->ncp.ipcp.my_ip.s_addr) { 622078c562eSBrian Somers /* we've been asked to send something addressed *to* us :( */ 623078c562eSBrian Somers if (Enabled(bundle, OPT_LOOPBACK)) { 624078c562eSBrian Somers pri = PacketCheck(bundle, tun.data, n, &bundle->filter.in); 625078c562eSBrian Somers if (pri >= 0) { 626078c562eSBrian Somers struct mbuf *bp; 627078c562eSBrian Somers 628078c562eSBrian Somers bp = mbuf_Alloc(n, MB_IPIN); 629078c562eSBrian Somers memcpy(MBUF_CTOP(bp), tun.data, n); 630078c562eSBrian Somers ip_Input(bundle, bp); 631078c562eSBrian Somers log_Printf(LogDEBUG, "Looped back packet addressed to myself\n"); 632078c562eSBrian Somers } 633078c562eSBrian Somers return; 634078c562eSBrian Somers } else 635078c562eSBrian Somers log_Printf(LogDEBUG, "Oops - forwarding packet addressed to myself\n"); 636078c562eSBrian Somers } 637078c562eSBrian Somers 638078c562eSBrian Somers /* 639078c562eSBrian Somers * Process on-demand dialup. Output packets are queued within tunnel 640078c562eSBrian Somers * device until IPCP is opened. 641078c562eSBrian Somers */ 642078c562eSBrian Somers 643078c562eSBrian Somers if (bundle_Phase(bundle) == PHASE_DEAD) { 644078c562eSBrian Somers /* 645078c562eSBrian Somers * Note, we must be in AUTO mode :-/ otherwise our interface should 646078c562eSBrian Somers * *not* be UP and we can't receive data 647078c562eSBrian Somers */ 648078c562eSBrian Somers if ((pri = PacketCheck(bundle, tun.data, n, &bundle->filter.dial)) >= 0) 649ba23f397SBrian Somers bundle_Open(bundle, NULL, PHYS_AUTO, 0); 650078c562eSBrian Somers else 651078c562eSBrian Somers /* 652078c562eSBrian Somers * Drop the packet. If we were to queue it, we'd just end up with 653078c562eSBrian Somers * a pile of timed-out data in our output queue by the time we get 654078c562eSBrian Somers * around to actually dialing. We'd also prematurely reach the 655078c562eSBrian Somers * threshold at which we stop select()ing to read() the tun 656078c562eSBrian Somers * device - breaking auto-dial. 657078c562eSBrian Somers */ 658078c562eSBrian Somers return; 659078c562eSBrian Somers } 660078c562eSBrian Somers 661078c562eSBrian Somers pri = PacketCheck(bundle, tun.data, n, &bundle->filter.out); 662078c562eSBrian Somers if (pri >= 0) { 663078c562eSBrian Somers #ifndef NOALIAS 664615ad4f9SBrian Somers if (bundle->AliasEnabled) { 665615ad4f9SBrian Somers PacketAliasOut(tun.data, sizeof tun.data); 666078c562eSBrian Somers n = ntohs(((struct ip *)tun.data)->ip_len); 667078c562eSBrian Somers } 668078c562eSBrian Somers #endif 6695a72b6edSBrian Somers ip_Enqueue(&bundle->ncp.ipcp, pri, tun.data, n); 670078c562eSBrian Somers } 671078c562eSBrian Somers } 6722f786681SBrian Somers } 6732f786681SBrian Somers 6741af29a6eSBrian Somers static int 6752f786681SBrian Somers bundle_DescriptorWrite(struct descriptor *d, struct bundle *bundle, 6762f786681SBrian Somers const fd_set *fdset) 6772f786681SBrian Somers { 6782f786681SBrian Somers struct datalink *dl; 6791af29a6eSBrian Somers int result = 0; 6802f786681SBrian Somers 681ea722969SBrian Somers /* This is not actually necessary as struct mpserver doesn't Write() */ 682ea722969SBrian Somers if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 683ea722969SBrian Somers descriptor_Write(&bundle->ncp.mp.server.desc, bundle, fdset); 684ea722969SBrian Somers 6852f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 6862f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 6871af29a6eSBrian Somers result += descriptor_Write(&dl->desc, bundle, fdset); 6881af29a6eSBrian Somers 6891af29a6eSBrian Somers return result; 6902f786681SBrian Somers } 6912f786681SBrian Somers 692da66dd13SBrian Somers void 6931384bd27SBrian Somers bundle_LockTun(struct bundle *bundle) 6941384bd27SBrian Somers { 6951384bd27SBrian Somers FILE *lockfile; 6961384bd27SBrian Somers char pidfile[MAXPATHLEN]; 6971384bd27SBrian Somers 6981384bd27SBrian Somers snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit); 6991384bd27SBrian Somers lockfile = ID0fopen(pidfile, "w"); 7001384bd27SBrian Somers if (lockfile != NULL) { 7011384bd27SBrian Somers fprintf(lockfile, "%d\n", (int)getpid()); 7021384bd27SBrian Somers fclose(lockfile); 7031384bd27SBrian Somers } 7041384bd27SBrian Somers #ifndef RELEASE_CRUNCH 7051384bd27SBrian Somers else 7061384bd27SBrian Somers log_Printf(LogERROR, "Warning: Can't create %s: %s\n", 7071384bd27SBrian Somers pidfile, strerror(errno)); 7081384bd27SBrian Somers #endif 7091384bd27SBrian Somers } 7101384bd27SBrian Somers 7111384bd27SBrian Somers static void 7121384bd27SBrian Somers bundle_UnlockTun(struct bundle *bundle) 7131384bd27SBrian Somers { 7141384bd27SBrian Somers char pidfile[MAXPATHLEN]; 7151384bd27SBrian Somers 7161384bd27SBrian Somers snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit); 7171384bd27SBrian Somers ID0unlink(pidfile); 7181384bd27SBrian Somers } 7197a6f8720SBrian Somers 7207a6f8720SBrian Somers struct bundle * 7218e7b8599SBrian Somers bundle_Create(const char *prefix, int type, const char **argv) 7227a6f8720SBrian Somers { 7237a6f8720SBrian Somers int s, enoentcount, err; 7248fa6ebe4SBrian Somers const char *ifname; 7257a6f8720SBrian Somers struct ifreq ifrq; 7267a6f8720SBrian Somers static struct bundle bundle; /* there can be only one */ 7277a6f8720SBrian Somers 7288fa6ebe4SBrian Somers if (bundle.iface != NULL) { /* Already allocated ! */ 729a33b2ef7SBrian Somers log_Printf(LogALERT, "bundle_Create: There's only one BUNDLE !\n"); 7307a6f8720SBrian Somers return NULL; 7317a6f8720SBrian Somers } 7327a6f8720SBrian Somers 7337a6f8720SBrian Somers err = ENOENT; 7347a6f8720SBrian Somers enoentcount = 0; 735107d62e7SBrian Somers for (bundle.unit = 0; ; bundle.unit++) { 736faefde08SBrian Somers snprintf(bundle.dev.Name, sizeof bundle.dev.Name, "%s%d", 737faefde08SBrian Somers prefix, bundle.unit); 738faefde08SBrian Somers bundle.dev.fd = ID0open(bundle.dev.Name, O_RDWR); 739faefde08SBrian Somers if (bundle.dev.fd >= 0) 7407a6f8720SBrian Somers break; 741107d62e7SBrian Somers else if (errno == ENXIO) { 7427a6f8720SBrian Somers err = errno; 743107d62e7SBrian Somers break; 7447a6f8720SBrian Somers } else if (errno == ENOENT) { 7457a6f8720SBrian Somers if (++enoentcount > 2) 746107d62e7SBrian Somers break; 7477a6f8720SBrian Somers } else 7487a6f8720SBrian Somers err = errno; 7497a6f8720SBrian Somers } 7507a6f8720SBrian Somers 751faefde08SBrian Somers if (bundle.dev.fd < 0) { 752dd7e2610SBrian Somers log_Printf(LogWARN, "No available tunnel devices found (%s).\n", 75385b542cfSBrian Somers strerror(err)); 7547a6f8720SBrian Somers return NULL; 7557a6f8720SBrian Somers } 7567a6f8720SBrian Somers 757dd7e2610SBrian Somers log_SetTun(bundle.unit); 7588e7b8599SBrian Somers bundle.argv = argv; 7590f781a72SBrian Somers bundle.argv0 = argv[0]; 7600f781a72SBrian Somers bundle.argv1 = argv[1]; 7617a6f8720SBrian Somers 7627a6f8720SBrian Somers s = socket(AF_INET, SOCK_DGRAM, 0); 7637a6f8720SBrian Somers if (s < 0) { 764dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_Create: socket(): %s\n", strerror(errno)); 765faefde08SBrian Somers close(bundle.dev.fd); 7667a6f8720SBrian Somers return NULL; 7677a6f8720SBrian Somers } 7687a6f8720SBrian Somers 7698fa6ebe4SBrian Somers ifname = strrchr(bundle.dev.Name, '/'); 7708fa6ebe4SBrian Somers if (ifname == NULL) 7718fa6ebe4SBrian Somers ifname = bundle.dev.Name; 7727a6f8720SBrian Somers else 7738fa6ebe4SBrian Somers ifname++; 7748fa6ebe4SBrian Somers 7758fa6ebe4SBrian Somers bundle.iface = iface_Create(ifname); 7768fa6ebe4SBrian Somers if (bundle.iface == NULL) { 7778fa6ebe4SBrian Somers close(s); 7788fa6ebe4SBrian Somers close(bundle.dev.fd); 7798fa6ebe4SBrian Somers return NULL; 7808fa6ebe4SBrian Somers } 7817a6f8720SBrian Somers 7827a6f8720SBrian Somers /* 7837a6f8720SBrian Somers * Now, bring up the interface. 7847a6f8720SBrian Somers */ 7857a6f8720SBrian Somers memset(&ifrq, '\0', sizeof ifrq); 7868fa6ebe4SBrian Somers strncpy(ifrq.ifr_name, ifname, sizeof ifrq.ifr_name - 1); 7877a6f8720SBrian Somers ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0'; 7887a6f8720SBrian Somers if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) { 789a33b2ef7SBrian Somers log_Printf(LogERROR, "bundle_Create: ioctl(SIOCGIFFLAGS): %s\n", 7907a6f8720SBrian Somers strerror(errno)); 7917a6f8720SBrian Somers close(s); 7928fa6ebe4SBrian Somers iface_Destroy(bundle.iface); 7938fa6ebe4SBrian Somers bundle.iface = NULL; 794faefde08SBrian Somers close(bundle.dev.fd); 7957a6f8720SBrian Somers return NULL; 7967a6f8720SBrian Somers } 7977a6f8720SBrian Somers ifrq.ifr_flags |= IFF_UP; 7987a6f8720SBrian Somers if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) { 799a33b2ef7SBrian Somers log_Printf(LogERROR, "bundle_Create: ioctl(SIOCSIFFLAGS): %s\n", 8007a6f8720SBrian Somers strerror(errno)); 8017a6f8720SBrian Somers close(s); 8028fa6ebe4SBrian Somers iface_Destroy(bundle.iface); 8038fa6ebe4SBrian Somers bundle.iface = NULL; 804faefde08SBrian Somers close(bundle.dev.fd); 8057a6f8720SBrian Somers return NULL; 8067a6f8720SBrian Somers } 8077a6f8720SBrian Somers 8087a6f8720SBrian Somers close(s); 8097a6f8720SBrian Somers 8108fa6ebe4SBrian Somers log_Printf(LogPHASE, "Using interface: %s\n", ifname); 8117a6f8720SBrian Somers 8128fa6ebe4SBrian Somers bundle.ifSpeed = 0; 8137a6f8720SBrian Somers 814820de6ebSBrian Somers bundle.routing_seq = 0; 815a0cbd833SBrian Somers bundle.phase = PHASE_DEAD; 816a0cbd833SBrian Somers bundle.CleaningUp = 0; 817615ad4f9SBrian Somers bundle.AliasEnabled = 0; 8187a6f8720SBrian Somers 8196d666775SBrian Somers bundle.fsm.LayerStart = bundle_LayerStart; 8206f384573SBrian Somers bundle.fsm.LayerUp = bundle_LayerUp; 8216d666775SBrian Somers bundle.fsm.LayerDown = bundle_LayerDown; 8226d666775SBrian Somers bundle.fsm.LayerFinish = bundle_LayerFinish; 8236d666775SBrian Somers bundle.fsm.object = &bundle; 8247a6f8720SBrian Somers 825ab886ad0SBrian Somers bundle.cfg.idle_timeout = NCP_IDLE_TIMEOUT; 8261342caedSBrian Somers *bundle.cfg.auth.name = '\0'; 8271342caedSBrian Somers *bundle.cfg.auth.key = '\0'; 828610b185fSBrian Somers bundle.cfg.opt = OPT_SROUTES | OPT_IDCHECK | OPT_LOOPBACK | 829610b185fSBrian Somers OPT_THROUGHPUT | OPT_UTMP; 83049052c95SBrian Somers *bundle.cfg.label = '\0'; 83149052c95SBrian Somers bundle.cfg.mtu = DEF_MTU; 83204eaa58cSBrian Somers bundle.cfg.autoload.max.packets = 0; 83304eaa58cSBrian Somers bundle.cfg.autoload.max.timeout = 0; 83404eaa58cSBrian Somers bundle.cfg.autoload.min.packets = 0; 83504eaa58cSBrian Somers bundle.cfg.autoload.min.timeout = 0; 8366f8e9f0aSBrian Somers bundle.cfg.choked.timeout = CHOKED_TIMEOUT; 837ff0f9439SBrian Somers bundle.phys_type.all = type; 838ff0f9439SBrian Somers bundle.phys_type.open = 0; 839ab886ad0SBrian Somers 8406f384573SBrian Somers bundle.links = datalink_Create("deflink", &bundle, type); 8413006ec67SBrian Somers if (bundle.links == NULL) { 842a33b2ef7SBrian Somers log_Printf(LogALERT, "Cannot create data link: %s\n", strerror(errno)); 8438fa6ebe4SBrian Somers iface_Destroy(bundle.iface); 8448fa6ebe4SBrian Somers bundle.iface = NULL; 845faefde08SBrian Somers close(bundle.dev.fd); 8462289f246SBrian Somers return NULL; 8472289f246SBrian Somers } 8482289f246SBrian Somers 8492f786681SBrian Somers bundle.desc.type = BUNDLE_DESCRIPTOR; 8502f786681SBrian Somers bundle.desc.UpdateSet = bundle_UpdateSet; 8512f786681SBrian Somers bundle.desc.IsSet = bundle_IsSet; 8522f786681SBrian Somers bundle.desc.Read = bundle_DescriptorRead; 8532f786681SBrian Somers bundle.desc.Write = bundle_DescriptorWrite; 8542f786681SBrian Somers 85549052c95SBrian Somers mp_Init(&bundle.ncp.mp, &bundle); 85649052c95SBrian Somers 85749052c95SBrian Somers /* Send over the first physical link by default */ 8585828db6dSBrian Somers ipcp_Init(&bundle.ncp.ipcp, &bundle, &bundle.links->physical->link, 8595828db6dSBrian Somers &bundle.fsm); 8606d666775SBrian Somers 8615ca5389aSBrian Somers memset(&bundle.filter, '\0', sizeof bundle.filter); 8625ca5389aSBrian Somers bundle.filter.in.fragok = bundle.filter.in.logok = 1; 8635ca5389aSBrian Somers bundle.filter.in.name = "IN"; 8645ca5389aSBrian Somers bundle.filter.out.fragok = bundle.filter.out.logok = 1; 8655ca5389aSBrian Somers bundle.filter.out.name = "OUT"; 8665ca5389aSBrian Somers bundle.filter.dial.name = "DIAL"; 8678390b576SBrian Somers bundle.filter.dial.logok = 1; 8685ca5389aSBrian Somers bundle.filter.alive.name = "ALIVE"; 8695ca5389aSBrian Somers bundle.filter.alive.logok = 1; 87093ee0ff2SBrian Somers memset(&bundle.idle.timer, '\0', sizeof bundle.idle.timer); 87193ee0ff2SBrian Somers bundle.idle.done = 0; 8725cf4388bSBrian Somers bundle.notify.fd = -1; 87304eaa58cSBrian Somers memset(&bundle.autoload.timer, '\0', sizeof bundle.autoload.timer); 87404eaa58cSBrian Somers bundle.autoload.done = 0; 87504eaa58cSBrian Somers bundle.autoload.running = 0; 8766f8e9f0aSBrian Somers memset(&bundle.choked.timer, '\0', sizeof bundle.choked.timer); 87793ee0ff2SBrian Somers 8786d666775SBrian Somers /* Clean out any leftover crud */ 8798fa6ebe4SBrian Somers iface_Clear(bundle.iface, IFACE_CLEAR_ALL); 8806d666775SBrian Somers 8811384bd27SBrian Somers bundle_LockTun(&bundle); 8821384bd27SBrian Somers 8837a6f8720SBrian Somers return &bundle; 8847a6f8720SBrian Somers } 8857a6f8720SBrian Somers 88668a0f0ccSBrian Somers static void 88768a0f0ccSBrian Somers bundle_DownInterface(struct bundle *bundle) 88868a0f0ccSBrian Somers { 88968a0f0ccSBrian Somers struct ifreq ifrq; 89068a0f0ccSBrian Somers int s; 89168a0f0ccSBrian Somers 892dd7e2610SBrian Somers route_IfDelete(bundle, 1); 89368a0f0ccSBrian Somers 89468a0f0ccSBrian Somers s = ID0socket(AF_INET, SOCK_DGRAM, 0); 89568a0f0ccSBrian Somers if (s < 0) { 896dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_DownInterface: socket: %s\n", strerror(errno)); 89768a0f0ccSBrian Somers return; 89868a0f0ccSBrian Somers } 89968a0f0ccSBrian Somers 90068a0f0ccSBrian Somers memset(&ifrq, '\0', sizeof ifrq); 9018fa6ebe4SBrian Somers strncpy(ifrq.ifr_name, bundle->iface->name, sizeof ifrq.ifr_name - 1); 90268a0f0ccSBrian Somers ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0'; 90368a0f0ccSBrian Somers if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) { 904dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_DownInterface: ioctl(SIOCGIFFLAGS): %s\n", 90568a0f0ccSBrian Somers strerror(errno)); 90668a0f0ccSBrian Somers close(s); 90768a0f0ccSBrian Somers return; 90868a0f0ccSBrian Somers } 90968a0f0ccSBrian Somers ifrq.ifr_flags &= ~IFF_UP; 91068a0f0ccSBrian Somers if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) { 911dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_DownInterface: ioctl(SIOCSIFFLAGS): %s\n", 91268a0f0ccSBrian Somers strerror(errno)); 91368a0f0ccSBrian Somers close(s); 91468a0f0ccSBrian Somers return; 91568a0f0ccSBrian Somers } 91668a0f0ccSBrian Somers close(s); 91768a0f0ccSBrian Somers } 91868a0f0ccSBrian Somers 91968a0f0ccSBrian Somers void 92068a0f0ccSBrian Somers bundle_Destroy(struct bundle *bundle) 92168a0f0ccSBrian Somers { 9223006ec67SBrian Somers struct datalink *dl; 923b6217683SBrian Somers 924ea722969SBrian Somers /* 92504eaa58cSBrian Somers * Clean up the interface. We don't need to timer_Stop()s, mp_Down(), 926ea722969SBrian Somers * ipcp_CleanInterface() and bundle_DownInterface() unless we're getting 927ea722969SBrian Somers * out under exceptional conditions such as a descriptor exception. 928ea722969SBrian Somers */ 92904eaa58cSBrian Somers timer_Stop(&bundle->idle.timer); 9306f8e9f0aSBrian Somers timer_Stop(&bundle->choked.timer); 93104eaa58cSBrian Somers timer_Stop(&bundle->autoload.timer); 93266f634b6SBrian Somers mp_Down(&bundle->ncp.mp); 933dd7e2610SBrian Somers ipcp_CleanInterface(&bundle->ncp.ipcp); 93468a0f0ccSBrian Somers bundle_DownInterface(bundle); 9353006ec67SBrian Somers 936ea722969SBrian Somers /* Again, these are all DATALINK_CLOSED unless we're abending */ 9373006ec67SBrian Somers dl = bundle->links; 9383006ec67SBrian Somers while (dl) 9393006ec67SBrian Somers dl = datalink_Destroy(dl); 9403006ec67SBrian Somers 9411384bd27SBrian Somers close(bundle->dev.fd); 9421384bd27SBrian Somers bundle_UnlockTun(bundle); 9431384bd27SBrian Somers 944ea722969SBrian Somers /* In case we never made PHASE_NETWORK */ 9455cf4388bSBrian Somers bundle_Notify(bundle, EX_ERRDEAD); 946b6217683SBrian Somers 9478fa6ebe4SBrian Somers iface_Destroy(bundle->iface); 9488fa6ebe4SBrian Somers bundle->iface = NULL; 94968a0f0ccSBrian Somers } 95068a0f0ccSBrian Somers 9517a6f8720SBrian Somers struct rtmsg { 9527a6f8720SBrian Somers struct rt_msghdr m_rtm; 9537a6f8720SBrian Somers char m_space[64]; 9547a6f8720SBrian Somers }; 9557a6f8720SBrian Somers 956610b185fSBrian Somers int 957820de6ebSBrian Somers bundle_SetRoute(struct bundle *bundle, int cmd, struct in_addr dst, 9582062443fSBrian Somers struct in_addr gateway, struct in_addr mask, int bang, int ssh) 9597a6f8720SBrian Somers { 9607a6f8720SBrian Somers struct rtmsg rtmes; 9617a6f8720SBrian Somers int s, nb, wb; 9627a6f8720SBrian Somers char *cp; 9637a6f8720SBrian Somers const char *cmdstr; 9647a6f8720SBrian Somers struct sockaddr_in rtdata; 965610b185fSBrian Somers int result = 1; 9667a6f8720SBrian Somers 9677a6f8720SBrian Somers if (bang) 9687a6f8720SBrian Somers cmdstr = (cmd == RTM_ADD ? "Add!" : "Delete!"); 9697a6f8720SBrian Somers else 9707a6f8720SBrian Somers cmdstr = (cmd == RTM_ADD ? "Add" : "Delete"); 9717a6f8720SBrian Somers s = ID0socket(PF_ROUTE, SOCK_RAW, 0); 9727a6f8720SBrian Somers if (s < 0) { 973dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_SetRoute: socket(): %s\n", strerror(errno)); 974610b185fSBrian Somers return result; 9757a6f8720SBrian Somers } 9767a6f8720SBrian Somers memset(&rtmes, '\0', sizeof rtmes); 9777a6f8720SBrian Somers rtmes.m_rtm.rtm_version = RTM_VERSION; 9787a6f8720SBrian Somers rtmes.m_rtm.rtm_type = cmd; 9797a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs = RTA_DST; 980820de6ebSBrian Somers rtmes.m_rtm.rtm_seq = ++bundle->routing_seq; 9817a6f8720SBrian Somers rtmes.m_rtm.rtm_pid = getpid(); 9827a6f8720SBrian Somers rtmes.m_rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC; 9837a6f8720SBrian Somers 9843afe5ccbSBrian Somers if (cmd == RTM_ADD || cmd == RTM_CHANGE) { 9853afe5ccbSBrian Somers if (bundle->ncp.ipcp.cfg.sendpipe > 0) { 9863afe5ccbSBrian Somers rtmes.m_rtm.rtm_rmx.rmx_sendpipe = bundle->ncp.ipcp.cfg.sendpipe; 9873afe5ccbSBrian Somers rtmes.m_rtm.rtm_inits |= RTV_SPIPE; 9883afe5ccbSBrian Somers } 9893afe5ccbSBrian Somers if (bundle->ncp.ipcp.cfg.recvpipe > 0) { 9903afe5ccbSBrian Somers rtmes.m_rtm.rtm_rmx.rmx_recvpipe = bundle->ncp.ipcp.cfg.recvpipe; 9913afe5ccbSBrian Somers rtmes.m_rtm.rtm_inits |= RTV_RPIPE; 9923afe5ccbSBrian Somers } 9933afe5ccbSBrian Somers } 9943afe5ccbSBrian Somers 9957a6f8720SBrian Somers memset(&rtdata, '\0', sizeof rtdata); 99650e5c17dSBrian Somers rtdata.sin_len = sizeof rtdata; 9977a6f8720SBrian Somers rtdata.sin_family = AF_INET; 9987a6f8720SBrian Somers rtdata.sin_port = 0; 9997a6f8720SBrian Somers rtdata.sin_addr = dst; 10007a6f8720SBrian Somers 10017a6f8720SBrian Somers cp = rtmes.m_space; 100250e5c17dSBrian Somers memcpy(cp, &rtdata, rtdata.sin_len); 100350e5c17dSBrian Somers cp += rtdata.sin_len; 1004e43ebac1SBrian Somers if (cmd == RTM_ADD) { 10057a6f8720SBrian Somers if (gateway.s_addr == INADDR_ANY) { 10068fa6ebe4SBrian Somers log_Printf(LogERROR, "bundle_SetRoute: Cannot add a route with" 10078fa6ebe4SBrian Somers " destination 0.0.0.0\n"); 10088fa6ebe4SBrian Somers close(s); 10098fa6ebe4SBrian Somers return result; 10107a6f8720SBrian Somers } else { 10117a6f8720SBrian Somers rtdata.sin_addr = gateway; 101250e5c17dSBrian Somers memcpy(cp, &rtdata, rtdata.sin_len); 101350e5c17dSBrian Somers cp += rtdata.sin_len; 10147a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY; 10157a6f8720SBrian Somers } 1016e43ebac1SBrian Somers } 10177a6f8720SBrian Somers 10187a6f8720SBrian Somers if (dst.s_addr == INADDR_ANY) 10197a6f8720SBrian Somers mask.s_addr = INADDR_ANY; 10207a6f8720SBrian Somers 10217a6f8720SBrian Somers if (cmd == RTM_ADD || dst.s_addr == INADDR_ANY) { 10227a6f8720SBrian Somers rtdata.sin_addr = mask; 102350e5c17dSBrian Somers memcpy(cp, &rtdata, rtdata.sin_len); 102450e5c17dSBrian Somers cp += rtdata.sin_len; 10257a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs |= RTA_NETMASK; 10267a6f8720SBrian Somers } 10277a6f8720SBrian Somers 10287a6f8720SBrian Somers nb = cp - (char *) &rtmes; 10297a6f8720SBrian Somers rtmes.m_rtm.rtm_msglen = nb; 10307a6f8720SBrian Somers wb = ID0write(s, &rtmes, nb); 10317a6f8720SBrian Somers if (wb < 0) { 1032dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute failure:\n"); 1033dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Cmd = %s\n", cmdstr); 1034dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Dst = %s\n", inet_ntoa(dst)); 1035dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Gateway = %s\n", inet_ntoa(gateway)); 1036dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Mask = %s\n", inet_ntoa(mask)); 10377a6f8720SBrian Somers failed: 10387a6f8720SBrian Somers if (cmd == RTM_ADD && (rtmes.m_rtm.rtm_errno == EEXIST || 1039e43ebac1SBrian Somers (rtmes.m_rtm.rtm_errno == 0 && errno == EEXIST))) { 1040610b185fSBrian Somers if (!bang) { 1041dd7e2610SBrian Somers log_Printf(LogWARN, "Add route failed: %s already exists\n", 10423afe5ccbSBrian Somers dst.s_addr == 0 ? "default" : inet_ntoa(dst)); 1043610b185fSBrian Somers result = 0; /* Don't add to our dynamic list */ 1044610b185fSBrian Somers } else { 10457a6f8720SBrian Somers rtmes.m_rtm.rtm_type = cmd = RTM_CHANGE; 10467a6f8720SBrian Somers if ((wb = ID0write(s, &rtmes, nb)) < 0) 10477a6f8720SBrian Somers goto failed; 10487a6f8720SBrian Somers } 1049e43ebac1SBrian Somers } else if (cmd == RTM_DELETE && 10507a6f8720SBrian Somers (rtmes.m_rtm.rtm_errno == ESRCH || 10517a6f8720SBrian Somers (rtmes.m_rtm.rtm_errno == 0 && errno == ESRCH))) { 10527a6f8720SBrian Somers if (!bang) 1053dd7e2610SBrian Somers log_Printf(LogWARN, "Del route failed: %s: Non-existent\n", 10547a6f8720SBrian Somers inet_ntoa(dst)); 10552062443fSBrian Somers } else if (rtmes.m_rtm.rtm_errno == 0) { 10562062443fSBrian Somers if (!ssh || errno != ENETUNREACH) 1057dd7e2610SBrian Somers log_Printf(LogWARN, "%s route failed: %s: errno: %s\n", cmdstr, 10587a6f8720SBrian Somers inet_ntoa(dst), strerror(errno)); 10592062443fSBrian Somers } else 1060dd7e2610SBrian Somers log_Printf(LogWARN, "%s route failed: %s: %s\n", 10617a6f8720SBrian Somers cmdstr, inet_ntoa(dst), strerror(rtmes.m_rtm.rtm_errno)); 10627a6f8720SBrian Somers } 1063dd7e2610SBrian Somers log_Printf(LogDEBUG, "wrote %d: cmd = %s, dst = %x, gateway = %x\n", 1064fe3125a0SBrian Somers wb, cmdstr, (unsigned)dst.s_addr, (unsigned)gateway.s_addr); 10657a6f8720SBrian Somers close(s); 1066610b185fSBrian Somers 1067610b185fSBrian Somers return result; 10687a6f8720SBrian Somers } 106983d1af55SBrian Somers 107083d1af55SBrian Somers void 10713006ec67SBrian Somers bundle_LinkClosed(struct bundle *bundle, struct datalink *dl) 10723006ec67SBrian Somers { 10733006ec67SBrian Somers /* 10743006ec67SBrian Somers * Our datalink has closed. 1075ea722969SBrian Somers * CleanDatalinks() (called from DoLoop()) will remove closed 107681358fa3SBrian Somers * BACKGROUND and DIRECT links. 10773b0f8d2eSBrian Somers * If it's the last data link, enter phase DEAD. 1078ea722969SBrian Somers * 1079ea722969SBrian Somers * NOTE: dl may not be in our list (bundle_SendDatalink()) ! 10803006ec67SBrian Somers */ 10815b8b8060SBrian Somers 10823b0f8d2eSBrian Somers struct datalink *odl; 10833b0f8d2eSBrian Somers int other_links; 10845b8b8060SBrian Somers 1085bf1d3ff6SBrian Somers log_SetTtyCommandMode(dl); 1086bf1d3ff6SBrian Somers 10873b0f8d2eSBrian Somers other_links = 0; 10883b0f8d2eSBrian Somers for (odl = bundle->links; odl; odl = odl->next) 10893b0f8d2eSBrian Somers if (odl != dl && odl->state != DATALINK_CLOSED) 10903b0f8d2eSBrian Somers other_links++; 10913b0f8d2eSBrian Somers 10923b0f8d2eSBrian Somers if (!other_links) { 109381358fa3SBrian Somers if (dl->physical->type != PHYS_AUTO) /* Not in -auto mode */ 109403704096SBrian Somers bundle_DownInterface(bundle); 109509206a6fSBrian Somers fsm2initial(&bundle->ncp.ipcp.fsm); 10965563ebdeSBrian Somers bundle_NewPhase(bundle, PHASE_DEAD); 10970f2f3eb3SBrian Somers bundle_StopIdleTimer(bundle); 109804eaa58cSBrian Somers bundle_StopAutoLoadTimer(bundle); 109904eaa58cSBrian Somers bundle->autoload.running = 0; 110004eaa58cSBrian Somers } else 110104eaa58cSBrian Somers bundle->autoload.running = 1; 1102455aabc3SBrian Somers } 1103455aabc3SBrian Somers 1104455aabc3SBrian Somers void 1105ba23f397SBrian Somers bundle_Open(struct bundle *bundle, const char *name, int mask, int force) 11063006ec67SBrian Somers { 11073006ec67SBrian Somers /* 11083006ec67SBrian Somers * Please open the given datalink, or all if name == NULL 11093006ec67SBrian Somers */ 11103006ec67SBrian Somers struct datalink *dl; 11113006ec67SBrian Somers 111204eaa58cSBrian Somers timer_Stop(&bundle->autoload.timer); 11133006ec67SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 11143006ec67SBrian Somers if (name == NULL || !strcasecmp(dl->name, name)) { 1115ba23f397SBrian Somers if ((mask & dl->physical->type) && 1116ba23f397SBrian Somers (dl->state == DATALINK_CLOSED || 1117ba23f397SBrian Somers (force && dl->state == DATALINK_OPENING && 1118ba23f397SBrian Somers dl->dial_timer.state == TIMER_RUNNING))) { 1119ba23f397SBrian Somers if (force) 1120ba23f397SBrian Somers timer_Stop(&dl->dial_timer); 1121565e35e5SBrian Somers datalink_Up(dl, 1, 1); 112281358fa3SBrian Somers if (mask == PHYS_AUTO) 112381358fa3SBrian Somers /* Only one AUTO link at a time (see the AutoLoad timer) */ 112404eaa58cSBrian Somers break; 112504eaa58cSBrian Somers } 11263006ec67SBrian Somers if (name != NULL) 11273006ec67SBrian Somers break; 11283006ec67SBrian Somers } 11293006ec67SBrian Somers } 11303006ec67SBrian Somers 11313006ec67SBrian Somers struct datalink * 11323006ec67SBrian Somers bundle2datalink(struct bundle *bundle, const char *name) 11333006ec67SBrian Somers { 11343006ec67SBrian Somers struct datalink *dl; 11353006ec67SBrian Somers 11363006ec67SBrian Somers if (name != NULL) { 11373006ec67SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 11383006ec67SBrian Somers if (!strcasecmp(dl->name, name)) 11393006ec67SBrian Somers return dl; 11403006ec67SBrian Somers } else if (bundle->links && !bundle->links->next) 11413006ec67SBrian Somers return bundle->links; 11423006ec67SBrian Somers 11433006ec67SBrian Somers return NULL; 11443006ec67SBrian Somers } 11453006ec67SBrian Somers 11463006ec67SBrian Somers int 11473006ec67SBrian Somers bundle_FillQueues(struct bundle *bundle) 11483006ec67SBrian Somers { 11493b0f8d2eSBrian Somers int total; 11503b0f8d2eSBrian Somers 11511bc9b5baSBrian Somers if (bundle->ncp.mp.active) 11523b0f8d2eSBrian Somers total = mp_FillQueues(bundle); 11531bc9b5baSBrian Somers else { 11541bc9b5baSBrian Somers struct datalink *dl; 11551bc9b5baSBrian Somers int add; 11561bc9b5baSBrian Somers 11571bc9b5baSBrian Somers for (total = 0, dl = bundle->links; dl; dl = dl->next) 11581bc9b5baSBrian Somers if (dl->state == DATALINK_OPEN) { 11591bc9b5baSBrian Somers add = link_QueueLen(&dl->physical->link); 11601bc9b5baSBrian Somers if (add == 0 && dl->physical->out == NULL) 11611bc9b5baSBrian Somers add = ip_FlushPacket(&dl->physical->link, bundle); 11621bc9b5baSBrian Somers total += add; 11631bc9b5baSBrian Somers } 11643006ec67SBrian Somers } 11653006ec67SBrian Somers 11665a72b6edSBrian Somers return total + ip_QueueLen(&bundle->ncp.ipcp); 11673006ec67SBrian Somers } 1168aef795ccSBrian Somers 1169aef795ccSBrian Somers int 1170aef795ccSBrian Somers bundle_ShowLinks(struct cmdargs const *arg) 1171aef795ccSBrian Somers { 1172aef795ccSBrian Somers struct datalink *dl; 1173aef795ccSBrian Somers 11749c53a7b1SBrian Somers for (dl = arg->bundle->links; dl; dl = dl->next) { 1175d4156d00SBrian Somers prompt_Printf(arg->prompt, "Name: %s [%s, %s]", 1176d4156d00SBrian Somers dl->name, mode2Nam(dl->physical->type), datalink_State(dl)); 1177eeab6bf5SBrian Somers if (dl->physical->link.throughput.rolling && dl->state == DATALINK_OPEN) 1178d4156d00SBrian Somers prompt_Printf(arg->prompt, " weight %d, %d bytes/sec", 1179eeab6bf5SBrian Somers dl->mp.weight, 11809c53a7b1SBrian Somers dl->physical->link.throughput.OctetsPerSecond); 11819c53a7b1SBrian Somers prompt_Printf(arg->prompt, "\n"); 11829c53a7b1SBrian Somers } 1183aef795ccSBrian Somers 1184aef795ccSBrian Somers return 0; 1185aef795ccSBrian Somers } 1186ab886ad0SBrian Somers 11871342caedSBrian Somers static const char * 11881342caedSBrian Somers optval(struct bundle *bundle, int bit) 11891342caedSBrian Somers { 11901342caedSBrian Somers return (bundle->cfg.opt & bit) ? "enabled" : "disabled"; 11911342caedSBrian Somers } 11921342caedSBrian Somers 1193c08717dfSBrian Somers int 1194c08717dfSBrian Somers bundle_ShowStatus(struct cmdargs const *arg) 1195c08717dfSBrian Somers { 1196c08717dfSBrian Somers int remaining; 1197c08717dfSBrian Somers 1198c08717dfSBrian Somers prompt_Printf(arg->prompt, "Phase %s\n", bundle_PhaseName(arg->bundle)); 11990f781a72SBrian Somers prompt_Printf(arg->prompt, " Title: %s\n", arg->bundle->argv[0]); 1200faefde08SBrian Somers prompt_Printf(arg->prompt, " Device: %s\n", arg->bundle->dev.Name); 1201faefde08SBrian Somers prompt_Printf(arg->prompt, " Interface: %s @ %lubps\n", 12028fa6ebe4SBrian Somers arg->bundle->iface->name, arg->bundle->ifSpeed); 1203c08717dfSBrian Somers 1204c08717dfSBrian Somers prompt_Printf(arg->prompt, "\nDefaults:\n"); 1205643f4904SBrian Somers prompt_Printf(arg->prompt, " Label: %s\n", arg->bundle->cfg.label); 1206610b185fSBrian Somers prompt_Printf(arg->prompt, " Auth name: %s\n", 1207610b185fSBrian Somers arg->bundle->cfg.auth.name); 120804eaa58cSBrian Somers prompt_Printf(arg->prompt, " Auto Load: Up after %ds of >= %d packets\n", 120904eaa58cSBrian Somers arg->bundle->cfg.autoload.max.timeout, 121004eaa58cSBrian Somers arg->bundle->cfg.autoload.max.packets); 121104eaa58cSBrian Somers prompt_Printf(arg->prompt, " Down after %ds of <= %d" 121204eaa58cSBrian Somers " packets\n", arg->bundle->cfg.autoload.min.timeout, 121304eaa58cSBrian Somers arg->bundle->cfg.autoload.min.packets); 121404eaa58cSBrian Somers if (arg->bundle->autoload.timer.state == TIMER_RUNNING) 121504eaa58cSBrian Somers prompt_Printf(arg->prompt, " %ds remaining 'till " 121604eaa58cSBrian Somers "a link comes %s\n", 121704eaa58cSBrian Somers bundle_RemainingAutoLoadTime(arg->bundle), 121804eaa58cSBrian Somers arg->bundle->autoload.comingup ? "up" : "down"); 121904eaa58cSBrian Somers else 122004eaa58cSBrian Somers prompt_Printf(arg->prompt, " %srunning with %d" 122104eaa58cSBrian Somers " packets queued\n", arg->bundle->autoload.running ? 12225a72b6edSBrian Somers "" : "not ", ip_QueueLen(&arg->bundle->ncp.ipcp)); 122304eaa58cSBrian Somers 12246f8e9f0aSBrian Somers prompt_Printf(arg->prompt, " Choked Timer: %ds\n", 12256f8e9f0aSBrian Somers arg->bundle->cfg.choked.timeout); 1226c08717dfSBrian Somers prompt_Printf(arg->prompt, " Idle Timer: "); 1227c08717dfSBrian Somers if (arg->bundle->cfg.idle_timeout) { 1228c08717dfSBrian Somers prompt_Printf(arg->prompt, "%ds", arg->bundle->cfg.idle_timeout); 1229c08717dfSBrian Somers remaining = bundle_RemainingIdleTime(arg->bundle); 1230c08717dfSBrian Somers if (remaining != -1) 1231c08717dfSBrian Somers prompt_Printf(arg->prompt, " (%ds remaining)", remaining); 1232c08717dfSBrian Somers prompt_Printf(arg->prompt, "\n"); 1233c08717dfSBrian Somers } else 1234c08717dfSBrian Somers prompt_Printf(arg->prompt, "disabled\n"); 1235ce828a6eSBrian Somers prompt_Printf(arg->prompt, " MTU: "); 1236ce828a6eSBrian Somers if (arg->bundle->cfg.mtu) 1237ce828a6eSBrian Somers prompt_Printf(arg->prompt, "%d\n", arg->bundle->cfg.mtu); 1238ce828a6eSBrian Somers else 1239ce828a6eSBrian Somers prompt_Printf(arg->prompt, "unspecified\n"); 1240c08717dfSBrian Somers 12413afe5ccbSBrian Somers prompt_Printf(arg->prompt, " sendpipe: "); 12423afe5ccbSBrian Somers if (arg->bundle->ncp.ipcp.cfg.sendpipe > 0) 12433afe5ccbSBrian Somers prompt_Printf(arg->prompt, "%ld\n", arg->bundle->ncp.ipcp.cfg.sendpipe); 12443afe5ccbSBrian Somers else 12453afe5ccbSBrian Somers prompt_Printf(arg->prompt, "unspecified\n"); 12463afe5ccbSBrian Somers prompt_Printf(arg->prompt, " recvpipe: "); 12473afe5ccbSBrian Somers if (arg->bundle->ncp.ipcp.cfg.recvpipe > 0) 12483afe5ccbSBrian Somers prompt_Printf(arg->prompt, "%ld\n", arg->bundle->ncp.ipcp.cfg.recvpipe); 12493afe5ccbSBrian Somers else 12503afe5ccbSBrian Somers prompt_Printf(arg->prompt, "unspecified\n"); 12513afe5ccbSBrian Somers 1252610b185fSBrian Somers prompt_Printf(arg->prompt, " Sticky Routes: %s\n", 1253610b185fSBrian Somers optval(arg->bundle, OPT_SROUTES)); 12541342caedSBrian Somers prompt_Printf(arg->prompt, " ID check: %s\n", 12551342caedSBrian Somers optval(arg->bundle, OPT_IDCHECK)); 12561342caedSBrian Somers prompt_Printf(arg->prompt, " Loopback: %s\n", 12571342caedSBrian Somers optval(arg->bundle, OPT_LOOPBACK)); 12581342caedSBrian Somers prompt_Printf(arg->prompt, " PasswdAuth: %s\n", 12591342caedSBrian Somers optval(arg->bundle, OPT_PASSWDAUTH)); 12601342caedSBrian Somers prompt_Printf(arg->prompt, " Proxy: %s\n", 12611342caedSBrian Somers optval(arg->bundle, OPT_PROXY)); 12623afe5ccbSBrian Somers prompt_Printf(arg->prompt, " Proxyall: %s\n", 12633afe5ccbSBrian Somers optval(arg->bundle, OPT_PROXYALL)); 12641342caedSBrian Somers prompt_Printf(arg->prompt, " Throughput: %s\n", 12651342caedSBrian Somers optval(arg->bundle, OPT_THROUGHPUT)); 1266610b185fSBrian Somers prompt_Printf(arg->prompt, " Utmp Logging: %s\n", 12671342caedSBrian Somers optval(arg->bundle, OPT_UTMP)); 12688fa6ebe4SBrian Somers prompt_Printf(arg->prompt, " Iface-Alias: %s\n", 12698fa6ebe4SBrian Somers optval(arg->bundle, OPT_IFACEALIAS)); 12701342caedSBrian Somers 1271c08717dfSBrian Somers return 0; 1272c08717dfSBrian Somers } 1273c08717dfSBrian Somers 1274ab886ad0SBrian Somers static void 1275ab886ad0SBrian Somers bundle_IdleTimeout(void *v) 1276ab886ad0SBrian Somers { 1277ab886ad0SBrian Somers struct bundle *bundle = (struct bundle *)v; 1278ab886ad0SBrian Somers 1279dd7e2610SBrian Somers log_Printf(LogPHASE, "Idle timer expired.\n"); 128004eaa58cSBrian Somers bundle_StopIdleTimer(bundle); 12819c81b87dSBrian Somers bundle_Close(bundle, NULL, CLOSE_STAYDOWN); 1282ab886ad0SBrian Somers } 1283ab886ad0SBrian Somers 1284ab886ad0SBrian Somers /* 1285ab886ad0SBrian Somers * Start Idle timer. If timeout is reached, we call bundle_Close() to 1286ab886ad0SBrian Somers * close LCP and link. 1287ab886ad0SBrian Somers */ 1288ab886ad0SBrian Somers void 1289ab886ad0SBrian Somers bundle_StartIdleTimer(struct bundle *bundle) 1290ab886ad0SBrian Somers { 1291dd7e2610SBrian Somers timer_Stop(&bundle->idle.timer); 1292ff0f9439SBrian Somers if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) != 1293ff0f9439SBrian Somers bundle->phys_type.open && bundle->cfg.idle_timeout) { 129493ee0ff2SBrian Somers bundle->idle.timer.func = bundle_IdleTimeout; 12953b0f8d2eSBrian Somers bundle->idle.timer.name = "idle"; 129693ee0ff2SBrian Somers bundle->idle.timer.load = bundle->cfg.idle_timeout * SECTICKS; 129793ee0ff2SBrian Somers bundle->idle.timer.arg = bundle; 1298dd7e2610SBrian Somers timer_Start(&bundle->idle.timer); 129993ee0ff2SBrian Somers bundle->idle.done = time(NULL) + bundle->cfg.idle_timeout; 1300ab886ad0SBrian Somers } 1301ab886ad0SBrian Somers } 1302ab886ad0SBrian Somers 1303ab886ad0SBrian Somers void 1304ab886ad0SBrian Somers bundle_SetIdleTimer(struct bundle *bundle, int value) 1305ab886ad0SBrian Somers { 1306ab886ad0SBrian Somers bundle->cfg.idle_timeout = value; 1307ab886ad0SBrian Somers if (bundle_LinkIsUp(bundle)) 1308ab886ad0SBrian Somers bundle_StartIdleTimer(bundle); 1309ab886ad0SBrian Somers } 1310ab886ad0SBrian Somers 1311ab886ad0SBrian Somers void 1312ab886ad0SBrian Somers bundle_StopIdleTimer(struct bundle *bundle) 1313ab886ad0SBrian Somers { 1314dd7e2610SBrian Somers timer_Stop(&bundle->idle.timer); 13154a632c80SBrian Somers bundle->idle.done = 0; 1316ab886ad0SBrian Somers } 1317ab886ad0SBrian Somers 131804eaa58cSBrian Somers static int 1319ab886ad0SBrian Somers bundle_RemainingIdleTime(struct bundle *bundle) 1320ab886ad0SBrian Somers { 132193ee0ff2SBrian Somers if (bundle->idle.done) 132293ee0ff2SBrian Somers return bundle->idle.done - time(NULL); 1323ab886ad0SBrian Somers return -1; 1324ab886ad0SBrian Somers } 13253b0f8d2eSBrian Somers 13263b0f8d2eSBrian Somers int 13273b0f8d2eSBrian Somers bundle_IsDead(struct bundle *bundle) 13283b0f8d2eSBrian Somers { 13293b0f8d2eSBrian Somers return !bundle->links || (bundle->phase == PHASE_DEAD && bundle->CleaningUp); 13303b0f8d2eSBrian Somers } 1331b6217683SBrian Somers 133204eaa58cSBrian Somers static struct datalink * 133304eaa58cSBrian Somers bundle_DatalinkLinkout(struct bundle *bundle, struct datalink *dl) 1334cd7bd93aSBrian Somers { 1335cd7bd93aSBrian Somers struct datalink **dlp; 1336cd7bd93aSBrian Somers 1337cd7bd93aSBrian Somers for (dlp = &bundle->links; *dlp; dlp = &(*dlp)->next) 1338cd7bd93aSBrian Somers if (*dlp == dl) { 133904eaa58cSBrian Somers *dlp = dl->next; 134004eaa58cSBrian Somers dl->next = NULL; 134104eaa58cSBrian Somers bundle_LinksRemoved(bundle); 134204eaa58cSBrian Somers return dl; 1343cd7bd93aSBrian Somers } 134404eaa58cSBrian Somers 134504eaa58cSBrian Somers return NULL; 134604eaa58cSBrian Somers } 134704eaa58cSBrian Somers 134804eaa58cSBrian Somers static void 134904eaa58cSBrian Somers bundle_DatalinkLinkin(struct bundle *bundle, struct datalink *dl) 135004eaa58cSBrian Somers { 135104eaa58cSBrian Somers struct datalink **dlp = &bundle->links; 135204eaa58cSBrian Somers 135304eaa58cSBrian Somers while (*dlp) 135404eaa58cSBrian Somers dlp = &(*dlp)->next; 135504eaa58cSBrian Somers 135604eaa58cSBrian Somers *dlp = dl; 135704eaa58cSBrian Somers dl->next = NULL; 135804eaa58cSBrian Somers 135904eaa58cSBrian Somers bundle_LinkAdded(bundle, dl); 1360565e35e5SBrian Somers } 1361565e35e5SBrian Somers 1362565e35e5SBrian Somers void 1363565e35e5SBrian Somers bundle_CleanDatalinks(struct bundle *bundle) 1364565e35e5SBrian Somers { 1365565e35e5SBrian Somers struct datalink **dlp = &bundle->links; 136604eaa58cSBrian Somers int found = 0; 1367565e35e5SBrian Somers 1368565e35e5SBrian Somers while (*dlp) 1369565e35e5SBrian Somers if ((*dlp)->state == DATALINK_CLOSED && 137081358fa3SBrian Somers (*dlp)->physical->type & (PHYS_DIRECT|PHYS_BACKGROUND)) { 1371565e35e5SBrian Somers *dlp = datalink_Destroy(*dlp); 137204eaa58cSBrian Somers found++; 137304eaa58cSBrian Somers } else 1374565e35e5SBrian Somers dlp = &(*dlp)->next; 137504eaa58cSBrian Somers 137604eaa58cSBrian Somers if (found) 137704eaa58cSBrian Somers bundle_LinksRemoved(bundle); 137804eaa58cSBrian Somers } 137904eaa58cSBrian Somers 138004eaa58cSBrian Somers int 138104eaa58cSBrian Somers bundle_DatalinkClone(struct bundle *bundle, struct datalink *dl, 138204eaa58cSBrian Somers const char *name) 138304eaa58cSBrian Somers { 138404eaa58cSBrian Somers if (bundle2datalink(bundle, name)) { 138504eaa58cSBrian Somers log_Printf(LogWARN, "Clone: %s: name already exists\n", name); 138604eaa58cSBrian Somers return 0; 138704eaa58cSBrian Somers } 138804eaa58cSBrian Somers 138904eaa58cSBrian Somers bundle_DatalinkLinkin(bundle, datalink_Clone(dl, name)); 139004eaa58cSBrian Somers return 1; 139104eaa58cSBrian Somers } 139204eaa58cSBrian Somers 139304eaa58cSBrian Somers void 139404eaa58cSBrian Somers bundle_DatalinkRemove(struct bundle *bundle, struct datalink *dl) 139504eaa58cSBrian Somers { 139604eaa58cSBrian Somers dl = bundle_DatalinkLinkout(bundle, dl); 139704eaa58cSBrian Somers if (dl) 139804eaa58cSBrian Somers datalink_Destroy(dl); 1399cd7bd93aSBrian Somers } 140049052c95SBrian Somers 140149052c95SBrian Somers void 140249052c95SBrian Somers bundle_SetLabel(struct bundle *bundle, const char *label) 140349052c95SBrian Somers { 140449052c95SBrian Somers if (label) 140549052c95SBrian Somers strncpy(bundle->cfg.label, label, sizeof bundle->cfg.label - 1); 140649052c95SBrian Somers else 140749052c95SBrian Somers *bundle->cfg.label = '\0'; 140849052c95SBrian Somers } 140949052c95SBrian Somers 141049052c95SBrian Somers const char * 141149052c95SBrian Somers bundle_GetLabel(struct bundle *bundle) 141249052c95SBrian Somers { 141349052c95SBrian Somers return *bundle->cfg.label ? bundle->cfg.label : NULL; 141449052c95SBrian Somers } 14151fa665f5SBrian Somers 14161fa665f5SBrian Somers void 141796c9bb21SBrian Somers bundle_ReceiveDatalink(struct bundle *bundle, int s, struct sockaddr_un *sun) 14181fa665f5SBrian Somers { 141996c9bb21SBrian Somers char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int)]; 142096c9bb21SBrian Somers struct cmsghdr *cmsg = (struct cmsghdr *)cmsgbuf; 142196c9bb21SBrian Somers struct msghdr msg; 142296c9bb21SBrian Somers struct iovec iov[SCATTER_SEGMENTS]; 1423b7c5748eSBrian Somers struct datalink *dl; 142496c9bb21SBrian Somers int niov, link_fd, expect, f; 142585fd273aSBrian Somers pid_t pid; 14266f384573SBrian Somers 1427dd7e2610SBrian Somers log_Printf(LogPHASE, "Receiving datalink\n"); 14286f384573SBrian Somers 142996c9bb21SBrian Somers /* Create our scatter/gather array */ 143096c9bb21SBrian Somers niov = 1; 143196c9bb21SBrian Somers iov[0].iov_len = strlen(Version) + 1; 143296c9bb21SBrian Somers iov[0].iov_base = (char *)malloc(iov[0].iov_len); 143385fd273aSBrian Somers if (datalink2iov(NULL, iov, &niov, sizeof iov / sizeof *iov, 0) == -1) { 143454cd8e13SBrian Somers close(s); 14356f384573SBrian Somers return; 143654cd8e13SBrian Somers } 14376f384573SBrian Somers 143885fd273aSBrian Somers pid = getpid(); 143985fd273aSBrian Somers write(s, &pid, sizeof pid); 144085fd273aSBrian Somers 144196c9bb21SBrian Somers for (f = expect = 0; f < niov; f++) 144296c9bb21SBrian Somers expect += iov[f].iov_len; 144396c9bb21SBrian Somers 144496c9bb21SBrian Somers /* Set up our message */ 144596c9bb21SBrian Somers cmsg->cmsg_len = sizeof cmsgbuf; 144696c9bb21SBrian Somers cmsg->cmsg_level = SOL_SOCKET; 144754cd8e13SBrian Somers cmsg->cmsg_type = 0; 144896c9bb21SBrian Somers 144996c9bb21SBrian Somers memset(&msg, '\0', sizeof msg); 145096c9bb21SBrian Somers msg.msg_name = (caddr_t)sun; 145196c9bb21SBrian Somers msg.msg_namelen = sizeof *sun; 145296c9bb21SBrian Somers msg.msg_iov = iov; 145396c9bb21SBrian Somers msg.msg_iovlen = niov; 145496c9bb21SBrian Somers msg.msg_control = cmsgbuf; 145596c9bb21SBrian Somers msg.msg_controllen = sizeof cmsgbuf; 145696c9bb21SBrian Somers 145796c9bb21SBrian Somers log_Printf(LogDEBUG, "Expecting %d scatter/gather bytes\n", expect); 145896c9bb21SBrian Somers f = expect + 100; 145996c9bb21SBrian Somers setsockopt(s, SOL_SOCKET, SO_RCVBUF, &f, sizeof f); 146096c9bb21SBrian Somers if ((f = recvmsg(s, &msg, MSG_WAITALL)) != expect) { 146196c9bb21SBrian Somers if (f == -1) 146296c9bb21SBrian Somers log_Printf(LogERROR, "Failed recvmsg: %s\n", strerror(errno)); 146396c9bb21SBrian Somers else 146496c9bb21SBrian Somers log_Printf(LogERROR, "Failed recvmsg: Got %d, not %d\n", f, expect); 146596c9bb21SBrian Somers while (niov--) 146696c9bb21SBrian Somers free(iov[niov].iov_base); 146754cd8e13SBrian Somers close(s); 146896c9bb21SBrian Somers return; 146996c9bb21SBrian Somers } 147096c9bb21SBrian Somers 1471c0d9a877SBrian Somers write(s, "!", 1); /* ACK */ 1472ad5b0e8bSBrian Somers close(s); 1473c0d9a877SBrian Somers 1474ad5b0e8bSBrian Somers if (cmsg->cmsg_type != SCM_RIGHTS) { 1475ad5b0e8bSBrian Somers log_Printf(LogERROR, "Recvmsg: no descriptor received !\n"); 1476ad5b0e8bSBrian Somers while (niov--) 1477ad5b0e8bSBrian Somers free(iov[niov].iov_base); 1478ad5b0e8bSBrian Somers return; 147954cd8e13SBrian Somers } 148054cd8e13SBrian Somers 1481ad5b0e8bSBrian Somers /* We've successfully received an open file descriptor through our socket */ 1482ad5b0e8bSBrian Somers log_Printf(LogDEBUG, "Receiving device descriptor\n"); 1483ad5b0e8bSBrian Somers link_fd = *(int *)CMSG_DATA(cmsg); 1484ad5b0e8bSBrian Somers 148596c9bb21SBrian Somers if (strncmp(Version, iov[0].iov_base, iov[0].iov_len)) { 148696c9bb21SBrian Somers log_Printf(LogWARN, "Cannot receive datalink, incorrect version" 148796c9bb21SBrian Somers " (\"%.*s\", not \"%s\")\n", (int)iov[0].iov_len, 14887d81ddf5SBrian Somers (char *)iov[0].iov_base, Version); 148996c9bb21SBrian Somers close(link_fd); 149096c9bb21SBrian Somers while (niov--) 149196c9bb21SBrian Somers free(iov[niov].iov_base); 149296c9bb21SBrian Somers return; 149396c9bb21SBrian Somers } 149496c9bb21SBrian Somers 149596c9bb21SBrian Somers niov = 1; 1496b7c5748eSBrian Somers dl = iov2datalink(bundle, iov, &niov, sizeof iov / sizeof *iov, link_fd); 1497b7c5748eSBrian Somers if (dl) { 149804eaa58cSBrian Somers bundle_DatalinkLinkin(bundle, dl); 1499b7c5748eSBrian Somers datalink_AuthOk(dl); 150096c9bb21SBrian Somers } else 150196c9bb21SBrian Somers close(link_fd); 150296c9bb21SBrian Somers 150396c9bb21SBrian Somers free(iov[0].iov_base); 15041fa665f5SBrian Somers } 15051fa665f5SBrian Somers 15061fa665f5SBrian Somers void 150796c9bb21SBrian Somers bundle_SendDatalink(struct datalink *dl, int s, struct sockaddr_un *sun) 15081fa665f5SBrian Somers { 1509c0d9a877SBrian Somers char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int)], ack; 151096c9bb21SBrian Somers struct cmsghdr *cmsg = (struct cmsghdr *)cmsgbuf; 151196c9bb21SBrian Somers struct msghdr msg; 151296c9bb21SBrian Somers struct iovec iov[SCATTER_SEGMENTS]; 15131384bd27SBrian Somers int niov, link_fd, f, expect, newsid; 151485fd273aSBrian Somers pid_t newpid; 15156f384573SBrian Somers 1516dd7e2610SBrian Somers log_Printf(LogPHASE, "Transmitting datalink %s\n", dl->name); 15176f384573SBrian Somers 151804eaa58cSBrian Somers bundle_LinkClosed(dl->bundle, dl); 15190f2f3eb3SBrian Somers bundle_DatalinkLinkout(dl->bundle, dl); 1520ea722969SBrian Somers 152196c9bb21SBrian Somers /* Build our scatter/gather array */ 152296c9bb21SBrian Somers iov[0].iov_len = strlen(Version) + 1; 152396c9bb21SBrian Somers iov[0].iov_base = strdup(Version); 152496c9bb21SBrian Somers niov = 1; 15256f384573SBrian Somers 152685fd273aSBrian Somers read(s, &newpid, sizeof newpid); 152785fd273aSBrian Somers link_fd = datalink2iov(dl, iov, &niov, sizeof iov / sizeof *iov, newpid); 15286f384573SBrian Somers 15296f384573SBrian Somers if (link_fd != -1) { 153096c9bb21SBrian Somers memset(&msg, '\0', sizeof msg); 153154cd8e13SBrian Somers 153296c9bb21SBrian Somers msg.msg_name = (caddr_t)sun; 153396c9bb21SBrian Somers msg.msg_namelen = sizeof *sun; 153496c9bb21SBrian Somers msg.msg_iov = iov; 153596c9bb21SBrian Somers msg.msg_iovlen = niov; 153654cd8e13SBrian Somers 153754cd8e13SBrian Somers cmsg->cmsg_len = sizeof cmsgbuf; 153854cd8e13SBrian Somers cmsg->cmsg_level = SOL_SOCKET; 153954cd8e13SBrian Somers cmsg->cmsg_type = SCM_RIGHTS; 154054cd8e13SBrian Somers *(int *)CMSG_DATA(cmsg) = link_fd; 154196c9bb21SBrian Somers msg.msg_control = cmsgbuf; 154296c9bb21SBrian Somers msg.msg_controllen = sizeof cmsgbuf; 154347723d29SBrian Somers 154496c9bb21SBrian Somers for (f = expect = 0; f < niov; f++) 154596c9bb21SBrian Somers expect += iov[f].iov_len; 154647723d29SBrian Somers 154796c9bb21SBrian Somers log_Printf(LogDEBUG, "Sending %d bytes in scatter/gather array\n", expect); 154847723d29SBrian Somers 154996c9bb21SBrian Somers f = expect + SOCKET_OVERHEAD; 155096c9bb21SBrian Somers setsockopt(s, SOL_SOCKET, SO_SNDBUF, &f, sizeof f); 155196c9bb21SBrian Somers if (sendmsg(s, &msg, 0) == -1) 155296c9bb21SBrian Somers log_Printf(LogERROR, "Failed sendmsg: %s\n", strerror(errno)); 1553c0d9a877SBrian Somers /* We must get the ACK before closing the descriptor ! */ 1554c0d9a877SBrian Somers read(s, &ack, 1); 155554cd8e13SBrian Somers 15561384bd27SBrian Somers newsid = tcgetpgrp(link_fd) == getpgrp(); 155796c9bb21SBrian Somers close(link_fd); 15581384bd27SBrian Somers if (newsid) 15591384bd27SBrian Somers bundle_setsid(dl->bundle, 1); 156047723d29SBrian Somers } 156185fd273aSBrian Somers close(s); 156296c9bb21SBrian Somers 156396c9bb21SBrian Somers while (niov--) 156496c9bb21SBrian Somers free(iov[niov].iov_base); 15651fa665f5SBrian Somers } 1566dd0645c5SBrian Somers 1567dd0645c5SBrian Somers int 156858d55334SBrian Somers bundle_RenameDatalink(struct bundle *bundle, struct datalink *ndl, 156958d55334SBrian Somers const char *name) 157058d55334SBrian Somers { 157158d55334SBrian Somers struct datalink *dl; 157258d55334SBrian Somers 157358d55334SBrian Somers if (!strcasecmp(ndl->name, name)) 157458d55334SBrian Somers return 1; 157558d55334SBrian Somers 157658d55334SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 157758d55334SBrian Somers if (!strcasecmp(dl->name, name)) 157858d55334SBrian Somers return 0; 157958d55334SBrian Somers 158058d55334SBrian Somers datalink_Rename(ndl, name); 158158d55334SBrian Somers return 1; 158258d55334SBrian Somers } 158358d55334SBrian Somers 158458d55334SBrian Somers int 1585dd0645c5SBrian Somers bundle_SetMode(struct bundle *bundle, struct datalink *dl, int mode) 1586dd0645c5SBrian Somers { 1587dd0645c5SBrian Somers int omode; 1588dd0645c5SBrian Somers 1589dd0645c5SBrian Somers omode = dl->physical->type; 1590dd0645c5SBrian Somers if (omode == mode) 1591dd0645c5SBrian Somers return 1; 1592dd0645c5SBrian Somers 1593ff0f9439SBrian Somers if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO)) 1594ff0f9439SBrian Somers /* First auto link */ 1595dd0645c5SBrian Somers if (bundle->ncp.ipcp.peer_ip.s_addr == INADDR_ANY) { 1596ff0f9439SBrian Somers log_Printf(LogWARN, "You must `set ifaddr' or `open' before" 1597ff0f9439SBrian Somers " changing mode to %s\n", mode2Nam(mode)); 1598dd0645c5SBrian Somers return 0; 1599dd0645c5SBrian Somers } 1600dd0645c5SBrian Somers 1601dd0645c5SBrian Somers if (!datalink_SetMode(dl, mode)) 1602dd0645c5SBrian Somers return 0; 1603dd0645c5SBrian Somers 1604ff0f9439SBrian Somers if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO) && 1605ff0f9439SBrian Somers bundle->phase != PHASE_NETWORK) 1606ff0f9439SBrian Somers /* First auto link, we need an interface */ 1607dd0645c5SBrian Somers ipcp_InterfaceUp(&bundle->ncp.ipcp); 1608dd0645c5SBrian Somers 160904eaa58cSBrian Somers /* Regenerate phys_type and adjust autoload & idle timers */ 161004eaa58cSBrian Somers bundle_LinksRemoved(bundle); 1611dd0645c5SBrian Somers 1612dd0645c5SBrian Somers return 1; 1613dd0645c5SBrian Somers } 16141384bd27SBrian Somers 16151384bd27SBrian Somers void 16161384bd27SBrian Somers bundle_setsid(struct bundle *bundle, int holdsession) 16171384bd27SBrian Somers { 16181384bd27SBrian Somers /* 16191384bd27SBrian Somers * Lose the current session. This means getting rid of our pid 16201384bd27SBrian Somers * too so that the tty device will really go away, and any getty 16211384bd27SBrian Somers * etc will be allowed to restart. 16221384bd27SBrian Somers */ 16231384bd27SBrian Somers pid_t pid, orig; 16241384bd27SBrian Somers int fds[2]; 16251384bd27SBrian Somers char done; 16261384bd27SBrian Somers struct datalink *dl; 16271384bd27SBrian Somers 16281384bd27SBrian Somers orig = getpid(); 16291384bd27SBrian Somers if (pipe(fds) == -1) { 16301384bd27SBrian Somers log_Printf(LogERROR, "pipe: %s\n", strerror(errno)); 16311384bd27SBrian Somers return; 16321384bd27SBrian Somers } 16331384bd27SBrian Somers switch ((pid = fork())) { 16341384bd27SBrian Somers case -1: 16351384bd27SBrian Somers log_Printf(LogERROR, "fork: %s\n", strerror(errno)); 16361384bd27SBrian Somers close(fds[0]); 16371384bd27SBrian Somers close(fds[1]); 16381384bd27SBrian Somers return; 16391384bd27SBrian Somers case 0: 16401384bd27SBrian Somers close(fds[0]); 16411384bd27SBrian Somers read(fds[1], &done, 1); /* uu_locks are mine ! */ 16421384bd27SBrian Somers close(fds[1]); 16431384bd27SBrian Somers if (pipe(fds) == -1) { 16441384bd27SBrian Somers log_Printf(LogERROR, "pipe(2): %s\n", strerror(errno)); 16451384bd27SBrian Somers return; 16461384bd27SBrian Somers } 16471384bd27SBrian Somers switch ((pid = fork())) { 16481384bd27SBrian Somers case -1: 1649a33b2ef7SBrian Somers log_Printf(LogERROR, "fork(2): %s\n", strerror(errno)); 16501384bd27SBrian Somers close(fds[0]); 16511384bd27SBrian Somers close(fds[1]); 16521384bd27SBrian Somers return; 16531384bd27SBrian Somers case 0: 16541384bd27SBrian Somers close(fds[0]); 16551384bd27SBrian Somers bundle_LockTun(bundle); /* update pid */ 16561384bd27SBrian Somers read(fds[1], &done, 1); /* uu_locks are mine ! */ 16571384bd27SBrian Somers close(fds[1]); 16581384bd27SBrian Somers setsid(); 16591384bd27SBrian Somers log_Printf(LogPHASE, "%d -> %d: %s session control\n", 16601384bd27SBrian Somers (int)orig, (int)getpid(), 16611384bd27SBrian Somers holdsession ? "Passed" : "Dropped"); 1662d2a69fc3SBrian Somers timer_InitService(); 16631384bd27SBrian Somers break; 16641384bd27SBrian Somers default: 16651384bd27SBrian Somers close(fds[1]); 16661384bd27SBrian Somers /* Give away all our modem locks (to the final process) */ 16671384bd27SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 16681384bd27SBrian Somers if (dl->state != DATALINK_CLOSED) 16691384bd27SBrian Somers modem_ChangedPid(dl->physical, pid); 16701384bd27SBrian Somers write(fds[0], "!", 1); /* done */ 16711384bd27SBrian Somers close(fds[0]); 16721384bd27SBrian Somers exit(0); 16731384bd27SBrian Somers break; 16741384bd27SBrian Somers } 16751384bd27SBrian Somers break; 16761384bd27SBrian Somers default: 16771384bd27SBrian Somers close(fds[1]); 16781384bd27SBrian Somers /* Give away all our modem locks (to the intermediate process) */ 16791384bd27SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 16801384bd27SBrian Somers if (dl->state != DATALINK_CLOSED) 16811384bd27SBrian Somers modem_ChangedPid(dl->physical, pid); 16821384bd27SBrian Somers write(fds[0], "!", 1); /* done */ 16831384bd27SBrian Somers close(fds[0]); 16841384bd27SBrian Somers if (holdsession) { 16851384bd27SBrian Somers int fd, status; 16861384bd27SBrian Somers 16871384bd27SBrian Somers timer_TermService(); 16881384bd27SBrian Somers signal(SIGPIPE, SIG_DFL); 16891384bd27SBrian Somers signal(SIGALRM, SIG_DFL); 16901384bd27SBrian Somers signal(SIGHUP, SIG_DFL); 16911384bd27SBrian Somers signal(SIGTERM, SIG_DFL); 16921384bd27SBrian Somers signal(SIGINT, SIG_DFL); 16931384bd27SBrian Somers signal(SIGQUIT, SIG_DFL); 16941384bd27SBrian Somers for (fd = getdtablesize(); fd >= 0; fd--) 16951384bd27SBrian Somers close(fd); 16961384bd27SBrian Somers setuid(geteuid()); 16971384bd27SBrian Somers /* 16981384bd27SBrian Somers * Reap the intermediate process. As we're not exiting but the 16991384bd27SBrian Somers * intermediate is, we don't want it to become defunct. 17001384bd27SBrian Somers */ 17011384bd27SBrian Somers waitpid(pid, &status, 0); 17028e7b8599SBrian Somers /* Tweak our process arguments.... */ 17038e7b8599SBrian Somers bundle->argv[0] = "session owner"; 17048e7b8599SBrian Somers bundle->argv[1] = NULL; 17051384bd27SBrian Somers /* 17061384bd27SBrian Somers * Hang around for a HUP. This should happen as soon as the 17071384bd27SBrian Somers * ppp that we passed our ctty descriptor to closes it. 17081384bd27SBrian Somers * NOTE: If this process dies, the passed descriptor becomes 17091384bd27SBrian Somers * invalid and will give a select() error by setting one 17101384bd27SBrian Somers * of the error fds, aborting the other ppp. We don't 17111384bd27SBrian Somers * want that to happen ! 17121384bd27SBrian Somers */ 17131384bd27SBrian Somers pause(); 17141384bd27SBrian Somers } 17151384bd27SBrian Somers exit(0); 17161384bd27SBrian Somers break; 17171384bd27SBrian Somers } 17181384bd27SBrian Somers } 17199b5f8ffdSBrian Somers 17209b5f8ffdSBrian Somers int 17219b5f8ffdSBrian Somers bundle_HighestState(struct bundle *bundle) 17229b5f8ffdSBrian Somers { 17239b5f8ffdSBrian Somers struct datalink *dl; 17249b5f8ffdSBrian Somers int result = DATALINK_CLOSED; 17259b5f8ffdSBrian Somers 17269b5f8ffdSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 17279b5f8ffdSBrian Somers if (result < dl->state) 17289b5f8ffdSBrian Somers result = dl->state; 17299b5f8ffdSBrian Somers 17309b5f8ffdSBrian Somers return result; 17319b5f8ffdSBrian Somers } 1732991c2a7bSBrian Somers 1733991c2a7bSBrian Somers int 1734991c2a7bSBrian Somers bundle_Exception(struct bundle *bundle, int fd) 1735991c2a7bSBrian Somers { 1736991c2a7bSBrian Somers struct datalink *dl; 1737991c2a7bSBrian Somers 1738991c2a7bSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 1739991c2a7bSBrian Somers if (dl->physical->fd == fd) { 1740991c2a7bSBrian Somers datalink_Down(dl, CLOSE_NORMAL); 1741991c2a7bSBrian Somers return 1; 1742991c2a7bSBrian Somers } 1743991c2a7bSBrian Somers 1744991c2a7bSBrian Somers return 0; 1745991c2a7bSBrian Somers } 1746