xref: /freebsd/usr.sbin/ppp/bundle.c (revision 2cb305af778acccc5b3ee351e99990c31781efc2)
17a6f8720SBrian Somers /*-
27a6f8720SBrian Somers  * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
37a6f8720SBrian Somers  * All rights reserved.
47a6f8720SBrian Somers  *
57a6f8720SBrian Somers  * Redistribution and use in source and binary forms, with or without
67a6f8720SBrian Somers  * modification, are permitted provided that the following conditions
77a6f8720SBrian Somers  * are met:
87a6f8720SBrian Somers  * 1. Redistributions of source code must retain the above copyright
97a6f8720SBrian Somers  *    notice, this list of conditions and the following disclaimer.
107a6f8720SBrian Somers  * 2. Redistributions in binary form must reproduce the above copyright
117a6f8720SBrian Somers  *    notice, this list of conditions and the following disclaimer in the
127a6f8720SBrian Somers  *    documentation and/or other materials provided with the distribution.
137a6f8720SBrian Somers  *
147a6f8720SBrian Somers  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
157a6f8720SBrian Somers  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
167a6f8720SBrian Somers  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
177a6f8720SBrian Somers  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
187a6f8720SBrian Somers  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
197a6f8720SBrian Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
207a6f8720SBrian Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
217a6f8720SBrian Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
227a6f8720SBrian Somers  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
237a6f8720SBrian Somers  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
247a6f8720SBrian Somers  * SUCH DAMAGE.
257a6f8720SBrian Somers  *
2697d92980SPeter Wemm  * $FreeBSD$
277a6f8720SBrian Somers  */
287a6f8720SBrian Somers 
291384bd27SBrian Somers #include <sys/param.h>
307a6f8720SBrian Somers #include <sys/socket.h>
317a6f8720SBrian Somers #include <netinet/in.h>
327a6f8720SBrian Somers #include <net/if.h>
33eb6e5e05SBrian Somers #include <net/if_tun.h>		/* For TUNSIFMODE & TUNSLMODE */
347a6f8720SBrian Somers #include <arpa/inet.h>
357a6f8720SBrian Somers #include <net/route.h>
36eaa4df37SBrian Somers #include <netinet/in_systm.h>
37eaa4df37SBrian Somers #include <netinet/ip.h>
381fa665f5SBrian Somers #include <sys/un.h>
397a6f8720SBrian Somers 
407a6f8720SBrian Somers #include <errno.h>
417a6f8720SBrian Somers #include <fcntl.h>
42cf0a3940SBrian Somers #ifdef __OpenBSD__
43cf0a3940SBrian Somers #include <util.h>
44cf0a3940SBrian Somers #else
45cf0a3940SBrian Somers #include <libutil.h>
46cf0a3940SBrian Somers #endif
4747723d29SBrian Somers #include <paths.h>
487a6f8720SBrian Somers #include <stdio.h>
496f384573SBrian Somers #include <stdlib.h>
507a6f8720SBrian Somers #include <string.h>
5196c9bb21SBrian Somers #include <sys/uio.h>
5254cd8e13SBrian Somers #include <sys/wait.h>
53fc3034caSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
54fc3034caSBrian Somers #include <sys/linker.h>
55fdb4bb1bSBrian Somers #include <sys/module.h>
56fc3034caSBrian Somers #endif
577a6f8720SBrian Somers #include <termios.h>
587a6f8720SBrian Somers #include <unistd.h>
597a6f8720SBrian Somers 
605d9e6103SBrian Somers #include "layer.h"
61c9e11a11SBrian Somers #include "defs.h"
627a6f8720SBrian Somers #include "command.h"
637a6f8720SBrian Somers #include "mbuf.h"
647a6f8720SBrian Somers #include "log.h"
657a6f8720SBrian Somers #include "id.h"
667a6f8720SBrian Somers #include "timer.h"
677a6f8720SBrian Somers #include "fsm.h"
687a6f8720SBrian Somers #include "iplist.h"
69879ed6faSBrian Somers #include "lqr.h"
70455aabc3SBrian Somers #include "hdlc.h"
717a6f8720SBrian Somers #include "throughput.h"
72eaa4df37SBrian Somers #include "slcompress.h"
737a6f8720SBrian Somers #include "ipcp.h"
745ca5389aSBrian Somers #include "filter.h"
752f786681SBrian Somers #include "descriptor.h"
767a6f8720SBrian Somers #include "route.h"
777a6f8720SBrian Somers #include "lcp.h"
787a6f8720SBrian Somers #include "ccp.h"
793b0f8d2eSBrian Somers #include "link.h"
803b0f8d2eSBrian Somers #include "mp.h"
81972a1bcfSBrian Somers #ifndef NORADIUS
82972a1bcfSBrian Somers #include "radius.h"
83972a1bcfSBrian Somers #endif
843b0f8d2eSBrian Somers #include "bundle.h"
85455aabc3SBrian Somers #include "async.h"
86455aabc3SBrian Somers #include "physical.h"
87455aabc3SBrian Somers #include "auth.h"
885d9e6103SBrian Somers #include "proto.h"
89455aabc3SBrian Somers #include "chap.h"
90455aabc3SBrian Somers #include "tun.h"
9185b542cfSBrian Somers #include "prompt.h"
923006ec67SBrian Somers #include "chat.h"
9392b09558SBrian Somers #include "cbcp.h"
943006ec67SBrian Somers #include "datalink.h"
953006ec67SBrian Somers #include "ip.h"
968fa6ebe4SBrian Somers #include "iface.h"
977a6f8720SBrian Somers 
98ab2de065SBrian Somers #define SCATTER_SEGMENTS 6  /* version, datalink, name, physical,
99ab2de065SBrian Somers                                throughput, device                   */
10087c3786eSBrian Somers 
1012cb305afSBrian Somers #define SEND_MAXFD 3        /* Max file descriptors passed through
10287c3786eSBrian Somers                                the local domain socket              */
10396c9bb21SBrian Somers 
10404eaa58cSBrian Somers static int bundle_RemainingIdleTime(struct bundle *);
10504eaa58cSBrian Somers 
106455aabc3SBrian Somers static const char *PhaseNames[] = {
107455aabc3SBrian Somers   "Dead", "Establish", "Authenticate", "Network", "Terminate"
108455aabc3SBrian Somers };
109455aabc3SBrian Somers 
110455aabc3SBrian Somers const char *
111455aabc3SBrian Somers bundle_PhaseName(struct bundle *bundle)
1127a6f8720SBrian Somers {
113455aabc3SBrian Somers   return bundle->phase <= PHASE_TERMINATE ?
114455aabc3SBrian Somers     PhaseNames[bundle->phase] : "unknown";
1157a6f8720SBrian Somers }
1167a6f8720SBrian Somers 
117455aabc3SBrian Somers void
1185563ebdeSBrian Somers bundle_NewPhase(struct bundle *bundle, u_int new)
119455aabc3SBrian Somers {
120aef795ccSBrian Somers   if (new == bundle->phase)
121aef795ccSBrian Somers     return;
122aef795ccSBrian Somers 
123e2ebb036SBrian Somers   if (new <= PHASE_TERMINATE)
124dd7e2610SBrian Somers     log_Printf(LogPHASE, "bundle: %s\n", PhaseNames[new]);
1257a6f8720SBrian Somers 
126455aabc3SBrian Somers   switch (new) {
127455aabc3SBrian Somers   case PHASE_DEAD:
1280f2f3eb3SBrian Somers     log_DisplayPrompts();
129455aabc3SBrian Somers     bundle->phase = new;
130455aabc3SBrian Somers     break;
131455aabc3SBrian Somers 
132455aabc3SBrian Somers   case PHASE_ESTABLISH:
133455aabc3SBrian Somers     bundle->phase = new;
134455aabc3SBrian Somers     break;
135455aabc3SBrian Somers 
136455aabc3SBrian Somers   case PHASE_AUTHENTICATE:
137455aabc3SBrian Somers     bundle->phase = new;
1380f2f3eb3SBrian Somers     log_DisplayPrompts();
139455aabc3SBrian Somers     break;
140455aabc3SBrian Somers 
141455aabc3SBrian Somers   case PHASE_NETWORK:
142dd7e2610SBrian Somers     fsm_Up(&bundle->ncp.ipcp.fsm);
143dd7e2610SBrian Somers     fsm_Open(&bundle->ncp.ipcp.fsm);
144673903ecSBrian Somers     bundle->phase = new;
1450f2f3eb3SBrian Somers     log_DisplayPrompts();
146673903ecSBrian Somers     break;
147455aabc3SBrian Somers 
148455aabc3SBrian Somers   case PHASE_TERMINATE:
149455aabc3SBrian Somers     bundle->phase = new;
150673903ecSBrian Somers     mp_Down(&bundle->ncp.mp);
1510f2f3eb3SBrian Somers     log_DisplayPrompts();
152455aabc3SBrian Somers     break;
1537a6f8720SBrian Somers   }
1547a6f8720SBrian Somers }
1557a6f8720SBrian Somers 
1566d666775SBrian Somers static void
1576d666775SBrian Somers bundle_LayerStart(void *v, struct fsm *fp)
1587a6f8720SBrian Somers {
1593006ec67SBrian Somers   /* The given FSM is about to start up ! */
1607a6f8720SBrian Somers }
1617a6f8720SBrian Somers 
1625cf4388bSBrian Somers 
1635cf4388bSBrian Somers static void
1645cf4388bSBrian Somers bundle_Notify(struct bundle *bundle, char c)
1655cf4388bSBrian Somers {
1665cf4388bSBrian Somers   if (bundle->notify.fd != -1) {
1675cf4388bSBrian Somers     if (write(bundle->notify.fd, &c, 1) == 1)
168dd7e2610SBrian Somers       log_Printf(LogPHASE, "Parent notified of success.\n");
1695cf4388bSBrian Somers     else
170dd7e2610SBrian Somers       log_Printf(LogPHASE, "Failed to notify parent of success.\n");
1715cf4388bSBrian Somers     close(bundle->notify.fd);
1725cf4388bSBrian Somers     bundle->notify.fd = -1;
1735cf4388bSBrian Somers   }
1745cf4388bSBrian Somers }
1753b0f8d2eSBrian Somers 
1766d666775SBrian Somers static void
1776f8e9f0aSBrian Somers bundle_ClearQueues(void *v)
1786f8e9f0aSBrian Somers {
1796f8e9f0aSBrian Somers   struct bundle *bundle = (struct bundle *)v;
1806f8e9f0aSBrian Somers   struct datalink *dl;
1816f8e9f0aSBrian Somers 
1826f8e9f0aSBrian Somers   log_Printf(LogPHASE, "Clearing choked output queue\n");
1836f8e9f0aSBrian Somers   timer_Stop(&bundle->choked.timer);
1846f8e9f0aSBrian Somers 
1856f8e9f0aSBrian Somers   /*
1866f8e9f0aSBrian Somers    * Emergency time:
1876f8e9f0aSBrian Somers    *
1886f8e9f0aSBrian Somers    * We've had a full queue for PACKET_DEL_SECS seconds without being
1896f8e9f0aSBrian Somers    * able to get rid of any of the packets.  We've probably given up
1906f8e9f0aSBrian Somers    * on the redials at this point, and the queued data has almost
1916f8e9f0aSBrian Somers    * definitely been timed out by the layer above.  As this is preventing
1926f8e9f0aSBrian Somers    * us from reading the TUN_NAME device (we don't want to buffer stuff
1936f8e9f0aSBrian Somers    * indefinitely), we may as well nuke this data and start with a clean
1946f8e9f0aSBrian Somers    * slate !
1956f8e9f0aSBrian Somers    *
1966f8e9f0aSBrian Somers    * Unfortunately, this has the side effect of shafting any compression
1976f8e9f0aSBrian Somers    * dictionaries in use (causing the relevant RESET_REQ/RESET_ACK).
1986f8e9f0aSBrian Somers    */
1996f8e9f0aSBrian Somers 
2005a72b6edSBrian Somers   ip_DeleteQueue(&bundle->ncp.ipcp);
2016f8e9f0aSBrian Somers   mp_DeleteQueue(&bundle->ncp.mp);
2026f8e9f0aSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
2036f8e9f0aSBrian Somers     physical_DeleteQueue(dl->physical);
2046f8e9f0aSBrian Somers }
2056f8e9f0aSBrian Somers 
2066f8e9f0aSBrian Somers static void
207ff0f9439SBrian Somers bundle_LinkAdded(struct bundle *bundle, struct datalink *dl)
208ff0f9439SBrian Somers {
209ff0f9439SBrian Somers   bundle->phys_type.all |= dl->physical->type;
210ff0f9439SBrian Somers   if (dl->state == DATALINK_OPEN)
211ff0f9439SBrian Somers     bundle->phys_type.open |= dl->physical->type;
212ff0f9439SBrian Somers 
213ff0f9439SBrian Somers   if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL))
214ff0f9439SBrian Somers       != bundle->phys_type.open && bundle->idle.timer.state == TIMER_STOPPED)
215ff0f9439SBrian Somers     /* We may need to start our idle timer */
216ff0f9439SBrian Somers     bundle_StartIdleTimer(bundle);
217ff0f9439SBrian Somers }
218ff0f9439SBrian Somers 
21992b09558SBrian Somers void
220ff0f9439SBrian Somers bundle_LinksRemoved(struct bundle *bundle)
221ff0f9439SBrian Somers {
222ff0f9439SBrian Somers   struct datalink *dl;
223ff0f9439SBrian Somers 
224ff0f9439SBrian Somers   bundle->phys_type.all = bundle->phys_type.open = 0;
225ff0f9439SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
226ff0f9439SBrian Somers     bundle_LinkAdded(bundle, dl);
227ff0f9439SBrian Somers 
228ab2de065SBrian Somers   bundle_CalculateBandwidth(bundle);
229ab2de065SBrian Somers   mp_CheckAutoloadTimer(&bundle->ncp.mp);
230ab2de065SBrian Somers 
231ff0f9439SBrian Somers   if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL))
232ff0f9439SBrian Somers       == bundle->phys_type.open)
233ff0f9439SBrian Somers     bundle_StopIdleTimer(bundle);
234ff0f9439SBrian Somers }
23504eaa58cSBrian Somers 
23604eaa58cSBrian Somers static void
2376f384573SBrian Somers bundle_LayerUp(void *v, struct fsm *fp)
2387a6f8720SBrian Somers {
2393006ec67SBrian Somers   /*
2403006ec67SBrian Somers    * The given fsm is now up
241ab2de065SBrian Somers    * If it's an LCP, adjust our phys_mode.open value and check the
242ab2de065SBrian Somers    * autoload timer.
243ab2de065SBrian Somers    * If it's the first NCP, calculate our bandwidth
244dade2407SBrian Somers    * If it's the first NCP, set our ``upat'' time
2453b0f8d2eSBrian Somers    * If it's the first NCP, start the idle timer.
246ab2de065SBrian Somers    * If it's an NCP, tell our -background parent to go away.
247ab2de065SBrian Somers    * If it's the first NCP, start the autoload timer
2483006ec67SBrian Somers    */
2496f384573SBrian Somers   struct bundle *bundle = (struct bundle *)v;
2506d666775SBrian Somers 
2515563ebdeSBrian Somers   if (fp->proto == PROTO_LCP) {
252ff0f9439SBrian Somers     struct physical *p = link2physical(fp->link);
253ff0f9439SBrian Somers 
254ff0f9439SBrian Somers     bundle_LinkAdded(bundle, p->dl);
255ab2de065SBrian Somers     mp_CheckAutoloadTimer(&bundle->ncp.mp);
2563b0f8d2eSBrian Somers   } else if (fp->proto == PROTO_IPCP) {
257ab2de065SBrian Somers     bundle_CalculateBandwidth(fp->bundle);
258dade2407SBrian Somers     time(&bundle->upat);
259ab886ad0SBrian Somers     bundle_StartIdleTimer(bundle);
2605cf4388bSBrian Somers     bundle_Notify(bundle, EX_NORMAL);
261ab2de065SBrian Somers     mp_CheckAutoloadTimer(&fp->bundle->ncp.mp);
2627a6f8720SBrian Somers   }
263ab886ad0SBrian Somers }
2647a6f8720SBrian Somers 
2656d666775SBrian Somers static void
2666d666775SBrian Somers bundle_LayerDown(void *v, struct fsm *fp)
2676d666775SBrian Somers {
2686d666775SBrian Somers   /*
2696d666775SBrian Somers    * The given FSM has been told to come down.
270ab886ad0SBrian Somers    * If it's our last NCP, stop the idle timer.
271dade2407SBrian Somers    * If it's our last NCP, clear our ``upat'' value.
272ab2de065SBrian Somers    * If it's our last NCP, stop the autoload timer
273ff0f9439SBrian Somers    * If it's an LCP, adjust our phys_type.open value and any timers.
27486b1f0d7SBrian Somers    * If it's an LCP and we're in multilink mode, adjust our tun
27586b1f0d7SBrian Somers    * speed and make sure our minimum sequence number is adjusted.
2766d666775SBrian Somers    */
277ab886ad0SBrian Somers 
278ab886ad0SBrian Somers   struct bundle *bundle = (struct bundle *)v;
279ab886ad0SBrian Somers 
280ab2de065SBrian Somers   if (fp->proto == PROTO_IPCP) {
281ab886ad0SBrian Somers     bundle_StopIdleTimer(bundle);
282dade2407SBrian Somers     bundle->upat = 0;
283ab2de065SBrian Somers     mp_StopAutoloadTimer(&bundle->ncp.mp);
284ab2de065SBrian Somers   } else if (fp->proto == PROTO_LCP) {
285ff0f9439SBrian Somers     bundle_LinksRemoved(bundle);  /* adjust timers & phys_type values */
286ff0f9439SBrian Somers     if (bundle->ncp.mp.active) {
2873b0f8d2eSBrian Somers       struct datalink *dl;
28886b1f0d7SBrian Somers       struct datalink *lost;
2893b0f8d2eSBrian Somers 
29086b1f0d7SBrian Somers       lost = NULL;
291faefde08SBrian Somers       for (dl = bundle->links; dl; dl = dl->next)
29286b1f0d7SBrian Somers         if (fp == &dl->physical->link.lcp.fsm)
29386b1f0d7SBrian Somers           lost = dl;
29486b1f0d7SBrian Somers 
295ab2de065SBrian Somers       bundle_CalculateBandwidth(bundle);
29686b1f0d7SBrian Somers 
29786b1f0d7SBrian Somers       if (lost)
29886b1f0d7SBrian Somers         mp_LinkLost(&bundle->ncp.mp, lost);
29986b1f0d7SBrian Somers       else
300a33b2ef7SBrian Somers         log_Printf(LogALERT, "Oops, lost an unrecognised datalink (%s) !\n",
30186b1f0d7SBrian Somers                    fp->link->name);
3023b0f8d2eSBrian Somers     }
3036d666775SBrian Somers   }
304ff0f9439SBrian Somers }
3056d666775SBrian Somers 
3066d666775SBrian Somers static void
3076d666775SBrian Somers bundle_LayerFinish(void *v, struct fsm *fp)
3086d666775SBrian Somers {
3096d666775SBrian Somers   /* The given fsm is now down (fp cannot be NULL)
3106d666775SBrian Somers    *
311dd7e2610SBrian Somers    * If it's the last LCP, fsm_Down all NCPs
312dd7e2610SBrian Somers    * If it's the last NCP, fsm_Close all LCPs
3136d666775SBrian Somers    */
3146d666775SBrian Somers 
3156d666775SBrian Somers   struct bundle *bundle = (struct bundle *)v;
3166d666775SBrian Somers   struct datalink *dl;
3176d666775SBrian Somers 
3183b0f8d2eSBrian Somers   if (fp->proto == PROTO_IPCP) {
31926afeaa2SBrian Somers     if (bundle_Phase(bundle) != PHASE_DEAD)
32025092092SBrian Somers       bundle_NewPhase(bundle, PHASE_TERMINATE);
3216d666775SBrian Somers     for (dl = bundle->links; dl; dl = dl->next)
322d4d5d2f8SBrian Somers       datalink_Close(dl, CLOSE_STAYDOWN);
32309206a6fSBrian Somers     fsm2initial(fp);
3243b0f8d2eSBrian Somers   } else if (fp->proto == PROTO_LCP) {
3253b0f8d2eSBrian Somers     int others_active;
326a611cad6SBrian Somers 
3273b0f8d2eSBrian Somers     others_active = 0;
3283b0f8d2eSBrian Somers     for (dl = bundle->links; dl; dl = dl->next)
3293b0f8d2eSBrian Somers       if (fp != &dl->physical->link.lcp.fsm &&
3303b0f8d2eSBrian Somers           dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP)
3313b0f8d2eSBrian Somers         others_active++;
3323b0f8d2eSBrian Somers 
33309206a6fSBrian Somers     if (!others_active)
33409206a6fSBrian Somers       fsm2initial(&bundle->ncp.ipcp.fsm);
3353b0f8d2eSBrian Somers   }
3363b0f8d2eSBrian Somers }
3376d666775SBrian Somers 
3387a6f8720SBrian Somers int
3397a6f8720SBrian Somers bundle_LinkIsUp(const struct bundle *bundle)
3407a6f8720SBrian Somers {
3415828db6dSBrian Somers   return bundle->ncp.ipcp.fsm.state == ST_OPENED;
3427a6f8720SBrian Somers }
3437a6f8720SBrian Somers 
3447a6f8720SBrian Somers void
3459c81b87dSBrian Somers bundle_Close(struct bundle *bundle, const char *name, int how)
3467a6f8720SBrian Somers {
347455aabc3SBrian Somers   /*
3483006ec67SBrian Somers    * Please close the given datalink.
349dd7e2610SBrian Somers    * If name == NULL or name is the last datalink, fsm_Close all NCPs
3506f384573SBrian Somers    * (except our MP)
3513b0f8d2eSBrian Somers    * If it isn't the last datalink, just Close that datalink.
352455aabc3SBrian Somers    */
3537a6f8720SBrian Somers 
3543b0f8d2eSBrian Somers   struct datalink *dl, *this_dl;
3553b0f8d2eSBrian Somers   int others_active;
3563006ec67SBrian Somers 
3573b0f8d2eSBrian Somers   others_active = 0;
3583b0f8d2eSBrian Somers   this_dl = NULL;
3593b0f8d2eSBrian Somers 
3603b0f8d2eSBrian Somers   for (dl = bundle->links; dl; dl = dl->next) {
3613b0f8d2eSBrian Somers     if (name && !strcasecmp(name, dl->name))
3623b0f8d2eSBrian Somers       this_dl = dl;
3633b0f8d2eSBrian Somers     if (name == NULL || this_dl == dl) {
3649c81b87dSBrian Somers       switch (how) {
3659c81b87dSBrian Somers         case CLOSE_LCP:
3669c81b87dSBrian Somers           datalink_DontHangup(dl);
3679c81b87dSBrian Somers           /* fall through */
3689c81b87dSBrian Somers         case CLOSE_STAYDOWN:
3693006ec67SBrian Somers           datalink_StayDown(dl);
3709c81b87dSBrian Somers           break;
3719c81b87dSBrian Somers       }
3723b0f8d2eSBrian Somers     } else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP)
3733b0f8d2eSBrian Somers       others_active++;
3743b0f8d2eSBrian Somers   }
3753b0f8d2eSBrian Somers 
3763b0f8d2eSBrian Somers   if (name && this_dl == NULL) {
377dd7e2610SBrian Somers     log_Printf(LogWARN, "%s: Invalid datalink name\n", name);
3783b0f8d2eSBrian Somers     return;
3793b0f8d2eSBrian Somers   }
3803b0f8d2eSBrian Somers 
3813b0f8d2eSBrian Somers   if (!others_active) {
38204eaa58cSBrian Somers     bundle_StopIdleTimer(bundle);
3833b0f8d2eSBrian Somers     if (bundle->ncp.ipcp.fsm.state > ST_CLOSED ||
3843b0f8d2eSBrian Somers         bundle->ncp.ipcp.fsm.state == ST_STARTING)
385dd7e2610SBrian Somers       fsm_Close(&bundle->ncp.ipcp.fsm);
3863b0f8d2eSBrian Somers     else {
38709206a6fSBrian Somers       fsm2initial(&bundle->ncp.ipcp.fsm);
388d345321bSBrian Somers       for (dl = bundle->links; dl; dl = dl->next)
3899c81b87dSBrian Somers         datalink_Close(dl, how);
3907a6f8720SBrian Somers     }
3913b0f8d2eSBrian Somers   } else if (this_dl && this_dl->state != DATALINK_CLOSED &&
3923b0f8d2eSBrian Somers              this_dl->state != DATALINK_HANGUP)
3939c81b87dSBrian Somers     datalink_Close(this_dl, how);
394d2fd8d77SBrian Somers }
3957a6f8720SBrian Somers 
3961bc9b5baSBrian Somers void
397899011c4SBrian Somers bundle_Down(struct bundle *bundle, int how)
3981bc9b5baSBrian Somers {
3991bc9b5baSBrian Somers   struct datalink *dl;
4001bc9b5baSBrian Somers 
4011bc9b5baSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
402899011c4SBrian Somers     datalink_Down(dl, how);
4031bc9b5baSBrian Somers }
4041bc9b5baSBrian Somers 
4052f786681SBrian Somers static int
4062f786681SBrian Somers bundle_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
4072f786681SBrian Somers {
4082f786681SBrian Somers   struct bundle *bundle = descriptor2bundle(d);
4092f786681SBrian Somers   struct datalink *dl;
410ab2de065SBrian Somers   int result, queued, nlinks;
4112f786681SBrian Somers 
4122f786681SBrian Somers   result = 0;
4132f786681SBrian Somers 
414078c562eSBrian Somers   /* If there are aren't many packets queued, look for some more. */
41504eaa58cSBrian Somers   for (nlinks = 0, dl = bundle->links; dl; dl = dl->next)
41604eaa58cSBrian Somers     nlinks++;
41704eaa58cSBrian Somers 
41804eaa58cSBrian Somers   if (nlinks) {
4195a72b6edSBrian Somers     queued = r ? bundle_FillQueues(bundle) : ip_QueueLen(&bundle->ncp.ipcp);
42004eaa58cSBrian Somers 
421ff0f9439SBrian Somers     if (r && (bundle->phase == PHASE_NETWORK ||
422ff0f9439SBrian Somers               bundle->phys_type.all & PHYS_AUTO)) {
42304eaa58cSBrian Somers       /* enough surplus so that we can tell if we're getting swamped */
424442f8495SBrian Somers       if (queued < 30) {
42504eaa58cSBrian Somers         /* Not enough - select() for more */
4266f8e9f0aSBrian Somers         if (bundle->choked.timer.state == TIMER_RUNNING)
4276f8e9f0aSBrian Somers           timer_Stop(&bundle->choked.timer);	/* Not needed any more */
42804eaa58cSBrian Somers         FD_SET(bundle->dev.fd, r);
429faefde08SBrian Somers         if (*n < bundle->dev.fd + 1)
430faefde08SBrian Somers           *n = bundle->dev.fd + 1;
4311384bd27SBrian Somers         log_Printf(LogTIMER, "%s: fdset(r) %d\n", TUN_NAME, bundle->dev.fd);
432078c562eSBrian Somers         result++;
4336f8e9f0aSBrian Somers       } else if (bundle->choked.timer.state == TIMER_STOPPED) {
4346f8e9f0aSBrian Somers         bundle->choked.timer.func = bundle_ClearQueues;
4356f8e9f0aSBrian Somers         bundle->choked.timer.name = "output choke";
4366f8e9f0aSBrian Somers         bundle->choked.timer.load = bundle->cfg.choked.timeout * SECTICKS;
4376f8e9f0aSBrian Somers         bundle->choked.timer.arg = bundle;
4386f8e9f0aSBrian Somers         timer_Start(&bundle->choked.timer);
439078c562eSBrian Somers       }
44004eaa58cSBrian Somers     }
44104eaa58cSBrian Somers   }
442078c562eSBrian Somers 
443f0cdd9c0SBrian Somers #ifndef NORADIUS
444f0cdd9c0SBrian Somers   result += descriptor_UpdateSet(&bundle->radius.desc, r, w, e, n);
445f0cdd9c0SBrian Somers #endif
446f0cdd9c0SBrian Somers 
44771555108SBrian Somers   /* Which links need a select() ? */
44871555108SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
44971555108SBrian Somers     result += descriptor_UpdateSet(&dl->desc, r, w, e, n);
45071555108SBrian Somers 
451ea722969SBrian Somers   /*
452ea722969SBrian Somers    * This *MUST* be called after the datalink UpdateSet()s as it
45304eaa58cSBrian Somers    * might be ``holding'' one of the datalinks (death-row) and
45404eaa58cSBrian Somers    * wants to be able to de-select() it from the descriptor set.
455ea722969SBrian Somers    */
4560f2f3eb3SBrian Somers   result += descriptor_UpdateSet(&bundle->ncp.mp.server.desc, r, w, e, n);
457ea722969SBrian Somers 
4582f786681SBrian Somers   return result;
4592f786681SBrian Somers }
4602f786681SBrian Somers 
4612f786681SBrian Somers static int
4622f786681SBrian Somers bundle_IsSet(struct descriptor *d, const fd_set *fdset)
4632f786681SBrian Somers {
4642f786681SBrian Somers   struct bundle *bundle = descriptor2bundle(d);
4652f786681SBrian Somers   struct datalink *dl;
4662f786681SBrian Somers 
4672f786681SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
4682f786681SBrian Somers     if (descriptor_IsSet(&dl->desc, fdset))
4692f786681SBrian Somers       return 1;
4702f786681SBrian Somers 
471f0cdd9c0SBrian Somers #ifndef NORADIUS
472f0cdd9c0SBrian Somers   if (descriptor_IsSet(&bundle->radius.desc, fdset))
473f0cdd9c0SBrian Somers     return 1;
474f0cdd9c0SBrian Somers #endif
475f0cdd9c0SBrian Somers 
476332b9de0SBrian Somers   if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset))
477332b9de0SBrian Somers     return 1;
478332b9de0SBrian Somers 
479faefde08SBrian Somers   return FD_ISSET(bundle->dev.fd, fdset);
4802f786681SBrian Somers }
4812f786681SBrian Somers 
4822f786681SBrian Somers static void
4832f786681SBrian Somers bundle_DescriptorRead(struct descriptor *d, struct bundle *bundle,
4842f786681SBrian Somers                       const fd_set *fdset)
4852f786681SBrian Somers {
4862f786681SBrian Somers   struct datalink *dl;
4872f786681SBrian Somers 
488ea722969SBrian Somers   if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset))
489ea722969SBrian Somers     descriptor_Read(&bundle->ncp.mp.server.desc, bundle, fdset);
490ea722969SBrian Somers 
4912f786681SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
4922f786681SBrian Somers     if (descriptor_IsSet(&dl->desc, fdset))
4932f786681SBrian Somers       descriptor_Read(&dl->desc, bundle, fdset);
494b6217683SBrian Somers 
495f0cdd9c0SBrian Somers #ifndef NORADIUS
496f0cdd9c0SBrian Somers   if (descriptor_IsSet(&bundle->radius.desc, fdset))
497f0cdd9c0SBrian Somers     descriptor_Read(&bundle->radius.desc, bundle, fdset);
498f0cdd9c0SBrian Somers #endif
499f0cdd9c0SBrian Somers 
500faefde08SBrian Somers   if (FD_ISSET(bundle->dev.fd, fdset)) {
501078c562eSBrian Somers     struct tun_data tun;
502078c562eSBrian Somers     int n, pri;
503078c562eSBrian Somers 
504078c562eSBrian Somers     /* something to read from tun */
505faefde08SBrian Somers     n = read(bundle->dev.fd, &tun, sizeof tun);
506078c562eSBrian Somers     if (n < 0) {
507a33b2ef7SBrian Somers       log_Printf(LogWARN, "read from %s: %s\n", TUN_NAME, strerror(errno));
508078c562eSBrian Somers       return;
509078c562eSBrian Somers     }
510078c562eSBrian Somers     n -= sizeof tun - sizeof tun.data;
511078c562eSBrian Somers     if (n <= 0) {
512a33b2ef7SBrian Somers       log_Printf(LogERROR, "read from %s: Only %d bytes read ?\n", TUN_NAME, n);
513078c562eSBrian Somers       return;
514078c562eSBrian Somers     }
515078c562eSBrian Somers     if (!tun_check_header(tun, AF_INET))
516078c562eSBrian Somers       return;
517078c562eSBrian Somers 
518078c562eSBrian Somers     if (((struct ip *)tun.data)->ip_dst.s_addr ==
519078c562eSBrian Somers         bundle->ncp.ipcp.my_ip.s_addr) {
520078c562eSBrian Somers       /* we've been asked to send something addressed *to* us :( */
521078c562eSBrian Somers       if (Enabled(bundle, OPT_LOOPBACK)) {
522078c562eSBrian Somers         pri = PacketCheck(bundle, tun.data, n, &bundle->filter.in);
523078c562eSBrian Somers         if (pri >= 0) {
5245d9e6103SBrian Somers           n += sizeof tun - sizeof tun.data;
5255d9e6103SBrian Somers           write(bundle->dev.fd, &tun, n);
526078c562eSBrian Somers           log_Printf(LogDEBUG, "Looped back packet addressed to myself\n");
527078c562eSBrian Somers         }
528078c562eSBrian Somers         return;
529078c562eSBrian Somers       } else
530078c562eSBrian Somers         log_Printf(LogDEBUG, "Oops - forwarding packet addressed to myself\n");
531078c562eSBrian Somers     }
532078c562eSBrian Somers 
533078c562eSBrian Somers     /*
534078c562eSBrian Somers      * Process on-demand dialup. Output packets are queued within tunnel
535078c562eSBrian Somers      * device until IPCP is opened.
536078c562eSBrian Somers      */
537078c562eSBrian Somers 
538078c562eSBrian Somers     if (bundle_Phase(bundle) == PHASE_DEAD) {
539078c562eSBrian Somers       /*
540078c562eSBrian Somers        * Note, we must be in AUTO mode :-/ otherwise our interface should
541078c562eSBrian Somers        * *not* be UP and we can't receive data
542078c562eSBrian Somers        */
543078c562eSBrian Somers       if ((pri = PacketCheck(bundle, tun.data, n, &bundle->filter.dial)) >= 0)
544ba23f397SBrian Somers         bundle_Open(bundle, NULL, PHYS_AUTO, 0);
545078c562eSBrian Somers       else
546078c562eSBrian Somers         /*
547078c562eSBrian Somers          * Drop the packet.  If we were to queue it, we'd just end up with
548078c562eSBrian Somers          * a pile of timed-out data in our output queue by the time we get
549078c562eSBrian Somers          * around to actually dialing.  We'd also prematurely reach the
550078c562eSBrian Somers          * threshold at which we stop select()ing to read() the tun
551078c562eSBrian Somers          * device - breaking auto-dial.
552078c562eSBrian Somers          */
553078c562eSBrian Somers         return;
554078c562eSBrian Somers     }
555078c562eSBrian Somers 
556078c562eSBrian Somers     pri = PacketCheck(bundle, tun.data, n, &bundle->filter.out);
5575d9e6103SBrian Somers     if (pri >= 0)
5585a72b6edSBrian Somers       ip_Enqueue(&bundle->ncp.ipcp, pri, tun.data, n);
559078c562eSBrian Somers   }
560078c562eSBrian Somers }
5612f786681SBrian Somers 
5621af29a6eSBrian Somers static int
5632f786681SBrian Somers bundle_DescriptorWrite(struct descriptor *d, struct bundle *bundle,
5642f786681SBrian Somers                        const fd_set *fdset)
5652f786681SBrian Somers {
5662f786681SBrian Somers   struct datalink *dl;
5671af29a6eSBrian Somers   int result = 0;
5682f786681SBrian Somers 
569ea722969SBrian Somers   /* This is not actually necessary as struct mpserver doesn't Write() */
570ea722969SBrian Somers   if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset))
571ea722969SBrian Somers     descriptor_Write(&bundle->ncp.mp.server.desc, bundle, fdset);
572ea722969SBrian Somers 
5732f786681SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
5742f786681SBrian Somers     if (descriptor_IsSet(&dl->desc, fdset))
5751af29a6eSBrian Somers       result += descriptor_Write(&dl->desc, bundle, fdset);
5761af29a6eSBrian Somers 
5771af29a6eSBrian Somers   return result;
5782f786681SBrian Somers }
5792f786681SBrian Somers 
580da66dd13SBrian Somers void
5811384bd27SBrian Somers bundle_LockTun(struct bundle *bundle)
5821384bd27SBrian Somers {
5831384bd27SBrian Somers   FILE *lockfile;
5841384bd27SBrian Somers   char pidfile[MAXPATHLEN];
5851384bd27SBrian Somers 
5861384bd27SBrian Somers   snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit);
5871384bd27SBrian Somers   lockfile = ID0fopen(pidfile, "w");
5881384bd27SBrian Somers   if (lockfile != NULL) {
5891384bd27SBrian Somers     fprintf(lockfile, "%d\n", (int)getpid());
5901384bd27SBrian Somers     fclose(lockfile);
5911384bd27SBrian Somers   }
5921384bd27SBrian Somers #ifndef RELEASE_CRUNCH
5931384bd27SBrian Somers   else
5941384bd27SBrian Somers     log_Printf(LogERROR, "Warning: Can't create %s: %s\n",
5951384bd27SBrian Somers                pidfile, strerror(errno));
5961384bd27SBrian Somers #endif
5971384bd27SBrian Somers }
5981384bd27SBrian Somers 
5991384bd27SBrian Somers static void
6001384bd27SBrian Somers bundle_UnlockTun(struct bundle *bundle)
6011384bd27SBrian Somers {
6021384bd27SBrian Somers   char pidfile[MAXPATHLEN];
6031384bd27SBrian Somers 
6041384bd27SBrian Somers   snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit);
6051384bd27SBrian Somers   ID0unlink(pidfile);
6061384bd27SBrian Somers }
6077a6f8720SBrian Somers 
6087a6f8720SBrian Somers struct bundle *
609cf0a3940SBrian Somers bundle_Create(const char *prefix, int type, int unit)
6107a6f8720SBrian Somers {
6117a6f8720SBrian Somers   static struct bundle bundle;		/* there can be only one */
612c0593e34SBrian Somers   int enoentcount, err, minunit, maxunit;
6134e5196e9SBrian Somers   const char *ifname;
614fdb4bb1bSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
615fc3034caSBrian Somers   int kldtried;
616fc3034caSBrian Somers #endif
6176ca65df0SBrian Somers #if defined(TUNSIFMODE) || defined(TUNSLMODE)
6180f203c7eSBrian Somers   int iff;
6190f203c7eSBrian Somers #endif
6207a6f8720SBrian Somers 
6218fa6ebe4SBrian Somers   if (bundle.iface != NULL) {	/* Already allocated ! */
622a33b2ef7SBrian Somers     log_Printf(LogALERT, "bundle_Create:  There's only one BUNDLE !\n");
6237a6f8720SBrian Somers     return NULL;
6247a6f8720SBrian Somers   }
6257a6f8720SBrian Somers 
626c0593e34SBrian Somers   if (unit == -1) {
627c0593e34SBrian Somers     minunit = 0;
628c0593e34SBrian Somers     maxunit = -1;
629c0593e34SBrian Somers   } else {
630c0593e34SBrian Somers     minunit = unit;
631c0593e34SBrian Somers     maxunit = unit + 1;
632c0593e34SBrian Somers   }
6337a6f8720SBrian Somers   err = ENOENT;
6347a6f8720SBrian Somers   enoentcount = 0;
635fdb4bb1bSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
636fc3034caSBrian Somers   kldtried = 0;
637fc3034caSBrian Somers #endif
638c0593e34SBrian Somers   for (bundle.unit = minunit; bundle.unit != maxunit; bundle.unit++) {
639faefde08SBrian Somers     snprintf(bundle.dev.Name, sizeof bundle.dev.Name, "%s%d",
640faefde08SBrian Somers              prefix, bundle.unit);
641faefde08SBrian Somers     bundle.dev.fd = ID0open(bundle.dev.Name, O_RDWR);
642faefde08SBrian Somers     if (bundle.dev.fd >= 0)
6437a6f8720SBrian Somers       break;
644107d62e7SBrian Somers     else if (errno == ENXIO) {
645fdb4bb1bSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
646c0593e34SBrian Somers       if (bundle.unit == minunit && !kldtried++) {
647fc3034caSBrian Somers         /*
648fdb4bb1bSBrian Somers 	 * Attempt to load the tunnel interface KLD if it isn't loaded
649fdb4bb1bSBrian Somers 	 * already.
650fc3034caSBrian Somers          */
651fdb4bb1bSBrian Somers         if (modfind("if_tun") == -1) {
652fc3034caSBrian Somers           if (ID0kldload("if_tun") != -1) {
653fc3034caSBrian Somers             bundle.unit--;
654fc3034caSBrian Somers             continue;
655fc3034caSBrian Somers           }
656fc3034caSBrian Somers           log_Printf(LogWARN, "kldload: if_tun: %s\n", strerror(errno));
657fc3034caSBrian Somers         }
658fc3034caSBrian Somers       }
659fc3034caSBrian Somers #endif
6607a6f8720SBrian Somers       err = errno;
661107d62e7SBrian Somers       break;
6627a6f8720SBrian Somers     } else if (errno == ENOENT) {
6637a6f8720SBrian Somers       if (++enoentcount > 2)
664107d62e7SBrian Somers 	break;
6657a6f8720SBrian Somers     } else
6667a6f8720SBrian Somers       err = errno;
6677a6f8720SBrian Somers   }
6687a6f8720SBrian Somers 
669faefde08SBrian Somers   if (bundle.dev.fd < 0) {
670c0593e34SBrian Somers     if (unit == -1)
671c0593e34SBrian Somers       log_Printf(LogWARN, "No available tunnel devices found (%s)\n",
67285b542cfSBrian Somers                 strerror(err));
673c0593e34SBrian Somers     else
674c0593e34SBrian Somers       log_Printf(LogWARN, "%s%d: %s\n", prefix, unit, strerror(err));
6757a6f8720SBrian Somers     return NULL;
6767a6f8720SBrian Somers   }
6777a6f8720SBrian Somers 
678dd7e2610SBrian Somers   log_SetTun(bundle.unit);
6797a6f8720SBrian Somers 
6808fa6ebe4SBrian Somers   ifname = strrchr(bundle.dev.Name, '/');
6818fa6ebe4SBrian Somers   if (ifname == NULL)
6828fa6ebe4SBrian Somers     ifname = bundle.dev.Name;
6837a6f8720SBrian Somers   else
6848fa6ebe4SBrian Somers     ifname++;
6858fa6ebe4SBrian Somers 
6868fa6ebe4SBrian Somers   bundle.iface = iface_Create(ifname);
6878fa6ebe4SBrian Somers   if (bundle.iface == NULL) {
6888fa6ebe4SBrian Somers     close(bundle.dev.fd);
6898fa6ebe4SBrian Somers     return NULL;
6908fa6ebe4SBrian Somers   }
6917a6f8720SBrian Somers 
6920f203c7eSBrian Somers #ifdef TUNSIFMODE
6930f203c7eSBrian Somers   /* Make sure we're POINTOPOINT */
6940f203c7eSBrian Somers   iff = IFF_POINTOPOINT;
6950f203c7eSBrian Somers   if (ID0ioctl(bundle.dev.fd, TUNSIFMODE, &iff) < 0)
6960f203c7eSBrian Somers     log_Printf(LogERROR, "bundle_Create: ioctl(TUNSIFMODE): %s\n",
6970f203c7eSBrian Somers 	       strerror(errno));
6980f203c7eSBrian Somers #endif
6990f203c7eSBrian Somers 
7006ca65df0SBrian Somers #ifdef TUNSLMODE
7016ca65df0SBrian Somers   /* Make sure we're POINTOPOINT */
7026ca65df0SBrian Somers   iff = 0;
7036ca65df0SBrian Somers   if (ID0ioctl(bundle.dev.fd, TUNSLMODE, &iff) < 0)
7046ca65df0SBrian Somers     log_Printf(LogERROR, "bundle_Create: ioctl(TUNSLMODE): %s\n",
7056ca65df0SBrian Somers 	       strerror(errno));
7066ca65df0SBrian Somers #endif
7076ca65df0SBrian Somers 
7084e5196e9SBrian Somers   if (!iface_SetFlags(bundle.iface, IFF_UP)) {
7098fa6ebe4SBrian Somers     iface_Destroy(bundle.iface);
7108fa6ebe4SBrian Somers     bundle.iface = NULL;
711faefde08SBrian Somers     close(bundle.dev.fd);
7127a6f8720SBrian Somers     return NULL;
7137a6f8720SBrian Somers   }
7147a6f8720SBrian Somers 
7158fa6ebe4SBrian Somers   log_Printf(LogPHASE, "Using interface: %s\n", ifname);
7167a6f8720SBrian Somers 
717ab2de065SBrian Somers   bundle.bandwidth = 0;
718820de6ebSBrian Somers   bundle.routing_seq = 0;
719a0cbd833SBrian Somers   bundle.phase = PHASE_DEAD;
720a0cbd833SBrian Somers   bundle.CleaningUp = 0;
72167b072f7SBrian Somers   bundle.NatEnabled = 0;
7227a6f8720SBrian Somers 
7236d666775SBrian Somers   bundle.fsm.LayerStart = bundle_LayerStart;
7246f384573SBrian Somers   bundle.fsm.LayerUp = bundle_LayerUp;
7256d666775SBrian Somers   bundle.fsm.LayerDown = bundle_LayerDown;
7266d666775SBrian Somers   bundle.fsm.LayerFinish = bundle_LayerFinish;
7276d666775SBrian Somers   bundle.fsm.object = &bundle;
7287a6f8720SBrian Somers 
729dade2407SBrian Somers   bundle.cfg.idle.timeout = NCP_IDLE_TIMEOUT;
730dade2407SBrian Somers   bundle.cfg.idle.min_timeout = 0;
7311342caedSBrian Somers   *bundle.cfg.auth.name = '\0';
7321342caedSBrian Somers   *bundle.cfg.auth.key = '\0';
733610b185fSBrian Somers   bundle.cfg.opt = OPT_SROUTES | OPT_IDCHECK | OPT_LOOPBACK |
734610b185fSBrian Somers                    OPT_THROUGHPUT | OPT_UTMP;
73549052c95SBrian Somers   *bundle.cfg.label = '\0';
73649052c95SBrian Somers   bundle.cfg.mtu = DEF_MTU;
7376f8e9f0aSBrian Somers   bundle.cfg.choked.timeout = CHOKED_TIMEOUT;
738ff0f9439SBrian Somers   bundle.phys_type.all = type;
739ff0f9439SBrian Somers   bundle.phys_type.open = 0;
740dade2407SBrian Somers   bundle.upat = 0;
741ab886ad0SBrian Somers 
7426f384573SBrian Somers   bundle.links = datalink_Create("deflink", &bundle, type);
7433006ec67SBrian Somers   if (bundle.links == NULL) {
744a33b2ef7SBrian Somers     log_Printf(LogALERT, "Cannot create data link: %s\n", strerror(errno));
7458fa6ebe4SBrian Somers     iface_Destroy(bundle.iface);
7468fa6ebe4SBrian Somers     bundle.iface = NULL;
747faefde08SBrian Somers     close(bundle.dev.fd);
7482289f246SBrian Somers     return NULL;
7492289f246SBrian Somers   }
7502289f246SBrian Somers 
7512f786681SBrian Somers   bundle.desc.type = BUNDLE_DESCRIPTOR;
7522f786681SBrian Somers   bundle.desc.UpdateSet = bundle_UpdateSet;
7532f786681SBrian Somers   bundle.desc.IsSet = bundle_IsSet;
7542f786681SBrian Somers   bundle.desc.Read = bundle_DescriptorRead;
7552f786681SBrian Somers   bundle.desc.Write = bundle_DescriptorWrite;
7562f786681SBrian Somers 
75749052c95SBrian Somers   mp_Init(&bundle.ncp.mp, &bundle);
75849052c95SBrian Somers 
75949052c95SBrian Somers   /* Send over the first physical link by default */
7605828db6dSBrian Somers   ipcp_Init(&bundle.ncp.ipcp, &bundle, &bundle.links->physical->link,
7615828db6dSBrian Somers             &bundle.fsm);
7626d666775SBrian Somers 
7635ca5389aSBrian Somers   memset(&bundle.filter, '\0', sizeof bundle.filter);
7645ca5389aSBrian Somers   bundle.filter.in.fragok = bundle.filter.in.logok = 1;
7655ca5389aSBrian Somers   bundle.filter.in.name = "IN";
7665ca5389aSBrian Somers   bundle.filter.out.fragok = bundle.filter.out.logok = 1;
7675ca5389aSBrian Somers   bundle.filter.out.name = "OUT";
7685ca5389aSBrian Somers   bundle.filter.dial.name = "DIAL";
7698390b576SBrian Somers   bundle.filter.dial.logok = 1;
7705ca5389aSBrian Somers   bundle.filter.alive.name = "ALIVE";
7715ca5389aSBrian Somers   bundle.filter.alive.logok = 1;
772cad7e742SBrian Somers   {
773cad7e742SBrian Somers     int	i;
774cad7e742SBrian Somers     for (i = 0; i < MAXFILTERS; i++) {
775cad7e742SBrian Somers         bundle.filter.in.rule[i].f_action = A_NONE;
776cad7e742SBrian Somers         bundle.filter.out.rule[i].f_action = A_NONE;
777cad7e742SBrian Somers         bundle.filter.dial.rule[i].f_action = A_NONE;
778cad7e742SBrian Somers         bundle.filter.alive.rule[i].f_action = A_NONE;
779cad7e742SBrian Somers     }
780cad7e742SBrian Somers   }
78193ee0ff2SBrian Somers   memset(&bundle.idle.timer, '\0', sizeof bundle.idle.timer);
78293ee0ff2SBrian Somers   bundle.idle.done = 0;
7835cf4388bSBrian Somers   bundle.notify.fd = -1;
7846f8e9f0aSBrian Somers   memset(&bundle.choked.timer, '\0', sizeof bundle.choked.timer);
785972a1bcfSBrian Somers #ifndef NORADIUS
786972a1bcfSBrian Somers   radius_Init(&bundle.radius);
787972a1bcfSBrian Somers #endif
78893ee0ff2SBrian Somers 
7896d666775SBrian Somers   /* Clean out any leftover crud */
7908fa6ebe4SBrian Somers   iface_Clear(bundle.iface, IFACE_CLEAR_ALL);
7916d666775SBrian Somers 
7921384bd27SBrian Somers   bundle_LockTun(&bundle);
7931384bd27SBrian Somers 
7947a6f8720SBrian Somers   return &bundle;
7957a6f8720SBrian Somers }
7967a6f8720SBrian Somers 
79768a0f0ccSBrian Somers static void
79868a0f0ccSBrian Somers bundle_DownInterface(struct bundle *bundle)
79968a0f0ccSBrian Somers {
800dd7e2610SBrian Somers   route_IfDelete(bundle, 1);
8014e5196e9SBrian Somers   iface_ClearFlags(bundle->iface, IFF_UP);
80268a0f0ccSBrian Somers }
80368a0f0ccSBrian Somers 
80468a0f0ccSBrian Somers void
80568a0f0ccSBrian Somers bundle_Destroy(struct bundle *bundle)
80668a0f0ccSBrian Somers {
8073006ec67SBrian Somers   struct datalink *dl;
808b6217683SBrian Somers 
809ea722969SBrian Somers   /*
81004eaa58cSBrian Somers    * Clean up the interface.  We don't need to timer_Stop()s, mp_Down(),
811ea722969SBrian Somers    * ipcp_CleanInterface() and bundle_DownInterface() unless we're getting
812ea722969SBrian Somers    * out under exceptional conditions such as a descriptor exception.
813ea722969SBrian Somers    */
81404eaa58cSBrian Somers   timer_Stop(&bundle->idle.timer);
8156f8e9f0aSBrian Somers   timer_Stop(&bundle->choked.timer);
81666f634b6SBrian Somers   mp_Down(&bundle->ncp.mp);
817dd7e2610SBrian Somers   ipcp_CleanInterface(&bundle->ncp.ipcp);
81868a0f0ccSBrian Somers   bundle_DownInterface(bundle);
8193006ec67SBrian Somers 
820972a1bcfSBrian Somers #ifndef NORADIUS
821972a1bcfSBrian Somers   /* Tell the radius server the bad news */
822972a1bcfSBrian Somers   radius_Destroy(&bundle->radius);
823972a1bcfSBrian Somers #endif
824972a1bcfSBrian Somers 
825ea722969SBrian Somers   /* Again, these are all DATALINK_CLOSED unless we're abending */
8263006ec67SBrian Somers   dl = bundle->links;
8273006ec67SBrian Somers   while (dl)
8283006ec67SBrian Somers     dl = datalink_Destroy(dl);
8293006ec67SBrian Somers 
830442f8495SBrian Somers   ipcp_Destroy(&bundle->ncp.ipcp);
831442f8495SBrian Somers 
8321384bd27SBrian Somers   close(bundle->dev.fd);
8331384bd27SBrian Somers   bundle_UnlockTun(bundle);
8341384bd27SBrian Somers 
835ea722969SBrian Somers   /* In case we never made PHASE_NETWORK */
8365cf4388bSBrian Somers   bundle_Notify(bundle, EX_ERRDEAD);
837b6217683SBrian Somers 
8388fa6ebe4SBrian Somers   iface_Destroy(bundle->iface);
8398fa6ebe4SBrian Somers   bundle->iface = NULL;
84068a0f0ccSBrian Somers }
84168a0f0ccSBrian Somers 
8427a6f8720SBrian Somers struct rtmsg {
8437a6f8720SBrian Somers   struct rt_msghdr m_rtm;
8447a6f8720SBrian Somers   char m_space[64];
8457a6f8720SBrian Somers };
8467a6f8720SBrian Somers 
847610b185fSBrian Somers int
848820de6ebSBrian Somers bundle_SetRoute(struct bundle *bundle, int cmd, struct in_addr dst,
8492062443fSBrian Somers                 struct in_addr gateway, struct in_addr mask, int bang, int ssh)
8507a6f8720SBrian Somers {
8517a6f8720SBrian Somers   struct rtmsg rtmes;
8527a6f8720SBrian Somers   int s, nb, wb;
8537a6f8720SBrian Somers   char *cp;
8547a6f8720SBrian Somers   const char *cmdstr;
8557a6f8720SBrian Somers   struct sockaddr_in rtdata;
856610b185fSBrian Somers   int result = 1;
8577a6f8720SBrian Somers 
8587a6f8720SBrian Somers   if (bang)
8597a6f8720SBrian Somers     cmdstr = (cmd == RTM_ADD ? "Add!" : "Delete!");
8607a6f8720SBrian Somers   else
8617a6f8720SBrian Somers     cmdstr = (cmd == RTM_ADD ? "Add" : "Delete");
8627a6f8720SBrian Somers   s = ID0socket(PF_ROUTE, SOCK_RAW, 0);
8637a6f8720SBrian Somers   if (s < 0) {
864dd7e2610SBrian Somers     log_Printf(LogERROR, "bundle_SetRoute: socket(): %s\n", strerror(errno));
865610b185fSBrian Somers     return result;
8667a6f8720SBrian Somers   }
8677a6f8720SBrian Somers   memset(&rtmes, '\0', sizeof rtmes);
8687a6f8720SBrian Somers   rtmes.m_rtm.rtm_version = RTM_VERSION;
8697a6f8720SBrian Somers   rtmes.m_rtm.rtm_type = cmd;
8707a6f8720SBrian Somers   rtmes.m_rtm.rtm_addrs = RTA_DST;
871820de6ebSBrian Somers   rtmes.m_rtm.rtm_seq = ++bundle->routing_seq;
8727a6f8720SBrian Somers   rtmes.m_rtm.rtm_pid = getpid();
8737a6f8720SBrian Somers   rtmes.m_rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;
8747a6f8720SBrian Somers 
8753afe5ccbSBrian Somers   if (cmd == RTM_ADD || cmd == RTM_CHANGE) {
8763afe5ccbSBrian Somers     if (bundle->ncp.ipcp.cfg.sendpipe > 0) {
8773afe5ccbSBrian Somers       rtmes.m_rtm.rtm_rmx.rmx_sendpipe = bundle->ncp.ipcp.cfg.sendpipe;
8783afe5ccbSBrian Somers       rtmes.m_rtm.rtm_inits |= RTV_SPIPE;
8793afe5ccbSBrian Somers     }
8803afe5ccbSBrian Somers     if (bundle->ncp.ipcp.cfg.recvpipe > 0) {
8813afe5ccbSBrian Somers       rtmes.m_rtm.rtm_rmx.rmx_recvpipe = bundle->ncp.ipcp.cfg.recvpipe;
8823afe5ccbSBrian Somers       rtmes.m_rtm.rtm_inits |= RTV_RPIPE;
8833afe5ccbSBrian Somers     }
8843afe5ccbSBrian Somers   }
8853afe5ccbSBrian Somers 
8867a6f8720SBrian Somers   memset(&rtdata, '\0', sizeof rtdata);
88750e5c17dSBrian Somers   rtdata.sin_len = sizeof rtdata;
8887a6f8720SBrian Somers   rtdata.sin_family = AF_INET;
8897a6f8720SBrian Somers   rtdata.sin_port = 0;
8907a6f8720SBrian Somers   rtdata.sin_addr = dst;
8917a6f8720SBrian Somers 
8927a6f8720SBrian Somers   cp = rtmes.m_space;
89350e5c17dSBrian Somers   memcpy(cp, &rtdata, rtdata.sin_len);
89450e5c17dSBrian Somers   cp += rtdata.sin_len;
895e43ebac1SBrian Somers   if (cmd == RTM_ADD) {
8967a6f8720SBrian Somers     if (gateway.s_addr == INADDR_ANY) {
8970aa8aa17SBrian Somers       if (!ssh)
8988fa6ebe4SBrian Somers         log_Printf(LogERROR, "bundle_SetRoute: Cannot add a route with"
8998fa6ebe4SBrian Somers                    " destination 0.0.0.0\n");
9008fa6ebe4SBrian Somers       close(s);
9018fa6ebe4SBrian Somers       return result;
9027a6f8720SBrian Somers     } else {
9037a6f8720SBrian Somers       rtdata.sin_addr = gateway;
90450e5c17dSBrian Somers       memcpy(cp, &rtdata, rtdata.sin_len);
90550e5c17dSBrian Somers       cp += rtdata.sin_len;
9067a6f8720SBrian Somers       rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY;
9077a6f8720SBrian Somers     }
908e43ebac1SBrian Somers   }
9097a6f8720SBrian Somers 
9107a6f8720SBrian Somers   if (dst.s_addr == INADDR_ANY)
9117a6f8720SBrian Somers     mask.s_addr = INADDR_ANY;
9127a6f8720SBrian Somers 
9137a6f8720SBrian Somers   if (cmd == RTM_ADD || dst.s_addr == INADDR_ANY) {
9147a6f8720SBrian Somers     rtdata.sin_addr = mask;
91550e5c17dSBrian Somers     memcpy(cp, &rtdata, rtdata.sin_len);
91650e5c17dSBrian Somers     cp += rtdata.sin_len;
9177a6f8720SBrian Somers     rtmes.m_rtm.rtm_addrs |= RTA_NETMASK;
9187a6f8720SBrian Somers   }
9197a6f8720SBrian Somers 
9207a6f8720SBrian Somers   nb = cp - (char *) &rtmes;
9217a6f8720SBrian Somers   rtmes.m_rtm.rtm_msglen = nb;
9227a6f8720SBrian Somers   wb = ID0write(s, &rtmes, nb);
9237a6f8720SBrian Somers   if (wb < 0) {
924dd7e2610SBrian Somers     log_Printf(LogTCPIP, "bundle_SetRoute failure:\n");
925dd7e2610SBrian Somers     log_Printf(LogTCPIP, "bundle_SetRoute:  Cmd = %s\n", cmdstr);
926dd7e2610SBrian Somers     log_Printf(LogTCPIP, "bundle_SetRoute:  Dst = %s\n", inet_ntoa(dst));
9272cb305afSBrian Somers     log_Printf(LogTCPIP, "bundle_SetRoute:  Gateway = %s\n",
9282cb305afSBrian Somers                inet_ntoa(gateway));
929dd7e2610SBrian Somers     log_Printf(LogTCPIP, "bundle_SetRoute:  Mask = %s\n", inet_ntoa(mask));
9307a6f8720SBrian Somers failed:
9317a6f8720SBrian Somers     if (cmd == RTM_ADD && (rtmes.m_rtm.rtm_errno == EEXIST ||
932e43ebac1SBrian Somers                            (rtmes.m_rtm.rtm_errno == 0 && errno == EEXIST))) {
933610b185fSBrian Somers       if (!bang) {
934dd7e2610SBrian Somers         log_Printf(LogWARN, "Add route failed: %s already exists\n",
9353afe5ccbSBrian Somers 		  dst.s_addr == 0 ? "default" : inet_ntoa(dst));
936610b185fSBrian Somers         result = 0;	/* Don't add to our dynamic list */
937610b185fSBrian Somers       } else {
9387a6f8720SBrian Somers         rtmes.m_rtm.rtm_type = cmd = RTM_CHANGE;
9397a6f8720SBrian Somers         if ((wb = ID0write(s, &rtmes, nb)) < 0)
9407a6f8720SBrian Somers           goto failed;
9417a6f8720SBrian Somers       }
942e43ebac1SBrian Somers     } else if (cmd == RTM_DELETE &&
9437a6f8720SBrian Somers              (rtmes.m_rtm.rtm_errno == ESRCH ||
9447a6f8720SBrian Somers               (rtmes.m_rtm.rtm_errno == 0 && errno == ESRCH))) {
9457a6f8720SBrian Somers       if (!bang)
946dd7e2610SBrian Somers         log_Printf(LogWARN, "Del route failed: %s: Non-existent\n",
9477a6f8720SBrian Somers                   inet_ntoa(dst));
9482062443fSBrian Somers     } else if (rtmes.m_rtm.rtm_errno == 0) {
9492062443fSBrian Somers       if (!ssh || errno != ENETUNREACH)
950dd7e2610SBrian Somers         log_Printf(LogWARN, "%s route failed: %s: errno: %s\n", cmdstr,
9517a6f8720SBrian Somers                    inet_ntoa(dst), strerror(errno));
9522062443fSBrian Somers     } else
953dd7e2610SBrian Somers       log_Printf(LogWARN, "%s route failed: %s: %s\n",
9547a6f8720SBrian Somers 		 cmdstr, inet_ntoa(dst), strerror(rtmes.m_rtm.rtm_errno));
9557a6f8720SBrian Somers   }
956dd7e2610SBrian Somers   log_Printf(LogDEBUG, "wrote %d: cmd = %s, dst = %x, gateway = %x\n",
957fe3125a0SBrian Somers             wb, cmdstr, (unsigned)dst.s_addr, (unsigned)gateway.s_addr);
9587a6f8720SBrian Somers   close(s);
959610b185fSBrian Somers 
960610b185fSBrian Somers   return result;
9617a6f8720SBrian Somers }
96283d1af55SBrian Somers 
96383d1af55SBrian Somers void
9643006ec67SBrian Somers bundle_LinkClosed(struct bundle *bundle, struct datalink *dl)
9653006ec67SBrian Somers {
9663006ec67SBrian Somers   /*
9673006ec67SBrian Somers    * Our datalink has closed.
968ea722969SBrian Somers    * CleanDatalinks() (called from DoLoop()) will remove closed
96981358fa3SBrian Somers    * BACKGROUND and DIRECT links.
9703b0f8d2eSBrian Somers    * If it's the last data link, enter phase DEAD.
971ea722969SBrian Somers    *
972ea722969SBrian Somers    * NOTE: dl may not be in our list (bundle_SendDatalink()) !
9733006ec67SBrian Somers    */
9745b8b8060SBrian Somers 
9753b0f8d2eSBrian Somers   struct datalink *odl;
9763b0f8d2eSBrian Somers   int other_links;
9775b8b8060SBrian Somers 
978bf1d3ff6SBrian Somers   log_SetTtyCommandMode(dl);
979bf1d3ff6SBrian Somers 
9803b0f8d2eSBrian Somers   other_links = 0;
9813b0f8d2eSBrian Somers   for (odl = bundle->links; odl; odl = odl->next)
9823b0f8d2eSBrian Somers     if (odl != dl && odl->state != DATALINK_CLOSED)
9833b0f8d2eSBrian Somers       other_links++;
9843b0f8d2eSBrian Somers 
9853b0f8d2eSBrian Somers   if (!other_links) {
98681358fa3SBrian Somers     if (dl->physical->type != PHYS_AUTO)	/* Not in -auto mode */
98703704096SBrian Somers       bundle_DownInterface(bundle);
98809206a6fSBrian Somers     fsm2initial(&bundle->ncp.ipcp.fsm);
9895563ebdeSBrian Somers     bundle_NewPhase(bundle, PHASE_DEAD);
9900f2f3eb3SBrian Somers     bundle_StopIdleTimer(bundle);
991ab2de065SBrian Somers   }
992455aabc3SBrian Somers }
993455aabc3SBrian Somers 
994455aabc3SBrian Somers void
995ba23f397SBrian Somers bundle_Open(struct bundle *bundle, const char *name, int mask, int force)
9963006ec67SBrian Somers {
9973006ec67SBrian Somers   /*
9983006ec67SBrian Somers    * Please open the given datalink, or all if name == NULL
9993006ec67SBrian Somers    */
10003006ec67SBrian Somers   struct datalink *dl;
10013006ec67SBrian Somers 
10023006ec67SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
10033006ec67SBrian Somers     if (name == NULL || !strcasecmp(dl->name, name)) {
1004ba23f397SBrian Somers       if ((mask & dl->physical->type) &&
1005ba23f397SBrian Somers           (dl->state == DATALINK_CLOSED ||
1006ba23f397SBrian Somers            (force && dl->state == DATALINK_OPENING &&
1007c11e57a3SBrian Somers             dl->dial.timer.state == TIMER_RUNNING))) {
1008c11e57a3SBrian Somers         if (force)	/* Ignore redial timeout ? */
1009c11e57a3SBrian Somers           timer_Stop(&dl->dial.timer);
1010565e35e5SBrian Somers         datalink_Up(dl, 1, 1);
1011ab2de065SBrian Somers         if (mask & PHYS_AUTO)
1012ab2de065SBrian Somers           /* Only one AUTO link at a time */
101304eaa58cSBrian Somers           break;
101404eaa58cSBrian Somers       }
10153006ec67SBrian Somers       if (name != NULL)
10163006ec67SBrian Somers         break;
10173006ec67SBrian Somers     }
10183006ec67SBrian Somers }
10193006ec67SBrian Somers 
10203006ec67SBrian Somers struct datalink *
10213006ec67SBrian Somers bundle2datalink(struct bundle *bundle, const char *name)
10223006ec67SBrian Somers {
10233006ec67SBrian Somers   struct datalink *dl;
10243006ec67SBrian Somers 
10253006ec67SBrian Somers   if (name != NULL) {
10263006ec67SBrian Somers     for (dl = bundle->links; dl; dl = dl->next)
10273006ec67SBrian Somers       if (!strcasecmp(dl->name, name))
10283006ec67SBrian Somers         return dl;
10293006ec67SBrian Somers   } else if (bundle->links && !bundle->links->next)
10303006ec67SBrian Somers     return bundle->links;
10313006ec67SBrian Somers 
10323006ec67SBrian Somers   return NULL;
10333006ec67SBrian Somers }
10343006ec67SBrian Somers 
10353006ec67SBrian Somers int
10363006ec67SBrian Somers bundle_FillQueues(struct bundle *bundle)
10373006ec67SBrian Somers {
10383b0f8d2eSBrian Somers   int total;
10393b0f8d2eSBrian Somers 
10401bc9b5baSBrian Somers   if (bundle->ncp.mp.active)
10413b0f8d2eSBrian Somers     total = mp_FillQueues(bundle);
10421bc9b5baSBrian Somers   else {
10431bc9b5baSBrian Somers     struct datalink *dl;
10441bc9b5baSBrian Somers     int add;
10451bc9b5baSBrian Somers 
10461bc9b5baSBrian Somers     for (total = 0, dl = bundle->links; dl; dl = dl->next)
10471bc9b5baSBrian Somers       if (dl->state == DATALINK_OPEN) {
10481bc9b5baSBrian Somers         add = link_QueueLen(&dl->physical->link);
10491bc9b5baSBrian Somers         if (add == 0 && dl->physical->out == NULL)
10505d9e6103SBrian Somers           add = ip_PushPacket(&dl->physical->link, bundle);
10511bc9b5baSBrian Somers         total += add;
10521bc9b5baSBrian Somers       }
10533006ec67SBrian Somers   }
10543006ec67SBrian Somers 
10555a72b6edSBrian Somers   return total + ip_QueueLen(&bundle->ncp.ipcp);
10563006ec67SBrian Somers }
1057aef795ccSBrian Somers 
1058aef795ccSBrian Somers int
1059aef795ccSBrian Somers bundle_ShowLinks(struct cmdargs const *arg)
1060aef795ccSBrian Somers {
1061aef795ccSBrian Somers   struct datalink *dl;
1062ab2de065SBrian Somers   struct pppThroughput *t;
1063ab2de065SBrian Somers   int secs;
1064aef795ccSBrian Somers 
10659c53a7b1SBrian Somers   for (dl = arg->bundle->links; dl; dl = dl->next) {
1066d4156d00SBrian Somers     prompt_Printf(arg->prompt, "Name: %s [%s, %s]",
1067d4156d00SBrian Somers                   dl->name, mode2Nam(dl->physical->type), datalink_State(dl));
1068eeab6bf5SBrian Somers     if (dl->physical->link.throughput.rolling && dl->state == DATALINK_OPEN)
1069e531f89aSBrian Somers       prompt_Printf(arg->prompt, " bandwidth %d, %llu bps (%llu bytes/sec)",
1070ab2de065SBrian Somers                     dl->mp.bandwidth ? dl->mp.bandwidth :
1071ab2de065SBrian Somers                                        physical_GetSpeed(dl->physical),
1072ab2de065SBrian Somers                     dl->physical->link.throughput.OctetsPerSecond * 8,
10739c53a7b1SBrian Somers                     dl->physical->link.throughput.OctetsPerSecond);
10749c53a7b1SBrian Somers     prompt_Printf(arg->prompt, "\n");
10759c53a7b1SBrian Somers   }
1076aef795ccSBrian Somers 
1077ab2de065SBrian Somers   t = &arg->bundle->ncp.mp.link.throughput;
1078ab2de065SBrian Somers   secs = t->downtime ? 0 : throughput_uptime(t);
1079ab2de065SBrian Somers   if (secs > t->SamplePeriod)
1080ab2de065SBrian Somers     secs = t->SamplePeriod;
1081ab2de065SBrian Somers   if (secs)
1082e531f89aSBrian Somers     prompt_Printf(arg->prompt, "Currently averaging %llu bps (%llu bytes/sec)"
1083ab2de065SBrian Somers                   " over the last %d secs\n", t->OctetsPerSecond * 8,
1084ab2de065SBrian Somers                   t->OctetsPerSecond, secs);
1085ab2de065SBrian Somers 
1086aef795ccSBrian Somers   return 0;
1087aef795ccSBrian Somers }
1088ab886ad0SBrian Somers 
10891342caedSBrian Somers static const char *
10901342caedSBrian Somers optval(struct bundle *bundle, int bit)
10911342caedSBrian Somers {
10921342caedSBrian Somers   return (bundle->cfg.opt & bit) ? "enabled" : "disabled";
10931342caedSBrian Somers }
10941342caedSBrian Somers 
1095c08717dfSBrian Somers int
1096c08717dfSBrian Somers bundle_ShowStatus(struct cmdargs const *arg)
1097c08717dfSBrian Somers {
1098c08717dfSBrian Somers   int remaining;
1099c08717dfSBrian Somers 
1100c08717dfSBrian Somers   prompt_Printf(arg->prompt, "Phase %s\n", bundle_PhaseName(arg->bundle));
1101faefde08SBrian Somers   prompt_Printf(arg->prompt, " Device:        %s\n", arg->bundle->dev.Name);
1102dade2407SBrian Somers   prompt_Printf(arg->prompt, " Interface:     %s @ %lubps",
1103ab2de065SBrian Somers                 arg->bundle->iface->name, arg->bundle->bandwidth);
1104c08717dfSBrian Somers 
1105dade2407SBrian Somers   if (arg->bundle->upat) {
1106dade2407SBrian Somers     int secs = time(NULL) - arg->bundle->upat;
1107dade2407SBrian Somers 
1108dade2407SBrian Somers     prompt_Printf(arg->prompt, ", up time %d:%02d:%02d", secs / 3600,
1109dade2407SBrian Somers                   (secs / 60) % 60, secs % 60);
1110dade2407SBrian Somers   }
1111dade2407SBrian Somers 
1112dade2407SBrian Somers   prompt_Printf(arg->prompt, "\n\nDefaults:\n");
1113643f4904SBrian Somers   prompt_Printf(arg->prompt, " Label:         %s\n", arg->bundle->cfg.label);
1114610b185fSBrian Somers   prompt_Printf(arg->prompt, " Auth name:     %s\n",
1115610b185fSBrian Somers                 arg->bundle->cfg.auth.name);
111604eaa58cSBrian Somers 
11176f8e9f0aSBrian Somers   prompt_Printf(arg->prompt, " Choked Timer:  %ds\n",
11186f8e9f0aSBrian Somers                 arg->bundle->cfg.choked.timeout);
1119972a1bcfSBrian Somers 
1120972a1bcfSBrian Somers #ifndef NORADIUS
1121972a1bcfSBrian Somers   radius_Show(&arg->bundle->radius, arg->prompt);
1122972a1bcfSBrian Somers #endif
1123972a1bcfSBrian Somers 
1124c08717dfSBrian Somers   prompt_Printf(arg->prompt, " Idle Timer:    ");
1125dade2407SBrian Somers   if (arg->bundle->cfg.idle.timeout) {
1126dade2407SBrian Somers     prompt_Printf(arg->prompt, "%ds", arg->bundle->cfg.idle.timeout);
1127dade2407SBrian Somers     if (arg->bundle->cfg.idle.min_timeout)
1128dade2407SBrian Somers       prompt_Printf(arg->prompt, ", min %ds",
1129dade2407SBrian Somers                     arg->bundle->cfg.idle.min_timeout);
1130c08717dfSBrian Somers     remaining = bundle_RemainingIdleTime(arg->bundle);
1131c08717dfSBrian Somers     if (remaining != -1)
1132c08717dfSBrian Somers       prompt_Printf(arg->prompt, " (%ds remaining)", remaining);
1133c08717dfSBrian Somers     prompt_Printf(arg->prompt, "\n");
1134c08717dfSBrian Somers   } else
1135c08717dfSBrian Somers     prompt_Printf(arg->prompt, "disabled\n");
1136ce828a6eSBrian Somers   prompt_Printf(arg->prompt, " MTU:           ");
1137ce828a6eSBrian Somers   if (arg->bundle->cfg.mtu)
1138ce828a6eSBrian Somers     prompt_Printf(arg->prompt, "%d\n", arg->bundle->cfg.mtu);
1139ce828a6eSBrian Somers   else
1140ce828a6eSBrian Somers     prompt_Printf(arg->prompt, "unspecified\n");
1141c08717dfSBrian Somers 
11423afe5ccbSBrian Somers   prompt_Printf(arg->prompt, " sendpipe:      ");
11433afe5ccbSBrian Somers   if (arg->bundle->ncp.ipcp.cfg.sendpipe > 0)
1144ab2de065SBrian Somers     prompt_Printf(arg->prompt, "%-20ld", arg->bundle->ncp.ipcp.cfg.sendpipe);
11453afe5ccbSBrian Somers   else
1146ab2de065SBrian Somers     prompt_Printf(arg->prompt, "unspecified         ");
11473afe5ccbSBrian Somers   prompt_Printf(arg->prompt, " recvpipe:      ");
11483afe5ccbSBrian Somers   if (arg->bundle->ncp.ipcp.cfg.recvpipe > 0)
11493afe5ccbSBrian Somers     prompt_Printf(arg->prompt, "%ld\n", arg->bundle->ncp.ipcp.cfg.recvpipe);
11503afe5ccbSBrian Somers   else
11513afe5ccbSBrian Somers     prompt_Printf(arg->prompt, "unspecified\n");
11523afe5ccbSBrian Somers 
1153ab2de065SBrian Somers   prompt_Printf(arg->prompt, " Sticky Routes: %-20.20s",
1154610b185fSBrian Somers                 optval(arg->bundle, OPT_SROUTES));
11551342caedSBrian Somers   prompt_Printf(arg->prompt, " ID check:      %s\n",
11561342caedSBrian Somers                 optval(arg->bundle, OPT_IDCHECK));
1157ab2de065SBrian Somers   prompt_Printf(arg->prompt, " Keep-Session:  %-20.20s",
1158ac685e31SBrian Somers                 optval(arg->bundle, OPT_KEEPSESSION));
11591342caedSBrian Somers   prompt_Printf(arg->prompt, " Loopback:      %s\n",
11601342caedSBrian Somers                 optval(arg->bundle, OPT_LOOPBACK));
1161ab2de065SBrian Somers   prompt_Printf(arg->prompt, " PasswdAuth:    %-20.20s",
11621342caedSBrian Somers                 optval(arg->bundle, OPT_PASSWDAUTH));
11631342caedSBrian Somers   prompt_Printf(arg->prompt, " Proxy:         %s\n",
11641342caedSBrian Somers                 optval(arg->bundle, OPT_PROXY));
1165ab2de065SBrian Somers   prompt_Printf(arg->prompt, " Proxyall:      %-20.20s",
11663afe5ccbSBrian Somers                 optval(arg->bundle, OPT_PROXYALL));
11671342caedSBrian Somers   prompt_Printf(arg->prompt, " Throughput:    %s\n",
11681342caedSBrian Somers                 optval(arg->bundle, OPT_THROUGHPUT));
1169ab2de065SBrian Somers   prompt_Printf(arg->prompt, " Utmp Logging:  %-20.20s",
11701342caedSBrian Somers                 optval(arg->bundle, OPT_UTMP));
11718fa6ebe4SBrian Somers   prompt_Printf(arg->prompt, " Iface-Alias:   %s\n",
11728fa6ebe4SBrian Somers                 optval(arg->bundle, OPT_IFACEALIAS));
11731342caedSBrian Somers 
1174c08717dfSBrian Somers   return 0;
1175c08717dfSBrian Somers }
1176c08717dfSBrian Somers 
1177ab886ad0SBrian Somers static void
1178ab886ad0SBrian Somers bundle_IdleTimeout(void *v)
1179ab886ad0SBrian Somers {
1180ab886ad0SBrian Somers   struct bundle *bundle = (struct bundle *)v;
1181ab886ad0SBrian Somers 
1182dd7e2610SBrian Somers   log_Printf(LogPHASE, "Idle timer expired.\n");
118304eaa58cSBrian Somers   bundle_StopIdleTimer(bundle);
11849c81b87dSBrian Somers   bundle_Close(bundle, NULL, CLOSE_STAYDOWN);
1185ab886ad0SBrian Somers }
1186ab886ad0SBrian Somers 
1187ab886ad0SBrian Somers /*
1188ab886ad0SBrian Somers  *  Start Idle timer. If timeout is reached, we call bundle_Close() to
1189ab886ad0SBrian Somers  *  close LCP and link.
1190ab886ad0SBrian Somers  */
1191ab886ad0SBrian Somers void
1192ab886ad0SBrian Somers bundle_StartIdleTimer(struct bundle *bundle)
1193ab886ad0SBrian Somers {
1194dd7e2610SBrian Somers   timer_Stop(&bundle->idle.timer);
1195ff0f9439SBrian Somers   if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) !=
1196dade2407SBrian Somers       bundle->phys_type.open && bundle->cfg.idle.timeout) {
1197dade2407SBrian Somers     int secs;
1198dade2407SBrian Somers 
1199dade2407SBrian Somers     secs = bundle->cfg.idle.timeout;
1200dade2407SBrian Somers     if (bundle->cfg.idle.min_timeout > secs && bundle->upat) {
1201dade2407SBrian Somers       int up = time(NULL) - bundle->upat;
1202dade2407SBrian Somers 
1203dade2407SBrian Somers       if ((long long)bundle->cfg.idle.min_timeout - up > (long long)secs)
1204dade2407SBrian Somers         secs = bundle->cfg.idle.min_timeout - up;
1205dade2407SBrian Somers     }
120693ee0ff2SBrian Somers     bundle->idle.timer.func = bundle_IdleTimeout;
12073b0f8d2eSBrian Somers     bundle->idle.timer.name = "idle";
1208dade2407SBrian Somers     bundle->idle.timer.load = secs * SECTICKS;
120993ee0ff2SBrian Somers     bundle->idle.timer.arg = bundle;
1210dd7e2610SBrian Somers     timer_Start(&bundle->idle.timer);
1211dade2407SBrian Somers     bundle->idle.done = time(NULL) + secs;
1212ab886ad0SBrian Somers   }
1213ab886ad0SBrian Somers }
1214ab886ad0SBrian Somers 
1215ab886ad0SBrian Somers void
1216dade2407SBrian Somers bundle_SetIdleTimer(struct bundle *bundle, int timeout, int min_timeout)
1217ab886ad0SBrian Somers {
1218dade2407SBrian Somers   bundle->cfg.idle.timeout = timeout;
1219dade2407SBrian Somers   if (min_timeout >= 0)
1220dade2407SBrian Somers     bundle->cfg.idle.min_timeout = min_timeout;
1221ab886ad0SBrian Somers   if (bundle_LinkIsUp(bundle))
1222ab886ad0SBrian Somers     bundle_StartIdleTimer(bundle);
1223ab886ad0SBrian Somers }
1224ab886ad0SBrian Somers 
1225ab886ad0SBrian Somers void
1226ab886ad0SBrian Somers bundle_StopIdleTimer(struct bundle *bundle)
1227ab886ad0SBrian Somers {
1228dd7e2610SBrian Somers   timer_Stop(&bundle->idle.timer);
12294a632c80SBrian Somers   bundle->idle.done = 0;
1230ab886ad0SBrian Somers }
1231ab886ad0SBrian Somers 
123204eaa58cSBrian Somers static int
1233ab886ad0SBrian Somers bundle_RemainingIdleTime(struct bundle *bundle)
1234ab886ad0SBrian Somers {
123593ee0ff2SBrian Somers   if (bundle->idle.done)
123693ee0ff2SBrian Somers     return bundle->idle.done - time(NULL);
1237ab886ad0SBrian Somers   return -1;
1238ab886ad0SBrian Somers }
12393b0f8d2eSBrian Somers 
12403b0f8d2eSBrian Somers int
12413b0f8d2eSBrian Somers bundle_IsDead(struct bundle *bundle)
12423b0f8d2eSBrian Somers {
12433b0f8d2eSBrian Somers   return !bundle->links || (bundle->phase == PHASE_DEAD && bundle->CleaningUp);
12443b0f8d2eSBrian Somers }
1245b6217683SBrian Somers 
124604eaa58cSBrian Somers static struct datalink *
124704eaa58cSBrian Somers bundle_DatalinkLinkout(struct bundle *bundle, struct datalink *dl)
1248cd7bd93aSBrian Somers {
1249cd7bd93aSBrian Somers   struct datalink **dlp;
1250cd7bd93aSBrian Somers 
1251cd7bd93aSBrian Somers   for (dlp = &bundle->links; *dlp; dlp = &(*dlp)->next)
1252cd7bd93aSBrian Somers     if (*dlp == dl) {
125304eaa58cSBrian Somers       *dlp = dl->next;
125404eaa58cSBrian Somers       dl->next = NULL;
125504eaa58cSBrian Somers       bundle_LinksRemoved(bundle);
125604eaa58cSBrian Somers       return dl;
1257cd7bd93aSBrian Somers     }
125804eaa58cSBrian Somers 
125904eaa58cSBrian Somers   return NULL;
126004eaa58cSBrian Somers }
126104eaa58cSBrian Somers 
126204eaa58cSBrian Somers static void
126304eaa58cSBrian Somers bundle_DatalinkLinkin(struct bundle *bundle, struct datalink *dl)
126404eaa58cSBrian Somers {
126504eaa58cSBrian Somers   struct datalink **dlp = &bundle->links;
126604eaa58cSBrian Somers 
126704eaa58cSBrian Somers   while (*dlp)
126804eaa58cSBrian Somers     dlp = &(*dlp)->next;
126904eaa58cSBrian Somers 
127004eaa58cSBrian Somers   *dlp = dl;
127104eaa58cSBrian Somers   dl->next = NULL;
127204eaa58cSBrian Somers 
127304eaa58cSBrian Somers   bundle_LinkAdded(bundle, dl);
1274ab2de065SBrian Somers   mp_CheckAutoloadTimer(&bundle->ncp.mp);
1275565e35e5SBrian Somers }
1276565e35e5SBrian Somers 
1277565e35e5SBrian Somers void
1278565e35e5SBrian Somers bundle_CleanDatalinks(struct bundle *bundle)
1279565e35e5SBrian Somers {
1280565e35e5SBrian Somers   struct datalink **dlp = &bundle->links;
128104eaa58cSBrian Somers   int found = 0;
1282565e35e5SBrian Somers 
1283565e35e5SBrian Somers   while (*dlp)
1284565e35e5SBrian Somers     if ((*dlp)->state == DATALINK_CLOSED &&
128581358fa3SBrian Somers         (*dlp)->physical->type & (PHYS_DIRECT|PHYS_BACKGROUND)) {
1286565e35e5SBrian Somers       *dlp = datalink_Destroy(*dlp);
128704eaa58cSBrian Somers       found++;
128804eaa58cSBrian Somers     } else
1289565e35e5SBrian Somers       dlp = &(*dlp)->next;
129004eaa58cSBrian Somers 
129104eaa58cSBrian Somers   if (found)
129204eaa58cSBrian Somers     bundle_LinksRemoved(bundle);
129304eaa58cSBrian Somers }
129404eaa58cSBrian Somers 
129504eaa58cSBrian Somers int
129604eaa58cSBrian Somers bundle_DatalinkClone(struct bundle *bundle, struct datalink *dl,
129704eaa58cSBrian Somers                      const char *name)
129804eaa58cSBrian Somers {
129904eaa58cSBrian Somers   if (bundle2datalink(bundle, name)) {
130004eaa58cSBrian Somers     log_Printf(LogWARN, "Clone: %s: name already exists\n", name);
130104eaa58cSBrian Somers     return 0;
130204eaa58cSBrian Somers   }
130304eaa58cSBrian Somers 
130404eaa58cSBrian Somers   bundle_DatalinkLinkin(bundle, datalink_Clone(dl, name));
130504eaa58cSBrian Somers   return 1;
130604eaa58cSBrian Somers }
130704eaa58cSBrian Somers 
130804eaa58cSBrian Somers void
130904eaa58cSBrian Somers bundle_DatalinkRemove(struct bundle *bundle, struct datalink *dl)
131004eaa58cSBrian Somers {
131104eaa58cSBrian Somers   dl = bundle_DatalinkLinkout(bundle, dl);
131204eaa58cSBrian Somers   if (dl)
131304eaa58cSBrian Somers     datalink_Destroy(dl);
1314cd7bd93aSBrian Somers }
131549052c95SBrian Somers 
131649052c95SBrian Somers void
131749052c95SBrian Somers bundle_SetLabel(struct bundle *bundle, const char *label)
131849052c95SBrian Somers {
131949052c95SBrian Somers   if (label)
132049052c95SBrian Somers     strncpy(bundle->cfg.label, label, sizeof bundle->cfg.label - 1);
132149052c95SBrian Somers   else
132249052c95SBrian Somers     *bundle->cfg.label = '\0';
132349052c95SBrian Somers }
132449052c95SBrian Somers 
132549052c95SBrian Somers const char *
132649052c95SBrian Somers bundle_GetLabel(struct bundle *bundle)
132749052c95SBrian Somers {
132849052c95SBrian Somers   return *bundle->cfg.label ? bundle->cfg.label : NULL;
132949052c95SBrian Somers }
13301fa665f5SBrian Somers 
13312cb305afSBrian Somers int
13322cb305afSBrian Somers bundle_LinkSize()
13331fa665f5SBrian Somers {
133496c9bb21SBrian Somers   struct iovec iov[SCATTER_SEGMENTS];
13352cb305afSBrian Somers   int niov, expect, f;
133687c3786eSBrian Somers 
133796c9bb21SBrian Somers   iov[0].iov_len = strlen(Version) + 1;
13382cb305afSBrian Somers   iov[0].iov_base = NULL;
13392cb305afSBrian Somers   niov = 1;
13402cb305afSBrian Somers   if (datalink2iov(NULL, iov, &niov, SCATTER_SEGMENTS, NULL, NULL) == -1) {
13412cb305afSBrian Somers     log_Printf(LogERROR, "Cannot determine space required for link\n");
13422cb305afSBrian Somers     return 0;
134354cd8e13SBrian Somers   }
13446f384573SBrian Somers 
134596c9bb21SBrian Somers   for (f = expect = 0; f < niov; f++)
134696c9bb21SBrian Somers     expect += iov[f].iov_len;
134796c9bb21SBrian Somers 
13482cb305afSBrian Somers   return expect;
134987c3786eSBrian Somers }
135096c9bb21SBrian Somers 
13512cb305afSBrian Somers void
13522cb305afSBrian Somers bundle_ReceiveDatalink(struct bundle *bundle, int s)
13532cb305afSBrian Somers {
13542cb305afSBrian Somers   char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int) * SEND_MAXFD];
13552cb305afSBrian Somers   int niov, expect, f, *fd, nfd, onfd;
13562cb305afSBrian Somers   struct iovec iov[SCATTER_SEGMENTS];
13572cb305afSBrian Somers   struct cmsghdr *cmsg;
13582cb305afSBrian Somers   struct msghdr msg;
13592cb305afSBrian Somers   struct datalink *dl;
13602cb305afSBrian Somers   pid_t pid;
13612cb305afSBrian Somers   char ack;
13622cb305afSBrian Somers 
13632cb305afSBrian Somers   log_Printf(LogPHASE, "Receiving datalink\n");
13642cb305afSBrian Somers 
13652cb305afSBrian Somers   /*
13662cb305afSBrian Somers    * Create our scatter/gather array - passing NULL gets the space
13672cb305afSBrian Somers    * allocation requirement rather than actually flattening the
13682cb305afSBrian Somers    * structures.
13692cb305afSBrian Somers    */
13702cb305afSBrian Somers   iov[0].iov_len = strlen(Version) + 1;
13712cb305afSBrian Somers   iov[0].iov_base = NULL;
13722cb305afSBrian Somers   niov = 1;
13732cb305afSBrian Somers   if (datalink2iov(NULL, iov, &niov, SCATTER_SEGMENTS, NULL, NULL) == -1) {
13742cb305afSBrian Somers     log_Printf(LogERROR, "Cannot determine space required for link\n");
13752cb305afSBrian Somers     return;
13762cb305afSBrian Somers   }
13772cb305afSBrian Somers 
13782cb305afSBrian Somers   /* Allocate the scatter/gather array for recvmsg() */
13792cb305afSBrian Somers   for (f = expect = 0; f < niov; f++) {
13802cb305afSBrian Somers     if ((iov[f].iov_base = malloc(iov[f].iov_len)) == NULL) {
13812cb305afSBrian Somers       log_Printf(LogERROR, "Cannot allocate space to receive link\n");
13822cb305afSBrian Somers       return;
13832cb305afSBrian Somers     }
13842cb305afSBrian Somers     expect += iov[f].iov_len;
13852cb305afSBrian Somers   }
13862cb305afSBrian Somers 
13872cb305afSBrian Somers   /* Set up our message */
13882cb305afSBrian Somers   cmsg = (struct cmsghdr *)cmsgbuf;
13892cb305afSBrian Somers   cmsg->cmsg_len = sizeof cmsgbuf;
13902cb305afSBrian Somers   cmsg->cmsg_level = SOL_SOCKET;
13912cb305afSBrian Somers   cmsg->cmsg_type = 0;
13922cb305afSBrian Somers 
139396c9bb21SBrian Somers   memset(&msg, '\0', sizeof msg);
13942cb305afSBrian Somers   msg.msg_name = NULL;
13952cb305afSBrian Somers   msg.msg_namelen = 0;
139696c9bb21SBrian Somers   msg.msg_iov = iov;
139796c9bb21SBrian Somers   msg.msg_iovlen = niov;
139896c9bb21SBrian Somers   msg.msg_control = cmsgbuf;
139996c9bb21SBrian Somers   msg.msg_controllen = sizeof cmsgbuf;
140096c9bb21SBrian Somers 
140196c9bb21SBrian Somers   log_Printf(LogDEBUG, "Expecting %d scatter/gather bytes\n", expect);
14022cb305afSBrian Somers 
140396c9bb21SBrian Somers   if ((f = recvmsg(s, &msg, MSG_WAITALL)) != expect) {
140496c9bb21SBrian Somers     if (f == -1)
140596c9bb21SBrian Somers       log_Printf(LogERROR, "Failed recvmsg: %s\n", strerror(errno));
140696c9bb21SBrian Somers     else
140796c9bb21SBrian Somers       log_Printf(LogERROR, "Failed recvmsg: Got %d, not %d\n", f, expect);
140896c9bb21SBrian Somers     while (niov--)
140996c9bb21SBrian Somers       free(iov[niov].iov_base);
141096c9bb21SBrian Somers     return;
141196c9bb21SBrian Somers   }
141296c9bb21SBrian Somers 
14132cb305afSBrian Somers   if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
14142cb305afSBrian Somers     log_Printf(LogERROR, "Recvmsg: no descriptors received !\n");
14152cb305afSBrian Somers     while (niov--)
14162cb305afSBrian Somers       free(iov[niov].iov_base);
14172cb305afSBrian Somers     return;
141887c3786eSBrian Somers   }
141987c3786eSBrian Somers 
14202cb305afSBrian Somers   fd = (int *)(cmsg + 1);
14212cb305afSBrian Somers   nfd = (cmsg->cmsg_len - sizeof *cmsg) / sizeof(int);
14222cb305afSBrian Somers 
14232cb305afSBrian Somers   if (nfd < 2) {
14242cb305afSBrian Somers     log_Printf(LogERROR, "Recvmsg: %d descriptor%s received (too few) !\n",
14252cb305afSBrian Somers                nfd, nfd == 1 ? "" : "s");
14262cb305afSBrian Somers     while (nfd--)
14272cb305afSBrian Somers       close(fd[nfd]);
1428ad5b0e8bSBrian Somers     while (niov--)
1429ad5b0e8bSBrian Somers       free(iov[niov].iov_base);
1430ad5b0e8bSBrian Somers     return;
143154cd8e13SBrian Somers   }
143254cd8e13SBrian Somers 
143387c3786eSBrian Somers   /*
143487c3786eSBrian Somers    * We've successfully received one or more open file descriptors
143587c3786eSBrian Somers    * through our socket
143687c3786eSBrian Somers    */
1437ad5b0e8bSBrian Somers   log_Printf(LogDEBUG, "Receiving device descriptor\n");
143887c3786eSBrian Somers 
143996c9bb21SBrian Somers   if (strncmp(Version, iov[0].iov_base, iov[0].iov_len)) {
144096c9bb21SBrian Somers     log_Printf(LogWARN, "Cannot receive datalink, incorrect version"
144196c9bb21SBrian Somers                " (\"%.*s\", not \"%s\")\n", (int)iov[0].iov_len,
14427d81ddf5SBrian Somers                (char *)iov[0].iov_base, Version);
14432cb305afSBrian Somers     while (nfd--)
14442cb305afSBrian Somers       close(fd[nfd]);
144596c9bb21SBrian Somers     while (niov--)
144696c9bb21SBrian Somers       free(iov[niov].iov_base);
144796c9bb21SBrian Somers     return;
144896c9bb21SBrian Somers   }
144996c9bb21SBrian Somers 
14502cb305afSBrian Somers   onfd = nfd;	/* We've got this many in our array */
14512cb305afSBrian Somers   nfd -= 2;	/* Don't include p->fd and our reply descriptor */
14522cb305afSBrian Somers   niov = 1;	/* Skip the version id */
145387c3786eSBrian Somers   dl = iov2datalink(bundle, iov, &niov, sizeof iov / sizeof *iov, fd[0],
14542cb305afSBrian Somers                     fd + 2, &nfd);
1455b7c5748eSBrian Somers   if (dl) {
14562cb305afSBrian Somers     pid = getpid();
14572cb305afSBrian Somers     write(fd[1], &pid, sizeof pid);		/* Please hand me any locks */
14582cb305afSBrian Somers     read(fd[1], &ack, 1);			/* Thanks (ACK) ! */
14592cb305afSBrian Somers     close(fd[1]);
14602cb305afSBrian Somers 
146187c3786eSBrian Somers     if (nfd) {
146287c3786eSBrian Somers       log_Printf(LogERROR, "bundle_ReceiveDatalink: Failed to handle %d "
14632cb305afSBrian Somers                  "auxiliary file descriptors (%d remain)\n", onfd, nfd);
146487c3786eSBrian Somers       datalink_Destroy(dl);
146587c3786eSBrian Somers       while (nfd--)
146687c3786eSBrian Somers         close(fd[onfd--]);
14672cb305afSBrian Somers       close(fd[0]);
146887c3786eSBrian Somers     } else {
146904eaa58cSBrian Somers       bundle_DatalinkLinkin(bundle, dl);
1470b7c5748eSBrian Somers       datalink_AuthOk(dl);
1471ab2de065SBrian Somers       bundle_CalculateBandwidth(dl->bundle);
147287c3786eSBrian Somers     }
147387c3786eSBrian Somers   } else {
147487c3786eSBrian Somers     while (nfd--)
147587c3786eSBrian Somers       close(fd[onfd--]);
147687c3786eSBrian Somers     close(fd[0]);
14772cb305afSBrian Somers     close(fd[1]);
147887c3786eSBrian Somers   }
147996c9bb21SBrian Somers 
148096c9bb21SBrian Somers   free(iov[0].iov_base);
14811fa665f5SBrian Somers }
14821fa665f5SBrian Somers 
14831fa665f5SBrian Somers void
148496c9bb21SBrian Somers bundle_SendDatalink(struct datalink *dl, int s, struct sockaddr_un *sun)
14851fa665f5SBrian Somers {
14862cb305afSBrian Somers   char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int) * SEND_MAXFD];
14872cb305afSBrian Somers   const char *constlock;
14882cb305afSBrian Somers   char *lock;
148987c3786eSBrian Somers   struct cmsghdr *cmsg;
149096c9bb21SBrian Somers   struct msghdr msg;
149196c9bb21SBrian Somers   struct iovec iov[SCATTER_SEGMENTS];
14922cb305afSBrian Somers   int niov, f, expect, newsid, fd[SEND_MAXFD], nfd, reply[2], got;
149385fd273aSBrian Somers   pid_t newpid;
14946f384573SBrian Somers 
1495dd7e2610SBrian Somers   log_Printf(LogPHASE, "Transmitting datalink %s\n", dl->name);
14966f384573SBrian Somers 
14972cb305afSBrian Somers   /* Record the base device name for a lock transfer later */
14982cb305afSBrian Somers   constlock = physical_LockedDevice(dl->physical);
14992cb305afSBrian Somers   if (constlock) {
15002cb305afSBrian Somers     lock = alloca(strlen(constlock) + 1);
15012cb305afSBrian Somers     strcpy(lock, constlock);
15022cb305afSBrian Somers   } else
15032cb305afSBrian Somers     lock = NULL;
15042cb305afSBrian Somers 
150504eaa58cSBrian Somers   bundle_LinkClosed(dl->bundle, dl);
15060f2f3eb3SBrian Somers   bundle_DatalinkLinkout(dl->bundle, dl);
1507ea722969SBrian Somers 
150896c9bb21SBrian Somers   /* Build our scatter/gather array */
150996c9bb21SBrian Somers   iov[0].iov_len = strlen(Version) + 1;
151096c9bb21SBrian Somers   iov[0].iov_base = strdup(Version);
151196c9bb21SBrian Somers   niov = 1;
151287c3786eSBrian Somers   nfd = 0;
15136f384573SBrian Somers 
15142cb305afSBrian Somers   fd[0] = datalink2iov(dl, iov, &niov, SCATTER_SEGMENTS, fd + 2, &nfd);
15156f384573SBrian Somers 
15162cb305afSBrian Somers   if (fd[0] != -1 && socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, reply) != -1) {
15172cb305afSBrian Somers     /*
15182cb305afSBrian Somers      * fd[1] is used to get the peer process id back, then to confirm that
15192cb305afSBrian Somers      * we've transferred any device locks to that process id.
15202cb305afSBrian Somers      */
15212cb305afSBrian Somers     fd[1] = reply[1];
152287c3786eSBrian Somers 
15232cb305afSBrian Somers     nfd += 2;			/* Include fd[0] and fd[1] */
152496c9bb21SBrian Somers     memset(&msg, '\0', sizeof msg);
152554cd8e13SBrian Somers 
15262cb305afSBrian Somers     msg.msg_name = NULL;
15272cb305afSBrian Somers     msg.msg_namelen = 0;
152896c9bb21SBrian Somers     msg.msg_iov = iov;
152996c9bb21SBrian Somers     msg.msg_iovlen = niov;
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 
154296c9bb21SBrian Somers     for (f = expect = 0; f < niov; f++)
154396c9bb21SBrian Somers       expect += iov[f].iov_len;
154447723d29SBrian Somers 
154587c3786eSBrian Somers     log_Printf(LogDEBUG, "Sending %d descriptor%s and %d bytes in scatter"
154687c3786eSBrian Somers                "/gather array\n", nfd, nfd == 1 ? "" : "s", expect);
154747723d29SBrian Somers 
15482cb305afSBrian Somers     if ((got = sendmsg(s, &msg, 0)) == -1)
15492cb305afSBrian Somers       log_Printf(LogERROR, "Failed sendmsg: %s: %s\n",
15502cb305afSBrian Somers                  sun->sun_path, strerror(errno));
15512cb305afSBrian Somers     else if (got != expect)
15522cb305afSBrian Somers       log_Printf(LogERROR, "Failed sendmsg: %s: Only sent %d of %d\n",
15532cb305afSBrian Somers                  sun->sun_path, got, expect);
15542cb305afSBrian Somers     else {
1555c0d9a877SBrian Somers       /* We must get the ACK before closing the descriptor ! */
15562cb305afSBrian Somers       int res;
15572cb305afSBrian Somers 
15582cb305afSBrian Somers       read(reply[0], &newpid, sizeof newpid);
15592cb305afSBrian Somers       log_Printf(LogDEBUG, "Received confirmation from pid %d\n", (int)newpid);
15602cb305afSBrian Somers       if (lock && (res = ID0uu_lock_txfr(lock, newpid)) != UU_LOCK_OK)
15612cb305afSBrian Somers           log_Printf(LogPHASE, "uu_lock_txfr: %s\n", uu_lockerr(res));
15622cb305afSBrian Somers 
15632cb305afSBrian Somers       write(reply[1], "!", 1);			/* Thanks (ACK) ! */
15642cb305afSBrian Somers     }
15652cb305afSBrian Somers 
15662cb305afSBrian Somers     close(reply[0]);
15672cb305afSBrian Somers     close(reply[1]);
156854cd8e13SBrian Somers 
1569ac685e31SBrian Somers     newsid = Enabled(dl->bundle, OPT_KEEPSESSION) ||
157087c3786eSBrian Somers              tcgetpgrp(fd[0]) == getpgrp();
157187c3786eSBrian Somers     while (nfd)
157287c3786eSBrian Somers       close(fd[--nfd]);
15731384bd27SBrian Somers     if (newsid)
15742cb305afSBrian Somers       bundle_setsid(dl->bundle, got != -1);
157547723d29SBrian Somers   }
157685fd273aSBrian Somers   close(s);
157796c9bb21SBrian Somers 
157896c9bb21SBrian Somers   while (niov--)
157996c9bb21SBrian Somers     free(iov[niov].iov_base);
15801fa665f5SBrian Somers }
1581dd0645c5SBrian Somers 
1582dd0645c5SBrian Somers int
158358d55334SBrian Somers bundle_RenameDatalink(struct bundle *bundle, struct datalink *ndl,
158458d55334SBrian Somers                       const char *name)
158558d55334SBrian Somers {
158658d55334SBrian Somers   struct datalink *dl;
158758d55334SBrian Somers 
158858d55334SBrian Somers   if (!strcasecmp(ndl->name, name))
158958d55334SBrian Somers     return 1;
159058d55334SBrian Somers 
159158d55334SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
159258d55334SBrian Somers     if (!strcasecmp(dl->name, name))
159358d55334SBrian Somers       return 0;
159458d55334SBrian Somers 
159558d55334SBrian Somers   datalink_Rename(ndl, name);
159658d55334SBrian Somers   return 1;
159758d55334SBrian Somers }
159858d55334SBrian Somers 
159958d55334SBrian Somers int
1600dd0645c5SBrian Somers bundle_SetMode(struct bundle *bundle, struct datalink *dl, int mode)
1601dd0645c5SBrian Somers {
1602dd0645c5SBrian Somers   int omode;
1603dd0645c5SBrian Somers 
1604dd0645c5SBrian Somers   omode = dl->physical->type;
1605dd0645c5SBrian Somers   if (omode == mode)
1606dd0645c5SBrian Somers     return 1;
1607dd0645c5SBrian Somers 
1608ff0f9439SBrian Somers   if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO))
1609ff0f9439SBrian Somers     /* First auto link */
1610dd0645c5SBrian Somers     if (bundle->ncp.ipcp.peer_ip.s_addr == INADDR_ANY) {
1611ff0f9439SBrian Somers       log_Printf(LogWARN, "You must `set ifaddr' or `open' before"
1612ff0f9439SBrian Somers                  " changing mode to %s\n", mode2Nam(mode));
1613dd0645c5SBrian Somers       return 0;
1614dd0645c5SBrian Somers     }
1615dd0645c5SBrian Somers 
1616dd0645c5SBrian Somers   if (!datalink_SetMode(dl, mode))
1617dd0645c5SBrian Somers     return 0;
1618dd0645c5SBrian Somers 
1619ff0f9439SBrian Somers   if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO) &&
1620ff0f9439SBrian Somers       bundle->phase != PHASE_NETWORK)
1621ff0f9439SBrian Somers     /* First auto link, we need an interface */
1622dd0645c5SBrian Somers     ipcp_InterfaceUp(&bundle->ncp.ipcp);
1623dd0645c5SBrian Somers 
1624ab2de065SBrian Somers   /* Regenerate phys_type and adjust idle timer */
162504eaa58cSBrian Somers   bundle_LinksRemoved(bundle);
1626dd0645c5SBrian Somers 
1627dd0645c5SBrian Somers   return 1;
1628dd0645c5SBrian Somers }
16291384bd27SBrian Somers 
16301384bd27SBrian Somers void
16311384bd27SBrian Somers bundle_setsid(struct bundle *bundle, int holdsession)
16321384bd27SBrian Somers {
16331384bd27SBrian Somers   /*
16341384bd27SBrian Somers    * Lose the current session.  This means getting rid of our pid
16351384bd27SBrian Somers    * too so that the tty device will really go away, and any getty
16361384bd27SBrian Somers    * etc will be allowed to restart.
16371384bd27SBrian Somers    */
16381384bd27SBrian Somers   pid_t pid, orig;
16391384bd27SBrian Somers   int fds[2];
16401384bd27SBrian Somers   char done;
16411384bd27SBrian Somers   struct datalink *dl;
16421384bd27SBrian Somers 
16431384bd27SBrian Somers   orig = getpid();
16441384bd27SBrian Somers   if (pipe(fds) == -1) {
16451384bd27SBrian Somers     log_Printf(LogERROR, "pipe: %s\n", strerror(errno));
16461384bd27SBrian Somers     return;
16471384bd27SBrian Somers   }
16481384bd27SBrian Somers   switch ((pid = fork())) {
16491384bd27SBrian Somers     case -1:
16501384bd27SBrian Somers       log_Printf(LogERROR, "fork: %s\n", strerror(errno));
16511384bd27SBrian Somers       close(fds[0]);
16521384bd27SBrian Somers       close(fds[1]);
16531384bd27SBrian Somers       return;
16541384bd27SBrian Somers     case 0:
16551384bd27SBrian Somers       close(fds[1]);
16564be0e57dSBrian Somers       read(fds[0], &done, 1);		/* uu_locks are mine ! */
16574be0e57dSBrian Somers       close(fds[0]);
16581384bd27SBrian Somers       if (pipe(fds) == -1) {
16591384bd27SBrian Somers         log_Printf(LogERROR, "pipe(2): %s\n", strerror(errno));
16601384bd27SBrian Somers         return;
16611384bd27SBrian Somers       }
16621384bd27SBrian Somers       switch ((pid = fork())) {
16631384bd27SBrian Somers         case -1:
1664a33b2ef7SBrian Somers           log_Printf(LogERROR, "fork(2): %s\n", strerror(errno));
16651384bd27SBrian Somers           close(fds[0]);
16661384bd27SBrian Somers           close(fds[1]);
16671384bd27SBrian Somers           return;
16681384bd27SBrian Somers         case 0:
16691384bd27SBrian Somers           close(fds[1]);
16704be0e57dSBrian Somers           bundle_LockTun(bundle);	/* update pid */
16714be0e57dSBrian Somers           read(fds[0], &done, 1);	/* uu_locks are mine ! */
16724be0e57dSBrian Somers           close(fds[0]);
16731384bd27SBrian Somers           setsid();
16741384bd27SBrian Somers           log_Printf(LogPHASE, "%d -> %d: %s session control\n",
16751384bd27SBrian Somers                      (int)orig, (int)getpid(),
16761384bd27SBrian Somers                      holdsession ? "Passed" : "Dropped");
16777e778f13SBrian Somers           timer_InitService(0);		/* Start the Timer Service */
16781384bd27SBrian Somers           break;
16791384bd27SBrian Somers         default:
16804be0e57dSBrian Somers           close(fds[0]);
16815d9e6103SBrian Somers           /* Give away all our physical locks (to the final process) */
16821384bd27SBrian Somers           for (dl = bundle->links; dl; dl = dl->next)
16831384bd27SBrian Somers             if (dl->state != DATALINK_CLOSED)
16845d9e6103SBrian Somers               physical_ChangedPid(dl->physical, pid);
16854be0e57dSBrian Somers           write(fds[1], "!", 1);	/* done */
16864be0e57dSBrian Somers           close(fds[1]);
16872cb305afSBrian Somers           _exit(0);
16881384bd27SBrian Somers           break;
16891384bd27SBrian Somers       }
16901384bd27SBrian Somers       break;
16911384bd27SBrian Somers     default:
16924be0e57dSBrian Somers       close(fds[0]);
16935d9e6103SBrian Somers       /* Give away all our physical locks (to the intermediate process) */
16941384bd27SBrian Somers       for (dl = bundle->links; dl; dl = dl->next)
16951384bd27SBrian Somers         if (dl->state != DATALINK_CLOSED)
16965d9e6103SBrian Somers           physical_ChangedPid(dl->physical, pid);
16974be0e57dSBrian Somers       write(fds[1], "!", 1);	/* done */
16984be0e57dSBrian Somers       close(fds[1]);
16991384bd27SBrian Somers       if (holdsession) {
17001384bd27SBrian Somers         int fd, status;
17011384bd27SBrian Somers 
17021384bd27SBrian Somers         timer_TermService();
17031384bd27SBrian Somers         signal(SIGPIPE, SIG_DFL);
17041384bd27SBrian Somers         signal(SIGALRM, SIG_DFL);
17051384bd27SBrian Somers         signal(SIGHUP, SIG_DFL);
17061384bd27SBrian Somers         signal(SIGTERM, SIG_DFL);
17071384bd27SBrian Somers         signal(SIGINT, SIG_DFL);
17081384bd27SBrian Somers         signal(SIGQUIT, SIG_DFL);
17091384bd27SBrian Somers         for (fd = getdtablesize(); fd >= 0; fd--)
17101384bd27SBrian Somers           close(fd);
17111384bd27SBrian Somers         /*
17121384bd27SBrian Somers          * Reap the intermediate process.  As we're not exiting but the
17131384bd27SBrian Somers          * intermediate is, we don't want it to become defunct.
17141384bd27SBrian Somers          */
17151384bd27SBrian Somers         waitpid(pid, &status, 0);
17168e7b8599SBrian Somers         /* Tweak our process arguments.... */
1717cf0a3940SBrian Somers         ID0setproctitle("session owner");
1718be8bbe88SBrian Somers         setuid(geteuid());
17191384bd27SBrian Somers         /*
17201384bd27SBrian Somers          * Hang around for a HUP.  This should happen as soon as the
17211384bd27SBrian Somers          * ppp that we passed our ctty descriptor to closes it.
17221384bd27SBrian Somers          * NOTE: If this process dies, the passed descriptor becomes
17231384bd27SBrian Somers          *       invalid and will give a select() error by setting one
17241384bd27SBrian Somers          *       of the error fds, aborting the other ppp.  We don't
17251384bd27SBrian Somers          *       want that to happen !
17261384bd27SBrian Somers          */
17271384bd27SBrian Somers         pause();
17281384bd27SBrian Somers       }
17292cb305afSBrian Somers       _exit(0);
17301384bd27SBrian Somers       break;
17311384bd27SBrian Somers   }
17321384bd27SBrian Somers }
17339b5f8ffdSBrian Somers 
17349b5f8ffdSBrian Somers int
17359b5f8ffdSBrian Somers bundle_HighestState(struct bundle *bundle)
17369b5f8ffdSBrian Somers {
17379b5f8ffdSBrian Somers   struct datalink *dl;
17389b5f8ffdSBrian Somers   int result = DATALINK_CLOSED;
17399b5f8ffdSBrian Somers 
17409b5f8ffdSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
17419b5f8ffdSBrian Somers     if (result < dl->state)
17429b5f8ffdSBrian Somers       result = dl->state;
17439b5f8ffdSBrian Somers 
17449b5f8ffdSBrian Somers   return result;
17459b5f8ffdSBrian Somers }
1746991c2a7bSBrian Somers 
1747991c2a7bSBrian Somers int
1748991c2a7bSBrian Somers bundle_Exception(struct bundle *bundle, int fd)
1749991c2a7bSBrian Somers {
1750991c2a7bSBrian Somers   struct datalink *dl;
1751991c2a7bSBrian Somers 
1752991c2a7bSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
1753991c2a7bSBrian Somers     if (dl->physical->fd == fd) {
1754991c2a7bSBrian Somers       datalink_Down(dl, CLOSE_NORMAL);
1755991c2a7bSBrian Somers       return 1;
1756991c2a7bSBrian Somers     }
1757991c2a7bSBrian Somers 
1758991c2a7bSBrian Somers   return 0;
1759991c2a7bSBrian Somers }
17601d1fc017SBrian Somers 
17611d1fc017SBrian Somers void
17621d1fc017SBrian Somers bundle_AdjustFilters(struct bundle *bundle, struct in_addr *my_ip,
17631d1fc017SBrian Somers                      struct in_addr *peer_ip)
17641d1fc017SBrian Somers {
17651d1fc017SBrian Somers   filter_AdjustAddr(&bundle->filter.in, my_ip, peer_ip);
17661d1fc017SBrian Somers   filter_AdjustAddr(&bundle->filter.out, my_ip, peer_ip);
17671d1fc017SBrian Somers   filter_AdjustAddr(&bundle->filter.dial, my_ip, peer_ip);
17681d1fc017SBrian Somers   filter_AdjustAddr(&bundle->filter.alive, my_ip, peer_ip);
17691d1fc017SBrian Somers }
1770ab2de065SBrian Somers 
1771ab2de065SBrian Somers void
1772ab2de065SBrian Somers bundle_CalculateBandwidth(struct bundle *bundle)
1773ab2de065SBrian Somers {
1774ab2de065SBrian Somers   struct datalink *dl;
1775ab2de065SBrian Somers   int mtu, sp;
1776ab2de065SBrian Somers 
1777ab2de065SBrian Somers   bundle->bandwidth = 0;
1778ab2de065SBrian Somers   mtu = 0;
1779ab2de065SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
1780ab2de065SBrian Somers     if (dl->state == DATALINK_OPEN) {
1781ab2de065SBrian Somers       if ((sp = dl->mp.bandwidth) == 0 &&
1782ab2de065SBrian Somers           (sp = physical_GetSpeed(dl->physical)) == 0)
1783ab2de065SBrian Somers         log_Printf(LogDEBUG, "%s: %s: Cannot determine bandwidth\n",
1784ab2de065SBrian Somers                    dl->name, dl->physical->name.full);
1785ab2de065SBrian Somers       else
1786ab2de065SBrian Somers         bundle->bandwidth += sp;
1787ab2de065SBrian Somers       if (!bundle->ncp.mp.active) {
1788ab2de065SBrian Somers         mtu = dl->physical->link.lcp.his_mru;
1789ab2de065SBrian Somers         break;
1790ab2de065SBrian Somers       }
1791ab2de065SBrian Somers     }
1792ab2de065SBrian Somers 
1793ab2de065SBrian Somers   if(bundle->bandwidth == 0)
1794ab2de065SBrian Somers     bundle->bandwidth = 115200;		/* Shrug */
1795ab2de065SBrian Somers 
1796ab2de065SBrian Somers   if (bundle->ncp.mp.active)
1797ab2de065SBrian Somers     mtu = bundle->ncp.mp.peer_mrru;
1798ab2de065SBrian Somers   else if (!mtu)
1799ab2de065SBrian Somers     mtu = 1500;
1800ab2de065SBrian Somers 
1801ab2de065SBrian Somers #ifndef NORADIUS
1802ab2de065SBrian Somers   if (bundle->radius.valid && bundle->radius.mtu && bundle->radius.mtu < mtu) {
1803ab2de065SBrian Somers     log_Printf(LogLCP, "Reducing MTU to radius value %lu\n",
1804ab2de065SBrian Somers                bundle->radius.mtu);
1805ab2de065SBrian Somers     mtu = bundle->radius.mtu;
1806ab2de065SBrian Somers   }
1807ab2de065SBrian Somers #endif
1808ab2de065SBrian Somers 
1809ab2de065SBrian Somers   tun_configure(bundle, mtu);
1810ab2de065SBrian Somers }
1811ab2de065SBrian Somers 
1812ab2de065SBrian Somers void
1813ab2de065SBrian Somers bundle_AutoAdjust(struct bundle *bundle, int percent, int what)
1814ab2de065SBrian Somers {
1815ab2de065SBrian Somers   struct datalink *dl, *choice, *otherlinkup;
1816ab2de065SBrian Somers 
1817ab2de065SBrian Somers   choice = otherlinkup = NULL;
1818ab2de065SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
1819ab2de065SBrian Somers     if (dl->physical->type == PHYS_AUTO) {
1820ab2de065SBrian Somers       if (dl->state == DATALINK_OPEN) {
1821ab2de065SBrian Somers         if (what == AUTO_DOWN) {
1822ab2de065SBrian Somers           if (choice)
1823ab2de065SBrian Somers             otherlinkup = choice;
1824ab2de065SBrian Somers           choice = dl;
1825ab2de065SBrian Somers         }
1826ab2de065SBrian Somers       } else if (dl->state == DATALINK_CLOSED) {
1827ab2de065SBrian Somers         if (what == AUTO_UP) {
1828ab2de065SBrian Somers           choice = dl;
1829ab2de065SBrian Somers           break;
1830ab2de065SBrian Somers         }
1831ab2de065SBrian Somers       } else {
1832ab2de065SBrian Somers         /* An auto link in an intermediate state - forget it for the moment */
1833ab2de065SBrian Somers         choice = NULL;
1834ab2de065SBrian Somers         break;
1835ab2de065SBrian Somers       }
1836ab2de065SBrian Somers     } else if (dl->state == DATALINK_OPEN && what == AUTO_DOWN)
1837ab2de065SBrian Somers       otherlinkup = dl;
1838ab2de065SBrian Somers 
1839ab2de065SBrian Somers   if (choice) {
1840ab2de065SBrian Somers     if (what == AUTO_UP) {
1841ab2de065SBrian Somers       log_Printf(LogPHASE, "%d%% saturation -> Opening link ``%s''\n",
1842ab2de065SBrian Somers                  percent, choice->name);
1843ab2de065SBrian Somers       datalink_Up(choice, 1, 1);
1844ab2de065SBrian Somers       mp_StopAutoloadTimer(&bundle->ncp.mp);
1845ab2de065SBrian Somers     } else if (otherlinkup) {	/* Only bring the second-last link down */
1846ab2de065SBrian Somers       log_Printf(LogPHASE, "%d%% saturation -> Closing link ``%s''\n",
1847ab2de065SBrian Somers                  percent, choice->name);
1848d2f5232dSBrian Somers       datalink_Close(choice, CLOSE_STAYDOWN);
1849ab2de065SBrian Somers       mp_StopAutoloadTimer(&bundle->ncp.mp);
1850ab2de065SBrian Somers     }
1851ab2de065SBrian Somers   }
1852ab2de065SBrian Somers }
1853ab2de065SBrian Somers 
1854ab2de065SBrian Somers int
1855ab2de065SBrian Somers bundle_WantAutoloadTimer(struct bundle *bundle)
1856ab2de065SBrian Somers {
1857ab2de065SBrian Somers   struct datalink *dl;
1858ab2de065SBrian Somers   int autolink, opened;
1859ab2de065SBrian Somers 
1860ab2de065SBrian Somers   if (bundle->phase == PHASE_NETWORK) {
1861ab2de065SBrian Somers     for (autolink = opened = 0, dl = bundle->links; dl; dl = dl->next)
1862ab2de065SBrian Somers       if (dl->physical->type == PHYS_AUTO) {
1863ab2de065SBrian Somers         if (++autolink == 2 || (autolink == 1 && opened))
1864ab2de065SBrian Somers           /* Two auto links or one auto and one open in NETWORK phase */
1865ab2de065SBrian Somers           return 1;
1866ab2de065SBrian Somers       } else if (dl->state == DATALINK_OPEN) {
1867ab2de065SBrian Somers         opened++;
1868ab2de065SBrian Somers         if (autolink)
1869ab2de065SBrian Somers           /* One auto and one open link in NETWORK phase */
1870ab2de065SBrian Somers           return 1;
1871ab2de065SBrian Somers       }
1872ab2de065SBrian Somers   }
1873ab2de065SBrian Somers 
1874ab2de065SBrian Somers   return 0;
1875ab2de065SBrian Somers }
1876