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