2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org>
5 * based on work by Toshiharu OHNO <tony-o@iij.ad.jp>
6 * Internet Initiative Japan, Inc (IIJ)
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/param.h>
34 #include <netinet/in.h>
35 #include <netinet/in_systm.h>
36 #include <netinet/ip.h>
38 #include <sys/socket.h>
39 #include <net/route.h>
75 #include "throughput.h"
76 #include "slcompress.h"
80 #include "descriptor.h"
103 #define O_NONBLOCK O_NDELAY
107 static void DoLoop(struct bundle *);
108 static void TerminalStop(int);
110 static struct bundle *SignalBundle;
111 static struct prompt *SignalPrompt;
117 SignalBundle->CleaningUp = 1;
118 bundle_Close(SignalBundle, NULL, CLOSE_STAYDOWN);
122 AbortProgram(int excode)
125 server_Close(SignalBundle);
126 log_Printf(LogPHASE, "PPP Terminated (%s).\n", ex_desc(excode));
128 bundle_Close(SignalBundle, NULL, CLOSE_STAYDOWN);
129 bundle_Destroy(SignalBundle);
136 CloseConnection(int signo)
138 /* NOTE, these are manual, we've done a setsid() */
139 sig_signal(SIGINT, SIG_IGN);
140 log_Printf(LogPHASE, "Caught signal %d, abort connection(s)\n", signo);
141 bundle_Down(SignalBundle, CLOSE_STAYDOWN);
142 sig_signal(SIGINT, CloseConnection);
146 CloseSession(int signo)
148 log_Printf(LogPHASE, "Signal %d, terminate.\n", signo);
152 static pid_t BGPid = 0;
157 signal(signo, SIG_IGN);
158 log_Printf(LogPHASE, "Parent: Signal %d\n", signo);
163 TerminalCont(int signo __unused)
165 signal(SIGCONT, SIG_DFL);
166 prompt_Continue(SignalPrompt);
170 TerminalStop(int signo __unused)
172 prompt_Suspend(SignalPrompt);
173 signal(SIGCONT, TerminalCont);
178 BringDownServer(int signo __unused)
180 /* Drops all child prompts too ! */
181 if (server_Close(SignalBundle))
182 log_Printf(LogPHASE, "Closed server socket\n");
186 RestartServer(int signo __unused)
188 /* Drops all child prompts and re-opens the socket */
189 server_Reopen(SignalBundle);
195 fprintf(stderr, "usage: ppp [-auto | -foreground | -background | -direct |"
196 " -dedicated | -ddial | -interactive]"
200 " [-quiet] [-unit N] [system ...]\n");
213 ProcessArgs(int argc, char **argv, struct switches *sw)
215 int optc, newmode, arg;
219 memset(sw, '\0', sizeof *sw);
220 sw->mode = PHYS_INTERACTIVE;
223 for (arg = 1; arg < argc && *argv[arg] == '-'; arg++, optc++) {
225 newmode = Nam2mode(cp);
228 if (strcmp(cp, "nat") == 0) {
230 log_Printf(LogWARN, "%s ignored: NAT is compiled out\n", argv[arg]);
234 optc--; /* this option isn't exclusive */
235 } else if (strcmp(cp, "alias") == 0) {
237 log_Printf(LogWARN, "%s ignored: NAT is compiled out\n", argv[arg]);
238 fprintf(stderr, "%s ignored: NAT is compiled out\n", argv[arg]);
240 log_Printf(LogWARN, "%s is deprecated\n", argv[arg]);
241 fprintf(stderr, "%s is deprecated\n", argv[arg]);
244 optc--; /* this option isn't exclusive */
245 } else if (strncmp(cp, "unit", 4) == 0) {
246 optc--; /* this option isn't exclusive */
248 optc--; /* nor is the argument */
250 fprintf(stderr, "-unit: Expected unit number\n");
253 sw->unit = atoi(argv[arg]);
255 sw->unit = atoi(cp + 4);
256 } else if (strcmp(cp, "quiet") == 0) {
258 optc--; /* this option isn't exclusive */
269 if (newmode == PHYS_FOREGROUND)
275 fprintf(stderr, "You may specify only one mode.\n");
279 if (sw->mode == PHYS_AUTO && arg == argc) {
280 fprintf(stderr, "A system must be specified in auto mode.\n");
284 return arg; /* Don't SetLabel yet ! */
288 CheckLabel(const char *label, struct prompt *prompt, int mode)
292 if ((err = system_IsValid(label, prompt, mode)) != NULL) {
293 fprintf(stderr, "%s: %s\n", label, err);
294 if (mode == PHYS_DIRECT)
295 log_Printf(LogWARN, "Label %s rejected -direct connection: %s\n",
304 main(int argc, char **argv)
307 const char *lastlabel;
308 int arg, holdfd[3], label;
310 struct bundle *bundle;
311 struct prompt *prompt;
317 * We open 3 descriptors to ensure that STDIN_FILENO, STDOUT_FILENO and
318 * STDERR_FILENO are always open. These are closed before DoLoop(),
319 * but *after* we've avoided the possibility of erroneously closing
320 * an important descriptor with close(STD{IN,OUT,ERR}_FILENO).
322 if ((holdfd[0] = open(_PATH_DEVNULL, O_RDWR)) == -1) {
323 fprintf(stderr, "Cannot open %s !\n", _PATH_DEVNULL);
326 for (f = 1; f < sizeof holdfd / sizeof *holdfd; f++)
327 holdfd[f] = dup(holdfd[0]);
329 name = strrchr(argv[0], '/');
330 log_Open(name ? name + 1 : argv[0]);
333 la = LibAliasInit(NULL);
335 label = ProcessArgs(argc, argv, &sw);
338 * A FreeBSD & OpenBSD hack to dodge a bug in the tty driver that drops
339 * output occasionally.... I must find the real reason some time. To
340 * display the dodgy behaviour, comment out this bit, make yourself a large
341 * routing table and then run ppp in interactive mode. The `show route'
342 * command will drop chunks of data !!!
344 if (sw.mode == PHYS_INTERACTIVE) {
346 if (open(_PATH_TTY, O_RDONLY) != STDIN_FILENO) {
347 fprintf(stderr, "Cannot open %s for input !\n", _PATH_TTY);
352 /* Allow output for the moment (except in direct mode) */
353 if (sw.mode == PHYS_DIRECT)
356 SignalPrompt = prompt = prompt_Create(NULL, NULL, PROMPT_STD);
359 if (ID0realuid() != 0) {
360 char conf[200], *ptr;
362 snprintf(conf, sizeof conf, "%s/%s", PPP_CONFDIR, CONFFILE);
366 if (stat(conf, &sb) == 0 && sb.st_mode & S_IWOTH) {
367 log_Printf(LogALERT, "ppp: Access violation: Please protect %s\n",
371 ptr = conf + strlen(conf)-2;
372 while (ptr > conf && *ptr != '/')
374 } while (ptr >= conf);
378 for (arg = label; arg < argc; arg++)
379 CheckLabel(argv[arg], prompt, sw.mode);
381 CheckLabel("default", prompt, sw.mode);
384 prompt_Printf(prompt, "Working in %s mode\n", mode2Nam(sw.mode));
386 if ((bundle = bundle_Create(TUN_PREFIX, sw.mode, sw.unit)) == NULL)
389 /* NOTE: We may now have changed argv[1] via a ``set proctitle'' */
391 SignalBundle = bundle;
392 bundle->NatEnabled = sw.nat;
394 opt_enable(bundle, OPT_IFACEALIAS);
396 if (system_Select(bundle, "default", CONFFILE, prompt, NULL) < 0)
397 prompt_Printf(prompt, "Warning: No default entry found in config file.\n");
399 sig_signal(SIGHUP, CloseSession);
400 sig_signal(SIGTERM, CloseSession);
401 sig_signal(SIGINT, CloseConnection);
402 sig_signal(SIGQUIT, CloseSession);
403 sig_signal(SIGALRM, SIG_IGN);
404 signal(SIGPIPE, SIG_IGN);
406 if (sw.mode == PHYS_INTERACTIVE)
407 sig_signal(SIGTSTP, TerminalStop);
409 sig_signal(SIGUSR1, RestartServer);
410 sig_signal(SIGUSR2, BringDownServer);
412 lastlabel = argv[argc - 1];
413 for (arg = label; arg < argc; arg++) {
414 /* In case we use LABEL or ``set enddisc label'' */
415 bundle_SetLabel(bundle, lastlabel);
416 system_Select(bundle, argv[arg], CONFFILE, prompt, NULL);
420 /* In case the last label did a ``load'' */
421 bundle_SetLabel(bundle, lastlabel);
423 if (sw.mode == PHYS_AUTO &&
424 ncprange_family(&bundle->ncp.ipcp.cfg.peer_range) == AF_UNSPEC) {
425 prompt_Printf(prompt, "You must ``set ifaddr'' with a peer address "
427 AbortProgram(EX_START);
431 prompt->bundle = bundle; /* couldn't do it earlier */
433 prompt_Printf(prompt, "Using interface: %s\n", bundle->iface->name);
436 if (sw.mode != PHYS_INTERACTIVE) {
437 if (sw.mode != PHYS_DIRECT) {
442 if (sw.mode == PHYS_BACKGROUND && pipe(bgpipe)) {
443 log_Printf(LogERROR, "pipe: %s\n", strerror(errno));
444 AbortProgram(EX_SOCK);
449 log_Printf(LogERROR, "fork: %s\n", strerror(errno));
450 AbortProgram(EX_SOCK);
457 if (sw.mode == PHYS_BACKGROUND) {
460 /* If we get a signal, kill the child */
461 signal(SIGHUP, KillChild);
462 signal(SIGTERM, KillChild);
463 signal(SIGINT, KillChild);
464 signal(SIGQUIT, KillChild);
466 /* Wait for our child to close its pipe before we exit */
467 while ((ret = read(bgpipe[0], &c, 1)) == 1) {
471 prompt_Printf(prompt, "PPP enabled\n");
472 log_Printf(LogPHASE, "Parent: PPP enabled\n");
477 prompt_Printf(prompt, "Attempting redial\n");
481 prompt_Printf(prompt, "Attempting reconnect\n");
484 prompt_Printf(prompt, "Child failed (%s)\n",
486 log_Printf(LogPHASE, "Parent: Child failed (%s)\n",
492 prompt_Printf(prompt, "Child exit, no status.\n");
493 log_Printf(LogPHASE, "Parent: Child exit, no status.\n");
498 } else if (sw.mode == PHYS_BACKGROUND) {
500 bundle->notify.fd = bgpipe[1];
503 bundle_ChangedPID(bundle);
504 bundle_LockTun(bundle); /* we have a new pid */
507 /* -auto, -dedicated, -ddial, -foreground & -background */
508 prompt_Destroy(prompt, 0);
509 close(STDOUT_FILENO);
510 close(STDERR_FILENO);
516 * -direct - STDIN_FILENO gets used by physical_Open. STDOUT_FILENO
517 * *may* get used in exec/pipe mode.
519 prompt_TtyInit(NULL);
520 close(STDERR_FILENO);
524 close(STDERR_FILENO);
525 prompt_TtyInit(prompt);
526 prompt_TtyCommandMode(prompt);
527 prompt_Required(prompt);
530 /* We can get rid of these now */
531 for (f = 0; f < sizeof holdfd / sizeof *holdfd; f++)
534 log_Printf(LogPHASE, "PPP Started (%s mode).\n", mode2Nam(sw.mode));
536 AbortProgram(EX_NORMAL);
542 DoLoop(struct bundle *bundle)
544 fd_set *rfds, *wfds, *efds;
545 int i, nfds, nothing_done;
547 if ((rfds = mkfdset()) == NULL) {
548 log_Printf(LogERROR, "DoLoop: Cannot create fd_set\n");
552 if ((wfds = mkfdset()) == NULL) {
553 log_Printf(LogERROR, "DoLoop: Cannot create fd_set\n");
558 if ((efds = mkfdset()) == NULL) {
559 log_Printf(LogERROR, "DoLoop: Cannot create fd_set\n");
565 for (; !bundle_IsDead(bundle); bundle_CleanDatalinks(bundle)) {
571 /* All our datalinks, the tun device and the MP socket */
572 descriptor_UpdateSet(&bundle->desc, rfds, wfds, efds, &nfds);
574 /* All our prompts and the diagnostic socket */
575 descriptor_UpdateSet(&server.desc, rfds, NULL, NULL, &nfds);
577 bundle_CleanDatalinks(bundle);
578 if (bundle_IsDead(bundle))
579 /* Don't select - we'll be here forever */
583 * It's possible that we've had a signal since we last checked. If
584 * we don't check again before calling select(), we may end up stuck
585 * after having missed the event.... sig_Handle() tries to be as
586 * quick as possible if nothing is likely to have happened.
587 * This is only really likely if we block in open(... O_NONBLOCK)
588 * which will happen with a misconfigured device.
593 i = select(nfds, rfds, wfds, efds, NULL);
595 if (i < 0 && errno != EINTR) {
596 log_Printf(LogERROR, "DoLoop: select(): %s\n", strerror(errno));
597 if (log_IsKept(LogTIMER)) {
600 for (i = 0; i <= nfds; i++) {
601 if (FD_ISSET(i, rfds)) {
602 log_Printf(LogTIMER, "Read set contains %d\n", i);
604 t.tv_sec = t.tv_usec = 0;
605 if (select(nfds, rfds, wfds, efds, &t) != -1) {
606 log_Printf(LogTIMER, "The culprit !\n");
610 if (FD_ISSET(i, wfds)) {
611 log_Printf(LogTIMER, "Write set contains %d\n", i);
613 t.tv_sec = t.tv_usec = 0;
614 if (select(nfds, rfds, wfds, efds, &t) != -1) {
615 log_Printf(LogTIMER, "The culprit !\n");
619 if (FD_ISSET(i, efds)) {
620 log_Printf(LogTIMER, "Error set contains %d\n", i);
622 t.tv_sec = t.tv_usec = 0;
623 if (select(nfds, rfds, wfds, efds, &t) != -1) {
624 log_Printf(LogTIMER, "The culprit !\n");
633 log_Printf(LogTIMER, "Select returns %d\n", i);
640 for (i = 0; i <= nfds; i++)
641 if (FD_ISSET(i, efds)) {
642 log_Printf(LogPHASE, "Exception detected on descriptor %d\n", i);
643 /* We deal gracefully with link descriptor exceptions */
644 if (!bundle_Exception(bundle, i)) {
645 log_Printf(LogERROR, "Exception cannot be handled !\n");
655 if (descriptor_IsSet(&server.desc, rfds)) {
656 descriptor_Read(&server.desc, bundle, rfds);
660 if (descriptor_IsSet(&bundle->desc, rfds)) {
661 descriptor_Read(&bundle->desc, bundle, rfds);
665 if (descriptor_IsSet(&bundle->desc, wfds))
666 if (descriptor_Write(&bundle->desc, bundle, wfds) <= 0 && nothing_done) {
668 * This is disastrous. The OS has told us that something is
669 * writable, and all our write()s have failed. Rather than
670 * going back immediately to do our UpdateSet()s and select(),
671 * we sleep for a bit to avoid gobbling up all cpu time.
677 select(0, NULL, NULL, NULL, &t);
681 log_Printf(LogDEBUG, "DoLoop done.\n");