xref: /freebsd/usr.sbin/ppp/bundle.c (revision 0f203c7e2eb881d67b856951c036793a2c477eaa)
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  *
260f203c7eSBrian Somers  *	$Id: bundle.c,v 1.49 1999/03/07 01:41:40 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 
521595bacdSBrian Somers #ifndef NOALIAS
531595bacdSBrian Somers #ifdef __OpenBSD__
541595bacdSBrian Somers #include "alias.h"
551595bacdSBrian Somers #else
561595bacdSBrian Somers #include <alias.h>
571595bacdSBrian Somers #endif
581595bacdSBrian Somers #endif
59c9e11a11SBrian Somers #include "defs.h"
607a6f8720SBrian Somers #include "command.h"
617a6f8720SBrian Somers #include "mbuf.h"
627a6f8720SBrian Somers #include "log.h"
637a6f8720SBrian Somers #include "id.h"
647a6f8720SBrian Somers #include "timer.h"
657a6f8720SBrian Somers #include "fsm.h"
667a6f8720SBrian Somers #include "iplist.h"
67879ed6faSBrian Somers #include "lqr.h"
68455aabc3SBrian Somers #include "hdlc.h"
697a6f8720SBrian Somers #include "throughput.h"
70eaa4df37SBrian Somers #include "slcompress.h"
717a6f8720SBrian Somers #include "ipcp.h"
725ca5389aSBrian Somers #include "filter.h"
732f786681SBrian Somers #include "descriptor.h"
747a6f8720SBrian Somers #include "route.h"
757a6f8720SBrian Somers #include "lcp.h"
767a6f8720SBrian Somers #include "ccp.h"
773b0f8d2eSBrian Somers #include "link.h"
783b0f8d2eSBrian Somers #include "mp.h"
79972a1bcfSBrian Somers #ifndef NORADIUS
80972a1bcfSBrian Somers #include "radius.h"
81972a1bcfSBrian Somers #endif
823b0f8d2eSBrian Somers #include "bundle.h"
83455aabc3SBrian Somers #include "async.h"
84455aabc3SBrian Somers #include "physical.h"
852289f246SBrian Somers #include "modem.h"
86455aabc3SBrian Somers #include "auth.h"
87455aabc3SBrian Somers #include "lcpproto.h"
88455aabc3SBrian Somers #include "chap.h"
89455aabc3SBrian Somers #include "tun.h"
9085b542cfSBrian Somers #include "prompt.h"
913006ec67SBrian Somers #include "chat.h"
9292b09558SBrian Somers #include "cbcp.h"
933006ec67SBrian Somers #include "datalink.h"
943006ec67SBrian Somers #include "ip.h"
958fa6ebe4SBrian Somers #include "iface.h"
967a6f8720SBrian Somers 
9796c9bb21SBrian Somers #define SCATTER_SEGMENTS 4	/* version, datalink, name, physical */
9896c9bb21SBrian Somers #define SOCKET_OVERHEAD	100	/* additional buffer space for large */
9996c9bb21SBrian Somers                                 /* {recv,send}msg() calls            */
10096c9bb21SBrian Somers 
10104eaa58cSBrian Somers static int bundle_RemainingIdleTime(struct bundle *);
10204eaa58cSBrian Somers static int bundle_RemainingAutoLoadTime(struct bundle *);
10304eaa58cSBrian Somers 
104455aabc3SBrian Somers static const char *PhaseNames[] = {
105455aabc3SBrian Somers   "Dead", "Establish", "Authenticate", "Network", "Terminate"
106455aabc3SBrian Somers };
107455aabc3SBrian Somers 
108455aabc3SBrian Somers const char *
109455aabc3SBrian Somers bundle_PhaseName(struct bundle *bundle)
1107a6f8720SBrian Somers {
111455aabc3SBrian Somers   return bundle->phase <= PHASE_TERMINATE ?
112455aabc3SBrian Somers     PhaseNames[bundle->phase] : "unknown";
1137a6f8720SBrian Somers }
1147a6f8720SBrian Somers 
115455aabc3SBrian Somers void
1165563ebdeSBrian Somers bundle_NewPhase(struct bundle *bundle, u_int new)
117455aabc3SBrian Somers {
118aef795ccSBrian Somers   if (new == bundle->phase)
119aef795ccSBrian Somers     return;
120aef795ccSBrian Somers 
121e2ebb036SBrian Somers   if (new <= PHASE_TERMINATE)
122dd7e2610SBrian Somers     log_Printf(LogPHASE, "bundle: %s\n", PhaseNames[new]);
1237a6f8720SBrian Somers 
124455aabc3SBrian Somers   switch (new) {
125455aabc3SBrian Somers   case PHASE_DEAD:
1260f2f3eb3SBrian Somers     log_DisplayPrompts();
127455aabc3SBrian Somers     bundle->phase = new;
128455aabc3SBrian Somers     break;
129455aabc3SBrian Somers 
130455aabc3SBrian Somers   case PHASE_ESTABLISH:
131455aabc3SBrian Somers     bundle->phase = new;
132455aabc3SBrian Somers     break;
133455aabc3SBrian Somers 
134455aabc3SBrian Somers   case PHASE_AUTHENTICATE:
135455aabc3SBrian Somers     bundle->phase = new;
1360f2f3eb3SBrian Somers     log_DisplayPrompts();
137455aabc3SBrian Somers     break;
138455aabc3SBrian Somers 
139455aabc3SBrian Somers   case PHASE_NETWORK:
140dd7e2610SBrian Somers     fsm_Up(&bundle->ncp.ipcp.fsm);
141dd7e2610SBrian Somers     fsm_Open(&bundle->ncp.ipcp.fsm);
142673903ecSBrian Somers     bundle->phase = new;
1430f2f3eb3SBrian Somers     log_DisplayPrompts();
144673903ecSBrian Somers     break;
145455aabc3SBrian Somers 
146455aabc3SBrian Somers   case PHASE_TERMINATE:
147455aabc3SBrian Somers     bundle->phase = new;
148673903ecSBrian Somers     mp_Down(&bundle->ncp.mp);
1490f2f3eb3SBrian Somers     log_DisplayPrompts();
150455aabc3SBrian Somers     break;
1517a6f8720SBrian Somers   }
1527a6f8720SBrian Somers }
1537a6f8720SBrian Somers 
1546d666775SBrian Somers static void
1556d666775SBrian Somers bundle_LayerStart(void *v, struct fsm *fp)
1567a6f8720SBrian Somers {
1573006ec67SBrian Somers   /* The given FSM is about to start up ! */
1587a6f8720SBrian Somers }
1597a6f8720SBrian Somers 
1605cf4388bSBrian Somers 
1615cf4388bSBrian Somers static void
1625cf4388bSBrian Somers bundle_Notify(struct bundle *bundle, char c)
1635cf4388bSBrian Somers {
1645cf4388bSBrian Somers   if (bundle->notify.fd != -1) {
1655cf4388bSBrian Somers     if (write(bundle->notify.fd, &c, 1) == 1)
166dd7e2610SBrian Somers       log_Printf(LogPHASE, "Parent notified of success.\n");
1675cf4388bSBrian Somers     else
168dd7e2610SBrian Somers       log_Printf(LogPHASE, "Failed to notify parent of success.\n");
1695cf4388bSBrian Somers     close(bundle->notify.fd);
1705cf4388bSBrian Somers     bundle->notify.fd = -1;
1715cf4388bSBrian Somers   }
1725cf4388bSBrian Somers }
1733b0f8d2eSBrian Somers 
1746d666775SBrian Somers static void
1756f8e9f0aSBrian Somers bundle_ClearQueues(void *v)
1766f8e9f0aSBrian Somers {
1776f8e9f0aSBrian Somers   struct bundle *bundle = (struct bundle *)v;
1786f8e9f0aSBrian Somers   struct datalink *dl;
1796f8e9f0aSBrian Somers 
1806f8e9f0aSBrian Somers   log_Printf(LogPHASE, "Clearing choked output queue\n");
1816f8e9f0aSBrian Somers   timer_Stop(&bundle->choked.timer);
1826f8e9f0aSBrian Somers 
1836f8e9f0aSBrian Somers   /*
1846f8e9f0aSBrian Somers    * Emergency time:
1856f8e9f0aSBrian Somers    *
1866f8e9f0aSBrian Somers    * We've had a full queue for PACKET_DEL_SECS seconds without being
1876f8e9f0aSBrian Somers    * able to get rid of any of the packets.  We've probably given up
1886f8e9f0aSBrian Somers    * on the redials at this point, and the queued data has almost
1896f8e9f0aSBrian Somers    * definitely been timed out by the layer above.  As this is preventing
1906f8e9f0aSBrian Somers    * us from reading the TUN_NAME device (we don't want to buffer stuff
1916f8e9f0aSBrian Somers    * indefinitely), we may as well nuke this data and start with a clean
1926f8e9f0aSBrian Somers    * slate !
1936f8e9f0aSBrian Somers    *
1946f8e9f0aSBrian Somers    * Unfortunately, this has the side effect of shafting any compression
1956f8e9f0aSBrian Somers    * dictionaries in use (causing the relevant RESET_REQ/RESET_ACK).
1966f8e9f0aSBrian Somers    */
1976f8e9f0aSBrian Somers 
1985a72b6edSBrian Somers   ip_DeleteQueue(&bundle->ncp.ipcp);
1996f8e9f0aSBrian Somers   mp_DeleteQueue(&bundle->ncp.mp);
2006f8e9f0aSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
2016f8e9f0aSBrian Somers     physical_DeleteQueue(dl->physical);
2026f8e9f0aSBrian Somers }
2036f8e9f0aSBrian Somers 
2046f8e9f0aSBrian Somers static void
20504eaa58cSBrian Somers bundle_AutoLoadTimeout(void *v)
20604eaa58cSBrian Somers {
20704eaa58cSBrian Somers   struct bundle *bundle = (struct bundle *)v;
20804eaa58cSBrian Somers 
20904eaa58cSBrian Somers   if (bundle->autoload.comingup) {
21004eaa58cSBrian Somers     log_Printf(LogPHASE, "autoload: Another link is required\n");
21104eaa58cSBrian Somers     /* bundle_Open() stops the timer */
212ba23f397SBrian Somers     bundle_Open(bundle, NULL, PHYS_AUTO, 0);
21304eaa58cSBrian Somers   } else {
21404eaa58cSBrian Somers     struct datalink *dl, *last;
21504eaa58cSBrian Somers 
21604eaa58cSBrian Somers     timer_Stop(&bundle->autoload.timer);
21704eaa58cSBrian Somers     for (last = NULL, dl = bundle->links; dl; dl = dl->next)
21881358fa3SBrian Somers       if (dl->physical->type == PHYS_AUTO && dl->state == DATALINK_OPEN)
21904eaa58cSBrian Somers         last = dl;
22004eaa58cSBrian Somers 
22104eaa58cSBrian Somers     if (last)
2229c81b87dSBrian Somers       datalink_Close(last, CLOSE_STAYDOWN);
22304eaa58cSBrian Somers   }
22404eaa58cSBrian Somers }
22504eaa58cSBrian Somers 
22604eaa58cSBrian Somers static void
22704eaa58cSBrian Somers bundle_StartAutoLoadTimer(struct bundle *bundle, int up)
22804eaa58cSBrian Somers {
22904eaa58cSBrian Somers   struct datalink *dl;
23004eaa58cSBrian Somers 
23104eaa58cSBrian Somers   timer_Stop(&bundle->autoload.timer);
232ec5e7f36SBrian Somers   bundle->autoload.comingup = up ? 1 : 0;
23304eaa58cSBrian Somers 
23404eaa58cSBrian Somers   if (bundle->CleaningUp || bundle->phase != PHASE_NETWORK) {
23504eaa58cSBrian Somers     dl = NULL;
23604eaa58cSBrian Somers     bundle->autoload.running = 0;
23704eaa58cSBrian Somers   } else if (up) {
23804eaa58cSBrian Somers     for (dl = bundle->links; dl; dl = dl->next)
23981358fa3SBrian Somers       if (dl->state == DATALINK_CLOSED && dl->physical->type == PHYS_AUTO) {
24004eaa58cSBrian Somers         if (bundle->cfg.autoload.max.timeout) {
24104eaa58cSBrian Somers           bundle->autoload.timer.func = bundle_AutoLoadTimeout;
24204eaa58cSBrian Somers           bundle->autoload.timer.name = "autoload up";
24304eaa58cSBrian Somers           bundle->autoload.timer.load =
24404eaa58cSBrian Somers             bundle->cfg.autoload.max.timeout * SECTICKS;
24504eaa58cSBrian Somers           bundle->autoload.timer.arg = bundle;
24604eaa58cSBrian Somers           timer_Start(&bundle->autoload.timer);
24704eaa58cSBrian Somers           bundle->autoload.done = time(NULL) + bundle->cfg.autoload.max.timeout;
24804eaa58cSBrian Somers         } else
24904eaa58cSBrian Somers           bundle_AutoLoadTimeout(bundle);
25004eaa58cSBrian Somers         break;
25104eaa58cSBrian Somers       }
25204eaa58cSBrian Somers     bundle->autoload.running = (dl || bundle->cfg.autoload.min.timeout) ? 1 : 0;
25304eaa58cSBrian Somers   } else {
25404eaa58cSBrian Somers     int nlinks;
25504eaa58cSBrian Somers     struct datalink *adl;
25604eaa58cSBrian Somers 
25704eaa58cSBrian Somers     for (nlinks = 0, adl = NULL, dl = bundle->links; dl; dl = dl->next)
25804eaa58cSBrian Somers       if (dl->state == DATALINK_OPEN) {
25981358fa3SBrian Somers         if (dl->physical->type == PHYS_AUTO)
26004eaa58cSBrian Somers           adl = dl;
26104eaa58cSBrian Somers         if (++nlinks > 1 && adl) {
26204eaa58cSBrian Somers           if (bundle->cfg.autoload.min.timeout) {
26304eaa58cSBrian Somers             bundle->autoload.timer.func = bundle_AutoLoadTimeout;
26404eaa58cSBrian Somers             bundle->autoload.timer.name = "autoload down";
26504eaa58cSBrian Somers             bundle->autoload.timer.load =
26604eaa58cSBrian Somers               bundle->cfg.autoload.min.timeout * SECTICKS;
26704eaa58cSBrian Somers             bundle->autoload.timer.arg = bundle;
26804eaa58cSBrian Somers             timer_Start(&bundle->autoload.timer);
26904eaa58cSBrian Somers             bundle->autoload.done =
27004eaa58cSBrian Somers               time(NULL) + bundle->cfg.autoload.min.timeout;
27104eaa58cSBrian Somers           }
27204eaa58cSBrian Somers           break;
27304eaa58cSBrian Somers         }
27404eaa58cSBrian Somers       }
27504eaa58cSBrian Somers 
27604eaa58cSBrian Somers     bundle->autoload.running = 1;
27704eaa58cSBrian Somers   }
27804eaa58cSBrian Somers }
27904eaa58cSBrian Somers 
28004eaa58cSBrian Somers static void
28104eaa58cSBrian Somers bundle_StopAutoLoadTimer(struct bundle *bundle)
28204eaa58cSBrian Somers {
28304eaa58cSBrian Somers   timer_Stop(&bundle->autoload.timer);
28404eaa58cSBrian Somers   bundle->autoload.done = 0;
28504eaa58cSBrian Somers }
28604eaa58cSBrian Somers 
28704eaa58cSBrian Somers static int
28804eaa58cSBrian Somers bundle_RemainingAutoLoadTime(struct bundle *bundle)
28904eaa58cSBrian Somers {
29004eaa58cSBrian Somers   if (bundle->autoload.done)
29104eaa58cSBrian Somers     return bundle->autoload.done - time(NULL);
29204eaa58cSBrian Somers   return -1;
29304eaa58cSBrian Somers }
29404eaa58cSBrian Somers 
295ff0f9439SBrian Somers static void
296ff0f9439SBrian Somers bundle_LinkAdded(struct bundle *bundle, struct datalink *dl)
297ff0f9439SBrian Somers {
298ff0f9439SBrian Somers   bundle->phys_type.all |= dl->physical->type;
299ff0f9439SBrian Somers   if (dl->state == DATALINK_OPEN)
300ff0f9439SBrian Somers     bundle->phys_type.open |= dl->physical->type;
301ff0f9439SBrian Somers 
302ff0f9439SBrian Somers   /* Note: We only re-add links that are DATALINK_OPEN */
303ff0f9439SBrian Somers   if (dl->physical->type == PHYS_AUTO &&
304ff0f9439SBrian Somers       bundle->autoload.timer.state == TIMER_STOPPED &&
305ff0f9439SBrian Somers       dl->state != DATALINK_OPEN &&
306ff0f9439SBrian Somers       bundle->phase == PHASE_NETWORK)
307ff0f9439SBrian Somers     bundle->autoload.running = 1;
308ff0f9439SBrian Somers 
309ff0f9439SBrian Somers   if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL))
310ff0f9439SBrian Somers       != bundle->phys_type.open && bundle->idle.timer.state == TIMER_STOPPED)
311ff0f9439SBrian Somers     /* We may need to start our idle timer */
312ff0f9439SBrian Somers     bundle_StartIdleTimer(bundle);
313ff0f9439SBrian Somers }
314ff0f9439SBrian Somers 
31592b09558SBrian Somers void
316ff0f9439SBrian Somers bundle_LinksRemoved(struct bundle *bundle)
317ff0f9439SBrian Somers {
318ff0f9439SBrian Somers   struct datalink *dl;
319ff0f9439SBrian Somers 
320ff0f9439SBrian Somers   bundle->phys_type.all = bundle->phys_type.open = 0;
321ff0f9439SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
322ff0f9439SBrian Somers     bundle_LinkAdded(bundle, dl);
323ff0f9439SBrian Somers 
324ff0f9439SBrian Somers   if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL))
325ff0f9439SBrian Somers       == bundle->phys_type.open)
326ff0f9439SBrian Somers     bundle_StopIdleTimer(bundle);
327ff0f9439SBrian Somers }
32804eaa58cSBrian Somers 
32904eaa58cSBrian Somers static void
3306f384573SBrian Somers bundle_LayerUp(void *v, struct fsm *fp)
3317a6f8720SBrian Somers {
3323006ec67SBrian Somers   /*
3333006ec67SBrian Somers    * The given fsm is now up
334ff0f9439SBrian Somers    * If it's an LCP, adjust our phys_mode.open value.
33549052c95SBrian Somers    * If it's an LCP set our mtu (if we're multilink, add up the link
33604eaa58cSBrian Somers    * speeds and set the MRRU) and start our autoload timer.
337565e35e5SBrian Somers    * If it's an NCP, tell our -background parent to go away.
3383b0f8d2eSBrian Somers    * If it's the first NCP, start the idle timer.
3393006ec67SBrian Somers    */
3406f384573SBrian Somers   struct bundle *bundle = (struct bundle *)v;
3416d666775SBrian Somers 
3425563ebdeSBrian Somers   if (fp->proto == PROTO_LCP) {
343ff0f9439SBrian Somers     struct physical *p = link2physical(fp->link);
344ff0f9439SBrian Somers 
345ff0f9439SBrian Somers     bundle_LinkAdded(bundle, p->dl);
3463b0f8d2eSBrian Somers     if (bundle->ncp.mp.active) {
3473b0f8d2eSBrian Somers       struct datalink *dl;
3485563ebdeSBrian Somers 
3498fa6ebe4SBrian Somers       bundle->ifSpeed = 0;
350faefde08SBrian Somers       for (dl = bundle->links; dl; dl = dl->next)
351eeab6bf5SBrian Somers         if (dl->state == DATALINK_OPEN)
3528fa6ebe4SBrian Somers           bundle->ifSpeed += modem_Speed(dl->physical);
353faefde08SBrian Somers       tun_configure(bundle, bundle->ncp.mp.peer_mrru);
35404eaa58cSBrian Somers       bundle->autoload.running = 1;
355faefde08SBrian Somers     } else {
3568fa6ebe4SBrian Somers       bundle->ifSpeed = modem_Speed(p);
357faefde08SBrian Somers       tun_configure(bundle, fsm2lcp(fp)->his_mru);
358faefde08SBrian Somers     }
3593b0f8d2eSBrian Somers   } else if (fp->proto == PROTO_IPCP) {
360ab886ad0SBrian Somers     bundle_StartIdleTimer(bundle);
3615cf4388bSBrian Somers     bundle_Notify(bundle, EX_NORMAL);
3627a6f8720SBrian Somers   }
363ab886ad0SBrian Somers }
3647a6f8720SBrian Somers 
3656d666775SBrian Somers static void
3666d666775SBrian Somers bundle_LayerDown(void *v, struct fsm *fp)
3676d666775SBrian Somers {
3686d666775SBrian Somers   /*
3696d666775SBrian Somers    * The given FSM has been told to come down.
370ab886ad0SBrian Somers    * If it's our last NCP, stop the idle timer.
371ff0f9439SBrian Somers    * If it's an LCP, adjust our phys_type.open value and any timers.
37286b1f0d7SBrian Somers    * If it's an LCP and we're in multilink mode, adjust our tun
37386b1f0d7SBrian Somers    * speed and make sure our minimum sequence number is adjusted.
3746d666775SBrian Somers    */
375ab886ad0SBrian Somers 
376ab886ad0SBrian Somers   struct bundle *bundle = (struct bundle *)v;
377ab886ad0SBrian Somers 
378078c562eSBrian Somers   if (fp->proto == PROTO_IPCP)
379ab886ad0SBrian Somers     bundle_StopIdleTimer(bundle);
380ff0f9439SBrian Somers   else if (fp->proto == PROTO_LCP) {
381ff0f9439SBrian Somers     bundle_LinksRemoved(bundle);  /* adjust timers & phys_type values */
382ff0f9439SBrian Somers     if (bundle->ncp.mp.active) {
3833b0f8d2eSBrian Somers       struct datalink *dl;
38486b1f0d7SBrian Somers       struct datalink *lost;
3853b0f8d2eSBrian Somers 
3868fa6ebe4SBrian Somers       bundle->ifSpeed = 0;
38786b1f0d7SBrian Somers       lost = NULL;
388faefde08SBrian Somers       for (dl = bundle->links; dl; dl = dl->next)
38986b1f0d7SBrian Somers         if (fp == &dl->physical->link.lcp.fsm)
39086b1f0d7SBrian Somers           lost = dl;
39186b1f0d7SBrian Somers         else if (dl->state == DATALINK_OPEN)
3928fa6ebe4SBrian Somers           bundle->ifSpeed += modem_Speed(dl->physical);
39386b1f0d7SBrian Somers 
3948fa6ebe4SBrian Somers       if (bundle->ifSpeed)
395faefde08SBrian Somers         /* Don't configure down to a speed of 0 */
396faefde08SBrian Somers         tun_configure(bundle, bundle->ncp.mp.link.lcp.his_mru);
39786b1f0d7SBrian Somers 
39886b1f0d7SBrian Somers       if (lost)
39986b1f0d7SBrian Somers         mp_LinkLost(&bundle->ncp.mp, lost);
40086b1f0d7SBrian Somers       else
401a33b2ef7SBrian Somers         log_Printf(LogALERT, "Oops, lost an unrecognised datalink (%s) !\n",
40286b1f0d7SBrian Somers                    fp->link->name);
4033b0f8d2eSBrian Somers     }
4046d666775SBrian Somers   }
405ff0f9439SBrian Somers }
4066d666775SBrian Somers 
4076d666775SBrian Somers static void
4086d666775SBrian Somers bundle_LayerFinish(void *v, struct fsm *fp)
4096d666775SBrian Somers {
4106d666775SBrian Somers   /* The given fsm is now down (fp cannot be NULL)
4116d666775SBrian Somers    *
412dd7e2610SBrian Somers    * If it's the last LCP, fsm_Down all NCPs
413dd7e2610SBrian Somers    * If it's the last NCP, fsm_Close all LCPs
4146d666775SBrian Somers    */
4156d666775SBrian Somers 
4166d666775SBrian Somers   struct bundle *bundle = (struct bundle *)v;
4176d666775SBrian Somers   struct datalink *dl;
4186d666775SBrian Somers 
4193b0f8d2eSBrian Somers   if (fp->proto == PROTO_IPCP) {
42026afeaa2SBrian Somers     if (bundle_Phase(bundle) != PHASE_DEAD)
42125092092SBrian Somers       bundle_NewPhase(bundle, PHASE_TERMINATE);
4226d666775SBrian Somers     for (dl = bundle->links; dl; dl = dl->next)
4239c81b87dSBrian Somers       datalink_Close(dl, CLOSE_NORMAL);
42409206a6fSBrian Somers     fsm2initial(fp);
4253b0f8d2eSBrian Somers   } else if (fp->proto == PROTO_LCP) {
4263b0f8d2eSBrian Somers     int others_active;
427a611cad6SBrian Somers 
4283b0f8d2eSBrian Somers     others_active = 0;
4293b0f8d2eSBrian Somers     for (dl = bundle->links; dl; dl = dl->next)
4303b0f8d2eSBrian Somers       if (fp != &dl->physical->link.lcp.fsm &&
4313b0f8d2eSBrian Somers           dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP)
4323b0f8d2eSBrian Somers         others_active++;
4333b0f8d2eSBrian Somers 
43409206a6fSBrian Somers     if (!others_active)
43509206a6fSBrian Somers       fsm2initial(&bundle->ncp.ipcp.fsm);
4363b0f8d2eSBrian Somers   }
4373b0f8d2eSBrian Somers }
4386d666775SBrian Somers 
4397a6f8720SBrian Somers int
4407a6f8720SBrian Somers bundle_LinkIsUp(const struct bundle *bundle)
4417a6f8720SBrian Somers {
4425828db6dSBrian Somers   return bundle->ncp.ipcp.fsm.state == ST_OPENED;
4437a6f8720SBrian Somers }
4447a6f8720SBrian Somers 
4457a6f8720SBrian Somers void
4469c81b87dSBrian Somers bundle_Close(struct bundle *bundle, const char *name, int how)
4477a6f8720SBrian Somers {
448455aabc3SBrian Somers   /*
4493006ec67SBrian Somers    * Please close the given datalink.
450dd7e2610SBrian Somers    * If name == NULL or name is the last datalink, fsm_Close all NCPs
4516f384573SBrian Somers    * (except our MP)
4523b0f8d2eSBrian Somers    * If it isn't the last datalink, just Close that datalink.
453455aabc3SBrian Somers    */
4547a6f8720SBrian Somers 
4553b0f8d2eSBrian Somers   struct datalink *dl, *this_dl;
4563b0f8d2eSBrian Somers   int others_active;
4573006ec67SBrian Somers 
4583b0f8d2eSBrian Somers   others_active = 0;
4593b0f8d2eSBrian Somers   this_dl = NULL;
4603b0f8d2eSBrian Somers 
4613b0f8d2eSBrian Somers   for (dl = bundle->links; dl; dl = dl->next) {
4623b0f8d2eSBrian Somers     if (name && !strcasecmp(name, dl->name))
4633b0f8d2eSBrian Somers       this_dl = dl;
4643b0f8d2eSBrian Somers     if (name == NULL || this_dl == dl) {
4659c81b87dSBrian Somers       switch (how) {
4669c81b87dSBrian Somers         case CLOSE_LCP:
4679c81b87dSBrian Somers           datalink_DontHangup(dl);
4689c81b87dSBrian Somers           /* fall through */
4699c81b87dSBrian Somers         case CLOSE_STAYDOWN:
4703006ec67SBrian Somers           datalink_StayDown(dl);
4719c81b87dSBrian Somers           break;
4729c81b87dSBrian Somers       }
4733b0f8d2eSBrian Somers     } else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP)
4743b0f8d2eSBrian Somers       others_active++;
4753b0f8d2eSBrian Somers   }
4763b0f8d2eSBrian Somers 
4773b0f8d2eSBrian Somers   if (name && this_dl == NULL) {
478dd7e2610SBrian Somers     log_Printf(LogWARN, "%s: Invalid datalink name\n", name);
4793b0f8d2eSBrian Somers     return;
4803b0f8d2eSBrian Somers   }
4813b0f8d2eSBrian Somers 
4823b0f8d2eSBrian Somers   if (!others_active) {
48304eaa58cSBrian Somers     bundle_StopIdleTimer(bundle);
48404eaa58cSBrian Somers     bundle_StopAutoLoadTimer(bundle);
4853b0f8d2eSBrian Somers     if (bundle->ncp.ipcp.fsm.state > ST_CLOSED ||
4863b0f8d2eSBrian Somers         bundle->ncp.ipcp.fsm.state == ST_STARTING)
487dd7e2610SBrian Somers       fsm_Close(&bundle->ncp.ipcp.fsm);
4883b0f8d2eSBrian Somers     else {
48909206a6fSBrian Somers       fsm2initial(&bundle->ncp.ipcp.fsm);
490d345321bSBrian Somers       for (dl = bundle->links; dl; dl = dl->next)
4919c81b87dSBrian Somers         datalink_Close(dl, how);
4927a6f8720SBrian Somers     }
4933b0f8d2eSBrian Somers   } else if (this_dl && this_dl->state != DATALINK_CLOSED &&
4943b0f8d2eSBrian Somers              this_dl->state != DATALINK_HANGUP)
4959c81b87dSBrian Somers     datalink_Close(this_dl, how);
496d2fd8d77SBrian Somers }
4977a6f8720SBrian Somers 
4981bc9b5baSBrian Somers void
499899011c4SBrian Somers bundle_Down(struct bundle *bundle, int how)
5001bc9b5baSBrian Somers {
5011bc9b5baSBrian Somers   struct datalink *dl;
5021bc9b5baSBrian Somers 
5031bc9b5baSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
504899011c4SBrian Somers     datalink_Down(dl, how);
5051bc9b5baSBrian Somers }
5061bc9b5baSBrian Somers 
5072f786681SBrian Somers static int
5082f786681SBrian Somers bundle_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
5092f786681SBrian Somers {
5102f786681SBrian Somers   struct bundle *bundle = descriptor2bundle(d);
5112f786681SBrian Somers   struct datalink *dl;
51204eaa58cSBrian Somers   int result, want, queued, nlinks;
5132f786681SBrian Somers 
5142f786681SBrian Somers   result = 0;
5152f786681SBrian Somers 
516078c562eSBrian Somers   /* If there are aren't many packets queued, look for some more. */
51704eaa58cSBrian Somers   for (nlinks = 0, dl = bundle->links; dl; dl = dl->next)
51804eaa58cSBrian Somers     nlinks++;
51904eaa58cSBrian Somers 
52004eaa58cSBrian Somers   if (nlinks) {
5215a72b6edSBrian Somers     queued = r ? bundle_FillQueues(bundle) : ip_QueueLen(&bundle->ncp.ipcp);
52204eaa58cSBrian Somers     if (bundle->autoload.running) {
52304eaa58cSBrian Somers       if (queued < bundle->cfg.autoload.max.packets) {
52404eaa58cSBrian Somers         if (queued > bundle->cfg.autoload.min.packets)
52504eaa58cSBrian Somers           bundle_StopAutoLoadTimer(bundle);
52604eaa58cSBrian Somers         else if (bundle->autoload.timer.state != TIMER_RUNNING ||
52704eaa58cSBrian Somers                  bundle->autoload.comingup)
52804eaa58cSBrian Somers           bundle_StartAutoLoadTimer(bundle, 0);
529ec5e7f36SBrian Somers       } else if ((bundle_Phase(bundle) == PHASE_NETWORK || queued) &&
530ec5e7f36SBrian Somers                  (bundle->autoload.timer.state != TIMER_RUNNING ||
531f4007327SBrian Somers                   !bundle->autoload.comingup))
53204eaa58cSBrian Somers         bundle_StartAutoLoadTimer(bundle, 1);
53304eaa58cSBrian Somers     }
53404eaa58cSBrian Somers 
535ff0f9439SBrian Somers     if (r && (bundle->phase == PHASE_NETWORK ||
536ff0f9439SBrian Somers               bundle->phys_type.all & PHYS_AUTO)) {
53704eaa58cSBrian Somers       /* enough surplus so that we can tell if we're getting swamped */
53804eaa58cSBrian Somers       want = bundle->cfg.autoload.max.packets + nlinks * 2;
53904eaa58cSBrian Somers       /* but at least 20 packets ! */
54004eaa58cSBrian Somers       if (want < 20)
54104eaa58cSBrian Somers         want = 20;
54204eaa58cSBrian Somers       if (queued < want) {
54304eaa58cSBrian Somers         /* Not enough - select() for more */
5446f8e9f0aSBrian Somers         if (bundle->choked.timer.state == TIMER_RUNNING)
5456f8e9f0aSBrian Somers           timer_Stop(&bundle->choked.timer);	/* Not needed any more */
54604eaa58cSBrian Somers         FD_SET(bundle->dev.fd, r);
547faefde08SBrian Somers         if (*n < bundle->dev.fd + 1)
548faefde08SBrian Somers           *n = bundle->dev.fd + 1;
5491384bd27SBrian Somers         log_Printf(LogTIMER, "%s: fdset(r) %d\n", TUN_NAME, bundle->dev.fd);
550078c562eSBrian Somers         result++;
5516f8e9f0aSBrian Somers       } else if (bundle->choked.timer.state == TIMER_STOPPED) {
5526f8e9f0aSBrian Somers         bundle->choked.timer.func = bundle_ClearQueues;
5536f8e9f0aSBrian Somers         bundle->choked.timer.name = "output choke";
5546f8e9f0aSBrian Somers         bundle->choked.timer.load = bundle->cfg.choked.timeout * SECTICKS;
5556f8e9f0aSBrian Somers         bundle->choked.timer.arg = bundle;
5566f8e9f0aSBrian Somers         timer_Start(&bundle->choked.timer);
557078c562eSBrian Somers       }
55804eaa58cSBrian Somers     }
55904eaa58cSBrian Somers   }
560078c562eSBrian Somers 
561f0cdd9c0SBrian Somers #ifndef NORADIUS
562f0cdd9c0SBrian Somers   result += descriptor_UpdateSet(&bundle->radius.desc, r, w, e, n);
563f0cdd9c0SBrian Somers #endif
564f0cdd9c0SBrian Somers 
56571555108SBrian Somers   /* Which links need a select() ? */
56671555108SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
56771555108SBrian Somers     result += descriptor_UpdateSet(&dl->desc, r, w, e, n);
56871555108SBrian Somers 
569ea722969SBrian Somers   /*
570ea722969SBrian Somers    * This *MUST* be called after the datalink UpdateSet()s as it
57104eaa58cSBrian Somers    * might be ``holding'' one of the datalinks (death-row) and
57204eaa58cSBrian Somers    * wants to be able to de-select() it from the descriptor set.
573ea722969SBrian Somers    */
5740f2f3eb3SBrian Somers   result += descriptor_UpdateSet(&bundle->ncp.mp.server.desc, r, w, e, n);
575ea722969SBrian Somers 
5762f786681SBrian Somers   return result;
5772f786681SBrian Somers }
5782f786681SBrian Somers 
5792f786681SBrian Somers static int
5802f786681SBrian Somers bundle_IsSet(struct descriptor *d, const fd_set *fdset)
5812f786681SBrian Somers {
5822f786681SBrian Somers   struct bundle *bundle = descriptor2bundle(d);
5832f786681SBrian Somers   struct datalink *dl;
5842f786681SBrian Somers 
5852f786681SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
5862f786681SBrian Somers     if (descriptor_IsSet(&dl->desc, fdset))
5872f786681SBrian Somers       return 1;
5882f786681SBrian Somers 
589f0cdd9c0SBrian Somers #ifndef NORADIUS
590f0cdd9c0SBrian Somers   if (descriptor_IsSet(&bundle->radius.desc, fdset))
591f0cdd9c0SBrian Somers     return 1;
592f0cdd9c0SBrian Somers #endif
593f0cdd9c0SBrian Somers 
594332b9de0SBrian Somers   if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset))
595332b9de0SBrian Somers     return 1;
596332b9de0SBrian Somers 
597faefde08SBrian Somers   return FD_ISSET(bundle->dev.fd, fdset);
5982f786681SBrian Somers }
5992f786681SBrian Somers 
6002f786681SBrian Somers static void
6012f786681SBrian Somers bundle_DescriptorRead(struct descriptor *d, struct bundle *bundle,
6022f786681SBrian Somers                       const fd_set *fdset)
6032f786681SBrian Somers {
6042f786681SBrian Somers   struct datalink *dl;
6052f786681SBrian Somers 
606ea722969SBrian Somers   if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset))
607ea722969SBrian Somers     descriptor_Read(&bundle->ncp.mp.server.desc, bundle, fdset);
608ea722969SBrian Somers 
6092f786681SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
6102f786681SBrian Somers     if (descriptor_IsSet(&dl->desc, fdset))
6112f786681SBrian Somers       descriptor_Read(&dl->desc, bundle, fdset);
612b6217683SBrian Somers 
613f0cdd9c0SBrian Somers #ifndef NORADIUS
614f0cdd9c0SBrian Somers   if (descriptor_IsSet(&bundle->radius.desc, fdset))
615f0cdd9c0SBrian Somers     descriptor_Read(&bundle->radius.desc, bundle, fdset);
616f0cdd9c0SBrian Somers #endif
617f0cdd9c0SBrian Somers 
618faefde08SBrian Somers   if (FD_ISSET(bundle->dev.fd, fdset)) {
619078c562eSBrian Somers     struct tun_data tun;
620078c562eSBrian Somers     int n, pri;
621078c562eSBrian Somers 
622078c562eSBrian Somers     /* something to read from tun */
623faefde08SBrian Somers     n = read(bundle->dev.fd, &tun, sizeof tun);
624078c562eSBrian Somers     if (n < 0) {
625a33b2ef7SBrian Somers       log_Printf(LogWARN, "read from %s: %s\n", TUN_NAME, strerror(errno));
626078c562eSBrian Somers       return;
627078c562eSBrian Somers     }
628078c562eSBrian Somers     n -= sizeof tun - sizeof tun.data;
629078c562eSBrian Somers     if (n <= 0) {
630a33b2ef7SBrian Somers       log_Printf(LogERROR, "read from %s: Only %d bytes read ?\n", TUN_NAME, n);
631078c562eSBrian Somers       return;
632078c562eSBrian Somers     }
633078c562eSBrian Somers     if (!tun_check_header(tun, AF_INET))
634078c562eSBrian Somers       return;
635078c562eSBrian Somers 
636078c562eSBrian Somers     if (((struct ip *)tun.data)->ip_dst.s_addr ==
637078c562eSBrian Somers         bundle->ncp.ipcp.my_ip.s_addr) {
638078c562eSBrian Somers       /* we've been asked to send something addressed *to* us :( */
639078c562eSBrian Somers       if (Enabled(bundle, OPT_LOOPBACK)) {
640078c562eSBrian Somers         pri = PacketCheck(bundle, tun.data, n, &bundle->filter.in);
641078c562eSBrian Somers         if (pri >= 0) {
642078c562eSBrian Somers           struct mbuf *bp;
643078c562eSBrian Somers 
644078c562eSBrian Somers           bp = mbuf_Alloc(n, MB_IPIN);
645078c562eSBrian Somers           memcpy(MBUF_CTOP(bp), tun.data, n);
646078c562eSBrian Somers           ip_Input(bundle, bp);
647078c562eSBrian Somers           log_Printf(LogDEBUG, "Looped back packet addressed to myself\n");
648078c562eSBrian Somers         }
649078c562eSBrian Somers         return;
650078c562eSBrian Somers       } else
651078c562eSBrian Somers         log_Printf(LogDEBUG, "Oops - forwarding packet addressed to myself\n");
652078c562eSBrian Somers     }
653078c562eSBrian Somers 
654078c562eSBrian Somers     /*
655078c562eSBrian Somers      * Process on-demand dialup. Output packets are queued within tunnel
656078c562eSBrian Somers      * device until IPCP is opened.
657078c562eSBrian Somers      */
658078c562eSBrian Somers 
659078c562eSBrian Somers     if (bundle_Phase(bundle) == PHASE_DEAD) {
660078c562eSBrian Somers       /*
661078c562eSBrian Somers        * Note, we must be in AUTO mode :-/ otherwise our interface should
662078c562eSBrian Somers        * *not* be UP and we can't receive data
663078c562eSBrian Somers        */
664078c562eSBrian Somers       if ((pri = PacketCheck(bundle, tun.data, n, &bundle->filter.dial)) >= 0)
665ba23f397SBrian Somers         bundle_Open(bundle, NULL, PHYS_AUTO, 0);
666078c562eSBrian Somers       else
667078c562eSBrian Somers         /*
668078c562eSBrian Somers          * Drop the packet.  If we were to queue it, we'd just end up with
669078c562eSBrian Somers          * a pile of timed-out data in our output queue by the time we get
670078c562eSBrian Somers          * around to actually dialing.  We'd also prematurely reach the
671078c562eSBrian Somers          * threshold at which we stop select()ing to read() the tun
672078c562eSBrian Somers          * device - breaking auto-dial.
673078c562eSBrian Somers          */
674078c562eSBrian Somers         return;
675078c562eSBrian Somers     }
676078c562eSBrian Somers 
677078c562eSBrian Somers     pri = PacketCheck(bundle, tun.data, n, &bundle->filter.out);
678078c562eSBrian Somers     if (pri >= 0) {
679078c562eSBrian Somers #ifndef NOALIAS
680615ad4f9SBrian Somers       if (bundle->AliasEnabled) {
681615ad4f9SBrian Somers         PacketAliasOut(tun.data, sizeof tun.data);
682078c562eSBrian Somers         n = ntohs(((struct ip *)tun.data)->ip_len);
683078c562eSBrian Somers       }
684078c562eSBrian Somers #endif
6855a72b6edSBrian Somers       ip_Enqueue(&bundle->ncp.ipcp, pri, tun.data, n);
686078c562eSBrian Somers     }
687078c562eSBrian Somers   }
6882f786681SBrian Somers }
6892f786681SBrian Somers 
6901af29a6eSBrian Somers static int
6912f786681SBrian Somers bundle_DescriptorWrite(struct descriptor *d, struct bundle *bundle,
6922f786681SBrian Somers                        const fd_set *fdset)
6932f786681SBrian Somers {
6942f786681SBrian Somers   struct datalink *dl;
6951af29a6eSBrian Somers   int result = 0;
6962f786681SBrian Somers 
697ea722969SBrian Somers   /* This is not actually necessary as struct mpserver doesn't Write() */
698ea722969SBrian Somers   if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset))
699ea722969SBrian Somers     descriptor_Write(&bundle->ncp.mp.server.desc, bundle, fdset);
700ea722969SBrian Somers 
7012f786681SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
7022f786681SBrian Somers     if (descriptor_IsSet(&dl->desc, fdset))
7031af29a6eSBrian Somers       result += descriptor_Write(&dl->desc, bundle, fdset);
7041af29a6eSBrian Somers 
7051af29a6eSBrian Somers   return result;
7062f786681SBrian Somers }
7072f786681SBrian Somers 
708da66dd13SBrian Somers void
7091384bd27SBrian Somers bundle_LockTun(struct bundle *bundle)
7101384bd27SBrian Somers {
7111384bd27SBrian Somers   FILE *lockfile;
7121384bd27SBrian Somers   char pidfile[MAXPATHLEN];
7131384bd27SBrian Somers 
7141384bd27SBrian Somers   snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit);
7151384bd27SBrian Somers   lockfile = ID0fopen(pidfile, "w");
7161384bd27SBrian Somers   if (lockfile != NULL) {
7171384bd27SBrian Somers     fprintf(lockfile, "%d\n", (int)getpid());
7181384bd27SBrian Somers     fclose(lockfile);
7191384bd27SBrian Somers   }
7201384bd27SBrian Somers #ifndef RELEASE_CRUNCH
7211384bd27SBrian Somers   else
7221384bd27SBrian Somers     log_Printf(LogERROR, "Warning: Can't create %s: %s\n",
7231384bd27SBrian Somers                pidfile, strerror(errno));
7241384bd27SBrian Somers #endif
7251384bd27SBrian Somers }
7261384bd27SBrian Somers 
7271384bd27SBrian Somers static void
7281384bd27SBrian Somers bundle_UnlockTun(struct bundle *bundle)
7291384bd27SBrian Somers {
7301384bd27SBrian Somers   char pidfile[MAXPATHLEN];
7311384bd27SBrian Somers 
7321384bd27SBrian Somers   snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit);
7331384bd27SBrian Somers   ID0unlink(pidfile);
7341384bd27SBrian Somers }
7357a6f8720SBrian Somers 
7367a6f8720SBrian Somers struct bundle *
7378e7b8599SBrian Somers bundle_Create(const char *prefix, int type, const char **argv)
7387a6f8720SBrian Somers {
7397a6f8720SBrian Somers   int s, enoentcount, err;
7408fa6ebe4SBrian Somers   const char *ifname;
7417a6f8720SBrian Somers   struct ifreq ifrq;
7427a6f8720SBrian Somers   static struct bundle bundle;		/* there can be only one */
7430f203c7eSBrian Somers #ifdef TUNSIFMODE
7440f203c7eSBrian Somers   int iff;
7450f203c7eSBrian Somers #endif
7467a6f8720SBrian Somers 
7478fa6ebe4SBrian Somers   if (bundle.iface != NULL) {	/* Already allocated ! */
748a33b2ef7SBrian Somers     log_Printf(LogALERT, "bundle_Create:  There's only one BUNDLE !\n");
7497a6f8720SBrian Somers     return NULL;
7507a6f8720SBrian Somers   }
7517a6f8720SBrian Somers 
7527a6f8720SBrian Somers   err = ENOENT;
7537a6f8720SBrian Somers   enoentcount = 0;
754107d62e7SBrian Somers   for (bundle.unit = 0; ; bundle.unit++) {
755faefde08SBrian Somers     snprintf(bundle.dev.Name, sizeof bundle.dev.Name, "%s%d",
756faefde08SBrian Somers              prefix, bundle.unit);
757faefde08SBrian Somers     bundle.dev.fd = ID0open(bundle.dev.Name, O_RDWR);
758faefde08SBrian Somers     if (bundle.dev.fd >= 0)
7597a6f8720SBrian Somers       break;
760107d62e7SBrian Somers     else if (errno == ENXIO) {
7617a6f8720SBrian Somers       err = errno;
762107d62e7SBrian Somers       break;
7637a6f8720SBrian Somers     } else if (errno == ENOENT) {
7647a6f8720SBrian Somers       if (++enoentcount > 2)
765107d62e7SBrian Somers 	break;
7667a6f8720SBrian Somers     } else
7677a6f8720SBrian Somers       err = errno;
7687a6f8720SBrian Somers   }
7697a6f8720SBrian Somers 
770faefde08SBrian Somers   if (bundle.dev.fd < 0) {
771dd7e2610SBrian Somers     log_Printf(LogWARN, "No available tunnel devices found (%s).\n",
77285b542cfSBrian Somers               strerror(err));
7737a6f8720SBrian Somers     return NULL;
7747a6f8720SBrian Somers   }
7757a6f8720SBrian Somers 
776dd7e2610SBrian Somers   log_SetTun(bundle.unit);
7778e7b8599SBrian Somers   bundle.argv = argv;
7780f781a72SBrian Somers   bundle.argv0 = argv[0];
7790f781a72SBrian Somers   bundle.argv1 = argv[1];
7807a6f8720SBrian Somers 
7817a6f8720SBrian Somers   s = socket(AF_INET, SOCK_DGRAM, 0);
7827a6f8720SBrian Somers   if (s < 0) {
783dd7e2610SBrian Somers     log_Printf(LogERROR, "bundle_Create: socket(): %s\n", strerror(errno));
784faefde08SBrian Somers     close(bundle.dev.fd);
7857a6f8720SBrian Somers     return NULL;
7867a6f8720SBrian Somers   }
7877a6f8720SBrian Somers 
7888fa6ebe4SBrian Somers   ifname = strrchr(bundle.dev.Name, '/');
7898fa6ebe4SBrian Somers   if (ifname == NULL)
7908fa6ebe4SBrian Somers     ifname = bundle.dev.Name;
7917a6f8720SBrian Somers   else
7928fa6ebe4SBrian Somers     ifname++;
7938fa6ebe4SBrian Somers 
7948fa6ebe4SBrian Somers   bundle.iface = iface_Create(ifname);
7958fa6ebe4SBrian Somers   if (bundle.iface == NULL) {
7968fa6ebe4SBrian Somers     close(s);
7978fa6ebe4SBrian Somers     close(bundle.dev.fd);
7988fa6ebe4SBrian Somers     return NULL;
7998fa6ebe4SBrian Somers   }
8007a6f8720SBrian Somers 
8010f203c7eSBrian Somers #ifdef TUNSIFMODE
8020f203c7eSBrian Somers   /* Make sure we're POINTOPOINT */
8030f203c7eSBrian Somers   iff = IFF_POINTOPOINT;
8040f203c7eSBrian Somers   if (ID0ioctl(bundle.dev.fd, TUNSIFMODE, &iff) < 0)
8050f203c7eSBrian Somers     log_Printf(LogERROR, "bundle_Create: ioctl(TUNSIFMODE): %s\n",
8060f203c7eSBrian Somers 	       strerror(errno));
8070f203c7eSBrian Somers #endif
8080f203c7eSBrian Somers 
8097a6f8720SBrian Somers   /*
8107a6f8720SBrian Somers    * Now, bring up the interface.
8117a6f8720SBrian Somers    */
8127a6f8720SBrian Somers   memset(&ifrq, '\0', sizeof ifrq);
8138fa6ebe4SBrian Somers   strncpy(ifrq.ifr_name, ifname, sizeof ifrq.ifr_name - 1);
8147a6f8720SBrian Somers   ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0';
8157a6f8720SBrian Somers   if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
816a33b2ef7SBrian Somers     log_Printf(LogERROR, "bundle_Create: ioctl(SIOCGIFFLAGS): %s\n",
8177a6f8720SBrian Somers 	      strerror(errno));
8187a6f8720SBrian Somers     close(s);
8198fa6ebe4SBrian Somers     iface_Destroy(bundle.iface);
8208fa6ebe4SBrian Somers     bundle.iface = NULL;
821faefde08SBrian Somers     close(bundle.dev.fd);
8227a6f8720SBrian Somers     return NULL;
8237a6f8720SBrian Somers   }
8247a6f8720SBrian Somers   ifrq.ifr_flags |= IFF_UP;
8257a6f8720SBrian Somers   if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) {
826a33b2ef7SBrian Somers     log_Printf(LogERROR, "bundle_Create: ioctl(SIOCSIFFLAGS): %s\n",
8277a6f8720SBrian Somers 	      strerror(errno));
8287a6f8720SBrian Somers     close(s);
8298fa6ebe4SBrian Somers     iface_Destroy(bundle.iface);
8308fa6ebe4SBrian Somers     bundle.iface = NULL;
831faefde08SBrian Somers     close(bundle.dev.fd);
8327a6f8720SBrian Somers     return NULL;
8337a6f8720SBrian Somers   }
8347a6f8720SBrian Somers 
8357a6f8720SBrian Somers   close(s);
8367a6f8720SBrian Somers 
8378fa6ebe4SBrian Somers   log_Printf(LogPHASE, "Using interface: %s\n", ifname);
8387a6f8720SBrian Somers 
8398fa6ebe4SBrian Somers   bundle.ifSpeed = 0;
8407a6f8720SBrian Somers 
841820de6ebSBrian Somers   bundle.routing_seq = 0;
842a0cbd833SBrian Somers   bundle.phase = PHASE_DEAD;
843a0cbd833SBrian Somers   bundle.CleaningUp = 0;
844615ad4f9SBrian Somers   bundle.AliasEnabled = 0;
8457a6f8720SBrian Somers 
8466d666775SBrian Somers   bundle.fsm.LayerStart = bundle_LayerStart;
8476f384573SBrian Somers   bundle.fsm.LayerUp = bundle_LayerUp;
8486d666775SBrian Somers   bundle.fsm.LayerDown = bundle_LayerDown;
8496d666775SBrian Somers   bundle.fsm.LayerFinish = bundle_LayerFinish;
8506d666775SBrian Somers   bundle.fsm.object = &bundle;
8517a6f8720SBrian Somers 
852ab886ad0SBrian Somers   bundle.cfg.idle_timeout = NCP_IDLE_TIMEOUT;
8531342caedSBrian Somers   *bundle.cfg.auth.name = '\0';
8541342caedSBrian Somers   *bundle.cfg.auth.key = '\0';
855610b185fSBrian Somers   bundle.cfg.opt = OPT_SROUTES | OPT_IDCHECK | OPT_LOOPBACK |
856610b185fSBrian Somers                    OPT_THROUGHPUT | OPT_UTMP;
85749052c95SBrian Somers   *bundle.cfg.label = '\0';
85849052c95SBrian Somers   bundle.cfg.mtu = DEF_MTU;
85904eaa58cSBrian Somers   bundle.cfg.autoload.max.packets = 0;
86004eaa58cSBrian Somers   bundle.cfg.autoload.max.timeout = 0;
86104eaa58cSBrian Somers   bundle.cfg.autoload.min.packets = 0;
86204eaa58cSBrian Somers   bundle.cfg.autoload.min.timeout = 0;
8636f8e9f0aSBrian Somers   bundle.cfg.choked.timeout = CHOKED_TIMEOUT;
864ff0f9439SBrian Somers   bundle.phys_type.all = type;
865ff0f9439SBrian Somers   bundle.phys_type.open = 0;
866ab886ad0SBrian Somers 
8676f384573SBrian Somers   bundle.links = datalink_Create("deflink", &bundle, type);
8683006ec67SBrian Somers   if (bundle.links == NULL) {
869a33b2ef7SBrian Somers     log_Printf(LogALERT, "Cannot create data link: %s\n", strerror(errno));
8708fa6ebe4SBrian Somers     iface_Destroy(bundle.iface);
8718fa6ebe4SBrian Somers     bundle.iface = NULL;
872faefde08SBrian Somers     close(bundle.dev.fd);
8732289f246SBrian Somers     return NULL;
8742289f246SBrian Somers   }
8752289f246SBrian Somers 
8762f786681SBrian Somers   bundle.desc.type = BUNDLE_DESCRIPTOR;
8772f786681SBrian Somers   bundle.desc.UpdateSet = bundle_UpdateSet;
8782f786681SBrian Somers   bundle.desc.IsSet = bundle_IsSet;
8792f786681SBrian Somers   bundle.desc.Read = bundle_DescriptorRead;
8802f786681SBrian Somers   bundle.desc.Write = bundle_DescriptorWrite;
8812f786681SBrian Somers 
88249052c95SBrian Somers   mp_Init(&bundle.ncp.mp, &bundle);
88349052c95SBrian Somers 
88449052c95SBrian Somers   /* Send over the first physical link by default */
8855828db6dSBrian Somers   ipcp_Init(&bundle.ncp.ipcp, &bundle, &bundle.links->physical->link,
8865828db6dSBrian Somers             &bundle.fsm);
8876d666775SBrian Somers 
8885ca5389aSBrian Somers   memset(&bundle.filter, '\0', sizeof bundle.filter);
8895ca5389aSBrian Somers   bundle.filter.in.fragok = bundle.filter.in.logok = 1;
8905ca5389aSBrian Somers   bundle.filter.in.name = "IN";
8915ca5389aSBrian Somers   bundle.filter.out.fragok = bundle.filter.out.logok = 1;
8925ca5389aSBrian Somers   bundle.filter.out.name = "OUT";
8935ca5389aSBrian Somers   bundle.filter.dial.name = "DIAL";
8948390b576SBrian Somers   bundle.filter.dial.logok = 1;
8955ca5389aSBrian Somers   bundle.filter.alive.name = "ALIVE";
8965ca5389aSBrian Somers   bundle.filter.alive.logok = 1;
89793ee0ff2SBrian Somers   memset(&bundle.idle.timer, '\0', sizeof bundle.idle.timer);
89893ee0ff2SBrian Somers   bundle.idle.done = 0;
8995cf4388bSBrian Somers   bundle.notify.fd = -1;
90004eaa58cSBrian Somers   memset(&bundle.autoload.timer, '\0', sizeof bundle.autoload.timer);
90104eaa58cSBrian Somers   bundle.autoload.done = 0;
90204eaa58cSBrian Somers   bundle.autoload.running = 0;
9036f8e9f0aSBrian Somers   memset(&bundle.choked.timer, '\0', sizeof bundle.choked.timer);
904972a1bcfSBrian Somers #ifndef NORADIUS
905972a1bcfSBrian Somers   radius_Init(&bundle.radius);
906972a1bcfSBrian Somers #endif
90793ee0ff2SBrian Somers 
9086d666775SBrian Somers   /* Clean out any leftover crud */
9098fa6ebe4SBrian Somers   iface_Clear(bundle.iface, IFACE_CLEAR_ALL);
9106d666775SBrian Somers 
9111384bd27SBrian Somers   bundle_LockTun(&bundle);
9121384bd27SBrian Somers 
9137a6f8720SBrian Somers   return &bundle;
9147a6f8720SBrian Somers }
9157a6f8720SBrian Somers 
91668a0f0ccSBrian Somers static void
91768a0f0ccSBrian Somers bundle_DownInterface(struct bundle *bundle)
91868a0f0ccSBrian Somers {
91968a0f0ccSBrian Somers   struct ifreq ifrq;
92068a0f0ccSBrian Somers   int s;
92168a0f0ccSBrian Somers 
922dd7e2610SBrian Somers   route_IfDelete(bundle, 1);
92368a0f0ccSBrian Somers 
92468a0f0ccSBrian Somers   s = ID0socket(AF_INET, SOCK_DGRAM, 0);
92568a0f0ccSBrian Somers   if (s < 0) {
926dd7e2610SBrian Somers     log_Printf(LogERROR, "bundle_DownInterface: socket: %s\n", strerror(errno));
92768a0f0ccSBrian Somers     return;
92868a0f0ccSBrian Somers   }
92968a0f0ccSBrian Somers 
93068a0f0ccSBrian Somers   memset(&ifrq, '\0', sizeof ifrq);
9318fa6ebe4SBrian Somers   strncpy(ifrq.ifr_name, bundle->iface->name, sizeof ifrq.ifr_name - 1);
93268a0f0ccSBrian Somers   ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0';
93368a0f0ccSBrian Somers   if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
934dd7e2610SBrian Somers     log_Printf(LogERROR, "bundle_DownInterface: ioctl(SIOCGIFFLAGS): %s\n",
93568a0f0ccSBrian Somers        strerror(errno));
93668a0f0ccSBrian Somers     close(s);
93768a0f0ccSBrian Somers     return;
93868a0f0ccSBrian Somers   }
93968a0f0ccSBrian Somers   ifrq.ifr_flags &= ~IFF_UP;
94068a0f0ccSBrian Somers   if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) {
941dd7e2610SBrian Somers     log_Printf(LogERROR, "bundle_DownInterface: ioctl(SIOCSIFFLAGS): %s\n",
94268a0f0ccSBrian Somers        strerror(errno));
94368a0f0ccSBrian Somers     close(s);
94468a0f0ccSBrian Somers     return;
94568a0f0ccSBrian Somers   }
94668a0f0ccSBrian Somers   close(s);
94768a0f0ccSBrian Somers }
94868a0f0ccSBrian Somers 
94968a0f0ccSBrian Somers void
95068a0f0ccSBrian Somers bundle_Destroy(struct bundle *bundle)
95168a0f0ccSBrian Somers {
9523006ec67SBrian Somers   struct datalink *dl;
953b6217683SBrian Somers 
954ea722969SBrian Somers   /*
95504eaa58cSBrian Somers    * Clean up the interface.  We don't need to timer_Stop()s, mp_Down(),
956ea722969SBrian Somers    * ipcp_CleanInterface() and bundle_DownInterface() unless we're getting
957ea722969SBrian Somers    * out under exceptional conditions such as a descriptor exception.
958ea722969SBrian Somers    */
95904eaa58cSBrian Somers   timer_Stop(&bundle->idle.timer);
9606f8e9f0aSBrian Somers   timer_Stop(&bundle->choked.timer);
96104eaa58cSBrian Somers   timer_Stop(&bundle->autoload.timer);
96266f634b6SBrian Somers   mp_Down(&bundle->ncp.mp);
963dd7e2610SBrian Somers   ipcp_CleanInterface(&bundle->ncp.ipcp);
96468a0f0ccSBrian Somers   bundle_DownInterface(bundle);
9653006ec67SBrian Somers 
966972a1bcfSBrian Somers #ifndef NORADIUS
967972a1bcfSBrian Somers   /* Tell the radius server the bad news */
968972a1bcfSBrian Somers   radius_Destroy(&bundle->radius);
969972a1bcfSBrian Somers #endif
970972a1bcfSBrian Somers 
971ea722969SBrian Somers   /* Again, these are all DATALINK_CLOSED unless we're abending */
9723006ec67SBrian Somers   dl = bundle->links;
9733006ec67SBrian Somers   while (dl)
9743006ec67SBrian Somers     dl = datalink_Destroy(dl);
9753006ec67SBrian Somers 
9761384bd27SBrian Somers   close(bundle->dev.fd);
9771384bd27SBrian Somers   bundle_UnlockTun(bundle);
9781384bd27SBrian Somers 
979ea722969SBrian Somers   /* In case we never made PHASE_NETWORK */
9805cf4388bSBrian Somers   bundle_Notify(bundle, EX_ERRDEAD);
981b6217683SBrian Somers 
9828fa6ebe4SBrian Somers   iface_Destroy(bundle->iface);
9838fa6ebe4SBrian Somers   bundle->iface = NULL;
98468a0f0ccSBrian Somers }
98568a0f0ccSBrian Somers 
9867a6f8720SBrian Somers struct rtmsg {
9877a6f8720SBrian Somers   struct rt_msghdr m_rtm;
9887a6f8720SBrian Somers   char m_space[64];
9897a6f8720SBrian Somers };
9907a6f8720SBrian Somers 
991610b185fSBrian Somers int
992820de6ebSBrian Somers bundle_SetRoute(struct bundle *bundle, int cmd, struct in_addr dst,
9932062443fSBrian Somers                 struct in_addr gateway, struct in_addr mask, int bang, int ssh)
9947a6f8720SBrian Somers {
9957a6f8720SBrian Somers   struct rtmsg rtmes;
9967a6f8720SBrian Somers   int s, nb, wb;
9977a6f8720SBrian Somers   char *cp;
9987a6f8720SBrian Somers   const char *cmdstr;
9997a6f8720SBrian Somers   struct sockaddr_in rtdata;
1000610b185fSBrian Somers   int result = 1;
10017a6f8720SBrian Somers 
10027a6f8720SBrian Somers   if (bang)
10037a6f8720SBrian Somers     cmdstr = (cmd == RTM_ADD ? "Add!" : "Delete!");
10047a6f8720SBrian Somers   else
10057a6f8720SBrian Somers     cmdstr = (cmd == RTM_ADD ? "Add" : "Delete");
10067a6f8720SBrian Somers   s = ID0socket(PF_ROUTE, SOCK_RAW, 0);
10077a6f8720SBrian Somers   if (s < 0) {
1008dd7e2610SBrian Somers     log_Printf(LogERROR, "bundle_SetRoute: socket(): %s\n", strerror(errno));
1009610b185fSBrian Somers     return result;
10107a6f8720SBrian Somers   }
10117a6f8720SBrian Somers   memset(&rtmes, '\0', sizeof rtmes);
10127a6f8720SBrian Somers   rtmes.m_rtm.rtm_version = RTM_VERSION;
10137a6f8720SBrian Somers   rtmes.m_rtm.rtm_type = cmd;
10147a6f8720SBrian Somers   rtmes.m_rtm.rtm_addrs = RTA_DST;
1015820de6ebSBrian Somers   rtmes.m_rtm.rtm_seq = ++bundle->routing_seq;
10167a6f8720SBrian Somers   rtmes.m_rtm.rtm_pid = getpid();
10177a6f8720SBrian Somers   rtmes.m_rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;
10187a6f8720SBrian Somers 
10193afe5ccbSBrian Somers   if (cmd == RTM_ADD || cmd == RTM_CHANGE) {
10203afe5ccbSBrian Somers     if (bundle->ncp.ipcp.cfg.sendpipe > 0) {
10213afe5ccbSBrian Somers       rtmes.m_rtm.rtm_rmx.rmx_sendpipe = bundle->ncp.ipcp.cfg.sendpipe;
10223afe5ccbSBrian Somers       rtmes.m_rtm.rtm_inits |= RTV_SPIPE;
10233afe5ccbSBrian Somers     }
10243afe5ccbSBrian Somers     if (bundle->ncp.ipcp.cfg.recvpipe > 0) {
10253afe5ccbSBrian Somers       rtmes.m_rtm.rtm_rmx.rmx_recvpipe = bundle->ncp.ipcp.cfg.recvpipe;
10263afe5ccbSBrian Somers       rtmes.m_rtm.rtm_inits |= RTV_RPIPE;
10273afe5ccbSBrian Somers     }
10283afe5ccbSBrian Somers   }
10293afe5ccbSBrian Somers 
10307a6f8720SBrian Somers   memset(&rtdata, '\0', sizeof rtdata);
103150e5c17dSBrian Somers   rtdata.sin_len = sizeof rtdata;
10327a6f8720SBrian Somers   rtdata.sin_family = AF_INET;
10337a6f8720SBrian Somers   rtdata.sin_port = 0;
10347a6f8720SBrian Somers   rtdata.sin_addr = dst;
10357a6f8720SBrian Somers 
10367a6f8720SBrian Somers   cp = rtmes.m_space;
103750e5c17dSBrian Somers   memcpy(cp, &rtdata, rtdata.sin_len);
103850e5c17dSBrian Somers   cp += rtdata.sin_len;
1039e43ebac1SBrian Somers   if (cmd == RTM_ADD) {
10407a6f8720SBrian Somers     if (gateway.s_addr == INADDR_ANY) {
10410aa8aa17SBrian Somers       if (!ssh)
10428fa6ebe4SBrian Somers         log_Printf(LogERROR, "bundle_SetRoute: Cannot add a route with"
10438fa6ebe4SBrian Somers                    " destination 0.0.0.0\n");
10448fa6ebe4SBrian Somers       close(s);
10458fa6ebe4SBrian Somers       return result;
10467a6f8720SBrian Somers     } else {
10477a6f8720SBrian Somers       rtdata.sin_addr = gateway;
104850e5c17dSBrian Somers       memcpy(cp, &rtdata, rtdata.sin_len);
104950e5c17dSBrian Somers       cp += rtdata.sin_len;
10507a6f8720SBrian Somers       rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY;
10517a6f8720SBrian Somers     }
1052e43ebac1SBrian Somers   }
10537a6f8720SBrian Somers 
10547a6f8720SBrian Somers   if (dst.s_addr == INADDR_ANY)
10557a6f8720SBrian Somers     mask.s_addr = INADDR_ANY;
10567a6f8720SBrian Somers 
10577a6f8720SBrian Somers   if (cmd == RTM_ADD || dst.s_addr == INADDR_ANY) {
10587a6f8720SBrian Somers     rtdata.sin_addr = mask;
105950e5c17dSBrian Somers     memcpy(cp, &rtdata, rtdata.sin_len);
106050e5c17dSBrian Somers     cp += rtdata.sin_len;
10617a6f8720SBrian Somers     rtmes.m_rtm.rtm_addrs |= RTA_NETMASK;
10627a6f8720SBrian Somers   }
10637a6f8720SBrian Somers 
10647a6f8720SBrian Somers   nb = cp - (char *) &rtmes;
10657a6f8720SBrian Somers   rtmes.m_rtm.rtm_msglen = nb;
10667a6f8720SBrian Somers   wb = ID0write(s, &rtmes, nb);
10677a6f8720SBrian Somers   if (wb < 0) {
1068dd7e2610SBrian Somers     log_Printf(LogTCPIP, "bundle_SetRoute failure:\n");
1069dd7e2610SBrian Somers     log_Printf(LogTCPIP, "bundle_SetRoute:  Cmd = %s\n", cmdstr);
1070dd7e2610SBrian Somers     log_Printf(LogTCPIP, "bundle_SetRoute:  Dst = %s\n", inet_ntoa(dst));
1071dd7e2610SBrian Somers     log_Printf(LogTCPIP, "bundle_SetRoute:  Gateway = %s\n", inet_ntoa(gateway));
1072dd7e2610SBrian Somers     log_Printf(LogTCPIP, "bundle_SetRoute:  Mask = %s\n", inet_ntoa(mask));
10737a6f8720SBrian Somers failed:
10747a6f8720SBrian Somers     if (cmd == RTM_ADD && (rtmes.m_rtm.rtm_errno == EEXIST ||
1075e43ebac1SBrian Somers                            (rtmes.m_rtm.rtm_errno == 0 && errno == EEXIST))) {
1076610b185fSBrian Somers       if (!bang) {
1077dd7e2610SBrian Somers         log_Printf(LogWARN, "Add route failed: %s already exists\n",
10783afe5ccbSBrian Somers 		  dst.s_addr == 0 ? "default" : inet_ntoa(dst));
1079610b185fSBrian Somers         result = 0;	/* Don't add to our dynamic list */
1080610b185fSBrian Somers       } else {
10817a6f8720SBrian Somers         rtmes.m_rtm.rtm_type = cmd = RTM_CHANGE;
10827a6f8720SBrian Somers         if ((wb = ID0write(s, &rtmes, nb)) < 0)
10837a6f8720SBrian Somers           goto failed;
10847a6f8720SBrian Somers       }
1085e43ebac1SBrian Somers     } else if (cmd == RTM_DELETE &&
10867a6f8720SBrian Somers              (rtmes.m_rtm.rtm_errno == ESRCH ||
10877a6f8720SBrian Somers               (rtmes.m_rtm.rtm_errno == 0 && errno == ESRCH))) {
10887a6f8720SBrian Somers       if (!bang)
1089dd7e2610SBrian Somers         log_Printf(LogWARN, "Del route failed: %s: Non-existent\n",
10907a6f8720SBrian Somers                   inet_ntoa(dst));
10912062443fSBrian Somers     } else if (rtmes.m_rtm.rtm_errno == 0) {
10922062443fSBrian Somers       if (!ssh || errno != ENETUNREACH)
1093dd7e2610SBrian Somers         log_Printf(LogWARN, "%s route failed: %s: errno: %s\n", cmdstr,
10947a6f8720SBrian Somers                    inet_ntoa(dst), strerror(errno));
10952062443fSBrian Somers     } else
1096dd7e2610SBrian Somers       log_Printf(LogWARN, "%s route failed: %s: %s\n",
10977a6f8720SBrian Somers 		 cmdstr, inet_ntoa(dst), strerror(rtmes.m_rtm.rtm_errno));
10987a6f8720SBrian Somers   }
1099dd7e2610SBrian Somers   log_Printf(LogDEBUG, "wrote %d: cmd = %s, dst = %x, gateway = %x\n",
1100fe3125a0SBrian Somers             wb, cmdstr, (unsigned)dst.s_addr, (unsigned)gateway.s_addr);
11017a6f8720SBrian Somers   close(s);
1102610b185fSBrian Somers 
1103610b185fSBrian Somers   return result;
11047a6f8720SBrian Somers }
110583d1af55SBrian Somers 
110683d1af55SBrian Somers void
11073006ec67SBrian Somers bundle_LinkClosed(struct bundle *bundle, struct datalink *dl)
11083006ec67SBrian Somers {
11093006ec67SBrian Somers   /*
11103006ec67SBrian Somers    * Our datalink has closed.
1111ea722969SBrian Somers    * CleanDatalinks() (called from DoLoop()) will remove closed
111281358fa3SBrian Somers    * BACKGROUND and DIRECT links.
11133b0f8d2eSBrian Somers    * If it's the last data link, enter phase DEAD.
1114ea722969SBrian Somers    *
1115ea722969SBrian Somers    * NOTE: dl may not be in our list (bundle_SendDatalink()) !
11163006ec67SBrian Somers    */
11175b8b8060SBrian Somers 
11183b0f8d2eSBrian Somers   struct datalink *odl;
11193b0f8d2eSBrian Somers   int other_links;
11205b8b8060SBrian Somers 
1121bf1d3ff6SBrian Somers   log_SetTtyCommandMode(dl);
1122bf1d3ff6SBrian Somers 
11233b0f8d2eSBrian Somers   other_links = 0;
11243b0f8d2eSBrian Somers   for (odl = bundle->links; odl; odl = odl->next)
11253b0f8d2eSBrian Somers     if (odl != dl && odl->state != DATALINK_CLOSED)
11263b0f8d2eSBrian Somers       other_links++;
11273b0f8d2eSBrian Somers 
11283b0f8d2eSBrian Somers   if (!other_links) {
112981358fa3SBrian Somers     if (dl->physical->type != PHYS_AUTO)	/* Not in -auto mode */
113003704096SBrian Somers       bundle_DownInterface(bundle);
113109206a6fSBrian Somers     fsm2initial(&bundle->ncp.ipcp.fsm);
11325563ebdeSBrian Somers     bundle_NewPhase(bundle, PHASE_DEAD);
11330f2f3eb3SBrian Somers     bundle_StopIdleTimer(bundle);
113404eaa58cSBrian Somers     bundle_StopAutoLoadTimer(bundle);
113504eaa58cSBrian Somers     bundle->autoload.running = 0;
113604eaa58cSBrian Somers   } else
113704eaa58cSBrian Somers     bundle->autoload.running = 1;
1138455aabc3SBrian Somers }
1139455aabc3SBrian Somers 
1140455aabc3SBrian Somers void
1141ba23f397SBrian Somers bundle_Open(struct bundle *bundle, const char *name, int mask, int force)
11423006ec67SBrian Somers {
11433006ec67SBrian Somers   /*
11443006ec67SBrian Somers    * Please open the given datalink, or all if name == NULL
11453006ec67SBrian Somers    */
11463006ec67SBrian Somers   struct datalink *dl;
11473006ec67SBrian Somers 
114804eaa58cSBrian Somers   timer_Stop(&bundle->autoload.timer);
11493006ec67SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
11503006ec67SBrian Somers     if (name == NULL || !strcasecmp(dl->name, name)) {
1151ba23f397SBrian Somers       if ((mask & dl->physical->type) &&
1152ba23f397SBrian Somers           (dl->state == DATALINK_CLOSED ||
1153ba23f397SBrian Somers            (force && dl->state == DATALINK_OPENING &&
1154c11e57a3SBrian Somers             dl->dial.timer.state == TIMER_RUNNING))) {
1155c11e57a3SBrian Somers         if (force)	/* Ignore redial timeout ? */
1156c11e57a3SBrian Somers           timer_Stop(&dl->dial.timer);
1157565e35e5SBrian Somers         datalink_Up(dl, 1, 1);
115881358fa3SBrian Somers         if (mask == PHYS_AUTO)
115981358fa3SBrian Somers           /* Only one AUTO link at a time (see the AutoLoad timer) */
116004eaa58cSBrian Somers           break;
116104eaa58cSBrian Somers       }
11623006ec67SBrian Somers       if (name != NULL)
11633006ec67SBrian Somers         break;
11643006ec67SBrian Somers     }
11653006ec67SBrian Somers }
11663006ec67SBrian Somers 
11673006ec67SBrian Somers struct datalink *
11683006ec67SBrian Somers bundle2datalink(struct bundle *bundle, const char *name)
11693006ec67SBrian Somers {
11703006ec67SBrian Somers   struct datalink *dl;
11713006ec67SBrian Somers 
11723006ec67SBrian Somers   if (name != NULL) {
11733006ec67SBrian Somers     for (dl = bundle->links; dl; dl = dl->next)
11743006ec67SBrian Somers       if (!strcasecmp(dl->name, name))
11753006ec67SBrian Somers         return dl;
11763006ec67SBrian Somers   } else if (bundle->links && !bundle->links->next)
11773006ec67SBrian Somers     return bundle->links;
11783006ec67SBrian Somers 
11793006ec67SBrian Somers   return NULL;
11803006ec67SBrian Somers }
11813006ec67SBrian Somers 
11823006ec67SBrian Somers int
11833006ec67SBrian Somers bundle_FillQueues(struct bundle *bundle)
11843006ec67SBrian Somers {
11853b0f8d2eSBrian Somers   int total;
11863b0f8d2eSBrian Somers 
11871bc9b5baSBrian Somers   if (bundle->ncp.mp.active)
11883b0f8d2eSBrian Somers     total = mp_FillQueues(bundle);
11891bc9b5baSBrian Somers   else {
11901bc9b5baSBrian Somers     struct datalink *dl;
11911bc9b5baSBrian Somers     int add;
11921bc9b5baSBrian Somers 
11931bc9b5baSBrian Somers     for (total = 0, dl = bundle->links; dl; dl = dl->next)
11941bc9b5baSBrian Somers       if (dl->state == DATALINK_OPEN) {
11951bc9b5baSBrian Somers         add = link_QueueLen(&dl->physical->link);
11961bc9b5baSBrian Somers         if (add == 0 && dl->physical->out == NULL)
11971bc9b5baSBrian Somers           add = ip_FlushPacket(&dl->physical->link, bundle);
11981bc9b5baSBrian Somers         total += add;
11991bc9b5baSBrian Somers       }
12003006ec67SBrian Somers   }
12013006ec67SBrian Somers 
12025a72b6edSBrian Somers   return total + ip_QueueLen(&bundle->ncp.ipcp);
12033006ec67SBrian Somers }
1204aef795ccSBrian Somers 
1205aef795ccSBrian Somers int
1206aef795ccSBrian Somers bundle_ShowLinks(struct cmdargs const *arg)
1207aef795ccSBrian Somers {
1208aef795ccSBrian Somers   struct datalink *dl;
1209aef795ccSBrian Somers 
12109c53a7b1SBrian Somers   for (dl = arg->bundle->links; dl; dl = dl->next) {
1211d4156d00SBrian Somers     prompt_Printf(arg->prompt, "Name: %s [%s, %s]",
1212d4156d00SBrian Somers                   dl->name, mode2Nam(dl->physical->type), datalink_State(dl));
1213eeab6bf5SBrian Somers     if (dl->physical->link.throughput.rolling && dl->state == DATALINK_OPEN)
1214d4156d00SBrian Somers       prompt_Printf(arg->prompt, " weight %d, %d bytes/sec",
1215eeab6bf5SBrian Somers                     dl->mp.weight,
12169c53a7b1SBrian Somers                     dl->physical->link.throughput.OctetsPerSecond);
12179c53a7b1SBrian Somers     prompt_Printf(arg->prompt, "\n");
12189c53a7b1SBrian Somers   }
1219aef795ccSBrian Somers 
1220aef795ccSBrian Somers   return 0;
1221aef795ccSBrian Somers }
1222ab886ad0SBrian Somers 
12231342caedSBrian Somers static const char *
12241342caedSBrian Somers optval(struct bundle *bundle, int bit)
12251342caedSBrian Somers {
12261342caedSBrian Somers   return (bundle->cfg.opt & bit) ? "enabled" : "disabled";
12271342caedSBrian Somers }
12281342caedSBrian Somers 
1229c08717dfSBrian Somers int
1230c08717dfSBrian Somers bundle_ShowStatus(struct cmdargs const *arg)
1231c08717dfSBrian Somers {
1232c08717dfSBrian Somers   int remaining;
1233c08717dfSBrian Somers 
1234c08717dfSBrian Somers   prompt_Printf(arg->prompt, "Phase %s\n", bundle_PhaseName(arg->bundle));
12350f781a72SBrian Somers   prompt_Printf(arg->prompt, " Title:         %s\n", arg->bundle->argv[0]);
1236faefde08SBrian Somers   prompt_Printf(arg->prompt, " Device:        %s\n", arg->bundle->dev.Name);
1237faefde08SBrian Somers   prompt_Printf(arg->prompt, " Interface:     %s @ %lubps\n",
12388fa6ebe4SBrian Somers                 arg->bundle->iface->name, arg->bundle->ifSpeed);
1239c08717dfSBrian Somers 
1240c08717dfSBrian Somers   prompt_Printf(arg->prompt, "\nDefaults:\n");
1241643f4904SBrian Somers   prompt_Printf(arg->prompt, " Label:         %s\n", arg->bundle->cfg.label);
1242610b185fSBrian Somers   prompt_Printf(arg->prompt, " Auth name:     %s\n",
1243610b185fSBrian Somers                 arg->bundle->cfg.auth.name);
124404eaa58cSBrian Somers   prompt_Printf(arg->prompt, " Auto Load:     Up after %ds of >= %d packets\n",
124504eaa58cSBrian Somers                 arg->bundle->cfg.autoload.max.timeout,
124604eaa58cSBrian Somers                 arg->bundle->cfg.autoload.max.packets);
124704eaa58cSBrian Somers   prompt_Printf(arg->prompt, "                Down after %ds of <= %d"
124804eaa58cSBrian Somers                 " packets\n", arg->bundle->cfg.autoload.min.timeout,
124904eaa58cSBrian Somers                 arg->bundle->cfg.autoload.min.packets);
125004eaa58cSBrian Somers   if (arg->bundle->autoload.timer.state == TIMER_RUNNING)
125104eaa58cSBrian Somers     prompt_Printf(arg->prompt, "                %ds remaining 'till "
125204eaa58cSBrian Somers                   "a link comes %s\n",
125304eaa58cSBrian Somers                   bundle_RemainingAutoLoadTime(arg->bundle),
125404eaa58cSBrian Somers                   arg->bundle->autoload.comingup ? "up" : "down");
125504eaa58cSBrian Somers   else
125604eaa58cSBrian Somers     prompt_Printf(arg->prompt, "                %srunning with %d"
125704eaa58cSBrian Somers                   " packets queued\n", arg->bundle->autoload.running ?
12585a72b6edSBrian Somers                   "" : "not ", ip_QueueLen(&arg->bundle->ncp.ipcp));
125904eaa58cSBrian Somers 
12606f8e9f0aSBrian Somers   prompt_Printf(arg->prompt, " Choked Timer:  %ds\n",
12616f8e9f0aSBrian Somers                 arg->bundle->cfg.choked.timeout);
1262972a1bcfSBrian Somers 
1263972a1bcfSBrian Somers #ifndef NORADIUS
1264972a1bcfSBrian Somers   radius_Show(&arg->bundle->radius, arg->prompt);
1265972a1bcfSBrian Somers #endif
1266972a1bcfSBrian Somers 
1267c08717dfSBrian Somers   prompt_Printf(arg->prompt, " Idle Timer:    ");
1268c08717dfSBrian Somers   if (arg->bundle->cfg.idle_timeout) {
1269c08717dfSBrian Somers     prompt_Printf(arg->prompt, "%ds", arg->bundle->cfg.idle_timeout);
1270c08717dfSBrian Somers     remaining = bundle_RemainingIdleTime(arg->bundle);
1271c08717dfSBrian Somers     if (remaining != -1)
1272c08717dfSBrian Somers       prompt_Printf(arg->prompt, " (%ds remaining)", remaining);
1273c08717dfSBrian Somers     prompt_Printf(arg->prompt, "\n");
1274c08717dfSBrian Somers   } else
1275c08717dfSBrian Somers     prompt_Printf(arg->prompt, "disabled\n");
1276ce828a6eSBrian Somers   prompt_Printf(arg->prompt, " MTU:           ");
1277ce828a6eSBrian Somers   if (arg->bundle->cfg.mtu)
1278ce828a6eSBrian Somers     prompt_Printf(arg->prompt, "%d\n", arg->bundle->cfg.mtu);
1279ce828a6eSBrian Somers   else
1280ce828a6eSBrian Somers     prompt_Printf(arg->prompt, "unspecified\n");
1281c08717dfSBrian Somers 
12823afe5ccbSBrian Somers   prompt_Printf(arg->prompt, " sendpipe:      ");
12833afe5ccbSBrian Somers   if (arg->bundle->ncp.ipcp.cfg.sendpipe > 0)
12843afe5ccbSBrian Somers     prompt_Printf(arg->prompt, "%ld\n", arg->bundle->ncp.ipcp.cfg.sendpipe);
12853afe5ccbSBrian Somers   else
12863afe5ccbSBrian Somers     prompt_Printf(arg->prompt, "unspecified\n");
12873afe5ccbSBrian Somers   prompt_Printf(arg->prompt, " recvpipe:      ");
12883afe5ccbSBrian Somers   if (arg->bundle->ncp.ipcp.cfg.recvpipe > 0)
12893afe5ccbSBrian Somers     prompt_Printf(arg->prompt, "%ld\n", arg->bundle->ncp.ipcp.cfg.recvpipe);
12903afe5ccbSBrian Somers   else
12913afe5ccbSBrian Somers     prompt_Printf(arg->prompt, "unspecified\n");
12923afe5ccbSBrian Somers 
1293610b185fSBrian Somers   prompt_Printf(arg->prompt, " Sticky Routes: %s\n",
1294610b185fSBrian Somers                 optval(arg->bundle, OPT_SROUTES));
12951342caedSBrian Somers   prompt_Printf(arg->prompt, " ID check:      %s\n",
12961342caedSBrian Somers                 optval(arg->bundle, OPT_IDCHECK));
12971342caedSBrian Somers   prompt_Printf(arg->prompt, " Loopback:      %s\n",
12981342caedSBrian Somers                 optval(arg->bundle, OPT_LOOPBACK));
12991342caedSBrian Somers   prompt_Printf(arg->prompt, " PasswdAuth:    %s\n",
13001342caedSBrian Somers                 optval(arg->bundle, OPT_PASSWDAUTH));
13011342caedSBrian Somers   prompt_Printf(arg->prompt, " Proxy:         %s\n",
13021342caedSBrian Somers                 optval(arg->bundle, OPT_PROXY));
13033afe5ccbSBrian Somers   prompt_Printf(arg->prompt, " Proxyall:      %s\n",
13043afe5ccbSBrian Somers                 optval(arg->bundle, OPT_PROXYALL));
13051342caedSBrian Somers   prompt_Printf(arg->prompt, " Throughput:    %s\n",
13061342caedSBrian Somers                 optval(arg->bundle, OPT_THROUGHPUT));
1307610b185fSBrian Somers   prompt_Printf(arg->prompt, " Utmp Logging:  %s\n",
13081342caedSBrian Somers                 optval(arg->bundle, OPT_UTMP));
13098fa6ebe4SBrian Somers   prompt_Printf(arg->prompt, " Iface-Alias:   %s\n",
13108fa6ebe4SBrian Somers                 optval(arg->bundle, OPT_IFACEALIAS));
13111342caedSBrian Somers 
1312c08717dfSBrian Somers   return 0;
1313c08717dfSBrian Somers }
1314c08717dfSBrian Somers 
1315ab886ad0SBrian Somers static void
1316ab886ad0SBrian Somers bundle_IdleTimeout(void *v)
1317ab886ad0SBrian Somers {
1318ab886ad0SBrian Somers   struct bundle *bundle = (struct bundle *)v;
1319ab886ad0SBrian Somers 
1320dd7e2610SBrian Somers   log_Printf(LogPHASE, "Idle timer expired.\n");
132104eaa58cSBrian Somers   bundle_StopIdleTimer(bundle);
13229c81b87dSBrian Somers   bundle_Close(bundle, NULL, CLOSE_STAYDOWN);
1323ab886ad0SBrian Somers }
1324ab886ad0SBrian Somers 
1325ab886ad0SBrian Somers /*
1326ab886ad0SBrian Somers  *  Start Idle timer. If timeout is reached, we call bundle_Close() to
1327ab886ad0SBrian Somers  *  close LCP and link.
1328ab886ad0SBrian Somers  */
1329ab886ad0SBrian Somers void
1330ab886ad0SBrian Somers bundle_StartIdleTimer(struct bundle *bundle)
1331ab886ad0SBrian Somers {
1332dd7e2610SBrian Somers   timer_Stop(&bundle->idle.timer);
1333ff0f9439SBrian Somers   if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) !=
1334ff0f9439SBrian Somers       bundle->phys_type.open && bundle->cfg.idle_timeout) {
133593ee0ff2SBrian Somers     bundle->idle.timer.func = bundle_IdleTimeout;
13363b0f8d2eSBrian Somers     bundle->idle.timer.name = "idle";
133793ee0ff2SBrian Somers     bundle->idle.timer.load = bundle->cfg.idle_timeout * SECTICKS;
133893ee0ff2SBrian Somers     bundle->idle.timer.arg = bundle;
1339dd7e2610SBrian Somers     timer_Start(&bundle->idle.timer);
134093ee0ff2SBrian Somers     bundle->idle.done = time(NULL) + bundle->cfg.idle_timeout;
1341ab886ad0SBrian Somers   }
1342ab886ad0SBrian Somers }
1343ab886ad0SBrian Somers 
1344ab886ad0SBrian Somers void
1345ab886ad0SBrian Somers bundle_SetIdleTimer(struct bundle *bundle, int value)
1346ab886ad0SBrian Somers {
1347ab886ad0SBrian Somers   bundle->cfg.idle_timeout = value;
1348ab886ad0SBrian Somers   if (bundle_LinkIsUp(bundle))
1349ab886ad0SBrian Somers     bundle_StartIdleTimer(bundle);
1350ab886ad0SBrian Somers }
1351ab886ad0SBrian Somers 
1352ab886ad0SBrian Somers void
1353ab886ad0SBrian Somers bundle_StopIdleTimer(struct bundle *bundle)
1354ab886ad0SBrian Somers {
1355dd7e2610SBrian Somers   timer_Stop(&bundle->idle.timer);
13564a632c80SBrian Somers   bundle->idle.done = 0;
1357ab886ad0SBrian Somers }
1358ab886ad0SBrian Somers 
135904eaa58cSBrian Somers static int
1360ab886ad0SBrian Somers bundle_RemainingIdleTime(struct bundle *bundle)
1361ab886ad0SBrian Somers {
136293ee0ff2SBrian Somers   if (bundle->idle.done)
136393ee0ff2SBrian Somers     return bundle->idle.done - time(NULL);
1364ab886ad0SBrian Somers   return -1;
1365ab886ad0SBrian Somers }
13663b0f8d2eSBrian Somers 
13673b0f8d2eSBrian Somers int
13683b0f8d2eSBrian Somers bundle_IsDead(struct bundle *bundle)
13693b0f8d2eSBrian Somers {
13703b0f8d2eSBrian Somers   return !bundle->links || (bundle->phase == PHASE_DEAD && bundle->CleaningUp);
13713b0f8d2eSBrian Somers }
1372b6217683SBrian Somers 
137304eaa58cSBrian Somers static struct datalink *
137404eaa58cSBrian Somers bundle_DatalinkLinkout(struct bundle *bundle, struct datalink *dl)
1375cd7bd93aSBrian Somers {
1376cd7bd93aSBrian Somers   struct datalink **dlp;
1377cd7bd93aSBrian Somers 
1378cd7bd93aSBrian Somers   for (dlp = &bundle->links; *dlp; dlp = &(*dlp)->next)
1379cd7bd93aSBrian Somers     if (*dlp == dl) {
138004eaa58cSBrian Somers       *dlp = dl->next;
138104eaa58cSBrian Somers       dl->next = NULL;
138204eaa58cSBrian Somers       bundle_LinksRemoved(bundle);
138304eaa58cSBrian Somers       return dl;
1384cd7bd93aSBrian Somers     }
138504eaa58cSBrian Somers 
138604eaa58cSBrian Somers   return NULL;
138704eaa58cSBrian Somers }
138804eaa58cSBrian Somers 
138904eaa58cSBrian Somers static void
139004eaa58cSBrian Somers bundle_DatalinkLinkin(struct bundle *bundle, struct datalink *dl)
139104eaa58cSBrian Somers {
139204eaa58cSBrian Somers   struct datalink **dlp = &bundle->links;
139304eaa58cSBrian Somers 
139404eaa58cSBrian Somers   while (*dlp)
139504eaa58cSBrian Somers     dlp = &(*dlp)->next;
139604eaa58cSBrian Somers 
139704eaa58cSBrian Somers   *dlp = dl;
139804eaa58cSBrian Somers   dl->next = NULL;
139904eaa58cSBrian Somers 
140004eaa58cSBrian Somers   bundle_LinkAdded(bundle, dl);
1401565e35e5SBrian Somers }
1402565e35e5SBrian Somers 
1403565e35e5SBrian Somers void
1404565e35e5SBrian Somers bundle_CleanDatalinks(struct bundle *bundle)
1405565e35e5SBrian Somers {
1406565e35e5SBrian Somers   struct datalink **dlp = &bundle->links;
140704eaa58cSBrian Somers   int found = 0;
1408565e35e5SBrian Somers 
1409565e35e5SBrian Somers   while (*dlp)
1410565e35e5SBrian Somers     if ((*dlp)->state == DATALINK_CLOSED &&
141181358fa3SBrian Somers         (*dlp)->physical->type & (PHYS_DIRECT|PHYS_BACKGROUND)) {
1412565e35e5SBrian Somers       *dlp = datalink_Destroy(*dlp);
141304eaa58cSBrian Somers       found++;
141404eaa58cSBrian Somers     } else
1415565e35e5SBrian Somers       dlp = &(*dlp)->next;
141604eaa58cSBrian Somers 
141704eaa58cSBrian Somers   if (found)
141804eaa58cSBrian Somers     bundle_LinksRemoved(bundle);
141904eaa58cSBrian Somers }
142004eaa58cSBrian Somers 
142104eaa58cSBrian Somers int
142204eaa58cSBrian Somers bundle_DatalinkClone(struct bundle *bundle, struct datalink *dl,
142304eaa58cSBrian Somers                      const char *name)
142404eaa58cSBrian Somers {
142504eaa58cSBrian Somers   if (bundle2datalink(bundle, name)) {
142604eaa58cSBrian Somers     log_Printf(LogWARN, "Clone: %s: name already exists\n", name);
142704eaa58cSBrian Somers     return 0;
142804eaa58cSBrian Somers   }
142904eaa58cSBrian Somers 
143004eaa58cSBrian Somers   bundle_DatalinkLinkin(bundle, datalink_Clone(dl, name));
143104eaa58cSBrian Somers   return 1;
143204eaa58cSBrian Somers }
143304eaa58cSBrian Somers 
143404eaa58cSBrian Somers void
143504eaa58cSBrian Somers bundle_DatalinkRemove(struct bundle *bundle, struct datalink *dl)
143604eaa58cSBrian Somers {
143704eaa58cSBrian Somers   dl = bundle_DatalinkLinkout(bundle, dl);
143804eaa58cSBrian Somers   if (dl)
143904eaa58cSBrian Somers     datalink_Destroy(dl);
1440cd7bd93aSBrian Somers }
144149052c95SBrian Somers 
144249052c95SBrian Somers void
144349052c95SBrian Somers bundle_SetLabel(struct bundle *bundle, const char *label)
144449052c95SBrian Somers {
144549052c95SBrian Somers   if (label)
144649052c95SBrian Somers     strncpy(bundle->cfg.label, label, sizeof bundle->cfg.label - 1);
144749052c95SBrian Somers   else
144849052c95SBrian Somers     *bundle->cfg.label = '\0';
144949052c95SBrian Somers }
145049052c95SBrian Somers 
145149052c95SBrian Somers const char *
145249052c95SBrian Somers bundle_GetLabel(struct bundle *bundle)
145349052c95SBrian Somers {
145449052c95SBrian Somers   return *bundle->cfg.label ? bundle->cfg.label : NULL;
145549052c95SBrian Somers }
14561fa665f5SBrian Somers 
14571fa665f5SBrian Somers void
145896c9bb21SBrian Somers bundle_ReceiveDatalink(struct bundle *bundle, int s, struct sockaddr_un *sun)
14591fa665f5SBrian Somers {
146096c9bb21SBrian Somers   char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int)];
146196c9bb21SBrian Somers   struct cmsghdr *cmsg = (struct cmsghdr *)cmsgbuf;
146296c9bb21SBrian Somers   struct msghdr msg;
146396c9bb21SBrian Somers   struct iovec iov[SCATTER_SEGMENTS];
1464b7c5748eSBrian Somers   struct datalink *dl;
146596c9bb21SBrian Somers   int niov, link_fd, expect, f;
146685fd273aSBrian Somers   pid_t pid;
14676f384573SBrian Somers 
1468dd7e2610SBrian Somers   log_Printf(LogPHASE, "Receiving datalink\n");
14696f384573SBrian Somers 
147096c9bb21SBrian Somers   /* Create our scatter/gather array */
147196c9bb21SBrian Somers   niov = 1;
147296c9bb21SBrian Somers   iov[0].iov_len = strlen(Version) + 1;
147396c9bb21SBrian Somers   iov[0].iov_base = (char *)malloc(iov[0].iov_len);
147485fd273aSBrian Somers   if (datalink2iov(NULL, iov, &niov, sizeof iov / sizeof *iov, 0) == -1) {
147554cd8e13SBrian Somers     close(s);
14766f384573SBrian Somers     return;
147754cd8e13SBrian Somers   }
14786f384573SBrian Somers 
147985fd273aSBrian Somers   pid = getpid();
148085fd273aSBrian Somers   write(s, &pid, sizeof pid);
148185fd273aSBrian Somers 
148296c9bb21SBrian Somers   for (f = expect = 0; f < niov; f++)
148396c9bb21SBrian Somers     expect += iov[f].iov_len;
148496c9bb21SBrian Somers 
148596c9bb21SBrian Somers   /* Set up our message */
148696c9bb21SBrian Somers   cmsg->cmsg_len = sizeof cmsgbuf;
148796c9bb21SBrian Somers   cmsg->cmsg_level = SOL_SOCKET;
148854cd8e13SBrian Somers   cmsg->cmsg_type = 0;
148996c9bb21SBrian Somers 
149096c9bb21SBrian Somers   memset(&msg, '\0', sizeof msg);
149196c9bb21SBrian Somers   msg.msg_name = (caddr_t)sun;
149296c9bb21SBrian Somers   msg.msg_namelen = sizeof *sun;
149396c9bb21SBrian Somers   msg.msg_iov = iov;
149496c9bb21SBrian Somers   msg.msg_iovlen = niov;
149596c9bb21SBrian Somers   msg.msg_control = cmsgbuf;
149696c9bb21SBrian Somers   msg.msg_controllen = sizeof cmsgbuf;
149796c9bb21SBrian Somers 
149896c9bb21SBrian Somers   log_Printf(LogDEBUG, "Expecting %d scatter/gather bytes\n", expect);
149996c9bb21SBrian Somers   f = expect + 100;
150096c9bb21SBrian Somers   setsockopt(s, SOL_SOCKET, SO_RCVBUF, &f, sizeof f);
150196c9bb21SBrian Somers   if ((f = recvmsg(s, &msg, MSG_WAITALL)) != expect) {
150296c9bb21SBrian Somers     if (f == -1)
150396c9bb21SBrian Somers       log_Printf(LogERROR, "Failed recvmsg: %s\n", strerror(errno));
150496c9bb21SBrian Somers     else
150596c9bb21SBrian Somers       log_Printf(LogERROR, "Failed recvmsg: Got %d, not %d\n", f, expect);
150696c9bb21SBrian Somers     while (niov--)
150796c9bb21SBrian Somers       free(iov[niov].iov_base);
150854cd8e13SBrian Somers     close(s);
150996c9bb21SBrian Somers     return;
151096c9bb21SBrian Somers   }
151196c9bb21SBrian Somers 
1512c0d9a877SBrian Somers   write(s, "!", 1);	/* ACK */
1513ad5b0e8bSBrian Somers   close(s);
1514c0d9a877SBrian Somers 
1515ad5b0e8bSBrian Somers   if (cmsg->cmsg_type != SCM_RIGHTS) {
1516ad5b0e8bSBrian Somers     log_Printf(LogERROR, "Recvmsg: no descriptor received !\n");
1517ad5b0e8bSBrian Somers     while (niov--)
1518ad5b0e8bSBrian Somers       free(iov[niov].iov_base);
1519ad5b0e8bSBrian Somers     return;
152054cd8e13SBrian Somers   }
152154cd8e13SBrian Somers 
1522ad5b0e8bSBrian Somers   /* We've successfully received an open file descriptor through our socket */
1523ad5b0e8bSBrian Somers   log_Printf(LogDEBUG, "Receiving device descriptor\n");
1524ad5b0e8bSBrian Somers   link_fd = *(int *)CMSG_DATA(cmsg);
1525ad5b0e8bSBrian Somers 
152696c9bb21SBrian Somers   if (strncmp(Version, iov[0].iov_base, iov[0].iov_len)) {
152796c9bb21SBrian Somers     log_Printf(LogWARN, "Cannot receive datalink, incorrect version"
152896c9bb21SBrian Somers                " (\"%.*s\", not \"%s\")\n", (int)iov[0].iov_len,
15297d81ddf5SBrian Somers                (char *)iov[0].iov_base, Version);
153096c9bb21SBrian Somers     close(link_fd);
153196c9bb21SBrian Somers     while (niov--)
153296c9bb21SBrian Somers       free(iov[niov].iov_base);
153396c9bb21SBrian Somers     return;
153496c9bb21SBrian Somers   }
153596c9bb21SBrian Somers 
153696c9bb21SBrian Somers   niov = 1;
1537b7c5748eSBrian Somers   dl = iov2datalink(bundle, iov, &niov, sizeof iov / sizeof *iov, link_fd);
1538b7c5748eSBrian Somers   if (dl) {
153904eaa58cSBrian Somers     bundle_DatalinkLinkin(bundle, dl);
1540b7c5748eSBrian Somers     datalink_AuthOk(dl);
154196c9bb21SBrian Somers   } else
154296c9bb21SBrian Somers     close(link_fd);
154396c9bb21SBrian Somers 
154496c9bb21SBrian Somers   free(iov[0].iov_base);
15451fa665f5SBrian Somers }
15461fa665f5SBrian Somers 
15471fa665f5SBrian Somers void
154896c9bb21SBrian Somers bundle_SendDatalink(struct datalink *dl, int s, struct sockaddr_un *sun)
15491fa665f5SBrian Somers {
1550c0d9a877SBrian Somers   char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int)], ack;
155196c9bb21SBrian Somers   struct cmsghdr *cmsg = (struct cmsghdr *)cmsgbuf;
155296c9bb21SBrian Somers   struct msghdr msg;
155396c9bb21SBrian Somers   struct iovec iov[SCATTER_SEGMENTS];
15541384bd27SBrian Somers   int niov, link_fd, f, expect, newsid;
155585fd273aSBrian Somers   pid_t newpid;
15566f384573SBrian Somers 
1557dd7e2610SBrian Somers   log_Printf(LogPHASE, "Transmitting datalink %s\n", dl->name);
15586f384573SBrian Somers 
155904eaa58cSBrian Somers   bundle_LinkClosed(dl->bundle, dl);
15600f2f3eb3SBrian Somers   bundle_DatalinkLinkout(dl->bundle, dl);
1561ea722969SBrian Somers 
156296c9bb21SBrian Somers   /* Build our scatter/gather array */
156396c9bb21SBrian Somers   iov[0].iov_len = strlen(Version) + 1;
156496c9bb21SBrian Somers   iov[0].iov_base = strdup(Version);
156596c9bb21SBrian Somers   niov = 1;
15666f384573SBrian Somers 
156785fd273aSBrian Somers   read(s, &newpid, sizeof newpid);
156885fd273aSBrian Somers   link_fd = datalink2iov(dl, iov, &niov, sizeof iov / sizeof *iov, newpid);
15696f384573SBrian Somers 
15706f384573SBrian Somers   if (link_fd != -1) {
157196c9bb21SBrian Somers     memset(&msg, '\0', sizeof msg);
157254cd8e13SBrian Somers 
157396c9bb21SBrian Somers     msg.msg_name = (caddr_t)sun;
157496c9bb21SBrian Somers     msg.msg_namelen = sizeof *sun;
157596c9bb21SBrian Somers     msg.msg_iov = iov;
157696c9bb21SBrian Somers     msg.msg_iovlen = niov;
157754cd8e13SBrian Somers 
157854cd8e13SBrian Somers     cmsg->cmsg_len = sizeof cmsgbuf;
157954cd8e13SBrian Somers     cmsg->cmsg_level = SOL_SOCKET;
158054cd8e13SBrian Somers     cmsg->cmsg_type = SCM_RIGHTS;
158154cd8e13SBrian Somers     *(int *)CMSG_DATA(cmsg) = link_fd;
158296c9bb21SBrian Somers     msg.msg_control = cmsgbuf;
158396c9bb21SBrian Somers     msg.msg_controllen = sizeof cmsgbuf;
158447723d29SBrian Somers 
158596c9bb21SBrian Somers     for (f = expect = 0; f < niov; f++)
158696c9bb21SBrian Somers       expect += iov[f].iov_len;
158747723d29SBrian Somers 
158896c9bb21SBrian Somers     log_Printf(LogDEBUG, "Sending %d bytes in scatter/gather array\n", expect);
158947723d29SBrian Somers 
159096c9bb21SBrian Somers     f = expect + SOCKET_OVERHEAD;
159196c9bb21SBrian Somers     setsockopt(s, SOL_SOCKET, SO_SNDBUF, &f, sizeof f);
159296c9bb21SBrian Somers     if (sendmsg(s, &msg, 0) == -1)
159396c9bb21SBrian Somers       log_Printf(LogERROR, "Failed sendmsg: %s\n", strerror(errno));
1594c0d9a877SBrian Somers     /* We must get the ACK before closing the descriptor ! */
1595c0d9a877SBrian Somers     read(s, &ack, 1);
159654cd8e13SBrian Somers 
15971384bd27SBrian Somers     newsid = tcgetpgrp(link_fd) == getpgrp();
159896c9bb21SBrian Somers     close(link_fd);
15991384bd27SBrian Somers     if (newsid)
16001384bd27SBrian Somers       bundle_setsid(dl->bundle, 1);
160147723d29SBrian Somers   }
160285fd273aSBrian Somers   close(s);
160396c9bb21SBrian Somers 
160496c9bb21SBrian Somers   while (niov--)
160596c9bb21SBrian Somers     free(iov[niov].iov_base);
16061fa665f5SBrian Somers }
1607dd0645c5SBrian Somers 
1608dd0645c5SBrian Somers int
160958d55334SBrian Somers bundle_RenameDatalink(struct bundle *bundle, struct datalink *ndl,
161058d55334SBrian Somers                       const char *name)
161158d55334SBrian Somers {
161258d55334SBrian Somers   struct datalink *dl;
161358d55334SBrian Somers 
161458d55334SBrian Somers   if (!strcasecmp(ndl->name, name))
161558d55334SBrian Somers     return 1;
161658d55334SBrian Somers 
161758d55334SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
161858d55334SBrian Somers     if (!strcasecmp(dl->name, name))
161958d55334SBrian Somers       return 0;
162058d55334SBrian Somers 
162158d55334SBrian Somers   datalink_Rename(ndl, name);
162258d55334SBrian Somers   return 1;
162358d55334SBrian Somers }
162458d55334SBrian Somers 
162558d55334SBrian Somers int
1626dd0645c5SBrian Somers bundle_SetMode(struct bundle *bundle, struct datalink *dl, int mode)
1627dd0645c5SBrian Somers {
1628dd0645c5SBrian Somers   int omode;
1629dd0645c5SBrian Somers 
1630dd0645c5SBrian Somers   omode = dl->physical->type;
1631dd0645c5SBrian Somers   if (omode == mode)
1632dd0645c5SBrian Somers     return 1;
1633dd0645c5SBrian Somers 
1634ff0f9439SBrian Somers   if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO))
1635ff0f9439SBrian Somers     /* First auto link */
1636dd0645c5SBrian Somers     if (bundle->ncp.ipcp.peer_ip.s_addr == INADDR_ANY) {
1637ff0f9439SBrian Somers       log_Printf(LogWARN, "You must `set ifaddr' or `open' before"
1638ff0f9439SBrian Somers                  " changing mode to %s\n", mode2Nam(mode));
1639dd0645c5SBrian Somers       return 0;
1640dd0645c5SBrian Somers     }
1641dd0645c5SBrian Somers 
1642dd0645c5SBrian Somers   if (!datalink_SetMode(dl, mode))
1643dd0645c5SBrian Somers     return 0;
1644dd0645c5SBrian Somers 
1645ff0f9439SBrian Somers   if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO) &&
1646ff0f9439SBrian Somers       bundle->phase != PHASE_NETWORK)
1647ff0f9439SBrian Somers     /* First auto link, we need an interface */
1648dd0645c5SBrian Somers     ipcp_InterfaceUp(&bundle->ncp.ipcp);
1649dd0645c5SBrian Somers 
165004eaa58cSBrian Somers   /* Regenerate phys_type and adjust autoload & idle timers */
165104eaa58cSBrian Somers   bundle_LinksRemoved(bundle);
1652dd0645c5SBrian Somers 
1653dd0645c5SBrian Somers   return 1;
1654dd0645c5SBrian Somers }
16551384bd27SBrian Somers 
16561384bd27SBrian Somers void
16571384bd27SBrian Somers bundle_setsid(struct bundle *bundle, int holdsession)
16581384bd27SBrian Somers {
16591384bd27SBrian Somers   /*
16601384bd27SBrian Somers    * Lose the current session.  This means getting rid of our pid
16611384bd27SBrian Somers    * too so that the tty device will really go away, and any getty
16621384bd27SBrian Somers    * etc will be allowed to restart.
16631384bd27SBrian Somers    */
16641384bd27SBrian Somers   pid_t pid, orig;
16651384bd27SBrian Somers   int fds[2];
16661384bd27SBrian Somers   char done;
16671384bd27SBrian Somers   struct datalink *dl;
16681384bd27SBrian Somers 
16691384bd27SBrian Somers   orig = getpid();
16701384bd27SBrian Somers   if (pipe(fds) == -1) {
16711384bd27SBrian Somers     log_Printf(LogERROR, "pipe: %s\n", strerror(errno));
16721384bd27SBrian Somers     return;
16731384bd27SBrian Somers   }
16741384bd27SBrian Somers   switch ((pid = fork())) {
16751384bd27SBrian Somers     case -1:
16761384bd27SBrian Somers       log_Printf(LogERROR, "fork: %s\n", strerror(errno));
16771384bd27SBrian Somers       close(fds[0]);
16781384bd27SBrian Somers       close(fds[1]);
16791384bd27SBrian Somers       return;
16801384bd27SBrian Somers     case 0:
16811384bd27SBrian Somers       close(fds[1]);
16824be0e57dSBrian Somers       read(fds[0], &done, 1);		/* uu_locks are mine ! */
16834be0e57dSBrian Somers       close(fds[0]);
16841384bd27SBrian Somers       if (pipe(fds) == -1) {
16851384bd27SBrian Somers         log_Printf(LogERROR, "pipe(2): %s\n", strerror(errno));
16861384bd27SBrian Somers         return;
16871384bd27SBrian Somers       }
16881384bd27SBrian Somers       switch ((pid = fork())) {
16891384bd27SBrian Somers         case -1:
1690a33b2ef7SBrian Somers           log_Printf(LogERROR, "fork(2): %s\n", strerror(errno));
16911384bd27SBrian Somers           close(fds[0]);
16921384bd27SBrian Somers           close(fds[1]);
16931384bd27SBrian Somers           return;
16941384bd27SBrian Somers         case 0:
16951384bd27SBrian Somers           close(fds[1]);
16964be0e57dSBrian Somers           bundle_LockTun(bundle);	/* update pid */
16974be0e57dSBrian Somers           read(fds[0], &done, 1);	/* uu_locks are mine ! */
16984be0e57dSBrian Somers           close(fds[0]);
16991384bd27SBrian Somers           setsid();
17001384bd27SBrian Somers           log_Printf(LogPHASE, "%d -> %d: %s session control\n",
17011384bd27SBrian Somers                      (int)orig, (int)getpid(),
17021384bd27SBrian Somers                      holdsession ? "Passed" : "Dropped");
17037e778f13SBrian Somers           timer_InitService(0);		/* Start the Timer Service */
17041384bd27SBrian Somers           break;
17051384bd27SBrian Somers         default:
17064be0e57dSBrian Somers           close(fds[0]);
17071384bd27SBrian Somers           /* Give away all our modem locks (to the final process) */
17081384bd27SBrian Somers           for (dl = bundle->links; dl; dl = dl->next)
17091384bd27SBrian Somers             if (dl->state != DATALINK_CLOSED)
17101384bd27SBrian Somers               modem_ChangedPid(dl->physical, pid);
17114be0e57dSBrian Somers           write(fds[1], "!", 1);	/* done */
17124be0e57dSBrian Somers           close(fds[1]);
17131384bd27SBrian Somers           exit(0);
17141384bd27SBrian Somers           break;
17151384bd27SBrian Somers       }
17161384bd27SBrian Somers       break;
17171384bd27SBrian Somers     default:
17184be0e57dSBrian Somers       close(fds[0]);
17191384bd27SBrian Somers       /* Give away all our modem locks (to the intermediate process) */
17201384bd27SBrian Somers       for (dl = bundle->links; dl; dl = dl->next)
17211384bd27SBrian Somers         if (dl->state != DATALINK_CLOSED)
17221384bd27SBrian Somers           modem_ChangedPid(dl->physical, pid);
17234be0e57dSBrian Somers       write(fds[1], "!", 1);	/* done */
17244be0e57dSBrian Somers       close(fds[1]);
17251384bd27SBrian Somers       if (holdsession) {
17261384bd27SBrian Somers         int fd, status;
17271384bd27SBrian Somers 
17281384bd27SBrian Somers         timer_TermService();
17291384bd27SBrian Somers         signal(SIGPIPE, SIG_DFL);
17301384bd27SBrian Somers         signal(SIGALRM, SIG_DFL);
17311384bd27SBrian Somers         signal(SIGHUP, SIG_DFL);
17321384bd27SBrian Somers         signal(SIGTERM, SIG_DFL);
17331384bd27SBrian Somers         signal(SIGINT, SIG_DFL);
17341384bd27SBrian Somers         signal(SIGQUIT, SIG_DFL);
17351384bd27SBrian Somers         for (fd = getdtablesize(); fd >= 0; fd--)
17361384bd27SBrian Somers           close(fd);
17371384bd27SBrian Somers         setuid(geteuid());
17381384bd27SBrian Somers         /*
17391384bd27SBrian Somers          * Reap the intermediate process.  As we're not exiting but the
17401384bd27SBrian Somers          * intermediate is, we don't want it to become defunct.
17411384bd27SBrian Somers          */
17421384bd27SBrian Somers         waitpid(pid, &status, 0);
17438e7b8599SBrian Somers         /* Tweak our process arguments.... */
17448e7b8599SBrian Somers         bundle->argv[0] = "session owner";
17458e7b8599SBrian Somers         bundle->argv[1] = NULL;
17461384bd27SBrian Somers         /*
17471384bd27SBrian Somers          * Hang around for a HUP.  This should happen as soon as the
17481384bd27SBrian Somers          * ppp that we passed our ctty descriptor to closes it.
17491384bd27SBrian Somers          * NOTE: If this process dies, the passed descriptor becomes
17501384bd27SBrian Somers          *       invalid and will give a select() error by setting one
17511384bd27SBrian Somers          *       of the error fds, aborting the other ppp.  We don't
17521384bd27SBrian Somers          *       want that to happen !
17531384bd27SBrian Somers          */
17541384bd27SBrian Somers         pause();
17551384bd27SBrian Somers       }
17561384bd27SBrian Somers       exit(0);
17571384bd27SBrian Somers       break;
17581384bd27SBrian Somers   }
17591384bd27SBrian Somers }
17609b5f8ffdSBrian Somers 
17619b5f8ffdSBrian Somers int
17629b5f8ffdSBrian Somers bundle_HighestState(struct bundle *bundle)
17639b5f8ffdSBrian Somers {
17649b5f8ffdSBrian Somers   struct datalink *dl;
17659b5f8ffdSBrian Somers   int result = DATALINK_CLOSED;
17669b5f8ffdSBrian Somers 
17679b5f8ffdSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
17689b5f8ffdSBrian Somers     if (result < dl->state)
17699b5f8ffdSBrian Somers       result = dl->state;
17709b5f8ffdSBrian Somers 
17719b5f8ffdSBrian Somers   return result;
17729b5f8ffdSBrian Somers }
1773991c2a7bSBrian Somers 
1774991c2a7bSBrian Somers int
1775991c2a7bSBrian Somers bundle_Exception(struct bundle *bundle, int fd)
1776991c2a7bSBrian Somers {
1777991c2a7bSBrian Somers   struct datalink *dl;
1778991c2a7bSBrian Somers 
1779991c2a7bSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
1780991c2a7bSBrian Somers     if (dl->physical->fd == fd) {
1781991c2a7bSBrian Somers       datalink_Down(dl, CLOSE_NORMAL);
1782991c2a7bSBrian Somers       return 1;
1783991c2a7bSBrian Somers     }
1784991c2a7bSBrian Somers 
1785991c2a7bSBrian Somers   return 0;
1786991c2a7bSBrian Somers }
1787