xref: /freebsd/usr.sbin/ppp/bundle.c (revision 42df3c252ee2d44a6dded9321cb8fa264585f990)
17a6f8720SBrian Somers /*-
27a6f8720SBrian Somers  * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
37a6f8720SBrian Somers  * All rights reserved.
47a6f8720SBrian Somers  *
57a6f8720SBrian Somers  * Redistribution and use in source and binary forms, with or without
67a6f8720SBrian Somers  * modification, are permitted provided that the following conditions
77a6f8720SBrian Somers  * are met:
87a6f8720SBrian Somers  * 1. Redistributions of source code must retain the above copyright
97a6f8720SBrian Somers  *    notice, this list of conditions and the following disclaimer.
107a6f8720SBrian Somers  * 2. Redistributions in binary form must reproduce the above copyright
117a6f8720SBrian Somers  *    notice, this list of conditions and the following disclaimer in the
127a6f8720SBrian Somers  *    documentation and/or other materials provided with the distribution.
137a6f8720SBrian Somers  *
147a6f8720SBrian Somers  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
157a6f8720SBrian Somers  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
167a6f8720SBrian Somers  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
177a6f8720SBrian Somers  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
187a6f8720SBrian Somers  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
197a6f8720SBrian Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
207a6f8720SBrian Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
217a6f8720SBrian Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
227a6f8720SBrian Somers  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
237a6f8720SBrian Somers  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
247a6f8720SBrian Somers  * SUCH DAMAGE.
257a6f8720SBrian Somers  *
2697d92980SPeter Wemm  * $FreeBSD$
277a6f8720SBrian Somers  */
287a6f8720SBrian Somers 
291384bd27SBrian Somers #include <sys/param.h>
307a6f8720SBrian Somers #include <sys/socket.h>
317a6f8720SBrian Somers #include <netinet/in.h>
327a6f8720SBrian Somers #include <net/if.h>
333a7b6d76SBrian Somers #include <net/if_tun.h>		/* For TUNS* ioctls */
347a6f8720SBrian Somers #include <net/route.h>
35eaa4df37SBrian Somers #include <netinet/in_systm.h>
36eaa4df37SBrian Somers #include <netinet/ip.h>
371fa665f5SBrian Somers #include <sys/un.h>
387a6f8720SBrian Somers 
397a6f8720SBrian Somers #include <errno.h>
407a6f8720SBrian Somers #include <fcntl.h>
41cf0a3940SBrian Somers #ifdef __OpenBSD__
42cf0a3940SBrian Somers #include <util.h>
43cf0a3940SBrian Somers #else
44cf0a3940SBrian Somers #include <libutil.h>
45cf0a3940SBrian Somers #endif
4647723d29SBrian Somers #include <paths.h>
477a6f8720SBrian Somers #include <stdio.h>
486f384573SBrian Somers #include <stdlib.h>
497a6f8720SBrian Somers #include <string.h>
5096c9bb21SBrian Somers #include <sys/uio.h>
5154cd8e13SBrian Somers #include <sys/wait.h>
527a6f8720SBrian Somers #include <termios.h>
537a6f8720SBrian Somers #include <unistd.h>
547a6f8720SBrian Somers 
555d9e6103SBrian Somers #include "layer.h"
56c9e11a11SBrian Somers #include "defs.h"
577a6f8720SBrian Somers #include "command.h"
587a6f8720SBrian Somers #include "mbuf.h"
597a6f8720SBrian Somers #include "log.h"
607a6f8720SBrian Somers #include "id.h"
617a6f8720SBrian Somers #include "timer.h"
627a6f8720SBrian Somers #include "fsm.h"
637a6f8720SBrian Somers #include "iplist.h"
64879ed6faSBrian Somers #include "lqr.h"
65455aabc3SBrian Somers #include "hdlc.h"
667a6f8720SBrian Somers #include "throughput.h"
67eaa4df37SBrian Somers #include "slcompress.h"
6830949fd4SBrian Somers #include "ncpaddr.h"
6930949fd4SBrian Somers #include "ip.h"
707a6f8720SBrian Somers #include "ipcp.h"
715ca5389aSBrian Somers #include "filter.h"
722f786681SBrian Somers #include "descriptor.h"
737a6f8720SBrian Somers #include "route.h"
747a6f8720SBrian Somers #include "lcp.h"
757a6f8720SBrian Somers #include "ccp.h"
763b0f8d2eSBrian Somers #include "link.h"
773b0f8d2eSBrian Somers #include "mp.h"
78972a1bcfSBrian Somers #ifndef NORADIUS
79972a1bcfSBrian Somers #include "radius.h"
80972a1bcfSBrian Somers #endif
8130949fd4SBrian Somers #include "ipv6cp.h"
8230949fd4SBrian Somers #include "ncp.h"
833b0f8d2eSBrian Somers #include "bundle.h"
84455aabc3SBrian Somers #include "async.h"
85455aabc3SBrian Somers #include "physical.h"
86455aabc3SBrian Somers #include "auth.h"
875d9e6103SBrian Somers #include "proto.h"
88455aabc3SBrian Somers #include "chap.h"
89455aabc3SBrian Somers #include "tun.h"
9085b542cfSBrian Somers #include "prompt.h"
913006ec67SBrian Somers #include "chat.h"
9292b09558SBrian Somers #include "cbcp.h"
933006ec67SBrian Somers #include "datalink.h"
948fa6ebe4SBrian Somers #include "iface.h"
9574457d3dSBrian Somers #include "server.h"
96971abb29SBrian Somers #include "probe.h"
97fb11a9c2SBrian Somers #ifndef NODES
98019d32bfSBrian Somers #include "mppe.h"
9903a2501aSBrian Somers #endif
1007a6f8720SBrian Somers 
10191cbd2eeSBrian Somers #define SCATTER_SEGMENTS 7  /* version, datalink, name, physical,
10291cbd2eeSBrian Somers                                throughput, throughput, device       */
10387c3786eSBrian Somers 
1042cb305afSBrian Somers #define SEND_MAXFD 3        /* Max file descriptors passed through
10587c3786eSBrian Somers                                the local domain socket              */
10696c9bb21SBrian Somers 
10704eaa58cSBrian Somers static int bundle_RemainingIdleTime(struct bundle *);
10804eaa58cSBrian Somers 
109182c898aSBrian Somers static const char * const PhaseNames[] = {
110455aabc3SBrian Somers   "Dead", "Establish", "Authenticate", "Network", "Terminate"
111455aabc3SBrian Somers };
112455aabc3SBrian Somers 
113455aabc3SBrian Somers const char *
114455aabc3SBrian Somers bundle_PhaseName(struct bundle *bundle)
1157a6f8720SBrian Somers {
116455aabc3SBrian Somers   return bundle->phase <= PHASE_TERMINATE ?
117455aabc3SBrian Somers     PhaseNames[bundle->phase] : "unknown";
1187a6f8720SBrian Somers }
1197a6f8720SBrian Somers 
120455aabc3SBrian Somers void
1215563ebdeSBrian Somers bundle_NewPhase(struct bundle *bundle, u_int new)
122455aabc3SBrian Somers {
123aef795ccSBrian Somers   if (new == bundle->phase)
124aef795ccSBrian Somers     return;
125aef795ccSBrian Somers 
126e2ebb036SBrian Somers   if (new <= PHASE_TERMINATE)
127dd7e2610SBrian Somers     log_Printf(LogPHASE, "bundle: %s\n", PhaseNames[new]);
1287a6f8720SBrian Somers 
129455aabc3SBrian Somers   switch (new) {
130455aabc3SBrian Somers   case PHASE_DEAD:
131455aabc3SBrian Somers     bundle->phase = new;
132fb11a9c2SBrian Somers #ifndef NODES
133019d32bfSBrian Somers     MPPE_MasterKeyValid = 0;
13464602637SBrian Somers #endif
135019d32bfSBrian Somers     log_DisplayPrompts();
136455aabc3SBrian Somers     break;
137455aabc3SBrian Somers 
138455aabc3SBrian Somers   case PHASE_ESTABLISH:
139455aabc3SBrian Somers     bundle->phase = new;
140455aabc3SBrian Somers     break;
141455aabc3SBrian Somers 
142455aabc3SBrian Somers   case PHASE_AUTHENTICATE:
143455aabc3SBrian Somers     bundle->phase = new;
1440f2f3eb3SBrian Somers     log_DisplayPrompts();
145455aabc3SBrian Somers     break;
146455aabc3SBrian Somers 
147455aabc3SBrian Somers   case PHASE_NETWORK:
14830949fd4SBrian Somers     if (ncp_fsmStart(&bundle->ncp, bundle)) {
149673903ecSBrian Somers       bundle->phase = new;
1500f2f3eb3SBrian Somers       log_DisplayPrompts();
15130949fd4SBrian Somers     } else {
15230949fd4SBrian Somers       log_Printf(LogPHASE, "bundle: All NCPs are disabled\n");
15330949fd4SBrian Somers       bundle_Close(bundle, NULL, CLOSE_STAYDOWN);
15430949fd4SBrian Somers     }
155673903ecSBrian Somers     break;
156455aabc3SBrian Somers 
157455aabc3SBrian Somers   case PHASE_TERMINATE:
158455aabc3SBrian Somers     bundle->phase = new;
159673903ecSBrian Somers     mp_Down(&bundle->ncp.mp);
1600f2f3eb3SBrian Somers     log_DisplayPrompts();
161455aabc3SBrian Somers     break;
1627a6f8720SBrian Somers   }
1637a6f8720SBrian Somers }
1647a6f8720SBrian Somers 
1656d666775SBrian Somers static void
1666d666775SBrian Somers bundle_LayerStart(void *v, struct fsm *fp)
1677a6f8720SBrian Somers {
1683006ec67SBrian Somers   /* The given FSM is about to start up ! */
1697a6f8720SBrian Somers }
1707a6f8720SBrian Somers 
1715cf4388bSBrian Somers 
172b42135deSBrian Somers void
1735cf4388bSBrian Somers bundle_Notify(struct bundle *bundle, char c)
1745cf4388bSBrian Somers {
1755cf4388bSBrian Somers   if (bundle->notify.fd != -1) {
176b42135deSBrian Somers     int ret;
177b42135deSBrian Somers 
178b42135deSBrian Somers     ret = write(bundle->notify.fd, &c, 1);
179b42135deSBrian Somers     if (c != EX_REDIAL && c != EX_RECONNECT) {
180b42135deSBrian Somers       if (ret == 1)
181b42135deSBrian Somers         log_Printf(LogCHAT, "Parent notified of %s\n",
1825a83ad1eSBrian Somers                    c == EX_NORMAL ? "success" : "failure");
1835cf4388bSBrian Somers       else
184b42135deSBrian Somers         log_Printf(LogERROR, "Failed to notify parent of success\n");
1855cf4388bSBrian Somers       close(bundle->notify.fd);
1865cf4388bSBrian Somers       bundle->notify.fd = -1;
187b42135deSBrian Somers     } else if (ret == 1)
188b42135deSBrian Somers       log_Printf(LogCHAT, "Parent notified of %s\n", ex_desc(c));
189b42135deSBrian Somers     else
190b42135deSBrian Somers       log_Printf(LogERROR, "Failed to notify parent of %s\n", ex_desc(c));
1915cf4388bSBrian Somers   }
1925cf4388bSBrian Somers }
1933b0f8d2eSBrian Somers 
1946d666775SBrian Somers static void
1956f8e9f0aSBrian Somers bundle_ClearQueues(void *v)
1966f8e9f0aSBrian Somers {
1976f8e9f0aSBrian Somers   struct bundle *bundle = (struct bundle *)v;
1986f8e9f0aSBrian Somers   struct datalink *dl;
1996f8e9f0aSBrian Somers 
2006f8e9f0aSBrian Somers   log_Printf(LogPHASE, "Clearing choked output queue\n");
2016f8e9f0aSBrian Somers   timer_Stop(&bundle->choked.timer);
2026f8e9f0aSBrian Somers 
2036f8e9f0aSBrian Somers   /*
2046f8e9f0aSBrian Somers    * Emergency time:
2056f8e9f0aSBrian Somers    *
2066f8e9f0aSBrian Somers    * We've had a full queue for PACKET_DEL_SECS seconds without being
2076f8e9f0aSBrian Somers    * able to get rid of any of the packets.  We've probably given up
2086f8e9f0aSBrian Somers    * on the redials at this point, and the queued data has almost
2096f8e9f0aSBrian Somers    * definitely been timed out by the layer above.  As this is preventing
2106f8e9f0aSBrian Somers    * us from reading the TUN_NAME device (we don't want to buffer stuff
2116f8e9f0aSBrian Somers    * indefinitely), we may as well nuke this data and start with a clean
2126f8e9f0aSBrian Somers    * slate !
2136f8e9f0aSBrian Somers    *
2146f8e9f0aSBrian Somers    * Unfortunately, this has the side effect of shafting any compression
2156f8e9f0aSBrian Somers    * dictionaries in use (causing the relevant RESET_REQ/RESET_ACK).
2166f8e9f0aSBrian Somers    */
2176f8e9f0aSBrian Somers 
21830949fd4SBrian Somers   ncp_DeleteQueues(&bundle->ncp);
2196f8e9f0aSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
2206f8e9f0aSBrian Somers     physical_DeleteQueue(dl->physical);
2216f8e9f0aSBrian Somers }
2226f8e9f0aSBrian Somers 
2236f8e9f0aSBrian Somers static void
224ff0f9439SBrian Somers bundle_LinkAdded(struct bundle *bundle, struct datalink *dl)
225ff0f9439SBrian Somers {
226ff0f9439SBrian Somers   bundle->phys_type.all |= dl->physical->type;
227ff0f9439SBrian Somers   if (dl->state == DATALINK_OPEN)
228ff0f9439SBrian Somers     bundle->phys_type.open |= dl->physical->type;
229ff0f9439SBrian Somers 
230bf1eaec5SBrian Somers #ifndef NORADIUS
231bf1eaec5SBrian Somers   if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL))
232bf1eaec5SBrian Somers       != bundle->phys_type.open && bundle->session.timer.state == TIMER_STOPPED)
233bf1eaec5SBrian Somers     if (bundle->radius.sessiontime)
234bf1eaec5SBrian Somers       bundle_StartSessionTimer(bundle, 0);
235bf1eaec5SBrian Somers #endif
236bf1eaec5SBrian Somers 
237ff0f9439SBrian Somers   if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL))
238ff0f9439SBrian Somers       != bundle->phys_type.open && bundle->idle.timer.state == TIMER_STOPPED)
239ff0f9439SBrian Somers     /* We may need to start our idle timer */
2400a4b6c5cSBrian Somers     bundle_StartIdleTimer(bundle, 0);
241ff0f9439SBrian Somers }
242ff0f9439SBrian Somers 
24392b09558SBrian Somers void
244ff0f9439SBrian Somers bundle_LinksRemoved(struct bundle *bundle)
245ff0f9439SBrian Somers {
246ff0f9439SBrian Somers   struct datalink *dl;
247ff0f9439SBrian Somers 
248ff0f9439SBrian Somers   bundle->phys_type.all = bundle->phys_type.open = 0;
249ff0f9439SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
250ff0f9439SBrian Somers     bundle_LinkAdded(bundle, dl);
251ff0f9439SBrian Somers 
252ab2de065SBrian Somers   bundle_CalculateBandwidth(bundle);
253ab2de065SBrian Somers   mp_CheckAutoloadTimer(&bundle->ncp.mp);
254ab2de065SBrian Somers 
255ff0f9439SBrian Somers   if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL))
256bf1eaec5SBrian Somers       == bundle->phys_type.open) {
257bf1eaec5SBrian Somers #ifndef NORADIUS
258bf1eaec5SBrian Somers     if (bundle->radius.sessiontime)
259bf1eaec5SBrian Somers       bundle_StopSessionTimer(bundle);
260bf1eaec5SBrian Somers #endif
261ff0f9439SBrian Somers     bundle_StopIdleTimer(bundle);
262ff0f9439SBrian Somers    }
263bf1eaec5SBrian Somers }
26404eaa58cSBrian Somers 
26504eaa58cSBrian Somers static void
2666f384573SBrian Somers bundle_LayerUp(void *v, struct fsm *fp)
2677a6f8720SBrian Somers {
2683006ec67SBrian Somers   /*
2693006ec67SBrian Somers    * The given fsm is now up
270ab2de065SBrian Somers    * If it's an LCP, adjust our phys_mode.open value and check the
271ab2de065SBrian Somers    * autoload timer.
272ab2de065SBrian Somers    * If it's the first NCP, calculate our bandwidth
273dade2407SBrian Somers    * If it's the first NCP, set our ``upat'' time
2743b0f8d2eSBrian Somers    * If it's the first NCP, start the idle timer.
275ab2de065SBrian Somers    * If it's an NCP, tell our -background parent to go away.
276ab2de065SBrian Somers    * If it's the first NCP, start the autoload timer
2773006ec67SBrian Somers    */
2786f384573SBrian Somers   struct bundle *bundle = (struct bundle *)v;
2796d666775SBrian Somers 
2805563ebdeSBrian Somers   if (fp->proto == PROTO_LCP) {
281ff0f9439SBrian Somers     struct physical *p = link2physical(fp->link);
282ff0f9439SBrian Somers 
283ff0f9439SBrian Somers     bundle_LinkAdded(bundle, p->dl);
284ab2de065SBrian Somers     mp_CheckAutoloadTimer(&bundle->ncp.mp);
28530949fd4SBrian Somers   } else if (isncp(fp->proto)) {
28630949fd4SBrian Somers     if (ncp_LayersOpen(&fp->bundle->ncp) == 1) {
287ab2de065SBrian Somers       bundle_CalculateBandwidth(fp->bundle);
288dade2407SBrian Somers       time(&bundle->upat);
289bf1eaec5SBrian Somers #ifndef NORADIUS
290bf1eaec5SBrian Somers       if (bundle->radius.sessiontime)
291bf1eaec5SBrian Somers         bundle_StartSessionTimer(bundle, 0);
292bf1eaec5SBrian Somers #endif
2930a4b6c5cSBrian Somers       bundle_StartIdleTimer(bundle, 0);
294ab2de065SBrian Somers       mp_CheckAutoloadTimer(&fp->bundle->ncp.mp);
29530949fd4SBrian Somers     }
29630949fd4SBrian Somers     bundle_Notify(bundle, EX_NORMAL);
2976301d506SBrian Somers   } else if (fp->proto == PROTO_CCP)
2986301d506SBrian Somers     bundle_CalculateBandwidth(fp->bundle);	/* Against ccp_MTUOverhead */
299ab886ad0SBrian Somers }
3007a6f8720SBrian Somers 
3016d666775SBrian Somers static void
3026d666775SBrian Somers bundle_LayerDown(void *v, struct fsm *fp)
3036d666775SBrian Somers {
3046d666775SBrian Somers   /*
3056d666775SBrian Somers    * The given FSM has been told to come down.
306ab886ad0SBrian Somers    * If it's our last NCP, stop the idle timer.
307dade2407SBrian Somers    * If it's our last NCP, clear our ``upat'' value.
308ab2de065SBrian Somers    * If it's our last NCP, stop the autoload timer
309ff0f9439SBrian Somers    * If it's an LCP, adjust our phys_type.open value and any timers.
31086b1f0d7SBrian Somers    * If it's an LCP and we're in multilink mode, adjust our tun
311c8f30703SBrian Somers    * If it's the last LCP, down all NCPs
31286b1f0d7SBrian Somers    * speed and make sure our minimum sequence number is adjusted.
3136d666775SBrian Somers    */
314ab886ad0SBrian Somers 
315ab886ad0SBrian Somers   struct bundle *bundle = (struct bundle *)v;
316ab886ad0SBrian Somers 
31730949fd4SBrian Somers   if (isncp(fp->proto)) {
31830949fd4SBrian Somers     if (ncp_LayersOpen(&fp->bundle->ncp) == 0) {
319bf1eaec5SBrian Somers #ifndef NORADIUS
320bf1eaec5SBrian Somers       if (bundle->radius.sessiontime)
321bf1eaec5SBrian Somers         bundle_StopSessionTimer(bundle);
322bf1eaec5SBrian Somers #endif
323ab886ad0SBrian Somers       bundle_StopIdleTimer(bundle);
324dade2407SBrian Somers       bundle->upat = 0;
325ab2de065SBrian Somers       mp_StopAutoloadTimer(&bundle->ncp.mp);
32630949fd4SBrian Somers     }
327ab2de065SBrian Somers   } else if (fp->proto == PROTO_LCP) {
3283b0f8d2eSBrian Somers     struct datalink *dl;
32986b1f0d7SBrian Somers     struct datalink *lost;
330c8f30703SBrian Somers     int others_active;
331c8f30703SBrian Somers 
332c8f30703SBrian Somers     bundle_LinksRemoved(bundle);  /* adjust timers & phys_type values */
3333b0f8d2eSBrian Somers 
33486b1f0d7SBrian Somers     lost = NULL;
335c8f30703SBrian Somers     others_active = 0;
336c8f30703SBrian Somers     for (dl = bundle->links; dl; dl = dl->next) {
33786b1f0d7SBrian Somers       if (fp == &dl->physical->link.lcp.fsm)
33886b1f0d7SBrian Somers         lost = dl;
339c8f30703SBrian Somers       else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP)
340c8f30703SBrian Somers         others_active++;
341c8f30703SBrian Somers     }
34286b1f0d7SBrian Somers 
343c8f30703SBrian Somers     if (bundle->ncp.mp.active) {
344ab2de065SBrian Somers       bundle_CalculateBandwidth(bundle);
34586b1f0d7SBrian Somers 
34686b1f0d7SBrian Somers       if (lost)
34786b1f0d7SBrian Somers         mp_LinkLost(&bundle->ncp.mp, lost);
34886b1f0d7SBrian Somers       else
349a33b2ef7SBrian Somers         log_Printf(LogALERT, "Oops, lost an unrecognised datalink (%s) !\n",
35086b1f0d7SBrian Somers                    fp->link->name);
3513b0f8d2eSBrian Somers     }
352c8f30703SBrian Somers 
353356bf92dSBrian Somers     if (!others_active) {
354c8f30703SBrian Somers       /* Down the NCPs.  We don't expect to get fsm_Close()d ourself ! */
35530949fd4SBrian Somers       ncp2initial(&bundle->ncp);
356356bf92dSBrian Somers       mp_Down(&bundle->ncp.mp);
357356bf92dSBrian Somers     }
3586d666775SBrian Somers   }
359ff0f9439SBrian Somers }
3606d666775SBrian Somers 
3616d666775SBrian Somers static void
3626d666775SBrian Somers bundle_LayerFinish(void *v, struct fsm *fp)
3636d666775SBrian Somers {
3646d666775SBrian Somers   /* The given fsm is now down (fp cannot be NULL)
3656d666775SBrian Somers    *
366dd7e2610SBrian Somers    * If it's the last NCP, fsm_Close all LCPs
367356bf92dSBrian Somers    * If it's the last NCP, bring any MP layer down
3686d666775SBrian Somers    */
3696d666775SBrian Somers 
3706d666775SBrian Somers   struct bundle *bundle = (struct bundle *)v;
3716d666775SBrian Somers   struct datalink *dl;
3726d666775SBrian Somers 
37330949fd4SBrian Somers   if (isncp(fp->proto) && !ncp_LayersUnfinished(&bundle->ncp)) {
37426afeaa2SBrian Somers     if (bundle_Phase(bundle) != PHASE_DEAD)
37525092092SBrian Somers       bundle_NewPhase(bundle, PHASE_TERMINATE);
3766d666775SBrian Somers     for (dl = bundle->links; dl; dl = dl->next)
377c8f30703SBrian Somers       if (dl->state == DATALINK_OPEN)
378d4d5d2f8SBrian Somers         datalink_Close(dl, CLOSE_STAYDOWN);
37909206a6fSBrian Somers     fsm2initial(fp);
380356bf92dSBrian Somers     mp_Down(&bundle->ncp.mp);
3813b0f8d2eSBrian Somers   }
3823b0f8d2eSBrian Somers }
3836d666775SBrian Somers 
3847a6f8720SBrian Somers void
3859c81b87dSBrian Somers bundle_Close(struct bundle *bundle, const char *name, int how)
3867a6f8720SBrian Somers {
387455aabc3SBrian Somers   /*
3883006ec67SBrian Somers    * Please close the given datalink.
389dd7e2610SBrian Somers    * If name == NULL or name is the last datalink, fsm_Close all NCPs
3906f384573SBrian Somers    * (except our MP)
3913b0f8d2eSBrian Somers    * If it isn't the last datalink, just Close that datalink.
392455aabc3SBrian Somers    */
3937a6f8720SBrian Somers 
3943b0f8d2eSBrian Somers   struct datalink *dl, *this_dl;
3953b0f8d2eSBrian Somers   int others_active;
3963006ec67SBrian Somers 
3973b0f8d2eSBrian Somers   others_active = 0;
3983b0f8d2eSBrian Somers   this_dl = NULL;
3993b0f8d2eSBrian Somers 
4003b0f8d2eSBrian Somers   for (dl = bundle->links; dl; dl = dl->next) {
4013b0f8d2eSBrian Somers     if (name && !strcasecmp(name, dl->name))
4023b0f8d2eSBrian Somers       this_dl = dl;
4033b0f8d2eSBrian Somers     if (name == NULL || this_dl == dl) {
4049c81b87dSBrian Somers       switch (how) {
4059c81b87dSBrian Somers         case CLOSE_LCP:
4069c81b87dSBrian Somers           datalink_DontHangup(dl);
4072fc2f705SBrian Somers           break;
4089c81b87dSBrian Somers         case CLOSE_STAYDOWN:
4093006ec67SBrian Somers           datalink_StayDown(dl);
4109c81b87dSBrian Somers           break;
4119c81b87dSBrian Somers       }
4123b0f8d2eSBrian Somers     } else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP)
4133b0f8d2eSBrian Somers       others_active++;
4143b0f8d2eSBrian Somers   }
4153b0f8d2eSBrian Somers 
4163b0f8d2eSBrian Somers   if (name && this_dl == NULL) {
417dd7e2610SBrian Somers     log_Printf(LogWARN, "%s: Invalid datalink name\n", name);
4183b0f8d2eSBrian Somers     return;
4193b0f8d2eSBrian Somers   }
4203b0f8d2eSBrian Somers 
4213b0f8d2eSBrian Somers   if (!others_active) {
422bf1eaec5SBrian Somers #ifndef NORADIUS
423bf1eaec5SBrian Somers     if (bundle->radius.sessiontime)
424bf1eaec5SBrian Somers       bundle_StopSessionTimer(bundle);
425bf1eaec5SBrian Somers #endif
42604eaa58cSBrian Somers     bundle_StopIdleTimer(bundle);
42730949fd4SBrian Somers     if (ncp_LayersUnfinished(&bundle->ncp))
42830949fd4SBrian Somers       ncp_Close(&bundle->ncp);
4293b0f8d2eSBrian Somers     else {
43030949fd4SBrian Somers       ncp2initial(&bundle->ncp);
431356bf92dSBrian Somers       mp_Down(&bundle->ncp.mp);
432d345321bSBrian Somers       for (dl = bundle->links; dl; dl = dl->next)
4339c81b87dSBrian Somers         datalink_Close(dl, how);
4347a6f8720SBrian Somers     }
4353b0f8d2eSBrian Somers   } else if (this_dl && this_dl->state != DATALINK_CLOSED &&
4363b0f8d2eSBrian Somers              this_dl->state != DATALINK_HANGUP)
4379c81b87dSBrian Somers     datalink_Close(this_dl, how);
438d2fd8d77SBrian Somers }
4397a6f8720SBrian Somers 
4401bc9b5baSBrian Somers void
441899011c4SBrian Somers bundle_Down(struct bundle *bundle, int how)
4421bc9b5baSBrian Somers {
4431bc9b5baSBrian Somers   struct datalink *dl;
4441bc9b5baSBrian Somers 
4451bc9b5baSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
446899011c4SBrian Somers     datalink_Down(dl, how);
4471bc9b5baSBrian Somers }
4481bc9b5baSBrian Somers 
4492f786681SBrian Somers static int
450f013f33eSBrian Somers bundle_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
4512f786681SBrian Somers {
4522f786681SBrian Somers   struct bundle *bundle = descriptor2bundle(d);
4532f786681SBrian Somers   struct datalink *dl;
45426af0ae9SBrian Somers   int result, nlinks;
4556c1d6731SBrian Somers   u_short ifqueue;
45626af0ae9SBrian Somers   size_t queued;
4572f786681SBrian Somers 
4582f786681SBrian Somers   result = 0;
4592f786681SBrian Somers 
460078c562eSBrian Somers   /* If there are aren't many packets queued, look for some more. */
46104eaa58cSBrian Somers   for (nlinks = 0, dl = bundle->links; dl; dl = dl->next)
46204eaa58cSBrian Somers     nlinks++;
46304eaa58cSBrian Somers 
46404eaa58cSBrian Somers   if (nlinks) {
46530949fd4SBrian Somers     queued = r ? ncp_FillPhysicalQueues(&bundle->ncp, bundle) :
46630949fd4SBrian Somers                  ncp_QueueLen(&bundle->ncp);
46704eaa58cSBrian Somers 
468ff0f9439SBrian Somers     if (r && (bundle->phase == PHASE_NETWORK ||
469ff0f9439SBrian Somers               bundle->phys_type.all & PHYS_AUTO)) {
47004eaa58cSBrian Somers       /* enough surplus so that we can tell if we're getting swamped */
4716c1d6731SBrian Somers       ifqueue = nlinks > bundle->cfg.ifqueue ? nlinks : bundle->cfg.ifqueue;
4726c1d6731SBrian Somers       if (queued < ifqueue) {
47304eaa58cSBrian Somers         /* Not enough - select() for more */
4746f8e9f0aSBrian Somers         if (bundle->choked.timer.state == TIMER_RUNNING)
4756f8e9f0aSBrian Somers           timer_Stop(&bundle->choked.timer);	/* Not needed any more */
47604eaa58cSBrian Somers         FD_SET(bundle->dev.fd, r);
477faefde08SBrian Somers         if (*n < bundle->dev.fd + 1)
478faefde08SBrian Somers           *n = bundle->dev.fd + 1;
4791384bd27SBrian Somers         log_Printf(LogTIMER, "%s: fdset(r) %d\n", TUN_NAME, bundle->dev.fd);
480078c562eSBrian Somers         result++;
4816f8e9f0aSBrian Somers       } else if (bundle->choked.timer.state == TIMER_STOPPED) {
4826f8e9f0aSBrian Somers         bundle->choked.timer.func = bundle_ClearQueues;
4836f8e9f0aSBrian Somers         bundle->choked.timer.name = "output choke";
4846f8e9f0aSBrian Somers         bundle->choked.timer.load = bundle->cfg.choked.timeout * SECTICKS;
4856f8e9f0aSBrian Somers         bundle->choked.timer.arg = bundle;
4866f8e9f0aSBrian Somers         timer_Start(&bundle->choked.timer);
487078c562eSBrian Somers       }
48804eaa58cSBrian Somers     }
48904eaa58cSBrian Somers   }
490078c562eSBrian Somers 
491f0cdd9c0SBrian Somers #ifndef NORADIUS
492f0cdd9c0SBrian Somers   result += descriptor_UpdateSet(&bundle->radius.desc, r, w, e, n);
493f0cdd9c0SBrian Somers #endif
494f0cdd9c0SBrian Somers 
49571555108SBrian Somers   /* Which links need a select() ? */
49671555108SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
49771555108SBrian Somers     result += descriptor_UpdateSet(&dl->desc, r, w, e, n);
49871555108SBrian Somers 
499ea722969SBrian Somers   /*
500ea722969SBrian Somers    * This *MUST* be called after the datalink UpdateSet()s as it
50104eaa58cSBrian Somers    * might be ``holding'' one of the datalinks (death-row) and
5028e7bd08eSBrian Somers    * wants to be able to de-select() it from the descriptor set.
503ea722969SBrian Somers    */
5040f2f3eb3SBrian Somers   result += descriptor_UpdateSet(&bundle->ncp.mp.server.desc, r, w, e, n);
505ea722969SBrian Somers 
5062f786681SBrian Somers   return result;
5072f786681SBrian Somers }
5082f786681SBrian Somers 
5092f786681SBrian Somers static int
510f013f33eSBrian Somers bundle_IsSet(struct fdescriptor *d, const fd_set *fdset)
5112f786681SBrian Somers {
5122f786681SBrian Somers   struct bundle *bundle = descriptor2bundle(d);
5132f786681SBrian Somers   struct datalink *dl;
5142f786681SBrian Somers 
5152f786681SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
5162f786681SBrian Somers     if (descriptor_IsSet(&dl->desc, fdset))
5172f786681SBrian Somers       return 1;
5182f786681SBrian Somers 
519f0cdd9c0SBrian Somers #ifndef NORADIUS
520f0cdd9c0SBrian Somers   if (descriptor_IsSet(&bundle->radius.desc, fdset))
521f0cdd9c0SBrian Somers     return 1;
522f0cdd9c0SBrian Somers #endif
523f0cdd9c0SBrian Somers 
524332b9de0SBrian Somers   if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset))
525332b9de0SBrian Somers     return 1;
526332b9de0SBrian Somers 
527faefde08SBrian Somers   return FD_ISSET(bundle->dev.fd, fdset);
5282f786681SBrian Somers }
5292f786681SBrian Somers 
5302f786681SBrian Somers static void
531f013f33eSBrian Somers bundle_DescriptorRead(struct fdescriptor *d, struct bundle *bundle,
5322f786681SBrian Somers                       const fd_set *fdset)
5332f786681SBrian Somers {
5342f786681SBrian Somers   struct datalink *dl;
5350a4b6c5cSBrian Somers   unsigned secs;
53630949fd4SBrian Somers   u_int32_t af;
5372f786681SBrian Somers 
538ea722969SBrian Somers   if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset))
539ea722969SBrian Somers     descriptor_Read(&bundle->ncp.mp.server.desc, bundle, fdset);
540ea722969SBrian Somers 
5412f786681SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
5422f786681SBrian Somers     if (descriptor_IsSet(&dl->desc, fdset))
5432f786681SBrian Somers       descriptor_Read(&dl->desc, bundle, fdset);
544b6217683SBrian Somers 
545f0cdd9c0SBrian Somers #ifndef NORADIUS
546f0cdd9c0SBrian Somers   if (descriptor_IsSet(&bundle->radius.desc, fdset))
547f0cdd9c0SBrian Somers     descriptor_Read(&bundle->radius.desc, bundle, fdset);
548f0cdd9c0SBrian Somers #endif
549f0cdd9c0SBrian Somers 
550faefde08SBrian Somers   if (FD_ISSET(bundle->dev.fd, fdset)) {
551078c562eSBrian Somers     struct tun_data tun;
552078c562eSBrian Somers     int n, pri;
553c1d57c38SBrian Somers     u_char *data;
5543a7b6d76SBrian Somers     size_t sz;
5553a7b6d76SBrian Somers 
5563a7b6d76SBrian Somers     if (bundle->dev.header) {
557c1d57c38SBrian Somers       data = (u_char *)&tun;
5583a7b6d76SBrian Somers       sz = sizeof tun;
5593a7b6d76SBrian Somers     } else {
5603a7b6d76SBrian Somers       data = tun.data;
5613a7b6d76SBrian Somers       sz = sizeof tun.data;
5623a7b6d76SBrian Somers     }
563078c562eSBrian Somers 
564078c562eSBrian Somers     /* something to read from tun */
5653a7b6d76SBrian Somers 
5663a7b6d76SBrian Somers     n = read(bundle->dev.fd, data, sz);
567078c562eSBrian Somers     if (n < 0) {
5683a7b6d76SBrian Somers       log_Printf(LogWARN, "%s: read: %s\n", bundle->dev.Name, strerror(errno));
569078c562eSBrian Somers       return;
570078c562eSBrian Somers     }
5713a7b6d76SBrian Somers 
5723a7b6d76SBrian Somers     if (bundle->dev.header) {
5733a7b6d76SBrian Somers       n -= sz - sizeof tun.data;
574078c562eSBrian Somers       if (n <= 0) {
5753a7b6d76SBrian Somers         log_Printf(LogERROR, "%s: read: Got only %d bytes of data !\n",
5763a7b6d76SBrian Somers                    bundle->dev.Name, n);
577078c562eSBrian Somers         return;
578078c562eSBrian Somers       }
57930949fd4SBrian Somers       af = ntohl(tun.header.family);
58030949fd4SBrian Somers #ifndef NOINET6
5811136c6acSBrian Somers       if (af != AF_INET && af != AF_INET6)
58230949fd4SBrian Somers #else
58330949fd4SBrian Somers       if (af != AF_INET)
58430949fd4SBrian Somers #endif
5853a7b6d76SBrian Somers         /* XXX: Should be maintaining drop/family counts ! */
586078c562eSBrian Somers         return;
58730949fd4SBrian Somers     } else
58830949fd4SBrian Somers       af = AF_INET;
589078c562eSBrian Somers 
59030949fd4SBrian Somers     if (af == AF_INET && ((struct ip *)tun.data)->ip_dst.s_addr ==
591078c562eSBrian Somers         bundle->ncp.ipcp.my_ip.s_addr) {
592078c562eSBrian Somers       /* we've been asked to send something addressed *to* us :( */
593078c562eSBrian Somers       if (Enabled(bundle, OPT_LOOPBACK)) {
59430949fd4SBrian Somers         pri = PacketCheck(bundle, af, tun.data, n, &bundle->filter.in,
59530949fd4SBrian Somers                           NULL, NULL);
596078c562eSBrian Somers         if (pri >= 0) {
5973a7b6d76SBrian Somers           n += sz - sizeof tun.data;
5983a7b6d76SBrian Somers           write(bundle->dev.fd, data, n);
599078c562eSBrian Somers           log_Printf(LogDEBUG, "Looped back packet addressed to myself\n");
600078c562eSBrian Somers         }
601078c562eSBrian Somers         return;
602078c562eSBrian Somers       } else
603078c562eSBrian Somers         log_Printf(LogDEBUG, "Oops - forwarding packet addressed to myself\n");
604078c562eSBrian Somers     }
605078c562eSBrian Somers 
606078c562eSBrian Somers     /*
60730949fd4SBrian Somers      * Process on-demand dialup. Output packets are queued within the tunnel
60830949fd4SBrian Somers      * device until the appropriate NCP is opened.
609078c562eSBrian Somers      */
610078c562eSBrian Somers 
611078c562eSBrian Somers     if (bundle_Phase(bundle) == PHASE_DEAD) {
612078c562eSBrian Somers       /*
613078c562eSBrian Somers        * Note, we must be in AUTO mode :-/ otherwise our interface should
614078c562eSBrian Somers        * *not* be UP and we can't receive data
615078c562eSBrian Somers        */
61630949fd4SBrian Somers       pri = PacketCheck(bundle, af, tun.data, n, &bundle->filter.dial,
61730949fd4SBrian Somers                         NULL, NULL);
618040cfe28SBrian Somers       if (pri >= 0)
619ba23f397SBrian Somers         bundle_Open(bundle, NULL, PHYS_AUTO, 0);
620078c562eSBrian Somers       else
621078c562eSBrian Somers         /*
622078c562eSBrian Somers          * Drop the packet.  If we were to queue it, we'd just end up with
623078c562eSBrian Somers          * a pile of timed-out data in our output queue by the time we get
624078c562eSBrian Somers          * around to actually dialing.  We'd also prematurely reach the
625078c562eSBrian Somers          * threshold at which we stop select()ing to read() the tun
626078c562eSBrian Somers          * device - breaking auto-dial.
627078c562eSBrian Somers          */
628078c562eSBrian Somers         return;
629078c562eSBrian Somers     }
630078c562eSBrian Somers 
6310a4b6c5cSBrian Somers     secs = 0;
63230949fd4SBrian Somers     pri = PacketCheck(bundle, af, tun.data, n, &bundle->filter.out,
63330949fd4SBrian Somers                       NULL, &secs);
6340a4b6c5cSBrian Somers     if (pri >= 0) {
6350a4b6c5cSBrian Somers       /* Prepend the number of seconds timeout given in the filter */
6360a4b6c5cSBrian Somers       tun.header.timeout = secs;
63730949fd4SBrian Somers       ncp_Enqueue(&bundle->ncp, af, pri, (char *)&tun, n + sizeof tun.header);
6380a4b6c5cSBrian Somers     }
639078c562eSBrian Somers   }
640078c562eSBrian Somers }
6412f786681SBrian Somers 
6421af29a6eSBrian Somers static int
643f013f33eSBrian Somers bundle_DescriptorWrite(struct fdescriptor *d, struct bundle *bundle,
6442f786681SBrian Somers                        const fd_set *fdset)
6452f786681SBrian Somers {
6462f786681SBrian Somers   struct datalink *dl;
6471af29a6eSBrian Somers   int result = 0;
6482f786681SBrian Somers 
649ea722969SBrian Somers   /* This is not actually necessary as struct mpserver doesn't Write() */
650ea722969SBrian Somers   if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset))
651fb11a9c2SBrian Somers     if (descriptor_Write(&bundle->ncp.mp.server.desc, bundle, fdset) == 1)
652fb11a9c2SBrian Somers       result++;
653ea722969SBrian Somers 
6542f786681SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
6552f786681SBrian Somers     if (descriptor_IsSet(&dl->desc, fdset))
656fb11a9c2SBrian Somers       switch (descriptor_Write(&dl->desc, bundle, fdset)) {
657fb11a9c2SBrian Somers       case -1:
658fb11a9c2SBrian Somers         datalink_ComeDown(dl, CLOSE_NORMAL);
659fb11a9c2SBrian Somers         break;
660fb11a9c2SBrian Somers       case 1:
661fb11a9c2SBrian Somers         result++;
662fb11a9c2SBrian Somers       }
6631af29a6eSBrian Somers 
6641af29a6eSBrian Somers   return result;
6652f786681SBrian Somers }
6662f786681SBrian Somers 
667da66dd13SBrian Somers void
6681384bd27SBrian Somers bundle_LockTun(struct bundle *bundle)
6691384bd27SBrian Somers {
6701384bd27SBrian Somers   FILE *lockfile;
67152847614SBrian Somers   char pidfile[PATH_MAX];
6721384bd27SBrian Somers 
6731384bd27SBrian Somers   snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit);
6741384bd27SBrian Somers   lockfile = ID0fopen(pidfile, "w");
6751384bd27SBrian Somers   if (lockfile != NULL) {
6761384bd27SBrian Somers     fprintf(lockfile, "%d\n", (int)getpid());
6771384bd27SBrian Somers     fclose(lockfile);
6781384bd27SBrian Somers   }
6791384bd27SBrian Somers #ifndef RELEASE_CRUNCH
6801384bd27SBrian Somers   else
6811384bd27SBrian Somers     log_Printf(LogERROR, "Warning: Can't create %s: %s\n",
6821384bd27SBrian Somers                pidfile, strerror(errno));
6831384bd27SBrian Somers #endif
6841384bd27SBrian Somers }
6851384bd27SBrian Somers 
6861384bd27SBrian Somers static void
6871384bd27SBrian Somers bundle_UnlockTun(struct bundle *bundle)
6881384bd27SBrian Somers {
68952847614SBrian Somers   char pidfile[PATH_MAX];
6901384bd27SBrian Somers 
6911384bd27SBrian Somers   snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit);
6921384bd27SBrian Somers   ID0unlink(pidfile);
6931384bd27SBrian Somers }
6947a6f8720SBrian Somers 
6957a6f8720SBrian Somers struct bundle *
696cf0a3940SBrian Somers bundle_Create(const char *prefix, int type, int unit)
6977a6f8720SBrian Somers {
6987a6f8720SBrian Somers   static struct bundle bundle;		/* there can be only one */
699c0593e34SBrian Somers   int enoentcount, err, minunit, maxunit;
7004e5196e9SBrian Somers   const char *ifname;
701fdb4bb1bSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
702fc3034caSBrian Somers   int kldtried;
703fc3034caSBrian Somers #endif
7043a7b6d76SBrian Somers #if defined(TUNSIFMODE) || defined(TUNSLMODE) || defined(TUNSIFHEAD)
7050f203c7eSBrian Somers   int iff;
7060f203c7eSBrian Somers #endif
7077a6f8720SBrian Somers 
7088fa6ebe4SBrian Somers   if (bundle.iface != NULL) {	/* Already allocated ! */
709a33b2ef7SBrian Somers     log_Printf(LogALERT, "bundle_Create:  There's only one BUNDLE !\n");
7107a6f8720SBrian Somers     return NULL;
7117a6f8720SBrian Somers   }
7127a6f8720SBrian Somers 
713c0593e34SBrian Somers   if (unit == -1) {
714c0593e34SBrian Somers     minunit = 0;
715c0593e34SBrian Somers     maxunit = -1;
716c0593e34SBrian Somers   } else {
717c0593e34SBrian Somers     minunit = unit;
718c0593e34SBrian Somers     maxunit = unit + 1;
719c0593e34SBrian Somers   }
7207a6f8720SBrian Somers   err = ENOENT;
7217a6f8720SBrian Somers   enoentcount = 0;
722fdb4bb1bSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
723fc3034caSBrian Somers   kldtried = 0;
724fc3034caSBrian Somers #endif
725c0593e34SBrian Somers   for (bundle.unit = minunit; bundle.unit != maxunit; bundle.unit++) {
726faefde08SBrian Somers     snprintf(bundle.dev.Name, sizeof bundle.dev.Name, "%s%d",
727faefde08SBrian Somers              prefix, bundle.unit);
728faefde08SBrian Somers     bundle.dev.fd = ID0open(bundle.dev.Name, O_RDWR);
729faefde08SBrian Somers     if (bundle.dev.fd >= 0)
7307a6f8720SBrian Somers       break;
731728ef5b2SBrian Somers     else if (errno == ENXIO || errno == ENOENT) {
732fdb4bb1bSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
733c0593e34SBrian Somers       if (bundle.unit == minunit && !kldtried++) {
734fc3034caSBrian Somers         /*
735fdb4bb1bSBrian Somers          * Attempt to load the tunnel interface KLD if it isn't loaded
736fdb4bb1bSBrian Somers          * already.
737fc3034caSBrian Somers          */
7385476d2e5SBrian Somers         if (loadmodules(LOAD_VERBOSLY, "if_tun", NULL))
7395476d2e5SBrian Somers           bundle.unit--;
740fc3034caSBrian Somers         continue;
741fc3034caSBrian Somers       }
742fc3034caSBrian Somers #endif
743c4c6616aSBrian Somers       if (errno != ENOENT || ++enoentcount > 2) {
7447a6f8720SBrian Somers         err = errno;
745107d62e7SBrian Somers 	break;
746c4c6616aSBrian Somers       }
7477a6f8720SBrian Somers     } else
7487a6f8720SBrian Somers       err = errno;
7497a6f8720SBrian Somers   }
7507a6f8720SBrian Somers 
751faefde08SBrian Somers   if (bundle.dev.fd < 0) {
752c0593e34SBrian Somers     if (unit == -1)
753c0593e34SBrian Somers       log_Printf(LogWARN, "No available tunnel devices found (%s)\n",
75485b542cfSBrian Somers                 strerror(err));
755c0593e34SBrian Somers     else
756c0593e34SBrian Somers       log_Printf(LogWARN, "%s%d: %s\n", prefix, unit, strerror(err));
7577a6f8720SBrian Somers     return NULL;
7587a6f8720SBrian Somers   }
7597a6f8720SBrian Somers 
760dd7e2610SBrian Somers   log_SetTun(bundle.unit);
7617a6f8720SBrian Somers 
7628fa6ebe4SBrian Somers   ifname = strrchr(bundle.dev.Name, '/');
7638fa6ebe4SBrian Somers   if (ifname == NULL)
7648fa6ebe4SBrian Somers     ifname = bundle.dev.Name;
7657a6f8720SBrian Somers   else
7668fa6ebe4SBrian Somers     ifname++;
7678fa6ebe4SBrian Somers 
7688fa6ebe4SBrian Somers   bundle.iface = iface_Create(ifname);
7698fa6ebe4SBrian Somers   if (bundle.iface == NULL) {
7708fa6ebe4SBrian Somers     close(bundle.dev.fd);
7718fa6ebe4SBrian Somers     return NULL;
7728fa6ebe4SBrian Somers   }
7737a6f8720SBrian Somers 
7740f203c7eSBrian Somers #ifdef TUNSIFMODE
775ebdcbc67SBrian Somers   /* Make sure we're POINTOPOINT & IFF_MULTICAST */
776ebdcbc67SBrian Somers   iff = IFF_POINTOPOINT | IFF_MULTICAST;
7770f203c7eSBrian Somers   if (ID0ioctl(bundle.dev.fd, TUNSIFMODE, &iff) < 0)
7780f203c7eSBrian Somers     log_Printf(LogERROR, "bundle_Create: ioctl(TUNSIFMODE): %s\n",
7790f203c7eSBrian Somers 	       strerror(errno));
7800f203c7eSBrian Somers #endif
7810f203c7eSBrian Somers 
7826ca65df0SBrian Somers #ifdef TUNSLMODE
7833a7b6d76SBrian Somers   /* Make sure we're not prepending sockaddrs */
7846ca65df0SBrian Somers   iff = 0;
7856ca65df0SBrian Somers   if (ID0ioctl(bundle.dev.fd, TUNSLMODE, &iff) < 0)
7866ca65df0SBrian Somers     log_Printf(LogERROR, "bundle_Create: ioctl(TUNSLMODE): %s\n",
7876ca65df0SBrian Somers 	       strerror(errno));
7886ca65df0SBrian Somers #endif
7896ca65df0SBrian Somers 
7903a7b6d76SBrian Somers #ifdef TUNSIFHEAD
7913a7b6d76SBrian Somers   /* We want the address family please ! */
7923a7b6d76SBrian Somers   iff = 1;
7933a7b6d76SBrian Somers   if (ID0ioctl(bundle.dev.fd, TUNSIFHEAD, &iff) < 0) {
7943a7b6d76SBrian Somers     log_Printf(LogERROR, "bundle_Create: ioctl(TUNSIFHEAD): %s\n",
7953a7b6d76SBrian Somers 	       strerror(errno));
7963a7b6d76SBrian Somers     bundle.dev.header = 0;
7973a7b6d76SBrian Somers   } else
7983a7b6d76SBrian Somers     bundle.dev.header = 1;
7993a7b6d76SBrian Somers #else
8003a7b6d76SBrian Somers #ifdef __OpenBSD__
8013a7b6d76SBrian Somers   /* Always present for OpenBSD */
8023a7b6d76SBrian Somers   bundle.dev.header = 1;
8033a7b6d76SBrian Somers #else
8043a7b6d76SBrian Somers   /*
8053a7b6d76SBrian Somers    * If TUNSIFHEAD isn't available and we're not OpenBSD, assume
8063a7b6d76SBrian Somers    * everything's AF_INET (hopefully the tun device won't pass us
8073a7b6d76SBrian Somers    * anything else !).
8083a7b6d76SBrian Somers    */
8093a7b6d76SBrian Somers   bundle.dev.header = 0;
8103a7b6d76SBrian Somers #endif
8113a7b6d76SBrian Somers #endif
8123a7b6d76SBrian Somers 
8138fa6ebe4SBrian Somers   log_Printf(LogPHASE, "Using interface: %s\n", ifname);
8147a6f8720SBrian Somers 
815ab2de065SBrian Somers   bundle.bandwidth = 0;
816820de6ebSBrian Somers   bundle.routing_seq = 0;
817a0cbd833SBrian Somers   bundle.phase = PHASE_DEAD;
818a0cbd833SBrian Somers   bundle.CleaningUp = 0;
81967b072f7SBrian Somers   bundle.NatEnabled = 0;
8207a6f8720SBrian Somers 
8216d666775SBrian Somers   bundle.fsm.LayerStart = bundle_LayerStart;
8226f384573SBrian Somers   bundle.fsm.LayerUp = bundle_LayerUp;
8236d666775SBrian Somers   bundle.fsm.LayerDown = bundle_LayerDown;
8246d666775SBrian Somers   bundle.fsm.LayerFinish = bundle_LayerFinish;
8256d666775SBrian Somers   bundle.fsm.object = &bundle;
8267a6f8720SBrian Somers 
827dade2407SBrian Somers   bundle.cfg.idle.timeout = NCP_IDLE_TIMEOUT;
828dade2407SBrian Somers   bundle.cfg.idle.min_timeout = 0;
8291342caedSBrian Somers   *bundle.cfg.auth.name = '\0';
8301342caedSBrian Somers   *bundle.cfg.auth.key = '\0';
83130949fd4SBrian Somers   bundle.cfg.opt = OPT_IDCHECK | OPT_LOOPBACK | OPT_SROUTES | OPT_TCPMSSFIXUP |
832610b185fSBrian Somers                    OPT_THROUGHPUT | OPT_UTMP;
833971abb29SBrian Somers #ifndef NOINET6
834971abb29SBrian Somers   bundle.cfg.opt |= OPT_IPCP;
835971abb29SBrian Somers   if (probe.ipv6_available)
836971abb29SBrian Somers     bundle.cfg.opt |= OPT_IPV6CP;
837971abb29SBrian Somers #endif
83849052c95SBrian Somers   *bundle.cfg.label = '\0';
8396c1d6731SBrian Somers   bundle.cfg.ifqueue = DEF_IFQUEUE;
8406f8e9f0aSBrian Somers   bundle.cfg.choked.timeout = CHOKED_TIMEOUT;
841ff0f9439SBrian Somers   bundle.phys_type.all = type;
842ff0f9439SBrian Somers   bundle.phys_type.open = 0;
843dade2407SBrian Somers   bundle.upat = 0;
844ab886ad0SBrian Somers 
8456f384573SBrian Somers   bundle.links = datalink_Create("deflink", &bundle, type);
8463006ec67SBrian Somers   if (bundle.links == NULL) {
847a33b2ef7SBrian Somers     log_Printf(LogALERT, "Cannot create data link: %s\n", strerror(errno));
8488fa6ebe4SBrian Somers     iface_Destroy(bundle.iface);
8498fa6ebe4SBrian Somers     bundle.iface = NULL;
850faefde08SBrian Somers     close(bundle.dev.fd);
8512289f246SBrian Somers     return NULL;
8522289f246SBrian Somers   }
8532289f246SBrian Somers 
8542f786681SBrian Somers   bundle.desc.type = BUNDLE_DESCRIPTOR;
8552f786681SBrian Somers   bundle.desc.UpdateSet = bundle_UpdateSet;
8562f786681SBrian Somers   bundle.desc.IsSet = bundle_IsSet;
8572f786681SBrian Somers   bundle.desc.Read = bundle_DescriptorRead;
8582f786681SBrian Somers   bundle.desc.Write = bundle_DescriptorWrite;
8592f786681SBrian Somers 
86030949fd4SBrian Somers   ncp_Init(&bundle.ncp, &bundle);
8616d666775SBrian Somers 
8625ca5389aSBrian Somers   memset(&bundle.filter, '\0', sizeof bundle.filter);
8635ca5389aSBrian Somers   bundle.filter.in.fragok = bundle.filter.in.logok = 1;
8645ca5389aSBrian Somers   bundle.filter.in.name = "IN";
8655ca5389aSBrian Somers   bundle.filter.out.fragok = bundle.filter.out.logok = 1;
8665ca5389aSBrian Somers   bundle.filter.out.name = "OUT";
8675ca5389aSBrian Somers   bundle.filter.dial.name = "DIAL";
8688390b576SBrian Somers   bundle.filter.dial.logok = 1;
8695ca5389aSBrian Somers   bundle.filter.alive.name = "ALIVE";
8705ca5389aSBrian Somers   bundle.filter.alive.logok = 1;
871cad7e742SBrian Somers   {
872cad7e742SBrian Somers     int	i;
873cad7e742SBrian Somers     for (i = 0; i < MAXFILTERS; i++) {
874cad7e742SBrian Somers         bundle.filter.in.rule[i].f_action = A_NONE;
875cad7e742SBrian Somers         bundle.filter.out.rule[i].f_action = A_NONE;
876cad7e742SBrian Somers         bundle.filter.dial.rule[i].f_action = A_NONE;
877cad7e742SBrian Somers         bundle.filter.alive.rule[i].f_action = A_NONE;
878cad7e742SBrian Somers     }
879cad7e742SBrian Somers   }
88093ee0ff2SBrian Somers   memset(&bundle.idle.timer, '\0', sizeof bundle.idle.timer);
88193ee0ff2SBrian Somers   bundle.idle.done = 0;
8825cf4388bSBrian Somers   bundle.notify.fd = -1;
8836f8e9f0aSBrian Somers   memset(&bundle.choked.timer, '\0', sizeof bundle.choked.timer);
884972a1bcfSBrian Somers #ifndef NORADIUS
885972a1bcfSBrian Somers   radius_Init(&bundle.radius);
886972a1bcfSBrian Somers #endif
88793ee0ff2SBrian Somers 
8886d666775SBrian Somers   /* Clean out any leftover crud */
88930949fd4SBrian Somers   iface_Clear(bundle.iface, &bundle.ncp, 0, IFACE_CLEAR_ALL);
8906d666775SBrian Somers 
8911384bd27SBrian Somers   bundle_LockTun(&bundle);
8921384bd27SBrian Somers 
8937a6f8720SBrian Somers   return &bundle;
8947a6f8720SBrian Somers }
8957a6f8720SBrian Somers 
89668a0f0ccSBrian Somers static void
89768a0f0ccSBrian Somers bundle_DownInterface(struct bundle *bundle)
89868a0f0ccSBrian Somers {
899dd7e2610SBrian Somers   route_IfDelete(bundle, 1);
900dc744e19SBrian Somers   iface_ClearFlags(bundle->iface->name, IFF_UP);
90168a0f0ccSBrian Somers }
90268a0f0ccSBrian Somers 
90368a0f0ccSBrian Somers void
90468a0f0ccSBrian Somers bundle_Destroy(struct bundle *bundle)
90568a0f0ccSBrian Somers {
9063006ec67SBrian Somers   struct datalink *dl;
907b6217683SBrian Somers 
908ea722969SBrian Somers   /*
90930949fd4SBrian Somers    * Clean up the interface.  We don't really need to do the timer_Stop()s,
91030949fd4SBrian Somers    * mp_Down(), iface_Clear() and bundle_DownInterface() unless we're getting
9118e7bd08eSBrian Somers    * out under exceptional conditions such as a descriptor exception.
912ea722969SBrian Somers    */
91304eaa58cSBrian Somers   timer_Stop(&bundle->idle.timer);
9146f8e9f0aSBrian Somers   timer_Stop(&bundle->choked.timer);
91566f634b6SBrian Somers   mp_Down(&bundle->ncp.mp);
91630949fd4SBrian Somers   iface_Clear(bundle->iface, &bundle->ncp, 0, IFACE_CLEAR_ALL);
91768a0f0ccSBrian Somers   bundle_DownInterface(bundle);
9183006ec67SBrian Somers 
919972a1bcfSBrian Somers #ifndef NORADIUS
920972a1bcfSBrian Somers   /* Tell the radius server the bad news */
921972a1bcfSBrian Somers   radius_Destroy(&bundle->radius);
922972a1bcfSBrian Somers #endif
923972a1bcfSBrian Somers 
924ea722969SBrian Somers   /* Again, these are all DATALINK_CLOSED unless we're abending */
9253006ec67SBrian Somers   dl = bundle->links;
9263006ec67SBrian Somers   while (dl)
9273006ec67SBrian Somers     dl = datalink_Destroy(dl);
9283006ec67SBrian Somers 
92930949fd4SBrian Somers   ncp_Destroy(&bundle->ncp);
930442f8495SBrian Somers 
9311384bd27SBrian Somers   close(bundle->dev.fd);
9321384bd27SBrian Somers   bundle_UnlockTun(bundle);
9331384bd27SBrian Somers 
934ea722969SBrian Somers   /* In case we never made PHASE_NETWORK */
9355cf4388bSBrian Somers   bundle_Notify(bundle, EX_ERRDEAD);
936b6217683SBrian Somers 
9378fa6ebe4SBrian Somers   iface_Destroy(bundle->iface);
9388fa6ebe4SBrian Somers   bundle->iface = NULL;
93968a0f0ccSBrian Somers }
94068a0f0ccSBrian Somers 
94183d1af55SBrian Somers void
9423006ec67SBrian Somers bundle_LinkClosed(struct bundle *bundle, struct datalink *dl)
9433006ec67SBrian Somers {
9443006ec67SBrian Somers   /*
9453006ec67SBrian Somers    * Our datalink has closed.
946ea722969SBrian Somers    * CleanDatalinks() (called from DoLoop()) will remove closed
947f6a4e748SBrian Somers    * BACKGROUND, FOREGROUND and DIRECT links.
9483b0f8d2eSBrian Somers    * If it's the last data link, enter phase DEAD.
949ea722969SBrian Somers    *
950ea722969SBrian Somers    * NOTE: dl may not be in our list (bundle_SendDatalink()) !
9513006ec67SBrian Somers    */
9525b8b8060SBrian Somers 
9533b0f8d2eSBrian Somers   struct datalink *odl;
9543b0f8d2eSBrian Somers   int other_links;
9555b8b8060SBrian Somers 
956bf1d3ff6SBrian Somers   log_SetTtyCommandMode(dl);
957bf1d3ff6SBrian Somers 
9583b0f8d2eSBrian Somers   other_links = 0;
9593b0f8d2eSBrian Somers   for (odl = bundle->links; odl; odl = odl->next)
9603b0f8d2eSBrian Somers     if (odl != dl && odl->state != DATALINK_CLOSED)
9613b0f8d2eSBrian Somers       other_links++;
9623b0f8d2eSBrian Somers 
9633b0f8d2eSBrian Somers   if (!other_links) {
96481358fa3SBrian Somers     if (dl->physical->type != PHYS_AUTO)	/* Not in -auto mode */
96503704096SBrian Somers       bundle_DownInterface(bundle);
96630949fd4SBrian Somers     ncp2initial(&bundle->ncp);
967356bf92dSBrian Somers     mp_Down(&bundle->ncp.mp);
9685563ebdeSBrian Somers     bundle_NewPhase(bundle, PHASE_DEAD);
969bf1eaec5SBrian Somers #ifndef NORADIUS
970bf1eaec5SBrian Somers     if (bundle->radius.sessiontime)
971bf1eaec5SBrian Somers       bundle_StopSessionTimer(bundle);
972bf1eaec5SBrian Somers #endif
9730f2f3eb3SBrian Somers     bundle_StopIdleTimer(bundle);
974ab2de065SBrian Somers   }
975455aabc3SBrian Somers }
976455aabc3SBrian Somers 
977455aabc3SBrian Somers void
978ba23f397SBrian Somers bundle_Open(struct bundle *bundle, const char *name, int mask, int force)
9793006ec67SBrian Somers {
9803006ec67SBrian Somers   /*
9813006ec67SBrian Somers    * Please open the given datalink, or all if name == NULL
9823006ec67SBrian Somers    */
9833006ec67SBrian Somers   struct datalink *dl;
9843006ec67SBrian Somers 
9853006ec67SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
9863006ec67SBrian Somers     if (name == NULL || !strcasecmp(dl->name, name)) {
987ba23f397SBrian Somers       if ((mask & dl->physical->type) &&
988ba23f397SBrian Somers           (dl->state == DATALINK_CLOSED ||
989ba23f397SBrian Somers            (force && dl->state == DATALINK_OPENING &&
9905e269efeSBrian Somers             dl->dial.timer.state == TIMER_RUNNING) ||
9915e269efeSBrian Somers            dl->state == DATALINK_READY)) {
9925e269efeSBrian Somers         timer_Stop(&dl->dial.timer);	/* We're finished with this */
993565e35e5SBrian Somers         datalink_Up(dl, 1, 1);
994ab2de065SBrian Somers         if (mask & PHYS_AUTO)
9955e269efeSBrian Somers           break;			/* Only one AUTO link at a time */
99604eaa58cSBrian Somers       }
9973006ec67SBrian Somers       if (name != NULL)
9983006ec67SBrian Somers         break;
9993006ec67SBrian Somers     }
10003006ec67SBrian Somers }
10013006ec67SBrian Somers 
10023006ec67SBrian Somers struct datalink *
10033006ec67SBrian Somers bundle2datalink(struct bundle *bundle, const char *name)
10043006ec67SBrian Somers {
10053006ec67SBrian Somers   struct datalink *dl;
10063006ec67SBrian Somers 
10073006ec67SBrian Somers   if (name != NULL) {
10083006ec67SBrian Somers     for (dl = bundle->links; dl; dl = dl->next)
10093006ec67SBrian Somers       if (!strcasecmp(dl->name, name))
10103006ec67SBrian Somers         return dl;
10113006ec67SBrian Somers   } else if (bundle->links && !bundle->links->next)
10123006ec67SBrian Somers     return bundle->links;
10133006ec67SBrian Somers 
10143006ec67SBrian Somers   return NULL;
10153006ec67SBrian Somers }
10163006ec67SBrian Somers 
10173006ec67SBrian Somers int
1018aef795ccSBrian Somers bundle_ShowLinks(struct cmdargs const *arg)
1019aef795ccSBrian Somers {
1020aef795ccSBrian Somers   struct datalink *dl;
1021ab2de065SBrian Somers   struct pppThroughput *t;
102291cbd2eeSBrian Somers   unsigned long long octets;
1023ab2de065SBrian Somers   int secs;
1024aef795ccSBrian Somers 
10259c53a7b1SBrian Somers   for (dl = arg->bundle->links; dl; dl = dl->next) {
102691cbd2eeSBrian Somers     octets = MAX(dl->physical->link.stats.total.in.OctetsPerSecond,
102791cbd2eeSBrian Somers                  dl->physical->link.stats.total.out.OctetsPerSecond);
102891cbd2eeSBrian Somers 
1029d4156d00SBrian Somers     prompt_Printf(arg->prompt, "Name: %s [%s, %s]",
1030d4156d00SBrian Somers                   dl->name, mode2Nam(dl->physical->type), datalink_State(dl));
103111572abfSBrian Somers     if (dl->physical->link.stats.total.rolling && dl->state == DATALINK_OPEN)
1032e531f89aSBrian Somers       prompt_Printf(arg->prompt, " bandwidth %d, %llu bps (%llu bytes/sec)",
1033ab2de065SBrian Somers                     dl->mp.bandwidth ? dl->mp.bandwidth :
1034ab2de065SBrian Somers                                        physical_GetSpeed(dl->physical),
103591cbd2eeSBrian Somers                     octets * 8, octets);
10369c53a7b1SBrian Somers     prompt_Printf(arg->prompt, "\n");
10379c53a7b1SBrian Somers   }
1038aef795ccSBrian Somers 
103911572abfSBrian Somers   t = &arg->bundle->ncp.mp.link.stats.total;
104091cbd2eeSBrian Somers   octets = MAX(t->in.OctetsPerSecond, t->out.OctetsPerSecond);
1041ab2de065SBrian Somers   secs = t->downtime ? 0 : throughput_uptime(t);
1042ab2de065SBrian Somers   if (secs > t->SamplePeriod)
1043ab2de065SBrian Somers     secs = t->SamplePeriod;
1044ab2de065SBrian Somers   if (secs)
1045e531f89aSBrian Somers     prompt_Printf(arg->prompt, "Currently averaging %llu bps (%llu bytes/sec)"
104691cbd2eeSBrian Somers                   " over the last %d secs\n", octets * 8, octets, secs);
1047ab2de065SBrian Somers 
1048aef795ccSBrian Somers   return 0;
1049aef795ccSBrian Somers }
1050ab886ad0SBrian Somers 
10511342caedSBrian Somers static const char *
10521342caedSBrian Somers optval(struct bundle *bundle, int bit)
10531342caedSBrian Somers {
10541342caedSBrian Somers   return (bundle->cfg.opt & bit) ? "enabled" : "disabled";
10551342caedSBrian Somers }
10561342caedSBrian Somers 
1057c08717dfSBrian Somers int
1058c08717dfSBrian Somers bundle_ShowStatus(struct cmdargs const *arg)
1059c08717dfSBrian Somers {
1060c08717dfSBrian Somers   int remaining;
1061c08717dfSBrian Somers 
1062c08717dfSBrian Somers   prompt_Printf(arg->prompt, "Phase %s\n", bundle_PhaseName(arg->bundle));
1063faefde08SBrian Somers   prompt_Printf(arg->prompt, " Device:        %s\n", arg->bundle->dev.Name);
1064dade2407SBrian Somers   prompt_Printf(arg->prompt, " Interface:     %s @ %lubps",
1065ab2de065SBrian Somers                 arg->bundle->iface->name, arg->bundle->bandwidth);
1066c08717dfSBrian Somers 
1067dade2407SBrian Somers   if (arg->bundle->upat) {
106846df5aa7SBrian Somers     int secs = bundle_Uptime(arg->bundle);
1069dade2407SBrian Somers 
1070dade2407SBrian Somers     prompt_Printf(arg->prompt, ", up time %d:%02d:%02d", secs / 3600,
1071dade2407SBrian Somers                   (secs / 60) % 60, secs % 60);
1072dade2407SBrian Somers   }
1073669b9965SBrian Somers   prompt_Printf(arg->prompt, "\n Queued:        %lu of %u\n",
107430949fd4SBrian Somers                 (unsigned long)ncp_QueueLen(&arg->bundle->ncp),
107577fc031dSBrian Somers                 arg->bundle->cfg.ifqueue);
1076dade2407SBrian Somers 
10776c1d6731SBrian Somers   prompt_Printf(arg->prompt, "\nDefaults:\n");
107874457d3dSBrian Somers   prompt_Printf(arg->prompt, " Label:             %s\n",
107974457d3dSBrian Somers                 arg->bundle->cfg.label);
1080610b185fSBrian Somers   prompt_Printf(arg->prompt, " Auth name:         %s\n",
1081610b185fSBrian Somers                 arg->bundle->cfg.auth.name);
108274457d3dSBrian Somers   prompt_Printf(arg->prompt, " Diagnostic socket: ");
108337b8a5c7SBrian Somers   if (*server.cfg.sockname != '\0') {
108437b8a5c7SBrian Somers     prompt_Printf(arg->prompt, "%s", server.cfg.sockname);
108537b8a5c7SBrian Somers     if (server.cfg.mask != (mode_t)-1)
108637b8a5c7SBrian Somers       prompt_Printf(arg->prompt, ", mask 0%03o", (int)server.cfg.mask);
108737b8a5c7SBrian Somers     prompt_Printf(arg->prompt, "%s\n", server.fd == -1 ? " (not open)" : "");
108837b8a5c7SBrian Somers   } else if (server.cfg.port != 0)
108974457d3dSBrian Somers     prompt_Printf(arg->prompt, "TCP port %d%s\n", server.cfg.port,
109074457d3dSBrian Somers                   server.fd == -1 ? " (not open)" : "");
109174457d3dSBrian Somers   else
109274457d3dSBrian Somers     prompt_Printf(arg->prompt, "none\n");
109304eaa58cSBrian Somers 
10946f8e9f0aSBrian Somers   prompt_Printf(arg->prompt, " Choked Timer:      %ds\n",
10956f8e9f0aSBrian Somers                 arg->bundle->cfg.choked.timeout);
1096972a1bcfSBrian Somers 
1097972a1bcfSBrian Somers #ifndef NORADIUS
1098972a1bcfSBrian Somers   radius_Show(&arg->bundle->radius, arg->prompt);
1099972a1bcfSBrian Somers #endif
1100972a1bcfSBrian Somers 
1101c08717dfSBrian Somers   prompt_Printf(arg->prompt, " Idle Timer:        ");
1102dade2407SBrian Somers   if (arg->bundle->cfg.idle.timeout) {
1103dade2407SBrian Somers     prompt_Printf(arg->prompt, "%ds", arg->bundle->cfg.idle.timeout);
1104dade2407SBrian Somers     if (arg->bundle->cfg.idle.min_timeout)
1105dade2407SBrian Somers       prompt_Printf(arg->prompt, ", min %ds",
1106dade2407SBrian Somers                     arg->bundle->cfg.idle.min_timeout);
1107c08717dfSBrian Somers     remaining = bundle_RemainingIdleTime(arg->bundle);
1108c08717dfSBrian Somers     if (remaining != -1)
1109c08717dfSBrian Somers       prompt_Printf(arg->prompt, " (%ds remaining)", remaining);
1110c08717dfSBrian Somers     prompt_Printf(arg->prompt, "\n");
1111c08717dfSBrian Somers   } else
1112c08717dfSBrian Somers     prompt_Printf(arg->prompt, "disabled\n");
1113c08717dfSBrian Somers 
111430949fd4SBrian Somers   prompt_Printf(arg->prompt, " Filter Decap:      %-20.20s",
111598251667SBrian Somers                 optval(arg->bundle, OPT_FILTERDECAP));
111630949fd4SBrian Somers   prompt_Printf(arg->prompt, " ID check:          %s\n",
11171342caedSBrian Somers                 optval(arg->bundle, OPT_IDCHECK));
111830949fd4SBrian Somers   prompt_Printf(arg->prompt, " Iface-Alias:       %-20.20s",
111930949fd4SBrian Somers                 optval(arg->bundle, OPT_IFACEALIAS));
112030949fd4SBrian Somers #ifndef NOINET6
112130949fd4SBrian Somers   prompt_Printf(arg->prompt, " IPCP:              %s\n",
112230949fd4SBrian Somers                 optval(arg->bundle, OPT_IPCP));
112330949fd4SBrian Somers   prompt_Printf(arg->prompt, " IPV6CP:            %-20.20s",
112430949fd4SBrian Somers                 optval(arg->bundle, OPT_IPV6CP));
112530949fd4SBrian Somers #endif
112698251667SBrian Somers   prompt_Printf(arg->prompt, " Keep-Session:      %s\n",
1127ac685e31SBrian Somers                 optval(arg->bundle, OPT_KEEPSESSION));
112898251667SBrian Somers   prompt_Printf(arg->prompt, " Loopback:          %-20.20s",
11291342caedSBrian Somers                 optval(arg->bundle, OPT_LOOPBACK));
113098251667SBrian Somers   prompt_Printf(arg->prompt, " PasswdAuth:        %s\n",
11311342caedSBrian Somers                 optval(arg->bundle, OPT_PASSWDAUTH));
113298251667SBrian Somers   prompt_Printf(arg->prompt, " Proxy:             %-20.20s",
11331342caedSBrian Somers                 optval(arg->bundle, OPT_PROXY));
113498251667SBrian Somers   prompt_Printf(arg->prompt, " Proxyall:          %s\n",
11353afe5ccbSBrian Somers                 optval(arg->bundle, OPT_PROXYALL));
113630949fd4SBrian Somers   prompt_Printf(arg->prompt, " Sticky Routes:     %-20.20s",
113730949fd4SBrian Somers                 optval(arg->bundle, OPT_SROUTES));
113830949fd4SBrian Somers   prompt_Printf(arg->prompt, " TCPMSS Fixup:      %s\n",
113994d7be52SBrian Somers                 optval(arg->bundle, OPT_TCPMSSFIXUP));
114030949fd4SBrian Somers   prompt_Printf(arg->prompt, " Throughput:        %-20.20s",
11411342caedSBrian Somers                 optval(arg->bundle, OPT_THROUGHPUT));
114230949fd4SBrian Somers   prompt_Printf(arg->prompt, " Utmp Logging:      %s\n",
11431342caedSBrian Somers                 optval(arg->bundle, OPT_UTMP));
11441342caedSBrian Somers 
1145c08717dfSBrian Somers   return 0;
1146c08717dfSBrian Somers }
1147c08717dfSBrian Somers 
1148ab886ad0SBrian Somers static void
1149ab886ad0SBrian Somers bundle_IdleTimeout(void *v)
1150ab886ad0SBrian Somers {
1151ab886ad0SBrian Somers   struct bundle *bundle = (struct bundle *)v;
1152ab886ad0SBrian Somers 
1153b42135deSBrian Somers   log_Printf(LogPHASE, "Idle timer expired\n");
115404eaa58cSBrian Somers   bundle_StopIdleTimer(bundle);
11559c81b87dSBrian Somers   bundle_Close(bundle, NULL, CLOSE_STAYDOWN);
1156ab886ad0SBrian Somers }
1157ab886ad0SBrian Somers 
1158ab886ad0SBrian Somers /*
1159ab886ad0SBrian Somers  *  Start Idle timer. If timeout is reached, we call bundle_Close() to
1160ab886ad0SBrian Somers  *  close LCP and link.
1161ab886ad0SBrian Somers  */
1162ab886ad0SBrian Somers void
11630a4b6c5cSBrian Somers bundle_StartIdleTimer(struct bundle *bundle, unsigned secs)
1164ab886ad0SBrian Somers {
1165dd7e2610SBrian Somers   timer_Stop(&bundle->idle.timer);
1166ff0f9439SBrian Somers   if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) !=
1167dade2407SBrian Somers       bundle->phys_type.open && bundle->cfg.idle.timeout) {
11680a4b6c5cSBrian Somers     time_t now = time(NULL);
1169dade2407SBrian Somers 
11700a4b6c5cSBrian Somers     if (secs == 0)
1171dade2407SBrian Somers       secs = bundle->cfg.idle.timeout;
11720a4b6c5cSBrian Somers 
11730a4b6c5cSBrian Somers     /* We want at least `secs' */
1174dade2407SBrian Somers     if (bundle->cfg.idle.min_timeout > secs && bundle->upat) {
11750a4b6c5cSBrian Somers       int up = now - bundle->upat;
1176dade2407SBrian Somers 
1177dade2407SBrian Somers       if ((long long)bundle->cfg.idle.min_timeout - up > (long long)secs)
11780a4b6c5cSBrian Somers         /* Only increase from the current `remaining' value */
1179dade2407SBrian Somers         secs = bundle->cfg.idle.min_timeout - up;
1180dade2407SBrian Somers     }
118193ee0ff2SBrian Somers     bundle->idle.timer.func = bundle_IdleTimeout;
11823b0f8d2eSBrian Somers     bundle->idle.timer.name = "idle";
1183dade2407SBrian Somers     bundle->idle.timer.load = secs * SECTICKS;
118493ee0ff2SBrian Somers     bundle->idle.timer.arg = bundle;
1185dd7e2610SBrian Somers     timer_Start(&bundle->idle.timer);
11860a4b6c5cSBrian Somers     bundle->idle.done = now + secs;
1187ab886ad0SBrian Somers   }
1188ab886ad0SBrian Somers }
1189ab886ad0SBrian Somers 
1190ab886ad0SBrian Somers void
1191dade2407SBrian Somers bundle_SetIdleTimer(struct bundle *bundle, int timeout, int min_timeout)
1192ab886ad0SBrian Somers {
1193dade2407SBrian Somers   bundle->cfg.idle.timeout = timeout;
1194dade2407SBrian Somers   if (min_timeout >= 0)
1195dade2407SBrian Somers     bundle->cfg.idle.min_timeout = min_timeout;
119630949fd4SBrian Somers   if (ncp_LayersOpen(&bundle->ncp))
11970a4b6c5cSBrian Somers     bundle_StartIdleTimer(bundle, 0);
1198ab886ad0SBrian Somers }
1199ab886ad0SBrian Somers 
1200ab886ad0SBrian Somers void
1201ab886ad0SBrian Somers bundle_StopIdleTimer(struct bundle *bundle)
1202ab886ad0SBrian Somers {
1203dd7e2610SBrian Somers   timer_Stop(&bundle->idle.timer);
12044a632c80SBrian Somers   bundle->idle.done = 0;
1205ab886ad0SBrian Somers }
1206ab886ad0SBrian Somers 
120704eaa58cSBrian Somers static int
1208ab886ad0SBrian Somers bundle_RemainingIdleTime(struct bundle *bundle)
1209ab886ad0SBrian Somers {
121093ee0ff2SBrian Somers   if (bundle->idle.done)
121193ee0ff2SBrian Somers     return bundle->idle.done - time(NULL);
1212ab886ad0SBrian Somers   return -1;
1213ab886ad0SBrian Somers }
12143b0f8d2eSBrian Somers 
1215bf1eaec5SBrian Somers #ifndef NORADIUS
1216bf1eaec5SBrian Somers 
1217bf1eaec5SBrian Somers static void
1218bf1eaec5SBrian Somers bundle_SessionTimeout(void *v)
1219bf1eaec5SBrian Somers {
1220bf1eaec5SBrian Somers   struct bundle *bundle = (struct bundle *)v;
1221bf1eaec5SBrian Somers 
1222bf1eaec5SBrian Somers   log_Printf(LogPHASE, "Session-Timeout timer expired\n");
1223bf1eaec5SBrian Somers   bundle_StopSessionTimer(bundle);
1224bf1eaec5SBrian Somers   bundle_Close(bundle, NULL, CLOSE_STAYDOWN);
1225bf1eaec5SBrian Somers }
1226bf1eaec5SBrian Somers 
1227bf1eaec5SBrian Somers void
1228bf1eaec5SBrian Somers bundle_StartSessionTimer(struct bundle *bundle, unsigned secs)
1229bf1eaec5SBrian Somers {
1230bf1eaec5SBrian Somers   timer_Stop(&bundle->session.timer);
1231bf1eaec5SBrian Somers   if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) !=
1232bf1eaec5SBrian Somers       bundle->phys_type.open && bundle->radius.sessiontime) {
1233bf1eaec5SBrian Somers     time_t now = time(NULL);
1234bf1eaec5SBrian Somers 
1235bf1eaec5SBrian Somers     if (secs == 0)
1236bf1eaec5SBrian Somers       secs = bundle->radius.sessiontime;
1237bf1eaec5SBrian Somers 
1238bf1eaec5SBrian Somers     bundle->session.timer.func = bundle_SessionTimeout;
1239bf1eaec5SBrian Somers     bundle->session.timer.name = "session";
1240bf1eaec5SBrian Somers     bundle->session.timer.load = secs * SECTICKS;
1241bf1eaec5SBrian Somers     bundle->session.timer.arg = bundle;
1242bf1eaec5SBrian Somers     timer_Start(&bundle->session.timer);
1243bf1eaec5SBrian Somers     bundle->session.done = now + secs;
1244bf1eaec5SBrian Somers   }
1245bf1eaec5SBrian Somers }
1246bf1eaec5SBrian Somers 
1247bf1eaec5SBrian Somers void
1248bf1eaec5SBrian Somers bundle_StopSessionTimer(struct bundle *bundle)
1249bf1eaec5SBrian Somers {
1250bf1eaec5SBrian Somers   timer_Stop(&bundle->session.timer);
1251bf1eaec5SBrian Somers   bundle->session.done = 0;
1252bf1eaec5SBrian Somers }
1253bf1eaec5SBrian Somers 
1254bf1eaec5SBrian Somers #endif
1255bf1eaec5SBrian Somers 
12563b0f8d2eSBrian Somers int
12573b0f8d2eSBrian Somers bundle_IsDead(struct bundle *bundle)
12583b0f8d2eSBrian Somers {
12593b0f8d2eSBrian Somers   return !bundle->links || (bundle->phase == PHASE_DEAD && bundle->CleaningUp);
12603b0f8d2eSBrian Somers }
1261b6217683SBrian Somers 
126204eaa58cSBrian Somers static struct datalink *
126304eaa58cSBrian Somers bundle_DatalinkLinkout(struct bundle *bundle, struct datalink *dl)
1264cd7bd93aSBrian Somers {
1265cd7bd93aSBrian Somers   struct datalink **dlp;
1266cd7bd93aSBrian Somers 
1267cd7bd93aSBrian Somers   for (dlp = &bundle->links; *dlp; dlp = &(*dlp)->next)
1268cd7bd93aSBrian Somers     if (*dlp == dl) {
126904eaa58cSBrian Somers       *dlp = dl->next;
127004eaa58cSBrian Somers       dl->next = NULL;
127104eaa58cSBrian Somers       bundle_LinksRemoved(bundle);
127204eaa58cSBrian Somers       return dl;
1273cd7bd93aSBrian Somers     }
127404eaa58cSBrian Somers 
127504eaa58cSBrian Somers   return NULL;
127604eaa58cSBrian Somers }
127704eaa58cSBrian Somers 
127804eaa58cSBrian Somers static void
127904eaa58cSBrian Somers bundle_DatalinkLinkin(struct bundle *bundle, struct datalink *dl)
128004eaa58cSBrian Somers {
128104eaa58cSBrian Somers   struct datalink **dlp = &bundle->links;
128204eaa58cSBrian Somers 
128304eaa58cSBrian Somers   while (*dlp)
128404eaa58cSBrian Somers     dlp = &(*dlp)->next;
128504eaa58cSBrian Somers 
128604eaa58cSBrian Somers   *dlp = dl;
128704eaa58cSBrian Somers   dl->next = NULL;
128804eaa58cSBrian Somers 
128904eaa58cSBrian Somers   bundle_LinkAdded(bundle, dl);
1290ab2de065SBrian Somers   mp_CheckAutoloadTimer(&bundle->ncp.mp);
1291565e35e5SBrian Somers }
1292565e35e5SBrian Somers 
1293565e35e5SBrian Somers void
1294565e35e5SBrian Somers bundle_CleanDatalinks(struct bundle *bundle)
1295565e35e5SBrian Somers {
1296565e35e5SBrian Somers   struct datalink **dlp = &bundle->links;
129704eaa58cSBrian Somers   int found = 0;
1298565e35e5SBrian Somers 
1299565e35e5SBrian Somers   while (*dlp)
1300565e35e5SBrian Somers     if ((*dlp)->state == DATALINK_CLOSED &&
1301f6a4e748SBrian Somers         (*dlp)->physical->type &
1302f6a4e748SBrian Somers         (PHYS_DIRECT|PHYS_BACKGROUND|PHYS_FOREGROUND)) {
1303565e35e5SBrian Somers       *dlp = datalink_Destroy(*dlp);
130404eaa58cSBrian Somers       found++;
130504eaa58cSBrian Somers     } else
1306565e35e5SBrian Somers       dlp = &(*dlp)->next;
130704eaa58cSBrian Somers 
130804eaa58cSBrian Somers   if (found)
130904eaa58cSBrian Somers     bundle_LinksRemoved(bundle);
131004eaa58cSBrian Somers }
131104eaa58cSBrian Somers 
131204eaa58cSBrian Somers int
131304eaa58cSBrian Somers bundle_DatalinkClone(struct bundle *bundle, struct datalink *dl,
131404eaa58cSBrian Somers                      const char *name)
131504eaa58cSBrian Somers {
131604eaa58cSBrian Somers   if (bundle2datalink(bundle, name)) {
131704eaa58cSBrian Somers     log_Printf(LogWARN, "Clone: %s: name already exists\n", name);
131804eaa58cSBrian Somers     return 0;
131904eaa58cSBrian Somers   }
132004eaa58cSBrian Somers 
132104eaa58cSBrian Somers   bundle_DatalinkLinkin(bundle, datalink_Clone(dl, name));
132204eaa58cSBrian Somers   return 1;
132304eaa58cSBrian Somers }
132404eaa58cSBrian Somers 
132504eaa58cSBrian Somers void
132604eaa58cSBrian Somers bundle_DatalinkRemove(struct bundle *bundle, struct datalink *dl)
132704eaa58cSBrian Somers {
132804eaa58cSBrian Somers   dl = bundle_DatalinkLinkout(bundle, dl);
132904eaa58cSBrian Somers   if (dl)
133004eaa58cSBrian Somers     datalink_Destroy(dl);
1331cd7bd93aSBrian Somers }
133249052c95SBrian Somers 
133349052c95SBrian Somers void
133449052c95SBrian Somers bundle_SetLabel(struct bundle *bundle, const char *label)
133549052c95SBrian Somers {
133649052c95SBrian Somers   if (label)
133749052c95SBrian Somers     strncpy(bundle->cfg.label, label, sizeof bundle->cfg.label - 1);
133849052c95SBrian Somers   else
133949052c95SBrian Somers     *bundle->cfg.label = '\0';
134049052c95SBrian Somers }
134149052c95SBrian Somers 
134249052c95SBrian Somers const char *
134349052c95SBrian Somers bundle_GetLabel(struct bundle *bundle)
134449052c95SBrian Somers {
134549052c95SBrian Somers   return *bundle->cfg.label ? bundle->cfg.label : NULL;
134649052c95SBrian Somers }
13471fa665f5SBrian Somers 
13482cb305afSBrian Somers int
13492cb305afSBrian Somers bundle_LinkSize()
13501fa665f5SBrian Somers {
135196c9bb21SBrian Somers   struct iovec iov[SCATTER_SEGMENTS];
13522cb305afSBrian Somers   int niov, expect, f;
135387c3786eSBrian Somers 
135496c9bb21SBrian Somers   iov[0].iov_len = strlen(Version) + 1;
13552cb305afSBrian Somers   iov[0].iov_base = NULL;
13562cb305afSBrian Somers   niov = 1;
13572cb305afSBrian Somers   if (datalink2iov(NULL, iov, &niov, SCATTER_SEGMENTS, NULL, NULL) == -1) {
13582cb305afSBrian Somers     log_Printf(LogERROR, "Cannot determine space required for link\n");
13592cb305afSBrian Somers     return 0;
136054cd8e13SBrian Somers   }
13616f384573SBrian Somers 
136296c9bb21SBrian Somers   for (f = expect = 0; f < niov; f++)
136396c9bb21SBrian Somers     expect += iov[f].iov_len;
136496c9bb21SBrian Somers 
13652cb305afSBrian Somers   return expect;
136687c3786eSBrian Somers }
136796c9bb21SBrian Somers 
13682cb305afSBrian Somers void
13692cb305afSBrian Somers bundle_ReceiveDatalink(struct bundle *bundle, int s)
13702cb305afSBrian Somers {
13712cb305afSBrian Somers   char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int) * SEND_MAXFD];
1372cbee9754SBrian Somers   int niov, expect, f, *fd, nfd, onfd, got;
13732cb305afSBrian Somers   struct iovec iov[SCATTER_SEGMENTS];
13742cb305afSBrian Somers   struct cmsghdr *cmsg;
13752cb305afSBrian Somers   struct msghdr msg;
13762cb305afSBrian Somers   struct datalink *dl;
13772cb305afSBrian Somers   pid_t pid;
13782cb305afSBrian Somers 
13792cb305afSBrian Somers   log_Printf(LogPHASE, "Receiving datalink\n");
13802cb305afSBrian Somers 
13812cb305afSBrian Somers   /*
13822cb305afSBrian Somers    * Create our scatter/gather array - passing NULL gets the space
13832cb305afSBrian Somers    * allocation requirement rather than actually flattening the
13842cb305afSBrian Somers    * structures.
13852cb305afSBrian Somers    */
13862cb305afSBrian Somers   iov[0].iov_len = strlen(Version) + 1;
13872cb305afSBrian Somers   iov[0].iov_base = NULL;
13882cb305afSBrian Somers   niov = 1;
13892cb305afSBrian Somers   if (datalink2iov(NULL, iov, &niov, SCATTER_SEGMENTS, NULL, NULL) == -1) {
13902cb305afSBrian Somers     log_Printf(LogERROR, "Cannot determine space required for link\n");
13912cb305afSBrian Somers     return;
13922cb305afSBrian Somers   }
13932cb305afSBrian Somers 
13942cb305afSBrian Somers   /* Allocate the scatter/gather array for recvmsg() */
13952cb305afSBrian Somers   for (f = expect = 0; f < niov; f++) {
13962cb305afSBrian Somers     if ((iov[f].iov_base = malloc(iov[f].iov_len)) == NULL) {
13972cb305afSBrian Somers       log_Printf(LogERROR, "Cannot allocate space to receive link\n");
13982cb305afSBrian Somers       return;
13992cb305afSBrian Somers     }
1400cbee9754SBrian Somers     if (f)
14012cb305afSBrian Somers       expect += iov[f].iov_len;
14022cb305afSBrian Somers   }
14032cb305afSBrian Somers 
14042cb305afSBrian Somers   /* Set up our message */
14052cb305afSBrian Somers   cmsg = (struct cmsghdr *)cmsgbuf;
14062cb305afSBrian Somers   cmsg->cmsg_len = sizeof cmsgbuf;
14072cb305afSBrian Somers   cmsg->cmsg_level = SOL_SOCKET;
14082cb305afSBrian Somers   cmsg->cmsg_type = 0;
14092cb305afSBrian Somers 
141096c9bb21SBrian Somers   memset(&msg, '\0', sizeof msg);
14112cb305afSBrian Somers   msg.msg_name = NULL;
14122cb305afSBrian Somers   msg.msg_namelen = 0;
141396c9bb21SBrian Somers   msg.msg_iov = iov;
1414cbee9754SBrian Somers   msg.msg_iovlen = 1;		/* Only send the version at the first pass */
141596c9bb21SBrian Somers   msg.msg_control = cmsgbuf;
141696c9bb21SBrian Somers   msg.msg_controllen = sizeof cmsgbuf;
141796c9bb21SBrian Somers 
1418209dc102SBrian Somers   log_Printf(LogDEBUG, "Expecting %u scatter/gather bytes\n",
1419209dc102SBrian Somers              (unsigned)iov[0].iov_len);
14202cb305afSBrian Somers 
1421cbee9754SBrian Somers   if ((got = recvmsg(s, &msg, MSG_WAITALL)) != iov[0].iov_len) {
1422cbee9754SBrian Somers     if (got == -1)
142396c9bb21SBrian Somers       log_Printf(LogERROR, "Failed recvmsg: %s\n", strerror(errno));
142496c9bb21SBrian Somers     else
1425209dc102SBrian Somers       log_Printf(LogERROR, "Failed recvmsg: Got %d, not %u\n",
1426209dc102SBrian Somers                  got, (unsigned)iov[0].iov_len);
142796c9bb21SBrian Somers     while (niov--)
142896c9bb21SBrian Somers       free(iov[niov].iov_base);
142996c9bb21SBrian Somers     return;
143096c9bb21SBrian Somers   }
143196c9bb21SBrian Somers 
14322cb305afSBrian Somers   if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
14332cb305afSBrian Somers     log_Printf(LogERROR, "Recvmsg: no descriptors received !\n");
14342cb305afSBrian Somers     while (niov--)
14352cb305afSBrian Somers       free(iov[niov].iov_base);
14362cb305afSBrian Somers     return;
143787c3786eSBrian Somers   }
143887c3786eSBrian Somers 
14392bc21ed9SDavid Malone   fd = (int *)CMSG_DATA(cmsg);
14402bc21ed9SDavid Malone   nfd = ((caddr_t)cmsg + cmsg->cmsg_len - (caddr_t)fd) / sizeof(int);
14412cb305afSBrian Somers 
14422cb305afSBrian Somers   if (nfd < 2) {
14438e7bd08eSBrian Somers     log_Printf(LogERROR, "Recvmsg: %d descriptor%s received (too few) !\n",
14442cb305afSBrian Somers                nfd, nfd == 1 ? "" : "s");
14452cb305afSBrian Somers     while (nfd--)
14462cb305afSBrian Somers       close(fd[nfd]);
1447ad5b0e8bSBrian Somers     while (niov--)
1448ad5b0e8bSBrian Somers       free(iov[niov].iov_base);
1449ad5b0e8bSBrian Somers     return;
145054cd8e13SBrian Somers   }
145154cd8e13SBrian Somers 
145287c3786eSBrian Somers   /*
1453cbee9754SBrian Somers    * We've successfully received two or more open file descriptors
1454cbee9754SBrian Somers    * through our socket, plus a version string.  Make sure it's the
1455cbee9754SBrian Somers    * correct version, and drop the connection if it's not.
145687c3786eSBrian Somers    */
145796c9bb21SBrian Somers   if (strncmp(Version, iov[0].iov_base, iov[0].iov_len)) {
145896c9bb21SBrian Somers     log_Printf(LogWARN, "Cannot receive datalink, incorrect version"
145996c9bb21SBrian Somers                " (\"%.*s\", not \"%s\")\n", (int)iov[0].iov_len,
14607d81ddf5SBrian Somers                (char *)iov[0].iov_base, Version);
14612cb305afSBrian Somers     while (nfd--)
14622cb305afSBrian Somers       close(fd[nfd]);
146396c9bb21SBrian Somers     while (niov--)
146496c9bb21SBrian Somers       free(iov[niov].iov_base);
146596c9bb21SBrian Somers     return;
146696c9bb21SBrian Somers   }
146796c9bb21SBrian Somers 
1468cbee9754SBrian Somers   /*
1469cbee9754SBrian Somers    * Everything looks good.  Send the other side our process id so that
1470cbee9754SBrian Somers    * they can transfer lock ownership, and wait for them to send the
1471cbee9754SBrian Somers    * actual link data.
1472cbee9754SBrian Somers    */
1473cbee9754SBrian Somers   pid = getpid();
1474cbee9754SBrian Somers   if ((got = write(fd[1], &pid, sizeof pid)) != sizeof pid) {
1475cbee9754SBrian Somers     if (got == -1)
1476cbee9754SBrian Somers       log_Printf(LogERROR, "Failed write: %s\n", strerror(errno));
1477cbee9754SBrian Somers     else
1478cbee9754SBrian Somers       log_Printf(LogERROR, "Failed write: Got %d, not %d\n", got,
1479cbee9754SBrian Somers                  (int)(sizeof pid));
1480cbee9754SBrian Somers     while (nfd--)
1481cbee9754SBrian Somers       close(fd[nfd]);
1482cbee9754SBrian Somers     while (niov--)
1483cbee9754SBrian Somers       free(iov[niov].iov_base);
1484cbee9754SBrian Somers     return;
1485cbee9754SBrian Somers   }
1486cbee9754SBrian Somers 
1487cbee9754SBrian Somers   if ((got = readv(fd[1], iov + 1, niov - 1)) != expect) {
1488cbee9754SBrian Somers     if (got == -1)
1489cbee9754SBrian Somers       log_Printf(LogERROR, "Failed write: %s\n", strerror(errno));
1490cbee9754SBrian Somers     else
1491cbee9754SBrian Somers       log_Printf(LogERROR, "Failed write: Got %d, not %d\n", got, expect);
1492cbee9754SBrian Somers     while (nfd--)
1493cbee9754SBrian Somers       close(fd[nfd]);
1494cbee9754SBrian Somers     while (niov--)
1495cbee9754SBrian Somers       free(iov[niov].iov_base);
1496cbee9754SBrian Somers     return;
1497cbee9754SBrian Somers   }
1498cbee9754SBrian Somers   close(fd[1]);
1499cbee9754SBrian Somers 
15002cb305afSBrian Somers   onfd = nfd;	/* We've got this many in our array */
15018e7bd08eSBrian Somers   nfd -= 2;	/* Don't include p->fd and our reply descriptor */
15022cb305afSBrian Somers   niov = 1;	/* Skip the version id */
150387c3786eSBrian Somers   dl = iov2datalink(bundle, iov, &niov, sizeof iov / sizeof *iov, fd[0],
15042cb305afSBrian Somers                     fd + 2, &nfd);
1505b7c5748eSBrian Somers   if (dl) {
15062cb305afSBrian Somers 
150787c3786eSBrian Somers     if (nfd) {
150887c3786eSBrian Somers       log_Printf(LogERROR, "bundle_ReceiveDatalink: Failed to handle %d "
15092cb305afSBrian Somers                  "auxiliary file descriptors (%d remain)\n", onfd, nfd);
151087c3786eSBrian Somers       datalink_Destroy(dl);
151187c3786eSBrian Somers       while (nfd--)
151287c3786eSBrian Somers         close(fd[onfd--]);
15132cb305afSBrian Somers       close(fd[0]);
151487c3786eSBrian Somers     } else {
151504eaa58cSBrian Somers       bundle_DatalinkLinkin(bundle, dl);
1516b7c5748eSBrian Somers       datalink_AuthOk(dl);
1517ab2de065SBrian Somers       bundle_CalculateBandwidth(dl->bundle);
151887c3786eSBrian Somers     }
151987c3786eSBrian Somers   } else {
152087c3786eSBrian Somers     while (nfd--)
152187c3786eSBrian Somers       close(fd[onfd--]);
152287c3786eSBrian Somers     close(fd[0]);
15232cb305afSBrian Somers     close(fd[1]);
152487c3786eSBrian Somers   }
152596c9bb21SBrian Somers 
152696c9bb21SBrian Somers   free(iov[0].iov_base);
15271fa665f5SBrian Somers }
15281fa665f5SBrian Somers 
15291fa665f5SBrian Somers void
153096c9bb21SBrian Somers bundle_SendDatalink(struct datalink *dl, int s, struct sockaddr_un *sun)
15311fa665f5SBrian Somers {
15322bc21ed9SDavid Malone   char cmsgbuf[CMSG_SPACE(sizeof(int) * SEND_MAXFD)];
15332cb305afSBrian Somers   const char *constlock;
15342cb305afSBrian Somers   char *lock;
153587c3786eSBrian Somers   struct cmsghdr *cmsg;
153696c9bb21SBrian Somers   struct msghdr msg;
153796c9bb21SBrian Somers   struct iovec iov[SCATTER_SEGMENTS];
15382cb305afSBrian Somers   int niov, f, expect, newsid, fd[SEND_MAXFD], nfd, reply[2], got;
153985fd273aSBrian Somers   pid_t newpid;
15406f384573SBrian Somers 
1541dd7e2610SBrian Somers   log_Printf(LogPHASE, "Transmitting datalink %s\n", dl->name);
15426f384573SBrian Somers 
15432cb305afSBrian Somers   /* Record the base device name for a lock transfer later */
15442cb305afSBrian Somers   constlock = physical_LockedDevice(dl->physical);
15452cb305afSBrian Somers   if (constlock) {
15462cb305afSBrian Somers     lock = alloca(strlen(constlock) + 1);
15472cb305afSBrian Somers     strcpy(lock, constlock);
15482cb305afSBrian Somers   } else
15492cb305afSBrian Somers     lock = NULL;
15502cb305afSBrian Somers 
155104eaa58cSBrian Somers   bundle_LinkClosed(dl->bundle, dl);
15520f2f3eb3SBrian Somers   bundle_DatalinkLinkout(dl->bundle, dl);
1553ea722969SBrian Somers 
155496c9bb21SBrian Somers   /* Build our scatter/gather array */
155596c9bb21SBrian Somers   iov[0].iov_len = strlen(Version) + 1;
155696c9bb21SBrian Somers   iov[0].iov_base = strdup(Version);
155796c9bb21SBrian Somers   niov = 1;
155887c3786eSBrian Somers   nfd = 0;
15596f384573SBrian Somers 
15602cb305afSBrian Somers   fd[0] = datalink2iov(dl, iov, &niov, SCATTER_SEGMENTS, fd + 2, &nfd);
15616f384573SBrian Somers 
15622cb305afSBrian Somers   if (fd[0] != -1 && socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, reply) != -1) {
15632cb305afSBrian Somers     /*
15642cb305afSBrian Somers      * fd[1] is used to get the peer process id back, then to confirm that
15652cb305afSBrian Somers      * we've transferred any device locks to that process id.
15662cb305afSBrian Somers      */
15672cb305afSBrian Somers     fd[1] = reply[1];
156887c3786eSBrian Somers 
15692cb305afSBrian Somers     nfd += 2;			/* Include fd[0] and fd[1] */
157096c9bb21SBrian Somers     memset(&msg, '\0', sizeof msg);
157154cd8e13SBrian Somers 
15722cb305afSBrian Somers     msg.msg_name = NULL;
15732cb305afSBrian Somers     msg.msg_namelen = 0;
1574cbee9754SBrian Somers     /*
1575cbee9754SBrian Somers      * Only send the version to start...  We used to send the whole lot, but
1576cbee9754SBrian Somers      * this caused problems with our RECVBUF size as a single link is about
1577cbee9754SBrian Somers      * 22k !  This way, we should bump into no limits.
1578cbee9754SBrian Somers      */
1579cbee9754SBrian Somers     msg.msg_iovlen = 1;
158096c9bb21SBrian Somers     msg.msg_iov = iov;
15812cb305afSBrian Somers     msg.msg_control = cmsgbuf;
15822bc21ed9SDavid Malone     msg.msg_controllen = CMSG_SPACE(sizeof(int) * nfd);
15832cb305afSBrian Somers     msg.msg_flags = 0;
158454cd8e13SBrian Somers 
15852cb305afSBrian Somers     cmsg = (struct cmsghdr *)cmsgbuf;
15862cb305afSBrian Somers     cmsg->cmsg_len = msg.msg_controllen;
158754cd8e13SBrian Somers     cmsg->cmsg_level = SOL_SOCKET;
158854cd8e13SBrian Somers     cmsg->cmsg_type = SCM_RIGHTS;
158987c3786eSBrian Somers 
15902cb305afSBrian Somers     for (f = 0; f < nfd; f++)
15912bc21ed9SDavid Malone       *((int *)CMSG_DATA(cmsg) + f) = fd[f];
159247723d29SBrian Somers 
1593cbee9754SBrian Somers     for (f = 1, expect = 0; f < niov; f++)
159496c9bb21SBrian Somers       expect += iov[f].iov_len;
159547723d29SBrian Somers 
1596cbee9754SBrian Somers     if (setsockopt(reply[0], SOL_SOCKET, SO_SNDBUF, &expect, sizeof(int)) == -1)
1597cbee9754SBrian Somers       log_Printf(LogERROR, "setsockopt(SO_RCVBUF, %d): %s\n", expect,
1598cbee9754SBrian Somers                  strerror(errno));
1599cbee9754SBrian Somers     if (setsockopt(reply[1], SOL_SOCKET, SO_RCVBUF, &expect, sizeof(int)) == -1)
1600cbee9754SBrian Somers       log_Printf(LogERROR, "setsockopt(SO_RCVBUF, %d): %s\n", expect,
1601cbee9754SBrian Somers                  strerror(errno));
1602cbee9754SBrian Somers 
1603209dc102SBrian Somers     log_Printf(LogDEBUG, "Sending %d descriptor%s and %u bytes in scatter"
1604209dc102SBrian Somers                "/gather array\n", nfd, nfd == 1 ? "" : "s",
1605209dc102SBrian Somers                (unsigned)iov[0].iov_len);
160647723d29SBrian Somers 
16072cb305afSBrian Somers     if ((got = sendmsg(s, &msg, 0)) == -1)
16082cb305afSBrian Somers       log_Printf(LogERROR, "Failed sendmsg: %s: %s\n",
16092cb305afSBrian Somers                  sun->sun_path, strerror(errno));
1610cbee9754SBrian Somers     else if (got != iov[0].iov_len)
1611209dc102SBrian Somers       log_Printf(LogERROR, "%s: Failed initial sendmsg: Only sent %d of %u\n",
1612209dc102SBrian Somers                  sun->sun_path, got, (unsigned)iov[0].iov_len);
16132cb305afSBrian Somers     else {
16148e7bd08eSBrian Somers       /* We must get the ACK before closing the descriptor ! */
16152cb305afSBrian Somers       int res;
16162cb305afSBrian Somers 
1617cbee9754SBrian Somers       if ((got = read(reply[0], &newpid, sizeof newpid)) == sizeof newpid) {
161842df3c25SBrian Somers         log_Printf(LogDEBUG, "Received confirmation from pid %ld\n",
161942df3c25SBrian Somers                    (long)newpid);
16202cb305afSBrian Somers         if (lock && (res = ID0uu_lock_txfr(lock, newpid)) != UU_LOCK_OK)
1621b42135deSBrian Somers             log_Printf(LogERROR, "uu_lock_txfr: %s\n", uu_lockerr(res));
16222cb305afSBrian Somers 
1623cbee9754SBrian Somers         log_Printf(LogDEBUG, "Transmitting link (%d bytes)\n", expect);
1624cbee9754SBrian Somers         if ((got = writev(reply[0], iov + 1, niov - 1)) != expect) {
1625cbee9754SBrian Somers           if (got == -1)
1626cbee9754SBrian Somers             log_Printf(LogERROR, "%s: Failed writev: %s\n",
1627cbee9754SBrian Somers                        sun->sun_path, strerror(errno));
1628cbee9754SBrian Somers           else
1629cbee9754SBrian Somers             log_Printf(LogERROR, "%s: Failed writev: Wrote %d of %d\n",
1630cbee9754SBrian Somers                        sun->sun_path, got, expect);
1631cbee9754SBrian Somers         }
1632cbee9754SBrian Somers       } else if (got == -1)
1633cbee9754SBrian Somers         log_Printf(LogERROR, "%s: Failed socketpair read: %s\n",
1634cbee9754SBrian Somers                    sun->sun_path, strerror(errno));
1635cbee9754SBrian Somers       else
1636cbee9754SBrian Somers         log_Printf(LogERROR, "%s: Failed socketpair read: Got %d of %d\n",
1637cbee9754SBrian Somers                    sun->sun_path, got, (int)(sizeof newpid));
16382cb305afSBrian Somers     }
16392cb305afSBrian Somers 
16402cb305afSBrian Somers     close(reply[0]);
16412cb305afSBrian Somers     close(reply[1]);
164254cd8e13SBrian Somers 
1643ac685e31SBrian Somers     newsid = Enabled(dl->bundle, OPT_KEEPSESSION) ||
164487c3786eSBrian Somers              tcgetpgrp(fd[0]) == getpgrp();
164587c3786eSBrian Somers     while (nfd)
164687c3786eSBrian Somers       close(fd[--nfd]);
16471384bd27SBrian Somers     if (newsid)
16482cb305afSBrian Somers       bundle_setsid(dl->bundle, got != -1);
164947723d29SBrian Somers   }
165085fd273aSBrian Somers   close(s);
165196c9bb21SBrian Somers 
165296c9bb21SBrian Somers   while (niov--)
165396c9bb21SBrian Somers     free(iov[niov].iov_base);
16541fa665f5SBrian Somers }
1655dd0645c5SBrian Somers 
1656dd0645c5SBrian Somers int
165758d55334SBrian Somers bundle_RenameDatalink(struct bundle *bundle, struct datalink *ndl,
165858d55334SBrian Somers                       const char *name)
165958d55334SBrian Somers {
166058d55334SBrian Somers   struct datalink *dl;
166158d55334SBrian Somers 
166258d55334SBrian Somers   if (!strcasecmp(ndl->name, name))
166358d55334SBrian Somers     return 1;
166458d55334SBrian Somers 
166558d55334SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
166658d55334SBrian Somers     if (!strcasecmp(dl->name, name))
166758d55334SBrian Somers       return 0;
166858d55334SBrian Somers 
166958d55334SBrian Somers   datalink_Rename(ndl, name);
167058d55334SBrian Somers   return 1;
167158d55334SBrian Somers }
167258d55334SBrian Somers 
167358d55334SBrian Somers int
1674dd0645c5SBrian Somers bundle_SetMode(struct bundle *bundle, struct datalink *dl, int mode)
1675dd0645c5SBrian Somers {
1676dd0645c5SBrian Somers   int omode;
1677dd0645c5SBrian Somers 
1678dd0645c5SBrian Somers   omode = dl->physical->type;
1679dd0645c5SBrian Somers   if (omode == mode)
1680dd0645c5SBrian Somers     return 1;
1681dd0645c5SBrian Somers 
1682ff0f9439SBrian Somers   if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO))
1683ff0f9439SBrian Somers     /* First auto link */
1684dd0645c5SBrian Somers     if (bundle->ncp.ipcp.peer_ip.s_addr == INADDR_ANY) {
1685ff0f9439SBrian Somers       log_Printf(LogWARN, "You must `set ifaddr' or `open' before"
1686ff0f9439SBrian Somers                  " changing mode to %s\n", mode2Nam(mode));
1687dd0645c5SBrian Somers       return 0;
1688dd0645c5SBrian Somers     }
1689dd0645c5SBrian Somers 
1690dd0645c5SBrian Somers   if (!datalink_SetMode(dl, mode))
1691dd0645c5SBrian Somers     return 0;
1692dd0645c5SBrian Somers 
1693ff0f9439SBrian Somers   if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO) &&
1694ff0f9439SBrian Somers       bundle->phase != PHASE_NETWORK)
1695ff0f9439SBrian Somers     /* First auto link, we need an interface */
1696dd0645c5SBrian Somers     ipcp_InterfaceUp(&bundle->ncp.ipcp);
1697dd0645c5SBrian Somers 
1698ab2de065SBrian Somers   /* Regenerate phys_type and adjust idle timer */
169904eaa58cSBrian Somers   bundle_LinksRemoved(bundle);
1700dd0645c5SBrian Somers 
1701dd0645c5SBrian Somers   return 1;
1702dd0645c5SBrian Somers }
17031384bd27SBrian Somers 
17041384bd27SBrian Somers void
17051384bd27SBrian Somers bundle_setsid(struct bundle *bundle, int holdsession)
17061384bd27SBrian Somers {
17071384bd27SBrian Somers   /*
17081384bd27SBrian Somers    * Lose the current session.  This means getting rid of our pid
17091384bd27SBrian Somers    * too so that the tty device will really go away, and any getty
17101384bd27SBrian Somers    * etc will be allowed to restart.
17111384bd27SBrian Somers    */
17121384bd27SBrian Somers   pid_t pid, orig;
17131384bd27SBrian Somers   int fds[2];
17141384bd27SBrian Somers   char done;
17151384bd27SBrian Somers   struct datalink *dl;
17161384bd27SBrian Somers 
1717e62ce959SBrian Somers   if (!holdsession && bundle_IsDead(bundle)) {
1718e62ce959SBrian Somers     /*
1719e62ce959SBrian Somers      * No need to lose our session after all... we're going away anyway
1720e62ce959SBrian Somers      *
1721e62ce959SBrian Somers      * We should really stop the timer and pause if holdsession is set and
1722e62ce959SBrian Somers      * the bundle's dead, but that leaves other resources lying about :-(
1723e62ce959SBrian Somers      */
1724e62ce959SBrian Somers     return;
1725e62ce959SBrian Somers   }
1726e62ce959SBrian Somers 
17271384bd27SBrian Somers   orig = getpid();
17281384bd27SBrian Somers   if (pipe(fds) == -1) {
17291384bd27SBrian Somers     log_Printf(LogERROR, "pipe: %s\n", strerror(errno));
17301384bd27SBrian Somers     return;
17311384bd27SBrian Somers   }
17321384bd27SBrian Somers   switch ((pid = fork())) {
17331384bd27SBrian Somers     case -1:
17341384bd27SBrian Somers       log_Printf(LogERROR, "fork: %s\n", strerror(errno));
17351384bd27SBrian Somers       close(fds[0]);
17361384bd27SBrian Somers       close(fds[1]);
17371384bd27SBrian Somers       return;
17381384bd27SBrian Somers     case 0:
17391384bd27SBrian Somers       close(fds[1]);
17404be0e57dSBrian Somers       read(fds[0], &done, 1);		/* uu_locks are mine ! */
17414be0e57dSBrian Somers       close(fds[0]);
17421384bd27SBrian Somers       if (pipe(fds) == -1) {
17431384bd27SBrian Somers         log_Printf(LogERROR, "pipe(2): %s\n", strerror(errno));
17441384bd27SBrian Somers         return;
17451384bd27SBrian Somers       }
17461384bd27SBrian Somers       switch ((pid = fork())) {
17471384bd27SBrian Somers         case -1:
1748a33b2ef7SBrian Somers           log_Printf(LogERROR, "fork(2): %s\n", strerror(errno));
17491384bd27SBrian Somers           close(fds[0]);
17501384bd27SBrian Somers           close(fds[1]);
17511384bd27SBrian Somers           return;
17521384bd27SBrian Somers         case 0:
17531384bd27SBrian Somers           close(fds[1]);
17544be0e57dSBrian Somers           bundle_LockTun(bundle);	/* update pid */
17554be0e57dSBrian Somers           read(fds[0], &done, 1);	/* uu_locks are mine ! */
17564be0e57dSBrian Somers           close(fds[0]);
17571384bd27SBrian Somers           setsid();
175806b47306SBrian Somers           bundle_ChangedPID(bundle);
175942df3c25SBrian Somers           log_Printf(LogDEBUG, "%ld -> %ld: %s session control\n",
176042df3c25SBrian Somers                      (long)orig, (long)getpid(),
17611384bd27SBrian Somers                      holdsession ? "Passed" : "Dropped");
17627e778f13SBrian Somers           timer_InitService(0);		/* Start the Timer Service */
17631384bd27SBrian Somers           break;
17641384bd27SBrian Somers         default:
17654be0e57dSBrian Somers           close(fds[0]);
17665d9e6103SBrian Somers           /* Give away all our physical locks (to the final process) */
17671384bd27SBrian Somers           for (dl = bundle->links; dl; dl = dl->next)
17681384bd27SBrian Somers             if (dl->state != DATALINK_CLOSED)
17695d9e6103SBrian Somers               physical_ChangedPid(dl->physical, pid);
17704be0e57dSBrian Somers           write(fds[1], "!", 1);	/* done */
17714be0e57dSBrian Somers           close(fds[1]);
17722cb305afSBrian Somers           _exit(0);
17731384bd27SBrian Somers           break;
17741384bd27SBrian Somers       }
17751384bd27SBrian Somers       break;
17761384bd27SBrian Somers     default:
17774be0e57dSBrian Somers       close(fds[0]);
17785d9e6103SBrian Somers       /* Give away all our physical locks (to the intermediate process) */
17791384bd27SBrian Somers       for (dl = bundle->links; dl; dl = dl->next)
17801384bd27SBrian Somers         if (dl->state != DATALINK_CLOSED)
17815d9e6103SBrian Somers           physical_ChangedPid(dl->physical, pid);
17824be0e57dSBrian Somers       write(fds[1], "!", 1);	/* done */
17834be0e57dSBrian Somers       close(fds[1]);
17841384bd27SBrian Somers       if (holdsession) {
17851384bd27SBrian Somers         int fd, status;
17861384bd27SBrian Somers 
17871384bd27SBrian Somers         timer_TermService();
17881384bd27SBrian Somers         signal(SIGPIPE, SIG_DFL);
17891384bd27SBrian Somers         signal(SIGALRM, SIG_DFL);
17901384bd27SBrian Somers         signal(SIGHUP, SIG_DFL);
17911384bd27SBrian Somers         signal(SIGTERM, SIG_DFL);
17921384bd27SBrian Somers         signal(SIGINT, SIG_DFL);
17931384bd27SBrian Somers         signal(SIGQUIT, SIG_DFL);
17941384bd27SBrian Somers         for (fd = getdtablesize(); fd >= 0; fd--)
17951384bd27SBrian Somers           close(fd);
17961384bd27SBrian Somers         /*
17971384bd27SBrian Somers          * Reap the intermediate process.  As we're not exiting but the
17981384bd27SBrian Somers          * intermediate is, we don't want it to become defunct.
17991384bd27SBrian Somers          */
18001384bd27SBrian Somers         waitpid(pid, &status, 0);
18018e7b8599SBrian Somers         /* Tweak our process arguments.... */
1802ebe96675SBrian Somers         SetTitle("session owner");
180368602c3eSBrian Somers #ifndef NOSUID
1804a19a5c02SBrian Somers         setuid(ID0realuid());
180568602c3eSBrian Somers #endif
18061384bd27SBrian Somers         /*
18071384bd27SBrian Somers          * Hang around for a HUP.  This should happen as soon as the
18088e7bd08eSBrian Somers          * ppp that we passed our ctty descriptor to closes it.
18098e7bd08eSBrian Somers          * NOTE: If this process dies, the passed descriptor becomes
18101384bd27SBrian Somers          *       invalid and will give a select() error by setting one
18111384bd27SBrian Somers          *       of the error fds, aborting the other ppp.  We don't
18121384bd27SBrian Somers          *       want that to happen !
18131384bd27SBrian Somers          */
18141384bd27SBrian Somers         pause();
18151384bd27SBrian Somers       }
18162cb305afSBrian Somers       _exit(0);
18171384bd27SBrian Somers       break;
18181384bd27SBrian Somers   }
18191384bd27SBrian Somers }
18209b5f8ffdSBrian Somers 
18219b5f8ffdSBrian Somers int
18229b5f8ffdSBrian Somers bundle_HighestState(struct bundle *bundle)
18239b5f8ffdSBrian Somers {
18249b5f8ffdSBrian Somers   struct datalink *dl;
18259b5f8ffdSBrian Somers   int result = DATALINK_CLOSED;
18269b5f8ffdSBrian Somers 
18279b5f8ffdSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
18289b5f8ffdSBrian Somers     if (result < dl->state)
18299b5f8ffdSBrian Somers       result = dl->state;
18309b5f8ffdSBrian Somers 
18319b5f8ffdSBrian Somers   return result;
18329b5f8ffdSBrian Somers }
1833991c2a7bSBrian Somers 
1834991c2a7bSBrian Somers int
1835991c2a7bSBrian Somers bundle_Exception(struct bundle *bundle, int fd)
1836991c2a7bSBrian Somers {
1837991c2a7bSBrian Somers   struct datalink *dl;
1838991c2a7bSBrian Somers 
1839991c2a7bSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
1840991c2a7bSBrian Somers     if (dl->physical->fd == fd) {
1841991c2a7bSBrian Somers       datalink_Down(dl, CLOSE_NORMAL);
1842991c2a7bSBrian Somers       return 1;
1843991c2a7bSBrian Somers     }
1844991c2a7bSBrian Somers 
1845991c2a7bSBrian Somers   return 0;
1846991c2a7bSBrian Somers }
18471d1fc017SBrian Somers 
18481d1fc017SBrian Somers void
184930949fd4SBrian Somers bundle_AdjustFilters(struct bundle *bundle, struct ncpaddr *local,
185030949fd4SBrian Somers                      struct ncpaddr *remote)
18511d1fc017SBrian Somers {
185230949fd4SBrian Somers   filter_AdjustAddr(&bundle->filter.in, local, remote, NULL);
185330949fd4SBrian Somers   filter_AdjustAddr(&bundle->filter.out, local, remote, NULL);
185430949fd4SBrian Somers   filter_AdjustAddr(&bundle->filter.dial, local, remote, NULL);
185530949fd4SBrian Somers   filter_AdjustAddr(&bundle->filter.alive, local, remote, NULL);
1856d568d6c4SBrian Somers }
1857d568d6c4SBrian Somers 
1858d568d6c4SBrian Somers void
185930949fd4SBrian Somers bundle_AdjustDNS(struct bundle *bundle)
1860d568d6c4SBrian Somers {
186130949fd4SBrian Somers   struct in_addr *dns = bundle->ncp.ipcp.ns.dns;
186230949fd4SBrian Somers 
1863d568d6c4SBrian Somers   filter_AdjustAddr(&bundle->filter.in, NULL, NULL, dns);
1864d568d6c4SBrian Somers   filter_AdjustAddr(&bundle->filter.out, NULL, NULL, dns);
1865d568d6c4SBrian Somers   filter_AdjustAddr(&bundle->filter.dial, NULL, NULL, dns);
1866d568d6c4SBrian Somers   filter_AdjustAddr(&bundle->filter.alive, NULL, NULL, dns);
18671d1fc017SBrian Somers }
1868ab2de065SBrian Somers 
1869ab2de065SBrian Somers void
1870ab2de065SBrian Somers bundle_CalculateBandwidth(struct bundle *bundle)
1871ab2de065SBrian Somers {
1872ab2de065SBrian Somers   struct datalink *dl;
18736301d506SBrian Somers   int sp, overhead, maxoverhead;
1874ab2de065SBrian Somers 
1875ab2de065SBrian Somers   bundle->bandwidth = 0;
1876c8b9fb53SBrian Somers   bundle->iface->mtu = 0;
18776301d506SBrian Somers   maxoverhead = 0;
18786301d506SBrian Somers 
18796301d506SBrian Somers   for (dl = bundle->links; dl; dl = dl->next) {
18806301d506SBrian Somers     overhead = ccp_MTUOverhead(&dl->physical->link.ccp);
18816301d506SBrian Somers     if (maxoverhead < overhead)
18826301d506SBrian Somers       maxoverhead = overhead;
1883ab2de065SBrian Somers     if (dl->state == DATALINK_OPEN) {
1884ab2de065SBrian Somers       if ((sp = dl->mp.bandwidth) == 0 &&
1885ab2de065SBrian Somers           (sp = physical_GetSpeed(dl->physical)) == 0)
1886ab2de065SBrian Somers         log_Printf(LogDEBUG, "%s: %s: Cannot determine bandwidth\n",
1887ab2de065SBrian Somers                    dl->name, dl->physical->name.full);
1888ab2de065SBrian Somers       else
1889ab2de065SBrian Somers         bundle->bandwidth += sp;
1890ab2de065SBrian Somers       if (!bundle->ncp.mp.active) {
1891c8b9fb53SBrian Somers         bundle->iface->mtu = dl->physical->link.lcp.his_mru;
1892ab2de065SBrian Somers         break;
1893ab2de065SBrian Somers       }
1894ab2de065SBrian Somers     }
18956301d506SBrian Somers   }
1896ab2de065SBrian Somers 
1897ab2de065SBrian Somers   if (bundle->bandwidth == 0)
1898ab2de065SBrian Somers     bundle->bandwidth = 115200;		/* Shrug */
1899ab2de065SBrian Somers 
19006301d506SBrian Somers   if (bundle->ncp.mp.active) {
1901c8b9fb53SBrian Somers     bundle->iface->mtu = bundle->ncp.mp.peer_mrru;
19026301d506SBrian Somers     overhead = ccp_MTUOverhead(&bundle->ncp.mp.link.ccp);
19036301d506SBrian Somers     if (maxoverhead < overhead)
19046301d506SBrian Somers       maxoverhead = overhead;
19056301d506SBrian Somers   } else if (!bundle->iface->mtu)
1906c8b9fb53SBrian Somers     bundle->iface->mtu = DEF_MRU;
1907ab2de065SBrian Somers 
1908ab2de065SBrian Somers #ifndef NORADIUS
190994d7be52SBrian Somers   if (bundle->radius.valid && bundle->radius.mtu &&
1910c8b9fb53SBrian Somers       bundle->radius.mtu < bundle->iface->mtu) {
1911ab2de065SBrian Somers     log_Printf(LogLCP, "Reducing MTU to radius value %lu\n",
1912ab2de065SBrian Somers                bundle->radius.mtu);
1913c8b9fb53SBrian Somers     bundle->iface->mtu = bundle->radius.mtu;
1914ab2de065SBrian Somers   }
1915ab2de065SBrian Somers #endif
1916ab2de065SBrian Somers 
19176301d506SBrian Somers   if (maxoverhead) {
19186301d506SBrian Somers     log_Printf(LogLCP, "Reducing MTU from %d to %d (CCP requirement)\n",
19196301d506SBrian Somers                bundle->iface->mtu, bundle->iface->mtu - maxoverhead);
19206301d506SBrian Somers     bundle->iface->mtu -= maxoverhead;
19216301d506SBrian Somers   }
19226301d506SBrian Somers 
192394d7be52SBrian Somers   tun_configure(bundle);
192403a2501aSBrian Somers 
192503a2501aSBrian Somers   route_UpdateMTU(bundle);
1926ab2de065SBrian Somers }
1927ab2de065SBrian Somers 
1928ab2de065SBrian Somers void
1929ab2de065SBrian Somers bundle_AutoAdjust(struct bundle *bundle, int percent, int what)
1930ab2de065SBrian Somers {
1931ab2de065SBrian Somers   struct datalink *dl, *choice, *otherlinkup;
1932ab2de065SBrian Somers 
1933ab2de065SBrian Somers   choice = otherlinkup = NULL;
1934ab2de065SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
1935ab2de065SBrian Somers     if (dl->physical->type == PHYS_AUTO) {
1936ab2de065SBrian Somers       if (dl->state == DATALINK_OPEN) {
1937ab2de065SBrian Somers         if (what == AUTO_DOWN) {
1938ab2de065SBrian Somers           if (choice)
1939ab2de065SBrian Somers             otherlinkup = choice;
1940ab2de065SBrian Somers           choice = dl;
1941ab2de065SBrian Somers         }
1942ab2de065SBrian Somers       } else if (dl->state == DATALINK_CLOSED) {
1943ab2de065SBrian Somers         if (what == AUTO_UP) {
1944ab2de065SBrian Somers           choice = dl;
1945ab2de065SBrian Somers           break;
1946ab2de065SBrian Somers         }
1947ab2de065SBrian Somers       } else {
1948ab2de065SBrian Somers         /* An auto link in an intermediate state - forget it for the moment */
1949ab2de065SBrian Somers         choice = NULL;
1950ab2de065SBrian Somers         break;
1951ab2de065SBrian Somers       }
1952ab2de065SBrian Somers     } else if (dl->state == DATALINK_OPEN && what == AUTO_DOWN)
1953ab2de065SBrian Somers       otherlinkup = dl;
1954ab2de065SBrian Somers 
1955ab2de065SBrian Somers   if (choice) {
1956ab2de065SBrian Somers     if (what == AUTO_UP) {
1957ab2de065SBrian Somers       log_Printf(LogPHASE, "%d%% saturation -> Opening link ``%s''\n",
1958ab2de065SBrian Somers                  percent, choice->name);
1959ab2de065SBrian Somers       datalink_Up(choice, 1, 1);
1960a339e644SBrian Somers       mp_CheckAutoloadTimer(&bundle->ncp.mp);
1961ab2de065SBrian Somers     } else if (otherlinkup) {	/* Only bring the second-last link down */
1962ab2de065SBrian Somers       log_Printf(LogPHASE, "%d%% saturation -> Closing link ``%s''\n",
1963ab2de065SBrian Somers                  percent, choice->name);
1964d2f5232dSBrian Somers       datalink_Close(choice, CLOSE_STAYDOWN);
1965a339e644SBrian Somers       mp_CheckAutoloadTimer(&bundle->ncp.mp);
1966ab2de065SBrian Somers     }
1967ab2de065SBrian Somers   }
1968ab2de065SBrian Somers }
1969ab2de065SBrian Somers 
1970ab2de065SBrian Somers int
1971ab2de065SBrian Somers bundle_WantAutoloadTimer(struct bundle *bundle)
1972ab2de065SBrian Somers {
1973ab2de065SBrian Somers   struct datalink *dl;
1974ab2de065SBrian Somers   int autolink, opened;
1975ab2de065SBrian Somers 
1976ab2de065SBrian Somers   if (bundle->phase == PHASE_NETWORK) {
1977ab2de065SBrian Somers     for (autolink = opened = 0, dl = bundle->links; dl; dl = dl->next)
1978ab2de065SBrian Somers       if (dl->physical->type == PHYS_AUTO) {
1979ab2de065SBrian Somers         if (++autolink == 2 || (autolink == 1 && opened))
1980ab2de065SBrian Somers           /* Two auto links or one auto and one open in NETWORK phase */
1981ab2de065SBrian Somers           return 1;
1982ab2de065SBrian Somers       } else if (dl->state == DATALINK_OPEN) {
1983ab2de065SBrian Somers         opened++;
1984ab2de065SBrian Somers         if (autolink)
1985ab2de065SBrian Somers           /* One auto and one open link in NETWORK phase */
1986ab2de065SBrian Somers           return 1;
1987ab2de065SBrian Somers       }
1988ab2de065SBrian Somers   }
1989ab2de065SBrian Somers 
1990ab2de065SBrian Somers   return 0;
1991ab2de065SBrian Somers }
199206b47306SBrian Somers 
199306b47306SBrian Somers void
199406b47306SBrian Somers bundle_ChangedPID(struct bundle *bundle)
199506b47306SBrian Somers {
199606b47306SBrian Somers #ifdef TUNSIFPID
199706b47306SBrian Somers   ioctl(bundle->dev.fd, TUNSIFPID, 0);
199806b47306SBrian Somers #endif
199906b47306SBrian Somers }
200046df5aa7SBrian Somers 
200146df5aa7SBrian Somers int
200246df5aa7SBrian Somers bundle_Uptime(struct bundle *bundle)
200346df5aa7SBrian Somers {
200446df5aa7SBrian Somers   if (bundle->upat)
200546df5aa7SBrian Somers     return time(NULL) - bundle->upat;
200646df5aa7SBrian Somers 
200746df5aa7SBrian Somers   return 0;
200846df5aa7SBrian Somers }
2009