17a6f8720SBrian Somers /*- 27a6f8720SBrian Somers * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org> 37a6f8720SBrian Somers * All rights reserved. 47a6f8720SBrian Somers * 57a6f8720SBrian Somers * Redistribution and use in source and binary forms, with or without 67a6f8720SBrian Somers * modification, are permitted provided that the following conditions 77a6f8720SBrian Somers * are met: 87a6f8720SBrian Somers * 1. Redistributions of source code must retain the above copyright 97a6f8720SBrian Somers * notice, this list of conditions and the following disclaimer. 107a6f8720SBrian Somers * 2. Redistributions in binary form must reproduce the above copyright 117a6f8720SBrian Somers * notice, this list of conditions and the following disclaimer in the 127a6f8720SBrian Somers * documentation and/or other materials provided with the distribution. 137a6f8720SBrian Somers * 147a6f8720SBrian Somers * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 157a6f8720SBrian Somers * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 167a6f8720SBrian Somers * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 177a6f8720SBrian Somers * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 187a6f8720SBrian Somers * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 197a6f8720SBrian Somers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 207a6f8720SBrian Somers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 217a6f8720SBrian Somers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 227a6f8720SBrian Somers * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 237a6f8720SBrian Somers * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 247a6f8720SBrian Somers * SUCH DAMAGE. 257a6f8720SBrian Somers * 2697d92980SPeter Wemm * $FreeBSD$ 277a6f8720SBrian Somers */ 287a6f8720SBrian Somers 291384bd27SBrian Somers #include <sys/param.h> 307a6f8720SBrian Somers #include <sys/socket.h> 317a6f8720SBrian Somers #include <netinet/in.h> 327a6f8720SBrian Somers #include <net/if.h> 33eb6e5e05SBrian Somers #include <net/if_tun.h> /* For TUNSIFMODE & TUNSLMODE */ 347a6f8720SBrian Somers #include <arpa/inet.h> 357a6f8720SBrian Somers #include <net/route.h> 36eaa4df37SBrian Somers #include <netinet/in_systm.h> 37eaa4df37SBrian Somers #include <netinet/ip.h> 381fa665f5SBrian Somers #include <sys/un.h> 397a6f8720SBrian Somers 407a6f8720SBrian Somers #include <errno.h> 417a6f8720SBrian Somers #include <fcntl.h> 42cf0a3940SBrian Somers #ifdef __OpenBSD__ 43cf0a3940SBrian Somers #include <util.h> 44cf0a3940SBrian Somers #else 45cf0a3940SBrian Somers #include <libutil.h> 46cf0a3940SBrian Somers #endif 4747723d29SBrian Somers #include <paths.h> 487a6f8720SBrian Somers #include <stdio.h> 496f384573SBrian Somers #include <stdlib.h> 507a6f8720SBrian Somers #include <string.h> 5196c9bb21SBrian Somers #include <sys/uio.h> 5254cd8e13SBrian Somers #include <sys/wait.h> 53fc3034caSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD) 54fc3034caSBrian Somers #include <sys/linker.h> 55fdb4bb1bSBrian Somers #include <sys/module.h> 56fc3034caSBrian Somers #endif 577a6f8720SBrian Somers #include <termios.h> 587a6f8720SBrian Somers #include <unistd.h> 597a6f8720SBrian Somers 605d9e6103SBrian Somers #include "layer.h" 61c9e11a11SBrian Somers #include "defs.h" 627a6f8720SBrian Somers #include "command.h" 637a6f8720SBrian Somers #include "mbuf.h" 647a6f8720SBrian Somers #include "log.h" 657a6f8720SBrian Somers #include "id.h" 667a6f8720SBrian Somers #include "timer.h" 677a6f8720SBrian Somers #include "fsm.h" 687a6f8720SBrian Somers #include "iplist.h" 69879ed6faSBrian Somers #include "lqr.h" 70455aabc3SBrian Somers #include "hdlc.h" 717a6f8720SBrian Somers #include "throughput.h" 72eaa4df37SBrian Somers #include "slcompress.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 843b0f8d2eSBrian Somers #include "bundle.h" 85455aabc3SBrian Somers #include "async.h" 86455aabc3SBrian Somers #include "physical.h" 87455aabc3SBrian Somers #include "auth.h" 885d9e6103SBrian Somers #include "proto.h" 89455aabc3SBrian Somers #include "chap.h" 90455aabc3SBrian Somers #include "tun.h" 9185b542cfSBrian Somers #include "prompt.h" 923006ec67SBrian Somers #include "chat.h" 9392b09558SBrian Somers #include "cbcp.h" 943006ec67SBrian Somers #include "datalink.h" 953006ec67SBrian Somers #include "ip.h" 968fa6ebe4SBrian Somers #include "iface.h" 977a6f8720SBrian Somers 98ab2de065SBrian Somers #define SCATTER_SEGMENTS 6 /* version, datalink, name, physical, 99ab2de065SBrian Somers throughput, device */ 10087c3786eSBrian Somers #define SOCKET_OVERHEAD 100 /* additional buffer space for large 10187c3786eSBrian Somers {recv,send}msg() calls */ 10287c3786eSBrian Somers 10387c3786eSBrian Somers #define SEND_MAXFD 2 /* Max file descriptors passed through 10487c3786eSBrian Somers the local domain socket */ 10596c9bb21SBrian Somers 10604eaa58cSBrian Somers static int bundle_RemainingIdleTime(struct bundle *); 10704eaa58cSBrian Somers 108455aabc3SBrian Somers static const char *PhaseNames[] = { 109455aabc3SBrian Somers "Dead", "Establish", "Authenticate", "Network", "Terminate" 110455aabc3SBrian Somers }; 111455aabc3SBrian Somers 112455aabc3SBrian Somers const char * 113455aabc3SBrian Somers bundle_PhaseName(struct bundle *bundle) 1147a6f8720SBrian Somers { 115455aabc3SBrian Somers return bundle->phase <= PHASE_TERMINATE ? 116455aabc3SBrian Somers PhaseNames[bundle->phase] : "unknown"; 1177a6f8720SBrian Somers } 1187a6f8720SBrian Somers 119455aabc3SBrian Somers void 1205563ebdeSBrian Somers bundle_NewPhase(struct bundle *bundle, u_int new) 121455aabc3SBrian Somers { 122aef795ccSBrian Somers if (new == bundle->phase) 123aef795ccSBrian Somers return; 124aef795ccSBrian Somers 125e2ebb036SBrian Somers if (new <= PHASE_TERMINATE) 126dd7e2610SBrian Somers log_Printf(LogPHASE, "bundle: %s\n", PhaseNames[new]); 1277a6f8720SBrian Somers 128455aabc3SBrian Somers switch (new) { 129455aabc3SBrian Somers case PHASE_DEAD: 1300f2f3eb3SBrian Somers log_DisplayPrompts(); 131455aabc3SBrian Somers bundle->phase = new; 132455aabc3SBrian Somers break; 133455aabc3SBrian Somers 134455aabc3SBrian Somers case PHASE_ESTABLISH: 135455aabc3SBrian Somers bundle->phase = new; 136455aabc3SBrian Somers break; 137455aabc3SBrian Somers 138455aabc3SBrian Somers case PHASE_AUTHENTICATE: 139455aabc3SBrian Somers bundle->phase = new; 1400f2f3eb3SBrian Somers log_DisplayPrompts(); 141455aabc3SBrian Somers break; 142455aabc3SBrian Somers 143455aabc3SBrian Somers case PHASE_NETWORK: 144dd7e2610SBrian Somers fsm_Up(&bundle->ncp.ipcp.fsm); 145dd7e2610SBrian Somers fsm_Open(&bundle->ncp.ipcp.fsm); 146673903ecSBrian Somers bundle->phase = new; 1470f2f3eb3SBrian Somers log_DisplayPrompts(); 148673903ecSBrian Somers break; 149455aabc3SBrian Somers 150455aabc3SBrian Somers case PHASE_TERMINATE: 151455aabc3SBrian Somers bundle->phase = new; 152673903ecSBrian Somers mp_Down(&bundle->ncp.mp); 1530f2f3eb3SBrian Somers log_DisplayPrompts(); 154455aabc3SBrian Somers break; 1557a6f8720SBrian Somers } 1567a6f8720SBrian Somers } 1577a6f8720SBrian Somers 1586d666775SBrian Somers static void 1596d666775SBrian Somers bundle_LayerStart(void *v, struct fsm *fp) 1607a6f8720SBrian Somers { 1613006ec67SBrian Somers /* The given FSM is about to start up ! */ 1627a6f8720SBrian Somers } 1637a6f8720SBrian Somers 1645cf4388bSBrian Somers 1655cf4388bSBrian Somers static void 1665cf4388bSBrian Somers bundle_Notify(struct bundle *bundle, char c) 1675cf4388bSBrian Somers { 1685cf4388bSBrian Somers if (bundle->notify.fd != -1) { 1695cf4388bSBrian Somers if (write(bundle->notify.fd, &c, 1) == 1) 170dd7e2610SBrian Somers log_Printf(LogPHASE, "Parent notified of success.\n"); 1715cf4388bSBrian Somers else 172dd7e2610SBrian Somers log_Printf(LogPHASE, "Failed to notify parent of success.\n"); 1735cf4388bSBrian Somers close(bundle->notify.fd); 1745cf4388bSBrian Somers bundle->notify.fd = -1; 1755cf4388bSBrian Somers } 1765cf4388bSBrian Somers } 1773b0f8d2eSBrian Somers 1786d666775SBrian Somers static void 1796f8e9f0aSBrian Somers bundle_ClearQueues(void *v) 1806f8e9f0aSBrian Somers { 1816f8e9f0aSBrian Somers struct bundle *bundle = (struct bundle *)v; 1826f8e9f0aSBrian Somers struct datalink *dl; 1836f8e9f0aSBrian Somers 1846f8e9f0aSBrian Somers log_Printf(LogPHASE, "Clearing choked output queue\n"); 1856f8e9f0aSBrian Somers timer_Stop(&bundle->choked.timer); 1866f8e9f0aSBrian Somers 1876f8e9f0aSBrian Somers /* 1886f8e9f0aSBrian Somers * Emergency time: 1896f8e9f0aSBrian Somers * 1906f8e9f0aSBrian Somers * We've had a full queue for PACKET_DEL_SECS seconds without being 1916f8e9f0aSBrian Somers * able to get rid of any of the packets. We've probably given up 1926f8e9f0aSBrian Somers * on the redials at this point, and the queued data has almost 1936f8e9f0aSBrian Somers * definitely been timed out by the layer above. As this is preventing 1946f8e9f0aSBrian Somers * us from reading the TUN_NAME device (we don't want to buffer stuff 1956f8e9f0aSBrian Somers * indefinitely), we may as well nuke this data and start with a clean 1966f8e9f0aSBrian Somers * slate ! 1976f8e9f0aSBrian Somers * 1986f8e9f0aSBrian Somers * Unfortunately, this has the side effect of shafting any compression 1996f8e9f0aSBrian Somers * dictionaries in use (causing the relevant RESET_REQ/RESET_ACK). 2006f8e9f0aSBrian Somers */ 2016f8e9f0aSBrian Somers 2025a72b6edSBrian Somers ip_DeleteQueue(&bundle->ncp.ipcp); 2036f8e9f0aSBrian Somers mp_DeleteQueue(&bundle->ncp.mp); 2046f8e9f0aSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 2056f8e9f0aSBrian Somers physical_DeleteQueue(dl->physical); 2066f8e9f0aSBrian Somers } 2076f8e9f0aSBrian Somers 2086f8e9f0aSBrian Somers static void 209ff0f9439SBrian Somers bundle_LinkAdded(struct bundle *bundle, struct datalink *dl) 210ff0f9439SBrian Somers { 211ff0f9439SBrian Somers bundle->phys_type.all |= dl->physical->type; 212ff0f9439SBrian Somers if (dl->state == DATALINK_OPEN) 213ff0f9439SBrian Somers bundle->phys_type.open |= dl->physical->type; 214ff0f9439SBrian Somers 215ff0f9439SBrian Somers if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) 216ff0f9439SBrian Somers != bundle->phys_type.open && bundle->idle.timer.state == TIMER_STOPPED) 217ff0f9439SBrian Somers /* We may need to start our idle timer */ 218ff0f9439SBrian Somers bundle_StartIdleTimer(bundle); 219ff0f9439SBrian Somers } 220ff0f9439SBrian Somers 22192b09558SBrian Somers void 222ff0f9439SBrian Somers bundle_LinksRemoved(struct bundle *bundle) 223ff0f9439SBrian Somers { 224ff0f9439SBrian Somers struct datalink *dl; 225ff0f9439SBrian Somers 226ff0f9439SBrian Somers bundle->phys_type.all = bundle->phys_type.open = 0; 227ff0f9439SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 228ff0f9439SBrian Somers bundle_LinkAdded(bundle, dl); 229ff0f9439SBrian Somers 230ab2de065SBrian Somers bundle_CalculateBandwidth(bundle); 231ab2de065SBrian Somers mp_CheckAutoloadTimer(&bundle->ncp.mp); 232ab2de065SBrian Somers 233ff0f9439SBrian Somers if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) 234ff0f9439SBrian Somers == bundle->phys_type.open) 235ff0f9439SBrian Somers bundle_StopIdleTimer(bundle); 236ff0f9439SBrian Somers } 23704eaa58cSBrian Somers 23804eaa58cSBrian Somers static void 2396f384573SBrian Somers bundle_LayerUp(void *v, struct fsm *fp) 2407a6f8720SBrian Somers { 2413006ec67SBrian Somers /* 2423006ec67SBrian Somers * The given fsm is now up 243ab2de065SBrian Somers * If it's an LCP, adjust our phys_mode.open value and check the 244ab2de065SBrian Somers * autoload timer. 245ab2de065SBrian Somers * If it's the first NCP, calculate our bandwidth 246dade2407SBrian Somers * If it's the first NCP, set our ``upat'' time 2473b0f8d2eSBrian Somers * If it's the first NCP, start the idle timer. 248ab2de065SBrian Somers * If it's an NCP, tell our -background parent to go away. 249ab2de065SBrian Somers * If it's the first NCP, start the autoload timer 2503006ec67SBrian Somers */ 2516f384573SBrian Somers struct bundle *bundle = (struct bundle *)v; 2526d666775SBrian Somers 2535563ebdeSBrian Somers if (fp->proto == PROTO_LCP) { 254ff0f9439SBrian Somers struct physical *p = link2physical(fp->link); 255ff0f9439SBrian Somers 256ff0f9439SBrian Somers bundle_LinkAdded(bundle, p->dl); 257ab2de065SBrian Somers mp_CheckAutoloadTimer(&bundle->ncp.mp); 2583b0f8d2eSBrian Somers } else if (fp->proto == PROTO_IPCP) { 259ab2de065SBrian Somers bundle_CalculateBandwidth(fp->bundle); 260dade2407SBrian Somers time(&bundle->upat); 261ab886ad0SBrian Somers bundle_StartIdleTimer(bundle); 2625cf4388bSBrian Somers bundle_Notify(bundle, EX_NORMAL); 263ab2de065SBrian Somers mp_CheckAutoloadTimer(&fp->bundle->ncp.mp); 2647a6f8720SBrian Somers } 265ab886ad0SBrian Somers } 2667a6f8720SBrian Somers 2676d666775SBrian Somers static void 2686d666775SBrian Somers bundle_LayerDown(void *v, struct fsm *fp) 2696d666775SBrian Somers { 2706d666775SBrian Somers /* 2716d666775SBrian Somers * The given FSM has been told to come down. 272ab886ad0SBrian Somers * If it's our last NCP, stop the idle timer. 273dade2407SBrian Somers * If it's our last NCP, clear our ``upat'' value. 274ab2de065SBrian Somers * If it's our last NCP, stop the autoload timer 275ff0f9439SBrian Somers * If it's an LCP, adjust our phys_type.open value and any timers. 27686b1f0d7SBrian Somers * If it's an LCP and we're in multilink mode, adjust our tun 27786b1f0d7SBrian Somers * speed and make sure our minimum sequence number is adjusted. 2786d666775SBrian Somers */ 279ab886ad0SBrian Somers 280ab886ad0SBrian Somers struct bundle *bundle = (struct bundle *)v; 281ab886ad0SBrian Somers 282ab2de065SBrian Somers if (fp->proto == PROTO_IPCP) { 283ab886ad0SBrian Somers bundle_StopIdleTimer(bundle); 284dade2407SBrian Somers bundle->upat = 0; 285ab2de065SBrian Somers mp_StopAutoloadTimer(&bundle->ncp.mp); 286ab2de065SBrian Somers } else if (fp->proto == PROTO_LCP) { 287ff0f9439SBrian Somers bundle_LinksRemoved(bundle); /* adjust timers & phys_type values */ 288ff0f9439SBrian Somers if (bundle->ncp.mp.active) { 2893b0f8d2eSBrian Somers struct datalink *dl; 29086b1f0d7SBrian Somers struct datalink *lost; 2913b0f8d2eSBrian Somers 29286b1f0d7SBrian Somers lost = NULL; 293faefde08SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 29486b1f0d7SBrian Somers if (fp == &dl->physical->link.lcp.fsm) 29586b1f0d7SBrian Somers lost = dl; 29686b1f0d7SBrian Somers 297ab2de065SBrian Somers bundle_CalculateBandwidth(bundle); 29886b1f0d7SBrian Somers 29986b1f0d7SBrian Somers if (lost) 30086b1f0d7SBrian Somers mp_LinkLost(&bundle->ncp.mp, lost); 30186b1f0d7SBrian Somers else 302a33b2ef7SBrian Somers log_Printf(LogALERT, "Oops, lost an unrecognised datalink (%s) !\n", 30386b1f0d7SBrian Somers fp->link->name); 3043b0f8d2eSBrian Somers } 3056d666775SBrian Somers } 306ff0f9439SBrian Somers } 3076d666775SBrian Somers 3086d666775SBrian Somers static void 3096d666775SBrian Somers bundle_LayerFinish(void *v, struct fsm *fp) 3106d666775SBrian Somers { 3116d666775SBrian Somers /* The given fsm is now down (fp cannot be NULL) 3126d666775SBrian Somers * 313dd7e2610SBrian Somers * If it's the last LCP, fsm_Down all NCPs 314dd7e2610SBrian Somers * If it's the last NCP, fsm_Close all LCPs 3156d666775SBrian Somers */ 3166d666775SBrian Somers 3176d666775SBrian Somers struct bundle *bundle = (struct bundle *)v; 3186d666775SBrian Somers struct datalink *dl; 3196d666775SBrian Somers 3203b0f8d2eSBrian Somers if (fp->proto == PROTO_IPCP) { 32126afeaa2SBrian Somers if (bundle_Phase(bundle) != PHASE_DEAD) 32225092092SBrian Somers bundle_NewPhase(bundle, PHASE_TERMINATE); 3236d666775SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 324d4d5d2f8SBrian Somers datalink_Close(dl, CLOSE_STAYDOWN); 32509206a6fSBrian Somers fsm2initial(fp); 3263b0f8d2eSBrian Somers } else if (fp->proto == PROTO_LCP) { 3273b0f8d2eSBrian Somers int others_active; 328a611cad6SBrian Somers 3293b0f8d2eSBrian Somers others_active = 0; 3303b0f8d2eSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 3313b0f8d2eSBrian Somers if (fp != &dl->physical->link.lcp.fsm && 3323b0f8d2eSBrian Somers dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) 3333b0f8d2eSBrian Somers others_active++; 3343b0f8d2eSBrian Somers 33509206a6fSBrian Somers if (!others_active) 33609206a6fSBrian Somers fsm2initial(&bundle->ncp.ipcp.fsm); 3373b0f8d2eSBrian Somers } 3383b0f8d2eSBrian Somers } 3396d666775SBrian Somers 3407a6f8720SBrian Somers int 3417a6f8720SBrian Somers bundle_LinkIsUp(const struct bundle *bundle) 3427a6f8720SBrian Somers { 3435828db6dSBrian Somers return bundle->ncp.ipcp.fsm.state == ST_OPENED; 3447a6f8720SBrian Somers } 3457a6f8720SBrian Somers 3467a6f8720SBrian Somers void 3479c81b87dSBrian Somers bundle_Close(struct bundle *bundle, const char *name, int how) 3487a6f8720SBrian Somers { 349455aabc3SBrian Somers /* 3503006ec67SBrian Somers * Please close the given datalink. 351dd7e2610SBrian Somers * If name == NULL or name is the last datalink, fsm_Close all NCPs 3526f384573SBrian Somers * (except our MP) 3533b0f8d2eSBrian Somers * If it isn't the last datalink, just Close that datalink. 354455aabc3SBrian Somers */ 3557a6f8720SBrian Somers 3563b0f8d2eSBrian Somers struct datalink *dl, *this_dl; 3573b0f8d2eSBrian Somers int others_active; 3583006ec67SBrian Somers 3593b0f8d2eSBrian Somers others_active = 0; 3603b0f8d2eSBrian Somers this_dl = NULL; 3613b0f8d2eSBrian Somers 3623b0f8d2eSBrian Somers for (dl = bundle->links; dl; dl = dl->next) { 3633b0f8d2eSBrian Somers if (name && !strcasecmp(name, dl->name)) 3643b0f8d2eSBrian Somers this_dl = dl; 3653b0f8d2eSBrian Somers if (name == NULL || this_dl == dl) { 3669c81b87dSBrian Somers switch (how) { 3679c81b87dSBrian Somers case CLOSE_LCP: 3689c81b87dSBrian Somers datalink_DontHangup(dl); 3699c81b87dSBrian Somers /* fall through */ 3709c81b87dSBrian Somers case CLOSE_STAYDOWN: 3713006ec67SBrian Somers datalink_StayDown(dl); 3729c81b87dSBrian Somers break; 3739c81b87dSBrian Somers } 3743b0f8d2eSBrian Somers } else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) 3753b0f8d2eSBrian Somers others_active++; 3763b0f8d2eSBrian Somers } 3773b0f8d2eSBrian Somers 3783b0f8d2eSBrian Somers if (name && this_dl == NULL) { 379dd7e2610SBrian Somers log_Printf(LogWARN, "%s: Invalid datalink name\n", name); 3803b0f8d2eSBrian Somers return; 3813b0f8d2eSBrian Somers } 3823b0f8d2eSBrian Somers 3833b0f8d2eSBrian Somers if (!others_active) { 38404eaa58cSBrian Somers bundle_StopIdleTimer(bundle); 3853b0f8d2eSBrian Somers if (bundle->ncp.ipcp.fsm.state > ST_CLOSED || 3863b0f8d2eSBrian Somers bundle->ncp.ipcp.fsm.state == ST_STARTING) 387dd7e2610SBrian Somers fsm_Close(&bundle->ncp.ipcp.fsm); 3883b0f8d2eSBrian Somers else { 38909206a6fSBrian Somers fsm2initial(&bundle->ncp.ipcp.fsm); 390d345321bSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 3919c81b87dSBrian Somers datalink_Close(dl, how); 3927a6f8720SBrian Somers } 3933b0f8d2eSBrian Somers } else if (this_dl && this_dl->state != DATALINK_CLOSED && 3943b0f8d2eSBrian Somers this_dl->state != DATALINK_HANGUP) 3959c81b87dSBrian Somers datalink_Close(this_dl, how); 396d2fd8d77SBrian Somers } 3977a6f8720SBrian Somers 3981bc9b5baSBrian Somers void 399899011c4SBrian Somers bundle_Down(struct bundle *bundle, int how) 4001bc9b5baSBrian Somers { 4011bc9b5baSBrian Somers struct datalink *dl; 4021bc9b5baSBrian Somers 4031bc9b5baSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 404899011c4SBrian Somers datalink_Down(dl, how); 4051bc9b5baSBrian Somers } 4061bc9b5baSBrian Somers 4072f786681SBrian Somers static int 4082f786681SBrian Somers bundle_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n) 4092f786681SBrian Somers { 4102f786681SBrian Somers struct bundle *bundle = descriptor2bundle(d); 4112f786681SBrian Somers struct datalink *dl; 412ab2de065SBrian Somers int result, queued, nlinks; 4132f786681SBrian Somers 4142f786681SBrian Somers result = 0; 4152f786681SBrian Somers 416078c562eSBrian Somers /* If there are aren't many packets queued, look for some more. */ 41704eaa58cSBrian Somers for (nlinks = 0, dl = bundle->links; dl; dl = dl->next) 41804eaa58cSBrian Somers nlinks++; 41904eaa58cSBrian Somers 42004eaa58cSBrian Somers if (nlinks) { 4215a72b6edSBrian Somers queued = r ? bundle_FillQueues(bundle) : ip_QueueLen(&bundle->ncp.ipcp); 42204eaa58cSBrian Somers 423ff0f9439SBrian Somers if (r && (bundle->phase == PHASE_NETWORK || 424ff0f9439SBrian Somers bundle->phys_type.all & PHYS_AUTO)) { 42504eaa58cSBrian Somers /* enough surplus so that we can tell if we're getting swamped */ 426442f8495SBrian Somers if (queued < 30) { 42704eaa58cSBrian Somers /* Not enough - select() for more */ 4286f8e9f0aSBrian Somers if (bundle->choked.timer.state == TIMER_RUNNING) 4296f8e9f0aSBrian Somers timer_Stop(&bundle->choked.timer); /* Not needed any more */ 43004eaa58cSBrian Somers FD_SET(bundle->dev.fd, r); 431faefde08SBrian Somers if (*n < bundle->dev.fd + 1) 432faefde08SBrian Somers *n = bundle->dev.fd + 1; 4331384bd27SBrian Somers log_Printf(LogTIMER, "%s: fdset(r) %d\n", TUN_NAME, bundle->dev.fd); 434078c562eSBrian Somers result++; 4356f8e9f0aSBrian Somers } else if (bundle->choked.timer.state == TIMER_STOPPED) { 4366f8e9f0aSBrian Somers bundle->choked.timer.func = bundle_ClearQueues; 4376f8e9f0aSBrian Somers bundle->choked.timer.name = "output choke"; 4386f8e9f0aSBrian Somers bundle->choked.timer.load = bundle->cfg.choked.timeout * SECTICKS; 4396f8e9f0aSBrian Somers bundle->choked.timer.arg = bundle; 4406f8e9f0aSBrian Somers timer_Start(&bundle->choked.timer); 441078c562eSBrian Somers } 44204eaa58cSBrian Somers } 44304eaa58cSBrian Somers } 444078c562eSBrian Somers 445f0cdd9c0SBrian Somers #ifndef NORADIUS 446f0cdd9c0SBrian Somers result += descriptor_UpdateSet(&bundle->radius.desc, r, w, e, n); 447f0cdd9c0SBrian Somers #endif 448f0cdd9c0SBrian Somers 44971555108SBrian Somers /* Which links need a select() ? */ 45071555108SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 45171555108SBrian Somers result += descriptor_UpdateSet(&dl->desc, r, w, e, n); 45271555108SBrian Somers 453ea722969SBrian Somers /* 454ea722969SBrian Somers * This *MUST* be called after the datalink UpdateSet()s as it 45504eaa58cSBrian Somers * might be ``holding'' one of the datalinks (death-row) and 45604eaa58cSBrian Somers * wants to be able to de-select() it from the descriptor set. 457ea722969SBrian Somers */ 4580f2f3eb3SBrian Somers result += descriptor_UpdateSet(&bundle->ncp.mp.server.desc, r, w, e, n); 459ea722969SBrian Somers 4602f786681SBrian Somers return result; 4612f786681SBrian Somers } 4622f786681SBrian Somers 4632f786681SBrian Somers static int 4642f786681SBrian Somers bundle_IsSet(struct descriptor *d, const fd_set *fdset) 4652f786681SBrian Somers { 4662f786681SBrian Somers struct bundle *bundle = descriptor2bundle(d); 4672f786681SBrian Somers struct datalink *dl; 4682f786681SBrian Somers 4692f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 4702f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 4712f786681SBrian Somers return 1; 4722f786681SBrian Somers 473f0cdd9c0SBrian Somers #ifndef NORADIUS 474f0cdd9c0SBrian Somers if (descriptor_IsSet(&bundle->radius.desc, fdset)) 475f0cdd9c0SBrian Somers return 1; 476f0cdd9c0SBrian Somers #endif 477f0cdd9c0SBrian Somers 478332b9de0SBrian Somers if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 479332b9de0SBrian Somers return 1; 480332b9de0SBrian Somers 481faefde08SBrian Somers return FD_ISSET(bundle->dev.fd, fdset); 4822f786681SBrian Somers } 4832f786681SBrian Somers 4842f786681SBrian Somers static void 4852f786681SBrian Somers bundle_DescriptorRead(struct descriptor *d, struct bundle *bundle, 4862f786681SBrian Somers const fd_set *fdset) 4872f786681SBrian Somers { 4882f786681SBrian Somers struct datalink *dl; 4892f786681SBrian Somers 490ea722969SBrian Somers if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 491ea722969SBrian Somers descriptor_Read(&bundle->ncp.mp.server.desc, bundle, fdset); 492ea722969SBrian Somers 4932f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 4942f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 4952f786681SBrian Somers descriptor_Read(&dl->desc, bundle, fdset); 496b6217683SBrian Somers 497f0cdd9c0SBrian Somers #ifndef NORADIUS 498f0cdd9c0SBrian Somers if (descriptor_IsSet(&bundle->radius.desc, fdset)) 499f0cdd9c0SBrian Somers descriptor_Read(&bundle->radius.desc, bundle, fdset); 500f0cdd9c0SBrian Somers #endif 501f0cdd9c0SBrian Somers 502faefde08SBrian Somers if (FD_ISSET(bundle->dev.fd, fdset)) { 503078c562eSBrian Somers struct tun_data tun; 504078c562eSBrian Somers int n, pri; 505078c562eSBrian Somers 506078c562eSBrian Somers /* something to read from tun */ 507faefde08SBrian Somers n = read(bundle->dev.fd, &tun, sizeof tun); 508078c562eSBrian Somers if (n < 0) { 509a33b2ef7SBrian Somers log_Printf(LogWARN, "read from %s: %s\n", TUN_NAME, strerror(errno)); 510078c562eSBrian Somers return; 511078c562eSBrian Somers } 512078c562eSBrian Somers n -= sizeof tun - sizeof tun.data; 513078c562eSBrian Somers if (n <= 0) { 514a33b2ef7SBrian Somers log_Printf(LogERROR, "read from %s: Only %d bytes read ?\n", TUN_NAME, n); 515078c562eSBrian Somers return; 516078c562eSBrian Somers } 517078c562eSBrian Somers if (!tun_check_header(tun, AF_INET)) 518078c562eSBrian Somers return; 519078c562eSBrian Somers 520078c562eSBrian Somers if (((struct ip *)tun.data)->ip_dst.s_addr == 521078c562eSBrian Somers bundle->ncp.ipcp.my_ip.s_addr) { 522078c562eSBrian Somers /* we've been asked to send something addressed *to* us :( */ 523078c562eSBrian Somers if (Enabled(bundle, OPT_LOOPBACK)) { 524078c562eSBrian Somers pri = PacketCheck(bundle, tun.data, n, &bundle->filter.in); 525078c562eSBrian Somers if (pri >= 0) { 5265d9e6103SBrian Somers n += sizeof tun - sizeof tun.data; 5275d9e6103SBrian Somers write(bundle->dev.fd, &tun, n); 528078c562eSBrian Somers log_Printf(LogDEBUG, "Looped back packet addressed to myself\n"); 529078c562eSBrian Somers } 530078c562eSBrian Somers return; 531078c562eSBrian Somers } else 532078c562eSBrian Somers log_Printf(LogDEBUG, "Oops - forwarding packet addressed to myself\n"); 533078c562eSBrian Somers } 534078c562eSBrian Somers 535078c562eSBrian Somers /* 536078c562eSBrian Somers * Process on-demand dialup. Output packets are queued within tunnel 537078c562eSBrian Somers * device until IPCP is opened. 538078c562eSBrian Somers */ 539078c562eSBrian Somers 540078c562eSBrian Somers if (bundle_Phase(bundle) == PHASE_DEAD) { 541078c562eSBrian Somers /* 542078c562eSBrian Somers * Note, we must be in AUTO mode :-/ otherwise our interface should 543078c562eSBrian Somers * *not* be UP and we can't receive data 544078c562eSBrian Somers */ 545078c562eSBrian Somers if ((pri = PacketCheck(bundle, tun.data, n, &bundle->filter.dial)) >= 0) 546ba23f397SBrian Somers bundle_Open(bundle, NULL, PHYS_AUTO, 0); 547078c562eSBrian Somers else 548078c562eSBrian Somers /* 549078c562eSBrian Somers * Drop the packet. If we were to queue it, we'd just end up with 550078c562eSBrian Somers * a pile of timed-out data in our output queue by the time we get 551078c562eSBrian Somers * around to actually dialing. We'd also prematurely reach the 552078c562eSBrian Somers * threshold at which we stop select()ing to read() the tun 553078c562eSBrian Somers * device - breaking auto-dial. 554078c562eSBrian Somers */ 555078c562eSBrian Somers return; 556078c562eSBrian Somers } 557078c562eSBrian Somers 558078c562eSBrian Somers pri = PacketCheck(bundle, tun.data, n, &bundle->filter.out); 5595d9e6103SBrian Somers if (pri >= 0) 5605a72b6edSBrian Somers ip_Enqueue(&bundle->ncp.ipcp, pri, tun.data, n); 561078c562eSBrian Somers } 562078c562eSBrian Somers } 5632f786681SBrian Somers 5641af29a6eSBrian Somers static int 5652f786681SBrian Somers bundle_DescriptorWrite(struct descriptor *d, struct bundle *bundle, 5662f786681SBrian Somers const fd_set *fdset) 5672f786681SBrian Somers { 5682f786681SBrian Somers struct datalink *dl; 5691af29a6eSBrian Somers int result = 0; 5702f786681SBrian Somers 571ea722969SBrian Somers /* This is not actually necessary as struct mpserver doesn't Write() */ 572ea722969SBrian Somers if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 573ea722969SBrian Somers descriptor_Write(&bundle->ncp.mp.server.desc, bundle, fdset); 574ea722969SBrian Somers 5752f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 5762f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 5771af29a6eSBrian Somers result += descriptor_Write(&dl->desc, bundle, fdset); 5781af29a6eSBrian Somers 5791af29a6eSBrian Somers return result; 5802f786681SBrian Somers } 5812f786681SBrian Somers 582da66dd13SBrian Somers void 5831384bd27SBrian Somers bundle_LockTun(struct bundle *bundle) 5841384bd27SBrian Somers { 5851384bd27SBrian Somers FILE *lockfile; 5861384bd27SBrian Somers char pidfile[MAXPATHLEN]; 5871384bd27SBrian Somers 5881384bd27SBrian Somers snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit); 5891384bd27SBrian Somers lockfile = ID0fopen(pidfile, "w"); 5901384bd27SBrian Somers if (lockfile != NULL) { 5911384bd27SBrian Somers fprintf(lockfile, "%d\n", (int)getpid()); 5921384bd27SBrian Somers fclose(lockfile); 5931384bd27SBrian Somers } 5941384bd27SBrian Somers #ifndef RELEASE_CRUNCH 5951384bd27SBrian Somers else 5961384bd27SBrian Somers log_Printf(LogERROR, "Warning: Can't create %s: %s\n", 5971384bd27SBrian Somers pidfile, strerror(errno)); 5981384bd27SBrian Somers #endif 5991384bd27SBrian Somers } 6001384bd27SBrian Somers 6011384bd27SBrian Somers static void 6021384bd27SBrian Somers bundle_UnlockTun(struct bundle *bundle) 6031384bd27SBrian Somers { 6041384bd27SBrian Somers char pidfile[MAXPATHLEN]; 6051384bd27SBrian Somers 6061384bd27SBrian Somers snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit); 6071384bd27SBrian Somers ID0unlink(pidfile); 6081384bd27SBrian Somers } 6097a6f8720SBrian Somers 6107a6f8720SBrian Somers struct bundle * 611cf0a3940SBrian Somers bundle_Create(const char *prefix, int type, int unit) 6127a6f8720SBrian Somers { 6137a6f8720SBrian Somers static struct bundle bundle; /* there can be only one */ 614c0593e34SBrian Somers int enoentcount, err, minunit, maxunit; 6154e5196e9SBrian Somers const char *ifname; 616fdb4bb1bSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD) 617fc3034caSBrian Somers int kldtried; 618fc3034caSBrian Somers #endif 6196ca65df0SBrian Somers #if defined(TUNSIFMODE) || defined(TUNSLMODE) 6200f203c7eSBrian Somers int iff; 6210f203c7eSBrian Somers #endif 6227a6f8720SBrian Somers 6238fa6ebe4SBrian Somers if (bundle.iface != NULL) { /* Already allocated ! */ 624a33b2ef7SBrian Somers log_Printf(LogALERT, "bundle_Create: There's only one BUNDLE !\n"); 6257a6f8720SBrian Somers return NULL; 6267a6f8720SBrian Somers } 6277a6f8720SBrian Somers 628c0593e34SBrian Somers if (unit == -1) { 629c0593e34SBrian Somers minunit = 0; 630c0593e34SBrian Somers maxunit = -1; 631c0593e34SBrian Somers } else { 632c0593e34SBrian Somers minunit = unit; 633c0593e34SBrian Somers maxunit = unit + 1; 634c0593e34SBrian Somers } 6357a6f8720SBrian Somers err = ENOENT; 6367a6f8720SBrian Somers enoentcount = 0; 637fdb4bb1bSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD) 638fc3034caSBrian Somers kldtried = 0; 639fc3034caSBrian Somers #endif 640c0593e34SBrian Somers for (bundle.unit = minunit; bundle.unit != maxunit; bundle.unit++) { 641faefde08SBrian Somers snprintf(bundle.dev.Name, sizeof bundle.dev.Name, "%s%d", 642faefde08SBrian Somers prefix, bundle.unit); 643faefde08SBrian Somers bundle.dev.fd = ID0open(bundle.dev.Name, O_RDWR); 644faefde08SBrian Somers if (bundle.dev.fd >= 0) 6457a6f8720SBrian Somers break; 646107d62e7SBrian Somers else if (errno == ENXIO) { 647fdb4bb1bSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD) 648c0593e34SBrian Somers if (bundle.unit == minunit && !kldtried++) { 649fc3034caSBrian Somers /* 650fdb4bb1bSBrian Somers * Attempt to load the tunnel interface KLD if it isn't loaded 651fdb4bb1bSBrian Somers * already. 652fc3034caSBrian Somers */ 653fdb4bb1bSBrian Somers if (modfind("if_tun") == -1) { 654fc3034caSBrian Somers if (ID0kldload("if_tun") != -1) { 655fc3034caSBrian Somers bundle.unit--; 656fc3034caSBrian Somers continue; 657fc3034caSBrian Somers } 658fc3034caSBrian Somers log_Printf(LogWARN, "kldload: if_tun: %s\n", strerror(errno)); 659fc3034caSBrian Somers } 660fc3034caSBrian Somers } 661fc3034caSBrian Somers #endif 6627a6f8720SBrian Somers err = errno; 663107d62e7SBrian Somers break; 6647a6f8720SBrian Somers } else if (errno == ENOENT) { 6657a6f8720SBrian Somers if (++enoentcount > 2) 666107d62e7SBrian Somers break; 6677a6f8720SBrian Somers } else 6687a6f8720SBrian Somers err = errno; 6697a6f8720SBrian Somers } 6707a6f8720SBrian Somers 671faefde08SBrian Somers if (bundle.dev.fd < 0) { 672c0593e34SBrian Somers if (unit == -1) 673c0593e34SBrian Somers log_Printf(LogWARN, "No available tunnel devices found (%s)\n", 67485b542cfSBrian Somers strerror(err)); 675c0593e34SBrian Somers else 676c0593e34SBrian Somers log_Printf(LogWARN, "%s%d: %s\n", prefix, unit, strerror(err)); 6777a6f8720SBrian Somers return NULL; 6787a6f8720SBrian Somers } 6797a6f8720SBrian Somers 680dd7e2610SBrian Somers log_SetTun(bundle.unit); 6817a6f8720SBrian Somers 6828fa6ebe4SBrian Somers ifname = strrchr(bundle.dev.Name, '/'); 6838fa6ebe4SBrian Somers if (ifname == NULL) 6848fa6ebe4SBrian Somers ifname = bundle.dev.Name; 6857a6f8720SBrian Somers else 6868fa6ebe4SBrian Somers ifname++; 6878fa6ebe4SBrian Somers 6888fa6ebe4SBrian Somers bundle.iface = iface_Create(ifname); 6898fa6ebe4SBrian Somers if (bundle.iface == NULL) { 6908fa6ebe4SBrian Somers close(bundle.dev.fd); 6918fa6ebe4SBrian Somers return NULL; 6928fa6ebe4SBrian Somers } 6937a6f8720SBrian Somers 6940f203c7eSBrian Somers #ifdef TUNSIFMODE 6950f203c7eSBrian Somers /* Make sure we're POINTOPOINT */ 6960f203c7eSBrian Somers iff = IFF_POINTOPOINT; 6970f203c7eSBrian Somers if (ID0ioctl(bundle.dev.fd, TUNSIFMODE, &iff) < 0) 6980f203c7eSBrian Somers log_Printf(LogERROR, "bundle_Create: ioctl(TUNSIFMODE): %s\n", 6990f203c7eSBrian Somers strerror(errno)); 7000f203c7eSBrian Somers #endif 7010f203c7eSBrian Somers 7026ca65df0SBrian Somers #ifdef TUNSLMODE 7036ca65df0SBrian Somers /* Make sure we're POINTOPOINT */ 7046ca65df0SBrian Somers iff = 0; 7056ca65df0SBrian Somers if (ID0ioctl(bundle.dev.fd, TUNSLMODE, &iff) < 0) 7066ca65df0SBrian Somers log_Printf(LogERROR, "bundle_Create: ioctl(TUNSLMODE): %s\n", 7076ca65df0SBrian Somers strerror(errno)); 7086ca65df0SBrian Somers #endif 7096ca65df0SBrian Somers 7104e5196e9SBrian Somers if (!iface_SetFlags(bundle.iface, IFF_UP)) { 7118fa6ebe4SBrian Somers iface_Destroy(bundle.iface); 7128fa6ebe4SBrian Somers bundle.iface = NULL; 713faefde08SBrian Somers close(bundle.dev.fd); 7147a6f8720SBrian Somers return NULL; 7157a6f8720SBrian Somers } 7167a6f8720SBrian Somers 7178fa6ebe4SBrian Somers log_Printf(LogPHASE, "Using interface: %s\n", ifname); 7187a6f8720SBrian Somers 719ab2de065SBrian Somers bundle.bandwidth = 0; 720820de6ebSBrian Somers bundle.routing_seq = 0; 721a0cbd833SBrian Somers bundle.phase = PHASE_DEAD; 722a0cbd833SBrian Somers bundle.CleaningUp = 0; 72367b072f7SBrian Somers bundle.NatEnabled = 0; 7247a6f8720SBrian Somers 7256d666775SBrian Somers bundle.fsm.LayerStart = bundle_LayerStart; 7266f384573SBrian Somers bundle.fsm.LayerUp = bundle_LayerUp; 7276d666775SBrian Somers bundle.fsm.LayerDown = bundle_LayerDown; 7286d666775SBrian Somers bundle.fsm.LayerFinish = bundle_LayerFinish; 7296d666775SBrian Somers bundle.fsm.object = &bundle; 7307a6f8720SBrian Somers 731dade2407SBrian Somers bundle.cfg.idle.timeout = NCP_IDLE_TIMEOUT; 732dade2407SBrian Somers bundle.cfg.idle.min_timeout = 0; 7331342caedSBrian Somers *bundle.cfg.auth.name = '\0'; 7341342caedSBrian Somers *bundle.cfg.auth.key = '\0'; 735610b185fSBrian Somers bundle.cfg.opt = OPT_SROUTES | OPT_IDCHECK | OPT_LOOPBACK | 736610b185fSBrian Somers OPT_THROUGHPUT | OPT_UTMP; 73749052c95SBrian Somers *bundle.cfg.label = '\0'; 73849052c95SBrian Somers bundle.cfg.mtu = DEF_MTU; 7396f8e9f0aSBrian Somers bundle.cfg.choked.timeout = CHOKED_TIMEOUT; 740ff0f9439SBrian Somers bundle.phys_type.all = type; 741ff0f9439SBrian Somers bundle.phys_type.open = 0; 742dade2407SBrian Somers bundle.upat = 0; 743ab886ad0SBrian Somers 7446f384573SBrian Somers bundle.links = datalink_Create("deflink", &bundle, type); 7453006ec67SBrian Somers if (bundle.links == NULL) { 746a33b2ef7SBrian Somers log_Printf(LogALERT, "Cannot create data link: %s\n", strerror(errno)); 7478fa6ebe4SBrian Somers iface_Destroy(bundle.iface); 7488fa6ebe4SBrian Somers bundle.iface = NULL; 749faefde08SBrian Somers close(bundle.dev.fd); 7502289f246SBrian Somers return NULL; 7512289f246SBrian Somers } 7522289f246SBrian Somers 7532f786681SBrian Somers bundle.desc.type = BUNDLE_DESCRIPTOR; 7542f786681SBrian Somers bundle.desc.UpdateSet = bundle_UpdateSet; 7552f786681SBrian Somers bundle.desc.IsSet = bundle_IsSet; 7562f786681SBrian Somers bundle.desc.Read = bundle_DescriptorRead; 7572f786681SBrian Somers bundle.desc.Write = bundle_DescriptorWrite; 7582f786681SBrian Somers 75949052c95SBrian Somers mp_Init(&bundle.ncp.mp, &bundle); 76049052c95SBrian Somers 76149052c95SBrian Somers /* Send over the first physical link by default */ 7625828db6dSBrian Somers ipcp_Init(&bundle.ncp.ipcp, &bundle, &bundle.links->physical->link, 7635828db6dSBrian Somers &bundle.fsm); 7646d666775SBrian Somers 7655ca5389aSBrian Somers memset(&bundle.filter, '\0', sizeof bundle.filter); 7665ca5389aSBrian Somers bundle.filter.in.fragok = bundle.filter.in.logok = 1; 7675ca5389aSBrian Somers bundle.filter.in.name = "IN"; 7685ca5389aSBrian Somers bundle.filter.out.fragok = bundle.filter.out.logok = 1; 7695ca5389aSBrian Somers bundle.filter.out.name = "OUT"; 7705ca5389aSBrian Somers bundle.filter.dial.name = "DIAL"; 7718390b576SBrian Somers bundle.filter.dial.logok = 1; 7725ca5389aSBrian Somers bundle.filter.alive.name = "ALIVE"; 7735ca5389aSBrian Somers bundle.filter.alive.logok = 1; 774cad7e742SBrian Somers { 775cad7e742SBrian Somers int i; 776cad7e742SBrian Somers for (i = 0; i < MAXFILTERS; i++) { 777cad7e742SBrian Somers bundle.filter.in.rule[i].f_action = A_NONE; 778cad7e742SBrian Somers bundle.filter.out.rule[i].f_action = A_NONE; 779cad7e742SBrian Somers bundle.filter.dial.rule[i].f_action = A_NONE; 780cad7e742SBrian Somers bundle.filter.alive.rule[i].f_action = A_NONE; 781cad7e742SBrian Somers } 782cad7e742SBrian Somers } 78393ee0ff2SBrian Somers memset(&bundle.idle.timer, '\0', sizeof bundle.idle.timer); 78493ee0ff2SBrian Somers bundle.idle.done = 0; 7855cf4388bSBrian Somers bundle.notify.fd = -1; 7866f8e9f0aSBrian Somers memset(&bundle.choked.timer, '\0', sizeof bundle.choked.timer); 787972a1bcfSBrian Somers #ifndef NORADIUS 788972a1bcfSBrian Somers radius_Init(&bundle.radius); 789972a1bcfSBrian Somers #endif 79093ee0ff2SBrian Somers 7916d666775SBrian Somers /* Clean out any leftover crud */ 7928fa6ebe4SBrian Somers iface_Clear(bundle.iface, IFACE_CLEAR_ALL); 7936d666775SBrian Somers 7941384bd27SBrian Somers bundle_LockTun(&bundle); 7951384bd27SBrian Somers 7967a6f8720SBrian Somers return &bundle; 7977a6f8720SBrian Somers } 7987a6f8720SBrian Somers 79968a0f0ccSBrian Somers static void 80068a0f0ccSBrian Somers bundle_DownInterface(struct bundle *bundle) 80168a0f0ccSBrian Somers { 802dd7e2610SBrian Somers route_IfDelete(bundle, 1); 8034e5196e9SBrian Somers iface_ClearFlags(bundle->iface, IFF_UP); 80468a0f0ccSBrian Somers } 80568a0f0ccSBrian Somers 80668a0f0ccSBrian Somers void 80768a0f0ccSBrian Somers bundle_Destroy(struct bundle *bundle) 80868a0f0ccSBrian Somers { 8093006ec67SBrian Somers struct datalink *dl; 810b6217683SBrian Somers 811ea722969SBrian Somers /* 81204eaa58cSBrian Somers * Clean up the interface. We don't need to timer_Stop()s, mp_Down(), 813ea722969SBrian Somers * ipcp_CleanInterface() and bundle_DownInterface() unless we're getting 814ea722969SBrian Somers * out under exceptional conditions such as a descriptor exception. 815ea722969SBrian Somers */ 81604eaa58cSBrian Somers timer_Stop(&bundle->idle.timer); 8176f8e9f0aSBrian Somers timer_Stop(&bundle->choked.timer); 81866f634b6SBrian Somers mp_Down(&bundle->ncp.mp); 819dd7e2610SBrian Somers ipcp_CleanInterface(&bundle->ncp.ipcp); 82068a0f0ccSBrian Somers bundle_DownInterface(bundle); 8213006ec67SBrian Somers 822972a1bcfSBrian Somers #ifndef NORADIUS 823972a1bcfSBrian Somers /* Tell the radius server the bad news */ 824972a1bcfSBrian Somers radius_Destroy(&bundle->radius); 825972a1bcfSBrian Somers #endif 826972a1bcfSBrian Somers 827ea722969SBrian Somers /* Again, these are all DATALINK_CLOSED unless we're abending */ 8283006ec67SBrian Somers dl = bundle->links; 8293006ec67SBrian Somers while (dl) 8303006ec67SBrian Somers dl = datalink_Destroy(dl); 8313006ec67SBrian Somers 832442f8495SBrian Somers ipcp_Destroy(&bundle->ncp.ipcp); 833442f8495SBrian Somers 8341384bd27SBrian Somers close(bundle->dev.fd); 8351384bd27SBrian Somers bundle_UnlockTun(bundle); 8361384bd27SBrian Somers 837ea722969SBrian Somers /* In case we never made PHASE_NETWORK */ 8385cf4388bSBrian Somers bundle_Notify(bundle, EX_ERRDEAD); 839b6217683SBrian Somers 8408fa6ebe4SBrian Somers iface_Destroy(bundle->iface); 8418fa6ebe4SBrian Somers bundle->iface = NULL; 84268a0f0ccSBrian Somers } 84368a0f0ccSBrian Somers 8447a6f8720SBrian Somers struct rtmsg { 8457a6f8720SBrian Somers struct rt_msghdr m_rtm; 8467a6f8720SBrian Somers char m_space[64]; 8477a6f8720SBrian Somers }; 8487a6f8720SBrian Somers 849610b185fSBrian Somers int 850820de6ebSBrian Somers bundle_SetRoute(struct bundle *bundle, int cmd, struct in_addr dst, 8512062443fSBrian Somers struct in_addr gateway, struct in_addr mask, int bang, int ssh) 8527a6f8720SBrian Somers { 8537a6f8720SBrian Somers struct rtmsg rtmes; 8547a6f8720SBrian Somers int s, nb, wb; 8557a6f8720SBrian Somers char *cp; 8567a6f8720SBrian Somers const char *cmdstr; 8577a6f8720SBrian Somers struct sockaddr_in rtdata; 858610b185fSBrian Somers int result = 1; 8597a6f8720SBrian Somers 8607a6f8720SBrian Somers if (bang) 8617a6f8720SBrian Somers cmdstr = (cmd == RTM_ADD ? "Add!" : "Delete!"); 8627a6f8720SBrian Somers else 8637a6f8720SBrian Somers cmdstr = (cmd == RTM_ADD ? "Add" : "Delete"); 8647a6f8720SBrian Somers s = ID0socket(PF_ROUTE, SOCK_RAW, 0); 8657a6f8720SBrian Somers if (s < 0) { 866dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_SetRoute: socket(): %s\n", strerror(errno)); 867610b185fSBrian Somers return result; 8687a6f8720SBrian Somers } 8697a6f8720SBrian Somers memset(&rtmes, '\0', sizeof rtmes); 8707a6f8720SBrian Somers rtmes.m_rtm.rtm_version = RTM_VERSION; 8717a6f8720SBrian Somers rtmes.m_rtm.rtm_type = cmd; 8727a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs = RTA_DST; 873820de6ebSBrian Somers rtmes.m_rtm.rtm_seq = ++bundle->routing_seq; 8747a6f8720SBrian Somers rtmes.m_rtm.rtm_pid = getpid(); 8757a6f8720SBrian Somers rtmes.m_rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC; 8767a6f8720SBrian Somers 8773afe5ccbSBrian Somers if (cmd == RTM_ADD || cmd == RTM_CHANGE) { 8783afe5ccbSBrian Somers if (bundle->ncp.ipcp.cfg.sendpipe > 0) { 8793afe5ccbSBrian Somers rtmes.m_rtm.rtm_rmx.rmx_sendpipe = bundle->ncp.ipcp.cfg.sendpipe; 8803afe5ccbSBrian Somers rtmes.m_rtm.rtm_inits |= RTV_SPIPE; 8813afe5ccbSBrian Somers } 8823afe5ccbSBrian Somers if (bundle->ncp.ipcp.cfg.recvpipe > 0) { 8833afe5ccbSBrian Somers rtmes.m_rtm.rtm_rmx.rmx_recvpipe = bundle->ncp.ipcp.cfg.recvpipe; 8843afe5ccbSBrian Somers rtmes.m_rtm.rtm_inits |= RTV_RPIPE; 8853afe5ccbSBrian Somers } 8863afe5ccbSBrian Somers } 8873afe5ccbSBrian Somers 8887a6f8720SBrian Somers memset(&rtdata, '\0', sizeof rtdata); 88950e5c17dSBrian Somers rtdata.sin_len = sizeof rtdata; 8907a6f8720SBrian Somers rtdata.sin_family = AF_INET; 8917a6f8720SBrian Somers rtdata.sin_port = 0; 8927a6f8720SBrian Somers rtdata.sin_addr = dst; 8937a6f8720SBrian Somers 8947a6f8720SBrian Somers cp = rtmes.m_space; 89550e5c17dSBrian Somers memcpy(cp, &rtdata, rtdata.sin_len); 89650e5c17dSBrian Somers cp += rtdata.sin_len; 897e43ebac1SBrian Somers if (cmd == RTM_ADD) { 8987a6f8720SBrian Somers if (gateway.s_addr == INADDR_ANY) { 8990aa8aa17SBrian Somers if (!ssh) 9008fa6ebe4SBrian Somers log_Printf(LogERROR, "bundle_SetRoute: Cannot add a route with" 9018fa6ebe4SBrian Somers " destination 0.0.0.0\n"); 9028fa6ebe4SBrian Somers close(s); 9038fa6ebe4SBrian Somers return result; 9047a6f8720SBrian Somers } else { 9057a6f8720SBrian Somers rtdata.sin_addr = gateway; 90650e5c17dSBrian Somers memcpy(cp, &rtdata, rtdata.sin_len); 90750e5c17dSBrian Somers cp += rtdata.sin_len; 9087a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY; 9097a6f8720SBrian Somers } 910e43ebac1SBrian Somers } 9117a6f8720SBrian Somers 9127a6f8720SBrian Somers if (dst.s_addr == INADDR_ANY) 9137a6f8720SBrian Somers mask.s_addr = INADDR_ANY; 9147a6f8720SBrian Somers 9157a6f8720SBrian Somers if (cmd == RTM_ADD || dst.s_addr == INADDR_ANY) { 9167a6f8720SBrian Somers rtdata.sin_addr = mask; 91750e5c17dSBrian Somers memcpy(cp, &rtdata, rtdata.sin_len); 91850e5c17dSBrian Somers cp += rtdata.sin_len; 9197a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs |= RTA_NETMASK; 9207a6f8720SBrian Somers } 9217a6f8720SBrian Somers 9227a6f8720SBrian Somers nb = cp - (char *) &rtmes; 9237a6f8720SBrian Somers rtmes.m_rtm.rtm_msglen = nb; 9247a6f8720SBrian Somers wb = ID0write(s, &rtmes, nb); 9257a6f8720SBrian Somers if (wb < 0) { 926dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute failure:\n"); 927dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Cmd = %s\n", cmdstr); 928dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Dst = %s\n", inet_ntoa(dst)); 929dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Gateway = %s\n", inet_ntoa(gateway)); 930dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Mask = %s\n", inet_ntoa(mask)); 9317a6f8720SBrian Somers failed: 9327a6f8720SBrian Somers if (cmd == RTM_ADD && (rtmes.m_rtm.rtm_errno == EEXIST || 933e43ebac1SBrian Somers (rtmes.m_rtm.rtm_errno == 0 && errno == EEXIST))) { 934610b185fSBrian Somers if (!bang) { 935dd7e2610SBrian Somers log_Printf(LogWARN, "Add route failed: %s already exists\n", 9363afe5ccbSBrian Somers dst.s_addr == 0 ? "default" : inet_ntoa(dst)); 937610b185fSBrian Somers result = 0; /* Don't add to our dynamic list */ 938610b185fSBrian Somers } else { 9397a6f8720SBrian Somers rtmes.m_rtm.rtm_type = cmd = RTM_CHANGE; 9407a6f8720SBrian Somers if ((wb = ID0write(s, &rtmes, nb)) < 0) 9417a6f8720SBrian Somers goto failed; 9427a6f8720SBrian Somers } 943e43ebac1SBrian Somers } else if (cmd == RTM_DELETE && 9447a6f8720SBrian Somers (rtmes.m_rtm.rtm_errno == ESRCH || 9457a6f8720SBrian Somers (rtmes.m_rtm.rtm_errno == 0 && errno == ESRCH))) { 9467a6f8720SBrian Somers if (!bang) 947dd7e2610SBrian Somers log_Printf(LogWARN, "Del route failed: %s: Non-existent\n", 9487a6f8720SBrian Somers inet_ntoa(dst)); 9492062443fSBrian Somers } else if (rtmes.m_rtm.rtm_errno == 0) { 9502062443fSBrian Somers if (!ssh || errno != ENETUNREACH) 951dd7e2610SBrian Somers log_Printf(LogWARN, "%s route failed: %s: errno: %s\n", cmdstr, 9527a6f8720SBrian Somers inet_ntoa(dst), strerror(errno)); 9532062443fSBrian Somers } else 954dd7e2610SBrian Somers log_Printf(LogWARN, "%s route failed: %s: %s\n", 9557a6f8720SBrian Somers cmdstr, inet_ntoa(dst), strerror(rtmes.m_rtm.rtm_errno)); 9567a6f8720SBrian Somers } 957dd7e2610SBrian Somers log_Printf(LogDEBUG, "wrote %d: cmd = %s, dst = %x, gateway = %x\n", 958fe3125a0SBrian Somers wb, cmdstr, (unsigned)dst.s_addr, (unsigned)gateway.s_addr); 9597a6f8720SBrian Somers close(s); 960610b185fSBrian Somers 961610b185fSBrian Somers return result; 9627a6f8720SBrian Somers } 96383d1af55SBrian Somers 96483d1af55SBrian Somers void 9653006ec67SBrian Somers bundle_LinkClosed(struct bundle *bundle, struct datalink *dl) 9663006ec67SBrian Somers { 9673006ec67SBrian Somers /* 9683006ec67SBrian Somers * Our datalink has closed. 969ea722969SBrian Somers * CleanDatalinks() (called from DoLoop()) will remove closed 97081358fa3SBrian Somers * BACKGROUND and DIRECT links. 9713b0f8d2eSBrian Somers * If it's the last data link, enter phase DEAD. 972ea722969SBrian Somers * 973ea722969SBrian Somers * NOTE: dl may not be in our list (bundle_SendDatalink()) ! 9743006ec67SBrian Somers */ 9755b8b8060SBrian Somers 9763b0f8d2eSBrian Somers struct datalink *odl; 9773b0f8d2eSBrian Somers int other_links; 9785b8b8060SBrian Somers 979bf1d3ff6SBrian Somers log_SetTtyCommandMode(dl); 980bf1d3ff6SBrian Somers 9813b0f8d2eSBrian Somers other_links = 0; 9823b0f8d2eSBrian Somers for (odl = bundle->links; odl; odl = odl->next) 9833b0f8d2eSBrian Somers if (odl != dl && odl->state != DATALINK_CLOSED) 9843b0f8d2eSBrian Somers other_links++; 9853b0f8d2eSBrian Somers 9863b0f8d2eSBrian Somers if (!other_links) { 98781358fa3SBrian Somers if (dl->physical->type != PHYS_AUTO) /* Not in -auto mode */ 98803704096SBrian Somers bundle_DownInterface(bundle); 98909206a6fSBrian Somers fsm2initial(&bundle->ncp.ipcp.fsm); 9905563ebdeSBrian Somers bundle_NewPhase(bundle, PHASE_DEAD); 9910f2f3eb3SBrian Somers bundle_StopIdleTimer(bundle); 992ab2de065SBrian Somers } 993455aabc3SBrian Somers } 994455aabc3SBrian Somers 995455aabc3SBrian Somers void 996ba23f397SBrian Somers bundle_Open(struct bundle *bundle, const char *name, int mask, int force) 9973006ec67SBrian Somers { 9983006ec67SBrian Somers /* 9993006ec67SBrian Somers * Please open the given datalink, or all if name == NULL 10003006ec67SBrian Somers */ 10013006ec67SBrian Somers struct datalink *dl; 10023006ec67SBrian Somers 10033006ec67SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 10043006ec67SBrian Somers if (name == NULL || !strcasecmp(dl->name, name)) { 1005ba23f397SBrian Somers if ((mask & dl->physical->type) && 1006ba23f397SBrian Somers (dl->state == DATALINK_CLOSED || 1007ba23f397SBrian Somers (force && dl->state == DATALINK_OPENING && 1008c11e57a3SBrian Somers dl->dial.timer.state == TIMER_RUNNING))) { 1009c11e57a3SBrian Somers if (force) /* Ignore redial timeout ? */ 1010c11e57a3SBrian Somers timer_Stop(&dl->dial.timer); 1011565e35e5SBrian Somers datalink_Up(dl, 1, 1); 1012ab2de065SBrian Somers if (mask & PHYS_AUTO) 1013ab2de065SBrian Somers /* Only one AUTO link at a time */ 101404eaa58cSBrian Somers break; 101504eaa58cSBrian Somers } 10163006ec67SBrian Somers if (name != NULL) 10173006ec67SBrian Somers break; 10183006ec67SBrian Somers } 10193006ec67SBrian Somers } 10203006ec67SBrian Somers 10213006ec67SBrian Somers struct datalink * 10223006ec67SBrian Somers bundle2datalink(struct bundle *bundle, const char *name) 10233006ec67SBrian Somers { 10243006ec67SBrian Somers struct datalink *dl; 10253006ec67SBrian Somers 10263006ec67SBrian Somers if (name != NULL) { 10273006ec67SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 10283006ec67SBrian Somers if (!strcasecmp(dl->name, name)) 10293006ec67SBrian Somers return dl; 10303006ec67SBrian Somers } else if (bundle->links && !bundle->links->next) 10313006ec67SBrian Somers return bundle->links; 10323006ec67SBrian Somers 10333006ec67SBrian Somers return NULL; 10343006ec67SBrian Somers } 10353006ec67SBrian Somers 10363006ec67SBrian Somers int 10373006ec67SBrian Somers bundle_FillQueues(struct bundle *bundle) 10383006ec67SBrian Somers { 10393b0f8d2eSBrian Somers int total; 10403b0f8d2eSBrian Somers 10411bc9b5baSBrian Somers if (bundle->ncp.mp.active) 10423b0f8d2eSBrian Somers total = mp_FillQueues(bundle); 10431bc9b5baSBrian Somers else { 10441bc9b5baSBrian Somers struct datalink *dl; 10451bc9b5baSBrian Somers int add; 10461bc9b5baSBrian Somers 10471bc9b5baSBrian Somers for (total = 0, dl = bundle->links; dl; dl = dl->next) 10481bc9b5baSBrian Somers if (dl->state == DATALINK_OPEN) { 10491bc9b5baSBrian Somers add = link_QueueLen(&dl->physical->link); 10501bc9b5baSBrian Somers if (add == 0 && dl->physical->out == NULL) 10515d9e6103SBrian Somers add = ip_PushPacket(&dl->physical->link, bundle); 10521bc9b5baSBrian Somers total += add; 10531bc9b5baSBrian Somers } 10543006ec67SBrian Somers } 10553006ec67SBrian Somers 10565a72b6edSBrian Somers return total + ip_QueueLen(&bundle->ncp.ipcp); 10573006ec67SBrian Somers } 1058aef795ccSBrian Somers 1059aef795ccSBrian Somers int 1060aef795ccSBrian Somers bundle_ShowLinks(struct cmdargs const *arg) 1061aef795ccSBrian Somers { 1062aef795ccSBrian Somers struct datalink *dl; 1063ab2de065SBrian Somers struct pppThroughput *t; 1064ab2de065SBrian Somers int secs; 1065aef795ccSBrian Somers 10669c53a7b1SBrian Somers for (dl = arg->bundle->links; dl; dl = dl->next) { 1067d4156d00SBrian Somers prompt_Printf(arg->prompt, "Name: %s [%s, %s]", 1068d4156d00SBrian Somers dl->name, mode2Nam(dl->physical->type), datalink_State(dl)); 1069eeab6bf5SBrian Somers if (dl->physical->link.throughput.rolling && dl->state == DATALINK_OPEN) 1070e531f89aSBrian Somers prompt_Printf(arg->prompt, " bandwidth %d, %llu bps (%llu bytes/sec)", 1071ab2de065SBrian Somers dl->mp.bandwidth ? dl->mp.bandwidth : 1072ab2de065SBrian Somers physical_GetSpeed(dl->physical), 1073ab2de065SBrian Somers dl->physical->link.throughput.OctetsPerSecond * 8, 10749c53a7b1SBrian Somers dl->physical->link.throughput.OctetsPerSecond); 10759c53a7b1SBrian Somers prompt_Printf(arg->prompt, "\n"); 10769c53a7b1SBrian Somers } 1077aef795ccSBrian Somers 1078ab2de065SBrian Somers t = &arg->bundle->ncp.mp.link.throughput; 1079ab2de065SBrian Somers secs = t->downtime ? 0 : throughput_uptime(t); 1080ab2de065SBrian Somers if (secs > t->SamplePeriod) 1081ab2de065SBrian Somers secs = t->SamplePeriod; 1082ab2de065SBrian Somers if (secs) 1083e531f89aSBrian Somers prompt_Printf(arg->prompt, "Currently averaging %llu bps (%llu bytes/sec)" 1084ab2de065SBrian Somers " over the last %d secs\n", t->OctetsPerSecond * 8, 1085ab2de065SBrian Somers t->OctetsPerSecond, secs); 1086ab2de065SBrian Somers 1087aef795ccSBrian Somers return 0; 1088aef795ccSBrian Somers } 1089ab886ad0SBrian Somers 10901342caedSBrian Somers static const char * 10911342caedSBrian Somers optval(struct bundle *bundle, int bit) 10921342caedSBrian Somers { 10931342caedSBrian Somers return (bundle->cfg.opt & bit) ? "enabled" : "disabled"; 10941342caedSBrian Somers } 10951342caedSBrian Somers 1096c08717dfSBrian Somers int 1097c08717dfSBrian Somers bundle_ShowStatus(struct cmdargs const *arg) 1098c08717dfSBrian Somers { 1099c08717dfSBrian Somers int remaining; 1100c08717dfSBrian Somers 1101c08717dfSBrian Somers prompt_Printf(arg->prompt, "Phase %s\n", bundle_PhaseName(arg->bundle)); 1102faefde08SBrian Somers prompt_Printf(arg->prompt, " Device: %s\n", arg->bundle->dev.Name); 1103dade2407SBrian Somers prompt_Printf(arg->prompt, " Interface: %s @ %lubps", 1104ab2de065SBrian Somers arg->bundle->iface->name, arg->bundle->bandwidth); 1105c08717dfSBrian Somers 1106dade2407SBrian Somers if (arg->bundle->upat) { 1107dade2407SBrian Somers int secs = time(NULL) - arg->bundle->upat; 1108dade2407SBrian Somers 1109dade2407SBrian Somers prompt_Printf(arg->prompt, ", up time %d:%02d:%02d", secs / 3600, 1110dade2407SBrian Somers (secs / 60) % 60, secs % 60); 1111dade2407SBrian Somers } 1112dade2407SBrian Somers 1113dade2407SBrian Somers prompt_Printf(arg->prompt, "\n\nDefaults:\n"); 1114643f4904SBrian Somers prompt_Printf(arg->prompt, " Label: %s\n", arg->bundle->cfg.label); 1115610b185fSBrian Somers prompt_Printf(arg->prompt, " Auth name: %s\n", 1116610b185fSBrian Somers arg->bundle->cfg.auth.name); 111704eaa58cSBrian Somers 11186f8e9f0aSBrian Somers prompt_Printf(arg->prompt, " Choked Timer: %ds\n", 11196f8e9f0aSBrian Somers arg->bundle->cfg.choked.timeout); 1120972a1bcfSBrian Somers 1121972a1bcfSBrian Somers #ifndef NORADIUS 1122972a1bcfSBrian Somers radius_Show(&arg->bundle->radius, arg->prompt); 1123972a1bcfSBrian Somers #endif 1124972a1bcfSBrian Somers 1125c08717dfSBrian Somers prompt_Printf(arg->prompt, " Idle Timer: "); 1126dade2407SBrian Somers if (arg->bundle->cfg.idle.timeout) { 1127dade2407SBrian Somers prompt_Printf(arg->prompt, "%ds", arg->bundle->cfg.idle.timeout); 1128dade2407SBrian Somers if (arg->bundle->cfg.idle.min_timeout) 1129dade2407SBrian Somers prompt_Printf(arg->prompt, ", min %ds", 1130dade2407SBrian Somers arg->bundle->cfg.idle.min_timeout); 1131c08717dfSBrian Somers remaining = bundle_RemainingIdleTime(arg->bundle); 1132c08717dfSBrian Somers if (remaining != -1) 1133c08717dfSBrian Somers prompt_Printf(arg->prompt, " (%ds remaining)", remaining); 1134c08717dfSBrian Somers prompt_Printf(arg->prompt, "\n"); 1135c08717dfSBrian Somers } else 1136c08717dfSBrian Somers prompt_Printf(arg->prompt, "disabled\n"); 1137ce828a6eSBrian Somers prompt_Printf(arg->prompt, " MTU: "); 1138ce828a6eSBrian Somers if (arg->bundle->cfg.mtu) 1139ce828a6eSBrian Somers prompt_Printf(arg->prompt, "%d\n", arg->bundle->cfg.mtu); 1140ce828a6eSBrian Somers else 1141ce828a6eSBrian Somers prompt_Printf(arg->prompt, "unspecified\n"); 1142c08717dfSBrian Somers 11433afe5ccbSBrian Somers prompt_Printf(arg->prompt, " sendpipe: "); 11443afe5ccbSBrian Somers if (arg->bundle->ncp.ipcp.cfg.sendpipe > 0) 1145ab2de065SBrian Somers prompt_Printf(arg->prompt, "%-20ld", arg->bundle->ncp.ipcp.cfg.sendpipe); 11463afe5ccbSBrian Somers else 1147ab2de065SBrian Somers prompt_Printf(arg->prompt, "unspecified "); 11483afe5ccbSBrian Somers prompt_Printf(arg->prompt, " recvpipe: "); 11493afe5ccbSBrian Somers if (arg->bundle->ncp.ipcp.cfg.recvpipe > 0) 11503afe5ccbSBrian Somers prompt_Printf(arg->prompt, "%ld\n", arg->bundle->ncp.ipcp.cfg.recvpipe); 11513afe5ccbSBrian Somers else 11523afe5ccbSBrian Somers prompt_Printf(arg->prompt, "unspecified\n"); 11533afe5ccbSBrian Somers 1154ab2de065SBrian Somers prompt_Printf(arg->prompt, " Sticky Routes: %-20.20s", 1155610b185fSBrian Somers optval(arg->bundle, OPT_SROUTES)); 11561342caedSBrian Somers prompt_Printf(arg->prompt, " ID check: %s\n", 11571342caedSBrian Somers optval(arg->bundle, OPT_IDCHECK)); 1158ab2de065SBrian Somers prompt_Printf(arg->prompt, " Keep-Session: %-20.20s", 1159ac685e31SBrian Somers optval(arg->bundle, OPT_KEEPSESSION)); 11601342caedSBrian Somers prompt_Printf(arg->prompt, " Loopback: %s\n", 11611342caedSBrian Somers optval(arg->bundle, OPT_LOOPBACK)); 1162ab2de065SBrian Somers prompt_Printf(arg->prompt, " PasswdAuth: %-20.20s", 11631342caedSBrian Somers optval(arg->bundle, OPT_PASSWDAUTH)); 11641342caedSBrian Somers prompt_Printf(arg->prompt, " Proxy: %s\n", 11651342caedSBrian Somers optval(arg->bundle, OPT_PROXY)); 1166ab2de065SBrian Somers prompt_Printf(arg->prompt, " Proxyall: %-20.20s", 11673afe5ccbSBrian Somers optval(arg->bundle, OPT_PROXYALL)); 11681342caedSBrian Somers prompt_Printf(arg->prompt, " Throughput: %s\n", 11691342caedSBrian Somers optval(arg->bundle, OPT_THROUGHPUT)); 1170ab2de065SBrian Somers prompt_Printf(arg->prompt, " Utmp Logging: %-20.20s", 11711342caedSBrian Somers optval(arg->bundle, OPT_UTMP)); 11728fa6ebe4SBrian Somers prompt_Printf(arg->prompt, " Iface-Alias: %s\n", 11738fa6ebe4SBrian Somers optval(arg->bundle, OPT_IFACEALIAS)); 11741342caedSBrian Somers 1175c08717dfSBrian Somers return 0; 1176c08717dfSBrian Somers } 1177c08717dfSBrian Somers 1178ab886ad0SBrian Somers static void 1179ab886ad0SBrian Somers bundle_IdleTimeout(void *v) 1180ab886ad0SBrian Somers { 1181ab886ad0SBrian Somers struct bundle *bundle = (struct bundle *)v; 1182ab886ad0SBrian Somers 1183dd7e2610SBrian Somers log_Printf(LogPHASE, "Idle timer expired.\n"); 118404eaa58cSBrian Somers bundle_StopIdleTimer(bundle); 11859c81b87dSBrian Somers bundle_Close(bundle, NULL, CLOSE_STAYDOWN); 1186ab886ad0SBrian Somers } 1187ab886ad0SBrian Somers 1188ab886ad0SBrian Somers /* 1189ab886ad0SBrian Somers * Start Idle timer. If timeout is reached, we call bundle_Close() to 1190ab886ad0SBrian Somers * close LCP and link. 1191ab886ad0SBrian Somers */ 1192ab886ad0SBrian Somers void 1193ab886ad0SBrian Somers bundle_StartIdleTimer(struct bundle *bundle) 1194ab886ad0SBrian Somers { 1195dd7e2610SBrian Somers timer_Stop(&bundle->idle.timer); 1196ff0f9439SBrian Somers if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) != 1197dade2407SBrian Somers bundle->phys_type.open && bundle->cfg.idle.timeout) { 1198dade2407SBrian Somers int secs; 1199dade2407SBrian Somers 1200dade2407SBrian Somers secs = bundle->cfg.idle.timeout; 1201dade2407SBrian Somers if (bundle->cfg.idle.min_timeout > secs && bundle->upat) { 1202dade2407SBrian Somers int up = time(NULL) - bundle->upat; 1203dade2407SBrian Somers 1204dade2407SBrian Somers if ((long long)bundle->cfg.idle.min_timeout - up > (long long)secs) 1205dade2407SBrian Somers secs = bundle->cfg.idle.min_timeout - up; 1206dade2407SBrian Somers } 120793ee0ff2SBrian Somers bundle->idle.timer.func = bundle_IdleTimeout; 12083b0f8d2eSBrian Somers bundle->idle.timer.name = "idle"; 1209dade2407SBrian Somers bundle->idle.timer.load = secs * SECTICKS; 121093ee0ff2SBrian Somers bundle->idle.timer.arg = bundle; 1211dd7e2610SBrian Somers timer_Start(&bundle->idle.timer); 1212dade2407SBrian Somers bundle->idle.done = time(NULL) + secs; 1213ab886ad0SBrian Somers } 1214ab886ad0SBrian Somers } 1215ab886ad0SBrian Somers 1216ab886ad0SBrian Somers void 1217dade2407SBrian Somers bundle_SetIdleTimer(struct bundle *bundle, int timeout, int min_timeout) 1218ab886ad0SBrian Somers { 1219dade2407SBrian Somers bundle->cfg.idle.timeout = timeout; 1220dade2407SBrian Somers if (min_timeout >= 0) 1221dade2407SBrian Somers bundle->cfg.idle.min_timeout = min_timeout; 1222ab886ad0SBrian Somers if (bundle_LinkIsUp(bundle)) 1223ab886ad0SBrian Somers bundle_StartIdleTimer(bundle); 1224ab886ad0SBrian Somers } 1225ab886ad0SBrian Somers 1226ab886ad0SBrian Somers void 1227ab886ad0SBrian Somers bundle_StopIdleTimer(struct bundle *bundle) 1228ab886ad0SBrian Somers { 1229dd7e2610SBrian Somers timer_Stop(&bundle->idle.timer); 12304a632c80SBrian Somers bundle->idle.done = 0; 1231ab886ad0SBrian Somers } 1232ab886ad0SBrian Somers 123304eaa58cSBrian Somers static int 1234ab886ad0SBrian Somers bundle_RemainingIdleTime(struct bundle *bundle) 1235ab886ad0SBrian Somers { 123693ee0ff2SBrian Somers if (bundle->idle.done) 123793ee0ff2SBrian Somers return bundle->idle.done - time(NULL); 1238ab886ad0SBrian Somers return -1; 1239ab886ad0SBrian Somers } 12403b0f8d2eSBrian Somers 12413b0f8d2eSBrian Somers int 12423b0f8d2eSBrian Somers bundle_IsDead(struct bundle *bundle) 12433b0f8d2eSBrian Somers { 12443b0f8d2eSBrian Somers return !bundle->links || (bundle->phase == PHASE_DEAD && bundle->CleaningUp); 12453b0f8d2eSBrian Somers } 1246b6217683SBrian Somers 124704eaa58cSBrian Somers static struct datalink * 124804eaa58cSBrian Somers bundle_DatalinkLinkout(struct bundle *bundle, struct datalink *dl) 1249cd7bd93aSBrian Somers { 1250cd7bd93aSBrian Somers struct datalink **dlp; 1251cd7bd93aSBrian Somers 1252cd7bd93aSBrian Somers for (dlp = &bundle->links; *dlp; dlp = &(*dlp)->next) 1253cd7bd93aSBrian Somers if (*dlp == dl) { 125404eaa58cSBrian Somers *dlp = dl->next; 125504eaa58cSBrian Somers dl->next = NULL; 125604eaa58cSBrian Somers bundle_LinksRemoved(bundle); 125704eaa58cSBrian Somers return dl; 1258cd7bd93aSBrian Somers } 125904eaa58cSBrian Somers 126004eaa58cSBrian Somers return NULL; 126104eaa58cSBrian Somers } 126204eaa58cSBrian Somers 126304eaa58cSBrian Somers static void 126404eaa58cSBrian Somers bundle_DatalinkLinkin(struct bundle *bundle, struct datalink *dl) 126504eaa58cSBrian Somers { 126604eaa58cSBrian Somers struct datalink **dlp = &bundle->links; 126704eaa58cSBrian Somers 126804eaa58cSBrian Somers while (*dlp) 126904eaa58cSBrian Somers dlp = &(*dlp)->next; 127004eaa58cSBrian Somers 127104eaa58cSBrian Somers *dlp = dl; 127204eaa58cSBrian Somers dl->next = NULL; 127304eaa58cSBrian Somers 127404eaa58cSBrian Somers bundle_LinkAdded(bundle, dl); 1275ab2de065SBrian Somers mp_CheckAutoloadTimer(&bundle->ncp.mp); 1276565e35e5SBrian Somers } 1277565e35e5SBrian Somers 1278565e35e5SBrian Somers void 1279565e35e5SBrian Somers bundle_CleanDatalinks(struct bundle *bundle) 1280565e35e5SBrian Somers { 1281565e35e5SBrian Somers struct datalink **dlp = &bundle->links; 128204eaa58cSBrian Somers int found = 0; 1283565e35e5SBrian Somers 1284565e35e5SBrian Somers while (*dlp) 1285565e35e5SBrian Somers if ((*dlp)->state == DATALINK_CLOSED && 128681358fa3SBrian Somers (*dlp)->physical->type & (PHYS_DIRECT|PHYS_BACKGROUND)) { 1287565e35e5SBrian Somers *dlp = datalink_Destroy(*dlp); 128804eaa58cSBrian Somers found++; 128904eaa58cSBrian Somers } else 1290565e35e5SBrian Somers dlp = &(*dlp)->next; 129104eaa58cSBrian Somers 129204eaa58cSBrian Somers if (found) 129304eaa58cSBrian Somers bundle_LinksRemoved(bundle); 129404eaa58cSBrian Somers } 129504eaa58cSBrian Somers 129604eaa58cSBrian Somers int 129704eaa58cSBrian Somers bundle_DatalinkClone(struct bundle *bundle, struct datalink *dl, 129804eaa58cSBrian Somers const char *name) 129904eaa58cSBrian Somers { 130004eaa58cSBrian Somers if (bundle2datalink(bundle, name)) { 130104eaa58cSBrian Somers log_Printf(LogWARN, "Clone: %s: name already exists\n", name); 130204eaa58cSBrian Somers return 0; 130304eaa58cSBrian Somers } 130404eaa58cSBrian Somers 130504eaa58cSBrian Somers bundle_DatalinkLinkin(bundle, datalink_Clone(dl, name)); 130604eaa58cSBrian Somers return 1; 130704eaa58cSBrian Somers } 130804eaa58cSBrian Somers 130904eaa58cSBrian Somers void 131004eaa58cSBrian Somers bundle_DatalinkRemove(struct bundle *bundle, struct datalink *dl) 131104eaa58cSBrian Somers { 131204eaa58cSBrian Somers dl = bundle_DatalinkLinkout(bundle, dl); 131304eaa58cSBrian Somers if (dl) 131404eaa58cSBrian Somers datalink_Destroy(dl); 1315cd7bd93aSBrian Somers } 131649052c95SBrian Somers 131749052c95SBrian Somers void 131849052c95SBrian Somers bundle_SetLabel(struct bundle *bundle, const char *label) 131949052c95SBrian Somers { 132049052c95SBrian Somers if (label) 132149052c95SBrian Somers strncpy(bundle->cfg.label, label, sizeof bundle->cfg.label - 1); 132249052c95SBrian Somers else 132349052c95SBrian Somers *bundle->cfg.label = '\0'; 132449052c95SBrian Somers } 132549052c95SBrian Somers 132649052c95SBrian Somers const char * 132749052c95SBrian Somers bundle_GetLabel(struct bundle *bundle) 132849052c95SBrian Somers { 132949052c95SBrian Somers return *bundle->cfg.label ? bundle->cfg.label : NULL; 133049052c95SBrian Somers } 13311fa665f5SBrian Somers 13321fa665f5SBrian Somers void 133396c9bb21SBrian Somers bundle_ReceiveDatalink(struct bundle *bundle, int s, struct sockaddr_un *sun) 13341fa665f5SBrian Somers { 133587c3786eSBrian Somers char cmsgbuf[(sizeof(struct cmsghdr) + sizeof(int)) * SEND_MAXFD]; 133687c3786eSBrian Somers struct cmsghdr *cmsg; 133796c9bb21SBrian Somers struct msghdr msg; 133896c9bb21SBrian Somers struct iovec iov[SCATTER_SEGMENTS]; 1339b7c5748eSBrian Somers struct datalink *dl; 134087c3786eSBrian Somers int niov, expect, f, fd[SEND_MAXFD], nfd, onfd; 134185fd273aSBrian Somers pid_t pid; 13426f384573SBrian Somers 1343dd7e2610SBrian Somers log_Printf(LogPHASE, "Receiving datalink\n"); 13446f384573SBrian Somers 134596c9bb21SBrian Somers /* Create our scatter/gather array */ 134696c9bb21SBrian Somers niov = 1; 134787c3786eSBrian Somers 134896c9bb21SBrian Somers iov[0].iov_len = strlen(Version) + 1; 134996c9bb21SBrian Somers iov[0].iov_base = (char *)malloc(iov[0].iov_len); 135087c3786eSBrian Somers if (datalink2iov(NULL, iov, &niov, sizeof iov / sizeof *iov, 135187c3786eSBrian Somers NULL, NULL, 0) == -1) { 135254cd8e13SBrian Somers close(s); 13536f384573SBrian Somers return; 135454cd8e13SBrian Somers } 13556f384573SBrian Somers 135685fd273aSBrian Somers pid = getpid(); 135785fd273aSBrian Somers write(s, &pid, sizeof pid); 135885fd273aSBrian Somers 135996c9bb21SBrian Somers for (f = expect = 0; f < niov; f++) 136096c9bb21SBrian Somers expect += iov[f].iov_len; 136196c9bb21SBrian Somers 136296c9bb21SBrian Somers /* Set up our message */ 136387c3786eSBrian Somers for (f = 0; f < SEND_MAXFD; f++) { 136487c3786eSBrian Somers cmsg = (struct cmsghdr *)(cmsgbuf + f * sizeof(struct cmsghdr)); 136587c3786eSBrian Somers cmsg->cmsg_len = sizeof *cmsg + sizeof(int); 136696c9bb21SBrian Somers cmsg->cmsg_level = SOL_SOCKET; 136754cd8e13SBrian Somers cmsg->cmsg_type = 0; 136887c3786eSBrian Somers } 136996c9bb21SBrian Somers 137096c9bb21SBrian Somers memset(&msg, '\0', sizeof msg); 137196c9bb21SBrian Somers msg.msg_name = (caddr_t)sun; 137296c9bb21SBrian Somers msg.msg_namelen = sizeof *sun; 137396c9bb21SBrian Somers msg.msg_iov = iov; 137496c9bb21SBrian Somers msg.msg_iovlen = niov; 137596c9bb21SBrian Somers msg.msg_control = cmsgbuf; 137696c9bb21SBrian Somers msg.msg_controllen = sizeof cmsgbuf; 137796c9bb21SBrian Somers 137896c9bb21SBrian Somers log_Printf(LogDEBUG, "Expecting %d scatter/gather bytes\n", expect); 137996c9bb21SBrian Somers f = expect + 100; 138096c9bb21SBrian Somers setsockopt(s, SOL_SOCKET, SO_RCVBUF, &f, sizeof f); 138196c9bb21SBrian Somers if ((f = recvmsg(s, &msg, MSG_WAITALL)) != expect) { 138296c9bb21SBrian Somers if (f == -1) 138396c9bb21SBrian Somers log_Printf(LogERROR, "Failed recvmsg: %s\n", strerror(errno)); 138496c9bb21SBrian Somers else 138596c9bb21SBrian Somers log_Printf(LogERROR, "Failed recvmsg: Got %d, not %d\n", f, expect); 138696c9bb21SBrian Somers while (niov--) 138796c9bb21SBrian Somers free(iov[niov].iov_base); 138854cd8e13SBrian Somers close(s); 138996c9bb21SBrian Somers return; 139096c9bb21SBrian Somers } 139196c9bb21SBrian Somers 1392c0d9a877SBrian Somers write(s, "!", 1); /* ACK */ 1393ad5b0e8bSBrian Somers close(s); 1394c0d9a877SBrian Somers 139587c3786eSBrian Somers for (nfd = 0; nfd < SEND_MAXFD; nfd++) { 139687c3786eSBrian Somers cmsg = (struct cmsghdr *)(cmsgbuf + nfd * sizeof(struct cmsghdr)); 139787c3786eSBrian Somers if (cmsg->cmsg_len == sizeof *cmsg + sizeof(int) && 139887c3786eSBrian Somers cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) 139987c3786eSBrian Somers fd[nfd] = *(int *)CMSG_DATA(cmsg); 140087c3786eSBrian Somers else 140187c3786eSBrian Somers break; 140287c3786eSBrian Somers } 140387c3786eSBrian Somers 140487c3786eSBrian Somers if (nfd == 0) { 140587c3786eSBrian Somers log_Printf(LogERROR, "Recvmsg: no descriptors received !\n"); 1406ad5b0e8bSBrian Somers while (niov--) 1407ad5b0e8bSBrian Somers free(iov[niov].iov_base); 1408ad5b0e8bSBrian Somers return; 140954cd8e13SBrian Somers } 141054cd8e13SBrian Somers 141187c3786eSBrian Somers /* 141287c3786eSBrian Somers * We've successfully received one or more open file descriptors 141387c3786eSBrian Somers * through our socket 141487c3786eSBrian Somers */ 1415ad5b0e8bSBrian Somers log_Printf(LogDEBUG, "Receiving device descriptor\n"); 141687c3786eSBrian Somers 141787c3786eSBrian Somers nfd--; /* Don't include p->fd */ 1418ad5b0e8bSBrian Somers 141996c9bb21SBrian Somers if (strncmp(Version, iov[0].iov_base, iov[0].iov_len)) { 142096c9bb21SBrian Somers log_Printf(LogWARN, "Cannot receive datalink, incorrect version" 142196c9bb21SBrian Somers " (\"%.*s\", not \"%s\")\n", (int)iov[0].iov_len, 14227d81ddf5SBrian Somers (char *)iov[0].iov_base, Version); 142387c3786eSBrian Somers while (nfd) 142487c3786eSBrian Somers close(fd[nfd--]); 142587c3786eSBrian Somers close(fd[0]); 142696c9bb21SBrian Somers while (niov--) 142796c9bb21SBrian Somers free(iov[niov].iov_base); 142896c9bb21SBrian Somers return; 142996c9bb21SBrian Somers } 143096c9bb21SBrian Somers 143196c9bb21SBrian Somers niov = 1; 143287c3786eSBrian Somers onfd = nfd; 143387c3786eSBrian Somers dl = iov2datalink(bundle, iov, &niov, sizeof iov / sizeof *iov, fd[0], 143487c3786eSBrian Somers fd + 1, &nfd); 1435b7c5748eSBrian Somers if (dl) { 143687c3786eSBrian Somers if (nfd) { 143787c3786eSBrian Somers log_Printf(LogERROR, "bundle_ReceiveDatalink: Failed to handle %d " 143887c3786eSBrian Somers "auxiliary file descriptors\n", nfd); 143987c3786eSBrian Somers datalink_Destroy(dl); 144087c3786eSBrian Somers while (nfd--) 144187c3786eSBrian Somers close(fd[onfd--]); 144287c3786eSBrian Somers } else { 144304eaa58cSBrian Somers bundle_DatalinkLinkin(bundle, dl); 1444b7c5748eSBrian Somers datalink_AuthOk(dl); 1445ab2de065SBrian Somers bundle_CalculateBandwidth(dl->bundle); 144687c3786eSBrian Somers } 144787c3786eSBrian Somers } else { 144887c3786eSBrian Somers while (nfd--) 144987c3786eSBrian Somers close(fd[onfd--]); 145087c3786eSBrian Somers close(fd[0]); 145187c3786eSBrian Somers } 145296c9bb21SBrian Somers 145396c9bb21SBrian Somers free(iov[0].iov_base); 14541fa665f5SBrian Somers } 14551fa665f5SBrian Somers 14561fa665f5SBrian Somers void 145796c9bb21SBrian Somers bundle_SendDatalink(struct datalink *dl, int s, struct sockaddr_un *sun) 14581fa665f5SBrian Somers { 145987c3786eSBrian Somers char cmsgbuf[(sizeof(struct cmsghdr) + sizeof(int)) * SEND_MAXFD], ack; 146087c3786eSBrian Somers struct cmsghdr *cmsg; 146196c9bb21SBrian Somers struct msghdr msg; 146296c9bb21SBrian Somers struct iovec iov[SCATTER_SEGMENTS]; 146387c3786eSBrian Somers int niov, f, expect, newsid, fd[SEND_MAXFD], nfd; 146485fd273aSBrian Somers pid_t newpid; 14656f384573SBrian Somers 1466dd7e2610SBrian Somers log_Printf(LogPHASE, "Transmitting datalink %s\n", dl->name); 14676f384573SBrian Somers 146804eaa58cSBrian Somers bundle_LinkClosed(dl->bundle, dl); 14690f2f3eb3SBrian Somers bundle_DatalinkLinkout(dl->bundle, dl); 1470ea722969SBrian Somers 147196c9bb21SBrian Somers /* Build our scatter/gather array */ 147296c9bb21SBrian Somers iov[0].iov_len = strlen(Version) + 1; 147396c9bb21SBrian Somers iov[0].iov_base = strdup(Version); 147496c9bb21SBrian Somers niov = 1; 147587c3786eSBrian Somers nfd = 0; 14766f384573SBrian Somers 147785fd273aSBrian Somers read(s, &newpid, sizeof newpid); 147887c3786eSBrian Somers fd[0] = datalink2iov(dl, iov, &niov, sizeof iov / sizeof *iov, 147987c3786eSBrian Somers fd + 1, &nfd, newpid); 14806f384573SBrian Somers 148187c3786eSBrian Somers if (fd[0] != -1) { 148287c3786eSBrian Somers nfd++; /* Include fd[0] */ 148387c3786eSBrian Somers 148496c9bb21SBrian Somers memset(&msg, '\0', sizeof msg); 148554cd8e13SBrian Somers 148696c9bb21SBrian Somers msg.msg_name = (caddr_t)sun; 148796c9bb21SBrian Somers msg.msg_namelen = sizeof *sun; 148896c9bb21SBrian Somers msg.msg_iov = iov; 148996c9bb21SBrian Somers msg.msg_iovlen = niov; 149054cd8e13SBrian Somers 149187c3786eSBrian Somers for (f = 0; f < nfd; f++) { 149287c3786eSBrian Somers cmsg = (struct cmsghdr *)(cmsgbuf + f * sizeof(struct cmsghdr)); 149387c3786eSBrian Somers cmsg->cmsg_len = sizeof *cmsg + sizeof(int); 149454cd8e13SBrian Somers cmsg->cmsg_level = SOL_SOCKET; 149554cd8e13SBrian Somers cmsg->cmsg_type = SCM_RIGHTS; 149687c3786eSBrian Somers *(int *)CMSG_DATA(cmsg) = fd[f]; 149787c3786eSBrian Somers } 149887c3786eSBrian Somers 149996c9bb21SBrian Somers msg.msg_control = cmsgbuf; 150087c3786eSBrian Somers msg.msg_controllen = (sizeof *cmsg + sizeof(int)) * nfd; 150147723d29SBrian Somers 150296c9bb21SBrian Somers for (f = expect = 0; f < niov; f++) 150396c9bb21SBrian Somers expect += iov[f].iov_len; 150447723d29SBrian Somers 150587c3786eSBrian Somers log_Printf(LogDEBUG, "Sending %d descriptor%s and %d bytes in scatter" 150687c3786eSBrian Somers "/gather array\n", nfd, nfd == 1 ? "" : "s", expect); 150747723d29SBrian Somers 150896c9bb21SBrian Somers f = expect + SOCKET_OVERHEAD; 150996c9bb21SBrian Somers setsockopt(s, SOL_SOCKET, SO_SNDBUF, &f, sizeof f); 151096c9bb21SBrian Somers if (sendmsg(s, &msg, 0) == -1) 151196c9bb21SBrian Somers log_Printf(LogERROR, "Failed sendmsg: %s\n", strerror(errno)); 1512c0d9a877SBrian Somers /* We must get the ACK before closing the descriptor ! */ 1513c0d9a877SBrian Somers read(s, &ack, 1); 151454cd8e13SBrian Somers 1515ac685e31SBrian Somers newsid = Enabled(dl->bundle, OPT_KEEPSESSION) || 151687c3786eSBrian Somers tcgetpgrp(fd[0]) == getpgrp(); 151787c3786eSBrian Somers while (nfd) 151887c3786eSBrian Somers close(fd[--nfd]); 15191384bd27SBrian Somers if (newsid) 15201384bd27SBrian Somers bundle_setsid(dl->bundle, 1); 152147723d29SBrian Somers } 152285fd273aSBrian Somers close(s); 152396c9bb21SBrian Somers 152496c9bb21SBrian Somers while (niov--) 152596c9bb21SBrian Somers free(iov[niov].iov_base); 15261fa665f5SBrian Somers } 1527dd0645c5SBrian Somers 1528dd0645c5SBrian Somers int 152958d55334SBrian Somers bundle_RenameDatalink(struct bundle *bundle, struct datalink *ndl, 153058d55334SBrian Somers const char *name) 153158d55334SBrian Somers { 153258d55334SBrian Somers struct datalink *dl; 153358d55334SBrian Somers 153458d55334SBrian Somers if (!strcasecmp(ndl->name, name)) 153558d55334SBrian Somers return 1; 153658d55334SBrian Somers 153758d55334SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 153858d55334SBrian Somers if (!strcasecmp(dl->name, name)) 153958d55334SBrian Somers return 0; 154058d55334SBrian Somers 154158d55334SBrian Somers datalink_Rename(ndl, name); 154258d55334SBrian Somers return 1; 154358d55334SBrian Somers } 154458d55334SBrian Somers 154558d55334SBrian Somers int 1546dd0645c5SBrian Somers bundle_SetMode(struct bundle *bundle, struct datalink *dl, int mode) 1547dd0645c5SBrian Somers { 1548dd0645c5SBrian Somers int omode; 1549dd0645c5SBrian Somers 1550dd0645c5SBrian Somers omode = dl->physical->type; 1551dd0645c5SBrian Somers if (omode == mode) 1552dd0645c5SBrian Somers return 1; 1553dd0645c5SBrian Somers 1554ff0f9439SBrian Somers if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO)) 1555ff0f9439SBrian Somers /* First auto link */ 1556dd0645c5SBrian Somers if (bundle->ncp.ipcp.peer_ip.s_addr == INADDR_ANY) { 1557ff0f9439SBrian Somers log_Printf(LogWARN, "You must `set ifaddr' or `open' before" 1558ff0f9439SBrian Somers " changing mode to %s\n", mode2Nam(mode)); 1559dd0645c5SBrian Somers return 0; 1560dd0645c5SBrian Somers } 1561dd0645c5SBrian Somers 1562dd0645c5SBrian Somers if (!datalink_SetMode(dl, mode)) 1563dd0645c5SBrian Somers return 0; 1564dd0645c5SBrian Somers 1565ff0f9439SBrian Somers if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO) && 1566ff0f9439SBrian Somers bundle->phase != PHASE_NETWORK) 1567ff0f9439SBrian Somers /* First auto link, we need an interface */ 1568dd0645c5SBrian Somers ipcp_InterfaceUp(&bundle->ncp.ipcp); 1569dd0645c5SBrian Somers 1570ab2de065SBrian Somers /* Regenerate phys_type and adjust idle timer */ 157104eaa58cSBrian Somers bundle_LinksRemoved(bundle); 1572dd0645c5SBrian Somers 1573dd0645c5SBrian Somers return 1; 1574dd0645c5SBrian Somers } 15751384bd27SBrian Somers 15761384bd27SBrian Somers void 15771384bd27SBrian Somers bundle_setsid(struct bundle *bundle, int holdsession) 15781384bd27SBrian Somers { 15791384bd27SBrian Somers /* 15801384bd27SBrian Somers * Lose the current session. This means getting rid of our pid 15811384bd27SBrian Somers * too so that the tty device will really go away, and any getty 15821384bd27SBrian Somers * etc will be allowed to restart. 15831384bd27SBrian Somers */ 15841384bd27SBrian Somers pid_t pid, orig; 15851384bd27SBrian Somers int fds[2]; 15861384bd27SBrian Somers char done; 15871384bd27SBrian Somers struct datalink *dl; 15881384bd27SBrian Somers 15891384bd27SBrian Somers orig = getpid(); 15901384bd27SBrian Somers if (pipe(fds) == -1) { 15911384bd27SBrian Somers log_Printf(LogERROR, "pipe: %s\n", strerror(errno)); 15921384bd27SBrian Somers return; 15931384bd27SBrian Somers } 15941384bd27SBrian Somers switch ((pid = fork())) { 15951384bd27SBrian Somers case -1: 15961384bd27SBrian Somers log_Printf(LogERROR, "fork: %s\n", strerror(errno)); 15971384bd27SBrian Somers close(fds[0]); 15981384bd27SBrian Somers close(fds[1]); 15991384bd27SBrian Somers return; 16001384bd27SBrian Somers case 0: 16011384bd27SBrian Somers close(fds[1]); 16024be0e57dSBrian Somers read(fds[0], &done, 1); /* uu_locks are mine ! */ 16034be0e57dSBrian Somers close(fds[0]); 16041384bd27SBrian Somers if (pipe(fds) == -1) { 16051384bd27SBrian Somers log_Printf(LogERROR, "pipe(2): %s\n", strerror(errno)); 16061384bd27SBrian Somers return; 16071384bd27SBrian Somers } 16081384bd27SBrian Somers switch ((pid = fork())) { 16091384bd27SBrian Somers case -1: 1610a33b2ef7SBrian Somers log_Printf(LogERROR, "fork(2): %s\n", strerror(errno)); 16111384bd27SBrian Somers close(fds[0]); 16121384bd27SBrian Somers close(fds[1]); 16131384bd27SBrian Somers return; 16141384bd27SBrian Somers case 0: 16151384bd27SBrian Somers close(fds[1]); 16164be0e57dSBrian Somers bundle_LockTun(bundle); /* update pid */ 16174be0e57dSBrian Somers read(fds[0], &done, 1); /* uu_locks are mine ! */ 16184be0e57dSBrian Somers close(fds[0]); 16191384bd27SBrian Somers setsid(); 16201384bd27SBrian Somers log_Printf(LogPHASE, "%d -> %d: %s session control\n", 16211384bd27SBrian Somers (int)orig, (int)getpid(), 16221384bd27SBrian Somers holdsession ? "Passed" : "Dropped"); 16237e778f13SBrian Somers timer_InitService(0); /* Start the Timer Service */ 16241384bd27SBrian Somers break; 16251384bd27SBrian Somers default: 16264be0e57dSBrian Somers close(fds[0]); 16275d9e6103SBrian Somers /* Give away all our physical locks (to the final process) */ 16281384bd27SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 16291384bd27SBrian Somers if (dl->state != DATALINK_CLOSED) 16305d9e6103SBrian Somers physical_ChangedPid(dl->physical, pid); 16314be0e57dSBrian Somers write(fds[1], "!", 1); /* done */ 16324be0e57dSBrian Somers close(fds[1]); 16331384bd27SBrian Somers exit(0); 16341384bd27SBrian Somers break; 16351384bd27SBrian Somers } 16361384bd27SBrian Somers break; 16371384bd27SBrian Somers default: 16384be0e57dSBrian Somers close(fds[0]); 16395d9e6103SBrian Somers /* Give away all our physical locks (to the intermediate process) */ 16401384bd27SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 16411384bd27SBrian Somers if (dl->state != DATALINK_CLOSED) 16425d9e6103SBrian Somers physical_ChangedPid(dl->physical, pid); 16434be0e57dSBrian Somers write(fds[1], "!", 1); /* done */ 16444be0e57dSBrian Somers close(fds[1]); 16451384bd27SBrian Somers if (holdsession) { 16461384bd27SBrian Somers int fd, status; 16471384bd27SBrian Somers 16481384bd27SBrian Somers timer_TermService(); 16491384bd27SBrian Somers signal(SIGPIPE, SIG_DFL); 16501384bd27SBrian Somers signal(SIGALRM, SIG_DFL); 16511384bd27SBrian Somers signal(SIGHUP, SIG_DFL); 16521384bd27SBrian Somers signal(SIGTERM, SIG_DFL); 16531384bd27SBrian Somers signal(SIGINT, SIG_DFL); 16541384bd27SBrian Somers signal(SIGQUIT, SIG_DFL); 16551384bd27SBrian Somers for (fd = getdtablesize(); fd >= 0; fd--) 16561384bd27SBrian Somers close(fd); 16571384bd27SBrian Somers setuid(geteuid()); 16581384bd27SBrian Somers /* 16591384bd27SBrian Somers * Reap the intermediate process. As we're not exiting but the 16601384bd27SBrian Somers * intermediate is, we don't want it to become defunct. 16611384bd27SBrian Somers */ 16621384bd27SBrian Somers waitpid(pid, &status, 0); 16638e7b8599SBrian Somers /* Tweak our process arguments.... */ 1664cf0a3940SBrian Somers ID0setproctitle("session owner"); 16651384bd27SBrian Somers /* 16661384bd27SBrian Somers * Hang around for a HUP. This should happen as soon as the 16671384bd27SBrian Somers * ppp that we passed our ctty descriptor to closes it. 16681384bd27SBrian Somers * NOTE: If this process dies, the passed descriptor becomes 16691384bd27SBrian Somers * invalid and will give a select() error by setting one 16701384bd27SBrian Somers * of the error fds, aborting the other ppp. We don't 16711384bd27SBrian Somers * want that to happen ! 16721384bd27SBrian Somers */ 16731384bd27SBrian Somers pause(); 16741384bd27SBrian Somers } 16751384bd27SBrian Somers exit(0); 16761384bd27SBrian Somers break; 16771384bd27SBrian Somers } 16781384bd27SBrian Somers } 16799b5f8ffdSBrian Somers 16809b5f8ffdSBrian Somers int 16819b5f8ffdSBrian Somers bundle_HighestState(struct bundle *bundle) 16829b5f8ffdSBrian Somers { 16839b5f8ffdSBrian Somers struct datalink *dl; 16849b5f8ffdSBrian Somers int result = DATALINK_CLOSED; 16859b5f8ffdSBrian Somers 16869b5f8ffdSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 16879b5f8ffdSBrian Somers if (result < dl->state) 16889b5f8ffdSBrian Somers result = dl->state; 16899b5f8ffdSBrian Somers 16909b5f8ffdSBrian Somers return result; 16919b5f8ffdSBrian Somers } 1692991c2a7bSBrian Somers 1693991c2a7bSBrian Somers int 1694991c2a7bSBrian Somers bundle_Exception(struct bundle *bundle, int fd) 1695991c2a7bSBrian Somers { 1696991c2a7bSBrian Somers struct datalink *dl; 1697991c2a7bSBrian Somers 1698991c2a7bSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 1699991c2a7bSBrian Somers if (dl->physical->fd == fd) { 1700991c2a7bSBrian Somers datalink_Down(dl, CLOSE_NORMAL); 1701991c2a7bSBrian Somers return 1; 1702991c2a7bSBrian Somers } 1703991c2a7bSBrian Somers 1704991c2a7bSBrian Somers return 0; 1705991c2a7bSBrian Somers } 17061d1fc017SBrian Somers 17071d1fc017SBrian Somers void 17081d1fc017SBrian Somers bundle_AdjustFilters(struct bundle *bundle, struct in_addr *my_ip, 17091d1fc017SBrian Somers struct in_addr *peer_ip) 17101d1fc017SBrian Somers { 17111d1fc017SBrian Somers filter_AdjustAddr(&bundle->filter.in, my_ip, peer_ip); 17121d1fc017SBrian Somers filter_AdjustAddr(&bundle->filter.out, my_ip, peer_ip); 17131d1fc017SBrian Somers filter_AdjustAddr(&bundle->filter.dial, my_ip, peer_ip); 17141d1fc017SBrian Somers filter_AdjustAddr(&bundle->filter.alive, my_ip, peer_ip); 17151d1fc017SBrian Somers } 1716ab2de065SBrian Somers 1717ab2de065SBrian Somers void 1718ab2de065SBrian Somers bundle_CalculateBandwidth(struct bundle *bundle) 1719ab2de065SBrian Somers { 1720ab2de065SBrian Somers struct datalink *dl; 1721ab2de065SBrian Somers int mtu, sp; 1722ab2de065SBrian Somers 1723ab2de065SBrian Somers bundle->bandwidth = 0; 1724ab2de065SBrian Somers mtu = 0; 1725ab2de065SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 1726ab2de065SBrian Somers if (dl->state == DATALINK_OPEN) { 1727ab2de065SBrian Somers if ((sp = dl->mp.bandwidth) == 0 && 1728ab2de065SBrian Somers (sp = physical_GetSpeed(dl->physical)) == 0) 1729ab2de065SBrian Somers log_Printf(LogDEBUG, "%s: %s: Cannot determine bandwidth\n", 1730ab2de065SBrian Somers dl->name, dl->physical->name.full); 1731ab2de065SBrian Somers else 1732ab2de065SBrian Somers bundle->bandwidth += sp; 1733ab2de065SBrian Somers if (!bundle->ncp.mp.active) { 1734ab2de065SBrian Somers mtu = dl->physical->link.lcp.his_mru; 1735ab2de065SBrian Somers break; 1736ab2de065SBrian Somers } 1737ab2de065SBrian Somers } 1738ab2de065SBrian Somers 1739ab2de065SBrian Somers if(bundle->bandwidth == 0) 1740ab2de065SBrian Somers bundle->bandwidth = 115200; /* Shrug */ 1741ab2de065SBrian Somers 1742ab2de065SBrian Somers if (bundle->ncp.mp.active) 1743ab2de065SBrian Somers mtu = bundle->ncp.mp.peer_mrru; 1744ab2de065SBrian Somers else if (!mtu) 1745ab2de065SBrian Somers mtu = 1500; 1746ab2de065SBrian Somers 1747ab2de065SBrian Somers #ifndef NORADIUS 1748ab2de065SBrian Somers if (bundle->radius.valid && bundle->radius.mtu && bundle->radius.mtu < mtu) { 1749ab2de065SBrian Somers log_Printf(LogLCP, "Reducing MTU to radius value %lu\n", 1750ab2de065SBrian Somers bundle->radius.mtu); 1751ab2de065SBrian Somers mtu = bundle->radius.mtu; 1752ab2de065SBrian Somers } 1753ab2de065SBrian Somers #endif 1754ab2de065SBrian Somers 1755ab2de065SBrian Somers tun_configure(bundle, mtu); 1756ab2de065SBrian Somers } 1757ab2de065SBrian Somers 1758ab2de065SBrian Somers void 1759ab2de065SBrian Somers bundle_AutoAdjust(struct bundle *bundle, int percent, int what) 1760ab2de065SBrian Somers { 1761ab2de065SBrian Somers struct datalink *dl, *choice, *otherlinkup; 1762ab2de065SBrian Somers 1763ab2de065SBrian Somers choice = otherlinkup = NULL; 1764ab2de065SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 1765ab2de065SBrian Somers if (dl->physical->type == PHYS_AUTO) { 1766ab2de065SBrian Somers if (dl->state == DATALINK_OPEN) { 1767ab2de065SBrian Somers if (what == AUTO_DOWN) { 1768ab2de065SBrian Somers if (choice) 1769ab2de065SBrian Somers otherlinkup = choice; 1770ab2de065SBrian Somers choice = dl; 1771ab2de065SBrian Somers } 1772ab2de065SBrian Somers } else if (dl->state == DATALINK_CLOSED) { 1773ab2de065SBrian Somers if (what == AUTO_UP) { 1774ab2de065SBrian Somers choice = dl; 1775ab2de065SBrian Somers break; 1776ab2de065SBrian Somers } 1777ab2de065SBrian Somers } else { 1778ab2de065SBrian Somers /* An auto link in an intermediate state - forget it for the moment */ 1779ab2de065SBrian Somers choice = NULL; 1780ab2de065SBrian Somers break; 1781ab2de065SBrian Somers } 1782ab2de065SBrian Somers } else if (dl->state == DATALINK_OPEN && what == AUTO_DOWN) 1783ab2de065SBrian Somers otherlinkup = dl; 1784ab2de065SBrian Somers 1785ab2de065SBrian Somers if (choice) { 1786ab2de065SBrian Somers if (what == AUTO_UP) { 1787ab2de065SBrian Somers log_Printf(LogPHASE, "%d%% saturation -> Opening link ``%s''\n", 1788ab2de065SBrian Somers percent, choice->name); 1789ab2de065SBrian Somers datalink_Up(choice, 1, 1); 1790ab2de065SBrian Somers mp_StopAutoloadTimer(&bundle->ncp.mp); 1791ab2de065SBrian Somers } else if (otherlinkup) { /* Only bring the second-last link down */ 1792ab2de065SBrian Somers log_Printf(LogPHASE, "%d%% saturation -> Closing link ``%s''\n", 1793ab2de065SBrian Somers percent, choice->name); 1794d2f5232dSBrian Somers datalink_Close(choice, CLOSE_STAYDOWN); 1795ab2de065SBrian Somers mp_StopAutoloadTimer(&bundle->ncp.mp); 1796ab2de065SBrian Somers } 1797ab2de065SBrian Somers } 1798ab2de065SBrian Somers } 1799ab2de065SBrian Somers 1800ab2de065SBrian Somers int 1801ab2de065SBrian Somers bundle_WantAutoloadTimer(struct bundle *bundle) 1802ab2de065SBrian Somers { 1803ab2de065SBrian Somers struct datalink *dl; 1804ab2de065SBrian Somers int autolink, opened; 1805ab2de065SBrian Somers 1806ab2de065SBrian Somers if (bundle->phase == PHASE_NETWORK) { 1807ab2de065SBrian Somers for (autolink = opened = 0, dl = bundle->links; dl; dl = dl->next) 1808ab2de065SBrian Somers if (dl->physical->type == PHYS_AUTO) { 1809ab2de065SBrian Somers if (++autolink == 2 || (autolink == 1 && opened)) 1810ab2de065SBrian Somers /* Two auto links or one auto and one open in NETWORK phase */ 1811ab2de065SBrian Somers return 1; 1812ab2de065SBrian Somers } else if (dl->state == DATALINK_OPEN) { 1813ab2de065SBrian Somers opened++; 1814ab2de065SBrian Somers if (autolink) 1815ab2de065SBrian Somers /* One auto and one open link in NETWORK phase */ 1816ab2de065SBrian Somers return 1; 1817ab2de065SBrian Somers } 1818ab2de065SBrian Somers } 1819ab2de065SBrian Somers 1820ab2de065SBrian Somers return 0; 1821ab2de065SBrian Somers } 1822