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