xref: /freebsd/usr.sbin/ppp/bundle.c (revision cad7e7426a71c7c3daa7fd8a508773508e50cad7)
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  *
26cad7e742SBrian Somers  *	$Id: bundle.c,v 1.57 1999/06/22 11:31:42 brian Exp $
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>
330f203c7eSBrian Somers #include <net/if_tun.h>
347a6f8720SBrian Somers #include <arpa/inet.h>
357a6f8720SBrian Somers #include <net/route.h>
36eaa4df37SBrian Somers #include <netinet/in_systm.h>
37eaa4df37SBrian Somers #include <netinet/ip.h>
381fa665f5SBrian Somers #include <sys/un.h>
397a6f8720SBrian Somers 
407a6f8720SBrian Somers #include <errno.h>
417a6f8720SBrian Somers #include <fcntl.h>
4247723d29SBrian Somers #include <paths.h>
437a6f8720SBrian Somers #include <stdio.h>
446f384573SBrian Somers #include <stdlib.h>
457a6f8720SBrian Somers #include <string.h>
467a6f8720SBrian Somers #include <sys/ioctl.h>
4796c9bb21SBrian Somers #include <sys/uio.h>
4854cd8e13SBrian Somers #include <sys/wait.h>
497a6f8720SBrian Somers #include <termios.h>
507a6f8720SBrian Somers #include <unistd.h>
517a6f8720SBrian Somers 
525d9e6103SBrian Somers #include "layer.h"
53c9e11a11SBrian Somers #include "defs.h"
547a6f8720SBrian Somers #include "command.h"
557a6f8720SBrian Somers #include "mbuf.h"
567a6f8720SBrian Somers #include "log.h"
577a6f8720SBrian Somers #include "id.h"
587a6f8720SBrian Somers #include "timer.h"
597a6f8720SBrian Somers #include "fsm.h"
607a6f8720SBrian Somers #include "iplist.h"
61879ed6faSBrian Somers #include "lqr.h"
62455aabc3SBrian Somers #include "hdlc.h"
637a6f8720SBrian Somers #include "throughput.h"
64eaa4df37SBrian Somers #include "slcompress.h"
657a6f8720SBrian Somers #include "ipcp.h"
665ca5389aSBrian Somers #include "filter.h"
672f786681SBrian Somers #include "descriptor.h"
687a6f8720SBrian Somers #include "route.h"
697a6f8720SBrian Somers #include "lcp.h"
707a6f8720SBrian Somers #include "ccp.h"
713b0f8d2eSBrian Somers #include "link.h"
723b0f8d2eSBrian Somers #include "mp.h"
73972a1bcfSBrian Somers #ifndef NORADIUS
74972a1bcfSBrian Somers #include "radius.h"
75972a1bcfSBrian Somers #endif
763b0f8d2eSBrian Somers #include "bundle.h"
77455aabc3SBrian Somers #include "async.h"
78455aabc3SBrian Somers #include "physical.h"
79455aabc3SBrian Somers #include "auth.h"
805d9e6103SBrian Somers #include "proto.h"
81455aabc3SBrian Somers #include "chap.h"
82455aabc3SBrian Somers #include "tun.h"
8385b542cfSBrian Somers #include "prompt.h"
843006ec67SBrian Somers #include "chat.h"
8592b09558SBrian Somers #include "cbcp.h"
863006ec67SBrian Somers #include "datalink.h"
873006ec67SBrian Somers #include "ip.h"
888fa6ebe4SBrian Somers #include "iface.h"
897a6f8720SBrian Somers 
906815097bSBrian Somers #define SCATTER_SEGMENTS 5	/* version, datalink, name, physical, device */
9196c9bb21SBrian Somers #define SOCKET_OVERHEAD	100	/* additional buffer space for large */
9296c9bb21SBrian Somers                                 /* {recv,send}msg() calls            */
9396c9bb21SBrian Somers 
9404eaa58cSBrian Somers static int bundle_RemainingIdleTime(struct bundle *);
9504eaa58cSBrian Somers static int bundle_RemainingAutoLoadTime(struct bundle *);
9604eaa58cSBrian Somers 
97455aabc3SBrian Somers static const char *PhaseNames[] = {
98455aabc3SBrian Somers   "Dead", "Establish", "Authenticate", "Network", "Terminate"
99455aabc3SBrian Somers };
100455aabc3SBrian Somers 
101455aabc3SBrian Somers const char *
102455aabc3SBrian Somers bundle_PhaseName(struct bundle *bundle)
1037a6f8720SBrian Somers {
104455aabc3SBrian Somers   return bundle->phase <= PHASE_TERMINATE ?
105455aabc3SBrian Somers     PhaseNames[bundle->phase] : "unknown";
1067a6f8720SBrian Somers }
1077a6f8720SBrian Somers 
108455aabc3SBrian Somers void
1095563ebdeSBrian Somers bundle_NewPhase(struct bundle *bundle, u_int new)
110455aabc3SBrian Somers {
111aef795ccSBrian Somers   if (new == bundle->phase)
112aef795ccSBrian Somers     return;
113aef795ccSBrian Somers 
114e2ebb036SBrian Somers   if (new <= PHASE_TERMINATE)
115dd7e2610SBrian Somers     log_Printf(LogPHASE, "bundle: %s\n", PhaseNames[new]);
1167a6f8720SBrian Somers 
117455aabc3SBrian Somers   switch (new) {
118455aabc3SBrian Somers   case PHASE_DEAD:
1190f2f3eb3SBrian Somers     log_DisplayPrompts();
120455aabc3SBrian Somers     bundle->phase = new;
121455aabc3SBrian Somers     break;
122455aabc3SBrian Somers 
123455aabc3SBrian Somers   case PHASE_ESTABLISH:
124455aabc3SBrian Somers     bundle->phase = new;
125455aabc3SBrian Somers     break;
126455aabc3SBrian Somers 
127455aabc3SBrian Somers   case PHASE_AUTHENTICATE:
128455aabc3SBrian Somers     bundle->phase = new;
1290f2f3eb3SBrian Somers     log_DisplayPrompts();
130455aabc3SBrian Somers     break;
131455aabc3SBrian Somers 
132455aabc3SBrian Somers   case PHASE_NETWORK:
133dd7e2610SBrian Somers     fsm_Up(&bundle->ncp.ipcp.fsm);
134dd7e2610SBrian Somers     fsm_Open(&bundle->ncp.ipcp.fsm);
135673903ecSBrian Somers     bundle->phase = new;
1360f2f3eb3SBrian Somers     log_DisplayPrompts();
137673903ecSBrian Somers     break;
138455aabc3SBrian Somers 
139455aabc3SBrian Somers   case PHASE_TERMINATE:
140455aabc3SBrian Somers     bundle->phase = new;
141673903ecSBrian Somers     mp_Down(&bundle->ncp.mp);
1420f2f3eb3SBrian Somers     log_DisplayPrompts();
143455aabc3SBrian Somers     break;
1447a6f8720SBrian Somers   }
1457a6f8720SBrian Somers }
1467a6f8720SBrian Somers 
1476d666775SBrian Somers static void
1486d666775SBrian Somers bundle_LayerStart(void *v, struct fsm *fp)
1497a6f8720SBrian Somers {
1503006ec67SBrian Somers   /* The given FSM is about to start up ! */
1517a6f8720SBrian Somers }
1527a6f8720SBrian Somers 
1535cf4388bSBrian Somers 
1545cf4388bSBrian Somers static void
1555cf4388bSBrian Somers bundle_Notify(struct bundle *bundle, char c)
1565cf4388bSBrian Somers {
1575cf4388bSBrian Somers   if (bundle->notify.fd != -1) {
1585cf4388bSBrian Somers     if (write(bundle->notify.fd, &c, 1) == 1)
159dd7e2610SBrian Somers       log_Printf(LogPHASE, "Parent notified of success.\n");
1605cf4388bSBrian Somers     else
161dd7e2610SBrian Somers       log_Printf(LogPHASE, "Failed to notify parent of success.\n");
1625cf4388bSBrian Somers     close(bundle->notify.fd);
1635cf4388bSBrian Somers     bundle->notify.fd = -1;
1645cf4388bSBrian Somers   }
1655cf4388bSBrian Somers }
1663b0f8d2eSBrian Somers 
1676d666775SBrian Somers static void
1686f8e9f0aSBrian Somers bundle_ClearQueues(void *v)
1696f8e9f0aSBrian Somers {
1706f8e9f0aSBrian Somers   struct bundle *bundle = (struct bundle *)v;
1716f8e9f0aSBrian Somers   struct datalink *dl;
1726f8e9f0aSBrian Somers 
1736f8e9f0aSBrian Somers   log_Printf(LogPHASE, "Clearing choked output queue\n");
1746f8e9f0aSBrian Somers   timer_Stop(&bundle->choked.timer);
1756f8e9f0aSBrian Somers 
1766f8e9f0aSBrian Somers   /*
1776f8e9f0aSBrian Somers    * Emergency time:
1786f8e9f0aSBrian Somers    *
1796f8e9f0aSBrian Somers    * We've had a full queue for PACKET_DEL_SECS seconds without being
1806f8e9f0aSBrian Somers    * able to get rid of any of the packets.  We've probably given up
1816f8e9f0aSBrian Somers    * on the redials at this point, and the queued data has almost
1826f8e9f0aSBrian Somers    * definitely been timed out by the layer above.  As this is preventing
1836f8e9f0aSBrian Somers    * us from reading the TUN_NAME device (we don't want to buffer stuff
1846f8e9f0aSBrian Somers    * indefinitely), we may as well nuke this data and start with a clean
1856f8e9f0aSBrian Somers    * slate !
1866f8e9f0aSBrian Somers    *
1876f8e9f0aSBrian Somers    * Unfortunately, this has the side effect of shafting any compression
1886f8e9f0aSBrian Somers    * dictionaries in use (causing the relevant RESET_REQ/RESET_ACK).
1896f8e9f0aSBrian Somers    */
1906f8e9f0aSBrian Somers 
1915a72b6edSBrian Somers   ip_DeleteQueue(&bundle->ncp.ipcp);
1926f8e9f0aSBrian Somers   mp_DeleteQueue(&bundle->ncp.mp);
1936f8e9f0aSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
1946f8e9f0aSBrian Somers     physical_DeleteQueue(dl->physical);
1956f8e9f0aSBrian Somers }
1966f8e9f0aSBrian Somers 
1976f8e9f0aSBrian Somers static void
19804eaa58cSBrian Somers bundle_AutoLoadTimeout(void *v)
19904eaa58cSBrian Somers {
20004eaa58cSBrian Somers   struct bundle *bundle = (struct bundle *)v;
20104eaa58cSBrian Somers 
20204eaa58cSBrian Somers   if (bundle->autoload.comingup) {
20304eaa58cSBrian Somers     log_Printf(LogPHASE, "autoload: Another link is required\n");
20404eaa58cSBrian Somers     /* bundle_Open() stops the timer */
205ba23f397SBrian Somers     bundle_Open(bundle, NULL, PHYS_AUTO, 0);
20604eaa58cSBrian Somers   } else {
20704eaa58cSBrian Somers     struct datalink *dl, *last;
20804eaa58cSBrian Somers 
20904eaa58cSBrian Somers     timer_Stop(&bundle->autoload.timer);
21004eaa58cSBrian Somers     for (last = NULL, dl = bundle->links; dl; dl = dl->next)
21181358fa3SBrian Somers       if (dl->physical->type == PHYS_AUTO && dl->state == DATALINK_OPEN)
21204eaa58cSBrian Somers         last = dl;
21304eaa58cSBrian Somers 
21404eaa58cSBrian Somers     if (last)
2159c81b87dSBrian Somers       datalink_Close(last, CLOSE_STAYDOWN);
21604eaa58cSBrian Somers   }
21704eaa58cSBrian Somers }
21804eaa58cSBrian Somers 
21904eaa58cSBrian Somers static void
22004eaa58cSBrian Somers bundle_StartAutoLoadTimer(struct bundle *bundle, int up)
22104eaa58cSBrian Somers {
22204eaa58cSBrian Somers   struct datalink *dl;
22304eaa58cSBrian Somers 
22404eaa58cSBrian Somers   timer_Stop(&bundle->autoload.timer);
225ec5e7f36SBrian Somers   bundle->autoload.comingup = up ? 1 : 0;
22604eaa58cSBrian Somers 
22704eaa58cSBrian Somers   if (bundle->CleaningUp || bundle->phase != PHASE_NETWORK) {
22804eaa58cSBrian Somers     dl = NULL;
22904eaa58cSBrian Somers     bundle->autoload.running = 0;
23004eaa58cSBrian Somers   } else if (up) {
23104eaa58cSBrian Somers     for (dl = bundle->links; dl; dl = dl->next)
23281358fa3SBrian Somers       if (dl->state == DATALINK_CLOSED && dl->physical->type == PHYS_AUTO) {
23304eaa58cSBrian Somers         if (bundle->cfg.autoload.max.timeout) {
23404eaa58cSBrian Somers           bundle->autoload.timer.func = bundle_AutoLoadTimeout;
23504eaa58cSBrian Somers           bundle->autoload.timer.name = "autoload up";
23604eaa58cSBrian Somers           bundle->autoload.timer.load =
23704eaa58cSBrian Somers             bundle->cfg.autoload.max.timeout * SECTICKS;
23804eaa58cSBrian Somers           bundle->autoload.timer.arg = bundle;
23904eaa58cSBrian Somers           timer_Start(&bundle->autoload.timer);
24004eaa58cSBrian Somers           bundle->autoload.done = time(NULL) + bundle->cfg.autoload.max.timeout;
24104eaa58cSBrian Somers         } else
24204eaa58cSBrian Somers           bundle_AutoLoadTimeout(bundle);
24304eaa58cSBrian Somers         break;
24404eaa58cSBrian Somers       }
24504eaa58cSBrian Somers     bundle->autoload.running = (dl || bundle->cfg.autoload.min.timeout) ? 1 : 0;
24604eaa58cSBrian Somers   } else {
24704eaa58cSBrian Somers     int nlinks;
24804eaa58cSBrian Somers     struct datalink *adl;
24904eaa58cSBrian Somers 
25004eaa58cSBrian Somers     for (nlinks = 0, adl = NULL, dl = bundle->links; dl; dl = dl->next)
25104eaa58cSBrian Somers       if (dl->state == DATALINK_OPEN) {
25281358fa3SBrian Somers         if (dl->physical->type == PHYS_AUTO)
25304eaa58cSBrian Somers           adl = dl;
25404eaa58cSBrian Somers         if (++nlinks > 1 && adl) {
25504eaa58cSBrian Somers           if (bundle->cfg.autoload.min.timeout) {
25604eaa58cSBrian Somers             bundle->autoload.timer.func = bundle_AutoLoadTimeout;
25704eaa58cSBrian Somers             bundle->autoload.timer.name = "autoload down";
25804eaa58cSBrian Somers             bundle->autoload.timer.load =
25904eaa58cSBrian Somers               bundle->cfg.autoload.min.timeout * SECTICKS;
26004eaa58cSBrian Somers             bundle->autoload.timer.arg = bundle;
26104eaa58cSBrian Somers             timer_Start(&bundle->autoload.timer);
26204eaa58cSBrian Somers             bundle->autoload.done =
26304eaa58cSBrian Somers               time(NULL) + bundle->cfg.autoload.min.timeout;
26404eaa58cSBrian Somers           }
26504eaa58cSBrian Somers           break;
26604eaa58cSBrian Somers         }
26704eaa58cSBrian Somers       }
26804eaa58cSBrian Somers 
26904eaa58cSBrian Somers     bundle->autoload.running = 1;
27004eaa58cSBrian Somers   }
27104eaa58cSBrian Somers }
27204eaa58cSBrian Somers 
27304eaa58cSBrian Somers static void
27404eaa58cSBrian Somers bundle_StopAutoLoadTimer(struct bundle *bundle)
27504eaa58cSBrian Somers {
27604eaa58cSBrian Somers   timer_Stop(&bundle->autoload.timer);
27704eaa58cSBrian Somers   bundle->autoload.done = 0;
27804eaa58cSBrian Somers }
27904eaa58cSBrian Somers 
28004eaa58cSBrian Somers static int
28104eaa58cSBrian Somers bundle_RemainingAutoLoadTime(struct bundle *bundle)
28204eaa58cSBrian Somers {
28304eaa58cSBrian Somers   if (bundle->autoload.done)
28404eaa58cSBrian Somers     return bundle->autoload.done - time(NULL);
28504eaa58cSBrian Somers   return -1;
28604eaa58cSBrian Somers }
28704eaa58cSBrian Somers 
288ff0f9439SBrian Somers static void
289ff0f9439SBrian Somers bundle_LinkAdded(struct bundle *bundle, struct datalink *dl)
290ff0f9439SBrian Somers {
291ff0f9439SBrian Somers   bundle->phys_type.all |= dl->physical->type;
292ff0f9439SBrian Somers   if (dl->state == DATALINK_OPEN)
293ff0f9439SBrian Somers     bundle->phys_type.open |= dl->physical->type;
294ff0f9439SBrian Somers 
295ff0f9439SBrian Somers   /* Note: We only re-add links that are DATALINK_OPEN */
296ff0f9439SBrian Somers   if (dl->physical->type == PHYS_AUTO &&
297ff0f9439SBrian Somers       bundle->autoload.timer.state == TIMER_STOPPED &&
298ff0f9439SBrian Somers       dl->state != DATALINK_OPEN &&
299ff0f9439SBrian Somers       bundle->phase == PHASE_NETWORK)
300ff0f9439SBrian Somers     bundle->autoload.running = 1;
301ff0f9439SBrian Somers 
302ff0f9439SBrian Somers   if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL))
303ff0f9439SBrian Somers       != bundle->phys_type.open && bundle->idle.timer.state == TIMER_STOPPED)
304ff0f9439SBrian Somers     /* We may need to start our idle timer */
305ff0f9439SBrian Somers     bundle_StartIdleTimer(bundle);
306ff0f9439SBrian Somers }
307ff0f9439SBrian Somers 
30892b09558SBrian Somers void
309ff0f9439SBrian Somers bundle_LinksRemoved(struct bundle *bundle)
310ff0f9439SBrian Somers {
311ff0f9439SBrian Somers   struct datalink *dl;
312ff0f9439SBrian Somers 
313ff0f9439SBrian Somers   bundle->phys_type.all = bundle->phys_type.open = 0;
314ff0f9439SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
315ff0f9439SBrian Somers     bundle_LinkAdded(bundle, dl);
316ff0f9439SBrian Somers 
317ff0f9439SBrian Somers   if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL))
318ff0f9439SBrian Somers       == bundle->phys_type.open)
319ff0f9439SBrian Somers     bundle_StopIdleTimer(bundle);
320ff0f9439SBrian Somers }
32104eaa58cSBrian Somers 
32204eaa58cSBrian Somers static void
3236f384573SBrian Somers bundle_LayerUp(void *v, struct fsm *fp)
3247a6f8720SBrian Somers {
3253006ec67SBrian Somers   /*
3263006ec67SBrian Somers    * The given fsm is now up
327ff0f9439SBrian Somers    * If it's an LCP, adjust our phys_mode.open value.
32849052c95SBrian Somers    * If it's an LCP set our mtu (if we're multilink, add up the link
32904eaa58cSBrian Somers    * speeds and set the MRRU) and start our autoload timer.
330565e35e5SBrian Somers    * If it's an NCP, tell our -background parent to go away.
3313b0f8d2eSBrian Somers    * If it's the first NCP, start the idle timer.
3323006ec67SBrian Somers    */
3336f384573SBrian Somers   struct bundle *bundle = (struct bundle *)v;
3346d666775SBrian Somers 
3355563ebdeSBrian Somers   if (fp->proto == PROTO_LCP) {
336ff0f9439SBrian Somers     struct physical *p = link2physical(fp->link);
337ff0f9439SBrian Somers 
338ff0f9439SBrian Somers     bundle_LinkAdded(bundle, p->dl);
3393b0f8d2eSBrian Somers     if (bundle->ncp.mp.active) {
3403b0f8d2eSBrian Somers       struct datalink *dl;
3415563ebdeSBrian Somers 
3428fa6ebe4SBrian Somers       bundle->ifSpeed = 0;
343faefde08SBrian Somers       for (dl = bundle->links; dl; dl = dl->next)
344eeab6bf5SBrian Somers         if (dl->state == DATALINK_OPEN)
3455d9e6103SBrian Somers           bundle->ifSpeed += physical_GetSpeed(dl->physical);
346faefde08SBrian Somers       tun_configure(bundle, bundle->ncp.mp.peer_mrru);
34704eaa58cSBrian Somers       bundle->autoload.running = 1;
348faefde08SBrian Somers     } else {
3495d9e6103SBrian Somers       bundle->ifSpeed = physical_GetSpeed(p);
350faefde08SBrian Somers       tun_configure(bundle, fsm2lcp(fp)->his_mru);
351faefde08SBrian Somers     }
3523b0f8d2eSBrian Somers   } else if (fp->proto == PROTO_IPCP) {
353ab886ad0SBrian Somers     bundle_StartIdleTimer(bundle);
3545cf4388bSBrian Somers     bundle_Notify(bundle, EX_NORMAL);
3557a6f8720SBrian Somers   }
356ab886ad0SBrian Somers }
3577a6f8720SBrian Somers 
3586d666775SBrian Somers static void
3596d666775SBrian Somers bundle_LayerDown(void *v, struct fsm *fp)
3606d666775SBrian Somers {
3616d666775SBrian Somers   /*
3626d666775SBrian Somers    * The given FSM has been told to come down.
363ab886ad0SBrian Somers    * If it's our last NCP, stop the idle timer.
364ff0f9439SBrian Somers    * If it's an LCP, adjust our phys_type.open value and any timers.
36586b1f0d7SBrian Somers    * If it's an LCP and we're in multilink mode, adjust our tun
36686b1f0d7SBrian Somers    * speed and make sure our minimum sequence number is adjusted.
3676d666775SBrian Somers    */
368ab886ad0SBrian Somers 
369ab886ad0SBrian Somers   struct bundle *bundle = (struct bundle *)v;
370ab886ad0SBrian Somers 
371078c562eSBrian Somers   if (fp->proto == PROTO_IPCP)
372ab886ad0SBrian Somers     bundle_StopIdleTimer(bundle);
373ff0f9439SBrian Somers   else if (fp->proto == PROTO_LCP) {
374ff0f9439SBrian Somers     bundle_LinksRemoved(bundle);  /* adjust timers & phys_type values */
375ff0f9439SBrian Somers     if (bundle->ncp.mp.active) {
3763b0f8d2eSBrian Somers       struct datalink *dl;
37786b1f0d7SBrian Somers       struct datalink *lost;
3783b0f8d2eSBrian Somers 
3798fa6ebe4SBrian Somers       bundle->ifSpeed = 0;
38086b1f0d7SBrian Somers       lost = NULL;
381faefde08SBrian Somers       for (dl = bundle->links; dl; dl = dl->next)
38286b1f0d7SBrian Somers         if (fp == &dl->physical->link.lcp.fsm)
38386b1f0d7SBrian Somers           lost = dl;
38486b1f0d7SBrian Somers         else if (dl->state == DATALINK_OPEN)
3855d9e6103SBrian Somers           bundle->ifSpeed += physical_GetSpeed(dl->physical);
38686b1f0d7SBrian Somers 
3878fa6ebe4SBrian Somers       if (bundle->ifSpeed)
388faefde08SBrian Somers         /* Don't configure down to a speed of 0 */
389faefde08SBrian Somers         tun_configure(bundle, bundle->ncp.mp.link.lcp.his_mru);
39086b1f0d7SBrian Somers 
39186b1f0d7SBrian Somers       if (lost)
39286b1f0d7SBrian Somers         mp_LinkLost(&bundle->ncp.mp, lost);
39386b1f0d7SBrian Somers       else
394a33b2ef7SBrian Somers         log_Printf(LogALERT, "Oops, lost an unrecognised datalink (%s) !\n",
39586b1f0d7SBrian Somers                    fp->link->name);
3963b0f8d2eSBrian Somers     }
3976d666775SBrian Somers   }
398ff0f9439SBrian Somers }
3996d666775SBrian Somers 
4006d666775SBrian Somers static void
4016d666775SBrian Somers bundle_LayerFinish(void *v, struct fsm *fp)
4026d666775SBrian Somers {
4036d666775SBrian Somers   /* The given fsm is now down (fp cannot be NULL)
4046d666775SBrian Somers    *
405dd7e2610SBrian Somers    * If it's the last LCP, fsm_Down all NCPs
406dd7e2610SBrian Somers    * If it's the last NCP, fsm_Close all LCPs
4076d666775SBrian Somers    */
4086d666775SBrian Somers 
4096d666775SBrian Somers   struct bundle *bundle = (struct bundle *)v;
4106d666775SBrian Somers   struct datalink *dl;
4116d666775SBrian Somers 
4123b0f8d2eSBrian Somers   if (fp->proto == PROTO_IPCP) {
41326afeaa2SBrian Somers     if (bundle_Phase(bundle) != PHASE_DEAD)
41425092092SBrian Somers       bundle_NewPhase(bundle, PHASE_TERMINATE);
4156d666775SBrian Somers     for (dl = bundle->links; dl; dl = dl->next)
4169c81b87dSBrian Somers       datalink_Close(dl, CLOSE_NORMAL);
41709206a6fSBrian Somers     fsm2initial(fp);
4183b0f8d2eSBrian Somers   } else if (fp->proto == PROTO_LCP) {
4193b0f8d2eSBrian Somers     int others_active;
420a611cad6SBrian Somers 
4213b0f8d2eSBrian Somers     others_active = 0;
4223b0f8d2eSBrian Somers     for (dl = bundle->links; dl; dl = dl->next)
4233b0f8d2eSBrian Somers       if (fp != &dl->physical->link.lcp.fsm &&
4243b0f8d2eSBrian Somers           dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP)
4253b0f8d2eSBrian Somers         others_active++;
4263b0f8d2eSBrian Somers 
42709206a6fSBrian Somers     if (!others_active)
42809206a6fSBrian Somers       fsm2initial(&bundle->ncp.ipcp.fsm);
4293b0f8d2eSBrian Somers   }
4303b0f8d2eSBrian Somers }
4316d666775SBrian Somers 
4327a6f8720SBrian Somers int
4337a6f8720SBrian Somers bundle_LinkIsUp(const struct bundle *bundle)
4347a6f8720SBrian Somers {
4355828db6dSBrian Somers   return bundle->ncp.ipcp.fsm.state == ST_OPENED;
4367a6f8720SBrian Somers }
4377a6f8720SBrian Somers 
4387a6f8720SBrian Somers void
4399c81b87dSBrian Somers bundle_Close(struct bundle *bundle, const char *name, int how)
4407a6f8720SBrian Somers {
441455aabc3SBrian Somers   /*
4423006ec67SBrian Somers    * Please close the given datalink.
443dd7e2610SBrian Somers    * If name == NULL or name is the last datalink, fsm_Close all NCPs
4446f384573SBrian Somers    * (except our MP)
4453b0f8d2eSBrian Somers    * If it isn't the last datalink, just Close that datalink.
446455aabc3SBrian Somers    */
4477a6f8720SBrian Somers 
4483b0f8d2eSBrian Somers   struct datalink *dl, *this_dl;
4493b0f8d2eSBrian Somers   int others_active;
4503006ec67SBrian Somers 
4513b0f8d2eSBrian Somers   others_active = 0;
4523b0f8d2eSBrian Somers   this_dl = NULL;
4533b0f8d2eSBrian Somers 
4543b0f8d2eSBrian Somers   for (dl = bundle->links; dl; dl = dl->next) {
4553b0f8d2eSBrian Somers     if (name && !strcasecmp(name, dl->name))
4563b0f8d2eSBrian Somers       this_dl = dl;
4573b0f8d2eSBrian Somers     if (name == NULL || this_dl == dl) {
4589c81b87dSBrian Somers       switch (how) {
4599c81b87dSBrian Somers         case CLOSE_LCP:
4609c81b87dSBrian Somers           datalink_DontHangup(dl);
4619c81b87dSBrian Somers           /* fall through */
4629c81b87dSBrian Somers         case CLOSE_STAYDOWN:
4633006ec67SBrian Somers           datalink_StayDown(dl);
4649c81b87dSBrian Somers           break;
4659c81b87dSBrian Somers       }
4663b0f8d2eSBrian Somers     } else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP)
4673b0f8d2eSBrian Somers       others_active++;
4683b0f8d2eSBrian Somers   }
4693b0f8d2eSBrian Somers 
4703b0f8d2eSBrian Somers   if (name && this_dl == NULL) {
471dd7e2610SBrian Somers     log_Printf(LogWARN, "%s: Invalid datalink name\n", name);
4723b0f8d2eSBrian Somers     return;
4733b0f8d2eSBrian Somers   }
4743b0f8d2eSBrian Somers 
4753b0f8d2eSBrian Somers   if (!others_active) {
47604eaa58cSBrian Somers     bundle_StopIdleTimer(bundle);
47704eaa58cSBrian Somers     bundle_StopAutoLoadTimer(bundle);
4783b0f8d2eSBrian Somers     if (bundle->ncp.ipcp.fsm.state > ST_CLOSED ||
4793b0f8d2eSBrian Somers         bundle->ncp.ipcp.fsm.state == ST_STARTING)
480dd7e2610SBrian Somers       fsm_Close(&bundle->ncp.ipcp.fsm);
4813b0f8d2eSBrian Somers     else {
48209206a6fSBrian Somers       fsm2initial(&bundle->ncp.ipcp.fsm);
483d345321bSBrian Somers       for (dl = bundle->links; dl; dl = dl->next)
4849c81b87dSBrian Somers         datalink_Close(dl, how);
4857a6f8720SBrian Somers     }
4863b0f8d2eSBrian Somers   } else if (this_dl && this_dl->state != DATALINK_CLOSED &&
4873b0f8d2eSBrian Somers              this_dl->state != DATALINK_HANGUP)
4889c81b87dSBrian Somers     datalink_Close(this_dl, how);
489d2fd8d77SBrian Somers }
4907a6f8720SBrian Somers 
4911bc9b5baSBrian Somers void
492899011c4SBrian Somers bundle_Down(struct bundle *bundle, int how)
4931bc9b5baSBrian Somers {
4941bc9b5baSBrian Somers   struct datalink *dl;
4951bc9b5baSBrian Somers 
4961bc9b5baSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
497899011c4SBrian Somers     datalink_Down(dl, how);
4981bc9b5baSBrian Somers }
4991bc9b5baSBrian Somers 
5002f786681SBrian Somers static int
5012f786681SBrian Somers bundle_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
5022f786681SBrian Somers {
5032f786681SBrian Somers   struct bundle *bundle = descriptor2bundle(d);
5042f786681SBrian Somers   struct datalink *dl;
50504eaa58cSBrian Somers   int result, want, queued, nlinks;
5062f786681SBrian Somers 
5072f786681SBrian Somers   result = 0;
5082f786681SBrian Somers 
509078c562eSBrian Somers   /* If there are aren't many packets queued, look for some more. */
51004eaa58cSBrian Somers   for (nlinks = 0, dl = bundle->links; dl; dl = dl->next)
51104eaa58cSBrian Somers     nlinks++;
51204eaa58cSBrian Somers 
51304eaa58cSBrian Somers   if (nlinks) {
5145a72b6edSBrian Somers     queued = r ? bundle_FillQueues(bundle) : ip_QueueLen(&bundle->ncp.ipcp);
51504eaa58cSBrian Somers     if (bundle->autoload.running) {
51604eaa58cSBrian Somers       if (queued < bundle->cfg.autoload.max.packets) {
51704eaa58cSBrian Somers         if (queued > bundle->cfg.autoload.min.packets)
51804eaa58cSBrian Somers           bundle_StopAutoLoadTimer(bundle);
51904eaa58cSBrian Somers         else if (bundle->autoload.timer.state != TIMER_RUNNING ||
52004eaa58cSBrian Somers                  bundle->autoload.comingup)
52104eaa58cSBrian Somers           bundle_StartAutoLoadTimer(bundle, 0);
522ec5e7f36SBrian Somers       } else if ((bundle_Phase(bundle) == PHASE_NETWORK || queued) &&
523ec5e7f36SBrian Somers                  (bundle->autoload.timer.state != TIMER_RUNNING ||
524f4007327SBrian Somers                   !bundle->autoload.comingup))
52504eaa58cSBrian Somers         bundle_StartAutoLoadTimer(bundle, 1);
52604eaa58cSBrian Somers     }
52704eaa58cSBrian Somers 
528ff0f9439SBrian Somers     if (r && (bundle->phase == PHASE_NETWORK ||
529ff0f9439SBrian Somers               bundle->phys_type.all & PHYS_AUTO)) {
53004eaa58cSBrian Somers       /* enough surplus so that we can tell if we're getting swamped */
53104eaa58cSBrian Somers       want = bundle->cfg.autoload.max.packets + nlinks * 2;
53204eaa58cSBrian Somers       /* but at least 20 packets ! */
53304eaa58cSBrian Somers       if (want < 20)
53404eaa58cSBrian Somers         want = 20;
53504eaa58cSBrian Somers       if (queued < want) {
53604eaa58cSBrian Somers         /* Not enough - select() for more */
5376f8e9f0aSBrian Somers         if (bundle->choked.timer.state == TIMER_RUNNING)
5386f8e9f0aSBrian Somers           timer_Stop(&bundle->choked.timer);	/* Not needed any more */
53904eaa58cSBrian Somers         FD_SET(bundle->dev.fd, r);
540faefde08SBrian Somers         if (*n < bundle->dev.fd + 1)
541faefde08SBrian Somers           *n = bundle->dev.fd + 1;
5421384bd27SBrian Somers         log_Printf(LogTIMER, "%s: fdset(r) %d\n", TUN_NAME, bundle->dev.fd);
543078c562eSBrian Somers         result++;
5446f8e9f0aSBrian Somers       } else if (bundle->choked.timer.state == TIMER_STOPPED) {
5456f8e9f0aSBrian Somers         bundle->choked.timer.func = bundle_ClearQueues;
5466f8e9f0aSBrian Somers         bundle->choked.timer.name = "output choke";
5476f8e9f0aSBrian Somers         bundle->choked.timer.load = bundle->cfg.choked.timeout * SECTICKS;
5486f8e9f0aSBrian Somers         bundle->choked.timer.arg = bundle;
5496f8e9f0aSBrian Somers         timer_Start(&bundle->choked.timer);
550078c562eSBrian Somers       }
55104eaa58cSBrian Somers     }
55204eaa58cSBrian Somers   }
553078c562eSBrian Somers 
554f0cdd9c0SBrian Somers #ifndef NORADIUS
555f0cdd9c0SBrian Somers   result += descriptor_UpdateSet(&bundle->radius.desc, r, w, e, n);
556f0cdd9c0SBrian Somers #endif
557f0cdd9c0SBrian Somers 
55871555108SBrian Somers   /* Which links need a select() ? */
55971555108SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
56071555108SBrian Somers     result += descriptor_UpdateSet(&dl->desc, r, w, e, n);
56171555108SBrian Somers 
562ea722969SBrian Somers   /*
563ea722969SBrian Somers    * This *MUST* be called after the datalink UpdateSet()s as it
56404eaa58cSBrian Somers    * might be ``holding'' one of the datalinks (death-row) and
56504eaa58cSBrian Somers    * wants to be able to de-select() it from the descriptor set.
566ea722969SBrian Somers    */
5670f2f3eb3SBrian Somers   result += descriptor_UpdateSet(&bundle->ncp.mp.server.desc, r, w, e, n);
568ea722969SBrian Somers 
5692f786681SBrian Somers   return result;
5702f786681SBrian Somers }
5712f786681SBrian Somers 
5722f786681SBrian Somers static int
5732f786681SBrian Somers bundle_IsSet(struct descriptor *d, const fd_set *fdset)
5742f786681SBrian Somers {
5752f786681SBrian Somers   struct bundle *bundle = descriptor2bundle(d);
5762f786681SBrian Somers   struct datalink *dl;
5772f786681SBrian Somers 
5782f786681SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
5792f786681SBrian Somers     if (descriptor_IsSet(&dl->desc, fdset))
5802f786681SBrian Somers       return 1;
5812f786681SBrian Somers 
582f0cdd9c0SBrian Somers #ifndef NORADIUS
583f0cdd9c0SBrian Somers   if (descriptor_IsSet(&bundle->radius.desc, fdset))
584f0cdd9c0SBrian Somers     return 1;
585f0cdd9c0SBrian Somers #endif
586f0cdd9c0SBrian Somers 
587332b9de0SBrian Somers   if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset))
588332b9de0SBrian Somers     return 1;
589332b9de0SBrian Somers 
590faefde08SBrian Somers   return FD_ISSET(bundle->dev.fd, fdset);
5912f786681SBrian Somers }
5922f786681SBrian Somers 
5932f786681SBrian Somers static void
5942f786681SBrian Somers bundle_DescriptorRead(struct descriptor *d, struct bundle *bundle,
5952f786681SBrian Somers                       const fd_set *fdset)
5962f786681SBrian Somers {
5972f786681SBrian Somers   struct datalink *dl;
5982f786681SBrian Somers 
599ea722969SBrian Somers   if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset))
600ea722969SBrian Somers     descriptor_Read(&bundle->ncp.mp.server.desc, bundle, fdset);
601ea722969SBrian Somers 
6022f786681SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
6032f786681SBrian Somers     if (descriptor_IsSet(&dl->desc, fdset))
6042f786681SBrian Somers       descriptor_Read(&dl->desc, bundle, fdset);
605b6217683SBrian Somers 
606f0cdd9c0SBrian Somers #ifndef NORADIUS
607f0cdd9c0SBrian Somers   if (descriptor_IsSet(&bundle->radius.desc, fdset))
608f0cdd9c0SBrian Somers     descriptor_Read(&bundle->radius.desc, bundle, fdset);
609f0cdd9c0SBrian Somers #endif
610f0cdd9c0SBrian Somers 
611faefde08SBrian Somers   if (FD_ISSET(bundle->dev.fd, fdset)) {
612078c562eSBrian Somers     struct tun_data tun;
613078c562eSBrian Somers     int n, pri;
614078c562eSBrian Somers 
615078c562eSBrian Somers     /* something to read from tun */
616faefde08SBrian Somers     n = read(bundle->dev.fd, &tun, sizeof tun);
617078c562eSBrian Somers     if (n < 0) {
618a33b2ef7SBrian Somers       log_Printf(LogWARN, "read from %s: %s\n", TUN_NAME, strerror(errno));
619078c562eSBrian Somers       return;
620078c562eSBrian Somers     }
621078c562eSBrian Somers     n -= sizeof tun - sizeof tun.data;
622078c562eSBrian Somers     if (n <= 0) {
623a33b2ef7SBrian Somers       log_Printf(LogERROR, "read from %s: Only %d bytes read ?\n", TUN_NAME, n);
624078c562eSBrian Somers       return;
625078c562eSBrian Somers     }
626078c562eSBrian Somers     if (!tun_check_header(tun, AF_INET))
627078c562eSBrian Somers       return;
628078c562eSBrian Somers 
629078c562eSBrian Somers     if (((struct ip *)tun.data)->ip_dst.s_addr ==
630078c562eSBrian Somers         bundle->ncp.ipcp.my_ip.s_addr) {
631078c562eSBrian Somers       /* we've been asked to send something addressed *to* us :( */
632078c562eSBrian Somers       if (Enabled(bundle, OPT_LOOPBACK)) {
633078c562eSBrian Somers         pri = PacketCheck(bundle, tun.data, n, &bundle->filter.in);
634078c562eSBrian Somers         if (pri >= 0) {
6355d9e6103SBrian Somers           n += sizeof tun - sizeof tun.data;
6365d9e6103SBrian Somers           write(bundle->dev.fd, &tun, n);
637078c562eSBrian Somers           log_Printf(LogDEBUG, "Looped back packet addressed to myself\n");
638078c562eSBrian Somers         }
639078c562eSBrian Somers         return;
640078c562eSBrian Somers       } else
641078c562eSBrian Somers         log_Printf(LogDEBUG, "Oops - forwarding packet addressed to myself\n");
642078c562eSBrian Somers     }
643078c562eSBrian Somers 
644078c562eSBrian Somers     /*
645078c562eSBrian Somers      * Process on-demand dialup. Output packets are queued within tunnel
646078c562eSBrian Somers      * device until IPCP is opened.
647078c562eSBrian Somers      */
648078c562eSBrian Somers 
649078c562eSBrian Somers     if (bundle_Phase(bundle) == PHASE_DEAD) {
650078c562eSBrian Somers       /*
651078c562eSBrian Somers        * Note, we must be in AUTO mode :-/ otherwise our interface should
652078c562eSBrian Somers        * *not* be UP and we can't receive data
653078c562eSBrian Somers        */
654078c562eSBrian Somers       if ((pri = PacketCheck(bundle, tun.data, n, &bundle->filter.dial)) >= 0)
655ba23f397SBrian Somers         bundle_Open(bundle, NULL, PHYS_AUTO, 0);
656078c562eSBrian Somers       else
657078c562eSBrian Somers         /*
658078c562eSBrian Somers          * Drop the packet.  If we were to queue it, we'd just end up with
659078c562eSBrian Somers          * a pile of timed-out data in our output queue by the time we get
660078c562eSBrian Somers          * around to actually dialing.  We'd also prematurely reach the
661078c562eSBrian Somers          * threshold at which we stop select()ing to read() the tun
662078c562eSBrian Somers          * device - breaking auto-dial.
663078c562eSBrian Somers          */
664078c562eSBrian Somers         return;
665078c562eSBrian Somers     }
666078c562eSBrian Somers 
667078c562eSBrian Somers     pri = PacketCheck(bundle, tun.data, n, &bundle->filter.out);
6685d9e6103SBrian Somers     if (pri >= 0)
6695a72b6edSBrian Somers       ip_Enqueue(&bundle->ncp.ipcp, pri, tun.data, n);
670078c562eSBrian Somers   }
671078c562eSBrian Somers }
6722f786681SBrian Somers 
6731af29a6eSBrian Somers static int
6742f786681SBrian Somers bundle_DescriptorWrite(struct descriptor *d, struct bundle *bundle,
6752f786681SBrian Somers                        const fd_set *fdset)
6762f786681SBrian Somers {
6772f786681SBrian Somers   struct datalink *dl;
6781af29a6eSBrian Somers   int result = 0;
6792f786681SBrian Somers 
680ea722969SBrian Somers   /* This is not actually necessary as struct mpserver doesn't Write() */
681ea722969SBrian Somers   if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset))
682ea722969SBrian Somers     descriptor_Write(&bundle->ncp.mp.server.desc, bundle, fdset);
683ea722969SBrian Somers 
6842f786681SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
6852f786681SBrian Somers     if (descriptor_IsSet(&dl->desc, fdset))
6861af29a6eSBrian Somers       result += descriptor_Write(&dl->desc, bundle, fdset);
6871af29a6eSBrian Somers 
6881af29a6eSBrian Somers   return result;
6892f786681SBrian Somers }
6902f786681SBrian Somers 
691da66dd13SBrian Somers void
6921384bd27SBrian Somers bundle_LockTun(struct bundle *bundle)
6931384bd27SBrian Somers {
6941384bd27SBrian Somers   FILE *lockfile;
6951384bd27SBrian Somers   char pidfile[MAXPATHLEN];
6961384bd27SBrian Somers 
6971384bd27SBrian Somers   snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit);
6981384bd27SBrian Somers   lockfile = ID0fopen(pidfile, "w");
6991384bd27SBrian Somers   if (lockfile != NULL) {
7001384bd27SBrian Somers     fprintf(lockfile, "%d\n", (int)getpid());
7011384bd27SBrian Somers     fclose(lockfile);
7021384bd27SBrian Somers   }
7031384bd27SBrian Somers #ifndef RELEASE_CRUNCH
7041384bd27SBrian Somers   else
7051384bd27SBrian Somers     log_Printf(LogERROR, "Warning: Can't create %s: %s\n",
7061384bd27SBrian Somers                pidfile, strerror(errno));
7071384bd27SBrian Somers #endif
7081384bd27SBrian Somers }
7091384bd27SBrian Somers 
7101384bd27SBrian Somers static void
7111384bd27SBrian Somers bundle_UnlockTun(struct bundle *bundle)
7121384bd27SBrian Somers {
7131384bd27SBrian Somers   char pidfile[MAXPATHLEN];
7141384bd27SBrian Somers 
7151384bd27SBrian Somers   snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit);
7161384bd27SBrian Somers   ID0unlink(pidfile);
7171384bd27SBrian Somers }
7187a6f8720SBrian Somers 
7197a6f8720SBrian Somers struct bundle *
7208e7b8599SBrian Somers bundle_Create(const char *prefix, int type, const char **argv)
7217a6f8720SBrian Somers {
7227a6f8720SBrian Somers   static struct bundle bundle;		/* there can be only one */
7234e5196e9SBrian Somers   int enoentcount, err;
7244e5196e9SBrian Somers   const char *ifname;
7256ca65df0SBrian Somers #if defined(TUNSIFMODE) || defined(TUNSLMODE)
7260f203c7eSBrian Somers   int iff;
7270f203c7eSBrian Somers #endif
7287a6f8720SBrian Somers 
7298fa6ebe4SBrian Somers   if (bundle.iface != NULL) {	/* Already allocated ! */
730a33b2ef7SBrian Somers     log_Printf(LogALERT, "bundle_Create:  There's only one BUNDLE !\n");
7317a6f8720SBrian Somers     return NULL;
7327a6f8720SBrian Somers   }
7337a6f8720SBrian Somers 
7347a6f8720SBrian Somers   err = ENOENT;
7357a6f8720SBrian Somers   enoentcount = 0;
736107d62e7SBrian Somers   for (bundle.unit = 0; ; bundle.unit++) {
737faefde08SBrian Somers     snprintf(bundle.dev.Name, sizeof bundle.dev.Name, "%s%d",
738faefde08SBrian Somers              prefix, bundle.unit);
739faefde08SBrian Somers     bundle.dev.fd = ID0open(bundle.dev.Name, O_RDWR);
740faefde08SBrian Somers     if (bundle.dev.fd >= 0)
7417a6f8720SBrian Somers       break;
742107d62e7SBrian Somers     else if (errno == ENXIO) {
7437a6f8720SBrian Somers       err = errno;
744107d62e7SBrian Somers       break;
7457a6f8720SBrian Somers     } else if (errno == ENOENT) {
7467a6f8720SBrian Somers       if (++enoentcount > 2)
747107d62e7SBrian Somers 	break;
7487a6f8720SBrian Somers     } else
7497a6f8720SBrian Somers       err = errno;
7507a6f8720SBrian Somers   }
7517a6f8720SBrian Somers 
752faefde08SBrian Somers   if (bundle.dev.fd < 0) {
753dd7e2610SBrian Somers     log_Printf(LogWARN, "No available tunnel devices found (%s).\n",
75485b542cfSBrian Somers               strerror(err));
7557a6f8720SBrian Somers     return NULL;
7567a6f8720SBrian Somers   }
7577a6f8720SBrian Somers 
758dd7e2610SBrian Somers   log_SetTun(bundle.unit);
7598e7b8599SBrian Somers   bundle.argv = argv;
7600f781a72SBrian Somers   bundle.argv0 = argv[0];
7610f781a72SBrian Somers   bundle.argv1 = argv[1];
7627a6f8720SBrian Somers 
7638fa6ebe4SBrian Somers   ifname = strrchr(bundle.dev.Name, '/');
7648fa6ebe4SBrian Somers   if (ifname == NULL)
7658fa6ebe4SBrian Somers     ifname = bundle.dev.Name;
7667a6f8720SBrian Somers   else
7678fa6ebe4SBrian Somers     ifname++;
7688fa6ebe4SBrian Somers 
7698fa6ebe4SBrian Somers   bundle.iface = iface_Create(ifname);
7708fa6ebe4SBrian Somers   if (bundle.iface == NULL) {
7718fa6ebe4SBrian Somers     close(bundle.dev.fd);
7728fa6ebe4SBrian Somers     return NULL;
7738fa6ebe4SBrian Somers   }
7747a6f8720SBrian Somers 
7750f203c7eSBrian Somers #ifdef TUNSIFMODE
7760f203c7eSBrian Somers   /* Make sure we're POINTOPOINT */
7770f203c7eSBrian Somers   iff = IFF_POINTOPOINT;
7780f203c7eSBrian Somers   if (ID0ioctl(bundle.dev.fd, TUNSIFMODE, &iff) < 0)
7790f203c7eSBrian Somers     log_Printf(LogERROR, "bundle_Create: ioctl(TUNSIFMODE): %s\n",
7800f203c7eSBrian Somers 	       strerror(errno));
7810f203c7eSBrian Somers #endif
7820f203c7eSBrian Somers 
7836ca65df0SBrian Somers #ifdef TUNSLMODE
7846ca65df0SBrian Somers   /* Make sure we're POINTOPOINT */
7856ca65df0SBrian Somers   iff = 0;
7866ca65df0SBrian Somers   if (ID0ioctl(bundle.dev.fd, TUNSLMODE, &iff) < 0)
7876ca65df0SBrian Somers     log_Printf(LogERROR, "bundle_Create: ioctl(TUNSLMODE): %s\n",
7886ca65df0SBrian Somers 	       strerror(errno));
7896ca65df0SBrian Somers #endif
7906ca65df0SBrian Somers 
7914e5196e9SBrian Somers   if (!iface_SetFlags(bundle.iface, IFF_UP)) {
7928fa6ebe4SBrian Somers     iface_Destroy(bundle.iface);
7938fa6ebe4SBrian Somers     bundle.iface = NULL;
794faefde08SBrian Somers     close(bundle.dev.fd);
7957a6f8720SBrian Somers     return NULL;
7967a6f8720SBrian Somers   }
7977a6f8720SBrian Somers 
7988fa6ebe4SBrian Somers   log_Printf(LogPHASE, "Using interface: %s\n", ifname);
7997a6f8720SBrian Somers 
8008fa6ebe4SBrian Somers   bundle.ifSpeed = 0;
801820de6ebSBrian Somers   bundle.routing_seq = 0;
802a0cbd833SBrian Somers   bundle.phase = PHASE_DEAD;
803a0cbd833SBrian Somers   bundle.CleaningUp = 0;
804615ad4f9SBrian Somers   bundle.AliasEnabled = 0;
8057a6f8720SBrian Somers 
8066d666775SBrian Somers   bundle.fsm.LayerStart = bundle_LayerStart;
8076f384573SBrian Somers   bundle.fsm.LayerUp = bundle_LayerUp;
8086d666775SBrian Somers   bundle.fsm.LayerDown = bundle_LayerDown;
8096d666775SBrian Somers   bundle.fsm.LayerFinish = bundle_LayerFinish;
8106d666775SBrian Somers   bundle.fsm.object = &bundle;
8117a6f8720SBrian Somers 
812ab886ad0SBrian Somers   bundle.cfg.idle_timeout = NCP_IDLE_TIMEOUT;
8131342caedSBrian Somers   *bundle.cfg.auth.name = '\0';
8141342caedSBrian Somers   *bundle.cfg.auth.key = '\0';
815610b185fSBrian Somers   bundle.cfg.opt = OPT_SROUTES | OPT_IDCHECK | OPT_LOOPBACK |
816610b185fSBrian Somers                    OPT_THROUGHPUT | OPT_UTMP;
81749052c95SBrian Somers   *bundle.cfg.label = '\0';
81849052c95SBrian Somers   bundle.cfg.mtu = DEF_MTU;
81904eaa58cSBrian Somers   bundle.cfg.autoload.max.packets = 0;
82004eaa58cSBrian Somers   bundle.cfg.autoload.max.timeout = 0;
82104eaa58cSBrian Somers   bundle.cfg.autoload.min.packets = 0;
82204eaa58cSBrian Somers   bundle.cfg.autoload.min.timeout = 0;
8236f8e9f0aSBrian Somers   bundle.cfg.choked.timeout = CHOKED_TIMEOUT;
824ff0f9439SBrian Somers   bundle.phys_type.all = type;
825ff0f9439SBrian Somers   bundle.phys_type.open = 0;
826ab886ad0SBrian Somers 
8276f384573SBrian Somers   bundle.links = datalink_Create("deflink", &bundle, type);
8283006ec67SBrian Somers   if (bundle.links == NULL) {
829a33b2ef7SBrian Somers     log_Printf(LogALERT, "Cannot create data link: %s\n", strerror(errno));
8308fa6ebe4SBrian Somers     iface_Destroy(bundle.iface);
8318fa6ebe4SBrian Somers     bundle.iface = NULL;
832faefde08SBrian Somers     close(bundle.dev.fd);
8332289f246SBrian Somers     return NULL;
8342289f246SBrian Somers   }
8352289f246SBrian Somers 
8362f786681SBrian Somers   bundle.desc.type = BUNDLE_DESCRIPTOR;
8372f786681SBrian Somers   bundle.desc.UpdateSet = bundle_UpdateSet;
8382f786681SBrian Somers   bundle.desc.IsSet = bundle_IsSet;
8392f786681SBrian Somers   bundle.desc.Read = bundle_DescriptorRead;
8402f786681SBrian Somers   bundle.desc.Write = bundle_DescriptorWrite;
8412f786681SBrian Somers 
84249052c95SBrian Somers   mp_Init(&bundle.ncp.mp, &bundle);
84349052c95SBrian Somers 
84449052c95SBrian Somers   /* Send over the first physical link by default */
8455828db6dSBrian Somers   ipcp_Init(&bundle.ncp.ipcp, &bundle, &bundle.links->physical->link,
8465828db6dSBrian Somers             &bundle.fsm);
8476d666775SBrian Somers 
8485ca5389aSBrian Somers   memset(&bundle.filter, '\0', sizeof bundle.filter);
8495ca5389aSBrian Somers   bundle.filter.in.fragok = bundle.filter.in.logok = 1;
8505ca5389aSBrian Somers   bundle.filter.in.name = "IN";
8515ca5389aSBrian Somers   bundle.filter.out.fragok = bundle.filter.out.logok = 1;
8525ca5389aSBrian Somers   bundle.filter.out.name = "OUT";
8535ca5389aSBrian Somers   bundle.filter.dial.name = "DIAL";
8548390b576SBrian Somers   bundle.filter.dial.logok = 1;
8555ca5389aSBrian Somers   bundle.filter.alive.name = "ALIVE";
8565ca5389aSBrian Somers   bundle.filter.alive.logok = 1;
857cad7e742SBrian Somers   {
858cad7e742SBrian Somers     int	i;
859cad7e742SBrian Somers     for (i = 0; i < MAXFILTERS; i++) {
860cad7e742SBrian Somers         bundle.filter.in.rule[i].f_action = A_NONE;
861cad7e742SBrian Somers         bundle.filter.out.rule[i].f_action = A_NONE;
862cad7e742SBrian Somers         bundle.filter.dial.rule[i].f_action = A_NONE;
863cad7e742SBrian Somers         bundle.filter.alive.rule[i].f_action = A_NONE;
864cad7e742SBrian Somers     }
865cad7e742SBrian Somers   }
86693ee0ff2SBrian Somers   memset(&bundle.idle.timer, '\0', sizeof bundle.idle.timer);
86793ee0ff2SBrian Somers   bundle.idle.done = 0;
8685cf4388bSBrian Somers   bundle.notify.fd = -1;
86904eaa58cSBrian Somers   memset(&bundle.autoload.timer, '\0', sizeof bundle.autoload.timer);
87004eaa58cSBrian Somers   bundle.autoload.done = 0;
87104eaa58cSBrian Somers   bundle.autoload.running = 0;
8726f8e9f0aSBrian Somers   memset(&bundle.choked.timer, '\0', sizeof bundle.choked.timer);
873972a1bcfSBrian Somers #ifndef NORADIUS
874972a1bcfSBrian Somers   radius_Init(&bundle.radius);
875972a1bcfSBrian Somers #endif
87693ee0ff2SBrian Somers 
8776d666775SBrian Somers   /* Clean out any leftover crud */
8788fa6ebe4SBrian Somers   iface_Clear(bundle.iface, IFACE_CLEAR_ALL);
8796d666775SBrian Somers 
8801384bd27SBrian Somers   bundle_LockTun(&bundle);
8811384bd27SBrian Somers 
8827a6f8720SBrian Somers   return &bundle;
8837a6f8720SBrian Somers }
8847a6f8720SBrian Somers 
88568a0f0ccSBrian Somers static void
88668a0f0ccSBrian Somers bundle_DownInterface(struct bundle *bundle)
88768a0f0ccSBrian Somers {
888dd7e2610SBrian Somers   route_IfDelete(bundle, 1);
8894e5196e9SBrian Somers   iface_ClearFlags(bundle->iface, IFF_UP);
89068a0f0ccSBrian Somers }
89168a0f0ccSBrian Somers 
89268a0f0ccSBrian Somers void
89368a0f0ccSBrian Somers bundle_Destroy(struct bundle *bundle)
89468a0f0ccSBrian Somers {
8953006ec67SBrian Somers   struct datalink *dl;
896b6217683SBrian Somers 
897ea722969SBrian Somers   /*
89804eaa58cSBrian Somers    * Clean up the interface.  We don't need to timer_Stop()s, mp_Down(),
899ea722969SBrian Somers    * ipcp_CleanInterface() and bundle_DownInterface() unless we're getting
900ea722969SBrian Somers    * out under exceptional conditions such as a descriptor exception.
901ea722969SBrian Somers    */
90204eaa58cSBrian Somers   timer_Stop(&bundle->idle.timer);
9036f8e9f0aSBrian Somers   timer_Stop(&bundle->choked.timer);
90404eaa58cSBrian Somers   timer_Stop(&bundle->autoload.timer);
90566f634b6SBrian Somers   mp_Down(&bundle->ncp.mp);
906dd7e2610SBrian Somers   ipcp_CleanInterface(&bundle->ncp.ipcp);
90768a0f0ccSBrian Somers   bundle_DownInterface(bundle);
9083006ec67SBrian Somers 
909972a1bcfSBrian Somers #ifndef NORADIUS
910972a1bcfSBrian Somers   /* Tell the radius server the bad news */
911972a1bcfSBrian Somers   radius_Destroy(&bundle->radius);
912972a1bcfSBrian Somers #endif
913972a1bcfSBrian Somers 
914ea722969SBrian Somers   /* Again, these are all DATALINK_CLOSED unless we're abending */
9153006ec67SBrian Somers   dl = bundle->links;
9163006ec67SBrian Somers   while (dl)
9173006ec67SBrian Somers     dl = datalink_Destroy(dl);
9183006ec67SBrian Somers 
9191384bd27SBrian Somers   close(bundle->dev.fd);
9201384bd27SBrian Somers   bundle_UnlockTun(bundle);
9211384bd27SBrian Somers 
922ea722969SBrian Somers   /* In case we never made PHASE_NETWORK */
9235cf4388bSBrian Somers   bundle_Notify(bundle, EX_ERRDEAD);
924b6217683SBrian Somers 
9258fa6ebe4SBrian Somers   iface_Destroy(bundle->iface);
9268fa6ebe4SBrian Somers   bundle->iface = NULL;
92768a0f0ccSBrian Somers }
92868a0f0ccSBrian Somers 
9297a6f8720SBrian Somers struct rtmsg {
9307a6f8720SBrian Somers   struct rt_msghdr m_rtm;
9317a6f8720SBrian Somers   char m_space[64];
9327a6f8720SBrian Somers };
9337a6f8720SBrian Somers 
934610b185fSBrian Somers int
935820de6ebSBrian Somers bundle_SetRoute(struct bundle *bundle, int cmd, struct in_addr dst,
9362062443fSBrian Somers                 struct in_addr gateway, struct in_addr mask, int bang, int ssh)
9377a6f8720SBrian Somers {
9387a6f8720SBrian Somers   struct rtmsg rtmes;
9397a6f8720SBrian Somers   int s, nb, wb;
9407a6f8720SBrian Somers   char *cp;
9417a6f8720SBrian Somers   const char *cmdstr;
9427a6f8720SBrian Somers   struct sockaddr_in rtdata;
943610b185fSBrian Somers   int result = 1;
9447a6f8720SBrian Somers 
9457a6f8720SBrian Somers   if (bang)
9467a6f8720SBrian Somers     cmdstr = (cmd == RTM_ADD ? "Add!" : "Delete!");
9477a6f8720SBrian Somers   else
9487a6f8720SBrian Somers     cmdstr = (cmd == RTM_ADD ? "Add" : "Delete");
9497a6f8720SBrian Somers   s = ID0socket(PF_ROUTE, SOCK_RAW, 0);
9507a6f8720SBrian Somers   if (s < 0) {
951dd7e2610SBrian Somers     log_Printf(LogERROR, "bundle_SetRoute: socket(): %s\n", strerror(errno));
952610b185fSBrian Somers     return result;
9537a6f8720SBrian Somers   }
9547a6f8720SBrian Somers   memset(&rtmes, '\0', sizeof rtmes);
9557a6f8720SBrian Somers   rtmes.m_rtm.rtm_version = RTM_VERSION;
9567a6f8720SBrian Somers   rtmes.m_rtm.rtm_type = cmd;
9577a6f8720SBrian Somers   rtmes.m_rtm.rtm_addrs = RTA_DST;
958820de6ebSBrian Somers   rtmes.m_rtm.rtm_seq = ++bundle->routing_seq;
9597a6f8720SBrian Somers   rtmes.m_rtm.rtm_pid = getpid();
9607a6f8720SBrian Somers   rtmes.m_rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;
9617a6f8720SBrian Somers 
9623afe5ccbSBrian Somers   if (cmd == RTM_ADD || cmd == RTM_CHANGE) {
9633afe5ccbSBrian Somers     if (bundle->ncp.ipcp.cfg.sendpipe > 0) {
9643afe5ccbSBrian Somers       rtmes.m_rtm.rtm_rmx.rmx_sendpipe = bundle->ncp.ipcp.cfg.sendpipe;
9653afe5ccbSBrian Somers       rtmes.m_rtm.rtm_inits |= RTV_SPIPE;
9663afe5ccbSBrian Somers     }
9673afe5ccbSBrian Somers     if (bundle->ncp.ipcp.cfg.recvpipe > 0) {
9683afe5ccbSBrian Somers       rtmes.m_rtm.rtm_rmx.rmx_recvpipe = bundle->ncp.ipcp.cfg.recvpipe;
9693afe5ccbSBrian Somers       rtmes.m_rtm.rtm_inits |= RTV_RPIPE;
9703afe5ccbSBrian Somers     }
9713afe5ccbSBrian Somers   }
9723afe5ccbSBrian Somers 
9737a6f8720SBrian Somers   memset(&rtdata, '\0', sizeof rtdata);
97450e5c17dSBrian Somers   rtdata.sin_len = sizeof rtdata;
9757a6f8720SBrian Somers   rtdata.sin_family = AF_INET;
9767a6f8720SBrian Somers   rtdata.sin_port = 0;
9777a6f8720SBrian Somers   rtdata.sin_addr = dst;
9787a6f8720SBrian Somers 
9797a6f8720SBrian Somers   cp = rtmes.m_space;
98050e5c17dSBrian Somers   memcpy(cp, &rtdata, rtdata.sin_len);
98150e5c17dSBrian Somers   cp += rtdata.sin_len;
982e43ebac1SBrian Somers   if (cmd == RTM_ADD) {
9837a6f8720SBrian Somers     if (gateway.s_addr == INADDR_ANY) {
9840aa8aa17SBrian Somers       if (!ssh)
9858fa6ebe4SBrian Somers         log_Printf(LogERROR, "bundle_SetRoute: Cannot add a route with"
9868fa6ebe4SBrian Somers                    " destination 0.0.0.0\n");
9878fa6ebe4SBrian Somers       close(s);
9888fa6ebe4SBrian Somers       return result;
9897a6f8720SBrian Somers     } else {
9907a6f8720SBrian Somers       rtdata.sin_addr = gateway;
99150e5c17dSBrian Somers       memcpy(cp, &rtdata, rtdata.sin_len);
99250e5c17dSBrian Somers       cp += rtdata.sin_len;
9937a6f8720SBrian Somers       rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY;
9947a6f8720SBrian Somers     }
995e43ebac1SBrian Somers   }
9967a6f8720SBrian Somers 
9977a6f8720SBrian Somers   if (dst.s_addr == INADDR_ANY)
9987a6f8720SBrian Somers     mask.s_addr = INADDR_ANY;
9997a6f8720SBrian Somers 
10007a6f8720SBrian Somers   if (cmd == RTM_ADD || dst.s_addr == INADDR_ANY) {
10017a6f8720SBrian Somers     rtdata.sin_addr = mask;
100250e5c17dSBrian Somers     memcpy(cp, &rtdata, rtdata.sin_len);
100350e5c17dSBrian Somers     cp += rtdata.sin_len;
10047a6f8720SBrian Somers     rtmes.m_rtm.rtm_addrs |= RTA_NETMASK;
10057a6f8720SBrian Somers   }
10067a6f8720SBrian Somers 
10077a6f8720SBrian Somers   nb = cp - (char *) &rtmes;
10087a6f8720SBrian Somers   rtmes.m_rtm.rtm_msglen = nb;
10097a6f8720SBrian Somers   wb = ID0write(s, &rtmes, nb);
10107a6f8720SBrian Somers   if (wb < 0) {
1011dd7e2610SBrian Somers     log_Printf(LogTCPIP, "bundle_SetRoute failure:\n");
1012dd7e2610SBrian Somers     log_Printf(LogTCPIP, "bundle_SetRoute:  Cmd = %s\n", cmdstr);
1013dd7e2610SBrian Somers     log_Printf(LogTCPIP, "bundle_SetRoute:  Dst = %s\n", inet_ntoa(dst));
1014dd7e2610SBrian Somers     log_Printf(LogTCPIP, "bundle_SetRoute:  Gateway = %s\n", inet_ntoa(gateway));
1015dd7e2610SBrian Somers     log_Printf(LogTCPIP, "bundle_SetRoute:  Mask = %s\n", inet_ntoa(mask));
10167a6f8720SBrian Somers failed:
10177a6f8720SBrian Somers     if (cmd == RTM_ADD && (rtmes.m_rtm.rtm_errno == EEXIST ||
1018e43ebac1SBrian Somers                            (rtmes.m_rtm.rtm_errno == 0 && errno == EEXIST))) {
1019610b185fSBrian Somers       if (!bang) {
1020dd7e2610SBrian Somers         log_Printf(LogWARN, "Add route failed: %s already exists\n",
10213afe5ccbSBrian Somers 		  dst.s_addr == 0 ? "default" : inet_ntoa(dst));
1022610b185fSBrian Somers         result = 0;	/* Don't add to our dynamic list */
1023610b185fSBrian Somers       } else {
10247a6f8720SBrian Somers         rtmes.m_rtm.rtm_type = cmd = RTM_CHANGE;
10257a6f8720SBrian Somers         if ((wb = ID0write(s, &rtmes, nb)) < 0)
10267a6f8720SBrian Somers           goto failed;
10277a6f8720SBrian Somers       }
1028e43ebac1SBrian Somers     } else if (cmd == RTM_DELETE &&
10297a6f8720SBrian Somers              (rtmes.m_rtm.rtm_errno == ESRCH ||
10307a6f8720SBrian Somers               (rtmes.m_rtm.rtm_errno == 0 && errno == ESRCH))) {
10317a6f8720SBrian Somers       if (!bang)
1032dd7e2610SBrian Somers         log_Printf(LogWARN, "Del route failed: %s: Non-existent\n",
10337a6f8720SBrian Somers                   inet_ntoa(dst));
10342062443fSBrian Somers     } else if (rtmes.m_rtm.rtm_errno == 0) {
10352062443fSBrian Somers       if (!ssh || errno != ENETUNREACH)
1036dd7e2610SBrian Somers         log_Printf(LogWARN, "%s route failed: %s: errno: %s\n", cmdstr,
10377a6f8720SBrian Somers                    inet_ntoa(dst), strerror(errno));
10382062443fSBrian Somers     } else
1039dd7e2610SBrian Somers       log_Printf(LogWARN, "%s route failed: %s: %s\n",
10407a6f8720SBrian Somers 		 cmdstr, inet_ntoa(dst), strerror(rtmes.m_rtm.rtm_errno));
10417a6f8720SBrian Somers   }
1042dd7e2610SBrian Somers   log_Printf(LogDEBUG, "wrote %d: cmd = %s, dst = %x, gateway = %x\n",
1043fe3125a0SBrian Somers             wb, cmdstr, (unsigned)dst.s_addr, (unsigned)gateway.s_addr);
10447a6f8720SBrian Somers   close(s);
1045610b185fSBrian Somers 
1046610b185fSBrian Somers   return result;
10477a6f8720SBrian Somers }
104883d1af55SBrian Somers 
104983d1af55SBrian Somers void
10503006ec67SBrian Somers bundle_LinkClosed(struct bundle *bundle, struct datalink *dl)
10513006ec67SBrian Somers {
10523006ec67SBrian Somers   /*
10533006ec67SBrian Somers    * Our datalink has closed.
1054ea722969SBrian Somers    * CleanDatalinks() (called from DoLoop()) will remove closed
105581358fa3SBrian Somers    * BACKGROUND and DIRECT links.
10563b0f8d2eSBrian Somers    * If it's the last data link, enter phase DEAD.
1057ea722969SBrian Somers    *
1058ea722969SBrian Somers    * NOTE: dl may not be in our list (bundle_SendDatalink()) !
10593006ec67SBrian Somers    */
10605b8b8060SBrian Somers 
10613b0f8d2eSBrian Somers   struct datalink *odl;
10623b0f8d2eSBrian Somers   int other_links;
10635b8b8060SBrian Somers 
1064bf1d3ff6SBrian Somers   log_SetTtyCommandMode(dl);
1065bf1d3ff6SBrian Somers 
10663b0f8d2eSBrian Somers   other_links = 0;
10673b0f8d2eSBrian Somers   for (odl = bundle->links; odl; odl = odl->next)
10683b0f8d2eSBrian Somers     if (odl != dl && odl->state != DATALINK_CLOSED)
10693b0f8d2eSBrian Somers       other_links++;
10703b0f8d2eSBrian Somers 
10713b0f8d2eSBrian Somers   if (!other_links) {
107281358fa3SBrian Somers     if (dl->physical->type != PHYS_AUTO)	/* Not in -auto mode */
107303704096SBrian Somers       bundle_DownInterface(bundle);
107409206a6fSBrian Somers     fsm2initial(&bundle->ncp.ipcp.fsm);
10755563ebdeSBrian Somers     bundle_NewPhase(bundle, PHASE_DEAD);
10760f2f3eb3SBrian Somers     bundle_StopIdleTimer(bundle);
107704eaa58cSBrian Somers     bundle_StopAutoLoadTimer(bundle);
107804eaa58cSBrian Somers     bundle->autoload.running = 0;
107904eaa58cSBrian Somers   } else
108004eaa58cSBrian Somers     bundle->autoload.running = 1;
1081455aabc3SBrian Somers }
1082455aabc3SBrian Somers 
1083455aabc3SBrian Somers void
1084ba23f397SBrian Somers bundle_Open(struct bundle *bundle, const char *name, int mask, int force)
10853006ec67SBrian Somers {
10863006ec67SBrian Somers   /*
10873006ec67SBrian Somers    * Please open the given datalink, or all if name == NULL
10883006ec67SBrian Somers    */
10893006ec67SBrian Somers   struct datalink *dl;
10903006ec67SBrian Somers 
109104eaa58cSBrian Somers   timer_Stop(&bundle->autoload.timer);
10923006ec67SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
10933006ec67SBrian Somers     if (name == NULL || !strcasecmp(dl->name, name)) {
1094ba23f397SBrian Somers       if ((mask & dl->physical->type) &&
1095ba23f397SBrian Somers           (dl->state == DATALINK_CLOSED ||
1096ba23f397SBrian Somers            (force && dl->state == DATALINK_OPENING &&
1097c11e57a3SBrian Somers             dl->dial.timer.state == TIMER_RUNNING))) {
1098c11e57a3SBrian Somers         if (force)	/* Ignore redial timeout ? */
1099c11e57a3SBrian Somers           timer_Stop(&dl->dial.timer);
1100565e35e5SBrian Somers         datalink_Up(dl, 1, 1);
110181358fa3SBrian Somers         if (mask == PHYS_AUTO)
110281358fa3SBrian Somers           /* Only one AUTO link at a time (see the AutoLoad timer) */
110304eaa58cSBrian Somers           break;
110404eaa58cSBrian Somers       }
11053006ec67SBrian Somers       if (name != NULL)
11063006ec67SBrian Somers         break;
11073006ec67SBrian Somers     }
11083006ec67SBrian Somers }
11093006ec67SBrian Somers 
11103006ec67SBrian Somers struct datalink *
11113006ec67SBrian Somers bundle2datalink(struct bundle *bundle, const char *name)
11123006ec67SBrian Somers {
11133006ec67SBrian Somers   struct datalink *dl;
11143006ec67SBrian Somers 
11153006ec67SBrian Somers   if (name != NULL) {
11163006ec67SBrian Somers     for (dl = bundle->links; dl; dl = dl->next)
11173006ec67SBrian Somers       if (!strcasecmp(dl->name, name))
11183006ec67SBrian Somers         return dl;
11193006ec67SBrian Somers   } else if (bundle->links && !bundle->links->next)
11203006ec67SBrian Somers     return bundle->links;
11213006ec67SBrian Somers 
11223006ec67SBrian Somers   return NULL;
11233006ec67SBrian Somers }
11243006ec67SBrian Somers 
11253006ec67SBrian Somers int
11263006ec67SBrian Somers bundle_FillQueues(struct bundle *bundle)
11273006ec67SBrian Somers {
11283b0f8d2eSBrian Somers   int total;
11293b0f8d2eSBrian Somers 
11301bc9b5baSBrian Somers   if (bundle->ncp.mp.active)
11313b0f8d2eSBrian Somers     total = mp_FillQueues(bundle);
11321bc9b5baSBrian Somers   else {
11331bc9b5baSBrian Somers     struct datalink *dl;
11341bc9b5baSBrian Somers     int add;
11351bc9b5baSBrian Somers 
11361bc9b5baSBrian Somers     for (total = 0, dl = bundle->links; dl; dl = dl->next)
11371bc9b5baSBrian Somers       if (dl->state == DATALINK_OPEN) {
11381bc9b5baSBrian Somers         add = link_QueueLen(&dl->physical->link);
11391bc9b5baSBrian Somers         if (add == 0 && dl->physical->out == NULL)
11405d9e6103SBrian Somers           add = ip_PushPacket(&dl->physical->link, bundle);
11411bc9b5baSBrian Somers         total += add;
11421bc9b5baSBrian Somers       }
11433006ec67SBrian Somers   }
11443006ec67SBrian Somers 
11455a72b6edSBrian Somers   return total + ip_QueueLen(&bundle->ncp.ipcp);
11463006ec67SBrian Somers }
1147aef795ccSBrian Somers 
1148aef795ccSBrian Somers int
1149aef795ccSBrian Somers bundle_ShowLinks(struct cmdargs const *arg)
1150aef795ccSBrian Somers {
1151aef795ccSBrian Somers   struct datalink *dl;
1152aef795ccSBrian Somers 
11539c53a7b1SBrian Somers   for (dl = arg->bundle->links; dl; dl = dl->next) {
1154d4156d00SBrian Somers     prompt_Printf(arg->prompt, "Name: %s [%s, %s]",
1155d4156d00SBrian Somers                   dl->name, mode2Nam(dl->physical->type), datalink_State(dl));
1156eeab6bf5SBrian Somers     if (dl->physical->link.throughput.rolling && dl->state == DATALINK_OPEN)
11575d9e6103SBrian Somers       prompt_Printf(arg->prompt, " weight %d, %Ld bytes/sec",
1158eeab6bf5SBrian Somers                     dl->mp.weight,
11599c53a7b1SBrian Somers                     dl->physical->link.throughput.OctetsPerSecond);
11609c53a7b1SBrian Somers     prompt_Printf(arg->prompt, "\n");
11619c53a7b1SBrian Somers   }
1162aef795ccSBrian Somers 
1163aef795ccSBrian Somers   return 0;
1164aef795ccSBrian Somers }
1165ab886ad0SBrian Somers 
11661342caedSBrian Somers static const char *
11671342caedSBrian Somers optval(struct bundle *bundle, int bit)
11681342caedSBrian Somers {
11691342caedSBrian Somers   return (bundle->cfg.opt & bit) ? "enabled" : "disabled";
11701342caedSBrian Somers }
11711342caedSBrian Somers 
1172c08717dfSBrian Somers int
1173c08717dfSBrian Somers bundle_ShowStatus(struct cmdargs const *arg)
1174c08717dfSBrian Somers {
1175c08717dfSBrian Somers   int remaining;
1176c08717dfSBrian Somers 
1177c08717dfSBrian Somers   prompt_Printf(arg->prompt, "Phase %s\n", bundle_PhaseName(arg->bundle));
11780f781a72SBrian Somers   prompt_Printf(arg->prompt, " Title:         %s\n", arg->bundle->argv[0]);
1179faefde08SBrian Somers   prompt_Printf(arg->prompt, " Device:        %s\n", arg->bundle->dev.Name);
1180faefde08SBrian Somers   prompt_Printf(arg->prompt, " Interface:     %s @ %lubps\n",
11818fa6ebe4SBrian Somers                 arg->bundle->iface->name, arg->bundle->ifSpeed);
1182c08717dfSBrian Somers 
1183c08717dfSBrian Somers   prompt_Printf(arg->prompt, "\nDefaults:\n");
1184643f4904SBrian Somers   prompt_Printf(arg->prompt, " Label:         %s\n", arg->bundle->cfg.label);
1185610b185fSBrian Somers   prompt_Printf(arg->prompt, " Auth name:     %s\n",
1186610b185fSBrian Somers                 arg->bundle->cfg.auth.name);
118704eaa58cSBrian Somers   prompt_Printf(arg->prompt, " Auto Load:     Up after %ds of >= %d packets\n",
118804eaa58cSBrian Somers                 arg->bundle->cfg.autoload.max.timeout,
118904eaa58cSBrian Somers                 arg->bundle->cfg.autoload.max.packets);
119004eaa58cSBrian Somers   prompt_Printf(arg->prompt, "                Down after %ds of <= %d"
119104eaa58cSBrian Somers                 " packets\n", arg->bundle->cfg.autoload.min.timeout,
119204eaa58cSBrian Somers                 arg->bundle->cfg.autoload.min.packets);
119304eaa58cSBrian Somers   if (arg->bundle->autoload.timer.state == TIMER_RUNNING)
119404eaa58cSBrian Somers     prompt_Printf(arg->prompt, "                %ds remaining 'till "
119504eaa58cSBrian Somers                   "a link comes %s\n",
119604eaa58cSBrian Somers                   bundle_RemainingAutoLoadTime(arg->bundle),
119704eaa58cSBrian Somers                   arg->bundle->autoload.comingup ? "up" : "down");
119804eaa58cSBrian Somers   else
119904eaa58cSBrian Somers     prompt_Printf(arg->prompt, "                %srunning with %d"
120004eaa58cSBrian Somers                   " packets queued\n", arg->bundle->autoload.running ?
12015a72b6edSBrian Somers                   "" : "not ", ip_QueueLen(&arg->bundle->ncp.ipcp));
120204eaa58cSBrian Somers 
12036f8e9f0aSBrian Somers   prompt_Printf(arg->prompt, " Choked Timer:  %ds\n",
12046f8e9f0aSBrian Somers                 arg->bundle->cfg.choked.timeout);
1205972a1bcfSBrian Somers 
1206972a1bcfSBrian Somers #ifndef NORADIUS
1207972a1bcfSBrian Somers   radius_Show(&arg->bundle->radius, arg->prompt);
1208972a1bcfSBrian Somers #endif
1209972a1bcfSBrian Somers 
1210c08717dfSBrian Somers   prompt_Printf(arg->prompt, " Idle Timer:    ");
1211c08717dfSBrian Somers   if (arg->bundle->cfg.idle_timeout) {
1212c08717dfSBrian Somers     prompt_Printf(arg->prompt, "%ds", arg->bundle->cfg.idle_timeout);
1213c08717dfSBrian Somers     remaining = bundle_RemainingIdleTime(arg->bundle);
1214c08717dfSBrian Somers     if (remaining != -1)
1215c08717dfSBrian Somers       prompt_Printf(arg->prompt, " (%ds remaining)", remaining);
1216c08717dfSBrian Somers     prompt_Printf(arg->prompt, "\n");
1217c08717dfSBrian Somers   } else
1218c08717dfSBrian Somers     prompt_Printf(arg->prompt, "disabled\n");
1219ce828a6eSBrian Somers   prompt_Printf(arg->prompt, " MTU:           ");
1220ce828a6eSBrian Somers   if (arg->bundle->cfg.mtu)
1221ce828a6eSBrian Somers     prompt_Printf(arg->prompt, "%d\n", arg->bundle->cfg.mtu);
1222ce828a6eSBrian Somers   else
1223ce828a6eSBrian Somers     prompt_Printf(arg->prompt, "unspecified\n");
1224c08717dfSBrian Somers 
12253afe5ccbSBrian Somers   prompt_Printf(arg->prompt, " sendpipe:      ");
12263afe5ccbSBrian Somers   if (arg->bundle->ncp.ipcp.cfg.sendpipe > 0)
12273afe5ccbSBrian Somers     prompt_Printf(arg->prompt, "%ld\n", arg->bundle->ncp.ipcp.cfg.sendpipe);
12283afe5ccbSBrian Somers   else
12293afe5ccbSBrian Somers     prompt_Printf(arg->prompt, "unspecified\n");
12303afe5ccbSBrian Somers   prompt_Printf(arg->prompt, " recvpipe:      ");
12313afe5ccbSBrian Somers   if (arg->bundle->ncp.ipcp.cfg.recvpipe > 0)
12323afe5ccbSBrian Somers     prompt_Printf(arg->prompt, "%ld\n", arg->bundle->ncp.ipcp.cfg.recvpipe);
12333afe5ccbSBrian Somers   else
12343afe5ccbSBrian Somers     prompt_Printf(arg->prompt, "unspecified\n");
12353afe5ccbSBrian Somers 
1236610b185fSBrian Somers   prompt_Printf(arg->prompt, " Sticky Routes: %s\n",
1237610b185fSBrian Somers                 optval(arg->bundle, OPT_SROUTES));
12381342caedSBrian Somers   prompt_Printf(arg->prompt, " ID check:      %s\n",
12391342caedSBrian Somers                 optval(arg->bundle, OPT_IDCHECK));
1240ac685e31SBrian Somers   prompt_Printf(arg->prompt, " Keep-Session:  %s\n",
1241ac685e31SBrian Somers                 optval(arg->bundle, OPT_KEEPSESSION));
12421342caedSBrian Somers   prompt_Printf(arg->prompt, " Loopback:      %s\n",
12431342caedSBrian Somers                 optval(arg->bundle, OPT_LOOPBACK));
12441342caedSBrian Somers   prompt_Printf(arg->prompt, " PasswdAuth:    %s\n",
12451342caedSBrian Somers                 optval(arg->bundle, OPT_PASSWDAUTH));
12461342caedSBrian Somers   prompt_Printf(arg->prompt, " Proxy:         %s\n",
12471342caedSBrian Somers                 optval(arg->bundle, OPT_PROXY));
12483afe5ccbSBrian Somers   prompt_Printf(arg->prompt, " Proxyall:      %s\n",
12493afe5ccbSBrian Somers                 optval(arg->bundle, OPT_PROXYALL));
12501342caedSBrian Somers   prompt_Printf(arg->prompt, " Throughput:    %s\n",
12511342caedSBrian Somers                 optval(arg->bundle, OPT_THROUGHPUT));
1252610b185fSBrian Somers   prompt_Printf(arg->prompt, " Utmp Logging:  %s\n",
12531342caedSBrian Somers                 optval(arg->bundle, OPT_UTMP));
12548fa6ebe4SBrian Somers   prompt_Printf(arg->prompt, " Iface-Alias:   %s\n",
12558fa6ebe4SBrian Somers                 optval(arg->bundle, OPT_IFACEALIAS));
12561342caedSBrian Somers 
1257c08717dfSBrian Somers   return 0;
1258c08717dfSBrian Somers }
1259c08717dfSBrian Somers 
1260ab886ad0SBrian Somers static void
1261ab886ad0SBrian Somers bundle_IdleTimeout(void *v)
1262ab886ad0SBrian Somers {
1263ab886ad0SBrian Somers   struct bundle *bundle = (struct bundle *)v;
1264ab886ad0SBrian Somers 
1265dd7e2610SBrian Somers   log_Printf(LogPHASE, "Idle timer expired.\n");
126604eaa58cSBrian Somers   bundle_StopIdleTimer(bundle);
12679c81b87dSBrian Somers   bundle_Close(bundle, NULL, CLOSE_STAYDOWN);
1268ab886ad0SBrian Somers }
1269ab886ad0SBrian Somers 
1270ab886ad0SBrian Somers /*
1271ab886ad0SBrian Somers  *  Start Idle timer. If timeout is reached, we call bundle_Close() to
1272ab886ad0SBrian Somers  *  close LCP and link.
1273ab886ad0SBrian Somers  */
1274ab886ad0SBrian Somers void
1275ab886ad0SBrian Somers bundle_StartIdleTimer(struct bundle *bundle)
1276ab886ad0SBrian Somers {
1277dd7e2610SBrian Somers   timer_Stop(&bundle->idle.timer);
1278ff0f9439SBrian Somers   if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) !=
1279ff0f9439SBrian Somers       bundle->phys_type.open && bundle->cfg.idle_timeout) {
128093ee0ff2SBrian Somers     bundle->idle.timer.func = bundle_IdleTimeout;
12813b0f8d2eSBrian Somers     bundle->idle.timer.name = "idle";
128293ee0ff2SBrian Somers     bundle->idle.timer.load = bundle->cfg.idle_timeout * SECTICKS;
128393ee0ff2SBrian Somers     bundle->idle.timer.arg = bundle;
1284dd7e2610SBrian Somers     timer_Start(&bundle->idle.timer);
128593ee0ff2SBrian Somers     bundle->idle.done = time(NULL) + bundle->cfg.idle_timeout;
1286ab886ad0SBrian Somers   }
1287ab886ad0SBrian Somers }
1288ab886ad0SBrian Somers 
1289ab886ad0SBrian Somers void
1290ab886ad0SBrian Somers bundle_SetIdleTimer(struct bundle *bundle, int value)
1291ab886ad0SBrian Somers {
1292ab886ad0SBrian Somers   bundle->cfg.idle_timeout = value;
1293ab886ad0SBrian Somers   if (bundle_LinkIsUp(bundle))
1294ab886ad0SBrian Somers     bundle_StartIdleTimer(bundle);
1295ab886ad0SBrian Somers }
1296ab886ad0SBrian Somers 
1297ab886ad0SBrian Somers void
1298ab886ad0SBrian Somers bundle_StopIdleTimer(struct bundle *bundle)
1299ab886ad0SBrian Somers {
1300dd7e2610SBrian Somers   timer_Stop(&bundle->idle.timer);
13014a632c80SBrian Somers   bundle->idle.done = 0;
1302ab886ad0SBrian Somers }
1303ab886ad0SBrian Somers 
130404eaa58cSBrian Somers static int
1305ab886ad0SBrian Somers bundle_RemainingIdleTime(struct bundle *bundle)
1306ab886ad0SBrian Somers {
130793ee0ff2SBrian Somers   if (bundle->idle.done)
130893ee0ff2SBrian Somers     return bundle->idle.done - time(NULL);
1309ab886ad0SBrian Somers   return -1;
1310ab886ad0SBrian Somers }
13113b0f8d2eSBrian Somers 
13123b0f8d2eSBrian Somers int
13133b0f8d2eSBrian Somers bundle_IsDead(struct bundle *bundle)
13143b0f8d2eSBrian Somers {
13153b0f8d2eSBrian Somers   return !bundle->links || (bundle->phase == PHASE_DEAD && bundle->CleaningUp);
13163b0f8d2eSBrian Somers }
1317b6217683SBrian Somers 
131804eaa58cSBrian Somers static struct datalink *
131904eaa58cSBrian Somers bundle_DatalinkLinkout(struct bundle *bundle, struct datalink *dl)
1320cd7bd93aSBrian Somers {
1321cd7bd93aSBrian Somers   struct datalink **dlp;
1322cd7bd93aSBrian Somers 
1323cd7bd93aSBrian Somers   for (dlp = &bundle->links; *dlp; dlp = &(*dlp)->next)
1324cd7bd93aSBrian Somers     if (*dlp == dl) {
132504eaa58cSBrian Somers       *dlp = dl->next;
132604eaa58cSBrian Somers       dl->next = NULL;
132704eaa58cSBrian Somers       bundle_LinksRemoved(bundle);
132804eaa58cSBrian Somers       return dl;
1329cd7bd93aSBrian Somers     }
133004eaa58cSBrian Somers 
133104eaa58cSBrian Somers   return NULL;
133204eaa58cSBrian Somers }
133304eaa58cSBrian Somers 
133404eaa58cSBrian Somers static void
133504eaa58cSBrian Somers bundle_DatalinkLinkin(struct bundle *bundle, struct datalink *dl)
133604eaa58cSBrian Somers {
133704eaa58cSBrian Somers   struct datalink **dlp = &bundle->links;
133804eaa58cSBrian Somers 
133904eaa58cSBrian Somers   while (*dlp)
134004eaa58cSBrian Somers     dlp = &(*dlp)->next;
134104eaa58cSBrian Somers 
134204eaa58cSBrian Somers   *dlp = dl;
134304eaa58cSBrian Somers   dl->next = NULL;
134404eaa58cSBrian Somers 
134504eaa58cSBrian Somers   bundle_LinkAdded(bundle, dl);
1346565e35e5SBrian Somers }
1347565e35e5SBrian Somers 
1348565e35e5SBrian Somers void
1349565e35e5SBrian Somers bundle_CleanDatalinks(struct bundle *bundle)
1350565e35e5SBrian Somers {
1351565e35e5SBrian Somers   struct datalink **dlp = &bundle->links;
135204eaa58cSBrian Somers   int found = 0;
1353565e35e5SBrian Somers 
1354565e35e5SBrian Somers   while (*dlp)
1355565e35e5SBrian Somers     if ((*dlp)->state == DATALINK_CLOSED &&
135681358fa3SBrian Somers         (*dlp)->physical->type & (PHYS_DIRECT|PHYS_BACKGROUND)) {
1357565e35e5SBrian Somers       *dlp = datalink_Destroy(*dlp);
135804eaa58cSBrian Somers       found++;
135904eaa58cSBrian Somers     } else
1360565e35e5SBrian Somers       dlp = &(*dlp)->next;
136104eaa58cSBrian Somers 
136204eaa58cSBrian Somers   if (found)
136304eaa58cSBrian Somers     bundle_LinksRemoved(bundle);
136404eaa58cSBrian Somers }
136504eaa58cSBrian Somers 
136604eaa58cSBrian Somers int
136704eaa58cSBrian Somers bundle_DatalinkClone(struct bundle *bundle, struct datalink *dl,
136804eaa58cSBrian Somers                      const char *name)
136904eaa58cSBrian Somers {
137004eaa58cSBrian Somers   if (bundle2datalink(bundle, name)) {
137104eaa58cSBrian Somers     log_Printf(LogWARN, "Clone: %s: name already exists\n", name);
137204eaa58cSBrian Somers     return 0;
137304eaa58cSBrian Somers   }
137404eaa58cSBrian Somers 
137504eaa58cSBrian Somers   bundle_DatalinkLinkin(bundle, datalink_Clone(dl, name));
137604eaa58cSBrian Somers   return 1;
137704eaa58cSBrian Somers }
137804eaa58cSBrian Somers 
137904eaa58cSBrian Somers void
138004eaa58cSBrian Somers bundle_DatalinkRemove(struct bundle *bundle, struct datalink *dl)
138104eaa58cSBrian Somers {
138204eaa58cSBrian Somers   dl = bundle_DatalinkLinkout(bundle, dl);
138304eaa58cSBrian Somers   if (dl)
138404eaa58cSBrian Somers     datalink_Destroy(dl);
1385cd7bd93aSBrian Somers }
138649052c95SBrian Somers 
138749052c95SBrian Somers void
138849052c95SBrian Somers bundle_SetLabel(struct bundle *bundle, const char *label)
138949052c95SBrian Somers {
139049052c95SBrian Somers   if (label)
139149052c95SBrian Somers     strncpy(bundle->cfg.label, label, sizeof bundle->cfg.label - 1);
139249052c95SBrian Somers   else
139349052c95SBrian Somers     *bundle->cfg.label = '\0';
139449052c95SBrian Somers }
139549052c95SBrian Somers 
139649052c95SBrian Somers const char *
139749052c95SBrian Somers bundle_GetLabel(struct bundle *bundle)
139849052c95SBrian Somers {
139949052c95SBrian Somers   return *bundle->cfg.label ? bundle->cfg.label : NULL;
140049052c95SBrian Somers }
14011fa665f5SBrian Somers 
14021fa665f5SBrian Somers void
140396c9bb21SBrian Somers bundle_ReceiveDatalink(struct bundle *bundle, int s, struct sockaddr_un *sun)
14041fa665f5SBrian Somers {
140596c9bb21SBrian Somers   char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int)];
140696c9bb21SBrian Somers   struct cmsghdr *cmsg = (struct cmsghdr *)cmsgbuf;
140796c9bb21SBrian Somers   struct msghdr msg;
140896c9bb21SBrian Somers   struct iovec iov[SCATTER_SEGMENTS];
1409b7c5748eSBrian Somers   struct datalink *dl;
141096c9bb21SBrian Somers   int niov, link_fd, expect, f;
141185fd273aSBrian Somers   pid_t pid;
14126f384573SBrian Somers 
1413dd7e2610SBrian Somers   log_Printf(LogPHASE, "Receiving datalink\n");
14146f384573SBrian Somers 
141596c9bb21SBrian Somers   /* Create our scatter/gather array */
141696c9bb21SBrian Somers   niov = 1;
141796c9bb21SBrian Somers   iov[0].iov_len = strlen(Version) + 1;
141896c9bb21SBrian Somers   iov[0].iov_base = (char *)malloc(iov[0].iov_len);
141985fd273aSBrian Somers   if (datalink2iov(NULL, iov, &niov, sizeof iov / sizeof *iov, 0) == -1) {
142054cd8e13SBrian Somers     close(s);
14216f384573SBrian Somers     return;
142254cd8e13SBrian Somers   }
14236f384573SBrian Somers 
142485fd273aSBrian Somers   pid = getpid();
142585fd273aSBrian Somers   write(s, &pid, sizeof pid);
142685fd273aSBrian Somers 
142796c9bb21SBrian Somers   for (f = expect = 0; f < niov; f++)
142896c9bb21SBrian Somers     expect += iov[f].iov_len;
142996c9bb21SBrian Somers 
143096c9bb21SBrian Somers   /* Set up our message */
143196c9bb21SBrian Somers   cmsg->cmsg_len = sizeof cmsgbuf;
143296c9bb21SBrian Somers   cmsg->cmsg_level = SOL_SOCKET;
143354cd8e13SBrian Somers   cmsg->cmsg_type = 0;
143496c9bb21SBrian Somers 
143596c9bb21SBrian Somers   memset(&msg, '\0', sizeof msg);
143696c9bb21SBrian Somers   msg.msg_name = (caddr_t)sun;
143796c9bb21SBrian Somers   msg.msg_namelen = sizeof *sun;
143896c9bb21SBrian Somers   msg.msg_iov = iov;
143996c9bb21SBrian Somers   msg.msg_iovlen = niov;
144096c9bb21SBrian Somers   msg.msg_control = cmsgbuf;
144196c9bb21SBrian Somers   msg.msg_controllen = sizeof cmsgbuf;
144296c9bb21SBrian Somers 
144396c9bb21SBrian Somers   log_Printf(LogDEBUG, "Expecting %d scatter/gather bytes\n", expect);
144496c9bb21SBrian Somers   f = expect + 100;
144596c9bb21SBrian Somers   setsockopt(s, SOL_SOCKET, SO_RCVBUF, &f, sizeof f);
144696c9bb21SBrian Somers   if ((f = recvmsg(s, &msg, MSG_WAITALL)) != expect) {
144796c9bb21SBrian Somers     if (f == -1)
144896c9bb21SBrian Somers       log_Printf(LogERROR, "Failed recvmsg: %s\n", strerror(errno));
144996c9bb21SBrian Somers     else
145096c9bb21SBrian Somers       log_Printf(LogERROR, "Failed recvmsg: Got %d, not %d\n", f, expect);
145196c9bb21SBrian Somers     while (niov--)
145296c9bb21SBrian Somers       free(iov[niov].iov_base);
145354cd8e13SBrian Somers     close(s);
145496c9bb21SBrian Somers     return;
145596c9bb21SBrian Somers   }
145696c9bb21SBrian Somers 
1457c0d9a877SBrian Somers   write(s, "!", 1);	/* ACK */
1458ad5b0e8bSBrian Somers   close(s);
1459c0d9a877SBrian Somers 
1460ad5b0e8bSBrian Somers   if (cmsg->cmsg_type != SCM_RIGHTS) {
1461ad5b0e8bSBrian Somers     log_Printf(LogERROR, "Recvmsg: no descriptor received !\n");
1462ad5b0e8bSBrian Somers     while (niov--)
1463ad5b0e8bSBrian Somers       free(iov[niov].iov_base);
1464ad5b0e8bSBrian Somers     return;
146554cd8e13SBrian Somers   }
146654cd8e13SBrian Somers 
1467ad5b0e8bSBrian Somers   /* We've successfully received an open file descriptor through our socket */
1468ad5b0e8bSBrian Somers   log_Printf(LogDEBUG, "Receiving device descriptor\n");
1469ad5b0e8bSBrian Somers   link_fd = *(int *)CMSG_DATA(cmsg);
1470ad5b0e8bSBrian Somers 
147196c9bb21SBrian Somers   if (strncmp(Version, iov[0].iov_base, iov[0].iov_len)) {
147296c9bb21SBrian Somers     log_Printf(LogWARN, "Cannot receive datalink, incorrect version"
147396c9bb21SBrian Somers                " (\"%.*s\", not \"%s\")\n", (int)iov[0].iov_len,
14747d81ddf5SBrian Somers                (char *)iov[0].iov_base, Version);
147596c9bb21SBrian Somers     close(link_fd);
147696c9bb21SBrian Somers     while (niov--)
147796c9bb21SBrian Somers       free(iov[niov].iov_base);
147896c9bb21SBrian Somers     return;
147996c9bb21SBrian Somers   }
148096c9bb21SBrian Somers 
148196c9bb21SBrian Somers   niov = 1;
1482b7c5748eSBrian Somers   dl = iov2datalink(bundle, iov, &niov, sizeof iov / sizeof *iov, link_fd);
1483b7c5748eSBrian Somers   if (dl) {
148404eaa58cSBrian Somers     bundle_DatalinkLinkin(bundle, dl);
1485b7c5748eSBrian Somers     datalink_AuthOk(dl);
148696c9bb21SBrian Somers   } else
148796c9bb21SBrian Somers     close(link_fd);
148896c9bb21SBrian Somers 
148996c9bb21SBrian Somers   free(iov[0].iov_base);
14901fa665f5SBrian Somers }
14911fa665f5SBrian Somers 
14921fa665f5SBrian Somers void
149396c9bb21SBrian Somers bundle_SendDatalink(struct datalink *dl, int s, struct sockaddr_un *sun)
14941fa665f5SBrian Somers {
1495c0d9a877SBrian Somers   char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int)], ack;
149696c9bb21SBrian Somers   struct cmsghdr *cmsg = (struct cmsghdr *)cmsgbuf;
149796c9bb21SBrian Somers   struct msghdr msg;
149896c9bb21SBrian Somers   struct iovec iov[SCATTER_SEGMENTS];
14991384bd27SBrian Somers   int niov, link_fd, f, expect, newsid;
150085fd273aSBrian Somers   pid_t newpid;
15016f384573SBrian Somers 
1502dd7e2610SBrian Somers   log_Printf(LogPHASE, "Transmitting datalink %s\n", dl->name);
15036f384573SBrian Somers 
150404eaa58cSBrian Somers   bundle_LinkClosed(dl->bundle, dl);
15050f2f3eb3SBrian Somers   bundle_DatalinkLinkout(dl->bundle, dl);
1506ea722969SBrian Somers 
150796c9bb21SBrian Somers   /* Build our scatter/gather array */
150896c9bb21SBrian Somers   iov[0].iov_len = strlen(Version) + 1;
150996c9bb21SBrian Somers   iov[0].iov_base = strdup(Version);
151096c9bb21SBrian Somers   niov = 1;
15116f384573SBrian Somers 
151285fd273aSBrian Somers   read(s, &newpid, sizeof newpid);
151385fd273aSBrian Somers   link_fd = datalink2iov(dl, iov, &niov, sizeof iov / sizeof *iov, newpid);
15146f384573SBrian Somers 
15156f384573SBrian Somers   if (link_fd != -1) {
151696c9bb21SBrian Somers     memset(&msg, '\0', sizeof msg);
151754cd8e13SBrian Somers 
151896c9bb21SBrian Somers     msg.msg_name = (caddr_t)sun;
151996c9bb21SBrian Somers     msg.msg_namelen = sizeof *sun;
152096c9bb21SBrian Somers     msg.msg_iov = iov;
152196c9bb21SBrian Somers     msg.msg_iovlen = niov;
152254cd8e13SBrian Somers 
152354cd8e13SBrian Somers     cmsg->cmsg_len = sizeof cmsgbuf;
152454cd8e13SBrian Somers     cmsg->cmsg_level = SOL_SOCKET;
152554cd8e13SBrian Somers     cmsg->cmsg_type = SCM_RIGHTS;
152654cd8e13SBrian Somers     *(int *)CMSG_DATA(cmsg) = link_fd;
152796c9bb21SBrian Somers     msg.msg_control = cmsgbuf;
152896c9bb21SBrian Somers     msg.msg_controllen = sizeof cmsgbuf;
152947723d29SBrian Somers 
153096c9bb21SBrian Somers     for (f = expect = 0; f < niov; f++)
153196c9bb21SBrian Somers       expect += iov[f].iov_len;
153247723d29SBrian Somers 
153396c9bb21SBrian Somers     log_Printf(LogDEBUG, "Sending %d bytes in scatter/gather array\n", expect);
153447723d29SBrian Somers 
153596c9bb21SBrian Somers     f = expect + SOCKET_OVERHEAD;
153696c9bb21SBrian Somers     setsockopt(s, SOL_SOCKET, SO_SNDBUF, &f, sizeof f);
153796c9bb21SBrian Somers     if (sendmsg(s, &msg, 0) == -1)
153896c9bb21SBrian Somers       log_Printf(LogERROR, "Failed sendmsg: %s\n", strerror(errno));
1539c0d9a877SBrian Somers     /* We must get the ACK before closing the descriptor ! */
1540c0d9a877SBrian Somers     read(s, &ack, 1);
154154cd8e13SBrian Somers 
1542ac685e31SBrian Somers     newsid = Enabled(dl->bundle, OPT_KEEPSESSION) ||
1543ac685e31SBrian Somers              tcgetpgrp(link_fd) == getpgrp();
154496c9bb21SBrian Somers     close(link_fd);
15451384bd27SBrian Somers     if (newsid)
15461384bd27SBrian Somers       bundle_setsid(dl->bundle, 1);
154747723d29SBrian Somers   }
154885fd273aSBrian Somers   close(s);
154996c9bb21SBrian Somers 
155096c9bb21SBrian Somers   while (niov--)
155196c9bb21SBrian Somers     free(iov[niov].iov_base);
15521fa665f5SBrian Somers }
1553dd0645c5SBrian Somers 
1554dd0645c5SBrian Somers int
155558d55334SBrian Somers bundle_RenameDatalink(struct bundle *bundle, struct datalink *ndl,
155658d55334SBrian Somers                       const char *name)
155758d55334SBrian Somers {
155858d55334SBrian Somers   struct datalink *dl;
155958d55334SBrian Somers 
156058d55334SBrian Somers   if (!strcasecmp(ndl->name, name))
156158d55334SBrian Somers     return 1;
156258d55334SBrian Somers 
156358d55334SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
156458d55334SBrian Somers     if (!strcasecmp(dl->name, name))
156558d55334SBrian Somers       return 0;
156658d55334SBrian Somers 
156758d55334SBrian Somers   datalink_Rename(ndl, name);
156858d55334SBrian Somers   return 1;
156958d55334SBrian Somers }
157058d55334SBrian Somers 
157158d55334SBrian Somers int
1572dd0645c5SBrian Somers bundle_SetMode(struct bundle *bundle, struct datalink *dl, int mode)
1573dd0645c5SBrian Somers {
1574dd0645c5SBrian Somers   int omode;
1575dd0645c5SBrian Somers 
1576dd0645c5SBrian Somers   omode = dl->physical->type;
1577dd0645c5SBrian Somers   if (omode == mode)
1578dd0645c5SBrian Somers     return 1;
1579dd0645c5SBrian Somers 
1580ff0f9439SBrian Somers   if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO))
1581ff0f9439SBrian Somers     /* First auto link */
1582dd0645c5SBrian Somers     if (bundle->ncp.ipcp.peer_ip.s_addr == INADDR_ANY) {
1583ff0f9439SBrian Somers       log_Printf(LogWARN, "You must `set ifaddr' or `open' before"
1584ff0f9439SBrian Somers                  " changing mode to %s\n", mode2Nam(mode));
1585dd0645c5SBrian Somers       return 0;
1586dd0645c5SBrian Somers     }
1587dd0645c5SBrian Somers 
1588dd0645c5SBrian Somers   if (!datalink_SetMode(dl, mode))
1589dd0645c5SBrian Somers     return 0;
1590dd0645c5SBrian Somers 
1591ff0f9439SBrian Somers   if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO) &&
1592ff0f9439SBrian Somers       bundle->phase != PHASE_NETWORK)
1593ff0f9439SBrian Somers     /* First auto link, we need an interface */
1594dd0645c5SBrian Somers     ipcp_InterfaceUp(&bundle->ncp.ipcp);
1595dd0645c5SBrian Somers 
159604eaa58cSBrian Somers   /* Regenerate phys_type and adjust autoload & idle timers */
159704eaa58cSBrian Somers   bundle_LinksRemoved(bundle);
1598dd0645c5SBrian Somers 
1599dd0645c5SBrian Somers   return 1;
1600dd0645c5SBrian Somers }
16011384bd27SBrian Somers 
16021384bd27SBrian Somers void
16031384bd27SBrian Somers bundle_setsid(struct bundle *bundle, int holdsession)
16041384bd27SBrian Somers {
16051384bd27SBrian Somers   /*
16061384bd27SBrian Somers    * Lose the current session.  This means getting rid of our pid
16071384bd27SBrian Somers    * too so that the tty device will really go away, and any getty
16081384bd27SBrian Somers    * etc will be allowed to restart.
16091384bd27SBrian Somers    */
16101384bd27SBrian Somers   pid_t pid, orig;
16111384bd27SBrian Somers   int fds[2];
16121384bd27SBrian Somers   char done;
16131384bd27SBrian Somers   struct datalink *dl;
16141384bd27SBrian Somers 
16151384bd27SBrian Somers   orig = getpid();
16161384bd27SBrian Somers   if (pipe(fds) == -1) {
16171384bd27SBrian Somers     log_Printf(LogERROR, "pipe: %s\n", strerror(errno));
16181384bd27SBrian Somers     return;
16191384bd27SBrian Somers   }
16201384bd27SBrian Somers   switch ((pid = fork())) {
16211384bd27SBrian Somers     case -1:
16221384bd27SBrian Somers       log_Printf(LogERROR, "fork: %s\n", strerror(errno));
16231384bd27SBrian Somers       close(fds[0]);
16241384bd27SBrian Somers       close(fds[1]);
16251384bd27SBrian Somers       return;
16261384bd27SBrian Somers     case 0:
16271384bd27SBrian Somers       close(fds[1]);
16284be0e57dSBrian Somers       read(fds[0], &done, 1);		/* uu_locks are mine ! */
16294be0e57dSBrian Somers       close(fds[0]);
16301384bd27SBrian Somers       if (pipe(fds) == -1) {
16311384bd27SBrian Somers         log_Printf(LogERROR, "pipe(2): %s\n", strerror(errno));
16321384bd27SBrian Somers         return;
16331384bd27SBrian Somers       }
16341384bd27SBrian Somers       switch ((pid = fork())) {
16351384bd27SBrian Somers         case -1:
1636a33b2ef7SBrian Somers           log_Printf(LogERROR, "fork(2): %s\n", strerror(errno));
16371384bd27SBrian Somers           close(fds[0]);
16381384bd27SBrian Somers           close(fds[1]);
16391384bd27SBrian Somers           return;
16401384bd27SBrian Somers         case 0:
16411384bd27SBrian Somers           close(fds[1]);
16424be0e57dSBrian Somers           bundle_LockTun(bundle);	/* update pid */
16434be0e57dSBrian Somers           read(fds[0], &done, 1);	/* uu_locks are mine ! */
16444be0e57dSBrian Somers           close(fds[0]);
16451384bd27SBrian Somers           setsid();
16461384bd27SBrian Somers           log_Printf(LogPHASE, "%d -> %d: %s session control\n",
16471384bd27SBrian Somers                      (int)orig, (int)getpid(),
16481384bd27SBrian Somers                      holdsession ? "Passed" : "Dropped");
16497e778f13SBrian Somers           timer_InitService(0);		/* Start the Timer Service */
16501384bd27SBrian Somers           break;
16511384bd27SBrian Somers         default:
16524be0e57dSBrian Somers           close(fds[0]);
16535d9e6103SBrian Somers           /* Give away all our physical locks (to the final process) */
16541384bd27SBrian Somers           for (dl = bundle->links; dl; dl = dl->next)
16551384bd27SBrian Somers             if (dl->state != DATALINK_CLOSED)
16565d9e6103SBrian Somers               physical_ChangedPid(dl->physical, pid);
16574be0e57dSBrian Somers           write(fds[1], "!", 1);	/* done */
16584be0e57dSBrian Somers           close(fds[1]);
16591384bd27SBrian Somers           exit(0);
16601384bd27SBrian Somers           break;
16611384bd27SBrian Somers       }
16621384bd27SBrian Somers       break;
16631384bd27SBrian Somers     default:
16644be0e57dSBrian Somers       close(fds[0]);
16655d9e6103SBrian Somers       /* Give away all our physical locks (to the intermediate process) */
16661384bd27SBrian Somers       for (dl = bundle->links; dl; dl = dl->next)
16671384bd27SBrian Somers         if (dl->state != DATALINK_CLOSED)
16685d9e6103SBrian Somers           physical_ChangedPid(dl->physical, pid);
16694be0e57dSBrian Somers       write(fds[1], "!", 1);	/* done */
16704be0e57dSBrian Somers       close(fds[1]);
16711384bd27SBrian Somers       if (holdsession) {
16721384bd27SBrian Somers         int fd, status;
16731384bd27SBrian Somers 
16741384bd27SBrian Somers         timer_TermService();
16751384bd27SBrian Somers         signal(SIGPIPE, SIG_DFL);
16761384bd27SBrian Somers         signal(SIGALRM, SIG_DFL);
16771384bd27SBrian Somers         signal(SIGHUP, SIG_DFL);
16781384bd27SBrian Somers         signal(SIGTERM, SIG_DFL);
16791384bd27SBrian Somers         signal(SIGINT, SIG_DFL);
16801384bd27SBrian Somers         signal(SIGQUIT, SIG_DFL);
16811384bd27SBrian Somers         for (fd = getdtablesize(); fd >= 0; fd--)
16821384bd27SBrian Somers           close(fd);
16831384bd27SBrian Somers         setuid(geteuid());
16841384bd27SBrian Somers         /*
16851384bd27SBrian Somers          * Reap the intermediate process.  As we're not exiting but the
16861384bd27SBrian Somers          * intermediate is, we don't want it to become defunct.
16871384bd27SBrian Somers          */
16881384bd27SBrian Somers         waitpid(pid, &status, 0);
16898e7b8599SBrian Somers         /* Tweak our process arguments.... */
16908e7b8599SBrian Somers         bundle->argv[0] = "session owner";
16918e7b8599SBrian Somers         bundle->argv[1] = NULL;
16921384bd27SBrian Somers         /*
16931384bd27SBrian Somers          * Hang around for a HUP.  This should happen as soon as the
16941384bd27SBrian Somers          * ppp that we passed our ctty descriptor to closes it.
16951384bd27SBrian Somers          * NOTE: If this process dies, the passed descriptor becomes
16961384bd27SBrian Somers          *       invalid and will give a select() error by setting one
16971384bd27SBrian Somers          *       of the error fds, aborting the other ppp.  We don't
16981384bd27SBrian Somers          *       want that to happen !
16991384bd27SBrian Somers          */
17001384bd27SBrian Somers         pause();
17011384bd27SBrian Somers       }
17021384bd27SBrian Somers       exit(0);
17031384bd27SBrian Somers       break;
17041384bd27SBrian Somers   }
17051384bd27SBrian Somers }
17069b5f8ffdSBrian Somers 
17079b5f8ffdSBrian Somers int
17089b5f8ffdSBrian Somers bundle_HighestState(struct bundle *bundle)
17099b5f8ffdSBrian Somers {
17109b5f8ffdSBrian Somers   struct datalink *dl;
17119b5f8ffdSBrian Somers   int result = DATALINK_CLOSED;
17129b5f8ffdSBrian Somers 
17139b5f8ffdSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
17149b5f8ffdSBrian Somers     if (result < dl->state)
17159b5f8ffdSBrian Somers       result = dl->state;
17169b5f8ffdSBrian Somers 
17179b5f8ffdSBrian Somers   return result;
17189b5f8ffdSBrian Somers }
1719991c2a7bSBrian Somers 
1720991c2a7bSBrian Somers int
1721991c2a7bSBrian Somers bundle_Exception(struct bundle *bundle, int fd)
1722991c2a7bSBrian Somers {
1723991c2a7bSBrian Somers   struct datalink *dl;
1724991c2a7bSBrian Somers 
1725991c2a7bSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
1726991c2a7bSBrian Somers     if (dl->physical->fd == fd) {
1727991c2a7bSBrian Somers       datalink_Down(dl, CLOSE_NORMAL);
1728991c2a7bSBrian Somers       return 1;
1729991c2a7bSBrian Somers     }
1730991c2a7bSBrian Somers 
1731991c2a7bSBrian Somers   return 0;
1732991c2a7bSBrian Somers }
17331d1fc017SBrian Somers 
17341d1fc017SBrian Somers void
17351d1fc017SBrian Somers bundle_AdjustFilters(struct bundle *bundle, struct in_addr *my_ip,
17361d1fc017SBrian Somers                      struct in_addr *peer_ip)
17371d1fc017SBrian Somers {
17381d1fc017SBrian Somers   filter_AdjustAddr(&bundle->filter.in, my_ip, peer_ip);
17391d1fc017SBrian Somers   filter_AdjustAddr(&bundle->filter.out, my_ip, peer_ip);
17401d1fc017SBrian Somers   filter_AdjustAddr(&bundle->filter.dial, my_ip, peer_ip);
17411d1fc017SBrian Somers   filter_AdjustAddr(&bundle->filter.alive, my_ip, peer_ip);
17421d1fc017SBrian Somers }
1743