]> git.ozlabs.org Git - ppp.git/blob - pppd/sys-svr4.c
5357aebfeb91bcb45aac7f6c7abc069068534cb9
[ppp.git] / pppd / sys-svr4.c
1 /*
2  * System-dependent procedures for pppd under Solaris 2.
3  *
4  * Copyright (c) 1994 The Australian National University.
5  * All rights reserved.
6  *
7  * Permission to use, copy, modify, and distribute this software and its
8  * documentation is hereby granted, provided that the above copyright
9  * notice appears in all copies.  This software is provided without any
10  * warranty, express or implied. The Australian National University
11  * makes no representations about the suitability of this software for
12  * any purpose.
13  *
14  * IN NO EVENT SHALL THE AUSTRALIAN NATIONAL UNIVERSITY BE LIABLE TO ANY
15  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
16  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
17  * THE AUSTRALIAN NATIONAL UNIVERSITY HAVE BEEN ADVISED OF THE POSSIBILITY
18  * OF SUCH DAMAGE.
19  *
20  * THE AUSTRALIAN NATIONAL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
21  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
23  * ON AN "AS IS" BASIS, AND THE AUSTRALIAN NATIONAL UNIVERSITY HAS NO
24  * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
25  * OR MODIFICATIONS.
26  */
27
28 #define RCSID   "$Id: sys-svr4.c,v 1.35 1999/09/30 19:59:06 masputra Exp $"
29
30 #include <limits.h>
31 #include <stdio.h>
32 #include <stddef.h>
33 #include <stdlib.h>
34 #include <ctype.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <unistd.h>
38 #include <termios.h>
39 #ifndef CRTSCTS
40 #include <sys/termiox.h>
41 #endif
42 #include <signal.h>
43 #include <utmpx.h>
44 #include <sys/types.h>
45 #include <sys/ioccom.h>
46 #include <sys/stream.h>
47 #include <sys/stropts.h>
48 #include <sys/socket.h>
49 #include <sys/sockio.h>
50 #include <sys/sysmacros.h>
51 #include <sys/systeminfo.h>
52 #include <sys/dlpi.h>
53 #include <sys/stat.h>
54 #include <sys/mkdev.h>
55 #include <net/if.h>
56 #include <net/if_arp.h>
57 #include <net/route.h>
58 #include <net/ppp_defs.h>
59 #include <net/pppio.h>
60 #include <netinet/in.h>
61 #ifdef SOL2
62 #include <sys/tihdr.h>
63 #include <sys/tiuser.h>
64 #include <inet/common.h>
65 #include <inet/mib2.h>
66 #include <sys/ethernet.h>
67 #endif
68
69 #include "pppd.h"
70
71 static const char rcsid[] = RCSID;
72
73 static int      pppfd;
74 static int      fdmuxid = -1;
75 static int      ipfd;
76 static int      ipmuxid = -1;
77
78 #if defined(INET6) && defined(SOL2)
79 static int      ip6fd;          /* IP file descriptor */
80 static int      ip6muxid = -1;  /* Multiplexer file descriptor */
81 static int      if6_is_up = 0;  /* IPv6 interface has been marked up */
82
83 #define _IN6_LLX_FROM_EUI64(l, s, eui64, as) do {       \
84         s->sin6_addr.s6_addr32[0] = htonl(as);  \
85         eui64_copy(eui64, s->sin6_addr.s6_addr32[2]);   \
86         s->sin6_family = AF_INET6;              \
87         l.lifr_addr.ss_family = AF_INET6;       \
88         l.lifr_addrlen = 10;                    \
89         l.lifr_addr = laddr;                    \
90         } while (0)
91
92 #define IN6_LLADDR_FROM_EUI64(l, s, eui64)  \
93     _IN6_LLX_FROM_EUI64(l, s, eui64, 0xfe800000)
94
95 #define IN6_LLTOKEN_FROM_EUI64(l, s, eui64) \
96     _IN6_LLX_FROM_EUI64(l, s, eui64, 0)
97
98 #endif /* defined(INET6) && defined(SOL2) */
99
100 static int      restore_term;
101 static struct termios inittermios;
102 #ifndef CRTSCTS
103 static struct termiox inittermiox;
104 static int      termiox_ok;
105 #endif
106 static struct winsize wsinfo;   /* Initial window size info */
107 static pid_t    tty_sid;        /* original session ID for terminal */
108
109 extern u_char   inpacket_buf[]; /* borrowed from main.c */
110
111 #define MAX_POLLFDS     32
112 static struct pollfd pollfds[MAX_POLLFDS];
113 static int n_pollfds;
114
115 static int      link_mtu, link_mru;
116
117 #define NMODULES        32
118 static int      tty_nmodules;
119 static char     tty_modules[NMODULES][FMNAMESZ+1];
120 static int      tty_npushed;
121
122 static int      if_is_up;       /* Interface has been marked up */
123 static u_int32_t remote_addr;           /* IP address of peer */
124 static u_int32_t default_route_gateway; /* Gateway for default route added */
125 static u_int32_t proxy_arp_addr;        /* Addr for proxy arp entry added */
126
127 /* Prototypes for procedures local to this file. */
128 static int translate_speed __P((int));
129 static int baud_rate_of __P((int));
130 static int get_ether_addr __P((u_int32_t, struct sockaddr *));
131 static int get_hw_addr __P((char *, u_int32_t, struct sockaddr *));
132 static int get_hw_addr_dlpi __P((char *, struct sockaddr *));
133 static int dlpi_attach __P((int, int));
134 static int dlpi_info_req __P((int));
135 static int dlpi_get_reply __P((int, union DL_primitives *, int, int));
136 static int strioctl __P((int, int, void *, int, int));
137
138 #ifdef SOL2
139 /*
140  * sifppa - Sets interface ppa
141  *
142  * without setting the ppa, ip module will return EINVAL upon setting the
143  * interface UP (SIOCSxIFFLAGS). This is because ip module in 2.8 expects
144  * two DLPI_INFO_REQ to be sent down to the driver (below ip) before
145  * IFF_UP can be set. Plumbing the device causes one DLPI_INFO_REQ to
146  * be sent down, and the second DLPI_INFO_REQ is sent upon receiving
147  * IF_UNITSEL (old) or SIOCSLIFNAME (new) ioctls. Such setting of the ppa
148  * is required because the ppp DLPI provider advertises itself as
149  * a DLPI style 2 type, which requires a point of attachment to be
150  * specified. The only way the user can specify a point of attachment
151  * is via SIOCSLIFNAME or IF_UNITSEL.
152  *
153  * Such changes in the behavior of ip module was made to meet new or
154  * evolving standards requirements.
155  *
156  */
157 static int
158 sifppa(fd, ppa)
159         int fd;
160         int ppa;
161 {
162     return (int)ioctl(fd, IF_UNITSEL, (char *)&ppa);
163 }
164 #endif /* SOL2 */
165
166 #if defined(SOL2) && defined(INET6)
167 /*
168  * slifname - Sets interface ppa and flags
169  *
170  * in addition to the comments stated in sifppa(), IFF_IPV6 bit must
171  * be set in order to declare this as an IPv6 interface
172  */
173 static int
174 slifname(fd, ppa)
175         int fd;
176         int ppa;
177 {
178     struct  lifreq lifr;
179     int     ret;
180
181     memset(&lifr, 0, sizeof(lifr));
182     ret = ioctl(fd, SIOCGLIFFLAGS, &lifr);
183     if (ret < 0)
184         goto slifname_done;
185
186     lifr.lifr_flags |= IFF_IPV6;
187     lifr.lifr_flags &= ~(IFF_BROADCAST | IFF_IPV4);
188     lifr.lifr_ppa = ppa;
189     strlcpy(lifr.lifr_name, ifname, sizeof(lifr.lifr_name));
190
191     ret = ioctl(fd, SIOCSLIFNAME, &lifr);
192
193 slifname_done:
194     return ret;
195
196
197 }
198
199 /*
200  * ether_to_eui64 - Convert 48-bit Ethernet address into 64-bit EUI
201  *
202  * walks the list of valid ethernet interfaces, and convert the first
203  * found 48-bit MAC address into EUI 64. caller also assumes that
204  * the system has a properly configured Ethernet interface for this
205  * function to return non-zero.
206  */
207 int
208 ether_to_eui64(eui64_t *p_eui64)
209 {
210     struct lifnum lifn;
211     struct lifconf lifc;
212     struct lifreq *plifreq;
213     struct lifreq lifr;
214     int fd, num_ifs, i, found;
215     uint_t fl, req_size;
216     char *req;
217     struct sockaddr s_eth_addr;
218     struct ether_addr *eth_addr = (struct ether_addr *)&s_eth_addr.sa_data;
219
220     fd = socket(AF_INET, SOCK_DGRAM, 0);
221     if (fd < 0) {
222         return 0;
223     }
224
225     /*
226      * Find out how many interfaces are running
227      */
228     lifn.lifn_family = AF_UNSPEC;
229     lifn.lifn_flags = LIFC_NOXMIT;
230     if (ioctl(fd, SIOCGLIFNUM, &lifn) < 0) {
231         close(fd);
232         error("could not determine number of interfaces: %m");
233         return 0;
234     }
235
236     num_ifs = lifn.lifn_count;
237     req_size = num_ifs * sizeof(struct lifreq);
238     req = malloc(req_size);
239     if (req == NULL) {
240         close(fd);
241         error("out of memory");
242         return 0;
243     }
244
245     /*
246      * Get interface configuration info for all interfaces
247      */
248     lifc.lifc_family = AF_UNSPEC;
249     lifc.lifc_flags = LIFC_NOXMIT;
250     lifc.lifc_len = req_size;
251     lifc.lifc_buf = req;
252     if (ioctl(fd, SIOCGLIFCONF, &lifc) < 0) {
253         close(fd);
254         free(req);
255         error("SIOCGLIFCONF: %m");
256         return 0;
257     }
258
259     /*
260      * And traverse each interface to look specifically for the first
261      * occurence of an Ethernet interface which has been marked up
262      */
263     plifreq = lifc.lifc_req;
264     found = 0;
265     for (i = lifc.lifc_len / sizeof(struct lifreq); i>0; i--, plifreq++) {
266
267         if (strchr(plifreq->lifr_name, ':') != NULL)
268             continue;
269
270         memset(&lifr, 0, sizeof(lifr));
271         strncpy(lifr.lifr_name, plifreq->lifr_name, sizeof(lifr.lifr_name));
272         if (ioctl(fd, SIOCGLIFFLAGS, &lifr) < 0) {
273             close(fd);
274             free(req);
275             error("SIOCGLIFFLAGS: %m");
276             return 0;
277         }
278         fl = lifr.lifr_flags;
279
280         if ((fl & (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
281                 != (IFF_UP | IFF_BROADCAST))
282             continue;
283
284         found = 1;
285         break;
286     }
287     free(req);
288     close(fd);
289
290     if (!found) {
291         error("no persistent id can be found");
292         return 0;
293     }
294  
295     /*
296      * Send DL_INFO_REQ to the driver to solicit its MAC address
297      */
298     if (!get_hw_addr_dlpi(plifreq->lifr_name, &s_eth_addr)) {
299         error("could not obtain hardware address for %s", plifreq->lifr_name);
300         return 0;
301     }
302
303     /*
304      * And convert the EUI-48 into EUI-64, per RFC 2472 [sec 4.1]
305      */
306     p_eui64->e8[0] = (eth_addr->ether_addr_octet[0] & 0xFF) | 0x02;
307     p_eui64->e8[1] = (eth_addr->ether_addr_octet[1] & 0xFF);
308     p_eui64->e8[2] = (eth_addr->ether_addr_octet[2] & 0xFF);
309     p_eui64->e8[3] = 0xFF;
310     p_eui64->e8[4] = 0xFE;
311     p_eui64->e8[5] = (eth_addr->ether_addr_octet[3] & 0xFF);
312     p_eui64->e8[6] = (eth_addr->ether_addr_octet[4] & 0xFF);
313     p_eui64->e8[7] = (eth_addr->ether_addr_octet[5] & 0xFF);
314
315     return 1;
316 }
317 #endif /* defined(SOL2) && defined(INET6) */
318
319 /*
320  * sys_init - System-dependent initialization.
321  */
322 void
323 sys_init()
324 {
325     int ifd, x;
326 #if defined(INET6) && defined(SOL2)
327     int i6fd;
328 #endif /* defined(INET6) && defined(SOL2) */
329 #if !defined(SOL2)
330     struct ifreq ifr;
331     struct {
332         union DL_primitives prim;
333         char space[64];
334     } reply;
335 #endif /* !defined(SOL2) */
336
337     ipfd = open("/dev/ip", O_RDWR, 0);
338     if (ipfd < 0)
339         fatal("Couldn't open IP device: %m");
340
341 #if defined(INET6) && defined(SOL2)
342     ip6fd = open("/dev/ip", O_RDWR, 0);
343     if (ip6fd < 0)
344         fatal("Couldn't open IP device (2): %m");
345 #endif /* defined(INET6) && defined(SOL2) */
346
347     if (default_device && !notty)
348         tty_sid = getsid((pid_t)0);
349
350     pppfd = open("/dev/ppp", O_RDWR | O_NONBLOCK, 0);
351     if (pppfd < 0)
352         fatal("Can't open /dev/ppp: %m");
353     if (kdebugflag & 1) {
354         x = PPPDBG_LOG + PPPDBG_DRIVER;
355         strioctl(pppfd, PPPIO_DEBUG, &x, sizeof(int), 0);
356     }
357
358     /* Assign a new PPA and get its unit number. */
359     if (strioctl(pppfd, PPPIO_NEWPPA, &ifunit, 0, sizeof(int)) < 0)
360         fatal("Can't create new PPP interface: %m");
361
362     /*
363      * Open the ppp device again and link it under the ip multiplexor.
364      * IP will assign a unit number which hopefully is the same as ifunit.
365      * I don't know any way to be certain they will be the same. :-(
366      */
367     ifd = open("/dev/ppp", O_RDWR, 0);
368     if (ifd < 0)
369         fatal("Can't open /dev/ppp (2): %m");
370     if (kdebugflag & 1) {
371         x = PPPDBG_LOG + PPPDBG_DRIVER;
372         strioctl(ifd, PPPIO_DEBUG, &x, sizeof(int), 0);
373     }
374
375 #if defined(INET6) && defined(SOL2)
376     /*
377      * Since sys_init() is called prior to ifname being set in main(),
378      * we need to get the ifname now, otherwise slifname() will fail,
379      * or maybe, I should move slifname() to a later point ?
380      */
381     sprintf(ifname, "ppp%d", ifunit);
382
383     i6fd = open("/dev/ppp", O_RDWR, 0);
384     if (i6fd < 0) {
385         close(ifd);
386         fatal("Can't open /dev/ppp (3): %m");
387     }
388     if (kdebugflag & 1) {
389         x = PPPDBG_LOG + PPPDBG_DRIVER;
390         strioctl(i6fd, PPPIO_DEBUG, &x, sizeof(int), 0);
391     }
392 #endif /* defined(INET6) && defined(SOL2) */
393
394 #if defined(SOL2)
395     if (ioctl(ifd, I_PUSH, "ip") < 0) {
396         close(ifd);
397 #if defined(INET6)
398         close(i6fd);
399 #endif /* defined(INET6) */
400         fatal("Can't push IP module: %m");
401     }
402
403     /*
404      * Assign ppa according to the unit number returned by ppp device
405      * after plumbing is completed above.
406      */
407     if (sifppa(ifd, ifunit) < 0) {
408         close (ifd);
409 #if defined(INET6)
410         close(i6fd);
411 #endif /* defined(INET6) */
412         fatal("Can't set ppa for unit %d: %m", ifunit);
413     }
414
415 #if defined(INET6)
416     /*
417      * An IPv6 interface is created anyway, even when the user does not 
418      * explicitly enable it. Note that the interface will be marked
419      * IPv6 during slifname().
420      */
421     if (ioctl(i6fd, I_PUSH, "ip") < 0) {
422         close(ifd);
423         close(i6fd);
424         fatal("Can't push IP module (2): %m");
425     }
426
427     /*
428      * Assign ppa according to the unit number returned by ppp device
429      * after plumbing is completed above. In addition, mark the interface
430      * as an IPv6 interface.
431      */
432     if (slifname(i6fd, ifunit) < 0) {
433         close(ifd);
434         close(i6fd);
435         fatal("Can't set ifname for unit %d: %m", ifunit);
436     }
437 #endif /* defined(INET6) */
438
439 #else /* else if !defined(SOL2) */
440
441     if (dlpi_attach(ifd, ifunit) < 0 ||
442         dlpi_get_reply(ifd, &reply.prim, DL_OK_ACK, sizeof(reply)) < 0) {
443         close(ifd);
444         fatal("Can't attach to ppp%d: %m", ifunit);
445     }
446 #endif /* defined(SOL2) */
447
448     ipmuxid = ioctl(ipfd, I_LINK, ifd);
449     close(ifd);
450     if (ipmuxid < 0)
451         fatal("Can't link PPP device to IP: %m");
452
453 #if defined(INET6) && defined(SOL2)
454     ip6muxid = ioctl(ip6fd, I_LINK, i6fd);
455     close(i6fd);
456     if (ip6muxid < 0) 
457         fatal("Can't link PPP device to IP (2): %m");
458 #endif /* defined(INET6) && defined(SOL2) */
459
460 #if !defined(SOL2)
461     /* Set the interface name for the link. */
462     slprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "ppp%d", ifunit);
463     ifr.ifr_metric = ipmuxid;
464     if (strioctl(ipfd, SIOCSIFNAME, (char *)&ifr, sizeof ifr, 0) < 0)
465         fatal("Can't set interface name %s: %m", ifr.ifr_name);
466 #endif /* !defined(SOL2) */
467
468     n_pollfds = 0;
469 }
470
471 /*
472  * sys_cleanup - restore any system state we modified before exiting:
473  * mark the interface down, delete default route and/or proxy arp entry.
474  * This should call die() because it's called from die().
475  */
476 void
477 sys_cleanup()
478 {
479     struct ifreq ifr;
480
481 #if defined(SOL2) && defined(INET6)
482     if (if6_is_up)
483         sif6down(0);
484 #endif /* defined(SOL2) && defined(INET6) */
485     if (if_is_up)
486         sifdown(0);
487     if (default_route_gateway)
488         cifdefaultroute(0, default_route_gateway, default_route_gateway);
489     if (proxy_arp_addr)
490         cifproxyarp(0, proxy_arp_addr);
491 }
492
493 /*
494  * sys_close - Clean up in a child process before execing.
495  */
496 void
497 sys_close()
498 {
499     close(ipfd);
500 #if defined(INET6) && defined(SOL2)
501     close(ip6fd);
502 #endif /* defined(INET6) && defined(SOL2) */
503     if (pppfd >= 0)
504         close(pppfd);
505 }
506
507 /*
508  * sys_check_options - check the options that the user specified
509  */
510 int
511 sys_check_options()
512 {
513     return 1;
514 }
515
516 #if 0
517 /*
518  * daemon - Detach us from controlling terminal session.
519  */
520 int
521 daemon(nochdir, noclose)
522     int nochdir, noclose;
523 {
524     int pid;
525
526     if ((pid = fork()) < 0)
527         return -1;
528     if (pid != 0)
529         exit(0);                /* parent dies */
530     setsid();
531     if (!nochdir)
532         chdir("/");
533     if (!noclose) {
534         fclose(stdin);          /* don't need stdin, stdout, stderr */
535         fclose(stdout);
536         fclose(stderr);
537     }
538     return 0;
539 }
540 #endif
541
542 /*
543  * ppp_available - check whether the system has any ppp interfaces
544  */
545 int
546 ppp_available()
547 {
548     struct stat buf;
549
550     return stat("/dev/ppp", &buf) >= 0;
551 }
552
553 /*
554  * establish_ppp - Turn the serial port into a ppp interface.
555  */
556 int
557 establish_ppp(fd)
558     int fd;
559 {
560     int i;
561
562     /* Pop any existing modules off the tty stream. */
563     for (i = 0;; ++i)
564         if (ioctl(fd, I_LOOK, tty_modules[i]) < 0
565             || strcmp(tty_modules[i], "ptem") == 0
566             || ioctl(fd, I_POP, 0) < 0)
567             break;
568     tty_nmodules = i;
569
570     /* Push the async hdlc module and the compressor module. */
571     tty_npushed = 0;
572
573     if(!sync_serial) {
574         if (ioctl(fd, I_PUSH, "ppp_ahdl") < 0) {
575             error("Couldn't push PPP Async HDLC module: %m");
576             return -1;
577         }
578         ++tty_npushed;
579     }
580     if (kdebugflag & 4) {
581         i = PPPDBG_LOG + PPPDBG_AHDLC;
582         strioctl(pppfd, PPPIO_DEBUG, &i, sizeof(int), 0);
583     }
584     if (ioctl(fd, I_PUSH, "ppp_comp") < 0)
585         error("Couldn't push PPP compression module: %m");
586     else
587         ++tty_npushed;
588     if (kdebugflag & 2) {
589         i = PPPDBG_LOG + PPPDBG_COMP;
590         strioctl(pppfd, PPPIO_DEBUG, &i, sizeof(int), 0);
591     }
592
593     /* Link the serial port under the PPP multiplexor. */
594     if ((fdmuxid = ioctl(pppfd, I_LINK, fd)) < 0) {
595         error("Can't link tty to PPP mux: %m");
596         return -1;
597     }
598
599     return pppfd;
600 }
601
602 /*
603  * restore_loop - reattach the ppp unit to the loopback.
604  * This doesn't need to do anything because disestablish_ppp does it.
605  */
606 void
607 restore_loop()
608 {
609 }
610
611 /*
612  * disestablish_ppp - Restore the serial port to normal operation.
613  * It attempts to reconstruct the stream with the previously popped
614  * modules.  This shouldn't call die() because it's called from die().
615  */
616 void
617 disestablish_ppp(fd)
618     int fd;
619 {
620     int i;
621
622     if (fdmuxid >= 0) {
623         if (ioctl(pppfd, I_UNLINK, fdmuxid) < 0) {
624             if (!hungup)
625                 error("Can't unlink tty from PPP mux: %m");
626         }
627         fdmuxid = -1;
628
629         if (!hungup) {
630             while (tty_npushed > 0 && ioctl(fd, I_POP, 0) >= 0)
631                 --tty_npushed;
632             for (i = tty_nmodules - 1; i >= 0; --i)
633                 if (ioctl(fd, I_PUSH, tty_modules[i]) < 0)
634                     error("Couldn't restore tty module %s: %m",
635                            tty_modules[i]);
636         }
637         if (hungup && default_device && tty_sid > 0) {
638             /*
639              * If we have received a hangup, we need to send a SIGHUP
640              * to the terminal's controlling process.  The reason is
641              * that the original stream head for the terminal hasn't
642              * seen the M_HANGUP message (it went up through the ppp
643              * driver to the stream head for our fd to /dev/ppp).
644              */
645             kill(tty_sid, SIGHUP);
646         }
647     }
648 }
649
650 /*
651  * Check whether the link seems not to be 8-bit clean.
652  */
653 void
654 clean_check()
655 {
656     int x;
657     char *s;
658
659     if (strioctl(pppfd, PPPIO_GCLEAN, &x, 0, sizeof(x)) < 0)
660         return;
661     s = NULL;
662     switch (~x) {
663     case RCV_B7_0:
664         s = "bit 7 set to 1";
665         break;
666     case RCV_B7_1:
667         s = "bit 7 set to 0";
668         break;
669     case RCV_EVNP:
670         s = "odd parity";
671         break;
672     case RCV_ODDP:
673         s = "even parity";
674         break;
675     }
676     if (s != NULL) {
677         warn("Serial link is not 8-bit clean:");
678         warn("All received characters had %s", s);
679     }
680 }
681
682 /*
683  * List of valid speeds.
684  */
685 struct speed {
686     int speed_int, speed_val;
687 } speeds[] = {
688 #ifdef B50
689     { 50, B50 },
690 #endif
691 #ifdef B75
692     { 75, B75 },
693 #endif
694 #ifdef B110
695     { 110, B110 },
696 #endif
697 #ifdef B134
698     { 134, B134 },
699 #endif
700 #ifdef B150
701     { 150, B150 },
702 #endif
703 #ifdef B200
704     { 200, B200 },
705 #endif
706 #ifdef B300
707     { 300, B300 },
708 #endif
709 #ifdef B600
710     { 600, B600 },
711 #endif
712 #ifdef B1200
713     { 1200, B1200 },
714 #endif
715 #ifdef B1800
716     { 1800, B1800 },
717 #endif
718 #ifdef B2000
719     { 2000, B2000 },
720 #endif
721 #ifdef B2400
722     { 2400, B2400 },
723 #endif
724 #ifdef B3600
725     { 3600, B3600 },
726 #endif
727 #ifdef B4800
728     { 4800, B4800 },
729 #endif
730 #ifdef B7200
731     { 7200, B7200 },
732 #endif
733 #ifdef B9600
734     { 9600, B9600 },
735 #endif
736 #ifdef B19200
737     { 19200, B19200 },
738 #endif
739 #ifdef B38400
740     { 38400, B38400 },
741 #endif
742 #ifdef EXTA
743     { 19200, EXTA },
744 #endif
745 #ifdef EXTB
746     { 38400, EXTB },
747 #endif
748 #ifdef B57600
749     { 57600, B57600 },
750 #endif
751 #ifdef B76800
752     { 76800, B76800 },
753 #endif
754 #ifdef B115200
755     { 115200, B115200 },
756 #endif
757     { 0, 0 }
758 };
759
760 /*
761  * Translate from bits/second to a speed_t.
762  */
763 static int
764 translate_speed(bps)
765     int bps;
766 {
767     struct speed *speedp;
768
769     if (bps == 0)
770         return 0;
771     for (speedp = speeds; speedp->speed_int; speedp++)
772         if (bps == speedp->speed_int)
773             return speedp->speed_val;
774     warn("speed %d not supported", bps);
775     return 0;
776 }
777
778 /*
779  * Translate from a speed_t to bits/second.
780  */
781 static int
782 baud_rate_of(speed)
783     int speed;
784 {
785     struct speed *speedp;
786
787     if (speed == 0)
788         return 0;
789     for (speedp = speeds; speedp->speed_int; speedp++)
790         if (speed == speedp->speed_val)
791             return speedp->speed_int;
792     return 0;
793 }
794
795 /*
796  * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
797  * at the requested speed, etc.  If `local' is true, set CLOCAL
798  * regardless of whether the modem option was specified.
799  */
800 void
801 set_up_tty(fd, local)
802     int fd, local;
803 {
804     int speed;
805     struct termios tios;
806 #if !defined (CRTSCTS)
807     struct termiox tiox;
808 #endif
809
810     if (!sync_serial && tcgetattr(fd, &tios) < 0)
811         fatal("tcgetattr: %m");
812
813 #ifndef CRTSCTS
814     termiox_ok = 1;
815     if (ioctl (fd, TCGETX, &tiox) < 0) {
816         termiox_ok = 0;
817         if (errno != ENOTTY)
818             error("TCGETX: %m");
819     }
820 #endif
821
822     if (!restore_term) {
823         inittermios = tios;
824 #ifndef CRTSCTS
825         inittermiox = tiox;
826 #endif
827         ioctl(fd, TIOCGWINSZ, &wsinfo);
828     }
829
830     tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
831 #ifdef CRTSCTS
832     if (crtscts > 0)
833         tios.c_cflag |= CRTSCTS;
834     else if (crtscts < 0)
835         tios.c_cflag &= ~CRTSCTS;
836 #else
837     if (crtscts != 0 && !termiox_ok) {
838         error("Can't set RTS/CTS flow control");
839     } else if (crtscts > 0) {
840         tiox.x_hflag |= RTSXOFF|CTSXON;
841     } else if (crtscts < 0) {
842         tiox.x_hflag &= ~(RTSXOFF|CTSXON);
843     }
844 #endif
845
846     tios.c_cflag |= CS8 | CREAD | HUPCL;
847     if (local || !modem)
848         tios.c_cflag |= CLOCAL;
849     tios.c_iflag = IGNBRK | IGNPAR;
850     tios.c_oflag = 0;
851     tios.c_lflag = 0;
852     tios.c_cc[VMIN] = 1;
853     tios.c_cc[VTIME] = 0;
854
855     if (crtscts == -2) {
856         tios.c_iflag |= IXON | IXOFF;
857         tios.c_cc[VSTOP] = 0x13;        /* DC3 = XOFF = ^S */
858         tios.c_cc[VSTART] = 0x11;       /* DC1 = XON  = ^Q */
859     }
860
861     speed = translate_speed(inspeed);
862     if (speed) {
863         cfsetospeed(&tios, speed);
864         cfsetispeed(&tios, speed);
865     } else {
866         speed = cfgetospeed(&tios);
867         /*
868          * We can't proceed if the serial port speed is 0,
869          * since that implies that the serial port is disabled.
870          */
871         if (speed == B0)
872             fatal("Baud rate for %s is 0; need explicit baud rate", devnam);
873     }
874
875     if (!sync_serial && tcsetattr(fd, TCSAFLUSH, &tios) < 0)
876         fatal("tcsetattr: %m");
877
878 #ifndef CRTSCTS
879     if (termiox_ok && ioctl (fd, TCSETXF, &tiox) < 0){
880         error("TCSETXF: %m");
881     }
882 #endif
883
884     baud_rate = inspeed = baud_rate_of(speed);
885     restore_term = 1;
886 }
887
888 /*
889  * restore_tty - restore the terminal to the saved settings.
890  */
891 void
892 restore_tty(fd)
893     int fd;
894 {
895     if (restore_term) {
896         if (!default_device) {
897             /*
898              * Turn off echoing, because otherwise we can get into
899              * a loop with the tty and the modem echoing to each other.
900              * We presume we are the sole user of this tty device, so
901              * when we close it, it will revert to its defaults anyway.
902              */
903             inittermios.c_lflag &= ~(ECHO | ECHONL);
904         }
905         if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
906             if (!hungup && errno != ENXIO)
907                 warn("tcsetattr: %m");
908 #ifndef CRTSCTS
909         if (ioctl (fd, TCSETXF, &inittermiox) < 0){
910             if (!hungup && errno != ENXIO)
911                 error("TCSETXF: %m");
912         }
913 #endif
914         ioctl(fd, TIOCSWINSZ, &wsinfo);
915         restore_term = 0;
916     }
917 }
918
919 /*
920  * setdtr - control the DTR line on the serial port.
921  * This is called from die(), so it shouldn't call die().
922  */
923 void
924 setdtr(fd, on)
925 int fd, on;
926 {
927     int modembits = TIOCM_DTR;
928
929     ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
930 }
931
932 /*
933  * open_loopback - open the device we use for getting packets
934  * in demand mode.  Under Solaris 2, we use our existing fd
935  * to the ppp driver.
936  */
937 int
938 open_ppp_loopback()
939 {
940     return pppfd;
941 }
942
943 /*
944  * output - Output PPP packet.
945  */
946 void
947 output(unit, p, len)
948     int unit;
949     u_char *p;
950     int len;
951 {
952     struct strbuf data;
953     int retries;
954     struct pollfd pfd;
955
956     if (debug)
957         dbglog("sent %P", p, len);
958
959     data.len = len;
960     data.buf = (caddr_t) p;
961     retries = 4;
962     while (putmsg(pppfd, NULL, &data, 0) < 0) {
963         if (--retries < 0 || (errno != EWOULDBLOCK && errno != EAGAIN)) {
964             if (errno != ENXIO)
965                 error("Couldn't send packet: %m");
966             break;
967         }
968         pfd.fd = pppfd;
969         pfd.events = POLLOUT;
970         poll(&pfd, 1, 250);     /* wait for up to 0.25 seconds */
971     }
972 }
973
974
975 /*
976  * wait_input - wait until there is data available,
977  * for the length of time specified by *timo (indefinite
978  * if timo is NULL).
979  */
980 void
981 wait_input(timo)
982     struct timeval *timo;
983 {
984     int t;
985
986     t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
987     if (poll(pollfds, n_pollfds, t) < 0 && errno != EINTR)
988         fatal("poll: %m");
989 }
990
991 /*
992  * add_fd - add an fd to the set that wait_input waits for.
993  */
994 void add_fd(fd)
995     int fd;
996 {
997     int n;
998
999     for (n = 0; n < n_pollfds; ++n)
1000         if (pollfds[n].fd == fd)
1001             return;
1002     if (n_pollfds < MAX_POLLFDS) {
1003         pollfds[n_pollfds].fd = fd;
1004         pollfds[n_pollfds].events = POLLIN | POLLPRI | POLLHUP;
1005         ++n_pollfds;
1006     } else
1007         error("Too many inputs!");
1008 }
1009
1010 /*
1011  * remove_fd - remove an fd from the set that wait_input waits for.
1012  */
1013 void remove_fd(fd)
1014     int fd;
1015 {
1016     int n;
1017
1018     for (n = 0; n < n_pollfds; ++n) {
1019         if (pollfds[n].fd == fd) {
1020             while (++n < n_pollfds)
1021                 pollfds[n-1] = pollfds[n];
1022             --n_pollfds;
1023             break;
1024         }
1025     }
1026 }
1027
1028 #if 0
1029 /*
1030  * wait_loop_output - wait until there is data available on the
1031  * loopback, for the length of time specified by *timo (indefinite
1032  * if timo is NULL).
1033  */
1034 void
1035 wait_loop_output(timo)
1036     struct timeval *timo;
1037 {
1038     wait_input(timo);
1039 }
1040
1041 /*
1042  * wait_time - wait for a given length of time or until a
1043  * signal is received.
1044  */
1045 void
1046 wait_time(timo)
1047     struct timeval *timo;
1048 {
1049     int n;
1050
1051     n = select(0, NULL, NULL, NULL, timo);
1052     if (n < 0 && errno != EINTR)
1053         fatal("select: %m");
1054 }
1055 #endif
1056
1057
1058 /*
1059  * read_packet - get a PPP packet from the serial device.
1060  */
1061 int
1062 read_packet(buf)
1063     u_char *buf;
1064 {
1065     struct strbuf ctrl, data;
1066     int flags, len;
1067     unsigned char ctrlbuf[sizeof(union DL_primitives) + 64];
1068
1069     for (;;) {
1070         data.maxlen = PPP_MRU + PPP_HDRLEN;
1071         data.buf = (caddr_t) buf;
1072         ctrl.maxlen = sizeof(ctrlbuf);
1073         ctrl.buf = (caddr_t) ctrlbuf;
1074         flags = 0;
1075         len = getmsg(pppfd, &ctrl, &data, &flags);
1076         if (len < 0) {
1077             if (errno == EAGAIN || errno == EINTR)
1078                 return -1;
1079             fatal("Error reading packet: %m");
1080         }
1081
1082         if (ctrl.len <= 0)
1083             return data.len;
1084
1085         /*
1086          * Got a M_PROTO or M_PCPROTO message.  Interpret it
1087          * as a DLPI primitive??
1088          */
1089         if (debug)
1090             dbglog("got dlpi prim 0x%x, len=%d",
1091                    ((union DL_primitives *)ctrlbuf)->dl_primitive, ctrl.len);
1092
1093     }
1094 }
1095
1096 /*
1097  * get_loop_output - get outgoing packets from the ppp device,
1098  * and detect when we want to bring the real link up.
1099  * Return value is 1 if we need to bring up the link, 0 otherwise.
1100  */
1101 int
1102 get_loop_output()
1103 {
1104     int len;
1105     int rv = 0;
1106
1107     while ((len = read_packet(inpacket_buf)) > 0) {
1108         if (loop_frame(inpacket_buf, len))
1109             rv = 1;
1110     }
1111     return rv;
1112 }
1113
1114 /*
1115  * ppp_send_config - configure the transmit characteristics of
1116  * the ppp interface.
1117  */
1118 void
1119 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
1120     int unit, mtu;
1121     u_int32_t asyncmap;
1122     int pcomp, accomp;
1123 {
1124     int cf[2];
1125     struct ifreq ifr;
1126 #if defined(INET6) && defined(SOL2)
1127     struct lifreq lifr;
1128     int fd;
1129 #endif /* defined(INET6) && defined(SOL2) */
1130
1131     link_mtu = mtu;
1132     if (strioctl(pppfd, PPPIO_MTU, &mtu, sizeof(mtu), 0) < 0) {
1133         if (hungup && errno == ENXIO)
1134             return;
1135         error("Couldn't set MTU: %m");
1136     }
1137     if (fdmuxid >= 0) {
1138         /* can't set these if we don't have a stream attached below /dev/ppp */
1139         if (strioctl(pppfd, PPPIO_XACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
1140             error("Couldn't set transmit ACCM: %m");
1141         }
1142         cf[0] = (pcomp? COMP_PROT: 0) + (accomp? COMP_AC: 0);
1143         cf[1] = COMP_PROT | COMP_AC;
1144         if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
1145             error("Couldn't set prot/AC compression: %m");
1146         }
1147     }
1148
1149     /* set the MTU for IP as well */
1150     memset(&ifr, 0, sizeof(ifr));
1151     strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1152     ifr.ifr_metric = link_mtu;
1153     if (ioctl(ipfd, SIOCSIFMTU, &ifr) < 0) {
1154         error("Couldn't set IP MTU: %m");
1155     }
1156
1157 #if defined(INET6) && defined(SOL2) 
1158     fd = socket(AF_INET6, SOCK_DGRAM, 0);
1159     if (fd < 0)
1160         error("Couldn't open IPv6 socket: %m");
1161
1162     memset(&lifr, 0, sizeof(lifr));
1163     strlcpy(lifr.lifr_name, ifname, sizeof(lifr.lifr_name));
1164     lifr.lifr_mtu = link_mtu;
1165     if (ioctl(fd, SIOCSLIFMTU, &lifr) < 0) {
1166         close(fd);
1167         error("Couldn't set IPv6 MTU: %m");
1168     }
1169     close(fd);
1170 #endif /* defined(INET6) && defined(SOL2) */
1171 }
1172
1173 /*
1174  * ppp_set_xaccm - set the extended transmit ACCM for the interface.
1175  */
1176 void
1177 ppp_set_xaccm(unit, accm)
1178     int unit;
1179     ext_accm accm;
1180 {
1181     if (fdmuxid >= 0
1182         && strioctl(pppfd, PPPIO_XACCM, accm, sizeof(ext_accm), 0) < 0) {
1183         if (!hungup || errno != ENXIO)
1184             warn("Couldn't set extended ACCM: %m");
1185     }
1186 }
1187
1188 /*
1189  * ppp_recv_config - configure the receive-side characteristics of
1190  * the ppp interface.
1191  */
1192 void
1193 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
1194     int unit, mru;
1195     u_int32_t asyncmap;
1196     int pcomp, accomp;
1197 {
1198     int cf[2];
1199
1200     link_mru = mru;
1201     if (strioctl(pppfd, PPPIO_MRU, &mru, sizeof(mru), 0) < 0) {
1202         if (hungup && errno == ENXIO)
1203             return;
1204         error("Couldn't set MRU: %m");
1205     }
1206     if (fdmuxid >= 0) {
1207         /* can't set these if we don't have a stream attached below /dev/ppp */
1208         if (strioctl(pppfd, PPPIO_RACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
1209             error("Couldn't set receive ACCM: %m");
1210         }
1211         cf[0] = (pcomp? DECOMP_PROT: 0) + (accomp? DECOMP_AC: 0);
1212         cf[1] = DECOMP_PROT | DECOMP_AC;
1213         if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
1214             error("Couldn't set prot/AC decompression: %m");
1215         }
1216     }
1217 }
1218
1219 /*
1220  * ccp_test - ask kernel whether a given compression method
1221  * is acceptable for use.
1222  */
1223 int
1224 ccp_test(unit, opt_ptr, opt_len, for_transmit)
1225     int unit, opt_len, for_transmit;
1226     u_char *opt_ptr;
1227 {
1228     if (strioctl(pppfd, (for_transmit? PPPIO_XCOMP: PPPIO_RCOMP),
1229                  opt_ptr, opt_len, 0) >= 0)
1230         return 1;
1231     return (errno == ENOSR)? 0: -1;
1232 }
1233
1234 /*
1235  * ccp_flags_set - inform kernel about the current state of CCP.
1236  */
1237 void
1238 ccp_flags_set(unit, isopen, isup)
1239     int unit, isopen, isup;
1240 {
1241     int cf[2];
1242
1243     cf[0] = (isopen? CCP_ISOPEN: 0) + (isup? CCP_ISUP: 0);
1244     cf[1] = CCP_ISOPEN | CCP_ISUP | CCP_ERROR | CCP_FATALERROR;
1245     if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
1246         if (!hungup || errno != ENXIO)
1247             error("Couldn't set kernel CCP state: %m");
1248     }
1249 }
1250
1251 /*
1252  * get_idle_time - return how long the link has been idle.
1253  */
1254 int
1255 get_idle_time(u, ip)
1256     int u;
1257     struct ppp_idle *ip;
1258 {
1259     return strioctl(pppfd, PPPIO_GIDLE, ip, 0, sizeof(struct ppp_idle)) >= 0;
1260 }
1261
1262 /*
1263  * get_ppp_stats - return statistics for the link.
1264  */
1265 int
1266 get_ppp_stats(u, stats)
1267     int u;
1268     struct pppd_stats *stats;
1269 {
1270     struct ppp_stats s;
1271
1272     if (strioctl(pppfd, PPPIO_GETSTAT, &s, 0, sizeof(s)) < 0) {
1273         error("Couldn't get link statistics: %m");
1274         return 0;
1275     }
1276     stats->bytes_in = s.p.ppp_ibytes;
1277     stats->bytes_out = s.p.ppp_obytes;
1278     return 1;
1279 }
1280
1281 #if 0
1282 /*
1283  * set_filters - transfer the pass and active filters to the kernel.
1284  */
1285 int
1286 set_filters(pass, active)
1287     struct bpf_program *pass, *active;
1288 {
1289     int ret = 1;
1290
1291     if (pass->bf_len > 0) {
1292         if (strioctl(pppfd, PPPIO_PASSFILT, pass,
1293                      sizeof(struct bpf_program), 0) < 0) {
1294             error("Couldn't set pass-filter in kernel: %m");
1295             ret = 0;
1296         }
1297     }
1298     if (active->bf_len > 0) {
1299         if (strioctl(pppfd, PPPIO_ACTIVEFILT, active,
1300                      sizeof(struct bpf_program), 0) < 0) {
1301             error("Couldn't set active-filter in kernel: %m");
1302             ret = 0;
1303         }
1304     }
1305     return ret;
1306 }
1307 #endif
1308
1309 /*
1310  * ccp_fatal_error - returns 1 if decompression was disabled as a
1311  * result of an error detected after decompression of a packet,
1312  * 0 otherwise.  This is necessary because of patent nonsense.
1313  */
1314 int
1315 ccp_fatal_error(unit)
1316     int unit;
1317 {
1318     int cf[2];
1319
1320     cf[0] = cf[1] = 0;
1321     if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
1322         if (errno != ENXIO && errno != EINVAL)
1323             error("Couldn't get compression flags: %m");
1324         return 0;
1325     }
1326     return cf[0] & CCP_FATALERROR;
1327 }
1328
1329 /*
1330  * sifvjcomp - config tcp header compression
1331  */
1332 int
1333 sifvjcomp(u, vjcomp, xcidcomp, xmaxcid)
1334     int u, vjcomp, xcidcomp, xmaxcid;
1335 {
1336     int cf[2];
1337     char maxcid[2];
1338
1339     if (vjcomp) {
1340         maxcid[0] = xcidcomp;
1341         maxcid[1] = 15;         /* XXX should be rmaxcid */
1342         if (strioctl(pppfd, PPPIO_VJINIT, maxcid, sizeof(maxcid), 0) < 0) {
1343             error("Couldn't initialize VJ compression: %m");
1344         }
1345     }
1346
1347     cf[0] = (vjcomp? COMP_VJC + DECOMP_VJC: 0)  /* XXX this is wrong */
1348         + (xcidcomp? COMP_VJCCID + DECOMP_VJCCID: 0);
1349     cf[1] = COMP_VJC + DECOMP_VJC + COMP_VJCCID + DECOMP_VJCCID;
1350     if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
1351         if (vjcomp)
1352             error("Couldn't enable VJ compression: %m");
1353     }
1354
1355     return 1;
1356 }
1357
1358 /*
1359  * sifup - Config the interface up and enable IP packets to pass.
1360  */
1361 int
1362 sifup(u)
1363     int u;
1364 {
1365     struct ifreq ifr;
1366
1367     strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1368     if (ioctl(ipfd, SIOCGIFFLAGS, &ifr) < 0) {
1369         error("Couldn't mark interface up (get): %m");
1370         return 0;
1371     }
1372     ifr.ifr_flags |= IFF_UP;
1373     if (ioctl(ipfd, SIOCSIFFLAGS, &ifr) < 0) {
1374         error("Couldn't mark interface up (set): %m");
1375         return 0;
1376     }
1377     if_is_up = 1;
1378     return 1;
1379 }
1380
1381 /*
1382  * sifdown - Config the interface down and disable IP.
1383  */
1384 int
1385 sifdown(u)
1386     int u;
1387 {
1388     struct ifreq ifr;
1389
1390     if (ipmuxid < 0)
1391         return 1;
1392     strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1393     if (ioctl(ipfd, SIOCGIFFLAGS, &ifr) < 0) {
1394         error("Couldn't mark interface down (get): %m");
1395         return 0;
1396     }
1397     ifr.ifr_flags &= ~IFF_UP;
1398     if (ioctl(ipfd, SIOCSIFFLAGS, &ifr) < 0) {
1399         error("Couldn't mark interface down (set): %m");
1400         return 0;
1401     }
1402     if_is_up = 0;
1403     return 1;
1404 }
1405
1406 /*
1407  * sifnpmode - Set the mode for handling packets for a given NP.
1408  */
1409 int
1410 sifnpmode(u, proto, mode)
1411     int u;
1412     int proto;
1413     enum NPmode mode;
1414 {
1415     int npi[2];
1416
1417     npi[0] = proto;
1418     npi[1] = (int) mode;
1419     if (strioctl(pppfd, PPPIO_NPMODE, &npi, 2 * sizeof(int), 0) < 0) {
1420         error("ioctl(set NP %d mode to %d): %m", proto, mode);
1421         return 0;
1422     }
1423     return 1;
1424 }
1425
1426 #if defined(SOL2) && defined(INET6)
1427 /*
1428  * sif6up - Config the IPv6 interface up and enable IPv6 packets to pass.
1429  */
1430 int
1431 sif6up(u)
1432     int u;
1433 {
1434     struct lifreq lifr;
1435     int fd;
1436
1437     fd = socket(AF_INET6, SOCK_DGRAM, 0);
1438     if (fd < 0) {
1439         return 0;
1440     }
1441
1442     memset(&lifr, 0, sizeof(lifr));
1443     strlcpy(lifr.lifr_name, ifname, sizeof(lifr.lifr_name));
1444     if (ioctl(fd, SIOCGLIFFLAGS, &lifr) < 0) {
1445         close(fd);
1446         return 0;
1447     }
1448
1449     lifr.lifr_flags |= IFF_UP;
1450     strlcpy(lifr.lifr_name, ifname, sizeof(lifr.lifr_name));
1451     if (ioctl(fd, SIOCSLIFFLAGS, &lifr) < 0) {
1452         close(fd);
1453         return 0;
1454     }
1455
1456     if6_is_up = 1;
1457     close(fd);
1458     return 1;
1459 }
1460
1461 /*
1462  * sifdown - Config the IPv6 interface down and disable IPv6.
1463  */
1464 int
1465 sif6down(u)
1466     int u;
1467 {
1468     struct lifreq lifr;
1469     int fd;
1470
1471     fd = socket(AF_INET6, SOCK_DGRAM, 0);
1472     if (fd < 0)
1473         return 0;
1474
1475     memset(&lifr, 0, sizeof(lifr));
1476     strlcpy(lifr.lifr_name, ifname, sizeof(lifr.lifr_name));
1477     if (ioctl(fd, SIOCGLIFFLAGS, &lifr) < 0) {
1478         close(fd);
1479         return 0;
1480     }
1481
1482     lifr.lifr_flags &= ~IFF_UP;
1483     strlcpy(lifr.lifr_name, ifname, sizeof(lifr.lifr_name));
1484     if (ioctl(fd, SIOCGLIFFLAGS, &lifr) < 0) {
1485         close(fd);
1486         return 0;
1487     }
1488
1489     if6_is_up = 0;
1490     close(fd);
1491     return 1;
1492 }
1493
1494 /*
1495  * sif6addr - Config the interface with an IPv6 link-local address
1496  */
1497 int
1498 sif6addr(u, o, h)
1499     int u;
1500     eui64_t o, h;
1501 {
1502     struct lifreq lifr;
1503     struct sockaddr_storage laddr;
1504     struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&laddr;
1505     int fd;
1506
1507     fd = socket(AF_INET6, SOCK_DGRAM, 0);
1508     if (fd < 0)
1509         return 0;
1510
1511     memset(&lifr, 0, sizeof(lifr));
1512     strlcpy(lifr.lifr_name, ifname, sizeof(lifr.lifr_name));
1513
1514     /*
1515      * Do this because /dev/ppp responds to DL_PHYS_ADDR_REQ with
1516      * zero values, hence the interface token came to be zero too,
1517      * and without this, in.ndpd will complain
1518      */
1519     IN6_LLTOKEN_FROM_EUI64(lifr, sin6, o);
1520     if (ioctl(fd, SIOCSLIFTOKEN, &lifr) < 0) {
1521         close(fd);
1522         return 0;
1523     }
1524
1525     /*
1526      * Set the interface address and destination address
1527      */
1528     IN6_LLADDR_FROM_EUI64(lifr, sin6, o);
1529     if (ioctl(fd, SIOCSLIFADDR, &lifr) < 0) {
1530         close(fd);
1531         return 0;
1532     }
1533
1534     memset(&lifr, 0, sizeof(lifr));
1535     strlcpy(lifr.lifr_name, ifname, sizeof(lifr.lifr_name));
1536     IN6_LLADDR_FROM_EUI64(lifr, sin6, h);
1537     if (ioctl(fd, SIOCSLIFDSTADDR, &lifr) < 0) {
1538         close(fd);
1539         return 0;
1540     }
1541
1542     return 1;
1543 }
1544
1545 /*
1546  * cif6addr - Remove the IPv6 address from interface
1547  */
1548 int
1549 cif6addr(u, o, h)
1550     int u;
1551     eui64_t o, h;
1552 {
1553     return 1;
1554 }
1555
1556 #endif /* defined(SOL2) && defined(INET6) */
1557
1558
1559 #define INET_ADDR(x)    (((struct sockaddr_in *) &(x))->sin_addr.s_addr)
1560
1561 /*
1562  * sifaddr - Config the interface IP addresses and netmask.
1563  */
1564 int
1565 sifaddr(u, o, h, m)
1566     int u;
1567     u_int32_t o, h, m;
1568 {
1569     struct ifreq ifr;
1570     int ret = 1;
1571
1572     memset(&ifr, 0, sizeof(ifr));
1573     strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1574     ifr.ifr_addr.sa_family = AF_INET;
1575     INET_ADDR(ifr.ifr_addr) = m;
1576     if (ioctl(ipfd, SIOCSIFNETMASK, &ifr) < 0) {
1577         error("Couldn't set IP netmask: %m");
1578         ret = 0;
1579     }
1580     ifr.ifr_addr.sa_family = AF_INET;
1581     INET_ADDR(ifr.ifr_addr) = o;
1582     if (ioctl(ipfd, SIOCSIFADDR, &ifr) < 0) {
1583         error("Couldn't set local IP address: %m");
1584         ret = 0;
1585     }
1586
1587     /*
1588      * On some systems, we have to explicitly set the point-to-point
1589      * flag bit before we can set a destination address.
1590      */
1591     if (ioctl(ipfd, SIOCGIFFLAGS, &ifr) >= 0
1592         && (ifr.ifr_flags & IFF_POINTOPOINT) == 0) {
1593         ifr.ifr_flags |= IFF_POINTOPOINT;
1594         if (ioctl(ipfd, SIOCSIFFLAGS, &ifr) < 0) {
1595             error("Couldn't mark interface pt-to-pt: %m");
1596             ret = 0;
1597         }
1598     }
1599     ifr.ifr_dstaddr.sa_family = AF_INET;
1600     INET_ADDR(ifr.ifr_dstaddr) = h;
1601     if (ioctl(ipfd, SIOCSIFDSTADDR, &ifr) < 0) {
1602         error("Couldn't set remote IP address: %m");
1603         ret = 0;
1604     }
1605 #if 0   /* now done in ppp_send_config */
1606     ifr.ifr_metric = link_mtu;
1607     if (ioctl(ipfd, SIOCSIFMTU, &ifr) < 0) {
1608         error("Couldn't set IP MTU: %m");
1609     }
1610 #endif
1611
1612     remote_addr = h;
1613     return ret;
1614 }
1615
1616 /*
1617  * cifaddr - Clear the interface IP addresses, and delete routes
1618  * through the interface if possible.
1619  */
1620 int
1621 cifaddr(u, o, h)
1622     int u;
1623     u_int32_t o, h;
1624 {
1625 #if defined(__USLC__)           /* was: #if 0 */
1626     cifroute(unit, ouraddr, hisaddr);
1627     if (ipmuxid >= 0) {
1628         notice("Removing ppp interface unit");
1629         if (ioctl(ipfd, I_UNLINK, ipmuxid) < 0) {
1630             error("Can't remove ppp interface unit: %m");
1631             return 0;
1632         }
1633         ipmuxid = -1;
1634     }
1635 #endif
1636     remote_addr = 0;
1637     return 1;
1638 }
1639
1640 /*
1641  * sifdefaultroute - assign a default route through the address given.
1642  */
1643 int
1644 sifdefaultroute(u, l, g)
1645     int u;
1646     u_int32_t l, g;
1647 {
1648     struct rtentry rt;
1649
1650 #if defined(__USLC__)
1651     g = l;                      /* use the local address as gateway */
1652 #endif
1653     memset(&rt, 0, sizeof(rt));
1654     rt.rt_dst.sa_family = AF_INET;
1655     INET_ADDR(rt.rt_dst) = 0;
1656     rt.rt_gateway.sa_family = AF_INET;
1657     INET_ADDR(rt.rt_gateway) = g;
1658     rt.rt_flags = RTF_GATEWAY;
1659
1660     if (ioctl(ipfd, SIOCADDRT, &rt) < 0) {
1661         error("Can't add default route: %m");
1662         return 0;
1663     }
1664
1665     default_route_gateway = g;
1666     return 1;
1667 }
1668
1669 /*
1670  * cifdefaultroute - delete a default route through the address given.
1671  */
1672 int
1673 cifdefaultroute(u, l, g)
1674     int u;
1675     u_int32_t l, g;
1676 {
1677     struct rtentry rt;
1678
1679 #if defined(__USLC__)
1680     g = l;                      /* use the local address as gateway */
1681 #endif
1682     memset(&rt, 0, sizeof(rt));
1683     rt.rt_dst.sa_family = AF_INET;
1684     INET_ADDR(rt.rt_dst) = 0;
1685     rt.rt_gateway.sa_family = AF_INET;
1686     INET_ADDR(rt.rt_gateway) = g;
1687     rt.rt_flags = RTF_GATEWAY;
1688
1689     if (ioctl(ipfd, SIOCDELRT, &rt) < 0) {
1690         error("Can't delete default route: %m");
1691         return 0;
1692     }
1693
1694     default_route_gateway = 0;
1695     return 1;
1696 }
1697
1698 /*
1699  * sifproxyarp - Make a proxy ARP entry for the peer.
1700  */
1701 int
1702 sifproxyarp(unit, hisaddr)
1703     int unit;
1704     u_int32_t hisaddr;
1705 {
1706     struct arpreq arpreq;
1707
1708     memset(&arpreq, 0, sizeof(arpreq));
1709     if (!get_ether_addr(hisaddr, &arpreq.arp_ha))
1710         return 0;
1711
1712     arpreq.arp_pa.sa_family = AF_INET;
1713     INET_ADDR(arpreq.arp_pa) = hisaddr;
1714     arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1715     if (ioctl(ipfd, SIOCSARP, (caddr_t) &arpreq) < 0) {
1716         error("Couldn't set proxy ARP entry: %m");
1717         return 0;
1718     }
1719
1720     proxy_arp_addr = hisaddr;
1721     return 1;
1722 }
1723
1724 /*
1725  * cifproxyarp - Delete the proxy ARP entry for the peer.
1726  */
1727 int
1728 cifproxyarp(unit, hisaddr)
1729     int unit;
1730     u_int32_t hisaddr;
1731 {
1732     struct arpreq arpreq;
1733
1734     memset(&arpreq, 0, sizeof(arpreq));
1735     arpreq.arp_pa.sa_family = AF_INET;
1736     INET_ADDR(arpreq.arp_pa) = hisaddr;
1737     if (ioctl(ipfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1738         error("Couldn't delete proxy ARP entry: %m");
1739         return 0;
1740     }
1741
1742     proxy_arp_addr = 0;
1743     return 1;
1744 }
1745
1746 /*
1747  * get_ether_addr - get the hardware address of an interface on the
1748  * the same subnet as ipaddr.
1749  */
1750 #define MAX_IFS         32
1751
1752 static int
1753 get_ether_addr(ipaddr, hwaddr)
1754     u_int32_t ipaddr;
1755     struct sockaddr *hwaddr;
1756 {
1757     struct ifreq *ifr, *ifend, ifreq;
1758     int nif;
1759     struct ifconf ifc;
1760     u_int32_t ina, mask;
1761
1762     /*
1763      * Scan through the system's network interfaces.
1764      */
1765 #ifdef SIOCGIFNUM
1766     if (ioctl(ipfd, SIOCGIFNUM, &nif) < 0)
1767 #endif
1768         nif = MAX_IFS;
1769     ifc.ifc_len = nif * sizeof(struct ifreq);
1770     ifc.ifc_buf = (caddr_t) malloc(ifc.ifc_len);
1771     if (ifc.ifc_buf == 0)
1772         return 0;
1773     if (ioctl(ipfd, SIOCGIFCONF, &ifc) < 0) {
1774         warn("Couldn't get system interface list: %m");
1775         free(ifc.ifc_buf);
1776         return 0;
1777     }
1778     ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1779     for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
1780         if (ifr->ifr_addr.sa_family != AF_INET)
1781             continue;
1782         /*
1783          * Check that the interface is up, and not point-to-point or loopback.
1784          */
1785         strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1786         if (ioctl(ipfd, SIOCGIFFLAGS, &ifreq) < 0)
1787             continue;
1788         if ((ifreq.ifr_flags &
1789              (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1790             != (IFF_UP|IFF_BROADCAST))
1791             continue;
1792         /*
1793          * Get its netmask and check that it's on the right subnet.
1794          */
1795         if (ioctl(ipfd, SIOCGIFNETMASK, &ifreq) < 0)
1796             continue;
1797         ina = INET_ADDR(ifr->ifr_addr);
1798         mask = INET_ADDR(ifreq.ifr_addr);
1799         if ((ipaddr & mask) == (ina & mask))
1800             break;
1801     }
1802
1803     if (ifr >= ifend) {
1804         warn("No suitable interface found for proxy ARP");
1805         free(ifc.ifc_buf);
1806         return 0;
1807     }
1808
1809     info("found interface %s for proxy ARP", ifr->ifr_name);
1810     if (!get_hw_addr(ifr->ifr_name, ina, hwaddr)) {
1811         error("Couldn't get hardware address for %s", ifr->ifr_name);
1812         free(ifc.ifc_buf);
1813         return 0;
1814     }
1815
1816     free(ifc.ifc_buf);
1817     return 1;
1818 }
1819
1820 /*
1821  * get_hw_addr_dlpi - obtain the hardware address using DLPI
1822  */
1823 static int
1824 get_hw_addr_dlpi(name, hwaddr)
1825     char *name;
1826     struct sockaddr *hwaddr;
1827 {
1828     char *p, *q;
1829     int unit, iffd, adrlen;
1830     unsigned char *adrp;
1831     char ifdev[24];
1832     struct {
1833         union DL_primitives prim;
1834         char space[64];
1835     } reply;
1836
1837     /*
1838      * We have to open the device and ask it for its hardware address.
1839      * First split apart the device name and unit.
1840      */
1841     slprintf(ifdev, sizeof(ifdev), "/dev/%s", name);
1842     for (q = ifdev + strlen(ifdev); --q >= ifdev; )
1843         if (!isdigit(*q))
1844             break;
1845     unit = atoi(q+1);
1846     q[1] = 0;
1847
1848     /*
1849      * Open the device and do a DLPI attach and phys_addr_req.
1850      */
1851     iffd = open(ifdev, O_RDWR);
1852     if (iffd < 0) {
1853         error("Can't open %s: %m", ifdev);
1854         return 0;
1855     }
1856     if (dlpi_attach(iffd, unit) < 0
1857         || dlpi_get_reply(iffd, &reply.prim, DL_OK_ACK, sizeof(reply)) < 0
1858         || dlpi_info_req(iffd) < 0
1859         || dlpi_get_reply(iffd, &reply.prim, DL_INFO_ACK, sizeof(reply)) < 0) {
1860         close(iffd);
1861         return 0;
1862     }
1863
1864     adrlen = reply.prim.info_ack.dl_addr_length;
1865     adrp = (unsigned char *)&reply + reply.prim.info_ack.dl_addr_offset;
1866
1867 #if DL_CURRENT_VERSION >= 2
1868     if (reply.prim.info_ack.dl_sap_length < 0)
1869         adrlen += reply.prim.info_ack.dl_sap_length;
1870     else
1871         adrp += reply.prim.info_ack.dl_sap_length;
1872 #endif
1873
1874     hwaddr->sa_family = AF_UNSPEC;
1875     memcpy(hwaddr->sa_data, adrp, adrlen);
1876
1877     return 1;
1878 }
1879 /*
1880  * get_hw_addr - obtain the hardware address for a named interface.
1881  */
1882 static int
1883 get_hw_addr(name, ina, hwaddr)
1884     char *name;
1885     u_int32_t ina;
1886     struct sockaddr *hwaddr;
1887 {
1888     /* New way - get the address by doing an arp request. */
1889     int s;
1890     struct arpreq req;
1891
1892     s = socket(AF_INET, SOCK_DGRAM, 0);
1893     if (s < 0)
1894         return 0;
1895     memset(&req, 0, sizeof(req));
1896     req.arp_pa.sa_family = AF_INET;
1897     INET_ADDR(req.arp_pa) = ina;
1898     if (ioctl(s, SIOCGARP, &req) < 0) {
1899         error("Couldn't get ARP entry for %s: %m", ip_ntoa(ina));
1900         return 0;
1901     }
1902     *hwaddr = req.arp_ha;
1903     hwaddr->sa_family = AF_UNSPEC;
1904
1905     return 1;
1906 }
1907
1908 static int
1909 dlpi_attach(fd, ppa)
1910     int fd, ppa;
1911 {
1912     dl_attach_req_t req;
1913     struct strbuf buf;
1914
1915     req.dl_primitive = DL_ATTACH_REQ;
1916     req.dl_ppa = ppa;
1917     buf.len = sizeof(req);
1918     buf.buf = (void *) &req;
1919     return putmsg(fd, &buf, NULL, RS_HIPRI);
1920 }
1921
1922 static int
1923 dlpi_info_req(fd)
1924     int fd;
1925 {
1926     dl_info_req_t req;
1927     struct strbuf buf;
1928
1929     req.dl_primitive = DL_INFO_REQ;
1930     buf.len = sizeof(req);
1931     buf.buf = (void *) &req;
1932     return putmsg(fd, &buf, NULL, RS_HIPRI);
1933 }
1934
1935 static int
1936 dlpi_get_reply(fd, reply, expected_prim, maxlen)
1937     union DL_primitives *reply;
1938     int fd, expected_prim, maxlen;
1939 {
1940     struct strbuf buf;
1941     int flags, n;
1942     struct pollfd pfd;
1943
1944     /*
1945      * Use poll to wait for a message with a timeout.
1946      */
1947     pfd.fd = fd;
1948     pfd.events = POLLIN | POLLPRI;
1949     do {
1950         n = poll(&pfd, 1, 1000);
1951     } while (n == -1 && errno == EINTR);
1952     if (n <= 0)
1953         return -1;
1954
1955     /*
1956      * Get the reply.
1957      */
1958     buf.maxlen = maxlen;
1959     buf.buf = (void *) reply;
1960     flags = 0;
1961     if (getmsg(fd, &buf, NULL, &flags) < 0)
1962         return -1;
1963
1964     if (buf.len < sizeof(ulong)) {
1965         if (debug)
1966             dbglog("dlpi response short (len=%d)\n", buf.len);
1967         return -1;
1968     }
1969
1970     if (reply->dl_primitive == expected_prim)
1971         return 0;
1972
1973     if (debug) {
1974         if (reply->dl_primitive == DL_ERROR_ACK) {
1975             dbglog("dlpi error %d (unix errno %d) for prim %x\n",
1976                    reply->error_ack.dl_errno, reply->error_ack.dl_unix_errno,
1977                    reply->error_ack.dl_error_primitive);
1978         } else {
1979             dbglog("dlpi unexpected response prim %x\n",
1980                    reply->dl_primitive);
1981         }
1982     }
1983
1984     return -1;
1985 }
1986
1987 /*
1988  * Return user specified netmask, modified by any mask we might determine
1989  * for address `addr' (in network byte order).
1990  * Here we scan through the system's list of interfaces, looking for
1991  * any non-point-to-point interfaces which might appear to be on the same
1992  * network as `addr'.  If we find any, we OR in their netmask to the
1993  * user-specified netmask.
1994  */
1995 u_int32_t
1996 GetMask(addr)
1997     u_int32_t addr;
1998 {
1999     u_int32_t mask, nmask, ina;
2000     struct ifreq *ifr, *ifend, ifreq;
2001     int nif;
2002     struct ifconf ifc;
2003
2004     addr = ntohl(addr);
2005     if (IN_CLASSA(addr))        /* determine network mask for address class */
2006         nmask = IN_CLASSA_NET;
2007     else if (IN_CLASSB(addr))
2008         nmask = IN_CLASSB_NET;
2009     else
2010         nmask = IN_CLASSC_NET;
2011     /* class D nets are disallowed by bad_ip_adrs */
2012     mask = netmask | htonl(nmask);
2013
2014     /*
2015      * Scan through the system's network interfaces.
2016      */
2017 #ifdef SIOCGIFNUM
2018     if (ioctl(ipfd, SIOCGIFNUM, &nif) < 0)
2019 #endif
2020         nif = MAX_IFS;
2021     ifc.ifc_len = nif * sizeof(struct ifreq);
2022     ifc.ifc_buf = (caddr_t) malloc(ifc.ifc_len);
2023     if (ifc.ifc_buf == 0)
2024         return mask;
2025     if (ioctl(ipfd, SIOCGIFCONF, &ifc) < 0) {
2026         warn("Couldn't get system interface list: %m");
2027         free(ifc.ifc_buf);
2028         return mask;
2029     }
2030     ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
2031     for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
2032         /*
2033          * Check the interface's internet address.
2034          */
2035         if (ifr->ifr_addr.sa_family != AF_INET)
2036             continue;
2037         ina = INET_ADDR(ifr->ifr_addr);
2038         if ((ntohl(ina) & nmask) != (addr & nmask))
2039             continue;
2040         /*
2041          * Check that the interface is up, and not point-to-point or loopback.
2042          */
2043         strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
2044         if (ioctl(ipfd, SIOCGIFFLAGS, &ifreq) < 0)
2045             continue;
2046         if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
2047             != IFF_UP)
2048             continue;
2049         /*
2050          * Get its netmask and OR it into our mask.
2051          */
2052         if (ioctl(ipfd, SIOCGIFNETMASK, &ifreq) < 0)
2053             continue;
2054         mask |= INET_ADDR(ifreq.ifr_addr);
2055     }
2056
2057     free(ifc.ifc_buf);
2058     return mask;
2059 }
2060
2061 /*
2062  * logwtmp - write an accounting record to the /var/adm/wtmp file.
2063  */
2064 void
2065 logwtmp(line, name, host)
2066     const char *line, *name, *host;
2067 {
2068     static struct utmpx utmpx;
2069
2070     if (name[0] != 0) {
2071         /* logging in */
2072         strncpy(utmpx.ut_user, name, sizeof(utmpx.ut_user));
2073         strncpy(utmpx.ut_id, ifname, sizeof(utmpx.ut_id));
2074         strncpy(utmpx.ut_line, line, sizeof(utmpx.ut_line));
2075         utmpx.ut_pid = getpid();
2076         utmpx.ut_type = USER_PROCESS;
2077     } else {
2078         utmpx.ut_type = DEAD_PROCESS;
2079     }
2080     gettimeofday(&utmpx.ut_tv, NULL);
2081     updwtmpx("/var/adm/wtmpx", &utmpx);
2082 }
2083
2084 /*
2085  * get_host_seed - return the serial number of this machine.
2086  */
2087 int
2088 get_host_seed()
2089 {
2090     char buf[32];
2091
2092     if (sysinfo(SI_HW_SERIAL, buf, sizeof(buf)) < 0) {
2093         error("sysinfo: %m");
2094         return 0;
2095     }
2096     return (int) strtoul(buf, NULL, 16);
2097 }
2098
2099 static int
2100 strioctl(fd, cmd, ptr, ilen, olen)
2101     int fd, cmd, ilen, olen;
2102     void *ptr;
2103 {
2104     struct strioctl str;
2105
2106     str.ic_cmd = cmd;
2107     str.ic_timout = 0;
2108     str.ic_len = ilen;
2109     str.ic_dp = ptr;
2110     if (ioctl(fd, I_STR, &str) == -1)
2111         return -1;
2112     if (str.ic_len != olen)
2113         dbglog("strioctl: expected %d bytes, got %d for cmd %x\n",
2114                olen, str.ic_len, cmd);
2115     return 0;
2116 }
2117
2118 #if 0
2119 /*
2120  * lock - create a lock file for the named lock device
2121  */
2122
2123 #define LOCK_PREFIX     "/var/spool/locks/LK."
2124 static char lock_file[40];      /* name of lock file created */
2125
2126 int
2127 lock(dev)
2128     char *dev;
2129 {
2130     int n, fd, pid;
2131     struct stat sbuf;
2132     char ascii_pid[12];
2133
2134     if (stat(dev, &sbuf) < 0) {
2135         error("Can't get device number for %s: %m", dev);
2136         return -1;
2137     }
2138     if ((sbuf.st_mode & S_IFMT) != S_IFCHR) {
2139         error("Can't lock %s: not a character device", dev);
2140         return -1;
2141     }
2142     slprintf(lock_file, sizeof(lock_file), "%s%03d.%03d.%03d",
2143              LOCK_PREFIX, major(sbuf.st_dev),
2144              major(sbuf.st_rdev), minor(sbuf.st_rdev));
2145
2146     while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
2147         if (errno == EEXIST
2148             && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
2149             /* Read the lock file to find out who has the device locked */
2150             n = read(fd, ascii_pid, 11);
2151             if (n <= 0) {
2152                 error("Can't read pid from lock file %s", lock_file);
2153                 close(fd);
2154             } else {
2155                 ascii_pid[n] = 0;
2156                 pid = atoi(ascii_pid);
2157                 if (pid > 0 && kill(pid, 0) == -1 && errno == ESRCH) {
2158                     /* pid no longer exists - remove the lock file */
2159                     if (unlink(lock_file) == 0) {
2160                         close(fd);
2161                         notice("Removed stale lock on %s (pid %d)",
2162                                dev, pid);
2163                         continue;
2164                     } else
2165                         warn("Couldn't remove stale lock on %s",
2166                                dev);
2167                 } else
2168                     notice("Device %s is locked by pid %d",
2169                            dev, pid);
2170             }
2171             close(fd);
2172         } else
2173             error("Can't create lock file %s: %m", lock_file);
2174         lock_file[0] = 0;
2175         return -1;
2176     }
2177
2178     slprintf(ascii_pid, sizeof(ascii_pid), "%10d\n", getpid());
2179     write(fd, ascii_pid, 11);
2180
2181     close(fd);
2182     return 1;
2183 }
2184
2185 /*
2186  * unlock - remove our lockfile
2187  */
2188 void
2189 unlock()
2190 {
2191     if (lock_file[0]) {
2192         unlink(lock_file);
2193         lock_file[0] = 0;
2194     }
2195 }
2196 #endif
2197
2198 /*
2199  * cifroute - delete a route through the addresses given.
2200  */
2201 int
2202 cifroute(u, our, his)
2203     int u;
2204     u_int32_t our, his;
2205 {
2206     struct rtentry rt;
2207
2208     memset(&rt, 0, sizeof(rt));
2209     rt.rt_dst.sa_family = AF_INET;
2210     INET_ADDR(rt.rt_dst) = his;
2211     rt.rt_gateway.sa_family = AF_INET;
2212     INET_ADDR(rt.rt_gateway) = our;
2213     rt.rt_flags = RTF_HOST;
2214
2215     if (ioctl(ipfd, SIOCDELRT, &rt) < 0) {
2216         error("Can't delete route: %m");
2217         return 0;
2218     }
2219
2220     return 1;
2221 }
2222
2223 /*
2224  * have_route_to - determine if the system has a route to the specified
2225  * IP address.  Returns 0 if not, 1 if so, -1 if we can't tell.
2226  * `addr' is in network byte order.
2227  * For demand mode to work properly, we have to ignore routes
2228  * through our own interface.
2229  */
2230 #ifndef T_CURRENT               /* needed for Solaris 2.5 */
2231 #define T_CURRENT       MI_T_CURRENT
2232 #endif
2233
2234 int
2235 have_route_to(addr)
2236     u_int32_t addr;
2237 {
2238 #ifdef SOL2
2239     int fd, r, flags, i;
2240     struct {
2241         struct T_optmgmt_req req;
2242         struct opthdr hdr;
2243     } req;
2244     union {
2245         struct T_optmgmt_ack ack;
2246         unsigned char space[64];
2247     } ack;
2248     struct opthdr *rh;
2249     struct strbuf cbuf, dbuf;
2250     int nroutes;
2251     mib2_ipRouteEntry_t routes[8];
2252     mib2_ipRouteEntry_t *rp;
2253
2254     fd = open("/dev/ip", O_RDWR);
2255     if (fd < 0) {
2256         warn("have_route_to: couldn't open /dev/ip: %m");
2257         return -1;
2258     }
2259
2260     req.req.PRIM_type = T_OPTMGMT_REQ;
2261     req.req.OPT_offset = (char *) &req.hdr - (char *) &req;
2262     req.req.OPT_length = sizeof(req.hdr);
2263     req.req.MGMT_flags = T_CURRENT;
2264
2265     req.hdr.level = MIB2_IP;
2266     req.hdr.name = 0;
2267     req.hdr.len = 0;
2268
2269     cbuf.buf = (char *) &req;
2270     cbuf.len = sizeof(req);
2271
2272     if (putmsg(fd, &cbuf, NULL, 0) == -1) {
2273         warn("have_route_to: putmsg: %m");
2274         close(fd);
2275         return -1;
2276     }
2277
2278     for (;;) {
2279         cbuf.buf = (char *) &ack;
2280         cbuf.maxlen = sizeof(ack);
2281         dbuf.buf = (char *) routes;
2282         dbuf.maxlen = sizeof(routes);
2283         flags = 0;
2284         r = getmsg(fd, &cbuf, &dbuf, &flags);
2285         if (r == -1) {
2286             warn("have_route_to: getmsg: %m");
2287             close(fd);
2288             return -1;
2289         }
2290
2291         if (cbuf.len < sizeof(struct T_optmgmt_ack)
2292             || ack.ack.PRIM_type != T_OPTMGMT_ACK
2293             || ack.ack.MGMT_flags != T_SUCCESS
2294             || ack.ack.OPT_length < sizeof(struct opthdr)) {
2295             dbglog("have_route_to: bad message len=%d prim=%d",
2296                    cbuf.len, ack.ack.PRIM_type);
2297             close(fd);
2298             return -1;
2299         }
2300
2301         rh = (struct opthdr *) ((char *)&ack + ack.ack.OPT_offset);
2302         if (rh->level == 0 && rh->name == 0)
2303             break;
2304         if (rh->level != MIB2_IP || rh->name != MIB2_IP_21) {
2305             while (r == MOREDATA)
2306                 r = getmsg(fd, NULL, &dbuf, &flags);
2307             continue;
2308         }
2309
2310         for (;;) {
2311             nroutes = dbuf.len / sizeof(mib2_ipRouteEntry_t);
2312             for (rp = routes, i = 0; i < nroutes; ++i, ++rp) {
2313                 if (rp->ipRouteMask != ~0) {
2314                     dbglog("have_route_to: dest=%x gw=%x mask=%x\n",
2315                            rp->ipRouteDest, rp->ipRouteNextHop,
2316                            rp->ipRouteMask);
2317                     if (((addr ^ rp->ipRouteDest) & rp->ipRouteMask) == 0
2318                         && rp->ipRouteNextHop != remote_addr)
2319                         return 1;
2320                 }
2321             }
2322             if (r == 0)
2323                 break;
2324             r = getmsg(fd, NULL, &dbuf, &flags);
2325         }
2326     }
2327     close(fd);
2328     return 0;
2329 #else
2330     return -1;
2331 #endif /* SOL2 */
2332 }
2333
2334 /*
2335  * get_pty - get a pty master/slave pair and chown the slave side to
2336  * the uid given.  Assumes slave_name points to MAXPATHLEN bytes of space.
2337  */
2338 int
2339 get_pty(master_fdp, slave_fdp, slave_name, uid)
2340     int *master_fdp;
2341     int *slave_fdp;
2342     char *slave_name;
2343     int uid;
2344 {
2345     int mfd, sfd;
2346     char *pty_name;
2347     struct termios tios;
2348
2349     mfd = open("/dev/ptmx", O_RDWR);
2350     if (mfd < 0) {
2351         error("Couldn't open pty master: %m");
2352         return 0;
2353     }
2354
2355     pty_name = ptsname(mfd);
2356     if (pty_name == NULL) {
2357         error("Couldn't get name of pty slave");
2358         close(mfd);
2359         return 0;
2360     }
2361     if (chown(pty_name, uid, -1) < 0)
2362         warn("Couldn't change owner of pty slave: %m");
2363     if (chmod(pty_name, S_IRUSR | S_IWUSR) < 0)
2364         warn("Couldn't change permissions on pty slave: %m");
2365     if (unlockpt(mfd) < 0)
2366         warn("Couldn't unlock pty slave: %m");
2367
2368     sfd = open(pty_name, O_RDWR);
2369     if (sfd < 0) {
2370         error("Couldn't open pty slave %s: %m", pty_name);
2371         close(mfd);
2372         return 0;
2373     }
2374     if (ioctl(sfd, I_PUSH, "ptem") < 0)
2375         warn("Couldn't push ptem module on pty slave: %m");
2376
2377     dbglog("Using %s", pty_name);
2378     strlcpy(slave_name, pty_name, MAXPATHLEN);
2379     *master_fdp = mfd;
2380     *slave_fdp = sfd;
2381
2382     return 1;
2383 }