2 * main.c - Point-to-Point Protocol main module
4 * Copyright (c) 1989 Carnegie Mellon University.
7 * Redistribution and use in source and binary forms are permitted
8 * provided that the above copyright notice and this paragraph are
9 * duplicated in all such forms and that any documentation,
10 * advertising materials, and other materials related to such
11 * distribution and use acknowledge that the software was developed
12 * by Carnegie Mellon University. The name of the
13 * University may not be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 static char rcsid[] = "$Id: main.c,v 1.10 1994/05/18 06:00:48 paulus Exp $";
37 * If REQ_SYSOPTIONS is defined to 1, pppd will not run unless
38 * /etc/ppp/options exists.
40 #ifndef REQ_SYSOPTIONS
41 #define REQ_SYSOPTIONS 1
48 #include <sys/ioctl.h>
53 #include <sys/param.h>
54 #include <sys/types.h>
56 #include <sys/socket.h>
62 #include <net/if_ppp.h>
79 #include "pathnames.h"
80 #include "patchlevel.h"
92 static char *pidpath = PIDPATH; /* filename in which pid will be stored */
94 static char *pidpath = _PATH_PIDFILE;
98 char ifname[IFNAMSIZ]; /* Interface name */
99 int ifunit; /* Interface unit number */
101 char *progname; /* Name of this program */
102 char hostname[MAXNAMELEN]; /* Our hostname */
103 char our_name[MAXNAMELEN];
104 char remote_name[MAXNAMELEN];
105 static char pidfilename[MAXPATHLEN];
107 static pid_t pid; /* Our pid */
108 static pid_t pgrpid; /* Process Group ID */
109 uid_t uid; /* Our real user-id */
111 char devname[MAXPATHLEN] = "/dev/tty"; /* Device name */
112 int default_device = TRUE; /* use default device (stdin/out) */
114 int fd = -1; /* Device file descriptor */
115 int s; /* Socket file descriptor */
117 int phase; /* where the link is at */
120 static struct sgttyb initsgttyb; /* Initial TTY sgttyb */
122 static struct termios inittermios; /* Initial TTY termios */
125 static int initfdflags = -1; /* Initial file descriptor flags */
127 static int restore_term; /* 1 => we've munged the terminal */
129 u_char outpacket_buf[MTU+DLLHEADERLEN]; /* buffer for outgoing packet */
130 static u_char inpacket_buf[MTU+DLLHEADERLEN]; /* buffer for incoming packet */
132 int hungup; /* terminal has been hung up */
133 static int n_children; /* # child processes still running */
135 /* configured variables */
137 int debug = 0; /* Debug flag */
138 int kdebugflag = 0; /* Kernel debugging flag */
139 char user[MAXNAMELEN]; /* username for PAP */
140 char passwd[MAXSECRETLEN]; /* password for PAP */
141 char *connector = NULL; /* "connect" command */
142 char *disconnector = NULL; /* "disconnect" command */
143 int inspeed = 0; /* Input/Output speed requested */
144 int baud_rate; /* bits/sec currently used */
145 u_long netmask = 0; /* netmask to use on ppp interface */
146 int crtscts = 0; /* use h/w flow control */
147 int nodetach = 0; /* don't fork */
148 int modem = 0; /* use modem control lines */
149 int auth_required = 0; /* require peer to authenticate */
150 int defaultroute = 0; /* assign default route through interface */
151 int proxyarp = 0; /* set entry in arp table */
152 int persist = 0; /* re-initiate on termination */
153 int answer = 0; /* wait for incoming call */
154 int uselogin = 0; /* check PAP info against /etc/passwd */
155 int lockflag = 0; /* lock the serial device */
159 static void hup __ARGS((int, int, struct sigcontext *, char *));
160 static void intr __ARGS((int, int, struct sigcontext *, char *));
161 static void term __ARGS((int, int, struct sigcontext *, char *));
162 static void alrm __ARGS((int, int, struct sigcontext *, char *));
163 static void io __ARGS((int, int, struct sigcontext *, char *));
164 static void incdebug __ARGS((int));
165 static void nodebug __ARGS((int));
166 void establish_ppp __ARGS((void));
168 void reap_kids __ARGS((void));
169 void cleanup __ARGS((int, caddr_t));
170 void die __ARGS((int));
171 void novm __ARGS((char *));
173 void log_packet __ARGS((u_char *, int, char *));
174 void format_packet __ARGS((u_char *, int,
175 void (*) (void *, char *, ...), void *));
176 void pr_log __ARGS((void *, char *, ...));
178 extern char *ttyname __ARGS((int));
179 extern char *getlogin __ARGS((void));
182 * PPP Data Link Layer "protocol" table.
183 * One entry per supported protocol.
185 static struct protent {
193 { LCP, lcp_init, lcp_input, lcp_protrej, lcp_printpkt, "LCP" },
194 { IPCP, ipcp_init, ipcp_input, ipcp_protrej, ipcp_printpkt, "IPCP" },
195 { UPAP, upap_init, upap_input, upap_protrej, upap_printpkt, "PAP" },
196 { CHAP, ChapInit, ChapInput, ChapProtocolReject, ChapPrintPkt, "CHAP" },
199 #define N_PROTO (sizeof(prottbl) / sizeof(prottbl[0]))
216 if (gethostname(hostname, MAXNAMELEN) < 0 ) {
217 perror("couldn't get hostname");
220 hostname[MAXNAMELEN-1] = 0;
225 if (!ppp_available()) {
226 fprintf(stderr, "Sorry - PPP is not available on this system\n");
231 * Initialize to the standard option set, then parse, in order,
232 * the system options file, the user's options file, and the command
235 for (i = 0; i < N_PROTO; i++)
236 (*prottbl[i].init)(0);
240 if (!options_from_file(_PATH_SYSOPTIONS, REQ_SYSOPTIONS, 0) ||
241 !options_from_user() ||
242 !parse_args(argc-1, argv+1) ||
245 check_auth_options();
248 if (lockflag && !default_device)
249 if (lock(devname) < 0)
253 * Initialize syslog system and magic number package.
255 #if (BSD >= 43 || defined(sun)) && !defined(ultrix)
256 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
257 setlogmask(LOG_UPTO(LOG_INFO));
259 openlog("pppd", LOG_PID);
260 #define LOG_UPTO(x) (x)
261 #define setlogmask(x) (x)
264 setlogmask(LOG_UPTO(LOG_DEBUG));
271 if (pw != NULL && pw->pw_name != NULL)
276 syslog(LOG_NOTICE, "pppd %s.%d started by %s, uid %d",
277 VERSION, PATCHLEVEL, p, uid);
281 * Make sure we can set the serial device to be our controlling terminal.
283 if (default_device) {
285 * No device name was specified:
286 * we are in the device's session already.
288 if ((pgrpid = getpgrp(0)) < 0) {
289 syslog(LOG_ERR, "getpgrp(0): %m");
295 * Not default device: make sure we're not a process group leader,
296 * then become session leader of a new session (so we can make
297 * our device its controlling terminal and thus get SIGHUPs).
300 /* fork so we're not a process group leader */
302 exit(0); /* parent is finished */
305 syslog(LOG_ERR, "fork: %m");
308 pid = getpid(); /* otherwise pid is 0 in child */
311 * try to put ourself into our parent's process group,
312 * so we're not a process group leader
314 if (setpgrp(pid, getppid()) < 0)
315 syslog(LOG_WARNING, "setpgrp: %m");
318 /* create new session */
319 if ((pgrpid = setsid()) < 0) {
320 syslog(LOG_ERR, "setsid(): %m");
326 /* Get an internet socket for doing socket ioctl's on. */
327 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
328 syslog(LOG_ERR, "socket : %m");
333 * Compute mask of all interesting signals and install signal handlers
334 * for each. Only one signal handler may be active at a time. Therefore,
335 * all other signals should be masked when any handler is executing.
338 sigaddset(&mask, SIGHUP);
339 sigaddset(&mask, SIGINT);
340 sigaddset(&mask, SIGALRM);
341 sigaddset(&mask, SIGIO);
343 sigaddset(&mask, SIGPOLL);
346 #define SIGNAL(s, handler) { \
347 sv.sv_handler = handler; \
348 if (sigvec(s, &sv, NULL) < 0) { \
349 syslog(LOG_ERR, "sigvec(%d): %m", s); \
356 SIGNAL(SIGHUP, hup); /* Hangup */
357 SIGNAL(SIGINT, intr); /* Interrupt */
358 SIGNAL(SIGTERM, term); /* Terminate */
359 SIGNAL(SIGALRM, alrm); /* Timeout */
360 SIGNAL(SIGIO, io); /* Input available */
362 SIGNAL(SIGPOLL, io); /* Input available */
365 signal(SIGUSR1, incdebug); /* Increment debug flag */
366 signal(SIGUSR2, nodebug); /* Reset debug flag */
369 * Block SIGIOs and SIGPOLLs for now
372 sigaddset(&mask, SIGIO);
374 sigaddset(&mask, SIGPOLL);
376 sigprocmask(SIG_BLOCK, &mask, NULL);
379 * Open the serial device and set it up to be the ppp interface.
381 if ((fd = open(devname, O_RDWR /*| O_NDELAY*/)) < 0) {
382 syslog(LOG_ERR, "open(%s): %m", devname);
388 /* set device to be controlling tty */
389 if (!default_device && ioctl(fd, TIOCSCTTY) < 0) {
390 syslog(LOG_ERR, "ioctl(TIOCSCTTY): %m");
393 #endif /* TIOCSCTTY */
395 /* run connection script */
397 MAINDEBUG((LOG_INFO, "Connecting with <%s>", connector));
399 /* set line speed, flow control, etc.; set CLOCAL for now */
402 /* drop dtr to hang up in case modem is off hook */
403 if (!default_device && modem) {
409 if (device_script(connector, fd, fd) < 0) {
410 syslog(LOG_ERR, "could not set up connection");
415 syslog(LOG_INFO, "Connected...");
416 sleep(1); /* give it time to set up its terminal */
419 /* set line speed, flow control, etc.; clear CLOCAL if modem option */
422 /* set up the serial device as a ppp interface */
425 syslog(LOG_INFO, "Using interface ppp%d", ifunit);
426 (void) sprintf(ifname, "ppp%d", ifunit);
428 /* write pid to file */
429 (void) sprintf(pidfilename, "%s/%s.pid", pidpath, ifname);
430 if ((pidfile = fopen(pidfilename, "w")) != NULL) {
431 fprintf(pidfile, "%d\n", pid);
432 (void) fclose(pidfile);
434 syslog(LOG_ERR, "unable to create pid file: %m");
439 * Set process group of device to our process group so we can get
440 * SIGIOs and SIGHUPs.
443 if (default_device) {
444 int id = tcgetpgrp(fd);
446 syslog(LOG_WARNING, "warning: not in tty's process group");
449 if (tcsetpgrp(fd, pgrpid) < 0) {
450 syslog(LOG_ERR, "tcsetpgrp(): %m");
455 /* set process group on tty so we get SIGIO's */
456 if (ioctl(fd, TIOCSPGRP, &pgrpid) < 0) {
457 syslog(LOG_ERR, "ioctl(TIOCSPGRP): %m");
463 * Record initial device flags, then set device to cause SIGIO
464 * signals to be generated.
466 if ((initfdflags = fcntl(fd, F_GETFL)) == -1) {
467 syslog(LOG_ERR, "fcntl(F_GETFL): %m");
470 if (fcntl(fd, F_SETFL, FNDELAY | FASYNC) == -1) {
471 syslog(LOG_ERR, "fcntl(F_SETFL, FNDELAY | FASYNC): %m");
476 * Block all signals, start opening the connection, and wait for
477 * incoming signals (reply, timeout, etc.).
479 syslog(LOG_NOTICE, "Connect: %s <--> %s", ifname, devname);
480 sigprocmask(SIG_BLOCK, &mask, NULL); /* Block signals now */
481 lcp_lowerup(0); /* XXX Well, sort of... */
482 lcp_open(0); /* Start protocol */
483 for (phase = PHASE_ESTABLISH; phase != PHASE_DEAD; ) {
484 sigpause(0); /* Wait for next signal */
485 reap_kids(); /* Don't leave dead kids lying around */
489 * Run disconnector script, if requested
492 if (device_script(disconnector, fd, fd) < 0) {
493 syslog(LOG_WARNING, "disconnect script failed");
497 syslog(LOG_INFO, "Disconnected...");
505 * XXX assume speed_t values numerically equal bits per second
506 * (so we can ask for any speed).
508 #define translate_speed(bps) (bps)
509 #define baud_rate_of(speed) (speed)
513 * List of valid speeds.
516 int speed_int, speed_val;
588 * Translate from bits/second to a speed_t.
594 struct speed *speedp;
598 for (speedp = speeds; speedp->speed_int; speedp++)
599 if (bps == speedp->speed_int)
600 return speedp->speed_val;
601 syslog(LOG_WARNING, "speed %d not supported", bps);
606 * Translate from a speed_t to bits/second.
612 struct speed *speedp;
616 for (speedp = speeds; speedp->speed_int; speedp++)
617 if (speed == speedp->speed_val)
618 return speedp->speed_int;
624 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
625 * at the requested speed, etc. If `local' is true, set CLOCAL
626 * regardless of whether the modem option was specified.
628 set_up_tty(fd, local)
635 if (tcgetattr(fd, &tios) < 0) {
636 syslog(LOG_ERR, "tcgetattr: %m");
644 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL | CRTSCTS);
646 tios.c_cflag |= CRTSCTS;
648 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
651 tios.c_cflag |= CS8 | CREAD | HUPCL;
653 tios.c_cflag |= CLOCAL;
654 tios.c_iflag = IGNBRK | IGNPAR;
658 tios.c_cc[VTIME] = 0;
659 speed = translate_speed(inspeed);
661 cfsetospeed(&tios, speed);
662 cfsetispeed(&tios, speed);
664 speed = cfgetospeed(&tios);
667 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
668 syslog(LOG_ERR, "tcsetattr: %m");
674 if (ioctl(fd, (crtscts || modem)? TIOCMODEM: TIOCNMODEM, &x) < 0)
675 syslog(LOG_WARNING, "TIOC(N)MODEM: %m");
676 if (ioctl(fd, (local || !modem)? TIOCNCAR: TIOCCAR) < 0)
677 syslog(LOG_WARNING, "TIOC(N)CAR: %m");
682 struct sgttyb sgttyb;
685 * Put the tty in raw mode.
687 if (ioctl(fd, TIOCGETP, &sgttyb) < 0) {
688 syslog(LOG_ERR, "ioctl(TIOCGETP): %m");
695 sgttyb.sg_flags = RAW | ANYP;
696 speed = translate_speed(inspeed);
698 sgttyb.sg_ispeed = speed;
700 speed = sgttyb.sg_ispeed;
702 if (ioctl(fd, TIOCSETP, &sgttyb) < 0) {
703 syslog(LOG_ERR, "ioctl(TIOCSETP): %m");
708 baud_rate = baud_rate_of(speed);
713 * setdtr - control the DTR line on the serial port.
714 * This is called from die(), so it shouldn't call die().
719 int modembits = TIOCM_DTR;
721 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
726 * quit - Clean up state and exit.
735 * die - like quit, except we can specify an exit status.
742 syslog(LOG_INFO, "Exit.");
747 * cleanup - restore anything which needs to be restored before we exit
756 /* drop dtr to hang up */
760 if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
761 syslog(LOG_WARNING, "fcntl(F_SETFL, fdflags): %m");
767 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
768 syslog(LOG_WARNING, "tcsetattr: %m");
770 if (ioctl(fd, TIOCSETP, &initsgttyb) < 0)
771 syslog(LOG_WARNING, "ioctl(TIOCSETP): %m");
779 if (pidfilename[0] != 0 && unlink(pidfilename) < 0)
780 syslog(LOG_WARNING, "unable to unlink pid file: %m");
783 if (lockflag && !default_device)
788 static struct callout *callout = NULL; /* Callout list */
789 static struct timeval schedtime; /* Time last timeout was set */
792 * timeout - Schedule a timeout.
794 * Note that this timeout takes the number of seconds, NOT hz (as in
798 timeout(func, arg, time)
803 struct itimerval itv;
804 struct callout *newp, **oldpp;
806 MAINDEBUG((LOG_DEBUG, "Timeout %x:%x in %d seconds.",
807 (int) func, (int) arg, time));
812 if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL) {
813 syslog(LOG_ERR, "Out of memory in timeout()!");
820 * Find correct place to link it in and decrement its time by the
821 * amount of time used by preceding timeouts.
823 for (oldpp = &callout;
824 *oldpp && (*oldpp)->c_time <= time;
825 oldpp = &(*oldpp)->c_next)
826 time -= (*oldpp)->c_time;
828 newp->c_next = *oldpp;
830 (*oldpp)->c_time -= time;
834 * If this is now the first callout then we have to set a new
837 if (callout == newp) {
838 itv.it_interval.tv_sec = itv.it_interval.tv_usec =
839 itv.it_value.tv_usec = 0;
840 itv.it_value.tv_sec = callout->c_time;
841 MAINDEBUG((LOG_DEBUG, "Setting itimer for %d seconds in timeout.",
842 itv.it_value.tv_sec));
843 if (setitimer(ITIMER_REAL, &itv, NULL)) {
844 syslog(LOG_ERR, "setitimer(ITIMER_REAL): %m");
847 if (gettimeofday(&schedtime, NULL)) {
848 syslog(LOG_ERR, "gettimeofday: %m");
856 * untimeout - Unschedule a timeout.
863 struct itimerval itv;
864 struct callout **copp, *freep;
867 MAINDEBUG((LOG_DEBUG, "Untimeout %x:%x.", (int) func, (int) arg));
870 * If the first callout is unscheduled then we have to set a new
874 callout->c_func == func &&
875 callout->c_arg == arg)
879 * Find first matching timeout. Add its time to the next timeouts
882 for (copp = &callout; *copp; copp = &(*copp)->c_next)
883 if ((*copp)->c_func == func &&
884 (*copp)->c_arg == arg) {
886 *copp = freep->c_next;
888 (*copp)->c_time += freep->c_time;
889 (void) free((char *) freep);
894 itv.it_interval.tv_sec = itv.it_interval.tv_usec =
895 itv.it_value.tv_usec = 0;
896 itv.it_value.tv_sec = callout ? callout->c_time : 0;
897 MAINDEBUG((LOG_DEBUG, "Setting itimer for %d seconds in untimeout.",
898 itv.it_value.tv_sec));
899 if (setitimer(ITIMER_REAL, &itv, NULL)) {
900 syslog(LOG_ERR, "setitimer(ITIMER_REAL): %m");
903 if (gettimeofday(&schedtime, NULL)) {
904 syslog(LOG_ERR, "gettimeofday: %m");
912 * adjtimeout - Decrement the first timeout by the amount of time since
924 * Make sure that the clock hasn't been warped dramatically.
925 * Account for recently expired, but blocked timer by adding
926 * small fudge factor.
928 if (gettimeofday(&tv, NULL)) {
929 syslog(LOG_ERR, "gettimeofday: %m");
932 timediff = tv.tv_sec - schedtime.tv_sec;
934 timediff > callout->c_time + 1)
937 callout->c_time -= timediff; /* OK, Adjust time */
942 * hup - Catch SIGHUP signal.
944 * Indicates that the physical layer has been disconnected.
948 hup(sig, code, scp, addr)
950 struct sigcontext *scp;
953 syslog(LOG_INFO, "Hangup (SIGHUP)");
955 hungup = 1; /* they hung up on us! */
956 persist = 0; /* don't try to restart */
957 adjtimeout(); /* Adjust timeouts */
958 lcp_lowerdown(0); /* Reset connection */
959 quit(); /* and die */
964 * term - Catch SIGTERM signal.
966 * Indicates that we should initiate a graceful disconnect and exit.
970 term(sig, code, scp, addr)
972 struct sigcontext *scp;
975 syslog(LOG_INFO, "Terminating link.");
976 persist = 0; /* don't try to restart */
977 adjtimeout(); /* Adjust timeouts */
978 lcp_close(0); /* Close connection */
983 * intr - Catch SIGINT signal (DEL/^C).
985 * Indicates that we should initiate a graceful disconnect and exit.
989 intr(sig, code, scp, addr)
991 struct sigcontext *scp;
994 syslog(LOG_INFO, "Interrupt received: terminating link");
995 persist = 0; /* don't try to restart */
996 adjtimeout(); /* Adjust timeouts */
997 lcp_close(0); /* Close connection */
1002 * alrm - Catch SIGALRM signal.
1004 * Indicates a timeout.
1008 alrm(sig, code, scp, addr)
1010 struct sigcontext *scp;
1013 struct itimerval itv;
1014 struct callout *freep, *list, *last;
1016 MAINDEBUG((LOG_DEBUG, "Alarm"));
1018 if (callout == NULL)
1021 * Get the first scheduled timeout and any that were scheduled
1022 * for the same time as a list, and remove them all from callout
1025 list = last = callout;
1026 while (last->c_next != NULL && last->c_next->c_time == 0)
1027 last = last->c_next;
1028 callout = last->c_next;
1029 last->c_next = NULL;
1032 * Set a new itimer if there are more timeouts scheduled.
1035 itv.it_interval.tv_sec = itv.it_interval.tv_usec = 0;
1036 itv.it_value.tv_usec = 0;
1037 itv.it_value.tv_sec = callout->c_time;
1038 MAINDEBUG((LOG_DEBUG, "Setting itimer for %d seconds in alrm.",
1039 itv.it_value.tv_sec));
1040 if (setitimer(ITIMER_REAL, &itv, NULL)) {
1041 syslog(LOG_ERR, "setitimer(ITIMER_REAL): %m");
1044 if (gettimeofday(&schedtime, NULL)) {
1045 syslog(LOG_ERR, "gettimeofday: %m");
1051 * Now call all the timeout routines scheduled for this time.
1054 (*list->c_func)(list->c_arg);
1056 list = list->c_next;
1057 (void) free((char *) freep);
1064 * io - Catch SIGIO signal.
1066 * Indicates that incoming data is available.
1070 io(sig, code, scp, addr)
1072 struct sigcontext *scp;
1079 struct timeval notime;
1082 MAINDEBUG((LOG_DEBUG, "IO signal received"));
1083 adjtimeout(); /* Adjust timeouts */
1085 /* we do this to see if the SIGIO handler is being invoked for input */
1086 /* ready, or for the socket buffer hitting the low-water mark. */
1093 if ((ready = select(32, &fdset, (fd_set *) NULL, (fd_set *) NULL,
1095 syslog(LOG_ERR, "Error in io() select: %m");
1100 MAINDEBUG((LOG_DEBUG, "IO non-input ready SIGIO occured."));
1104 /* Yup, this is for real */
1105 for (;;) { /* Read all available packets */
1106 p = inpacket_buf; /* point to beginning of packet buffer */
1108 len = read_packet(inpacket_buf);
1113 syslog(LOG_WARNING, "End of file on fd!");
1117 if (debug /*&& (debugflags & DBG_INPACKET)*/)
1118 log_packet(p, len, "rcvd ");
1120 if (len < DLLHEADERLEN) {
1121 MAINDEBUG((LOG_INFO, "io(): Received short packet."));
1125 p += 2; /* Skip address and control */
1126 GETSHORT(protocol, p);
1127 len -= DLLHEADERLEN;
1130 * Toss all non-LCP packets unless LCP is OPEN.
1132 if (protocol != LCP && lcp_fsm[0].state != OPENED) {
1133 MAINDEBUG((LOG_INFO,
1134 "io(): Received non-LCP packet when LCP not open."));
1139 * Upcall the proper protocol input routine.
1141 for (i = 0; i < sizeof (prottbl) / sizeof (struct protent); i++)
1142 if (prottbl[i].protocol == protocol) {
1143 (*prottbl[i].input)(0, p, len);
1147 if (i == sizeof (prottbl) / sizeof (struct protent)) {
1148 syslog(LOG_WARNING, "input: Unknown protocol (%x) received!",
1150 lcp_sprotrej(0, p - DLLHEADERLEN, len + DLLHEADERLEN);
1156 * demuxprotrej - Demultiplex a Protocol-Reject.
1159 demuxprotrej(unit, protocol)
1166 * Upcall the proper Protocol-Reject routine.
1168 for (i = 0; i < sizeof (prottbl) / sizeof (struct protent); i++)
1169 if (prottbl[i].protocol == protocol) {
1170 (*prottbl[i].protrej)(unit);
1175 "demuxprotrej: Unrecognized Protocol-Reject for protocol %d!",
1181 * incdebug - Catch SIGUSR1 signal.
1183 * Increment debug flag.
1190 syslog(LOG_INFO, "Debug turned ON, Level %d", debug);
1191 setlogmask(LOG_UPTO(LOG_DEBUG));
1197 * nodebug - Catch SIGUSR2 signal.
1199 * Turn off debugging.
1206 setlogmask(LOG_UPTO(LOG_WARNING));
1212 * device_script - run a program to connect or disconnect the
1216 device_script(program, in, out)
1225 sigaddset(&mask, SIGINT);
1226 sigaddset(&mask, SIGHUP);
1227 sigprocmask(SIG_BLOCK, &mask, &mask);
1232 syslog(LOG_ERR, "fork: %m");
1237 setreuid(getuid(), getuid());
1238 setregid(getgid(), getgid());
1239 sigprocmask(SIG_SETMASK, &mask, NULL);
1242 execl("/bin/sh", "sh", "-c", program, (char *)0);
1243 syslog(LOG_ERR, "could not exec /bin/sh: %m");
1248 while (waitpid(pid, &status, 0) < 0) {
1251 syslog(LOG_ERR, "waiting for (dis)connection process: %m");
1254 sigprocmask(SIG_SETMASK, &mask, NULL);
1256 return (status == 0 ? 0 : -1);
1261 * run-program - execute a program with given arguments,
1262 * but don't wait for it.
1263 * If the program can't be executed, logs an error unless
1264 * must_exist is 0 and the program file doesn't exist.
1267 run_program(prog, args, must_exist)
1276 syslog(LOG_ERR, "can't fork to run %s: %m", prog);
1281 if (must_exist || errno != ENOENT)
1282 syslog(LOG_WARNING, "can't execute %s: %m", prog);
1285 MAINDEBUG((LOG_DEBUG, "Script %s started; pid = %d", prog, pid));
1292 * reap_kids - get status from any dead child processes,
1293 * and log a message for abnormal terminations.
1300 if (n_children == 0)
1302 if ((pid = waitpid(-1, &status, WNOHANG)) == -1) {
1303 if (errno != ECHILD)
1304 syslog(LOG_ERR, "waitpid: %m");
1309 if (WIFSIGNALED(status)) {
1310 syslog(LOG_WARNING, "child process %d terminated with signal %d",
1311 pid, WTERMSIG(status));
1318 * log_packet - format a packet and log it.
1321 char line[256]; /* line to be logged accumulated here */
1325 log_packet(p, len, prefix)
1330 strcpy(line, prefix);
1331 linep = line + strlen(line);
1332 format_packet(p, len, pr_log, NULL);
1334 syslog(LOG_DEBUG, "%s", line);
1338 * format_packet - make a readable representation of a packet,
1339 * calling `printer(arg, format, ...)' to output it.
1342 format_packet(p, len, printer, arg)
1345 void (*printer) __ARGS((void *, char *, ...));
1352 if (len >= DLLHEADERLEN && p[0] == ALLSTATIONS && p[1] == UI) {
1355 len -= DLLHEADERLEN;
1356 for (i = 0; i < N_PROTO; ++i)
1357 if (proto == prottbl[i].protocol)
1360 printer(arg, "[%s", prottbl[i].name);
1361 n = (*prottbl[i].printpkt)(p, len, printer, arg);
1366 printer(arg, "[proto=0x%x]", proto);
1370 for (; len > 0; --len) {
1372 printer(arg, " %.2x", x);
1380 pr_log(void *arg, char *fmt, ...)
1386 va_start(pvar, fmt);
1387 vsprintf(buf, fmt, pvar);
1391 if (linep + n + 1 > line + sizeof(line)) {
1392 syslog(LOG_DEBUG, "%s", line);
1399 #else /* __STDC__ */
1400 #include <varargs.h>
1403 pr_log(arg, fmt, va_alist)
1413 vsprintf(buf, fmt, pvar);
1417 if (linep + n + 1 > line + sizeof(line)) {
1418 syslog(LOG_DEBUG, "%s", line);
1427 * print_string - print a readable representation of a string using
1431 print_string(p, len, printer, arg)
1434 void (*printer) __ARGS((void *, char *, ...));
1440 for (; len > 0; --len) {
1442 if (' ' <= c && c <= '~')
1443 printer(arg, "%c", c);
1445 printer(arg, "\\%.3o", c);
1451 * novm - log an error message saying we ran out of memory, and die.
1457 syslog(LOG_ERR, "Virtual memory exhausted allocating %s\n", msg);