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 * 2671555108SBrian Somers * $Id: bundle.c,v 1.13 1998/06/06 20:50:54 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 */ 21981358fa3SBrian Somers bundle_Open(bundle, NULL, PHYS_AUTO); 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) 22581358fa3SBrian Somers if (dl->physical->type == PHYS_AUTO && 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) 24581358fa3SBrian Somers if (dl->state == DATALINK_CLOSED && dl->physical->type == PHYS_AUTO) { 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) { 26581358fa3SBrian Somers if (dl->physical->type == PHYS_AUTO) 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 486078c562eSBrian Somers /* If there are aren't many packets queued, look for some more. */ 48704eaa58cSBrian Somers for (nlinks = 0, dl = bundle->links; dl; dl = dl->next) 48804eaa58cSBrian Somers nlinks++; 48904eaa58cSBrian Somers 49004eaa58cSBrian Somers if (nlinks) { 49104eaa58cSBrian Somers queued = r ? bundle_FillQueues(bundle) : ip_QueueLen(); 49204eaa58cSBrian Somers if (bundle->autoload.running) { 49304eaa58cSBrian Somers if (queued < bundle->cfg.autoload.max.packets) { 49404eaa58cSBrian Somers if (queued > bundle->cfg.autoload.min.packets) 49504eaa58cSBrian Somers bundle_StopAutoLoadTimer(bundle); 49604eaa58cSBrian Somers else if (bundle->autoload.timer.state != TIMER_RUNNING || 49704eaa58cSBrian Somers bundle->autoload.comingup) 49804eaa58cSBrian Somers bundle_StartAutoLoadTimer(bundle, 0); 49904eaa58cSBrian Somers } else if (bundle->autoload.timer.state != TIMER_RUNNING || 50004eaa58cSBrian Somers !bundle->autoload.comingup) 50104eaa58cSBrian Somers bundle_StartAutoLoadTimer(bundle, 1); 50204eaa58cSBrian Somers } 50304eaa58cSBrian Somers 5040f2f3eb3SBrian Somers if (r && 50581358fa3SBrian Somers (bundle->phase == PHASE_NETWORK || bundle->phys_type & PHYS_AUTO)) { 50604eaa58cSBrian Somers /* enough surplus so that we can tell if we're getting swamped */ 50704eaa58cSBrian Somers want = bundle->cfg.autoload.max.packets + nlinks * 2; 50804eaa58cSBrian Somers /* but at least 20 packets ! */ 50904eaa58cSBrian Somers if (want < 20) 51004eaa58cSBrian Somers want = 20; 51104eaa58cSBrian Somers if (queued < want) { 51204eaa58cSBrian Somers /* Not enough - select() for more */ 51304eaa58cSBrian Somers FD_SET(bundle->dev.fd, r); 514faefde08SBrian Somers if (*n < bundle->dev.fd + 1) 515faefde08SBrian Somers *n = bundle->dev.fd + 1; 5161384bd27SBrian Somers log_Printf(LogTIMER, "%s: fdset(r) %d\n", TUN_NAME, bundle->dev.fd); 517078c562eSBrian Somers result++; 518078c562eSBrian Somers } 51904eaa58cSBrian Somers } 52004eaa58cSBrian Somers } 521078c562eSBrian Somers 52271555108SBrian Somers /* Which links need a select() ? */ 52371555108SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 52471555108SBrian Somers result += descriptor_UpdateSet(&dl->desc, r, w, e, n); 52571555108SBrian Somers 526ea722969SBrian Somers /* 527ea722969SBrian Somers * This *MUST* be called after the datalink UpdateSet()s as it 52804eaa58cSBrian Somers * might be ``holding'' one of the datalinks (death-row) and 52904eaa58cSBrian Somers * wants to be able to de-select() it from the descriptor set. 530ea722969SBrian Somers */ 5310f2f3eb3SBrian Somers result += descriptor_UpdateSet(&bundle->ncp.mp.server.desc, r, w, e, n); 532ea722969SBrian Somers 5332f786681SBrian Somers return result; 5342f786681SBrian Somers } 5352f786681SBrian Somers 5362f786681SBrian Somers static int 5372f786681SBrian Somers bundle_IsSet(struct descriptor *d, const fd_set *fdset) 5382f786681SBrian Somers { 5392f786681SBrian Somers struct bundle *bundle = descriptor2bundle(d); 5402f786681SBrian Somers struct datalink *dl; 5412f786681SBrian Somers 5422f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 5432f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 5442f786681SBrian Somers return 1; 5452f786681SBrian Somers 546332b9de0SBrian Somers if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 547332b9de0SBrian Somers return 1; 548332b9de0SBrian Somers 549faefde08SBrian Somers return FD_ISSET(bundle->dev.fd, fdset); 5502f786681SBrian Somers } 5512f786681SBrian Somers 5522f786681SBrian Somers static void 5532f786681SBrian Somers bundle_DescriptorRead(struct descriptor *d, struct bundle *bundle, 5542f786681SBrian Somers const fd_set *fdset) 5552f786681SBrian Somers { 5562f786681SBrian Somers struct datalink *dl; 5572f786681SBrian Somers 558ea722969SBrian Somers if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 559ea722969SBrian Somers descriptor_Read(&bundle->ncp.mp.server.desc, bundle, fdset); 560ea722969SBrian Somers 5612f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 5622f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 5632f786681SBrian Somers descriptor_Read(&dl->desc, bundle, fdset); 564b6217683SBrian Somers 565faefde08SBrian Somers if (FD_ISSET(bundle->dev.fd, fdset)) { 566078c562eSBrian Somers struct tun_data tun; 567078c562eSBrian Somers int n, pri; 568078c562eSBrian Somers 569078c562eSBrian Somers /* something to read from tun */ 570faefde08SBrian Somers n = read(bundle->dev.fd, &tun, sizeof tun); 571078c562eSBrian Somers if (n < 0) { 5721384bd27SBrian Somers log_Printf(LogERROR, "read from %s: %s\n", TUN_NAME, strerror(errno)); 573078c562eSBrian Somers return; 574078c562eSBrian Somers } 575078c562eSBrian Somers n -= sizeof tun - sizeof tun.data; 576078c562eSBrian Somers if (n <= 0) { 5771384bd27SBrian Somers log_Printf(LogERROR, "read from %s: Only %d bytes read\n", TUN_NAME, n); 578078c562eSBrian Somers return; 579078c562eSBrian Somers } 580078c562eSBrian Somers if (!tun_check_header(tun, AF_INET)) 581078c562eSBrian Somers return; 582078c562eSBrian Somers 583078c562eSBrian Somers if (((struct ip *)tun.data)->ip_dst.s_addr == 584078c562eSBrian Somers bundle->ncp.ipcp.my_ip.s_addr) { 585078c562eSBrian Somers /* we've been asked to send something addressed *to* us :( */ 586078c562eSBrian Somers if (Enabled(bundle, OPT_LOOPBACK)) { 587078c562eSBrian Somers pri = PacketCheck(bundle, tun.data, n, &bundle->filter.in); 588078c562eSBrian Somers if (pri >= 0) { 589078c562eSBrian Somers struct mbuf *bp; 590078c562eSBrian Somers 591078c562eSBrian Somers #ifndef NOALIAS 592078c562eSBrian Somers if (alias_IsEnabled()) { 593078c562eSBrian Somers (*PacketAlias.In)(tun.data, sizeof tun.data); 594078c562eSBrian Somers n = ntohs(((struct ip *)tun.data)->ip_len); 595078c562eSBrian Somers } 596078c562eSBrian Somers #endif 597078c562eSBrian Somers bp = mbuf_Alloc(n, MB_IPIN); 598078c562eSBrian Somers memcpy(MBUF_CTOP(bp), tun.data, n); 599078c562eSBrian Somers ip_Input(bundle, bp); 600078c562eSBrian Somers log_Printf(LogDEBUG, "Looped back packet addressed to myself\n"); 601078c562eSBrian Somers } 602078c562eSBrian Somers return; 603078c562eSBrian Somers } else 604078c562eSBrian Somers log_Printf(LogDEBUG, "Oops - forwarding packet addressed to myself\n"); 605078c562eSBrian Somers } 606078c562eSBrian Somers 607078c562eSBrian Somers /* 608078c562eSBrian Somers * Process on-demand dialup. Output packets are queued within tunnel 609078c562eSBrian Somers * device until IPCP is opened. 610078c562eSBrian Somers */ 611078c562eSBrian Somers 612078c562eSBrian Somers if (bundle_Phase(bundle) == PHASE_DEAD) { 613078c562eSBrian Somers /* 614078c562eSBrian Somers * Note, we must be in AUTO mode :-/ otherwise our interface should 615078c562eSBrian Somers * *not* be UP and we can't receive data 616078c562eSBrian Somers */ 617078c562eSBrian Somers if ((pri = PacketCheck(bundle, tun.data, n, &bundle->filter.dial)) >= 0) 61881358fa3SBrian Somers bundle_Open(bundle, NULL, PHYS_AUTO); 619078c562eSBrian Somers else 620078c562eSBrian Somers /* 621078c562eSBrian Somers * Drop the packet. If we were to queue it, we'd just end up with 622078c562eSBrian Somers * a pile of timed-out data in our output queue by the time we get 623078c562eSBrian Somers * around to actually dialing. We'd also prematurely reach the 624078c562eSBrian Somers * threshold at which we stop select()ing to read() the tun 625078c562eSBrian Somers * device - breaking auto-dial. 626078c562eSBrian Somers */ 627078c562eSBrian Somers return; 628078c562eSBrian Somers } 629078c562eSBrian Somers 630078c562eSBrian Somers pri = PacketCheck(bundle, tun.data, n, &bundle->filter.out); 631078c562eSBrian Somers if (pri >= 0) { 632078c562eSBrian Somers #ifndef NOALIAS 633078c562eSBrian Somers if (alias_IsEnabled()) { 634078c562eSBrian Somers (*PacketAlias.Out)(tun.data, sizeof tun.data); 635078c562eSBrian Somers n = ntohs(((struct ip *)tun.data)->ip_len); 636078c562eSBrian Somers } 637078c562eSBrian Somers #endif 638078c562eSBrian Somers ip_Enqueue(pri, tun.data, n); 639078c562eSBrian Somers } 640078c562eSBrian Somers } 6412f786681SBrian Somers } 6422f786681SBrian Somers 6432f786681SBrian Somers static void 6442f786681SBrian Somers bundle_DescriptorWrite(struct descriptor *d, struct bundle *bundle, 6452f786681SBrian Somers const fd_set *fdset) 6462f786681SBrian Somers { 6472f786681SBrian Somers struct datalink *dl; 6482f786681SBrian Somers 649ea722969SBrian Somers /* This is not actually necessary as struct mpserver doesn't Write() */ 650ea722969SBrian Somers if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 651ea722969SBrian Somers descriptor_Write(&bundle->ncp.mp.server.desc, bundle, fdset); 652ea722969SBrian Somers 6532f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 6542f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 6552f786681SBrian Somers descriptor_Write(&dl->desc, bundle, fdset); 6562f786681SBrian Somers } 6572f786681SBrian Somers 658da66dd13SBrian Somers void 6591384bd27SBrian Somers bundle_LockTun(struct bundle *bundle) 6601384bd27SBrian Somers { 6611384bd27SBrian Somers FILE *lockfile; 6621384bd27SBrian Somers char pidfile[MAXPATHLEN]; 6631384bd27SBrian Somers 6641384bd27SBrian Somers snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit); 6651384bd27SBrian Somers lockfile = ID0fopen(pidfile, "w"); 6661384bd27SBrian Somers if (lockfile != NULL) { 6671384bd27SBrian Somers fprintf(lockfile, "%d\n", (int)getpid()); 6681384bd27SBrian Somers fclose(lockfile); 6691384bd27SBrian Somers } 6701384bd27SBrian Somers #ifndef RELEASE_CRUNCH 6711384bd27SBrian Somers else 6721384bd27SBrian Somers log_Printf(LogERROR, "Warning: Can't create %s: %s\n", 6731384bd27SBrian Somers pidfile, strerror(errno)); 6741384bd27SBrian Somers #endif 6751384bd27SBrian Somers } 6761384bd27SBrian Somers 6771384bd27SBrian Somers static void 6781384bd27SBrian Somers bundle_UnlockTun(struct bundle *bundle) 6791384bd27SBrian Somers { 6801384bd27SBrian Somers char pidfile[MAXPATHLEN]; 6811384bd27SBrian Somers 6821384bd27SBrian Somers snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit); 6831384bd27SBrian Somers ID0unlink(pidfile); 6841384bd27SBrian Somers } 6857a6f8720SBrian Somers 6867a6f8720SBrian Somers struct bundle * 6878e7b8599SBrian Somers bundle_Create(const char *prefix, int type, const char **argv) 6887a6f8720SBrian Somers { 6897a6f8720SBrian Somers int s, enoentcount, err; 6907a6f8720SBrian Somers struct ifreq ifrq; 6917a6f8720SBrian Somers static struct bundle bundle; /* there can be only one */ 6927a6f8720SBrian Somers 693faefde08SBrian Somers if (bundle.ifp.Name != NULL) { /* Already allocated ! */ 694dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_Create: There's only one BUNDLE !\n"); 6957a6f8720SBrian Somers return NULL; 6967a6f8720SBrian Somers } 6977a6f8720SBrian Somers 6987a6f8720SBrian Somers err = ENOENT; 6997a6f8720SBrian Somers enoentcount = 0; 700107d62e7SBrian Somers for (bundle.unit = 0; ; bundle.unit++) { 701faefde08SBrian Somers snprintf(bundle.dev.Name, sizeof bundle.dev.Name, "%s%d", 702faefde08SBrian Somers prefix, bundle.unit); 703faefde08SBrian Somers bundle.dev.fd = ID0open(bundle.dev.Name, O_RDWR); 704faefde08SBrian Somers if (bundle.dev.fd >= 0) 7057a6f8720SBrian Somers break; 706107d62e7SBrian Somers else if (errno == ENXIO) { 7077a6f8720SBrian Somers err = errno; 708107d62e7SBrian Somers break; 7097a6f8720SBrian Somers } else if (errno == ENOENT) { 7107a6f8720SBrian Somers if (++enoentcount > 2) 711107d62e7SBrian Somers break; 7127a6f8720SBrian Somers } else 7137a6f8720SBrian Somers err = errno; 7147a6f8720SBrian Somers } 7157a6f8720SBrian Somers 716faefde08SBrian Somers if (bundle.dev.fd < 0) { 717dd7e2610SBrian Somers log_Printf(LogWARN, "No available tunnel devices found (%s).\n", 71885b542cfSBrian Somers strerror(err)); 7197a6f8720SBrian Somers return NULL; 7207a6f8720SBrian Somers } 7217a6f8720SBrian Somers 722dd7e2610SBrian Somers log_SetTun(bundle.unit); 7238e7b8599SBrian Somers bundle.argv = argv; 7247a6f8720SBrian Somers 7257a6f8720SBrian Somers s = socket(AF_INET, SOCK_DGRAM, 0); 7267a6f8720SBrian Somers if (s < 0) { 727dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_Create: socket(): %s\n", strerror(errno)); 728faefde08SBrian Somers close(bundle.dev.fd); 7297a6f8720SBrian Somers return NULL; 7307a6f8720SBrian Somers } 7317a6f8720SBrian Somers 732faefde08SBrian Somers bundle.ifp.Name = strrchr(bundle.dev.Name, '/'); 733faefde08SBrian Somers if (bundle.ifp.Name == NULL) 734faefde08SBrian Somers bundle.ifp.Name = bundle.dev.Name; 7357a6f8720SBrian Somers else 736faefde08SBrian Somers bundle.ifp.Name++; 7377a6f8720SBrian Somers 7387a6f8720SBrian Somers /* 7397a6f8720SBrian Somers * Now, bring up the interface. 7407a6f8720SBrian Somers */ 7417a6f8720SBrian Somers memset(&ifrq, '\0', sizeof ifrq); 742faefde08SBrian Somers strncpy(ifrq.ifr_name, bundle.ifp.Name, sizeof ifrq.ifr_name - 1); 7437a6f8720SBrian Somers ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0'; 7447a6f8720SBrian Somers if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) { 745dd7e2610SBrian Somers log_Printf(LogERROR, "OpenTunnel: ioctl(SIOCGIFFLAGS): %s\n", 7467a6f8720SBrian Somers strerror(errno)); 7477a6f8720SBrian Somers close(s); 748faefde08SBrian Somers close(bundle.dev.fd); 749faefde08SBrian Somers bundle.ifp.Name = NULL; 7507a6f8720SBrian Somers return NULL; 7517a6f8720SBrian Somers } 7527a6f8720SBrian Somers ifrq.ifr_flags |= IFF_UP; 7537a6f8720SBrian Somers if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) { 754dd7e2610SBrian Somers log_Printf(LogERROR, "OpenTunnel: ioctl(SIOCSIFFLAGS): %s\n", 7557a6f8720SBrian Somers strerror(errno)); 7567a6f8720SBrian Somers close(s); 757faefde08SBrian Somers close(bundle.dev.fd); 758faefde08SBrian Somers bundle.ifp.Name = NULL; 7597a6f8720SBrian Somers return NULL; 7607a6f8720SBrian Somers } 7617a6f8720SBrian Somers 7627a6f8720SBrian Somers close(s); 7637a6f8720SBrian Somers 764faefde08SBrian Somers if ((bundle.ifp.Index = GetIfIndex(bundle.ifp.Name)) < 0) { 765faefde08SBrian Somers log_Printf(LogERROR, "OpenTunnel: Can't find interface index.\n"); 766faefde08SBrian Somers close(bundle.dev.fd); 767faefde08SBrian Somers bundle.ifp.Name = NULL; 7687a6f8720SBrian Somers return NULL; 7697a6f8720SBrian Somers } 770faefde08SBrian Somers log_Printf(LogPHASE, "Using interface: %s\n", bundle.ifp.Name); 7717a6f8720SBrian Somers 772faefde08SBrian Somers bundle.ifp.Speed = 0; 7737a6f8720SBrian Somers 774820de6ebSBrian Somers bundle.routing_seq = 0; 775a0cbd833SBrian Somers bundle.phase = PHASE_DEAD; 776a0cbd833SBrian Somers bundle.CleaningUp = 0; 7777a6f8720SBrian Somers 7786d666775SBrian Somers bundle.fsm.LayerStart = bundle_LayerStart; 7796f384573SBrian Somers bundle.fsm.LayerUp = bundle_LayerUp; 7806d666775SBrian Somers bundle.fsm.LayerDown = bundle_LayerDown; 7816d666775SBrian Somers bundle.fsm.LayerFinish = bundle_LayerFinish; 7826d666775SBrian Somers bundle.fsm.object = &bundle; 7837a6f8720SBrian Somers 784ab886ad0SBrian Somers bundle.cfg.idle_timeout = NCP_IDLE_TIMEOUT; 7851342caedSBrian Somers *bundle.cfg.auth.name = '\0'; 7861342caedSBrian Somers *bundle.cfg.auth.key = '\0'; 787610b185fSBrian Somers bundle.cfg.opt = OPT_SROUTES | OPT_IDCHECK | OPT_LOOPBACK | 788610b185fSBrian Somers OPT_THROUGHPUT | OPT_UTMP; 78949052c95SBrian Somers *bundle.cfg.label = '\0'; 79049052c95SBrian Somers bundle.cfg.mtu = DEF_MTU; 79104eaa58cSBrian Somers bundle.cfg.autoload.max.packets = 0; 79204eaa58cSBrian Somers bundle.cfg.autoload.max.timeout = 0; 79304eaa58cSBrian Somers bundle.cfg.autoload.min.packets = 0; 79404eaa58cSBrian Somers bundle.cfg.autoload.min.timeout = 0; 795565e35e5SBrian Somers bundle.phys_type = type; 796ab886ad0SBrian Somers 7976f384573SBrian Somers bundle.links = datalink_Create("deflink", &bundle, type); 7983006ec67SBrian Somers if (bundle.links == NULL) { 799dd7e2610SBrian Somers log_Printf(LogERROR, "Cannot create data link: %s\n", strerror(errno)); 800faefde08SBrian Somers close(bundle.dev.fd); 801faefde08SBrian Somers bundle.ifp.Name = NULL; 8022289f246SBrian Somers return NULL; 8032289f246SBrian Somers } 8042289f246SBrian Somers 8052f786681SBrian Somers bundle.desc.type = BUNDLE_DESCRIPTOR; 8062f786681SBrian Somers bundle.desc.UpdateSet = bundle_UpdateSet; 8072f786681SBrian Somers bundle.desc.IsSet = bundle_IsSet; 8082f786681SBrian Somers bundle.desc.Read = bundle_DescriptorRead; 8092f786681SBrian Somers bundle.desc.Write = bundle_DescriptorWrite; 8102f786681SBrian Somers 81149052c95SBrian Somers mp_Init(&bundle.ncp.mp, &bundle); 81249052c95SBrian Somers 81349052c95SBrian Somers /* Send over the first physical link by default */ 8145828db6dSBrian Somers ipcp_Init(&bundle.ncp.ipcp, &bundle, &bundle.links->physical->link, 8155828db6dSBrian Somers &bundle.fsm); 8166d666775SBrian Somers 8175ca5389aSBrian Somers memset(&bundle.filter, '\0', sizeof bundle.filter); 8185ca5389aSBrian Somers bundle.filter.in.fragok = bundle.filter.in.logok = 1; 8195ca5389aSBrian Somers bundle.filter.in.name = "IN"; 8205ca5389aSBrian Somers bundle.filter.out.fragok = bundle.filter.out.logok = 1; 8215ca5389aSBrian Somers bundle.filter.out.name = "OUT"; 8225ca5389aSBrian Somers bundle.filter.dial.name = "DIAL"; 8238390b576SBrian Somers bundle.filter.dial.logok = 1; 8245ca5389aSBrian Somers bundle.filter.alive.name = "ALIVE"; 8255ca5389aSBrian Somers bundle.filter.alive.logok = 1; 82693ee0ff2SBrian Somers memset(&bundle.idle.timer, '\0', sizeof bundle.idle.timer); 82793ee0ff2SBrian Somers bundle.idle.done = 0; 8285cf4388bSBrian Somers bundle.notify.fd = -1; 82904eaa58cSBrian Somers memset(&bundle.autoload.timer, '\0', sizeof bundle.autoload.timer); 83004eaa58cSBrian Somers bundle.autoload.done = 0; 83104eaa58cSBrian Somers bundle.autoload.running = 0; 83293ee0ff2SBrian Somers 8336d666775SBrian Somers /* Clean out any leftover crud */ 8346d666775SBrian Somers bundle_CleanInterface(&bundle); 8356d666775SBrian Somers 8361384bd27SBrian Somers bundle_LockTun(&bundle); 8371384bd27SBrian Somers 8387a6f8720SBrian Somers return &bundle; 8397a6f8720SBrian Somers } 8407a6f8720SBrian Somers 84168a0f0ccSBrian Somers static void 84268a0f0ccSBrian Somers bundle_DownInterface(struct bundle *bundle) 84368a0f0ccSBrian Somers { 84468a0f0ccSBrian Somers struct ifreq ifrq; 84568a0f0ccSBrian Somers int s; 84668a0f0ccSBrian Somers 847dd7e2610SBrian Somers route_IfDelete(bundle, 1); 84868a0f0ccSBrian Somers 84968a0f0ccSBrian Somers s = ID0socket(AF_INET, SOCK_DGRAM, 0); 85068a0f0ccSBrian Somers if (s < 0) { 851dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_DownInterface: socket: %s\n", strerror(errno)); 85268a0f0ccSBrian Somers return; 85368a0f0ccSBrian Somers } 85468a0f0ccSBrian Somers 85568a0f0ccSBrian Somers memset(&ifrq, '\0', sizeof ifrq); 856faefde08SBrian Somers strncpy(ifrq.ifr_name, bundle->ifp.Name, sizeof ifrq.ifr_name - 1); 85768a0f0ccSBrian Somers ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0'; 85868a0f0ccSBrian Somers if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) { 859dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_DownInterface: ioctl(SIOCGIFFLAGS): %s\n", 86068a0f0ccSBrian Somers strerror(errno)); 86168a0f0ccSBrian Somers close(s); 86268a0f0ccSBrian Somers return; 86368a0f0ccSBrian Somers } 86468a0f0ccSBrian Somers ifrq.ifr_flags &= ~IFF_UP; 86568a0f0ccSBrian Somers if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) { 866dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_DownInterface: ioctl(SIOCSIFFLAGS): %s\n", 86768a0f0ccSBrian Somers strerror(errno)); 86868a0f0ccSBrian Somers close(s); 86968a0f0ccSBrian Somers return; 87068a0f0ccSBrian Somers } 87168a0f0ccSBrian Somers close(s); 87268a0f0ccSBrian Somers } 87368a0f0ccSBrian Somers 87468a0f0ccSBrian Somers void 87568a0f0ccSBrian Somers bundle_Destroy(struct bundle *bundle) 87668a0f0ccSBrian Somers { 8773006ec67SBrian Somers struct datalink *dl; 878b6217683SBrian Somers 879ea722969SBrian Somers /* 88004eaa58cSBrian Somers * Clean up the interface. We don't need to timer_Stop()s, mp_Down(), 881ea722969SBrian Somers * ipcp_CleanInterface() and bundle_DownInterface() unless we're getting 882ea722969SBrian Somers * out under exceptional conditions such as a descriptor exception. 883ea722969SBrian Somers */ 88404eaa58cSBrian Somers timer_Stop(&bundle->idle.timer); 88504eaa58cSBrian Somers timer_Stop(&bundle->autoload.timer); 88666f634b6SBrian Somers mp_Down(&bundle->ncp.mp); 887dd7e2610SBrian Somers ipcp_CleanInterface(&bundle->ncp.ipcp); 88868a0f0ccSBrian Somers bundle_DownInterface(bundle); 8893006ec67SBrian Somers 890ea722969SBrian Somers /* Again, these are all DATALINK_CLOSED unless we're abending */ 8913006ec67SBrian Somers dl = bundle->links; 8923006ec67SBrian Somers while (dl) 8933006ec67SBrian Somers dl = datalink_Destroy(dl); 8943006ec67SBrian Somers 8951384bd27SBrian Somers close(bundle->dev.fd); 8961384bd27SBrian Somers bundle_UnlockTun(bundle); 8971384bd27SBrian Somers 898ea722969SBrian Somers /* In case we never made PHASE_NETWORK */ 8995cf4388bSBrian Somers bundle_Notify(bundle, EX_ERRDEAD); 900b6217683SBrian Somers 901faefde08SBrian Somers bundle->ifp.Name = NULL; 90268a0f0ccSBrian Somers } 90368a0f0ccSBrian Somers 9047a6f8720SBrian Somers struct rtmsg { 9057a6f8720SBrian Somers struct rt_msghdr m_rtm; 9067a6f8720SBrian Somers char m_space[64]; 9077a6f8720SBrian Somers }; 9087a6f8720SBrian Somers 909610b185fSBrian Somers int 910820de6ebSBrian Somers bundle_SetRoute(struct bundle *bundle, int cmd, struct in_addr dst, 9117a6f8720SBrian Somers struct in_addr gateway, struct in_addr mask, int bang) 9127a6f8720SBrian Somers { 9137a6f8720SBrian Somers struct rtmsg rtmes; 9147a6f8720SBrian Somers int s, nb, wb; 9157a6f8720SBrian Somers char *cp; 9167a6f8720SBrian Somers const char *cmdstr; 9177a6f8720SBrian Somers struct sockaddr_in rtdata; 918610b185fSBrian Somers int result = 1; 9197a6f8720SBrian Somers 9207a6f8720SBrian Somers if (bang) 9217a6f8720SBrian Somers cmdstr = (cmd == RTM_ADD ? "Add!" : "Delete!"); 9227a6f8720SBrian Somers else 9237a6f8720SBrian Somers cmdstr = (cmd == RTM_ADD ? "Add" : "Delete"); 9247a6f8720SBrian Somers s = ID0socket(PF_ROUTE, SOCK_RAW, 0); 9257a6f8720SBrian Somers if (s < 0) { 926dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_SetRoute: socket(): %s\n", strerror(errno)); 927610b185fSBrian Somers return result; 9287a6f8720SBrian Somers } 9297a6f8720SBrian Somers memset(&rtmes, '\0', sizeof rtmes); 9307a6f8720SBrian Somers rtmes.m_rtm.rtm_version = RTM_VERSION; 9317a6f8720SBrian Somers rtmes.m_rtm.rtm_type = cmd; 9327a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs = RTA_DST; 933820de6ebSBrian Somers rtmes.m_rtm.rtm_seq = ++bundle->routing_seq; 9347a6f8720SBrian Somers rtmes.m_rtm.rtm_pid = getpid(); 9357a6f8720SBrian Somers rtmes.m_rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC; 9367a6f8720SBrian Somers 9377a6f8720SBrian Somers memset(&rtdata, '\0', sizeof rtdata); 93850e5c17dSBrian Somers rtdata.sin_len = sizeof rtdata; 9397a6f8720SBrian Somers rtdata.sin_family = AF_INET; 9407a6f8720SBrian Somers rtdata.sin_port = 0; 9417a6f8720SBrian Somers rtdata.sin_addr = dst; 9427a6f8720SBrian Somers 9437a6f8720SBrian Somers cp = rtmes.m_space; 94450e5c17dSBrian Somers memcpy(cp, &rtdata, rtdata.sin_len); 94550e5c17dSBrian Somers cp += rtdata.sin_len; 946e43ebac1SBrian Somers if (cmd == RTM_ADD) { 9477a6f8720SBrian Somers if (gateway.s_addr == INADDR_ANY) { 9487a6f8720SBrian Somers /* Add a route through the interface */ 9497a6f8720SBrian Somers struct sockaddr_dl dl; 9507a6f8720SBrian Somers const char *iname; 9517a6f8720SBrian Somers int ilen; 9527a6f8720SBrian Somers 953faefde08SBrian Somers iname = Index2Nam(bundle->ifp.Index); 9547a6f8720SBrian Somers ilen = strlen(iname); 9557a6f8720SBrian Somers dl.sdl_len = sizeof dl - sizeof dl.sdl_data + ilen; 9567a6f8720SBrian Somers dl.sdl_family = AF_LINK; 957faefde08SBrian Somers dl.sdl_index = bundle->ifp.Index; 9587a6f8720SBrian Somers dl.sdl_type = 0; 9597a6f8720SBrian Somers dl.sdl_nlen = ilen; 9607a6f8720SBrian Somers dl.sdl_alen = 0; 9617a6f8720SBrian Somers dl.sdl_slen = 0; 9627a6f8720SBrian Somers strncpy(dl.sdl_data, iname, sizeof dl.sdl_data); 9637a6f8720SBrian Somers memcpy(cp, &dl, dl.sdl_len); 9647a6f8720SBrian Somers cp += dl.sdl_len; 9657a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY; 9667a6f8720SBrian Somers } else { 9677a6f8720SBrian Somers rtdata.sin_addr = gateway; 96850e5c17dSBrian Somers memcpy(cp, &rtdata, rtdata.sin_len); 96950e5c17dSBrian Somers cp += rtdata.sin_len; 9707a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY; 9717a6f8720SBrian Somers } 972e43ebac1SBrian Somers } 9737a6f8720SBrian Somers 9747a6f8720SBrian Somers if (dst.s_addr == INADDR_ANY) 9757a6f8720SBrian Somers mask.s_addr = INADDR_ANY; 9767a6f8720SBrian Somers 9777a6f8720SBrian Somers if (cmd == RTM_ADD || dst.s_addr == INADDR_ANY) { 9787a6f8720SBrian Somers rtdata.sin_addr = mask; 97950e5c17dSBrian Somers memcpy(cp, &rtdata, rtdata.sin_len); 98050e5c17dSBrian Somers cp += rtdata.sin_len; 9817a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs |= RTA_NETMASK; 9827a6f8720SBrian Somers } 9837a6f8720SBrian Somers 9847a6f8720SBrian Somers nb = cp - (char *) &rtmes; 9857a6f8720SBrian Somers rtmes.m_rtm.rtm_msglen = nb; 9867a6f8720SBrian Somers wb = ID0write(s, &rtmes, nb); 9877a6f8720SBrian Somers if (wb < 0) { 988dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute failure:\n"); 989dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Cmd = %s\n", cmdstr); 990dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Dst = %s\n", inet_ntoa(dst)); 991dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Gateway = %s\n", inet_ntoa(gateway)); 992dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Mask = %s\n", inet_ntoa(mask)); 9937a6f8720SBrian Somers failed: 9947a6f8720SBrian Somers if (cmd == RTM_ADD && (rtmes.m_rtm.rtm_errno == EEXIST || 995e43ebac1SBrian Somers (rtmes.m_rtm.rtm_errno == 0 && errno == EEXIST))) { 996610b185fSBrian Somers if (!bang) { 997dd7e2610SBrian Somers log_Printf(LogWARN, "Add route failed: %s already exists\n", 9987a6f8720SBrian Somers inet_ntoa(dst)); 999610b185fSBrian Somers result = 0; /* Don't add to our dynamic list */ 1000610b185fSBrian Somers } else { 10017a6f8720SBrian Somers rtmes.m_rtm.rtm_type = cmd = RTM_CHANGE; 10027a6f8720SBrian Somers if ((wb = ID0write(s, &rtmes, nb)) < 0) 10037a6f8720SBrian Somers goto failed; 10047a6f8720SBrian Somers } 1005e43ebac1SBrian Somers } else if (cmd == RTM_DELETE && 10067a6f8720SBrian Somers (rtmes.m_rtm.rtm_errno == ESRCH || 10077a6f8720SBrian Somers (rtmes.m_rtm.rtm_errno == 0 && errno == ESRCH))) { 10087a6f8720SBrian Somers if (!bang) 1009dd7e2610SBrian Somers log_Printf(LogWARN, "Del route failed: %s: Non-existent\n", 10107a6f8720SBrian Somers inet_ntoa(dst)); 10117a6f8720SBrian Somers } else if (rtmes.m_rtm.rtm_errno == 0) 1012dd7e2610SBrian Somers log_Printf(LogWARN, "%s route failed: %s: errno: %s\n", cmdstr, 10137a6f8720SBrian Somers inet_ntoa(dst), strerror(errno)); 10147a6f8720SBrian Somers else 1015dd7e2610SBrian Somers log_Printf(LogWARN, "%s route failed: %s: %s\n", 10167a6f8720SBrian Somers cmdstr, inet_ntoa(dst), strerror(rtmes.m_rtm.rtm_errno)); 10177a6f8720SBrian Somers } 1018dd7e2610SBrian Somers log_Printf(LogDEBUG, "wrote %d: cmd = %s, dst = %x, gateway = %x\n", 1019fe3125a0SBrian Somers wb, cmdstr, (unsigned)dst.s_addr, (unsigned)gateway.s_addr); 10207a6f8720SBrian Somers close(s); 1021610b185fSBrian Somers 1022610b185fSBrian Somers return result; 10237a6f8720SBrian Somers } 102483d1af55SBrian Somers 102583d1af55SBrian Somers void 10263006ec67SBrian Somers bundle_LinkClosed(struct bundle *bundle, struct datalink *dl) 10273006ec67SBrian Somers { 10283006ec67SBrian Somers /* 10293006ec67SBrian Somers * Our datalink has closed. 1030ea722969SBrian Somers * CleanDatalinks() (called from DoLoop()) will remove closed 103181358fa3SBrian Somers * BACKGROUND and DIRECT links. 10323b0f8d2eSBrian Somers * If it's the last data link, enter phase DEAD. 1033ea722969SBrian Somers * 1034ea722969SBrian Somers * NOTE: dl may not be in our list (bundle_SendDatalink()) ! 10353006ec67SBrian Somers */ 10365b8b8060SBrian Somers 10373b0f8d2eSBrian Somers struct datalink *odl; 10383b0f8d2eSBrian Somers int other_links; 10395b8b8060SBrian Somers 10403b0f8d2eSBrian Somers other_links = 0; 10413b0f8d2eSBrian Somers for (odl = bundle->links; odl; odl = odl->next) 10423b0f8d2eSBrian Somers if (odl != dl && odl->state != DATALINK_CLOSED) 10433b0f8d2eSBrian Somers other_links++; 10443b0f8d2eSBrian Somers 10453b0f8d2eSBrian Somers if (!other_links) { 104681358fa3SBrian Somers if (dl->physical->type != PHYS_AUTO) /* Not in -auto mode */ 104703704096SBrian Somers bundle_DownInterface(bundle); 104826afeaa2SBrian Somers if (bundle->ncp.ipcp.fsm.state > ST_CLOSED || 104926afeaa2SBrian Somers bundle->ncp.ipcp.fsm.state == ST_STARTING) { 1050dd7e2610SBrian Somers fsm_Down(&bundle->ncp.ipcp.fsm); 1051dd7e2610SBrian Somers fsm_Close(&bundle->ncp.ipcp.fsm); /* ST_INITIAL please */ 105226afeaa2SBrian Somers } 10535563ebdeSBrian Somers bundle_NewPhase(bundle, PHASE_DEAD); 10540f2f3eb3SBrian Somers bundle_StopIdleTimer(bundle); 105504eaa58cSBrian Somers bundle_StopAutoLoadTimer(bundle); 105604eaa58cSBrian Somers bundle->autoload.running = 0; 105704eaa58cSBrian Somers } else 105804eaa58cSBrian Somers bundle->autoload.running = 1; 1059455aabc3SBrian Somers } 1060455aabc3SBrian Somers 1061455aabc3SBrian Somers void 1062565e35e5SBrian Somers bundle_Open(struct bundle *bundle, const char *name, int mask) 10633006ec67SBrian Somers { 10643006ec67SBrian Somers /* 10653006ec67SBrian Somers * Please open the given datalink, or all if name == NULL 10663006ec67SBrian Somers */ 10673006ec67SBrian Somers struct datalink *dl; 10683006ec67SBrian Somers 106904eaa58cSBrian Somers timer_Stop(&bundle->autoload.timer); 10703006ec67SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 10713006ec67SBrian Somers if (name == NULL || !strcasecmp(dl->name, name)) { 107204eaa58cSBrian Somers if (dl->state == DATALINK_CLOSED && (mask & dl->physical->type)) { 1073565e35e5SBrian Somers datalink_Up(dl, 1, 1); 107481358fa3SBrian Somers if (mask == PHYS_AUTO) 107581358fa3SBrian Somers /* Only one AUTO link at a time (see the AutoLoad timer) */ 107604eaa58cSBrian Somers break; 107704eaa58cSBrian Somers } 10783006ec67SBrian Somers if (name != NULL) 10793006ec67SBrian Somers break; 10803006ec67SBrian Somers } 10813006ec67SBrian Somers } 10823006ec67SBrian Somers 10833006ec67SBrian Somers struct datalink * 10843006ec67SBrian Somers bundle2datalink(struct bundle *bundle, const char *name) 10853006ec67SBrian Somers { 10863006ec67SBrian Somers struct datalink *dl; 10873006ec67SBrian Somers 10883006ec67SBrian Somers if (name != NULL) { 10893006ec67SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 10903006ec67SBrian Somers if (!strcasecmp(dl->name, name)) 10913006ec67SBrian Somers return dl; 10923006ec67SBrian Somers } else if (bundle->links && !bundle->links->next) 10933006ec67SBrian Somers return bundle->links; 10943006ec67SBrian Somers 10953006ec67SBrian Somers return NULL; 10963006ec67SBrian Somers } 10973006ec67SBrian Somers 10983006ec67SBrian Somers int 10993006ec67SBrian Somers bundle_FillQueues(struct bundle *bundle) 11003006ec67SBrian Somers { 11013b0f8d2eSBrian Somers int total; 11023b0f8d2eSBrian Somers 11031bc9b5baSBrian Somers if (bundle->ncp.mp.active) 11043b0f8d2eSBrian Somers total = mp_FillQueues(bundle); 11051bc9b5baSBrian Somers else { 11061bc9b5baSBrian Somers struct datalink *dl; 11071bc9b5baSBrian Somers int add; 11081bc9b5baSBrian Somers 11091bc9b5baSBrian Somers for (total = 0, dl = bundle->links; dl; dl = dl->next) 11101bc9b5baSBrian Somers if (dl->state == DATALINK_OPEN) { 11111bc9b5baSBrian Somers add = link_QueueLen(&dl->physical->link); 11121bc9b5baSBrian Somers if (add == 0 && dl->physical->out == NULL) 11131bc9b5baSBrian Somers add = ip_FlushPacket(&dl->physical->link, bundle); 11141bc9b5baSBrian Somers total += add; 11151bc9b5baSBrian Somers } 11163006ec67SBrian Somers } 11173006ec67SBrian Somers 11183b0f8d2eSBrian Somers return total + ip_QueueLen(); 11193006ec67SBrian Somers } 1120aef795ccSBrian Somers 1121aef795ccSBrian Somers int 1122aef795ccSBrian Somers bundle_ShowLinks(struct cmdargs const *arg) 1123aef795ccSBrian Somers { 1124aef795ccSBrian Somers struct datalink *dl; 1125aef795ccSBrian Somers 11269c53a7b1SBrian Somers for (dl = arg->bundle->links; dl; dl = dl->next) { 1127d4156d00SBrian Somers prompt_Printf(arg->prompt, "Name: %s [%s, %s]", 1128d4156d00SBrian Somers dl->name, mode2Nam(dl->physical->type), datalink_State(dl)); 1129eeab6bf5SBrian Somers if (dl->physical->link.throughput.rolling && dl->state == DATALINK_OPEN) 1130d4156d00SBrian Somers prompt_Printf(arg->prompt, " weight %d, %d bytes/sec", 1131eeab6bf5SBrian Somers dl->mp.weight, 11329c53a7b1SBrian Somers dl->physical->link.throughput.OctetsPerSecond); 11339c53a7b1SBrian Somers prompt_Printf(arg->prompt, "\n"); 11349c53a7b1SBrian Somers } 1135aef795ccSBrian Somers 1136aef795ccSBrian Somers return 0; 1137aef795ccSBrian Somers } 1138ab886ad0SBrian Somers 11391342caedSBrian Somers static const char * 11401342caedSBrian Somers optval(struct bundle *bundle, int bit) 11411342caedSBrian Somers { 11421342caedSBrian Somers return (bundle->cfg.opt & bit) ? "enabled" : "disabled"; 11431342caedSBrian Somers } 11441342caedSBrian Somers 1145c08717dfSBrian Somers int 1146c08717dfSBrian Somers bundle_ShowStatus(struct cmdargs const *arg) 1147c08717dfSBrian Somers { 1148c08717dfSBrian Somers int remaining; 1149c08717dfSBrian Somers 1150c08717dfSBrian Somers prompt_Printf(arg->prompt, "Phase %s\n", bundle_PhaseName(arg->bundle)); 1151faefde08SBrian Somers prompt_Printf(arg->prompt, " Device: %s\n", arg->bundle->dev.Name); 1152faefde08SBrian Somers prompt_Printf(arg->prompt, " Interface: %s @ %lubps\n", 1153faefde08SBrian Somers arg->bundle->ifp.Name, arg->bundle->ifp.Speed); 1154c08717dfSBrian Somers 1155c08717dfSBrian Somers prompt_Printf(arg->prompt, "\nDefaults:\n"); 1156643f4904SBrian Somers prompt_Printf(arg->prompt, " Label: %s\n", arg->bundle->cfg.label); 1157610b185fSBrian Somers prompt_Printf(arg->prompt, " Auth name: %s\n", 1158610b185fSBrian Somers arg->bundle->cfg.auth.name); 115904eaa58cSBrian Somers prompt_Printf(arg->prompt, " Auto Load: Up after %ds of >= %d packets\n", 116004eaa58cSBrian Somers arg->bundle->cfg.autoload.max.timeout, 116104eaa58cSBrian Somers arg->bundle->cfg.autoload.max.packets); 116204eaa58cSBrian Somers prompt_Printf(arg->prompt, " Down after %ds of <= %d" 116304eaa58cSBrian Somers " packets\n", arg->bundle->cfg.autoload.min.timeout, 116404eaa58cSBrian Somers arg->bundle->cfg.autoload.min.packets); 116504eaa58cSBrian Somers if (arg->bundle->autoload.timer.state == TIMER_RUNNING) 116604eaa58cSBrian Somers prompt_Printf(arg->prompt, " %ds remaining 'till " 116704eaa58cSBrian Somers "a link comes %s\n", 116804eaa58cSBrian Somers bundle_RemainingAutoLoadTime(arg->bundle), 116904eaa58cSBrian Somers arg->bundle->autoload.comingup ? "up" : "down"); 117004eaa58cSBrian Somers else 117104eaa58cSBrian Somers prompt_Printf(arg->prompt, " %srunning with %d" 117204eaa58cSBrian Somers " packets queued\n", arg->bundle->autoload.running ? 117304eaa58cSBrian Somers "" : "not ", ip_QueueLen()); 117404eaa58cSBrian Somers 1175c08717dfSBrian Somers prompt_Printf(arg->prompt, " Idle Timer: "); 1176c08717dfSBrian Somers if (arg->bundle->cfg.idle_timeout) { 1177c08717dfSBrian Somers prompt_Printf(arg->prompt, "%ds", arg->bundle->cfg.idle_timeout); 1178c08717dfSBrian Somers remaining = bundle_RemainingIdleTime(arg->bundle); 1179c08717dfSBrian Somers if (remaining != -1) 1180c08717dfSBrian Somers prompt_Printf(arg->prompt, " (%ds remaining)", remaining); 1181c08717dfSBrian Somers prompt_Printf(arg->prompt, "\n"); 1182c08717dfSBrian Somers } else 1183c08717dfSBrian Somers prompt_Printf(arg->prompt, "disabled\n"); 1184ce828a6eSBrian Somers prompt_Printf(arg->prompt, " MTU: "); 1185ce828a6eSBrian Somers if (arg->bundle->cfg.mtu) 1186ce828a6eSBrian Somers prompt_Printf(arg->prompt, "%d\n", arg->bundle->cfg.mtu); 1187ce828a6eSBrian Somers else 1188ce828a6eSBrian Somers prompt_Printf(arg->prompt, "unspecified\n"); 1189c08717dfSBrian Somers 1190610b185fSBrian Somers prompt_Printf(arg->prompt, " Sticky Routes: %s\n", 1191610b185fSBrian Somers optval(arg->bundle, OPT_SROUTES)); 11921342caedSBrian Somers prompt_Printf(arg->prompt, " ID check: %s\n", 11931342caedSBrian Somers optval(arg->bundle, OPT_IDCHECK)); 11941342caedSBrian Somers prompt_Printf(arg->prompt, " Loopback: %s\n", 11951342caedSBrian Somers optval(arg->bundle, OPT_LOOPBACK)); 11961342caedSBrian Somers prompt_Printf(arg->prompt, " PasswdAuth: %s\n", 11971342caedSBrian Somers optval(arg->bundle, OPT_PASSWDAUTH)); 11981342caedSBrian Somers prompt_Printf(arg->prompt, " Proxy: %s\n", 11991342caedSBrian Somers optval(arg->bundle, OPT_PROXY)); 12001342caedSBrian Somers prompt_Printf(arg->prompt, " Throughput: %s\n", 12011342caedSBrian Somers optval(arg->bundle, OPT_THROUGHPUT)); 1202610b185fSBrian Somers prompt_Printf(arg->prompt, " Utmp Logging: %s\n", 12031342caedSBrian Somers optval(arg->bundle, OPT_UTMP)); 12041342caedSBrian Somers 1205c08717dfSBrian Somers return 0; 1206c08717dfSBrian Somers } 1207c08717dfSBrian Somers 1208ab886ad0SBrian Somers static void 1209ab886ad0SBrian Somers bundle_IdleTimeout(void *v) 1210ab886ad0SBrian Somers { 1211ab886ad0SBrian Somers struct bundle *bundle = (struct bundle *)v; 1212ab886ad0SBrian Somers 1213dd7e2610SBrian Somers log_Printf(LogPHASE, "Idle timer expired.\n"); 121404eaa58cSBrian Somers bundle_StopIdleTimer(bundle); 1215ab886ad0SBrian Somers bundle_Close(bundle, NULL, 1); 1216ab886ad0SBrian Somers } 1217ab886ad0SBrian Somers 1218ab886ad0SBrian Somers /* 1219ab886ad0SBrian Somers * Start Idle timer. If timeout is reached, we call bundle_Close() to 1220ab886ad0SBrian Somers * close LCP and link. 1221ab886ad0SBrian Somers */ 1222ab886ad0SBrian Somers void 1223ab886ad0SBrian Somers bundle_StartIdleTimer(struct bundle *bundle) 1224ab886ad0SBrian Somers { 1225dd7e2610SBrian Somers timer_Stop(&bundle->idle.timer); 122681358fa3SBrian Somers if ((bundle->phys_type & (PHYS_DEDICATED|PHYS_DDIAL)) != bundle->phys_type && 122704eaa58cSBrian Somers bundle->cfg.idle_timeout) { 122893ee0ff2SBrian Somers bundle->idle.timer.func = bundle_IdleTimeout; 12293b0f8d2eSBrian Somers bundle->idle.timer.name = "idle"; 123093ee0ff2SBrian Somers bundle->idle.timer.load = bundle->cfg.idle_timeout * SECTICKS; 123193ee0ff2SBrian Somers bundle->idle.timer.arg = bundle; 1232dd7e2610SBrian Somers timer_Start(&bundle->idle.timer); 123393ee0ff2SBrian Somers bundle->idle.done = time(NULL) + bundle->cfg.idle_timeout; 1234ab886ad0SBrian Somers } 1235ab886ad0SBrian Somers } 1236ab886ad0SBrian Somers 1237ab886ad0SBrian Somers void 1238ab886ad0SBrian Somers bundle_SetIdleTimer(struct bundle *bundle, int value) 1239ab886ad0SBrian Somers { 1240ab886ad0SBrian Somers bundle->cfg.idle_timeout = value; 1241ab886ad0SBrian Somers if (bundle_LinkIsUp(bundle)) 1242ab886ad0SBrian Somers bundle_StartIdleTimer(bundle); 1243ab886ad0SBrian Somers } 1244ab886ad0SBrian Somers 1245ab886ad0SBrian Somers void 1246ab886ad0SBrian Somers bundle_StopIdleTimer(struct bundle *bundle) 1247ab886ad0SBrian Somers { 1248dd7e2610SBrian Somers timer_Stop(&bundle->idle.timer); 12494a632c80SBrian Somers bundle->idle.done = 0; 1250ab886ad0SBrian Somers } 1251ab886ad0SBrian Somers 125204eaa58cSBrian Somers static int 1253ab886ad0SBrian Somers bundle_RemainingIdleTime(struct bundle *bundle) 1254ab886ad0SBrian Somers { 125593ee0ff2SBrian Somers if (bundle->idle.done) 125693ee0ff2SBrian Somers return bundle->idle.done - time(NULL); 1257ab886ad0SBrian Somers return -1; 1258ab886ad0SBrian Somers } 12593b0f8d2eSBrian Somers 12603b0f8d2eSBrian Somers int 12613b0f8d2eSBrian Somers bundle_IsDead(struct bundle *bundle) 12623b0f8d2eSBrian Somers { 12633b0f8d2eSBrian Somers return !bundle->links || (bundle->phase == PHASE_DEAD && bundle->CleaningUp); 12643b0f8d2eSBrian Somers } 1265b6217683SBrian Somers 1266565e35e5SBrian Somers static void 126704eaa58cSBrian Somers bundle_LinkAdded(struct bundle *bundle, struct datalink *dl) 126804eaa58cSBrian Somers { 126904eaa58cSBrian Somers bundle->phys_type |= dl->physical->type; 127081358fa3SBrian Somers if (dl->physical->type == PHYS_AUTO && 127104eaa58cSBrian Somers bundle->autoload.timer.state == TIMER_STOPPED && 127204eaa58cSBrian Somers bundle->phase == PHASE_NETWORK) 127304eaa58cSBrian Somers bundle->autoload.running = 1; 127404eaa58cSBrian Somers } 127504eaa58cSBrian Somers 127604eaa58cSBrian Somers static void 127704eaa58cSBrian Somers bundle_LinksRemoved(struct bundle *bundle) 1278565e35e5SBrian Somers { 1279565e35e5SBrian Somers struct datalink *dl; 1280565e35e5SBrian Somers 1281565e35e5SBrian Somers bundle->phys_type = 0; 1282565e35e5SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 128304eaa58cSBrian Somers bundle_LinkAdded(bundle, dl); 128404eaa58cSBrian Somers 128581358fa3SBrian Somers if ((bundle->phys_type & (PHYS_DEDICATED|PHYS_DDIAL)) == bundle->phys_type) 128604eaa58cSBrian Somers timer_Stop(&bundle->idle.timer); 1287565e35e5SBrian Somers } 1288565e35e5SBrian Somers 128904eaa58cSBrian Somers static struct datalink * 129004eaa58cSBrian Somers bundle_DatalinkLinkout(struct bundle *bundle, struct datalink *dl) 1291cd7bd93aSBrian Somers { 1292cd7bd93aSBrian Somers struct datalink **dlp; 1293cd7bd93aSBrian Somers 1294cd7bd93aSBrian Somers for (dlp = &bundle->links; *dlp; dlp = &(*dlp)->next) 1295cd7bd93aSBrian Somers if (*dlp == dl) { 129604eaa58cSBrian Somers *dlp = dl->next; 129704eaa58cSBrian Somers dl->next = NULL; 129804eaa58cSBrian Somers bundle_LinksRemoved(bundle); 129904eaa58cSBrian Somers return dl; 1300cd7bd93aSBrian Somers } 130104eaa58cSBrian Somers 130204eaa58cSBrian Somers return NULL; 130304eaa58cSBrian Somers } 130404eaa58cSBrian Somers 130504eaa58cSBrian Somers static void 130604eaa58cSBrian Somers bundle_DatalinkLinkin(struct bundle *bundle, struct datalink *dl) 130704eaa58cSBrian Somers { 130804eaa58cSBrian Somers struct datalink **dlp = &bundle->links; 130904eaa58cSBrian Somers 131004eaa58cSBrian Somers while (*dlp) 131104eaa58cSBrian Somers dlp = &(*dlp)->next; 131204eaa58cSBrian Somers 131304eaa58cSBrian Somers *dlp = dl; 131404eaa58cSBrian Somers dl->next = NULL; 131504eaa58cSBrian Somers 131604eaa58cSBrian Somers bundle_LinkAdded(bundle, dl); 1317565e35e5SBrian Somers } 1318565e35e5SBrian Somers 1319565e35e5SBrian Somers void 1320565e35e5SBrian Somers bundle_CleanDatalinks(struct bundle *bundle) 1321565e35e5SBrian Somers { 1322565e35e5SBrian Somers struct datalink **dlp = &bundle->links; 132304eaa58cSBrian Somers int found = 0; 1324565e35e5SBrian Somers 1325565e35e5SBrian Somers while (*dlp) 1326565e35e5SBrian Somers if ((*dlp)->state == DATALINK_CLOSED && 132781358fa3SBrian Somers (*dlp)->physical->type & (PHYS_DIRECT|PHYS_BACKGROUND)) { 1328565e35e5SBrian Somers *dlp = datalink_Destroy(*dlp); 132904eaa58cSBrian Somers found++; 133004eaa58cSBrian Somers } else 1331565e35e5SBrian Somers dlp = &(*dlp)->next; 133204eaa58cSBrian Somers 133304eaa58cSBrian Somers if (found) 133404eaa58cSBrian Somers bundle_LinksRemoved(bundle); 133504eaa58cSBrian Somers } 133604eaa58cSBrian Somers 133704eaa58cSBrian Somers int 133804eaa58cSBrian Somers bundle_DatalinkClone(struct bundle *bundle, struct datalink *dl, 133904eaa58cSBrian Somers const char *name) 134004eaa58cSBrian Somers { 134104eaa58cSBrian Somers if (bundle2datalink(bundle, name)) { 134204eaa58cSBrian Somers log_Printf(LogWARN, "Clone: %s: name already exists\n", name); 134304eaa58cSBrian Somers return 0; 134404eaa58cSBrian Somers } 134504eaa58cSBrian Somers 134604eaa58cSBrian Somers bundle_DatalinkLinkin(bundle, datalink_Clone(dl, name)); 134704eaa58cSBrian Somers return 1; 134804eaa58cSBrian Somers } 134904eaa58cSBrian Somers 135004eaa58cSBrian Somers void 135104eaa58cSBrian Somers bundle_DatalinkRemove(struct bundle *bundle, struct datalink *dl) 135204eaa58cSBrian Somers { 135304eaa58cSBrian Somers dl = bundle_DatalinkLinkout(bundle, dl); 135404eaa58cSBrian Somers if (dl) 135504eaa58cSBrian Somers datalink_Destroy(dl); 1356cd7bd93aSBrian Somers } 135749052c95SBrian Somers 135849052c95SBrian Somers void 135949052c95SBrian Somers bundle_SetLabel(struct bundle *bundle, const char *label) 136049052c95SBrian Somers { 136149052c95SBrian Somers if (label) 136249052c95SBrian Somers strncpy(bundle->cfg.label, label, sizeof bundle->cfg.label - 1); 136349052c95SBrian Somers else 136449052c95SBrian Somers *bundle->cfg.label = '\0'; 136549052c95SBrian Somers } 136649052c95SBrian Somers 136749052c95SBrian Somers const char * 136849052c95SBrian Somers bundle_GetLabel(struct bundle *bundle) 136949052c95SBrian Somers { 137049052c95SBrian Somers return *bundle->cfg.label ? bundle->cfg.label : NULL; 137149052c95SBrian Somers } 13721fa665f5SBrian Somers 13731fa665f5SBrian Somers void 137496c9bb21SBrian Somers bundle_ReceiveDatalink(struct bundle *bundle, int s, struct sockaddr_un *sun) 13751fa665f5SBrian Somers { 137696c9bb21SBrian Somers char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int)]; 137796c9bb21SBrian Somers struct cmsghdr *cmsg = (struct cmsghdr *)cmsgbuf; 137896c9bb21SBrian Somers struct msghdr msg; 137996c9bb21SBrian Somers struct iovec iov[SCATTER_SEGMENTS]; 1380b7c5748eSBrian Somers struct datalink *dl; 138196c9bb21SBrian Somers int niov, link_fd, expect, f; 138285fd273aSBrian Somers pid_t pid; 13836f384573SBrian Somers 1384dd7e2610SBrian Somers log_Printf(LogPHASE, "Receiving datalink\n"); 13856f384573SBrian Somers 138696c9bb21SBrian Somers /* Create our scatter/gather array */ 138796c9bb21SBrian Somers niov = 1; 138896c9bb21SBrian Somers iov[0].iov_len = strlen(Version) + 1; 138996c9bb21SBrian Somers iov[0].iov_base = (char *)malloc(iov[0].iov_len); 139085fd273aSBrian Somers if (datalink2iov(NULL, iov, &niov, sizeof iov / sizeof *iov, 0) == -1) { 139154cd8e13SBrian Somers close(s); 13926f384573SBrian Somers return; 139354cd8e13SBrian Somers } 13946f384573SBrian Somers 139585fd273aSBrian Somers pid = getpid(); 139685fd273aSBrian Somers write(s, &pid, sizeof pid); 139785fd273aSBrian Somers 139896c9bb21SBrian Somers for (f = expect = 0; f < niov; f++) 139996c9bb21SBrian Somers expect += iov[f].iov_len; 140096c9bb21SBrian Somers 140196c9bb21SBrian Somers /* Set up our message */ 140296c9bb21SBrian Somers cmsg->cmsg_len = sizeof cmsgbuf; 140396c9bb21SBrian Somers cmsg->cmsg_level = SOL_SOCKET; 140454cd8e13SBrian Somers cmsg->cmsg_type = 0; 140596c9bb21SBrian Somers 140696c9bb21SBrian Somers memset(&msg, '\0', sizeof msg); 140796c9bb21SBrian Somers msg.msg_name = (caddr_t)sun; 140896c9bb21SBrian Somers msg.msg_namelen = sizeof *sun; 140996c9bb21SBrian Somers msg.msg_iov = iov; 141096c9bb21SBrian Somers msg.msg_iovlen = niov; 141196c9bb21SBrian Somers msg.msg_control = cmsgbuf; 141296c9bb21SBrian Somers msg.msg_controllen = sizeof cmsgbuf; 141396c9bb21SBrian Somers 141496c9bb21SBrian Somers log_Printf(LogDEBUG, "Expecting %d scatter/gather bytes\n", expect); 141596c9bb21SBrian Somers f = expect + 100; 141696c9bb21SBrian Somers setsockopt(s, SOL_SOCKET, SO_RCVBUF, &f, sizeof f); 141796c9bb21SBrian Somers if ((f = recvmsg(s, &msg, MSG_WAITALL)) != expect) { 141896c9bb21SBrian Somers if (f == -1) 141996c9bb21SBrian Somers log_Printf(LogERROR, "Failed recvmsg: %s\n", strerror(errno)); 142096c9bb21SBrian Somers else 142196c9bb21SBrian Somers log_Printf(LogERROR, "Failed recvmsg: Got %d, not %d\n", f, expect); 142296c9bb21SBrian Somers while (niov--) 142396c9bb21SBrian Somers free(iov[niov].iov_base); 142454cd8e13SBrian Somers close(s); 142596c9bb21SBrian Somers return; 142696c9bb21SBrian Somers } 142796c9bb21SBrian Somers 1428c0d9a877SBrian Somers write(s, "!", 1); /* ACK */ 1429c0d9a877SBrian Somers 143054cd8e13SBrian Somers if (cmsg->cmsg_type == SCM_RIGHTS) { 143154cd8e13SBrian Somers /* We've successfully received an open file descriptor through our socket */ 143254cd8e13SBrian Somers log_Printf(LogDEBUG, "Receiving non-tty device\n"); 143354cd8e13SBrian Somers link_fd = *(int *)CMSG_DATA(cmsg); 143454cd8e13SBrian Somers } else { 143554cd8e13SBrian Somers /* It's a ``controlling'' tty device via CATPROG */ 143654cd8e13SBrian Somers log_Printf(LogDEBUG, "Receiving tty device\n"); 143754cd8e13SBrian Somers link_fd = dup(s); 143854cd8e13SBrian Somers fcntl(link_fd, F_SETFL, fcntl(link_fd, F_GETFL, 0) | O_NONBLOCK); 143954cd8e13SBrian Somers } 144054cd8e13SBrian Somers 144196c9bb21SBrian Somers if (strncmp(Version, iov[0].iov_base, iov[0].iov_len)) { 144296c9bb21SBrian Somers log_Printf(LogWARN, "Cannot receive datalink, incorrect version" 144396c9bb21SBrian Somers " (\"%.*s\", not \"%s\")\n", (int)iov[0].iov_len, 144496c9bb21SBrian Somers iov[0].iov_base, Version); 144596c9bb21SBrian Somers close(link_fd); 144696c9bb21SBrian Somers while (niov--) 144796c9bb21SBrian Somers free(iov[niov].iov_base); 144896c9bb21SBrian Somers return; 144996c9bb21SBrian Somers } 145096c9bb21SBrian Somers 145196c9bb21SBrian Somers niov = 1; 1452b7c5748eSBrian Somers dl = iov2datalink(bundle, iov, &niov, sizeof iov / sizeof *iov, link_fd); 1453b7c5748eSBrian Somers if (dl) { 145404eaa58cSBrian Somers bundle_DatalinkLinkin(bundle, dl); 1455b7c5748eSBrian Somers datalink_AuthOk(dl); 145696c9bb21SBrian Somers } else 145796c9bb21SBrian Somers close(link_fd); 145896c9bb21SBrian Somers 145996c9bb21SBrian Somers free(iov[0].iov_base); 146054cd8e13SBrian Somers close(s); 14611fa665f5SBrian Somers } 14621fa665f5SBrian Somers 14631fa665f5SBrian Somers void 146496c9bb21SBrian Somers bundle_SendDatalink(struct datalink *dl, int s, struct sockaddr_un *sun) 14651fa665f5SBrian Somers { 1466c0d9a877SBrian Somers char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int)], ack; 146796c9bb21SBrian Somers struct cmsghdr *cmsg = (struct cmsghdr *)cmsgbuf; 146896c9bb21SBrian Somers struct msghdr msg; 146996c9bb21SBrian Somers struct iovec iov[SCATTER_SEGMENTS]; 14701384bd27SBrian Somers int niov, link_fd, f, expect, newsid; 147185fd273aSBrian Somers pid_t newpid; 14726f384573SBrian Somers 1473dd7e2610SBrian Somers log_Printf(LogPHASE, "Transmitting datalink %s\n", dl->name); 14746f384573SBrian Somers 147504eaa58cSBrian Somers bundle_LinkClosed(dl->bundle, dl); 14760f2f3eb3SBrian Somers bundle_DatalinkLinkout(dl->bundle, dl); 1477ea722969SBrian Somers 147896c9bb21SBrian Somers /* Build our scatter/gather array */ 147996c9bb21SBrian Somers iov[0].iov_len = strlen(Version) + 1; 148096c9bb21SBrian Somers iov[0].iov_base = strdup(Version); 148196c9bb21SBrian Somers niov = 1; 14826f384573SBrian Somers 148385fd273aSBrian Somers read(s, &newpid, sizeof newpid); 148485fd273aSBrian Somers link_fd = datalink2iov(dl, iov, &niov, sizeof iov / sizeof *iov, newpid); 14856f384573SBrian Somers 14866f384573SBrian Somers if (link_fd != -1) { 148796c9bb21SBrian Somers memset(&msg, '\0', sizeof msg); 148854cd8e13SBrian Somers 148996c9bb21SBrian Somers msg.msg_name = (caddr_t)sun; 149096c9bb21SBrian Somers msg.msg_namelen = sizeof *sun; 149196c9bb21SBrian Somers msg.msg_iov = iov; 149296c9bb21SBrian Somers msg.msg_iovlen = niov; 149354cd8e13SBrian Somers 149454cd8e13SBrian Somers cmsg->cmsg_len = sizeof cmsgbuf; 149554cd8e13SBrian Somers cmsg->cmsg_level = SOL_SOCKET; 149654cd8e13SBrian Somers cmsg->cmsg_type = SCM_RIGHTS; 149754cd8e13SBrian Somers *(int *)CMSG_DATA(cmsg) = link_fd; 149896c9bb21SBrian Somers msg.msg_control = cmsgbuf; 149996c9bb21SBrian Somers msg.msg_controllen = sizeof cmsgbuf; 150047723d29SBrian Somers 150196c9bb21SBrian Somers for (f = expect = 0; f < niov; f++) 150296c9bb21SBrian Somers expect += iov[f].iov_len; 150347723d29SBrian Somers 150496c9bb21SBrian Somers log_Printf(LogDEBUG, "Sending %d bytes in scatter/gather array\n", expect); 150547723d29SBrian Somers 150696c9bb21SBrian Somers f = expect + SOCKET_OVERHEAD; 150796c9bb21SBrian Somers setsockopt(s, SOL_SOCKET, SO_SNDBUF, &f, sizeof f); 150896c9bb21SBrian Somers if (sendmsg(s, &msg, 0) == -1) 150996c9bb21SBrian Somers log_Printf(LogERROR, "Failed sendmsg: %s\n", strerror(errno)); 1510c0d9a877SBrian Somers /* We must get the ACK before closing the descriptor ! */ 1511c0d9a877SBrian Somers read(s, &ack, 1); 151254cd8e13SBrian Somers 15131384bd27SBrian Somers newsid = tcgetpgrp(link_fd) == getpgrp(); 151496c9bb21SBrian Somers close(link_fd); 15151384bd27SBrian Somers if (newsid) 15161384bd27SBrian Somers bundle_setsid(dl->bundle, 1); 151747723d29SBrian Somers } 151885fd273aSBrian Somers close(s); 151996c9bb21SBrian Somers 152096c9bb21SBrian Somers while (niov--) 152196c9bb21SBrian Somers free(iov[niov].iov_base); 15221fa665f5SBrian Somers } 1523dd0645c5SBrian Somers 1524dd0645c5SBrian Somers int 152558d55334SBrian Somers bundle_RenameDatalink(struct bundle *bundle, struct datalink *ndl, 152658d55334SBrian Somers const char *name) 152758d55334SBrian Somers { 152858d55334SBrian Somers struct datalink *dl; 152958d55334SBrian Somers 153058d55334SBrian Somers if (!strcasecmp(ndl->name, name)) 153158d55334SBrian Somers return 1; 153258d55334SBrian Somers 153358d55334SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 153458d55334SBrian Somers if (!strcasecmp(dl->name, name)) 153558d55334SBrian Somers return 0; 153658d55334SBrian Somers 153758d55334SBrian Somers datalink_Rename(ndl, name); 153858d55334SBrian Somers return 1; 153958d55334SBrian Somers } 154058d55334SBrian Somers 154158d55334SBrian Somers int 1542dd0645c5SBrian Somers bundle_SetMode(struct bundle *bundle, struct datalink *dl, int mode) 1543dd0645c5SBrian Somers { 1544dd0645c5SBrian Somers int omode; 1545dd0645c5SBrian Somers 1546dd0645c5SBrian Somers omode = dl->physical->type; 1547dd0645c5SBrian Somers if (omode == mode) 1548dd0645c5SBrian Somers return 1; 1549dd0645c5SBrian Somers 155081358fa3SBrian Somers if (mode == PHYS_AUTO && !(bundle->phys_type & PHYS_AUTO)) 1551dd0645c5SBrian Somers /* Changing to demand-dial mode */ 1552dd0645c5SBrian Somers if (bundle->ncp.ipcp.peer_ip.s_addr == INADDR_ANY) { 1553dd0645c5SBrian Somers log_Printf(LogWARN, "You must `set ifaddr' before changing mode to %s\n", 1554dd0645c5SBrian Somers mode2Nam(mode)); 1555dd0645c5SBrian Somers return 0; 1556dd0645c5SBrian Somers } 1557dd0645c5SBrian Somers 1558dd0645c5SBrian Somers if (!datalink_SetMode(dl, mode)) 1559dd0645c5SBrian Somers return 0; 1560dd0645c5SBrian Somers 156181358fa3SBrian Somers if (mode == PHYS_AUTO && !(bundle->phys_type & PHYS_AUTO)) 1562dd0645c5SBrian Somers ipcp_InterfaceUp(&bundle->ncp.ipcp); 1563dd0645c5SBrian Somers 156404eaa58cSBrian Somers /* Regenerate phys_type and adjust autoload & idle timers */ 156504eaa58cSBrian Somers bundle_LinksRemoved(bundle); 1566dd0645c5SBrian Somers 156781358fa3SBrian Somers if (omode == PHYS_AUTO && !(bundle->phys_type & PHYS_AUTO)) 1568dd0645c5SBrian Somers /* Changing from demand-dial mode */ 1569dd0645c5SBrian Somers ipcp_CleanInterface(&bundle->ncp.ipcp); 1570dd0645c5SBrian Somers 1571dd0645c5SBrian Somers return 1; 1572dd0645c5SBrian Somers } 15731384bd27SBrian Somers 15741384bd27SBrian Somers void 15751384bd27SBrian Somers bundle_setsid(struct bundle *bundle, int holdsession) 15761384bd27SBrian Somers { 15771384bd27SBrian Somers /* 15781384bd27SBrian Somers * Lose the current session. This means getting rid of our pid 15791384bd27SBrian Somers * too so that the tty device will really go away, and any getty 15801384bd27SBrian Somers * etc will be allowed to restart. 15811384bd27SBrian Somers */ 15821384bd27SBrian Somers pid_t pid, orig; 15831384bd27SBrian Somers int fds[2]; 15841384bd27SBrian Somers char done; 15851384bd27SBrian Somers struct datalink *dl; 15861384bd27SBrian Somers 15871384bd27SBrian Somers orig = getpid(); 15881384bd27SBrian Somers if (pipe(fds) == -1) { 15891384bd27SBrian Somers log_Printf(LogERROR, "pipe: %s\n", strerror(errno)); 15901384bd27SBrian Somers return; 15911384bd27SBrian Somers } 15921384bd27SBrian Somers switch ((pid = fork())) { 15931384bd27SBrian Somers case -1: 15941384bd27SBrian Somers log_Printf(LogERROR, "fork: %s\n", strerror(errno)); 15951384bd27SBrian Somers close(fds[0]); 15961384bd27SBrian Somers close(fds[1]); 15971384bd27SBrian Somers return; 15981384bd27SBrian Somers case 0: 15991384bd27SBrian Somers close(fds[0]); 16001384bd27SBrian Somers read(fds[1], &done, 1); /* uu_locks are mine ! */ 16011384bd27SBrian Somers close(fds[1]); 16021384bd27SBrian Somers if (pipe(fds) == -1) { 16031384bd27SBrian Somers log_Printf(LogERROR, "pipe(2): %s\n", strerror(errno)); 16041384bd27SBrian Somers return; 16051384bd27SBrian Somers } 16061384bd27SBrian Somers switch ((pid = fork())) { 16071384bd27SBrian Somers case -1: 16081384bd27SBrian Somers log_Printf(LogERROR, "fork: %s\n", strerror(errno)); 16091384bd27SBrian Somers close(fds[0]); 16101384bd27SBrian Somers close(fds[1]); 16111384bd27SBrian Somers return; 16121384bd27SBrian Somers case 0: 16131384bd27SBrian Somers close(fds[0]); 16141384bd27SBrian Somers bundle_LockTun(bundle); /* update pid */ 16151384bd27SBrian Somers read(fds[1], &done, 1); /* uu_locks are mine ! */ 16161384bd27SBrian Somers close(fds[1]); 16171384bd27SBrian Somers setsid(); 16181384bd27SBrian Somers log_Printf(LogPHASE, "%d -> %d: %s session control\n", 16191384bd27SBrian Somers (int)orig, (int)getpid(), 16201384bd27SBrian Somers holdsession ? "Passed" : "Dropped"); 16211384bd27SBrian Somers break; 16221384bd27SBrian Somers default: 16231384bd27SBrian Somers close(fds[1]); 16241384bd27SBrian Somers /* Give away all our modem locks (to the final process) */ 16251384bd27SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 16261384bd27SBrian Somers if (dl->state != DATALINK_CLOSED) 16271384bd27SBrian Somers modem_ChangedPid(dl->physical, pid); 16281384bd27SBrian Somers write(fds[0], "!", 1); /* done */ 16291384bd27SBrian Somers close(fds[0]); 16301384bd27SBrian Somers exit(0); 16311384bd27SBrian Somers break; 16321384bd27SBrian Somers } 16331384bd27SBrian Somers break; 16341384bd27SBrian Somers default: 16351384bd27SBrian Somers close(fds[1]); 16361384bd27SBrian Somers /* Give away all our modem locks (to the intermediate process) */ 16371384bd27SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 16381384bd27SBrian Somers if (dl->state != DATALINK_CLOSED) 16391384bd27SBrian Somers modem_ChangedPid(dl->physical, pid); 16401384bd27SBrian Somers write(fds[0], "!", 1); /* done */ 16411384bd27SBrian Somers close(fds[0]); 16421384bd27SBrian Somers if (holdsession) { 16431384bd27SBrian Somers int fd, status; 16441384bd27SBrian Somers 16451384bd27SBrian Somers timer_TermService(); 16461384bd27SBrian Somers signal(SIGPIPE, SIG_DFL); 16471384bd27SBrian Somers signal(SIGALRM, SIG_DFL); 16481384bd27SBrian Somers signal(SIGHUP, SIG_DFL); 16491384bd27SBrian Somers signal(SIGTERM, SIG_DFL); 16501384bd27SBrian Somers signal(SIGINT, SIG_DFL); 16511384bd27SBrian Somers signal(SIGQUIT, SIG_DFL); 16521384bd27SBrian Somers for (fd = getdtablesize(); fd >= 0; fd--) 16531384bd27SBrian Somers close(fd); 16541384bd27SBrian Somers setuid(geteuid()); 16551384bd27SBrian Somers /* 16561384bd27SBrian Somers * Reap the intermediate process. As we're not exiting but the 16571384bd27SBrian Somers * intermediate is, we don't want it to become defunct. 16581384bd27SBrian Somers */ 16591384bd27SBrian Somers waitpid(pid, &status, 0); 16608e7b8599SBrian Somers /* Tweak our process arguments.... */ 16618e7b8599SBrian Somers bundle->argv[0] = "session owner"; 16628e7b8599SBrian Somers bundle->argv[1] = NULL; 16631384bd27SBrian Somers /* 16641384bd27SBrian Somers * Hang around for a HUP. This should happen as soon as the 16651384bd27SBrian Somers * ppp that we passed our ctty descriptor to closes it. 16661384bd27SBrian Somers * NOTE: If this process dies, the passed descriptor becomes 16671384bd27SBrian Somers * invalid and will give a select() error by setting one 16681384bd27SBrian Somers * of the error fds, aborting the other ppp. We don't 16691384bd27SBrian Somers * want that to happen ! 16701384bd27SBrian Somers */ 16711384bd27SBrian Somers pause(); 16721384bd27SBrian Somers } 16731384bd27SBrian Somers exit(0); 16741384bd27SBrian Somers break; 16751384bd27SBrian Somers } 16761384bd27SBrian Somers } 1677