]> git.ozlabs.org Git - ppp.git/blob - pppd/sys-osf.c
*** empty log message ***
[ppp.git] / pppd / sys-osf.c
1 /*
2  * System-dependent procedures for pppd under Digital UNIX (OSF/1).
3  *
4  * Copyright (c) 1994 The Australian National University.
5  * All rights reserved.
6  *
7  * Permission to use, copy, modify, and distribute this software and its
8  * documentation is hereby granted, provided that the above copyright
9  * notice appears in all copies.  This software is provided without any
10  * warranty, express or implied. The Australian National University
11  * makes no representations about the suitability of this software for
12  * any purpose.
13  *
14  * IN NO EVENT SHALL THE AUSTRALIAN NATIONAL UNIVERSITY BE LIABLE TO ANY
15  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
16  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
17  * THE AUSTRALIAN NATIONAL UNIVERSITY HAVE BEEN ADVISED OF THE POSSIBILITY
18  * OF SUCH DAMAGE.
19  *
20  * THE AUSTRALIAN NATIONAL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
21  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
23  * ON AN "AS IS" BASIS, AND THE AUSTRALIAN NATIONAL UNIVERSITY HAS NO
24  * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
25  * OR MODIFICATIONS.
26  */
27
28 #ifndef lint
29 static char rcsid[] = "$Id: sys-osf.c,v 1.12 1997/04/30 05:58:44 paulus Exp $";
30 #endif
31
32 #include <stdio.h>
33 #include <stddef.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <ctype.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <unistd.h>
40 #include <termios.h>
41 #include <signal.h>
42 #include <malloc.h>
43 #include <utmp.h>
44 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <sys/socket.h>
47 #include <sys/stream.h>
48 #include <sys/stropts.h>
49 #include <sys/syslog.h>
50 #include <sys/stat.h>
51 #include <sys/time.h>
52 #include <sys/poll.h>
53 #include <net/if.h>
54 #include <net/if_arp.h>
55 #include <net/route.h>
56 #include <net/ppp_defs.h>
57 #include <net/pppio.h>
58 #include <netinet/in.h>
59
60 #include "pppd.h"
61
62 static int      pppfd;
63 static int      fdmuxid = -1;
64 static int      iffd;
65 static int      sockfd;
66
67 static int      restore_term;
68 static struct termios inittermios;
69 static struct winsize wsinfo;   /* Initial window size info */
70 static pid_t    tty_sid;        /* PID of our session leader */
71
72 extern u_char   inpacket_buf[]; /* borrowed from main.c */
73
74 static int      link_mtu, link_mru;
75
76 #define NMODULES        32
77 static int      tty_nmodules;
78 static char     tty_modules[NMODULES][FMNAMESZ+1];
79
80 static int closed_stdio;
81 static int initfdflags = -1;
82 static int orig_ttyfd = -1;
83
84 static int      if_is_up;       /* Interface has been marked up */
85 static u_int32_t ifaddrs[2];    /* local and remote addresses */
86 static u_int32_t default_route_gateway; /* Gateway for default route added */
87 static u_int32_t proxy_arp_addr;        /* Addr for proxy arp entry added */
88
89 /* Prototypes for procedures local to this file. */
90 static int translate_speed __P((int));
91 static int baud_rate_of __P((int));
92 static int get_ether_addr __P((u_int32_t, struct sockaddr *));
93 static int strioctl __P((int, int, void *, int, int));
94
95
96 /*
97  * sys_init - System-dependent initialization.
98  */
99 void
100 sys_init()
101 {
102     int x;
103
104     openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
105     setlogmask(LOG_UPTO(LOG_INFO));
106     if (debug)
107         setlogmask(LOG_UPTO(LOG_DEBUG));
108
109     /* Get an internet socket for doing socket ioctl's on. */
110     if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
111         syslog(LOG_ERR, "Couldn't create IP socket: %m");
112         die(1);
113     }
114
115     if (default_device)
116         tty_sid = getsid((pid_t)0);
117
118     /*
119      * Open the ppp device.
120      */
121     pppfd = open("/dev/streams/ppp", O_RDWR | O_NONBLOCK, 0);
122     if (pppfd < 0) {
123         syslog(LOG_ERR, "Can't open /dev/streams/ppp: %m");
124         die(1);
125     }
126     if (kdebugflag) {
127         x = PPPDBG_LOG + PPPDBG_DRIVER;
128         strioctl(pppfd, PPPIO_DEBUG, &x, sizeof(int), 0);
129     }
130
131     /* Assign a new PPA and get its unit number. */
132     if (strioctl(pppfd, PPPIO_NEWPPA, &ifunit, 0, sizeof(int)) < 0) {
133         syslog(LOG_ERR, "Can't create new PPP interface: %m");
134         die(1);
135     }
136
137     /*
138      * Open the ppp device again and push the if_ppp module on it.
139      */
140     iffd = open("/dev/streams/ppp", O_RDWR, 0);
141     if (iffd < 0) {
142         syslog(LOG_ERR, "Can't open /dev/streams/ppp (2): %m");
143         die(1);
144     }
145     if (kdebugflag) {
146         x = PPPDBG_LOG + PPPDBG_DRIVER;
147         strioctl(iffd, PPPIO_DEBUG, &x, sizeof(int), 0);
148     }
149     if (strioctl(iffd, PPPIO_ATTACH, &ifunit, sizeof(int), 0) < 0) {
150         syslog(LOG_ERR, "Couldn't attach ppp interface to device: %m");
151         die(1);
152     }
153     if (ioctl(iffd, I_PUSH, "if_ppp") < 0) {
154         syslog(LOG_ERR, "Can't push ppp interface module: %m");
155         die(1);
156     }
157     if (kdebugflag) {
158         x = PPPDBG_LOG + PPPDBG_IF;
159         strioctl(iffd, PPPIO_DEBUG, &x, sizeof(int), 0);
160     }
161     if (strioctl(iffd, PPPIO_NEWPPA, &ifunit, sizeof(int), 0) < 0) {
162         syslog(LOG_ERR, "Couldn't create ppp interface unit: %m");
163         die(1);
164     }
165     x = PPP_IP;
166     if (strioctl(iffd, PPPIO_BIND, &x, sizeof(int), 0) < 0) {
167         syslog(LOG_ERR, "Couldn't bind ppp interface to IP SAP: %m");
168         die(1);
169     }
170 }
171
172 /*
173  * sys_cleanup - restore any system state we modified before exiting:
174  * mark the interface down, delete default route and/or proxy arp entry.
175  * This shouldn't call die() because it's called from die().
176  */
177 void
178 sys_cleanup()
179 {
180     if (if_is_up)
181         sifdown(0);
182     if (ifaddrs[0])
183         cifaddr(0, ifaddrs[0], ifaddrs[1]);
184     if (default_route_gateway)
185         cifdefaultroute(0, 0, default_route_gateway);
186     if (proxy_arp_addr)
187         cifproxyarp(0, proxy_arp_addr);
188 }
189
190 /*
191  * sys_close - Clean up in a child process before execing.
192  */
193 void
194 sys_close()
195 {
196     close(iffd);
197     close(pppfd);
198     close(sockfd);
199     closelog();
200 }
201
202 /*
203  * sys_check_options - check the options that the user specified
204  */
205 void
206 sys_check_options()
207 {
208 }
209
210
211 /*
212  * daemon - Detach us from controlling terminal session.
213  */
214 int
215 daemon(nochdir, noclose)
216     int nochdir, noclose;
217 {
218     int pid;
219
220     if ((pid = fork()) < 0)
221         return -1;
222     if (pid != 0)
223         exit(0);                /* parent dies */
224     setsid();
225     if (!nochdir)
226         chdir("/");
227     if (!noclose) {
228         fclose(stdin);          /* don't need stdin, stdout, stderr */
229         fclose(stdout);
230         fclose(stderr);
231     }
232     return 0;
233 }
234
235 /*
236  * note_debug_level - note a change in the debug level.
237  */
238 void
239 note_debug_level()
240 {
241     if (debug) {
242         setlogmask(LOG_UPTO(LOG_DEBUG));
243     } else {
244         setlogmask(LOG_UPTO(LOG_WARNING));
245     }
246 }
247
248 /*
249  * ppp_available - check whether the system has any ppp interfaces
250  */
251 int
252 ppp_available()
253 {
254     struct stat buf;
255
256     return stat("/dev/streams/ppp", &buf) >= 0;
257 }
258
259 char pipename[] = "/dev/streams/pipe";
260
261 /*
262  *  streampipe -- Opens a STREAMS based pipe.  Used by streamify().
263  */
264
265 int 
266 streampipe(int fd[2])
267 {
268     if ((fd[0]=open(pipename, O_RDWR)) == -1)
269         return(-1);
270     else if ((fd[1]=open(pipename, O_RDWR)) == -1) {
271         close(fd[0]);
272         return(-1);
273     } else if (ioctl(fd[0], I_PIPE, fd[1]) != 0) {
274         close(fd[0]);
275         close(fd[1]);
276         return(-1);
277     } else {
278         return(ioctl(fd[0], I_PUSH, "pipemod"));
279     }
280 }
281
282 /*
283  *  streamify -- Needed for Digital UNIX, since some tty devices are not STREAMS
284  *               modules (but ptys are, and pipes can be).
285  */
286
287 #define BUFFSIZE 1000     /*  Size of buffer for streamify()  */
288
289 int 
290 streamify(int fd)
291 {
292     int fdes[2];
293     fd_set readfds;
294     int ret, fret, rret, maxfd;
295     static char buffer[BUFFSIZE];
296     struct sigaction sa;
297
298     if (streampipe(fdes) != 0)
299         syslog(LOG_ERR, "streampipe(): %m\n");
300     else if (isastream(fdes[0]) == 1) {
301         if ((fret=fork()) < 0) {
302             syslog(LOG_ERR, "fork(): %m\n");
303         } else if (fret == 0) {
304             /*  Process to forward things from pipe to tty  */
305             sigemptyset(&(sa.sa_mask));
306             sa.sa_handler = SIG_DFL;
307             sa.sa_flags = 0;
308             sigaction(SIGHUP, &sa, NULL);   /*  Go back to default actions */
309             sigaction(SIGINT, &sa, NULL);   /*  for changed signals.  */
310             sigaction(SIGTERM, &sa, NULL);
311             sigaction(SIGCHLD, &sa, NULL);
312             sigaction(SIGUSR1, &sa, NULL);
313             sigaction(SIGUSR2, &sa, NULL);
314             close(fdes[0]);
315
316             maxfd = (fdes[1]>fd)?fdes[1]:fd;
317             while (1) {
318                 FD_ZERO(&readfds);
319                 FD_SET(fdes[1], &readfds);
320                 FD_SET(fd, &readfds);
321                 ret = select(maxfd+1, &readfds, NULL, NULL, NULL);
322                 if (FD_ISSET(fd, &readfds)) {
323                     rret = read(fd, buffer, BUFFSIZE);
324                     if (rret == 0) {
325                         MAINDEBUG((LOG_DEBUG, "slave died:  EOF on tty."));
326                         exit(0);
327                     } else {
328                         write(fdes[1], buffer, rret);
329                     }
330                 }
331                 if (FD_ISSET(fdes[1], &readfds)) {
332                     rret = read(fdes[1], buffer, BUFFSIZE);
333                     if (rret == 0) {
334                         MAINDEBUG((LOG_DEBUG, "slave died:  EOF on pipe."));
335                         exit(0);
336                     } else {
337                         write(fd, buffer, rret);
338                     }
339                 }
340             }
341         } else {
342             close(fdes[1]);
343             orig_ttyfd = fd;
344             return(fdes[0]);
345         }
346     }
347
348     return(-1);
349 }
350
351 /*
352  * establish_ppp - Turn the serial port into a ppp interface.
353  */
354 void
355 establish_ppp(fd)
356     int fd;
357 {
358     int i;
359
360     if (isastream(fd) != 1) {
361         if ((ttyfd = fd = streamify(fd)) < 0) {
362             syslog(LOG_ERR, "Couldn't get a STREAMS module!\n");
363             die(1);
364         }
365     }
366
367     /* Pop any existing modules off the tty stream. */
368     for (i = 0;; ++i) {
369         if (ioctl(fd, I_LOOK, tty_modules[i]) < 0
370             || ioctl(fd, I_POP, 0) < 0)
371             break;
372         syslog(LOG_ERR, "popping module %s\n", tty_modules[i]);
373     }
374
375     tty_nmodules = i;
376
377     /* Push the async hdlc module and the compressor module. */
378     if (ioctl(fd, I_PUSH, "ppp_ahdl") < 0) {
379         syslog(LOG_ERR, "Couldn't push PPP Async HDLC module: %m");
380         die(1);
381     }
382     if (ioctl(fd, I_PUSH, "ppp_comp") < 0) {
383         syslog(LOG_ERR, "Couldn't push PPP compression module: %m");
384 /*      die(1); */
385     }
386
387     /* read mode, message non-discard mode */
388     if (ioctl(fd, I_SRDOPT, RMSGN|RPROTNORM) < 0) {
389         syslog(LOG_ERR, "ioctl(I_SRDOPT, RMSGN): %m");
390         die(1);
391     }
392
393     /* Link the serial port under the PPP multiplexor. */
394     if ((fdmuxid = ioctl(pppfd, I_LINK, fd)) < 0) {
395         syslog(LOG_ERR, "Can't link tty to PPP mux: %m");
396         die(1);
397     }
398
399     /* close stdin, stdout, stderr if they might refer to the device */
400     if (default_device && !closed_stdio) {
401         int i;
402
403         for (i = 0; i <= 2; ++i)
404             if (i != fd && i != sockfd)
405                 close(i);
406         closed_stdio = 1;
407     }
408
409     /*
410      * Set device for non-blocking reads.
411      */
412     if ((initfdflags = fcntl(fd, F_GETFL)) == -1
413         || fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
414         syslog(LOG_WARNING, "Couldn't set device to non-blocking mode: %m");
415     }
416 }
417
418 /*
419  * restore_loop - reattach the ppp unit to the loopback.
420  * This doesn't need to do anything because disestablish_ppp does it.
421  */
422 void
423 restore_loop()
424 {
425 }
426
427 /*
428  * disestablish_ppp - Restore the serial port to normal operation.
429  * It attempts to reconstruct the stream with the previously popped
430  * modules.  This shouldn't call die() because it's called from die().
431  */
432 void
433 disestablish_ppp(fd)
434     int fd;
435 {
436     int i;
437
438     if (fdmuxid >= 0) {
439         if (ioctl(pppfd, I_UNLINK, fdmuxid) < 0) {
440             if (!hungup)
441                 syslog(LOG_ERR, "Can't unlink tty from PPP mux: %m");
442         }
443         fdmuxid = -1;
444
445         /* Reset non-blocking mode on the file descriptor. */
446         if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
447             syslog(LOG_WARNING, "Couldn't restore device fd flags: %m");
448         initfdflags = -1;
449
450         if (!hungup) {
451             while (ioctl(fd, I_POP, 0) >= 0)
452                 ;
453             for (i = tty_nmodules - 1; i >= 0; --i)
454                 if (ioctl(fd, I_PUSH, tty_modules[i]) < 0)
455                     syslog(LOG_ERR, "Couldn't restore tty module %s: %m",
456                            tty_modules[i]);
457         }
458
459         if (hungup && default_device && tty_sid > 0) {
460             /*
461              * If we have received a hangup, we need to send a SIGHUP
462              * to the terminal's controlling process.  The reason is
463              * that the original stream head for the terminal hasn't
464              * seen the M_HANGUP message (it went up through the ppp
465              * driver to the stream head for our fd to /dev/ppp).
466              */
467             syslog(LOG_DEBUG, "sending hangup to %d", tty_sid);
468             if (kill(tty_sid, SIGHUP) < 0)
469                 syslog(LOG_ERR, "couldn't kill pgrp: %m");
470         }
471         if (orig_ttyfd >= 0) {
472             close(fd);
473             (void)wait((void *)0);
474             ttyfd = orig_ttyfd;
475             orig_ttyfd = -1;
476         }
477     }
478 }
479
480 /*
481  * Check whether the link seems not to be 8-bit clean.
482  */
483 void
484 clean_check()
485 {
486     int x;
487     char *s;
488
489     if (strioctl(pppfd, PPPIO_GCLEAN, &x, 0, sizeof(x)) < 0)
490         return;
491     s = NULL;
492     switch (~x) {
493     case RCV_B7_0:
494         s = "bit 7 set to 1";
495         break;
496     case RCV_B7_1:
497         s = "bit 7 set to 0";
498         break;
499     case RCV_EVNP:
500         s = "odd parity";
501         break;
502     case RCV_ODDP:
503         s = "even parity";
504         break;
505     }
506     if (s != NULL) {
507         syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
508         syslog(LOG_WARNING, "All received characters had %s", s);
509     }
510 }
511
512 /*
513  * List of valid speeds.
514  */
515 struct speed {
516     int speed_int, speed_val;
517 } speeds[] = {
518 #ifdef B50
519     { 50, B50 },
520 #endif
521 #ifdef B75
522     { 75, B75 },
523 #endif
524 #ifdef B110
525     { 110, B110 },
526 #endif
527 #ifdef B134
528     { 134, B134 },
529 #endif
530 #ifdef B150
531     { 150, B150 },
532 #endif
533 #ifdef B200
534     { 200, B200 },
535 #endif
536 #ifdef B300
537     { 300, B300 },
538 #endif
539 #ifdef B600
540     { 600, B600 },
541 #endif
542 #ifdef B1200
543     { 1200, B1200 },
544 #endif
545 #ifdef B1800
546     { 1800, B1800 },
547 #endif
548 #ifdef B2000
549     { 2000, B2000 },
550 #endif
551 #ifdef B2400
552     { 2400, B2400 },
553 #endif
554 #ifdef B3600
555     { 3600, B3600 },
556 #endif
557 #ifdef B4800
558     { 4800, B4800 },
559 #endif
560 #ifdef B7200
561     { 7200, B7200 },
562 #endif
563 #ifdef B9600
564     { 9600, B9600 },
565 #endif
566 #ifdef B19200
567     { 19200, B19200 },
568 #endif
569 #ifdef B38400
570     { 38400, B38400 },
571 #endif
572 #ifdef EXTA
573     { 19200, EXTA },
574 #endif
575 #ifdef EXTB
576     { 38400, EXTB },
577 #endif
578 #ifdef B57600
579     { 57600, B57600 },
580 #endif
581 #ifdef B115200
582     { 115200, B115200 },
583 #endif
584     { 0, 0 }
585 };
586
587 /*
588  * Translate from bits/second to a speed_t.
589  */
590 static int
591 translate_speed(bps)
592     int bps;
593 {
594     struct speed *speedp;
595
596     if (bps == 0)
597         return 0;
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);
602     return 0;
603 }
604
605 /*
606  * Translate from a speed_t to bits/second.
607  */
608 static int
609 baud_rate_of(speed)
610     int speed;
611 {
612     struct speed *speedp;
613
614     if (speed == 0)
615         return 0;
616     for (speedp = speeds; speedp->speed_int; speedp++)
617         if (speed == speedp->speed_val)
618             return speedp->speed_int;
619     return 0;
620 }
621
622 /*
623  * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
624  * at the requested speed, etc.  If `local' is true, set CLOCAL
625  * regardless of whether the modem option was specified.
626  */
627 void
628 set_up_tty(fd, local)
629     int fd, local;
630 {
631     int speed;
632     struct termios tios;
633
634     if (tcgetattr(fd, &tios) < 0) {
635         syslog(LOG_ERR, "tcgetattr: %m");
636         die(1);
637     }
638
639     if (!restore_term) {
640         inittermios = tios;
641         ioctl(fd, TIOCGWINSZ, &wsinfo);
642     }
643
644     tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
645     if (crtscts > 0)
646         tios.c_cflag |= CRTSCTS;
647     else if (crtscts < 0)
648         tios.c_cflag &= ~CRTSCTS;
649
650     tios.c_cflag |= CS8 | CREAD | HUPCL;
651     if (local || !modem)
652         tios.c_cflag |= CLOCAL;
653     tios.c_iflag = IGNBRK | IGNPAR;
654     tios.c_oflag = 0;
655     tios.c_lflag = 0;
656     tios.c_cc[VMIN] = 1;
657     tios.c_cc[VTIME] = 0;
658
659     if (crtscts == -2) {
660         tios.c_iflag |= IXON | IXOFF;
661         tios.c_cc[VSTOP] = 0x13;        /* DC3 = XOFF = ^S */
662         tios.c_cc[VSTART] = 0x11;       /* DC1 = XON  = ^Q */
663     }
664
665     speed = translate_speed(inspeed);
666     if (speed) {
667         cfsetospeed(&tios, speed);
668         cfsetispeed(&tios, speed);
669     } else {
670         speed = cfgetospeed(&tios);
671         /*
672          * We can't proceed if the serial port speed is 0,
673          * since that implies that the serial port is disabled.
674          */
675         if (speed == B0) {
676             syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
677                    devnam);
678             die(1);
679         }
680     }
681
682     if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
683         syslog(LOG_ERR, "tcsetattr: %m");
684         die(1);
685     }
686
687     baud_rate = inspeed = baud_rate_of(speed);
688     restore_term = 1;
689 }
690
691 /*
692  * restore_tty - restore the terminal to the saved settings.
693  */
694 void
695 restore_tty(fd)
696     int fd;
697 {
698     if (restore_term) {
699         if (!default_device) {
700             /*
701              * Turn off echoing, because otherwise we can get into
702              * a loop with the tty and the modem echoing to each other.
703              * We presume we are the sole user of this tty device, so
704              * when we close it, it will revert to its defaults anyway.
705              */
706             inittermios.c_lflag &= ~(ECHO | ECHONL);
707         }
708         if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
709             if (!hungup && errno != ENXIO)
710                 syslog(LOG_WARNING, "tcsetattr: %m");
711         ioctl(fd, TIOCSWINSZ, &wsinfo);
712         restore_term = 0;
713     }
714 }
715
716 /*
717  * setdtr - control the DTR line on the serial port.
718  * This is called from die(), so it shouldn't call die().
719  */
720 void
721 setdtr(fd, on)
722 int fd, on;
723 {
724     int modembits = TIOCM_DTR;
725
726     ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
727 }
728
729 /*
730  * open_loopback - open the device we use for getting packets
731  * in demand mode.  Under Solaris 2, we use our existing fd
732  * to the ppp driver.
733  */
734 void
735 open_ppp_loopback()
736 {
737 }
738
739 /*
740  * output - Output PPP packet.
741  */
742 void
743 output(unit, p, len)
744     int unit;
745     u_char *p;
746     int len;
747 {
748     struct strbuf data;
749     int retries;
750     struct pollfd pfd;
751
752     if (debug)
753         log_packet(p, len, "sent ", LOG_DEBUG);
754
755     data.len = len;
756     data.buf = (caddr_t) p;
757     retries = 4;
758     while (putmsg(pppfd, NULL, &data, 0) < 0) {
759         if (--retries < 0 || (errno != EWOULDBLOCK && errno != EAGAIN)) {
760             if (errno != ENXIO)
761                 syslog(LOG_ERR, "Couldn't send packet: %m");
762             break;
763         }
764         pfd.fd = pppfd;
765         pfd.events = POLLOUT;
766         poll(&pfd, 1, 250);     /* wait for up to 0.25 seconds */
767     }
768 }
769
770
771 /*
772  * wait_input - wait until there is data available on fd,
773  * for the length of time specified by *timo (indefinite
774  * if timo is NULL).
775  */
776 void
777 wait_input(timo)
778     struct timeval *timo;
779 {
780     int t;
781     struct pollfd pfd;
782
783     t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
784     pfd.fd = pppfd;
785     pfd.events = POLLIN | POLLPRI | POLLHUP;
786     if (poll(&pfd, 1, t) < 0 && errno != EINTR) {
787         syslog(LOG_ERR, "poll: %m");
788         die(1);
789     }
790 }
791
792 /*
793  * wait_loop_output - wait until there is data available on the
794  * loopback, for the length of time specified by *timo (indefinite
795  * if timo is NULL).
796  */
797 void
798 wait_loop_output(timo)
799     struct timeval *timo;
800 {
801     wait_input(timo);
802 }
803
804 /*
805  * wait_time - wait for a given length of time or until a
806  * signal is received.
807  */
808 void
809 wait_time(timo)
810     struct timeval *timo;
811 {
812     int n;
813
814     n = select(0, NULL, NULL, NULL, timo);
815     if (n < 0 && errno != EINTR) {
816         syslog(LOG_ERR, "select: %m");
817         die(1);
818     }
819 }
820
821
822 /*
823  * read_packet - get a PPP packet from the serial device.
824  */
825 int
826 read_packet(buf)
827     u_char *buf;
828 {
829     struct strbuf ctrl, data;
830     int flags, len;
831     unsigned char ctrlbuf[64];
832
833     for (;;) {
834         data.maxlen = PPP_MRU + PPP_HDRLEN;
835         data.buf = (caddr_t) buf;
836         ctrl.maxlen = sizeof(ctrlbuf);
837         ctrl.buf = (caddr_t) ctrlbuf;
838         flags = 0;
839         len = getmsg(pppfd, &ctrl, &data, &flags);
840         if (len < 0) {
841             if (errno = EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
842                 return -1;
843             syslog(LOG_ERR, "Error reading packet: %m");
844             die(1);
845         }
846
847         if (ctrl.len <= 0)
848             return data.len;
849
850         /*
851          * Got a M_PROTO or M_PCPROTO message.  Huh?
852          */
853         if (debug)
854             syslog(LOG_DEBUG, "got ctrl msg len=%d", ctrl.len);
855
856     }
857 }
858
859 /*
860  * get_loop_output - get outgoing packets from the ppp device,
861  * and detect when we want to bring the real link up.
862  * Return value is 1 if we need to bring up the link, 0 otherwise.
863  */
864 int
865 get_loop_output()
866 {
867     int len;
868     int rv = 0;
869
870     while ((len = read_packet(inpacket_buf)) > 0) {
871         if (loop_frame(inpacket_buf, len))
872             rv = 1;
873     }
874     return rv;
875 }
876
877 /*
878  * ppp_send_config - configure the transmit characteristics of
879  * the ppp interface.
880  */
881 void
882 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
883     int unit, mtu;
884     u_int32_t asyncmap;
885     int pcomp, accomp;
886 {
887     int cf[2];
888
889     link_mtu = mtu;
890     if (strioctl(pppfd, PPPIO_MTU, &mtu, sizeof(mtu), 0) < 0) {
891         if (hungup && errno == ENXIO)
892             return;
893         syslog(LOG_ERR, "Couldn't set MTU: %m");
894     }
895     if (strioctl(pppfd, PPPIO_XACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
896         syslog(LOG_ERR, "Couldn't set transmit ACCM: %m");
897     }
898     cf[0] = (pcomp? COMP_PROT: 0) + (accomp? COMP_AC: 0);
899     cf[1] = COMP_PROT | COMP_AC;
900     if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
901         syslog(LOG_ERR, "Couldn't set prot/AC compression: %m");
902     }
903 }
904
905 /*
906  * ppp_set_xaccm - set the extended transmit ACCM for the interface.
907  */
908 void
909 ppp_set_xaccm(unit, accm)
910     int unit;
911     ext_accm accm;
912 {
913     if (strioctl(pppfd, PPPIO_XACCM, accm, sizeof(ext_accm), 0) < 0) {
914         if (!hungup || errno != ENXIO)
915             syslog(LOG_WARNING, "Couldn't set extended ACCM: %m");
916     }
917 }
918
919 /*
920  * ppp_recv_config - configure the receive-side characteristics of
921  * the ppp interface.
922  */
923 void
924 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
925     int unit, mru;
926     u_int32_t asyncmap;
927     int pcomp, accomp;
928 {
929     int cf[2];
930
931     link_mru = mru;
932     if (strioctl(pppfd, PPPIO_MRU, &mru, sizeof(mru), 0) < 0) {
933         if (hungup && errno == ENXIO)
934             return;
935         syslog(LOG_ERR, "Couldn't set MRU: %m");
936     }
937     if (strioctl(pppfd, PPPIO_RACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
938         syslog(LOG_ERR, "Couldn't set receive ACCM: %m");
939     }
940     cf[0] = (pcomp? DECOMP_PROT: 0) + (accomp? DECOMP_AC: 0);
941     cf[1] = DECOMP_PROT | DECOMP_AC;
942     if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
943         syslog(LOG_ERR, "Couldn't set prot/AC decompression: %m");
944     }
945 }
946
947 /*
948  * ccp_test - ask kernel whether a given compression method
949  * is acceptable for use.
950  *
951  * In Digital UNIX the memory buckets for chunks >16K are not
952  * primed when the system comes up.  That means we're not
953  * likely to get the memory needed for the compressor on
954  * the first try.  The way we work around this is to have
955  * the driver spin off a thread to go get the memory for us
956  * (we can't block at that point in a streams context.)
957  *
958  * This code synchronizes with the thread when it has returned
959  * with the memory we need.  The driver will continue to return
960  * with EAGAIN until the thread comes back.  We give up here
961  * if after 10 attempts in one second we still don't have memory.
962  * It's up to the driver to not lose track of that memory if
963  * thread takes too long to return.
964  */
965 int
966 ccp_test(unit, opt_ptr, opt_len, for_transmit)
967     int unit, opt_len, for_transmit;
968     u_char *opt_ptr;
969 {
970     struct timeval tval;
971     int i;
972
973     tval.tv_sec = 0;
974     tval.tv_usec = 100000;
975     for (i = 0; i < 10; ++i) {
976         if (strioctl(pppfd, (for_transmit? PPPIO_XCOMP: PPPIO_RCOMP),
977             opt_ptr, opt_len, 0) >= 0) {
978             return 1;
979         }
980         if (errno != EAGAIN)
981             break;
982         wait_time(&tval);
983     }
984     if (errno != 0)
985         syslog(LOG_ERR, "hard failure trying to get memory for a compressor: %m");
986     return (errno == ENOSR)? 0: -1;
987 }
988
989 /*
990  * ccp_flags_set - inform kernel about the current state of CCP.
991  */
992 void
993 ccp_flags_set(unit, isopen, isup)
994     int unit, isopen, isup;
995 {
996     int cf[2];
997
998     cf[0] = (isopen? CCP_ISOPEN: 0) + (isup? CCP_ISUP: 0);
999     cf[1] = CCP_ISOPEN | CCP_ISUP | CCP_ERROR | CCP_FATALERROR;
1000     if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
1001         if (!hungup || errno != ENXIO)
1002             syslog(LOG_ERR, "Couldn't set kernel CCP state: %m");
1003     }
1004 }
1005
1006 /*
1007  * get_idle_time - return how long the link has been idle.
1008  */
1009 int
1010 get_idle_time(u, ip)
1011     int u;
1012     struct ppp_idle *ip;
1013 {
1014     return strioctl(pppfd, PPPIO_GIDLE, ip, 0, sizeof(struct ppp_idle)) >= 0;
1015 }
1016
1017
1018 /*
1019  * ccp_fatal_error - returns 1 if decompression was disabled as a
1020  * result of an error detected after decompression of a packet,
1021  * 0 otherwise.  This is necessary because of patent nonsense.
1022  */
1023 int
1024 ccp_fatal_error(unit)
1025     int unit;
1026 {
1027     int cf[2];
1028
1029     cf[0] = cf[1] = 0;
1030     if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
1031         if (errno != ENXIO && errno != EINVAL)
1032             syslog(LOG_ERR, "Couldn't get compression flags: %m");
1033         return 0;
1034     }
1035     return cf[0] & CCP_FATALERROR;
1036 }
1037
1038 /*
1039  * sifvjcomp - config tcp header compression
1040  */
1041 int
1042 sifvjcomp(u, vjcomp, xcidcomp, xmaxcid)
1043     int u, vjcomp, xcidcomp, xmaxcid;
1044 {
1045     int cf[2];
1046     char maxcid[2];
1047
1048     if (vjcomp) {
1049         maxcid[0] = xcidcomp;
1050         maxcid[1] = 15;         /* XXX should be rmaxcid */
1051         if (strioctl(pppfd, PPPIO_VJINIT, maxcid, sizeof(maxcid), 0) < 0) {
1052             syslog(LOG_ERR, "Couldn't initialize VJ compression: %m");
1053         }
1054     }
1055
1056     cf[0] = (vjcomp? COMP_VJC + DECOMP_VJC: 0)  /* XXX this is wrong */
1057         + (xcidcomp? COMP_VJCCID + DECOMP_VJCCID: 0);
1058     cf[1] = COMP_VJC + DECOMP_VJC + COMP_VJCCID + DECOMP_VJCCID;
1059     if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
1060         if (vjcomp)
1061             syslog(LOG_ERR, "Couldn't enable VJ compression: %m");
1062     }
1063
1064     return 1;
1065 }
1066
1067 /*
1068  * sifup - Config the interface up and enable IP packets to pass.
1069  */
1070 int
1071 sifup(u)
1072     int u;
1073 {
1074     struct ifreq ifr;
1075
1076     strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1077     if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
1078         syslog(LOG_ERR, "Couldn't mark interface up (get): %m");
1079         return 0;
1080     }
1081     ifr.ifr_flags |= IFF_UP;
1082     if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
1083         syslog(LOG_ERR, "Couldn't mark interface up (set): %m");
1084         return 0;
1085     }
1086     if_is_up = 1;
1087     return 1;
1088 }
1089
1090 /*
1091  * sifdown - Config the interface down and disable IP.
1092  */
1093 int
1094 sifdown(u)
1095     int u;
1096 {
1097     struct ifreq ifr;
1098
1099     strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1100     if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
1101         syslog(LOG_ERR, "Couldn't mark interface down (get): %m");
1102         return 0;
1103     }
1104     if ((ifr.ifr_flags & IFF_UP) != 0) {
1105         ifr.ifr_flags &= ~IFF_UP;
1106         if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
1107             syslog(LOG_ERR, "Couldn't mark interface down (set): %m");
1108             return 0;
1109         }
1110     }
1111     if_is_up = 0;
1112     return 1;
1113 }
1114
1115 /*
1116  * sifnpmode - Set the mode for handling packets for a given NP.
1117  */
1118 int
1119 sifnpmode(u, proto, mode)
1120     int u;
1121     int proto;
1122     enum NPmode mode;
1123 {
1124     int npi[2];
1125
1126     npi[0] = proto;
1127     npi[1] = (int) mode;
1128     if (strioctl(pppfd, PPPIO_NPMODE, npi, 2 * sizeof(int), 0) < 0) {
1129         syslog(LOG_ERR, "ioctl(set NP %d mode to %d): %m", proto, mode);
1130         return 0;
1131     }
1132     return 1;
1133 }
1134
1135 #define INET_ADDR(x)    (((struct sockaddr_in *) &(x))->sin_addr.s_addr)
1136
1137 /*
1138  * SET_SA_FAMILY - initialize a struct sockaddr, setting the sa_family field.
1139  */
1140 #define SET_SA_FAMILY(addr, family)             \
1141     BZERO((char *) &(addr), sizeof(addr));      \
1142     addr.sa_family = (family);                  \
1143     addr.sa_len = sizeof ((addr))
1144
1145 /*
1146  * sifaddr - Config the interface IP addresses and netmask.
1147  */
1148 int
1149 sifaddr(u, o, h, m)
1150     int u;
1151     u_int32_t o, h, m;
1152 {
1153     struct ifreq ifr;
1154     struct ifaliasreq addreq;
1155     int ret;
1156
1157     ret = 1;
1158
1159     /* flush old address, if any
1160      */
1161     bzero(&ifr, sizeof (ifr));
1162     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
1163     SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
1164     ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
1165     if ((ioctl(sockfd, (int)SIOCDIFADDR, (caddr_t) &ifr) < 0)
1166         && errno != EADDRNOTAVAIL) {
1167         syslog(LOG_ERR, "ioctl(SIOCDIFADDR): %m");
1168         ret = 0;
1169     }
1170
1171     bzero(&addreq, sizeof (addreq));
1172     strncpy(addreq.ifra_name, ifname, sizeof (addreq.ifra_name));
1173     SET_SA_FAMILY(addreq.ifra_addr, AF_INET);
1174     SET_SA_FAMILY(addreq.ifra_broadaddr, AF_INET);
1175     ((struct sockaddr_in *)&addreq.ifra_addr)->sin_addr.s_addr = o;
1176     ((struct sockaddr_in *)&addreq.ifra_broadaddr)->sin_addr.s_addr = h;
1177
1178     if (m != 0) {
1179         ((struct sockaddr_in *)&addreq.ifra_mask)->sin_addr.s_addr = m;
1180         addreq.ifra_mask.sa_len = sizeof (struct sockaddr);
1181         syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
1182     }
1183
1184     /* install new src/dst and (possibly) netmask
1185      */
1186     if (ioctl(sockfd, SIOCPIFADDR, &addreq) < 0) {
1187         syslog(LOG_ERR, "ioctl(SIOCPIFADDR): %m");
1188         ret = 0;
1189     }
1190
1191     ifr.ifr_metric = link_mtu;
1192     if (ioctl(sockfd, SIOCSIPMTU, &ifr) < 0) {
1193         syslog(LOG_ERR, "Couldn't set IP MTU: %m");
1194         ret = 0;
1195     }
1196
1197     ifaddrs[0] = o;
1198     ifaddrs[1] = h;
1199     return (ret);
1200 }
1201
1202
1203 /*
1204  * cifaddr - Clear the interface IP addresses, and delete routes
1205  * through the interface if possible.
1206  */
1207 int
1208 cifaddr(u, o, h)
1209     int u;
1210     u_int32_t o, h;
1211 {
1212     struct ifreq ifr;
1213
1214     ifaddrs[0] = 0;
1215     bzero(&ifr, sizeof (ifr));
1216     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
1217     SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
1218     ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
1219     if (ioctl(sockfd, (int)SIOCDIFADDR, (caddr_t) &ifr) < 0) {
1220         syslog(LOG_ERR, "ioctl(SIOCDIFADDR): %m");
1221         return 0;
1222     }
1223     return 1;
1224 }
1225
1226
1227 /*
1228  * sifdefaultroute - assign a default route through the address given.
1229  */
1230 int
1231 sifdefaultroute(u, l, g)
1232     int u;
1233     u_int32_t l, g;
1234 {
1235     struct ortentry rt;
1236
1237     BZERO(&rt, sizeof(rt));
1238     SET_SA_FAMILY(rt.rt_dst, AF_INET);
1239     SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1240     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
1241     rt.rt_flags = RTF_GATEWAY;
1242     if (ioctl(sockfd, (int)SIOCADDRT, &rt) < 0) {
1243         syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
1244         return 0;
1245     }
1246     default_route_gateway = g;
1247     return 1;
1248 }
1249
1250
1251 /*
1252  * cifdefaultroute - delete a default route through the address given.
1253  */
1254 int
1255 cifdefaultroute(u, l, g)
1256     int u;
1257     u_int32_t l, g;
1258 {
1259     struct ortentry rt;
1260
1261     BZERO(&rt, sizeof(rt));
1262     SET_SA_FAMILY(rt.rt_dst, AF_INET);
1263     SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1264     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
1265     rt.rt_flags = RTF_GATEWAY;
1266     if (ioctl(sockfd, (int)SIOCDELRT, &rt) < 0) {
1267         syslog(LOG_ERR, "default route ioctl(SIOCDELRT): %m");
1268         return 0;
1269     }
1270     default_route_gateway = 0;
1271     return 1;
1272 }
1273
1274 /*
1275  * sifproxyarp - Make a proxy ARP entry for the peer.
1276  */
1277 int
1278 sifproxyarp(unit, hisaddr)
1279     int unit;
1280     u_int32_t hisaddr;
1281 {
1282     struct arpreq arpreq;
1283
1284     BZERO(&arpreq, sizeof(arpreq));
1285
1286     /*
1287      * Get the hardware address of an interface on the same subnet
1288      * as our local address.
1289      */
1290     if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
1291         syslog(LOG_WARNING, "Cannot determine ethernet address for proxy ARP");
1292         return 0;
1293     }
1294
1295     SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1296     ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1297     arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1298     if (ioctl(sockfd, (int)SIOCSARP, (caddr_t)&arpreq) < 0) {
1299         syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
1300         return 0;
1301     }
1302
1303     proxy_arp_addr = hisaddr;
1304     return 1;
1305 }
1306
1307
1308 /*
1309  * cifproxyarp - Delete the proxy ARP entry for the peer.
1310  */
1311 int
1312 cifproxyarp(unit, hisaddr)
1313     int unit;
1314     u_int32_t hisaddr;
1315 {
1316     struct arpreq arpreq;
1317
1318     BZERO(&arpreq, sizeof(arpreq));
1319     SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1320     ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1321     if (ioctl(sockfd, (int)SIOCDARP, (caddr_t)&arpreq) < 0) {
1322         syslog(LOG_ERR, "ioctl(SIOCDARP): %m");
1323         return 0;
1324     }
1325     proxy_arp_addr = 0;
1326     return 1;
1327 }
1328
1329 /*
1330  * get_ether_addr - get the hardware address of an interface on the
1331  * the same subnet as ipaddr.
1332  */
1333 #define MAX_IFS         32
1334
1335 static int
1336 get_ether_addr(ipaddr, hwaddr)
1337     u_int32_t ipaddr;
1338     struct sockaddr *hwaddr;
1339 {
1340     struct ifreq *ifr, *ifend;
1341     u_int32_t ina, mask;
1342     struct ifreq ifreq;
1343     struct ifconf ifc;
1344     struct ifreq ifs[MAX_IFS];
1345     struct ifdevea ifdevreq;
1346
1347     ifc.ifc_len = sizeof(ifs);
1348     ifc.ifc_req = ifs;
1349     if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1350         syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
1351         return 0;
1352     }
1353
1354     /*
1355      * Scan through looking for an interface with an Internet
1356      * address on the same subnet as `ipaddr'.
1357      */
1358     ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1359     for (ifr = ifc.ifc_req; ifr < ifend; ifr++) {
1360         if (ifr->ifr_addr.sa_family == AF_INET) {
1361
1362             /*
1363              * Check that the interface is up, and not point-to-point
1364              * or loopback.
1365              */
1366             strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1367             if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1368                 continue;
1369             if ((ifreq.ifr_flags &
1370                  (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1371                  != (IFF_UP|IFF_BROADCAST))
1372                 continue;
1373
1374             /*
1375              * Get its netmask and check that it's on the right subnet.
1376              */
1377             if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1378                 continue;
1379             ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1380             mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1381             if ((ipaddr & mask) != (ina & mask))
1382                 continue;
1383
1384             break;
1385         } else {
1386             if (ifr->ifr_addr.sa_len > sizeof (ifr->ifr_addr))
1387                 ifr = (struct ifreq *)((caddr_t)ifr + (ifr->ifr_addr.sa_len - sizeof (ifr->ifr_addr)));
1388         }
1389     }
1390
1391     if (ifr >= ifend)
1392         return 0;
1393     syslog(LOG_INFO, "found interface %s for proxy arp", ifr->ifr_name);
1394
1395     strncpy(ifdevreq.ifr_name, ifr->ifr_name, sizeof(ifdevreq.ifr_name));
1396
1397     if (ioctl(sockfd, (int)SIOCRPHYSADDR, &ifdevreq) < 0) {
1398         perror("ioctl(SIOCRPHYSADDR)");
1399         return(0);
1400     }
1401
1402     hwaddr->sa_family = AF_UNSPEC;
1403     memcpy(hwaddr->sa_data, ifdevreq.current_pa, sizeof(ifdevreq.current_pa));
1404     return 1;
1405 }
1406
1407 #define WTMPFILE        "/usr/adm/wtmp"
1408
1409 void
1410 logwtmp(line, name, host)
1411     const char *line, *name, *host;
1412 {
1413     int fd;
1414     struct stat buf;
1415     struct utmp ut;
1416
1417     if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1418         return;
1419     if (!fstat(fd, &buf)) {
1420         (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1421         (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1422         (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1423         (void)time(&ut.ut_time);
1424         if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1425             (void)ftruncate(fd, buf.st_size);
1426     }
1427     close(fd);
1428 }
1429
1430 /*
1431  * Return user specified netmask, modified by any mask we might determine
1432  * for address `addr' (in network byte order).
1433  * Here we scan through the system's list of interfaces, looking for
1434  * any non-point-to-point interfaces which might appear to be on the same
1435  * network as `addr'.  If we find any, we OR in their netmask to the
1436  * user-specified netmask.
1437  */
1438 u_int32_t
1439 GetMask(addr)
1440     u_int32_t addr;
1441 {
1442     u_int32_t mask, nmask, ina;
1443     struct ifreq *ifr, *ifend, ifreq;
1444     struct ifconf ifc;
1445
1446     addr = ntohl(addr);
1447     if (IN_CLASSA(addr))        /* determine network mask for address class */
1448         nmask = IN_CLASSA_NET;
1449     else if (IN_CLASSB(addr))
1450         nmask = IN_CLASSB_NET;
1451     else
1452         nmask = IN_CLASSC_NET;
1453     /* class D nets are disallowed by bad_ip_adrs */
1454     mask = netmask | htonl(nmask);
1455
1456     /*
1457      * Scan through the system's network interfaces.
1458      */
1459     ifc.ifc_len = MAX_IFS * sizeof(struct ifreq);
1460     ifc.ifc_req = (struct ifreq *)alloca(ifc.ifc_len);
1461     if (ifc.ifc_req == 0)
1462         return mask;
1463     if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1464         syslog(LOG_WARNING, "Couldn't get system interface list: %m");
1465         return mask;
1466     }
1467     ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1468     for (ifr = ifc.ifc_req; ifr < ifend; ifr++) {
1469         /*
1470          * Check the interface's internet address.
1471          */
1472         if (ifr->ifr_addr.sa_family == AF_INET) {
1473             ina = INET_ADDR(ifr->ifr_addr);
1474             if ((ntohl(ina) & nmask) != (addr & nmask))
1475                 continue;
1476             /*
1477              * Check that the interface is up, and not point-to-point or loopback.
1478              */
1479             strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1480             if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1481                 continue;
1482             if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1483                 != IFF_UP)
1484                 continue;
1485             /*
1486              * Get its netmask and OR it into our mask.
1487              */
1488             if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1489                 continue;
1490             mask |= INET_ADDR(ifreq.ifr_addr);
1491             break;
1492         } else {
1493             if (ifr->ifr_addr.sa_len > sizeof (ifr->ifr_addr))
1494                 ifr = (struct ifreq *)((caddr_t)ifr + (ifr->ifr_addr.sa_len - sizeof (ifr->ifr_addr)));
1495         }
1496     }
1497
1498     return mask;
1499 }
1500
1501 static int
1502 strioctl(fd, cmd, ptr, ilen, olen)
1503     int fd, cmd, ilen, olen;
1504     void *ptr;
1505 {
1506     struct strioctl str;
1507
1508     str.ic_cmd = cmd;
1509     str.ic_timout = 0;
1510     str.ic_len = ilen;
1511     str.ic_dp = ptr;
1512     if (ioctl(fd, I_STR, &str) == -1)
1513         return -1;
1514     if (str.ic_len != olen)
1515         syslog(LOG_DEBUG, "strioctl: expected %d bytes, got %d for cmd %x\n",
1516                olen, str.ic_len, cmd);
1517     return 0;
1518 }
1519
1520 /*
1521  * Code for locking/unlocking the serial device.
1522  * This code is derived from chat.c.
1523  */
1524
1525 #if !defined(HDB) && !defined(SUNOS3)
1526 #define HDB     1               /* ascii lock files are the default */
1527 #endif
1528
1529 #ifndef LOCK_DIR
1530 # if HDB
1531 #  define       PIDSTRING
1532 #  define       LOCK_PREFIX     "/usr/spool/locks/LCK.."
1533 # else /* HDB */
1534 #  define       LOCK_PREFIX     "/usr/spool/uucp/LCK.."
1535 # endif /* HDB */
1536 #endif /* LOCK_DIR */
1537
1538 static char *lock_file;         /* name of lock file created */
1539
1540 /*
1541  * lock - create a lock file for the named device.
1542  */
1543 int
1544 lock(dev)
1545     char *dev;
1546 {
1547     char hdb_lock_buffer[12];
1548     int fd, pid, n;
1549     char *p;
1550
1551     if ((p = strrchr(dev, '/')) != NULL)
1552         dev = p + 1;
1553     lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1554     if (lock_file == NULL)
1555         novm("lock file name");
1556     strcat(strcpy(lock_file, LOCK_PREFIX), dev);
1557
1558     while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1559         if (errno == EEXIST
1560             && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1561             /* Read the lock file to find out who has the device locked */
1562 #ifdef PIDSTRING
1563             n = read(fd, hdb_lock_buffer, 11);
1564             if (n > 0) {
1565                 hdb_lock_buffer[n] = 0;
1566                 pid = atoi(hdb_lock_buffer);
1567             }
1568 #else
1569             n = read(fd, &pid, sizeof(pid));
1570 #endif
1571             if (n <= 0) {
1572                 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1573                 close(fd);
1574             } else {
1575                 if (kill(pid, 0) == -1 && errno == ESRCH) {
1576                     /* pid no longer exists - remove the lock file */
1577                     if (unlink(lock_file) == 0) {
1578                         close(fd);
1579                         syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1580                                dev, pid);
1581                         continue;
1582                     } else
1583                         syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
1584                                dev);
1585                 } else
1586                     syslog(LOG_NOTICE, "Device %s is locked by pid %d",
1587                            dev, pid);
1588             }
1589             close(fd);
1590         } else
1591             syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1592         free(lock_file);
1593         lock_file = NULL;
1594         return -1;
1595     }
1596
1597 #ifdef PIDSTRING
1598     sprintf(hdb_lock_buffer, "%10d\n", getpid());
1599     write(fd, hdb_lock_buffer, 11);
1600 #else
1601     pid = getpid();
1602     write(fd, &pid, sizeof pid);
1603 #endif
1604
1605     close(fd);
1606     return 0;
1607 }
1608
1609 /*
1610  * unlock - remove our lockfile
1611  */
1612 void
1613 unlock()
1614 {
1615     if (lock_file) {
1616         unlink(lock_file);
1617         free(lock_file);
1618         lock_file = NULL;
1619     }
1620 }
1621
1622 int
1623 set_filters(pass, active)
1624     struct bpf_program *pass, *active;
1625 {
1626     return 1;
1627 }
1628
1629 int
1630 bpf_compile(program, buf, optimize)
1631     struct bpf_program *program;
1632     char *buf;
1633     int optimize;
1634 {
1635     return 0;
1636 }
1637
1638 char *
1639 bpf_geterr()
1640 {
1641     return 0;
1642 }
1643
1644 u_int
1645 bpf_filter(pc, p, wirelen, buflen)
1646     struct bpf_insn *pc;
1647     u_char *p;
1648     u_int wirelen;
1649     u_int buflen;
1650 {
1651     return 0;
1652 }