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