xref: /freebsd/usr.sbin/ppp/bundle.c (revision 0508c09a41d7c9d8efecd4b42c0db72eec1d84d3)
17a6f8720SBrian Somers /*-
27a6f8720SBrian Somers  * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
37a6f8720SBrian Somers  * All rights reserved.
47a6f8720SBrian Somers  *
57a6f8720SBrian Somers  * Redistribution and use in source and binary forms, with or without
67a6f8720SBrian Somers  * modification, are permitted provided that the following conditions
77a6f8720SBrian Somers  * are met:
87a6f8720SBrian Somers  * 1. Redistributions of source code must retain the above copyright
97a6f8720SBrian Somers  *    notice, this list of conditions and the following disclaimer.
107a6f8720SBrian Somers  * 2. Redistributions in binary form must reproduce the above copyright
117a6f8720SBrian Somers  *    notice, this list of conditions and the following disclaimer in the
127a6f8720SBrian Somers  *    documentation and/or other materials provided with the distribution.
137a6f8720SBrian Somers  *
147a6f8720SBrian Somers  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
157a6f8720SBrian Somers  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
167a6f8720SBrian Somers  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
177a6f8720SBrian Somers  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
187a6f8720SBrian Somers  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
197a6f8720SBrian Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
207a6f8720SBrian Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
217a6f8720SBrian Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
227a6f8720SBrian Somers  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
237a6f8720SBrian Somers  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
247a6f8720SBrian Somers  * SUCH DAMAGE.
257a6f8720SBrian Somers  *
2697d92980SPeter Wemm  * $FreeBSD$
277a6f8720SBrian Somers  */
287a6f8720SBrian Somers 
291384bd27SBrian Somers #include <sys/param.h>
307a6f8720SBrian Somers #include <sys/socket.h>
317a6f8720SBrian Somers #include <netinet/in.h>
327a6f8720SBrian Somers #include <net/if.h>
333a7b6d76SBrian Somers #include <net/if_tun.h>		/* For TUNS* ioctls */
347a6f8720SBrian Somers #include <net/route.h>
35eaa4df37SBrian Somers #include <netinet/in_systm.h>
36eaa4df37SBrian Somers #include <netinet/ip.h>
371fa665f5SBrian Somers #include <sys/un.h>
387a6f8720SBrian Somers 
397a6f8720SBrian Somers #include <errno.h>
407a6f8720SBrian Somers #include <fcntl.h>
41cf0a3940SBrian Somers #ifdef __OpenBSD__
42cf0a3940SBrian Somers #include <util.h>
43cf0a3940SBrian Somers #else
44cf0a3940SBrian Somers #include <libutil.h>
45cf0a3940SBrian Somers #endif
4647723d29SBrian Somers #include <paths.h>
476eafd353SBrian Somers #include <stdarg.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>
537a6f8720SBrian Somers #include <termios.h>
547a6f8720SBrian Somers #include <unistd.h>
557a6f8720SBrian Somers 
565d9e6103SBrian Somers #include "layer.h"
57c9e11a11SBrian Somers #include "defs.h"
587a6f8720SBrian Somers #include "command.h"
597a6f8720SBrian Somers #include "mbuf.h"
607a6f8720SBrian Somers #include "log.h"
617a6f8720SBrian Somers #include "id.h"
627a6f8720SBrian Somers #include "timer.h"
637a6f8720SBrian Somers #include "fsm.h"
647a6f8720SBrian Somers #include "iplist.h"
65879ed6faSBrian Somers #include "lqr.h"
66455aabc3SBrian Somers #include "hdlc.h"
677a6f8720SBrian Somers #include "throughput.h"
68eaa4df37SBrian Somers #include "slcompress.h"
6930949fd4SBrian Somers #include "ncpaddr.h"
7030949fd4SBrian Somers #include "ip.h"
717a6f8720SBrian Somers #include "ipcp.h"
725ca5389aSBrian Somers #include "filter.h"
732f786681SBrian Somers #include "descriptor.h"
747a6f8720SBrian Somers #include "route.h"
757a6f8720SBrian Somers #include "lcp.h"
767a6f8720SBrian Somers #include "ccp.h"
773b0f8d2eSBrian Somers #include "link.h"
783b0f8d2eSBrian Somers #include "mp.h"
79972a1bcfSBrian Somers #ifndef NORADIUS
80972a1bcfSBrian Somers #include "radius.h"
81972a1bcfSBrian Somers #endif
8230949fd4SBrian Somers #include "ipv6cp.h"
8330949fd4SBrian Somers #include "ncp.h"
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"
958fa6ebe4SBrian Somers #include "iface.h"
9674457d3dSBrian Somers #include "server.h"
97971abb29SBrian Somers #include "probe.h"
98fb11a9c2SBrian Somers #ifndef NODES
99019d32bfSBrian Somers #include "mppe.h"
10003a2501aSBrian Somers #endif
1017a6f8720SBrian Somers 
10291cbd2eeSBrian Somers #define SCATTER_SEGMENTS 7  /* version, datalink, name, physical,
10391cbd2eeSBrian Somers                                throughput, throughput, device       */
10487c3786eSBrian Somers 
1052cb305afSBrian Somers #define SEND_MAXFD 3        /* Max file descriptors passed through
10687c3786eSBrian Somers                                the local domain socket              */
10796c9bb21SBrian Somers 
10804eaa58cSBrian Somers static int bundle_RemainingIdleTime(struct bundle *);
10904eaa58cSBrian Somers 
110182c898aSBrian Somers static const char * const PhaseNames[] = {
111455aabc3SBrian Somers   "Dead", "Establish", "Authenticate", "Network", "Terminate"
112455aabc3SBrian Somers };
113455aabc3SBrian Somers 
114455aabc3SBrian Somers const char *
115455aabc3SBrian Somers bundle_PhaseName(struct bundle *bundle)
1167a6f8720SBrian Somers {
117455aabc3SBrian Somers   return bundle->phase <= PHASE_TERMINATE ?
118455aabc3SBrian Somers     PhaseNames[bundle->phase] : "unknown";
1197a6f8720SBrian Somers }
1207a6f8720SBrian Somers 
121455aabc3SBrian Somers void
1225563ebdeSBrian Somers bundle_NewPhase(struct bundle *bundle, u_int new)
123455aabc3SBrian Somers {
124aef795ccSBrian Somers   if (new == bundle->phase)
125aef795ccSBrian Somers     return;
126aef795ccSBrian Somers 
127e2ebb036SBrian Somers   if (new <= PHASE_TERMINATE)
128dd7e2610SBrian Somers     log_Printf(LogPHASE, "bundle: %s\n", PhaseNames[new]);
1297a6f8720SBrian Somers 
130455aabc3SBrian Somers   switch (new) {
131455aabc3SBrian Somers   case PHASE_DEAD:
132455aabc3SBrian Somers     bundle->phase = new;
133fb11a9c2SBrian Somers #ifndef NODES
134019d32bfSBrian Somers     MPPE_MasterKeyValid = 0;
13564602637SBrian Somers #endif
136019d32bfSBrian Somers     log_DisplayPrompts();
137455aabc3SBrian Somers     break;
138455aabc3SBrian Somers 
139455aabc3SBrian Somers   case PHASE_ESTABLISH:
140455aabc3SBrian Somers     bundle->phase = new;
141455aabc3SBrian Somers     break;
142455aabc3SBrian Somers 
143455aabc3SBrian Somers   case PHASE_AUTHENTICATE:
144455aabc3SBrian Somers     bundle->phase = new;
1450f2f3eb3SBrian Somers     log_DisplayPrompts();
146455aabc3SBrian Somers     break;
147455aabc3SBrian Somers 
148455aabc3SBrian Somers   case PHASE_NETWORK:
14930949fd4SBrian Somers     if (ncp_fsmStart(&bundle->ncp, bundle)) {
150673903ecSBrian Somers       bundle->phase = new;
1510f2f3eb3SBrian Somers       log_DisplayPrompts();
15230949fd4SBrian Somers     } else {
15330949fd4SBrian Somers       log_Printf(LogPHASE, "bundle: All NCPs are disabled\n");
15430949fd4SBrian Somers       bundle_Close(bundle, NULL, CLOSE_STAYDOWN);
15530949fd4SBrian Somers     }
156673903ecSBrian Somers     break;
157455aabc3SBrian Somers 
158455aabc3SBrian Somers   case PHASE_TERMINATE:
159455aabc3SBrian Somers     bundle->phase = new;
160673903ecSBrian Somers     mp_Down(&bundle->ncp.mp);
1610f2f3eb3SBrian Somers     log_DisplayPrompts();
162455aabc3SBrian Somers     break;
1637a6f8720SBrian Somers   }
1647a6f8720SBrian Somers }
1657a6f8720SBrian Somers 
1666d666775SBrian Somers static void
167057f1760SBrian Somers bundle_LayerStart(void *v __unused, struct fsm *fp __unused)
1687a6f8720SBrian Somers {
1693006ec67SBrian Somers   /* The given FSM is about to start up ! */
1707a6f8720SBrian Somers }
1717a6f8720SBrian Somers 
1725cf4388bSBrian Somers 
173b42135deSBrian Somers void
1745cf4388bSBrian Somers bundle_Notify(struct bundle *bundle, char c)
1755cf4388bSBrian Somers {
1765cf4388bSBrian Somers   if (bundle->notify.fd != -1) {
177b42135deSBrian Somers     int ret;
178b42135deSBrian Somers 
179b42135deSBrian Somers     ret = write(bundle->notify.fd, &c, 1);
180b42135deSBrian Somers     if (c != EX_REDIAL && c != EX_RECONNECT) {
181b42135deSBrian Somers       if (ret == 1)
182b42135deSBrian Somers         log_Printf(LogCHAT, "Parent notified of %s\n",
1835a83ad1eSBrian Somers                    c == EX_NORMAL ? "success" : "failure");
1845cf4388bSBrian Somers       else
185b42135deSBrian Somers         log_Printf(LogERROR, "Failed to notify parent of success\n");
1865cf4388bSBrian Somers       close(bundle->notify.fd);
1875cf4388bSBrian Somers       bundle->notify.fd = -1;
188b42135deSBrian Somers     } else if (ret == 1)
189b42135deSBrian Somers       log_Printf(LogCHAT, "Parent notified of %s\n", ex_desc(c));
190b42135deSBrian Somers     else
191b42135deSBrian Somers       log_Printf(LogERROR, "Failed to notify parent of %s\n", ex_desc(c));
1925cf4388bSBrian Somers   }
1935cf4388bSBrian Somers }
1943b0f8d2eSBrian Somers 
1956d666775SBrian Somers static void
1966f8e9f0aSBrian Somers bundle_ClearQueues(void *v)
1976f8e9f0aSBrian Somers {
1986f8e9f0aSBrian Somers   struct bundle *bundle = (struct bundle *)v;
1996f8e9f0aSBrian Somers   struct datalink *dl;
2006f8e9f0aSBrian Somers 
2016f8e9f0aSBrian Somers   log_Printf(LogPHASE, "Clearing choked output queue\n");
2026f8e9f0aSBrian Somers   timer_Stop(&bundle->choked.timer);
2036f8e9f0aSBrian Somers 
2046f8e9f0aSBrian Somers   /*
2056f8e9f0aSBrian Somers    * Emergency time:
2066f8e9f0aSBrian Somers    *
2076f8e9f0aSBrian Somers    * We've had a full queue for PACKET_DEL_SECS seconds without being
2086f8e9f0aSBrian Somers    * able to get rid of any of the packets.  We've probably given up
2096f8e9f0aSBrian Somers    * on the redials at this point, and the queued data has almost
2106f8e9f0aSBrian Somers    * definitely been timed out by the layer above.  As this is preventing
2116f8e9f0aSBrian Somers    * us from reading the TUN_NAME device (we don't want to buffer stuff
2126f8e9f0aSBrian Somers    * indefinitely), we may as well nuke this data and start with a clean
2136f8e9f0aSBrian Somers    * slate !
2146f8e9f0aSBrian Somers    *
2156f8e9f0aSBrian Somers    * Unfortunately, this has the side effect of shafting any compression
2166f8e9f0aSBrian Somers    * dictionaries in use (causing the relevant RESET_REQ/RESET_ACK).
2176f8e9f0aSBrian Somers    */
2186f8e9f0aSBrian Somers 
21930949fd4SBrian Somers   ncp_DeleteQueues(&bundle->ncp);
2206f8e9f0aSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
2216f8e9f0aSBrian Somers     physical_DeleteQueue(dl->physical);
2226f8e9f0aSBrian Somers }
2236f8e9f0aSBrian Somers 
2246f8e9f0aSBrian Somers static void
225ff0f9439SBrian Somers bundle_LinkAdded(struct bundle *bundle, struct datalink *dl)
226ff0f9439SBrian Somers {
227ff0f9439SBrian Somers   bundle->phys_type.all |= dl->physical->type;
228ff0f9439SBrian Somers   if (dl->state == DATALINK_OPEN)
229ff0f9439SBrian Somers     bundle->phys_type.open |= dl->physical->type;
230ff0f9439SBrian Somers 
231bf1eaec5SBrian Somers #ifndef NORADIUS
232bf1eaec5SBrian Somers   if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL))
233bf1eaec5SBrian Somers       != bundle->phys_type.open && bundle->session.timer.state == TIMER_STOPPED)
234bf1eaec5SBrian Somers     if (bundle->radius.sessiontime)
235bf1eaec5SBrian Somers       bundle_StartSessionTimer(bundle, 0);
236bf1eaec5SBrian Somers #endif
237bf1eaec5SBrian Somers 
238ff0f9439SBrian Somers   if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL))
239ff0f9439SBrian Somers       != bundle->phys_type.open && bundle->idle.timer.state == TIMER_STOPPED)
240ff0f9439SBrian Somers     /* We may need to start our idle timer */
2410a4b6c5cSBrian Somers     bundle_StartIdleTimer(bundle, 0);
242ff0f9439SBrian Somers }
243ff0f9439SBrian Somers 
24492b09558SBrian Somers void
245ff0f9439SBrian Somers bundle_LinksRemoved(struct bundle *bundle)
246ff0f9439SBrian Somers {
247ff0f9439SBrian Somers   struct datalink *dl;
248ff0f9439SBrian Somers 
249ff0f9439SBrian Somers   bundle->phys_type.all = bundle->phys_type.open = 0;
250ff0f9439SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
251ff0f9439SBrian Somers     bundle_LinkAdded(bundle, dl);
252ff0f9439SBrian Somers 
253ab2de065SBrian Somers   bundle_CalculateBandwidth(bundle);
254ab2de065SBrian Somers   mp_CheckAutoloadTimer(&bundle->ncp.mp);
255ab2de065SBrian Somers 
256ff0f9439SBrian Somers   if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL))
257bf1eaec5SBrian Somers       == bundle->phys_type.open) {
258bf1eaec5SBrian Somers #ifndef NORADIUS
259bf1eaec5SBrian Somers     if (bundle->radius.sessiontime)
260bf1eaec5SBrian Somers       bundle_StopSessionTimer(bundle);
261bf1eaec5SBrian Somers #endif
262ff0f9439SBrian Somers     bundle_StopIdleTimer(bundle);
263ff0f9439SBrian Somers    }
264bf1eaec5SBrian Somers }
26504eaa58cSBrian Somers 
26604eaa58cSBrian Somers static void
2676f384573SBrian Somers bundle_LayerUp(void *v, struct fsm *fp)
2687a6f8720SBrian Somers {
2693006ec67SBrian Somers   /*
2703006ec67SBrian Somers    * The given fsm is now up
271ab2de065SBrian Somers    * If it's an LCP, adjust our phys_mode.open value and check the
272ab2de065SBrian Somers    * autoload timer.
273ab2de065SBrian Somers    * If it's the first NCP, calculate our bandwidth
274dade2407SBrian Somers    * If it's the first NCP, set our ``upat'' time
2753b0f8d2eSBrian Somers    * If it's the first NCP, start the idle timer.
276ab2de065SBrian Somers    * If it's an NCP, tell our -background parent to go away.
277ab2de065SBrian Somers    * If it's the first NCP, start the autoload timer
2783006ec67SBrian Somers    */
2796f384573SBrian Somers   struct bundle *bundle = (struct bundle *)v;
2806d666775SBrian Somers 
2815563ebdeSBrian Somers   if (fp->proto == PROTO_LCP) {
282ff0f9439SBrian Somers     struct physical *p = link2physical(fp->link);
283ff0f9439SBrian Somers 
284ff0f9439SBrian Somers     bundle_LinkAdded(bundle, p->dl);
285ab2de065SBrian Somers     mp_CheckAutoloadTimer(&bundle->ncp.mp);
28630949fd4SBrian Somers   } else if (isncp(fp->proto)) {
28730949fd4SBrian Somers     if (ncp_LayersOpen(&fp->bundle->ncp) == 1) {
288ab2de065SBrian Somers       bundle_CalculateBandwidth(fp->bundle);
289dade2407SBrian Somers       time(&bundle->upat);
290bf1eaec5SBrian Somers #ifndef NORADIUS
291bf1eaec5SBrian Somers       if (bundle->radius.sessiontime)
292bf1eaec5SBrian Somers         bundle_StartSessionTimer(bundle, 0);
293bf1eaec5SBrian Somers #endif
2940a4b6c5cSBrian Somers       bundle_StartIdleTimer(bundle, 0);
295ab2de065SBrian Somers       mp_CheckAutoloadTimer(&fp->bundle->ncp.mp);
29630949fd4SBrian Somers     }
29730949fd4SBrian Somers     bundle_Notify(bundle, EX_NORMAL);
2986301d506SBrian Somers   } else if (fp->proto == PROTO_CCP)
2996301d506SBrian Somers     bundle_CalculateBandwidth(fp->bundle);	/* Against ccp_MTUOverhead */
300ab886ad0SBrian Somers }
3017a6f8720SBrian Somers 
3026d666775SBrian Somers static void
3036d666775SBrian Somers bundle_LayerDown(void *v, struct fsm *fp)
3046d666775SBrian Somers {
3056d666775SBrian Somers   /*
3066d666775SBrian Somers    * The given FSM has been told to come down.
307ab886ad0SBrian Somers    * If it's our last NCP, stop the idle timer.
308dade2407SBrian Somers    * If it's our last NCP, clear our ``upat'' value.
309ab2de065SBrian Somers    * If it's our last NCP, stop the autoload timer
310ff0f9439SBrian Somers    * If it's an LCP, adjust our phys_type.open value and any timers.
31186b1f0d7SBrian Somers    * If it's an LCP and we're in multilink mode, adjust our tun
312c8f30703SBrian Somers    * If it's the last LCP, down all NCPs
31386b1f0d7SBrian Somers    * speed and make sure our minimum sequence number is adjusted.
3146d666775SBrian Somers    */
315ab886ad0SBrian Somers 
316ab886ad0SBrian Somers   struct bundle *bundle = (struct bundle *)v;
317ab886ad0SBrian Somers 
31830949fd4SBrian Somers   if (isncp(fp->proto)) {
31930949fd4SBrian Somers     if (ncp_LayersOpen(&fp->bundle->ncp) == 0) {
320bf1eaec5SBrian Somers #ifndef NORADIUS
321bf1eaec5SBrian Somers       if (bundle->radius.sessiontime)
322bf1eaec5SBrian Somers         bundle_StopSessionTimer(bundle);
323bf1eaec5SBrian Somers #endif
324ab886ad0SBrian Somers       bundle_StopIdleTimer(bundle);
325dade2407SBrian Somers       bundle->upat = 0;
326ab2de065SBrian Somers       mp_StopAutoloadTimer(&bundle->ncp.mp);
32730949fd4SBrian Somers     }
328ab2de065SBrian Somers   } else if (fp->proto == PROTO_LCP) {
3293b0f8d2eSBrian Somers     struct datalink *dl;
33086b1f0d7SBrian Somers     struct datalink *lost;
331c8f30703SBrian Somers     int others_active;
332c8f30703SBrian Somers 
333c8f30703SBrian Somers     bundle_LinksRemoved(bundle);  /* adjust timers & phys_type values */
3343b0f8d2eSBrian Somers 
33586b1f0d7SBrian Somers     lost = NULL;
336c8f30703SBrian Somers     others_active = 0;
337c8f30703SBrian Somers     for (dl = bundle->links; dl; dl = dl->next) {
33886b1f0d7SBrian Somers       if (fp == &dl->physical->link.lcp.fsm)
33986b1f0d7SBrian Somers         lost = dl;
340c8f30703SBrian Somers       else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP)
341c8f30703SBrian Somers         others_active++;
342c8f30703SBrian Somers     }
34386b1f0d7SBrian Somers 
344c8f30703SBrian Somers     if (bundle->ncp.mp.active) {
345ab2de065SBrian Somers       bundle_CalculateBandwidth(bundle);
34686b1f0d7SBrian Somers 
34786b1f0d7SBrian Somers       if (lost)
34886b1f0d7SBrian Somers         mp_LinkLost(&bundle->ncp.mp, lost);
34986b1f0d7SBrian Somers       else
350a33b2ef7SBrian Somers         log_Printf(LogALERT, "Oops, lost an unrecognised datalink (%s) !\n",
35186b1f0d7SBrian Somers                    fp->link->name);
3523b0f8d2eSBrian Somers     }
353c8f30703SBrian Somers 
354356bf92dSBrian Somers     if (!others_active) {
355c8f30703SBrian Somers       /* Down the NCPs.  We don't expect to get fsm_Close()d ourself ! */
35630949fd4SBrian Somers       ncp2initial(&bundle->ncp);
357356bf92dSBrian Somers       mp_Down(&bundle->ncp.mp);
358356bf92dSBrian Somers     }
3596d666775SBrian Somers   }
360ff0f9439SBrian Somers }
3616d666775SBrian Somers 
3626d666775SBrian Somers static void
3636d666775SBrian Somers bundle_LayerFinish(void *v, struct fsm *fp)
3646d666775SBrian Somers {
3656d666775SBrian Somers   /* The given fsm is now down (fp cannot be NULL)
3666d666775SBrian Somers    *
367dd7e2610SBrian Somers    * If it's the last NCP, fsm_Close all LCPs
368356bf92dSBrian Somers    * If it's the last NCP, bring any MP layer down
3696d666775SBrian Somers    */
3706d666775SBrian Somers 
3716d666775SBrian Somers   struct bundle *bundle = (struct bundle *)v;
3726d666775SBrian Somers   struct datalink *dl;
3736d666775SBrian Somers 
37430949fd4SBrian Somers   if (isncp(fp->proto) && !ncp_LayersUnfinished(&bundle->ncp)) {
37526afeaa2SBrian Somers     if (bundle_Phase(bundle) != PHASE_DEAD)
37625092092SBrian Somers       bundle_NewPhase(bundle, PHASE_TERMINATE);
3776d666775SBrian Somers     for (dl = bundle->links; dl; dl = dl->next)
378c8f30703SBrian Somers       if (dl->state == DATALINK_OPEN)
379d4d5d2f8SBrian Somers         datalink_Close(dl, CLOSE_STAYDOWN);
38009206a6fSBrian Somers     fsm2initial(fp);
381356bf92dSBrian Somers     mp_Down(&bundle->ncp.mp);
3823b0f8d2eSBrian Somers   }
3833b0f8d2eSBrian Somers }
3846d666775SBrian Somers 
3857a6f8720SBrian Somers void
3869c81b87dSBrian Somers bundle_Close(struct bundle *bundle, const char *name, int how)
3877a6f8720SBrian Somers {
388455aabc3SBrian Somers   /*
3893006ec67SBrian Somers    * Please close the given datalink.
390dd7e2610SBrian Somers    * If name == NULL or name is the last datalink, fsm_Close all NCPs
3916f384573SBrian Somers    * (except our MP)
3923b0f8d2eSBrian Somers    * If it isn't the last datalink, just Close that datalink.
393455aabc3SBrian Somers    */
3947a6f8720SBrian Somers 
3953b0f8d2eSBrian Somers   struct datalink *dl, *this_dl;
3963b0f8d2eSBrian Somers   int others_active;
3973006ec67SBrian Somers 
3983b0f8d2eSBrian Somers   others_active = 0;
3993b0f8d2eSBrian Somers   this_dl = NULL;
4003b0f8d2eSBrian Somers 
4013b0f8d2eSBrian Somers   for (dl = bundle->links; dl; dl = dl->next) {
4023b0f8d2eSBrian Somers     if (name && !strcasecmp(name, dl->name))
4033b0f8d2eSBrian Somers       this_dl = dl;
4043b0f8d2eSBrian Somers     if (name == NULL || this_dl == dl) {
4059c81b87dSBrian Somers       switch (how) {
4069c81b87dSBrian Somers         case CLOSE_LCP:
4079c81b87dSBrian Somers           datalink_DontHangup(dl);
4082fc2f705SBrian Somers           break;
4099c81b87dSBrian Somers         case CLOSE_STAYDOWN:
4103006ec67SBrian Somers           datalink_StayDown(dl);
4119c81b87dSBrian Somers           break;
4129c81b87dSBrian Somers       }
4133b0f8d2eSBrian Somers     } else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP)
4143b0f8d2eSBrian Somers       others_active++;
4153b0f8d2eSBrian Somers   }
4163b0f8d2eSBrian Somers 
4173b0f8d2eSBrian Somers   if (name && this_dl == NULL) {
418dd7e2610SBrian Somers     log_Printf(LogWARN, "%s: Invalid datalink name\n", name);
4193b0f8d2eSBrian Somers     return;
4203b0f8d2eSBrian Somers   }
4213b0f8d2eSBrian Somers 
4223b0f8d2eSBrian Somers   if (!others_active) {
423bf1eaec5SBrian Somers #ifndef NORADIUS
424bf1eaec5SBrian Somers     if (bundle->radius.sessiontime)
425bf1eaec5SBrian Somers       bundle_StopSessionTimer(bundle);
426bf1eaec5SBrian Somers #endif
42704eaa58cSBrian Somers     bundle_StopIdleTimer(bundle);
42830949fd4SBrian Somers     if (ncp_LayersUnfinished(&bundle->ncp))
42930949fd4SBrian Somers       ncp_Close(&bundle->ncp);
4303b0f8d2eSBrian Somers     else {
43130949fd4SBrian Somers       ncp2initial(&bundle->ncp);
432356bf92dSBrian Somers       mp_Down(&bundle->ncp.mp);
433d345321bSBrian Somers       for (dl = bundle->links; dl; dl = dl->next)
4349c81b87dSBrian Somers         datalink_Close(dl, how);
4357a6f8720SBrian Somers     }
4363b0f8d2eSBrian Somers   } else if (this_dl && this_dl->state != DATALINK_CLOSED &&
4373b0f8d2eSBrian Somers              this_dl->state != DATALINK_HANGUP)
4389c81b87dSBrian Somers     datalink_Close(this_dl, how);
439d2fd8d77SBrian Somers }
4407a6f8720SBrian Somers 
4411bc9b5baSBrian Somers void
442899011c4SBrian Somers bundle_Down(struct bundle *bundle, int how)
4431bc9b5baSBrian Somers {
4441bc9b5baSBrian Somers   struct datalink *dl;
4451bc9b5baSBrian Somers 
4461bc9b5baSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
447899011c4SBrian Somers     datalink_Down(dl, how);
4481bc9b5baSBrian Somers }
4491bc9b5baSBrian Somers 
4502f786681SBrian Somers static int
451f013f33eSBrian Somers bundle_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
4522f786681SBrian Somers {
4532f786681SBrian Somers   struct bundle *bundle = descriptor2bundle(d);
4542f786681SBrian Somers   struct datalink *dl;
45526af0ae9SBrian Somers   int result, nlinks;
4566c1d6731SBrian Somers   u_short ifqueue;
45726af0ae9SBrian Somers   size_t queued;
4582f786681SBrian Somers 
4592f786681SBrian Somers   result = 0;
4602f786681SBrian Somers 
461078c562eSBrian Somers   /* If there are aren't many packets queued, look for some more. */
46204eaa58cSBrian Somers   for (nlinks = 0, dl = bundle->links; dl; dl = dl->next)
46304eaa58cSBrian Somers     nlinks++;
46404eaa58cSBrian Somers 
46504eaa58cSBrian Somers   if (nlinks) {
46630949fd4SBrian Somers     queued = r ? ncp_FillPhysicalQueues(&bundle->ncp, bundle) :
46730949fd4SBrian Somers                  ncp_QueueLen(&bundle->ncp);
46804eaa58cSBrian Somers 
469ff0f9439SBrian Somers     if (r && (bundle->phase == PHASE_NETWORK ||
470ff0f9439SBrian Somers               bundle->phys_type.all & PHYS_AUTO)) {
47104eaa58cSBrian Somers       /* enough surplus so that we can tell if we're getting swamped */
4726c1d6731SBrian Somers       ifqueue = nlinks > bundle->cfg.ifqueue ? nlinks : bundle->cfg.ifqueue;
4736c1d6731SBrian Somers       if (queued < ifqueue) {
47404eaa58cSBrian Somers         /* Not enough - select() for more */
4756f8e9f0aSBrian Somers         if (bundle->choked.timer.state == TIMER_RUNNING)
4766f8e9f0aSBrian Somers           timer_Stop(&bundle->choked.timer);	/* Not needed any more */
47704eaa58cSBrian Somers         FD_SET(bundle->dev.fd, r);
478faefde08SBrian Somers         if (*n < bundle->dev.fd + 1)
479faefde08SBrian Somers           *n = bundle->dev.fd + 1;
4801384bd27SBrian Somers         log_Printf(LogTIMER, "%s: fdset(r) %d\n", TUN_NAME, bundle->dev.fd);
481078c562eSBrian Somers         result++;
4826f8e9f0aSBrian Somers       } else if (bundle->choked.timer.state == TIMER_STOPPED) {
4836f8e9f0aSBrian Somers         bundle->choked.timer.func = bundle_ClearQueues;
4846f8e9f0aSBrian Somers         bundle->choked.timer.name = "output choke";
4856f8e9f0aSBrian Somers         bundle->choked.timer.load = bundle->cfg.choked.timeout * SECTICKS;
4866f8e9f0aSBrian Somers         bundle->choked.timer.arg = bundle;
4876f8e9f0aSBrian Somers         timer_Start(&bundle->choked.timer);
488078c562eSBrian Somers       }
48904eaa58cSBrian Somers     }
49004eaa58cSBrian Somers   }
491078c562eSBrian Somers 
492f0cdd9c0SBrian Somers #ifndef NORADIUS
493f0cdd9c0SBrian Somers   result += descriptor_UpdateSet(&bundle->radius.desc, r, w, e, n);
494f0cdd9c0SBrian Somers #endif
495f0cdd9c0SBrian Somers 
49671555108SBrian Somers   /* Which links need a select() ? */
49771555108SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
49871555108SBrian Somers     result += descriptor_UpdateSet(&dl->desc, r, w, e, n);
49971555108SBrian Somers 
500ea722969SBrian Somers   /*
501ea722969SBrian Somers    * This *MUST* be called after the datalink UpdateSet()s as it
50204eaa58cSBrian Somers    * might be ``holding'' one of the datalinks (death-row) and
5038e7bd08eSBrian Somers    * wants to be able to de-select() it from the descriptor set.
504ea722969SBrian Somers    */
5050f2f3eb3SBrian Somers   result += descriptor_UpdateSet(&bundle->ncp.mp.server.desc, r, w, e, n);
506ea722969SBrian Somers 
5072f786681SBrian Somers   return result;
5082f786681SBrian Somers }
5092f786681SBrian Somers 
5102f786681SBrian Somers static int
511f013f33eSBrian Somers bundle_IsSet(struct fdescriptor *d, const fd_set *fdset)
5122f786681SBrian Somers {
5132f786681SBrian Somers   struct bundle *bundle = descriptor2bundle(d);
5142f786681SBrian Somers   struct datalink *dl;
5152f786681SBrian Somers 
5162f786681SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
5172f786681SBrian Somers     if (descriptor_IsSet(&dl->desc, fdset))
5182f786681SBrian Somers       return 1;
5192f786681SBrian Somers 
520f0cdd9c0SBrian Somers #ifndef NORADIUS
521f0cdd9c0SBrian Somers   if (descriptor_IsSet(&bundle->radius.desc, fdset))
522f0cdd9c0SBrian Somers     return 1;
523f0cdd9c0SBrian Somers #endif
524f0cdd9c0SBrian Somers 
525332b9de0SBrian Somers   if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset))
526332b9de0SBrian Somers     return 1;
527332b9de0SBrian Somers 
528faefde08SBrian Somers   return FD_ISSET(bundle->dev.fd, fdset);
5292f786681SBrian Somers }
5302f786681SBrian Somers 
5312f786681SBrian Somers static void
532057f1760SBrian Somers bundle_DescriptorRead(struct fdescriptor *d __unused, struct bundle *bundle,
5332f786681SBrian Somers                       const fd_set *fdset)
5342f786681SBrian Somers {
5352f786681SBrian Somers   struct datalink *dl;
5360a4b6c5cSBrian Somers   unsigned secs;
53730949fd4SBrian Somers   u_int32_t af;
5382f786681SBrian Somers 
539ea722969SBrian Somers   if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset))
540ea722969SBrian Somers     descriptor_Read(&bundle->ncp.mp.server.desc, bundle, fdset);
541ea722969SBrian Somers 
5422f786681SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
5432f786681SBrian Somers     if (descriptor_IsSet(&dl->desc, fdset))
5442f786681SBrian Somers       descriptor_Read(&dl->desc, bundle, fdset);
545b6217683SBrian Somers 
546f0cdd9c0SBrian Somers #ifndef NORADIUS
547f0cdd9c0SBrian Somers   if (descriptor_IsSet(&bundle->radius.desc, fdset))
548f0cdd9c0SBrian Somers     descriptor_Read(&bundle->radius.desc, bundle, fdset);
549f0cdd9c0SBrian Somers #endif
550f0cdd9c0SBrian Somers 
551faefde08SBrian Somers   if (FD_ISSET(bundle->dev.fd, fdset)) {
552078c562eSBrian Somers     struct tun_data tun;
553078c562eSBrian Somers     int n, pri;
554c1d57c38SBrian Somers     u_char *data;
5553a7b6d76SBrian Somers     size_t sz;
5563a7b6d76SBrian Somers 
5573a7b6d76SBrian Somers     if (bundle->dev.header) {
558c1d57c38SBrian Somers       data = (u_char *)&tun;
5593a7b6d76SBrian Somers       sz = sizeof tun;
5603a7b6d76SBrian Somers     } else {
5613a7b6d76SBrian Somers       data = tun.data;
5623a7b6d76SBrian Somers       sz = sizeof tun.data;
5633a7b6d76SBrian Somers     }
564078c562eSBrian Somers 
565078c562eSBrian Somers     /* something to read from tun */
5663a7b6d76SBrian Somers 
5673a7b6d76SBrian Somers     n = read(bundle->dev.fd, data, sz);
568078c562eSBrian Somers     if (n < 0) {
5693a7b6d76SBrian Somers       log_Printf(LogWARN, "%s: read: %s\n", bundle->dev.Name, strerror(errno));
570078c562eSBrian Somers       return;
571078c562eSBrian Somers     }
5723a7b6d76SBrian Somers 
5733a7b6d76SBrian Somers     if (bundle->dev.header) {
5743a7b6d76SBrian Somers       n -= sz - sizeof tun.data;
575078c562eSBrian Somers       if (n <= 0) {
5763a7b6d76SBrian Somers         log_Printf(LogERROR, "%s: read: Got only %d bytes of data !\n",
5773a7b6d76SBrian Somers                    bundle->dev.Name, n);
578078c562eSBrian Somers         return;
579078c562eSBrian Somers       }
58030949fd4SBrian Somers       af = ntohl(tun.header.family);
58130949fd4SBrian Somers #ifndef NOINET6
5821136c6acSBrian Somers       if (af != AF_INET && af != AF_INET6)
58330949fd4SBrian Somers #else
58430949fd4SBrian Somers       if (af != AF_INET)
58530949fd4SBrian Somers #endif
5863a7b6d76SBrian Somers         /* XXX: Should be maintaining drop/family counts ! */
587078c562eSBrian Somers         return;
58830949fd4SBrian Somers     } else
58930949fd4SBrian Somers       af = AF_INET;
590078c562eSBrian Somers 
59130949fd4SBrian Somers     if (af == AF_INET && ((struct ip *)tun.data)->ip_dst.s_addr ==
592078c562eSBrian Somers         bundle->ncp.ipcp.my_ip.s_addr) {
593078c562eSBrian Somers       /* we've been asked to send something addressed *to* us :( */
594078c562eSBrian Somers       if (Enabled(bundle, OPT_LOOPBACK)) {
59530949fd4SBrian Somers         pri = PacketCheck(bundle, af, tun.data, n, &bundle->filter.in,
59630949fd4SBrian Somers                           NULL, NULL);
597078c562eSBrian Somers         if (pri >= 0) {
5983a7b6d76SBrian Somers           n += sz - sizeof tun.data;
5993a7b6d76SBrian Somers           write(bundle->dev.fd, data, n);
600078c562eSBrian Somers           log_Printf(LogDEBUG, "Looped back packet addressed to myself\n");
601078c562eSBrian Somers         }
602078c562eSBrian Somers         return;
603078c562eSBrian Somers       } else
604078c562eSBrian Somers         log_Printf(LogDEBUG, "Oops - forwarding packet addressed to myself\n");
605078c562eSBrian Somers     }
606078c562eSBrian Somers 
607078c562eSBrian Somers     /*
60830949fd4SBrian Somers      * Process on-demand dialup. Output packets are queued within the tunnel
60930949fd4SBrian Somers      * device until the appropriate NCP is opened.
610078c562eSBrian Somers      */
611078c562eSBrian Somers 
612078c562eSBrian Somers     if (bundle_Phase(bundle) == PHASE_DEAD) {
613078c562eSBrian Somers       /*
614078c562eSBrian Somers        * Note, we must be in AUTO mode :-/ otherwise our interface should
615078c562eSBrian Somers        * *not* be UP and we can't receive data
616078c562eSBrian Somers        */
61730949fd4SBrian Somers       pri = PacketCheck(bundle, af, tun.data, n, &bundle->filter.dial,
61830949fd4SBrian Somers                         NULL, NULL);
619040cfe28SBrian Somers       if (pri >= 0)
620ba23f397SBrian Somers         bundle_Open(bundle, NULL, PHYS_AUTO, 0);
621078c562eSBrian Somers       else
622078c562eSBrian Somers         /*
623078c562eSBrian Somers          * Drop the packet.  If we were to queue it, we'd just end up with
624078c562eSBrian Somers          * a pile of timed-out data in our output queue by the time we get
625078c562eSBrian Somers          * around to actually dialing.  We'd also prematurely reach the
626078c562eSBrian Somers          * threshold at which we stop select()ing to read() the tun
627078c562eSBrian Somers          * device - breaking auto-dial.
628078c562eSBrian Somers          */
629078c562eSBrian Somers         return;
630078c562eSBrian Somers     }
631078c562eSBrian Somers 
6320a4b6c5cSBrian Somers     secs = 0;
63330949fd4SBrian Somers     pri = PacketCheck(bundle, af, tun.data, n, &bundle->filter.out,
63430949fd4SBrian Somers                       NULL, &secs);
6350a4b6c5cSBrian Somers     if (pri >= 0) {
6360a4b6c5cSBrian Somers       /* Prepend the number of seconds timeout given in the filter */
6370a4b6c5cSBrian Somers       tun.header.timeout = secs;
63830949fd4SBrian Somers       ncp_Enqueue(&bundle->ncp, af, pri, (char *)&tun, n + sizeof tun.header);
6390a4b6c5cSBrian Somers     }
640078c562eSBrian Somers   }
641078c562eSBrian Somers }
6422f786681SBrian Somers 
6431af29a6eSBrian Somers static int
644057f1760SBrian Somers bundle_DescriptorWrite(struct fdescriptor *d __unused, struct bundle *bundle,
6452f786681SBrian Somers                        const fd_set *fdset)
6462f786681SBrian Somers {
6472f786681SBrian Somers   struct datalink *dl;
6481af29a6eSBrian Somers   int result = 0;
6492f786681SBrian Somers 
650ea722969SBrian Somers   /* This is not actually necessary as struct mpserver doesn't Write() */
651ea722969SBrian Somers   if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset))
652fb11a9c2SBrian Somers     if (descriptor_Write(&bundle->ncp.mp.server.desc, bundle, fdset) == 1)
653fb11a9c2SBrian Somers       result++;
654ea722969SBrian Somers 
6552f786681SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
6562f786681SBrian Somers     if (descriptor_IsSet(&dl->desc, fdset))
657fb11a9c2SBrian Somers       switch (descriptor_Write(&dl->desc, bundle, fdset)) {
658fb11a9c2SBrian Somers       case -1:
659fb11a9c2SBrian Somers         datalink_ComeDown(dl, CLOSE_NORMAL);
660fb11a9c2SBrian Somers         break;
661fb11a9c2SBrian Somers       case 1:
662fb11a9c2SBrian Somers         result++;
663fb11a9c2SBrian Somers       }
6641af29a6eSBrian Somers 
6651af29a6eSBrian Somers   return result;
6662f786681SBrian Somers }
6672f786681SBrian Somers 
668da66dd13SBrian Somers void
6691384bd27SBrian Somers bundle_LockTun(struct bundle *bundle)
6701384bd27SBrian Somers {
6711384bd27SBrian Somers   FILE *lockfile;
67252847614SBrian Somers   char pidfile[PATH_MAX];
6731384bd27SBrian Somers 
6741384bd27SBrian Somers   snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit);
6751384bd27SBrian Somers   lockfile = ID0fopen(pidfile, "w");
6761384bd27SBrian Somers   if (lockfile != NULL) {
6771384bd27SBrian Somers     fprintf(lockfile, "%d\n", (int)getpid());
6781384bd27SBrian Somers     fclose(lockfile);
6791384bd27SBrian Somers   }
6801384bd27SBrian Somers #ifndef RELEASE_CRUNCH
6811384bd27SBrian Somers   else
6821384bd27SBrian Somers     log_Printf(LogERROR, "Warning: Can't create %s: %s\n",
6831384bd27SBrian Somers                pidfile, strerror(errno));
6841384bd27SBrian Somers #endif
6851384bd27SBrian Somers }
6861384bd27SBrian Somers 
6871384bd27SBrian Somers static void
6881384bd27SBrian Somers bundle_UnlockTun(struct bundle *bundle)
6891384bd27SBrian Somers {
69052847614SBrian Somers   char pidfile[PATH_MAX];
6911384bd27SBrian Somers 
6921384bd27SBrian Somers   snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit);
6931384bd27SBrian Somers   ID0unlink(pidfile);
6941384bd27SBrian Somers }
6957a6f8720SBrian Somers 
6967a6f8720SBrian Somers struct bundle *
697cf0a3940SBrian Somers bundle_Create(const char *prefix, int type, int unit)
6987a6f8720SBrian Somers {
6997a6f8720SBrian Somers   static struct bundle bundle;		/* there can be only one */
700c0593e34SBrian Somers   int enoentcount, err, minunit, maxunit;
7014e5196e9SBrian Somers   const char *ifname;
702fdb4bb1bSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
703fc3034caSBrian Somers   int kldtried;
704fc3034caSBrian Somers #endif
7053a7b6d76SBrian Somers #if defined(TUNSIFMODE) || defined(TUNSLMODE) || defined(TUNSIFHEAD)
7060f203c7eSBrian Somers   int iff;
7070f203c7eSBrian Somers #endif
7087a6f8720SBrian Somers 
7098fa6ebe4SBrian Somers   if (bundle.iface != NULL) {	/* Already allocated ! */
710a33b2ef7SBrian Somers     log_Printf(LogALERT, "bundle_Create:  There's only one BUNDLE !\n");
7117a6f8720SBrian Somers     return NULL;
7127a6f8720SBrian Somers   }
7137a6f8720SBrian Somers 
714c0593e34SBrian Somers   if (unit == -1) {
715c0593e34SBrian Somers     minunit = 0;
716c0593e34SBrian Somers     maxunit = -1;
717c0593e34SBrian Somers   } else {
718c0593e34SBrian Somers     minunit = unit;
719c0593e34SBrian Somers     maxunit = unit + 1;
720c0593e34SBrian Somers   }
7217a6f8720SBrian Somers   err = ENOENT;
7227a6f8720SBrian Somers   enoentcount = 0;
723fdb4bb1bSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
724fc3034caSBrian Somers   kldtried = 0;
725fc3034caSBrian Somers #endif
726c0593e34SBrian Somers   for (bundle.unit = minunit; bundle.unit != maxunit; bundle.unit++) {
727faefde08SBrian Somers     snprintf(bundle.dev.Name, sizeof bundle.dev.Name, "%s%d",
728faefde08SBrian Somers              prefix, bundle.unit);
729faefde08SBrian Somers     bundle.dev.fd = ID0open(bundle.dev.Name, O_RDWR);
730faefde08SBrian Somers     if (bundle.dev.fd >= 0)
7317a6f8720SBrian Somers       break;
732728ef5b2SBrian Somers     else if (errno == ENXIO || errno == ENOENT) {
733fdb4bb1bSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
734c0593e34SBrian Somers       if (bundle.unit == minunit && !kldtried++) {
735fc3034caSBrian Somers         /*
736fdb4bb1bSBrian Somers          * Attempt to load the tunnel interface KLD if it isn't loaded
737fdb4bb1bSBrian Somers          * already.
738fc3034caSBrian Somers          */
7395476d2e5SBrian Somers         if (loadmodules(LOAD_VERBOSLY, "if_tun", NULL))
7405476d2e5SBrian Somers           bundle.unit--;
741fc3034caSBrian Somers         continue;
742fc3034caSBrian Somers       }
743fc3034caSBrian Somers #endif
744c4c6616aSBrian Somers       if (errno != ENOENT || ++enoentcount > 2) {
7457a6f8720SBrian Somers         err = errno;
746107d62e7SBrian Somers 	break;
747c4c6616aSBrian Somers       }
7487a6f8720SBrian Somers     } else
7497a6f8720SBrian Somers       err = errno;
7507a6f8720SBrian Somers   }
7517a6f8720SBrian Somers 
752faefde08SBrian Somers   if (bundle.dev.fd < 0) {
753c0593e34SBrian Somers     if (unit == -1)
754c0593e34SBrian Somers       log_Printf(LogWARN, "No available tunnel devices found (%s)\n",
75585b542cfSBrian Somers                 strerror(err));
756c0593e34SBrian Somers     else
757c0593e34SBrian Somers       log_Printf(LogWARN, "%s%d: %s\n", prefix, unit, strerror(err));
7587a6f8720SBrian Somers     return NULL;
7597a6f8720SBrian Somers   }
7607a6f8720SBrian Somers 
761dd7e2610SBrian Somers   log_SetTun(bundle.unit);
7627a6f8720SBrian Somers 
7638fa6ebe4SBrian Somers   ifname = strrchr(bundle.dev.Name, '/');
7648fa6ebe4SBrian Somers   if (ifname == NULL)
7658fa6ebe4SBrian Somers     ifname = bundle.dev.Name;
7667a6f8720SBrian Somers   else
7678fa6ebe4SBrian Somers     ifname++;
7688fa6ebe4SBrian Somers 
7698fa6ebe4SBrian Somers   bundle.iface = iface_Create(ifname);
7708fa6ebe4SBrian Somers   if (bundle.iface == NULL) {
7718fa6ebe4SBrian Somers     close(bundle.dev.fd);
7728fa6ebe4SBrian Somers     return NULL;
7738fa6ebe4SBrian Somers   }
7747a6f8720SBrian Somers 
7750f203c7eSBrian Somers #ifdef TUNSIFMODE
776ebdcbc67SBrian Somers   /* Make sure we're POINTOPOINT & IFF_MULTICAST */
777ebdcbc67SBrian Somers   iff = IFF_POINTOPOINT | IFF_MULTICAST;
7780f203c7eSBrian Somers   if (ID0ioctl(bundle.dev.fd, TUNSIFMODE, &iff) < 0)
7790f203c7eSBrian Somers     log_Printf(LogERROR, "bundle_Create: ioctl(TUNSIFMODE): %s\n",
7800f203c7eSBrian Somers 	       strerror(errno));
7810f203c7eSBrian Somers #endif
7820f203c7eSBrian Somers 
7836ca65df0SBrian Somers #ifdef TUNSLMODE
7843a7b6d76SBrian Somers   /* Make sure we're not prepending sockaddrs */
7856ca65df0SBrian Somers   iff = 0;
7866ca65df0SBrian Somers   if (ID0ioctl(bundle.dev.fd, TUNSLMODE, &iff) < 0)
7876ca65df0SBrian Somers     log_Printf(LogERROR, "bundle_Create: ioctl(TUNSLMODE): %s\n",
7886ca65df0SBrian Somers 	       strerror(errno));
7896ca65df0SBrian Somers #endif
7906ca65df0SBrian Somers 
7913a7b6d76SBrian Somers #ifdef TUNSIFHEAD
7923a7b6d76SBrian Somers   /* We want the address family please ! */
7933a7b6d76SBrian Somers   iff = 1;
7943a7b6d76SBrian Somers   if (ID0ioctl(bundle.dev.fd, TUNSIFHEAD, &iff) < 0) {
7953a7b6d76SBrian Somers     log_Printf(LogERROR, "bundle_Create: ioctl(TUNSIFHEAD): %s\n",
7963a7b6d76SBrian Somers 	       strerror(errno));
7973a7b6d76SBrian Somers     bundle.dev.header = 0;
7983a7b6d76SBrian Somers   } else
7993a7b6d76SBrian Somers     bundle.dev.header = 1;
8003a7b6d76SBrian Somers #else
8013a7b6d76SBrian Somers #ifdef __OpenBSD__
8023a7b6d76SBrian Somers   /* Always present for OpenBSD */
8033a7b6d76SBrian Somers   bundle.dev.header = 1;
8043a7b6d76SBrian Somers #else
8053a7b6d76SBrian Somers   /*
8063a7b6d76SBrian Somers    * If TUNSIFHEAD isn't available and we're not OpenBSD, assume
8073a7b6d76SBrian Somers    * everything's AF_INET (hopefully the tun device won't pass us
8083a7b6d76SBrian Somers    * anything else !).
8093a7b6d76SBrian Somers    */
8103a7b6d76SBrian Somers   bundle.dev.header = 0;
8113a7b6d76SBrian Somers #endif
8123a7b6d76SBrian Somers #endif
8133a7b6d76SBrian Somers 
8148fa6ebe4SBrian Somers   log_Printf(LogPHASE, "Using interface: %s\n", ifname);
8157a6f8720SBrian Somers 
816ab2de065SBrian Somers   bundle.bandwidth = 0;
817820de6ebSBrian Somers   bundle.routing_seq = 0;
818a0cbd833SBrian Somers   bundle.phase = PHASE_DEAD;
819a0cbd833SBrian Somers   bundle.CleaningUp = 0;
82067b072f7SBrian Somers   bundle.NatEnabled = 0;
8217a6f8720SBrian Somers 
8226d666775SBrian Somers   bundle.fsm.LayerStart = bundle_LayerStart;
8236f384573SBrian Somers   bundle.fsm.LayerUp = bundle_LayerUp;
8246d666775SBrian Somers   bundle.fsm.LayerDown = bundle_LayerDown;
8256d666775SBrian Somers   bundle.fsm.LayerFinish = bundle_LayerFinish;
8266d666775SBrian Somers   bundle.fsm.object = &bundle;
8277a6f8720SBrian Somers 
828dade2407SBrian Somers   bundle.cfg.idle.timeout = NCP_IDLE_TIMEOUT;
829dade2407SBrian Somers   bundle.cfg.idle.min_timeout = 0;
8301342caedSBrian Somers   *bundle.cfg.auth.name = '\0';
8311342caedSBrian Somers   *bundle.cfg.auth.key = '\0';
8320508c09aSBrian Somers   bundle.cfg.optmask = (1ull << OPT_IDCHECK) | (1ull << OPT_LOOPBACK) |
8330508c09aSBrian Somers                        (1ull << OPT_SROUTES) | (1ull << OPT_TCPMSSFIXUP) |
8340508c09aSBrian Somers                        (1ull << OPT_THROUGHPUT) | (1ull << OPT_UTMP) |
8350508c09aSBrian Somers                        (1ull << OPT_NAS_IP_ADDRESS) |
8360508c09aSBrian Somers                        (1ull << OPT_NAS_IDENTIFIER);
837971abb29SBrian Somers #ifndef NOINET6
8380508c09aSBrian Somers   opt_enable(&bundle, OPT_IPCP);
839971abb29SBrian Somers   if (probe.ipv6_available)
8400508c09aSBrian Somers     opt_enable(&bundle, OPT_IPV6CP);
841971abb29SBrian Somers #endif
84249052c95SBrian Somers   *bundle.cfg.label = '\0';
8436c1d6731SBrian Somers   bundle.cfg.ifqueue = DEF_IFQUEUE;
8446f8e9f0aSBrian Somers   bundle.cfg.choked.timeout = CHOKED_TIMEOUT;
845ff0f9439SBrian Somers   bundle.phys_type.all = type;
846ff0f9439SBrian Somers   bundle.phys_type.open = 0;
847dade2407SBrian Somers   bundle.upat = 0;
848ab886ad0SBrian Somers 
8496f384573SBrian Somers   bundle.links = datalink_Create("deflink", &bundle, type);
8503006ec67SBrian Somers   if (bundle.links == NULL) {
851a33b2ef7SBrian Somers     log_Printf(LogALERT, "Cannot create data link: %s\n", strerror(errno));
8528fa6ebe4SBrian Somers     iface_Destroy(bundle.iface);
8538fa6ebe4SBrian Somers     bundle.iface = NULL;
854faefde08SBrian Somers     close(bundle.dev.fd);
8552289f246SBrian Somers     return NULL;
8562289f246SBrian Somers   }
8572289f246SBrian Somers 
8582f786681SBrian Somers   bundle.desc.type = BUNDLE_DESCRIPTOR;
8592f786681SBrian Somers   bundle.desc.UpdateSet = bundle_UpdateSet;
8602f786681SBrian Somers   bundle.desc.IsSet = bundle_IsSet;
8612f786681SBrian Somers   bundle.desc.Read = bundle_DescriptorRead;
8622f786681SBrian Somers   bundle.desc.Write = bundle_DescriptorWrite;
8632f786681SBrian Somers 
86430949fd4SBrian Somers   ncp_Init(&bundle.ncp, &bundle);
8656d666775SBrian Somers 
8665ca5389aSBrian Somers   memset(&bundle.filter, '\0', sizeof bundle.filter);
8675ca5389aSBrian Somers   bundle.filter.in.fragok = bundle.filter.in.logok = 1;
8685ca5389aSBrian Somers   bundle.filter.in.name = "IN";
8695ca5389aSBrian Somers   bundle.filter.out.fragok = bundle.filter.out.logok = 1;
8705ca5389aSBrian Somers   bundle.filter.out.name = "OUT";
8715ca5389aSBrian Somers   bundle.filter.dial.name = "DIAL";
8728390b576SBrian Somers   bundle.filter.dial.logok = 1;
8735ca5389aSBrian Somers   bundle.filter.alive.name = "ALIVE";
8745ca5389aSBrian Somers   bundle.filter.alive.logok = 1;
875cad7e742SBrian Somers   {
876cad7e742SBrian Somers     int i;
877cad7e742SBrian Somers     for (i = 0; i < MAXFILTERS; i++) {
878cad7e742SBrian Somers         bundle.filter.in.rule[i].f_action = A_NONE;
879cad7e742SBrian Somers         bundle.filter.out.rule[i].f_action = A_NONE;
880cad7e742SBrian Somers         bundle.filter.dial.rule[i].f_action = A_NONE;
881cad7e742SBrian Somers         bundle.filter.alive.rule[i].f_action = A_NONE;
882cad7e742SBrian Somers     }
883cad7e742SBrian Somers   }
88493ee0ff2SBrian Somers   memset(&bundle.idle.timer, '\0', sizeof bundle.idle.timer);
88593ee0ff2SBrian Somers   bundle.idle.done = 0;
8865cf4388bSBrian Somers   bundle.notify.fd = -1;
8876f8e9f0aSBrian Somers   memset(&bundle.choked.timer, '\0', sizeof bundle.choked.timer);
888972a1bcfSBrian Somers #ifndef NORADIUS
889972a1bcfSBrian Somers   radius_Init(&bundle.radius);
890972a1bcfSBrian Somers #endif
89193ee0ff2SBrian Somers 
8926d666775SBrian Somers   /* Clean out any leftover crud */
89330949fd4SBrian Somers   iface_Clear(bundle.iface, &bundle.ncp, 0, IFACE_CLEAR_ALL);
8946d666775SBrian Somers 
8951384bd27SBrian Somers   bundle_LockTun(&bundle);
8961384bd27SBrian Somers 
8977a6f8720SBrian Somers   return &bundle;
8987a6f8720SBrian Somers }
8997a6f8720SBrian Somers 
90068a0f0ccSBrian Somers static void
90168a0f0ccSBrian Somers bundle_DownInterface(struct bundle *bundle)
90268a0f0ccSBrian Somers {
903dd7e2610SBrian Somers   route_IfDelete(bundle, 1);
904dc744e19SBrian Somers   iface_ClearFlags(bundle->iface->name, IFF_UP);
90568a0f0ccSBrian Somers }
90668a0f0ccSBrian Somers 
90768a0f0ccSBrian Somers void
90868a0f0ccSBrian Somers bundle_Destroy(struct bundle *bundle)
90968a0f0ccSBrian Somers {
9103006ec67SBrian Somers   struct datalink *dl;
911b6217683SBrian Somers 
912ea722969SBrian Somers   /*
91330949fd4SBrian Somers    * Clean up the interface.  We don't really need to do the timer_Stop()s,
91430949fd4SBrian Somers    * mp_Down(), iface_Clear() and bundle_DownInterface() unless we're getting
9158e7bd08eSBrian Somers    * out under exceptional conditions such as a descriptor exception.
916ea722969SBrian Somers    */
91704eaa58cSBrian Somers   timer_Stop(&bundle->idle.timer);
9186f8e9f0aSBrian Somers   timer_Stop(&bundle->choked.timer);
91966f634b6SBrian Somers   mp_Down(&bundle->ncp.mp);
92030949fd4SBrian Somers   iface_Clear(bundle->iface, &bundle->ncp, 0, IFACE_CLEAR_ALL);
92168a0f0ccSBrian Somers   bundle_DownInterface(bundle);
9223006ec67SBrian Somers 
923972a1bcfSBrian Somers #ifndef NORADIUS
924972a1bcfSBrian Somers   /* Tell the radius server the bad news */
925972a1bcfSBrian Somers   radius_Destroy(&bundle->radius);
926972a1bcfSBrian Somers #endif
927972a1bcfSBrian Somers 
928ea722969SBrian Somers   /* Again, these are all DATALINK_CLOSED unless we're abending */
9293006ec67SBrian Somers   dl = bundle->links;
9303006ec67SBrian Somers   while (dl)
9313006ec67SBrian Somers     dl = datalink_Destroy(dl);
9323006ec67SBrian Somers 
93330949fd4SBrian Somers   ncp_Destroy(&bundle->ncp);
934442f8495SBrian Somers 
9351384bd27SBrian Somers   close(bundle->dev.fd);
9361384bd27SBrian Somers   bundle_UnlockTun(bundle);
9371384bd27SBrian Somers 
938ea722969SBrian Somers   /* In case we never made PHASE_NETWORK */
9395cf4388bSBrian Somers   bundle_Notify(bundle, EX_ERRDEAD);
940b6217683SBrian Somers 
9418fa6ebe4SBrian Somers   iface_Destroy(bundle->iface);
9428fa6ebe4SBrian Somers   bundle->iface = NULL;
94368a0f0ccSBrian Somers }
94468a0f0ccSBrian Somers 
94583d1af55SBrian Somers void
9463006ec67SBrian Somers bundle_LinkClosed(struct bundle *bundle, struct datalink *dl)
9473006ec67SBrian Somers {
9483006ec67SBrian Somers   /*
9493006ec67SBrian Somers    * Our datalink has closed.
950ea722969SBrian Somers    * CleanDatalinks() (called from DoLoop()) will remove closed
951f6a4e748SBrian Somers    * BACKGROUND, FOREGROUND and DIRECT links.
9523b0f8d2eSBrian Somers    * If it's the last data link, enter phase DEAD.
953ea722969SBrian Somers    *
954ea722969SBrian Somers    * NOTE: dl may not be in our list (bundle_SendDatalink()) !
9553006ec67SBrian Somers    */
9565b8b8060SBrian Somers 
9573b0f8d2eSBrian Somers   struct datalink *odl;
9583b0f8d2eSBrian Somers   int other_links;
9595b8b8060SBrian Somers 
960bf1d3ff6SBrian Somers   log_SetTtyCommandMode(dl);
961bf1d3ff6SBrian Somers 
9623b0f8d2eSBrian Somers   other_links = 0;
9633b0f8d2eSBrian Somers   for (odl = bundle->links; odl; odl = odl->next)
9643b0f8d2eSBrian Somers     if (odl != dl && odl->state != DATALINK_CLOSED)
9653b0f8d2eSBrian Somers       other_links++;
9663b0f8d2eSBrian Somers 
9673b0f8d2eSBrian Somers   if (!other_links) {
96881358fa3SBrian Somers     if (dl->physical->type != PHYS_AUTO)	/* Not in -auto mode */
96903704096SBrian Somers       bundle_DownInterface(bundle);
97030949fd4SBrian Somers     ncp2initial(&bundle->ncp);
971356bf92dSBrian Somers     mp_Down(&bundle->ncp.mp);
9725563ebdeSBrian Somers     bundle_NewPhase(bundle, PHASE_DEAD);
973bf1eaec5SBrian Somers #ifndef NORADIUS
974bf1eaec5SBrian Somers     if (bundle->radius.sessiontime)
975bf1eaec5SBrian Somers       bundle_StopSessionTimer(bundle);
976bf1eaec5SBrian Somers #endif
9770f2f3eb3SBrian Somers     bundle_StopIdleTimer(bundle);
978ab2de065SBrian Somers   }
979455aabc3SBrian Somers }
980455aabc3SBrian Somers 
981455aabc3SBrian Somers void
982ba23f397SBrian Somers bundle_Open(struct bundle *bundle, const char *name, int mask, int force)
9833006ec67SBrian Somers {
9843006ec67SBrian Somers   /*
9853006ec67SBrian Somers    * Please open the given datalink, or all if name == NULL
9863006ec67SBrian Somers    */
9873006ec67SBrian Somers   struct datalink *dl;
9883006ec67SBrian Somers 
9893006ec67SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
9903006ec67SBrian Somers     if (name == NULL || !strcasecmp(dl->name, name)) {
991ba23f397SBrian Somers       if ((mask & dl->physical->type) &&
992ba23f397SBrian Somers           (dl->state == DATALINK_CLOSED ||
993ba23f397SBrian Somers            (force && dl->state == DATALINK_OPENING &&
9945e269efeSBrian Somers             dl->dial.timer.state == TIMER_RUNNING) ||
9955e269efeSBrian Somers            dl->state == DATALINK_READY)) {
9965e269efeSBrian Somers         timer_Stop(&dl->dial.timer);	/* We're finished with this */
997565e35e5SBrian Somers         datalink_Up(dl, 1, 1);
998ab2de065SBrian Somers         if (mask & PHYS_AUTO)
9995e269efeSBrian Somers           break;			/* Only one AUTO link at a time */
100004eaa58cSBrian Somers       }
10013006ec67SBrian Somers       if (name != NULL)
10023006ec67SBrian Somers         break;
10033006ec67SBrian Somers     }
10043006ec67SBrian Somers }
10053006ec67SBrian Somers 
10063006ec67SBrian Somers struct datalink *
10073006ec67SBrian Somers bundle2datalink(struct bundle *bundle, const char *name)
10083006ec67SBrian Somers {
10093006ec67SBrian Somers   struct datalink *dl;
10103006ec67SBrian Somers 
10113006ec67SBrian Somers   if (name != NULL) {
10123006ec67SBrian Somers     for (dl = bundle->links; dl; dl = dl->next)
10133006ec67SBrian Somers       if (!strcasecmp(dl->name, name))
10143006ec67SBrian Somers         return dl;
10153006ec67SBrian Somers   } else if (bundle->links && !bundle->links->next)
10163006ec67SBrian Somers     return bundle->links;
10173006ec67SBrian Somers 
10183006ec67SBrian Somers   return NULL;
10193006ec67SBrian Somers }
10203006ec67SBrian Somers 
10213006ec67SBrian Somers int
1022aef795ccSBrian Somers bundle_ShowLinks(struct cmdargs const *arg)
1023aef795ccSBrian Somers {
1024aef795ccSBrian Somers   struct datalink *dl;
1025ab2de065SBrian Somers   struct pppThroughput *t;
102691cbd2eeSBrian Somers   unsigned long long octets;
1027ab2de065SBrian Somers   int secs;
1028aef795ccSBrian Somers 
10299c53a7b1SBrian Somers   for (dl = arg->bundle->links; dl; dl = dl->next) {
103091cbd2eeSBrian Somers     octets = MAX(dl->physical->link.stats.total.in.OctetsPerSecond,
103191cbd2eeSBrian Somers                  dl->physical->link.stats.total.out.OctetsPerSecond);
103291cbd2eeSBrian Somers 
1033d4156d00SBrian Somers     prompt_Printf(arg->prompt, "Name: %s [%s, %s]",
1034d4156d00SBrian Somers                   dl->name, mode2Nam(dl->physical->type), datalink_State(dl));
103511572abfSBrian Somers     if (dl->physical->link.stats.total.rolling && dl->state == DATALINK_OPEN)
1036e531f89aSBrian Somers       prompt_Printf(arg->prompt, " bandwidth %d, %llu bps (%llu bytes/sec)",
1037ab2de065SBrian Somers                     dl->mp.bandwidth ? dl->mp.bandwidth :
1038ab2de065SBrian Somers                                        physical_GetSpeed(dl->physical),
103991cbd2eeSBrian Somers                     octets * 8, octets);
10409c53a7b1SBrian Somers     prompt_Printf(arg->prompt, "\n");
10419c53a7b1SBrian Somers   }
1042aef795ccSBrian Somers 
104311572abfSBrian Somers   t = &arg->bundle->ncp.mp.link.stats.total;
104491cbd2eeSBrian Somers   octets = MAX(t->in.OctetsPerSecond, t->out.OctetsPerSecond);
1045ab2de065SBrian Somers   secs = t->downtime ? 0 : throughput_uptime(t);
1046ab2de065SBrian Somers   if (secs > t->SamplePeriod)
1047ab2de065SBrian Somers     secs = t->SamplePeriod;
1048ab2de065SBrian Somers   if (secs)
1049e531f89aSBrian Somers     prompt_Printf(arg->prompt, "Currently averaging %llu bps (%llu bytes/sec)"
105091cbd2eeSBrian Somers                   " over the last %d secs\n", octets * 8, octets, secs);
1051ab2de065SBrian Somers 
1052aef795ccSBrian Somers   return 0;
1053aef795ccSBrian Somers }
1054ab886ad0SBrian Somers 
10551342caedSBrian Somers static const char *
10560508c09aSBrian Somers optval(struct bundle *bundle, int opt)
10571342caedSBrian Somers {
10580508c09aSBrian Somers   return Enabled(bundle, opt) ? "enabled" : "disabled";
10591342caedSBrian Somers }
10601342caedSBrian Somers 
1061c08717dfSBrian Somers int
1062c08717dfSBrian Somers bundle_ShowStatus(struct cmdargs const *arg)
1063c08717dfSBrian Somers {
1064c08717dfSBrian Somers   int remaining;
1065c08717dfSBrian Somers 
1066c08717dfSBrian Somers   prompt_Printf(arg->prompt, "Phase %s\n", bundle_PhaseName(arg->bundle));
1067faefde08SBrian Somers   prompt_Printf(arg->prompt, " Device:        %s\n", arg->bundle->dev.Name);
1068dade2407SBrian Somers   prompt_Printf(arg->prompt, " Interface:     %s @ %lubps",
1069ab2de065SBrian Somers                 arg->bundle->iface->name, arg->bundle->bandwidth);
1070c08717dfSBrian Somers 
1071dade2407SBrian Somers   if (arg->bundle->upat) {
107246df5aa7SBrian Somers     int secs = bundle_Uptime(arg->bundle);
1073dade2407SBrian Somers 
1074dade2407SBrian Somers     prompt_Printf(arg->prompt, ", up time %d:%02d:%02d", secs / 3600,
1075dade2407SBrian Somers                   (secs / 60) % 60, secs % 60);
1076dade2407SBrian Somers   }
1077669b9965SBrian Somers   prompt_Printf(arg->prompt, "\n Queued:        %lu of %u\n",
107830949fd4SBrian Somers                 (unsigned long)ncp_QueueLen(&arg->bundle->ncp),
107977fc031dSBrian Somers                 arg->bundle->cfg.ifqueue);
1080dade2407SBrian Somers 
10816c1d6731SBrian Somers   prompt_Printf(arg->prompt, "\nDefaults:\n");
108274457d3dSBrian Somers   prompt_Printf(arg->prompt, " Label:             %s\n",
108374457d3dSBrian Somers                 arg->bundle->cfg.label);
1084610b185fSBrian Somers   prompt_Printf(arg->prompt, " Auth name:         %s\n",
1085610b185fSBrian Somers                 arg->bundle->cfg.auth.name);
108674457d3dSBrian Somers   prompt_Printf(arg->prompt, " Diagnostic socket: ");
108737b8a5c7SBrian Somers   if (*server.cfg.sockname != '\0') {
108837b8a5c7SBrian Somers     prompt_Printf(arg->prompt, "%s", server.cfg.sockname);
108937b8a5c7SBrian Somers     if (server.cfg.mask != (mode_t)-1)
109037b8a5c7SBrian Somers       prompt_Printf(arg->prompt, ", mask 0%03o", (int)server.cfg.mask);
109137b8a5c7SBrian Somers     prompt_Printf(arg->prompt, "%s\n", server.fd == -1 ? " (not open)" : "");
109237b8a5c7SBrian Somers   } else if (server.cfg.port != 0)
109374457d3dSBrian Somers     prompt_Printf(arg->prompt, "TCP port %d%s\n", server.cfg.port,
109474457d3dSBrian Somers                   server.fd == -1 ? " (not open)" : "");
109574457d3dSBrian Somers   else
109674457d3dSBrian Somers     prompt_Printf(arg->prompt, "none\n");
109704eaa58cSBrian Somers 
1098057f1760SBrian Somers   prompt_Printf(arg->prompt, " Choked Timer:      %us\n",
10996f8e9f0aSBrian Somers                 arg->bundle->cfg.choked.timeout);
1100972a1bcfSBrian Somers 
1101972a1bcfSBrian Somers #ifndef NORADIUS
1102972a1bcfSBrian Somers   radius_Show(&arg->bundle->radius, arg->prompt);
1103972a1bcfSBrian Somers #endif
1104972a1bcfSBrian Somers 
1105c08717dfSBrian Somers   prompt_Printf(arg->prompt, " Idle Timer:        ");
1106dade2407SBrian Somers   if (arg->bundle->cfg.idle.timeout) {
1107057f1760SBrian Somers     prompt_Printf(arg->prompt, "%us", arg->bundle->cfg.idle.timeout);
1108dade2407SBrian Somers     if (arg->bundle->cfg.idle.min_timeout)
1109057f1760SBrian Somers       prompt_Printf(arg->prompt, ", min %us",
1110dade2407SBrian Somers                     arg->bundle->cfg.idle.min_timeout);
1111c08717dfSBrian Somers     remaining = bundle_RemainingIdleTime(arg->bundle);
1112c08717dfSBrian Somers     if (remaining != -1)
1113c08717dfSBrian Somers       prompt_Printf(arg->prompt, " (%ds remaining)", remaining);
1114c08717dfSBrian Somers     prompt_Printf(arg->prompt, "\n");
1115c08717dfSBrian Somers   } else
1116c08717dfSBrian Somers     prompt_Printf(arg->prompt, "disabled\n");
1117c08717dfSBrian Somers 
111830949fd4SBrian Somers   prompt_Printf(arg->prompt, " Filter Decap:      %-20.20s",
111998251667SBrian Somers                 optval(arg->bundle, OPT_FILTERDECAP));
112030949fd4SBrian Somers   prompt_Printf(arg->prompt, " ID check:          %s\n",
11211342caedSBrian Somers                 optval(arg->bundle, OPT_IDCHECK));
112230949fd4SBrian Somers   prompt_Printf(arg->prompt, " Iface-Alias:       %-20.20s",
112330949fd4SBrian Somers                 optval(arg->bundle, OPT_IFACEALIAS));
112430949fd4SBrian Somers #ifndef NOINET6
112530949fd4SBrian Somers   prompt_Printf(arg->prompt, " IPCP:              %s\n",
112630949fd4SBrian Somers                 optval(arg->bundle, OPT_IPCP));
112730949fd4SBrian Somers   prompt_Printf(arg->prompt, " IPV6CP:            %-20.20s",
112830949fd4SBrian Somers                 optval(arg->bundle, OPT_IPV6CP));
112930949fd4SBrian Somers #endif
113098251667SBrian Somers   prompt_Printf(arg->prompt, " Keep-Session:      %s\n",
1131ac685e31SBrian Somers                 optval(arg->bundle, OPT_KEEPSESSION));
113298251667SBrian Somers   prompt_Printf(arg->prompt, " Loopback:          %-20.20s",
11331342caedSBrian Somers                 optval(arg->bundle, OPT_LOOPBACK));
113498251667SBrian Somers   prompt_Printf(arg->prompt, " PasswdAuth:        %s\n",
11351342caedSBrian Somers                 optval(arg->bundle, OPT_PASSWDAUTH));
113698251667SBrian Somers   prompt_Printf(arg->prompt, " Proxy:             %-20.20s",
11371342caedSBrian Somers                 optval(arg->bundle, OPT_PROXY));
113898251667SBrian Somers   prompt_Printf(arg->prompt, " Proxyall:          %s\n",
11393afe5ccbSBrian Somers                 optval(arg->bundle, OPT_PROXYALL));
114030949fd4SBrian Somers   prompt_Printf(arg->prompt, " Sticky Routes:     %-20.20s",
114130949fd4SBrian Somers                 optval(arg->bundle, OPT_SROUTES));
114230949fd4SBrian Somers   prompt_Printf(arg->prompt, " TCPMSS Fixup:      %s\n",
114394d7be52SBrian Somers                 optval(arg->bundle, OPT_TCPMSSFIXUP));
114430949fd4SBrian Somers   prompt_Printf(arg->prompt, " Throughput:        %-20.20s",
11451342caedSBrian Somers                 optval(arg->bundle, OPT_THROUGHPUT));
114630949fd4SBrian Somers   prompt_Printf(arg->prompt, " Utmp Logging:      %s\n",
11471342caedSBrian Somers                 optval(arg->bundle, OPT_UTMP));
11480508c09aSBrian Somers   prompt_Printf(arg->prompt, " NAS-IP-Address:    %-20.20s",
11490508c09aSBrian Somers                 optval(arg->bundle, OPT_NAS_IP_ADDRESS));
11500508c09aSBrian Somers   prompt_Printf(arg->prompt, " NAS-Identifier:    %s\n",
11510508c09aSBrian Somers                 optval(arg->bundle, OPT_NAS_IDENTIFIER));
11521342caedSBrian Somers 
1153c08717dfSBrian Somers   return 0;
1154c08717dfSBrian Somers }
1155c08717dfSBrian Somers 
1156ab886ad0SBrian Somers static void
1157ab886ad0SBrian Somers bundle_IdleTimeout(void *v)
1158ab886ad0SBrian Somers {
1159ab886ad0SBrian Somers   struct bundle *bundle = (struct bundle *)v;
1160ab886ad0SBrian Somers 
1161b42135deSBrian Somers   log_Printf(LogPHASE, "Idle timer expired\n");
116204eaa58cSBrian Somers   bundle_StopIdleTimer(bundle);
11639c81b87dSBrian Somers   bundle_Close(bundle, NULL, CLOSE_STAYDOWN);
1164ab886ad0SBrian Somers }
1165ab886ad0SBrian Somers 
1166ab886ad0SBrian Somers /*
1167ab886ad0SBrian Somers  *  Start Idle timer. If timeout is reached, we call bundle_Close() to
1168ab886ad0SBrian Somers  *  close LCP and link.
1169ab886ad0SBrian Somers  */
1170ab886ad0SBrian Somers void
11710a4b6c5cSBrian Somers bundle_StartIdleTimer(struct bundle *bundle, unsigned secs)
1172ab886ad0SBrian Somers {
1173dd7e2610SBrian Somers   timer_Stop(&bundle->idle.timer);
1174ff0f9439SBrian Somers   if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) !=
1175dade2407SBrian Somers       bundle->phys_type.open && bundle->cfg.idle.timeout) {
11760a4b6c5cSBrian Somers     time_t now = time(NULL);
1177dade2407SBrian Somers 
11780a4b6c5cSBrian Somers     if (secs == 0)
1179dade2407SBrian Somers       secs = bundle->cfg.idle.timeout;
11800a4b6c5cSBrian Somers 
11810a4b6c5cSBrian Somers     /* We want at least `secs' */
1182dade2407SBrian Somers     if (bundle->cfg.idle.min_timeout > secs && bundle->upat) {
1183057f1760SBrian Somers       unsigned up = now - bundle->upat;
1184dade2407SBrian Somers 
1185057f1760SBrian Somers       if (bundle->cfg.idle.min_timeout > up &&
1186057f1760SBrian Somers           bundle->cfg.idle.min_timeout - up > (long long)secs)
11870a4b6c5cSBrian Somers         /* Only increase from the current `remaining' value */
1188dade2407SBrian Somers         secs = bundle->cfg.idle.min_timeout - up;
1189dade2407SBrian Somers     }
119093ee0ff2SBrian Somers     bundle->idle.timer.func = bundle_IdleTimeout;
11913b0f8d2eSBrian Somers     bundle->idle.timer.name = "idle";
1192dade2407SBrian Somers     bundle->idle.timer.load = secs * SECTICKS;
119393ee0ff2SBrian Somers     bundle->idle.timer.arg = bundle;
1194dd7e2610SBrian Somers     timer_Start(&bundle->idle.timer);
11950a4b6c5cSBrian Somers     bundle->idle.done = now + secs;
1196ab886ad0SBrian Somers   }
1197ab886ad0SBrian Somers }
1198ab886ad0SBrian Somers 
1199ab886ad0SBrian Somers void
1200057f1760SBrian Somers bundle_SetIdleTimer(struct bundle *bundle, unsigned timeout,
1201057f1760SBrian Somers 		    unsigned min_timeout)
1202ab886ad0SBrian Somers {
1203dade2407SBrian Somers   bundle->cfg.idle.timeout = timeout;
1204dade2407SBrian Somers   bundle->cfg.idle.min_timeout = min_timeout;
120530949fd4SBrian Somers   if (ncp_LayersOpen(&bundle->ncp))
12060a4b6c5cSBrian Somers     bundle_StartIdleTimer(bundle, 0);
1207ab886ad0SBrian Somers }
1208ab886ad0SBrian Somers 
1209ab886ad0SBrian Somers void
1210ab886ad0SBrian Somers bundle_StopIdleTimer(struct bundle *bundle)
1211ab886ad0SBrian Somers {
1212dd7e2610SBrian Somers   timer_Stop(&bundle->idle.timer);
12134a632c80SBrian Somers   bundle->idle.done = 0;
1214ab886ad0SBrian Somers }
1215ab886ad0SBrian Somers 
121604eaa58cSBrian Somers static int
1217ab886ad0SBrian Somers bundle_RemainingIdleTime(struct bundle *bundle)
1218ab886ad0SBrian Somers {
121993ee0ff2SBrian Somers   if (bundle->idle.done)
122093ee0ff2SBrian Somers     return bundle->idle.done - time(NULL);
1221ab886ad0SBrian Somers   return -1;
1222ab886ad0SBrian Somers }
12233b0f8d2eSBrian Somers 
1224bf1eaec5SBrian Somers #ifndef NORADIUS
1225bf1eaec5SBrian Somers 
1226bf1eaec5SBrian Somers static void
1227bf1eaec5SBrian Somers bundle_SessionTimeout(void *v)
1228bf1eaec5SBrian Somers {
1229bf1eaec5SBrian Somers   struct bundle *bundle = (struct bundle *)v;
1230bf1eaec5SBrian Somers 
1231bf1eaec5SBrian Somers   log_Printf(LogPHASE, "Session-Timeout timer expired\n");
1232bf1eaec5SBrian Somers   bundle_StopSessionTimer(bundle);
1233bf1eaec5SBrian Somers   bundle_Close(bundle, NULL, CLOSE_STAYDOWN);
1234bf1eaec5SBrian Somers }
1235bf1eaec5SBrian Somers 
1236bf1eaec5SBrian Somers void
1237bf1eaec5SBrian Somers bundle_StartSessionTimer(struct bundle *bundle, unsigned secs)
1238bf1eaec5SBrian Somers {
1239bf1eaec5SBrian Somers   timer_Stop(&bundle->session.timer);
1240bf1eaec5SBrian Somers   if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) !=
1241bf1eaec5SBrian Somers       bundle->phys_type.open && bundle->radius.sessiontime) {
1242bf1eaec5SBrian Somers     time_t now = time(NULL);
1243bf1eaec5SBrian Somers 
1244bf1eaec5SBrian Somers     if (secs == 0)
1245bf1eaec5SBrian Somers       secs = bundle->radius.sessiontime;
1246bf1eaec5SBrian Somers 
1247bf1eaec5SBrian Somers     bundle->session.timer.func = bundle_SessionTimeout;
1248bf1eaec5SBrian Somers     bundle->session.timer.name = "session";
1249bf1eaec5SBrian Somers     bundle->session.timer.load = secs * SECTICKS;
1250bf1eaec5SBrian Somers     bundle->session.timer.arg = bundle;
1251bf1eaec5SBrian Somers     timer_Start(&bundle->session.timer);
1252bf1eaec5SBrian Somers     bundle->session.done = now + secs;
1253bf1eaec5SBrian Somers   }
1254bf1eaec5SBrian Somers }
1255bf1eaec5SBrian Somers 
1256bf1eaec5SBrian Somers void
1257bf1eaec5SBrian Somers bundle_StopSessionTimer(struct bundle *bundle)
1258bf1eaec5SBrian Somers {
1259bf1eaec5SBrian Somers   timer_Stop(&bundle->session.timer);
1260bf1eaec5SBrian Somers   bundle->session.done = 0;
1261bf1eaec5SBrian Somers }
1262bf1eaec5SBrian Somers 
1263bf1eaec5SBrian Somers #endif
1264bf1eaec5SBrian Somers 
12653b0f8d2eSBrian Somers int
12663b0f8d2eSBrian Somers bundle_IsDead(struct bundle *bundle)
12673b0f8d2eSBrian Somers {
12683b0f8d2eSBrian Somers   return !bundle->links || (bundle->phase == PHASE_DEAD && bundle->CleaningUp);
12693b0f8d2eSBrian Somers }
1270b6217683SBrian Somers 
127104eaa58cSBrian Somers static struct datalink *
127204eaa58cSBrian Somers bundle_DatalinkLinkout(struct bundle *bundle, struct datalink *dl)
1273cd7bd93aSBrian Somers {
1274cd7bd93aSBrian Somers   struct datalink **dlp;
1275cd7bd93aSBrian Somers 
1276cd7bd93aSBrian Somers   for (dlp = &bundle->links; *dlp; dlp = &(*dlp)->next)
1277cd7bd93aSBrian Somers     if (*dlp == dl) {
127804eaa58cSBrian Somers       *dlp = dl->next;
127904eaa58cSBrian Somers       dl->next = NULL;
128004eaa58cSBrian Somers       bundle_LinksRemoved(bundle);
128104eaa58cSBrian Somers       return dl;
1282cd7bd93aSBrian Somers     }
128304eaa58cSBrian Somers 
128404eaa58cSBrian Somers   return NULL;
128504eaa58cSBrian Somers }
128604eaa58cSBrian Somers 
128704eaa58cSBrian Somers static void
128804eaa58cSBrian Somers bundle_DatalinkLinkin(struct bundle *bundle, struct datalink *dl)
128904eaa58cSBrian Somers {
129004eaa58cSBrian Somers   struct datalink **dlp = &bundle->links;
129104eaa58cSBrian Somers 
129204eaa58cSBrian Somers   while (*dlp)
129304eaa58cSBrian Somers     dlp = &(*dlp)->next;
129404eaa58cSBrian Somers 
129504eaa58cSBrian Somers   *dlp = dl;
129604eaa58cSBrian Somers   dl->next = NULL;
129704eaa58cSBrian Somers 
129804eaa58cSBrian Somers   bundle_LinkAdded(bundle, dl);
1299ab2de065SBrian Somers   mp_CheckAutoloadTimer(&bundle->ncp.mp);
1300565e35e5SBrian Somers }
1301565e35e5SBrian Somers 
1302565e35e5SBrian Somers void
1303565e35e5SBrian Somers bundle_CleanDatalinks(struct bundle *bundle)
1304565e35e5SBrian Somers {
1305565e35e5SBrian Somers   struct datalink **dlp = &bundle->links;
130604eaa58cSBrian Somers   int found = 0;
1307565e35e5SBrian Somers 
1308565e35e5SBrian Somers   while (*dlp)
1309565e35e5SBrian Somers     if ((*dlp)->state == DATALINK_CLOSED &&
1310f6a4e748SBrian Somers         (*dlp)->physical->type &
1311f6a4e748SBrian Somers         (PHYS_DIRECT|PHYS_BACKGROUND|PHYS_FOREGROUND)) {
1312565e35e5SBrian Somers       *dlp = datalink_Destroy(*dlp);
131304eaa58cSBrian Somers       found++;
131404eaa58cSBrian Somers     } else
1315565e35e5SBrian Somers       dlp = &(*dlp)->next;
131604eaa58cSBrian Somers 
131704eaa58cSBrian Somers   if (found)
131804eaa58cSBrian Somers     bundle_LinksRemoved(bundle);
131904eaa58cSBrian Somers }
132004eaa58cSBrian Somers 
132104eaa58cSBrian Somers int
132204eaa58cSBrian Somers bundle_DatalinkClone(struct bundle *bundle, struct datalink *dl,
132304eaa58cSBrian Somers                      const char *name)
132404eaa58cSBrian Somers {
132504eaa58cSBrian Somers   if (bundle2datalink(bundle, name)) {
132604eaa58cSBrian Somers     log_Printf(LogWARN, "Clone: %s: name already exists\n", name);
132704eaa58cSBrian Somers     return 0;
132804eaa58cSBrian Somers   }
132904eaa58cSBrian Somers 
133004eaa58cSBrian Somers   bundle_DatalinkLinkin(bundle, datalink_Clone(dl, name));
133104eaa58cSBrian Somers   return 1;
133204eaa58cSBrian Somers }
133304eaa58cSBrian Somers 
133404eaa58cSBrian Somers void
133504eaa58cSBrian Somers bundle_DatalinkRemove(struct bundle *bundle, struct datalink *dl)
133604eaa58cSBrian Somers {
133704eaa58cSBrian Somers   dl = bundle_DatalinkLinkout(bundle, dl);
133804eaa58cSBrian Somers   if (dl)
133904eaa58cSBrian Somers     datalink_Destroy(dl);
1340cd7bd93aSBrian Somers }
134149052c95SBrian Somers 
134249052c95SBrian Somers void
134349052c95SBrian Somers bundle_SetLabel(struct bundle *bundle, const char *label)
134449052c95SBrian Somers {
134549052c95SBrian Somers   if (label)
134649052c95SBrian Somers     strncpy(bundle->cfg.label, label, sizeof bundle->cfg.label - 1);
134749052c95SBrian Somers   else
134849052c95SBrian Somers     *bundle->cfg.label = '\0';
134949052c95SBrian Somers }
135049052c95SBrian Somers 
135149052c95SBrian Somers const char *
135249052c95SBrian Somers bundle_GetLabel(struct bundle *bundle)
135349052c95SBrian Somers {
135449052c95SBrian Somers   return *bundle->cfg.label ? bundle->cfg.label : NULL;
135549052c95SBrian Somers }
13561fa665f5SBrian Somers 
13572cb305afSBrian Somers int
13582cb305afSBrian Somers bundle_LinkSize()
13591fa665f5SBrian Somers {
136096c9bb21SBrian Somers   struct iovec iov[SCATTER_SEGMENTS];
13612cb305afSBrian Somers   int niov, expect, f;
136287c3786eSBrian Somers 
136396c9bb21SBrian Somers   iov[0].iov_len = strlen(Version) + 1;
13642cb305afSBrian Somers   iov[0].iov_base = NULL;
13652cb305afSBrian Somers   niov = 1;
13662cb305afSBrian Somers   if (datalink2iov(NULL, iov, &niov, SCATTER_SEGMENTS, NULL, NULL) == -1) {
13672cb305afSBrian Somers     log_Printf(LogERROR, "Cannot determine space required for link\n");
13682cb305afSBrian Somers     return 0;
136954cd8e13SBrian Somers   }
13706f384573SBrian Somers 
137196c9bb21SBrian Somers   for (f = expect = 0; f < niov; f++)
137296c9bb21SBrian Somers     expect += iov[f].iov_len;
137396c9bb21SBrian Somers 
13742cb305afSBrian Somers   return expect;
137587c3786eSBrian Somers }
137696c9bb21SBrian Somers 
13772cb305afSBrian Somers void
13782cb305afSBrian Somers bundle_ReceiveDatalink(struct bundle *bundle, int s)
13792cb305afSBrian Somers {
13802cb305afSBrian Somers   char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int) * SEND_MAXFD];
1381057f1760SBrian Somers   int niov, expect, f, *fd, nfd, onfd;
1382057f1760SBrian Somers   ssize_t got;
13832cb305afSBrian Somers   struct iovec iov[SCATTER_SEGMENTS];
13842cb305afSBrian Somers   struct cmsghdr *cmsg;
13852cb305afSBrian Somers   struct msghdr msg;
13862cb305afSBrian Somers   struct datalink *dl;
13872cb305afSBrian Somers   pid_t pid;
13882cb305afSBrian Somers 
13892cb305afSBrian Somers   log_Printf(LogPHASE, "Receiving datalink\n");
13902cb305afSBrian Somers 
13912cb305afSBrian Somers   /*
13922cb305afSBrian Somers    * Create our scatter/gather array - passing NULL gets the space
13932cb305afSBrian Somers    * allocation requirement rather than actually flattening the
13942cb305afSBrian Somers    * structures.
13952cb305afSBrian Somers    */
13962cb305afSBrian Somers   iov[0].iov_len = strlen(Version) + 1;
13972cb305afSBrian Somers   iov[0].iov_base = NULL;
13982cb305afSBrian Somers   niov = 1;
13992cb305afSBrian Somers   if (datalink2iov(NULL, iov, &niov, SCATTER_SEGMENTS, NULL, NULL) == -1) {
14002cb305afSBrian Somers     log_Printf(LogERROR, "Cannot determine space required for link\n");
14012cb305afSBrian Somers     return;
14022cb305afSBrian Somers   }
14032cb305afSBrian Somers 
14042cb305afSBrian Somers   /* Allocate the scatter/gather array for recvmsg() */
14052cb305afSBrian Somers   for (f = expect = 0; f < niov; f++) {
14062cb305afSBrian Somers     if ((iov[f].iov_base = malloc(iov[f].iov_len)) == NULL) {
14072cb305afSBrian Somers       log_Printf(LogERROR, "Cannot allocate space to receive link\n");
14082cb305afSBrian Somers       return;
14092cb305afSBrian Somers     }
1410cbee9754SBrian Somers     if (f)
14112cb305afSBrian Somers       expect += iov[f].iov_len;
14122cb305afSBrian Somers   }
14132cb305afSBrian Somers 
14142cb305afSBrian Somers   /* Set up our message */
14152cb305afSBrian Somers   cmsg = (struct cmsghdr *)cmsgbuf;
14162cb305afSBrian Somers   cmsg->cmsg_len = sizeof cmsgbuf;
14172cb305afSBrian Somers   cmsg->cmsg_level = SOL_SOCKET;
14182cb305afSBrian Somers   cmsg->cmsg_type = 0;
14192cb305afSBrian Somers 
142096c9bb21SBrian Somers   memset(&msg, '\0', sizeof msg);
14212cb305afSBrian Somers   msg.msg_name = NULL;
14222cb305afSBrian Somers   msg.msg_namelen = 0;
142396c9bb21SBrian Somers   msg.msg_iov = iov;
1424cbee9754SBrian Somers   msg.msg_iovlen = 1;		/* Only send the version at the first pass */
142596c9bb21SBrian Somers   msg.msg_control = cmsgbuf;
142696c9bb21SBrian Somers   msg.msg_controllen = sizeof cmsgbuf;
142796c9bb21SBrian Somers 
1428209dc102SBrian Somers   log_Printf(LogDEBUG, "Expecting %u scatter/gather bytes\n",
1429209dc102SBrian Somers              (unsigned)iov[0].iov_len);
14302cb305afSBrian Somers 
1431057f1760SBrian Somers   if ((got = recvmsg(s, &msg, MSG_WAITALL)) != (ssize_t)iov[0].iov_len) {
1432cbee9754SBrian Somers     if (got == -1)
143396c9bb21SBrian Somers       log_Printf(LogERROR, "Failed recvmsg: %s\n", strerror(errno));
143496c9bb21SBrian Somers     else
14351814213eSMarcel Moolenaar       log_Printf(LogERROR, "Failed recvmsg: Got %zd, not %u\n",
1436209dc102SBrian Somers                  got, (unsigned)iov[0].iov_len);
143796c9bb21SBrian Somers     while (niov--)
143896c9bb21SBrian Somers       free(iov[niov].iov_base);
143996c9bb21SBrian Somers     return;
144096c9bb21SBrian Somers   }
144196c9bb21SBrian Somers 
14422cb305afSBrian Somers   if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
14432cb305afSBrian Somers     log_Printf(LogERROR, "Recvmsg: no descriptors received !\n");
14442cb305afSBrian Somers     while (niov--)
14452cb305afSBrian Somers       free(iov[niov].iov_base);
14462cb305afSBrian Somers     return;
144787c3786eSBrian Somers   }
144887c3786eSBrian Somers 
14492bc21ed9SDavid Malone   fd = (int *)CMSG_DATA(cmsg);
14502bc21ed9SDavid Malone   nfd = ((caddr_t)cmsg + cmsg->cmsg_len - (caddr_t)fd) / sizeof(int);
14512cb305afSBrian Somers 
14522cb305afSBrian Somers   if (nfd < 2) {
14538e7bd08eSBrian Somers     log_Printf(LogERROR, "Recvmsg: %d descriptor%s received (too few) !\n",
14542cb305afSBrian Somers                nfd, nfd == 1 ? "" : "s");
14552cb305afSBrian Somers     while (nfd--)
14562cb305afSBrian Somers       close(fd[nfd]);
1457ad5b0e8bSBrian Somers     while (niov--)
1458ad5b0e8bSBrian Somers       free(iov[niov].iov_base);
1459ad5b0e8bSBrian Somers     return;
146054cd8e13SBrian Somers   }
146154cd8e13SBrian Somers 
146287c3786eSBrian Somers   /*
1463cbee9754SBrian Somers    * We've successfully received two or more open file descriptors
1464cbee9754SBrian Somers    * through our socket, plus a version string.  Make sure it's the
1465cbee9754SBrian Somers    * correct version, and drop the connection if it's not.
146687c3786eSBrian Somers    */
146796c9bb21SBrian Somers   if (strncmp(Version, iov[0].iov_base, iov[0].iov_len)) {
146896c9bb21SBrian Somers     log_Printf(LogWARN, "Cannot receive datalink, incorrect version"
146996c9bb21SBrian Somers                " (\"%.*s\", not \"%s\")\n", (int)iov[0].iov_len,
14707d81ddf5SBrian Somers                (char *)iov[0].iov_base, Version);
14712cb305afSBrian Somers     while (nfd--)
14722cb305afSBrian Somers       close(fd[nfd]);
147396c9bb21SBrian Somers     while (niov--)
147496c9bb21SBrian Somers       free(iov[niov].iov_base);
147596c9bb21SBrian Somers     return;
147696c9bb21SBrian Somers   }
147796c9bb21SBrian Somers 
1478cbee9754SBrian Somers   /*
1479cbee9754SBrian Somers    * Everything looks good.  Send the other side our process id so that
1480cbee9754SBrian Somers    * they can transfer lock ownership, and wait for them to send the
1481cbee9754SBrian Somers    * actual link data.
1482cbee9754SBrian Somers    */
1483cbee9754SBrian Somers   pid = getpid();
1484cbee9754SBrian Somers   if ((got = write(fd[1], &pid, sizeof pid)) != sizeof pid) {
1485cbee9754SBrian Somers     if (got == -1)
1486cbee9754SBrian Somers       log_Printf(LogERROR, "Failed write: %s\n", strerror(errno));
1487cbee9754SBrian Somers     else
14881814213eSMarcel Moolenaar       log_Printf(LogERROR, "Failed write: Got %zd, not %d\n", got,
1489cbee9754SBrian Somers                  (int)(sizeof pid));
1490cbee9754SBrian Somers     while (nfd--)
1491cbee9754SBrian Somers       close(fd[nfd]);
1492cbee9754SBrian Somers     while (niov--)
1493cbee9754SBrian Somers       free(iov[niov].iov_base);
1494cbee9754SBrian Somers     return;
1495cbee9754SBrian Somers   }
1496cbee9754SBrian Somers 
1497cbee9754SBrian Somers   if ((got = readv(fd[1], iov + 1, niov - 1)) != expect) {
1498cbee9754SBrian Somers     if (got == -1)
1499cbee9754SBrian Somers       log_Printf(LogERROR, "Failed write: %s\n", strerror(errno));
1500cbee9754SBrian Somers     else
15011814213eSMarcel Moolenaar       log_Printf(LogERROR, "Failed write: Got %zd, not %d\n", got, expect);
1502cbee9754SBrian Somers     while (nfd--)
1503cbee9754SBrian Somers       close(fd[nfd]);
1504cbee9754SBrian Somers     while (niov--)
1505cbee9754SBrian Somers       free(iov[niov].iov_base);
1506cbee9754SBrian Somers     return;
1507cbee9754SBrian Somers   }
1508cbee9754SBrian Somers   close(fd[1]);
1509cbee9754SBrian Somers 
15102cb305afSBrian Somers   onfd = nfd;	/* We've got this many in our array */
15118e7bd08eSBrian Somers   nfd -= 2;	/* Don't include p->fd and our reply descriptor */
15122cb305afSBrian Somers   niov = 1;	/* Skip the version id */
151387c3786eSBrian Somers   dl = iov2datalink(bundle, iov, &niov, sizeof iov / sizeof *iov, fd[0],
15142cb305afSBrian Somers                     fd + 2, &nfd);
1515b7c5748eSBrian Somers   if (dl) {
15162cb305afSBrian Somers 
151787c3786eSBrian Somers     if (nfd) {
151887c3786eSBrian Somers       log_Printf(LogERROR, "bundle_ReceiveDatalink: Failed to handle %d "
15192cb305afSBrian Somers                  "auxiliary file descriptors (%d remain)\n", onfd, nfd);
152087c3786eSBrian Somers       datalink_Destroy(dl);
152187c3786eSBrian Somers       while (nfd--)
152287c3786eSBrian Somers         close(fd[onfd--]);
15232cb305afSBrian Somers       close(fd[0]);
152487c3786eSBrian Somers     } else {
152504eaa58cSBrian Somers       bundle_DatalinkLinkin(bundle, dl);
1526b7c5748eSBrian Somers       datalink_AuthOk(dl);
1527ab2de065SBrian Somers       bundle_CalculateBandwidth(dl->bundle);
152887c3786eSBrian Somers     }
152987c3786eSBrian Somers   } else {
153087c3786eSBrian Somers     while (nfd--)
153187c3786eSBrian Somers       close(fd[onfd--]);
153287c3786eSBrian Somers     close(fd[0]);
15332cb305afSBrian Somers     close(fd[1]);
153487c3786eSBrian Somers   }
153596c9bb21SBrian Somers 
153696c9bb21SBrian Somers   free(iov[0].iov_base);
15371fa665f5SBrian Somers }
15381fa665f5SBrian Somers 
15391fa665f5SBrian Somers void
154096c9bb21SBrian Somers bundle_SendDatalink(struct datalink *dl, int s, struct sockaddr_un *sun)
15411fa665f5SBrian Somers {
15422bc21ed9SDavid Malone   char cmsgbuf[CMSG_SPACE(sizeof(int) * SEND_MAXFD)];
15432cb305afSBrian Somers   const char *constlock;
15442cb305afSBrian Somers   char *lock;
154587c3786eSBrian Somers   struct cmsghdr *cmsg;
154696c9bb21SBrian Somers   struct msghdr msg;
154796c9bb21SBrian Somers   struct iovec iov[SCATTER_SEGMENTS];
1548057f1760SBrian Somers   int niov, f, expect, newsid, fd[SEND_MAXFD], nfd, reply[2];
1549057f1760SBrian Somers   ssize_t got;
155085fd273aSBrian Somers   pid_t newpid;
15516f384573SBrian Somers 
1552dd7e2610SBrian Somers   log_Printf(LogPHASE, "Transmitting datalink %s\n", dl->name);
15536f384573SBrian Somers 
15542cb305afSBrian Somers   /* Record the base device name for a lock transfer later */
15552cb305afSBrian Somers   constlock = physical_LockedDevice(dl->physical);
15562cb305afSBrian Somers   if (constlock) {
15572cb305afSBrian Somers     lock = alloca(strlen(constlock) + 1);
15582cb305afSBrian Somers     strcpy(lock, constlock);
15592cb305afSBrian Somers   } else
15602cb305afSBrian Somers     lock = NULL;
15612cb305afSBrian Somers 
156204eaa58cSBrian Somers   bundle_LinkClosed(dl->bundle, dl);
15630f2f3eb3SBrian Somers   bundle_DatalinkLinkout(dl->bundle, dl);
1564ea722969SBrian Somers 
156596c9bb21SBrian Somers   /* Build our scatter/gather array */
156696c9bb21SBrian Somers   iov[0].iov_len = strlen(Version) + 1;
156796c9bb21SBrian Somers   iov[0].iov_base = strdup(Version);
156896c9bb21SBrian Somers   niov = 1;
156987c3786eSBrian Somers   nfd = 0;
15706f384573SBrian Somers 
15712cb305afSBrian Somers   fd[0] = datalink2iov(dl, iov, &niov, SCATTER_SEGMENTS, fd + 2, &nfd);
15726f384573SBrian Somers 
15732cb305afSBrian Somers   if (fd[0] != -1 && socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, reply) != -1) {
15742cb305afSBrian Somers     /*
15752cb305afSBrian Somers      * fd[1] is used to get the peer process id back, then to confirm that
15762cb305afSBrian Somers      * we've transferred any device locks to that process id.
15772cb305afSBrian Somers      */
15782cb305afSBrian Somers     fd[1] = reply[1];
157987c3786eSBrian Somers 
15802cb305afSBrian Somers     nfd += 2;			/* Include fd[0] and fd[1] */
158196c9bb21SBrian Somers     memset(&msg, '\0', sizeof msg);
158254cd8e13SBrian Somers 
15832cb305afSBrian Somers     msg.msg_name = NULL;
15842cb305afSBrian Somers     msg.msg_namelen = 0;
1585cbee9754SBrian Somers     /*
1586cbee9754SBrian Somers      * Only send the version to start...  We used to send the whole lot, but
1587cbee9754SBrian Somers      * this caused problems with our RECVBUF size as a single link is about
1588cbee9754SBrian Somers      * 22k !  This way, we should bump into no limits.
1589cbee9754SBrian Somers      */
1590cbee9754SBrian Somers     msg.msg_iovlen = 1;
159196c9bb21SBrian Somers     msg.msg_iov = iov;
15922cb305afSBrian Somers     msg.msg_control = cmsgbuf;
15932bc21ed9SDavid Malone     msg.msg_controllen = CMSG_SPACE(sizeof(int) * nfd);
15942cb305afSBrian Somers     msg.msg_flags = 0;
159554cd8e13SBrian Somers 
15962cb305afSBrian Somers     cmsg = (struct cmsghdr *)cmsgbuf;
15972cb305afSBrian Somers     cmsg->cmsg_len = msg.msg_controllen;
159854cd8e13SBrian Somers     cmsg->cmsg_level = SOL_SOCKET;
159954cd8e13SBrian Somers     cmsg->cmsg_type = SCM_RIGHTS;
160087c3786eSBrian Somers 
16012cb305afSBrian Somers     for (f = 0; f < nfd; f++)
16022bc21ed9SDavid Malone       *((int *)CMSG_DATA(cmsg) + f) = fd[f];
160347723d29SBrian Somers 
1604cbee9754SBrian Somers     for (f = 1, expect = 0; f < niov; f++)
160596c9bb21SBrian Somers       expect += iov[f].iov_len;
160647723d29SBrian Somers 
1607cbee9754SBrian Somers     if (setsockopt(reply[0], SOL_SOCKET, SO_SNDBUF, &expect, sizeof(int)) == -1)
1608cbee9754SBrian Somers       log_Printf(LogERROR, "setsockopt(SO_RCVBUF, %d): %s\n", expect,
1609cbee9754SBrian Somers                  strerror(errno));
1610cbee9754SBrian Somers     if (setsockopt(reply[1], SOL_SOCKET, SO_RCVBUF, &expect, sizeof(int)) == -1)
1611cbee9754SBrian Somers       log_Printf(LogERROR, "setsockopt(SO_RCVBUF, %d): %s\n", expect,
1612cbee9754SBrian Somers                  strerror(errno));
1613cbee9754SBrian Somers 
1614209dc102SBrian Somers     log_Printf(LogDEBUG, "Sending %d descriptor%s and %u bytes in scatter"
1615209dc102SBrian Somers                "/gather array\n", nfd, nfd == 1 ? "" : "s",
1616209dc102SBrian Somers                (unsigned)iov[0].iov_len);
161747723d29SBrian Somers 
16182cb305afSBrian Somers     if ((got = sendmsg(s, &msg, 0)) == -1)
16192cb305afSBrian Somers       log_Printf(LogERROR, "Failed sendmsg: %s: %s\n",
16202cb305afSBrian Somers                  sun->sun_path, strerror(errno));
1621057f1760SBrian Somers     else if (got != (ssize_t)iov[0].iov_len)
16221814213eSMarcel Moolenaar       log_Printf(LogERROR, "%s: Failed initial sendmsg: Only sent %zd of %u\n",
1623209dc102SBrian Somers                  sun->sun_path, got, (unsigned)iov[0].iov_len);
16242cb305afSBrian Somers     else {
16258e7bd08eSBrian Somers       /* We must get the ACK before closing the descriptor ! */
16262cb305afSBrian Somers       int res;
16272cb305afSBrian Somers 
1628cbee9754SBrian Somers       if ((got = read(reply[0], &newpid, sizeof newpid)) == sizeof newpid) {
162942df3c25SBrian Somers         log_Printf(LogDEBUG, "Received confirmation from pid %ld\n",
163042df3c25SBrian Somers                    (long)newpid);
16312cb305afSBrian Somers         if (lock && (res = ID0uu_lock_txfr(lock, newpid)) != UU_LOCK_OK)
1632b42135deSBrian Somers             log_Printf(LogERROR, "uu_lock_txfr: %s\n", uu_lockerr(res));
16332cb305afSBrian Somers 
1634cbee9754SBrian Somers         log_Printf(LogDEBUG, "Transmitting link (%d bytes)\n", expect);
1635cbee9754SBrian Somers         if ((got = writev(reply[0], iov + 1, niov - 1)) != expect) {
1636cbee9754SBrian Somers           if (got == -1)
1637cbee9754SBrian Somers             log_Printf(LogERROR, "%s: Failed writev: %s\n",
1638cbee9754SBrian Somers                        sun->sun_path, strerror(errno));
1639cbee9754SBrian Somers           else
16401814213eSMarcel Moolenaar             log_Printf(LogERROR, "%s: Failed writev: Wrote %zd of %d\n",
1641cbee9754SBrian Somers                        sun->sun_path, got, expect);
1642cbee9754SBrian Somers         }
1643cbee9754SBrian Somers       } else if (got == -1)
1644cbee9754SBrian Somers         log_Printf(LogERROR, "%s: Failed socketpair read: %s\n",
1645cbee9754SBrian Somers                    sun->sun_path, strerror(errno));
1646cbee9754SBrian Somers       else
16471814213eSMarcel Moolenaar         log_Printf(LogERROR, "%s: Failed socketpair read: Got %zd of %d\n",
1648cbee9754SBrian Somers                    sun->sun_path, got, (int)(sizeof newpid));
16492cb305afSBrian Somers     }
16502cb305afSBrian Somers 
16512cb305afSBrian Somers     close(reply[0]);
16522cb305afSBrian Somers     close(reply[1]);
165354cd8e13SBrian Somers 
1654ac685e31SBrian Somers     newsid = Enabled(dl->bundle, OPT_KEEPSESSION) ||
165587c3786eSBrian Somers              tcgetpgrp(fd[0]) == getpgrp();
165687c3786eSBrian Somers     while (nfd)
165787c3786eSBrian Somers       close(fd[--nfd]);
16581384bd27SBrian Somers     if (newsid)
16592cb305afSBrian Somers       bundle_setsid(dl->bundle, got != -1);
166047723d29SBrian Somers   }
166185fd273aSBrian Somers   close(s);
166296c9bb21SBrian Somers 
166396c9bb21SBrian Somers   while (niov--)
166496c9bb21SBrian Somers     free(iov[niov].iov_base);
16651fa665f5SBrian Somers }
1666dd0645c5SBrian Somers 
1667dd0645c5SBrian Somers int
166858d55334SBrian Somers bundle_RenameDatalink(struct bundle *bundle, struct datalink *ndl,
166958d55334SBrian Somers                       const char *name)
167058d55334SBrian Somers {
167158d55334SBrian Somers   struct datalink *dl;
167258d55334SBrian Somers 
167358d55334SBrian Somers   if (!strcasecmp(ndl->name, name))
167458d55334SBrian Somers     return 1;
167558d55334SBrian Somers 
167658d55334SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
167758d55334SBrian Somers     if (!strcasecmp(dl->name, name))
167858d55334SBrian Somers       return 0;
167958d55334SBrian Somers 
168058d55334SBrian Somers   datalink_Rename(ndl, name);
168158d55334SBrian Somers   return 1;
168258d55334SBrian Somers }
168358d55334SBrian Somers 
168458d55334SBrian Somers int
1685dd0645c5SBrian Somers bundle_SetMode(struct bundle *bundle, struct datalink *dl, int mode)
1686dd0645c5SBrian Somers {
1687dd0645c5SBrian Somers   int omode;
1688dd0645c5SBrian Somers 
1689dd0645c5SBrian Somers   omode = dl->physical->type;
1690dd0645c5SBrian Somers   if (omode == mode)
1691dd0645c5SBrian Somers     return 1;
1692dd0645c5SBrian Somers 
1693ff0f9439SBrian Somers   if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO))
1694ff0f9439SBrian Somers     /* First auto link */
1695dd0645c5SBrian Somers     if (bundle->ncp.ipcp.peer_ip.s_addr == INADDR_ANY) {
1696ff0f9439SBrian Somers       log_Printf(LogWARN, "You must `set ifaddr' or `open' before"
1697ff0f9439SBrian Somers                  " changing mode to %s\n", mode2Nam(mode));
1698dd0645c5SBrian Somers       return 0;
1699dd0645c5SBrian Somers     }
1700dd0645c5SBrian Somers 
1701dd0645c5SBrian Somers   if (!datalink_SetMode(dl, mode))
1702dd0645c5SBrian Somers     return 0;
1703dd0645c5SBrian Somers 
1704ff0f9439SBrian Somers   if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO) &&
1705ff0f9439SBrian Somers       bundle->phase != PHASE_NETWORK)
1706ff0f9439SBrian Somers     /* First auto link, we need an interface */
1707dd0645c5SBrian Somers     ipcp_InterfaceUp(&bundle->ncp.ipcp);
1708dd0645c5SBrian Somers 
1709ab2de065SBrian Somers   /* Regenerate phys_type and adjust idle timer */
171004eaa58cSBrian Somers   bundle_LinksRemoved(bundle);
1711dd0645c5SBrian Somers 
1712dd0645c5SBrian Somers   return 1;
1713dd0645c5SBrian Somers }
17141384bd27SBrian Somers 
17151384bd27SBrian Somers void
17161384bd27SBrian Somers bundle_setsid(struct bundle *bundle, int holdsession)
17171384bd27SBrian Somers {
17181384bd27SBrian Somers   /*
17191384bd27SBrian Somers    * Lose the current session.  This means getting rid of our pid
17201384bd27SBrian Somers    * too so that the tty device will really go away, and any getty
17211384bd27SBrian Somers    * etc will be allowed to restart.
17221384bd27SBrian Somers    */
17231384bd27SBrian Somers   pid_t pid, orig;
17241384bd27SBrian Somers   int fds[2];
17251384bd27SBrian Somers   char done;
17261384bd27SBrian Somers   struct datalink *dl;
17271384bd27SBrian Somers 
1728e62ce959SBrian Somers   if (!holdsession && bundle_IsDead(bundle)) {
1729e62ce959SBrian Somers     /*
1730e62ce959SBrian Somers      * No need to lose our session after all... we're going away anyway
1731e62ce959SBrian Somers      *
1732e62ce959SBrian Somers      * We should really stop the timer and pause if holdsession is set and
1733e62ce959SBrian Somers      * the bundle's dead, but that leaves other resources lying about :-(
1734e62ce959SBrian Somers      */
1735e62ce959SBrian Somers     return;
1736e62ce959SBrian Somers   }
1737e62ce959SBrian Somers 
17381384bd27SBrian Somers   orig = getpid();
17391384bd27SBrian Somers   if (pipe(fds) == -1) {
17401384bd27SBrian Somers     log_Printf(LogERROR, "pipe: %s\n", strerror(errno));
17411384bd27SBrian Somers     return;
17421384bd27SBrian Somers   }
17431384bd27SBrian Somers   switch ((pid = fork())) {
17441384bd27SBrian Somers     case -1:
17451384bd27SBrian Somers       log_Printf(LogERROR, "fork: %s\n", strerror(errno));
17461384bd27SBrian Somers       close(fds[0]);
17471384bd27SBrian Somers       close(fds[1]);
17481384bd27SBrian Somers       return;
17491384bd27SBrian Somers     case 0:
17501384bd27SBrian Somers       close(fds[1]);
17514be0e57dSBrian Somers       read(fds[0], &done, 1);		/* uu_locks are mine ! */
17524be0e57dSBrian Somers       close(fds[0]);
17531384bd27SBrian Somers       if (pipe(fds) == -1) {
17541384bd27SBrian Somers         log_Printf(LogERROR, "pipe(2): %s\n", strerror(errno));
17551384bd27SBrian Somers         return;
17561384bd27SBrian Somers       }
17571384bd27SBrian Somers       switch ((pid = fork())) {
17581384bd27SBrian Somers         case -1:
1759a33b2ef7SBrian Somers           log_Printf(LogERROR, "fork(2): %s\n", strerror(errno));
17601384bd27SBrian Somers           close(fds[0]);
17611384bd27SBrian Somers           close(fds[1]);
17621384bd27SBrian Somers           return;
17631384bd27SBrian Somers         case 0:
17641384bd27SBrian Somers           close(fds[1]);
17654be0e57dSBrian Somers           bundle_LockTun(bundle);	/* update pid */
17664be0e57dSBrian Somers           read(fds[0], &done, 1);	/* uu_locks are mine ! */
17674be0e57dSBrian Somers           close(fds[0]);
17681384bd27SBrian Somers           setsid();
176906b47306SBrian Somers           bundle_ChangedPID(bundle);
177042df3c25SBrian Somers           log_Printf(LogDEBUG, "%ld -> %ld: %s session control\n",
177142df3c25SBrian Somers                      (long)orig, (long)getpid(),
17721384bd27SBrian Somers                      holdsession ? "Passed" : "Dropped");
17737e778f13SBrian Somers           timer_InitService(0);		/* Start the Timer Service */
17741384bd27SBrian Somers           break;
17751384bd27SBrian Somers         default:
17764be0e57dSBrian Somers           close(fds[0]);
17775d9e6103SBrian Somers           /* Give away all our physical locks (to the final process) */
17781384bd27SBrian Somers           for (dl = bundle->links; dl; dl = dl->next)
17791384bd27SBrian Somers             if (dl->state != DATALINK_CLOSED)
17805d9e6103SBrian Somers               physical_ChangedPid(dl->physical, pid);
17814be0e57dSBrian Somers           write(fds[1], "!", 1);	/* done */
17824be0e57dSBrian Somers           close(fds[1]);
17832cb305afSBrian Somers           _exit(0);
17841384bd27SBrian Somers           break;
17851384bd27SBrian Somers       }
17861384bd27SBrian Somers       break;
17871384bd27SBrian Somers     default:
17884be0e57dSBrian Somers       close(fds[0]);
17895d9e6103SBrian Somers       /* Give away all our physical locks (to the intermediate process) */
17901384bd27SBrian Somers       for (dl = bundle->links; dl; dl = dl->next)
17911384bd27SBrian Somers         if (dl->state != DATALINK_CLOSED)
17925d9e6103SBrian Somers           physical_ChangedPid(dl->physical, pid);
17934be0e57dSBrian Somers       write(fds[1], "!", 1);	/* done */
17944be0e57dSBrian Somers       close(fds[1]);
17951384bd27SBrian Somers       if (holdsession) {
17961384bd27SBrian Somers         int fd, status;
17971384bd27SBrian Somers 
17981384bd27SBrian Somers         timer_TermService();
17991384bd27SBrian Somers         signal(SIGPIPE, SIG_DFL);
18001384bd27SBrian Somers         signal(SIGALRM, SIG_DFL);
18011384bd27SBrian Somers         signal(SIGHUP, SIG_DFL);
18021384bd27SBrian Somers         signal(SIGTERM, SIG_DFL);
18031384bd27SBrian Somers         signal(SIGINT, SIG_DFL);
18041384bd27SBrian Somers         signal(SIGQUIT, SIG_DFL);
18051384bd27SBrian Somers         for (fd = getdtablesize(); fd >= 0; fd--)
18061384bd27SBrian Somers           close(fd);
18071384bd27SBrian Somers         /*
18081384bd27SBrian Somers          * Reap the intermediate process.  As we're not exiting but the
18091384bd27SBrian Somers          * intermediate is, we don't want it to become defunct.
18101384bd27SBrian Somers          */
18111384bd27SBrian Somers         waitpid(pid, &status, 0);
18128e7b8599SBrian Somers         /* Tweak our process arguments.... */
1813ebe96675SBrian Somers         SetTitle("session owner");
181468602c3eSBrian Somers #ifndef NOSUID
1815a19a5c02SBrian Somers         setuid(ID0realuid());
181668602c3eSBrian Somers #endif
18171384bd27SBrian Somers         /*
18181384bd27SBrian Somers          * Hang around for a HUP.  This should happen as soon as the
18198e7bd08eSBrian Somers          * ppp that we passed our ctty descriptor to closes it.
18208e7bd08eSBrian Somers          * NOTE: If this process dies, the passed descriptor becomes
18211384bd27SBrian Somers          *       invalid and will give a select() error by setting one
18221384bd27SBrian Somers          *       of the error fds, aborting the other ppp.  We don't
18231384bd27SBrian Somers          *       want that to happen !
18241384bd27SBrian Somers          */
18251384bd27SBrian Somers         pause();
18261384bd27SBrian Somers       }
18272cb305afSBrian Somers       _exit(0);
18281384bd27SBrian Somers       break;
18291384bd27SBrian Somers   }
18301384bd27SBrian Somers }
18319b5f8ffdSBrian Somers 
1832057f1760SBrian Somers unsigned
18339b5f8ffdSBrian Somers bundle_HighestState(struct bundle *bundle)
18349b5f8ffdSBrian Somers {
18359b5f8ffdSBrian Somers   struct datalink *dl;
1836057f1760SBrian Somers   unsigned result = DATALINK_CLOSED;
18379b5f8ffdSBrian Somers 
18389b5f8ffdSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
18399b5f8ffdSBrian Somers     if (result < dl->state)
18409b5f8ffdSBrian Somers       result = dl->state;
18419b5f8ffdSBrian Somers 
18429b5f8ffdSBrian Somers   return result;
18439b5f8ffdSBrian Somers }
1844991c2a7bSBrian Somers 
1845991c2a7bSBrian Somers int
1846991c2a7bSBrian Somers bundle_Exception(struct bundle *bundle, int fd)
1847991c2a7bSBrian Somers {
1848991c2a7bSBrian Somers   struct datalink *dl;
1849991c2a7bSBrian Somers 
1850991c2a7bSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
1851991c2a7bSBrian Somers     if (dl->physical->fd == fd) {
1852991c2a7bSBrian Somers       datalink_Down(dl, CLOSE_NORMAL);
1853991c2a7bSBrian Somers       return 1;
1854991c2a7bSBrian Somers     }
1855991c2a7bSBrian Somers 
1856991c2a7bSBrian Somers   return 0;
1857991c2a7bSBrian Somers }
18581d1fc017SBrian Somers 
18591d1fc017SBrian Somers void
186030949fd4SBrian Somers bundle_AdjustFilters(struct bundle *bundle, struct ncpaddr *local,
186130949fd4SBrian Somers                      struct ncpaddr *remote)
18621d1fc017SBrian Somers {
186330949fd4SBrian Somers   filter_AdjustAddr(&bundle->filter.in, local, remote, NULL);
186430949fd4SBrian Somers   filter_AdjustAddr(&bundle->filter.out, local, remote, NULL);
186530949fd4SBrian Somers   filter_AdjustAddr(&bundle->filter.dial, local, remote, NULL);
186630949fd4SBrian Somers   filter_AdjustAddr(&bundle->filter.alive, local, remote, NULL);
1867d568d6c4SBrian Somers }
1868d568d6c4SBrian Somers 
1869d568d6c4SBrian Somers void
187030949fd4SBrian Somers bundle_AdjustDNS(struct bundle *bundle)
1871d568d6c4SBrian Somers {
187230949fd4SBrian Somers   struct in_addr *dns = bundle->ncp.ipcp.ns.dns;
187330949fd4SBrian Somers 
1874d568d6c4SBrian Somers   filter_AdjustAddr(&bundle->filter.in, NULL, NULL, dns);
1875d568d6c4SBrian Somers   filter_AdjustAddr(&bundle->filter.out, NULL, NULL, dns);
1876d568d6c4SBrian Somers   filter_AdjustAddr(&bundle->filter.dial, NULL, NULL, dns);
1877d568d6c4SBrian Somers   filter_AdjustAddr(&bundle->filter.alive, NULL, NULL, dns);
18781d1fc017SBrian Somers }
1879ab2de065SBrian Somers 
1880ab2de065SBrian Somers void
1881ab2de065SBrian Somers bundle_CalculateBandwidth(struct bundle *bundle)
1882ab2de065SBrian Somers {
1883ab2de065SBrian Somers   struct datalink *dl;
18846301d506SBrian Somers   int sp, overhead, maxoverhead;
1885ab2de065SBrian Somers 
1886ab2de065SBrian Somers   bundle->bandwidth = 0;
1887c8b9fb53SBrian Somers   bundle->iface->mtu = 0;
18886301d506SBrian Somers   maxoverhead = 0;
18896301d506SBrian Somers 
18906301d506SBrian Somers   for (dl = bundle->links; dl; dl = dl->next) {
18916301d506SBrian Somers     overhead = ccp_MTUOverhead(&dl->physical->link.ccp);
18926301d506SBrian Somers     if (maxoverhead < overhead)
18936301d506SBrian Somers       maxoverhead = overhead;
1894ab2de065SBrian Somers     if (dl->state == DATALINK_OPEN) {
1895ab2de065SBrian Somers       if ((sp = dl->mp.bandwidth) == 0 &&
1896ab2de065SBrian Somers           (sp = physical_GetSpeed(dl->physical)) == 0)
1897ab2de065SBrian Somers         log_Printf(LogDEBUG, "%s: %s: Cannot determine bandwidth\n",
1898ab2de065SBrian Somers                    dl->name, dl->physical->name.full);
1899ab2de065SBrian Somers       else
1900ab2de065SBrian Somers         bundle->bandwidth += sp;
1901ab2de065SBrian Somers       if (!bundle->ncp.mp.active) {
1902c8b9fb53SBrian Somers         bundle->iface->mtu = dl->physical->link.lcp.his_mru;
1903ab2de065SBrian Somers         break;
1904ab2de065SBrian Somers       }
1905ab2de065SBrian Somers     }
19066301d506SBrian Somers   }
1907ab2de065SBrian Somers 
1908ab2de065SBrian Somers   if (bundle->bandwidth == 0)
1909ab2de065SBrian Somers     bundle->bandwidth = 115200;		/* Shrug */
1910ab2de065SBrian Somers 
19116301d506SBrian Somers   if (bundle->ncp.mp.active) {
1912c8b9fb53SBrian Somers     bundle->iface->mtu = bundle->ncp.mp.peer_mrru;
19136301d506SBrian Somers     overhead = ccp_MTUOverhead(&bundle->ncp.mp.link.ccp);
19146301d506SBrian Somers     if (maxoverhead < overhead)
19156301d506SBrian Somers       maxoverhead = overhead;
19166301d506SBrian Somers   } else if (!bundle->iface->mtu)
1917c8b9fb53SBrian Somers     bundle->iface->mtu = DEF_MRU;
1918ab2de065SBrian Somers 
1919ab2de065SBrian Somers #ifndef NORADIUS
192094d7be52SBrian Somers   if (bundle->radius.valid && bundle->radius.mtu &&
1921c8b9fb53SBrian Somers       bundle->radius.mtu < bundle->iface->mtu) {
1922ab2de065SBrian Somers     log_Printf(LogLCP, "Reducing MTU to radius value %lu\n",
1923ab2de065SBrian Somers                bundle->radius.mtu);
1924c8b9fb53SBrian Somers     bundle->iface->mtu = bundle->radius.mtu;
1925ab2de065SBrian Somers   }
1926ab2de065SBrian Somers #endif
1927ab2de065SBrian Somers 
19286301d506SBrian Somers   if (maxoverhead) {
1929057f1760SBrian Somers     log_Printf(LogLCP, "Reducing MTU from %lu to %lu (CCP requirement)\n",
19306301d506SBrian Somers                bundle->iface->mtu, bundle->iface->mtu - maxoverhead);
19316301d506SBrian Somers     bundle->iface->mtu -= maxoverhead;
19326301d506SBrian Somers   }
19336301d506SBrian Somers 
193494d7be52SBrian Somers   tun_configure(bundle);
193503a2501aSBrian Somers 
193603a2501aSBrian Somers   route_UpdateMTU(bundle);
1937ab2de065SBrian Somers }
1938ab2de065SBrian Somers 
1939ab2de065SBrian Somers void
1940ab2de065SBrian Somers bundle_AutoAdjust(struct bundle *bundle, int percent, int what)
1941ab2de065SBrian Somers {
1942ab2de065SBrian Somers   struct datalink *dl, *choice, *otherlinkup;
1943ab2de065SBrian Somers 
1944ab2de065SBrian Somers   choice = otherlinkup = NULL;
1945ab2de065SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
1946ab2de065SBrian Somers     if (dl->physical->type == PHYS_AUTO) {
1947ab2de065SBrian Somers       if (dl->state == DATALINK_OPEN) {
1948ab2de065SBrian Somers         if (what == AUTO_DOWN) {
1949ab2de065SBrian Somers           if (choice)
1950ab2de065SBrian Somers             otherlinkup = choice;
1951ab2de065SBrian Somers           choice = dl;
1952ab2de065SBrian Somers         }
1953ab2de065SBrian Somers       } else if (dl->state == DATALINK_CLOSED) {
1954ab2de065SBrian Somers         if (what == AUTO_UP) {
1955ab2de065SBrian Somers           choice = dl;
1956ab2de065SBrian Somers           break;
1957ab2de065SBrian Somers         }
1958ab2de065SBrian Somers       } else {
1959ab2de065SBrian Somers         /* An auto link in an intermediate state - forget it for the moment */
1960ab2de065SBrian Somers         choice = NULL;
1961ab2de065SBrian Somers         break;
1962ab2de065SBrian Somers       }
1963ab2de065SBrian Somers     } else if (dl->state == DATALINK_OPEN && what == AUTO_DOWN)
1964ab2de065SBrian Somers       otherlinkup = dl;
1965ab2de065SBrian Somers 
1966ab2de065SBrian Somers   if (choice) {
1967ab2de065SBrian Somers     if (what == AUTO_UP) {
1968ab2de065SBrian Somers       log_Printf(LogPHASE, "%d%% saturation -> Opening link ``%s''\n",
1969ab2de065SBrian Somers                  percent, choice->name);
1970ab2de065SBrian Somers       datalink_Up(choice, 1, 1);
1971a339e644SBrian Somers       mp_CheckAutoloadTimer(&bundle->ncp.mp);
1972ab2de065SBrian Somers     } else if (otherlinkup) {	/* Only bring the second-last link down */
1973ab2de065SBrian Somers       log_Printf(LogPHASE, "%d%% saturation -> Closing link ``%s''\n",
1974ab2de065SBrian Somers                  percent, choice->name);
1975d2f5232dSBrian Somers       datalink_Close(choice, CLOSE_STAYDOWN);
1976a339e644SBrian Somers       mp_CheckAutoloadTimer(&bundle->ncp.mp);
1977ab2de065SBrian Somers     }
1978ab2de065SBrian Somers   }
1979ab2de065SBrian Somers }
1980ab2de065SBrian Somers 
1981ab2de065SBrian Somers int
1982ab2de065SBrian Somers bundle_WantAutoloadTimer(struct bundle *bundle)
1983ab2de065SBrian Somers {
1984ab2de065SBrian Somers   struct datalink *dl;
1985ab2de065SBrian Somers   int autolink, opened;
1986ab2de065SBrian Somers 
1987ab2de065SBrian Somers   if (bundle->phase == PHASE_NETWORK) {
1988ab2de065SBrian Somers     for (autolink = opened = 0, dl = bundle->links; dl; dl = dl->next)
1989ab2de065SBrian Somers       if (dl->physical->type == PHYS_AUTO) {
1990ab2de065SBrian Somers         if (++autolink == 2 || (autolink == 1 && opened))
1991ab2de065SBrian Somers           /* Two auto links or one auto and one open in NETWORK phase */
1992ab2de065SBrian Somers           return 1;
1993ab2de065SBrian Somers       } else if (dl->state == DATALINK_OPEN) {
1994ab2de065SBrian Somers         opened++;
1995ab2de065SBrian Somers         if (autolink)
1996ab2de065SBrian Somers           /* One auto and one open link in NETWORK phase */
1997ab2de065SBrian Somers           return 1;
1998ab2de065SBrian Somers       }
1999ab2de065SBrian Somers   }
2000ab2de065SBrian Somers 
2001ab2de065SBrian Somers   return 0;
2002ab2de065SBrian Somers }
200306b47306SBrian Somers 
200406b47306SBrian Somers void
200506b47306SBrian Somers bundle_ChangedPID(struct bundle *bundle)
200606b47306SBrian Somers {
200706b47306SBrian Somers #ifdef TUNSIFPID
200806b47306SBrian Somers   ioctl(bundle->dev.fd, TUNSIFPID, 0);
200906b47306SBrian Somers #endif
201006b47306SBrian Somers }
201146df5aa7SBrian Somers 
201246df5aa7SBrian Somers int
201346df5aa7SBrian Somers bundle_Uptime(struct bundle *bundle)
201446df5aa7SBrian Somers {
201546df5aa7SBrian Somers   if (bundle->upat)
201646df5aa7SBrian Somers     return time(NULL) - bundle->upat;
201746df5aa7SBrian Somers 
201846df5aa7SBrian Somers   return 0;
201946df5aa7SBrian Somers }
2020