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