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 * 261384bd27SBrian Somers * $Id: bundle.c,v 1.9 1998/05/28 23:15:29 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> 38e43ebac1SBrian Somers #include <net/if_tun.h> 391fa665f5SBrian Somers #include <sys/un.h> 407a6f8720SBrian Somers 417a6f8720SBrian Somers #include <errno.h> 427a6f8720SBrian Somers #include <fcntl.h> 4347723d29SBrian Somers #include <paths.h> 4454cd8e13SBrian Somers #include <signal.h> 457a6f8720SBrian Somers #include <stdio.h> 466f384573SBrian Somers #include <stdlib.h> 477a6f8720SBrian Somers #include <string.h> 487a6f8720SBrian Somers #include <sys/ioctl.h> 4996c9bb21SBrian Somers #include <sys/uio.h> 5054cd8e13SBrian Somers #include <sys/wait.h> 517a6f8720SBrian Somers #include <termios.h> 527a6f8720SBrian Somers #include <unistd.h> 537a6f8720SBrian Somers 547a6f8720SBrian Somers #include "command.h" 557a6f8720SBrian Somers #include "mbuf.h" 567a6f8720SBrian Somers #include "log.h" 577a6f8720SBrian Somers #include "id.h" 587a6f8720SBrian Somers #include "defs.h" 597a6f8720SBrian Somers #include "timer.h" 607a6f8720SBrian Somers #include "fsm.h" 617a6f8720SBrian Somers #include "iplist.h" 62879ed6faSBrian Somers #include "lqr.h" 63455aabc3SBrian Somers #include "hdlc.h" 647a6f8720SBrian Somers #include "throughput.h" 65eaa4df37SBrian Somers #include "slcompress.h" 667a6f8720SBrian Somers #include "ipcp.h" 675ca5389aSBrian Somers #include "filter.h" 682f786681SBrian Somers #include "descriptor.h" 697a6f8720SBrian Somers #include "route.h" 707a6f8720SBrian Somers #include "lcp.h" 717a6f8720SBrian Somers #include "ccp.h" 723b0f8d2eSBrian Somers #include "link.h" 733b0f8d2eSBrian Somers #include "mp.h" 743b0f8d2eSBrian Somers #include "bundle.h" 75455aabc3SBrian Somers #include "async.h" 76455aabc3SBrian Somers #include "physical.h" 772289f246SBrian Somers #include "modem.h" 78078c562eSBrian Somers #include "loadalias.h" 79455aabc3SBrian Somers #include "auth.h" 80455aabc3SBrian Somers #include "lcpproto.h" 81455aabc3SBrian Somers #include "chap.h" 82455aabc3SBrian Somers #include "tun.h" 8385b542cfSBrian Somers #include "prompt.h" 843006ec67SBrian Somers #include "chat.h" 853006ec67SBrian Somers #include "datalink.h" 863006ec67SBrian Somers #include "ip.h" 877a6f8720SBrian Somers 8896c9bb21SBrian Somers #define SCATTER_SEGMENTS 4 /* version, datalink, name, physical */ 8996c9bb21SBrian Somers #define SOCKET_OVERHEAD 100 /* additional buffer space for large */ 9096c9bb21SBrian Somers /* {recv,send}msg() calls */ 9196c9bb21SBrian Somers 9204eaa58cSBrian Somers static int bundle_RemainingIdleTime(struct bundle *); 9304eaa58cSBrian Somers static int bundle_RemainingAutoLoadTime(struct bundle *); 9404eaa58cSBrian Somers 95455aabc3SBrian Somers static const char *PhaseNames[] = { 96455aabc3SBrian Somers "Dead", "Establish", "Authenticate", "Network", "Terminate" 97455aabc3SBrian Somers }; 98455aabc3SBrian Somers 99455aabc3SBrian Somers const char * 100455aabc3SBrian Somers bundle_PhaseName(struct bundle *bundle) 1017a6f8720SBrian Somers { 102455aabc3SBrian Somers return bundle->phase <= PHASE_TERMINATE ? 103455aabc3SBrian Somers PhaseNames[bundle->phase] : "unknown"; 1047a6f8720SBrian Somers } 1057a6f8720SBrian Somers 106455aabc3SBrian Somers void 1075563ebdeSBrian Somers bundle_NewPhase(struct bundle *bundle, u_int new) 108455aabc3SBrian Somers { 109aef795ccSBrian Somers if (new == bundle->phase) 110aef795ccSBrian Somers return; 111aef795ccSBrian Somers 112e2ebb036SBrian Somers if (new <= PHASE_TERMINATE) 113dd7e2610SBrian Somers log_Printf(LogPHASE, "bundle: %s\n", PhaseNames[new]); 1147a6f8720SBrian Somers 115455aabc3SBrian Somers switch (new) { 116455aabc3SBrian Somers case PHASE_DEAD: 1170f2f3eb3SBrian Somers log_DisplayPrompts(); 118455aabc3SBrian Somers bundle->phase = new; 119455aabc3SBrian Somers break; 120455aabc3SBrian Somers 121455aabc3SBrian Somers case PHASE_ESTABLISH: 122455aabc3SBrian Somers bundle->phase = new; 123455aabc3SBrian Somers break; 124455aabc3SBrian Somers 125455aabc3SBrian Somers case PHASE_AUTHENTICATE: 126455aabc3SBrian Somers bundle->phase = new; 1270f2f3eb3SBrian Somers log_DisplayPrompts(); 128455aabc3SBrian Somers break; 129455aabc3SBrian Somers 130455aabc3SBrian Somers case PHASE_NETWORK: 1315828db6dSBrian Somers ipcp_Setup(&bundle->ncp.ipcp); 132dd7e2610SBrian Somers fsm_Up(&bundle->ncp.ipcp.fsm); 133dd7e2610SBrian Somers fsm_Open(&bundle->ncp.ipcp.fsm); 134673903ecSBrian Somers bundle->phase = new; 1350f2f3eb3SBrian Somers log_DisplayPrompts(); 136673903ecSBrian Somers break; 137455aabc3SBrian Somers 138455aabc3SBrian Somers case PHASE_TERMINATE: 139455aabc3SBrian Somers bundle->phase = new; 140673903ecSBrian Somers mp_Down(&bundle->ncp.mp); 1410f2f3eb3SBrian Somers log_DisplayPrompts(); 142455aabc3SBrian Somers break; 1437a6f8720SBrian Somers } 1447a6f8720SBrian Somers } 1457a6f8720SBrian Somers 1467a6f8720SBrian Somers static int 1477a6f8720SBrian Somers bundle_CleanInterface(const struct bundle *bundle) 1487a6f8720SBrian Somers { 1497a6f8720SBrian Somers int s; 1507a6f8720SBrian Somers struct ifreq ifrq; 1517a6f8720SBrian Somers struct ifaliasreq ifra; 1527a6f8720SBrian Somers 1537a6f8720SBrian Somers s = ID0socket(AF_INET, SOCK_DGRAM, 0); 1547a6f8720SBrian Somers if (s < 0) { 155dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_CleanInterface: socket(): %s\n", 1567a6f8720SBrian Somers strerror(errno)); 1577a6f8720SBrian Somers return (-1); 1587a6f8720SBrian Somers } 159faefde08SBrian Somers strncpy(ifrq.ifr_name, bundle->ifp.Name, sizeof ifrq.ifr_name - 1); 1607a6f8720SBrian Somers ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0'; 1617a6f8720SBrian Somers while (ID0ioctl(s, SIOCGIFADDR, &ifrq) == 0) { 1627a6f8720SBrian Somers memset(&ifra.ifra_mask, '\0', sizeof ifra.ifra_mask); 163faefde08SBrian Somers strncpy(ifra.ifra_name, bundle->ifp.Name, sizeof ifra.ifra_name - 1); 1647a6f8720SBrian Somers ifra.ifra_name[sizeof ifra.ifra_name - 1] = '\0'; 1657a6f8720SBrian Somers ifra.ifra_addr = ifrq.ifr_addr; 1667a6f8720SBrian Somers if (ID0ioctl(s, SIOCGIFDSTADDR, &ifrq) < 0) { 1677a6f8720SBrian Somers if (ifra.ifra_addr.sa_family == AF_INET) 168dd7e2610SBrian Somers log_Printf(LogERROR, 1697a6f8720SBrian Somers "bundle_CleanInterface: Can't get dst for %s on %s !\n", 1707a6f8720SBrian Somers inet_ntoa(((struct sockaddr_in *)&ifra.ifra_addr)->sin_addr), 171faefde08SBrian Somers bundle->ifp.Name); 1720f8037a9SBrian Somers close(s); 1737a6f8720SBrian Somers return 0; 1747a6f8720SBrian Somers } 1757a6f8720SBrian Somers ifra.ifra_broadaddr = ifrq.ifr_dstaddr; 1767a6f8720SBrian Somers if (ID0ioctl(s, SIOCDIFADDR, &ifra) < 0) { 1777a6f8720SBrian Somers if (ifra.ifra_addr.sa_family == AF_INET) 178dd7e2610SBrian Somers log_Printf(LogERROR, 1797a6f8720SBrian Somers "bundle_CleanInterface: Can't delete %s address on %s !\n", 1807a6f8720SBrian Somers inet_ntoa(((struct sockaddr_in *)&ifra.ifra_addr)->sin_addr), 181faefde08SBrian Somers bundle->ifp.Name); 1820f8037a9SBrian Somers close(s); 1837a6f8720SBrian Somers return 0; 1847a6f8720SBrian Somers } 1857a6f8720SBrian Somers } 1860f8037a9SBrian Somers close(s); 1877a6f8720SBrian Somers 1887a6f8720SBrian Somers return 1; 1897a6f8720SBrian Somers } 1907a6f8720SBrian Somers 1916d666775SBrian Somers static void 1926d666775SBrian Somers bundle_LayerStart(void *v, struct fsm *fp) 1937a6f8720SBrian Somers { 1943006ec67SBrian Somers /* The given FSM is about to start up ! */ 1957a6f8720SBrian Somers } 1967a6f8720SBrian Somers 1975cf4388bSBrian Somers 1985cf4388bSBrian Somers static void 1995cf4388bSBrian Somers bundle_Notify(struct bundle *bundle, char c) 2005cf4388bSBrian Somers { 2015cf4388bSBrian Somers if (bundle->notify.fd != -1) { 2025cf4388bSBrian Somers if (write(bundle->notify.fd, &c, 1) == 1) 203dd7e2610SBrian Somers log_Printf(LogPHASE, "Parent notified of success.\n"); 2045cf4388bSBrian Somers else 205dd7e2610SBrian Somers log_Printf(LogPHASE, "Failed to notify parent of success.\n"); 2065cf4388bSBrian Somers close(bundle->notify.fd); 2075cf4388bSBrian Somers bundle->notify.fd = -1; 2085cf4388bSBrian Somers } 2095cf4388bSBrian Somers } 2103b0f8d2eSBrian Somers 2116d666775SBrian Somers static void 21204eaa58cSBrian Somers bundle_AutoLoadTimeout(void *v) 21304eaa58cSBrian Somers { 21404eaa58cSBrian Somers struct bundle *bundle = (struct bundle *)v; 21504eaa58cSBrian Somers 21604eaa58cSBrian Somers if (bundle->autoload.comingup) { 21704eaa58cSBrian Somers log_Printf(LogPHASE, "autoload: Another link is required\n"); 21804eaa58cSBrian Somers /* bundle_Open() stops the timer */ 21904eaa58cSBrian Somers bundle_Open(bundle, NULL, PHYS_DEMAND); 22004eaa58cSBrian Somers } else { 22104eaa58cSBrian Somers struct datalink *dl, *last; 22204eaa58cSBrian Somers 22304eaa58cSBrian Somers timer_Stop(&bundle->autoload.timer); 22404eaa58cSBrian Somers for (last = NULL, dl = bundle->links; dl; dl = dl->next) 22504eaa58cSBrian Somers if (dl->physical->type == PHYS_DEMAND && dl->state == DATALINK_OPEN) 22604eaa58cSBrian Somers last = dl; 22704eaa58cSBrian Somers 22804eaa58cSBrian Somers if (last) 22904eaa58cSBrian Somers datalink_Close(last, 1); 23004eaa58cSBrian Somers } 23104eaa58cSBrian Somers } 23204eaa58cSBrian Somers 23304eaa58cSBrian Somers static void 23404eaa58cSBrian Somers bundle_StartAutoLoadTimer(struct bundle *bundle, int up) 23504eaa58cSBrian Somers { 23604eaa58cSBrian Somers struct datalink *dl; 23704eaa58cSBrian Somers 23804eaa58cSBrian Somers timer_Stop(&bundle->autoload.timer); 23904eaa58cSBrian Somers 24004eaa58cSBrian Somers if (bundle->CleaningUp || bundle->phase != PHASE_NETWORK) { 24104eaa58cSBrian Somers dl = NULL; 24204eaa58cSBrian Somers bundle->autoload.running = 0; 24304eaa58cSBrian Somers } else if (up) { 24404eaa58cSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 24504eaa58cSBrian Somers if (dl->state == DATALINK_CLOSED && dl->physical->type == PHYS_DEMAND) { 24604eaa58cSBrian Somers if (bundle->cfg.autoload.max.timeout) { 24704eaa58cSBrian Somers bundle->autoload.timer.func = bundle_AutoLoadTimeout; 24804eaa58cSBrian Somers bundle->autoload.timer.name = "autoload up"; 24904eaa58cSBrian Somers bundle->autoload.timer.load = 25004eaa58cSBrian Somers bundle->cfg.autoload.max.timeout * SECTICKS; 25104eaa58cSBrian Somers bundle->autoload.timer.arg = bundle; 25204eaa58cSBrian Somers timer_Start(&bundle->autoload.timer); 25304eaa58cSBrian Somers bundle->autoload.done = time(NULL) + bundle->cfg.autoload.max.timeout; 25404eaa58cSBrian Somers } else 25504eaa58cSBrian Somers bundle_AutoLoadTimeout(bundle); 25604eaa58cSBrian Somers break; 25704eaa58cSBrian Somers } 25804eaa58cSBrian Somers bundle->autoload.running = (dl || bundle->cfg.autoload.min.timeout) ? 1 : 0; 25904eaa58cSBrian Somers } else { 26004eaa58cSBrian Somers int nlinks; 26104eaa58cSBrian Somers struct datalink *adl; 26204eaa58cSBrian Somers 26304eaa58cSBrian Somers for (nlinks = 0, adl = NULL, dl = bundle->links; dl; dl = dl->next) 26404eaa58cSBrian Somers if (dl->state == DATALINK_OPEN) { 26504eaa58cSBrian Somers if (dl->physical->type == PHYS_DEMAND) 26604eaa58cSBrian Somers adl = dl; 26704eaa58cSBrian Somers if (++nlinks > 1 && adl) { 26804eaa58cSBrian Somers if (bundle->cfg.autoload.min.timeout) { 26904eaa58cSBrian Somers bundle->autoload.timer.func = bundle_AutoLoadTimeout; 27004eaa58cSBrian Somers bundle->autoload.timer.name = "autoload down"; 27104eaa58cSBrian Somers bundle->autoload.timer.load = 27204eaa58cSBrian Somers bundle->cfg.autoload.min.timeout * SECTICKS; 27304eaa58cSBrian Somers bundle->autoload.timer.arg = bundle; 27404eaa58cSBrian Somers timer_Start(&bundle->autoload.timer); 27504eaa58cSBrian Somers bundle->autoload.done = 27604eaa58cSBrian Somers time(NULL) + bundle->cfg.autoload.min.timeout; 27704eaa58cSBrian Somers } 27804eaa58cSBrian Somers break; 27904eaa58cSBrian Somers } 28004eaa58cSBrian Somers } 28104eaa58cSBrian Somers 28204eaa58cSBrian Somers bundle->autoload.running = 1; 28304eaa58cSBrian Somers } 28404eaa58cSBrian Somers 28504eaa58cSBrian Somers bundle->autoload.comingup = up ? 1 : 0; 28604eaa58cSBrian Somers } 28704eaa58cSBrian Somers 28804eaa58cSBrian Somers static void 28904eaa58cSBrian Somers bundle_StopAutoLoadTimer(struct bundle *bundle) 29004eaa58cSBrian Somers { 29104eaa58cSBrian Somers timer_Stop(&bundle->autoload.timer); 29204eaa58cSBrian Somers bundle->autoload.done = 0; 29304eaa58cSBrian Somers } 29404eaa58cSBrian Somers 29504eaa58cSBrian Somers static int 29604eaa58cSBrian Somers bundle_RemainingAutoLoadTime(struct bundle *bundle) 29704eaa58cSBrian Somers { 29804eaa58cSBrian Somers if (bundle->autoload.done) 29904eaa58cSBrian Somers return bundle->autoload.done - time(NULL); 30004eaa58cSBrian Somers return -1; 30104eaa58cSBrian Somers } 30204eaa58cSBrian Somers 30304eaa58cSBrian Somers 30404eaa58cSBrian Somers static void 3056f384573SBrian Somers bundle_LayerUp(void *v, struct fsm *fp) 3067a6f8720SBrian Somers { 3073006ec67SBrian Somers /* 3083006ec67SBrian Somers * The given fsm is now up 30949052c95SBrian Somers * If it's an LCP set our mtu (if we're multilink, add up the link 31004eaa58cSBrian Somers * speeds and set the MRRU) and start our autoload timer. 311565e35e5SBrian Somers * If it's an NCP, tell our -background parent to go away. 3123b0f8d2eSBrian Somers * If it's the first NCP, start the idle timer. 3133006ec67SBrian Somers */ 3146f384573SBrian Somers struct bundle *bundle = (struct bundle *)v; 3156d666775SBrian Somers 3165563ebdeSBrian Somers if (fp->proto == PROTO_LCP) { 3173b0f8d2eSBrian Somers if (bundle->ncp.mp.active) { 3183b0f8d2eSBrian Somers struct datalink *dl; 3195563ebdeSBrian Somers 320faefde08SBrian Somers bundle->ifp.Speed = 0; 321faefde08SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 322eeab6bf5SBrian Somers if (dl->state == DATALINK_OPEN) 323faefde08SBrian Somers bundle->ifp.Speed += modem_Speed(dl->physical); 324faefde08SBrian Somers tun_configure(bundle, bundle->ncp.mp.peer_mrru); 32504eaa58cSBrian Somers bundle->autoload.running = 1; 326faefde08SBrian Somers } else { 327faefde08SBrian Somers bundle->ifp.Speed = modem_Speed(link2physical(fp->link)); 328faefde08SBrian Somers tun_configure(bundle, fsm2lcp(fp)->his_mru); 329faefde08SBrian Somers } 3303b0f8d2eSBrian Somers } else if (fp->proto == PROTO_IPCP) { 331ab886ad0SBrian Somers bundle_StartIdleTimer(bundle); 3325cf4388bSBrian Somers bundle_Notify(bundle, EX_NORMAL); 3337a6f8720SBrian Somers } 334ab886ad0SBrian Somers } 3357a6f8720SBrian Somers 3366d666775SBrian Somers static void 3376d666775SBrian Somers bundle_LayerDown(void *v, struct fsm *fp) 3386d666775SBrian Somers { 3396d666775SBrian Somers /* 3406d666775SBrian Somers * The given FSM has been told to come down. 341ab886ad0SBrian Somers * If it's our last NCP, stop the idle timer. 34286b1f0d7SBrian Somers * If it's an LCP and we're in multilink mode, adjust our tun 34386b1f0d7SBrian Somers * speed and make sure our minimum sequence number is adjusted. 3446d666775SBrian Somers */ 345ab886ad0SBrian Somers 346ab886ad0SBrian Somers struct bundle *bundle = (struct bundle *)v; 347ab886ad0SBrian Somers 348078c562eSBrian Somers if (fp->proto == PROTO_IPCP) 349ab886ad0SBrian Somers bundle_StopIdleTimer(bundle); 350078c562eSBrian Somers else if (fp->proto == PROTO_LCP && bundle->ncp.mp.active) { 3513b0f8d2eSBrian Somers struct datalink *dl; 35286b1f0d7SBrian Somers struct datalink *lost; 3533b0f8d2eSBrian Somers 354faefde08SBrian Somers bundle->ifp.Speed = 0; 35586b1f0d7SBrian Somers lost = NULL; 356faefde08SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 35786b1f0d7SBrian Somers if (fp == &dl->physical->link.lcp.fsm) 35886b1f0d7SBrian Somers lost = dl; 35986b1f0d7SBrian Somers else if (dl->state == DATALINK_OPEN) 360faefde08SBrian Somers bundle->ifp.Speed += modem_Speed(dl->physical); 36186b1f0d7SBrian Somers 362faefde08SBrian Somers if (bundle->ifp.Speed) 363faefde08SBrian Somers /* Don't configure down to a speed of 0 */ 364faefde08SBrian Somers tun_configure(bundle, bundle->ncp.mp.link.lcp.his_mru); 36586b1f0d7SBrian Somers 36686b1f0d7SBrian Somers if (lost) 36786b1f0d7SBrian Somers mp_LinkLost(&bundle->ncp.mp, lost); 36886b1f0d7SBrian Somers else 36986b1f0d7SBrian Somers log_Printf(LogERROR, "Oops, lost an unrecognised datalink (%s) !\n", 37086b1f0d7SBrian Somers fp->link->name); 3713b0f8d2eSBrian Somers } 3726d666775SBrian Somers } 3736d666775SBrian Somers 3746d666775SBrian Somers static void 3756d666775SBrian Somers bundle_LayerFinish(void *v, struct fsm *fp) 3766d666775SBrian Somers { 3776d666775SBrian Somers /* The given fsm is now down (fp cannot be NULL) 3786d666775SBrian Somers * 379dd7e2610SBrian Somers * If it's the last LCP, fsm_Down all NCPs 380dd7e2610SBrian Somers * If it's the last NCP, fsm_Close all LCPs 3816d666775SBrian Somers */ 3826d666775SBrian Somers 3836d666775SBrian Somers struct bundle *bundle = (struct bundle *)v; 3846d666775SBrian Somers struct datalink *dl; 3856d666775SBrian Somers 3863b0f8d2eSBrian Somers if (fp->proto == PROTO_IPCP) { 38726afeaa2SBrian Somers if (bundle_Phase(bundle) != PHASE_DEAD) 38825092092SBrian Somers bundle_NewPhase(bundle, PHASE_TERMINATE); 3896d666775SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 3903b0f8d2eSBrian Somers datalink_Close(dl, 0); 391dd7e2610SBrian Somers fsm_Down(fp); 392dd7e2610SBrian Somers fsm_Close(fp); 3933b0f8d2eSBrian Somers } else if (fp->proto == PROTO_LCP) { 3943b0f8d2eSBrian Somers int others_active; 395a611cad6SBrian Somers 3963b0f8d2eSBrian Somers others_active = 0; 3973b0f8d2eSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 3983b0f8d2eSBrian Somers if (fp != &dl->physical->link.lcp.fsm && 3993b0f8d2eSBrian Somers dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) 4003b0f8d2eSBrian Somers others_active++; 4013b0f8d2eSBrian Somers 4023b0f8d2eSBrian Somers if (!others_active) { 403dd7e2610SBrian Somers fsm_Down(&bundle->ncp.ipcp.fsm); 404dd7e2610SBrian Somers fsm_Close(&bundle->ncp.ipcp.fsm); /* ST_INITIAL please */ 4056d666775SBrian Somers } 4063b0f8d2eSBrian Somers } 4073b0f8d2eSBrian Somers } 4086d666775SBrian Somers 4097a6f8720SBrian Somers int 4107a6f8720SBrian Somers bundle_LinkIsUp(const struct bundle *bundle) 4117a6f8720SBrian Somers { 4125828db6dSBrian Somers return bundle->ncp.ipcp.fsm.state == ST_OPENED; 4137a6f8720SBrian Somers } 4147a6f8720SBrian Somers 4157a6f8720SBrian Somers void 4163006ec67SBrian Somers bundle_Close(struct bundle *bundle, const char *name, int staydown) 4177a6f8720SBrian Somers { 418455aabc3SBrian Somers /* 4193006ec67SBrian Somers * Please close the given datalink. 420dd7e2610SBrian Somers * If name == NULL or name is the last datalink, fsm_Close all NCPs 4216f384573SBrian Somers * (except our MP) 4223b0f8d2eSBrian Somers * If it isn't the last datalink, just Close that datalink. 423455aabc3SBrian Somers */ 4247a6f8720SBrian Somers 4253b0f8d2eSBrian Somers struct datalink *dl, *this_dl; 4263b0f8d2eSBrian Somers int others_active; 4273006ec67SBrian Somers 4283b0f8d2eSBrian Somers if (bundle->phase == PHASE_TERMINATE || bundle->phase == PHASE_DEAD) 4293b0f8d2eSBrian Somers return; 4303b0f8d2eSBrian Somers 4313b0f8d2eSBrian Somers others_active = 0; 4323b0f8d2eSBrian Somers this_dl = NULL; 4333b0f8d2eSBrian Somers 4343b0f8d2eSBrian Somers for (dl = bundle->links; dl; dl = dl->next) { 4353b0f8d2eSBrian Somers if (name && !strcasecmp(name, dl->name)) 4363b0f8d2eSBrian Somers this_dl = dl; 4373b0f8d2eSBrian Somers if (name == NULL || this_dl == dl) { 438d345321bSBrian Somers if (staydown) 4393006ec67SBrian Somers datalink_StayDown(dl); 4403b0f8d2eSBrian Somers } else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) 4413b0f8d2eSBrian Somers others_active++; 4423b0f8d2eSBrian Somers } 4433b0f8d2eSBrian Somers 4443b0f8d2eSBrian Somers if (name && this_dl == NULL) { 445dd7e2610SBrian Somers log_Printf(LogWARN, "%s: Invalid datalink name\n", name); 4463b0f8d2eSBrian Somers return; 4473b0f8d2eSBrian Somers } 4483b0f8d2eSBrian Somers 4493b0f8d2eSBrian Somers if (!others_active) { 45004eaa58cSBrian Somers bundle_StopIdleTimer(bundle); 45104eaa58cSBrian Somers bundle_StopAutoLoadTimer(bundle); 4523b0f8d2eSBrian Somers if (bundle->ncp.ipcp.fsm.state > ST_CLOSED || 4533b0f8d2eSBrian Somers bundle->ncp.ipcp.fsm.state == ST_STARTING) 454dd7e2610SBrian Somers fsm_Close(&bundle->ncp.ipcp.fsm); 4553b0f8d2eSBrian Somers else { 4565828db6dSBrian Somers if (bundle->ncp.ipcp.fsm.state > ST_INITIAL) { 457dd7e2610SBrian Somers fsm_Close(&bundle->ncp.ipcp.fsm); 458dd7e2610SBrian Somers fsm_Down(&bundle->ncp.ipcp.fsm); 459d2fd8d77SBrian Somers } 460d345321bSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 461d345321bSBrian Somers datalink_Close(dl, staydown); 4627a6f8720SBrian Somers } 4633b0f8d2eSBrian Somers } else if (this_dl && this_dl->state != DATALINK_CLOSED && 4643b0f8d2eSBrian Somers this_dl->state != DATALINK_HANGUP) 4653b0f8d2eSBrian Somers datalink_Close(this_dl, staydown); 466d2fd8d77SBrian Somers } 4677a6f8720SBrian Somers 4681bc9b5baSBrian Somers void 4691bc9b5baSBrian Somers bundle_Down(struct bundle *bundle) 4701bc9b5baSBrian Somers { 4711bc9b5baSBrian Somers struct datalink *dl; 4721bc9b5baSBrian Somers 4731bc9b5baSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 4741bc9b5baSBrian Somers datalink_Down(dl, 1); 4751bc9b5baSBrian Somers } 4761bc9b5baSBrian Somers 4772f786681SBrian Somers static int 4782f786681SBrian Somers bundle_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n) 4792f786681SBrian Somers { 4802f786681SBrian Somers struct bundle *bundle = descriptor2bundle(d); 4812f786681SBrian Somers struct datalink *dl; 48204eaa58cSBrian Somers int result, want, queued, nlinks; 4832f786681SBrian Somers 4842f786681SBrian Somers result = 0; 4852f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 4862f786681SBrian Somers result += descriptor_UpdateSet(&dl->desc, r, w, e, n); 4872f786681SBrian Somers 488078c562eSBrian Somers /* If there are aren't many packets queued, look for some more. */ 48904eaa58cSBrian Somers for (nlinks = 0, dl = bundle->links; dl; dl = dl->next) 49004eaa58cSBrian Somers nlinks++; 49104eaa58cSBrian Somers 49204eaa58cSBrian Somers if (nlinks) { 49304eaa58cSBrian Somers queued = r ? bundle_FillQueues(bundle) : ip_QueueLen(); 49404eaa58cSBrian Somers if (bundle->autoload.running) { 49504eaa58cSBrian Somers if (queued < bundle->cfg.autoload.max.packets) { 49604eaa58cSBrian Somers if (queued > bundle->cfg.autoload.min.packets) 49704eaa58cSBrian Somers bundle_StopAutoLoadTimer(bundle); 49804eaa58cSBrian Somers else if (bundle->autoload.timer.state != TIMER_RUNNING || 49904eaa58cSBrian Somers bundle->autoload.comingup) 50004eaa58cSBrian Somers bundle_StartAutoLoadTimer(bundle, 0); 50104eaa58cSBrian Somers } else if (bundle->autoload.timer.state != TIMER_RUNNING || 50204eaa58cSBrian Somers !bundle->autoload.comingup) 50304eaa58cSBrian Somers bundle_StartAutoLoadTimer(bundle, 1); 50404eaa58cSBrian Somers } 50504eaa58cSBrian Somers 5060f2f3eb3SBrian Somers if (r && 5070f2f3eb3SBrian Somers (bundle->phase == PHASE_NETWORK || bundle->phys_type & PHYS_DEMAND)) { 50804eaa58cSBrian Somers /* enough surplus so that we can tell if we're getting swamped */ 50904eaa58cSBrian Somers want = bundle->cfg.autoload.max.packets + nlinks * 2; 51004eaa58cSBrian Somers /* but at least 20 packets ! */ 51104eaa58cSBrian Somers if (want < 20) 51204eaa58cSBrian Somers want = 20; 51304eaa58cSBrian Somers if (queued < want) { 51404eaa58cSBrian Somers /* Not enough - select() for more */ 51504eaa58cSBrian Somers FD_SET(bundle->dev.fd, r); 516faefde08SBrian Somers if (*n < bundle->dev.fd + 1) 517faefde08SBrian Somers *n = bundle->dev.fd + 1; 5181384bd27SBrian Somers log_Printf(LogTIMER, "%s: fdset(r) %d\n", TUN_NAME, bundle->dev.fd); 519078c562eSBrian Somers result++; 520078c562eSBrian Somers } 52104eaa58cSBrian Somers } 52204eaa58cSBrian Somers } 523078c562eSBrian Somers 524ea722969SBrian Somers /* 525ea722969SBrian Somers * This *MUST* be called after the datalink UpdateSet()s as it 52604eaa58cSBrian Somers * might be ``holding'' one of the datalinks (death-row) and 52704eaa58cSBrian Somers * wants to be able to de-select() it from the descriptor set. 528ea722969SBrian Somers */ 5290f2f3eb3SBrian Somers result += descriptor_UpdateSet(&bundle->ncp.mp.server.desc, r, w, e, n); 530ea722969SBrian Somers 5312f786681SBrian Somers return result; 5322f786681SBrian Somers } 5332f786681SBrian Somers 5342f786681SBrian Somers static int 5352f786681SBrian Somers bundle_IsSet(struct descriptor *d, const fd_set *fdset) 5362f786681SBrian Somers { 5372f786681SBrian Somers struct bundle *bundle = descriptor2bundle(d); 5382f786681SBrian Somers struct datalink *dl; 5392f786681SBrian Somers 5402f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 5412f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 5422f786681SBrian Somers return 1; 5432f786681SBrian Somers 544332b9de0SBrian Somers if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 545332b9de0SBrian Somers return 1; 546332b9de0SBrian Somers 547faefde08SBrian Somers return FD_ISSET(bundle->dev.fd, fdset); 5482f786681SBrian Somers } 5492f786681SBrian Somers 5502f786681SBrian Somers static void 5512f786681SBrian Somers bundle_DescriptorRead(struct descriptor *d, struct bundle *bundle, 5522f786681SBrian Somers const fd_set *fdset) 5532f786681SBrian Somers { 5542f786681SBrian Somers struct datalink *dl; 5552f786681SBrian Somers 556ea722969SBrian Somers if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 557ea722969SBrian Somers descriptor_Read(&bundle->ncp.mp.server.desc, bundle, fdset); 558ea722969SBrian Somers 5592f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 5602f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 5612f786681SBrian Somers descriptor_Read(&dl->desc, bundle, fdset); 562b6217683SBrian Somers 563faefde08SBrian Somers if (FD_ISSET(bundle->dev.fd, fdset)) { 564078c562eSBrian Somers struct tun_data tun; 565078c562eSBrian Somers int n, pri; 566078c562eSBrian Somers 567078c562eSBrian Somers /* something to read from tun */ 568faefde08SBrian Somers n = read(bundle->dev.fd, &tun, sizeof tun); 569078c562eSBrian Somers if (n < 0) { 5701384bd27SBrian Somers log_Printf(LogERROR, "read from %s: %s\n", TUN_NAME, strerror(errno)); 571078c562eSBrian Somers return; 572078c562eSBrian Somers } 573078c562eSBrian Somers n -= sizeof tun - sizeof tun.data; 574078c562eSBrian Somers if (n <= 0) { 5751384bd27SBrian Somers log_Printf(LogERROR, "read from %s: Only %d bytes read\n", TUN_NAME, n); 576078c562eSBrian Somers return; 577078c562eSBrian Somers } 578078c562eSBrian Somers if (!tun_check_header(tun, AF_INET)) 579078c562eSBrian Somers return; 580078c562eSBrian Somers 581078c562eSBrian Somers if (((struct ip *)tun.data)->ip_dst.s_addr == 582078c562eSBrian Somers bundle->ncp.ipcp.my_ip.s_addr) { 583078c562eSBrian Somers /* we've been asked to send something addressed *to* us :( */ 584078c562eSBrian Somers if (Enabled(bundle, OPT_LOOPBACK)) { 585078c562eSBrian Somers pri = PacketCheck(bundle, tun.data, n, &bundle->filter.in); 586078c562eSBrian Somers if (pri >= 0) { 587078c562eSBrian Somers struct mbuf *bp; 588078c562eSBrian Somers 589078c562eSBrian Somers #ifndef NOALIAS 590078c562eSBrian Somers if (alias_IsEnabled()) { 591078c562eSBrian Somers (*PacketAlias.In)(tun.data, sizeof tun.data); 592078c562eSBrian Somers n = ntohs(((struct ip *)tun.data)->ip_len); 593078c562eSBrian Somers } 594078c562eSBrian Somers #endif 595078c562eSBrian Somers bp = mbuf_Alloc(n, MB_IPIN); 596078c562eSBrian Somers memcpy(MBUF_CTOP(bp), tun.data, n); 597078c562eSBrian Somers ip_Input(bundle, bp); 598078c562eSBrian Somers log_Printf(LogDEBUG, "Looped back packet addressed to myself\n"); 599078c562eSBrian Somers } 600078c562eSBrian Somers return; 601078c562eSBrian Somers } else 602078c562eSBrian Somers log_Printf(LogDEBUG, "Oops - forwarding packet addressed to myself\n"); 603078c562eSBrian Somers } 604078c562eSBrian Somers 605078c562eSBrian Somers /* 606078c562eSBrian Somers * Process on-demand dialup. Output packets are queued within tunnel 607078c562eSBrian Somers * device until IPCP is opened. 608078c562eSBrian Somers */ 609078c562eSBrian Somers 610078c562eSBrian Somers if (bundle_Phase(bundle) == PHASE_DEAD) { 611078c562eSBrian Somers /* 612078c562eSBrian Somers * Note, we must be in AUTO mode :-/ otherwise our interface should 613078c562eSBrian Somers * *not* be UP and we can't receive data 614078c562eSBrian Somers */ 615078c562eSBrian Somers if ((pri = PacketCheck(bundle, tun.data, n, &bundle->filter.dial)) >= 0) 616078c562eSBrian Somers bundle_Open(bundle, NULL, PHYS_DEMAND); 617078c562eSBrian Somers else 618078c562eSBrian Somers /* 619078c562eSBrian Somers * Drop the packet. If we were to queue it, we'd just end up with 620078c562eSBrian Somers * a pile of timed-out data in our output queue by the time we get 621078c562eSBrian Somers * around to actually dialing. We'd also prematurely reach the 622078c562eSBrian Somers * threshold at which we stop select()ing to read() the tun 623078c562eSBrian Somers * device - breaking auto-dial. 624078c562eSBrian Somers */ 625078c562eSBrian Somers return; 626078c562eSBrian Somers } 627078c562eSBrian Somers 628078c562eSBrian Somers pri = PacketCheck(bundle, tun.data, n, &bundle->filter.out); 629078c562eSBrian Somers if (pri >= 0) { 630078c562eSBrian Somers #ifndef NOALIAS 631078c562eSBrian Somers if (alias_IsEnabled()) { 632078c562eSBrian Somers (*PacketAlias.Out)(tun.data, sizeof tun.data); 633078c562eSBrian Somers n = ntohs(((struct ip *)tun.data)->ip_len); 634078c562eSBrian Somers } 635078c562eSBrian Somers #endif 636078c562eSBrian Somers ip_Enqueue(pri, tun.data, n); 637078c562eSBrian Somers } 638078c562eSBrian Somers } 6392f786681SBrian Somers } 6402f786681SBrian Somers 6412f786681SBrian Somers static void 6422f786681SBrian Somers bundle_DescriptorWrite(struct descriptor *d, struct bundle *bundle, 6432f786681SBrian Somers const fd_set *fdset) 6442f786681SBrian Somers { 6452f786681SBrian Somers struct datalink *dl; 6462f786681SBrian Somers 647ea722969SBrian Somers /* This is not actually necessary as struct mpserver doesn't Write() */ 648ea722969SBrian Somers if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 649ea722969SBrian Somers descriptor_Write(&bundle->ncp.mp.server.desc, bundle, fdset); 650ea722969SBrian Somers 6512f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 6522f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 6532f786681SBrian Somers descriptor_Write(&dl->desc, bundle, fdset); 6542f786681SBrian Somers } 6552f786681SBrian Somers 6561384bd27SBrian Somers static void 6571384bd27SBrian Somers bundle_LockTun(struct bundle *bundle) 6581384bd27SBrian Somers { 6591384bd27SBrian Somers FILE *lockfile; 6601384bd27SBrian Somers char pidfile[MAXPATHLEN]; 6611384bd27SBrian Somers 6621384bd27SBrian Somers snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit); 6631384bd27SBrian Somers lockfile = ID0fopen(pidfile, "w"); 6641384bd27SBrian Somers if (lockfile != NULL) { 6651384bd27SBrian Somers fprintf(lockfile, "%d\n", (int)getpid()); 6661384bd27SBrian Somers fclose(lockfile); 6671384bd27SBrian Somers } 6681384bd27SBrian Somers #ifndef RELEASE_CRUNCH 6691384bd27SBrian Somers else 6701384bd27SBrian Somers log_Printf(LogERROR, "Warning: Can't create %s: %s\n", 6711384bd27SBrian Somers pidfile, strerror(errno)); 6721384bd27SBrian Somers #endif 6731384bd27SBrian Somers } 6741384bd27SBrian Somers 6751384bd27SBrian Somers static void 6761384bd27SBrian Somers bundle_UnlockTun(struct bundle *bundle) 6771384bd27SBrian Somers { 6781384bd27SBrian Somers char pidfile[MAXPATHLEN]; 6791384bd27SBrian Somers 6801384bd27SBrian Somers snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit); 6811384bd27SBrian Somers ID0unlink(pidfile); 6821384bd27SBrian Somers } 6837a6f8720SBrian Somers 6847a6f8720SBrian Somers struct bundle * 6850f2f3eb3SBrian Somers bundle_Create(const char *prefix, int type) 6867a6f8720SBrian Somers { 6877a6f8720SBrian Somers int s, enoentcount, err; 6887a6f8720SBrian Somers struct ifreq ifrq; 6897a6f8720SBrian Somers static struct bundle bundle; /* there can be only one */ 6907a6f8720SBrian Somers 691faefde08SBrian Somers if (bundle.ifp.Name != NULL) { /* Already allocated ! */ 692dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_Create: There's only one BUNDLE !\n"); 6937a6f8720SBrian Somers return NULL; 6947a6f8720SBrian Somers } 6957a6f8720SBrian Somers 6967a6f8720SBrian Somers err = ENOENT; 6977a6f8720SBrian Somers enoentcount = 0; 698107d62e7SBrian Somers for (bundle.unit = 0; ; bundle.unit++) { 699faefde08SBrian Somers snprintf(bundle.dev.Name, sizeof bundle.dev.Name, "%s%d", 700faefde08SBrian Somers prefix, bundle.unit); 701faefde08SBrian Somers bundle.dev.fd = ID0open(bundle.dev.Name, O_RDWR); 702faefde08SBrian Somers if (bundle.dev.fd >= 0) 7037a6f8720SBrian Somers break; 704107d62e7SBrian Somers else if (errno == ENXIO) { 7057a6f8720SBrian Somers err = errno; 706107d62e7SBrian Somers break; 7077a6f8720SBrian Somers } else if (errno == ENOENT) { 7087a6f8720SBrian Somers if (++enoentcount > 2) 709107d62e7SBrian Somers break; 7107a6f8720SBrian Somers } else 7117a6f8720SBrian Somers err = errno; 7127a6f8720SBrian Somers } 7137a6f8720SBrian Somers 714faefde08SBrian Somers if (bundle.dev.fd < 0) { 715dd7e2610SBrian Somers log_Printf(LogWARN, "No available tunnel devices found (%s).\n", 71685b542cfSBrian Somers strerror(err)); 7177a6f8720SBrian Somers return NULL; 7187a6f8720SBrian Somers } 7197a6f8720SBrian Somers 720dd7e2610SBrian Somers log_SetTun(bundle.unit); 7217a6f8720SBrian Somers 7227a6f8720SBrian Somers s = socket(AF_INET, SOCK_DGRAM, 0); 7237a6f8720SBrian Somers if (s < 0) { 724dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_Create: socket(): %s\n", strerror(errno)); 725faefde08SBrian Somers close(bundle.dev.fd); 7267a6f8720SBrian Somers return NULL; 7277a6f8720SBrian Somers } 7287a6f8720SBrian Somers 729faefde08SBrian Somers bundle.ifp.Name = strrchr(bundle.dev.Name, '/'); 730faefde08SBrian Somers if (bundle.ifp.Name == NULL) 731faefde08SBrian Somers bundle.ifp.Name = bundle.dev.Name; 7327a6f8720SBrian Somers else 733faefde08SBrian Somers bundle.ifp.Name++; 7347a6f8720SBrian Somers 7357a6f8720SBrian Somers /* 7367a6f8720SBrian Somers * Now, bring up the interface. 7377a6f8720SBrian Somers */ 7387a6f8720SBrian Somers memset(&ifrq, '\0', sizeof ifrq); 739faefde08SBrian Somers strncpy(ifrq.ifr_name, bundle.ifp.Name, sizeof ifrq.ifr_name - 1); 7407a6f8720SBrian Somers ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0'; 7417a6f8720SBrian Somers if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) { 742dd7e2610SBrian Somers log_Printf(LogERROR, "OpenTunnel: ioctl(SIOCGIFFLAGS): %s\n", 7437a6f8720SBrian Somers strerror(errno)); 7447a6f8720SBrian Somers close(s); 745faefde08SBrian Somers close(bundle.dev.fd); 746faefde08SBrian Somers bundle.ifp.Name = NULL; 7477a6f8720SBrian Somers return NULL; 7487a6f8720SBrian Somers } 7497a6f8720SBrian Somers ifrq.ifr_flags |= IFF_UP; 7507a6f8720SBrian Somers if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) { 751dd7e2610SBrian Somers log_Printf(LogERROR, "OpenTunnel: ioctl(SIOCSIFFLAGS): %s\n", 7527a6f8720SBrian Somers strerror(errno)); 7537a6f8720SBrian Somers close(s); 754faefde08SBrian Somers close(bundle.dev.fd); 755faefde08SBrian Somers bundle.ifp.Name = NULL; 7567a6f8720SBrian Somers return NULL; 7577a6f8720SBrian Somers } 7587a6f8720SBrian Somers 7597a6f8720SBrian Somers close(s); 7607a6f8720SBrian Somers 761faefde08SBrian Somers if ((bundle.ifp.Index = GetIfIndex(bundle.ifp.Name)) < 0) { 762faefde08SBrian Somers log_Printf(LogERROR, "OpenTunnel: Can't find interface index.\n"); 763faefde08SBrian Somers close(bundle.dev.fd); 764faefde08SBrian Somers bundle.ifp.Name = NULL; 7657a6f8720SBrian Somers return NULL; 7667a6f8720SBrian Somers } 767faefde08SBrian Somers log_Printf(LogPHASE, "Using interface: %s\n", bundle.ifp.Name); 7687a6f8720SBrian Somers 769faefde08SBrian Somers bundle.ifp.Speed = 0; 7707a6f8720SBrian Somers 771820de6ebSBrian Somers bundle.routing_seq = 0; 772a0cbd833SBrian Somers bundle.phase = PHASE_DEAD; 773a0cbd833SBrian Somers bundle.CleaningUp = 0; 7747a6f8720SBrian Somers 7756d666775SBrian Somers bundle.fsm.LayerStart = bundle_LayerStart; 7766f384573SBrian Somers bundle.fsm.LayerUp = bundle_LayerUp; 7776d666775SBrian Somers bundle.fsm.LayerDown = bundle_LayerDown; 7786d666775SBrian Somers bundle.fsm.LayerFinish = bundle_LayerFinish; 7796d666775SBrian Somers bundle.fsm.object = &bundle; 7807a6f8720SBrian Somers 781ab886ad0SBrian Somers bundle.cfg.idle_timeout = NCP_IDLE_TIMEOUT; 7821342caedSBrian Somers *bundle.cfg.auth.name = '\0'; 7831342caedSBrian Somers *bundle.cfg.auth.key = '\0'; 784610b185fSBrian Somers bundle.cfg.opt = OPT_SROUTES | OPT_IDCHECK | OPT_LOOPBACK | 785610b185fSBrian Somers OPT_THROUGHPUT | OPT_UTMP; 78649052c95SBrian Somers *bundle.cfg.label = '\0'; 78749052c95SBrian Somers bundle.cfg.mtu = DEF_MTU; 78804eaa58cSBrian Somers bundle.cfg.autoload.max.packets = 0; 78904eaa58cSBrian Somers bundle.cfg.autoload.max.timeout = 0; 79004eaa58cSBrian Somers bundle.cfg.autoload.min.packets = 0; 79104eaa58cSBrian Somers bundle.cfg.autoload.min.timeout = 0; 792565e35e5SBrian Somers bundle.phys_type = type; 793ab886ad0SBrian Somers 7946f384573SBrian Somers bundle.links = datalink_Create("deflink", &bundle, type); 7953006ec67SBrian Somers if (bundle.links == NULL) { 796dd7e2610SBrian Somers log_Printf(LogERROR, "Cannot create data link: %s\n", strerror(errno)); 797faefde08SBrian Somers close(bundle.dev.fd); 798faefde08SBrian Somers bundle.ifp.Name = NULL; 7992289f246SBrian Somers return NULL; 8002289f246SBrian Somers } 8012289f246SBrian Somers 8022f786681SBrian Somers bundle.desc.type = BUNDLE_DESCRIPTOR; 8032f786681SBrian Somers bundle.desc.UpdateSet = bundle_UpdateSet; 8042f786681SBrian Somers bundle.desc.IsSet = bundle_IsSet; 8052f786681SBrian Somers bundle.desc.Read = bundle_DescriptorRead; 8062f786681SBrian Somers bundle.desc.Write = bundle_DescriptorWrite; 8072f786681SBrian Somers 80849052c95SBrian Somers mp_Init(&bundle.ncp.mp, &bundle); 80949052c95SBrian Somers 81049052c95SBrian Somers /* Send over the first physical link by default */ 8115828db6dSBrian Somers ipcp_Init(&bundle.ncp.ipcp, &bundle, &bundle.links->physical->link, 8125828db6dSBrian Somers &bundle.fsm); 8136d666775SBrian Somers 8145ca5389aSBrian Somers memset(&bundle.filter, '\0', sizeof bundle.filter); 8155ca5389aSBrian Somers bundle.filter.in.fragok = bundle.filter.in.logok = 1; 8165ca5389aSBrian Somers bundle.filter.in.name = "IN"; 8175ca5389aSBrian Somers bundle.filter.out.fragok = bundle.filter.out.logok = 1; 8185ca5389aSBrian Somers bundle.filter.out.name = "OUT"; 8195ca5389aSBrian Somers bundle.filter.dial.name = "DIAL"; 8208390b576SBrian Somers bundle.filter.dial.logok = 1; 8215ca5389aSBrian Somers bundle.filter.alive.name = "ALIVE"; 8225ca5389aSBrian Somers bundle.filter.alive.logok = 1; 82393ee0ff2SBrian Somers memset(&bundle.idle.timer, '\0', sizeof bundle.idle.timer); 82493ee0ff2SBrian Somers bundle.idle.done = 0; 8255cf4388bSBrian Somers bundle.notify.fd = -1; 82604eaa58cSBrian Somers memset(&bundle.autoload.timer, '\0', sizeof bundle.autoload.timer); 82704eaa58cSBrian Somers bundle.autoload.done = 0; 82804eaa58cSBrian Somers bundle.autoload.running = 0; 82993ee0ff2SBrian Somers 8306d666775SBrian Somers /* Clean out any leftover crud */ 8316d666775SBrian Somers bundle_CleanInterface(&bundle); 8326d666775SBrian Somers 8331384bd27SBrian Somers bundle_LockTun(&bundle); 8341384bd27SBrian Somers 8357a6f8720SBrian Somers return &bundle; 8367a6f8720SBrian Somers } 8377a6f8720SBrian Somers 83868a0f0ccSBrian Somers static void 83968a0f0ccSBrian Somers bundle_DownInterface(struct bundle *bundle) 84068a0f0ccSBrian Somers { 84168a0f0ccSBrian Somers struct ifreq ifrq; 84268a0f0ccSBrian Somers int s; 84368a0f0ccSBrian Somers 844dd7e2610SBrian Somers route_IfDelete(bundle, 1); 84568a0f0ccSBrian Somers 84668a0f0ccSBrian Somers s = ID0socket(AF_INET, SOCK_DGRAM, 0); 84768a0f0ccSBrian Somers if (s < 0) { 848dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_DownInterface: socket: %s\n", strerror(errno)); 84968a0f0ccSBrian Somers return; 85068a0f0ccSBrian Somers } 85168a0f0ccSBrian Somers 85268a0f0ccSBrian Somers memset(&ifrq, '\0', sizeof ifrq); 853faefde08SBrian Somers strncpy(ifrq.ifr_name, bundle->ifp.Name, sizeof ifrq.ifr_name - 1); 85468a0f0ccSBrian Somers ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0'; 85568a0f0ccSBrian Somers if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) { 856dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_DownInterface: ioctl(SIOCGIFFLAGS): %s\n", 85768a0f0ccSBrian Somers strerror(errno)); 85868a0f0ccSBrian Somers close(s); 85968a0f0ccSBrian Somers return; 86068a0f0ccSBrian Somers } 86168a0f0ccSBrian Somers ifrq.ifr_flags &= ~IFF_UP; 86268a0f0ccSBrian Somers if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) { 863dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_DownInterface: ioctl(SIOCSIFFLAGS): %s\n", 86468a0f0ccSBrian Somers strerror(errno)); 86568a0f0ccSBrian Somers close(s); 86668a0f0ccSBrian Somers return; 86768a0f0ccSBrian Somers } 86868a0f0ccSBrian Somers close(s); 86968a0f0ccSBrian Somers } 87068a0f0ccSBrian Somers 87168a0f0ccSBrian Somers void 87268a0f0ccSBrian Somers bundle_Destroy(struct bundle *bundle) 87368a0f0ccSBrian Somers { 8743006ec67SBrian Somers struct datalink *dl; 875b6217683SBrian Somers 876ea722969SBrian Somers /* 87704eaa58cSBrian Somers * Clean up the interface. We don't need to timer_Stop()s, mp_Down(), 878ea722969SBrian Somers * ipcp_CleanInterface() and bundle_DownInterface() unless we're getting 879ea722969SBrian Somers * out under exceptional conditions such as a descriptor exception. 880ea722969SBrian Somers */ 88104eaa58cSBrian Somers timer_Stop(&bundle->idle.timer); 88204eaa58cSBrian Somers timer_Stop(&bundle->autoload.timer); 88366f634b6SBrian Somers mp_Down(&bundle->ncp.mp); 884dd7e2610SBrian Somers ipcp_CleanInterface(&bundle->ncp.ipcp); 88568a0f0ccSBrian Somers bundle_DownInterface(bundle); 8863006ec67SBrian Somers 887ea722969SBrian Somers /* Again, these are all DATALINK_CLOSED unless we're abending */ 8883006ec67SBrian Somers dl = bundle->links; 8893006ec67SBrian Somers while (dl) 8903006ec67SBrian Somers dl = datalink_Destroy(dl); 8913006ec67SBrian Somers 8921384bd27SBrian Somers close(bundle->dev.fd); 8931384bd27SBrian Somers bundle_UnlockTun(bundle); 8941384bd27SBrian Somers 895ea722969SBrian Somers /* In case we never made PHASE_NETWORK */ 8965cf4388bSBrian Somers bundle_Notify(bundle, EX_ERRDEAD); 897b6217683SBrian Somers 898faefde08SBrian Somers bundle->ifp.Name = NULL; 89968a0f0ccSBrian Somers } 90068a0f0ccSBrian Somers 9017a6f8720SBrian Somers struct rtmsg { 9027a6f8720SBrian Somers struct rt_msghdr m_rtm; 9037a6f8720SBrian Somers char m_space[64]; 9047a6f8720SBrian Somers }; 9057a6f8720SBrian Somers 906610b185fSBrian Somers int 907820de6ebSBrian Somers bundle_SetRoute(struct bundle *bundle, int cmd, struct in_addr dst, 9087a6f8720SBrian Somers struct in_addr gateway, struct in_addr mask, int bang) 9097a6f8720SBrian Somers { 9107a6f8720SBrian Somers struct rtmsg rtmes; 9117a6f8720SBrian Somers int s, nb, wb; 9127a6f8720SBrian Somers char *cp; 9137a6f8720SBrian Somers const char *cmdstr; 9147a6f8720SBrian Somers struct sockaddr_in rtdata; 915610b185fSBrian Somers int result = 1; 9167a6f8720SBrian Somers 9177a6f8720SBrian Somers if (bang) 9187a6f8720SBrian Somers cmdstr = (cmd == RTM_ADD ? "Add!" : "Delete!"); 9197a6f8720SBrian Somers else 9207a6f8720SBrian Somers cmdstr = (cmd == RTM_ADD ? "Add" : "Delete"); 9217a6f8720SBrian Somers s = ID0socket(PF_ROUTE, SOCK_RAW, 0); 9227a6f8720SBrian Somers if (s < 0) { 923dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_SetRoute: socket(): %s\n", strerror(errno)); 924610b185fSBrian Somers return result; 9257a6f8720SBrian Somers } 9267a6f8720SBrian Somers memset(&rtmes, '\0', sizeof rtmes); 9277a6f8720SBrian Somers rtmes.m_rtm.rtm_version = RTM_VERSION; 9287a6f8720SBrian Somers rtmes.m_rtm.rtm_type = cmd; 9297a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs = RTA_DST; 930820de6ebSBrian Somers rtmes.m_rtm.rtm_seq = ++bundle->routing_seq; 9317a6f8720SBrian Somers rtmes.m_rtm.rtm_pid = getpid(); 9327a6f8720SBrian Somers rtmes.m_rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC; 9337a6f8720SBrian Somers 9347a6f8720SBrian Somers memset(&rtdata, '\0', sizeof rtdata); 93550e5c17dSBrian Somers rtdata.sin_len = sizeof rtdata; 9367a6f8720SBrian Somers rtdata.sin_family = AF_INET; 9377a6f8720SBrian Somers rtdata.sin_port = 0; 9387a6f8720SBrian Somers rtdata.sin_addr = dst; 9397a6f8720SBrian Somers 9407a6f8720SBrian Somers cp = rtmes.m_space; 94150e5c17dSBrian Somers memcpy(cp, &rtdata, rtdata.sin_len); 94250e5c17dSBrian Somers cp += rtdata.sin_len; 943e43ebac1SBrian Somers if (cmd == RTM_ADD) { 9447a6f8720SBrian Somers if (gateway.s_addr == INADDR_ANY) { 9457a6f8720SBrian Somers /* Add a route through the interface */ 9467a6f8720SBrian Somers struct sockaddr_dl dl; 9477a6f8720SBrian Somers const char *iname; 9487a6f8720SBrian Somers int ilen; 9497a6f8720SBrian Somers 950faefde08SBrian Somers iname = Index2Nam(bundle->ifp.Index); 9517a6f8720SBrian Somers ilen = strlen(iname); 9527a6f8720SBrian Somers dl.sdl_len = sizeof dl - sizeof dl.sdl_data + ilen; 9537a6f8720SBrian Somers dl.sdl_family = AF_LINK; 954faefde08SBrian Somers dl.sdl_index = bundle->ifp.Index; 9557a6f8720SBrian Somers dl.sdl_type = 0; 9567a6f8720SBrian Somers dl.sdl_nlen = ilen; 9577a6f8720SBrian Somers dl.sdl_alen = 0; 9587a6f8720SBrian Somers dl.sdl_slen = 0; 9597a6f8720SBrian Somers strncpy(dl.sdl_data, iname, sizeof dl.sdl_data); 9607a6f8720SBrian Somers memcpy(cp, &dl, dl.sdl_len); 9617a6f8720SBrian Somers cp += dl.sdl_len; 9627a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY; 9637a6f8720SBrian Somers } else { 9647a6f8720SBrian Somers rtdata.sin_addr = gateway; 96550e5c17dSBrian Somers memcpy(cp, &rtdata, rtdata.sin_len); 96650e5c17dSBrian Somers cp += rtdata.sin_len; 9677a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY; 9687a6f8720SBrian Somers } 969e43ebac1SBrian Somers } 9707a6f8720SBrian Somers 9717a6f8720SBrian Somers if (dst.s_addr == INADDR_ANY) 9727a6f8720SBrian Somers mask.s_addr = INADDR_ANY; 9737a6f8720SBrian Somers 9747a6f8720SBrian Somers if (cmd == RTM_ADD || dst.s_addr == INADDR_ANY) { 9757a6f8720SBrian Somers rtdata.sin_addr = mask; 97650e5c17dSBrian Somers memcpy(cp, &rtdata, rtdata.sin_len); 97750e5c17dSBrian Somers cp += rtdata.sin_len; 9787a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs |= RTA_NETMASK; 9797a6f8720SBrian Somers } 9807a6f8720SBrian Somers 9817a6f8720SBrian Somers nb = cp - (char *) &rtmes; 9827a6f8720SBrian Somers rtmes.m_rtm.rtm_msglen = nb; 9837a6f8720SBrian Somers wb = ID0write(s, &rtmes, nb); 9847a6f8720SBrian Somers if (wb < 0) { 985dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute failure:\n"); 986dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Cmd = %s\n", cmdstr); 987dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Dst = %s\n", inet_ntoa(dst)); 988dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Gateway = %s\n", inet_ntoa(gateway)); 989dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Mask = %s\n", inet_ntoa(mask)); 9907a6f8720SBrian Somers failed: 9917a6f8720SBrian Somers if (cmd == RTM_ADD && (rtmes.m_rtm.rtm_errno == EEXIST || 992e43ebac1SBrian Somers (rtmes.m_rtm.rtm_errno == 0 && errno == EEXIST))) { 993610b185fSBrian Somers if (!bang) { 994dd7e2610SBrian Somers log_Printf(LogWARN, "Add route failed: %s already exists\n", 9957a6f8720SBrian Somers inet_ntoa(dst)); 996610b185fSBrian Somers result = 0; /* Don't add to our dynamic list */ 997610b185fSBrian Somers } else { 9987a6f8720SBrian Somers rtmes.m_rtm.rtm_type = cmd = RTM_CHANGE; 9997a6f8720SBrian Somers if ((wb = ID0write(s, &rtmes, nb)) < 0) 10007a6f8720SBrian Somers goto failed; 10017a6f8720SBrian Somers } 1002e43ebac1SBrian Somers } else if (cmd == RTM_DELETE && 10037a6f8720SBrian Somers (rtmes.m_rtm.rtm_errno == ESRCH || 10047a6f8720SBrian Somers (rtmes.m_rtm.rtm_errno == 0 && errno == ESRCH))) { 10057a6f8720SBrian Somers if (!bang) 1006dd7e2610SBrian Somers log_Printf(LogWARN, "Del route failed: %s: Non-existent\n", 10077a6f8720SBrian Somers inet_ntoa(dst)); 10087a6f8720SBrian Somers } else if (rtmes.m_rtm.rtm_errno == 0) 1009dd7e2610SBrian Somers log_Printf(LogWARN, "%s route failed: %s: errno: %s\n", cmdstr, 10107a6f8720SBrian Somers inet_ntoa(dst), strerror(errno)); 10117a6f8720SBrian Somers else 1012dd7e2610SBrian Somers log_Printf(LogWARN, "%s route failed: %s: %s\n", 10137a6f8720SBrian Somers cmdstr, inet_ntoa(dst), strerror(rtmes.m_rtm.rtm_errno)); 10147a6f8720SBrian Somers } 1015dd7e2610SBrian Somers log_Printf(LogDEBUG, "wrote %d: cmd = %s, dst = %x, gateway = %x\n", 1016fe3125a0SBrian Somers wb, cmdstr, (unsigned)dst.s_addr, (unsigned)gateway.s_addr); 10177a6f8720SBrian Somers close(s); 1018610b185fSBrian Somers 1019610b185fSBrian Somers return result; 10207a6f8720SBrian Somers } 102183d1af55SBrian Somers 102283d1af55SBrian Somers void 10233006ec67SBrian Somers bundle_LinkClosed(struct bundle *bundle, struct datalink *dl) 10243006ec67SBrian Somers { 10253006ec67SBrian Somers /* 10263006ec67SBrian Somers * Our datalink has closed. 1027ea722969SBrian Somers * CleanDatalinks() (called from DoLoop()) will remove closed 1028ea722969SBrian Somers * 1OFF and DIRECT links. 10293b0f8d2eSBrian Somers * If it's the last data link, enter phase DEAD. 1030ea722969SBrian Somers * 1031ea722969SBrian Somers * NOTE: dl may not be in our list (bundle_SendDatalink()) ! 10323006ec67SBrian Somers */ 10335b8b8060SBrian Somers 10343b0f8d2eSBrian Somers struct datalink *odl; 10353b0f8d2eSBrian Somers int other_links; 10365b8b8060SBrian Somers 10373b0f8d2eSBrian Somers other_links = 0; 10383b0f8d2eSBrian Somers for (odl = bundle->links; odl; odl = odl->next) 10393b0f8d2eSBrian Somers if (odl != dl && odl->state != DATALINK_CLOSED) 10403b0f8d2eSBrian Somers other_links++; 10413b0f8d2eSBrian Somers 10423b0f8d2eSBrian Somers if (!other_links) { 104303704096SBrian Somers if (dl->physical->type != PHYS_DEMAND) /* Not in -auto mode */ 104403704096SBrian Somers bundle_DownInterface(bundle); 104526afeaa2SBrian Somers if (bundle->ncp.ipcp.fsm.state > ST_CLOSED || 104626afeaa2SBrian Somers bundle->ncp.ipcp.fsm.state == ST_STARTING) { 1047dd7e2610SBrian Somers fsm_Down(&bundle->ncp.ipcp.fsm); 1048dd7e2610SBrian Somers fsm_Close(&bundle->ncp.ipcp.fsm); /* ST_INITIAL please */ 104926afeaa2SBrian Somers } 10505563ebdeSBrian Somers bundle_NewPhase(bundle, PHASE_DEAD); 10510f2f3eb3SBrian Somers bundle_StopIdleTimer(bundle); 105204eaa58cSBrian Somers bundle_StopAutoLoadTimer(bundle); 105304eaa58cSBrian Somers bundle->autoload.running = 0; 105404eaa58cSBrian Somers } else 105504eaa58cSBrian Somers bundle->autoload.running = 1; 1056455aabc3SBrian Somers } 1057455aabc3SBrian Somers 1058455aabc3SBrian Somers void 1059565e35e5SBrian Somers bundle_Open(struct bundle *bundle, const char *name, int mask) 10603006ec67SBrian Somers { 10613006ec67SBrian Somers /* 10623006ec67SBrian Somers * Please open the given datalink, or all if name == NULL 10633006ec67SBrian Somers */ 10643006ec67SBrian Somers struct datalink *dl; 10653006ec67SBrian Somers 106604eaa58cSBrian Somers timer_Stop(&bundle->autoload.timer); 10673006ec67SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 10683006ec67SBrian Somers if (name == NULL || !strcasecmp(dl->name, name)) { 106904eaa58cSBrian Somers if (dl->state == DATALINK_CLOSED && (mask & dl->physical->type)) { 1070565e35e5SBrian Somers datalink_Up(dl, 1, 1); 107104eaa58cSBrian Somers if (mask == PHYS_DEMAND) 107204eaa58cSBrian Somers /* Only one DEMAND link at a time (see the AutoLoad timer) */ 107304eaa58cSBrian Somers break; 107404eaa58cSBrian Somers } 10753006ec67SBrian Somers if (name != NULL) 10763006ec67SBrian Somers break; 10773006ec67SBrian Somers } 10783006ec67SBrian Somers } 10793006ec67SBrian Somers 10803006ec67SBrian Somers struct datalink * 10813006ec67SBrian Somers bundle2datalink(struct bundle *bundle, const char *name) 10823006ec67SBrian Somers { 10833006ec67SBrian Somers struct datalink *dl; 10843006ec67SBrian Somers 10853006ec67SBrian Somers if (name != NULL) { 10863006ec67SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 10873006ec67SBrian Somers if (!strcasecmp(dl->name, name)) 10883006ec67SBrian Somers return dl; 10893006ec67SBrian Somers } else if (bundle->links && !bundle->links->next) 10903006ec67SBrian Somers return bundle->links; 10913006ec67SBrian Somers 10923006ec67SBrian Somers return NULL; 10933006ec67SBrian Somers } 10943006ec67SBrian Somers 10953006ec67SBrian Somers int 10963006ec67SBrian Somers bundle_FillQueues(struct bundle *bundle) 10973006ec67SBrian Somers { 10983b0f8d2eSBrian Somers int total; 10993b0f8d2eSBrian Somers 11001bc9b5baSBrian Somers if (bundle->ncp.mp.active) 11013b0f8d2eSBrian Somers total = mp_FillQueues(bundle); 11021bc9b5baSBrian Somers else { 11031bc9b5baSBrian Somers struct datalink *dl; 11041bc9b5baSBrian Somers int add; 11051bc9b5baSBrian Somers 11061bc9b5baSBrian Somers for (total = 0, dl = bundle->links; dl; dl = dl->next) 11071bc9b5baSBrian Somers if (dl->state == DATALINK_OPEN) { 11081bc9b5baSBrian Somers add = link_QueueLen(&dl->physical->link); 11091bc9b5baSBrian Somers if (add == 0 && dl->physical->out == NULL) 11101bc9b5baSBrian Somers add = ip_FlushPacket(&dl->physical->link, bundle); 11111bc9b5baSBrian Somers total += add; 11121bc9b5baSBrian Somers } 11133006ec67SBrian Somers } 11143006ec67SBrian Somers 11153b0f8d2eSBrian Somers return total + ip_QueueLen(); 11163006ec67SBrian Somers } 1117aef795ccSBrian Somers 1118aef795ccSBrian Somers int 1119aef795ccSBrian Somers bundle_ShowLinks(struct cmdargs const *arg) 1120aef795ccSBrian Somers { 1121aef795ccSBrian Somers struct datalink *dl; 1122aef795ccSBrian Somers 11239c53a7b1SBrian Somers for (dl = arg->bundle->links; dl; dl = dl->next) { 1124d4156d00SBrian Somers prompt_Printf(arg->prompt, "Name: %s [%s, %s]", 1125d4156d00SBrian Somers dl->name, mode2Nam(dl->physical->type), datalink_State(dl)); 1126eeab6bf5SBrian Somers if (dl->physical->link.throughput.rolling && dl->state == DATALINK_OPEN) 1127d4156d00SBrian Somers prompt_Printf(arg->prompt, " weight %d, %d bytes/sec", 1128eeab6bf5SBrian Somers dl->mp.weight, 11299c53a7b1SBrian Somers dl->physical->link.throughput.OctetsPerSecond); 11309c53a7b1SBrian Somers prompt_Printf(arg->prompt, "\n"); 11319c53a7b1SBrian Somers } 1132aef795ccSBrian Somers 1133aef795ccSBrian Somers return 0; 1134aef795ccSBrian Somers } 1135ab886ad0SBrian Somers 11361342caedSBrian Somers static const char * 11371342caedSBrian Somers optval(struct bundle *bundle, int bit) 11381342caedSBrian Somers { 11391342caedSBrian Somers return (bundle->cfg.opt & bit) ? "enabled" : "disabled"; 11401342caedSBrian Somers } 11411342caedSBrian Somers 1142c08717dfSBrian Somers int 1143c08717dfSBrian Somers bundle_ShowStatus(struct cmdargs const *arg) 1144c08717dfSBrian Somers { 1145c08717dfSBrian Somers int remaining; 1146c08717dfSBrian Somers 1147c08717dfSBrian Somers prompt_Printf(arg->prompt, "Phase %s\n", bundle_PhaseName(arg->bundle)); 1148faefde08SBrian Somers prompt_Printf(arg->prompt, " Device: %s\n", arg->bundle->dev.Name); 1149faefde08SBrian Somers prompt_Printf(arg->prompt, " Interface: %s @ %lubps\n", 1150faefde08SBrian Somers arg->bundle->ifp.Name, arg->bundle->ifp.Speed); 1151c08717dfSBrian Somers 1152c08717dfSBrian Somers prompt_Printf(arg->prompt, "\nDefaults:\n"); 1153643f4904SBrian Somers prompt_Printf(arg->prompt, " Label: %s\n", arg->bundle->cfg.label); 1154610b185fSBrian Somers prompt_Printf(arg->prompt, " Auth name: %s\n", 1155610b185fSBrian Somers arg->bundle->cfg.auth.name); 115604eaa58cSBrian Somers prompt_Printf(arg->prompt, " Auto Load: Up after %ds of >= %d packets\n", 115704eaa58cSBrian Somers arg->bundle->cfg.autoload.max.timeout, 115804eaa58cSBrian Somers arg->bundle->cfg.autoload.max.packets); 115904eaa58cSBrian Somers prompt_Printf(arg->prompt, " Down after %ds of <= %d" 116004eaa58cSBrian Somers " packets\n", arg->bundle->cfg.autoload.min.timeout, 116104eaa58cSBrian Somers arg->bundle->cfg.autoload.min.packets); 116204eaa58cSBrian Somers if (arg->bundle->autoload.timer.state == TIMER_RUNNING) 116304eaa58cSBrian Somers prompt_Printf(arg->prompt, " %ds remaining 'till " 116404eaa58cSBrian Somers "a link comes %s\n", 116504eaa58cSBrian Somers bundle_RemainingAutoLoadTime(arg->bundle), 116604eaa58cSBrian Somers arg->bundle->autoload.comingup ? "up" : "down"); 116704eaa58cSBrian Somers else 116804eaa58cSBrian Somers prompt_Printf(arg->prompt, " %srunning with %d" 116904eaa58cSBrian Somers " packets queued\n", arg->bundle->autoload.running ? 117004eaa58cSBrian Somers "" : "not ", ip_QueueLen()); 117104eaa58cSBrian Somers 1172c08717dfSBrian Somers prompt_Printf(arg->prompt, " Idle Timer: "); 1173c08717dfSBrian Somers if (arg->bundle->cfg.idle_timeout) { 1174c08717dfSBrian Somers prompt_Printf(arg->prompt, "%ds", arg->bundle->cfg.idle_timeout); 1175c08717dfSBrian Somers remaining = bundle_RemainingIdleTime(arg->bundle); 1176c08717dfSBrian Somers if (remaining != -1) 1177c08717dfSBrian Somers prompt_Printf(arg->prompt, " (%ds remaining)", remaining); 1178c08717dfSBrian Somers prompt_Printf(arg->prompt, "\n"); 1179c08717dfSBrian Somers } else 1180c08717dfSBrian Somers prompt_Printf(arg->prompt, "disabled\n"); 1181ce828a6eSBrian Somers prompt_Printf(arg->prompt, " MTU: "); 1182ce828a6eSBrian Somers if (arg->bundle->cfg.mtu) 1183ce828a6eSBrian Somers prompt_Printf(arg->prompt, "%d\n", arg->bundle->cfg.mtu); 1184ce828a6eSBrian Somers else 1185ce828a6eSBrian Somers prompt_Printf(arg->prompt, "unspecified\n"); 1186c08717dfSBrian Somers 1187610b185fSBrian Somers prompt_Printf(arg->prompt, " Sticky Routes: %s\n", 1188610b185fSBrian Somers optval(arg->bundle, OPT_SROUTES)); 11891342caedSBrian Somers prompt_Printf(arg->prompt, " ID check: %s\n", 11901342caedSBrian Somers optval(arg->bundle, OPT_IDCHECK)); 11911342caedSBrian Somers prompt_Printf(arg->prompt, " Loopback: %s\n", 11921342caedSBrian Somers optval(arg->bundle, OPT_LOOPBACK)); 11931342caedSBrian Somers prompt_Printf(arg->prompt, " PasswdAuth: %s\n", 11941342caedSBrian Somers optval(arg->bundle, OPT_PASSWDAUTH)); 11951342caedSBrian Somers prompt_Printf(arg->prompt, " Proxy: %s\n", 11961342caedSBrian Somers optval(arg->bundle, OPT_PROXY)); 11971342caedSBrian Somers prompt_Printf(arg->prompt, " Throughput: %s\n", 11981342caedSBrian Somers optval(arg->bundle, OPT_THROUGHPUT)); 1199610b185fSBrian Somers prompt_Printf(arg->prompt, " Utmp Logging: %s\n", 12001342caedSBrian Somers optval(arg->bundle, OPT_UTMP)); 12011342caedSBrian Somers 1202c08717dfSBrian Somers return 0; 1203c08717dfSBrian Somers } 1204c08717dfSBrian Somers 1205ab886ad0SBrian Somers static void 1206ab886ad0SBrian Somers bundle_IdleTimeout(void *v) 1207ab886ad0SBrian Somers { 1208ab886ad0SBrian Somers struct bundle *bundle = (struct bundle *)v; 1209ab886ad0SBrian Somers 1210dd7e2610SBrian Somers log_Printf(LogPHASE, "Idle timer expired.\n"); 121104eaa58cSBrian Somers bundle_StopIdleTimer(bundle); 1212ab886ad0SBrian Somers bundle_Close(bundle, NULL, 1); 1213ab886ad0SBrian Somers } 1214ab886ad0SBrian Somers 1215ab886ad0SBrian Somers /* 1216ab886ad0SBrian Somers * Start Idle timer. If timeout is reached, we call bundle_Close() to 1217ab886ad0SBrian Somers * close LCP and link. 1218ab886ad0SBrian Somers */ 1219ab886ad0SBrian Somers void 1220ab886ad0SBrian Somers bundle_StartIdleTimer(struct bundle *bundle) 1221ab886ad0SBrian Somers { 1222dd7e2610SBrian Somers timer_Stop(&bundle->idle.timer); 122304eaa58cSBrian Somers if ((bundle->phys_type & (PHYS_DEDICATED|PHYS_PERM)) != bundle->phys_type && 122404eaa58cSBrian Somers bundle->cfg.idle_timeout) { 122593ee0ff2SBrian Somers bundle->idle.timer.func = bundle_IdleTimeout; 12263b0f8d2eSBrian Somers bundle->idle.timer.name = "idle"; 122793ee0ff2SBrian Somers bundle->idle.timer.load = bundle->cfg.idle_timeout * SECTICKS; 122893ee0ff2SBrian Somers bundle->idle.timer.arg = bundle; 1229dd7e2610SBrian Somers timer_Start(&bundle->idle.timer); 123093ee0ff2SBrian Somers bundle->idle.done = time(NULL) + bundle->cfg.idle_timeout; 1231ab886ad0SBrian Somers } 1232ab886ad0SBrian Somers } 1233ab886ad0SBrian Somers 1234ab886ad0SBrian Somers void 1235ab886ad0SBrian Somers bundle_SetIdleTimer(struct bundle *bundle, int value) 1236ab886ad0SBrian Somers { 1237ab886ad0SBrian Somers bundle->cfg.idle_timeout = value; 1238ab886ad0SBrian Somers if (bundle_LinkIsUp(bundle)) 1239ab886ad0SBrian Somers bundle_StartIdleTimer(bundle); 1240ab886ad0SBrian Somers } 1241ab886ad0SBrian Somers 1242ab886ad0SBrian Somers void 1243ab886ad0SBrian Somers bundle_StopIdleTimer(struct bundle *bundle) 1244ab886ad0SBrian Somers { 1245dd7e2610SBrian Somers timer_Stop(&bundle->idle.timer); 12464a632c80SBrian Somers bundle->idle.done = 0; 1247ab886ad0SBrian Somers } 1248ab886ad0SBrian Somers 124904eaa58cSBrian Somers static int 1250ab886ad0SBrian Somers bundle_RemainingIdleTime(struct bundle *bundle) 1251ab886ad0SBrian Somers { 125293ee0ff2SBrian Somers if (bundle->idle.done) 125393ee0ff2SBrian Somers return bundle->idle.done - time(NULL); 1254ab886ad0SBrian Somers return -1; 1255ab886ad0SBrian Somers } 12563b0f8d2eSBrian Somers 12573b0f8d2eSBrian Somers int 12583b0f8d2eSBrian Somers bundle_IsDead(struct bundle *bundle) 12593b0f8d2eSBrian Somers { 12603b0f8d2eSBrian Somers return !bundle->links || (bundle->phase == PHASE_DEAD && bundle->CleaningUp); 12613b0f8d2eSBrian Somers } 1262b6217683SBrian Somers 1263565e35e5SBrian Somers static void 126404eaa58cSBrian Somers bundle_LinkAdded(struct bundle *bundle, struct datalink *dl) 126504eaa58cSBrian Somers { 126604eaa58cSBrian Somers bundle->phys_type |= dl->physical->type; 126704eaa58cSBrian Somers if (dl->physical->type == PHYS_DEMAND && 126804eaa58cSBrian Somers bundle->autoload.timer.state == TIMER_STOPPED && 126904eaa58cSBrian Somers bundle->phase == PHASE_NETWORK) 127004eaa58cSBrian Somers bundle->autoload.running = 1; 127104eaa58cSBrian Somers } 127204eaa58cSBrian Somers 127304eaa58cSBrian Somers static void 127404eaa58cSBrian Somers bundle_LinksRemoved(struct bundle *bundle) 1275565e35e5SBrian Somers { 1276565e35e5SBrian Somers struct datalink *dl; 1277565e35e5SBrian Somers 1278565e35e5SBrian Somers bundle->phys_type = 0; 1279565e35e5SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 128004eaa58cSBrian Somers bundle_LinkAdded(bundle, dl); 128104eaa58cSBrian Somers 128204eaa58cSBrian Somers if ((bundle->phys_type & (PHYS_DEDICATED|PHYS_PERM)) == bundle->phys_type) 128304eaa58cSBrian Somers timer_Stop(&bundle->idle.timer); 1284565e35e5SBrian Somers } 1285565e35e5SBrian Somers 128604eaa58cSBrian Somers static struct datalink * 128704eaa58cSBrian Somers bundle_DatalinkLinkout(struct bundle *bundle, struct datalink *dl) 1288cd7bd93aSBrian Somers { 1289cd7bd93aSBrian Somers struct datalink **dlp; 1290cd7bd93aSBrian Somers 1291cd7bd93aSBrian Somers for (dlp = &bundle->links; *dlp; dlp = &(*dlp)->next) 1292cd7bd93aSBrian Somers if (*dlp == dl) { 129304eaa58cSBrian Somers *dlp = dl->next; 129404eaa58cSBrian Somers dl->next = NULL; 129504eaa58cSBrian Somers bundle_LinksRemoved(bundle); 129604eaa58cSBrian Somers return dl; 1297cd7bd93aSBrian Somers } 129804eaa58cSBrian Somers 129904eaa58cSBrian Somers return NULL; 130004eaa58cSBrian Somers } 130104eaa58cSBrian Somers 130204eaa58cSBrian Somers static void 130304eaa58cSBrian Somers bundle_DatalinkLinkin(struct bundle *bundle, struct datalink *dl) 130404eaa58cSBrian Somers { 130504eaa58cSBrian Somers struct datalink **dlp = &bundle->links; 130604eaa58cSBrian Somers 130704eaa58cSBrian Somers while (*dlp) 130804eaa58cSBrian Somers dlp = &(*dlp)->next; 130904eaa58cSBrian Somers 131004eaa58cSBrian Somers *dlp = dl; 131104eaa58cSBrian Somers dl->next = NULL; 131204eaa58cSBrian Somers 131304eaa58cSBrian Somers bundle_LinkAdded(bundle, dl); 1314565e35e5SBrian Somers } 1315565e35e5SBrian Somers 1316565e35e5SBrian Somers void 1317565e35e5SBrian Somers bundle_CleanDatalinks(struct bundle *bundle) 1318565e35e5SBrian Somers { 1319565e35e5SBrian Somers struct datalink **dlp = &bundle->links; 132004eaa58cSBrian Somers int found = 0; 1321565e35e5SBrian Somers 1322565e35e5SBrian Somers while (*dlp) 1323565e35e5SBrian Somers if ((*dlp)->state == DATALINK_CLOSED && 132404eaa58cSBrian Somers (*dlp)->physical->type & (PHYS_DIRECT|PHYS_1OFF)) { 1325565e35e5SBrian Somers *dlp = datalink_Destroy(*dlp); 132604eaa58cSBrian Somers found++; 132704eaa58cSBrian Somers } else 1328565e35e5SBrian Somers dlp = &(*dlp)->next; 132904eaa58cSBrian Somers 133004eaa58cSBrian Somers if (found) 133104eaa58cSBrian Somers bundle_LinksRemoved(bundle); 133204eaa58cSBrian Somers } 133304eaa58cSBrian Somers 133404eaa58cSBrian Somers int 133504eaa58cSBrian Somers bundle_DatalinkClone(struct bundle *bundle, struct datalink *dl, 133604eaa58cSBrian Somers const char *name) 133704eaa58cSBrian Somers { 133804eaa58cSBrian Somers if (bundle2datalink(bundle, name)) { 133904eaa58cSBrian Somers log_Printf(LogWARN, "Clone: %s: name already exists\n", name); 134004eaa58cSBrian Somers return 0; 134104eaa58cSBrian Somers } 134204eaa58cSBrian Somers 134304eaa58cSBrian Somers bundle_DatalinkLinkin(bundle, datalink_Clone(dl, name)); 134404eaa58cSBrian Somers return 1; 134504eaa58cSBrian Somers } 134604eaa58cSBrian Somers 134704eaa58cSBrian Somers void 134804eaa58cSBrian Somers bundle_DatalinkRemove(struct bundle *bundle, struct datalink *dl) 134904eaa58cSBrian Somers { 135004eaa58cSBrian Somers dl = bundle_DatalinkLinkout(bundle, dl); 135104eaa58cSBrian Somers if (dl) 135204eaa58cSBrian Somers datalink_Destroy(dl); 1353cd7bd93aSBrian Somers } 135449052c95SBrian Somers 135549052c95SBrian Somers void 135649052c95SBrian Somers bundle_SetLabel(struct bundle *bundle, const char *label) 135749052c95SBrian Somers { 135849052c95SBrian Somers if (label) 135949052c95SBrian Somers strncpy(bundle->cfg.label, label, sizeof bundle->cfg.label - 1); 136049052c95SBrian Somers else 136149052c95SBrian Somers *bundle->cfg.label = '\0'; 136249052c95SBrian Somers } 136349052c95SBrian Somers 136449052c95SBrian Somers const char * 136549052c95SBrian Somers bundle_GetLabel(struct bundle *bundle) 136649052c95SBrian Somers { 136749052c95SBrian Somers return *bundle->cfg.label ? bundle->cfg.label : NULL; 136849052c95SBrian Somers } 13691fa665f5SBrian Somers 13701fa665f5SBrian Somers void 137196c9bb21SBrian Somers bundle_ReceiveDatalink(struct bundle *bundle, int s, struct sockaddr_un *sun) 13721fa665f5SBrian Somers { 137396c9bb21SBrian Somers char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int)]; 137496c9bb21SBrian Somers struct cmsghdr *cmsg = (struct cmsghdr *)cmsgbuf; 137596c9bb21SBrian Somers struct msghdr msg; 137696c9bb21SBrian Somers struct iovec iov[SCATTER_SEGMENTS]; 1377b7c5748eSBrian Somers struct datalink *dl; 137896c9bb21SBrian Somers int niov, link_fd, expect, f; 137985fd273aSBrian Somers pid_t pid; 13806f384573SBrian Somers 1381dd7e2610SBrian Somers log_Printf(LogPHASE, "Receiving datalink\n"); 13826f384573SBrian Somers 138396c9bb21SBrian Somers /* Create our scatter/gather array */ 138496c9bb21SBrian Somers niov = 1; 138596c9bb21SBrian Somers iov[0].iov_len = strlen(Version) + 1; 138696c9bb21SBrian Somers iov[0].iov_base = (char *)malloc(iov[0].iov_len); 138785fd273aSBrian Somers if (datalink2iov(NULL, iov, &niov, sizeof iov / sizeof *iov, 0) == -1) { 138854cd8e13SBrian Somers close(s); 13896f384573SBrian Somers return; 139054cd8e13SBrian Somers } 13916f384573SBrian Somers 139285fd273aSBrian Somers pid = getpid(); 139385fd273aSBrian Somers write(s, &pid, sizeof pid); 139485fd273aSBrian Somers 139596c9bb21SBrian Somers for (f = expect = 0; f < niov; f++) 139696c9bb21SBrian Somers expect += iov[f].iov_len; 139796c9bb21SBrian Somers 139896c9bb21SBrian Somers /* Set up our message */ 139996c9bb21SBrian Somers cmsg->cmsg_len = sizeof cmsgbuf; 140096c9bb21SBrian Somers cmsg->cmsg_level = SOL_SOCKET; 140154cd8e13SBrian Somers cmsg->cmsg_type = 0; 140296c9bb21SBrian Somers 140396c9bb21SBrian Somers memset(&msg, '\0', sizeof msg); 140496c9bb21SBrian Somers msg.msg_name = (caddr_t)sun; 140596c9bb21SBrian Somers msg.msg_namelen = sizeof *sun; 140696c9bb21SBrian Somers msg.msg_iov = iov; 140796c9bb21SBrian Somers msg.msg_iovlen = niov; 140896c9bb21SBrian Somers msg.msg_control = cmsgbuf; 140996c9bb21SBrian Somers msg.msg_controllen = sizeof cmsgbuf; 141096c9bb21SBrian Somers 141196c9bb21SBrian Somers log_Printf(LogDEBUG, "Expecting %d scatter/gather bytes\n", expect); 141296c9bb21SBrian Somers f = expect + 100; 141396c9bb21SBrian Somers setsockopt(s, SOL_SOCKET, SO_RCVBUF, &f, sizeof f); 141496c9bb21SBrian Somers if ((f = recvmsg(s, &msg, MSG_WAITALL)) != expect) { 141596c9bb21SBrian Somers if (f == -1) 141696c9bb21SBrian Somers log_Printf(LogERROR, "Failed recvmsg: %s\n", strerror(errno)); 141796c9bb21SBrian Somers else 141896c9bb21SBrian Somers log_Printf(LogERROR, "Failed recvmsg: Got %d, not %d\n", f, expect); 141996c9bb21SBrian Somers while (niov--) 142096c9bb21SBrian Somers free(iov[niov].iov_base); 142154cd8e13SBrian Somers close(s); 142296c9bb21SBrian Somers return; 142396c9bb21SBrian Somers } 142496c9bb21SBrian Somers 1425c0d9a877SBrian Somers write(s, "!", 1); /* ACK */ 1426c0d9a877SBrian Somers 142754cd8e13SBrian Somers if (cmsg->cmsg_type == SCM_RIGHTS) { 142854cd8e13SBrian Somers /* We've successfully received an open file descriptor through our socket */ 142954cd8e13SBrian Somers log_Printf(LogDEBUG, "Receiving non-tty device\n"); 143054cd8e13SBrian Somers link_fd = *(int *)CMSG_DATA(cmsg); 143154cd8e13SBrian Somers } else { 143254cd8e13SBrian Somers /* It's a ``controlling'' tty device via CATPROG */ 143354cd8e13SBrian Somers log_Printf(LogDEBUG, "Receiving tty device\n"); 143454cd8e13SBrian Somers link_fd = dup(s); 143554cd8e13SBrian Somers fcntl(link_fd, F_SETFL, fcntl(link_fd, F_GETFL, 0) | O_NONBLOCK); 143654cd8e13SBrian Somers } 143754cd8e13SBrian Somers 143896c9bb21SBrian Somers if (strncmp(Version, iov[0].iov_base, iov[0].iov_len)) { 143996c9bb21SBrian Somers log_Printf(LogWARN, "Cannot receive datalink, incorrect version" 144096c9bb21SBrian Somers " (\"%.*s\", not \"%s\")\n", (int)iov[0].iov_len, 144196c9bb21SBrian Somers iov[0].iov_base, Version); 144296c9bb21SBrian Somers close(link_fd); 144396c9bb21SBrian Somers while (niov--) 144496c9bb21SBrian Somers free(iov[niov].iov_base); 144596c9bb21SBrian Somers return; 144696c9bb21SBrian Somers } 144796c9bb21SBrian Somers 144896c9bb21SBrian Somers niov = 1; 1449b7c5748eSBrian Somers dl = iov2datalink(bundle, iov, &niov, sizeof iov / sizeof *iov, link_fd); 1450b7c5748eSBrian Somers if (dl) { 145104eaa58cSBrian Somers bundle_DatalinkLinkin(bundle, dl); 1452b7c5748eSBrian Somers datalink_AuthOk(dl); 145396c9bb21SBrian Somers } else 145496c9bb21SBrian Somers close(link_fd); 145596c9bb21SBrian Somers 145696c9bb21SBrian Somers free(iov[0].iov_base); 145754cd8e13SBrian Somers close(s); 14581fa665f5SBrian Somers } 14591fa665f5SBrian Somers 14601fa665f5SBrian Somers void 146196c9bb21SBrian Somers bundle_SendDatalink(struct datalink *dl, int s, struct sockaddr_un *sun) 14621fa665f5SBrian Somers { 1463c0d9a877SBrian Somers char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int)], ack; 146496c9bb21SBrian Somers struct cmsghdr *cmsg = (struct cmsghdr *)cmsgbuf; 146596c9bb21SBrian Somers struct msghdr msg; 146696c9bb21SBrian Somers struct iovec iov[SCATTER_SEGMENTS]; 14671384bd27SBrian Somers int niov, link_fd, f, expect, newsid; 146885fd273aSBrian Somers pid_t newpid; 14696f384573SBrian Somers 1470dd7e2610SBrian Somers log_Printf(LogPHASE, "Transmitting datalink %s\n", dl->name); 14716f384573SBrian Somers 147204eaa58cSBrian Somers bundle_LinkClosed(dl->bundle, dl); 14730f2f3eb3SBrian Somers bundle_DatalinkLinkout(dl->bundle, dl); 1474ea722969SBrian Somers 147596c9bb21SBrian Somers /* Build our scatter/gather array */ 147696c9bb21SBrian Somers iov[0].iov_len = strlen(Version) + 1; 147796c9bb21SBrian Somers iov[0].iov_base = strdup(Version); 147896c9bb21SBrian Somers niov = 1; 14796f384573SBrian Somers 148085fd273aSBrian Somers read(s, &newpid, sizeof newpid); 148185fd273aSBrian Somers link_fd = datalink2iov(dl, iov, &niov, sizeof iov / sizeof *iov, newpid); 14826f384573SBrian Somers 14836f384573SBrian Somers if (link_fd != -1) { 148496c9bb21SBrian Somers memset(&msg, '\0', sizeof msg); 148554cd8e13SBrian Somers 148696c9bb21SBrian Somers msg.msg_name = (caddr_t)sun; 148796c9bb21SBrian Somers msg.msg_namelen = sizeof *sun; 148896c9bb21SBrian Somers msg.msg_iov = iov; 148996c9bb21SBrian Somers msg.msg_iovlen = niov; 149054cd8e13SBrian Somers 149154cd8e13SBrian Somers cmsg->cmsg_len = sizeof cmsgbuf; 149254cd8e13SBrian Somers cmsg->cmsg_level = SOL_SOCKET; 149354cd8e13SBrian Somers cmsg->cmsg_type = SCM_RIGHTS; 149454cd8e13SBrian Somers *(int *)CMSG_DATA(cmsg) = link_fd; 149596c9bb21SBrian Somers msg.msg_control = cmsgbuf; 149696c9bb21SBrian Somers msg.msg_controllen = sizeof cmsgbuf; 149747723d29SBrian Somers 149896c9bb21SBrian Somers for (f = expect = 0; f < niov; f++) 149996c9bb21SBrian Somers expect += iov[f].iov_len; 150047723d29SBrian Somers 150196c9bb21SBrian Somers log_Printf(LogDEBUG, "Sending %d bytes in scatter/gather array\n", expect); 150247723d29SBrian Somers 150396c9bb21SBrian Somers f = expect + SOCKET_OVERHEAD; 150496c9bb21SBrian Somers setsockopt(s, SOL_SOCKET, SO_SNDBUF, &f, sizeof f); 150596c9bb21SBrian Somers if (sendmsg(s, &msg, 0) == -1) 150696c9bb21SBrian Somers log_Printf(LogERROR, "Failed sendmsg: %s\n", strerror(errno)); 1507c0d9a877SBrian Somers /* We must get the ACK before closing the descriptor ! */ 1508c0d9a877SBrian Somers read(s, &ack, 1); 150954cd8e13SBrian Somers 15101384bd27SBrian Somers newsid = tcgetpgrp(link_fd) == getpgrp(); 151196c9bb21SBrian Somers close(link_fd); 15121384bd27SBrian Somers if (newsid) 15131384bd27SBrian Somers bundle_setsid(dl->bundle, 1); 151447723d29SBrian Somers } 151585fd273aSBrian Somers close(s); 151696c9bb21SBrian Somers 151796c9bb21SBrian Somers while (niov--) 151896c9bb21SBrian Somers free(iov[niov].iov_base); 15191fa665f5SBrian Somers } 1520dd0645c5SBrian Somers 1521dd0645c5SBrian Somers int 152258d55334SBrian Somers bundle_RenameDatalink(struct bundle *bundle, struct datalink *ndl, 152358d55334SBrian Somers const char *name) 152458d55334SBrian Somers { 152558d55334SBrian Somers struct datalink *dl; 152658d55334SBrian Somers 152758d55334SBrian Somers if (!strcasecmp(ndl->name, name)) 152858d55334SBrian Somers return 1; 152958d55334SBrian Somers 153058d55334SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 153158d55334SBrian Somers if (!strcasecmp(dl->name, name)) 153258d55334SBrian Somers return 0; 153358d55334SBrian Somers 153458d55334SBrian Somers datalink_Rename(ndl, name); 153558d55334SBrian Somers return 1; 153658d55334SBrian Somers } 153758d55334SBrian Somers 153858d55334SBrian Somers int 1539dd0645c5SBrian Somers bundle_SetMode(struct bundle *bundle, struct datalink *dl, int mode) 1540dd0645c5SBrian Somers { 1541dd0645c5SBrian Somers int omode; 1542dd0645c5SBrian Somers 1543dd0645c5SBrian Somers omode = dl->physical->type; 1544dd0645c5SBrian Somers if (omode == mode) 1545dd0645c5SBrian Somers return 1; 1546dd0645c5SBrian Somers 1547dd0645c5SBrian Somers if (mode == PHYS_DEMAND && !(bundle->phys_type & PHYS_DEMAND)) 1548dd0645c5SBrian Somers /* Changing to demand-dial mode */ 1549dd0645c5SBrian Somers if (bundle->ncp.ipcp.peer_ip.s_addr == INADDR_ANY) { 1550dd0645c5SBrian Somers log_Printf(LogWARN, "You must `set ifaddr' before changing mode to %s\n", 1551dd0645c5SBrian Somers mode2Nam(mode)); 1552dd0645c5SBrian Somers return 0; 1553dd0645c5SBrian Somers } 1554dd0645c5SBrian Somers 1555dd0645c5SBrian Somers if (!datalink_SetMode(dl, mode)) 1556dd0645c5SBrian Somers return 0; 1557dd0645c5SBrian Somers 1558dd0645c5SBrian Somers if (mode == PHYS_DEMAND && !(bundle->phys_type & PHYS_DEMAND)) 1559dd0645c5SBrian Somers ipcp_InterfaceUp(&bundle->ncp.ipcp); 1560dd0645c5SBrian Somers 156104eaa58cSBrian Somers /* Regenerate phys_type and adjust autoload & idle timers */ 156204eaa58cSBrian Somers bundle_LinksRemoved(bundle); 1563dd0645c5SBrian Somers 1564dd0645c5SBrian Somers if (omode == PHYS_DEMAND && !(bundle->phys_type & PHYS_DEMAND)) 1565dd0645c5SBrian Somers /* Changing from demand-dial mode */ 1566dd0645c5SBrian Somers ipcp_CleanInterface(&bundle->ncp.ipcp); 1567dd0645c5SBrian Somers 1568dd0645c5SBrian Somers return 1; 1569dd0645c5SBrian Somers } 15701384bd27SBrian Somers 15711384bd27SBrian Somers void 15721384bd27SBrian Somers bundle_setsid(struct bundle *bundle, int holdsession) 15731384bd27SBrian Somers { 15741384bd27SBrian Somers /* 15751384bd27SBrian Somers * Lose the current session. This means getting rid of our pid 15761384bd27SBrian Somers * too so that the tty device will really go away, and any getty 15771384bd27SBrian Somers * etc will be allowed to restart. 15781384bd27SBrian Somers */ 15791384bd27SBrian Somers pid_t pid, orig; 15801384bd27SBrian Somers int fds[2]; 15811384bd27SBrian Somers char done; 15821384bd27SBrian Somers struct datalink *dl; 15831384bd27SBrian Somers 15841384bd27SBrian Somers orig = getpid(); 15851384bd27SBrian Somers if (pipe(fds) == -1) { 15861384bd27SBrian Somers log_Printf(LogERROR, "pipe: %s\n", strerror(errno)); 15871384bd27SBrian Somers return; 15881384bd27SBrian Somers } 15891384bd27SBrian Somers switch ((pid = fork())) { 15901384bd27SBrian Somers case -1: 15911384bd27SBrian Somers log_Printf(LogERROR, "fork: %s\n", strerror(errno)); 15921384bd27SBrian Somers close(fds[0]); 15931384bd27SBrian Somers close(fds[1]); 15941384bd27SBrian Somers return; 15951384bd27SBrian Somers case 0: 15961384bd27SBrian Somers close(fds[0]); 15971384bd27SBrian Somers read(fds[1], &done, 1); /* uu_locks are mine ! */ 15981384bd27SBrian Somers close(fds[1]); 15991384bd27SBrian Somers if (pipe(fds) == -1) { 16001384bd27SBrian Somers log_Printf(LogERROR, "pipe(2): %s\n", strerror(errno)); 16011384bd27SBrian Somers return; 16021384bd27SBrian Somers } 16031384bd27SBrian Somers switch ((pid = fork())) { 16041384bd27SBrian Somers case -1: 16051384bd27SBrian Somers log_Printf(LogERROR, "fork: %s\n", strerror(errno)); 16061384bd27SBrian Somers close(fds[0]); 16071384bd27SBrian Somers close(fds[1]); 16081384bd27SBrian Somers return; 16091384bd27SBrian Somers case 0: 16101384bd27SBrian Somers close(fds[0]); 16111384bd27SBrian Somers bundle_LockTun(bundle); /* update pid */ 16121384bd27SBrian Somers read(fds[1], &done, 1); /* uu_locks are mine ! */ 16131384bd27SBrian Somers close(fds[1]); 16141384bd27SBrian Somers setsid(); 16151384bd27SBrian Somers log_Printf(LogPHASE, "%d -> %d: %s session control\n", 16161384bd27SBrian Somers (int)orig, (int)getpid(), 16171384bd27SBrian Somers holdsession ? "Passed" : "Dropped"); 16181384bd27SBrian Somers break; 16191384bd27SBrian Somers default: 16201384bd27SBrian Somers close(fds[1]); 16211384bd27SBrian Somers /* Give away all our modem locks (to the final process) */ 16221384bd27SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 16231384bd27SBrian Somers if (dl->state != DATALINK_CLOSED) 16241384bd27SBrian Somers modem_ChangedPid(dl->physical, pid); 16251384bd27SBrian Somers write(fds[0], "!", 1); /* done */ 16261384bd27SBrian Somers close(fds[0]); 16271384bd27SBrian Somers exit(0); 16281384bd27SBrian Somers break; 16291384bd27SBrian Somers } 16301384bd27SBrian Somers break; 16311384bd27SBrian Somers default: 16321384bd27SBrian Somers close(fds[1]); 16331384bd27SBrian Somers /* Give away all our modem locks (to the intermediate process) */ 16341384bd27SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 16351384bd27SBrian Somers if (dl->state != DATALINK_CLOSED) 16361384bd27SBrian Somers modem_ChangedPid(dl->physical, pid); 16371384bd27SBrian Somers write(fds[0], "!", 1); /* done */ 16381384bd27SBrian Somers close(fds[0]); 16391384bd27SBrian Somers if (holdsession) { 16401384bd27SBrian Somers int fd, status; 16411384bd27SBrian Somers 16421384bd27SBrian Somers timer_TermService(); 16431384bd27SBrian Somers signal(SIGPIPE, SIG_DFL); 16441384bd27SBrian Somers signal(SIGALRM, SIG_DFL); 16451384bd27SBrian Somers signal(SIGHUP, SIG_DFL); 16461384bd27SBrian Somers signal(SIGTERM, SIG_DFL); 16471384bd27SBrian Somers signal(SIGINT, SIG_DFL); 16481384bd27SBrian Somers signal(SIGQUIT, SIG_DFL); 16491384bd27SBrian Somers for (fd = getdtablesize(); fd >= 0; fd--) 16501384bd27SBrian Somers close(fd); 16511384bd27SBrian Somers setuid(geteuid()); 16521384bd27SBrian Somers /* 16531384bd27SBrian Somers * Reap the intermediate process. As we're not exiting but the 16541384bd27SBrian Somers * intermediate is, we don't want it to become defunct. 16551384bd27SBrian Somers */ 16561384bd27SBrian Somers waitpid(pid, &status, 0); 16571384bd27SBrian Somers /* 16581384bd27SBrian Somers * Hang around for a HUP. This should happen as soon as the 16591384bd27SBrian Somers * ppp that we passed our ctty descriptor to closes it. 16601384bd27SBrian Somers * NOTE: If this process dies, the passed descriptor becomes 16611384bd27SBrian Somers * invalid and will give a select() error by setting one 16621384bd27SBrian Somers * of the error fds, aborting the other ppp. We don't 16631384bd27SBrian Somers * want that to happen ! 16641384bd27SBrian Somers */ 16651384bd27SBrian Somers pause(); 16661384bd27SBrian Somers } 16671384bd27SBrian Somers exit(0); 16681384bd27SBrian Somers break; 16691384bd27SBrian Somers } 16701384bd27SBrian Somers } 1671