1af57ed9fSAtsushi Murai /* 2af57ed9fSAtsushi Murai * User Process PPP 3af57ed9fSAtsushi Murai * 4af57ed9fSAtsushi Murai * Written by Toshiharu OHNO (tony-o@iij.ad.jp) 5af57ed9fSAtsushi Murai * 6af57ed9fSAtsushi Murai * Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd. 7af57ed9fSAtsushi Murai * 8af57ed9fSAtsushi Murai * Redistribution and use in source and binary forms are permitted 9af57ed9fSAtsushi Murai * provided that the above copyright notice and this paragraph are 10af57ed9fSAtsushi Murai * duplicated in all such forms and that any documentation, 11af57ed9fSAtsushi Murai * advertising materials, and other materials related to such 12af57ed9fSAtsushi Murai * distribution and use acknowledge that the software was developed 13af57ed9fSAtsushi Murai * by the Internet Initiative Japan, Inc. The name of the 14af57ed9fSAtsushi Murai * IIJ may not be used to endorse or promote products derived 15af57ed9fSAtsushi Murai * from this software without specific prior written permission. 16af57ed9fSAtsushi Murai * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 17af57ed9fSAtsushi Murai * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 18af57ed9fSAtsushi Murai * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 19af57ed9fSAtsushi Murai * 2097d92980SPeter Wemm * $FreeBSD$ 21af57ed9fSAtsushi Murai * 22af57ed9fSAtsushi Murai * TODO: 23af57ed9fSAtsushi Murai */ 242764b86aSBrian Somers 25972a1bcfSBrian Somers #include <sys/param.h> 2675240ed1SBrian Somers #include <netinet/in.h> 27a9f484e5SJordan K. Hubbard #include <netinet/in_systm.h> 28a9f484e5SJordan K. Hubbard #include <netinet/ip.h> 29565e35e5SBrian Somers #include <sys/un.h> 3075240ed1SBrian Somers 3175240ed1SBrian Somers #include <errno.h> 3275240ed1SBrian Somers #include <fcntl.h> 3375240ed1SBrian Somers #include <paths.h> 3475240ed1SBrian Somers #include <signal.h> 3575240ed1SBrian Somers #include <stdio.h> 36c0593e34SBrian Somers #include <stdlib.h> 3775240ed1SBrian Somers #include <string.h> 3875240ed1SBrian Somers #include <sys/time.h> 3975240ed1SBrian Somers #include <termios.h> 4075240ed1SBrian Somers #include <unistd.h> 411080ea25SBrian Somers #include <sys/stat.h> 4275240ed1SBrian Somers 4367b072f7SBrian Somers #ifndef NONAT 447884358fSBrian Somers #ifdef __FreeBSD__ 451595bacdSBrian Somers #include <alias.h> 467884358fSBrian Somers #else 477884358fSBrian Somers #include "alias.h" 481595bacdSBrian Somers #endif 491595bacdSBrian Somers #endif 505d9e6103SBrian Somers #include "layer.h" 511af29a6eSBrian Somers #include "probe.h" 5275240ed1SBrian Somers #include "mbuf.h" 5375240ed1SBrian Somers #include "log.h" 5475240ed1SBrian Somers #include "defs.h" 555106c671SBrian Somers #include "id.h" 5675240ed1SBrian Somers #include "timer.h" 5775240ed1SBrian Somers #include "fsm.h" 58879ed6faSBrian Somers #include "lqr.h" 59af57ed9fSAtsushi Murai #include "hdlc.h" 60af57ed9fSAtsushi Murai #include "lcp.h" 610053cc58SBrian Somers #include "ccp.h" 6229e275ceSBrian Somers #include "iplist.h" 6329e275ceSBrian Somers #include "throughput.h" 64eaa4df37SBrian Somers #include "slcompress.h" 65af57ed9fSAtsushi Murai #include "ipcp.h" 6684b8a6ebSAtsushi Murai #include "filter.h" 672f786681SBrian Somers #include "descriptor.h" 683b0f8d2eSBrian Somers #include "link.h" 693b0f8d2eSBrian Somers #include "mp.h" 70972a1bcfSBrian Somers #ifndef NORADIUS 71972a1bcfSBrian Somers #include "radius.h" 72972a1bcfSBrian Somers #endif 735828db6dSBrian Somers #include "bundle.h" 741ae349f5Scvs2svn #include "auth.h" 75ed6a16c1SPoul-Henning Kamp #include "systems.h" 76f5ff0f7cSBrian Somers #include "sig.h" 7775240ed1SBrian Somers #include "main.h" 7877ff88adSBrian Somers #include "server.h" 7985b542cfSBrian Somers #include "prompt.h" 80b6dec9f0SBrian Somers #include "chat.h" 81e2ebb036SBrian Somers #include "chap.h" 8292b09558SBrian Somers #include "cbcp.h" 833006ec67SBrian Somers #include "datalink.h" 848fa6ebe4SBrian Somers #include "iface.h" 8553c9f6c0SAtsushi Murai 8653c9f6c0SAtsushi Murai #ifndef O_NONBLOCK 8753c9f6c0SAtsushi Murai #ifdef O_NDELAY 8853c9f6c0SAtsushi Murai #define O_NONBLOCK O_NDELAY 8953c9f6c0SAtsushi Murai #endif 9053c9f6c0SAtsushi Murai #endif 91af57ed9fSAtsushi Murai 920f78c7a7SBrian Somers static void DoLoop(struct bundle *); 9375240ed1SBrian Somers static void TerminalStop(int); 94b6e82f33SBrian Somers static const char *ex_desc(int); 9575240ed1SBrian Somers 9683d1af55SBrian Somers static struct bundle *SignalBundle; 97b6217683SBrian Somers static struct prompt *SignalPrompt; 98c3899f8dSAtsushi Murai 99c3899f8dSAtsushi Murai void 100944f7098SBrian Somers Cleanup(int excode) 101af57ed9fSAtsushi Murai { 102a0cbd833SBrian Somers SignalBundle->CleaningUp = 1; 1039c81b87dSBrian Somers bundle_Close(SignalBundle, NULL, CLOSE_STAYDOWN); 1046e4959f0SBrian Somers } 105af57ed9fSAtsushi Murai 1061afedc4bSBrian Somers void 1071afedc4bSBrian Somers AbortProgram(int excode) 1081afedc4bSBrian Somers { 109dd7e2610SBrian Somers server_Close(SignalBundle); 110dd7e2610SBrian Somers log_Printf(LogPHASE, "PPP Terminated (%s).\n", ex_desc(excode)); 1119c81b87dSBrian Somers bundle_Close(SignalBundle, NULL, CLOSE_STAYDOWN); 11268a0f0ccSBrian Somers bundle_Destroy(SignalBundle); 113dd7e2610SBrian Somers log_Close(); 114af57ed9fSAtsushi Murai exit(excode); 115af57ed9fSAtsushi Murai } 116af57ed9fSAtsushi Murai 117af57ed9fSAtsushi Murai static void 118944f7098SBrian Somers CloseConnection(int signo) 119af57ed9fSAtsushi Murai { 120368aee2bSBrian Somers /* NOTE, these are manual, we've done a setsid() */ 121dd7e2610SBrian Somers sig_signal(SIGINT, SIG_IGN); 122dd7e2610SBrian Somers log_Printf(LogPHASE, "Caught signal %d, abort connection(s)\n", signo); 123899011c4SBrian Somers bundle_Down(SignalBundle, CLOSE_STAYDOWN); 124dd7e2610SBrian Somers sig_signal(SIGINT, CloseConnection); 1256d14e2a8SJordan K. Hubbard } 126af57ed9fSAtsushi Murai 127af57ed9fSAtsushi Murai static void 128944f7098SBrian Somers CloseSession(int signo) 129af57ed9fSAtsushi Murai { 130dd7e2610SBrian Somers log_Printf(LogPHASE, "Signal %d, terminate.\n", signo); 131af57ed9fSAtsushi Murai Cleanup(EX_TERM); 132af57ed9fSAtsushi Murai } 133c3899f8dSAtsushi Murai 13475b8d283SBrian Somers static pid_t BGPid = 0; 13575b8d283SBrian Somers 13675b8d283SBrian Somers static void 13775b8d283SBrian Somers KillChild(int signo) 13875b8d283SBrian Somers { 139ac37ab22SBrian Somers signal(signo, SIG_IGN); 140dd7e2610SBrian Somers log_Printf(LogPHASE, "Parent: Signal %d\n", signo); 14175b8d283SBrian Somers kill(BGPid, SIGINT); 14275b8d283SBrian Somers } 14375b8d283SBrian Somers 144c3899f8dSAtsushi Murai static void 145b6e82f33SBrian Somers TerminalCont(int signo) 146c3899f8dSAtsushi Murai { 147f91ad6b0SBrian Somers signal(SIGCONT, SIG_DFL); 148f91ad6b0SBrian Somers prompt_Continue(SignalPrompt); 149c3899f8dSAtsushi Murai } 150c3899f8dSAtsushi Murai 151c3899f8dSAtsushi Murai static void 152944f7098SBrian Somers TerminalStop(int signo) 153c3899f8dSAtsushi Murai { 154f91ad6b0SBrian Somers prompt_Suspend(SignalPrompt); 155f91ad6b0SBrian Somers signal(SIGCONT, TerminalCont); 156f91ad6b0SBrian Somers raise(SIGSTOP); 1574ef16f24SBrian Somers } 1584ef16f24SBrian Somers 1598ea8442cSBrian Somers static void 1608ea8442cSBrian Somers BringDownServer(int signo) 1618ea8442cSBrian Somers { 162b6217683SBrian Somers /* Drops all child prompts too ! */ 163dd7e2610SBrian Somers server_Close(SignalBundle); 1648ea8442cSBrian Somers } 1658ea8442cSBrian Somers 166b6e82f33SBrian Somers static const char * 1676efd9292SBrian Somers ex_desc(int ex) 1686efd9292SBrian Somers { 169d93d3a9cSBrian Somers static char num[12]; /* Used immediately if returned */ 170b6e82f33SBrian Somers static const char *desc[] = { 171b6e82f33SBrian Somers "normal", "start", "sock", "modem", "dial", "dead", "done", 172b6e82f33SBrian Somers "reboot", "errdead", "hangup", "term", "nodial", "nologin" 173b6e82f33SBrian Somers }; 1746efd9292SBrian Somers 17570ee81ffSBrian Somers if (ex >= 0 && ex < sizeof desc / sizeof *desc) 1766efd9292SBrian Somers return desc[ex]; 1776efd9292SBrian Somers snprintf(num, sizeof num, "%d", ex); 1786efd9292SBrian Somers return num; 1796efd9292SBrian Somers } 180c3899f8dSAtsushi Murai 18175240ed1SBrian Somers static void 182b6e82f33SBrian Somers Usage(void) 183af57ed9fSAtsushi Murai { 184680026d6SNate Williams fprintf(stderr, 18567b072f7SBrian Somers "Usage: ppp [-auto | -foreground | -background | -direct | -dedicated | -ddial | -interactive]" 186b6e82f33SBrian Somers #ifndef NOALIAS 18767b072f7SBrian Somers " [-nat]" 188b6e82f33SBrian Somers #endif 189c0593e34SBrian Somers " [-quiet] [-unit N] [system ...]\n"); 190af57ed9fSAtsushi Murai exit(EX_START); 191af57ed9fSAtsushi Murai } 192af57ed9fSAtsushi Murai 193c0593e34SBrian Somers struct switches { 194c0593e34SBrian Somers unsigned nat : 1; 195c0593e34SBrian Somers unsigned fg : 1; 196c0593e34SBrian Somers unsigned quiet : 1; 197c0593e34SBrian Somers int mode; 198c0593e34SBrian Somers int unit; 199c0593e34SBrian Somers }; 200c0593e34SBrian Somers 2017cf368ebSBrian Somers static int 202c0593e34SBrian Somers ProcessArgs(int argc, char **argv, struct switches *sw) 203af57ed9fSAtsushi Murai { 2047cf368ebSBrian Somers int optc, newmode, arg; 205af57ed9fSAtsushi Murai char *cp; 206af57ed9fSAtsushi Murai 2077cf368ebSBrian Somers optc = 0; 208c0593e34SBrian Somers memset(sw, '\0', sizeof *sw); 209c0593e34SBrian Somers sw->mode = PHYS_INTERACTIVE; 210c0593e34SBrian Somers sw->unit = -1; 211c0593e34SBrian Somers 2127cf368ebSBrian Somers for (arg = 1; arg < argc && *argv[arg] == '-'; arg++, optc++) { 2137cf368ebSBrian Somers cp = argv[arg] + 1; 21481358fa3SBrian Somers newmode = Nam2mode(cp); 21581358fa3SBrian Somers switch (newmode) { 21681358fa3SBrian Somers case PHYS_NONE: 217c0593e34SBrian Somers if (strcmp(cp, "nat") == 0) { 21867b072f7SBrian Somers #ifdef NONAT 219c0593e34SBrian Somers log_Printf(LogWARN, "%s ignored: NAT is compiled out\n", argv[arg]); 220615ad4f9SBrian Somers #else 221c0593e34SBrian Somers sw->nat = 1; 222615ad4f9SBrian Somers #endif 2231ae349f5Scvs2svn optc--; /* this option isn't exclusive */ 224c0593e34SBrian Somers } else if (strcmp(cp, "alias") == 0) { 225c0593e34SBrian Somers #ifdef NONAT 226c0593e34SBrian Somers log_Printf(LogWARN, "%s ignored: NAT is compiled out\n", argv[arg]); 227c0593e34SBrian Somers fprintf(stderr, "%s ignored: NAT is compiled out\n", argv[arg]); 228c0593e34SBrian Somers #else 229c0593e34SBrian Somers log_Printf(LogWARN, "%s is depricated\n", argv[arg]); 230c0593e34SBrian Somers fprintf(stderr, "%s is depricated\n", argv[arg]); 231c0593e34SBrian Somers sw->nat = 1; 232c0593e34SBrian Somers #endif 233c0593e34SBrian Somers optc--; /* this option isn't exclusive */ 234c0593e34SBrian Somers } else if (strncmp(cp, "unit", 4) == 0) { 2359d06928dSBrian Somers optc--; /* this option isn't exclusive */ 236c0593e34SBrian Somers if (cp[4] == '\0') { 2379d06928dSBrian Somers optc--; /* nor is the argument */ 238c0593e34SBrian Somers if (++arg == argc) { 239c0593e34SBrian Somers fprintf(stderr, "-unit: Expected unit number\n"); 240c0593e34SBrian Somers Usage(); 241c0593e34SBrian Somers } else 242c0593e34SBrian Somers sw->unit = atoi(argv[arg]); 243c0593e34SBrian Somers } else 244c0593e34SBrian Somers sw->unit = atoi(cp + 4); 24567b072f7SBrian Somers } else if (strcmp(cp, "quiet") == 0) { 246c0593e34SBrian Somers sw->quiet = 1; 24767b072f7SBrian Somers optc--; /* this option isn't exclusive */ 248944f7098SBrian Somers } else 249af57ed9fSAtsushi Murai Usage(); 25081358fa3SBrian Somers break; 25181358fa3SBrian Somers 25281358fa3SBrian Somers case PHYS_ALL: 25381358fa3SBrian Somers Usage(); 25481358fa3SBrian Somers break; 25581358fa3SBrian Somers 25681358fa3SBrian Somers default: 257c0593e34SBrian Somers sw->mode = newmode; 258f6a4e748SBrian Somers if (newmode == PHYS_FOREGROUND) 259f6a4e748SBrian Somers sw->fg = 1; 26081358fa3SBrian Somers } 261af57ed9fSAtsushi Murai } 262af57ed9fSAtsushi Murai 263af57ed9fSAtsushi Murai if (optc > 1) { 26485602e52SBrian Somers fprintf(stderr, "You may specify only one mode.\n"); 2651ae349f5Scvs2svn exit(EX_START); 2661ae349f5Scvs2svn } 2671ae349f5Scvs2svn 268c0593e34SBrian Somers if (sw->mode == PHYS_AUTO && arg == argc) { 2697cf368ebSBrian Somers fprintf(stderr, "A system must be specified in auto mode.\n"); 270af57ed9fSAtsushi Murai exit(EX_START); 271af57ed9fSAtsushi Murai } 27239f94eddSBrian Somers 2737cf368ebSBrian Somers return arg; /* Don't SetLabel yet ! */ 274af57ed9fSAtsushi Murai } 275af57ed9fSAtsushi Murai 2767cf368ebSBrian Somers static void 2777cf368ebSBrian Somers CheckLabel(const char *label, struct prompt *prompt, int mode) 2787cf368ebSBrian Somers { 2797cf368ebSBrian Somers const char *err; 2807cf368ebSBrian Somers 2817cf368ebSBrian Somers if ((err = system_IsValid(label, prompt, mode)) != NULL) { 2827cf368ebSBrian Somers fprintf(stderr, "%s: %s\n", label, err); 2837cf368ebSBrian Somers if (mode == PHYS_DIRECT) 2847cf368ebSBrian Somers log_Printf(LogWARN, "Label %s rejected -direct connection: %s\n", 2857cf368ebSBrian Somers label, err); 2867cf368ebSBrian Somers log_Close(); 2877cf368ebSBrian Somers exit(1); 2887cf368ebSBrian Somers } 2897cf368ebSBrian Somers } 2907cf368ebSBrian Somers 2917cf368ebSBrian Somers 2924ef16f24SBrian Somers int 293944f7098SBrian Somers main(int argc, char **argv) 294af57ed9fSAtsushi Murai { 2957cf368ebSBrian Somers char *name; 296756783fcSBrian Somers const char *lastlabel; 297c0593e34SBrian Somers int nfds, label, arg; 2987a6f8720SBrian Somers struct bundle *bundle; 299b6217683SBrian Somers struct prompt *prompt; 300c0593e34SBrian Somers struct switches sw; 301e3b4c400SBrian Somers 302e3b4c400SBrian Somers nfds = getdtablesize(); 303e3b4c400SBrian Somers if (nfds >= FD_SETSIZE) 304e3b4c400SBrian Somers /* 305e3b4c400SBrian Somers * If we've got loads of file descriptors, make sure they're all 306e3b4c400SBrian Somers * closed. If they aren't, we may end up with a seg fault when our 307e3b4c400SBrian Somers * `fd_set's get too big when select()ing ! 308e3b4c400SBrian Somers */ 309e3b4c400SBrian Somers while (--nfds > 2) 310e3b4c400SBrian Somers close(nfds); 311af57ed9fSAtsushi Murai 31275240ed1SBrian Somers name = strrchr(argv[0], '/'); 313dd7e2610SBrian Somers log_Open(name ? name + 1 : argv[0]); 3142a279fedSBrian Somers 31567b072f7SBrian Somers #ifndef NONAT 3162a630835SBrian Somers PacketAliasInit(); 3172a630835SBrian Somers #endif 318c0593e34SBrian Somers label = ProcessArgs(argc, argv, &sw); 31985b542cfSBrian Somers 32085b542cfSBrian Somers /* 321a611383fSBrian Somers * A FreeBSD & OpenBSD hack to dodge a bug in the tty driver that drops 322a611383fSBrian Somers * output occasionally.... I must find the real reason some time. To 323a611383fSBrian Somers * display the dodgy behaviour, comment out this bit, make yourself a large 32485b542cfSBrian Somers * routing table and then run ppp in interactive mode. The `show route' 32585b542cfSBrian Somers * command will drop chunks of data !!! 32685b542cfSBrian Somers */ 327c0593e34SBrian Somers if (sw.mode == PHYS_INTERACTIVE) { 32885b542cfSBrian Somers close(STDIN_FILENO); 32985b542cfSBrian Somers if (open(_PATH_TTY, O_RDONLY) != STDIN_FILENO) { 33085b542cfSBrian Somers fprintf(stderr, "Cannot open %s for input !\n", _PATH_TTY); 33185b542cfSBrian Somers return 2; 33285b542cfSBrian Somers } 33385b542cfSBrian Somers } 33485b542cfSBrian Somers 335b6217683SBrian Somers /* Allow output for the moment (except in direct mode) */ 336c0593e34SBrian Somers if (sw.mode == PHYS_DIRECT) 337b6217683SBrian Somers prompt = NULL; 338ed0e9269SBrian Somers else 339b6217683SBrian Somers SignalPrompt = prompt = prompt_Create(NULL, NULL, PROMPT_STD); 34012ef29a8SBrian Somers 3415106c671SBrian Somers ID0init(); 3424562be74SBrian Somers if (ID0realuid() != 0) { 3434562be74SBrian Somers char conf[200], *ptr; 3444562be74SBrian Somers 3454562be74SBrian Somers snprintf(conf, sizeof conf, "%s/%s", _PATH_PPP, CONFFILE); 3464562be74SBrian Somers do { 3471080ea25SBrian Somers struct stat sb; 3481080ea25SBrian Somers 3491080ea25SBrian Somers if (stat(conf, &sb) == 0 && sb.st_mode & S_IWOTH) { 350a33b2ef7SBrian Somers log_Printf(LogALERT, "ppp: Access violation: Please protect %s\n", 351a33b2ef7SBrian Somers conf); 3524562be74SBrian Somers return -1; 3534562be74SBrian Somers } 3544562be74SBrian Somers ptr = conf + strlen(conf)-2; 3554562be74SBrian Somers while (ptr > conf && *ptr != '/') 3564562be74SBrian Somers *ptr-- = '\0'; 3574562be74SBrian Somers } while (ptr >= conf); 3584562be74SBrian Somers } 3594562be74SBrian Somers 3607cf368ebSBrian Somers if (label < argc) 3617cf368ebSBrian Somers for (arg = label; arg < argc; arg++) 362c0593e34SBrian Somers CheckLabel(argv[arg], prompt, sw.mode); 3637cf368ebSBrian Somers else 364c0593e34SBrian Somers CheckLabel("default", prompt, sw.mode); 36512ef29a8SBrian Somers 366c0593e34SBrian Somers if (!sw.quiet) 367c0593e34SBrian Somers prompt_Printf(prompt, "Working in %s mode\n", mode2Nam(sw.mode)); 368ed0e9269SBrian Somers 369cf0a3940SBrian Somers if ((bundle = bundle_Create(TUN_PREFIX, sw.mode, sw.unit)) == NULL) 3704ef16f24SBrian Somers return EX_START; 371756783fcSBrian Somers 372756783fcSBrian Somers /* NOTE: We may now have changed argv[1] via a ``set proctitle'' */ 373756783fcSBrian Somers 3740f2f3eb3SBrian Somers if (prompt) { 3750f2f3eb3SBrian Somers prompt->bundle = bundle; /* couldn't do it earlier */ 376c0593e34SBrian Somers if (!sw.quiet) 3778fa6ebe4SBrian Somers prompt_Printf(prompt, "Using interface: %s\n", bundle->iface->name); 3780f2f3eb3SBrian Somers } 37983d1af55SBrian Somers SignalBundle = bundle; 380c0593e34SBrian Somers bundle->NatEnabled = sw.nat; 381c0593e34SBrian Somers if (sw.nat) 3828fa6ebe4SBrian Somers bundle->cfg.opt |= OPT_IFACEALIAS; 38312ef29a8SBrian Somers 38430291ffbSBrian Somers if (system_Select(bundle, "default", CONFFILE, prompt, NULL) < 0) 385565e35e5SBrian Somers prompt_Printf(prompt, "Warning: No default entry found in config file.\n"); 3861ae349f5Scvs2svn 387dd7e2610SBrian Somers sig_signal(SIGHUP, CloseSession); 388dd7e2610SBrian Somers sig_signal(SIGTERM, CloseSession); 389dd7e2610SBrian Somers sig_signal(SIGINT, CloseConnection); 390dd7e2610SBrian Somers sig_signal(SIGQUIT, CloseSession); 391dd7e2610SBrian Somers sig_signal(SIGALRM, SIG_IGN); 392e0d3e233SAndrey A. Chernov signal(SIGPIPE, SIG_IGN); 393565e35e5SBrian Somers 394c0593e34SBrian Somers if (sw.mode == PHYS_INTERACTIVE) 395dd7e2610SBrian Somers sig_signal(SIGTSTP, TerminalStop); 396f91ad6b0SBrian Somers 397dd7e2610SBrian Somers sig_signal(SIGUSR2, BringDownServer); 398af57ed9fSAtsushi Murai 399cf0a3940SBrian Somers lastlabel = argv[argc - 1]; 4007cf368ebSBrian Somers for (arg = label; arg < argc; arg++) { 4017cf368ebSBrian Somers /* In case we use LABEL or ``set enddisc label'' */ 402756783fcSBrian Somers bundle_SetLabel(bundle, lastlabel); 403cf0a3940SBrian Somers system_Select(bundle, argv[arg], CONFFILE, prompt, NULL); 4041ae349f5Scvs2svn } 4057cf368ebSBrian Somers 4067cf368ebSBrian Somers if (label < argc) 4077cf368ebSBrian Somers /* In case the last label did a ``load'' */ 408756783fcSBrian Somers bundle_SetLabel(bundle, lastlabel); 4097cf368ebSBrian Somers 410c0593e34SBrian Somers if (sw.mode == PHYS_AUTO && 4115828db6dSBrian Somers bundle->ncp.ipcp.cfg.peer_range.ipaddr.s_addr == INADDR_ANY) { 4127cf368ebSBrian Somers prompt_Printf(prompt, "You must ``set ifaddr'' with a peer address " 4137cf368ebSBrian Somers "in auto mode.\n"); 4148390b576SBrian Somers AbortProgram(EX_START); 415af57ed9fSAtsushi Murai } 4166efd9292SBrian Somers 417c0593e34SBrian Somers if (sw.mode != PHYS_INTERACTIVE) { 418c0593e34SBrian Somers if (sw.mode != PHYS_DIRECT) { 419c0593e34SBrian Somers if (!sw.fg) { 4205cf4388bSBrian Somers int bgpipe[2]; 4216d14e2a8SJordan K. Hubbard pid_t bgpid; 422a9c6b5dfSAtsushi Murai 423c0593e34SBrian Somers if (sw.mode == PHYS_BACKGROUND && pipe(bgpipe)) { 424dd7e2610SBrian Somers log_Printf(LogERROR, "pipe: %s\n", strerror(errno)); 4258390b576SBrian Somers AbortProgram(EX_SOCK); 4261ae349f5Scvs2svn } 4271ae349f5Scvs2svn 4286d14e2a8SJordan K. Hubbard bgpid = fork(); 4296d14e2a8SJordan K. Hubbard if (bgpid == -1) { 430dd7e2610SBrian Somers log_Printf(LogERROR, "fork: %s\n", strerror(errno)); 4318390b576SBrian Somers AbortProgram(EX_SOCK); 4326d14e2a8SJordan K. Hubbard } 4335cf4388bSBrian Somers 4346d14e2a8SJordan K. Hubbard if (bgpid) { 4356d14e2a8SJordan K. Hubbard char c = EX_NORMAL; 436a9c6b5dfSAtsushi Murai 437c0593e34SBrian Somers if (sw.mode == PHYS_BACKGROUND) { 4385cf4388bSBrian Somers close(bgpipe[1]); 4396d14e2a8SJordan K. Hubbard BGPid = bgpid; 44075b8d283SBrian Somers /* If we get a signal, kill the child */ 44175b8d283SBrian Somers signal(SIGHUP, KillChild); 44275b8d283SBrian Somers signal(SIGTERM, KillChild); 44375b8d283SBrian Somers signal(SIGINT, KillChild); 44475b8d283SBrian Somers signal(SIGQUIT, KillChild); 44575b8d283SBrian Somers 44675b8d283SBrian Somers /* Wait for our child to close its pipe before we exit */ 4475cf4388bSBrian Somers if (read(bgpipe[0], &c, 1) != 1) { 448b6217683SBrian Somers prompt_Printf(prompt, "Child exit, no status.\n"); 449dd7e2610SBrian Somers log_Printf(LogPHASE, "Parent: Child exit, no status.\n"); 4506efd9292SBrian Somers } else if (c == EX_NORMAL) { 451b6217683SBrian Somers prompt_Printf(prompt, "PPP enabled.\n"); 452dd7e2610SBrian Somers log_Printf(LogPHASE, "Parent: PPP enabled.\n"); 4536efd9292SBrian Somers } else { 454b6217683SBrian Somers prompt_Printf(prompt, "Child failed (%s).\n", ex_desc((int) c)); 455dd7e2610SBrian Somers log_Printf(LogPHASE, "Parent: Child failed (%s).\n", 45680e37c72SBrian Somers ex_desc((int) c)); 4576efd9292SBrian Somers } 4585cf4388bSBrian Somers close(bgpipe[0]); 4596d14e2a8SJordan K. Hubbard } 4604ef16f24SBrian Somers return c; 461c0593e34SBrian Somers } else if (sw.mode == PHYS_BACKGROUND) { 4625cf4388bSBrian Somers close(bgpipe[0]); 4635cf4388bSBrian Somers bundle->notify.fd = bgpipe[1]; 464aefd026aSBrian Somers } 465aefd026aSBrian Somers 466da66dd13SBrian Somers bundle_LockTun(bundle); /* we have a new pid */ 46767b072f7SBrian Somers } 468da66dd13SBrian Somers 46967b072f7SBrian Somers /* -auto, -dedicated, -ddial, -foreground & -background */ 470b6217683SBrian Somers prompt_Destroy(prompt, 0); 4712a279fedSBrian Somers close(STDOUT_FILENO); 4722a279fedSBrian Somers close(STDERR_FILENO); 4732a279fedSBrian Somers close(STDIN_FILENO); 474c0593e34SBrian Somers if (!sw.fg) 475b6217683SBrian Somers setsid(); 476565e35e5SBrian Somers } else { 47767b072f7SBrian Somers /* -direct - STDIN_FILENO gets used by physical_Open */ 478565e35e5SBrian Somers prompt_TtyInit(NULL); 479565e35e5SBrian Somers close(STDOUT_FILENO); 480565e35e5SBrian Somers close(STDERR_FILENO); 481d656a4c5SBrian Somers } 482af57ed9fSAtsushi Murai } else { 48367b072f7SBrian Somers /* -interactive */ 4842a279fedSBrian Somers close(STDERR_FILENO); 485565e35e5SBrian Somers prompt_TtyInit(prompt); 486b6217683SBrian Somers prompt_TtyCommandMode(prompt); 487b6217683SBrian Somers prompt_Required(prompt); 488af57ed9fSAtsushi Murai } 48935495becSBrian Somers 490c0593e34SBrian Somers log_Printf(LogPHASE, "PPP Started (%s mode).\n", mode2Nam(sw.mode)); 4910f78c7a7SBrian Somers DoLoop(bundle); 4921afedc4bSBrian Somers AbortProgram(EX_NORMAL); 493af57ed9fSAtsushi Murai 4941afedc4bSBrian Somers return EX_NORMAL; 495af57ed9fSAtsushi Murai } 496af57ed9fSAtsushi Murai 497af57ed9fSAtsushi Murai static void 4980f78c7a7SBrian Somers DoLoop(struct bundle *bundle) 499af57ed9fSAtsushi Murai { 500af57ed9fSAtsushi Murai fd_set rfds, wfds, efds; 5011af29a6eSBrian Somers int i, nfds, nothing_done; 5021af29a6eSBrian Somers struct probe probe; 5031af29a6eSBrian Somers 5041af29a6eSBrian Somers probe_Init(&probe); 505c3899f8dSAtsushi Murai 506da8b7034SBrian Somers for (; !bundle_IsDead(bundle); bundle_CleanDatalinks(bundle)) { 507780700e5SAndrey A. Chernov nfds = 0; 508944f7098SBrian Somers FD_ZERO(&rfds); 509944f7098SBrian Somers FD_ZERO(&wfds); 510944f7098SBrian Somers FD_ZERO(&efds); 51184b8a6ebSAtsushi Murai 5120f2f3eb3SBrian Somers /* All our datalinks, the tun device and the MP socket */ 5136f384573SBrian Somers descriptor_UpdateSet(&bundle->desc, &rfds, &wfds, &efds, &nfds); 5140f2f3eb3SBrian Somers 5150f2f3eb3SBrian Somers /* All our prompts and the diagnostic socket */ 5160f2f3eb3SBrian Somers descriptor_UpdateSet(&server.desc, &rfds, NULL, NULL, &nfds); 51707030d97SBrian Somers 5183b0f8d2eSBrian Somers if (bundle_IsDead(bundle)) 519f4768038SBrian Somers /* Don't select - we'll be here forever */ 520f4768038SBrian Somers break; 5210706ff38SBrian Somers 522486105bcSBrian Somers /* 523486105bcSBrian Somers * It's possible that we've had a signal since we last checked. If 524486105bcSBrian Somers * we don't check again before calling select(), we may end up stuck 525486105bcSBrian Somers * after having missed the event.... sig_Handle() tries to be as 526486105bcSBrian Somers * quick as possible if nothing is likely to have happened. 527486105bcSBrian Somers * This is only really likely if we block in open(... O_NONBLOCK) 528486105bcSBrian Somers * which will happen with a misconfigured device. 529486105bcSBrian Somers */ 530486105bcSBrian Somers if (sig_Handle()) 531486105bcSBrian Somers continue; 532486105bcSBrian Somers 5333006ec67SBrian Somers i = select(nfds, &rfds, &wfds, &efds, NULL); 534712ae387SBrian Somers 53554cd8e13SBrian Somers if (i < 0 && errno != EINTR) { 536dd7e2610SBrian Somers log_Printf(LogERROR, "DoLoop: select(): %s\n", strerror(errno)); 53724989c68SBrian Somers if (log_IsKept(LogTIMER)) { 53824989c68SBrian Somers struct timeval t; 53924989c68SBrian Somers 54024989c68SBrian Somers for (i = 0; i <= nfds; i++) { 54124989c68SBrian Somers if (FD_ISSET(i, &rfds)) { 54224989c68SBrian Somers log_Printf(LogTIMER, "Read set contains %d\n", i); 54324989c68SBrian Somers FD_CLR(i, &rfds); 54424989c68SBrian Somers t.tv_sec = t.tv_usec = 0; 54524989c68SBrian Somers if (select(nfds, &rfds, &wfds, &efds, &t) != -1) { 54624989c68SBrian Somers log_Printf(LogTIMER, "The culprit !\n"); 547af57ed9fSAtsushi Murai break; 548af57ed9fSAtsushi Murai } 54924989c68SBrian Somers } 55024989c68SBrian Somers if (FD_ISSET(i, &wfds)) { 55124989c68SBrian Somers log_Printf(LogTIMER, "Write set contains %d\n", i); 55224989c68SBrian Somers FD_CLR(i, &wfds); 55324989c68SBrian Somers t.tv_sec = t.tv_usec = 0; 55424989c68SBrian Somers if (select(nfds, &rfds, &wfds, &efds, &t) != -1) { 55524989c68SBrian Somers log_Printf(LogTIMER, "The culprit !\n"); 556af57ed9fSAtsushi Murai break; 557af57ed9fSAtsushi Murai } 558e0d3e233SAndrey A. Chernov } 55924989c68SBrian Somers if (FD_ISSET(i, &efds)) { 56024989c68SBrian Somers log_Printf(LogTIMER, "Error set contains %d\n", i); 56124989c68SBrian Somers FD_CLR(i, &efds); 56224989c68SBrian Somers t.tv_sec = t.tv_usec = 0; 56324989c68SBrian Somers if (select(nfds, &rfds, &wfds, &efds, &t) != -1) { 56424989c68SBrian Somers log_Printf(LogTIMER, "The culprit !\n"); 56558f264e1SBrian Somers break; 56658f264e1SBrian Somers } 56724989c68SBrian Somers } 56824989c68SBrian Somers } 56924989c68SBrian Somers } 57058f264e1SBrian Somers break; 571de451c68SBrian Somers } 572de451c68SBrian Somers 573f0cdd9c0SBrian Somers log_Printf(LogTIMER, "Select returns %d\n", i); 574f0cdd9c0SBrian Somers 57554cd8e13SBrian Somers sig_Handle(); 57654cd8e13SBrian Somers 57754cd8e13SBrian Somers if (i <= 0) 57854cd8e13SBrian Somers continue; 57954cd8e13SBrian Somers 5803006ec67SBrian Somers for (i = 0; i <= nfds; i++) 5813006ec67SBrian Somers if (FD_ISSET(i, &efds)) { 582991c2a7bSBrian Somers log_Printf(LogPHASE, "Exception detected on descriptor %d\n", i); 583991c2a7bSBrian Somers /* We deal gracefully with link descriptor exceptions */ 584991c2a7bSBrian Somers if (!bundle_Exception(bundle, i)) { 585991c2a7bSBrian Somers log_Printf(LogERROR, "Exception cannot be handled !\n"); 5861ae349f5Scvs2svn break; 5871ae349f5Scvs2svn } 588991c2a7bSBrian Somers } 589c60f92caSBrian Somers 590b7c5748eSBrian Somers if (i <= nfds) 591b7c5748eSBrian Somers break; 592b7c5748eSBrian Somers 5931af29a6eSBrian Somers nothing_done = 1; 5941af29a6eSBrian Somers 5951af29a6eSBrian Somers if (descriptor_IsSet(&server.desc, &rfds)) { 596b77776a7SBrian Somers descriptor_Read(&server.desc, bundle, &rfds); 5971af29a6eSBrian Somers nothing_done = 0; 5981af29a6eSBrian Somers } 5991af29a6eSBrian Somers 6001af29a6eSBrian Somers if (descriptor_IsSet(&bundle->desc, &rfds)) { 6011af29a6eSBrian Somers descriptor_Read(&bundle->desc, bundle, &rfds); 6021af29a6eSBrian Somers nothing_done = 0; 6031af29a6eSBrian Somers } 60442d4d396SBrian Somers 6052f786681SBrian Somers if (descriptor_IsSet(&bundle->desc, &wfds)) 6061af29a6eSBrian Somers if (!descriptor_Write(&bundle->desc, bundle, &wfds) && nothing_done) { 6071af29a6eSBrian Somers /* 6081af29a6eSBrian Somers * This is disasterous. The OS has told us that something is 6091af29a6eSBrian Somers * writable, and all our write()s have failed. Rather than 6101af29a6eSBrian Somers * going back immediately to do our UpdateSet()s and select(), 6111af29a6eSBrian Somers * we sleep for a bit to avoid gobbling up all cpu time. 6121af29a6eSBrian Somers */ 6131af29a6eSBrian Somers struct timeval t; 6142f786681SBrian Somers 6151af29a6eSBrian Somers t.tv_sec = 0; 6161af29a6eSBrian Somers t.tv_usec = 100000; 6171af29a6eSBrian Somers select(0, NULL, NULL, NULL, &t); 6181af29a6eSBrian Somers } 619da8b7034SBrian Somers } 620565e35e5SBrian Somers 621dd7e2610SBrian Somers log_Printf(LogDEBUG, "DoLoop done.\n"); 622af57ed9fSAtsushi Murai } 623