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