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