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