bundle.c revision 71971
136285Sbrian/*- 236285Sbrian * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org> 336285Sbrian * All rights reserved. 436285Sbrian * 536285Sbrian * Redistribution and use in source and binary forms, with or without 636285Sbrian * modification, are permitted provided that the following conditions 736285Sbrian * are met: 836285Sbrian * 1. Redistributions of source code must retain the above copyright 936285Sbrian * notice, this list of conditions and the following disclaimer. 1036285Sbrian * 2. Redistributions in binary form must reproduce the above copyright 1136285Sbrian * notice, this list of conditions and the following disclaimer in the 1236285Sbrian * documentation and/or other materials provided with the distribution. 1336285Sbrian * 1436285Sbrian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1536285Sbrian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1636285Sbrian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1736285Sbrian * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1836285Sbrian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1936285Sbrian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2036285Sbrian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2136285Sbrian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2236285Sbrian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2336285Sbrian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2436285Sbrian * SUCH DAMAGE. 2536285Sbrian * 2650479Speter * $FreeBSD: head/usr.sbin/ppp/bundle.c 71971 2001-02-04 01:08:24Z brian $ 2736285Sbrian */ 2836285Sbrian 2936452Sbrian#include <sys/param.h> 3036285Sbrian#include <sys/socket.h> 3136285Sbrian#include <netinet/in.h> 3236285Sbrian#include <net/if.h> 3356413Sbrian#include <net/if_tun.h> /* For TUNS* ioctls */ 3436285Sbrian#include <arpa/inet.h> 3536285Sbrian#include <net/route.h> 3636285Sbrian#include <netinet/in_systm.h> 3736285Sbrian#include <netinet/ip.h> 3836285Sbrian#include <sys/un.h> 3936285Sbrian 4036285Sbrian#include <errno.h> 4136285Sbrian#include <fcntl.h> 4253298Sbrian#ifdef __OpenBSD__ 4353298Sbrian#include <util.h> 4453298Sbrian#else 4553298Sbrian#include <libutil.h> 4653298Sbrian#endif 4736285Sbrian#include <paths.h> 4836285Sbrian#include <stdio.h> 4936285Sbrian#include <stdlib.h> 5036285Sbrian#include <string.h> 5136285Sbrian#include <sys/uio.h> 5236345Sbrian#include <sys/wait.h> 5351525Sbrian#if defined(__FreeBSD__) && !defined(NOKLDLOAD) 5464802Sbrian#ifdef NOSUID 5564802Sbrian#include <sys/linker.h> 5664802Sbrian#endif 5753241Sbrian#include <sys/module.h> 5851525Sbrian#endif 5936285Sbrian#include <termios.h> 6036285Sbrian#include <unistd.h> 6136285Sbrian 6246686Sbrian#include "layer.h" 6337009Sbrian#include "defs.h" 6436285Sbrian#include "command.h" 6536285Sbrian#include "mbuf.h" 6636285Sbrian#include "log.h" 6736285Sbrian#include "id.h" 6836285Sbrian#include "timer.h" 6936285Sbrian#include "fsm.h" 7036285Sbrian#include "iplist.h" 7136285Sbrian#include "lqr.h" 7236285Sbrian#include "hdlc.h" 7336285Sbrian#include "throughput.h" 7436285Sbrian#include "slcompress.h" 7536285Sbrian#include "ipcp.h" 7636285Sbrian#include "filter.h" 7736285Sbrian#include "descriptor.h" 7836285Sbrian#include "route.h" 7936285Sbrian#include "lcp.h" 8036285Sbrian#include "ccp.h" 8136285Sbrian#include "link.h" 8236285Sbrian#include "mp.h" 8343313Sbrian#ifndef NORADIUS 8443313Sbrian#include "radius.h" 8543313Sbrian#endif 8636285Sbrian#include "bundle.h" 8736285Sbrian#include "async.h" 8836285Sbrian#include "physical.h" 8936285Sbrian#include "auth.h" 9046686Sbrian#include "proto.h" 9136285Sbrian#include "chap.h" 9236285Sbrian#include "tun.h" 9336285Sbrian#include "prompt.h" 9436285Sbrian#include "chat.h" 9538174Sbrian#include "cbcp.h" 9636285Sbrian#include "datalink.h" 9736285Sbrian#include "ip.h" 9840561Sbrian#include "iface.h" 9971657Sbrian#include "server.h" 10071971Sbrian#include "mppe.h" 10136285Sbrian 10264670Sbrian#define SCATTER_SEGMENTS 7 /* version, datalink, name, physical, 10364670Sbrian throughput, throughput, device */ 10436285Sbrian 10553684Sbrian#define SEND_MAXFD 3 /* Max file descriptors passed through 10653684Sbrian the local domain socket */ 10752942Sbrian 10836285Sbrianstatic int bundle_RemainingIdleTime(struct bundle *); 10936285Sbrian 11055146Sbrianstatic const char * const PhaseNames[] = { 11136285Sbrian "Dead", "Establish", "Authenticate", "Network", "Terminate" 11236285Sbrian}; 11336285Sbrian 11436285Sbrianconst char * 11536285Sbrianbundle_PhaseName(struct bundle *bundle) 11636285Sbrian{ 11736285Sbrian return bundle->phase <= PHASE_TERMINATE ? 11836285Sbrian PhaseNames[bundle->phase] : "unknown"; 11936285Sbrian} 12036285Sbrian 12136285Sbrianvoid 12236285Sbrianbundle_NewPhase(struct bundle *bundle, u_int new) 12336285Sbrian{ 12436285Sbrian if (new == bundle->phase) 12536285Sbrian return; 12636285Sbrian 12736285Sbrian if (new <= PHASE_TERMINATE) 12836285Sbrian log_Printf(LogPHASE, "bundle: %s\n", PhaseNames[new]); 12936285Sbrian 13036285Sbrian switch (new) { 13136285Sbrian case PHASE_DEAD: 13271971Sbrian bundle->phase = new; 13371971Sbrian MPPE_MasterKeyValid = 0; 13436314Sbrian log_DisplayPrompts(); 13536285Sbrian break; 13636285Sbrian 13736285Sbrian case PHASE_ESTABLISH: 13836285Sbrian bundle->phase = new; 13936285Sbrian break; 14036285Sbrian 14136285Sbrian case PHASE_AUTHENTICATE: 14236285Sbrian bundle->phase = new; 14336314Sbrian log_DisplayPrompts(); 14436285Sbrian break; 14536285Sbrian 14636285Sbrian case PHASE_NETWORK: 14736285Sbrian fsm_Up(&bundle->ncp.ipcp.fsm); 14836285Sbrian fsm_Open(&bundle->ncp.ipcp.fsm); 14936285Sbrian bundle->phase = new; 15036314Sbrian log_DisplayPrompts(); 15136285Sbrian break; 15236285Sbrian 15336285Sbrian case PHASE_TERMINATE: 15436285Sbrian bundle->phase = new; 15536285Sbrian mp_Down(&bundle->ncp.mp); 15636314Sbrian log_DisplayPrompts(); 15736285Sbrian break; 15836285Sbrian } 15936285Sbrian} 16036285Sbrian 16136285Sbrianstatic void 16236285Sbrianbundle_LayerStart(void *v, struct fsm *fp) 16336285Sbrian{ 16436285Sbrian /* The given FSM is about to start up ! */ 16536285Sbrian} 16636285Sbrian 16736285Sbrian 16859084Sbrianvoid 16936285Sbrianbundle_Notify(struct bundle *bundle, char c) 17036285Sbrian{ 17136285Sbrian if (bundle->notify.fd != -1) { 17259084Sbrian int ret; 17359084Sbrian 17459084Sbrian ret = write(bundle->notify.fd, &c, 1); 17559084Sbrian if (c != EX_REDIAL && c != EX_RECONNECT) { 17659084Sbrian if (ret == 1) 17759084Sbrian log_Printf(LogCHAT, "Parent notified of %s\n", 17859084Sbrian c == EX_NORMAL ? "success" : "failure"); 17959084Sbrian else 18059084Sbrian log_Printf(LogERROR, "Failed to notify parent of success\n"); 18159084Sbrian close(bundle->notify.fd); 18259084Sbrian bundle->notify.fd = -1; 18359084Sbrian } else if (ret == 1) 18459084Sbrian log_Printf(LogCHAT, "Parent notified of %s\n", ex_desc(c)); 18536285Sbrian else 18659084Sbrian log_Printf(LogERROR, "Failed to notify parent of %s\n", ex_desc(c)); 18736285Sbrian } 18836285Sbrian} 18936285Sbrian 19038544Sbrianstatic void 19138544Sbrianbundle_ClearQueues(void *v) 19238544Sbrian{ 19338544Sbrian struct bundle *bundle = (struct bundle *)v; 19438544Sbrian struct datalink *dl; 19538544Sbrian 19638544Sbrian log_Printf(LogPHASE, "Clearing choked output queue\n"); 19738544Sbrian timer_Stop(&bundle->choked.timer); 19838544Sbrian 19938544Sbrian /* 20038544Sbrian * Emergency time: 20138544Sbrian * 20238544Sbrian * We've had a full queue for PACKET_DEL_SECS seconds without being 20338544Sbrian * able to get rid of any of the packets. We've probably given up 20438544Sbrian * on the redials at this point, and the queued data has almost 20538544Sbrian * definitely been timed out by the layer above. As this is preventing 20638544Sbrian * us from reading the TUN_NAME device (we don't want to buffer stuff 20738544Sbrian * indefinitely), we may as well nuke this data and start with a clean 20838544Sbrian * slate ! 20938544Sbrian * 21038544Sbrian * Unfortunately, this has the side effect of shafting any compression 21138544Sbrian * dictionaries in use (causing the relevant RESET_REQ/RESET_ACK). 21238544Sbrian */ 21338544Sbrian 21438557Sbrian ip_DeleteQueue(&bundle->ncp.ipcp); 21538544Sbrian mp_DeleteQueue(&bundle->ncp.mp); 21638544Sbrian for (dl = bundle->links; dl; dl = dl->next) 21738544Sbrian physical_DeleteQueue(dl->physical); 21838544Sbrian} 21938544Sbrian 22036285Sbrianstatic void 22136928Sbrianbundle_LinkAdded(struct bundle *bundle, struct datalink *dl) 22236928Sbrian{ 22336928Sbrian bundle->phys_type.all |= dl->physical->type; 22436928Sbrian if (dl->state == DATALINK_OPEN) 22536928Sbrian bundle->phys_type.open |= dl->physical->type; 22636285Sbrian 22736928Sbrian if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) 22836928Sbrian != bundle->phys_type.open && bundle->idle.timer.state == TIMER_STOPPED) 22936928Sbrian /* We may need to start our idle timer */ 23062977Sbrian bundle_StartIdleTimer(bundle, 0); 23136928Sbrian} 23236928Sbrian 23338174Sbrianvoid 23436928Sbrianbundle_LinksRemoved(struct bundle *bundle) 23536928Sbrian{ 23636928Sbrian struct datalink *dl; 23736928Sbrian 23836928Sbrian bundle->phys_type.all = bundle->phys_type.open = 0; 23936928Sbrian for (dl = bundle->links; dl; dl = dl->next) 24036928Sbrian bundle_LinkAdded(bundle, dl); 24136928Sbrian 24249434Sbrian bundle_CalculateBandwidth(bundle); 24349434Sbrian mp_CheckAutoloadTimer(&bundle->ncp.mp); 24449434Sbrian 24536928Sbrian if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) 24636928Sbrian == bundle->phys_type.open) 24736928Sbrian bundle_StopIdleTimer(bundle); 24836928Sbrian} 24936928Sbrian 25036928Sbrianstatic void 25136285Sbrianbundle_LayerUp(void *v, struct fsm *fp) 25236285Sbrian{ 25336285Sbrian /* 25436285Sbrian * The given fsm is now up 25549434Sbrian * If it's an LCP, adjust our phys_mode.open value and check the 25649434Sbrian * autoload timer. 25749434Sbrian * If it's the first NCP, calculate our bandwidth 25849978Sbrian * If it's the first NCP, set our ``upat'' time 25949434Sbrian * If it's the first NCP, start the idle timer. 26036285Sbrian * If it's an NCP, tell our -background parent to go away. 26149434Sbrian * If it's the first NCP, start the autoload timer 26236285Sbrian */ 26336285Sbrian struct bundle *bundle = (struct bundle *)v; 26436285Sbrian 26536285Sbrian if (fp->proto == PROTO_LCP) { 26636928Sbrian struct physical *p = link2physical(fp->link); 26736928Sbrian 26836928Sbrian bundle_LinkAdded(bundle, p->dl); 26949434Sbrian mp_CheckAutoloadTimer(&bundle->ncp.mp); 27036285Sbrian } else if (fp->proto == PROTO_IPCP) { 27149434Sbrian bundle_CalculateBandwidth(fp->bundle); 27249978Sbrian time(&bundle->upat); 27362977Sbrian bundle_StartIdleTimer(bundle, 0); 27436285Sbrian bundle_Notify(bundle, EX_NORMAL); 27549434Sbrian mp_CheckAutoloadTimer(&fp->bundle->ncp.mp); 27636285Sbrian } 27736285Sbrian} 27836285Sbrian 27936285Sbrianstatic void 28036285Sbrianbundle_LayerDown(void *v, struct fsm *fp) 28136285Sbrian{ 28236285Sbrian /* 28336285Sbrian * The given FSM has been told to come down. 28436285Sbrian * If it's our last NCP, stop the idle timer. 28549978Sbrian * If it's our last NCP, clear our ``upat'' value. 28649434Sbrian * If it's our last NCP, stop the autoload timer 28736928Sbrian * If it's an LCP, adjust our phys_type.open value and any timers. 28836312Sbrian * If it's an LCP and we're in multilink mode, adjust our tun 28959070Sbrian * If it's the last LCP, down all NCPs 29036312Sbrian * speed and make sure our minimum sequence number is adjusted. 29136285Sbrian */ 29236285Sbrian 29336285Sbrian struct bundle *bundle = (struct bundle *)v; 29436285Sbrian 29549434Sbrian if (fp->proto == PROTO_IPCP) { 29636285Sbrian bundle_StopIdleTimer(bundle); 29749978Sbrian bundle->upat = 0; 29849434Sbrian mp_StopAutoloadTimer(&bundle->ncp.mp); 29949434Sbrian } else if (fp->proto == PROTO_LCP) { 30059070Sbrian struct datalink *dl; 30159070Sbrian struct datalink *lost; 30259070Sbrian int others_active; 30359070Sbrian 30436928Sbrian bundle_LinksRemoved(bundle); /* adjust timers & phys_type values */ 30536285Sbrian 30659070Sbrian lost = NULL; 30759070Sbrian others_active = 0; 30859070Sbrian for (dl = bundle->links; dl; dl = dl->next) { 30959070Sbrian if (fp == &dl->physical->link.lcp.fsm) 31059070Sbrian lost = dl; 31159070Sbrian else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) 31259070Sbrian others_active++; 31359070Sbrian } 31436312Sbrian 31559070Sbrian if (bundle->ncp.mp.active) { 31649434Sbrian bundle_CalculateBandwidth(bundle); 31736312Sbrian 31836928Sbrian if (lost) 31936928Sbrian mp_LinkLost(&bundle->ncp.mp, lost); 32036928Sbrian else 32137019Sbrian log_Printf(LogALERT, "Oops, lost an unrecognised datalink (%s) !\n", 32236928Sbrian fp->link->name); 32336928Sbrian } 32459070Sbrian 32559070Sbrian if (!others_active) 32659070Sbrian /* Down the NCPs. We don't expect to get fsm_Close()d ourself ! */ 32759070Sbrian fsm2initial(&bundle->ncp.ipcp.fsm); 32836285Sbrian } 32936285Sbrian} 33036285Sbrian 33136285Sbrianstatic void 33236285Sbrianbundle_LayerFinish(void *v, struct fsm *fp) 33336285Sbrian{ 33436285Sbrian /* The given fsm is now down (fp cannot be NULL) 33536285Sbrian * 33636285Sbrian * If it's the last NCP, fsm_Close all LCPs 33736285Sbrian */ 33836285Sbrian 33936285Sbrian struct bundle *bundle = (struct bundle *)v; 34036285Sbrian struct datalink *dl; 34136285Sbrian 34236285Sbrian if (fp->proto == PROTO_IPCP) { 34336285Sbrian if (bundle_Phase(bundle) != PHASE_DEAD) 34436285Sbrian bundle_NewPhase(bundle, PHASE_TERMINATE); 34536285Sbrian for (dl = bundle->links; dl; dl = dl->next) 34659070Sbrian if (dl->state == DATALINK_OPEN) 34759070Sbrian datalink_Close(dl, CLOSE_STAYDOWN); 34837060Sbrian fsm2initial(fp); 34936285Sbrian } 35036285Sbrian} 35136285Sbrian 35236285Sbrianint 35336285Sbrianbundle_LinkIsUp(const struct bundle *bundle) 35436285Sbrian{ 35536285Sbrian return bundle->ncp.ipcp.fsm.state == ST_OPENED; 35636285Sbrian} 35736285Sbrian 35836285Sbrianvoid 35937007Sbrianbundle_Close(struct bundle *bundle, const char *name, int how) 36036285Sbrian{ 36136285Sbrian /* 36236285Sbrian * Please close the given datalink. 36336285Sbrian * If name == NULL or name is the last datalink, fsm_Close all NCPs 36436285Sbrian * (except our MP) 36536285Sbrian * If it isn't the last datalink, just Close that datalink. 36636285Sbrian */ 36736285Sbrian 36836285Sbrian struct datalink *dl, *this_dl; 36936285Sbrian int others_active; 37036285Sbrian 37136285Sbrian others_active = 0; 37236285Sbrian this_dl = NULL; 37336285Sbrian 37436285Sbrian for (dl = bundle->links; dl; dl = dl->next) { 37536285Sbrian if (name && !strcasecmp(name, dl->name)) 37636285Sbrian this_dl = dl; 37736285Sbrian if (name == NULL || this_dl == dl) { 37837007Sbrian switch (how) { 37937007Sbrian case CLOSE_LCP: 38037007Sbrian datalink_DontHangup(dl); 38171970Sbrian break; 38237007Sbrian case CLOSE_STAYDOWN: 38337007Sbrian datalink_StayDown(dl); 38437007Sbrian break; 38537007Sbrian } 38636285Sbrian } else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) 38736285Sbrian others_active++; 38836285Sbrian } 38936285Sbrian 39036285Sbrian if (name && this_dl == NULL) { 39136285Sbrian log_Printf(LogWARN, "%s: Invalid datalink name\n", name); 39236285Sbrian return; 39336285Sbrian } 39436285Sbrian 39536285Sbrian if (!others_active) { 39636285Sbrian bundle_StopIdleTimer(bundle); 39736285Sbrian if (bundle->ncp.ipcp.fsm.state > ST_CLOSED || 39836285Sbrian bundle->ncp.ipcp.fsm.state == ST_STARTING) 39936285Sbrian fsm_Close(&bundle->ncp.ipcp.fsm); 40036285Sbrian else { 40137060Sbrian fsm2initial(&bundle->ncp.ipcp.fsm); 40236285Sbrian for (dl = bundle->links; dl; dl = dl->next) 40337007Sbrian datalink_Close(dl, how); 40436285Sbrian } 40536285Sbrian } else if (this_dl && this_dl->state != DATALINK_CLOSED && 40636285Sbrian this_dl->state != DATALINK_HANGUP) 40737007Sbrian datalink_Close(this_dl, how); 40836285Sbrian} 40936285Sbrian 41036285Sbrianvoid 41137018Sbrianbundle_Down(struct bundle *bundle, int how) 41236285Sbrian{ 41336285Sbrian struct datalink *dl; 41436285Sbrian 41536285Sbrian for (dl = bundle->links; dl; dl = dl->next) 41637018Sbrian datalink_Down(dl, how); 41736285Sbrian} 41836285Sbrian 41954912Sbrianstatic size_t 42054912Sbrianbundle_FillQueues(struct bundle *bundle) 42154912Sbrian{ 42254912Sbrian size_t total; 42354912Sbrian 42454912Sbrian if (bundle->ncp.mp.active) 42554912Sbrian total = mp_FillQueues(bundle); 42654912Sbrian else { 42754912Sbrian struct datalink *dl; 42854912Sbrian size_t add; 42954912Sbrian 43054912Sbrian for (total = 0, dl = bundle->links; dl; dl = dl->next) 43154912Sbrian if (dl->state == DATALINK_OPEN) { 43254912Sbrian add = link_QueueLen(&dl->physical->link); 43354912Sbrian if (add == 0 && dl->physical->out == NULL) 43454912Sbrian add = ip_PushPacket(&dl->physical->link, bundle); 43554912Sbrian total += add; 43654912Sbrian } 43754912Sbrian } 43854912Sbrian 43954912Sbrian return total + ip_QueueLen(&bundle->ncp.ipcp); 44054912Sbrian} 44154912Sbrian 44236285Sbrianstatic int 44358028Sbrianbundle_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n) 44436285Sbrian{ 44536285Sbrian struct bundle *bundle = descriptor2bundle(d); 44636285Sbrian struct datalink *dl; 44754912Sbrian int result, nlinks; 44861534Sbrian u_short ifqueue; 44954912Sbrian size_t queued; 45036285Sbrian 45136285Sbrian result = 0; 45236285Sbrian 45336285Sbrian /* If there are aren't many packets queued, look for some more. */ 45436285Sbrian for (nlinks = 0, dl = bundle->links; dl; dl = dl->next) 45536285Sbrian nlinks++; 45636285Sbrian 45736285Sbrian if (nlinks) { 45838557Sbrian queued = r ? bundle_FillQueues(bundle) : ip_QueueLen(&bundle->ncp.ipcp); 45936285Sbrian 46036928Sbrian if (r && (bundle->phase == PHASE_NETWORK || 46136928Sbrian bundle->phys_type.all & PHYS_AUTO)) { 46236285Sbrian /* enough surplus so that we can tell if we're getting swamped */ 46361534Sbrian ifqueue = nlinks > bundle->cfg.ifqueue ? nlinks : bundle->cfg.ifqueue; 46461534Sbrian if (queued < ifqueue) { 46536285Sbrian /* Not enough - select() for more */ 46638544Sbrian if (bundle->choked.timer.state == TIMER_RUNNING) 46738544Sbrian timer_Stop(&bundle->choked.timer); /* Not needed any more */ 46836285Sbrian FD_SET(bundle->dev.fd, r); 46936285Sbrian if (*n < bundle->dev.fd + 1) 47036285Sbrian *n = bundle->dev.fd + 1; 47136452Sbrian log_Printf(LogTIMER, "%s: fdset(r) %d\n", TUN_NAME, bundle->dev.fd); 47236285Sbrian result++; 47338544Sbrian } else if (bundle->choked.timer.state == TIMER_STOPPED) { 47438544Sbrian bundle->choked.timer.func = bundle_ClearQueues; 47538544Sbrian bundle->choked.timer.name = "output choke"; 47638544Sbrian bundle->choked.timer.load = bundle->cfg.choked.timeout * SECTICKS; 47738544Sbrian bundle->choked.timer.arg = bundle; 47838544Sbrian timer_Start(&bundle->choked.timer); 47936285Sbrian } 48036285Sbrian } 48136285Sbrian } 48236285Sbrian 48343693Sbrian#ifndef NORADIUS 48443693Sbrian result += descriptor_UpdateSet(&bundle->radius.desc, r, w, e, n); 48543693Sbrian#endif 48643693Sbrian 48736714Sbrian /* Which links need a select() ? */ 48836714Sbrian for (dl = bundle->links; dl; dl = dl->next) 48936714Sbrian result += descriptor_UpdateSet(&dl->desc, r, w, e, n); 49036714Sbrian 49136285Sbrian /* 49236285Sbrian * This *MUST* be called after the datalink UpdateSet()s as it 49336285Sbrian * might be ``holding'' one of the datalinks (death-row) and 49458038Sbrian * wants to be able to de-select() it from the descriptor set. 49536285Sbrian */ 49636314Sbrian result += descriptor_UpdateSet(&bundle->ncp.mp.server.desc, r, w, e, n); 49736285Sbrian 49836285Sbrian return result; 49936285Sbrian} 50036285Sbrian 50136285Sbrianstatic int 50258028Sbrianbundle_IsSet(struct fdescriptor *d, const fd_set *fdset) 50336285Sbrian{ 50436285Sbrian struct bundle *bundle = descriptor2bundle(d); 50536285Sbrian struct datalink *dl; 50636285Sbrian 50736285Sbrian for (dl = bundle->links; dl; dl = dl->next) 50836285Sbrian if (descriptor_IsSet(&dl->desc, fdset)) 50936285Sbrian return 1; 51036285Sbrian 51143693Sbrian#ifndef NORADIUS 51243693Sbrian if (descriptor_IsSet(&bundle->radius.desc, fdset)) 51343693Sbrian return 1; 51443693Sbrian#endif 51543693Sbrian 51636285Sbrian if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 51736285Sbrian return 1; 51836285Sbrian 51936285Sbrian return FD_ISSET(bundle->dev.fd, fdset); 52036285Sbrian} 52136285Sbrian 52236285Sbrianstatic void 52358028Sbrianbundle_DescriptorRead(struct fdescriptor *d, struct bundle *bundle, 52436285Sbrian const fd_set *fdset) 52536285Sbrian{ 52636285Sbrian struct datalink *dl; 52762977Sbrian unsigned secs; 52836285Sbrian 52936285Sbrian if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 53036285Sbrian descriptor_Read(&bundle->ncp.mp.server.desc, bundle, fdset); 53136285Sbrian 53236285Sbrian for (dl = bundle->links; dl; dl = dl->next) 53336285Sbrian if (descriptor_IsSet(&dl->desc, fdset)) 53436285Sbrian descriptor_Read(&dl->desc, bundle, fdset); 53536285Sbrian 53643693Sbrian#ifndef NORADIUS 53743693Sbrian if (descriptor_IsSet(&bundle->radius.desc, fdset)) 53843693Sbrian descriptor_Read(&bundle->radius.desc, bundle, fdset); 53943693Sbrian#endif 54043693Sbrian 54136285Sbrian if (FD_ISSET(bundle->dev.fd, fdset)) { 54236285Sbrian struct tun_data tun; 54336285Sbrian int n, pri; 54456413Sbrian char *data; 54556413Sbrian size_t sz; 54636285Sbrian 54756413Sbrian if (bundle->dev.header) { 54856413Sbrian data = (char *)&tun; 54956413Sbrian sz = sizeof tun; 55056413Sbrian } else { 55156413Sbrian data = tun.data; 55256413Sbrian sz = sizeof tun.data; 55356413Sbrian } 55456413Sbrian 55536285Sbrian /* something to read from tun */ 55656413Sbrian 55756413Sbrian n = read(bundle->dev.fd, data, sz); 55836285Sbrian if (n < 0) { 55956413Sbrian log_Printf(LogWARN, "%s: read: %s\n", bundle->dev.Name, strerror(errno)); 56036285Sbrian return; 56136285Sbrian } 56256413Sbrian 56356413Sbrian if (bundle->dev.header) { 56456413Sbrian n -= sz - sizeof tun.data; 56556413Sbrian if (n <= 0) { 56656413Sbrian log_Printf(LogERROR, "%s: read: Got only %d bytes of data !\n", 56756413Sbrian bundle->dev.Name, n); 56856413Sbrian return; 56956413Sbrian } 57062977Sbrian if (ntohl(tun.header.family) != AF_INET) 57156413Sbrian /* XXX: Should be maintaining drop/family counts ! */ 57256413Sbrian return; 57336285Sbrian } 57436285Sbrian 57536285Sbrian if (((struct ip *)tun.data)->ip_dst.s_addr == 57636285Sbrian bundle->ncp.ipcp.my_ip.s_addr) { 57736285Sbrian /* we've been asked to send something addressed *to* us :( */ 57836285Sbrian if (Enabled(bundle, OPT_LOOPBACK)) { 57962977Sbrian pri = PacketCheck(bundle, tun.data, n, &bundle->filter.in, NULL, NULL); 58036285Sbrian if (pri >= 0) { 58156413Sbrian n += sz - sizeof tun.data; 58256413Sbrian write(bundle->dev.fd, data, n); 58336285Sbrian log_Printf(LogDEBUG, "Looped back packet addressed to myself\n"); 58436285Sbrian } 58536285Sbrian return; 58636285Sbrian } else 58736285Sbrian log_Printf(LogDEBUG, "Oops - forwarding packet addressed to myself\n"); 58836285Sbrian } 58936285Sbrian 59036285Sbrian /* 59136285Sbrian * Process on-demand dialup. Output packets are queued within tunnel 59236285Sbrian * device until IPCP is opened. 59336285Sbrian */ 59436285Sbrian 59536285Sbrian if (bundle_Phase(bundle) == PHASE_DEAD) { 59636285Sbrian /* 59736285Sbrian * Note, we must be in AUTO mode :-/ otherwise our interface should 59836285Sbrian * *not* be UP and we can't receive data 59936285Sbrian */ 60062977Sbrian pri = PacketCheck(bundle, tun.data, n, &bundle->filter.dial, NULL, NULL); 60162938Sbrian if (pri >= 0) 60237955Sbrian bundle_Open(bundle, NULL, PHYS_AUTO, 0); 60336285Sbrian else 60436285Sbrian /* 60536285Sbrian * Drop the packet. If we were to queue it, we'd just end up with 60636285Sbrian * a pile of timed-out data in our output queue by the time we get 60736285Sbrian * around to actually dialing. We'd also prematurely reach the 60836285Sbrian * threshold at which we stop select()ing to read() the tun 60936285Sbrian * device - breaking auto-dial. 61036285Sbrian */ 61136285Sbrian return; 61236285Sbrian } 61336285Sbrian 61462977Sbrian secs = 0; 61562977Sbrian pri = PacketCheck(bundle, tun.data, n, &bundle->filter.out, NULL, &secs); 61662977Sbrian if (pri >= 0) { 61762977Sbrian /* Prepend the number of seconds timeout given in the filter */ 61862977Sbrian tun.header.timeout = secs; 61962977Sbrian ip_Enqueue(&bundle->ncp.ipcp, pri, (char *)&tun, n + sizeof tun.header); 62062977Sbrian } 62136285Sbrian } 62236285Sbrian} 62336285Sbrian 62437141Sbrianstatic int 62558028Sbrianbundle_DescriptorWrite(struct fdescriptor *d, struct bundle *bundle, 62636285Sbrian const fd_set *fdset) 62736285Sbrian{ 62836285Sbrian struct datalink *dl; 62937141Sbrian int result = 0; 63036285Sbrian 63136285Sbrian /* This is not actually necessary as struct mpserver doesn't Write() */ 63236285Sbrian if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 63336285Sbrian descriptor_Write(&bundle->ncp.mp.server.desc, bundle, fdset); 63436285Sbrian 63536285Sbrian for (dl = bundle->links; dl; dl = dl->next) 63636285Sbrian if (descriptor_IsSet(&dl->desc, fdset)) 63737141Sbrian result += descriptor_Write(&dl->desc, bundle, fdset); 63837141Sbrian 63937141Sbrian return result; 64036285Sbrian} 64136285Sbrian 64236709Sbrianvoid 64336452Sbrianbundle_LockTun(struct bundle *bundle) 64436452Sbrian{ 64536452Sbrian FILE *lockfile; 64636452Sbrian char pidfile[MAXPATHLEN]; 64736285Sbrian 64836452Sbrian snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit); 64936452Sbrian lockfile = ID0fopen(pidfile, "w"); 65036452Sbrian if (lockfile != NULL) { 65136452Sbrian fprintf(lockfile, "%d\n", (int)getpid()); 65236452Sbrian fclose(lockfile); 65336452Sbrian } 65436452Sbrian#ifndef RELEASE_CRUNCH 65536452Sbrian else 65636452Sbrian log_Printf(LogERROR, "Warning: Can't create %s: %s\n", 65736452Sbrian pidfile, strerror(errno)); 65836452Sbrian#endif 65936452Sbrian} 66036452Sbrian 66136452Sbrianstatic void 66236452Sbrianbundle_UnlockTun(struct bundle *bundle) 66336452Sbrian{ 66436452Sbrian char pidfile[MAXPATHLEN]; 66536452Sbrian 66636452Sbrian snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit); 66736452Sbrian ID0unlink(pidfile); 66836452Sbrian} 66936452Sbrian 67036285Sbrianstruct bundle * 67153298Sbrianbundle_Create(const char *prefix, int type, int unit) 67236285Sbrian{ 67347538Sbrian static struct bundle bundle; /* there can be only one */ 67452396Sbrian int enoentcount, err, minunit, maxunit; 67540561Sbrian const char *ifname; 67653241Sbrian#if defined(__FreeBSD__) && !defined(NOKLDLOAD) 67751525Sbrian int kldtried; 67851525Sbrian#endif 67956413Sbrian#if defined(TUNSIFMODE) || defined(TUNSLMODE) || defined(TUNSIFHEAD) 68045032Sbrian int iff; 68145032Sbrian#endif 68236285Sbrian 68340561Sbrian if (bundle.iface != NULL) { /* Already allocated ! */ 68437019Sbrian log_Printf(LogALERT, "bundle_Create: There's only one BUNDLE !\n"); 68536285Sbrian return NULL; 68636285Sbrian } 68736285Sbrian 68852396Sbrian if (unit == -1) { 68952396Sbrian minunit = 0; 69052396Sbrian maxunit = -1; 69152396Sbrian } else { 69252396Sbrian minunit = unit; 69352396Sbrian maxunit = unit + 1; 69452396Sbrian } 69536285Sbrian err = ENOENT; 69636285Sbrian enoentcount = 0; 69753241Sbrian#if defined(__FreeBSD__) && !defined(NOKLDLOAD) 69851525Sbrian kldtried = 0; 69951525Sbrian#endif 70052396Sbrian for (bundle.unit = minunit; bundle.unit != maxunit; bundle.unit++) { 70136285Sbrian snprintf(bundle.dev.Name, sizeof bundle.dev.Name, "%s%d", 70236285Sbrian prefix, bundle.unit); 70336285Sbrian bundle.dev.fd = ID0open(bundle.dev.Name, O_RDWR); 70436285Sbrian if (bundle.dev.fd >= 0) 70536285Sbrian break; 70671912Sbrian else if (errno == ENXIO || errno == ENOENT) { 70753241Sbrian#if defined(__FreeBSD__) && !defined(NOKLDLOAD) 70852396Sbrian if (bundle.unit == minunit && !kldtried++) { 70951525Sbrian /* 71053241Sbrian * Attempt to load the tunnel interface KLD if it isn't loaded 71153241Sbrian * already. 71251525Sbrian */ 71353241Sbrian if (modfind("if_tun") == -1) { 71451525Sbrian if (ID0kldload("if_tun") != -1) { 71551525Sbrian bundle.unit--; 71651525Sbrian continue; 71751525Sbrian } 71851525Sbrian log_Printf(LogWARN, "kldload: if_tun: %s\n", strerror(errno)); 71951525Sbrian } 72051525Sbrian } 72151525Sbrian#endif 72236285Sbrian err = errno; 72336285Sbrian break; 72436285Sbrian } else if (errno == ENOENT) { 72536285Sbrian if (++enoentcount > 2) 72636285Sbrian break; 72736285Sbrian } else 72836285Sbrian err = errno; 72936285Sbrian } 73036285Sbrian 73136285Sbrian if (bundle.dev.fd < 0) { 73252396Sbrian if (unit == -1) 73352396Sbrian log_Printf(LogWARN, "No available tunnel devices found (%s)\n", 73452396Sbrian strerror(err)); 73552396Sbrian else 73652396Sbrian log_Printf(LogWARN, "%s%d: %s\n", prefix, unit, strerror(err)); 73736285Sbrian return NULL; 73836285Sbrian } 73936285Sbrian 74036285Sbrian log_SetTun(bundle.unit); 74136285Sbrian 74240561Sbrian ifname = strrchr(bundle.dev.Name, '/'); 74340561Sbrian if (ifname == NULL) 74440561Sbrian ifname = bundle.dev.Name; 74536285Sbrian else 74640561Sbrian ifname++; 74736285Sbrian 74840561Sbrian bundle.iface = iface_Create(ifname); 74940561Sbrian if (bundle.iface == NULL) { 75040561Sbrian close(bundle.dev.fd); 75140561Sbrian return NULL; 75240561Sbrian } 75340561Sbrian 75445032Sbrian#ifdef TUNSIFMODE 75545032Sbrian /* Make sure we're POINTOPOINT */ 75645032Sbrian iff = IFF_POINTOPOINT; 75745032Sbrian if (ID0ioctl(bundle.dev.fd, TUNSIFMODE, &iff) < 0) 75845032Sbrian log_Printf(LogERROR, "bundle_Create: ioctl(TUNSIFMODE): %s\n", 75945032Sbrian strerror(errno)); 76045032Sbrian#endif 76145032Sbrian 76248103Sbrian#ifdef TUNSLMODE 76356413Sbrian /* Make sure we're not prepending sockaddrs */ 76448103Sbrian iff = 0; 76548103Sbrian if (ID0ioctl(bundle.dev.fd, TUNSLMODE, &iff) < 0) 76648103Sbrian log_Printf(LogERROR, "bundle_Create: ioctl(TUNSLMODE): %s\n", 76748103Sbrian strerror(errno)); 76848103Sbrian#endif 76948103Sbrian 77056413Sbrian#ifdef TUNSIFHEAD 77156413Sbrian /* We want the address family please ! */ 77256413Sbrian iff = 1; 77356413Sbrian if (ID0ioctl(bundle.dev.fd, TUNSIFHEAD, &iff) < 0) { 77456413Sbrian log_Printf(LogERROR, "bundle_Create: ioctl(TUNSIFHEAD): %s\n", 77556413Sbrian strerror(errno)); 77656413Sbrian bundle.dev.header = 0; 77756413Sbrian } else 77856413Sbrian bundle.dev.header = 1; 77956413Sbrian#else 78056413Sbrian#ifdef __OpenBSD__ 78156413Sbrian /* Always present for OpenBSD */ 78256413Sbrian bundle.dev.header = 1; 78356413Sbrian#else 78456413Sbrian /* 78556413Sbrian * If TUNSIFHEAD isn't available and we're not OpenBSD, assume 78656413Sbrian * everything's AF_INET (hopefully the tun device won't pass us 78756413Sbrian * anything else !). 78856413Sbrian */ 78956413Sbrian bundle.dev.header = 0; 79056413Sbrian#endif 79156413Sbrian#endif 79256413Sbrian 79347538Sbrian if (!iface_SetFlags(bundle.iface, IFF_UP)) { 79440561Sbrian iface_Destroy(bundle.iface); 79540561Sbrian bundle.iface = NULL; 79636285Sbrian close(bundle.dev.fd); 79736285Sbrian return NULL; 79836285Sbrian } 79936285Sbrian 80040561Sbrian log_Printf(LogPHASE, "Using interface: %s\n", ifname); 80136285Sbrian 80249434Sbrian bundle.bandwidth = 0; 80369303Sbrian bundle.mtu = 1500; 80436285Sbrian bundle.routing_seq = 0; 80536285Sbrian bundle.phase = PHASE_DEAD; 80636285Sbrian bundle.CleaningUp = 0; 80750059Sbrian bundle.NatEnabled = 0; 80836285Sbrian 80936285Sbrian bundle.fsm.LayerStart = bundle_LayerStart; 81036285Sbrian bundle.fsm.LayerUp = bundle_LayerUp; 81136285Sbrian bundle.fsm.LayerDown = bundle_LayerDown; 81236285Sbrian bundle.fsm.LayerFinish = bundle_LayerFinish; 81336285Sbrian bundle.fsm.object = &bundle; 81436285Sbrian 81549978Sbrian bundle.cfg.idle.timeout = NCP_IDLE_TIMEOUT; 81649978Sbrian bundle.cfg.idle.min_timeout = 0; 81736285Sbrian *bundle.cfg.auth.name = '\0'; 81836285Sbrian *bundle.cfg.auth.key = '\0'; 81969303Sbrian bundle.cfg.opt = OPT_SROUTES | OPT_IDCHECK | OPT_LOOPBACK | OPT_TCPMSSFIXUP | 82036285Sbrian OPT_THROUGHPUT | OPT_UTMP; 82136285Sbrian *bundle.cfg.label = '\0'; 82236285Sbrian bundle.cfg.mtu = DEF_MTU; 82361534Sbrian bundle.cfg.ifqueue = DEF_IFQUEUE; 82438544Sbrian bundle.cfg.choked.timeout = CHOKED_TIMEOUT; 82536928Sbrian bundle.phys_type.all = type; 82636928Sbrian bundle.phys_type.open = 0; 82749978Sbrian bundle.upat = 0; 82836285Sbrian 82936285Sbrian bundle.links = datalink_Create("deflink", &bundle, type); 83036285Sbrian if (bundle.links == NULL) { 83137019Sbrian log_Printf(LogALERT, "Cannot create data link: %s\n", strerror(errno)); 83240561Sbrian iface_Destroy(bundle.iface); 83340561Sbrian bundle.iface = NULL; 83436285Sbrian close(bundle.dev.fd); 83536285Sbrian return NULL; 83636285Sbrian } 83736285Sbrian 83836285Sbrian bundle.desc.type = BUNDLE_DESCRIPTOR; 83936285Sbrian bundle.desc.UpdateSet = bundle_UpdateSet; 84036285Sbrian bundle.desc.IsSet = bundle_IsSet; 84136285Sbrian bundle.desc.Read = bundle_DescriptorRead; 84236285Sbrian bundle.desc.Write = bundle_DescriptorWrite; 84336285Sbrian 84436285Sbrian mp_Init(&bundle.ncp.mp, &bundle); 84536285Sbrian 84636285Sbrian /* Send over the first physical link by default */ 84736285Sbrian ipcp_Init(&bundle.ncp.ipcp, &bundle, &bundle.links->physical->link, 84836285Sbrian &bundle.fsm); 84936285Sbrian 85036285Sbrian memset(&bundle.filter, '\0', sizeof bundle.filter); 85136285Sbrian bundle.filter.in.fragok = bundle.filter.in.logok = 1; 85236285Sbrian bundle.filter.in.name = "IN"; 85336285Sbrian bundle.filter.out.fragok = bundle.filter.out.logok = 1; 85436285Sbrian bundle.filter.out.name = "OUT"; 85536285Sbrian bundle.filter.dial.name = "DIAL"; 85636285Sbrian bundle.filter.dial.logok = 1; 85736285Sbrian bundle.filter.alive.name = "ALIVE"; 85836285Sbrian bundle.filter.alive.logok = 1; 85949140Sbrian { 86049140Sbrian int i; 86149140Sbrian for (i = 0; i < MAXFILTERS; i++) { 86249140Sbrian bundle.filter.in.rule[i].f_action = A_NONE; 86349140Sbrian bundle.filter.out.rule[i].f_action = A_NONE; 86449140Sbrian bundle.filter.dial.rule[i].f_action = A_NONE; 86549140Sbrian bundle.filter.alive.rule[i].f_action = A_NONE; 86649140Sbrian } 86749140Sbrian } 86836285Sbrian memset(&bundle.idle.timer, '\0', sizeof bundle.idle.timer); 86936285Sbrian bundle.idle.done = 0; 87036285Sbrian bundle.notify.fd = -1; 87138544Sbrian memset(&bundle.choked.timer, '\0', sizeof bundle.choked.timer); 87243313Sbrian#ifndef NORADIUS 87343313Sbrian radius_Init(&bundle.radius); 87443313Sbrian#endif 87536285Sbrian 87636285Sbrian /* Clean out any leftover crud */ 87740561Sbrian iface_Clear(bundle.iface, IFACE_CLEAR_ALL); 87836285Sbrian 87936452Sbrian bundle_LockTun(&bundle); 88036452Sbrian 88136285Sbrian return &bundle; 88236285Sbrian} 88336285Sbrian 88436285Sbrianstatic void 88536285Sbrianbundle_DownInterface(struct bundle *bundle) 88636285Sbrian{ 88736285Sbrian route_IfDelete(bundle, 1); 88847538Sbrian iface_ClearFlags(bundle->iface, IFF_UP); 88936285Sbrian} 89036285Sbrian 89136285Sbrianvoid 89236285Sbrianbundle_Destroy(struct bundle *bundle) 89336285Sbrian{ 89436285Sbrian struct datalink *dl; 89536285Sbrian 89636285Sbrian /* 89736285Sbrian * Clean up the interface. We don't need to timer_Stop()s, mp_Down(), 89836285Sbrian * ipcp_CleanInterface() and bundle_DownInterface() unless we're getting 89958038Sbrian * out under exceptional conditions such as a descriptor exception. 90036285Sbrian */ 90136285Sbrian timer_Stop(&bundle->idle.timer); 90238544Sbrian timer_Stop(&bundle->choked.timer); 90336285Sbrian mp_Down(&bundle->ncp.mp); 90436285Sbrian ipcp_CleanInterface(&bundle->ncp.ipcp); 90536285Sbrian bundle_DownInterface(bundle); 90636452Sbrian 90743313Sbrian#ifndef NORADIUS 90843313Sbrian /* Tell the radius server the bad news */ 90965178Sbrian log_Printf(LogDEBUG, "Radius: Destroy called from bundle_Destroy\n"); 91043313Sbrian radius_Destroy(&bundle->radius); 91143313Sbrian#endif 91243313Sbrian 91336285Sbrian /* Again, these are all DATALINK_CLOSED unless we're abending */ 91436285Sbrian dl = bundle->links; 91536285Sbrian while (dl) 91636285Sbrian dl = datalink_Destroy(dl); 91736285Sbrian 91850867Sbrian ipcp_Destroy(&bundle->ncp.ipcp); 91950867Sbrian 92036452Sbrian close(bundle->dev.fd); 92136452Sbrian bundle_UnlockTun(bundle); 92236452Sbrian 92336285Sbrian /* In case we never made PHASE_NETWORK */ 92436285Sbrian bundle_Notify(bundle, EX_ERRDEAD); 92536285Sbrian 92640561Sbrian iface_Destroy(bundle->iface); 92740561Sbrian bundle->iface = NULL; 92836285Sbrian} 92936285Sbrian 93036285Sbrianstruct rtmsg { 93136285Sbrian struct rt_msghdr m_rtm; 93236285Sbrian char m_space[64]; 93336285Sbrian}; 93436285Sbrian 93536285Sbrianint 93636285Sbrianbundle_SetRoute(struct bundle *bundle, int cmd, struct in_addr dst, 93737927Sbrian struct in_addr gateway, struct in_addr mask, int bang, int ssh) 93836285Sbrian{ 93936285Sbrian struct rtmsg rtmes; 94036285Sbrian int s, nb, wb; 94136285Sbrian char *cp; 94236285Sbrian const char *cmdstr; 94336285Sbrian struct sockaddr_in rtdata; 94436285Sbrian int result = 1; 94536285Sbrian 94636285Sbrian if (bang) 94736285Sbrian cmdstr = (cmd == RTM_ADD ? "Add!" : "Delete!"); 94836285Sbrian else 94936285Sbrian cmdstr = (cmd == RTM_ADD ? "Add" : "Delete"); 95036285Sbrian s = ID0socket(PF_ROUTE, SOCK_RAW, 0); 95136285Sbrian if (s < 0) { 95236285Sbrian log_Printf(LogERROR, "bundle_SetRoute: socket(): %s\n", strerror(errno)); 95336285Sbrian return result; 95436285Sbrian } 95536285Sbrian memset(&rtmes, '\0', sizeof rtmes); 95636285Sbrian rtmes.m_rtm.rtm_version = RTM_VERSION; 95736285Sbrian rtmes.m_rtm.rtm_type = cmd; 95836285Sbrian rtmes.m_rtm.rtm_addrs = RTA_DST; 95936285Sbrian rtmes.m_rtm.rtm_seq = ++bundle->routing_seq; 96036285Sbrian rtmes.m_rtm.rtm_pid = getpid(); 96136285Sbrian rtmes.m_rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC; 96236285Sbrian 96340665Sbrian if (cmd == RTM_ADD || cmd == RTM_CHANGE) { 96440665Sbrian if (bundle->ncp.ipcp.cfg.sendpipe > 0) { 96540665Sbrian rtmes.m_rtm.rtm_rmx.rmx_sendpipe = bundle->ncp.ipcp.cfg.sendpipe; 96640665Sbrian rtmes.m_rtm.rtm_inits |= RTV_SPIPE; 96740665Sbrian } 96840665Sbrian if (bundle->ncp.ipcp.cfg.recvpipe > 0) { 96940665Sbrian rtmes.m_rtm.rtm_rmx.rmx_recvpipe = bundle->ncp.ipcp.cfg.recvpipe; 97040665Sbrian rtmes.m_rtm.rtm_inits |= RTV_RPIPE; 97140665Sbrian } 97240665Sbrian } 97340665Sbrian 97436285Sbrian memset(&rtdata, '\0', sizeof rtdata); 97536285Sbrian rtdata.sin_len = sizeof rtdata; 97636285Sbrian rtdata.sin_family = AF_INET; 97736285Sbrian rtdata.sin_port = 0; 97836285Sbrian rtdata.sin_addr = dst; 97936285Sbrian 98036285Sbrian cp = rtmes.m_space; 98136285Sbrian memcpy(cp, &rtdata, rtdata.sin_len); 98236285Sbrian cp += rtdata.sin_len; 98336285Sbrian if (cmd == RTM_ADD) { 98436285Sbrian if (gateway.s_addr == INADDR_ANY) { 98542321Sbrian if (!ssh) 98642321Sbrian log_Printf(LogERROR, "bundle_SetRoute: Cannot add a route with" 98742321Sbrian " destination 0.0.0.0\n"); 98840561Sbrian close(s); 98940561Sbrian return result; 99036285Sbrian } else { 99136285Sbrian rtdata.sin_addr = gateway; 99236285Sbrian memcpy(cp, &rtdata, rtdata.sin_len); 99336285Sbrian cp += rtdata.sin_len; 99436285Sbrian rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY; 99536285Sbrian } 99636285Sbrian } 99736285Sbrian 99836285Sbrian if (dst.s_addr == INADDR_ANY) 99936285Sbrian mask.s_addr = INADDR_ANY; 100036285Sbrian 100136285Sbrian if (cmd == RTM_ADD || dst.s_addr == INADDR_ANY) { 100236285Sbrian rtdata.sin_addr = mask; 100336285Sbrian memcpy(cp, &rtdata, rtdata.sin_len); 100436285Sbrian cp += rtdata.sin_len; 100536285Sbrian rtmes.m_rtm.rtm_addrs |= RTA_NETMASK; 100636285Sbrian } 100736285Sbrian 100836285Sbrian nb = cp - (char *) &rtmes; 100936285Sbrian rtmes.m_rtm.rtm_msglen = nb; 101036285Sbrian wb = ID0write(s, &rtmes, nb); 101136285Sbrian if (wb < 0) { 101236285Sbrian log_Printf(LogTCPIP, "bundle_SetRoute failure:\n"); 101336285Sbrian log_Printf(LogTCPIP, "bundle_SetRoute: Cmd = %s\n", cmdstr); 101436285Sbrian log_Printf(LogTCPIP, "bundle_SetRoute: Dst = %s\n", inet_ntoa(dst)); 101553684Sbrian log_Printf(LogTCPIP, "bundle_SetRoute: Gateway = %s\n", 101653684Sbrian inet_ntoa(gateway)); 101736285Sbrian log_Printf(LogTCPIP, "bundle_SetRoute: Mask = %s\n", inet_ntoa(mask)); 101836285Sbrianfailed: 101936285Sbrian if (cmd == RTM_ADD && (rtmes.m_rtm.rtm_errno == EEXIST || 102036285Sbrian (rtmes.m_rtm.rtm_errno == 0 && errno == EEXIST))) { 102136285Sbrian if (!bang) { 102236285Sbrian log_Printf(LogWARN, "Add route failed: %s already exists\n", 102340665Sbrian dst.s_addr == 0 ? "default" : inet_ntoa(dst)); 102436285Sbrian result = 0; /* Don't add to our dynamic list */ 102536285Sbrian } else { 102636285Sbrian rtmes.m_rtm.rtm_type = cmd = RTM_CHANGE; 102736285Sbrian if ((wb = ID0write(s, &rtmes, nb)) < 0) 102836285Sbrian goto failed; 102936285Sbrian } 103036285Sbrian } else if (cmd == RTM_DELETE && 103136285Sbrian (rtmes.m_rtm.rtm_errno == ESRCH || 103236285Sbrian (rtmes.m_rtm.rtm_errno == 0 && errno == ESRCH))) { 103336285Sbrian if (!bang) 103436285Sbrian log_Printf(LogWARN, "Del route failed: %s: Non-existent\n", 103536285Sbrian inet_ntoa(dst)); 103637927Sbrian } else if (rtmes.m_rtm.rtm_errno == 0) { 103737927Sbrian if (!ssh || errno != ENETUNREACH) 103837927Sbrian log_Printf(LogWARN, "%s route failed: %s: errno: %s\n", cmdstr, 103937927Sbrian inet_ntoa(dst), strerror(errno)); 104037927Sbrian } else 104136285Sbrian log_Printf(LogWARN, "%s route failed: %s: %s\n", 104237927Sbrian cmdstr, inet_ntoa(dst), strerror(rtmes.m_rtm.rtm_errno)); 104336285Sbrian } 104436285Sbrian log_Printf(LogDEBUG, "wrote %d: cmd = %s, dst = %x, gateway = %x\n", 104536285Sbrian wb, cmdstr, (unsigned)dst.s_addr, (unsigned)gateway.s_addr); 104636285Sbrian close(s); 104736285Sbrian 104836285Sbrian return result; 104936285Sbrian} 105036285Sbrian 105136285Sbrianvoid 105236285Sbrianbundle_LinkClosed(struct bundle *bundle, struct datalink *dl) 105336285Sbrian{ 105436285Sbrian /* 105536285Sbrian * Our datalink has closed. 105636285Sbrian * CleanDatalinks() (called from DoLoop()) will remove closed 105753830Sbrian * BACKGROUND, FOREGROUND and DIRECT links. 105836285Sbrian * If it's the last data link, enter phase DEAD. 105936285Sbrian * 106036285Sbrian * NOTE: dl may not be in our list (bundle_SendDatalink()) ! 106136285Sbrian */ 106236285Sbrian 106336285Sbrian struct datalink *odl; 106436285Sbrian int other_links; 106536285Sbrian 106638200Sbrian log_SetTtyCommandMode(dl); 106738200Sbrian 106836285Sbrian other_links = 0; 106936285Sbrian for (odl = bundle->links; odl; odl = odl->next) 107036285Sbrian if (odl != dl && odl->state != DATALINK_CLOSED) 107136285Sbrian other_links++; 107236285Sbrian 107336285Sbrian if (!other_links) { 107436465Sbrian if (dl->physical->type != PHYS_AUTO) /* Not in -auto mode */ 107536285Sbrian bundle_DownInterface(bundle); 107637060Sbrian fsm2initial(&bundle->ncp.ipcp.fsm); 107736285Sbrian bundle_NewPhase(bundle, PHASE_DEAD); 107836314Sbrian bundle_StopIdleTimer(bundle); 107949434Sbrian } 108036285Sbrian} 108136285Sbrian 108236285Sbrianvoid 108337955Sbrianbundle_Open(struct bundle *bundle, const char *name, int mask, int force) 108436285Sbrian{ 108536285Sbrian /* 108636285Sbrian * Please open the given datalink, or all if name == NULL 108736285Sbrian */ 108836285Sbrian struct datalink *dl; 108936285Sbrian 109036285Sbrian for (dl = bundle->links; dl; dl = dl->next) 109136285Sbrian if (name == NULL || !strcasecmp(dl->name, name)) { 109237955Sbrian if ((mask & dl->physical->type) && 109337955Sbrian (dl->state == DATALINK_CLOSED || 109437955Sbrian (force && dl->state == DATALINK_OPENING && 109560945Sbrian dl->dial.timer.state == TIMER_RUNNING) || 109660945Sbrian dl->state == DATALINK_READY)) { 109760945Sbrian timer_Stop(&dl->dial.timer); /* We're finished with this */ 109836285Sbrian datalink_Up(dl, 1, 1); 109949434Sbrian if (mask & PHYS_AUTO) 110060945Sbrian break; /* Only one AUTO link at a time */ 110136285Sbrian } 110236285Sbrian if (name != NULL) 110336285Sbrian break; 110436285Sbrian } 110536285Sbrian} 110636285Sbrian 110736285Sbrianstruct datalink * 110836285Sbrianbundle2datalink(struct bundle *bundle, const char *name) 110936285Sbrian{ 111036285Sbrian struct datalink *dl; 111136285Sbrian 111236285Sbrian if (name != NULL) { 111336285Sbrian for (dl = bundle->links; dl; dl = dl->next) 111436285Sbrian if (!strcasecmp(dl->name, name)) 111536285Sbrian return dl; 111636285Sbrian } else if (bundle->links && !bundle->links->next) 111736285Sbrian return bundle->links; 111836285Sbrian 111936285Sbrian return NULL; 112036285Sbrian} 112136285Sbrian 112236285Sbrianint 112336285Sbrianbundle_ShowLinks(struct cmdargs const *arg) 112436285Sbrian{ 112536285Sbrian struct datalink *dl; 112649434Sbrian struct pppThroughput *t; 112764670Sbrian unsigned long long octets; 112849434Sbrian int secs; 112936285Sbrian 113036285Sbrian for (dl = arg->bundle->links; dl; dl = dl->next) { 113164670Sbrian octets = MAX(dl->physical->link.stats.total.in.OctetsPerSecond, 113264670Sbrian dl->physical->link.stats.total.out.OctetsPerSecond); 113364670Sbrian 113436316Sbrian prompt_Printf(arg->prompt, "Name: %s [%s, %s]", 113536316Sbrian dl->name, mode2Nam(dl->physical->type), datalink_State(dl)); 113664652Sbrian if (dl->physical->link.stats.total.rolling && dl->state == DATALINK_OPEN) 113749582Sbrian prompt_Printf(arg->prompt, " bandwidth %d, %llu bps (%llu bytes/sec)", 113849434Sbrian dl->mp.bandwidth ? dl->mp.bandwidth : 113949434Sbrian physical_GetSpeed(dl->physical), 114064670Sbrian octets * 8, octets); 114136285Sbrian prompt_Printf(arg->prompt, "\n"); 114236285Sbrian } 114336285Sbrian 114464652Sbrian t = &arg->bundle->ncp.mp.link.stats.total; 114564670Sbrian octets = MAX(t->in.OctetsPerSecond, t->out.OctetsPerSecond); 114649434Sbrian secs = t->downtime ? 0 : throughput_uptime(t); 114749434Sbrian if (secs > t->SamplePeriod) 114849434Sbrian secs = t->SamplePeriod; 114949434Sbrian if (secs) 115049582Sbrian prompt_Printf(arg->prompt, "Currently averaging %llu bps (%llu bytes/sec)" 115164670Sbrian " over the last %d secs\n", octets * 8, octets, secs); 115249434Sbrian 115336285Sbrian return 0; 115436285Sbrian} 115536285Sbrian 115636285Sbrianstatic const char * 115736285Sbrianoptval(struct bundle *bundle, int bit) 115836285Sbrian{ 115936285Sbrian return (bundle->cfg.opt & bit) ? "enabled" : "disabled"; 116036285Sbrian} 116136285Sbrian 116236285Sbrianint 116336285Sbrianbundle_ShowStatus(struct cmdargs const *arg) 116436285Sbrian{ 116536285Sbrian int remaining; 116636285Sbrian 116736285Sbrian prompt_Printf(arg->prompt, "Phase %s\n", bundle_PhaseName(arg->bundle)); 116836285Sbrian prompt_Printf(arg->prompt, " Device: %s\n", arg->bundle->dev.Name); 116949978Sbrian prompt_Printf(arg->prompt, " Interface: %s @ %lubps", 117049434Sbrian arg->bundle->iface->name, arg->bundle->bandwidth); 117136285Sbrian 117249978Sbrian if (arg->bundle->upat) { 117349978Sbrian int secs = time(NULL) - arg->bundle->upat; 117449978Sbrian 117549978Sbrian prompt_Printf(arg->prompt, ", up time %d:%02d:%02d", secs / 3600, 117649978Sbrian (secs / 60) % 60, secs % 60); 117749978Sbrian } 117861800Sbrian prompt_Printf(arg->prompt, "\n Queued: %lu of %u\n", 117962000Sbrian (unsigned long)ip_QueueLen(&arg->bundle->ncp.ipcp), 118062000Sbrian arg->bundle->cfg.ifqueue); 118149978Sbrian 118261534Sbrian prompt_Printf(arg->prompt, "\nDefaults:\n"); 118371657Sbrian prompt_Printf(arg->prompt, " Label: %s\n", 118471657Sbrian arg->bundle->cfg.label); 118571657Sbrian prompt_Printf(arg->prompt, " Auth name: %s\n", 118636285Sbrian arg->bundle->cfg.auth.name); 118771657Sbrian prompt_Printf(arg->prompt, " Diagnostic socket: "); 118871764Sbrian if (*server.cfg.sockname != '\0') { 118971764Sbrian prompt_Printf(arg->prompt, "%s", server.cfg.sockname); 119071764Sbrian if (server.cfg.mask != (mode_t)-1) 119171764Sbrian prompt_Printf(arg->prompt, ", mask 0%03o", (int)server.cfg.mask); 119271764Sbrian prompt_Printf(arg->prompt, "%s\n", server.fd == -1 ? " (not open)" : ""); 119371764Sbrian } else if (server.cfg.port != 0) 119471657Sbrian prompt_Printf(arg->prompt, "TCP port %d%s\n", server.cfg.port, 119571657Sbrian server.fd == -1 ? " (not open)" : ""); 119671657Sbrian else 119771657Sbrian prompt_Printf(arg->prompt, "none\n"); 119836285Sbrian 119971657Sbrian prompt_Printf(arg->prompt, " Choked Timer: %ds\n", 120038544Sbrian arg->bundle->cfg.choked.timeout); 120143313Sbrian 120243313Sbrian#ifndef NORADIUS 120343313Sbrian radius_Show(&arg->bundle->radius, arg->prompt); 120443313Sbrian#endif 120543313Sbrian 120671657Sbrian prompt_Printf(arg->prompt, " Idle Timer: "); 120749978Sbrian if (arg->bundle->cfg.idle.timeout) { 120849978Sbrian prompt_Printf(arg->prompt, "%ds", arg->bundle->cfg.idle.timeout); 120949978Sbrian if (arg->bundle->cfg.idle.min_timeout) 121049978Sbrian prompt_Printf(arg->prompt, ", min %ds", 121149978Sbrian arg->bundle->cfg.idle.min_timeout); 121236285Sbrian remaining = bundle_RemainingIdleTime(arg->bundle); 121336285Sbrian if (remaining != -1) 121436285Sbrian prompt_Printf(arg->prompt, " (%ds remaining)", remaining); 121536285Sbrian prompt_Printf(arg->prompt, "\n"); 121636285Sbrian } else 121736285Sbrian prompt_Printf(arg->prompt, "disabled\n"); 121871657Sbrian prompt_Printf(arg->prompt, " MTU: "); 121936285Sbrian if (arg->bundle->cfg.mtu) 122036285Sbrian prompt_Printf(arg->prompt, "%d\n", arg->bundle->cfg.mtu); 122136285Sbrian else 122236285Sbrian prompt_Printf(arg->prompt, "unspecified\n"); 122336285Sbrian 122471657Sbrian prompt_Printf(arg->prompt, " sendpipe: "); 122540665Sbrian if (arg->bundle->ncp.ipcp.cfg.sendpipe > 0) 122649434Sbrian prompt_Printf(arg->prompt, "%-20ld", arg->bundle->ncp.ipcp.cfg.sendpipe); 122740665Sbrian else 122849434Sbrian prompt_Printf(arg->prompt, "unspecified "); 122940665Sbrian prompt_Printf(arg->prompt, " recvpipe: "); 123040665Sbrian if (arg->bundle->ncp.ipcp.cfg.recvpipe > 0) 123140665Sbrian prompt_Printf(arg->prompt, "%ld\n", arg->bundle->ncp.ipcp.cfg.recvpipe); 123240665Sbrian else 123340665Sbrian prompt_Printf(arg->prompt, "unspecified\n"); 123440665Sbrian 123571657Sbrian prompt_Printf(arg->prompt, " Sticky Routes: %-20.20s", 123636285Sbrian optval(arg->bundle, OPT_SROUTES)); 123771657Sbrian prompt_Printf(arg->prompt, " Filter Decap: %s\n", 123862778Sbrian optval(arg->bundle, OPT_FILTERDECAP)); 123971657Sbrian prompt_Printf(arg->prompt, " ID check: %-20.20s", 124036285Sbrian optval(arg->bundle, OPT_IDCHECK)); 124171657Sbrian prompt_Printf(arg->prompt, " Keep-Session: %s\n", 124247689Sbrian optval(arg->bundle, OPT_KEEPSESSION)); 124371657Sbrian prompt_Printf(arg->prompt, " Loopback: %-20.20s", 124436285Sbrian optval(arg->bundle, OPT_LOOPBACK)); 124571657Sbrian prompt_Printf(arg->prompt, " PasswdAuth: %s\n", 124636285Sbrian optval(arg->bundle, OPT_PASSWDAUTH)); 124771657Sbrian prompt_Printf(arg->prompt, " Proxy: %-20.20s", 124836285Sbrian optval(arg->bundle, OPT_PROXY)); 124971657Sbrian prompt_Printf(arg->prompt, " Proxyall: %s\n", 125040665Sbrian optval(arg->bundle, OPT_PROXYALL)); 125171657Sbrian prompt_Printf(arg->prompt, " TCPMSS Fixup: %-20.20s", 125269303Sbrian optval(arg->bundle, OPT_TCPMSSFIXUP)); 125371657Sbrian prompt_Printf(arg->prompt, " Throughput: %s\n", 125436285Sbrian optval(arg->bundle, OPT_THROUGHPUT)); 125571657Sbrian prompt_Printf(arg->prompt, " Utmp Logging: %-20.20s", 125636285Sbrian optval(arg->bundle, OPT_UTMP)); 125771657Sbrian prompt_Printf(arg->prompt, " Iface-Alias: %s\n", 125840561Sbrian optval(arg->bundle, OPT_IFACEALIAS)); 125936285Sbrian 126036285Sbrian return 0; 126136285Sbrian} 126236285Sbrian 126336285Sbrianstatic void 126436285Sbrianbundle_IdleTimeout(void *v) 126536285Sbrian{ 126636285Sbrian struct bundle *bundle = (struct bundle *)v; 126736285Sbrian 126859084Sbrian log_Printf(LogPHASE, "Idle timer expired\n"); 126936285Sbrian bundle_StopIdleTimer(bundle); 127037007Sbrian bundle_Close(bundle, NULL, CLOSE_STAYDOWN); 127136285Sbrian} 127236285Sbrian 127336285Sbrian/* 127436285Sbrian * Start Idle timer. If timeout is reached, we call bundle_Close() to 127536285Sbrian * close LCP and link. 127636285Sbrian */ 127736285Sbrianvoid 127862977Sbrianbundle_StartIdleTimer(struct bundle *bundle, unsigned secs) 127936285Sbrian{ 128036285Sbrian timer_Stop(&bundle->idle.timer); 128136928Sbrian if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) != 128249978Sbrian bundle->phys_type.open && bundle->cfg.idle.timeout) { 128362977Sbrian time_t now = time(NULL); 128449978Sbrian 128562977Sbrian if (secs == 0) 128662977Sbrian secs = bundle->cfg.idle.timeout; 128762977Sbrian 128862977Sbrian /* We want at least `secs' */ 128949978Sbrian if (bundle->cfg.idle.min_timeout > secs && bundle->upat) { 129062977Sbrian int up = now - bundle->upat; 129149978Sbrian 129249978Sbrian if ((long long)bundle->cfg.idle.min_timeout - up > (long long)secs) 129362977Sbrian /* Only increase from the current `remaining' value */ 129449978Sbrian secs = bundle->cfg.idle.min_timeout - up; 129549978Sbrian } 129636285Sbrian bundle->idle.timer.func = bundle_IdleTimeout; 129736285Sbrian bundle->idle.timer.name = "idle"; 129849978Sbrian bundle->idle.timer.load = secs * SECTICKS; 129936285Sbrian bundle->idle.timer.arg = bundle; 130036285Sbrian timer_Start(&bundle->idle.timer); 130162977Sbrian bundle->idle.done = now + secs; 130236285Sbrian } 130336285Sbrian} 130436285Sbrian 130536285Sbrianvoid 130649978Sbrianbundle_SetIdleTimer(struct bundle *bundle, int timeout, int min_timeout) 130736285Sbrian{ 130849978Sbrian bundle->cfg.idle.timeout = timeout; 130949978Sbrian if (min_timeout >= 0) 131049978Sbrian bundle->cfg.idle.min_timeout = min_timeout; 131136285Sbrian if (bundle_LinkIsUp(bundle)) 131262977Sbrian bundle_StartIdleTimer(bundle, 0); 131336285Sbrian} 131436285Sbrian 131536285Sbrianvoid 131636285Sbrianbundle_StopIdleTimer(struct bundle *bundle) 131736285Sbrian{ 131836285Sbrian timer_Stop(&bundle->idle.timer); 131936285Sbrian bundle->idle.done = 0; 132036285Sbrian} 132136285Sbrian 132236285Sbrianstatic int 132336285Sbrianbundle_RemainingIdleTime(struct bundle *bundle) 132436285Sbrian{ 132536285Sbrian if (bundle->idle.done) 132636285Sbrian return bundle->idle.done - time(NULL); 132736285Sbrian return -1; 132836285Sbrian} 132936285Sbrian 133036285Sbrianint 133136285Sbrianbundle_IsDead(struct bundle *bundle) 133236285Sbrian{ 133336285Sbrian return !bundle->links || (bundle->phase == PHASE_DEAD && bundle->CleaningUp); 133436285Sbrian} 133536285Sbrian 133636285Sbrianstatic struct datalink * 133736285Sbrianbundle_DatalinkLinkout(struct bundle *bundle, struct datalink *dl) 133836285Sbrian{ 133936285Sbrian struct datalink **dlp; 134036285Sbrian 134136314Sbrian for (dlp = &bundle->links; *dlp; dlp = &(*dlp)->next) 134236314Sbrian if (*dlp == dl) { 134336314Sbrian *dlp = dl->next; 134436314Sbrian dl->next = NULL; 134536314Sbrian bundle_LinksRemoved(bundle); 134636314Sbrian return dl; 134736314Sbrian } 134836285Sbrian 134936285Sbrian return NULL; 135036285Sbrian} 135136285Sbrian 135236285Sbrianstatic void 135336285Sbrianbundle_DatalinkLinkin(struct bundle *bundle, struct datalink *dl) 135436285Sbrian{ 135536285Sbrian struct datalink **dlp = &bundle->links; 135636285Sbrian 135736285Sbrian while (*dlp) 135836285Sbrian dlp = &(*dlp)->next; 135936285Sbrian 136036285Sbrian *dlp = dl; 136136285Sbrian dl->next = NULL; 136236285Sbrian 136336285Sbrian bundle_LinkAdded(bundle, dl); 136449434Sbrian mp_CheckAutoloadTimer(&bundle->ncp.mp); 136536285Sbrian} 136636285Sbrian 136736285Sbrianvoid 136836285Sbrianbundle_CleanDatalinks(struct bundle *bundle) 136936285Sbrian{ 137036285Sbrian struct datalink **dlp = &bundle->links; 137136285Sbrian int found = 0; 137236285Sbrian 137336285Sbrian while (*dlp) 137436285Sbrian if ((*dlp)->state == DATALINK_CLOSED && 137553830Sbrian (*dlp)->physical->type & 137653830Sbrian (PHYS_DIRECT|PHYS_BACKGROUND|PHYS_FOREGROUND)) { 137736285Sbrian *dlp = datalink_Destroy(*dlp); 137836285Sbrian found++; 137936285Sbrian } else 138036285Sbrian dlp = &(*dlp)->next; 138136285Sbrian 138236285Sbrian if (found) 138336285Sbrian bundle_LinksRemoved(bundle); 138436285Sbrian} 138536285Sbrian 138636285Sbrianint 138736285Sbrianbundle_DatalinkClone(struct bundle *bundle, struct datalink *dl, 138836285Sbrian const char *name) 138936285Sbrian{ 139036285Sbrian if (bundle2datalink(bundle, name)) { 139136285Sbrian log_Printf(LogWARN, "Clone: %s: name already exists\n", name); 139236285Sbrian return 0; 139336285Sbrian } 139436285Sbrian 139536285Sbrian bundle_DatalinkLinkin(bundle, datalink_Clone(dl, name)); 139636285Sbrian return 1; 139736285Sbrian} 139836285Sbrian 139936285Sbrianvoid 140036285Sbrianbundle_DatalinkRemove(struct bundle *bundle, struct datalink *dl) 140136285Sbrian{ 140236285Sbrian dl = bundle_DatalinkLinkout(bundle, dl); 140336285Sbrian if (dl) 140436285Sbrian datalink_Destroy(dl); 140536285Sbrian} 140636285Sbrian 140736285Sbrianvoid 140836285Sbrianbundle_SetLabel(struct bundle *bundle, const char *label) 140936285Sbrian{ 141036285Sbrian if (label) 141136285Sbrian strncpy(bundle->cfg.label, label, sizeof bundle->cfg.label - 1); 141236285Sbrian else 141336285Sbrian *bundle->cfg.label = '\0'; 141436285Sbrian} 141536285Sbrian 141636285Sbrianconst char * 141736285Sbrianbundle_GetLabel(struct bundle *bundle) 141836285Sbrian{ 141936285Sbrian return *bundle->cfg.label ? bundle->cfg.label : NULL; 142036285Sbrian} 142136285Sbrian 142253684Sbrianint 142353684Sbrianbundle_LinkSize() 142453684Sbrian{ 142553684Sbrian struct iovec iov[SCATTER_SEGMENTS]; 142653684Sbrian int niov, expect, f; 142753684Sbrian 142853684Sbrian iov[0].iov_len = strlen(Version) + 1; 142953684Sbrian iov[0].iov_base = NULL; 143053684Sbrian niov = 1; 143153684Sbrian if (datalink2iov(NULL, iov, &niov, SCATTER_SEGMENTS, NULL, NULL) == -1) { 143253684Sbrian log_Printf(LogERROR, "Cannot determine space required for link\n"); 143353684Sbrian return 0; 143453684Sbrian } 143553684Sbrian 143653684Sbrian for (f = expect = 0; f < niov; f++) 143753684Sbrian expect += iov[f].iov_len; 143853684Sbrian 143953684Sbrian return expect; 144053684Sbrian} 144153684Sbrian 144236285Sbrianvoid 144353684Sbrianbundle_ReceiveDatalink(struct bundle *bundle, int s) 144436285Sbrian{ 144553684Sbrian char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int) * SEND_MAXFD]; 144653970Sbrian int niov, expect, f, *fd, nfd, onfd, got; 144753684Sbrian struct iovec iov[SCATTER_SEGMENTS]; 144852942Sbrian struct cmsghdr *cmsg; 144936285Sbrian struct msghdr msg; 145036285Sbrian struct datalink *dl; 145136450Sbrian pid_t pid; 145236285Sbrian 145336285Sbrian log_Printf(LogPHASE, "Receiving datalink\n"); 145436285Sbrian 145553684Sbrian /* 145653684Sbrian * Create our scatter/gather array - passing NULL gets the space 145753684Sbrian * allocation requirement rather than actually flattening the 145853684Sbrian * structures. 145953684Sbrian */ 146053684Sbrian iov[0].iov_len = strlen(Version) + 1; 146153684Sbrian iov[0].iov_base = NULL; 146236285Sbrian niov = 1; 146353684Sbrian if (datalink2iov(NULL, iov, &niov, SCATTER_SEGMENTS, NULL, NULL) == -1) { 146453684Sbrian log_Printf(LogERROR, "Cannot determine space required for link\n"); 146536285Sbrian return; 146636345Sbrian } 146736285Sbrian 146853684Sbrian /* Allocate the scatter/gather array for recvmsg() */ 146953684Sbrian for (f = expect = 0; f < niov; f++) { 147053684Sbrian if ((iov[f].iov_base = malloc(iov[f].iov_len)) == NULL) { 147153684Sbrian log_Printf(LogERROR, "Cannot allocate space to receive link\n"); 147253684Sbrian return; 147353684Sbrian } 147453970Sbrian if (f) 147553970Sbrian expect += iov[f].iov_len; 147653684Sbrian } 147736285Sbrian 147836285Sbrian /* Set up our message */ 147953684Sbrian cmsg = (struct cmsghdr *)cmsgbuf; 148053684Sbrian cmsg->cmsg_len = sizeof cmsgbuf; 148153684Sbrian cmsg->cmsg_level = SOL_SOCKET; 148253684Sbrian cmsg->cmsg_type = 0; 148336285Sbrian 148436285Sbrian memset(&msg, '\0', sizeof msg); 148553684Sbrian msg.msg_name = NULL; 148653684Sbrian msg.msg_namelen = 0; 148736285Sbrian msg.msg_iov = iov; 148853970Sbrian msg.msg_iovlen = 1; /* Only send the version at the first pass */ 148936285Sbrian msg.msg_control = cmsgbuf; 149036285Sbrian msg.msg_controllen = sizeof cmsgbuf; 149136285Sbrian 149258042Sbrian log_Printf(LogDEBUG, "Expecting %u scatter/gather bytes\n", 149358042Sbrian (unsigned)iov[0].iov_len); 149453684Sbrian 149553970Sbrian if ((got = recvmsg(s, &msg, MSG_WAITALL)) != iov[0].iov_len) { 149653970Sbrian if (got == -1) 149736285Sbrian log_Printf(LogERROR, "Failed recvmsg: %s\n", strerror(errno)); 149836285Sbrian else 149958042Sbrian log_Printf(LogERROR, "Failed recvmsg: Got %d, not %u\n", 150058042Sbrian got, (unsigned)iov[0].iov_len); 150136285Sbrian while (niov--) 150236285Sbrian free(iov[niov].iov_base); 150336285Sbrian return; 150436285Sbrian } 150536285Sbrian 150653684Sbrian if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) { 150753684Sbrian log_Printf(LogERROR, "Recvmsg: no descriptors received !\n"); 150853684Sbrian while (niov--) 150953684Sbrian free(iov[niov].iov_base); 151053684Sbrian return; 151152942Sbrian } 151252942Sbrian 151353684Sbrian fd = (int *)(cmsg + 1); 151453684Sbrian nfd = (cmsg->cmsg_len - sizeof *cmsg) / sizeof(int); 151553684Sbrian 151653684Sbrian if (nfd < 2) { 151758038Sbrian log_Printf(LogERROR, "Recvmsg: %d descriptor%s received (too few) !\n", 151853684Sbrian nfd, nfd == 1 ? "" : "s"); 151953684Sbrian while (nfd--) 152053684Sbrian close(fd[nfd]); 152137054Sbrian while (niov--) 152237054Sbrian free(iov[niov].iov_base); 152337054Sbrian return; 152436345Sbrian } 152536285Sbrian 152652942Sbrian /* 152753970Sbrian * We've successfully received two or more open file descriptors 152853970Sbrian * through our socket, plus a version string. Make sure it's the 152953970Sbrian * correct version, and drop the connection if it's not. 153052942Sbrian */ 153136285Sbrian if (strncmp(Version, iov[0].iov_base, iov[0].iov_len)) { 153236285Sbrian log_Printf(LogWARN, "Cannot receive datalink, incorrect version" 153336285Sbrian " (\"%.*s\", not \"%s\")\n", (int)iov[0].iov_len, 153437188Sbrian (char *)iov[0].iov_base, Version); 153553684Sbrian while (nfd--) 153653684Sbrian close(fd[nfd]); 153736285Sbrian while (niov--) 153836285Sbrian free(iov[niov].iov_base); 153936285Sbrian return; 154036285Sbrian } 154136285Sbrian 154253970Sbrian /* 154353970Sbrian * Everything looks good. Send the other side our process id so that 154453970Sbrian * they can transfer lock ownership, and wait for them to send the 154553970Sbrian * actual link data. 154653970Sbrian */ 154753970Sbrian pid = getpid(); 154853970Sbrian if ((got = write(fd[1], &pid, sizeof pid)) != sizeof pid) { 154953970Sbrian if (got == -1) 155053970Sbrian log_Printf(LogERROR, "Failed write: %s\n", strerror(errno)); 155153970Sbrian else 155253970Sbrian log_Printf(LogERROR, "Failed write: Got %d, not %d\n", got, 155353970Sbrian (int)(sizeof pid)); 155453970Sbrian while (nfd--) 155553970Sbrian close(fd[nfd]); 155653970Sbrian while (niov--) 155753970Sbrian free(iov[niov].iov_base); 155853970Sbrian return; 155953970Sbrian } 156053970Sbrian 156153970Sbrian if ((got = readv(fd[1], iov + 1, niov - 1)) != expect) { 156253970Sbrian if (got == -1) 156353970Sbrian log_Printf(LogERROR, "Failed write: %s\n", strerror(errno)); 156453970Sbrian else 156553970Sbrian log_Printf(LogERROR, "Failed write: Got %d, not %d\n", got, expect); 156653970Sbrian while (nfd--) 156753970Sbrian close(fd[nfd]); 156853970Sbrian while (niov--) 156953970Sbrian free(iov[niov].iov_base); 157053970Sbrian return; 157153970Sbrian } 157253970Sbrian close(fd[1]); 157353970Sbrian 157453684Sbrian onfd = nfd; /* We've got this many in our array */ 157558038Sbrian nfd -= 2; /* Don't include p->fd and our reply descriptor */ 157653684Sbrian niov = 1; /* Skip the version id */ 157752942Sbrian dl = iov2datalink(bundle, iov, &niov, sizeof iov / sizeof *iov, fd[0], 157853684Sbrian fd + 2, &nfd); 157936285Sbrian if (dl) { 158053684Sbrian 158152942Sbrian if (nfd) { 158252942Sbrian log_Printf(LogERROR, "bundle_ReceiveDatalink: Failed to handle %d " 158353684Sbrian "auxiliary file descriptors (%d remain)\n", onfd, nfd); 158452942Sbrian datalink_Destroy(dl); 158552942Sbrian while (nfd--) 158652942Sbrian close(fd[onfd--]); 158753684Sbrian close(fd[0]); 158852942Sbrian } else { 158952942Sbrian bundle_DatalinkLinkin(bundle, dl); 159052942Sbrian datalink_AuthOk(dl); 159152942Sbrian bundle_CalculateBandwidth(dl->bundle); 159252942Sbrian } 159352942Sbrian } else { 159452942Sbrian while (nfd--) 159552942Sbrian close(fd[onfd--]); 159652942Sbrian close(fd[0]); 159753684Sbrian close(fd[1]); 159852942Sbrian } 159936285Sbrian 160036285Sbrian free(iov[0].iov_base); 160136285Sbrian} 160236285Sbrian 160336285Sbrianvoid 160436285Sbrianbundle_SendDatalink(struct datalink *dl, int s, struct sockaddr_un *sun) 160536285Sbrian{ 160653684Sbrian char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int) * SEND_MAXFD]; 160753684Sbrian const char *constlock; 160853684Sbrian char *lock; 160952942Sbrian struct cmsghdr *cmsg; 161036285Sbrian struct msghdr msg; 161136285Sbrian struct iovec iov[SCATTER_SEGMENTS]; 161253684Sbrian int niov, f, expect, newsid, fd[SEND_MAXFD], nfd, reply[2], got; 161336450Sbrian pid_t newpid; 161436285Sbrian 161536285Sbrian log_Printf(LogPHASE, "Transmitting datalink %s\n", dl->name); 161636285Sbrian 161753684Sbrian /* Record the base device name for a lock transfer later */ 161853684Sbrian constlock = physical_LockedDevice(dl->physical); 161953684Sbrian if (constlock) { 162053684Sbrian lock = alloca(strlen(constlock) + 1); 162153684Sbrian strcpy(lock, constlock); 162253684Sbrian } else 162353684Sbrian lock = NULL; 162453684Sbrian 162536314Sbrian bundle_LinkClosed(dl->bundle, dl); 162636285Sbrian bundle_DatalinkLinkout(dl->bundle, dl); 162736285Sbrian 162836285Sbrian /* Build our scatter/gather array */ 162936285Sbrian iov[0].iov_len = strlen(Version) + 1; 163036285Sbrian iov[0].iov_base = strdup(Version); 163136285Sbrian niov = 1; 163252942Sbrian nfd = 0; 163336285Sbrian 163453684Sbrian fd[0] = datalink2iov(dl, iov, &niov, SCATTER_SEGMENTS, fd + 2, &nfd); 163536285Sbrian 163653684Sbrian if (fd[0] != -1 && socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, reply) != -1) { 163753684Sbrian /* 163853684Sbrian * fd[1] is used to get the peer process id back, then to confirm that 163953684Sbrian * we've transferred any device locks to that process id. 164053684Sbrian */ 164153684Sbrian fd[1] = reply[1]; 164252942Sbrian 164353684Sbrian nfd += 2; /* Include fd[0] and fd[1] */ 164436345Sbrian memset(&msg, '\0', sizeof msg); 164536285Sbrian 164653684Sbrian msg.msg_name = NULL; 164753684Sbrian msg.msg_namelen = 0; 164853970Sbrian /* 164953970Sbrian * Only send the version to start... We used to send the whole lot, but 165053970Sbrian * this caused problems with our RECVBUF size as a single link is about 165153970Sbrian * 22k ! This way, we should bump into no limits. 165253970Sbrian */ 165353970Sbrian msg.msg_iovlen = 1; 165436285Sbrian msg.msg_iov = iov; 165553684Sbrian msg.msg_control = cmsgbuf; 165653684Sbrian msg.msg_controllen = sizeof *cmsg + sizeof(int) * nfd; 165753684Sbrian msg.msg_flags = 0; 165836285Sbrian 165953684Sbrian cmsg = (struct cmsghdr *)cmsgbuf; 166053684Sbrian cmsg->cmsg_len = msg.msg_controllen; 166153684Sbrian cmsg->cmsg_level = SOL_SOCKET; 166253684Sbrian cmsg->cmsg_type = SCM_RIGHTS; 166352942Sbrian 166453684Sbrian for (f = 0; f < nfd; f++) 166553684Sbrian *((int *)(cmsg + 1) + f) = fd[f]; 166636345Sbrian 166753970Sbrian for (f = 1, expect = 0; f < niov; f++) 166836285Sbrian expect += iov[f].iov_len; 166936285Sbrian 167053970Sbrian if (setsockopt(reply[0], SOL_SOCKET, SO_SNDBUF, &expect, sizeof(int)) == -1) 167153970Sbrian log_Printf(LogERROR, "setsockopt(SO_RCVBUF, %d): %s\n", expect, 167253970Sbrian strerror(errno)); 167353970Sbrian if (setsockopt(reply[1], SOL_SOCKET, SO_RCVBUF, &expect, sizeof(int)) == -1) 167453970Sbrian log_Printf(LogERROR, "setsockopt(SO_RCVBUF, %d): %s\n", expect, 167553970Sbrian strerror(errno)); 167653970Sbrian 167758042Sbrian log_Printf(LogDEBUG, "Sending %d descriptor%s and %u bytes in scatter" 167858042Sbrian "/gather array\n", nfd, nfd == 1 ? "" : "s", 167958042Sbrian (unsigned)iov[0].iov_len); 168036285Sbrian 168153684Sbrian if ((got = sendmsg(s, &msg, 0)) == -1) 168253684Sbrian log_Printf(LogERROR, "Failed sendmsg: %s: %s\n", 168353684Sbrian sun->sun_path, strerror(errno)); 168453970Sbrian else if (got != iov[0].iov_len) 168558042Sbrian log_Printf(LogERROR, "%s: Failed initial sendmsg: Only sent %d of %u\n", 168658042Sbrian sun->sun_path, got, (unsigned)iov[0].iov_len); 168753684Sbrian else { 168858038Sbrian /* We must get the ACK before closing the descriptor ! */ 168953684Sbrian int res; 169036345Sbrian 169153970Sbrian if ((got = read(reply[0], &newpid, sizeof newpid)) == sizeof newpid) { 169253970Sbrian log_Printf(LogDEBUG, "Received confirmation from pid %d\n", 169353970Sbrian (int)newpid); 169453970Sbrian if (lock && (res = ID0uu_lock_txfr(lock, newpid)) != UU_LOCK_OK) 169559084Sbrian log_Printf(LogERROR, "uu_lock_txfr: %s\n", uu_lockerr(res)); 169653684Sbrian 169753970Sbrian log_Printf(LogDEBUG, "Transmitting link (%d bytes)\n", expect); 169853970Sbrian if ((got = writev(reply[0], iov + 1, niov - 1)) != expect) { 169953970Sbrian if (got == -1) 170053970Sbrian log_Printf(LogERROR, "%s: Failed writev: %s\n", 170153970Sbrian sun->sun_path, strerror(errno)); 170253970Sbrian else 170353970Sbrian log_Printf(LogERROR, "%s: Failed writev: Wrote %d of %d\n", 170453970Sbrian sun->sun_path, got, expect); 170553970Sbrian } 170653970Sbrian } else if (got == -1) 170753970Sbrian log_Printf(LogERROR, "%s: Failed socketpair read: %s\n", 170853970Sbrian sun->sun_path, strerror(errno)); 170953970Sbrian else 171053970Sbrian log_Printf(LogERROR, "%s: Failed socketpair read: Got %d of %d\n", 171153970Sbrian sun->sun_path, got, (int)(sizeof newpid)); 171253684Sbrian } 171353684Sbrian 171453684Sbrian close(reply[0]); 171553684Sbrian close(reply[1]); 171653684Sbrian 171747689Sbrian newsid = Enabled(dl->bundle, OPT_KEEPSESSION) || 171852942Sbrian tcgetpgrp(fd[0]) == getpgrp(); 171952942Sbrian while (nfd) 172052942Sbrian close(fd[--nfd]); 172136452Sbrian if (newsid) 172253684Sbrian bundle_setsid(dl->bundle, got != -1); 172336285Sbrian } 172436450Sbrian close(s); 172536285Sbrian 172636285Sbrian while (niov--) 172736285Sbrian free(iov[niov].iov_base); 172836285Sbrian} 172936285Sbrian 173036285Sbrianint 173136285Sbrianbundle_RenameDatalink(struct bundle *bundle, struct datalink *ndl, 173236285Sbrian const char *name) 173336285Sbrian{ 173436285Sbrian struct datalink *dl; 173536285Sbrian 173636285Sbrian if (!strcasecmp(ndl->name, name)) 173736285Sbrian return 1; 173836285Sbrian 173936285Sbrian for (dl = bundle->links; dl; dl = dl->next) 174036285Sbrian if (!strcasecmp(dl->name, name)) 174136285Sbrian return 0; 174236285Sbrian 174336285Sbrian datalink_Rename(ndl, name); 174436285Sbrian return 1; 174536285Sbrian} 174636285Sbrian 174736285Sbrianint 174836285Sbrianbundle_SetMode(struct bundle *bundle, struct datalink *dl, int mode) 174936285Sbrian{ 175036285Sbrian int omode; 175136285Sbrian 175236285Sbrian omode = dl->physical->type; 175336285Sbrian if (omode == mode) 175436285Sbrian return 1; 175536285Sbrian 175636928Sbrian if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO)) 175736928Sbrian /* First auto link */ 175836285Sbrian if (bundle->ncp.ipcp.peer_ip.s_addr == INADDR_ANY) { 175936928Sbrian log_Printf(LogWARN, "You must `set ifaddr' or `open' before" 176036928Sbrian " changing mode to %s\n", mode2Nam(mode)); 176136285Sbrian return 0; 176236285Sbrian } 176336285Sbrian 176436285Sbrian if (!datalink_SetMode(dl, mode)) 176536285Sbrian return 0; 176636285Sbrian 176736928Sbrian if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO) && 176836928Sbrian bundle->phase != PHASE_NETWORK) 176936928Sbrian /* First auto link, we need an interface */ 177036285Sbrian ipcp_InterfaceUp(&bundle->ncp.ipcp); 177136285Sbrian 177249434Sbrian /* Regenerate phys_type and adjust idle timer */ 177336285Sbrian bundle_LinksRemoved(bundle); 177436285Sbrian 177536285Sbrian return 1; 177636285Sbrian} 177736452Sbrian 177836452Sbrianvoid 177936452Sbrianbundle_setsid(struct bundle *bundle, int holdsession) 178036452Sbrian{ 178136452Sbrian /* 178236452Sbrian * Lose the current session. This means getting rid of our pid 178336452Sbrian * too so that the tty device will really go away, and any getty 178436452Sbrian * etc will be allowed to restart. 178536452Sbrian */ 178636452Sbrian pid_t pid, orig; 178736452Sbrian int fds[2]; 178836452Sbrian char done; 178936452Sbrian struct datalink *dl; 179036452Sbrian 179155066Sbrian if (!holdsession && bundle_IsDead(bundle)) { 179255066Sbrian /* 179355066Sbrian * No need to lose our session after all... we're going away anyway 179455066Sbrian * 179555066Sbrian * We should really stop the timer and pause if holdsession is set and 179655066Sbrian * the bundle's dead, but that leaves other resources lying about :-( 179755066Sbrian */ 179855066Sbrian return; 179955066Sbrian } 180055066Sbrian 180136452Sbrian orig = getpid(); 180236452Sbrian if (pipe(fds) == -1) { 180336452Sbrian log_Printf(LogERROR, "pipe: %s\n", strerror(errno)); 180436452Sbrian return; 180536452Sbrian } 180636452Sbrian switch ((pid = fork())) { 180736452Sbrian case -1: 180836452Sbrian log_Printf(LogERROR, "fork: %s\n", strerror(errno)); 180936452Sbrian close(fds[0]); 181036452Sbrian close(fds[1]); 181136452Sbrian return; 181236452Sbrian case 0: 181344541Sbrian close(fds[1]); 181444541Sbrian read(fds[0], &done, 1); /* uu_locks are mine ! */ 181536452Sbrian close(fds[0]); 181636452Sbrian if (pipe(fds) == -1) { 181736452Sbrian log_Printf(LogERROR, "pipe(2): %s\n", strerror(errno)); 181836452Sbrian return; 181936452Sbrian } 182036452Sbrian switch ((pid = fork())) { 182136452Sbrian case -1: 182237019Sbrian log_Printf(LogERROR, "fork(2): %s\n", strerror(errno)); 182336452Sbrian close(fds[0]); 182436452Sbrian close(fds[1]); 182536452Sbrian return; 182636452Sbrian case 0: 182744541Sbrian close(fds[1]); 182844541Sbrian bundle_LockTun(bundle); /* update pid */ 182944541Sbrian read(fds[0], &done, 1); /* uu_locks are mine ! */ 183036452Sbrian close(fds[0]); 183136452Sbrian setsid(); 183256350Sbrian bundle_ChangedPID(bundle); 183359084Sbrian log_Printf(LogDEBUG, "%d -> %d: %s session control\n", 183436452Sbrian (int)orig, (int)getpid(), 183536452Sbrian holdsession ? "Passed" : "Dropped"); 183641799Sbrian timer_InitService(0); /* Start the Timer Service */ 183736452Sbrian break; 183836452Sbrian default: 183944541Sbrian close(fds[0]); 184046686Sbrian /* Give away all our physical locks (to the final process) */ 184136452Sbrian for (dl = bundle->links; dl; dl = dl->next) 184236452Sbrian if (dl->state != DATALINK_CLOSED) 184346686Sbrian physical_ChangedPid(dl->physical, pid); 184444541Sbrian write(fds[1], "!", 1); /* done */ 184544541Sbrian close(fds[1]); 184653684Sbrian _exit(0); 184736452Sbrian break; 184836452Sbrian } 184936452Sbrian break; 185036452Sbrian default: 185144541Sbrian close(fds[0]); 185246686Sbrian /* Give away all our physical locks (to the intermediate process) */ 185336452Sbrian for (dl = bundle->links; dl; dl = dl->next) 185436452Sbrian if (dl->state != DATALINK_CLOSED) 185546686Sbrian physical_ChangedPid(dl->physical, pid); 185644541Sbrian write(fds[1], "!", 1); /* done */ 185744541Sbrian close(fds[1]); 185836452Sbrian if (holdsession) { 185936452Sbrian int fd, status; 186036452Sbrian 186136452Sbrian timer_TermService(); 186236452Sbrian signal(SIGPIPE, SIG_DFL); 186336452Sbrian signal(SIGALRM, SIG_DFL); 186436452Sbrian signal(SIGHUP, SIG_DFL); 186536452Sbrian signal(SIGTERM, SIG_DFL); 186636452Sbrian signal(SIGINT, SIG_DFL); 186736452Sbrian signal(SIGQUIT, SIG_DFL); 186836452Sbrian for (fd = getdtablesize(); fd >= 0; fd--) 186936452Sbrian close(fd); 187036452Sbrian /* 187136452Sbrian * Reap the intermediate process. As we're not exiting but the 187236452Sbrian * intermediate is, we don't want it to become defunct. 187336452Sbrian */ 187436452Sbrian waitpid(pid, &status, 0); 187536467Sbrian /* Tweak our process arguments.... */ 187664698Sbrian SetTitle("session owner"); 187764802Sbrian#ifndef NOSUID 187855252Sbrian setuid(ID0realuid()); 187964802Sbrian#endif 188036452Sbrian /* 188136452Sbrian * Hang around for a HUP. This should happen as soon as the 188258038Sbrian * ppp that we passed our ctty descriptor to closes it. 188358038Sbrian * NOTE: If this process dies, the passed descriptor becomes 188436452Sbrian * invalid and will give a select() error by setting one 188536452Sbrian * of the error fds, aborting the other ppp. We don't 188636452Sbrian * want that to happen ! 188736452Sbrian */ 188836452Sbrian pause(); 188936452Sbrian } 189053684Sbrian _exit(0); 189136452Sbrian break; 189236452Sbrian } 189336452Sbrian} 189440622Sbrian 189540622Sbrianint 189640622Sbrianbundle_HighestState(struct bundle *bundle) 189740622Sbrian{ 189840622Sbrian struct datalink *dl; 189940622Sbrian int result = DATALINK_CLOSED; 190040622Sbrian 190140622Sbrian for (dl = bundle->links; dl; dl = dl->next) 190240622Sbrian if (result < dl->state) 190340622Sbrian result = dl->state; 190440622Sbrian 190540622Sbrian return result; 190640622Sbrian} 190741654Sbrian 190841654Sbrianint 190941654Sbrianbundle_Exception(struct bundle *bundle, int fd) 191041654Sbrian{ 191141654Sbrian struct datalink *dl; 191241654Sbrian 191341654Sbrian for (dl = bundle->links; dl; dl = dl->next) 191441654Sbrian if (dl->physical->fd == fd) { 191541654Sbrian datalink_Down(dl, CLOSE_NORMAL); 191641654Sbrian return 1; 191741654Sbrian } 191841654Sbrian 191941654Sbrian return 0; 192041654Sbrian} 192147648Sbrian 192247648Sbrianvoid 192347648Sbrianbundle_AdjustFilters(struct bundle *bundle, struct in_addr *my_ip, 192447648Sbrian struct in_addr *peer_ip) 192547648Sbrian{ 192658044Sbrian filter_AdjustAddr(&bundle->filter.in, my_ip, peer_ip, NULL); 192758044Sbrian filter_AdjustAddr(&bundle->filter.out, my_ip, peer_ip, NULL); 192858044Sbrian filter_AdjustAddr(&bundle->filter.dial, my_ip, peer_ip, NULL); 192958044Sbrian filter_AdjustAddr(&bundle->filter.alive, my_ip, peer_ip, NULL); 193047648Sbrian} 193149434Sbrian 193249434Sbrianvoid 193358044Sbrianbundle_AdjustDNS(struct bundle *bundle, struct in_addr dns[2]) 193458044Sbrian{ 193558044Sbrian filter_AdjustAddr(&bundle->filter.in, NULL, NULL, dns); 193658044Sbrian filter_AdjustAddr(&bundle->filter.out, NULL, NULL, dns); 193758044Sbrian filter_AdjustAddr(&bundle->filter.dial, NULL, NULL, dns); 193858044Sbrian filter_AdjustAddr(&bundle->filter.alive, NULL, NULL, dns); 193958044Sbrian} 194058044Sbrian 194158044Sbrianvoid 194249434Sbrianbundle_CalculateBandwidth(struct bundle *bundle) 194349434Sbrian{ 194449434Sbrian struct datalink *dl; 194569303Sbrian int sp; 194649434Sbrian 194749434Sbrian bundle->bandwidth = 0; 194869303Sbrian bundle->mtu = 0; 194949434Sbrian for (dl = bundle->links; dl; dl = dl->next) 195049434Sbrian if (dl->state == DATALINK_OPEN) { 195149434Sbrian if ((sp = dl->mp.bandwidth) == 0 && 195249434Sbrian (sp = physical_GetSpeed(dl->physical)) == 0) 195349434Sbrian log_Printf(LogDEBUG, "%s: %s: Cannot determine bandwidth\n", 195449434Sbrian dl->name, dl->physical->name.full); 195549434Sbrian else 195649434Sbrian bundle->bandwidth += sp; 195749434Sbrian if (!bundle->ncp.mp.active) { 195869303Sbrian bundle->mtu = dl->physical->link.lcp.his_mru; 195949434Sbrian break; 196049434Sbrian } 196149434Sbrian } 196249434Sbrian 196349434Sbrian if(bundle->bandwidth == 0) 196449434Sbrian bundle->bandwidth = 115200; /* Shrug */ 196549434Sbrian 196649434Sbrian if (bundle->ncp.mp.active) 196769303Sbrian bundle->mtu = bundle->ncp.mp.peer_mrru; 196869303Sbrian else if (!bundle->mtu) 196969303Sbrian bundle->mtu = 1500; 197049434Sbrian 197149434Sbrian#ifndef NORADIUS 197269303Sbrian if (bundle->radius.valid && bundle->radius.mtu && 197369303Sbrian bundle->radius.mtu < bundle->mtu) { 197449434Sbrian log_Printf(LogLCP, "Reducing MTU to radius value %lu\n", 197549434Sbrian bundle->radius.mtu); 197669303Sbrian bundle->mtu = bundle->radius.mtu; 197749434Sbrian } 197849434Sbrian#endif 197949434Sbrian 198069303Sbrian tun_configure(bundle); 198149434Sbrian} 198249434Sbrian 198349434Sbrianvoid 198449434Sbrianbundle_AutoAdjust(struct bundle *bundle, int percent, int what) 198549434Sbrian{ 198649434Sbrian struct datalink *dl, *choice, *otherlinkup; 198749434Sbrian 198849434Sbrian choice = otherlinkup = NULL; 198949434Sbrian for (dl = bundle->links; dl; dl = dl->next) 199049434Sbrian if (dl->physical->type == PHYS_AUTO) { 199149434Sbrian if (dl->state == DATALINK_OPEN) { 199249434Sbrian if (what == AUTO_DOWN) { 199349434Sbrian if (choice) 199449434Sbrian otherlinkup = choice; 199549434Sbrian choice = dl; 199649434Sbrian } 199749434Sbrian } else if (dl->state == DATALINK_CLOSED) { 199849434Sbrian if (what == AUTO_UP) { 199949434Sbrian choice = dl; 200049434Sbrian break; 200149434Sbrian } 200249434Sbrian } else { 200349434Sbrian /* An auto link in an intermediate state - forget it for the moment */ 200449434Sbrian choice = NULL; 200549434Sbrian break; 200649434Sbrian } 200749434Sbrian } else if (dl->state == DATALINK_OPEN && what == AUTO_DOWN) 200849434Sbrian otherlinkup = dl; 200949434Sbrian 201049434Sbrian if (choice) { 201149434Sbrian if (what == AUTO_UP) { 201249434Sbrian log_Printf(LogPHASE, "%d%% saturation -> Opening link ``%s''\n", 201349434Sbrian percent, choice->name); 201449434Sbrian datalink_Up(choice, 1, 1); 201561129Sbrian mp_CheckAutoloadTimer(&bundle->ncp.mp); 201649434Sbrian } else if (otherlinkup) { /* Only bring the second-last link down */ 201749434Sbrian log_Printf(LogPHASE, "%d%% saturation -> Closing link ``%s''\n", 201849434Sbrian percent, choice->name); 201951945Sbrian datalink_Close(choice, CLOSE_STAYDOWN); 202061129Sbrian mp_CheckAutoloadTimer(&bundle->ncp.mp); 202149434Sbrian } 202249434Sbrian } 202349434Sbrian} 202449434Sbrian 202549434Sbrianint 202649434Sbrianbundle_WantAutoloadTimer(struct bundle *bundle) 202749434Sbrian{ 202849434Sbrian struct datalink *dl; 202949434Sbrian int autolink, opened; 203049434Sbrian 203149434Sbrian if (bundle->phase == PHASE_NETWORK) { 203249434Sbrian for (autolink = opened = 0, dl = bundle->links; dl; dl = dl->next) 203349434Sbrian if (dl->physical->type == PHYS_AUTO) { 203449434Sbrian if (++autolink == 2 || (autolink == 1 && opened)) 203549434Sbrian /* Two auto links or one auto and one open in NETWORK phase */ 203649434Sbrian return 1; 203749434Sbrian } else if (dl->state == DATALINK_OPEN) { 203849434Sbrian opened++; 203949434Sbrian if (autolink) 204049434Sbrian /* One auto and one open link in NETWORK phase */ 204149434Sbrian return 1; 204249434Sbrian } 204349434Sbrian } 204449434Sbrian 204549434Sbrian return 0; 204649434Sbrian} 204756350Sbrian 204856350Sbrianvoid 204956350Sbrianbundle_ChangedPID(struct bundle *bundle) 205056350Sbrian{ 205156350Sbrian#ifdef TUNSIFPID 205256350Sbrian ioctl(bundle->dev.fd, TUNSIFPID, 0); 205356350Sbrian#endif 205456350Sbrian} 2055