xref: /freebsd/usr.sbin/ppp/bundle.c (revision e531f89a62caa5d380bd4efb2e6a0f9ad48377fb)
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  *
26e531f89aSBrian Somers  *	$Id: bundle.c,v 1.60 1999/08/06 20:04:01 brian Exp $
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>
4247723d29SBrian Somers #include <paths.h>
437a6f8720SBrian Somers #include <stdio.h>
446f384573SBrian Somers #include <stdlib.h>
457a6f8720SBrian Somers #include <string.h>
4696c9bb21SBrian Somers #include <sys/uio.h>
4754cd8e13SBrian Somers #include <sys/wait.h>
487a6f8720SBrian Somers #include <termios.h>
497a6f8720SBrian Somers #include <unistd.h>
507a6f8720SBrian Somers 
515d9e6103SBrian Somers #include "layer.h"
52c9e11a11SBrian Somers #include "defs.h"
537a6f8720SBrian Somers #include "command.h"
547a6f8720SBrian Somers #include "mbuf.h"
557a6f8720SBrian Somers #include "log.h"
567a6f8720SBrian Somers #include "id.h"
577a6f8720SBrian Somers #include "timer.h"
587a6f8720SBrian Somers #include "fsm.h"
597a6f8720SBrian Somers #include "iplist.h"
60879ed6faSBrian Somers #include "lqr.h"
61455aabc3SBrian Somers #include "hdlc.h"
627a6f8720SBrian Somers #include "throughput.h"
63eaa4df37SBrian Somers #include "slcompress.h"
647a6f8720SBrian Somers #include "ipcp.h"
655ca5389aSBrian Somers #include "filter.h"
662f786681SBrian Somers #include "descriptor.h"
677a6f8720SBrian Somers #include "route.h"
687a6f8720SBrian Somers #include "lcp.h"
697a6f8720SBrian Somers #include "ccp.h"
703b0f8d2eSBrian Somers #include "link.h"
713b0f8d2eSBrian Somers #include "mp.h"
72972a1bcfSBrian Somers #ifndef NORADIUS
73972a1bcfSBrian Somers #include "radius.h"
74972a1bcfSBrian Somers #endif
753b0f8d2eSBrian Somers #include "bundle.h"
76455aabc3SBrian Somers #include "async.h"
77455aabc3SBrian Somers #include "physical.h"
78455aabc3SBrian Somers #include "auth.h"
795d9e6103SBrian Somers #include "proto.h"
80455aabc3SBrian Somers #include "chap.h"
81455aabc3SBrian Somers #include "tun.h"
8285b542cfSBrian Somers #include "prompt.h"
833006ec67SBrian Somers #include "chat.h"
8492b09558SBrian Somers #include "cbcp.h"
853006ec67SBrian Somers #include "datalink.h"
863006ec67SBrian Somers #include "ip.h"
878fa6ebe4SBrian Somers #include "iface.h"
887a6f8720SBrian Somers 
89ab2de065SBrian Somers #define SCATTER_SEGMENTS 6	/* version, datalink, name, physical,
90ab2de065SBrian Somers                                    throughput, device */
9196c9bb21SBrian Somers #define SOCKET_OVERHEAD	100	/* additional buffer space for large */
9296c9bb21SBrian Somers                                 /* {recv,send}msg() calls            */
9396c9bb21SBrian Somers 
9404eaa58cSBrian Somers static int bundle_RemainingIdleTime(struct bundle *);
9504eaa58cSBrian Somers 
96455aabc3SBrian Somers static const char *PhaseNames[] = {
97455aabc3SBrian Somers   "Dead", "Establish", "Authenticate", "Network", "Terminate"
98455aabc3SBrian Somers };
99455aabc3SBrian Somers 
100455aabc3SBrian Somers const char *
101455aabc3SBrian Somers bundle_PhaseName(struct bundle *bundle)
1027a6f8720SBrian Somers {
103455aabc3SBrian Somers   return bundle->phase <= PHASE_TERMINATE ?
104455aabc3SBrian Somers     PhaseNames[bundle->phase] : "unknown";
1057a6f8720SBrian Somers }
1067a6f8720SBrian Somers 
107455aabc3SBrian Somers void
1085563ebdeSBrian Somers bundle_NewPhase(struct bundle *bundle, u_int new)
109455aabc3SBrian Somers {
110aef795ccSBrian Somers   if (new == bundle->phase)
111aef795ccSBrian Somers     return;
112aef795ccSBrian Somers 
113e2ebb036SBrian Somers   if (new <= PHASE_TERMINATE)
114dd7e2610SBrian Somers     log_Printf(LogPHASE, "bundle: %s\n", PhaseNames[new]);
1157a6f8720SBrian Somers 
116455aabc3SBrian Somers   switch (new) {
117455aabc3SBrian Somers   case PHASE_DEAD:
1180f2f3eb3SBrian Somers     log_DisplayPrompts();
119455aabc3SBrian Somers     bundle->phase = new;
120455aabc3SBrian Somers     break;
121455aabc3SBrian Somers 
122455aabc3SBrian Somers   case PHASE_ESTABLISH:
123455aabc3SBrian Somers     bundle->phase = new;
124455aabc3SBrian Somers     break;
125455aabc3SBrian Somers 
126455aabc3SBrian Somers   case PHASE_AUTHENTICATE:
127455aabc3SBrian Somers     bundle->phase = new;
1280f2f3eb3SBrian Somers     log_DisplayPrompts();
129455aabc3SBrian Somers     break;
130455aabc3SBrian Somers 
131455aabc3SBrian Somers   case PHASE_NETWORK:
132dd7e2610SBrian Somers     fsm_Up(&bundle->ncp.ipcp.fsm);
133dd7e2610SBrian Somers     fsm_Open(&bundle->ncp.ipcp.fsm);
134673903ecSBrian Somers     bundle->phase = new;
1350f2f3eb3SBrian Somers     log_DisplayPrompts();
136673903ecSBrian Somers     break;
137455aabc3SBrian Somers 
138455aabc3SBrian Somers   case PHASE_TERMINATE:
139455aabc3SBrian Somers     bundle->phase = new;
140673903ecSBrian Somers     mp_Down(&bundle->ncp.mp);
1410f2f3eb3SBrian Somers     log_DisplayPrompts();
142455aabc3SBrian Somers     break;
1437a6f8720SBrian Somers   }
1447a6f8720SBrian Somers }
1457a6f8720SBrian Somers 
1466d666775SBrian Somers static void
1476d666775SBrian Somers bundle_LayerStart(void *v, struct fsm *fp)
1487a6f8720SBrian Somers {
1493006ec67SBrian Somers   /* The given FSM is about to start up ! */
1507a6f8720SBrian Somers }
1517a6f8720SBrian Somers 
1525cf4388bSBrian Somers 
1535cf4388bSBrian Somers static void
1545cf4388bSBrian Somers bundle_Notify(struct bundle *bundle, char c)
1555cf4388bSBrian Somers {
1565cf4388bSBrian Somers   if (bundle->notify.fd != -1) {
1575cf4388bSBrian Somers     if (write(bundle->notify.fd, &c, 1) == 1)
158dd7e2610SBrian Somers       log_Printf(LogPHASE, "Parent notified of success.\n");
1595cf4388bSBrian Somers     else
160dd7e2610SBrian Somers       log_Printf(LogPHASE, "Failed to notify parent of success.\n");
1615cf4388bSBrian Somers     close(bundle->notify.fd);
1625cf4388bSBrian Somers     bundle->notify.fd = -1;
1635cf4388bSBrian Somers   }
1645cf4388bSBrian Somers }
1653b0f8d2eSBrian Somers 
1666d666775SBrian Somers static void
1676f8e9f0aSBrian Somers bundle_ClearQueues(void *v)
1686f8e9f0aSBrian Somers {
1696f8e9f0aSBrian Somers   struct bundle *bundle = (struct bundle *)v;
1706f8e9f0aSBrian Somers   struct datalink *dl;
1716f8e9f0aSBrian Somers 
1726f8e9f0aSBrian Somers   log_Printf(LogPHASE, "Clearing choked output queue\n");
1736f8e9f0aSBrian Somers   timer_Stop(&bundle->choked.timer);
1746f8e9f0aSBrian Somers 
1756f8e9f0aSBrian Somers   /*
1766f8e9f0aSBrian Somers    * Emergency time:
1776f8e9f0aSBrian Somers    *
1786f8e9f0aSBrian Somers    * We've had a full queue for PACKET_DEL_SECS seconds without being
1796f8e9f0aSBrian Somers    * able to get rid of any of the packets.  We've probably given up
1806f8e9f0aSBrian Somers    * on the redials at this point, and the queued data has almost
1816f8e9f0aSBrian Somers    * definitely been timed out by the layer above.  As this is preventing
1826f8e9f0aSBrian Somers    * us from reading the TUN_NAME device (we don't want to buffer stuff
1836f8e9f0aSBrian Somers    * indefinitely), we may as well nuke this data and start with a clean
1846f8e9f0aSBrian Somers    * slate !
1856f8e9f0aSBrian Somers    *
1866f8e9f0aSBrian Somers    * Unfortunately, this has the side effect of shafting any compression
1876f8e9f0aSBrian Somers    * dictionaries in use (causing the relevant RESET_REQ/RESET_ACK).
1886f8e9f0aSBrian Somers    */
1896f8e9f0aSBrian Somers 
1905a72b6edSBrian Somers   ip_DeleteQueue(&bundle->ncp.ipcp);
1916f8e9f0aSBrian Somers   mp_DeleteQueue(&bundle->ncp.mp);
1926f8e9f0aSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
1936f8e9f0aSBrian Somers     physical_DeleteQueue(dl->physical);
1946f8e9f0aSBrian Somers }
1956f8e9f0aSBrian Somers 
1966f8e9f0aSBrian Somers static void
197ff0f9439SBrian Somers bundle_LinkAdded(struct bundle *bundle, struct datalink *dl)
198ff0f9439SBrian Somers {
199ff0f9439SBrian Somers   bundle->phys_type.all |= dl->physical->type;
200ff0f9439SBrian Somers   if (dl->state == DATALINK_OPEN)
201ff0f9439SBrian Somers     bundle->phys_type.open |= dl->physical->type;
202ff0f9439SBrian Somers 
203ff0f9439SBrian Somers   if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL))
204ff0f9439SBrian Somers       != bundle->phys_type.open && bundle->idle.timer.state == TIMER_STOPPED)
205ff0f9439SBrian Somers     /* We may need to start our idle timer */
206ff0f9439SBrian Somers     bundle_StartIdleTimer(bundle);
207ff0f9439SBrian Somers }
208ff0f9439SBrian Somers 
20992b09558SBrian Somers void
210ff0f9439SBrian Somers bundle_LinksRemoved(struct bundle *bundle)
211ff0f9439SBrian Somers {
212ff0f9439SBrian Somers   struct datalink *dl;
213ff0f9439SBrian Somers 
214ff0f9439SBrian Somers   bundle->phys_type.all = bundle->phys_type.open = 0;
215ff0f9439SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
216ff0f9439SBrian Somers     bundle_LinkAdded(bundle, dl);
217ff0f9439SBrian Somers 
218ab2de065SBrian Somers   bundle_CalculateBandwidth(bundle);
219ab2de065SBrian Somers   mp_CheckAutoloadTimer(&bundle->ncp.mp);
220ab2de065SBrian Somers 
221ff0f9439SBrian Somers   if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL))
222ff0f9439SBrian Somers       == bundle->phys_type.open)
223ff0f9439SBrian Somers     bundle_StopIdleTimer(bundle);
224ff0f9439SBrian Somers }
22504eaa58cSBrian Somers 
22604eaa58cSBrian Somers static void
2276f384573SBrian Somers bundle_LayerUp(void *v, struct fsm *fp)
2287a6f8720SBrian Somers {
2293006ec67SBrian Somers   /*
2303006ec67SBrian Somers    * The given fsm is now up
231ab2de065SBrian Somers    * If it's an LCP, adjust our phys_mode.open value and check the
232ab2de065SBrian Somers    * autoload timer.
233ab2de065SBrian Somers    * If it's the first NCP, calculate our bandwidth
2343b0f8d2eSBrian Somers    * If it's the first NCP, start the idle timer.
235ab2de065SBrian Somers    * If it's an NCP, tell our -background parent to go away.
236ab2de065SBrian Somers    * If it's the first NCP, start the autoload timer
2373006ec67SBrian Somers    */
2386f384573SBrian Somers   struct bundle *bundle = (struct bundle *)v;
2396d666775SBrian Somers 
2405563ebdeSBrian Somers   if (fp->proto == PROTO_LCP) {
241ff0f9439SBrian Somers     struct physical *p = link2physical(fp->link);
242ff0f9439SBrian Somers 
243ff0f9439SBrian Somers     bundle_LinkAdded(bundle, p->dl);
244ab2de065SBrian Somers     mp_CheckAutoloadTimer(&bundle->ncp.mp);
2453b0f8d2eSBrian Somers   } else if (fp->proto == PROTO_IPCP) {
246ab2de065SBrian Somers     bundle_CalculateBandwidth(fp->bundle);
247ab886ad0SBrian Somers     bundle_StartIdleTimer(bundle);
2485cf4388bSBrian Somers     bundle_Notify(bundle, EX_NORMAL);
249ab2de065SBrian Somers     mp_CheckAutoloadTimer(&fp->bundle->ncp.mp);
2507a6f8720SBrian Somers   }
251ab886ad0SBrian Somers }
2527a6f8720SBrian Somers 
2536d666775SBrian Somers static void
2546d666775SBrian Somers bundle_LayerDown(void *v, struct fsm *fp)
2556d666775SBrian Somers {
2566d666775SBrian Somers   /*
2576d666775SBrian Somers    * The given FSM has been told to come down.
258ab886ad0SBrian Somers    * If it's our last NCP, stop the idle timer.
259ab2de065SBrian Somers    * If it's our last NCP, stop the autoload timer
260ff0f9439SBrian Somers    * If it's an LCP, adjust our phys_type.open value and any timers.
26186b1f0d7SBrian Somers    * If it's an LCP and we're in multilink mode, adjust our tun
26286b1f0d7SBrian Somers    * speed and make sure our minimum sequence number is adjusted.
2636d666775SBrian Somers    */
264ab886ad0SBrian Somers 
265ab886ad0SBrian Somers   struct bundle *bundle = (struct bundle *)v;
266ab886ad0SBrian Somers 
267ab2de065SBrian Somers   if (fp->proto == PROTO_IPCP) {
268ab886ad0SBrian Somers     bundle_StopIdleTimer(bundle);
269ab2de065SBrian Somers     mp_StopAutoloadTimer(&bundle->ncp.mp);
270ab2de065SBrian Somers   } else if (fp->proto == PROTO_LCP) {
271ff0f9439SBrian Somers     bundle_LinksRemoved(bundle);  /* adjust timers & phys_type values */
272ff0f9439SBrian Somers     if (bundle->ncp.mp.active) {
2733b0f8d2eSBrian Somers       struct datalink *dl;
27486b1f0d7SBrian Somers       struct datalink *lost;
2753b0f8d2eSBrian Somers 
27686b1f0d7SBrian Somers       lost = NULL;
277faefde08SBrian Somers       for (dl = bundle->links; dl; dl = dl->next)
27886b1f0d7SBrian Somers         if (fp == &dl->physical->link.lcp.fsm)
27986b1f0d7SBrian Somers           lost = dl;
28086b1f0d7SBrian Somers 
281ab2de065SBrian Somers       bundle_CalculateBandwidth(bundle);
28286b1f0d7SBrian Somers 
28386b1f0d7SBrian Somers       if (lost)
28486b1f0d7SBrian Somers         mp_LinkLost(&bundle->ncp.mp, lost);
28586b1f0d7SBrian Somers       else
286a33b2ef7SBrian Somers         log_Printf(LogALERT, "Oops, lost an unrecognised datalink (%s) !\n",
28786b1f0d7SBrian Somers                    fp->link->name);
2883b0f8d2eSBrian Somers     }
2896d666775SBrian Somers   }
290ff0f9439SBrian Somers }
2916d666775SBrian Somers 
2926d666775SBrian Somers static void
2936d666775SBrian Somers bundle_LayerFinish(void *v, struct fsm *fp)
2946d666775SBrian Somers {
2956d666775SBrian Somers   /* The given fsm is now down (fp cannot be NULL)
2966d666775SBrian Somers    *
297dd7e2610SBrian Somers    * If it's the last LCP, fsm_Down all NCPs
298dd7e2610SBrian Somers    * If it's the last NCP, fsm_Close all LCPs
2996d666775SBrian Somers    */
3006d666775SBrian Somers 
3016d666775SBrian Somers   struct bundle *bundle = (struct bundle *)v;
3026d666775SBrian Somers   struct datalink *dl;
3036d666775SBrian Somers 
3043b0f8d2eSBrian Somers   if (fp->proto == PROTO_IPCP) {
30526afeaa2SBrian Somers     if (bundle_Phase(bundle) != PHASE_DEAD)
30625092092SBrian Somers       bundle_NewPhase(bundle, PHASE_TERMINATE);
3076d666775SBrian Somers     for (dl = bundle->links; dl; dl = dl->next)
3089c81b87dSBrian Somers       datalink_Close(dl, CLOSE_NORMAL);
30909206a6fSBrian Somers     fsm2initial(fp);
3103b0f8d2eSBrian Somers   } else if (fp->proto == PROTO_LCP) {
3113b0f8d2eSBrian Somers     int others_active;
312a611cad6SBrian Somers 
3133b0f8d2eSBrian Somers     others_active = 0;
3143b0f8d2eSBrian Somers     for (dl = bundle->links; dl; dl = dl->next)
3153b0f8d2eSBrian Somers       if (fp != &dl->physical->link.lcp.fsm &&
3163b0f8d2eSBrian Somers           dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP)
3173b0f8d2eSBrian Somers         others_active++;
3183b0f8d2eSBrian Somers 
31909206a6fSBrian Somers     if (!others_active)
32009206a6fSBrian Somers       fsm2initial(&bundle->ncp.ipcp.fsm);
3213b0f8d2eSBrian Somers   }
3223b0f8d2eSBrian Somers }
3236d666775SBrian Somers 
3247a6f8720SBrian Somers int
3257a6f8720SBrian Somers bundle_LinkIsUp(const struct bundle *bundle)
3267a6f8720SBrian Somers {
3275828db6dSBrian Somers   return bundle->ncp.ipcp.fsm.state == ST_OPENED;
3287a6f8720SBrian Somers }
3297a6f8720SBrian Somers 
3307a6f8720SBrian Somers void
3319c81b87dSBrian Somers bundle_Close(struct bundle *bundle, const char *name, int how)
3327a6f8720SBrian Somers {
333455aabc3SBrian Somers   /*
3343006ec67SBrian Somers    * Please close the given datalink.
335dd7e2610SBrian Somers    * If name == NULL or name is the last datalink, fsm_Close all NCPs
3366f384573SBrian Somers    * (except our MP)
3373b0f8d2eSBrian Somers    * If it isn't the last datalink, just Close that datalink.
338455aabc3SBrian Somers    */
3397a6f8720SBrian Somers 
3403b0f8d2eSBrian Somers   struct datalink *dl, *this_dl;
3413b0f8d2eSBrian Somers   int others_active;
3423006ec67SBrian Somers 
3433b0f8d2eSBrian Somers   others_active = 0;
3443b0f8d2eSBrian Somers   this_dl = NULL;
3453b0f8d2eSBrian Somers 
3463b0f8d2eSBrian Somers   for (dl = bundle->links; dl; dl = dl->next) {
3473b0f8d2eSBrian Somers     if (name && !strcasecmp(name, dl->name))
3483b0f8d2eSBrian Somers       this_dl = dl;
3493b0f8d2eSBrian Somers     if (name == NULL || this_dl == dl) {
3509c81b87dSBrian Somers       switch (how) {
3519c81b87dSBrian Somers         case CLOSE_LCP:
3529c81b87dSBrian Somers           datalink_DontHangup(dl);
3539c81b87dSBrian Somers           /* fall through */
3549c81b87dSBrian Somers         case CLOSE_STAYDOWN:
3553006ec67SBrian Somers           datalink_StayDown(dl);
3569c81b87dSBrian Somers           break;
3579c81b87dSBrian Somers       }
3583b0f8d2eSBrian Somers     } else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP)
3593b0f8d2eSBrian Somers       others_active++;
3603b0f8d2eSBrian Somers   }
3613b0f8d2eSBrian Somers 
3623b0f8d2eSBrian Somers   if (name && this_dl == NULL) {
363dd7e2610SBrian Somers     log_Printf(LogWARN, "%s: Invalid datalink name\n", name);
3643b0f8d2eSBrian Somers     return;
3653b0f8d2eSBrian Somers   }
3663b0f8d2eSBrian Somers 
3673b0f8d2eSBrian Somers   if (!others_active) {
36804eaa58cSBrian Somers     bundle_StopIdleTimer(bundle);
3693b0f8d2eSBrian Somers     if (bundle->ncp.ipcp.fsm.state > ST_CLOSED ||
3703b0f8d2eSBrian Somers         bundle->ncp.ipcp.fsm.state == ST_STARTING)
371dd7e2610SBrian Somers       fsm_Close(&bundle->ncp.ipcp.fsm);
3723b0f8d2eSBrian Somers     else {
37309206a6fSBrian Somers       fsm2initial(&bundle->ncp.ipcp.fsm);
374d345321bSBrian Somers       for (dl = bundle->links; dl; dl = dl->next)
3759c81b87dSBrian Somers         datalink_Close(dl, how);
3767a6f8720SBrian Somers     }
3773b0f8d2eSBrian Somers   } else if (this_dl && this_dl->state != DATALINK_CLOSED &&
3783b0f8d2eSBrian Somers              this_dl->state != DATALINK_HANGUP)
3799c81b87dSBrian Somers     datalink_Close(this_dl, how);
380d2fd8d77SBrian Somers }
3817a6f8720SBrian Somers 
3821bc9b5baSBrian Somers void
383899011c4SBrian Somers bundle_Down(struct bundle *bundle, int how)
3841bc9b5baSBrian Somers {
3851bc9b5baSBrian Somers   struct datalink *dl;
3861bc9b5baSBrian Somers 
3871bc9b5baSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
388899011c4SBrian Somers     datalink_Down(dl, how);
3891bc9b5baSBrian Somers }
3901bc9b5baSBrian Somers 
3912f786681SBrian Somers static int
3922f786681SBrian Somers bundle_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
3932f786681SBrian Somers {
3942f786681SBrian Somers   struct bundle *bundle = descriptor2bundle(d);
3952f786681SBrian Somers   struct datalink *dl;
396ab2de065SBrian Somers   int result, queued, nlinks;
3972f786681SBrian Somers 
3982f786681SBrian Somers   result = 0;
3992f786681SBrian Somers 
400078c562eSBrian Somers   /* If there are aren't many packets queued, look for some more. */
40104eaa58cSBrian Somers   for (nlinks = 0, dl = bundle->links; dl; dl = dl->next)
40204eaa58cSBrian Somers     nlinks++;
40304eaa58cSBrian Somers 
40404eaa58cSBrian Somers   if (nlinks) {
4055a72b6edSBrian Somers     queued = r ? bundle_FillQueues(bundle) : ip_QueueLen(&bundle->ncp.ipcp);
40604eaa58cSBrian Somers 
407ff0f9439SBrian Somers     if (r && (bundle->phase == PHASE_NETWORK ||
408ff0f9439SBrian Somers               bundle->phys_type.all & PHYS_AUTO)) {
40904eaa58cSBrian Somers       /* enough surplus so that we can tell if we're getting swamped */
410ab2de065SBrian Somers       if (queued < 20) {
41104eaa58cSBrian Somers         /* Not enough - select() for more */
4126f8e9f0aSBrian Somers         if (bundle->choked.timer.state == TIMER_RUNNING)
4136f8e9f0aSBrian Somers           timer_Stop(&bundle->choked.timer);	/* Not needed any more */
41404eaa58cSBrian Somers         FD_SET(bundle->dev.fd, r);
415faefde08SBrian Somers         if (*n < bundle->dev.fd + 1)
416faefde08SBrian Somers           *n = bundle->dev.fd + 1;
4171384bd27SBrian Somers         log_Printf(LogTIMER, "%s: fdset(r) %d\n", TUN_NAME, bundle->dev.fd);
418078c562eSBrian Somers         result++;
4196f8e9f0aSBrian Somers       } else if (bundle->choked.timer.state == TIMER_STOPPED) {
4206f8e9f0aSBrian Somers         bundle->choked.timer.func = bundle_ClearQueues;
4216f8e9f0aSBrian Somers         bundle->choked.timer.name = "output choke";
4226f8e9f0aSBrian Somers         bundle->choked.timer.load = bundle->cfg.choked.timeout * SECTICKS;
4236f8e9f0aSBrian Somers         bundle->choked.timer.arg = bundle;
4246f8e9f0aSBrian Somers         timer_Start(&bundle->choked.timer);
425078c562eSBrian Somers       }
42604eaa58cSBrian Somers     }
42704eaa58cSBrian Somers   }
428078c562eSBrian Somers 
429f0cdd9c0SBrian Somers #ifndef NORADIUS
430f0cdd9c0SBrian Somers   result += descriptor_UpdateSet(&bundle->radius.desc, r, w, e, n);
431f0cdd9c0SBrian Somers #endif
432f0cdd9c0SBrian Somers 
43371555108SBrian Somers   /* Which links need a select() ? */
43471555108SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
43571555108SBrian Somers     result += descriptor_UpdateSet(&dl->desc, r, w, e, n);
43671555108SBrian Somers 
437ea722969SBrian Somers   /*
438ea722969SBrian Somers    * This *MUST* be called after the datalink UpdateSet()s as it
43904eaa58cSBrian Somers    * might be ``holding'' one of the datalinks (death-row) and
44004eaa58cSBrian Somers    * wants to be able to de-select() it from the descriptor set.
441ea722969SBrian Somers    */
4420f2f3eb3SBrian Somers   result += descriptor_UpdateSet(&bundle->ncp.mp.server.desc, r, w, e, n);
443ea722969SBrian Somers 
4442f786681SBrian Somers   return result;
4452f786681SBrian Somers }
4462f786681SBrian Somers 
4472f786681SBrian Somers static int
4482f786681SBrian Somers bundle_IsSet(struct descriptor *d, const fd_set *fdset)
4492f786681SBrian Somers {
4502f786681SBrian Somers   struct bundle *bundle = descriptor2bundle(d);
4512f786681SBrian Somers   struct datalink *dl;
4522f786681SBrian Somers 
4532f786681SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
4542f786681SBrian Somers     if (descriptor_IsSet(&dl->desc, fdset))
4552f786681SBrian Somers       return 1;
4562f786681SBrian Somers 
457f0cdd9c0SBrian Somers #ifndef NORADIUS
458f0cdd9c0SBrian Somers   if (descriptor_IsSet(&bundle->radius.desc, fdset))
459f0cdd9c0SBrian Somers     return 1;
460f0cdd9c0SBrian Somers #endif
461f0cdd9c0SBrian Somers 
462332b9de0SBrian Somers   if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset))
463332b9de0SBrian Somers     return 1;
464332b9de0SBrian Somers 
465faefde08SBrian Somers   return FD_ISSET(bundle->dev.fd, fdset);
4662f786681SBrian Somers }
4672f786681SBrian Somers 
4682f786681SBrian Somers static void
4692f786681SBrian Somers bundle_DescriptorRead(struct descriptor *d, struct bundle *bundle,
4702f786681SBrian Somers                       const fd_set *fdset)
4712f786681SBrian Somers {
4722f786681SBrian Somers   struct datalink *dl;
4732f786681SBrian Somers 
474ea722969SBrian Somers   if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset))
475ea722969SBrian Somers     descriptor_Read(&bundle->ncp.mp.server.desc, bundle, fdset);
476ea722969SBrian Somers 
4772f786681SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
4782f786681SBrian Somers     if (descriptor_IsSet(&dl->desc, fdset))
4792f786681SBrian Somers       descriptor_Read(&dl->desc, bundle, fdset);
480b6217683SBrian Somers 
481f0cdd9c0SBrian Somers #ifndef NORADIUS
482f0cdd9c0SBrian Somers   if (descriptor_IsSet(&bundle->radius.desc, fdset))
483f0cdd9c0SBrian Somers     descriptor_Read(&bundle->radius.desc, bundle, fdset);
484f0cdd9c0SBrian Somers #endif
485f0cdd9c0SBrian Somers 
486faefde08SBrian Somers   if (FD_ISSET(bundle->dev.fd, fdset)) {
487078c562eSBrian Somers     struct tun_data tun;
488078c562eSBrian Somers     int n, pri;
489078c562eSBrian Somers 
490078c562eSBrian Somers     /* something to read from tun */
491faefde08SBrian Somers     n = read(bundle->dev.fd, &tun, sizeof tun);
492078c562eSBrian Somers     if (n < 0) {
493a33b2ef7SBrian Somers       log_Printf(LogWARN, "read from %s: %s\n", TUN_NAME, strerror(errno));
494078c562eSBrian Somers       return;
495078c562eSBrian Somers     }
496078c562eSBrian Somers     n -= sizeof tun - sizeof tun.data;
497078c562eSBrian Somers     if (n <= 0) {
498a33b2ef7SBrian Somers       log_Printf(LogERROR, "read from %s: Only %d bytes read ?\n", TUN_NAME, n);
499078c562eSBrian Somers       return;
500078c562eSBrian Somers     }
501078c562eSBrian Somers     if (!tun_check_header(tun, AF_INET))
502078c562eSBrian Somers       return;
503078c562eSBrian Somers 
504078c562eSBrian Somers     if (((struct ip *)tun.data)->ip_dst.s_addr ==
505078c562eSBrian Somers         bundle->ncp.ipcp.my_ip.s_addr) {
506078c562eSBrian Somers       /* we've been asked to send something addressed *to* us :( */
507078c562eSBrian Somers       if (Enabled(bundle, OPT_LOOPBACK)) {
508078c562eSBrian Somers         pri = PacketCheck(bundle, tun.data, n, &bundle->filter.in);
509078c562eSBrian Somers         if (pri >= 0) {
5105d9e6103SBrian Somers           n += sizeof tun - sizeof tun.data;
5115d9e6103SBrian Somers           write(bundle->dev.fd, &tun, n);
512078c562eSBrian Somers           log_Printf(LogDEBUG, "Looped back packet addressed to myself\n");
513078c562eSBrian Somers         }
514078c562eSBrian Somers         return;
515078c562eSBrian Somers       } else
516078c562eSBrian Somers         log_Printf(LogDEBUG, "Oops - forwarding packet addressed to myself\n");
517078c562eSBrian Somers     }
518078c562eSBrian Somers 
519078c562eSBrian Somers     /*
520078c562eSBrian Somers      * Process on-demand dialup. Output packets are queued within tunnel
521078c562eSBrian Somers      * device until IPCP is opened.
522078c562eSBrian Somers      */
523078c562eSBrian Somers 
524078c562eSBrian Somers     if (bundle_Phase(bundle) == PHASE_DEAD) {
525078c562eSBrian Somers       /*
526078c562eSBrian Somers        * Note, we must be in AUTO mode :-/ otherwise our interface should
527078c562eSBrian Somers        * *not* be UP and we can't receive data
528078c562eSBrian Somers        */
529078c562eSBrian Somers       if ((pri = PacketCheck(bundle, tun.data, n, &bundle->filter.dial)) >= 0)
530ba23f397SBrian Somers         bundle_Open(bundle, NULL, PHYS_AUTO, 0);
531078c562eSBrian Somers       else
532078c562eSBrian Somers         /*
533078c562eSBrian Somers          * Drop the packet.  If we were to queue it, we'd just end up with
534078c562eSBrian Somers          * a pile of timed-out data in our output queue by the time we get
535078c562eSBrian Somers          * around to actually dialing.  We'd also prematurely reach the
536078c562eSBrian Somers          * threshold at which we stop select()ing to read() the tun
537078c562eSBrian Somers          * device - breaking auto-dial.
538078c562eSBrian Somers          */
539078c562eSBrian Somers         return;
540078c562eSBrian Somers     }
541078c562eSBrian Somers 
542078c562eSBrian Somers     pri = PacketCheck(bundle, tun.data, n, &bundle->filter.out);
5435d9e6103SBrian Somers     if (pri >= 0)
5445a72b6edSBrian Somers       ip_Enqueue(&bundle->ncp.ipcp, pri, tun.data, n);
545078c562eSBrian Somers   }
546078c562eSBrian Somers }
5472f786681SBrian Somers 
5481af29a6eSBrian Somers static int
5492f786681SBrian Somers bundle_DescriptorWrite(struct descriptor *d, struct bundle *bundle,
5502f786681SBrian Somers                        const fd_set *fdset)
5512f786681SBrian Somers {
5522f786681SBrian Somers   struct datalink *dl;
5531af29a6eSBrian Somers   int result = 0;
5542f786681SBrian Somers 
555ea722969SBrian Somers   /* This is not actually necessary as struct mpserver doesn't Write() */
556ea722969SBrian Somers   if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset))
557ea722969SBrian Somers     descriptor_Write(&bundle->ncp.mp.server.desc, bundle, fdset);
558ea722969SBrian Somers 
5592f786681SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
5602f786681SBrian Somers     if (descriptor_IsSet(&dl->desc, fdset))
5611af29a6eSBrian Somers       result += descriptor_Write(&dl->desc, bundle, fdset);
5621af29a6eSBrian Somers 
5631af29a6eSBrian Somers   return result;
5642f786681SBrian Somers }
5652f786681SBrian Somers 
566da66dd13SBrian Somers void
5671384bd27SBrian Somers bundle_LockTun(struct bundle *bundle)
5681384bd27SBrian Somers {
5691384bd27SBrian Somers   FILE *lockfile;
5701384bd27SBrian Somers   char pidfile[MAXPATHLEN];
5711384bd27SBrian Somers 
5721384bd27SBrian Somers   snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit);
5731384bd27SBrian Somers   lockfile = ID0fopen(pidfile, "w");
5741384bd27SBrian Somers   if (lockfile != NULL) {
5751384bd27SBrian Somers     fprintf(lockfile, "%d\n", (int)getpid());
5761384bd27SBrian Somers     fclose(lockfile);
5771384bd27SBrian Somers   }
5781384bd27SBrian Somers #ifndef RELEASE_CRUNCH
5791384bd27SBrian Somers   else
5801384bd27SBrian Somers     log_Printf(LogERROR, "Warning: Can't create %s: %s\n",
5811384bd27SBrian Somers                pidfile, strerror(errno));
5821384bd27SBrian Somers #endif
5831384bd27SBrian Somers }
5841384bd27SBrian Somers 
5851384bd27SBrian Somers static void
5861384bd27SBrian Somers bundle_UnlockTun(struct bundle *bundle)
5871384bd27SBrian Somers {
5881384bd27SBrian Somers   char pidfile[MAXPATHLEN];
5891384bd27SBrian Somers 
5901384bd27SBrian Somers   snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit);
5911384bd27SBrian Somers   ID0unlink(pidfile);
5921384bd27SBrian Somers }
5937a6f8720SBrian Somers 
5947a6f8720SBrian Somers struct bundle *
5958e7b8599SBrian Somers bundle_Create(const char *prefix, int type, const char **argv)
5967a6f8720SBrian Somers {
5977a6f8720SBrian Somers   static struct bundle bundle;		/* there can be only one */
5984e5196e9SBrian Somers   int enoentcount, err;
5994e5196e9SBrian Somers   const char *ifname;
6006ca65df0SBrian Somers #if defined(TUNSIFMODE) || defined(TUNSLMODE)
6010f203c7eSBrian Somers   int iff;
6020f203c7eSBrian Somers #endif
6037a6f8720SBrian Somers 
6048fa6ebe4SBrian Somers   if (bundle.iface != NULL) {	/* Already allocated ! */
605a33b2ef7SBrian Somers     log_Printf(LogALERT, "bundle_Create:  There's only one BUNDLE !\n");
6067a6f8720SBrian Somers     return NULL;
6077a6f8720SBrian Somers   }
6087a6f8720SBrian Somers 
6097a6f8720SBrian Somers   err = ENOENT;
6107a6f8720SBrian Somers   enoentcount = 0;
611107d62e7SBrian Somers   for (bundle.unit = 0; ; bundle.unit++) {
612faefde08SBrian Somers     snprintf(bundle.dev.Name, sizeof bundle.dev.Name, "%s%d",
613faefde08SBrian Somers              prefix, bundle.unit);
614faefde08SBrian Somers     bundle.dev.fd = ID0open(bundle.dev.Name, O_RDWR);
615faefde08SBrian Somers     if (bundle.dev.fd >= 0)
6167a6f8720SBrian Somers       break;
617107d62e7SBrian Somers     else if (errno == ENXIO) {
6187a6f8720SBrian Somers       err = errno;
619107d62e7SBrian Somers       break;
6207a6f8720SBrian Somers     } else if (errno == ENOENT) {
6217a6f8720SBrian Somers       if (++enoentcount > 2)
622107d62e7SBrian Somers 	break;
6237a6f8720SBrian Somers     } else
6247a6f8720SBrian Somers       err = errno;
6257a6f8720SBrian Somers   }
6267a6f8720SBrian Somers 
627faefde08SBrian Somers   if (bundle.dev.fd < 0) {
628dd7e2610SBrian Somers     log_Printf(LogWARN, "No available tunnel devices found (%s).\n",
62985b542cfSBrian Somers               strerror(err));
6307a6f8720SBrian Somers     return NULL;
6317a6f8720SBrian Somers   }
6327a6f8720SBrian Somers 
633dd7e2610SBrian Somers   log_SetTun(bundle.unit);
6348e7b8599SBrian Somers   bundle.argv = argv;
6350f781a72SBrian Somers   bundle.argv0 = argv[0];
6360f781a72SBrian Somers   bundle.argv1 = argv[1];
6377a6f8720SBrian Somers 
6388fa6ebe4SBrian Somers   ifname = strrchr(bundle.dev.Name, '/');
6398fa6ebe4SBrian Somers   if (ifname == NULL)
6408fa6ebe4SBrian Somers     ifname = bundle.dev.Name;
6417a6f8720SBrian Somers   else
6428fa6ebe4SBrian Somers     ifname++;
6438fa6ebe4SBrian Somers 
6448fa6ebe4SBrian Somers   bundle.iface = iface_Create(ifname);
6458fa6ebe4SBrian Somers   if (bundle.iface == NULL) {
6468fa6ebe4SBrian Somers     close(bundle.dev.fd);
6478fa6ebe4SBrian Somers     return NULL;
6488fa6ebe4SBrian Somers   }
6497a6f8720SBrian Somers 
6500f203c7eSBrian Somers #ifdef TUNSIFMODE
6510f203c7eSBrian Somers   /* Make sure we're POINTOPOINT */
6520f203c7eSBrian Somers   iff = IFF_POINTOPOINT;
6530f203c7eSBrian Somers   if (ID0ioctl(bundle.dev.fd, TUNSIFMODE, &iff) < 0)
6540f203c7eSBrian Somers     log_Printf(LogERROR, "bundle_Create: ioctl(TUNSIFMODE): %s\n",
6550f203c7eSBrian Somers 	       strerror(errno));
6560f203c7eSBrian Somers #endif
6570f203c7eSBrian Somers 
6586ca65df0SBrian Somers #ifdef TUNSLMODE
6596ca65df0SBrian Somers   /* Make sure we're POINTOPOINT */
6606ca65df0SBrian Somers   iff = 0;
6616ca65df0SBrian Somers   if (ID0ioctl(bundle.dev.fd, TUNSLMODE, &iff) < 0)
6626ca65df0SBrian Somers     log_Printf(LogERROR, "bundle_Create: ioctl(TUNSLMODE): %s\n",
6636ca65df0SBrian Somers 	       strerror(errno));
6646ca65df0SBrian Somers #endif
6656ca65df0SBrian Somers 
6664e5196e9SBrian Somers   if (!iface_SetFlags(bundle.iface, IFF_UP)) {
6678fa6ebe4SBrian Somers     iface_Destroy(bundle.iface);
6688fa6ebe4SBrian Somers     bundle.iface = NULL;
669faefde08SBrian Somers     close(bundle.dev.fd);
6707a6f8720SBrian Somers     return NULL;
6717a6f8720SBrian Somers   }
6727a6f8720SBrian Somers 
6738fa6ebe4SBrian Somers   log_Printf(LogPHASE, "Using interface: %s\n", ifname);
6747a6f8720SBrian Somers 
675ab2de065SBrian Somers   bundle.bandwidth = 0;
676820de6ebSBrian Somers   bundle.routing_seq = 0;
677a0cbd833SBrian Somers   bundle.phase = PHASE_DEAD;
678a0cbd833SBrian Somers   bundle.CleaningUp = 0;
679615ad4f9SBrian Somers   bundle.AliasEnabled = 0;
6807a6f8720SBrian Somers 
6816d666775SBrian Somers   bundle.fsm.LayerStart = bundle_LayerStart;
6826f384573SBrian Somers   bundle.fsm.LayerUp = bundle_LayerUp;
6836d666775SBrian Somers   bundle.fsm.LayerDown = bundle_LayerDown;
6846d666775SBrian Somers   bundle.fsm.LayerFinish = bundle_LayerFinish;
6856d666775SBrian Somers   bundle.fsm.object = &bundle;
6867a6f8720SBrian Somers 
687ab886ad0SBrian Somers   bundle.cfg.idle_timeout = NCP_IDLE_TIMEOUT;
6881342caedSBrian Somers   *bundle.cfg.auth.name = '\0';
6891342caedSBrian Somers   *bundle.cfg.auth.key = '\0';
690610b185fSBrian Somers   bundle.cfg.opt = OPT_SROUTES | OPT_IDCHECK | OPT_LOOPBACK |
691610b185fSBrian Somers                    OPT_THROUGHPUT | OPT_UTMP;
69249052c95SBrian Somers   *bundle.cfg.label = '\0';
69349052c95SBrian Somers   bundle.cfg.mtu = DEF_MTU;
6946f8e9f0aSBrian Somers   bundle.cfg.choked.timeout = CHOKED_TIMEOUT;
695ff0f9439SBrian Somers   bundle.phys_type.all = type;
696ff0f9439SBrian Somers   bundle.phys_type.open = 0;
697ab886ad0SBrian Somers 
6986f384573SBrian Somers   bundle.links = datalink_Create("deflink", &bundle, type);
6993006ec67SBrian Somers   if (bundle.links == NULL) {
700a33b2ef7SBrian Somers     log_Printf(LogALERT, "Cannot create data link: %s\n", strerror(errno));
7018fa6ebe4SBrian Somers     iface_Destroy(bundle.iface);
7028fa6ebe4SBrian Somers     bundle.iface = NULL;
703faefde08SBrian Somers     close(bundle.dev.fd);
7042289f246SBrian Somers     return NULL;
7052289f246SBrian Somers   }
7062289f246SBrian Somers 
7072f786681SBrian Somers   bundle.desc.type = BUNDLE_DESCRIPTOR;
7082f786681SBrian Somers   bundle.desc.UpdateSet = bundle_UpdateSet;
7092f786681SBrian Somers   bundle.desc.IsSet = bundle_IsSet;
7102f786681SBrian Somers   bundle.desc.Read = bundle_DescriptorRead;
7112f786681SBrian Somers   bundle.desc.Write = bundle_DescriptorWrite;
7122f786681SBrian Somers 
71349052c95SBrian Somers   mp_Init(&bundle.ncp.mp, &bundle);
71449052c95SBrian Somers 
71549052c95SBrian Somers   /* Send over the first physical link by default */
7165828db6dSBrian Somers   ipcp_Init(&bundle.ncp.ipcp, &bundle, &bundle.links->physical->link,
7175828db6dSBrian Somers             &bundle.fsm);
7186d666775SBrian Somers 
7195ca5389aSBrian Somers   memset(&bundle.filter, '\0', sizeof bundle.filter);
7205ca5389aSBrian Somers   bundle.filter.in.fragok = bundle.filter.in.logok = 1;
7215ca5389aSBrian Somers   bundle.filter.in.name = "IN";
7225ca5389aSBrian Somers   bundle.filter.out.fragok = bundle.filter.out.logok = 1;
7235ca5389aSBrian Somers   bundle.filter.out.name = "OUT";
7245ca5389aSBrian Somers   bundle.filter.dial.name = "DIAL";
7258390b576SBrian Somers   bundle.filter.dial.logok = 1;
7265ca5389aSBrian Somers   bundle.filter.alive.name = "ALIVE";
7275ca5389aSBrian Somers   bundle.filter.alive.logok = 1;
728cad7e742SBrian Somers   {
729cad7e742SBrian Somers     int	i;
730cad7e742SBrian Somers     for (i = 0; i < MAXFILTERS; i++) {
731cad7e742SBrian Somers         bundle.filter.in.rule[i].f_action = A_NONE;
732cad7e742SBrian Somers         bundle.filter.out.rule[i].f_action = A_NONE;
733cad7e742SBrian Somers         bundle.filter.dial.rule[i].f_action = A_NONE;
734cad7e742SBrian Somers         bundle.filter.alive.rule[i].f_action = A_NONE;
735cad7e742SBrian Somers     }
736cad7e742SBrian Somers   }
73793ee0ff2SBrian Somers   memset(&bundle.idle.timer, '\0', sizeof bundle.idle.timer);
73893ee0ff2SBrian Somers   bundle.idle.done = 0;
7395cf4388bSBrian Somers   bundle.notify.fd = -1;
7406f8e9f0aSBrian Somers   memset(&bundle.choked.timer, '\0', sizeof bundle.choked.timer);
741972a1bcfSBrian Somers #ifndef NORADIUS
742972a1bcfSBrian Somers   radius_Init(&bundle.radius);
743972a1bcfSBrian Somers #endif
74493ee0ff2SBrian Somers 
7456d666775SBrian Somers   /* Clean out any leftover crud */
7468fa6ebe4SBrian Somers   iface_Clear(bundle.iface, IFACE_CLEAR_ALL);
7476d666775SBrian Somers 
7481384bd27SBrian Somers   bundle_LockTun(&bundle);
7491384bd27SBrian Somers 
7507a6f8720SBrian Somers   return &bundle;
7517a6f8720SBrian Somers }
7527a6f8720SBrian Somers 
75368a0f0ccSBrian Somers static void
75468a0f0ccSBrian Somers bundle_DownInterface(struct bundle *bundle)
75568a0f0ccSBrian Somers {
756dd7e2610SBrian Somers   route_IfDelete(bundle, 1);
7574e5196e9SBrian Somers   iface_ClearFlags(bundle->iface, IFF_UP);
75868a0f0ccSBrian Somers }
75968a0f0ccSBrian Somers 
76068a0f0ccSBrian Somers void
76168a0f0ccSBrian Somers bundle_Destroy(struct bundle *bundle)
76268a0f0ccSBrian Somers {
7633006ec67SBrian Somers   struct datalink *dl;
764b6217683SBrian Somers 
765ea722969SBrian Somers   /*
76604eaa58cSBrian Somers    * Clean up the interface.  We don't need to timer_Stop()s, mp_Down(),
767ea722969SBrian Somers    * ipcp_CleanInterface() and bundle_DownInterface() unless we're getting
768ea722969SBrian Somers    * out under exceptional conditions such as a descriptor exception.
769ea722969SBrian Somers    */
77004eaa58cSBrian Somers   timer_Stop(&bundle->idle.timer);
7716f8e9f0aSBrian Somers   timer_Stop(&bundle->choked.timer);
77266f634b6SBrian Somers   mp_Down(&bundle->ncp.mp);
773dd7e2610SBrian Somers   ipcp_CleanInterface(&bundle->ncp.ipcp);
77468a0f0ccSBrian Somers   bundle_DownInterface(bundle);
7753006ec67SBrian Somers 
776972a1bcfSBrian Somers #ifndef NORADIUS
777972a1bcfSBrian Somers   /* Tell the radius server the bad news */
778972a1bcfSBrian Somers   radius_Destroy(&bundle->radius);
779972a1bcfSBrian Somers #endif
780972a1bcfSBrian Somers 
781ea722969SBrian Somers   /* Again, these are all DATALINK_CLOSED unless we're abending */
7823006ec67SBrian Somers   dl = bundle->links;
7833006ec67SBrian Somers   while (dl)
7843006ec67SBrian Somers     dl = datalink_Destroy(dl);
7853006ec67SBrian Somers 
7861384bd27SBrian Somers   close(bundle->dev.fd);
7871384bd27SBrian Somers   bundle_UnlockTun(bundle);
7881384bd27SBrian Somers 
789ea722969SBrian Somers   /* In case we never made PHASE_NETWORK */
7905cf4388bSBrian Somers   bundle_Notify(bundle, EX_ERRDEAD);
791b6217683SBrian Somers 
7928fa6ebe4SBrian Somers   iface_Destroy(bundle->iface);
7938fa6ebe4SBrian Somers   bundle->iface = NULL;
79468a0f0ccSBrian Somers }
79568a0f0ccSBrian Somers 
7967a6f8720SBrian Somers struct rtmsg {
7977a6f8720SBrian Somers   struct rt_msghdr m_rtm;
7987a6f8720SBrian Somers   char m_space[64];
7997a6f8720SBrian Somers };
8007a6f8720SBrian Somers 
801610b185fSBrian Somers int
802820de6ebSBrian Somers bundle_SetRoute(struct bundle *bundle, int cmd, struct in_addr dst,
8032062443fSBrian Somers                 struct in_addr gateway, struct in_addr mask, int bang, int ssh)
8047a6f8720SBrian Somers {
8057a6f8720SBrian Somers   struct rtmsg rtmes;
8067a6f8720SBrian Somers   int s, nb, wb;
8077a6f8720SBrian Somers   char *cp;
8087a6f8720SBrian Somers   const char *cmdstr;
8097a6f8720SBrian Somers   struct sockaddr_in rtdata;
810610b185fSBrian Somers   int result = 1;
8117a6f8720SBrian Somers 
8127a6f8720SBrian Somers   if (bang)
8137a6f8720SBrian Somers     cmdstr = (cmd == RTM_ADD ? "Add!" : "Delete!");
8147a6f8720SBrian Somers   else
8157a6f8720SBrian Somers     cmdstr = (cmd == RTM_ADD ? "Add" : "Delete");
8167a6f8720SBrian Somers   s = ID0socket(PF_ROUTE, SOCK_RAW, 0);
8177a6f8720SBrian Somers   if (s < 0) {
818dd7e2610SBrian Somers     log_Printf(LogERROR, "bundle_SetRoute: socket(): %s\n", strerror(errno));
819610b185fSBrian Somers     return result;
8207a6f8720SBrian Somers   }
8217a6f8720SBrian Somers   memset(&rtmes, '\0', sizeof rtmes);
8227a6f8720SBrian Somers   rtmes.m_rtm.rtm_version = RTM_VERSION;
8237a6f8720SBrian Somers   rtmes.m_rtm.rtm_type = cmd;
8247a6f8720SBrian Somers   rtmes.m_rtm.rtm_addrs = RTA_DST;
825820de6ebSBrian Somers   rtmes.m_rtm.rtm_seq = ++bundle->routing_seq;
8267a6f8720SBrian Somers   rtmes.m_rtm.rtm_pid = getpid();
8277a6f8720SBrian Somers   rtmes.m_rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;
8287a6f8720SBrian Somers 
8293afe5ccbSBrian Somers   if (cmd == RTM_ADD || cmd == RTM_CHANGE) {
8303afe5ccbSBrian Somers     if (bundle->ncp.ipcp.cfg.sendpipe > 0) {
8313afe5ccbSBrian Somers       rtmes.m_rtm.rtm_rmx.rmx_sendpipe = bundle->ncp.ipcp.cfg.sendpipe;
8323afe5ccbSBrian Somers       rtmes.m_rtm.rtm_inits |= RTV_SPIPE;
8333afe5ccbSBrian Somers     }
8343afe5ccbSBrian Somers     if (bundle->ncp.ipcp.cfg.recvpipe > 0) {
8353afe5ccbSBrian Somers       rtmes.m_rtm.rtm_rmx.rmx_recvpipe = bundle->ncp.ipcp.cfg.recvpipe;
8363afe5ccbSBrian Somers       rtmes.m_rtm.rtm_inits |= RTV_RPIPE;
8373afe5ccbSBrian Somers     }
8383afe5ccbSBrian Somers   }
8393afe5ccbSBrian Somers 
8407a6f8720SBrian Somers   memset(&rtdata, '\0', sizeof rtdata);
84150e5c17dSBrian Somers   rtdata.sin_len = sizeof rtdata;
8427a6f8720SBrian Somers   rtdata.sin_family = AF_INET;
8437a6f8720SBrian Somers   rtdata.sin_port = 0;
8447a6f8720SBrian Somers   rtdata.sin_addr = dst;
8457a6f8720SBrian Somers 
8467a6f8720SBrian Somers   cp = rtmes.m_space;
84750e5c17dSBrian Somers   memcpy(cp, &rtdata, rtdata.sin_len);
84850e5c17dSBrian Somers   cp += rtdata.sin_len;
849e43ebac1SBrian Somers   if (cmd == RTM_ADD) {
8507a6f8720SBrian Somers     if (gateway.s_addr == INADDR_ANY) {
8510aa8aa17SBrian Somers       if (!ssh)
8528fa6ebe4SBrian Somers         log_Printf(LogERROR, "bundle_SetRoute: Cannot add a route with"
8538fa6ebe4SBrian Somers                    " destination 0.0.0.0\n");
8548fa6ebe4SBrian Somers       close(s);
8558fa6ebe4SBrian Somers       return result;
8567a6f8720SBrian Somers     } else {
8577a6f8720SBrian Somers       rtdata.sin_addr = gateway;
85850e5c17dSBrian Somers       memcpy(cp, &rtdata, rtdata.sin_len);
85950e5c17dSBrian Somers       cp += rtdata.sin_len;
8607a6f8720SBrian Somers       rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY;
8617a6f8720SBrian Somers     }
862e43ebac1SBrian Somers   }
8637a6f8720SBrian Somers 
8647a6f8720SBrian Somers   if (dst.s_addr == INADDR_ANY)
8657a6f8720SBrian Somers     mask.s_addr = INADDR_ANY;
8667a6f8720SBrian Somers 
8677a6f8720SBrian Somers   if (cmd == RTM_ADD || dst.s_addr == INADDR_ANY) {
8687a6f8720SBrian Somers     rtdata.sin_addr = mask;
86950e5c17dSBrian Somers     memcpy(cp, &rtdata, rtdata.sin_len);
87050e5c17dSBrian Somers     cp += rtdata.sin_len;
8717a6f8720SBrian Somers     rtmes.m_rtm.rtm_addrs |= RTA_NETMASK;
8727a6f8720SBrian Somers   }
8737a6f8720SBrian Somers 
8747a6f8720SBrian Somers   nb = cp - (char *) &rtmes;
8757a6f8720SBrian Somers   rtmes.m_rtm.rtm_msglen = nb;
8767a6f8720SBrian Somers   wb = ID0write(s, &rtmes, nb);
8777a6f8720SBrian Somers   if (wb < 0) {
878dd7e2610SBrian Somers     log_Printf(LogTCPIP, "bundle_SetRoute failure:\n");
879dd7e2610SBrian Somers     log_Printf(LogTCPIP, "bundle_SetRoute:  Cmd = %s\n", cmdstr);
880dd7e2610SBrian Somers     log_Printf(LogTCPIP, "bundle_SetRoute:  Dst = %s\n", inet_ntoa(dst));
881dd7e2610SBrian Somers     log_Printf(LogTCPIP, "bundle_SetRoute:  Gateway = %s\n", inet_ntoa(gateway));
882dd7e2610SBrian Somers     log_Printf(LogTCPIP, "bundle_SetRoute:  Mask = %s\n", inet_ntoa(mask));
8837a6f8720SBrian Somers failed:
8847a6f8720SBrian Somers     if (cmd == RTM_ADD && (rtmes.m_rtm.rtm_errno == EEXIST ||
885e43ebac1SBrian Somers                            (rtmes.m_rtm.rtm_errno == 0 && errno == EEXIST))) {
886610b185fSBrian Somers       if (!bang) {
887dd7e2610SBrian Somers         log_Printf(LogWARN, "Add route failed: %s already exists\n",
8883afe5ccbSBrian Somers 		  dst.s_addr == 0 ? "default" : inet_ntoa(dst));
889610b185fSBrian Somers         result = 0;	/* Don't add to our dynamic list */
890610b185fSBrian Somers       } else {
8917a6f8720SBrian Somers         rtmes.m_rtm.rtm_type = cmd = RTM_CHANGE;
8927a6f8720SBrian Somers         if ((wb = ID0write(s, &rtmes, nb)) < 0)
8937a6f8720SBrian Somers           goto failed;
8947a6f8720SBrian Somers       }
895e43ebac1SBrian Somers     } else if (cmd == RTM_DELETE &&
8967a6f8720SBrian Somers              (rtmes.m_rtm.rtm_errno == ESRCH ||
8977a6f8720SBrian Somers               (rtmes.m_rtm.rtm_errno == 0 && errno == ESRCH))) {
8987a6f8720SBrian Somers       if (!bang)
899dd7e2610SBrian Somers         log_Printf(LogWARN, "Del route failed: %s: Non-existent\n",
9007a6f8720SBrian Somers                   inet_ntoa(dst));
9012062443fSBrian Somers     } else if (rtmes.m_rtm.rtm_errno == 0) {
9022062443fSBrian Somers       if (!ssh || errno != ENETUNREACH)
903dd7e2610SBrian Somers         log_Printf(LogWARN, "%s route failed: %s: errno: %s\n", cmdstr,
9047a6f8720SBrian Somers                    inet_ntoa(dst), strerror(errno));
9052062443fSBrian Somers     } else
906dd7e2610SBrian Somers       log_Printf(LogWARN, "%s route failed: %s: %s\n",
9077a6f8720SBrian Somers 		 cmdstr, inet_ntoa(dst), strerror(rtmes.m_rtm.rtm_errno));
9087a6f8720SBrian Somers   }
909dd7e2610SBrian Somers   log_Printf(LogDEBUG, "wrote %d: cmd = %s, dst = %x, gateway = %x\n",
910fe3125a0SBrian Somers             wb, cmdstr, (unsigned)dst.s_addr, (unsigned)gateway.s_addr);
9117a6f8720SBrian Somers   close(s);
912610b185fSBrian Somers 
913610b185fSBrian Somers   return result;
9147a6f8720SBrian Somers }
91583d1af55SBrian Somers 
91683d1af55SBrian Somers void
9173006ec67SBrian Somers bundle_LinkClosed(struct bundle *bundle, struct datalink *dl)
9183006ec67SBrian Somers {
9193006ec67SBrian Somers   /*
9203006ec67SBrian Somers    * Our datalink has closed.
921ea722969SBrian Somers    * CleanDatalinks() (called from DoLoop()) will remove closed
92281358fa3SBrian Somers    * BACKGROUND and DIRECT links.
9233b0f8d2eSBrian Somers    * If it's the last data link, enter phase DEAD.
924ea722969SBrian Somers    *
925ea722969SBrian Somers    * NOTE: dl may not be in our list (bundle_SendDatalink()) !
9263006ec67SBrian Somers    */
9275b8b8060SBrian Somers 
9283b0f8d2eSBrian Somers   struct datalink *odl;
9293b0f8d2eSBrian Somers   int other_links;
9305b8b8060SBrian Somers 
931bf1d3ff6SBrian Somers   log_SetTtyCommandMode(dl);
932bf1d3ff6SBrian Somers 
9333b0f8d2eSBrian Somers   other_links = 0;
9343b0f8d2eSBrian Somers   for (odl = bundle->links; odl; odl = odl->next)
9353b0f8d2eSBrian Somers     if (odl != dl && odl->state != DATALINK_CLOSED)
9363b0f8d2eSBrian Somers       other_links++;
9373b0f8d2eSBrian Somers 
9383b0f8d2eSBrian Somers   if (!other_links) {
93981358fa3SBrian Somers     if (dl->physical->type != PHYS_AUTO)	/* Not in -auto mode */
94003704096SBrian Somers       bundle_DownInterface(bundle);
94109206a6fSBrian Somers     fsm2initial(&bundle->ncp.ipcp.fsm);
9425563ebdeSBrian Somers     bundle_NewPhase(bundle, PHASE_DEAD);
9430f2f3eb3SBrian Somers     bundle_StopIdleTimer(bundle);
944ab2de065SBrian Somers   }
945455aabc3SBrian Somers }
946455aabc3SBrian Somers 
947455aabc3SBrian Somers void
948ba23f397SBrian Somers bundle_Open(struct bundle *bundle, const char *name, int mask, int force)
9493006ec67SBrian Somers {
9503006ec67SBrian Somers   /*
9513006ec67SBrian Somers    * Please open the given datalink, or all if name == NULL
9523006ec67SBrian Somers    */
9533006ec67SBrian Somers   struct datalink *dl;
9543006ec67SBrian Somers 
9553006ec67SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
9563006ec67SBrian Somers     if (name == NULL || !strcasecmp(dl->name, name)) {
957ba23f397SBrian Somers       if ((mask & dl->physical->type) &&
958ba23f397SBrian Somers           (dl->state == DATALINK_CLOSED ||
959ba23f397SBrian Somers            (force && dl->state == DATALINK_OPENING &&
960c11e57a3SBrian Somers             dl->dial.timer.state == TIMER_RUNNING))) {
961c11e57a3SBrian Somers         if (force)	/* Ignore redial timeout ? */
962c11e57a3SBrian Somers           timer_Stop(&dl->dial.timer);
963565e35e5SBrian Somers         datalink_Up(dl, 1, 1);
964ab2de065SBrian Somers         if (mask & PHYS_AUTO)
965ab2de065SBrian Somers           /* Only one AUTO link at a time */
96604eaa58cSBrian Somers           break;
96704eaa58cSBrian Somers       }
9683006ec67SBrian Somers       if (name != NULL)
9693006ec67SBrian Somers         break;
9703006ec67SBrian Somers     }
9713006ec67SBrian Somers }
9723006ec67SBrian Somers 
9733006ec67SBrian Somers struct datalink *
9743006ec67SBrian Somers bundle2datalink(struct bundle *bundle, const char *name)
9753006ec67SBrian Somers {
9763006ec67SBrian Somers   struct datalink *dl;
9773006ec67SBrian Somers 
9783006ec67SBrian Somers   if (name != NULL) {
9793006ec67SBrian Somers     for (dl = bundle->links; dl; dl = dl->next)
9803006ec67SBrian Somers       if (!strcasecmp(dl->name, name))
9813006ec67SBrian Somers         return dl;
9823006ec67SBrian Somers   } else if (bundle->links && !bundle->links->next)
9833006ec67SBrian Somers     return bundle->links;
9843006ec67SBrian Somers 
9853006ec67SBrian Somers   return NULL;
9863006ec67SBrian Somers }
9873006ec67SBrian Somers 
9883006ec67SBrian Somers int
9893006ec67SBrian Somers bundle_FillQueues(struct bundle *bundle)
9903006ec67SBrian Somers {
9913b0f8d2eSBrian Somers   int total;
9923b0f8d2eSBrian Somers 
9931bc9b5baSBrian Somers   if (bundle->ncp.mp.active)
9943b0f8d2eSBrian Somers     total = mp_FillQueues(bundle);
9951bc9b5baSBrian Somers   else {
9961bc9b5baSBrian Somers     struct datalink *dl;
9971bc9b5baSBrian Somers     int add;
9981bc9b5baSBrian Somers 
9991bc9b5baSBrian Somers     for (total = 0, dl = bundle->links; dl; dl = dl->next)
10001bc9b5baSBrian Somers       if (dl->state == DATALINK_OPEN) {
10011bc9b5baSBrian Somers         add = link_QueueLen(&dl->physical->link);
10021bc9b5baSBrian Somers         if (add == 0 && dl->physical->out == NULL)
10035d9e6103SBrian Somers           add = ip_PushPacket(&dl->physical->link, bundle);
10041bc9b5baSBrian Somers         total += add;
10051bc9b5baSBrian Somers       }
10063006ec67SBrian Somers   }
10073006ec67SBrian Somers 
10085a72b6edSBrian Somers   return total + ip_QueueLen(&bundle->ncp.ipcp);
10093006ec67SBrian Somers }
1010aef795ccSBrian Somers 
1011aef795ccSBrian Somers int
1012aef795ccSBrian Somers bundle_ShowLinks(struct cmdargs const *arg)
1013aef795ccSBrian Somers {
1014aef795ccSBrian Somers   struct datalink *dl;
1015ab2de065SBrian Somers   struct pppThroughput *t;
1016ab2de065SBrian Somers   int secs;
1017aef795ccSBrian Somers 
10189c53a7b1SBrian Somers   for (dl = arg->bundle->links; dl; dl = dl->next) {
1019d4156d00SBrian Somers     prompt_Printf(arg->prompt, "Name: %s [%s, %s]",
1020d4156d00SBrian Somers                   dl->name, mode2Nam(dl->physical->type), datalink_State(dl));
1021eeab6bf5SBrian Somers     if (dl->physical->link.throughput.rolling && dl->state == DATALINK_OPEN)
1022e531f89aSBrian Somers       prompt_Printf(arg->prompt, " bandwidth %d, %llu bps (%llu bytes/sec)",
1023ab2de065SBrian Somers                     dl->mp.bandwidth ? dl->mp.bandwidth :
1024ab2de065SBrian Somers                                        physical_GetSpeed(dl->physical),
1025ab2de065SBrian Somers                     dl->physical->link.throughput.OctetsPerSecond * 8,
10269c53a7b1SBrian Somers                     dl->physical->link.throughput.OctetsPerSecond);
10279c53a7b1SBrian Somers     prompt_Printf(arg->prompt, "\n");
10289c53a7b1SBrian Somers   }
1029aef795ccSBrian Somers 
1030ab2de065SBrian Somers   t = &arg->bundle->ncp.mp.link.throughput;
1031ab2de065SBrian Somers   secs = t->downtime ? 0 : throughput_uptime(t);
1032ab2de065SBrian Somers   if (secs > t->SamplePeriod)
1033ab2de065SBrian Somers     secs = t->SamplePeriod;
1034ab2de065SBrian Somers   if (secs)
1035e531f89aSBrian Somers     prompt_Printf(arg->prompt, "Currently averaging %llu bps (%llu bytes/sec)"
1036ab2de065SBrian Somers                   " over the last %d secs\n", t->OctetsPerSecond * 8,
1037ab2de065SBrian Somers                   t->OctetsPerSecond, secs);
1038ab2de065SBrian Somers 
1039aef795ccSBrian Somers   return 0;
1040aef795ccSBrian Somers }
1041ab886ad0SBrian Somers 
10421342caedSBrian Somers static const char *
10431342caedSBrian Somers optval(struct bundle *bundle, int bit)
10441342caedSBrian Somers {
10451342caedSBrian Somers   return (bundle->cfg.opt & bit) ? "enabled" : "disabled";
10461342caedSBrian Somers }
10471342caedSBrian Somers 
1048c08717dfSBrian Somers int
1049c08717dfSBrian Somers bundle_ShowStatus(struct cmdargs const *arg)
1050c08717dfSBrian Somers {
1051c08717dfSBrian Somers   int remaining;
1052c08717dfSBrian Somers 
1053c08717dfSBrian Somers   prompt_Printf(arg->prompt, "Phase %s\n", bundle_PhaseName(arg->bundle));
10540f781a72SBrian Somers   prompt_Printf(arg->prompt, " Title:         %s\n", arg->bundle->argv[0]);
1055faefde08SBrian Somers   prompt_Printf(arg->prompt, " Device:        %s\n", arg->bundle->dev.Name);
1056faefde08SBrian Somers   prompt_Printf(arg->prompt, " Interface:     %s @ %lubps\n",
1057ab2de065SBrian Somers                 arg->bundle->iface->name, arg->bundle->bandwidth);
1058c08717dfSBrian Somers 
1059c08717dfSBrian Somers   prompt_Printf(arg->prompt, "\nDefaults:\n");
1060643f4904SBrian Somers   prompt_Printf(arg->prompt, " Label:         %s\n", arg->bundle->cfg.label);
1061610b185fSBrian Somers   prompt_Printf(arg->prompt, " Auth name:     %s\n",
1062610b185fSBrian Somers                 arg->bundle->cfg.auth.name);
106304eaa58cSBrian Somers 
10646f8e9f0aSBrian Somers   prompt_Printf(arg->prompt, " Choked Timer:  %ds\n",
10656f8e9f0aSBrian Somers                 arg->bundle->cfg.choked.timeout);
1066972a1bcfSBrian Somers 
1067972a1bcfSBrian Somers #ifndef NORADIUS
1068972a1bcfSBrian Somers   radius_Show(&arg->bundle->radius, arg->prompt);
1069972a1bcfSBrian Somers #endif
1070972a1bcfSBrian Somers 
1071c08717dfSBrian Somers   prompt_Printf(arg->prompt, " Idle Timer:    ");
1072c08717dfSBrian Somers   if (arg->bundle->cfg.idle_timeout) {
1073c08717dfSBrian Somers     prompt_Printf(arg->prompt, "%ds", arg->bundle->cfg.idle_timeout);
1074c08717dfSBrian Somers     remaining = bundle_RemainingIdleTime(arg->bundle);
1075c08717dfSBrian Somers     if (remaining != -1)
1076c08717dfSBrian Somers       prompt_Printf(arg->prompt, " (%ds remaining)", remaining);
1077c08717dfSBrian Somers     prompt_Printf(arg->prompt, "\n");
1078c08717dfSBrian Somers   } else
1079c08717dfSBrian Somers     prompt_Printf(arg->prompt, "disabled\n");
1080ce828a6eSBrian Somers   prompt_Printf(arg->prompt, " MTU:           ");
1081ce828a6eSBrian Somers   if (arg->bundle->cfg.mtu)
1082ce828a6eSBrian Somers     prompt_Printf(arg->prompt, "%d\n", arg->bundle->cfg.mtu);
1083ce828a6eSBrian Somers   else
1084ce828a6eSBrian Somers     prompt_Printf(arg->prompt, "unspecified\n");
1085c08717dfSBrian Somers 
10863afe5ccbSBrian Somers   prompt_Printf(arg->prompt, " sendpipe:      ");
10873afe5ccbSBrian Somers   if (arg->bundle->ncp.ipcp.cfg.sendpipe > 0)
1088ab2de065SBrian Somers     prompt_Printf(arg->prompt, "%-20ld", arg->bundle->ncp.ipcp.cfg.sendpipe);
10893afe5ccbSBrian Somers   else
1090ab2de065SBrian Somers     prompt_Printf(arg->prompt, "unspecified         ");
10913afe5ccbSBrian Somers   prompt_Printf(arg->prompt, " recvpipe:      ");
10923afe5ccbSBrian Somers   if (arg->bundle->ncp.ipcp.cfg.recvpipe > 0)
10933afe5ccbSBrian Somers     prompt_Printf(arg->prompt, "%ld\n", arg->bundle->ncp.ipcp.cfg.recvpipe);
10943afe5ccbSBrian Somers   else
10953afe5ccbSBrian Somers     prompt_Printf(arg->prompt, "unspecified\n");
10963afe5ccbSBrian Somers 
1097ab2de065SBrian Somers   prompt_Printf(arg->prompt, " Sticky Routes: %-20.20s",
1098610b185fSBrian Somers                 optval(arg->bundle, OPT_SROUTES));
10991342caedSBrian Somers   prompt_Printf(arg->prompt, " ID check:      %s\n",
11001342caedSBrian Somers                 optval(arg->bundle, OPT_IDCHECK));
1101ab2de065SBrian Somers   prompt_Printf(arg->prompt, " Keep-Session:  %-20.20s",
1102ac685e31SBrian Somers                 optval(arg->bundle, OPT_KEEPSESSION));
11031342caedSBrian Somers   prompt_Printf(arg->prompt, " Loopback:      %s\n",
11041342caedSBrian Somers                 optval(arg->bundle, OPT_LOOPBACK));
1105ab2de065SBrian Somers   prompt_Printf(arg->prompt, " PasswdAuth:    %-20.20s",
11061342caedSBrian Somers                 optval(arg->bundle, OPT_PASSWDAUTH));
11071342caedSBrian Somers   prompt_Printf(arg->prompt, " Proxy:         %s\n",
11081342caedSBrian Somers                 optval(arg->bundle, OPT_PROXY));
1109ab2de065SBrian Somers   prompt_Printf(arg->prompt, " Proxyall:      %-20.20s",
11103afe5ccbSBrian Somers                 optval(arg->bundle, OPT_PROXYALL));
11111342caedSBrian Somers   prompt_Printf(arg->prompt, " Throughput:    %s\n",
11121342caedSBrian Somers                 optval(arg->bundle, OPT_THROUGHPUT));
1113ab2de065SBrian Somers   prompt_Printf(arg->prompt, " Utmp Logging:  %-20.20s",
11141342caedSBrian Somers                 optval(arg->bundle, OPT_UTMP));
11158fa6ebe4SBrian Somers   prompt_Printf(arg->prompt, " Iface-Alias:   %s\n",
11168fa6ebe4SBrian Somers                 optval(arg->bundle, OPT_IFACEALIAS));
11171342caedSBrian Somers 
1118c08717dfSBrian Somers   return 0;
1119c08717dfSBrian Somers }
1120c08717dfSBrian Somers 
1121ab886ad0SBrian Somers static void
1122ab886ad0SBrian Somers bundle_IdleTimeout(void *v)
1123ab886ad0SBrian Somers {
1124ab886ad0SBrian Somers   struct bundle *bundle = (struct bundle *)v;
1125ab886ad0SBrian Somers 
1126dd7e2610SBrian Somers   log_Printf(LogPHASE, "Idle timer expired.\n");
112704eaa58cSBrian Somers   bundle_StopIdleTimer(bundle);
11289c81b87dSBrian Somers   bundle_Close(bundle, NULL, CLOSE_STAYDOWN);
1129ab886ad0SBrian Somers }
1130ab886ad0SBrian Somers 
1131ab886ad0SBrian Somers /*
1132ab886ad0SBrian Somers  *  Start Idle timer. If timeout is reached, we call bundle_Close() to
1133ab886ad0SBrian Somers  *  close LCP and link.
1134ab886ad0SBrian Somers  */
1135ab886ad0SBrian Somers void
1136ab886ad0SBrian Somers bundle_StartIdleTimer(struct bundle *bundle)
1137ab886ad0SBrian Somers {
1138dd7e2610SBrian Somers   timer_Stop(&bundle->idle.timer);
1139ff0f9439SBrian Somers   if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) !=
1140ff0f9439SBrian Somers       bundle->phys_type.open && bundle->cfg.idle_timeout) {
114193ee0ff2SBrian Somers     bundle->idle.timer.func = bundle_IdleTimeout;
11423b0f8d2eSBrian Somers     bundle->idle.timer.name = "idle";
114393ee0ff2SBrian Somers     bundle->idle.timer.load = bundle->cfg.idle_timeout * SECTICKS;
114493ee0ff2SBrian Somers     bundle->idle.timer.arg = bundle;
1145dd7e2610SBrian Somers     timer_Start(&bundle->idle.timer);
114693ee0ff2SBrian Somers     bundle->idle.done = time(NULL) + bundle->cfg.idle_timeout;
1147ab886ad0SBrian Somers   }
1148ab886ad0SBrian Somers }
1149ab886ad0SBrian Somers 
1150ab886ad0SBrian Somers void
1151ab886ad0SBrian Somers bundle_SetIdleTimer(struct bundle *bundle, int value)
1152ab886ad0SBrian Somers {
1153ab886ad0SBrian Somers   bundle->cfg.idle_timeout = value;
1154ab886ad0SBrian Somers   if (bundle_LinkIsUp(bundle))
1155ab886ad0SBrian Somers     bundle_StartIdleTimer(bundle);
1156ab886ad0SBrian Somers }
1157ab886ad0SBrian Somers 
1158ab886ad0SBrian Somers void
1159ab886ad0SBrian Somers bundle_StopIdleTimer(struct bundle *bundle)
1160ab886ad0SBrian Somers {
1161dd7e2610SBrian Somers   timer_Stop(&bundle->idle.timer);
11624a632c80SBrian Somers   bundle->idle.done = 0;
1163ab886ad0SBrian Somers }
1164ab886ad0SBrian Somers 
116504eaa58cSBrian Somers static int
1166ab886ad0SBrian Somers bundle_RemainingIdleTime(struct bundle *bundle)
1167ab886ad0SBrian Somers {
116893ee0ff2SBrian Somers   if (bundle->idle.done)
116993ee0ff2SBrian Somers     return bundle->idle.done - time(NULL);
1170ab886ad0SBrian Somers   return -1;
1171ab886ad0SBrian Somers }
11723b0f8d2eSBrian Somers 
11733b0f8d2eSBrian Somers int
11743b0f8d2eSBrian Somers bundle_IsDead(struct bundle *bundle)
11753b0f8d2eSBrian Somers {
11763b0f8d2eSBrian Somers   return !bundle->links || (bundle->phase == PHASE_DEAD && bundle->CleaningUp);
11773b0f8d2eSBrian Somers }
1178b6217683SBrian Somers 
117904eaa58cSBrian Somers static struct datalink *
118004eaa58cSBrian Somers bundle_DatalinkLinkout(struct bundle *bundle, struct datalink *dl)
1181cd7bd93aSBrian Somers {
1182cd7bd93aSBrian Somers   struct datalink **dlp;
1183cd7bd93aSBrian Somers 
1184cd7bd93aSBrian Somers   for (dlp = &bundle->links; *dlp; dlp = &(*dlp)->next)
1185cd7bd93aSBrian Somers     if (*dlp == dl) {
118604eaa58cSBrian Somers       *dlp = dl->next;
118704eaa58cSBrian Somers       dl->next = NULL;
118804eaa58cSBrian Somers       bundle_LinksRemoved(bundle);
118904eaa58cSBrian Somers       return dl;
1190cd7bd93aSBrian Somers     }
119104eaa58cSBrian Somers 
119204eaa58cSBrian Somers   return NULL;
119304eaa58cSBrian Somers }
119404eaa58cSBrian Somers 
119504eaa58cSBrian Somers static void
119604eaa58cSBrian Somers bundle_DatalinkLinkin(struct bundle *bundle, struct datalink *dl)
119704eaa58cSBrian Somers {
119804eaa58cSBrian Somers   struct datalink **dlp = &bundle->links;
119904eaa58cSBrian Somers 
120004eaa58cSBrian Somers   while (*dlp)
120104eaa58cSBrian Somers     dlp = &(*dlp)->next;
120204eaa58cSBrian Somers 
120304eaa58cSBrian Somers   *dlp = dl;
120404eaa58cSBrian Somers   dl->next = NULL;
120504eaa58cSBrian Somers 
120604eaa58cSBrian Somers   bundle_LinkAdded(bundle, dl);
1207ab2de065SBrian Somers   mp_CheckAutoloadTimer(&bundle->ncp.mp);
1208565e35e5SBrian Somers }
1209565e35e5SBrian Somers 
1210565e35e5SBrian Somers void
1211565e35e5SBrian Somers bundle_CleanDatalinks(struct bundle *bundle)
1212565e35e5SBrian Somers {
1213565e35e5SBrian Somers   struct datalink **dlp = &bundle->links;
121404eaa58cSBrian Somers   int found = 0;
1215565e35e5SBrian Somers 
1216565e35e5SBrian Somers   while (*dlp)
1217565e35e5SBrian Somers     if ((*dlp)->state == DATALINK_CLOSED &&
121881358fa3SBrian Somers         (*dlp)->physical->type & (PHYS_DIRECT|PHYS_BACKGROUND)) {
1219565e35e5SBrian Somers       *dlp = datalink_Destroy(*dlp);
122004eaa58cSBrian Somers       found++;
122104eaa58cSBrian Somers     } else
1222565e35e5SBrian Somers       dlp = &(*dlp)->next;
122304eaa58cSBrian Somers 
122404eaa58cSBrian Somers   if (found)
122504eaa58cSBrian Somers     bundle_LinksRemoved(bundle);
122604eaa58cSBrian Somers }
122704eaa58cSBrian Somers 
122804eaa58cSBrian Somers int
122904eaa58cSBrian Somers bundle_DatalinkClone(struct bundle *bundle, struct datalink *dl,
123004eaa58cSBrian Somers                      const char *name)
123104eaa58cSBrian Somers {
123204eaa58cSBrian Somers   if (bundle2datalink(bundle, name)) {
123304eaa58cSBrian Somers     log_Printf(LogWARN, "Clone: %s: name already exists\n", name);
123404eaa58cSBrian Somers     return 0;
123504eaa58cSBrian Somers   }
123604eaa58cSBrian Somers 
123704eaa58cSBrian Somers   bundle_DatalinkLinkin(bundle, datalink_Clone(dl, name));
123804eaa58cSBrian Somers   return 1;
123904eaa58cSBrian Somers }
124004eaa58cSBrian Somers 
124104eaa58cSBrian Somers void
124204eaa58cSBrian Somers bundle_DatalinkRemove(struct bundle *bundle, struct datalink *dl)
124304eaa58cSBrian Somers {
124404eaa58cSBrian Somers   dl = bundle_DatalinkLinkout(bundle, dl);
124504eaa58cSBrian Somers   if (dl)
124604eaa58cSBrian Somers     datalink_Destroy(dl);
1247cd7bd93aSBrian Somers }
124849052c95SBrian Somers 
124949052c95SBrian Somers void
125049052c95SBrian Somers bundle_SetLabel(struct bundle *bundle, const char *label)
125149052c95SBrian Somers {
125249052c95SBrian Somers   if (label)
125349052c95SBrian Somers     strncpy(bundle->cfg.label, label, sizeof bundle->cfg.label - 1);
125449052c95SBrian Somers   else
125549052c95SBrian Somers     *bundle->cfg.label = '\0';
125649052c95SBrian Somers }
125749052c95SBrian Somers 
125849052c95SBrian Somers const char *
125949052c95SBrian Somers bundle_GetLabel(struct bundle *bundle)
126049052c95SBrian Somers {
126149052c95SBrian Somers   return *bundle->cfg.label ? bundle->cfg.label : NULL;
126249052c95SBrian Somers }
12631fa665f5SBrian Somers 
12641fa665f5SBrian Somers void
126596c9bb21SBrian Somers bundle_ReceiveDatalink(struct bundle *bundle, int s, struct sockaddr_un *sun)
12661fa665f5SBrian Somers {
126796c9bb21SBrian Somers   char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int)];
126896c9bb21SBrian Somers   struct cmsghdr *cmsg = (struct cmsghdr *)cmsgbuf;
126996c9bb21SBrian Somers   struct msghdr msg;
127096c9bb21SBrian Somers   struct iovec iov[SCATTER_SEGMENTS];
1271b7c5748eSBrian Somers   struct datalink *dl;
127296c9bb21SBrian Somers   int niov, link_fd, expect, f;
127385fd273aSBrian Somers   pid_t pid;
12746f384573SBrian Somers 
1275dd7e2610SBrian Somers   log_Printf(LogPHASE, "Receiving datalink\n");
12766f384573SBrian Somers 
127796c9bb21SBrian Somers   /* Create our scatter/gather array */
127896c9bb21SBrian Somers   niov = 1;
127996c9bb21SBrian Somers   iov[0].iov_len = strlen(Version) + 1;
128096c9bb21SBrian Somers   iov[0].iov_base = (char *)malloc(iov[0].iov_len);
128185fd273aSBrian Somers   if (datalink2iov(NULL, iov, &niov, sizeof iov / sizeof *iov, 0) == -1) {
128254cd8e13SBrian Somers     close(s);
12836f384573SBrian Somers     return;
128454cd8e13SBrian Somers   }
12856f384573SBrian Somers 
128685fd273aSBrian Somers   pid = getpid();
128785fd273aSBrian Somers   write(s, &pid, sizeof pid);
128885fd273aSBrian Somers 
128996c9bb21SBrian Somers   for (f = expect = 0; f < niov; f++)
129096c9bb21SBrian Somers     expect += iov[f].iov_len;
129196c9bb21SBrian Somers 
129296c9bb21SBrian Somers   /* Set up our message */
129396c9bb21SBrian Somers   cmsg->cmsg_len = sizeof cmsgbuf;
129496c9bb21SBrian Somers   cmsg->cmsg_level = SOL_SOCKET;
129554cd8e13SBrian Somers   cmsg->cmsg_type = 0;
129696c9bb21SBrian Somers 
129796c9bb21SBrian Somers   memset(&msg, '\0', sizeof msg);
129896c9bb21SBrian Somers   msg.msg_name = (caddr_t)sun;
129996c9bb21SBrian Somers   msg.msg_namelen = sizeof *sun;
130096c9bb21SBrian Somers   msg.msg_iov = iov;
130196c9bb21SBrian Somers   msg.msg_iovlen = niov;
130296c9bb21SBrian Somers   msg.msg_control = cmsgbuf;
130396c9bb21SBrian Somers   msg.msg_controllen = sizeof cmsgbuf;
130496c9bb21SBrian Somers 
130596c9bb21SBrian Somers   log_Printf(LogDEBUG, "Expecting %d scatter/gather bytes\n", expect);
130696c9bb21SBrian Somers   f = expect + 100;
130796c9bb21SBrian Somers   setsockopt(s, SOL_SOCKET, SO_RCVBUF, &f, sizeof f);
130896c9bb21SBrian Somers   if ((f = recvmsg(s, &msg, MSG_WAITALL)) != expect) {
130996c9bb21SBrian Somers     if (f == -1)
131096c9bb21SBrian Somers       log_Printf(LogERROR, "Failed recvmsg: %s\n", strerror(errno));
131196c9bb21SBrian Somers     else
131296c9bb21SBrian Somers       log_Printf(LogERROR, "Failed recvmsg: Got %d, not %d\n", f, expect);
131396c9bb21SBrian Somers     while (niov--)
131496c9bb21SBrian Somers       free(iov[niov].iov_base);
131554cd8e13SBrian Somers     close(s);
131696c9bb21SBrian Somers     return;
131796c9bb21SBrian Somers   }
131896c9bb21SBrian Somers 
1319c0d9a877SBrian Somers   write(s, "!", 1);	/* ACK */
1320ad5b0e8bSBrian Somers   close(s);
1321c0d9a877SBrian Somers 
1322ad5b0e8bSBrian Somers   if (cmsg->cmsg_type != SCM_RIGHTS) {
1323ad5b0e8bSBrian Somers     log_Printf(LogERROR, "Recvmsg: no descriptor received !\n");
1324ad5b0e8bSBrian Somers     while (niov--)
1325ad5b0e8bSBrian Somers       free(iov[niov].iov_base);
1326ad5b0e8bSBrian Somers     return;
132754cd8e13SBrian Somers   }
132854cd8e13SBrian Somers 
1329ad5b0e8bSBrian Somers   /* We've successfully received an open file descriptor through our socket */
1330ad5b0e8bSBrian Somers   log_Printf(LogDEBUG, "Receiving device descriptor\n");
1331ad5b0e8bSBrian Somers   link_fd = *(int *)CMSG_DATA(cmsg);
1332ad5b0e8bSBrian Somers 
133396c9bb21SBrian Somers   if (strncmp(Version, iov[0].iov_base, iov[0].iov_len)) {
133496c9bb21SBrian Somers     log_Printf(LogWARN, "Cannot receive datalink, incorrect version"
133596c9bb21SBrian Somers                " (\"%.*s\", not \"%s\")\n", (int)iov[0].iov_len,
13367d81ddf5SBrian Somers                (char *)iov[0].iov_base, Version);
133796c9bb21SBrian Somers     close(link_fd);
133896c9bb21SBrian Somers     while (niov--)
133996c9bb21SBrian Somers       free(iov[niov].iov_base);
134096c9bb21SBrian Somers     return;
134196c9bb21SBrian Somers   }
134296c9bb21SBrian Somers 
134396c9bb21SBrian Somers   niov = 1;
1344b7c5748eSBrian Somers   dl = iov2datalink(bundle, iov, &niov, sizeof iov / sizeof *iov, link_fd);
1345b7c5748eSBrian Somers   if (dl) {
134604eaa58cSBrian Somers     bundle_DatalinkLinkin(bundle, dl);
1347b7c5748eSBrian Somers     datalink_AuthOk(dl);
1348ab2de065SBrian Somers     bundle_CalculateBandwidth(dl->bundle);
134996c9bb21SBrian Somers   } else
135096c9bb21SBrian Somers     close(link_fd);
135196c9bb21SBrian Somers 
135296c9bb21SBrian Somers   free(iov[0].iov_base);
13531fa665f5SBrian Somers }
13541fa665f5SBrian Somers 
13551fa665f5SBrian Somers void
135696c9bb21SBrian Somers bundle_SendDatalink(struct datalink *dl, int s, struct sockaddr_un *sun)
13571fa665f5SBrian Somers {
1358c0d9a877SBrian Somers   char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int)], ack;
135996c9bb21SBrian Somers   struct cmsghdr *cmsg = (struct cmsghdr *)cmsgbuf;
136096c9bb21SBrian Somers   struct msghdr msg;
136196c9bb21SBrian Somers   struct iovec iov[SCATTER_SEGMENTS];
13621384bd27SBrian Somers   int niov, link_fd, f, expect, newsid;
136385fd273aSBrian Somers   pid_t newpid;
13646f384573SBrian Somers 
1365dd7e2610SBrian Somers   log_Printf(LogPHASE, "Transmitting datalink %s\n", dl->name);
13666f384573SBrian Somers 
136704eaa58cSBrian Somers   bundle_LinkClosed(dl->bundle, dl);
13680f2f3eb3SBrian Somers   bundle_DatalinkLinkout(dl->bundle, dl);
1369ea722969SBrian Somers 
137096c9bb21SBrian Somers   /* Build our scatter/gather array */
137196c9bb21SBrian Somers   iov[0].iov_len = strlen(Version) + 1;
137296c9bb21SBrian Somers   iov[0].iov_base = strdup(Version);
137396c9bb21SBrian Somers   niov = 1;
13746f384573SBrian Somers 
137585fd273aSBrian Somers   read(s, &newpid, sizeof newpid);
137685fd273aSBrian Somers   link_fd = datalink2iov(dl, iov, &niov, sizeof iov / sizeof *iov, newpid);
13776f384573SBrian Somers 
13786f384573SBrian Somers   if (link_fd != -1) {
137996c9bb21SBrian Somers     memset(&msg, '\0', sizeof msg);
138054cd8e13SBrian Somers 
138196c9bb21SBrian Somers     msg.msg_name = (caddr_t)sun;
138296c9bb21SBrian Somers     msg.msg_namelen = sizeof *sun;
138396c9bb21SBrian Somers     msg.msg_iov = iov;
138496c9bb21SBrian Somers     msg.msg_iovlen = niov;
138554cd8e13SBrian Somers 
138654cd8e13SBrian Somers     cmsg->cmsg_len = sizeof cmsgbuf;
138754cd8e13SBrian Somers     cmsg->cmsg_level = SOL_SOCKET;
138854cd8e13SBrian Somers     cmsg->cmsg_type = SCM_RIGHTS;
138954cd8e13SBrian Somers     *(int *)CMSG_DATA(cmsg) = link_fd;
139096c9bb21SBrian Somers     msg.msg_control = cmsgbuf;
139196c9bb21SBrian Somers     msg.msg_controllen = sizeof cmsgbuf;
139247723d29SBrian Somers 
139396c9bb21SBrian Somers     for (f = expect = 0; f < niov; f++)
139496c9bb21SBrian Somers       expect += iov[f].iov_len;
139547723d29SBrian Somers 
139696c9bb21SBrian Somers     log_Printf(LogDEBUG, "Sending %d bytes in scatter/gather array\n", expect);
139747723d29SBrian Somers 
139896c9bb21SBrian Somers     f = expect + SOCKET_OVERHEAD;
139996c9bb21SBrian Somers     setsockopt(s, SOL_SOCKET, SO_SNDBUF, &f, sizeof f);
140096c9bb21SBrian Somers     if (sendmsg(s, &msg, 0) == -1)
140196c9bb21SBrian Somers       log_Printf(LogERROR, "Failed sendmsg: %s\n", strerror(errno));
1402c0d9a877SBrian Somers     /* We must get the ACK before closing the descriptor ! */
1403c0d9a877SBrian Somers     read(s, &ack, 1);
140454cd8e13SBrian Somers 
1405ac685e31SBrian Somers     newsid = Enabled(dl->bundle, OPT_KEEPSESSION) ||
1406ac685e31SBrian Somers              tcgetpgrp(link_fd) == getpgrp();
140796c9bb21SBrian Somers     close(link_fd);
14081384bd27SBrian Somers     if (newsid)
14091384bd27SBrian Somers       bundle_setsid(dl->bundle, 1);
141047723d29SBrian Somers   }
141185fd273aSBrian Somers   close(s);
141296c9bb21SBrian Somers 
141396c9bb21SBrian Somers   while (niov--)
141496c9bb21SBrian Somers     free(iov[niov].iov_base);
14151fa665f5SBrian Somers }
1416dd0645c5SBrian Somers 
1417dd0645c5SBrian Somers int
141858d55334SBrian Somers bundle_RenameDatalink(struct bundle *bundle, struct datalink *ndl,
141958d55334SBrian Somers                       const char *name)
142058d55334SBrian Somers {
142158d55334SBrian Somers   struct datalink *dl;
142258d55334SBrian Somers 
142358d55334SBrian Somers   if (!strcasecmp(ndl->name, name))
142458d55334SBrian Somers     return 1;
142558d55334SBrian Somers 
142658d55334SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
142758d55334SBrian Somers     if (!strcasecmp(dl->name, name))
142858d55334SBrian Somers       return 0;
142958d55334SBrian Somers 
143058d55334SBrian Somers   datalink_Rename(ndl, name);
143158d55334SBrian Somers   return 1;
143258d55334SBrian Somers }
143358d55334SBrian Somers 
143458d55334SBrian Somers int
1435dd0645c5SBrian Somers bundle_SetMode(struct bundle *bundle, struct datalink *dl, int mode)
1436dd0645c5SBrian Somers {
1437dd0645c5SBrian Somers   int omode;
1438dd0645c5SBrian Somers 
1439dd0645c5SBrian Somers   omode = dl->physical->type;
1440dd0645c5SBrian Somers   if (omode == mode)
1441dd0645c5SBrian Somers     return 1;
1442dd0645c5SBrian Somers 
1443ff0f9439SBrian Somers   if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO))
1444ff0f9439SBrian Somers     /* First auto link */
1445dd0645c5SBrian Somers     if (bundle->ncp.ipcp.peer_ip.s_addr == INADDR_ANY) {
1446ff0f9439SBrian Somers       log_Printf(LogWARN, "You must `set ifaddr' or `open' before"
1447ff0f9439SBrian Somers                  " changing mode to %s\n", mode2Nam(mode));
1448dd0645c5SBrian Somers       return 0;
1449dd0645c5SBrian Somers     }
1450dd0645c5SBrian Somers 
1451dd0645c5SBrian Somers   if (!datalink_SetMode(dl, mode))
1452dd0645c5SBrian Somers     return 0;
1453dd0645c5SBrian Somers 
1454ff0f9439SBrian Somers   if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO) &&
1455ff0f9439SBrian Somers       bundle->phase != PHASE_NETWORK)
1456ff0f9439SBrian Somers     /* First auto link, we need an interface */
1457dd0645c5SBrian Somers     ipcp_InterfaceUp(&bundle->ncp.ipcp);
1458dd0645c5SBrian Somers 
1459ab2de065SBrian Somers   /* Regenerate phys_type and adjust idle timer */
146004eaa58cSBrian Somers   bundle_LinksRemoved(bundle);
1461dd0645c5SBrian Somers 
1462dd0645c5SBrian Somers   return 1;
1463dd0645c5SBrian Somers }
14641384bd27SBrian Somers 
14651384bd27SBrian Somers void
14661384bd27SBrian Somers bundle_setsid(struct bundle *bundle, int holdsession)
14671384bd27SBrian Somers {
14681384bd27SBrian Somers   /*
14691384bd27SBrian Somers    * Lose the current session.  This means getting rid of our pid
14701384bd27SBrian Somers    * too so that the tty device will really go away, and any getty
14711384bd27SBrian Somers    * etc will be allowed to restart.
14721384bd27SBrian Somers    */
14731384bd27SBrian Somers   pid_t pid, orig;
14741384bd27SBrian Somers   int fds[2];
14751384bd27SBrian Somers   char done;
14761384bd27SBrian Somers   struct datalink *dl;
14771384bd27SBrian Somers 
14781384bd27SBrian Somers   orig = getpid();
14791384bd27SBrian Somers   if (pipe(fds) == -1) {
14801384bd27SBrian Somers     log_Printf(LogERROR, "pipe: %s\n", strerror(errno));
14811384bd27SBrian Somers     return;
14821384bd27SBrian Somers   }
14831384bd27SBrian Somers   switch ((pid = fork())) {
14841384bd27SBrian Somers     case -1:
14851384bd27SBrian Somers       log_Printf(LogERROR, "fork: %s\n", strerror(errno));
14861384bd27SBrian Somers       close(fds[0]);
14871384bd27SBrian Somers       close(fds[1]);
14881384bd27SBrian Somers       return;
14891384bd27SBrian Somers     case 0:
14901384bd27SBrian Somers       close(fds[1]);
14914be0e57dSBrian Somers       read(fds[0], &done, 1);		/* uu_locks are mine ! */
14924be0e57dSBrian Somers       close(fds[0]);
14931384bd27SBrian Somers       if (pipe(fds) == -1) {
14941384bd27SBrian Somers         log_Printf(LogERROR, "pipe(2): %s\n", strerror(errno));
14951384bd27SBrian Somers         return;
14961384bd27SBrian Somers       }
14971384bd27SBrian Somers       switch ((pid = fork())) {
14981384bd27SBrian Somers         case -1:
1499a33b2ef7SBrian Somers           log_Printf(LogERROR, "fork(2): %s\n", strerror(errno));
15001384bd27SBrian Somers           close(fds[0]);
15011384bd27SBrian Somers           close(fds[1]);
15021384bd27SBrian Somers           return;
15031384bd27SBrian Somers         case 0:
15041384bd27SBrian Somers           close(fds[1]);
15054be0e57dSBrian Somers           bundle_LockTun(bundle);	/* update pid */
15064be0e57dSBrian Somers           read(fds[0], &done, 1);	/* uu_locks are mine ! */
15074be0e57dSBrian Somers           close(fds[0]);
15081384bd27SBrian Somers           setsid();
15091384bd27SBrian Somers           log_Printf(LogPHASE, "%d -> %d: %s session control\n",
15101384bd27SBrian Somers                      (int)orig, (int)getpid(),
15111384bd27SBrian Somers                      holdsession ? "Passed" : "Dropped");
15127e778f13SBrian Somers           timer_InitService(0);		/* Start the Timer Service */
15131384bd27SBrian Somers           break;
15141384bd27SBrian Somers         default:
15154be0e57dSBrian Somers           close(fds[0]);
15165d9e6103SBrian Somers           /* Give away all our physical locks (to the final process) */
15171384bd27SBrian Somers           for (dl = bundle->links; dl; dl = dl->next)
15181384bd27SBrian Somers             if (dl->state != DATALINK_CLOSED)
15195d9e6103SBrian Somers               physical_ChangedPid(dl->physical, pid);
15204be0e57dSBrian Somers           write(fds[1], "!", 1);	/* done */
15214be0e57dSBrian Somers           close(fds[1]);
15221384bd27SBrian Somers           exit(0);
15231384bd27SBrian Somers           break;
15241384bd27SBrian Somers       }
15251384bd27SBrian Somers       break;
15261384bd27SBrian Somers     default:
15274be0e57dSBrian Somers       close(fds[0]);
15285d9e6103SBrian Somers       /* Give away all our physical locks (to the intermediate process) */
15291384bd27SBrian Somers       for (dl = bundle->links; dl; dl = dl->next)
15301384bd27SBrian Somers         if (dl->state != DATALINK_CLOSED)
15315d9e6103SBrian Somers           physical_ChangedPid(dl->physical, pid);
15324be0e57dSBrian Somers       write(fds[1], "!", 1);	/* done */
15334be0e57dSBrian Somers       close(fds[1]);
15341384bd27SBrian Somers       if (holdsession) {
15351384bd27SBrian Somers         int fd, status;
15361384bd27SBrian Somers 
15371384bd27SBrian Somers         timer_TermService();
15381384bd27SBrian Somers         signal(SIGPIPE, SIG_DFL);
15391384bd27SBrian Somers         signal(SIGALRM, SIG_DFL);
15401384bd27SBrian Somers         signal(SIGHUP, SIG_DFL);
15411384bd27SBrian Somers         signal(SIGTERM, SIG_DFL);
15421384bd27SBrian Somers         signal(SIGINT, SIG_DFL);
15431384bd27SBrian Somers         signal(SIGQUIT, SIG_DFL);
15441384bd27SBrian Somers         for (fd = getdtablesize(); fd >= 0; fd--)
15451384bd27SBrian Somers           close(fd);
15461384bd27SBrian Somers         setuid(geteuid());
15471384bd27SBrian Somers         /*
15481384bd27SBrian Somers          * Reap the intermediate process.  As we're not exiting but the
15491384bd27SBrian Somers          * intermediate is, we don't want it to become defunct.
15501384bd27SBrian Somers          */
15511384bd27SBrian Somers         waitpid(pid, &status, 0);
15528e7b8599SBrian Somers         /* Tweak our process arguments.... */
15538e7b8599SBrian Somers         bundle->argv[0] = "session owner";
15548e7b8599SBrian Somers         bundle->argv[1] = NULL;
15551384bd27SBrian Somers         /*
15561384bd27SBrian Somers          * Hang around for a HUP.  This should happen as soon as the
15571384bd27SBrian Somers          * ppp that we passed our ctty descriptor to closes it.
15581384bd27SBrian Somers          * NOTE: If this process dies, the passed descriptor becomes
15591384bd27SBrian Somers          *       invalid and will give a select() error by setting one
15601384bd27SBrian Somers          *       of the error fds, aborting the other ppp.  We don't
15611384bd27SBrian Somers          *       want that to happen !
15621384bd27SBrian Somers          */
15631384bd27SBrian Somers         pause();
15641384bd27SBrian Somers       }
15651384bd27SBrian Somers       exit(0);
15661384bd27SBrian Somers       break;
15671384bd27SBrian Somers   }
15681384bd27SBrian Somers }
15699b5f8ffdSBrian Somers 
15709b5f8ffdSBrian Somers int
15719b5f8ffdSBrian Somers bundle_HighestState(struct bundle *bundle)
15729b5f8ffdSBrian Somers {
15739b5f8ffdSBrian Somers   struct datalink *dl;
15749b5f8ffdSBrian Somers   int result = DATALINK_CLOSED;
15759b5f8ffdSBrian Somers 
15769b5f8ffdSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
15779b5f8ffdSBrian Somers     if (result < dl->state)
15789b5f8ffdSBrian Somers       result = dl->state;
15799b5f8ffdSBrian Somers 
15809b5f8ffdSBrian Somers   return result;
15819b5f8ffdSBrian Somers }
1582991c2a7bSBrian Somers 
1583991c2a7bSBrian Somers int
1584991c2a7bSBrian Somers bundle_Exception(struct bundle *bundle, int fd)
1585991c2a7bSBrian Somers {
1586991c2a7bSBrian Somers   struct datalink *dl;
1587991c2a7bSBrian Somers 
1588991c2a7bSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
1589991c2a7bSBrian Somers     if (dl->physical->fd == fd) {
1590991c2a7bSBrian Somers       datalink_Down(dl, CLOSE_NORMAL);
1591991c2a7bSBrian Somers       return 1;
1592991c2a7bSBrian Somers     }
1593991c2a7bSBrian Somers 
1594991c2a7bSBrian Somers   return 0;
1595991c2a7bSBrian Somers }
15961d1fc017SBrian Somers 
15971d1fc017SBrian Somers void
15981d1fc017SBrian Somers bundle_AdjustFilters(struct bundle *bundle, struct in_addr *my_ip,
15991d1fc017SBrian Somers                      struct in_addr *peer_ip)
16001d1fc017SBrian Somers {
16011d1fc017SBrian Somers   filter_AdjustAddr(&bundle->filter.in, my_ip, peer_ip);
16021d1fc017SBrian Somers   filter_AdjustAddr(&bundle->filter.out, my_ip, peer_ip);
16031d1fc017SBrian Somers   filter_AdjustAddr(&bundle->filter.dial, my_ip, peer_ip);
16041d1fc017SBrian Somers   filter_AdjustAddr(&bundle->filter.alive, my_ip, peer_ip);
16051d1fc017SBrian Somers }
1606ab2de065SBrian Somers 
1607ab2de065SBrian Somers void
1608ab2de065SBrian Somers bundle_CalculateBandwidth(struct bundle *bundle)
1609ab2de065SBrian Somers {
1610ab2de065SBrian Somers   struct datalink *dl;
1611ab2de065SBrian Somers   int mtu, sp;
1612ab2de065SBrian Somers 
1613ab2de065SBrian Somers   bundle->bandwidth = 0;
1614ab2de065SBrian Somers   mtu = 0;
1615ab2de065SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
1616ab2de065SBrian Somers     if (dl->state == DATALINK_OPEN) {
1617ab2de065SBrian Somers       if ((sp = dl->mp.bandwidth) == 0 &&
1618ab2de065SBrian Somers           (sp = physical_GetSpeed(dl->physical)) == 0)
1619ab2de065SBrian Somers         log_Printf(LogDEBUG, "%s: %s: Cannot determine bandwidth\n",
1620ab2de065SBrian Somers                    dl->name, dl->physical->name.full);
1621ab2de065SBrian Somers       else
1622ab2de065SBrian Somers         bundle->bandwidth += sp;
1623ab2de065SBrian Somers       if (!bundle->ncp.mp.active) {
1624ab2de065SBrian Somers         mtu = dl->physical->link.lcp.his_mru;
1625ab2de065SBrian Somers         break;
1626ab2de065SBrian Somers       }
1627ab2de065SBrian Somers     }
1628ab2de065SBrian Somers 
1629ab2de065SBrian Somers   if(bundle->bandwidth == 0)
1630ab2de065SBrian Somers     bundle->bandwidth = 115200;		/* Shrug */
1631ab2de065SBrian Somers 
1632ab2de065SBrian Somers   if (bundle->ncp.mp.active)
1633ab2de065SBrian Somers     mtu = bundle->ncp.mp.peer_mrru;
1634ab2de065SBrian Somers   else if (!mtu)
1635ab2de065SBrian Somers     mtu = 1500;
1636ab2de065SBrian Somers 
1637ab2de065SBrian Somers #ifndef NORADIUS
1638ab2de065SBrian Somers   if (bundle->radius.valid && bundle->radius.mtu && bundle->radius.mtu < mtu) {
1639ab2de065SBrian Somers     log_Printf(LogLCP, "Reducing MTU to radius value %lu\n",
1640ab2de065SBrian Somers                bundle->radius.mtu);
1641ab2de065SBrian Somers     mtu = bundle->radius.mtu;
1642ab2de065SBrian Somers   }
1643ab2de065SBrian Somers #endif
1644ab2de065SBrian Somers 
1645ab2de065SBrian Somers   tun_configure(bundle, mtu);
1646ab2de065SBrian Somers }
1647ab2de065SBrian Somers 
1648ab2de065SBrian Somers void
1649ab2de065SBrian Somers bundle_AutoAdjust(struct bundle *bundle, int percent, int what)
1650ab2de065SBrian Somers {
1651ab2de065SBrian Somers   struct datalink *dl, *choice, *otherlinkup;
1652ab2de065SBrian Somers 
1653ab2de065SBrian Somers   choice = otherlinkup = NULL;
1654ab2de065SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
1655ab2de065SBrian Somers     if (dl->physical->type == PHYS_AUTO) {
1656ab2de065SBrian Somers       if (dl->state == DATALINK_OPEN) {
1657ab2de065SBrian Somers         if (what == AUTO_DOWN) {
1658ab2de065SBrian Somers           if (choice)
1659ab2de065SBrian Somers             otherlinkup = choice;
1660ab2de065SBrian Somers           choice = dl;
1661ab2de065SBrian Somers         }
1662ab2de065SBrian Somers       } else if (dl->state == DATALINK_CLOSED) {
1663ab2de065SBrian Somers         if (what == AUTO_UP) {
1664ab2de065SBrian Somers           choice = dl;
1665ab2de065SBrian Somers           break;
1666ab2de065SBrian Somers         }
1667ab2de065SBrian Somers       } else {
1668ab2de065SBrian Somers         /* An auto link in an intermediate state - forget it for the moment */
1669ab2de065SBrian Somers         choice = NULL;
1670ab2de065SBrian Somers         break;
1671ab2de065SBrian Somers       }
1672ab2de065SBrian Somers     } else if (dl->state == DATALINK_OPEN && what == AUTO_DOWN)
1673ab2de065SBrian Somers       otherlinkup = dl;
1674ab2de065SBrian Somers 
1675ab2de065SBrian Somers   if (choice) {
1676ab2de065SBrian Somers     if (what == AUTO_UP) {
1677ab2de065SBrian Somers       log_Printf(LogPHASE, "%d%% saturation -> Opening link ``%s''\n",
1678ab2de065SBrian Somers                  percent, choice->name);
1679ab2de065SBrian Somers       datalink_Up(choice, 1, 1);
1680ab2de065SBrian Somers       mp_StopAutoloadTimer(&bundle->ncp.mp);
1681ab2de065SBrian Somers     } else if (otherlinkup) {	/* Only bring the second-last link down */
1682ab2de065SBrian Somers       log_Printf(LogPHASE, "%d%% saturation -> Closing link ``%s''\n",
1683ab2de065SBrian Somers                  percent, choice->name);
1684ab2de065SBrian Somers       datalink_Down(choice, CLOSE_NORMAL);
1685ab2de065SBrian Somers       mp_StopAutoloadTimer(&bundle->ncp.mp);
1686ab2de065SBrian Somers     }
1687ab2de065SBrian Somers   }
1688ab2de065SBrian Somers }
1689ab2de065SBrian Somers 
1690ab2de065SBrian Somers int
1691ab2de065SBrian Somers bundle_WantAutoloadTimer(struct bundle *bundle)
1692ab2de065SBrian Somers {
1693ab2de065SBrian Somers   struct datalink *dl;
1694ab2de065SBrian Somers   int autolink, opened;
1695ab2de065SBrian Somers 
1696ab2de065SBrian Somers   if (bundle->phase == PHASE_NETWORK) {
1697ab2de065SBrian Somers     for (autolink = opened = 0, dl = bundle->links; dl; dl = dl->next)
1698ab2de065SBrian Somers       if (dl->physical->type == PHYS_AUTO) {
1699ab2de065SBrian Somers         if (++autolink == 2 || (autolink == 1 && opened))
1700ab2de065SBrian Somers           /* Two auto links or one auto and one open in NETWORK phase */
1701ab2de065SBrian Somers           return 1;
1702ab2de065SBrian Somers       } else if (dl->state == DATALINK_OPEN) {
1703ab2de065SBrian Somers         opened++;
1704ab2de065SBrian Somers         if (autolink)
1705ab2de065SBrian Somers           /* One auto and one open link in NETWORK phase */
1706ab2de065SBrian Somers           return 1;
1707ab2de065SBrian Somers       }
1708ab2de065SBrian Somers   }
1709ab2de065SBrian Somers 
1710ab2de065SBrian Somers   return 0;
1711ab2de065SBrian Somers }
1712