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) 5468602c3eSBrian Somers #ifdef NOSUID 5568602c3eSBrian Somers #include <sys/linker.h> 5668602c3eSBrian Somers #endif 57fdb4bb1bSBrian Somers #include <sys/module.h> 58fc3034caSBrian Somers #endif 597a6f8720SBrian Somers #include <termios.h> 607a6f8720SBrian Somers #include <unistd.h> 617a6f8720SBrian Somers 625d9e6103SBrian Somers #include "layer.h" 63c9e11a11SBrian Somers #include "defs.h" 647a6f8720SBrian Somers #include "command.h" 657a6f8720SBrian Somers #include "mbuf.h" 667a6f8720SBrian Somers #include "log.h" 677a6f8720SBrian Somers #include "id.h" 687a6f8720SBrian Somers #include "timer.h" 697a6f8720SBrian Somers #include "fsm.h" 707a6f8720SBrian Somers #include "iplist.h" 71879ed6faSBrian Somers #include "lqr.h" 72455aabc3SBrian Somers #include "hdlc.h" 737a6f8720SBrian Somers #include "throughput.h" 74eaa4df37SBrian Somers #include "slcompress.h" 757a6f8720SBrian Somers #include "ipcp.h" 765ca5389aSBrian Somers #include "filter.h" 772f786681SBrian Somers #include "descriptor.h" 787a6f8720SBrian Somers #include "route.h" 797a6f8720SBrian Somers #include "lcp.h" 807a6f8720SBrian Somers #include "ccp.h" 813b0f8d2eSBrian Somers #include "link.h" 823b0f8d2eSBrian Somers #include "mp.h" 83972a1bcfSBrian Somers #ifndef NORADIUS 84972a1bcfSBrian Somers #include "radius.h" 85972a1bcfSBrian Somers #endif 863b0f8d2eSBrian Somers #include "bundle.h" 87455aabc3SBrian Somers #include "async.h" 88455aabc3SBrian Somers #include "physical.h" 89455aabc3SBrian Somers #include "auth.h" 905d9e6103SBrian Somers #include "proto.h" 91455aabc3SBrian Somers #include "chap.h" 92455aabc3SBrian Somers #include "tun.h" 9385b542cfSBrian Somers #include "prompt.h" 943006ec67SBrian Somers #include "chat.h" 9592b09558SBrian Somers #include "cbcp.h" 963006ec67SBrian Somers #include "datalink.h" 973006ec67SBrian Somers #include "ip.h" 988fa6ebe4SBrian Somers #include "iface.h" 997a6f8720SBrian Somers 10091cbd2eeSBrian Somers #define SCATTER_SEGMENTS 7 /* version, datalink, name, physical, 10191cbd2eeSBrian Somers throughput, throughput, device */ 10287c3786eSBrian Somers 1032cb305afSBrian Somers #define SEND_MAXFD 3 /* Max file descriptors passed through 10487c3786eSBrian Somers the local domain socket */ 10596c9bb21SBrian Somers 10604eaa58cSBrian Somers static int bundle_RemainingIdleTime(struct bundle *); 10704eaa58cSBrian Somers 108182c898aSBrian Somers static const char * const PhaseNames[] = { 109455aabc3SBrian Somers "Dead", "Establish", "Authenticate", "Network", "Terminate" 110455aabc3SBrian Somers }; 111455aabc3SBrian Somers 112455aabc3SBrian Somers const char * 113455aabc3SBrian Somers bundle_PhaseName(struct bundle *bundle) 1147a6f8720SBrian Somers { 115455aabc3SBrian Somers return bundle->phase <= PHASE_TERMINATE ? 116455aabc3SBrian Somers PhaseNames[bundle->phase] : "unknown"; 1177a6f8720SBrian Somers } 1187a6f8720SBrian Somers 119455aabc3SBrian Somers void 1205563ebdeSBrian Somers bundle_NewPhase(struct bundle *bundle, u_int new) 121455aabc3SBrian Somers { 122aef795ccSBrian Somers if (new == bundle->phase) 123aef795ccSBrian Somers return; 124aef795ccSBrian Somers 125e2ebb036SBrian Somers if (new <= PHASE_TERMINATE) 126dd7e2610SBrian Somers log_Printf(LogPHASE, "bundle: %s\n", PhaseNames[new]); 1277a6f8720SBrian Somers 128455aabc3SBrian Somers switch (new) { 129455aabc3SBrian Somers case PHASE_DEAD: 1300f2f3eb3SBrian Somers log_DisplayPrompts(); 131455aabc3SBrian Somers bundle->phase = new; 132455aabc3SBrian Somers break; 133455aabc3SBrian Somers 134455aabc3SBrian Somers case PHASE_ESTABLISH: 135455aabc3SBrian Somers bundle->phase = new; 136455aabc3SBrian Somers break; 137455aabc3SBrian Somers 138455aabc3SBrian Somers case PHASE_AUTHENTICATE: 139455aabc3SBrian Somers bundle->phase = new; 1400f2f3eb3SBrian Somers log_DisplayPrompts(); 141455aabc3SBrian Somers break; 142455aabc3SBrian Somers 143455aabc3SBrian Somers case PHASE_NETWORK: 144dd7e2610SBrian Somers fsm_Up(&bundle->ncp.ipcp.fsm); 145dd7e2610SBrian Somers fsm_Open(&bundle->ncp.ipcp.fsm); 146673903ecSBrian Somers bundle->phase = new; 1470f2f3eb3SBrian Somers log_DisplayPrompts(); 148673903ecSBrian Somers break; 149455aabc3SBrian Somers 150455aabc3SBrian Somers case PHASE_TERMINATE: 151455aabc3SBrian Somers bundle->phase = new; 152673903ecSBrian Somers mp_Down(&bundle->ncp.mp); 1530f2f3eb3SBrian Somers log_DisplayPrompts(); 154455aabc3SBrian Somers break; 1557a6f8720SBrian Somers } 1567a6f8720SBrian Somers } 1577a6f8720SBrian Somers 1586d666775SBrian Somers static void 1596d666775SBrian Somers bundle_LayerStart(void *v, struct fsm *fp) 1607a6f8720SBrian Somers { 1613006ec67SBrian Somers /* The given FSM is about to start up ! */ 1627a6f8720SBrian Somers } 1637a6f8720SBrian Somers 1645cf4388bSBrian Somers 165b42135deSBrian Somers void 1665cf4388bSBrian Somers bundle_Notify(struct bundle *bundle, char c) 1675cf4388bSBrian Somers { 1685cf4388bSBrian Somers if (bundle->notify.fd != -1) { 169b42135deSBrian Somers int ret; 170b42135deSBrian Somers 171b42135deSBrian Somers ret = write(bundle->notify.fd, &c, 1); 172b42135deSBrian Somers if (c != EX_REDIAL && c != EX_RECONNECT) { 173b42135deSBrian Somers if (ret == 1) 174b42135deSBrian Somers log_Printf(LogCHAT, "Parent notified of %s\n", 1755a83ad1eSBrian Somers c == EX_NORMAL ? "success" : "failure"); 1765cf4388bSBrian Somers else 177b42135deSBrian Somers log_Printf(LogERROR, "Failed to notify parent of success\n"); 1785cf4388bSBrian Somers close(bundle->notify.fd); 1795cf4388bSBrian Somers bundle->notify.fd = -1; 180b42135deSBrian Somers } else if (ret == 1) 181b42135deSBrian Somers log_Printf(LogCHAT, "Parent notified of %s\n", ex_desc(c)); 182b42135deSBrian Somers else 183b42135deSBrian Somers log_Printf(LogERROR, "Failed to notify parent of %s\n", ex_desc(c)); 1845cf4388bSBrian Somers } 1855cf4388bSBrian Somers } 1863b0f8d2eSBrian Somers 1876d666775SBrian Somers static void 1886f8e9f0aSBrian Somers bundle_ClearQueues(void *v) 1896f8e9f0aSBrian Somers { 1906f8e9f0aSBrian Somers struct bundle *bundle = (struct bundle *)v; 1916f8e9f0aSBrian Somers struct datalink *dl; 1926f8e9f0aSBrian Somers 1936f8e9f0aSBrian Somers log_Printf(LogPHASE, "Clearing choked output queue\n"); 1946f8e9f0aSBrian Somers timer_Stop(&bundle->choked.timer); 1956f8e9f0aSBrian Somers 1966f8e9f0aSBrian Somers /* 1976f8e9f0aSBrian Somers * Emergency time: 1986f8e9f0aSBrian Somers * 1996f8e9f0aSBrian Somers * We've had a full queue for PACKET_DEL_SECS seconds without being 2006f8e9f0aSBrian Somers * able to get rid of any of the packets. We've probably given up 2016f8e9f0aSBrian Somers * on the redials at this point, and the queued data has almost 2026f8e9f0aSBrian Somers * definitely been timed out by the layer above. As this is preventing 2036f8e9f0aSBrian Somers * us from reading the TUN_NAME device (we don't want to buffer stuff 2046f8e9f0aSBrian Somers * indefinitely), we may as well nuke this data and start with a clean 2056f8e9f0aSBrian Somers * slate ! 2066f8e9f0aSBrian Somers * 2076f8e9f0aSBrian Somers * Unfortunately, this has the side effect of shafting any compression 2086f8e9f0aSBrian Somers * dictionaries in use (causing the relevant RESET_REQ/RESET_ACK). 2096f8e9f0aSBrian Somers */ 2106f8e9f0aSBrian Somers 2115a72b6edSBrian Somers ip_DeleteQueue(&bundle->ncp.ipcp); 2126f8e9f0aSBrian Somers mp_DeleteQueue(&bundle->ncp.mp); 2136f8e9f0aSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 2146f8e9f0aSBrian Somers physical_DeleteQueue(dl->physical); 2156f8e9f0aSBrian Somers } 2166f8e9f0aSBrian Somers 2176f8e9f0aSBrian Somers static void 218ff0f9439SBrian Somers bundle_LinkAdded(struct bundle *bundle, struct datalink *dl) 219ff0f9439SBrian Somers { 220ff0f9439SBrian Somers bundle->phys_type.all |= dl->physical->type; 221ff0f9439SBrian Somers if (dl->state == DATALINK_OPEN) 222ff0f9439SBrian Somers bundle->phys_type.open |= dl->physical->type; 223ff0f9439SBrian Somers 224ff0f9439SBrian Somers if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) 225ff0f9439SBrian Somers != bundle->phys_type.open && bundle->idle.timer.state == TIMER_STOPPED) 226ff0f9439SBrian Somers /* We may need to start our idle timer */ 2270a4b6c5cSBrian Somers bundle_StartIdleTimer(bundle, 0); 228ff0f9439SBrian Somers } 229ff0f9439SBrian Somers 23092b09558SBrian Somers void 231ff0f9439SBrian Somers bundle_LinksRemoved(struct bundle *bundle) 232ff0f9439SBrian Somers { 233ff0f9439SBrian Somers struct datalink *dl; 234ff0f9439SBrian Somers 235ff0f9439SBrian Somers bundle->phys_type.all = bundle->phys_type.open = 0; 236ff0f9439SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 237ff0f9439SBrian Somers bundle_LinkAdded(bundle, dl); 238ff0f9439SBrian Somers 239ab2de065SBrian Somers bundle_CalculateBandwidth(bundle); 240ab2de065SBrian Somers mp_CheckAutoloadTimer(&bundle->ncp.mp); 241ab2de065SBrian Somers 242ff0f9439SBrian Somers if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) 243ff0f9439SBrian Somers == bundle->phys_type.open) 244ff0f9439SBrian Somers bundle_StopIdleTimer(bundle); 245ff0f9439SBrian Somers } 24604eaa58cSBrian Somers 24704eaa58cSBrian Somers static void 2486f384573SBrian Somers bundle_LayerUp(void *v, struct fsm *fp) 2497a6f8720SBrian Somers { 2503006ec67SBrian Somers /* 2513006ec67SBrian Somers * The given fsm is now up 252ab2de065SBrian Somers * If it's an LCP, adjust our phys_mode.open value and check the 253ab2de065SBrian Somers * autoload timer. 254ab2de065SBrian Somers * If it's the first NCP, calculate our bandwidth 255dade2407SBrian Somers * If it's the first NCP, set our ``upat'' time 2563b0f8d2eSBrian Somers * If it's the first NCP, start the idle timer. 257ab2de065SBrian Somers * If it's an NCP, tell our -background parent to go away. 258ab2de065SBrian Somers * If it's the first NCP, start the autoload timer 2593006ec67SBrian Somers */ 2606f384573SBrian Somers struct bundle *bundle = (struct bundle *)v; 2616d666775SBrian Somers 2625563ebdeSBrian Somers if (fp->proto == PROTO_LCP) { 263ff0f9439SBrian Somers struct physical *p = link2physical(fp->link); 264ff0f9439SBrian Somers 265ff0f9439SBrian Somers bundle_LinkAdded(bundle, p->dl); 266ab2de065SBrian Somers mp_CheckAutoloadTimer(&bundle->ncp.mp); 2673b0f8d2eSBrian Somers } else if (fp->proto == PROTO_IPCP) { 268ab2de065SBrian Somers bundle_CalculateBandwidth(fp->bundle); 269dade2407SBrian Somers time(&bundle->upat); 2700a4b6c5cSBrian Somers bundle_StartIdleTimer(bundle, 0); 2715cf4388bSBrian Somers bundle_Notify(bundle, EX_NORMAL); 272ab2de065SBrian Somers mp_CheckAutoloadTimer(&fp->bundle->ncp.mp); 2737a6f8720SBrian Somers } 274ab886ad0SBrian Somers } 2757a6f8720SBrian Somers 2766d666775SBrian Somers static void 2776d666775SBrian Somers bundle_LayerDown(void *v, struct fsm *fp) 2786d666775SBrian Somers { 2796d666775SBrian Somers /* 2806d666775SBrian Somers * The given FSM has been told to come down. 281ab886ad0SBrian Somers * If it's our last NCP, stop the idle timer. 282dade2407SBrian Somers * If it's our last NCP, clear our ``upat'' value. 283ab2de065SBrian Somers * If it's our last NCP, stop the autoload timer 284ff0f9439SBrian Somers * If it's an LCP, adjust our phys_type.open value and any timers. 28586b1f0d7SBrian Somers * If it's an LCP and we're in multilink mode, adjust our tun 286c8f30703SBrian Somers * If it's the last LCP, down all NCPs 28786b1f0d7SBrian Somers * speed and make sure our minimum sequence number is adjusted. 2886d666775SBrian Somers */ 289ab886ad0SBrian Somers 290ab886ad0SBrian Somers struct bundle *bundle = (struct bundle *)v; 291ab886ad0SBrian Somers 292ab2de065SBrian Somers if (fp->proto == PROTO_IPCP) { 293ab886ad0SBrian Somers bundle_StopIdleTimer(bundle); 294dade2407SBrian Somers bundle->upat = 0; 295ab2de065SBrian Somers mp_StopAutoloadTimer(&bundle->ncp.mp); 296ab2de065SBrian Somers } else if (fp->proto == PROTO_LCP) { 2973b0f8d2eSBrian Somers struct datalink *dl; 29886b1f0d7SBrian Somers struct datalink *lost; 299c8f30703SBrian Somers int others_active; 300c8f30703SBrian Somers 301c8f30703SBrian Somers bundle_LinksRemoved(bundle); /* adjust timers & phys_type values */ 3023b0f8d2eSBrian Somers 30386b1f0d7SBrian Somers lost = NULL; 304c8f30703SBrian Somers others_active = 0; 305c8f30703SBrian Somers for (dl = bundle->links; dl; dl = dl->next) { 30686b1f0d7SBrian Somers if (fp == &dl->physical->link.lcp.fsm) 30786b1f0d7SBrian Somers lost = dl; 308c8f30703SBrian Somers else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) 309c8f30703SBrian Somers others_active++; 310c8f30703SBrian Somers } 31186b1f0d7SBrian Somers 312c8f30703SBrian Somers if (bundle->ncp.mp.active) { 313ab2de065SBrian Somers bundle_CalculateBandwidth(bundle); 31486b1f0d7SBrian Somers 31586b1f0d7SBrian Somers if (lost) 31686b1f0d7SBrian Somers mp_LinkLost(&bundle->ncp.mp, lost); 31786b1f0d7SBrian Somers else 318a33b2ef7SBrian Somers log_Printf(LogALERT, "Oops, lost an unrecognised datalink (%s) !\n", 31986b1f0d7SBrian Somers fp->link->name); 3203b0f8d2eSBrian Somers } 321c8f30703SBrian Somers 322c8f30703SBrian Somers if (!others_active) 323c8f30703SBrian Somers /* Down the NCPs. We don't expect to get fsm_Close()d ourself ! */ 324c8f30703SBrian Somers fsm2initial(&bundle->ncp.ipcp.fsm); 3256d666775SBrian Somers } 326ff0f9439SBrian Somers } 3276d666775SBrian Somers 3286d666775SBrian Somers static void 3296d666775SBrian Somers bundle_LayerFinish(void *v, struct fsm *fp) 3306d666775SBrian Somers { 3316d666775SBrian Somers /* The given fsm is now down (fp cannot be NULL) 3326d666775SBrian Somers * 333dd7e2610SBrian Somers * If it's the last NCP, fsm_Close all LCPs 3346d666775SBrian Somers */ 3356d666775SBrian Somers 3366d666775SBrian Somers struct bundle *bundle = (struct bundle *)v; 3376d666775SBrian Somers struct datalink *dl; 3386d666775SBrian Somers 3393b0f8d2eSBrian Somers if (fp->proto == PROTO_IPCP) { 34026afeaa2SBrian Somers if (bundle_Phase(bundle) != PHASE_DEAD) 34125092092SBrian Somers bundle_NewPhase(bundle, PHASE_TERMINATE); 3426d666775SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 343c8f30703SBrian Somers if (dl->state == DATALINK_OPEN) 344d4d5d2f8SBrian Somers datalink_Close(dl, CLOSE_STAYDOWN); 34509206a6fSBrian Somers fsm2initial(fp); 3463b0f8d2eSBrian Somers } 3473b0f8d2eSBrian Somers } 3486d666775SBrian Somers 3497a6f8720SBrian Somers int 3507a6f8720SBrian Somers bundle_LinkIsUp(const struct bundle *bundle) 3517a6f8720SBrian Somers { 3525828db6dSBrian Somers return bundle->ncp.ipcp.fsm.state == ST_OPENED; 3537a6f8720SBrian Somers } 3547a6f8720SBrian Somers 3557a6f8720SBrian Somers void 3569c81b87dSBrian Somers bundle_Close(struct bundle *bundle, const char *name, int how) 3577a6f8720SBrian Somers { 358455aabc3SBrian Somers /* 3593006ec67SBrian Somers * Please close the given datalink. 360dd7e2610SBrian Somers * If name == NULL or name is the last datalink, fsm_Close all NCPs 3616f384573SBrian Somers * (except our MP) 3623b0f8d2eSBrian Somers * If it isn't the last datalink, just Close that datalink. 363455aabc3SBrian Somers */ 3647a6f8720SBrian Somers 3653b0f8d2eSBrian Somers struct datalink *dl, *this_dl; 3663b0f8d2eSBrian Somers int others_active; 3673006ec67SBrian Somers 3683b0f8d2eSBrian Somers others_active = 0; 3693b0f8d2eSBrian Somers this_dl = NULL; 3703b0f8d2eSBrian Somers 3713b0f8d2eSBrian Somers for (dl = bundle->links; dl; dl = dl->next) { 3723b0f8d2eSBrian Somers if (name && !strcasecmp(name, dl->name)) 3733b0f8d2eSBrian Somers this_dl = dl; 3743b0f8d2eSBrian Somers if (name == NULL || this_dl == dl) { 3759c81b87dSBrian Somers switch (how) { 3769c81b87dSBrian Somers case CLOSE_LCP: 3779c81b87dSBrian Somers datalink_DontHangup(dl); 3789c81b87dSBrian Somers /* fall through */ 3799c81b87dSBrian Somers case CLOSE_STAYDOWN: 3803006ec67SBrian Somers datalink_StayDown(dl); 3819c81b87dSBrian Somers break; 3829c81b87dSBrian Somers } 3833b0f8d2eSBrian Somers } else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP) 3843b0f8d2eSBrian Somers others_active++; 3853b0f8d2eSBrian Somers } 3863b0f8d2eSBrian Somers 3873b0f8d2eSBrian Somers if (name && this_dl == NULL) { 388dd7e2610SBrian Somers log_Printf(LogWARN, "%s: Invalid datalink name\n", name); 3893b0f8d2eSBrian Somers return; 3903b0f8d2eSBrian Somers } 3913b0f8d2eSBrian Somers 3923b0f8d2eSBrian Somers if (!others_active) { 39304eaa58cSBrian Somers bundle_StopIdleTimer(bundle); 3943b0f8d2eSBrian Somers if (bundle->ncp.ipcp.fsm.state > ST_CLOSED || 3953b0f8d2eSBrian Somers bundle->ncp.ipcp.fsm.state == ST_STARTING) 396dd7e2610SBrian Somers fsm_Close(&bundle->ncp.ipcp.fsm); 3973b0f8d2eSBrian Somers else { 39809206a6fSBrian Somers fsm2initial(&bundle->ncp.ipcp.fsm); 399d345321bSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 4009c81b87dSBrian Somers datalink_Close(dl, how); 4017a6f8720SBrian Somers } 4023b0f8d2eSBrian Somers } else if (this_dl && this_dl->state != DATALINK_CLOSED && 4033b0f8d2eSBrian Somers this_dl->state != DATALINK_HANGUP) 4049c81b87dSBrian Somers datalink_Close(this_dl, how); 405d2fd8d77SBrian Somers } 4067a6f8720SBrian Somers 4071bc9b5baSBrian Somers void 408899011c4SBrian Somers bundle_Down(struct bundle *bundle, int how) 4091bc9b5baSBrian Somers { 4101bc9b5baSBrian Somers struct datalink *dl; 4111bc9b5baSBrian Somers 4121bc9b5baSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 413899011c4SBrian Somers datalink_Down(dl, how); 4141bc9b5baSBrian Somers } 4151bc9b5baSBrian Somers 41626af0ae9SBrian Somers static size_t 41726af0ae9SBrian Somers bundle_FillQueues(struct bundle *bundle) 41826af0ae9SBrian Somers { 41926af0ae9SBrian Somers size_t total; 42026af0ae9SBrian Somers 42126af0ae9SBrian Somers if (bundle->ncp.mp.active) 42226af0ae9SBrian Somers total = mp_FillQueues(bundle); 42326af0ae9SBrian Somers else { 42426af0ae9SBrian Somers struct datalink *dl; 42526af0ae9SBrian Somers size_t add; 42626af0ae9SBrian Somers 42726af0ae9SBrian Somers for (total = 0, dl = bundle->links; dl; dl = dl->next) 42826af0ae9SBrian Somers if (dl->state == DATALINK_OPEN) { 42926af0ae9SBrian Somers add = link_QueueLen(&dl->physical->link); 43026af0ae9SBrian Somers if (add == 0 && dl->physical->out == NULL) 43126af0ae9SBrian Somers add = ip_PushPacket(&dl->physical->link, bundle); 43226af0ae9SBrian Somers total += add; 43326af0ae9SBrian Somers } 43426af0ae9SBrian Somers } 43526af0ae9SBrian Somers 43626af0ae9SBrian Somers return total + ip_QueueLen(&bundle->ncp.ipcp); 43726af0ae9SBrian Somers } 43826af0ae9SBrian Somers 4392f786681SBrian Somers static int 440f013f33eSBrian Somers bundle_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n) 4412f786681SBrian Somers { 4422f786681SBrian Somers struct bundle *bundle = descriptor2bundle(d); 4432f786681SBrian Somers struct datalink *dl; 44426af0ae9SBrian Somers int result, nlinks; 4456c1d6731SBrian Somers u_short ifqueue; 44626af0ae9SBrian Somers size_t queued; 4472f786681SBrian Somers 4482f786681SBrian Somers result = 0; 4492f786681SBrian Somers 450078c562eSBrian Somers /* If there are aren't many packets queued, look for some more. */ 45104eaa58cSBrian Somers for (nlinks = 0, dl = bundle->links; dl; dl = dl->next) 45204eaa58cSBrian Somers nlinks++; 45304eaa58cSBrian Somers 45404eaa58cSBrian Somers if (nlinks) { 4555a72b6edSBrian Somers queued = r ? bundle_FillQueues(bundle) : ip_QueueLen(&bundle->ncp.ipcp); 45604eaa58cSBrian Somers 457ff0f9439SBrian Somers if (r && (bundle->phase == PHASE_NETWORK || 458ff0f9439SBrian Somers bundle->phys_type.all & PHYS_AUTO)) { 45904eaa58cSBrian Somers /* enough surplus so that we can tell if we're getting swamped */ 4606c1d6731SBrian Somers ifqueue = nlinks > bundle->cfg.ifqueue ? nlinks : bundle->cfg.ifqueue; 4616c1d6731SBrian Somers if (queued < ifqueue) { 46204eaa58cSBrian Somers /* Not enough - select() for more */ 4636f8e9f0aSBrian Somers if (bundle->choked.timer.state == TIMER_RUNNING) 4646f8e9f0aSBrian Somers timer_Stop(&bundle->choked.timer); /* Not needed any more */ 46504eaa58cSBrian Somers FD_SET(bundle->dev.fd, r); 466faefde08SBrian Somers if (*n < bundle->dev.fd + 1) 467faefde08SBrian Somers *n = bundle->dev.fd + 1; 4681384bd27SBrian Somers log_Printf(LogTIMER, "%s: fdset(r) %d\n", TUN_NAME, bundle->dev.fd); 469078c562eSBrian Somers result++; 4706f8e9f0aSBrian Somers } else if (bundle->choked.timer.state == TIMER_STOPPED) { 4716f8e9f0aSBrian Somers bundle->choked.timer.func = bundle_ClearQueues; 4726f8e9f0aSBrian Somers bundle->choked.timer.name = "output choke"; 4736f8e9f0aSBrian Somers bundle->choked.timer.load = bundle->cfg.choked.timeout * SECTICKS; 4746f8e9f0aSBrian Somers bundle->choked.timer.arg = bundle; 4756f8e9f0aSBrian Somers timer_Start(&bundle->choked.timer); 476078c562eSBrian Somers } 47704eaa58cSBrian Somers } 47804eaa58cSBrian Somers } 479078c562eSBrian Somers 480f0cdd9c0SBrian Somers #ifndef NORADIUS 481f0cdd9c0SBrian Somers result += descriptor_UpdateSet(&bundle->radius.desc, r, w, e, n); 482f0cdd9c0SBrian Somers #endif 483f0cdd9c0SBrian Somers 48471555108SBrian Somers /* Which links need a select() ? */ 48571555108SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 48671555108SBrian Somers result += descriptor_UpdateSet(&dl->desc, r, w, e, n); 48771555108SBrian Somers 488ea722969SBrian Somers /* 489ea722969SBrian Somers * This *MUST* be called after the datalink UpdateSet()s as it 49004eaa58cSBrian Somers * might be ``holding'' one of the datalinks (death-row) and 4918e7bd08eSBrian Somers * wants to be able to de-select() it from the descriptor set. 492ea722969SBrian Somers */ 4930f2f3eb3SBrian Somers result += descriptor_UpdateSet(&bundle->ncp.mp.server.desc, r, w, e, n); 494ea722969SBrian Somers 4952f786681SBrian Somers return result; 4962f786681SBrian Somers } 4972f786681SBrian Somers 4982f786681SBrian Somers static int 499f013f33eSBrian Somers bundle_IsSet(struct fdescriptor *d, const fd_set *fdset) 5002f786681SBrian Somers { 5012f786681SBrian Somers struct bundle *bundle = descriptor2bundle(d); 5022f786681SBrian Somers struct datalink *dl; 5032f786681SBrian Somers 5042f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 5052f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 5062f786681SBrian Somers return 1; 5072f786681SBrian Somers 508f0cdd9c0SBrian Somers #ifndef NORADIUS 509f0cdd9c0SBrian Somers if (descriptor_IsSet(&bundle->radius.desc, fdset)) 510f0cdd9c0SBrian Somers return 1; 511f0cdd9c0SBrian Somers #endif 512f0cdd9c0SBrian Somers 513332b9de0SBrian Somers if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 514332b9de0SBrian Somers return 1; 515332b9de0SBrian Somers 516faefde08SBrian Somers return FD_ISSET(bundle->dev.fd, fdset); 5172f786681SBrian Somers } 5182f786681SBrian Somers 5192f786681SBrian Somers static void 520f013f33eSBrian Somers bundle_DescriptorRead(struct fdescriptor *d, struct bundle *bundle, 5212f786681SBrian Somers const fd_set *fdset) 5222f786681SBrian Somers { 5232f786681SBrian Somers struct datalink *dl; 5240a4b6c5cSBrian Somers unsigned secs; 5252f786681SBrian Somers 526ea722969SBrian Somers if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 527ea722969SBrian Somers descriptor_Read(&bundle->ncp.mp.server.desc, bundle, fdset); 528ea722969SBrian Somers 5292f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 5302f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 5312f786681SBrian Somers descriptor_Read(&dl->desc, bundle, fdset); 532b6217683SBrian Somers 533f0cdd9c0SBrian Somers #ifndef NORADIUS 534f0cdd9c0SBrian Somers if (descriptor_IsSet(&bundle->radius.desc, fdset)) 535f0cdd9c0SBrian Somers descriptor_Read(&bundle->radius.desc, bundle, fdset); 536f0cdd9c0SBrian Somers #endif 537f0cdd9c0SBrian Somers 538faefde08SBrian Somers if (FD_ISSET(bundle->dev.fd, fdset)) { 539078c562eSBrian Somers struct tun_data tun; 540078c562eSBrian Somers int n, pri; 5413a7b6d76SBrian Somers char *data; 5423a7b6d76SBrian Somers size_t sz; 5433a7b6d76SBrian Somers 5443a7b6d76SBrian Somers if (bundle->dev.header) { 5453a7b6d76SBrian Somers data = (char *)&tun; 5463a7b6d76SBrian Somers sz = sizeof tun; 5473a7b6d76SBrian Somers } else { 5483a7b6d76SBrian Somers data = tun.data; 5493a7b6d76SBrian Somers sz = sizeof tun.data; 5503a7b6d76SBrian Somers } 551078c562eSBrian Somers 552078c562eSBrian Somers /* something to read from tun */ 5533a7b6d76SBrian Somers 5543a7b6d76SBrian Somers n = read(bundle->dev.fd, data, sz); 555078c562eSBrian Somers if (n < 0) { 5563a7b6d76SBrian Somers log_Printf(LogWARN, "%s: read: %s\n", bundle->dev.Name, strerror(errno)); 557078c562eSBrian Somers return; 558078c562eSBrian Somers } 5593a7b6d76SBrian Somers 5603a7b6d76SBrian Somers if (bundle->dev.header) { 5613a7b6d76SBrian Somers n -= sz - sizeof tun.data; 562078c562eSBrian Somers if (n <= 0) { 5633a7b6d76SBrian Somers log_Printf(LogERROR, "%s: read: Got only %d bytes of data !\n", 5643a7b6d76SBrian Somers bundle->dev.Name, n); 565078c562eSBrian Somers return; 566078c562eSBrian Somers } 5670a4b6c5cSBrian Somers if (ntohl(tun.header.family) != AF_INET) 5683a7b6d76SBrian Somers /* XXX: Should be maintaining drop/family counts ! */ 569078c562eSBrian Somers return; 5703a7b6d76SBrian Somers } 571078c562eSBrian Somers 572078c562eSBrian Somers if (((struct ip *)tun.data)->ip_dst.s_addr == 573078c562eSBrian Somers bundle->ncp.ipcp.my_ip.s_addr) { 574078c562eSBrian Somers /* we've been asked to send something addressed *to* us :( */ 575078c562eSBrian Somers if (Enabled(bundle, OPT_LOOPBACK)) { 5760a4b6c5cSBrian Somers pri = PacketCheck(bundle, tun.data, n, &bundle->filter.in, NULL, NULL); 577078c562eSBrian Somers if (pri >= 0) { 5783a7b6d76SBrian Somers n += sz - sizeof tun.data; 5793a7b6d76SBrian Somers write(bundle->dev.fd, data, n); 580078c562eSBrian Somers log_Printf(LogDEBUG, "Looped back packet addressed to myself\n"); 581078c562eSBrian Somers } 582078c562eSBrian Somers return; 583078c562eSBrian Somers } else 584078c562eSBrian Somers log_Printf(LogDEBUG, "Oops - forwarding packet addressed to myself\n"); 585078c562eSBrian Somers } 586078c562eSBrian Somers 587078c562eSBrian Somers /* 588078c562eSBrian Somers * Process on-demand dialup. Output packets are queued within tunnel 589078c562eSBrian Somers * device until IPCP is opened. 590078c562eSBrian Somers */ 591078c562eSBrian Somers 592078c562eSBrian Somers if (bundle_Phase(bundle) == PHASE_DEAD) { 593078c562eSBrian Somers /* 594078c562eSBrian Somers * Note, we must be in AUTO mode :-/ otherwise our interface should 595078c562eSBrian Somers * *not* be UP and we can't receive data 596078c562eSBrian Somers */ 5970a4b6c5cSBrian Somers pri = PacketCheck(bundle, tun.data, n, &bundle->filter.dial, NULL, NULL); 598040cfe28SBrian Somers if (pri >= 0) 599ba23f397SBrian Somers bundle_Open(bundle, NULL, PHYS_AUTO, 0); 600078c562eSBrian Somers else 601078c562eSBrian Somers /* 602078c562eSBrian Somers * Drop the packet. If we were to queue it, we'd just end up with 603078c562eSBrian Somers * a pile of timed-out data in our output queue by the time we get 604078c562eSBrian Somers * around to actually dialing. We'd also prematurely reach the 605078c562eSBrian Somers * threshold at which we stop select()ing to read() the tun 606078c562eSBrian Somers * device - breaking auto-dial. 607078c562eSBrian Somers */ 608078c562eSBrian Somers return; 609078c562eSBrian Somers } 610078c562eSBrian Somers 6110a4b6c5cSBrian Somers secs = 0; 6120a4b6c5cSBrian Somers pri = PacketCheck(bundle, tun.data, n, &bundle->filter.out, NULL, &secs); 6130a4b6c5cSBrian Somers if (pri >= 0) { 6140a4b6c5cSBrian Somers /* Prepend the number of seconds timeout given in the filter */ 6150a4b6c5cSBrian Somers tun.header.timeout = secs; 6160a4b6c5cSBrian Somers ip_Enqueue(&bundle->ncp.ipcp, pri, (char *)&tun, n + sizeof tun.header); 6170a4b6c5cSBrian Somers } 618078c562eSBrian Somers } 619078c562eSBrian Somers } 6202f786681SBrian Somers 6211af29a6eSBrian Somers static int 622f013f33eSBrian Somers bundle_DescriptorWrite(struct fdescriptor *d, struct bundle *bundle, 6232f786681SBrian Somers const fd_set *fdset) 6242f786681SBrian Somers { 6252f786681SBrian Somers struct datalink *dl; 6261af29a6eSBrian Somers int result = 0; 6272f786681SBrian Somers 628ea722969SBrian Somers /* This is not actually necessary as struct mpserver doesn't Write() */ 629ea722969SBrian Somers if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset)) 630ea722969SBrian Somers descriptor_Write(&bundle->ncp.mp.server.desc, bundle, fdset); 631ea722969SBrian Somers 6322f786681SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 6332f786681SBrian Somers if (descriptor_IsSet(&dl->desc, fdset)) 6341af29a6eSBrian Somers result += descriptor_Write(&dl->desc, bundle, fdset); 6351af29a6eSBrian Somers 6361af29a6eSBrian Somers return result; 6372f786681SBrian Somers } 6382f786681SBrian Somers 639da66dd13SBrian Somers void 6401384bd27SBrian Somers bundle_LockTun(struct bundle *bundle) 6411384bd27SBrian Somers { 6421384bd27SBrian Somers FILE *lockfile; 6431384bd27SBrian Somers char pidfile[MAXPATHLEN]; 6441384bd27SBrian Somers 6451384bd27SBrian Somers snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit); 6461384bd27SBrian Somers lockfile = ID0fopen(pidfile, "w"); 6471384bd27SBrian Somers if (lockfile != NULL) { 6481384bd27SBrian Somers fprintf(lockfile, "%d\n", (int)getpid()); 6491384bd27SBrian Somers fclose(lockfile); 6501384bd27SBrian Somers } 6511384bd27SBrian Somers #ifndef RELEASE_CRUNCH 6521384bd27SBrian Somers else 6531384bd27SBrian Somers log_Printf(LogERROR, "Warning: Can't create %s: %s\n", 6541384bd27SBrian Somers pidfile, strerror(errno)); 6551384bd27SBrian Somers #endif 6561384bd27SBrian Somers } 6571384bd27SBrian Somers 6581384bd27SBrian Somers static void 6591384bd27SBrian Somers bundle_UnlockTun(struct bundle *bundle) 6601384bd27SBrian Somers { 6611384bd27SBrian Somers char pidfile[MAXPATHLEN]; 6621384bd27SBrian Somers 6631384bd27SBrian Somers snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit); 6641384bd27SBrian Somers ID0unlink(pidfile); 6651384bd27SBrian Somers } 6667a6f8720SBrian Somers 6677a6f8720SBrian Somers struct bundle * 668cf0a3940SBrian Somers bundle_Create(const char *prefix, int type, int unit) 6697a6f8720SBrian Somers { 6707a6f8720SBrian Somers static struct bundle bundle; /* there can be only one */ 671c0593e34SBrian Somers int enoentcount, err, minunit, maxunit; 6724e5196e9SBrian Somers const char *ifname; 673fdb4bb1bSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD) 674fc3034caSBrian Somers int kldtried; 675fc3034caSBrian Somers #endif 6763a7b6d76SBrian Somers #if defined(TUNSIFMODE) || defined(TUNSLMODE) || defined(TUNSIFHEAD) 6770f203c7eSBrian Somers int iff; 6780f203c7eSBrian Somers #endif 6797a6f8720SBrian Somers 6808fa6ebe4SBrian Somers if (bundle.iface != NULL) { /* Already allocated ! */ 681a33b2ef7SBrian Somers log_Printf(LogALERT, "bundle_Create: There's only one BUNDLE !\n"); 6827a6f8720SBrian Somers return NULL; 6837a6f8720SBrian Somers } 6847a6f8720SBrian Somers 685c0593e34SBrian Somers if (unit == -1) { 686c0593e34SBrian Somers minunit = 0; 687c0593e34SBrian Somers maxunit = -1; 688c0593e34SBrian Somers } else { 689c0593e34SBrian Somers minunit = unit; 690c0593e34SBrian Somers maxunit = unit + 1; 691c0593e34SBrian Somers } 6927a6f8720SBrian Somers err = ENOENT; 6937a6f8720SBrian Somers enoentcount = 0; 694fdb4bb1bSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD) 695fc3034caSBrian Somers kldtried = 0; 696fc3034caSBrian Somers #endif 697c0593e34SBrian Somers for (bundle.unit = minunit; bundle.unit != maxunit; bundle.unit++) { 698faefde08SBrian Somers snprintf(bundle.dev.Name, sizeof bundle.dev.Name, "%s%d", 699faefde08SBrian Somers prefix, bundle.unit); 700faefde08SBrian Somers bundle.dev.fd = ID0open(bundle.dev.Name, O_RDWR); 701faefde08SBrian Somers if (bundle.dev.fd >= 0) 7027a6f8720SBrian Somers break; 703107d62e7SBrian Somers else if (errno == ENXIO) { 704fdb4bb1bSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD) 705c0593e34SBrian Somers if (bundle.unit == minunit && !kldtried++) { 706fc3034caSBrian Somers /* 707fdb4bb1bSBrian Somers * Attempt to load the tunnel interface KLD if it isn't loaded 708fdb4bb1bSBrian Somers * already. 709fc3034caSBrian Somers */ 710fdb4bb1bSBrian Somers if (modfind("if_tun") == -1) { 711fc3034caSBrian Somers if (ID0kldload("if_tun") != -1) { 712fc3034caSBrian Somers bundle.unit--; 713fc3034caSBrian Somers continue; 714fc3034caSBrian Somers } 715fc3034caSBrian Somers log_Printf(LogWARN, "kldload: if_tun: %s\n", strerror(errno)); 716fc3034caSBrian Somers } 717fc3034caSBrian Somers } 718fc3034caSBrian Somers #endif 7197a6f8720SBrian Somers err = errno; 720107d62e7SBrian Somers break; 7217a6f8720SBrian Somers } else if (errno == ENOENT) { 7227a6f8720SBrian Somers if (++enoentcount > 2) 723107d62e7SBrian Somers break; 7247a6f8720SBrian Somers } else 7257a6f8720SBrian Somers err = errno; 7267a6f8720SBrian Somers } 7277a6f8720SBrian Somers 728faefde08SBrian Somers if (bundle.dev.fd < 0) { 729c0593e34SBrian Somers if (unit == -1) 730c0593e34SBrian Somers log_Printf(LogWARN, "No available tunnel devices found (%s)\n", 73185b542cfSBrian Somers strerror(err)); 732c0593e34SBrian Somers else 733c0593e34SBrian Somers log_Printf(LogWARN, "%s%d: %s\n", prefix, unit, strerror(err)); 7347a6f8720SBrian Somers return NULL; 7357a6f8720SBrian Somers } 7367a6f8720SBrian Somers 737dd7e2610SBrian Somers log_SetTun(bundle.unit); 7387a6f8720SBrian Somers 7398fa6ebe4SBrian Somers ifname = strrchr(bundle.dev.Name, '/'); 7408fa6ebe4SBrian Somers if (ifname == NULL) 7418fa6ebe4SBrian Somers ifname = bundle.dev.Name; 7427a6f8720SBrian Somers else 7438fa6ebe4SBrian Somers ifname++; 7448fa6ebe4SBrian Somers 7458fa6ebe4SBrian Somers bundle.iface = iface_Create(ifname); 7468fa6ebe4SBrian Somers if (bundle.iface == NULL) { 7478fa6ebe4SBrian Somers close(bundle.dev.fd); 7488fa6ebe4SBrian Somers return NULL; 7498fa6ebe4SBrian Somers } 7507a6f8720SBrian Somers 7510f203c7eSBrian Somers #ifdef TUNSIFMODE 7520f203c7eSBrian Somers /* Make sure we're POINTOPOINT */ 7530f203c7eSBrian Somers iff = IFF_POINTOPOINT; 7540f203c7eSBrian Somers if (ID0ioctl(bundle.dev.fd, TUNSIFMODE, &iff) < 0) 7550f203c7eSBrian Somers log_Printf(LogERROR, "bundle_Create: ioctl(TUNSIFMODE): %s\n", 7560f203c7eSBrian Somers strerror(errno)); 7570f203c7eSBrian Somers #endif 7580f203c7eSBrian Somers 7596ca65df0SBrian Somers #ifdef TUNSLMODE 7603a7b6d76SBrian Somers /* Make sure we're not prepending sockaddrs */ 7616ca65df0SBrian Somers iff = 0; 7626ca65df0SBrian Somers if (ID0ioctl(bundle.dev.fd, TUNSLMODE, &iff) < 0) 7636ca65df0SBrian Somers log_Printf(LogERROR, "bundle_Create: ioctl(TUNSLMODE): %s\n", 7646ca65df0SBrian Somers strerror(errno)); 7656ca65df0SBrian Somers #endif 7666ca65df0SBrian Somers 7673a7b6d76SBrian Somers #ifdef TUNSIFHEAD 7683a7b6d76SBrian Somers /* We want the address family please ! */ 7693a7b6d76SBrian Somers iff = 1; 7703a7b6d76SBrian Somers if (ID0ioctl(bundle.dev.fd, TUNSIFHEAD, &iff) < 0) { 7713a7b6d76SBrian Somers log_Printf(LogERROR, "bundle_Create: ioctl(TUNSIFHEAD): %s\n", 7723a7b6d76SBrian Somers strerror(errno)); 7733a7b6d76SBrian Somers bundle.dev.header = 0; 7743a7b6d76SBrian Somers } else 7753a7b6d76SBrian Somers bundle.dev.header = 1; 7763a7b6d76SBrian Somers #else 7773a7b6d76SBrian Somers #ifdef __OpenBSD__ 7783a7b6d76SBrian Somers /* Always present for OpenBSD */ 7793a7b6d76SBrian Somers bundle.dev.header = 1; 7803a7b6d76SBrian Somers #else 7813a7b6d76SBrian Somers /* 7823a7b6d76SBrian Somers * If TUNSIFHEAD isn't available and we're not OpenBSD, assume 7833a7b6d76SBrian Somers * everything's AF_INET (hopefully the tun device won't pass us 7843a7b6d76SBrian Somers * anything else !). 7853a7b6d76SBrian Somers */ 7863a7b6d76SBrian Somers bundle.dev.header = 0; 7873a7b6d76SBrian Somers #endif 7883a7b6d76SBrian Somers #endif 7893a7b6d76SBrian Somers 7904e5196e9SBrian Somers if (!iface_SetFlags(bundle.iface, IFF_UP)) { 7918fa6ebe4SBrian Somers iface_Destroy(bundle.iface); 7928fa6ebe4SBrian Somers bundle.iface = NULL; 793faefde08SBrian Somers close(bundle.dev.fd); 7947a6f8720SBrian Somers return NULL; 7957a6f8720SBrian Somers } 7967a6f8720SBrian Somers 7978fa6ebe4SBrian Somers log_Printf(LogPHASE, "Using interface: %s\n", ifname); 7987a6f8720SBrian Somers 799ab2de065SBrian Somers bundle.bandwidth = 0; 80094d7be52SBrian Somers bundle.mtu = 1500; 801820de6ebSBrian Somers bundle.routing_seq = 0; 802a0cbd833SBrian Somers bundle.phase = PHASE_DEAD; 803a0cbd833SBrian Somers bundle.CleaningUp = 0; 80467b072f7SBrian Somers bundle.NatEnabled = 0; 8057a6f8720SBrian Somers 8066d666775SBrian Somers bundle.fsm.LayerStart = bundle_LayerStart; 8076f384573SBrian Somers bundle.fsm.LayerUp = bundle_LayerUp; 8086d666775SBrian Somers bundle.fsm.LayerDown = bundle_LayerDown; 8096d666775SBrian Somers bundle.fsm.LayerFinish = bundle_LayerFinish; 8106d666775SBrian Somers bundle.fsm.object = &bundle; 8117a6f8720SBrian Somers 812dade2407SBrian Somers bundle.cfg.idle.timeout = NCP_IDLE_TIMEOUT; 813dade2407SBrian Somers bundle.cfg.idle.min_timeout = 0; 8141342caedSBrian Somers *bundle.cfg.auth.name = '\0'; 8151342caedSBrian Somers *bundle.cfg.auth.key = '\0'; 81694d7be52SBrian Somers bundle.cfg.opt = OPT_SROUTES | OPT_IDCHECK | OPT_LOOPBACK | OPT_TCPMSSFIXUP | 817610b185fSBrian Somers OPT_THROUGHPUT | OPT_UTMP; 81849052c95SBrian Somers *bundle.cfg.label = '\0'; 81949052c95SBrian Somers bundle.cfg.mtu = DEF_MTU; 8206c1d6731SBrian Somers bundle.cfg.ifqueue = DEF_IFQUEUE; 8216f8e9f0aSBrian Somers bundle.cfg.choked.timeout = CHOKED_TIMEOUT; 822ff0f9439SBrian Somers bundle.phys_type.all = type; 823ff0f9439SBrian Somers bundle.phys_type.open = 0; 824dade2407SBrian Somers bundle.upat = 0; 825ab886ad0SBrian Somers 8266f384573SBrian Somers bundle.links = datalink_Create("deflink", &bundle, type); 8273006ec67SBrian Somers if (bundle.links == NULL) { 828a33b2ef7SBrian Somers log_Printf(LogALERT, "Cannot create data link: %s\n", strerror(errno)); 8298fa6ebe4SBrian Somers iface_Destroy(bundle.iface); 8308fa6ebe4SBrian Somers bundle.iface = NULL; 831faefde08SBrian Somers close(bundle.dev.fd); 8322289f246SBrian Somers return NULL; 8332289f246SBrian Somers } 8342289f246SBrian Somers 8352f786681SBrian Somers bundle.desc.type = BUNDLE_DESCRIPTOR; 8362f786681SBrian Somers bundle.desc.UpdateSet = bundle_UpdateSet; 8372f786681SBrian Somers bundle.desc.IsSet = bundle_IsSet; 8382f786681SBrian Somers bundle.desc.Read = bundle_DescriptorRead; 8392f786681SBrian Somers bundle.desc.Write = bundle_DescriptorWrite; 8402f786681SBrian Somers 84149052c95SBrian Somers mp_Init(&bundle.ncp.mp, &bundle); 84249052c95SBrian Somers 84349052c95SBrian Somers /* Send over the first physical link by default */ 8445828db6dSBrian Somers ipcp_Init(&bundle.ncp.ipcp, &bundle, &bundle.links->physical->link, 8455828db6dSBrian Somers &bundle.fsm); 8466d666775SBrian Somers 8475ca5389aSBrian Somers memset(&bundle.filter, '\0', sizeof bundle.filter); 8485ca5389aSBrian Somers bundle.filter.in.fragok = bundle.filter.in.logok = 1; 8495ca5389aSBrian Somers bundle.filter.in.name = "IN"; 8505ca5389aSBrian Somers bundle.filter.out.fragok = bundle.filter.out.logok = 1; 8515ca5389aSBrian Somers bundle.filter.out.name = "OUT"; 8525ca5389aSBrian Somers bundle.filter.dial.name = "DIAL"; 8538390b576SBrian Somers bundle.filter.dial.logok = 1; 8545ca5389aSBrian Somers bundle.filter.alive.name = "ALIVE"; 8555ca5389aSBrian Somers bundle.filter.alive.logok = 1; 856cad7e742SBrian Somers { 857cad7e742SBrian Somers int i; 858cad7e742SBrian Somers for (i = 0; i < MAXFILTERS; i++) { 859cad7e742SBrian Somers bundle.filter.in.rule[i].f_action = A_NONE; 860cad7e742SBrian Somers bundle.filter.out.rule[i].f_action = A_NONE; 861cad7e742SBrian Somers bundle.filter.dial.rule[i].f_action = A_NONE; 862cad7e742SBrian Somers bundle.filter.alive.rule[i].f_action = A_NONE; 863cad7e742SBrian Somers } 864cad7e742SBrian Somers } 86593ee0ff2SBrian Somers memset(&bundle.idle.timer, '\0', sizeof bundle.idle.timer); 86693ee0ff2SBrian Somers bundle.idle.done = 0; 8675cf4388bSBrian Somers bundle.notify.fd = -1; 8686f8e9f0aSBrian Somers memset(&bundle.choked.timer, '\0', sizeof bundle.choked.timer); 869972a1bcfSBrian Somers #ifndef NORADIUS 870972a1bcfSBrian Somers radius_Init(&bundle.radius); 871972a1bcfSBrian Somers #endif 87293ee0ff2SBrian Somers 8736d666775SBrian Somers /* Clean out any leftover crud */ 8748fa6ebe4SBrian Somers iface_Clear(bundle.iface, IFACE_CLEAR_ALL); 8756d666775SBrian Somers 8761384bd27SBrian Somers bundle_LockTun(&bundle); 8771384bd27SBrian Somers 8787a6f8720SBrian Somers return &bundle; 8797a6f8720SBrian Somers } 8807a6f8720SBrian Somers 88168a0f0ccSBrian Somers static void 88268a0f0ccSBrian Somers bundle_DownInterface(struct bundle *bundle) 88368a0f0ccSBrian Somers { 884dd7e2610SBrian Somers route_IfDelete(bundle, 1); 8854e5196e9SBrian Somers iface_ClearFlags(bundle->iface, IFF_UP); 88668a0f0ccSBrian Somers } 88768a0f0ccSBrian Somers 88868a0f0ccSBrian Somers void 88968a0f0ccSBrian Somers bundle_Destroy(struct bundle *bundle) 89068a0f0ccSBrian Somers { 8913006ec67SBrian Somers struct datalink *dl; 892b6217683SBrian Somers 893ea722969SBrian Somers /* 89404eaa58cSBrian Somers * Clean up the interface. We don't need to timer_Stop()s, mp_Down(), 895ea722969SBrian Somers * ipcp_CleanInterface() and bundle_DownInterface() unless we're getting 8968e7bd08eSBrian Somers * out under exceptional conditions such as a descriptor exception. 897ea722969SBrian Somers */ 89804eaa58cSBrian Somers timer_Stop(&bundle->idle.timer); 8996f8e9f0aSBrian Somers timer_Stop(&bundle->choked.timer); 90066f634b6SBrian Somers mp_Down(&bundle->ncp.mp); 901dd7e2610SBrian Somers ipcp_CleanInterface(&bundle->ncp.ipcp); 90268a0f0ccSBrian Somers bundle_DownInterface(bundle); 9033006ec67SBrian Somers 904972a1bcfSBrian Somers #ifndef NORADIUS 905972a1bcfSBrian Somers /* Tell the radius server the bad news */ 906794c9bbcSBrian Somers log_Printf(LogDEBUG, "Radius: Destroy called from bundle_Destroy\n"); 907972a1bcfSBrian Somers radius_Destroy(&bundle->radius); 908972a1bcfSBrian Somers #endif 909972a1bcfSBrian Somers 910ea722969SBrian Somers /* Again, these are all DATALINK_CLOSED unless we're abending */ 9113006ec67SBrian Somers dl = bundle->links; 9123006ec67SBrian Somers while (dl) 9133006ec67SBrian Somers dl = datalink_Destroy(dl); 9143006ec67SBrian Somers 915442f8495SBrian Somers ipcp_Destroy(&bundle->ncp.ipcp); 916442f8495SBrian Somers 9171384bd27SBrian Somers close(bundle->dev.fd); 9181384bd27SBrian Somers bundle_UnlockTun(bundle); 9191384bd27SBrian Somers 920ea722969SBrian Somers /* In case we never made PHASE_NETWORK */ 9215cf4388bSBrian Somers bundle_Notify(bundle, EX_ERRDEAD); 922b6217683SBrian Somers 9238fa6ebe4SBrian Somers iface_Destroy(bundle->iface); 9248fa6ebe4SBrian Somers bundle->iface = NULL; 92568a0f0ccSBrian Somers } 92668a0f0ccSBrian Somers 9277a6f8720SBrian Somers struct rtmsg { 9287a6f8720SBrian Somers struct rt_msghdr m_rtm; 9297a6f8720SBrian Somers char m_space[64]; 9307a6f8720SBrian Somers }; 9317a6f8720SBrian Somers 932610b185fSBrian Somers int 933820de6ebSBrian Somers bundle_SetRoute(struct bundle *bundle, int cmd, struct in_addr dst, 9342062443fSBrian Somers struct in_addr gateway, struct in_addr mask, int bang, int ssh) 9357a6f8720SBrian Somers { 9367a6f8720SBrian Somers struct rtmsg rtmes; 9377a6f8720SBrian Somers int s, nb, wb; 9387a6f8720SBrian Somers char *cp; 9397a6f8720SBrian Somers const char *cmdstr; 9407a6f8720SBrian Somers struct sockaddr_in rtdata; 941610b185fSBrian Somers int result = 1; 9427a6f8720SBrian Somers 9437a6f8720SBrian Somers if (bang) 9447a6f8720SBrian Somers cmdstr = (cmd == RTM_ADD ? "Add!" : "Delete!"); 9457a6f8720SBrian Somers else 9467a6f8720SBrian Somers cmdstr = (cmd == RTM_ADD ? "Add" : "Delete"); 9477a6f8720SBrian Somers s = ID0socket(PF_ROUTE, SOCK_RAW, 0); 9487a6f8720SBrian Somers if (s < 0) { 949dd7e2610SBrian Somers log_Printf(LogERROR, "bundle_SetRoute: socket(): %s\n", strerror(errno)); 950610b185fSBrian Somers return result; 9517a6f8720SBrian Somers } 9527a6f8720SBrian Somers memset(&rtmes, '\0', sizeof rtmes); 9537a6f8720SBrian Somers rtmes.m_rtm.rtm_version = RTM_VERSION; 9547a6f8720SBrian Somers rtmes.m_rtm.rtm_type = cmd; 9557a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs = RTA_DST; 956820de6ebSBrian Somers rtmes.m_rtm.rtm_seq = ++bundle->routing_seq; 9577a6f8720SBrian Somers rtmes.m_rtm.rtm_pid = getpid(); 9587a6f8720SBrian Somers rtmes.m_rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC; 9597a6f8720SBrian Somers 9603afe5ccbSBrian Somers if (cmd == RTM_ADD || cmd == RTM_CHANGE) { 9613afe5ccbSBrian Somers if (bundle->ncp.ipcp.cfg.sendpipe > 0) { 9623afe5ccbSBrian Somers rtmes.m_rtm.rtm_rmx.rmx_sendpipe = bundle->ncp.ipcp.cfg.sendpipe; 9633afe5ccbSBrian Somers rtmes.m_rtm.rtm_inits |= RTV_SPIPE; 9643afe5ccbSBrian Somers } 9653afe5ccbSBrian Somers if (bundle->ncp.ipcp.cfg.recvpipe > 0) { 9663afe5ccbSBrian Somers rtmes.m_rtm.rtm_rmx.rmx_recvpipe = bundle->ncp.ipcp.cfg.recvpipe; 9673afe5ccbSBrian Somers rtmes.m_rtm.rtm_inits |= RTV_RPIPE; 9683afe5ccbSBrian Somers } 9693afe5ccbSBrian Somers } 9703afe5ccbSBrian Somers 9717a6f8720SBrian Somers memset(&rtdata, '\0', sizeof rtdata); 97250e5c17dSBrian Somers rtdata.sin_len = sizeof rtdata; 9737a6f8720SBrian Somers rtdata.sin_family = AF_INET; 9747a6f8720SBrian Somers rtdata.sin_port = 0; 9757a6f8720SBrian Somers rtdata.sin_addr = dst; 9767a6f8720SBrian Somers 9777a6f8720SBrian Somers cp = rtmes.m_space; 97850e5c17dSBrian Somers memcpy(cp, &rtdata, rtdata.sin_len); 97950e5c17dSBrian Somers cp += rtdata.sin_len; 980e43ebac1SBrian Somers if (cmd == RTM_ADD) { 9817a6f8720SBrian Somers if (gateway.s_addr == INADDR_ANY) { 9820aa8aa17SBrian Somers if (!ssh) 9838fa6ebe4SBrian Somers log_Printf(LogERROR, "bundle_SetRoute: Cannot add a route with" 9848fa6ebe4SBrian Somers " destination 0.0.0.0\n"); 9858fa6ebe4SBrian Somers close(s); 9868fa6ebe4SBrian Somers return result; 9877a6f8720SBrian Somers } else { 9887a6f8720SBrian Somers rtdata.sin_addr = gateway; 98950e5c17dSBrian Somers memcpy(cp, &rtdata, rtdata.sin_len); 99050e5c17dSBrian Somers cp += rtdata.sin_len; 9917a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY; 9927a6f8720SBrian Somers } 993e43ebac1SBrian Somers } 9947a6f8720SBrian Somers 9957a6f8720SBrian Somers if (dst.s_addr == INADDR_ANY) 9967a6f8720SBrian Somers mask.s_addr = INADDR_ANY; 9977a6f8720SBrian Somers 9987a6f8720SBrian Somers if (cmd == RTM_ADD || dst.s_addr == INADDR_ANY) { 9997a6f8720SBrian Somers rtdata.sin_addr = mask; 100050e5c17dSBrian Somers memcpy(cp, &rtdata, rtdata.sin_len); 100150e5c17dSBrian Somers cp += rtdata.sin_len; 10027a6f8720SBrian Somers rtmes.m_rtm.rtm_addrs |= RTA_NETMASK; 10037a6f8720SBrian Somers } 10047a6f8720SBrian Somers 10057a6f8720SBrian Somers nb = cp - (char *) &rtmes; 10067a6f8720SBrian Somers rtmes.m_rtm.rtm_msglen = nb; 10077a6f8720SBrian Somers wb = ID0write(s, &rtmes, nb); 10087a6f8720SBrian Somers if (wb < 0) { 1009dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute failure:\n"); 1010dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Cmd = %s\n", cmdstr); 1011dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Dst = %s\n", inet_ntoa(dst)); 10122cb305afSBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Gateway = %s\n", 10132cb305afSBrian Somers inet_ntoa(gateway)); 1014dd7e2610SBrian Somers log_Printf(LogTCPIP, "bundle_SetRoute: Mask = %s\n", inet_ntoa(mask)); 10157a6f8720SBrian Somers failed: 10167a6f8720SBrian Somers if (cmd == RTM_ADD && (rtmes.m_rtm.rtm_errno == EEXIST || 1017e43ebac1SBrian Somers (rtmes.m_rtm.rtm_errno == 0 && errno == EEXIST))) { 1018610b185fSBrian Somers if (!bang) { 1019dd7e2610SBrian Somers log_Printf(LogWARN, "Add route failed: %s already exists\n", 10203afe5ccbSBrian Somers dst.s_addr == 0 ? "default" : inet_ntoa(dst)); 1021610b185fSBrian Somers result = 0; /* Don't add to our dynamic list */ 1022610b185fSBrian Somers } else { 10237a6f8720SBrian Somers rtmes.m_rtm.rtm_type = cmd = RTM_CHANGE; 10247a6f8720SBrian Somers if ((wb = ID0write(s, &rtmes, nb)) < 0) 10257a6f8720SBrian Somers goto failed; 10267a6f8720SBrian Somers } 1027e43ebac1SBrian Somers } else if (cmd == RTM_DELETE && 10287a6f8720SBrian Somers (rtmes.m_rtm.rtm_errno == ESRCH || 10297a6f8720SBrian Somers (rtmes.m_rtm.rtm_errno == 0 && errno == ESRCH))) { 10307a6f8720SBrian Somers if (!bang) 1031dd7e2610SBrian Somers log_Printf(LogWARN, "Del route failed: %s: Non-existent\n", 10327a6f8720SBrian Somers inet_ntoa(dst)); 10332062443fSBrian Somers } else if (rtmes.m_rtm.rtm_errno == 0) { 10342062443fSBrian Somers if (!ssh || errno != ENETUNREACH) 1035dd7e2610SBrian Somers log_Printf(LogWARN, "%s route failed: %s: errno: %s\n", cmdstr, 10367a6f8720SBrian Somers inet_ntoa(dst), strerror(errno)); 10372062443fSBrian Somers } else 1038dd7e2610SBrian Somers log_Printf(LogWARN, "%s route failed: %s: %s\n", 10397a6f8720SBrian Somers cmdstr, inet_ntoa(dst), strerror(rtmes.m_rtm.rtm_errno)); 10407a6f8720SBrian Somers } 1041dd7e2610SBrian Somers log_Printf(LogDEBUG, "wrote %d: cmd = %s, dst = %x, gateway = %x\n", 1042fe3125a0SBrian Somers wb, cmdstr, (unsigned)dst.s_addr, (unsigned)gateway.s_addr); 10437a6f8720SBrian Somers close(s); 1044610b185fSBrian Somers 1045610b185fSBrian Somers return result; 10467a6f8720SBrian Somers } 104783d1af55SBrian Somers 104883d1af55SBrian Somers void 10493006ec67SBrian Somers bundle_LinkClosed(struct bundle *bundle, struct datalink *dl) 10503006ec67SBrian Somers { 10513006ec67SBrian Somers /* 10523006ec67SBrian Somers * Our datalink has closed. 1053ea722969SBrian Somers * CleanDatalinks() (called from DoLoop()) will remove closed 1054f6a4e748SBrian Somers * BACKGROUND, FOREGROUND and DIRECT links. 10553b0f8d2eSBrian Somers * If it's the last data link, enter phase DEAD. 1056ea722969SBrian Somers * 1057ea722969SBrian Somers * NOTE: dl may not be in our list (bundle_SendDatalink()) ! 10583006ec67SBrian Somers */ 10595b8b8060SBrian Somers 10603b0f8d2eSBrian Somers struct datalink *odl; 10613b0f8d2eSBrian Somers int other_links; 10625b8b8060SBrian Somers 1063bf1d3ff6SBrian Somers log_SetTtyCommandMode(dl); 1064bf1d3ff6SBrian Somers 10653b0f8d2eSBrian Somers other_links = 0; 10663b0f8d2eSBrian Somers for (odl = bundle->links; odl; odl = odl->next) 10673b0f8d2eSBrian Somers if (odl != dl && odl->state != DATALINK_CLOSED) 10683b0f8d2eSBrian Somers other_links++; 10693b0f8d2eSBrian Somers 10703b0f8d2eSBrian Somers if (!other_links) { 107181358fa3SBrian Somers if (dl->physical->type != PHYS_AUTO) /* Not in -auto mode */ 107203704096SBrian Somers bundle_DownInterface(bundle); 107309206a6fSBrian Somers fsm2initial(&bundle->ncp.ipcp.fsm); 10745563ebdeSBrian Somers bundle_NewPhase(bundle, PHASE_DEAD); 10750f2f3eb3SBrian Somers bundle_StopIdleTimer(bundle); 1076ab2de065SBrian Somers } 1077455aabc3SBrian Somers } 1078455aabc3SBrian Somers 1079455aabc3SBrian Somers void 1080ba23f397SBrian Somers bundle_Open(struct bundle *bundle, const char *name, int mask, int force) 10813006ec67SBrian Somers { 10823006ec67SBrian Somers /* 10833006ec67SBrian Somers * Please open the given datalink, or all if name == NULL 10843006ec67SBrian Somers */ 10853006ec67SBrian Somers struct datalink *dl; 10863006ec67SBrian Somers 10873006ec67SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 10883006ec67SBrian Somers if (name == NULL || !strcasecmp(dl->name, name)) { 1089ba23f397SBrian Somers if ((mask & dl->physical->type) && 1090ba23f397SBrian Somers (dl->state == DATALINK_CLOSED || 1091ba23f397SBrian Somers (force && dl->state == DATALINK_OPENING && 10925e269efeSBrian Somers dl->dial.timer.state == TIMER_RUNNING) || 10935e269efeSBrian Somers dl->state == DATALINK_READY)) { 10945e269efeSBrian Somers timer_Stop(&dl->dial.timer); /* We're finished with this */ 1095565e35e5SBrian Somers datalink_Up(dl, 1, 1); 1096ab2de065SBrian Somers if (mask & PHYS_AUTO) 10975e269efeSBrian Somers break; /* Only one AUTO link at a time */ 109804eaa58cSBrian Somers } 10993006ec67SBrian Somers if (name != NULL) 11003006ec67SBrian Somers break; 11013006ec67SBrian Somers } 11023006ec67SBrian Somers } 11033006ec67SBrian Somers 11043006ec67SBrian Somers struct datalink * 11053006ec67SBrian Somers bundle2datalink(struct bundle *bundle, const char *name) 11063006ec67SBrian Somers { 11073006ec67SBrian Somers struct datalink *dl; 11083006ec67SBrian Somers 11093006ec67SBrian Somers if (name != NULL) { 11103006ec67SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 11113006ec67SBrian Somers if (!strcasecmp(dl->name, name)) 11123006ec67SBrian Somers return dl; 11133006ec67SBrian Somers } else if (bundle->links && !bundle->links->next) 11143006ec67SBrian Somers return bundle->links; 11153006ec67SBrian Somers 11163006ec67SBrian Somers return NULL; 11173006ec67SBrian Somers } 11183006ec67SBrian Somers 11193006ec67SBrian Somers int 1120aef795ccSBrian Somers bundle_ShowLinks(struct cmdargs const *arg) 1121aef795ccSBrian Somers { 1122aef795ccSBrian Somers struct datalink *dl; 1123ab2de065SBrian Somers struct pppThroughput *t; 112491cbd2eeSBrian Somers unsigned long long octets; 1125ab2de065SBrian Somers int secs; 1126aef795ccSBrian Somers 11279c53a7b1SBrian Somers for (dl = arg->bundle->links; dl; dl = dl->next) { 112891cbd2eeSBrian Somers octets = MAX(dl->physical->link.stats.total.in.OctetsPerSecond, 112991cbd2eeSBrian Somers dl->physical->link.stats.total.out.OctetsPerSecond); 113091cbd2eeSBrian Somers 1131d4156d00SBrian Somers prompt_Printf(arg->prompt, "Name: %s [%s, %s]", 1132d4156d00SBrian Somers dl->name, mode2Nam(dl->physical->type), datalink_State(dl)); 113311572abfSBrian Somers if (dl->physical->link.stats.total.rolling && dl->state == DATALINK_OPEN) 1134e531f89aSBrian Somers prompt_Printf(arg->prompt, " bandwidth %d, %llu bps (%llu bytes/sec)", 1135ab2de065SBrian Somers dl->mp.bandwidth ? dl->mp.bandwidth : 1136ab2de065SBrian Somers physical_GetSpeed(dl->physical), 113791cbd2eeSBrian Somers octets * 8, octets); 11389c53a7b1SBrian Somers prompt_Printf(arg->prompt, "\n"); 11399c53a7b1SBrian Somers } 1140aef795ccSBrian Somers 114111572abfSBrian Somers t = &arg->bundle->ncp.mp.link.stats.total; 114291cbd2eeSBrian Somers octets = MAX(t->in.OctetsPerSecond, t->out.OctetsPerSecond); 1143ab2de065SBrian Somers secs = t->downtime ? 0 : throughput_uptime(t); 1144ab2de065SBrian Somers if (secs > t->SamplePeriod) 1145ab2de065SBrian Somers secs = t->SamplePeriod; 1146ab2de065SBrian Somers if (secs) 1147e531f89aSBrian Somers prompt_Printf(arg->prompt, "Currently averaging %llu bps (%llu bytes/sec)" 114891cbd2eeSBrian Somers " over the last %d secs\n", octets * 8, octets, secs); 1149ab2de065SBrian Somers 1150aef795ccSBrian Somers return 0; 1151aef795ccSBrian Somers } 1152ab886ad0SBrian Somers 11531342caedSBrian Somers static const char * 11541342caedSBrian Somers optval(struct bundle *bundle, int bit) 11551342caedSBrian Somers { 11561342caedSBrian Somers return (bundle->cfg.opt & bit) ? "enabled" : "disabled"; 11571342caedSBrian Somers } 11581342caedSBrian Somers 1159c08717dfSBrian Somers int 1160c08717dfSBrian Somers bundle_ShowStatus(struct cmdargs const *arg) 1161c08717dfSBrian Somers { 1162c08717dfSBrian Somers int remaining; 1163c08717dfSBrian Somers 1164c08717dfSBrian Somers prompt_Printf(arg->prompt, "Phase %s\n", bundle_PhaseName(arg->bundle)); 1165faefde08SBrian Somers prompt_Printf(arg->prompt, " Device: %s\n", arg->bundle->dev.Name); 1166dade2407SBrian Somers prompt_Printf(arg->prompt, " Interface: %s @ %lubps", 1167ab2de065SBrian Somers arg->bundle->iface->name, arg->bundle->bandwidth); 1168c08717dfSBrian Somers 1169dade2407SBrian Somers if (arg->bundle->upat) { 1170dade2407SBrian Somers int secs = time(NULL) - arg->bundle->upat; 1171dade2407SBrian Somers 1172dade2407SBrian Somers prompt_Printf(arg->prompt, ", up time %d:%02d:%02d", secs / 3600, 1173dade2407SBrian Somers (secs / 60) % 60, secs % 60); 1174dade2407SBrian Somers } 1175669b9965SBrian Somers prompt_Printf(arg->prompt, "\n Queued: %lu of %u\n", 117677fc031dSBrian Somers (unsigned long)ip_QueueLen(&arg->bundle->ncp.ipcp), 117777fc031dSBrian Somers arg->bundle->cfg.ifqueue); 1178dade2407SBrian Somers 11796c1d6731SBrian Somers prompt_Printf(arg->prompt, "\nDefaults:\n"); 1180643f4904SBrian Somers prompt_Printf(arg->prompt, " Label: %s\n", arg->bundle->cfg.label); 1181610b185fSBrian Somers prompt_Printf(arg->prompt, " Auth name: %s\n", 1182610b185fSBrian Somers arg->bundle->cfg.auth.name); 118304eaa58cSBrian Somers 11846f8e9f0aSBrian Somers prompt_Printf(arg->prompt, " Choked Timer: %ds\n", 11856f8e9f0aSBrian Somers arg->bundle->cfg.choked.timeout); 1186972a1bcfSBrian Somers 1187972a1bcfSBrian Somers #ifndef NORADIUS 1188972a1bcfSBrian Somers radius_Show(&arg->bundle->radius, arg->prompt); 1189972a1bcfSBrian Somers #endif 1190972a1bcfSBrian Somers 1191c08717dfSBrian Somers prompt_Printf(arg->prompt, " Idle Timer: "); 1192dade2407SBrian Somers if (arg->bundle->cfg.idle.timeout) { 1193dade2407SBrian Somers prompt_Printf(arg->prompt, "%ds", arg->bundle->cfg.idle.timeout); 1194dade2407SBrian Somers if (arg->bundle->cfg.idle.min_timeout) 1195dade2407SBrian Somers prompt_Printf(arg->prompt, ", min %ds", 1196dade2407SBrian Somers arg->bundle->cfg.idle.min_timeout); 1197c08717dfSBrian Somers remaining = bundle_RemainingIdleTime(arg->bundle); 1198c08717dfSBrian Somers if (remaining != -1) 1199c08717dfSBrian Somers prompt_Printf(arg->prompt, " (%ds remaining)", remaining); 1200c08717dfSBrian Somers prompt_Printf(arg->prompt, "\n"); 1201c08717dfSBrian Somers } else 1202c08717dfSBrian Somers prompt_Printf(arg->prompt, "disabled\n"); 1203ce828a6eSBrian Somers prompt_Printf(arg->prompt, " MTU: "); 1204ce828a6eSBrian Somers if (arg->bundle->cfg.mtu) 1205ce828a6eSBrian Somers prompt_Printf(arg->prompt, "%d\n", arg->bundle->cfg.mtu); 1206ce828a6eSBrian Somers else 1207ce828a6eSBrian Somers prompt_Printf(arg->prompt, "unspecified\n"); 1208c08717dfSBrian Somers 12093afe5ccbSBrian Somers prompt_Printf(arg->prompt, " sendpipe: "); 12103afe5ccbSBrian Somers if (arg->bundle->ncp.ipcp.cfg.sendpipe > 0) 1211ab2de065SBrian Somers prompt_Printf(arg->prompt, "%-20ld", arg->bundle->ncp.ipcp.cfg.sendpipe); 12123afe5ccbSBrian Somers else 1213ab2de065SBrian Somers prompt_Printf(arg->prompt, "unspecified "); 12143afe5ccbSBrian Somers prompt_Printf(arg->prompt, " recvpipe: "); 12153afe5ccbSBrian Somers if (arg->bundle->ncp.ipcp.cfg.recvpipe > 0) 12163afe5ccbSBrian Somers prompt_Printf(arg->prompt, "%ld\n", arg->bundle->ncp.ipcp.cfg.recvpipe); 12173afe5ccbSBrian Somers else 12183afe5ccbSBrian Somers prompt_Printf(arg->prompt, "unspecified\n"); 12193afe5ccbSBrian Somers 1220ab2de065SBrian Somers prompt_Printf(arg->prompt, " Sticky Routes: %-20.20s", 1221610b185fSBrian Somers optval(arg->bundle, OPT_SROUTES)); 122298251667SBrian Somers prompt_Printf(arg->prompt, " Filter Decap: %s\n", 122398251667SBrian Somers optval(arg->bundle, OPT_FILTERDECAP)); 122498251667SBrian Somers prompt_Printf(arg->prompt, " ID check: %-20.20s", 12251342caedSBrian Somers optval(arg->bundle, OPT_IDCHECK)); 122698251667SBrian Somers prompt_Printf(arg->prompt, " Keep-Session: %s\n", 1227ac685e31SBrian Somers optval(arg->bundle, OPT_KEEPSESSION)); 122898251667SBrian Somers prompt_Printf(arg->prompt, " Loopback: %-20.20s", 12291342caedSBrian Somers optval(arg->bundle, OPT_LOOPBACK)); 123098251667SBrian Somers prompt_Printf(arg->prompt, " PasswdAuth: %s\n", 12311342caedSBrian Somers optval(arg->bundle, OPT_PASSWDAUTH)); 123298251667SBrian Somers prompt_Printf(arg->prompt, " Proxy: %-20.20s", 12331342caedSBrian Somers optval(arg->bundle, OPT_PROXY)); 123498251667SBrian Somers prompt_Printf(arg->prompt, " Proxyall: %s\n", 12353afe5ccbSBrian Somers optval(arg->bundle, OPT_PROXYALL)); 123694d7be52SBrian Somers prompt_Printf(arg->prompt, " TCPMSS Fixup: %-20.20s", 123794d7be52SBrian Somers optval(arg->bundle, OPT_TCPMSSFIXUP)); 123894d7be52SBrian Somers prompt_Printf(arg->prompt, " Throughput: %s\n", 12391342caedSBrian Somers optval(arg->bundle, OPT_THROUGHPUT)); 124094d7be52SBrian Somers prompt_Printf(arg->prompt, " Utmp Logging: %-20.20s", 12411342caedSBrian Somers optval(arg->bundle, OPT_UTMP)); 12428fa6ebe4SBrian Somers prompt_Printf(arg->prompt, " Iface-Alias: %s\n", 12438fa6ebe4SBrian Somers optval(arg->bundle, OPT_IFACEALIAS)); 12441342caedSBrian Somers 1245c08717dfSBrian Somers return 0; 1246c08717dfSBrian Somers } 1247c08717dfSBrian Somers 1248ab886ad0SBrian Somers static void 1249ab886ad0SBrian Somers bundle_IdleTimeout(void *v) 1250ab886ad0SBrian Somers { 1251ab886ad0SBrian Somers struct bundle *bundle = (struct bundle *)v; 1252ab886ad0SBrian Somers 1253b42135deSBrian Somers log_Printf(LogPHASE, "Idle timer expired\n"); 125404eaa58cSBrian Somers bundle_StopIdleTimer(bundle); 12559c81b87dSBrian Somers bundle_Close(bundle, NULL, CLOSE_STAYDOWN); 1256ab886ad0SBrian Somers } 1257ab886ad0SBrian Somers 1258ab886ad0SBrian Somers /* 1259ab886ad0SBrian Somers * Start Idle timer. If timeout is reached, we call bundle_Close() to 1260ab886ad0SBrian Somers * close LCP and link. 1261ab886ad0SBrian Somers */ 1262ab886ad0SBrian Somers void 12630a4b6c5cSBrian Somers bundle_StartIdleTimer(struct bundle *bundle, unsigned secs) 1264ab886ad0SBrian Somers { 1265dd7e2610SBrian Somers timer_Stop(&bundle->idle.timer); 1266ff0f9439SBrian Somers if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) != 1267dade2407SBrian Somers bundle->phys_type.open && bundle->cfg.idle.timeout) { 12680a4b6c5cSBrian Somers time_t now = time(NULL); 1269dade2407SBrian Somers 12700a4b6c5cSBrian Somers if (secs == 0) 1271dade2407SBrian Somers secs = bundle->cfg.idle.timeout; 12720a4b6c5cSBrian Somers 12730a4b6c5cSBrian Somers /* We want at least `secs' */ 1274dade2407SBrian Somers if (bundle->cfg.idle.min_timeout > secs && bundle->upat) { 12750a4b6c5cSBrian Somers int up = now - bundle->upat; 1276dade2407SBrian Somers 1277dade2407SBrian Somers if ((long long)bundle->cfg.idle.min_timeout - up > (long long)secs) 12780a4b6c5cSBrian Somers /* Only increase from the current `remaining' value */ 1279dade2407SBrian Somers secs = bundle->cfg.idle.min_timeout - up; 1280dade2407SBrian Somers } 128193ee0ff2SBrian Somers bundle->idle.timer.func = bundle_IdleTimeout; 12823b0f8d2eSBrian Somers bundle->idle.timer.name = "idle"; 1283dade2407SBrian Somers bundle->idle.timer.load = secs * SECTICKS; 128493ee0ff2SBrian Somers bundle->idle.timer.arg = bundle; 1285dd7e2610SBrian Somers timer_Start(&bundle->idle.timer); 12860a4b6c5cSBrian Somers bundle->idle.done = now + secs; 1287ab886ad0SBrian Somers } 1288ab886ad0SBrian Somers } 1289ab886ad0SBrian Somers 1290ab886ad0SBrian Somers void 1291dade2407SBrian Somers bundle_SetIdleTimer(struct bundle *bundle, int timeout, int min_timeout) 1292ab886ad0SBrian Somers { 1293dade2407SBrian Somers bundle->cfg.idle.timeout = timeout; 1294dade2407SBrian Somers if (min_timeout >= 0) 1295dade2407SBrian Somers bundle->cfg.idle.min_timeout = min_timeout; 1296ab886ad0SBrian Somers if (bundle_LinkIsUp(bundle)) 12970a4b6c5cSBrian Somers bundle_StartIdleTimer(bundle, 0); 1298ab886ad0SBrian Somers } 1299ab886ad0SBrian Somers 1300ab886ad0SBrian Somers void 1301ab886ad0SBrian Somers bundle_StopIdleTimer(struct bundle *bundle) 1302ab886ad0SBrian Somers { 1303dd7e2610SBrian Somers timer_Stop(&bundle->idle.timer); 13044a632c80SBrian Somers bundle->idle.done = 0; 1305ab886ad0SBrian Somers } 1306ab886ad0SBrian Somers 130704eaa58cSBrian Somers static int 1308ab886ad0SBrian Somers bundle_RemainingIdleTime(struct bundle *bundle) 1309ab886ad0SBrian Somers { 131093ee0ff2SBrian Somers if (bundle->idle.done) 131193ee0ff2SBrian Somers return bundle->idle.done - time(NULL); 1312ab886ad0SBrian Somers return -1; 1313ab886ad0SBrian Somers } 13143b0f8d2eSBrian Somers 13153b0f8d2eSBrian Somers int 13163b0f8d2eSBrian Somers bundle_IsDead(struct bundle *bundle) 13173b0f8d2eSBrian Somers { 13183b0f8d2eSBrian Somers return !bundle->links || (bundle->phase == PHASE_DEAD && bundle->CleaningUp); 13193b0f8d2eSBrian Somers } 1320b6217683SBrian Somers 132104eaa58cSBrian Somers static struct datalink * 132204eaa58cSBrian Somers bundle_DatalinkLinkout(struct bundle *bundle, struct datalink *dl) 1323cd7bd93aSBrian Somers { 1324cd7bd93aSBrian Somers struct datalink **dlp; 1325cd7bd93aSBrian Somers 1326cd7bd93aSBrian Somers for (dlp = &bundle->links; *dlp; dlp = &(*dlp)->next) 1327cd7bd93aSBrian Somers if (*dlp == dl) { 132804eaa58cSBrian Somers *dlp = dl->next; 132904eaa58cSBrian Somers dl->next = NULL; 133004eaa58cSBrian Somers bundle_LinksRemoved(bundle); 133104eaa58cSBrian Somers return dl; 1332cd7bd93aSBrian Somers } 133304eaa58cSBrian Somers 133404eaa58cSBrian Somers return NULL; 133504eaa58cSBrian Somers } 133604eaa58cSBrian Somers 133704eaa58cSBrian Somers static void 133804eaa58cSBrian Somers bundle_DatalinkLinkin(struct bundle *bundle, struct datalink *dl) 133904eaa58cSBrian Somers { 134004eaa58cSBrian Somers struct datalink **dlp = &bundle->links; 134104eaa58cSBrian Somers 134204eaa58cSBrian Somers while (*dlp) 134304eaa58cSBrian Somers dlp = &(*dlp)->next; 134404eaa58cSBrian Somers 134504eaa58cSBrian Somers *dlp = dl; 134604eaa58cSBrian Somers dl->next = NULL; 134704eaa58cSBrian Somers 134804eaa58cSBrian Somers bundle_LinkAdded(bundle, dl); 1349ab2de065SBrian Somers mp_CheckAutoloadTimer(&bundle->ncp.mp); 1350565e35e5SBrian Somers } 1351565e35e5SBrian Somers 1352565e35e5SBrian Somers void 1353565e35e5SBrian Somers bundle_CleanDatalinks(struct bundle *bundle) 1354565e35e5SBrian Somers { 1355565e35e5SBrian Somers struct datalink **dlp = &bundle->links; 135604eaa58cSBrian Somers int found = 0; 1357565e35e5SBrian Somers 1358565e35e5SBrian Somers while (*dlp) 1359565e35e5SBrian Somers if ((*dlp)->state == DATALINK_CLOSED && 1360f6a4e748SBrian Somers (*dlp)->physical->type & 1361f6a4e748SBrian Somers (PHYS_DIRECT|PHYS_BACKGROUND|PHYS_FOREGROUND)) { 1362565e35e5SBrian Somers *dlp = datalink_Destroy(*dlp); 136304eaa58cSBrian Somers found++; 136404eaa58cSBrian Somers } else 1365565e35e5SBrian Somers dlp = &(*dlp)->next; 136604eaa58cSBrian Somers 136704eaa58cSBrian Somers if (found) 136804eaa58cSBrian Somers bundle_LinksRemoved(bundle); 136904eaa58cSBrian Somers } 137004eaa58cSBrian Somers 137104eaa58cSBrian Somers int 137204eaa58cSBrian Somers bundle_DatalinkClone(struct bundle *bundle, struct datalink *dl, 137304eaa58cSBrian Somers const char *name) 137404eaa58cSBrian Somers { 137504eaa58cSBrian Somers if (bundle2datalink(bundle, name)) { 137604eaa58cSBrian Somers log_Printf(LogWARN, "Clone: %s: name already exists\n", name); 137704eaa58cSBrian Somers return 0; 137804eaa58cSBrian Somers } 137904eaa58cSBrian Somers 138004eaa58cSBrian Somers bundle_DatalinkLinkin(bundle, datalink_Clone(dl, name)); 138104eaa58cSBrian Somers return 1; 138204eaa58cSBrian Somers } 138304eaa58cSBrian Somers 138404eaa58cSBrian Somers void 138504eaa58cSBrian Somers bundle_DatalinkRemove(struct bundle *bundle, struct datalink *dl) 138604eaa58cSBrian Somers { 138704eaa58cSBrian Somers dl = bundle_DatalinkLinkout(bundle, dl); 138804eaa58cSBrian Somers if (dl) 138904eaa58cSBrian Somers datalink_Destroy(dl); 1390cd7bd93aSBrian Somers } 139149052c95SBrian Somers 139249052c95SBrian Somers void 139349052c95SBrian Somers bundle_SetLabel(struct bundle *bundle, const char *label) 139449052c95SBrian Somers { 139549052c95SBrian Somers if (label) 139649052c95SBrian Somers strncpy(bundle->cfg.label, label, sizeof bundle->cfg.label - 1); 139749052c95SBrian Somers else 139849052c95SBrian Somers *bundle->cfg.label = '\0'; 139949052c95SBrian Somers } 140049052c95SBrian Somers 140149052c95SBrian Somers const char * 140249052c95SBrian Somers bundle_GetLabel(struct bundle *bundle) 140349052c95SBrian Somers { 140449052c95SBrian Somers return *bundle->cfg.label ? bundle->cfg.label : NULL; 140549052c95SBrian Somers } 14061fa665f5SBrian Somers 14072cb305afSBrian Somers int 14082cb305afSBrian Somers bundle_LinkSize() 14091fa665f5SBrian Somers { 141096c9bb21SBrian Somers struct iovec iov[SCATTER_SEGMENTS]; 14112cb305afSBrian Somers int niov, expect, f; 141287c3786eSBrian Somers 141396c9bb21SBrian Somers iov[0].iov_len = strlen(Version) + 1; 14142cb305afSBrian Somers iov[0].iov_base = NULL; 14152cb305afSBrian Somers niov = 1; 14162cb305afSBrian Somers if (datalink2iov(NULL, iov, &niov, SCATTER_SEGMENTS, NULL, NULL) == -1) { 14172cb305afSBrian Somers log_Printf(LogERROR, "Cannot determine space required for link\n"); 14182cb305afSBrian Somers return 0; 141954cd8e13SBrian Somers } 14206f384573SBrian Somers 142196c9bb21SBrian Somers for (f = expect = 0; f < niov; f++) 142296c9bb21SBrian Somers expect += iov[f].iov_len; 142396c9bb21SBrian Somers 14242cb305afSBrian Somers return expect; 142587c3786eSBrian Somers } 142696c9bb21SBrian Somers 14272cb305afSBrian Somers void 14282cb305afSBrian Somers bundle_ReceiveDatalink(struct bundle *bundle, int s) 14292cb305afSBrian Somers { 14302cb305afSBrian Somers char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int) * SEND_MAXFD]; 1431cbee9754SBrian Somers int niov, expect, f, *fd, nfd, onfd, got; 14322cb305afSBrian Somers struct iovec iov[SCATTER_SEGMENTS]; 14332cb305afSBrian Somers struct cmsghdr *cmsg; 14342cb305afSBrian Somers struct msghdr msg; 14352cb305afSBrian Somers struct datalink *dl; 14362cb305afSBrian Somers pid_t pid; 14372cb305afSBrian Somers 14382cb305afSBrian Somers log_Printf(LogPHASE, "Receiving datalink\n"); 14392cb305afSBrian Somers 14402cb305afSBrian Somers /* 14412cb305afSBrian Somers * Create our scatter/gather array - passing NULL gets the space 14422cb305afSBrian Somers * allocation requirement rather than actually flattening the 14432cb305afSBrian Somers * structures. 14442cb305afSBrian Somers */ 14452cb305afSBrian Somers iov[0].iov_len = strlen(Version) + 1; 14462cb305afSBrian Somers iov[0].iov_base = NULL; 14472cb305afSBrian Somers niov = 1; 14482cb305afSBrian Somers if (datalink2iov(NULL, iov, &niov, SCATTER_SEGMENTS, NULL, NULL) == -1) { 14492cb305afSBrian Somers log_Printf(LogERROR, "Cannot determine space required for link\n"); 14502cb305afSBrian Somers return; 14512cb305afSBrian Somers } 14522cb305afSBrian Somers 14532cb305afSBrian Somers /* Allocate the scatter/gather array for recvmsg() */ 14542cb305afSBrian Somers for (f = expect = 0; f < niov; f++) { 14552cb305afSBrian Somers if ((iov[f].iov_base = malloc(iov[f].iov_len)) == NULL) { 14562cb305afSBrian Somers log_Printf(LogERROR, "Cannot allocate space to receive link\n"); 14572cb305afSBrian Somers return; 14582cb305afSBrian Somers } 1459cbee9754SBrian Somers if (f) 14602cb305afSBrian Somers expect += iov[f].iov_len; 14612cb305afSBrian Somers } 14622cb305afSBrian Somers 14632cb305afSBrian Somers /* Set up our message */ 14642cb305afSBrian Somers cmsg = (struct cmsghdr *)cmsgbuf; 14652cb305afSBrian Somers cmsg->cmsg_len = sizeof cmsgbuf; 14662cb305afSBrian Somers cmsg->cmsg_level = SOL_SOCKET; 14672cb305afSBrian Somers cmsg->cmsg_type = 0; 14682cb305afSBrian Somers 146996c9bb21SBrian Somers memset(&msg, '\0', sizeof msg); 14702cb305afSBrian Somers msg.msg_name = NULL; 14712cb305afSBrian Somers msg.msg_namelen = 0; 147296c9bb21SBrian Somers msg.msg_iov = iov; 1473cbee9754SBrian Somers msg.msg_iovlen = 1; /* Only send the version at the first pass */ 147496c9bb21SBrian Somers msg.msg_control = cmsgbuf; 147596c9bb21SBrian Somers msg.msg_controllen = sizeof cmsgbuf; 147696c9bb21SBrian Somers 1477209dc102SBrian Somers log_Printf(LogDEBUG, "Expecting %u scatter/gather bytes\n", 1478209dc102SBrian Somers (unsigned)iov[0].iov_len); 14792cb305afSBrian Somers 1480cbee9754SBrian Somers if ((got = recvmsg(s, &msg, MSG_WAITALL)) != iov[0].iov_len) { 1481cbee9754SBrian Somers if (got == -1) 148296c9bb21SBrian Somers log_Printf(LogERROR, "Failed recvmsg: %s\n", strerror(errno)); 148396c9bb21SBrian Somers else 1484209dc102SBrian Somers log_Printf(LogERROR, "Failed recvmsg: Got %d, not %u\n", 1485209dc102SBrian Somers got, (unsigned)iov[0].iov_len); 148696c9bb21SBrian Somers while (niov--) 148796c9bb21SBrian Somers free(iov[niov].iov_base); 148896c9bb21SBrian Somers return; 148996c9bb21SBrian Somers } 149096c9bb21SBrian Somers 14912cb305afSBrian Somers if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) { 14922cb305afSBrian Somers log_Printf(LogERROR, "Recvmsg: no descriptors received !\n"); 14932cb305afSBrian Somers while (niov--) 14942cb305afSBrian Somers free(iov[niov].iov_base); 14952cb305afSBrian Somers return; 149687c3786eSBrian Somers } 149787c3786eSBrian Somers 14982cb305afSBrian Somers fd = (int *)(cmsg + 1); 14992cb305afSBrian Somers nfd = (cmsg->cmsg_len - sizeof *cmsg) / sizeof(int); 15002cb305afSBrian Somers 15012cb305afSBrian Somers if (nfd < 2) { 15028e7bd08eSBrian Somers log_Printf(LogERROR, "Recvmsg: %d descriptor%s received (too few) !\n", 15032cb305afSBrian Somers nfd, nfd == 1 ? "" : "s"); 15042cb305afSBrian Somers while (nfd--) 15052cb305afSBrian Somers close(fd[nfd]); 1506ad5b0e8bSBrian Somers while (niov--) 1507ad5b0e8bSBrian Somers free(iov[niov].iov_base); 1508ad5b0e8bSBrian Somers return; 150954cd8e13SBrian Somers } 151054cd8e13SBrian Somers 151187c3786eSBrian Somers /* 1512cbee9754SBrian Somers * We've successfully received two or more open file descriptors 1513cbee9754SBrian Somers * through our socket, plus a version string. Make sure it's the 1514cbee9754SBrian Somers * correct version, and drop the connection if it's not. 151587c3786eSBrian Somers */ 151696c9bb21SBrian Somers if (strncmp(Version, iov[0].iov_base, iov[0].iov_len)) { 151796c9bb21SBrian Somers log_Printf(LogWARN, "Cannot receive datalink, incorrect version" 151896c9bb21SBrian Somers " (\"%.*s\", not \"%s\")\n", (int)iov[0].iov_len, 15197d81ddf5SBrian Somers (char *)iov[0].iov_base, Version); 15202cb305afSBrian Somers while (nfd--) 15212cb305afSBrian Somers close(fd[nfd]); 152296c9bb21SBrian Somers while (niov--) 152396c9bb21SBrian Somers free(iov[niov].iov_base); 152496c9bb21SBrian Somers return; 152596c9bb21SBrian Somers } 152696c9bb21SBrian Somers 1527cbee9754SBrian Somers /* 1528cbee9754SBrian Somers * Everything looks good. Send the other side our process id so that 1529cbee9754SBrian Somers * they can transfer lock ownership, and wait for them to send the 1530cbee9754SBrian Somers * actual link data. 1531cbee9754SBrian Somers */ 1532cbee9754SBrian Somers pid = getpid(); 1533cbee9754SBrian Somers if ((got = write(fd[1], &pid, sizeof pid)) != sizeof pid) { 1534cbee9754SBrian Somers if (got == -1) 1535cbee9754SBrian Somers log_Printf(LogERROR, "Failed write: %s\n", strerror(errno)); 1536cbee9754SBrian Somers else 1537cbee9754SBrian Somers log_Printf(LogERROR, "Failed write: Got %d, not %d\n", got, 1538cbee9754SBrian Somers (int)(sizeof pid)); 1539cbee9754SBrian Somers while (nfd--) 1540cbee9754SBrian Somers close(fd[nfd]); 1541cbee9754SBrian Somers while (niov--) 1542cbee9754SBrian Somers free(iov[niov].iov_base); 1543cbee9754SBrian Somers return; 1544cbee9754SBrian Somers } 1545cbee9754SBrian Somers 1546cbee9754SBrian Somers if ((got = readv(fd[1], iov + 1, niov - 1)) != expect) { 1547cbee9754SBrian Somers if (got == -1) 1548cbee9754SBrian Somers log_Printf(LogERROR, "Failed write: %s\n", strerror(errno)); 1549cbee9754SBrian Somers else 1550cbee9754SBrian Somers log_Printf(LogERROR, "Failed write: Got %d, not %d\n", got, expect); 1551cbee9754SBrian Somers while (nfd--) 1552cbee9754SBrian Somers close(fd[nfd]); 1553cbee9754SBrian Somers while (niov--) 1554cbee9754SBrian Somers free(iov[niov].iov_base); 1555cbee9754SBrian Somers return; 1556cbee9754SBrian Somers } 1557cbee9754SBrian Somers close(fd[1]); 1558cbee9754SBrian Somers 15592cb305afSBrian Somers onfd = nfd; /* We've got this many in our array */ 15608e7bd08eSBrian Somers nfd -= 2; /* Don't include p->fd and our reply descriptor */ 15612cb305afSBrian Somers niov = 1; /* Skip the version id */ 156287c3786eSBrian Somers dl = iov2datalink(bundle, iov, &niov, sizeof iov / sizeof *iov, fd[0], 15632cb305afSBrian Somers fd + 2, &nfd); 1564b7c5748eSBrian Somers if (dl) { 15652cb305afSBrian Somers 156687c3786eSBrian Somers if (nfd) { 156787c3786eSBrian Somers log_Printf(LogERROR, "bundle_ReceiveDatalink: Failed to handle %d " 15682cb305afSBrian Somers "auxiliary file descriptors (%d remain)\n", onfd, nfd); 156987c3786eSBrian Somers datalink_Destroy(dl); 157087c3786eSBrian Somers while (nfd--) 157187c3786eSBrian Somers close(fd[onfd--]); 15722cb305afSBrian Somers close(fd[0]); 157387c3786eSBrian Somers } else { 157404eaa58cSBrian Somers bundle_DatalinkLinkin(bundle, dl); 1575b7c5748eSBrian Somers datalink_AuthOk(dl); 1576ab2de065SBrian Somers bundle_CalculateBandwidth(dl->bundle); 157787c3786eSBrian Somers } 157887c3786eSBrian Somers } else { 157987c3786eSBrian Somers while (nfd--) 158087c3786eSBrian Somers close(fd[onfd--]); 158187c3786eSBrian Somers close(fd[0]); 15822cb305afSBrian Somers close(fd[1]); 158387c3786eSBrian Somers } 158496c9bb21SBrian Somers 158596c9bb21SBrian Somers free(iov[0].iov_base); 15861fa665f5SBrian Somers } 15871fa665f5SBrian Somers 15881fa665f5SBrian Somers void 158996c9bb21SBrian Somers bundle_SendDatalink(struct datalink *dl, int s, struct sockaddr_un *sun) 15901fa665f5SBrian Somers { 15912cb305afSBrian Somers char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int) * SEND_MAXFD]; 15922cb305afSBrian Somers const char *constlock; 15932cb305afSBrian Somers char *lock; 159487c3786eSBrian Somers struct cmsghdr *cmsg; 159596c9bb21SBrian Somers struct msghdr msg; 159696c9bb21SBrian Somers struct iovec iov[SCATTER_SEGMENTS]; 15972cb305afSBrian Somers int niov, f, expect, newsid, fd[SEND_MAXFD], nfd, reply[2], got; 159885fd273aSBrian Somers pid_t newpid; 15996f384573SBrian Somers 1600dd7e2610SBrian Somers log_Printf(LogPHASE, "Transmitting datalink %s\n", dl->name); 16016f384573SBrian Somers 16022cb305afSBrian Somers /* Record the base device name for a lock transfer later */ 16032cb305afSBrian Somers constlock = physical_LockedDevice(dl->physical); 16042cb305afSBrian Somers if (constlock) { 16052cb305afSBrian Somers lock = alloca(strlen(constlock) + 1); 16062cb305afSBrian Somers strcpy(lock, constlock); 16072cb305afSBrian Somers } else 16082cb305afSBrian Somers lock = NULL; 16092cb305afSBrian Somers 161004eaa58cSBrian Somers bundle_LinkClosed(dl->bundle, dl); 16110f2f3eb3SBrian Somers bundle_DatalinkLinkout(dl->bundle, dl); 1612ea722969SBrian Somers 161396c9bb21SBrian Somers /* Build our scatter/gather array */ 161496c9bb21SBrian Somers iov[0].iov_len = strlen(Version) + 1; 161596c9bb21SBrian Somers iov[0].iov_base = strdup(Version); 161696c9bb21SBrian Somers niov = 1; 161787c3786eSBrian Somers nfd = 0; 16186f384573SBrian Somers 16192cb305afSBrian Somers fd[0] = datalink2iov(dl, iov, &niov, SCATTER_SEGMENTS, fd + 2, &nfd); 16206f384573SBrian Somers 16212cb305afSBrian Somers if (fd[0] != -1 && socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, reply) != -1) { 16222cb305afSBrian Somers /* 16232cb305afSBrian Somers * fd[1] is used to get the peer process id back, then to confirm that 16242cb305afSBrian Somers * we've transferred any device locks to that process id. 16252cb305afSBrian Somers */ 16262cb305afSBrian Somers fd[1] = reply[1]; 162787c3786eSBrian Somers 16282cb305afSBrian Somers nfd += 2; /* Include fd[0] and fd[1] */ 162996c9bb21SBrian Somers memset(&msg, '\0', sizeof msg); 163054cd8e13SBrian Somers 16312cb305afSBrian Somers msg.msg_name = NULL; 16322cb305afSBrian Somers msg.msg_namelen = 0; 1633cbee9754SBrian Somers /* 1634cbee9754SBrian Somers * Only send the version to start... We used to send the whole lot, but 1635cbee9754SBrian Somers * this caused problems with our RECVBUF size as a single link is about 1636cbee9754SBrian Somers * 22k ! This way, we should bump into no limits. 1637cbee9754SBrian Somers */ 1638cbee9754SBrian Somers msg.msg_iovlen = 1; 163996c9bb21SBrian Somers msg.msg_iov = iov; 16402cb305afSBrian Somers msg.msg_control = cmsgbuf; 16412cb305afSBrian Somers msg.msg_controllen = sizeof *cmsg + sizeof(int) * nfd; 16422cb305afSBrian Somers msg.msg_flags = 0; 164354cd8e13SBrian Somers 16442cb305afSBrian Somers cmsg = (struct cmsghdr *)cmsgbuf; 16452cb305afSBrian Somers cmsg->cmsg_len = msg.msg_controllen; 164654cd8e13SBrian Somers cmsg->cmsg_level = SOL_SOCKET; 164754cd8e13SBrian Somers cmsg->cmsg_type = SCM_RIGHTS; 164887c3786eSBrian Somers 16492cb305afSBrian Somers for (f = 0; f < nfd; f++) 16502cb305afSBrian Somers *((int *)(cmsg + 1) + f) = fd[f]; 165147723d29SBrian Somers 1652cbee9754SBrian Somers for (f = 1, expect = 0; f < niov; f++) 165396c9bb21SBrian Somers expect += iov[f].iov_len; 165447723d29SBrian Somers 1655cbee9754SBrian Somers if (setsockopt(reply[0], SOL_SOCKET, SO_SNDBUF, &expect, sizeof(int)) == -1) 1656cbee9754SBrian Somers log_Printf(LogERROR, "setsockopt(SO_RCVBUF, %d): %s\n", expect, 1657cbee9754SBrian Somers strerror(errno)); 1658cbee9754SBrian Somers if (setsockopt(reply[1], SOL_SOCKET, SO_RCVBUF, &expect, sizeof(int)) == -1) 1659cbee9754SBrian Somers log_Printf(LogERROR, "setsockopt(SO_RCVBUF, %d): %s\n", expect, 1660cbee9754SBrian Somers strerror(errno)); 1661cbee9754SBrian Somers 1662209dc102SBrian Somers log_Printf(LogDEBUG, "Sending %d descriptor%s and %u bytes in scatter" 1663209dc102SBrian Somers "/gather array\n", nfd, nfd == 1 ? "" : "s", 1664209dc102SBrian Somers (unsigned)iov[0].iov_len); 166547723d29SBrian Somers 16662cb305afSBrian Somers if ((got = sendmsg(s, &msg, 0)) == -1) 16672cb305afSBrian Somers log_Printf(LogERROR, "Failed sendmsg: %s: %s\n", 16682cb305afSBrian Somers sun->sun_path, strerror(errno)); 1669cbee9754SBrian Somers else if (got != iov[0].iov_len) 1670209dc102SBrian Somers log_Printf(LogERROR, "%s: Failed initial sendmsg: Only sent %d of %u\n", 1671209dc102SBrian Somers sun->sun_path, got, (unsigned)iov[0].iov_len); 16722cb305afSBrian Somers else { 16738e7bd08eSBrian Somers /* We must get the ACK before closing the descriptor ! */ 16742cb305afSBrian Somers int res; 16752cb305afSBrian Somers 1676cbee9754SBrian Somers if ((got = read(reply[0], &newpid, sizeof newpid)) == sizeof newpid) { 1677cbee9754SBrian Somers log_Printf(LogDEBUG, "Received confirmation from pid %d\n", 1678cbee9754SBrian Somers (int)newpid); 16792cb305afSBrian Somers if (lock && (res = ID0uu_lock_txfr(lock, newpid)) != UU_LOCK_OK) 1680b42135deSBrian Somers log_Printf(LogERROR, "uu_lock_txfr: %s\n", uu_lockerr(res)); 16812cb305afSBrian Somers 1682cbee9754SBrian Somers log_Printf(LogDEBUG, "Transmitting link (%d bytes)\n", expect); 1683cbee9754SBrian Somers if ((got = writev(reply[0], iov + 1, niov - 1)) != expect) { 1684cbee9754SBrian Somers if (got == -1) 1685cbee9754SBrian Somers log_Printf(LogERROR, "%s: Failed writev: %s\n", 1686cbee9754SBrian Somers sun->sun_path, strerror(errno)); 1687cbee9754SBrian Somers else 1688cbee9754SBrian Somers log_Printf(LogERROR, "%s: Failed writev: Wrote %d of %d\n", 1689cbee9754SBrian Somers sun->sun_path, got, expect); 1690cbee9754SBrian Somers } 1691cbee9754SBrian Somers } else if (got == -1) 1692cbee9754SBrian Somers log_Printf(LogERROR, "%s: Failed socketpair read: %s\n", 1693cbee9754SBrian Somers sun->sun_path, strerror(errno)); 1694cbee9754SBrian Somers else 1695cbee9754SBrian Somers log_Printf(LogERROR, "%s: Failed socketpair read: Got %d of %d\n", 1696cbee9754SBrian Somers sun->sun_path, got, (int)(sizeof newpid)); 16972cb305afSBrian Somers } 16982cb305afSBrian Somers 16992cb305afSBrian Somers close(reply[0]); 17002cb305afSBrian Somers close(reply[1]); 170154cd8e13SBrian Somers 1702ac685e31SBrian Somers newsid = Enabled(dl->bundle, OPT_KEEPSESSION) || 170387c3786eSBrian Somers tcgetpgrp(fd[0]) == getpgrp(); 170487c3786eSBrian Somers while (nfd) 170587c3786eSBrian Somers close(fd[--nfd]); 17061384bd27SBrian Somers if (newsid) 17072cb305afSBrian Somers bundle_setsid(dl->bundle, got != -1); 170847723d29SBrian Somers } 170985fd273aSBrian Somers close(s); 171096c9bb21SBrian Somers 171196c9bb21SBrian Somers while (niov--) 171296c9bb21SBrian Somers free(iov[niov].iov_base); 17131fa665f5SBrian Somers } 1714dd0645c5SBrian Somers 1715dd0645c5SBrian Somers int 171658d55334SBrian Somers bundle_RenameDatalink(struct bundle *bundle, struct datalink *ndl, 171758d55334SBrian Somers const char *name) 171858d55334SBrian Somers { 171958d55334SBrian Somers struct datalink *dl; 172058d55334SBrian Somers 172158d55334SBrian Somers if (!strcasecmp(ndl->name, name)) 172258d55334SBrian Somers return 1; 172358d55334SBrian Somers 172458d55334SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 172558d55334SBrian Somers if (!strcasecmp(dl->name, name)) 172658d55334SBrian Somers return 0; 172758d55334SBrian Somers 172858d55334SBrian Somers datalink_Rename(ndl, name); 172958d55334SBrian Somers return 1; 173058d55334SBrian Somers } 173158d55334SBrian Somers 173258d55334SBrian Somers int 1733dd0645c5SBrian Somers bundle_SetMode(struct bundle *bundle, struct datalink *dl, int mode) 1734dd0645c5SBrian Somers { 1735dd0645c5SBrian Somers int omode; 1736dd0645c5SBrian Somers 1737dd0645c5SBrian Somers omode = dl->physical->type; 1738dd0645c5SBrian Somers if (omode == mode) 1739dd0645c5SBrian Somers return 1; 1740dd0645c5SBrian Somers 1741ff0f9439SBrian Somers if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO)) 1742ff0f9439SBrian Somers /* First auto link */ 1743dd0645c5SBrian Somers if (bundle->ncp.ipcp.peer_ip.s_addr == INADDR_ANY) { 1744ff0f9439SBrian Somers log_Printf(LogWARN, "You must `set ifaddr' or `open' before" 1745ff0f9439SBrian Somers " changing mode to %s\n", mode2Nam(mode)); 1746dd0645c5SBrian Somers return 0; 1747dd0645c5SBrian Somers } 1748dd0645c5SBrian Somers 1749dd0645c5SBrian Somers if (!datalink_SetMode(dl, mode)) 1750dd0645c5SBrian Somers return 0; 1751dd0645c5SBrian Somers 1752ff0f9439SBrian Somers if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO) && 1753ff0f9439SBrian Somers bundle->phase != PHASE_NETWORK) 1754ff0f9439SBrian Somers /* First auto link, we need an interface */ 1755dd0645c5SBrian Somers ipcp_InterfaceUp(&bundle->ncp.ipcp); 1756dd0645c5SBrian Somers 1757ab2de065SBrian Somers /* Regenerate phys_type and adjust idle timer */ 175804eaa58cSBrian Somers bundle_LinksRemoved(bundle); 1759dd0645c5SBrian Somers 1760dd0645c5SBrian Somers return 1; 1761dd0645c5SBrian Somers } 17621384bd27SBrian Somers 17631384bd27SBrian Somers void 17641384bd27SBrian Somers bundle_setsid(struct bundle *bundle, int holdsession) 17651384bd27SBrian Somers { 17661384bd27SBrian Somers /* 17671384bd27SBrian Somers * Lose the current session. This means getting rid of our pid 17681384bd27SBrian Somers * too so that the tty device will really go away, and any getty 17691384bd27SBrian Somers * etc will be allowed to restart. 17701384bd27SBrian Somers */ 17711384bd27SBrian Somers pid_t pid, orig; 17721384bd27SBrian Somers int fds[2]; 17731384bd27SBrian Somers char done; 17741384bd27SBrian Somers struct datalink *dl; 17751384bd27SBrian Somers 1776e62ce959SBrian Somers if (!holdsession && bundle_IsDead(bundle)) { 1777e62ce959SBrian Somers /* 1778e62ce959SBrian Somers * No need to lose our session after all... we're going away anyway 1779e62ce959SBrian Somers * 1780e62ce959SBrian Somers * We should really stop the timer and pause if holdsession is set and 1781e62ce959SBrian Somers * the bundle's dead, but that leaves other resources lying about :-( 1782e62ce959SBrian Somers */ 1783e62ce959SBrian Somers return; 1784e62ce959SBrian Somers } 1785e62ce959SBrian Somers 17861384bd27SBrian Somers orig = getpid(); 17871384bd27SBrian Somers if (pipe(fds) == -1) { 17881384bd27SBrian Somers log_Printf(LogERROR, "pipe: %s\n", strerror(errno)); 17891384bd27SBrian Somers return; 17901384bd27SBrian Somers } 17911384bd27SBrian Somers switch ((pid = fork())) { 17921384bd27SBrian Somers case -1: 17931384bd27SBrian Somers log_Printf(LogERROR, "fork: %s\n", strerror(errno)); 17941384bd27SBrian Somers close(fds[0]); 17951384bd27SBrian Somers close(fds[1]); 17961384bd27SBrian Somers return; 17971384bd27SBrian Somers case 0: 17981384bd27SBrian Somers close(fds[1]); 17994be0e57dSBrian Somers read(fds[0], &done, 1); /* uu_locks are mine ! */ 18004be0e57dSBrian Somers close(fds[0]); 18011384bd27SBrian Somers if (pipe(fds) == -1) { 18021384bd27SBrian Somers log_Printf(LogERROR, "pipe(2): %s\n", strerror(errno)); 18031384bd27SBrian Somers return; 18041384bd27SBrian Somers } 18051384bd27SBrian Somers switch ((pid = fork())) { 18061384bd27SBrian Somers case -1: 1807a33b2ef7SBrian Somers log_Printf(LogERROR, "fork(2): %s\n", strerror(errno)); 18081384bd27SBrian Somers close(fds[0]); 18091384bd27SBrian Somers close(fds[1]); 18101384bd27SBrian Somers return; 18111384bd27SBrian Somers case 0: 18121384bd27SBrian Somers close(fds[1]); 18134be0e57dSBrian Somers bundle_LockTun(bundle); /* update pid */ 18144be0e57dSBrian Somers read(fds[0], &done, 1); /* uu_locks are mine ! */ 18154be0e57dSBrian Somers close(fds[0]); 18161384bd27SBrian Somers setsid(); 181706b47306SBrian Somers bundle_ChangedPID(bundle); 1818b42135deSBrian Somers log_Printf(LogDEBUG, "%d -> %d: %s session control\n", 18191384bd27SBrian Somers (int)orig, (int)getpid(), 18201384bd27SBrian Somers holdsession ? "Passed" : "Dropped"); 18217e778f13SBrian Somers timer_InitService(0); /* Start the Timer Service */ 18221384bd27SBrian Somers break; 18231384bd27SBrian Somers default: 18244be0e57dSBrian Somers close(fds[0]); 18255d9e6103SBrian Somers /* Give away all our physical locks (to the final process) */ 18261384bd27SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 18271384bd27SBrian Somers if (dl->state != DATALINK_CLOSED) 18285d9e6103SBrian Somers physical_ChangedPid(dl->physical, pid); 18294be0e57dSBrian Somers write(fds[1], "!", 1); /* done */ 18304be0e57dSBrian Somers close(fds[1]); 18312cb305afSBrian Somers _exit(0); 18321384bd27SBrian Somers break; 18331384bd27SBrian Somers } 18341384bd27SBrian Somers break; 18351384bd27SBrian Somers default: 18364be0e57dSBrian Somers close(fds[0]); 18375d9e6103SBrian Somers /* Give away all our physical locks (to the intermediate process) */ 18381384bd27SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 18391384bd27SBrian Somers if (dl->state != DATALINK_CLOSED) 18405d9e6103SBrian Somers physical_ChangedPid(dl->physical, pid); 18414be0e57dSBrian Somers write(fds[1], "!", 1); /* done */ 18424be0e57dSBrian Somers close(fds[1]); 18431384bd27SBrian Somers if (holdsession) { 18441384bd27SBrian Somers int fd, status; 18451384bd27SBrian Somers 18461384bd27SBrian Somers timer_TermService(); 18471384bd27SBrian Somers signal(SIGPIPE, SIG_DFL); 18481384bd27SBrian Somers signal(SIGALRM, SIG_DFL); 18491384bd27SBrian Somers signal(SIGHUP, SIG_DFL); 18501384bd27SBrian Somers signal(SIGTERM, SIG_DFL); 18511384bd27SBrian Somers signal(SIGINT, SIG_DFL); 18521384bd27SBrian Somers signal(SIGQUIT, SIG_DFL); 18531384bd27SBrian Somers for (fd = getdtablesize(); fd >= 0; fd--) 18541384bd27SBrian Somers close(fd); 18551384bd27SBrian Somers /* 18561384bd27SBrian Somers * Reap the intermediate process. As we're not exiting but the 18571384bd27SBrian Somers * intermediate is, we don't want it to become defunct. 18581384bd27SBrian Somers */ 18591384bd27SBrian Somers waitpid(pid, &status, 0); 18608e7b8599SBrian Somers /* Tweak our process arguments.... */ 1861ebe96675SBrian Somers SetTitle("session owner"); 186268602c3eSBrian Somers #ifndef NOSUID 1863a19a5c02SBrian Somers setuid(ID0realuid()); 186468602c3eSBrian Somers #endif 18651384bd27SBrian Somers /* 18661384bd27SBrian Somers * Hang around for a HUP. This should happen as soon as the 18678e7bd08eSBrian Somers * ppp that we passed our ctty descriptor to closes it. 18688e7bd08eSBrian Somers * NOTE: If this process dies, the passed descriptor becomes 18691384bd27SBrian Somers * invalid and will give a select() error by setting one 18701384bd27SBrian Somers * of the error fds, aborting the other ppp. We don't 18711384bd27SBrian Somers * want that to happen ! 18721384bd27SBrian Somers */ 18731384bd27SBrian Somers pause(); 18741384bd27SBrian Somers } 18752cb305afSBrian Somers _exit(0); 18761384bd27SBrian Somers break; 18771384bd27SBrian Somers } 18781384bd27SBrian Somers } 18799b5f8ffdSBrian Somers 18809b5f8ffdSBrian Somers int 18819b5f8ffdSBrian Somers bundle_HighestState(struct bundle *bundle) 18829b5f8ffdSBrian Somers { 18839b5f8ffdSBrian Somers struct datalink *dl; 18849b5f8ffdSBrian Somers int result = DATALINK_CLOSED; 18859b5f8ffdSBrian Somers 18869b5f8ffdSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 18879b5f8ffdSBrian Somers if (result < dl->state) 18889b5f8ffdSBrian Somers result = dl->state; 18899b5f8ffdSBrian Somers 18909b5f8ffdSBrian Somers return result; 18919b5f8ffdSBrian Somers } 1892991c2a7bSBrian Somers 1893991c2a7bSBrian Somers int 1894991c2a7bSBrian Somers bundle_Exception(struct bundle *bundle, int fd) 1895991c2a7bSBrian Somers { 1896991c2a7bSBrian Somers struct datalink *dl; 1897991c2a7bSBrian Somers 1898991c2a7bSBrian Somers for (dl = bundle->links; dl; dl = dl->next) 1899991c2a7bSBrian Somers if (dl->physical->fd == fd) { 1900991c2a7bSBrian Somers datalink_Down(dl, CLOSE_NORMAL); 1901991c2a7bSBrian Somers return 1; 1902991c2a7bSBrian Somers } 1903991c2a7bSBrian Somers 1904991c2a7bSBrian Somers return 0; 1905991c2a7bSBrian Somers } 19061d1fc017SBrian Somers 19071d1fc017SBrian Somers void 19081d1fc017SBrian Somers bundle_AdjustFilters(struct bundle *bundle, struct in_addr *my_ip, 19091d1fc017SBrian Somers struct in_addr *peer_ip) 19101d1fc017SBrian Somers { 1911d568d6c4SBrian Somers filter_AdjustAddr(&bundle->filter.in, my_ip, peer_ip, NULL); 1912d568d6c4SBrian Somers filter_AdjustAddr(&bundle->filter.out, my_ip, peer_ip, NULL); 1913d568d6c4SBrian Somers filter_AdjustAddr(&bundle->filter.dial, my_ip, peer_ip, NULL); 1914d568d6c4SBrian Somers filter_AdjustAddr(&bundle->filter.alive, my_ip, peer_ip, NULL); 1915d568d6c4SBrian Somers } 1916d568d6c4SBrian Somers 1917d568d6c4SBrian Somers void 1918d568d6c4SBrian Somers bundle_AdjustDNS(struct bundle *bundle, struct in_addr dns[2]) 1919d568d6c4SBrian Somers { 1920d568d6c4SBrian Somers filter_AdjustAddr(&bundle->filter.in, NULL, NULL, dns); 1921d568d6c4SBrian Somers filter_AdjustAddr(&bundle->filter.out, NULL, NULL, dns); 1922d568d6c4SBrian Somers filter_AdjustAddr(&bundle->filter.dial, NULL, NULL, dns); 1923d568d6c4SBrian Somers filter_AdjustAddr(&bundle->filter.alive, NULL, NULL, dns); 19241d1fc017SBrian Somers } 1925ab2de065SBrian Somers 1926ab2de065SBrian Somers void 1927ab2de065SBrian Somers bundle_CalculateBandwidth(struct bundle *bundle) 1928ab2de065SBrian Somers { 1929ab2de065SBrian Somers struct datalink *dl; 193094d7be52SBrian Somers int sp; 1931ab2de065SBrian Somers 1932ab2de065SBrian Somers bundle->bandwidth = 0; 193394d7be52SBrian Somers bundle->mtu = 0; 1934ab2de065SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 1935ab2de065SBrian Somers if (dl->state == DATALINK_OPEN) { 1936ab2de065SBrian Somers if ((sp = dl->mp.bandwidth) == 0 && 1937ab2de065SBrian Somers (sp = physical_GetSpeed(dl->physical)) == 0) 1938ab2de065SBrian Somers log_Printf(LogDEBUG, "%s: %s: Cannot determine bandwidth\n", 1939ab2de065SBrian Somers dl->name, dl->physical->name.full); 1940ab2de065SBrian Somers else 1941ab2de065SBrian Somers bundle->bandwidth += sp; 1942ab2de065SBrian Somers if (!bundle->ncp.mp.active) { 194394d7be52SBrian Somers bundle->mtu = dl->physical->link.lcp.his_mru; 1944ab2de065SBrian Somers break; 1945ab2de065SBrian Somers } 1946ab2de065SBrian Somers } 1947ab2de065SBrian Somers 1948ab2de065SBrian Somers if(bundle->bandwidth == 0) 1949ab2de065SBrian Somers bundle->bandwidth = 115200; /* Shrug */ 1950ab2de065SBrian Somers 1951ab2de065SBrian Somers if (bundle->ncp.mp.active) 195294d7be52SBrian Somers bundle->mtu = bundle->ncp.mp.peer_mrru; 195394d7be52SBrian Somers else if (!bundle->mtu) 195494d7be52SBrian Somers bundle->mtu = 1500; 1955ab2de065SBrian Somers 1956ab2de065SBrian Somers #ifndef NORADIUS 195794d7be52SBrian Somers if (bundle->radius.valid && bundle->radius.mtu && 195894d7be52SBrian Somers bundle->radius.mtu < bundle->mtu) { 1959ab2de065SBrian Somers log_Printf(LogLCP, "Reducing MTU to radius value %lu\n", 1960ab2de065SBrian Somers bundle->radius.mtu); 196194d7be52SBrian Somers bundle->mtu = bundle->radius.mtu; 1962ab2de065SBrian Somers } 1963ab2de065SBrian Somers #endif 1964ab2de065SBrian Somers 196594d7be52SBrian Somers tun_configure(bundle); 1966ab2de065SBrian Somers } 1967ab2de065SBrian Somers 1968ab2de065SBrian Somers void 1969ab2de065SBrian Somers bundle_AutoAdjust(struct bundle *bundle, int percent, int what) 1970ab2de065SBrian Somers { 1971ab2de065SBrian Somers struct datalink *dl, *choice, *otherlinkup; 1972ab2de065SBrian Somers 1973ab2de065SBrian Somers choice = otherlinkup = NULL; 1974ab2de065SBrian Somers for (dl = bundle->links; dl; dl = dl->next) 1975ab2de065SBrian Somers if (dl->physical->type == PHYS_AUTO) { 1976ab2de065SBrian Somers if (dl->state == DATALINK_OPEN) { 1977ab2de065SBrian Somers if (what == AUTO_DOWN) { 1978ab2de065SBrian Somers if (choice) 1979ab2de065SBrian Somers otherlinkup = choice; 1980ab2de065SBrian Somers choice = dl; 1981ab2de065SBrian Somers } 1982ab2de065SBrian Somers } else if (dl->state == DATALINK_CLOSED) { 1983ab2de065SBrian Somers if (what == AUTO_UP) { 1984ab2de065SBrian Somers choice = dl; 1985ab2de065SBrian Somers break; 1986ab2de065SBrian Somers } 1987ab2de065SBrian Somers } else { 1988ab2de065SBrian Somers /* An auto link in an intermediate state - forget it for the moment */ 1989ab2de065SBrian Somers choice = NULL; 1990ab2de065SBrian Somers break; 1991ab2de065SBrian Somers } 1992ab2de065SBrian Somers } else if (dl->state == DATALINK_OPEN && what == AUTO_DOWN) 1993ab2de065SBrian Somers otherlinkup = dl; 1994ab2de065SBrian Somers 1995ab2de065SBrian Somers if (choice) { 1996ab2de065SBrian Somers if (what == AUTO_UP) { 1997ab2de065SBrian Somers log_Printf(LogPHASE, "%d%% saturation -> Opening link ``%s''\n", 1998ab2de065SBrian Somers percent, choice->name); 1999ab2de065SBrian Somers datalink_Up(choice, 1, 1); 2000a339e644SBrian Somers mp_CheckAutoloadTimer(&bundle->ncp.mp); 2001ab2de065SBrian Somers } else if (otherlinkup) { /* Only bring the second-last link down */ 2002ab2de065SBrian Somers log_Printf(LogPHASE, "%d%% saturation -> Closing link ``%s''\n", 2003ab2de065SBrian Somers percent, choice->name); 2004d2f5232dSBrian Somers datalink_Close(choice, CLOSE_STAYDOWN); 2005a339e644SBrian Somers mp_CheckAutoloadTimer(&bundle->ncp.mp); 2006ab2de065SBrian Somers } 2007ab2de065SBrian Somers } 2008ab2de065SBrian Somers } 2009ab2de065SBrian Somers 2010ab2de065SBrian Somers int 2011ab2de065SBrian Somers bundle_WantAutoloadTimer(struct bundle *bundle) 2012ab2de065SBrian Somers { 2013ab2de065SBrian Somers struct datalink *dl; 2014ab2de065SBrian Somers int autolink, opened; 2015ab2de065SBrian Somers 2016ab2de065SBrian Somers if (bundle->phase == PHASE_NETWORK) { 2017ab2de065SBrian Somers for (autolink = opened = 0, dl = bundle->links; dl; dl = dl->next) 2018ab2de065SBrian Somers if (dl->physical->type == PHYS_AUTO) { 2019ab2de065SBrian Somers if (++autolink == 2 || (autolink == 1 && opened)) 2020ab2de065SBrian Somers /* Two auto links or one auto and one open in NETWORK phase */ 2021ab2de065SBrian Somers return 1; 2022ab2de065SBrian Somers } else if (dl->state == DATALINK_OPEN) { 2023ab2de065SBrian Somers opened++; 2024ab2de065SBrian Somers if (autolink) 2025ab2de065SBrian Somers /* One auto and one open link in NETWORK phase */ 2026ab2de065SBrian Somers return 1; 2027ab2de065SBrian Somers } 2028ab2de065SBrian Somers } 2029ab2de065SBrian Somers 2030ab2de065SBrian Somers return 0; 2031ab2de065SBrian Somers } 203206b47306SBrian Somers 203306b47306SBrian Somers void 203406b47306SBrian Somers bundle_ChangedPID(struct bundle *bundle) 203506b47306SBrian Somers { 203606b47306SBrian Somers #ifdef TUNSIFPID 203706b47306SBrian Somers ioctl(bundle->dev.fd, TUNSIFPID, 0); 203806b47306SBrian Somers #endif 203906b47306SBrian Somers } 2040