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