xref: /freebsd/usr.sbin/ppp/bundle.c (revision 3006ec67fe3d15592a083f3ff7038db84742e9d2)
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  *
263006ec67SBrian Somers  *	$Id: bundle.c,v 1.1.2.8 1998/02/10 03:23:06 brian Exp $
277a6f8720SBrian Somers  */
287a6f8720SBrian Somers 
297a6f8720SBrian Somers #include <sys/param.h>
307a6f8720SBrian Somers #include <sys/time.h>
317a6f8720SBrian Somers #include <sys/socket.h>
327a6f8720SBrian Somers #include <netinet/in.h>
337a6f8720SBrian Somers #include <net/if.h>
347a6f8720SBrian Somers #include <arpa/inet.h>
357a6f8720SBrian Somers #include <net/route.h>
367a6f8720SBrian Somers #include <net/if_dl.h>
377a6f8720SBrian Somers 
387a6f8720SBrian Somers #include <errno.h>
397a6f8720SBrian Somers #include <fcntl.h>
407a6f8720SBrian Somers #include <stdio.h>
417a6f8720SBrian Somers #include <string.h>
427a6f8720SBrian Somers #include <sys/ioctl.h>
437a6f8720SBrian Somers #include <termios.h>
447a6f8720SBrian Somers #include <unistd.h>
457a6f8720SBrian Somers 
467a6f8720SBrian Somers #include "command.h"
477a6f8720SBrian Somers #include "mbuf.h"
487a6f8720SBrian Somers #include "log.h"
497a6f8720SBrian Somers #include "id.h"
507a6f8720SBrian Somers #include "defs.h"
517a6f8720SBrian Somers #include "timer.h"
527a6f8720SBrian Somers #include "fsm.h"
537a6f8720SBrian Somers #include "iplist.h"
54455aabc3SBrian Somers #include "hdlc.h"
557a6f8720SBrian Somers #include "throughput.h"
567a6f8720SBrian Somers #include "ipcp.h"
57455aabc3SBrian Somers #include "link.h"
587a6f8720SBrian Somers #include "bundle.h"
597a6f8720SBrian Somers #include "loadalias.h"
607a6f8720SBrian Somers #include "vars.h"
617a6f8720SBrian Somers #include "arp.h"
627a6f8720SBrian Somers #include "systems.h"
637a6f8720SBrian Somers #include "route.h"
647a6f8720SBrian Somers #include "lcp.h"
657a6f8720SBrian Somers #include "ccp.h"
66455aabc3SBrian Somers #include "async.h"
6742d4d396SBrian Somers #include "descriptor.h"
68455aabc3SBrian Somers #include "physical.h"
692289f246SBrian Somers #include "modem.h"
70455aabc3SBrian Somers #include "main.h"
71455aabc3SBrian Somers #include "auth.h"
72455aabc3SBrian Somers #include "lcpproto.h"
73455aabc3SBrian Somers #include "pap.h"
74455aabc3SBrian Somers #include "chap.h"
75455aabc3SBrian Somers #include "tun.h"
7685b542cfSBrian Somers #include "prompt.h"
773006ec67SBrian Somers #include "chat.h"
783006ec67SBrian Somers #include "datalink.h"
793006ec67SBrian Somers #include "ip.h"
807a6f8720SBrian Somers 
81455aabc3SBrian Somers static const char *PhaseNames[] = {
82455aabc3SBrian Somers   "Dead", "Establish", "Authenticate", "Network", "Terminate"
83455aabc3SBrian Somers };
84455aabc3SBrian Somers 
85455aabc3SBrian Somers const char *
86455aabc3SBrian Somers bundle_PhaseName(struct bundle *bundle)
877a6f8720SBrian Somers {
88455aabc3SBrian Somers   return bundle->phase <= PHASE_TERMINATE ?
89455aabc3SBrian Somers     PhaseNames[bundle->phase] : "unknown";
907a6f8720SBrian Somers }
917a6f8720SBrian Somers 
92455aabc3SBrian Somers void
93455aabc3SBrian Somers bundle_NewPhase(struct bundle *bundle, struct physical *physical, u_int new)
94455aabc3SBrian Somers {
95455aabc3SBrian Somers   if (new <= PHASE_NETWORK)
96455aabc3SBrian Somers     LogPrintf(LogPHASE, "bundle_NewPhase: %s\n", PhaseNames[new]);
977a6f8720SBrian Somers 
98455aabc3SBrian Somers   switch (new) {
99455aabc3SBrian Somers   case PHASE_DEAD:
100455aabc3SBrian Somers     bundle->phase = new;
101455aabc3SBrian Somers     break;
102455aabc3SBrian Somers 
103455aabc3SBrian Somers   case PHASE_ESTABLISH:
104455aabc3SBrian Somers     bundle->phase = new;
105455aabc3SBrian Somers     break;
106455aabc3SBrian Somers 
107455aabc3SBrian Somers   case PHASE_AUTHENTICATE:
108455aabc3SBrian Somers     LcpInfo.auth_ineed = LcpInfo.want_auth;
109455aabc3SBrian Somers     LcpInfo.auth_iwait = LcpInfo.his_auth;
110455aabc3SBrian Somers     if (LcpInfo.his_auth || LcpInfo.want_auth) {
111455aabc3SBrian Somers       LogPrintf(LogPHASE, " his = %s, mine = %s\n",
112455aabc3SBrian Somers                 Auth2Nam(LcpInfo.his_auth), Auth2Nam(LcpInfo.want_auth));
113455aabc3SBrian Somers        /* XXX-ML AuthPapInfo and AuthChapInfo must be allocated! */
114455aabc3SBrian Somers       if (LcpInfo.his_auth == PROTO_PAP)
115455aabc3SBrian Somers 	StartAuthChallenge(&AuthPapInfo, physical);
116455aabc3SBrian Somers       if (LcpInfo.want_auth == PROTO_CHAP)
117455aabc3SBrian Somers 	StartAuthChallenge(&AuthChapInfo, physical);
118455aabc3SBrian Somers       bundle->phase = new;
11985b542cfSBrian Somers       prompt_Display(&prompt, bundle);
120455aabc3SBrian Somers     } else
121455aabc3SBrian Somers       bundle_NewPhase(bundle, physical, PHASE_NETWORK);
122455aabc3SBrian Somers     break;
123455aabc3SBrian Somers 
124455aabc3SBrian Somers   case PHASE_NETWORK:
125455aabc3SBrian Somers     tun_configure(bundle, LcpInfo.his_mru, modem_Speed(physical));
1263006ec67SBrian Somers     IpcpInit(bundle, &physical->link);
127455aabc3SBrian Somers     IpcpUp();
128455aabc3SBrian Somers     IpcpOpen();
129455aabc3SBrian Somers     CcpUp();
130455aabc3SBrian Somers     CcpOpen();
131455aabc3SBrian Somers     /* Fall through */
132455aabc3SBrian Somers 
133455aabc3SBrian Somers   case PHASE_TERMINATE:
134455aabc3SBrian Somers     bundle->phase = new;
13585b542cfSBrian Somers     prompt_Display(&prompt, bundle);
136455aabc3SBrian Somers     break;
1377a6f8720SBrian Somers   }
1387a6f8720SBrian Somers }
1397a6f8720SBrian Somers 
1407a6f8720SBrian Somers static int
1417a6f8720SBrian Somers bundle_CleanInterface(const struct bundle *bundle)
1427a6f8720SBrian Somers {
1437a6f8720SBrian Somers   int s;
1447a6f8720SBrian Somers   struct ifreq ifrq;
1457a6f8720SBrian Somers   struct ifaliasreq ifra;
1467a6f8720SBrian Somers 
1477a6f8720SBrian Somers   s = ID0socket(AF_INET, SOCK_DGRAM, 0);
1487a6f8720SBrian Somers   if (s < 0) {
1497a6f8720SBrian Somers     LogPrintf(LogERROR, "bundle_CleanInterface: socket(): %s\n",
1507a6f8720SBrian Somers               strerror(errno));
1517a6f8720SBrian Somers     return (-1);
1527a6f8720SBrian Somers   }
1537a6f8720SBrian Somers   strncpy(ifrq.ifr_name, bundle->ifname, sizeof ifrq.ifr_name - 1);
1547a6f8720SBrian Somers   ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0';
1557a6f8720SBrian Somers   while (ID0ioctl(s, SIOCGIFADDR, &ifrq) == 0) {
1567a6f8720SBrian Somers     memset(&ifra.ifra_mask, '\0', sizeof ifra.ifra_mask);
1577a6f8720SBrian Somers     strncpy(ifra.ifra_name, bundle->ifname, sizeof ifra.ifra_name - 1);
1587a6f8720SBrian Somers     ifra.ifra_name[sizeof ifra.ifra_name - 1] = '\0';
1597a6f8720SBrian Somers     ifra.ifra_addr = ifrq.ifr_addr;
1607a6f8720SBrian Somers     if (ID0ioctl(s, SIOCGIFDSTADDR, &ifrq) < 0) {
1617a6f8720SBrian Somers       if (ifra.ifra_addr.sa_family == AF_INET)
1627a6f8720SBrian Somers         LogPrintf(LogERROR,
1637a6f8720SBrian Somers                   "bundle_CleanInterface: Can't get dst for %s on %s !\n",
1647a6f8720SBrian Somers                   inet_ntoa(((struct sockaddr_in *)&ifra.ifra_addr)->sin_addr),
1657a6f8720SBrian Somers                   bundle->ifname);
1667a6f8720SBrian Somers       return 0;
1677a6f8720SBrian Somers     }
1687a6f8720SBrian Somers     ifra.ifra_broadaddr = ifrq.ifr_dstaddr;
1697a6f8720SBrian Somers     if (ID0ioctl(s, SIOCDIFADDR, &ifra) < 0) {
1707a6f8720SBrian Somers       if (ifra.ifra_addr.sa_family == AF_INET)
1717a6f8720SBrian Somers         LogPrintf(LogERROR,
1727a6f8720SBrian Somers                   "bundle_CleanInterface: Can't delete %s address on %s !\n",
1737a6f8720SBrian Somers                   inet_ntoa(((struct sockaddr_in *)&ifra.ifra_addr)->sin_addr),
1747a6f8720SBrian Somers                   bundle->ifname);
1757a6f8720SBrian Somers       return 0;
1767a6f8720SBrian Somers     }
1777a6f8720SBrian Somers   }
1787a6f8720SBrian Somers 
1797a6f8720SBrian Somers   return 1;
1807a6f8720SBrian Somers }
1817a6f8720SBrian Somers 
182455aabc3SBrian Somers void
183455aabc3SBrian Somers bundle_LayerStart(struct bundle *bundle, struct fsm *fp)
1847a6f8720SBrian Somers {
1853006ec67SBrian Somers   /* The given FSM is about to start up ! */
186455aabc3SBrian Somers   if (fp == &LcpInfo.fsm)
187455aabc3SBrian Somers     bundle_NewPhase(bundle, link2physical(fp->link), PHASE_ESTABLISH);
1887a6f8720SBrian Somers }
1897a6f8720SBrian Somers 
1907a6f8720SBrian Somers void
191455aabc3SBrian Somers bundle_LayerUp(struct bundle *bundle, struct fsm *fp)
1927a6f8720SBrian Somers {
1933006ec67SBrian Somers   /*
1943006ec67SBrian Somers    * The given fsm is now up
1953006ec67SBrian Somers    * If it's the first datalink, bring all NCPs up.
1963006ec67SBrian Somers    */
1973006ec67SBrian Somers   if (fp == &LcpInfo.fsm)
198455aabc3SBrian Somers     bundle_NewPhase(bundle, link2physical(fp->link), PHASE_AUTHENTICATE);
199455aabc3SBrian Somers 
200455aabc3SBrian Somers   if (fp == &IpcpInfo.fsm)
2017a6f8720SBrian Somers     if (mode & MODE_BACKGROUND && BGFiledes[1] != -1) {
2027a6f8720SBrian Somers       char c = EX_NORMAL;
2037a6f8720SBrian Somers 
2047a6f8720SBrian Somers       if (write(BGFiledes[1], &c, 1) == 1)
2057a6f8720SBrian Somers 	LogPrintf(LogPHASE, "Parent notified of success.\n");
2067a6f8720SBrian Somers       else
2077a6f8720SBrian Somers 	LogPrintf(LogPHASE, "Failed to notify parent of success.\n");
2087a6f8720SBrian Somers       close(BGFiledes[1]);
2097a6f8720SBrian Somers       BGFiledes[1] = -1;
2107a6f8720SBrian Somers     }
2117a6f8720SBrian Somers }
2127a6f8720SBrian Somers 
2137a6f8720SBrian Somers int
2147a6f8720SBrian Somers bundle_LinkIsUp(const struct bundle *bundle)
2157a6f8720SBrian Somers {
216455aabc3SBrian Somers   return IpcpInfo.fsm.state == ST_OPENED;
2177a6f8720SBrian Somers }
2187a6f8720SBrian Somers 
2197a6f8720SBrian Somers void
2203006ec67SBrian Somers bundle_Close(struct bundle *bundle, const char *name, int staydown)
2217a6f8720SBrian Somers {
222455aabc3SBrian Somers   /*
2233006ec67SBrian Somers    * Please close the given datalink.
224455aabc3SBrian Somers    *
2253006ec67SBrian Somers    * If name == NULL or name is the last datalink, enter TERMINATE phase.
226455aabc3SBrian Somers    *
2273006ec67SBrian Somers    * If name == NULL, FsmClose all NCPs.
228455aabc3SBrian Somers    *
2293006ec67SBrian Somers    * If name is the last datalink, FsmClose all NCPs.
230455aabc3SBrian Somers    *
2313006ec67SBrian Somers    * If isn't the last datalink, just Close that datalink.
232455aabc3SBrian Somers    */
2337a6f8720SBrian Somers 
234455aabc3SBrian Somers   bundle_NewPhase(bundle, NULL, PHASE_TERMINATE);
2357a6f8720SBrian Somers   FsmClose(&IpcpInfo.fsm);
2363006ec67SBrian Somers   if (staydown) {
2373006ec67SBrian Somers     struct datalink *dl;
2383006ec67SBrian Somers 
2393006ec67SBrian Somers     for (dl = bundle->links; dl; dl = dl->next)
2403006ec67SBrian Somers       datalink_StayDown(dl);
2413006ec67SBrian Somers   }
2427a6f8720SBrian Somers }
2437a6f8720SBrian Somers 
2447a6f8720SBrian Somers /*
2457a6f8720SBrian Somers  *  Open tunnel device and returns its descriptor
2467a6f8720SBrian Somers  */
2477a6f8720SBrian Somers 
2487a6f8720SBrian Somers #define MAX_TUN 256
2497a6f8720SBrian Somers /*
2507a6f8720SBrian Somers  * MAX_TUN is set at 256 because that is the largest minor number
2517a6f8720SBrian Somers  * we can use (certainly with mknod(1) anyway.  The search for a
2527a6f8720SBrian Somers  * device aborts when it reaches the first `Device not configured'
2537a6f8720SBrian Somers  * (ENXIO) or the third `No such file or directory' (ENOENT) error.
2547a6f8720SBrian Somers  */
2557a6f8720SBrian Somers struct bundle *
2567a6f8720SBrian Somers bundle_Create(const char *prefix)
2577a6f8720SBrian Somers {
2587a6f8720SBrian Somers   int s, enoentcount, err;
2597a6f8720SBrian Somers   struct ifreq ifrq;
2607a6f8720SBrian Somers   static struct bundle bundle;		/* there can be only one */
2617a6f8720SBrian Somers 
2627a6f8720SBrian Somers   if (bundle.ifname != NULL) {	/* Already allocated ! */
2637a6f8720SBrian Somers     LogPrintf(LogERROR, "bundle_Create:  There's only one BUNDLE !\n");
2647a6f8720SBrian Somers     return NULL;
2657a6f8720SBrian Somers   }
2667a6f8720SBrian Somers 
2677a6f8720SBrian Somers   err = ENOENT;
2687a6f8720SBrian Somers   enoentcount = 0;
2697a6f8720SBrian Somers   for (bundle.unit = 0; bundle.unit <= MAX_TUN; bundle.unit++) {
2707a6f8720SBrian Somers     snprintf(bundle.dev, sizeof bundle.dev, "%s%d", prefix, bundle.unit);
2717a6f8720SBrian Somers     bundle.tun_fd = ID0open(bundle.dev, O_RDWR);
2727a6f8720SBrian Somers     if (bundle.tun_fd >= 0)
2737a6f8720SBrian Somers       break;
2747a6f8720SBrian Somers     if (errno == ENXIO) {
2757a6f8720SBrian Somers       bundle.unit = MAX_TUN;
2767a6f8720SBrian Somers       err = errno;
2777a6f8720SBrian Somers     } else if (errno == ENOENT) {
2787a6f8720SBrian Somers       if (++enoentcount > 2)
2797a6f8720SBrian Somers 	bundle.unit = MAX_TUN;
2807a6f8720SBrian Somers     } else
2817a6f8720SBrian Somers       err = errno;
2827a6f8720SBrian Somers   }
2837a6f8720SBrian Somers 
2847a6f8720SBrian Somers   if (bundle.unit > MAX_TUN) {
28585b542cfSBrian Somers     prompt_Printf(&prompt, "No tunnel device is available (%s).\n",
28685b542cfSBrian Somers                   strerror(err));
2877a6f8720SBrian Somers     return NULL;
2887a6f8720SBrian Somers   }
2897a6f8720SBrian Somers 
2907a6f8720SBrian Somers   LogSetTun(bundle.unit);
2917a6f8720SBrian Somers 
2927a6f8720SBrian Somers   s = socket(AF_INET, SOCK_DGRAM, 0);
2937a6f8720SBrian Somers   if (s < 0) {
2947a6f8720SBrian Somers     LogPrintf(LogERROR, "bundle_Create: socket(): %s\n", strerror(errno));
2957a6f8720SBrian Somers     close(bundle.tun_fd);
2967a6f8720SBrian Somers     return NULL;
2977a6f8720SBrian Somers   }
2987a6f8720SBrian Somers 
2997a6f8720SBrian Somers   bundle.ifname = strrchr(bundle.dev, '/');
3007a6f8720SBrian Somers   if (bundle.ifname == NULL)
3017a6f8720SBrian Somers     bundle.ifname = bundle.dev;
3027a6f8720SBrian Somers   else
3037a6f8720SBrian Somers     bundle.ifname++;
3047a6f8720SBrian Somers 
3057a6f8720SBrian Somers   /*
3067a6f8720SBrian Somers    * Now, bring up the interface.
3077a6f8720SBrian Somers    */
3087a6f8720SBrian Somers   memset(&ifrq, '\0', sizeof ifrq);
3097a6f8720SBrian Somers   strncpy(ifrq.ifr_name, bundle.ifname, sizeof ifrq.ifr_name - 1);
3107a6f8720SBrian Somers   ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0';
3117a6f8720SBrian Somers   if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
3127a6f8720SBrian Somers     LogPrintf(LogERROR, "OpenTunnel: ioctl(SIOCGIFFLAGS): %s\n",
3137a6f8720SBrian Somers 	      strerror(errno));
3147a6f8720SBrian Somers     close(s);
3157a6f8720SBrian Somers     close(bundle.tun_fd);
3167a6f8720SBrian Somers     bundle.ifname = NULL;
3177a6f8720SBrian Somers     return NULL;
3187a6f8720SBrian Somers   }
3197a6f8720SBrian Somers   ifrq.ifr_flags |= IFF_UP;
3207a6f8720SBrian Somers   if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) {
3217a6f8720SBrian Somers     LogPrintf(LogERROR, "OpenTunnel: ioctl(SIOCSIFFLAGS): %s\n",
3227a6f8720SBrian Somers 	      strerror(errno));
3237a6f8720SBrian Somers     close(s);
3247a6f8720SBrian Somers     close(bundle.tun_fd);
3257a6f8720SBrian Somers     bundle.ifname = NULL;
3267a6f8720SBrian Somers     return NULL;
3277a6f8720SBrian Somers   }
3287a6f8720SBrian Somers 
3297a6f8720SBrian Somers   close(s);
3307a6f8720SBrian Somers 
3317a6f8720SBrian Somers   if ((bundle.ifIndex = GetIfIndex(bundle.ifname)) < 0) {
3327a6f8720SBrian Somers     LogPrintf(LogERROR, "OpenTunnel: Can't find ifindex.\n");
3337a6f8720SBrian Somers     close(bundle.tun_fd);
3347a6f8720SBrian Somers     bundle.ifname = NULL;
3357a6f8720SBrian Somers     return NULL;
3367a6f8720SBrian Somers   }
3377a6f8720SBrian Somers 
33885b542cfSBrian Somers   prompt_Printf(&prompt, "Using interface: %s\n", bundle.ifname);
3397a6f8720SBrian Somers   LogPrintf(LogPHASE, "Using interface: %s\n", bundle.ifname);
3407a6f8720SBrian Somers 
341820de6ebSBrian Somers   bundle.routing_seq = 0;
342455aabc3SBrian Somers   bundle.phase = 0;
3437a6f8720SBrian Somers 
3447a6f8720SBrian Somers   /* Clean out any leftover crud */
3457a6f8720SBrian Somers   bundle_CleanInterface(&bundle);
3467a6f8720SBrian Somers 
3473006ec67SBrian Somers   bundle.links = datalink_Create("Modem", &bundle);
3483006ec67SBrian Somers   if (bundle.links == NULL) {
3493006ec67SBrian Somers     LogPrintf(LogERROR, "Cannot create data link: %s\n", strerror(errno));
3502289f246SBrian Somers     return NULL;
3512289f246SBrian Somers   }
3522289f246SBrian Somers 
3533006ec67SBrian Somers   IpcpInit(&bundle, &bundle.links->physical->link);
35468a0f0ccSBrian Somers 
3557a6f8720SBrian Somers   return &bundle;
3567a6f8720SBrian Somers }
3577a6f8720SBrian Somers 
35868a0f0ccSBrian Somers static void
35968a0f0ccSBrian Somers bundle_DownInterface(struct bundle *bundle)
36068a0f0ccSBrian Somers {
36168a0f0ccSBrian Somers   struct ifreq ifrq;
36268a0f0ccSBrian Somers   int s;
36368a0f0ccSBrian Somers 
36468a0f0ccSBrian Somers   DeleteIfRoutes(bundle, 1);
36568a0f0ccSBrian Somers 
36668a0f0ccSBrian Somers   s = ID0socket(AF_INET, SOCK_DGRAM, 0);
36768a0f0ccSBrian Somers   if (s < 0) {
36868a0f0ccSBrian Somers     LogPrintf(LogERROR, "bundle_DownInterface: socket: %s\n", strerror(errno));
36968a0f0ccSBrian Somers     return;
37068a0f0ccSBrian Somers   }
37168a0f0ccSBrian Somers 
37268a0f0ccSBrian Somers   memset(&ifrq, '\0', sizeof ifrq);
37368a0f0ccSBrian Somers   strncpy(ifrq.ifr_name, bundle->ifname, sizeof ifrq.ifr_name - 1);
37468a0f0ccSBrian Somers   ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0';
37568a0f0ccSBrian Somers   if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
37668a0f0ccSBrian Somers     LogPrintf(LogERROR, "bundle_DownInterface: ioctl(SIOCGIFFLAGS): %s\n",
37768a0f0ccSBrian Somers        strerror(errno));
37868a0f0ccSBrian Somers     close(s);
37968a0f0ccSBrian Somers     return;
38068a0f0ccSBrian Somers   }
38168a0f0ccSBrian Somers   ifrq.ifr_flags &= ~IFF_UP;
38268a0f0ccSBrian Somers   if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) {
38368a0f0ccSBrian Somers     LogPrintf(LogERROR, "bundle_DownInterface: ioctl(SIOCSIFFLAGS): %s\n",
38468a0f0ccSBrian Somers        strerror(errno));
38568a0f0ccSBrian Somers     close(s);
38668a0f0ccSBrian Somers     return;
38768a0f0ccSBrian Somers   }
38868a0f0ccSBrian Somers   close(s);
38968a0f0ccSBrian Somers }
39068a0f0ccSBrian Somers 
39168a0f0ccSBrian Somers void
39268a0f0ccSBrian Somers bundle_Destroy(struct bundle *bundle)
39368a0f0ccSBrian Somers {
3943006ec67SBrian Somers   struct datalink *dl;
3953006ec67SBrian Somers 
39668a0f0ccSBrian Somers   if (mode & MODE_AUTO) {
39768a0f0ccSBrian Somers     IpcpCleanInterface(&IpcpInfo.fsm);
39868a0f0ccSBrian Somers     bundle_DownInterface(bundle);
39968a0f0ccSBrian Somers   }
4003006ec67SBrian Somers 
4013006ec67SBrian Somers   dl = bundle->links;
4023006ec67SBrian Somers   while (dl)
4033006ec67SBrian Somers     dl = datalink_Destroy(dl);
4043006ec67SBrian Somers 
40568a0f0ccSBrian Somers   bundle->ifname = NULL;
40668a0f0ccSBrian Somers }
40768a0f0ccSBrian Somers 
4087a6f8720SBrian Somers struct rtmsg {
4097a6f8720SBrian Somers   struct rt_msghdr m_rtm;
4107a6f8720SBrian Somers   char m_space[64];
4117a6f8720SBrian Somers };
4127a6f8720SBrian Somers 
4137a6f8720SBrian Somers void
414820de6ebSBrian Somers bundle_SetRoute(struct bundle *bundle, int cmd, struct in_addr dst,
4157a6f8720SBrian Somers                 struct in_addr gateway, struct in_addr mask, int bang)
4167a6f8720SBrian Somers {
4177a6f8720SBrian Somers   struct rtmsg rtmes;
4187a6f8720SBrian Somers   int s, nb, wb;
4197a6f8720SBrian Somers   char *cp;
4207a6f8720SBrian Somers   const char *cmdstr;
4217a6f8720SBrian Somers   struct sockaddr_in rtdata;
4227a6f8720SBrian Somers 
4237a6f8720SBrian Somers   if (bang)
4247a6f8720SBrian Somers     cmdstr = (cmd == RTM_ADD ? "Add!" : "Delete!");
4257a6f8720SBrian Somers   else
4267a6f8720SBrian Somers     cmdstr = (cmd == RTM_ADD ? "Add" : "Delete");
4277a6f8720SBrian Somers   s = ID0socket(PF_ROUTE, SOCK_RAW, 0);
4287a6f8720SBrian Somers   if (s < 0) {
42968a0f0ccSBrian Somers     LogPrintf(LogERROR, "bundle_SetRoute: socket(): %s\n", strerror(errno));
4307a6f8720SBrian Somers     return;
4317a6f8720SBrian Somers   }
4327a6f8720SBrian Somers   memset(&rtmes, '\0', sizeof rtmes);
4337a6f8720SBrian Somers   rtmes.m_rtm.rtm_version = RTM_VERSION;
4347a6f8720SBrian Somers   rtmes.m_rtm.rtm_type = cmd;
4357a6f8720SBrian Somers   rtmes.m_rtm.rtm_addrs = RTA_DST;
436820de6ebSBrian Somers   rtmes.m_rtm.rtm_seq = ++bundle->routing_seq;
4377a6f8720SBrian Somers   rtmes.m_rtm.rtm_pid = getpid();
4387a6f8720SBrian Somers   rtmes.m_rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;
4397a6f8720SBrian Somers 
4407a6f8720SBrian Somers   memset(&rtdata, '\0', sizeof rtdata);
4417a6f8720SBrian Somers   rtdata.sin_len = 16;
4427a6f8720SBrian Somers   rtdata.sin_family = AF_INET;
4437a6f8720SBrian Somers   rtdata.sin_port = 0;
4447a6f8720SBrian Somers   rtdata.sin_addr = dst;
4457a6f8720SBrian Somers 
4467a6f8720SBrian Somers   cp = rtmes.m_space;
4477a6f8720SBrian Somers   memcpy(cp, &rtdata, 16);
4487a6f8720SBrian Somers   cp += 16;
4497a6f8720SBrian Somers   if (cmd == RTM_ADD)
4507a6f8720SBrian Somers     if (gateway.s_addr == INADDR_ANY) {
4517a6f8720SBrian Somers       /* Add a route through the interface */
4527a6f8720SBrian Somers       struct sockaddr_dl dl;
4537a6f8720SBrian Somers       const char *iname;
4547a6f8720SBrian Somers       int ilen;
4557a6f8720SBrian Somers 
4567a6f8720SBrian Somers       iname = Index2Nam(bundle->ifIndex);
4577a6f8720SBrian Somers       ilen = strlen(iname);
4587a6f8720SBrian Somers       dl.sdl_len = sizeof dl - sizeof dl.sdl_data + ilen;
4597a6f8720SBrian Somers       dl.sdl_family = AF_LINK;
4607a6f8720SBrian Somers       dl.sdl_index = bundle->ifIndex;
4617a6f8720SBrian Somers       dl.sdl_type = 0;
4627a6f8720SBrian Somers       dl.sdl_nlen = ilen;
4637a6f8720SBrian Somers       dl.sdl_alen = 0;
4647a6f8720SBrian Somers       dl.sdl_slen = 0;
4657a6f8720SBrian Somers       strncpy(dl.sdl_data, iname, sizeof dl.sdl_data);
4667a6f8720SBrian Somers       memcpy(cp, &dl, dl.sdl_len);
4677a6f8720SBrian Somers       cp += dl.sdl_len;
4687a6f8720SBrian Somers       rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY;
4697a6f8720SBrian Somers     } else {
4707a6f8720SBrian Somers       rtdata.sin_addr = gateway;
4717a6f8720SBrian Somers       memcpy(cp, &rtdata, 16);
4727a6f8720SBrian Somers       cp += 16;
4737a6f8720SBrian Somers       rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY;
4747a6f8720SBrian Somers     }
4757a6f8720SBrian Somers 
4767a6f8720SBrian Somers   if (dst.s_addr == INADDR_ANY)
4777a6f8720SBrian Somers     mask.s_addr = INADDR_ANY;
4787a6f8720SBrian Somers 
4797a6f8720SBrian Somers   if (cmd == RTM_ADD || dst.s_addr == INADDR_ANY) {
4807a6f8720SBrian Somers     rtdata.sin_addr = mask;
4817a6f8720SBrian Somers     memcpy(cp, &rtdata, 16);
4827a6f8720SBrian Somers     cp += 16;
4837a6f8720SBrian Somers     rtmes.m_rtm.rtm_addrs |= RTA_NETMASK;
4847a6f8720SBrian Somers   }
4857a6f8720SBrian Somers 
4867a6f8720SBrian Somers   nb = cp - (char *) &rtmes;
4877a6f8720SBrian Somers   rtmes.m_rtm.rtm_msglen = nb;
4887a6f8720SBrian Somers   wb = ID0write(s, &rtmes, nb);
4897a6f8720SBrian Somers   if (wb < 0) {
49068a0f0ccSBrian Somers     LogPrintf(LogTCPIP, "bundle_SetRoute failure:\n");
49168a0f0ccSBrian Somers     LogPrintf(LogTCPIP, "bundle_SetRoute:  Cmd = %s\n", cmd);
49268a0f0ccSBrian Somers     LogPrintf(LogTCPIP, "bundle_SetRoute:  Dst = %s\n", inet_ntoa(dst));
49368a0f0ccSBrian Somers     LogPrintf(LogTCPIP, "bundle_SetRoute:  Gateway = %s\n", inet_ntoa(gateway));
49468a0f0ccSBrian Somers     LogPrintf(LogTCPIP, "bundle_SetRoute:  Mask = %s\n", inet_ntoa(mask));
4957a6f8720SBrian Somers failed:
4967a6f8720SBrian Somers     if (cmd == RTM_ADD && (rtmes.m_rtm.rtm_errno == EEXIST ||
4977a6f8720SBrian Somers                            (rtmes.m_rtm.rtm_errno == 0 && errno == EEXIST)))
4987a6f8720SBrian Somers       if (!bang)
4997a6f8720SBrian Somers         LogPrintf(LogWARN, "Add route failed: %s already exists\n",
5007a6f8720SBrian Somers                   inet_ntoa(dst));
5017a6f8720SBrian Somers       else {
5027a6f8720SBrian Somers         rtmes.m_rtm.rtm_type = cmd = RTM_CHANGE;
5037a6f8720SBrian Somers         if ((wb = ID0write(s, &rtmes, nb)) < 0)
5047a6f8720SBrian Somers           goto failed;
5057a6f8720SBrian Somers       }
5067a6f8720SBrian Somers     else if (cmd == RTM_DELETE &&
5077a6f8720SBrian Somers              (rtmes.m_rtm.rtm_errno == ESRCH ||
5087a6f8720SBrian Somers               (rtmes.m_rtm.rtm_errno == 0 && errno == ESRCH))) {
5097a6f8720SBrian Somers       if (!bang)
5107a6f8720SBrian Somers         LogPrintf(LogWARN, "Del route failed: %s: Non-existent\n",
5117a6f8720SBrian Somers                   inet_ntoa(dst));
5127a6f8720SBrian Somers     } else if (rtmes.m_rtm.rtm_errno == 0)
5137a6f8720SBrian Somers       LogPrintf(LogWARN, "%s route failed: %s: errno: %s\n", cmdstr,
5147a6f8720SBrian Somers                 inet_ntoa(dst), strerror(errno));
5157a6f8720SBrian Somers     else
5167a6f8720SBrian Somers       LogPrintf(LogWARN, "%s route failed: %s: %s\n",
5177a6f8720SBrian Somers 		cmdstr, inet_ntoa(dst), strerror(rtmes.m_rtm.rtm_errno));
5187a6f8720SBrian Somers   }
5197a6f8720SBrian Somers   LogPrintf(LogDEBUG, "wrote %d: cmd = %s, dst = %x, gateway = %x\n",
5207a6f8720SBrian Somers             wb, cmdstr, dst.s_addr, gateway.s_addr);
5217a6f8720SBrian Somers   close(s);
5227a6f8720SBrian Somers }
52383d1af55SBrian Somers 
52483d1af55SBrian Somers void
5253006ec67SBrian Somers bundle_LinkLost(struct bundle *bundle, struct link *link, int staydown)
52683d1af55SBrian Somers {
527455aabc3SBrian Somers   /*
5283006ec67SBrian Somers    * Locate the appropriate datalink, and Down it.
5293006ec67SBrian Somers    *
5303006ec67SBrian Somers    * The LayerFinish() called from the datalinks LCP will
5313006ec67SBrian Somers    * potentially Down our NCPs (if it's the last link).
5323006ec67SBrian Somers    *
5333006ec67SBrian Somers    * The LinkClosed() called when the datalink is finally in
5343006ec67SBrian Somers    * the CLOSED state MAY cause the entire datalink to be deleted
5353006ec67SBrian Somers    * and MAY cause a program exit.
536455aabc3SBrian Somers    */
53783d1af55SBrian Somers 
5383006ec67SBrian Somers   datalink_Down(bundle->links, staydown);
5393006ec67SBrian Somers }
5403006ec67SBrian Somers 
5413006ec67SBrian Somers void
5423006ec67SBrian Somers bundle_LinkClosed(struct bundle *bundle, struct datalink *dl)
5433006ec67SBrian Somers {
5443006ec67SBrian Somers   /*
5453006ec67SBrian Somers    * Our datalink has closed.
5463006ec67SBrian Somers    * If it's DIRECT, delete it.
5473006ec67SBrian Somers    * If it's the last data link,
5483006ec67SBrian Somers    */
5493006ec67SBrian Somers   if (!(mode & MODE_AUTO))
5503006ec67SBrian Somers     bundle_DownInterface(bundle);
5513006ec67SBrian Somers   if (mode & MODE_DDIAL)
5523006ec67SBrian Somers     datalink_Up(dl);
5533006ec67SBrian Somers   else
5543006ec67SBrian Somers     bundle_NewPhase(bundle, NULL, PHASE_DEAD);
555455aabc3SBrian Somers }
556455aabc3SBrian Somers 
557455aabc3SBrian Somers void
558455aabc3SBrian Somers bundle_LayerDown(struct bundle *bundle, struct fsm *fp)
559455aabc3SBrian Somers {
560455aabc3SBrian Somers   /*
561455aabc3SBrian Somers    * The given FSM has been told to come down.
562455aabc3SBrian Somers    * We don't do anything here, as the FSM will eventually
563455aabc3SBrian Somers    * come up or down and will call LayerUp or LayerFinish.
564455aabc3SBrian Somers    */
565455aabc3SBrian Somers }
566455aabc3SBrian Somers 
567455aabc3SBrian Somers void
568455aabc3SBrian Somers bundle_LayerFinish(struct bundle *bundle, struct fsm *fp)
569455aabc3SBrian Somers {
570455aabc3SBrian Somers   /* The given fsm is now down (fp cannot be NULL)
571455aabc3SBrian Somers    *
572455aabc3SBrian Somers    * If it's a CCP, just bring it back to STARTING in case we get more REQs
5733006ec67SBrian Somers    *
5743006ec67SBrian Somers    * If it's an LCP, FsmDown the corresponding CCP and Close the link if
5753006ec67SBrian Somers    * it's open.  The link_Close causes the LCP to be FsmDown()d,
5763006ec67SBrian Somers    * via bundle_LinkLost() causing re-entry.
5773006ec67SBrian Somers    *
578455aabc3SBrian Somers    * If it's the last LCP, FsmDown all NCPs
5793006ec67SBrian Somers    *
580455aabc3SBrian Somers    * If it's the last NCP, FsmClose all LCPs and enter TERMINATE phase.
581455aabc3SBrian Somers    */
582455aabc3SBrian Somers 
583455aabc3SBrian Somers   if (fp == &CcpInfo.fsm) {
58483d1af55SBrian Somers     FsmDown(&CcpInfo.fsm);
585455aabc3SBrian Somers     FsmOpen(&CcpInfo.fsm);
586455aabc3SBrian Somers   } else if (fp == &LcpInfo.fsm) {
587455aabc3SBrian Somers     FsmDown(&CcpInfo.fsm);
588455aabc3SBrian Somers 
589455aabc3SBrian Somers     FsmDown(&IpcpInfo.fsm);		/* You've lost your underlings */
590455aabc3SBrian Somers     FsmClose(&IpcpInfo.fsm);		/* ST_INITIAL please */
591455aabc3SBrian Somers 
592455aabc3SBrian Somers     if (link_IsActive(fp->link))
5933006ec67SBrian Somers       link_Close(fp->link, bundle, 0, 0);	/* clean shutdown */
5943006ec67SBrian Somers       /* And wait for the LinkLost() */
595455aabc3SBrian Somers   } else if (fp == &IpcpInfo.fsm) {
5963006ec67SBrian Somers     struct datalink *dl;
5973006ec67SBrian Somers 
598455aabc3SBrian Somers     if (fp->bundle->phase != PHASE_TERMINATE)
599455aabc3SBrian Somers       bundle_NewPhase(bundle, NULL, PHASE_TERMINATE);
6003006ec67SBrian Somers 
6013006ec67SBrian Somers     for (dl = bundle->links; dl; dl = dl->next)
6023006ec67SBrian Somers       datalink_Close(dl, 1);
603455aabc3SBrian Somers   }
60483d1af55SBrian Somers }
6053006ec67SBrian Somers 
6063006ec67SBrian Somers void
6073006ec67SBrian Somers bundle_Open(struct bundle *bundle, const char *name)
6083006ec67SBrian Somers {
6093006ec67SBrian Somers   /*
6103006ec67SBrian Somers    * Please open the given datalink, or all if name == NULL
6113006ec67SBrian Somers    */
6123006ec67SBrian Somers   struct datalink *dl;
6133006ec67SBrian Somers 
6143006ec67SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
6153006ec67SBrian Somers     if (name == NULL || !strcasecmp(dl->name, name)) {
6163006ec67SBrian Somers       datalink_Up(dl);
6173006ec67SBrian Somers       if (name != NULL)
6183006ec67SBrian Somers         break;
6193006ec67SBrian Somers     }
6203006ec67SBrian Somers }
6213006ec67SBrian Somers 
6223006ec67SBrian Somers struct datalink *
6233006ec67SBrian Somers bundle2datalink(struct bundle *bundle, const char *name)
6243006ec67SBrian Somers {
6253006ec67SBrian Somers   struct datalink *dl;
6263006ec67SBrian Somers 
6273006ec67SBrian Somers   if (name != NULL) {
6283006ec67SBrian Somers     for (dl = bundle->links; dl; dl = dl->next)
6293006ec67SBrian Somers       if (!strcasecmp(dl->name, name))
6303006ec67SBrian Somers         return dl;
6313006ec67SBrian Somers   } else if (bundle->links && !bundle->links->next)
6323006ec67SBrian Somers     return bundle->links;
6333006ec67SBrian Somers 
6343006ec67SBrian Somers   return NULL;
6353006ec67SBrian Somers }
6363006ec67SBrian Somers 
6373006ec67SBrian Somers struct physical *
6383006ec67SBrian Somers bundle2physical(struct bundle *bundle, const char *name)
6393006ec67SBrian Somers {
6403006ec67SBrian Somers   struct datalink *dl = bundle2datalink(bundle, name);
6413006ec67SBrian Somers   return dl ? dl->physical : NULL;
6423006ec67SBrian Somers }
6433006ec67SBrian Somers 
6443006ec67SBrian Somers struct link *
6453006ec67SBrian Somers bundle2link(struct bundle *bundle, const char *name)
6463006ec67SBrian Somers {
6473006ec67SBrian Somers   struct physical *physical = bundle2physical(bundle, name);
6483006ec67SBrian Somers   return physical ? &physical->link : NULL;
6493006ec67SBrian Somers }
6503006ec67SBrian Somers 
6513006ec67SBrian Somers int
6523006ec67SBrian Somers bundle_UpdateSet(struct bundle *bundle, fd_set *r, fd_set *w, fd_set *e, int *n)
6533006ec67SBrian Somers {
6543006ec67SBrian Somers   struct datalink *dl;
6553006ec67SBrian Somers   int result;
6563006ec67SBrian Somers 
6573006ec67SBrian Somers   result = 0;
6583006ec67SBrian Somers   for (dl = bundle->links; dl; dl = dl->next)
6593006ec67SBrian Somers     result += descriptor_UpdateSet(&dl->desc, r, w, e, n);
6603006ec67SBrian Somers 
6613006ec67SBrian Somers   return result;
6623006ec67SBrian Somers }
6633006ec67SBrian Somers 
6643006ec67SBrian Somers int
6653006ec67SBrian Somers bundle_FillQueues(struct bundle *bundle)
6663006ec67SBrian Somers {
6673006ec67SBrian Somers   struct datalink *dl;
6683006ec67SBrian Somers   int packets, total;
6693006ec67SBrian Somers 
6703006ec67SBrian Somers   total = 0;
6713006ec67SBrian Somers   for (dl = bundle->links; dl; dl = dl->next) {
6723006ec67SBrian Somers     packets = link_QueueLen(&dl->physical->link);
6733006ec67SBrian Somers     if (packets == 0) {
6743006ec67SBrian Somers       IpStartOutput(&dl->physical->link);
6753006ec67SBrian Somers       packets = link_QueueLen(&dl->physical->link);
6763006ec67SBrian Somers     }
6773006ec67SBrian Somers     total += packets;
6783006ec67SBrian Somers   }
6793006ec67SBrian Somers 
6803006ec67SBrian Somers   return total;
6813006ec67SBrian Somers }
682