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