xref: /freebsd/usr.sbin/ppp/bundle.c (revision 669b99659d1c3621c829b402fdfeb8b25a4c6f0e)
17a6f8720SBrian Somers /*-
27a6f8720SBrian Somers  * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
37a6f8720SBrian Somers  * All rights reserved.
47a6f8720SBrian Somers  *
57a6f8720SBrian Somers  * Redistribution and use in source and binary forms, with or without
67a6f8720SBrian Somers  * modification, are permitted provided that the following conditions
77a6f8720SBrian Somers  * are met:
87a6f8720SBrian Somers  * 1. Redistributions of source code must retain the above copyright
97a6f8720SBrian Somers  *    notice, this list of conditions and the following disclaimer.
107a6f8720SBrian Somers  * 2. Redistributions in binary form must reproduce the above copyright
117a6f8720SBrian Somers  *    notice, this list of conditions and the following disclaimer in the
127a6f8720SBrian Somers  *    documentation and/or other materials provided with the distribution.
137a6f8720SBrian Somers  *
147a6f8720SBrian Somers  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
157a6f8720SBrian Somers  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
167a6f8720SBrian Somers  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
177a6f8720SBrian Somers  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
187a6f8720SBrian Somers  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
197a6f8720SBrian Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
207a6f8720SBrian Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
217a6f8720SBrian Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
227a6f8720SBrian Somers  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
237a6f8720SBrian Somers  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
247a6f8720SBrian Somers  * SUCH DAMAGE.
257a6f8720SBrian Somers  *
2697d92980SPeter Wemm  * $FreeBSD$
277a6f8720SBrian Somers  */
287a6f8720SBrian Somers 
291384bd27SBrian Somers #include <sys/param.h>
307a6f8720SBrian Somers #include <sys/socket.h>
317a6f8720SBrian Somers #include <netinet/in.h>
327a6f8720SBrian Somers #include <net/if.h>
333a7b6d76SBrian Somers #include <net/if_tun.h>		/* For TUNS* ioctls */
347a6f8720SBrian Somers #include <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>
42cf0a3940SBrian Somers #ifdef __OpenBSD__
43cf0a3940SBrian Somers #include <util.h>
44cf0a3940SBrian Somers #else
45cf0a3940SBrian Somers #include <libutil.h>
46cf0a3940SBrian Somers #endif
4747723d29SBrian Somers #include <paths.h>
487a6f8720SBrian Somers #include <stdio.h>
496f384573SBrian Somers #include <stdlib.h>
507a6f8720SBrian Somers #include <string.h>
5196c9bb21SBrian Somers #include <sys/uio.h>
5254cd8e13SBrian Somers #include <sys/wait.h>
53fc3034caSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
54fc3034caSBrian Somers #include <sys/linker.h>
55fdb4bb1bSBrian Somers #include <sys/module.h>
56fc3034caSBrian Somers #endif
577a6f8720SBrian Somers #include <termios.h>
587a6f8720SBrian Somers #include <unistd.h>
597a6f8720SBrian Somers 
605d9e6103SBrian Somers #include "layer.h"
61c9e11a11SBrian Somers #include "defs.h"
627a6f8720SBrian Somers #include "command.h"
637a6f8720SBrian Somers #include "mbuf.h"
647a6f8720SBrian Somers #include "log.h"
657a6f8720SBrian Somers #include "id.h"
667a6f8720SBrian Somers #include "timer.h"
677a6f8720SBrian Somers #include "fsm.h"
687a6f8720SBrian Somers #include "iplist.h"
69879ed6faSBrian Somers #include "lqr.h"
70455aabc3SBrian Somers #include "hdlc.h"
717a6f8720SBrian Somers #include "throughput.h"
72eaa4df37SBrian Somers #include "slcompress.h"
737a6f8720SBrian Somers #include "ipcp.h"
745ca5389aSBrian Somers #include "filter.h"
752f786681SBrian Somers #include "descriptor.h"
767a6f8720SBrian Somers #include "route.h"
777a6f8720SBrian Somers #include "lcp.h"
787a6f8720SBrian Somers #include "ccp.h"
793b0f8d2eSBrian Somers #include "link.h"
803b0f8d2eSBrian Somers #include "mp.h"
81972a1bcfSBrian Somers #ifndef NORADIUS
82972a1bcfSBrian Somers #include "radius.h"
83972a1bcfSBrian Somers #endif
843b0f8d2eSBrian Somers #include "bundle.h"
85455aabc3SBrian Somers #include "async.h"
86455aabc3SBrian Somers #include "physical.h"
87455aabc3SBrian Somers #include "auth.h"
885d9e6103SBrian Somers #include "proto.h"
89455aabc3SBrian Somers #include "chap.h"
90455aabc3SBrian Somers #include "tun.h"
9185b542cfSBrian Somers #include "prompt.h"
923006ec67SBrian Somers #include "chat.h"
9392b09558SBrian Somers #include "cbcp.h"
943006ec67SBrian Somers #include "datalink.h"
953006ec67SBrian Somers #include "ip.h"
968fa6ebe4SBrian Somers #include "iface.h"
977a6f8720SBrian Somers 
98ab2de065SBrian Somers #define SCATTER_SEGMENTS 6  /* version, datalink, name, physical,
99ab2de065SBrian Somers                                throughput, device                   */
10087c3786eSBrian Somers 
1012cb305afSBrian Somers #define SEND_MAXFD 3        /* Max file descriptors passed through
10287c3786eSBrian Somers                                the local domain socket              */
10396c9bb21SBrian Somers 
10404eaa58cSBrian Somers static int bundle_RemainingIdleTime(struct bundle *);
10504eaa58cSBrian Somers 
106182c898aSBrian Somers static const char * const PhaseNames[] = {
107455aabc3SBrian Somers   "Dead", "Establish", "Authenticate", "Network", "Terminate"
108455aabc3SBrian Somers };
109455aabc3SBrian Somers 
110455aabc3SBrian Somers const char *
111455aabc3SBrian Somers bundle_PhaseName(struct bundle *bundle)
1127a6f8720SBrian Somers {
113455aabc3SBrian Somers   return bundle->phase <= PHASE_TERMINATE ?
114455aabc3SBrian Somers     PhaseNames[bundle->phase] : "unknown";
1157a6f8720SBrian Somers }
1167a6f8720SBrian Somers 
117455aabc3SBrian Somers void
1185563ebdeSBrian Somers bundle_NewPhase(struct bundle *bundle, u_int new)
119455aabc3SBrian Somers {
120aef795ccSBrian Somers   if (new == bundle->phase)
121aef795ccSBrian Somers     return;
122aef795ccSBrian Somers 
123e2ebb036SBrian Somers   if (new <= PHASE_TERMINATE)
124dd7e2610SBrian Somers     log_Printf(LogPHASE, "bundle: %s\n", PhaseNames[new]);
1257a6f8720SBrian Somers 
126455aabc3SBrian Somers   switch (new) {
127455aabc3SBrian Somers   case PHASE_DEAD:
1280f2f3eb3SBrian Somers     log_DisplayPrompts();
129455aabc3SBrian Somers     bundle->phase = new;
130455aabc3SBrian Somers     break;
131455aabc3SBrian Somers 
132455aabc3SBrian Somers   case PHASE_ESTABLISH:
133455aabc3SBrian Somers     bundle->phase = new;
134455aabc3SBrian Somers     break;
135455aabc3SBrian Somers 
136455aabc3SBrian Somers   case PHASE_AUTHENTICATE:
137455aabc3SBrian Somers     bundle->phase = new;
1380f2f3eb3SBrian Somers     log_DisplayPrompts();
139455aabc3SBrian Somers     break;
140455aabc3SBrian Somers 
141455aabc3SBrian Somers   case PHASE_NETWORK:
142dd7e2610SBrian Somers     fsm_Up(&bundle->ncp.ipcp.fsm);
143dd7e2610SBrian Somers     fsm_Open(&bundle->ncp.ipcp.fsm);
144673903ecSBrian Somers     bundle->phase = new;
1450f2f3eb3SBrian Somers     log_DisplayPrompts();
146673903ecSBrian Somers     break;
147455aabc3SBrian Somers 
148455aabc3SBrian Somers   case PHASE_TERMINATE:
149455aabc3SBrian Somers     bundle->phase = new;
150673903ecSBrian Somers     mp_Down(&bundle->ncp.mp);
1510f2f3eb3SBrian Somers     log_DisplayPrompts();
152455aabc3SBrian Somers     break;
1537a6f8720SBrian Somers   }
1547a6f8720SBrian Somers }
1557a6f8720SBrian Somers 
1566d666775SBrian Somers static void
1576d666775SBrian Somers bundle_LayerStart(void *v, struct fsm *fp)
1587a6f8720SBrian Somers {
1593006ec67SBrian Somers   /* The given FSM is about to start up ! */
1607a6f8720SBrian Somers }
1617a6f8720SBrian Somers 
1625cf4388bSBrian Somers 
163b42135deSBrian Somers void
1645cf4388bSBrian Somers bundle_Notify(struct bundle *bundle, char c)
1655cf4388bSBrian Somers {
1665cf4388bSBrian Somers   if (bundle->notify.fd != -1) {
167b42135deSBrian Somers     int ret;
168b42135deSBrian Somers 
169b42135deSBrian Somers     ret = write(bundle->notify.fd, &c, 1);
170b42135deSBrian Somers     if (c != EX_REDIAL && c != EX_RECONNECT) {
171b42135deSBrian Somers       if (ret == 1)
172b42135deSBrian Somers         log_Printf(LogCHAT, "Parent notified of %s\n",
1735a83ad1eSBrian Somers                    c == EX_NORMAL ? "success" : "failure");
1745cf4388bSBrian Somers       else
175b42135deSBrian Somers         log_Printf(LogERROR, "Failed to notify parent of success\n");
1765cf4388bSBrian Somers       close(bundle->notify.fd);
1775cf4388bSBrian Somers       bundle->notify.fd = -1;
178b42135deSBrian Somers     } else if (ret == 1)
179b42135deSBrian Somers       log_Printf(LogCHAT, "Parent notified of %s\n", ex_desc(c));
180b42135deSBrian Somers     else
181b42135deSBrian Somers       log_Printf(LogERROR, "Failed to notify parent of %s\n", ex_desc(c));
1825cf4388bSBrian Somers   }
1835cf4388bSBrian Somers }
1843b0f8d2eSBrian Somers 
1856d666775SBrian Somers static void
1866f8e9f0aSBrian Somers bundle_ClearQueues(void *v)
1876f8e9f0aSBrian Somers {
1886f8e9f0aSBrian Somers   struct bundle *bundle = (struct bundle *)v;
1896f8e9f0aSBrian Somers   struct datalink *dl;
1906f8e9f0aSBrian Somers 
1916f8e9f0aSBrian Somers   log_Printf(LogPHASE, "Clearing choked output queue\n");
1926f8e9f0aSBrian Somers   timer_Stop(&bundle->choked.timer);
1936f8e9f0aSBrian Somers 
1946f8e9f0aSBrian Somers   /*
1956f8e9f0aSBrian Somers    * Emergency time:
1966f8e9f0aSBrian Somers    *
1976f8e9f0aSBrian Somers    * We've had a full queue for PACKET_DEL_SECS seconds without being
1986f8e9f0aSBrian Somers    * able to get rid of any of the packets.  We've probably given up
1996f8e9f0aSBrian Somers    * on the redials at this point, and the queued data has almost
2006f8e9f0aSBrian Somers    * definitely been timed out by the layer above.  As this is preventing
2016f8e9f0aSBrian Somers    * us from reading the TUN_NAME device (we don't want to buffer stuff
2026f8e9f0aSBrian Somers    * indefinitely), we may as well nuke this data and start with a clean
2036f8e9f0aSBrian Somers    * slate !
2046f8e9f0aSBrian Somers    *
2056f8e9f0aSBrian Somers    * Unfortunately, this has the side effect of shafting any compression
2066f8e9f0aSBrian Somers    * dictionaries in use (causing the relevant RESET_REQ/RESET_ACK).
2076f8e9f0aSBrian Somers    */
2086f8e9f0aSBrian Somers 
2095a72b6edSBrian Somers   ip_DeleteQueue(&bundle->ncp.ipcp);
2106f8e9f0aSBrian Somers   mp_DeleteQueue(&bundle->ncp.mp);
2116f8e9f0aSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
2126f8e9f0aSBrian Somers     physical_DeleteQueue(dl->physical);
2136f8e9f0aSBrian Somers }
2146f8e9f0aSBrian Somers 
2156f8e9f0aSBrian Somers static void
216ff0f9439SBrian Somers bundle_LinkAdded(struct bundle *bundle, struct datalink *dl)
217ff0f9439SBrian Somers {
218ff0f9439SBrian Somers   bundle->phys_type.all |= dl->physical->type;
219ff0f9439SBrian Somers   if (dl->state == DATALINK_OPEN)
220ff0f9439SBrian Somers     bundle->phys_type.open |= dl->physical->type;
221ff0f9439SBrian Somers 
222ff0f9439SBrian Somers   if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL))
223ff0f9439SBrian Somers       != bundle->phys_type.open && bundle->idle.timer.state == TIMER_STOPPED)
224ff0f9439SBrian Somers     /* We may need to start our idle timer */
225ff0f9439SBrian Somers     bundle_StartIdleTimer(bundle);
226ff0f9439SBrian Somers }
227ff0f9439SBrian Somers 
22892b09558SBrian Somers void
229ff0f9439SBrian Somers bundle_LinksRemoved(struct bundle *bundle)
230ff0f9439SBrian Somers {
231ff0f9439SBrian Somers   struct datalink *dl;
232ff0f9439SBrian Somers 
233ff0f9439SBrian Somers   bundle->phys_type.all = bundle->phys_type.open = 0;
234ff0f9439SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
235ff0f9439SBrian Somers     bundle_LinkAdded(bundle, dl);
236ff0f9439SBrian Somers 
237ab2de065SBrian Somers   bundle_CalculateBandwidth(bundle);
238ab2de065SBrian Somers   mp_CheckAutoloadTimer(&bundle->ncp.mp);
239ab2de065SBrian Somers 
240ff0f9439SBrian Somers   if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL))
241ff0f9439SBrian Somers       == bundle->phys_type.open)
242ff0f9439SBrian Somers     bundle_StopIdleTimer(bundle);
243ff0f9439SBrian Somers }
24404eaa58cSBrian Somers 
24504eaa58cSBrian Somers static void
2466f384573SBrian Somers bundle_LayerUp(void *v, struct fsm *fp)
2477a6f8720SBrian Somers {
2483006ec67SBrian Somers   /*
2493006ec67SBrian Somers    * The given fsm is now up
250ab2de065SBrian Somers    * If it's an LCP, adjust our phys_mode.open value and check the
251ab2de065SBrian Somers    * autoload timer.
252ab2de065SBrian Somers    * If it's the first NCP, calculate our bandwidth
253dade2407SBrian Somers    * If it's the first NCP, set our ``upat'' time
2543b0f8d2eSBrian Somers    * If it's the first NCP, start the idle timer.
255ab2de065SBrian Somers    * If it's an NCP, tell our -background parent to go away.
256ab2de065SBrian Somers    * If it's the first NCP, start the autoload timer
2573006ec67SBrian Somers    */
2586f384573SBrian Somers   struct bundle *bundle = (struct bundle *)v;
2596d666775SBrian Somers 
2605563ebdeSBrian Somers   if (fp->proto == PROTO_LCP) {
261ff0f9439SBrian Somers     struct physical *p = link2physical(fp->link);
262ff0f9439SBrian Somers 
263ff0f9439SBrian Somers     bundle_LinkAdded(bundle, p->dl);
264ab2de065SBrian Somers     mp_CheckAutoloadTimer(&bundle->ncp.mp);
2653b0f8d2eSBrian Somers   } else if (fp->proto == PROTO_IPCP) {
266ab2de065SBrian Somers     bundle_CalculateBandwidth(fp->bundle);
267dade2407SBrian Somers     time(&bundle->upat);
268ab886ad0SBrian Somers     bundle_StartIdleTimer(bundle);
2695cf4388bSBrian Somers     bundle_Notify(bundle, EX_NORMAL);
270ab2de065SBrian Somers     mp_CheckAutoloadTimer(&fp->bundle->ncp.mp);
2717a6f8720SBrian Somers   }
272ab886ad0SBrian Somers }
2737a6f8720SBrian Somers 
2746d666775SBrian Somers static void
2756d666775SBrian Somers bundle_LayerDown(void *v, struct fsm *fp)
2766d666775SBrian Somers {
2776d666775SBrian Somers   /*
2786d666775SBrian Somers    * The given FSM has been told to come down.
279ab886ad0SBrian Somers    * If it's our last NCP, stop the idle timer.
280dade2407SBrian Somers    * If it's our last NCP, clear our ``upat'' value.
281ab2de065SBrian Somers    * If it's our last NCP, stop the autoload timer
282ff0f9439SBrian Somers    * If it's an LCP, adjust our phys_type.open value and any timers.
28386b1f0d7SBrian Somers    * If it's an LCP and we're in multilink mode, adjust our tun
284c8f30703SBrian Somers    * If it's the last LCP, down all NCPs
28586b1f0d7SBrian Somers    * speed and make sure our minimum sequence number is adjusted.
2866d666775SBrian Somers    */
287ab886ad0SBrian Somers 
288ab886ad0SBrian Somers   struct bundle *bundle = (struct bundle *)v;
289ab886ad0SBrian Somers 
290ab2de065SBrian Somers   if (fp->proto == PROTO_IPCP) {
291ab886ad0SBrian Somers     bundle_StopIdleTimer(bundle);
292dade2407SBrian Somers     bundle->upat = 0;
293ab2de065SBrian Somers     mp_StopAutoloadTimer(&bundle->ncp.mp);
294ab2de065SBrian Somers   } else if (fp->proto == PROTO_LCP) {
2953b0f8d2eSBrian Somers     struct datalink *dl;
29686b1f0d7SBrian Somers     struct datalink *lost;
297c8f30703SBrian Somers     int others_active;
298c8f30703SBrian Somers 
299c8f30703SBrian Somers     bundle_LinksRemoved(bundle);  /* adjust timers & phys_type values */
3003b0f8d2eSBrian Somers 
30186b1f0d7SBrian Somers     lost = NULL;
302c8f30703SBrian Somers     others_active = 0;
303c8f30703SBrian Somers     for (dl = bundle->links; dl; dl = dl->next) {
30486b1f0d7SBrian Somers       if (fp == &dl->physical->link.lcp.fsm)
30586b1f0d7SBrian Somers         lost = dl;
306c8f30703SBrian Somers       else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP)
307c8f30703SBrian Somers         others_active++;
308c8f30703SBrian Somers     }
30986b1f0d7SBrian Somers 
310c8f30703SBrian Somers     if (bundle->ncp.mp.active) {
311ab2de065SBrian Somers       bundle_CalculateBandwidth(bundle);
31286b1f0d7SBrian Somers 
31386b1f0d7SBrian Somers       if (lost)
31486b1f0d7SBrian Somers         mp_LinkLost(&bundle->ncp.mp, lost);
31586b1f0d7SBrian Somers       else
316a33b2ef7SBrian Somers         log_Printf(LogALERT, "Oops, lost an unrecognised datalink (%s) !\n",
31786b1f0d7SBrian Somers                    fp->link->name);
3183b0f8d2eSBrian Somers     }
319c8f30703SBrian Somers 
320c8f30703SBrian Somers     if (!others_active)
321c8f30703SBrian Somers       /* Down the NCPs.  We don't expect to get fsm_Close()d ourself ! */
322c8f30703SBrian Somers       fsm2initial(&bundle->ncp.ipcp.fsm);
3236d666775SBrian Somers   }
324ff0f9439SBrian Somers }
3256d666775SBrian Somers 
3266d666775SBrian Somers static void
3276d666775SBrian Somers bundle_LayerFinish(void *v, struct fsm *fp)
3286d666775SBrian Somers {
3296d666775SBrian Somers   /* The given fsm is now down (fp cannot be NULL)
3306d666775SBrian Somers    *
331dd7e2610SBrian Somers    * If it's the last NCP, fsm_Close all LCPs
3326d666775SBrian Somers    */
3336d666775SBrian Somers 
3346d666775SBrian Somers   struct bundle *bundle = (struct bundle *)v;
3356d666775SBrian Somers   struct datalink *dl;
3366d666775SBrian Somers 
3373b0f8d2eSBrian Somers   if (fp->proto == PROTO_IPCP) {
33826afeaa2SBrian Somers     if (bundle_Phase(bundle) != PHASE_DEAD)
33925092092SBrian Somers       bundle_NewPhase(bundle, PHASE_TERMINATE);
3406d666775SBrian Somers     for (dl = bundle->links; dl; dl = dl->next)
341c8f30703SBrian Somers       if (dl->state == DATALINK_OPEN)
342d4d5d2f8SBrian Somers         datalink_Close(dl, CLOSE_STAYDOWN);
34309206a6fSBrian Somers     fsm2initial(fp);
3443b0f8d2eSBrian Somers   }
3453b0f8d2eSBrian Somers }
3466d666775SBrian Somers 
3477a6f8720SBrian Somers int
3487a6f8720SBrian Somers bundle_LinkIsUp(const struct bundle *bundle)
3497a6f8720SBrian Somers {
3505828db6dSBrian Somers   return bundle->ncp.ipcp.fsm.state == ST_OPENED;
3517a6f8720SBrian Somers }
3527a6f8720SBrian Somers 
3537a6f8720SBrian Somers void
3549c81b87dSBrian Somers bundle_Close(struct bundle *bundle, const char *name, int how)
3557a6f8720SBrian Somers {
356455aabc3SBrian Somers   /*
3573006ec67SBrian Somers    * Please close the given datalink.
358dd7e2610SBrian Somers    * If name == NULL or name is the last datalink, fsm_Close all NCPs
3596f384573SBrian Somers    * (except our MP)
3603b0f8d2eSBrian Somers    * If it isn't the last datalink, just Close that datalink.
361455aabc3SBrian Somers    */
3627a6f8720SBrian Somers 
3633b0f8d2eSBrian Somers   struct datalink *dl, *this_dl;
3643b0f8d2eSBrian Somers   int others_active;
3653006ec67SBrian Somers 
3663b0f8d2eSBrian Somers   others_active = 0;
3673b0f8d2eSBrian Somers   this_dl = NULL;
3683b0f8d2eSBrian Somers 
3693b0f8d2eSBrian Somers   for (dl = bundle->links; dl; dl = dl->next) {
3703b0f8d2eSBrian Somers     if (name && !strcasecmp(name, dl->name))
3713b0f8d2eSBrian Somers       this_dl = dl;
3723b0f8d2eSBrian Somers     if (name == NULL || this_dl == dl) {
3739c81b87dSBrian Somers       switch (how) {
3749c81b87dSBrian Somers         case CLOSE_LCP:
3759c81b87dSBrian Somers           datalink_DontHangup(dl);
3769c81b87dSBrian Somers           /* fall through */
3779c81b87dSBrian Somers         case CLOSE_STAYDOWN:
3783006ec67SBrian Somers           datalink_StayDown(dl);
3799c81b87dSBrian Somers           break;
3809c81b87dSBrian Somers       }
3813b0f8d2eSBrian Somers     } else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP)
3823b0f8d2eSBrian Somers       others_active++;
3833b0f8d2eSBrian Somers   }
3843b0f8d2eSBrian Somers 
3853b0f8d2eSBrian Somers   if (name && this_dl == NULL) {
386dd7e2610SBrian Somers     log_Printf(LogWARN, "%s: Invalid datalink name\n", name);
3873b0f8d2eSBrian Somers     return;
3883b0f8d2eSBrian Somers   }
3893b0f8d2eSBrian Somers 
3903b0f8d2eSBrian Somers   if (!others_active) {
39104eaa58cSBrian Somers     bundle_StopIdleTimer(bundle);
3923b0f8d2eSBrian Somers     if (bundle->ncp.ipcp.fsm.state > ST_CLOSED ||
3933b0f8d2eSBrian Somers         bundle->ncp.ipcp.fsm.state == ST_STARTING)
394dd7e2610SBrian Somers       fsm_Close(&bundle->ncp.ipcp.fsm);
3953b0f8d2eSBrian Somers     else {
39609206a6fSBrian Somers       fsm2initial(&bundle->ncp.ipcp.fsm);
397d345321bSBrian Somers       for (dl = bundle->links; dl; dl = dl->next)
3989c81b87dSBrian Somers         datalink_Close(dl, how);
3997a6f8720SBrian Somers     }
4003b0f8d2eSBrian Somers   } else if (this_dl && this_dl->state != DATALINK_CLOSED &&
4013b0f8d2eSBrian Somers              this_dl->state != DATALINK_HANGUP)
4029c81b87dSBrian Somers     datalink_Close(this_dl, how);
403d2fd8d77SBrian Somers }
4047a6f8720SBrian Somers 
4051bc9b5baSBrian Somers void
406899011c4SBrian Somers bundle_Down(struct bundle *bundle, int how)
4071bc9b5baSBrian Somers {
4081bc9b5baSBrian Somers   struct datalink *dl;
4091bc9b5baSBrian Somers 
4101bc9b5baSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
411899011c4SBrian Somers     datalink_Down(dl, how);
4121bc9b5baSBrian Somers }
4131bc9b5baSBrian Somers 
41426af0ae9SBrian Somers static size_t
41526af0ae9SBrian Somers bundle_FillQueues(struct bundle *bundle)
41626af0ae9SBrian Somers {
41726af0ae9SBrian Somers   size_t total;
41826af0ae9SBrian Somers 
41926af0ae9SBrian Somers   if (bundle->ncp.mp.active)
42026af0ae9SBrian Somers     total = mp_FillQueues(bundle);
42126af0ae9SBrian Somers   else {
42226af0ae9SBrian Somers     struct datalink *dl;
42326af0ae9SBrian Somers     size_t add;
42426af0ae9SBrian Somers 
42526af0ae9SBrian Somers     for (total = 0, dl = bundle->links; dl; dl = dl->next)
42626af0ae9SBrian Somers       if (dl->state == DATALINK_OPEN) {
42726af0ae9SBrian Somers         add = link_QueueLen(&dl->physical->link);
42826af0ae9SBrian Somers         if (add == 0 && dl->physical->out == NULL)
42926af0ae9SBrian Somers           add = ip_PushPacket(&dl->physical->link, bundle);
43026af0ae9SBrian Somers         total += add;
43126af0ae9SBrian Somers       }
43226af0ae9SBrian Somers   }
43326af0ae9SBrian Somers 
43426af0ae9SBrian Somers   return total + ip_QueueLen(&bundle->ncp.ipcp);
43526af0ae9SBrian Somers }
43626af0ae9SBrian Somers 
4372f786681SBrian Somers static int
438f013f33eSBrian Somers bundle_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
4392f786681SBrian Somers {
4402f786681SBrian Somers   struct bundle *bundle = descriptor2bundle(d);
4412f786681SBrian Somers   struct datalink *dl;
44226af0ae9SBrian Somers   int result, nlinks;
4436c1d6731SBrian Somers   u_short ifqueue;
44426af0ae9SBrian Somers   size_t queued;
4452f786681SBrian Somers 
4462f786681SBrian Somers   result = 0;
4472f786681SBrian Somers 
448078c562eSBrian Somers   /* If there are aren't many packets queued, look for some more. */
44904eaa58cSBrian Somers   for (nlinks = 0, dl = bundle->links; dl; dl = dl->next)
45004eaa58cSBrian Somers     nlinks++;
45104eaa58cSBrian Somers 
45204eaa58cSBrian Somers   if (nlinks) {
4535a72b6edSBrian Somers     queued = r ? bundle_FillQueues(bundle) : ip_QueueLen(&bundle->ncp.ipcp);
45404eaa58cSBrian Somers 
455ff0f9439SBrian Somers     if (r && (bundle->phase == PHASE_NETWORK ||
456ff0f9439SBrian Somers               bundle->phys_type.all & PHYS_AUTO)) {
45704eaa58cSBrian Somers       /* enough surplus so that we can tell if we're getting swamped */
4586c1d6731SBrian Somers       ifqueue = nlinks > bundle->cfg.ifqueue ? nlinks : bundle->cfg.ifqueue;
4596c1d6731SBrian Somers       if (queued < ifqueue) {
46004eaa58cSBrian Somers         /* Not enough - select() for more */
4616f8e9f0aSBrian Somers         if (bundle->choked.timer.state == TIMER_RUNNING)
4626f8e9f0aSBrian Somers           timer_Stop(&bundle->choked.timer);	/* Not needed any more */
46304eaa58cSBrian Somers         FD_SET(bundle->dev.fd, r);
464faefde08SBrian Somers         if (*n < bundle->dev.fd + 1)
465faefde08SBrian Somers           *n = bundle->dev.fd + 1;
4661384bd27SBrian Somers         log_Printf(LogTIMER, "%s: fdset(r) %d\n", TUN_NAME, bundle->dev.fd);
467078c562eSBrian Somers         result++;
4686f8e9f0aSBrian Somers       } else if (bundle->choked.timer.state == TIMER_STOPPED) {
4696f8e9f0aSBrian Somers         bundle->choked.timer.func = bundle_ClearQueues;
4706f8e9f0aSBrian Somers         bundle->choked.timer.name = "output choke";
4716f8e9f0aSBrian Somers         bundle->choked.timer.load = bundle->cfg.choked.timeout * SECTICKS;
4726f8e9f0aSBrian Somers         bundle->choked.timer.arg = bundle;
4736f8e9f0aSBrian Somers         timer_Start(&bundle->choked.timer);
474078c562eSBrian Somers       }
47504eaa58cSBrian Somers     }
47604eaa58cSBrian Somers   }
477078c562eSBrian Somers 
478f0cdd9c0SBrian Somers #ifndef NORADIUS
479f0cdd9c0SBrian Somers   result += descriptor_UpdateSet(&bundle->radius.desc, r, w, e, n);
480f0cdd9c0SBrian Somers #endif
481f0cdd9c0SBrian Somers 
48271555108SBrian Somers   /* Which links need a select() ? */
48371555108SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
48471555108SBrian Somers     result += descriptor_UpdateSet(&dl->desc, r, w, e, n);
48571555108SBrian Somers 
486ea722969SBrian Somers   /*
487ea722969SBrian Somers    * This *MUST* be called after the datalink UpdateSet()s as it
48804eaa58cSBrian Somers    * might be ``holding'' one of the datalinks (death-row) and
4898e7bd08eSBrian Somers    * wants to be able to de-select() it from the descriptor set.
490ea722969SBrian Somers    */
4910f2f3eb3SBrian Somers   result += descriptor_UpdateSet(&bundle->ncp.mp.server.desc, r, w, e, n);
492ea722969SBrian Somers 
4932f786681SBrian Somers   return result;
4942f786681SBrian Somers }
4952f786681SBrian Somers 
4962f786681SBrian Somers static int
497f013f33eSBrian Somers bundle_IsSet(struct fdescriptor *d, const fd_set *fdset)
4982f786681SBrian Somers {
4992f786681SBrian Somers   struct bundle *bundle = descriptor2bundle(d);
5002f786681SBrian Somers   struct datalink *dl;
5012f786681SBrian Somers 
5022f786681SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
5032f786681SBrian Somers     if (descriptor_IsSet(&dl->desc, fdset))
5042f786681SBrian Somers       return 1;
5052f786681SBrian Somers 
506f0cdd9c0SBrian Somers #ifndef NORADIUS
507f0cdd9c0SBrian Somers   if (descriptor_IsSet(&bundle->radius.desc, fdset))
508f0cdd9c0SBrian Somers     return 1;
509f0cdd9c0SBrian Somers #endif
510f0cdd9c0SBrian Somers 
511332b9de0SBrian Somers   if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset))
512332b9de0SBrian Somers     return 1;
513332b9de0SBrian Somers 
514faefde08SBrian Somers   return FD_ISSET(bundle->dev.fd, fdset);
5152f786681SBrian Somers }
5162f786681SBrian Somers 
5172f786681SBrian Somers static void
518f013f33eSBrian Somers bundle_DescriptorRead(struct fdescriptor *d, struct bundle *bundle,
5192f786681SBrian Somers                       const fd_set *fdset)
5202f786681SBrian Somers {
5212f786681SBrian Somers   struct datalink *dl;
5222f786681SBrian Somers 
523ea722969SBrian Somers   if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset))
524ea722969SBrian Somers     descriptor_Read(&bundle->ncp.mp.server.desc, bundle, fdset);
525ea722969SBrian Somers 
5262f786681SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
5272f786681SBrian Somers     if (descriptor_IsSet(&dl->desc, fdset))
5282f786681SBrian Somers       descriptor_Read(&dl->desc, bundle, fdset);
529b6217683SBrian Somers 
530f0cdd9c0SBrian Somers #ifndef NORADIUS
531f0cdd9c0SBrian Somers   if (descriptor_IsSet(&bundle->radius.desc, fdset))
532f0cdd9c0SBrian Somers     descriptor_Read(&bundle->radius.desc, bundle, fdset);
533f0cdd9c0SBrian Somers #endif
534f0cdd9c0SBrian Somers 
535faefde08SBrian Somers   if (FD_ISSET(bundle->dev.fd, fdset)) {
536078c562eSBrian Somers     struct tun_data tun;
537078c562eSBrian Somers     int n, pri;
5383a7b6d76SBrian Somers     char *data;
5393a7b6d76SBrian Somers     size_t sz;
5403a7b6d76SBrian Somers 
5413a7b6d76SBrian Somers     if (bundle->dev.header) {
5423a7b6d76SBrian Somers       data = (char *)&tun;
5433a7b6d76SBrian Somers       sz = sizeof tun;
5443a7b6d76SBrian Somers     } else {
5453a7b6d76SBrian Somers       data = tun.data;
5463a7b6d76SBrian Somers       sz = sizeof tun.data;
5473a7b6d76SBrian Somers     }
548078c562eSBrian Somers 
549078c562eSBrian Somers     /* something to read from tun */
5503a7b6d76SBrian Somers 
5513a7b6d76SBrian Somers     n = read(bundle->dev.fd, data, sz);
552078c562eSBrian Somers     if (n < 0) {
5533a7b6d76SBrian Somers       log_Printf(LogWARN, "%s: read: %s\n", bundle->dev.Name, strerror(errno));
554078c562eSBrian Somers       return;
555078c562eSBrian Somers     }
5563a7b6d76SBrian Somers 
5573a7b6d76SBrian Somers     if (bundle->dev.header) {
5583a7b6d76SBrian Somers       n -= sz - sizeof tun.data;
559078c562eSBrian Somers       if (n <= 0) {
5603a7b6d76SBrian Somers         log_Printf(LogERROR, "%s: read: Got only %d bytes of data !\n",
5613a7b6d76SBrian Somers                    bundle->dev.Name, n);
562078c562eSBrian Somers         return;
563078c562eSBrian Somers       }
5643a7b6d76SBrian Somers       if (ntohl(tun.family) != AF_INET)
5653a7b6d76SBrian Somers         /* XXX: Should be maintaining drop/family counts ! */
566078c562eSBrian Somers         return;
5673a7b6d76SBrian Somers     }
568078c562eSBrian Somers 
569078c562eSBrian Somers     if (((struct ip *)tun.data)->ip_dst.s_addr ==
570078c562eSBrian Somers         bundle->ncp.ipcp.my_ip.s_addr) {
571078c562eSBrian Somers       /* we've been asked to send something addressed *to* us :( */
572078c562eSBrian Somers       if (Enabled(bundle, OPT_LOOPBACK)) {
573078c562eSBrian Somers         pri = PacketCheck(bundle, tun.data, n, &bundle->filter.in);
574078c562eSBrian Somers         if (pri >= 0) {
5753a7b6d76SBrian Somers           n += sz - sizeof tun.data;
5763a7b6d76SBrian Somers           write(bundle->dev.fd, data, n);
577078c562eSBrian Somers           log_Printf(LogDEBUG, "Looped back packet addressed to myself\n");
578078c562eSBrian Somers         }
579078c562eSBrian Somers         return;
580078c562eSBrian Somers       } else
581078c562eSBrian Somers         log_Printf(LogDEBUG, "Oops - forwarding packet addressed to myself\n");
582078c562eSBrian Somers     }
583078c562eSBrian Somers 
584078c562eSBrian Somers     /*
585078c562eSBrian Somers      * Process on-demand dialup. Output packets are queued within tunnel
586078c562eSBrian Somers      * device until IPCP is opened.
587078c562eSBrian Somers      */
588078c562eSBrian Somers 
589078c562eSBrian Somers     if (bundle_Phase(bundle) == PHASE_DEAD) {
590078c562eSBrian Somers       /*
591078c562eSBrian Somers        * Note, we must be in AUTO mode :-/ otherwise our interface should
592078c562eSBrian Somers        * *not* be UP and we can't receive data
593078c562eSBrian Somers        */
594078c562eSBrian Somers       if ((pri = PacketCheck(bundle, tun.data, n, &bundle->filter.dial)) >= 0)
595ba23f397SBrian Somers         bundle_Open(bundle, NULL, PHYS_AUTO, 0);
596078c562eSBrian Somers       else
597078c562eSBrian Somers         /*
598078c562eSBrian Somers          * Drop the packet.  If we were to queue it, we'd just end up with
599078c562eSBrian Somers          * a pile of timed-out data in our output queue by the time we get
600078c562eSBrian Somers          * around to actually dialing.  We'd also prematurely reach the
601078c562eSBrian Somers          * threshold at which we stop select()ing to read() the tun
602078c562eSBrian Somers          * device - breaking auto-dial.
603078c562eSBrian Somers          */
604078c562eSBrian Somers         return;
605078c562eSBrian Somers     }
606078c562eSBrian Somers 
607078c562eSBrian Somers     pri = PacketCheck(bundle, tun.data, n, &bundle->filter.out);
6085d9e6103SBrian Somers     if (pri >= 0)
6095a72b6edSBrian Somers       ip_Enqueue(&bundle->ncp.ipcp, pri, tun.data, n);
610078c562eSBrian Somers   }
611078c562eSBrian Somers }
6122f786681SBrian Somers 
6131af29a6eSBrian Somers static int
614f013f33eSBrian Somers bundle_DescriptorWrite(struct fdescriptor *d, struct bundle *bundle,
6152f786681SBrian Somers                        const fd_set *fdset)
6162f786681SBrian Somers {
6172f786681SBrian Somers   struct datalink *dl;
6181af29a6eSBrian Somers   int result = 0;
6192f786681SBrian Somers 
620ea722969SBrian Somers   /* This is not actually necessary as struct mpserver doesn't Write() */
621ea722969SBrian Somers   if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset))
622ea722969SBrian Somers     descriptor_Write(&bundle->ncp.mp.server.desc, bundle, fdset);
623ea722969SBrian Somers 
6242f786681SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
6252f786681SBrian Somers     if (descriptor_IsSet(&dl->desc, fdset))
6261af29a6eSBrian Somers       result += descriptor_Write(&dl->desc, bundle, fdset);
6271af29a6eSBrian Somers 
6281af29a6eSBrian Somers   return result;
6292f786681SBrian Somers }
6302f786681SBrian Somers 
631da66dd13SBrian Somers void
6321384bd27SBrian Somers bundle_LockTun(struct bundle *bundle)
6331384bd27SBrian Somers {
6341384bd27SBrian Somers   FILE *lockfile;
6351384bd27SBrian Somers   char pidfile[MAXPATHLEN];
6361384bd27SBrian Somers 
6371384bd27SBrian Somers   snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit);
6381384bd27SBrian Somers   lockfile = ID0fopen(pidfile, "w");
6391384bd27SBrian Somers   if (lockfile != NULL) {
6401384bd27SBrian Somers     fprintf(lockfile, "%d\n", (int)getpid());
6411384bd27SBrian Somers     fclose(lockfile);
6421384bd27SBrian Somers   }
6431384bd27SBrian Somers #ifndef RELEASE_CRUNCH
6441384bd27SBrian Somers   else
6451384bd27SBrian Somers     log_Printf(LogERROR, "Warning: Can't create %s: %s\n",
6461384bd27SBrian Somers                pidfile, strerror(errno));
6471384bd27SBrian Somers #endif
6481384bd27SBrian Somers }
6491384bd27SBrian Somers 
6501384bd27SBrian Somers static void
6511384bd27SBrian Somers bundle_UnlockTun(struct bundle *bundle)
6521384bd27SBrian Somers {
6531384bd27SBrian Somers   char pidfile[MAXPATHLEN];
6541384bd27SBrian Somers 
6551384bd27SBrian Somers   snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit);
6561384bd27SBrian Somers   ID0unlink(pidfile);
6571384bd27SBrian Somers }
6587a6f8720SBrian Somers 
6597a6f8720SBrian Somers struct bundle *
660cf0a3940SBrian Somers bundle_Create(const char *prefix, int type, int unit)
6617a6f8720SBrian Somers {
6627a6f8720SBrian Somers   static struct bundle bundle;		/* there can be only one */
663c0593e34SBrian Somers   int enoentcount, err, minunit, maxunit;
6644e5196e9SBrian Somers   const char *ifname;
665fdb4bb1bSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
666fc3034caSBrian Somers   int kldtried;
667fc3034caSBrian Somers #endif
6683a7b6d76SBrian Somers #if defined(TUNSIFMODE) || defined(TUNSLMODE) || defined(TUNSIFHEAD)
6690f203c7eSBrian Somers   int iff;
6700f203c7eSBrian Somers #endif
6717a6f8720SBrian Somers 
6728fa6ebe4SBrian Somers   if (bundle.iface != NULL) {	/* Already allocated ! */
673a33b2ef7SBrian Somers     log_Printf(LogALERT, "bundle_Create:  There's only one BUNDLE !\n");
6747a6f8720SBrian Somers     return NULL;
6757a6f8720SBrian Somers   }
6767a6f8720SBrian Somers 
677c0593e34SBrian Somers   if (unit == -1) {
678c0593e34SBrian Somers     minunit = 0;
679c0593e34SBrian Somers     maxunit = -1;
680c0593e34SBrian Somers   } else {
681c0593e34SBrian Somers     minunit = unit;
682c0593e34SBrian Somers     maxunit = unit + 1;
683c0593e34SBrian Somers   }
6847a6f8720SBrian Somers   err = ENOENT;
6857a6f8720SBrian Somers   enoentcount = 0;
686fdb4bb1bSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
687fc3034caSBrian Somers   kldtried = 0;
688fc3034caSBrian Somers #endif
689c0593e34SBrian Somers   for (bundle.unit = minunit; bundle.unit != maxunit; bundle.unit++) {
690faefde08SBrian Somers     snprintf(bundle.dev.Name, sizeof bundle.dev.Name, "%s%d",
691faefde08SBrian Somers              prefix, bundle.unit);
692faefde08SBrian Somers     bundle.dev.fd = ID0open(bundle.dev.Name, O_RDWR);
693faefde08SBrian Somers     if (bundle.dev.fd >= 0)
6947a6f8720SBrian Somers       break;
695107d62e7SBrian Somers     else if (errno == ENXIO) {
696fdb4bb1bSBrian Somers #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
697c0593e34SBrian Somers       if (bundle.unit == minunit && !kldtried++) {
698fc3034caSBrian Somers         /*
699fdb4bb1bSBrian Somers 	 * Attempt to load the tunnel interface KLD if it isn't loaded
700fdb4bb1bSBrian Somers 	 * already.
701fc3034caSBrian Somers          */
702fdb4bb1bSBrian Somers         if (modfind("if_tun") == -1) {
703fc3034caSBrian Somers           if (ID0kldload("if_tun") != -1) {
704fc3034caSBrian Somers             bundle.unit--;
705fc3034caSBrian Somers             continue;
706fc3034caSBrian Somers           }
707fc3034caSBrian Somers           log_Printf(LogWARN, "kldload: if_tun: %s\n", strerror(errno));
708fc3034caSBrian Somers         }
709fc3034caSBrian Somers       }
710fc3034caSBrian Somers #endif
7117a6f8720SBrian Somers       err = errno;
712107d62e7SBrian Somers       break;
7137a6f8720SBrian Somers     } else if (errno == ENOENT) {
7147a6f8720SBrian Somers       if (++enoentcount > 2)
715107d62e7SBrian Somers 	break;
7167a6f8720SBrian Somers     } else
7177a6f8720SBrian Somers       err = errno;
7187a6f8720SBrian Somers   }
7197a6f8720SBrian Somers 
720faefde08SBrian Somers   if (bundle.dev.fd < 0) {
721c0593e34SBrian Somers     if (unit == -1)
722c0593e34SBrian Somers       log_Printf(LogWARN, "No available tunnel devices found (%s)\n",
72385b542cfSBrian Somers                 strerror(err));
724c0593e34SBrian Somers     else
725c0593e34SBrian Somers       log_Printf(LogWARN, "%s%d: %s\n", prefix, unit, strerror(err));
7267a6f8720SBrian Somers     return NULL;
7277a6f8720SBrian Somers   }
7287a6f8720SBrian Somers 
729dd7e2610SBrian Somers   log_SetTun(bundle.unit);
7307a6f8720SBrian Somers 
7318fa6ebe4SBrian Somers   ifname = strrchr(bundle.dev.Name, '/');
7328fa6ebe4SBrian Somers   if (ifname == NULL)
7338fa6ebe4SBrian Somers     ifname = bundle.dev.Name;
7347a6f8720SBrian Somers   else
7358fa6ebe4SBrian Somers     ifname++;
7368fa6ebe4SBrian Somers 
7378fa6ebe4SBrian Somers   bundle.iface = iface_Create(ifname);
7388fa6ebe4SBrian Somers   if (bundle.iface == NULL) {
7398fa6ebe4SBrian Somers     close(bundle.dev.fd);
7408fa6ebe4SBrian Somers     return NULL;
7418fa6ebe4SBrian Somers   }
7427a6f8720SBrian Somers 
7430f203c7eSBrian Somers #ifdef TUNSIFMODE
7440f203c7eSBrian Somers   /* Make sure we're POINTOPOINT */
7450f203c7eSBrian Somers   iff = IFF_POINTOPOINT;
7460f203c7eSBrian Somers   if (ID0ioctl(bundle.dev.fd, TUNSIFMODE, &iff) < 0)
7470f203c7eSBrian Somers     log_Printf(LogERROR, "bundle_Create: ioctl(TUNSIFMODE): %s\n",
7480f203c7eSBrian Somers 	       strerror(errno));
7490f203c7eSBrian Somers #endif
7500f203c7eSBrian Somers 
7516ca65df0SBrian Somers #ifdef TUNSLMODE
7523a7b6d76SBrian Somers   /* Make sure we're not prepending sockaddrs */
7536ca65df0SBrian Somers   iff = 0;
7546ca65df0SBrian Somers   if (ID0ioctl(bundle.dev.fd, TUNSLMODE, &iff) < 0)
7556ca65df0SBrian Somers     log_Printf(LogERROR, "bundle_Create: ioctl(TUNSLMODE): %s\n",
7566ca65df0SBrian Somers 	       strerror(errno));
7576ca65df0SBrian Somers #endif
7586ca65df0SBrian Somers 
7593a7b6d76SBrian Somers #ifdef TUNSIFHEAD
7603a7b6d76SBrian Somers   /* We want the address family please ! */
7613a7b6d76SBrian Somers   iff = 1;
7623a7b6d76SBrian Somers   if (ID0ioctl(bundle.dev.fd, TUNSIFHEAD, &iff) < 0) {
7633a7b6d76SBrian Somers     log_Printf(LogERROR, "bundle_Create: ioctl(TUNSIFHEAD): %s\n",
7643a7b6d76SBrian Somers 	       strerror(errno));
7653a7b6d76SBrian Somers     bundle.dev.header = 0;
7663a7b6d76SBrian Somers   } else
7673a7b6d76SBrian Somers     bundle.dev.header = 1;
7683a7b6d76SBrian Somers #else
7693a7b6d76SBrian Somers #ifdef __OpenBSD__
7703a7b6d76SBrian Somers   /* Always present for OpenBSD */
7713a7b6d76SBrian Somers   bundle.dev.header = 1;
7723a7b6d76SBrian Somers #else
7733a7b6d76SBrian Somers   /*
7743a7b6d76SBrian Somers    * If TUNSIFHEAD isn't available and we're not OpenBSD, assume
7753a7b6d76SBrian Somers    * everything's AF_INET (hopefully the tun device won't pass us
7763a7b6d76SBrian Somers    * anything else !).
7773a7b6d76SBrian Somers    */
7783a7b6d76SBrian Somers   bundle.dev.header = 0;
7793a7b6d76SBrian Somers #endif
7803a7b6d76SBrian Somers #endif
7813a7b6d76SBrian Somers 
7824e5196e9SBrian Somers   if (!iface_SetFlags(bundle.iface, IFF_UP)) {
7838fa6ebe4SBrian Somers     iface_Destroy(bundle.iface);
7848fa6ebe4SBrian Somers     bundle.iface = NULL;
785faefde08SBrian Somers     close(bundle.dev.fd);
7867a6f8720SBrian Somers     return NULL;
7877a6f8720SBrian Somers   }
7887a6f8720SBrian Somers 
7898fa6ebe4SBrian Somers   log_Printf(LogPHASE, "Using interface: %s\n", ifname);
7907a6f8720SBrian Somers 
791ab2de065SBrian Somers   bundle.bandwidth = 0;
792820de6ebSBrian Somers   bundle.routing_seq = 0;
793a0cbd833SBrian Somers   bundle.phase = PHASE_DEAD;
794a0cbd833SBrian Somers   bundle.CleaningUp = 0;
79567b072f7SBrian Somers   bundle.NatEnabled = 0;
7967a6f8720SBrian Somers 
7976d666775SBrian Somers   bundle.fsm.LayerStart = bundle_LayerStart;
7986f384573SBrian Somers   bundle.fsm.LayerUp = bundle_LayerUp;
7996d666775SBrian Somers   bundle.fsm.LayerDown = bundle_LayerDown;
8006d666775SBrian Somers   bundle.fsm.LayerFinish = bundle_LayerFinish;
8016d666775SBrian Somers   bundle.fsm.object = &bundle;
8027a6f8720SBrian Somers 
803dade2407SBrian Somers   bundle.cfg.idle.timeout = NCP_IDLE_TIMEOUT;
804dade2407SBrian Somers   bundle.cfg.idle.min_timeout = 0;
8051342caedSBrian Somers   *bundle.cfg.auth.name = '\0';
8061342caedSBrian Somers   *bundle.cfg.auth.key = '\0';
807610b185fSBrian Somers   bundle.cfg.opt = OPT_SROUTES | OPT_IDCHECK | OPT_LOOPBACK |
808610b185fSBrian Somers                    OPT_THROUGHPUT | OPT_UTMP;
80949052c95SBrian Somers   *bundle.cfg.label = '\0';
81049052c95SBrian Somers   bundle.cfg.mtu = DEF_MTU;
8116c1d6731SBrian Somers   bundle.cfg.ifqueue = DEF_IFQUEUE;
8126f8e9f0aSBrian Somers   bundle.cfg.choked.timeout = CHOKED_TIMEOUT;
813ff0f9439SBrian Somers   bundle.phys_type.all = type;
814ff0f9439SBrian Somers   bundle.phys_type.open = 0;
815dade2407SBrian Somers   bundle.upat = 0;
816ab886ad0SBrian Somers 
8176f384573SBrian Somers   bundle.links = datalink_Create("deflink", &bundle, type);
8183006ec67SBrian Somers   if (bundle.links == NULL) {
819a33b2ef7SBrian Somers     log_Printf(LogALERT, "Cannot create data link: %s\n", strerror(errno));
8208fa6ebe4SBrian Somers     iface_Destroy(bundle.iface);
8218fa6ebe4SBrian Somers     bundle.iface = NULL;
822faefde08SBrian Somers     close(bundle.dev.fd);
8232289f246SBrian Somers     return NULL;
8242289f246SBrian Somers   }
8252289f246SBrian Somers 
8262f786681SBrian Somers   bundle.desc.type = BUNDLE_DESCRIPTOR;
8272f786681SBrian Somers   bundle.desc.UpdateSet = bundle_UpdateSet;
8282f786681SBrian Somers   bundle.desc.IsSet = bundle_IsSet;
8292f786681SBrian Somers   bundle.desc.Read = bundle_DescriptorRead;
8302f786681SBrian Somers   bundle.desc.Write = bundle_DescriptorWrite;
8312f786681SBrian Somers 
83249052c95SBrian Somers   mp_Init(&bundle.ncp.mp, &bundle);
83349052c95SBrian Somers 
83449052c95SBrian Somers   /* Send over the first physical link by default */
8355828db6dSBrian Somers   ipcp_Init(&bundle.ncp.ipcp, &bundle, &bundle.links->physical->link,
8365828db6dSBrian Somers             &bundle.fsm);
8376d666775SBrian Somers 
8385ca5389aSBrian Somers   memset(&bundle.filter, '\0', sizeof bundle.filter);
8395ca5389aSBrian Somers   bundle.filter.in.fragok = bundle.filter.in.logok = 1;
8405ca5389aSBrian Somers   bundle.filter.in.name = "IN";
8415ca5389aSBrian Somers   bundle.filter.out.fragok = bundle.filter.out.logok = 1;
8425ca5389aSBrian Somers   bundle.filter.out.name = "OUT";
8435ca5389aSBrian Somers   bundle.filter.dial.name = "DIAL";
8448390b576SBrian Somers   bundle.filter.dial.logok = 1;
8455ca5389aSBrian Somers   bundle.filter.alive.name = "ALIVE";
8465ca5389aSBrian Somers   bundle.filter.alive.logok = 1;
847cad7e742SBrian Somers   {
848cad7e742SBrian Somers     int	i;
849cad7e742SBrian Somers     for (i = 0; i < MAXFILTERS; i++) {
850cad7e742SBrian Somers         bundle.filter.in.rule[i].f_action = A_NONE;
851cad7e742SBrian Somers         bundle.filter.out.rule[i].f_action = A_NONE;
852cad7e742SBrian Somers         bundle.filter.dial.rule[i].f_action = A_NONE;
853cad7e742SBrian Somers         bundle.filter.alive.rule[i].f_action = A_NONE;
854cad7e742SBrian Somers     }
855cad7e742SBrian Somers   }
85693ee0ff2SBrian Somers   memset(&bundle.idle.timer, '\0', sizeof bundle.idle.timer);
85793ee0ff2SBrian Somers   bundle.idle.done = 0;
8585cf4388bSBrian Somers   bundle.notify.fd = -1;
8596f8e9f0aSBrian Somers   memset(&bundle.choked.timer, '\0', sizeof bundle.choked.timer);
860972a1bcfSBrian Somers #ifndef NORADIUS
861972a1bcfSBrian Somers   radius_Init(&bundle.radius);
862972a1bcfSBrian Somers #endif
86393ee0ff2SBrian Somers 
8646d666775SBrian Somers   /* Clean out any leftover crud */
8658fa6ebe4SBrian Somers   iface_Clear(bundle.iface, IFACE_CLEAR_ALL);
8666d666775SBrian Somers 
8671384bd27SBrian Somers   bundle_LockTun(&bundle);
8681384bd27SBrian Somers 
8697a6f8720SBrian Somers   return &bundle;
8707a6f8720SBrian Somers }
8717a6f8720SBrian Somers 
87268a0f0ccSBrian Somers static void
87368a0f0ccSBrian Somers bundle_DownInterface(struct bundle *bundle)
87468a0f0ccSBrian Somers {
875dd7e2610SBrian Somers   route_IfDelete(bundle, 1);
8764e5196e9SBrian Somers   iface_ClearFlags(bundle->iface, IFF_UP);
87768a0f0ccSBrian Somers }
87868a0f0ccSBrian Somers 
87968a0f0ccSBrian Somers void
88068a0f0ccSBrian Somers bundle_Destroy(struct bundle *bundle)
88168a0f0ccSBrian Somers {
8823006ec67SBrian Somers   struct datalink *dl;
883b6217683SBrian Somers 
884ea722969SBrian Somers   /*
88504eaa58cSBrian Somers    * Clean up the interface.  We don't need to timer_Stop()s, mp_Down(),
886ea722969SBrian Somers    * ipcp_CleanInterface() and bundle_DownInterface() unless we're getting
8878e7bd08eSBrian Somers    * out under exceptional conditions such as a descriptor exception.
888ea722969SBrian Somers    */
88904eaa58cSBrian Somers   timer_Stop(&bundle->idle.timer);
8906f8e9f0aSBrian Somers   timer_Stop(&bundle->choked.timer);
89166f634b6SBrian Somers   mp_Down(&bundle->ncp.mp);
892dd7e2610SBrian Somers   ipcp_CleanInterface(&bundle->ncp.ipcp);
89368a0f0ccSBrian Somers   bundle_DownInterface(bundle);
8943006ec67SBrian Somers 
895972a1bcfSBrian Somers #ifndef NORADIUS
896972a1bcfSBrian Somers   /* Tell the radius server the bad news */
897972a1bcfSBrian Somers   radius_Destroy(&bundle->radius);
898972a1bcfSBrian Somers #endif
899972a1bcfSBrian Somers 
900ea722969SBrian Somers   /* Again, these are all DATALINK_CLOSED unless we're abending */
9013006ec67SBrian Somers   dl = bundle->links;
9023006ec67SBrian Somers   while (dl)
9033006ec67SBrian Somers     dl = datalink_Destroy(dl);
9043006ec67SBrian Somers 
905442f8495SBrian Somers   ipcp_Destroy(&bundle->ncp.ipcp);
906442f8495SBrian Somers 
9071384bd27SBrian Somers   close(bundle->dev.fd);
9081384bd27SBrian Somers   bundle_UnlockTun(bundle);
9091384bd27SBrian Somers 
910ea722969SBrian Somers   /* In case we never made PHASE_NETWORK */
9115cf4388bSBrian Somers   bundle_Notify(bundle, EX_ERRDEAD);
912b6217683SBrian Somers 
9138fa6ebe4SBrian Somers   iface_Destroy(bundle->iface);
9148fa6ebe4SBrian Somers   bundle->iface = NULL;
91568a0f0ccSBrian Somers }
91668a0f0ccSBrian Somers 
9177a6f8720SBrian Somers struct rtmsg {
9187a6f8720SBrian Somers   struct rt_msghdr m_rtm;
9197a6f8720SBrian Somers   char m_space[64];
9207a6f8720SBrian Somers };
9217a6f8720SBrian Somers 
922610b185fSBrian Somers int
923820de6ebSBrian Somers bundle_SetRoute(struct bundle *bundle, int cmd, struct in_addr dst,
9242062443fSBrian Somers                 struct in_addr gateway, struct in_addr mask, int bang, int ssh)
9257a6f8720SBrian Somers {
9267a6f8720SBrian Somers   struct rtmsg rtmes;
9277a6f8720SBrian Somers   int s, nb, wb;
9287a6f8720SBrian Somers   char *cp;
9297a6f8720SBrian Somers   const char *cmdstr;
9307a6f8720SBrian Somers   struct sockaddr_in rtdata;
931610b185fSBrian Somers   int result = 1;
9327a6f8720SBrian Somers 
9337a6f8720SBrian Somers   if (bang)
9347a6f8720SBrian Somers     cmdstr = (cmd == RTM_ADD ? "Add!" : "Delete!");
9357a6f8720SBrian Somers   else
9367a6f8720SBrian Somers     cmdstr = (cmd == RTM_ADD ? "Add" : "Delete");
9377a6f8720SBrian Somers   s = ID0socket(PF_ROUTE, SOCK_RAW, 0);
9387a6f8720SBrian Somers   if (s < 0) {
939dd7e2610SBrian Somers     log_Printf(LogERROR, "bundle_SetRoute: socket(): %s\n", strerror(errno));
940610b185fSBrian Somers     return result;
9417a6f8720SBrian Somers   }
9427a6f8720SBrian Somers   memset(&rtmes, '\0', sizeof rtmes);
9437a6f8720SBrian Somers   rtmes.m_rtm.rtm_version = RTM_VERSION;
9447a6f8720SBrian Somers   rtmes.m_rtm.rtm_type = cmd;
9457a6f8720SBrian Somers   rtmes.m_rtm.rtm_addrs = RTA_DST;
946820de6ebSBrian Somers   rtmes.m_rtm.rtm_seq = ++bundle->routing_seq;
9477a6f8720SBrian Somers   rtmes.m_rtm.rtm_pid = getpid();
9487a6f8720SBrian Somers   rtmes.m_rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;
9497a6f8720SBrian Somers 
9503afe5ccbSBrian Somers   if (cmd == RTM_ADD || cmd == RTM_CHANGE) {
9513afe5ccbSBrian Somers     if (bundle->ncp.ipcp.cfg.sendpipe > 0) {
9523afe5ccbSBrian Somers       rtmes.m_rtm.rtm_rmx.rmx_sendpipe = bundle->ncp.ipcp.cfg.sendpipe;
9533afe5ccbSBrian Somers       rtmes.m_rtm.rtm_inits |= RTV_SPIPE;
9543afe5ccbSBrian Somers     }
9553afe5ccbSBrian Somers     if (bundle->ncp.ipcp.cfg.recvpipe > 0) {
9563afe5ccbSBrian Somers       rtmes.m_rtm.rtm_rmx.rmx_recvpipe = bundle->ncp.ipcp.cfg.recvpipe;
9573afe5ccbSBrian Somers       rtmes.m_rtm.rtm_inits |= RTV_RPIPE;
9583afe5ccbSBrian Somers     }
9593afe5ccbSBrian Somers   }
9603afe5ccbSBrian Somers 
9617a6f8720SBrian Somers   memset(&rtdata, '\0', sizeof rtdata);
96250e5c17dSBrian Somers   rtdata.sin_len = sizeof rtdata;
9637a6f8720SBrian Somers   rtdata.sin_family = AF_INET;
9647a6f8720SBrian Somers   rtdata.sin_port = 0;
9657a6f8720SBrian Somers   rtdata.sin_addr = dst;
9667a6f8720SBrian Somers 
9677a6f8720SBrian Somers   cp = rtmes.m_space;
96850e5c17dSBrian Somers   memcpy(cp, &rtdata, rtdata.sin_len);
96950e5c17dSBrian Somers   cp += rtdata.sin_len;
970e43ebac1SBrian Somers   if (cmd == RTM_ADD) {
9717a6f8720SBrian Somers     if (gateway.s_addr == INADDR_ANY) {
9720aa8aa17SBrian Somers       if (!ssh)
9738fa6ebe4SBrian Somers         log_Printf(LogERROR, "bundle_SetRoute: Cannot add a route with"
9748fa6ebe4SBrian Somers                    " destination 0.0.0.0\n");
9758fa6ebe4SBrian Somers       close(s);
9768fa6ebe4SBrian Somers       return result;
9777a6f8720SBrian Somers     } else {
9787a6f8720SBrian Somers       rtdata.sin_addr = gateway;
97950e5c17dSBrian Somers       memcpy(cp, &rtdata, rtdata.sin_len);
98050e5c17dSBrian Somers       cp += rtdata.sin_len;
9817a6f8720SBrian Somers       rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY;
9827a6f8720SBrian Somers     }
983e43ebac1SBrian Somers   }
9847a6f8720SBrian Somers 
9857a6f8720SBrian Somers   if (dst.s_addr == INADDR_ANY)
9867a6f8720SBrian Somers     mask.s_addr = INADDR_ANY;
9877a6f8720SBrian Somers 
9887a6f8720SBrian Somers   if (cmd == RTM_ADD || dst.s_addr == INADDR_ANY) {
9897a6f8720SBrian Somers     rtdata.sin_addr = mask;
99050e5c17dSBrian Somers     memcpy(cp, &rtdata, rtdata.sin_len);
99150e5c17dSBrian Somers     cp += rtdata.sin_len;
9927a6f8720SBrian Somers     rtmes.m_rtm.rtm_addrs |= RTA_NETMASK;
9937a6f8720SBrian Somers   }
9947a6f8720SBrian Somers 
9957a6f8720SBrian Somers   nb = cp - (char *) &rtmes;
9967a6f8720SBrian Somers   rtmes.m_rtm.rtm_msglen = nb;
9977a6f8720SBrian Somers   wb = ID0write(s, &rtmes, nb);
9987a6f8720SBrian Somers   if (wb < 0) {
999dd7e2610SBrian Somers     log_Printf(LogTCPIP, "bundle_SetRoute failure:\n");
1000dd7e2610SBrian Somers     log_Printf(LogTCPIP, "bundle_SetRoute:  Cmd = %s\n", cmdstr);
1001dd7e2610SBrian Somers     log_Printf(LogTCPIP, "bundle_SetRoute:  Dst = %s\n", inet_ntoa(dst));
10022cb305afSBrian Somers     log_Printf(LogTCPIP, "bundle_SetRoute:  Gateway = %s\n",
10032cb305afSBrian Somers                inet_ntoa(gateway));
1004dd7e2610SBrian Somers     log_Printf(LogTCPIP, "bundle_SetRoute:  Mask = %s\n", inet_ntoa(mask));
10057a6f8720SBrian Somers failed:
10067a6f8720SBrian Somers     if (cmd == RTM_ADD && (rtmes.m_rtm.rtm_errno == EEXIST ||
1007e43ebac1SBrian Somers                            (rtmes.m_rtm.rtm_errno == 0 && errno == EEXIST))) {
1008610b185fSBrian Somers       if (!bang) {
1009dd7e2610SBrian Somers         log_Printf(LogWARN, "Add route failed: %s already exists\n",
10103afe5ccbSBrian Somers 		  dst.s_addr == 0 ? "default" : inet_ntoa(dst));
1011610b185fSBrian Somers         result = 0;	/* Don't add to our dynamic list */
1012610b185fSBrian Somers       } else {
10137a6f8720SBrian Somers         rtmes.m_rtm.rtm_type = cmd = RTM_CHANGE;
10147a6f8720SBrian Somers         if ((wb = ID0write(s, &rtmes, nb)) < 0)
10157a6f8720SBrian Somers           goto failed;
10167a6f8720SBrian Somers       }
1017e43ebac1SBrian Somers     } else if (cmd == RTM_DELETE &&
10187a6f8720SBrian Somers              (rtmes.m_rtm.rtm_errno == ESRCH ||
10197a6f8720SBrian Somers               (rtmes.m_rtm.rtm_errno == 0 && errno == ESRCH))) {
10207a6f8720SBrian Somers       if (!bang)
1021dd7e2610SBrian Somers         log_Printf(LogWARN, "Del route failed: %s: Non-existent\n",
10227a6f8720SBrian Somers                   inet_ntoa(dst));
10232062443fSBrian Somers     } else if (rtmes.m_rtm.rtm_errno == 0) {
10242062443fSBrian Somers       if (!ssh || errno != ENETUNREACH)
1025dd7e2610SBrian Somers         log_Printf(LogWARN, "%s route failed: %s: errno: %s\n", cmdstr,
10267a6f8720SBrian Somers                    inet_ntoa(dst), strerror(errno));
10272062443fSBrian Somers     } else
1028dd7e2610SBrian Somers       log_Printf(LogWARN, "%s route failed: %s: %s\n",
10297a6f8720SBrian Somers 		 cmdstr, inet_ntoa(dst), strerror(rtmes.m_rtm.rtm_errno));
10307a6f8720SBrian Somers   }
1031dd7e2610SBrian Somers   log_Printf(LogDEBUG, "wrote %d: cmd = %s, dst = %x, gateway = %x\n",
1032fe3125a0SBrian Somers             wb, cmdstr, (unsigned)dst.s_addr, (unsigned)gateway.s_addr);
10337a6f8720SBrian Somers   close(s);
1034610b185fSBrian Somers 
1035610b185fSBrian Somers   return result;
10367a6f8720SBrian Somers }
103783d1af55SBrian Somers 
103883d1af55SBrian Somers void
10393006ec67SBrian Somers bundle_LinkClosed(struct bundle *bundle, struct datalink *dl)
10403006ec67SBrian Somers {
10413006ec67SBrian Somers   /*
10423006ec67SBrian Somers    * Our datalink has closed.
1043ea722969SBrian Somers    * CleanDatalinks() (called from DoLoop()) will remove closed
1044f6a4e748SBrian Somers    * BACKGROUND, FOREGROUND and DIRECT links.
10453b0f8d2eSBrian Somers    * If it's the last data link, enter phase DEAD.
1046ea722969SBrian Somers    *
1047ea722969SBrian Somers    * NOTE: dl may not be in our list (bundle_SendDatalink()) !
10483006ec67SBrian Somers    */
10495b8b8060SBrian Somers 
10503b0f8d2eSBrian Somers   struct datalink *odl;
10513b0f8d2eSBrian Somers   int other_links;
10525b8b8060SBrian Somers 
1053bf1d3ff6SBrian Somers   log_SetTtyCommandMode(dl);
1054bf1d3ff6SBrian Somers 
10553b0f8d2eSBrian Somers   other_links = 0;
10563b0f8d2eSBrian Somers   for (odl = bundle->links; odl; odl = odl->next)
10573b0f8d2eSBrian Somers     if (odl != dl && odl->state != DATALINK_CLOSED)
10583b0f8d2eSBrian Somers       other_links++;
10593b0f8d2eSBrian Somers 
10603b0f8d2eSBrian Somers   if (!other_links) {
106181358fa3SBrian Somers     if (dl->physical->type != PHYS_AUTO)	/* Not in -auto mode */
106203704096SBrian Somers       bundle_DownInterface(bundle);
106309206a6fSBrian Somers     fsm2initial(&bundle->ncp.ipcp.fsm);
10645563ebdeSBrian Somers     bundle_NewPhase(bundle, PHASE_DEAD);
10650f2f3eb3SBrian Somers     bundle_StopIdleTimer(bundle);
1066ab2de065SBrian Somers   }
1067455aabc3SBrian Somers }
1068455aabc3SBrian Somers 
1069455aabc3SBrian Somers void
1070ba23f397SBrian Somers bundle_Open(struct bundle *bundle, const char *name, int mask, int force)
10713006ec67SBrian Somers {
10723006ec67SBrian Somers   /*
10733006ec67SBrian Somers    * Please open the given datalink, or all if name == NULL
10743006ec67SBrian Somers    */
10753006ec67SBrian Somers   struct datalink *dl;
10763006ec67SBrian Somers 
10773006ec67SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
10783006ec67SBrian Somers     if (name == NULL || !strcasecmp(dl->name, name)) {
1079ba23f397SBrian Somers       if ((mask & dl->physical->type) &&
1080ba23f397SBrian Somers           (dl->state == DATALINK_CLOSED ||
1081ba23f397SBrian Somers            (force && dl->state == DATALINK_OPENING &&
10825e269efeSBrian Somers             dl->dial.timer.state == TIMER_RUNNING) ||
10835e269efeSBrian Somers            dl->state == DATALINK_READY)) {
10845e269efeSBrian Somers         timer_Stop(&dl->dial.timer);	/* We're finished with this */
1085565e35e5SBrian Somers         datalink_Up(dl, 1, 1);
1086ab2de065SBrian Somers         if (mask & PHYS_AUTO)
10875e269efeSBrian Somers           break;			/* Only one AUTO link at a time */
108804eaa58cSBrian Somers       }
10893006ec67SBrian Somers       if (name != NULL)
10903006ec67SBrian Somers         break;
10913006ec67SBrian Somers     }
10923006ec67SBrian Somers }
10933006ec67SBrian Somers 
10943006ec67SBrian Somers struct datalink *
10953006ec67SBrian Somers bundle2datalink(struct bundle *bundle, const char *name)
10963006ec67SBrian Somers {
10973006ec67SBrian Somers   struct datalink *dl;
10983006ec67SBrian Somers 
10993006ec67SBrian Somers   if (name != NULL) {
11003006ec67SBrian Somers     for (dl = bundle->links; dl; dl = dl->next)
11013006ec67SBrian Somers       if (!strcasecmp(dl->name, name))
11023006ec67SBrian Somers         return dl;
11033006ec67SBrian Somers   } else if (bundle->links && !bundle->links->next)
11043006ec67SBrian Somers     return bundle->links;
11053006ec67SBrian Somers 
11063006ec67SBrian Somers   return NULL;
11073006ec67SBrian Somers }
11083006ec67SBrian Somers 
11093006ec67SBrian Somers int
1110aef795ccSBrian Somers bundle_ShowLinks(struct cmdargs const *arg)
1111aef795ccSBrian Somers {
1112aef795ccSBrian Somers   struct datalink *dl;
1113ab2de065SBrian Somers   struct pppThroughput *t;
1114ab2de065SBrian Somers   int secs;
1115aef795ccSBrian Somers 
11169c53a7b1SBrian Somers   for (dl = arg->bundle->links; dl; dl = dl->next) {
1117d4156d00SBrian Somers     prompt_Printf(arg->prompt, "Name: %s [%s, %s]",
1118d4156d00SBrian Somers                   dl->name, mode2Nam(dl->physical->type), datalink_State(dl));
1119eeab6bf5SBrian Somers     if (dl->physical->link.throughput.rolling && dl->state == DATALINK_OPEN)
1120e531f89aSBrian Somers       prompt_Printf(arg->prompt, " bandwidth %d, %llu bps (%llu bytes/sec)",
1121ab2de065SBrian Somers                     dl->mp.bandwidth ? dl->mp.bandwidth :
1122ab2de065SBrian Somers                                        physical_GetSpeed(dl->physical),
1123ab2de065SBrian Somers                     dl->physical->link.throughput.OctetsPerSecond * 8,
11249c53a7b1SBrian Somers                     dl->physical->link.throughput.OctetsPerSecond);
11259c53a7b1SBrian Somers     prompt_Printf(arg->prompt, "\n");
11269c53a7b1SBrian Somers   }
1127aef795ccSBrian Somers 
1128ab2de065SBrian Somers   t = &arg->bundle->ncp.mp.link.throughput;
1129ab2de065SBrian Somers   secs = t->downtime ? 0 : throughput_uptime(t);
1130ab2de065SBrian Somers   if (secs > t->SamplePeriod)
1131ab2de065SBrian Somers     secs = t->SamplePeriod;
1132ab2de065SBrian Somers   if (secs)
1133e531f89aSBrian Somers     prompt_Printf(arg->prompt, "Currently averaging %llu bps (%llu bytes/sec)"
1134ab2de065SBrian Somers                   " over the last %d secs\n", t->OctetsPerSecond * 8,
1135ab2de065SBrian Somers                   t->OctetsPerSecond, secs);
1136ab2de065SBrian Somers 
1137aef795ccSBrian Somers   return 0;
1138aef795ccSBrian Somers }
1139ab886ad0SBrian Somers 
11401342caedSBrian Somers static const char *
11411342caedSBrian Somers optval(struct bundle *bundle, int bit)
11421342caedSBrian Somers {
11431342caedSBrian Somers   return (bundle->cfg.opt & bit) ? "enabled" : "disabled";
11441342caedSBrian Somers }
11451342caedSBrian Somers 
1146c08717dfSBrian Somers int
1147c08717dfSBrian Somers bundle_ShowStatus(struct cmdargs const *arg)
1148c08717dfSBrian Somers {
1149c08717dfSBrian Somers   int remaining;
1150c08717dfSBrian Somers 
1151c08717dfSBrian Somers   prompt_Printf(arg->prompt, "Phase %s\n", bundle_PhaseName(arg->bundle));
1152faefde08SBrian Somers   prompt_Printf(arg->prompt, " Device:        %s\n", arg->bundle->dev.Name);
1153dade2407SBrian Somers   prompt_Printf(arg->prompt, " Interface:     %s @ %lubps",
1154ab2de065SBrian Somers                 arg->bundle->iface->name, arg->bundle->bandwidth);
1155c08717dfSBrian Somers 
1156dade2407SBrian Somers   if (arg->bundle->upat) {
1157dade2407SBrian Somers     int secs = time(NULL) - arg->bundle->upat;
1158dade2407SBrian Somers 
1159dade2407SBrian Somers     prompt_Printf(arg->prompt, ", up time %d:%02d:%02d", secs / 3600,
1160dade2407SBrian Somers                   (secs / 60) % 60, secs % 60);
1161dade2407SBrian Somers   }
1162669b9965SBrian Somers   prompt_Printf(arg->prompt, "\n Queued:        %lu of %u\n",
11636c1d6731SBrian Somers                 ip_QueueLen(&arg->bundle->ncp.ipcp), arg->bundle->cfg.ifqueue);
1164dade2407SBrian Somers 
11656c1d6731SBrian Somers   prompt_Printf(arg->prompt, "\nDefaults:\n");
1166643f4904SBrian Somers   prompt_Printf(arg->prompt, " Label:         %s\n", arg->bundle->cfg.label);
1167610b185fSBrian Somers   prompt_Printf(arg->prompt, " Auth name:     %s\n",
1168610b185fSBrian Somers                 arg->bundle->cfg.auth.name);
116904eaa58cSBrian Somers 
11706f8e9f0aSBrian Somers   prompt_Printf(arg->prompt, " Choked Timer:  %ds\n",
11716f8e9f0aSBrian Somers                 arg->bundle->cfg.choked.timeout);
1172972a1bcfSBrian Somers 
1173972a1bcfSBrian Somers #ifndef NORADIUS
1174972a1bcfSBrian Somers   radius_Show(&arg->bundle->radius, arg->prompt);
1175972a1bcfSBrian Somers #endif
1176972a1bcfSBrian Somers 
1177c08717dfSBrian Somers   prompt_Printf(arg->prompt, " Idle Timer:    ");
1178dade2407SBrian Somers   if (arg->bundle->cfg.idle.timeout) {
1179dade2407SBrian Somers     prompt_Printf(arg->prompt, "%ds", arg->bundle->cfg.idle.timeout);
1180dade2407SBrian Somers     if (arg->bundle->cfg.idle.min_timeout)
1181dade2407SBrian Somers       prompt_Printf(arg->prompt, ", min %ds",
1182dade2407SBrian Somers                     arg->bundle->cfg.idle.min_timeout);
1183c08717dfSBrian Somers     remaining = bundle_RemainingIdleTime(arg->bundle);
1184c08717dfSBrian Somers     if (remaining != -1)
1185c08717dfSBrian Somers       prompt_Printf(arg->prompt, " (%ds remaining)", remaining);
1186c08717dfSBrian Somers     prompt_Printf(arg->prompt, "\n");
1187c08717dfSBrian Somers   } else
1188c08717dfSBrian Somers     prompt_Printf(arg->prompt, "disabled\n");
1189ce828a6eSBrian Somers   prompt_Printf(arg->prompt, " MTU:           ");
1190ce828a6eSBrian Somers   if (arg->bundle->cfg.mtu)
1191ce828a6eSBrian Somers     prompt_Printf(arg->prompt, "%d\n", arg->bundle->cfg.mtu);
1192ce828a6eSBrian Somers   else
1193ce828a6eSBrian Somers     prompt_Printf(arg->prompt, "unspecified\n");
1194c08717dfSBrian Somers 
11953afe5ccbSBrian Somers   prompt_Printf(arg->prompt, " sendpipe:      ");
11963afe5ccbSBrian Somers   if (arg->bundle->ncp.ipcp.cfg.sendpipe > 0)
1197ab2de065SBrian Somers     prompt_Printf(arg->prompt, "%-20ld", arg->bundle->ncp.ipcp.cfg.sendpipe);
11983afe5ccbSBrian Somers   else
1199ab2de065SBrian Somers     prompt_Printf(arg->prompt, "unspecified         ");
12003afe5ccbSBrian Somers   prompt_Printf(arg->prompt, " recvpipe:      ");
12013afe5ccbSBrian Somers   if (arg->bundle->ncp.ipcp.cfg.recvpipe > 0)
12023afe5ccbSBrian Somers     prompt_Printf(arg->prompt, "%ld\n", arg->bundle->ncp.ipcp.cfg.recvpipe);
12033afe5ccbSBrian Somers   else
12043afe5ccbSBrian Somers     prompt_Printf(arg->prompt, "unspecified\n");
12053afe5ccbSBrian Somers 
1206ab2de065SBrian Somers   prompt_Printf(arg->prompt, " Sticky Routes: %-20.20s",
1207610b185fSBrian Somers                 optval(arg->bundle, OPT_SROUTES));
12081342caedSBrian Somers   prompt_Printf(arg->prompt, " ID check:      %s\n",
12091342caedSBrian Somers                 optval(arg->bundle, OPT_IDCHECK));
1210ab2de065SBrian Somers   prompt_Printf(arg->prompt, " Keep-Session:  %-20.20s",
1211ac685e31SBrian Somers                 optval(arg->bundle, OPT_KEEPSESSION));
12121342caedSBrian Somers   prompt_Printf(arg->prompt, " Loopback:      %s\n",
12131342caedSBrian Somers                 optval(arg->bundle, OPT_LOOPBACK));
1214ab2de065SBrian Somers   prompt_Printf(arg->prompt, " PasswdAuth:    %-20.20s",
12151342caedSBrian Somers                 optval(arg->bundle, OPT_PASSWDAUTH));
12161342caedSBrian Somers   prompt_Printf(arg->prompt, " Proxy:         %s\n",
12171342caedSBrian Somers                 optval(arg->bundle, OPT_PROXY));
1218ab2de065SBrian Somers   prompt_Printf(arg->prompt, " Proxyall:      %-20.20s",
12193afe5ccbSBrian Somers                 optval(arg->bundle, OPT_PROXYALL));
12201342caedSBrian Somers   prompt_Printf(arg->prompt, " Throughput:    %s\n",
12211342caedSBrian Somers                 optval(arg->bundle, OPT_THROUGHPUT));
1222ab2de065SBrian Somers   prompt_Printf(arg->prompt, " Utmp Logging:  %-20.20s",
12231342caedSBrian Somers                 optval(arg->bundle, OPT_UTMP));
12248fa6ebe4SBrian Somers   prompt_Printf(arg->prompt, " Iface-Alias:   %s\n",
12258fa6ebe4SBrian Somers                 optval(arg->bundle, OPT_IFACEALIAS));
12261342caedSBrian Somers 
1227c08717dfSBrian Somers   return 0;
1228c08717dfSBrian Somers }
1229c08717dfSBrian Somers 
1230ab886ad0SBrian Somers static void
1231ab886ad0SBrian Somers bundle_IdleTimeout(void *v)
1232ab886ad0SBrian Somers {
1233ab886ad0SBrian Somers   struct bundle *bundle = (struct bundle *)v;
1234ab886ad0SBrian Somers 
1235b42135deSBrian Somers   log_Printf(LogPHASE, "Idle timer expired\n");
123604eaa58cSBrian Somers   bundle_StopIdleTimer(bundle);
12379c81b87dSBrian Somers   bundle_Close(bundle, NULL, CLOSE_STAYDOWN);
1238ab886ad0SBrian Somers }
1239ab886ad0SBrian Somers 
1240ab886ad0SBrian Somers /*
1241ab886ad0SBrian Somers  *  Start Idle timer. If timeout is reached, we call bundle_Close() to
1242ab886ad0SBrian Somers  *  close LCP and link.
1243ab886ad0SBrian Somers  */
1244ab886ad0SBrian Somers void
1245ab886ad0SBrian Somers bundle_StartIdleTimer(struct bundle *bundle)
1246ab886ad0SBrian Somers {
1247dd7e2610SBrian Somers   timer_Stop(&bundle->idle.timer);
1248ff0f9439SBrian Somers   if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) !=
1249dade2407SBrian Somers       bundle->phys_type.open && bundle->cfg.idle.timeout) {
1250dade2407SBrian Somers     int secs;
1251dade2407SBrian Somers 
1252dade2407SBrian Somers     secs = bundle->cfg.idle.timeout;
1253dade2407SBrian Somers     if (bundle->cfg.idle.min_timeout > secs && bundle->upat) {
1254dade2407SBrian Somers       int up = time(NULL) - bundle->upat;
1255dade2407SBrian Somers 
1256dade2407SBrian Somers       if ((long long)bundle->cfg.idle.min_timeout - up > (long long)secs)
1257dade2407SBrian Somers         secs = bundle->cfg.idle.min_timeout - up;
1258dade2407SBrian Somers     }
125993ee0ff2SBrian Somers     bundle->idle.timer.func = bundle_IdleTimeout;
12603b0f8d2eSBrian Somers     bundle->idle.timer.name = "idle";
1261dade2407SBrian Somers     bundle->idle.timer.load = secs * SECTICKS;
126293ee0ff2SBrian Somers     bundle->idle.timer.arg = bundle;
1263dd7e2610SBrian Somers     timer_Start(&bundle->idle.timer);
1264dade2407SBrian Somers     bundle->idle.done = time(NULL) + secs;
1265ab886ad0SBrian Somers   }
1266ab886ad0SBrian Somers }
1267ab886ad0SBrian Somers 
1268ab886ad0SBrian Somers void
1269dade2407SBrian Somers bundle_SetIdleTimer(struct bundle *bundle, int timeout, int min_timeout)
1270ab886ad0SBrian Somers {
1271dade2407SBrian Somers   bundle->cfg.idle.timeout = timeout;
1272dade2407SBrian Somers   if (min_timeout >= 0)
1273dade2407SBrian Somers     bundle->cfg.idle.min_timeout = min_timeout;
1274ab886ad0SBrian Somers   if (bundle_LinkIsUp(bundle))
1275ab886ad0SBrian Somers     bundle_StartIdleTimer(bundle);
1276ab886ad0SBrian Somers }
1277ab886ad0SBrian Somers 
1278ab886ad0SBrian Somers void
1279ab886ad0SBrian Somers bundle_StopIdleTimer(struct bundle *bundle)
1280ab886ad0SBrian Somers {
1281dd7e2610SBrian Somers   timer_Stop(&bundle->idle.timer);
12824a632c80SBrian Somers   bundle->idle.done = 0;
1283ab886ad0SBrian Somers }
1284ab886ad0SBrian Somers 
128504eaa58cSBrian Somers static int
1286ab886ad0SBrian Somers bundle_RemainingIdleTime(struct bundle *bundle)
1287ab886ad0SBrian Somers {
128893ee0ff2SBrian Somers   if (bundle->idle.done)
128993ee0ff2SBrian Somers     return bundle->idle.done - time(NULL);
1290ab886ad0SBrian Somers   return -1;
1291ab886ad0SBrian Somers }
12923b0f8d2eSBrian Somers 
12933b0f8d2eSBrian Somers int
12943b0f8d2eSBrian Somers bundle_IsDead(struct bundle *bundle)
12953b0f8d2eSBrian Somers {
12963b0f8d2eSBrian Somers   return !bundle->links || (bundle->phase == PHASE_DEAD && bundle->CleaningUp);
12973b0f8d2eSBrian Somers }
1298b6217683SBrian Somers 
129904eaa58cSBrian Somers static struct datalink *
130004eaa58cSBrian Somers bundle_DatalinkLinkout(struct bundle *bundle, struct datalink *dl)
1301cd7bd93aSBrian Somers {
1302cd7bd93aSBrian Somers   struct datalink **dlp;
1303cd7bd93aSBrian Somers 
1304cd7bd93aSBrian Somers   for (dlp = &bundle->links; *dlp; dlp = &(*dlp)->next)
1305cd7bd93aSBrian Somers     if (*dlp == dl) {
130604eaa58cSBrian Somers       *dlp = dl->next;
130704eaa58cSBrian Somers       dl->next = NULL;
130804eaa58cSBrian Somers       bundle_LinksRemoved(bundle);
130904eaa58cSBrian Somers       return dl;
1310cd7bd93aSBrian Somers     }
131104eaa58cSBrian Somers 
131204eaa58cSBrian Somers   return NULL;
131304eaa58cSBrian Somers }
131404eaa58cSBrian Somers 
131504eaa58cSBrian Somers static void
131604eaa58cSBrian Somers bundle_DatalinkLinkin(struct bundle *bundle, struct datalink *dl)
131704eaa58cSBrian Somers {
131804eaa58cSBrian Somers   struct datalink **dlp = &bundle->links;
131904eaa58cSBrian Somers 
132004eaa58cSBrian Somers   while (*dlp)
132104eaa58cSBrian Somers     dlp = &(*dlp)->next;
132204eaa58cSBrian Somers 
132304eaa58cSBrian Somers   *dlp = dl;
132404eaa58cSBrian Somers   dl->next = NULL;
132504eaa58cSBrian Somers 
132604eaa58cSBrian Somers   bundle_LinkAdded(bundle, dl);
1327ab2de065SBrian Somers   mp_CheckAutoloadTimer(&bundle->ncp.mp);
1328565e35e5SBrian Somers }
1329565e35e5SBrian Somers 
1330565e35e5SBrian Somers void
1331565e35e5SBrian Somers bundle_CleanDatalinks(struct bundle *bundle)
1332565e35e5SBrian Somers {
1333565e35e5SBrian Somers   struct datalink **dlp = &bundle->links;
133404eaa58cSBrian Somers   int found = 0;
1335565e35e5SBrian Somers 
1336565e35e5SBrian Somers   while (*dlp)
1337565e35e5SBrian Somers     if ((*dlp)->state == DATALINK_CLOSED &&
1338f6a4e748SBrian Somers         (*dlp)->physical->type &
1339f6a4e748SBrian Somers         (PHYS_DIRECT|PHYS_BACKGROUND|PHYS_FOREGROUND)) {
1340565e35e5SBrian Somers       *dlp = datalink_Destroy(*dlp);
134104eaa58cSBrian Somers       found++;
134204eaa58cSBrian Somers     } else
1343565e35e5SBrian Somers       dlp = &(*dlp)->next;
134404eaa58cSBrian Somers 
134504eaa58cSBrian Somers   if (found)
134604eaa58cSBrian Somers     bundle_LinksRemoved(bundle);
134704eaa58cSBrian Somers }
134804eaa58cSBrian Somers 
134904eaa58cSBrian Somers int
135004eaa58cSBrian Somers bundle_DatalinkClone(struct bundle *bundle, struct datalink *dl,
135104eaa58cSBrian Somers                      const char *name)
135204eaa58cSBrian Somers {
135304eaa58cSBrian Somers   if (bundle2datalink(bundle, name)) {
135404eaa58cSBrian Somers     log_Printf(LogWARN, "Clone: %s: name already exists\n", name);
135504eaa58cSBrian Somers     return 0;
135604eaa58cSBrian Somers   }
135704eaa58cSBrian Somers 
135804eaa58cSBrian Somers   bundle_DatalinkLinkin(bundle, datalink_Clone(dl, name));
135904eaa58cSBrian Somers   return 1;
136004eaa58cSBrian Somers }
136104eaa58cSBrian Somers 
136204eaa58cSBrian Somers void
136304eaa58cSBrian Somers bundle_DatalinkRemove(struct bundle *bundle, struct datalink *dl)
136404eaa58cSBrian Somers {
136504eaa58cSBrian Somers   dl = bundle_DatalinkLinkout(bundle, dl);
136604eaa58cSBrian Somers   if (dl)
136704eaa58cSBrian Somers     datalink_Destroy(dl);
1368cd7bd93aSBrian Somers }
136949052c95SBrian Somers 
137049052c95SBrian Somers void
137149052c95SBrian Somers bundle_SetLabel(struct bundle *bundle, const char *label)
137249052c95SBrian Somers {
137349052c95SBrian Somers   if (label)
137449052c95SBrian Somers     strncpy(bundle->cfg.label, label, sizeof bundle->cfg.label - 1);
137549052c95SBrian Somers   else
137649052c95SBrian Somers     *bundle->cfg.label = '\0';
137749052c95SBrian Somers }
137849052c95SBrian Somers 
137949052c95SBrian Somers const char *
138049052c95SBrian Somers bundle_GetLabel(struct bundle *bundle)
138149052c95SBrian Somers {
138249052c95SBrian Somers   return *bundle->cfg.label ? bundle->cfg.label : NULL;
138349052c95SBrian Somers }
13841fa665f5SBrian Somers 
13852cb305afSBrian Somers int
13862cb305afSBrian Somers bundle_LinkSize()
13871fa665f5SBrian Somers {
138896c9bb21SBrian Somers   struct iovec iov[SCATTER_SEGMENTS];
13892cb305afSBrian Somers   int niov, expect, f;
139087c3786eSBrian Somers 
139196c9bb21SBrian Somers   iov[0].iov_len = strlen(Version) + 1;
13922cb305afSBrian Somers   iov[0].iov_base = NULL;
13932cb305afSBrian Somers   niov = 1;
13942cb305afSBrian Somers   if (datalink2iov(NULL, iov, &niov, SCATTER_SEGMENTS, NULL, NULL) == -1) {
13952cb305afSBrian Somers     log_Printf(LogERROR, "Cannot determine space required for link\n");
13962cb305afSBrian Somers     return 0;
139754cd8e13SBrian Somers   }
13986f384573SBrian Somers 
139996c9bb21SBrian Somers   for (f = expect = 0; f < niov; f++)
140096c9bb21SBrian Somers     expect += iov[f].iov_len;
140196c9bb21SBrian Somers 
14022cb305afSBrian Somers   return expect;
140387c3786eSBrian Somers }
140496c9bb21SBrian Somers 
14052cb305afSBrian Somers void
14062cb305afSBrian Somers bundle_ReceiveDatalink(struct bundle *bundle, int s)
14072cb305afSBrian Somers {
14082cb305afSBrian Somers   char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int) * SEND_MAXFD];
1409cbee9754SBrian Somers   int niov, expect, f, *fd, nfd, onfd, got;
14102cb305afSBrian Somers   struct iovec iov[SCATTER_SEGMENTS];
14112cb305afSBrian Somers   struct cmsghdr *cmsg;
14122cb305afSBrian Somers   struct msghdr msg;
14132cb305afSBrian Somers   struct datalink *dl;
14142cb305afSBrian Somers   pid_t pid;
14152cb305afSBrian Somers 
14162cb305afSBrian Somers   log_Printf(LogPHASE, "Receiving datalink\n");
14172cb305afSBrian Somers 
14182cb305afSBrian Somers   /*
14192cb305afSBrian Somers    * Create our scatter/gather array - passing NULL gets the space
14202cb305afSBrian Somers    * allocation requirement rather than actually flattening the
14212cb305afSBrian Somers    * structures.
14222cb305afSBrian Somers    */
14232cb305afSBrian Somers   iov[0].iov_len = strlen(Version) + 1;
14242cb305afSBrian Somers   iov[0].iov_base = NULL;
14252cb305afSBrian Somers   niov = 1;
14262cb305afSBrian Somers   if (datalink2iov(NULL, iov, &niov, SCATTER_SEGMENTS, NULL, NULL) == -1) {
14272cb305afSBrian Somers     log_Printf(LogERROR, "Cannot determine space required for link\n");
14282cb305afSBrian Somers     return;
14292cb305afSBrian Somers   }
14302cb305afSBrian Somers 
14312cb305afSBrian Somers   /* Allocate the scatter/gather array for recvmsg() */
14322cb305afSBrian Somers   for (f = expect = 0; f < niov; f++) {
14332cb305afSBrian Somers     if ((iov[f].iov_base = malloc(iov[f].iov_len)) == NULL) {
14342cb305afSBrian Somers       log_Printf(LogERROR, "Cannot allocate space to receive link\n");
14352cb305afSBrian Somers       return;
14362cb305afSBrian Somers     }
1437cbee9754SBrian Somers     if (f)
14382cb305afSBrian Somers       expect += iov[f].iov_len;
14392cb305afSBrian Somers   }
14402cb305afSBrian Somers 
14412cb305afSBrian Somers   /* Set up our message */
14422cb305afSBrian Somers   cmsg = (struct cmsghdr *)cmsgbuf;
14432cb305afSBrian Somers   cmsg->cmsg_len = sizeof cmsgbuf;
14442cb305afSBrian Somers   cmsg->cmsg_level = SOL_SOCKET;
14452cb305afSBrian Somers   cmsg->cmsg_type = 0;
14462cb305afSBrian Somers 
144796c9bb21SBrian Somers   memset(&msg, '\0', sizeof msg);
14482cb305afSBrian Somers   msg.msg_name = NULL;
14492cb305afSBrian Somers   msg.msg_namelen = 0;
145096c9bb21SBrian Somers   msg.msg_iov = iov;
1451cbee9754SBrian Somers   msg.msg_iovlen = 1;		/* Only send the version at the first pass */
145296c9bb21SBrian Somers   msg.msg_control = cmsgbuf;
145396c9bb21SBrian Somers   msg.msg_controllen = sizeof cmsgbuf;
145496c9bb21SBrian Somers 
1455209dc102SBrian Somers   log_Printf(LogDEBUG, "Expecting %u scatter/gather bytes\n",
1456209dc102SBrian Somers              (unsigned)iov[0].iov_len);
14572cb305afSBrian Somers 
1458cbee9754SBrian Somers   if ((got = recvmsg(s, &msg, MSG_WAITALL)) != iov[0].iov_len) {
1459cbee9754SBrian Somers     if (got == -1)
146096c9bb21SBrian Somers       log_Printf(LogERROR, "Failed recvmsg: %s\n", strerror(errno));
146196c9bb21SBrian Somers     else
1462209dc102SBrian Somers       log_Printf(LogERROR, "Failed recvmsg: Got %d, not %u\n",
1463209dc102SBrian Somers                  got, (unsigned)iov[0].iov_len);
146496c9bb21SBrian Somers     while (niov--)
146596c9bb21SBrian Somers       free(iov[niov].iov_base);
146696c9bb21SBrian Somers     return;
146796c9bb21SBrian Somers   }
146896c9bb21SBrian Somers 
14692cb305afSBrian Somers   if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
14702cb305afSBrian Somers     log_Printf(LogERROR, "Recvmsg: no descriptors received !\n");
14712cb305afSBrian Somers     while (niov--)
14722cb305afSBrian Somers       free(iov[niov].iov_base);
14732cb305afSBrian Somers     return;
147487c3786eSBrian Somers   }
147587c3786eSBrian Somers 
14762cb305afSBrian Somers   fd = (int *)(cmsg + 1);
14772cb305afSBrian Somers   nfd = (cmsg->cmsg_len - sizeof *cmsg) / sizeof(int);
14782cb305afSBrian Somers 
14792cb305afSBrian Somers   if (nfd < 2) {
14808e7bd08eSBrian Somers     log_Printf(LogERROR, "Recvmsg: %d descriptor%s received (too few) !\n",
14812cb305afSBrian Somers                nfd, nfd == 1 ? "" : "s");
14822cb305afSBrian Somers     while (nfd--)
14832cb305afSBrian Somers       close(fd[nfd]);
1484ad5b0e8bSBrian Somers     while (niov--)
1485ad5b0e8bSBrian Somers       free(iov[niov].iov_base);
1486ad5b0e8bSBrian Somers     return;
148754cd8e13SBrian Somers   }
148854cd8e13SBrian Somers 
148987c3786eSBrian Somers   /*
1490cbee9754SBrian Somers    * We've successfully received two or more open file descriptors
1491cbee9754SBrian Somers    * through our socket, plus a version string.  Make sure it's the
1492cbee9754SBrian Somers    * correct version, and drop the connection if it's not.
149387c3786eSBrian Somers    */
149496c9bb21SBrian Somers   if (strncmp(Version, iov[0].iov_base, iov[0].iov_len)) {
149596c9bb21SBrian Somers     log_Printf(LogWARN, "Cannot receive datalink, incorrect version"
149696c9bb21SBrian Somers                " (\"%.*s\", not \"%s\")\n", (int)iov[0].iov_len,
14977d81ddf5SBrian Somers                (char *)iov[0].iov_base, Version);
14982cb305afSBrian Somers     while (nfd--)
14992cb305afSBrian Somers       close(fd[nfd]);
150096c9bb21SBrian Somers     while (niov--)
150196c9bb21SBrian Somers       free(iov[niov].iov_base);
150296c9bb21SBrian Somers     return;
150396c9bb21SBrian Somers   }
150496c9bb21SBrian Somers 
1505cbee9754SBrian Somers   /*
1506cbee9754SBrian Somers    * Everything looks good.  Send the other side our process id so that
1507cbee9754SBrian Somers    * they can transfer lock ownership, and wait for them to send the
1508cbee9754SBrian Somers    * actual link data.
1509cbee9754SBrian Somers    */
1510cbee9754SBrian Somers   pid = getpid();
1511cbee9754SBrian Somers   if ((got = write(fd[1], &pid, sizeof pid)) != sizeof pid) {
1512cbee9754SBrian Somers     if (got == -1)
1513cbee9754SBrian Somers       log_Printf(LogERROR, "Failed write: %s\n", strerror(errno));
1514cbee9754SBrian Somers     else
1515cbee9754SBrian Somers       log_Printf(LogERROR, "Failed write: Got %d, not %d\n", got,
1516cbee9754SBrian Somers                  (int)(sizeof pid));
1517cbee9754SBrian Somers     while (nfd--)
1518cbee9754SBrian Somers       close(fd[nfd]);
1519cbee9754SBrian Somers     while (niov--)
1520cbee9754SBrian Somers       free(iov[niov].iov_base);
1521cbee9754SBrian Somers     return;
1522cbee9754SBrian Somers   }
1523cbee9754SBrian Somers 
1524cbee9754SBrian Somers   if ((got = readv(fd[1], iov + 1, niov - 1)) != expect) {
1525cbee9754SBrian Somers     if (got == -1)
1526cbee9754SBrian Somers       log_Printf(LogERROR, "Failed write: %s\n", strerror(errno));
1527cbee9754SBrian Somers     else
1528cbee9754SBrian Somers       log_Printf(LogERROR, "Failed write: Got %d, not %d\n", got, expect);
1529cbee9754SBrian Somers     while (nfd--)
1530cbee9754SBrian Somers       close(fd[nfd]);
1531cbee9754SBrian Somers     while (niov--)
1532cbee9754SBrian Somers       free(iov[niov].iov_base);
1533cbee9754SBrian Somers     return;
1534cbee9754SBrian Somers   }
1535cbee9754SBrian Somers   close(fd[1]);
1536cbee9754SBrian Somers 
15372cb305afSBrian Somers   onfd = nfd;	/* We've got this many in our array */
15388e7bd08eSBrian Somers   nfd -= 2;	/* Don't include p->fd and our reply descriptor */
15392cb305afSBrian Somers   niov = 1;	/* Skip the version id */
154087c3786eSBrian Somers   dl = iov2datalink(bundle, iov, &niov, sizeof iov / sizeof *iov, fd[0],
15412cb305afSBrian Somers                     fd + 2, &nfd);
1542b7c5748eSBrian Somers   if (dl) {
15432cb305afSBrian Somers 
154487c3786eSBrian Somers     if (nfd) {
154587c3786eSBrian Somers       log_Printf(LogERROR, "bundle_ReceiveDatalink: Failed to handle %d "
15462cb305afSBrian Somers                  "auxiliary file descriptors (%d remain)\n", onfd, nfd);
154787c3786eSBrian Somers       datalink_Destroy(dl);
154887c3786eSBrian Somers       while (nfd--)
154987c3786eSBrian Somers         close(fd[onfd--]);
15502cb305afSBrian Somers       close(fd[0]);
155187c3786eSBrian Somers     } else {
155204eaa58cSBrian Somers       bundle_DatalinkLinkin(bundle, dl);
1553b7c5748eSBrian Somers       datalink_AuthOk(dl);
1554ab2de065SBrian Somers       bundle_CalculateBandwidth(dl->bundle);
155587c3786eSBrian Somers     }
155687c3786eSBrian Somers   } else {
155787c3786eSBrian Somers     while (nfd--)
155887c3786eSBrian Somers       close(fd[onfd--]);
155987c3786eSBrian Somers     close(fd[0]);
15602cb305afSBrian Somers     close(fd[1]);
156187c3786eSBrian Somers   }
156296c9bb21SBrian Somers 
156396c9bb21SBrian Somers   free(iov[0].iov_base);
15641fa665f5SBrian Somers }
15651fa665f5SBrian Somers 
15661fa665f5SBrian Somers void
156796c9bb21SBrian Somers bundle_SendDatalink(struct datalink *dl, int s, struct sockaddr_un *sun)
15681fa665f5SBrian Somers {
15692cb305afSBrian Somers   char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int) * SEND_MAXFD];
15702cb305afSBrian Somers   const char *constlock;
15712cb305afSBrian Somers   char *lock;
157287c3786eSBrian Somers   struct cmsghdr *cmsg;
157396c9bb21SBrian Somers   struct msghdr msg;
157496c9bb21SBrian Somers   struct iovec iov[SCATTER_SEGMENTS];
15752cb305afSBrian Somers   int niov, f, expect, newsid, fd[SEND_MAXFD], nfd, reply[2], got;
157685fd273aSBrian Somers   pid_t newpid;
15776f384573SBrian Somers 
1578dd7e2610SBrian Somers   log_Printf(LogPHASE, "Transmitting datalink %s\n", dl->name);
15796f384573SBrian Somers 
15802cb305afSBrian Somers   /* Record the base device name for a lock transfer later */
15812cb305afSBrian Somers   constlock = physical_LockedDevice(dl->physical);
15822cb305afSBrian Somers   if (constlock) {
15832cb305afSBrian Somers     lock = alloca(strlen(constlock) + 1);
15842cb305afSBrian Somers     strcpy(lock, constlock);
15852cb305afSBrian Somers   } else
15862cb305afSBrian Somers     lock = NULL;
15872cb305afSBrian Somers 
158804eaa58cSBrian Somers   bundle_LinkClosed(dl->bundle, dl);
15890f2f3eb3SBrian Somers   bundle_DatalinkLinkout(dl->bundle, dl);
1590ea722969SBrian Somers 
159196c9bb21SBrian Somers   /* Build our scatter/gather array */
159296c9bb21SBrian Somers   iov[0].iov_len = strlen(Version) + 1;
159396c9bb21SBrian Somers   iov[0].iov_base = strdup(Version);
159496c9bb21SBrian Somers   niov = 1;
159587c3786eSBrian Somers   nfd = 0;
15966f384573SBrian Somers 
15972cb305afSBrian Somers   fd[0] = datalink2iov(dl, iov, &niov, SCATTER_SEGMENTS, fd + 2, &nfd);
15986f384573SBrian Somers 
15992cb305afSBrian Somers   if (fd[0] != -1 && socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, reply) != -1) {
16002cb305afSBrian Somers     /*
16012cb305afSBrian Somers      * fd[1] is used to get the peer process id back, then to confirm that
16022cb305afSBrian Somers      * we've transferred any device locks to that process id.
16032cb305afSBrian Somers      */
16042cb305afSBrian Somers     fd[1] = reply[1];
160587c3786eSBrian Somers 
16062cb305afSBrian Somers     nfd += 2;			/* Include fd[0] and fd[1] */
160796c9bb21SBrian Somers     memset(&msg, '\0', sizeof msg);
160854cd8e13SBrian Somers 
16092cb305afSBrian Somers     msg.msg_name = NULL;
16102cb305afSBrian Somers     msg.msg_namelen = 0;
1611cbee9754SBrian Somers     /*
1612cbee9754SBrian Somers      * Only send the version to start...  We used to send the whole lot, but
1613cbee9754SBrian Somers      * this caused problems with our RECVBUF size as a single link is about
1614cbee9754SBrian Somers      * 22k !  This way, we should bump into no limits.
1615cbee9754SBrian Somers      */
1616cbee9754SBrian Somers     msg.msg_iovlen = 1;
161796c9bb21SBrian Somers     msg.msg_iov = iov;
16182cb305afSBrian Somers     msg.msg_control = cmsgbuf;
16192cb305afSBrian Somers     msg.msg_controllen = sizeof *cmsg + sizeof(int) * nfd;
16202cb305afSBrian Somers     msg.msg_flags = 0;
162154cd8e13SBrian Somers 
16222cb305afSBrian Somers     cmsg = (struct cmsghdr *)cmsgbuf;
16232cb305afSBrian Somers     cmsg->cmsg_len = msg.msg_controllen;
162454cd8e13SBrian Somers     cmsg->cmsg_level = SOL_SOCKET;
162554cd8e13SBrian Somers     cmsg->cmsg_type = SCM_RIGHTS;
162687c3786eSBrian Somers 
16272cb305afSBrian Somers     for (f = 0; f < nfd; f++)
16282cb305afSBrian Somers       *((int *)(cmsg + 1) + f) = fd[f];
162947723d29SBrian Somers 
1630cbee9754SBrian Somers     for (f = 1, expect = 0; f < niov; f++)
163196c9bb21SBrian Somers       expect += iov[f].iov_len;
163247723d29SBrian Somers 
1633cbee9754SBrian Somers     if (setsockopt(reply[0], SOL_SOCKET, SO_SNDBUF, &expect, sizeof(int)) == -1)
1634cbee9754SBrian Somers       log_Printf(LogERROR, "setsockopt(SO_RCVBUF, %d): %s\n", expect,
1635cbee9754SBrian Somers                  strerror(errno));
1636cbee9754SBrian Somers     if (setsockopt(reply[1], SOL_SOCKET, SO_RCVBUF, &expect, sizeof(int)) == -1)
1637cbee9754SBrian Somers       log_Printf(LogERROR, "setsockopt(SO_RCVBUF, %d): %s\n", expect,
1638cbee9754SBrian Somers                  strerror(errno));
1639cbee9754SBrian Somers 
1640209dc102SBrian Somers     log_Printf(LogDEBUG, "Sending %d descriptor%s and %u bytes in scatter"
1641209dc102SBrian Somers                "/gather array\n", nfd, nfd == 1 ? "" : "s",
1642209dc102SBrian Somers                (unsigned)iov[0].iov_len);
164347723d29SBrian Somers 
16442cb305afSBrian Somers     if ((got = sendmsg(s, &msg, 0)) == -1)
16452cb305afSBrian Somers       log_Printf(LogERROR, "Failed sendmsg: %s: %s\n",
16462cb305afSBrian Somers                  sun->sun_path, strerror(errno));
1647cbee9754SBrian Somers     else if (got != iov[0].iov_len)
1648209dc102SBrian Somers       log_Printf(LogERROR, "%s: Failed initial sendmsg: Only sent %d of %u\n",
1649209dc102SBrian Somers                  sun->sun_path, got, (unsigned)iov[0].iov_len);
16502cb305afSBrian Somers     else {
16518e7bd08eSBrian Somers       /* We must get the ACK before closing the descriptor ! */
16522cb305afSBrian Somers       int res;
16532cb305afSBrian Somers 
1654cbee9754SBrian Somers       if ((got = read(reply[0], &newpid, sizeof newpid)) == sizeof newpid) {
1655cbee9754SBrian Somers         log_Printf(LogDEBUG, "Received confirmation from pid %d\n",
1656cbee9754SBrian Somers                    (int)newpid);
16572cb305afSBrian Somers         if (lock && (res = ID0uu_lock_txfr(lock, newpid)) != UU_LOCK_OK)
1658b42135deSBrian Somers             log_Printf(LogERROR, "uu_lock_txfr: %s\n", uu_lockerr(res));
16592cb305afSBrian Somers 
1660cbee9754SBrian Somers         log_Printf(LogDEBUG, "Transmitting link (%d bytes)\n", expect);
1661cbee9754SBrian Somers         if ((got = writev(reply[0], iov + 1, niov - 1)) != expect) {
1662cbee9754SBrian Somers           if (got == -1)
1663cbee9754SBrian Somers             log_Printf(LogERROR, "%s: Failed writev: %s\n",
1664cbee9754SBrian Somers                        sun->sun_path, strerror(errno));
1665cbee9754SBrian Somers           else
1666cbee9754SBrian Somers             log_Printf(LogERROR, "%s: Failed writev: Wrote %d of %d\n",
1667cbee9754SBrian Somers                        sun->sun_path, got, expect);
1668cbee9754SBrian Somers         }
1669cbee9754SBrian Somers       } else if (got == -1)
1670cbee9754SBrian Somers         log_Printf(LogERROR, "%s: Failed socketpair read: %s\n",
1671cbee9754SBrian Somers                    sun->sun_path, strerror(errno));
1672cbee9754SBrian Somers       else
1673cbee9754SBrian Somers         log_Printf(LogERROR, "%s: Failed socketpair read: Got %d of %d\n",
1674cbee9754SBrian Somers                    sun->sun_path, got, (int)(sizeof newpid));
16752cb305afSBrian Somers     }
16762cb305afSBrian Somers 
16772cb305afSBrian Somers     close(reply[0]);
16782cb305afSBrian Somers     close(reply[1]);
167954cd8e13SBrian Somers 
1680ac685e31SBrian Somers     newsid = Enabled(dl->bundle, OPT_KEEPSESSION) ||
168187c3786eSBrian Somers              tcgetpgrp(fd[0]) == getpgrp();
168287c3786eSBrian Somers     while (nfd)
168387c3786eSBrian Somers       close(fd[--nfd]);
16841384bd27SBrian Somers     if (newsid)
16852cb305afSBrian Somers       bundle_setsid(dl->bundle, got != -1);
168647723d29SBrian Somers   }
168785fd273aSBrian Somers   close(s);
168896c9bb21SBrian Somers 
168996c9bb21SBrian Somers   while (niov--)
169096c9bb21SBrian Somers     free(iov[niov].iov_base);
16911fa665f5SBrian Somers }
1692dd0645c5SBrian Somers 
1693dd0645c5SBrian Somers int
169458d55334SBrian Somers bundle_RenameDatalink(struct bundle *bundle, struct datalink *ndl,
169558d55334SBrian Somers                       const char *name)
169658d55334SBrian Somers {
169758d55334SBrian Somers   struct datalink *dl;
169858d55334SBrian Somers 
169958d55334SBrian Somers   if (!strcasecmp(ndl->name, name))
170058d55334SBrian Somers     return 1;
170158d55334SBrian Somers 
170258d55334SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
170358d55334SBrian Somers     if (!strcasecmp(dl->name, name))
170458d55334SBrian Somers       return 0;
170558d55334SBrian Somers 
170658d55334SBrian Somers   datalink_Rename(ndl, name);
170758d55334SBrian Somers   return 1;
170858d55334SBrian Somers }
170958d55334SBrian Somers 
171058d55334SBrian Somers int
1711dd0645c5SBrian Somers bundle_SetMode(struct bundle *bundle, struct datalink *dl, int mode)
1712dd0645c5SBrian Somers {
1713dd0645c5SBrian Somers   int omode;
1714dd0645c5SBrian Somers 
1715dd0645c5SBrian Somers   omode = dl->physical->type;
1716dd0645c5SBrian Somers   if (omode == mode)
1717dd0645c5SBrian Somers     return 1;
1718dd0645c5SBrian Somers 
1719ff0f9439SBrian Somers   if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO))
1720ff0f9439SBrian Somers     /* First auto link */
1721dd0645c5SBrian Somers     if (bundle->ncp.ipcp.peer_ip.s_addr == INADDR_ANY) {
1722ff0f9439SBrian Somers       log_Printf(LogWARN, "You must `set ifaddr' or `open' before"
1723ff0f9439SBrian Somers                  " changing mode to %s\n", mode2Nam(mode));
1724dd0645c5SBrian Somers       return 0;
1725dd0645c5SBrian Somers     }
1726dd0645c5SBrian Somers 
1727dd0645c5SBrian Somers   if (!datalink_SetMode(dl, mode))
1728dd0645c5SBrian Somers     return 0;
1729dd0645c5SBrian Somers 
1730ff0f9439SBrian Somers   if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO) &&
1731ff0f9439SBrian Somers       bundle->phase != PHASE_NETWORK)
1732ff0f9439SBrian Somers     /* First auto link, we need an interface */
1733dd0645c5SBrian Somers     ipcp_InterfaceUp(&bundle->ncp.ipcp);
1734dd0645c5SBrian Somers 
1735ab2de065SBrian Somers   /* Regenerate phys_type and adjust idle timer */
173604eaa58cSBrian Somers   bundle_LinksRemoved(bundle);
1737dd0645c5SBrian Somers 
1738dd0645c5SBrian Somers   return 1;
1739dd0645c5SBrian Somers }
17401384bd27SBrian Somers 
17411384bd27SBrian Somers void
17421384bd27SBrian Somers bundle_setsid(struct bundle *bundle, int holdsession)
17431384bd27SBrian Somers {
17441384bd27SBrian Somers   /*
17451384bd27SBrian Somers    * Lose the current session.  This means getting rid of our pid
17461384bd27SBrian Somers    * too so that the tty device will really go away, and any getty
17471384bd27SBrian Somers    * etc will be allowed to restart.
17481384bd27SBrian Somers    */
17491384bd27SBrian Somers   pid_t pid, orig;
17501384bd27SBrian Somers   int fds[2];
17511384bd27SBrian Somers   char done;
17521384bd27SBrian Somers   struct datalink *dl;
17531384bd27SBrian Somers 
1754e62ce959SBrian Somers   if (!holdsession && bundle_IsDead(bundle)) {
1755e62ce959SBrian Somers     /*
1756e62ce959SBrian Somers      * No need to lose our session after all... we're going away anyway
1757e62ce959SBrian Somers      *
1758e62ce959SBrian Somers      * We should really stop the timer and pause if holdsession is set and
1759e62ce959SBrian Somers      * the bundle's dead, but that leaves other resources lying about :-(
1760e62ce959SBrian Somers      */
1761e62ce959SBrian Somers     return;
1762e62ce959SBrian Somers   }
1763e62ce959SBrian Somers 
17641384bd27SBrian Somers   orig = getpid();
17651384bd27SBrian Somers   if (pipe(fds) == -1) {
17661384bd27SBrian Somers     log_Printf(LogERROR, "pipe: %s\n", strerror(errno));
17671384bd27SBrian Somers     return;
17681384bd27SBrian Somers   }
17691384bd27SBrian Somers   switch ((pid = fork())) {
17701384bd27SBrian Somers     case -1:
17711384bd27SBrian Somers       log_Printf(LogERROR, "fork: %s\n", strerror(errno));
17721384bd27SBrian Somers       close(fds[0]);
17731384bd27SBrian Somers       close(fds[1]);
17741384bd27SBrian Somers       return;
17751384bd27SBrian Somers     case 0:
17761384bd27SBrian Somers       close(fds[1]);
17774be0e57dSBrian Somers       read(fds[0], &done, 1);		/* uu_locks are mine ! */
17784be0e57dSBrian Somers       close(fds[0]);
17791384bd27SBrian Somers       if (pipe(fds) == -1) {
17801384bd27SBrian Somers         log_Printf(LogERROR, "pipe(2): %s\n", strerror(errno));
17811384bd27SBrian Somers         return;
17821384bd27SBrian Somers       }
17831384bd27SBrian Somers       switch ((pid = fork())) {
17841384bd27SBrian Somers         case -1:
1785a33b2ef7SBrian Somers           log_Printf(LogERROR, "fork(2): %s\n", strerror(errno));
17861384bd27SBrian Somers           close(fds[0]);
17871384bd27SBrian Somers           close(fds[1]);
17881384bd27SBrian Somers           return;
17891384bd27SBrian Somers         case 0:
17901384bd27SBrian Somers           close(fds[1]);
17914be0e57dSBrian Somers           bundle_LockTun(bundle);	/* update pid */
17924be0e57dSBrian Somers           read(fds[0], &done, 1);	/* uu_locks are mine ! */
17934be0e57dSBrian Somers           close(fds[0]);
17941384bd27SBrian Somers           setsid();
179506b47306SBrian Somers           bundle_ChangedPID(bundle);
1796b42135deSBrian Somers           log_Printf(LogDEBUG, "%d -> %d: %s session control\n",
17971384bd27SBrian Somers                      (int)orig, (int)getpid(),
17981384bd27SBrian Somers                      holdsession ? "Passed" : "Dropped");
17997e778f13SBrian Somers           timer_InitService(0);		/* Start the Timer Service */
18001384bd27SBrian Somers           break;
18011384bd27SBrian Somers         default:
18024be0e57dSBrian Somers           close(fds[0]);
18035d9e6103SBrian Somers           /* Give away all our physical locks (to the final process) */
18041384bd27SBrian Somers           for (dl = bundle->links; dl; dl = dl->next)
18051384bd27SBrian Somers             if (dl->state != DATALINK_CLOSED)
18065d9e6103SBrian Somers               physical_ChangedPid(dl->physical, pid);
18074be0e57dSBrian Somers           write(fds[1], "!", 1);	/* done */
18084be0e57dSBrian Somers           close(fds[1]);
18092cb305afSBrian Somers           _exit(0);
18101384bd27SBrian Somers           break;
18111384bd27SBrian Somers       }
18121384bd27SBrian Somers       break;
18131384bd27SBrian Somers     default:
18144be0e57dSBrian Somers       close(fds[0]);
18155d9e6103SBrian Somers       /* Give away all our physical locks (to the intermediate process) */
18161384bd27SBrian Somers       for (dl = bundle->links; dl; dl = dl->next)
18171384bd27SBrian Somers         if (dl->state != DATALINK_CLOSED)
18185d9e6103SBrian Somers           physical_ChangedPid(dl->physical, pid);
18194be0e57dSBrian Somers       write(fds[1], "!", 1);	/* done */
18204be0e57dSBrian Somers       close(fds[1]);
18211384bd27SBrian Somers       if (holdsession) {
18221384bd27SBrian Somers         int fd, status;
18231384bd27SBrian Somers 
18241384bd27SBrian Somers         timer_TermService();
18251384bd27SBrian Somers         signal(SIGPIPE, SIG_DFL);
18261384bd27SBrian Somers         signal(SIGALRM, SIG_DFL);
18271384bd27SBrian Somers         signal(SIGHUP, SIG_DFL);
18281384bd27SBrian Somers         signal(SIGTERM, SIG_DFL);
18291384bd27SBrian Somers         signal(SIGINT, SIG_DFL);
18301384bd27SBrian Somers         signal(SIGQUIT, SIG_DFL);
18311384bd27SBrian Somers         for (fd = getdtablesize(); fd >= 0; fd--)
18321384bd27SBrian Somers           close(fd);
18331384bd27SBrian Somers         /*
18341384bd27SBrian Somers          * Reap the intermediate process.  As we're not exiting but the
18351384bd27SBrian Somers          * intermediate is, we don't want it to become defunct.
18361384bd27SBrian Somers          */
18371384bd27SBrian Somers         waitpid(pid, &status, 0);
18388e7b8599SBrian Somers         /* Tweak our process arguments.... */
1839cf0a3940SBrian Somers         ID0setproctitle("session owner");
1840a19a5c02SBrian Somers         setuid(ID0realuid());
18411384bd27SBrian Somers         /*
18421384bd27SBrian Somers          * Hang around for a HUP.  This should happen as soon as the
18438e7bd08eSBrian Somers          * ppp that we passed our ctty descriptor to closes it.
18448e7bd08eSBrian Somers          * NOTE: If this process dies, the passed descriptor becomes
18451384bd27SBrian Somers          *       invalid and will give a select() error by setting one
18461384bd27SBrian Somers          *       of the error fds, aborting the other ppp.  We don't
18471384bd27SBrian Somers          *       want that to happen !
18481384bd27SBrian Somers          */
18491384bd27SBrian Somers         pause();
18501384bd27SBrian Somers       }
18512cb305afSBrian Somers       _exit(0);
18521384bd27SBrian Somers       break;
18531384bd27SBrian Somers   }
18541384bd27SBrian Somers }
18559b5f8ffdSBrian Somers 
18569b5f8ffdSBrian Somers int
18579b5f8ffdSBrian Somers bundle_HighestState(struct bundle *bundle)
18589b5f8ffdSBrian Somers {
18599b5f8ffdSBrian Somers   struct datalink *dl;
18609b5f8ffdSBrian Somers   int result = DATALINK_CLOSED;
18619b5f8ffdSBrian Somers 
18629b5f8ffdSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
18639b5f8ffdSBrian Somers     if (result < dl->state)
18649b5f8ffdSBrian Somers       result = dl->state;
18659b5f8ffdSBrian Somers 
18669b5f8ffdSBrian Somers   return result;
18679b5f8ffdSBrian Somers }
1868991c2a7bSBrian Somers 
1869991c2a7bSBrian Somers int
1870991c2a7bSBrian Somers bundle_Exception(struct bundle *bundle, int fd)
1871991c2a7bSBrian Somers {
1872991c2a7bSBrian Somers   struct datalink *dl;
1873991c2a7bSBrian Somers 
1874991c2a7bSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
1875991c2a7bSBrian Somers     if (dl->physical->fd == fd) {
1876991c2a7bSBrian Somers       datalink_Down(dl, CLOSE_NORMAL);
1877991c2a7bSBrian Somers       return 1;
1878991c2a7bSBrian Somers     }
1879991c2a7bSBrian Somers 
1880991c2a7bSBrian Somers   return 0;
1881991c2a7bSBrian Somers }
18821d1fc017SBrian Somers 
18831d1fc017SBrian Somers void
18841d1fc017SBrian Somers bundle_AdjustFilters(struct bundle *bundle, struct in_addr *my_ip,
18851d1fc017SBrian Somers                      struct in_addr *peer_ip)
18861d1fc017SBrian Somers {
1887d568d6c4SBrian Somers   filter_AdjustAddr(&bundle->filter.in, my_ip, peer_ip, NULL);
1888d568d6c4SBrian Somers   filter_AdjustAddr(&bundle->filter.out, my_ip, peer_ip, NULL);
1889d568d6c4SBrian Somers   filter_AdjustAddr(&bundle->filter.dial, my_ip, peer_ip, NULL);
1890d568d6c4SBrian Somers   filter_AdjustAddr(&bundle->filter.alive, my_ip, peer_ip, NULL);
1891d568d6c4SBrian Somers }
1892d568d6c4SBrian Somers 
1893d568d6c4SBrian Somers void
1894d568d6c4SBrian Somers bundle_AdjustDNS(struct bundle *bundle, struct in_addr dns[2])
1895d568d6c4SBrian Somers {
1896d568d6c4SBrian Somers   filter_AdjustAddr(&bundle->filter.in, NULL, NULL, dns);
1897d568d6c4SBrian Somers   filter_AdjustAddr(&bundle->filter.out, NULL, NULL, dns);
1898d568d6c4SBrian Somers   filter_AdjustAddr(&bundle->filter.dial, NULL, NULL, dns);
1899d568d6c4SBrian Somers   filter_AdjustAddr(&bundle->filter.alive, NULL, NULL, dns);
19001d1fc017SBrian Somers }
1901ab2de065SBrian Somers 
1902ab2de065SBrian Somers void
1903ab2de065SBrian Somers bundle_CalculateBandwidth(struct bundle *bundle)
1904ab2de065SBrian Somers {
1905ab2de065SBrian Somers   struct datalink *dl;
1906ab2de065SBrian Somers   int mtu, sp;
1907ab2de065SBrian Somers 
1908ab2de065SBrian Somers   bundle->bandwidth = 0;
1909ab2de065SBrian Somers   mtu = 0;
1910ab2de065SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
1911ab2de065SBrian Somers     if (dl->state == DATALINK_OPEN) {
1912ab2de065SBrian Somers       if ((sp = dl->mp.bandwidth) == 0 &&
1913ab2de065SBrian Somers           (sp = physical_GetSpeed(dl->physical)) == 0)
1914ab2de065SBrian Somers         log_Printf(LogDEBUG, "%s: %s: Cannot determine bandwidth\n",
1915ab2de065SBrian Somers                    dl->name, dl->physical->name.full);
1916ab2de065SBrian Somers       else
1917ab2de065SBrian Somers         bundle->bandwidth += sp;
1918ab2de065SBrian Somers       if (!bundle->ncp.mp.active) {
1919ab2de065SBrian Somers         mtu = dl->physical->link.lcp.his_mru;
1920ab2de065SBrian Somers         break;
1921ab2de065SBrian Somers       }
1922ab2de065SBrian Somers     }
1923ab2de065SBrian Somers 
1924ab2de065SBrian Somers   if(bundle->bandwidth == 0)
1925ab2de065SBrian Somers     bundle->bandwidth = 115200;		/* Shrug */
1926ab2de065SBrian Somers 
1927ab2de065SBrian Somers   if (bundle->ncp.mp.active)
1928ab2de065SBrian Somers     mtu = bundle->ncp.mp.peer_mrru;
1929ab2de065SBrian Somers   else if (!mtu)
1930ab2de065SBrian Somers     mtu = 1500;
1931ab2de065SBrian Somers 
1932ab2de065SBrian Somers #ifndef NORADIUS
1933ab2de065SBrian Somers   if (bundle->radius.valid && bundle->radius.mtu && bundle->radius.mtu < mtu) {
1934ab2de065SBrian Somers     log_Printf(LogLCP, "Reducing MTU to radius value %lu\n",
1935ab2de065SBrian Somers                bundle->radius.mtu);
1936ab2de065SBrian Somers     mtu = bundle->radius.mtu;
1937ab2de065SBrian Somers   }
1938ab2de065SBrian Somers #endif
1939ab2de065SBrian Somers 
1940ab2de065SBrian Somers   tun_configure(bundle, mtu);
1941ab2de065SBrian Somers }
1942ab2de065SBrian Somers 
1943ab2de065SBrian Somers void
1944ab2de065SBrian Somers bundle_AutoAdjust(struct bundle *bundle, int percent, int what)
1945ab2de065SBrian Somers {
1946ab2de065SBrian Somers   struct datalink *dl, *choice, *otherlinkup;
1947ab2de065SBrian Somers 
1948ab2de065SBrian Somers   choice = otherlinkup = NULL;
1949ab2de065SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
1950ab2de065SBrian Somers     if (dl->physical->type == PHYS_AUTO) {
1951ab2de065SBrian Somers       if (dl->state == DATALINK_OPEN) {
1952ab2de065SBrian Somers         if (what == AUTO_DOWN) {
1953ab2de065SBrian Somers           if (choice)
1954ab2de065SBrian Somers             otherlinkup = choice;
1955ab2de065SBrian Somers           choice = dl;
1956ab2de065SBrian Somers         }
1957ab2de065SBrian Somers       } else if (dl->state == DATALINK_CLOSED) {
1958ab2de065SBrian Somers         if (what == AUTO_UP) {
1959ab2de065SBrian Somers           choice = dl;
1960ab2de065SBrian Somers           break;
1961ab2de065SBrian Somers         }
1962ab2de065SBrian Somers       } else {
1963ab2de065SBrian Somers         /* An auto link in an intermediate state - forget it for the moment */
1964ab2de065SBrian Somers         choice = NULL;
1965ab2de065SBrian Somers         break;
1966ab2de065SBrian Somers       }
1967ab2de065SBrian Somers     } else if (dl->state == DATALINK_OPEN && what == AUTO_DOWN)
1968ab2de065SBrian Somers       otherlinkup = dl;
1969ab2de065SBrian Somers 
1970ab2de065SBrian Somers   if (choice) {
1971ab2de065SBrian Somers     if (what == AUTO_UP) {
1972ab2de065SBrian Somers       log_Printf(LogPHASE, "%d%% saturation -> Opening link ``%s''\n",
1973ab2de065SBrian Somers                  percent, choice->name);
1974ab2de065SBrian Somers       datalink_Up(choice, 1, 1);
1975a339e644SBrian Somers       mp_CheckAutoloadTimer(&bundle->ncp.mp);
1976ab2de065SBrian Somers     } else if (otherlinkup) {	/* Only bring the second-last link down */
1977ab2de065SBrian Somers       log_Printf(LogPHASE, "%d%% saturation -> Closing link ``%s''\n",
1978ab2de065SBrian Somers                  percent, choice->name);
1979d2f5232dSBrian Somers       datalink_Close(choice, CLOSE_STAYDOWN);
1980a339e644SBrian Somers       mp_CheckAutoloadTimer(&bundle->ncp.mp);
1981ab2de065SBrian Somers     }
1982ab2de065SBrian Somers   }
1983ab2de065SBrian Somers }
1984ab2de065SBrian Somers 
1985ab2de065SBrian Somers int
1986ab2de065SBrian Somers bundle_WantAutoloadTimer(struct bundle *bundle)
1987ab2de065SBrian Somers {
1988ab2de065SBrian Somers   struct datalink *dl;
1989ab2de065SBrian Somers   int autolink, opened;
1990ab2de065SBrian Somers 
1991ab2de065SBrian Somers   if (bundle->phase == PHASE_NETWORK) {
1992ab2de065SBrian Somers     for (autolink = opened = 0, dl = bundle->links; dl; dl = dl->next)
1993ab2de065SBrian Somers       if (dl->physical->type == PHYS_AUTO) {
1994ab2de065SBrian Somers         if (++autolink == 2 || (autolink == 1 && opened))
1995ab2de065SBrian Somers           /* Two auto links or one auto and one open in NETWORK phase */
1996ab2de065SBrian Somers           return 1;
1997ab2de065SBrian Somers       } else if (dl->state == DATALINK_OPEN) {
1998ab2de065SBrian Somers         opened++;
1999ab2de065SBrian Somers         if (autolink)
2000ab2de065SBrian Somers           /* One auto and one open link in NETWORK phase */
2001ab2de065SBrian Somers           return 1;
2002ab2de065SBrian Somers       }
2003ab2de065SBrian Somers   }
2004ab2de065SBrian Somers 
2005ab2de065SBrian Somers   return 0;
2006ab2de065SBrian Somers }
200706b47306SBrian Somers 
200806b47306SBrian Somers void
200906b47306SBrian Somers bundle_ChangedPID(struct bundle *bundle)
201006b47306SBrian Somers {
201106b47306SBrian Somers #ifdef TUNSIFPID
201206b47306SBrian Somers   ioctl(bundle->dev.fd, TUNSIFPID, 0);
201306b47306SBrian Somers #endif
201406b47306SBrian Somers }
2015