17a6f8720SBrian Somers /*- 27a6f8720SBrian Somers * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org> 37a6f8720SBrian Somers * All rights reserved. 47a6f8720SBrian Somers * 57a6f8720SBrian Somers * Redistribution and use in source and binary forms, with or without 67a6f8720SBrian Somers * modification, are permitted provided that the following conditions 77a6f8720SBrian Somers * are met: 87a6f8720SBrian Somers * 1. Redistributions of source code must retain the above copyright 97a6f8720SBrian Somers * notice, this list of conditions and the following disclaimer. 107a6f8720SBrian Somers * 2. Redistributions in binary form must reproduce the above copyright 117a6f8720SBrian Somers * notice, this list of conditions and the following disclaimer in the 127a6f8720SBrian Somers * documentation and/or other materials provided with the distribution. 137a6f8720SBrian Somers * 147a6f8720SBrian Somers * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 157a6f8720SBrian Somers * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 167a6f8720SBrian Somers * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 177a6f8720SBrian Somers * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 187a6f8720SBrian Somers * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 197a6f8720SBrian Somers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 207a6f8720SBrian Somers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 217a6f8720SBrian Somers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 227a6f8720SBrian Somers * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 237a6f8720SBrian Somers * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 247a6f8720SBrian Somers * SUCH DAMAGE. 257a6f8720SBrian Somers * 2697d92980SPeter Wemm * $FreeBSD$ 277a6f8720SBrian Somers */ 287a6f8720SBrian Somers 291384bd27SBrian Somers #include <sys/param.h> 307a6f8720SBrian Somers #include <sys/socket.h> 317a6f8720SBrian Somers #include <netinet/in.h> 327a6f8720SBrian Somers #include <net/if.h> 333a7b6d76SBrian Somers #include <net/if_tun.h> /* For TUNS* ioctls */ 347a6f8720SBrian Somers #include <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 1012cb305afSBrian Somers #define SEND_MAXFD 3 /* Max file descriptors passed through 10287c3786eSBrian Somers the local domain socket */ 10396c9bb21SBrian Somers 10404eaa58cSBrian Somers static int bundle_RemainingIdleTime(struct bundle *); 10504eaa58cSBrian Somers 106182c898aSBrian Somers static const char * const PhaseNames[] = { 107455aabc3SBrian Somers "Dead", "Establish", "Authenticate", "Network", "Terminate" 108455aabc3SBrian Somers }; 109455aabc3SBrian Somers 110455aabc3SBrian Somers const char * 111455aabc3SBrian Somers bundle_PhaseName(struct bundle *bundle) 1127a6f8720SBrian Somers { 113455aabc3SBrian Somers return bundle->phase <= PHASE_TERMINATE ? 114455aabc3SBrian Somers PhaseNames[bundle->phase] : "unknown"; 1157a6f8720SBrian Somers } 1167a6f8720SBrian Somers 117455aabc3SBrian Somers void 1185563ebdeSBrian Somers bundle_NewPhase(struct bundle *bundle, u_int new) 119455aabc3SBrian Somers { 120aef795ccSBrian Somers if (new == bundle->phase) 121aef795ccSBrian Somers return; 122aef795ccSBrian Somers 123e2ebb036SBrian Somers if (new <= PHASE_TERMINATE) 124dd7e2610SBrian Somers log_Printf(LogPHASE, "bundle: %s\n", PhaseNames[new]); 1257a6f8720SBrian Somers 126455aabc3SBrian Somers switch (new) { 127455aabc3SBrian Somers case PHASE_DEAD: 1280f2f3eb3SBrian Somers log_DisplayPrompts(); 129455aabc3SBrian Somers bundle->phase = new; 130455aabc3SBrian Somers break; 131455aabc3SBrian Somers 132455aabc3SBrian Somers case PHASE_ESTABLISH: 133455aabc3SBrian Somers bundle->phase = new; 134455aabc3SBrian Somers break; 135455aabc3SBrian Somers 136455aabc3SBrian Somers case PHASE_AUTHENTICATE: 137455aabc3SBrian Somers bundle->phase = new; 1380f2f3eb3SBrian Somers log_DisplayPrompts(); 139455aabc3SBrian Somers break; 140455aabc3SBrian Somers 141455aabc3SBrian Somers case PHASE_NETWORK: 142dd7e2610SBrian Somers fsm_Up(&bundle->ncp.ipcp.fsm); 143dd7e2610SBrian Somers fsm_Open(&bundle->ncp.ipcp.fsm); 144673903ecSBrian Somers bundle->phase = new; 1450f2f3eb3SBrian Somers log_DisplayPrompts(); 146673903ecSBrian Somers break; 147455aabc3SBrian Somers 148455aabc3SBrian Somers case PHASE_TERMINATE: 149455aabc3SBrian Somers bundle->phase = new; 150673903ecSBrian Somers mp_Down(&bundle->ncp.mp); 1510f2f3eb3SBrian Somers log_DisplayPrompts(); 152455aabc3SBrian Somers break; 1537a6f8720SBrian Somers } 1547a6f8720SBrian Somers } 1557a6f8720SBrian Somers 1566d666775SBrian Somers static void 1576d666775SBrian Somers bundle_LayerStart(void *v, struct fsm *fp) 1587a6f8720SBrian Somers { 1593006ec67SBrian Somers /* The given FSM is about to start up ! */ 1607a6f8720SBrian Somers } 1617a6f8720SBrian Somers 1625cf4388bSBrian Somers 1635cf4388bSBrian Somers static void 1645cf4388bSBrian Somers bundle_Notify(struct bundle *bundle, char c) 1655cf4388bSBrian Somers { 1665cf4388bSBrian Somers if (bundle->notify.fd != -1) { 1675cf4388bSBrian Somers if (write(bundle->notify.fd, &c, 1) == 1) 1685a83ad1eSBrian Somers log_Printf(LogPHASE, "Parent notified of %s\n", 1695a83ad1eSBrian Somers c == EX_NORMAL ? "success" : "failure"); 1705cf4388bSBrian Somers else 171dd7e2610SBrian Somers log_Printf(LogPHASE, "Failed to notify parent of success.\n"); 1725cf4388bSBrian Somers close(bundle->notify.fd); 1735cf4388bSBrian Somers bundle->notify.fd = -1; 1745cf4388bSBrian Somers } 1755cf4388bSBrian Somers } 1763b0f8d2eSBrian Somers 1776d666775SBrian Somers static void 1786f8e9f0aSBrian Somers bundle_ClearQueues(void *v) 1796f8e9f0aSBrian Somers { 1806f8e9f0aSBrian Somers struct bundle *bundle = (struct bundle *)v; 1816f8e9f0aSBrian Somers struct datalink *dl; 1826f8e9f0aSBrian Somers 1836f8e9f0aSBrian Somers log_Printf(LogPHASE, "Clearing choked output queue\n"); 1846f8e9f0aSBrian Somers timer_Stop(&bundle->choked.timer); 1856f8e9f0aSBrian Somers 1866f8e9f0aSBrian Somers /* 1876f8e9f0aSBrian Somers * Emergency time: 1886f8e9f0aSBrian Somers * 1896f8e9f0aSBrian Somers * We've had a full queue for PACKET_DEL_SECS seconds without being 1906f8e9f0aSBrian Somers * able to get rid of any of the packets. We've probably given up 1916f8e9f0aSBrian Somers * on the redials at this point, and the queued data has almost 1926f8e9f0aSBrian Somers * definitely been timed out by the layer above. As this is preventing 1936f8e9f0aSBrian Somers * us from reading the TUN_NAME device (we don't want to buffer stuff 1946f8e9f0aSBrian Somers * indefinitely), we may as well nuke this data and start with a clean 1956f8e9f0aSBrian Somers * slate ! 1966f8e9f0aSBrian Somers * 1976f8e9f0aSBrian Somers * Unfortunately, this has the side effect of shafting any compression 1986f8e9f0aSBrian Somers * dictionaries in use (causing the relevant RESET_REQ/RESET_ACK). 1996f8e9f0aSBrian Somers */ 2006f8e9f0aSBrian Somers 2015a72b6edSBrian Somers ip_DeleteQueue(&bundle->ncp.ipcp); 2026f8e9f0aSBrian Somers mp_DeleteQueue(&bundle->ncp.mp); 2036f8e9f0aSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 2046f8e9f0aSBrian Somers physical_DeleteQueue(dl->physical); 2056f8e9f0aSBrian Somers } 2066f8e9f0aSBrian Somers 2076f8e9f0aSBrian Somers static void 208ff0f9439SBrian Somers bundle_LinkAdded(struct bundle *bundle, struct datalink *dl) 209ff0f9439SBrian Somers { 210ff0f9439SBrian Somers bundle->phys_type.all |= dl->physical->type; 211ff0f9439SBrian Somers if (dl->state == DATALINK_OPEN) 212ff0f9439SBrian Somers bundle->phys_type.open |= dl->physical->type; 213ff0f9439SBrian Somers 214ff0f9439SBrian Somers if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) 215ff0f9439SBrian Somers != bundle->phys_type.open && bundle->idle.timer.state == TIMER_STOPPED) 216ff0f9439SBrian Somers /* We may need to start our idle timer */ 217ff0f9439SBrian Somers bundle_StartIdleTimer(bundle); 218ff0f9439SBrian Somers } 219ff0f9439SBrian Somers 22092b09558SBrian Somers void 221ff0f9439SBrian Somers bundle_LinksRemoved(struct bundle *bundle) 222ff0f9439SBrian Somers { 223ff0f9439SBrian Somers struct datalink *dl; 224ff0f9439SBrian Somers 225ff0f9439SBrian Somers bundle->phys_type.all = bundle->phys_type.open = 0; 226ff0f9439SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 227ff0f9439SBrian Somers bundle_LinkAdded(bundle, dl); 228ff0f9439SBrian Somers 229ab2de065SBrian Somers bundle_CalculateBandwidth(bundle); 230ab2de065SBrian Somers mp_CheckAutoloadTimer(&bundle->ncp.mp); 231ab2de065SBrian Somers 232ff0f9439SBrian Somers if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) 233ff0f9439SBrian Somers == bundle->phys_type.open) 234ff0f9439SBrian Somers bundle_StopIdleTimer(bundle); 235ff0f9439SBrian Somers } 23604eaa58cSBrian Somers 23704eaa58cSBrian Somers static void 2386f384573SBrian Somers bundle_LayerUp(void *v, struct fsm *fp) 2397a6f8720SBrian Somers { 2403006ec67SBrian Somers /* 2413006ec67SBrian Somers * The given fsm is now up 242ab2de065SBrian Somers * If it's an LCP, adjust our phys_mode.open value and check the 243ab2de065SBrian Somers * autoload timer. 244ab2de065SBrian Somers * If it's the first NCP, calculate our bandwidth 245dade2407SBrian Somers * If it's the first NCP, set our ``upat'' time 2463b0f8d2eSBrian Somers * If it's the first NCP, start the idle timer. 247ab2de065SBrian Somers * If it's an NCP, tell our -background parent to go away. 248ab2de065SBrian Somers * If it's the first NCP, start the autoload timer 2493006ec67SBrian Somers */ 2506f384573SBrian Somers struct bundle *bundle = (struct bundle *)v; 2516d666775SBrian Somers 2525563ebdeSBrian Somers if (fp->proto == PROTO_LCP) { 253ff0f9439SBrian Somers struct physical *p = link2physical(fp->link); 254ff0f9439SBrian Somers 255ff0f9439SBrian Somers bundle_LinkAdded(bundle, p->dl); 256ab2de065SBrian Somers mp_CheckAutoloadTimer(&bundle->ncp.mp); 2573b0f8d2eSBrian Somers } else if (fp->proto == PROTO_IPCP) { 258ab2de065SBrian Somers bundle_CalculateBandwidth(fp->bundle); 259dade2407SBrian Somers time(&bundle->upat); 260ab886ad0SBrian Somers bundle_StartIdleTimer(bundle); 2615cf4388bSBrian Somers bundle_Notify(bundle, EX_NORMAL); 262ab2de065SBrian Somers mp_CheckAutoloadTimer(&fp->bundle->ncp.mp); 2637a6f8720SBrian Somers } 264ab886ad0SBrian Somers } 2657a6f8720SBrian Somers 2666d666775SBrian Somers static void 2676d666775SBrian Somers bundle_LayerDown(void *v, struct fsm *fp) 2686d666775SBrian Somers { 2696d666775SBrian Somers /* 2706d666775SBrian Somers * The given FSM has been told to come down. 271ab886ad0SBrian Somers * If it's our last NCP, stop the idle timer. 272dade2407SBrian Somers * If it's our last NCP, clear our ``upat'' value. 273ab2de065SBrian Somers * If it's our last NCP, stop the autoload timer 274ff0f9439SBrian Somers * If it's an LCP, adjust our phys_type.open value and any timers. 27586b1f0d7SBrian Somers * If it's an LCP and we're in multilink mode, adjust our tun 27686b1f0d7SBrian Somers * speed and make sure our minimum sequence number is adjusted. 2776d666775SBrian Somers */ 278ab886ad0SBrian Somers 279ab886ad0SBrian Somers struct bundle *bundle = (struct bundle *)v; 280ab886ad0SBrian Somers 281ab2de065SBrian Somers if (fp->proto == PROTO_IPCP) { 282ab886ad0SBrian Somers bundle_StopIdleTimer(bundle); 283dade2407SBrian Somers bundle->upat = 0; 284ab2de065SBrian Somers mp_StopAutoloadTimer(&bundle->ncp.mp); 285ab2de065SBrian Somers } else if (fp->proto == PROTO_LCP) { 286ff0f9439SBrian Somers bundle_LinksRemoved(bundle); /* adjust timers & phys_type values */ 287ff0f9439SBrian Somers if (bundle->ncp.mp.active) { 2883b0f8d2eSBrian Somers struct datalink *dl; 28986b1f0d7SBrian Somers struct datalink *lost; 2903b0f8d2eSBrian Somers 29186b1f0d7SBrian Somers lost = NULL; 292faefde08SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 29386b1f0d7SBrian Somers if (fp == &dl->physical->link.lcp.fsm) 29486b1f0d7SBrian Somers lost = dl; 29586b1f0d7SBrian Somers 296ab2de065SBrian Somers bundle_CalculateBandwidth(bundle); 29786b1f0d7SBrian Somers 29886b1f0d7SBrian Somers if (lost) 29986b1f0d7SBrian Somers mp_LinkLost(&bundle->ncp.mp, lost); 30086b1f0d7SBrian Somers else 301a33b2ef7SBrian Somers log_Printf(LogALERT, "Oops, lost an unrecognised datalink (%s) !\n", 30286b1f0d7SBrian Somers fp->link->name); 3033b0f8d2eSBrian Somers } 3046d666775SBrian Somers } 305ff0f9439SBrian Somers } 3066d666775SBrian Somers 3076d666775SBrian Somers static void 3086d666775SBrian Somers bundle_LayerFinish(void *v, struct fsm *fp) 3096d666775SBrian Somers { 3106d666775SBrian Somers /* The given fsm is now down (fp cannot be NULL) 3116d666775SBrian Somers * 312dd7e2610SBrian Somers * If it's the last NCP, fsm_Close all LCPs 3136d666775SBrian Somers */ 3146d666775SBrian Somers 3156d666775SBrian Somers struct bundle *bundle = (struct bundle *)v; 3166d666775SBrian Somers struct datalink *dl; 3176d666775SBrian Somers 3183b0f8d2eSBrian Somers if (fp->proto == PROTO_IPCP) { 31926afeaa2SBrian Somers if (bundle_Phase(bundle) != PHASE_DEAD) 32025092092SBrian Somers bundle_NewPhase(bundle, PHASE_TERMINATE); 3216d666775SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 322d4d5d2f8SBrian Somers datalink_Close(dl, CLOSE_STAYDOWN); 32309206a6fSBrian Somers fsm2initial(fp); 3243b0f8d2eSBrian Somers } 3255a8b6a98SBrian Somers /* 3265a8b6a98SBrian Somers * If it's an LCP, don't try to murder any NCPs, let bundle_LinkClosed() 3275a8b6a98SBrian Somers * do that side of things (at a time when a call to fsm2initial() on the 3285a8b6a98SBrian Somers * NCP isn't going to take charge of bringing down this link). 3295a8b6a98SBrian Somers */ 3303b0f8d2eSBrian Somers } 3316d666775SBrian Somers 3327a6f8720SBrian Somers int 3337a6f8720SBrian Somers bundle_LinkIsUp(const struct bundle *bundle) 3347a6f8720SBrian Somers { 3355828db6dSBrian Somers return bundle->ncp.ipcp.fsm.state == ST_OPENED; 3367a6f8720SBrian Somers } 3377a6f8720SBrian Somers 3387a6f8720SBrian Somers void 3399c81b87dSBrian Somers bundle_Close(struct bundle *bundle, const char *name, int how) 3407a6f8720SBrian Somers { 341455aabc3SBrian Somers /* 3423006ec67SBrian Somers * Please close the given datalink. 343dd7e2610SBrian Somers * If name == NULL or name is the last datalink, fsm_Close all NCPs 3446f384573SBrian Somers * (except our MP) 3453b0f8d2eSBrian Somers * If it isn't the last datalink, just Close that datalink. 346455aabc3SBrian Somers */ 3477a6f8720SBrian Somers 3483b0f8d2eSBrian Somers struct datalink *dl, *this_dl; 3493b0f8d2eSBrian Somers int others_active; 3503006ec67SBrian Somers 3513b0f8d2eSBrian Somers others_active = 0; 3523b0f8d2eSBrian Somers this_dl = NULL; 3533b0f8d2eSBrian Somers 3543b0f8d2eSBrian Somers for (dl = bundle->links; dl; dl = dl->next) { 3553b0f8d2eSBrian Somers if (name && !strcasecmp(name, dl->name)) 3563b0f8d2eSBrian Somers this_dl = dl; 3573b0f8d2eSBrian Somers if (name == NULL || this_dl == dl) { 3589c81b87dSBrian Somers switch (how) { 3599c81b87dSBrian Somers case CLOSE_LCP: 3609c81b87dSBrian Somers datalink_DontHangup(dl); 3619c81b87dSBrian Somers /* fall through */ 3629c81b87dSBrian Somers case CLOSE_STAYDOWN: 3633006ec67SBrian Somers datalink_StayDown(dl); 3649c81b87dSBrian Somers break; 3659c81b87dSBrian Somers } 3663b0f8d2eSBrian Somers } else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) 3673b0f8d2eSBrian Somers others_active++; 3683b0f8d2eSBrian Somers } 3693b0f8d2eSBrian Somers 3703b0f8d2eSBrian Somers if (name && this_dl == NULL) { 371dd7e2610SBrian Somers log_Printf(LogWARN, "%s: Invalid datalink name\n", name); 3723b0f8d2eSBrian Somers return; 3733b0f8d2eSBrian Somers } 3743b0f8d2eSBrian Somers 3753b0f8d2eSBrian Somers if (!others_active) { 37604eaa58cSBrian Somers bundle_StopIdleTimer(bundle); 3773b0f8d2eSBrian Somers if (bundle->ncp.ipcp.fsm.state > ST_CLOSED || 3783b0f8d2eSBrian Somers bundle->ncp.ipcp.fsm.state == ST_STARTING) 379dd7e2610SBrian Somers fsm_Close(&bundle->ncp.ipcp.fsm); 3803b0f8d2eSBrian Somers else { 38109206a6fSBrian Somers fsm2initial(&bundle->ncp.ipcp.fsm); 382d345321bSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 3839c81b87dSBrian Somers datalink_Close(dl, how); 3847a6f8720SBrian Somers } 3853b0f8d2eSBrian Somers } else if (this_dl && this_dl->state != DATALINK_CLOSED && 3863b0f8d2eSBrian Somers this_dl->state != DATALINK_HANGUP) 3879c81b87dSBrian Somers datalink_Close(this_dl, how); 388d2fd8d77SBrian Somers } 3897a6f8720SBrian Somers 3901bc9b5baSBrian Somers void 391899011c4SBrian Somers bundle_Down(struct bundle *bundle, int how) 3921bc9b5baSBrian Somers { 3931bc9b5baSBrian Somers struct datalink *dl; 3941bc9b5baSBrian Somers 3951bc9b5baSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 396899011c4SBrian Somers datalink_Down(dl, how); 3971bc9b5baSBrian Somers } 3981bc9b5baSBrian Somers 39926af0ae9SBrian Somers static size_t 40026af0ae9SBrian Somers bundle_FillQueues(struct bundle *bundle) 40126af0ae9SBrian Somers { 40226af0ae9SBrian Somers size_t total; 40326af0ae9SBrian Somers 40426af0ae9SBrian Somers if (bundle->ncp.mp.active) 40526af0ae9SBrian Somers total = mp_FillQueues(bundle); 40626af0ae9SBrian Somers else { 40726af0ae9SBrian Somers struct datalink *dl; 40826af0ae9SBrian Somers size_t add; 40926af0ae9SBrian Somers 41026af0ae9SBrian Somers for (total = 0, dl = bundle->links; dl; dl = dl->next) 41126af0ae9SBrian Somers if (dl->state == DATALINK_OPEN) { 41226af0ae9SBrian Somers add = link_QueueLen(&dl->physical->link); 41326af0ae9SBrian Somers if (add == 0 && dl->physical->out == NULL) 41426af0ae9SBrian Somers add = ip_PushPacket(&dl->physical->link, bundle); 41526af0ae9SBrian Somers total += add; 41626af0ae9SBrian Somers } 41726af0ae9SBrian Somers } 41826af0ae9SBrian Somers 41926af0ae9SBrian Somers return total + ip_QueueLen(&bundle->ncp.ipcp); 42026af0ae9SBrian Somers } 42126af0ae9SBrian Somers 4222f786681SBrian Somers static int 423f013f33eSBrian Somers bundle_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n) 4242f786681SBrian Somers { 4252f786681SBrian Somers struct bundle *bundle = descriptor2bundle(d); 4262f786681SBrian Somers struct datalink *dl; 42726af0ae9SBrian Somers int result, nlinks; 42826af0ae9SBrian Somers size_t queued; 4292f786681SBrian Somers 4302f786681SBrian Somers result = 0; 4312f786681SBrian Somers 432078c562eSBrian Somers /* If there are aren't many packets queued, look for some more. */ 43304eaa58cSBrian Somers for (nlinks = 0, dl = bundle->links; dl; dl = dl->next) 43404eaa58cSBrian Somers nlinks++; 43504eaa58cSBrian Somers 43604eaa58cSBrian Somers if (nlinks) { 4375a72b6edSBrian Somers queued = r ? bundle_FillQueues(bundle) : ip_QueueLen(&bundle->ncp.ipcp); 43804eaa58cSBrian Somers 439ff0f9439SBrian Somers if (r && (bundle->phase == PHASE_NETWORK || 440ff0f9439SBrian Somers bundle->phys_type.all & PHYS_AUTO)) { 44104eaa58cSBrian Somers /* enough surplus so that we can tell if we're getting swamped */ 442442f8495SBrian Somers if (queued < 30) { 44304eaa58cSBrian Somers /* Not enough - select() for more */ 4446f8e9f0aSBrian Somers if (bundle->choked.timer.state == TIMER_RUNNING) 4456f8e9f0aSBrian Somers timer_Stop(&bundle->choked.timer); /* Not needed any more */ 44604eaa58cSBrian Somers FD_SET(bundle->dev.fd, r); 447faefde08SBrian Somers if (*n < bundle->dev.fd + 1) 448faefde08SBrian Somers *n = bundle->dev.fd + 1; 4491384bd27SBrian Somers log_Printf(LogTIMER, "%s: fdset(r) %d\n", TUN_NAME, bundle->dev.fd); 450078c562eSBrian Somers result++; 4516f8e9f0aSBrian Somers } else if (bundle->choked.timer.state == TIMER_STOPPED) { 4526f8e9f0aSBrian Somers bundle->choked.timer.func = bundle_ClearQueues; 4536f8e9f0aSBrian Somers bundle->choked.timer.name = "output choke"; 4546f8e9f0aSBrian Somers bundle->choked.timer.load = bundle->cfg.choked.timeout * SECTICKS; 4556f8e9f0aSBrian Somers bundle->choked.timer.arg = bundle; 4566f8e9f0aSBrian Somers timer_Start(&bundle->choked.timer); 457078c562eSBrian Somers } 45804eaa58cSBrian Somers } 45904eaa58cSBrian Somers } 460078c562eSBrian Somers 461f0cdd9c0SBrian Somers #ifndef NORADIUS 462f0cdd9c0SBrian Somers result += descriptor_UpdateSet(&bundle->radius.desc, r, w, e, n); 463f0cdd9c0SBrian Somers #endif 464f0cdd9c0SBrian Somers 46571555108SBrian Somers /* Which links need a select() ? */ 46671555108SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 46771555108SBrian Somers result += descriptor_UpdateSet(&dl->desc, r, w, e, n); 46871555108SBrian Somers 469ea722969SBrian Somers /* 470ea722969SBrian Somers * This *MUST* be called after the datalink UpdateSet()s as it 47104eaa58cSBrian Somers * might be ``holding'' one of the datalinks (death-row) and 4728e7bd08eSBrian Somers * wants to be able to de-select() it from the descriptor set. 473ea722969SBrian Somers */ 4740f2f3eb3SBrian Somers result += descriptor_UpdateSet(&bundle->ncp.mp.server.desc, r, w, e, n); 475ea722969SBrian Somers 4762f786681SBrian Somers return result; 4772f786681SBrian Somers } 4782f786681SBrian Somers 4792f786681SBrian Somers static int 480f013f33eSBrian Somers bundle_IsSet(struct fdescriptor *d, const fd_set *fdset) 4812f786681SBrian Somers { 4822f786681SBrian Somers struct bundle *bundle = descriptor2bundle(d); 4832f786681SBrian Somers struct datalink *dl; 4842f786681SBrian Somers 4852f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 4862f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 4872f786681SBrian Somers return 1; 4882f786681SBrian Somers 489f0cdd9c0SBrian Somers #ifndef NORADIUS 490f0cdd9c0SBrian Somers if (descriptor_IsSet(&bundle->radius.desc, fdset)) 491f0cdd9c0SBrian Somers return 1; 492f0cdd9c0SBrian Somers #endif 493f0cdd9c0SBrian Somers 494332b9de0SBrian Somers if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 495332b9de0SBrian Somers return 1; 496332b9de0SBrian Somers 497faefde08SBrian Somers return FD_ISSET(bundle->dev.fd, fdset); 4982f786681SBrian Somers } 4992f786681SBrian Somers 5002f786681SBrian Somers static void 501f013f33eSBrian Somers bundle_DescriptorRead(struct fdescriptor *d, struct bundle *bundle, 5022f786681SBrian Somers const fd_set *fdset) 5032f786681SBrian Somers { 5042f786681SBrian Somers struct datalink *dl; 5052f786681SBrian Somers 506ea722969SBrian Somers if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 507ea722969SBrian Somers descriptor_Read(&bundle->ncp.mp.server.desc, bundle, fdset); 508ea722969SBrian Somers 5092f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 5102f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 5112f786681SBrian Somers descriptor_Read(&dl->desc, bundle, fdset); 512b6217683SBrian Somers 513f0cdd9c0SBrian Somers #ifndef NORADIUS 514f0cdd9c0SBrian Somers if (descriptor_IsSet(&bundle->radius.desc, fdset)) 515f0cdd9c0SBrian Somers descriptor_Read(&bundle->radius.desc, bundle, fdset); 516f0cdd9c0SBrian Somers #endif 517f0cdd9c0SBrian Somers 518faefde08SBrian Somers if (FD_ISSET(bundle->dev.fd, fdset)) { 519078c562eSBrian Somers struct tun_data tun; 520078c562eSBrian Somers int n, pri; 5213a7b6d76SBrian Somers char *data; 5223a7b6d76SBrian Somers size_t sz; 5233a7b6d76SBrian Somers 5243a7b6d76SBrian Somers if (bundle->dev.header) { 5253a7b6d76SBrian Somers data = (char *)&tun; 5263a7b6d76SBrian Somers sz = sizeof tun; 5273a7b6d76SBrian Somers } else { 5283a7b6d76SBrian Somers data = tun.data; 5293a7b6d76SBrian Somers sz = sizeof tun.data; 5303a7b6d76SBrian Somers } 531078c562eSBrian Somers 532078c562eSBrian Somers /* something to read from tun */ 5333a7b6d76SBrian Somers 5343a7b6d76SBrian Somers n = read(bundle->dev.fd, data, sz); 535078c562eSBrian Somers if (n < 0) { 5363a7b6d76SBrian Somers log_Printf(LogWARN, "%s: read: %s\n", bundle->dev.Name, strerror(errno)); 537078c562eSBrian Somers return; 538078c562eSBrian Somers } 5393a7b6d76SBrian Somers 5403a7b6d76SBrian Somers if (bundle->dev.header) { 5413a7b6d76SBrian Somers n -= sz - sizeof tun.data; 542078c562eSBrian Somers if (n <= 0) { 5433a7b6d76SBrian Somers log_Printf(LogERROR, "%s: read: Got only %d bytes of data !\n", 5443a7b6d76SBrian Somers bundle->dev.Name, n); 545078c562eSBrian Somers return; 546078c562eSBrian Somers } 5473a7b6d76SBrian Somers if (ntohl(tun.family) != AF_INET) 5483a7b6d76SBrian Somers /* XXX: Should be maintaining drop/family counts ! */ 549078c562eSBrian Somers return; 5503a7b6d76SBrian Somers } 551078c562eSBrian Somers 552078c562eSBrian Somers if (((struct ip *)tun.data)->ip_dst.s_addr == 553078c562eSBrian Somers bundle->ncp.ipcp.my_ip.s_addr) { 554078c562eSBrian Somers /* we've been asked to send something addressed *to* us :( */ 555078c562eSBrian Somers if (Enabled(bundle, OPT_LOOPBACK)) { 556078c562eSBrian Somers pri = PacketCheck(bundle, tun.data, n, &bundle->filter.in); 557078c562eSBrian Somers if (pri >= 0) { 5583a7b6d76SBrian Somers n += sz - sizeof tun.data; 5593a7b6d76SBrian Somers write(bundle->dev.fd, data, n); 560078c562eSBrian Somers log_Printf(LogDEBUG, "Looped back packet addressed to myself\n"); 561078c562eSBrian Somers } 562078c562eSBrian Somers return; 563078c562eSBrian Somers } else 564078c562eSBrian Somers log_Printf(LogDEBUG, "Oops - forwarding packet addressed to myself\n"); 565078c562eSBrian Somers } 566078c562eSBrian Somers 567078c562eSBrian Somers /* 568078c562eSBrian Somers * Process on-demand dialup. Output packets are queued within tunnel 569078c562eSBrian Somers * device until IPCP is opened. 570078c562eSBrian Somers */ 571078c562eSBrian Somers 572078c562eSBrian Somers if (bundle_Phase(bundle) == PHASE_DEAD) { 573078c562eSBrian Somers /* 574078c562eSBrian Somers * Note, we must be in AUTO mode :-/ otherwise our interface should 575078c562eSBrian Somers * *not* be UP and we can't receive data 576078c562eSBrian Somers */ 577078c562eSBrian Somers if ((pri = PacketCheck(bundle, tun.data, n, &bundle->filter.dial)) >= 0) 578ba23f397SBrian Somers bundle_Open(bundle, NULL, PHYS_AUTO, 0); 579078c562eSBrian Somers else 580078c562eSBrian Somers /* 581078c562eSBrian Somers * Drop the packet. If we were to queue it, we'd just end up with 582078c562eSBrian Somers * a pile of timed-out data in our output queue by the time we get 583078c562eSBrian Somers * around to actually dialing. We'd also prematurely reach the 584078c562eSBrian Somers * threshold at which we stop select()ing to read() the tun 585078c562eSBrian Somers * device - breaking auto-dial. 586078c562eSBrian Somers */ 587078c562eSBrian Somers return; 588078c562eSBrian Somers } 589078c562eSBrian Somers 590078c562eSBrian Somers pri = PacketCheck(bundle, tun.data, n, &bundle->filter.out); 5915d9e6103SBrian Somers if (pri >= 0) 5925a72b6edSBrian Somers ip_Enqueue(&bundle->ncp.ipcp, pri, tun.data, n); 593078c562eSBrian Somers } 594078c562eSBrian Somers } 5952f786681SBrian Somers 5961af29a6eSBrian Somers static int 597f013f33eSBrian Somers bundle_DescriptorWrite(struct fdescriptor *d, struct bundle *bundle, 5982f786681SBrian Somers const fd_set *fdset) 5992f786681SBrian Somers { 6002f786681SBrian Somers struct datalink *dl; 6011af29a6eSBrian Somers int result = 0; 6022f786681SBrian Somers 603ea722969SBrian Somers /* This is not actually necessary as struct mpserver doesn't Write() */ 604ea722969SBrian Somers if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 605ea722969SBrian Somers descriptor_Write(&bundle->ncp.mp.server.desc, bundle, fdset); 606ea722969SBrian Somers 6072f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 6082f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 6091af29a6eSBrian Somers result += descriptor_Write(&dl->desc, bundle, fdset); 6101af29a6eSBrian Somers 6111af29a6eSBrian Somers return result; 6122f786681SBrian Somers } 6132f786681SBrian Somers 614da66dd13SBrian Somers void 6151384bd27SBrian Somers bundle_LockTun(struct bundle *bundle) 6161384bd27SBrian Somers { 6171384bd27SBrian Somers FILE *lockfile; 6181384bd27SBrian Somers char pidfile[MAXPATHLEN]; 6191384bd27SBrian Somers 6201384bd27SBrian Somers snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit); 6211384bd27SBrian Somers lockfile = ID0fopen(pidfile, "w"); 6221384bd27SBrian Somers if (lockfile != NULL) { 6231384bd27SBrian Somers fprintf(lockfile, "%d\n", (int)getpid()); 6241384bd27SBrian Somers fclose(lockfile); 6251384bd27SBrian Somers } 6261384bd27SBrian Somers #ifndef RELEASE_CRUNCH 6271384bd27SBrian Somers else 6281384bd27SBrian Somers log_Printf(LogERROR, "Warning: Can't create %s: %s\n", 6291384bd27SBrian Somers pidfile, strerror(errno)); 6301384bd27SBrian Somers #endif 6311384bd27SBrian Somers } 6321384bd27SBrian Somers 6331384bd27SBrian Somers static void 6341384bd27SBrian Somers bundle_UnlockTun(struct bundle *bundle) 6351384bd27SBrian Somers { 6361384bd27SBrian Somers char pidfile[MAXPATHLEN]; 6371384bd27SBrian Somers 6381384bd27SBrian Somers snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit); 6391384bd27SBrian Somers ID0unlink(pidfile); 6401384bd27SBrian Somers } 6417a6f8720SBrian Somers 6427a6f8720SBrian Somers struct bundle * 643cf0a3940SBrian Somers bundle_Create(const char *prefix, int type, int unit) 6447a6f8720SBrian Somers { 6457a6f8720SBrian Somers static struct bundle bundle; /* there can be only one */ 646c0593e34SBrian Somers int enoentcount, err, minunit, maxunit; 6474e5196e9SBrian Somers const char *ifname; 648fdb4bb1bSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD) 649fc3034caSBrian Somers int kldtried; 650fc3034caSBrian Somers #endif 6513a7b6d76SBrian Somers #if defined(TUNSIFMODE) || defined(TUNSLMODE) || defined(TUNSIFHEAD) 6520f203c7eSBrian Somers int iff; 6530f203c7eSBrian Somers #endif 6547a6f8720SBrian Somers 6558fa6ebe4SBrian Somers if (bundle.iface != NULL) { /* Already allocated ! */ 656a33b2ef7SBrian Somers log_Printf(LogALERT, "bundle_Create: There's only one BUNDLE !\n"); 6577a6f8720SBrian Somers return NULL; 6587a6f8720SBrian Somers } 6597a6f8720SBrian Somers 660c0593e34SBrian Somers if (unit == -1) { 661c0593e34SBrian Somers minunit = 0; 662c0593e34SBrian Somers maxunit = -1; 663c0593e34SBrian Somers } else { 664c0593e34SBrian Somers minunit = unit; 665c0593e34SBrian Somers maxunit = unit + 1; 666c0593e34SBrian Somers } 6677a6f8720SBrian Somers err = ENOENT; 6687a6f8720SBrian Somers enoentcount = 0; 669fdb4bb1bSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD) 670fc3034caSBrian Somers kldtried = 0; 671fc3034caSBrian Somers #endif 672c0593e34SBrian Somers for (bundle.unit = minunit; bundle.unit != maxunit; bundle.unit++) { 673faefde08SBrian Somers snprintf(bundle.dev.Name, sizeof bundle.dev.Name, "%s%d", 674faefde08SBrian Somers prefix, bundle.unit); 675faefde08SBrian Somers bundle.dev.fd = ID0open(bundle.dev.Name, O_RDWR); 676faefde08SBrian Somers if (bundle.dev.fd >= 0) 6777a6f8720SBrian Somers break; 678107d62e7SBrian Somers else if (errno == ENXIO) { 679fdb4bb1bSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD) 680c0593e34SBrian Somers if (bundle.unit == minunit && !kldtried++) { 681fc3034caSBrian Somers /* 682fdb4bb1bSBrian Somers * Attempt to load the tunnel interface KLD if it isn't loaded 683fdb4bb1bSBrian Somers * already. 684fc3034caSBrian Somers */ 685fdb4bb1bSBrian Somers if (modfind("if_tun") == -1) { 686fc3034caSBrian Somers if (ID0kldload("if_tun") != -1) { 687fc3034caSBrian Somers bundle.unit--; 688fc3034caSBrian Somers continue; 689fc3034caSBrian Somers } 690fc3034caSBrian Somers log_Printf(LogWARN, "kldload: if_tun: %s\n", strerror(errno)); 691fc3034caSBrian Somers } 692fc3034caSBrian Somers } 693fc3034caSBrian Somers #endif 6947a6f8720SBrian Somers err = errno; 695107d62e7SBrian Somers break; 6967a6f8720SBrian Somers } else if (errno == ENOENT) { 6977a6f8720SBrian Somers if (++enoentcount > 2) 698107d62e7SBrian Somers break; 6997a6f8720SBrian Somers } else 7007a6f8720SBrian Somers err = errno; 7017a6f8720SBrian Somers } 7027a6f8720SBrian Somers 703faefde08SBrian Somers if (bundle.dev.fd < 0) { 704c0593e34SBrian Somers if (unit == -1) 705c0593e34SBrian Somers log_Printf(LogWARN, "No available tunnel devices found (%s)\n", 70685b542cfSBrian Somers strerror(err)); 707c0593e34SBrian Somers else 708c0593e34SBrian Somers log_Printf(LogWARN, "%s%d: %s\n", prefix, unit, strerror(err)); 7097a6f8720SBrian Somers return NULL; 7107a6f8720SBrian Somers } 7117a6f8720SBrian Somers 712dd7e2610SBrian Somers log_SetTun(bundle.unit); 7137a6f8720SBrian Somers 7148fa6ebe4SBrian Somers ifname = strrchr(bundle.dev.Name, '/'); 7158fa6ebe4SBrian Somers if (ifname == NULL) 7168fa6ebe4SBrian Somers ifname = bundle.dev.Name; 7177a6f8720SBrian Somers else 7188fa6ebe4SBrian Somers ifname++; 7198fa6ebe4SBrian Somers 7208fa6ebe4SBrian Somers bundle.iface = iface_Create(ifname); 7218fa6ebe4SBrian Somers if (bundle.iface == NULL) { 7228fa6ebe4SBrian Somers close(bundle.dev.fd); 7238fa6ebe4SBrian Somers return NULL; 7248fa6ebe4SBrian Somers } 7257a6f8720SBrian Somers 7260f203c7eSBrian Somers #ifdef TUNSIFMODE 7270f203c7eSBrian Somers /* Make sure we're POINTOPOINT */ 7280f203c7eSBrian Somers iff = IFF_POINTOPOINT; 7290f203c7eSBrian Somers if (ID0ioctl(bundle.dev.fd, TUNSIFMODE, &iff) < 0) 7300f203c7eSBrian Somers log_Printf(LogERROR, "bundle_Create: ioctl(TUNSIFMODE): %s\n", 7310f203c7eSBrian Somers strerror(errno)); 7320f203c7eSBrian Somers #endif 7330f203c7eSBrian Somers 7346ca65df0SBrian Somers #ifdef TUNSLMODE 7353a7b6d76SBrian Somers /* Make sure we're not prepending sockaddrs */ 7366ca65df0SBrian Somers iff = 0; 7376ca65df0SBrian Somers if (ID0ioctl(bundle.dev.fd, TUNSLMODE, &iff) < 0) 7386ca65df0SBrian Somers log_Printf(LogERROR, "bundle_Create: ioctl(TUNSLMODE): %s\n", 7396ca65df0SBrian Somers strerror(errno)); 7406ca65df0SBrian Somers #endif 7416ca65df0SBrian Somers 7423a7b6d76SBrian Somers #ifdef TUNSIFHEAD 7433a7b6d76SBrian Somers /* We want the address family please ! */ 7443a7b6d76SBrian Somers iff = 1; 7453a7b6d76SBrian Somers if (ID0ioctl(bundle.dev.fd, TUNSIFHEAD, &iff) < 0) { 7463a7b6d76SBrian Somers log_Printf(LogERROR, "bundle_Create: ioctl(TUNSIFHEAD): %s\n", 7473a7b6d76SBrian Somers strerror(errno)); 7483a7b6d76SBrian Somers bundle.dev.header = 0; 7493a7b6d76SBrian Somers } else 7503a7b6d76SBrian Somers bundle.dev.header = 1; 7513a7b6d76SBrian Somers #else 7523a7b6d76SBrian Somers #ifdef __OpenBSD__ 7533a7b6d76SBrian Somers /* Always present for OpenBSD */ 7543a7b6d76SBrian Somers bundle.dev.header = 1; 7553a7b6d76SBrian Somers #else 7563a7b6d76SBrian Somers /* 7573a7b6d76SBrian Somers * If TUNSIFHEAD isn't available and we're not OpenBSD, assume 7583a7b6d76SBrian Somers * everything's AF_INET (hopefully the tun device won't pass us 7593a7b6d76SBrian Somers * anything else !). 7603a7b6d76SBrian Somers */ 7613a7b6d76SBrian Somers bundle.dev.header = 0; 7623a7b6d76SBrian Somers #endif 7633a7b6d76SBrian Somers #endif 7643a7b6d76SBrian Somers 7654e5196e9SBrian Somers if (!iface_SetFlags(bundle.iface, IFF_UP)) { 7668fa6ebe4SBrian Somers iface_Destroy(bundle.iface); 7678fa6ebe4SBrian Somers bundle.iface = NULL; 768faefde08SBrian Somers close(bundle.dev.fd); 7697a6f8720SBrian Somers return NULL; 7707a6f8720SBrian Somers } 7717a6f8720SBrian Somers 7728fa6ebe4SBrian Somers log_Printf(LogPHASE, "Using interface: %s\n", ifname); 7737a6f8720SBrian Somers 774ab2de065SBrian Somers bundle.bandwidth = 0; 775820de6ebSBrian Somers bundle.routing_seq = 0; 776a0cbd833SBrian Somers bundle.phase = PHASE_DEAD; 777a0cbd833SBrian Somers bundle.CleaningUp = 0; 77867b072f7SBrian Somers bundle.NatEnabled = 0; 7797a6f8720SBrian Somers 7806d666775SBrian Somers bundle.fsm.LayerStart = bundle_LayerStart; 7816f384573SBrian Somers bundle.fsm.LayerUp = bundle_LayerUp; 7826d666775SBrian Somers bundle.fsm.LayerDown = bundle_LayerDown; 7836d666775SBrian Somers bundle.fsm.LayerFinish = bundle_LayerFinish; 7846d666775SBrian Somers bundle.fsm.object = &bundle; 7857a6f8720SBrian Somers 786dade2407SBrian Somers bundle.cfg.idle.timeout = NCP_IDLE_TIMEOUT; 787dade2407SBrian Somers bundle.cfg.idle.min_timeout = 0; 7881342caedSBrian Somers *bundle.cfg.auth.name = '\0'; 7891342caedSBrian Somers *bundle.cfg.auth.key = '\0'; 790610b185fSBrian Somers bundle.cfg.opt = OPT_SROUTES | OPT_IDCHECK | OPT_LOOPBACK | 791610b185fSBrian Somers OPT_THROUGHPUT | OPT_UTMP; 79249052c95SBrian Somers *bundle.cfg.label = '\0'; 79349052c95SBrian Somers bundle.cfg.mtu = DEF_MTU; 7946f8e9f0aSBrian Somers bundle.cfg.choked.timeout = CHOKED_TIMEOUT; 795ff0f9439SBrian Somers bundle.phys_type.all = type; 796ff0f9439SBrian Somers bundle.phys_type.open = 0; 797dade2407SBrian Somers bundle.upat = 0; 798ab886ad0SBrian Somers 7996f384573SBrian Somers bundle.links = datalink_Create("deflink", &bundle, type); 8003006ec67SBrian Somers if (bundle.links == NULL) { 801a33b2ef7SBrian Somers log_Printf(LogALERT, "Cannot create data link: %s\n", strerror(errno)); 8028fa6ebe4SBrian Somers iface_Destroy(bundle.iface); 8038fa6ebe4SBrian Somers bundle.iface = NULL; 804faefde08SBrian Somers close(bundle.dev.fd); 8052289f246SBrian Somers return NULL; 8062289f246SBrian Somers } 8072289f246SBrian Somers 8082f786681SBrian Somers bundle.desc.type = BUNDLE_DESCRIPTOR; 8092f786681SBrian Somers bundle.desc.UpdateSet = bundle_UpdateSet; 8102f786681SBrian Somers bundle.desc.IsSet = bundle_IsSet; 8112f786681SBrian Somers bundle.desc.Read = bundle_DescriptorRead; 8122f786681SBrian Somers bundle.desc.Write = bundle_DescriptorWrite; 8132f786681SBrian Somers 81449052c95SBrian Somers mp_Init(&bundle.ncp.mp, &bundle); 81549052c95SBrian Somers 81649052c95SBrian Somers /* Send over the first physical link by default */ 8175828db6dSBrian Somers ipcp_Init(&bundle.ncp.ipcp, &bundle, &bundle.links->physical->link, 8185828db6dSBrian Somers &bundle.fsm); 8196d666775SBrian Somers 8205ca5389aSBrian Somers memset(&bundle.filter, '\0', sizeof bundle.filter); 8215ca5389aSBrian Somers bundle.filter.in.fragok = bundle.filter.in.logok = 1; 8225ca5389aSBrian Somers bundle.filter.in.name = "IN"; 8235ca5389aSBrian Somers bundle.filter.out.fragok = bundle.filter.out.logok = 1; 8245ca5389aSBrian Somers bundle.filter.out.name = "OUT"; 8255ca5389aSBrian Somers bundle.filter.dial.name = "DIAL"; 8268390b576SBrian Somers bundle.filter.dial.logok = 1; 8275ca5389aSBrian Somers bundle.filter.alive.name = "ALIVE"; 8285ca5389aSBrian Somers bundle.filter.alive.logok = 1; 829cad7e742SBrian Somers { 830cad7e742SBrian Somers int i; 831cad7e742SBrian Somers for (i = 0; i < MAXFILTERS; i++) { 832cad7e742SBrian Somers bundle.filter.in.rule[i].f_action = A_NONE; 833cad7e742SBrian Somers bundle.filter.out.rule[i].f_action = A_NONE; 834cad7e742SBrian Somers bundle.filter.dial.rule[i].f_action = A_NONE; 835cad7e742SBrian Somers bundle.filter.alive.rule[i].f_action = A_NONE; 836cad7e742SBrian Somers } 837cad7e742SBrian Somers } 83893ee0ff2SBrian Somers memset(&bundle.idle.timer, '\0', sizeof bundle.idle.timer); 83993ee0ff2SBrian Somers bundle.idle.done = 0; 8405cf4388bSBrian Somers bundle.notify.fd = -1; 8416f8e9f0aSBrian Somers memset(&bundle.choked.timer, '\0', sizeof bundle.choked.timer); 842972a1bcfSBrian Somers #ifndef NORADIUS 843972a1bcfSBrian Somers radius_Init(&bundle.radius); 844972a1bcfSBrian Somers #endif 84593ee0ff2SBrian Somers 8466d666775SBrian Somers /* Clean out any leftover crud */ 8478fa6ebe4SBrian Somers iface_Clear(bundle.iface, IFACE_CLEAR_ALL); 8486d666775SBrian Somers 8491384bd27SBrian Somers bundle_LockTun(&bundle); 8501384bd27SBrian Somers 8517a6f8720SBrian Somers return &bundle; 8527a6f8720SBrian Somers } 8537a6f8720SBrian Somers 85468a0f0ccSBrian Somers static void 85568a0f0ccSBrian Somers bundle_DownInterface(struct bundle *bundle) 85668a0f0ccSBrian Somers { 857dd7e2610SBrian Somers route_IfDelete(bundle, 1); 8584e5196e9SBrian Somers iface_ClearFlags(bundle->iface, IFF_UP); 85968a0f0ccSBrian Somers } 86068a0f0ccSBrian Somers 86168a0f0ccSBrian Somers void 86268a0f0ccSBrian Somers bundle_Destroy(struct bundle *bundle) 86368a0f0ccSBrian Somers { 8643006ec67SBrian Somers struct datalink *dl; 865b6217683SBrian Somers 866ea722969SBrian Somers /* 86704eaa58cSBrian Somers * Clean up the interface. We don't need to timer_Stop()s, mp_Down(), 868ea722969SBrian Somers * ipcp_CleanInterface() and bundle_DownInterface() unless we're getting 8698e7bd08eSBrian Somers * out under exceptional conditions such as a descriptor exception. 870ea722969SBrian Somers */ 87104eaa58cSBrian Somers timer_Stop(&bundle->idle.timer); 8726f8e9f0aSBrian Somers timer_Stop(&bundle->choked.timer); 87366f634b6SBrian Somers mp_Down(&bundle->ncp.mp); 874dd7e2610SBrian Somers ipcp_CleanInterface(&bundle->ncp.ipcp); 87568a0f0ccSBrian Somers bundle_DownInterface(bundle); 8763006ec67SBrian Somers 877972a1bcfSBrian Somers #ifndef NORADIUS 878972a1bcfSBrian Somers /* Tell the radius server the bad news */ 879972a1bcfSBrian Somers radius_Destroy(&bundle->radius); 880972a1bcfSBrian Somers #endif 881972a1bcfSBrian Somers 882ea722969SBrian Somers /* Again, these are all DATALINK_CLOSED unless we're abending */ 8833006ec67SBrian Somers dl = bundle->links; 8843006ec67SBrian Somers while (dl) 8853006ec67SBrian Somers dl = datalink_Destroy(dl); 8863006ec67SBrian Somers 887442f8495SBrian Somers ipcp_Destroy(&bundle->ncp.ipcp); 888442f8495SBrian Somers 8891384bd27SBrian Somers close(bundle->dev.fd); 8901384bd27SBrian Somers bundle_UnlockTun(bundle); 8911384bd27SBrian Somers 892ea722969SBrian Somers /* In case we never made PHASE_NETWORK */ 8935cf4388bSBrian Somers bundle_Notify(bundle, EX_ERRDEAD); 894b6217683SBrian Somers 8958fa6ebe4SBrian Somers iface_Destroy(bundle->iface); 8968fa6ebe4SBrian Somers bundle->iface = NULL; 89768a0f0ccSBrian Somers } 89868a0f0ccSBrian Somers 8997a6f8720SBrian Somers struct rtmsg { 9007a6f8720SBrian Somers struct rt_msghdr m_rtm; 9017a6f8720SBrian Somers char m_space[64]; 9027a6f8720SBrian Somers }; 9037a6f8720SBrian Somers 904610b185fSBrian Somers int 905820de6ebSBrian Somers bundle_SetRoute(struct bundle *bundle, int cmd, struct in_addr dst, 9062062443fSBrian Somers struct in_addr gateway, struct in_addr mask, int bang, int ssh) 9077a6f8720SBrian Somers { 9087a6f8720SBrian Somers struct rtmsg rtmes; 9097a6f8720SBrian Somers int s, nb, wb; 9107a6f8720SBrian Somers char *cp; 9117a6f8720SBrian Somers const char *cmdstr; 9127a6f8720SBrian Somers struct sockaddr_in rtdata; 913610b185fSBrian Somers int result = 1; 9147a6f8720SBrian Somers 9157a6f8720SBrian Somers if (bang) 9167a6f8720SBrian Somers cmdstr = (cmd == RTM_ADD ? "Add!" : "Delete!"); 9177a6f8720SBrian Somers else 9187a6f8720SBrian Somers cmdstr = (cmd == RTM_ADD ? "Add" : "Delete"); 9197a6f8720SBrian Somers s = ID0socket(PF_ROUTE, SOCK_RAW, 0); 9207a6f8720SBrian Somers if (s < 0) { 921dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_SetRoute: socket(): %s\n", strerror(errno)); 922610b185fSBrian Somers return result; 9237a6f8720SBrian Somers } 9247a6f8720SBrian Somers memset(&rtmes, '\0', sizeof rtmes); 9257a6f8720SBrian Somers rtmes.m_rtm.rtm_version = RTM_VERSION; 9267a6f8720SBrian Somers rtmes.m_rtm.rtm_type = cmd; 9277a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs = RTA_DST; 928820de6ebSBrian Somers rtmes.m_rtm.rtm_seq = ++bundle->routing_seq; 9297a6f8720SBrian Somers rtmes.m_rtm.rtm_pid = getpid(); 9307a6f8720SBrian Somers rtmes.m_rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC; 9317a6f8720SBrian Somers 9323afe5ccbSBrian Somers if (cmd == RTM_ADD || cmd == RTM_CHANGE) { 9333afe5ccbSBrian Somers if (bundle->ncp.ipcp.cfg.sendpipe > 0) { 9343afe5ccbSBrian Somers rtmes.m_rtm.rtm_rmx.rmx_sendpipe = bundle->ncp.ipcp.cfg.sendpipe; 9353afe5ccbSBrian Somers rtmes.m_rtm.rtm_inits |= RTV_SPIPE; 9363afe5ccbSBrian Somers } 9373afe5ccbSBrian Somers if (bundle->ncp.ipcp.cfg.recvpipe > 0) { 9383afe5ccbSBrian Somers rtmes.m_rtm.rtm_rmx.rmx_recvpipe = bundle->ncp.ipcp.cfg.recvpipe; 9393afe5ccbSBrian Somers rtmes.m_rtm.rtm_inits |= RTV_RPIPE; 9403afe5ccbSBrian Somers } 9413afe5ccbSBrian Somers } 9423afe5ccbSBrian Somers 9437a6f8720SBrian Somers memset(&rtdata, '\0', sizeof rtdata); 94450e5c17dSBrian Somers rtdata.sin_len = sizeof rtdata; 9457a6f8720SBrian Somers rtdata.sin_family = AF_INET; 9467a6f8720SBrian Somers rtdata.sin_port = 0; 9477a6f8720SBrian Somers rtdata.sin_addr = dst; 9487a6f8720SBrian Somers 9497a6f8720SBrian Somers cp = rtmes.m_space; 95050e5c17dSBrian Somers memcpy(cp, &rtdata, rtdata.sin_len); 95150e5c17dSBrian Somers cp += rtdata.sin_len; 952e43ebac1SBrian Somers if (cmd == RTM_ADD) { 9537a6f8720SBrian Somers if (gateway.s_addr == INADDR_ANY) { 9540aa8aa17SBrian Somers if (!ssh) 9558fa6ebe4SBrian Somers log_Printf(LogERROR, "bundle_SetRoute: Cannot add a route with" 9568fa6ebe4SBrian Somers " destination 0.0.0.0\n"); 9578fa6ebe4SBrian Somers close(s); 9588fa6ebe4SBrian Somers return result; 9597a6f8720SBrian Somers } else { 9607a6f8720SBrian Somers rtdata.sin_addr = gateway; 96150e5c17dSBrian Somers memcpy(cp, &rtdata, rtdata.sin_len); 96250e5c17dSBrian Somers cp += rtdata.sin_len; 9637a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY; 9647a6f8720SBrian Somers } 965e43ebac1SBrian Somers } 9667a6f8720SBrian Somers 9677a6f8720SBrian Somers if (dst.s_addr == INADDR_ANY) 9687a6f8720SBrian Somers mask.s_addr = INADDR_ANY; 9697a6f8720SBrian Somers 9707a6f8720SBrian Somers if (cmd == RTM_ADD || dst.s_addr == INADDR_ANY) { 9717a6f8720SBrian Somers rtdata.sin_addr = mask; 97250e5c17dSBrian Somers memcpy(cp, &rtdata, rtdata.sin_len); 97350e5c17dSBrian Somers cp += rtdata.sin_len; 9747a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs |= RTA_NETMASK; 9757a6f8720SBrian Somers } 9767a6f8720SBrian Somers 9777a6f8720SBrian Somers nb = cp - (char *) &rtmes; 9787a6f8720SBrian Somers rtmes.m_rtm.rtm_msglen = nb; 9797a6f8720SBrian Somers wb = ID0write(s, &rtmes, nb); 9807a6f8720SBrian Somers if (wb < 0) { 981dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute failure:\n"); 982dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Cmd = %s\n", cmdstr); 983dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Dst = %s\n", inet_ntoa(dst)); 9842cb305afSBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Gateway = %s\n", 9852cb305afSBrian Somers inet_ntoa(gateway)); 986dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Mask = %s\n", inet_ntoa(mask)); 9877a6f8720SBrian Somers failed: 9887a6f8720SBrian Somers if (cmd == RTM_ADD && (rtmes.m_rtm.rtm_errno == EEXIST || 989e43ebac1SBrian Somers (rtmes.m_rtm.rtm_errno == 0 && errno == EEXIST))) { 990610b185fSBrian Somers if (!bang) { 991dd7e2610SBrian Somers log_Printf(LogWARN, "Add route failed: %s already exists\n", 9923afe5ccbSBrian Somers dst.s_addr == 0 ? "default" : inet_ntoa(dst)); 993610b185fSBrian Somers result = 0; /* Don't add to our dynamic list */ 994610b185fSBrian Somers } else { 9957a6f8720SBrian Somers rtmes.m_rtm.rtm_type = cmd = RTM_CHANGE; 9967a6f8720SBrian Somers if ((wb = ID0write(s, &rtmes, nb)) < 0) 9977a6f8720SBrian Somers goto failed; 9987a6f8720SBrian Somers } 999e43ebac1SBrian Somers } else if (cmd == RTM_DELETE && 10007a6f8720SBrian Somers (rtmes.m_rtm.rtm_errno == ESRCH || 10017a6f8720SBrian Somers (rtmes.m_rtm.rtm_errno == 0 && errno == ESRCH))) { 10027a6f8720SBrian Somers if (!bang) 1003dd7e2610SBrian Somers log_Printf(LogWARN, "Del route failed: %s: Non-existent\n", 10047a6f8720SBrian Somers inet_ntoa(dst)); 10052062443fSBrian Somers } else if (rtmes.m_rtm.rtm_errno == 0) { 10062062443fSBrian Somers if (!ssh || errno != ENETUNREACH) 1007dd7e2610SBrian Somers log_Printf(LogWARN, "%s route failed: %s: errno: %s\n", cmdstr, 10087a6f8720SBrian Somers inet_ntoa(dst), strerror(errno)); 10092062443fSBrian Somers } else 1010dd7e2610SBrian Somers log_Printf(LogWARN, "%s route failed: %s: %s\n", 10117a6f8720SBrian Somers cmdstr, inet_ntoa(dst), strerror(rtmes.m_rtm.rtm_errno)); 10127a6f8720SBrian Somers } 1013dd7e2610SBrian Somers log_Printf(LogDEBUG, "wrote %d: cmd = %s, dst = %x, gateway = %x\n", 1014fe3125a0SBrian Somers wb, cmdstr, (unsigned)dst.s_addr, (unsigned)gateway.s_addr); 10157a6f8720SBrian Somers close(s); 1016610b185fSBrian Somers 1017610b185fSBrian Somers return result; 10187a6f8720SBrian Somers } 101983d1af55SBrian Somers 102083d1af55SBrian Somers void 10213006ec67SBrian Somers bundle_LinkClosed(struct bundle *bundle, struct datalink *dl) 10223006ec67SBrian Somers { 10233006ec67SBrian Somers /* 10243006ec67SBrian Somers * Our datalink has closed. 1025ea722969SBrian Somers * CleanDatalinks() (called from DoLoop()) will remove closed 1026f6a4e748SBrian Somers * BACKGROUND, FOREGROUND and DIRECT links. 10273b0f8d2eSBrian Somers * If it's the last data link, enter phase DEAD. 1028ea722969SBrian Somers * 1029ea722969SBrian Somers * NOTE: dl may not be in our list (bundle_SendDatalink()) ! 10303006ec67SBrian Somers */ 10315b8b8060SBrian Somers 10323b0f8d2eSBrian Somers struct datalink *odl; 10333b0f8d2eSBrian Somers int other_links; 10345b8b8060SBrian Somers 1035bf1d3ff6SBrian Somers log_SetTtyCommandMode(dl); 1036bf1d3ff6SBrian Somers 10373b0f8d2eSBrian Somers other_links = 0; 10383b0f8d2eSBrian Somers for (odl = bundle->links; odl; odl = odl->next) 10393b0f8d2eSBrian Somers if (odl != dl && odl->state != DATALINK_CLOSED) 10403b0f8d2eSBrian Somers other_links++; 10413b0f8d2eSBrian Somers 10423b0f8d2eSBrian Somers if (!other_links) { 104381358fa3SBrian Somers if (dl->physical->type != PHYS_AUTO) /* Not in -auto mode */ 104403704096SBrian Somers bundle_DownInterface(bundle); 104509206a6fSBrian Somers fsm2initial(&bundle->ncp.ipcp.fsm); 10465563ebdeSBrian Somers bundle_NewPhase(bundle, PHASE_DEAD); 10470f2f3eb3SBrian Somers bundle_StopIdleTimer(bundle); 1048ab2de065SBrian Somers } 1049455aabc3SBrian Somers } 1050455aabc3SBrian Somers 1051455aabc3SBrian Somers void 1052ba23f397SBrian Somers bundle_Open(struct bundle *bundle, const char *name, int mask, int force) 10533006ec67SBrian Somers { 10543006ec67SBrian Somers /* 10553006ec67SBrian Somers * Please open the given datalink, or all if name == NULL 10563006ec67SBrian Somers */ 10573006ec67SBrian Somers struct datalink *dl; 10583006ec67SBrian Somers 10593006ec67SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 10603006ec67SBrian Somers if (name == NULL || !strcasecmp(dl->name, name)) { 1061ba23f397SBrian Somers if ((mask & dl->physical->type) && 1062ba23f397SBrian Somers (dl->state == DATALINK_CLOSED || 1063ba23f397SBrian Somers (force && dl->state == DATALINK_OPENING && 1064c11e57a3SBrian Somers dl->dial.timer.state == TIMER_RUNNING))) { 1065c11e57a3SBrian Somers if (force) /* Ignore redial timeout ? */ 1066c11e57a3SBrian Somers timer_Stop(&dl->dial.timer); 1067565e35e5SBrian Somers datalink_Up(dl, 1, 1); 1068ab2de065SBrian Somers if (mask & PHYS_AUTO) 1069ab2de065SBrian Somers /* Only one AUTO link at a time */ 107004eaa58cSBrian Somers break; 107104eaa58cSBrian Somers } 10723006ec67SBrian Somers if (name != NULL) 10733006ec67SBrian Somers break; 10743006ec67SBrian Somers } 10753006ec67SBrian Somers } 10763006ec67SBrian Somers 10773006ec67SBrian Somers struct datalink * 10783006ec67SBrian Somers bundle2datalink(struct bundle *bundle, const char *name) 10793006ec67SBrian Somers { 10803006ec67SBrian Somers struct datalink *dl; 10813006ec67SBrian Somers 10823006ec67SBrian Somers if (name != NULL) { 10833006ec67SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 10843006ec67SBrian Somers if (!strcasecmp(dl->name, name)) 10853006ec67SBrian Somers return dl; 10863006ec67SBrian Somers } else if (bundle->links && !bundle->links->next) 10873006ec67SBrian Somers return bundle->links; 10883006ec67SBrian Somers 10893006ec67SBrian Somers return NULL; 10903006ec67SBrian Somers } 10913006ec67SBrian Somers 10923006ec67SBrian Somers int 1093aef795ccSBrian Somers bundle_ShowLinks(struct cmdargs const *arg) 1094aef795ccSBrian Somers { 1095aef795ccSBrian Somers struct datalink *dl; 1096ab2de065SBrian Somers struct pppThroughput *t; 1097ab2de065SBrian Somers int secs; 1098aef795ccSBrian Somers 10999c53a7b1SBrian Somers for (dl = arg->bundle->links; dl; dl = dl->next) { 1100d4156d00SBrian Somers prompt_Printf(arg->prompt, "Name: %s [%s, %s]", 1101d4156d00SBrian Somers dl->name, mode2Nam(dl->physical->type), datalink_State(dl)); 1102eeab6bf5SBrian Somers if (dl->physical->link.throughput.rolling && dl->state == DATALINK_OPEN) 1103e531f89aSBrian Somers prompt_Printf(arg->prompt, " bandwidth %d, %llu bps (%llu bytes/sec)", 1104ab2de065SBrian Somers dl->mp.bandwidth ? dl->mp.bandwidth : 1105ab2de065SBrian Somers physical_GetSpeed(dl->physical), 1106ab2de065SBrian Somers dl->physical->link.throughput.OctetsPerSecond * 8, 11079c53a7b1SBrian Somers dl->physical->link.throughput.OctetsPerSecond); 11089c53a7b1SBrian Somers prompt_Printf(arg->prompt, "\n"); 11099c53a7b1SBrian Somers } 1110aef795ccSBrian Somers 1111ab2de065SBrian Somers t = &arg->bundle->ncp.mp.link.throughput; 1112ab2de065SBrian Somers secs = t->downtime ? 0 : throughput_uptime(t); 1113ab2de065SBrian Somers if (secs > t->SamplePeriod) 1114ab2de065SBrian Somers secs = t->SamplePeriod; 1115ab2de065SBrian Somers if (secs) 1116e531f89aSBrian Somers prompt_Printf(arg->prompt, "Currently averaging %llu bps (%llu bytes/sec)" 1117ab2de065SBrian Somers " over the last %d secs\n", t->OctetsPerSecond * 8, 1118ab2de065SBrian Somers t->OctetsPerSecond, secs); 1119ab2de065SBrian Somers 1120aef795ccSBrian Somers return 0; 1121aef795ccSBrian Somers } 1122ab886ad0SBrian Somers 11231342caedSBrian Somers static const char * 11241342caedSBrian Somers optval(struct bundle *bundle, int bit) 11251342caedSBrian Somers { 11261342caedSBrian Somers return (bundle->cfg.opt & bit) ? "enabled" : "disabled"; 11271342caedSBrian Somers } 11281342caedSBrian Somers 1129c08717dfSBrian Somers int 1130c08717dfSBrian Somers bundle_ShowStatus(struct cmdargs const *arg) 1131c08717dfSBrian Somers { 1132c08717dfSBrian Somers int remaining; 1133c08717dfSBrian Somers 1134c08717dfSBrian Somers prompt_Printf(arg->prompt, "Phase %s\n", bundle_PhaseName(arg->bundle)); 1135faefde08SBrian Somers prompt_Printf(arg->prompt, " Device: %s\n", arg->bundle->dev.Name); 1136dade2407SBrian Somers prompt_Printf(arg->prompt, " Interface: %s @ %lubps", 1137ab2de065SBrian Somers arg->bundle->iface->name, arg->bundle->bandwidth); 1138c08717dfSBrian Somers 1139dade2407SBrian Somers if (arg->bundle->upat) { 1140dade2407SBrian Somers int secs = time(NULL) - arg->bundle->upat; 1141dade2407SBrian Somers 1142dade2407SBrian Somers prompt_Printf(arg->prompt, ", up time %d:%02d:%02d", secs / 3600, 1143dade2407SBrian Somers (secs / 60) % 60, secs % 60); 1144dade2407SBrian Somers } 1145dade2407SBrian Somers 1146dade2407SBrian Somers prompt_Printf(arg->prompt, "\n\nDefaults:\n"); 1147643f4904SBrian Somers prompt_Printf(arg->prompt, " Label: %s\n", arg->bundle->cfg.label); 1148610b185fSBrian Somers prompt_Printf(arg->prompt, " Auth name: %s\n", 1149610b185fSBrian Somers arg->bundle->cfg.auth.name); 115004eaa58cSBrian Somers 11516f8e9f0aSBrian Somers prompt_Printf(arg->prompt, " Choked Timer: %ds\n", 11526f8e9f0aSBrian Somers arg->bundle->cfg.choked.timeout); 1153972a1bcfSBrian Somers 1154972a1bcfSBrian Somers #ifndef NORADIUS 1155972a1bcfSBrian Somers radius_Show(&arg->bundle->radius, arg->prompt); 1156972a1bcfSBrian Somers #endif 1157972a1bcfSBrian Somers 1158c08717dfSBrian Somers prompt_Printf(arg->prompt, " Idle Timer: "); 1159dade2407SBrian Somers if (arg->bundle->cfg.idle.timeout) { 1160dade2407SBrian Somers prompt_Printf(arg->prompt, "%ds", arg->bundle->cfg.idle.timeout); 1161dade2407SBrian Somers if (arg->bundle->cfg.idle.min_timeout) 1162dade2407SBrian Somers prompt_Printf(arg->prompt, ", min %ds", 1163dade2407SBrian Somers arg->bundle->cfg.idle.min_timeout); 1164c08717dfSBrian Somers remaining = bundle_RemainingIdleTime(arg->bundle); 1165c08717dfSBrian Somers if (remaining != -1) 1166c08717dfSBrian Somers prompt_Printf(arg->prompt, " (%ds remaining)", remaining); 1167c08717dfSBrian Somers prompt_Printf(arg->prompt, "\n"); 1168c08717dfSBrian Somers } else 1169c08717dfSBrian Somers prompt_Printf(arg->prompt, "disabled\n"); 1170ce828a6eSBrian Somers prompt_Printf(arg->prompt, " MTU: "); 1171ce828a6eSBrian Somers if (arg->bundle->cfg.mtu) 1172ce828a6eSBrian Somers prompt_Printf(arg->prompt, "%d\n", arg->bundle->cfg.mtu); 1173ce828a6eSBrian Somers else 1174ce828a6eSBrian Somers prompt_Printf(arg->prompt, "unspecified\n"); 1175c08717dfSBrian Somers 11763afe5ccbSBrian Somers prompt_Printf(arg->prompt, " sendpipe: "); 11773afe5ccbSBrian Somers if (arg->bundle->ncp.ipcp.cfg.sendpipe > 0) 1178ab2de065SBrian Somers prompt_Printf(arg->prompt, "%-20ld", arg->bundle->ncp.ipcp.cfg.sendpipe); 11793afe5ccbSBrian Somers else 1180ab2de065SBrian Somers prompt_Printf(arg->prompt, "unspecified "); 11813afe5ccbSBrian Somers prompt_Printf(arg->prompt, " recvpipe: "); 11823afe5ccbSBrian Somers if (arg->bundle->ncp.ipcp.cfg.recvpipe > 0) 11833afe5ccbSBrian Somers prompt_Printf(arg->prompt, "%ld\n", arg->bundle->ncp.ipcp.cfg.recvpipe); 11843afe5ccbSBrian Somers else 11853afe5ccbSBrian Somers prompt_Printf(arg->prompt, "unspecified\n"); 11863afe5ccbSBrian Somers 1187ab2de065SBrian Somers prompt_Printf(arg->prompt, " Sticky Routes: %-20.20s", 1188610b185fSBrian Somers optval(arg->bundle, OPT_SROUTES)); 11891342caedSBrian Somers prompt_Printf(arg->prompt, " ID check: %s\n", 11901342caedSBrian Somers optval(arg->bundle, OPT_IDCHECK)); 1191ab2de065SBrian Somers prompt_Printf(arg->prompt, " Keep-Session: %-20.20s", 1192ac685e31SBrian Somers optval(arg->bundle, OPT_KEEPSESSION)); 11931342caedSBrian Somers prompt_Printf(arg->prompt, " Loopback: %s\n", 11941342caedSBrian Somers optval(arg->bundle, OPT_LOOPBACK)); 1195ab2de065SBrian Somers prompt_Printf(arg->prompt, " PasswdAuth: %-20.20s", 11961342caedSBrian Somers optval(arg->bundle, OPT_PASSWDAUTH)); 11971342caedSBrian Somers prompt_Printf(arg->prompt, " Proxy: %s\n", 11981342caedSBrian Somers optval(arg->bundle, OPT_PROXY)); 1199ab2de065SBrian Somers prompt_Printf(arg->prompt, " Proxyall: %-20.20s", 12003afe5ccbSBrian Somers optval(arg->bundle, OPT_PROXYALL)); 12011342caedSBrian Somers prompt_Printf(arg->prompt, " Throughput: %s\n", 12021342caedSBrian Somers optval(arg->bundle, OPT_THROUGHPUT)); 1203ab2de065SBrian Somers prompt_Printf(arg->prompt, " Utmp Logging: %-20.20s", 12041342caedSBrian Somers optval(arg->bundle, OPT_UTMP)); 12058fa6ebe4SBrian Somers prompt_Printf(arg->prompt, " Iface-Alias: %s\n", 12068fa6ebe4SBrian Somers optval(arg->bundle, OPT_IFACEALIAS)); 12071342caedSBrian Somers 1208c08717dfSBrian Somers return 0; 1209c08717dfSBrian Somers } 1210c08717dfSBrian Somers 1211ab886ad0SBrian Somers static void 1212ab886ad0SBrian Somers bundle_IdleTimeout(void *v) 1213ab886ad0SBrian Somers { 1214ab886ad0SBrian Somers struct bundle *bundle = (struct bundle *)v; 1215ab886ad0SBrian Somers 1216dd7e2610SBrian Somers log_Printf(LogPHASE, "Idle timer expired.\n"); 121704eaa58cSBrian Somers bundle_StopIdleTimer(bundle); 12189c81b87dSBrian Somers bundle_Close(bundle, NULL, CLOSE_STAYDOWN); 1219ab886ad0SBrian Somers } 1220ab886ad0SBrian Somers 1221ab886ad0SBrian Somers /* 1222ab886ad0SBrian Somers * Start Idle timer. If timeout is reached, we call bundle_Close() to 1223ab886ad0SBrian Somers * close LCP and link. 1224ab886ad0SBrian Somers */ 1225ab886ad0SBrian Somers void 1226ab886ad0SBrian Somers bundle_StartIdleTimer(struct bundle *bundle) 1227ab886ad0SBrian Somers { 1228dd7e2610SBrian Somers timer_Stop(&bundle->idle.timer); 1229ff0f9439SBrian Somers if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) != 1230dade2407SBrian Somers bundle->phys_type.open && bundle->cfg.idle.timeout) { 1231dade2407SBrian Somers int secs; 1232dade2407SBrian Somers 1233dade2407SBrian Somers secs = bundle->cfg.idle.timeout; 1234dade2407SBrian Somers if (bundle->cfg.idle.min_timeout > secs && bundle->upat) { 1235dade2407SBrian Somers int up = time(NULL) - bundle->upat; 1236dade2407SBrian Somers 1237dade2407SBrian Somers if ((long long)bundle->cfg.idle.min_timeout - up > (long long)secs) 1238dade2407SBrian Somers secs = bundle->cfg.idle.min_timeout - up; 1239dade2407SBrian Somers } 124093ee0ff2SBrian Somers bundle->idle.timer.func = bundle_IdleTimeout; 12413b0f8d2eSBrian Somers bundle->idle.timer.name = "idle"; 1242dade2407SBrian Somers bundle->idle.timer.load = secs * SECTICKS; 124393ee0ff2SBrian Somers bundle->idle.timer.arg = bundle; 1244dd7e2610SBrian Somers timer_Start(&bundle->idle.timer); 1245dade2407SBrian Somers bundle->idle.done = time(NULL) + secs; 1246ab886ad0SBrian Somers } 1247ab886ad0SBrian Somers } 1248ab886ad0SBrian Somers 1249ab886ad0SBrian Somers void 1250dade2407SBrian Somers bundle_SetIdleTimer(struct bundle *bundle, int timeout, int min_timeout) 1251ab886ad0SBrian Somers { 1252dade2407SBrian Somers bundle->cfg.idle.timeout = timeout; 1253dade2407SBrian Somers if (min_timeout >= 0) 1254dade2407SBrian Somers bundle->cfg.idle.min_timeout = min_timeout; 1255ab886ad0SBrian Somers if (bundle_LinkIsUp(bundle)) 1256ab886ad0SBrian Somers bundle_StartIdleTimer(bundle); 1257ab886ad0SBrian Somers } 1258ab886ad0SBrian Somers 1259ab886ad0SBrian Somers void 1260ab886ad0SBrian Somers bundle_StopIdleTimer(struct bundle *bundle) 1261ab886ad0SBrian Somers { 1262dd7e2610SBrian Somers timer_Stop(&bundle->idle.timer); 12634a632c80SBrian Somers bundle->idle.done = 0; 1264ab886ad0SBrian Somers } 1265ab886ad0SBrian Somers 126604eaa58cSBrian Somers static int 1267ab886ad0SBrian Somers bundle_RemainingIdleTime(struct bundle *bundle) 1268ab886ad0SBrian Somers { 126993ee0ff2SBrian Somers if (bundle->idle.done) 127093ee0ff2SBrian Somers return bundle->idle.done - time(NULL); 1271ab886ad0SBrian Somers return -1; 1272ab886ad0SBrian Somers } 12733b0f8d2eSBrian Somers 12743b0f8d2eSBrian Somers int 12753b0f8d2eSBrian Somers bundle_IsDead(struct bundle *bundle) 12763b0f8d2eSBrian Somers { 12773b0f8d2eSBrian Somers return !bundle->links || (bundle->phase == PHASE_DEAD && bundle->CleaningUp); 12783b0f8d2eSBrian Somers } 1279b6217683SBrian Somers 128004eaa58cSBrian Somers static struct datalink * 128104eaa58cSBrian Somers bundle_DatalinkLinkout(struct bundle *bundle, struct datalink *dl) 1282cd7bd93aSBrian Somers { 1283cd7bd93aSBrian Somers struct datalink **dlp; 1284cd7bd93aSBrian Somers 1285cd7bd93aSBrian Somers for (dlp = &bundle->links; *dlp; dlp = &(*dlp)->next) 1286cd7bd93aSBrian Somers if (*dlp == dl) { 128704eaa58cSBrian Somers *dlp = dl->next; 128804eaa58cSBrian Somers dl->next = NULL; 128904eaa58cSBrian Somers bundle_LinksRemoved(bundle); 129004eaa58cSBrian Somers return dl; 1291cd7bd93aSBrian Somers } 129204eaa58cSBrian Somers 129304eaa58cSBrian Somers return NULL; 129404eaa58cSBrian Somers } 129504eaa58cSBrian Somers 129604eaa58cSBrian Somers static void 129704eaa58cSBrian Somers bundle_DatalinkLinkin(struct bundle *bundle, struct datalink *dl) 129804eaa58cSBrian Somers { 129904eaa58cSBrian Somers struct datalink **dlp = &bundle->links; 130004eaa58cSBrian Somers 130104eaa58cSBrian Somers while (*dlp) 130204eaa58cSBrian Somers dlp = &(*dlp)->next; 130304eaa58cSBrian Somers 130404eaa58cSBrian Somers *dlp = dl; 130504eaa58cSBrian Somers dl->next = NULL; 130604eaa58cSBrian Somers 130704eaa58cSBrian Somers bundle_LinkAdded(bundle, dl); 1308ab2de065SBrian Somers mp_CheckAutoloadTimer(&bundle->ncp.mp); 1309565e35e5SBrian Somers } 1310565e35e5SBrian Somers 1311565e35e5SBrian Somers void 1312565e35e5SBrian Somers bundle_CleanDatalinks(struct bundle *bundle) 1313565e35e5SBrian Somers { 1314565e35e5SBrian Somers struct datalink **dlp = &bundle->links; 131504eaa58cSBrian Somers int found = 0; 1316565e35e5SBrian Somers 1317565e35e5SBrian Somers while (*dlp) 1318565e35e5SBrian Somers if ((*dlp)->state == DATALINK_CLOSED && 1319f6a4e748SBrian Somers (*dlp)->physical->type & 1320f6a4e748SBrian Somers (PHYS_DIRECT|PHYS_BACKGROUND|PHYS_FOREGROUND)) { 1321565e35e5SBrian Somers *dlp = datalink_Destroy(*dlp); 132204eaa58cSBrian Somers found++; 132304eaa58cSBrian Somers } else 1324565e35e5SBrian Somers dlp = &(*dlp)->next; 132504eaa58cSBrian Somers 132604eaa58cSBrian Somers if (found) 132704eaa58cSBrian Somers bundle_LinksRemoved(bundle); 132804eaa58cSBrian Somers } 132904eaa58cSBrian Somers 133004eaa58cSBrian Somers int 133104eaa58cSBrian Somers bundle_DatalinkClone(struct bundle *bundle, struct datalink *dl, 133204eaa58cSBrian Somers const char *name) 133304eaa58cSBrian Somers { 133404eaa58cSBrian Somers if (bundle2datalink(bundle, name)) { 133504eaa58cSBrian Somers log_Printf(LogWARN, "Clone: %s: name already exists\n", name); 133604eaa58cSBrian Somers return 0; 133704eaa58cSBrian Somers } 133804eaa58cSBrian Somers 133904eaa58cSBrian Somers bundle_DatalinkLinkin(bundle, datalink_Clone(dl, name)); 134004eaa58cSBrian Somers return 1; 134104eaa58cSBrian Somers } 134204eaa58cSBrian Somers 134304eaa58cSBrian Somers void 134404eaa58cSBrian Somers bundle_DatalinkRemove(struct bundle *bundle, struct datalink *dl) 134504eaa58cSBrian Somers { 134604eaa58cSBrian Somers dl = bundle_DatalinkLinkout(bundle, dl); 134704eaa58cSBrian Somers if (dl) 134804eaa58cSBrian Somers datalink_Destroy(dl); 1349cd7bd93aSBrian Somers } 135049052c95SBrian Somers 135149052c95SBrian Somers void 135249052c95SBrian Somers bundle_SetLabel(struct bundle *bundle, const char *label) 135349052c95SBrian Somers { 135449052c95SBrian Somers if (label) 135549052c95SBrian Somers strncpy(bundle->cfg.label, label, sizeof bundle->cfg.label - 1); 135649052c95SBrian Somers else 135749052c95SBrian Somers *bundle->cfg.label = '\0'; 135849052c95SBrian Somers } 135949052c95SBrian Somers 136049052c95SBrian Somers const char * 136149052c95SBrian Somers bundle_GetLabel(struct bundle *bundle) 136249052c95SBrian Somers { 136349052c95SBrian Somers return *bundle->cfg.label ? bundle->cfg.label : NULL; 136449052c95SBrian Somers } 13651fa665f5SBrian Somers 13662cb305afSBrian Somers int 13672cb305afSBrian Somers bundle_LinkSize() 13681fa665f5SBrian Somers { 136996c9bb21SBrian Somers struct iovec iov[SCATTER_SEGMENTS]; 13702cb305afSBrian Somers int niov, expect, f; 137187c3786eSBrian Somers 137296c9bb21SBrian Somers iov[0].iov_len = strlen(Version) + 1; 13732cb305afSBrian Somers iov[0].iov_base = NULL; 13742cb305afSBrian Somers niov = 1; 13752cb305afSBrian Somers if (datalink2iov(NULL, iov, &niov, SCATTER_SEGMENTS, NULL, NULL) == -1) { 13762cb305afSBrian Somers log_Printf(LogERROR, "Cannot determine space required for link\n"); 13772cb305afSBrian Somers return 0; 137854cd8e13SBrian Somers } 13796f384573SBrian Somers 138096c9bb21SBrian Somers for (f = expect = 0; f < niov; f++) 138196c9bb21SBrian Somers expect += iov[f].iov_len; 138296c9bb21SBrian Somers 13832cb305afSBrian Somers return expect; 138487c3786eSBrian Somers } 138596c9bb21SBrian Somers 13862cb305afSBrian Somers void 13872cb305afSBrian Somers bundle_ReceiveDatalink(struct bundle *bundle, int s) 13882cb305afSBrian Somers { 13892cb305afSBrian Somers char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int) * SEND_MAXFD]; 1390cbee9754SBrian Somers int niov, expect, f, *fd, nfd, onfd, got; 13912cb305afSBrian Somers struct iovec iov[SCATTER_SEGMENTS]; 13922cb305afSBrian Somers struct cmsghdr *cmsg; 13932cb305afSBrian Somers struct msghdr msg; 13942cb305afSBrian Somers struct datalink *dl; 13952cb305afSBrian Somers pid_t pid; 13962cb305afSBrian Somers 13972cb305afSBrian Somers log_Printf(LogPHASE, "Receiving datalink\n"); 13982cb305afSBrian Somers 13992cb305afSBrian Somers /* 14002cb305afSBrian Somers * Create our scatter/gather array - passing NULL gets the space 14012cb305afSBrian Somers * allocation requirement rather than actually flattening the 14022cb305afSBrian Somers * structures. 14032cb305afSBrian Somers */ 14042cb305afSBrian Somers iov[0].iov_len = strlen(Version) + 1; 14052cb305afSBrian Somers iov[0].iov_base = NULL; 14062cb305afSBrian Somers niov = 1; 14072cb305afSBrian Somers if (datalink2iov(NULL, iov, &niov, SCATTER_SEGMENTS, NULL, NULL) == -1) { 14082cb305afSBrian Somers log_Printf(LogERROR, "Cannot determine space required for link\n"); 14092cb305afSBrian Somers return; 14102cb305afSBrian Somers } 14112cb305afSBrian Somers 14122cb305afSBrian Somers /* Allocate the scatter/gather array for recvmsg() */ 14132cb305afSBrian Somers for (f = expect = 0; f < niov; f++) { 14142cb305afSBrian Somers if ((iov[f].iov_base = malloc(iov[f].iov_len)) == NULL) { 14152cb305afSBrian Somers log_Printf(LogERROR, "Cannot allocate space to receive link\n"); 14162cb305afSBrian Somers return; 14172cb305afSBrian Somers } 1418cbee9754SBrian Somers if (f) 14192cb305afSBrian Somers expect += iov[f].iov_len; 14202cb305afSBrian Somers } 14212cb305afSBrian Somers 14222cb305afSBrian Somers /* Set up our message */ 14232cb305afSBrian Somers cmsg = (struct cmsghdr *)cmsgbuf; 14242cb305afSBrian Somers cmsg->cmsg_len = sizeof cmsgbuf; 14252cb305afSBrian Somers cmsg->cmsg_level = SOL_SOCKET; 14262cb305afSBrian Somers cmsg->cmsg_type = 0; 14272cb305afSBrian Somers 142896c9bb21SBrian Somers memset(&msg, '\0', sizeof msg); 14292cb305afSBrian Somers msg.msg_name = NULL; 14302cb305afSBrian Somers msg.msg_namelen = 0; 143196c9bb21SBrian Somers msg.msg_iov = iov; 1432cbee9754SBrian Somers msg.msg_iovlen = 1; /* Only send the version at the first pass */ 143396c9bb21SBrian Somers msg.msg_control = cmsgbuf; 143496c9bb21SBrian Somers msg.msg_controllen = sizeof cmsgbuf; 143596c9bb21SBrian Somers 1436209dc102SBrian Somers log_Printf(LogDEBUG, "Expecting %u scatter/gather bytes\n", 1437209dc102SBrian Somers (unsigned)iov[0].iov_len); 14382cb305afSBrian Somers 1439cbee9754SBrian Somers if ((got = recvmsg(s, &msg, MSG_WAITALL)) != iov[0].iov_len) { 1440cbee9754SBrian Somers if (got == -1) 144196c9bb21SBrian Somers log_Printf(LogERROR, "Failed recvmsg: %s\n", strerror(errno)); 144296c9bb21SBrian Somers else 1443209dc102SBrian Somers log_Printf(LogERROR, "Failed recvmsg: Got %d, not %u\n", 1444209dc102SBrian Somers got, (unsigned)iov[0].iov_len); 144596c9bb21SBrian Somers while (niov--) 144696c9bb21SBrian Somers free(iov[niov].iov_base); 144796c9bb21SBrian Somers return; 144896c9bb21SBrian Somers } 144996c9bb21SBrian Somers 14502cb305afSBrian Somers if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) { 14512cb305afSBrian Somers log_Printf(LogERROR, "Recvmsg: no descriptors received !\n"); 14522cb305afSBrian Somers while (niov--) 14532cb305afSBrian Somers free(iov[niov].iov_base); 14542cb305afSBrian Somers return; 145587c3786eSBrian Somers } 145687c3786eSBrian Somers 14572cb305afSBrian Somers fd = (int *)(cmsg + 1); 14582cb305afSBrian Somers nfd = (cmsg->cmsg_len - sizeof *cmsg) / sizeof(int); 14592cb305afSBrian Somers 14602cb305afSBrian Somers if (nfd < 2) { 14618e7bd08eSBrian Somers log_Printf(LogERROR, "Recvmsg: %d descriptor%s received (too few) !\n", 14622cb305afSBrian Somers nfd, nfd == 1 ? "" : "s"); 14632cb305afSBrian Somers while (nfd--) 14642cb305afSBrian Somers close(fd[nfd]); 1465ad5b0e8bSBrian Somers while (niov--) 1466ad5b0e8bSBrian Somers free(iov[niov].iov_base); 1467ad5b0e8bSBrian Somers return; 146854cd8e13SBrian Somers } 146954cd8e13SBrian Somers 147087c3786eSBrian Somers /* 1471cbee9754SBrian Somers * We've successfully received two or more open file descriptors 1472cbee9754SBrian Somers * through our socket, plus a version string. Make sure it's the 1473cbee9754SBrian Somers * correct version, and drop the connection if it's not. 147487c3786eSBrian Somers */ 147596c9bb21SBrian Somers if (strncmp(Version, iov[0].iov_base, iov[0].iov_len)) { 147696c9bb21SBrian Somers log_Printf(LogWARN, "Cannot receive datalink, incorrect version" 147796c9bb21SBrian Somers " (\"%.*s\", not \"%s\")\n", (int)iov[0].iov_len, 14787d81ddf5SBrian Somers (char *)iov[0].iov_base, Version); 14792cb305afSBrian Somers while (nfd--) 14802cb305afSBrian Somers close(fd[nfd]); 148196c9bb21SBrian Somers while (niov--) 148296c9bb21SBrian Somers free(iov[niov].iov_base); 148396c9bb21SBrian Somers return; 148496c9bb21SBrian Somers } 148596c9bb21SBrian Somers 1486cbee9754SBrian Somers /* 1487cbee9754SBrian Somers * Everything looks good. Send the other side our process id so that 1488cbee9754SBrian Somers * they can transfer lock ownership, and wait for them to send the 1489cbee9754SBrian Somers * actual link data. 1490cbee9754SBrian Somers */ 1491cbee9754SBrian Somers pid = getpid(); 1492cbee9754SBrian Somers if ((got = write(fd[1], &pid, sizeof pid)) != sizeof pid) { 1493cbee9754SBrian Somers if (got == -1) 1494cbee9754SBrian Somers log_Printf(LogERROR, "Failed write: %s\n", strerror(errno)); 1495cbee9754SBrian Somers else 1496cbee9754SBrian Somers log_Printf(LogERROR, "Failed write: Got %d, not %d\n", got, 1497cbee9754SBrian Somers (int)(sizeof pid)); 1498cbee9754SBrian Somers while (nfd--) 1499cbee9754SBrian Somers close(fd[nfd]); 1500cbee9754SBrian Somers while (niov--) 1501cbee9754SBrian Somers free(iov[niov].iov_base); 1502cbee9754SBrian Somers return; 1503cbee9754SBrian Somers } 1504cbee9754SBrian Somers 1505cbee9754SBrian Somers if ((got = readv(fd[1], iov + 1, niov - 1)) != expect) { 1506cbee9754SBrian Somers if (got == -1) 1507cbee9754SBrian Somers log_Printf(LogERROR, "Failed write: %s\n", strerror(errno)); 1508cbee9754SBrian Somers else 1509cbee9754SBrian Somers log_Printf(LogERROR, "Failed write: Got %d, not %d\n", got, expect); 1510cbee9754SBrian Somers while (nfd--) 1511cbee9754SBrian Somers close(fd[nfd]); 1512cbee9754SBrian Somers while (niov--) 1513cbee9754SBrian Somers free(iov[niov].iov_base); 1514cbee9754SBrian Somers return; 1515cbee9754SBrian Somers } 1516cbee9754SBrian Somers close(fd[1]); 1517cbee9754SBrian Somers 15182cb305afSBrian Somers onfd = nfd; /* We've got this many in our array */ 15198e7bd08eSBrian Somers nfd -= 2; /* Don't include p->fd and our reply descriptor */ 15202cb305afSBrian Somers niov = 1; /* Skip the version id */ 152187c3786eSBrian Somers dl = iov2datalink(bundle, iov, &niov, sizeof iov / sizeof *iov, fd[0], 15222cb305afSBrian Somers fd + 2, &nfd); 1523b7c5748eSBrian Somers if (dl) { 15242cb305afSBrian Somers 152587c3786eSBrian Somers if (nfd) { 152687c3786eSBrian Somers log_Printf(LogERROR, "bundle_ReceiveDatalink: Failed to handle %d " 15272cb305afSBrian Somers "auxiliary file descriptors (%d remain)\n", onfd, nfd); 152887c3786eSBrian Somers datalink_Destroy(dl); 152987c3786eSBrian Somers while (nfd--) 153087c3786eSBrian Somers close(fd[onfd--]); 15312cb305afSBrian Somers close(fd[0]); 153287c3786eSBrian Somers } else { 153304eaa58cSBrian Somers bundle_DatalinkLinkin(bundle, dl); 1534b7c5748eSBrian Somers datalink_AuthOk(dl); 1535ab2de065SBrian Somers bundle_CalculateBandwidth(dl->bundle); 153687c3786eSBrian Somers } 153787c3786eSBrian Somers } else { 153887c3786eSBrian Somers while (nfd--) 153987c3786eSBrian Somers close(fd[onfd--]); 154087c3786eSBrian Somers close(fd[0]); 15412cb305afSBrian Somers close(fd[1]); 154287c3786eSBrian Somers } 154396c9bb21SBrian Somers 154496c9bb21SBrian Somers free(iov[0].iov_base); 15451fa665f5SBrian Somers } 15461fa665f5SBrian Somers 15471fa665f5SBrian Somers void 154896c9bb21SBrian Somers bundle_SendDatalink(struct datalink *dl, int s, struct sockaddr_un *sun) 15491fa665f5SBrian Somers { 15502cb305afSBrian Somers char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int) * SEND_MAXFD]; 15512cb305afSBrian Somers const char *constlock; 15522cb305afSBrian Somers char *lock; 155387c3786eSBrian Somers struct cmsghdr *cmsg; 155496c9bb21SBrian Somers struct msghdr msg; 155596c9bb21SBrian Somers struct iovec iov[SCATTER_SEGMENTS]; 15562cb305afSBrian Somers int niov, f, expect, newsid, fd[SEND_MAXFD], nfd, reply[2], got; 155785fd273aSBrian Somers pid_t newpid; 15586f384573SBrian Somers 1559dd7e2610SBrian Somers log_Printf(LogPHASE, "Transmitting datalink %s\n", dl->name); 15606f384573SBrian Somers 15612cb305afSBrian Somers /* Record the base device name for a lock transfer later */ 15622cb305afSBrian Somers constlock = physical_LockedDevice(dl->physical); 15632cb305afSBrian Somers if (constlock) { 15642cb305afSBrian Somers lock = alloca(strlen(constlock) + 1); 15652cb305afSBrian Somers strcpy(lock, constlock); 15662cb305afSBrian Somers } else 15672cb305afSBrian Somers lock = NULL; 15682cb305afSBrian Somers 156904eaa58cSBrian Somers bundle_LinkClosed(dl->bundle, dl); 15700f2f3eb3SBrian Somers bundle_DatalinkLinkout(dl->bundle, dl); 1571ea722969SBrian Somers 157296c9bb21SBrian Somers /* Build our scatter/gather array */ 157396c9bb21SBrian Somers iov[0].iov_len = strlen(Version) + 1; 157496c9bb21SBrian Somers iov[0].iov_base = strdup(Version); 157596c9bb21SBrian Somers niov = 1; 157687c3786eSBrian Somers nfd = 0; 15776f384573SBrian Somers 15782cb305afSBrian Somers fd[0] = datalink2iov(dl, iov, &niov, SCATTER_SEGMENTS, fd + 2, &nfd); 15796f384573SBrian Somers 15802cb305afSBrian Somers if (fd[0] != -1 && socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, reply) != -1) { 15812cb305afSBrian Somers /* 15822cb305afSBrian Somers * fd[1] is used to get the peer process id back, then to confirm that 15832cb305afSBrian Somers * we've transferred any device locks to that process id. 15842cb305afSBrian Somers */ 15852cb305afSBrian Somers fd[1] = reply[1]; 158687c3786eSBrian Somers 15872cb305afSBrian Somers nfd += 2; /* Include fd[0] and fd[1] */ 158896c9bb21SBrian Somers memset(&msg, '\0', sizeof msg); 158954cd8e13SBrian Somers 15902cb305afSBrian Somers msg.msg_name = NULL; 15912cb305afSBrian Somers msg.msg_namelen = 0; 1592cbee9754SBrian Somers /* 1593cbee9754SBrian Somers * Only send the version to start... We used to send the whole lot, but 1594cbee9754SBrian Somers * this caused problems with our RECVBUF size as a single link is about 1595cbee9754SBrian Somers * 22k ! This way, we should bump into no limits. 1596cbee9754SBrian Somers */ 1597cbee9754SBrian Somers msg.msg_iovlen = 1; 159896c9bb21SBrian Somers msg.msg_iov = iov; 15992cb305afSBrian Somers msg.msg_control = cmsgbuf; 16002cb305afSBrian Somers msg.msg_controllen = sizeof *cmsg + sizeof(int) * nfd; 16012cb305afSBrian Somers msg.msg_flags = 0; 160254cd8e13SBrian Somers 16032cb305afSBrian Somers cmsg = (struct cmsghdr *)cmsgbuf; 16042cb305afSBrian Somers cmsg->cmsg_len = msg.msg_controllen; 160554cd8e13SBrian Somers cmsg->cmsg_level = SOL_SOCKET; 160654cd8e13SBrian Somers cmsg->cmsg_type = SCM_RIGHTS; 160787c3786eSBrian Somers 16082cb305afSBrian Somers for (f = 0; f < nfd; f++) 16092cb305afSBrian Somers *((int *)(cmsg + 1) + f) = fd[f]; 161047723d29SBrian Somers 1611cbee9754SBrian Somers for (f = 1, expect = 0; f < niov; f++) 161296c9bb21SBrian Somers expect += iov[f].iov_len; 161347723d29SBrian Somers 1614cbee9754SBrian Somers if (setsockopt(reply[0], SOL_SOCKET, SO_SNDBUF, &expect, sizeof(int)) == -1) 1615cbee9754SBrian Somers log_Printf(LogERROR, "setsockopt(SO_RCVBUF, %d): %s\n", expect, 1616cbee9754SBrian Somers strerror(errno)); 1617cbee9754SBrian Somers if (setsockopt(reply[1], SOL_SOCKET, SO_RCVBUF, &expect, sizeof(int)) == -1) 1618cbee9754SBrian Somers log_Printf(LogERROR, "setsockopt(SO_RCVBUF, %d): %s\n", expect, 1619cbee9754SBrian Somers strerror(errno)); 1620cbee9754SBrian Somers 1621209dc102SBrian Somers log_Printf(LogDEBUG, "Sending %d descriptor%s and %u bytes in scatter" 1622209dc102SBrian Somers "/gather array\n", nfd, nfd == 1 ? "" : "s", 1623209dc102SBrian Somers (unsigned)iov[0].iov_len); 162447723d29SBrian Somers 16252cb305afSBrian Somers if ((got = sendmsg(s, &msg, 0)) == -1) 16262cb305afSBrian Somers log_Printf(LogERROR, "Failed sendmsg: %s: %s\n", 16272cb305afSBrian Somers sun->sun_path, strerror(errno)); 1628cbee9754SBrian Somers else if (got != iov[0].iov_len) 1629209dc102SBrian Somers log_Printf(LogERROR, "%s: Failed initial sendmsg: Only sent %d of %u\n", 1630209dc102SBrian Somers sun->sun_path, got, (unsigned)iov[0].iov_len); 16312cb305afSBrian Somers else { 16328e7bd08eSBrian Somers /* We must get the ACK before closing the descriptor ! */ 16332cb305afSBrian Somers int res; 16342cb305afSBrian Somers 1635cbee9754SBrian Somers if ((got = read(reply[0], &newpid, sizeof newpid)) == sizeof newpid) { 1636cbee9754SBrian Somers log_Printf(LogDEBUG, "Received confirmation from pid %d\n", 1637cbee9754SBrian Somers (int)newpid); 16382cb305afSBrian Somers if (lock && (res = ID0uu_lock_txfr(lock, newpid)) != UU_LOCK_OK) 16392cb305afSBrian Somers log_Printf(LogPHASE, "uu_lock_txfr: %s\n", uu_lockerr(res)); 16402cb305afSBrian Somers 1641cbee9754SBrian Somers log_Printf(LogDEBUG, "Transmitting link (%d bytes)\n", expect); 1642cbee9754SBrian Somers if ((got = writev(reply[0], iov + 1, niov - 1)) != expect) { 1643cbee9754SBrian Somers if (got == -1) 1644cbee9754SBrian Somers log_Printf(LogERROR, "%s: Failed writev: %s\n", 1645cbee9754SBrian Somers sun->sun_path, strerror(errno)); 1646cbee9754SBrian Somers else 1647cbee9754SBrian Somers log_Printf(LogERROR, "%s: Failed writev: Wrote %d of %d\n", 1648cbee9754SBrian Somers sun->sun_path, got, expect); 1649cbee9754SBrian Somers } 1650cbee9754SBrian Somers } else if (got == -1) 1651cbee9754SBrian Somers log_Printf(LogERROR, "%s: Failed socketpair read: %s\n", 1652cbee9754SBrian Somers sun->sun_path, strerror(errno)); 1653cbee9754SBrian Somers else 1654cbee9754SBrian Somers log_Printf(LogERROR, "%s: Failed socketpair read: Got %d of %d\n", 1655cbee9754SBrian Somers sun->sun_path, got, (int)(sizeof newpid)); 16562cb305afSBrian Somers } 16572cb305afSBrian Somers 16582cb305afSBrian Somers close(reply[0]); 16592cb305afSBrian Somers close(reply[1]); 166054cd8e13SBrian Somers 1661ac685e31SBrian Somers newsid = Enabled(dl->bundle, OPT_KEEPSESSION) || 166287c3786eSBrian Somers tcgetpgrp(fd[0]) == getpgrp(); 166387c3786eSBrian Somers while (nfd) 166487c3786eSBrian Somers close(fd[--nfd]); 16651384bd27SBrian Somers if (newsid) 16662cb305afSBrian Somers bundle_setsid(dl->bundle, got != -1); 166747723d29SBrian Somers } 166885fd273aSBrian Somers close(s); 166996c9bb21SBrian Somers 167096c9bb21SBrian Somers while (niov--) 167196c9bb21SBrian Somers free(iov[niov].iov_base); 16721fa665f5SBrian Somers } 1673dd0645c5SBrian Somers 1674dd0645c5SBrian Somers int 167558d55334SBrian Somers bundle_RenameDatalink(struct bundle *bundle, struct datalink *ndl, 167658d55334SBrian Somers const char *name) 167758d55334SBrian Somers { 167858d55334SBrian Somers struct datalink *dl; 167958d55334SBrian Somers 168058d55334SBrian Somers if (!strcasecmp(ndl->name, name)) 168158d55334SBrian Somers return 1; 168258d55334SBrian Somers 168358d55334SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 168458d55334SBrian Somers if (!strcasecmp(dl->name, name)) 168558d55334SBrian Somers return 0; 168658d55334SBrian Somers 168758d55334SBrian Somers datalink_Rename(ndl, name); 168858d55334SBrian Somers return 1; 168958d55334SBrian Somers } 169058d55334SBrian Somers 169158d55334SBrian Somers int 1692dd0645c5SBrian Somers bundle_SetMode(struct bundle *bundle, struct datalink *dl, int mode) 1693dd0645c5SBrian Somers { 1694dd0645c5SBrian Somers int omode; 1695dd0645c5SBrian Somers 1696dd0645c5SBrian Somers omode = dl->physical->type; 1697dd0645c5SBrian Somers if (omode == mode) 1698dd0645c5SBrian Somers return 1; 1699dd0645c5SBrian Somers 1700ff0f9439SBrian Somers if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO)) 1701ff0f9439SBrian Somers /* First auto link */ 1702dd0645c5SBrian Somers if (bundle->ncp.ipcp.peer_ip.s_addr == INADDR_ANY) { 1703ff0f9439SBrian Somers log_Printf(LogWARN, "You must `set ifaddr' or `open' before" 1704ff0f9439SBrian Somers " changing mode to %s\n", mode2Nam(mode)); 1705dd0645c5SBrian Somers return 0; 1706dd0645c5SBrian Somers } 1707dd0645c5SBrian Somers 1708dd0645c5SBrian Somers if (!datalink_SetMode(dl, mode)) 1709dd0645c5SBrian Somers return 0; 1710dd0645c5SBrian Somers 1711ff0f9439SBrian Somers if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO) && 1712ff0f9439SBrian Somers bundle->phase != PHASE_NETWORK) 1713ff0f9439SBrian Somers /* First auto link, we need an interface */ 1714dd0645c5SBrian Somers ipcp_InterfaceUp(&bundle->ncp.ipcp); 1715dd0645c5SBrian Somers 1716ab2de065SBrian Somers /* Regenerate phys_type and adjust idle timer */ 171704eaa58cSBrian Somers bundle_LinksRemoved(bundle); 1718dd0645c5SBrian Somers 1719dd0645c5SBrian Somers return 1; 1720dd0645c5SBrian Somers } 17211384bd27SBrian Somers 17221384bd27SBrian Somers void 17231384bd27SBrian Somers bundle_setsid(struct bundle *bundle, int holdsession) 17241384bd27SBrian Somers { 17251384bd27SBrian Somers /* 17261384bd27SBrian Somers * Lose the current session. This means getting rid of our pid 17271384bd27SBrian Somers * too so that the tty device will really go away, and any getty 17281384bd27SBrian Somers * etc will be allowed to restart. 17291384bd27SBrian Somers */ 17301384bd27SBrian Somers pid_t pid, orig; 17311384bd27SBrian Somers int fds[2]; 17321384bd27SBrian Somers char done; 17331384bd27SBrian Somers struct datalink *dl; 17341384bd27SBrian Somers 1735e62ce959SBrian Somers if (!holdsession && bundle_IsDead(bundle)) { 1736e62ce959SBrian Somers /* 1737e62ce959SBrian Somers * No need to lose our session after all... we're going away anyway 1738e62ce959SBrian Somers * 1739e62ce959SBrian Somers * We should really stop the timer and pause if holdsession is set and 1740e62ce959SBrian Somers * the bundle's dead, but that leaves other resources lying about :-( 1741e62ce959SBrian Somers */ 1742e62ce959SBrian Somers return; 1743e62ce959SBrian Somers } 1744e62ce959SBrian Somers 17451384bd27SBrian Somers orig = getpid(); 17461384bd27SBrian Somers if (pipe(fds) == -1) { 17471384bd27SBrian Somers log_Printf(LogERROR, "pipe: %s\n", strerror(errno)); 17481384bd27SBrian Somers return; 17491384bd27SBrian Somers } 17501384bd27SBrian Somers switch ((pid = fork())) { 17511384bd27SBrian Somers case -1: 17521384bd27SBrian Somers log_Printf(LogERROR, "fork: %s\n", strerror(errno)); 17531384bd27SBrian Somers close(fds[0]); 17541384bd27SBrian Somers close(fds[1]); 17551384bd27SBrian Somers return; 17561384bd27SBrian Somers case 0: 17571384bd27SBrian Somers close(fds[1]); 17584be0e57dSBrian Somers read(fds[0], &done, 1); /* uu_locks are mine ! */ 17594be0e57dSBrian Somers close(fds[0]); 17601384bd27SBrian Somers if (pipe(fds) == -1) { 17611384bd27SBrian Somers log_Printf(LogERROR, "pipe(2): %s\n", strerror(errno)); 17621384bd27SBrian Somers return; 17631384bd27SBrian Somers } 17641384bd27SBrian Somers switch ((pid = fork())) { 17651384bd27SBrian Somers case -1: 1766a33b2ef7SBrian Somers log_Printf(LogERROR, "fork(2): %s\n", strerror(errno)); 17671384bd27SBrian Somers close(fds[0]); 17681384bd27SBrian Somers close(fds[1]); 17691384bd27SBrian Somers return; 17701384bd27SBrian Somers case 0: 17711384bd27SBrian Somers close(fds[1]); 17724be0e57dSBrian Somers bundle_LockTun(bundle); /* update pid */ 17734be0e57dSBrian Somers read(fds[0], &done, 1); /* uu_locks are mine ! */ 17744be0e57dSBrian Somers close(fds[0]); 17751384bd27SBrian Somers setsid(); 177606b47306SBrian Somers bundle_ChangedPID(bundle); 17771384bd27SBrian Somers log_Printf(LogPHASE, "%d -> %d: %s session control\n", 17781384bd27SBrian Somers (int)orig, (int)getpid(), 17791384bd27SBrian Somers holdsession ? "Passed" : "Dropped"); 17807e778f13SBrian Somers timer_InitService(0); /* Start the Timer Service */ 17811384bd27SBrian Somers break; 17821384bd27SBrian Somers default: 17834be0e57dSBrian Somers close(fds[0]); 17845d9e6103SBrian Somers /* Give away all our physical locks (to the final process) */ 17851384bd27SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 17861384bd27SBrian Somers if (dl->state != DATALINK_CLOSED) 17875d9e6103SBrian Somers physical_ChangedPid(dl->physical, pid); 17884be0e57dSBrian Somers write(fds[1], "!", 1); /* done */ 17894be0e57dSBrian Somers close(fds[1]); 17902cb305afSBrian Somers _exit(0); 17911384bd27SBrian Somers break; 17921384bd27SBrian Somers } 17931384bd27SBrian Somers break; 17941384bd27SBrian Somers default: 17954be0e57dSBrian Somers close(fds[0]); 17965d9e6103SBrian Somers /* Give away all our physical locks (to the intermediate process) */ 17971384bd27SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 17981384bd27SBrian Somers if (dl->state != DATALINK_CLOSED) 17995d9e6103SBrian Somers physical_ChangedPid(dl->physical, pid); 18004be0e57dSBrian Somers write(fds[1], "!", 1); /* done */ 18014be0e57dSBrian Somers close(fds[1]); 18021384bd27SBrian Somers if (holdsession) { 18031384bd27SBrian Somers int fd, status; 18041384bd27SBrian Somers 18051384bd27SBrian Somers timer_TermService(); 18061384bd27SBrian Somers signal(SIGPIPE, SIG_DFL); 18071384bd27SBrian Somers signal(SIGALRM, SIG_DFL); 18081384bd27SBrian Somers signal(SIGHUP, SIG_DFL); 18091384bd27SBrian Somers signal(SIGTERM, SIG_DFL); 18101384bd27SBrian Somers signal(SIGINT, SIG_DFL); 18111384bd27SBrian Somers signal(SIGQUIT, SIG_DFL); 18121384bd27SBrian Somers for (fd = getdtablesize(); fd >= 0; fd--) 18131384bd27SBrian Somers close(fd); 18141384bd27SBrian Somers /* 18151384bd27SBrian Somers * Reap the intermediate process. As we're not exiting but the 18161384bd27SBrian Somers * intermediate is, we don't want it to become defunct. 18171384bd27SBrian Somers */ 18181384bd27SBrian Somers waitpid(pid, &status, 0); 18198e7b8599SBrian Somers /* Tweak our process arguments.... */ 1820cf0a3940SBrian Somers ID0setproctitle("session owner"); 1821a19a5c02SBrian Somers setuid(ID0realuid()); 18221384bd27SBrian Somers /* 18231384bd27SBrian Somers * Hang around for a HUP. This should happen as soon as the 18248e7bd08eSBrian Somers * ppp that we passed our ctty descriptor to closes it. 18258e7bd08eSBrian Somers * NOTE: If this process dies, the passed descriptor becomes 18261384bd27SBrian Somers * invalid and will give a select() error by setting one 18271384bd27SBrian Somers * of the error fds, aborting the other ppp. We don't 18281384bd27SBrian Somers * want that to happen ! 18291384bd27SBrian Somers */ 18301384bd27SBrian Somers pause(); 18311384bd27SBrian Somers } 18322cb305afSBrian Somers _exit(0); 18331384bd27SBrian Somers break; 18341384bd27SBrian Somers } 18351384bd27SBrian Somers } 18369b5f8ffdSBrian Somers 18379b5f8ffdSBrian Somers int 18389b5f8ffdSBrian Somers bundle_HighestState(struct bundle *bundle) 18399b5f8ffdSBrian Somers { 18409b5f8ffdSBrian Somers struct datalink *dl; 18419b5f8ffdSBrian Somers int result = DATALINK_CLOSED; 18429b5f8ffdSBrian Somers 18439b5f8ffdSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 18449b5f8ffdSBrian Somers if (result < dl->state) 18459b5f8ffdSBrian Somers result = dl->state; 18469b5f8ffdSBrian Somers 18479b5f8ffdSBrian Somers return result; 18489b5f8ffdSBrian Somers } 1849991c2a7bSBrian Somers 1850991c2a7bSBrian Somers int 1851991c2a7bSBrian Somers bundle_Exception(struct bundle *bundle, int fd) 1852991c2a7bSBrian Somers { 1853991c2a7bSBrian Somers struct datalink *dl; 1854991c2a7bSBrian Somers 1855991c2a7bSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 1856991c2a7bSBrian Somers if (dl->physical->fd == fd) { 1857991c2a7bSBrian Somers datalink_Down(dl, CLOSE_NORMAL); 1858991c2a7bSBrian Somers return 1; 1859991c2a7bSBrian Somers } 1860991c2a7bSBrian Somers 1861991c2a7bSBrian Somers return 0; 1862991c2a7bSBrian Somers } 18631d1fc017SBrian Somers 18641d1fc017SBrian Somers void 18651d1fc017SBrian Somers bundle_AdjustFilters(struct bundle *bundle, struct in_addr *my_ip, 18661d1fc017SBrian Somers struct in_addr *peer_ip) 18671d1fc017SBrian Somers { 1868d568d6c4SBrian Somers filter_AdjustAddr(&bundle->filter.in, my_ip, peer_ip, NULL); 1869d568d6c4SBrian Somers filter_AdjustAddr(&bundle->filter.out, my_ip, peer_ip, NULL); 1870d568d6c4SBrian Somers filter_AdjustAddr(&bundle->filter.dial, my_ip, peer_ip, NULL); 1871d568d6c4SBrian Somers filter_AdjustAddr(&bundle->filter.alive, my_ip, peer_ip, NULL); 1872d568d6c4SBrian Somers } 1873d568d6c4SBrian Somers 1874d568d6c4SBrian Somers void 1875d568d6c4SBrian Somers bundle_AdjustDNS(struct bundle *bundle, struct in_addr dns[2]) 1876d568d6c4SBrian Somers { 1877d568d6c4SBrian Somers filter_AdjustAddr(&bundle->filter.in, NULL, NULL, dns); 1878d568d6c4SBrian Somers filter_AdjustAddr(&bundle->filter.out, NULL, NULL, dns); 1879d568d6c4SBrian Somers filter_AdjustAddr(&bundle->filter.dial, NULL, NULL, dns); 1880d568d6c4SBrian Somers filter_AdjustAddr(&bundle->filter.alive, NULL, NULL, dns); 18811d1fc017SBrian Somers } 1882ab2de065SBrian Somers 1883ab2de065SBrian Somers void 1884ab2de065SBrian Somers bundle_CalculateBandwidth(struct bundle *bundle) 1885ab2de065SBrian Somers { 1886ab2de065SBrian Somers struct datalink *dl; 1887ab2de065SBrian Somers int mtu, sp; 1888ab2de065SBrian Somers 1889ab2de065SBrian Somers bundle->bandwidth = 0; 1890ab2de065SBrian Somers mtu = 0; 1891ab2de065SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 1892ab2de065SBrian Somers if (dl->state == DATALINK_OPEN) { 1893ab2de065SBrian Somers if ((sp = dl->mp.bandwidth) == 0 && 1894ab2de065SBrian Somers (sp = physical_GetSpeed(dl->physical)) == 0) 1895ab2de065SBrian Somers log_Printf(LogDEBUG, "%s: %s: Cannot determine bandwidth\n", 1896ab2de065SBrian Somers dl->name, dl->physical->name.full); 1897ab2de065SBrian Somers else 1898ab2de065SBrian Somers bundle->bandwidth += sp; 1899ab2de065SBrian Somers if (!bundle->ncp.mp.active) { 1900ab2de065SBrian Somers mtu = dl->physical->link.lcp.his_mru; 1901ab2de065SBrian Somers break; 1902ab2de065SBrian Somers } 1903ab2de065SBrian Somers } 1904ab2de065SBrian Somers 1905ab2de065SBrian Somers if(bundle->bandwidth == 0) 1906ab2de065SBrian Somers bundle->bandwidth = 115200; /* Shrug */ 1907ab2de065SBrian Somers 1908ab2de065SBrian Somers if (bundle->ncp.mp.active) 1909ab2de065SBrian Somers mtu = bundle->ncp.mp.peer_mrru; 1910ab2de065SBrian Somers else if (!mtu) 1911ab2de065SBrian Somers mtu = 1500; 1912ab2de065SBrian Somers 1913ab2de065SBrian Somers #ifndef NORADIUS 1914ab2de065SBrian Somers if (bundle->radius.valid && bundle->radius.mtu && bundle->radius.mtu < mtu) { 1915ab2de065SBrian Somers log_Printf(LogLCP, "Reducing MTU to radius value %lu\n", 1916ab2de065SBrian Somers bundle->radius.mtu); 1917ab2de065SBrian Somers mtu = bundle->radius.mtu; 1918ab2de065SBrian Somers } 1919ab2de065SBrian Somers #endif 1920ab2de065SBrian Somers 1921ab2de065SBrian Somers tun_configure(bundle, mtu); 1922ab2de065SBrian Somers } 1923ab2de065SBrian Somers 1924ab2de065SBrian Somers void 1925ab2de065SBrian Somers bundle_AutoAdjust(struct bundle *bundle, int percent, int what) 1926ab2de065SBrian Somers { 1927ab2de065SBrian Somers struct datalink *dl, *choice, *otherlinkup; 1928ab2de065SBrian Somers 1929ab2de065SBrian Somers choice = otherlinkup = NULL; 1930ab2de065SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 1931ab2de065SBrian Somers if (dl->physical->type == PHYS_AUTO) { 1932ab2de065SBrian Somers if (dl->state == DATALINK_OPEN) { 1933ab2de065SBrian Somers if (what == AUTO_DOWN) { 1934ab2de065SBrian Somers if (choice) 1935ab2de065SBrian Somers otherlinkup = choice; 1936ab2de065SBrian Somers choice = dl; 1937ab2de065SBrian Somers } 1938ab2de065SBrian Somers } else if (dl->state == DATALINK_CLOSED) { 1939ab2de065SBrian Somers if (what == AUTO_UP) { 1940ab2de065SBrian Somers choice = dl; 1941ab2de065SBrian Somers break; 1942ab2de065SBrian Somers } 1943ab2de065SBrian Somers } else { 1944ab2de065SBrian Somers /* An auto link in an intermediate state - forget it for the moment */ 1945ab2de065SBrian Somers choice = NULL; 1946ab2de065SBrian Somers break; 1947ab2de065SBrian Somers } 1948ab2de065SBrian Somers } else if (dl->state == DATALINK_OPEN && what == AUTO_DOWN) 1949ab2de065SBrian Somers otherlinkup = dl; 1950ab2de065SBrian Somers 1951ab2de065SBrian Somers if (choice) { 1952ab2de065SBrian Somers if (what == AUTO_UP) { 1953ab2de065SBrian Somers log_Printf(LogPHASE, "%d%% saturation -> Opening link ``%s''\n", 1954ab2de065SBrian Somers percent, choice->name); 1955ab2de065SBrian Somers datalink_Up(choice, 1, 1); 1956ab2de065SBrian Somers mp_StopAutoloadTimer(&bundle->ncp.mp); 1957ab2de065SBrian Somers } else if (otherlinkup) { /* Only bring the second-last link down */ 1958ab2de065SBrian Somers log_Printf(LogPHASE, "%d%% saturation -> Closing link ``%s''\n", 1959ab2de065SBrian Somers percent, choice->name); 1960d2f5232dSBrian Somers datalink_Close(choice, CLOSE_STAYDOWN); 1961ab2de065SBrian Somers mp_StopAutoloadTimer(&bundle->ncp.mp); 1962ab2de065SBrian Somers } 1963ab2de065SBrian Somers } 1964ab2de065SBrian Somers } 1965ab2de065SBrian Somers 1966ab2de065SBrian Somers int 1967ab2de065SBrian Somers bundle_WantAutoloadTimer(struct bundle *bundle) 1968ab2de065SBrian Somers { 1969ab2de065SBrian Somers struct datalink *dl; 1970ab2de065SBrian Somers int autolink, opened; 1971ab2de065SBrian Somers 1972ab2de065SBrian Somers if (bundle->phase == PHASE_NETWORK) { 1973ab2de065SBrian Somers for (autolink = opened = 0, dl = bundle->links; dl; dl = dl->next) 1974ab2de065SBrian Somers if (dl->physical->type == PHYS_AUTO) { 1975ab2de065SBrian Somers if (++autolink == 2 || (autolink == 1 && opened)) 1976ab2de065SBrian Somers /* Two auto links or one auto and one open in NETWORK phase */ 1977ab2de065SBrian Somers return 1; 1978ab2de065SBrian Somers } else if (dl->state == DATALINK_OPEN) { 1979ab2de065SBrian Somers opened++; 1980ab2de065SBrian Somers if (autolink) 1981ab2de065SBrian Somers /* One auto and one open link in NETWORK phase */ 1982ab2de065SBrian Somers return 1; 1983ab2de065SBrian Somers } 1984ab2de065SBrian Somers } 1985ab2de065SBrian Somers 1986ab2de065SBrian Somers return 0; 1987ab2de065SBrian Somers } 198806b47306SBrian Somers 198906b47306SBrian Somers void 199006b47306SBrian Somers bundle_ChangedPID(struct bundle *bundle) 199106b47306SBrian Somers { 199206b47306SBrian Somers #ifdef TUNSIFPID 199306b47306SBrian Somers ioctl(bundle->dev.fd, TUNSIFPID, 0); 199406b47306SBrian Somers #endif 199506b47306SBrian Somers } 1996