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