4 * Written by Toshiharu OHNO (tony-o@iij.ad.jp)
6 * Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by the Internet Initiative Japan, Inc. The name of the
14 * IIJ may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
25 #include <sys/param.h>
26 #include <netinet/in.h>
27 #include <netinet/in_systm.h>
28 #include <netinet/ip.h>
63 #include "throughput.h"
64 #include "slcompress.h"
67 #include "descriptor.h"
88 #define O_NONBLOCK O_NDELAY
92 static void DoLoop(struct bundle *);
93 static void TerminalStop(int);
94 static const char *ex_desc(int);
96 static struct bundle *SignalBundle;
97 static struct prompt *SignalPrompt;
102 SignalBundle->CleaningUp = 1;
103 bundle_Close(SignalBundle, NULL, CLOSE_STAYDOWN);
107 AbortProgram(int excode)
109 server_Close(SignalBundle);
110 log_Printf(LogPHASE, "PPP Terminated (%s).\n", ex_desc(excode));
111 bundle_Close(SignalBundle, NULL, CLOSE_STAYDOWN);
112 bundle_Destroy(SignalBundle);
118 CloseConnection(int signo)
120 /* NOTE, these are manual, we've done a setsid() */
121 sig_signal(SIGINT, SIG_IGN);
122 log_Printf(LogPHASE, "Caught signal %d, abort connection(s)\n", signo);
123 bundle_Down(SignalBundle, CLOSE_STAYDOWN);
124 sig_signal(SIGINT, CloseConnection);
128 CloseSession(int signo)
130 log_Printf(LogPHASE, "Signal %d, terminate.\n", signo);
134 static pid_t BGPid = 0;
139 signal(signo, SIG_IGN);
140 log_Printf(LogPHASE, "Parent: Signal %d\n", signo);
145 TerminalCont(int signo)
147 signal(SIGCONT, SIG_DFL);
148 prompt_Continue(SignalPrompt);
152 TerminalStop(int signo)
154 prompt_Suspend(SignalPrompt);
155 signal(SIGCONT, TerminalCont);
160 BringDownServer(int signo)
162 /* Drops all child prompts too ! */
163 server_Close(SignalBundle);
169 static char num[12]; /* Used immediately if returned */
170 static const char * const desc[] = {
171 "normal", "start", "sock", "modem", "dial", "dead", "done",
172 "reboot", "errdead", "hangup", "term", "nodial", "nologin"
175 if (ex >= 0 && ex < sizeof desc / sizeof *desc)
177 snprintf(num, sizeof num, "%d", ex);
185 "Usage: ppp [-auto | -foreground | -background | -direct | -dedicated | -ddial | -interactive]"
189 " [-quiet] [-unit N] [system ...]\n");
202 ProcessArgs(int argc, char **argv, struct switches *sw)
204 int optc, newmode, arg;
208 memset(sw, '\0', sizeof *sw);
209 sw->mode = PHYS_INTERACTIVE;
212 for (arg = 1; arg < argc && *argv[arg] == '-'; arg++, optc++) {
214 newmode = Nam2mode(cp);
217 if (strcmp(cp, "nat") == 0) {
219 log_Printf(LogWARN, "%s ignored: NAT is compiled out\n", argv[arg]);
223 optc--; /* this option isn't exclusive */
224 } else if (strcmp(cp, "alias") == 0) {
226 log_Printf(LogWARN, "%s ignored: NAT is compiled out\n", argv[arg]);
227 fprintf(stderr, "%s ignored: NAT is compiled out\n", argv[arg]);
229 log_Printf(LogWARN, "%s is deprecated\n", argv[arg]);
230 fprintf(stderr, "%s is deprecated\n", argv[arg]);
233 optc--; /* this option isn't exclusive */
234 } else if (strncmp(cp, "unit", 4) == 0) {
235 optc--; /* this option isn't exclusive */
237 optc--; /* nor is the argument */
239 fprintf(stderr, "-unit: Expected unit number\n");
242 sw->unit = atoi(argv[arg]);
244 sw->unit = atoi(cp + 4);
245 } else if (strcmp(cp, "quiet") == 0) {
247 optc--; /* this option isn't exclusive */
258 if (newmode == PHYS_FOREGROUND)
264 fprintf(stderr, "You may specify only one mode.\n");
268 if (sw->mode == PHYS_AUTO && arg == argc) {
269 fprintf(stderr, "A system must be specified in auto mode.\n");
273 return arg; /* Don't SetLabel yet ! */
277 CheckLabel(const char *label, struct prompt *prompt, int mode)
281 if ((err = system_IsValid(label, prompt, mode)) != NULL) {
282 fprintf(stderr, "%s: %s\n", label, err);
283 if (mode == PHYS_DIRECT)
284 log_Printf(LogWARN, "Label %s rejected -direct connection: %s\n",
293 main(int argc, char **argv)
296 const char *lastlabel;
297 int nfds, label, arg;
298 struct bundle *bundle;
299 struct prompt *prompt;
302 nfds = getdtablesize();
303 if (nfds >= FD_SETSIZE)
305 * If we've got loads of file descriptors, make sure they're all
306 * closed. If they aren't, we may end up with a seg fault when our
307 * `fd_set's get too big when select()ing !
312 name = strrchr(argv[0], '/');
313 log_Open(name ? name + 1 : argv[0]);
318 label = ProcessArgs(argc, argv, &sw);
321 * A FreeBSD & OpenBSD hack to dodge a bug in the tty driver that drops
322 * output occasionally.... I must find the real reason some time. To
323 * display the dodgy behaviour, comment out this bit, make yourself a large
324 * routing table and then run ppp in interactive mode. The `show route'
325 * command will drop chunks of data !!!
327 if (sw.mode == PHYS_INTERACTIVE) {
329 if (open(_PATH_TTY, O_RDONLY) != STDIN_FILENO) {
330 fprintf(stderr, "Cannot open %s for input !\n", _PATH_TTY);
335 /* Allow output for the moment (except in direct mode) */
336 if (sw.mode == PHYS_DIRECT)
339 SignalPrompt = prompt = prompt_Create(NULL, NULL, PROMPT_STD);
342 if (ID0realuid() != 0) {
343 char conf[200], *ptr;
345 snprintf(conf, sizeof conf, "%s/%s", _PATH_PPP, CONFFILE);
349 if (stat(conf, &sb) == 0 && sb.st_mode & S_IWOTH) {
350 log_Printf(LogALERT, "ppp: Access violation: Please protect %s\n",
354 ptr = conf + strlen(conf)-2;
355 while (ptr > conf && *ptr != '/')
357 } while (ptr >= conf);
361 for (arg = label; arg < argc; arg++)
362 CheckLabel(argv[arg], prompt, sw.mode);
364 CheckLabel("default", prompt, sw.mode);
367 prompt_Printf(prompt, "Working in %s mode\n", mode2Nam(sw.mode));
369 if ((bundle = bundle_Create(TUN_PREFIX, sw.mode, sw.unit)) == NULL)
372 /* NOTE: We may now have changed argv[1] via a ``set proctitle'' */
375 prompt->bundle = bundle; /* couldn't do it earlier */
377 prompt_Printf(prompt, "Using interface: %s\n", bundle->iface->name);
379 SignalBundle = bundle;
380 bundle->NatEnabled = sw.nat;
382 bundle->cfg.opt |= OPT_IFACEALIAS;
384 if (system_Select(bundle, "default", CONFFILE, prompt, NULL) < 0)
385 prompt_Printf(prompt, "Warning: No default entry found in config file.\n");
387 sig_signal(SIGHUP, CloseSession);
388 sig_signal(SIGTERM, CloseSession);
389 sig_signal(SIGINT, CloseConnection);
390 sig_signal(SIGQUIT, CloseSession);
391 sig_signal(SIGALRM, SIG_IGN);
392 signal(SIGPIPE, SIG_IGN);
394 if (sw.mode == PHYS_INTERACTIVE)
395 sig_signal(SIGTSTP, TerminalStop);
397 sig_signal(SIGUSR2, BringDownServer);
399 lastlabel = argv[argc - 1];
400 for (arg = label; arg < argc; arg++) {
401 /* In case we use LABEL or ``set enddisc label'' */
402 bundle_SetLabel(bundle, lastlabel);
403 system_Select(bundle, argv[arg], CONFFILE, prompt, NULL);
407 /* In case the last label did a ``load'' */
408 bundle_SetLabel(bundle, lastlabel);
410 if (sw.mode == PHYS_AUTO &&
411 bundle->ncp.ipcp.cfg.peer_range.ipaddr.s_addr == INADDR_ANY) {
412 prompt_Printf(prompt, "You must ``set ifaddr'' with a peer address "
414 AbortProgram(EX_START);
417 if (sw.mode != PHYS_INTERACTIVE) {
418 if (sw.mode != PHYS_DIRECT) {
423 if (sw.mode == PHYS_BACKGROUND && pipe(bgpipe)) {
424 log_Printf(LogERROR, "pipe: %s\n", strerror(errno));
425 AbortProgram(EX_SOCK);
430 log_Printf(LogERROR, "fork: %s\n", strerror(errno));
431 AbortProgram(EX_SOCK);
437 if (sw.mode == PHYS_BACKGROUND) {
440 /* If we get a signal, kill the child */
441 signal(SIGHUP, KillChild);
442 signal(SIGTERM, KillChild);
443 signal(SIGINT, KillChild);
444 signal(SIGQUIT, KillChild);
446 /* Wait for our child to close its pipe before we exit */
447 if (read(bgpipe[0], &c, 1) != 1) {
448 prompt_Printf(prompt, "Child exit, no status.\n");
449 log_Printf(LogPHASE, "Parent: Child exit, no status.\n");
450 } else if (c == EX_NORMAL) {
451 prompt_Printf(prompt, "PPP enabled.\n");
452 log_Printf(LogPHASE, "Parent: PPP enabled.\n");
454 prompt_Printf(prompt, "Child failed (%s).\n", ex_desc((int) c));
455 log_Printf(LogPHASE, "Parent: Child failed (%s).\n",
461 } else if (sw.mode == PHYS_BACKGROUND) {
463 bundle->notify.fd = bgpipe[1];
466 bundle_ChangedPID(bundle);
467 bundle_LockTun(bundle); /* we have a new pid */
470 /* -auto, -dedicated, -ddial, -foreground & -background */
471 prompt_Destroy(prompt, 0);
472 close(STDOUT_FILENO);
473 close(STDERR_FILENO);
478 /* -direct - STDIN_FILENO gets used by physical_Open */
479 prompt_TtyInit(NULL);
480 close(STDOUT_FILENO);
481 close(STDERR_FILENO);
485 close(STDERR_FILENO);
486 prompt_TtyInit(prompt);
487 prompt_TtyCommandMode(prompt);
488 prompt_Required(prompt);
491 log_Printf(LogPHASE, "PPP Started (%s mode).\n", mode2Nam(sw.mode));
493 AbortProgram(EX_NORMAL);
499 DoLoop(struct bundle *bundle)
501 fd_set rfds, wfds, efds;
502 int i, nfds, nothing_done;
507 for (; !bundle_IsDead(bundle); bundle_CleanDatalinks(bundle)) {
513 /* All our datalinks, the tun device and the MP socket */
514 descriptor_UpdateSet(&bundle->desc, &rfds, &wfds, &efds, &nfds);
516 /* All our prompts and the diagnostic socket */
517 descriptor_UpdateSet(&server.desc, &rfds, NULL, NULL, &nfds);
519 if (bundle_IsDead(bundle))
520 /* Don't select - we'll be here forever */
524 * It's possible that we've had a signal since we last checked. If
525 * we don't check again before calling select(), we may end up stuck
526 * after having missed the event.... sig_Handle() tries to be as
527 * quick as possible if nothing is likely to have happened.
528 * This is only really likely if we block in open(... O_NONBLOCK)
529 * which will happen with a misconfigured device.
534 i = select(nfds, &rfds, &wfds, &efds, NULL);
536 if (i < 0 && errno != EINTR) {
537 log_Printf(LogERROR, "DoLoop: select(): %s\n", strerror(errno));
538 if (log_IsKept(LogTIMER)) {
541 for (i = 0; i <= nfds; i++) {
542 if (FD_ISSET(i, &rfds)) {
543 log_Printf(LogTIMER, "Read set contains %d\n", i);
545 t.tv_sec = t.tv_usec = 0;
546 if (select(nfds, &rfds, &wfds, &efds, &t) != -1) {
547 log_Printf(LogTIMER, "The culprit !\n");
551 if (FD_ISSET(i, &wfds)) {
552 log_Printf(LogTIMER, "Write set contains %d\n", i);
554 t.tv_sec = t.tv_usec = 0;
555 if (select(nfds, &rfds, &wfds, &efds, &t) != -1) {
556 log_Printf(LogTIMER, "The culprit !\n");
560 if (FD_ISSET(i, &efds)) {
561 log_Printf(LogTIMER, "Error set contains %d\n", i);
563 t.tv_sec = t.tv_usec = 0;
564 if (select(nfds, &rfds, &wfds, &efds, &t) != -1) {
565 log_Printf(LogTIMER, "The culprit !\n");
574 log_Printf(LogTIMER, "Select returns %d\n", i);
581 for (i = 0; i <= nfds; i++)
582 if (FD_ISSET(i, &efds)) {
583 log_Printf(LogPHASE, "Exception detected on descriptor %d\n", i);
584 /* We deal gracefully with link descriptor exceptions */
585 if (!bundle_Exception(bundle, i)) {
586 log_Printf(LogERROR, "Exception cannot be handled !\n");
596 if (descriptor_IsSet(&server.desc, &rfds)) {
597 descriptor_Read(&server.desc, bundle, &rfds);
601 if (descriptor_IsSet(&bundle->desc, &rfds)) {
602 descriptor_Read(&bundle->desc, bundle, &rfds);
606 if (descriptor_IsSet(&bundle->desc, &wfds))
607 if (!descriptor_Write(&bundle->desc, bundle, &wfds) && nothing_done) {
609 * This is disasterous. The OS has told us that something is
610 * writable, and all our write()s have failed. Rather than
611 * going back immediately to do our UpdateSet()s and select(),
612 * we sleep for a bit to avoid gobbling up all cpu time.
618 select(0, NULL, NULL, NULL, &t);
622 log_Printf(LogDEBUG, "DoLoop done.\n");