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 * 267d81ddf5SBrian Somers * $Id: bundle.c,v 1.24 1998/06/27 12:03:35 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> 4354cd8e13SBrian Somers #include <signal.h> 447a6f8720SBrian Somers #include <stdio.h> 456f384573SBrian Somers #include <stdlib.h> 467a6f8720SBrian Somers #include <string.h> 477a6f8720SBrian Somers #include <sys/ioctl.h> 4896c9bb21SBrian Somers #include <sys/uio.h> 4954cd8e13SBrian Somers #include <sys/wait.h> 507a6f8720SBrian Somers #include <termios.h> 517a6f8720SBrian Somers #include <unistd.h> 527a6f8720SBrian Somers 53c9e11a11SBrian Somers #include "defs.h" 547a6f8720SBrian Somers #include "command.h" 557a6f8720SBrian Somers #include "mbuf.h" 567a6f8720SBrian Somers #include "log.h" 577a6f8720SBrian Somers #include "id.h" 587a6f8720SBrian Somers #include "timer.h" 597a6f8720SBrian Somers #include "fsm.h" 607a6f8720SBrian Somers #include "iplist.h" 61879ed6faSBrian Somers #include "lqr.h" 62455aabc3SBrian Somers #include "hdlc.h" 637a6f8720SBrian Somers #include "throughput.h" 64eaa4df37SBrian Somers #include "slcompress.h" 657a6f8720SBrian Somers #include "ipcp.h" 665ca5389aSBrian Somers #include "filter.h" 672f786681SBrian Somers #include "descriptor.h" 687a6f8720SBrian Somers #include "route.h" 697a6f8720SBrian Somers #include "lcp.h" 707a6f8720SBrian Somers #include "ccp.h" 713b0f8d2eSBrian Somers #include "link.h" 723b0f8d2eSBrian Somers #include "mp.h" 733b0f8d2eSBrian Somers #include "bundle.h" 74455aabc3SBrian Somers #include "async.h" 75455aabc3SBrian Somers #include "physical.h" 762289f246SBrian Somers #include "modem.h" 77078c562eSBrian Somers #include "loadalias.h" 78455aabc3SBrian Somers #include "auth.h" 79455aabc3SBrian Somers #include "lcpproto.h" 80455aabc3SBrian Somers #include "chap.h" 81455aabc3SBrian Somers #include "tun.h" 8285b542cfSBrian Somers #include "prompt.h" 833006ec67SBrian Somers #include "chat.h" 843006ec67SBrian Somers #include "datalink.h" 853006ec67SBrian Somers #include "ip.h" 867a6f8720SBrian Somers 8796c9bb21SBrian Somers #define SCATTER_SEGMENTS 4 /* version, datalink, name, physical */ 8896c9bb21SBrian Somers #define SOCKET_OVERHEAD 100 /* additional buffer space for large */ 8996c9bb21SBrian Somers /* {recv,send}msg() calls */ 9096c9bb21SBrian Somers 9104eaa58cSBrian Somers static int bundle_RemainingIdleTime(struct bundle *); 9204eaa58cSBrian Somers static int bundle_RemainingAutoLoadTime(struct bundle *); 9304eaa58cSBrian Somers 94455aabc3SBrian Somers static const char *PhaseNames[] = { 95455aabc3SBrian Somers "Dead", "Establish", "Authenticate", "Network", "Terminate" 96455aabc3SBrian Somers }; 97455aabc3SBrian Somers 98455aabc3SBrian Somers const char * 99455aabc3SBrian Somers bundle_PhaseName(struct bundle *bundle) 1007a6f8720SBrian Somers { 101455aabc3SBrian Somers return bundle->phase <= PHASE_TERMINATE ? 102455aabc3SBrian Somers PhaseNames[bundle->phase] : "unknown"; 1037a6f8720SBrian Somers } 1047a6f8720SBrian Somers 105455aabc3SBrian Somers void 1065563ebdeSBrian Somers bundle_NewPhase(struct bundle *bundle, u_int new) 107455aabc3SBrian Somers { 108aef795ccSBrian Somers if (new == bundle->phase) 109aef795ccSBrian Somers return; 110aef795ccSBrian Somers 111e2ebb036SBrian Somers if (new <= PHASE_TERMINATE) 112dd7e2610SBrian Somers log_Printf(LogPHASE, "bundle: %s\n", PhaseNames[new]); 1137a6f8720SBrian Somers 114455aabc3SBrian Somers switch (new) { 115455aabc3SBrian Somers case PHASE_DEAD: 1160f2f3eb3SBrian Somers log_DisplayPrompts(); 117455aabc3SBrian Somers bundle->phase = new; 118455aabc3SBrian Somers break; 119455aabc3SBrian Somers 120455aabc3SBrian Somers case PHASE_ESTABLISH: 121455aabc3SBrian Somers bundle->phase = new; 122455aabc3SBrian Somers break; 123455aabc3SBrian Somers 124455aabc3SBrian Somers case PHASE_AUTHENTICATE: 125455aabc3SBrian Somers bundle->phase = new; 1260f2f3eb3SBrian Somers log_DisplayPrompts(); 127455aabc3SBrian Somers break; 128455aabc3SBrian Somers 129455aabc3SBrian Somers case PHASE_NETWORK: 1305828db6dSBrian Somers ipcp_Setup(&bundle->ncp.ipcp); 131dd7e2610SBrian Somers fsm_Up(&bundle->ncp.ipcp.fsm); 132dd7e2610SBrian Somers fsm_Open(&bundle->ncp.ipcp.fsm); 133673903ecSBrian Somers bundle->phase = new; 1340f2f3eb3SBrian Somers log_DisplayPrompts(); 135673903ecSBrian Somers break; 136455aabc3SBrian Somers 137455aabc3SBrian Somers case PHASE_TERMINATE: 138455aabc3SBrian Somers bundle->phase = new; 139673903ecSBrian Somers mp_Down(&bundle->ncp.mp); 1400f2f3eb3SBrian Somers log_DisplayPrompts(); 141455aabc3SBrian Somers break; 1427a6f8720SBrian Somers } 1437a6f8720SBrian Somers } 1447a6f8720SBrian Somers 1457a6f8720SBrian Somers static int 1467a6f8720SBrian Somers bundle_CleanInterface(const struct bundle *bundle) 1477a6f8720SBrian Somers { 1487a6f8720SBrian Somers int s; 1497a6f8720SBrian Somers struct ifreq ifrq; 1507a6f8720SBrian Somers struct ifaliasreq ifra; 1517a6f8720SBrian Somers 1527a6f8720SBrian Somers s = ID0socket(AF_INET, SOCK_DGRAM, 0); 1537a6f8720SBrian Somers if (s < 0) { 154dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_CleanInterface: socket(): %s\n", 1557a6f8720SBrian Somers strerror(errno)); 1567a6f8720SBrian Somers return (-1); 1577a6f8720SBrian Somers } 158faefde08SBrian Somers strncpy(ifrq.ifr_name, bundle->ifp.Name, sizeof ifrq.ifr_name - 1); 1597a6f8720SBrian Somers ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0'; 1607a6f8720SBrian Somers while (ID0ioctl(s, SIOCGIFADDR, &ifrq) == 0) { 1617a6f8720SBrian Somers memset(&ifra.ifra_mask, '\0', sizeof ifra.ifra_mask); 162faefde08SBrian Somers strncpy(ifra.ifra_name, bundle->ifp.Name, sizeof ifra.ifra_name - 1); 1637a6f8720SBrian Somers ifra.ifra_name[sizeof ifra.ifra_name - 1] = '\0'; 1647a6f8720SBrian Somers ifra.ifra_addr = ifrq.ifr_addr; 1657a6f8720SBrian Somers if (ID0ioctl(s, SIOCGIFDSTADDR, &ifrq) < 0) { 1667a6f8720SBrian Somers if (ifra.ifra_addr.sa_family == AF_INET) 167a33b2ef7SBrian Somers log_Printf(LogERROR, "Can't get dst for %s on %s !\n", 1687a6f8720SBrian Somers inet_ntoa(((struct sockaddr_in *)&ifra.ifra_addr)->sin_addr), 169faefde08SBrian Somers bundle->ifp.Name); 1700f8037a9SBrian Somers close(s); 1717a6f8720SBrian Somers return 0; 1727a6f8720SBrian Somers } 1737a6f8720SBrian Somers ifra.ifra_broadaddr = ifrq.ifr_dstaddr; 1747a6f8720SBrian Somers if (ID0ioctl(s, SIOCDIFADDR, &ifra) < 0) { 1757a6f8720SBrian Somers if (ifra.ifra_addr.sa_family == AF_INET) 176a33b2ef7SBrian Somers log_Printf(LogERROR, "Can't delete %s address on %s !\n", 1777a6f8720SBrian Somers inet_ntoa(((struct sockaddr_in *)&ifra.ifra_addr)->sin_addr), 178faefde08SBrian Somers bundle->ifp.Name); 1790f8037a9SBrian Somers close(s); 1807a6f8720SBrian Somers return 0; 1817a6f8720SBrian Somers } 1827a6f8720SBrian Somers } 1830f8037a9SBrian Somers close(s); 1847a6f8720SBrian Somers 1857a6f8720SBrian Somers return 1; 1867a6f8720SBrian Somers } 1877a6f8720SBrian Somers 1886d666775SBrian Somers static void 1896d666775SBrian Somers bundle_LayerStart(void *v, struct fsm *fp) 1907a6f8720SBrian Somers { 1913006ec67SBrian Somers /* The given FSM is about to start up ! */ 1927a6f8720SBrian Somers } 1937a6f8720SBrian Somers 1945cf4388bSBrian Somers 1955cf4388bSBrian Somers static void 1965cf4388bSBrian Somers bundle_Notify(struct bundle *bundle, char c) 1975cf4388bSBrian Somers { 1985cf4388bSBrian Somers if (bundle->notify.fd != -1) { 1995cf4388bSBrian Somers if (write(bundle->notify.fd, &c, 1) == 1) 200dd7e2610SBrian Somers log_Printf(LogPHASE, "Parent notified of success.\n"); 2015cf4388bSBrian Somers else 202dd7e2610SBrian Somers log_Printf(LogPHASE, "Failed to notify parent of success.\n"); 2035cf4388bSBrian Somers close(bundle->notify.fd); 2045cf4388bSBrian Somers bundle->notify.fd = -1; 2055cf4388bSBrian Somers } 2065cf4388bSBrian Somers } 2073b0f8d2eSBrian Somers 2086d666775SBrian Somers static void 20904eaa58cSBrian Somers bundle_AutoLoadTimeout(void *v) 21004eaa58cSBrian Somers { 21104eaa58cSBrian Somers struct bundle *bundle = (struct bundle *)v; 21204eaa58cSBrian Somers 21304eaa58cSBrian Somers if (bundle->autoload.comingup) { 21404eaa58cSBrian Somers log_Printf(LogPHASE, "autoload: Another link is required\n"); 21504eaa58cSBrian Somers /* bundle_Open() stops the timer */ 21681358fa3SBrian Somers bundle_Open(bundle, NULL, PHYS_AUTO); 21704eaa58cSBrian Somers } else { 21804eaa58cSBrian Somers struct datalink *dl, *last; 21904eaa58cSBrian Somers 22004eaa58cSBrian Somers timer_Stop(&bundle->autoload.timer); 22104eaa58cSBrian Somers for (last = NULL, dl = bundle->links; dl; dl = dl->next) 22281358fa3SBrian Somers if (dl->physical->type == PHYS_AUTO && dl->state == DATALINK_OPEN) 22304eaa58cSBrian Somers last = dl; 22404eaa58cSBrian Somers 22504eaa58cSBrian Somers if (last) 2269c81b87dSBrian Somers datalink_Close(last, CLOSE_STAYDOWN); 22704eaa58cSBrian Somers } 22804eaa58cSBrian Somers } 22904eaa58cSBrian Somers 23004eaa58cSBrian Somers static void 23104eaa58cSBrian Somers bundle_StartAutoLoadTimer(struct bundle *bundle, int up) 23204eaa58cSBrian Somers { 23304eaa58cSBrian Somers struct datalink *dl; 23404eaa58cSBrian Somers 23504eaa58cSBrian Somers timer_Stop(&bundle->autoload.timer); 23604eaa58cSBrian Somers 23704eaa58cSBrian Somers if (bundle->CleaningUp || bundle->phase != PHASE_NETWORK) { 23804eaa58cSBrian Somers dl = NULL; 23904eaa58cSBrian Somers bundle->autoload.running = 0; 24004eaa58cSBrian Somers } else if (up) { 24104eaa58cSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 24281358fa3SBrian Somers if (dl->state == DATALINK_CLOSED && dl->physical->type == PHYS_AUTO) { 24304eaa58cSBrian Somers if (bundle->cfg.autoload.max.timeout) { 24404eaa58cSBrian Somers bundle->autoload.timer.func = bundle_AutoLoadTimeout; 24504eaa58cSBrian Somers bundle->autoload.timer.name = "autoload up"; 24604eaa58cSBrian Somers bundle->autoload.timer.load = 24704eaa58cSBrian Somers bundle->cfg.autoload.max.timeout * SECTICKS; 24804eaa58cSBrian Somers bundle->autoload.timer.arg = bundle; 24904eaa58cSBrian Somers timer_Start(&bundle->autoload.timer); 25004eaa58cSBrian Somers bundle->autoload.done = time(NULL) + bundle->cfg.autoload.max.timeout; 25104eaa58cSBrian Somers } else 25204eaa58cSBrian Somers bundle_AutoLoadTimeout(bundle); 25304eaa58cSBrian Somers break; 25404eaa58cSBrian Somers } 25504eaa58cSBrian Somers bundle->autoload.running = (dl || bundle->cfg.autoload.min.timeout) ? 1 : 0; 25604eaa58cSBrian Somers } else { 25704eaa58cSBrian Somers int nlinks; 25804eaa58cSBrian Somers struct datalink *adl; 25904eaa58cSBrian Somers 26004eaa58cSBrian Somers for (nlinks = 0, adl = NULL, dl = bundle->links; dl; dl = dl->next) 26104eaa58cSBrian Somers if (dl->state == DATALINK_OPEN) { 26281358fa3SBrian Somers if (dl->physical->type == PHYS_AUTO) 26304eaa58cSBrian Somers adl = dl; 26404eaa58cSBrian Somers if (++nlinks > 1 && adl) { 26504eaa58cSBrian Somers if (bundle->cfg.autoload.min.timeout) { 26604eaa58cSBrian Somers bundle->autoload.timer.func = bundle_AutoLoadTimeout; 26704eaa58cSBrian Somers bundle->autoload.timer.name = "autoload down"; 26804eaa58cSBrian Somers bundle->autoload.timer.load = 26904eaa58cSBrian Somers bundle->cfg.autoload.min.timeout * SECTICKS; 27004eaa58cSBrian Somers bundle->autoload.timer.arg = bundle; 27104eaa58cSBrian Somers timer_Start(&bundle->autoload.timer); 27204eaa58cSBrian Somers bundle->autoload.done = 27304eaa58cSBrian Somers time(NULL) + bundle->cfg.autoload.min.timeout; 27404eaa58cSBrian Somers } 27504eaa58cSBrian Somers break; 27604eaa58cSBrian Somers } 27704eaa58cSBrian Somers } 27804eaa58cSBrian Somers 27904eaa58cSBrian Somers bundle->autoload.running = 1; 28004eaa58cSBrian Somers } 28104eaa58cSBrian Somers 28204eaa58cSBrian Somers bundle->autoload.comingup = up ? 1 : 0; 28304eaa58cSBrian Somers } 28404eaa58cSBrian Somers 28504eaa58cSBrian Somers static void 28604eaa58cSBrian Somers bundle_StopAutoLoadTimer(struct bundle *bundle) 28704eaa58cSBrian Somers { 28804eaa58cSBrian Somers timer_Stop(&bundle->autoload.timer); 28904eaa58cSBrian Somers bundle->autoload.done = 0; 29004eaa58cSBrian Somers } 29104eaa58cSBrian Somers 29204eaa58cSBrian Somers static int 29304eaa58cSBrian Somers bundle_RemainingAutoLoadTime(struct bundle *bundle) 29404eaa58cSBrian Somers { 29504eaa58cSBrian Somers if (bundle->autoload.done) 29604eaa58cSBrian Somers return bundle->autoload.done - time(NULL); 29704eaa58cSBrian Somers return -1; 29804eaa58cSBrian Somers } 29904eaa58cSBrian Somers 300ff0f9439SBrian Somers static void 301ff0f9439SBrian Somers bundle_LinkAdded(struct bundle *bundle, struct datalink *dl) 302ff0f9439SBrian Somers { 303ff0f9439SBrian Somers bundle->phys_type.all |= dl->physical->type; 304ff0f9439SBrian Somers if (dl->state == DATALINK_OPEN) 305ff0f9439SBrian Somers bundle->phys_type.open |= dl->physical->type; 306ff0f9439SBrian Somers 307ff0f9439SBrian Somers /* Note: We only re-add links that are DATALINK_OPEN */ 308ff0f9439SBrian Somers if (dl->physical->type == PHYS_AUTO && 309ff0f9439SBrian Somers bundle->autoload.timer.state == TIMER_STOPPED && 310ff0f9439SBrian Somers dl->state != DATALINK_OPEN && 311ff0f9439SBrian Somers bundle->phase == PHASE_NETWORK) 312ff0f9439SBrian Somers bundle->autoload.running = 1; 313ff0f9439SBrian Somers 314ff0f9439SBrian Somers if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) 315ff0f9439SBrian Somers != bundle->phys_type.open && bundle->idle.timer.state == TIMER_STOPPED) 316ff0f9439SBrian Somers /* We may need to start our idle timer */ 317ff0f9439SBrian Somers bundle_StartIdleTimer(bundle); 318ff0f9439SBrian Somers } 319ff0f9439SBrian Somers 320ff0f9439SBrian Somers static void 321ff0f9439SBrian Somers bundle_LinksRemoved(struct bundle *bundle) 322ff0f9439SBrian Somers { 323ff0f9439SBrian Somers struct datalink *dl; 324ff0f9439SBrian Somers 325ff0f9439SBrian Somers bundle->phys_type.all = bundle->phys_type.open = 0; 326ff0f9439SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 327ff0f9439SBrian Somers bundle_LinkAdded(bundle, dl); 328ff0f9439SBrian Somers 329ff0f9439SBrian Somers if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) 330ff0f9439SBrian Somers == bundle->phys_type.open) 331ff0f9439SBrian Somers bundle_StopIdleTimer(bundle); 332ff0f9439SBrian Somers } 33304eaa58cSBrian Somers 33404eaa58cSBrian Somers static void 3356f384573SBrian Somers bundle_LayerUp(void *v, struct fsm *fp) 3367a6f8720SBrian Somers { 3373006ec67SBrian Somers /* 3383006ec67SBrian Somers * The given fsm is now up 339ff0f9439SBrian Somers * If it's an LCP, adjust our phys_mode.open value. 34049052c95SBrian Somers * If it's an LCP set our mtu (if we're multilink, add up the link 34104eaa58cSBrian Somers * speeds and set the MRRU) and start our autoload timer. 342565e35e5SBrian Somers * If it's an NCP, tell our -background parent to go away. 3433b0f8d2eSBrian Somers * If it's the first NCP, start the idle timer. 3443006ec67SBrian Somers */ 3456f384573SBrian Somers struct bundle *bundle = (struct bundle *)v; 3466d666775SBrian Somers 3475563ebdeSBrian Somers if (fp->proto == PROTO_LCP) { 348ff0f9439SBrian Somers struct physical *p = link2physical(fp->link); 349ff0f9439SBrian Somers 350ff0f9439SBrian Somers bundle_LinkAdded(bundle, p->dl); 3513b0f8d2eSBrian Somers if (bundle->ncp.mp.active) { 3523b0f8d2eSBrian Somers struct datalink *dl; 3535563ebdeSBrian Somers 354faefde08SBrian Somers bundle->ifp.Speed = 0; 355faefde08SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 356eeab6bf5SBrian Somers if (dl->state == DATALINK_OPEN) 357faefde08SBrian Somers bundle->ifp.Speed += modem_Speed(dl->physical); 358faefde08SBrian Somers tun_configure(bundle, bundle->ncp.mp.peer_mrru); 35904eaa58cSBrian Somers bundle->autoload.running = 1; 360faefde08SBrian Somers } else { 361ff0f9439SBrian Somers bundle->ifp.Speed = modem_Speed(p); 362faefde08SBrian Somers tun_configure(bundle, fsm2lcp(fp)->his_mru); 363faefde08SBrian Somers } 3643b0f8d2eSBrian Somers } else if (fp->proto == PROTO_IPCP) { 365ab886ad0SBrian Somers bundle_StartIdleTimer(bundle); 3665cf4388bSBrian Somers bundle_Notify(bundle, EX_NORMAL); 3677a6f8720SBrian Somers } 368ab886ad0SBrian Somers } 3697a6f8720SBrian Somers 3706d666775SBrian Somers static void 3716d666775SBrian Somers bundle_LayerDown(void *v, struct fsm *fp) 3726d666775SBrian Somers { 3736d666775SBrian Somers /* 3746d666775SBrian Somers * The given FSM has been told to come down. 375ab886ad0SBrian Somers * If it's our last NCP, stop the idle timer. 376ff0f9439SBrian Somers * If it's an LCP, adjust our phys_type.open value and any timers. 37786b1f0d7SBrian Somers * If it's an LCP and we're in multilink mode, adjust our tun 37886b1f0d7SBrian Somers * speed and make sure our minimum sequence number is adjusted. 3796d666775SBrian Somers */ 380ab886ad0SBrian Somers 381ab886ad0SBrian Somers struct bundle *bundle = (struct bundle *)v; 382ab886ad0SBrian Somers 383078c562eSBrian Somers if (fp->proto == PROTO_IPCP) 384ab886ad0SBrian Somers bundle_StopIdleTimer(bundle); 385ff0f9439SBrian Somers else if (fp->proto == PROTO_LCP) { 386ff0f9439SBrian Somers bundle_LinksRemoved(bundle); /* adjust timers & phys_type values */ 387ff0f9439SBrian Somers if (bundle->ncp.mp.active) { 3883b0f8d2eSBrian Somers struct datalink *dl; 38986b1f0d7SBrian Somers struct datalink *lost; 3903b0f8d2eSBrian Somers 391faefde08SBrian Somers bundle->ifp.Speed = 0; 39286b1f0d7SBrian Somers lost = NULL; 393faefde08SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 39486b1f0d7SBrian Somers if (fp == &dl->physical->link.lcp.fsm) 39586b1f0d7SBrian Somers lost = dl; 39686b1f0d7SBrian Somers else if (dl->state == DATALINK_OPEN) 397faefde08SBrian Somers bundle->ifp.Speed += modem_Speed(dl->physical); 39886b1f0d7SBrian Somers 399faefde08SBrian Somers if (bundle->ifp.Speed) 400faefde08SBrian Somers /* Don't configure down to a speed of 0 */ 401faefde08SBrian Somers tun_configure(bundle, bundle->ncp.mp.link.lcp.his_mru); 40286b1f0d7SBrian Somers 40386b1f0d7SBrian Somers if (lost) 40486b1f0d7SBrian Somers mp_LinkLost(&bundle->ncp.mp, lost); 40586b1f0d7SBrian Somers else 406a33b2ef7SBrian Somers log_Printf(LogALERT, "Oops, lost an unrecognised datalink (%s) !\n", 40786b1f0d7SBrian Somers fp->link->name); 4083b0f8d2eSBrian Somers } 4096d666775SBrian Somers } 410ff0f9439SBrian Somers } 4116d666775SBrian Somers 4126d666775SBrian Somers static void 4136d666775SBrian Somers bundle_LayerFinish(void *v, struct fsm *fp) 4146d666775SBrian Somers { 4156d666775SBrian Somers /* The given fsm is now down (fp cannot be NULL) 4166d666775SBrian Somers * 417dd7e2610SBrian Somers * If it's the last LCP, fsm_Down all NCPs 418dd7e2610SBrian Somers * If it's the last NCP, fsm_Close all LCPs 4196d666775SBrian Somers */ 4206d666775SBrian Somers 4216d666775SBrian Somers struct bundle *bundle = (struct bundle *)v; 4226d666775SBrian Somers struct datalink *dl; 4236d666775SBrian Somers 4243b0f8d2eSBrian Somers if (fp->proto == PROTO_IPCP) { 42526afeaa2SBrian Somers if (bundle_Phase(bundle) != PHASE_DEAD) 42625092092SBrian Somers bundle_NewPhase(bundle, PHASE_TERMINATE); 4276d666775SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 4289c81b87dSBrian Somers datalink_Close(dl, CLOSE_NORMAL); 42909206a6fSBrian Somers fsm2initial(fp); 4303b0f8d2eSBrian Somers } else if (fp->proto == PROTO_LCP) { 4313b0f8d2eSBrian Somers int others_active; 432a611cad6SBrian Somers 4333b0f8d2eSBrian Somers others_active = 0; 4343b0f8d2eSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 4353b0f8d2eSBrian Somers if (fp != &dl->physical->link.lcp.fsm && 4363b0f8d2eSBrian Somers dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) 4373b0f8d2eSBrian Somers others_active++; 4383b0f8d2eSBrian Somers 43909206a6fSBrian Somers if (!others_active) 44009206a6fSBrian Somers fsm2initial(&bundle->ncp.ipcp.fsm); 4413b0f8d2eSBrian Somers } 4423b0f8d2eSBrian Somers } 4436d666775SBrian Somers 4447a6f8720SBrian Somers int 4457a6f8720SBrian Somers bundle_LinkIsUp(const struct bundle *bundle) 4467a6f8720SBrian Somers { 4475828db6dSBrian Somers return bundle->ncp.ipcp.fsm.state == ST_OPENED; 4487a6f8720SBrian Somers } 4497a6f8720SBrian Somers 4507a6f8720SBrian Somers void 4519c81b87dSBrian Somers bundle_Close(struct bundle *bundle, const char *name, int how) 4527a6f8720SBrian Somers { 453455aabc3SBrian Somers /* 4543006ec67SBrian Somers * Please close the given datalink. 455dd7e2610SBrian Somers * If name == NULL or name is the last datalink, fsm_Close all NCPs 4566f384573SBrian Somers * (except our MP) 4573b0f8d2eSBrian Somers * If it isn't the last datalink, just Close that datalink. 458455aabc3SBrian Somers */ 4597a6f8720SBrian Somers 4603b0f8d2eSBrian Somers struct datalink *dl, *this_dl; 4613b0f8d2eSBrian Somers int others_active; 4623006ec67SBrian Somers 4633b0f8d2eSBrian Somers if (bundle->phase == PHASE_TERMINATE || bundle->phase == PHASE_DEAD) 4643b0f8d2eSBrian Somers return; 4653b0f8d2eSBrian Somers 4663b0f8d2eSBrian Somers others_active = 0; 4673b0f8d2eSBrian Somers this_dl = NULL; 4683b0f8d2eSBrian Somers 4693b0f8d2eSBrian Somers for (dl = bundle->links; dl; dl = dl->next) { 4703b0f8d2eSBrian Somers if (name && !strcasecmp(name, dl->name)) 4713b0f8d2eSBrian Somers this_dl = dl; 4723b0f8d2eSBrian Somers if (name == NULL || this_dl == dl) { 4739c81b87dSBrian Somers switch (how) { 4749c81b87dSBrian Somers case CLOSE_LCP: 4759c81b87dSBrian Somers datalink_DontHangup(dl); 4769c81b87dSBrian Somers /* fall through */ 4779c81b87dSBrian Somers case CLOSE_STAYDOWN: 4783006ec67SBrian Somers datalink_StayDown(dl); 4799c81b87dSBrian Somers break; 4809c81b87dSBrian Somers } 4813b0f8d2eSBrian Somers } else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) 4823b0f8d2eSBrian Somers others_active++; 4833b0f8d2eSBrian Somers } 4843b0f8d2eSBrian Somers 4853b0f8d2eSBrian Somers if (name && this_dl == NULL) { 486dd7e2610SBrian Somers log_Printf(LogWARN, "%s: Invalid datalink name\n", name); 4873b0f8d2eSBrian Somers return; 4883b0f8d2eSBrian Somers } 4893b0f8d2eSBrian Somers 4903b0f8d2eSBrian Somers if (!others_active) { 49104eaa58cSBrian Somers bundle_StopIdleTimer(bundle); 49204eaa58cSBrian Somers bundle_StopAutoLoadTimer(bundle); 4933b0f8d2eSBrian Somers if (bundle->ncp.ipcp.fsm.state > ST_CLOSED || 4943b0f8d2eSBrian Somers bundle->ncp.ipcp.fsm.state == ST_STARTING) 495dd7e2610SBrian Somers fsm_Close(&bundle->ncp.ipcp.fsm); 4963b0f8d2eSBrian Somers else { 49709206a6fSBrian Somers fsm2initial(&bundle->ncp.ipcp.fsm); 498d345321bSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 4999c81b87dSBrian Somers datalink_Close(dl, how); 5007a6f8720SBrian Somers } 5013b0f8d2eSBrian Somers } else if (this_dl && this_dl->state != DATALINK_CLOSED && 5023b0f8d2eSBrian Somers this_dl->state != DATALINK_HANGUP) 5039c81b87dSBrian Somers datalink_Close(this_dl, how); 504d2fd8d77SBrian Somers } 5057a6f8720SBrian Somers 5061bc9b5baSBrian Somers void 507899011c4SBrian Somers bundle_Down(struct bundle *bundle, int how) 5081bc9b5baSBrian Somers { 5091bc9b5baSBrian Somers struct datalink *dl; 5101bc9b5baSBrian Somers 5111bc9b5baSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 512899011c4SBrian Somers datalink_Down(dl, how); 5131bc9b5baSBrian Somers } 5141bc9b5baSBrian Somers 5152f786681SBrian Somers static int 5162f786681SBrian Somers bundle_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n) 5172f786681SBrian Somers { 5182f786681SBrian Somers struct bundle *bundle = descriptor2bundle(d); 5192f786681SBrian Somers struct datalink *dl; 52004eaa58cSBrian Somers int result, want, queued, nlinks; 5212f786681SBrian Somers 5222f786681SBrian Somers result = 0; 5232f786681SBrian Somers 524078c562eSBrian Somers /* If there are aren't many packets queued, look for some more. */ 52504eaa58cSBrian Somers for (nlinks = 0, dl = bundle->links; dl; dl = dl->next) 52604eaa58cSBrian Somers nlinks++; 52704eaa58cSBrian Somers 52804eaa58cSBrian Somers if (nlinks) { 52904eaa58cSBrian Somers queued = r ? bundle_FillQueues(bundle) : ip_QueueLen(); 53004eaa58cSBrian Somers if (bundle->autoload.running) { 53104eaa58cSBrian Somers if (queued < bundle->cfg.autoload.max.packets) { 53204eaa58cSBrian Somers if (queued > bundle->cfg.autoload.min.packets) 53304eaa58cSBrian Somers bundle_StopAutoLoadTimer(bundle); 53404eaa58cSBrian Somers else if (bundle->autoload.timer.state != TIMER_RUNNING || 53504eaa58cSBrian Somers bundle->autoload.comingup) 53604eaa58cSBrian Somers bundle_StartAutoLoadTimer(bundle, 0); 53704eaa58cSBrian Somers } else if (bundle->autoload.timer.state != TIMER_RUNNING || 53804eaa58cSBrian Somers !bundle->autoload.comingup) 53904eaa58cSBrian Somers bundle_StartAutoLoadTimer(bundle, 1); 54004eaa58cSBrian Somers } 54104eaa58cSBrian Somers 542ff0f9439SBrian Somers if (r && (bundle->phase == PHASE_NETWORK || 543ff0f9439SBrian Somers bundle->phys_type.all & PHYS_AUTO)) { 54404eaa58cSBrian Somers /* enough surplus so that we can tell if we're getting swamped */ 54504eaa58cSBrian Somers want = bundle->cfg.autoload.max.packets + nlinks * 2; 54604eaa58cSBrian Somers /* but at least 20 packets ! */ 54704eaa58cSBrian Somers if (want < 20) 54804eaa58cSBrian Somers want = 20; 54904eaa58cSBrian Somers if (queued < want) { 55004eaa58cSBrian Somers /* Not enough - select() for more */ 55104eaa58cSBrian Somers FD_SET(bundle->dev.fd, r); 552faefde08SBrian Somers if (*n < bundle->dev.fd + 1) 553faefde08SBrian Somers *n = bundle->dev.fd + 1; 5541384bd27SBrian Somers log_Printf(LogTIMER, "%s: fdset(r) %d\n", TUN_NAME, bundle->dev.fd); 555078c562eSBrian Somers result++; 556078c562eSBrian Somers } 55704eaa58cSBrian Somers } 55804eaa58cSBrian Somers } 559078c562eSBrian Somers 56071555108SBrian Somers /* Which links need a select() ? */ 56171555108SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 56271555108SBrian Somers result += descriptor_UpdateSet(&dl->desc, r, w, e, n); 56371555108SBrian Somers 564ea722969SBrian Somers /* 565ea722969SBrian Somers * This *MUST* be called after the datalink UpdateSet()s as it 56604eaa58cSBrian Somers * might be ``holding'' one of the datalinks (death-row) and 56704eaa58cSBrian Somers * wants to be able to de-select() it from the descriptor set. 568ea722969SBrian Somers */ 5690f2f3eb3SBrian Somers result += descriptor_UpdateSet(&bundle->ncp.mp.server.desc, r, w, e, n); 570ea722969SBrian Somers 5712f786681SBrian Somers return result; 5722f786681SBrian Somers } 5732f786681SBrian Somers 5742f786681SBrian Somers static int 5752f786681SBrian Somers bundle_IsSet(struct descriptor *d, const fd_set *fdset) 5762f786681SBrian Somers { 5772f786681SBrian Somers struct bundle *bundle = descriptor2bundle(d); 5782f786681SBrian Somers struct datalink *dl; 5792f786681SBrian Somers 5802f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 5812f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 5822f786681SBrian Somers return 1; 5832f786681SBrian Somers 584332b9de0SBrian Somers if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 585332b9de0SBrian Somers return 1; 586332b9de0SBrian Somers 587faefde08SBrian Somers return FD_ISSET(bundle->dev.fd, fdset); 5882f786681SBrian Somers } 5892f786681SBrian Somers 5902f786681SBrian Somers static void 5912f786681SBrian Somers bundle_DescriptorRead(struct descriptor *d, struct bundle *bundle, 5922f786681SBrian Somers const fd_set *fdset) 5932f786681SBrian Somers { 5942f786681SBrian Somers struct datalink *dl; 5952f786681SBrian Somers 596ea722969SBrian Somers if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 597ea722969SBrian Somers descriptor_Read(&bundle->ncp.mp.server.desc, bundle, fdset); 598ea722969SBrian Somers 5992f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 6002f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 6012f786681SBrian Somers descriptor_Read(&dl->desc, bundle, fdset); 602b6217683SBrian Somers 603faefde08SBrian Somers if (FD_ISSET(bundle->dev.fd, fdset)) { 604078c562eSBrian Somers struct tun_data tun; 605078c562eSBrian Somers int n, pri; 606078c562eSBrian Somers 607078c562eSBrian Somers /* something to read from tun */ 608faefde08SBrian Somers n = read(bundle->dev.fd, &tun, sizeof tun); 609078c562eSBrian Somers if (n < 0) { 610a33b2ef7SBrian Somers log_Printf(LogWARN, "read from %s: %s\n", TUN_NAME, strerror(errno)); 611078c562eSBrian Somers return; 612078c562eSBrian Somers } 613078c562eSBrian Somers n -= sizeof tun - sizeof tun.data; 614078c562eSBrian Somers if (n <= 0) { 615a33b2ef7SBrian Somers log_Printf(LogERROR, "read from %s: Only %d bytes read ?\n", TUN_NAME, n); 616078c562eSBrian Somers return; 617078c562eSBrian Somers } 618078c562eSBrian Somers if (!tun_check_header(tun, AF_INET)) 619078c562eSBrian Somers return; 620078c562eSBrian Somers 621078c562eSBrian Somers if (((struct ip *)tun.data)->ip_dst.s_addr == 622078c562eSBrian Somers bundle->ncp.ipcp.my_ip.s_addr) { 623078c562eSBrian Somers /* we've been asked to send something addressed *to* us :( */ 624078c562eSBrian Somers if (Enabled(bundle, OPT_LOOPBACK)) { 625078c562eSBrian Somers pri = PacketCheck(bundle, tun.data, n, &bundle->filter.in); 626078c562eSBrian Somers if (pri >= 0) { 627078c562eSBrian Somers struct mbuf *bp; 628078c562eSBrian Somers 629078c562eSBrian Somers #ifndef NOALIAS 630078c562eSBrian Somers if (alias_IsEnabled()) { 631078c562eSBrian Somers (*PacketAlias.In)(tun.data, sizeof tun.data); 632078c562eSBrian Somers n = ntohs(((struct ip *)tun.data)->ip_len); 633078c562eSBrian Somers } 634078c562eSBrian Somers #endif 635078c562eSBrian Somers bp = mbuf_Alloc(n, MB_IPIN); 636078c562eSBrian Somers memcpy(MBUF_CTOP(bp), tun.data, n); 637078c562eSBrian Somers ip_Input(bundle, bp); 638078c562eSBrian Somers log_Printf(LogDEBUG, "Looped back packet addressed to myself\n"); 639078c562eSBrian Somers } 640078c562eSBrian Somers return; 641078c562eSBrian Somers } else 642078c562eSBrian Somers log_Printf(LogDEBUG, "Oops - forwarding packet addressed to myself\n"); 643078c562eSBrian Somers } 644078c562eSBrian Somers 645078c562eSBrian Somers /* 646078c562eSBrian Somers * Process on-demand dialup. Output packets are queued within tunnel 647078c562eSBrian Somers * device until IPCP is opened. 648078c562eSBrian Somers */ 649078c562eSBrian Somers 650078c562eSBrian Somers if (bundle_Phase(bundle) == PHASE_DEAD) { 651078c562eSBrian Somers /* 652078c562eSBrian Somers * Note, we must be in AUTO mode :-/ otherwise our interface should 653078c562eSBrian Somers * *not* be UP and we can't receive data 654078c562eSBrian Somers */ 655078c562eSBrian Somers if ((pri = PacketCheck(bundle, tun.data, n, &bundle->filter.dial)) >= 0) 65681358fa3SBrian Somers bundle_Open(bundle, NULL, PHYS_AUTO); 657078c562eSBrian Somers else 658078c562eSBrian Somers /* 659078c562eSBrian Somers * Drop the packet. If we were to queue it, we'd just end up with 660078c562eSBrian Somers * a pile of timed-out data in our output queue by the time we get 661078c562eSBrian Somers * around to actually dialing. We'd also prematurely reach the 662078c562eSBrian Somers * threshold at which we stop select()ing to read() the tun 663078c562eSBrian Somers * device - breaking auto-dial. 664078c562eSBrian Somers */ 665078c562eSBrian Somers return; 666078c562eSBrian Somers } 667078c562eSBrian Somers 668078c562eSBrian Somers pri = PacketCheck(bundle, tun.data, n, &bundle->filter.out); 669078c562eSBrian Somers if (pri >= 0) { 670078c562eSBrian Somers #ifndef NOALIAS 671078c562eSBrian Somers if (alias_IsEnabled()) { 672078c562eSBrian Somers (*PacketAlias.Out)(tun.data, sizeof tun.data); 673078c562eSBrian Somers n = ntohs(((struct ip *)tun.data)->ip_len); 674078c562eSBrian Somers } 675078c562eSBrian Somers #endif 676078c562eSBrian Somers ip_Enqueue(pri, tun.data, n); 677078c562eSBrian Somers } 678078c562eSBrian Somers } 6792f786681SBrian Somers } 6802f786681SBrian Somers 6811af29a6eSBrian Somers static int 6822f786681SBrian Somers bundle_DescriptorWrite(struct descriptor *d, struct bundle *bundle, 6832f786681SBrian Somers const fd_set *fdset) 6842f786681SBrian Somers { 6852f786681SBrian Somers struct datalink *dl; 6861af29a6eSBrian Somers int result = 0; 6872f786681SBrian Somers 688ea722969SBrian Somers /* This is not actually necessary as struct mpserver doesn't Write() */ 689ea722969SBrian Somers if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 690ea722969SBrian Somers descriptor_Write(&bundle->ncp.mp.server.desc, bundle, fdset); 691ea722969SBrian Somers 6922f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 6932f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 6941af29a6eSBrian Somers result += descriptor_Write(&dl->desc, bundle, fdset); 6951af29a6eSBrian Somers 6961af29a6eSBrian Somers return result; 6972f786681SBrian Somers } 6982f786681SBrian Somers 699da66dd13SBrian Somers void 7001384bd27SBrian Somers bundle_LockTun(struct bundle *bundle) 7011384bd27SBrian Somers { 7021384bd27SBrian Somers FILE *lockfile; 7031384bd27SBrian Somers char pidfile[MAXPATHLEN]; 7041384bd27SBrian Somers 7051384bd27SBrian Somers snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit); 7061384bd27SBrian Somers lockfile = ID0fopen(pidfile, "w"); 7071384bd27SBrian Somers if (lockfile != NULL) { 7081384bd27SBrian Somers fprintf(lockfile, "%d\n", (int)getpid()); 7091384bd27SBrian Somers fclose(lockfile); 7101384bd27SBrian Somers } 7111384bd27SBrian Somers #ifndef RELEASE_CRUNCH 7121384bd27SBrian Somers else 7131384bd27SBrian Somers log_Printf(LogERROR, "Warning: Can't create %s: %s\n", 7141384bd27SBrian Somers pidfile, strerror(errno)); 7151384bd27SBrian Somers #endif 7161384bd27SBrian Somers } 7171384bd27SBrian Somers 7181384bd27SBrian Somers static void 7191384bd27SBrian Somers bundle_UnlockTun(struct bundle *bundle) 7201384bd27SBrian Somers { 7211384bd27SBrian Somers char pidfile[MAXPATHLEN]; 7221384bd27SBrian Somers 7231384bd27SBrian Somers snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit); 7241384bd27SBrian Somers ID0unlink(pidfile); 7251384bd27SBrian Somers } 7267a6f8720SBrian Somers 7277a6f8720SBrian Somers struct bundle * 7288e7b8599SBrian Somers bundle_Create(const char *prefix, int type, const char **argv) 7297a6f8720SBrian Somers { 7307a6f8720SBrian Somers int s, enoentcount, err; 7317a6f8720SBrian Somers struct ifreq ifrq; 7327a6f8720SBrian Somers static struct bundle bundle; /* there can be only one */ 7337a6f8720SBrian Somers 734faefde08SBrian Somers if (bundle.ifp.Name != NULL) { /* Already allocated ! */ 735a33b2ef7SBrian Somers log_Printf(LogALERT, "bundle_Create: There's only one BUNDLE !\n"); 7367a6f8720SBrian Somers return NULL; 7377a6f8720SBrian Somers } 7387a6f8720SBrian Somers 7397a6f8720SBrian Somers err = ENOENT; 7407a6f8720SBrian Somers enoentcount = 0; 741107d62e7SBrian Somers for (bundle.unit = 0; ; bundle.unit++) { 742faefde08SBrian Somers snprintf(bundle.dev.Name, sizeof bundle.dev.Name, "%s%d", 743faefde08SBrian Somers prefix, bundle.unit); 744faefde08SBrian Somers bundle.dev.fd = ID0open(bundle.dev.Name, O_RDWR); 745faefde08SBrian Somers if (bundle.dev.fd >= 0) 7467a6f8720SBrian Somers break; 747107d62e7SBrian Somers else if (errno == ENXIO) { 7487a6f8720SBrian Somers err = errno; 749107d62e7SBrian Somers break; 7507a6f8720SBrian Somers } else if (errno == ENOENT) { 7517a6f8720SBrian Somers if (++enoentcount > 2) 752107d62e7SBrian Somers break; 7537a6f8720SBrian Somers } else 7547a6f8720SBrian Somers err = errno; 7557a6f8720SBrian Somers } 7567a6f8720SBrian Somers 757faefde08SBrian Somers if (bundle.dev.fd < 0) { 758dd7e2610SBrian Somers log_Printf(LogWARN, "No available tunnel devices found (%s).\n", 75985b542cfSBrian Somers strerror(err)); 7607a6f8720SBrian Somers return NULL; 7617a6f8720SBrian Somers } 7627a6f8720SBrian Somers 763dd7e2610SBrian Somers log_SetTun(bundle.unit); 7648e7b8599SBrian Somers bundle.argv = argv; 7657a6f8720SBrian Somers 7667a6f8720SBrian Somers s = socket(AF_INET, SOCK_DGRAM, 0); 7677a6f8720SBrian Somers if (s < 0) { 768dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_Create: socket(): %s\n", strerror(errno)); 769faefde08SBrian Somers close(bundle.dev.fd); 7707a6f8720SBrian Somers return NULL; 7717a6f8720SBrian Somers } 7727a6f8720SBrian Somers 773faefde08SBrian Somers bundle.ifp.Name = strrchr(bundle.dev.Name, '/'); 774faefde08SBrian Somers if (bundle.ifp.Name == NULL) 775faefde08SBrian Somers bundle.ifp.Name = bundle.dev.Name; 7767a6f8720SBrian Somers else 777faefde08SBrian Somers bundle.ifp.Name++; 7787a6f8720SBrian Somers 7797a6f8720SBrian Somers /* 7807a6f8720SBrian Somers * Now, bring up the interface. 7817a6f8720SBrian Somers */ 7827a6f8720SBrian Somers memset(&ifrq, '\0', sizeof ifrq); 783faefde08SBrian Somers strncpy(ifrq.ifr_name, bundle.ifp.Name, sizeof ifrq.ifr_name - 1); 7847a6f8720SBrian Somers ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0'; 7857a6f8720SBrian Somers if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) { 786a33b2ef7SBrian Somers log_Printf(LogERROR, "bundle_Create: ioctl(SIOCGIFFLAGS): %s\n", 7877a6f8720SBrian Somers strerror(errno)); 7887a6f8720SBrian Somers close(s); 789faefde08SBrian Somers close(bundle.dev.fd); 790faefde08SBrian Somers bundle.ifp.Name = NULL; 7917a6f8720SBrian Somers return NULL; 7927a6f8720SBrian Somers } 7937a6f8720SBrian Somers ifrq.ifr_flags |= IFF_UP; 7947a6f8720SBrian Somers if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) { 795a33b2ef7SBrian Somers log_Printf(LogERROR, "bundle_Create: ioctl(SIOCSIFFLAGS): %s\n", 7967a6f8720SBrian Somers strerror(errno)); 7977a6f8720SBrian Somers close(s); 798faefde08SBrian Somers close(bundle.dev.fd); 799faefde08SBrian Somers bundle.ifp.Name = NULL; 8007a6f8720SBrian Somers return NULL; 8017a6f8720SBrian Somers } 8027a6f8720SBrian Somers 8037a6f8720SBrian Somers close(s); 8047a6f8720SBrian Somers 805faefde08SBrian Somers if ((bundle.ifp.Index = GetIfIndex(bundle.ifp.Name)) < 0) { 806a33b2ef7SBrian Somers log_Printf(LogERROR, "Can't find interface index.\n"); 807faefde08SBrian Somers close(bundle.dev.fd); 808faefde08SBrian Somers bundle.ifp.Name = NULL; 8097a6f8720SBrian Somers return NULL; 8107a6f8720SBrian Somers } 811faefde08SBrian Somers log_Printf(LogPHASE, "Using interface: %s\n", bundle.ifp.Name); 8127a6f8720SBrian Somers 813faefde08SBrian Somers bundle.ifp.Speed = 0; 8147a6f8720SBrian Somers 815820de6ebSBrian Somers bundle.routing_seq = 0; 816a0cbd833SBrian Somers bundle.phase = PHASE_DEAD; 817a0cbd833SBrian Somers bundle.CleaningUp = 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; 836ff0f9439SBrian Somers bundle.phys_type.all = type; 837ff0f9439SBrian Somers bundle.phys_type.open = 0; 838ab886ad0SBrian Somers 8396f384573SBrian Somers bundle.links = datalink_Create("deflink", &bundle, type); 8403006ec67SBrian Somers if (bundle.links == NULL) { 841a33b2ef7SBrian Somers log_Printf(LogALERT, "Cannot create data link: %s\n", strerror(errno)); 842faefde08SBrian Somers close(bundle.dev.fd); 843faefde08SBrian Somers bundle.ifp.Name = NULL; 8442289f246SBrian Somers return NULL; 8452289f246SBrian Somers } 8462289f246SBrian Somers 8472f786681SBrian Somers bundle.desc.type = BUNDLE_DESCRIPTOR; 8482f786681SBrian Somers bundle.desc.UpdateSet = bundle_UpdateSet; 8492f786681SBrian Somers bundle.desc.IsSet = bundle_IsSet; 8502f786681SBrian Somers bundle.desc.Read = bundle_DescriptorRead; 8512f786681SBrian Somers bundle.desc.Write = bundle_DescriptorWrite; 8522f786681SBrian Somers 85349052c95SBrian Somers mp_Init(&bundle.ncp.mp, &bundle); 85449052c95SBrian Somers 85549052c95SBrian Somers /* Send over the first physical link by default */ 8565828db6dSBrian Somers ipcp_Init(&bundle.ncp.ipcp, &bundle, &bundle.links->physical->link, 8575828db6dSBrian Somers &bundle.fsm); 8586d666775SBrian Somers 8595ca5389aSBrian Somers memset(&bundle.filter, '\0', sizeof bundle.filter); 8605ca5389aSBrian Somers bundle.filter.in.fragok = bundle.filter.in.logok = 1; 8615ca5389aSBrian Somers bundle.filter.in.name = "IN"; 8625ca5389aSBrian Somers bundle.filter.out.fragok = bundle.filter.out.logok = 1; 8635ca5389aSBrian Somers bundle.filter.out.name = "OUT"; 8645ca5389aSBrian Somers bundle.filter.dial.name = "DIAL"; 8658390b576SBrian Somers bundle.filter.dial.logok = 1; 8665ca5389aSBrian Somers bundle.filter.alive.name = "ALIVE"; 8675ca5389aSBrian Somers bundle.filter.alive.logok = 1; 86893ee0ff2SBrian Somers memset(&bundle.idle.timer, '\0', sizeof bundle.idle.timer); 86993ee0ff2SBrian Somers bundle.idle.done = 0; 8705cf4388bSBrian Somers bundle.notify.fd = -1; 87104eaa58cSBrian Somers memset(&bundle.autoload.timer, '\0', sizeof bundle.autoload.timer); 87204eaa58cSBrian Somers bundle.autoload.done = 0; 87304eaa58cSBrian Somers bundle.autoload.running = 0; 87493ee0ff2SBrian Somers 8756d666775SBrian Somers /* Clean out any leftover crud */ 8766d666775SBrian Somers bundle_CleanInterface(&bundle); 8776d666775SBrian Somers 8781384bd27SBrian Somers bundle_LockTun(&bundle); 8791384bd27SBrian Somers 8807a6f8720SBrian Somers return &bundle; 8817a6f8720SBrian Somers } 8827a6f8720SBrian Somers 88368a0f0ccSBrian Somers static void 88468a0f0ccSBrian Somers bundle_DownInterface(struct bundle *bundle) 88568a0f0ccSBrian Somers { 88668a0f0ccSBrian Somers struct ifreq ifrq; 88768a0f0ccSBrian Somers int s; 88868a0f0ccSBrian Somers 889dd7e2610SBrian Somers route_IfDelete(bundle, 1); 89068a0f0ccSBrian Somers 89168a0f0ccSBrian Somers s = ID0socket(AF_INET, SOCK_DGRAM, 0); 89268a0f0ccSBrian Somers if (s < 0) { 893dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_DownInterface: socket: %s\n", strerror(errno)); 89468a0f0ccSBrian Somers return; 89568a0f0ccSBrian Somers } 89668a0f0ccSBrian Somers 89768a0f0ccSBrian Somers memset(&ifrq, '\0', sizeof ifrq); 898faefde08SBrian Somers strncpy(ifrq.ifr_name, bundle->ifp.Name, sizeof ifrq.ifr_name - 1); 89968a0f0ccSBrian Somers ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0'; 90068a0f0ccSBrian Somers if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) { 901dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_DownInterface: ioctl(SIOCGIFFLAGS): %s\n", 90268a0f0ccSBrian Somers strerror(errno)); 90368a0f0ccSBrian Somers close(s); 90468a0f0ccSBrian Somers return; 90568a0f0ccSBrian Somers } 90668a0f0ccSBrian Somers ifrq.ifr_flags &= ~IFF_UP; 90768a0f0ccSBrian Somers if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) { 908dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_DownInterface: ioctl(SIOCSIFFLAGS): %s\n", 90968a0f0ccSBrian Somers strerror(errno)); 91068a0f0ccSBrian Somers close(s); 91168a0f0ccSBrian Somers return; 91268a0f0ccSBrian Somers } 91368a0f0ccSBrian Somers close(s); 91468a0f0ccSBrian Somers } 91568a0f0ccSBrian Somers 91668a0f0ccSBrian Somers void 91768a0f0ccSBrian Somers bundle_Destroy(struct bundle *bundle) 91868a0f0ccSBrian Somers { 9193006ec67SBrian Somers struct datalink *dl; 920b6217683SBrian Somers 921ea722969SBrian Somers /* 92204eaa58cSBrian Somers * Clean up the interface. We don't need to timer_Stop()s, mp_Down(), 923ea722969SBrian Somers * ipcp_CleanInterface() and bundle_DownInterface() unless we're getting 924ea722969SBrian Somers * out under exceptional conditions such as a descriptor exception. 925ea722969SBrian Somers */ 92604eaa58cSBrian Somers timer_Stop(&bundle->idle.timer); 92704eaa58cSBrian Somers timer_Stop(&bundle->autoload.timer); 92866f634b6SBrian Somers mp_Down(&bundle->ncp.mp); 929dd7e2610SBrian Somers ipcp_CleanInterface(&bundle->ncp.ipcp); 93068a0f0ccSBrian Somers bundle_DownInterface(bundle); 9313006ec67SBrian Somers 932ea722969SBrian Somers /* Again, these are all DATALINK_CLOSED unless we're abending */ 9333006ec67SBrian Somers dl = bundle->links; 9343006ec67SBrian Somers while (dl) 9353006ec67SBrian Somers dl = datalink_Destroy(dl); 9363006ec67SBrian Somers 9371384bd27SBrian Somers close(bundle->dev.fd); 9381384bd27SBrian Somers bundle_UnlockTun(bundle); 9391384bd27SBrian Somers 940ea722969SBrian Somers /* In case we never made PHASE_NETWORK */ 9415cf4388bSBrian Somers bundle_Notify(bundle, EX_ERRDEAD); 942b6217683SBrian Somers 943faefde08SBrian Somers bundle->ifp.Name = NULL; 94468a0f0ccSBrian Somers } 94568a0f0ccSBrian Somers 9467a6f8720SBrian Somers struct rtmsg { 9477a6f8720SBrian Somers struct rt_msghdr m_rtm; 9487a6f8720SBrian Somers char m_space[64]; 9497a6f8720SBrian Somers }; 9507a6f8720SBrian Somers 951610b185fSBrian Somers int 952820de6ebSBrian Somers bundle_SetRoute(struct bundle *bundle, int cmd, struct in_addr dst, 9537a6f8720SBrian Somers struct in_addr gateway, struct in_addr mask, int bang) 9547a6f8720SBrian Somers { 9557a6f8720SBrian Somers struct rtmsg rtmes; 9567a6f8720SBrian Somers int s, nb, wb; 9577a6f8720SBrian Somers char *cp; 9587a6f8720SBrian Somers const char *cmdstr; 9597a6f8720SBrian Somers struct sockaddr_in rtdata; 960610b185fSBrian Somers int result = 1; 9617a6f8720SBrian Somers 9627a6f8720SBrian Somers if (bang) 9637a6f8720SBrian Somers cmdstr = (cmd == RTM_ADD ? "Add!" : "Delete!"); 9647a6f8720SBrian Somers else 9657a6f8720SBrian Somers cmdstr = (cmd == RTM_ADD ? "Add" : "Delete"); 9667a6f8720SBrian Somers s = ID0socket(PF_ROUTE, SOCK_RAW, 0); 9677a6f8720SBrian Somers if (s < 0) { 968dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_SetRoute: socket(): %s\n", strerror(errno)); 969610b185fSBrian Somers return result; 9707a6f8720SBrian Somers } 9717a6f8720SBrian Somers memset(&rtmes, '\0', sizeof rtmes); 9727a6f8720SBrian Somers rtmes.m_rtm.rtm_version = RTM_VERSION; 9737a6f8720SBrian Somers rtmes.m_rtm.rtm_type = cmd; 9747a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs = RTA_DST; 975820de6ebSBrian Somers rtmes.m_rtm.rtm_seq = ++bundle->routing_seq; 9767a6f8720SBrian Somers rtmes.m_rtm.rtm_pid = getpid(); 9777a6f8720SBrian Somers rtmes.m_rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC; 9787a6f8720SBrian Somers 9797a6f8720SBrian Somers memset(&rtdata, '\0', sizeof rtdata); 98050e5c17dSBrian Somers rtdata.sin_len = sizeof rtdata; 9817a6f8720SBrian Somers rtdata.sin_family = AF_INET; 9827a6f8720SBrian Somers rtdata.sin_port = 0; 9837a6f8720SBrian Somers rtdata.sin_addr = dst; 9847a6f8720SBrian Somers 9857a6f8720SBrian Somers cp = rtmes.m_space; 98650e5c17dSBrian Somers memcpy(cp, &rtdata, rtdata.sin_len); 98750e5c17dSBrian Somers cp += rtdata.sin_len; 988e43ebac1SBrian Somers if (cmd == RTM_ADD) { 9897a6f8720SBrian Somers if (gateway.s_addr == INADDR_ANY) { 9907a6f8720SBrian Somers /* Add a route through the interface */ 9917a6f8720SBrian Somers struct sockaddr_dl dl; 9927a6f8720SBrian Somers const char *iname; 9937a6f8720SBrian Somers int ilen; 9947a6f8720SBrian Somers 995faefde08SBrian Somers iname = Index2Nam(bundle->ifp.Index); 9967a6f8720SBrian Somers ilen = strlen(iname); 9977a6f8720SBrian Somers dl.sdl_len = sizeof dl - sizeof dl.sdl_data + ilen; 9987a6f8720SBrian Somers dl.sdl_family = AF_LINK; 999faefde08SBrian Somers dl.sdl_index = bundle->ifp.Index; 10007a6f8720SBrian Somers dl.sdl_type = 0; 10017a6f8720SBrian Somers dl.sdl_nlen = ilen; 10027a6f8720SBrian Somers dl.sdl_alen = 0; 10037a6f8720SBrian Somers dl.sdl_slen = 0; 10047a6f8720SBrian Somers strncpy(dl.sdl_data, iname, sizeof dl.sdl_data); 10057a6f8720SBrian Somers memcpy(cp, &dl, dl.sdl_len); 10067a6f8720SBrian Somers cp += dl.sdl_len; 10077a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY; 10087a6f8720SBrian Somers } else { 10097a6f8720SBrian Somers rtdata.sin_addr = gateway; 101050e5c17dSBrian Somers memcpy(cp, &rtdata, rtdata.sin_len); 101150e5c17dSBrian Somers cp += rtdata.sin_len; 10127a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY; 10137a6f8720SBrian Somers } 1014e43ebac1SBrian Somers } 10157a6f8720SBrian Somers 10167a6f8720SBrian Somers if (dst.s_addr == INADDR_ANY) 10177a6f8720SBrian Somers mask.s_addr = INADDR_ANY; 10187a6f8720SBrian Somers 10197a6f8720SBrian Somers if (cmd == RTM_ADD || dst.s_addr == INADDR_ANY) { 10207a6f8720SBrian Somers rtdata.sin_addr = mask; 102150e5c17dSBrian Somers memcpy(cp, &rtdata, rtdata.sin_len); 102250e5c17dSBrian Somers cp += rtdata.sin_len; 10237a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs |= RTA_NETMASK; 10247a6f8720SBrian Somers } 10257a6f8720SBrian Somers 10267a6f8720SBrian Somers nb = cp - (char *) &rtmes; 10277a6f8720SBrian Somers rtmes.m_rtm.rtm_msglen = nb; 10287a6f8720SBrian Somers wb = ID0write(s, &rtmes, nb); 10297a6f8720SBrian Somers if (wb < 0) { 1030dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute failure:\n"); 1031dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Cmd = %s\n", cmdstr); 1032dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Dst = %s\n", inet_ntoa(dst)); 1033dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Gateway = %s\n", inet_ntoa(gateway)); 1034dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Mask = %s\n", inet_ntoa(mask)); 10357a6f8720SBrian Somers failed: 10367a6f8720SBrian Somers if (cmd == RTM_ADD && (rtmes.m_rtm.rtm_errno == EEXIST || 1037e43ebac1SBrian Somers (rtmes.m_rtm.rtm_errno == 0 && errno == EEXIST))) { 1038610b185fSBrian Somers if (!bang) { 1039dd7e2610SBrian Somers log_Printf(LogWARN, "Add route failed: %s already exists\n", 10407a6f8720SBrian Somers inet_ntoa(dst)); 1041610b185fSBrian Somers result = 0; /* Don't add to our dynamic list */ 1042610b185fSBrian Somers } else { 10437a6f8720SBrian Somers rtmes.m_rtm.rtm_type = cmd = RTM_CHANGE; 10447a6f8720SBrian Somers if ((wb = ID0write(s, &rtmes, nb)) < 0) 10457a6f8720SBrian Somers goto failed; 10467a6f8720SBrian Somers } 1047e43ebac1SBrian Somers } else if (cmd == RTM_DELETE && 10487a6f8720SBrian Somers (rtmes.m_rtm.rtm_errno == ESRCH || 10497a6f8720SBrian Somers (rtmes.m_rtm.rtm_errno == 0 && errno == ESRCH))) { 10507a6f8720SBrian Somers if (!bang) 1051dd7e2610SBrian Somers log_Printf(LogWARN, "Del route failed: %s: Non-existent\n", 10527a6f8720SBrian Somers inet_ntoa(dst)); 10537a6f8720SBrian Somers } else if (rtmes.m_rtm.rtm_errno == 0) 1054dd7e2610SBrian Somers log_Printf(LogWARN, "%s route failed: %s: errno: %s\n", cmdstr, 10557a6f8720SBrian Somers inet_ntoa(dst), strerror(errno)); 10567a6f8720SBrian Somers else 1057dd7e2610SBrian Somers log_Printf(LogWARN, "%s route failed: %s: %s\n", 10587a6f8720SBrian Somers cmdstr, inet_ntoa(dst), strerror(rtmes.m_rtm.rtm_errno)); 10597a6f8720SBrian Somers } 1060dd7e2610SBrian Somers log_Printf(LogDEBUG, "wrote %d: cmd = %s, dst = %x, gateway = %x\n", 1061fe3125a0SBrian Somers wb, cmdstr, (unsigned)dst.s_addr, (unsigned)gateway.s_addr); 10627a6f8720SBrian Somers close(s); 1063610b185fSBrian Somers 1064610b185fSBrian Somers return result; 10657a6f8720SBrian Somers } 106683d1af55SBrian Somers 106783d1af55SBrian Somers void 10683006ec67SBrian Somers bundle_LinkClosed(struct bundle *bundle, struct datalink *dl) 10693006ec67SBrian Somers { 10703006ec67SBrian Somers /* 10713006ec67SBrian Somers * Our datalink has closed. 1072ea722969SBrian Somers * CleanDatalinks() (called from DoLoop()) will remove closed 107381358fa3SBrian Somers * BACKGROUND and DIRECT links. 10743b0f8d2eSBrian Somers * If it's the last data link, enter phase DEAD. 1075ea722969SBrian Somers * 1076ea722969SBrian Somers * NOTE: dl may not be in our list (bundle_SendDatalink()) ! 10773006ec67SBrian Somers */ 10785b8b8060SBrian Somers 10793b0f8d2eSBrian Somers struct datalink *odl; 10803b0f8d2eSBrian Somers int other_links; 10815b8b8060SBrian Somers 10823b0f8d2eSBrian Somers other_links = 0; 10833b0f8d2eSBrian Somers for (odl = bundle->links; odl; odl = odl->next) 10843b0f8d2eSBrian Somers if (odl != dl && odl->state != DATALINK_CLOSED) 10853b0f8d2eSBrian Somers other_links++; 10863b0f8d2eSBrian Somers 10873b0f8d2eSBrian Somers if (!other_links) { 108881358fa3SBrian Somers if (dl->physical->type != PHYS_AUTO) /* Not in -auto mode */ 108903704096SBrian Somers bundle_DownInterface(bundle); 109009206a6fSBrian Somers fsm2initial(&bundle->ncp.ipcp.fsm); 10915563ebdeSBrian Somers bundle_NewPhase(bundle, PHASE_DEAD); 10920f2f3eb3SBrian Somers bundle_StopIdleTimer(bundle); 109304eaa58cSBrian Somers bundle_StopAutoLoadTimer(bundle); 109404eaa58cSBrian Somers bundle->autoload.running = 0; 109504eaa58cSBrian Somers } else 109604eaa58cSBrian Somers bundle->autoload.running = 1; 1097455aabc3SBrian Somers } 1098455aabc3SBrian Somers 1099455aabc3SBrian Somers void 1100565e35e5SBrian Somers bundle_Open(struct bundle *bundle, const char *name, int mask) 11013006ec67SBrian Somers { 11023006ec67SBrian Somers /* 11033006ec67SBrian Somers * Please open the given datalink, or all if name == NULL 11043006ec67SBrian Somers */ 11053006ec67SBrian Somers struct datalink *dl; 11063006ec67SBrian Somers 110704eaa58cSBrian Somers timer_Stop(&bundle->autoload.timer); 11083006ec67SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 11093006ec67SBrian Somers if (name == NULL || !strcasecmp(dl->name, name)) { 111004eaa58cSBrian Somers if (dl->state == DATALINK_CLOSED && (mask & dl->physical->type)) { 1111565e35e5SBrian Somers datalink_Up(dl, 1, 1); 111281358fa3SBrian Somers if (mask == PHYS_AUTO) 111381358fa3SBrian Somers /* Only one AUTO link at a time (see the AutoLoad timer) */ 111404eaa58cSBrian Somers break; 111504eaa58cSBrian Somers } 11163006ec67SBrian Somers if (name != NULL) 11173006ec67SBrian Somers break; 11183006ec67SBrian Somers } 11193006ec67SBrian Somers } 11203006ec67SBrian Somers 11213006ec67SBrian Somers struct datalink * 11223006ec67SBrian Somers bundle2datalink(struct bundle *bundle, const char *name) 11233006ec67SBrian Somers { 11243006ec67SBrian Somers struct datalink *dl; 11253006ec67SBrian Somers 11263006ec67SBrian Somers if (name != NULL) { 11273006ec67SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 11283006ec67SBrian Somers if (!strcasecmp(dl->name, name)) 11293006ec67SBrian Somers return dl; 11303006ec67SBrian Somers } else if (bundle->links && !bundle->links->next) 11313006ec67SBrian Somers return bundle->links; 11323006ec67SBrian Somers 11333006ec67SBrian Somers return NULL; 11343006ec67SBrian Somers } 11353006ec67SBrian Somers 11363006ec67SBrian Somers int 11373006ec67SBrian Somers bundle_FillQueues(struct bundle *bundle) 11383006ec67SBrian Somers { 11393b0f8d2eSBrian Somers int total; 11403b0f8d2eSBrian Somers 11411bc9b5baSBrian Somers if (bundle->ncp.mp.active) 11423b0f8d2eSBrian Somers total = mp_FillQueues(bundle); 11431bc9b5baSBrian Somers else { 11441bc9b5baSBrian Somers struct datalink *dl; 11451bc9b5baSBrian Somers int add; 11461bc9b5baSBrian Somers 11471bc9b5baSBrian Somers for (total = 0, dl = bundle->links; dl; dl = dl->next) 11481bc9b5baSBrian Somers if (dl->state == DATALINK_OPEN) { 11491bc9b5baSBrian Somers add = link_QueueLen(&dl->physical->link); 11501bc9b5baSBrian Somers if (add == 0 && dl->physical->out == NULL) 11511bc9b5baSBrian Somers add = ip_FlushPacket(&dl->physical->link, bundle); 11521bc9b5baSBrian Somers total += add; 11531bc9b5baSBrian Somers } 11543006ec67SBrian Somers } 11553006ec67SBrian Somers 11563b0f8d2eSBrian Somers return total + ip_QueueLen(); 11573006ec67SBrian Somers } 1158aef795ccSBrian Somers 1159aef795ccSBrian Somers int 1160aef795ccSBrian Somers bundle_ShowLinks(struct cmdargs const *arg) 1161aef795ccSBrian Somers { 1162aef795ccSBrian Somers struct datalink *dl; 1163aef795ccSBrian Somers 11649c53a7b1SBrian Somers for (dl = arg->bundle->links; dl; dl = dl->next) { 1165d4156d00SBrian Somers prompt_Printf(arg->prompt, "Name: %s [%s, %s]", 1166d4156d00SBrian Somers dl->name, mode2Nam(dl->physical->type), datalink_State(dl)); 1167eeab6bf5SBrian Somers if (dl->physical->link.throughput.rolling && dl->state == DATALINK_OPEN) 1168d4156d00SBrian Somers prompt_Printf(arg->prompt, " weight %d, %d bytes/sec", 1169eeab6bf5SBrian Somers dl->mp.weight, 11709c53a7b1SBrian Somers dl->physical->link.throughput.OctetsPerSecond); 11719c53a7b1SBrian Somers prompt_Printf(arg->prompt, "\n"); 11729c53a7b1SBrian Somers } 1173aef795ccSBrian Somers 1174aef795ccSBrian Somers return 0; 1175aef795ccSBrian Somers } 1176ab886ad0SBrian Somers 11771342caedSBrian Somers static const char * 11781342caedSBrian Somers optval(struct bundle *bundle, int bit) 11791342caedSBrian Somers { 11801342caedSBrian Somers return (bundle->cfg.opt & bit) ? "enabled" : "disabled"; 11811342caedSBrian Somers } 11821342caedSBrian Somers 1183c08717dfSBrian Somers int 1184c08717dfSBrian Somers bundle_ShowStatus(struct cmdargs const *arg) 1185c08717dfSBrian Somers { 1186c08717dfSBrian Somers int remaining; 1187c08717dfSBrian Somers 1188c08717dfSBrian Somers prompt_Printf(arg->prompt, "Phase %s\n", bundle_PhaseName(arg->bundle)); 1189faefde08SBrian Somers prompt_Printf(arg->prompt, " Device: %s\n", arg->bundle->dev.Name); 1190faefde08SBrian Somers prompt_Printf(arg->prompt, " Interface: %s @ %lubps\n", 1191faefde08SBrian Somers arg->bundle->ifp.Name, arg->bundle->ifp.Speed); 1192c08717dfSBrian Somers 1193c08717dfSBrian Somers prompt_Printf(arg->prompt, "\nDefaults:\n"); 1194643f4904SBrian Somers prompt_Printf(arg->prompt, " Label: %s\n", arg->bundle->cfg.label); 1195610b185fSBrian Somers prompt_Printf(arg->prompt, " Auth name: %s\n", 1196610b185fSBrian Somers arg->bundle->cfg.auth.name); 119704eaa58cSBrian Somers prompt_Printf(arg->prompt, " Auto Load: Up after %ds of >= %d packets\n", 119804eaa58cSBrian Somers arg->bundle->cfg.autoload.max.timeout, 119904eaa58cSBrian Somers arg->bundle->cfg.autoload.max.packets); 120004eaa58cSBrian Somers prompt_Printf(arg->prompt, " Down after %ds of <= %d" 120104eaa58cSBrian Somers " packets\n", arg->bundle->cfg.autoload.min.timeout, 120204eaa58cSBrian Somers arg->bundle->cfg.autoload.min.packets); 120304eaa58cSBrian Somers if (arg->bundle->autoload.timer.state == TIMER_RUNNING) 120404eaa58cSBrian Somers prompt_Printf(arg->prompt, " %ds remaining 'till " 120504eaa58cSBrian Somers "a link comes %s\n", 120604eaa58cSBrian Somers bundle_RemainingAutoLoadTime(arg->bundle), 120704eaa58cSBrian Somers arg->bundle->autoload.comingup ? "up" : "down"); 120804eaa58cSBrian Somers else 120904eaa58cSBrian Somers prompt_Printf(arg->prompt, " %srunning with %d" 121004eaa58cSBrian Somers " packets queued\n", arg->bundle->autoload.running ? 121104eaa58cSBrian Somers "" : "not ", ip_QueueLen()); 121204eaa58cSBrian Somers 1213c08717dfSBrian Somers prompt_Printf(arg->prompt, " Idle Timer: "); 1214c08717dfSBrian Somers if (arg->bundle->cfg.idle_timeout) { 1215c08717dfSBrian Somers prompt_Printf(arg->prompt, "%ds", arg->bundle->cfg.idle_timeout); 1216c08717dfSBrian Somers remaining = bundle_RemainingIdleTime(arg->bundle); 1217c08717dfSBrian Somers if (remaining != -1) 1218c08717dfSBrian Somers prompt_Printf(arg->prompt, " (%ds remaining)", remaining); 1219c08717dfSBrian Somers prompt_Printf(arg->prompt, "\n"); 1220c08717dfSBrian Somers } else 1221c08717dfSBrian Somers prompt_Printf(arg->prompt, "disabled\n"); 1222ce828a6eSBrian Somers prompt_Printf(arg->prompt, " MTU: "); 1223ce828a6eSBrian Somers if (arg->bundle->cfg.mtu) 1224ce828a6eSBrian Somers prompt_Printf(arg->prompt, "%d\n", arg->bundle->cfg.mtu); 1225ce828a6eSBrian Somers else 1226ce828a6eSBrian Somers prompt_Printf(arg->prompt, "unspecified\n"); 1227c08717dfSBrian Somers 1228610b185fSBrian Somers prompt_Printf(arg->prompt, " Sticky Routes: %s\n", 1229610b185fSBrian Somers optval(arg->bundle, OPT_SROUTES)); 12301342caedSBrian Somers prompt_Printf(arg->prompt, " ID check: %s\n", 12311342caedSBrian Somers optval(arg->bundle, OPT_IDCHECK)); 12321342caedSBrian Somers prompt_Printf(arg->prompt, " Loopback: %s\n", 12331342caedSBrian Somers optval(arg->bundle, OPT_LOOPBACK)); 12341342caedSBrian Somers prompt_Printf(arg->prompt, " PasswdAuth: %s\n", 12351342caedSBrian Somers optval(arg->bundle, OPT_PASSWDAUTH)); 12361342caedSBrian Somers prompt_Printf(arg->prompt, " Proxy: %s\n", 12371342caedSBrian Somers optval(arg->bundle, OPT_PROXY)); 12381342caedSBrian Somers prompt_Printf(arg->prompt, " Throughput: %s\n", 12391342caedSBrian Somers optval(arg->bundle, OPT_THROUGHPUT)); 1240610b185fSBrian Somers prompt_Printf(arg->prompt, " Utmp Logging: %s\n", 12411342caedSBrian Somers optval(arg->bundle, OPT_UTMP)); 12421342caedSBrian Somers 1243c08717dfSBrian Somers return 0; 1244c08717dfSBrian Somers } 1245c08717dfSBrian Somers 1246ab886ad0SBrian Somers static void 1247ab886ad0SBrian Somers bundle_IdleTimeout(void *v) 1248ab886ad0SBrian Somers { 1249ab886ad0SBrian Somers struct bundle *bundle = (struct bundle *)v; 1250ab886ad0SBrian Somers 1251dd7e2610SBrian Somers log_Printf(LogPHASE, "Idle timer expired.\n"); 125204eaa58cSBrian Somers bundle_StopIdleTimer(bundle); 12539c81b87dSBrian Somers bundle_Close(bundle, NULL, CLOSE_STAYDOWN); 1254ab886ad0SBrian Somers } 1255ab886ad0SBrian Somers 1256ab886ad0SBrian Somers /* 1257ab886ad0SBrian Somers * Start Idle timer. If timeout is reached, we call bundle_Close() to 1258ab886ad0SBrian Somers * close LCP and link. 1259ab886ad0SBrian Somers */ 1260ab886ad0SBrian Somers void 1261ab886ad0SBrian Somers bundle_StartIdleTimer(struct bundle *bundle) 1262ab886ad0SBrian Somers { 1263dd7e2610SBrian Somers timer_Stop(&bundle->idle.timer); 1264ff0f9439SBrian Somers if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) != 1265ff0f9439SBrian Somers bundle->phys_type.open && bundle->cfg.idle_timeout) { 126693ee0ff2SBrian Somers bundle->idle.timer.func = bundle_IdleTimeout; 12673b0f8d2eSBrian Somers bundle->idle.timer.name = "idle"; 126893ee0ff2SBrian Somers bundle->idle.timer.load = bundle->cfg.idle_timeout * SECTICKS; 126993ee0ff2SBrian Somers bundle->idle.timer.arg = bundle; 1270dd7e2610SBrian Somers timer_Start(&bundle->idle.timer); 127193ee0ff2SBrian Somers bundle->idle.done = time(NULL) + bundle->cfg.idle_timeout; 1272ab886ad0SBrian Somers } 1273ab886ad0SBrian Somers } 1274ab886ad0SBrian Somers 1275ab886ad0SBrian Somers void 1276ab886ad0SBrian Somers bundle_SetIdleTimer(struct bundle *bundle, int value) 1277ab886ad0SBrian Somers { 1278ab886ad0SBrian Somers bundle->cfg.idle_timeout = value; 1279ab886ad0SBrian Somers if (bundle_LinkIsUp(bundle)) 1280ab886ad0SBrian Somers bundle_StartIdleTimer(bundle); 1281ab886ad0SBrian Somers } 1282ab886ad0SBrian Somers 1283ab886ad0SBrian Somers void 1284ab886ad0SBrian Somers bundle_StopIdleTimer(struct bundle *bundle) 1285ab886ad0SBrian Somers { 1286dd7e2610SBrian Somers timer_Stop(&bundle->idle.timer); 12874a632c80SBrian Somers bundle->idle.done = 0; 1288ab886ad0SBrian Somers } 1289ab886ad0SBrian Somers 129004eaa58cSBrian Somers static int 1291ab886ad0SBrian Somers bundle_RemainingIdleTime(struct bundle *bundle) 1292ab886ad0SBrian Somers { 129393ee0ff2SBrian Somers if (bundle->idle.done) 129493ee0ff2SBrian Somers return bundle->idle.done - time(NULL); 1295ab886ad0SBrian Somers return -1; 1296ab886ad0SBrian Somers } 12973b0f8d2eSBrian Somers 12983b0f8d2eSBrian Somers int 12993b0f8d2eSBrian Somers bundle_IsDead(struct bundle *bundle) 13003b0f8d2eSBrian Somers { 13013b0f8d2eSBrian Somers return !bundle->links || (bundle->phase == PHASE_DEAD && bundle->CleaningUp); 13023b0f8d2eSBrian Somers } 1303b6217683SBrian Somers 130404eaa58cSBrian Somers static struct datalink * 130504eaa58cSBrian Somers bundle_DatalinkLinkout(struct bundle *bundle, struct datalink *dl) 1306cd7bd93aSBrian Somers { 1307cd7bd93aSBrian Somers struct datalink **dlp; 1308cd7bd93aSBrian Somers 1309cd7bd93aSBrian Somers for (dlp = &bundle->links; *dlp; dlp = &(*dlp)->next) 1310cd7bd93aSBrian Somers if (*dlp == dl) { 131104eaa58cSBrian Somers *dlp = dl->next; 131204eaa58cSBrian Somers dl->next = NULL; 131304eaa58cSBrian Somers bundle_LinksRemoved(bundle); 131404eaa58cSBrian Somers return dl; 1315cd7bd93aSBrian Somers } 131604eaa58cSBrian Somers 131704eaa58cSBrian Somers return NULL; 131804eaa58cSBrian Somers } 131904eaa58cSBrian Somers 132004eaa58cSBrian Somers static void 132104eaa58cSBrian Somers bundle_DatalinkLinkin(struct bundle *bundle, struct datalink *dl) 132204eaa58cSBrian Somers { 132304eaa58cSBrian Somers struct datalink **dlp = &bundle->links; 132404eaa58cSBrian Somers 132504eaa58cSBrian Somers while (*dlp) 132604eaa58cSBrian Somers dlp = &(*dlp)->next; 132704eaa58cSBrian Somers 132804eaa58cSBrian Somers *dlp = dl; 132904eaa58cSBrian Somers dl->next = NULL; 133004eaa58cSBrian Somers 133104eaa58cSBrian Somers bundle_LinkAdded(bundle, dl); 1332565e35e5SBrian Somers } 1333565e35e5SBrian Somers 1334565e35e5SBrian Somers void 1335565e35e5SBrian Somers bundle_CleanDatalinks(struct bundle *bundle) 1336565e35e5SBrian Somers { 1337565e35e5SBrian Somers struct datalink **dlp = &bundle->links; 133804eaa58cSBrian Somers int found = 0; 1339565e35e5SBrian Somers 1340565e35e5SBrian Somers while (*dlp) 1341565e35e5SBrian Somers if ((*dlp)->state == DATALINK_CLOSED && 134281358fa3SBrian Somers (*dlp)->physical->type & (PHYS_DIRECT|PHYS_BACKGROUND)) { 1343565e35e5SBrian Somers *dlp = datalink_Destroy(*dlp); 134404eaa58cSBrian Somers found++; 134504eaa58cSBrian Somers } else 1346565e35e5SBrian Somers dlp = &(*dlp)->next; 134704eaa58cSBrian Somers 134804eaa58cSBrian Somers if (found) 134904eaa58cSBrian Somers bundle_LinksRemoved(bundle); 135004eaa58cSBrian Somers } 135104eaa58cSBrian Somers 135204eaa58cSBrian Somers int 135304eaa58cSBrian Somers bundle_DatalinkClone(struct bundle *bundle, struct datalink *dl, 135404eaa58cSBrian Somers const char *name) 135504eaa58cSBrian Somers { 135604eaa58cSBrian Somers if (bundle2datalink(bundle, name)) { 135704eaa58cSBrian Somers log_Printf(LogWARN, "Clone: %s: name already exists\n", name); 135804eaa58cSBrian Somers return 0; 135904eaa58cSBrian Somers } 136004eaa58cSBrian Somers 136104eaa58cSBrian Somers bundle_DatalinkLinkin(bundle, datalink_Clone(dl, name)); 136204eaa58cSBrian Somers return 1; 136304eaa58cSBrian Somers } 136404eaa58cSBrian Somers 136504eaa58cSBrian Somers void 136604eaa58cSBrian Somers bundle_DatalinkRemove(struct bundle *bundle, struct datalink *dl) 136704eaa58cSBrian Somers { 136804eaa58cSBrian Somers dl = bundle_DatalinkLinkout(bundle, dl); 136904eaa58cSBrian Somers if (dl) 137004eaa58cSBrian Somers datalink_Destroy(dl); 1371cd7bd93aSBrian Somers } 137249052c95SBrian Somers 137349052c95SBrian Somers void 137449052c95SBrian Somers bundle_SetLabel(struct bundle *bundle, const char *label) 137549052c95SBrian Somers { 137649052c95SBrian Somers if (label) 137749052c95SBrian Somers strncpy(bundle->cfg.label, label, sizeof bundle->cfg.label - 1); 137849052c95SBrian Somers else 137949052c95SBrian Somers *bundle->cfg.label = '\0'; 138049052c95SBrian Somers } 138149052c95SBrian Somers 138249052c95SBrian Somers const char * 138349052c95SBrian Somers bundle_GetLabel(struct bundle *bundle) 138449052c95SBrian Somers { 138549052c95SBrian Somers return *bundle->cfg.label ? bundle->cfg.label : NULL; 138649052c95SBrian Somers } 13871fa665f5SBrian Somers 13881fa665f5SBrian Somers void 138996c9bb21SBrian Somers bundle_ReceiveDatalink(struct bundle *bundle, int s, struct sockaddr_un *sun) 13901fa665f5SBrian Somers { 139196c9bb21SBrian Somers char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int)]; 139296c9bb21SBrian Somers struct cmsghdr *cmsg = (struct cmsghdr *)cmsgbuf; 139396c9bb21SBrian Somers struct msghdr msg; 139496c9bb21SBrian Somers struct iovec iov[SCATTER_SEGMENTS]; 1395b7c5748eSBrian Somers struct datalink *dl; 139696c9bb21SBrian Somers int niov, link_fd, expect, f; 139785fd273aSBrian Somers pid_t pid; 13986f384573SBrian Somers 1399dd7e2610SBrian Somers log_Printf(LogPHASE, "Receiving datalink\n"); 14006f384573SBrian Somers 140196c9bb21SBrian Somers /* Create our scatter/gather array */ 140296c9bb21SBrian Somers niov = 1; 140396c9bb21SBrian Somers iov[0].iov_len = strlen(Version) + 1; 140496c9bb21SBrian Somers iov[0].iov_base = (char *)malloc(iov[0].iov_len); 140585fd273aSBrian Somers if (datalink2iov(NULL, iov, &niov, sizeof iov / sizeof *iov, 0) == -1) { 140654cd8e13SBrian Somers close(s); 14076f384573SBrian Somers return; 140854cd8e13SBrian Somers } 14096f384573SBrian Somers 141085fd273aSBrian Somers pid = getpid(); 141185fd273aSBrian Somers write(s, &pid, sizeof pid); 141285fd273aSBrian Somers 141396c9bb21SBrian Somers for (f = expect = 0; f < niov; f++) 141496c9bb21SBrian Somers expect += iov[f].iov_len; 141596c9bb21SBrian Somers 141696c9bb21SBrian Somers /* Set up our message */ 141796c9bb21SBrian Somers cmsg->cmsg_len = sizeof cmsgbuf; 141896c9bb21SBrian Somers cmsg->cmsg_level = SOL_SOCKET; 141954cd8e13SBrian Somers cmsg->cmsg_type = 0; 142096c9bb21SBrian Somers 142196c9bb21SBrian Somers memset(&msg, '\0', sizeof msg); 142296c9bb21SBrian Somers msg.msg_name = (caddr_t)sun; 142396c9bb21SBrian Somers msg.msg_namelen = sizeof *sun; 142496c9bb21SBrian Somers msg.msg_iov = iov; 142596c9bb21SBrian Somers msg.msg_iovlen = niov; 142696c9bb21SBrian Somers msg.msg_control = cmsgbuf; 142796c9bb21SBrian Somers msg.msg_controllen = sizeof cmsgbuf; 142896c9bb21SBrian Somers 142996c9bb21SBrian Somers log_Printf(LogDEBUG, "Expecting %d scatter/gather bytes\n", expect); 143096c9bb21SBrian Somers f = expect + 100; 143196c9bb21SBrian Somers setsockopt(s, SOL_SOCKET, SO_RCVBUF, &f, sizeof f); 143296c9bb21SBrian Somers if ((f = recvmsg(s, &msg, MSG_WAITALL)) != expect) { 143396c9bb21SBrian Somers if (f == -1) 143496c9bb21SBrian Somers log_Printf(LogERROR, "Failed recvmsg: %s\n", strerror(errno)); 143596c9bb21SBrian Somers else 143696c9bb21SBrian Somers log_Printf(LogERROR, "Failed recvmsg: Got %d, not %d\n", f, expect); 143796c9bb21SBrian Somers while (niov--) 143896c9bb21SBrian Somers free(iov[niov].iov_base); 143954cd8e13SBrian Somers close(s); 144096c9bb21SBrian Somers return; 144196c9bb21SBrian Somers } 144296c9bb21SBrian Somers 1443c0d9a877SBrian Somers write(s, "!", 1); /* ACK */ 1444ad5b0e8bSBrian Somers close(s); 1445c0d9a877SBrian Somers 1446ad5b0e8bSBrian Somers if (cmsg->cmsg_type != SCM_RIGHTS) { 1447ad5b0e8bSBrian Somers log_Printf(LogERROR, "Recvmsg: no descriptor received !\n"); 1448ad5b0e8bSBrian Somers while (niov--) 1449ad5b0e8bSBrian Somers free(iov[niov].iov_base); 1450ad5b0e8bSBrian Somers return; 145154cd8e13SBrian Somers } 145254cd8e13SBrian Somers 1453ad5b0e8bSBrian Somers /* We've successfully received an open file descriptor through our socket */ 1454ad5b0e8bSBrian Somers log_Printf(LogDEBUG, "Receiving device descriptor\n"); 1455ad5b0e8bSBrian Somers link_fd = *(int *)CMSG_DATA(cmsg); 1456ad5b0e8bSBrian Somers 145796c9bb21SBrian Somers if (strncmp(Version, iov[0].iov_base, iov[0].iov_len)) { 145896c9bb21SBrian Somers log_Printf(LogWARN, "Cannot receive datalink, incorrect version" 145996c9bb21SBrian Somers " (\"%.*s\", not \"%s\")\n", (int)iov[0].iov_len, 14607d81ddf5SBrian Somers (char *)iov[0].iov_base, Version); 146196c9bb21SBrian Somers close(link_fd); 146296c9bb21SBrian Somers while (niov--) 146396c9bb21SBrian Somers free(iov[niov].iov_base); 146496c9bb21SBrian Somers return; 146596c9bb21SBrian Somers } 146696c9bb21SBrian Somers 146796c9bb21SBrian Somers niov = 1; 1468b7c5748eSBrian Somers dl = iov2datalink(bundle, iov, &niov, sizeof iov / sizeof *iov, link_fd); 1469b7c5748eSBrian Somers if (dl) { 147004eaa58cSBrian Somers bundle_DatalinkLinkin(bundle, dl); 1471b7c5748eSBrian Somers datalink_AuthOk(dl); 147296c9bb21SBrian Somers } else 147396c9bb21SBrian Somers close(link_fd); 147496c9bb21SBrian Somers 147596c9bb21SBrian Somers free(iov[0].iov_base); 14761fa665f5SBrian Somers } 14771fa665f5SBrian Somers 14781fa665f5SBrian Somers void 147996c9bb21SBrian Somers bundle_SendDatalink(struct datalink *dl, int s, struct sockaddr_un *sun) 14801fa665f5SBrian Somers { 1481c0d9a877SBrian Somers char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int)], ack; 148296c9bb21SBrian Somers struct cmsghdr *cmsg = (struct cmsghdr *)cmsgbuf; 148396c9bb21SBrian Somers struct msghdr msg; 148496c9bb21SBrian Somers struct iovec iov[SCATTER_SEGMENTS]; 14851384bd27SBrian Somers int niov, link_fd, f, expect, newsid; 148685fd273aSBrian Somers pid_t newpid; 14876f384573SBrian Somers 1488dd7e2610SBrian Somers log_Printf(LogPHASE, "Transmitting datalink %s\n", dl->name); 14896f384573SBrian Somers 149004eaa58cSBrian Somers bundle_LinkClosed(dl->bundle, dl); 14910f2f3eb3SBrian Somers bundle_DatalinkLinkout(dl->bundle, dl); 1492ea722969SBrian Somers 149396c9bb21SBrian Somers /* Build our scatter/gather array */ 149496c9bb21SBrian Somers iov[0].iov_len = strlen(Version) + 1; 149596c9bb21SBrian Somers iov[0].iov_base = strdup(Version); 149696c9bb21SBrian Somers niov = 1; 14976f384573SBrian Somers 149885fd273aSBrian Somers read(s, &newpid, sizeof newpid); 149985fd273aSBrian Somers link_fd = datalink2iov(dl, iov, &niov, sizeof iov / sizeof *iov, newpid); 15006f384573SBrian Somers 15016f384573SBrian Somers if (link_fd != -1) { 150296c9bb21SBrian Somers memset(&msg, '\0', sizeof msg); 150354cd8e13SBrian Somers 150496c9bb21SBrian Somers msg.msg_name = (caddr_t)sun; 150596c9bb21SBrian Somers msg.msg_namelen = sizeof *sun; 150696c9bb21SBrian Somers msg.msg_iov = iov; 150796c9bb21SBrian Somers msg.msg_iovlen = niov; 150854cd8e13SBrian Somers 150954cd8e13SBrian Somers cmsg->cmsg_len = sizeof cmsgbuf; 151054cd8e13SBrian Somers cmsg->cmsg_level = SOL_SOCKET; 151154cd8e13SBrian Somers cmsg->cmsg_type = SCM_RIGHTS; 151254cd8e13SBrian Somers *(int *)CMSG_DATA(cmsg) = link_fd; 151396c9bb21SBrian Somers msg.msg_control = cmsgbuf; 151496c9bb21SBrian Somers msg.msg_controllen = sizeof cmsgbuf; 151547723d29SBrian Somers 151696c9bb21SBrian Somers for (f = expect = 0; f < niov; f++) 151796c9bb21SBrian Somers expect += iov[f].iov_len; 151847723d29SBrian Somers 151996c9bb21SBrian Somers log_Printf(LogDEBUG, "Sending %d bytes in scatter/gather array\n", expect); 152047723d29SBrian Somers 152196c9bb21SBrian Somers f = expect + SOCKET_OVERHEAD; 152296c9bb21SBrian Somers setsockopt(s, SOL_SOCKET, SO_SNDBUF, &f, sizeof f); 152396c9bb21SBrian Somers if (sendmsg(s, &msg, 0) == -1) 152496c9bb21SBrian Somers log_Printf(LogERROR, "Failed sendmsg: %s\n", strerror(errno)); 1525c0d9a877SBrian Somers /* We must get the ACK before closing the descriptor ! */ 1526c0d9a877SBrian Somers read(s, &ack, 1); 152754cd8e13SBrian Somers 15281384bd27SBrian Somers newsid = tcgetpgrp(link_fd) == getpgrp(); 152996c9bb21SBrian Somers close(link_fd); 15301384bd27SBrian Somers if (newsid) 15311384bd27SBrian Somers bundle_setsid(dl->bundle, 1); 153247723d29SBrian Somers } 153385fd273aSBrian Somers close(s); 153496c9bb21SBrian Somers 153596c9bb21SBrian Somers while (niov--) 153696c9bb21SBrian Somers free(iov[niov].iov_base); 15371fa665f5SBrian Somers } 1538dd0645c5SBrian Somers 1539dd0645c5SBrian Somers int 154058d55334SBrian Somers bundle_RenameDatalink(struct bundle *bundle, struct datalink *ndl, 154158d55334SBrian Somers const char *name) 154258d55334SBrian Somers { 154358d55334SBrian Somers struct datalink *dl; 154458d55334SBrian Somers 154558d55334SBrian Somers if (!strcasecmp(ndl->name, name)) 154658d55334SBrian Somers return 1; 154758d55334SBrian Somers 154858d55334SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 154958d55334SBrian Somers if (!strcasecmp(dl->name, name)) 155058d55334SBrian Somers return 0; 155158d55334SBrian Somers 155258d55334SBrian Somers datalink_Rename(ndl, name); 155358d55334SBrian Somers return 1; 155458d55334SBrian Somers } 155558d55334SBrian Somers 155658d55334SBrian Somers int 1557dd0645c5SBrian Somers bundle_SetMode(struct bundle *bundle, struct datalink *dl, int mode) 1558dd0645c5SBrian Somers { 1559dd0645c5SBrian Somers int omode; 1560dd0645c5SBrian Somers 1561dd0645c5SBrian Somers omode = dl->physical->type; 1562dd0645c5SBrian Somers if (omode == mode) 1563dd0645c5SBrian Somers return 1; 1564dd0645c5SBrian Somers 1565ff0f9439SBrian Somers if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO)) 1566ff0f9439SBrian Somers /* First auto link */ 1567dd0645c5SBrian Somers if (bundle->ncp.ipcp.peer_ip.s_addr == INADDR_ANY) { 1568ff0f9439SBrian Somers log_Printf(LogWARN, "You must `set ifaddr' or `open' before" 1569ff0f9439SBrian Somers " changing mode to %s\n", mode2Nam(mode)); 1570dd0645c5SBrian Somers return 0; 1571dd0645c5SBrian Somers } 1572dd0645c5SBrian Somers 1573dd0645c5SBrian Somers if (!datalink_SetMode(dl, mode)) 1574dd0645c5SBrian Somers return 0; 1575dd0645c5SBrian Somers 1576ff0f9439SBrian Somers if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO) && 1577ff0f9439SBrian Somers bundle->phase != PHASE_NETWORK) 1578ff0f9439SBrian Somers /* First auto link, we need an interface */ 1579dd0645c5SBrian Somers ipcp_InterfaceUp(&bundle->ncp.ipcp); 1580dd0645c5SBrian Somers 158104eaa58cSBrian Somers /* Regenerate phys_type and adjust autoload & idle timers */ 158204eaa58cSBrian Somers bundle_LinksRemoved(bundle); 1583dd0645c5SBrian Somers 1584ff0f9439SBrian Somers if (omode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO) && 1585ff0f9439SBrian Somers bundle->phase != PHASE_NETWORK) 1586ff0f9439SBrian Somers /* No auto links left */ 1587dd0645c5SBrian Somers ipcp_CleanInterface(&bundle->ncp.ipcp); 1588dd0645c5SBrian Somers 1589dd0645c5SBrian Somers return 1; 1590dd0645c5SBrian Somers } 15911384bd27SBrian Somers 15921384bd27SBrian Somers void 15931384bd27SBrian Somers bundle_setsid(struct bundle *bundle, int holdsession) 15941384bd27SBrian Somers { 15951384bd27SBrian Somers /* 15961384bd27SBrian Somers * Lose the current session. This means getting rid of our pid 15971384bd27SBrian Somers * too so that the tty device will really go away, and any getty 15981384bd27SBrian Somers * etc will be allowed to restart. 15991384bd27SBrian Somers */ 16001384bd27SBrian Somers pid_t pid, orig; 16011384bd27SBrian Somers int fds[2]; 16021384bd27SBrian Somers char done; 16031384bd27SBrian Somers struct datalink *dl; 16041384bd27SBrian Somers 16051384bd27SBrian Somers orig = getpid(); 16061384bd27SBrian Somers if (pipe(fds) == -1) { 16071384bd27SBrian Somers log_Printf(LogERROR, "pipe: %s\n", strerror(errno)); 16081384bd27SBrian Somers return; 16091384bd27SBrian Somers } 16101384bd27SBrian Somers switch ((pid = fork())) { 16111384bd27SBrian Somers case -1: 16121384bd27SBrian Somers log_Printf(LogERROR, "fork: %s\n", strerror(errno)); 16131384bd27SBrian Somers close(fds[0]); 16141384bd27SBrian Somers close(fds[1]); 16151384bd27SBrian Somers return; 16161384bd27SBrian Somers case 0: 16171384bd27SBrian Somers close(fds[0]); 16181384bd27SBrian Somers read(fds[1], &done, 1); /* uu_locks are mine ! */ 16191384bd27SBrian Somers close(fds[1]); 16201384bd27SBrian Somers if (pipe(fds) == -1) { 16211384bd27SBrian Somers log_Printf(LogERROR, "pipe(2): %s\n", strerror(errno)); 16221384bd27SBrian Somers return; 16231384bd27SBrian Somers } 16241384bd27SBrian Somers switch ((pid = fork())) { 16251384bd27SBrian Somers case -1: 1626a33b2ef7SBrian Somers log_Printf(LogERROR, "fork(2): %s\n", strerror(errno)); 16271384bd27SBrian Somers close(fds[0]); 16281384bd27SBrian Somers close(fds[1]); 16291384bd27SBrian Somers return; 16301384bd27SBrian Somers case 0: 16311384bd27SBrian Somers close(fds[0]); 16321384bd27SBrian Somers bundle_LockTun(bundle); /* update pid */ 16331384bd27SBrian Somers read(fds[1], &done, 1); /* uu_locks are mine ! */ 16341384bd27SBrian Somers close(fds[1]); 16351384bd27SBrian Somers setsid(); 16361384bd27SBrian Somers log_Printf(LogPHASE, "%d -> %d: %s session control\n", 16371384bd27SBrian Somers (int)orig, (int)getpid(), 16381384bd27SBrian Somers holdsession ? "Passed" : "Dropped"); 1639d2a69fc3SBrian Somers timer_InitService(); 16401384bd27SBrian Somers break; 16411384bd27SBrian Somers default: 16421384bd27SBrian Somers close(fds[1]); 16431384bd27SBrian Somers /* Give away all our modem locks (to the final process) */ 16441384bd27SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 16451384bd27SBrian Somers if (dl->state != DATALINK_CLOSED) 16461384bd27SBrian Somers modem_ChangedPid(dl->physical, pid); 16471384bd27SBrian Somers write(fds[0], "!", 1); /* done */ 16481384bd27SBrian Somers close(fds[0]); 16491384bd27SBrian Somers exit(0); 16501384bd27SBrian Somers break; 16511384bd27SBrian Somers } 16521384bd27SBrian Somers break; 16531384bd27SBrian Somers default: 16541384bd27SBrian Somers close(fds[1]); 16551384bd27SBrian Somers /* Give away all our modem locks (to the intermediate process) */ 16561384bd27SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 16571384bd27SBrian Somers if (dl->state != DATALINK_CLOSED) 16581384bd27SBrian Somers modem_ChangedPid(dl->physical, pid); 16591384bd27SBrian Somers write(fds[0], "!", 1); /* done */ 16601384bd27SBrian Somers close(fds[0]); 16611384bd27SBrian Somers if (holdsession) { 16621384bd27SBrian Somers int fd, status; 16631384bd27SBrian Somers 16641384bd27SBrian Somers timer_TermService(); 16651384bd27SBrian Somers signal(SIGPIPE, SIG_DFL); 16661384bd27SBrian Somers signal(SIGALRM, SIG_DFL); 16671384bd27SBrian Somers signal(SIGHUP, SIG_DFL); 16681384bd27SBrian Somers signal(SIGTERM, SIG_DFL); 16691384bd27SBrian Somers signal(SIGINT, SIG_DFL); 16701384bd27SBrian Somers signal(SIGQUIT, SIG_DFL); 16711384bd27SBrian Somers for (fd = getdtablesize(); fd >= 0; fd--) 16721384bd27SBrian Somers close(fd); 16731384bd27SBrian Somers setuid(geteuid()); 16741384bd27SBrian Somers /* 16751384bd27SBrian Somers * Reap the intermediate process. As we're not exiting but the 16761384bd27SBrian Somers * intermediate is, we don't want it to become defunct. 16771384bd27SBrian Somers */ 16781384bd27SBrian Somers waitpid(pid, &status, 0); 16798e7b8599SBrian Somers /* Tweak our process arguments.... */ 16808e7b8599SBrian Somers bundle->argv[0] = "session owner"; 16818e7b8599SBrian Somers bundle->argv[1] = NULL; 16821384bd27SBrian Somers /* 16831384bd27SBrian Somers * Hang around for a HUP. This should happen as soon as the 16841384bd27SBrian Somers * ppp that we passed our ctty descriptor to closes it. 16851384bd27SBrian Somers * NOTE: If this process dies, the passed descriptor becomes 16861384bd27SBrian Somers * invalid and will give a select() error by setting one 16871384bd27SBrian Somers * of the error fds, aborting the other ppp. We don't 16881384bd27SBrian Somers * want that to happen ! 16891384bd27SBrian Somers */ 16901384bd27SBrian Somers pause(); 16911384bd27SBrian Somers } 16921384bd27SBrian Somers exit(0); 16931384bd27SBrian Somers break; 16941384bd27SBrian Somers } 16951384bd27SBrian Somers } 1696