xref: /freebsd/usr.sbin/ppp/bundle.c (revision 356bf92dce9195fab31f0dfba7163ac9c54059e5)
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 
230ff0f9439SBrian Somers   if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL))
231ff0f9439SBrian Somers       != bundle->phys_type.open && bundle->idle.timer.state == TIMER_STOPPED)
232ff0f9439SBrian Somers     /* We may need to start our idle timer */
2330a4b6c5cSBrian Somers     bundle_StartIdleTimer(bundle, 0);
234ff0f9439SBrian Somers }
235ff0f9439SBrian Somers 
23692b09558SBrian Somers void
237ff0f9439SBrian Somers bundle_LinksRemoved(struct bundle *bundle)
238ff0f9439SBrian Somers {
239ff0f9439SBrian Somers   struct datalink *dl;
240ff0f9439SBrian Somers 
241ff0f9439SBrian Somers   bundle->phys_type.all = bundle->phys_type.open = 0;
242ff0f9439SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
243ff0f9439SBrian Somers     bundle_LinkAdded(bundle, dl);
244ff0f9439SBrian Somers 
245ab2de065SBrian Somers   bundle_CalculateBandwidth(bundle);
246ab2de065SBrian Somers   mp_CheckAutoloadTimer(&bundle->ncp.mp);
247ab2de065SBrian Somers 
248ff0f9439SBrian Somers   if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL))
249ff0f9439SBrian Somers       == bundle->phys_type.open)
250ff0f9439SBrian Somers     bundle_StopIdleTimer(bundle);
251ff0f9439SBrian Somers }
25204eaa58cSBrian Somers 
25304eaa58cSBrian Somers static void
2546f384573SBrian Somers bundle_LayerUp(void *v, struct fsm *fp)
2557a6f8720SBrian Somers {
2563006ec67SBrian Somers   /*
2573006ec67SBrian Somers    * The given fsm is now up
258ab2de065SBrian Somers    * If it's an LCP, adjust our phys_mode.open value and check the
259ab2de065SBrian Somers    * autoload timer.
260ab2de065SBrian Somers    * If it's the first NCP, calculate our bandwidth
261dade2407SBrian Somers    * If it's the first NCP, set our ``upat'' time
2623b0f8d2eSBrian Somers    * If it's the first NCP, start the idle timer.
263ab2de065SBrian Somers    * If it's an NCP, tell our -background parent to go away.
264ab2de065SBrian Somers    * If it's the first NCP, start the autoload timer
2653006ec67SBrian Somers    */
2666f384573SBrian Somers   struct bundle *bundle = (struct bundle *)v;
2676d666775SBrian Somers 
2685563ebdeSBrian Somers   if (fp->proto == PROTO_LCP) {
269ff0f9439SBrian Somers     struct physical *p = link2physical(fp->link);
270ff0f9439SBrian Somers 
271ff0f9439SBrian Somers     bundle_LinkAdded(bundle, p->dl);
272ab2de065SBrian Somers     mp_CheckAutoloadTimer(&bundle->ncp.mp);
27330949fd4SBrian Somers   } else if (isncp(fp->proto)) {
27430949fd4SBrian Somers     if (ncp_LayersOpen(&fp->bundle->ncp) == 1) {
275ab2de065SBrian Somers       bundle_CalculateBandwidth(fp->bundle);
276dade2407SBrian Somers       time(&bundle->upat);
2770a4b6c5cSBrian Somers       bundle_StartIdleTimer(bundle, 0);
278ab2de065SBrian Somers       mp_CheckAutoloadTimer(&fp->bundle->ncp.mp);
27930949fd4SBrian Somers     }
28030949fd4SBrian Somers     bundle_Notify(bundle, EX_NORMAL);
2816301d506SBrian Somers   } else if (fp->proto == PROTO_CCP)
2826301d506SBrian Somers     bundle_CalculateBandwidth(fp->bundle);	/* Against ccp_MTUOverhead */
283ab886ad0SBrian Somers }
2847a6f8720SBrian Somers 
2856d666775SBrian Somers static void
2866d666775SBrian Somers bundle_LayerDown(void *v, struct fsm *fp)
2876d666775SBrian Somers {
2886d666775SBrian Somers   /*
2896d666775SBrian Somers    * The given FSM has been told to come down.
290ab886ad0SBrian Somers    * If it's our last NCP, stop the idle timer.
291dade2407SBrian Somers    * If it's our last NCP, clear our ``upat'' value.
292ab2de065SBrian Somers    * If it's our last NCP, stop the autoload timer
293ff0f9439SBrian Somers    * If it's an LCP, adjust our phys_type.open value and any timers.
29486b1f0d7SBrian Somers    * If it's an LCP and we're in multilink mode, adjust our tun
295c8f30703SBrian Somers    * If it's the last LCP, down all NCPs
29686b1f0d7SBrian Somers    * speed and make sure our minimum sequence number is adjusted.
2976d666775SBrian Somers    */
298ab886ad0SBrian Somers 
299ab886ad0SBrian Somers   struct bundle *bundle = (struct bundle *)v;
300ab886ad0SBrian Somers 
30130949fd4SBrian Somers   if (isncp(fp->proto)) {
30230949fd4SBrian Somers     if (ncp_LayersOpen(&fp->bundle->ncp) == 0) {
303ab886ad0SBrian Somers       bundle_StopIdleTimer(bundle);
304dade2407SBrian Somers       bundle->upat = 0;
305ab2de065SBrian Somers       mp_StopAutoloadTimer(&bundle->ncp.mp);
30630949fd4SBrian Somers     }
307ab2de065SBrian Somers   } else if (fp->proto == PROTO_LCP) {
3083b0f8d2eSBrian Somers     struct datalink *dl;
30986b1f0d7SBrian Somers     struct datalink *lost;
310c8f30703SBrian Somers     int others_active;
311c8f30703SBrian Somers 
312c8f30703SBrian Somers     bundle_LinksRemoved(bundle);  /* adjust timers & phys_type values */
3133b0f8d2eSBrian Somers 
31486b1f0d7SBrian Somers     lost = NULL;
315c8f30703SBrian Somers     others_active = 0;
316c8f30703SBrian Somers     for (dl = bundle->links; dl; dl = dl->next) {
31786b1f0d7SBrian Somers       if (fp == &dl->physical->link.lcp.fsm)
31886b1f0d7SBrian Somers         lost = dl;
319c8f30703SBrian Somers       else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP)
320c8f30703SBrian Somers         others_active++;
321c8f30703SBrian Somers     }
32286b1f0d7SBrian Somers 
323c8f30703SBrian Somers     if (bundle->ncp.mp.active) {
324ab2de065SBrian Somers       bundle_CalculateBandwidth(bundle);
32586b1f0d7SBrian Somers 
32686b1f0d7SBrian Somers       if (lost)
32786b1f0d7SBrian Somers         mp_LinkLost(&bundle->ncp.mp, lost);
32886b1f0d7SBrian Somers       else
329a33b2ef7SBrian Somers         log_Printf(LogALERT, "Oops, lost an unrecognised datalink (%s) !\n",
33086b1f0d7SBrian Somers                    fp->link->name);
3313b0f8d2eSBrian Somers     }
332c8f30703SBrian Somers 
333356bf92dSBrian Somers     if (!others_active) {
334c8f30703SBrian Somers       /* Down the NCPs.  We don't expect to get fsm_Close()d ourself ! */
33530949fd4SBrian Somers       ncp2initial(&bundle->ncp);
336356bf92dSBrian Somers       mp_Down(&bundle->ncp.mp);
337356bf92dSBrian Somers     }
3386d666775SBrian Somers   }
339ff0f9439SBrian Somers }
3406d666775SBrian Somers 
3416d666775SBrian Somers static void
3426d666775SBrian Somers bundle_LayerFinish(void *v, struct fsm *fp)
3436d666775SBrian Somers {
3446d666775SBrian Somers   /* The given fsm is now down (fp cannot be NULL)
3456d666775SBrian Somers    *
346dd7e2610SBrian Somers    * If it's the last NCP, fsm_Close all LCPs
347356bf92dSBrian Somers    * If it's the last NCP, bring any MP layer down
3486d666775SBrian Somers    */
3496d666775SBrian Somers 
3506d666775SBrian Somers   struct bundle *bundle = (struct bundle *)v;
3516d666775SBrian Somers   struct datalink *dl;
3526d666775SBrian Somers 
35330949fd4SBrian Somers   if (isncp(fp->proto) && !ncp_LayersUnfinished(&bundle->ncp)) {
35426afeaa2SBrian Somers     if (bundle_Phase(bundle) != PHASE_DEAD)
35525092092SBrian Somers       bundle_NewPhase(bundle, PHASE_TERMINATE);
3566d666775SBrian Somers     for (dl = bundle->links; dl; dl = dl->next)
357c8f30703SBrian Somers       if (dl->state == DATALINK_OPEN)
358d4d5d2f8SBrian Somers         datalink_Close(dl, CLOSE_STAYDOWN);
35909206a6fSBrian Somers     fsm2initial(fp);
360356bf92dSBrian Somers     mp_Down(&bundle->ncp.mp);
3613b0f8d2eSBrian Somers   }
3623b0f8d2eSBrian Somers }
3636d666775SBrian Somers 
3647a6f8720SBrian Somers void
3659c81b87dSBrian Somers bundle_Close(struct bundle *bundle, const char *name, int how)
3667a6f8720SBrian Somers {
367455aabc3SBrian Somers   /*
3683006ec67SBrian Somers    * Please close the given datalink.
369dd7e2610SBrian Somers    * If name == NULL or name is the last datalink, fsm_Close all NCPs
3706f384573SBrian Somers    * (except our MP)
3713b0f8d2eSBrian Somers    * If it isn't the last datalink, just Close that datalink.
372455aabc3SBrian Somers    */
3737a6f8720SBrian Somers 
3743b0f8d2eSBrian Somers   struct datalink *dl, *this_dl;
3753b0f8d2eSBrian Somers   int others_active;
3763006ec67SBrian Somers 
3773b0f8d2eSBrian Somers   others_active = 0;
3783b0f8d2eSBrian Somers   this_dl = NULL;
3793b0f8d2eSBrian Somers 
3803b0f8d2eSBrian Somers   for (dl = bundle->links; dl; dl = dl->next) {
3813b0f8d2eSBrian Somers     if (name && !strcasecmp(name, dl->name))
3823b0f8d2eSBrian Somers       this_dl = dl;
3833b0f8d2eSBrian Somers     if (name == NULL || this_dl == dl) {
3849c81b87dSBrian Somers       switch (how) {
3859c81b87dSBrian Somers         case CLOSE_LCP:
3869c81b87dSBrian Somers           datalink_DontHangup(dl);
3872fc2f705SBrian Somers           break;
3889c81b87dSBrian Somers         case CLOSE_STAYDOWN:
3893006ec67SBrian Somers           datalink_StayDown(dl);
3909c81b87dSBrian Somers           break;
3919c81b87dSBrian Somers       }
3923b0f8d2eSBrian Somers     } else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP)
3933b0f8d2eSBrian Somers       others_active++;
3943b0f8d2eSBrian Somers   }
3953b0f8d2eSBrian Somers 
3963b0f8d2eSBrian Somers   if (name && this_dl == NULL) {
397dd7e2610SBrian Somers     log_Printf(LogWARN, "%s: Invalid datalink name\n", name);
3983b0f8d2eSBrian Somers     return;
3993b0f8d2eSBrian Somers   }
4003b0f8d2eSBrian Somers 
4013b0f8d2eSBrian Somers   if (!others_active) {
40204eaa58cSBrian Somers     bundle_StopIdleTimer(bundle);
40330949fd4SBrian Somers     if (ncp_LayersUnfinished(&bundle->ncp))
40430949fd4SBrian Somers       ncp_Close(&bundle->ncp);
4053b0f8d2eSBrian Somers     else {
40630949fd4SBrian Somers       ncp2initial(&bundle->ncp);
407356bf92dSBrian Somers       mp_Down(&bundle->ncp.mp);
408d345321bSBrian Somers       for (dl = bundle->links; dl; dl = dl->next)
4099c81b87dSBrian Somers         datalink_Close(dl, how);
4107a6f8720SBrian Somers     }
4113b0f8d2eSBrian Somers   } else if (this_dl && this_dl->state != DATALINK_CLOSED &&
4123b0f8d2eSBrian Somers              this_dl->state != DATALINK_HANGUP)
4139c81b87dSBrian Somers     datalink_Close(this_dl, how);
414d2fd8d77SBrian Somers }
4157a6f8720SBrian Somers 
4161bc9b5baSBrian Somers void
417899011c4SBrian Somers bundle_Down(struct bundle *bundle, int how)
4181bc9b5baSBrian Somers {
4191bc9b5baSBrian Somers   struct datalink *dl;
4201bc9b5baSBrian Somers 
4211bc9b5baSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
422899011c4SBrian Somers     datalink_Down(dl, how);
4231bc9b5baSBrian Somers }
4241bc9b5baSBrian Somers 
4252f786681SBrian Somers static int
426f013f33eSBrian Somers bundle_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
4272f786681SBrian Somers {
4282f786681SBrian Somers   struct bundle *bundle = descriptor2bundle(d);
4292f786681SBrian Somers   struct datalink *dl;
43026af0ae9SBrian Somers   int result, nlinks;
4316c1d6731SBrian Somers   u_short ifqueue;
43226af0ae9SBrian Somers   size_t queued;
4332f786681SBrian Somers 
4342f786681SBrian Somers   result = 0;
4352f786681SBrian Somers 
436078c562eSBrian Somers   /* If there are aren't many packets queued, look for some more. */
43704eaa58cSBrian Somers   for (nlinks = 0, dl = bundle->links; dl; dl = dl->next)
43804eaa58cSBrian Somers     nlinks++;
43904eaa58cSBrian Somers 
44004eaa58cSBrian Somers   if (nlinks) {
44130949fd4SBrian Somers     queued = r ? ncp_FillPhysicalQueues(&bundle->ncp, bundle) :
44230949fd4SBrian Somers                  ncp_QueueLen(&bundle->ncp);
44304eaa58cSBrian Somers 
444ff0f9439SBrian Somers     if (r && (bundle->phase == PHASE_NETWORK ||
445ff0f9439SBrian Somers               bundle->phys_type.all & PHYS_AUTO)) {
44604eaa58cSBrian Somers       /* enough surplus so that we can tell if we're getting swamped */
4476c1d6731SBrian Somers       ifqueue = nlinks > bundle->cfg.ifqueue ? nlinks : bundle->cfg.ifqueue;
4486c1d6731SBrian Somers       if (queued < ifqueue) {
44904eaa58cSBrian Somers         /* Not enough - select() for more */
4506f8e9f0aSBrian Somers         if (bundle->choked.timer.state == TIMER_RUNNING)
4516f8e9f0aSBrian Somers           timer_Stop(&bundle->choked.timer);	/* Not needed any more */
45204eaa58cSBrian Somers         FD_SET(bundle->dev.fd, r);
453faefde08SBrian Somers         if (*n < bundle->dev.fd + 1)
454faefde08SBrian Somers           *n = bundle->dev.fd + 1;
4551384bd27SBrian Somers         log_Printf(LogTIMER, "%s: fdset(r) %d\n", TUN_NAME, bundle->dev.fd);
456078c562eSBrian Somers         result++;
4576f8e9f0aSBrian Somers       } else if (bundle->choked.timer.state == TIMER_STOPPED) {
4586f8e9f0aSBrian Somers         bundle->choked.timer.func = bundle_ClearQueues;
4596f8e9f0aSBrian Somers         bundle->choked.timer.name = "output choke";
4606f8e9f0aSBrian Somers         bundle->choked.timer.load = bundle->cfg.choked.timeout * SECTICKS;
4616f8e9f0aSBrian Somers         bundle->choked.timer.arg = bundle;
4626f8e9f0aSBrian Somers         timer_Start(&bundle->choked.timer);
463078c562eSBrian Somers       }
46404eaa58cSBrian Somers     }
46504eaa58cSBrian Somers   }
466078c562eSBrian Somers 
467f0cdd9c0SBrian Somers #ifndef NORADIUS
468f0cdd9c0SBrian Somers   result += descriptor_UpdateSet(&bundle->radius.desc, r, w, e, n);
469f0cdd9c0SBrian Somers #endif
470f0cdd9c0SBrian Somers 
47171555108SBrian Somers   /* Which links need a select() ? */
47271555108SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
47371555108SBrian Somers     result += descriptor_UpdateSet(&dl->desc, r, w, e, n);
47471555108SBrian Somers 
475ea722969SBrian Somers   /*
476ea722969SBrian Somers    * This *MUST* be called after the datalink UpdateSet()s as it
47704eaa58cSBrian Somers    * might be ``holding'' one of the datalinks (death-row) and
4788e7bd08eSBrian Somers    * wants to be able to de-select() it from the descriptor set.
479ea722969SBrian Somers    */
4800f2f3eb3SBrian Somers   result += descriptor_UpdateSet(&bundle->ncp.mp.server.desc, r, w, e, n);
481ea722969SBrian Somers 
4822f786681SBrian Somers   return result;
4832f786681SBrian Somers }
4842f786681SBrian Somers 
4852f786681SBrian Somers static int
486f013f33eSBrian Somers bundle_IsSet(struct fdescriptor *d, const fd_set *fdset)
4872f786681SBrian Somers {
4882f786681SBrian Somers   struct bundle *bundle = descriptor2bundle(d);
4892f786681SBrian Somers   struct datalink *dl;
4902f786681SBrian Somers 
4912f786681SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
4922f786681SBrian Somers     if (descriptor_IsSet(&dl->desc, fdset))
4932f786681SBrian Somers       return 1;
4942f786681SBrian Somers 
495f0cdd9c0SBrian Somers #ifndef NORADIUS
496f0cdd9c0SBrian Somers   if (descriptor_IsSet(&bundle->radius.desc, fdset))
497f0cdd9c0SBrian Somers     return 1;
498f0cdd9c0SBrian Somers #endif
499f0cdd9c0SBrian Somers 
500332b9de0SBrian Somers   if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset))
501332b9de0SBrian Somers     return 1;
502332b9de0SBrian Somers 
503faefde08SBrian Somers   return FD_ISSET(bundle->dev.fd, fdset);
5042f786681SBrian Somers }
5052f786681SBrian Somers 
5062f786681SBrian Somers static void
507f013f33eSBrian Somers bundle_DescriptorRead(struct fdescriptor *d, struct bundle *bundle,
5082f786681SBrian Somers                       const fd_set *fdset)
5092f786681SBrian Somers {
5102f786681SBrian Somers   struct datalink *dl;
5110a4b6c5cSBrian Somers   unsigned secs;
51230949fd4SBrian Somers   u_int32_t af;
5132f786681SBrian Somers 
514ea722969SBrian Somers   if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset))
515ea722969SBrian Somers     descriptor_Read(&bundle->ncp.mp.server.desc, bundle, fdset);
516ea722969SBrian Somers 
5172f786681SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
5182f786681SBrian Somers     if (descriptor_IsSet(&dl->desc, fdset))
5192f786681SBrian Somers       descriptor_Read(&dl->desc, bundle, fdset);
520b6217683SBrian Somers 
521f0cdd9c0SBrian Somers #ifndef NORADIUS
522f0cdd9c0SBrian Somers   if (descriptor_IsSet(&bundle->radius.desc, fdset))
523f0cdd9c0SBrian Somers     descriptor_Read(&bundle->radius.desc, bundle, fdset);
524f0cdd9c0SBrian Somers #endif
525f0cdd9c0SBrian Somers 
526faefde08SBrian Somers   if (FD_ISSET(bundle->dev.fd, fdset)) {
527078c562eSBrian Somers     struct tun_data tun;
528078c562eSBrian Somers     int n, pri;
5293a7b6d76SBrian Somers     char *data;
5303a7b6d76SBrian Somers     size_t sz;
5313a7b6d76SBrian Somers 
5323a7b6d76SBrian Somers     if (bundle->dev.header) {
5333a7b6d76SBrian Somers       data = (char *)&tun;
5343a7b6d76SBrian Somers       sz = sizeof tun;
5353a7b6d76SBrian Somers     } else {
5363a7b6d76SBrian Somers       data = tun.data;
5373a7b6d76SBrian Somers       sz = sizeof tun.data;
5383a7b6d76SBrian Somers     }
539078c562eSBrian Somers 
540078c562eSBrian Somers     /* something to read from tun */
5413a7b6d76SBrian Somers 
5423a7b6d76SBrian Somers     n = read(bundle->dev.fd, data, sz);
543078c562eSBrian Somers     if (n < 0) {
5443a7b6d76SBrian Somers       log_Printf(LogWARN, "%s: read: %s\n", bundle->dev.Name, strerror(errno));
545078c562eSBrian Somers       return;
546078c562eSBrian Somers     }
5473a7b6d76SBrian Somers 
5483a7b6d76SBrian Somers     if (bundle->dev.header) {
5493a7b6d76SBrian Somers       n -= sz - sizeof tun.data;
550078c562eSBrian Somers       if (n <= 0) {
5513a7b6d76SBrian Somers         log_Printf(LogERROR, "%s: read: Got only %d bytes of data !\n",
5523a7b6d76SBrian Somers                    bundle->dev.Name, n);
553078c562eSBrian Somers         return;
554078c562eSBrian Somers       }
55530949fd4SBrian Somers       af = ntohl(tun.header.family);
55630949fd4SBrian Somers #ifndef NOINET6
5571136c6acSBrian Somers       if (af != AF_INET && af != AF_INET6)
55830949fd4SBrian Somers #else
55930949fd4SBrian Somers       if (af != AF_INET)
56030949fd4SBrian Somers #endif
5613a7b6d76SBrian Somers         /* XXX: Should be maintaining drop/family counts ! */
562078c562eSBrian Somers         return;
56330949fd4SBrian Somers     } else
56430949fd4SBrian Somers       af = AF_INET;
565078c562eSBrian Somers 
56630949fd4SBrian Somers     if (af == AF_INET && ((struct ip *)tun.data)->ip_dst.s_addr ==
567078c562eSBrian Somers         bundle->ncp.ipcp.my_ip.s_addr) {
568078c562eSBrian Somers       /* we've been asked to send something addressed *to* us :( */
569078c562eSBrian Somers       if (Enabled(bundle, OPT_LOOPBACK)) {
57030949fd4SBrian Somers         pri = PacketCheck(bundle, af, tun.data, n, &bundle->filter.in,
57130949fd4SBrian Somers                           NULL, NULL);
572078c562eSBrian Somers         if (pri >= 0) {
5733a7b6d76SBrian Somers           n += sz - sizeof tun.data;
5743a7b6d76SBrian Somers           write(bundle->dev.fd, data, n);
575078c562eSBrian Somers           log_Printf(LogDEBUG, "Looped back packet addressed to myself\n");
576078c562eSBrian Somers         }
577078c562eSBrian Somers         return;
578078c562eSBrian Somers       } else
579078c562eSBrian Somers         log_Printf(LogDEBUG, "Oops - forwarding packet addressed to myself\n");
580078c562eSBrian Somers     }
581078c562eSBrian Somers 
582078c562eSBrian Somers     /*
58330949fd4SBrian Somers      * Process on-demand dialup. Output packets are queued within the tunnel
58430949fd4SBrian Somers      * device until the appropriate NCP is opened.
585078c562eSBrian Somers      */
586078c562eSBrian Somers 
587078c562eSBrian Somers     if (bundle_Phase(bundle) == PHASE_DEAD) {
588078c562eSBrian Somers       /*
589078c562eSBrian Somers        * Note, we must be in AUTO mode :-/ otherwise our interface should
590078c562eSBrian Somers        * *not* be UP and we can't receive data
591078c562eSBrian Somers        */
59230949fd4SBrian Somers       pri = PacketCheck(bundle, af, tun.data, n, &bundle->filter.dial,
59330949fd4SBrian Somers                         NULL, NULL);
594040cfe28SBrian Somers       if (pri >= 0)
595ba23f397SBrian Somers         bundle_Open(bundle, NULL, PHYS_AUTO, 0);
596078c562eSBrian Somers       else
597078c562eSBrian Somers         /*
598078c562eSBrian Somers          * Drop the packet.  If we were to queue it, we'd just end up with
599078c562eSBrian Somers          * a pile of timed-out data in our output queue by the time we get
600078c562eSBrian Somers          * around to actually dialing.  We'd also prematurely reach the
601078c562eSBrian Somers          * threshold at which we stop select()ing to read() the tun
602078c562eSBrian Somers          * device - breaking auto-dial.
603078c562eSBrian Somers          */
604078c562eSBrian Somers         return;
605078c562eSBrian Somers     }
606078c562eSBrian Somers 
6070a4b6c5cSBrian Somers     secs = 0;
60830949fd4SBrian Somers     pri = PacketCheck(bundle, af, tun.data, n, &bundle->filter.out,
60930949fd4SBrian Somers                       NULL, &secs);
6100a4b6c5cSBrian Somers     if (pri >= 0) {
6110a4b6c5cSBrian Somers       /* Prepend the number of seconds timeout given in the filter */
6120a4b6c5cSBrian Somers       tun.header.timeout = secs;
61330949fd4SBrian Somers       ncp_Enqueue(&bundle->ncp, af, pri, (char *)&tun, n + sizeof tun.header);
6140a4b6c5cSBrian Somers     }
615078c562eSBrian Somers   }
616078c562eSBrian Somers }
6172f786681SBrian Somers 
6181af29a6eSBrian Somers static int
619f013f33eSBrian Somers bundle_DescriptorWrite(struct fdescriptor *d, struct bundle *bundle,
6202f786681SBrian Somers                        const fd_set *fdset)
6212f786681SBrian Somers {
6222f786681SBrian Somers   struct datalink *dl;
6231af29a6eSBrian Somers   int result = 0;
6242f786681SBrian Somers 
625ea722969SBrian Somers   /* This is not actually necessary as struct mpserver doesn't Write() */
626ea722969SBrian Somers   if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset))
627fb11a9c2SBrian Somers     if (descriptor_Write(&bundle->ncp.mp.server.desc, bundle, fdset) == 1)
628fb11a9c2SBrian Somers       result++;
629ea722969SBrian Somers 
6302f786681SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
6312f786681SBrian Somers     if (descriptor_IsSet(&dl->desc, fdset))
632fb11a9c2SBrian Somers       switch (descriptor_Write(&dl->desc, bundle, fdset)) {
633fb11a9c2SBrian Somers       case -1:
634fb11a9c2SBrian Somers         datalink_ComeDown(dl, CLOSE_NORMAL);
635fb11a9c2SBrian Somers         break;
636fb11a9c2SBrian Somers       case 1:
637fb11a9c2SBrian Somers         result++;
638fb11a9c2SBrian Somers       }
6391af29a6eSBrian Somers 
6401af29a6eSBrian Somers   return result;
6412f786681SBrian Somers }
6422f786681SBrian Somers 
643da66dd13SBrian Somers void
6441384bd27SBrian Somers bundle_LockTun(struct bundle *bundle)
6451384bd27SBrian Somers {
6461384bd27SBrian Somers   FILE *lockfile;
64752847614SBrian Somers   char pidfile[PATH_MAX];
6481384bd27SBrian Somers 
6491384bd27SBrian Somers   snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit);
6501384bd27SBrian Somers   lockfile = ID0fopen(pidfile, "w");
6511384bd27SBrian Somers   if (lockfile != NULL) {
6521384bd27SBrian Somers     fprintf(lockfile, "%d\n", (int)getpid());
6531384bd27SBrian Somers     fclose(lockfile);
6541384bd27SBrian Somers   }
6551384bd27SBrian Somers #ifndef RELEASE_CRUNCH
6561384bd27SBrian Somers   else
6571384bd27SBrian Somers     log_Printf(LogERROR, "Warning: Can't create %s: %s\n",
6581384bd27SBrian Somers                pidfile, strerror(errno));
6591384bd27SBrian Somers #endif
6601384bd27SBrian Somers }
6611384bd27SBrian Somers 
6621384bd27SBrian Somers static void
6631384bd27SBrian Somers bundle_UnlockTun(struct bundle *bundle)
6641384bd27SBrian Somers {
66552847614SBrian Somers   char pidfile[PATH_MAX];
6661384bd27SBrian Somers 
6671384bd27SBrian Somers   snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit);
6681384bd27SBrian Somers   ID0unlink(pidfile);
6691384bd27SBrian Somers }
6707a6f8720SBrian Somers 
6717a6f8720SBrian Somers struct bundle *
672cf0a3940SBrian Somers bundle_Create(const char *prefix, int type, int unit)
6737a6f8720SBrian Somers {
6747a6f8720SBrian Somers   static struct bundle bundle;		/* there can be only one */
675c0593e34SBrian Somers   int enoentcount, err, minunit, maxunit;
6764e5196e9SBrian Somers   const char *ifname;
677fdb4bb1bSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
678fc3034caSBrian Somers   int kldtried;
679fc3034caSBrian Somers #endif
6803a7b6d76SBrian Somers #if defined(TUNSIFMODE) || defined(TUNSLMODE) || defined(TUNSIFHEAD)
6810f203c7eSBrian Somers   int iff;
6820f203c7eSBrian Somers #endif
6837a6f8720SBrian Somers 
6848fa6ebe4SBrian Somers   if (bundle.iface != NULL) {	/* Already allocated ! */
685a33b2ef7SBrian Somers     log_Printf(LogALERT, "bundle_Create:  There's only one BUNDLE !\n");
6867a6f8720SBrian Somers     return NULL;
6877a6f8720SBrian Somers   }
6887a6f8720SBrian Somers 
689c0593e34SBrian Somers   if (unit == -1) {
690c0593e34SBrian Somers     minunit = 0;
691c0593e34SBrian Somers     maxunit = -1;
692c0593e34SBrian Somers   } else {
693c0593e34SBrian Somers     minunit = unit;
694c0593e34SBrian Somers     maxunit = unit + 1;
695c0593e34SBrian Somers   }
6967a6f8720SBrian Somers   err = ENOENT;
6977a6f8720SBrian Somers   enoentcount = 0;
698fdb4bb1bSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
699fc3034caSBrian Somers   kldtried = 0;
700fc3034caSBrian Somers #endif
701c0593e34SBrian Somers   for (bundle.unit = minunit; bundle.unit != maxunit; bundle.unit++) {
702faefde08SBrian Somers     snprintf(bundle.dev.Name, sizeof bundle.dev.Name, "%s%d",
703faefde08SBrian Somers              prefix, bundle.unit);
704faefde08SBrian Somers     bundle.dev.fd = ID0open(bundle.dev.Name, O_RDWR);
705faefde08SBrian Somers     if (bundle.dev.fd >= 0)
7067a6f8720SBrian Somers       break;
707728ef5b2SBrian Somers     else if (errno == ENXIO || errno == ENOENT) {
708fdb4bb1bSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
709c0593e34SBrian Somers       if (bundle.unit == minunit && !kldtried++) {
710fc3034caSBrian Somers         /*
711fdb4bb1bSBrian Somers 	 * Attempt to load the tunnel interface KLD if it isn't loaded
712fdb4bb1bSBrian Somers 	 * already.
713fc3034caSBrian Somers          */
714fb11a9c2SBrian Somers         loadmodules(LOAD_VERBOSLY, "if_tun", NULL);
715fc3034caSBrian Somers         continue;
716fc3034caSBrian Somers       }
717fc3034caSBrian Somers #endif
718c4c6616aSBrian Somers       if (errno != ENOENT || ++enoentcount > 2) {
7197a6f8720SBrian Somers         err = errno;
720107d62e7SBrian Somers 	break;
721c4c6616aSBrian Somers       }
7227a6f8720SBrian Somers     } else
7237a6f8720SBrian Somers       err = errno;
7247a6f8720SBrian Somers   }
7257a6f8720SBrian Somers 
726faefde08SBrian Somers   if (bundle.dev.fd < 0) {
727c0593e34SBrian Somers     if (unit == -1)
728c0593e34SBrian Somers       log_Printf(LogWARN, "No available tunnel devices found (%s)\n",
72985b542cfSBrian Somers                 strerror(err));
730c0593e34SBrian Somers     else
731c0593e34SBrian Somers       log_Printf(LogWARN, "%s%d: %s\n", prefix, unit, strerror(err));
7327a6f8720SBrian Somers     return NULL;
7337a6f8720SBrian Somers   }
7347a6f8720SBrian Somers 
735dd7e2610SBrian Somers   log_SetTun(bundle.unit);
7367a6f8720SBrian Somers 
7378fa6ebe4SBrian Somers   ifname = strrchr(bundle.dev.Name, '/');
7388fa6ebe4SBrian Somers   if (ifname == NULL)
7398fa6ebe4SBrian Somers     ifname = bundle.dev.Name;
7407a6f8720SBrian Somers   else
7418fa6ebe4SBrian Somers     ifname++;
7428fa6ebe4SBrian Somers 
7438fa6ebe4SBrian Somers   bundle.iface = iface_Create(ifname);
7448fa6ebe4SBrian Somers   if (bundle.iface == NULL) {
7458fa6ebe4SBrian Somers     close(bundle.dev.fd);
7468fa6ebe4SBrian Somers     return NULL;
7478fa6ebe4SBrian Somers   }
7487a6f8720SBrian Somers 
7490f203c7eSBrian Somers #ifdef TUNSIFMODE
750ebdcbc67SBrian Somers   /* Make sure we're POINTOPOINT & IFF_MULTICAST */
751ebdcbc67SBrian Somers   iff = IFF_POINTOPOINT | IFF_MULTICAST;
7520f203c7eSBrian Somers   if (ID0ioctl(bundle.dev.fd, TUNSIFMODE, &iff) < 0)
7530f203c7eSBrian Somers     log_Printf(LogERROR, "bundle_Create: ioctl(TUNSIFMODE): %s\n",
7540f203c7eSBrian Somers 	       strerror(errno));
7550f203c7eSBrian Somers #endif
7560f203c7eSBrian Somers 
7576ca65df0SBrian Somers #ifdef TUNSLMODE
7583a7b6d76SBrian Somers   /* Make sure we're not prepending sockaddrs */
7596ca65df0SBrian Somers   iff = 0;
7606ca65df0SBrian Somers   if (ID0ioctl(bundle.dev.fd, TUNSLMODE, &iff) < 0)
7616ca65df0SBrian Somers     log_Printf(LogERROR, "bundle_Create: ioctl(TUNSLMODE): %s\n",
7626ca65df0SBrian Somers 	       strerror(errno));
7636ca65df0SBrian Somers #endif
7646ca65df0SBrian Somers 
7653a7b6d76SBrian Somers #ifdef TUNSIFHEAD
7663a7b6d76SBrian Somers   /* We want the address family please ! */
7673a7b6d76SBrian Somers   iff = 1;
7683a7b6d76SBrian Somers   if (ID0ioctl(bundle.dev.fd, TUNSIFHEAD, &iff) < 0) {
7693a7b6d76SBrian Somers     log_Printf(LogERROR, "bundle_Create: ioctl(TUNSIFHEAD): %s\n",
7703a7b6d76SBrian Somers 	       strerror(errno));
7713a7b6d76SBrian Somers     bundle.dev.header = 0;
7723a7b6d76SBrian Somers   } else
7733a7b6d76SBrian Somers     bundle.dev.header = 1;
7743a7b6d76SBrian Somers #else
7753a7b6d76SBrian Somers #ifdef __OpenBSD__
7763a7b6d76SBrian Somers   /* Always present for OpenBSD */
7773a7b6d76SBrian Somers   bundle.dev.header = 1;
7783a7b6d76SBrian Somers #else
7793a7b6d76SBrian Somers   /*
7803a7b6d76SBrian Somers    * If TUNSIFHEAD isn't available and we're not OpenBSD, assume
7813a7b6d76SBrian Somers    * everything's AF_INET (hopefully the tun device won't pass us
7823a7b6d76SBrian Somers    * anything else !).
7833a7b6d76SBrian Somers    */
7843a7b6d76SBrian Somers   bundle.dev.header = 0;
7853a7b6d76SBrian Somers #endif
7863a7b6d76SBrian Somers #endif
7873a7b6d76SBrian Somers 
7888fa6ebe4SBrian Somers   log_Printf(LogPHASE, "Using interface: %s\n", ifname);
7897a6f8720SBrian Somers 
790ab2de065SBrian Somers   bundle.bandwidth = 0;
791820de6ebSBrian Somers   bundle.routing_seq = 0;
792a0cbd833SBrian Somers   bundle.phase = PHASE_DEAD;
793a0cbd833SBrian Somers   bundle.CleaningUp = 0;
79467b072f7SBrian Somers   bundle.NatEnabled = 0;
7957a6f8720SBrian Somers 
7966d666775SBrian Somers   bundle.fsm.LayerStart = bundle_LayerStart;
7976f384573SBrian Somers   bundle.fsm.LayerUp = bundle_LayerUp;
7986d666775SBrian Somers   bundle.fsm.LayerDown = bundle_LayerDown;
7996d666775SBrian Somers   bundle.fsm.LayerFinish = bundle_LayerFinish;
8006d666775SBrian Somers   bundle.fsm.object = &bundle;
8017a6f8720SBrian Somers 
802dade2407SBrian Somers   bundle.cfg.idle.timeout = NCP_IDLE_TIMEOUT;
803dade2407SBrian Somers   bundle.cfg.idle.min_timeout = 0;
8041342caedSBrian Somers   *bundle.cfg.auth.name = '\0';
8051342caedSBrian Somers   *bundle.cfg.auth.key = '\0';
80630949fd4SBrian Somers   bundle.cfg.opt = OPT_IDCHECK | OPT_LOOPBACK | OPT_SROUTES | OPT_TCPMSSFIXUP |
807610b185fSBrian Somers                    OPT_THROUGHPUT | OPT_UTMP;
808971abb29SBrian Somers #ifndef NOINET6
809971abb29SBrian Somers   bundle.cfg.opt |= OPT_IPCP;
810971abb29SBrian Somers   if (probe.ipv6_available)
811971abb29SBrian Somers     bundle.cfg.opt |= OPT_IPV6CP;
812971abb29SBrian Somers #endif
81349052c95SBrian Somers   *bundle.cfg.label = '\0';
8146c1d6731SBrian Somers   bundle.cfg.ifqueue = DEF_IFQUEUE;
8156f8e9f0aSBrian Somers   bundle.cfg.choked.timeout = CHOKED_TIMEOUT;
816ff0f9439SBrian Somers   bundle.phys_type.all = type;
817ff0f9439SBrian Somers   bundle.phys_type.open = 0;
818dade2407SBrian Somers   bundle.upat = 0;
819ab886ad0SBrian Somers 
8206f384573SBrian Somers   bundle.links = datalink_Create("deflink", &bundle, type);
8213006ec67SBrian Somers   if (bundle.links == NULL) {
822a33b2ef7SBrian Somers     log_Printf(LogALERT, "Cannot create data link: %s\n", strerror(errno));
8238fa6ebe4SBrian Somers     iface_Destroy(bundle.iface);
8248fa6ebe4SBrian Somers     bundle.iface = NULL;
825faefde08SBrian Somers     close(bundle.dev.fd);
8262289f246SBrian Somers     return NULL;
8272289f246SBrian Somers   }
8282289f246SBrian Somers 
8292f786681SBrian Somers   bundle.desc.type = BUNDLE_DESCRIPTOR;
8302f786681SBrian Somers   bundle.desc.UpdateSet = bundle_UpdateSet;
8312f786681SBrian Somers   bundle.desc.IsSet = bundle_IsSet;
8322f786681SBrian Somers   bundle.desc.Read = bundle_DescriptorRead;
8332f786681SBrian Somers   bundle.desc.Write = bundle_DescriptorWrite;
8342f786681SBrian Somers 
83530949fd4SBrian Somers   ncp_Init(&bundle.ncp, &bundle);
8366d666775SBrian Somers 
8375ca5389aSBrian Somers   memset(&bundle.filter, '\0', sizeof bundle.filter);
8385ca5389aSBrian Somers   bundle.filter.in.fragok = bundle.filter.in.logok = 1;
8395ca5389aSBrian Somers   bundle.filter.in.name = "IN";
8405ca5389aSBrian Somers   bundle.filter.out.fragok = bundle.filter.out.logok = 1;
8415ca5389aSBrian Somers   bundle.filter.out.name = "OUT";
8425ca5389aSBrian Somers   bundle.filter.dial.name = "DIAL";
8438390b576SBrian Somers   bundle.filter.dial.logok = 1;
8445ca5389aSBrian Somers   bundle.filter.alive.name = "ALIVE";
8455ca5389aSBrian Somers   bundle.filter.alive.logok = 1;
846cad7e742SBrian Somers   {
847cad7e742SBrian Somers     int	i;
848cad7e742SBrian Somers     for (i = 0; i < MAXFILTERS; i++) {
849cad7e742SBrian Somers         bundle.filter.in.rule[i].f_action = A_NONE;
850cad7e742SBrian Somers         bundle.filter.out.rule[i].f_action = A_NONE;
851cad7e742SBrian Somers         bundle.filter.dial.rule[i].f_action = A_NONE;
852cad7e742SBrian Somers         bundle.filter.alive.rule[i].f_action = A_NONE;
853cad7e742SBrian Somers     }
854cad7e742SBrian Somers   }
85593ee0ff2SBrian Somers   memset(&bundle.idle.timer, '\0', sizeof bundle.idle.timer);
85693ee0ff2SBrian Somers   bundle.idle.done = 0;
8575cf4388bSBrian Somers   bundle.notify.fd = -1;
8586f8e9f0aSBrian Somers   memset(&bundle.choked.timer, '\0', sizeof bundle.choked.timer);
859972a1bcfSBrian Somers #ifndef NORADIUS
860972a1bcfSBrian Somers   radius_Init(&bundle.radius);
861972a1bcfSBrian Somers #endif
86293ee0ff2SBrian Somers 
8636d666775SBrian Somers   /* Clean out any leftover crud */
86430949fd4SBrian Somers   iface_Clear(bundle.iface, &bundle.ncp, 0, IFACE_CLEAR_ALL);
8656d666775SBrian Somers 
8661384bd27SBrian Somers   bundle_LockTun(&bundle);
8671384bd27SBrian Somers 
8687a6f8720SBrian Somers   return &bundle;
8697a6f8720SBrian Somers }
8707a6f8720SBrian Somers 
87168a0f0ccSBrian Somers static void
87268a0f0ccSBrian Somers bundle_DownInterface(struct bundle *bundle)
87368a0f0ccSBrian Somers {
874dd7e2610SBrian Somers   route_IfDelete(bundle, 1);
875dc744e19SBrian Somers   iface_ClearFlags(bundle->iface->name, IFF_UP);
87668a0f0ccSBrian Somers }
87768a0f0ccSBrian Somers 
87868a0f0ccSBrian Somers void
87968a0f0ccSBrian Somers bundle_Destroy(struct bundle *bundle)
88068a0f0ccSBrian Somers {
8813006ec67SBrian Somers   struct datalink *dl;
882b6217683SBrian Somers 
883ea722969SBrian Somers   /*
88430949fd4SBrian Somers    * Clean up the interface.  We don't really need to do the timer_Stop()s,
88530949fd4SBrian Somers    * mp_Down(), iface_Clear() and bundle_DownInterface() unless we're getting
8868e7bd08eSBrian Somers    * out under exceptional conditions such as a descriptor exception.
887ea722969SBrian Somers    */
88804eaa58cSBrian Somers   timer_Stop(&bundle->idle.timer);
8896f8e9f0aSBrian Somers   timer_Stop(&bundle->choked.timer);
89066f634b6SBrian Somers   mp_Down(&bundle->ncp.mp);
89130949fd4SBrian Somers   iface_Clear(bundle->iface, &bundle->ncp, 0, IFACE_CLEAR_ALL);
89268a0f0ccSBrian Somers   bundle_DownInterface(bundle);
8933006ec67SBrian Somers 
894972a1bcfSBrian Somers #ifndef NORADIUS
895972a1bcfSBrian Somers   /* Tell the radius server the bad news */
896972a1bcfSBrian Somers   radius_Destroy(&bundle->radius);
897972a1bcfSBrian Somers #endif
898972a1bcfSBrian Somers 
899ea722969SBrian Somers   /* Again, these are all DATALINK_CLOSED unless we're abending */
9003006ec67SBrian Somers   dl = bundle->links;
9013006ec67SBrian Somers   while (dl)
9023006ec67SBrian Somers     dl = datalink_Destroy(dl);
9033006ec67SBrian Somers 
90430949fd4SBrian Somers   ncp_Destroy(&bundle->ncp);
905442f8495SBrian Somers 
9061384bd27SBrian Somers   close(bundle->dev.fd);
9071384bd27SBrian Somers   bundle_UnlockTun(bundle);
9081384bd27SBrian Somers 
909ea722969SBrian Somers   /* In case we never made PHASE_NETWORK */
9105cf4388bSBrian Somers   bundle_Notify(bundle, EX_ERRDEAD);
911b6217683SBrian Somers 
9128fa6ebe4SBrian Somers   iface_Destroy(bundle->iface);
9138fa6ebe4SBrian Somers   bundle->iface = NULL;
91468a0f0ccSBrian Somers }
91568a0f0ccSBrian Somers 
91683d1af55SBrian Somers void
9173006ec67SBrian Somers bundle_LinkClosed(struct bundle *bundle, struct datalink *dl)
9183006ec67SBrian Somers {
9193006ec67SBrian Somers   /*
9203006ec67SBrian Somers    * Our datalink has closed.
921ea722969SBrian Somers    * CleanDatalinks() (called from DoLoop()) will remove closed
922f6a4e748SBrian Somers    * BACKGROUND, FOREGROUND and DIRECT links.
9233b0f8d2eSBrian Somers    * If it's the last data link, enter phase DEAD.
924ea722969SBrian Somers    *
925ea722969SBrian Somers    * NOTE: dl may not be in our list (bundle_SendDatalink()) !
9263006ec67SBrian Somers    */
9275b8b8060SBrian Somers 
9283b0f8d2eSBrian Somers   struct datalink *odl;
9293b0f8d2eSBrian Somers   int other_links;
9305b8b8060SBrian Somers 
931bf1d3ff6SBrian Somers   log_SetTtyCommandMode(dl);
932bf1d3ff6SBrian Somers 
9333b0f8d2eSBrian Somers   other_links = 0;
9343b0f8d2eSBrian Somers   for (odl = bundle->links; odl; odl = odl->next)
9353b0f8d2eSBrian Somers     if (odl != dl && odl->state != DATALINK_CLOSED)
9363b0f8d2eSBrian Somers       other_links++;
9373b0f8d2eSBrian Somers 
9383b0f8d2eSBrian Somers   if (!other_links) {
93981358fa3SBrian Somers     if (dl->physical->type != PHYS_AUTO)	/* Not in -auto mode */
94003704096SBrian Somers       bundle_DownInterface(bundle);
94130949fd4SBrian Somers     ncp2initial(&bundle->ncp);
942356bf92dSBrian Somers     mp_Down(&bundle->ncp.mp);
9435563ebdeSBrian Somers     bundle_NewPhase(bundle, PHASE_DEAD);
9440f2f3eb3SBrian Somers     bundle_StopIdleTimer(bundle);
945ab2de065SBrian Somers   }
946455aabc3SBrian Somers }
947455aabc3SBrian Somers 
948455aabc3SBrian Somers void
949ba23f397SBrian Somers bundle_Open(struct bundle *bundle, const char *name, int mask, int force)
9503006ec67SBrian Somers {
9513006ec67SBrian Somers   /*
9523006ec67SBrian Somers    * Please open the given datalink, or all if name == NULL
9533006ec67SBrian Somers    */
9543006ec67SBrian Somers   struct datalink *dl;
9553006ec67SBrian Somers 
9563006ec67SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
9573006ec67SBrian Somers     if (name == NULL || !strcasecmp(dl->name, name)) {
958ba23f397SBrian Somers       if ((mask & dl->physical->type) &&
959ba23f397SBrian Somers           (dl->state == DATALINK_CLOSED ||
960ba23f397SBrian Somers            (force && dl->state == DATALINK_OPENING &&
9615e269efeSBrian Somers             dl->dial.timer.state == TIMER_RUNNING) ||
9625e269efeSBrian Somers            dl->state == DATALINK_READY)) {
9635e269efeSBrian Somers         timer_Stop(&dl->dial.timer);	/* We're finished with this */
964565e35e5SBrian Somers         datalink_Up(dl, 1, 1);
965ab2de065SBrian Somers         if (mask & PHYS_AUTO)
9665e269efeSBrian Somers           break;			/* Only one AUTO link at a time */
96704eaa58cSBrian Somers       }
9683006ec67SBrian Somers       if (name != NULL)
9693006ec67SBrian Somers         break;
9703006ec67SBrian Somers     }
9713006ec67SBrian Somers }
9723006ec67SBrian Somers 
9733006ec67SBrian Somers struct datalink *
9743006ec67SBrian Somers bundle2datalink(struct bundle *bundle, const char *name)
9753006ec67SBrian Somers {
9763006ec67SBrian Somers   struct datalink *dl;
9773006ec67SBrian Somers 
9783006ec67SBrian Somers   if (name != NULL) {
9793006ec67SBrian Somers     for (dl = bundle->links; dl; dl = dl->next)
9803006ec67SBrian Somers       if (!strcasecmp(dl->name, name))
9813006ec67SBrian Somers         return dl;
9823006ec67SBrian Somers   } else if (bundle->links && !bundle->links->next)
9833006ec67SBrian Somers     return bundle->links;
9843006ec67SBrian Somers 
9853006ec67SBrian Somers   return NULL;
9863006ec67SBrian Somers }
9873006ec67SBrian Somers 
9883006ec67SBrian Somers int
989aef795ccSBrian Somers bundle_ShowLinks(struct cmdargs const *arg)
990aef795ccSBrian Somers {
991aef795ccSBrian Somers   struct datalink *dl;
992ab2de065SBrian Somers   struct pppThroughput *t;
99391cbd2eeSBrian Somers   unsigned long long octets;
994ab2de065SBrian Somers   int secs;
995aef795ccSBrian Somers 
9969c53a7b1SBrian Somers   for (dl = arg->bundle->links; dl; dl = dl->next) {
99791cbd2eeSBrian Somers     octets = MAX(dl->physical->link.stats.total.in.OctetsPerSecond,
99891cbd2eeSBrian Somers                  dl->physical->link.stats.total.out.OctetsPerSecond);
99991cbd2eeSBrian Somers 
1000d4156d00SBrian Somers     prompt_Printf(arg->prompt, "Name: %s [%s, %s]",
1001d4156d00SBrian Somers                   dl->name, mode2Nam(dl->physical->type), datalink_State(dl));
100211572abfSBrian Somers     if (dl->physical->link.stats.total.rolling && dl->state == DATALINK_OPEN)
1003e531f89aSBrian Somers       prompt_Printf(arg->prompt, " bandwidth %d, %llu bps (%llu bytes/sec)",
1004ab2de065SBrian Somers                     dl->mp.bandwidth ? dl->mp.bandwidth :
1005ab2de065SBrian Somers                                        physical_GetSpeed(dl->physical),
100691cbd2eeSBrian Somers                     octets * 8, octets);
10079c53a7b1SBrian Somers     prompt_Printf(arg->prompt, "\n");
10089c53a7b1SBrian Somers   }
1009aef795ccSBrian Somers 
101011572abfSBrian Somers   t = &arg->bundle->ncp.mp.link.stats.total;
101191cbd2eeSBrian Somers   octets = MAX(t->in.OctetsPerSecond, t->out.OctetsPerSecond);
1012ab2de065SBrian Somers   secs = t->downtime ? 0 : throughput_uptime(t);
1013ab2de065SBrian Somers   if (secs > t->SamplePeriod)
1014ab2de065SBrian Somers     secs = t->SamplePeriod;
1015ab2de065SBrian Somers   if (secs)
1016e531f89aSBrian Somers     prompt_Printf(arg->prompt, "Currently averaging %llu bps (%llu bytes/sec)"
101791cbd2eeSBrian Somers                   " over the last %d secs\n", octets * 8, octets, secs);
1018ab2de065SBrian Somers 
1019aef795ccSBrian Somers   return 0;
1020aef795ccSBrian Somers }
1021ab886ad0SBrian Somers 
10221342caedSBrian Somers static const char *
10231342caedSBrian Somers optval(struct bundle *bundle, int bit)
10241342caedSBrian Somers {
10251342caedSBrian Somers   return (bundle->cfg.opt & bit) ? "enabled" : "disabled";
10261342caedSBrian Somers }
10271342caedSBrian Somers 
1028c08717dfSBrian Somers int
1029c08717dfSBrian Somers bundle_ShowStatus(struct cmdargs const *arg)
1030c08717dfSBrian Somers {
1031c08717dfSBrian Somers   int remaining;
1032c08717dfSBrian Somers 
1033c08717dfSBrian Somers   prompt_Printf(arg->prompt, "Phase %s\n", bundle_PhaseName(arg->bundle));
1034faefde08SBrian Somers   prompt_Printf(arg->prompt, " Device:        %s\n", arg->bundle->dev.Name);
1035dade2407SBrian Somers   prompt_Printf(arg->prompt, " Interface:     %s @ %lubps",
1036ab2de065SBrian Somers                 arg->bundle->iface->name, arg->bundle->bandwidth);
1037c08717dfSBrian Somers 
1038dade2407SBrian Somers   if (arg->bundle->upat) {
103946df5aa7SBrian Somers     int secs = bundle_Uptime(arg->bundle);
1040dade2407SBrian Somers 
1041dade2407SBrian Somers     prompt_Printf(arg->prompt, ", up time %d:%02d:%02d", secs / 3600,
1042dade2407SBrian Somers                   (secs / 60) % 60, secs % 60);
1043dade2407SBrian Somers   }
1044669b9965SBrian Somers   prompt_Printf(arg->prompt, "\n Queued:        %lu of %u\n",
104530949fd4SBrian Somers                 (unsigned long)ncp_QueueLen(&arg->bundle->ncp),
104677fc031dSBrian Somers                 arg->bundle->cfg.ifqueue);
1047dade2407SBrian Somers 
10486c1d6731SBrian Somers   prompt_Printf(arg->prompt, "\nDefaults:\n");
104974457d3dSBrian Somers   prompt_Printf(arg->prompt, " Label:             %s\n",
105074457d3dSBrian Somers                 arg->bundle->cfg.label);
1051610b185fSBrian Somers   prompt_Printf(arg->prompt, " Auth name:         %s\n",
1052610b185fSBrian Somers                 arg->bundle->cfg.auth.name);
105374457d3dSBrian Somers   prompt_Printf(arg->prompt, " Diagnostic socket: ");
105437b8a5c7SBrian Somers   if (*server.cfg.sockname != '\0') {
105537b8a5c7SBrian Somers     prompt_Printf(arg->prompt, "%s", server.cfg.sockname);
105637b8a5c7SBrian Somers     if (server.cfg.mask != (mode_t)-1)
105737b8a5c7SBrian Somers       prompt_Printf(arg->prompt, ", mask 0%03o", (int)server.cfg.mask);
105837b8a5c7SBrian Somers     prompt_Printf(arg->prompt, "%s\n", server.fd == -1 ? " (not open)" : "");
105937b8a5c7SBrian Somers   } else if (server.cfg.port != 0)
106074457d3dSBrian Somers     prompt_Printf(arg->prompt, "TCP port %d%s\n", server.cfg.port,
106174457d3dSBrian Somers                   server.fd == -1 ? " (not open)" : "");
106274457d3dSBrian Somers   else
106374457d3dSBrian Somers     prompt_Printf(arg->prompt, "none\n");
106404eaa58cSBrian Somers 
10656f8e9f0aSBrian Somers   prompt_Printf(arg->prompt, " Choked Timer:      %ds\n",
10666f8e9f0aSBrian Somers                 arg->bundle->cfg.choked.timeout);
1067972a1bcfSBrian Somers 
1068972a1bcfSBrian Somers #ifndef NORADIUS
1069972a1bcfSBrian Somers   radius_Show(&arg->bundle->radius, arg->prompt);
1070972a1bcfSBrian Somers #endif
1071972a1bcfSBrian Somers 
1072c08717dfSBrian Somers   prompt_Printf(arg->prompt, " Idle Timer:        ");
1073dade2407SBrian Somers   if (arg->bundle->cfg.idle.timeout) {
1074dade2407SBrian Somers     prompt_Printf(arg->prompt, "%ds", arg->bundle->cfg.idle.timeout);
1075dade2407SBrian Somers     if (arg->bundle->cfg.idle.min_timeout)
1076dade2407SBrian Somers       prompt_Printf(arg->prompt, ", min %ds",
1077dade2407SBrian Somers                     arg->bundle->cfg.idle.min_timeout);
1078c08717dfSBrian Somers     remaining = bundle_RemainingIdleTime(arg->bundle);
1079c08717dfSBrian Somers     if (remaining != -1)
1080c08717dfSBrian Somers       prompt_Printf(arg->prompt, " (%ds remaining)", remaining);
1081c08717dfSBrian Somers     prompt_Printf(arg->prompt, "\n");
1082c08717dfSBrian Somers   } else
1083c08717dfSBrian Somers     prompt_Printf(arg->prompt, "disabled\n");
1084c08717dfSBrian Somers 
108530949fd4SBrian Somers   prompt_Printf(arg->prompt, " Filter Decap:      %-20.20s",
108698251667SBrian Somers                 optval(arg->bundle, OPT_FILTERDECAP));
108730949fd4SBrian Somers   prompt_Printf(arg->prompt, " ID check:          %s\n",
10881342caedSBrian Somers                 optval(arg->bundle, OPT_IDCHECK));
108930949fd4SBrian Somers   prompt_Printf(arg->prompt, " Iface-Alias:       %-20.20s",
109030949fd4SBrian Somers                 optval(arg->bundle, OPT_IFACEALIAS));
109130949fd4SBrian Somers #ifndef NOINET6
109230949fd4SBrian Somers   prompt_Printf(arg->prompt, " IPCP:              %s\n",
109330949fd4SBrian Somers                 optval(arg->bundle, OPT_IPCP));
109430949fd4SBrian Somers   prompt_Printf(arg->prompt, " IPV6CP:            %-20.20s",
109530949fd4SBrian Somers                 optval(arg->bundle, OPT_IPV6CP));
109630949fd4SBrian Somers #endif
109798251667SBrian Somers   prompt_Printf(arg->prompt, " Keep-Session:      %s\n",
1098ac685e31SBrian Somers                 optval(arg->bundle, OPT_KEEPSESSION));
109998251667SBrian Somers   prompt_Printf(arg->prompt, " Loopback:          %-20.20s",
11001342caedSBrian Somers                 optval(arg->bundle, OPT_LOOPBACK));
110198251667SBrian Somers   prompt_Printf(arg->prompt, " PasswdAuth:        %s\n",
11021342caedSBrian Somers                 optval(arg->bundle, OPT_PASSWDAUTH));
110398251667SBrian Somers   prompt_Printf(arg->prompt, " Proxy:             %-20.20s",
11041342caedSBrian Somers                 optval(arg->bundle, OPT_PROXY));
110598251667SBrian Somers   prompt_Printf(arg->prompt, " Proxyall:          %s\n",
11063afe5ccbSBrian Somers                 optval(arg->bundle, OPT_PROXYALL));
110730949fd4SBrian Somers   prompt_Printf(arg->prompt, " Sticky Routes:     %-20.20s",
110830949fd4SBrian Somers                 optval(arg->bundle, OPT_SROUTES));
110930949fd4SBrian Somers   prompt_Printf(arg->prompt, " TCPMSS Fixup:      %s\n",
111094d7be52SBrian Somers                 optval(arg->bundle, OPT_TCPMSSFIXUP));
111130949fd4SBrian Somers   prompt_Printf(arg->prompt, " Throughput:        %-20.20s",
11121342caedSBrian Somers                 optval(arg->bundle, OPT_THROUGHPUT));
111330949fd4SBrian Somers   prompt_Printf(arg->prompt, " Utmp Logging:      %s\n",
11141342caedSBrian Somers                 optval(arg->bundle, OPT_UTMP));
11151342caedSBrian Somers 
1116c08717dfSBrian Somers   return 0;
1117c08717dfSBrian Somers }
1118c08717dfSBrian Somers 
1119ab886ad0SBrian Somers static void
1120ab886ad0SBrian Somers bundle_IdleTimeout(void *v)
1121ab886ad0SBrian Somers {
1122ab886ad0SBrian Somers   struct bundle *bundle = (struct bundle *)v;
1123ab886ad0SBrian Somers 
1124b42135deSBrian Somers   log_Printf(LogPHASE, "Idle timer expired\n");
112504eaa58cSBrian Somers   bundle_StopIdleTimer(bundle);
11269c81b87dSBrian Somers   bundle_Close(bundle, NULL, CLOSE_STAYDOWN);
1127ab886ad0SBrian Somers }
1128ab886ad0SBrian Somers 
1129ab886ad0SBrian Somers /*
1130ab886ad0SBrian Somers  *  Start Idle timer. If timeout is reached, we call bundle_Close() to
1131ab886ad0SBrian Somers  *  close LCP and link.
1132ab886ad0SBrian Somers  */
1133ab886ad0SBrian Somers void
11340a4b6c5cSBrian Somers bundle_StartIdleTimer(struct bundle *bundle, unsigned secs)
1135ab886ad0SBrian Somers {
1136dd7e2610SBrian Somers   timer_Stop(&bundle->idle.timer);
1137ff0f9439SBrian Somers   if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) !=
1138dade2407SBrian Somers       bundle->phys_type.open && bundle->cfg.idle.timeout) {
11390a4b6c5cSBrian Somers     time_t now = time(NULL);
1140dade2407SBrian Somers 
11410a4b6c5cSBrian Somers     if (secs == 0)
1142dade2407SBrian Somers       secs = bundle->cfg.idle.timeout;
11430a4b6c5cSBrian Somers 
11440a4b6c5cSBrian Somers     /* We want at least `secs' */
1145dade2407SBrian Somers     if (bundle->cfg.idle.min_timeout > secs && bundle->upat) {
11460a4b6c5cSBrian Somers       int up = now - bundle->upat;
1147dade2407SBrian Somers 
1148dade2407SBrian Somers       if ((long long)bundle->cfg.idle.min_timeout - up > (long long)secs)
11490a4b6c5cSBrian Somers         /* Only increase from the current `remaining' value */
1150dade2407SBrian Somers         secs = bundle->cfg.idle.min_timeout - up;
1151dade2407SBrian Somers     }
115293ee0ff2SBrian Somers     bundle->idle.timer.func = bundle_IdleTimeout;
11533b0f8d2eSBrian Somers     bundle->idle.timer.name = "idle";
1154dade2407SBrian Somers     bundle->idle.timer.load = secs * SECTICKS;
115593ee0ff2SBrian Somers     bundle->idle.timer.arg = bundle;
1156dd7e2610SBrian Somers     timer_Start(&bundle->idle.timer);
11570a4b6c5cSBrian Somers     bundle->idle.done = now + secs;
1158ab886ad0SBrian Somers   }
1159ab886ad0SBrian Somers }
1160ab886ad0SBrian Somers 
1161ab886ad0SBrian Somers void
1162dade2407SBrian Somers bundle_SetIdleTimer(struct bundle *bundle, int timeout, int min_timeout)
1163ab886ad0SBrian Somers {
1164dade2407SBrian Somers   bundle->cfg.idle.timeout = timeout;
1165dade2407SBrian Somers   if (min_timeout >= 0)
1166dade2407SBrian Somers     bundle->cfg.idle.min_timeout = min_timeout;
116730949fd4SBrian Somers   if (ncp_LayersOpen(&bundle->ncp))
11680a4b6c5cSBrian Somers     bundle_StartIdleTimer(bundle, 0);
1169ab886ad0SBrian Somers }
1170ab886ad0SBrian Somers 
1171ab886ad0SBrian Somers void
1172ab886ad0SBrian Somers bundle_StopIdleTimer(struct bundle *bundle)
1173ab886ad0SBrian Somers {
1174dd7e2610SBrian Somers   timer_Stop(&bundle->idle.timer);
11754a632c80SBrian Somers   bundle->idle.done = 0;
1176ab886ad0SBrian Somers }
1177ab886ad0SBrian Somers 
117804eaa58cSBrian Somers static int
1179ab886ad0SBrian Somers bundle_RemainingIdleTime(struct bundle *bundle)
1180ab886ad0SBrian Somers {
118193ee0ff2SBrian Somers   if (bundle->idle.done)
118293ee0ff2SBrian Somers     return bundle->idle.done - time(NULL);
1183ab886ad0SBrian Somers   return -1;
1184ab886ad0SBrian Somers }
11853b0f8d2eSBrian Somers 
11863b0f8d2eSBrian Somers int
11873b0f8d2eSBrian Somers bundle_IsDead(struct bundle *bundle)
11883b0f8d2eSBrian Somers {
11893b0f8d2eSBrian Somers   return !bundle->links || (bundle->phase == PHASE_DEAD && bundle->CleaningUp);
11903b0f8d2eSBrian Somers }
1191b6217683SBrian Somers 
119204eaa58cSBrian Somers static struct datalink *
119304eaa58cSBrian Somers bundle_DatalinkLinkout(struct bundle *bundle, struct datalink *dl)
1194cd7bd93aSBrian Somers {
1195cd7bd93aSBrian Somers   struct datalink **dlp;
1196cd7bd93aSBrian Somers 
1197cd7bd93aSBrian Somers   for (dlp = &bundle->links; *dlp; dlp = &(*dlp)->next)
1198cd7bd93aSBrian Somers     if (*dlp == dl) {
119904eaa58cSBrian Somers       *dlp = dl->next;
120004eaa58cSBrian Somers       dl->next = NULL;
120104eaa58cSBrian Somers       bundle_LinksRemoved(bundle);
120204eaa58cSBrian Somers       return dl;
1203cd7bd93aSBrian Somers     }
120404eaa58cSBrian Somers 
120504eaa58cSBrian Somers   return NULL;
120604eaa58cSBrian Somers }
120704eaa58cSBrian Somers 
120804eaa58cSBrian Somers static void
120904eaa58cSBrian Somers bundle_DatalinkLinkin(struct bundle *bundle, struct datalink *dl)
121004eaa58cSBrian Somers {
121104eaa58cSBrian Somers   struct datalink **dlp = &bundle->links;
121204eaa58cSBrian Somers 
121304eaa58cSBrian Somers   while (*dlp)
121404eaa58cSBrian Somers     dlp = &(*dlp)->next;
121504eaa58cSBrian Somers 
121604eaa58cSBrian Somers   *dlp = dl;
121704eaa58cSBrian Somers   dl->next = NULL;
121804eaa58cSBrian Somers 
121904eaa58cSBrian Somers   bundle_LinkAdded(bundle, dl);
1220ab2de065SBrian Somers   mp_CheckAutoloadTimer(&bundle->ncp.mp);
1221565e35e5SBrian Somers }
1222565e35e5SBrian Somers 
1223565e35e5SBrian Somers void
1224565e35e5SBrian Somers bundle_CleanDatalinks(struct bundle *bundle)
1225565e35e5SBrian Somers {
1226565e35e5SBrian Somers   struct datalink **dlp = &bundle->links;
122704eaa58cSBrian Somers   int found = 0;
1228565e35e5SBrian Somers 
1229565e35e5SBrian Somers   while (*dlp)
1230565e35e5SBrian Somers     if ((*dlp)->state == DATALINK_CLOSED &&
1231f6a4e748SBrian Somers         (*dlp)->physical->type &
1232f6a4e748SBrian Somers         (PHYS_DIRECT|PHYS_BACKGROUND|PHYS_FOREGROUND)) {
1233565e35e5SBrian Somers       *dlp = datalink_Destroy(*dlp);
123404eaa58cSBrian Somers       found++;
123504eaa58cSBrian Somers     } else
1236565e35e5SBrian Somers       dlp = &(*dlp)->next;
123704eaa58cSBrian Somers 
123804eaa58cSBrian Somers   if (found)
123904eaa58cSBrian Somers     bundle_LinksRemoved(bundle);
124004eaa58cSBrian Somers }
124104eaa58cSBrian Somers 
124204eaa58cSBrian Somers int
124304eaa58cSBrian Somers bundle_DatalinkClone(struct bundle *bundle, struct datalink *dl,
124404eaa58cSBrian Somers                      const char *name)
124504eaa58cSBrian Somers {
124604eaa58cSBrian Somers   if (bundle2datalink(bundle, name)) {
124704eaa58cSBrian Somers     log_Printf(LogWARN, "Clone: %s: name already exists\n", name);
124804eaa58cSBrian Somers     return 0;
124904eaa58cSBrian Somers   }
125004eaa58cSBrian Somers 
125104eaa58cSBrian Somers   bundle_DatalinkLinkin(bundle, datalink_Clone(dl, name));
125204eaa58cSBrian Somers   return 1;
125304eaa58cSBrian Somers }
125404eaa58cSBrian Somers 
125504eaa58cSBrian Somers void
125604eaa58cSBrian Somers bundle_DatalinkRemove(struct bundle *bundle, struct datalink *dl)
125704eaa58cSBrian Somers {
125804eaa58cSBrian Somers   dl = bundle_DatalinkLinkout(bundle, dl);
125904eaa58cSBrian Somers   if (dl)
126004eaa58cSBrian Somers     datalink_Destroy(dl);
1261cd7bd93aSBrian Somers }
126249052c95SBrian Somers 
126349052c95SBrian Somers void
126449052c95SBrian Somers bundle_SetLabel(struct bundle *bundle, const char *label)
126549052c95SBrian Somers {
126649052c95SBrian Somers   if (label)
126749052c95SBrian Somers     strncpy(bundle->cfg.label, label, sizeof bundle->cfg.label - 1);
126849052c95SBrian Somers   else
126949052c95SBrian Somers     *bundle->cfg.label = '\0';
127049052c95SBrian Somers }
127149052c95SBrian Somers 
127249052c95SBrian Somers const char *
127349052c95SBrian Somers bundle_GetLabel(struct bundle *bundle)
127449052c95SBrian Somers {
127549052c95SBrian Somers   return *bundle->cfg.label ? bundle->cfg.label : NULL;
127649052c95SBrian Somers }
12771fa665f5SBrian Somers 
12782cb305afSBrian Somers int
12792cb305afSBrian Somers bundle_LinkSize()
12801fa665f5SBrian Somers {
128196c9bb21SBrian Somers   struct iovec iov[SCATTER_SEGMENTS];
12822cb305afSBrian Somers   int niov, expect, f;
128387c3786eSBrian Somers 
128496c9bb21SBrian Somers   iov[0].iov_len = strlen(Version) + 1;
12852cb305afSBrian Somers   iov[0].iov_base = NULL;
12862cb305afSBrian Somers   niov = 1;
12872cb305afSBrian Somers   if (datalink2iov(NULL, iov, &niov, SCATTER_SEGMENTS, NULL, NULL) == -1) {
12882cb305afSBrian Somers     log_Printf(LogERROR, "Cannot determine space required for link\n");
12892cb305afSBrian Somers     return 0;
129054cd8e13SBrian Somers   }
12916f384573SBrian Somers 
129296c9bb21SBrian Somers   for (f = expect = 0; f < niov; f++)
129396c9bb21SBrian Somers     expect += iov[f].iov_len;
129496c9bb21SBrian Somers 
12952cb305afSBrian Somers   return expect;
129687c3786eSBrian Somers }
129796c9bb21SBrian Somers 
12982cb305afSBrian Somers void
12992cb305afSBrian Somers bundle_ReceiveDatalink(struct bundle *bundle, int s)
13002cb305afSBrian Somers {
13012cb305afSBrian Somers   char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int) * SEND_MAXFD];
1302cbee9754SBrian Somers   int niov, expect, f, *fd, nfd, onfd, got;
13032cb305afSBrian Somers   struct iovec iov[SCATTER_SEGMENTS];
13042cb305afSBrian Somers   struct cmsghdr *cmsg;
13052cb305afSBrian Somers   struct msghdr msg;
13062cb305afSBrian Somers   struct datalink *dl;
13072cb305afSBrian Somers   pid_t pid;
13082cb305afSBrian Somers 
13092cb305afSBrian Somers   log_Printf(LogPHASE, "Receiving datalink\n");
13102cb305afSBrian Somers 
13112cb305afSBrian Somers   /*
13122cb305afSBrian Somers    * Create our scatter/gather array - passing NULL gets the space
13132cb305afSBrian Somers    * allocation requirement rather than actually flattening the
13142cb305afSBrian Somers    * structures.
13152cb305afSBrian Somers    */
13162cb305afSBrian Somers   iov[0].iov_len = strlen(Version) + 1;
13172cb305afSBrian Somers   iov[0].iov_base = NULL;
13182cb305afSBrian Somers   niov = 1;
13192cb305afSBrian Somers   if (datalink2iov(NULL, iov, &niov, SCATTER_SEGMENTS, NULL, NULL) == -1) {
13202cb305afSBrian Somers     log_Printf(LogERROR, "Cannot determine space required for link\n");
13212cb305afSBrian Somers     return;
13222cb305afSBrian Somers   }
13232cb305afSBrian Somers 
13242cb305afSBrian Somers   /* Allocate the scatter/gather array for recvmsg() */
13252cb305afSBrian Somers   for (f = expect = 0; f < niov; f++) {
13262cb305afSBrian Somers     if ((iov[f].iov_base = malloc(iov[f].iov_len)) == NULL) {
13272cb305afSBrian Somers       log_Printf(LogERROR, "Cannot allocate space to receive link\n");
13282cb305afSBrian Somers       return;
13292cb305afSBrian Somers     }
1330cbee9754SBrian Somers     if (f)
13312cb305afSBrian Somers       expect += iov[f].iov_len;
13322cb305afSBrian Somers   }
13332cb305afSBrian Somers 
13342cb305afSBrian Somers   /* Set up our message */
13352cb305afSBrian Somers   cmsg = (struct cmsghdr *)cmsgbuf;
13362cb305afSBrian Somers   cmsg->cmsg_len = sizeof cmsgbuf;
13372cb305afSBrian Somers   cmsg->cmsg_level = SOL_SOCKET;
13382cb305afSBrian Somers   cmsg->cmsg_type = 0;
13392cb305afSBrian Somers 
134096c9bb21SBrian Somers   memset(&msg, '\0', sizeof msg);
13412cb305afSBrian Somers   msg.msg_name = NULL;
13422cb305afSBrian Somers   msg.msg_namelen = 0;
134396c9bb21SBrian Somers   msg.msg_iov = iov;
1344cbee9754SBrian Somers   msg.msg_iovlen = 1;		/* Only send the version at the first pass */
134596c9bb21SBrian Somers   msg.msg_control = cmsgbuf;
134696c9bb21SBrian Somers   msg.msg_controllen = sizeof cmsgbuf;
134796c9bb21SBrian Somers 
1348209dc102SBrian Somers   log_Printf(LogDEBUG, "Expecting %u scatter/gather bytes\n",
1349209dc102SBrian Somers              (unsigned)iov[0].iov_len);
13502cb305afSBrian Somers 
1351cbee9754SBrian Somers   if ((got = recvmsg(s, &msg, MSG_WAITALL)) != iov[0].iov_len) {
1352cbee9754SBrian Somers     if (got == -1)
135396c9bb21SBrian Somers       log_Printf(LogERROR, "Failed recvmsg: %s\n", strerror(errno));
135496c9bb21SBrian Somers     else
1355209dc102SBrian Somers       log_Printf(LogERROR, "Failed recvmsg: Got %d, not %u\n",
1356209dc102SBrian Somers                  got, (unsigned)iov[0].iov_len);
135796c9bb21SBrian Somers     while (niov--)
135896c9bb21SBrian Somers       free(iov[niov].iov_base);
135996c9bb21SBrian Somers     return;
136096c9bb21SBrian Somers   }
136196c9bb21SBrian Somers 
13622cb305afSBrian Somers   if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
13632cb305afSBrian Somers     log_Printf(LogERROR, "Recvmsg: no descriptors received !\n");
13642cb305afSBrian Somers     while (niov--)
13652cb305afSBrian Somers       free(iov[niov].iov_base);
13662cb305afSBrian Somers     return;
136787c3786eSBrian Somers   }
136887c3786eSBrian Somers 
13692bc21ed9SDavid Malone   fd = (int *)CMSG_DATA(cmsg);
13702bc21ed9SDavid Malone   nfd = ((caddr_t)cmsg + cmsg->cmsg_len - (caddr_t)fd) / sizeof(int);
13712cb305afSBrian Somers 
13722cb305afSBrian Somers   if (nfd < 2) {
13738e7bd08eSBrian Somers     log_Printf(LogERROR, "Recvmsg: %d descriptor%s received (too few) !\n",
13742cb305afSBrian Somers                nfd, nfd == 1 ? "" : "s");
13752cb305afSBrian Somers     while (nfd--)
13762cb305afSBrian Somers       close(fd[nfd]);
1377ad5b0e8bSBrian Somers     while (niov--)
1378ad5b0e8bSBrian Somers       free(iov[niov].iov_base);
1379ad5b0e8bSBrian Somers     return;
138054cd8e13SBrian Somers   }
138154cd8e13SBrian Somers 
138287c3786eSBrian Somers   /*
1383cbee9754SBrian Somers    * We've successfully received two or more open file descriptors
1384cbee9754SBrian Somers    * through our socket, plus a version string.  Make sure it's the
1385cbee9754SBrian Somers    * correct version, and drop the connection if it's not.
138687c3786eSBrian Somers    */
138796c9bb21SBrian Somers   if (strncmp(Version, iov[0].iov_base, iov[0].iov_len)) {
138896c9bb21SBrian Somers     log_Printf(LogWARN, "Cannot receive datalink, incorrect version"
138996c9bb21SBrian Somers                " (\"%.*s\", not \"%s\")\n", (int)iov[0].iov_len,
13907d81ddf5SBrian Somers                (char *)iov[0].iov_base, Version);
13912cb305afSBrian Somers     while (nfd--)
13922cb305afSBrian Somers       close(fd[nfd]);
139396c9bb21SBrian Somers     while (niov--)
139496c9bb21SBrian Somers       free(iov[niov].iov_base);
139596c9bb21SBrian Somers     return;
139696c9bb21SBrian Somers   }
139796c9bb21SBrian Somers 
1398cbee9754SBrian Somers   /*
1399cbee9754SBrian Somers    * Everything looks good.  Send the other side our process id so that
1400cbee9754SBrian Somers    * they can transfer lock ownership, and wait for them to send the
1401cbee9754SBrian Somers    * actual link data.
1402cbee9754SBrian Somers    */
1403cbee9754SBrian Somers   pid = getpid();
1404cbee9754SBrian Somers   if ((got = write(fd[1], &pid, sizeof pid)) != sizeof pid) {
1405cbee9754SBrian Somers     if (got == -1)
1406cbee9754SBrian Somers       log_Printf(LogERROR, "Failed write: %s\n", strerror(errno));
1407cbee9754SBrian Somers     else
1408cbee9754SBrian Somers       log_Printf(LogERROR, "Failed write: Got %d, not %d\n", got,
1409cbee9754SBrian Somers                  (int)(sizeof pid));
1410cbee9754SBrian Somers     while (nfd--)
1411cbee9754SBrian Somers       close(fd[nfd]);
1412cbee9754SBrian Somers     while (niov--)
1413cbee9754SBrian Somers       free(iov[niov].iov_base);
1414cbee9754SBrian Somers     return;
1415cbee9754SBrian Somers   }
1416cbee9754SBrian Somers 
1417cbee9754SBrian Somers   if ((got = readv(fd[1], iov + 1, niov - 1)) != expect) {
1418cbee9754SBrian Somers     if (got == -1)
1419cbee9754SBrian Somers       log_Printf(LogERROR, "Failed write: %s\n", strerror(errno));
1420cbee9754SBrian Somers     else
1421cbee9754SBrian Somers       log_Printf(LogERROR, "Failed write: Got %d, not %d\n", got, expect);
1422cbee9754SBrian Somers     while (nfd--)
1423cbee9754SBrian Somers       close(fd[nfd]);
1424cbee9754SBrian Somers     while (niov--)
1425cbee9754SBrian Somers       free(iov[niov].iov_base);
1426cbee9754SBrian Somers     return;
1427cbee9754SBrian Somers   }
1428cbee9754SBrian Somers   close(fd[1]);
1429cbee9754SBrian Somers 
14302cb305afSBrian Somers   onfd = nfd;	/* We've got this many in our array */
14318e7bd08eSBrian Somers   nfd -= 2;	/* Don't include p->fd and our reply descriptor */
14322cb305afSBrian Somers   niov = 1;	/* Skip the version id */
143387c3786eSBrian Somers   dl = iov2datalink(bundle, iov, &niov, sizeof iov / sizeof *iov, fd[0],
14342cb305afSBrian Somers                     fd + 2, &nfd);
1435b7c5748eSBrian Somers   if (dl) {
14362cb305afSBrian Somers 
143787c3786eSBrian Somers     if (nfd) {
143887c3786eSBrian Somers       log_Printf(LogERROR, "bundle_ReceiveDatalink: Failed to handle %d "
14392cb305afSBrian Somers                  "auxiliary file descriptors (%d remain)\n", onfd, nfd);
144087c3786eSBrian Somers       datalink_Destroy(dl);
144187c3786eSBrian Somers       while (nfd--)
144287c3786eSBrian Somers         close(fd[onfd--]);
14432cb305afSBrian Somers       close(fd[0]);
144487c3786eSBrian Somers     } else {
144504eaa58cSBrian Somers       bundle_DatalinkLinkin(bundle, dl);
1446b7c5748eSBrian Somers       datalink_AuthOk(dl);
1447ab2de065SBrian Somers       bundle_CalculateBandwidth(dl->bundle);
144887c3786eSBrian Somers     }
144987c3786eSBrian Somers   } else {
145087c3786eSBrian Somers     while (nfd--)
145187c3786eSBrian Somers       close(fd[onfd--]);
145287c3786eSBrian Somers     close(fd[0]);
14532cb305afSBrian Somers     close(fd[1]);
145487c3786eSBrian Somers   }
145596c9bb21SBrian Somers 
145696c9bb21SBrian Somers   free(iov[0].iov_base);
14571fa665f5SBrian Somers }
14581fa665f5SBrian Somers 
14591fa665f5SBrian Somers void
146096c9bb21SBrian Somers bundle_SendDatalink(struct datalink *dl, int s, struct sockaddr_un *sun)
14611fa665f5SBrian Somers {
14622bc21ed9SDavid Malone   char cmsgbuf[CMSG_SPACE(sizeof(int) * SEND_MAXFD)];
14632cb305afSBrian Somers   const char *constlock;
14642cb305afSBrian Somers   char *lock;
146587c3786eSBrian Somers   struct cmsghdr *cmsg;
146696c9bb21SBrian Somers   struct msghdr msg;
146796c9bb21SBrian Somers   struct iovec iov[SCATTER_SEGMENTS];
14682cb305afSBrian Somers   int niov, f, expect, newsid, fd[SEND_MAXFD], nfd, reply[2], got;
146985fd273aSBrian Somers   pid_t newpid;
14706f384573SBrian Somers 
1471dd7e2610SBrian Somers   log_Printf(LogPHASE, "Transmitting datalink %s\n", dl->name);
14726f384573SBrian Somers 
14732cb305afSBrian Somers   /* Record the base device name for a lock transfer later */
14742cb305afSBrian Somers   constlock = physical_LockedDevice(dl->physical);
14752cb305afSBrian Somers   if (constlock) {
14762cb305afSBrian Somers     lock = alloca(strlen(constlock) + 1);
14772cb305afSBrian Somers     strcpy(lock, constlock);
14782cb305afSBrian Somers   } else
14792cb305afSBrian Somers     lock = NULL;
14802cb305afSBrian Somers 
148104eaa58cSBrian Somers   bundle_LinkClosed(dl->bundle, dl);
14820f2f3eb3SBrian Somers   bundle_DatalinkLinkout(dl->bundle, dl);
1483ea722969SBrian Somers 
148496c9bb21SBrian Somers   /* Build our scatter/gather array */
148596c9bb21SBrian Somers   iov[0].iov_len = strlen(Version) + 1;
148696c9bb21SBrian Somers   iov[0].iov_base = strdup(Version);
148796c9bb21SBrian Somers   niov = 1;
148887c3786eSBrian Somers   nfd = 0;
14896f384573SBrian Somers 
14902cb305afSBrian Somers   fd[0] = datalink2iov(dl, iov, &niov, SCATTER_SEGMENTS, fd + 2, &nfd);
14916f384573SBrian Somers 
14922cb305afSBrian Somers   if (fd[0] != -1 && socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, reply) != -1) {
14932cb305afSBrian Somers     /*
14942cb305afSBrian Somers      * fd[1] is used to get the peer process id back, then to confirm that
14952cb305afSBrian Somers      * we've transferred any device locks to that process id.
14962cb305afSBrian Somers      */
14972cb305afSBrian Somers     fd[1] = reply[1];
149887c3786eSBrian Somers 
14992cb305afSBrian Somers     nfd += 2;			/* Include fd[0] and fd[1] */
150096c9bb21SBrian Somers     memset(&msg, '\0', sizeof msg);
150154cd8e13SBrian Somers 
15022cb305afSBrian Somers     msg.msg_name = NULL;
15032cb305afSBrian Somers     msg.msg_namelen = 0;
1504cbee9754SBrian Somers     /*
1505cbee9754SBrian Somers      * Only send the version to start...  We used to send the whole lot, but
1506cbee9754SBrian Somers      * this caused problems with our RECVBUF size as a single link is about
1507cbee9754SBrian Somers      * 22k !  This way, we should bump into no limits.
1508cbee9754SBrian Somers      */
1509cbee9754SBrian Somers     msg.msg_iovlen = 1;
151096c9bb21SBrian Somers     msg.msg_iov = iov;
15112cb305afSBrian Somers     msg.msg_control = cmsgbuf;
15122bc21ed9SDavid Malone     msg.msg_controllen = CMSG_SPACE(sizeof(int) * nfd);
15132cb305afSBrian Somers     msg.msg_flags = 0;
151454cd8e13SBrian Somers 
15152cb305afSBrian Somers     cmsg = (struct cmsghdr *)cmsgbuf;
15162cb305afSBrian Somers     cmsg->cmsg_len = msg.msg_controllen;
151754cd8e13SBrian Somers     cmsg->cmsg_level = SOL_SOCKET;
151854cd8e13SBrian Somers     cmsg->cmsg_type = SCM_RIGHTS;
151987c3786eSBrian Somers 
15202cb305afSBrian Somers     for (f = 0; f < nfd; f++)
15212bc21ed9SDavid Malone       *((int *)CMSG_DATA(cmsg) + f) = fd[f];
152247723d29SBrian Somers 
1523cbee9754SBrian Somers     for (f = 1, expect = 0; f < niov; f++)
152496c9bb21SBrian Somers       expect += iov[f].iov_len;
152547723d29SBrian Somers 
1526cbee9754SBrian Somers     if (setsockopt(reply[0], SOL_SOCKET, SO_SNDBUF, &expect, sizeof(int)) == -1)
1527cbee9754SBrian Somers       log_Printf(LogERROR, "setsockopt(SO_RCVBUF, %d): %s\n", expect,
1528cbee9754SBrian Somers                  strerror(errno));
1529cbee9754SBrian Somers     if (setsockopt(reply[1], SOL_SOCKET, SO_RCVBUF, &expect, sizeof(int)) == -1)
1530cbee9754SBrian Somers       log_Printf(LogERROR, "setsockopt(SO_RCVBUF, %d): %s\n", expect,
1531cbee9754SBrian Somers                  strerror(errno));
1532cbee9754SBrian Somers 
1533209dc102SBrian Somers     log_Printf(LogDEBUG, "Sending %d descriptor%s and %u bytes in scatter"
1534209dc102SBrian Somers                "/gather array\n", nfd, nfd == 1 ? "" : "s",
1535209dc102SBrian Somers                (unsigned)iov[0].iov_len);
153647723d29SBrian Somers 
15372cb305afSBrian Somers     if ((got = sendmsg(s, &msg, 0)) == -1)
15382cb305afSBrian Somers       log_Printf(LogERROR, "Failed sendmsg: %s: %s\n",
15392cb305afSBrian Somers                  sun->sun_path, strerror(errno));
1540cbee9754SBrian Somers     else if (got != iov[0].iov_len)
1541209dc102SBrian Somers       log_Printf(LogERROR, "%s: Failed initial sendmsg: Only sent %d of %u\n",
1542209dc102SBrian Somers                  sun->sun_path, got, (unsigned)iov[0].iov_len);
15432cb305afSBrian Somers     else {
15448e7bd08eSBrian Somers       /* We must get the ACK before closing the descriptor ! */
15452cb305afSBrian Somers       int res;
15462cb305afSBrian Somers 
1547cbee9754SBrian Somers       if ((got = read(reply[0], &newpid, sizeof newpid)) == sizeof newpid) {
1548cbee9754SBrian Somers         log_Printf(LogDEBUG, "Received confirmation from pid %d\n",
1549cbee9754SBrian Somers                    (int)newpid);
15502cb305afSBrian Somers         if (lock && (res = ID0uu_lock_txfr(lock, newpid)) != UU_LOCK_OK)
1551b42135deSBrian Somers             log_Printf(LogERROR, "uu_lock_txfr: %s\n", uu_lockerr(res));
15522cb305afSBrian Somers 
1553cbee9754SBrian Somers         log_Printf(LogDEBUG, "Transmitting link (%d bytes)\n", expect);
1554cbee9754SBrian Somers         if ((got = writev(reply[0], iov + 1, niov - 1)) != expect) {
1555cbee9754SBrian Somers           if (got == -1)
1556cbee9754SBrian Somers             log_Printf(LogERROR, "%s: Failed writev: %s\n",
1557cbee9754SBrian Somers                        sun->sun_path, strerror(errno));
1558cbee9754SBrian Somers           else
1559cbee9754SBrian Somers             log_Printf(LogERROR, "%s: Failed writev: Wrote %d of %d\n",
1560cbee9754SBrian Somers                        sun->sun_path, got, expect);
1561cbee9754SBrian Somers         }
1562cbee9754SBrian Somers       } else if (got == -1)
1563cbee9754SBrian Somers         log_Printf(LogERROR, "%s: Failed socketpair read: %s\n",
1564cbee9754SBrian Somers                    sun->sun_path, strerror(errno));
1565cbee9754SBrian Somers       else
1566cbee9754SBrian Somers         log_Printf(LogERROR, "%s: Failed socketpair read: Got %d of %d\n",
1567cbee9754SBrian Somers                    sun->sun_path, got, (int)(sizeof newpid));
15682cb305afSBrian Somers     }
15692cb305afSBrian Somers 
15702cb305afSBrian Somers     close(reply[0]);
15712cb305afSBrian Somers     close(reply[1]);
157254cd8e13SBrian Somers 
1573ac685e31SBrian Somers     newsid = Enabled(dl->bundle, OPT_KEEPSESSION) ||
157487c3786eSBrian Somers              tcgetpgrp(fd[0]) == getpgrp();
157587c3786eSBrian Somers     while (nfd)
157687c3786eSBrian Somers       close(fd[--nfd]);
15771384bd27SBrian Somers     if (newsid)
15782cb305afSBrian Somers       bundle_setsid(dl->bundle, got != -1);
157947723d29SBrian Somers   }
158085fd273aSBrian Somers   close(s);
158196c9bb21SBrian Somers 
158296c9bb21SBrian Somers   while (niov--)
158396c9bb21SBrian Somers     free(iov[niov].iov_base);
15841fa665f5SBrian Somers }
1585dd0645c5SBrian Somers 
1586dd0645c5SBrian Somers int
158758d55334SBrian Somers bundle_RenameDatalink(struct bundle *bundle, struct datalink *ndl,
158858d55334SBrian Somers                       const char *name)
158958d55334SBrian Somers {
159058d55334SBrian Somers   struct datalink *dl;
159158d55334SBrian Somers 
159258d55334SBrian Somers   if (!strcasecmp(ndl->name, name))
159358d55334SBrian Somers     return 1;
159458d55334SBrian Somers 
159558d55334SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
159658d55334SBrian Somers     if (!strcasecmp(dl->name, name))
159758d55334SBrian Somers       return 0;
159858d55334SBrian Somers 
159958d55334SBrian Somers   datalink_Rename(ndl, name);
160058d55334SBrian Somers   return 1;
160158d55334SBrian Somers }
160258d55334SBrian Somers 
160358d55334SBrian Somers int
1604dd0645c5SBrian Somers bundle_SetMode(struct bundle *bundle, struct datalink *dl, int mode)
1605dd0645c5SBrian Somers {
1606dd0645c5SBrian Somers   int omode;
1607dd0645c5SBrian Somers 
1608dd0645c5SBrian Somers   omode = dl->physical->type;
1609dd0645c5SBrian Somers   if (omode == mode)
1610dd0645c5SBrian Somers     return 1;
1611dd0645c5SBrian Somers 
1612ff0f9439SBrian Somers   if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO))
1613ff0f9439SBrian Somers     /* First auto link */
1614dd0645c5SBrian Somers     if (bundle->ncp.ipcp.peer_ip.s_addr == INADDR_ANY) {
1615ff0f9439SBrian Somers       log_Printf(LogWARN, "You must `set ifaddr' or `open' before"
1616ff0f9439SBrian Somers                  " changing mode to %s\n", mode2Nam(mode));
1617dd0645c5SBrian Somers       return 0;
1618dd0645c5SBrian Somers     }
1619dd0645c5SBrian Somers 
1620dd0645c5SBrian Somers   if (!datalink_SetMode(dl, mode))
1621dd0645c5SBrian Somers     return 0;
1622dd0645c5SBrian Somers 
1623ff0f9439SBrian Somers   if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO) &&
1624ff0f9439SBrian Somers       bundle->phase != PHASE_NETWORK)
1625ff0f9439SBrian Somers     /* First auto link, we need an interface */
1626dd0645c5SBrian Somers     ipcp_InterfaceUp(&bundle->ncp.ipcp);
1627dd0645c5SBrian Somers 
1628ab2de065SBrian Somers   /* Regenerate phys_type and adjust idle timer */
162904eaa58cSBrian Somers   bundle_LinksRemoved(bundle);
1630dd0645c5SBrian Somers 
1631dd0645c5SBrian Somers   return 1;
1632dd0645c5SBrian Somers }
16331384bd27SBrian Somers 
16341384bd27SBrian Somers void
16351384bd27SBrian Somers bundle_setsid(struct bundle *bundle, int holdsession)
16361384bd27SBrian Somers {
16371384bd27SBrian Somers   /*
16381384bd27SBrian Somers    * Lose the current session.  This means getting rid of our pid
16391384bd27SBrian Somers    * too so that the tty device will really go away, and any getty
16401384bd27SBrian Somers    * etc will be allowed to restart.
16411384bd27SBrian Somers    */
16421384bd27SBrian Somers   pid_t pid, orig;
16431384bd27SBrian Somers   int fds[2];
16441384bd27SBrian Somers   char done;
16451384bd27SBrian Somers   struct datalink *dl;
16461384bd27SBrian Somers 
1647e62ce959SBrian Somers   if (!holdsession && bundle_IsDead(bundle)) {
1648e62ce959SBrian Somers     /*
1649e62ce959SBrian Somers      * No need to lose our session after all... we're going away anyway
1650e62ce959SBrian Somers      *
1651e62ce959SBrian Somers      * We should really stop the timer and pause if holdsession is set and
1652e62ce959SBrian Somers      * the bundle's dead, but that leaves other resources lying about :-(
1653e62ce959SBrian Somers      */
1654e62ce959SBrian Somers     return;
1655e62ce959SBrian Somers   }
1656e62ce959SBrian Somers 
16571384bd27SBrian Somers   orig = getpid();
16581384bd27SBrian Somers   if (pipe(fds) == -1) {
16591384bd27SBrian Somers     log_Printf(LogERROR, "pipe: %s\n", strerror(errno));
16601384bd27SBrian Somers     return;
16611384bd27SBrian Somers   }
16621384bd27SBrian Somers   switch ((pid = fork())) {
16631384bd27SBrian Somers     case -1:
16641384bd27SBrian Somers       log_Printf(LogERROR, "fork: %s\n", strerror(errno));
16651384bd27SBrian Somers       close(fds[0]);
16661384bd27SBrian Somers       close(fds[1]);
16671384bd27SBrian Somers       return;
16681384bd27SBrian Somers     case 0:
16691384bd27SBrian Somers       close(fds[1]);
16704be0e57dSBrian Somers       read(fds[0], &done, 1);		/* uu_locks are mine ! */
16714be0e57dSBrian Somers       close(fds[0]);
16721384bd27SBrian Somers       if (pipe(fds) == -1) {
16731384bd27SBrian Somers         log_Printf(LogERROR, "pipe(2): %s\n", strerror(errno));
16741384bd27SBrian Somers         return;
16751384bd27SBrian Somers       }
16761384bd27SBrian Somers       switch ((pid = fork())) {
16771384bd27SBrian Somers         case -1:
1678a33b2ef7SBrian Somers           log_Printf(LogERROR, "fork(2): %s\n", strerror(errno));
16791384bd27SBrian Somers           close(fds[0]);
16801384bd27SBrian Somers           close(fds[1]);
16811384bd27SBrian Somers           return;
16821384bd27SBrian Somers         case 0:
16831384bd27SBrian Somers           close(fds[1]);
16844be0e57dSBrian Somers           bundle_LockTun(bundle);	/* update pid */
16854be0e57dSBrian Somers           read(fds[0], &done, 1);	/* uu_locks are mine ! */
16864be0e57dSBrian Somers           close(fds[0]);
16871384bd27SBrian Somers           setsid();
168806b47306SBrian Somers           bundle_ChangedPID(bundle);
1689b42135deSBrian Somers           log_Printf(LogDEBUG, "%d -> %d: %s session control\n",
16901384bd27SBrian Somers                      (int)orig, (int)getpid(),
16911384bd27SBrian Somers                      holdsession ? "Passed" : "Dropped");
16927e778f13SBrian Somers           timer_InitService(0);		/* Start the Timer Service */
16931384bd27SBrian Somers           break;
16941384bd27SBrian Somers         default:
16954be0e57dSBrian Somers           close(fds[0]);
16965d9e6103SBrian Somers           /* Give away all our physical locks (to the final process) */
16971384bd27SBrian Somers           for (dl = bundle->links; dl; dl = dl->next)
16981384bd27SBrian Somers             if (dl->state != DATALINK_CLOSED)
16995d9e6103SBrian Somers               physical_ChangedPid(dl->physical, pid);
17004be0e57dSBrian Somers           write(fds[1], "!", 1);	/* done */
17014be0e57dSBrian Somers           close(fds[1]);
17022cb305afSBrian Somers           _exit(0);
17031384bd27SBrian Somers           break;
17041384bd27SBrian Somers       }
17051384bd27SBrian Somers       break;
17061384bd27SBrian Somers     default:
17074be0e57dSBrian Somers       close(fds[0]);
17085d9e6103SBrian Somers       /* Give away all our physical locks (to the intermediate process) */
17091384bd27SBrian Somers       for (dl = bundle->links; dl; dl = dl->next)
17101384bd27SBrian Somers         if (dl->state != DATALINK_CLOSED)
17115d9e6103SBrian Somers           physical_ChangedPid(dl->physical, pid);
17124be0e57dSBrian Somers       write(fds[1], "!", 1);	/* done */
17134be0e57dSBrian Somers       close(fds[1]);
17141384bd27SBrian Somers       if (holdsession) {
17151384bd27SBrian Somers         int fd, status;
17161384bd27SBrian Somers 
17171384bd27SBrian Somers         timer_TermService();
17181384bd27SBrian Somers         signal(SIGPIPE, SIG_DFL);
17191384bd27SBrian Somers         signal(SIGALRM, SIG_DFL);
17201384bd27SBrian Somers         signal(SIGHUP, SIG_DFL);
17211384bd27SBrian Somers         signal(SIGTERM, SIG_DFL);
17221384bd27SBrian Somers         signal(SIGINT, SIG_DFL);
17231384bd27SBrian Somers         signal(SIGQUIT, SIG_DFL);
17241384bd27SBrian Somers         for (fd = getdtablesize(); fd >= 0; fd--)
17251384bd27SBrian Somers           close(fd);
17261384bd27SBrian Somers         /*
17271384bd27SBrian Somers          * Reap the intermediate process.  As we're not exiting but the
17281384bd27SBrian Somers          * intermediate is, we don't want it to become defunct.
17291384bd27SBrian Somers          */
17301384bd27SBrian Somers         waitpid(pid, &status, 0);
17318e7b8599SBrian Somers         /* Tweak our process arguments.... */
1732ebe96675SBrian Somers         SetTitle("session owner");
173368602c3eSBrian Somers #ifndef NOSUID
1734a19a5c02SBrian Somers         setuid(ID0realuid());
173568602c3eSBrian Somers #endif
17361384bd27SBrian Somers         /*
17371384bd27SBrian Somers          * Hang around for a HUP.  This should happen as soon as the
17388e7bd08eSBrian Somers          * ppp that we passed our ctty descriptor to closes it.
17398e7bd08eSBrian Somers          * NOTE: If this process dies, the passed descriptor becomes
17401384bd27SBrian Somers          *       invalid and will give a select() error by setting one
17411384bd27SBrian Somers          *       of the error fds, aborting the other ppp.  We don't
17421384bd27SBrian Somers          *       want that to happen !
17431384bd27SBrian Somers          */
17441384bd27SBrian Somers         pause();
17451384bd27SBrian Somers       }
17462cb305afSBrian Somers       _exit(0);
17471384bd27SBrian Somers       break;
17481384bd27SBrian Somers   }
17491384bd27SBrian Somers }
17509b5f8ffdSBrian Somers 
17519b5f8ffdSBrian Somers int
17529b5f8ffdSBrian Somers bundle_HighestState(struct bundle *bundle)
17539b5f8ffdSBrian Somers {
17549b5f8ffdSBrian Somers   struct datalink *dl;
17559b5f8ffdSBrian Somers   int result = DATALINK_CLOSED;
17569b5f8ffdSBrian Somers 
17579b5f8ffdSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
17589b5f8ffdSBrian Somers     if (result < dl->state)
17599b5f8ffdSBrian Somers       result = dl->state;
17609b5f8ffdSBrian Somers 
17619b5f8ffdSBrian Somers   return result;
17629b5f8ffdSBrian Somers }
1763991c2a7bSBrian Somers 
1764991c2a7bSBrian Somers int
1765991c2a7bSBrian Somers bundle_Exception(struct bundle *bundle, int fd)
1766991c2a7bSBrian Somers {
1767991c2a7bSBrian Somers   struct datalink *dl;
1768991c2a7bSBrian Somers 
1769991c2a7bSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
1770991c2a7bSBrian Somers     if (dl->physical->fd == fd) {
1771991c2a7bSBrian Somers       datalink_Down(dl, CLOSE_NORMAL);
1772991c2a7bSBrian Somers       return 1;
1773991c2a7bSBrian Somers     }
1774991c2a7bSBrian Somers 
1775991c2a7bSBrian Somers   return 0;
1776991c2a7bSBrian Somers }
17771d1fc017SBrian Somers 
17781d1fc017SBrian Somers void
177930949fd4SBrian Somers bundle_AdjustFilters(struct bundle *bundle, struct ncpaddr *local,
178030949fd4SBrian Somers                      struct ncpaddr *remote)
17811d1fc017SBrian Somers {
178230949fd4SBrian Somers   filter_AdjustAddr(&bundle->filter.in, local, remote, NULL);
178330949fd4SBrian Somers   filter_AdjustAddr(&bundle->filter.out, local, remote, NULL);
178430949fd4SBrian Somers   filter_AdjustAddr(&bundle->filter.dial, local, remote, NULL);
178530949fd4SBrian Somers   filter_AdjustAddr(&bundle->filter.alive, local, remote, NULL);
1786d568d6c4SBrian Somers }
1787d568d6c4SBrian Somers 
1788d568d6c4SBrian Somers void
178930949fd4SBrian Somers bundle_AdjustDNS(struct bundle *bundle)
1790d568d6c4SBrian Somers {
179130949fd4SBrian Somers   struct in_addr *dns = bundle->ncp.ipcp.ns.dns;
179230949fd4SBrian Somers 
1793d568d6c4SBrian Somers   filter_AdjustAddr(&bundle->filter.in, NULL, NULL, dns);
1794d568d6c4SBrian Somers   filter_AdjustAddr(&bundle->filter.out, NULL, NULL, dns);
1795d568d6c4SBrian Somers   filter_AdjustAddr(&bundle->filter.dial, NULL, NULL, dns);
1796d568d6c4SBrian Somers   filter_AdjustAddr(&bundle->filter.alive, NULL, NULL, dns);
17971d1fc017SBrian Somers }
1798ab2de065SBrian Somers 
1799ab2de065SBrian Somers void
1800ab2de065SBrian Somers bundle_CalculateBandwidth(struct bundle *bundle)
1801ab2de065SBrian Somers {
1802ab2de065SBrian Somers   struct datalink *dl;
18036301d506SBrian Somers   int sp, overhead, maxoverhead;
1804ab2de065SBrian Somers 
1805ab2de065SBrian Somers   bundle->bandwidth = 0;
1806c8b9fb53SBrian Somers   bundle->iface->mtu = 0;
18076301d506SBrian Somers   maxoverhead = 0;
18086301d506SBrian Somers 
18096301d506SBrian Somers   for (dl = bundle->links; dl; dl = dl->next) {
18106301d506SBrian Somers     overhead = ccp_MTUOverhead(&dl->physical->link.ccp);
18116301d506SBrian Somers     if (maxoverhead < overhead)
18126301d506SBrian Somers       maxoverhead = overhead;
1813ab2de065SBrian Somers     if (dl->state == DATALINK_OPEN) {
1814ab2de065SBrian Somers       if ((sp = dl->mp.bandwidth) == 0 &&
1815ab2de065SBrian Somers           (sp = physical_GetSpeed(dl->physical)) == 0)
1816ab2de065SBrian Somers         log_Printf(LogDEBUG, "%s: %s: Cannot determine bandwidth\n",
1817ab2de065SBrian Somers                    dl->name, dl->physical->name.full);
1818ab2de065SBrian Somers       else
1819ab2de065SBrian Somers         bundle->bandwidth += sp;
1820ab2de065SBrian Somers       if (!bundle->ncp.mp.active) {
1821c8b9fb53SBrian Somers         bundle->iface->mtu = dl->physical->link.lcp.his_mru;
1822ab2de065SBrian Somers         break;
1823ab2de065SBrian Somers       }
1824ab2de065SBrian Somers     }
18256301d506SBrian Somers   }
1826ab2de065SBrian Somers 
1827ab2de065SBrian Somers   if(bundle->bandwidth == 0)
1828ab2de065SBrian Somers     bundle->bandwidth = 115200;		/* Shrug */
1829ab2de065SBrian Somers 
18306301d506SBrian Somers   if (bundle->ncp.mp.active) {
1831c8b9fb53SBrian Somers     bundle->iface->mtu = bundle->ncp.mp.peer_mrru;
18326301d506SBrian Somers     overhead = ccp_MTUOverhead(&bundle->ncp.mp.link.ccp);
18336301d506SBrian Somers     if (maxoverhead < overhead)
18346301d506SBrian Somers       maxoverhead = overhead;
18356301d506SBrian Somers   } else if (!bundle->iface->mtu)
1836c8b9fb53SBrian Somers     bundle->iface->mtu = DEF_MRU;
1837ab2de065SBrian Somers 
1838ab2de065SBrian Somers #ifndef NORADIUS
183994d7be52SBrian Somers   if (bundle->radius.valid && bundle->radius.mtu &&
1840c8b9fb53SBrian Somers       bundle->radius.mtu < bundle->iface->mtu) {
1841ab2de065SBrian Somers     log_Printf(LogLCP, "Reducing MTU to radius value %lu\n",
1842ab2de065SBrian Somers                bundle->radius.mtu);
1843c8b9fb53SBrian Somers     bundle->iface->mtu = bundle->radius.mtu;
1844ab2de065SBrian Somers   }
1845ab2de065SBrian Somers #endif
1846ab2de065SBrian Somers 
18476301d506SBrian Somers   if (maxoverhead) {
18486301d506SBrian Somers     log_Printf(LogLCP, "Reducing MTU from %d to %d (CCP requirement)\n",
18496301d506SBrian Somers                bundle->iface->mtu, bundle->iface->mtu - maxoverhead);
18506301d506SBrian Somers     bundle->iface->mtu -= maxoverhead;
18516301d506SBrian Somers   }
18526301d506SBrian Somers 
185394d7be52SBrian Somers   tun_configure(bundle);
185403a2501aSBrian Somers 
185503a2501aSBrian Somers   route_UpdateMTU(bundle);
1856ab2de065SBrian Somers }
1857ab2de065SBrian Somers 
1858ab2de065SBrian Somers void
1859ab2de065SBrian Somers bundle_AutoAdjust(struct bundle *bundle, int percent, int what)
1860ab2de065SBrian Somers {
1861ab2de065SBrian Somers   struct datalink *dl, *choice, *otherlinkup;
1862ab2de065SBrian Somers 
1863ab2de065SBrian Somers   choice = otherlinkup = NULL;
1864ab2de065SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
1865ab2de065SBrian Somers     if (dl->physical->type == PHYS_AUTO) {
1866ab2de065SBrian Somers       if (dl->state == DATALINK_OPEN) {
1867ab2de065SBrian Somers         if (what == AUTO_DOWN) {
1868ab2de065SBrian Somers           if (choice)
1869ab2de065SBrian Somers             otherlinkup = choice;
1870ab2de065SBrian Somers           choice = dl;
1871ab2de065SBrian Somers         }
1872ab2de065SBrian Somers       } else if (dl->state == DATALINK_CLOSED) {
1873ab2de065SBrian Somers         if (what == AUTO_UP) {
1874ab2de065SBrian Somers           choice = dl;
1875ab2de065SBrian Somers           break;
1876ab2de065SBrian Somers         }
1877ab2de065SBrian Somers       } else {
1878ab2de065SBrian Somers         /* An auto link in an intermediate state - forget it for the moment */
1879ab2de065SBrian Somers         choice = NULL;
1880ab2de065SBrian Somers         break;
1881ab2de065SBrian Somers       }
1882ab2de065SBrian Somers     } else if (dl->state == DATALINK_OPEN && what == AUTO_DOWN)
1883ab2de065SBrian Somers       otherlinkup = dl;
1884ab2de065SBrian Somers 
1885ab2de065SBrian Somers   if (choice) {
1886ab2de065SBrian Somers     if (what == AUTO_UP) {
1887ab2de065SBrian Somers       log_Printf(LogPHASE, "%d%% saturation -> Opening link ``%s''\n",
1888ab2de065SBrian Somers                  percent, choice->name);
1889ab2de065SBrian Somers       datalink_Up(choice, 1, 1);
1890a339e644SBrian Somers       mp_CheckAutoloadTimer(&bundle->ncp.mp);
1891ab2de065SBrian Somers     } else if (otherlinkup) {	/* Only bring the second-last link down */
1892ab2de065SBrian Somers       log_Printf(LogPHASE, "%d%% saturation -> Closing link ``%s''\n",
1893ab2de065SBrian Somers                  percent, choice->name);
1894d2f5232dSBrian Somers       datalink_Close(choice, CLOSE_STAYDOWN);
1895a339e644SBrian Somers       mp_CheckAutoloadTimer(&bundle->ncp.mp);
1896ab2de065SBrian Somers     }
1897ab2de065SBrian Somers   }
1898ab2de065SBrian Somers }
1899ab2de065SBrian Somers 
1900ab2de065SBrian Somers int
1901ab2de065SBrian Somers bundle_WantAutoloadTimer(struct bundle *bundle)
1902ab2de065SBrian Somers {
1903ab2de065SBrian Somers   struct datalink *dl;
1904ab2de065SBrian Somers   int autolink, opened;
1905ab2de065SBrian Somers 
1906ab2de065SBrian Somers   if (bundle->phase == PHASE_NETWORK) {
1907ab2de065SBrian Somers     for (autolink = opened = 0, dl = bundle->links; dl; dl = dl->next)
1908ab2de065SBrian Somers       if (dl->physical->type == PHYS_AUTO) {
1909ab2de065SBrian Somers         if (++autolink == 2 || (autolink == 1 && opened))
1910ab2de065SBrian Somers           /* Two auto links or one auto and one open in NETWORK phase */
1911ab2de065SBrian Somers           return 1;
1912ab2de065SBrian Somers       } else if (dl->state == DATALINK_OPEN) {
1913ab2de065SBrian Somers         opened++;
1914ab2de065SBrian Somers         if (autolink)
1915ab2de065SBrian Somers           /* One auto and one open link in NETWORK phase */
1916ab2de065SBrian Somers           return 1;
1917ab2de065SBrian Somers       }
1918ab2de065SBrian Somers   }
1919ab2de065SBrian Somers 
1920ab2de065SBrian Somers   return 0;
1921ab2de065SBrian Somers }
192206b47306SBrian Somers 
192306b47306SBrian Somers void
192406b47306SBrian Somers bundle_ChangedPID(struct bundle *bundle)
192506b47306SBrian Somers {
192606b47306SBrian Somers #ifdef TUNSIFPID
192706b47306SBrian Somers   ioctl(bundle->dev.fd, TUNSIFPID, 0);
192806b47306SBrian Somers #endif
192906b47306SBrian Somers }
193046df5aa7SBrian Somers 
193146df5aa7SBrian Somers int
193246df5aa7SBrian Somers bundle_Uptime(struct bundle *bundle)
193346df5aa7SBrian Somers {
193446df5aa7SBrian Somers   if (bundle->upat)
193546df5aa7SBrian Somers     return time(NULL) - bundle->upat;
193646df5aa7SBrian Somers 
193746df5aa7SBrian Somers   return 0;
193846df5aa7SBrian Somers }
1939