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 * 2697d92980SPeter Wemm * $FreeBSD$ 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> 333a7b6d76SBrian Somers #include <net/if_tun.h> /* For TUNS* ioctls */ 347a6f8720SBrian Somers #include <net/route.h> 35eaa4df37SBrian Somers #include <netinet/in_systm.h> 36eaa4df37SBrian Somers #include <netinet/ip.h> 371fa665f5SBrian Somers #include <sys/un.h> 387a6f8720SBrian Somers 397a6f8720SBrian Somers #include <errno.h> 407a6f8720SBrian Somers #include <fcntl.h> 41cf0a3940SBrian Somers #ifdef __OpenBSD__ 42cf0a3940SBrian Somers #include <util.h> 43cf0a3940SBrian Somers #else 44cf0a3940SBrian Somers #include <libutil.h> 45cf0a3940SBrian Somers #endif 4647723d29SBrian Somers #include <paths.h> 476eafd353SBrian Somers #include <stdarg.h> 487a6f8720SBrian Somers #include <stdio.h> 496f384573SBrian Somers #include <stdlib.h> 507a6f8720SBrian Somers #include <string.h> 5196c9bb21SBrian Somers #include <sys/uio.h> 5254cd8e13SBrian Somers #include <sys/wait.h> 537a6f8720SBrian Somers #include <termios.h> 547a6f8720SBrian Somers #include <unistd.h> 557a6f8720SBrian Somers 565d9e6103SBrian Somers #include "layer.h" 57c9e11a11SBrian Somers #include "defs.h" 587a6f8720SBrian Somers #include "command.h" 597a6f8720SBrian Somers #include "mbuf.h" 607a6f8720SBrian Somers #include "log.h" 617a6f8720SBrian Somers #include "id.h" 627a6f8720SBrian Somers #include "timer.h" 637a6f8720SBrian Somers #include "fsm.h" 647a6f8720SBrian Somers #include "iplist.h" 65879ed6faSBrian Somers #include "lqr.h" 66455aabc3SBrian Somers #include "hdlc.h" 677a6f8720SBrian Somers #include "throughput.h" 68eaa4df37SBrian Somers #include "slcompress.h" 6930949fd4SBrian Somers #include "ncpaddr.h" 7030949fd4SBrian Somers #include "ip.h" 717a6f8720SBrian Somers #include "ipcp.h" 725ca5389aSBrian Somers #include "filter.h" 732f786681SBrian Somers #include "descriptor.h" 747a6f8720SBrian Somers #include "route.h" 757a6f8720SBrian Somers #include "lcp.h" 767a6f8720SBrian Somers #include "ccp.h" 773b0f8d2eSBrian Somers #include "link.h" 783b0f8d2eSBrian Somers #include "mp.h" 79972a1bcfSBrian Somers #ifndef NORADIUS 80972a1bcfSBrian Somers #include "radius.h" 81972a1bcfSBrian Somers #endif 8230949fd4SBrian Somers #include "ipv6cp.h" 8330949fd4SBrian Somers #include "ncp.h" 843b0f8d2eSBrian Somers #include "bundle.h" 85455aabc3SBrian Somers #include "async.h" 86455aabc3SBrian Somers #include "physical.h" 87455aabc3SBrian Somers #include "auth.h" 885d9e6103SBrian Somers #include "proto.h" 89455aabc3SBrian Somers #include "chap.h" 90455aabc3SBrian Somers #include "tun.h" 9185b542cfSBrian Somers #include "prompt.h" 923006ec67SBrian Somers #include "chat.h" 9392b09558SBrian Somers #include "cbcp.h" 943006ec67SBrian Somers #include "datalink.h" 958fa6ebe4SBrian Somers #include "iface.h" 9674457d3dSBrian Somers #include "server.h" 97971abb29SBrian Somers #include "probe.h" 98fb11a9c2SBrian Somers #ifndef NODES 99019d32bfSBrian Somers #include "mppe.h" 10003a2501aSBrian Somers #endif 1017a6f8720SBrian Somers 10291cbd2eeSBrian Somers #define SCATTER_SEGMENTS 7 /* version, datalink, name, physical, 10391cbd2eeSBrian Somers throughput, throughput, device */ 10487c3786eSBrian Somers 1052cb305afSBrian Somers #define SEND_MAXFD 3 /* Max file descriptors passed through 10687c3786eSBrian Somers the local domain socket */ 10796c9bb21SBrian Somers 10804eaa58cSBrian Somers static int bundle_RemainingIdleTime(struct bundle *); 10904eaa58cSBrian Somers 110182c898aSBrian Somers static const char * const PhaseNames[] = { 111455aabc3SBrian Somers "Dead", "Establish", "Authenticate", "Network", "Terminate" 112455aabc3SBrian Somers }; 113455aabc3SBrian Somers 114455aabc3SBrian Somers const char * 115455aabc3SBrian Somers bundle_PhaseName(struct bundle *bundle) 1167a6f8720SBrian Somers { 117455aabc3SBrian Somers return bundle->phase <= PHASE_TERMINATE ? 118455aabc3SBrian Somers PhaseNames[bundle->phase] : "unknown"; 1197a6f8720SBrian Somers } 1207a6f8720SBrian Somers 121455aabc3SBrian Somers void 1225563ebdeSBrian Somers bundle_NewPhase(struct bundle *bundle, u_int new) 123455aabc3SBrian Somers { 124aef795ccSBrian Somers if (new == bundle->phase) 125aef795ccSBrian Somers return; 126aef795ccSBrian Somers 127e2ebb036SBrian Somers if (new <= PHASE_TERMINATE) 128dd7e2610SBrian Somers log_Printf(LogPHASE, "bundle: %s\n", PhaseNames[new]); 1297a6f8720SBrian Somers 130455aabc3SBrian Somers switch (new) { 131455aabc3SBrian Somers case PHASE_DEAD: 132455aabc3SBrian Somers bundle->phase = new; 133fb11a9c2SBrian Somers #ifndef NODES 134019d32bfSBrian Somers MPPE_MasterKeyValid = 0; 13564602637SBrian Somers #endif 136019d32bfSBrian Somers log_DisplayPrompts(); 137455aabc3SBrian Somers break; 138455aabc3SBrian Somers 139455aabc3SBrian Somers case PHASE_ESTABLISH: 140455aabc3SBrian Somers bundle->phase = new; 141455aabc3SBrian Somers break; 142455aabc3SBrian Somers 143455aabc3SBrian Somers case PHASE_AUTHENTICATE: 144455aabc3SBrian Somers bundle->phase = new; 1450f2f3eb3SBrian Somers log_DisplayPrompts(); 146455aabc3SBrian Somers break; 147455aabc3SBrian Somers 148455aabc3SBrian Somers case PHASE_NETWORK: 14930949fd4SBrian Somers if (ncp_fsmStart(&bundle->ncp, bundle)) { 150673903ecSBrian Somers bundle->phase = new; 1510f2f3eb3SBrian Somers log_DisplayPrompts(); 15230949fd4SBrian Somers } else { 15330949fd4SBrian Somers log_Printf(LogPHASE, "bundle: All NCPs are disabled\n"); 15430949fd4SBrian Somers bundle_Close(bundle, NULL, CLOSE_STAYDOWN); 15530949fd4SBrian Somers } 156673903ecSBrian Somers break; 157455aabc3SBrian Somers 158455aabc3SBrian Somers case PHASE_TERMINATE: 159455aabc3SBrian Somers bundle->phase = new; 160673903ecSBrian Somers mp_Down(&bundle->ncp.mp); 1610f2f3eb3SBrian Somers log_DisplayPrompts(); 162455aabc3SBrian Somers break; 1637a6f8720SBrian Somers } 1647a6f8720SBrian Somers } 1657a6f8720SBrian Somers 1666d666775SBrian Somers static void 167057f1760SBrian Somers bundle_LayerStart(void *v __unused, struct fsm *fp __unused) 1687a6f8720SBrian Somers { 1693006ec67SBrian Somers /* The given FSM is about to start up ! */ 1707a6f8720SBrian Somers } 1717a6f8720SBrian Somers 1725cf4388bSBrian Somers 173b42135deSBrian Somers void 1745cf4388bSBrian Somers bundle_Notify(struct bundle *bundle, char c) 1755cf4388bSBrian Somers { 1765cf4388bSBrian Somers if (bundle->notify.fd != -1) { 177b42135deSBrian Somers int ret; 178b42135deSBrian Somers 179b42135deSBrian Somers ret = write(bundle->notify.fd, &c, 1); 180b42135deSBrian Somers if (c != EX_REDIAL && c != EX_RECONNECT) { 181b42135deSBrian Somers if (ret == 1) 182b42135deSBrian Somers log_Printf(LogCHAT, "Parent notified of %s\n", 1835a83ad1eSBrian Somers c == EX_NORMAL ? "success" : "failure"); 1845cf4388bSBrian Somers else 185b42135deSBrian Somers log_Printf(LogERROR, "Failed to notify parent of success\n"); 1865cf4388bSBrian Somers close(bundle->notify.fd); 1875cf4388bSBrian Somers bundle->notify.fd = -1; 188b42135deSBrian Somers } else if (ret == 1) 189b42135deSBrian Somers log_Printf(LogCHAT, "Parent notified of %s\n", ex_desc(c)); 190b42135deSBrian Somers else 191b42135deSBrian Somers log_Printf(LogERROR, "Failed to notify parent of %s\n", ex_desc(c)); 1925cf4388bSBrian Somers } 1935cf4388bSBrian Somers } 1943b0f8d2eSBrian Somers 1956d666775SBrian Somers static void 1966f8e9f0aSBrian Somers bundle_ClearQueues(void *v) 1976f8e9f0aSBrian Somers { 1986f8e9f0aSBrian Somers struct bundle *bundle = (struct bundle *)v; 1996f8e9f0aSBrian Somers struct datalink *dl; 2006f8e9f0aSBrian Somers 2016f8e9f0aSBrian Somers log_Printf(LogPHASE, "Clearing choked output queue\n"); 2026f8e9f0aSBrian Somers timer_Stop(&bundle->choked.timer); 2036f8e9f0aSBrian Somers 2046f8e9f0aSBrian Somers /* 2056f8e9f0aSBrian Somers * Emergency time: 2066f8e9f0aSBrian Somers * 2076f8e9f0aSBrian Somers * We've had a full queue for PACKET_DEL_SECS seconds without being 2086f8e9f0aSBrian Somers * able to get rid of any of the packets. We've probably given up 2096f8e9f0aSBrian Somers * on the redials at this point, and the queued data has almost 2106f8e9f0aSBrian Somers * definitely been timed out by the layer above. As this is preventing 2116f8e9f0aSBrian Somers * us from reading the TUN_NAME device (we don't want to buffer stuff 2126f8e9f0aSBrian Somers * indefinitely), we may as well nuke this data and start with a clean 2136f8e9f0aSBrian Somers * slate ! 2146f8e9f0aSBrian Somers * 2156f8e9f0aSBrian Somers * Unfortunately, this has the side effect of shafting any compression 2166f8e9f0aSBrian Somers * dictionaries in use (causing the relevant RESET_REQ/RESET_ACK). 2176f8e9f0aSBrian Somers */ 2186f8e9f0aSBrian Somers 21930949fd4SBrian Somers ncp_DeleteQueues(&bundle->ncp); 2206f8e9f0aSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 2216f8e9f0aSBrian Somers physical_DeleteQueue(dl->physical); 2226f8e9f0aSBrian Somers } 2236f8e9f0aSBrian Somers 2246f8e9f0aSBrian Somers static void 225ff0f9439SBrian Somers bundle_LinkAdded(struct bundle *bundle, struct datalink *dl) 226ff0f9439SBrian Somers { 227ff0f9439SBrian Somers bundle->phys_type.all |= dl->physical->type; 228ff0f9439SBrian Somers if (dl->state == DATALINK_OPEN) 229ff0f9439SBrian Somers bundle->phys_type.open |= dl->physical->type; 230ff0f9439SBrian Somers 231bf1eaec5SBrian Somers #ifndef NORADIUS 232bf1eaec5SBrian Somers if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) 233bf1eaec5SBrian Somers != bundle->phys_type.open && bundle->session.timer.state == TIMER_STOPPED) 234bf1eaec5SBrian Somers if (bundle->radius.sessiontime) 235bf1eaec5SBrian Somers bundle_StartSessionTimer(bundle, 0); 236bf1eaec5SBrian Somers #endif 237bf1eaec5SBrian Somers 238ff0f9439SBrian Somers if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) 239ff0f9439SBrian Somers != bundle->phys_type.open && bundle->idle.timer.state == TIMER_STOPPED) 240ff0f9439SBrian Somers /* We may need to start our idle timer */ 2410a4b6c5cSBrian Somers bundle_StartIdleTimer(bundle, 0); 242ff0f9439SBrian Somers } 243ff0f9439SBrian Somers 24492b09558SBrian Somers void 245ff0f9439SBrian Somers bundle_LinksRemoved(struct bundle *bundle) 246ff0f9439SBrian Somers { 247ff0f9439SBrian Somers struct datalink *dl; 248ff0f9439SBrian Somers 249ff0f9439SBrian Somers bundle->phys_type.all = bundle->phys_type.open = 0; 250ff0f9439SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 251ff0f9439SBrian Somers bundle_LinkAdded(bundle, dl); 252ff0f9439SBrian Somers 253ab2de065SBrian Somers bundle_CalculateBandwidth(bundle); 254ab2de065SBrian Somers mp_CheckAutoloadTimer(&bundle->ncp.mp); 255ab2de065SBrian Somers 256ff0f9439SBrian Somers if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) 257bf1eaec5SBrian Somers == bundle->phys_type.open) { 258bf1eaec5SBrian Somers #ifndef NORADIUS 259bf1eaec5SBrian Somers if (bundle->radius.sessiontime) 260bf1eaec5SBrian Somers bundle_StopSessionTimer(bundle); 261bf1eaec5SBrian Somers #endif 262ff0f9439SBrian Somers bundle_StopIdleTimer(bundle); 263ff0f9439SBrian Somers } 264bf1eaec5SBrian Somers } 26504eaa58cSBrian Somers 26604eaa58cSBrian Somers static void 2676f384573SBrian Somers bundle_LayerUp(void *v, struct fsm *fp) 2687a6f8720SBrian Somers { 2693006ec67SBrian Somers /* 2703006ec67SBrian Somers * The given fsm is now up 271ab2de065SBrian Somers * If it's an LCP, adjust our phys_mode.open value and check the 272ab2de065SBrian Somers * autoload timer. 273ab2de065SBrian Somers * If it's the first NCP, calculate our bandwidth 274dade2407SBrian Somers * If it's the first NCP, set our ``upat'' time 2753b0f8d2eSBrian Somers * If it's the first NCP, start the idle timer. 276ab2de065SBrian Somers * If it's an NCP, tell our -background parent to go away. 277ab2de065SBrian Somers * If it's the first NCP, start the autoload timer 2783006ec67SBrian Somers */ 2796f384573SBrian Somers struct bundle *bundle = (struct bundle *)v; 2806d666775SBrian Somers 2815563ebdeSBrian Somers if (fp->proto == PROTO_LCP) { 282ff0f9439SBrian Somers struct physical *p = link2physical(fp->link); 283ff0f9439SBrian Somers 284ff0f9439SBrian Somers bundle_LinkAdded(bundle, p->dl); 285ab2de065SBrian Somers mp_CheckAutoloadTimer(&bundle->ncp.mp); 28630949fd4SBrian Somers } else if (isncp(fp->proto)) { 28730949fd4SBrian Somers if (ncp_LayersOpen(&fp->bundle->ncp) == 1) { 288ab2de065SBrian Somers bundle_CalculateBandwidth(fp->bundle); 289dade2407SBrian Somers time(&bundle->upat); 290bf1eaec5SBrian Somers #ifndef NORADIUS 291bf1eaec5SBrian Somers if (bundle->radius.sessiontime) 292bf1eaec5SBrian Somers bundle_StartSessionTimer(bundle, 0); 293bf1eaec5SBrian Somers #endif 2940a4b6c5cSBrian Somers bundle_StartIdleTimer(bundle, 0); 295ab2de065SBrian Somers mp_CheckAutoloadTimer(&fp->bundle->ncp.mp); 29630949fd4SBrian Somers } 29730949fd4SBrian Somers bundle_Notify(bundle, EX_NORMAL); 2986301d506SBrian Somers } else if (fp->proto == PROTO_CCP) 2996301d506SBrian Somers bundle_CalculateBandwidth(fp->bundle); /* Against ccp_MTUOverhead */ 300ab886ad0SBrian Somers } 3017a6f8720SBrian Somers 3026d666775SBrian Somers static void 3036d666775SBrian Somers bundle_LayerDown(void *v, struct fsm *fp) 3046d666775SBrian Somers { 3056d666775SBrian Somers /* 3066d666775SBrian Somers * The given FSM has been told to come down. 307ab886ad0SBrian Somers * If it's our last NCP, stop the idle timer. 308dade2407SBrian Somers * If it's our last NCP, clear our ``upat'' value. 309ab2de065SBrian Somers * If it's our last NCP, stop the autoload timer 310ff0f9439SBrian Somers * If it's an LCP, adjust our phys_type.open value and any timers. 31186b1f0d7SBrian Somers * If it's an LCP and we're in multilink mode, adjust our tun 312c8f30703SBrian Somers * If it's the last LCP, down all NCPs 31386b1f0d7SBrian Somers * speed and make sure our minimum sequence number is adjusted. 3146d666775SBrian Somers */ 315ab886ad0SBrian Somers 316ab886ad0SBrian Somers struct bundle *bundle = (struct bundle *)v; 317ab886ad0SBrian Somers 31830949fd4SBrian Somers if (isncp(fp->proto)) { 31930949fd4SBrian Somers if (ncp_LayersOpen(&fp->bundle->ncp) == 0) { 320bf1eaec5SBrian Somers #ifndef NORADIUS 321bf1eaec5SBrian Somers if (bundle->radius.sessiontime) 322bf1eaec5SBrian Somers bundle_StopSessionTimer(bundle); 323bf1eaec5SBrian Somers #endif 324ab886ad0SBrian Somers bundle_StopIdleTimer(bundle); 325dade2407SBrian Somers bundle->upat = 0; 326ab2de065SBrian Somers mp_StopAutoloadTimer(&bundle->ncp.mp); 32730949fd4SBrian Somers } 328ab2de065SBrian Somers } else if (fp->proto == PROTO_LCP) { 3293b0f8d2eSBrian Somers struct datalink *dl; 33086b1f0d7SBrian Somers struct datalink *lost; 331c8f30703SBrian Somers int others_active; 332c8f30703SBrian Somers 333c8f30703SBrian Somers bundle_LinksRemoved(bundle); /* adjust timers & phys_type values */ 3343b0f8d2eSBrian Somers 33586b1f0d7SBrian Somers lost = NULL; 336c8f30703SBrian Somers others_active = 0; 337c8f30703SBrian Somers for (dl = bundle->links; dl; dl = dl->next) { 33886b1f0d7SBrian Somers if (fp == &dl->physical->link.lcp.fsm) 33986b1f0d7SBrian Somers lost = dl; 340c8f30703SBrian Somers else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) 341c8f30703SBrian Somers others_active++; 342c8f30703SBrian Somers } 34386b1f0d7SBrian Somers 344c8f30703SBrian Somers if (bundle->ncp.mp.active) { 345ab2de065SBrian Somers bundle_CalculateBandwidth(bundle); 34686b1f0d7SBrian Somers 34786b1f0d7SBrian Somers if (lost) 34886b1f0d7SBrian Somers mp_LinkLost(&bundle->ncp.mp, lost); 34986b1f0d7SBrian Somers else 350a33b2ef7SBrian Somers log_Printf(LogALERT, "Oops, lost an unrecognised datalink (%s) !\n", 35186b1f0d7SBrian Somers fp->link->name); 3523b0f8d2eSBrian Somers } 353c8f30703SBrian Somers 354356bf92dSBrian Somers if (!others_active) { 355c8f30703SBrian Somers /* Down the NCPs. We don't expect to get fsm_Close()d ourself ! */ 35630949fd4SBrian Somers ncp2initial(&bundle->ncp); 357356bf92dSBrian Somers mp_Down(&bundle->ncp.mp); 358356bf92dSBrian Somers } 3596d666775SBrian Somers } 360ff0f9439SBrian Somers } 3616d666775SBrian Somers 3626d666775SBrian Somers static void 3636d666775SBrian Somers bundle_LayerFinish(void *v, struct fsm *fp) 3646d666775SBrian Somers { 3656d666775SBrian Somers /* The given fsm is now down (fp cannot be NULL) 3666d666775SBrian Somers * 367dd7e2610SBrian Somers * If it's the last NCP, fsm_Close all LCPs 368356bf92dSBrian Somers * If it's the last NCP, bring any MP layer down 3696d666775SBrian Somers */ 3706d666775SBrian Somers 3716d666775SBrian Somers struct bundle *bundle = (struct bundle *)v; 3726d666775SBrian Somers struct datalink *dl; 3736d666775SBrian Somers 37430949fd4SBrian Somers if (isncp(fp->proto) && !ncp_LayersUnfinished(&bundle->ncp)) { 37526afeaa2SBrian Somers if (bundle_Phase(bundle) != PHASE_DEAD) 37625092092SBrian Somers bundle_NewPhase(bundle, PHASE_TERMINATE); 3776d666775SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 378c8f30703SBrian Somers if (dl->state == DATALINK_OPEN) 379d4d5d2f8SBrian Somers datalink_Close(dl, CLOSE_STAYDOWN); 38009206a6fSBrian Somers fsm2initial(fp); 381356bf92dSBrian Somers mp_Down(&bundle->ncp.mp); 3823b0f8d2eSBrian Somers } 3833b0f8d2eSBrian Somers } 3846d666775SBrian Somers 3857a6f8720SBrian Somers void 3869c81b87dSBrian Somers bundle_Close(struct bundle *bundle, const char *name, int how) 3877a6f8720SBrian Somers { 388455aabc3SBrian Somers /* 3893006ec67SBrian Somers * Please close the given datalink. 390dd7e2610SBrian Somers * If name == NULL or name is the last datalink, fsm_Close all NCPs 3916f384573SBrian Somers * (except our MP) 3923b0f8d2eSBrian Somers * If it isn't the last datalink, just Close that datalink. 393455aabc3SBrian Somers */ 3947a6f8720SBrian Somers 3953b0f8d2eSBrian Somers struct datalink *dl, *this_dl; 3963b0f8d2eSBrian Somers int others_active; 3973006ec67SBrian Somers 3983b0f8d2eSBrian Somers others_active = 0; 3993b0f8d2eSBrian Somers this_dl = NULL; 4003b0f8d2eSBrian Somers 4013b0f8d2eSBrian Somers for (dl = bundle->links; dl; dl = dl->next) { 4023b0f8d2eSBrian Somers if (name && !strcasecmp(name, dl->name)) 4033b0f8d2eSBrian Somers this_dl = dl; 4043b0f8d2eSBrian Somers if (name == NULL || this_dl == dl) { 4059c81b87dSBrian Somers switch (how) { 4069c81b87dSBrian Somers case CLOSE_LCP: 4079c81b87dSBrian Somers datalink_DontHangup(dl); 4082fc2f705SBrian Somers break; 4099c81b87dSBrian Somers case CLOSE_STAYDOWN: 4103006ec67SBrian Somers datalink_StayDown(dl); 4119c81b87dSBrian Somers break; 4129c81b87dSBrian Somers } 4133b0f8d2eSBrian Somers } else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) 4143b0f8d2eSBrian Somers others_active++; 4153b0f8d2eSBrian Somers } 4163b0f8d2eSBrian Somers 4173b0f8d2eSBrian Somers if (name && this_dl == NULL) { 418dd7e2610SBrian Somers log_Printf(LogWARN, "%s: Invalid datalink name\n", name); 4193b0f8d2eSBrian Somers return; 4203b0f8d2eSBrian Somers } 4213b0f8d2eSBrian Somers 4223b0f8d2eSBrian Somers if (!others_active) { 423bf1eaec5SBrian Somers #ifndef NORADIUS 424bf1eaec5SBrian Somers if (bundle->radius.sessiontime) 425bf1eaec5SBrian Somers bundle_StopSessionTimer(bundle); 426bf1eaec5SBrian Somers #endif 42704eaa58cSBrian Somers bundle_StopIdleTimer(bundle); 42830949fd4SBrian Somers if (ncp_LayersUnfinished(&bundle->ncp)) 42930949fd4SBrian Somers ncp_Close(&bundle->ncp); 4303b0f8d2eSBrian Somers else { 43130949fd4SBrian Somers ncp2initial(&bundle->ncp); 432356bf92dSBrian Somers mp_Down(&bundle->ncp.mp); 433d345321bSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 4349c81b87dSBrian Somers datalink_Close(dl, how); 4357a6f8720SBrian Somers } 4363b0f8d2eSBrian Somers } else if (this_dl && this_dl->state != DATALINK_CLOSED && 4373b0f8d2eSBrian Somers this_dl->state != DATALINK_HANGUP) 4389c81b87dSBrian Somers datalink_Close(this_dl, how); 439d2fd8d77SBrian Somers } 4407a6f8720SBrian Somers 4411bc9b5baSBrian Somers void 442899011c4SBrian Somers bundle_Down(struct bundle *bundle, int how) 4431bc9b5baSBrian Somers { 4441bc9b5baSBrian Somers struct datalink *dl; 4451bc9b5baSBrian Somers 4461bc9b5baSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 447899011c4SBrian Somers datalink_Down(dl, how); 4481bc9b5baSBrian Somers } 4491bc9b5baSBrian Somers 4502f786681SBrian Somers static int 451f013f33eSBrian Somers bundle_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n) 4522f786681SBrian Somers { 4532f786681SBrian Somers struct bundle *bundle = descriptor2bundle(d); 4542f786681SBrian Somers struct datalink *dl; 45526af0ae9SBrian Somers int result, nlinks; 4566c1d6731SBrian Somers u_short ifqueue; 45726af0ae9SBrian Somers size_t queued; 4582f786681SBrian Somers 4592f786681SBrian Somers result = 0; 4602f786681SBrian Somers 461078c562eSBrian Somers /* If there are aren't many packets queued, look for some more. */ 46204eaa58cSBrian Somers for (nlinks = 0, dl = bundle->links; dl; dl = dl->next) 46304eaa58cSBrian Somers nlinks++; 46404eaa58cSBrian Somers 46504eaa58cSBrian Somers if (nlinks) { 46630949fd4SBrian Somers queued = r ? ncp_FillPhysicalQueues(&bundle->ncp, bundle) : 46730949fd4SBrian Somers ncp_QueueLen(&bundle->ncp); 46804eaa58cSBrian Somers 469ff0f9439SBrian Somers if (r && (bundle->phase == PHASE_NETWORK || 470ff0f9439SBrian Somers bundle->phys_type.all & PHYS_AUTO)) { 47104eaa58cSBrian Somers /* enough surplus so that we can tell if we're getting swamped */ 4726c1d6731SBrian Somers ifqueue = nlinks > bundle->cfg.ifqueue ? nlinks : bundle->cfg.ifqueue; 4736c1d6731SBrian Somers if (queued < ifqueue) { 47404eaa58cSBrian Somers /* Not enough - select() for more */ 4756f8e9f0aSBrian Somers if (bundle->choked.timer.state == TIMER_RUNNING) 4766f8e9f0aSBrian Somers timer_Stop(&bundle->choked.timer); /* Not needed any more */ 47704eaa58cSBrian Somers FD_SET(bundle->dev.fd, r); 478faefde08SBrian Somers if (*n < bundle->dev.fd + 1) 479faefde08SBrian Somers *n = bundle->dev.fd + 1; 4801384bd27SBrian Somers log_Printf(LogTIMER, "%s: fdset(r) %d\n", TUN_NAME, bundle->dev.fd); 481078c562eSBrian Somers result++; 4826f8e9f0aSBrian Somers } else if (bundle->choked.timer.state == TIMER_STOPPED) { 4836f8e9f0aSBrian Somers bundle->choked.timer.func = bundle_ClearQueues; 4846f8e9f0aSBrian Somers bundle->choked.timer.name = "output choke"; 4856f8e9f0aSBrian Somers bundle->choked.timer.load = bundle->cfg.choked.timeout * SECTICKS; 4866f8e9f0aSBrian Somers bundle->choked.timer.arg = bundle; 4876f8e9f0aSBrian Somers timer_Start(&bundle->choked.timer); 488078c562eSBrian Somers } 48904eaa58cSBrian Somers } 49004eaa58cSBrian Somers } 491078c562eSBrian Somers 492f0cdd9c0SBrian Somers #ifndef NORADIUS 493f0cdd9c0SBrian Somers result += descriptor_UpdateSet(&bundle->radius.desc, r, w, e, n); 494f0cdd9c0SBrian Somers #endif 495f0cdd9c0SBrian Somers 49671555108SBrian Somers /* Which links need a select() ? */ 49771555108SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 49871555108SBrian Somers result += descriptor_UpdateSet(&dl->desc, r, w, e, n); 49971555108SBrian Somers 500ea722969SBrian Somers /* 501ea722969SBrian Somers * This *MUST* be called after the datalink UpdateSet()s as it 50204eaa58cSBrian Somers * might be ``holding'' one of the datalinks (death-row) and 5038e7bd08eSBrian Somers * wants to be able to de-select() it from the descriptor set. 504ea722969SBrian Somers */ 5050f2f3eb3SBrian Somers result += descriptor_UpdateSet(&bundle->ncp.mp.server.desc, r, w, e, n); 506ea722969SBrian Somers 5072f786681SBrian Somers return result; 5082f786681SBrian Somers } 5092f786681SBrian Somers 5102f786681SBrian Somers static int 511f013f33eSBrian Somers bundle_IsSet(struct fdescriptor *d, const fd_set *fdset) 5122f786681SBrian Somers { 5132f786681SBrian Somers struct bundle *bundle = descriptor2bundle(d); 5142f786681SBrian Somers struct datalink *dl; 5152f786681SBrian Somers 5162f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 5172f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 5182f786681SBrian Somers return 1; 5192f786681SBrian Somers 520f0cdd9c0SBrian Somers #ifndef NORADIUS 521f0cdd9c0SBrian Somers if (descriptor_IsSet(&bundle->radius.desc, fdset)) 522f0cdd9c0SBrian Somers return 1; 523f0cdd9c0SBrian Somers #endif 524f0cdd9c0SBrian Somers 525332b9de0SBrian Somers if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 526332b9de0SBrian Somers return 1; 527332b9de0SBrian Somers 528faefde08SBrian Somers return FD_ISSET(bundle->dev.fd, fdset); 5292f786681SBrian Somers } 5302f786681SBrian Somers 5312f786681SBrian Somers static void 532057f1760SBrian Somers bundle_DescriptorRead(struct fdescriptor *d __unused, struct bundle *bundle, 5332f786681SBrian Somers const fd_set *fdset) 5342f786681SBrian Somers { 5352f786681SBrian Somers struct datalink *dl; 5360a4b6c5cSBrian Somers unsigned secs; 53730949fd4SBrian Somers u_int32_t af; 5382f786681SBrian Somers 539ea722969SBrian Somers if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 540ea722969SBrian Somers descriptor_Read(&bundle->ncp.mp.server.desc, bundle, fdset); 541ea722969SBrian Somers 5422f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 5432f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 5442f786681SBrian Somers descriptor_Read(&dl->desc, bundle, fdset); 545b6217683SBrian Somers 546f0cdd9c0SBrian Somers #ifndef NORADIUS 547f0cdd9c0SBrian Somers if (descriptor_IsSet(&bundle->radius.desc, fdset)) 548f0cdd9c0SBrian Somers descriptor_Read(&bundle->radius.desc, bundle, fdset); 549f0cdd9c0SBrian Somers #endif 550f0cdd9c0SBrian Somers 551faefde08SBrian Somers if (FD_ISSET(bundle->dev.fd, fdset)) { 552078c562eSBrian Somers struct tun_data tun; 553078c562eSBrian Somers int n, pri; 554c1d57c38SBrian Somers u_char *data; 5553a7b6d76SBrian Somers size_t sz; 5563a7b6d76SBrian Somers 5573a7b6d76SBrian Somers if (bundle->dev.header) { 558c1d57c38SBrian Somers data = (u_char *)&tun; 5593a7b6d76SBrian Somers sz = sizeof tun; 5603a7b6d76SBrian Somers } else { 5613a7b6d76SBrian Somers data = tun.data; 5623a7b6d76SBrian Somers sz = sizeof tun.data; 5633a7b6d76SBrian Somers } 564078c562eSBrian Somers 565078c562eSBrian Somers /* something to read from tun */ 5663a7b6d76SBrian Somers 5673a7b6d76SBrian Somers n = read(bundle->dev.fd, data, sz); 568078c562eSBrian Somers if (n < 0) { 5693a7b6d76SBrian Somers log_Printf(LogWARN, "%s: read: %s\n", bundle->dev.Name, strerror(errno)); 570078c562eSBrian Somers return; 571078c562eSBrian Somers } 5723a7b6d76SBrian Somers 5733a7b6d76SBrian Somers if (bundle->dev.header) { 5743a7b6d76SBrian Somers n -= sz - sizeof tun.data; 575078c562eSBrian Somers if (n <= 0) { 5763a7b6d76SBrian Somers log_Printf(LogERROR, "%s: read: Got only %d bytes of data !\n", 5773a7b6d76SBrian Somers bundle->dev.Name, n); 578078c562eSBrian Somers return; 579078c562eSBrian Somers } 58030949fd4SBrian Somers af = ntohl(tun.header.family); 58130949fd4SBrian Somers #ifndef NOINET6 5821136c6acSBrian Somers if (af != AF_INET && af != AF_INET6) 58330949fd4SBrian Somers #else 58430949fd4SBrian Somers if (af != AF_INET) 58530949fd4SBrian Somers #endif 5863a7b6d76SBrian Somers /* XXX: Should be maintaining drop/family counts ! */ 587078c562eSBrian Somers return; 58830949fd4SBrian Somers } else 58930949fd4SBrian Somers af = AF_INET; 590078c562eSBrian Somers 59130949fd4SBrian Somers if (af == AF_INET && ((struct ip *)tun.data)->ip_dst.s_addr == 592078c562eSBrian Somers bundle->ncp.ipcp.my_ip.s_addr) { 593078c562eSBrian Somers /* we've been asked to send something addressed *to* us :( */ 594078c562eSBrian Somers if (Enabled(bundle, OPT_LOOPBACK)) { 59530949fd4SBrian Somers pri = PacketCheck(bundle, af, tun.data, n, &bundle->filter.in, 59630949fd4SBrian Somers NULL, NULL); 597078c562eSBrian Somers if (pri >= 0) { 5983a7b6d76SBrian Somers n += sz - sizeof tun.data; 5993a7b6d76SBrian Somers write(bundle->dev.fd, data, n); 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 /* 60830949fd4SBrian Somers * Process on-demand dialup. Output packets are queued within the tunnel 60930949fd4SBrian Somers * device until the appropriate NCP 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 */ 61730949fd4SBrian Somers pri = PacketCheck(bundle, af, tun.data, n, &bundle->filter.dial, 61830949fd4SBrian Somers NULL, NULL); 619040cfe28SBrian Somers if (pri >= 0) 620ba23f397SBrian Somers bundle_Open(bundle, NULL, PHYS_AUTO, 0); 621078c562eSBrian Somers else 622078c562eSBrian Somers /* 623078c562eSBrian Somers * Drop the packet. If we were to queue it, we'd just end up with 624078c562eSBrian Somers * a pile of timed-out data in our output queue by the time we get 625078c562eSBrian Somers * around to actually dialing. We'd also prematurely reach the 626078c562eSBrian Somers * threshold at which we stop select()ing to read() the tun 627078c562eSBrian Somers * device - breaking auto-dial. 628078c562eSBrian Somers */ 629078c562eSBrian Somers return; 630078c562eSBrian Somers } 631078c562eSBrian Somers 6320a4b6c5cSBrian Somers secs = 0; 63330949fd4SBrian Somers pri = PacketCheck(bundle, af, tun.data, n, &bundle->filter.out, 63430949fd4SBrian Somers NULL, &secs); 6350a4b6c5cSBrian Somers if (pri >= 0) { 6360a4b6c5cSBrian Somers /* Prepend the number of seconds timeout given in the filter */ 6370a4b6c5cSBrian Somers tun.header.timeout = secs; 63830949fd4SBrian Somers ncp_Enqueue(&bundle->ncp, af, pri, (char *)&tun, n + sizeof tun.header); 6390a4b6c5cSBrian Somers } 640078c562eSBrian Somers } 641078c562eSBrian Somers } 6422f786681SBrian Somers 6431af29a6eSBrian Somers static int 644057f1760SBrian Somers bundle_DescriptorWrite(struct fdescriptor *d __unused, struct bundle *bundle, 6452f786681SBrian Somers const fd_set *fdset) 6462f786681SBrian Somers { 6472f786681SBrian Somers struct datalink *dl; 6481af29a6eSBrian Somers int result = 0; 6492f786681SBrian Somers 650ea722969SBrian Somers /* This is not actually necessary as struct mpserver doesn't Write() */ 651ea722969SBrian Somers if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 652fb11a9c2SBrian Somers if (descriptor_Write(&bundle->ncp.mp.server.desc, bundle, fdset) == 1) 653fb11a9c2SBrian Somers result++; 654ea722969SBrian Somers 6552f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 6562f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 657fb11a9c2SBrian Somers switch (descriptor_Write(&dl->desc, bundle, fdset)) { 658fb11a9c2SBrian Somers case -1: 659fb11a9c2SBrian Somers datalink_ComeDown(dl, CLOSE_NORMAL); 660fb11a9c2SBrian Somers break; 661fb11a9c2SBrian Somers case 1: 662fb11a9c2SBrian Somers result++; 663fb11a9c2SBrian Somers } 6641af29a6eSBrian Somers 6651af29a6eSBrian Somers return result; 6662f786681SBrian Somers } 6672f786681SBrian Somers 668da66dd13SBrian Somers void 6691384bd27SBrian Somers bundle_LockTun(struct bundle *bundle) 6701384bd27SBrian Somers { 6711384bd27SBrian Somers FILE *lockfile; 67252847614SBrian Somers char pidfile[PATH_MAX]; 6731384bd27SBrian Somers 6741384bd27SBrian Somers snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit); 6751384bd27SBrian Somers lockfile = ID0fopen(pidfile, "w"); 6761384bd27SBrian Somers if (lockfile != NULL) { 6771384bd27SBrian Somers fprintf(lockfile, "%d\n", (int)getpid()); 6781384bd27SBrian Somers fclose(lockfile); 6791384bd27SBrian Somers } 6801384bd27SBrian Somers #ifndef RELEASE_CRUNCH 6811384bd27SBrian Somers else 6821384bd27SBrian Somers log_Printf(LogERROR, "Warning: Can't create %s: %s\n", 6831384bd27SBrian Somers pidfile, strerror(errno)); 6841384bd27SBrian Somers #endif 6851384bd27SBrian Somers } 6861384bd27SBrian Somers 6871384bd27SBrian Somers static void 6881384bd27SBrian Somers bundle_UnlockTun(struct bundle *bundle) 6891384bd27SBrian Somers { 69052847614SBrian Somers char pidfile[PATH_MAX]; 6911384bd27SBrian Somers 6921384bd27SBrian Somers snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit); 6931384bd27SBrian Somers ID0unlink(pidfile); 6941384bd27SBrian Somers } 6957a6f8720SBrian Somers 6967a6f8720SBrian Somers struct bundle * 697cf0a3940SBrian Somers bundle_Create(const char *prefix, int type, int unit) 6987a6f8720SBrian Somers { 6997a6f8720SBrian Somers static struct bundle bundle; /* there can be only one */ 700c0593e34SBrian Somers int enoentcount, err, minunit, maxunit; 7014e5196e9SBrian Somers const char *ifname; 702fdb4bb1bSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD) 703fc3034caSBrian Somers int kldtried; 704fc3034caSBrian Somers #endif 7053a7b6d76SBrian Somers #if defined(TUNSIFMODE) || defined(TUNSLMODE) || defined(TUNSIFHEAD) 7060f203c7eSBrian Somers int iff; 7070f203c7eSBrian Somers #endif 7087a6f8720SBrian Somers 7098fa6ebe4SBrian Somers if (bundle.iface != NULL) { /* Already allocated ! */ 710a33b2ef7SBrian Somers log_Printf(LogALERT, "bundle_Create: There's only one BUNDLE !\n"); 7117a6f8720SBrian Somers return NULL; 7127a6f8720SBrian Somers } 7137a6f8720SBrian Somers 714c0593e34SBrian Somers if (unit == -1) { 715c0593e34SBrian Somers minunit = 0; 716c0593e34SBrian Somers maxunit = -1; 717c0593e34SBrian Somers } else { 718c0593e34SBrian Somers minunit = unit; 719c0593e34SBrian Somers maxunit = unit + 1; 720c0593e34SBrian Somers } 7217a6f8720SBrian Somers err = ENOENT; 7227a6f8720SBrian Somers enoentcount = 0; 723fdb4bb1bSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD) 724fc3034caSBrian Somers kldtried = 0; 725fc3034caSBrian Somers #endif 726c0593e34SBrian Somers for (bundle.unit = minunit; bundle.unit != maxunit; bundle.unit++) { 727faefde08SBrian Somers snprintf(bundle.dev.Name, sizeof bundle.dev.Name, "%s%d", 728faefde08SBrian Somers prefix, bundle.unit); 729faefde08SBrian Somers bundle.dev.fd = ID0open(bundle.dev.Name, O_RDWR); 730faefde08SBrian Somers if (bundle.dev.fd >= 0) 7317a6f8720SBrian Somers break; 732728ef5b2SBrian Somers else if (errno == ENXIO || errno == ENOENT) { 733fdb4bb1bSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD) 734c0593e34SBrian Somers if (bundle.unit == minunit && !kldtried++) { 735fc3034caSBrian Somers /* 736fdb4bb1bSBrian Somers * Attempt to load the tunnel interface KLD if it isn't loaded 737fdb4bb1bSBrian Somers * already. 738fc3034caSBrian Somers */ 7395476d2e5SBrian Somers if (loadmodules(LOAD_VERBOSLY, "if_tun", NULL)) 7405476d2e5SBrian Somers bundle.unit--; 741fc3034caSBrian Somers continue; 742fc3034caSBrian Somers } 743fc3034caSBrian Somers #endif 744c4c6616aSBrian Somers if (errno != ENOENT || ++enoentcount > 2) { 7457a6f8720SBrian Somers err = errno; 746107d62e7SBrian Somers break; 747c4c6616aSBrian Somers } 7487a6f8720SBrian Somers } else 7497a6f8720SBrian Somers err = errno; 7507a6f8720SBrian Somers } 7517a6f8720SBrian Somers 752faefde08SBrian Somers if (bundle.dev.fd < 0) { 753c0593e34SBrian Somers if (unit == -1) 754c0593e34SBrian Somers log_Printf(LogWARN, "No available tunnel devices found (%s)\n", 75585b542cfSBrian Somers strerror(err)); 756c0593e34SBrian Somers else 757c0593e34SBrian Somers log_Printf(LogWARN, "%s%d: %s\n", prefix, unit, strerror(err)); 7587a6f8720SBrian Somers return NULL; 7597a6f8720SBrian Somers } 7607a6f8720SBrian Somers 761dd7e2610SBrian Somers log_SetTun(bundle.unit); 7627a6f8720SBrian Somers 7638fa6ebe4SBrian Somers ifname = strrchr(bundle.dev.Name, '/'); 7648fa6ebe4SBrian Somers if (ifname == NULL) 7658fa6ebe4SBrian Somers ifname = bundle.dev.Name; 7667a6f8720SBrian Somers else 7678fa6ebe4SBrian Somers ifname++; 7688fa6ebe4SBrian Somers 7698fa6ebe4SBrian Somers bundle.iface = iface_Create(ifname); 7708fa6ebe4SBrian Somers if (bundle.iface == NULL) { 7718fa6ebe4SBrian Somers close(bundle.dev.fd); 7728fa6ebe4SBrian Somers return NULL; 7738fa6ebe4SBrian Somers } 7747a6f8720SBrian Somers 7750f203c7eSBrian Somers #ifdef TUNSIFMODE 776ebdcbc67SBrian Somers /* Make sure we're POINTOPOINT & IFF_MULTICAST */ 777ebdcbc67SBrian Somers iff = IFF_POINTOPOINT | IFF_MULTICAST; 7780f203c7eSBrian Somers if (ID0ioctl(bundle.dev.fd, TUNSIFMODE, &iff) < 0) 7790f203c7eSBrian Somers log_Printf(LogERROR, "bundle_Create: ioctl(TUNSIFMODE): %s\n", 7800f203c7eSBrian Somers strerror(errno)); 7810f203c7eSBrian Somers #endif 7820f203c7eSBrian Somers 7836ca65df0SBrian Somers #ifdef TUNSLMODE 7843a7b6d76SBrian Somers /* Make sure we're not prepending sockaddrs */ 7856ca65df0SBrian Somers iff = 0; 7866ca65df0SBrian Somers if (ID0ioctl(bundle.dev.fd, TUNSLMODE, &iff) < 0) 7876ca65df0SBrian Somers log_Printf(LogERROR, "bundle_Create: ioctl(TUNSLMODE): %s\n", 7886ca65df0SBrian Somers strerror(errno)); 7896ca65df0SBrian Somers #endif 7906ca65df0SBrian Somers 7913a7b6d76SBrian Somers #ifdef TUNSIFHEAD 7923a7b6d76SBrian Somers /* We want the address family please ! */ 7933a7b6d76SBrian Somers iff = 1; 7943a7b6d76SBrian Somers if (ID0ioctl(bundle.dev.fd, TUNSIFHEAD, &iff) < 0) { 7953a7b6d76SBrian Somers log_Printf(LogERROR, "bundle_Create: ioctl(TUNSIFHEAD): %s\n", 7963a7b6d76SBrian Somers strerror(errno)); 7973a7b6d76SBrian Somers bundle.dev.header = 0; 7983a7b6d76SBrian Somers } else 7993a7b6d76SBrian Somers bundle.dev.header = 1; 8003a7b6d76SBrian Somers #else 8013a7b6d76SBrian Somers #ifdef __OpenBSD__ 8023a7b6d76SBrian Somers /* Always present for OpenBSD */ 8033a7b6d76SBrian Somers bundle.dev.header = 1; 8043a7b6d76SBrian Somers #else 8053a7b6d76SBrian Somers /* 8063a7b6d76SBrian Somers * If TUNSIFHEAD isn't available and we're not OpenBSD, assume 8073a7b6d76SBrian Somers * everything's AF_INET (hopefully the tun device won't pass us 8083a7b6d76SBrian Somers * anything else !). 8093a7b6d76SBrian Somers */ 8103a7b6d76SBrian Somers bundle.dev.header = 0; 8113a7b6d76SBrian Somers #endif 8123a7b6d76SBrian Somers #endif 8133a7b6d76SBrian Somers 8148fa6ebe4SBrian Somers log_Printf(LogPHASE, "Using interface: %s\n", ifname); 8157a6f8720SBrian Somers 816ab2de065SBrian Somers bundle.bandwidth = 0; 817820de6ebSBrian Somers bundle.routing_seq = 0; 818a0cbd833SBrian Somers bundle.phase = PHASE_DEAD; 819a0cbd833SBrian Somers bundle.CleaningUp = 0; 82067b072f7SBrian Somers bundle.NatEnabled = 0; 8217a6f8720SBrian Somers 8226d666775SBrian Somers bundle.fsm.LayerStart = bundle_LayerStart; 8236f384573SBrian Somers bundle.fsm.LayerUp = bundle_LayerUp; 8246d666775SBrian Somers bundle.fsm.LayerDown = bundle_LayerDown; 8256d666775SBrian Somers bundle.fsm.LayerFinish = bundle_LayerFinish; 8266d666775SBrian Somers bundle.fsm.object = &bundle; 8277a6f8720SBrian Somers 828dade2407SBrian Somers bundle.cfg.idle.timeout = NCP_IDLE_TIMEOUT; 829dade2407SBrian Somers bundle.cfg.idle.min_timeout = 0; 8301342caedSBrian Somers *bundle.cfg.auth.name = '\0'; 8311342caedSBrian Somers *bundle.cfg.auth.key = '\0'; 83230949fd4SBrian Somers bundle.cfg.opt = OPT_IDCHECK | OPT_LOOPBACK | OPT_SROUTES | OPT_TCPMSSFIXUP | 833610b185fSBrian Somers OPT_THROUGHPUT | OPT_UTMP; 834971abb29SBrian Somers #ifndef NOINET6 835971abb29SBrian Somers bundle.cfg.opt |= OPT_IPCP; 836971abb29SBrian Somers if (probe.ipv6_available) 837971abb29SBrian Somers bundle.cfg.opt |= OPT_IPV6CP; 838971abb29SBrian Somers #endif 83949052c95SBrian Somers *bundle.cfg.label = '\0'; 8406c1d6731SBrian Somers bundle.cfg.ifqueue = DEF_IFQUEUE; 8416f8e9f0aSBrian Somers bundle.cfg.choked.timeout = CHOKED_TIMEOUT; 842ff0f9439SBrian Somers bundle.phys_type.all = type; 843ff0f9439SBrian Somers bundle.phys_type.open = 0; 844dade2407SBrian Somers bundle.upat = 0; 845ab886ad0SBrian Somers 8466f384573SBrian Somers bundle.links = datalink_Create("deflink", &bundle, type); 8473006ec67SBrian Somers if (bundle.links == NULL) { 848a33b2ef7SBrian Somers log_Printf(LogALERT, "Cannot create data link: %s\n", strerror(errno)); 8498fa6ebe4SBrian Somers iface_Destroy(bundle.iface); 8508fa6ebe4SBrian Somers bundle.iface = NULL; 851faefde08SBrian Somers close(bundle.dev.fd); 8522289f246SBrian Somers return NULL; 8532289f246SBrian Somers } 8542289f246SBrian Somers 8552f786681SBrian Somers bundle.desc.type = BUNDLE_DESCRIPTOR; 8562f786681SBrian Somers bundle.desc.UpdateSet = bundle_UpdateSet; 8572f786681SBrian Somers bundle.desc.IsSet = bundle_IsSet; 8582f786681SBrian Somers bundle.desc.Read = bundle_DescriptorRead; 8592f786681SBrian Somers bundle.desc.Write = bundle_DescriptorWrite; 8602f786681SBrian Somers 86130949fd4SBrian Somers ncp_Init(&bundle.ncp, &bundle); 8626d666775SBrian Somers 8635ca5389aSBrian Somers memset(&bundle.filter, '\0', sizeof bundle.filter); 8645ca5389aSBrian Somers bundle.filter.in.fragok = bundle.filter.in.logok = 1; 8655ca5389aSBrian Somers bundle.filter.in.name = "IN"; 8665ca5389aSBrian Somers bundle.filter.out.fragok = bundle.filter.out.logok = 1; 8675ca5389aSBrian Somers bundle.filter.out.name = "OUT"; 8685ca5389aSBrian Somers bundle.filter.dial.name = "DIAL"; 8698390b576SBrian Somers bundle.filter.dial.logok = 1; 8705ca5389aSBrian Somers bundle.filter.alive.name = "ALIVE"; 8715ca5389aSBrian Somers bundle.filter.alive.logok = 1; 872cad7e742SBrian Somers { 873cad7e742SBrian Somers int i; 874cad7e742SBrian Somers for (i = 0; i < MAXFILTERS; i++) { 875cad7e742SBrian Somers bundle.filter.in.rule[i].f_action = A_NONE; 876cad7e742SBrian Somers bundle.filter.out.rule[i].f_action = A_NONE; 877cad7e742SBrian Somers bundle.filter.dial.rule[i].f_action = A_NONE; 878cad7e742SBrian Somers bundle.filter.alive.rule[i].f_action = A_NONE; 879cad7e742SBrian Somers } 880cad7e742SBrian Somers } 88193ee0ff2SBrian Somers memset(&bundle.idle.timer, '\0', sizeof bundle.idle.timer); 88293ee0ff2SBrian Somers bundle.idle.done = 0; 8835cf4388bSBrian Somers bundle.notify.fd = -1; 8846f8e9f0aSBrian Somers memset(&bundle.choked.timer, '\0', sizeof bundle.choked.timer); 885972a1bcfSBrian Somers #ifndef NORADIUS 886972a1bcfSBrian Somers radius_Init(&bundle.radius); 887972a1bcfSBrian Somers #endif 88893ee0ff2SBrian Somers 8896d666775SBrian Somers /* Clean out any leftover crud */ 89030949fd4SBrian Somers iface_Clear(bundle.iface, &bundle.ncp, 0, IFACE_CLEAR_ALL); 8916d666775SBrian Somers 8921384bd27SBrian Somers bundle_LockTun(&bundle); 8931384bd27SBrian Somers 8947a6f8720SBrian Somers return &bundle; 8957a6f8720SBrian Somers } 8967a6f8720SBrian Somers 89768a0f0ccSBrian Somers static void 89868a0f0ccSBrian Somers bundle_DownInterface(struct bundle *bundle) 89968a0f0ccSBrian Somers { 900dd7e2610SBrian Somers route_IfDelete(bundle, 1); 901dc744e19SBrian Somers iface_ClearFlags(bundle->iface->name, IFF_UP); 90268a0f0ccSBrian Somers } 90368a0f0ccSBrian Somers 90468a0f0ccSBrian Somers void 90568a0f0ccSBrian Somers bundle_Destroy(struct bundle *bundle) 90668a0f0ccSBrian Somers { 9073006ec67SBrian Somers struct datalink *dl; 908b6217683SBrian Somers 909ea722969SBrian Somers /* 91030949fd4SBrian Somers * Clean up the interface. We don't really need to do the timer_Stop()s, 91130949fd4SBrian Somers * mp_Down(), iface_Clear() and bundle_DownInterface() unless we're getting 9128e7bd08eSBrian Somers * out under exceptional conditions such as a descriptor exception. 913ea722969SBrian Somers */ 91404eaa58cSBrian Somers timer_Stop(&bundle->idle.timer); 9156f8e9f0aSBrian Somers timer_Stop(&bundle->choked.timer); 91666f634b6SBrian Somers mp_Down(&bundle->ncp.mp); 91730949fd4SBrian Somers iface_Clear(bundle->iface, &bundle->ncp, 0, IFACE_CLEAR_ALL); 91868a0f0ccSBrian Somers bundle_DownInterface(bundle); 9193006ec67SBrian Somers 920972a1bcfSBrian Somers #ifndef NORADIUS 921972a1bcfSBrian Somers /* Tell the radius server the bad news */ 922972a1bcfSBrian Somers radius_Destroy(&bundle->radius); 923972a1bcfSBrian Somers #endif 924972a1bcfSBrian Somers 925ea722969SBrian Somers /* Again, these are all DATALINK_CLOSED unless we're abending */ 9263006ec67SBrian Somers dl = bundle->links; 9273006ec67SBrian Somers while (dl) 9283006ec67SBrian Somers dl = datalink_Destroy(dl); 9293006ec67SBrian Somers 93030949fd4SBrian Somers ncp_Destroy(&bundle->ncp); 931442f8495SBrian Somers 9321384bd27SBrian Somers close(bundle->dev.fd); 9331384bd27SBrian Somers bundle_UnlockTun(bundle); 9341384bd27SBrian Somers 935ea722969SBrian Somers /* In case we never made PHASE_NETWORK */ 9365cf4388bSBrian Somers bundle_Notify(bundle, EX_ERRDEAD); 937b6217683SBrian Somers 9388fa6ebe4SBrian Somers iface_Destroy(bundle->iface); 9398fa6ebe4SBrian Somers bundle->iface = NULL; 94068a0f0ccSBrian Somers } 94168a0f0ccSBrian Somers 94283d1af55SBrian Somers void 9433006ec67SBrian Somers bundle_LinkClosed(struct bundle *bundle, struct datalink *dl) 9443006ec67SBrian Somers { 9453006ec67SBrian Somers /* 9463006ec67SBrian Somers * Our datalink has closed. 947ea722969SBrian Somers * CleanDatalinks() (called from DoLoop()) will remove closed 948f6a4e748SBrian Somers * BACKGROUND, FOREGROUND and DIRECT links. 9493b0f8d2eSBrian Somers * If it's the last data link, enter phase DEAD. 950ea722969SBrian Somers * 951ea722969SBrian Somers * NOTE: dl may not be in our list (bundle_SendDatalink()) ! 9523006ec67SBrian Somers */ 9535b8b8060SBrian Somers 9543b0f8d2eSBrian Somers struct datalink *odl; 9553b0f8d2eSBrian Somers int other_links; 9565b8b8060SBrian Somers 957bf1d3ff6SBrian Somers log_SetTtyCommandMode(dl); 958bf1d3ff6SBrian Somers 9593b0f8d2eSBrian Somers other_links = 0; 9603b0f8d2eSBrian Somers for (odl = bundle->links; odl; odl = odl->next) 9613b0f8d2eSBrian Somers if (odl != dl && odl->state != DATALINK_CLOSED) 9623b0f8d2eSBrian Somers other_links++; 9633b0f8d2eSBrian Somers 9643b0f8d2eSBrian Somers if (!other_links) { 96581358fa3SBrian Somers if (dl->physical->type != PHYS_AUTO) /* Not in -auto mode */ 96603704096SBrian Somers bundle_DownInterface(bundle); 96730949fd4SBrian Somers ncp2initial(&bundle->ncp); 968356bf92dSBrian Somers mp_Down(&bundle->ncp.mp); 9695563ebdeSBrian Somers bundle_NewPhase(bundle, PHASE_DEAD); 970bf1eaec5SBrian Somers #ifndef NORADIUS 971bf1eaec5SBrian Somers if (bundle->radius.sessiontime) 972bf1eaec5SBrian Somers bundle_StopSessionTimer(bundle); 973bf1eaec5SBrian Somers #endif 9740f2f3eb3SBrian Somers bundle_StopIdleTimer(bundle); 975ab2de065SBrian Somers } 976455aabc3SBrian Somers } 977455aabc3SBrian Somers 978455aabc3SBrian Somers void 979ba23f397SBrian Somers bundle_Open(struct bundle *bundle, const char *name, int mask, int force) 9803006ec67SBrian Somers { 9813006ec67SBrian Somers /* 9823006ec67SBrian Somers * Please open the given datalink, or all if name == NULL 9833006ec67SBrian Somers */ 9843006ec67SBrian Somers struct datalink *dl; 9853006ec67SBrian Somers 9863006ec67SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 9873006ec67SBrian Somers if (name == NULL || !strcasecmp(dl->name, name)) { 988ba23f397SBrian Somers if ((mask & dl->physical->type) && 989ba23f397SBrian Somers (dl->state == DATALINK_CLOSED || 990ba23f397SBrian Somers (force && dl->state == DATALINK_OPENING && 9915e269efeSBrian Somers dl->dial.timer.state == TIMER_RUNNING) || 9925e269efeSBrian Somers dl->state == DATALINK_READY)) { 9935e269efeSBrian Somers timer_Stop(&dl->dial.timer); /* We're finished with this */ 994565e35e5SBrian Somers datalink_Up(dl, 1, 1); 995ab2de065SBrian Somers if (mask & PHYS_AUTO) 9965e269efeSBrian Somers break; /* Only one AUTO link at a time */ 99704eaa58cSBrian Somers } 9983006ec67SBrian Somers if (name != NULL) 9993006ec67SBrian Somers break; 10003006ec67SBrian Somers } 10013006ec67SBrian Somers } 10023006ec67SBrian Somers 10033006ec67SBrian Somers struct datalink * 10043006ec67SBrian Somers bundle2datalink(struct bundle *bundle, const char *name) 10053006ec67SBrian Somers { 10063006ec67SBrian Somers struct datalink *dl; 10073006ec67SBrian Somers 10083006ec67SBrian Somers if (name != NULL) { 10093006ec67SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 10103006ec67SBrian Somers if (!strcasecmp(dl->name, name)) 10113006ec67SBrian Somers return dl; 10123006ec67SBrian Somers } else if (bundle->links && !bundle->links->next) 10133006ec67SBrian Somers return bundle->links; 10143006ec67SBrian Somers 10153006ec67SBrian Somers return NULL; 10163006ec67SBrian Somers } 10173006ec67SBrian Somers 10183006ec67SBrian Somers int 1019aef795ccSBrian Somers bundle_ShowLinks(struct cmdargs const *arg) 1020aef795ccSBrian Somers { 1021aef795ccSBrian Somers struct datalink *dl; 1022ab2de065SBrian Somers struct pppThroughput *t; 102391cbd2eeSBrian Somers unsigned long long octets; 1024ab2de065SBrian Somers int secs; 1025aef795ccSBrian Somers 10269c53a7b1SBrian Somers for (dl = arg->bundle->links; dl; dl = dl->next) { 102791cbd2eeSBrian Somers octets = MAX(dl->physical->link.stats.total.in.OctetsPerSecond, 102891cbd2eeSBrian Somers dl->physical->link.stats.total.out.OctetsPerSecond); 102991cbd2eeSBrian Somers 1030d4156d00SBrian Somers prompt_Printf(arg->prompt, "Name: %s [%s, %s]", 1031d4156d00SBrian Somers dl->name, mode2Nam(dl->physical->type), datalink_State(dl)); 103211572abfSBrian Somers if (dl->physical->link.stats.total.rolling && dl->state == DATALINK_OPEN) 1033e531f89aSBrian Somers prompt_Printf(arg->prompt, " bandwidth %d, %llu bps (%llu bytes/sec)", 1034ab2de065SBrian Somers dl->mp.bandwidth ? dl->mp.bandwidth : 1035ab2de065SBrian Somers physical_GetSpeed(dl->physical), 103691cbd2eeSBrian Somers octets * 8, octets); 10379c53a7b1SBrian Somers prompt_Printf(arg->prompt, "\n"); 10389c53a7b1SBrian Somers } 1039aef795ccSBrian Somers 104011572abfSBrian Somers t = &arg->bundle->ncp.mp.link.stats.total; 104191cbd2eeSBrian Somers octets = MAX(t->in.OctetsPerSecond, t->out.OctetsPerSecond); 1042ab2de065SBrian Somers secs = t->downtime ? 0 : throughput_uptime(t); 1043ab2de065SBrian Somers if (secs > t->SamplePeriod) 1044ab2de065SBrian Somers secs = t->SamplePeriod; 1045ab2de065SBrian Somers if (secs) 1046e531f89aSBrian Somers prompt_Printf(arg->prompt, "Currently averaging %llu bps (%llu bytes/sec)" 104791cbd2eeSBrian Somers " over the last %d secs\n", octets * 8, octets, secs); 1048ab2de065SBrian Somers 1049aef795ccSBrian Somers return 0; 1050aef795ccSBrian Somers } 1051ab886ad0SBrian Somers 10521342caedSBrian Somers static const char * 10531342caedSBrian Somers optval(struct bundle *bundle, int bit) 10541342caedSBrian Somers { 10551342caedSBrian Somers return (bundle->cfg.opt & bit) ? "enabled" : "disabled"; 10561342caedSBrian Somers } 10571342caedSBrian Somers 1058c08717dfSBrian Somers int 1059c08717dfSBrian Somers bundle_ShowStatus(struct cmdargs const *arg) 1060c08717dfSBrian Somers { 1061c08717dfSBrian Somers int remaining; 1062c08717dfSBrian Somers 1063c08717dfSBrian Somers prompt_Printf(arg->prompt, "Phase %s\n", bundle_PhaseName(arg->bundle)); 1064faefde08SBrian Somers prompt_Printf(arg->prompt, " Device: %s\n", arg->bundle->dev.Name); 1065dade2407SBrian Somers prompt_Printf(arg->prompt, " Interface: %s @ %lubps", 1066ab2de065SBrian Somers arg->bundle->iface->name, arg->bundle->bandwidth); 1067c08717dfSBrian Somers 1068dade2407SBrian Somers if (arg->bundle->upat) { 106946df5aa7SBrian Somers int secs = bundle_Uptime(arg->bundle); 1070dade2407SBrian Somers 1071dade2407SBrian Somers prompt_Printf(arg->prompt, ", up time %d:%02d:%02d", secs / 3600, 1072dade2407SBrian Somers (secs / 60) % 60, secs % 60); 1073dade2407SBrian Somers } 1074669b9965SBrian Somers prompt_Printf(arg->prompt, "\n Queued: %lu of %u\n", 107530949fd4SBrian Somers (unsigned long)ncp_QueueLen(&arg->bundle->ncp), 107677fc031dSBrian Somers arg->bundle->cfg.ifqueue); 1077dade2407SBrian Somers 10786c1d6731SBrian Somers prompt_Printf(arg->prompt, "\nDefaults:\n"); 107974457d3dSBrian Somers prompt_Printf(arg->prompt, " Label: %s\n", 108074457d3dSBrian Somers arg->bundle->cfg.label); 1081610b185fSBrian Somers prompt_Printf(arg->prompt, " Auth name: %s\n", 1082610b185fSBrian Somers arg->bundle->cfg.auth.name); 108374457d3dSBrian Somers prompt_Printf(arg->prompt, " Diagnostic socket: "); 108437b8a5c7SBrian Somers if (*server.cfg.sockname != '\0') { 108537b8a5c7SBrian Somers prompt_Printf(arg->prompt, "%s", server.cfg.sockname); 108637b8a5c7SBrian Somers if (server.cfg.mask != (mode_t)-1) 108737b8a5c7SBrian Somers prompt_Printf(arg->prompt, ", mask 0%03o", (int)server.cfg.mask); 108837b8a5c7SBrian Somers prompt_Printf(arg->prompt, "%s\n", server.fd == -1 ? " (not open)" : ""); 108937b8a5c7SBrian Somers } else if (server.cfg.port != 0) 109074457d3dSBrian Somers prompt_Printf(arg->prompt, "TCP port %d%s\n", server.cfg.port, 109174457d3dSBrian Somers server.fd == -1 ? " (not open)" : ""); 109274457d3dSBrian Somers else 109374457d3dSBrian Somers prompt_Printf(arg->prompt, "none\n"); 109404eaa58cSBrian Somers 1095057f1760SBrian Somers prompt_Printf(arg->prompt, " Choked Timer: %us\n", 10966f8e9f0aSBrian Somers arg->bundle->cfg.choked.timeout); 1097972a1bcfSBrian Somers 1098972a1bcfSBrian Somers #ifndef NORADIUS 1099972a1bcfSBrian Somers radius_Show(&arg->bundle->radius, arg->prompt); 1100972a1bcfSBrian Somers #endif 1101972a1bcfSBrian Somers 1102c08717dfSBrian Somers prompt_Printf(arg->prompt, " Idle Timer: "); 1103dade2407SBrian Somers if (arg->bundle->cfg.idle.timeout) { 1104057f1760SBrian Somers prompt_Printf(arg->prompt, "%us", arg->bundle->cfg.idle.timeout); 1105dade2407SBrian Somers if (arg->bundle->cfg.idle.min_timeout) 1106057f1760SBrian Somers prompt_Printf(arg->prompt, ", min %us", 1107dade2407SBrian Somers arg->bundle->cfg.idle.min_timeout); 1108c08717dfSBrian Somers remaining = bundle_RemainingIdleTime(arg->bundle); 1109c08717dfSBrian Somers if (remaining != -1) 1110c08717dfSBrian Somers prompt_Printf(arg->prompt, " (%ds remaining)", remaining); 1111c08717dfSBrian Somers prompt_Printf(arg->prompt, "\n"); 1112c08717dfSBrian Somers } else 1113c08717dfSBrian Somers prompt_Printf(arg->prompt, "disabled\n"); 1114c08717dfSBrian Somers 111530949fd4SBrian Somers prompt_Printf(arg->prompt, " Filter Decap: %-20.20s", 111698251667SBrian Somers optval(arg->bundle, OPT_FILTERDECAP)); 111730949fd4SBrian Somers prompt_Printf(arg->prompt, " ID check: %s\n", 11181342caedSBrian Somers optval(arg->bundle, OPT_IDCHECK)); 111930949fd4SBrian Somers prompt_Printf(arg->prompt, " Iface-Alias: %-20.20s", 112030949fd4SBrian Somers optval(arg->bundle, OPT_IFACEALIAS)); 112130949fd4SBrian Somers #ifndef NOINET6 112230949fd4SBrian Somers prompt_Printf(arg->prompt, " IPCP: %s\n", 112330949fd4SBrian Somers optval(arg->bundle, OPT_IPCP)); 112430949fd4SBrian Somers prompt_Printf(arg->prompt, " IPV6CP: %-20.20s", 112530949fd4SBrian Somers optval(arg->bundle, OPT_IPV6CP)); 112630949fd4SBrian Somers #endif 112798251667SBrian Somers prompt_Printf(arg->prompt, " Keep-Session: %s\n", 1128ac685e31SBrian Somers optval(arg->bundle, OPT_KEEPSESSION)); 112998251667SBrian Somers prompt_Printf(arg->prompt, " Loopback: %-20.20s", 11301342caedSBrian Somers optval(arg->bundle, OPT_LOOPBACK)); 113198251667SBrian Somers prompt_Printf(arg->prompt, " PasswdAuth: %s\n", 11321342caedSBrian Somers optval(arg->bundle, OPT_PASSWDAUTH)); 113398251667SBrian Somers prompt_Printf(arg->prompt, " Proxy: %-20.20s", 11341342caedSBrian Somers optval(arg->bundle, OPT_PROXY)); 113598251667SBrian Somers prompt_Printf(arg->prompt, " Proxyall: %s\n", 11363afe5ccbSBrian Somers optval(arg->bundle, OPT_PROXYALL)); 113730949fd4SBrian Somers prompt_Printf(arg->prompt, " Sticky Routes: %-20.20s", 113830949fd4SBrian Somers optval(arg->bundle, OPT_SROUTES)); 113930949fd4SBrian Somers prompt_Printf(arg->prompt, " TCPMSS Fixup: %s\n", 114094d7be52SBrian Somers optval(arg->bundle, OPT_TCPMSSFIXUP)); 114130949fd4SBrian Somers prompt_Printf(arg->prompt, " Throughput: %-20.20s", 11421342caedSBrian Somers optval(arg->bundle, OPT_THROUGHPUT)); 114330949fd4SBrian Somers prompt_Printf(arg->prompt, " Utmp Logging: %s\n", 11441342caedSBrian Somers optval(arg->bundle, OPT_UTMP)); 11451342caedSBrian Somers 1146c08717dfSBrian Somers return 0; 1147c08717dfSBrian Somers } 1148c08717dfSBrian Somers 1149ab886ad0SBrian Somers static void 1150ab886ad0SBrian Somers bundle_IdleTimeout(void *v) 1151ab886ad0SBrian Somers { 1152ab886ad0SBrian Somers struct bundle *bundle = (struct bundle *)v; 1153ab886ad0SBrian Somers 1154b42135deSBrian Somers log_Printf(LogPHASE, "Idle timer expired\n"); 115504eaa58cSBrian Somers bundle_StopIdleTimer(bundle); 11569c81b87dSBrian Somers bundle_Close(bundle, NULL, CLOSE_STAYDOWN); 1157ab886ad0SBrian Somers } 1158ab886ad0SBrian Somers 1159ab886ad0SBrian Somers /* 1160ab886ad0SBrian Somers * Start Idle timer. If timeout is reached, we call bundle_Close() to 1161ab886ad0SBrian Somers * close LCP and link. 1162ab886ad0SBrian Somers */ 1163ab886ad0SBrian Somers void 11640a4b6c5cSBrian Somers bundle_StartIdleTimer(struct bundle *bundle, unsigned secs) 1165ab886ad0SBrian Somers { 1166dd7e2610SBrian Somers timer_Stop(&bundle->idle.timer); 1167ff0f9439SBrian Somers if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) != 1168dade2407SBrian Somers bundle->phys_type.open && bundle->cfg.idle.timeout) { 11690a4b6c5cSBrian Somers time_t now = time(NULL); 1170dade2407SBrian Somers 11710a4b6c5cSBrian Somers if (secs == 0) 1172dade2407SBrian Somers secs = bundle->cfg.idle.timeout; 11730a4b6c5cSBrian Somers 11740a4b6c5cSBrian Somers /* We want at least `secs' */ 1175dade2407SBrian Somers if (bundle->cfg.idle.min_timeout > secs && bundle->upat) { 1176057f1760SBrian Somers unsigned up = now - bundle->upat; 1177dade2407SBrian Somers 1178057f1760SBrian Somers if (bundle->cfg.idle.min_timeout > up && 1179057f1760SBrian Somers bundle->cfg.idle.min_timeout - up > (long long)secs) 11800a4b6c5cSBrian Somers /* Only increase from the current `remaining' value */ 1181dade2407SBrian Somers secs = bundle->cfg.idle.min_timeout - up; 1182dade2407SBrian Somers } 118393ee0ff2SBrian Somers bundle->idle.timer.func = bundle_IdleTimeout; 11843b0f8d2eSBrian Somers bundle->idle.timer.name = "idle"; 1185dade2407SBrian Somers bundle->idle.timer.load = secs * SECTICKS; 118693ee0ff2SBrian Somers bundle->idle.timer.arg = bundle; 1187dd7e2610SBrian Somers timer_Start(&bundle->idle.timer); 11880a4b6c5cSBrian Somers bundle->idle.done = now + secs; 1189ab886ad0SBrian Somers } 1190ab886ad0SBrian Somers } 1191ab886ad0SBrian Somers 1192ab886ad0SBrian Somers void 1193057f1760SBrian Somers bundle_SetIdleTimer(struct bundle *bundle, unsigned timeout, 1194057f1760SBrian Somers unsigned min_timeout) 1195ab886ad0SBrian Somers { 1196dade2407SBrian Somers bundle->cfg.idle.timeout = timeout; 1197dade2407SBrian Somers bundle->cfg.idle.min_timeout = min_timeout; 119830949fd4SBrian Somers if (ncp_LayersOpen(&bundle->ncp)) 11990a4b6c5cSBrian Somers bundle_StartIdleTimer(bundle, 0); 1200ab886ad0SBrian Somers } 1201ab886ad0SBrian Somers 1202ab886ad0SBrian Somers void 1203ab886ad0SBrian Somers bundle_StopIdleTimer(struct bundle *bundle) 1204ab886ad0SBrian Somers { 1205dd7e2610SBrian Somers timer_Stop(&bundle->idle.timer); 12064a632c80SBrian Somers bundle->idle.done = 0; 1207ab886ad0SBrian Somers } 1208ab886ad0SBrian Somers 120904eaa58cSBrian Somers static int 1210ab886ad0SBrian Somers bundle_RemainingIdleTime(struct bundle *bundle) 1211ab886ad0SBrian Somers { 121293ee0ff2SBrian Somers if (bundle->idle.done) 121393ee0ff2SBrian Somers return bundle->idle.done - time(NULL); 1214ab886ad0SBrian Somers return -1; 1215ab886ad0SBrian Somers } 12163b0f8d2eSBrian Somers 1217bf1eaec5SBrian Somers #ifndef NORADIUS 1218bf1eaec5SBrian Somers 1219bf1eaec5SBrian Somers static void 1220bf1eaec5SBrian Somers bundle_SessionTimeout(void *v) 1221bf1eaec5SBrian Somers { 1222bf1eaec5SBrian Somers struct bundle *bundle = (struct bundle *)v; 1223bf1eaec5SBrian Somers 1224bf1eaec5SBrian Somers log_Printf(LogPHASE, "Session-Timeout timer expired\n"); 1225bf1eaec5SBrian Somers bundle_StopSessionTimer(bundle); 1226bf1eaec5SBrian Somers bundle_Close(bundle, NULL, CLOSE_STAYDOWN); 1227bf1eaec5SBrian Somers } 1228bf1eaec5SBrian Somers 1229bf1eaec5SBrian Somers void 1230bf1eaec5SBrian Somers bundle_StartSessionTimer(struct bundle *bundle, unsigned secs) 1231bf1eaec5SBrian Somers { 1232bf1eaec5SBrian Somers timer_Stop(&bundle->session.timer); 1233bf1eaec5SBrian Somers if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) != 1234bf1eaec5SBrian Somers bundle->phys_type.open && bundle->radius.sessiontime) { 1235bf1eaec5SBrian Somers time_t now = time(NULL); 1236bf1eaec5SBrian Somers 1237bf1eaec5SBrian Somers if (secs == 0) 1238bf1eaec5SBrian Somers secs = bundle->radius.sessiontime; 1239bf1eaec5SBrian Somers 1240bf1eaec5SBrian Somers bundle->session.timer.func = bundle_SessionTimeout; 1241bf1eaec5SBrian Somers bundle->session.timer.name = "session"; 1242bf1eaec5SBrian Somers bundle->session.timer.load = secs * SECTICKS; 1243bf1eaec5SBrian Somers bundle->session.timer.arg = bundle; 1244bf1eaec5SBrian Somers timer_Start(&bundle->session.timer); 1245bf1eaec5SBrian Somers bundle->session.done = now + secs; 1246bf1eaec5SBrian Somers } 1247bf1eaec5SBrian Somers } 1248bf1eaec5SBrian Somers 1249bf1eaec5SBrian Somers void 1250bf1eaec5SBrian Somers bundle_StopSessionTimer(struct bundle *bundle) 1251bf1eaec5SBrian Somers { 1252bf1eaec5SBrian Somers timer_Stop(&bundle->session.timer); 1253bf1eaec5SBrian Somers bundle->session.done = 0; 1254bf1eaec5SBrian Somers } 1255bf1eaec5SBrian Somers 1256bf1eaec5SBrian Somers #endif 1257bf1eaec5SBrian Somers 12583b0f8d2eSBrian Somers int 12593b0f8d2eSBrian Somers bundle_IsDead(struct bundle *bundle) 12603b0f8d2eSBrian Somers { 12613b0f8d2eSBrian Somers return !bundle->links || (bundle->phase == PHASE_DEAD && bundle->CleaningUp); 12623b0f8d2eSBrian Somers } 1263b6217683SBrian Somers 126404eaa58cSBrian Somers static struct datalink * 126504eaa58cSBrian Somers bundle_DatalinkLinkout(struct bundle *bundle, struct datalink *dl) 1266cd7bd93aSBrian Somers { 1267cd7bd93aSBrian Somers struct datalink **dlp; 1268cd7bd93aSBrian Somers 1269cd7bd93aSBrian Somers for (dlp = &bundle->links; *dlp; dlp = &(*dlp)->next) 1270cd7bd93aSBrian Somers if (*dlp == dl) { 127104eaa58cSBrian Somers *dlp = dl->next; 127204eaa58cSBrian Somers dl->next = NULL; 127304eaa58cSBrian Somers bundle_LinksRemoved(bundle); 127404eaa58cSBrian Somers return dl; 1275cd7bd93aSBrian Somers } 127604eaa58cSBrian Somers 127704eaa58cSBrian Somers return NULL; 127804eaa58cSBrian Somers } 127904eaa58cSBrian Somers 128004eaa58cSBrian Somers static void 128104eaa58cSBrian Somers bundle_DatalinkLinkin(struct bundle *bundle, struct datalink *dl) 128204eaa58cSBrian Somers { 128304eaa58cSBrian Somers struct datalink **dlp = &bundle->links; 128404eaa58cSBrian Somers 128504eaa58cSBrian Somers while (*dlp) 128604eaa58cSBrian Somers dlp = &(*dlp)->next; 128704eaa58cSBrian Somers 128804eaa58cSBrian Somers *dlp = dl; 128904eaa58cSBrian Somers dl->next = NULL; 129004eaa58cSBrian Somers 129104eaa58cSBrian Somers bundle_LinkAdded(bundle, dl); 1292ab2de065SBrian Somers mp_CheckAutoloadTimer(&bundle->ncp.mp); 1293565e35e5SBrian Somers } 1294565e35e5SBrian Somers 1295565e35e5SBrian Somers void 1296565e35e5SBrian Somers bundle_CleanDatalinks(struct bundle *bundle) 1297565e35e5SBrian Somers { 1298565e35e5SBrian Somers struct datalink **dlp = &bundle->links; 129904eaa58cSBrian Somers int found = 0; 1300565e35e5SBrian Somers 1301565e35e5SBrian Somers while (*dlp) 1302565e35e5SBrian Somers if ((*dlp)->state == DATALINK_CLOSED && 1303f6a4e748SBrian Somers (*dlp)->physical->type & 1304f6a4e748SBrian Somers (PHYS_DIRECT|PHYS_BACKGROUND|PHYS_FOREGROUND)) { 1305565e35e5SBrian Somers *dlp = datalink_Destroy(*dlp); 130604eaa58cSBrian Somers found++; 130704eaa58cSBrian Somers } else 1308565e35e5SBrian Somers dlp = &(*dlp)->next; 130904eaa58cSBrian Somers 131004eaa58cSBrian Somers if (found) 131104eaa58cSBrian Somers bundle_LinksRemoved(bundle); 131204eaa58cSBrian Somers } 131304eaa58cSBrian Somers 131404eaa58cSBrian Somers int 131504eaa58cSBrian Somers bundle_DatalinkClone(struct bundle *bundle, struct datalink *dl, 131604eaa58cSBrian Somers const char *name) 131704eaa58cSBrian Somers { 131804eaa58cSBrian Somers if (bundle2datalink(bundle, name)) { 131904eaa58cSBrian Somers log_Printf(LogWARN, "Clone: %s: name already exists\n", name); 132004eaa58cSBrian Somers return 0; 132104eaa58cSBrian Somers } 132204eaa58cSBrian Somers 132304eaa58cSBrian Somers bundle_DatalinkLinkin(bundle, datalink_Clone(dl, name)); 132404eaa58cSBrian Somers return 1; 132504eaa58cSBrian Somers } 132604eaa58cSBrian Somers 132704eaa58cSBrian Somers void 132804eaa58cSBrian Somers bundle_DatalinkRemove(struct bundle *bundle, struct datalink *dl) 132904eaa58cSBrian Somers { 133004eaa58cSBrian Somers dl = bundle_DatalinkLinkout(bundle, dl); 133104eaa58cSBrian Somers if (dl) 133204eaa58cSBrian Somers datalink_Destroy(dl); 1333cd7bd93aSBrian Somers } 133449052c95SBrian Somers 133549052c95SBrian Somers void 133649052c95SBrian Somers bundle_SetLabel(struct bundle *bundle, const char *label) 133749052c95SBrian Somers { 133849052c95SBrian Somers if (label) 133949052c95SBrian Somers strncpy(bundle->cfg.label, label, sizeof bundle->cfg.label - 1); 134049052c95SBrian Somers else 134149052c95SBrian Somers *bundle->cfg.label = '\0'; 134249052c95SBrian Somers } 134349052c95SBrian Somers 134449052c95SBrian Somers const char * 134549052c95SBrian Somers bundle_GetLabel(struct bundle *bundle) 134649052c95SBrian Somers { 134749052c95SBrian Somers return *bundle->cfg.label ? bundle->cfg.label : NULL; 134849052c95SBrian Somers } 13491fa665f5SBrian Somers 13502cb305afSBrian Somers int 13512cb305afSBrian Somers bundle_LinkSize() 13521fa665f5SBrian Somers { 135396c9bb21SBrian Somers struct iovec iov[SCATTER_SEGMENTS]; 13542cb305afSBrian Somers int niov, expect, f; 135587c3786eSBrian Somers 135696c9bb21SBrian Somers iov[0].iov_len = strlen(Version) + 1; 13572cb305afSBrian Somers iov[0].iov_base = NULL; 13582cb305afSBrian Somers niov = 1; 13592cb305afSBrian Somers if (datalink2iov(NULL, iov, &niov, SCATTER_SEGMENTS, NULL, NULL) == -1) { 13602cb305afSBrian Somers log_Printf(LogERROR, "Cannot determine space required for link\n"); 13612cb305afSBrian Somers return 0; 136254cd8e13SBrian Somers } 13636f384573SBrian Somers 136496c9bb21SBrian Somers for (f = expect = 0; f < niov; f++) 136596c9bb21SBrian Somers expect += iov[f].iov_len; 136696c9bb21SBrian Somers 13672cb305afSBrian Somers return expect; 136887c3786eSBrian Somers } 136996c9bb21SBrian Somers 13702cb305afSBrian Somers void 13712cb305afSBrian Somers bundle_ReceiveDatalink(struct bundle *bundle, int s) 13722cb305afSBrian Somers { 13732cb305afSBrian Somers char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int) * SEND_MAXFD]; 1374057f1760SBrian Somers int niov, expect, f, *fd, nfd, onfd; 1375057f1760SBrian Somers ssize_t got; 13762cb305afSBrian Somers struct iovec iov[SCATTER_SEGMENTS]; 13772cb305afSBrian Somers struct cmsghdr *cmsg; 13782cb305afSBrian Somers struct msghdr msg; 13792cb305afSBrian Somers struct datalink *dl; 13802cb305afSBrian Somers pid_t pid; 13812cb305afSBrian Somers 13822cb305afSBrian Somers log_Printf(LogPHASE, "Receiving datalink\n"); 13832cb305afSBrian Somers 13842cb305afSBrian Somers /* 13852cb305afSBrian Somers * Create our scatter/gather array - passing NULL gets the space 13862cb305afSBrian Somers * allocation requirement rather than actually flattening the 13872cb305afSBrian Somers * structures. 13882cb305afSBrian Somers */ 13892cb305afSBrian Somers iov[0].iov_len = strlen(Version) + 1; 13902cb305afSBrian Somers iov[0].iov_base = NULL; 13912cb305afSBrian Somers niov = 1; 13922cb305afSBrian Somers if (datalink2iov(NULL, iov, &niov, SCATTER_SEGMENTS, NULL, NULL) == -1) { 13932cb305afSBrian Somers log_Printf(LogERROR, "Cannot determine space required for link\n"); 13942cb305afSBrian Somers return; 13952cb305afSBrian Somers } 13962cb305afSBrian Somers 13972cb305afSBrian Somers /* Allocate the scatter/gather array for recvmsg() */ 13982cb305afSBrian Somers for (f = expect = 0; f < niov; f++) { 13992cb305afSBrian Somers if ((iov[f].iov_base = malloc(iov[f].iov_len)) == NULL) { 14002cb305afSBrian Somers log_Printf(LogERROR, "Cannot allocate space to receive link\n"); 14012cb305afSBrian Somers return; 14022cb305afSBrian Somers } 1403cbee9754SBrian Somers if (f) 14042cb305afSBrian Somers expect += iov[f].iov_len; 14052cb305afSBrian Somers } 14062cb305afSBrian Somers 14072cb305afSBrian Somers /* Set up our message */ 14082cb305afSBrian Somers cmsg = (struct cmsghdr *)cmsgbuf; 14092cb305afSBrian Somers cmsg->cmsg_len = sizeof cmsgbuf; 14102cb305afSBrian Somers cmsg->cmsg_level = SOL_SOCKET; 14112cb305afSBrian Somers cmsg->cmsg_type = 0; 14122cb305afSBrian Somers 141396c9bb21SBrian Somers memset(&msg, '\0', sizeof msg); 14142cb305afSBrian Somers msg.msg_name = NULL; 14152cb305afSBrian Somers msg.msg_namelen = 0; 141696c9bb21SBrian Somers msg.msg_iov = iov; 1417cbee9754SBrian Somers msg.msg_iovlen = 1; /* Only send the version at the first pass */ 141896c9bb21SBrian Somers msg.msg_control = cmsgbuf; 141996c9bb21SBrian Somers msg.msg_controllen = sizeof cmsgbuf; 142096c9bb21SBrian Somers 1421209dc102SBrian Somers log_Printf(LogDEBUG, "Expecting %u scatter/gather bytes\n", 1422209dc102SBrian Somers (unsigned)iov[0].iov_len); 14232cb305afSBrian Somers 1424057f1760SBrian Somers if ((got = recvmsg(s, &msg, MSG_WAITALL)) != (ssize_t)iov[0].iov_len) { 1425cbee9754SBrian Somers if (got == -1) 142696c9bb21SBrian Somers log_Printf(LogERROR, "Failed recvmsg: %s\n", strerror(errno)); 142796c9bb21SBrian Somers else 14281814213eSMarcel Moolenaar log_Printf(LogERROR, "Failed recvmsg: Got %zd, not %u\n", 1429209dc102SBrian Somers got, (unsigned)iov[0].iov_len); 143096c9bb21SBrian Somers while (niov--) 143196c9bb21SBrian Somers free(iov[niov].iov_base); 143296c9bb21SBrian Somers return; 143396c9bb21SBrian Somers } 143496c9bb21SBrian Somers 14352cb305afSBrian Somers if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) { 14362cb305afSBrian Somers log_Printf(LogERROR, "Recvmsg: no descriptors received !\n"); 14372cb305afSBrian Somers while (niov--) 14382cb305afSBrian Somers free(iov[niov].iov_base); 14392cb305afSBrian Somers return; 144087c3786eSBrian Somers } 144187c3786eSBrian Somers 14422bc21ed9SDavid Malone fd = (int *)CMSG_DATA(cmsg); 14432bc21ed9SDavid Malone nfd = ((caddr_t)cmsg + cmsg->cmsg_len - (caddr_t)fd) / sizeof(int); 14442cb305afSBrian Somers 14452cb305afSBrian Somers if (nfd < 2) { 14468e7bd08eSBrian Somers log_Printf(LogERROR, "Recvmsg: %d descriptor%s received (too few) !\n", 14472cb305afSBrian Somers nfd, nfd == 1 ? "" : "s"); 14482cb305afSBrian Somers while (nfd--) 14492cb305afSBrian Somers close(fd[nfd]); 1450ad5b0e8bSBrian Somers while (niov--) 1451ad5b0e8bSBrian Somers free(iov[niov].iov_base); 1452ad5b0e8bSBrian Somers return; 145354cd8e13SBrian Somers } 145454cd8e13SBrian Somers 145587c3786eSBrian Somers /* 1456cbee9754SBrian Somers * We've successfully received two or more open file descriptors 1457cbee9754SBrian Somers * through our socket, plus a version string. Make sure it's the 1458cbee9754SBrian Somers * correct version, and drop the connection if it's not. 145987c3786eSBrian Somers */ 146096c9bb21SBrian Somers if (strncmp(Version, iov[0].iov_base, iov[0].iov_len)) { 146196c9bb21SBrian Somers log_Printf(LogWARN, "Cannot receive datalink, incorrect version" 146296c9bb21SBrian Somers " (\"%.*s\", not \"%s\")\n", (int)iov[0].iov_len, 14637d81ddf5SBrian Somers (char *)iov[0].iov_base, Version); 14642cb305afSBrian Somers while (nfd--) 14652cb305afSBrian Somers close(fd[nfd]); 146696c9bb21SBrian Somers while (niov--) 146796c9bb21SBrian Somers free(iov[niov].iov_base); 146896c9bb21SBrian Somers return; 146996c9bb21SBrian Somers } 147096c9bb21SBrian Somers 1471cbee9754SBrian Somers /* 1472cbee9754SBrian Somers * Everything looks good. Send the other side our process id so that 1473cbee9754SBrian Somers * they can transfer lock ownership, and wait for them to send the 1474cbee9754SBrian Somers * actual link data. 1475cbee9754SBrian Somers */ 1476cbee9754SBrian Somers pid = getpid(); 1477cbee9754SBrian Somers if ((got = write(fd[1], &pid, sizeof pid)) != sizeof pid) { 1478cbee9754SBrian Somers if (got == -1) 1479cbee9754SBrian Somers log_Printf(LogERROR, "Failed write: %s\n", strerror(errno)); 1480cbee9754SBrian Somers else 14811814213eSMarcel Moolenaar log_Printf(LogERROR, "Failed write: Got %zd, not %d\n", got, 1482cbee9754SBrian Somers (int)(sizeof pid)); 1483cbee9754SBrian Somers while (nfd--) 1484cbee9754SBrian Somers close(fd[nfd]); 1485cbee9754SBrian Somers while (niov--) 1486cbee9754SBrian Somers free(iov[niov].iov_base); 1487cbee9754SBrian Somers return; 1488cbee9754SBrian Somers } 1489cbee9754SBrian Somers 1490cbee9754SBrian Somers if ((got = readv(fd[1], iov + 1, niov - 1)) != expect) { 1491cbee9754SBrian Somers if (got == -1) 1492cbee9754SBrian Somers log_Printf(LogERROR, "Failed write: %s\n", strerror(errno)); 1493cbee9754SBrian Somers else 14941814213eSMarcel Moolenaar log_Printf(LogERROR, "Failed write: Got %zd, not %d\n", got, expect); 1495cbee9754SBrian Somers while (nfd--) 1496cbee9754SBrian Somers close(fd[nfd]); 1497cbee9754SBrian Somers while (niov--) 1498cbee9754SBrian Somers free(iov[niov].iov_base); 1499cbee9754SBrian Somers return; 1500cbee9754SBrian Somers } 1501cbee9754SBrian Somers close(fd[1]); 1502cbee9754SBrian Somers 15032cb305afSBrian Somers onfd = nfd; /* We've got this many in our array */ 15048e7bd08eSBrian Somers nfd -= 2; /* Don't include p->fd and our reply descriptor */ 15052cb305afSBrian Somers niov = 1; /* Skip the version id */ 150687c3786eSBrian Somers dl = iov2datalink(bundle, iov, &niov, sizeof iov / sizeof *iov, fd[0], 15072cb305afSBrian Somers fd + 2, &nfd); 1508b7c5748eSBrian Somers if (dl) { 15092cb305afSBrian Somers 151087c3786eSBrian Somers if (nfd) { 151187c3786eSBrian Somers log_Printf(LogERROR, "bundle_ReceiveDatalink: Failed to handle %d " 15122cb305afSBrian Somers "auxiliary file descriptors (%d remain)\n", onfd, nfd); 151387c3786eSBrian Somers datalink_Destroy(dl); 151487c3786eSBrian Somers while (nfd--) 151587c3786eSBrian Somers close(fd[onfd--]); 15162cb305afSBrian Somers close(fd[0]); 151787c3786eSBrian Somers } else { 151804eaa58cSBrian Somers bundle_DatalinkLinkin(bundle, dl); 1519b7c5748eSBrian Somers datalink_AuthOk(dl); 1520ab2de065SBrian Somers bundle_CalculateBandwidth(dl->bundle); 152187c3786eSBrian Somers } 152287c3786eSBrian Somers } else { 152387c3786eSBrian Somers while (nfd--) 152487c3786eSBrian Somers close(fd[onfd--]); 152587c3786eSBrian Somers close(fd[0]); 15262cb305afSBrian Somers close(fd[1]); 152787c3786eSBrian Somers } 152896c9bb21SBrian Somers 152996c9bb21SBrian Somers free(iov[0].iov_base); 15301fa665f5SBrian Somers } 15311fa665f5SBrian Somers 15321fa665f5SBrian Somers void 153396c9bb21SBrian Somers bundle_SendDatalink(struct datalink *dl, int s, struct sockaddr_un *sun) 15341fa665f5SBrian Somers { 15352bc21ed9SDavid Malone char cmsgbuf[CMSG_SPACE(sizeof(int) * SEND_MAXFD)]; 15362cb305afSBrian Somers const char *constlock; 15372cb305afSBrian Somers char *lock; 153887c3786eSBrian Somers struct cmsghdr *cmsg; 153996c9bb21SBrian Somers struct msghdr msg; 154096c9bb21SBrian Somers struct iovec iov[SCATTER_SEGMENTS]; 1541057f1760SBrian Somers int niov, f, expect, newsid, fd[SEND_MAXFD], nfd, reply[2]; 1542057f1760SBrian Somers ssize_t got; 154385fd273aSBrian Somers pid_t newpid; 15446f384573SBrian Somers 1545dd7e2610SBrian Somers log_Printf(LogPHASE, "Transmitting datalink %s\n", dl->name); 15466f384573SBrian Somers 15472cb305afSBrian Somers /* Record the base device name for a lock transfer later */ 15482cb305afSBrian Somers constlock = physical_LockedDevice(dl->physical); 15492cb305afSBrian Somers if (constlock) { 15502cb305afSBrian Somers lock = alloca(strlen(constlock) + 1); 15512cb305afSBrian Somers strcpy(lock, constlock); 15522cb305afSBrian Somers } else 15532cb305afSBrian Somers lock = NULL; 15542cb305afSBrian Somers 155504eaa58cSBrian Somers bundle_LinkClosed(dl->bundle, dl); 15560f2f3eb3SBrian Somers bundle_DatalinkLinkout(dl->bundle, dl); 1557ea722969SBrian Somers 155896c9bb21SBrian Somers /* Build our scatter/gather array */ 155996c9bb21SBrian Somers iov[0].iov_len = strlen(Version) + 1; 156096c9bb21SBrian Somers iov[0].iov_base = strdup(Version); 156196c9bb21SBrian Somers niov = 1; 156287c3786eSBrian Somers nfd = 0; 15636f384573SBrian Somers 15642cb305afSBrian Somers fd[0] = datalink2iov(dl, iov, &niov, SCATTER_SEGMENTS, fd + 2, &nfd); 15656f384573SBrian Somers 15662cb305afSBrian Somers if (fd[0] != -1 && socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, reply) != -1) { 15672cb305afSBrian Somers /* 15682cb305afSBrian Somers * fd[1] is used to get the peer process id back, then to confirm that 15692cb305afSBrian Somers * we've transferred any device locks to that process id. 15702cb305afSBrian Somers */ 15712cb305afSBrian Somers fd[1] = reply[1]; 157287c3786eSBrian Somers 15732cb305afSBrian Somers nfd += 2; /* Include fd[0] and fd[1] */ 157496c9bb21SBrian Somers memset(&msg, '\0', sizeof msg); 157554cd8e13SBrian Somers 15762cb305afSBrian Somers msg.msg_name = NULL; 15772cb305afSBrian Somers msg.msg_namelen = 0; 1578cbee9754SBrian Somers /* 1579cbee9754SBrian Somers * Only send the version to start... We used to send the whole lot, but 1580cbee9754SBrian Somers * this caused problems with our RECVBUF size as a single link is about 1581cbee9754SBrian Somers * 22k ! This way, we should bump into no limits. 1582cbee9754SBrian Somers */ 1583cbee9754SBrian Somers msg.msg_iovlen = 1; 158496c9bb21SBrian Somers msg.msg_iov = iov; 15852cb305afSBrian Somers msg.msg_control = cmsgbuf; 15862bc21ed9SDavid Malone msg.msg_controllen = CMSG_SPACE(sizeof(int) * nfd); 15872cb305afSBrian Somers msg.msg_flags = 0; 158854cd8e13SBrian Somers 15892cb305afSBrian Somers cmsg = (struct cmsghdr *)cmsgbuf; 15902cb305afSBrian Somers cmsg->cmsg_len = msg.msg_controllen; 159154cd8e13SBrian Somers cmsg->cmsg_level = SOL_SOCKET; 159254cd8e13SBrian Somers cmsg->cmsg_type = SCM_RIGHTS; 159387c3786eSBrian Somers 15942cb305afSBrian Somers for (f = 0; f < nfd; f++) 15952bc21ed9SDavid Malone *((int *)CMSG_DATA(cmsg) + f) = fd[f]; 159647723d29SBrian Somers 1597cbee9754SBrian Somers for (f = 1, expect = 0; f < niov; f++) 159896c9bb21SBrian Somers expect += iov[f].iov_len; 159947723d29SBrian Somers 1600cbee9754SBrian Somers if (setsockopt(reply[0], SOL_SOCKET, SO_SNDBUF, &expect, sizeof(int)) == -1) 1601cbee9754SBrian Somers log_Printf(LogERROR, "setsockopt(SO_RCVBUF, %d): %s\n", expect, 1602cbee9754SBrian Somers strerror(errno)); 1603cbee9754SBrian Somers if (setsockopt(reply[1], SOL_SOCKET, SO_RCVBUF, &expect, sizeof(int)) == -1) 1604cbee9754SBrian Somers log_Printf(LogERROR, "setsockopt(SO_RCVBUF, %d): %s\n", expect, 1605cbee9754SBrian Somers strerror(errno)); 1606cbee9754SBrian Somers 1607209dc102SBrian Somers log_Printf(LogDEBUG, "Sending %d descriptor%s and %u bytes in scatter" 1608209dc102SBrian Somers "/gather array\n", nfd, nfd == 1 ? "" : "s", 1609209dc102SBrian Somers (unsigned)iov[0].iov_len); 161047723d29SBrian Somers 16112cb305afSBrian Somers if ((got = sendmsg(s, &msg, 0)) == -1) 16122cb305afSBrian Somers log_Printf(LogERROR, "Failed sendmsg: %s: %s\n", 16132cb305afSBrian Somers sun->sun_path, strerror(errno)); 1614057f1760SBrian Somers else if (got != (ssize_t)iov[0].iov_len) 16151814213eSMarcel Moolenaar log_Printf(LogERROR, "%s: Failed initial sendmsg: Only sent %zd of %u\n", 1616209dc102SBrian Somers sun->sun_path, got, (unsigned)iov[0].iov_len); 16172cb305afSBrian Somers else { 16188e7bd08eSBrian Somers /* We must get the ACK before closing the descriptor ! */ 16192cb305afSBrian Somers int res; 16202cb305afSBrian Somers 1621cbee9754SBrian Somers if ((got = read(reply[0], &newpid, sizeof newpid)) == sizeof newpid) { 162242df3c25SBrian Somers log_Printf(LogDEBUG, "Received confirmation from pid %ld\n", 162342df3c25SBrian Somers (long)newpid); 16242cb305afSBrian Somers if (lock && (res = ID0uu_lock_txfr(lock, newpid)) != UU_LOCK_OK) 1625b42135deSBrian Somers log_Printf(LogERROR, "uu_lock_txfr: %s\n", uu_lockerr(res)); 16262cb305afSBrian Somers 1627cbee9754SBrian Somers log_Printf(LogDEBUG, "Transmitting link (%d bytes)\n", expect); 1628cbee9754SBrian Somers if ((got = writev(reply[0], iov + 1, niov - 1)) != expect) { 1629cbee9754SBrian Somers if (got == -1) 1630cbee9754SBrian Somers log_Printf(LogERROR, "%s: Failed writev: %s\n", 1631cbee9754SBrian Somers sun->sun_path, strerror(errno)); 1632cbee9754SBrian Somers else 16331814213eSMarcel Moolenaar log_Printf(LogERROR, "%s: Failed writev: Wrote %zd of %d\n", 1634cbee9754SBrian Somers sun->sun_path, got, expect); 1635cbee9754SBrian Somers } 1636cbee9754SBrian Somers } else if (got == -1) 1637cbee9754SBrian Somers log_Printf(LogERROR, "%s: Failed socketpair read: %s\n", 1638cbee9754SBrian Somers sun->sun_path, strerror(errno)); 1639cbee9754SBrian Somers else 16401814213eSMarcel Moolenaar log_Printf(LogERROR, "%s: Failed socketpair read: Got %zd of %d\n", 1641cbee9754SBrian Somers sun->sun_path, got, (int)(sizeof newpid)); 16422cb305afSBrian Somers } 16432cb305afSBrian Somers 16442cb305afSBrian Somers close(reply[0]); 16452cb305afSBrian Somers close(reply[1]); 164654cd8e13SBrian Somers 1647ac685e31SBrian Somers newsid = Enabled(dl->bundle, OPT_KEEPSESSION) || 164887c3786eSBrian Somers tcgetpgrp(fd[0]) == getpgrp(); 164987c3786eSBrian Somers while (nfd) 165087c3786eSBrian Somers close(fd[--nfd]); 16511384bd27SBrian Somers if (newsid) 16522cb305afSBrian Somers bundle_setsid(dl->bundle, got != -1); 165347723d29SBrian Somers } 165485fd273aSBrian Somers close(s); 165596c9bb21SBrian Somers 165696c9bb21SBrian Somers while (niov--) 165796c9bb21SBrian Somers free(iov[niov].iov_base); 16581fa665f5SBrian Somers } 1659dd0645c5SBrian Somers 1660dd0645c5SBrian Somers int 166158d55334SBrian Somers bundle_RenameDatalink(struct bundle *bundle, struct datalink *ndl, 166258d55334SBrian Somers const char *name) 166358d55334SBrian Somers { 166458d55334SBrian Somers struct datalink *dl; 166558d55334SBrian Somers 166658d55334SBrian Somers if (!strcasecmp(ndl->name, name)) 166758d55334SBrian Somers return 1; 166858d55334SBrian Somers 166958d55334SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 167058d55334SBrian Somers if (!strcasecmp(dl->name, name)) 167158d55334SBrian Somers return 0; 167258d55334SBrian Somers 167358d55334SBrian Somers datalink_Rename(ndl, name); 167458d55334SBrian Somers return 1; 167558d55334SBrian Somers } 167658d55334SBrian Somers 167758d55334SBrian Somers int 1678dd0645c5SBrian Somers bundle_SetMode(struct bundle *bundle, struct datalink *dl, int mode) 1679dd0645c5SBrian Somers { 1680dd0645c5SBrian Somers int omode; 1681dd0645c5SBrian Somers 1682dd0645c5SBrian Somers omode = dl->physical->type; 1683dd0645c5SBrian Somers if (omode == mode) 1684dd0645c5SBrian Somers return 1; 1685dd0645c5SBrian Somers 1686ff0f9439SBrian Somers if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO)) 1687ff0f9439SBrian Somers /* First auto link */ 1688dd0645c5SBrian Somers if (bundle->ncp.ipcp.peer_ip.s_addr == INADDR_ANY) { 1689ff0f9439SBrian Somers log_Printf(LogWARN, "You must `set ifaddr' or `open' before" 1690ff0f9439SBrian Somers " changing mode to %s\n", mode2Nam(mode)); 1691dd0645c5SBrian Somers return 0; 1692dd0645c5SBrian Somers } 1693dd0645c5SBrian Somers 1694dd0645c5SBrian Somers if (!datalink_SetMode(dl, mode)) 1695dd0645c5SBrian Somers return 0; 1696dd0645c5SBrian Somers 1697ff0f9439SBrian Somers if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO) && 1698ff0f9439SBrian Somers bundle->phase != PHASE_NETWORK) 1699ff0f9439SBrian Somers /* First auto link, we need an interface */ 1700dd0645c5SBrian Somers ipcp_InterfaceUp(&bundle->ncp.ipcp); 1701dd0645c5SBrian Somers 1702ab2de065SBrian Somers /* Regenerate phys_type and adjust idle timer */ 170304eaa58cSBrian Somers bundle_LinksRemoved(bundle); 1704dd0645c5SBrian Somers 1705dd0645c5SBrian Somers return 1; 1706dd0645c5SBrian Somers } 17071384bd27SBrian Somers 17081384bd27SBrian Somers void 17091384bd27SBrian Somers bundle_setsid(struct bundle *bundle, int holdsession) 17101384bd27SBrian Somers { 17111384bd27SBrian Somers /* 17121384bd27SBrian Somers * Lose the current session. This means getting rid of our pid 17131384bd27SBrian Somers * too so that the tty device will really go away, and any getty 17141384bd27SBrian Somers * etc will be allowed to restart. 17151384bd27SBrian Somers */ 17161384bd27SBrian Somers pid_t pid, orig; 17171384bd27SBrian Somers int fds[2]; 17181384bd27SBrian Somers char done; 17191384bd27SBrian Somers struct datalink *dl; 17201384bd27SBrian Somers 1721e62ce959SBrian Somers if (!holdsession && bundle_IsDead(bundle)) { 1722e62ce959SBrian Somers /* 1723e62ce959SBrian Somers * No need to lose our session after all... we're going away anyway 1724e62ce959SBrian Somers * 1725e62ce959SBrian Somers * We should really stop the timer and pause if holdsession is set and 1726e62ce959SBrian Somers * the bundle's dead, but that leaves other resources lying about :-( 1727e62ce959SBrian Somers */ 1728e62ce959SBrian Somers return; 1729e62ce959SBrian Somers } 1730e62ce959SBrian Somers 17311384bd27SBrian Somers orig = getpid(); 17321384bd27SBrian Somers if (pipe(fds) == -1) { 17331384bd27SBrian Somers log_Printf(LogERROR, "pipe: %s\n", strerror(errno)); 17341384bd27SBrian Somers return; 17351384bd27SBrian Somers } 17361384bd27SBrian Somers switch ((pid = fork())) { 17371384bd27SBrian Somers case -1: 17381384bd27SBrian Somers log_Printf(LogERROR, "fork: %s\n", strerror(errno)); 17391384bd27SBrian Somers close(fds[0]); 17401384bd27SBrian Somers close(fds[1]); 17411384bd27SBrian Somers return; 17421384bd27SBrian Somers case 0: 17431384bd27SBrian Somers close(fds[1]); 17444be0e57dSBrian Somers read(fds[0], &done, 1); /* uu_locks are mine ! */ 17454be0e57dSBrian Somers close(fds[0]); 17461384bd27SBrian Somers if (pipe(fds) == -1) { 17471384bd27SBrian Somers log_Printf(LogERROR, "pipe(2): %s\n", strerror(errno)); 17481384bd27SBrian Somers return; 17491384bd27SBrian Somers } 17501384bd27SBrian Somers switch ((pid = fork())) { 17511384bd27SBrian Somers case -1: 1752a33b2ef7SBrian Somers log_Printf(LogERROR, "fork(2): %s\n", strerror(errno)); 17531384bd27SBrian Somers close(fds[0]); 17541384bd27SBrian Somers close(fds[1]); 17551384bd27SBrian Somers return; 17561384bd27SBrian Somers case 0: 17571384bd27SBrian Somers close(fds[1]); 17584be0e57dSBrian Somers bundle_LockTun(bundle); /* update pid */ 17594be0e57dSBrian Somers read(fds[0], &done, 1); /* uu_locks are mine ! */ 17604be0e57dSBrian Somers close(fds[0]); 17611384bd27SBrian Somers setsid(); 176206b47306SBrian Somers bundle_ChangedPID(bundle); 176342df3c25SBrian Somers log_Printf(LogDEBUG, "%ld -> %ld: %s session control\n", 176442df3c25SBrian Somers (long)orig, (long)getpid(), 17651384bd27SBrian Somers holdsession ? "Passed" : "Dropped"); 17667e778f13SBrian Somers timer_InitService(0); /* Start the Timer Service */ 17671384bd27SBrian Somers break; 17681384bd27SBrian Somers default: 17694be0e57dSBrian Somers close(fds[0]); 17705d9e6103SBrian Somers /* Give away all our physical locks (to the final process) */ 17711384bd27SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 17721384bd27SBrian Somers if (dl->state != DATALINK_CLOSED) 17735d9e6103SBrian Somers physical_ChangedPid(dl->physical, pid); 17744be0e57dSBrian Somers write(fds[1], "!", 1); /* done */ 17754be0e57dSBrian Somers close(fds[1]); 17762cb305afSBrian Somers _exit(0); 17771384bd27SBrian Somers break; 17781384bd27SBrian Somers } 17791384bd27SBrian Somers break; 17801384bd27SBrian Somers default: 17814be0e57dSBrian Somers close(fds[0]); 17825d9e6103SBrian Somers /* Give away all our physical locks (to the intermediate process) */ 17831384bd27SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 17841384bd27SBrian Somers if (dl->state != DATALINK_CLOSED) 17855d9e6103SBrian Somers physical_ChangedPid(dl->physical, pid); 17864be0e57dSBrian Somers write(fds[1], "!", 1); /* done */ 17874be0e57dSBrian Somers close(fds[1]); 17881384bd27SBrian Somers if (holdsession) { 17891384bd27SBrian Somers int fd, status; 17901384bd27SBrian Somers 17911384bd27SBrian Somers timer_TermService(); 17921384bd27SBrian Somers signal(SIGPIPE, SIG_DFL); 17931384bd27SBrian Somers signal(SIGALRM, SIG_DFL); 17941384bd27SBrian Somers signal(SIGHUP, SIG_DFL); 17951384bd27SBrian Somers signal(SIGTERM, SIG_DFL); 17961384bd27SBrian Somers signal(SIGINT, SIG_DFL); 17971384bd27SBrian Somers signal(SIGQUIT, SIG_DFL); 17981384bd27SBrian Somers for (fd = getdtablesize(); fd >= 0; fd--) 17991384bd27SBrian Somers close(fd); 18001384bd27SBrian Somers /* 18011384bd27SBrian Somers * Reap the intermediate process. As we're not exiting but the 18021384bd27SBrian Somers * intermediate is, we don't want it to become defunct. 18031384bd27SBrian Somers */ 18041384bd27SBrian Somers waitpid(pid, &status, 0); 18058e7b8599SBrian Somers /* Tweak our process arguments.... */ 1806ebe96675SBrian Somers SetTitle("session owner"); 180768602c3eSBrian Somers #ifndef NOSUID 1808a19a5c02SBrian Somers setuid(ID0realuid()); 180968602c3eSBrian Somers #endif 18101384bd27SBrian Somers /* 18111384bd27SBrian Somers * Hang around for a HUP. This should happen as soon as the 18128e7bd08eSBrian Somers * ppp that we passed our ctty descriptor to closes it. 18138e7bd08eSBrian Somers * NOTE: If this process dies, the passed descriptor becomes 18141384bd27SBrian Somers * invalid and will give a select() error by setting one 18151384bd27SBrian Somers * of the error fds, aborting the other ppp. We don't 18161384bd27SBrian Somers * want that to happen ! 18171384bd27SBrian Somers */ 18181384bd27SBrian Somers pause(); 18191384bd27SBrian Somers } 18202cb305afSBrian Somers _exit(0); 18211384bd27SBrian Somers break; 18221384bd27SBrian Somers } 18231384bd27SBrian Somers } 18249b5f8ffdSBrian Somers 1825057f1760SBrian Somers unsigned 18269b5f8ffdSBrian Somers bundle_HighestState(struct bundle *bundle) 18279b5f8ffdSBrian Somers { 18289b5f8ffdSBrian Somers struct datalink *dl; 1829057f1760SBrian Somers unsigned result = DATALINK_CLOSED; 18309b5f8ffdSBrian Somers 18319b5f8ffdSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 18329b5f8ffdSBrian Somers if (result < dl->state) 18339b5f8ffdSBrian Somers result = dl->state; 18349b5f8ffdSBrian Somers 18359b5f8ffdSBrian Somers return result; 18369b5f8ffdSBrian Somers } 1837991c2a7bSBrian Somers 1838991c2a7bSBrian Somers int 1839991c2a7bSBrian Somers bundle_Exception(struct bundle *bundle, int fd) 1840991c2a7bSBrian Somers { 1841991c2a7bSBrian Somers struct datalink *dl; 1842991c2a7bSBrian Somers 1843991c2a7bSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 1844991c2a7bSBrian Somers if (dl->physical->fd == fd) { 1845991c2a7bSBrian Somers datalink_Down(dl, CLOSE_NORMAL); 1846991c2a7bSBrian Somers return 1; 1847991c2a7bSBrian Somers } 1848991c2a7bSBrian Somers 1849991c2a7bSBrian Somers return 0; 1850991c2a7bSBrian Somers } 18511d1fc017SBrian Somers 18521d1fc017SBrian Somers void 185330949fd4SBrian Somers bundle_AdjustFilters(struct bundle *bundle, struct ncpaddr *local, 185430949fd4SBrian Somers struct ncpaddr *remote) 18551d1fc017SBrian Somers { 185630949fd4SBrian Somers filter_AdjustAddr(&bundle->filter.in, local, remote, NULL); 185730949fd4SBrian Somers filter_AdjustAddr(&bundle->filter.out, local, remote, NULL); 185830949fd4SBrian Somers filter_AdjustAddr(&bundle->filter.dial, local, remote, NULL); 185930949fd4SBrian Somers filter_AdjustAddr(&bundle->filter.alive, local, remote, NULL); 1860d568d6c4SBrian Somers } 1861d568d6c4SBrian Somers 1862d568d6c4SBrian Somers void 186330949fd4SBrian Somers bundle_AdjustDNS(struct bundle *bundle) 1864d568d6c4SBrian Somers { 186530949fd4SBrian Somers struct in_addr *dns = bundle->ncp.ipcp.ns.dns; 186630949fd4SBrian Somers 1867d568d6c4SBrian Somers filter_AdjustAddr(&bundle->filter.in, NULL, NULL, dns); 1868d568d6c4SBrian Somers filter_AdjustAddr(&bundle->filter.out, NULL, NULL, dns); 1869d568d6c4SBrian Somers filter_AdjustAddr(&bundle->filter.dial, NULL, NULL, dns); 1870d568d6c4SBrian Somers filter_AdjustAddr(&bundle->filter.alive, NULL, NULL, dns); 18711d1fc017SBrian Somers } 1872ab2de065SBrian Somers 1873ab2de065SBrian Somers void 1874ab2de065SBrian Somers bundle_CalculateBandwidth(struct bundle *bundle) 1875ab2de065SBrian Somers { 1876ab2de065SBrian Somers struct datalink *dl; 18776301d506SBrian Somers int sp, overhead, maxoverhead; 1878ab2de065SBrian Somers 1879ab2de065SBrian Somers bundle->bandwidth = 0; 1880c8b9fb53SBrian Somers bundle->iface->mtu = 0; 18816301d506SBrian Somers maxoverhead = 0; 18826301d506SBrian Somers 18836301d506SBrian Somers for (dl = bundle->links; dl; dl = dl->next) { 18846301d506SBrian Somers overhead = ccp_MTUOverhead(&dl->physical->link.ccp); 18856301d506SBrian Somers if (maxoverhead < overhead) 18866301d506SBrian Somers maxoverhead = overhead; 1887ab2de065SBrian Somers if (dl->state == DATALINK_OPEN) { 1888ab2de065SBrian Somers if ((sp = dl->mp.bandwidth) == 0 && 1889ab2de065SBrian Somers (sp = physical_GetSpeed(dl->physical)) == 0) 1890ab2de065SBrian Somers log_Printf(LogDEBUG, "%s: %s: Cannot determine bandwidth\n", 1891ab2de065SBrian Somers dl->name, dl->physical->name.full); 1892ab2de065SBrian Somers else 1893ab2de065SBrian Somers bundle->bandwidth += sp; 1894ab2de065SBrian Somers if (!bundle->ncp.mp.active) { 1895c8b9fb53SBrian Somers bundle->iface->mtu = dl->physical->link.lcp.his_mru; 1896ab2de065SBrian Somers break; 1897ab2de065SBrian Somers } 1898ab2de065SBrian Somers } 18996301d506SBrian Somers } 1900ab2de065SBrian Somers 1901ab2de065SBrian Somers if (bundle->bandwidth == 0) 1902ab2de065SBrian Somers bundle->bandwidth = 115200; /* Shrug */ 1903ab2de065SBrian Somers 19046301d506SBrian Somers if (bundle->ncp.mp.active) { 1905c8b9fb53SBrian Somers bundle->iface->mtu = bundle->ncp.mp.peer_mrru; 19066301d506SBrian Somers overhead = ccp_MTUOverhead(&bundle->ncp.mp.link.ccp); 19076301d506SBrian Somers if (maxoverhead < overhead) 19086301d506SBrian Somers maxoverhead = overhead; 19096301d506SBrian Somers } else if (!bundle->iface->mtu) 1910c8b9fb53SBrian Somers bundle->iface->mtu = DEF_MRU; 1911ab2de065SBrian Somers 1912ab2de065SBrian Somers #ifndef NORADIUS 191394d7be52SBrian Somers if (bundle->radius.valid && bundle->radius.mtu && 1914c8b9fb53SBrian Somers bundle->radius.mtu < bundle->iface->mtu) { 1915ab2de065SBrian Somers log_Printf(LogLCP, "Reducing MTU to radius value %lu\n", 1916ab2de065SBrian Somers bundle->radius.mtu); 1917c8b9fb53SBrian Somers bundle->iface->mtu = bundle->radius.mtu; 1918ab2de065SBrian Somers } 1919ab2de065SBrian Somers #endif 1920ab2de065SBrian Somers 19216301d506SBrian Somers if (maxoverhead) { 1922057f1760SBrian Somers log_Printf(LogLCP, "Reducing MTU from %lu to %lu (CCP requirement)\n", 19236301d506SBrian Somers bundle->iface->mtu, bundle->iface->mtu - maxoverhead); 19246301d506SBrian Somers bundle->iface->mtu -= maxoverhead; 19256301d506SBrian Somers } 19266301d506SBrian Somers 192794d7be52SBrian Somers tun_configure(bundle); 192803a2501aSBrian Somers 192903a2501aSBrian Somers route_UpdateMTU(bundle); 1930ab2de065SBrian Somers } 1931ab2de065SBrian Somers 1932ab2de065SBrian Somers void 1933ab2de065SBrian Somers bundle_AutoAdjust(struct bundle *bundle, int percent, int what) 1934ab2de065SBrian Somers { 1935ab2de065SBrian Somers struct datalink *dl, *choice, *otherlinkup; 1936ab2de065SBrian Somers 1937ab2de065SBrian Somers choice = otherlinkup = NULL; 1938ab2de065SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 1939ab2de065SBrian Somers if (dl->physical->type == PHYS_AUTO) { 1940ab2de065SBrian Somers if (dl->state == DATALINK_OPEN) { 1941ab2de065SBrian Somers if (what == AUTO_DOWN) { 1942ab2de065SBrian Somers if (choice) 1943ab2de065SBrian Somers otherlinkup = choice; 1944ab2de065SBrian Somers choice = dl; 1945ab2de065SBrian Somers } 1946ab2de065SBrian Somers } else if (dl->state == DATALINK_CLOSED) { 1947ab2de065SBrian Somers if (what == AUTO_UP) { 1948ab2de065SBrian Somers choice = dl; 1949ab2de065SBrian Somers break; 1950ab2de065SBrian Somers } 1951ab2de065SBrian Somers } else { 1952ab2de065SBrian Somers /* An auto link in an intermediate state - forget it for the moment */ 1953ab2de065SBrian Somers choice = NULL; 1954ab2de065SBrian Somers break; 1955ab2de065SBrian Somers } 1956ab2de065SBrian Somers } else if (dl->state == DATALINK_OPEN && what == AUTO_DOWN) 1957ab2de065SBrian Somers otherlinkup = dl; 1958ab2de065SBrian Somers 1959ab2de065SBrian Somers if (choice) { 1960ab2de065SBrian Somers if (what == AUTO_UP) { 1961ab2de065SBrian Somers log_Printf(LogPHASE, "%d%% saturation -> Opening link ``%s''\n", 1962ab2de065SBrian Somers percent, choice->name); 1963ab2de065SBrian Somers datalink_Up(choice, 1, 1); 1964a339e644SBrian Somers mp_CheckAutoloadTimer(&bundle->ncp.mp); 1965ab2de065SBrian Somers } else if (otherlinkup) { /* Only bring the second-last link down */ 1966ab2de065SBrian Somers log_Printf(LogPHASE, "%d%% saturation -> Closing link ``%s''\n", 1967ab2de065SBrian Somers percent, choice->name); 1968d2f5232dSBrian Somers datalink_Close(choice, CLOSE_STAYDOWN); 1969a339e644SBrian Somers mp_CheckAutoloadTimer(&bundle->ncp.mp); 1970ab2de065SBrian Somers } 1971ab2de065SBrian Somers } 1972ab2de065SBrian Somers } 1973ab2de065SBrian Somers 1974ab2de065SBrian Somers int 1975ab2de065SBrian Somers bundle_WantAutoloadTimer(struct bundle *bundle) 1976ab2de065SBrian Somers { 1977ab2de065SBrian Somers struct datalink *dl; 1978ab2de065SBrian Somers int autolink, opened; 1979ab2de065SBrian Somers 1980ab2de065SBrian Somers if (bundle->phase == PHASE_NETWORK) { 1981ab2de065SBrian Somers for (autolink = opened = 0, dl = bundle->links; dl; dl = dl->next) 1982ab2de065SBrian Somers if (dl->physical->type == PHYS_AUTO) { 1983ab2de065SBrian Somers if (++autolink == 2 || (autolink == 1 && opened)) 1984ab2de065SBrian Somers /* Two auto links or one auto and one open in NETWORK phase */ 1985ab2de065SBrian Somers return 1; 1986ab2de065SBrian Somers } else if (dl->state == DATALINK_OPEN) { 1987ab2de065SBrian Somers opened++; 1988ab2de065SBrian Somers if (autolink) 1989ab2de065SBrian Somers /* One auto and one open link in NETWORK phase */ 1990ab2de065SBrian Somers return 1; 1991ab2de065SBrian Somers } 1992ab2de065SBrian Somers } 1993ab2de065SBrian Somers 1994ab2de065SBrian Somers return 0; 1995ab2de065SBrian Somers } 199606b47306SBrian Somers 199706b47306SBrian Somers void 199806b47306SBrian Somers bundle_ChangedPID(struct bundle *bundle) 199906b47306SBrian Somers { 200006b47306SBrian Somers #ifdef TUNSIFPID 200106b47306SBrian Somers ioctl(bundle->dev.fd, TUNSIFPID, 0); 200206b47306SBrian Somers #endif 200306b47306SBrian Somers } 200446df5aa7SBrian Somers 200546df5aa7SBrian Somers int 200646df5aa7SBrian Somers bundle_Uptime(struct bundle *bundle) 200746df5aa7SBrian Somers { 200846df5aa7SBrian Somers if (bundle->upat) 200946df5aa7SBrian Somers return time(NULL) - bundle->upat; 201046df5aa7SBrian Somers 201146df5aa7SBrian Somers return 0; 201246df5aa7SBrian Somers } 2013