2 * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org>
3 * based on work by Toshiharu OHNO <tony-o@iij.ad.jp>
4 * Internet Initiative Japan, Inc (IIJ)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/param.h>
32 #include <netinet/in.h>
33 #include <netinet/in_systm.h>
34 #include <netinet/ip.h>
36 #include <sys/socket.h>
37 #include <net/route.h>
72 #include "throughput.h"
73 #include "slcompress.h"
78 #include "descriptor.h"
101 #define O_NONBLOCK O_NDELAY
105 static void DoLoop(struct bundle *);
106 static void TerminalStop(int);
108 static struct bundle *SignalBundle;
109 static struct prompt *SignalPrompt;
114 SignalBundle->CleaningUp = 1;
115 bundle_Close(SignalBundle, NULL, CLOSE_STAYDOWN);
119 AbortProgram(int excode)
121 server_Close(SignalBundle);
122 log_Printf(LogPHASE, "PPP Terminated (%s).\n", ex_desc(excode));
123 bundle_Close(SignalBundle, NULL, CLOSE_STAYDOWN);
124 bundle_Destroy(SignalBundle);
130 CloseConnection(int signo)
132 /* NOTE, these are manual, we've done a setsid() */
133 sig_signal(SIGINT, SIG_IGN);
134 log_Printf(LogPHASE, "Caught signal %d, abort connection(s)\n", signo);
135 bundle_Down(SignalBundle, CLOSE_STAYDOWN);
136 sig_signal(SIGINT, CloseConnection);
140 CloseSession(int signo)
142 log_Printf(LogPHASE, "Signal %d, terminate.\n", signo);
146 static pid_t BGPid = 0;
151 signal(signo, SIG_IGN);
152 log_Printf(LogPHASE, "Parent: Signal %d\n", signo);
157 TerminalCont(int signo)
159 signal(SIGCONT, SIG_DFL);
160 prompt_Continue(SignalPrompt);
164 TerminalStop(int signo)
166 prompt_Suspend(SignalPrompt);
167 signal(SIGCONT, TerminalCont);
172 BringDownServer(int signo)
174 /* Drops all child prompts too ! */
175 if (server_Close(SignalBundle))
176 log_Printf(LogPHASE, "Closed server socket\n");
180 RestartServer(int signo)
182 /* Drops all child prompts and re-opens the socket */
183 server_Reopen(SignalBundle);
189 fprintf(stderr, "usage: ppp [-auto | -foreground | -background | -direct |"
190 " -dedicated | -ddial | -interactive]"
194 " [-quiet] [-unit N] [system ...]\n");
207 ProcessArgs(int argc, char **argv, struct switches *sw)
209 int optc, newmode, arg;
213 memset(sw, '\0', sizeof *sw);
214 sw->mode = PHYS_INTERACTIVE;
217 for (arg = 1; arg < argc && *argv[arg] == '-'; arg++, optc++) {
219 newmode = Nam2mode(cp);
222 if (strcmp(cp, "nat") == 0) {
224 log_Printf(LogWARN, "%s ignored: NAT is compiled out\n", argv[arg]);
228 optc--; /* this option isn't exclusive */
229 } else if (strcmp(cp, "alias") == 0) {
231 log_Printf(LogWARN, "%s ignored: NAT is compiled out\n", argv[arg]);
232 fprintf(stderr, "%s ignored: NAT is compiled out\n", argv[arg]);
234 log_Printf(LogWARN, "%s is deprecated\n", argv[arg]);
235 fprintf(stderr, "%s is deprecated\n", argv[arg]);
238 optc--; /* this option isn't exclusive */
239 } else if (strncmp(cp, "unit", 4) == 0) {
240 optc--; /* this option isn't exclusive */
242 optc--; /* nor is the argument */
244 fprintf(stderr, "-unit: Expected unit number\n");
247 sw->unit = atoi(argv[arg]);
249 sw->unit = atoi(cp + 4);
250 } else if (strcmp(cp, "quiet") == 0) {
252 optc--; /* this option isn't exclusive */
263 if (newmode == PHYS_FOREGROUND)
269 fprintf(stderr, "You may specify only one mode.\n");
273 if (sw->mode == PHYS_AUTO && arg == argc) {
274 fprintf(stderr, "A system must be specified in auto mode.\n");
278 return arg; /* Don't SetLabel yet ! */
282 CheckLabel(const char *label, struct prompt *prompt, int mode)
286 if ((err = system_IsValid(label, prompt, mode)) != NULL) {
287 fprintf(stderr, "%s: %s\n", label, err);
288 if (mode == PHYS_DIRECT)
289 log_Printf(LogWARN, "Label %s rejected -direct connection: %s\n",
298 main(int argc, char **argv)
301 const char *lastlabel;
302 int arg, f, holdfd[3], label;
303 struct bundle *bundle;
304 struct prompt *prompt;
310 * We open 3 descriptors to ensure that STDIN_FILENO, STDOUT_FILENO and
311 * STDERR_FILENO are always open. These are closed before DoLoop(),
312 * but *after* we've avoided the possibility of erroneously closing
313 * an important descriptor with close(STD{IN,OUT,ERR}_FILENO).
315 if ((holdfd[0] = open(_PATH_DEVNULL, O_RDWR)) == -1) {
316 fprintf(stderr, "Cannot open %s !\n", _PATH_DEVNULL);
319 for (f = 1; f < sizeof holdfd / sizeof *holdfd; f++)
320 holdfd[f] = dup(holdfd[0]);
322 name = strrchr(argv[0], '/');
323 log_Open(name ? name + 1 : argv[0]);
328 label = ProcessArgs(argc, argv, &sw);
331 * A FreeBSD & OpenBSD hack to dodge a bug in the tty driver that drops
332 * output occasionally.... I must find the real reason some time. To
333 * display the dodgy behaviour, comment out this bit, make yourself a large
334 * routing table and then run ppp in interactive mode. The `show route'
335 * command will drop chunks of data !!!
337 if (sw.mode == PHYS_INTERACTIVE) {
339 if (open(_PATH_TTY, O_RDONLY) != STDIN_FILENO) {
340 fprintf(stderr, "Cannot open %s for input !\n", _PATH_TTY);
345 /* Allow output for the moment (except in direct mode) */
346 if (sw.mode == PHYS_DIRECT)
349 SignalPrompt = prompt = prompt_Create(NULL, NULL, PROMPT_STD);
352 if (ID0realuid() != 0) {
353 char conf[200], *ptr;
355 snprintf(conf, sizeof conf, "%s/%s", PPP_CONFDIR, CONFFILE);
359 if (stat(conf, &sb) == 0 && sb.st_mode & S_IWOTH) {
360 log_Printf(LogALERT, "ppp: Access violation: Please protect %s\n",
364 ptr = conf + strlen(conf)-2;
365 while (ptr > conf && *ptr != '/')
367 } while (ptr >= conf);
371 for (arg = label; arg < argc; arg++)
372 CheckLabel(argv[arg], prompt, sw.mode);
374 CheckLabel("default", prompt, sw.mode);
377 prompt_Printf(prompt, "Working in %s mode\n", mode2Nam(sw.mode));
379 if ((bundle = bundle_Create(TUN_PREFIX, sw.mode, sw.unit)) == NULL)
382 /* NOTE: We may now have changed argv[1] via a ``set proctitle'' */
385 prompt->bundle = bundle; /* couldn't do it earlier */
387 prompt_Printf(prompt, "Using interface: %s\n", bundle->iface->name);
389 SignalBundle = bundle;
390 bundle->NatEnabled = sw.nat;
392 bundle->cfg.opt |= OPT_IFACEALIAS;
394 if (system_Select(bundle, "default", CONFFILE, prompt, NULL) < 0)
395 prompt_Printf(prompt, "Warning: No default entry found in config file.\n");
397 sig_signal(SIGHUP, CloseSession);
398 sig_signal(SIGTERM, CloseSession);
399 sig_signal(SIGINT, CloseConnection);
400 sig_signal(SIGQUIT, CloseSession);
401 sig_signal(SIGALRM, SIG_IGN);
402 signal(SIGPIPE, SIG_IGN);
404 if (sw.mode == PHYS_INTERACTIVE)
405 sig_signal(SIGTSTP, TerminalStop);
407 sig_signal(SIGUSR1, RestartServer);
408 sig_signal(SIGUSR2, BringDownServer);
410 lastlabel = argv[argc - 1];
411 for (arg = label; arg < argc; arg++) {
412 /* In case we use LABEL or ``set enddisc label'' */
413 bundle_SetLabel(bundle, lastlabel);
414 system_Select(bundle, argv[arg], CONFFILE, prompt, NULL);
418 /* In case the last label did a ``load'' */
419 bundle_SetLabel(bundle, lastlabel);
421 if (sw.mode == PHYS_AUTO &&
422 ncprange_family(&bundle->ncp.ipcp.cfg.peer_range) == AF_UNSPEC) {
423 prompt_Printf(prompt, "You must ``set ifaddr'' with a peer address "
425 AbortProgram(EX_START);
428 if (sw.mode != PHYS_INTERACTIVE) {
429 if (sw.mode != PHYS_DIRECT) {
434 if (sw.mode == PHYS_BACKGROUND && pipe(bgpipe)) {
435 log_Printf(LogERROR, "pipe: %s\n", strerror(errno));
436 AbortProgram(EX_SOCK);
441 log_Printf(LogERROR, "fork: %s\n", strerror(errno));
442 AbortProgram(EX_SOCK);
449 if (sw.mode == PHYS_BACKGROUND) {
452 /* If we get a signal, kill the child */
453 signal(SIGHUP, KillChild);
454 signal(SIGTERM, KillChild);
455 signal(SIGINT, KillChild);
456 signal(SIGQUIT, KillChild);
458 /* Wait for our child to close its pipe before we exit */
459 while ((ret = read(bgpipe[0], &c, 1)) == 1) {
463 prompt_Printf(prompt, "PPP enabled\n");
464 log_Printf(LogPHASE, "Parent: PPP enabled\n");
469 prompt_Printf(prompt, "Attempting redial\n");
473 prompt_Printf(prompt, "Attempting reconnect\n");
476 prompt_Printf(prompt, "Child failed (%s)\n",
478 log_Printf(LogPHASE, "Parent: Child failed (%s)\n",
484 prompt_Printf(prompt, "Child exit, no status.\n");
485 log_Printf(LogPHASE, "Parent: Child exit, no status.\n");
490 } else if (sw.mode == PHYS_BACKGROUND) {
492 bundle->notify.fd = bgpipe[1];
495 bundle_ChangedPID(bundle);
496 bundle_LockTun(bundle); /* we have a new pid */
499 /* -auto, -dedicated, -ddial, -foreground & -background */
500 prompt_Destroy(prompt, 0);
501 close(STDOUT_FILENO);
502 close(STDERR_FILENO);
507 /* -direct - STDIN_FILENO gets used by physical_Open */
508 prompt_TtyInit(NULL);
509 close(STDOUT_FILENO);
510 close(STDERR_FILENO);
514 close(STDERR_FILENO);
515 prompt_TtyInit(prompt);
516 prompt_TtyCommandMode(prompt);
517 prompt_Required(prompt);
520 /* We can get rid of these now */
521 for (f = 0; f < sizeof holdfd / sizeof *holdfd; f++)
524 log_Printf(LogPHASE, "PPP Started (%s mode).\n", mode2Nam(sw.mode));
526 AbortProgram(EX_NORMAL);
532 DoLoop(struct bundle *bundle)
534 fd_set *rfds, *wfds, *efds;
535 int i, nfds, nothing_done;
537 if ((rfds = mkfdset()) == NULL) {
538 log_Printf(LogERROR, "DoLoop: Cannot create fd_set\n");
542 if ((wfds = mkfdset()) == NULL) {
543 log_Printf(LogERROR, "DoLoop: Cannot create fd_set\n");
548 if ((efds = mkfdset()) == NULL) {
549 log_Printf(LogERROR, "DoLoop: Cannot create fd_set\n");
555 for (; !bundle_IsDead(bundle); bundle_CleanDatalinks(bundle)) {
561 /* All our datalinks, the tun device and the MP socket */
562 descriptor_UpdateSet(&bundle->desc, rfds, wfds, efds, &nfds);
564 /* All our prompts and the diagnostic socket */
565 descriptor_UpdateSet(&server.desc, rfds, NULL, NULL, &nfds);
567 bundle_CleanDatalinks(bundle);
568 if (bundle_IsDead(bundle))
569 /* Don't select - we'll be here forever */
573 * It's possible that we've had a signal since we last checked. If
574 * we don't check again before calling select(), we may end up stuck
575 * after having missed the event.... sig_Handle() tries to be as
576 * quick as possible if nothing is likely to have happened.
577 * This is only really likely if we block in open(... O_NONBLOCK)
578 * which will happen with a misconfigured device.
583 i = select(nfds, rfds, wfds, efds, NULL);
585 if (i < 0 && errno != EINTR) {
586 log_Printf(LogERROR, "DoLoop: select(): %s\n", strerror(errno));
587 if (log_IsKept(LogTIMER)) {
590 for (i = 0; i <= nfds; i++) {
591 if (FD_ISSET(i, rfds)) {
592 log_Printf(LogTIMER, "Read set contains %d\n", i);
594 t.tv_sec = t.tv_usec = 0;
595 if (select(nfds, rfds, wfds, efds, &t) != -1) {
596 log_Printf(LogTIMER, "The culprit !\n");
600 if (FD_ISSET(i, wfds)) {
601 log_Printf(LogTIMER, "Write set contains %d\n", i);
603 t.tv_sec = t.tv_usec = 0;
604 if (select(nfds, rfds, wfds, efds, &t) != -1) {
605 log_Printf(LogTIMER, "The culprit !\n");
609 if (FD_ISSET(i, efds)) {
610 log_Printf(LogTIMER, "Error set contains %d\n", i);
612 t.tv_sec = t.tv_usec = 0;
613 if (select(nfds, rfds, wfds, efds, &t) != -1) {
614 log_Printf(LogTIMER, "The culprit !\n");
623 log_Printf(LogTIMER, "Select returns %d\n", i);
630 for (i = 0; i <= nfds; i++)
631 if (FD_ISSET(i, efds)) {
632 log_Printf(LogPHASE, "Exception detected on descriptor %d\n", i);
633 /* We deal gracefully with link descriptor exceptions */
634 if (!bundle_Exception(bundle, i)) {
635 log_Printf(LogERROR, "Exception cannot be handled !\n");
645 if (descriptor_IsSet(&server.desc, rfds)) {
646 descriptor_Read(&server.desc, bundle, rfds);
650 if (descriptor_IsSet(&bundle->desc, rfds)) {
651 descriptor_Read(&bundle->desc, bundle, rfds);
655 if (descriptor_IsSet(&bundle->desc, wfds))
656 if (descriptor_Write(&bundle->desc, bundle, wfds) <= 0 && nothing_done) {
658 * This is disastrous. The OS has told us that something is
659 * writable, and all our write()s have failed. Rather than
660 * going back immediately to do our UpdateSet()s and select(),
661 * we sleep for a bit to avoid gobbling up all cpu time.
667 select(0, NULL, NULL, NULL, &t);
671 log_Printf(LogDEBUG, "DoLoop done.\n");