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 * 2654cd8e13SBrian Somers * $Id: bundle.c,v 1.6 1998/05/23 22:28:19 brian Exp $ 277a6f8720SBrian Somers */ 287a6f8720SBrian Somers 292764b86aSBrian Somers #include <sys/types.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; 51824989c68SBrian Somers log_Printf(LogTIMER, "tun: fdset(r) %d\n", 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) { 570078c562eSBrian Somers log_Printf(LogERROR, "read from tun: %s\n", strerror(errno)); 571078c562eSBrian Somers return; 572078c562eSBrian Somers } 573078c562eSBrian Somers n -= sizeof tun - sizeof tun.data; 574078c562eSBrian Somers if (n <= 0) { 575078c562eSBrian Somers log_Printf(LogERROR, "read from tun: Only %d bytes read\n", 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 6567a6f8720SBrian Somers 6577a6f8720SBrian Somers struct bundle * 6580f2f3eb3SBrian Somers bundle_Create(const char *prefix, int type) 6597a6f8720SBrian Somers { 6607a6f8720SBrian Somers int s, enoentcount, err; 6617a6f8720SBrian Somers struct ifreq ifrq; 6627a6f8720SBrian Somers static struct bundle bundle; /* there can be only one */ 6637a6f8720SBrian Somers 664faefde08SBrian Somers if (bundle.ifp.Name != NULL) { /* Already allocated ! */ 665dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_Create: There's only one BUNDLE !\n"); 6667a6f8720SBrian Somers return NULL; 6677a6f8720SBrian Somers } 6687a6f8720SBrian Somers 6697a6f8720SBrian Somers err = ENOENT; 6707a6f8720SBrian Somers enoentcount = 0; 671107d62e7SBrian Somers for (bundle.unit = 0; ; bundle.unit++) { 672faefde08SBrian Somers snprintf(bundle.dev.Name, sizeof bundle.dev.Name, "%s%d", 673faefde08SBrian Somers prefix, bundle.unit); 674faefde08SBrian Somers bundle.dev.fd = ID0open(bundle.dev.Name, O_RDWR); 675faefde08SBrian Somers if (bundle.dev.fd >= 0) 6767a6f8720SBrian Somers break; 677107d62e7SBrian Somers else if (errno == ENXIO) { 6787a6f8720SBrian Somers err = errno; 679107d62e7SBrian Somers break; 6807a6f8720SBrian Somers } else if (errno == ENOENT) { 6817a6f8720SBrian Somers if (++enoentcount > 2) 682107d62e7SBrian Somers break; 6837a6f8720SBrian Somers } else 6847a6f8720SBrian Somers err = errno; 6857a6f8720SBrian Somers } 6867a6f8720SBrian Somers 687faefde08SBrian Somers if (bundle.dev.fd < 0) { 688dd7e2610SBrian Somers log_Printf(LogWARN, "No available tunnel devices found (%s).\n", 68985b542cfSBrian Somers strerror(err)); 6907a6f8720SBrian Somers return NULL; 6917a6f8720SBrian Somers } 6927a6f8720SBrian Somers 693dd7e2610SBrian Somers log_SetTun(bundle.unit); 6947a6f8720SBrian Somers 6957a6f8720SBrian Somers s = socket(AF_INET, SOCK_DGRAM, 0); 6967a6f8720SBrian Somers if (s < 0) { 697dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_Create: socket(): %s\n", strerror(errno)); 698faefde08SBrian Somers close(bundle.dev.fd); 6997a6f8720SBrian Somers return NULL; 7007a6f8720SBrian Somers } 7017a6f8720SBrian Somers 702faefde08SBrian Somers bundle.ifp.Name = strrchr(bundle.dev.Name, '/'); 703faefde08SBrian Somers if (bundle.ifp.Name == NULL) 704faefde08SBrian Somers bundle.ifp.Name = bundle.dev.Name; 7057a6f8720SBrian Somers else 706faefde08SBrian Somers bundle.ifp.Name++; 7077a6f8720SBrian Somers 7087a6f8720SBrian Somers /* 7097a6f8720SBrian Somers * Now, bring up the interface. 7107a6f8720SBrian Somers */ 7117a6f8720SBrian Somers memset(&ifrq, '\0', sizeof ifrq); 712faefde08SBrian Somers strncpy(ifrq.ifr_name, bundle.ifp.Name, sizeof ifrq.ifr_name - 1); 7137a6f8720SBrian Somers ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0'; 7147a6f8720SBrian Somers if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) { 715dd7e2610SBrian Somers log_Printf(LogERROR, "OpenTunnel: ioctl(SIOCGIFFLAGS): %s\n", 7167a6f8720SBrian Somers strerror(errno)); 7177a6f8720SBrian Somers close(s); 718faefde08SBrian Somers close(bundle.dev.fd); 719faefde08SBrian Somers bundle.ifp.Name = NULL; 7207a6f8720SBrian Somers return NULL; 7217a6f8720SBrian Somers } 7227a6f8720SBrian Somers ifrq.ifr_flags |= IFF_UP; 7237a6f8720SBrian Somers if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) { 724dd7e2610SBrian Somers log_Printf(LogERROR, "OpenTunnel: ioctl(SIOCSIFFLAGS): %s\n", 7257a6f8720SBrian Somers strerror(errno)); 7267a6f8720SBrian Somers close(s); 727faefde08SBrian Somers close(bundle.dev.fd); 728faefde08SBrian Somers bundle.ifp.Name = NULL; 7297a6f8720SBrian Somers return NULL; 7307a6f8720SBrian Somers } 7317a6f8720SBrian Somers 7327a6f8720SBrian Somers close(s); 7337a6f8720SBrian Somers 734faefde08SBrian Somers if ((bundle.ifp.Index = GetIfIndex(bundle.ifp.Name)) < 0) { 735faefde08SBrian Somers log_Printf(LogERROR, "OpenTunnel: Can't find interface index.\n"); 736faefde08SBrian Somers close(bundle.dev.fd); 737faefde08SBrian Somers bundle.ifp.Name = NULL; 7387a6f8720SBrian Somers return NULL; 7397a6f8720SBrian Somers } 740faefde08SBrian Somers log_Printf(LogPHASE, "Using interface: %s\n", bundle.ifp.Name); 7417a6f8720SBrian Somers 742faefde08SBrian Somers bundle.ifp.Speed = 0; 7437a6f8720SBrian Somers 744820de6ebSBrian Somers bundle.routing_seq = 0; 745a0cbd833SBrian Somers bundle.phase = PHASE_DEAD; 746a0cbd833SBrian Somers bundle.CleaningUp = 0; 7477a6f8720SBrian Somers 7486d666775SBrian Somers bundle.fsm.LayerStart = bundle_LayerStart; 7496f384573SBrian Somers bundle.fsm.LayerUp = bundle_LayerUp; 7506d666775SBrian Somers bundle.fsm.LayerDown = bundle_LayerDown; 7516d666775SBrian Somers bundle.fsm.LayerFinish = bundle_LayerFinish; 7526d666775SBrian Somers bundle.fsm.object = &bundle; 7537a6f8720SBrian Somers 754ab886ad0SBrian Somers bundle.cfg.idle_timeout = NCP_IDLE_TIMEOUT; 7551342caedSBrian Somers *bundle.cfg.auth.name = '\0'; 7561342caedSBrian Somers *bundle.cfg.auth.key = '\0'; 757610b185fSBrian Somers bundle.cfg.opt = OPT_SROUTES | OPT_IDCHECK | OPT_LOOPBACK | 758610b185fSBrian Somers OPT_THROUGHPUT | OPT_UTMP; 75949052c95SBrian Somers *bundle.cfg.label = '\0'; 76049052c95SBrian Somers bundle.cfg.mtu = DEF_MTU; 76104eaa58cSBrian Somers bundle.cfg.autoload.max.packets = 0; 76204eaa58cSBrian Somers bundle.cfg.autoload.max.timeout = 0; 76304eaa58cSBrian Somers bundle.cfg.autoload.min.packets = 0; 76404eaa58cSBrian Somers bundle.cfg.autoload.min.timeout = 0; 765565e35e5SBrian Somers bundle.phys_type = type; 766ab886ad0SBrian Somers 7676f384573SBrian Somers bundle.links = datalink_Create("deflink", &bundle, type); 7683006ec67SBrian Somers if (bundle.links == NULL) { 769dd7e2610SBrian Somers log_Printf(LogERROR, "Cannot create data link: %s\n", strerror(errno)); 770faefde08SBrian Somers close(bundle.dev.fd); 771faefde08SBrian Somers bundle.ifp.Name = NULL; 7722289f246SBrian Somers return NULL; 7732289f246SBrian Somers } 7742289f246SBrian Somers 7752f786681SBrian Somers bundle.desc.type = BUNDLE_DESCRIPTOR; 7762f786681SBrian Somers bundle.desc.UpdateSet = bundle_UpdateSet; 7772f786681SBrian Somers bundle.desc.IsSet = bundle_IsSet; 7782f786681SBrian Somers bundle.desc.Read = bundle_DescriptorRead; 7792f786681SBrian Somers bundle.desc.Write = bundle_DescriptorWrite; 7802f786681SBrian Somers 78149052c95SBrian Somers mp_Init(&bundle.ncp.mp, &bundle); 78249052c95SBrian Somers 78349052c95SBrian Somers /* Send over the first physical link by default */ 7845828db6dSBrian Somers ipcp_Init(&bundle.ncp.ipcp, &bundle, &bundle.links->physical->link, 7855828db6dSBrian Somers &bundle.fsm); 7866d666775SBrian Somers 7875ca5389aSBrian Somers memset(&bundle.filter, '\0', sizeof bundle.filter); 7885ca5389aSBrian Somers bundle.filter.in.fragok = bundle.filter.in.logok = 1; 7895ca5389aSBrian Somers bundle.filter.in.name = "IN"; 7905ca5389aSBrian Somers bundle.filter.out.fragok = bundle.filter.out.logok = 1; 7915ca5389aSBrian Somers bundle.filter.out.name = "OUT"; 7925ca5389aSBrian Somers bundle.filter.dial.name = "DIAL"; 7938390b576SBrian Somers bundle.filter.dial.logok = 1; 7945ca5389aSBrian Somers bundle.filter.alive.name = "ALIVE"; 7955ca5389aSBrian Somers bundle.filter.alive.logok = 1; 79693ee0ff2SBrian Somers memset(&bundle.idle.timer, '\0', sizeof bundle.idle.timer); 79793ee0ff2SBrian Somers bundle.idle.done = 0; 7985cf4388bSBrian Somers bundle.notify.fd = -1; 79904eaa58cSBrian Somers memset(&bundle.autoload.timer, '\0', sizeof bundle.autoload.timer); 80004eaa58cSBrian Somers bundle.autoload.done = 0; 80104eaa58cSBrian Somers bundle.autoload.running = 0; 80293ee0ff2SBrian Somers 8036d666775SBrian Somers /* Clean out any leftover crud */ 8046d666775SBrian Somers bundle_CleanInterface(&bundle); 8056d666775SBrian Somers 8067a6f8720SBrian Somers return &bundle; 8077a6f8720SBrian Somers } 8087a6f8720SBrian Somers 80968a0f0ccSBrian Somers static void 81068a0f0ccSBrian Somers bundle_DownInterface(struct bundle *bundle) 81168a0f0ccSBrian Somers { 81268a0f0ccSBrian Somers struct ifreq ifrq; 81368a0f0ccSBrian Somers int s; 81468a0f0ccSBrian Somers 815dd7e2610SBrian Somers route_IfDelete(bundle, 1); 81668a0f0ccSBrian Somers 81768a0f0ccSBrian Somers s = ID0socket(AF_INET, SOCK_DGRAM, 0); 81868a0f0ccSBrian Somers if (s < 0) { 819dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_DownInterface: socket: %s\n", strerror(errno)); 82068a0f0ccSBrian Somers return; 82168a0f0ccSBrian Somers } 82268a0f0ccSBrian Somers 82368a0f0ccSBrian Somers memset(&ifrq, '\0', sizeof ifrq); 824faefde08SBrian Somers strncpy(ifrq.ifr_name, bundle->ifp.Name, sizeof ifrq.ifr_name - 1); 82568a0f0ccSBrian Somers ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0'; 82668a0f0ccSBrian Somers if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) { 827dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_DownInterface: ioctl(SIOCGIFFLAGS): %s\n", 82868a0f0ccSBrian Somers strerror(errno)); 82968a0f0ccSBrian Somers close(s); 83068a0f0ccSBrian Somers return; 83168a0f0ccSBrian Somers } 83268a0f0ccSBrian Somers ifrq.ifr_flags &= ~IFF_UP; 83368a0f0ccSBrian Somers if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) { 834dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_DownInterface: ioctl(SIOCSIFFLAGS): %s\n", 83568a0f0ccSBrian Somers strerror(errno)); 83668a0f0ccSBrian Somers close(s); 83768a0f0ccSBrian Somers return; 83868a0f0ccSBrian Somers } 83968a0f0ccSBrian Somers close(s); 84068a0f0ccSBrian Somers } 84168a0f0ccSBrian Somers 84268a0f0ccSBrian Somers void 84368a0f0ccSBrian Somers bundle_Destroy(struct bundle *bundle) 84468a0f0ccSBrian Somers { 8453006ec67SBrian Somers struct datalink *dl; 846b6217683SBrian Somers 847ea722969SBrian Somers /* 84804eaa58cSBrian Somers * Clean up the interface. We don't need to timer_Stop()s, mp_Down(), 849ea722969SBrian Somers * ipcp_CleanInterface() and bundle_DownInterface() unless we're getting 850ea722969SBrian Somers * out under exceptional conditions such as a descriptor exception. 851ea722969SBrian Somers */ 85204eaa58cSBrian Somers timer_Stop(&bundle->idle.timer); 85304eaa58cSBrian Somers timer_Stop(&bundle->autoload.timer); 85466f634b6SBrian Somers mp_Down(&bundle->ncp.mp); 855dd7e2610SBrian Somers ipcp_CleanInterface(&bundle->ncp.ipcp); 85668a0f0ccSBrian Somers bundle_DownInterface(bundle); 8573006ec67SBrian Somers 858ea722969SBrian Somers /* Again, these are all DATALINK_CLOSED unless we're abending */ 8593006ec67SBrian Somers dl = bundle->links; 8603006ec67SBrian Somers while (dl) 8613006ec67SBrian Somers dl = datalink_Destroy(dl); 8623006ec67SBrian Somers 863ea722969SBrian Somers /* In case we never made PHASE_NETWORK */ 8645cf4388bSBrian Somers bundle_Notify(bundle, EX_ERRDEAD); 865b6217683SBrian Somers 866faefde08SBrian Somers bundle->ifp.Name = NULL; 86768a0f0ccSBrian Somers } 86868a0f0ccSBrian Somers 8697a6f8720SBrian Somers struct rtmsg { 8707a6f8720SBrian Somers struct rt_msghdr m_rtm; 8717a6f8720SBrian Somers char m_space[64]; 8727a6f8720SBrian Somers }; 8737a6f8720SBrian Somers 874610b185fSBrian Somers int 875820de6ebSBrian Somers bundle_SetRoute(struct bundle *bundle, int cmd, struct in_addr dst, 8767a6f8720SBrian Somers struct in_addr gateway, struct in_addr mask, int bang) 8777a6f8720SBrian Somers { 8787a6f8720SBrian Somers struct rtmsg rtmes; 8797a6f8720SBrian Somers int s, nb, wb; 8807a6f8720SBrian Somers char *cp; 8817a6f8720SBrian Somers const char *cmdstr; 8827a6f8720SBrian Somers struct sockaddr_in rtdata; 883610b185fSBrian Somers int result = 1; 8847a6f8720SBrian Somers 8857a6f8720SBrian Somers if (bang) 8867a6f8720SBrian Somers cmdstr = (cmd == RTM_ADD ? "Add!" : "Delete!"); 8877a6f8720SBrian Somers else 8887a6f8720SBrian Somers cmdstr = (cmd == RTM_ADD ? "Add" : "Delete"); 8897a6f8720SBrian Somers s = ID0socket(PF_ROUTE, SOCK_RAW, 0); 8907a6f8720SBrian Somers if (s < 0) { 891dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_SetRoute: socket(): %s\n", strerror(errno)); 892610b185fSBrian Somers return result; 8937a6f8720SBrian Somers } 8947a6f8720SBrian Somers memset(&rtmes, '\0', sizeof rtmes); 8957a6f8720SBrian Somers rtmes.m_rtm.rtm_version = RTM_VERSION; 8967a6f8720SBrian Somers rtmes.m_rtm.rtm_type = cmd; 8977a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs = RTA_DST; 898820de6ebSBrian Somers rtmes.m_rtm.rtm_seq = ++bundle->routing_seq; 8997a6f8720SBrian Somers rtmes.m_rtm.rtm_pid = getpid(); 9007a6f8720SBrian Somers rtmes.m_rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC; 9017a6f8720SBrian Somers 9027a6f8720SBrian Somers memset(&rtdata, '\0', sizeof rtdata); 90350e5c17dSBrian Somers rtdata.sin_len = sizeof rtdata; 9047a6f8720SBrian Somers rtdata.sin_family = AF_INET; 9057a6f8720SBrian Somers rtdata.sin_port = 0; 9067a6f8720SBrian Somers rtdata.sin_addr = dst; 9077a6f8720SBrian Somers 9087a6f8720SBrian Somers cp = rtmes.m_space; 90950e5c17dSBrian Somers memcpy(cp, &rtdata, rtdata.sin_len); 91050e5c17dSBrian Somers cp += rtdata.sin_len; 911e43ebac1SBrian Somers if (cmd == RTM_ADD) { 9127a6f8720SBrian Somers if (gateway.s_addr == INADDR_ANY) { 9137a6f8720SBrian Somers /* Add a route through the interface */ 9147a6f8720SBrian Somers struct sockaddr_dl dl; 9157a6f8720SBrian Somers const char *iname; 9167a6f8720SBrian Somers int ilen; 9177a6f8720SBrian Somers 918faefde08SBrian Somers iname = Index2Nam(bundle->ifp.Index); 9197a6f8720SBrian Somers ilen = strlen(iname); 9207a6f8720SBrian Somers dl.sdl_len = sizeof dl - sizeof dl.sdl_data + ilen; 9217a6f8720SBrian Somers dl.sdl_family = AF_LINK; 922faefde08SBrian Somers dl.sdl_index = bundle->ifp.Index; 9237a6f8720SBrian Somers dl.sdl_type = 0; 9247a6f8720SBrian Somers dl.sdl_nlen = ilen; 9257a6f8720SBrian Somers dl.sdl_alen = 0; 9267a6f8720SBrian Somers dl.sdl_slen = 0; 9277a6f8720SBrian Somers strncpy(dl.sdl_data, iname, sizeof dl.sdl_data); 9287a6f8720SBrian Somers memcpy(cp, &dl, dl.sdl_len); 9297a6f8720SBrian Somers cp += dl.sdl_len; 9307a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY; 9317a6f8720SBrian Somers } else { 9327a6f8720SBrian Somers rtdata.sin_addr = gateway; 93350e5c17dSBrian Somers memcpy(cp, &rtdata, rtdata.sin_len); 93450e5c17dSBrian Somers cp += rtdata.sin_len; 9357a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY; 9367a6f8720SBrian Somers } 937e43ebac1SBrian Somers } 9387a6f8720SBrian Somers 9397a6f8720SBrian Somers if (dst.s_addr == INADDR_ANY) 9407a6f8720SBrian Somers mask.s_addr = INADDR_ANY; 9417a6f8720SBrian Somers 9427a6f8720SBrian Somers if (cmd == RTM_ADD || dst.s_addr == INADDR_ANY) { 9437a6f8720SBrian Somers rtdata.sin_addr = mask; 94450e5c17dSBrian Somers memcpy(cp, &rtdata, rtdata.sin_len); 94550e5c17dSBrian Somers cp += rtdata.sin_len; 9467a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs |= RTA_NETMASK; 9477a6f8720SBrian Somers } 9487a6f8720SBrian Somers 9497a6f8720SBrian Somers nb = cp - (char *) &rtmes; 9507a6f8720SBrian Somers rtmes.m_rtm.rtm_msglen = nb; 9517a6f8720SBrian Somers wb = ID0write(s, &rtmes, nb); 9527a6f8720SBrian Somers if (wb < 0) { 953dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute failure:\n"); 954dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Cmd = %s\n", cmdstr); 955dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Dst = %s\n", inet_ntoa(dst)); 956dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Gateway = %s\n", inet_ntoa(gateway)); 957dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Mask = %s\n", inet_ntoa(mask)); 9587a6f8720SBrian Somers failed: 9597a6f8720SBrian Somers if (cmd == RTM_ADD && (rtmes.m_rtm.rtm_errno == EEXIST || 960e43ebac1SBrian Somers (rtmes.m_rtm.rtm_errno == 0 && errno == EEXIST))) { 961610b185fSBrian Somers if (!bang) { 962dd7e2610SBrian Somers log_Printf(LogWARN, "Add route failed: %s already exists\n", 9637a6f8720SBrian Somers inet_ntoa(dst)); 964610b185fSBrian Somers result = 0; /* Don't add to our dynamic list */ 965610b185fSBrian Somers } else { 9667a6f8720SBrian Somers rtmes.m_rtm.rtm_type = cmd = RTM_CHANGE; 9677a6f8720SBrian Somers if ((wb = ID0write(s, &rtmes, nb)) < 0) 9687a6f8720SBrian Somers goto failed; 9697a6f8720SBrian Somers } 970e43ebac1SBrian Somers } else if (cmd == RTM_DELETE && 9717a6f8720SBrian Somers (rtmes.m_rtm.rtm_errno == ESRCH || 9727a6f8720SBrian Somers (rtmes.m_rtm.rtm_errno == 0 && errno == ESRCH))) { 9737a6f8720SBrian Somers if (!bang) 974dd7e2610SBrian Somers log_Printf(LogWARN, "Del route failed: %s: Non-existent\n", 9757a6f8720SBrian Somers inet_ntoa(dst)); 9767a6f8720SBrian Somers } else if (rtmes.m_rtm.rtm_errno == 0) 977dd7e2610SBrian Somers log_Printf(LogWARN, "%s route failed: %s: errno: %s\n", cmdstr, 9787a6f8720SBrian Somers inet_ntoa(dst), strerror(errno)); 9797a6f8720SBrian Somers else 980dd7e2610SBrian Somers log_Printf(LogWARN, "%s route failed: %s: %s\n", 9817a6f8720SBrian Somers cmdstr, inet_ntoa(dst), strerror(rtmes.m_rtm.rtm_errno)); 9827a6f8720SBrian Somers } 983dd7e2610SBrian Somers log_Printf(LogDEBUG, "wrote %d: cmd = %s, dst = %x, gateway = %x\n", 984fe3125a0SBrian Somers wb, cmdstr, (unsigned)dst.s_addr, (unsigned)gateway.s_addr); 9857a6f8720SBrian Somers close(s); 986610b185fSBrian Somers 987610b185fSBrian Somers return result; 9887a6f8720SBrian Somers } 98983d1af55SBrian Somers 99083d1af55SBrian Somers void 9913006ec67SBrian Somers bundle_LinkClosed(struct bundle *bundle, struct datalink *dl) 9923006ec67SBrian Somers { 9933006ec67SBrian Somers /* 9943006ec67SBrian Somers * Our datalink has closed. 995ea722969SBrian Somers * CleanDatalinks() (called from DoLoop()) will remove closed 996ea722969SBrian Somers * 1OFF and DIRECT links. 9973b0f8d2eSBrian Somers * If it's the last data link, enter phase DEAD. 998ea722969SBrian Somers * 999ea722969SBrian Somers * NOTE: dl may not be in our list (bundle_SendDatalink()) ! 10003006ec67SBrian Somers */ 10015b8b8060SBrian Somers 10023b0f8d2eSBrian Somers struct datalink *odl; 10033b0f8d2eSBrian Somers int other_links; 10045b8b8060SBrian Somers 10053b0f8d2eSBrian Somers other_links = 0; 10063b0f8d2eSBrian Somers for (odl = bundle->links; odl; odl = odl->next) 10073b0f8d2eSBrian Somers if (odl != dl && odl->state != DATALINK_CLOSED) 10083b0f8d2eSBrian Somers other_links++; 10093b0f8d2eSBrian Somers 10103b0f8d2eSBrian Somers if (!other_links) { 101103704096SBrian Somers if (dl->physical->type != PHYS_DEMAND) /* Not in -auto mode */ 101203704096SBrian Somers bundle_DownInterface(bundle); 101326afeaa2SBrian Somers if (bundle->ncp.ipcp.fsm.state > ST_CLOSED || 101426afeaa2SBrian Somers bundle->ncp.ipcp.fsm.state == ST_STARTING) { 1015dd7e2610SBrian Somers fsm_Down(&bundle->ncp.ipcp.fsm); 1016dd7e2610SBrian Somers fsm_Close(&bundle->ncp.ipcp.fsm); /* ST_INITIAL please */ 101726afeaa2SBrian Somers } 10185563ebdeSBrian Somers bundle_NewPhase(bundle, PHASE_DEAD); 10190f2f3eb3SBrian Somers bundle_StopIdleTimer(bundle); 102004eaa58cSBrian Somers bundle_StopAutoLoadTimer(bundle); 102104eaa58cSBrian Somers bundle->autoload.running = 0; 102204eaa58cSBrian Somers } else 102304eaa58cSBrian Somers bundle->autoload.running = 1; 1024455aabc3SBrian Somers } 1025455aabc3SBrian Somers 1026455aabc3SBrian Somers void 1027565e35e5SBrian Somers bundle_Open(struct bundle *bundle, const char *name, int mask) 10283006ec67SBrian Somers { 10293006ec67SBrian Somers /* 10303006ec67SBrian Somers * Please open the given datalink, or all if name == NULL 10313006ec67SBrian Somers */ 10323006ec67SBrian Somers struct datalink *dl; 10333006ec67SBrian Somers 103404eaa58cSBrian Somers timer_Stop(&bundle->autoload.timer); 10353006ec67SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 10363006ec67SBrian Somers if (name == NULL || !strcasecmp(dl->name, name)) { 103704eaa58cSBrian Somers if (dl->state == DATALINK_CLOSED && (mask & dl->physical->type)) { 1038565e35e5SBrian Somers datalink_Up(dl, 1, 1); 103904eaa58cSBrian Somers if (mask == PHYS_DEMAND) 104004eaa58cSBrian Somers /* Only one DEMAND link at a time (see the AutoLoad timer) */ 104104eaa58cSBrian Somers break; 104204eaa58cSBrian Somers } 10433006ec67SBrian Somers if (name != NULL) 10443006ec67SBrian Somers break; 10453006ec67SBrian Somers } 10463006ec67SBrian Somers } 10473006ec67SBrian Somers 10483006ec67SBrian Somers struct datalink * 10493006ec67SBrian Somers bundle2datalink(struct bundle *bundle, const char *name) 10503006ec67SBrian Somers { 10513006ec67SBrian Somers struct datalink *dl; 10523006ec67SBrian Somers 10533006ec67SBrian Somers if (name != NULL) { 10543006ec67SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 10553006ec67SBrian Somers if (!strcasecmp(dl->name, name)) 10563006ec67SBrian Somers return dl; 10573006ec67SBrian Somers } else if (bundle->links && !bundle->links->next) 10583006ec67SBrian Somers return bundle->links; 10593006ec67SBrian Somers 10603006ec67SBrian Somers return NULL; 10613006ec67SBrian Somers } 10623006ec67SBrian Somers 10633006ec67SBrian Somers int 10643006ec67SBrian Somers bundle_FillQueues(struct bundle *bundle) 10653006ec67SBrian Somers { 10663b0f8d2eSBrian Somers int total; 10673b0f8d2eSBrian Somers 10681bc9b5baSBrian Somers if (bundle->ncp.mp.active) 10693b0f8d2eSBrian Somers total = mp_FillQueues(bundle); 10701bc9b5baSBrian Somers else { 10711bc9b5baSBrian Somers struct datalink *dl; 10721bc9b5baSBrian Somers int add; 10731bc9b5baSBrian Somers 10741bc9b5baSBrian Somers for (total = 0, dl = bundle->links; dl; dl = dl->next) 10751bc9b5baSBrian Somers if (dl->state == DATALINK_OPEN) { 10761bc9b5baSBrian Somers add = link_QueueLen(&dl->physical->link); 10771bc9b5baSBrian Somers if (add == 0 && dl->physical->out == NULL) 10781bc9b5baSBrian Somers add = ip_FlushPacket(&dl->physical->link, bundle); 10791bc9b5baSBrian Somers total += add; 10801bc9b5baSBrian Somers } 10813006ec67SBrian Somers } 10823006ec67SBrian Somers 10833b0f8d2eSBrian Somers return total + ip_QueueLen(); 10843006ec67SBrian Somers } 1085aef795ccSBrian Somers 1086aef795ccSBrian Somers int 1087aef795ccSBrian Somers bundle_ShowLinks(struct cmdargs const *arg) 1088aef795ccSBrian Somers { 1089aef795ccSBrian Somers struct datalink *dl; 1090aef795ccSBrian Somers 10919c53a7b1SBrian Somers for (dl = arg->bundle->links; dl; dl = dl->next) { 1092d4156d00SBrian Somers prompt_Printf(arg->prompt, "Name: %s [%s, %s]", 1093d4156d00SBrian Somers dl->name, mode2Nam(dl->physical->type), datalink_State(dl)); 1094eeab6bf5SBrian Somers if (dl->physical->link.throughput.rolling && dl->state == DATALINK_OPEN) 1095d4156d00SBrian Somers prompt_Printf(arg->prompt, " weight %d, %d bytes/sec", 1096eeab6bf5SBrian Somers dl->mp.weight, 10979c53a7b1SBrian Somers dl->physical->link.throughput.OctetsPerSecond); 10989c53a7b1SBrian Somers prompt_Printf(arg->prompt, "\n"); 10999c53a7b1SBrian Somers } 1100aef795ccSBrian Somers 1101aef795ccSBrian Somers return 0; 1102aef795ccSBrian Somers } 1103ab886ad0SBrian Somers 11041342caedSBrian Somers static const char * 11051342caedSBrian Somers optval(struct bundle *bundle, int bit) 11061342caedSBrian Somers { 11071342caedSBrian Somers return (bundle->cfg.opt & bit) ? "enabled" : "disabled"; 11081342caedSBrian Somers } 11091342caedSBrian Somers 1110c08717dfSBrian Somers int 1111c08717dfSBrian Somers bundle_ShowStatus(struct cmdargs const *arg) 1112c08717dfSBrian Somers { 1113c08717dfSBrian Somers int remaining; 1114c08717dfSBrian Somers 1115c08717dfSBrian Somers prompt_Printf(arg->prompt, "Phase %s\n", bundle_PhaseName(arg->bundle)); 1116faefde08SBrian Somers prompt_Printf(arg->prompt, " Device: %s\n", arg->bundle->dev.Name); 1117faefde08SBrian Somers prompt_Printf(arg->prompt, " Interface: %s @ %lubps\n", 1118faefde08SBrian Somers arg->bundle->ifp.Name, arg->bundle->ifp.Speed); 1119c08717dfSBrian Somers 1120c08717dfSBrian Somers prompt_Printf(arg->prompt, "\nDefaults:\n"); 1121643f4904SBrian Somers prompt_Printf(arg->prompt, " Label: %s\n", arg->bundle->cfg.label); 1122610b185fSBrian Somers prompt_Printf(arg->prompt, " Auth name: %s\n", 1123610b185fSBrian Somers arg->bundle->cfg.auth.name); 112404eaa58cSBrian Somers prompt_Printf(arg->prompt, " Auto Load: Up after %ds of >= %d packets\n", 112504eaa58cSBrian Somers arg->bundle->cfg.autoload.max.timeout, 112604eaa58cSBrian Somers arg->bundle->cfg.autoload.max.packets); 112704eaa58cSBrian Somers prompt_Printf(arg->prompt, " Down after %ds of <= %d" 112804eaa58cSBrian Somers " packets\n", arg->bundle->cfg.autoload.min.timeout, 112904eaa58cSBrian Somers arg->bundle->cfg.autoload.min.packets); 113004eaa58cSBrian Somers if (arg->bundle->autoload.timer.state == TIMER_RUNNING) 113104eaa58cSBrian Somers prompt_Printf(arg->prompt, " %ds remaining 'till " 113204eaa58cSBrian Somers "a link comes %s\n", 113304eaa58cSBrian Somers bundle_RemainingAutoLoadTime(arg->bundle), 113404eaa58cSBrian Somers arg->bundle->autoload.comingup ? "up" : "down"); 113504eaa58cSBrian Somers else 113604eaa58cSBrian Somers prompt_Printf(arg->prompt, " %srunning with %d" 113704eaa58cSBrian Somers " packets queued\n", arg->bundle->autoload.running ? 113804eaa58cSBrian Somers "" : "not ", ip_QueueLen()); 113904eaa58cSBrian Somers 1140c08717dfSBrian Somers prompt_Printf(arg->prompt, " Idle Timer: "); 1141c08717dfSBrian Somers if (arg->bundle->cfg.idle_timeout) { 1142c08717dfSBrian Somers prompt_Printf(arg->prompt, "%ds", arg->bundle->cfg.idle_timeout); 1143c08717dfSBrian Somers remaining = bundle_RemainingIdleTime(arg->bundle); 1144c08717dfSBrian Somers if (remaining != -1) 1145c08717dfSBrian Somers prompt_Printf(arg->prompt, " (%ds remaining)", remaining); 1146c08717dfSBrian Somers prompt_Printf(arg->prompt, "\n"); 1147c08717dfSBrian Somers } else 1148c08717dfSBrian Somers prompt_Printf(arg->prompt, "disabled\n"); 1149ce828a6eSBrian Somers prompt_Printf(arg->prompt, " MTU: "); 1150ce828a6eSBrian Somers if (arg->bundle->cfg.mtu) 1151ce828a6eSBrian Somers prompt_Printf(arg->prompt, "%d\n", arg->bundle->cfg.mtu); 1152ce828a6eSBrian Somers else 1153ce828a6eSBrian Somers prompt_Printf(arg->prompt, "unspecified\n"); 1154c08717dfSBrian Somers 1155610b185fSBrian Somers prompt_Printf(arg->prompt, " Sticky Routes: %s\n", 1156610b185fSBrian Somers optval(arg->bundle, OPT_SROUTES)); 11571342caedSBrian Somers prompt_Printf(arg->prompt, " ID check: %s\n", 11581342caedSBrian Somers optval(arg->bundle, OPT_IDCHECK)); 11591342caedSBrian Somers prompt_Printf(arg->prompt, " Loopback: %s\n", 11601342caedSBrian Somers optval(arg->bundle, OPT_LOOPBACK)); 11611342caedSBrian Somers prompt_Printf(arg->prompt, " PasswdAuth: %s\n", 11621342caedSBrian Somers optval(arg->bundle, OPT_PASSWDAUTH)); 11631342caedSBrian Somers prompt_Printf(arg->prompt, " Proxy: %s\n", 11641342caedSBrian Somers optval(arg->bundle, OPT_PROXY)); 11651342caedSBrian Somers prompt_Printf(arg->prompt, " Throughput: %s\n", 11661342caedSBrian Somers optval(arg->bundle, OPT_THROUGHPUT)); 1167610b185fSBrian Somers prompt_Printf(arg->prompt, " Utmp Logging: %s\n", 11681342caedSBrian Somers optval(arg->bundle, OPT_UTMP)); 11691342caedSBrian Somers 1170c08717dfSBrian Somers return 0; 1171c08717dfSBrian Somers } 1172c08717dfSBrian Somers 1173ab886ad0SBrian Somers static void 1174ab886ad0SBrian Somers bundle_IdleTimeout(void *v) 1175ab886ad0SBrian Somers { 1176ab886ad0SBrian Somers struct bundle *bundle = (struct bundle *)v; 1177ab886ad0SBrian Somers 1178dd7e2610SBrian Somers log_Printf(LogPHASE, "Idle timer expired.\n"); 117904eaa58cSBrian Somers bundle_StopIdleTimer(bundle); 1180ab886ad0SBrian Somers bundle_Close(bundle, NULL, 1); 1181ab886ad0SBrian Somers } 1182ab886ad0SBrian Somers 1183ab886ad0SBrian Somers /* 1184ab886ad0SBrian Somers * Start Idle timer. If timeout is reached, we call bundle_Close() to 1185ab886ad0SBrian Somers * close LCP and link. 1186ab886ad0SBrian Somers */ 1187ab886ad0SBrian Somers void 1188ab886ad0SBrian Somers bundle_StartIdleTimer(struct bundle *bundle) 1189ab886ad0SBrian Somers { 1190dd7e2610SBrian Somers timer_Stop(&bundle->idle.timer); 119104eaa58cSBrian Somers if ((bundle->phys_type & (PHYS_DEDICATED|PHYS_PERM)) != bundle->phys_type && 119204eaa58cSBrian Somers bundle->cfg.idle_timeout) { 119393ee0ff2SBrian Somers bundle->idle.timer.func = bundle_IdleTimeout; 11943b0f8d2eSBrian Somers bundle->idle.timer.name = "idle"; 119593ee0ff2SBrian Somers bundle->idle.timer.load = bundle->cfg.idle_timeout * SECTICKS; 119693ee0ff2SBrian Somers bundle->idle.timer.arg = bundle; 1197dd7e2610SBrian Somers timer_Start(&bundle->idle.timer); 119893ee0ff2SBrian Somers bundle->idle.done = time(NULL) + bundle->cfg.idle_timeout; 1199ab886ad0SBrian Somers } 1200ab886ad0SBrian Somers } 1201ab886ad0SBrian Somers 1202ab886ad0SBrian Somers void 1203ab886ad0SBrian Somers bundle_SetIdleTimer(struct bundle *bundle, int value) 1204ab886ad0SBrian Somers { 1205ab886ad0SBrian Somers bundle->cfg.idle_timeout = value; 1206ab886ad0SBrian Somers if (bundle_LinkIsUp(bundle)) 1207ab886ad0SBrian Somers bundle_StartIdleTimer(bundle); 1208ab886ad0SBrian Somers } 1209ab886ad0SBrian Somers 1210ab886ad0SBrian Somers void 1211ab886ad0SBrian Somers bundle_StopIdleTimer(struct bundle *bundle) 1212ab886ad0SBrian Somers { 1213dd7e2610SBrian Somers timer_Stop(&bundle->idle.timer); 12144a632c80SBrian Somers bundle->idle.done = 0; 1215ab886ad0SBrian Somers } 1216ab886ad0SBrian Somers 121704eaa58cSBrian Somers static int 1218ab886ad0SBrian Somers bundle_RemainingIdleTime(struct bundle *bundle) 1219ab886ad0SBrian Somers { 122093ee0ff2SBrian Somers if (bundle->idle.done) 122193ee0ff2SBrian Somers return bundle->idle.done - time(NULL); 1222ab886ad0SBrian Somers return -1; 1223ab886ad0SBrian Somers } 12243b0f8d2eSBrian Somers 12253b0f8d2eSBrian Somers int 12263b0f8d2eSBrian Somers bundle_IsDead(struct bundle *bundle) 12273b0f8d2eSBrian Somers { 12283b0f8d2eSBrian Somers return !bundle->links || (bundle->phase == PHASE_DEAD && bundle->CleaningUp); 12293b0f8d2eSBrian Somers } 1230b6217683SBrian Somers 1231565e35e5SBrian Somers static void 123204eaa58cSBrian Somers bundle_LinkAdded(struct bundle *bundle, struct datalink *dl) 123304eaa58cSBrian Somers { 123404eaa58cSBrian Somers bundle->phys_type |= dl->physical->type; 123504eaa58cSBrian Somers if (dl->physical->type == PHYS_DEMAND && 123604eaa58cSBrian Somers bundle->autoload.timer.state == TIMER_STOPPED && 123704eaa58cSBrian Somers bundle->phase == PHASE_NETWORK) 123804eaa58cSBrian Somers bundle->autoload.running = 1; 123904eaa58cSBrian Somers } 124004eaa58cSBrian Somers 124104eaa58cSBrian Somers static void 124204eaa58cSBrian Somers bundle_LinksRemoved(struct bundle *bundle) 1243565e35e5SBrian Somers { 1244565e35e5SBrian Somers struct datalink *dl; 1245565e35e5SBrian Somers 1246565e35e5SBrian Somers bundle->phys_type = 0; 1247565e35e5SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 124804eaa58cSBrian Somers bundle_LinkAdded(bundle, dl); 124904eaa58cSBrian Somers 125004eaa58cSBrian Somers if ((bundle->phys_type & (PHYS_DEDICATED|PHYS_PERM)) == bundle->phys_type) 125104eaa58cSBrian Somers timer_Stop(&bundle->idle.timer); 1252565e35e5SBrian Somers } 1253565e35e5SBrian Somers 125404eaa58cSBrian Somers static struct datalink * 125504eaa58cSBrian Somers bundle_DatalinkLinkout(struct bundle *bundle, struct datalink *dl) 1256cd7bd93aSBrian Somers { 1257cd7bd93aSBrian Somers struct datalink **dlp; 1258cd7bd93aSBrian Somers 1259cd7bd93aSBrian Somers for (dlp = &bundle->links; *dlp; dlp = &(*dlp)->next) 1260cd7bd93aSBrian Somers if (*dlp == dl) { 126104eaa58cSBrian Somers *dlp = dl->next; 126204eaa58cSBrian Somers dl->next = NULL; 126304eaa58cSBrian Somers bundle_LinksRemoved(bundle); 126404eaa58cSBrian Somers return dl; 1265cd7bd93aSBrian Somers } 126604eaa58cSBrian Somers 126704eaa58cSBrian Somers return NULL; 126804eaa58cSBrian Somers } 126904eaa58cSBrian Somers 127004eaa58cSBrian Somers static void 127104eaa58cSBrian Somers bundle_DatalinkLinkin(struct bundle *bundle, struct datalink *dl) 127204eaa58cSBrian Somers { 127304eaa58cSBrian Somers struct datalink **dlp = &bundle->links; 127404eaa58cSBrian Somers 127504eaa58cSBrian Somers while (*dlp) 127604eaa58cSBrian Somers dlp = &(*dlp)->next; 127704eaa58cSBrian Somers 127804eaa58cSBrian Somers *dlp = dl; 127904eaa58cSBrian Somers dl->next = NULL; 128004eaa58cSBrian Somers 128104eaa58cSBrian Somers bundle_LinkAdded(bundle, dl); 1282565e35e5SBrian Somers } 1283565e35e5SBrian Somers 1284565e35e5SBrian Somers void 1285565e35e5SBrian Somers bundle_CleanDatalinks(struct bundle *bundle) 1286565e35e5SBrian Somers { 1287565e35e5SBrian Somers struct datalink **dlp = &bundle->links; 128804eaa58cSBrian Somers int found = 0; 1289565e35e5SBrian Somers 1290565e35e5SBrian Somers while (*dlp) 1291565e35e5SBrian Somers if ((*dlp)->state == DATALINK_CLOSED && 129204eaa58cSBrian Somers (*dlp)->physical->type & (PHYS_DIRECT|PHYS_1OFF)) { 1293565e35e5SBrian Somers *dlp = datalink_Destroy(*dlp); 129404eaa58cSBrian Somers found++; 129504eaa58cSBrian Somers } else 1296565e35e5SBrian Somers dlp = &(*dlp)->next; 129704eaa58cSBrian Somers 129804eaa58cSBrian Somers if (found) 129904eaa58cSBrian Somers bundle_LinksRemoved(bundle); 130004eaa58cSBrian Somers } 130104eaa58cSBrian Somers 130204eaa58cSBrian Somers int 130304eaa58cSBrian Somers bundle_DatalinkClone(struct bundle *bundle, struct datalink *dl, 130404eaa58cSBrian Somers const char *name) 130504eaa58cSBrian Somers { 130604eaa58cSBrian Somers if (bundle2datalink(bundle, name)) { 130704eaa58cSBrian Somers log_Printf(LogWARN, "Clone: %s: name already exists\n", name); 130804eaa58cSBrian Somers return 0; 130904eaa58cSBrian Somers } 131004eaa58cSBrian Somers 131104eaa58cSBrian Somers bundle_DatalinkLinkin(bundle, datalink_Clone(dl, name)); 131204eaa58cSBrian Somers return 1; 131304eaa58cSBrian Somers } 131404eaa58cSBrian Somers 131504eaa58cSBrian Somers void 131604eaa58cSBrian Somers bundle_DatalinkRemove(struct bundle *bundle, struct datalink *dl) 131704eaa58cSBrian Somers { 131804eaa58cSBrian Somers dl = bundle_DatalinkLinkout(bundle, dl); 131904eaa58cSBrian Somers if (dl) 132004eaa58cSBrian Somers datalink_Destroy(dl); 1321cd7bd93aSBrian Somers } 132249052c95SBrian Somers 132349052c95SBrian Somers void 132449052c95SBrian Somers bundle_SetLabel(struct bundle *bundle, const char *label) 132549052c95SBrian Somers { 132649052c95SBrian Somers if (label) 132749052c95SBrian Somers strncpy(bundle->cfg.label, label, sizeof bundle->cfg.label - 1); 132849052c95SBrian Somers else 132949052c95SBrian Somers *bundle->cfg.label = '\0'; 133049052c95SBrian Somers } 133149052c95SBrian Somers 133249052c95SBrian Somers const char * 133349052c95SBrian Somers bundle_GetLabel(struct bundle *bundle) 133449052c95SBrian Somers { 133549052c95SBrian Somers return *bundle->cfg.label ? bundle->cfg.label : NULL; 133649052c95SBrian Somers } 13371fa665f5SBrian Somers 13381fa665f5SBrian Somers void 133996c9bb21SBrian Somers bundle_ReceiveDatalink(struct bundle *bundle, int s, struct sockaddr_un *sun) 13401fa665f5SBrian Somers { 134196c9bb21SBrian Somers char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int)]; 134296c9bb21SBrian Somers struct cmsghdr *cmsg = (struct cmsghdr *)cmsgbuf; 134396c9bb21SBrian Somers struct msghdr msg; 134496c9bb21SBrian Somers struct iovec iov[SCATTER_SEGMENTS]; 1345b7c5748eSBrian Somers struct datalink *dl; 134696c9bb21SBrian Somers int niov, link_fd, expect, f; 13476f384573SBrian Somers 1348dd7e2610SBrian Somers log_Printf(LogPHASE, "Receiving datalink\n"); 13496f384573SBrian Somers 135096c9bb21SBrian Somers /* Create our scatter/gather array */ 135196c9bb21SBrian Somers niov = 1; 135296c9bb21SBrian Somers iov[0].iov_len = strlen(Version) + 1; 135396c9bb21SBrian Somers iov[0].iov_base = (char *)malloc(iov[0].iov_len); 135454cd8e13SBrian Somers if (datalink2iov(NULL, iov, &niov, sizeof iov / sizeof *iov) == -1) { 135554cd8e13SBrian Somers close(s); 13566f384573SBrian Somers return; 135754cd8e13SBrian Somers } 13586f384573SBrian Somers 135996c9bb21SBrian Somers for (f = expect = 0; f < niov; f++) 136096c9bb21SBrian Somers expect += iov[f].iov_len; 136196c9bb21SBrian Somers 136296c9bb21SBrian Somers /* Set up our message */ 136396c9bb21SBrian Somers cmsg->cmsg_len = sizeof cmsgbuf; 136496c9bb21SBrian Somers cmsg->cmsg_level = SOL_SOCKET; 136554cd8e13SBrian Somers cmsg->cmsg_type = 0; 136696c9bb21SBrian Somers 136796c9bb21SBrian Somers memset(&msg, '\0', sizeof msg); 136896c9bb21SBrian Somers msg.msg_name = (caddr_t)sun; 136996c9bb21SBrian Somers msg.msg_namelen = sizeof *sun; 137096c9bb21SBrian Somers msg.msg_iov = iov; 137196c9bb21SBrian Somers msg.msg_iovlen = niov; 137296c9bb21SBrian Somers msg.msg_control = cmsgbuf; 137396c9bb21SBrian Somers msg.msg_controllen = sizeof cmsgbuf; 137496c9bb21SBrian Somers 137596c9bb21SBrian Somers log_Printf(LogDEBUG, "Expecting %d scatter/gather bytes\n", expect); 137696c9bb21SBrian Somers f = expect + 100; 137796c9bb21SBrian Somers setsockopt(s, SOL_SOCKET, SO_RCVBUF, &f, sizeof f); 137896c9bb21SBrian Somers if ((f = recvmsg(s, &msg, MSG_WAITALL)) != expect) { 137996c9bb21SBrian Somers if (f == -1) 138096c9bb21SBrian Somers log_Printf(LogERROR, "Failed recvmsg: %s\n", strerror(errno)); 138196c9bb21SBrian Somers else 138296c9bb21SBrian Somers log_Printf(LogERROR, "Failed recvmsg: Got %d, not %d\n", f, expect); 138396c9bb21SBrian Somers while (niov--) 138496c9bb21SBrian Somers free(iov[niov].iov_base); 138554cd8e13SBrian Somers close(s); 138696c9bb21SBrian Somers return; 138796c9bb21SBrian Somers } 138896c9bb21SBrian Somers 1389c0d9a877SBrian Somers write(s, "!", 1); /* ACK */ 1390c0d9a877SBrian Somers 139154cd8e13SBrian Somers if (cmsg->cmsg_type == SCM_RIGHTS) { 139254cd8e13SBrian Somers /* We've successfully received an open file descriptor through our socket */ 139354cd8e13SBrian Somers log_Printf(LogDEBUG, "Receiving non-tty device\n"); 139454cd8e13SBrian Somers link_fd = *(int *)CMSG_DATA(cmsg); 139554cd8e13SBrian Somers } else { 139654cd8e13SBrian Somers /* It's a ``controlling'' tty device via CATPROG */ 139754cd8e13SBrian Somers log_Printf(LogDEBUG, "Receiving tty device\n"); 139854cd8e13SBrian Somers link_fd = dup(s); 139954cd8e13SBrian Somers fcntl(link_fd, F_SETFL, fcntl(link_fd, F_GETFL, 0) | O_NONBLOCK); 140054cd8e13SBrian Somers } 140154cd8e13SBrian Somers 140296c9bb21SBrian Somers if (strncmp(Version, iov[0].iov_base, iov[0].iov_len)) { 140396c9bb21SBrian Somers log_Printf(LogWARN, "Cannot receive datalink, incorrect version" 140496c9bb21SBrian Somers " (\"%.*s\", not \"%s\")\n", (int)iov[0].iov_len, 140596c9bb21SBrian Somers iov[0].iov_base, Version); 140696c9bb21SBrian Somers close(link_fd); 140796c9bb21SBrian Somers while (niov--) 140896c9bb21SBrian Somers free(iov[niov].iov_base); 140996c9bb21SBrian Somers return; 141096c9bb21SBrian Somers } 141196c9bb21SBrian Somers 141296c9bb21SBrian Somers niov = 1; 1413b7c5748eSBrian Somers dl = iov2datalink(bundle, iov, &niov, sizeof iov / sizeof *iov, link_fd); 1414b7c5748eSBrian Somers if (dl) { 141504eaa58cSBrian Somers bundle_DatalinkLinkin(bundle, dl); 1416b7c5748eSBrian Somers datalink_AuthOk(dl); 141796c9bb21SBrian Somers } else 141896c9bb21SBrian Somers close(link_fd); 141996c9bb21SBrian Somers 142096c9bb21SBrian Somers free(iov[0].iov_base); 142154cd8e13SBrian Somers close(s); 14221fa665f5SBrian Somers } 14231fa665f5SBrian Somers 14241fa665f5SBrian Somers void 142596c9bb21SBrian Somers bundle_SendDatalink(struct datalink *dl, int s, struct sockaddr_un *sun) 14261fa665f5SBrian Somers { 1427c0d9a877SBrian Somers char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int)], ack; 142896c9bb21SBrian Somers struct cmsghdr *cmsg = (struct cmsghdr *)cmsgbuf; 142996c9bb21SBrian Somers struct msghdr msg; 143096c9bb21SBrian Somers struct iovec iov[SCATTER_SEGMENTS]; 143196c9bb21SBrian Somers int niov, link_fd, f, expect; 14326f384573SBrian Somers 1433dd7e2610SBrian Somers log_Printf(LogPHASE, "Transmitting datalink %s\n", dl->name); 14346f384573SBrian Somers 143504eaa58cSBrian Somers bundle_LinkClosed(dl->bundle, dl); 14360f2f3eb3SBrian Somers bundle_DatalinkLinkout(dl->bundle, dl); 1437ea722969SBrian Somers 143896c9bb21SBrian Somers /* Build our scatter/gather array */ 143996c9bb21SBrian Somers iov[0].iov_len = strlen(Version) + 1; 144096c9bb21SBrian Somers iov[0].iov_base = strdup(Version); 144196c9bb21SBrian Somers niov = 1; 14426f384573SBrian Somers 144396c9bb21SBrian Somers link_fd = datalink2iov(dl, iov, &niov, sizeof iov / sizeof *iov); 14446f384573SBrian Somers 14456f384573SBrian Somers if (link_fd != -1) { 144696c9bb21SBrian Somers memset(&msg, '\0', sizeof msg); 144754cd8e13SBrian Somers 144896c9bb21SBrian Somers msg.msg_name = (caddr_t)sun; 144996c9bb21SBrian Somers msg.msg_namelen = sizeof *sun; 145096c9bb21SBrian Somers msg.msg_iov = iov; 145196c9bb21SBrian Somers msg.msg_iovlen = niov; 145254cd8e13SBrian Somers 145354cd8e13SBrian Somers if (link_fd == STDIN_FILENO && isatty(link_fd)) { 145454cd8e13SBrian Somers /* 145554cd8e13SBrian Somers * We can't transfer this tty descriptor. If we do, then once the 145654cd8e13SBrian Somers * session leader exits, the descriptor becomes unusable by the 145754cd8e13SBrian Somers * other ppp process. Instead, we'll fork() two `/bin/cat' 145854cd8e13SBrian Somers * processes..... 145954cd8e13SBrian Somers */ 146054cd8e13SBrian Somers msg.msg_control = NULL; 146154cd8e13SBrian Somers msg.msg_controllen = 0; 146254cd8e13SBrian Somers } else { 146354cd8e13SBrian Somers cmsg->cmsg_len = sizeof cmsgbuf; 146454cd8e13SBrian Somers cmsg->cmsg_level = SOL_SOCKET; 146554cd8e13SBrian Somers cmsg->cmsg_type = SCM_RIGHTS; 146654cd8e13SBrian Somers *(int *)CMSG_DATA(cmsg) = link_fd; 146796c9bb21SBrian Somers msg.msg_control = cmsgbuf; 146896c9bb21SBrian Somers msg.msg_controllen = sizeof cmsgbuf; 146954cd8e13SBrian Somers } 147047723d29SBrian Somers 147196c9bb21SBrian Somers for (f = expect = 0; f < niov; f++) 147296c9bb21SBrian Somers expect += iov[f].iov_len; 147347723d29SBrian Somers 147496c9bb21SBrian Somers log_Printf(LogDEBUG, "Sending %d bytes in scatter/gather array\n", expect); 147547723d29SBrian Somers 147696c9bb21SBrian Somers f = expect + SOCKET_OVERHEAD; 147796c9bb21SBrian Somers setsockopt(s, SOL_SOCKET, SO_SNDBUF, &f, sizeof f); 147896c9bb21SBrian Somers if (sendmsg(s, &msg, 0) == -1) 147996c9bb21SBrian Somers log_Printf(LogERROR, "Failed sendmsg: %s\n", strerror(errno)); 1480c0d9a877SBrian Somers /* We must get the ACK before closing the descriptor ! */ 1481c0d9a877SBrian Somers read(s, &ack, 1); 148254cd8e13SBrian Somers 148354cd8e13SBrian Somers if (link_fd == STDIN_FILENO && isatty(link_fd)) { 148454cd8e13SBrian Somers /* We use `/bin/cat' to keep the tty session id */ 148554cd8e13SBrian Somers pid_t pid; 148654cd8e13SBrian Somers int status, len, fd; 148754cd8e13SBrian Somers char name[50], *tname; 148854cd8e13SBrian Somers 148954cd8e13SBrian Somers tname = ttyname(link_fd); 149054cd8e13SBrian Somers len = strlen(_PATH_DEV); 149154cd8e13SBrian Somers if (!strncmp(tname, _PATH_DEV, len)) 149254cd8e13SBrian Somers tname += len; 149354cd8e13SBrian Somers 149454cd8e13SBrian Somers log_Printf(LogPHASE, "%s: Using twin %s invocations\n", tname, CATPROG); 149554cd8e13SBrian Somers 149654cd8e13SBrian Somers switch ((pid = fork())) { 149754cd8e13SBrian Somers case -1: 149854cd8e13SBrian Somers log_Printf(LogERROR, "fork: %s\n", strerror(errno)); 149954cd8e13SBrian Somers break; 150054cd8e13SBrian Somers case 0: 150154cd8e13SBrian Somers if (fork()) /* Don't want to belong to the parent any more */ 150254cd8e13SBrian Somers exit(0); 150354cd8e13SBrian Somers setsid(); 150454cd8e13SBrian Somers log_Printf(LogPHASE, "%d: Continuing without controlling terminal\n", 150554cd8e13SBrian Somers (int)getpid()); 150654cd8e13SBrian Somers break; 150754cd8e13SBrian Somers default: 150854cd8e13SBrian Somers /* Parent does the execs .... */ 150954cd8e13SBrian Somers timer_TermService(); 151054cd8e13SBrian Somers waitpid(pid, &status, 0); 151154cd8e13SBrian Somers 151254cd8e13SBrian Somers fcntl(3, F_SETFD, 1); /* Set close-on-exec flag */ 151354cd8e13SBrian Somers fcntl(s, F_SETFL, fcntl(s, F_GETFL, 0) & ~O_NONBLOCK); 151454cd8e13SBrian Somers fcntl(link_fd, F_SETFL, fcntl(link_fd, F_GETFL, 0) & ~O_NONBLOCK); 151554cd8e13SBrian Somers s = fcntl(s, F_DUPFD, 3); 151654cd8e13SBrian Somers link_fd = fcntl(link_fd, F_DUPFD, 3); 151754cd8e13SBrian Somers dup2(open(_PATH_DEVNULL, O_WRONLY|O_APPEND), STDERR_FILENO); 151854cd8e13SBrian Somers 151954cd8e13SBrian Somers setuid(geteuid()); 152054cd8e13SBrian Somers 152154cd8e13SBrian Somers switch (fork()) { 152254cd8e13SBrian Somers case -1: 152354cd8e13SBrian Somers _exit(0); 152454cd8e13SBrian Somers break; 152554cd8e13SBrian Somers case 0: 152654cd8e13SBrian Somers dup2(link_fd, STDIN_FILENO); 152754cd8e13SBrian Somers dup2(s, STDOUT_FILENO); 152854cd8e13SBrian Somers snprintf(name, sizeof name, "%s <- %s", dl->name, tname); 152954cd8e13SBrian Somers break; 153054cd8e13SBrian Somers default: 153154cd8e13SBrian Somers dup2(s, STDIN_FILENO); 153254cd8e13SBrian Somers dup2(link_fd, STDOUT_FILENO); 153354cd8e13SBrian Somers snprintf(name, sizeof name, "%s -> %s", dl->name, tname); 153454cd8e13SBrian Somers break; 153554cd8e13SBrian Somers } 153654cd8e13SBrian Somers signal(SIGPIPE, SIG_DFL); 153754cd8e13SBrian Somers signal(SIGALRM, SIG_DFL); 153854cd8e13SBrian Somers for (fd = getdtablesize(); fd > 2; fd--) 153954cd8e13SBrian Somers close(fd); 154054cd8e13SBrian Somers execl(CATPROG, name, NULL); 154154cd8e13SBrian Somers _exit(0); 154254cd8e13SBrian Somers break; 154354cd8e13SBrian Somers } 154454cd8e13SBrian Somers } 154554cd8e13SBrian Somers close(s); 154696c9bb21SBrian Somers close(link_fd); 154747723d29SBrian Somers } 154896c9bb21SBrian Somers 154996c9bb21SBrian Somers while (niov--) 155096c9bb21SBrian Somers free(iov[niov].iov_base); 15511fa665f5SBrian Somers } 1552dd0645c5SBrian Somers 1553dd0645c5SBrian Somers int 155458d55334SBrian Somers bundle_RenameDatalink(struct bundle *bundle, struct datalink *ndl, 155558d55334SBrian Somers const char *name) 155658d55334SBrian Somers { 155758d55334SBrian Somers struct datalink *dl; 155858d55334SBrian Somers 155958d55334SBrian Somers if (!strcasecmp(ndl->name, name)) 156058d55334SBrian Somers return 1; 156158d55334SBrian Somers 156258d55334SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 156358d55334SBrian Somers if (!strcasecmp(dl->name, name)) 156458d55334SBrian Somers return 0; 156558d55334SBrian Somers 156658d55334SBrian Somers datalink_Rename(ndl, name); 156758d55334SBrian Somers return 1; 156858d55334SBrian Somers } 156958d55334SBrian Somers 157058d55334SBrian Somers int 1571dd0645c5SBrian Somers bundle_SetMode(struct bundle *bundle, struct datalink *dl, int mode) 1572dd0645c5SBrian Somers { 1573dd0645c5SBrian Somers int omode; 1574dd0645c5SBrian Somers 1575dd0645c5SBrian Somers omode = dl->physical->type; 1576dd0645c5SBrian Somers if (omode == mode) 1577dd0645c5SBrian Somers return 1; 1578dd0645c5SBrian Somers 1579dd0645c5SBrian Somers if (mode == PHYS_DEMAND && !(bundle->phys_type & PHYS_DEMAND)) 1580dd0645c5SBrian Somers /* Changing to demand-dial mode */ 1581dd0645c5SBrian Somers if (bundle->ncp.ipcp.peer_ip.s_addr == INADDR_ANY) { 1582dd0645c5SBrian Somers log_Printf(LogWARN, "You must `set ifaddr' before changing mode to %s\n", 1583dd0645c5SBrian Somers mode2Nam(mode)); 1584dd0645c5SBrian Somers return 0; 1585dd0645c5SBrian Somers } 1586dd0645c5SBrian Somers 1587dd0645c5SBrian Somers if (!datalink_SetMode(dl, mode)) 1588dd0645c5SBrian Somers return 0; 1589dd0645c5SBrian Somers 1590dd0645c5SBrian Somers if (mode == PHYS_DEMAND && !(bundle->phys_type & PHYS_DEMAND)) 1591dd0645c5SBrian Somers ipcp_InterfaceUp(&bundle->ncp.ipcp); 1592dd0645c5SBrian Somers 159304eaa58cSBrian Somers /* Regenerate phys_type and adjust autoload & idle timers */ 159404eaa58cSBrian Somers bundle_LinksRemoved(bundle); 1595dd0645c5SBrian Somers 1596dd0645c5SBrian Somers if (omode == PHYS_DEMAND && !(bundle->phys_type & PHYS_DEMAND)) 1597dd0645c5SBrian Somers /* Changing from demand-dial mode */ 1598dd0645c5SBrian Somers ipcp_CleanInterface(&bundle->ncp.ipcp); 1599dd0645c5SBrian Somers 1600dd0645c5SBrian Somers return 1; 1601dd0645c5SBrian Somers } 1602