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