xref: /freebsd/usr.sbin/ppp/bundle.c (revision 9c81b87d1e56d3a7bbb680c3d2dab59d88d29481)
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  *
269c81b87dSBrian Somers  *	$Id: bundle.c,v 1.15 1998/06/12 17:45:03 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>
337a6f8720SBrian Somers #include <arpa/inet.h>
347a6f8720SBrian Somers #include <net/route.h>
357a6f8720SBrian Somers #include <net/if_dl.h>
36eaa4df37SBrian Somers #include <netinet/in_systm.h>
37eaa4df37SBrian Somers #include <netinet/ip.h>
38e43ebac1SBrian Somers #include <net/if_tun.h>
391fa665f5SBrian Somers #include <sys/un.h>
407a6f8720SBrian Somers 
417a6f8720SBrian Somers #include <errno.h>
427a6f8720SBrian Somers #include <fcntl.h>
4347723d29SBrian Somers #include <paths.h>
4454cd8e13SBrian Somers #include <signal.h>
457a6f8720SBrian Somers #include <stdio.h>
466f384573SBrian Somers #include <stdlib.h>
477a6f8720SBrian Somers #include <string.h>
487a6f8720SBrian Somers #include <sys/ioctl.h>
4996c9bb21SBrian Somers #include <sys/uio.h>
5054cd8e13SBrian Somers #include <sys/wait.h>
517a6f8720SBrian Somers #include <termios.h>
527a6f8720SBrian Somers #include <unistd.h>
537a6f8720SBrian Somers 
547a6f8720SBrian Somers #include "command.h"
557a6f8720SBrian Somers #include "mbuf.h"
567a6f8720SBrian Somers #include "log.h"
577a6f8720SBrian Somers #include "id.h"
587a6f8720SBrian Somers #include "defs.h"
597a6f8720SBrian Somers #include "timer.h"
607a6f8720SBrian Somers #include "fsm.h"
617a6f8720SBrian Somers #include "iplist.h"
62879ed6faSBrian Somers #include "lqr.h"
63455aabc3SBrian Somers #include "hdlc.h"
647a6f8720SBrian Somers #include "throughput.h"
65eaa4df37SBrian Somers #include "slcompress.h"
667a6f8720SBrian Somers #include "ipcp.h"
675ca5389aSBrian Somers #include "filter.h"
682f786681SBrian Somers #include "descriptor.h"
697a6f8720SBrian Somers #include "route.h"
707a6f8720SBrian Somers #include "lcp.h"
717a6f8720SBrian Somers #include "ccp.h"
723b0f8d2eSBrian Somers #include "link.h"
733b0f8d2eSBrian Somers #include "mp.h"
743b0f8d2eSBrian Somers #include "bundle.h"
75455aabc3SBrian Somers #include "async.h"
76455aabc3SBrian Somers #include "physical.h"
772289f246SBrian Somers #include "modem.h"
78078c562eSBrian Somers #include "loadalias.h"
79455aabc3SBrian Somers #include "auth.h"
80455aabc3SBrian Somers #include "lcpproto.h"
81455aabc3SBrian Somers #include "chap.h"
82455aabc3SBrian Somers #include "tun.h"
8385b542cfSBrian Somers #include "prompt.h"
843006ec67SBrian Somers #include "chat.h"
853006ec67SBrian Somers #include "datalink.h"
863006ec67SBrian Somers #include "ip.h"
877a6f8720SBrian Somers 
8896c9bb21SBrian Somers #define SCATTER_SEGMENTS 4	/* version, datalink, name, physical */
8996c9bb21SBrian Somers #define SOCKET_OVERHEAD	100	/* additional buffer space for large */
9096c9bb21SBrian Somers                                 /* {recv,send}msg() calls            */
9196c9bb21SBrian Somers 
9204eaa58cSBrian Somers static int bundle_RemainingIdleTime(struct bundle *);
9304eaa58cSBrian Somers static int bundle_RemainingAutoLoadTime(struct bundle *);
9404eaa58cSBrian Somers 
95455aabc3SBrian Somers static const char *PhaseNames[] = {
96455aabc3SBrian Somers   "Dead", "Establish", "Authenticate", "Network", "Terminate"
97455aabc3SBrian Somers };
98455aabc3SBrian Somers 
99455aabc3SBrian Somers const char *
100455aabc3SBrian Somers bundle_PhaseName(struct bundle *bundle)
1017a6f8720SBrian Somers {
102455aabc3SBrian Somers   return bundle->phase <= PHASE_TERMINATE ?
103455aabc3SBrian Somers     PhaseNames[bundle->phase] : "unknown";
1047a6f8720SBrian Somers }
1057a6f8720SBrian Somers 
106455aabc3SBrian Somers void
1075563ebdeSBrian Somers bundle_NewPhase(struct bundle *bundle, u_int new)
108455aabc3SBrian Somers {
109aef795ccSBrian Somers   if (new == bundle->phase)
110aef795ccSBrian Somers     return;
111aef795ccSBrian Somers 
112e2ebb036SBrian Somers   if (new <= PHASE_TERMINATE)
113dd7e2610SBrian Somers     log_Printf(LogPHASE, "bundle: %s\n", PhaseNames[new]);
1147a6f8720SBrian Somers 
115455aabc3SBrian Somers   switch (new) {
116455aabc3SBrian Somers   case PHASE_DEAD:
1170f2f3eb3SBrian Somers     log_DisplayPrompts();
118455aabc3SBrian Somers     bundle->phase = new;
119455aabc3SBrian Somers     break;
120455aabc3SBrian Somers 
121455aabc3SBrian Somers   case PHASE_ESTABLISH:
122455aabc3SBrian Somers     bundle->phase = new;
123455aabc3SBrian Somers     break;
124455aabc3SBrian Somers 
125455aabc3SBrian Somers   case PHASE_AUTHENTICATE:
126455aabc3SBrian Somers     bundle->phase = new;
1270f2f3eb3SBrian Somers     log_DisplayPrompts();
128455aabc3SBrian Somers     break;
129455aabc3SBrian Somers 
130455aabc3SBrian Somers   case PHASE_NETWORK:
1315828db6dSBrian Somers     ipcp_Setup(&bundle->ncp.ipcp);
132dd7e2610SBrian Somers     fsm_Up(&bundle->ncp.ipcp.fsm);
133dd7e2610SBrian Somers     fsm_Open(&bundle->ncp.ipcp.fsm);
134673903ecSBrian Somers     bundle->phase = new;
1350f2f3eb3SBrian Somers     log_DisplayPrompts();
136673903ecSBrian Somers     break;
137455aabc3SBrian Somers 
138455aabc3SBrian Somers   case PHASE_TERMINATE:
139455aabc3SBrian Somers     bundle->phase = new;
140673903ecSBrian Somers     mp_Down(&bundle->ncp.mp);
1410f2f3eb3SBrian Somers     log_DisplayPrompts();
142455aabc3SBrian Somers     break;
1437a6f8720SBrian Somers   }
1447a6f8720SBrian Somers }
1457a6f8720SBrian Somers 
1467a6f8720SBrian Somers static int
1477a6f8720SBrian Somers bundle_CleanInterface(const struct bundle *bundle)
1487a6f8720SBrian Somers {
1497a6f8720SBrian Somers   int s;
1507a6f8720SBrian Somers   struct ifreq ifrq;
1517a6f8720SBrian Somers   struct ifaliasreq ifra;
1527a6f8720SBrian Somers 
1537a6f8720SBrian Somers   s = ID0socket(AF_INET, SOCK_DGRAM, 0);
1547a6f8720SBrian Somers   if (s < 0) {
155dd7e2610SBrian Somers     log_Printf(LogERROR, "bundle_CleanInterface: socket(): %s\n",
1567a6f8720SBrian Somers               strerror(errno));
1577a6f8720SBrian Somers     return (-1);
1587a6f8720SBrian Somers   }
159faefde08SBrian Somers   strncpy(ifrq.ifr_name, bundle->ifp.Name, sizeof ifrq.ifr_name - 1);
1607a6f8720SBrian Somers   ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0';
1617a6f8720SBrian Somers   while (ID0ioctl(s, SIOCGIFADDR, &ifrq) == 0) {
1627a6f8720SBrian Somers     memset(&ifra.ifra_mask, '\0', sizeof ifra.ifra_mask);
163faefde08SBrian Somers     strncpy(ifra.ifra_name, bundle->ifp.Name, sizeof ifra.ifra_name - 1);
1647a6f8720SBrian Somers     ifra.ifra_name[sizeof ifra.ifra_name - 1] = '\0';
1657a6f8720SBrian Somers     ifra.ifra_addr = ifrq.ifr_addr;
1667a6f8720SBrian Somers     if (ID0ioctl(s, SIOCGIFDSTADDR, &ifrq) < 0) {
1677a6f8720SBrian Somers       if (ifra.ifra_addr.sa_family == AF_INET)
168dd7e2610SBrian Somers         log_Printf(LogERROR,
1697a6f8720SBrian Somers                   "bundle_CleanInterface: Can't get dst for %s on %s !\n",
1707a6f8720SBrian Somers                   inet_ntoa(((struct sockaddr_in *)&ifra.ifra_addr)->sin_addr),
171faefde08SBrian Somers                   bundle->ifp.Name);
1720f8037a9SBrian Somers       close(s);
1737a6f8720SBrian Somers       return 0;
1747a6f8720SBrian Somers     }
1757a6f8720SBrian Somers     ifra.ifra_broadaddr = ifrq.ifr_dstaddr;
1767a6f8720SBrian Somers     if (ID0ioctl(s, SIOCDIFADDR, &ifra) < 0) {
1777a6f8720SBrian Somers       if (ifra.ifra_addr.sa_family == AF_INET)
178dd7e2610SBrian Somers         log_Printf(LogERROR,
1797a6f8720SBrian Somers                   "bundle_CleanInterface: Can't delete %s address on %s !\n",
1807a6f8720SBrian Somers                   inet_ntoa(((struct sockaddr_in *)&ifra.ifra_addr)->sin_addr),
181faefde08SBrian Somers                   bundle->ifp.Name);
1820f8037a9SBrian Somers       close(s);
1837a6f8720SBrian Somers       return 0;
1847a6f8720SBrian Somers     }
1857a6f8720SBrian Somers   }
1860f8037a9SBrian Somers   close(s);
1877a6f8720SBrian Somers 
1887a6f8720SBrian Somers   return 1;
1897a6f8720SBrian Somers }
1907a6f8720SBrian Somers 
1916d666775SBrian Somers static void
1926d666775SBrian Somers bundle_LayerStart(void *v, struct fsm *fp)
1937a6f8720SBrian Somers {
1943006ec67SBrian Somers   /* The given FSM is about to start up ! */
1957a6f8720SBrian Somers }
1967a6f8720SBrian Somers 
1975cf4388bSBrian Somers 
1985cf4388bSBrian Somers static void
1995cf4388bSBrian Somers bundle_Notify(struct bundle *bundle, char c)
2005cf4388bSBrian Somers {
2015cf4388bSBrian Somers   if (bundle->notify.fd != -1) {
2025cf4388bSBrian Somers     if (write(bundle->notify.fd, &c, 1) == 1)
203dd7e2610SBrian Somers       log_Printf(LogPHASE, "Parent notified of success.\n");
2045cf4388bSBrian Somers     else
205dd7e2610SBrian Somers       log_Printf(LogPHASE, "Failed to notify parent of success.\n");
2065cf4388bSBrian Somers     close(bundle->notify.fd);
2075cf4388bSBrian Somers     bundle->notify.fd = -1;
2085cf4388bSBrian Somers   }
2095cf4388bSBrian Somers }
2103b0f8d2eSBrian Somers 
2116d666775SBrian Somers static void
21204eaa58cSBrian Somers bundle_AutoLoadTimeout(void *v)
21304eaa58cSBrian Somers {
21404eaa58cSBrian Somers   struct bundle *bundle = (struct bundle *)v;
21504eaa58cSBrian Somers 
21604eaa58cSBrian Somers   if (bundle->autoload.comingup) {
21704eaa58cSBrian Somers     log_Printf(LogPHASE, "autoload: Another link is required\n");
21804eaa58cSBrian Somers     /* bundle_Open() stops the timer */
21981358fa3SBrian Somers     bundle_Open(bundle, NULL, PHYS_AUTO);
22004eaa58cSBrian Somers   } else {
22104eaa58cSBrian Somers     struct datalink *dl, *last;
22204eaa58cSBrian Somers 
22304eaa58cSBrian Somers     timer_Stop(&bundle->autoload.timer);
22404eaa58cSBrian Somers     for (last = NULL, dl = bundle->links; dl; dl = dl->next)
22581358fa3SBrian Somers       if (dl->physical->type == PHYS_AUTO && dl->state == DATALINK_OPEN)
22604eaa58cSBrian Somers         last = dl;
22704eaa58cSBrian Somers 
22804eaa58cSBrian Somers     if (last)
2299c81b87dSBrian Somers       datalink_Close(last, CLOSE_STAYDOWN);
23004eaa58cSBrian Somers   }
23104eaa58cSBrian Somers }
23204eaa58cSBrian Somers 
23304eaa58cSBrian Somers static void
23404eaa58cSBrian Somers bundle_StartAutoLoadTimer(struct bundle *bundle, int up)
23504eaa58cSBrian Somers {
23604eaa58cSBrian Somers   struct datalink *dl;
23704eaa58cSBrian Somers 
23804eaa58cSBrian Somers   timer_Stop(&bundle->autoload.timer);
23904eaa58cSBrian Somers 
24004eaa58cSBrian Somers   if (bundle->CleaningUp || bundle->phase != PHASE_NETWORK) {
24104eaa58cSBrian Somers     dl = NULL;
24204eaa58cSBrian Somers     bundle->autoload.running = 0;
24304eaa58cSBrian Somers   } else if (up) {
24404eaa58cSBrian Somers     for (dl = bundle->links; dl; dl = dl->next)
24581358fa3SBrian Somers       if (dl->state == DATALINK_CLOSED && dl->physical->type == PHYS_AUTO) {
24604eaa58cSBrian Somers         if (bundle->cfg.autoload.max.timeout) {
24704eaa58cSBrian Somers           bundle->autoload.timer.func = bundle_AutoLoadTimeout;
24804eaa58cSBrian Somers           bundle->autoload.timer.name = "autoload up";
24904eaa58cSBrian Somers           bundle->autoload.timer.load =
25004eaa58cSBrian Somers             bundle->cfg.autoload.max.timeout * SECTICKS;
25104eaa58cSBrian Somers           bundle->autoload.timer.arg = bundle;
25204eaa58cSBrian Somers           timer_Start(&bundle->autoload.timer);
25304eaa58cSBrian Somers           bundle->autoload.done = time(NULL) + bundle->cfg.autoload.max.timeout;
25404eaa58cSBrian Somers         } else
25504eaa58cSBrian Somers           bundle_AutoLoadTimeout(bundle);
25604eaa58cSBrian Somers         break;
25704eaa58cSBrian Somers       }
25804eaa58cSBrian Somers     bundle->autoload.running = (dl || bundle->cfg.autoload.min.timeout) ? 1 : 0;
25904eaa58cSBrian Somers   } else {
26004eaa58cSBrian Somers     int nlinks;
26104eaa58cSBrian Somers     struct datalink *adl;
26204eaa58cSBrian Somers 
26304eaa58cSBrian Somers     for (nlinks = 0, adl = NULL, dl = bundle->links; dl; dl = dl->next)
26404eaa58cSBrian Somers       if (dl->state == DATALINK_OPEN) {
26581358fa3SBrian Somers         if (dl->physical->type == PHYS_AUTO)
26604eaa58cSBrian Somers           adl = dl;
26704eaa58cSBrian Somers         if (++nlinks > 1 && adl) {
26804eaa58cSBrian Somers           if (bundle->cfg.autoload.min.timeout) {
26904eaa58cSBrian Somers             bundle->autoload.timer.func = bundle_AutoLoadTimeout;
27004eaa58cSBrian Somers             bundle->autoload.timer.name = "autoload down";
27104eaa58cSBrian Somers             bundle->autoload.timer.load =
27204eaa58cSBrian Somers               bundle->cfg.autoload.min.timeout * SECTICKS;
27304eaa58cSBrian Somers             bundle->autoload.timer.arg = bundle;
27404eaa58cSBrian Somers             timer_Start(&bundle->autoload.timer);
27504eaa58cSBrian Somers             bundle->autoload.done =
27604eaa58cSBrian Somers               time(NULL) + bundle->cfg.autoload.min.timeout;
27704eaa58cSBrian Somers           }
27804eaa58cSBrian Somers           break;
27904eaa58cSBrian Somers         }
28004eaa58cSBrian Somers       }
28104eaa58cSBrian Somers 
28204eaa58cSBrian Somers     bundle->autoload.running = 1;
28304eaa58cSBrian Somers   }
28404eaa58cSBrian Somers 
28504eaa58cSBrian Somers   bundle->autoload.comingup = up ? 1 : 0;
28604eaa58cSBrian Somers }
28704eaa58cSBrian Somers 
28804eaa58cSBrian Somers static void
28904eaa58cSBrian Somers bundle_StopAutoLoadTimer(struct bundle *bundle)
29004eaa58cSBrian Somers {
29104eaa58cSBrian Somers   timer_Stop(&bundle->autoload.timer);
29204eaa58cSBrian Somers   bundle->autoload.done = 0;
29304eaa58cSBrian Somers }
29404eaa58cSBrian Somers 
29504eaa58cSBrian Somers static int
29604eaa58cSBrian Somers bundle_RemainingAutoLoadTime(struct bundle *bundle)
29704eaa58cSBrian Somers {
29804eaa58cSBrian Somers   if (bundle->autoload.done)
29904eaa58cSBrian Somers     return bundle->autoload.done - time(NULL);
30004eaa58cSBrian Somers   return -1;
30104eaa58cSBrian Somers }
30204eaa58cSBrian Somers 
303ff0f9439SBrian Somers static void
304ff0f9439SBrian Somers bundle_LinkAdded(struct bundle *bundle, struct datalink *dl)
305ff0f9439SBrian Somers {
306ff0f9439SBrian Somers   bundle->phys_type.all |= dl->physical->type;
307ff0f9439SBrian Somers   if (dl->state == DATALINK_OPEN)
308ff0f9439SBrian Somers     bundle->phys_type.open |= dl->physical->type;
309ff0f9439SBrian Somers 
310ff0f9439SBrian Somers   /* Note: We only re-add links that are DATALINK_OPEN */
311ff0f9439SBrian Somers   if (dl->physical->type == PHYS_AUTO &&
312ff0f9439SBrian Somers       bundle->autoload.timer.state == TIMER_STOPPED &&
313ff0f9439SBrian Somers       dl->state != DATALINK_OPEN &&
314ff0f9439SBrian Somers       bundle->phase == PHASE_NETWORK)
315ff0f9439SBrian Somers     bundle->autoload.running = 1;
316ff0f9439SBrian Somers 
317ff0f9439SBrian Somers   if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL))
318ff0f9439SBrian Somers       != bundle->phys_type.open && bundle->idle.timer.state == TIMER_STOPPED)
319ff0f9439SBrian Somers     /* We may need to start our idle timer */
320ff0f9439SBrian Somers     bundle_StartIdleTimer(bundle);
321ff0f9439SBrian Somers }
322ff0f9439SBrian Somers 
323ff0f9439SBrian Somers static void
324ff0f9439SBrian Somers bundle_LinksRemoved(struct bundle *bundle)
325ff0f9439SBrian Somers {
326ff0f9439SBrian Somers   struct datalink *dl;
327ff0f9439SBrian Somers 
328ff0f9439SBrian Somers   bundle->phys_type.all = bundle->phys_type.open = 0;
329ff0f9439SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
330ff0f9439SBrian Somers     bundle_LinkAdded(bundle, dl);
331ff0f9439SBrian Somers 
332ff0f9439SBrian Somers   if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL))
333ff0f9439SBrian Somers       == bundle->phys_type.open)
334ff0f9439SBrian Somers     bundle_StopIdleTimer(bundle);
335ff0f9439SBrian Somers }
33604eaa58cSBrian Somers 
33704eaa58cSBrian Somers static void
3386f384573SBrian Somers bundle_LayerUp(void *v, struct fsm *fp)
3397a6f8720SBrian Somers {
3403006ec67SBrian Somers   /*
3413006ec67SBrian Somers    * The given fsm is now up
342ff0f9439SBrian Somers    * If it's an LCP, adjust our phys_mode.open value.
34349052c95SBrian Somers    * If it's an LCP set our mtu (if we're multilink, add up the link
34404eaa58cSBrian Somers    * speeds and set the MRRU) and start our autoload timer.
345565e35e5SBrian Somers    * If it's an NCP, tell our -background parent to go away.
3463b0f8d2eSBrian Somers    * If it's the first NCP, start the idle timer.
3473006ec67SBrian Somers    */
3486f384573SBrian Somers   struct bundle *bundle = (struct bundle *)v;
3496d666775SBrian Somers 
3505563ebdeSBrian Somers   if (fp->proto == PROTO_LCP) {
351ff0f9439SBrian Somers     struct physical *p = link2physical(fp->link);
352ff0f9439SBrian Somers 
353ff0f9439SBrian Somers     bundle_LinkAdded(bundle, p->dl);
3543b0f8d2eSBrian Somers     if (bundle->ncp.mp.active) {
3553b0f8d2eSBrian Somers       struct datalink *dl;
3565563ebdeSBrian Somers 
357faefde08SBrian Somers       bundle->ifp.Speed = 0;
358faefde08SBrian Somers       for (dl = bundle->links; dl; dl = dl->next)
359eeab6bf5SBrian Somers         if (dl->state == DATALINK_OPEN)
360faefde08SBrian Somers           bundle->ifp.Speed += modem_Speed(dl->physical);
361faefde08SBrian Somers       tun_configure(bundle, bundle->ncp.mp.peer_mrru);
36204eaa58cSBrian Somers       bundle->autoload.running = 1;
363faefde08SBrian Somers     } else {
364ff0f9439SBrian Somers       bundle->ifp.Speed = modem_Speed(p);
365faefde08SBrian Somers       tun_configure(bundle, fsm2lcp(fp)->his_mru);
366faefde08SBrian Somers     }
3673b0f8d2eSBrian Somers   } else if (fp->proto == PROTO_IPCP) {
368ab886ad0SBrian Somers     bundle_StartIdleTimer(bundle);
3695cf4388bSBrian Somers     bundle_Notify(bundle, EX_NORMAL);
3707a6f8720SBrian Somers   }
371ab886ad0SBrian Somers }
3727a6f8720SBrian Somers 
3736d666775SBrian Somers static void
3746d666775SBrian Somers bundle_LayerDown(void *v, struct fsm *fp)
3756d666775SBrian Somers {
3766d666775SBrian Somers   /*
3776d666775SBrian Somers    * The given FSM has been told to come down.
378ab886ad0SBrian Somers    * If it's our last NCP, stop the idle timer.
379ff0f9439SBrian Somers    * If it's an LCP, adjust our phys_type.open value and any timers.
38086b1f0d7SBrian Somers    * If it's an LCP and we're in multilink mode, adjust our tun
38186b1f0d7SBrian Somers    * speed and make sure our minimum sequence number is adjusted.
3826d666775SBrian Somers    */
383ab886ad0SBrian Somers 
384ab886ad0SBrian Somers   struct bundle *bundle = (struct bundle *)v;
385ab886ad0SBrian Somers 
386078c562eSBrian Somers   if (fp->proto == PROTO_IPCP)
387ab886ad0SBrian Somers     bundle_StopIdleTimer(bundle);
388ff0f9439SBrian Somers   else if (fp->proto == PROTO_LCP) {
389ff0f9439SBrian Somers     bundle_LinksRemoved(bundle);  /* adjust timers & phys_type values */
390ff0f9439SBrian Somers     if (bundle->ncp.mp.active) {
3913b0f8d2eSBrian Somers       struct datalink *dl;
39286b1f0d7SBrian Somers       struct datalink *lost;
3933b0f8d2eSBrian Somers 
394faefde08SBrian Somers       bundle->ifp.Speed = 0;
39586b1f0d7SBrian Somers       lost = NULL;
396faefde08SBrian Somers       for (dl = bundle->links; dl; dl = dl->next)
39786b1f0d7SBrian Somers         if (fp == &dl->physical->link.lcp.fsm)
39886b1f0d7SBrian Somers           lost = dl;
39986b1f0d7SBrian Somers         else if (dl->state == DATALINK_OPEN)
400faefde08SBrian Somers           bundle->ifp.Speed += modem_Speed(dl->physical);
40186b1f0d7SBrian Somers 
402faefde08SBrian Somers       if (bundle->ifp.Speed)
403faefde08SBrian Somers         /* Don't configure down to a speed of 0 */
404faefde08SBrian Somers         tun_configure(bundle, bundle->ncp.mp.link.lcp.his_mru);
40586b1f0d7SBrian Somers 
40686b1f0d7SBrian Somers       if (lost)
40786b1f0d7SBrian Somers         mp_LinkLost(&bundle->ncp.mp, lost);
40886b1f0d7SBrian Somers       else
40986b1f0d7SBrian Somers         log_Printf(LogERROR, "Oops, lost an unrecognised datalink (%s) !\n",
41086b1f0d7SBrian Somers                    fp->link->name);
4113b0f8d2eSBrian Somers     }
4126d666775SBrian Somers   }
413ff0f9439SBrian Somers }
4146d666775SBrian Somers 
4156d666775SBrian Somers static void
4166d666775SBrian Somers bundle_LayerFinish(void *v, struct fsm *fp)
4176d666775SBrian Somers {
4186d666775SBrian Somers   /* The given fsm is now down (fp cannot be NULL)
4196d666775SBrian Somers    *
420dd7e2610SBrian Somers    * If it's the last LCP, fsm_Down all NCPs
421dd7e2610SBrian Somers    * If it's the last NCP, fsm_Close all LCPs
4226d666775SBrian Somers    */
4236d666775SBrian Somers 
4246d666775SBrian Somers   struct bundle *bundle = (struct bundle *)v;
4256d666775SBrian Somers   struct datalink *dl;
4266d666775SBrian Somers 
4273b0f8d2eSBrian Somers   if (fp->proto == PROTO_IPCP) {
42826afeaa2SBrian Somers     if (bundle_Phase(bundle) != PHASE_DEAD)
42925092092SBrian Somers       bundle_NewPhase(bundle, PHASE_TERMINATE);
4306d666775SBrian Somers     for (dl = bundle->links; dl; dl = dl->next)
4319c81b87dSBrian Somers       datalink_Close(dl, CLOSE_NORMAL);
432dd7e2610SBrian Somers     fsm_Down(fp);
433dd7e2610SBrian Somers     fsm_Close(fp);
4343b0f8d2eSBrian Somers   } else if (fp->proto == PROTO_LCP) {
4353b0f8d2eSBrian Somers     int others_active;
436a611cad6SBrian Somers 
4373b0f8d2eSBrian Somers     others_active = 0;
4383b0f8d2eSBrian Somers     for (dl = bundle->links; dl; dl = dl->next)
4393b0f8d2eSBrian Somers       if (fp != &dl->physical->link.lcp.fsm &&
4403b0f8d2eSBrian Somers           dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP)
4413b0f8d2eSBrian Somers         others_active++;
4423b0f8d2eSBrian Somers 
4433b0f8d2eSBrian Somers     if (!others_active) {
444dd7e2610SBrian Somers       fsm_Down(&bundle->ncp.ipcp.fsm);
445dd7e2610SBrian Somers       fsm_Close(&bundle->ncp.ipcp.fsm);		/* ST_INITIAL please */
4466d666775SBrian Somers     }
4473b0f8d2eSBrian Somers   }
4483b0f8d2eSBrian Somers }
4496d666775SBrian Somers 
4507a6f8720SBrian Somers int
4517a6f8720SBrian Somers bundle_LinkIsUp(const struct bundle *bundle)
4527a6f8720SBrian Somers {
4535828db6dSBrian Somers   return bundle->ncp.ipcp.fsm.state == ST_OPENED;
4547a6f8720SBrian Somers }
4557a6f8720SBrian Somers 
4567a6f8720SBrian Somers void
4579c81b87dSBrian Somers bundle_Close(struct bundle *bundle, const char *name, int how)
4587a6f8720SBrian Somers {
459455aabc3SBrian Somers   /*
4603006ec67SBrian Somers    * Please close the given datalink.
461dd7e2610SBrian Somers    * If name == NULL or name is the last datalink, fsm_Close all NCPs
4626f384573SBrian Somers    * (except our MP)
4633b0f8d2eSBrian Somers    * If it isn't the last datalink, just Close that datalink.
464455aabc3SBrian Somers    */
4657a6f8720SBrian Somers 
4663b0f8d2eSBrian Somers   struct datalink *dl, *this_dl;
4673b0f8d2eSBrian Somers   int others_active;
4683006ec67SBrian Somers 
4693b0f8d2eSBrian Somers   if (bundle->phase == PHASE_TERMINATE || bundle->phase == PHASE_DEAD)
4703b0f8d2eSBrian Somers     return;
4713b0f8d2eSBrian Somers 
4723b0f8d2eSBrian Somers   others_active = 0;
4733b0f8d2eSBrian Somers   this_dl = NULL;
4743b0f8d2eSBrian Somers 
4753b0f8d2eSBrian Somers   for (dl = bundle->links; dl; dl = dl->next) {
4763b0f8d2eSBrian Somers     if (name && !strcasecmp(name, dl->name))
4773b0f8d2eSBrian Somers       this_dl = dl;
4783b0f8d2eSBrian Somers     if (name == NULL || this_dl == dl) {
4799c81b87dSBrian Somers       switch (how) {
4809c81b87dSBrian Somers         case CLOSE_LCP:
4819c81b87dSBrian Somers           datalink_DontHangup(dl);
4829c81b87dSBrian Somers           /* fall through */
4839c81b87dSBrian Somers         case CLOSE_STAYDOWN:
4843006ec67SBrian Somers           datalink_StayDown(dl);
4859c81b87dSBrian Somers           break;
4869c81b87dSBrian Somers       }
4873b0f8d2eSBrian Somers     } else if (dl->state != DATALINK_CLOSED && dl->state != DATALINK_HANGUP)
4883b0f8d2eSBrian Somers       others_active++;
4893b0f8d2eSBrian Somers   }
4903b0f8d2eSBrian Somers 
4913b0f8d2eSBrian Somers   if (name && this_dl == NULL) {
492dd7e2610SBrian Somers     log_Printf(LogWARN, "%s: Invalid datalink name\n", name);
4933b0f8d2eSBrian Somers     return;
4943b0f8d2eSBrian Somers   }
4953b0f8d2eSBrian Somers 
4963b0f8d2eSBrian Somers   if (!others_active) {
49704eaa58cSBrian Somers     bundle_StopIdleTimer(bundle);
49804eaa58cSBrian Somers     bundle_StopAutoLoadTimer(bundle);
4993b0f8d2eSBrian Somers     if (bundle->ncp.ipcp.fsm.state > ST_CLOSED ||
5003b0f8d2eSBrian Somers         bundle->ncp.ipcp.fsm.state == ST_STARTING)
501dd7e2610SBrian Somers       fsm_Close(&bundle->ncp.ipcp.fsm);
5023b0f8d2eSBrian Somers     else {
5035828db6dSBrian Somers       if (bundle->ncp.ipcp.fsm.state > ST_INITIAL) {
504dd7e2610SBrian Somers         fsm_Close(&bundle->ncp.ipcp.fsm);
505dd7e2610SBrian Somers         fsm_Down(&bundle->ncp.ipcp.fsm);
506d2fd8d77SBrian Somers       }
507d345321bSBrian Somers       for (dl = bundle->links; dl; dl = dl->next)
5089c81b87dSBrian Somers         datalink_Close(dl, how);
5097a6f8720SBrian Somers     }
5103b0f8d2eSBrian Somers   } else if (this_dl && this_dl->state != DATALINK_CLOSED &&
5113b0f8d2eSBrian Somers              this_dl->state != DATALINK_HANGUP)
5129c81b87dSBrian Somers     datalink_Close(this_dl, how);
513d2fd8d77SBrian Somers }
5147a6f8720SBrian Somers 
5151bc9b5baSBrian Somers void
5161bc9b5baSBrian Somers bundle_Down(struct bundle *bundle)
5171bc9b5baSBrian Somers {
5181bc9b5baSBrian Somers   struct datalink *dl;
5191bc9b5baSBrian Somers 
5201bc9b5baSBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
5219c81b87dSBrian Somers     datalink_Down(dl, CLOSE_STAYDOWN);
5221bc9b5baSBrian Somers }
5231bc9b5baSBrian Somers 
5242f786681SBrian Somers static int
5252f786681SBrian Somers bundle_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
5262f786681SBrian Somers {
5272f786681SBrian Somers   struct bundle *bundle = descriptor2bundle(d);
5282f786681SBrian Somers   struct datalink *dl;
52904eaa58cSBrian Somers   int result, want, queued, nlinks;
5302f786681SBrian Somers 
5312f786681SBrian Somers   result = 0;
5322f786681SBrian Somers 
533078c562eSBrian Somers   /* If there are aren't many packets queued, look for some more. */
53404eaa58cSBrian Somers   for (nlinks = 0, dl = bundle->links; dl; dl = dl->next)
53504eaa58cSBrian Somers     nlinks++;
53604eaa58cSBrian Somers 
53704eaa58cSBrian Somers   if (nlinks) {
53804eaa58cSBrian Somers     queued = r ? bundle_FillQueues(bundle) : ip_QueueLen();
53904eaa58cSBrian Somers     if (bundle->autoload.running) {
54004eaa58cSBrian Somers       if (queued < bundle->cfg.autoload.max.packets) {
54104eaa58cSBrian Somers         if (queued > bundle->cfg.autoload.min.packets)
54204eaa58cSBrian Somers           bundle_StopAutoLoadTimer(bundle);
54304eaa58cSBrian Somers         else if (bundle->autoload.timer.state != TIMER_RUNNING ||
54404eaa58cSBrian Somers                  bundle->autoload.comingup)
54504eaa58cSBrian Somers           bundle_StartAutoLoadTimer(bundle, 0);
54604eaa58cSBrian Somers       } else if (bundle->autoload.timer.state != TIMER_RUNNING ||
54704eaa58cSBrian Somers                  !bundle->autoload.comingup)
54804eaa58cSBrian Somers         bundle_StartAutoLoadTimer(bundle, 1);
54904eaa58cSBrian Somers     }
55004eaa58cSBrian Somers 
551ff0f9439SBrian Somers     if (r && (bundle->phase == PHASE_NETWORK ||
552ff0f9439SBrian Somers               bundle->phys_type.all & PHYS_AUTO)) {
55304eaa58cSBrian Somers       /* enough surplus so that we can tell if we're getting swamped */
55404eaa58cSBrian Somers       want = bundle->cfg.autoload.max.packets + nlinks * 2;
55504eaa58cSBrian Somers       /* but at least 20 packets ! */
55604eaa58cSBrian Somers       if (want < 20)
55704eaa58cSBrian Somers         want = 20;
55804eaa58cSBrian Somers       if (queued < want) {
55904eaa58cSBrian Somers         /* Not enough - select() for more */
56004eaa58cSBrian Somers         FD_SET(bundle->dev.fd, r);
561faefde08SBrian Somers         if (*n < bundle->dev.fd + 1)
562faefde08SBrian Somers           *n = bundle->dev.fd + 1;
5631384bd27SBrian Somers         log_Printf(LogTIMER, "%s: fdset(r) %d\n", TUN_NAME, bundle->dev.fd);
564078c562eSBrian Somers         result++;
565078c562eSBrian Somers       }
56604eaa58cSBrian Somers     }
56704eaa58cSBrian Somers   }
568078c562eSBrian Somers 
56971555108SBrian Somers   /* Which links need a select() ? */
57071555108SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
57171555108SBrian Somers     result += descriptor_UpdateSet(&dl->desc, r, w, e, n);
57271555108SBrian Somers 
573ea722969SBrian Somers   /*
574ea722969SBrian Somers    * This *MUST* be called after the datalink UpdateSet()s as it
57504eaa58cSBrian Somers    * might be ``holding'' one of the datalinks (death-row) and
57604eaa58cSBrian Somers    * wants to be able to de-select() it from the descriptor set.
577ea722969SBrian Somers    */
5780f2f3eb3SBrian Somers   result += descriptor_UpdateSet(&bundle->ncp.mp.server.desc, r, w, e, n);
579ea722969SBrian Somers 
5802f786681SBrian Somers   return result;
5812f786681SBrian Somers }
5822f786681SBrian Somers 
5832f786681SBrian Somers static int
5842f786681SBrian Somers bundle_IsSet(struct descriptor *d, const fd_set *fdset)
5852f786681SBrian Somers {
5862f786681SBrian Somers   struct bundle *bundle = descriptor2bundle(d);
5872f786681SBrian Somers   struct datalink *dl;
5882f786681SBrian Somers 
5892f786681SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
5902f786681SBrian Somers     if (descriptor_IsSet(&dl->desc, fdset))
5912f786681SBrian Somers       return 1;
5922f786681SBrian Somers 
593332b9de0SBrian Somers   if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset))
594332b9de0SBrian Somers     return 1;
595332b9de0SBrian Somers 
596faefde08SBrian Somers   return FD_ISSET(bundle->dev.fd, fdset);
5972f786681SBrian Somers }
5982f786681SBrian Somers 
5992f786681SBrian Somers static void
6002f786681SBrian Somers bundle_DescriptorRead(struct descriptor *d, struct bundle *bundle,
6012f786681SBrian Somers                       const fd_set *fdset)
6022f786681SBrian Somers {
6032f786681SBrian Somers   struct datalink *dl;
6042f786681SBrian Somers 
605ea722969SBrian Somers   if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset))
606ea722969SBrian Somers     descriptor_Read(&bundle->ncp.mp.server.desc, bundle, fdset);
607ea722969SBrian Somers 
6082f786681SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
6092f786681SBrian Somers     if (descriptor_IsSet(&dl->desc, fdset))
6102f786681SBrian Somers       descriptor_Read(&dl->desc, bundle, fdset);
611b6217683SBrian Somers 
612faefde08SBrian Somers   if (FD_ISSET(bundle->dev.fd, fdset)) {
613078c562eSBrian Somers     struct tun_data tun;
614078c562eSBrian Somers     int n, pri;
615078c562eSBrian Somers 
616078c562eSBrian Somers     /* something to read from tun */
617faefde08SBrian Somers     n = read(bundle->dev.fd, &tun, sizeof tun);
618078c562eSBrian Somers     if (n < 0) {
6191384bd27SBrian Somers       log_Printf(LogERROR, "read from %s: %s\n", TUN_NAME, strerror(errno));
620078c562eSBrian Somers       return;
621078c562eSBrian Somers     }
622078c562eSBrian Somers     n -= sizeof tun - sizeof tun.data;
623078c562eSBrian Somers     if (n <= 0) {
6241384bd27SBrian Somers       log_Printf(LogERROR, "read from %s: Only %d bytes read\n", TUN_NAME, n);
625078c562eSBrian Somers       return;
626078c562eSBrian Somers     }
627078c562eSBrian Somers     if (!tun_check_header(tun, AF_INET))
628078c562eSBrian Somers       return;
629078c562eSBrian Somers 
630078c562eSBrian Somers     if (((struct ip *)tun.data)->ip_dst.s_addr ==
631078c562eSBrian Somers         bundle->ncp.ipcp.my_ip.s_addr) {
632078c562eSBrian Somers       /* we've been asked to send something addressed *to* us :( */
633078c562eSBrian Somers       if (Enabled(bundle, OPT_LOOPBACK)) {
634078c562eSBrian Somers         pri = PacketCheck(bundle, tun.data, n, &bundle->filter.in);
635078c562eSBrian Somers         if (pri >= 0) {
636078c562eSBrian Somers           struct mbuf *bp;
637078c562eSBrian Somers 
638078c562eSBrian Somers #ifndef NOALIAS
639078c562eSBrian Somers           if (alias_IsEnabled()) {
640078c562eSBrian Somers             (*PacketAlias.In)(tun.data, sizeof tun.data);
641078c562eSBrian Somers             n = ntohs(((struct ip *)tun.data)->ip_len);
642078c562eSBrian Somers           }
643078c562eSBrian Somers #endif
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)
66581358fa3SBrian Somers         bundle_Open(bundle, NULL, PHYS_AUTO);
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
680078c562eSBrian Somers       if (alias_IsEnabled()) {
681078c562eSBrian Somers         (*PacketAlias.Out)(tun.data, sizeof tun.data);
682078c562eSBrian Somers         n = ntohs(((struct ip *)tun.data)->ip_len);
683078c562eSBrian Somers       }
684078c562eSBrian Somers #endif
685078c562eSBrian Somers       ip_Enqueue(pri, tun.data, n);
686078c562eSBrian Somers     }
687078c562eSBrian Somers   }
6882f786681SBrian Somers }
6892f786681SBrian Somers 
6902f786681SBrian Somers static void
6912f786681SBrian Somers bundle_DescriptorWrite(struct descriptor *d, struct bundle *bundle,
6922f786681SBrian Somers                        const fd_set *fdset)
6932f786681SBrian Somers {
6942f786681SBrian Somers   struct datalink *dl;
6952f786681SBrian Somers 
696ea722969SBrian Somers   /* This is not actually necessary as struct mpserver doesn't Write() */
697ea722969SBrian Somers   if (descriptor_IsSet(&bundle->ncp.mp.server.desc, fdset))
698ea722969SBrian Somers     descriptor_Write(&bundle->ncp.mp.server.desc, bundle, fdset);
699ea722969SBrian Somers 
7002f786681SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
7012f786681SBrian Somers     if (descriptor_IsSet(&dl->desc, fdset))
7022f786681SBrian Somers       descriptor_Write(&dl->desc, bundle, fdset);
7032f786681SBrian Somers }
7042f786681SBrian Somers 
705da66dd13SBrian Somers void
7061384bd27SBrian Somers bundle_LockTun(struct bundle *bundle)
7071384bd27SBrian Somers {
7081384bd27SBrian Somers   FILE *lockfile;
7091384bd27SBrian Somers   char pidfile[MAXPATHLEN];
7101384bd27SBrian Somers 
7111384bd27SBrian Somers   snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit);
7121384bd27SBrian Somers   lockfile = ID0fopen(pidfile, "w");
7131384bd27SBrian Somers   if (lockfile != NULL) {
7141384bd27SBrian Somers     fprintf(lockfile, "%d\n", (int)getpid());
7151384bd27SBrian Somers     fclose(lockfile);
7161384bd27SBrian Somers   }
7171384bd27SBrian Somers #ifndef RELEASE_CRUNCH
7181384bd27SBrian Somers   else
7191384bd27SBrian Somers     log_Printf(LogERROR, "Warning: Can't create %s: %s\n",
7201384bd27SBrian Somers                pidfile, strerror(errno));
7211384bd27SBrian Somers #endif
7221384bd27SBrian Somers }
7231384bd27SBrian Somers 
7241384bd27SBrian Somers static void
7251384bd27SBrian Somers bundle_UnlockTun(struct bundle *bundle)
7261384bd27SBrian Somers {
7271384bd27SBrian Somers   char pidfile[MAXPATHLEN];
7281384bd27SBrian Somers 
7291384bd27SBrian Somers   snprintf(pidfile, sizeof pidfile, "%stun%d.pid", _PATH_VARRUN, bundle->unit);
7301384bd27SBrian Somers   ID0unlink(pidfile);
7311384bd27SBrian Somers }
7327a6f8720SBrian Somers 
7337a6f8720SBrian Somers struct bundle *
7348e7b8599SBrian Somers bundle_Create(const char *prefix, int type, const char **argv)
7357a6f8720SBrian Somers {
7367a6f8720SBrian Somers   int s, enoentcount, err;
7377a6f8720SBrian Somers   struct ifreq ifrq;
7387a6f8720SBrian Somers   static struct bundle bundle;		/* there can be only one */
7397a6f8720SBrian Somers 
740faefde08SBrian Somers   if (bundle.ifp.Name != NULL) {	/* Already allocated ! */
741dd7e2610SBrian Somers     log_Printf(LogERROR, "bundle_Create:  There's only one BUNDLE !\n");
7427a6f8720SBrian Somers     return NULL;
7437a6f8720SBrian Somers   }
7447a6f8720SBrian Somers 
7457a6f8720SBrian Somers   err = ENOENT;
7467a6f8720SBrian Somers   enoentcount = 0;
747107d62e7SBrian Somers   for (bundle.unit = 0; ; bundle.unit++) {
748faefde08SBrian Somers     snprintf(bundle.dev.Name, sizeof bundle.dev.Name, "%s%d",
749faefde08SBrian Somers              prefix, bundle.unit);
750faefde08SBrian Somers     bundle.dev.fd = ID0open(bundle.dev.Name, O_RDWR);
751faefde08SBrian Somers     if (bundle.dev.fd >= 0)
7527a6f8720SBrian Somers       break;
753107d62e7SBrian Somers     else if (errno == ENXIO) {
7547a6f8720SBrian Somers       err = errno;
755107d62e7SBrian Somers       break;
7567a6f8720SBrian Somers     } else if (errno == ENOENT) {
7577a6f8720SBrian Somers       if (++enoentcount > 2)
758107d62e7SBrian Somers 	break;
7597a6f8720SBrian Somers     } else
7607a6f8720SBrian Somers       err = errno;
7617a6f8720SBrian Somers   }
7627a6f8720SBrian Somers 
763faefde08SBrian Somers   if (bundle.dev.fd < 0) {
764dd7e2610SBrian Somers     log_Printf(LogWARN, "No available tunnel devices found (%s).\n",
76585b542cfSBrian Somers               strerror(err));
7667a6f8720SBrian Somers     return NULL;
7677a6f8720SBrian Somers   }
7687a6f8720SBrian Somers 
769dd7e2610SBrian Somers   log_SetTun(bundle.unit);
7708e7b8599SBrian Somers   bundle.argv = argv;
7717a6f8720SBrian Somers 
7727a6f8720SBrian Somers   s = socket(AF_INET, SOCK_DGRAM, 0);
7737a6f8720SBrian Somers   if (s < 0) {
774dd7e2610SBrian Somers     log_Printf(LogERROR, "bundle_Create: socket(): %s\n", strerror(errno));
775faefde08SBrian Somers     close(bundle.dev.fd);
7767a6f8720SBrian Somers     return NULL;
7777a6f8720SBrian Somers   }
7787a6f8720SBrian Somers 
779faefde08SBrian Somers   bundle.ifp.Name = strrchr(bundle.dev.Name, '/');
780faefde08SBrian Somers   if (bundle.ifp.Name == NULL)
781faefde08SBrian Somers     bundle.ifp.Name = bundle.dev.Name;
7827a6f8720SBrian Somers   else
783faefde08SBrian Somers     bundle.ifp.Name++;
7847a6f8720SBrian Somers 
7857a6f8720SBrian Somers   /*
7867a6f8720SBrian Somers    * Now, bring up the interface.
7877a6f8720SBrian Somers    */
7887a6f8720SBrian Somers   memset(&ifrq, '\0', sizeof ifrq);
789faefde08SBrian Somers   strncpy(ifrq.ifr_name, bundle.ifp.Name, sizeof ifrq.ifr_name - 1);
7907a6f8720SBrian Somers   ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0';
7917a6f8720SBrian Somers   if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
792dd7e2610SBrian Somers     log_Printf(LogERROR, "OpenTunnel: ioctl(SIOCGIFFLAGS): %s\n",
7937a6f8720SBrian Somers 	      strerror(errno));
7947a6f8720SBrian Somers     close(s);
795faefde08SBrian Somers     close(bundle.dev.fd);
796faefde08SBrian Somers     bundle.ifp.Name = NULL;
7977a6f8720SBrian Somers     return NULL;
7987a6f8720SBrian Somers   }
7997a6f8720SBrian Somers   ifrq.ifr_flags |= IFF_UP;
8007a6f8720SBrian Somers   if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) {
801dd7e2610SBrian Somers     log_Printf(LogERROR, "OpenTunnel: ioctl(SIOCSIFFLAGS): %s\n",
8027a6f8720SBrian Somers 	      strerror(errno));
8037a6f8720SBrian Somers     close(s);
804faefde08SBrian Somers     close(bundle.dev.fd);
805faefde08SBrian Somers     bundle.ifp.Name = NULL;
8067a6f8720SBrian Somers     return NULL;
8077a6f8720SBrian Somers   }
8087a6f8720SBrian Somers 
8097a6f8720SBrian Somers   close(s);
8107a6f8720SBrian Somers 
811faefde08SBrian Somers   if ((bundle.ifp.Index = GetIfIndex(bundle.ifp.Name)) < 0) {
812faefde08SBrian Somers     log_Printf(LogERROR, "OpenTunnel: Can't find interface index.\n");
813faefde08SBrian Somers     close(bundle.dev.fd);
814faefde08SBrian Somers     bundle.ifp.Name = NULL;
8157a6f8720SBrian Somers     return NULL;
8167a6f8720SBrian Somers   }
817faefde08SBrian Somers   log_Printf(LogPHASE, "Using interface: %s\n", bundle.ifp.Name);
8187a6f8720SBrian Somers 
819faefde08SBrian Somers   bundle.ifp.Speed = 0;
8207a6f8720SBrian Somers 
821820de6ebSBrian Somers   bundle.routing_seq = 0;
822a0cbd833SBrian Somers   bundle.phase = PHASE_DEAD;
823a0cbd833SBrian Somers   bundle.CleaningUp = 0;
8247a6f8720SBrian Somers 
8256d666775SBrian Somers   bundle.fsm.LayerStart = bundle_LayerStart;
8266f384573SBrian Somers   bundle.fsm.LayerUp = bundle_LayerUp;
8276d666775SBrian Somers   bundle.fsm.LayerDown = bundle_LayerDown;
8286d666775SBrian Somers   bundle.fsm.LayerFinish = bundle_LayerFinish;
8296d666775SBrian Somers   bundle.fsm.object = &bundle;
8307a6f8720SBrian Somers 
831ab886ad0SBrian Somers   bundle.cfg.idle_timeout = NCP_IDLE_TIMEOUT;
8321342caedSBrian Somers   *bundle.cfg.auth.name = '\0';
8331342caedSBrian Somers   *bundle.cfg.auth.key = '\0';
834610b185fSBrian Somers   bundle.cfg.opt = OPT_SROUTES | OPT_IDCHECK | OPT_LOOPBACK |
835610b185fSBrian Somers                    OPT_THROUGHPUT | OPT_UTMP;
83649052c95SBrian Somers   *bundle.cfg.label = '\0';
83749052c95SBrian Somers   bundle.cfg.mtu = DEF_MTU;
83804eaa58cSBrian Somers   bundle.cfg.autoload.max.packets = 0;
83904eaa58cSBrian Somers   bundle.cfg.autoload.max.timeout = 0;
84004eaa58cSBrian Somers   bundle.cfg.autoload.min.packets = 0;
84104eaa58cSBrian Somers   bundle.cfg.autoload.min.timeout = 0;
842ff0f9439SBrian Somers   bundle.phys_type.all = type;
843ff0f9439SBrian Somers   bundle.phys_type.open = 0;
844ab886ad0SBrian Somers 
8456f384573SBrian Somers   bundle.links = datalink_Create("deflink", &bundle, type);
8463006ec67SBrian Somers   if (bundle.links == NULL) {
847dd7e2610SBrian Somers     log_Printf(LogERROR, "Cannot create data link: %s\n", strerror(errno));
848faefde08SBrian Somers     close(bundle.dev.fd);
849faefde08SBrian Somers     bundle.ifp.Name = NULL;
8502289f246SBrian Somers     return NULL;
8512289f246SBrian Somers   }
8522289f246SBrian Somers 
8532f786681SBrian Somers   bundle.desc.type = BUNDLE_DESCRIPTOR;
8542f786681SBrian Somers   bundle.desc.UpdateSet = bundle_UpdateSet;
8552f786681SBrian Somers   bundle.desc.IsSet = bundle_IsSet;
8562f786681SBrian Somers   bundle.desc.Read = bundle_DescriptorRead;
8572f786681SBrian Somers   bundle.desc.Write = bundle_DescriptorWrite;
8582f786681SBrian Somers 
85949052c95SBrian Somers   mp_Init(&bundle.ncp.mp, &bundle);
86049052c95SBrian Somers 
86149052c95SBrian Somers   /* Send over the first physical link by default */
8625828db6dSBrian Somers   ipcp_Init(&bundle.ncp.ipcp, &bundle, &bundle.links->physical->link,
8635828db6dSBrian Somers             &bundle.fsm);
8646d666775SBrian Somers 
8655ca5389aSBrian Somers   memset(&bundle.filter, '\0', sizeof bundle.filter);
8665ca5389aSBrian Somers   bundle.filter.in.fragok = bundle.filter.in.logok = 1;
8675ca5389aSBrian Somers   bundle.filter.in.name = "IN";
8685ca5389aSBrian Somers   bundle.filter.out.fragok = bundle.filter.out.logok = 1;
8695ca5389aSBrian Somers   bundle.filter.out.name = "OUT";
8705ca5389aSBrian Somers   bundle.filter.dial.name = "DIAL";
8718390b576SBrian Somers   bundle.filter.dial.logok = 1;
8725ca5389aSBrian Somers   bundle.filter.alive.name = "ALIVE";
8735ca5389aSBrian Somers   bundle.filter.alive.logok = 1;
87493ee0ff2SBrian Somers   memset(&bundle.idle.timer, '\0', sizeof bundle.idle.timer);
87593ee0ff2SBrian Somers   bundle.idle.done = 0;
8765cf4388bSBrian Somers   bundle.notify.fd = -1;
87704eaa58cSBrian Somers   memset(&bundle.autoload.timer, '\0', sizeof bundle.autoload.timer);
87804eaa58cSBrian Somers   bundle.autoload.done = 0;
87904eaa58cSBrian Somers   bundle.autoload.running = 0;
88093ee0ff2SBrian Somers 
8816d666775SBrian Somers   /* Clean out any leftover crud */
8826d666775SBrian Somers   bundle_CleanInterface(&bundle);
8836d666775SBrian Somers 
8841384bd27SBrian Somers   bundle_LockTun(&bundle);
8851384bd27SBrian Somers 
8867a6f8720SBrian Somers   return &bundle;
8877a6f8720SBrian Somers }
8887a6f8720SBrian Somers 
88968a0f0ccSBrian Somers static void
89068a0f0ccSBrian Somers bundle_DownInterface(struct bundle *bundle)
89168a0f0ccSBrian Somers {
89268a0f0ccSBrian Somers   struct ifreq ifrq;
89368a0f0ccSBrian Somers   int s;
89468a0f0ccSBrian Somers 
895dd7e2610SBrian Somers   route_IfDelete(bundle, 1);
89668a0f0ccSBrian Somers 
89768a0f0ccSBrian Somers   s = ID0socket(AF_INET, SOCK_DGRAM, 0);
89868a0f0ccSBrian Somers   if (s < 0) {
899dd7e2610SBrian Somers     log_Printf(LogERROR, "bundle_DownInterface: socket: %s\n", strerror(errno));
90068a0f0ccSBrian Somers     return;
90168a0f0ccSBrian Somers   }
90268a0f0ccSBrian Somers 
90368a0f0ccSBrian Somers   memset(&ifrq, '\0', sizeof ifrq);
904faefde08SBrian Somers   strncpy(ifrq.ifr_name, bundle->ifp.Name, sizeof ifrq.ifr_name - 1);
90568a0f0ccSBrian Somers   ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0';
90668a0f0ccSBrian Somers   if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
907dd7e2610SBrian Somers     log_Printf(LogERROR, "bundle_DownInterface: ioctl(SIOCGIFFLAGS): %s\n",
90868a0f0ccSBrian Somers        strerror(errno));
90968a0f0ccSBrian Somers     close(s);
91068a0f0ccSBrian Somers     return;
91168a0f0ccSBrian Somers   }
91268a0f0ccSBrian Somers   ifrq.ifr_flags &= ~IFF_UP;
91368a0f0ccSBrian Somers   if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) {
914dd7e2610SBrian Somers     log_Printf(LogERROR, "bundle_DownInterface: ioctl(SIOCSIFFLAGS): %s\n",
91568a0f0ccSBrian Somers        strerror(errno));
91668a0f0ccSBrian Somers     close(s);
91768a0f0ccSBrian Somers     return;
91868a0f0ccSBrian Somers   }
91968a0f0ccSBrian Somers   close(s);
92068a0f0ccSBrian Somers }
92168a0f0ccSBrian Somers 
92268a0f0ccSBrian Somers void
92368a0f0ccSBrian Somers bundle_Destroy(struct bundle *bundle)
92468a0f0ccSBrian Somers {
9253006ec67SBrian Somers   struct datalink *dl;
926b6217683SBrian Somers 
927ea722969SBrian Somers   /*
92804eaa58cSBrian Somers    * Clean up the interface.  We don't need to timer_Stop()s, mp_Down(),
929ea722969SBrian Somers    * ipcp_CleanInterface() and bundle_DownInterface() unless we're getting
930ea722969SBrian Somers    * out under exceptional conditions such as a descriptor exception.
931ea722969SBrian Somers    */
93204eaa58cSBrian Somers   timer_Stop(&bundle->idle.timer);
93304eaa58cSBrian Somers   timer_Stop(&bundle->autoload.timer);
93466f634b6SBrian Somers   mp_Down(&bundle->ncp.mp);
935dd7e2610SBrian Somers   ipcp_CleanInterface(&bundle->ncp.ipcp);
93668a0f0ccSBrian Somers   bundle_DownInterface(bundle);
9373006ec67SBrian Somers 
938ea722969SBrian Somers   /* Again, these are all DATALINK_CLOSED unless we're abending */
9393006ec67SBrian Somers   dl = bundle->links;
9403006ec67SBrian Somers   while (dl)
9413006ec67SBrian Somers     dl = datalink_Destroy(dl);
9423006ec67SBrian Somers 
9431384bd27SBrian Somers   close(bundle->dev.fd);
9441384bd27SBrian Somers   bundle_UnlockTun(bundle);
9451384bd27SBrian Somers 
946ea722969SBrian Somers   /* In case we never made PHASE_NETWORK */
9475cf4388bSBrian Somers   bundle_Notify(bundle, EX_ERRDEAD);
948b6217683SBrian Somers 
949faefde08SBrian Somers   bundle->ifp.Name = NULL;
95068a0f0ccSBrian Somers }
95168a0f0ccSBrian Somers 
9527a6f8720SBrian Somers struct rtmsg {
9537a6f8720SBrian Somers   struct rt_msghdr m_rtm;
9547a6f8720SBrian Somers   char m_space[64];
9557a6f8720SBrian Somers };
9567a6f8720SBrian Somers 
957610b185fSBrian Somers int
958820de6ebSBrian Somers bundle_SetRoute(struct bundle *bundle, int cmd, struct in_addr dst,
9597a6f8720SBrian Somers                 struct in_addr gateway, struct in_addr mask, int bang)
9607a6f8720SBrian Somers {
9617a6f8720SBrian Somers   struct rtmsg rtmes;
9627a6f8720SBrian Somers   int s, nb, wb;
9637a6f8720SBrian Somers   char *cp;
9647a6f8720SBrian Somers   const char *cmdstr;
9657a6f8720SBrian Somers   struct sockaddr_in rtdata;
966610b185fSBrian Somers   int result = 1;
9677a6f8720SBrian Somers 
9687a6f8720SBrian Somers   if (bang)
9697a6f8720SBrian Somers     cmdstr = (cmd == RTM_ADD ? "Add!" : "Delete!");
9707a6f8720SBrian Somers   else
9717a6f8720SBrian Somers     cmdstr = (cmd == RTM_ADD ? "Add" : "Delete");
9727a6f8720SBrian Somers   s = ID0socket(PF_ROUTE, SOCK_RAW, 0);
9737a6f8720SBrian Somers   if (s < 0) {
974dd7e2610SBrian Somers     log_Printf(LogERROR, "bundle_SetRoute: socket(): %s\n", strerror(errno));
975610b185fSBrian Somers     return result;
9767a6f8720SBrian Somers   }
9777a6f8720SBrian Somers   memset(&rtmes, '\0', sizeof rtmes);
9787a6f8720SBrian Somers   rtmes.m_rtm.rtm_version = RTM_VERSION;
9797a6f8720SBrian Somers   rtmes.m_rtm.rtm_type = cmd;
9807a6f8720SBrian Somers   rtmes.m_rtm.rtm_addrs = RTA_DST;
981820de6ebSBrian Somers   rtmes.m_rtm.rtm_seq = ++bundle->routing_seq;
9827a6f8720SBrian Somers   rtmes.m_rtm.rtm_pid = getpid();
9837a6f8720SBrian Somers   rtmes.m_rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;
9847a6f8720SBrian Somers 
9857a6f8720SBrian Somers   memset(&rtdata, '\0', sizeof rtdata);
98650e5c17dSBrian Somers   rtdata.sin_len = sizeof rtdata;
9877a6f8720SBrian Somers   rtdata.sin_family = AF_INET;
9887a6f8720SBrian Somers   rtdata.sin_port = 0;
9897a6f8720SBrian Somers   rtdata.sin_addr = dst;
9907a6f8720SBrian Somers 
9917a6f8720SBrian Somers   cp = rtmes.m_space;
99250e5c17dSBrian Somers   memcpy(cp, &rtdata, rtdata.sin_len);
99350e5c17dSBrian Somers   cp += rtdata.sin_len;
994e43ebac1SBrian Somers   if (cmd == RTM_ADD) {
9957a6f8720SBrian Somers     if (gateway.s_addr == INADDR_ANY) {
9967a6f8720SBrian Somers       /* Add a route through the interface */
9977a6f8720SBrian Somers       struct sockaddr_dl dl;
9987a6f8720SBrian Somers       const char *iname;
9997a6f8720SBrian Somers       int ilen;
10007a6f8720SBrian Somers 
1001faefde08SBrian Somers       iname = Index2Nam(bundle->ifp.Index);
10027a6f8720SBrian Somers       ilen = strlen(iname);
10037a6f8720SBrian Somers       dl.sdl_len = sizeof dl - sizeof dl.sdl_data + ilen;
10047a6f8720SBrian Somers       dl.sdl_family = AF_LINK;
1005faefde08SBrian Somers       dl.sdl_index = bundle->ifp.Index;
10067a6f8720SBrian Somers       dl.sdl_type = 0;
10077a6f8720SBrian Somers       dl.sdl_nlen = ilen;
10087a6f8720SBrian Somers       dl.sdl_alen = 0;
10097a6f8720SBrian Somers       dl.sdl_slen = 0;
10107a6f8720SBrian Somers       strncpy(dl.sdl_data, iname, sizeof dl.sdl_data);
10117a6f8720SBrian Somers       memcpy(cp, &dl, dl.sdl_len);
10127a6f8720SBrian Somers       cp += dl.sdl_len;
10137a6f8720SBrian Somers       rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY;
10147a6f8720SBrian Somers     } else {
10157a6f8720SBrian Somers       rtdata.sin_addr = gateway;
101650e5c17dSBrian Somers       memcpy(cp, &rtdata, rtdata.sin_len);
101750e5c17dSBrian Somers       cp += rtdata.sin_len;
10187a6f8720SBrian Somers       rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY;
10197a6f8720SBrian Somers     }
1020e43ebac1SBrian Somers   }
10217a6f8720SBrian Somers 
10227a6f8720SBrian Somers   if (dst.s_addr == INADDR_ANY)
10237a6f8720SBrian Somers     mask.s_addr = INADDR_ANY;
10247a6f8720SBrian Somers 
10257a6f8720SBrian Somers   if (cmd == RTM_ADD || dst.s_addr == INADDR_ANY) {
10267a6f8720SBrian Somers     rtdata.sin_addr = mask;
102750e5c17dSBrian Somers     memcpy(cp, &rtdata, rtdata.sin_len);
102850e5c17dSBrian Somers     cp += rtdata.sin_len;
10297a6f8720SBrian Somers     rtmes.m_rtm.rtm_addrs |= RTA_NETMASK;
10307a6f8720SBrian Somers   }
10317a6f8720SBrian Somers 
10327a6f8720SBrian Somers   nb = cp - (char *) &rtmes;
10337a6f8720SBrian Somers   rtmes.m_rtm.rtm_msglen = nb;
10347a6f8720SBrian Somers   wb = ID0write(s, &rtmes, nb);
10357a6f8720SBrian Somers   if (wb < 0) {
1036dd7e2610SBrian Somers     log_Printf(LogTCPIP, "bundle_SetRoute failure:\n");
1037dd7e2610SBrian Somers     log_Printf(LogTCPIP, "bundle_SetRoute:  Cmd = %s\n", cmdstr);
1038dd7e2610SBrian Somers     log_Printf(LogTCPIP, "bundle_SetRoute:  Dst = %s\n", inet_ntoa(dst));
1039dd7e2610SBrian Somers     log_Printf(LogTCPIP, "bundle_SetRoute:  Gateway = %s\n", inet_ntoa(gateway));
1040dd7e2610SBrian Somers     log_Printf(LogTCPIP, "bundle_SetRoute:  Mask = %s\n", inet_ntoa(mask));
10417a6f8720SBrian Somers failed:
10427a6f8720SBrian Somers     if (cmd == RTM_ADD && (rtmes.m_rtm.rtm_errno == EEXIST ||
1043e43ebac1SBrian Somers                            (rtmes.m_rtm.rtm_errno == 0 && errno == EEXIST))) {
1044610b185fSBrian Somers       if (!bang) {
1045dd7e2610SBrian Somers         log_Printf(LogWARN, "Add route failed: %s already exists\n",
10467a6f8720SBrian Somers                   inet_ntoa(dst));
1047610b185fSBrian Somers         result = 0;	/* Don't add to our dynamic list */
1048610b185fSBrian Somers       } else {
10497a6f8720SBrian Somers         rtmes.m_rtm.rtm_type = cmd = RTM_CHANGE;
10507a6f8720SBrian Somers         if ((wb = ID0write(s, &rtmes, nb)) < 0)
10517a6f8720SBrian Somers           goto failed;
10527a6f8720SBrian Somers       }
1053e43ebac1SBrian Somers     } else if (cmd == RTM_DELETE &&
10547a6f8720SBrian Somers              (rtmes.m_rtm.rtm_errno == ESRCH ||
10557a6f8720SBrian Somers               (rtmes.m_rtm.rtm_errno == 0 && errno == ESRCH))) {
10567a6f8720SBrian Somers       if (!bang)
1057dd7e2610SBrian Somers         log_Printf(LogWARN, "Del route failed: %s: Non-existent\n",
10587a6f8720SBrian Somers                   inet_ntoa(dst));
10597a6f8720SBrian Somers     } else if (rtmes.m_rtm.rtm_errno == 0)
1060dd7e2610SBrian Somers       log_Printf(LogWARN, "%s route failed: %s: errno: %s\n", cmdstr,
10617a6f8720SBrian Somers                 inet_ntoa(dst), strerror(errno));
10627a6f8720SBrian Somers     else
1063dd7e2610SBrian Somers       log_Printf(LogWARN, "%s route failed: %s: %s\n",
10647a6f8720SBrian Somers 		cmdstr, inet_ntoa(dst), strerror(rtmes.m_rtm.rtm_errno));
10657a6f8720SBrian Somers   }
1066dd7e2610SBrian Somers   log_Printf(LogDEBUG, "wrote %d: cmd = %s, dst = %x, gateway = %x\n",
1067fe3125a0SBrian Somers             wb, cmdstr, (unsigned)dst.s_addr, (unsigned)gateway.s_addr);
10687a6f8720SBrian Somers   close(s);
1069610b185fSBrian Somers 
1070610b185fSBrian Somers   return result;
10717a6f8720SBrian Somers }
107283d1af55SBrian Somers 
107383d1af55SBrian Somers void
10743006ec67SBrian Somers bundle_LinkClosed(struct bundle *bundle, struct datalink *dl)
10753006ec67SBrian Somers {
10763006ec67SBrian Somers   /*
10773006ec67SBrian Somers    * Our datalink has closed.
1078ea722969SBrian Somers    * CleanDatalinks() (called from DoLoop()) will remove closed
107981358fa3SBrian Somers    * BACKGROUND and DIRECT links.
10803b0f8d2eSBrian Somers    * If it's the last data link, enter phase DEAD.
1081ea722969SBrian Somers    *
1082ea722969SBrian Somers    * NOTE: dl may not be in our list (bundle_SendDatalink()) !
10833006ec67SBrian Somers    */
10845b8b8060SBrian Somers 
10853b0f8d2eSBrian Somers   struct datalink *odl;
10863b0f8d2eSBrian Somers   int other_links;
10875b8b8060SBrian Somers 
10883b0f8d2eSBrian Somers   other_links = 0;
10893b0f8d2eSBrian Somers   for (odl = bundle->links; odl; odl = odl->next)
10903b0f8d2eSBrian Somers     if (odl != dl && odl->state != DATALINK_CLOSED)
10913b0f8d2eSBrian Somers       other_links++;
10923b0f8d2eSBrian Somers 
10933b0f8d2eSBrian Somers   if (!other_links) {
109481358fa3SBrian Somers     if (dl->physical->type != PHYS_AUTO)	/* Not in -auto mode */
109503704096SBrian Somers       bundle_DownInterface(bundle);
109626afeaa2SBrian Somers     if (bundle->ncp.ipcp.fsm.state > ST_CLOSED ||
109726afeaa2SBrian Somers         bundle->ncp.ipcp.fsm.state == ST_STARTING) {
1098dd7e2610SBrian Somers       fsm_Down(&bundle->ncp.ipcp.fsm);
1099dd7e2610SBrian Somers       fsm_Close(&bundle->ncp.ipcp.fsm);		/* ST_INITIAL please */
110026afeaa2SBrian Somers     }
11015563ebdeSBrian Somers     bundle_NewPhase(bundle, PHASE_DEAD);
11020f2f3eb3SBrian Somers     bundle_StopIdleTimer(bundle);
110304eaa58cSBrian Somers     bundle_StopAutoLoadTimer(bundle);
110404eaa58cSBrian Somers     bundle->autoload.running = 0;
110504eaa58cSBrian Somers   } else
110604eaa58cSBrian Somers     bundle->autoload.running = 1;
1107455aabc3SBrian Somers }
1108455aabc3SBrian Somers 
1109455aabc3SBrian Somers void
1110565e35e5SBrian Somers bundle_Open(struct bundle *bundle, const char *name, int mask)
11113006ec67SBrian Somers {
11123006ec67SBrian Somers   /*
11133006ec67SBrian Somers    * Please open the given datalink, or all if name == NULL
11143006ec67SBrian Somers    */
11153006ec67SBrian Somers   struct datalink *dl;
11163006ec67SBrian Somers 
111704eaa58cSBrian Somers   timer_Stop(&bundle->autoload.timer);
11183006ec67SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
11193006ec67SBrian Somers     if (name == NULL || !strcasecmp(dl->name, name)) {
112004eaa58cSBrian Somers       if (dl->state == DATALINK_CLOSED && (mask & dl->physical->type)) {
1121565e35e5SBrian Somers         datalink_Up(dl, 1, 1);
112281358fa3SBrian Somers         if (mask == PHYS_AUTO)
112381358fa3SBrian Somers           /* Only one AUTO link at a time (see the AutoLoad timer) */
112404eaa58cSBrian Somers           break;
112504eaa58cSBrian Somers       }
11263006ec67SBrian Somers       if (name != NULL)
11273006ec67SBrian Somers         break;
11283006ec67SBrian Somers     }
11293006ec67SBrian Somers }
11303006ec67SBrian Somers 
11313006ec67SBrian Somers struct datalink *
11323006ec67SBrian Somers bundle2datalink(struct bundle *bundle, const char *name)
11333006ec67SBrian Somers {
11343006ec67SBrian Somers   struct datalink *dl;
11353006ec67SBrian Somers 
11363006ec67SBrian Somers   if (name != NULL) {
11373006ec67SBrian Somers     for (dl = bundle->links; dl; dl = dl->next)
11383006ec67SBrian Somers       if (!strcasecmp(dl->name, name))
11393006ec67SBrian Somers         return dl;
11403006ec67SBrian Somers   } else if (bundle->links && !bundle->links->next)
11413006ec67SBrian Somers     return bundle->links;
11423006ec67SBrian Somers 
11433006ec67SBrian Somers   return NULL;
11443006ec67SBrian Somers }
11453006ec67SBrian Somers 
11463006ec67SBrian Somers int
11473006ec67SBrian Somers bundle_FillQueues(struct bundle *bundle)
11483006ec67SBrian Somers {
11493b0f8d2eSBrian Somers   int total;
11503b0f8d2eSBrian Somers 
11511bc9b5baSBrian Somers   if (bundle->ncp.mp.active)
11523b0f8d2eSBrian Somers     total = mp_FillQueues(bundle);
11531bc9b5baSBrian Somers   else {
11541bc9b5baSBrian Somers     struct datalink *dl;
11551bc9b5baSBrian Somers     int add;
11561bc9b5baSBrian Somers 
11571bc9b5baSBrian Somers     for (total = 0, dl = bundle->links; dl; dl = dl->next)
11581bc9b5baSBrian Somers       if (dl->state == DATALINK_OPEN) {
11591bc9b5baSBrian Somers         add = link_QueueLen(&dl->physical->link);
11601bc9b5baSBrian Somers         if (add == 0 && dl->physical->out == NULL)
11611bc9b5baSBrian Somers           add = ip_FlushPacket(&dl->physical->link, bundle);
11621bc9b5baSBrian Somers         total += add;
11631bc9b5baSBrian Somers       }
11643006ec67SBrian Somers   }
11653006ec67SBrian Somers 
11663b0f8d2eSBrian Somers   return total + ip_QueueLen();
11673006ec67SBrian Somers }
1168aef795ccSBrian Somers 
1169aef795ccSBrian Somers int
1170aef795ccSBrian Somers bundle_ShowLinks(struct cmdargs const *arg)
1171aef795ccSBrian Somers {
1172aef795ccSBrian Somers   struct datalink *dl;
1173aef795ccSBrian Somers 
11749c53a7b1SBrian Somers   for (dl = arg->bundle->links; dl; dl = dl->next) {
1175d4156d00SBrian Somers     prompt_Printf(arg->prompt, "Name: %s [%s, %s]",
1176d4156d00SBrian Somers                   dl->name, mode2Nam(dl->physical->type), datalink_State(dl));
1177eeab6bf5SBrian Somers     if (dl->physical->link.throughput.rolling && dl->state == DATALINK_OPEN)
1178d4156d00SBrian Somers       prompt_Printf(arg->prompt, " weight %d, %d bytes/sec",
1179eeab6bf5SBrian Somers                     dl->mp.weight,
11809c53a7b1SBrian Somers                     dl->physical->link.throughput.OctetsPerSecond);
11819c53a7b1SBrian Somers     prompt_Printf(arg->prompt, "\n");
11829c53a7b1SBrian Somers   }
1183aef795ccSBrian Somers 
1184aef795ccSBrian Somers   return 0;
1185aef795ccSBrian Somers }
1186ab886ad0SBrian Somers 
11871342caedSBrian Somers static const char *
11881342caedSBrian Somers optval(struct bundle *bundle, int bit)
11891342caedSBrian Somers {
11901342caedSBrian Somers   return (bundle->cfg.opt & bit) ? "enabled" : "disabled";
11911342caedSBrian Somers }
11921342caedSBrian Somers 
1193c08717dfSBrian Somers int
1194c08717dfSBrian Somers bundle_ShowStatus(struct cmdargs const *arg)
1195c08717dfSBrian Somers {
1196c08717dfSBrian Somers   int remaining;
1197c08717dfSBrian Somers 
1198c08717dfSBrian Somers   prompt_Printf(arg->prompt, "Phase %s\n", bundle_PhaseName(arg->bundle));
1199faefde08SBrian Somers   prompt_Printf(arg->prompt, " Device:        %s\n", arg->bundle->dev.Name);
1200faefde08SBrian Somers   prompt_Printf(arg->prompt, " Interface:     %s @ %lubps\n",
1201faefde08SBrian Somers                 arg->bundle->ifp.Name, arg->bundle->ifp.Speed);
1202c08717dfSBrian Somers 
1203c08717dfSBrian Somers   prompt_Printf(arg->prompt, "\nDefaults:\n");
1204643f4904SBrian Somers   prompt_Printf(arg->prompt, " Label:         %s\n", arg->bundle->cfg.label);
1205610b185fSBrian Somers   prompt_Printf(arg->prompt, " Auth name:     %s\n",
1206610b185fSBrian Somers                 arg->bundle->cfg.auth.name);
120704eaa58cSBrian Somers   prompt_Printf(arg->prompt, " Auto Load:     Up after %ds of >= %d packets\n",
120804eaa58cSBrian Somers                 arg->bundle->cfg.autoload.max.timeout,
120904eaa58cSBrian Somers                 arg->bundle->cfg.autoload.max.packets);
121004eaa58cSBrian Somers   prompt_Printf(arg->prompt, "                Down after %ds of <= %d"
121104eaa58cSBrian Somers                 " packets\n", arg->bundle->cfg.autoload.min.timeout,
121204eaa58cSBrian Somers                 arg->bundle->cfg.autoload.min.packets);
121304eaa58cSBrian Somers   if (arg->bundle->autoload.timer.state == TIMER_RUNNING)
121404eaa58cSBrian Somers     prompt_Printf(arg->prompt, "                %ds remaining 'till "
121504eaa58cSBrian Somers                   "a link comes %s\n",
121604eaa58cSBrian Somers                   bundle_RemainingAutoLoadTime(arg->bundle),
121704eaa58cSBrian Somers                   arg->bundle->autoload.comingup ? "up" : "down");
121804eaa58cSBrian Somers   else
121904eaa58cSBrian Somers     prompt_Printf(arg->prompt, "                %srunning with %d"
122004eaa58cSBrian Somers                   " packets queued\n", arg->bundle->autoload.running ?
122104eaa58cSBrian Somers                   "" : "not ", ip_QueueLen());
122204eaa58cSBrian Somers 
1223c08717dfSBrian Somers   prompt_Printf(arg->prompt, " Idle Timer:    ");
1224c08717dfSBrian Somers   if (arg->bundle->cfg.idle_timeout) {
1225c08717dfSBrian Somers     prompt_Printf(arg->prompt, "%ds", arg->bundle->cfg.idle_timeout);
1226c08717dfSBrian Somers     remaining = bundle_RemainingIdleTime(arg->bundle);
1227c08717dfSBrian Somers     if (remaining != -1)
1228c08717dfSBrian Somers       prompt_Printf(arg->prompt, " (%ds remaining)", remaining);
1229c08717dfSBrian Somers     prompt_Printf(arg->prompt, "\n");
1230c08717dfSBrian Somers   } else
1231c08717dfSBrian Somers     prompt_Printf(arg->prompt, "disabled\n");
1232ce828a6eSBrian Somers   prompt_Printf(arg->prompt, " MTU:           ");
1233ce828a6eSBrian Somers   if (arg->bundle->cfg.mtu)
1234ce828a6eSBrian Somers     prompt_Printf(arg->prompt, "%d\n", arg->bundle->cfg.mtu);
1235ce828a6eSBrian Somers   else
1236ce828a6eSBrian Somers     prompt_Printf(arg->prompt, "unspecified\n");
1237c08717dfSBrian Somers 
1238610b185fSBrian Somers   prompt_Printf(arg->prompt, " Sticky Routes: %s\n",
1239610b185fSBrian Somers                 optval(arg->bundle, OPT_SROUTES));
12401342caedSBrian Somers   prompt_Printf(arg->prompt, " ID check:      %s\n",
12411342caedSBrian Somers                 optval(arg->bundle, OPT_IDCHECK));
12421342caedSBrian Somers   prompt_Printf(arg->prompt, " Loopback:      %s\n",
12431342caedSBrian Somers                 optval(arg->bundle, OPT_LOOPBACK));
12441342caedSBrian Somers   prompt_Printf(arg->prompt, " PasswdAuth:    %s\n",
12451342caedSBrian Somers                 optval(arg->bundle, OPT_PASSWDAUTH));
12461342caedSBrian Somers   prompt_Printf(arg->prompt, " Proxy:         %s\n",
12471342caedSBrian Somers                 optval(arg->bundle, OPT_PROXY));
12481342caedSBrian Somers   prompt_Printf(arg->prompt, " Throughput:    %s\n",
12491342caedSBrian Somers                 optval(arg->bundle, OPT_THROUGHPUT));
1250610b185fSBrian Somers   prompt_Printf(arg->prompt, " Utmp Logging:  %s\n",
12511342caedSBrian Somers                 optval(arg->bundle, OPT_UTMP));
12521342caedSBrian Somers 
1253c08717dfSBrian Somers   return 0;
1254c08717dfSBrian Somers }
1255c08717dfSBrian Somers 
1256ab886ad0SBrian Somers static void
1257ab886ad0SBrian Somers bundle_IdleTimeout(void *v)
1258ab886ad0SBrian Somers {
1259ab886ad0SBrian Somers   struct bundle *bundle = (struct bundle *)v;
1260ab886ad0SBrian Somers 
1261dd7e2610SBrian Somers   log_Printf(LogPHASE, "Idle timer expired.\n");
126204eaa58cSBrian Somers   bundle_StopIdleTimer(bundle);
12639c81b87dSBrian Somers   bundle_Close(bundle, NULL, CLOSE_STAYDOWN);
1264ab886ad0SBrian Somers }
1265ab886ad0SBrian Somers 
1266ab886ad0SBrian Somers /*
1267ab886ad0SBrian Somers  *  Start Idle timer. If timeout is reached, we call bundle_Close() to
1268ab886ad0SBrian Somers  *  close LCP and link.
1269ab886ad0SBrian Somers  */
1270ab886ad0SBrian Somers void
1271ab886ad0SBrian Somers bundle_StartIdleTimer(struct bundle *bundle)
1272ab886ad0SBrian Somers {
1273dd7e2610SBrian Somers   timer_Stop(&bundle->idle.timer);
1274ff0f9439SBrian Somers   if ((bundle->phys_type.open & (PHYS_DEDICATED|PHYS_DDIAL)) !=
1275ff0f9439SBrian Somers       bundle->phys_type.open && bundle->cfg.idle_timeout) {
127693ee0ff2SBrian Somers     bundle->idle.timer.func = bundle_IdleTimeout;
12773b0f8d2eSBrian Somers     bundle->idle.timer.name = "idle";
127893ee0ff2SBrian Somers     bundle->idle.timer.load = bundle->cfg.idle_timeout * SECTICKS;
127993ee0ff2SBrian Somers     bundle->idle.timer.arg = bundle;
1280dd7e2610SBrian Somers     timer_Start(&bundle->idle.timer);
128193ee0ff2SBrian Somers     bundle->idle.done = time(NULL) + bundle->cfg.idle_timeout;
1282ab886ad0SBrian Somers   }
1283ab886ad0SBrian Somers }
1284ab886ad0SBrian Somers 
1285ab886ad0SBrian Somers void
1286ab886ad0SBrian Somers bundle_SetIdleTimer(struct bundle *bundle, int value)
1287ab886ad0SBrian Somers {
1288ab886ad0SBrian Somers   bundle->cfg.idle_timeout = value;
1289ab886ad0SBrian Somers   if (bundle_LinkIsUp(bundle))
1290ab886ad0SBrian Somers     bundle_StartIdleTimer(bundle);
1291ab886ad0SBrian Somers }
1292ab886ad0SBrian Somers 
1293ab886ad0SBrian Somers void
1294ab886ad0SBrian Somers bundle_StopIdleTimer(struct bundle *bundle)
1295ab886ad0SBrian Somers {
1296dd7e2610SBrian Somers   timer_Stop(&bundle->idle.timer);
12974a632c80SBrian Somers   bundle->idle.done = 0;
1298ab886ad0SBrian Somers }
1299ab886ad0SBrian Somers 
130004eaa58cSBrian Somers static int
1301ab886ad0SBrian Somers bundle_RemainingIdleTime(struct bundle *bundle)
1302ab886ad0SBrian Somers {
130393ee0ff2SBrian Somers   if (bundle->idle.done)
130493ee0ff2SBrian Somers     return bundle->idle.done - time(NULL);
1305ab886ad0SBrian Somers   return -1;
1306ab886ad0SBrian Somers }
13073b0f8d2eSBrian Somers 
13083b0f8d2eSBrian Somers int
13093b0f8d2eSBrian Somers bundle_IsDead(struct bundle *bundle)
13103b0f8d2eSBrian Somers {
13113b0f8d2eSBrian Somers   return !bundle->links || (bundle->phase == PHASE_DEAD && bundle->CleaningUp);
13123b0f8d2eSBrian Somers }
1313b6217683SBrian Somers 
131404eaa58cSBrian Somers static struct datalink *
131504eaa58cSBrian Somers bundle_DatalinkLinkout(struct bundle *bundle, struct datalink *dl)
1316cd7bd93aSBrian Somers {
1317cd7bd93aSBrian Somers   struct datalink **dlp;
1318cd7bd93aSBrian Somers 
1319cd7bd93aSBrian Somers   for (dlp = &bundle->links; *dlp; dlp = &(*dlp)->next)
1320cd7bd93aSBrian Somers     if (*dlp == dl) {
132104eaa58cSBrian Somers       *dlp = dl->next;
132204eaa58cSBrian Somers       dl->next = NULL;
132304eaa58cSBrian Somers       bundle_LinksRemoved(bundle);
132404eaa58cSBrian Somers       return dl;
1325cd7bd93aSBrian Somers     }
132604eaa58cSBrian Somers 
132704eaa58cSBrian Somers   return NULL;
132804eaa58cSBrian Somers }
132904eaa58cSBrian Somers 
133004eaa58cSBrian Somers static void
133104eaa58cSBrian Somers bundle_DatalinkLinkin(struct bundle *bundle, struct datalink *dl)
133204eaa58cSBrian Somers {
133304eaa58cSBrian Somers   struct datalink **dlp = &bundle->links;
133404eaa58cSBrian Somers 
133504eaa58cSBrian Somers   while (*dlp)
133604eaa58cSBrian Somers     dlp = &(*dlp)->next;
133704eaa58cSBrian Somers 
133804eaa58cSBrian Somers   *dlp = dl;
133904eaa58cSBrian Somers   dl->next = NULL;
134004eaa58cSBrian Somers 
134104eaa58cSBrian Somers   bundle_LinkAdded(bundle, dl);
1342565e35e5SBrian Somers }
1343565e35e5SBrian Somers 
1344565e35e5SBrian Somers void
1345565e35e5SBrian Somers bundle_CleanDatalinks(struct bundle *bundle)
1346565e35e5SBrian Somers {
1347565e35e5SBrian Somers   struct datalink **dlp = &bundle->links;
134804eaa58cSBrian Somers   int found = 0;
1349565e35e5SBrian Somers 
1350565e35e5SBrian Somers   while (*dlp)
1351565e35e5SBrian Somers     if ((*dlp)->state == DATALINK_CLOSED &&
135281358fa3SBrian Somers         (*dlp)->physical->type & (PHYS_DIRECT|PHYS_BACKGROUND)) {
1353565e35e5SBrian Somers       *dlp = datalink_Destroy(*dlp);
135404eaa58cSBrian Somers       found++;
135504eaa58cSBrian Somers     } else
1356565e35e5SBrian Somers       dlp = &(*dlp)->next;
135704eaa58cSBrian Somers 
135804eaa58cSBrian Somers   if (found)
135904eaa58cSBrian Somers     bundle_LinksRemoved(bundle);
136004eaa58cSBrian Somers }
136104eaa58cSBrian Somers 
136204eaa58cSBrian Somers int
136304eaa58cSBrian Somers bundle_DatalinkClone(struct bundle *bundle, struct datalink *dl,
136404eaa58cSBrian Somers                      const char *name)
136504eaa58cSBrian Somers {
136604eaa58cSBrian Somers   if (bundle2datalink(bundle, name)) {
136704eaa58cSBrian Somers     log_Printf(LogWARN, "Clone: %s: name already exists\n", name);
136804eaa58cSBrian Somers     return 0;
136904eaa58cSBrian Somers   }
137004eaa58cSBrian Somers 
137104eaa58cSBrian Somers   bundle_DatalinkLinkin(bundle, datalink_Clone(dl, name));
137204eaa58cSBrian Somers   return 1;
137304eaa58cSBrian Somers }
137404eaa58cSBrian Somers 
137504eaa58cSBrian Somers void
137604eaa58cSBrian Somers bundle_DatalinkRemove(struct bundle *bundle, struct datalink *dl)
137704eaa58cSBrian Somers {
137804eaa58cSBrian Somers   dl = bundle_DatalinkLinkout(bundle, dl);
137904eaa58cSBrian Somers   if (dl)
138004eaa58cSBrian Somers     datalink_Destroy(dl);
1381cd7bd93aSBrian Somers }
138249052c95SBrian Somers 
138349052c95SBrian Somers void
138449052c95SBrian Somers bundle_SetLabel(struct bundle *bundle, const char *label)
138549052c95SBrian Somers {
138649052c95SBrian Somers   if (label)
138749052c95SBrian Somers     strncpy(bundle->cfg.label, label, sizeof bundle->cfg.label - 1);
138849052c95SBrian Somers   else
138949052c95SBrian Somers     *bundle->cfg.label = '\0';
139049052c95SBrian Somers }
139149052c95SBrian Somers 
139249052c95SBrian Somers const char *
139349052c95SBrian Somers bundle_GetLabel(struct bundle *bundle)
139449052c95SBrian Somers {
139549052c95SBrian Somers   return *bundle->cfg.label ? bundle->cfg.label : NULL;
139649052c95SBrian Somers }
13971fa665f5SBrian Somers 
13981fa665f5SBrian Somers void
139996c9bb21SBrian Somers bundle_ReceiveDatalink(struct bundle *bundle, int s, struct sockaddr_un *sun)
14001fa665f5SBrian Somers {
140196c9bb21SBrian Somers   char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int)];
140296c9bb21SBrian Somers   struct cmsghdr *cmsg = (struct cmsghdr *)cmsgbuf;
140396c9bb21SBrian Somers   struct msghdr msg;
140496c9bb21SBrian Somers   struct iovec iov[SCATTER_SEGMENTS];
1405b7c5748eSBrian Somers   struct datalink *dl;
140696c9bb21SBrian Somers   int niov, link_fd, expect, f;
140785fd273aSBrian Somers   pid_t pid;
14086f384573SBrian Somers 
1409dd7e2610SBrian Somers   log_Printf(LogPHASE, "Receiving datalink\n");
14106f384573SBrian Somers 
141196c9bb21SBrian Somers   /* Create our scatter/gather array */
141296c9bb21SBrian Somers   niov = 1;
141396c9bb21SBrian Somers   iov[0].iov_len = strlen(Version) + 1;
141496c9bb21SBrian Somers   iov[0].iov_base = (char *)malloc(iov[0].iov_len);
141585fd273aSBrian Somers   if (datalink2iov(NULL, iov, &niov, sizeof iov / sizeof *iov, 0) == -1) {
141654cd8e13SBrian Somers     close(s);
14176f384573SBrian Somers     return;
141854cd8e13SBrian Somers   }
14196f384573SBrian Somers 
142085fd273aSBrian Somers   pid = getpid();
142185fd273aSBrian Somers   write(s, &pid, sizeof pid);
142285fd273aSBrian Somers 
142396c9bb21SBrian Somers   for (f = expect = 0; f < niov; f++)
142496c9bb21SBrian Somers     expect += iov[f].iov_len;
142596c9bb21SBrian Somers 
142696c9bb21SBrian Somers   /* Set up our message */
142796c9bb21SBrian Somers   cmsg->cmsg_len = sizeof cmsgbuf;
142896c9bb21SBrian Somers   cmsg->cmsg_level = SOL_SOCKET;
142954cd8e13SBrian Somers   cmsg->cmsg_type = 0;
143096c9bb21SBrian Somers 
143196c9bb21SBrian Somers   memset(&msg, '\0', sizeof msg);
143296c9bb21SBrian Somers   msg.msg_name = (caddr_t)sun;
143396c9bb21SBrian Somers   msg.msg_namelen = sizeof *sun;
143496c9bb21SBrian Somers   msg.msg_iov = iov;
143596c9bb21SBrian Somers   msg.msg_iovlen = niov;
143696c9bb21SBrian Somers   msg.msg_control = cmsgbuf;
143796c9bb21SBrian Somers   msg.msg_controllen = sizeof cmsgbuf;
143896c9bb21SBrian Somers 
143996c9bb21SBrian Somers   log_Printf(LogDEBUG, "Expecting %d scatter/gather bytes\n", expect);
144096c9bb21SBrian Somers   f = expect + 100;
144196c9bb21SBrian Somers   setsockopt(s, SOL_SOCKET, SO_RCVBUF, &f, sizeof f);
144296c9bb21SBrian Somers   if ((f = recvmsg(s, &msg, MSG_WAITALL)) != expect) {
144396c9bb21SBrian Somers     if (f == -1)
144496c9bb21SBrian Somers       log_Printf(LogERROR, "Failed recvmsg: %s\n", strerror(errno));
144596c9bb21SBrian Somers     else
144696c9bb21SBrian Somers       log_Printf(LogERROR, "Failed recvmsg: Got %d, not %d\n", f, expect);
144796c9bb21SBrian Somers     while (niov--)
144896c9bb21SBrian Somers       free(iov[niov].iov_base);
144954cd8e13SBrian Somers     close(s);
145096c9bb21SBrian Somers     return;
145196c9bb21SBrian Somers   }
145296c9bb21SBrian Somers 
1453c0d9a877SBrian Somers   write(s, "!", 1);	/* ACK */
1454c0d9a877SBrian Somers 
145554cd8e13SBrian Somers   if (cmsg->cmsg_type == SCM_RIGHTS) {
145654cd8e13SBrian Somers     /* We've successfully received an open file descriptor through our socket */
145754cd8e13SBrian Somers     log_Printf(LogDEBUG, "Receiving non-tty device\n");
145854cd8e13SBrian Somers     link_fd = *(int *)CMSG_DATA(cmsg);
145954cd8e13SBrian Somers   } else {
146054cd8e13SBrian Somers     /* It's a ``controlling'' tty device via CATPROG */
146154cd8e13SBrian Somers     log_Printf(LogDEBUG, "Receiving tty device\n");
146254cd8e13SBrian Somers     link_fd = dup(s);
146354cd8e13SBrian Somers     fcntl(link_fd, F_SETFL, fcntl(link_fd, F_GETFL, 0) | O_NONBLOCK);
146454cd8e13SBrian Somers   }
146554cd8e13SBrian Somers 
146696c9bb21SBrian Somers   if (strncmp(Version, iov[0].iov_base, iov[0].iov_len)) {
146796c9bb21SBrian Somers     log_Printf(LogWARN, "Cannot receive datalink, incorrect version"
146896c9bb21SBrian Somers                " (\"%.*s\", not \"%s\")\n", (int)iov[0].iov_len,
146996c9bb21SBrian Somers                iov[0].iov_base, Version);
147096c9bb21SBrian Somers     close(link_fd);
147196c9bb21SBrian Somers     while (niov--)
147296c9bb21SBrian Somers       free(iov[niov].iov_base);
147396c9bb21SBrian Somers     return;
147496c9bb21SBrian Somers   }
147596c9bb21SBrian Somers 
147696c9bb21SBrian Somers   niov = 1;
1477b7c5748eSBrian Somers   dl = iov2datalink(bundle, iov, &niov, sizeof iov / sizeof *iov, link_fd);
1478b7c5748eSBrian Somers   if (dl) {
147904eaa58cSBrian Somers     bundle_DatalinkLinkin(bundle, dl);
1480b7c5748eSBrian Somers     datalink_AuthOk(dl);
148196c9bb21SBrian Somers   } else
148296c9bb21SBrian Somers     close(link_fd);
148396c9bb21SBrian Somers 
148496c9bb21SBrian Somers   free(iov[0].iov_base);
148554cd8e13SBrian Somers   close(s);
14861fa665f5SBrian Somers }
14871fa665f5SBrian Somers 
14881fa665f5SBrian Somers void
148996c9bb21SBrian Somers bundle_SendDatalink(struct datalink *dl, int s, struct sockaddr_un *sun)
14901fa665f5SBrian Somers {
1491c0d9a877SBrian Somers   char cmsgbuf[sizeof(struct cmsghdr) + sizeof(int)], ack;
149296c9bb21SBrian Somers   struct cmsghdr *cmsg = (struct cmsghdr *)cmsgbuf;
149396c9bb21SBrian Somers   struct msghdr msg;
149496c9bb21SBrian Somers   struct iovec iov[SCATTER_SEGMENTS];
14951384bd27SBrian Somers   int niov, link_fd, f, expect, newsid;
149685fd273aSBrian Somers   pid_t newpid;
14976f384573SBrian Somers 
1498dd7e2610SBrian Somers   log_Printf(LogPHASE, "Transmitting datalink %s\n", dl->name);
14996f384573SBrian Somers 
150004eaa58cSBrian Somers   bundle_LinkClosed(dl->bundle, dl);
15010f2f3eb3SBrian Somers   bundle_DatalinkLinkout(dl->bundle, dl);
1502ea722969SBrian Somers 
150396c9bb21SBrian Somers   /* Build our scatter/gather array */
150496c9bb21SBrian Somers   iov[0].iov_len = strlen(Version) + 1;
150596c9bb21SBrian Somers   iov[0].iov_base = strdup(Version);
150696c9bb21SBrian Somers   niov = 1;
15076f384573SBrian Somers 
150885fd273aSBrian Somers   read(s, &newpid, sizeof newpid);
150985fd273aSBrian Somers   link_fd = datalink2iov(dl, iov, &niov, sizeof iov / sizeof *iov, newpid);
15106f384573SBrian Somers 
15116f384573SBrian Somers   if (link_fd != -1) {
151296c9bb21SBrian Somers     memset(&msg, '\0', sizeof msg);
151354cd8e13SBrian Somers 
151496c9bb21SBrian Somers     msg.msg_name = (caddr_t)sun;
151596c9bb21SBrian Somers     msg.msg_namelen = sizeof *sun;
151696c9bb21SBrian Somers     msg.msg_iov = iov;
151796c9bb21SBrian Somers     msg.msg_iovlen = niov;
151854cd8e13SBrian Somers 
151954cd8e13SBrian Somers     cmsg->cmsg_len = sizeof cmsgbuf;
152054cd8e13SBrian Somers     cmsg->cmsg_level = SOL_SOCKET;
152154cd8e13SBrian Somers     cmsg->cmsg_type = SCM_RIGHTS;
152254cd8e13SBrian Somers     *(int *)CMSG_DATA(cmsg) = link_fd;
152396c9bb21SBrian Somers     msg.msg_control = cmsgbuf;
152496c9bb21SBrian Somers     msg.msg_controllen = sizeof cmsgbuf;
152547723d29SBrian Somers 
152696c9bb21SBrian Somers     for (f = expect = 0; f < niov; f++)
152796c9bb21SBrian Somers       expect += iov[f].iov_len;
152847723d29SBrian Somers 
152996c9bb21SBrian Somers     log_Printf(LogDEBUG, "Sending %d bytes in scatter/gather array\n", expect);
153047723d29SBrian Somers 
153196c9bb21SBrian Somers     f = expect + SOCKET_OVERHEAD;
153296c9bb21SBrian Somers     setsockopt(s, SOL_SOCKET, SO_SNDBUF, &f, sizeof f);
153396c9bb21SBrian Somers     if (sendmsg(s, &msg, 0) == -1)
153496c9bb21SBrian Somers       log_Printf(LogERROR, "Failed sendmsg: %s\n", strerror(errno));
1535c0d9a877SBrian Somers     /* We must get the ACK before closing the descriptor ! */
1536c0d9a877SBrian Somers     read(s, &ack, 1);
153754cd8e13SBrian Somers 
15381384bd27SBrian Somers     newsid = tcgetpgrp(link_fd) == getpgrp();
153996c9bb21SBrian Somers     close(link_fd);
15401384bd27SBrian Somers     if (newsid)
15411384bd27SBrian Somers       bundle_setsid(dl->bundle, 1);
154247723d29SBrian Somers   }
154385fd273aSBrian Somers   close(s);
154496c9bb21SBrian Somers 
154596c9bb21SBrian Somers   while (niov--)
154696c9bb21SBrian Somers     free(iov[niov].iov_base);
15471fa665f5SBrian Somers }
1548dd0645c5SBrian Somers 
1549dd0645c5SBrian Somers int
155058d55334SBrian Somers bundle_RenameDatalink(struct bundle *bundle, struct datalink *ndl,
155158d55334SBrian Somers                       const char *name)
155258d55334SBrian Somers {
155358d55334SBrian Somers   struct datalink *dl;
155458d55334SBrian Somers 
155558d55334SBrian Somers   if (!strcasecmp(ndl->name, name))
155658d55334SBrian Somers     return 1;
155758d55334SBrian Somers 
155858d55334SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
155958d55334SBrian Somers     if (!strcasecmp(dl->name, name))
156058d55334SBrian Somers       return 0;
156158d55334SBrian Somers 
156258d55334SBrian Somers   datalink_Rename(ndl, name);
156358d55334SBrian Somers   return 1;
156458d55334SBrian Somers }
156558d55334SBrian Somers 
156658d55334SBrian Somers int
1567dd0645c5SBrian Somers bundle_SetMode(struct bundle *bundle, struct datalink *dl, int mode)
1568dd0645c5SBrian Somers {
1569dd0645c5SBrian Somers   int omode;
1570dd0645c5SBrian Somers 
1571dd0645c5SBrian Somers   omode = dl->physical->type;
1572dd0645c5SBrian Somers   if (omode == mode)
1573dd0645c5SBrian Somers     return 1;
1574dd0645c5SBrian Somers 
1575ff0f9439SBrian Somers   if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO))
1576ff0f9439SBrian Somers     /* First auto link */
1577dd0645c5SBrian Somers     if (bundle->ncp.ipcp.peer_ip.s_addr == INADDR_ANY) {
1578ff0f9439SBrian Somers       log_Printf(LogWARN, "You must `set ifaddr' or `open' before"
1579ff0f9439SBrian Somers                  " changing mode to %s\n", mode2Nam(mode));
1580dd0645c5SBrian Somers       return 0;
1581dd0645c5SBrian Somers     }
1582dd0645c5SBrian Somers 
1583dd0645c5SBrian Somers   if (!datalink_SetMode(dl, mode))
1584dd0645c5SBrian Somers     return 0;
1585dd0645c5SBrian Somers 
1586ff0f9439SBrian Somers   if (mode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO) &&
1587ff0f9439SBrian Somers       bundle->phase != PHASE_NETWORK)
1588ff0f9439SBrian Somers     /* First auto link, we need an interface */
1589dd0645c5SBrian Somers     ipcp_InterfaceUp(&bundle->ncp.ipcp);
1590dd0645c5SBrian Somers 
159104eaa58cSBrian Somers   /* Regenerate phys_type and adjust autoload & idle timers */
159204eaa58cSBrian Somers   bundle_LinksRemoved(bundle);
1593dd0645c5SBrian Somers 
1594ff0f9439SBrian Somers   if (omode == PHYS_AUTO && !(bundle->phys_type.all & PHYS_AUTO) &&
1595ff0f9439SBrian Somers       bundle->phase != PHASE_NETWORK)
1596ff0f9439SBrian Somers     /* No auto links left */
1597dd0645c5SBrian Somers     ipcp_CleanInterface(&bundle->ncp.ipcp);
1598dd0645c5SBrian Somers 
1599dd0645c5SBrian Somers   return 1;
1600dd0645c5SBrian Somers }
16011384bd27SBrian Somers 
16021384bd27SBrian Somers void
16031384bd27SBrian Somers bundle_setsid(struct bundle *bundle, int holdsession)
16041384bd27SBrian Somers {
16051384bd27SBrian Somers   /*
16061384bd27SBrian Somers    * Lose the current session.  This means getting rid of our pid
16071384bd27SBrian Somers    * too so that the tty device will really go away, and any getty
16081384bd27SBrian Somers    * etc will be allowed to restart.
16091384bd27SBrian Somers    */
16101384bd27SBrian Somers   pid_t pid, orig;
16111384bd27SBrian Somers   int fds[2];
16121384bd27SBrian Somers   char done;
16131384bd27SBrian Somers   struct datalink *dl;
16141384bd27SBrian Somers 
16151384bd27SBrian Somers   orig = getpid();
16161384bd27SBrian Somers   if (pipe(fds) == -1) {
16171384bd27SBrian Somers     log_Printf(LogERROR, "pipe: %s\n", strerror(errno));
16181384bd27SBrian Somers     return;
16191384bd27SBrian Somers   }
16201384bd27SBrian Somers   switch ((pid = fork())) {
16211384bd27SBrian Somers     case -1:
16221384bd27SBrian Somers       log_Printf(LogERROR, "fork: %s\n", strerror(errno));
16231384bd27SBrian Somers       close(fds[0]);
16241384bd27SBrian Somers       close(fds[1]);
16251384bd27SBrian Somers       return;
16261384bd27SBrian Somers     case 0:
16271384bd27SBrian Somers       close(fds[0]);
16281384bd27SBrian Somers       read(fds[1], &done, 1);		/* uu_locks are mine ! */
16291384bd27SBrian Somers       close(fds[1]);
16301384bd27SBrian Somers       if (pipe(fds) == -1) {
16311384bd27SBrian Somers         log_Printf(LogERROR, "pipe(2): %s\n", strerror(errno));
16321384bd27SBrian Somers         return;
16331384bd27SBrian Somers       }
16341384bd27SBrian Somers       switch ((pid = fork())) {
16351384bd27SBrian Somers         case -1:
16361384bd27SBrian Somers           log_Printf(LogERROR, "fork: %s\n", strerror(errno));
16371384bd27SBrian Somers           close(fds[0]);
16381384bd27SBrian Somers           close(fds[1]);
16391384bd27SBrian Somers           return;
16401384bd27SBrian Somers         case 0:
16411384bd27SBrian Somers           close(fds[0]);
16421384bd27SBrian Somers           bundle_LockTun(bundle);	/* update pid */
16431384bd27SBrian Somers           read(fds[1], &done, 1);	/* uu_locks are mine ! */
16441384bd27SBrian Somers           close(fds[1]);
16451384bd27SBrian Somers           setsid();
16461384bd27SBrian Somers           log_Printf(LogPHASE, "%d -> %d: %s session control\n",
16471384bd27SBrian Somers                      (int)orig, (int)getpid(),
16481384bd27SBrian Somers                      holdsession ? "Passed" : "Dropped");
16491384bd27SBrian Somers           break;
16501384bd27SBrian Somers         default:
16511384bd27SBrian Somers           close(fds[1]);
16521384bd27SBrian Somers           /* Give away all our modem locks (to the final process) */
16531384bd27SBrian Somers           for (dl = bundle->links; dl; dl = dl->next)
16541384bd27SBrian Somers             if (dl->state != DATALINK_CLOSED)
16551384bd27SBrian Somers               modem_ChangedPid(dl->physical, pid);
16561384bd27SBrian Somers           write(fds[0], "!", 1);	/* done */
16571384bd27SBrian Somers           close(fds[0]);
16581384bd27SBrian Somers           exit(0);
16591384bd27SBrian Somers           break;
16601384bd27SBrian Somers       }
16611384bd27SBrian Somers       break;
16621384bd27SBrian Somers     default:
16631384bd27SBrian Somers       close(fds[1]);
16641384bd27SBrian Somers       /* Give away all our modem locks (to the intermediate process) */
16651384bd27SBrian Somers       for (dl = bundle->links; dl; dl = dl->next)
16661384bd27SBrian Somers         if (dl->state != DATALINK_CLOSED)
16671384bd27SBrian Somers           modem_ChangedPid(dl->physical, pid);
16681384bd27SBrian Somers       write(fds[0], "!", 1);	/* done */
16691384bd27SBrian Somers       close(fds[0]);
16701384bd27SBrian Somers       if (holdsession) {
16711384bd27SBrian Somers         int fd, status;
16721384bd27SBrian Somers 
16731384bd27SBrian Somers         timer_TermService();
16741384bd27SBrian Somers         signal(SIGPIPE, SIG_DFL);
16751384bd27SBrian Somers         signal(SIGALRM, SIG_DFL);
16761384bd27SBrian Somers         signal(SIGHUP, SIG_DFL);
16771384bd27SBrian Somers         signal(SIGTERM, SIG_DFL);
16781384bd27SBrian Somers         signal(SIGINT, SIG_DFL);
16791384bd27SBrian Somers         signal(SIGQUIT, SIG_DFL);
16801384bd27SBrian Somers         for (fd = getdtablesize(); fd >= 0; fd--)
16811384bd27SBrian Somers           close(fd);
16821384bd27SBrian Somers         setuid(geteuid());
16831384bd27SBrian Somers         /*
16841384bd27SBrian Somers          * Reap the intermediate process.  As we're not exiting but the
16851384bd27SBrian Somers          * intermediate is, we don't want it to become defunct.
16861384bd27SBrian Somers          */
16871384bd27SBrian Somers         waitpid(pid, &status, 0);
16888e7b8599SBrian Somers         /* Tweak our process arguments.... */
16898e7b8599SBrian Somers         bundle->argv[0] = "session owner";
16908e7b8599SBrian Somers         bundle->argv[1] = NULL;
16911384bd27SBrian Somers         /*
16921384bd27SBrian Somers          * Hang around for a HUP.  This should happen as soon as the
16931384bd27SBrian Somers          * ppp that we passed our ctty descriptor to closes it.
16941384bd27SBrian Somers          * NOTE: If this process dies, the passed descriptor becomes
16951384bd27SBrian Somers          *       invalid and will give a select() error by setting one
16961384bd27SBrian Somers          *       of the error fds, aborting the other ppp.  We don't
16971384bd27SBrian Somers          *       want that to happen !
16981384bd27SBrian Somers          */
16991384bd27SBrian Somers         pause();
17001384bd27SBrian Somers       }
17011384bd27SBrian Somers       exit(0);
17021384bd27SBrian Somers       break;
17031384bd27SBrian Somers   }
17041384bd27SBrian Somers }
1705