xref: /freebsd/usr.sbin/ppp/main.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
165309e5cSBrian Somers /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni  *
465309e5cSBrian Somers  * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org>
565309e5cSBrian Somers  *          based on work by Toshiharu OHNO <tony-o@iij.ad.jp>
665309e5cSBrian Somers  *                           Internet Initiative Japan, Inc (IIJ)
765309e5cSBrian Somers  * All rights reserved.
8af57ed9fSAtsushi Murai  *
965309e5cSBrian Somers  * Redistribution and use in source and binary forms, with or without
1065309e5cSBrian Somers  * modification, are permitted provided that the following conditions
1165309e5cSBrian Somers  * are met:
1265309e5cSBrian Somers  * 1. Redistributions of source code must retain the above copyright
1365309e5cSBrian Somers  *    notice, this list of conditions and the following disclaimer.
1465309e5cSBrian Somers  * 2. Redistributions in binary form must reproduce the above copyright
1565309e5cSBrian Somers  *    notice, this list of conditions and the following disclaimer in the
1665309e5cSBrian Somers  *    documentation and/or other materials provided with the distribution.
17af57ed9fSAtsushi Murai  *
1865309e5cSBrian Somers  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1965309e5cSBrian Somers  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2065309e5cSBrian Somers  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2165309e5cSBrian Somers  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2265309e5cSBrian Somers  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2365309e5cSBrian Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2465309e5cSBrian Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2565309e5cSBrian Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2665309e5cSBrian Somers  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2765309e5cSBrian Somers  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2865309e5cSBrian Somers  * SUCH DAMAGE.
29af57ed9fSAtsushi Murai  */
302764b86aSBrian Somers 
31972a1bcfSBrian Somers #include <sys/param.h>
3275240ed1SBrian Somers #include <netinet/in.h>
33a9f484e5SJordan K. Hubbard #include <netinet/in_systm.h>
34a9f484e5SJordan K. Hubbard #include <netinet/ip.h>
35565e35e5SBrian Somers #include <sys/un.h>
366b457978SBrian Somers #include <sys/socket.h>
376b457978SBrian Somers #include <net/route.h>
3875240ed1SBrian Somers 
3975240ed1SBrian Somers #include <errno.h>
4075240ed1SBrian Somers #include <fcntl.h>
4175240ed1SBrian Somers #include <paths.h>
4275240ed1SBrian Somers #include <signal.h>
436eafd353SBrian Somers #include <stdarg.h>
4475240ed1SBrian Somers #include <stdio.h>
45c0593e34SBrian Somers #include <stdlib.h>
4675240ed1SBrian Somers #include <string.h>
47b08bf2deSBrian Somers #include <sys/time.h>
4875240ed1SBrian Somers #include <termios.h>
4975240ed1SBrian Somers #include <unistd.h>
501080ea25SBrian Somers #include <sys/stat.h>
5175240ed1SBrian Somers 
5267b072f7SBrian Somers #ifndef NONAT
5310e629b9SBrian Somers #ifdef LOCALNAT
547884358fSBrian Somers #include "alias.h"
5510e629b9SBrian Somers #else
5610e629b9SBrian Somers #include <alias.h>
571595bacdSBrian Somers #endif
581595bacdSBrian Somers #endif
5910e629b9SBrian Somers 
605d9e6103SBrian Somers #include "layer.h"
611af29a6eSBrian Somers #include "probe.h"
6275240ed1SBrian Somers #include "mbuf.h"
6375240ed1SBrian Somers #include "log.h"
6475240ed1SBrian Somers #include "defs.h"
655106c671SBrian Somers #include "id.h"
6675240ed1SBrian Somers #include "timer.h"
6775240ed1SBrian Somers #include "fsm.h"
68879ed6faSBrian Somers #include "lqr.h"
69af57ed9fSAtsushi Murai #include "hdlc.h"
70af57ed9fSAtsushi Murai #include "lcp.h"
710053cc58SBrian Somers #include "ccp.h"
7229e275ceSBrian Somers #include "iplist.h"
7329e275ceSBrian Somers #include "throughput.h"
74eaa4df37SBrian Somers #include "slcompress.h"
7530949fd4SBrian Somers #include "ncpaddr.h"
76af57ed9fSAtsushi Murai #include "ipcp.h"
7784b8a6ebSAtsushi Murai #include "filter.h"
782f786681SBrian Somers #include "descriptor.h"
793b0f8d2eSBrian Somers #include "link.h"
803b0f8d2eSBrian Somers #include "mp.h"
81972a1bcfSBrian Somers #ifndef NORADIUS
82972a1bcfSBrian Somers #include "radius.h"
83972a1bcfSBrian Somers #endif
8430949fd4SBrian Somers #include "ipv6cp.h"
8530949fd4SBrian Somers #include "ncp.h"
865828db6dSBrian Somers #include "bundle.h"
871ae349f5Scvs2svn #include "auth.h"
88ed6a16c1SPoul-Henning Kamp #include "systems.h"
89f5ff0f7cSBrian Somers #include "sig.h"
9075240ed1SBrian Somers #include "main.h"
9177ff88adSBrian Somers #include "server.h"
9285b542cfSBrian Somers #include "prompt.h"
93b6dec9f0SBrian Somers #include "chat.h"
94e2ebb036SBrian Somers #include "chap.h"
9592b09558SBrian Somers #include "cbcp.h"
963006ec67SBrian Somers #include "datalink.h"
978fa6ebe4SBrian Somers #include "iface.h"
9853c9f6c0SAtsushi Murai 
9953c9f6c0SAtsushi Murai #ifndef O_NONBLOCK
10053c9f6c0SAtsushi Murai #ifdef O_NDELAY
10153c9f6c0SAtsushi Murai #define	O_NONBLOCK O_NDELAY
10253c9f6c0SAtsushi Murai #endif
10353c9f6c0SAtsushi Murai #endif
104af57ed9fSAtsushi Murai 
1050f78c7a7SBrian Somers static void DoLoop(struct bundle *);
10675240ed1SBrian Somers static void TerminalStop(int);
10775240ed1SBrian Somers 
10883d1af55SBrian Somers static struct bundle *SignalBundle;
109b6217683SBrian Somers static struct prompt *SignalPrompt;
110a5625ae7SPaolo Pisati struct libalias *la;
111c3899f8dSAtsushi Murai 
112c3899f8dSAtsushi Murai void
Cleanup(void)113672eba24SJohn Baldwin Cleanup(void)
114af57ed9fSAtsushi Murai {
115a0cbd833SBrian Somers   SignalBundle->CleaningUp = 1;
1169c81b87dSBrian Somers   bundle_Close(SignalBundle, NULL, CLOSE_STAYDOWN);
1176e4959f0SBrian Somers }
118af57ed9fSAtsushi Murai 
1191afedc4bSBrian Somers void
AbortProgram(int excode)1201afedc4bSBrian Somers AbortProgram(int excode)
1211afedc4bSBrian Somers {
12223ddebe2SBrian Somers   if (SignalBundle)
123dd7e2610SBrian Somers     server_Close(SignalBundle);
124dd7e2610SBrian Somers   log_Printf(LogPHASE, "PPP Terminated (%s).\n", ex_desc(excode));
12523ddebe2SBrian Somers   if (SignalBundle) {
1269c81b87dSBrian Somers     bundle_Close(SignalBundle, NULL, CLOSE_STAYDOWN);
12768a0f0ccSBrian Somers     bundle_Destroy(SignalBundle);
12823ddebe2SBrian Somers   }
129dd7e2610SBrian Somers   log_Close();
130af57ed9fSAtsushi Murai   exit(excode);
131af57ed9fSAtsushi Murai }
132af57ed9fSAtsushi Murai 
133af57ed9fSAtsushi Murai static void
CloseConnection(int signo)134944f7098SBrian Somers CloseConnection(int signo)
135af57ed9fSAtsushi Murai {
136368aee2bSBrian Somers   /* NOTE, these are manual, we've done a setsid() */
137dd7e2610SBrian Somers   sig_signal(SIGINT, SIG_IGN);
138dd7e2610SBrian Somers   log_Printf(LogPHASE, "Caught signal %d, abort connection(s)\n", signo);
139899011c4SBrian Somers   bundle_Down(SignalBundle, CLOSE_STAYDOWN);
140dd7e2610SBrian Somers   sig_signal(SIGINT, CloseConnection);
1416d14e2a8SJordan K. Hubbard }
142af57ed9fSAtsushi Murai 
143af57ed9fSAtsushi Murai static void
CloseSession(int signo)144944f7098SBrian Somers CloseSession(int signo)
145af57ed9fSAtsushi Murai {
146dd7e2610SBrian Somers   log_Printf(LogPHASE, "Signal %d, terminate.\n", signo);
147057f1760SBrian Somers   Cleanup();
148af57ed9fSAtsushi Murai }
149c3899f8dSAtsushi Murai 
15075b8d283SBrian Somers static pid_t BGPid = 0;
15175b8d283SBrian Somers 
15275b8d283SBrian Somers static void
KillChild(int signo)15375b8d283SBrian Somers KillChild(int signo)
15475b8d283SBrian Somers {
155ac37ab22SBrian Somers   signal(signo, SIG_IGN);
156dd7e2610SBrian Somers   log_Printf(LogPHASE, "Parent: Signal %d\n", signo);
15775b8d283SBrian Somers   kill(BGPid, SIGINT);
15875b8d283SBrian Somers }
15975b8d283SBrian Somers 
160c3899f8dSAtsushi Murai static void
TerminalCont(int signo __unused)161057f1760SBrian Somers TerminalCont(int signo __unused)
162c3899f8dSAtsushi Murai {
163f91ad6b0SBrian Somers   signal(SIGCONT, SIG_DFL);
164f91ad6b0SBrian Somers   prompt_Continue(SignalPrompt);
165c3899f8dSAtsushi Murai }
166c3899f8dSAtsushi Murai 
167c3899f8dSAtsushi Murai static void
TerminalStop(int signo __unused)168057f1760SBrian Somers TerminalStop(int signo __unused)
169c3899f8dSAtsushi Murai {
170f91ad6b0SBrian Somers   prompt_Suspend(SignalPrompt);
171f91ad6b0SBrian Somers   signal(SIGCONT, TerminalCont);
172f91ad6b0SBrian Somers   raise(SIGSTOP);
1734ef16f24SBrian Somers }
1744ef16f24SBrian Somers 
1758ea8442cSBrian Somers static void
BringDownServer(int signo __unused)176057f1760SBrian Somers BringDownServer(int signo __unused)
1778ea8442cSBrian Somers {
178b6217683SBrian Somers   /* Drops all child prompts too ! */
17974457d3dSBrian Somers   if (server_Close(SignalBundle))
18074457d3dSBrian Somers     log_Printf(LogPHASE, "Closed server socket\n");
18174457d3dSBrian Somers }
18274457d3dSBrian Somers 
18374457d3dSBrian Somers static void
RestartServer(int signo __unused)184057f1760SBrian Somers RestartServer(int signo __unused)
18574457d3dSBrian Somers {
18674457d3dSBrian Somers   /* Drops all child prompts and re-opens the socket */
18774457d3dSBrian Somers   server_Reopen(SignalBundle);
1888ea8442cSBrian Somers }
1898ea8442cSBrian Somers 
19075240ed1SBrian Somers static void
Usage(void)191b6e82f33SBrian Somers Usage(void)
192af57ed9fSAtsushi Murai {
193d3974088SDag-Erling Smørgrav   fprintf(stderr, "usage: ppp [-auto | -foreground | -background | -direct |"
1949b996792SBrian Somers           " -dedicated | -ddial | -interactive]"
195564299efSRuslan Ermilov #ifndef NONAT
19667b072f7SBrian Somers           " [-nat]"
197b6e82f33SBrian Somers #endif
198c0593e34SBrian Somers           " [-quiet] [-unit N] [system ...]\n");
199af57ed9fSAtsushi Murai   exit(EX_START);
200af57ed9fSAtsushi Murai }
201af57ed9fSAtsushi Murai 
202c0593e34SBrian Somers struct switches {
203c0593e34SBrian Somers   unsigned nat : 1;
204c0593e34SBrian Somers   unsigned fg : 1;
205c0593e34SBrian Somers   unsigned quiet : 1;
206c0593e34SBrian Somers   int mode;
207c0593e34SBrian Somers   int unit;
208c0593e34SBrian Somers };
209c0593e34SBrian Somers 
2107cf368ebSBrian Somers static int
ProcessArgs(int argc,char ** argv,struct switches * sw)211c0593e34SBrian Somers ProcessArgs(int argc, char **argv, struct switches *sw)
212af57ed9fSAtsushi Murai {
2137cf368ebSBrian Somers   int optc, newmode, arg;
214af57ed9fSAtsushi Murai   char *cp;
215af57ed9fSAtsushi Murai 
2167cf368ebSBrian Somers   optc = 0;
217c0593e34SBrian Somers   memset(sw, '\0', sizeof *sw);
218c0593e34SBrian Somers   sw->mode = PHYS_INTERACTIVE;
219c0593e34SBrian Somers   sw->unit = -1;
220c0593e34SBrian Somers 
2217cf368ebSBrian Somers   for (arg = 1; arg < argc && *argv[arg] == '-'; arg++, optc++) {
2227cf368ebSBrian Somers     cp = argv[arg] + 1;
22381358fa3SBrian Somers     newmode = Nam2mode(cp);
22481358fa3SBrian Somers     switch (newmode) {
22581358fa3SBrian Somers       case PHYS_NONE:
226c0593e34SBrian Somers         if (strcmp(cp, "nat") == 0) {
22767b072f7SBrian Somers #ifdef NONAT
228c0593e34SBrian Somers           log_Printf(LogWARN, "%s ignored: NAT is compiled out\n", argv[arg]);
229615ad4f9SBrian Somers #else
230c0593e34SBrian Somers           sw->nat = 1;
231615ad4f9SBrian Somers #endif
2321ae349f5Scvs2svn           optc--;			/* this option isn't exclusive */
233c0593e34SBrian Somers         } else if (strcmp(cp, "alias") == 0) {
234c0593e34SBrian Somers #ifdef NONAT
235c0593e34SBrian Somers           log_Printf(LogWARN, "%s ignored: NAT is compiled out\n", argv[arg]);
236c0593e34SBrian Somers           fprintf(stderr, "%s ignored: NAT is compiled out\n", argv[arg]);
237c0593e34SBrian Somers #else
238886530abSBrian Somers           log_Printf(LogWARN, "%s is deprecated\n", argv[arg]);
239886530abSBrian Somers           fprintf(stderr, "%s is deprecated\n", argv[arg]);
240c0593e34SBrian Somers           sw->nat = 1;
241c0593e34SBrian Somers #endif
242c0593e34SBrian Somers           optc--;			/* this option isn't exclusive */
243c0593e34SBrian Somers         } else if (strncmp(cp, "unit", 4) == 0) {
2449d06928dSBrian Somers           optc--;			/* this option isn't exclusive */
245c0593e34SBrian Somers           if (cp[4] == '\0') {
2469d06928dSBrian Somers             optc--;			/* nor is the argument */
247c0593e34SBrian Somers             if (++arg == argc) {
248c0593e34SBrian Somers               fprintf(stderr, "-unit: Expected unit number\n");
249c0593e34SBrian Somers               Usage();
250c0593e34SBrian Somers             } else
251c0593e34SBrian Somers               sw->unit = atoi(argv[arg]);
252c0593e34SBrian Somers           } else
253c0593e34SBrian Somers             sw->unit = atoi(cp + 4);
25467b072f7SBrian Somers         } else if (strcmp(cp, "quiet") == 0) {
255c0593e34SBrian Somers           sw->quiet = 1;
25667b072f7SBrian Somers           optc--;			/* this option isn't exclusive */
257944f7098SBrian Somers         } else
258af57ed9fSAtsushi Murai           Usage();
25981358fa3SBrian Somers         break;
26081358fa3SBrian Somers 
26181358fa3SBrian Somers       case PHYS_ALL:
26281358fa3SBrian Somers         Usage();
26381358fa3SBrian Somers         break;
26481358fa3SBrian Somers 
26581358fa3SBrian Somers       default:
266c0593e34SBrian Somers         sw->mode = newmode;
267f6a4e748SBrian Somers         if (newmode == PHYS_FOREGROUND)
268f6a4e748SBrian Somers           sw->fg = 1;
26981358fa3SBrian Somers     }
270af57ed9fSAtsushi Murai   }
271af57ed9fSAtsushi Murai 
272af57ed9fSAtsushi Murai   if (optc > 1) {
27385602e52SBrian Somers     fprintf(stderr, "You may specify only one mode.\n");
2741ae349f5Scvs2svn     exit(EX_START);
2751ae349f5Scvs2svn   }
2761ae349f5Scvs2svn 
277c0593e34SBrian Somers   if (sw->mode == PHYS_AUTO && arg == argc) {
2787cf368ebSBrian Somers     fprintf(stderr, "A system must be specified in auto mode.\n");
279af57ed9fSAtsushi Murai     exit(EX_START);
280af57ed9fSAtsushi Murai   }
28139f94eddSBrian Somers 
2827cf368ebSBrian Somers   return arg;		/* Don't SetLabel yet ! */
283af57ed9fSAtsushi Murai }
284af57ed9fSAtsushi Murai 
2857cf368ebSBrian Somers static void
CheckLabel(const char * label,struct prompt * prompt,int mode)2867cf368ebSBrian Somers CheckLabel(const char *label, struct prompt *prompt, int mode)
2877cf368ebSBrian Somers {
2887cf368ebSBrian Somers   const char *err;
2897cf368ebSBrian Somers 
2907cf368ebSBrian Somers   if ((err = system_IsValid(label, prompt, mode)) != NULL) {
2917cf368ebSBrian Somers     fprintf(stderr, "%s: %s\n", label, err);
2927cf368ebSBrian Somers     if (mode == PHYS_DIRECT)
2937cf368ebSBrian Somers       log_Printf(LogWARN, "Label %s rejected -direct connection: %s\n",
2947cf368ebSBrian Somers                  label, err);
2957cf368ebSBrian Somers     log_Close();
2967cf368ebSBrian Somers     exit(1);
2977cf368ebSBrian Somers   }
2987cf368ebSBrian Somers }
2997cf368ebSBrian Somers 
3007cf368ebSBrian Somers 
3014ef16f24SBrian Somers int
main(int argc,char ** argv)302944f7098SBrian Somers main(int argc, char **argv)
303af57ed9fSAtsushi Murai {
3047cf368ebSBrian Somers   char *name;
305756783fcSBrian Somers   const char *lastlabel;
306057f1760SBrian Somers   int arg, holdfd[3], label;
307057f1760SBrian Somers   unsigned f;
3087a6f8720SBrian Somers   struct bundle *bundle;
309b6217683SBrian Somers   struct prompt *prompt;
310c0593e34SBrian Somers   struct switches sw;
311e3b4c400SBrian Somers 
312971abb29SBrian Somers   probe_Init();
313971abb29SBrian Somers 
314108e336aSBrian Somers   /*
315108e336aSBrian Somers    * We open 3 descriptors to ensure that STDIN_FILENO, STDOUT_FILENO and
316108e336aSBrian Somers    * STDERR_FILENO are always open.  These are closed before DoLoop(),
317108e336aSBrian Somers    * but *after* we've avoided the possibility of erroneously closing
318108e336aSBrian Somers    * an important descriptor with close(STD{IN,OUT,ERR}_FILENO).
319108e336aSBrian Somers    */
320108e336aSBrian Somers   if ((holdfd[0] = open(_PATH_DEVNULL, O_RDWR)) == -1) {
321108e336aSBrian Somers     fprintf(stderr, "Cannot open %s !\n", _PATH_DEVNULL);
322108e336aSBrian Somers     return 2;
323108e336aSBrian Somers   }
324108e336aSBrian Somers   for (f = 1; f < sizeof holdfd / sizeof *holdfd; f++)
325dd1a52b9SBrian Somers     holdfd[f] = dup(holdfd[0]);
326108e336aSBrian Somers 
32775240ed1SBrian Somers   name = strrchr(argv[0], '/');
328dd7e2610SBrian Somers   log_Open(name ? name + 1 : argv[0]);
3292a279fedSBrian Somers 
33067b072f7SBrian Somers #ifndef NONAT
331a5625ae7SPaolo Pisati   la = LibAliasInit(NULL);
3322a630835SBrian Somers #endif
333c0593e34SBrian Somers   label = ProcessArgs(argc, argv, &sw);
33485b542cfSBrian Somers 
33585b542cfSBrian Somers   /*
336a611383fSBrian Somers    * A FreeBSD & OpenBSD hack to dodge a bug in the tty driver that drops
337a611383fSBrian Somers    * output occasionally.... I must find the real reason some time.  To
338a611383fSBrian Somers    * display the dodgy behaviour, comment out this bit, make yourself a large
33985b542cfSBrian Somers    * routing table and then run ppp in interactive mode.  The `show route'
34085b542cfSBrian Somers    * command will drop chunks of data !!!
34185b542cfSBrian Somers    */
342c0593e34SBrian Somers   if (sw.mode == PHYS_INTERACTIVE) {
34385b542cfSBrian Somers     close(STDIN_FILENO);
34485b542cfSBrian Somers     if (open(_PATH_TTY, O_RDONLY) != STDIN_FILENO) {
34585b542cfSBrian Somers       fprintf(stderr, "Cannot open %s for input !\n", _PATH_TTY);
34685b542cfSBrian Somers       return 2;
34785b542cfSBrian Somers     }
34885b542cfSBrian Somers   }
34985b542cfSBrian Somers 
350b6217683SBrian Somers   /* Allow output for the moment (except in direct mode) */
351c0593e34SBrian Somers   if (sw.mode == PHYS_DIRECT)
352b6217683SBrian Somers     prompt = NULL;
353ed0e9269SBrian Somers   else
354b6217683SBrian Somers     SignalPrompt = prompt = prompt_Create(NULL, NULL, PROMPT_STD);
35512ef29a8SBrian Somers 
3565106c671SBrian Somers   ID0init();
3574562be74SBrian Somers   if (ID0realuid() != 0) {
3584562be74SBrian Somers     char conf[200], *ptr;
3594562be74SBrian Somers 
3607a66a36dSBrian Somers     snprintf(conf, sizeof conf, "%s/%s", PPP_CONFDIR, CONFFILE);
3614562be74SBrian Somers     do {
3621080ea25SBrian Somers       struct stat sb;
3631080ea25SBrian Somers 
3641080ea25SBrian Somers       if (stat(conf, &sb) == 0 && sb.st_mode & S_IWOTH) {
365a33b2ef7SBrian Somers         log_Printf(LogALERT, "ppp: Access violation: Please protect %s\n",
366a33b2ef7SBrian Somers                    conf);
3674562be74SBrian Somers         return -1;
3684562be74SBrian Somers       }
3694562be74SBrian Somers       ptr = conf + strlen(conf)-2;
3704562be74SBrian Somers       while (ptr > conf && *ptr != '/')
3714562be74SBrian Somers         *ptr-- = '\0';
3724562be74SBrian Somers     } while (ptr >= conf);
3734562be74SBrian Somers   }
3744562be74SBrian Somers 
3757cf368ebSBrian Somers   if (label < argc)
3767cf368ebSBrian Somers     for (arg = label; arg < argc; arg++)
377c0593e34SBrian Somers       CheckLabel(argv[arg], prompt, sw.mode);
3787cf368ebSBrian Somers   else
379c0593e34SBrian Somers     CheckLabel("default", prompt, sw.mode);
38012ef29a8SBrian Somers 
381c0593e34SBrian Somers   if (!sw.quiet)
382c0593e34SBrian Somers     prompt_Printf(prompt, "Working in %s mode\n", mode2Nam(sw.mode));
383ed0e9269SBrian Somers 
384cf0a3940SBrian Somers   if ((bundle = bundle_Create(TUN_PREFIX, sw.mode, sw.unit)) == NULL)
3854ef16f24SBrian Somers     return EX_START;
386756783fcSBrian Somers 
387756783fcSBrian Somers   /* NOTE:  We may now have changed argv[1] via a ``set proctitle'' */
388756783fcSBrian Somers 
38983d1af55SBrian Somers   SignalBundle = bundle;
390c0593e34SBrian Somers   bundle->NatEnabled = sw.nat;
391c0593e34SBrian Somers   if (sw.nat)
3920508c09aSBrian Somers     opt_enable(bundle, OPT_IFACEALIAS);
39312ef29a8SBrian Somers 
39430291ffbSBrian Somers   if (system_Select(bundle, "default", CONFFILE, prompt, NULL) < 0)
395565e35e5SBrian Somers     prompt_Printf(prompt, "Warning: No default entry found in config file.\n");
3961ae349f5Scvs2svn 
397dd7e2610SBrian Somers   sig_signal(SIGHUP, CloseSession);
398dd7e2610SBrian Somers   sig_signal(SIGTERM, CloseSession);
399dd7e2610SBrian Somers   sig_signal(SIGINT, CloseConnection);
400dd7e2610SBrian Somers   sig_signal(SIGQUIT, CloseSession);
401dd7e2610SBrian Somers   sig_signal(SIGALRM, SIG_IGN);
402e0d3e233SAndrey A. Chernov   signal(SIGPIPE, SIG_IGN);
403565e35e5SBrian Somers 
404c0593e34SBrian Somers   if (sw.mode == PHYS_INTERACTIVE)
405dd7e2610SBrian Somers     sig_signal(SIGTSTP, TerminalStop);
406f91ad6b0SBrian Somers 
40774457d3dSBrian Somers   sig_signal(SIGUSR1, RestartServer);
408dd7e2610SBrian Somers   sig_signal(SIGUSR2, BringDownServer);
409af57ed9fSAtsushi Murai 
410cf0a3940SBrian Somers   lastlabel = argv[argc - 1];
4117cf368ebSBrian Somers   for (arg = label; arg < argc; arg++) {
4127cf368ebSBrian Somers     /* In case we use LABEL or ``set enddisc label'' */
413756783fcSBrian Somers     bundle_SetLabel(bundle, lastlabel);
414cf0a3940SBrian Somers     system_Select(bundle, argv[arg], CONFFILE, prompt, NULL);
4151ae349f5Scvs2svn   }
4167cf368ebSBrian Somers 
4177cf368ebSBrian Somers   if (label < argc)
4187cf368ebSBrian Somers     /* In case the last label did a ``load'' */
419756783fcSBrian Somers     bundle_SetLabel(bundle, lastlabel);
4207cf368ebSBrian Somers 
421c0593e34SBrian Somers   if (sw.mode == PHYS_AUTO &&
42230949fd4SBrian Somers       ncprange_family(&bundle->ncp.ipcp.cfg.peer_range) == AF_UNSPEC) {
4237cf368ebSBrian Somers     prompt_Printf(prompt, "You must ``set ifaddr'' with a peer address "
4247cf368ebSBrian Somers                   "in auto mode.\n");
4258390b576SBrian Somers     AbortProgram(EX_START);
426af57ed9fSAtsushi Murai   }
4276efd9292SBrian Somers 
428f2f5156fSBrian Somers   if (prompt) {
429f2f5156fSBrian Somers     prompt->bundle = bundle;	/* couldn't do it earlier */
430f2f5156fSBrian Somers     if (!sw.quiet)
431f2f5156fSBrian Somers       prompt_Printf(prompt, "Using interface: %s\n", bundle->iface->name);
432f2f5156fSBrian Somers   }
433f2f5156fSBrian Somers 
434c0593e34SBrian Somers   if (sw.mode != PHYS_INTERACTIVE) {
435c0593e34SBrian Somers     if (sw.mode != PHYS_DIRECT) {
436c0593e34SBrian Somers       if (!sw.fg) {
4375cf4388bSBrian Somers         int bgpipe[2];
4386d14e2a8SJordan K. Hubbard         pid_t bgpid;
439a9c6b5dfSAtsushi Murai 
440c0593e34SBrian Somers         if (sw.mode == PHYS_BACKGROUND && pipe(bgpipe)) {
441dd7e2610SBrian Somers           log_Printf(LogERROR, "pipe: %s\n", strerror(errno));
4428390b576SBrian Somers 	  AbortProgram(EX_SOCK);
4431ae349f5Scvs2svn         }
4441ae349f5Scvs2svn 
4456d14e2a8SJordan K. Hubbard         bgpid = fork();
4466d14e2a8SJordan K. Hubbard         if (bgpid == -1) {
447dd7e2610SBrian Somers 	  log_Printf(LogERROR, "fork: %s\n", strerror(errno));
4488390b576SBrian Somers 	  AbortProgram(EX_SOCK);
4496d14e2a8SJordan K. Hubbard         }
4505cf4388bSBrian Somers 
4516d14e2a8SJordan K. Hubbard         if (bgpid) {
4526d14e2a8SJordan K. Hubbard 	  char c = EX_NORMAL;
453b42135deSBrian Somers           int ret;
454a9c6b5dfSAtsushi Murai 
455c0593e34SBrian Somers 	  if (sw.mode == PHYS_BACKGROUND) {
4565cf4388bSBrian Somers 	    close(bgpipe[1]);
4576d14e2a8SJordan K. Hubbard 	    BGPid = bgpid;
45875b8d283SBrian Somers             /* If we get a signal, kill the child */
45975b8d283SBrian Somers             signal(SIGHUP, KillChild);
46075b8d283SBrian Somers             signal(SIGTERM, KillChild);
46175b8d283SBrian Somers             signal(SIGINT, KillChild);
46275b8d283SBrian Somers             signal(SIGQUIT, KillChild);
46375b8d283SBrian Somers 
46475b8d283SBrian Somers 	    /* Wait for our child to close its pipe before we exit */
465b42135deSBrian Somers             while ((ret = read(bgpipe[0], &c, 1)) == 1) {
466b42135deSBrian Somers               switch (c) {
467b42135deSBrian Somers                 case EX_NORMAL:
4680fea4ed0SBrian Somers                   if (!sw.quiet) {
469b42135deSBrian Somers 	            prompt_Printf(prompt, "PPP enabled\n");
470b42135deSBrian Somers 	            log_Printf(LogPHASE, "Parent: PPP enabled\n");
4710fea4ed0SBrian Somers                   }
4729bf01bcbSBrian Somers 	          break;
473b42135deSBrian Somers                 case EX_REDIAL:
474b42135deSBrian Somers                   if (!sw.quiet)
475b42135deSBrian Somers 	            prompt_Printf(prompt, "Attempting redial\n");
476b42135deSBrian Somers                   continue;
477b42135deSBrian Somers                 case EX_RECONNECT:
478b42135deSBrian Somers                   if (!sw.quiet)
479b42135deSBrian Somers 	            prompt_Printf(prompt, "Attempting reconnect\n");
480b42135deSBrian Somers                   continue;
481b42135deSBrian Somers 	        default:
482b42135deSBrian Somers 	          prompt_Printf(prompt, "Child failed (%s)\n",
483b42135deSBrian Somers                                 ex_desc((int)c));
484b42135deSBrian Somers 	          log_Printf(LogPHASE, "Parent: Child failed (%s)\n",
485b42135deSBrian Somers 		             ex_desc((int) c));
486b42135deSBrian Somers 	      }
487b42135deSBrian Somers 	      break;
488b42135deSBrian Somers             }
489b42135deSBrian Somers             if (ret != 1) {
490b6217683SBrian Somers 	      prompt_Printf(prompt, "Child exit, no status.\n");
491dd7e2610SBrian Somers 	      log_Printf(LogPHASE, "Parent: Child exit, no status.\n");
4926efd9292SBrian Somers 	    }
4935cf4388bSBrian Somers 	    close(bgpipe[0]);
4946d14e2a8SJordan K. Hubbard 	  }
4954ef16f24SBrian Somers 	  return c;
496c0593e34SBrian Somers         } else if (sw.mode == PHYS_BACKGROUND) {
4975cf4388bSBrian Somers 	  close(bgpipe[0]);
4985cf4388bSBrian Somers           bundle->notify.fd = bgpipe[1];
499aefd026aSBrian Somers         }
500aefd026aSBrian Somers 
50106b47306SBrian Somers         bundle_ChangedPID(bundle);
502da66dd13SBrian Somers         bundle_LockTun(bundle);	/* we have a new pid */
50367b072f7SBrian Somers       }
504da66dd13SBrian Somers 
50567b072f7SBrian Somers       /* -auto, -dedicated, -ddial, -foreground & -background */
506b6217683SBrian Somers       prompt_Destroy(prompt, 0);
5072a279fedSBrian Somers       close(STDOUT_FILENO);
5082a279fedSBrian Somers       close(STDERR_FILENO);
5092a279fedSBrian Somers       close(STDIN_FILENO);
510c0593e34SBrian Somers       if (!sw.fg)
511b6217683SBrian Somers         setsid();
512565e35e5SBrian Somers     } else {
513887ff31fSBrian Somers       /*
514887ff31fSBrian Somers        * -direct - STDIN_FILENO gets used by physical_Open.  STDOUT_FILENO
515887ff31fSBrian Somers        * *may* get used in exec/pipe mode.
516887ff31fSBrian Somers        */
517565e35e5SBrian Somers       prompt_TtyInit(NULL);
518565e35e5SBrian Somers       close(STDERR_FILENO);
519d656a4c5SBrian Somers     }
520af57ed9fSAtsushi Murai   } else {
52167b072f7SBrian Somers     /* -interactive */
5222a279fedSBrian Somers     close(STDERR_FILENO);
523565e35e5SBrian Somers     prompt_TtyInit(prompt);
524b6217683SBrian Somers     prompt_TtyCommandMode(prompt);
525b6217683SBrian Somers     prompt_Required(prompt);
526af57ed9fSAtsushi Murai   }
52735495becSBrian Somers 
528108e336aSBrian Somers   /* We can get rid of these now */
529108e336aSBrian Somers   for (f = 0; f < sizeof holdfd / sizeof *holdfd; f++)
530108e336aSBrian Somers     close(holdfd[f]);
531108e336aSBrian Somers 
532c0593e34SBrian Somers   log_Printf(LogPHASE, "PPP Started (%s mode).\n", mode2Nam(sw.mode));
5330f78c7a7SBrian Somers   DoLoop(bundle);
5341afedc4bSBrian Somers   AbortProgram(EX_NORMAL);
535af57ed9fSAtsushi Murai 
5361afedc4bSBrian Somers   return EX_NORMAL;
537af57ed9fSAtsushi Murai }
538af57ed9fSAtsushi Murai 
539af57ed9fSAtsushi Murai static void
DoLoop(struct bundle * bundle)5400f78c7a7SBrian Somers DoLoop(struct bundle *bundle)
541af57ed9fSAtsushi Murai {
5428a52f3ecSBrian Somers   fd_set *rfds, *wfds, *efds;
5431af29a6eSBrian Somers   int i, nfds, nothing_done;
544c3899f8dSAtsushi Murai 
5458a52f3ecSBrian Somers   if ((rfds = mkfdset()) == NULL) {
5468a52f3ecSBrian Somers     log_Printf(LogERROR, "DoLoop: Cannot create fd_set\n");
5478a52f3ecSBrian Somers     return;
5488a52f3ecSBrian Somers   }
5498a52f3ecSBrian Somers 
5508a52f3ecSBrian Somers   if ((wfds = mkfdset()) == NULL) {
5518a52f3ecSBrian Somers     log_Printf(LogERROR, "DoLoop: Cannot create fd_set\n");
5528a52f3ecSBrian Somers     free(rfds);
5538a52f3ecSBrian Somers     return;
5548a52f3ecSBrian Somers   }
5558a52f3ecSBrian Somers 
5568a52f3ecSBrian Somers   if ((efds = mkfdset()) == NULL) {
5578a52f3ecSBrian Somers     log_Printf(LogERROR, "DoLoop: Cannot create fd_set\n");
5588a52f3ecSBrian Somers     free(rfds);
5598a52f3ecSBrian Somers     free(wfds);
5608a52f3ecSBrian Somers     return;
5618a52f3ecSBrian Somers   }
5628a52f3ecSBrian Somers 
563da8b7034SBrian Somers   for (; !bundle_IsDead(bundle); bundle_CleanDatalinks(bundle)) {
564780700e5SAndrey A. Chernov     nfds = 0;
5658a52f3ecSBrian Somers     zerofdset(rfds);
5668a52f3ecSBrian Somers     zerofdset(wfds);
5678a52f3ecSBrian Somers     zerofdset(efds);
56884b8a6ebSAtsushi Murai 
5690f2f3eb3SBrian Somers     /* All our datalinks, the tun device and the MP socket */
5708a52f3ecSBrian Somers     descriptor_UpdateSet(&bundle->desc, rfds, wfds, efds, &nfds);
5710f2f3eb3SBrian Somers 
5720f2f3eb3SBrian Somers     /* All our prompts and the diagnostic socket */
5738a52f3ecSBrian Somers     descriptor_UpdateSet(&server.desc, rfds, NULL, NULL, &nfds);
57407030d97SBrian Somers 
5750cd8e902SBrian Somers     bundle_CleanDatalinks(bundle);
5763b0f8d2eSBrian Somers     if (bundle_IsDead(bundle))
577f4768038SBrian Somers       /* Don't select - we'll be here forever */
578f4768038SBrian Somers       break;
5790706ff38SBrian Somers 
580486105bcSBrian Somers     /*
581486105bcSBrian Somers      * It's possible that we've had a signal since we last checked.  If
582486105bcSBrian Somers      * we don't check again before calling select(), we may end up stuck
583486105bcSBrian Somers      * after having missed the event.... sig_Handle() tries to be as
584486105bcSBrian Somers      * quick as possible if nothing is likely to have happened.
585486105bcSBrian Somers      * This is only really likely if we block in open(... O_NONBLOCK)
586486105bcSBrian Somers      * which will happen with a misconfigured device.
587486105bcSBrian Somers      */
588486105bcSBrian Somers     if (sig_Handle())
589486105bcSBrian Somers       continue;
590486105bcSBrian Somers 
5918a52f3ecSBrian Somers     i = select(nfds, rfds, wfds, efds, NULL);
592712ae387SBrian Somers 
59354cd8e13SBrian Somers     if (i < 0 && errno != EINTR) {
594dd7e2610SBrian Somers       log_Printf(LogERROR, "DoLoop: select(): %s\n", strerror(errno));
59524989c68SBrian Somers       if (log_IsKept(LogTIMER)) {
59624989c68SBrian Somers         struct timeval t;
59724989c68SBrian Somers 
59824989c68SBrian Somers         for (i = 0; i <= nfds; i++) {
5998a52f3ecSBrian Somers           if (FD_ISSET(i, rfds)) {
60024989c68SBrian Somers             log_Printf(LogTIMER, "Read set contains %d\n", i);
6018a52f3ecSBrian Somers             FD_CLR(i, rfds);
60224989c68SBrian Somers             t.tv_sec = t.tv_usec = 0;
6038a52f3ecSBrian Somers             if (select(nfds, rfds, wfds, efds, &t) != -1) {
60424989c68SBrian Somers               log_Printf(LogTIMER, "The culprit !\n");
605af57ed9fSAtsushi Murai               break;
606af57ed9fSAtsushi Murai             }
60724989c68SBrian Somers           }
6088a52f3ecSBrian Somers           if (FD_ISSET(i, wfds)) {
60924989c68SBrian Somers             log_Printf(LogTIMER, "Write set contains %d\n", i);
6108a52f3ecSBrian Somers             FD_CLR(i, wfds);
61124989c68SBrian Somers             t.tv_sec = t.tv_usec = 0;
6128a52f3ecSBrian Somers             if (select(nfds, rfds, wfds, efds, &t) != -1) {
61324989c68SBrian Somers               log_Printf(LogTIMER, "The culprit !\n");
614af57ed9fSAtsushi Murai               break;
615af57ed9fSAtsushi Murai             }
616e0d3e233SAndrey A. Chernov           }
6178a52f3ecSBrian Somers           if (FD_ISSET(i, efds)) {
61824989c68SBrian Somers             log_Printf(LogTIMER, "Error set contains %d\n", i);
6198a52f3ecSBrian Somers             FD_CLR(i, efds);
62024989c68SBrian Somers             t.tv_sec = t.tv_usec = 0;
6218a52f3ecSBrian Somers             if (select(nfds, rfds, wfds, efds, &t) != -1) {
62224989c68SBrian Somers               log_Printf(LogTIMER, "The culprit !\n");
62358f264e1SBrian Somers               break;
62458f264e1SBrian Somers             }
62524989c68SBrian Somers           }
62624989c68SBrian Somers         }
62724989c68SBrian Somers       }
62858f264e1SBrian Somers       break;
629de451c68SBrian Somers     }
630de451c68SBrian Somers 
631f0cdd9c0SBrian Somers     log_Printf(LogTIMER, "Select returns %d\n", i);
632f0cdd9c0SBrian Somers 
63354cd8e13SBrian Somers     sig_Handle();
63454cd8e13SBrian Somers 
63554cd8e13SBrian Somers     if (i <= 0)
63654cd8e13SBrian Somers       continue;
63754cd8e13SBrian Somers 
6383006ec67SBrian Somers     for (i = 0; i <= nfds; i++)
6398a52f3ecSBrian Somers       if (FD_ISSET(i, efds)) {
6408e7bd08eSBrian Somers         log_Printf(LogPHASE, "Exception detected on descriptor %d\n", i);
6418e7bd08eSBrian Somers         /* We deal gracefully with link descriptor exceptions */
642991c2a7bSBrian Somers         if (!bundle_Exception(bundle, i)) {
643991c2a7bSBrian Somers           log_Printf(LogERROR, "Exception cannot be handled !\n");
6441ae349f5Scvs2svn           break;
6451ae349f5Scvs2svn         }
646991c2a7bSBrian Somers       }
647c60f92caSBrian Somers 
648b7c5748eSBrian Somers     if (i <= nfds)
649b7c5748eSBrian Somers       break;
650b7c5748eSBrian Somers 
6511af29a6eSBrian Somers     nothing_done = 1;
6521af29a6eSBrian Somers 
6538a52f3ecSBrian Somers     if (descriptor_IsSet(&server.desc, rfds)) {
6548a52f3ecSBrian Somers       descriptor_Read(&server.desc, bundle, rfds);
6551af29a6eSBrian Somers       nothing_done = 0;
6561af29a6eSBrian Somers     }
6571af29a6eSBrian Somers 
6588a52f3ecSBrian Somers     if (descriptor_IsSet(&bundle->desc, rfds)) {
6598a52f3ecSBrian Somers       descriptor_Read(&bundle->desc, bundle, rfds);
6601af29a6eSBrian Somers       nothing_done = 0;
6611af29a6eSBrian Somers     }
66242d4d396SBrian Somers 
6638a52f3ecSBrian Somers     if (descriptor_IsSet(&bundle->desc, wfds))
664fb11a9c2SBrian Somers       if (descriptor_Write(&bundle->desc, bundle, wfds) <= 0 && nothing_done) {
6651af29a6eSBrian Somers         /*
666fb11a9c2SBrian Somers          * This is disastrous.  The OS has told us that something is
6671af29a6eSBrian Somers          * writable, and all our write()s have failed.  Rather than
6681af29a6eSBrian Somers          * going back immediately to do our UpdateSet()s and select(),
6691af29a6eSBrian Somers          * we sleep for a bit to avoid gobbling up all cpu time.
6701af29a6eSBrian Somers          */
6711af29a6eSBrian Somers         struct timeval t;
6722f786681SBrian Somers 
6731af29a6eSBrian Somers         t.tv_sec = 0;
6741af29a6eSBrian Somers         t.tv_usec = 100000;
6751af29a6eSBrian Somers         select(0, NULL, NULL, NULL, &t);
6761af29a6eSBrian Somers       }
677da8b7034SBrian Somers   }
678565e35e5SBrian Somers 
679dd7e2610SBrian Somers   log_Printf(LogDEBUG, "DoLoop done.\n");
680af57ed9fSAtsushi Murai }
681