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