xref: /freebsd/usr.sbin/ppp/bundle.c (revision d2f5232de34632802c54b6be197313ec4fc1ceaa)
17a6f8720SBrian Somers /*-
27a6f8720SBrian Somers  * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
37a6f8720SBrian Somers  * All rights reserved.
47a6f8720SBrian Somers  *
57a6f8720SBrian Somers  * Redistribution and use in source and binary forms, with or without
67a6f8720SBrian Somers  * modification, are permitted provided that the following conditions
77a6f8720SBrian Somers  * are met:
87a6f8720SBrian Somers  * 1. Redistributions of source code must retain the above copyright
97a6f8720SBrian Somers  *    notice, this list of conditions and the following disclaimer.
107a6f8720SBrian Somers  * 2. Redistributions in binary form must reproduce the above copyright
117a6f8720SBrian Somers  *    notice, this list of conditions and the following disclaimer in the
127a6f8720SBrian Somers  *    documentation and/or other materials provided with the distribution.
137a6f8720SBrian Somers  *
147a6f8720SBrian Somers  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
157a6f8720SBrian Somers  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
167a6f8720SBrian Somers  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
177a6f8720SBrian Somers  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
187a6f8720SBrian Somers  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
197a6f8720SBrian Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
207a6f8720SBrian Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
217a6f8720SBrian Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
227a6f8720SBrian Somers  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
237a6f8720SBrian Somers  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
247a6f8720SBrian Somers  * SUCH DAMAGE.
257a6f8720SBrian Somers  *
2697d92980SPeter Wemm  * $FreeBSD$
277a6f8720SBrian Somers  */
287a6f8720SBrian Somers 
291384bd27SBrian Somers #include <sys/param.h>
307a6f8720SBrian Somers #include <sys/socket.h>
317a6f8720SBrian Somers #include <netinet/in.h>
327a6f8720SBrian Somers #include <net/if.h>
33eb6e5e05SBrian Somers #include <net/if_tun.h>		/* For TUNSIFMODE & TUNSLMODE */
347a6f8720SBrian Somers #include <arpa/inet.h>
357a6f8720SBrian Somers #include <net/route.h>
36eaa4df37SBrian Somers #include <netinet/in_systm.h>
37eaa4df37SBrian Somers #include <netinet/ip.h>
381fa665f5SBrian Somers #include <sys/un.h>
397a6f8720SBrian Somers 
407a6f8720SBrian Somers #include <errno.h>
417a6f8720SBrian Somers #include <fcntl.h>
4247723d29SBrian Somers #include <paths.h>
437a6f8720SBrian Somers #include <stdio.h>
446f384573SBrian Somers #include <stdlib.h>
457a6f8720SBrian Somers #include <string.h>
4696c9bb21SBrian Somers #include <sys/uio.h>
4754cd8e13SBrian Somers #include <sys/wait.h>
48fc3034caSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
49fc3034caSBrian Somers #include <sys/linker.h>
50fc3034caSBrian Somers #endif
517a6f8720SBrian Somers #include <termios.h>
527a6f8720SBrian Somers #include <unistd.h>
537a6f8720SBrian Somers 
545d9e6103SBrian Somers #include "layer.h"
55c9e11a11SBrian Somers #include "defs.h"
567a6f8720SBrian Somers #include "command.h"
577a6f8720SBrian Somers #include "mbuf.h"
587a6f8720SBrian Somers #include "log.h"
597a6f8720SBrian Somers #include "id.h"
607a6f8720SBrian Somers #include "timer.h"
617a6f8720SBrian Somers #include "fsm.h"
627a6f8720SBrian Somers #include "iplist.h"
63879ed6faSBrian Somers #include "lqr.h"
64455aabc3SBrian Somers #include "hdlc.h"
657a6f8720SBrian Somers #include "throughput.h"
66eaa4df37SBrian Somers #include "slcompress.h"
677a6f8720SBrian Somers #include "ipcp.h"
685ca5389aSBrian Somers #include "filter.h"
692f786681SBrian Somers #include "descriptor.h"
707a6f8720SBrian Somers #include "route.h"
717a6f8720SBrian Somers #include "lcp.h"
727a6f8720SBrian Somers #include "ccp.h"
733b0f8d2eSBrian Somers #include "link.h"
743b0f8d2eSBrian Somers #include "mp.h"
75972a1bcfSBrian Somers #ifndef NORADIUS
76972a1bcfSBrian Somers #include "radius.h"
77972a1bcfSBrian Somers #endif
783b0f8d2eSBrian Somers #include "bundle.h"
79455aabc3SBrian Somers #include "async.h"
80455aabc3SBrian Somers #include "physical.h"
81455aabc3SBrian Somers #include "auth.h"
825d9e6103SBrian Somers #include "proto.h"
83455aabc3SBrian Somers #include "chap.h"
84455aabc3SBrian Somers #include "tun.h"
8585b542cfSBrian Somers #include "prompt.h"
863006ec67SBrian Somers #include "chat.h"
8792b09558SBrian Somers #include "cbcp.h"
883006ec67SBrian Somers #include "datalink.h"
893006ec67SBrian Somers #include "ip.h"
908fa6ebe4SBrian Somers #include "iface.h"
917a6f8720SBrian Somers 
92ab2de065SBrian Somers #define SCATTER_SEGMENTS 6	/* version, datalink, name, physical,
93ab2de065SBrian Somers                                    throughput, device */
9496c9bb21SBrian Somers #define SOCKET_OVERHEAD	100	/* additional buffer space for large */
9596c9bb21SBrian Somers                                 /* {recv,send}msg() calls            */
9696c9bb21SBrian Somers 
9704eaa58cSBrian Somers static int bundle_RemainingIdleTime(struct bundle *);
9804eaa58cSBrian Somers 
99455aabc3SBrian Somers static const char *PhaseNames[] = {
100455aabc3SBrian Somers   "Dead", "Establish", "Authenticate", "Network", "Terminate"
101455aabc3SBrian Somers };
102455aabc3SBrian Somers 
103455aabc3SBrian Somers const char *
104455aabc3SBrian Somers bundle_PhaseName(struct bundle *bundle)
1057a6f8720SBrian Somers {
106455aabc3SBrian Somers   return bundle->phase <= PHASE_TERMINATE ?
107455aabc3SBrian Somers     PhaseNames[bundle->phase] : "unknown";
1087a6f8720SBrian Somers }
1097a6f8720SBrian Somers 
110455aabc3SBrian Somers void
1115563ebdeSBrian Somers bundle_NewPhase(struct bundle *bundle, u_int new)
112455aabc3SBrian Somers {
113aef795ccSBrian Somers   if (new == bundle->phase)
114aef795ccSBrian Somers     return;
115aef795ccSBrian Somers 
116e2ebb036SBrian Somers   if (new <= PHASE_TERMINATE)
117dd7e2610SBrian Somers     log_Printf(LogPHASE, "bundle: %s\n", PhaseNames[new]);
1187a6f8720SBrian Somers 
119455aabc3SBrian Somers   switch (new) {
120455aabc3SBrian Somers   case PHASE_DEAD:
1210f2f3eb3SBrian Somers     log_DisplayPrompts();
122455aabc3SBrian Somers     bundle->phase = new;
123455aabc3SBrian Somers     break;
124455aabc3SBrian Somers 
125455aabc3SBrian Somers   case PHASE_ESTABLISH:
126455aabc3SBrian Somers     bundle->phase = new;
127455aabc3SBrian Somers     break;
128455aabc3SBrian Somers 
129455aabc3SBrian Somers   case PHASE_AUTHENTICATE:
130455aabc3SBrian Somers     bundle->phase = new;
1310f2f3eb3SBrian Somers     log_DisplayPrompts();
132455aabc3SBrian Somers     break;
133455aabc3SBrian Somers 
134455aabc3SBrian Somers   case PHASE_NETWORK:
135dd7e2610SBrian Somers     fsm_Up(&bundle->ncp.ipcp.fsm);
136dd7e2610SBrian Somers     fsm_Open(&bundle->ncp.ipcp.fsm);
137673903ecSBrian Somers     bundle->phase = new;
1380f2f3eb3SBrian Somers     log_DisplayPrompts();
139673903ecSBrian Somers     break;
140455aabc3SBrian Somers 
141455aabc3SBrian Somers   case PHASE_TERMINATE:
142455aabc3SBrian Somers     bundle->phase = new;
143673903ecSBrian Somers     mp_Down(&bundle->ncp.mp);
1440f2f3eb3SBrian Somers     log_DisplayPrompts();
145455aabc3SBrian Somers     break;
1467a6f8720SBrian Somers   }
1477a6f8720SBrian Somers }
1487a6f8720SBrian Somers 
1496d666775SBrian Somers static void
1506d666775SBrian Somers bundle_LayerStart(void *v, struct fsm *fp)
1517a6f8720SBrian Somers {
1523006ec67SBrian Somers   /* The given FSM is about to start up ! */
1537a6f8720SBrian Somers }
1547a6f8720SBrian Somers 
1555cf4388bSBrian Somers 
1565cf4388bSBrian Somers static void
1575cf4388bSBrian Somers bundle_Notify(struct bundle *bundle, char c)
1585cf4388bSBrian Somers {
1595cf4388bSBrian Somers   if (bundle->notify.fd != -1) {
1605cf4388bSBrian Somers     if (write(bundle->notify.fd, &c, 1) == 1)
161dd7e2610SBrian Somers       log_Printf(LogPHASE, "Parent notified of success.\n");
1625cf4388bSBrian Somers     else
163dd7e2610SBrian Somers       log_Printf(LogPHASE, "Failed to notify parent of success.\n");
1645cf4388bSBrian Somers     close(bundle->notify.fd);
1655cf4388bSBrian Somers     bundle->notify.fd = -1;
1665cf4388bSBrian Somers   }
1675cf4388bSBrian Somers }
1683b0f8d2eSBrian Somers 
1696d666775SBrian Somers static void
1706f8e9f0aSBrian Somers bundle_ClearQueues(void *v)
1716f8e9f0aSBrian Somers {
1726f8e9f0aSBrian Somers   struct bundle *bundle = (struct bundle *)v;
1736f8e9f0aSBrian Somers   struct datalink *dl;
1746f8e9f0aSBrian Somers 
1756f8e9f0aSBrian Somers   log_Printf(LogPHASE, "Clearing choked output queue\n");
1766f8e9f0aSBrian Somers   timer_Stop(&bundle->choked.timer);
1776f8e9f0aSBrian Somers 
1786f8e9f0aSBrian Somers   /*
1796f8e9f0aSBrian Somers    * Emergency time:
1806f8e9f0aSBrian Somers    *
1816f8e9f0aSBrian Somers    * We've had a full queue for PACKET_DEL_SECS seconds without being
1826f8e9f0aSBrian Somers    * able to get rid of any of the packets.  We've probably given up
1836f8e9f0aSBrian Somers    * on the redials at this point, and the queued data has almost
1846f8e9f0aSBrian Somers    * definitely been timed out by the layer above.  As this is preventing
1856f8e9f0aSBrian Somers    * us from reading the TUN_NAME device (we don't want to buffer stuff
1866f8e9f0aSBrian Somers    * indefinitely), we may as well nuke this data and start with a clean
1876f8e9f0aSBrian Somers    * slate !
1886f8e9f0aSBrian Somers    *
1896f8e9f0aSBrian Somers    * Unfortunately, this has the side effect of shafting any compression
1906f8e9f0aSBrian Somers    * dictionaries in use (causing the relevant RESET_REQ/RESET_ACK).
1916f8e9f0aSBrian Somers    */
1926f8e9f0aSBrian Somers 
1935a72b6edSBrian Somers   ip_DeleteQueue(&bundle->ncp.ipcp);
1946f8e9f0aSBrian Somers   mp_DeleteQueue(&bundle->ncp.mp);
1956f8e9f0aSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
1966f8e9f0aSBrian Somers     physical_DeleteQueue(dl->physical);
1976f8e9f0aSBrian Somers }
1986f8e9f0aSBrian Somers 
1996f8e9f0aSBrian Somers static void
200ff0f9439SBrian Somers bundle_LinkAdded(struct bundle *bundle, struct datalink *dl)
201ff0f9439SBrian Somers {
202ff0f9439SBrian Somers   bundle->phys_type.all |= dl->physical->type;
203ff0f9439SBrian Somers   if (dl->state == DATALINK_OPEN)
204ff0f9439SBrian Somers     bundle->phys_type.open |= dl->physical->type;
205ff0f9439SBrian Somers 
206ff0f9439SBrian Somers   if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL))
207ff0f9439SBrian Somers       != bundle->phys_type.open && bundle->idle.timer.state == TIMER_STOPPED)
208ff0f9439SBrian Somers     /* We may need to start our idle timer */
209ff0f9439SBrian Somers     bundle_StartIdleTimer(bundle);
210ff0f9439SBrian Somers }
211ff0f9439SBrian Somers 
21292b09558SBrian Somers void
213ff0f9439SBrian Somers bundle_LinksRemoved(struct bundle *bundle)
214ff0f9439SBrian Somers {
215ff0f9439SBrian Somers   struct datalink *dl;
216ff0f9439SBrian Somers 
217ff0f9439SBrian Somers   bundle->phys_type.all = bundle->phys_type.open = 0;
218ff0f9439SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
219ff0f9439SBrian Somers     bundle_LinkAdded(bundle, dl);
220ff0f9439SBrian Somers 
221ab2de065SBrian Somers   bundle_CalculateBandwidth(bundle);
222ab2de065SBrian Somers   mp_CheckAutoloadTimer(&bundle->ncp.mp);
223ab2de065SBrian Somers 
224ff0f9439SBrian Somers   if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL))
225ff0f9439SBrian Somers       == bundle->phys_type.open)
226ff0f9439SBrian Somers     bundle_StopIdleTimer(bundle);
227ff0f9439SBrian Somers }
22804eaa58cSBrian Somers 
22904eaa58cSBrian Somers static void
2306f384573SBrian Somers bundle_LayerUp(void *v, struct fsm *fp)
2317a6f8720SBrian Somers {
2323006ec67SBrian Somers   /*
2333006ec67SBrian Somers    * The given fsm is now up
234ab2de065SBrian Somers    * If it's an LCP, adjust our phys_mode.open value and check the
235ab2de065SBrian Somers    * autoload timer.
236ab2de065SBrian Somers    * If it's the first NCP, calculate our bandwidth
237dade2407SBrian Somers    * If it's the first NCP, set our ``upat'' time
2383b0f8d2eSBrian Somers    * If it's the first NCP, start the idle timer.
239ab2de065SBrian Somers    * If it's an NCP, tell our -background parent to go away.
240ab2de065SBrian Somers    * If it's the first NCP, start the autoload timer
2413006ec67SBrian Somers    */
2426f384573SBrian Somers   struct bundle *bundle = (struct bundle *)v;
2436d666775SBrian Somers 
2445563ebdeSBrian Somers   if (fp->proto == PROTO_LCP) {
245ff0f9439SBrian Somers     struct physical *p = link2physical(fp->link);
246ff0f9439SBrian Somers 
247ff0f9439SBrian Somers     bundle_LinkAdded(bundle, p->dl);
248ab2de065SBrian Somers     mp_CheckAutoloadTimer(&bundle->ncp.mp);
2493b0f8d2eSBrian Somers   } else if (fp->proto == PROTO_IPCP) {
250ab2de065SBrian Somers     bundle_CalculateBandwidth(fp->bundle);
251dade2407SBrian Somers     time(&bundle->upat);
252ab886ad0SBrian Somers     bundle_StartIdleTimer(bundle);
2535cf4388bSBrian Somers     bundle_Notify(bundle, EX_NORMAL);
254ab2de065SBrian Somers     mp_CheckAutoloadTimer(&fp->bundle->ncp.mp);
2557a6f8720SBrian Somers   }
256ab886ad0SBrian Somers }
2577a6f8720SBrian Somers 
2586d666775SBrian Somers static void
2596d666775SBrian Somers bundle_LayerDown(void *v, struct fsm *fp)
2606d666775SBrian Somers {
2616d666775SBrian Somers   /*
2626d666775SBrian Somers    * The given FSM has been told to come down.
263ab886ad0SBrian Somers    * If it's our last NCP, stop the idle timer.
264dade2407SBrian Somers    * If it's our last NCP, clear our ``upat'' value.
265ab2de065SBrian Somers    * If it's our last NCP, stop the autoload timer
266ff0f9439SBrian Somers    * If it's an LCP, adjust our phys_type.open value and any timers.
26786b1f0d7SBrian Somers    * If it's an LCP and we're in multilink mode, adjust our tun
26886b1f0d7SBrian Somers    * speed and make sure our minimum sequence number is adjusted.
2696d666775SBrian Somers    */
270ab886ad0SBrian Somers 
271ab886ad0SBrian Somers   struct bundle *bundle = (struct bundle *)v;
272ab886ad0SBrian Somers 
273ab2de065SBrian Somers   if (fp->proto == PROTO_IPCP) {
274ab886ad0SBrian Somers     bundle_StopIdleTimer(bundle);
275dade2407SBrian Somers     bundle->upat = 0;
276ab2de065SBrian Somers     mp_StopAutoloadTimer(&bundle->ncp.mp);
277ab2de065SBrian Somers   } else if (fp->proto == PROTO_LCP) {
278ff0f9439SBrian Somers     bundle_LinksRemoved(bundle);  /* adjust timers & phys_type values */
279ff0f9439SBrian Somers     if (bundle->ncp.mp.active) {
2803b0f8d2eSBrian Somers       struct datalink *dl;
28186b1f0d7SBrian Somers       struct datalink *lost;
2823b0f8d2eSBrian Somers 
28386b1f0d7SBrian Somers       lost = NULL;
284faefde08SBrian Somers       for (dl = bundle->links; dl; dl = dl->next)
28586b1f0d7SBrian Somers         if (fp == &dl->physical->link.lcp.fsm)
28686b1f0d7SBrian Somers           lost = dl;
28786b1f0d7SBrian Somers 
288ab2de065SBrian Somers       bundle_CalculateBandwidth(bundle);
28986b1f0d7SBrian Somers 
29086b1f0d7SBrian Somers       if (lost)
29186b1f0d7SBrian Somers         mp_LinkLost(&bundle->ncp.mp, lost);
29286b1f0d7SBrian Somers       else
293a33b2ef7SBrian Somers         log_Printf(LogALERT, "Oops, lost an unrecognised datalink (%s) !\n",
29486b1f0d7SBrian Somers                    fp->link->name);
2953b0f8d2eSBrian Somers     }
2966d666775SBrian Somers   }
297ff0f9439SBrian Somers }
2986d666775SBrian Somers 
2996d666775SBrian Somers static void
3006d666775SBrian Somers bundle_LayerFinish(void *v, struct fsm *fp)
3016d666775SBrian Somers {
3026d666775SBrian Somers   /* The given fsm is now down (fp cannot be NULL)
3036d666775SBrian Somers    *
304dd7e2610SBrian Somers    * If it's the last LCP, fsm_Down all NCPs
305dd7e2610SBrian Somers    * If it's the last NCP, fsm_Close all LCPs
3066d666775SBrian Somers    */
3076d666775SBrian Somers 
3086d666775SBrian Somers   struct bundle *bundle = (struct bundle *)v;
3096d666775SBrian Somers   struct datalink *dl;
3106d666775SBrian Somers 
3113b0f8d2eSBrian Somers   if (fp->proto == PROTO_IPCP) {
31226afeaa2SBrian Somers     if (bundle_Phase(bundle) != PHASE_DEAD)
31325092092SBrian Somers       bundle_NewPhase(bundle, PHASE_TERMINATE);
3146d666775SBrian Somers     for (dl = bundle->links; dl; dl = dl->next)
3159c81b87dSBrian Somers       datalink_Close(dl, CLOSE_NORMAL);
31609206a6fSBrian Somers     fsm2initial(fp);
3173b0f8d2eSBrian Somers   } else if (fp->proto == PROTO_LCP) {
3183b0f8d2eSBrian Somers     int others_active;
319a611cad6SBrian Somers 
3203b0f8d2eSBrian Somers     others_active = 0;
3213b0f8d2eSBrian Somers     for (dl = bundle->links; dl; dl = dl->next)
3223b0f8d2eSBrian Somers       if (fp != &dl->physical->link.lcp.fsm &&
3233b0f8d2eSBrian Somers           dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP)
3243b0f8d2eSBrian Somers         others_active++;
3253b0f8d2eSBrian Somers 
32609206a6fSBrian Somers     if (!others_active)
32709206a6fSBrian Somers       fsm2initial(&bundle->ncp.ipcp.fsm);
3283b0f8d2eSBrian Somers   }
3293b0f8d2eSBrian Somers }
3306d666775SBrian Somers 
3317a6f8720SBrian Somers int
3327a6f8720SBrian Somers bundle_LinkIsUp(const struct bundle *bundle)
3337a6f8720SBrian Somers {
3345828db6dSBrian Somers   return bundle->ncp.ipcp.fsm.state == ST_OPENED;
3357a6f8720SBrian Somers }
3367a6f8720SBrian Somers 
3377a6f8720SBrian Somers void
3389c81b87dSBrian Somers bundle_Close(struct bundle *bundle, const char *name, int how)
3397a6f8720SBrian Somers {
340455aabc3SBrian Somers   /*
3413006ec67SBrian Somers    * Please close the given datalink.
342dd7e2610SBrian Somers    * If name == NULL or name is the last datalink, fsm_Close all NCPs
3436f384573SBrian Somers    * (except our MP)
3443b0f8d2eSBrian Somers    * If it isn't the last datalink, just Close that datalink.
345455aabc3SBrian Somers    */
3467a6f8720SBrian Somers 
3473b0f8d2eSBrian Somers   struct datalink *dl, *this_dl;
3483b0f8d2eSBrian Somers   int others_active;
3493006ec67SBrian Somers 
3503b0f8d2eSBrian Somers   others_active = 0;
3513b0f8d2eSBrian Somers   this_dl = NULL;
3523b0f8d2eSBrian Somers 
3533b0f8d2eSBrian Somers   for (dl = bundle->links; dl; dl = dl->next) {
3543b0f8d2eSBrian Somers     if (name && !strcasecmp(name, dl->name))
3553b0f8d2eSBrian Somers       this_dl = dl;
3563b0f8d2eSBrian Somers     if (name == NULL || this_dl == dl) {
3579c81b87dSBrian Somers       switch (how) {
3589c81b87dSBrian Somers         case CLOSE_LCP:
3599c81b87dSBrian Somers           datalink_DontHangup(dl);
3609c81b87dSBrian Somers           /* fall through */
3619c81b87dSBrian Somers         case CLOSE_STAYDOWN:
3623006ec67SBrian Somers           datalink_StayDown(dl);
3639c81b87dSBrian Somers           break;
3649c81b87dSBrian Somers       }
3653b0f8d2eSBrian Somers     } else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP)
3663b0f8d2eSBrian Somers       others_active++;
3673b0f8d2eSBrian Somers   }
3683b0f8d2eSBrian Somers 
3693b0f8d2eSBrian Somers   if (name && this_dl == NULL) {
370dd7e2610SBrian Somers     log_Printf(LogWARN, "%s: Invalid datalink name\n", name);
3713b0f8d2eSBrian Somers     return;
3723b0f8d2eSBrian Somers   }
3733b0f8d2eSBrian Somers 
3743b0f8d2eSBrian Somers   if (!others_active) {
37504eaa58cSBrian Somers     bundle_StopIdleTimer(bundle);
3763b0f8d2eSBrian Somers     if (bundle->ncp.ipcp.fsm.state > ST_CLOSED ||
3773b0f8d2eSBrian Somers         bundle->ncp.ipcp.fsm.state == ST_STARTING)
378dd7e2610SBrian Somers       fsm_Close(&bundle->ncp.ipcp.fsm);
3793b0f8d2eSBrian Somers     else {
38009206a6fSBrian Somers       fsm2initial(&bundle->ncp.ipcp.fsm);
381d345321bSBrian Somers       for (dl = bundle->links; dl; dl = dl->next)
3829c81b87dSBrian Somers         datalink_Close(dl, how);
3837a6f8720SBrian Somers     }
3843b0f8d2eSBrian Somers   } else if (this_dl && this_dl->state != DATALINK_CLOSED &&
3853b0f8d2eSBrian Somers              this_dl->state != DATALINK_HANGUP)
3869c81b87dSBrian Somers     datalink_Close(this_dl, how);
387d2fd8d77SBrian Somers }
3887a6f8720SBrian Somers 
3891bc9b5baSBrian Somers void
390899011c4SBrian Somers bundle_Down(struct bundle *bundle, int how)
3911bc9b5baSBrian Somers {
3921bc9b5baSBrian Somers   struct datalink *dl;
3931bc9b5baSBrian Somers 
3941bc9b5baSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
395899011c4SBrian Somers     datalink_Down(dl, how);
3961bc9b5baSBrian Somers }
3971bc9b5baSBrian Somers 
3982f786681SBrian Somers static int
3992f786681SBrian Somers bundle_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
4002f786681SBrian Somers {
4012f786681SBrian Somers   struct bundle *bundle = descriptor2bundle(d);
4022f786681SBrian Somers   struct datalink *dl;
403ab2de065SBrian Somers   int result, queued, nlinks;
4042f786681SBrian Somers 
4052f786681SBrian Somers   result = 0;
4062f786681SBrian Somers 
407078c562eSBrian Somers   /* If there are aren't many packets queued, look for some more. */
40804eaa58cSBrian Somers   for (nlinks = 0, dl = bundle->links; dl; dl = dl->next)
40904eaa58cSBrian Somers     nlinks++;
41004eaa58cSBrian Somers 
41104eaa58cSBrian Somers   if (nlinks) {
4125a72b6edSBrian Somers     queued = r ? bundle_FillQueues(bundle) : ip_QueueLen(&bundle->ncp.ipcp);
41304eaa58cSBrian Somers 
414ff0f9439SBrian Somers     if (r && (bundle->phase == PHASE_NETWORK ||
415ff0f9439SBrian Somers               bundle->phys_type.all & PHYS_AUTO)) {
41604eaa58cSBrian Somers       /* enough surplus so that we can tell if we're getting swamped */
417442f8495SBrian Somers       if (queued < 30) {
41804eaa58cSBrian Somers         /* Not enough - select() for more */
4196f8e9f0aSBrian Somers         if (bundle->choked.timer.state == TIMER_RUNNING)
4206f8e9f0aSBrian Somers           timer_Stop(&bundle->choked.timer);	/* Not needed any more */
42104eaa58cSBrian Somers         FD_SET(bundle->dev.fd, r);
422faefde08SBrian Somers         if (*n < bundle->dev.fd + 1)
423faefde08SBrian Somers           *n = bundle->dev.fd + 1;
4241384bd27SBrian Somers         log_Printf(LogTIMER, "%s: fdset(r) %d\n", TUN_NAME, bundle->dev.fd);
425078c562eSBrian Somers         result++;
4266f8e9f0aSBrian Somers       } else if (bundle->choked.timer.state == TIMER_STOPPED) {
4276f8e9f0aSBrian Somers         bundle->choked.timer.func = bundle_ClearQueues;
4286f8e9f0aSBrian Somers         bundle->choked.timer.name = "output choke";
4296f8e9f0aSBrian Somers         bundle->choked.timer.load = bundle->cfg.choked.timeout * SECTICKS;
4306f8e9f0aSBrian Somers         bundle->choked.timer.arg = bundle;
4316f8e9f0aSBrian Somers         timer_Start(&bundle->choked.timer);
432078c562eSBrian Somers       }
43304eaa58cSBrian Somers     }
43404eaa58cSBrian Somers   }
435078c562eSBrian Somers 
436f0cdd9c0SBrian Somers #ifndef NORADIUS
437f0cdd9c0SBrian Somers   result += descriptor_UpdateSet(&bundle->radius.desc, r, w, e, n);
438f0cdd9c0SBrian Somers #endif
439f0cdd9c0SBrian Somers 
44071555108SBrian Somers   /* Which links need a select() ? */
44171555108SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
44271555108SBrian Somers     result += descriptor_UpdateSet(&dl->desc, r, w, e, n);
44371555108SBrian Somers 
444ea722969SBrian Somers   /*
445ea722969SBrian Somers    * This *MUST* be called after the datalink UpdateSet()s as it
44604eaa58cSBrian Somers    * might be ``holding'' one of the datalinks (death-row) and
44704eaa58cSBrian Somers    * wants to be able to de-select() it from the descriptor set.
448ea722969SBrian Somers    */
4490f2f3eb3SBrian Somers   result += descriptor_UpdateSet(&bundle->ncp.mp.server.desc, r, w, e, n);
450ea722969SBrian Somers 
4512f786681SBrian Somers   return result;
4522f786681SBrian Somers }
4532f786681SBrian Somers 
4542f786681SBrian Somers static int
4552f786681SBrian Somers bundle_IsSet(struct descriptor *d, const fd_set *fdset)
4562f786681SBrian Somers {
4572f786681SBrian Somers   struct bundle *bundle = descriptor2bundle(d);
4582f786681SBrian Somers   struct datalink *dl;
4592f786681SBrian Somers 
4602f786681SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
4612f786681SBrian Somers     if (descriptor_IsSet(&dl->desc, fdset))
4622f786681SBrian Somers       return 1;
4632f786681SBrian Somers 
464f0cdd9c0SBrian Somers #ifndef NORADIUS
465f0cdd9c0SBrian Somers   if (descriptor_IsSet(&bundle->radius.desc, fdset))
466f0cdd9c0SBrian Somers     return 1;
467f0cdd9c0SBrian Somers #endif
468f0cdd9c0SBrian Somers 
469332b9de0SBrian Somers   if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset))
470332b9de0SBrian Somers     return 1;
471332b9de0SBrian Somers 
472faefde08SBrian Somers   return FD_ISSET(bundle->dev.fd, fdset);
4732f786681SBrian Somers }
4742f786681SBrian Somers 
4752f786681SBrian Somers static void
4762f786681SBrian Somers bundle_DescriptorRead(struct descriptor *d, struct bundle *bundle,
4772f786681SBrian Somers                       const fd_set *fdset)
4782f786681SBrian Somers {
4792f786681SBrian Somers   struct datalink *dl;
4802f786681SBrian Somers 
481ea722969SBrian Somers   if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset))
482ea722969SBrian Somers     descriptor_Read(&bundle->ncp.mp.server.desc, bundle, fdset);
483ea722969SBrian Somers 
4842f786681SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
4852f786681SBrian Somers     if (descriptor_IsSet(&dl->desc, fdset))
4862f786681SBrian Somers       descriptor_Read(&dl->desc, bundle, fdset);
487b6217683SBrian Somers 
488f0cdd9c0SBrian Somers #ifndef NORADIUS
489f0cdd9c0SBrian Somers   if (descriptor_IsSet(&bundle->radius.desc, fdset))
490f0cdd9c0SBrian Somers     descriptor_Read(&bundle->radius.desc, bundle, fdset);
491f0cdd9c0SBrian Somers #endif
492f0cdd9c0SBrian Somers 
493faefde08SBrian Somers   if (FD_ISSET(bundle->dev.fd, fdset)) {
494078c562eSBrian Somers     struct tun_data tun;
495078c562eSBrian Somers     int n, pri;
496078c562eSBrian Somers 
497078c562eSBrian Somers     /* something to read from tun */
498faefde08SBrian Somers     n = read(bundle->dev.fd, &tun, sizeof tun);
499078c562eSBrian Somers     if (n < 0) {
500a33b2ef7SBrian Somers       log_Printf(LogWARN, "read from %s: %s\n", TUN_NAME, strerror(errno));
501078c562eSBrian Somers       return;
502078c562eSBrian Somers     }
503078c562eSBrian Somers     n -= sizeof tun - sizeof tun.data;
504078c562eSBrian Somers     if (n <= 0) {
505a33b2ef7SBrian Somers       log_Printf(LogERROR, "read from %s: Only %d bytes read ?\n", TUN_NAME, n);
506078c562eSBrian Somers       return;
507078c562eSBrian Somers     }
508078c562eSBrian Somers     if (!tun_check_header(tun, AF_INET))
509078c562eSBrian Somers       return;
510078c562eSBrian Somers 
511078c562eSBrian Somers     if (((struct ip *)tun.data)->ip_dst.s_addr ==
512078c562eSBrian Somers         bundle->ncp.ipcp.my_ip.s_addr) {
513078c562eSBrian Somers       /* we've been asked to send something addressed *to* us :( */
514078c562eSBrian Somers       if (Enabled(bundle, OPT_LOOPBACK)) {
515078c562eSBrian Somers         pri = PacketCheck(bundle, tun.data, n, &bundle->filter.in);
516078c562eSBrian Somers         if (pri >= 0) {
5175d9e6103SBrian Somers           n += sizeof tun - sizeof tun.data;
5185d9e6103SBrian Somers           write(bundle->dev.fd, &tun, n);
519078c562eSBrian Somers           log_Printf(LogDEBUG, "Looped back packet addressed to myself\n");
520078c562eSBrian Somers         }
521078c562eSBrian Somers         return;
522078c562eSBrian Somers       } else
523078c562eSBrian Somers         log_Printf(LogDEBUG, "Oops - forwarding packet addressed to myself\n");
524078c562eSBrian Somers     }
525078c562eSBrian Somers 
526078c562eSBrian Somers     /*
527078c562eSBrian Somers      * Process on-demand dialup. Output packets are queued within tunnel
528078c562eSBrian Somers      * device until IPCP is opened.
529078c562eSBrian Somers      */
530078c562eSBrian Somers 
531078c562eSBrian Somers     if (bundle_Phase(bundle) == PHASE_DEAD) {
532078c562eSBrian Somers       /*
533078c562eSBrian Somers        * Note, we must be in AUTO mode :-/ otherwise our interface should
534078c562eSBrian Somers        * *not* be UP and we can't receive data
535078c562eSBrian Somers        */
536078c562eSBrian Somers       if ((pri = PacketCheck(bundle, tun.data, n, &bundle->filter.dial)) >= 0)
537ba23f397SBrian Somers         bundle_Open(bundle, NULL, PHYS_AUTO, 0);
538078c562eSBrian Somers       else
539078c562eSBrian Somers         /*
540078c562eSBrian Somers          * Drop the packet.  If we were to queue it, we'd just end up with
541078c562eSBrian Somers          * a pile of timed-out data in our output queue by the time we get
542078c562eSBrian Somers          * around to actually dialing.  We'd also prematurely reach the
543078c562eSBrian Somers          * threshold at which we stop select()ing to read() the tun
544078c562eSBrian Somers          * device - breaking auto-dial.
545078c562eSBrian Somers          */
546078c562eSBrian Somers         return;
547078c562eSBrian Somers     }
548078c562eSBrian Somers 
549078c562eSBrian Somers     pri = PacketCheck(bundle, tun.data, n, &bundle->filter.out);
5505d9e6103SBrian Somers     if (pri >= 0)
5515a72b6edSBrian Somers       ip_Enqueue(&bundle->ncp.ipcp, pri, tun.data, n);
552078c562eSBrian Somers   }
553078c562eSBrian Somers }
5542f786681SBrian Somers 
5551af29a6eSBrian Somers static int
5562f786681SBrian Somers bundle_DescriptorWrite(struct descriptor *d, struct bundle *bundle,
5572f786681SBrian Somers                        const fd_set *fdset)
5582f786681SBrian Somers {
5592f786681SBrian Somers   struct datalink *dl;
5601af29a6eSBrian Somers   int result = 0;
5612f786681SBrian Somers 
562ea722969SBrian Somers   /* This is not actually necessary as struct mpserver doesn't Write() */
563ea722969SBrian Somers   if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset))
564ea722969SBrian Somers     descriptor_Write(&bundle->ncp.mp.server.desc, bundle, fdset);
565ea722969SBrian Somers 
5662f786681SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
5672f786681SBrian Somers     if (descriptor_IsSet(&dl->desc, fdset))
5681af29a6eSBrian Somers       result += descriptor_Write(&dl->desc, bundle, fdset);
5691af29a6eSBrian Somers 
5701af29a6eSBrian Somers   return result;
5712f786681SBrian Somers }
5722f786681SBrian Somers 
573da66dd13SBrian Somers void
5741384bd27SBrian Somers bundle_LockTun(struct bundle *bundle)
5751384bd27SBrian Somers {
5761384bd27SBrian Somers   FILE *lockfile;
5771384bd27SBrian Somers   char pidfile[MAXPATHLEN];
5781384bd27SBrian Somers 
5791384bd27SBrian Somers   snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit);
5801384bd27SBrian Somers   lockfile = ID0fopen(pidfile, "w");
5811384bd27SBrian Somers   if (lockfile != NULL) {
5821384bd27SBrian Somers     fprintf(lockfile, "%d\n", (int)getpid());
5831384bd27SBrian Somers     fclose(lockfile);
5841384bd27SBrian Somers   }
5851384bd27SBrian Somers #ifndef RELEASE_CRUNCH
5861384bd27SBrian Somers   else
5871384bd27SBrian Somers     log_Printf(LogERROR, "Warning: Can't create %s: %s\n",
5881384bd27SBrian Somers                pidfile, strerror(errno));
5891384bd27SBrian Somers #endif
5901384bd27SBrian Somers }
5911384bd27SBrian Somers 
5921384bd27SBrian Somers static void
5931384bd27SBrian Somers bundle_UnlockTun(struct bundle *bundle)
5941384bd27SBrian Somers {
5951384bd27SBrian Somers   char pidfile[MAXPATHLEN];
5961384bd27SBrian Somers 
5971384bd27SBrian Somers   snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit);
5981384bd27SBrian Somers   ID0unlink(pidfile);
5991384bd27SBrian Somers }
6007a6f8720SBrian Somers 
6017a6f8720SBrian Somers struct bundle *
6028e7b8599SBrian Somers bundle_Create(const char *prefix, int type, const char **argv)
6037a6f8720SBrian Somers {
6047a6f8720SBrian Somers   static struct bundle bundle;		/* there can be only one */
6054e5196e9SBrian Somers   int enoentcount, err;
6064e5196e9SBrian Somers   const char *ifname;
607fc3034caSBrian Somers #ifdef KLDSYM_LOOKUP
608fc3034caSBrian Somers   int kldtried;
609fc3034caSBrian Somers #endif
6106ca65df0SBrian Somers #if defined(TUNSIFMODE) || defined(TUNSLMODE)
6110f203c7eSBrian Somers   int iff;
6120f203c7eSBrian Somers #endif
6137a6f8720SBrian Somers 
6148fa6ebe4SBrian Somers   if (bundle.iface != NULL) {	/* Already allocated ! */
615a33b2ef7SBrian Somers     log_Printf(LogALERT, "bundle_Create:  There's only one BUNDLE !\n");
6167a6f8720SBrian Somers     return NULL;
6177a6f8720SBrian Somers   }
6187a6f8720SBrian Somers 
6197a6f8720SBrian Somers   err = ENOENT;
6207a6f8720SBrian Somers   enoentcount = 0;
621fc3034caSBrian Somers #ifdef KLDSYM_LOOKUP
622fc3034caSBrian Somers   kldtried = 0;
623fc3034caSBrian Somers #endif
624107d62e7SBrian Somers   for (bundle.unit = 0; ; bundle.unit++) {
625faefde08SBrian Somers     snprintf(bundle.dev.Name, sizeof bundle.dev.Name, "%s%d",
626faefde08SBrian Somers              prefix, bundle.unit);
627faefde08SBrian Somers     bundle.dev.fd = ID0open(bundle.dev.Name, O_RDWR);
628faefde08SBrian Somers     if (bundle.dev.fd >= 0)
6297a6f8720SBrian Somers       break;
630107d62e7SBrian Somers     else if (errno == ENXIO) {
631fc3034caSBrian Somers #ifdef KLDSYM_LOOKUP
632fc3034caSBrian Somers       if (bundle.unit == 0 && !kldtried++) {
633fc3034caSBrian Somers         /*
634fc3034caSBrian Somers          * XXX:  For some odd reason, FreeBSD (right now) allows if_tun.ko to
635fc3034caSBrian Somers          *       load even when the kernel contains the tun device. This lookup
636fc3034caSBrian Somers          *       should go away when this is fixed, leaving just the kldload().
637fc3034caSBrian Somers          * Note also that kldsym() finds static symbols...
638fc3034caSBrian Somers          */
639fc3034caSBrian Somers         char devsw[] = "tun_cdevsw";
640fc3034caSBrian Somers         struct kld_sym_lookup ksl = { sizeof ksl, devsw, 0, 0 };
641fc3034caSBrian Somers 
642fc3034caSBrian Somers         if (kldsym(0, KLDSYM_LOOKUP, &ksl) == -1) {
643fc3034caSBrian Somers           if (ID0kldload("if_tun") != -1) {
644fc3034caSBrian Somers             bundle.unit--;
645fc3034caSBrian Somers             continue;
646fc3034caSBrian Somers           }
647fc3034caSBrian Somers           log_Printf(LogWARN, "kldload: if_tun: %s\n", strerror(errno));
648fc3034caSBrian Somers         }
649fc3034caSBrian Somers       }
650fc3034caSBrian Somers #endif
6517a6f8720SBrian Somers       err = errno;
652107d62e7SBrian Somers       break;
6537a6f8720SBrian Somers     } else if (errno == ENOENT) {
6547a6f8720SBrian Somers       if (++enoentcount > 2)
655107d62e7SBrian Somers 	break;
6567a6f8720SBrian Somers     } else
6577a6f8720SBrian Somers       err = errno;
6587a6f8720SBrian Somers   }
6597a6f8720SBrian Somers 
660faefde08SBrian Somers   if (bundle.dev.fd < 0) {
661dd7e2610SBrian Somers     log_Printf(LogWARN, "No available tunnel devices found (%s).\n",
66285b542cfSBrian Somers               strerror(err));
6637a6f8720SBrian Somers     return NULL;
6647a6f8720SBrian Somers   }
6657a6f8720SBrian Somers 
666dd7e2610SBrian Somers   log_SetTun(bundle.unit);
6678e7b8599SBrian Somers   bundle.argv = argv;
6680f781a72SBrian Somers   bundle.argv0 = argv[0];
6690f781a72SBrian Somers   bundle.argv1 = argv[1];
6707a6f8720SBrian Somers 
6718fa6ebe4SBrian Somers   ifname = strrchr(bundle.dev.Name, '/');
6728fa6ebe4SBrian Somers   if (ifname == NULL)
6738fa6ebe4SBrian Somers     ifname = bundle.dev.Name;
6747a6f8720SBrian Somers   else
6758fa6ebe4SBrian Somers     ifname++;
6768fa6ebe4SBrian Somers 
6778fa6ebe4SBrian Somers   bundle.iface = iface_Create(ifname);
6788fa6ebe4SBrian Somers   if (bundle.iface == NULL) {
6798fa6ebe4SBrian Somers     close(bundle.dev.fd);
6808fa6ebe4SBrian Somers     return NULL;
6818fa6ebe4SBrian Somers   }
6827a6f8720SBrian Somers 
6830f203c7eSBrian Somers #ifdef TUNSIFMODE
6840f203c7eSBrian Somers   /* Make sure we're POINTOPOINT */
6850f203c7eSBrian Somers   iff = IFF_POINTOPOINT;
6860f203c7eSBrian Somers   if (ID0ioctl(bundle.dev.fd, TUNSIFMODE, &iff) < 0)
6870f203c7eSBrian Somers     log_Printf(LogERROR, "bundle_Create: ioctl(TUNSIFMODE): %s\n",
6880f203c7eSBrian Somers 	       strerror(errno));
6890f203c7eSBrian Somers #endif
6900f203c7eSBrian Somers 
6916ca65df0SBrian Somers #ifdef TUNSLMODE
6926ca65df0SBrian Somers   /* Make sure we're POINTOPOINT */
6936ca65df0SBrian Somers   iff = 0;
6946ca65df0SBrian Somers   if (ID0ioctl(bundle.dev.fd, TUNSLMODE, &iff) < 0)
6956ca65df0SBrian Somers     log_Printf(LogERROR, "bundle_Create: ioctl(TUNSLMODE): %s\n",
6966ca65df0SBrian Somers 	       strerror(errno));
6976ca65df0SBrian Somers #endif
6986ca65df0SBrian Somers 
6994e5196e9SBrian Somers   if (!iface_SetFlags(bundle.iface, IFF_UP)) {
7008fa6ebe4SBrian Somers     iface_Destroy(bundle.iface);
7018fa6ebe4SBrian Somers     bundle.iface = NULL;
702faefde08SBrian Somers     close(bundle.dev.fd);
7037a6f8720SBrian Somers     return NULL;
7047a6f8720SBrian Somers   }
7057a6f8720SBrian Somers 
7068fa6ebe4SBrian Somers   log_Printf(LogPHASE, "Using interface: %s\n", ifname);
7077a6f8720SBrian Somers 
708ab2de065SBrian Somers   bundle.bandwidth = 0;
709820de6ebSBrian Somers   bundle.routing_seq = 0;
710a0cbd833SBrian Somers   bundle.phase = PHASE_DEAD;
711a0cbd833SBrian Somers   bundle.CleaningUp = 0;
71267b072f7SBrian Somers   bundle.NatEnabled = 0;
7137a6f8720SBrian Somers 
7146d666775SBrian Somers   bundle.fsm.LayerStart = bundle_LayerStart;
7156f384573SBrian Somers   bundle.fsm.LayerUp = bundle_LayerUp;
7166d666775SBrian Somers   bundle.fsm.LayerDown = bundle_LayerDown;
7176d666775SBrian Somers   bundle.fsm.LayerFinish = bundle_LayerFinish;
7186d666775SBrian Somers   bundle.fsm.object = &bundle;
7197a6f8720SBrian Somers 
720dade2407SBrian Somers   bundle.cfg.idle.timeout = NCP_IDLE_TIMEOUT;
721dade2407SBrian Somers   bundle.cfg.idle.min_timeout = 0;
7221342caedSBrian Somers   *bundle.cfg.auth.name = '\0';
7231342caedSBrian Somers   *bundle.cfg.auth.key = '\0';
724610b185fSBrian Somers   bundle.cfg.opt = OPT_SROUTES | OPT_IDCHECK | OPT_LOOPBACK |
725610b185fSBrian Somers                    OPT_THROUGHPUT | OPT_UTMP;
72649052c95SBrian Somers   *bundle.cfg.label = '\0';
72749052c95SBrian Somers   bundle.cfg.mtu = DEF_MTU;
7286f8e9f0aSBrian Somers   bundle.cfg.choked.timeout = CHOKED_TIMEOUT;
729ff0f9439SBrian Somers   bundle.phys_type.all = type;
730ff0f9439SBrian Somers   bundle.phys_type.open = 0;
731dade2407SBrian Somers   bundle.upat = 0;
732ab886ad0SBrian Somers 
7336f384573SBrian Somers   bundle.links = datalink_Create("deflink", &bundle, type);
7343006ec67SBrian Somers   if (bundle.links == NULL) {
735a33b2ef7SBrian Somers     log_Printf(LogALERT, "Cannot create data link: %s\n", strerror(errno));
7368fa6ebe4SBrian Somers     iface_Destroy(bundle.iface);
7378fa6ebe4SBrian Somers     bundle.iface = NULL;
738faefde08SBrian Somers     close(bundle.dev.fd);
7392289f246SBrian Somers     return NULL;
7402289f246SBrian Somers   }
7412289f246SBrian Somers 
7422f786681SBrian Somers   bundle.desc.type = BUNDLE_DESCRIPTOR;
7432f786681SBrian Somers   bundle.desc.UpdateSet = bundle_UpdateSet;
7442f786681SBrian Somers   bundle.desc.IsSet = bundle_IsSet;
7452f786681SBrian Somers   bundle.desc.Read = bundle_DescriptorRead;
7462f786681SBrian Somers   bundle.desc.Write = bundle_DescriptorWrite;
7472f786681SBrian Somers 
74849052c95SBrian Somers   mp_Init(&bundle.ncp.mp, &bundle);
74949052c95SBrian Somers 
75049052c95SBrian Somers   /* Send over the first physical link by default */
7515828db6dSBrian Somers   ipcp_Init(&bundle.ncp.ipcp, &bundle, &bundle.links->physical->link,
7525828db6dSBrian Somers             &bundle.fsm);
7536d666775SBrian Somers 
7545ca5389aSBrian Somers   memset(&bundle.filter, '\0', sizeof bundle.filter);
7555ca5389aSBrian Somers   bundle.filter.in.fragok = bundle.filter.in.logok = 1;
7565ca5389aSBrian Somers   bundle.filter.in.name = "IN";
7575ca5389aSBrian Somers   bundle.filter.out.fragok = bundle.filter.out.logok = 1;
7585ca5389aSBrian Somers   bundle.filter.out.name = "OUT";
7595ca5389aSBrian Somers   bundle.filter.dial.name = "DIAL";
7608390b576SBrian Somers   bundle.filter.dial.logok = 1;
7615ca5389aSBrian Somers   bundle.filter.alive.name = "ALIVE";
7625ca5389aSBrian Somers   bundle.filter.alive.logok = 1;
763cad7e742SBrian Somers   {
764cad7e742SBrian Somers     int	i;
765cad7e742SBrian Somers     for (i = 0; i < MAXFILTERS; i++) {
766cad7e742SBrian Somers         bundle.filter.in.rule[i].f_action = A_NONE;
767cad7e742SBrian Somers         bundle.filter.out.rule[i].f_action = A_NONE;
768cad7e742SBrian Somers         bundle.filter.dial.rule[i].f_action = A_NONE;
769cad7e742SBrian Somers         bundle.filter.alive.rule[i].f_action = A_NONE;
770cad7e742SBrian Somers     }
771cad7e742SBrian Somers   }
77293ee0ff2SBrian Somers   memset(&bundle.idle.timer, '\0', sizeof bundle.idle.timer);
77393ee0ff2SBrian Somers   bundle.idle.done = 0;
7745cf4388bSBrian Somers   bundle.notify.fd = -1;
7756f8e9f0aSBrian Somers   memset(&bundle.choked.timer, '\0', sizeof bundle.choked.timer);
776972a1bcfSBrian Somers #ifndef NORADIUS
777972a1bcfSBrian Somers   radius_Init(&bundle.radius);
778972a1bcfSBrian Somers #endif
77993ee0ff2SBrian Somers 
7806d666775SBrian Somers   /* Clean out any leftover crud */
7818fa6ebe4SBrian Somers   iface_Clear(bundle.iface, IFACE_CLEAR_ALL);
7826d666775SBrian Somers 
7831384bd27SBrian Somers   bundle_LockTun(&bundle);
7841384bd27SBrian Somers 
7857a6f8720SBrian Somers   return &bundle;
7867a6f8720SBrian Somers }
7877a6f8720SBrian Somers 
78868a0f0ccSBrian Somers static void
78968a0f0ccSBrian Somers bundle_DownInterface(struct bundle *bundle)
79068a0f0ccSBrian Somers {
791dd7e2610SBrian Somers   route_IfDelete(bundle, 1);
7924e5196e9SBrian Somers   iface_ClearFlags(bundle->iface, IFF_UP);
79368a0f0ccSBrian Somers }
79468a0f0ccSBrian Somers 
79568a0f0ccSBrian Somers void
79668a0f0ccSBrian Somers bundle_Destroy(struct bundle *bundle)
79768a0f0ccSBrian Somers {
7983006ec67SBrian Somers   struct datalink *dl;
799b6217683SBrian Somers 
800ea722969SBrian Somers   /*
80104eaa58cSBrian Somers    * Clean up the interface.  We don't need to timer_Stop()s, mp_Down(),
802ea722969SBrian Somers    * ipcp_CleanInterface() and bundle_DownInterface() unless we're getting
803ea722969SBrian Somers    * out under exceptional conditions such as a descriptor exception.
804ea722969SBrian Somers    */
80504eaa58cSBrian Somers   timer_Stop(&bundle->idle.timer);
8066f8e9f0aSBrian Somers   timer_Stop(&bundle->choked.timer);
80766f634b6SBrian Somers   mp_Down(&bundle->ncp.mp);
808dd7e2610SBrian Somers   ipcp_CleanInterface(&bundle->ncp.ipcp);
80968a0f0ccSBrian Somers   bundle_DownInterface(bundle);
8103006ec67SBrian Somers 
811972a1bcfSBrian Somers #ifndef NORADIUS
812972a1bcfSBrian Somers   /* Tell the radius server the bad news */
813972a1bcfSBrian Somers   radius_Destroy(&bundle->radius);
814972a1bcfSBrian Somers #endif
815972a1bcfSBrian Somers 
816ea722969SBrian Somers   /* Again, these are all DATALINK_CLOSED unless we're abending */
8173006ec67SBrian Somers   dl = bundle->links;
8183006ec67SBrian Somers   while (dl)
8193006ec67SBrian Somers     dl = datalink_Destroy(dl);
8203006ec67SBrian Somers 
821442f8495SBrian Somers   ipcp_Destroy(&bundle->ncp.ipcp);
822442f8495SBrian Somers 
8231384bd27SBrian Somers   close(bundle->dev.fd);
8241384bd27SBrian Somers   bundle_UnlockTun(bundle);
8251384bd27SBrian Somers 
826ea722969SBrian Somers   /* In case we never made PHASE_NETWORK */
8275cf4388bSBrian Somers   bundle_Notify(bundle, EX_ERRDEAD);
828b6217683SBrian Somers 
8298fa6ebe4SBrian Somers   iface_Destroy(bundle->iface);
8308fa6ebe4SBrian Somers   bundle->iface = NULL;
83168a0f0ccSBrian Somers }
83268a0f0ccSBrian Somers 
8337a6f8720SBrian Somers struct rtmsg {
8347a6f8720SBrian Somers   struct rt_msghdr m_rtm;
8357a6f8720SBrian Somers   char m_space[64];
8367a6f8720SBrian Somers };
8377a6f8720SBrian Somers 
838610b185fSBrian Somers int
839820de6ebSBrian Somers bundle_SetRoute(struct bundle *bundle, int cmd, struct in_addr dst,
8402062443fSBrian Somers                 struct in_addr gateway, struct in_addr mask, int bang, int ssh)
8417a6f8720SBrian Somers {
8427a6f8720SBrian Somers   struct rtmsg rtmes;
8437a6f8720SBrian Somers   int s, nb, wb;
8447a6f8720SBrian Somers   char *cp;
8457a6f8720SBrian Somers   const char *cmdstr;
8467a6f8720SBrian Somers   struct sockaddr_in rtdata;
847610b185fSBrian Somers   int result = 1;
8487a6f8720SBrian Somers 
8497a6f8720SBrian Somers   if (bang)
8507a6f8720SBrian Somers     cmdstr = (cmd == RTM_ADD ? "Add!" : "Delete!");
8517a6f8720SBrian Somers   else
8527a6f8720SBrian Somers     cmdstr = (cmd == RTM_ADD ? "Add" : "Delete");
8537a6f8720SBrian Somers   s = ID0socket(PF_ROUTE, SOCK_RAW, 0);
8547a6f8720SBrian Somers   if (s < 0) {
855dd7e2610SBrian Somers     log_Printf(LogERROR, "bundle_SetRoute: socket(): %s\n", strerror(errno));
856610b185fSBrian Somers     return result;
8577a6f8720SBrian Somers   }
8587a6f8720SBrian Somers   memset(&rtmes, '\0', sizeof rtmes);
8597a6f8720SBrian Somers   rtmes.m_rtm.rtm_version = RTM_VERSION;
8607a6f8720SBrian Somers   rtmes.m_rtm.rtm_type = cmd;
8617a6f8720SBrian Somers   rtmes.m_rtm.rtm_addrs = RTA_DST;
862820de6ebSBrian Somers   rtmes.m_rtm.rtm_seq = ++bundle->routing_seq;
8637a6f8720SBrian Somers   rtmes.m_rtm.rtm_pid = getpid();
8647a6f8720SBrian Somers   rtmes.m_rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;
8657a6f8720SBrian Somers 
8663afe5ccbSBrian Somers   if (cmd == RTM_ADD || cmd == RTM_CHANGE) {
8673afe5ccbSBrian Somers     if (bundle->ncp.ipcp.cfg.sendpipe > 0) {
8683afe5ccbSBrian Somers       rtmes.m_rtm.rtm_rmx.rmx_sendpipe = bundle->ncp.ipcp.cfg.sendpipe;
8693afe5ccbSBrian Somers       rtmes.m_rtm.rtm_inits |= RTV_SPIPE;
8703afe5ccbSBrian Somers     }
8713afe5ccbSBrian Somers     if (bundle->ncp.ipcp.cfg.recvpipe > 0) {
8723afe5ccbSBrian Somers       rtmes.m_rtm.rtm_rmx.rmx_recvpipe = bundle->ncp.ipcp.cfg.recvpipe;
8733afe5ccbSBrian Somers       rtmes.m_rtm.rtm_inits |= RTV_RPIPE;
8743afe5ccbSBrian Somers     }
8753afe5ccbSBrian Somers   }
8763afe5ccbSBrian Somers 
8777a6f8720SBrian Somers   memset(&rtdata, '\0', sizeof rtdata);
87850e5c17dSBrian Somers   rtdata.sin_len = sizeof rtdata;
8797a6f8720SBrian Somers   rtdata.sin_family = AF_INET;
8807a6f8720SBrian Somers   rtdata.sin_port = 0;
8817a6f8720SBrian Somers   rtdata.sin_addr = dst;
8827a6f8720SBrian Somers 
8837a6f8720SBrian Somers   cp = rtmes.m_space;
88450e5c17dSBrian Somers   memcpy(cp, &rtdata, rtdata.sin_len);
88550e5c17dSBrian Somers   cp += rtdata.sin_len;
886e43ebac1SBrian Somers   if (cmd == RTM_ADD) {
8877a6f8720SBrian Somers     if (gateway.s_addr == INADDR_ANY) {
8880aa8aa17SBrian Somers       if (!ssh)
8898fa6ebe4SBrian Somers         log_Printf(LogERROR, "bundle_SetRoute: Cannot add a route with"
8908fa6ebe4SBrian Somers                    " destination 0.0.0.0\n");
8918fa6ebe4SBrian Somers       close(s);
8928fa6ebe4SBrian Somers       return result;
8937a6f8720SBrian Somers     } else {
8947a6f8720SBrian Somers       rtdata.sin_addr = gateway;
89550e5c17dSBrian Somers       memcpy(cp, &rtdata, rtdata.sin_len);
89650e5c17dSBrian Somers       cp += rtdata.sin_len;
8977a6f8720SBrian Somers       rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY;
8987a6f8720SBrian Somers     }
899e43ebac1SBrian Somers   }
9007a6f8720SBrian Somers 
9017a6f8720SBrian Somers   if (dst.s_addr == INADDR_ANY)
9027a6f8720SBrian Somers     mask.s_addr = INADDR_ANY;
9037a6f8720SBrian Somers 
9047a6f8720SBrian Somers   if (cmd == RTM_ADD || dst.s_addr == INADDR_ANY) {
9057a6f8720SBrian Somers     rtdata.sin_addr = mask;
90650e5c17dSBrian Somers     memcpy(cp, &rtdata, rtdata.sin_len);
90750e5c17dSBrian Somers     cp += rtdata.sin_len;
9087a6f8720SBrian Somers     rtmes.m_rtm.rtm_addrs |= RTA_NETMASK;
9097a6f8720SBrian Somers   }
9107a6f8720SBrian Somers 
9117a6f8720SBrian Somers   nb = cp - (char *) &rtmes;
9127a6f8720SBrian Somers   rtmes.m_rtm.rtm_msglen = nb;
9137a6f8720SBrian Somers   wb = ID0write(s, &rtmes, nb);
9147a6f8720SBrian Somers   if (wb < 0) {
915dd7e2610SBrian Somers     log_Printf(LogTCPIP, "bundle_SetRoute failure:\n");
916dd7e2610SBrian Somers     log_Printf(LogTCPIP, "bundle_SetRoute:  Cmd = %s\n", cmdstr);
917dd7e2610SBrian Somers     log_Printf(LogTCPIP, "bundle_SetRoute:  Dst = %s\n", inet_ntoa(dst));
918dd7e2610SBrian Somers     log_Printf(LogTCPIP, "bundle_SetRoute:  Gateway = %s\n", inet_ntoa(gateway));
919dd7e2610SBrian Somers     log_Printf(LogTCPIP, "bundle_SetRoute:  Mask = %s\n", inet_ntoa(mask));
9207a6f8720SBrian Somers failed:
9217a6f8720SBrian Somers     if (cmd == RTM_ADD && (rtmes.m_rtm.rtm_errno == EEXIST ||
922e43ebac1SBrian Somers                            (rtmes.m_rtm.rtm_errno == 0 && errno == EEXIST))) {
923610b185fSBrian Somers       if (!bang) {
924dd7e2610SBrian Somers         log_Printf(LogWARN, "Add route failed: %s already exists\n",
9253afe5ccbSBrian Somers 		  dst.s_addr == 0 ? "default" : inet_ntoa(dst));
926610b185fSBrian Somers         result = 0;	/* Don't add to our dynamic list */
927610b185fSBrian Somers       } else {
9287a6f8720SBrian Somers         rtmes.m_rtm.rtm_type = cmd = RTM_CHANGE;
9297a6f8720SBrian Somers         if ((wb = ID0write(s, &rtmes, nb)) < 0)
9307a6f8720SBrian Somers           goto failed;
9317a6f8720SBrian Somers       }
932e43ebac1SBrian Somers     } else if (cmd == RTM_DELETE &&
9337a6f8720SBrian Somers              (rtmes.m_rtm.rtm_errno == ESRCH ||
9347a6f8720SBrian Somers               (rtmes.m_rtm.rtm_errno == 0 && errno == ESRCH))) {
9357a6f8720SBrian Somers       if (!bang)
936dd7e2610SBrian Somers         log_Printf(LogWARN, "Del route failed: %s: Non-existent\n",
9377a6f8720SBrian Somers                   inet_ntoa(dst));
9382062443fSBrian Somers     } else if (rtmes.m_rtm.rtm_errno == 0) {
9392062443fSBrian Somers       if (!ssh || errno != ENETUNREACH)
940dd7e2610SBrian Somers         log_Printf(LogWARN, "%s route failed: %s: errno: %s\n", cmdstr,
9417a6f8720SBrian Somers                    inet_ntoa(dst), strerror(errno));
9422062443fSBrian Somers     } else
943dd7e2610SBrian Somers       log_Printf(LogWARN, "%s route failed: %s: %s\n",
9447a6f8720SBrian Somers 		 cmdstr, inet_ntoa(dst), strerror(rtmes.m_rtm.rtm_errno));
9457a6f8720SBrian Somers   }
946dd7e2610SBrian Somers   log_Printf(LogDEBUG, "wrote %d: cmd = %s, dst = %x, gateway = %x\n",
947fe3125a0SBrian Somers             wb, cmdstr, (unsigned)dst.s_addr, (unsigned)gateway.s_addr);
9487a6f8720SBrian Somers   close(s);
949610b185fSBrian Somers 
950610b185fSBrian Somers   return result;
9517a6f8720SBrian Somers }
95283d1af55SBrian Somers 
95383d1af55SBrian Somers void
9543006ec67SBrian Somers bundle_LinkClosed(struct bundle *bundle, struct datalink *dl)
9553006ec67SBrian Somers {
9563006ec67SBrian Somers   /*
9573006ec67SBrian Somers    * Our datalink has closed.
958ea722969SBrian Somers    * CleanDatalinks() (called from DoLoop()) will remove closed
95981358fa3SBrian Somers    * BACKGROUND and DIRECT links.
9603b0f8d2eSBrian Somers    * If it's the last data link, enter phase DEAD.
961ea722969SBrian Somers    *
962ea722969SBrian Somers    * NOTE: dl may not be in our list (bundle_SendDatalink()) !
9633006ec67SBrian Somers    */
9645b8b8060SBrian Somers 
9653b0f8d2eSBrian Somers   struct datalink *odl;
9663b0f8d2eSBrian Somers   int other_links;
9675b8b8060SBrian Somers 
968bf1d3ff6SBrian Somers   log_SetTtyCommandMode(dl);
969bf1d3ff6SBrian Somers 
9703b0f8d2eSBrian Somers   other_links = 0;
9713b0f8d2eSBrian Somers   for (odl = bundle->links; odl; odl = odl->next)
9723b0f8d2eSBrian Somers     if (odl != dl && odl->state != DATALINK_CLOSED)
9733b0f8d2eSBrian Somers       other_links++;
9743b0f8d2eSBrian Somers 
9753b0f8d2eSBrian Somers   if (!other_links) {
97681358fa3SBrian Somers     if (dl->physical->type != PHYS_AUTO)	/* Not in -auto mode */
97703704096SBrian Somers       bundle_DownInterface(bundle);
97809206a6fSBrian Somers     fsm2initial(&bundle->ncp.ipcp.fsm);
9795563ebdeSBrian Somers     bundle_NewPhase(bundle, PHASE_DEAD);
9800f2f3eb3SBrian Somers     bundle_StopIdleTimer(bundle);
981ab2de065SBrian Somers   }
982455aabc3SBrian Somers }
983455aabc3SBrian Somers 
984455aabc3SBrian Somers void
985ba23f397SBrian Somers bundle_Open(struct bundle *bundle, const char *name, int mask, int force)
9863006ec67SBrian Somers {
9873006ec67SBrian Somers   /*
9883006ec67SBrian Somers    * Please open the given datalink, or all if name == NULL
9893006ec67SBrian Somers    */
9903006ec67SBrian Somers   struct datalink *dl;
9913006ec67SBrian Somers 
9923006ec67SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
9933006ec67SBrian Somers     if (name == NULL || !strcasecmp(dl->name, name)) {
994ba23f397SBrian Somers       if ((mask & dl->physical->type) &&
995ba23f397SBrian Somers           (dl->state == DATALINK_CLOSED ||
996ba23f397SBrian Somers            (force && dl->state == DATALINK_OPENING &&
997c11e57a3SBrian Somers             dl->dial.timer.state == TIMER_RUNNING))) {
998c11e57a3SBrian Somers         if (force)	/* Ignore redial timeout ? */
999c11e57a3SBrian Somers           timer_Stop(&dl->dial.timer);
1000565e35e5SBrian Somers         datalink_Up(dl, 1, 1);
1001ab2de065SBrian Somers         if (mask & PHYS_AUTO)
1002ab2de065SBrian Somers           /* Only one AUTO link at a time */
100304eaa58cSBrian Somers           break;
100404eaa58cSBrian Somers       }
10053006ec67SBrian Somers       if (name != NULL)
10063006ec67SBrian Somers         break;
10073006ec67SBrian Somers     }
10083006ec67SBrian Somers }
10093006ec67SBrian Somers 
10103006ec67SBrian Somers struct datalink *
10113006ec67SBrian Somers bundle2datalink(struct bundle *bundle, const char *name)
10123006ec67SBrian Somers {
10133006ec67SBrian Somers   struct datalink *dl;
10143006ec67SBrian Somers 
10153006ec67SBrian Somers   if (name != NULL) {
10163006ec67SBrian Somers     for (dl = bundle->links; dl; dl = dl->next)
10173006ec67SBrian Somers       if (!strcasecmp(dl->name, name))
10183006ec67SBrian Somers         return dl;
10193006ec67SBrian Somers   } else if (bundle->links && !bundle->links->next)
10203006ec67SBrian Somers     return bundle->links;
10213006ec67SBrian Somers 
10223006ec67SBrian Somers   return NULL;
10233006ec67SBrian Somers }
10243006ec67SBrian Somers 
10253006ec67SBrian Somers int
10263006ec67SBrian Somers bundle_FillQueues(struct bundle *bundle)
10273006ec67SBrian Somers {
10283b0f8d2eSBrian Somers   int total;
10293b0f8d2eSBrian Somers 
10301bc9b5baSBrian Somers   if (bundle->ncp.mp.active)
10313b0f8d2eSBrian Somers     total = mp_FillQueues(bundle);
10321bc9b5baSBrian Somers   else {
10331bc9b5baSBrian Somers     struct datalink *dl;
10341bc9b5baSBrian Somers     int add;
10351bc9b5baSBrian Somers 
10361bc9b5baSBrian Somers     for (total = 0, dl = bundle->links; dl; dl = dl->next)
10371bc9b5baSBrian Somers       if (dl->state == DATALINK_OPEN) {
10381bc9b5baSBrian Somers         add = link_QueueLen(&dl->physical->link);
10391bc9b5baSBrian Somers         if (add == 0 && dl->physical->out == NULL)
10405d9e6103SBrian Somers           add = ip_PushPacket(&dl->physical->link, bundle);
10411bc9b5baSBrian Somers         total += add;
10421bc9b5baSBrian Somers       }
10433006ec67SBrian Somers   }
10443006ec67SBrian Somers 
10455a72b6edSBrian Somers   return total + ip_QueueLen(&bundle->ncp.ipcp);
10463006ec67SBrian Somers }
1047aef795ccSBrian Somers 
1048aef795ccSBrian Somers int
1049aef795ccSBrian Somers bundle_ShowLinks(struct cmdargs const *arg)
1050aef795ccSBrian Somers {
1051aef795ccSBrian Somers   struct datalink *dl;
1052ab2de065SBrian Somers   struct pppThroughput *t;
1053ab2de065SBrian Somers   int secs;
1054aef795ccSBrian Somers 
10559c53a7b1SBrian Somers   for (dl = arg->bundle->links; dl; dl = dl->next) {
1056d4156d00SBrian Somers     prompt_Printf(arg->prompt, "Name: %s [%s, %s]",
1057d4156d00SBrian Somers                   dl->name, mode2Nam(dl->physical->type), datalink_State(dl));
1058eeab6bf5SBrian Somers     if (dl->physical->link.throughput.rolling && dl->state == DATALINK_OPEN)
1059e531f89aSBrian Somers       prompt_Printf(arg->prompt, " bandwidth %d, %llu bps (%llu bytes/sec)",
1060ab2de065SBrian Somers                     dl->mp.bandwidth ? dl->mp.bandwidth :
1061ab2de065SBrian Somers                                        physical_GetSpeed(dl->physical),
1062ab2de065SBrian Somers                     dl->physical->link.throughput.OctetsPerSecond * 8,
10639c53a7b1SBrian Somers                     dl->physical->link.throughput.OctetsPerSecond);
10649c53a7b1SBrian Somers     prompt_Printf(arg->prompt, "\n");
10659c53a7b1SBrian Somers   }
1066aef795ccSBrian Somers 
1067ab2de065SBrian Somers   t = &arg->bundle->ncp.mp.link.throughput;
1068ab2de065SBrian Somers   secs = t->downtime ? 0 : throughput_uptime(t);
1069ab2de065SBrian Somers   if (secs > t->SamplePeriod)
1070ab2de065SBrian Somers     secs = t->SamplePeriod;
1071ab2de065SBrian Somers   if (secs)
1072e531f89aSBrian Somers     prompt_Printf(arg->prompt, "Currently averaging %llu bps (%llu bytes/sec)"
1073ab2de065SBrian Somers                   " over the last %d secs\n", t->OctetsPerSecond * 8,
1074ab2de065SBrian Somers                   t->OctetsPerSecond, secs);
1075ab2de065SBrian Somers 
1076aef795ccSBrian Somers   return 0;
1077aef795ccSBrian Somers }
1078ab886ad0SBrian Somers 
10791342caedSBrian Somers static const char *
10801342caedSBrian Somers optval(struct bundle *bundle, int bit)
10811342caedSBrian Somers {
10821342caedSBrian Somers   return (bundle->cfg.opt & bit) ? "enabled" : "disabled";
10831342caedSBrian Somers }
10841342caedSBrian Somers 
1085c08717dfSBrian Somers int
1086c08717dfSBrian Somers bundle_ShowStatus(struct cmdargs const *arg)
1087c08717dfSBrian Somers {
1088c08717dfSBrian Somers   int remaining;
1089c08717dfSBrian Somers 
1090c08717dfSBrian Somers   prompt_Printf(arg->prompt, "Phase %s\n", bundle_PhaseName(arg->bundle));
10910f781a72SBrian Somers   prompt_Printf(arg->prompt, " Title:         %s\n", arg->bundle->argv[0]);
1092faefde08SBrian Somers   prompt_Printf(arg->prompt, " Device:        %s\n", arg->bundle->dev.Name);
1093dade2407SBrian Somers   prompt_Printf(arg->prompt, " Interface:     %s @ %lubps",
1094ab2de065SBrian Somers                 arg->bundle->iface->name, arg->bundle->bandwidth);
1095c08717dfSBrian Somers 
1096dade2407SBrian Somers   if (arg->bundle->upat) {
1097dade2407SBrian Somers     int secs = time(NULL) - arg->bundle->upat;
1098dade2407SBrian Somers 
1099dade2407SBrian Somers     prompt_Printf(arg->prompt, ", up time %d:%02d:%02d", secs / 3600,
1100dade2407SBrian Somers                   (secs / 60) % 60, secs % 60);
1101dade2407SBrian Somers   }
1102dade2407SBrian Somers 
1103dade2407SBrian Somers   prompt_Printf(arg->prompt, "\n\nDefaults:\n");
1104643f4904SBrian Somers   prompt_Printf(arg->prompt, " Label:         %s\n", arg->bundle->cfg.label);
1105610b185fSBrian Somers   prompt_Printf(arg->prompt, " Auth name:     %s\n",
1106610b185fSBrian Somers                 arg->bundle->cfg.auth.name);
110704eaa58cSBrian Somers 
11086f8e9f0aSBrian Somers   prompt_Printf(arg->prompt, " Choked Timer:  %ds\n",
11096f8e9f0aSBrian Somers                 arg->bundle->cfg.choked.timeout);
1110972a1bcfSBrian Somers 
1111972a1bcfSBrian Somers #ifndef NORADIUS
1112972a1bcfSBrian Somers   radius_Show(&arg->bundle->radius, arg->prompt);
1113972a1bcfSBrian Somers #endif
1114972a1bcfSBrian Somers 
1115c08717dfSBrian Somers   prompt_Printf(arg->prompt, " Idle Timer:    ");
1116dade2407SBrian Somers   if (arg->bundle->cfg.idle.timeout) {
1117dade2407SBrian Somers     prompt_Printf(arg->prompt, "%ds", arg->bundle->cfg.idle.timeout);
1118dade2407SBrian Somers     if (arg->bundle->cfg.idle.min_timeout)
1119dade2407SBrian Somers       prompt_Printf(arg->prompt, ", min %ds",
1120dade2407SBrian Somers                     arg->bundle->cfg.idle.min_timeout);
1121c08717dfSBrian Somers     remaining = bundle_RemainingIdleTime(arg->bundle);
1122c08717dfSBrian Somers     if (remaining != -1)
1123c08717dfSBrian Somers       prompt_Printf(arg->prompt, " (%ds remaining)", remaining);
1124c08717dfSBrian Somers     prompt_Printf(arg->prompt, "\n");
1125c08717dfSBrian Somers   } else
1126c08717dfSBrian Somers     prompt_Printf(arg->prompt, "disabled\n");
1127ce828a6eSBrian Somers   prompt_Printf(arg->prompt, " MTU:           ");
1128ce828a6eSBrian Somers   if (arg->bundle->cfg.mtu)
1129ce828a6eSBrian Somers     prompt_Printf(arg->prompt, "%d\n", arg->bundle->cfg.mtu);
1130ce828a6eSBrian Somers   else
1131ce828a6eSBrian Somers     prompt_Printf(arg->prompt, "unspecified\n");
1132c08717dfSBrian Somers 
11333afe5ccbSBrian Somers   prompt_Printf(arg->prompt, " sendpipe:      ");
11343afe5ccbSBrian Somers   if (arg->bundle->ncp.ipcp.cfg.sendpipe > 0)
1135ab2de065SBrian Somers     prompt_Printf(arg->prompt, "%-20ld", arg->bundle->ncp.ipcp.cfg.sendpipe);
11363afe5ccbSBrian Somers   else
1137ab2de065SBrian Somers     prompt_Printf(arg->prompt, "unspecified         ");
11383afe5ccbSBrian Somers   prompt_Printf(arg->prompt, " recvpipe:      ");
11393afe5ccbSBrian Somers   if (arg->bundle->ncp.ipcp.cfg.recvpipe > 0)
11403afe5ccbSBrian Somers     prompt_Printf(arg->prompt, "%ld\n", arg->bundle->ncp.ipcp.cfg.recvpipe);
11413afe5ccbSBrian Somers   else
11423afe5ccbSBrian Somers     prompt_Printf(arg->prompt, "unspecified\n");
11433afe5ccbSBrian Somers 
1144ab2de065SBrian Somers   prompt_Printf(arg->prompt, " Sticky Routes: %-20.20s",
1145610b185fSBrian Somers                 optval(arg->bundle, OPT_SROUTES));
11461342caedSBrian Somers   prompt_Printf(arg->prompt, " ID check:      %s\n",
11471342caedSBrian Somers                 optval(arg->bundle, OPT_IDCHECK));
1148ab2de065SBrian Somers   prompt_Printf(arg->prompt, " Keep-Session:  %-20.20s",
1149ac685e31SBrian Somers                 optval(arg->bundle, OPT_KEEPSESSION));
11501342caedSBrian Somers   prompt_Printf(arg->prompt, " Loopback:      %s\n",
11511342caedSBrian Somers                 optval(arg->bundle, OPT_LOOPBACK));
1152ab2de065SBrian Somers   prompt_Printf(arg->prompt, " PasswdAuth:    %-20.20s",
11531342caedSBrian Somers                 optval(arg->bundle, OPT_PASSWDAUTH));
11541342caedSBrian Somers   prompt_Printf(arg->prompt, " Proxy:         %s\n",
11551342caedSBrian Somers                 optval(arg->bundle, OPT_PROXY));
1156ab2de065SBrian Somers   prompt_Printf(arg->prompt, " Proxyall:      %-20.20s",
11573afe5ccbSBrian Somers                 optval(arg->bundle, OPT_PROXYALL));
11581342caedSBrian Somers   prompt_Printf(arg->prompt, " Throughput:    %s\n",
11591342caedSBrian Somers                 optval(arg->bundle, OPT_THROUGHPUT));
1160ab2de065SBrian Somers   prompt_Printf(arg->prompt, " Utmp Logging:  %-20.20s",
11611342caedSBrian Somers                 optval(arg->bundle, OPT_UTMP));
11628fa6ebe4SBrian Somers   prompt_Printf(arg->prompt, " Iface-Alias:   %s\n",
11638fa6ebe4SBrian Somers                 optval(arg->bundle, OPT_IFACEALIAS));
11641342caedSBrian Somers 
1165c08717dfSBrian Somers   return 0;
1166c08717dfSBrian Somers }
1167c08717dfSBrian Somers 
1168ab886ad0SBrian Somers static void
1169ab886ad0SBrian Somers bundle_IdleTimeout(void *v)
1170ab886ad0SBrian Somers {
1171ab886ad0SBrian Somers   struct bundle *bundle = (struct bundle *)v;
1172ab886ad0SBrian Somers 
1173dd7e2610SBrian Somers   log_Printf(LogPHASE, "Idle timer expired.\n");
117404eaa58cSBrian Somers   bundle_StopIdleTimer(bundle);
11759c81b87dSBrian Somers   bundle_Close(bundle, NULL, CLOSE_STAYDOWN);
1176ab886ad0SBrian Somers }
1177ab886ad0SBrian Somers 
1178ab886ad0SBrian Somers /*
1179ab886ad0SBrian Somers  *  Start Idle timer. If timeout is reached, we call bundle_Close() to
1180ab886ad0SBrian Somers  *  close LCP and link.
1181ab886ad0SBrian Somers  */
1182ab886ad0SBrian Somers void
1183ab886ad0SBrian Somers bundle_StartIdleTimer(struct bundle *bundle)
1184ab886ad0SBrian Somers {
1185dd7e2610SBrian Somers   timer_Stop(&bundle->idle.timer);
1186ff0f9439SBrian Somers   if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) !=
1187dade2407SBrian Somers       bundle->phys_type.open && bundle->cfg.idle.timeout) {
1188dade2407SBrian Somers     int secs;
1189dade2407SBrian Somers 
1190dade2407SBrian Somers     secs = bundle->cfg.idle.timeout;
1191dade2407SBrian Somers     if (bundle->cfg.idle.min_timeout > secs && bundle->upat) {
1192dade2407SBrian Somers       int up = time(NULL) - bundle->upat;
1193dade2407SBrian Somers 
1194dade2407SBrian Somers       if ((long long)bundle->cfg.idle.min_timeout - up > (long long)secs)
1195dade2407SBrian Somers         secs = bundle->cfg.idle.min_timeout - up;
1196dade2407SBrian Somers     }
119793ee0ff2SBrian Somers     bundle->idle.timer.func = bundle_IdleTimeout;
11983b0f8d2eSBrian Somers     bundle->idle.timer.name = "idle";
1199dade2407SBrian Somers     bundle->idle.timer.load = secs * SECTICKS;
120093ee0ff2SBrian Somers     bundle->idle.timer.arg = bundle;
1201dd7e2610SBrian Somers     timer_Start(&bundle->idle.timer);
1202dade2407SBrian Somers     bundle->idle.done = time(NULL) + secs;
1203ab886ad0SBrian Somers   }
1204ab886ad0SBrian Somers }
1205ab886ad0SBrian Somers 
1206ab886ad0SBrian Somers void
1207dade2407SBrian Somers bundle_SetIdleTimer(struct bundle *bundle, int timeout, int min_timeout)
1208ab886ad0SBrian Somers {
1209dade2407SBrian Somers   bundle->cfg.idle.timeout = timeout;
1210dade2407SBrian Somers   if (min_timeout >= 0)
1211dade2407SBrian Somers     bundle->cfg.idle.min_timeout = min_timeout;
1212ab886ad0SBrian Somers   if (bundle_LinkIsUp(bundle))
1213ab886ad0SBrian Somers     bundle_StartIdleTimer(bundle);
1214ab886ad0SBrian Somers }
1215ab886ad0SBrian Somers 
1216ab886ad0SBrian Somers void
1217ab886ad0SBrian Somers bundle_StopIdleTimer(struct bundle *bundle)
1218ab886ad0SBrian Somers {
1219dd7e2610SBrian Somers   timer_Stop(&bundle->idle.timer);
12204a632c80SBrian Somers   bundle->idle.done = 0;
1221ab886ad0SBrian Somers }
1222ab886ad0SBrian Somers 
122304eaa58cSBrian Somers static int
1224ab886ad0SBrian Somers bundle_RemainingIdleTime(struct bundle *bundle)
1225ab886ad0SBrian Somers {
122693ee0ff2SBrian Somers   if (bundle->idle.done)
122793ee0ff2SBrian Somers     return bundle->idle.done - time(NULL);
1228ab886ad0SBrian Somers   return -1;
1229ab886ad0SBrian Somers }
12303b0f8d2eSBrian Somers 
12313b0f8d2eSBrian Somers int
12323b0f8d2eSBrian Somers bundle_IsDead(struct bundle *bundle)
12333b0f8d2eSBrian Somers {
12343b0f8d2eSBrian Somers   return !bundle->links || (bundle->phase == PHASE_DEAD && bundle->CleaningUp);
12353b0f8d2eSBrian Somers }
1236b6217683SBrian Somers 
123704eaa58cSBrian Somers static struct datalink *
123804eaa58cSBrian Somers bundle_DatalinkLinkout(struct bundle *bundle, struct datalink *dl)
1239cd7bd93aSBrian Somers {
1240cd7bd93aSBrian Somers   struct datalink **dlp;
1241cd7bd93aSBrian Somers 
1242cd7bd93aSBrian Somers   for (dlp = &bundle->links; *dlp; dlp = &(*dlp)->next)
1243cd7bd93aSBrian Somers     if (*dlp == dl) {
124404eaa58cSBrian Somers       *dlp = dl->next;
124504eaa58cSBrian Somers       dl->next = NULL;
124604eaa58cSBrian Somers       bundle_LinksRemoved(bundle);
124704eaa58cSBrian Somers       return dl;
1248cd7bd93aSBrian Somers     }
124904eaa58cSBrian Somers 
125004eaa58cSBrian Somers   return NULL;
125104eaa58cSBrian Somers }
125204eaa58cSBrian Somers 
125304eaa58cSBrian Somers static void
125404eaa58cSBrian Somers bundle_DatalinkLinkin(struct bundle *bundle, struct datalink *dl)
125504eaa58cSBrian Somers {
125604eaa58cSBrian Somers   struct datalink **dlp = &bundle->links;
125704eaa58cSBrian Somers 
125804eaa58cSBrian Somers   while (*dlp)
125904eaa58cSBrian Somers     dlp = &(*dlp)->next;
126004eaa58cSBrian Somers 
126104eaa58cSBrian Somers   *dlp = dl;
126204eaa58cSBrian Somers   dl->next = NULL;
126304eaa58cSBrian Somers 
126404eaa58cSBrian Somers   bundle_LinkAdded(bundle, dl);
1265ab2de065SBrian Somers   mp_CheckAutoloadTimer(&bundle->ncp.mp);
1266565e35e5SBrian Somers }
1267565e35e5SBrian Somers 
1268565e35e5SBrian Somers void
1269565e35e5SBrian Somers bundle_CleanDatalinks(struct bundle *bundle)
1270565e35e5SBrian Somers {
1271565e35e5SBrian Somers   struct datalink **dlp = &bundle->links;
127204eaa58cSBrian Somers   int found = 0;
1273565e35e5SBrian Somers 
1274565e35e5SBrian Somers   while (*dlp)
1275565e35e5SBrian Somers     if ((*dlp)->state == DATALINK_CLOSED &&
127681358fa3SBrian Somers         (*dlp)->physical->type & (PHYS_DIRECT|PHYS_BACKGROUND)) {
1277565e35e5SBrian Somers       *dlp = datalink_Destroy(*dlp);
127804eaa58cSBrian Somers       found++;
127904eaa58cSBrian Somers     } else
1280565e35e5SBrian Somers       dlp = &(*dlp)->next;
128104eaa58cSBrian Somers 
128204eaa58cSBrian Somers   if (found)
128304eaa58cSBrian Somers     bundle_LinksRemoved(bundle);
128404eaa58cSBrian Somers }
128504eaa58cSBrian Somers 
128604eaa58cSBrian Somers int
128704eaa58cSBrian Somers bundle_DatalinkClone(struct bundle *bundle, struct datalink *dl,
128804eaa58cSBrian Somers                      const char *name)
128904eaa58cSBrian Somers {
129004eaa58cSBrian Somers   if (bundle2datalink(bundle, name)) {
129104eaa58cSBrian Somers     log_Printf(LogWARN, "Clone: %s: name already exists\n", name);
129204eaa58cSBrian Somers     return 0;
129304eaa58cSBrian Somers   }
129404eaa58cSBrian Somers 
129504eaa58cSBrian Somers   bundle_DatalinkLinkin(bundle, datalink_Clone(dl, name));
129604eaa58cSBrian Somers   return 1;
129704eaa58cSBrian Somers }
129804eaa58cSBrian Somers 
129904eaa58cSBrian Somers void
130004eaa58cSBrian Somers bundle_DatalinkRemove(struct bundle *bundle, struct datalink *dl)
130104eaa58cSBrian Somers {
130204eaa58cSBrian Somers   dl = bundle_DatalinkLinkout(bundle, dl);
130304eaa58cSBrian Somers   if (dl)
130404eaa58cSBrian Somers     datalink_Destroy(dl);
1305cd7bd93aSBrian Somers }
130649052c95SBrian Somers 
130749052c95SBrian Somers void
130849052c95SBrian Somers bundle_SetLabel(struct bundle *bundle, const char *label)
130949052c95SBrian Somers {
131049052c95SBrian Somers   if (label)
131149052c95SBrian Somers     strncpy(bundle->cfg.label, label, sizeof bundle->cfg.label - 1);
131249052c95SBrian Somers   else
131349052c95SBrian Somers     *bundle->cfg.label = '\0';
131449052c95SBrian Somers }
131549052c95SBrian Somers 
131649052c95SBrian Somers const char *
131749052c95SBrian Somers bundle_GetLabel(struct bundle *bundle)
131849052c95SBrian Somers {
131949052c95SBrian Somers   return *bundle->cfg.label ? bundle->cfg.label : NULL;
132049052c95SBrian Somers }
13211fa665f5SBrian Somers 
13221fa665f5SBrian Somers void
132396c9bb21SBrian Somers bundle_ReceiveDatalink(struct bundle *bundle, int s, struct sockaddr_un *sun)
13241fa665f5SBrian Somers {
132596c9bb21SBrian Somers   char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int)];
132696c9bb21SBrian Somers   struct cmsghdr *cmsg = (struct cmsghdr *)cmsgbuf;
132796c9bb21SBrian Somers   struct msghdr msg;
132896c9bb21SBrian Somers   struct iovec iov[SCATTER_SEGMENTS];
1329b7c5748eSBrian Somers   struct datalink *dl;
133096c9bb21SBrian Somers   int niov, link_fd, expect, f;
133185fd273aSBrian Somers   pid_t pid;
13326f384573SBrian Somers 
1333dd7e2610SBrian Somers   log_Printf(LogPHASE, "Receiving datalink\n");
13346f384573SBrian Somers 
133596c9bb21SBrian Somers   /* Create our scatter/gather array */
133696c9bb21SBrian Somers   niov = 1;
133796c9bb21SBrian Somers   iov[0].iov_len = strlen(Version) + 1;
133896c9bb21SBrian Somers   iov[0].iov_base = (char *)malloc(iov[0].iov_len);
133985fd273aSBrian Somers   if (datalink2iov(NULL, iov, &niov, sizeof iov / sizeof *iov, 0) == -1) {
134054cd8e13SBrian Somers     close(s);
13416f384573SBrian Somers     return;
134254cd8e13SBrian Somers   }
13436f384573SBrian Somers 
134485fd273aSBrian Somers   pid = getpid();
134585fd273aSBrian Somers   write(s, &pid, sizeof pid);
134685fd273aSBrian Somers 
134796c9bb21SBrian Somers   for (f = expect = 0; f < niov; f++)
134896c9bb21SBrian Somers     expect += iov[f].iov_len;
134996c9bb21SBrian Somers 
135096c9bb21SBrian Somers   /* Set up our message */
135196c9bb21SBrian Somers   cmsg->cmsg_len = sizeof cmsgbuf;
135296c9bb21SBrian Somers   cmsg->cmsg_level = SOL_SOCKET;
135354cd8e13SBrian Somers   cmsg->cmsg_type = 0;
135496c9bb21SBrian Somers 
135596c9bb21SBrian Somers   memset(&msg, '\0', sizeof msg);
135696c9bb21SBrian Somers   msg.msg_name = (caddr_t)sun;
135796c9bb21SBrian Somers   msg.msg_namelen = sizeof *sun;
135896c9bb21SBrian Somers   msg.msg_iov = iov;
135996c9bb21SBrian Somers   msg.msg_iovlen = niov;
136096c9bb21SBrian Somers   msg.msg_control = cmsgbuf;
136196c9bb21SBrian Somers   msg.msg_controllen = sizeof cmsgbuf;
136296c9bb21SBrian Somers 
136396c9bb21SBrian Somers   log_Printf(LogDEBUG, "Expecting %d scatter/gather bytes\n", expect);
136496c9bb21SBrian Somers   f = expect + 100;
136596c9bb21SBrian Somers   setsockopt(s, SOL_SOCKET, SO_RCVBUF, &f, sizeof f);
136696c9bb21SBrian Somers   if ((f = recvmsg(s, &msg, MSG_WAITALL)) != expect) {
136796c9bb21SBrian Somers     if (f == -1)
136896c9bb21SBrian Somers       log_Printf(LogERROR, "Failed recvmsg: %s\n", strerror(errno));
136996c9bb21SBrian Somers     else
137096c9bb21SBrian Somers       log_Printf(LogERROR, "Failed recvmsg: Got %d, not %d\n", f, expect);
137196c9bb21SBrian Somers     while (niov--)
137296c9bb21SBrian Somers       free(iov[niov].iov_base);
137354cd8e13SBrian Somers     close(s);
137496c9bb21SBrian Somers     return;
137596c9bb21SBrian Somers   }
137696c9bb21SBrian Somers 
1377c0d9a877SBrian Somers   write(s, "!", 1);	/* ACK */
1378ad5b0e8bSBrian Somers   close(s);
1379c0d9a877SBrian Somers 
1380ad5b0e8bSBrian Somers   if (cmsg->cmsg_type != SCM_RIGHTS) {
1381ad5b0e8bSBrian Somers     log_Printf(LogERROR, "Recvmsg: no descriptor received !\n");
1382ad5b0e8bSBrian Somers     while (niov--)
1383ad5b0e8bSBrian Somers       free(iov[niov].iov_base);
1384ad5b0e8bSBrian Somers     return;
138554cd8e13SBrian Somers   }
138654cd8e13SBrian Somers 
1387ad5b0e8bSBrian Somers   /* We've successfully received an open file descriptor through our socket */
1388ad5b0e8bSBrian Somers   log_Printf(LogDEBUG, "Receiving device descriptor\n");
1389ad5b0e8bSBrian Somers   link_fd = *(int *)CMSG_DATA(cmsg);
1390ad5b0e8bSBrian Somers 
139196c9bb21SBrian Somers   if (strncmp(Version, iov[0].iov_base, iov[0].iov_len)) {
139296c9bb21SBrian Somers     log_Printf(LogWARN, "Cannot receive datalink, incorrect version"
139396c9bb21SBrian Somers                " (\"%.*s\", not \"%s\")\n", (int)iov[0].iov_len,
13947d81ddf5SBrian Somers                (char *)iov[0].iov_base, Version);
139596c9bb21SBrian Somers     close(link_fd);
139696c9bb21SBrian Somers     while (niov--)
139796c9bb21SBrian Somers       free(iov[niov].iov_base);
139896c9bb21SBrian Somers     return;
139996c9bb21SBrian Somers   }
140096c9bb21SBrian Somers 
140196c9bb21SBrian Somers   niov = 1;
1402b7c5748eSBrian Somers   dl = iov2datalink(bundle, iov, &niov, sizeof iov / sizeof *iov, link_fd);
1403b7c5748eSBrian Somers   if (dl) {
140404eaa58cSBrian Somers     bundle_DatalinkLinkin(bundle, dl);
1405b7c5748eSBrian Somers     datalink_AuthOk(dl);
1406ab2de065SBrian Somers     bundle_CalculateBandwidth(dl->bundle);
140796c9bb21SBrian Somers   } else
140896c9bb21SBrian Somers     close(link_fd);
140996c9bb21SBrian Somers 
141096c9bb21SBrian Somers   free(iov[0].iov_base);
14111fa665f5SBrian Somers }
14121fa665f5SBrian Somers 
14131fa665f5SBrian Somers void
141496c9bb21SBrian Somers bundle_SendDatalink(struct datalink *dl, int s, struct sockaddr_un *sun)
14151fa665f5SBrian Somers {
1416c0d9a877SBrian Somers   char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int)], ack;
141796c9bb21SBrian Somers   struct cmsghdr *cmsg = (struct cmsghdr *)cmsgbuf;
141896c9bb21SBrian Somers   struct msghdr msg;
141996c9bb21SBrian Somers   struct iovec iov[SCATTER_SEGMENTS];
14201384bd27SBrian Somers   int niov, link_fd, f, expect, newsid;
142185fd273aSBrian Somers   pid_t newpid;
14226f384573SBrian Somers 
1423dd7e2610SBrian Somers   log_Printf(LogPHASE, "Transmitting datalink %s\n", dl->name);
14246f384573SBrian Somers 
142504eaa58cSBrian Somers   bundle_LinkClosed(dl->bundle, dl);
14260f2f3eb3SBrian Somers   bundle_DatalinkLinkout(dl->bundle, dl);
1427ea722969SBrian Somers 
142896c9bb21SBrian Somers   /* Build our scatter/gather array */
142996c9bb21SBrian Somers   iov[0].iov_len = strlen(Version) + 1;
143096c9bb21SBrian Somers   iov[0].iov_base = strdup(Version);
143196c9bb21SBrian Somers   niov = 1;
14326f384573SBrian Somers 
143385fd273aSBrian Somers   read(s, &newpid, sizeof newpid);
143485fd273aSBrian Somers   link_fd = datalink2iov(dl, iov, &niov, sizeof iov / sizeof *iov, newpid);
14356f384573SBrian Somers 
14366f384573SBrian Somers   if (link_fd != -1) {
143796c9bb21SBrian Somers     memset(&msg, '\0', sizeof msg);
143854cd8e13SBrian Somers 
143996c9bb21SBrian Somers     msg.msg_name = (caddr_t)sun;
144096c9bb21SBrian Somers     msg.msg_namelen = sizeof *sun;
144196c9bb21SBrian Somers     msg.msg_iov = iov;
144296c9bb21SBrian Somers     msg.msg_iovlen = niov;
144354cd8e13SBrian Somers 
144454cd8e13SBrian Somers     cmsg->cmsg_len = sizeof cmsgbuf;
144554cd8e13SBrian Somers     cmsg->cmsg_level = SOL_SOCKET;
144654cd8e13SBrian Somers     cmsg->cmsg_type = SCM_RIGHTS;
144754cd8e13SBrian Somers     *(int *)CMSG_DATA(cmsg) = link_fd;
144896c9bb21SBrian Somers     msg.msg_control = cmsgbuf;
144996c9bb21SBrian Somers     msg.msg_controllen = sizeof cmsgbuf;
145047723d29SBrian Somers 
145196c9bb21SBrian Somers     for (f = expect = 0; f < niov; f++)
145296c9bb21SBrian Somers       expect += iov[f].iov_len;
145347723d29SBrian Somers 
145496c9bb21SBrian Somers     log_Printf(LogDEBUG, "Sending %d bytes in scatter/gather array\n", expect);
145547723d29SBrian Somers 
145696c9bb21SBrian Somers     f = expect + SOCKET_OVERHEAD;
145796c9bb21SBrian Somers     setsockopt(s, SOL_SOCKET, SO_SNDBUF, &f, sizeof f);
145896c9bb21SBrian Somers     if (sendmsg(s, &msg, 0) == -1)
145996c9bb21SBrian Somers       log_Printf(LogERROR, "Failed sendmsg: %s\n", strerror(errno));
1460c0d9a877SBrian Somers     /* We must get the ACK before closing the descriptor ! */
1461c0d9a877SBrian Somers     read(s, &ack, 1);
146254cd8e13SBrian Somers 
1463ac685e31SBrian Somers     newsid = Enabled(dl->bundle, OPT_KEEPSESSION) ||
1464ac685e31SBrian Somers              tcgetpgrp(link_fd) == getpgrp();
146596c9bb21SBrian Somers     close(link_fd);
14661384bd27SBrian Somers     if (newsid)
14671384bd27SBrian Somers       bundle_setsid(dl->bundle, 1);
146847723d29SBrian Somers   }
146985fd273aSBrian Somers   close(s);
147096c9bb21SBrian Somers 
147196c9bb21SBrian Somers   while (niov--)
147296c9bb21SBrian Somers     free(iov[niov].iov_base);
14731fa665f5SBrian Somers }
1474dd0645c5SBrian Somers 
1475dd0645c5SBrian Somers int
147658d55334SBrian Somers bundle_RenameDatalink(struct bundle *bundle, struct datalink *ndl,
147758d55334SBrian Somers                       const char *name)
147858d55334SBrian Somers {
147958d55334SBrian Somers   struct datalink *dl;
148058d55334SBrian Somers 
148158d55334SBrian Somers   if (!strcasecmp(ndl->name, name))
148258d55334SBrian Somers     return 1;
148358d55334SBrian Somers 
148458d55334SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
148558d55334SBrian Somers     if (!strcasecmp(dl->name, name))
148658d55334SBrian Somers       return 0;
148758d55334SBrian Somers 
148858d55334SBrian Somers   datalink_Rename(ndl, name);
148958d55334SBrian Somers   return 1;
149058d55334SBrian Somers }
149158d55334SBrian Somers 
149258d55334SBrian Somers int
1493dd0645c5SBrian Somers bundle_SetMode(struct bundle *bundle, struct datalink *dl, int mode)
1494dd0645c5SBrian Somers {
1495dd0645c5SBrian Somers   int omode;
1496dd0645c5SBrian Somers 
1497dd0645c5SBrian Somers   omode = dl->physical->type;
1498dd0645c5SBrian Somers   if (omode == mode)
1499dd0645c5SBrian Somers     return 1;
1500dd0645c5SBrian Somers 
1501ff0f9439SBrian Somers   if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO))
1502ff0f9439SBrian Somers     /* First auto link */
1503dd0645c5SBrian Somers     if (bundle->ncp.ipcp.peer_ip.s_addr == INADDR_ANY) {
1504ff0f9439SBrian Somers       log_Printf(LogWARN, "You must `set ifaddr' or `open' before"
1505ff0f9439SBrian Somers                  " changing mode to %s\n", mode2Nam(mode));
1506dd0645c5SBrian Somers       return 0;
1507dd0645c5SBrian Somers     }
1508dd0645c5SBrian Somers 
1509dd0645c5SBrian Somers   if (!datalink_SetMode(dl, mode))
1510dd0645c5SBrian Somers     return 0;
1511dd0645c5SBrian Somers 
1512ff0f9439SBrian Somers   if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO) &&
1513ff0f9439SBrian Somers       bundle->phase != PHASE_NETWORK)
1514ff0f9439SBrian Somers     /* First auto link, we need an interface */
1515dd0645c5SBrian Somers     ipcp_InterfaceUp(&bundle->ncp.ipcp);
1516dd0645c5SBrian Somers 
1517ab2de065SBrian Somers   /* Regenerate phys_type and adjust idle timer */
151804eaa58cSBrian Somers   bundle_LinksRemoved(bundle);
1519dd0645c5SBrian Somers 
1520dd0645c5SBrian Somers   return 1;
1521dd0645c5SBrian Somers }
15221384bd27SBrian Somers 
15231384bd27SBrian Somers void
15241384bd27SBrian Somers bundle_setsid(struct bundle *bundle, int holdsession)
15251384bd27SBrian Somers {
15261384bd27SBrian Somers   /*
15271384bd27SBrian Somers    * Lose the current session.  This means getting rid of our pid
15281384bd27SBrian Somers    * too so that the tty device will really go away, and any getty
15291384bd27SBrian Somers    * etc will be allowed to restart.
15301384bd27SBrian Somers    */
15311384bd27SBrian Somers   pid_t pid, orig;
15321384bd27SBrian Somers   int fds[2];
15331384bd27SBrian Somers   char done;
15341384bd27SBrian Somers   struct datalink *dl;
15351384bd27SBrian Somers 
15361384bd27SBrian Somers   orig = getpid();
15371384bd27SBrian Somers   if (pipe(fds) == -1) {
15381384bd27SBrian Somers     log_Printf(LogERROR, "pipe: %s\n", strerror(errno));
15391384bd27SBrian Somers     return;
15401384bd27SBrian Somers   }
15411384bd27SBrian Somers   switch ((pid = fork())) {
15421384bd27SBrian Somers     case -1:
15431384bd27SBrian Somers       log_Printf(LogERROR, "fork: %s\n", strerror(errno));
15441384bd27SBrian Somers       close(fds[0]);
15451384bd27SBrian Somers       close(fds[1]);
15461384bd27SBrian Somers       return;
15471384bd27SBrian Somers     case 0:
15481384bd27SBrian Somers       close(fds[1]);
15494be0e57dSBrian Somers       read(fds[0], &done, 1);		/* uu_locks are mine ! */
15504be0e57dSBrian Somers       close(fds[0]);
15511384bd27SBrian Somers       if (pipe(fds) == -1) {
15521384bd27SBrian Somers         log_Printf(LogERROR, "pipe(2): %s\n", strerror(errno));
15531384bd27SBrian Somers         return;
15541384bd27SBrian Somers       }
15551384bd27SBrian Somers       switch ((pid = fork())) {
15561384bd27SBrian Somers         case -1:
1557a33b2ef7SBrian Somers           log_Printf(LogERROR, "fork(2): %s\n", strerror(errno));
15581384bd27SBrian Somers           close(fds[0]);
15591384bd27SBrian Somers           close(fds[1]);
15601384bd27SBrian Somers           return;
15611384bd27SBrian Somers         case 0:
15621384bd27SBrian Somers           close(fds[1]);
15634be0e57dSBrian Somers           bundle_LockTun(bundle);	/* update pid */
15644be0e57dSBrian Somers           read(fds[0], &done, 1);	/* uu_locks are mine ! */
15654be0e57dSBrian Somers           close(fds[0]);
15661384bd27SBrian Somers           setsid();
15671384bd27SBrian Somers           log_Printf(LogPHASE, "%d -> %d: %s session control\n",
15681384bd27SBrian Somers                      (int)orig, (int)getpid(),
15691384bd27SBrian Somers                      holdsession ? "Passed" : "Dropped");
15707e778f13SBrian Somers           timer_InitService(0);		/* Start the Timer Service */
15711384bd27SBrian Somers           break;
15721384bd27SBrian Somers         default:
15734be0e57dSBrian Somers           close(fds[0]);
15745d9e6103SBrian Somers           /* Give away all our physical locks (to the final process) */
15751384bd27SBrian Somers           for (dl = bundle->links; dl; dl = dl->next)
15761384bd27SBrian Somers             if (dl->state != DATALINK_CLOSED)
15775d9e6103SBrian Somers               physical_ChangedPid(dl->physical, pid);
15784be0e57dSBrian Somers           write(fds[1], "!", 1);	/* done */
15794be0e57dSBrian Somers           close(fds[1]);
15801384bd27SBrian Somers           exit(0);
15811384bd27SBrian Somers           break;
15821384bd27SBrian Somers       }
15831384bd27SBrian Somers       break;
15841384bd27SBrian Somers     default:
15854be0e57dSBrian Somers       close(fds[0]);
15865d9e6103SBrian Somers       /* Give away all our physical locks (to the intermediate process) */
15871384bd27SBrian Somers       for (dl = bundle->links; dl; dl = dl->next)
15881384bd27SBrian Somers         if (dl->state != DATALINK_CLOSED)
15895d9e6103SBrian Somers           physical_ChangedPid(dl->physical, pid);
15904be0e57dSBrian Somers       write(fds[1], "!", 1);	/* done */
15914be0e57dSBrian Somers       close(fds[1]);
15921384bd27SBrian Somers       if (holdsession) {
15931384bd27SBrian Somers         int fd, status;
15941384bd27SBrian Somers 
15951384bd27SBrian Somers         timer_TermService();
15961384bd27SBrian Somers         signal(SIGPIPE, SIG_DFL);
15971384bd27SBrian Somers         signal(SIGALRM, SIG_DFL);
15981384bd27SBrian Somers         signal(SIGHUP, SIG_DFL);
15991384bd27SBrian Somers         signal(SIGTERM, SIG_DFL);
16001384bd27SBrian Somers         signal(SIGINT, SIG_DFL);
16011384bd27SBrian Somers         signal(SIGQUIT, SIG_DFL);
16021384bd27SBrian Somers         for (fd = getdtablesize(); fd >= 0; fd--)
16031384bd27SBrian Somers           close(fd);
16041384bd27SBrian Somers         setuid(geteuid());
16051384bd27SBrian Somers         /*
16061384bd27SBrian Somers          * Reap the intermediate process.  As we're not exiting but the
16071384bd27SBrian Somers          * intermediate is, we don't want it to become defunct.
16081384bd27SBrian Somers          */
16091384bd27SBrian Somers         waitpid(pid, &status, 0);
16108e7b8599SBrian Somers         /* Tweak our process arguments.... */
16118e7b8599SBrian Somers         bundle->argv[0] = "session owner";
16128e7b8599SBrian Somers         bundle->argv[1] = NULL;
16131384bd27SBrian Somers         /*
16141384bd27SBrian Somers          * Hang around for a HUP.  This should happen as soon as the
16151384bd27SBrian Somers          * ppp that we passed our ctty descriptor to closes it.
16161384bd27SBrian Somers          * NOTE: If this process dies, the passed descriptor becomes
16171384bd27SBrian Somers          *       invalid and will give a select() error by setting one
16181384bd27SBrian Somers          *       of the error fds, aborting the other ppp.  We don't
16191384bd27SBrian Somers          *       want that to happen !
16201384bd27SBrian Somers          */
16211384bd27SBrian Somers         pause();
16221384bd27SBrian Somers       }
16231384bd27SBrian Somers       exit(0);
16241384bd27SBrian Somers       break;
16251384bd27SBrian Somers   }
16261384bd27SBrian Somers }
16279b5f8ffdSBrian Somers 
16289b5f8ffdSBrian Somers int
16299b5f8ffdSBrian Somers bundle_HighestState(struct bundle *bundle)
16309b5f8ffdSBrian Somers {
16319b5f8ffdSBrian Somers   struct datalink *dl;
16329b5f8ffdSBrian Somers   int result = DATALINK_CLOSED;
16339b5f8ffdSBrian Somers 
16349b5f8ffdSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
16359b5f8ffdSBrian Somers     if (result < dl->state)
16369b5f8ffdSBrian Somers       result = dl->state;
16379b5f8ffdSBrian Somers 
16389b5f8ffdSBrian Somers   return result;
16399b5f8ffdSBrian Somers }
1640991c2a7bSBrian Somers 
1641991c2a7bSBrian Somers int
1642991c2a7bSBrian Somers bundle_Exception(struct bundle *bundle, int fd)
1643991c2a7bSBrian Somers {
1644991c2a7bSBrian Somers   struct datalink *dl;
1645991c2a7bSBrian Somers 
1646991c2a7bSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
1647991c2a7bSBrian Somers     if (dl->physical->fd == fd) {
1648991c2a7bSBrian Somers       datalink_Down(dl, CLOSE_NORMAL);
1649991c2a7bSBrian Somers       return 1;
1650991c2a7bSBrian Somers     }
1651991c2a7bSBrian Somers 
1652991c2a7bSBrian Somers   return 0;
1653991c2a7bSBrian Somers }
16541d1fc017SBrian Somers 
16551d1fc017SBrian Somers void
16561d1fc017SBrian Somers bundle_AdjustFilters(struct bundle *bundle, struct in_addr *my_ip,
16571d1fc017SBrian Somers                      struct in_addr *peer_ip)
16581d1fc017SBrian Somers {
16591d1fc017SBrian Somers   filter_AdjustAddr(&bundle->filter.in, my_ip, peer_ip);
16601d1fc017SBrian Somers   filter_AdjustAddr(&bundle->filter.out, my_ip, peer_ip);
16611d1fc017SBrian Somers   filter_AdjustAddr(&bundle->filter.dial, my_ip, peer_ip);
16621d1fc017SBrian Somers   filter_AdjustAddr(&bundle->filter.alive, my_ip, peer_ip);
16631d1fc017SBrian Somers }
1664ab2de065SBrian Somers 
1665ab2de065SBrian Somers void
1666ab2de065SBrian Somers bundle_CalculateBandwidth(struct bundle *bundle)
1667ab2de065SBrian Somers {
1668ab2de065SBrian Somers   struct datalink *dl;
1669ab2de065SBrian Somers   int mtu, sp;
1670ab2de065SBrian Somers 
1671ab2de065SBrian Somers   bundle->bandwidth = 0;
1672ab2de065SBrian Somers   mtu = 0;
1673ab2de065SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
1674ab2de065SBrian Somers     if (dl->state == DATALINK_OPEN) {
1675ab2de065SBrian Somers       if ((sp = dl->mp.bandwidth) == 0 &&
1676ab2de065SBrian Somers           (sp = physical_GetSpeed(dl->physical)) == 0)
1677ab2de065SBrian Somers         log_Printf(LogDEBUG, "%s: %s: Cannot determine bandwidth\n",
1678ab2de065SBrian Somers                    dl->name, dl->physical->name.full);
1679ab2de065SBrian Somers       else
1680ab2de065SBrian Somers         bundle->bandwidth += sp;
1681ab2de065SBrian Somers       if (!bundle->ncp.mp.active) {
1682ab2de065SBrian Somers         mtu = dl->physical->link.lcp.his_mru;
1683ab2de065SBrian Somers         break;
1684ab2de065SBrian Somers       }
1685ab2de065SBrian Somers     }
1686ab2de065SBrian Somers 
1687ab2de065SBrian Somers   if(bundle->bandwidth == 0)
1688ab2de065SBrian Somers     bundle->bandwidth = 115200;		/* Shrug */
1689ab2de065SBrian Somers 
1690ab2de065SBrian Somers   if (bundle->ncp.mp.active)
1691ab2de065SBrian Somers     mtu = bundle->ncp.mp.peer_mrru;
1692ab2de065SBrian Somers   else if (!mtu)
1693ab2de065SBrian Somers     mtu = 1500;
1694ab2de065SBrian Somers 
1695ab2de065SBrian Somers #ifndef NORADIUS
1696ab2de065SBrian Somers   if (bundle->radius.valid && bundle->radius.mtu && bundle->radius.mtu < mtu) {
1697ab2de065SBrian Somers     log_Printf(LogLCP, "Reducing MTU to radius value %lu\n",
1698ab2de065SBrian Somers                bundle->radius.mtu);
1699ab2de065SBrian Somers     mtu = bundle->radius.mtu;
1700ab2de065SBrian Somers   }
1701ab2de065SBrian Somers #endif
1702ab2de065SBrian Somers 
1703ab2de065SBrian Somers   tun_configure(bundle, mtu);
1704ab2de065SBrian Somers }
1705ab2de065SBrian Somers 
1706ab2de065SBrian Somers void
1707ab2de065SBrian Somers bundle_AutoAdjust(struct bundle *bundle, int percent, int what)
1708ab2de065SBrian Somers {
1709ab2de065SBrian Somers   struct datalink *dl, *choice, *otherlinkup;
1710ab2de065SBrian Somers 
1711ab2de065SBrian Somers   choice = otherlinkup = NULL;
1712ab2de065SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
1713ab2de065SBrian Somers     if (dl->physical->type == PHYS_AUTO) {
1714ab2de065SBrian Somers       if (dl->state == DATALINK_OPEN) {
1715ab2de065SBrian Somers         if (what == AUTO_DOWN) {
1716ab2de065SBrian Somers           if (choice)
1717ab2de065SBrian Somers             otherlinkup = choice;
1718ab2de065SBrian Somers           choice = dl;
1719ab2de065SBrian Somers         }
1720ab2de065SBrian Somers       } else if (dl->state == DATALINK_CLOSED) {
1721ab2de065SBrian Somers         if (what == AUTO_UP) {
1722ab2de065SBrian Somers           choice = dl;
1723ab2de065SBrian Somers           break;
1724ab2de065SBrian Somers         }
1725ab2de065SBrian Somers       } else {
1726ab2de065SBrian Somers         /* An auto link in an intermediate state - forget it for the moment */
1727ab2de065SBrian Somers         choice = NULL;
1728ab2de065SBrian Somers         break;
1729ab2de065SBrian Somers       }
1730ab2de065SBrian Somers     } else if (dl->state == DATALINK_OPEN && what == AUTO_DOWN)
1731ab2de065SBrian Somers       otherlinkup = dl;
1732ab2de065SBrian Somers 
1733ab2de065SBrian Somers   if (choice) {
1734ab2de065SBrian Somers     if (what == AUTO_UP) {
1735ab2de065SBrian Somers       log_Printf(LogPHASE, "%d%% saturation -> Opening link ``%s''\n",
1736ab2de065SBrian Somers                  percent, choice->name);
1737ab2de065SBrian Somers       datalink_Up(choice, 1, 1);
1738ab2de065SBrian Somers       mp_StopAutoloadTimer(&bundle->ncp.mp);
1739ab2de065SBrian Somers     } else if (otherlinkup) {	/* Only bring the second-last link down */
1740ab2de065SBrian Somers       log_Printf(LogPHASE, "%d%% saturation -> Closing link ``%s''\n",
1741ab2de065SBrian Somers                  percent, choice->name);
1742d2f5232dSBrian Somers       datalink_Close(choice, CLOSE_STAYDOWN);
1743ab2de065SBrian Somers       mp_StopAutoloadTimer(&bundle->ncp.mp);
1744ab2de065SBrian Somers     }
1745ab2de065SBrian Somers   }
1746ab2de065SBrian Somers }
1747ab2de065SBrian Somers 
1748ab2de065SBrian Somers int
1749ab2de065SBrian Somers bundle_WantAutoloadTimer(struct bundle *bundle)
1750ab2de065SBrian Somers {
1751ab2de065SBrian Somers   struct datalink *dl;
1752ab2de065SBrian Somers   int autolink, opened;
1753ab2de065SBrian Somers 
1754ab2de065SBrian Somers   if (bundle->phase == PHASE_NETWORK) {
1755ab2de065SBrian Somers     for (autolink = opened = 0, dl = bundle->links; dl; dl = dl->next)
1756ab2de065SBrian Somers       if (dl->physical->type == PHYS_AUTO) {
1757ab2de065SBrian Somers         if (++autolink == 2 || (autolink == 1 && opened))
1758ab2de065SBrian Somers           /* Two auto links or one auto and one open in NETWORK phase */
1759ab2de065SBrian Somers           return 1;
1760ab2de065SBrian Somers       } else if (dl->state == DATALINK_OPEN) {
1761ab2de065SBrian Somers         opened++;
1762ab2de065SBrian Somers         if (autolink)
1763ab2de065SBrian Somers           /* One auto and one open link in NETWORK phase */
1764ab2de065SBrian Somers           return 1;
1765ab2de065SBrian Somers       }
1766ab2de065SBrian Somers   }
1767ab2de065SBrian Somers 
1768ab2de065SBrian Somers   return 0;
1769ab2de065SBrian Somers }
1770