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
28 * $FreeBSD: src/usr.sbin/ppp/main.c,v 1.167.2.8 2002/09/01 02:12:28 brian Exp $
29 * $DragonFly: src/usr.sbin/ppp/main.c,v 1.2 2003/06/17 04:30:00 dillon Exp $
32 #include <sys/param.h>
33 #include <netinet/in.h>
34 #include <netinet/in_systm.h>
35 #include <netinet/ip.h>
37 #include <sys/socket.h>
38 #include <net/route.h>
74 #include "throughput.h"
75 #include "slcompress.h"
79 #include "descriptor.h"
102 #define O_NONBLOCK O_NDELAY
106 static void DoLoop(struct bundle
*);
107 static void TerminalStop(int);
109 static struct bundle
*SignalBundle
;
110 static struct prompt
*SignalPrompt
;
115 SignalBundle
->CleaningUp
= 1;
116 bundle_Close(SignalBundle
, NULL
, CLOSE_STAYDOWN
);
120 AbortProgram(int excode
)
123 server_Close(SignalBundle
);
124 log_Printf(LogPHASE
, "PPP Terminated (%s).\n", ex_desc(excode
));
126 bundle_Close(SignalBundle
, NULL
, CLOSE_STAYDOWN
);
127 bundle_Destroy(SignalBundle
);
134 CloseConnection(int signo
)
136 /* NOTE, these are manual, we've done a setsid() */
137 sig_signal(SIGINT
, SIG_IGN
);
138 log_Printf(LogPHASE
, "Caught signal %d, abort connection(s)\n", signo
);
139 bundle_Down(SignalBundle
, CLOSE_STAYDOWN
);
140 sig_signal(SIGINT
, CloseConnection
);
144 CloseSession(int signo
)
146 log_Printf(LogPHASE
, "Signal %d, terminate.\n", signo
);
150 static pid_t BGPid
= 0;
155 signal(signo
, SIG_IGN
);
156 log_Printf(LogPHASE
, "Parent: Signal %d\n", signo
);
161 TerminalCont(int signo __unused
)
163 signal(SIGCONT
, SIG_DFL
);
164 prompt_Continue(SignalPrompt
);
168 TerminalStop(int signo __unused
)
170 prompt_Suspend(SignalPrompt
);
171 signal(SIGCONT
, TerminalCont
);
176 BringDownServer(int signo __unused
)
178 /* Drops all child prompts too ! */
179 if (server_Close(SignalBundle
))
180 log_Printf(LogPHASE
, "Closed server socket\n");
184 RestartServer(int signo __unused
)
186 /* Drops all child prompts and re-opens the socket */
187 server_Reopen(SignalBundle
);
193 fprintf(stderr
, "usage: ppp [-auto | -foreground | -background | -direct |"
194 " -dedicated | -ddial | -interactive]"
198 " [-quiet] [-unit N] [system ...]\n");
211 ProcessArgs(int argc
, char **argv
, struct switches
*sw
)
213 int optc
, newmode
, arg
;
217 memset(sw
, '\0', sizeof *sw
);
218 sw
->mode
= PHYS_INTERACTIVE
;
221 for (arg
= 1; arg
< argc
&& *argv
[arg
] == '-'; arg
++, optc
++) {
223 newmode
= Nam2mode(cp
);
226 if (strcmp(cp
, "nat") == 0) {
228 log_Printf(LogWARN
, "%s ignored: NAT is compiled out\n", argv
[arg
]);
232 optc
--; /* this option isn't exclusive */
233 } else if (strcmp(cp
, "alias") == 0) {
235 log_Printf(LogWARN
, "%s ignored: NAT is compiled out\n", argv
[arg
]);
236 fprintf(stderr
, "%s ignored: NAT is compiled out\n", argv
[arg
]);
238 log_Printf(LogWARN
, "%s is deprecated\n", argv
[arg
]);
239 fprintf(stderr
, "%s is deprecated\n", argv
[arg
]);
242 optc
--; /* this option isn't exclusive */
243 } else if (strncmp(cp
, "unit", 4) == 0) {
244 optc
--; /* this option isn't exclusive */
246 optc
--; /* nor is the argument */
248 fprintf(stderr
, "-unit: Expected unit number\n");
251 sw
->unit
= atoi(argv
[arg
]);
253 sw
->unit
= atoi(cp
+ 4);
254 } else if (strcmp(cp
, "quiet") == 0) {
256 optc
--; /* this option isn't exclusive */
267 if (newmode
== PHYS_FOREGROUND
)
273 fprintf(stderr
, "You may specify only one mode.\n");
277 if (sw
->mode
== PHYS_AUTO
&& arg
== argc
) {
278 fprintf(stderr
, "A system must be specified in auto mode.\n");
282 return arg
; /* Don't SetLabel yet ! */
286 CheckLabel(const char *label
, struct prompt
*prompt
, int mode
)
290 if ((err
= system_IsValid(label
, prompt
, mode
)) != NULL
) {
291 fprintf(stderr
, "%s: %s\n", label
, err
);
292 if (mode
== PHYS_DIRECT
)
293 log_Printf(LogWARN
, "Label %s rejected -direct connection: %s\n",
302 main(int argc
, char **argv
)
305 const char *lastlabel
;
306 int arg
, holdfd
[3], label
;
308 struct bundle
*bundle
;
309 struct prompt
*prompt
;
315 * We open 3 descriptors to ensure that STDIN_FILENO, STDOUT_FILENO and
316 * STDERR_FILENO are always open. These are closed before DoLoop(),
317 * but *after* we've avoided the possibility of erroneously closing
318 * an important descriptor with close(STD{IN,OUT,ERR}_FILENO).
320 if ((holdfd
[0] = open(_PATH_DEVNULL
, O_RDWR
)) == -1) {
321 fprintf(stderr
, "Cannot open %s !\n", _PATH_DEVNULL
);
324 for (f
= 1; f
< sizeof holdfd
/ sizeof *holdfd
; f
++)
325 holdfd
[f
] = dup(holdfd
[0]);
327 name
= strrchr(argv
[0], '/');
328 log_Open(name
? name
+ 1 : argv
[0]);
333 label
= ProcessArgs(argc
, argv
, &sw
);
336 * A FreeBSD & OpenBSD hack to dodge a bug in the tty driver that drops
337 * output occasionally.... I must find the real reason some time. To
338 * display the dodgy behaviour, comment out this bit, make yourself a large
339 * routing table and then run ppp in interactive mode. The `show route'
340 * command will drop chunks of data !!!
342 if (sw
.mode
== PHYS_INTERACTIVE
) {
344 if (open(_PATH_TTY
, O_RDONLY
) != STDIN_FILENO
) {
345 fprintf(stderr
, "Cannot open %s for input !\n", _PATH_TTY
);
350 /* Allow output for the moment (except in direct mode) */
351 if (sw
.mode
== PHYS_DIRECT
)
354 SignalPrompt
= prompt
= prompt_Create(NULL
, NULL
, PROMPT_STD
);
357 if (ID0realuid() != 0) {
358 char conf
[200], *ptr
;
360 snprintf(conf
, sizeof conf
, "%s/%s", PPP_CONFDIR
, CONFFILE
);
364 if (stat(conf
, &sb
) == 0 && sb
.st_mode
& S_IWOTH
) {
365 log_Printf(LogALERT
, "ppp: Access violation: Please protect %s\n",
369 ptr
= conf
+ strlen(conf
)-2;
370 while (ptr
> conf
&& *ptr
!= '/')
372 } while (ptr
>= conf
);
376 for (arg
= label
; arg
< argc
; arg
++)
377 CheckLabel(argv
[arg
], prompt
, sw
.mode
);
379 CheckLabel("default", prompt
, sw
.mode
);
382 prompt_Printf(prompt
, "Working in %s mode\n", mode2Nam(sw
.mode
));
384 if ((bundle
= bundle_Create(TUN_PREFIX
, sw
.mode
, sw
.unit
)) == NULL
)
387 /* NOTE: We may now have changed argv[1] via a ``set proctitle'' */
390 prompt
->bundle
= bundle
; /* couldn't do it earlier */
392 prompt_Printf(prompt
, "Using interface: %s\n", bundle
->iface
->name
);
394 SignalBundle
= bundle
;
395 bundle
->NatEnabled
= sw
.nat
;
397 bundle
->cfg
.opt
|= OPT_IFACEALIAS
;
399 if (system_Select(bundle
, "default", CONFFILE
, prompt
, NULL
) < 0)
400 prompt_Printf(prompt
, "Warning: No default entry found in config file.\n");
402 sig_signal(SIGHUP
, CloseSession
);
403 sig_signal(SIGTERM
, CloseSession
);
404 sig_signal(SIGINT
, CloseConnection
);
405 sig_signal(SIGQUIT
, CloseSession
);
406 sig_signal(SIGALRM
, SIG_IGN
);
407 signal(SIGPIPE
, SIG_IGN
);
409 if (sw
.mode
== PHYS_INTERACTIVE
)
410 sig_signal(SIGTSTP
, TerminalStop
);
412 sig_signal(SIGUSR1
, RestartServer
);
413 sig_signal(SIGUSR2
, BringDownServer
);
415 lastlabel
= argv
[argc
- 1];
416 for (arg
= label
; arg
< argc
; arg
++) {
417 /* In case we use LABEL or ``set enddisc label'' */
418 bundle_SetLabel(bundle
, lastlabel
);
419 system_Select(bundle
, argv
[arg
], CONFFILE
, prompt
, NULL
);
423 /* In case the last label did a ``load'' */
424 bundle_SetLabel(bundle
, lastlabel
);
426 if (sw
.mode
== PHYS_AUTO
&&
427 ncprange_family(&bundle
->ncp
.ipcp
.cfg
.peer_range
) == AF_UNSPEC
) {
428 prompt_Printf(prompt
, "You must ``set ifaddr'' with a peer address "
430 AbortProgram(EX_START
);
433 if (sw
.mode
!= PHYS_INTERACTIVE
) {
434 if (sw
.mode
!= PHYS_DIRECT
) {
439 if (sw
.mode
== PHYS_BACKGROUND
&& pipe(bgpipe
)) {
440 log_Printf(LogERROR
, "pipe: %s\n", strerror(errno
));
441 AbortProgram(EX_SOCK
);
446 log_Printf(LogERROR
, "fork: %s\n", strerror(errno
));
447 AbortProgram(EX_SOCK
);
454 if (sw
.mode
== PHYS_BACKGROUND
) {
457 /* If we get a signal, kill the child */
458 signal(SIGHUP
, KillChild
);
459 signal(SIGTERM
, KillChild
);
460 signal(SIGINT
, KillChild
);
461 signal(SIGQUIT
, KillChild
);
463 /* Wait for our child to close its pipe before we exit */
464 while ((ret
= read(bgpipe
[0], &c
, 1)) == 1) {
468 prompt_Printf(prompt
, "PPP enabled\n");
469 log_Printf(LogPHASE
, "Parent: PPP enabled\n");
474 prompt_Printf(prompt
, "Attempting redial\n");
478 prompt_Printf(prompt
, "Attempting reconnect\n");
481 prompt_Printf(prompt
, "Child failed (%s)\n",
483 log_Printf(LogPHASE
, "Parent: Child failed (%s)\n",
489 prompt_Printf(prompt
, "Child exit, no status.\n");
490 log_Printf(LogPHASE
, "Parent: Child exit, no status.\n");
495 } else if (sw
.mode
== PHYS_BACKGROUND
) {
497 bundle
->notify
.fd
= bgpipe
[1];
500 bundle_ChangedPID(bundle
);
501 bundle_LockTun(bundle
); /* we have a new pid */
504 /* -auto, -dedicated, -ddial, -foreground & -background */
505 prompt_Destroy(prompt
, 0);
506 close(STDOUT_FILENO
);
507 close(STDERR_FILENO
);
512 /* -direct - STDIN_FILENO gets used by physical_Open */
513 prompt_TtyInit(NULL
);
514 close(STDOUT_FILENO
);
515 close(STDERR_FILENO
);
519 close(STDERR_FILENO
);
520 prompt_TtyInit(prompt
);
521 prompt_TtyCommandMode(prompt
);
522 prompt_Required(prompt
);
525 /* We can get rid of these now */
526 for (f
= 0; f
< sizeof holdfd
/ sizeof *holdfd
; f
++)
529 log_Printf(LogPHASE
, "PPP Started (%s mode).\n", mode2Nam(sw
.mode
));
531 AbortProgram(EX_NORMAL
);
537 DoLoop(struct bundle
*bundle
)
539 fd_set
*rfds
, *wfds
, *efds
;
540 int i
, nfds
, nothing_done
;
542 if ((rfds
= mkfdset()) == NULL
) {
543 log_Printf(LogERROR
, "DoLoop: Cannot create fd_set\n");
547 if ((wfds
= mkfdset()) == NULL
) {
548 log_Printf(LogERROR
, "DoLoop: Cannot create fd_set\n");
553 if ((efds
= mkfdset()) == NULL
) {
554 log_Printf(LogERROR
, "DoLoop: Cannot create fd_set\n");
560 for (; !bundle_IsDead(bundle
); bundle_CleanDatalinks(bundle
)) {
566 /* All our datalinks, the tun device and the MP socket */
567 descriptor_UpdateSet(&bundle
->desc
, rfds
, wfds
, efds
, &nfds
);
569 /* All our prompts and the diagnostic socket */
570 descriptor_UpdateSet(&server
.desc
, rfds
, NULL
, NULL
, &nfds
);
572 bundle_CleanDatalinks(bundle
);
573 if (bundle_IsDead(bundle
))
574 /* Don't select - we'll be here forever */
578 * It's possible that we've had a signal since we last checked. If
579 * we don't check again before calling select(), we may end up stuck
580 * after having missed the event.... sig_Handle() tries to be as
581 * quick as possible if nothing is likely to have happened.
582 * This is only really likely if we block in open(... O_NONBLOCK)
583 * which will happen with a misconfigured device.
588 i
= select(nfds
, rfds
, wfds
, efds
, NULL
);
590 if (i
< 0 && errno
!= EINTR
) {
591 log_Printf(LogERROR
, "DoLoop: select(): %s\n", strerror(errno
));
592 if (log_IsKept(LogTIMER
)) {
595 for (i
= 0; i
<= nfds
; i
++) {
596 if (FD_ISSET(i
, rfds
)) {
597 log_Printf(LogTIMER
, "Read set contains %d\n", i
);
599 t
.tv_sec
= t
.tv_usec
= 0;
600 if (select(nfds
, rfds
, wfds
, efds
, &t
) != -1) {
601 log_Printf(LogTIMER
, "The culprit !\n");
605 if (FD_ISSET(i
, wfds
)) {
606 log_Printf(LogTIMER
, "Write set contains %d\n", i
);
608 t
.tv_sec
= t
.tv_usec
= 0;
609 if (select(nfds
, rfds
, wfds
, efds
, &t
) != -1) {
610 log_Printf(LogTIMER
, "The culprit !\n");
614 if (FD_ISSET(i
, efds
)) {
615 log_Printf(LogTIMER
, "Error set contains %d\n", i
);
617 t
.tv_sec
= t
.tv_usec
= 0;
618 if (select(nfds
, rfds
, wfds
, efds
, &t
) != -1) {
619 log_Printf(LogTIMER
, "The culprit !\n");
628 log_Printf(LogTIMER
, "Select returns %d\n", i
);
635 for (i
= 0; i
<= nfds
; i
++)
636 if (FD_ISSET(i
, efds
)) {
637 log_Printf(LogPHASE
, "Exception detected on descriptor %d\n", i
);
638 /* We deal gracefully with link descriptor exceptions */
639 if (!bundle_Exception(bundle
, i
)) {
640 log_Printf(LogERROR
, "Exception cannot be handled !\n");
650 if (descriptor_IsSet(&server
.desc
, rfds
)) {
651 descriptor_Read(&server
.desc
, bundle
, rfds
);
655 if (descriptor_IsSet(&bundle
->desc
, rfds
)) {
656 descriptor_Read(&bundle
->desc
, bundle
, rfds
);
660 if (descriptor_IsSet(&bundle
->desc
, wfds
))
661 if (descriptor_Write(&bundle
->desc
, bundle
, wfds
) <= 0 && nothing_done
) {
663 * This is disastrous. The OS has told us that something is
664 * writable, and all our write()s have failed. Rather than
665 * going back immediately to do our UpdateSet()s and select(),
666 * we sleep for a bit to avoid gobbling up all cpu time.
672 select(0, NULL
, NULL
, NULL
, &t
);
676 log_Printf(LogDEBUG
, "DoLoop done.\n");