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