1998-01-29 00:44:16 +00:00
|
|
|
/*
|
|
|
|
* User Process PPP
|
|
|
|
*
|
|
|
|
* Written by Toshiharu OHNO (tony-o@iij.ad.jp)
|
|
|
|
*
|
|
|
|
* Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms are permitted
|
|
|
|
* provided that the above copyright notice and this paragraph are
|
|
|
|
* duplicated in all such forms and that any documentation,
|
|
|
|
* advertising materials, and other materials related to such
|
|
|
|
* distribution and use acknowledge that the software was developed
|
|
|
|
* by the Internet Initiative Japan, Inc. The name of the
|
|
|
|
* IIJ may not be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
*
|
1998-04-30 23:53:56 +00:00
|
|
|
* $Id: main.c,v 1.121.2.52 1998/04/28 01:25:30 brian Exp $
|
1998-01-29 00:44:16 +00:00
|
|
|
*
|
|
|
|
* TODO:
|
|
|
|
*/
|
1998-04-07 00:54:26 +00:00
|
|
|
|
1998-01-29 00:44:16 +00:00
|
|
|
#include <sys/param.h>
|
1998-04-25 10:49:52 +00:00
|
|
|
#include <sys/socket.h>
|
1998-01-29 00:44:16 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
1998-04-10 13:19:23 +00:00
|
|
|
#include <sys/un.h>
|
1998-04-25 10:49:52 +00:00
|
|
|
#include <net/if_tun.h>
|
1998-01-29 00:44:16 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <paths.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "mbuf.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "defs.h"
|
|
|
|
#include "id.h"
|
|
|
|
#include "timer.h"
|
|
|
|
#include "fsm.h"
|
1998-03-13 00:44:26 +00:00
|
|
|
#include "lqr.h"
|
1998-01-29 00:44:16 +00:00
|
|
|
#include "hdlc.h"
|
|
|
|
#include "lcp.h"
|
|
|
|
#include "ccp.h"
|
1998-01-29 23:11:44 +00:00
|
|
|
#include "iplist.h"
|
|
|
|
#include "throughput.h"
|
1998-03-16 22:54:35 +00:00
|
|
|
#include "slcompress.h"
|
1998-01-29 00:44:16 +00:00
|
|
|
#include "ipcp.h"
|
1998-03-16 22:52:54 +00:00
|
|
|
#include "filter.h"
|
1998-03-20 19:48:28 +00:00
|
|
|
#include "descriptor.h"
|
1998-04-03 19:21:56 +00:00
|
|
|
#include "link.h"
|
|
|
|
#include "mp.h"
|
1998-03-13 21:07:46 +00:00
|
|
|
#include "bundle.h"
|
1998-01-29 00:44:16 +00:00
|
|
|
#include "loadalias.h"
|
|
|
|
#include "auth.h"
|
|
|
|
#include "systems.h"
|
|
|
|
#include "ip.h"
|
|
|
|
#include "sig.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "pathnames.h"
|
|
|
|
#include "tun.h"
|
1998-02-09 19:24:03 +00:00
|
|
|
#include "server.h"
|
1998-02-10 03:23:50 +00:00
|
|
|
#include "prompt.h"
|
1998-02-13 05:10:26 +00:00
|
|
|
#include "chat.h"
|
1998-03-01 01:07:49 +00:00
|
|
|
#include "chap.h"
|
1998-02-16 00:01:12 +00:00
|
|
|
#include "datalink.h"
|
1998-01-29 00:44:16 +00:00
|
|
|
|
|
|
|
#ifndef O_NONBLOCK
|
|
|
|
#ifdef O_NDELAY
|
|
|
|
#define O_NONBLOCK O_NDELAY
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static char pid_filename[MAXPATHLEN];
|
|
|
|
|
1998-04-03 19:26:02 +00:00
|
|
|
static void DoLoop(struct bundle *, struct prompt *);
|
1998-01-29 00:44:16 +00:00
|
|
|
static void TerminalStop(int);
|
|
|
|
static const char *ex_desc(int);
|
|
|
|
|
1998-02-06 02:24:29 +00:00
|
|
|
static struct bundle *SignalBundle;
|
1998-04-03 19:26:02 +00:00
|
|
|
static struct prompt *SignalPrompt;
|
1998-02-02 19:32:16 +00:00
|
|
|
|
1998-01-29 00:44:16 +00:00
|
|
|
void
|
|
|
|
Cleanup(int excode)
|
|
|
|
{
|
1998-03-16 22:53:15 +00:00
|
|
|
SignalBundle->CleaningUp = 1;
|
1998-02-16 00:01:12 +00:00
|
|
|
if (bundle_Phase(SignalBundle) != PHASE_DEAD)
|
1998-04-03 19:21:56 +00:00
|
|
|
bundle_Close(SignalBundle, NULL, 1);
|
1998-02-08 11:07:32 +00:00
|
|
|
}
|
1998-02-07 20:50:08 +00:00
|
|
|
|
1998-02-08 11:07:32 +00:00
|
|
|
void
|
|
|
|
AbortProgram(int excode)
|
|
|
|
{
|
1998-04-03 19:26:02 +00:00
|
|
|
ServerClose(SignalBundle);
|
1998-01-29 00:44:16 +00:00
|
|
|
ID0unlink(pid_filename);
|
|
|
|
LogPrintf(LogPHASE, "PPP Terminated (%s).\n", ex_desc(excode));
|
1998-02-18 00:27:49 +00:00
|
|
|
bundle_Close(SignalBundle, NULL, 1);
|
1998-02-08 19:29:45 +00:00
|
|
|
bundle_Destroy(SignalBundle);
|
1998-02-16 00:01:12 +00:00
|
|
|
LogClose();
|
1998-01-29 00:44:16 +00:00
|
|
|
exit(excode);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
CloseConnection(int signo)
|
|
|
|
{
|
|
|
|
/* NOTE, these are manual, we've done a setsid() */
|
1998-04-03 19:21:56 +00:00
|
|
|
struct datalink *dl;
|
|
|
|
|
1998-01-29 00:44:16 +00:00
|
|
|
pending_signal(SIGINT, SIG_IGN);
|
1998-04-03 19:21:56 +00:00
|
|
|
LogPrintf(LogPHASE, "Caught signal %d, abort connection(s)\n", signo);
|
|
|
|
for (dl = SignalBundle->links; dl; dl = dl->next)
|
|
|
|
datalink_Down(dl, 1);
|
1998-01-29 00:44:16 +00:00
|
|
|
pending_signal(SIGINT, CloseConnection);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
CloseSession(int signo)
|
|
|
|
{
|
|
|
|
LogPrintf(LogPHASE, "Signal %d, terminate.\n", signo);
|
|
|
|
Cleanup(EX_TERM);
|
|
|
|
}
|
|
|
|
|
1998-03-25 18:38:24 +00:00
|
|
|
static pid_t BGPid = 0;
|
|
|
|
|
|
|
|
static void
|
|
|
|
KillChild(int signo)
|
|
|
|
{
|
|
|
|
LogPrintf(LogPHASE, "Parent: Signal %d\n", signo);
|
|
|
|
kill(BGPid, SIGINT);
|
|
|
|
}
|
|
|
|
|
1998-01-29 00:44:16 +00:00
|
|
|
static void
|
|
|
|
TerminalCont(int signo)
|
|
|
|
{
|
1998-04-05 18:25:34 +00:00
|
|
|
signal(SIGCONT, SIG_DFL);
|
|
|
|
prompt_Continue(SignalPrompt);
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
TerminalStop(int signo)
|
|
|
|
{
|
1998-04-05 18:25:34 +00:00
|
|
|
prompt_Suspend(SignalPrompt);
|
|
|
|
signal(SIGCONT, TerminalCont);
|
|
|
|
raise(SIGSTOP);
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
|
|
|
|
1998-04-03 19:26:02 +00:00
|
|
|
#if 0 /* What's our passwd :-O */
|
1998-01-29 00:44:16 +00:00
|
|
|
static void
|
|
|
|
SetUpServer(int signo)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
|
|
|
VarHaveLocalAuthKey = 0;
|
|
|
|
LocalAuthInit();
|
1998-02-07 20:50:08 +00:00
|
|
|
if ((res = ServerTcpOpen(SERVER_PORT + SignalBundle->unit)) != 0)
|
1998-01-29 00:44:16 +00:00
|
|
|
LogPrintf(LogERROR, "SIGUSR1: Failed %d to open port %d\n",
|
1998-02-07 20:50:08 +00:00
|
|
|
res, SERVER_PORT + SignalBundle->unit);
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
1998-04-03 19:26:02 +00:00
|
|
|
#endif
|
1998-01-29 00:44:16 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
BringDownServer(int signo)
|
|
|
|
{
|
1998-04-03 19:26:02 +00:00
|
|
|
/* Drops all child prompts too ! */
|
|
|
|
ServerClose(SignalBundle);
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
ex_desc(int ex)
|
|
|
|
{
|
|
|
|
static char num[12];
|
|
|
|
static const char *desc[] = {
|
|
|
|
"normal", "start", "sock", "modem", "dial", "dead", "done",
|
|
|
|
"reboot", "errdead", "hangup", "term", "nodial", "nologin"
|
|
|
|
};
|
|
|
|
|
|
|
|
if (ex >= 0 && ex < sizeof desc / sizeof *desc)
|
|
|
|
return desc[ex];
|
|
|
|
snprintf(num, sizeof num, "%d", ex);
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
Usage(void)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"Usage: ppp [-auto | -background | -direct | -dedicated | -ddial ]"
|
|
|
|
#ifndef NOALIAS
|
|
|
|
" [ -alias ]"
|
|
|
|
#endif
|
|
|
|
" [system]\n");
|
|
|
|
exit(EX_START);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
1998-04-10 13:19:23 +00:00
|
|
|
ProcessArgs(int argc, char **argv, int *mode)
|
1998-01-29 00:44:16 +00:00
|
|
|
{
|
1998-04-10 13:19:23 +00:00
|
|
|
int optc, labelrequired;
|
1998-01-29 00:44:16 +00:00
|
|
|
char *cp;
|
|
|
|
|
1998-04-10 13:19:23 +00:00
|
|
|
optc = labelrequired = 0;
|
|
|
|
*mode = PHYS_MANUAL;
|
1998-01-29 00:44:16 +00:00
|
|
|
while (argc > 0 && **argv == '-') {
|
|
|
|
cp = *argv + 1;
|
|
|
|
if (strcmp(cp, "auto") == 0) {
|
1998-04-10 13:19:23 +00:00
|
|
|
*mode = PHYS_DEMAND;
|
|
|
|
labelrequired = 1;
|
1998-01-29 00:44:16 +00:00
|
|
|
} else if (strcmp(cp, "background") == 0) {
|
1998-04-10 13:19:23 +00:00
|
|
|
*mode = PHYS_1OFF;
|
|
|
|
labelrequired = 1;
|
|
|
|
} else if (strcmp(cp, "direct") == 0)
|
1998-04-30 23:53:56 +00:00
|
|
|
*mode = PHYS_DIRECT;
|
1998-04-10 13:19:23 +00:00
|
|
|
else if (strcmp(cp, "dedicated") == 0)
|
|
|
|
*mode = PHYS_DEDICATED;
|
|
|
|
else if (strcmp(cp, "ddial") == 0) {
|
|
|
|
*mode = PHYS_PERM;
|
|
|
|
labelrequired = 1;
|
1998-01-29 00:44:16 +00:00
|
|
|
} else if (strcmp(cp, "alias") == 0) {
|
1998-04-10 13:19:23 +00:00
|
|
|
#ifndef NOALIAS
|
1998-04-07 23:46:09 +00:00
|
|
|
if (loadAliasHandlers() != 0)
|
1998-04-10 13:19:23 +00:00
|
|
|
#endif
|
1998-01-29 00:44:16 +00:00
|
|
|
LogPrintf(LogWARN, "Cannot load alias library\n");
|
|
|
|
optc--; /* this option isn't exclusive */
|
|
|
|
} else
|
|
|
|
Usage();
|
|
|
|
optc++;
|
|
|
|
argv++;
|
|
|
|
argc--;
|
|
|
|
}
|
|
|
|
if (argc > 1) {
|
1998-04-07 23:46:09 +00:00
|
|
|
fprintf(stderr, "You may specify only one system label.\n");
|
1998-01-29 00:44:16 +00:00
|
|
|
exit(EX_START);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optc > 1) {
|
1998-04-07 23:46:09 +00:00
|
|
|
fprintf(stderr, "You may specify only one mode.\n");
|
1998-01-29 00:44:16 +00:00
|
|
|
exit(EX_START);
|
|
|
|
}
|
|
|
|
|
1998-04-10 13:19:23 +00:00
|
|
|
if (labelrequired && argc != 1) {
|
|
|
|
fprintf(stderr, "Destination system must be specified in"
|
|
|
|
" auto, background or ddial mode.\n");
|
|
|
|
exit(EX_START);
|
|
|
|
}
|
|
|
|
|
1998-01-29 00:44:16 +00:00
|
|
|
return argc == 1 ? *argv : NULL; /* Don't SetLabel yet ! */
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
FILE *lockfile;
|
|
|
|
char *name, *label;
|
1998-04-10 13:19:23 +00:00
|
|
|
int nfds, mode;
|
1998-02-02 19:32:16 +00:00
|
|
|
struct bundle *bundle;
|
1998-04-03 19:26:02 +00:00
|
|
|
struct prompt *prompt;
|
1998-01-29 00:44:16 +00:00
|
|
|
|
|
|
|
nfds = getdtablesize();
|
|
|
|
if (nfds >= FD_SETSIZE)
|
|
|
|
/*
|
|
|
|
* If we've got loads of file descriptors, make sure they're all
|
|
|
|
* closed. If they aren't, we may end up with a seg fault when our
|
|
|
|
* `fd_set's get too big when select()ing !
|
|
|
|
*/
|
|
|
|
while (--nfds > 2)
|
|
|
|
close(nfds);
|
|
|
|
|
|
|
|
name = strrchr(argv[0], '/');
|
|
|
|
LogOpen(name ? name + 1 : argv[0]);
|
|
|
|
|
|
|
|
argc--;
|
|
|
|
argv++;
|
1998-04-10 13:19:23 +00:00
|
|
|
label = ProcessArgs(argc, argv, &mode);
|
1998-02-10 03:23:50 +00:00
|
|
|
|
|
|
|
#ifdef __FreeBSD__
|
|
|
|
/*
|
|
|
|
* A FreeBSD hack to dodge a bug in the tty driver that drops output
|
|
|
|
* occasionally.... I must find the real reason some time. To display
|
|
|
|
* the dodgy behaviour, comment out this bit, make yourself a large
|
|
|
|
* routing table and then run ppp in interactive mode. The `show route'
|
|
|
|
* command will drop chunks of data !!!
|
|
|
|
*/
|
1998-04-10 13:19:23 +00:00
|
|
|
if (mode == PHYS_MANUAL) {
|
1998-02-10 03:23:50 +00:00
|
|
|
close(STDIN_FILENO);
|
|
|
|
if (open(_PATH_TTY, O_RDONLY) != STDIN_FILENO) {
|
|
|
|
fprintf(stderr, "Cannot open %s for input !\n", _PATH_TTY);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1998-04-03 19:26:02 +00:00
|
|
|
/* Allow output for the moment (except in direct mode) */
|
1998-04-30 23:53:56 +00:00
|
|
|
if (mode == PHYS_DIRECT)
|
1998-04-03 19:26:02 +00:00
|
|
|
prompt = NULL;
|
1998-04-07 23:46:09 +00:00
|
|
|
else {
|
|
|
|
const char *m;
|
|
|
|
|
1998-04-03 19:26:02 +00:00
|
|
|
SignalPrompt = prompt = prompt_Create(NULL, NULL, PROMPT_STD);
|
1998-04-10 13:19:23 +00:00
|
|
|
if (mode == PHYS_PERM)
|
1998-04-07 23:46:09 +00:00
|
|
|
m = "direct dial";
|
1998-04-10 13:19:23 +00:00
|
|
|
else if (mode & PHYS_1OFF)
|
1998-04-07 23:46:09 +00:00
|
|
|
m = "background";
|
1998-04-10 13:19:23 +00:00
|
|
|
else if (mode & PHYS_DEMAND)
|
1998-04-07 23:46:09 +00:00
|
|
|
m = "auto";
|
1998-04-10 13:19:23 +00:00
|
|
|
else if (mode & PHYS_DEDICATED)
|
1998-04-07 23:46:09 +00:00
|
|
|
m = "dedicated";
|
1998-04-10 13:19:23 +00:00
|
|
|
else if (mode & PHYS_MANUAL)
|
1998-04-07 23:46:09 +00:00
|
|
|
m = "interactive";
|
|
|
|
else
|
|
|
|
m = NULL;
|
|
|
|
|
|
|
|
if (m)
|
|
|
|
prompt_Printf(prompt, "Working in %s mode\n", m);
|
|
|
|
}
|
1998-01-29 00:44:16 +00:00
|
|
|
|
|
|
|
ID0init();
|
|
|
|
if (ID0realuid() != 0) {
|
|
|
|
char conf[200], *ptr;
|
|
|
|
|
|
|
|
snprintf(conf, sizeof conf, "%s/%s", _PATH_PPP, CONFFILE);
|
|
|
|
do {
|
|
|
|
if (!access(conf, W_OK)) {
|
|
|
|
LogPrintf(LogALERT, "ppp: Access violation: Please protect %s\n", conf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ptr = conf + strlen(conf)-2;
|
|
|
|
while (ptr > conf && *ptr != '/')
|
|
|
|
*ptr-- = '\0';
|
|
|
|
} while (ptr >= conf);
|
|
|
|
}
|
|
|
|
|
1998-04-10 13:19:23 +00:00
|
|
|
if (!ValidSystem(label, prompt, mode)) {
|
1998-01-29 00:44:16 +00:00
|
|
|
fprintf(stderr, "You may not use ppp in this mode with this label\n");
|
1998-04-30 23:53:56 +00:00
|
|
|
if (mode == PHYS_DIRECT) {
|
1998-01-29 00:44:16 +00:00
|
|
|
const char *l;
|
|
|
|
l = label ? label : "default";
|
|
|
|
LogPrintf(LogWARN, "Label %s rejected -direct connection\n", l);
|
|
|
|
}
|
|
|
|
LogClose();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1998-04-10 13:19:23 +00:00
|
|
|
if ((bundle = bundle_Create(TUN_PREFIX, prompt, mode)) == NULL) {
|
1998-02-02 19:32:16 +00:00
|
|
|
LogPrintf(LogWARN, "bundle_Create: %s\n", strerror(errno));
|
1998-01-29 00:44:16 +00:00
|
|
|
return EX_START;
|
|
|
|
}
|
1998-02-06 02:24:29 +00:00
|
|
|
SignalBundle = bundle;
|
1998-02-02 19:32:16 +00:00
|
|
|
|
1998-04-03 19:26:02 +00:00
|
|
|
if (SelectSystem(bundle, "default", CONFFILE, prompt) < 0)
|
1998-04-10 13:19:23 +00:00
|
|
|
prompt_Printf(prompt, "Warning: No default entry found in config file.\n");
|
1998-01-29 00:44:16 +00:00
|
|
|
|
|
|
|
pending_signal(SIGHUP, CloseSession);
|
|
|
|
pending_signal(SIGTERM, CloseSession);
|
|
|
|
pending_signal(SIGINT, CloseConnection);
|
|
|
|
pending_signal(SIGQUIT, CloseSession);
|
1998-04-03 19:21:56 +00:00
|
|
|
pending_signal(SIGALRM, SIG_IGN);
|
1998-01-29 00:44:16 +00:00
|
|
|
signal(SIGPIPE, SIG_IGN);
|
1998-04-10 13:19:23 +00:00
|
|
|
|
|
|
|
if (mode == PHYS_MANUAL)
|
1998-01-29 00:44:16 +00:00
|
|
|
pending_signal(SIGTSTP, TerminalStop);
|
1998-04-05 18:25:34 +00:00
|
|
|
|
1998-04-03 19:26:02 +00:00
|
|
|
#if 0 /* What's our passwd :-O */
|
1998-04-05 18:25:34 +00:00
|
|
|
pending_signal(SIGUSR1, SetUpServer);
|
1998-01-29 00:44:16 +00:00
|
|
|
#endif
|
1998-04-05 18:25:34 +00:00
|
|
|
pending_signal(SIGUSR2, BringDownServer);
|
1998-01-29 00:44:16 +00:00
|
|
|
|
|
|
|
if (label) {
|
1998-04-23 03:23:03 +00:00
|
|
|
/*
|
|
|
|
* Set label both before and after SelectSystem !
|
|
|
|
* This way, "set enddisc label" works during SelectSystem, and we
|
|
|
|
* also end up with the correct label if we have embedded load
|
|
|
|
* commands.
|
|
|
|
*/
|
|
|
|
bundle_SetLabel(bundle, label);
|
1998-04-03 19:26:02 +00:00
|
|
|
if (SelectSystem(bundle, label, CONFFILE, prompt) < 0) {
|
|
|
|
prompt_Printf(prompt, "Destination system (%s) not found.\n", label);
|
1998-04-03 19:25:07 +00:00
|
|
|
AbortProgram(EX_START);
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
1998-04-23 03:23:03 +00:00
|
|
|
bundle_SetLabel(bundle, label);
|
1998-04-10 13:19:23 +00:00
|
|
|
if (mode == PHYS_DEMAND &&
|
1998-03-13 21:07:46 +00:00
|
|
|
bundle->ncp.ipcp.cfg.peer_range.ipaddr.s_addr == INADDR_ANY) {
|
1998-04-10 13:19:23 +00:00
|
|
|
prompt_Printf(prompt, "You must \"set ifaddr\" with a peer address "
|
|
|
|
"in label %s for auto mode.\n", label);
|
1998-04-03 19:25:07 +00:00
|
|
|
AbortProgram(EX_START);
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-04-10 13:19:23 +00:00
|
|
|
if (mode != PHYS_MANUAL) {
|
1998-04-30 23:53:56 +00:00
|
|
|
if (mode != PHYS_DIRECT) {
|
1998-03-25 18:38:59 +00:00
|
|
|
int bgpipe[2];
|
1998-01-29 00:44:16 +00:00
|
|
|
pid_t bgpid;
|
|
|
|
|
1998-04-10 13:19:23 +00:00
|
|
|
if (mode == PHYS_1OFF && pipe(bgpipe)) {
|
1998-03-25 18:38:59 +00:00
|
|
|
LogPrintf(LogERROR, "pipe: %s\n", strerror(errno));
|
1998-04-03 19:25:07 +00:00
|
|
|
AbortProgram(EX_SOCK);
|
1998-03-25 18:38:59 +00:00
|
|
|
}
|
|
|
|
|
1998-01-29 00:44:16 +00:00
|
|
|
bgpid = fork();
|
|
|
|
if (bgpid == -1) {
|
|
|
|
LogPrintf(LogERROR, "fork: %s\n", strerror(errno));
|
1998-04-03 19:25:07 +00:00
|
|
|
AbortProgram(EX_SOCK);
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
1998-03-25 18:38:59 +00:00
|
|
|
|
1998-01-29 00:44:16 +00:00
|
|
|
if (bgpid) {
|
|
|
|
char c = EX_NORMAL;
|
|
|
|
|
1998-04-10 13:19:23 +00:00
|
|
|
if (mode == PHYS_1OFF) {
|
1998-03-25 18:38:59 +00:00
|
|
|
close(bgpipe[1]);
|
1998-03-25 18:38:24 +00:00
|
|
|
BGPid = bgpid;
|
|
|
|
/* If we get a signal, kill the child */
|
|
|
|
signal(SIGHUP, KillChild);
|
|
|
|
signal(SIGTERM, KillChild);
|
|
|
|
signal(SIGINT, KillChild);
|
|
|
|
signal(SIGQUIT, KillChild);
|
|
|
|
|
|
|
|
/* Wait for our child to close its pipe before we exit */
|
1998-03-25 18:38:59 +00:00
|
|
|
if (read(bgpipe[0], &c, 1) != 1) {
|
1998-04-03 19:26:02 +00:00
|
|
|
prompt_Printf(prompt, "Child exit, no status.\n");
|
1998-01-29 00:44:16 +00:00
|
|
|
LogPrintf(LogPHASE, "Parent: Child exit, no status.\n");
|
|
|
|
} else if (c == EX_NORMAL) {
|
1998-04-03 19:26:02 +00:00
|
|
|
prompt_Printf(prompt, "PPP enabled.\n");
|
1998-01-29 00:44:16 +00:00
|
|
|
LogPrintf(LogPHASE, "Parent: PPP enabled.\n");
|
|
|
|
} else {
|
1998-04-03 19:26:02 +00:00
|
|
|
prompt_Printf(prompt, "Child failed (%s).\n", ex_desc((int) c));
|
1998-01-29 00:44:16 +00:00
|
|
|
LogPrintf(LogPHASE, "Parent: Child failed (%s).\n",
|
|
|
|
ex_desc((int) c));
|
|
|
|
}
|
1998-03-25 18:38:59 +00:00
|
|
|
close(bgpipe[0]);
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
|
|
|
return c;
|
1998-04-10 13:19:23 +00:00
|
|
|
} else if (mode == PHYS_1OFF) {
|
1998-03-25 18:38:59 +00:00
|
|
|
close(bgpipe[0]);
|
|
|
|
bundle->notify.fd = bgpipe[1];
|
|
|
|
}
|
1998-01-29 00:44:16 +00:00
|
|
|
|
1998-04-10 13:19:23 +00:00
|
|
|
/* -auto, -dedicated, -ddial & -background */
|
1998-04-03 19:26:02 +00:00
|
|
|
prompt_Destroy(prompt, 0);
|
|
|
|
close(STDOUT_FILENO);
|
|
|
|
close(STDERR_FILENO);
|
1998-01-29 00:44:16 +00:00
|
|
|
close(STDIN_FILENO);
|
1998-04-03 19:26:02 +00:00
|
|
|
setsid();
|
1998-04-10 13:19:23 +00:00
|
|
|
} else {
|
|
|
|
/* -direct: STDIN_FILENO gets used by modem_Open */
|
|
|
|
prompt_TtyInit(NULL);
|
|
|
|
close(STDOUT_FILENO);
|
|
|
|
close(STDERR_FILENO);
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
|
|
|
} else {
|
1998-04-10 13:19:23 +00:00
|
|
|
/* Interactive mode */
|
1998-01-29 00:44:16 +00:00
|
|
|
close(STDERR_FILENO);
|
1998-04-10 13:19:23 +00:00
|
|
|
prompt_TtyInit(prompt);
|
1998-04-03 19:26:02 +00:00
|
|
|
prompt_TtyCommandMode(prompt);
|
|
|
|
prompt_Required(prompt);
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(pid_filename, sizeof pid_filename, "%stun%d.pid",
|
1998-02-07 20:50:08 +00:00
|
|
|
_PATH_VARRUN, bundle->unit);
|
1998-01-29 00:44:16 +00:00
|
|
|
lockfile = ID0fopen(pid_filename, "w");
|
|
|
|
if (lockfile != NULL) {
|
|
|
|
fprintf(lockfile, "%d\n", (int) getpid());
|
|
|
|
fclose(lockfile);
|
|
|
|
}
|
|
|
|
#ifndef RELEASE_CRUNCH
|
|
|
|
else
|
|
|
|
LogPrintf(LogALERT, "Warning: Can't create %s: %s\n",
|
|
|
|
pid_filename, strerror(errno));
|
|
|
|
#endif
|
|
|
|
|
1998-04-10 13:19:23 +00:00
|
|
|
LogPrintf(LogPHASE, "PPP Started (%s mode).\n", mode2Nam(mode));
|
|
|
|
DoLoop(bundle, prompt);
|
1998-02-08 11:07:32 +00:00
|
|
|
AbortProgram(EX_NORMAL);
|
|
|
|
|
|
|
|
return EX_NORMAL;
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1998-04-03 19:26:02 +00:00
|
|
|
DoLoop(struct bundle *bundle, struct prompt *prompt)
|
1998-01-29 00:44:16 +00:00
|
|
|
{
|
|
|
|
fd_set rfds, wfds, efds;
|
1998-02-16 00:01:12 +00:00
|
|
|
int pri, i, n, nfds;
|
1998-01-29 00:44:16 +00:00
|
|
|
int qlen;
|
|
|
|
struct tun_data tun;
|
|
|
|
|
1998-04-10 13:19:23 +00:00
|
|
|
do {
|
1998-01-29 00:44:16 +00:00
|
|
|
nfds = 0;
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_ZERO(&wfds);
|
|
|
|
FD_ZERO(&efds);
|
|
|
|
|
1998-02-16 00:01:12 +00:00
|
|
|
qlen = bundle_FillQueues(bundle);
|
1998-01-30 19:46:07 +00:00
|
|
|
|
1998-02-10 22:28:51 +00:00
|
|
|
handle_signals();
|
1998-02-10 03:23:50 +00:00
|
|
|
|
1998-04-30 23:53:56 +00:00
|
|
|
/* This one comes first 'cos it may nuke a datalink */
|
1998-04-28 01:25:46 +00:00
|
|
|
descriptor_UpdateSet(&bundle->ncp.mp.server.desc, &rfds, &wfds,
|
|
|
|
&efds, &nfds);
|
1998-04-30 23:53:56 +00:00
|
|
|
descriptor_UpdateSet(&bundle->desc, &rfds, &wfds, &efds, &nfds);
|
|
|
|
descriptor_UpdateSet(&server.desc, &rfds, &wfds, &efds, &nfds);
|
1998-01-29 00:44:16 +00:00
|
|
|
|
|
|
|
/* If there are aren't many packets queued, look for some more. */
|
1998-02-02 19:32:16 +00:00
|
|
|
if (qlen < 20 && bundle->tun_fd >= 0) {
|
|
|
|
if (bundle->tun_fd + 1 > nfds)
|
|
|
|
nfds = bundle->tun_fd + 1;
|
|
|
|
FD_SET(bundle->tun_fd, &rfds);
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
1998-02-09 19:24:03 +00:00
|
|
|
|
1998-04-03 19:21:56 +00:00
|
|
|
if (bundle_IsDead(bundle))
|
1998-02-23 00:38:44 +00:00
|
|
|
/* Don't select - we'll be here forever */
|
|
|
|
break;
|
|
|
|
|
1998-02-16 00:01:12 +00:00
|
|
|
i = select(nfds, &rfds, &wfds, &efds, NULL);
|
1998-01-29 00:44:16 +00:00
|
|
|
|
1998-02-16 00:01:12 +00:00
|
|
|
if (i == 0)
|
1998-01-29 00:44:16 +00:00
|
|
|
continue;
|
1998-02-09 19:24:03 +00:00
|
|
|
|
1998-01-29 00:44:16 +00:00
|
|
|
if (i < 0) {
|
|
|
|
if (errno == EINTR) {
|
|
|
|
handle_signals();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
LogPrintf(LogERROR, "DoLoop: select(): %s\n", strerror(errno));
|
|
|
|
break;
|
|
|
|
}
|
1998-02-09 19:24:03 +00:00
|
|
|
|
1998-02-16 00:01:12 +00:00
|
|
|
for (i = 0; i <= nfds; i++)
|
|
|
|
if (FD_ISSET(i, &efds)) {
|
|
|
|
LogPrintf(LogALERT, "Exception detected on descriptor %d\n", i);
|
|
|
|
break;
|
|
|
|
}
|
1998-02-09 19:24:03 +00:00
|
|
|
|
1998-04-28 01:25:46 +00:00
|
|
|
if (descriptor_IsSet(&bundle->ncp.mp.server.desc, &rfds))
|
|
|
|
descriptor_Read(&bundle->ncp.mp.server.desc, bundle, &rfds);
|
|
|
|
|
1998-02-09 19:24:03 +00:00
|
|
|
if (descriptor_IsSet(&server.desc, &rfds))
|
1998-02-10 03:22:05 +00:00
|
|
|
descriptor_Read(&server.desc, bundle, &rfds);
|
1998-02-09 19:21:11 +00:00
|
|
|
|
1998-03-20 19:48:28 +00:00
|
|
|
if (descriptor_IsSet(&bundle->desc, &wfds))
|
|
|
|
descriptor_Write(&bundle->desc, bundle, &wfds);
|
|
|
|
|
|
|
|
if (descriptor_IsSet(&bundle->desc, &rfds))
|
|
|
|
descriptor_Read(&bundle->desc, bundle, &rfds);
|
1998-02-09 19:21:11 +00:00
|
|
|
|
1998-02-02 19:32:16 +00:00
|
|
|
if (bundle->tun_fd >= 0 && FD_ISSET(bundle->tun_fd, &rfds)) {
|
|
|
|
/* something to read from tun */
|
|
|
|
n = read(bundle->tun_fd, &tun, sizeof tun);
|
1998-01-29 00:44:16 +00:00
|
|
|
if (n < 0) {
|
|
|
|
LogPrintf(LogERROR, "read from tun: %s\n", strerror(errno));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
n -= sizeof tun - sizeof tun.data;
|
|
|
|
if (n <= 0) {
|
|
|
|
LogPrintf(LogERROR, "read from tun: Only %d bytes read\n", n);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!tun_check_header(tun, AF_INET))
|
|
|
|
continue;
|
1998-03-13 21:07:46 +00:00
|
|
|
if (((struct ip *)tun.data)->ip_dst.s_addr ==
|
|
|
|
bundle->ncp.ipcp.my_ip.s_addr) {
|
1998-01-29 00:44:16 +00:00
|
|
|
/* we've been asked to send something addressed *to* us :( */
|
1998-04-16 00:26:21 +00:00
|
|
|
if (Enabled(bundle, OPT_LOOPBACK)) {
|
1998-03-16 22:52:54 +00:00
|
|
|
pri = PacketCheck(bundle, tun.data, n, &bundle->filter.in);
|
1998-01-29 00:44:16 +00:00
|
|
|
if (pri >= 0) {
|
|
|
|
struct mbuf *bp;
|
|
|
|
|
|
|
|
#ifndef NOALIAS
|
1998-04-07 23:46:09 +00:00
|
|
|
if (AliasEnabled()) {
|
1998-04-07 00:54:26 +00:00
|
|
|
(*PacketAlias.In)(tun.data, sizeof tun.data);
|
1998-02-23 00:38:44 +00:00
|
|
|
n = ntohs(((struct ip *)tun.data)->ip_len);
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
bp = mballoc(n, MB_IPIN);
|
1998-02-23 00:38:44 +00:00
|
|
|
memcpy(MBUF_CTOP(bp), tun.data, n);
|
1998-02-02 19:32:16 +00:00
|
|
|
IpInput(bundle, bp);
|
1998-01-29 00:44:16 +00:00
|
|
|
LogPrintf(LogDEBUG, "Looped back packet addressed to myself\n");
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
} else
|
|
|
|
LogPrintf(LogDEBUG, "Oops - forwarding packet addressed to myself\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process on-demand dialup. Output packets are queued within tunnel
|
|
|
|
* device until IPCP is opened.
|
|
|
|
*/
|
1998-04-25 10:49:52 +00:00
|
|
|
if (bundle_Phase(bundle) == PHASE_DEAD) {
|
1998-04-03 19:25:07 +00:00
|
|
|
/*
|
|
|
|
* Note, we must be in AUTO mode :-/ otherwise our interface should
|
|
|
|
* *not* be UP and we can't receive data
|
|
|
|
*/
|
1998-04-10 13:19:23 +00:00
|
|
|
if ((pri = PacketCheck(bundle, tun.data, n, &bundle->filter.dial)) >= 0)
|
|
|
|
bundle_Open(bundle, NULL, PHYS_DEMAND);
|
1998-04-03 19:25:07 +00:00
|
|
|
else
|
|
|
|
/*
|
|
|
|
* Drop the packet. If we were to queue it, we'd just end up with
|
|
|
|
* a pile of timed-out data in our output queue by the time we get
|
|
|
|
* around to actually dialing. We'd also prematurely reach the
|
|
|
|
* threshold at which we stop select()ing to read() the tun
|
|
|
|
* device - breaking auto-dial.
|
|
|
|
*/
|
|
|
|
continue;
|
1998-04-25 10:49:52 +00:00
|
|
|
}
|
1998-01-29 00:44:16 +00:00
|
|
|
|
1998-03-16 22:52:54 +00:00
|
|
|
pri = PacketCheck(bundle, tun.data, n, &bundle->filter.out);
|
1998-01-29 00:44:16 +00:00
|
|
|
if (pri >= 0) {
|
|
|
|
#ifndef NOALIAS
|
1998-04-07 23:46:09 +00:00
|
|
|
if (AliasEnabled()) {
|
1998-04-07 00:54:26 +00:00
|
|
|
(*PacketAlias.Out)(tun.data, sizeof tun.data);
|
1998-02-23 00:38:44 +00:00
|
|
|
n = ntohs(((struct ip *)tun.data)->ip_len);
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
|
|
|
#endif
|
1998-02-23 00:38:44 +00:00
|
|
|
IpEnqueue(pri, tun.data, n);
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
|
|
|
}
|
1998-04-10 13:19:23 +00:00
|
|
|
} while (bundle_CleanDatalinks(bundle), !bundle_IsDead(bundle));
|
|
|
|
|
|
|
|
LogPrintf(LogDEBUG, "DoLoop done.\n");
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|