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