xref: /freebsd/usr.sbin/ppp/bundle.c (revision 6301d506fb03264736d864d191614a2c5a35efc1)
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 <net/route.h>
35eaa4df37SBrian Somers #include <netinet/in_systm.h>
36eaa4df37SBrian Somers #include <netinet/ip.h>
371fa665f5SBrian Somers #include <sys/un.h>
387a6f8720SBrian Somers 
397a6f8720SBrian Somers #include <errno.h>
407a6f8720SBrian Somers #include <fcntl.h>
41cf0a3940SBrian Somers #ifdef __OpenBSD__
42cf0a3940SBrian Somers #include <util.h>
43cf0a3940SBrian Somers #else
44cf0a3940SBrian Somers #include <libutil.h>
45cf0a3940SBrian Somers #endif
4647723d29SBrian Somers #include <paths.h>
477a6f8720SBrian Somers #include <stdio.h>
486f384573SBrian Somers #include <stdlib.h>
497a6f8720SBrian Somers #include <string.h>
5096c9bb21SBrian Somers #include <sys/uio.h>
5154cd8e13SBrian Somers #include <sys/wait.h>
52fc3034caSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
5368602c3eSBrian Somers #ifdef NOSUID
5468602c3eSBrian Somers #include <sys/linker.h>
5568602c3eSBrian Somers #endif
56fdb4bb1bSBrian Somers #include <sys/module.h>
57fc3034caSBrian Somers #endif
587a6f8720SBrian Somers #include <termios.h>
597a6f8720SBrian Somers #include <unistd.h>
607a6f8720SBrian Somers 
615d9e6103SBrian Somers #include "layer.h"
62c9e11a11SBrian Somers #include "defs.h"
637a6f8720SBrian Somers #include "command.h"
647a6f8720SBrian Somers #include "mbuf.h"
657a6f8720SBrian Somers #include "log.h"
667a6f8720SBrian Somers #include "id.h"
677a6f8720SBrian Somers #include "timer.h"
687a6f8720SBrian Somers #include "fsm.h"
697a6f8720SBrian Somers #include "iplist.h"
70879ed6faSBrian Somers #include "lqr.h"
71455aabc3SBrian Somers #include "hdlc.h"
727a6f8720SBrian Somers #include "throughput.h"
73eaa4df37SBrian Somers #include "slcompress.h"
747a6f8720SBrian Somers #include "ipcp.h"
755ca5389aSBrian Somers #include "filter.h"
762f786681SBrian Somers #include "descriptor.h"
777a6f8720SBrian Somers #include "route.h"
787a6f8720SBrian Somers #include "lcp.h"
797a6f8720SBrian Somers #include "ccp.h"
803b0f8d2eSBrian Somers #include "link.h"
813b0f8d2eSBrian Somers #include "mp.h"
82972a1bcfSBrian Somers #ifndef NORADIUS
83972a1bcfSBrian Somers #include "radius.h"
84972a1bcfSBrian Somers #endif
853b0f8d2eSBrian Somers #include "bundle.h"
86455aabc3SBrian Somers #include "async.h"
87455aabc3SBrian Somers #include "physical.h"
88455aabc3SBrian Somers #include "auth.h"
895d9e6103SBrian Somers #include "proto.h"
90455aabc3SBrian Somers #include "chap.h"
91455aabc3SBrian Somers #include "tun.h"
9285b542cfSBrian Somers #include "prompt.h"
933006ec67SBrian Somers #include "chat.h"
9492b09558SBrian Somers #include "cbcp.h"
953006ec67SBrian Somers #include "datalink.h"
963006ec67SBrian Somers #include "ip.h"
978fa6ebe4SBrian Somers #include "iface.h"
9874457d3dSBrian Somers #include "server.h"
9903a2501aSBrian Somers #ifdef HAVE_DES
100019d32bfSBrian Somers #include "mppe.h"
10103a2501aSBrian Somers #endif
1027a6f8720SBrian Somers 
10391cbd2eeSBrian Somers #define SCATTER_SEGMENTS 7  /* version, datalink, name, physical,
10491cbd2eeSBrian Somers                                throughput, throughput, device       */
10587c3786eSBrian Somers 
1062cb305afSBrian Somers #define SEND_MAXFD 3        /* Max file descriptors passed through
10787c3786eSBrian Somers                                the local domain socket              */
10896c9bb21SBrian Somers 
10904eaa58cSBrian Somers static int bundle_RemainingIdleTime(struct bundle *);
11004eaa58cSBrian Somers 
111182c898aSBrian Somers static const char * const PhaseNames[] = {
112455aabc3SBrian Somers   "Dead", "Establish", "Authenticate", "Network", "Terminate"
113455aabc3SBrian Somers };
114455aabc3SBrian Somers 
115455aabc3SBrian Somers const char *
116455aabc3SBrian Somers bundle_PhaseName(struct bundle *bundle)
1177a6f8720SBrian Somers {
118455aabc3SBrian Somers   return bundle->phase <= PHASE_TERMINATE ?
119455aabc3SBrian Somers     PhaseNames[bundle->phase] : "unknown";
1207a6f8720SBrian Somers }
1217a6f8720SBrian Somers 
122455aabc3SBrian Somers void
1235563ebdeSBrian Somers bundle_NewPhase(struct bundle *bundle, u_int new)
124455aabc3SBrian Somers {
125aef795ccSBrian Somers   if (new == bundle->phase)
126aef795ccSBrian Somers     return;
127aef795ccSBrian Somers 
128e2ebb036SBrian Somers   if (new <= PHASE_TERMINATE)
129dd7e2610SBrian Somers     log_Printf(LogPHASE, "bundle: %s\n", PhaseNames[new]);
1307a6f8720SBrian Somers 
131455aabc3SBrian Somers   switch (new) {
132455aabc3SBrian Somers   case PHASE_DEAD:
133455aabc3SBrian Somers     bundle->phase = new;
13464602637SBrian Somers #ifdef HAVE_DES
135019d32bfSBrian Somers     MPPE_MasterKeyValid = 0;
13664602637SBrian Somers #endif
137019d32bfSBrian Somers     log_DisplayPrompts();
138455aabc3SBrian Somers     break;
139455aabc3SBrian Somers 
140455aabc3SBrian Somers   case PHASE_ESTABLISH:
141455aabc3SBrian Somers     bundle->phase = new;
142455aabc3SBrian Somers     break;
143455aabc3SBrian Somers 
144455aabc3SBrian Somers   case PHASE_AUTHENTICATE:
145455aabc3SBrian Somers     bundle->phase = new;
1460f2f3eb3SBrian Somers     log_DisplayPrompts();
147455aabc3SBrian Somers     break;
148455aabc3SBrian Somers 
149455aabc3SBrian Somers   case PHASE_NETWORK:
150dd7e2610SBrian Somers     fsm_Up(&bundle->ncp.ipcp.fsm);
151dd7e2610SBrian Somers     fsm_Open(&bundle->ncp.ipcp.fsm);
152673903ecSBrian Somers     bundle->phase = new;
1530f2f3eb3SBrian Somers     log_DisplayPrompts();
154673903ecSBrian Somers     break;
155455aabc3SBrian Somers 
156455aabc3SBrian Somers   case PHASE_TERMINATE:
157455aabc3SBrian Somers     bundle->phase = new;
158673903ecSBrian Somers     mp_Down(&bundle->ncp.mp);
1590f2f3eb3SBrian Somers     log_DisplayPrompts();
160455aabc3SBrian Somers     break;
1617a6f8720SBrian Somers   }
1627a6f8720SBrian Somers }
1637a6f8720SBrian Somers 
1646d666775SBrian Somers static void
1656d666775SBrian Somers bundle_LayerStart(void *v, struct fsm *fp)
1667a6f8720SBrian Somers {
1673006ec67SBrian Somers   /* The given FSM is about to start up ! */
1687a6f8720SBrian Somers }
1697a6f8720SBrian Somers 
1705cf4388bSBrian Somers 
171b42135deSBrian Somers void
1725cf4388bSBrian Somers bundle_Notify(struct bundle *bundle, char c)
1735cf4388bSBrian Somers {
1745cf4388bSBrian Somers   if (bundle->notify.fd != -1) {
175b42135deSBrian Somers     int ret;
176b42135deSBrian Somers 
177b42135deSBrian Somers     ret = write(bundle->notify.fd, &c, 1);
178b42135deSBrian Somers     if (c != EX_REDIAL && c != EX_RECONNECT) {
179b42135deSBrian Somers       if (ret == 1)
180b42135deSBrian Somers         log_Printf(LogCHAT, "Parent notified of %s\n",
1815a83ad1eSBrian Somers                    c == EX_NORMAL ? "success" : "failure");
1825cf4388bSBrian Somers       else
183b42135deSBrian Somers         log_Printf(LogERROR, "Failed to notify parent of success\n");
1845cf4388bSBrian Somers       close(bundle->notify.fd);
1855cf4388bSBrian Somers       bundle->notify.fd = -1;
186b42135deSBrian Somers     } else if (ret == 1)
187b42135deSBrian Somers       log_Printf(LogCHAT, "Parent notified of %s\n", ex_desc(c));
188b42135deSBrian Somers     else
189b42135deSBrian Somers       log_Printf(LogERROR, "Failed to notify parent of %s\n", ex_desc(c));
1905cf4388bSBrian Somers   }
1915cf4388bSBrian Somers }
1923b0f8d2eSBrian Somers 
1936d666775SBrian Somers static void
1946f8e9f0aSBrian Somers bundle_ClearQueues(void *v)
1956f8e9f0aSBrian Somers {
1966f8e9f0aSBrian Somers   struct bundle *bundle = (struct bundle *)v;
1976f8e9f0aSBrian Somers   struct datalink *dl;
1986f8e9f0aSBrian Somers 
1996f8e9f0aSBrian Somers   log_Printf(LogPHASE, "Clearing choked output queue\n");
2006f8e9f0aSBrian Somers   timer_Stop(&bundle->choked.timer);
2016f8e9f0aSBrian Somers 
2026f8e9f0aSBrian Somers   /*
2036f8e9f0aSBrian Somers    * Emergency time:
2046f8e9f0aSBrian Somers    *
2056f8e9f0aSBrian Somers    * We've had a full queue for PACKET_DEL_SECS seconds without being
2066f8e9f0aSBrian Somers    * able to get rid of any of the packets.  We've probably given up
2076f8e9f0aSBrian Somers    * on the redials at this point, and the queued data has almost
2086f8e9f0aSBrian Somers    * definitely been timed out by the layer above.  As this is preventing
2096f8e9f0aSBrian Somers    * us from reading the TUN_NAME device (we don't want to buffer stuff
2106f8e9f0aSBrian Somers    * indefinitely), we may as well nuke this data and start with a clean
2116f8e9f0aSBrian Somers    * slate !
2126f8e9f0aSBrian Somers    *
2136f8e9f0aSBrian Somers    * Unfortunately, this has the side effect of shafting any compression
2146f8e9f0aSBrian Somers    * dictionaries in use (causing the relevant RESET_REQ/RESET_ACK).
2156f8e9f0aSBrian Somers    */
2166f8e9f0aSBrian Somers 
2175a72b6edSBrian Somers   ip_DeleteQueue(&bundle->ncp.ipcp);
2186f8e9f0aSBrian Somers   mp_DeleteQueue(&bundle->ncp.mp);
2196f8e9f0aSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
2206f8e9f0aSBrian Somers     physical_DeleteQueue(dl->physical);
2216f8e9f0aSBrian Somers }
2226f8e9f0aSBrian Somers 
2236f8e9f0aSBrian Somers static void
224ff0f9439SBrian Somers bundle_LinkAdded(struct bundle *bundle, struct datalink *dl)
225ff0f9439SBrian Somers {
226ff0f9439SBrian Somers   bundle->phys_type.all |= dl->physical->type;
227ff0f9439SBrian Somers   if (dl->state == DATALINK_OPEN)
228ff0f9439SBrian Somers     bundle->phys_type.open |= dl->physical->type;
229ff0f9439SBrian Somers 
230ff0f9439SBrian Somers   if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL))
231ff0f9439SBrian Somers       != bundle->phys_type.open && bundle->idle.timer.state == TIMER_STOPPED)
232ff0f9439SBrian Somers     /* We may need to start our idle timer */
2330a4b6c5cSBrian Somers     bundle_StartIdleTimer(bundle, 0);
234ff0f9439SBrian Somers }
235ff0f9439SBrian Somers 
23692b09558SBrian Somers void
237ff0f9439SBrian Somers bundle_LinksRemoved(struct bundle *bundle)
238ff0f9439SBrian Somers {
239ff0f9439SBrian Somers   struct datalink *dl;
240ff0f9439SBrian Somers 
241ff0f9439SBrian Somers   bundle->phys_type.all = bundle->phys_type.open = 0;
242ff0f9439SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
243ff0f9439SBrian Somers     bundle_LinkAdded(bundle, dl);
244ff0f9439SBrian Somers 
245ab2de065SBrian Somers   bundle_CalculateBandwidth(bundle);
246ab2de065SBrian Somers   mp_CheckAutoloadTimer(&bundle->ncp.mp);
247ab2de065SBrian Somers 
248ff0f9439SBrian Somers   if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL))
249ff0f9439SBrian Somers       == bundle->phys_type.open)
250ff0f9439SBrian Somers     bundle_StopIdleTimer(bundle);
251ff0f9439SBrian Somers }
25204eaa58cSBrian Somers 
25304eaa58cSBrian Somers static void
2546f384573SBrian Somers bundle_LayerUp(void *v, struct fsm *fp)
2557a6f8720SBrian Somers {
2563006ec67SBrian Somers   /*
2573006ec67SBrian Somers    * The given fsm is now up
258ab2de065SBrian Somers    * If it's an LCP, adjust our phys_mode.open value and check the
259ab2de065SBrian Somers    * autoload timer.
260ab2de065SBrian Somers    * If it's the first NCP, calculate our bandwidth
261dade2407SBrian Somers    * If it's the first NCP, set our ``upat'' time
2623b0f8d2eSBrian Somers    * If it's the first NCP, start the idle timer.
263ab2de065SBrian Somers    * If it's an NCP, tell our -background parent to go away.
264ab2de065SBrian Somers    * If it's the first NCP, start the autoload timer
2653006ec67SBrian Somers    */
2666f384573SBrian Somers   struct bundle *bundle = (struct bundle *)v;
2676d666775SBrian Somers 
2685563ebdeSBrian Somers   if (fp->proto == PROTO_LCP) {
269ff0f9439SBrian Somers     struct physical *p = link2physical(fp->link);
270ff0f9439SBrian Somers 
271ff0f9439SBrian Somers     bundle_LinkAdded(bundle, p->dl);
272ab2de065SBrian Somers     mp_CheckAutoloadTimer(&bundle->ncp.mp);
2733b0f8d2eSBrian Somers   } else if (fp->proto == PROTO_IPCP) {
274ab2de065SBrian Somers     bundle_CalculateBandwidth(fp->bundle);
275dade2407SBrian Somers     time(&bundle->upat);
2760a4b6c5cSBrian Somers     bundle_StartIdleTimer(bundle, 0);
2775cf4388bSBrian Somers     bundle_Notify(bundle, EX_NORMAL);
278ab2de065SBrian Somers     mp_CheckAutoloadTimer(&fp->bundle->ncp.mp);
2796301d506SBrian Somers   } else if (fp->proto == PROTO_CCP)
2806301d506SBrian Somers     bundle_CalculateBandwidth(fp->bundle);	/* Against ccp_MTUOverhead */
281ab886ad0SBrian Somers }
2827a6f8720SBrian Somers 
2836d666775SBrian Somers static void
2846d666775SBrian Somers bundle_LayerDown(void *v, struct fsm *fp)
2856d666775SBrian Somers {
2866d666775SBrian Somers   /*
2876d666775SBrian Somers    * The given FSM has been told to come down.
288ab886ad0SBrian Somers    * If it's our last NCP, stop the idle timer.
289dade2407SBrian Somers    * If it's our last NCP, clear our ``upat'' value.
290ab2de065SBrian Somers    * If it's our last NCP, stop the autoload timer
291ff0f9439SBrian Somers    * If it's an LCP, adjust our phys_type.open value and any timers.
29286b1f0d7SBrian Somers    * If it's an LCP and we're in multilink mode, adjust our tun
293c8f30703SBrian Somers    * If it's the last LCP, down all NCPs
29486b1f0d7SBrian Somers    * speed and make sure our minimum sequence number is adjusted.
2956d666775SBrian Somers    */
296ab886ad0SBrian Somers 
297ab886ad0SBrian Somers   struct bundle *bundle = (struct bundle *)v;
298ab886ad0SBrian Somers 
299ab2de065SBrian Somers   if (fp->proto == PROTO_IPCP) {
300ab886ad0SBrian Somers     bundle_StopIdleTimer(bundle);
301dade2407SBrian Somers     bundle->upat = 0;
302ab2de065SBrian Somers     mp_StopAutoloadTimer(&bundle->ncp.mp);
303ab2de065SBrian Somers   } else if (fp->proto == PROTO_LCP) {
3043b0f8d2eSBrian Somers     struct datalink *dl;
30586b1f0d7SBrian Somers     struct datalink *lost;
306c8f30703SBrian Somers     int others_active;
307c8f30703SBrian Somers 
308c8f30703SBrian Somers     bundle_LinksRemoved(bundle);  /* adjust timers & phys_type values */
3093b0f8d2eSBrian Somers 
31086b1f0d7SBrian Somers     lost = NULL;
311c8f30703SBrian Somers     others_active = 0;
312c8f30703SBrian Somers     for (dl = bundle->links; dl; dl = dl->next) {
31386b1f0d7SBrian Somers       if (fp == &dl->physical->link.lcp.fsm)
31486b1f0d7SBrian Somers         lost = dl;
315c8f30703SBrian Somers       else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP)
316c8f30703SBrian Somers         others_active++;
317c8f30703SBrian Somers     }
31886b1f0d7SBrian Somers 
319c8f30703SBrian Somers     if (bundle->ncp.mp.active) {
320ab2de065SBrian Somers       bundle_CalculateBandwidth(bundle);
32186b1f0d7SBrian Somers 
32286b1f0d7SBrian Somers       if (lost)
32386b1f0d7SBrian Somers         mp_LinkLost(&bundle->ncp.mp, lost);
32486b1f0d7SBrian Somers       else
325a33b2ef7SBrian Somers         log_Printf(LogALERT, "Oops, lost an unrecognised datalink (%s) !\n",
32686b1f0d7SBrian Somers                    fp->link->name);
3273b0f8d2eSBrian Somers     }
328c8f30703SBrian Somers 
329c8f30703SBrian Somers     if (!others_active)
330c8f30703SBrian Somers       /* Down the NCPs.  We don't expect to get fsm_Close()d ourself ! */
331c8f30703SBrian Somers       fsm2initial(&bundle->ncp.ipcp.fsm);
3326d666775SBrian Somers   }
333ff0f9439SBrian Somers }
3346d666775SBrian Somers 
3356d666775SBrian Somers static void
3366d666775SBrian Somers bundle_LayerFinish(void *v, struct fsm *fp)
3376d666775SBrian Somers {
3386d666775SBrian Somers   /* The given fsm is now down (fp cannot be NULL)
3396d666775SBrian Somers    *
340dd7e2610SBrian Somers    * If it's the last NCP, fsm_Close all LCPs
3416d666775SBrian Somers    */
3426d666775SBrian Somers 
3436d666775SBrian Somers   struct bundle *bundle = (struct bundle *)v;
3446d666775SBrian Somers   struct datalink *dl;
3456d666775SBrian Somers 
3463b0f8d2eSBrian Somers   if (fp->proto == PROTO_IPCP) {
34726afeaa2SBrian Somers     if (bundle_Phase(bundle) != PHASE_DEAD)
34825092092SBrian Somers       bundle_NewPhase(bundle, PHASE_TERMINATE);
3496d666775SBrian Somers     for (dl = bundle->links; dl; dl = dl->next)
350c8f30703SBrian Somers       if (dl->state == DATALINK_OPEN)
351d4d5d2f8SBrian Somers         datalink_Close(dl, CLOSE_STAYDOWN);
35209206a6fSBrian Somers     fsm2initial(fp);
3533b0f8d2eSBrian Somers   }
3543b0f8d2eSBrian Somers }
3556d666775SBrian Somers 
3567a6f8720SBrian Somers int
3577a6f8720SBrian Somers bundle_LinkIsUp(const struct bundle *bundle)
3587a6f8720SBrian Somers {
3595828db6dSBrian Somers   return bundle->ncp.ipcp.fsm.state == ST_OPENED;
3607a6f8720SBrian Somers }
3617a6f8720SBrian Somers 
3627a6f8720SBrian Somers void
3639c81b87dSBrian Somers bundle_Close(struct bundle *bundle, const char *name, int how)
3647a6f8720SBrian Somers {
365455aabc3SBrian Somers   /*
3663006ec67SBrian Somers    * Please close the given datalink.
367dd7e2610SBrian Somers    * If name == NULL or name is the last datalink, fsm_Close all NCPs
3686f384573SBrian Somers    * (except our MP)
3693b0f8d2eSBrian Somers    * If it isn't the last datalink, just Close that datalink.
370455aabc3SBrian Somers    */
3717a6f8720SBrian Somers 
3723b0f8d2eSBrian Somers   struct datalink *dl, *this_dl;
3733b0f8d2eSBrian Somers   int others_active;
3743006ec67SBrian Somers 
3753b0f8d2eSBrian Somers   others_active = 0;
3763b0f8d2eSBrian Somers   this_dl = NULL;
3773b0f8d2eSBrian Somers 
3783b0f8d2eSBrian Somers   for (dl = bundle->links; dl; dl = dl->next) {
3793b0f8d2eSBrian Somers     if (name && !strcasecmp(name, dl->name))
3803b0f8d2eSBrian Somers       this_dl = dl;
3813b0f8d2eSBrian Somers     if (name == NULL || this_dl == dl) {
3829c81b87dSBrian Somers       switch (how) {
3839c81b87dSBrian Somers         case CLOSE_LCP:
3849c81b87dSBrian Somers           datalink_DontHangup(dl);
3852fc2f705SBrian Somers           break;
3869c81b87dSBrian Somers         case CLOSE_STAYDOWN:
3873006ec67SBrian Somers           datalink_StayDown(dl);
3889c81b87dSBrian Somers           break;
3899c81b87dSBrian Somers       }
3903b0f8d2eSBrian Somers     } else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP)
3913b0f8d2eSBrian Somers       others_active++;
3923b0f8d2eSBrian Somers   }
3933b0f8d2eSBrian Somers 
3943b0f8d2eSBrian Somers   if (name && this_dl == NULL) {
395dd7e2610SBrian Somers     log_Printf(LogWARN, "%s: Invalid datalink name\n", name);
3963b0f8d2eSBrian Somers     return;
3973b0f8d2eSBrian Somers   }
3983b0f8d2eSBrian Somers 
3993b0f8d2eSBrian Somers   if (!others_active) {
40004eaa58cSBrian Somers     bundle_StopIdleTimer(bundle);
4013b0f8d2eSBrian Somers     if (bundle->ncp.ipcp.fsm.state > ST_CLOSED ||
4023b0f8d2eSBrian Somers         bundle->ncp.ipcp.fsm.state == ST_STARTING)
403dd7e2610SBrian Somers       fsm_Close(&bundle->ncp.ipcp.fsm);
4043b0f8d2eSBrian Somers     else {
40509206a6fSBrian Somers       fsm2initial(&bundle->ncp.ipcp.fsm);
406d345321bSBrian Somers       for (dl = bundle->links; dl; dl = dl->next)
4079c81b87dSBrian Somers         datalink_Close(dl, how);
4087a6f8720SBrian Somers     }
4093b0f8d2eSBrian Somers   } else if (this_dl && this_dl->state != DATALINK_CLOSED &&
4103b0f8d2eSBrian Somers              this_dl->state != DATALINK_HANGUP)
4119c81b87dSBrian Somers     datalink_Close(this_dl, how);
412d2fd8d77SBrian Somers }
4137a6f8720SBrian Somers 
4141bc9b5baSBrian Somers void
415899011c4SBrian Somers bundle_Down(struct bundle *bundle, int how)
4161bc9b5baSBrian Somers {
4171bc9b5baSBrian Somers   struct datalink *dl;
4181bc9b5baSBrian Somers 
4191bc9b5baSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
420899011c4SBrian Somers     datalink_Down(dl, how);
4211bc9b5baSBrian Somers }
4221bc9b5baSBrian Somers 
42326af0ae9SBrian Somers static size_t
42426af0ae9SBrian Somers bundle_FillQueues(struct bundle *bundle)
42526af0ae9SBrian Somers {
42626af0ae9SBrian Somers   size_t total;
42726af0ae9SBrian Somers 
42826af0ae9SBrian Somers   if (bundle->ncp.mp.active)
42926af0ae9SBrian Somers     total = mp_FillQueues(bundle);
43026af0ae9SBrian Somers   else {
43126af0ae9SBrian Somers     struct datalink *dl;
43226af0ae9SBrian Somers     size_t add;
43326af0ae9SBrian Somers 
43426af0ae9SBrian Somers     for (total = 0, dl = bundle->links; dl; dl = dl->next)
43526af0ae9SBrian Somers       if (dl->state == DATALINK_OPEN) {
43626af0ae9SBrian Somers         add = link_QueueLen(&dl->physical->link);
43726af0ae9SBrian Somers         if (add == 0 && dl->physical->out == NULL)
43826af0ae9SBrian Somers           add = ip_PushPacket(&dl->physical->link, bundle);
43926af0ae9SBrian Somers         total += add;
44026af0ae9SBrian Somers       }
44126af0ae9SBrian Somers   }
44226af0ae9SBrian Somers 
44326af0ae9SBrian Somers   return total + ip_QueueLen(&bundle->ncp.ipcp);
44426af0ae9SBrian Somers }
44526af0ae9SBrian Somers 
4462f786681SBrian Somers static int
447f013f33eSBrian Somers bundle_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
4482f786681SBrian Somers {
4492f786681SBrian Somers   struct bundle *bundle = descriptor2bundle(d);
4502f786681SBrian Somers   struct datalink *dl;
45126af0ae9SBrian Somers   int result, nlinks;
4526c1d6731SBrian Somers   u_short ifqueue;
45326af0ae9SBrian Somers   size_t queued;
4542f786681SBrian Somers 
4552f786681SBrian Somers   result = 0;
4562f786681SBrian Somers 
457078c562eSBrian Somers   /* If there are aren't many packets queued, look for some more. */
45804eaa58cSBrian Somers   for (nlinks = 0, dl = bundle->links; dl; dl = dl->next)
45904eaa58cSBrian Somers     nlinks++;
46004eaa58cSBrian Somers 
46104eaa58cSBrian Somers   if (nlinks) {
4625a72b6edSBrian Somers     queued = r ? bundle_FillQueues(bundle) : ip_QueueLen(&bundle->ncp.ipcp);
46304eaa58cSBrian Somers 
464ff0f9439SBrian Somers     if (r && (bundle->phase == PHASE_NETWORK ||
465ff0f9439SBrian Somers               bundle->phys_type.all & PHYS_AUTO)) {
46604eaa58cSBrian Somers       /* enough surplus so that we can tell if we're getting swamped */
4676c1d6731SBrian Somers       ifqueue = nlinks > bundle->cfg.ifqueue ? nlinks : bundle->cfg.ifqueue;
4686c1d6731SBrian Somers       if (queued < ifqueue) {
46904eaa58cSBrian Somers         /* Not enough - select() for more */
4706f8e9f0aSBrian Somers         if (bundle->choked.timer.state == TIMER_RUNNING)
4716f8e9f0aSBrian Somers           timer_Stop(&bundle->choked.timer);	/* Not needed any more */
47204eaa58cSBrian Somers         FD_SET(bundle->dev.fd, r);
473faefde08SBrian Somers         if (*n < bundle->dev.fd + 1)
474faefde08SBrian Somers           *n = bundle->dev.fd + 1;
4751384bd27SBrian Somers         log_Printf(LogTIMER, "%s: fdset(r) %d\n", TUN_NAME, bundle->dev.fd);
476078c562eSBrian Somers         result++;
4776f8e9f0aSBrian Somers       } else if (bundle->choked.timer.state == TIMER_STOPPED) {
4786f8e9f0aSBrian Somers         bundle->choked.timer.func = bundle_ClearQueues;
4796f8e9f0aSBrian Somers         bundle->choked.timer.name = "output choke";
4806f8e9f0aSBrian Somers         bundle->choked.timer.load = bundle->cfg.choked.timeout * SECTICKS;
4816f8e9f0aSBrian Somers         bundle->choked.timer.arg = bundle;
4826f8e9f0aSBrian Somers         timer_Start(&bundle->choked.timer);
483078c562eSBrian Somers       }
48404eaa58cSBrian Somers     }
48504eaa58cSBrian Somers   }
486078c562eSBrian Somers 
487f0cdd9c0SBrian Somers #ifndef NORADIUS
488f0cdd9c0SBrian Somers   result += descriptor_UpdateSet(&bundle->radius.desc, r, w, e, n);
489f0cdd9c0SBrian Somers #endif
490f0cdd9c0SBrian Somers 
49171555108SBrian Somers   /* Which links need a select() ? */
49271555108SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
49371555108SBrian Somers     result += descriptor_UpdateSet(&dl->desc, r, w, e, n);
49471555108SBrian Somers 
495ea722969SBrian Somers   /*
496ea722969SBrian Somers    * This *MUST* be called after the datalink UpdateSet()s as it
49704eaa58cSBrian Somers    * might be ``holding'' one of the datalinks (death-row) and
4988e7bd08eSBrian Somers    * wants to be able to de-select() it from the descriptor set.
499ea722969SBrian Somers    */
5000f2f3eb3SBrian Somers   result += descriptor_UpdateSet(&bundle->ncp.mp.server.desc, r, w, e, n);
501ea722969SBrian Somers 
5022f786681SBrian Somers   return result;
5032f786681SBrian Somers }
5042f786681SBrian Somers 
5052f786681SBrian Somers static int
506f013f33eSBrian Somers bundle_IsSet(struct fdescriptor *d, const fd_set *fdset)
5072f786681SBrian Somers {
5082f786681SBrian Somers   struct bundle *bundle = descriptor2bundle(d);
5092f786681SBrian Somers   struct datalink *dl;
5102f786681SBrian Somers 
5112f786681SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
5122f786681SBrian Somers     if (descriptor_IsSet(&dl->desc, fdset))
5132f786681SBrian Somers       return 1;
5142f786681SBrian Somers 
515f0cdd9c0SBrian Somers #ifndef NORADIUS
516f0cdd9c0SBrian Somers   if (descriptor_IsSet(&bundle->radius.desc, fdset))
517f0cdd9c0SBrian Somers     return 1;
518f0cdd9c0SBrian Somers #endif
519f0cdd9c0SBrian Somers 
520332b9de0SBrian Somers   if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset))
521332b9de0SBrian Somers     return 1;
522332b9de0SBrian Somers 
523faefde08SBrian Somers   return FD_ISSET(bundle->dev.fd, fdset);
5242f786681SBrian Somers }
5252f786681SBrian Somers 
5262f786681SBrian Somers static void
527f013f33eSBrian Somers bundle_DescriptorRead(struct fdescriptor *d, struct bundle *bundle,
5282f786681SBrian Somers                       const fd_set *fdset)
5292f786681SBrian Somers {
5302f786681SBrian Somers   struct datalink *dl;
5310a4b6c5cSBrian Somers   unsigned secs;
5322f786681SBrian Somers 
533ea722969SBrian Somers   if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset))
534ea722969SBrian Somers     descriptor_Read(&bundle->ncp.mp.server.desc, bundle, fdset);
535ea722969SBrian Somers 
5362f786681SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
5372f786681SBrian Somers     if (descriptor_IsSet(&dl->desc, fdset))
5382f786681SBrian Somers       descriptor_Read(&dl->desc, bundle, fdset);
539b6217683SBrian Somers 
540f0cdd9c0SBrian Somers #ifndef NORADIUS
541f0cdd9c0SBrian Somers   if (descriptor_IsSet(&bundle->radius.desc, fdset))
542f0cdd9c0SBrian Somers     descriptor_Read(&bundle->radius.desc, bundle, fdset);
543f0cdd9c0SBrian Somers #endif
544f0cdd9c0SBrian Somers 
545faefde08SBrian Somers   if (FD_ISSET(bundle->dev.fd, fdset)) {
546078c562eSBrian Somers     struct tun_data tun;
547078c562eSBrian Somers     int n, pri;
5483a7b6d76SBrian Somers     char *data;
5493a7b6d76SBrian Somers     size_t sz;
5503a7b6d76SBrian Somers 
5513a7b6d76SBrian Somers     if (bundle->dev.header) {
5523a7b6d76SBrian Somers       data = (char *)&tun;
5533a7b6d76SBrian Somers       sz = sizeof tun;
5543a7b6d76SBrian Somers     } else {
5553a7b6d76SBrian Somers       data = tun.data;
5563a7b6d76SBrian Somers       sz = sizeof tun.data;
5573a7b6d76SBrian Somers     }
558078c562eSBrian Somers 
559078c562eSBrian Somers     /* something to read from tun */
5603a7b6d76SBrian Somers 
5613a7b6d76SBrian Somers     n = read(bundle->dev.fd, data, sz);
562078c562eSBrian Somers     if (n < 0) {
5633a7b6d76SBrian Somers       log_Printf(LogWARN, "%s: read: %s\n", bundle->dev.Name, strerror(errno));
564078c562eSBrian Somers       return;
565078c562eSBrian Somers     }
5663a7b6d76SBrian Somers 
5673a7b6d76SBrian Somers     if (bundle->dev.header) {
5683a7b6d76SBrian Somers       n -= sz - sizeof tun.data;
569078c562eSBrian Somers       if (n <= 0) {
5703a7b6d76SBrian Somers         log_Printf(LogERROR, "%s: read: Got only %d bytes of data !\n",
5713a7b6d76SBrian Somers                    bundle->dev.Name, n);
572078c562eSBrian Somers         return;
573078c562eSBrian Somers       }
5740a4b6c5cSBrian Somers       if (ntohl(tun.header.family) != AF_INET)
5753a7b6d76SBrian Somers         /* XXX: Should be maintaining drop/family counts ! */
576078c562eSBrian Somers         return;
5773a7b6d76SBrian Somers     }
578078c562eSBrian Somers 
579078c562eSBrian Somers     if (((struct ip *)tun.data)->ip_dst.s_addr ==
580078c562eSBrian Somers         bundle->ncp.ipcp.my_ip.s_addr) {
581078c562eSBrian Somers       /* we've been asked to send something addressed *to* us :( */
582078c562eSBrian Somers       if (Enabled(bundle, OPT_LOOPBACK)) {
5830a4b6c5cSBrian Somers         pri = PacketCheck(bundle, tun.data, n, &bundle->filter.in, NULL, NULL);
584078c562eSBrian Somers         if (pri >= 0) {
5853a7b6d76SBrian Somers           n += sz - sizeof tun.data;
5863a7b6d76SBrian Somers           write(bundle->dev.fd, data, n);
587078c562eSBrian Somers           log_Printf(LogDEBUG, "Looped back packet addressed to myself\n");
588078c562eSBrian Somers         }
589078c562eSBrian Somers         return;
590078c562eSBrian Somers       } else
591078c562eSBrian Somers         log_Printf(LogDEBUG, "Oops - forwarding packet addressed to myself\n");
592078c562eSBrian Somers     }
593078c562eSBrian Somers 
594078c562eSBrian Somers     /*
595078c562eSBrian Somers      * Process on-demand dialup. Output packets are queued within tunnel
596078c562eSBrian Somers      * device until IPCP is opened.
597078c562eSBrian Somers      */
598078c562eSBrian Somers 
599078c562eSBrian Somers     if (bundle_Phase(bundle) == PHASE_DEAD) {
600078c562eSBrian Somers       /*
601078c562eSBrian Somers        * Note, we must be in AUTO mode :-/ otherwise our interface should
602078c562eSBrian Somers        * *not* be UP and we can't receive data
603078c562eSBrian Somers        */
6040a4b6c5cSBrian Somers       pri = PacketCheck(bundle, tun.data, n, &bundle->filter.dial, NULL, NULL);
605040cfe28SBrian Somers       if (pri >= 0)
606ba23f397SBrian Somers         bundle_Open(bundle, NULL, PHYS_AUTO, 0);
607078c562eSBrian Somers       else
608078c562eSBrian Somers         /*
609078c562eSBrian Somers          * Drop the packet.  If we were to queue it, we'd just end up with
610078c562eSBrian Somers          * a pile of timed-out data in our output queue by the time we get
611078c562eSBrian Somers          * around to actually dialing.  We'd also prematurely reach the
612078c562eSBrian Somers          * threshold at which we stop select()ing to read() the tun
613078c562eSBrian Somers          * device - breaking auto-dial.
614078c562eSBrian Somers          */
615078c562eSBrian Somers         return;
616078c562eSBrian Somers     }
617078c562eSBrian Somers 
6180a4b6c5cSBrian Somers     secs = 0;
6190a4b6c5cSBrian Somers     pri = PacketCheck(bundle, tun.data, n, &bundle->filter.out, NULL, &secs);
6200a4b6c5cSBrian Somers     if (pri >= 0) {
6210a4b6c5cSBrian Somers       /* Prepend the number of seconds timeout given in the filter */
6220a4b6c5cSBrian Somers       tun.header.timeout = secs;
6230a4b6c5cSBrian Somers       ip_Enqueue(&bundle->ncp.ipcp, pri, (char *)&tun, n + sizeof tun.header);
6240a4b6c5cSBrian Somers     }
625078c562eSBrian Somers   }
626078c562eSBrian Somers }
6272f786681SBrian Somers 
6281af29a6eSBrian Somers static int
629f013f33eSBrian Somers bundle_DescriptorWrite(struct fdescriptor *d, struct bundle *bundle,
6302f786681SBrian Somers                        const fd_set *fdset)
6312f786681SBrian Somers {
6322f786681SBrian Somers   struct datalink *dl;
6331af29a6eSBrian Somers   int result = 0;
6342f786681SBrian Somers 
635ea722969SBrian Somers   /* This is not actually necessary as struct mpserver doesn't Write() */
636ea722969SBrian Somers   if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset))
637ea722969SBrian Somers     descriptor_Write(&bundle->ncp.mp.server.desc, bundle, fdset);
638ea722969SBrian Somers 
6392f786681SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
6402f786681SBrian Somers     if (descriptor_IsSet(&dl->desc, fdset))
6411af29a6eSBrian Somers       result += descriptor_Write(&dl->desc, bundle, fdset);
6421af29a6eSBrian Somers 
6431af29a6eSBrian Somers   return result;
6442f786681SBrian Somers }
6452f786681SBrian Somers 
646da66dd13SBrian Somers void
6471384bd27SBrian Somers bundle_LockTun(struct bundle *bundle)
6481384bd27SBrian Somers {
6491384bd27SBrian Somers   FILE *lockfile;
65052847614SBrian Somers   char pidfile[PATH_MAX];
6511384bd27SBrian Somers 
6521384bd27SBrian Somers   snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit);
6531384bd27SBrian Somers   lockfile = ID0fopen(pidfile, "w");
6541384bd27SBrian Somers   if (lockfile != NULL) {
6551384bd27SBrian Somers     fprintf(lockfile, "%d\n", (int)getpid());
6561384bd27SBrian Somers     fclose(lockfile);
6571384bd27SBrian Somers   }
6581384bd27SBrian Somers #ifndef RELEASE_CRUNCH
6591384bd27SBrian Somers   else
6601384bd27SBrian Somers     log_Printf(LogERROR, "Warning: Can't create %s: %s\n",
6611384bd27SBrian Somers                pidfile, strerror(errno));
6621384bd27SBrian Somers #endif
6631384bd27SBrian Somers }
6641384bd27SBrian Somers 
6651384bd27SBrian Somers static void
6661384bd27SBrian Somers bundle_UnlockTun(struct bundle *bundle)
6671384bd27SBrian Somers {
66852847614SBrian Somers   char pidfile[PATH_MAX];
6691384bd27SBrian Somers 
6701384bd27SBrian Somers   snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit);
6711384bd27SBrian Somers   ID0unlink(pidfile);
6721384bd27SBrian Somers }
6737a6f8720SBrian Somers 
6747a6f8720SBrian Somers struct bundle *
675cf0a3940SBrian Somers bundle_Create(const char *prefix, int type, int unit)
6767a6f8720SBrian Somers {
6777a6f8720SBrian Somers   static struct bundle bundle;		/* there can be only one */
678c0593e34SBrian Somers   int enoentcount, err, minunit, maxunit;
6794e5196e9SBrian Somers   const char *ifname;
680fdb4bb1bSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
681fc3034caSBrian Somers   int kldtried;
682fc3034caSBrian Somers #endif
6833a7b6d76SBrian Somers #if defined(TUNSIFMODE) || defined(TUNSLMODE) || defined(TUNSIFHEAD)
6840f203c7eSBrian Somers   int iff;
6850f203c7eSBrian Somers #endif
6867a6f8720SBrian Somers 
6878fa6ebe4SBrian Somers   if (bundle.iface != NULL) {	/* Already allocated ! */
688a33b2ef7SBrian Somers     log_Printf(LogALERT, "bundle_Create:  There's only one BUNDLE !\n");
6897a6f8720SBrian Somers     return NULL;
6907a6f8720SBrian Somers   }
6917a6f8720SBrian Somers 
692c0593e34SBrian Somers   if (unit == -1) {
693c0593e34SBrian Somers     minunit = 0;
694c0593e34SBrian Somers     maxunit = -1;
695c0593e34SBrian Somers   } else {
696c0593e34SBrian Somers     minunit = unit;
697c0593e34SBrian Somers     maxunit = unit + 1;
698c0593e34SBrian Somers   }
6997a6f8720SBrian Somers   err = ENOENT;
7007a6f8720SBrian Somers   enoentcount = 0;
701fdb4bb1bSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
702fc3034caSBrian Somers   kldtried = 0;
703fc3034caSBrian Somers #endif
704c0593e34SBrian Somers   for (bundle.unit = minunit; bundle.unit != maxunit; bundle.unit++) {
705faefde08SBrian Somers     snprintf(bundle.dev.Name, sizeof bundle.dev.Name, "%s%d",
706faefde08SBrian Somers              prefix, bundle.unit);
707faefde08SBrian Somers     bundle.dev.fd = ID0open(bundle.dev.Name, O_RDWR);
708faefde08SBrian Somers     if (bundle.dev.fd >= 0)
7097a6f8720SBrian Somers       break;
710728ef5b2SBrian Somers     else if (errno == ENXIO || errno == ENOENT) {
711fdb4bb1bSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
712c0593e34SBrian Somers       if (bundle.unit == minunit && !kldtried++) {
713fc3034caSBrian Somers         /*
714fdb4bb1bSBrian Somers 	 * Attempt to load the tunnel interface KLD if it isn't loaded
715fdb4bb1bSBrian Somers 	 * already.
716fc3034caSBrian Somers          */
717fdb4bb1bSBrian Somers         if (modfind("if_tun") == -1) {
718fc3034caSBrian Somers           if (ID0kldload("if_tun") != -1) {
719fc3034caSBrian Somers             bundle.unit--;
720fc3034caSBrian Somers             continue;
721fc3034caSBrian Somers           }
722fc3034caSBrian Somers           log_Printf(LogWARN, "kldload: if_tun: %s\n", strerror(errno));
723fc3034caSBrian Somers         }
724fc3034caSBrian Somers       }
725fc3034caSBrian Somers #endif
726c4c6616aSBrian Somers       if (errno != ENOENT || ++enoentcount > 2) {
7277a6f8720SBrian Somers         err = errno;
728107d62e7SBrian Somers 	break;
729c4c6616aSBrian Somers       }
7307a6f8720SBrian Somers     } else
7317a6f8720SBrian Somers       err = errno;
7327a6f8720SBrian Somers   }
7337a6f8720SBrian Somers 
734faefde08SBrian Somers   if (bundle.dev.fd < 0) {
735c0593e34SBrian Somers     if (unit == -1)
736c0593e34SBrian Somers       log_Printf(LogWARN, "No available tunnel devices found (%s)\n",
73785b542cfSBrian Somers                 strerror(err));
738c0593e34SBrian Somers     else
739c0593e34SBrian Somers       log_Printf(LogWARN, "%s%d: %s\n", prefix, unit, strerror(err));
7407a6f8720SBrian Somers     return NULL;
7417a6f8720SBrian Somers   }
7427a6f8720SBrian Somers 
743dd7e2610SBrian Somers   log_SetTun(bundle.unit);
7447a6f8720SBrian Somers 
7458fa6ebe4SBrian Somers   ifname = strrchr(bundle.dev.Name, '/');
7468fa6ebe4SBrian Somers   if (ifname == NULL)
7478fa6ebe4SBrian Somers     ifname = bundle.dev.Name;
7487a6f8720SBrian Somers   else
7498fa6ebe4SBrian Somers     ifname++;
7508fa6ebe4SBrian Somers 
7518fa6ebe4SBrian Somers   bundle.iface = iface_Create(ifname);
7528fa6ebe4SBrian Somers   if (bundle.iface == NULL) {
7538fa6ebe4SBrian Somers     close(bundle.dev.fd);
7548fa6ebe4SBrian Somers     return NULL;
7558fa6ebe4SBrian Somers   }
7567a6f8720SBrian Somers 
7570f203c7eSBrian Somers #ifdef TUNSIFMODE
7580f203c7eSBrian Somers   /* Make sure we're POINTOPOINT */
7590f203c7eSBrian Somers   iff = IFF_POINTOPOINT;
7600f203c7eSBrian Somers   if (ID0ioctl(bundle.dev.fd, TUNSIFMODE, &iff) < 0)
7610f203c7eSBrian Somers     log_Printf(LogERROR, "bundle_Create: ioctl(TUNSIFMODE): %s\n",
7620f203c7eSBrian Somers 	       strerror(errno));
7630f203c7eSBrian Somers #endif
7640f203c7eSBrian Somers 
7656ca65df0SBrian Somers #ifdef TUNSLMODE
7663a7b6d76SBrian Somers   /* Make sure we're not prepending sockaddrs */
7676ca65df0SBrian Somers   iff = 0;
7686ca65df0SBrian Somers   if (ID0ioctl(bundle.dev.fd, TUNSLMODE, &iff) < 0)
7696ca65df0SBrian Somers     log_Printf(LogERROR, "bundle_Create: ioctl(TUNSLMODE): %s\n",
7706ca65df0SBrian Somers 	       strerror(errno));
7716ca65df0SBrian Somers #endif
7726ca65df0SBrian Somers 
7733a7b6d76SBrian Somers #ifdef TUNSIFHEAD
7743a7b6d76SBrian Somers   /* We want the address family please ! */
7753a7b6d76SBrian Somers   iff = 1;
7763a7b6d76SBrian Somers   if (ID0ioctl(bundle.dev.fd, TUNSIFHEAD, &iff) < 0) {
7773a7b6d76SBrian Somers     log_Printf(LogERROR, "bundle_Create: ioctl(TUNSIFHEAD): %s\n",
7783a7b6d76SBrian Somers 	       strerror(errno));
7793a7b6d76SBrian Somers     bundle.dev.header = 0;
7803a7b6d76SBrian Somers   } else
7813a7b6d76SBrian Somers     bundle.dev.header = 1;
7823a7b6d76SBrian Somers #else
7833a7b6d76SBrian Somers #ifdef __OpenBSD__
7843a7b6d76SBrian Somers   /* Always present for OpenBSD */
7853a7b6d76SBrian Somers   bundle.dev.header = 1;
7863a7b6d76SBrian Somers #else
7873a7b6d76SBrian Somers   /*
7883a7b6d76SBrian Somers    * If TUNSIFHEAD isn't available and we're not OpenBSD, assume
7893a7b6d76SBrian Somers    * everything's AF_INET (hopefully the tun device won't pass us
7903a7b6d76SBrian Somers    * anything else !).
7913a7b6d76SBrian Somers    */
7923a7b6d76SBrian Somers   bundle.dev.header = 0;
7933a7b6d76SBrian Somers #endif
7943a7b6d76SBrian Somers #endif
7953a7b6d76SBrian Somers 
796dc744e19SBrian Somers   if (!iface_SetFlags(bundle.iface->name, IFF_UP)) {
7978fa6ebe4SBrian Somers     iface_Destroy(bundle.iface);
7988fa6ebe4SBrian Somers     bundle.iface = NULL;
799faefde08SBrian Somers     close(bundle.dev.fd);
8007a6f8720SBrian Somers     return NULL;
8017a6f8720SBrian Somers   }
8027a6f8720SBrian Somers 
8038fa6ebe4SBrian Somers   log_Printf(LogPHASE, "Using interface: %s\n", ifname);
8047a6f8720SBrian Somers 
805ab2de065SBrian Somers   bundle.bandwidth = 0;
806820de6ebSBrian Somers   bundle.routing_seq = 0;
807a0cbd833SBrian Somers   bundle.phase = PHASE_DEAD;
808a0cbd833SBrian Somers   bundle.CleaningUp = 0;
80967b072f7SBrian Somers   bundle.NatEnabled = 0;
8107a6f8720SBrian Somers 
8116d666775SBrian Somers   bundle.fsm.LayerStart = bundle_LayerStart;
8126f384573SBrian Somers   bundle.fsm.LayerUp = bundle_LayerUp;
8136d666775SBrian Somers   bundle.fsm.LayerDown = bundle_LayerDown;
8146d666775SBrian Somers   bundle.fsm.LayerFinish = bundle_LayerFinish;
8156d666775SBrian Somers   bundle.fsm.object = &bundle;
8167a6f8720SBrian Somers 
817dade2407SBrian Somers   bundle.cfg.idle.timeout = NCP_IDLE_TIMEOUT;
818dade2407SBrian Somers   bundle.cfg.idle.min_timeout = 0;
8191342caedSBrian Somers   *bundle.cfg.auth.name = '\0';
8201342caedSBrian Somers   *bundle.cfg.auth.key = '\0';
82194d7be52SBrian Somers   bundle.cfg.opt = OPT_SROUTES | OPT_IDCHECK | OPT_LOOPBACK | OPT_TCPMSSFIXUP |
822610b185fSBrian Somers                    OPT_THROUGHPUT | OPT_UTMP;
82349052c95SBrian Somers   *bundle.cfg.label = '\0';
8246c1d6731SBrian Somers   bundle.cfg.ifqueue = DEF_IFQUEUE;
8256f8e9f0aSBrian Somers   bundle.cfg.choked.timeout = CHOKED_TIMEOUT;
826ff0f9439SBrian Somers   bundle.phys_type.all = type;
827ff0f9439SBrian Somers   bundle.phys_type.open = 0;
828dade2407SBrian Somers   bundle.upat = 0;
829ab886ad0SBrian Somers 
8306f384573SBrian Somers   bundle.links = datalink_Create("deflink", &bundle, type);
8313006ec67SBrian Somers   if (bundle.links == NULL) {
832a33b2ef7SBrian Somers     log_Printf(LogALERT, "Cannot create data link: %s\n", strerror(errno));
8338fa6ebe4SBrian Somers     iface_Destroy(bundle.iface);
8348fa6ebe4SBrian Somers     bundle.iface = NULL;
835faefde08SBrian Somers     close(bundle.dev.fd);
8362289f246SBrian Somers     return NULL;
8372289f246SBrian Somers   }
8382289f246SBrian Somers 
8392f786681SBrian Somers   bundle.desc.type = BUNDLE_DESCRIPTOR;
8402f786681SBrian Somers   bundle.desc.UpdateSet = bundle_UpdateSet;
8412f786681SBrian Somers   bundle.desc.IsSet = bundle_IsSet;
8422f786681SBrian Somers   bundle.desc.Read = bundle_DescriptorRead;
8432f786681SBrian Somers   bundle.desc.Write = bundle_DescriptorWrite;
8442f786681SBrian Somers 
84549052c95SBrian Somers   mp_Init(&bundle.ncp.mp, &bundle);
84649052c95SBrian Somers 
84749052c95SBrian Somers   /* Send over the first physical link by default */
8485828db6dSBrian Somers   ipcp_Init(&bundle.ncp.ipcp, &bundle, &bundle.links->physical->link,
8495828db6dSBrian Somers             &bundle.fsm);
8506d666775SBrian Somers 
8515ca5389aSBrian Somers   memset(&bundle.filter, '\0', sizeof bundle.filter);
8525ca5389aSBrian Somers   bundle.filter.in.fragok = bundle.filter.in.logok = 1;
8535ca5389aSBrian Somers   bundle.filter.in.name = "IN";
8545ca5389aSBrian Somers   bundle.filter.out.fragok = bundle.filter.out.logok = 1;
8555ca5389aSBrian Somers   bundle.filter.out.name = "OUT";
8565ca5389aSBrian Somers   bundle.filter.dial.name = "DIAL";
8578390b576SBrian Somers   bundle.filter.dial.logok = 1;
8585ca5389aSBrian Somers   bundle.filter.alive.name = "ALIVE";
8595ca5389aSBrian Somers   bundle.filter.alive.logok = 1;
860cad7e742SBrian Somers   {
861cad7e742SBrian Somers     int	i;
862cad7e742SBrian Somers     for (i = 0; i < MAXFILTERS; i++) {
863cad7e742SBrian Somers         bundle.filter.in.rule[i].f_action = A_NONE;
864cad7e742SBrian Somers         bundle.filter.out.rule[i].f_action = A_NONE;
865cad7e742SBrian Somers         bundle.filter.dial.rule[i].f_action = A_NONE;
866cad7e742SBrian Somers         bundle.filter.alive.rule[i].f_action = A_NONE;
867cad7e742SBrian Somers     }
868cad7e742SBrian Somers   }
86993ee0ff2SBrian Somers   memset(&bundle.idle.timer, '\0', sizeof bundle.idle.timer);
87093ee0ff2SBrian Somers   bundle.idle.done = 0;
8715cf4388bSBrian Somers   bundle.notify.fd = -1;
8726f8e9f0aSBrian Somers   memset(&bundle.choked.timer, '\0', sizeof bundle.choked.timer);
873972a1bcfSBrian Somers #ifndef NORADIUS
874972a1bcfSBrian Somers   radius_Init(&bundle.radius);
875972a1bcfSBrian Somers #endif
87693ee0ff2SBrian Somers 
8776d666775SBrian Somers   /* Clean out any leftover crud */
8788fa6ebe4SBrian Somers   iface_Clear(bundle.iface, IFACE_CLEAR_ALL);
8796d666775SBrian Somers 
8801384bd27SBrian Somers   bundle_LockTun(&bundle);
8811384bd27SBrian Somers 
8827a6f8720SBrian Somers   return &bundle;
8837a6f8720SBrian Somers }
8847a6f8720SBrian Somers 
88568a0f0ccSBrian Somers static void
88668a0f0ccSBrian Somers bundle_DownInterface(struct bundle *bundle)
88768a0f0ccSBrian Somers {
888dd7e2610SBrian Somers   route_IfDelete(bundle, 1);
889dc744e19SBrian Somers   iface_ClearFlags(bundle->iface->name, IFF_UP);
89068a0f0ccSBrian Somers }
89168a0f0ccSBrian Somers 
89268a0f0ccSBrian Somers void
89368a0f0ccSBrian Somers bundle_Destroy(struct bundle *bundle)
89468a0f0ccSBrian Somers {
8953006ec67SBrian Somers   struct datalink *dl;
896b6217683SBrian Somers 
897ea722969SBrian Somers   /*
89804eaa58cSBrian Somers    * Clean up the interface.  We don't need to timer_Stop()s, mp_Down(),
899ea722969SBrian Somers    * ipcp_CleanInterface() and bundle_DownInterface() unless we're getting
9008e7bd08eSBrian Somers    * out under exceptional conditions such as a descriptor exception.
901ea722969SBrian Somers    */
90204eaa58cSBrian Somers   timer_Stop(&bundle->idle.timer);
9036f8e9f0aSBrian Somers   timer_Stop(&bundle->choked.timer);
90466f634b6SBrian Somers   mp_Down(&bundle->ncp.mp);
905dd7e2610SBrian Somers   ipcp_CleanInterface(&bundle->ncp.ipcp);
90668a0f0ccSBrian Somers   bundle_DownInterface(bundle);
9073006ec67SBrian Somers 
908972a1bcfSBrian Somers #ifndef NORADIUS
909972a1bcfSBrian Somers   /* Tell the radius server the bad news */
910794c9bbcSBrian Somers   log_Printf(LogDEBUG, "Radius: Destroy called from bundle_Destroy\n");
911972a1bcfSBrian Somers   radius_Destroy(&bundle->radius);
912972a1bcfSBrian Somers #endif
913972a1bcfSBrian Somers 
914ea722969SBrian Somers   /* Again, these are all DATALINK_CLOSED unless we're abending */
9153006ec67SBrian Somers   dl = bundle->links;
9163006ec67SBrian Somers   while (dl)
9173006ec67SBrian Somers     dl = datalink_Destroy(dl);
9183006ec67SBrian Somers 
919442f8495SBrian Somers   ipcp_Destroy(&bundle->ncp.ipcp);
920442f8495SBrian Somers 
9211384bd27SBrian Somers   close(bundle->dev.fd);
9221384bd27SBrian Somers   bundle_UnlockTun(bundle);
9231384bd27SBrian Somers 
924ea722969SBrian Somers   /* In case we never made PHASE_NETWORK */
9255cf4388bSBrian Somers   bundle_Notify(bundle, EX_ERRDEAD);
926b6217683SBrian Somers 
9278fa6ebe4SBrian Somers   iface_Destroy(bundle->iface);
9288fa6ebe4SBrian Somers   bundle->iface = NULL;
92968a0f0ccSBrian Somers }
93068a0f0ccSBrian Somers 
93183d1af55SBrian Somers void
9323006ec67SBrian Somers bundle_LinkClosed(struct bundle *bundle, struct datalink *dl)
9333006ec67SBrian Somers {
9343006ec67SBrian Somers   /*
9353006ec67SBrian Somers    * Our datalink has closed.
936ea722969SBrian Somers    * CleanDatalinks() (called from DoLoop()) will remove closed
937f6a4e748SBrian Somers    * BACKGROUND, FOREGROUND and DIRECT links.
9383b0f8d2eSBrian Somers    * If it's the last data link, enter phase DEAD.
939ea722969SBrian Somers    *
940ea722969SBrian Somers    * NOTE: dl may not be in our list (bundle_SendDatalink()) !
9413006ec67SBrian Somers    */
9425b8b8060SBrian Somers 
9433b0f8d2eSBrian Somers   struct datalink *odl;
9443b0f8d2eSBrian Somers   int other_links;
9455b8b8060SBrian Somers 
946bf1d3ff6SBrian Somers   log_SetTtyCommandMode(dl);
947bf1d3ff6SBrian Somers 
9483b0f8d2eSBrian Somers   other_links = 0;
9493b0f8d2eSBrian Somers   for (odl = bundle->links; odl; odl = odl->next)
9503b0f8d2eSBrian Somers     if (odl != dl && odl->state != DATALINK_CLOSED)
9513b0f8d2eSBrian Somers       other_links++;
9523b0f8d2eSBrian Somers 
9533b0f8d2eSBrian Somers   if (!other_links) {
95481358fa3SBrian Somers     if (dl->physical->type != PHYS_AUTO)	/* Not in -auto mode */
95503704096SBrian Somers       bundle_DownInterface(bundle);
95609206a6fSBrian Somers     fsm2initial(&bundle->ncp.ipcp.fsm);
9575563ebdeSBrian Somers     bundle_NewPhase(bundle, PHASE_DEAD);
9580f2f3eb3SBrian Somers     bundle_StopIdleTimer(bundle);
959ab2de065SBrian Somers   }
960455aabc3SBrian Somers }
961455aabc3SBrian Somers 
962455aabc3SBrian Somers void
963ba23f397SBrian Somers bundle_Open(struct bundle *bundle, const char *name, int mask, int force)
9643006ec67SBrian Somers {
9653006ec67SBrian Somers   /*
9663006ec67SBrian Somers    * Please open the given datalink, or all if name == NULL
9673006ec67SBrian Somers    */
9683006ec67SBrian Somers   struct datalink *dl;
9693006ec67SBrian Somers 
9703006ec67SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
9713006ec67SBrian Somers     if (name == NULL || !strcasecmp(dl->name, name)) {
972ba23f397SBrian Somers       if ((mask & dl->physical->type) &&
973ba23f397SBrian Somers           (dl->state == DATALINK_CLOSED ||
974ba23f397SBrian Somers            (force && dl->state == DATALINK_OPENING &&
9755e269efeSBrian Somers             dl->dial.timer.state == TIMER_RUNNING) ||
9765e269efeSBrian Somers            dl->state == DATALINK_READY)) {
9775e269efeSBrian Somers         timer_Stop(&dl->dial.timer);	/* We're finished with this */
978565e35e5SBrian Somers         datalink_Up(dl, 1, 1);
979ab2de065SBrian Somers         if (mask & PHYS_AUTO)
9805e269efeSBrian Somers           break;			/* Only one AUTO link at a time */
98104eaa58cSBrian Somers       }
9823006ec67SBrian Somers       if (name != NULL)
9833006ec67SBrian Somers         break;
9843006ec67SBrian Somers     }
9853006ec67SBrian Somers }
9863006ec67SBrian Somers 
9873006ec67SBrian Somers struct datalink *
9883006ec67SBrian Somers bundle2datalink(struct bundle *bundle, const char *name)
9893006ec67SBrian Somers {
9903006ec67SBrian Somers   struct datalink *dl;
9913006ec67SBrian Somers 
9923006ec67SBrian Somers   if (name != NULL) {
9933006ec67SBrian Somers     for (dl = bundle->links; dl; dl = dl->next)
9943006ec67SBrian Somers       if (!strcasecmp(dl->name, name))
9953006ec67SBrian Somers         return dl;
9963006ec67SBrian Somers   } else if (bundle->links && !bundle->links->next)
9973006ec67SBrian Somers     return bundle->links;
9983006ec67SBrian Somers 
9993006ec67SBrian Somers   return NULL;
10003006ec67SBrian Somers }
10013006ec67SBrian Somers 
10023006ec67SBrian Somers int
1003aef795ccSBrian Somers bundle_ShowLinks(struct cmdargs const *arg)
1004aef795ccSBrian Somers {
1005aef795ccSBrian Somers   struct datalink *dl;
1006ab2de065SBrian Somers   struct pppThroughput *t;
100791cbd2eeSBrian Somers   unsigned long long octets;
1008ab2de065SBrian Somers   int secs;
1009aef795ccSBrian Somers 
10109c53a7b1SBrian Somers   for (dl = arg->bundle->links; dl; dl = dl->next) {
101191cbd2eeSBrian Somers     octets = MAX(dl->physical->link.stats.total.in.OctetsPerSecond,
101291cbd2eeSBrian Somers                  dl->physical->link.stats.total.out.OctetsPerSecond);
101391cbd2eeSBrian Somers 
1014d4156d00SBrian Somers     prompt_Printf(arg->prompt, "Name: %s [%s, %s]",
1015d4156d00SBrian Somers                   dl->name, mode2Nam(dl->physical->type), datalink_State(dl));
101611572abfSBrian Somers     if (dl->physical->link.stats.total.rolling && dl->state == DATALINK_OPEN)
1017e531f89aSBrian Somers       prompt_Printf(arg->prompt, " bandwidth %d, %llu bps (%llu bytes/sec)",
1018ab2de065SBrian Somers                     dl->mp.bandwidth ? dl->mp.bandwidth :
1019ab2de065SBrian Somers                                        physical_GetSpeed(dl->physical),
102091cbd2eeSBrian Somers                     octets * 8, octets);
10219c53a7b1SBrian Somers     prompt_Printf(arg->prompt, "\n");
10229c53a7b1SBrian Somers   }
1023aef795ccSBrian Somers 
102411572abfSBrian Somers   t = &arg->bundle->ncp.mp.link.stats.total;
102591cbd2eeSBrian Somers   octets = MAX(t->in.OctetsPerSecond, t->out.OctetsPerSecond);
1026ab2de065SBrian Somers   secs = t->downtime ? 0 : throughput_uptime(t);
1027ab2de065SBrian Somers   if (secs > t->SamplePeriod)
1028ab2de065SBrian Somers     secs = t->SamplePeriod;
1029ab2de065SBrian Somers   if (secs)
1030e531f89aSBrian Somers     prompt_Printf(arg->prompt, "Currently averaging %llu bps (%llu bytes/sec)"
103191cbd2eeSBrian Somers                   " over the last %d secs\n", octets * 8, octets, secs);
1032ab2de065SBrian Somers 
1033aef795ccSBrian Somers   return 0;
1034aef795ccSBrian Somers }
1035ab886ad0SBrian Somers 
10361342caedSBrian Somers static const char *
10371342caedSBrian Somers optval(struct bundle *bundle, int bit)
10381342caedSBrian Somers {
10391342caedSBrian Somers   return (bundle->cfg.opt & bit) ? "enabled" : "disabled";
10401342caedSBrian Somers }
10411342caedSBrian Somers 
1042c08717dfSBrian Somers int
1043c08717dfSBrian Somers bundle_ShowStatus(struct cmdargs const *arg)
1044c08717dfSBrian Somers {
1045c08717dfSBrian Somers   int remaining;
1046c08717dfSBrian Somers 
1047c08717dfSBrian Somers   prompt_Printf(arg->prompt, "Phase %s\n", bundle_PhaseName(arg->bundle));
1048faefde08SBrian Somers   prompt_Printf(arg->prompt, " Device:        %s\n", arg->bundle->dev.Name);
1049dade2407SBrian Somers   prompt_Printf(arg->prompt, " Interface:     %s @ %lubps",
1050ab2de065SBrian Somers                 arg->bundle->iface->name, arg->bundle->bandwidth);
1051c08717dfSBrian Somers 
1052dade2407SBrian Somers   if (arg->bundle->upat) {
1053dade2407SBrian Somers     int secs = time(NULL) - arg->bundle->upat;
1054dade2407SBrian Somers 
1055dade2407SBrian Somers     prompt_Printf(arg->prompt, ", up time %d:%02d:%02d", secs / 3600,
1056dade2407SBrian Somers                   (secs / 60) % 60, secs % 60);
1057dade2407SBrian Somers   }
1058669b9965SBrian Somers   prompt_Printf(arg->prompt, "\n Queued:        %lu of %u\n",
105977fc031dSBrian Somers                 (unsigned long)ip_QueueLen(&arg->bundle->ncp.ipcp),
106077fc031dSBrian Somers                 arg->bundle->cfg.ifqueue);
1061dade2407SBrian Somers 
10626c1d6731SBrian Somers   prompt_Printf(arg->prompt, "\nDefaults:\n");
106374457d3dSBrian Somers   prompt_Printf(arg->prompt, " Label:             %s\n",
106474457d3dSBrian Somers                 arg->bundle->cfg.label);
1065610b185fSBrian Somers   prompt_Printf(arg->prompt, " Auth name:         %s\n",
1066610b185fSBrian Somers                 arg->bundle->cfg.auth.name);
106774457d3dSBrian Somers   prompt_Printf(arg->prompt, " Diagnostic socket: ");
106837b8a5c7SBrian Somers   if (*server.cfg.sockname != '\0') {
106937b8a5c7SBrian Somers     prompt_Printf(arg->prompt, "%s", server.cfg.sockname);
107037b8a5c7SBrian Somers     if (server.cfg.mask != (mode_t)-1)
107137b8a5c7SBrian Somers       prompt_Printf(arg->prompt, ", mask 0%03o", (int)server.cfg.mask);
107237b8a5c7SBrian Somers     prompt_Printf(arg->prompt, "%s\n", server.fd == -1 ? " (not open)" : "");
107337b8a5c7SBrian Somers   } else if (server.cfg.port != 0)
107474457d3dSBrian Somers     prompt_Printf(arg->prompt, "TCP port %d%s\n", server.cfg.port,
107574457d3dSBrian Somers                   server.fd == -1 ? " (not open)" : "");
107674457d3dSBrian Somers   else
107774457d3dSBrian Somers     prompt_Printf(arg->prompt, "none\n");
107804eaa58cSBrian Somers 
10796f8e9f0aSBrian Somers   prompt_Printf(arg->prompt, " Choked Timer:      %ds\n",
10806f8e9f0aSBrian Somers                 arg->bundle->cfg.choked.timeout);
1081972a1bcfSBrian Somers 
1082972a1bcfSBrian Somers #ifndef NORADIUS
1083972a1bcfSBrian Somers   radius_Show(&arg->bundle->radius, arg->prompt);
1084972a1bcfSBrian Somers #endif
1085972a1bcfSBrian Somers 
1086c08717dfSBrian Somers   prompt_Printf(arg->prompt, " Idle Timer:        ");
1087dade2407SBrian Somers   if (arg->bundle->cfg.idle.timeout) {
1088dade2407SBrian Somers     prompt_Printf(arg->prompt, "%ds", arg->bundle->cfg.idle.timeout);
1089dade2407SBrian Somers     if (arg->bundle->cfg.idle.min_timeout)
1090dade2407SBrian Somers       prompt_Printf(arg->prompt, ", min %ds",
1091dade2407SBrian Somers                     arg->bundle->cfg.idle.min_timeout);
1092c08717dfSBrian Somers     remaining = bundle_RemainingIdleTime(arg->bundle);
1093c08717dfSBrian Somers     if (remaining != -1)
1094c08717dfSBrian Somers       prompt_Printf(arg->prompt, " (%ds remaining)", remaining);
1095c08717dfSBrian Somers     prompt_Printf(arg->prompt, "\n");
1096c08717dfSBrian Somers   } else
1097c08717dfSBrian Somers     prompt_Printf(arg->prompt, "disabled\n");
1098c08717dfSBrian Somers 
10993afe5ccbSBrian Somers   prompt_Printf(arg->prompt, " sendpipe:          ");
11003afe5ccbSBrian Somers   if (arg->bundle->ncp.ipcp.cfg.sendpipe > 0)
1101ab2de065SBrian Somers     prompt_Printf(arg->prompt, "%-20ld", arg->bundle->ncp.ipcp.cfg.sendpipe);
11023afe5ccbSBrian Somers   else
1103ab2de065SBrian Somers     prompt_Printf(arg->prompt, "unspecified         ");
11043afe5ccbSBrian Somers   prompt_Printf(arg->prompt, " recvpipe:      ");
11053afe5ccbSBrian Somers   if (arg->bundle->ncp.ipcp.cfg.recvpipe > 0)
11063afe5ccbSBrian Somers     prompt_Printf(arg->prompt, "%ld\n", arg->bundle->ncp.ipcp.cfg.recvpipe);
11073afe5ccbSBrian Somers   else
11083afe5ccbSBrian Somers     prompt_Printf(arg->prompt, "unspecified\n");
11093afe5ccbSBrian Somers 
1110ab2de065SBrian Somers   prompt_Printf(arg->prompt, " Sticky Routes:     %-20.20s",
1111610b185fSBrian Somers                 optval(arg->bundle, OPT_SROUTES));
111298251667SBrian Somers   prompt_Printf(arg->prompt, " Filter Decap:      %s\n",
111398251667SBrian Somers                 optval(arg->bundle, OPT_FILTERDECAP));
111498251667SBrian Somers   prompt_Printf(arg->prompt, " ID check:          %-20.20s",
11151342caedSBrian Somers                 optval(arg->bundle, OPT_IDCHECK));
111698251667SBrian Somers   prompt_Printf(arg->prompt, " Keep-Session:      %s\n",
1117ac685e31SBrian Somers                 optval(arg->bundle, OPT_KEEPSESSION));
111898251667SBrian Somers   prompt_Printf(arg->prompt, " Loopback:          %-20.20s",
11191342caedSBrian Somers                 optval(arg->bundle, OPT_LOOPBACK));
112098251667SBrian Somers   prompt_Printf(arg->prompt, " PasswdAuth:        %s\n",
11211342caedSBrian Somers                 optval(arg->bundle, OPT_PASSWDAUTH));
112298251667SBrian Somers   prompt_Printf(arg->prompt, " Proxy:             %-20.20s",
11231342caedSBrian Somers                 optval(arg->bundle, OPT_PROXY));
112498251667SBrian Somers   prompt_Printf(arg->prompt, " Proxyall:          %s\n",
11253afe5ccbSBrian Somers                 optval(arg->bundle, OPT_PROXYALL));
112694d7be52SBrian Somers   prompt_Printf(arg->prompt, " TCPMSS Fixup:      %-20.20s",
112794d7be52SBrian Somers                 optval(arg->bundle, OPT_TCPMSSFIXUP));
112894d7be52SBrian Somers   prompt_Printf(arg->prompt, " Throughput:        %s\n",
11291342caedSBrian Somers                 optval(arg->bundle, OPT_THROUGHPUT));
113094d7be52SBrian Somers   prompt_Printf(arg->prompt, " Utmp Logging:      %-20.20s",
11311342caedSBrian Somers                 optval(arg->bundle, OPT_UTMP));
11328fa6ebe4SBrian Somers   prompt_Printf(arg->prompt, " Iface-Alias:       %s\n",
11338fa6ebe4SBrian Somers                 optval(arg->bundle, OPT_IFACEALIAS));
11341342caedSBrian Somers 
1135c08717dfSBrian Somers   return 0;
1136c08717dfSBrian Somers }
1137c08717dfSBrian Somers 
1138ab886ad0SBrian Somers static void
1139ab886ad0SBrian Somers bundle_IdleTimeout(void *v)
1140ab886ad0SBrian Somers {
1141ab886ad0SBrian Somers   struct bundle *bundle = (struct bundle *)v;
1142ab886ad0SBrian Somers 
1143b42135deSBrian Somers   log_Printf(LogPHASE, "Idle timer expired\n");
114404eaa58cSBrian Somers   bundle_StopIdleTimer(bundle);
11459c81b87dSBrian Somers   bundle_Close(bundle, NULL, CLOSE_STAYDOWN);
1146ab886ad0SBrian Somers }
1147ab886ad0SBrian Somers 
1148ab886ad0SBrian Somers /*
1149ab886ad0SBrian Somers  *  Start Idle timer. If timeout is reached, we call bundle_Close() to
1150ab886ad0SBrian Somers  *  close LCP and link.
1151ab886ad0SBrian Somers  */
1152ab886ad0SBrian Somers void
11530a4b6c5cSBrian Somers bundle_StartIdleTimer(struct bundle *bundle, unsigned secs)
1154ab886ad0SBrian Somers {
1155dd7e2610SBrian Somers   timer_Stop(&bundle->idle.timer);
1156ff0f9439SBrian Somers   if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) !=
1157dade2407SBrian Somers       bundle->phys_type.open && bundle->cfg.idle.timeout) {
11580a4b6c5cSBrian Somers     time_t now = time(NULL);
1159dade2407SBrian Somers 
11600a4b6c5cSBrian Somers     if (secs == 0)
1161dade2407SBrian Somers       secs = bundle->cfg.idle.timeout;
11620a4b6c5cSBrian Somers 
11630a4b6c5cSBrian Somers     /* We want at least `secs' */
1164dade2407SBrian Somers     if (bundle->cfg.idle.min_timeout > secs && bundle->upat) {
11650a4b6c5cSBrian Somers       int up = now - bundle->upat;
1166dade2407SBrian Somers 
1167dade2407SBrian Somers       if ((long long)bundle->cfg.idle.min_timeout - up > (long long)secs)
11680a4b6c5cSBrian Somers         /* Only increase from the current `remaining' value */
1169dade2407SBrian Somers         secs = bundle->cfg.idle.min_timeout - up;
1170dade2407SBrian Somers     }
117193ee0ff2SBrian Somers     bundle->idle.timer.func = bundle_IdleTimeout;
11723b0f8d2eSBrian Somers     bundle->idle.timer.name = "idle";
1173dade2407SBrian Somers     bundle->idle.timer.load = secs * SECTICKS;
117493ee0ff2SBrian Somers     bundle->idle.timer.arg = bundle;
1175dd7e2610SBrian Somers     timer_Start(&bundle->idle.timer);
11760a4b6c5cSBrian Somers     bundle->idle.done = now + secs;
1177ab886ad0SBrian Somers   }
1178ab886ad0SBrian Somers }
1179ab886ad0SBrian Somers 
1180ab886ad0SBrian Somers void
1181dade2407SBrian Somers bundle_SetIdleTimer(struct bundle *bundle, int timeout, int min_timeout)
1182ab886ad0SBrian Somers {
1183dade2407SBrian Somers   bundle->cfg.idle.timeout = timeout;
1184dade2407SBrian Somers   if (min_timeout >= 0)
1185dade2407SBrian Somers     bundle->cfg.idle.min_timeout = min_timeout;
1186ab886ad0SBrian Somers   if (bundle_LinkIsUp(bundle))
11870a4b6c5cSBrian Somers     bundle_StartIdleTimer(bundle, 0);
1188ab886ad0SBrian Somers }
1189ab886ad0SBrian Somers 
1190ab886ad0SBrian Somers void
1191ab886ad0SBrian Somers bundle_StopIdleTimer(struct bundle *bundle)
1192ab886ad0SBrian Somers {
1193dd7e2610SBrian Somers   timer_Stop(&bundle->idle.timer);
11944a632c80SBrian Somers   bundle->idle.done = 0;
1195ab886ad0SBrian Somers }
1196ab886ad0SBrian Somers 
119704eaa58cSBrian Somers static int
1198ab886ad0SBrian Somers bundle_RemainingIdleTime(struct bundle *bundle)
1199ab886ad0SBrian Somers {
120093ee0ff2SBrian Somers   if (bundle->idle.done)
120193ee0ff2SBrian Somers     return bundle->idle.done - time(NULL);
1202ab886ad0SBrian Somers   return -1;
1203ab886ad0SBrian Somers }
12043b0f8d2eSBrian Somers 
12053b0f8d2eSBrian Somers int
12063b0f8d2eSBrian Somers bundle_IsDead(struct bundle *bundle)
12073b0f8d2eSBrian Somers {
12083b0f8d2eSBrian Somers   return !bundle->links || (bundle->phase == PHASE_DEAD && bundle->CleaningUp);
12093b0f8d2eSBrian Somers }
1210b6217683SBrian Somers 
121104eaa58cSBrian Somers static struct datalink *
121204eaa58cSBrian Somers bundle_DatalinkLinkout(struct bundle *bundle, struct datalink *dl)
1213cd7bd93aSBrian Somers {
1214cd7bd93aSBrian Somers   struct datalink **dlp;
1215cd7bd93aSBrian Somers 
1216cd7bd93aSBrian Somers   for (dlp = &bundle->links; *dlp; dlp = &(*dlp)->next)
1217cd7bd93aSBrian Somers     if (*dlp == dl) {
121804eaa58cSBrian Somers       *dlp = dl->next;
121904eaa58cSBrian Somers       dl->next = NULL;
122004eaa58cSBrian Somers       bundle_LinksRemoved(bundle);
122104eaa58cSBrian Somers       return dl;
1222cd7bd93aSBrian Somers     }
122304eaa58cSBrian Somers 
122404eaa58cSBrian Somers   return NULL;
122504eaa58cSBrian Somers }
122604eaa58cSBrian Somers 
122704eaa58cSBrian Somers static void
122804eaa58cSBrian Somers bundle_DatalinkLinkin(struct bundle *bundle, struct datalink *dl)
122904eaa58cSBrian Somers {
123004eaa58cSBrian Somers   struct datalink **dlp = &bundle->links;
123104eaa58cSBrian Somers 
123204eaa58cSBrian Somers   while (*dlp)
123304eaa58cSBrian Somers     dlp = &(*dlp)->next;
123404eaa58cSBrian Somers 
123504eaa58cSBrian Somers   *dlp = dl;
123604eaa58cSBrian Somers   dl->next = NULL;
123704eaa58cSBrian Somers 
123804eaa58cSBrian Somers   bundle_LinkAdded(bundle, dl);
1239ab2de065SBrian Somers   mp_CheckAutoloadTimer(&bundle->ncp.mp);
1240565e35e5SBrian Somers }
1241565e35e5SBrian Somers 
1242565e35e5SBrian Somers void
1243565e35e5SBrian Somers bundle_CleanDatalinks(struct bundle *bundle)
1244565e35e5SBrian Somers {
1245565e35e5SBrian Somers   struct datalink **dlp = &bundle->links;
124604eaa58cSBrian Somers   int found = 0;
1247565e35e5SBrian Somers 
1248565e35e5SBrian Somers   while (*dlp)
1249565e35e5SBrian Somers     if ((*dlp)->state == DATALINK_CLOSED &&
1250f6a4e748SBrian Somers         (*dlp)->physical->type &
1251f6a4e748SBrian Somers         (PHYS_DIRECT|PHYS_BACKGROUND|PHYS_FOREGROUND)) {
1252565e35e5SBrian Somers       *dlp = datalink_Destroy(*dlp);
125304eaa58cSBrian Somers       found++;
125404eaa58cSBrian Somers     } else
1255565e35e5SBrian Somers       dlp = &(*dlp)->next;
125604eaa58cSBrian Somers 
125704eaa58cSBrian Somers   if (found)
125804eaa58cSBrian Somers     bundle_LinksRemoved(bundle);
125904eaa58cSBrian Somers }
126004eaa58cSBrian Somers 
126104eaa58cSBrian Somers int
126204eaa58cSBrian Somers bundle_DatalinkClone(struct bundle *bundle, struct datalink *dl,
126304eaa58cSBrian Somers                      const char *name)
126404eaa58cSBrian Somers {
126504eaa58cSBrian Somers   if (bundle2datalink(bundle, name)) {
126604eaa58cSBrian Somers     log_Printf(LogWARN, "Clone: %s: name already exists\n", name);
126704eaa58cSBrian Somers     return 0;
126804eaa58cSBrian Somers   }
126904eaa58cSBrian Somers 
127004eaa58cSBrian Somers   bundle_DatalinkLinkin(bundle, datalink_Clone(dl, name));
127104eaa58cSBrian Somers   return 1;
127204eaa58cSBrian Somers }
127304eaa58cSBrian Somers 
127404eaa58cSBrian Somers void
127504eaa58cSBrian Somers bundle_DatalinkRemove(struct bundle *bundle, struct datalink *dl)
127604eaa58cSBrian Somers {
127704eaa58cSBrian Somers   dl = bundle_DatalinkLinkout(bundle, dl);
127804eaa58cSBrian Somers   if (dl)
127904eaa58cSBrian Somers     datalink_Destroy(dl);
1280cd7bd93aSBrian Somers }
128149052c95SBrian Somers 
128249052c95SBrian Somers void
128349052c95SBrian Somers bundle_SetLabel(struct bundle *bundle, const char *label)
128449052c95SBrian Somers {
128549052c95SBrian Somers   if (label)
128649052c95SBrian Somers     strncpy(bundle->cfg.label, label, sizeof bundle->cfg.label - 1);
128749052c95SBrian Somers   else
128849052c95SBrian Somers     *bundle->cfg.label = '\0';
128949052c95SBrian Somers }
129049052c95SBrian Somers 
129149052c95SBrian Somers const char *
129249052c95SBrian Somers bundle_GetLabel(struct bundle *bundle)
129349052c95SBrian Somers {
129449052c95SBrian Somers   return *bundle->cfg.label ? bundle->cfg.label : NULL;
129549052c95SBrian Somers }
12961fa665f5SBrian Somers 
12972cb305afSBrian Somers int
12982cb305afSBrian Somers bundle_LinkSize()
12991fa665f5SBrian Somers {
130096c9bb21SBrian Somers   struct iovec iov[SCATTER_SEGMENTS];
13012cb305afSBrian Somers   int niov, expect, f;
130287c3786eSBrian Somers 
130396c9bb21SBrian Somers   iov[0].iov_len = strlen(Version) + 1;
13042cb305afSBrian Somers   iov[0].iov_base = NULL;
13052cb305afSBrian Somers   niov = 1;
13062cb305afSBrian Somers   if (datalink2iov(NULL, iov, &niov, SCATTER_SEGMENTS, NULL, NULL) == -1) {
13072cb305afSBrian Somers     log_Printf(LogERROR, "Cannot determine space required for link\n");
13082cb305afSBrian Somers     return 0;
130954cd8e13SBrian Somers   }
13106f384573SBrian Somers 
131196c9bb21SBrian Somers   for (f = expect = 0; f < niov; f++)
131296c9bb21SBrian Somers     expect += iov[f].iov_len;
131396c9bb21SBrian Somers 
13142cb305afSBrian Somers   return expect;
131587c3786eSBrian Somers }
131696c9bb21SBrian Somers 
13172cb305afSBrian Somers void
13182cb305afSBrian Somers bundle_ReceiveDatalink(struct bundle *bundle, int s)
13192cb305afSBrian Somers {
13202cb305afSBrian Somers   char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int) * SEND_MAXFD];
1321cbee9754SBrian Somers   int niov, expect, f, *fd, nfd, onfd, got;
13222cb305afSBrian Somers   struct iovec iov[SCATTER_SEGMENTS];
13232cb305afSBrian Somers   struct cmsghdr *cmsg;
13242cb305afSBrian Somers   struct msghdr msg;
13252cb305afSBrian Somers   struct datalink *dl;
13262cb305afSBrian Somers   pid_t pid;
13272cb305afSBrian Somers 
13282cb305afSBrian Somers   log_Printf(LogPHASE, "Receiving datalink\n");
13292cb305afSBrian Somers 
13302cb305afSBrian Somers   /*
13312cb305afSBrian Somers    * Create our scatter/gather array - passing NULL gets the space
13322cb305afSBrian Somers    * allocation requirement rather than actually flattening the
13332cb305afSBrian Somers    * structures.
13342cb305afSBrian Somers    */
13352cb305afSBrian Somers   iov[0].iov_len = strlen(Version) + 1;
13362cb305afSBrian Somers   iov[0].iov_base = NULL;
13372cb305afSBrian Somers   niov = 1;
13382cb305afSBrian Somers   if (datalink2iov(NULL, iov, &niov, SCATTER_SEGMENTS, NULL, NULL) == -1) {
13392cb305afSBrian Somers     log_Printf(LogERROR, "Cannot determine space required for link\n");
13402cb305afSBrian Somers     return;
13412cb305afSBrian Somers   }
13422cb305afSBrian Somers 
13432cb305afSBrian Somers   /* Allocate the scatter/gather array for recvmsg() */
13442cb305afSBrian Somers   for (f = expect = 0; f < niov; f++) {
13452cb305afSBrian Somers     if ((iov[f].iov_base = malloc(iov[f].iov_len)) == NULL) {
13462cb305afSBrian Somers       log_Printf(LogERROR, "Cannot allocate space to receive link\n");
13472cb305afSBrian Somers       return;
13482cb305afSBrian Somers     }
1349cbee9754SBrian Somers     if (f)
13502cb305afSBrian Somers       expect += iov[f].iov_len;
13512cb305afSBrian Somers   }
13522cb305afSBrian Somers 
13532cb305afSBrian Somers   /* Set up our message */
13542cb305afSBrian Somers   cmsg = (struct cmsghdr *)cmsgbuf;
13552cb305afSBrian Somers   cmsg->cmsg_len = sizeof cmsgbuf;
13562cb305afSBrian Somers   cmsg->cmsg_level = SOL_SOCKET;
13572cb305afSBrian Somers   cmsg->cmsg_type = 0;
13582cb305afSBrian Somers 
135996c9bb21SBrian Somers   memset(&msg, '\0', sizeof msg);
13602cb305afSBrian Somers   msg.msg_name = NULL;
13612cb305afSBrian Somers   msg.msg_namelen = 0;
136296c9bb21SBrian Somers   msg.msg_iov = iov;
1363cbee9754SBrian Somers   msg.msg_iovlen = 1;		/* Only send the version at the first pass */
136496c9bb21SBrian Somers   msg.msg_control = cmsgbuf;
136596c9bb21SBrian Somers   msg.msg_controllen = sizeof cmsgbuf;
136696c9bb21SBrian Somers 
1367209dc102SBrian Somers   log_Printf(LogDEBUG, "Expecting %u scatter/gather bytes\n",
1368209dc102SBrian Somers              (unsigned)iov[0].iov_len);
13692cb305afSBrian Somers 
1370cbee9754SBrian Somers   if ((got = recvmsg(s, &msg, MSG_WAITALL)) != iov[0].iov_len) {
1371cbee9754SBrian Somers     if (got == -1)
137296c9bb21SBrian Somers       log_Printf(LogERROR, "Failed recvmsg: %s\n", strerror(errno));
137396c9bb21SBrian Somers     else
1374209dc102SBrian Somers       log_Printf(LogERROR, "Failed recvmsg: Got %d, not %u\n",
1375209dc102SBrian Somers                  got, (unsigned)iov[0].iov_len);
137696c9bb21SBrian Somers     while (niov--)
137796c9bb21SBrian Somers       free(iov[niov].iov_base);
137896c9bb21SBrian Somers     return;
137996c9bb21SBrian Somers   }
138096c9bb21SBrian Somers 
13812cb305afSBrian Somers   if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
13822cb305afSBrian Somers     log_Printf(LogERROR, "Recvmsg: no descriptors received !\n");
13832cb305afSBrian Somers     while (niov--)
13842cb305afSBrian Somers       free(iov[niov].iov_base);
13852cb305afSBrian Somers     return;
138687c3786eSBrian Somers   }
138787c3786eSBrian Somers 
13882cb305afSBrian Somers   fd = (int *)(cmsg + 1);
13892cb305afSBrian Somers   nfd = (cmsg->cmsg_len - sizeof *cmsg) / sizeof(int);
13902cb305afSBrian Somers 
13912cb305afSBrian Somers   if (nfd < 2) {
13928e7bd08eSBrian Somers     log_Printf(LogERROR, "Recvmsg: %d descriptor%s received (too few) !\n",
13932cb305afSBrian Somers                nfd, nfd == 1 ? "" : "s");
13942cb305afSBrian Somers     while (nfd--)
13952cb305afSBrian Somers       close(fd[nfd]);
1396ad5b0e8bSBrian Somers     while (niov--)
1397ad5b0e8bSBrian Somers       free(iov[niov].iov_base);
1398ad5b0e8bSBrian Somers     return;
139954cd8e13SBrian Somers   }
140054cd8e13SBrian Somers 
140187c3786eSBrian Somers   /*
1402cbee9754SBrian Somers    * We've successfully received two or more open file descriptors
1403cbee9754SBrian Somers    * through our socket, plus a version string.  Make sure it's the
1404cbee9754SBrian Somers    * correct version, and drop the connection if it's not.
140587c3786eSBrian Somers    */
140696c9bb21SBrian Somers   if (strncmp(Version, iov[0].iov_base, iov[0].iov_len)) {
140796c9bb21SBrian Somers     log_Printf(LogWARN, "Cannot receive datalink, incorrect version"
140896c9bb21SBrian Somers                " (\"%.*s\", not \"%s\")\n", (int)iov[0].iov_len,
14097d81ddf5SBrian Somers                (char *)iov[0].iov_base, Version);
14102cb305afSBrian Somers     while (nfd--)
14112cb305afSBrian Somers       close(fd[nfd]);
141296c9bb21SBrian Somers     while (niov--)
141396c9bb21SBrian Somers       free(iov[niov].iov_base);
141496c9bb21SBrian Somers     return;
141596c9bb21SBrian Somers   }
141696c9bb21SBrian Somers 
1417cbee9754SBrian Somers   /*
1418cbee9754SBrian Somers    * Everything looks good.  Send the other side our process id so that
1419cbee9754SBrian Somers    * they can transfer lock ownership, and wait for them to send the
1420cbee9754SBrian Somers    * actual link data.
1421cbee9754SBrian Somers    */
1422cbee9754SBrian Somers   pid = getpid();
1423cbee9754SBrian Somers   if ((got = write(fd[1], &pid, sizeof pid)) != sizeof pid) {
1424cbee9754SBrian Somers     if (got == -1)
1425cbee9754SBrian Somers       log_Printf(LogERROR, "Failed write: %s\n", strerror(errno));
1426cbee9754SBrian Somers     else
1427cbee9754SBrian Somers       log_Printf(LogERROR, "Failed write: Got %d, not %d\n", got,
1428cbee9754SBrian Somers                  (int)(sizeof pid));
1429cbee9754SBrian Somers     while (nfd--)
1430cbee9754SBrian Somers       close(fd[nfd]);
1431cbee9754SBrian Somers     while (niov--)
1432cbee9754SBrian Somers       free(iov[niov].iov_base);
1433cbee9754SBrian Somers     return;
1434cbee9754SBrian Somers   }
1435cbee9754SBrian Somers 
1436cbee9754SBrian Somers   if ((got = readv(fd[1], iov + 1, niov - 1)) != expect) {
1437cbee9754SBrian Somers     if (got == -1)
1438cbee9754SBrian Somers       log_Printf(LogERROR, "Failed write: %s\n", strerror(errno));
1439cbee9754SBrian Somers     else
1440cbee9754SBrian Somers       log_Printf(LogERROR, "Failed write: Got %d, not %d\n", got, expect);
1441cbee9754SBrian Somers     while (nfd--)
1442cbee9754SBrian Somers       close(fd[nfd]);
1443cbee9754SBrian Somers     while (niov--)
1444cbee9754SBrian Somers       free(iov[niov].iov_base);
1445cbee9754SBrian Somers     return;
1446cbee9754SBrian Somers   }
1447cbee9754SBrian Somers   close(fd[1]);
1448cbee9754SBrian Somers 
14492cb305afSBrian Somers   onfd = nfd;	/* We've got this many in our array */
14508e7bd08eSBrian Somers   nfd -= 2;	/* Don't include p->fd and our reply descriptor */
14512cb305afSBrian Somers   niov = 1;	/* Skip the version id */
145287c3786eSBrian Somers   dl = iov2datalink(bundle, iov, &niov, sizeof iov / sizeof *iov, fd[0],
14532cb305afSBrian Somers                     fd + 2, &nfd);
1454b7c5748eSBrian Somers   if (dl) {
14552cb305afSBrian Somers 
145687c3786eSBrian Somers     if (nfd) {
145787c3786eSBrian Somers       log_Printf(LogERROR, "bundle_ReceiveDatalink: Failed to handle %d "
14582cb305afSBrian Somers                  "auxiliary file descriptors (%d remain)\n", onfd, nfd);
145987c3786eSBrian Somers       datalink_Destroy(dl);
146087c3786eSBrian Somers       while (nfd--)
146187c3786eSBrian Somers         close(fd[onfd--]);
14622cb305afSBrian Somers       close(fd[0]);
146387c3786eSBrian Somers     } else {
146404eaa58cSBrian Somers       bundle_DatalinkLinkin(bundle, dl);
1465b7c5748eSBrian Somers       datalink_AuthOk(dl);
1466ab2de065SBrian Somers       bundle_CalculateBandwidth(dl->bundle);
146787c3786eSBrian Somers     }
146887c3786eSBrian Somers   } else {
146987c3786eSBrian Somers     while (nfd--)
147087c3786eSBrian Somers       close(fd[onfd--]);
147187c3786eSBrian Somers     close(fd[0]);
14722cb305afSBrian Somers     close(fd[1]);
147387c3786eSBrian Somers   }
147496c9bb21SBrian Somers 
147596c9bb21SBrian Somers   free(iov[0].iov_base);
14761fa665f5SBrian Somers }
14771fa665f5SBrian Somers 
14781fa665f5SBrian Somers void
147996c9bb21SBrian Somers bundle_SendDatalink(struct datalink *dl, int s, struct sockaddr_un *sun)
14801fa665f5SBrian Somers {
14812cb305afSBrian Somers   char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int) * SEND_MAXFD];
14822cb305afSBrian Somers   const char *constlock;
14832cb305afSBrian Somers   char *lock;
148487c3786eSBrian Somers   struct cmsghdr *cmsg;
148596c9bb21SBrian Somers   struct msghdr msg;
148696c9bb21SBrian Somers   struct iovec iov[SCATTER_SEGMENTS];
14872cb305afSBrian Somers   int niov, f, expect, newsid, fd[SEND_MAXFD], nfd, reply[2], got;
148885fd273aSBrian Somers   pid_t newpid;
14896f384573SBrian Somers 
1490dd7e2610SBrian Somers   log_Printf(LogPHASE, "Transmitting datalink %s\n", dl->name);
14916f384573SBrian Somers 
14922cb305afSBrian Somers   /* Record the base device name for a lock transfer later */
14932cb305afSBrian Somers   constlock = physical_LockedDevice(dl->physical);
14942cb305afSBrian Somers   if (constlock) {
14952cb305afSBrian Somers     lock = alloca(strlen(constlock) + 1);
14962cb305afSBrian Somers     strcpy(lock, constlock);
14972cb305afSBrian Somers   } else
14982cb305afSBrian Somers     lock = NULL;
14992cb305afSBrian Somers 
150004eaa58cSBrian Somers   bundle_LinkClosed(dl->bundle, dl);
15010f2f3eb3SBrian Somers   bundle_DatalinkLinkout(dl->bundle, dl);
1502ea722969SBrian Somers 
150396c9bb21SBrian Somers   /* Build our scatter/gather array */
150496c9bb21SBrian Somers   iov[0].iov_len = strlen(Version) + 1;
150596c9bb21SBrian Somers   iov[0].iov_base = strdup(Version);
150696c9bb21SBrian Somers   niov = 1;
150787c3786eSBrian Somers   nfd = 0;
15086f384573SBrian Somers 
15092cb305afSBrian Somers   fd[0] = datalink2iov(dl, iov, &niov, SCATTER_SEGMENTS, fd + 2, &nfd);
15106f384573SBrian Somers 
15112cb305afSBrian Somers   if (fd[0] != -1 && socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, reply) != -1) {
15122cb305afSBrian Somers     /*
15132cb305afSBrian Somers      * fd[1] is used to get the peer process id back, then to confirm that
15142cb305afSBrian Somers      * we've transferred any device locks to that process id.
15152cb305afSBrian Somers      */
15162cb305afSBrian Somers     fd[1] = reply[1];
151787c3786eSBrian Somers 
15182cb305afSBrian Somers     nfd += 2;			/* Include fd[0] and fd[1] */
151996c9bb21SBrian Somers     memset(&msg, '\0', sizeof msg);
152054cd8e13SBrian Somers 
15212cb305afSBrian Somers     msg.msg_name = NULL;
15222cb305afSBrian Somers     msg.msg_namelen = 0;
1523cbee9754SBrian Somers     /*
1524cbee9754SBrian Somers      * Only send the version to start...  We used to send the whole lot, but
1525cbee9754SBrian Somers      * this caused problems with our RECVBUF size as a single link is about
1526cbee9754SBrian Somers      * 22k !  This way, we should bump into no limits.
1527cbee9754SBrian Somers      */
1528cbee9754SBrian Somers     msg.msg_iovlen = 1;
152996c9bb21SBrian Somers     msg.msg_iov = iov;
15302cb305afSBrian Somers     msg.msg_control = cmsgbuf;
15312cb305afSBrian Somers     msg.msg_controllen = sizeof *cmsg + sizeof(int) * nfd;
15322cb305afSBrian Somers     msg.msg_flags = 0;
153354cd8e13SBrian Somers 
15342cb305afSBrian Somers     cmsg = (struct cmsghdr *)cmsgbuf;
15352cb305afSBrian Somers     cmsg->cmsg_len = msg.msg_controllen;
153654cd8e13SBrian Somers     cmsg->cmsg_level = SOL_SOCKET;
153754cd8e13SBrian Somers     cmsg->cmsg_type = SCM_RIGHTS;
153887c3786eSBrian Somers 
15392cb305afSBrian Somers     for (f = 0; f < nfd; f++)
15402cb305afSBrian Somers       *((int *)(cmsg + 1) + f) = fd[f];
154147723d29SBrian Somers 
1542cbee9754SBrian Somers     for (f = 1, expect = 0; f < niov; f++)
154396c9bb21SBrian Somers       expect += iov[f].iov_len;
154447723d29SBrian Somers 
1545cbee9754SBrian Somers     if (setsockopt(reply[0], SOL_SOCKET, SO_SNDBUF, &expect, sizeof(int)) == -1)
1546cbee9754SBrian Somers       log_Printf(LogERROR, "setsockopt(SO_RCVBUF, %d): %s\n", expect,
1547cbee9754SBrian Somers                  strerror(errno));
1548cbee9754SBrian Somers     if (setsockopt(reply[1], SOL_SOCKET, SO_RCVBUF, &expect, sizeof(int)) == -1)
1549cbee9754SBrian Somers       log_Printf(LogERROR, "setsockopt(SO_RCVBUF, %d): %s\n", expect,
1550cbee9754SBrian Somers                  strerror(errno));
1551cbee9754SBrian Somers 
1552209dc102SBrian Somers     log_Printf(LogDEBUG, "Sending %d descriptor%s and %u bytes in scatter"
1553209dc102SBrian Somers                "/gather array\n", nfd, nfd == 1 ? "" : "s",
1554209dc102SBrian Somers                (unsigned)iov[0].iov_len);
155547723d29SBrian Somers 
15562cb305afSBrian Somers     if ((got = sendmsg(s, &msg, 0)) == -1)
15572cb305afSBrian Somers       log_Printf(LogERROR, "Failed sendmsg: %s: %s\n",
15582cb305afSBrian Somers                  sun->sun_path, strerror(errno));
1559cbee9754SBrian Somers     else if (got != iov[0].iov_len)
1560209dc102SBrian Somers       log_Printf(LogERROR, "%s: Failed initial sendmsg: Only sent %d of %u\n",
1561209dc102SBrian Somers                  sun->sun_path, got, (unsigned)iov[0].iov_len);
15622cb305afSBrian Somers     else {
15638e7bd08eSBrian Somers       /* We must get the ACK before closing the descriptor ! */
15642cb305afSBrian Somers       int res;
15652cb305afSBrian Somers 
1566cbee9754SBrian Somers       if ((got = read(reply[0], &newpid, sizeof newpid)) == sizeof newpid) {
1567cbee9754SBrian Somers         log_Printf(LogDEBUG, "Received confirmation from pid %d\n",
1568cbee9754SBrian Somers                    (int)newpid);
15692cb305afSBrian Somers         if (lock && (res = ID0uu_lock_txfr(lock, newpid)) != UU_LOCK_OK)
1570b42135deSBrian Somers             log_Printf(LogERROR, "uu_lock_txfr: %s\n", uu_lockerr(res));
15712cb305afSBrian Somers 
1572cbee9754SBrian Somers         log_Printf(LogDEBUG, "Transmitting link (%d bytes)\n", expect);
1573cbee9754SBrian Somers         if ((got = writev(reply[0], iov + 1, niov - 1)) != expect) {
1574cbee9754SBrian Somers           if (got == -1)
1575cbee9754SBrian Somers             log_Printf(LogERROR, "%s: Failed writev: %s\n",
1576cbee9754SBrian Somers                        sun->sun_path, strerror(errno));
1577cbee9754SBrian Somers           else
1578cbee9754SBrian Somers             log_Printf(LogERROR, "%s: Failed writev: Wrote %d of %d\n",
1579cbee9754SBrian Somers                        sun->sun_path, got, expect);
1580cbee9754SBrian Somers         }
1581cbee9754SBrian Somers       } else if (got == -1)
1582cbee9754SBrian Somers         log_Printf(LogERROR, "%s: Failed socketpair read: %s\n",
1583cbee9754SBrian Somers                    sun->sun_path, strerror(errno));
1584cbee9754SBrian Somers       else
1585cbee9754SBrian Somers         log_Printf(LogERROR, "%s: Failed socketpair read: Got %d of %d\n",
1586cbee9754SBrian Somers                    sun->sun_path, got, (int)(sizeof newpid));
15872cb305afSBrian Somers     }
15882cb305afSBrian Somers 
15892cb305afSBrian Somers     close(reply[0]);
15902cb305afSBrian Somers     close(reply[1]);
159154cd8e13SBrian Somers 
1592ac685e31SBrian Somers     newsid = Enabled(dl->bundle, OPT_KEEPSESSION) ||
159387c3786eSBrian Somers              tcgetpgrp(fd[0]) == getpgrp();
159487c3786eSBrian Somers     while (nfd)
159587c3786eSBrian Somers       close(fd[--nfd]);
15961384bd27SBrian Somers     if (newsid)
15972cb305afSBrian Somers       bundle_setsid(dl->bundle, got != -1);
159847723d29SBrian Somers   }
159985fd273aSBrian Somers   close(s);
160096c9bb21SBrian Somers 
160196c9bb21SBrian Somers   while (niov--)
160296c9bb21SBrian Somers     free(iov[niov].iov_base);
16031fa665f5SBrian Somers }
1604dd0645c5SBrian Somers 
1605dd0645c5SBrian Somers int
160658d55334SBrian Somers bundle_RenameDatalink(struct bundle *bundle, struct datalink *ndl,
160758d55334SBrian Somers                       const char *name)
160858d55334SBrian Somers {
160958d55334SBrian Somers   struct datalink *dl;
161058d55334SBrian Somers 
161158d55334SBrian Somers   if (!strcasecmp(ndl->name, name))
161258d55334SBrian Somers     return 1;
161358d55334SBrian Somers 
161458d55334SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
161558d55334SBrian Somers     if (!strcasecmp(dl->name, name))
161658d55334SBrian Somers       return 0;
161758d55334SBrian Somers 
161858d55334SBrian Somers   datalink_Rename(ndl, name);
161958d55334SBrian Somers   return 1;
162058d55334SBrian Somers }
162158d55334SBrian Somers 
162258d55334SBrian Somers int
1623dd0645c5SBrian Somers bundle_SetMode(struct bundle *bundle, struct datalink *dl, int mode)
1624dd0645c5SBrian Somers {
1625dd0645c5SBrian Somers   int omode;
1626dd0645c5SBrian Somers 
1627dd0645c5SBrian Somers   omode = dl->physical->type;
1628dd0645c5SBrian Somers   if (omode == mode)
1629dd0645c5SBrian Somers     return 1;
1630dd0645c5SBrian Somers 
1631ff0f9439SBrian Somers   if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO))
1632ff0f9439SBrian Somers     /* First auto link */
1633dd0645c5SBrian Somers     if (bundle->ncp.ipcp.peer_ip.s_addr == INADDR_ANY) {
1634ff0f9439SBrian Somers       log_Printf(LogWARN, "You must `set ifaddr' or `open' before"
1635ff0f9439SBrian Somers                  " changing mode to %s\n", mode2Nam(mode));
1636dd0645c5SBrian Somers       return 0;
1637dd0645c5SBrian Somers     }
1638dd0645c5SBrian Somers 
1639dd0645c5SBrian Somers   if (!datalink_SetMode(dl, mode))
1640dd0645c5SBrian Somers     return 0;
1641dd0645c5SBrian Somers 
1642ff0f9439SBrian Somers   if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO) &&
1643ff0f9439SBrian Somers       bundle->phase != PHASE_NETWORK)
1644ff0f9439SBrian Somers     /* First auto link, we need an interface */
1645dd0645c5SBrian Somers     ipcp_InterfaceUp(&bundle->ncp.ipcp);
1646dd0645c5SBrian Somers 
1647ab2de065SBrian Somers   /* Regenerate phys_type and adjust idle timer */
164804eaa58cSBrian Somers   bundle_LinksRemoved(bundle);
1649dd0645c5SBrian Somers 
1650dd0645c5SBrian Somers   return 1;
1651dd0645c5SBrian Somers }
16521384bd27SBrian Somers 
16531384bd27SBrian Somers void
16541384bd27SBrian Somers bundle_setsid(struct bundle *bundle, int holdsession)
16551384bd27SBrian Somers {
16561384bd27SBrian Somers   /*
16571384bd27SBrian Somers    * Lose the current session.  This means getting rid of our pid
16581384bd27SBrian Somers    * too so that the tty device will really go away, and any getty
16591384bd27SBrian Somers    * etc will be allowed to restart.
16601384bd27SBrian Somers    */
16611384bd27SBrian Somers   pid_t pid, orig;
16621384bd27SBrian Somers   int fds[2];
16631384bd27SBrian Somers   char done;
16641384bd27SBrian Somers   struct datalink *dl;
16651384bd27SBrian Somers 
1666e62ce959SBrian Somers   if (!holdsession && bundle_IsDead(bundle)) {
1667e62ce959SBrian Somers     /*
1668e62ce959SBrian Somers      * No need to lose our session after all... we're going away anyway
1669e62ce959SBrian Somers      *
1670e62ce959SBrian Somers      * We should really stop the timer and pause if holdsession is set and
1671e62ce959SBrian Somers      * the bundle's dead, but that leaves other resources lying about :-(
1672e62ce959SBrian Somers      */
1673e62ce959SBrian Somers     return;
1674e62ce959SBrian Somers   }
1675e62ce959SBrian Somers 
16761384bd27SBrian Somers   orig = getpid();
16771384bd27SBrian Somers   if (pipe(fds) == -1) {
16781384bd27SBrian Somers     log_Printf(LogERROR, "pipe: %s\n", strerror(errno));
16791384bd27SBrian Somers     return;
16801384bd27SBrian Somers   }
16811384bd27SBrian Somers   switch ((pid = fork())) {
16821384bd27SBrian Somers     case -1:
16831384bd27SBrian Somers       log_Printf(LogERROR, "fork: %s\n", strerror(errno));
16841384bd27SBrian Somers       close(fds[0]);
16851384bd27SBrian Somers       close(fds[1]);
16861384bd27SBrian Somers       return;
16871384bd27SBrian Somers     case 0:
16881384bd27SBrian Somers       close(fds[1]);
16894be0e57dSBrian Somers       read(fds[0], &done, 1);		/* uu_locks are mine ! */
16904be0e57dSBrian Somers       close(fds[0]);
16911384bd27SBrian Somers       if (pipe(fds) == -1) {
16921384bd27SBrian Somers         log_Printf(LogERROR, "pipe(2): %s\n", strerror(errno));
16931384bd27SBrian Somers         return;
16941384bd27SBrian Somers       }
16951384bd27SBrian Somers       switch ((pid = fork())) {
16961384bd27SBrian Somers         case -1:
1697a33b2ef7SBrian Somers           log_Printf(LogERROR, "fork(2): %s\n", strerror(errno));
16981384bd27SBrian Somers           close(fds[0]);
16991384bd27SBrian Somers           close(fds[1]);
17001384bd27SBrian Somers           return;
17011384bd27SBrian Somers         case 0:
17021384bd27SBrian Somers           close(fds[1]);
17034be0e57dSBrian Somers           bundle_LockTun(bundle);	/* update pid */
17044be0e57dSBrian Somers           read(fds[0], &done, 1);	/* uu_locks are mine ! */
17054be0e57dSBrian Somers           close(fds[0]);
17061384bd27SBrian Somers           setsid();
170706b47306SBrian Somers           bundle_ChangedPID(bundle);
1708b42135deSBrian Somers           log_Printf(LogDEBUG, "%d -> %d: %s session control\n",
17091384bd27SBrian Somers                      (int)orig, (int)getpid(),
17101384bd27SBrian Somers                      holdsession ? "Passed" : "Dropped");
17117e778f13SBrian Somers           timer_InitService(0);		/* Start the Timer Service */
17121384bd27SBrian Somers           break;
17131384bd27SBrian Somers         default:
17144be0e57dSBrian Somers           close(fds[0]);
17155d9e6103SBrian Somers           /* Give away all our physical locks (to the final process) */
17161384bd27SBrian Somers           for (dl = bundle->links; dl; dl = dl->next)
17171384bd27SBrian Somers             if (dl->state != DATALINK_CLOSED)
17185d9e6103SBrian Somers               physical_ChangedPid(dl->physical, pid);
17194be0e57dSBrian Somers           write(fds[1], "!", 1);	/* done */
17204be0e57dSBrian Somers           close(fds[1]);
17212cb305afSBrian Somers           _exit(0);
17221384bd27SBrian Somers           break;
17231384bd27SBrian Somers       }
17241384bd27SBrian Somers       break;
17251384bd27SBrian Somers     default:
17264be0e57dSBrian Somers       close(fds[0]);
17275d9e6103SBrian Somers       /* Give away all our physical locks (to the intermediate process) */
17281384bd27SBrian Somers       for (dl = bundle->links; dl; dl = dl->next)
17291384bd27SBrian Somers         if (dl->state != DATALINK_CLOSED)
17305d9e6103SBrian Somers           physical_ChangedPid(dl->physical, pid);
17314be0e57dSBrian Somers       write(fds[1], "!", 1);	/* done */
17324be0e57dSBrian Somers       close(fds[1]);
17331384bd27SBrian Somers       if (holdsession) {
17341384bd27SBrian Somers         int fd, status;
17351384bd27SBrian Somers 
17361384bd27SBrian Somers         timer_TermService();
17371384bd27SBrian Somers         signal(SIGPIPE, SIG_DFL);
17381384bd27SBrian Somers         signal(SIGALRM, SIG_DFL);
17391384bd27SBrian Somers         signal(SIGHUP, SIG_DFL);
17401384bd27SBrian Somers         signal(SIGTERM, SIG_DFL);
17411384bd27SBrian Somers         signal(SIGINT, SIG_DFL);
17421384bd27SBrian Somers         signal(SIGQUIT, SIG_DFL);
17431384bd27SBrian Somers         for (fd = getdtablesize(); fd >= 0; fd--)
17441384bd27SBrian Somers           close(fd);
17451384bd27SBrian Somers         /*
17461384bd27SBrian Somers          * Reap the intermediate process.  As we're not exiting but the
17471384bd27SBrian Somers          * intermediate is, we don't want it to become defunct.
17481384bd27SBrian Somers          */
17491384bd27SBrian Somers         waitpid(pid, &status, 0);
17508e7b8599SBrian Somers         /* Tweak our process arguments.... */
1751ebe96675SBrian Somers         SetTitle("session owner");
175268602c3eSBrian Somers #ifndef NOSUID
1753a19a5c02SBrian Somers         setuid(ID0realuid());
175468602c3eSBrian Somers #endif
17551384bd27SBrian Somers         /*
17561384bd27SBrian Somers          * Hang around for a HUP.  This should happen as soon as the
17578e7bd08eSBrian Somers          * ppp that we passed our ctty descriptor to closes it.
17588e7bd08eSBrian Somers          * NOTE: If this process dies, the passed descriptor becomes
17591384bd27SBrian Somers          *       invalid and will give a select() error by setting one
17601384bd27SBrian Somers          *       of the error fds, aborting the other ppp.  We don't
17611384bd27SBrian Somers          *       want that to happen !
17621384bd27SBrian Somers          */
17631384bd27SBrian Somers         pause();
17641384bd27SBrian Somers       }
17652cb305afSBrian Somers       _exit(0);
17661384bd27SBrian Somers       break;
17671384bd27SBrian Somers   }
17681384bd27SBrian Somers }
17699b5f8ffdSBrian Somers 
17709b5f8ffdSBrian Somers int
17719b5f8ffdSBrian Somers bundle_HighestState(struct bundle *bundle)
17729b5f8ffdSBrian Somers {
17739b5f8ffdSBrian Somers   struct datalink *dl;
17749b5f8ffdSBrian Somers   int result = DATALINK_CLOSED;
17759b5f8ffdSBrian Somers 
17769b5f8ffdSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
17779b5f8ffdSBrian Somers     if (result < dl->state)
17789b5f8ffdSBrian Somers       result = dl->state;
17799b5f8ffdSBrian Somers 
17809b5f8ffdSBrian Somers   return result;
17819b5f8ffdSBrian Somers }
1782991c2a7bSBrian Somers 
1783991c2a7bSBrian Somers int
1784991c2a7bSBrian Somers bundle_Exception(struct bundle *bundle, int fd)
1785991c2a7bSBrian Somers {
1786991c2a7bSBrian Somers   struct datalink *dl;
1787991c2a7bSBrian Somers 
1788991c2a7bSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
1789991c2a7bSBrian Somers     if (dl->physical->fd == fd) {
1790991c2a7bSBrian Somers       datalink_Down(dl, CLOSE_NORMAL);
1791991c2a7bSBrian Somers       return 1;
1792991c2a7bSBrian Somers     }
1793991c2a7bSBrian Somers 
1794991c2a7bSBrian Somers   return 0;
1795991c2a7bSBrian Somers }
17961d1fc017SBrian Somers 
17971d1fc017SBrian Somers void
17981d1fc017SBrian Somers bundle_AdjustFilters(struct bundle *bundle, struct in_addr *my_ip,
17991d1fc017SBrian Somers                      struct in_addr *peer_ip)
18001d1fc017SBrian Somers {
1801d568d6c4SBrian Somers   filter_AdjustAddr(&bundle->filter.in, my_ip, peer_ip, NULL);
1802d568d6c4SBrian Somers   filter_AdjustAddr(&bundle->filter.out, my_ip, peer_ip, NULL);
1803d568d6c4SBrian Somers   filter_AdjustAddr(&bundle->filter.dial, my_ip, peer_ip, NULL);
1804d568d6c4SBrian Somers   filter_AdjustAddr(&bundle->filter.alive, my_ip, peer_ip, NULL);
1805d568d6c4SBrian Somers }
1806d568d6c4SBrian Somers 
1807d568d6c4SBrian Somers void
1808d568d6c4SBrian Somers bundle_AdjustDNS(struct bundle *bundle, struct in_addr dns[2])
1809d568d6c4SBrian Somers {
1810d568d6c4SBrian Somers   filter_AdjustAddr(&bundle->filter.in, NULL, NULL, dns);
1811d568d6c4SBrian Somers   filter_AdjustAddr(&bundle->filter.out, NULL, NULL, dns);
1812d568d6c4SBrian Somers   filter_AdjustAddr(&bundle->filter.dial, NULL, NULL, dns);
1813d568d6c4SBrian Somers   filter_AdjustAddr(&bundle->filter.alive, NULL, NULL, dns);
18141d1fc017SBrian Somers }
1815ab2de065SBrian Somers 
1816ab2de065SBrian Somers void
1817ab2de065SBrian Somers bundle_CalculateBandwidth(struct bundle *bundle)
1818ab2de065SBrian Somers {
1819ab2de065SBrian Somers   struct datalink *dl;
18206301d506SBrian Somers   int sp, overhead, maxoverhead;
1821ab2de065SBrian Somers 
1822ab2de065SBrian Somers   bundle->bandwidth = 0;
1823c8b9fb53SBrian Somers   bundle->iface->mtu = 0;
18246301d506SBrian Somers   maxoverhead = 0;
18256301d506SBrian Somers 
18266301d506SBrian Somers   for (dl = bundle->links; dl; dl = dl->next) {
18276301d506SBrian Somers     overhead = ccp_MTUOverhead(&dl->physical->link.ccp);
18286301d506SBrian Somers     if (maxoverhead < overhead)
18296301d506SBrian Somers       maxoverhead = overhead;
1830ab2de065SBrian Somers     if (dl->state == DATALINK_OPEN) {
1831ab2de065SBrian Somers       if ((sp = dl->mp.bandwidth) == 0 &&
1832ab2de065SBrian Somers           (sp = physical_GetSpeed(dl->physical)) == 0)
1833ab2de065SBrian Somers         log_Printf(LogDEBUG, "%s: %s: Cannot determine bandwidth\n",
1834ab2de065SBrian Somers                    dl->name, dl->physical->name.full);
1835ab2de065SBrian Somers       else
1836ab2de065SBrian Somers         bundle->bandwidth += sp;
1837ab2de065SBrian Somers       if (!bundle->ncp.mp.active) {
1838c8b9fb53SBrian Somers         bundle->iface->mtu = dl->physical->link.lcp.his_mru;
1839ab2de065SBrian Somers         break;
1840ab2de065SBrian Somers       }
1841ab2de065SBrian Somers     }
18426301d506SBrian Somers   }
1843ab2de065SBrian Somers 
1844ab2de065SBrian Somers   if(bundle->bandwidth == 0)
1845ab2de065SBrian Somers     bundle->bandwidth = 115200;		/* Shrug */
1846ab2de065SBrian Somers 
18476301d506SBrian Somers   if (bundle->ncp.mp.active) {
1848c8b9fb53SBrian Somers     bundle->iface->mtu = bundle->ncp.mp.peer_mrru;
18496301d506SBrian Somers     overhead = ccp_MTUOverhead(&bundle->ncp.mp.link.ccp);
18506301d506SBrian Somers     if (maxoverhead < overhead)
18516301d506SBrian Somers       maxoverhead = overhead;
18526301d506SBrian Somers   } else if (!bundle->iface->mtu)
1853c8b9fb53SBrian Somers     bundle->iface->mtu = DEF_MRU;
1854ab2de065SBrian Somers 
1855ab2de065SBrian Somers #ifndef NORADIUS
185694d7be52SBrian Somers   if (bundle->radius.valid && bundle->radius.mtu &&
1857c8b9fb53SBrian Somers       bundle->radius.mtu < bundle->iface->mtu) {
1858ab2de065SBrian Somers     log_Printf(LogLCP, "Reducing MTU to radius value %lu\n",
1859ab2de065SBrian Somers                bundle->radius.mtu);
1860c8b9fb53SBrian Somers     bundle->iface->mtu = bundle->radius.mtu;
1861ab2de065SBrian Somers   }
1862ab2de065SBrian Somers #endif
1863ab2de065SBrian Somers 
18646301d506SBrian Somers   if (maxoverhead) {
18656301d506SBrian Somers     log_Printf(LogLCP, "Reducing MTU from %d to %d (CCP requirement)\n",
18666301d506SBrian Somers                bundle->iface->mtu, bundle->iface->mtu - maxoverhead);
18676301d506SBrian Somers     bundle->iface->mtu -= maxoverhead;
18686301d506SBrian Somers   }
18696301d506SBrian Somers 
187094d7be52SBrian Somers   tun_configure(bundle);
187103a2501aSBrian Somers 
187203a2501aSBrian Somers   route_UpdateMTU(bundle);
1873ab2de065SBrian Somers }
1874ab2de065SBrian Somers 
1875ab2de065SBrian Somers void
1876ab2de065SBrian Somers bundle_AutoAdjust(struct bundle *bundle, int percent, int what)
1877ab2de065SBrian Somers {
1878ab2de065SBrian Somers   struct datalink *dl, *choice, *otherlinkup;
1879ab2de065SBrian Somers 
1880ab2de065SBrian Somers   choice = otherlinkup = NULL;
1881ab2de065SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
1882ab2de065SBrian Somers     if (dl->physical->type == PHYS_AUTO) {
1883ab2de065SBrian Somers       if (dl->state == DATALINK_OPEN) {
1884ab2de065SBrian Somers         if (what == AUTO_DOWN) {
1885ab2de065SBrian Somers           if (choice)
1886ab2de065SBrian Somers             otherlinkup = choice;
1887ab2de065SBrian Somers           choice = dl;
1888ab2de065SBrian Somers         }
1889ab2de065SBrian Somers       } else if (dl->state == DATALINK_CLOSED) {
1890ab2de065SBrian Somers         if (what == AUTO_UP) {
1891ab2de065SBrian Somers           choice = dl;
1892ab2de065SBrian Somers           break;
1893ab2de065SBrian Somers         }
1894ab2de065SBrian Somers       } else {
1895ab2de065SBrian Somers         /* An auto link in an intermediate state - forget it for the moment */
1896ab2de065SBrian Somers         choice = NULL;
1897ab2de065SBrian Somers         break;
1898ab2de065SBrian Somers       }
1899ab2de065SBrian Somers     } else if (dl->state == DATALINK_OPEN && what == AUTO_DOWN)
1900ab2de065SBrian Somers       otherlinkup = dl;
1901ab2de065SBrian Somers 
1902ab2de065SBrian Somers   if (choice) {
1903ab2de065SBrian Somers     if (what == AUTO_UP) {
1904ab2de065SBrian Somers       log_Printf(LogPHASE, "%d%% saturation -> Opening link ``%s''\n",
1905ab2de065SBrian Somers                  percent, choice->name);
1906ab2de065SBrian Somers       datalink_Up(choice, 1, 1);
1907a339e644SBrian Somers       mp_CheckAutoloadTimer(&bundle->ncp.mp);
1908ab2de065SBrian Somers     } else if (otherlinkup) {	/* Only bring the second-last link down */
1909ab2de065SBrian Somers       log_Printf(LogPHASE, "%d%% saturation -> Closing link ``%s''\n",
1910ab2de065SBrian Somers                  percent, choice->name);
1911d2f5232dSBrian Somers       datalink_Close(choice, CLOSE_STAYDOWN);
1912a339e644SBrian Somers       mp_CheckAutoloadTimer(&bundle->ncp.mp);
1913ab2de065SBrian Somers     }
1914ab2de065SBrian Somers   }
1915ab2de065SBrian Somers }
1916ab2de065SBrian Somers 
1917ab2de065SBrian Somers int
1918ab2de065SBrian Somers bundle_WantAutoloadTimer(struct bundle *bundle)
1919ab2de065SBrian Somers {
1920ab2de065SBrian Somers   struct datalink *dl;
1921ab2de065SBrian Somers   int autolink, opened;
1922ab2de065SBrian Somers 
1923ab2de065SBrian Somers   if (bundle->phase == PHASE_NETWORK) {
1924ab2de065SBrian Somers     for (autolink = opened = 0, dl = bundle->links; dl; dl = dl->next)
1925ab2de065SBrian Somers       if (dl->physical->type == PHYS_AUTO) {
1926ab2de065SBrian Somers         if (++autolink == 2 || (autolink == 1 && opened))
1927ab2de065SBrian Somers           /* Two auto links or one auto and one open in NETWORK phase */
1928ab2de065SBrian Somers           return 1;
1929ab2de065SBrian Somers       } else if (dl->state == DATALINK_OPEN) {
1930ab2de065SBrian Somers         opened++;
1931ab2de065SBrian Somers         if (autolink)
1932ab2de065SBrian Somers           /* One auto and one open link in NETWORK phase */
1933ab2de065SBrian Somers           return 1;
1934ab2de065SBrian Somers       }
1935ab2de065SBrian Somers   }
1936ab2de065SBrian Somers 
1937ab2de065SBrian Somers   return 0;
1938ab2de065SBrian Somers }
193906b47306SBrian Somers 
194006b47306SBrian Somers void
194106b47306SBrian Somers bundle_ChangedPID(struct bundle *bundle)
194206b47306SBrian Somers {
194306b47306SBrian Somers #ifdef TUNSIFPID
194406b47306SBrian Somers   ioctl(bundle->dev.fd, TUNSIFPID, 0);
194506b47306SBrian Somers #endif
194606b47306SBrian Somers }
1947