17a6f8720SBrian Somers /*- 2*1de7b4b8SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3*1de7b4b8SPedro F. Giffuni * 47a6f8720SBrian Somers * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org> 57a6f8720SBrian Somers * All rights reserved. 67a6f8720SBrian Somers * 77a6f8720SBrian Somers * Redistribution and use in source and binary forms, with or without 87a6f8720SBrian Somers * modification, are permitted provided that the following conditions 97a6f8720SBrian Somers * are met: 107a6f8720SBrian Somers * 1. Redistributions of source code must retain the above copyright 117a6f8720SBrian Somers * notice, this list of conditions and the following disclaimer. 127a6f8720SBrian Somers * 2. Redistributions in binary form must reproduce the above copyright 137a6f8720SBrian Somers * notice, this list of conditions and the following disclaimer in the 147a6f8720SBrian Somers * documentation and/or other materials provided with the distribution. 157a6f8720SBrian Somers * 167a6f8720SBrian Somers * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 177a6f8720SBrian Somers * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 187a6f8720SBrian Somers * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 197a6f8720SBrian Somers * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 207a6f8720SBrian Somers * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 217a6f8720SBrian Somers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 227a6f8720SBrian Somers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 237a6f8720SBrian Somers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 247a6f8720SBrian Somers * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 257a6f8720SBrian Somers * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 267a6f8720SBrian Somers * SUCH DAMAGE. 277a6f8720SBrian Somers * 2897d92980SPeter Wemm * $FreeBSD$ 297a6f8720SBrian Somers */ 307a6f8720SBrian Somers 311384bd27SBrian Somers #include <sys/param.h> 327a6f8720SBrian Somers #include <sys/socket.h> 337a6f8720SBrian Somers #include <netinet/in.h> 347a6f8720SBrian Somers #include <net/if.h> 353a7b6d76SBrian Somers #include <net/if_tun.h> /* For TUNS* ioctls */ 367a6f8720SBrian Somers #include <net/route.h> 37eaa4df37SBrian Somers #include <netinet/in_systm.h> 38eaa4df37SBrian Somers #include <netinet/ip.h> 391fa665f5SBrian Somers #include <sys/un.h> 407a6f8720SBrian Somers 417a6f8720SBrian Somers #include <errno.h> 427a6f8720SBrian Somers #include <fcntl.h> 43cf0a3940SBrian Somers #ifdef __OpenBSD__ 44cf0a3940SBrian Somers #include <util.h> 45cf0a3940SBrian Somers #else 46cf0a3940SBrian Somers #include <libutil.h> 47cf0a3940SBrian Somers #endif 4847723d29SBrian Somers #include <paths.h> 496eafd353SBrian Somers #include <stdarg.h> 507a6f8720SBrian Somers #include <stdio.h> 516f384573SBrian Somers #include <stdlib.h> 527a6f8720SBrian Somers #include <string.h> 5396c9bb21SBrian Somers #include <sys/uio.h> 5454cd8e13SBrian Somers #include <sys/wait.h> 557a6f8720SBrian Somers #include <termios.h> 567a6f8720SBrian Somers #include <unistd.h> 577a6f8720SBrian Somers 585d9e6103SBrian Somers #include "layer.h" 59c9e11a11SBrian Somers #include "defs.h" 607a6f8720SBrian Somers #include "command.h" 617a6f8720SBrian Somers #include "mbuf.h" 627a6f8720SBrian Somers #include "log.h" 637a6f8720SBrian Somers #include "id.h" 647a6f8720SBrian Somers #include "timer.h" 657a6f8720SBrian Somers #include "fsm.h" 667a6f8720SBrian Somers #include "iplist.h" 67879ed6faSBrian Somers #include "lqr.h" 68455aabc3SBrian Somers #include "hdlc.h" 697a6f8720SBrian Somers #include "throughput.h" 70eaa4df37SBrian Somers #include "slcompress.h" 7130949fd4SBrian Somers #include "ncpaddr.h" 7230949fd4SBrian Somers #include "ip.h" 737a6f8720SBrian Somers #include "ipcp.h" 745ca5389aSBrian Somers #include "filter.h" 752f786681SBrian Somers #include "descriptor.h" 767a6f8720SBrian Somers #include "route.h" 777a6f8720SBrian Somers #include "lcp.h" 787a6f8720SBrian Somers #include "ccp.h" 793b0f8d2eSBrian Somers #include "link.h" 803b0f8d2eSBrian Somers #include "mp.h" 81972a1bcfSBrian Somers #ifndef NORADIUS 82972a1bcfSBrian Somers #include "radius.h" 83972a1bcfSBrian Somers #endif 8430949fd4SBrian Somers #include "ipv6cp.h" 8530949fd4SBrian Somers #include "ncp.h" 863b0f8d2eSBrian Somers #include "bundle.h" 87455aabc3SBrian Somers #include "async.h" 88455aabc3SBrian Somers #include "physical.h" 89455aabc3SBrian Somers #include "auth.h" 905d9e6103SBrian Somers #include "proto.h" 91455aabc3SBrian Somers #include "chap.h" 92455aabc3SBrian Somers #include "tun.h" 9385b542cfSBrian Somers #include "prompt.h" 943006ec67SBrian Somers #include "chat.h" 9592b09558SBrian Somers #include "cbcp.h" 963006ec67SBrian Somers #include "datalink.h" 978fa6ebe4SBrian Somers #include "iface.h" 9874457d3dSBrian Somers #include "server.h" 99971abb29SBrian Somers #include "probe.h" 100fb11a9c2SBrian Somers #ifndef NODES 101019d32bfSBrian Somers #include "mppe.h" 10203a2501aSBrian Somers #endif 1037a6f8720SBrian Somers 10491cbd2eeSBrian Somers #define SCATTER_SEGMENTS 7 /* version, datalink, name, physical, 10591cbd2eeSBrian Somers throughput, throughput, device */ 10687c3786eSBrian Somers 1072cb305afSBrian Somers #define SEND_MAXFD 3 /* Max file descriptors passed through 10887c3786eSBrian Somers the local domain socket */ 10996c9bb21SBrian Somers 11004eaa58cSBrian Somers static int bundle_RemainingIdleTime(struct bundle *); 11104eaa58cSBrian Somers 112182c898aSBrian Somers static const char * const PhaseNames[] = { 113455aabc3SBrian Somers "Dead", "Establish", "Authenticate", "Network", "Terminate" 114455aabc3SBrian Somers }; 115455aabc3SBrian Somers 116455aabc3SBrian Somers const char * 117455aabc3SBrian Somers bundle_PhaseName(struct bundle *bundle) 1187a6f8720SBrian Somers { 119455aabc3SBrian Somers return bundle->phase <= PHASE_TERMINATE ? 120455aabc3SBrian Somers PhaseNames[bundle->phase] : "unknown"; 1217a6f8720SBrian Somers } 1227a6f8720SBrian Somers 123455aabc3SBrian Somers void 1245563ebdeSBrian Somers bundle_NewPhase(struct bundle *bundle, u_int new) 125455aabc3SBrian Somers { 126aef795ccSBrian Somers if (new == bundle->phase) 127aef795ccSBrian Somers return; 128aef795ccSBrian Somers 129e2ebb036SBrian Somers if (new <= PHASE_TERMINATE) 130dd7e2610SBrian Somers log_Printf(LogPHASE, "bundle: %s\n", PhaseNames[new]); 1317a6f8720SBrian Somers 132455aabc3SBrian Somers switch (new) { 133455aabc3SBrian Somers case PHASE_DEAD: 134455aabc3SBrian Somers bundle->phase = new; 135fb11a9c2SBrian Somers #ifndef NODES 136019d32bfSBrian Somers MPPE_MasterKeyValid = 0; 13764602637SBrian Somers #endif 138019d32bfSBrian Somers log_DisplayPrompts(); 139455aabc3SBrian Somers break; 140455aabc3SBrian Somers 141455aabc3SBrian Somers case PHASE_ESTABLISH: 142455aabc3SBrian Somers bundle->phase = new; 143455aabc3SBrian Somers break; 144455aabc3SBrian Somers 145455aabc3SBrian Somers case PHASE_AUTHENTICATE: 146455aabc3SBrian Somers bundle->phase = new; 1470f2f3eb3SBrian Somers log_DisplayPrompts(); 148455aabc3SBrian Somers break; 149455aabc3SBrian Somers 150455aabc3SBrian Somers case PHASE_NETWORK: 15130949fd4SBrian Somers if (ncp_fsmStart(&bundle->ncp, bundle)) { 152673903ecSBrian Somers bundle->phase = new; 1530f2f3eb3SBrian Somers log_DisplayPrompts(); 15430949fd4SBrian Somers } else { 15530949fd4SBrian Somers log_Printf(LogPHASE, "bundle: All NCPs are disabled\n"); 15630949fd4SBrian Somers bundle_Close(bundle, NULL, CLOSE_STAYDOWN); 15730949fd4SBrian Somers } 158673903ecSBrian Somers break; 159455aabc3SBrian Somers 160455aabc3SBrian Somers case PHASE_TERMINATE: 161455aabc3SBrian Somers bundle->phase = new; 162673903ecSBrian Somers mp_Down(&bundle->ncp.mp); 1630f2f3eb3SBrian Somers log_DisplayPrompts(); 164455aabc3SBrian Somers break; 1657a6f8720SBrian Somers } 1667a6f8720SBrian Somers } 1677a6f8720SBrian Somers 1686d666775SBrian Somers static void 169057f1760SBrian Somers bundle_LayerStart(void *v __unused, struct fsm *fp __unused) 1707a6f8720SBrian Somers { 1713006ec67SBrian Somers /* The given FSM is about to start up ! */ 1727a6f8720SBrian Somers } 1737a6f8720SBrian Somers 1745cf4388bSBrian Somers 175b42135deSBrian Somers void 1765cf4388bSBrian Somers bundle_Notify(struct bundle *bundle, char c) 1775cf4388bSBrian Somers { 1785cf4388bSBrian Somers if (bundle->notify.fd != -1) { 179b42135deSBrian Somers int ret; 180b42135deSBrian Somers 181b42135deSBrian Somers ret = write(bundle->notify.fd, &c, 1); 182b42135deSBrian Somers if (c != EX_REDIAL && c != EX_RECONNECT) { 183b42135deSBrian Somers if (ret == 1) 184b42135deSBrian Somers log_Printf(LogCHAT, "Parent notified of %s\n", 1855a83ad1eSBrian Somers c == EX_NORMAL ? "success" : "failure"); 1865cf4388bSBrian Somers else 187b42135deSBrian Somers log_Printf(LogERROR, "Failed to notify parent of success\n"); 1885cf4388bSBrian Somers close(bundle->notify.fd); 1895cf4388bSBrian Somers bundle->notify.fd = -1; 190b42135deSBrian Somers } else if (ret == 1) 191b42135deSBrian Somers log_Printf(LogCHAT, "Parent notified of %s\n", ex_desc(c)); 192b42135deSBrian Somers else 193b42135deSBrian Somers log_Printf(LogERROR, "Failed to notify parent of %s\n", ex_desc(c)); 1945cf4388bSBrian Somers } 1955cf4388bSBrian Somers } 1963b0f8d2eSBrian Somers 1976d666775SBrian Somers static void 1986f8e9f0aSBrian Somers bundle_ClearQueues(void *v) 1996f8e9f0aSBrian Somers { 2006f8e9f0aSBrian Somers struct bundle *bundle = (struct bundle *)v; 2016f8e9f0aSBrian Somers struct datalink *dl; 2026f8e9f0aSBrian Somers 2036f8e9f0aSBrian Somers log_Printf(LogPHASE, "Clearing choked output queue\n"); 2046f8e9f0aSBrian Somers timer_Stop(&bundle->choked.timer); 2056f8e9f0aSBrian Somers 2066f8e9f0aSBrian Somers /* 2076f8e9f0aSBrian Somers * Emergency time: 2086f8e9f0aSBrian Somers * 2096f8e9f0aSBrian Somers * We've had a full queue for PACKET_DEL_SECS seconds without being 2106f8e9f0aSBrian Somers * able to get rid of any of the packets. We've probably given up 2116f8e9f0aSBrian Somers * on the redials at this point, and the queued data has almost 2126f8e9f0aSBrian Somers * definitely been timed out by the layer above. As this is preventing 2136f8e9f0aSBrian Somers * us from reading the TUN_NAME device (we don't want to buffer stuff 2146f8e9f0aSBrian Somers * indefinitely), we may as well nuke this data and start with a clean 2156f8e9f0aSBrian Somers * slate ! 2166f8e9f0aSBrian Somers * 2176f8e9f0aSBrian Somers * Unfortunately, this has the side effect of shafting any compression 2186f8e9f0aSBrian Somers * dictionaries in use (causing the relevant RESET_REQ/RESET_ACK). 2196f8e9f0aSBrian Somers */ 2206f8e9f0aSBrian Somers 22130949fd4SBrian Somers ncp_DeleteQueues(&bundle->ncp); 2226f8e9f0aSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 2236f8e9f0aSBrian Somers physical_DeleteQueue(dl->physical); 2246f8e9f0aSBrian Somers } 2256f8e9f0aSBrian Somers 2266f8e9f0aSBrian Somers static void 227ff0f9439SBrian Somers bundle_LinkAdded(struct bundle *bundle, struct datalink *dl) 228ff0f9439SBrian Somers { 229ff0f9439SBrian Somers bundle->phys_type.all |= dl->physical->type; 230ff0f9439SBrian Somers if (dl->state == DATALINK_OPEN) 231ff0f9439SBrian Somers bundle->phys_type.open |= dl->physical->type; 232ff0f9439SBrian Somers 233bf1eaec5SBrian Somers #ifndef NORADIUS 234bf1eaec5SBrian Somers if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) 235bf1eaec5SBrian Somers != bundle->phys_type.open && bundle->session.timer.state == TIMER_STOPPED) 236bf1eaec5SBrian Somers if (bundle->radius.sessiontime) 237bf1eaec5SBrian Somers bundle_StartSessionTimer(bundle, 0); 238bf1eaec5SBrian Somers #endif 239bf1eaec5SBrian Somers 240ff0f9439SBrian Somers if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) 241ff0f9439SBrian Somers != bundle->phys_type.open && bundle->idle.timer.state == TIMER_STOPPED) 242ff0f9439SBrian Somers /* We may need to start our idle timer */ 2430a4b6c5cSBrian Somers bundle_StartIdleTimer(bundle, 0); 244ff0f9439SBrian Somers } 245ff0f9439SBrian Somers 24692b09558SBrian Somers void 247ff0f9439SBrian Somers bundle_LinksRemoved(struct bundle *bundle) 248ff0f9439SBrian Somers { 249ff0f9439SBrian Somers struct datalink *dl; 250ff0f9439SBrian Somers 251ff0f9439SBrian Somers bundle->phys_type.all = bundle->phys_type.open = 0; 252ff0f9439SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 253ff0f9439SBrian Somers bundle_LinkAdded(bundle, dl); 254ff0f9439SBrian Somers 255ab2de065SBrian Somers bundle_CalculateBandwidth(bundle); 256ab2de065SBrian Somers mp_CheckAutoloadTimer(&bundle->ncp.mp); 257ab2de065SBrian Somers 258ff0f9439SBrian Somers if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) 259bf1eaec5SBrian Somers == bundle->phys_type.open) { 260bf1eaec5SBrian Somers #ifndef NORADIUS 261bf1eaec5SBrian Somers if (bundle->radius.sessiontime) 262bf1eaec5SBrian Somers bundle_StopSessionTimer(bundle); 263bf1eaec5SBrian Somers #endif 264ff0f9439SBrian Somers bundle_StopIdleTimer(bundle); 265ff0f9439SBrian Somers } 266bf1eaec5SBrian Somers } 26704eaa58cSBrian Somers 26804eaa58cSBrian Somers static void 2696f384573SBrian Somers bundle_LayerUp(void *v, struct fsm *fp) 2707a6f8720SBrian Somers { 2713006ec67SBrian Somers /* 2723006ec67SBrian Somers * The given fsm is now up 273ab2de065SBrian Somers * If it's an LCP, adjust our phys_mode.open value and check the 274ab2de065SBrian Somers * autoload timer. 275ab2de065SBrian Somers * If it's the first NCP, calculate our bandwidth 276dade2407SBrian Somers * If it's the first NCP, set our ``upat'' time 2773b0f8d2eSBrian Somers * If it's the first NCP, start the idle timer. 278ab2de065SBrian Somers * If it's an NCP, tell our -background parent to go away. 279ab2de065SBrian Somers * If it's the first NCP, start the autoload timer 2803006ec67SBrian Somers */ 2816f384573SBrian Somers struct bundle *bundle = (struct bundle *)v; 2826d666775SBrian Somers 2835563ebdeSBrian Somers if (fp->proto == PROTO_LCP) { 284ff0f9439SBrian Somers struct physical *p = link2physical(fp->link); 285ff0f9439SBrian Somers 286ff0f9439SBrian Somers bundle_LinkAdded(bundle, p->dl); 287ab2de065SBrian Somers mp_CheckAutoloadTimer(&bundle->ncp.mp); 28830949fd4SBrian Somers } else if (isncp(fp->proto)) { 28930949fd4SBrian Somers if (ncp_LayersOpen(&fp->bundle->ncp) == 1) { 290ab2de065SBrian Somers bundle_CalculateBandwidth(fp->bundle); 291dade2407SBrian Somers time(&bundle->upat); 292bf1eaec5SBrian Somers #ifndef NORADIUS 293bf1eaec5SBrian Somers if (bundle->radius.sessiontime) 294bf1eaec5SBrian Somers bundle_StartSessionTimer(bundle, 0); 295bf1eaec5SBrian Somers #endif 2960a4b6c5cSBrian Somers bundle_StartIdleTimer(bundle, 0); 297ab2de065SBrian Somers mp_CheckAutoloadTimer(&fp->bundle->ncp.mp); 29830949fd4SBrian Somers } 29930949fd4SBrian Somers bundle_Notify(bundle, EX_NORMAL); 3006301d506SBrian Somers } else if (fp->proto == PROTO_CCP) 3016301d506SBrian Somers bundle_CalculateBandwidth(fp->bundle); /* Against ccp_MTUOverhead */ 302ab886ad0SBrian Somers } 3037a6f8720SBrian Somers 3046d666775SBrian Somers static void 3056d666775SBrian Somers bundle_LayerDown(void *v, struct fsm *fp) 3066d666775SBrian Somers { 3076d666775SBrian Somers /* 3086d666775SBrian Somers * The given FSM has been told to come down. 309ab886ad0SBrian Somers * If it's our last NCP, stop the idle timer. 310dade2407SBrian Somers * If it's our last NCP, clear our ``upat'' value. 311ab2de065SBrian Somers * If it's our last NCP, stop the autoload timer 312ff0f9439SBrian Somers * If it's an LCP, adjust our phys_type.open value and any timers. 31386b1f0d7SBrian Somers * If it's an LCP and we're in multilink mode, adjust our tun 314c8f30703SBrian Somers * If it's the last LCP, down all NCPs 31586b1f0d7SBrian Somers * speed and make sure our minimum sequence number is adjusted. 3166d666775SBrian Somers */ 317ab886ad0SBrian Somers 318ab886ad0SBrian Somers struct bundle *bundle = (struct bundle *)v; 319ab886ad0SBrian Somers 32030949fd4SBrian Somers if (isncp(fp->proto)) { 32130949fd4SBrian Somers if (ncp_LayersOpen(&fp->bundle->ncp) == 0) { 322bf1eaec5SBrian Somers #ifndef NORADIUS 323bf1eaec5SBrian Somers if (bundle->radius.sessiontime) 324bf1eaec5SBrian Somers bundle_StopSessionTimer(bundle); 325bf1eaec5SBrian Somers #endif 326ab886ad0SBrian Somers bundle_StopIdleTimer(bundle); 327dade2407SBrian Somers bundle->upat = 0; 328ab2de065SBrian Somers mp_StopAutoloadTimer(&bundle->ncp.mp); 32930949fd4SBrian Somers } 330ab2de065SBrian Somers } else if (fp->proto == PROTO_LCP) { 3313b0f8d2eSBrian Somers struct datalink *dl; 33286b1f0d7SBrian Somers struct datalink *lost; 333c8f30703SBrian Somers int others_active; 334c8f30703SBrian Somers 335c8f30703SBrian Somers bundle_LinksRemoved(bundle); /* adjust timers & phys_type values */ 3363b0f8d2eSBrian Somers 33786b1f0d7SBrian Somers lost = NULL; 338c8f30703SBrian Somers others_active = 0; 339c8f30703SBrian Somers for (dl = bundle->links; dl; dl = dl->next) { 34086b1f0d7SBrian Somers if (fp == &dl->physical->link.lcp.fsm) 34186b1f0d7SBrian Somers lost = dl; 342c8f30703SBrian Somers else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) 343c8f30703SBrian Somers others_active++; 344c8f30703SBrian Somers } 34586b1f0d7SBrian Somers 346c8f30703SBrian Somers if (bundle->ncp.mp.active) { 347ab2de065SBrian Somers bundle_CalculateBandwidth(bundle); 34886b1f0d7SBrian Somers 34986b1f0d7SBrian Somers if (lost) 35086b1f0d7SBrian Somers mp_LinkLost(&bundle->ncp.mp, lost); 35186b1f0d7SBrian Somers else 352a33b2ef7SBrian Somers log_Printf(LogALERT, "Oops, lost an unrecognised datalink (%s) !\n", 35386b1f0d7SBrian Somers fp->link->name); 3543b0f8d2eSBrian Somers } 355c8f30703SBrian Somers 356356bf92dSBrian Somers if (!others_active) { 357c8f30703SBrian Somers /* Down the NCPs. We don't expect to get fsm_Close()d ourself ! */ 35830949fd4SBrian Somers ncp2initial(&bundle->ncp); 359356bf92dSBrian Somers mp_Down(&bundle->ncp.mp); 360356bf92dSBrian Somers } 3616d666775SBrian Somers } 362ff0f9439SBrian Somers } 3636d666775SBrian Somers 3646d666775SBrian Somers static void 3656d666775SBrian Somers bundle_LayerFinish(void *v, struct fsm *fp) 3666d666775SBrian Somers { 3676d666775SBrian Somers /* The given fsm is now down (fp cannot be NULL) 3686d666775SBrian Somers * 369dd7e2610SBrian Somers * If it's the last NCP, fsm_Close all LCPs 370356bf92dSBrian Somers * If it's the last NCP, bring any MP layer down 3716d666775SBrian Somers */ 3726d666775SBrian Somers 3736d666775SBrian Somers struct bundle *bundle = (struct bundle *)v; 3746d666775SBrian Somers struct datalink *dl; 3756d666775SBrian Somers 37630949fd4SBrian Somers if (isncp(fp->proto) && !ncp_LayersUnfinished(&bundle->ncp)) { 37726afeaa2SBrian Somers if (bundle_Phase(bundle) != PHASE_DEAD) 37825092092SBrian Somers bundle_NewPhase(bundle, PHASE_TERMINATE); 3796d666775SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 380c8f30703SBrian Somers if (dl->state == DATALINK_OPEN) 381d4d5d2f8SBrian Somers datalink_Close(dl, CLOSE_STAYDOWN); 38209206a6fSBrian Somers fsm2initial(fp); 383356bf92dSBrian Somers mp_Down(&bundle->ncp.mp); 3843b0f8d2eSBrian Somers } 3853b0f8d2eSBrian Somers } 3866d666775SBrian Somers 3877a6f8720SBrian Somers void 3889c81b87dSBrian Somers bundle_Close(struct bundle *bundle, const char *name, int how) 3897a6f8720SBrian Somers { 390455aabc3SBrian Somers /* 3913006ec67SBrian Somers * Please close the given datalink. 392dd7e2610SBrian Somers * If name == NULL or name is the last datalink, fsm_Close all NCPs 3936f384573SBrian Somers * (except our MP) 3943b0f8d2eSBrian Somers * If it isn't the last datalink, just Close that datalink. 395455aabc3SBrian Somers */ 3967a6f8720SBrian Somers 3973b0f8d2eSBrian Somers struct datalink *dl, *this_dl; 3983b0f8d2eSBrian Somers int others_active; 3993006ec67SBrian Somers 4003b0f8d2eSBrian Somers others_active = 0; 4013b0f8d2eSBrian Somers this_dl = NULL; 4023b0f8d2eSBrian Somers 4033b0f8d2eSBrian Somers for (dl = bundle->links; dl; dl = dl->next) { 4043b0f8d2eSBrian Somers if (name && !strcasecmp(name, dl->name)) 4053b0f8d2eSBrian Somers this_dl = dl; 4063b0f8d2eSBrian Somers if (name == NULL || this_dl == dl) { 4079c81b87dSBrian Somers switch (how) { 4089c81b87dSBrian Somers case CLOSE_LCP: 4099c81b87dSBrian Somers datalink_DontHangup(dl); 4102fc2f705SBrian Somers break; 4119c81b87dSBrian Somers case CLOSE_STAYDOWN: 4123006ec67SBrian Somers datalink_StayDown(dl); 4139c81b87dSBrian Somers break; 4149c81b87dSBrian Somers } 4153b0f8d2eSBrian Somers } else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) 4163b0f8d2eSBrian Somers others_active++; 4173b0f8d2eSBrian Somers } 4183b0f8d2eSBrian Somers 4193b0f8d2eSBrian Somers if (name && this_dl == NULL) { 420dd7e2610SBrian Somers log_Printf(LogWARN, "%s: Invalid datalink name\n", name); 4213b0f8d2eSBrian Somers return; 4223b0f8d2eSBrian Somers } 4233b0f8d2eSBrian Somers 4243b0f8d2eSBrian Somers if (!others_active) { 425bf1eaec5SBrian Somers #ifndef NORADIUS 426bf1eaec5SBrian Somers if (bundle->radius.sessiontime) 427bf1eaec5SBrian Somers bundle_StopSessionTimer(bundle); 428bf1eaec5SBrian Somers #endif 42904eaa58cSBrian Somers bundle_StopIdleTimer(bundle); 43030949fd4SBrian Somers if (ncp_LayersUnfinished(&bundle->ncp)) 43130949fd4SBrian Somers ncp_Close(&bundle->ncp); 4323b0f8d2eSBrian Somers else { 43330949fd4SBrian Somers ncp2initial(&bundle->ncp); 434356bf92dSBrian Somers mp_Down(&bundle->ncp.mp); 435d345321bSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 4369c81b87dSBrian Somers datalink_Close(dl, how); 4377a6f8720SBrian Somers } 4383b0f8d2eSBrian Somers } else if (this_dl && this_dl->state != DATALINK_CLOSED && 4393b0f8d2eSBrian Somers this_dl->state != DATALINK_HANGUP) 4409c81b87dSBrian Somers datalink_Close(this_dl, how); 441d2fd8d77SBrian Somers } 4427a6f8720SBrian Somers 4431bc9b5baSBrian Somers void 444899011c4SBrian Somers bundle_Down(struct bundle *bundle, int how) 4451bc9b5baSBrian Somers { 4461bc9b5baSBrian Somers struct datalink *dl; 4471bc9b5baSBrian Somers 4481bc9b5baSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 449899011c4SBrian Somers datalink_Down(dl, how); 4501bc9b5baSBrian Somers } 4511bc9b5baSBrian Somers 4522f786681SBrian Somers static int 453f013f33eSBrian Somers bundle_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n) 4542f786681SBrian Somers { 4552f786681SBrian Somers struct bundle *bundle = descriptor2bundle(d); 4562f786681SBrian Somers struct datalink *dl; 45726af0ae9SBrian Somers int result, nlinks; 4586c1d6731SBrian Somers u_short ifqueue; 45926af0ae9SBrian Somers size_t queued; 4602f786681SBrian Somers 4612f786681SBrian Somers result = 0; 4622f786681SBrian Somers 463078c562eSBrian Somers /* If there are aren't many packets queued, look for some more. */ 46404eaa58cSBrian Somers for (nlinks = 0, dl = bundle->links; dl; dl = dl->next) 46504eaa58cSBrian Somers nlinks++; 46604eaa58cSBrian Somers 46704eaa58cSBrian Somers if (nlinks) { 46830949fd4SBrian Somers queued = r ? ncp_FillPhysicalQueues(&bundle->ncp, bundle) : 46930949fd4SBrian Somers ncp_QueueLen(&bundle->ncp); 47004eaa58cSBrian Somers 471ff0f9439SBrian Somers if (r && (bundle->phase == PHASE_NETWORK || 472ff0f9439SBrian Somers bundle->phys_type.all & PHYS_AUTO)) { 47304eaa58cSBrian Somers /* enough surplus so that we can tell if we're getting swamped */ 4746c1d6731SBrian Somers ifqueue = nlinks > bundle->cfg.ifqueue ? nlinks : bundle->cfg.ifqueue; 4756c1d6731SBrian Somers if (queued < ifqueue) { 47604eaa58cSBrian Somers /* Not enough - select() for more */ 4776f8e9f0aSBrian Somers if (bundle->choked.timer.state == TIMER_RUNNING) 4786f8e9f0aSBrian Somers timer_Stop(&bundle->choked.timer); /* Not needed any more */ 47904eaa58cSBrian Somers FD_SET(bundle->dev.fd, r); 480faefde08SBrian Somers if (*n < bundle->dev.fd + 1) 481faefde08SBrian Somers *n = bundle->dev.fd + 1; 4821384bd27SBrian Somers log_Printf(LogTIMER, "%s: fdset(r) %d\n", TUN_NAME, bundle->dev.fd); 483078c562eSBrian Somers result++; 4846f8e9f0aSBrian Somers } else if (bundle->choked.timer.state == TIMER_STOPPED) { 4856f8e9f0aSBrian Somers bundle->choked.timer.func = bundle_ClearQueues; 4866f8e9f0aSBrian Somers bundle->choked.timer.name = "output choke"; 4876f8e9f0aSBrian Somers bundle->choked.timer.load = bundle->cfg.choked.timeout * SECTICKS; 4886f8e9f0aSBrian Somers bundle->choked.timer.arg = bundle; 4896f8e9f0aSBrian Somers timer_Start(&bundle->choked.timer); 490078c562eSBrian Somers } 49104eaa58cSBrian Somers } 49204eaa58cSBrian Somers } 493078c562eSBrian Somers 494f0cdd9c0SBrian Somers #ifndef NORADIUS 495f0cdd9c0SBrian Somers result += descriptor_UpdateSet(&bundle->radius.desc, r, w, e, n); 496f0cdd9c0SBrian Somers #endif 497f0cdd9c0SBrian Somers 49871555108SBrian Somers /* Which links need a select() ? */ 49971555108SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 50071555108SBrian Somers result += descriptor_UpdateSet(&dl->desc, r, w, e, n); 50171555108SBrian Somers 502ea722969SBrian Somers /* 503ea722969SBrian Somers * This *MUST* be called after the datalink UpdateSet()s as it 50404eaa58cSBrian Somers * might be ``holding'' one of the datalinks (death-row) and 5058e7bd08eSBrian Somers * wants to be able to de-select() it from the descriptor set. 506ea722969SBrian Somers */ 5070f2f3eb3SBrian Somers result += descriptor_UpdateSet(&bundle->ncp.mp.server.desc, r, w, e, n); 508ea722969SBrian Somers 5092f786681SBrian Somers return result; 5102f786681SBrian Somers } 5112f786681SBrian Somers 5122f786681SBrian Somers static int 513f013f33eSBrian Somers bundle_IsSet(struct fdescriptor *d, const fd_set *fdset) 5142f786681SBrian Somers { 5152f786681SBrian Somers struct bundle *bundle = descriptor2bundle(d); 5162f786681SBrian Somers struct datalink *dl; 5172f786681SBrian Somers 5182f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 5192f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 5202f786681SBrian Somers return 1; 5212f786681SBrian Somers 522f0cdd9c0SBrian Somers #ifndef NORADIUS 523f0cdd9c0SBrian Somers if (descriptor_IsSet(&bundle->radius.desc, fdset)) 524f0cdd9c0SBrian Somers return 1; 525f0cdd9c0SBrian Somers #endif 526f0cdd9c0SBrian Somers 527332b9de0SBrian Somers if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 528332b9de0SBrian Somers return 1; 529332b9de0SBrian Somers 530faefde08SBrian Somers return FD_ISSET(bundle->dev.fd, fdset); 5312f786681SBrian Somers } 5322f786681SBrian Somers 5332f786681SBrian Somers static void 534057f1760SBrian Somers bundle_DescriptorRead(struct fdescriptor *d __unused, struct bundle *bundle, 5352f786681SBrian Somers const fd_set *fdset) 5362f786681SBrian Somers { 5372f786681SBrian Somers struct datalink *dl; 5380a4b6c5cSBrian Somers unsigned secs; 53930949fd4SBrian Somers u_int32_t af; 5402f786681SBrian Somers 541ea722969SBrian Somers if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 542ea722969SBrian Somers descriptor_Read(&bundle->ncp.mp.server.desc, bundle, fdset); 543ea722969SBrian Somers 5442f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 5452f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 5462f786681SBrian Somers descriptor_Read(&dl->desc, bundle, fdset); 547b6217683SBrian Somers 548f0cdd9c0SBrian Somers #ifndef NORADIUS 549f0cdd9c0SBrian Somers if (descriptor_IsSet(&bundle->radius.desc, fdset)) 550f0cdd9c0SBrian Somers descriptor_Read(&bundle->radius.desc, bundle, fdset); 551f0cdd9c0SBrian Somers #endif 552f0cdd9c0SBrian Somers 553faefde08SBrian Somers if (FD_ISSET(bundle->dev.fd, fdset)) { 554078c562eSBrian Somers struct tun_data tun; 555078c562eSBrian Somers int n, pri; 556c1d57c38SBrian Somers u_char *data; 5573a7b6d76SBrian Somers size_t sz; 5583a7b6d76SBrian Somers 5593a7b6d76SBrian Somers if (bundle->dev.header) { 560c1d57c38SBrian Somers data = (u_char *)&tun; 5613a7b6d76SBrian Somers sz = sizeof tun; 5623a7b6d76SBrian Somers } else { 5633a7b6d76SBrian Somers data = tun.data; 5643a7b6d76SBrian Somers sz = sizeof tun.data; 5653a7b6d76SBrian Somers } 566078c562eSBrian Somers 567078c562eSBrian Somers /* something to read from tun */ 5683a7b6d76SBrian Somers 5693a7b6d76SBrian Somers n = read(bundle->dev.fd, data, sz); 570078c562eSBrian Somers if (n < 0) { 5713a7b6d76SBrian Somers log_Printf(LogWARN, "%s: read: %s\n", bundle->dev.Name, strerror(errno)); 572078c562eSBrian Somers return; 573078c562eSBrian Somers } 5743a7b6d76SBrian Somers 5753a7b6d76SBrian Somers if (bundle->dev.header) { 5763a7b6d76SBrian Somers n -= sz - sizeof tun.data; 577078c562eSBrian Somers if (n <= 0) { 5783a7b6d76SBrian Somers log_Printf(LogERROR, "%s: read: Got only %d bytes of data !\n", 5793a7b6d76SBrian Somers bundle->dev.Name, n); 580078c562eSBrian Somers return; 581078c562eSBrian Somers } 58230949fd4SBrian Somers af = ntohl(tun.header.family); 58330949fd4SBrian Somers #ifndef NOINET6 5841136c6acSBrian Somers if (af != AF_INET && af != AF_INET6) 58530949fd4SBrian Somers #else 58630949fd4SBrian Somers if (af != AF_INET) 58730949fd4SBrian Somers #endif 5883a7b6d76SBrian Somers /* XXX: Should be maintaining drop/family counts ! */ 589078c562eSBrian Somers return; 59030949fd4SBrian Somers } else 59130949fd4SBrian Somers af = AF_INET; 592078c562eSBrian Somers 59330949fd4SBrian Somers if (af == AF_INET && ((struct ip *)tun.data)->ip_dst.s_addr == 594078c562eSBrian Somers bundle->ncp.ipcp.my_ip.s_addr) { 595078c562eSBrian Somers /* we've been asked to send something addressed *to* us :( */ 596078c562eSBrian Somers if (Enabled(bundle, OPT_LOOPBACK)) { 59730949fd4SBrian Somers pri = PacketCheck(bundle, af, tun.data, n, &bundle->filter.in, 59830949fd4SBrian Somers NULL, NULL); 599078c562eSBrian Somers if (pri >= 0) { 6003a7b6d76SBrian Somers n += sz - sizeof tun.data; 6013a7b6d76SBrian Somers write(bundle->dev.fd, data, n); 602078c562eSBrian Somers log_Printf(LogDEBUG, "Looped back packet addressed to myself\n"); 603078c562eSBrian Somers } 604078c562eSBrian Somers return; 605078c562eSBrian Somers } else 606078c562eSBrian Somers log_Printf(LogDEBUG, "Oops - forwarding packet addressed to myself\n"); 607078c562eSBrian Somers } 608078c562eSBrian Somers 609078c562eSBrian Somers /* 61030949fd4SBrian Somers * Process on-demand dialup. Output packets are queued within the tunnel 61130949fd4SBrian Somers * device until the appropriate NCP is opened. 612078c562eSBrian Somers */ 613078c562eSBrian Somers 614078c562eSBrian Somers if (bundle_Phase(bundle) == PHASE_DEAD) { 615078c562eSBrian Somers /* 616078c562eSBrian Somers * Note, we must be in AUTO mode :-/ otherwise our interface should 617078c562eSBrian Somers * *not* be UP and we can't receive data 618078c562eSBrian Somers */ 61930949fd4SBrian Somers pri = PacketCheck(bundle, af, tun.data, n, &bundle->filter.dial, 62030949fd4SBrian Somers NULL, NULL); 621040cfe28SBrian Somers if (pri >= 0) 622ba23f397SBrian Somers bundle_Open(bundle, NULL, PHYS_AUTO, 0); 623078c562eSBrian Somers else 624078c562eSBrian Somers /* 625078c562eSBrian Somers * Drop the packet. If we were to queue it, we'd just end up with 626078c562eSBrian Somers * a pile of timed-out data in our output queue by the time we get 627078c562eSBrian Somers * around to actually dialing. We'd also prematurely reach the 628078c562eSBrian Somers * threshold at which we stop select()ing to read() the tun 629078c562eSBrian Somers * device - breaking auto-dial. 630078c562eSBrian Somers */ 631078c562eSBrian Somers return; 632078c562eSBrian Somers } 633078c562eSBrian Somers 6340a4b6c5cSBrian Somers secs = 0; 63530949fd4SBrian Somers pri = PacketCheck(bundle, af, tun.data, n, &bundle->filter.out, 63630949fd4SBrian Somers NULL, &secs); 6370a4b6c5cSBrian Somers if (pri >= 0) { 6380a4b6c5cSBrian Somers /* Prepend the number of seconds timeout given in the filter */ 6390a4b6c5cSBrian Somers tun.header.timeout = secs; 64030949fd4SBrian Somers ncp_Enqueue(&bundle->ncp, af, pri, (char *)&tun, n + sizeof tun.header); 6410a4b6c5cSBrian Somers } 642078c562eSBrian Somers } 643078c562eSBrian Somers } 6442f786681SBrian Somers 6451af29a6eSBrian Somers static int 646057f1760SBrian Somers bundle_DescriptorWrite(struct fdescriptor *d __unused, struct bundle *bundle, 6472f786681SBrian Somers const fd_set *fdset) 6482f786681SBrian Somers { 6492f786681SBrian Somers struct datalink *dl; 6501af29a6eSBrian Somers int result = 0; 6512f786681SBrian Somers 652ea722969SBrian Somers /* This is not actually necessary as struct mpserver doesn't Write() */ 653ea722969SBrian Somers if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 654fb11a9c2SBrian Somers if (descriptor_Write(&bundle->ncp.mp.server.desc, bundle, fdset) == 1) 655fb11a9c2SBrian Somers result++; 656ea722969SBrian Somers 6572f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 6582f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 659fb11a9c2SBrian Somers switch (descriptor_Write(&dl->desc, bundle, fdset)) { 660fb11a9c2SBrian Somers case -1: 661fb11a9c2SBrian Somers datalink_ComeDown(dl, CLOSE_NORMAL); 662fb11a9c2SBrian Somers break; 663fb11a9c2SBrian Somers case 1: 664fb11a9c2SBrian Somers result++; 665fb11a9c2SBrian Somers } 6661af29a6eSBrian Somers 6671af29a6eSBrian Somers return result; 6682f786681SBrian Somers } 6692f786681SBrian Somers 670da66dd13SBrian Somers void 6711384bd27SBrian Somers bundle_LockTun(struct bundle *bundle) 6721384bd27SBrian Somers { 6731384bd27SBrian Somers FILE *lockfile; 67452847614SBrian Somers char pidfile[PATH_MAX]; 6751384bd27SBrian Somers 6761384bd27SBrian Somers snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit); 6771384bd27SBrian Somers lockfile = ID0fopen(pidfile, "w"); 6781384bd27SBrian Somers if (lockfile != NULL) { 6791384bd27SBrian Somers fprintf(lockfile, "%d\n", (int)getpid()); 6801384bd27SBrian Somers fclose(lockfile); 6811384bd27SBrian Somers } 6821384bd27SBrian Somers #ifndef RELEASE_CRUNCH 6831384bd27SBrian Somers else 6841384bd27SBrian Somers log_Printf(LogERROR, "Warning: Can't create %s: %s\n", 6851384bd27SBrian Somers pidfile, strerror(errno)); 6861384bd27SBrian Somers #endif 6871384bd27SBrian Somers } 6881384bd27SBrian Somers 6891384bd27SBrian Somers static void 6901384bd27SBrian Somers bundle_UnlockTun(struct bundle *bundle) 6911384bd27SBrian Somers { 69252847614SBrian Somers char pidfile[PATH_MAX]; 6931384bd27SBrian Somers 6941384bd27SBrian Somers snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit); 6951384bd27SBrian Somers ID0unlink(pidfile); 6961384bd27SBrian Somers } 6977a6f8720SBrian Somers 6987a6f8720SBrian Somers struct bundle * 699cf0a3940SBrian Somers bundle_Create(const char *prefix, int type, int unit) 7007a6f8720SBrian Somers { 7017a6f8720SBrian Somers static struct bundle bundle; /* there can be only one */ 702c0593e34SBrian Somers int enoentcount, err, minunit, maxunit; 7034e5196e9SBrian Somers const char *ifname; 704fdb4bb1bSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD) 705fc3034caSBrian Somers int kldtried; 706fc3034caSBrian Somers #endif 7073a7b6d76SBrian Somers #if defined(TUNSIFMODE) || defined(TUNSLMODE) || defined(TUNSIFHEAD) 7080f203c7eSBrian Somers int iff; 7090f203c7eSBrian Somers #endif 7107a6f8720SBrian Somers 7118fa6ebe4SBrian Somers if (bundle.iface != NULL) { /* Already allocated ! */ 712a33b2ef7SBrian Somers log_Printf(LogALERT, "bundle_Create: There's only one BUNDLE !\n"); 7137a6f8720SBrian Somers return NULL; 7147a6f8720SBrian Somers } 7157a6f8720SBrian Somers 716c0593e34SBrian Somers if (unit == -1) { 717c0593e34SBrian Somers minunit = 0; 718c0593e34SBrian Somers maxunit = -1; 719c0593e34SBrian Somers } else { 720c0593e34SBrian Somers minunit = unit; 721c0593e34SBrian Somers maxunit = unit + 1; 722c0593e34SBrian Somers } 7237a6f8720SBrian Somers err = ENOENT; 7247a6f8720SBrian Somers enoentcount = 0; 725fdb4bb1bSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD) 726fc3034caSBrian Somers kldtried = 0; 727fc3034caSBrian Somers #endif 728c0593e34SBrian Somers for (bundle.unit = minunit; bundle.unit != maxunit; bundle.unit++) { 729faefde08SBrian Somers snprintf(bundle.dev.Name, sizeof bundle.dev.Name, "%s%d", 730faefde08SBrian Somers prefix, bundle.unit); 731faefde08SBrian Somers bundle.dev.fd = ID0open(bundle.dev.Name, O_RDWR); 732faefde08SBrian Somers if (bundle.dev.fd >= 0) 7337a6f8720SBrian Somers break; 734728ef5b2SBrian Somers else if (errno == ENXIO || errno == ENOENT) { 735fdb4bb1bSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD) 736c0593e34SBrian Somers if (bundle.unit == minunit && !kldtried++) { 737fc3034caSBrian Somers /* 738fdb4bb1bSBrian Somers * Attempt to load the tunnel interface KLD if it isn't loaded 739fdb4bb1bSBrian Somers * already. 740fc3034caSBrian Somers */ 7415476d2e5SBrian Somers if (loadmodules(LOAD_VERBOSLY, "if_tun", NULL)) 7425476d2e5SBrian Somers bundle.unit--; 743fc3034caSBrian Somers continue; 744fc3034caSBrian Somers } 745fc3034caSBrian Somers #endif 746c4c6616aSBrian Somers if (errno != ENOENT || ++enoentcount > 2) { 7477a6f8720SBrian Somers err = errno; 748107d62e7SBrian Somers break; 749c4c6616aSBrian Somers } 7507a6f8720SBrian Somers } else 7517a6f8720SBrian Somers err = errno; 7527a6f8720SBrian Somers } 7537a6f8720SBrian Somers 754faefde08SBrian Somers if (bundle.dev.fd < 0) { 755c0593e34SBrian Somers if (unit == -1) 756c0593e34SBrian Somers log_Printf(LogWARN, "No available tunnel devices found (%s)\n", 75785b542cfSBrian Somers strerror(err)); 758c0593e34SBrian Somers else 759c0593e34SBrian Somers log_Printf(LogWARN, "%s%d: %s\n", prefix, unit, strerror(err)); 7607a6f8720SBrian Somers return NULL; 7617a6f8720SBrian Somers } 7627a6f8720SBrian Somers 763f2f5156fSBrian Somers log_SetTun(bundle.unit, NULL); 7647a6f8720SBrian Somers 7658fa6ebe4SBrian Somers ifname = strrchr(bundle.dev.Name, '/'); 7668fa6ebe4SBrian Somers if (ifname == NULL) 7678fa6ebe4SBrian Somers ifname = bundle.dev.Name; 7687a6f8720SBrian Somers else 7698fa6ebe4SBrian Somers ifname++; 7708fa6ebe4SBrian Somers 7718fa6ebe4SBrian Somers bundle.iface = iface_Create(ifname); 7728fa6ebe4SBrian Somers if (bundle.iface == NULL) { 7738fa6ebe4SBrian Somers close(bundle.dev.fd); 7748fa6ebe4SBrian Somers return NULL; 7758fa6ebe4SBrian Somers } 7767a6f8720SBrian Somers 7770f203c7eSBrian Somers #ifdef TUNSIFMODE 778ebdcbc67SBrian Somers /* Make sure we're POINTOPOINT & IFF_MULTICAST */ 779ebdcbc67SBrian Somers iff = IFF_POINTOPOINT | IFF_MULTICAST; 7800f203c7eSBrian Somers if (ID0ioctl(bundle.dev.fd, TUNSIFMODE, &iff) < 0) 7810f203c7eSBrian Somers log_Printf(LogERROR, "bundle_Create: ioctl(TUNSIFMODE): %s\n", 7820f203c7eSBrian Somers strerror(errno)); 7830f203c7eSBrian Somers #endif 7840f203c7eSBrian Somers 7856ca65df0SBrian Somers #ifdef TUNSLMODE 7863a7b6d76SBrian Somers /* Make sure we're not prepending sockaddrs */ 7876ca65df0SBrian Somers iff = 0; 7886ca65df0SBrian Somers if (ID0ioctl(bundle.dev.fd, TUNSLMODE, &iff) < 0) 7896ca65df0SBrian Somers log_Printf(LogERROR, "bundle_Create: ioctl(TUNSLMODE): %s\n", 7906ca65df0SBrian Somers strerror(errno)); 7916ca65df0SBrian Somers #endif 7926ca65df0SBrian Somers 7933a7b6d76SBrian Somers #ifdef TUNSIFHEAD 7943a7b6d76SBrian Somers /* We want the address family please ! */ 7953a7b6d76SBrian Somers iff = 1; 7963a7b6d76SBrian Somers if (ID0ioctl(bundle.dev.fd, TUNSIFHEAD, &iff) < 0) { 7973a7b6d76SBrian Somers log_Printf(LogERROR, "bundle_Create: ioctl(TUNSIFHEAD): %s\n", 7983a7b6d76SBrian Somers strerror(errno)); 7993a7b6d76SBrian Somers bundle.dev.header = 0; 8003a7b6d76SBrian Somers } else 8013a7b6d76SBrian Somers bundle.dev.header = 1; 8023a7b6d76SBrian Somers #else 8033a7b6d76SBrian Somers #ifdef __OpenBSD__ 8043a7b6d76SBrian Somers /* Always present for OpenBSD */ 8053a7b6d76SBrian Somers bundle.dev.header = 1; 8063a7b6d76SBrian Somers #else 8073a7b6d76SBrian Somers /* 8083a7b6d76SBrian Somers * If TUNSIFHEAD isn't available and we're not OpenBSD, assume 8093a7b6d76SBrian Somers * everything's AF_INET (hopefully the tun device won't pass us 8103a7b6d76SBrian Somers * anything else !). 8113a7b6d76SBrian Somers */ 8123a7b6d76SBrian Somers bundle.dev.header = 0; 8133a7b6d76SBrian Somers #endif 8143a7b6d76SBrian Somers #endif 8153a7b6d76SBrian Somers 8168fa6ebe4SBrian Somers log_Printf(LogPHASE, "Using interface: %s\n", ifname); 8177a6f8720SBrian Somers 818ab2de065SBrian Somers bundle.bandwidth = 0; 819820de6ebSBrian Somers bundle.routing_seq = 0; 820a0cbd833SBrian Somers bundle.phase = PHASE_DEAD; 821a0cbd833SBrian Somers bundle.CleaningUp = 0; 82267b072f7SBrian Somers bundle.NatEnabled = 0; 8237a6f8720SBrian Somers 8246d666775SBrian Somers bundle.fsm.LayerStart = bundle_LayerStart; 8256f384573SBrian Somers bundle.fsm.LayerUp = bundle_LayerUp; 8266d666775SBrian Somers bundle.fsm.LayerDown = bundle_LayerDown; 8276d666775SBrian Somers bundle.fsm.LayerFinish = bundle_LayerFinish; 8286d666775SBrian Somers bundle.fsm.object = &bundle; 8297a6f8720SBrian Somers 830dade2407SBrian Somers bundle.cfg.idle.timeout = NCP_IDLE_TIMEOUT; 831dade2407SBrian Somers bundle.cfg.idle.min_timeout = 0; 8321342caedSBrian Somers *bundle.cfg.auth.name = '\0'; 8331342caedSBrian Somers *bundle.cfg.auth.key = '\0'; 8340508c09aSBrian Somers bundle.cfg.optmask = (1ull << OPT_IDCHECK) | (1ull << OPT_LOOPBACK) | 8350508c09aSBrian Somers (1ull << OPT_SROUTES) | (1ull << OPT_TCPMSSFIXUP) | 8360508c09aSBrian Somers (1ull << OPT_THROUGHPUT) | (1ull << OPT_UTMP) | 8370508c09aSBrian Somers (1ull << OPT_NAS_IP_ADDRESS) | 8380508c09aSBrian Somers (1ull << OPT_NAS_IDENTIFIER); 839971abb29SBrian Somers #ifndef NOINET6 8400508c09aSBrian Somers opt_enable(&bundle, OPT_IPCP); 841971abb29SBrian Somers if (probe.ipv6_available) 8420508c09aSBrian Somers opt_enable(&bundle, OPT_IPV6CP); 843971abb29SBrian Somers #endif 84449052c95SBrian Somers *bundle.cfg.label = '\0'; 8456c1d6731SBrian Somers bundle.cfg.ifqueue = DEF_IFQUEUE; 8466f8e9f0aSBrian Somers bundle.cfg.choked.timeout = CHOKED_TIMEOUT; 847ff0f9439SBrian Somers bundle.phys_type.all = type; 848ff0f9439SBrian Somers bundle.phys_type.open = 0; 849dade2407SBrian Somers bundle.upat = 0; 850ab886ad0SBrian Somers 8516f384573SBrian Somers bundle.links = datalink_Create("deflink", &bundle, type); 8523006ec67SBrian Somers if (bundle.links == NULL) { 853a33b2ef7SBrian Somers log_Printf(LogALERT, "Cannot create data link: %s\n", strerror(errno)); 854f2f5156fSBrian Somers iface_Free(bundle.iface); 8558fa6ebe4SBrian Somers bundle.iface = NULL; 856faefde08SBrian Somers close(bundle.dev.fd); 8572289f246SBrian Somers return NULL; 8582289f246SBrian Somers } 8592289f246SBrian Somers 8602f786681SBrian Somers bundle.desc.type = BUNDLE_DESCRIPTOR; 8612f786681SBrian Somers bundle.desc.UpdateSet = bundle_UpdateSet; 8622f786681SBrian Somers bundle.desc.IsSet = bundle_IsSet; 8632f786681SBrian Somers bundle.desc.Read = bundle_DescriptorRead; 8642f786681SBrian Somers bundle.desc.Write = bundle_DescriptorWrite; 8652f786681SBrian Somers 86630949fd4SBrian Somers ncp_Init(&bundle.ncp, &bundle); 8676d666775SBrian Somers 8685ca5389aSBrian Somers memset(&bundle.filter, '\0', sizeof bundle.filter); 8695ca5389aSBrian Somers bundle.filter.in.fragok = bundle.filter.in.logok = 1; 8705ca5389aSBrian Somers bundle.filter.in.name = "IN"; 8715ca5389aSBrian Somers bundle.filter.out.fragok = bundle.filter.out.logok = 1; 8725ca5389aSBrian Somers bundle.filter.out.name = "OUT"; 8735ca5389aSBrian Somers bundle.filter.dial.name = "DIAL"; 8748390b576SBrian Somers bundle.filter.dial.logok = 1; 8755ca5389aSBrian Somers bundle.filter.alive.name = "ALIVE"; 8765ca5389aSBrian Somers bundle.filter.alive.logok = 1; 877cad7e742SBrian Somers { 878cad7e742SBrian Somers int i; 879cad7e742SBrian Somers for (i = 0; i < MAXFILTERS; i++) { 880cad7e742SBrian Somers bundle.filter.in.rule[i].f_action = A_NONE; 881cad7e742SBrian Somers bundle.filter.out.rule[i].f_action = A_NONE; 882cad7e742SBrian Somers bundle.filter.dial.rule[i].f_action = A_NONE; 883cad7e742SBrian Somers bundle.filter.alive.rule[i].f_action = A_NONE; 884cad7e742SBrian Somers } 885cad7e742SBrian Somers } 88693ee0ff2SBrian Somers memset(&bundle.idle.timer, '\0', sizeof bundle.idle.timer); 88793ee0ff2SBrian Somers bundle.idle.done = 0; 8885cf4388bSBrian Somers bundle.notify.fd = -1; 8896f8e9f0aSBrian Somers memset(&bundle.choked.timer, '\0', sizeof bundle.choked.timer); 890972a1bcfSBrian Somers #ifndef NORADIUS 891972a1bcfSBrian Somers radius_Init(&bundle.radius); 892972a1bcfSBrian Somers #endif 89393ee0ff2SBrian Somers 8946d666775SBrian Somers /* Clean out any leftover crud */ 89530949fd4SBrian Somers iface_Clear(bundle.iface, &bundle.ncp, 0, IFACE_CLEAR_ALL); 8966d666775SBrian Somers 8971384bd27SBrian Somers bundle_LockTun(&bundle); 8981384bd27SBrian Somers 8997a6f8720SBrian Somers return &bundle; 9007a6f8720SBrian Somers } 9017a6f8720SBrian Somers 90268a0f0ccSBrian Somers static void 90368a0f0ccSBrian Somers bundle_DownInterface(struct bundle *bundle) 90468a0f0ccSBrian Somers { 905dd7e2610SBrian Somers route_IfDelete(bundle, 1); 906dc744e19SBrian Somers iface_ClearFlags(bundle->iface->name, IFF_UP); 90768a0f0ccSBrian Somers } 90868a0f0ccSBrian Somers 90968a0f0ccSBrian Somers void 91068a0f0ccSBrian Somers bundle_Destroy(struct bundle *bundle) 91168a0f0ccSBrian Somers { 9123006ec67SBrian Somers struct datalink *dl; 913b6217683SBrian Somers 914ea722969SBrian Somers /* 91530949fd4SBrian Somers * Clean up the interface. We don't really need to do the timer_Stop()s, 91630949fd4SBrian Somers * mp_Down(), iface_Clear() and bundle_DownInterface() unless we're getting 9178e7bd08eSBrian Somers * out under exceptional conditions such as a descriptor exception. 918ea722969SBrian Somers */ 91904eaa58cSBrian Somers timer_Stop(&bundle->idle.timer); 9206f8e9f0aSBrian Somers timer_Stop(&bundle->choked.timer); 92166f634b6SBrian Somers mp_Down(&bundle->ncp.mp); 92230949fd4SBrian Somers iface_Clear(bundle->iface, &bundle->ncp, 0, IFACE_CLEAR_ALL); 92368a0f0ccSBrian Somers bundle_DownInterface(bundle); 9243006ec67SBrian Somers 925972a1bcfSBrian Somers #ifndef NORADIUS 926972a1bcfSBrian Somers /* Tell the radius server the bad news */ 927972a1bcfSBrian Somers radius_Destroy(&bundle->radius); 928972a1bcfSBrian Somers #endif 929972a1bcfSBrian Somers 930ea722969SBrian Somers /* Again, these are all DATALINK_CLOSED unless we're abending */ 9313006ec67SBrian Somers dl = bundle->links; 9323006ec67SBrian Somers while (dl) 9333006ec67SBrian Somers dl = datalink_Destroy(dl); 9343006ec67SBrian Somers 93530949fd4SBrian Somers ncp_Destroy(&bundle->ncp); 936442f8495SBrian Somers 9371384bd27SBrian Somers close(bundle->dev.fd); 9381384bd27SBrian Somers bundle_UnlockTun(bundle); 9391384bd27SBrian Somers 940ea722969SBrian Somers /* In case we never made PHASE_NETWORK */ 9415cf4388bSBrian Somers bundle_Notify(bundle, EX_ERRDEAD); 942b6217683SBrian Somers 9438fa6ebe4SBrian Somers iface_Destroy(bundle->iface); 9448fa6ebe4SBrian Somers bundle->iface = NULL; 94568a0f0ccSBrian Somers } 94668a0f0ccSBrian Somers 94783d1af55SBrian Somers void 9483006ec67SBrian Somers bundle_LinkClosed(struct bundle *bundle, struct datalink *dl) 9493006ec67SBrian Somers { 9503006ec67SBrian Somers /* 9513006ec67SBrian Somers * Our datalink has closed. 952ea722969SBrian Somers * CleanDatalinks() (called from DoLoop()) will remove closed 953f6a4e748SBrian Somers * BACKGROUND, FOREGROUND and DIRECT links. 9543b0f8d2eSBrian Somers * If it's the last data link, enter phase DEAD. 955ea722969SBrian Somers * 956ea722969SBrian Somers * NOTE: dl may not be in our list (bundle_SendDatalink()) ! 9573006ec67SBrian Somers */ 9585b8b8060SBrian Somers 9593b0f8d2eSBrian Somers struct datalink *odl; 9603b0f8d2eSBrian Somers int other_links; 9615b8b8060SBrian Somers 962bf1d3ff6SBrian Somers log_SetTtyCommandMode(dl); 963bf1d3ff6SBrian Somers 9643b0f8d2eSBrian Somers other_links = 0; 9653b0f8d2eSBrian Somers for (odl = bundle->links; odl; odl = odl->next) 9663b0f8d2eSBrian Somers if (odl != dl && odl->state != DATALINK_CLOSED) 9673b0f8d2eSBrian Somers other_links++; 9683b0f8d2eSBrian Somers 9693b0f8d2eSBrian Somers if (!other_links) { 97081358fa3SBrian Somers if (dl->physical->type != PHYS_AUTO) /* Not in -auto mode */ 97103704096SBrian Somers bundle_DownInterface(bundle); 97230949fd4SBrian Somers ncp2initial(&bundle->ncp); 973356bf92dSBrian Somers mp_Down(&bundle->ncp.mp); 9745563ebdeSBrian Somers bundle_NewPhase(bundle, PHASE_DEAD); 975bf1eaec5SBrian Somers #ifndef NORADIUS 976bf1eaec5SBrian Somers if (bundle->radius.sessiontime) 977bf1eaec5SBrian Somers bundle_StopSessionTimer(bundle); 978bf1eaec5SBrian Somers #endif 9790f2f3eb3SBrian Somers bundle_StopIdleTimer(bundle); 980ab2de065SBrian Somers } 981455aabc3SBrian Somers } 982455aabc3SBrian Somers 983455aabc3SBrian Somers void 984ba23f397SBrian Somers bundle_Open(struct bundle *bundle, const char *name, int mask, int force) 9853006ec67SBrian Somers { 9863006ec67SBrian Somers /* 9873006ec67SBrian Somers * Please open the given datalink, or all if name == NULL 9883006ec67SBrian Somers */ 9893006ec67SBrian Somers struct datalink *dl; 9903006ec67SBrian Somers 9913006ec67SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 9923006ec67SBrian Somers if (name == NULL || !strcasecmp(dl->name, name)) { 993ba23f397SBrian Somers if ((mask & dl->physical->type) && 994ba23f397SBrian Somers (dl->state == DATALINK_CLOSED || 995ba23f397SBrian Somers (force && dl->state == DATALINK_OPENING && 9965e269efeSBrian Somers dl->dial.timer.state == TIMER_RUNNING) || 9975e269efeSBrian Somers dl->state == DATALINK_READY)) { 9985e269efeSBrian Somers timer_Stop(&dl->dial.timer); /* We're finished with this */ 999565e35e5SBrian Somers datalink_Up(dl, 1, 1); 1000ab2de065SBrian Somers if (mask & PHYS_AUTO) 10015e269efeSBrian Somers break; /* Only one AUTO link at a time */ 100204eaa58cSBrian Somers } 10033006ec67SBrian Somers if (name != NULL) 10043006ec67SBrian Somers break; 10053006ec67SBrian Somers } 10063006ec67SBrian Somers } 10073006ec67SBrian Somers 10083006ec67SBrian Somers struct datalink * 10093006ec67SBrian Somers bundle2datalink(struct bundle *bundle, const char *name) 10103006ec67SBrian Somers { 10113006ec67SBrian Somers struct datalink *dl; 10123006ec67SBrian Somers 10133006ec67SBrian Somers if (name != NULL) { 10143006ec67SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 10153006ec67SBrian Somers if (!strcasecmp(dl->name, name)) 10163006ec67SBrian Somers return dl; 10173006ec67SBrian Somers } else if (bundle->links && !bundle->links->next) 10183006ec67SBrian Somers return bundle->links; 10193006ec67SBrian Somers 10203006ec67SBrian Somers return NULL; 10213006ec67SBrian Somers } 10223006ec67SBrian Somers 10233006ec67SBrian Somers int 1024aef795ccSBrian Somers bundle_ShowLinks(struct cmdargs const *arg) 1025aef795ccSBrian Somers { 1026aef795ccSBrian Somers struct datalink *dl; 1027ab2de065SBrian Somers struct pppThroughput *t; 102891cbd2eeSBrian Somers unsigned long long octets; 1029ab2de065SBrian Somers int secs; 1030aef795ccSBrian Somers 10319c53a7b1SBrian Somers for (dl = arg->bundle->links; dl; dl = dl->next) { 103291cbd2eeSBrian Somers octets = MAX(dl->physical->link.stats.total.in.OctetsPerSecond, 103391cbd2eeSBrian Somers dl->physical->link.stats.total.out.OctetsPerSecond); 103491cbd2eeSBrian Somers 1035d4156d00SBrian Somers prompt_Printf(arg->prompt, "Name: %s [%s, %s]", 1036d4156d00SBrian Somers dl->name, mode2Nam(dl->physical->type), datalink_State(dl)); 103711572abfSBrian Somers if (dl->physical->link.stats.total.rolling && dl->state == DATALINK_OPEN) 1038e531f89aSBrian Somers prompt_Printf(arg->prompt, " bandwidth %d, %llu bps (%llu bytes/sec)", 1039ab2de065SBrian Somers dl->mp.bandwidth ? dl->mp.bandwidth : 1040ab2de065SBrian Somers physical_GetSpeed(dl->physical), 104191cbd2eeSBrian Somers octets * 8, octets); 10429c53a7b1SBrian Somers prompt_Printf(arg->prompt, "\n"); 10439c53a7b1SBrian Somers } 1044aef795ccSBrian Somers 104511572abfSBrian Somers t = &arg->bundle->ncp.mp.link.stats.total; 104691cbd2eeSBrian Somers octets = MAX(t->in.OctetsPerSecond, t->out.OctetsPerSecond); 1047ab2de065SBrian Somers secs = t->downtime ? 0 : throughput_uptime(t); 1048ab2de065SBrian Somers if (secs > t->SamplePeriod) 1049ab2de065SBrian Somers secs = t->SamplePeriod; 1050ab2de065SBrian Somers if (secs) 1051e531f89aSBrian Somers prompt_Printf(arg->prompt, "Currently averaging %llu bps (%llu bytes/sec)" 105291cbd2eeSBrian Somers " over the last %d secs\n", octets * 8, octets, secs); 1053ab2de065SBrian Somers 1054aef795ccSBrian Somers return 0; 1055aef795ccSBrian Somers } 1056ab886ad0SBrian Somers 10571342caedSBrian Somers static const char * 10580508c09aSBrian Somers optval(struct bundle *bundle, int opt) 10591342caedSBrian Somers { 10600508c09aSBrian Somers return Enabled(bundle, opt) ? "enabled" : "disabled"; 10611342caedSBrian Somers } 10621342caedSBrian Somers 1063c08717dfSBrian Somers int 1064c08717dfSBrian Somers bundle_ShowStatus(struct cmdargs const *arg) 1065c08717dfSBrian Somers { 1066c08717dfSBrian Somers int remaining; 1067c08717dfSBrian Somers 1068c08717dfSBrian Somers prompt_Printf(arg->prompt, "Phase %s\n", bundle_PhaseName(arg->bundle)); 1069faefde08SBrian Somers prompt_Printf(arg->prompt, " Device: %s\n", arg->bundle->dev.Name); 1070dade2407SBrian Somers prompt_Printf(arg->prompt, " Interface: %s @ %lubps", 1071ab2de065SBrian Somers arg->bundle->iface->name, arg->bundle->bandwidth); 1072c08717dfSBrian Somers 1073dade2407SBrian Somers if (arg->bundle->upat) { 107446df5aa7SBrian Somers int secs = bundle_Uptime(arg->bundle); 1075dade2407SBrian Somers 1076dade2407SBrian Somers prompt_Printf(arg->prompt, ", up time %d:%02d:%02d", secs / 3600, 1077dade2407SBrian Somers (secs / 60) % 60, secs % 60); 1078dade2407SBrian Somers } 1079669b9965SBrian Somers prompt_Printf(arg->prompt, "\n Queued: %lu of %u\n", 108030949fd4SBrian Somers (unsigned long)ncp_QueueLen(&arg->bundle->ncp), 108177fc031dSBrian Somers arg->bundle->cfg.ifqueue); 1082dade2407SBrian Somers 10836c1d6731SBrian Somers prompt_Printf(arg->prompt, "\nDefaults:\n"); 108474457d3dSBrian Somers prompt_Printf(arg->prompt, " Label: %s\n", 108574457d3dSBrian Somers arg->bundle->cfg.label); 1086610b185fSBrian Somers prompt_Printf(arg->prompt, " Auth name: %s\n", 1087610b185fSBrian Somers arg->bundle->cfg.auth.name); 108874457d3dSBrian Somers prompt_Printf(arg->prompt, " Diagnostic socket: "); 108937b8a5c7SBrian Somers if (*server.cfg.sockname != '\0') { 109037b8a5c7SBrian Somers prompt_Printf(arg->prompt, "%s", server.cfg.sockname); 109137b8a5c7SBrian Somers if (server.cfg.mask != (mode_t)-1) 109237b8a5c7SBrian Somers prompt_Printf(arg->prompt, ", mask 0%03o", (int)server.cfg.mask); 109337b8a5c7SBrian Somers prompt_Printf(arg->prompt, "%s\n", server.fd == -1 ? " (not open)" : ""); 109437b8a5c7SBrian Somers } else if (server.cfg.port != 0) 109574457d3dSBrian Somers prompt_Printf(arg->prompt, "TCP port %d%s\n", server.cfg.port, 109674457d3dSBrian Somers server.fd == -1 ? " (not open)" : ""); 109774457d3dSBrian Somers else 109874457d3dSBrian Somers prompt_Printf(arg->prompt, "none\n"); 109904eaa58cSBrian Somers 1100057f1760SBrian Somers prompt_Printf(arg->prompt, " Choked Timer: %us\n", 11016f8e9f0aSBrian Somers arg->bundle->cfg.choked.timeout); 1102972a1bcfSBrian Somers 1103972a1bcfSBrian Somers #ifndef NORADIUS 1104972a1bcfSBrian Somers radius_Show(&arg->bundle->radius, arg->prompt); 1105972a1bcfSBrian Somers #endif 1106972a1bcfSBrian Somers 1107c08717dfSBrian Somers prompt_Printf(arg->prompt, " Idle Timer: "); 1108dade2407SBrian Somers if (arg->bundle->cfg.idle.timeout) { 1109057f1760SBrian Somers prompt_Printf(arg->prompt, "%us", arg->bundle->cfg.idle.timeout); 1110dade2407SBrian Somers if (arg->bundle->cfg.idle.min_timeout) 1111057f1760SBrian Somers prompt_Printf(arg->prompt, ", min %us", 1112dade2407SBrian Somers arg->bundle->cfg.idle.min_timeout); 1113c08717dfSBrian Somers remaining = bundle_RemainingIdleTime(arg->bundle); 1114c08717dfSBrian Somers if (remaining != -1) 1115c08717dfSBrian Somers prompt_Printf(arg->prompt, " (%ds remaining)", remaining); 1116c08717dfSBrian Somers prompt_Printf(arg->prompt, "\n"); 1117c08717dfSBrian Somers } else 1118c08717dfSBrian Somers prompt_Printf(arg->prompt, "disabled\n"); 1119c08717dfSBrian Somers 112030949fd4SBrian Somers prompt_Printf(arg->prompt, " Filter Decap: %-20.20s", 112198251667SBrian Somers optval(arg->bundle, OPT_FILTERDECAP)); 112230949fd4SBrian Somers prompt_Printf(arg->prompt, " ID check: %s\n", 11231342caedSBrian Somers optval(arg->bundle, OPT_IDCHECK)); 112430949fd4SBrian Somers prompt_Printf(arg->prompt, " Iface-Alias: %-20.20s", 112530949fd4SBrian Somers optval(arg->bundle, OPT_IFACEALIAS)); 112630949fd4SBrian Somers #ifndef NOINET6 112730949fd4SBrian Somers prompt_Printf(arg->prompt, " IPCP: %s\n", 112830949fd4SBrian Somers optval(arg->bundle, OPT_IPCP)); 112930949fd4SBrian Somers prompt_Printf(arg->prompt, " IPV6CP: %-20.20s", 113030949fd4SBrian Somers optval(arg->bundle, OPT_IPV6CP)); 113130949fd4SBrian Somers #endif 113298251667SBrian Somers prompt_Printf(arg->prompt, " Keep-Session: %s\n", 1133ac685e31SBrian Somers optval(arg->bundle, OPT_KEEPSESSION)); 113498251667SBrian Somers prompt_Printf(arg->prompt, " Loopback: %-20.20s", 11351342caedSBrian Somers optval(arg->bundle, OPT_LOOPBACK)); 113698251667SBrian Somers prompt_Printf(arg->prompt, " PasswdAuth: %s\n", 11371342caedSBrian Somers optval(arg->bundle, OPT_PASSWDAUTH)); 113898251667SBrian Somers prompt_Printf(arg->prompt, " Proxy: %-20.20s", 11391342caedSBrian Somers optval(arg->bundle, OPT_PROXY)); 114098251667SBrian Somers prompt_Printf(arg->prompt, " Proxyall: %s\n", 11413afe5ccbSBrian Somers optval(arg->bundle, OPT_PROXYALL)); 114230949fd4SBrian Somers prompt_Printf(arg->prompt, " Sticky Routes: %-20.20s", 114330949fd4SBrian Somers optval(arg->bundle, OPT_SROUTES)); 114430949fd4SBrian Somers prompt_Printf(arg->prompt, " TCPMSS Fixup: %s\n", 114594d7be52SBrian Somers optval(arg->bundle, OPT_TCPMSSFIXUP)); 114630949fd4SBrian Somers prompt_Printf(arg->prompt, " Throughput: %-20.20s", 11471342caedSBrian Somers optval(arg->bundle, OPT_THROUGHPUT)); 114830949fd4SBrian Somers prompt_Printf(arg->prompt, " Utmp Logging: %s\n", 11491342caedSBrian Somers optval(arg->bundle, OPT_UTMP)); 11500508c09aSBrian Somers prompt_Printf(arg->prompt, " NAS-IP-Address: %-20.20s", 11510508c09aSBrian Somers optval(arg->bundle, OPT_NAS_IP_ADDRESS)); 11520508c09aSBrian Somers prompt_Printf(arg->prompt, " NAS-Identifier: %s\n", 11530508c09aSBrian Somers optval(arg->bundle, OPT_NAS_IDENTIFIER)); 11541342caedSBrian Somers 1155c08717dfSBrian Somers return 0; 1156c08717dfSBrian Somers } 1157c08717dfSBrian Somers 1158ab886ad0SBrian Somers static void 1159ab886ad0SBrian Somers bundle_IdleTimeout(void *v) 1160ab886ad0SBrian Somers { 1161ab886ad0SBrian Somers struct bundle *bundle = (struct bundle *)v; 1162ab886ad0SBrian Somers 1163b42135deSBrian Somers log_Printf(LogPHASE, "Idle timer expired\n"); 116404eaa58cSBrian Somers bundle_StopIdleTimer(bundle); 11659c81b87dSBrian Somers bundle_Close(bundle, NULL, CLOSE_STAYDOWN); 1166ab886ad0SBrian Somers } 1167ab886ad0SBrian Somers 1168ab886ad0SBrian Somers /* 1169ab886ad0SBrian Somers * Start Idle timer. If timeout is reached, we call bundle_Close() to 1170ab886ad0SBrian Somers * close LCP and link. 1171ab886ad0SBrian Somers */ 1172ab886ad0SBrian Somers void 11730a4b6c5cSBrian Somers bundle_StartIdleTimer(struct bundle *bundle, unsigned secs) 1174ab886ad0SBrian Somers { 1175dd7e2610SBrian Somers timer_Stop(&bundle->idle.timer); 1176ff0f9439SBrian Somers if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) != 1177dade2407SBrian Somers bundle->phys_type.open && bundle->cfg.idle.timeout) { 11780a4b6c5cSBrian Somers time_t now = time(NULL); 1179dade2407SBrian Somers 11800a4b6c5cSBrian Somers if (secs == 0) 1181dade2407SBrian Somers secs = bundle->cfg.idle.timeout; 11820a4b6c5cSBrian Somers 11830a4b6c5cSBrian Somers /* We want at least `secs' */ 1184dade2407SBrian Somers if (bundle->cfg.idle.min_timeout > secs && bundle->upat) { 1185057f1760SBrian Somers unsigned up = now - bundle->upat; 1186dade2407SBrian Somers 1187057f1760SBrian Somers if (bundle->cfg.idle.min_timeout > up && 1188057f1760SBrian Somers bundle->cfg.idle.min_timeout - up > (long long)secs) 11890a4b6c5cSBrian Somers /* Only increase from the current `remaining' value */ 1190dade2407SBrian Somers secs = bundle->cfg.idle.min_timeout - up; 1191dade2407SBrian Somers } 119293ee0ff2SBrian Somers bundle->idle.timer.func = bundle_IdleTimeout; 11933b0f8d2eSBrian Somers bundle->idle.timer.name = "idle"; 1194dade2407SBrian Somers bundle->idle.timer.load = secs * SECTICKS; 119593ee0ff2SBrian Somers bundle->idle.timer.arg = bundle; 1196dd7e2610SBrian Somers timer_Start(&bundle->idle.timer); 11970a4b6c5cSBrian Somers bundle->idle.done = now + secs; 1198ab886ad0SBrian Somers } 1199ab886ad0SBrian Somers } 1200ab886ad0SBrian Somers 1201ab886ad0SBrian Somers void 1202057f1760SBrian Somers bundle_SetIdleTimer(struct bundle *bundle, unsigned timeout, 1203057f1760SBrian Somers unsigned min_timeout) 1204ab886ad0SBrian Somers { 1205dade2407SBrian Somers bundle->cfg.idle.timeout = timeout; 1206dade2407SBrian Somers bundle->cfg.idle.min_timeout = min_timeout; 120730949fd4SBrian Somers if (ncp_LayersOpen(&bundle->ncp)) 12080a4b6c5cSBrian Somers bundle_StartIdleTimer(bundle, 0); 1209ab886ad0SBrian Somers } 1210ab886ad0SBrian Somers 1211ab886ad0SBrian Somers void 1212ab886ad0SBrian Somers bundle_StopIdleTimer(struct bundle *bundle) 1213ab886ad0SBrian Somers { 1214dd7e2610SBrian Somers timer_Stop(&bundle->idle.timer); 12154a632c80SBrian Somers bundle->idle.done = 0; 1216ab886ad0SBrian Somers } 1217ab886ad0SBrian Somers 121804eaa58cSBrian Somers static int 1219ab886ad0SBrian Somers bundle_RemainingIdleTime(struct bundle *bundle) 1220ab886ad0SBrian Somers { 122193ee0ff2SBrian Somers if (bundle->idle.done) 122293ee0ff2SBrian Somers return bundle->idle.done - time(NULL); 1223ab886ad0SBrian Somers return -1; 1224ab886ad0SBrian Somers } 12253b0f8d2eSBrian Somers 1226bf1eaec5SBrian Somers #ifndef NORADIUS 1227bf1eaec5SBrian Somers 1228bf1eaec5SBrian Somers static void 1229bf1eaec5SBrian Somers bundle_SessionTimeout(void *v) 1230bf1eaec5SBrian Somers { 1231bf1eaec5SBrian Somers struct bundle *bundle = (struct bundle *)v; 1232bf1eaec5SBrian Somers 1233bf1eaec5SBrian Somers log_Printf(LogPHASE, "Session-Timeout timer expired\n"); 1234bf1eaec5SBrian Somers bundle_StopSessionTimer(bundle); 1235bf1eaec5SBrian Somers bundle_Close(bundle, NULL, CLOSE_STAYDOWN); 1236bf1eaec5SBrian Somers } 1237bf1eaec5SBrian Somers 1238bf1eaec5SBrian Somers void 1239bf1eaec5SBrian Somers bundle_StartSessionTimer(struct bundle *bundle, unsigned secs) 1240bf1eaec5SBrian Somers { 1241bf1eaec5SBrian Somers timer_Stop(&bundle->session.timer); 1242bf1eaec5SBrian Somers if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) != 1243bf1eaec5SBrian Somers bundle->phys_type.open && bundle->radius.sessiontime) { 1244bf1eaec5SBrian Somers time_t now = time(NULL); 1245bf1eaec5SBrian Somers 1246bf1eaec5SBrian Somers if (secs == 0) 1247bf1eaec5SBrian Somers secs = bundle->radius.sessiontime; 1248bf1eaec5SBrian Somers 1249bf1eaec5SBrian Somers bundle->session.timer.func = bundle_SessionTimeout; 1250bf1eaec5SBrian Somers bundle->session.timer.name = "session"; 1251bf1eaec5SBrian Somers bundle->session.timer.load = secs * SECTICKS; 1252bf1eaec5SBrian Somers bundle->session.timer.arg = bundle; 1253bf1eaec5SBrian Somers timer_Start(&bundle->session.timer); 1254bf1eaec5SBrian Somers bundle->session.done = now + secs; 1255bf1eaec5SBrian Somers } 1256bf1eaec5SBrian Somers } 1257bf1eaec5SBrian Somers 1258bf1eaec5SBrian Somers void 1259bf1eaec5SBrian Somers bundle_StopSessionTimer(struct bundle *bundle) 1260bf1eaec5SBrian Somers { 1261bf1eaec5SBrian Somers timer_Stop(&bundle->session.timer); 1262bf1eaec5SBrian Somers bundle->session.done = 0; 1263bf1eaec5SBrian Somers } 1264bf1eaec5SBrian Somers 1265bf1eaec5SBrian Somers #endif 1266bf1eaec5SBrian Somers 12673b0f8d2eSBrian Somers int 12683b0f8d2eSBrian Somers bundle_IsDead(struct bundle *bundle) 12693b0f8d2eSBrian Somers { 12703b0f8d2eSBrian Somers return !bundle->links || (bundle->phase == PHASE_DEAD && bundle->CleaningUp); 12713b0f8d2eSBrian Somers } 1272b6217683SBrian Somers 127304eaa58cSBrian Somers static struct datalink * 127404eaa58cSBrian Somers bundle_DatalinkLinkout(struct bundle *bundle, struct datalink *dl) 1275cd7bd93aSBrian Somers { 1276cd7bd93aSBrian Somers struct datalink **dlp; 1277cd7bd93aSBrian Somers 1278cd7bd93aSBrian Somers for (dlp = &bundle->links; *dlp; dlp = &(*dlp)->next) 1279cd7bd93aSBrian Somers if (*dlp == dl) { 128004eaa58cSBrian Somers *dlp = dl->next; 128104eaa58cSBrian Somers dl->next = NULL; 128204eaa58cSBrian Somers bundle_LinksRemoved(bundle); 128304eaa58cSBrian Somers return dl; 1284cd7bd93aSBrian Somers } 128504eaa58cSBrian Somers 128604eaa58cSBrian Somers return NULL; 128704eaa58cSBrian Somers } 128804eaa58cSBrian Somers 128904eaa58cSBrian Somers static void 129004eaa58cSBrian Somers bundle_DatalinkLinkin(struct bundle *bundle, struct datalink *dl) 129104eaa58cSBrian Somers { 129204eaa58cSBrian Somers struct datalink **dlp = &bundle->links; 129304eaa58cSBrian Somers 129404eaa58cSBrian Somers while (*dlp) 129504eaa58cSBrian Somers dlp = &(*dlp)->next; 129604eaa58cSBrian Somers 129704eaa58cSBrian Somers *dlp = dl; 129804eaa58cSBrian Somers dl->next = NULL; 129904eaa58cSBrian Somers 130004eaa58cSBrian Somers bundle_LinkAdded(bundle, dl); 1301ab2de065SBrian Somers mp_CheckAutoloadTimer(&bundle->ncp.mp); 1302565e35e5SBrian Somers } 1303565e35e5SBrian Somers 1304565e35e5SBrian Somers void 1305565e35e5SBrian Somers bundle_CleanDatalinks(struct bundle *bundle) 1306565e35e5SBrian Somers { 1307565e35e5SBrian Somers struct datalink **dlp = &bundle->links; 130804eaa58cSBrian Somers int found = 0; 1309565e35e5SBrian Somers 1310565e35e5SBrian Somers while (*dlp) 1311565e35e5SBrian Somers if ((*dlp)->state == DATALINK_CLOSED && 1312f6a4e748SBrian Somers (*dlp)->physical->type & 1313f6a4e748SBrian Somers (PHYS_DIRECT|PHYS_BACKGROUND|PHYS_FOREGROUND)) { 1314565e35e5SBrian Somers *dlp = datalink_Destroy(*dlp); 131504eaa58cSBrian Somers found++; 131604eaa58cSBrian Somers } else 1317565e35e5SBrian Somers dlp = &(*dlp)->next; 131804eaa58cSBrian Somers 131904eaa58cSBrian Somers if (found) 132004eaa58cSBrian Somers bundle_LinksRemoved(bundle); 132104eaa58cSBrian Somers } 132204eaa58cSBrian Somers 132304eaa58cSBrian Somers int 132404eaa58cSBrian Somers bundle_DatalinkClone(struct bundle *bundle, struct datalink *dl, 132504eaa58cSBrian Somers const char *name) 132604eaa58cSBrian Somers { 132704eaa58cSBrian Somers if (bundle2datalink(bundle, name)) { 132804eaa58cSBrian Somers log_Printf(LogWARN, "Clone: %s: name already exists\n", name); 132904eaa58cSBrian Somers return 0; 133004eaa58cSBrian Somers } 133104eaa58cSBrian Somers 133204eaa58cSBrian Somers bundle_DatalinkLinkin(bundle, datalink_Clone(dl, name)); 133304eaa58cSBrian Somers return 1; 133404eaa58cSBrian Somers } 133504eaa58cSBrian Somers 133604eaa58cSBrian Somers void 133704eaa58cSBrian Somers bundle_DatalinkRemove(struct bundle *bundle, struct datalink *dl) 133804eaa58cSBrian Somers { 133904eaa58cSBrian Somers dl = bundle_DatalinkLinkout(bundle, dl); 134004eaa58cSBrian Somers if (dl) 134104eaa58cSBrian Somers datalink_Destroy(dl); 1342cd7bd93aSBrian Somers } 134349052c95SBrian Somers 134449052c95SBrian Somers void 134549052c95SBrian Somers bundle_SetLabel(struct bundle *bundle, const char *label) 134649052c95SBrian Somers { 134749052c95SBrian Somers if (label) 134849052c95SBrian Somers strncpy(bundle->cfg.label, label, sizeof bundle->cfg.label - 1); 134949052c95SBrian Somers else 135049052c95SBrian Somers *bundle->cfg.label = '\0'; 135149052c95SBrian Somers } 135249052c95SBrian Somers 135349052c95SBrian Somers const char * 135449052c95SBrian Somers bundle_GetLabel(struct bundle *bundle) 135549052c95SBrian Somers { 135649052c95SBrian Somers return *bundle->cfg.label ? bundle->cfg.label : NULL; 135749052c95SBrian Somers } 13581fa665f5SBrian Somers 13592cb305afSBrian Somers int 13602cb305afSBrian Somers bundle_LinkSize() 13611fa665f5SBrian Somers { 136296c9bb21SBrian Somers struct iovec iov[SCATTER_SEGMENTS]; 13632cb305afSBrian Somers int niov, expect, f; 136487c3786eSBrian Somers 136596c9bb21SBrian Somers iov[0].iov_len = strlen(Version) + 1; 13662cb305afSBrian Somers iov[0].iov_base = NULL; 13672cb305afSBrian Somers niov = 1; 13682cb305afSBrian Somers if (datalink2iov(NULL, iov, &niov, SCATTER_SEGMENTS, NULL, NULL) == -1) { 13692cb305afSBrian Somers log_Printf(LogERROR, "Cannot determine space required for link\n"); 13702cb305afSBrian Somers return 0; 137154cd8e13SBrian Somers } 13726f384573SBrian Somers 137396c9bb21SBrian Somers for (f = expect = 0; f < niov; f++) 137496c9bb21SBrian Somers expect += iov[f].iov_len; 137596c9bb21SBrian Somers 13762cb305afSBrian Somers return expect; 137787c3786eSBrian Somers } 137896c9bb21SBrian Somers 13792cb305afSBrian Somers void 13802cb305afSBrian Somers bundle_ReceiveDatalink(struct bundle *bundle, int s) 13812cb305afSBrian Somers { 13822cb305afSBrian Somers char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int) * SEND_MAXFD]; 1383057f1760SBrian Somers int niov, expect, f, *fd, nfd, onfd; 1384057f1760SBrian Somers ssize_t got; 13852cb305afSBrian Somers struct iovec iov[SCATTER_SEGMENTS]; 13862cb305afSBrian Somers struct cmsghdr *cmsg; 13872cb305afSBrian Somers struct msghdr msg; 13882cb305afSBrian Somers struct datalink *dl; 13892cb305afSBrian Somers pid_t pid; 13902cb305afSBrian Somers 13912cb305afSBrian Somers log_Printf(LogPHASE, "Receiving datalink\n"); 13922cb305afSBrian Somers 13932cb305afSBrian Somers /* 13942cb305afSBrian Somers * Create our scatter/gather array - passing NULL gets the space 13952cb305afSBrian Somers * allocation requirement rather than actually flattening the 13962cb305afSBrian Somers * structures. 13972cb305afSBrian Somers */ 13982cb305afSBrian Somers iov[0].iov_len = strlen(Version) + 1; 13992cb305afSBrian Somers iov[0].iov_base = NULL; 14002cb305afSBrian Somers niov = 1; 14012cb305afSBrian Somers if (datalink2iov(NULL, iov, &niov, SCATTER_SEGMENTS, NULL, NULL) == -1) { 14022cb305afSBrian Somers log_Printf(LogERROR, "Cannot determine space required for link\n"); 14032cb305afSBrian Somers return; 14042cb305afSBrian Somers } 14052cb305afSBrian Somers 14062cb305afSBrian Somers /* Allocate the scatter/gather array for recvmsg() */ 14072cb305afSBrian Somers for (f = expect = 0; f < niov; f++) { 14082cb305afSBrian Somers if ((iov[f].iov_base = malloc(iov[f].iov_len)) == NULL) { 14092cb305afSBrian Somers log_Printf(LogERROR, "Cannot allocate space to receive link\n"); 14102cb305afSBrian Somers return; 14112cb305afSBrian Somers } 1412cbee9754SBrian Somers if (f) 14132cb305afSBrian Somers expect += iov[f].iov_len; 14142cb305afSBrian Somers } 14152cb305afSBrian Somers 14162cb305afSBrian Somers /* Set up our message */ 14172cb305afSBrian Somers cmsg = (struct cmsghdr *)cmsgbuf; 14182cb305afSBrian Somers cmsg->cmsg_len = sizeof cmsgbuf; 14192cb305afSBrian Somers cmsg->cmsg_level = SOL_SOCKET; 14202cb305afSBrian Somers cmsg->cmsg_type = 0; 14212cb305afSBrian Somers 142296c9bb21SBrian Somers memset(&msg, '\0', sizeof msg); 14232cb305afSBrian Somers msg.msg_name = NULL; 14242cb305afSBrian Somers msg.msg_namelen = 0; 142596c9bb21SBrian Somers msg.msg_iov = iov; 1426cbee9754SBrian Somers msg.msg_iovlen = 1; /* Only send the version at the first pass */ 142796c9bb21SBrian Somers msg.msg_control = cmsgbuf; 142896c9bb21SBrian Somers msg.msg_controllen = sizeof cmsgbuf; 142996c9bb21SBrian Somers 1430209dc102SBrian Somers log_Printf(LogDEBUG, "Expecting %u scatter/gather bytes\n", 1431209dc102SBrian Somers (unsigned)iov[0].iov_len); 14322cb305afSBrian Somers 1433057f1760SBrian Somers if ((got = recvmsg(s, &msg, MSG_WAITALL)) != (ssize_t)iov[0].iov_len) { 1434cbee9754SBrian Somers if (got == -1) 143596c9bb21SBrian Somers log_Printf(LogERROR, "Failed recvmsg: %s\n", strerror(errno)); 143696c9bb21SBrian Somers else 14371814213eSMarcel Moolenaar log_Printf(LogERROR, "Failed recvmsg: Got %zd, not %u\n", 1438209dc102SBrian Somers got, (unsigned)iov[0].iov_len); 143996c9bb21SBrian Somers while (niov--) 144096c9bb21SBrian Somers free(iov[niov].iov_base); 144196c9bb21SBrian Somers return; 144296c9bb21SBrian Somers } 144396c9bb21SBrian Somers 14442cb305afSBrian Somers if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) { 14452cb305afSBrian Somers log_Printf(LogERROR, "Recvmsg: no descriptors received !\n"); 14462cb305afSBrian Somers while (niov--) 14472cb305afSBrian Somers free(iov[niov].iov_base); 14482cb305afSBrian Somers return; 144987c3786eSBrian Somers } 145087c3786eSBrian Somers 14512bc21ed9SDavid Malone fd = (int *)CMSG_DATA(cmsg); 14522bc21ed9SDavid Malone nfd = ((caddr_t)cmsg + cmsg->cmsg_len - (caddr_t)fd) / sizeof(int); 14532cb305afSBrian Somers 14542cb305afSBrian Somers if (nfd < 2) { 14558e7bd08eSBrian Somers log_Printf(LogERROR, "Recvmsg: %d descriptor%s received (too few) !\n", 14562cb305afSBrian Somers nfd, nfd == 1 ? "" : "s"); 14572cb305afSBrian Somers while (nfd--) 14582cb305afSBrian Somers close(fd[nfd]); 1459ad5b0e8bSBrian Somers while (niov--) 1460ad5b0e8bSBrian Somers free(iov[niov].iov_base); 1461ad5b0e8bSBrian Somers return; 146254cd8e13SBrian Somers } 146354cd8e13SBrian Somers 146487c3786eSBrian Somers /* 1465cbee9754SBrian Somers * We've successfully received two or more open file descriptors 1466cbee9754SBrian Somers * through our socket, plus a version string. Make sure it's the 1467cbee9754SBrian Somers * correct version, and drop the connection if it's not. 146887c3786eSBrian Somers */ 146996c9bb21SBrian Somers if (strncmp(Version, iov[0].iov_base, iov[0].iov_len)) { 147096c9bb21SBrian Somers log_Printf(LogWARN, "Cannot receive datalink, incorrect version" 147196c9bb21SBrian Somers " (\"%.*s\", not \"%s\")\n", (int)iov[0].iov_len, 14727d81ddf5SBrian Somers (char *)iov[0].iov_base, Version); 14732cb305afSBrian Somers while (nfd--) 14742cb305afSBrian Somers close(fd[nfd]); 147596c9bb21SBrian Somers while (niov--) 147696c9bb21SBrian Somers free(iov[niov].iov_base); 147796c9bb21SBrian Somers return; 147896c9bb21SBrian Somers } 147996c9bb21SBrian Somers 1480cbee9754SBrian Somers /* 1481cbee9754SBrian Somers * Everything looks good. Send the other side our process id so that 1482cbee9754SBrian Somers * they can transfer lock ownership, and wait for them to send the 1483cbee9754SBrian Somers * actual link data. 1484cbee9754SBrian Somers */ 1485cbee9754SBrian Somers pid = getpid(); 1486cbee9754SBrian Somers if ((got = write(fd[1], &pid, sizeof pid)) != sizeof pid) { 1487cbee9754SBrian Somers if (got == -1) 1488cbee9754SBrian Somers log_Printf(LogERROR, "Failed write: %s\n", strerror(errno)); 1489cbee9754SBrian Somers else 14901814213eSMarcel Moolenaar log_Printf(LogERROR, "Failed write: Got %zd, not %d\n", got, 1491cbee9754SBrian Somers (int)(sizeof pid)); 1492cbee9754SBrian Somers while (nfd--) 1493cbee9754SBrian Somers close(fd[nfd]); 1494cbee9754SBrian Somers while (niov--) 1495cbee9754SBrian Somers free(iov[niov].iov_base); 1496cbee9754SBrian Somers return; 1497cbee9754SBrian Somers } 1498cbee9754SBrian Somers 1499cbee9754SBrian Somers if ((got = readv(fd[1], iov + 1, niov - 1)) != expect) { 1500cbee9754SBrian Somers if (got == -1) 1501cbee9754SBrian Somers log_Printf(LogERROR, "Failed write: %s\n", strerror(errno)); 1502cbee9754SBrian Somers else 15031814213eSMarcel Moolenaar log_Printf(LogERROR, "Failed write: Got %zd, not %d\n", got, expect); 1504cbee9754SBrian Somers while (nfd--) 1505cbee9754SBrian Somers close(fd[nfd]); 1506cbee9754SBrian Somers while (niov--) 1507cbee9754SBrian Somers free(iov[niov].iov_base); 1508cbee9754SBrian Somers return; 1509cbee9754SBrian Somers } 1510cbee9754SBrian Somers close(fd[1]); 1511cbee9754SBrian Somers 15122cb305afSBrian Somers onfd = nfd; /* We've got this many in our array */ 15138e7bd08eSBrian Somers nfd -= 2; /* Don't include p->fd and our reply descriptor */ 15142cb305afSBrian Somers niov = 1; /* Skip the version id */ 151587c3786eSBrian Somers dl = iov2datalink(bundle, iov, &niov, sizeof iov / sizeof *iov, fd[0], 15162cb305afSBrian Somers fd + 2, &nfd); 1517b7c5748eSBrian Somers if (dl) { 15182cb305afSBrian Somers 151987c3786eSBrian Somers if (nfd) { 152087c3786eSBrian Somers log_Printf(LogERROR, "bundle_ReceiveDatalink: Failed to handle %d " 15212cb305afSBrian Somers "auxiliary file descriptors (%d remain)\n", onfd, nfd); 152287c3786eSBrian Somers datalink_Destroy(dl); 152387c3786eSBrian Somers while (nfd--) 152487c3786eSBrian Somers close(fd[onfd--]); 15252cb305afSBrian Somers close(fd[0]); 152687c3786eSBrian Somers } else { 152704eaa58cSBrian Somers bundle_DatalinkLinkin(bundle, dl); 1528b7c5748eSBrian Somers datalink_AuthOk(dl); 1529ab2de065SBrian Somers bundle_CalculateBandwidth(dl->bundle); 153087c3786eSBrian Somers } 153187c3786eSBrian Somers } else { 153287c3786eSBrian Somers while (nfd--) 153387c3786eSBrian Somers close(fd[onfd--]); 153487c3786eSBrian Somers close(fd[0]); 15352cb305afSBrian Somers close(fd[1]); 153687c3786eSBrian Somers } 153796c9bb21SBrian Somers 153896c9bb21SBrian Somers free(iov[0].iov_base); 15391fa665f5SBrian Somers } 15401fa665f5SBrian Somers 15411fa665f5SBrian Somers void 154296c9bb21SBrian Somers bundle_SendDatalink(struct datalink *dl, int s, struct sockaddr_un *sun) 15431fa665f5SBrian Somers { 15442bc21ed9SDavid Malone char cmsgbuf[CMSG_SPACE(sizeof(int) * SEND_MAXFD)]; 15452cb305afSBrian Somers const char *constlock; 15462cb305afSBrian Somers char *lock; 154787c3786eSBrian Somers struct cmsghdr *cmsg; 154896c9bb21SBrian Somers struct msghdr msg; 154996c9bb21SBrian Somers struct iovec iov[SCATTER_SEGMENTS]; 1550057f1760SBrian Somers int niov, f, expect, newsid, fd[SEND_MAXFD], nfd, reply[2]; 1551057f1760SBrian Somers ssize_t got; 155285fd273aSBrian Somers pid_t newpid; 15536f384573SBrian Somers 1554dd7e2610SBrian Somers log_Printf(LogPHASE, "Transmitting datalink %s\n", dl->name); 15556f384573SBrian Somers 15562cb305afSBrian Somers /* Record the base device name for a lock transfer later */ 15572cb305afSBrian Somers constlock = physical_LockedDevice(dl->physical); 15582cb305afSBrian Somers if (constlock) { 15592cb305afSBrian Somers lock = alloca(strlen(constlock) + 1); 15602cb305afSBrian Somers strcpy(lock, constlock); 15612cb305afSBrian Somers } else 15622cb305afSBrian Somers lock = NULL; 15632cb305afSBrian Somers 156404eaa58cSBrian Somers bundle_LinkClosed(dl->bundle, dl); 15650f2f3eb3SBrian Somers bundle_DatalinkLinkout(dl->bundle, dl); 1566ea722969SBrian Somers 156796c9bb21SBrian Somers /* Build our scatter/gather array */ 156896c9bb21SBrian Somers iov[0].iov_len = strlen(Version) + 1; 156996c9bb21SBrian Somers iov[0].iov_base = strdup(Version); 157096c9bb21SBrian Somers niov = 1; 157187c3786eSBrian Somers nfd = 0; 15726f384573SBrian Somers 15732cb305afSBrian Somers fd[0] = datalink2iov(dl, iov, &niov, SCATTER_SEGMENTS, fd + 2, &nfd); 15746f384573SBrian Somers 15752cb305afSBrian Somers if (fd[0] != -1 && socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, reply) != -1) { 15762cb305afSBrian Somers /* 15772cb305afSBrian Somers * fd[1] is used to get the peer process id back, then to confirm that 15782cb305afSBrian Somers * we've transferred any device locks to that process id. 15792cb305afSBrian Somers */ 15802cb305afSBrian Somers fd[1] = reply[1]; 158187c3786eSBrian Somers 15822cb305afSBrian Somers nfd += 2; /* Include fd[0] and fd[1] */ 158396c9bb21SBrian Somers memset(&msg, '\0', sizeof msg); 158454cd8e13SBrian Somers 15852cb305afSBrian Somers msg.msg_name = NULL; 15862cb305afSBrian Somers msg.msg_namelen = 0; 1587cbee9754SBrian Somers /* 1588cbee9754SBrian Somers * Only send the version to start... We used to send the whole lot, but 1589cbee9754SBrian Somers * this caused problems with our RECVBUF size as a single link is about 1590cbee9754SBrian Somers * 22k ! This way, we should bump into no limits. 1591cbee9754SBrian Somers */ 1592cbee9754SBrian Somers msg.msg_iovlen = 1; 159396c9bb21SBrian Somers msg.msg_iov = iov; 15942cb305afSBrian Somers msg.msg_control = cmsgbuf; 15952bc21ed9SDavid Malone msg.msg_controllen = CMSG_SPACE(sizeof(int) * nfd); 15962cb305afSBrian Somers msg.msg_flags = 0; 159754cd8e13SBrian Somers 15982cb305afSBrian Somers cmsg = (struct cmsghdr *)cmsgbuf; 15992cb305afSBrian Somers cmsg->cmsg_len = msg.msg_controllen; 160054cd8e13SBrian Somers cmsg->cmsg_level = SOL_SOCKET; 160154cd8e13SBrian Somers cmsg->cmsg_type = SCM_RIGHTS; 160287c3786eSBrian Somers 16032cb305afSBrian Somers for (f = 0; f < nfd; f++) 16042bc21ed9SDavid Malone *((int *)CMSG_DATA(cmsg) + f) = fd[f]; 160547723d29SBrian Somers 1606cbee9754SBrian Somers for (f = 1, expect = 0; f < niov; f++) 160796c9bb21SBrian Somers expect += iov[f].iov_len; 160847723d29SBrian Somers 1609cbee9754SBrian Somers if (setsockopt(reply[0], SOL_SOCKET, SO_SNDBUF, &expect, sizeof(int)) == -1) 1610cbee9754SBrian Somers log_Printf(LogERROR, "setsockopt(SO_RCVBUF, %d): %s\n", expect, 1611cbee9754SBrian Somers strerror(errno)); 1612cbee9754SBrian Somers if (setsockopt(reply[1], SOL_SOCKET, SO_RCVBUF, &expect, sizeof(int)) == -1) 1613cbee9754SBrian Somers log_Printf(LogERROR, "setsockopt(SO_RCVBUF, %d): %s\n", expect, 1614cbee9754SBrian Somers strerror(errno)); 1615cbee9754SBrian Somers 1616209dc102SBrian Somers log_Printf(LogDEBUG, "Sending %d descriptor%s and %u bytes in scatter" 1617209dc102SBrian Somers "/gather array\n", nfd, nfd == 1 ? "" : "s", 1618209dc102SBrian Somers (unsigned)iov[0].iov_len); 161947723d29SBrian Somers 16202cb305afSBrian Somers if ((got = sendmsg(s, &msg, 0)) == -1) 16212cb305afSBrian Somers log_Printf(LogERROR, "Failed sendmsg: %s: %s\n", 16222cb305afSBrian Somers sun->sun_path, strerror(errno)); 1623057f1760SBrian Somers else if (got != (ssize_t)iov[0].iov_len) 16241814213eSMarcel Moolenaar log_Printf(LogERROR, "%s: Failed initial sendmsg: Only sent %zd of %u\n", 1625209dc102SBrian Somers sun->sun_path, got, (unsigned)iov[0].iov_len); 16262cb305afSBrian Somers else { 16278e7bd08eSBrian Somers /* We must get the ACK before closing the descriptor ! */ 16282cb305afSBrian Somers int res; 16292cb305afSBrian Somers 1630cbee9754SBrian Somers if ((got = read(reply[0], &newpid, sizeof newpid)) == sizeof newpid) { 163142df3c25SBrian Somers log_Printf(LogDEBUG, "Received confirmation from pid %ld\n", 163242df3c25SBrian Somers (long)newpid); 16332cb305afSBrian Somers if (lock && (res = ID0uu_lock_txfr(lock, newpid)) != UU_LOCK_OK) 1634b42135deSBrian Somers log_Printf(LogERROR, "uu_lock_txfr: %s\n", uu_lockerr(res)); 16352cb305afSBrian Somers 1636cbee9754SBrian Somers log_Printf(LogDEBUG, "Transmitting link (%d bytes)\n", expect); 1637cbee9754SBrian Somers if ((got = writev(reply[0], iov + 1, niov - 1)) != expect) { 1638cbee9754SBrian Somers if (got == -1) 1639cbee9754SBrian Somers log_Printf(LogERROR, "%s: Failed writev: %s\n", 1640cbee9754SBrian Somers sun->sun_path, strerror(errno)); 1641cbee9754SBrian Somers else 16421814213eSMarcel Moolenaar log_Printf(LogERROR, "%s: Failed writev: Wrote %zd of %d\n", 1643cbee9754SBrian Somers sun->sun_path, got, expect); 1644cbee9754SBrian Somers } 1645cbee9754SBrian Somers } else if (got == -1) 1646cbee9754SBrian Somers log_Printf(LogERROR, "%s: Failed socketpair read: %s\n", 1647cbee9754SBrian Somers sun->sun_path, strerror(errno)); 1648cbee9754SBrian Somers else 16491814213eSMarcel Moolenaar log_Printf(LogERROR, "%s: Failed socketpair read: Got %zd of %d\n", 1650cbee9754SBrian Somers sun->sun_path, got, (int)(sizeof newpid)); 16512cb305afSBrian Somers } 16522cb305afSBrian Somers 16532cb305afSBrian Somers close(reply[0]); 16542cb305afSBrian Somers close(reply[1]); 165554cd8e13SBrian Somers 1656ac685e31SBrian Somers newsid = Enabled(dl->bundle, OPT_KEEPSESSION) || 165787c3786eSBrian Somers tcgetpgrp(fd[0]) == getpgrp(); 165887c3786eSBrian Somers while (nfd) 165987c3786eSBrian Somers close(fd[--nfd]); 16601384bd27SBrian Somers if (newsid) 16612cb305afSBrian Somers bundle_setsid(dl->bundle, got != -1); 166247723d29SBrian Somers } 166385fd273aSBrian Somers close(s); 166496c9bb21SBrian Somers 166596c9bb21SBrian Somers while (niov--) 166696c9bb21SBrian Somers free(iov[niov].iov_base); 16671fa665f5SBrian Somers } 1668dd0645c5SBrian Somers 1669dd0645c5SBrian Somers int 167058d55334SBrian Somers bundle_RenameDatalink(struct bundle *bundle, struct datalink *ndl, 167158d55334SBrian Somers const char *name) 167258d55334SBrian Somers { 167358d55334SBrian Somers struct datalink *dl; 167458d55334SBrian Somers 167558d55334SBrian Somers if (!strcasecmp(ndl->name, name)) 167658d55334SBrian Somers return 1; 167758d55334SBrian Somers 167858d55334SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 167958d55334SBrian Somers if (!strcasecmp(dl->name, name)) 168058d55334SBrian Somers return 0; 168158d55334SBrian Somers 168258d55334SBrian Somers datalink_Rename(ndl, name); 168358d55334SBrian Somers return 1; 168458d55334SBrian Somers } 168558d55334SBrian Somers 168658d55334SBrian Somers int 1687dd0645c5SBrian Somers bundle_SetMode(struct bundle *bundle, struct datalink *dl, int mode) 1688dd0645c5SBrian Somers { 1689dd0645c5SBrian Somers int omode; 1690dd0645c5SBrian Somers 1691dd0645c5SBrian Somers omode = dl->physical->type; 1692dd0645c5SBrian Somers if (omode == mode) 1693dd0645c5SBrian Somers return 1; 1694dd0645c5SBrian Somers 1695ff0f9439SBrian Somers if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO)) 1696ff0f9439SBrian Somers /* First auto link */ 1697dd0645c5SBrian Somers if (bundle->ncp.ipcp.peer_ip.s_addr == INADDR_ANY) { 1698ff0f9439SBrian Somers log_Printf(LogWARN, "You must `set ifaddr' or `open' before" 1699ff0f9439SBrian Somers " changing mode to %s\n", mode2Nam(mode)); 1700dd0645c5SBrian Somers return 0; 1701dd0645c5SBrian Somers } 1702dd0645c5SBrian Somers 1703dd0645c5SBrian Somers if (!datalink_SetMode(dl, mode)) 1704dd0645c5SBrian Somers return 0; 1705dd0645c5SBrian Somers 1706ff0f9439SBrian Somers if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO) && 1707ff0f9439SBrian Somers bundle->phase != PHASE_NETWORK) 1708ff0f9439SBrian Somers /* First auto link, we need an interface */ 1709dd0645c5SBrian Somers ipcp_InterfaceUp(&bundle->ncp.ipcp); 1710dd0645c5SBrian Somers 1711ab2de065SBrian Somers /* Regenerate phys_type and adjust idle timer */ 171204eaa58cSBrian Somers bundle_LinksRemoved(bundle); 1713dd0645c5SBrian Somers 1714dd0645c5SBrian Somers return 1; 1715dd0645c5SBrian Somers } 17161384bd27SBrian Somers 17171384bd27SBrian Somers void 17181384bd27SBrian Somers bundle_setsid(struct bundle *bundle, int holdsession) 17191384bd27SBrian Somers { 17201384bd27SBrian Somers /* 17211384bd27SBrian Somers * Lose the current session. This means getting rid of our pid 17221384bd27SBrian Somers * too so that the tty device will really go away, and any getty 17231384bd27SBrian Somers * etc will be allowed to restart. 17241384bd27SBrian Somers */ 17251384bd27SBrian Somers pid_t pid, orig; 17261384bd27SBrian Somers int fds[2]; 17271384bd27SBrian Somers char done; 17281384bd27SBrian Somers struct datalink *dl; 17291384bd27SBrian Somers 1730e62ce959SBrian Somers if (!holdsession && bundle_IsDead(bundle)) { 1731e62ce959SBrian Somers /* 1732e62ce959SBrian Somers * No need to lose our session after all... we're going away anyway 1733e62ce959SBrian Somers * 1734e62ce959SBrian Somers * We should really stop the timer and pause if holdsession is set and 1735e62ce959SBrian Somers * the bundle's dead, but that leaves other resources lying about :-( 1736e62ce959SBrian Somers */ 1737e62ce959SBrian Somers return; 1738e62ce959SBrian Somers } 1739e62ce959SBrian Somers 17401384bd27SBrian Somers orig = getpid(); 17411384bd27SBrian Somers if (pipe(fds) == -1) { 17421384bd27SBrian Somers log_Printf(LogERROR, "pipe: %s\n", strerror(errno)); 17431384bd27SBrian Somers return; 17441384bd27SBrian Somers } 17451384bd27SBrian Somers switch ((pid = fork())) { 17461384bd27SBrian Somers case -1: 17471384bd27SBrian Somers log_Printf(LogERROR, "fork: %s\n", strerror(errno)); 17481384bd27SBrian Somers close(fds[0]); 17491384bd27SBrian Somers close(fds[1]); 17501384bd27SBrian Somers return; 17511384bd27SBrian Somers case 0: 17521384bd27SBrian Somers close(fds[1]); 17534be0e57dSBrian Somers read(fds[0], &done, 1); /* uu_locks are mine ! */ 17544be0e57dSBrian Somers close(fds[0]); 17551384bd27SBrian Somers if (pipe(fds) == -1) { 17561384bd27SBrian Somers log_Printf(LogERROR, "pipe(2): %s\n", strerror(errno)); 17571384bd27SBrian Somers return; 17581384bd27SBrian Somers } 17591384bd27SBrian Somers switch ((pid = fork())) { 17601384bd27SBrian Somers case -1: 1761a33b2ef7SBrian Somers log_Printf(LogERROR, "fork(2): %s\n", strerror(errno)); 17621384bd27SBrian Somers close(fds[0]); 17631384bd27SBrian Somers close(fds[1]); 17641384bd27SBrian Somers return; 17651384bd27SBrian Somers case 0: 17661384bd27SBrian Somers close(fds[1]); 17674be0e57dSBrian Somers bundle_LockTun(bundle); /* update pid */ 17684be0e57dSBrian Somers read(fds[0], &done, 1); /* uu_locks are mine ! */ 17694be0e57dSBrian Somers close(fds[0]); 17701384bd27SBrian Somers setsid(); 177106b47306SBrian Somers bundle_ChangedPID(bundle); 177242df3c25SBrian Somers log_Printf(LogDEBUG, "%ld -> %ld: %s session control\n", 177342df3c25SBrian Somers (long)orig, (long)getpid(), 17741384bd27SBrian Somers holdsession ? "Passed" : "Dropped"); 17757e778f13SBrian Somers timer_InitService(0); /* Start the Timer Service */ 17761384bd27SBrian Somers break; 17771384bd27SBrian Somers default: 17784be0e57dSBrian Somers close(fds[0]); 17795d9e6103SBrian Somers /* Give away all our physical locks (to the final process) */ 17801384bd27SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 17811384bd27SBrian Somers if (dl->state != DATALINK_CLOSED) 17825d9e6103SBrian Somers physical_ChangedPid(dl->physical, pid); 17834be0e57dSBrian Somers write(fds[1], "!", 1); /* done */ 17844be0e57dSBrian Somers close(fds[1]); 17852cb305afSBrian Somers _exit(0); 17861384bd27SBrian Somers break; 17871384bd27SBrian Somers } 17881384bd27SBrian Somers break; 17891384bd27SBrian Somers default: 17904be0e57dSBrian Somers close(fds[0]); 17915d9e6103SBrian Somers /* Give away all our physical locks (to the intermediate process) */ 17921384bd27SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 17931384bd27SBrian Somers if (dl->state != DATALINK_CLOSED) 17945d9e6103SBrian Somers physical_ChangedPid(dl->physical, pid); 17954be0e57dSBrian Somers write(fds[1], "!", 1); /* done */ 17964be0e57dSBrian Somers close(fds[1]); 17971384bd27SBrian Somers if (holdsession) { 17981384bd27SBrian Somers int fd, status; 17991384bd27SBrian Somers 18001384bd27SBrian Somers timer_TermService(); 18011384bd27SBrian Somers signal(SIGPIPE, SIG_DFL); 18021384bd27SBrian Somers signal(SIGALRM, SIG_DFL); 18031384bd27SBrian Somers signal(SIGHUP, SIG_DFL); 18041384bd27SBrian Somers signal(SIGTERM, SIG_DFL); 18051384bd27SBrian Somers signal(SIGINT, SIG_DFL); 18061384bd27SBrian Somers signal(SIGQUIT, SIG_DFL); 18071384bd27SBrian Somers for (fd = getdtablesize(); fd >= 0; fd--) 18081384bd27SBrian Somers close(fd); 18091384bd27SBrian Somers /* 18101384bd27SBrian Somers * Reap the intermediate process. As we're not exiting but the 18111384bd27SBrian Somers * intermediate is, we don't want it to become defunct. 18121384bd27SBrian Somers */ 18131384bd27SBrian Somers waitpid(pid, &status, 0); 18148e7b8599SBrian Somers /* Tweak our process arguments.... */ 1815ebe96675SBrian Somers SetTitle("session owner"); 181668602c3eSBrian Somers #ifndef NOSUID 1817a19a5c02SBrian Somers setuid(ID0realuid()); 181868602c3eSBrian Somers #endif 18191384bd27SBrian Somers /* 18201384bd27SBrian Somers * Hang around for a HUP. This should happen as soon as the 18218e7bd08eSBrian Somers * ppp that we passed our ctty descriptor to closes it. 18228e7bd08eSBrian Somers * NOTE: If this process dies, the passed descriptor becomes 18231384bd27SBrian Somers * invalid and will give a select() error by setting one 18241384bd27SBrian Somers * of the error fds, aborting the other ppp. We don't 18251384bd27SBrian Somers * want that to happen ! 18261384bd27SBrian Somers */ 18271384bd27SBrian Somers pause(); 18281384bd27SBrian Somers } 18292cb305afSBrian Somers _exit(0); 18301384bd27SBrian Somers break; 18311384bd27SBrian Somers } 18321384bd27SBrian Somers } 18339b5f8ffdSBrian Somers 1834057f1760SBrian Somers unsigned 18359b5f8ffdSBrian Somers bundle_HighestState(struct bundle *bundle) 18369b5f8ffdSBrian Somers { 18379b5f8ffdSBrian Somers struct datalink *dl; 1838057f1760SBrian Somers unsigned result = DATALINK_CLOSED; 18399b5f8ffdSBrian Somers 18409b5f8ffdSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 18419b5f8ffdSBrian Somers if (result < dl->state) 18429b5f8ffdSBrian Somers result = dl->state; 18439b5f8ffdSBrian Somers 18449b5f8ffdSBrian Somers return result; 18459b5f8ffdSBrian Somers } 1846991c2a7bSBrian Somers 1847991c2a7bSBrian Somers int 1848991c2a7bSBrian Somers bundle_Exception(struct bundle *bundle, int fd) 1849991c2a7bSBrian Somers { 1850991c2a7bSBrian Somers struct datalink *dl; 1851991c2a7bSBrian Somers 1852991c2a7bSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 1853991c2a7bSBrian Somers if (dl->physical->fd == fd) { 1854991c2a7bSBrian Somers datalink_Down(dl, CLOSE_NORMAL); 1855991c2a7bSBrian Somers return 1; 1856991c2a7bSBrian Somers } 1857991c2a7bSBrian Somers 1858991c2a7bSBrian Somers return 0; 1859991c2a7bSBrian Somers } 18601d1fc017SBrian Somers 18611d1fc017SBrian Somers void 186230949fd4SBrian Somers bundle_AdjustFilters(struct bundle *bundle, struct ncpaddr *local, 186330949fd4SBrian Somers struct ncpaddr *remote) 18641d1fc017SBrian Somers { 186530949fd4SBrian Somers filter_AdjustAddr(&bundle->filter.in, local, remote, NULL); 186630949fd4SBrian Somers filter_AdjustAddr(&bundle->filter.out, local, remote, NULL); 186730949fd4SBrian Somers filter_AdjustAddr(&bundle->filter.dial, local, remote, NULL); 186830949fd4SBrian Somers filter_AdjustAddr(&bundle->filter.alive, local, remote, NULL); 1869d568d6c4SBrian Somers } 1870d568d6c4SBrian Somers 1871d568d6c4SBrian Somers void 187230949fd4SBrian Somers bundle_AdjustDNS(struct bundle *bundle) 1873d568d6c4SBrian Somers { 187430949fd4SBrian Somers struct in_addr *dns = bundle->ncp.ipcp.ns.dns; 187530949fd4SBrian Somers 1876d568d6c4SBrian Somers filter_AdjustAddr(&bundle->filter.in, NULL, NULL, dns); 1877d568d6c4SBrian Somers filter_AdjustAddr(&bundle->filter.out, NULL, NULL, dns); 1878d568d6c4SBrian Somers filter_AdjustAddr(&bundle->filter.dial, NULL, NULL, dns); 1879d568d6c4SBrian Somers filter_AdjustAddr(&bundle->filter.alive, NULL, NULL, dns); 18801d1fc017SBrian Somers } 1881ab2de065SBrian Somers 1882ab2de065SBrian Somers void 1883ab2de065SBrian Somers bundle_CalculateBandwidth(struct bundle *bundle) 1884ab2de065SBrian Somers { 1885ab2de065SBrian Somers struct datalink *dl; 18866301d506SBrian Somers int sp, overhead, maxoverhead; 1887ab2de065SBrian Somers 1888ab2de065SBrian Somers bundle->bandwidth = 0; 1889c8b9fb53SBrian Somers bundle->iface->mtu = 0; 18906301d506SBrian Somers maxoverhead = 0; 18916301d506SBrian Somers 18926301d506SBrian Somers for (dl = bundle->links; dl; dl = dl->next) { 18936301d506SBrian Somers overhead = ccp_MTUOverhead(&dl->physical->link.ccp); 18946301d506SBrian Somers if (maxoverhead < overhead) 18956301d506SBrian Somers maxoverhead = overhead; 1896ab2de065SBrian Somers if (dl->state == DATALINK_OPEN) { 1897ab2de065SBrian Somers if ((sp = dl->mp.bandwidth) == 0 && 1898ab2de065SBrian Somers (sp = physical_GetSpeed(dl->physical)) == 0) 1899ab2de065SBrian Somers log_Printf(LogDEBUG, "%s: %s: Cannot determine bandwidth\n", 1900ab2de065SBrian Somers dl->name, dl->physical->name.full); 1901ab2de065SBrian Somers else 1902ab2de065SBrian Somers bundle->bandwidth += sp; 1903ab2de065SBrian Somers if (!bundle->ncp.mp.active) { 1904c8b9fb53SBrian Somers bundle->iface->mtu = dl->physical->link.lcp.his_mru; 1905ab2de065SBrian Somers break; 1906ab2de065SBrian Somers } 1907ab2de065SBrian Somers } 19086301d506SBrian Somers } 1909ab2de065SBrian Somers 1910ab2de065SBrian Somers if (bundle->bandwidth == 0) 1911ab2de065SBrian Somers bundle->bandwidth = 115200; /* Shrug */ 1912ab2de065SBrian Somers 19136301d506SBrian Somers if (bundle->ncp.mp.active) { 1914c8b9fb53SBrian Somers bundle->iface->mtu = bundle->ncp.mp.peer_mrru; 19156301d506SBrian Somers overhead = ccp_MTUOverhead(&bundle->ncp.mp.link.ccp); 19166301d506SBrian Somers if (maxoverhead < overhead) 19176301d506SBrian Somers maxoverhead = overhead; 19186301d506SBrian Somers } else if (!bundle->iface->mtu) 1919c8b9fb53SBrian Somers bundle->iface->mtu = DEF_MRU; 1920ab2de065SBrian Somers 1921ab2de065SBrian Somers #ifndef NORADIUS 192294d7be52SBrian Somers if (bundle->radius.valid && bundle->radius.mtu && 1923c8b9fb53SBrian Somers bundle->radius.mtu < bundle->iface->mtu) { 1924ab2de065SBrian Somers log_Printf(LogLCP, "Reducing MTU to radius value %lu\n", 1925ab2de065SBrian Somers bundle->radius.mtu); 1926c8b9fb53SBrian Somers bundle->iface->mtu = bundle->radius.mtu; 1927ab2de065SBrian Somers } 1928ab2de065SBrian Somers #endif 1929ab2de065SBrian Somers 19306301d506SBrian Somers if (maxoverhead) { 1931057f1760SBrian Somers log_Printf(LogLCP, "Reducing MTU from %lu to %lu (CCP requirement)\n", 19326301d506SBrian Somers bundle->iface->mtu, bundle->iface->mtu - maxoverhead); 19336301d506SBrian Somers bundle->iface->mtu -= maxoverhead; 19346301d506SBrian Somers } 19356301d506SBrian Somers 193694d7be52SBrian Somers tun_configure(bundle); 193703a2501aSBrian Somers 193803a2501aSBrian Somers route_UpdateMTU(bundle); 1939ab2de065SBrian Somers } 1940ab2de065SBrian Somers 1941ab2de065SBrian Somers void 1942ab2de065SBrian Somers bundle_AutoAdjust(struct bundle *bundle, int percent, int what) 1943ab2de065SBrian Somers { 1944ab2de065SBrian Somers struct datalink *dl, *choice, *otherlinkup; 1945ab2de065SBrian Somers 1946ab2de065SBrian Somers choice = otherlinkup = NULL; 1947ab2de065SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 1948ab2de065SBrian Somers if (dl->physical->type == PHYS_AUTO) { 1949ab2de065SBrian Somers if (dl->state == DATALINK_OPEN) { 1950ab2de065SBrian Somers if (what == AUTO_DOWN) { 1951ab2de065SBrian Somers if (choice) 1952ab2de065SBrian Somers otherlinkup = choice; 1953ab2de065SBrian Somers choice = dl; 1954ab2de065SBrian Somers } 1955ab2de065SBrian Somers } else if (dl->state == DATALINK_CLOSED) { 1956ab2de065SBrian Somers if (what == AUTO_UP) { 1957ab2de065SBrian Somers choice = dl; 1958ab2de065SBrian Somers break; 1959ab2de065SBrian Somers } 1960ab2de065SBrian Somers } else { 1961ab2de065SBrian Somers /* An auto link in an intermediate state - forget it for the moment */ 1962ab2de065SBrian Somers choice = NULL; 1963ab2de065SBrian Somers break; 1964ab2de065SBrian Somers } 1965ab2de065SBrian Somers } else if (dl->state == DATALINK_OPEN && what == AUTO_DOWN) 1966ab2de065SBrian Somers otherlinkup = dl; 1967ab2de065SBrian Somers 1968ab2de065SBrian Somers if (choice) { 1969ab2de065SBrian Somers if (what == AUTO_UP) { 1970ab2de065SBrian Somers log_Printf(LogPHASE, "%d%% saturation -> Opening link ``%s''\n", 1971ab2de065SBrian Somers percent, choice->name); 1972ab2de065SBrian Somers datalink_Up(choice, 1, 1); 1973a339e644SBrian Somers mp_CheckAutoloadTimer(&bundle->ncp.mp); 1974ab2de065SBrian Somers } else if (otherlinkup) { /* Only bring the second-last link down */ 1975ab2de065SBrian Somers log_Printf(LogPHASE, "%d%% saturation -> Closing link ``%s''\n", 1976ab2de065SBrian Somers percent, choice->name); 1977d2f5232dSBrian Somers datalink_Close(choice, CLOSE_STAYDOWN); 1978a339e644SBrian Somers mp_CheckAutoloadTimer(&bundle->ncp.mp); 1979ab2de065SBrian Somers } 1980ab2de065SBrian Somers } 1981ab2de065SBrian Somers } 1982ab2de065SBrian Somers 1983ab2de065SBrian Somers int 1984ab2de065SBrian Somers bundle_WantAutoloadTimer(struct bundle *bundle) 1985ab2de065SBrian Somers { 1986ab2de065SBrian Somers struct datalink *dl; 1987ab2de065SBrian Somers int autolink, opened; 1988ab2de065SBrian Somers 1989ab2de065SBrian Somers if (bundle->phase == PHASE_NETWORK) { 1990ab2de065SBrian Somers for (autolink = opened = 0, dl = bundle->links; dl; dl = dl->next) 1991ab2de065SBrian Somers if (dl->physical->type == PHYS_AUTO) { 1992ab2de065SBrian Somers if (++autolink == 2 || (autolink == 1 && opened)) 1993ab2de065SBrian Somers /* Two auto links or one auto and one open in NETWORK phase */ 1994ab2de065SBrian Somers return 1; 1995ab2de065SBrian Somers } else if (dl->state == DATALINK_OPEN) { 1996ab2de065SBrian Somers opened++; 1997ab2de065SBrian Somers if (autolink) 1998ab2de065SBrian Somers /* One auto and one open link in NETWORK phase */ 1999ab2de065SBrian Somers return 1; 2000ab2de065SBrian Somers } 2001ab2de065SBrian Somers } 2002ab2de065SBrian Somers 2003ab2de065SBrian Somers return 0; 2004ab2de065SBrian Somers } 200506b47306SBrian Somers 200606b47306SBrian Somers void 200706b47306SBrian Somers bundle_ChangedPID(struct bundle *bundle) 200806b47306SBrian Somers { 200906b47306SBrian Somers #ifdef TUNSIFPID 201006b47306SBrian Somers ioctl(bundle->dev.fd, TUNSIFPID, 0); 201106b47306SBrian Somers #endif 201206b47306SBrian Somers } 201346df5aa7SBrian Somers 201446df5aa7SBrian Somers int 201546df5aa7SBrian Somers bundle_Uptime(struct bundle *bundle) 201646df5aa7SBrian Somers { 201746df5aa7SBrian Somers if (bundle->upat) 201846df5aa7SBrian Somers return time(NULL) - bundle->upat; 201946df5aa7SBrian Somers 202046df5aa7SBrian Somers return 0; 202146df5aa7SBrian Somers } 2022