2 * System-dependent procedures for pppd under Solaris 2.x (SunOS 5.x).
4 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
5 * Use is subject to license terms.
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.
11 * SUN MAKES NO REPRESENTATION OR WARRANTIES ABOUT THE SUITABILITY OF
12 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
13 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
14 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
15 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
16 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES
18 * Copyright (c) 1994 The Australian National University.
19 * All rights reserved.
21 * Permission to use, copy, modify, and distribute this software and its
22 * documentation is hereby granted, provided that the above copyright
23 * notice appears in all copies. This software is provided without any
24 * warranty, express or implied. The Australian National University
25 * makes no representations about the suitability of this software for
28 * IN NO EVENT SHALL THE AUSTRALIAN NATIONAL UNIVERSITY BE LIABLE TO ANY
29 * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
30 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
31 * THE AUSTRALIAN NATIONAL UNIVERSITY HAVE BEEN ADVISED OF THE POSSIBILITY
34 * THE AUSTRALIAN NATIONAL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
35 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
36 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
37 * ON AN "AS IS" BASIS, AND THE AUSTRALIAN NATIONAL UNIVERSITY HAS NO
38 * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
56 #include <sys/types.h>
57 #include <sys/ioccom.h>
58 #include <sys/stream.h>
59 #include <sys/stropts.h>
60 #include <sys/socket.h>
61 #include <sys/sockio.h>
62 #include <sys/sysmacros.h>
63 #include <sys/systeminfo.h>
66 #include <net/if_arp.h>
67 #include <net/route.h>
68 #include <net/ppp_defs.h>
69 #include <net/pppio.h>
70 #include <net/if_types.h>
71 #include <net/if_dl.h>
72 #include <netinet/in.h>
73 #include <sys/tihdr.h>
74 #include <inet/mib2.h>
76 #include <sys/ethernet.h>
77 #include <sys/ser_sync.h>
79 #include <arpa/inet.h>
90 #define PPPSTRTIMOUT 1 /* Timeout in seconds for ioctl */
91 #define MAX_POLLFDS 32
99 #define _IN6_LLX_FROM_EUI64(l, s, eui64, as, len) \
100 (s->sin6_addr.s6_addr32[0] = htonl(as), \
101 eui64_copy(eui64, s->sin6_addr.s6_addr32[2]), \
102 s->sin6_family = AF_INET6, \
103 l.lifr_addr.ss_family = AF_INET6, \
104 l.lifr_addrlen = len, \
108 * Generate a link-local address with an interface-id based on the given
109 * EUI64 identifier. Note that the len field is unused by SIOCSLIFADDR.
111 #define IN6_LLADDR_FROM_EUI64(l, s, eui64) \
112 _IN6_LLX_FROM_EUI64(l, s, eui64, 0xfe800000, 0)
115 * Generate an EUI64 based interface-id for use by stateless address
116 * autoconfiguration. These are required to be 64 bits long as defined in
117 * the "Interface Identifiers" section of the IPv6 Addressing Architecture
120 #define IN6_LLTOKEN_FROM_EUI64(l, s, eui64) \
121 _IN6_LLX_FROM_EUI64(l, s, eui64, 0, 64)
124 #define IPCP_ENABLED ipcp_protent.enabled_flag
126 #define IPV6CP_ENABLED ipv6cp_protent.enabled_flag
129 /* For plug-in usage. */
130 int (*sys_read_packet_hook
) __P((int retv
, struct strbuf
*ctrl
,
131 struct strbuf
*data
, int flags
)) = NULL
;
132 bool already_ppp
= 0; /* Already in PPP mode */
134 static int pppfd
= -1; /* ppp driver fd */
135 static int fdmuxid
= -1; /* driver mux fd */
136 static int ipfd
= -1; /* IPv4 fd */
137 static int ipmuxid
= -1; /* IPv4 mux fd */
138 static int ip6fd
= -1; /* IPv6 fd */
139 static int ip6muxid
= -1; /* IPv6 mux fd */
140 static bool if6_is_up
= 0; /* IPv6 if marked as up */
141 static bool if_is_up
= 0; /* IPv4 if marked as up */
142 static bool restore_term
= 0; /* Restore TTY after closing link */
143 static struct termios inittermios
; /* TTY settings */
144 static struct winsize wsinfo
; /* Initial window size info */
145 static pid_t tty_sid
; /* original sess ID for term */
146 static struct pollfd pollfds
[MAX_POLLFDS
]; /* array of polled fd */
147 static int n_pollfds
= 0; /* total count of polled fd */
148 static int link_mtu
; /* link Maximum Transmit Unit */
149 static int tty_nmodules
; /* total count of TTY modules used */
150 static char tty_modules
[NMODULES
][FMNAMESZ
+1];
151 /* array of TTY modules used */
152 static int tty_npushed
; /* total count of pushed PPP modules */
153 static u_int32_t remote_addr
; /* IP address of peer */
154 static u_int32_t default_route_gateway
; /* Gateway for default route */
155 static u_int32_t proxy_arp_addr
; /* Addr for proxy arp entry */
156 static u_int32_t lastlink_status
; /* Last link status info */
158 static bool use_plink
= 0; /* Use I_LINK by default */
159 static bool plumbed
= 0; /* Use existing interface */
161 /* Default is to use /dev/sppp as driver. */
162 static const char *drvnam
= PPP_DEV_NAME
;
163 static bool integrated_driver
= 0;
164 static int extra_dev_fd
= -1; /* keep open until ready */
166 static option_t solaris_option_list
[] = {
167 { "plink", o_bool
, &use_plink
, "Use I_PLINK instead of I_LINK",
169 { "noplink", o_bool
, &use_plink
, "Use I_LINK instead of I_PLINK",
171 { "plumbed", o_bool
, &plumbed
, "Use pre-plumbed interface",
177 * Prototypes for procedures local to this file.
179 static int translate_speed
__P((int));
180 static int baud_rate_of
__P((int));
181 static int get_ether_addr
__P((u_int32_t
, struct sockaddr_dl
*, int));
182 static int strioctl
__P((int, int, void *, int, int));
183 static int plumb_ipif
__P((int));
184 static int unplumb_ipif
__P((int));
186 static int plumb_ip6if
__P((int));
187 static int unplumb_ip6if
__P((int));
188 static int open_ip6fd(void);
190 static int open_ipfd(void);
191 static int sifroute
__P((int, u_int32_t
, u_int32_t
, int, const char *));
192 static int giflags
__P((u_int32_t
, bool *));
193 static void handle_unbind
__P((u_int32_t
));
194 static void handle_bind
__P((u_int32_t
));
197 * Wrapper for regular ioctl; masks out EINTR.
200 myioctl(int fd
, int cmd
, void *arg
)
205 while ((retv
= ioctl(fd
, cmd
, arg
)) == -1) {
213 * sys_check_options()
215 * Check the options that the user specified.
218 sys_check_options(void)
232 * Add or remove system-specific options.
237 (void) remove_option("ktune");
238 (void) remove_option("noktune");
239 add_options(solaris_option_list
);
245 * Set ifname[] to contain name of IP interface for this unit.
252 if ((cp
= strrchr(drvnam
, '/')) == NULL
)
256 (void) slprintf(ifname
, sizeof (ifname
), "%s%d", cp
, ifunit
);
262 * Check whether the system has any ppp interfaces.
271 if (stat(PPP_DEV_NAME
, &buf
) >= 0)
275 * Simple check for system using Apollo POS without SUNWpppd
276 * (/dev/sppp) installed. This is intentionally not kept open
277 * here, since the user may not have the same privileges (as
278 * determined later). If Apollo were just shipped with the
279 * full complement of packages, this wouldn't be an issue.
281 if (devnam
[0] == '\0' &&
282 (fd
= open(devnam
, O_RDWR
| O_NONBLOCK
| O_NOCTTY
)) >= 0) {
283 if (strioctl(fd
, PPPIO_GTYPE
, &typ
, 0, sizeof (typ
)) >= 0 &&
296 ipfd
= open(IP_DEV_NAME
, O_RDWR
| O_NONBLOCK
, 0);
298 error("Couldn't open IP device (%s): %m", IP_DEV_NAME
);
304 read_ip_interface(int unit
)
307 struct sockaddr_in sin
;
309 if (ipfd
== -1 && open_ipfd() == -1)
312 BZERO(&ifr
, sizeof (ifr
));
313 (void) strlcpy(ifr
.ifr_name
, ifname
, sizeof (ifr
.ifr_name
));
315 /* Get the existing MTU */
316 if (myioctl(ipfd
, SIOCGIFMTU
, &ifr
) < 0) {
317 warn("Couldn't get IP MTU on %s: %m", ifr
.ifr_name
);
320 dbglog("got MTU %d from interface", ifr
.ifr_metric
);
321 if (ifr
.ifr_metric
!= 0 &&
322 (lcp_allowoptions
[unit
].mru
== 0 ||
323 lcp_allowoptions
[unit
].mru
> ifr
.ifr_metric
))
324 lcp_allowoptions
[unit
].mru
= ifr
.ifr_metric
;
326 /* Get the local IP address */
327 if (ipcp_wantoptions
[unit
].ouraddr
== 0 ||
328 ipcp_from_hostname
) {
329 if (myioctl(ipfd
, SIOCGIFADDR
, &ifr
) < 0) {
330 warn("Couldn't get local IP address (%s): %m",
334 BCOPY(&ifr
.ifr_addr
, &sin
, sizeof (struct sockaddr_in
));
335 ipcp_wantoptions
[unit
].ouraddr
= sin
.sin_addr
.s_addr
;
336 dbglog("got local address %I from interface",
337 ipcp_wantoptions
[unit
].ouraddr
);
340 /* Get the remote IP address */
341 if (ipcp_wantoptions
[unit
].hisaddr
== 0) {
342 if (myioctl(ipfd
, SIOCGIFDSTADDR
, &ifr
) < 0) {
343 warn("Couldn't get remote IP address (%s): %m",
347 BCOPY(&ifr
.ifr_dstaddr
, &sin
, sizeof (struct sockaddr_in
));
348 ipcp_wantoptions
[unit
].hisaddr
= sin
.sin_addr
.s_addr
;
349 dbglog("got remote address %I from interface",
350 ipcp_wantoptions
[unit
].hisaddr
);
359 ip6fd
= open(IP6_DEV_NAME
, O_RDWR
| O_NONBLOCK
, 0);
361 error("Couldn't open IPv6 device (%s): %m", IP6_DEV_NAME
);
367 read_ipv6_interface(int unit
)
370 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)&lifr
.lifr_addr
;
372 if (ip6fd
== -1 && open_ip6fd() == -1)
375 BZERO(&lifr
, sizeof (lifr
));
376 (void) strlcpy(lifr
.lifr_name
, ifname
, sizeof (lifr
.lifr_name
));
378 /* Get the existing MTU */
379 if (myioctl(ip6fd
, SIOCGLIFMTU
, &lifr
) < 0) {
380 warn("Couldn't get IPv6 MTU on %s: %m", lifr
.lifr_name
);
383 if (lifr
.lifr_mtu
!= 0 &&
384 (lcp_allowoptions
[unit
].mru
== 0 ||
385 lcp_allowoptions
[unit
].mru
> lifr
.lifr_mtu
))
386 lcp_allowoptions
[unit
].mru
= lifr
.lifr_mtu
;
388 /* Get the local IPv6 address */
389 if (eui64_iszero(ipv6cp_wantoptions
[unit
].ourid
) ||
390 (ipcp_from_hostname
&& ipv6cp_wantoptions
[unit
].use_ip
)) {
391 if (myioctl(ip6fd
, SIOCGLIFADDR
, &lifr
) < 0) {
392 warn("Couldn't get local IPv6 address (%s): %m",
396 eui64_copy(sin6
->sin6_addr
.s6_addr32
[2],
397 ipv6cp_wantoptions
[unit
].ourid
);
400 /* Get the remote IP address */
401 if (eui64_iszero(ipv6cp_wantoptions
[unit
].hisid
)) {
402 if (myioctl(ip6fd
, SIOCGLIFDSTADDR
, &lifr
) < 0) {
403 warn("Couldn't get remote IPv6 address (%s): %m",
407 eui64_copy(sin6
->sin6_addr
.s6_addr32
[2],
408 ipv6cp_wantoptions
[unit
].hisid
);
415 * Read information on existing interface(s) and configure ourselves
416 * to negotiate appropriately.
419 read_interface(int unit
)
421 dbglog("reading existing interface data; %sip %sipv6",
422 IPCP_ENABLED
? "" : "!",
424 IPV6CP_ENABLED
? "" :
427 if (IPCP_ENABLED
&& !read_ip_interface(unit
))
430 if (IPV6CP_ENABLED
&& !read_ipv6_interface(unit
))
438 * System-dependent initialization.
441 sys_init(bool open_as_user
)
450 if (!direct_tty
&& devnam
[0] != '\0') {
452 * Check for integrated driver-like devices (such as
453 * POS). These identify themselves as "PPP
454 * multiplexor" drivers.
457 (void) seteuid(getuid());
458 pppfd
= open(devnam
, O_RDWR
| O_NONBLOCK
);
462 strioctl(pppfd
, PPPIO_GTYPE
, &typ
, 0, sizeof (typ
)) >= 0 &&
464 integrated_driver
= 1;
470 extra_dev_fd
= pppfd
;
476 * Open Solaris PPP device driver.
479 pppfd
= open(drvnam
, O_RDWR
| O_NONBLOCK
);
481 fatal("Can't open %s: %m", drvnam
);
483 if (kdebugflag
& 1) {
484 x
= PPPDBG_LOG
+ PPPDBG_DRIVER
;
485 if (strioctl(pppfd
, PPPIO_DEBUG
, &x
, sizeof (x
), 0) < 0) {
486 warn("PPPIO_DEBUG ioctl for mux failed: %m");
490 * Assign a new PPA and get its unit number.
493 if (strioctl(pppfd
, PPPIO_NEWPPA
, &x
, sizeof (x
), sizeof (x
)) < 0) {
494 if (errno
== ENXIO
&& plumbed
)
495 fatal("No idle interfaces available for use");
496 fatal("PPPIO_NEWPPA ioctl failed: %m");
499 if (req_unit
>= 0 && ifunit
!= req_unit
) {
501 fatal("unable to get requested unit %d", req_unit
);
503 warn("unable to get requested unit %d", req_unit
);
506 * Enable packet time-stamping when idle option is specified. Note
507 * that we need to only do this on the control stream. Subsequent
508 * streams attached to this control stream (ppa) will inherit
509 * the time-stamp bit.
511 if (idle_time_limit
> 0) {
512 if (strioctl(pppfd
, PPPIO_USETIMESTAMP
, NULL
, 0, 0) < 0) {
513 warn("PPPIO_USETIMESTAMP ioctl failed: %m");
537 udpfd
= open(UDP_DEV_NAME
, O_RDWR
| O_NONBLOCK
, 0);
539 error("Couldn't open UDP device (%s): %m", UDP_DEV_NAME
);
547 * Perform IP interface plumbing.
553 int udpfd
= -1, tmpfd
;
557 if (!IPCP_ENABLED
|| (ifunit
== -1) || (pppfd
== -1)) {
562 if (ipfd
== -1 && open_ipfd() == -1)
564 if (use_plink
&& (udpfd
= open_udpfd()) == -1)
566 tmpfd
= open(drvnam
, O_RDWR
| O_NONBLOCK
, 0);
568 error("Couldn't open PPP device (%s): %m", drvnam
);
573 if (kdebugflag
& 1) {
574 x
= PPPDBG_LOG
+ PPPDBG_DRIVER
;
575 if (strioctl(tmpfd
, PPPIO_DEBUG
, &x
, sizeof (x
), 0) < 0) {
576 warn("PPPIO_DEBUG ioctl for mux failed: %m");
579 if (myioctl(tmpfd
, I_PUSH
, IP_MOD_NAME
) < 0) {
580 error("Couldn't push IP module (%s): %m", IP_MOD_NAME
);
584 * Assign ppa according to the unit number returned by ppp device
585 * after plumbing is completed above. Without setting the ppa, ip
586 * module will return EINVAL upon setting the interface UP
587 * (SIOCSxIFFLAGS). This is because ip module in 2.8 expects two
588 * DLPI_INFO_REQ to be sent down to the driver (below ip) before
589 * IFF_UP bit can be set. Plumbing the device causes one DLPI_INFO_REQ
590 * to be sent down, and the second DLPI_INFO_REQ is sent upon receiving
591 * IF_UNITSEL (old) or SIOCSLIFNAME (new) ioctls. Such setting of the
592 * ppa is required because the ppp DLPI provider advertises itself as
593 * a DLPI style 2 type, which requires a point of attachment to be
594 * specified. The only way the user can specify a point of attachment
595 * is via SIOCSLIFNAME or IF_UNITSEL. Such changes in the behavior of
596 * ip module was made to meet new or evolving standards requirements.
598 if (myioctl(tmpfd
, IF_UNITSEL
, &ifunit
) < 0) {
599 error("Couldn't set ppa for unit %d: %m", ifunit
);
603 ipmuxid
= myioctl(udpfd
, I_PLINK
, (void *)tmpfd
);
605 error("Can't I_PLINK PPP device to IP: %m");
609 ipmuxid
= myioctl(ipfd
, I_LINK
, (void *)tmpfd
);
611 error("Can't I_LINK PPP device to IP: %m");
615 BZERO(&ifr
, sizeof (ifr
));
616 (void) strlcpy(ifr
.ifr_name
, ifname
, sizeof (ifr
.ifr_name
));
617 ifr
.ifr_ip_muxid
= ipmuxid
;
618 ifr
.ifr_arp_muxid
= -1;
619 if (myioctl(ipfd
, SIOCSIFMUXID
, (caddr_t
)&ifr
) < 0) {
620 error("Can't set mux ID SIOCSIFMUXID on %s: %m", ifname
);
637 * Perform IP interface unplumbing. Possibly called from die(), so there
638 * shouldn't be any call to die() or fatal() here.
641 unplumb_ipif(int unit
)
643 int udpfd
= -1, fd
= -1;
647 if (!IPCP_ENABLED
|| (ifunit
== -1)) {
650 if (!plumbed
&& (ipmuxid
== -1 || (ipfd
== -1 && !use_plink
)))
653 if (!plumbed
&& use_plink
) {
654 if ((udpfd
= open_udpfd()) == -1)
657 * Note: must re-get mux ID, since any intervening
658 * ifconfigs will change this.
660 BZERO(&lifr
, sizeof (lifr
));
661 (void) strlcpy(lifr
.lifr_name
, ifname
,
662 sizeof (lifr
.lifr_name
));
663 if (myioctl(ipfd
, SIOCGLIFMUXID
, (caddr_t
)&lifr
) < 0) {
664 warn("Can't get mux fd: SIOCGLIFMUXID: %m");
666 id
= lifr
.lifr_ip_muxid
;
667 fd
= myioctl(udpfd
, _I_MUXID2FD
, (void *)id
);
669 warn("Can't get mux fd: _I_MUXID2FD: %m");
674 * Mark down and unlink the ip interface.
676 (void) sifdown(unit
);
677 if (default_route_gateway
!= 0) {
678 (void) cifdefaultroute(0, default_route_gateway
,
679 default_route_gateway
);
681 if (proxy_arp_addr
!= 0) {
682 (void) cifproxyarp(0, proxy_arp_addr
);
688 if (myioctl(udpfd
, I_PUNLINK
, (void *)id
) < 0) {
689 error("Can't I_PUNLINK PPP from IP: %m");
699 if (myioctl(ipfd
, I_UNLINK
, (void *)id
) < 0) {
700 error("Can't I_UNLINK PPP from IP: %m");
710 * Restore any system state we modified before exiting: mark the
711 * interface down, delete default route and/or proxy arp entry. This
712 * should not call die() because it's called from die().
717 (void) unplumb_ipif(0);
719 (void) unplumb_ip6if(0);
726 * Stores the first hardware interface address found in the system
727 * into addr and return 1 upon success, or 0 if none is found. This
728 * is also called from the multilink code.
731 get_first_hwaddr(addr
, msize
)
736 register struct ifreq
*pifreq
;
746 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
748 error("get_first_hwaddr: error opening IP socket: %m");
752 * Find out how many interfaces are running
754 if (myioctl(fd
, SIOCGIFNUM
, (caddr_t
)&num_ifs
) < 0) {
757 req_size
= num_ifs
* sizeof (struct ifreq
);
758 req
= malloc(req_size
);
760 novm("interface request structure.");
763 * Get interface configuration info for all interfaces
765 ifc
.ifc_len
= req_size
;
767 if (myioctl(fd
, SIOCGIFCONF
, &ifc
) < 0) {
768 error("SIOCGIFCONF: %m");
774 * And traverse each interface to look specifically for the first
775 * occurence of an Ethernet interface which has been marked up
777 pifreq
= ifc
.ifc_req
;
779 for (i
= ifc
.ifc_len
/ sizeof (struct ifreq
); i
> 0; i
--, pifreq
++) {
781 if (strchr(pifreq
->ifr_name
, ':') != NULL
) {
784 BZERO(&ifr
, sizeof (ifr
));
785 (void) strncpy(ifr
.ifr_name
, pifreq
->ifr_name
,
786 sizeof (ifr
.ifr_name
));
787 if (myioctl(fd
, SIOCGIFFLAGS
, &ifr
) < 0) {
791 if ((fl
& (IFF_UP
|IFF_BROADCAST
|IFF_POINTOPOINT
|IFF_LOOPBACK
))
792 != (IFF_UP
| IFF_BROADCAST
)) {
795 if (get_if_hwaddr(addr
, msize
, ifr
.ifr_name
) <= 0) {
810 * Get the hardware address for the specified network interface device.
811 * Return the length of the MAC address (in bytes) or -1 if error.
814 get_if_hwaddr(uchar_t
*addrp
, int msize
, char *linkname
)
817 uchar_t physaddr
[DLPI_PHYSADDR_MAX
];
818 size_t physaddrlen
= sizeof (physaddr
);
821 if ((addrp
== NULL
) || (linkname
== NULL
))
825 * Open the link and ask for hardware address.
827 if ((retv
= dlpi_open(linkname
, &dh
, 0)) != DLPI_SUCCESS
) {
828 error("Could not open %s: %s", linkname
, dlpi_strerror(retv
));
832 retv
= dlpi_get_physaddr(dh
, DL_CURR_PHYS_ADDR
,
833 physaddr
, &physaddrlen
);
835 if (retv
!= DLPI_SUCCESS
) {
836 error("Could not get physical address on %s: %s", linkname
,
837 dlpi_strerror(retv
));
842 * Check if we have enough space to copy the address to.
844 if (physaddrlen
> msize
)
846 (void) memcpy(addrp
, physaddr
, physaddrlen
);
847 return (physaddrlen
);
854 giflags(u_int32_t flag
, bool *retval
)
860 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
862 error("giflags: error opening IP socket: %m");
866 BZERO(&ifr
, sizeof (ifr
));
867 (void) strncpy(ifr
.ifr_name
, ifname
, sizeof (ifr
.ifr_name
));
868 if (ioctl(fd
, SIOCGIFFLAGS
, &ifr
) < 0) {
873 *retval
= ((ifr
.ifr_flags
& flag
) != 0);
881 * Clean up in a child process before exec-ing.
905 * Check if compression is enabled or not. In the STREAMS implementation of
906 * kernel-portion pppd, the comp STREAMS module performs the ACFC, PFC, as
907 * well CCP and VJ compressions. However, if the user has explicitly declare
908 * to not enable them from the command line, there is no point of having the
909 * comp module be pushed on the stream.
912 any_compressions(void)
914 if ((!lcp_wantoptions
[0].neg_accompression
) &&
915 (!lcp_wantoptions
[0].neg_pcompression
) &&
916 (!ccp_protent
.enabled_flag
) &&
917 (!ipcp_wantoptions
[0].neg_vj
)) {
926 * Push a module on the stream.
929 modpush(int fd
, const char *modname
, const char *text
)
931 if (myioctl(fd
, I_PUSH
, (void *)modname
) < 0) {
932 error("Couldn't push %s module: %m", text
);
935 if (++tty_npushed
== 1 && !already_ppp
) {
936 if (strioctl(fd
, PPPIO_LASTMOD
, NULL
, 0, 0) < 0) {
937 warn("unable to set LASTMOD on %s: %m", text
);
946 * Turn the serial port into a ppp interface.
955 if (default_device
&& !notty
) {
956 tty_sid
= getsid((pid_t
)0);
959 if (integrated_driver
)
963 * Pop any existing modules off the tty stream
966 if ((myioctl(fd
, I_LOOK
, tty_modules
[i
]) < 0) ||
967 (strcmp(tty_modules
[i
], "ptem") == 0) ||
968 (myioctl(fd
, I_POP
, NULL
) < 0)) {
974 * Push the async hdlc module and the compressor module
977 if (!sync_serial
&& !already_ppp
&&
978 modpush(fd
, AHDLC_MOD_NAME
, "PPP async HDLC") < 0) {
982 * There's no need to push comp module if we don't intend
983 * to compress anything
985 if (any_compressions()) {
986 (void) modpush(fd
, COMP_MOD_NAME
, "PPP compression");
990 * Link the serial port under the PPP multiplexor
992 if ((fdmuxid
= myioctl(pppfd
, I_LINK
, (void *)fd
)) < 0) {
993 error("Can't link tty to PPP mux: %m");
996 if (tty_npushed
== 0 && !already_ppp
) {
997 if (strioctl(pppfd
, PPPIO_LASTMOD
, NULL
, 0, 0) < 0) {
998 warn("unable to set LASTMOD on PPP mux: %m");
1002 * Debug configuration must occur *after* I_LINK.
1004 if (kdebugflag
& 4) {
1005 x
= PPPDBG_LOG
+ PPPDBG_AHDLC
;
1006 if (strioctl(pppfd
, PPPIO_DEBUG
, &x
, sizeof (x
), 0) < 0) {
1007 warn("PPPIO_DEBUG ioctl for ahdlc module failed: %m");
1010 if (any_compressions() && (kdebugflag
& 2)) {
1011 x
= PPPDBG_LOG
+ PPPDBG_COMP
;
1012 if (strioctl(pppfd
, PPPIO_DEBUG
, &x
, sizeof (x
), 0) < 0) {
1013 warn("PPPIO_DEBUG ioctl for comp module failed: %m");
1022 * Reattach the ppp unit to the loopback. This doesn't need to do anything
1023 * because disestablish_ppp does it
1031 * disestablish_ppp()
1033 * Restore the serial port to normal operation. It attempts to reconstruct
1034 * the stream with the previously popped modules. This shouldn't call die()
1035 * because it's called from die(). Stream reconstruction is needed in case
1036 * pppd is used for dial-in on /dev/tty and there's an option error.
1039 disestablish_ppp(fd
)
1044 if (fdmuxid
== -1 || integrated_driver
) {
1047 if (myioctl(pppfd
, I_UNLINK
, (void *)fdmuxid
) < 0) {
1049 error("Can't unlink tty from PPP mux: %m");
1054 while (tty_npushed
> 0 && myioctl(fd
, I_POP
, NULL
) >= 0) {
1057 for (i
= tty_nmodules
- 1; i
>= 0; --i
) {
1058 if (myioctl(fd
, I_PUSH
, tty_modules
[i
]) < 0) {
1059 error("Couldn't restore tty module %s: %m",
1064 if (hungup
&& default_device
&& tty_sid
> 0) {
1066 * If we have received a hangup, we need to send a
1067 * SIGHUP to the terminal's controlling process.
1068 * The reason is that the original stream head for
1069 * the terminal hasn't seen the M_HANGUP message
1070 * (it went up through the ppp driver to the stream
1071 * head for our fd to /dev/ppp).
1073 (void) kill(tty_sid
, SIGHUP
);
1080 * Check whether the link seems not to be 8-bit clean
1089 * Skip this is synchronous link is used, since spppasyn won't
1090 * be anywhere in the stream below to handle the ioctl.
1096 if (strioctl(pppfd
, PPPIO_GCLEAN
, &x
, 0, sizeof (x
)) < 0) {
1097 warn("unable to obtain serial link status: %m");
1102 s
= "bit 7 set to 1";
1105 s
= "bit 7 set to 0";
1115 warn("Serial link is not 8-bit clean:");
1116 warn("All received characters had %s", s
);
1121 * List of valid speeds.
1194 { 115200, B115200
},
1197 { 153600, B153600
},
1200 { 230400, B230400
},
1203 { 307200, B307200
},
1206 { 460800, B460800
},
1209 { 921600, B921600
},
1217 * Translate from bits/second to a speed_t
1220 translate_speed(int bps
)
1222 struct speed
*speedp
;
1227 for (speedp
= speeds
; speedp
->speed_int
; speedp
++) {
1228 if (bps
== speedp
->speed_int
) {
1229 return (speedp
->speed_val
);
1232 set_source(&speed_info
);
1233 option_error("speed %d not supported", bps
);
1240 * Translate from a speed_t to bits/second
1243 baud_rate_of(int speed
)
1245 struct speed
*speedp
;
1250 for (speedp
= speeds
; speedp
->speed_int
; speedp
++) {
1251 if (speed
== speedp
->speed_val
) {
1252 return (speedp
->speed_int
);
1261 * Set up the serial port on `fd' for 8 bits, no parity, at the requested
1262 * speed, etc. If `local' is true, set CLOCAL regardless of whether the
1263 * modem option was specified.
1266 set_up_tty(fd
, local
)
1270 struct termios tios
;
1281 if (strioctl(fd
, S_IOCGETMODE
, &sm
, sizeof (sm
),
1286 baud_rate
= sm
.sm_baudrate
;
1287 dbglog("synchronous speed appears to be %d bps", baud_rate
);
1289 if (tcgetattr(fd
, &tios
) < 0) {
1290 fatal("tcgetattr: %m");
1292 if (!restore_term
) {
1294 if (myioctl(fd
, TIOCGWINSZ
, &wsinfo
) < 0) {
1295 if (errno
== EINVAL
) {
1297 * ptem returns EINVAL if all zeroes.
1298 * Strange and unfixable code.
1300 bzero(&wsinfo
, sizeof (wsinfo
));
1302 warn("unable to get TTY window "
1307 tios
.c_cflag
&= ~(CSIZE
| CSTOPB
| PARENB
| CLOCAL
);
1309 tios
.c_cflag
|= CRTSCTS
| CRTSXOFF
;
1310 } else if (crtscts
< 0) {
1311 tios
.c_cflag
&= ~CRTSCTS
& ~CRTSXOFF
;
1313 tios
.c_cflag
|= CS8
| CREAD
| HUPCL
;
1314 if (local
|| !modem
) {
1315 tios
.c_cflag
|= CLOCAL
;
1317 tios
.c_iflag
= IGNBRK
| IGNPAR
;
1320 tios
.c_cc
[VMIN
] = 1;
1321 tios
.c_cc
[VTIME
] = 0;
1323 if (crtscts
== -2) {
1324 tios
.c_iflag
|= IXON
| IXOFF
;
1325 tios
.c_cc
[VSTOP
] = 0x13; /* DC3 = XOFF = ^S */
1326 tios
.c_cc
[VSTART
] = 0x11; /* DC1 = XON = ^Q */
1328 speed
= translate_speed(inspeed
);
1330 (void) cfsetospeed(&tios
, speed
);
1331 (void) cfsetispeed(&tios
, speed
);
1333 speed
= cfgetospeed(&tios
);
1335 * We can't proceed if the serial port speed is 0,
1336 * since that implies that the serial port is disabled.
1339 fatal("Baud rate for %s is 0; need explicit "
1340 "baud rate", devnam
);
1343 if (tcsetattr(fd
, TCSAFLUSH
, &tios
) < 0) {
1344 fatal("tcsetattr: %m");
1346 baud_rate
= baud_rate_of(speed
);
1347 dbglog("%s speed set to %d bps",
1348 fd
== pty_slave
? "pty" : "serial", baud_rate
);
1356 * Restore the terminal to the saved settings.
1362 if (restore_term
== 0) {
1365 if (!default_device
) {
1367 * Turn off echoing, because otherwise we can get into
1368 * a loop with the tty and the modem echoing to each
1369 * other. We presume we are the sole user of this tty
1370 * device, so when we close it, it will revert to its
1373 inittermios
.c_lflag
&= ~(ECHO
| ECHONL
);
1375 if (tcsetattr(fd
, TCSAFLUSH
, &inittermios
) < 0) {
1376 if (!hungup
&& errno
!= ENXIO
) {
1377 warn("tcsetattr: %m");
1380 if (wsinfo
.ws_row
!= 0 || wsinfo
.ws_col
!= 0 ||
1381 wsinfo
.ws_xpixel
!= 0 || wsinfo
.ws_ypixel
!= 0) {
1382 if (myioctl(fd
, TIOCSWINSZ
, &wsinfo
) < 0) {
1383 warn("unable to set TTY window size: %m");
1392 * Control the DTR line on the serial port. This is called from die(), so it
1393 * shouldn't call die()
1399 int modembits
= TIOCM_DTR
;
1401 myioctl(fd
, (on
? TIOCMBIS
: TIOCMBIC
), &modembits
) < 0) {
1402 warn("unable to set DTR line %s: %m", (on
? "ON" : "OFF"));
1409 * Open the device we use for getting packets in demand mode. Under Solaris 2,
1410 * we use our existing fd to the ppp driver.
1416 * Plumb the interface.
1418 if (IPCP_ENABLED
&& (plumb_ipif(0) == 0)) {
1419 fatal("Unable to initialize IP interface for demand dial.");
1422 if (IPV6CP_ENABLED
&& (plumb_ip6if(0) == 0)) {
1423 fatal("Unable to initialize IPv6 interface for demand dial.");
1433 * Output PPP packet downstream
1437 output(unit
, p
, len
)
1448 data
.buf
= (caddr_t
)p
;
1451 while (putmsg(pppfd
, NULL
, &data
, 0) < 0) {
1454 if (--retries
< 0 ||
1455 (errno
!= EWOULDBLOCK
&& errno
!= EAGAIN
)) {
1456 if (errno
!= ENXIO
) {
1457 error("Couldn't send packet: %m");
1463 pfd
.events
= POLLOUT
;
1465 /* wait for up to 0.25 seconds */
1466 n
= poll(&pfd
, 1, 250);
1467 } while ((n
== -1) && (errno
== EINTR
));
1469 if (debug
&& sent_ok
) {
1470 dbglog("sent %P", p
, len
);
1477 * Wait until there is data available, for the length of time specified by
1478 * timo (indefinite if timo is NULL).
1482 struct timeval
*timo
;
1486 t
= (timo
== NULL
? -1 : (timo
->tv_sec
* 1000 + timo
->tv_usec
/ 1000));
1487 if ((poll(pollfds
, n_pollfds
, t
) < 0) && (errno
!= EINTR
)) {
1495 * Add an fd to the set that wait_input waits for.
1506 for (n
= 0; n
< n_pollfds
; ++n
) {
1507 if (pollfds
[n
].fd
== fd
) {
1511 if (n_pollfds
< MAX_POLLFDS
) {
1512 pollfds
[n_pollfds
].fd
= fd
;
1513 pollfds
[n_pollfds
].events
= POLLIN
| POLLPRI
| POLLHUP
;
1516 fatal("add_fd: too many inputs!");
1523 * Remove an fd from the set that wait_input waits for.
1531 for (n
= 0; n
< n_pollfds
; ++n
) {
1532 if (pollfds
[n
].fd
== fd
) {
1533 while (++n
< n_pollfds
) {
1534 pollfds
[n
-1] = pollfds
[n
];
1543 dump_packet(uchar_t
*buf
, int len
)
1550 struct protoent
*pep
;
1551 struct in6_addr addr
;
1552 char fromstr
[INET6_ADDRSTRLEN
];
1553 char tostr
[INET6_ADDRSTRLEN
];
1556 notice("strange link activity: %.*B", len
, buf
);
1560 if (bp
[0] == 0xFF && bp
[1] == 0x03)
1564 proto
= (proto
<< 8) + *bp
++;
1568 if (len
< IP_HDRLEN
|| get_ipv(bp
) != 4 || get_iphl(bp
) < 5) {
1569 notice("strange IP packet activity: %16.*B", len
, buf
);
1572 src
= get_ipsrc(bp
);
1573 dst
= get_ipdst(bp
);
1574 proto
= get_ipproto(bp
);
1575 if ((pep
= getprotobynumber(proto
)) != NULL
) {
1578 (void) slprintf(sbuf
, sizeof (sbuf
), "IP proto %d",
1582 if ((get_ipoff(bp
) & IP_OFFMASK
) != 0) {
1583 len
-= get_iphl(bp
) * 4;
1584 bp
+= get_iphl(bp
) * 4;
1585 notice("%s fragment from %I->%I: %8.*B", cp
, src
, dst
,
1588 if (len
> get_iplen(bp
))
1589 len
= get_iplen(bp
);
1590 len
-= get_iphl(bp
) * 4;
1591 bp
+= get_iphl(bp
) * 4;
1592 offs
= proto
== IPPROTO_TCP
? (get_tcpoff(bp
)*4) : 8;
1593 if (proto
== IPPROTO_TCP
|| proto
== IPPROTO_UDP
)
1594 notice("%s data:%d %s%I:%d->%I:%d: %8.*B", cp
,
1596 proto
== IPPROTO_TCP
?
1597 tcp_flag_decode(get_tcpflags(bp
)) : "",
1598 src
, get_sport(bp
), dst
, get_dport(bp
),
1601 notice("%s %d bytes %I->%I: %8.*B", cp
, len
,
1607 if (len
< IP6_HDRLEN
) {
1608 notice("strange IPv6 activity: %16.*B", len
, buf
);
1611 (void) BCOPY(get_ip6src(bp
), &addr
, sizeof (addr
));
1612 (void) inet_ntop(AF_INET6
, &addr
, fromstr
, sizeof (fromstr
));
1613 (void) BCOPY(get_ip6dst(bp
), &addr
, sizeof (addr
));
1614 (void) inet_ntop(AF_INET6
, &addr
, tostr
, sizeof (tostr
));
1615 proto
= get_ip6nh(bp
);
1616 if (proto
== IPPROTO_FRAGMENT
) {
1617 notice("IPv6 fragment from %s->%s", fromstr
,
1621 if ((pep
= getprotobynumber(proto
)) != NULL
) {
1624 (void) slprintf(sbuf
, sizeof (sbuf
), "IPv6 proto %d",
1630 offs
= proto
== IPPROTO_TCP
? (get_tcpoff(bp
)*4) : 8;
1631 if (proto
== IPPROTO_TCP
|| proto
== IPPROTO_UDP
)
1632 notice("%s data:%d %s[%s]%d->[%s]%d: %8.*B", cp
,
1634 proto
== IPPROTO_TCP
?
1635 tcp_flag_decode(get_tcpflags(bp
)) : "",
1636 fromstr
, get_sport(bp
), tostr
, get_dport(bp
),
1639 notice("%s %d bytes %s->%s: %8.*B", cp
, len
,
1640 fromstr
, tostr
, len
, bp
);
1643 if ((cp
= protocol_name(proto
)) == NULL
) {
1644 (void) slprintf(sbuf
, sizeof (sbuf
), "0x#X", proto
);
1645 cp
= (const char *)sbuf
;
1647 notice("link activity: %s %16.*B", cp
, len
, bp
);
1654 handle_bind(u_int32_t reason
)
1657 * Here we might, in the future, handle DL_BIND_REQ notifications
1658 * in order to close and re-open a NCP when certain interface
1659 * parameters (addresses, etc.) are changed via external mechanisms
1660 * such as through the "ifconfig" program.
1663 case PPP_LINKSTAT_IPV4_BOUND
:
1666 case PPP_LINKSTAT_IPV6_BOUND
:
1670 error("handle_bind: unrecognized reason");
1679 handle_unbind(u_int32_t reason
)
1683 static const char *unplumb_str
= "unplumbed";
1684 static const char *down_str
= "downed";
1687 * Since the kernel driver (sppp) notifies this daemon of the
1688 * DLPI bind/unbind activities (for the purpose of bringing down
1689 * a NCP), we need to explicitly test the "actual" status of
1690 * the interface instance for which the notification is destined
1691 * from. This is because /dev/ip performs multiple DLPI attach-
1692 * bind-unbind-detach during the early life of the interface,
1693 * and when certain interface parameters change. A DL_UNBIND_REQ
1694 * coming down to the sppp driver from /dev/ip (which results in
1695 * our receiving of the PPP_LINKSTAT_*_UNBOUND link status message)
1696 * is not enough to conclude that the interface has been marked
1697 * DOWN (its IFF_UP bit is cleared) or is going away. Therefore,
1698 * we should query /dev/ip directly, upon receiving such *_UNBOUND
1699 * notification, to determine whether the interface is DOWN
1700 * for real, and only take the necessary actions when IFF_UP
1701 * bit for the interface instance is actually cleared.
1704 case PPP_LINKSTAT_IPV4_UNBOUND
:
1706 rc
= giflags(IFF_UP
, &iff_up_isset
);
1707 if (!iff_up_isset
) {
1710 info("IPv4 interface %s by administrator",
1711 ((rc
< 0 && rc
== ENXIO
) ? unplumb_str
: down_str
));
1712 fsm_close(&ipcp_fsm
[0],
1713 "administratively disconnected");
1717 case PPP_LINKSTAT_IPV6_UNBOUND
:
1719 rc
= giflags(IFF_UP
, &iff_up_isset
);
1720 if (!iff_up_isset
) {
1723 info("IPv6 interface %s by administrator",
1724 ((rc
< 0 && rc
== ENXIO
) ? unplumb_str
: down_str
));
1725 fsm_close(&ipv6cp_fsm
[0],
1726 "administratively disconnected");
1731 error("handle_unbind: unrecognized reason");
1739 * Get a PPP packet from the serial device.
1751 uint32_t ctrlbuf
[1536 / sizeof (uint32_t)];
1757 data
.maxlen
= PPP_MRU
+ PPP_HDRLEN
;
1758 data
.buf
= (caddr_t
)buf
;
1760 ctrl
.maxlen
= sizeof (ctrlbuf
);
1761 ctrl
.buf
= (caddr_t
)ctrlbuf
;
1764 rc
= len
= getmsg(pppfd
, &ctrl
, &data
, &flags
);
1765 if (sys_read_packet_hook
!= NULL
) {
1766 rc
= len
= (*sys_read_packet_hook
)(len
, &ctrl
, &data
,
1770 if (errno
== EAGAIN
|| errno
== EINTR
) {
1773 fatal("Error reading packet: %m");
1775 if ((data
.len
> 0) && (ctrl
.len
< 0)) {
1777 * If there's more data on stream head, keep reading
1778 * but discard, since the stream is now corrupt.
1780 if (rc
& MOREDATA
) {
1781 dbglog("More data; input packet garbled");
1789 } else if (ctrl
.len
> 0) {
1791 * If there's more ctl on stream head, keep reading,
1792 * but start discarding. We can't deal with fragmented
1796 dbglog("More control; stream garbled");
1802 if (ctrl
.len
< sizeof (struct ppp_ls
)) {
1803 warn("read_packet: ctl.len %d < "
1805 ctrl
.len
, sizeof (struct ppp_ls
));
1808 plp
= (struct ppp_ls
*)ctrlbuf
;
1809 if (plp
->magic
!= PPPLSMAGIC
) {
1810 /* Skip, as we don't understand it */
1811 dbglog("read_packet: unrecognized control %lX",
1816 lastlink_status
= plp
->ppp_message
;
1818 switch (plp
->ppp_message
) {
1819 case PPP_LINKSTAT_HANGUP
:
1820 return (0); /* Hangup */
1821 /* For use by integrated drivers. */
1822 case PPP_LINKSTAT_UP
:
1826 case PPP_LINKSTAT_NEEDUP
:
1828 dump_packet(buf
, data
.len
);
1829 return (-1); /* Demand dial */
1830 case PPP_LINKSTAT_IPV4_UNBOUND
:
1831 (void) handle_unbind(plp
->ppp_message
);
1833 case PPP_LINKSTAT_IPV4_BOUND
:
1834 (void) handle_bind(plp
->ppp_message
);
1837 case PPP_LINKSTAT_IPV6_UNBOUND
:
1838 (void) handle_unbind(plp
->ppp_message
);
1840 case PPP_LINKSTAT_IPV6_BOUND
:
1841 (void) handle_bind(plp
->ppp_message
);
1845 warn("read_packet: unknown link status type!");
1850 * We get here on zero length data or control.
1860 * Get outgoing packets from the ppp device, and detect when we want to bring
1861 * the real link up. Return value is 1 if we need to bring up the link, or 0
1870 * In the Solaris 2.x kernel-level portion implementation, packets
1871 * which are received on a demand-dial interface are immediately
1872 * discarded, and a notification message is sent up the control
1873 * stream to the pppd process. Therefore, the call to read_packet()
1874 * below is merely there to wait for such message.
1876 lastlink_status
= 0;
1878 while (read_packet(inpacket_buf
) > 0) {
1882 return (lastlink_status
== PPP_LINKSTAT_NEEDUP
);
1888 ppp_send_muxoption(unit
, muxflag
)
1895 * Since muxed frame feature is implemented in the async module,
1896 * don't send down the ioctl in the synchronous case.
1898 if (!sync_serial
&& fdmuxid
>= 0 && pppfd
!= -1) {
1902 if (strioctl(pppfd
, PPPIO_MUX
, cf
, sizeof (cf
), 0) < 0) {
1903 error("Couldn't set mux option: %m");
1910 ppp_recv_muxoption(unit
, muxflag
)
1917 * Since muxed frame feature is implemented in the async module,
1918 * don't send down the ioctl in the synchronous case.
1920 if (!sync_serial
&& fdmuxid
>= 0 && pppfd
!= -1) {
1924 if (strioctl(pppfd
, PPPIO_MUX
, cf
, sizeof (cf
), 0) < 0) {
1925 error("Couldn't set receive mux option: %m");
1934 * Configure the transmit characteristics of the ppp interface.
1938 ppp_send_config(unit
, mtu
, asyncmap
, pcomp
, accomp
)
1948 error("ppp_send_config called with invalid device handle");
1951 cf
[0] = link_mtu
= mtu
;
1952 if (strioctl(pppfd
, PPPIO_MTU
, cf
, sizeof (cf
[0]), 0) < 0) {
1953 if (hungup
&& errno
== ENXIO
) {
1956 error("Couldn't set MTU: %m");
1958 if (fdmuxid
!= -1) {
1960 if (strioctl(pppfd
, PPPIO_XACCM
, &asyncmap
,
1961 sizeof (asyncmap
), 0) < 0) {
1962 error("Couldn't set transmit ACCM: %m");
1965 cf
[0] = (pcomp
? COMP_PROT
: 0) + (accomp
? COMP_AC
: 0);
1966 cf
[1] = COMP_PROT
| COMP_AC
;
1968 if (any_compressions() && strioctl(pppfd
, PPPIO_CFLAGS
, cf
,
1969 sizeof (cf
), sizeof (cf
[0])) < 0) {
1970 error("Couldn't set prot/AC compression: %m");
1978 * Set the extended transmit ACCM for the interface.
1982 ppp_set_xaccm(unit
, accm
)
1989 if (fdmuxid
!= -1 && strioctl(pppfd
, PPPIO_XACCM
, accm
,
1990 sizeof (ext_accm
), 0) < 0) {
1991 if (!hungup
|| errno
!= ENXIO
) {
1992 warn("Couldn't set extended ACCM: %m");
2000 * Configure the receive-side characteristics of the ppp interface.
2004 ppp_recv_config(unit
, mru
, asyncmap
, pcomp
, accomp
)
2014 error("ppp_recv_config called with invalid device handle");
2018 if (strioctl(pppfd
, PPPIO_MRU
, cf
, sizeof (cf
[0]), 0) < 0) {
2019 if (hungup
&& errno
== ENXIO
) {
2022 error("Couldn't set MRU: %m");
2024 if (fdmuxid
!= -1) {
2026 if (strioctl(pppfd
, PPPIO_RACCM
, &asyncmap
,
2027 sizeof (asyncmap
), 0) < 0) {
2028 error("Couldn't set receive ACCM: %m");
2031 cf
[0] = (pcomp
? DECOMP_PROT
: 0) + (accomp
? DECOMP_AC
: 0);
2032 cf
[1] = DECOMP_PROT
| DECOMP_AC
;
2034 if (any_compressions() && strioctl(pppfd
, PPPIO_CFLAGS
, cf
,
2035 sizeof (cf
), sizeof (cf
[0])) < 0) {
2036 error("Couldn't set prot/AC decompression: %m");
2041 #ifdef NEGOTIATE_FCS
2045 * Configure the sender-side FCS.
2049 ppp_send_fcs(unit
, fcstype
)
2058 if (fcstype
& FCSALT_32
) {
2060 } else if (fcstype
& FCSALT_NULL
) {
2065 if (strioctl(pppfd
, PPPIO_XFCS
, &fcs
, sizeof (fcs
), 0) < 0) {
2066 warn("Couldn't set transmit FCS: %m");
2073 * Configure the receiver-side FCS.
2077 ppp_recv_fcs(unit
, fcstype
)
2086 if (fcstype
& FCSALT_32
) {
2088 } else if (fcstype
& FCSALT_NULL
) {
2093 if (strioctl(pppfd
, PPPIO_RFCS
, &fcs
, sizeof (fcs
), 0) < 0) {
2094 warn("Couldn't set receive FCS: %m");
2102 * Ask kernel whether a given compression method is acceptable for use.
2106 ccp_test(unit
, opt_ptr
, opt_len
, for_transmit
)
2112 if (strioctl(pppfd
, (for_transmit
? PPPIO_XCOMP
: PPPIO_RCOMP
),
2113 opt_ptr
, opt_len
, 0) >= 0) {
2116 warn("Error in %s ioctl: %m",
2117 (for_transmit
? "PPPIO_XCOMP" : "PPPIO_RCOMP"));
2118 return ((errno
== ENOSR
) ? 0 : -1);
2125 * Tune compression effort level.
2129 ccp_tune(unit
, effort
)
2135 if (strioctl(pppfd
, PPPIO_COMPLEV
, &x
, sizeof (x
), 0) < 0) {
2136 warn("unable to set compression effort level: %m");
2144 * Inform kernel about the current state of CCP.
2148 ccp_flags_set(unit
, isopen
, isup
)
2149 int unit
, isopen
, isup
;
2153 cf
[0] = (isopen
? CCP_ISOPEN
: 0) + (isup
? CCP_ISUP
: 0);
2154 cf
[1] = CCP_ISOPEN
| CCP_ISUP
| CCP_ERROR
| CCP_FATALERROR
;
2156 if (strioctl(pppfd
, PPPIO_CFLAGS
, cf
, sizeof (cf
), sizeof (cf
[0]))
2158 if (!hungup
|| errno
!= ENXIO
) {
2159 error("Couldn't set kernel CCP state: %m");
2167 * Return how long the link has been idle.
2171 get_idle_time(u
, pids
)
2173 struct ppp_idle
*pids
;
2177 rc
= strioctl(pppfd
, PPPIO_GIDLE
, pids
, 0, sizeof (struct ppp_idle
));
2179 warn("unable to obtain idle time: %m");
2181 return ((rc
== 0) ? 1 : 0);
2187 * Return statistics for the link.
2191 get_ppp_stats(u
, stats
)
2193 struct pppd_stats
*stats
;
2195 struct ppp_stats64 s64
;
2198 /* Try first to get these from the 64-bit interface */
2199 if (strioctl(pppfd
, PPPIO_GETSTAT64
, &s64
, 0, sizeof (s64
)) >= 0) {
2200 stats
->bytes_in
= s64
.p
.ppp_ibytes
;
2201 stats
->bytes_out
= s64
.p
.ppp_obytes
;
2202 stats
->pkts_in
= s64
.p
.ppp_ipackets
;
2203 stats
->pkts_out
= s64
.p
.ppp_opackets
;
2207 if (strioctl(pppfd
, PPPIO_GETSTAT
, &s
, 0, sizeof (s
)) < 0) {
2208 error("Couldn't get link statistics: %m");
2211 stats
->bytes_in
= s
.p
.ppp_ibytes
;
2212 stats
->bytes_out
= s
.p
.ppp_obytes
;
2213 stats
->pkts_in
= s
.p
.ppp_ipackets
;
2214 stats
->pkts_out
= s
.p
.ppp_opackets
;
2218 #if defined(FILTER_PACKETS)
2222 * Transfer the pass and active filters to the kernel.
2225 set_filters(pass
, active
)
2226 struct bpf_program
*pass
;
2227 struct bpf_program
*active
;
2231 if (pass
->bf_len
> 0) {
2232 if (strioctl(pppfd
, PPPIO_PASSFILT
, pass
,
2233 sizeof (struct bpf_program
), 0) < 0) {
2234 error("Couldn't set pass-filter in kernel: %m");
2238 if (active
->bf_len
> 0) {
2239 if (strioctl(pppfd
, PPPIO_ACTIVEFILT
, active
,
2240 sizeof (struct bpf_program
), 0) < 0) {
2241 error("Couldn't set active-filter in kernel: %m");
2247 #endif /* FILTER_PACKETS */
2252 * Returns 1 if decompression was disabled as a result of an error detected
2253 * after decompression of a packet, 0 otherwise. This is necessary because
2254 * of patent nonsense.
2258 ccp_fatal_error(unit
)
2264 if (strioctl(pppfd
, PPPIO_CFLAGS
, cf
, sizeof (cf
), sizeof (cf
[0]))
2266 if (errno
!= ENXIO
&& errno
!= EINVAL
) {
2267 error("Couldn't get compression flags: %m");
2271 return (cf
[0] & CCP_FATALERROR
);
2277 * Config TCP header compression.
2281 sifvjcomp(u
, vjcomp
, xcidcomp
, xmaxcid
)
2282 int u
, vjcomp
, xcidcomp
, xmaxcid
;
2288 * Since VJ compression code is in the comp module, there's no
2289 * point of sending down any ioctls pertaining to VJ compression
2290 * when the module isn't pushed on the stream.
2292 if (!any_compressions()) {
2297 maxcid
[0] = xcidcomp
;
2298 maxcid
[1] = 15; /* XXX should be rmaxcid */
2300 if (strioctl(pppfd
, PPPIO_VJINIT
, maxcid
,
2301 sizeof (maxcid
), 0) < 0) {
2302 error("Couldn't initialize VJ compression: %m");
2307 cf
[0] = (vjcomp
? COMP_VJC
+ DECOMP_VJC
: 0) /* XXX this is wrong */
2308 + (xcidcomp
? COMP_VJCCID
+ DECOMP_VJCCID
: 0);
2310 cf
[1] = COMP_VJC
+ DECOMP_VJC
+ COMP_VJCCID
+ DECOMP_VJCCID
;
2312 if (strioctl(pppfd
, PPPIO_CFLAGS
, cf
, sizeof (cf
), sizeof (cf
[0]))
2315 error("Couldn't enable VJ compression: %m");
2317 error("Couldn't disable VJ compression: %m");
2327 * Set or clear the IP interface flags.
2336 if (!IPCP_ENABLED
|| (ipmuxid
== -1)) {
2339 if (ipfd
== -1 && open_ipfd() == -1)
2341 BZERO(&ifr
, sizeof (ifr
));
2342 (void) strlcpy(ifr
.ifr_name
, ifname
, sizeof (ifr
.ifr_name
));
2343 if (myioctl(ipfd
, SIOCGIFFLAGS
, &ifr
) < 0) {
2344 error("Couldn't get IP interface flags: %m");
2350 ifr
.ifr_flags
&= ~f
;
2352 if (myioctl(ipfd
, SIOCSIFFLAGS
, &ifr
) < 0) {
2353 error("Couldn't set IP interface flags: %m");
2362 * Config the interface up and enable IP packets to pass.
2371 } else if (!IPCP_ENABLED
) {
2372 warn("sifup called when IPCP is disabled");
2374 } else if (ipmuxid
== -1) {
2375 warn("sifup called in wrong state");
2377 } else if (!siflags(IFF_UP
, 1)) {
2378 error("Unable to mark the IP interface UP");
2388 * Config the interface down and disable IP. Possibly called from die(),
2389 * so there shouldn't be any call to die() here.
2396 if (!IPCP_ENABLED
) {
2397 warn("sifdown called when IPCP is disabled");
2399 } else if (!if_is_up
|| (ipmuxid
== -1)) {
2401 } else if (!siflags(IFF_UP
, 0)) {
2402 error("Unable to mark the IP interface DOWN");
2412 * Set the mode for handling packets for a given NP. Not worried
2413 * about performance here since this is done only rarely.
2417 sifnpmode(u
, proto
, mode
)
2424 static const struct npi_entry
{
2425 enum NPmode ne_value
;
2426 const char *ne_name
;
2428 { NPMODE_PASS
, "pass" },
2429 { NPMODE_DROP
, "drop" },
2430 { NPMODE_ERROR
, "error" },
2431 { NPMODE_QUEUE
, "queue" },
2434 char pname
[32], mname
[32];
2437 npi
[1] = (uint32_t)mode
;
2439 cp
= protocol_name(proto
);
2441 (void) slprintf(pname
, sizeof (pname
), "NP %04X", proto
);
2443 (void) strlcpy(pname
, cp
, sizeof (pname
));
2444 for (i
= 0; i
< Dim(npi_list
); i
++)
2445 if (npi_list
[i
].ne_value
== mode
)
2447 if (i
>= Dim(npi_list
))
2448 (void) slprintf(mname
, sizeof (mname
), "mode %d", (int)mode
);
2450 (void) strlcpy(mname
, npi_list
[i
].ne_name
, sizeof (mname
));
2452 if ((proto
== PPP_IP
&& !if_is_up
) ||
2453 (proto
== PPP_IPV6
&& !if6_is_up
)) {
2454 dbglog("ignoring request to set %s to %s", pname
, mname
);
2457 if (strioctl(pppfd
, PPPIO_NPMODE
, npi
, sizeof (npi
), 0) < 0) {
2458 error("unable to set %s to %s: %m", pname
, mname
);
2467 * Config the interface IP MTU.
2475 if (!IPCP_ENABLED
|| (ipmuxid
== -1)) {
2478 if (ipfd
== -1 && open_ipfd() == -1)
2480 BZERO(&ifr
, sizeof (ifr
));
2481 (void) strlcpy(ifr
.ifr_name
, ifname
, sizeof (ifr
.ifr_name
));
2482 ifr
.ifr_metric
= mtu
;
2483 if (myioctl(ipfd
, SIOCSIFMTU
, &ifr
) < 0) {
2484 error("Couldn't set IP MTU on %s to %d: %m", ifr
.ifr_name
,
2494 * Config the interface IP addresses and netmask.
2505 struct sockaddr_in sin
;
2507 if (!IPCP_ENABLED
|| (ipmuxid
== -1 && plumb_ipif(u
) == 0)) {
2510 if (ipfd
== -1 && open_ipfd() == -1)
2513 * Set the IP interface MTU.
2515 if (!sifmtu(link_mtu
)) {
2519 * Set the IP interface local point-to-point address.
2521 BZERO(&sin
, sizeof (sin
));
2522 sin
.sin_family
= AF_INET
;
2523 sin
.sin_addr
.s_addr
= o
;
2525 BZERO(&ifr
, sizeof (ifr
));
2526 (void) strlcpy(ifr
.ifr_name
, ifname
, sizeof (ifr
.ifr_name
));
2527 ifr
.ifr_addr
= *(struct sockaddr
*)&sin
;
2528 if (myioctl(ipfd
, SIOCSIFADDR
, &ifr
) < 0) {
2529 error("Couldn't set local IP address (%s): %m", ifr
.ifr_name
);
2533 * Set the IP interface remote point-to-point address.
2535 sin
.sin_addr
.s_addr
= h
;
2537 ifr
.ifr_dstaddr
= *(struct sockaddr
*)&sin
;
2538 if (myioctl(ipfd
, SIOCSIFDSTADDR
, &ifr
) < 0) {
2539 error("Couldn't set remote IP address (%s): %m", ifr
.ifr_name
);
2549 * Clear the interface IP addresses.
2558 if (!IPCP_ENABLED
) {
2562 * Most of the work is done in sifdown().
2571 * Add or delete a route.
2575 sifroute(int u
, u_int32_t l
, u_int32_t g
, int add
, const char *str
)
2577 struct sockaddr_in sin_dst
, sin_gtw
;
2580 if (!IPCP_ENABLED
|| (ipmuxid
== -1)) {
2581 error("Can't %s route: IP is not enabled", str
);
2584 if (ipfd
== -1 && open_ipfd() == -1)
2587 BZERO(&sin_dst
, sizeof (sin_dst
));
2588 sin_dst
.sin_family
= AF_INET
;
2589 sin_dst
.sin_addr
.s_addr
= l
;
2591 BZERO(&sin_gtw
, sizeof (sin_gtw
));
2592 sin_gtw
.sin_family
= AF_INET
;
2593 sin_gtw
.sin_addr
.s_addr
= g
;
2595 BZERO(&rt
, sizeof (rt
));
2596 rt
.rt_dst
= *(struct sockaddr
*)&sin_dst
;
2597 rt
.rt_gateway
= *(struct sockaddr
*)&sin_gtw
;
2598 rt
.rt_flags
= (RTF_GATEWAY
|RTF_STATIC
);
2600 if (myioctl(ipfd
, (add
? SIOCADDRT
: SIOCDELRT
), &rt
) < 0) {
2601 error("Can't %s route: %m", str
);
2610 * Assign a default route through the address given.
2614 sifdefaultroute(u
, l
, g
)
2619 if (!sifroute(u
, 0, g
, 1, "add default")) {
2622 default_route_gateway
= g
;
2629 * Delete a default route through the address given.
2633 cifdefaultroute(u
, l
, g
)
2638 if (!sifroute(u
, 0, g
, 0, "delete default")) {
2641 default_route_gateway
= 0;
2648 * Make a proxy ARP entry for the peer.
2652 sifproxyarp(unit
, hisaddr
, quietflag
)
2657 struct sockaddr_in sin
;
2658 struct xarpreq arpreq
;
2662 if (!IPCP_ENABLED
|| (ipmuxid
== -1)) {
2665 if (ipfd
== -1 && open_ipfd() == -1)
2668 BZERO(&sin
, sizeof (sin
));
2669 sin
.sin_family
= AF_INET
;
2670 sin
.sin_addr
.s_addr
= hisaddr
;
2672 BZERO(&arpreq
, sizeof (arpreq
));
2673 if (!get_ether_addr(hisaddr
, &arpreq
.xarp_ha
, quietflag
)) {
2676 BCOPY(&sin
, &arpreq
.xarp_pa
, sizeof (sin
));
2677 arpreq
.xarp_flags
= ATF_PERM
| ATF_PUBL
;
2678 arpreq
.xarp_ha
.sdl_family
= AF_LINK
;
2680 if (myioctl(ipfd
, SIOCSXARP
, (caddr_t
)&arpreq
) < 0) {
2682 error("Couldn't set proxy ARP entry: %m");
2685 cp
= (const uchar_t
*)LLADDR(&arpreq
.xarp_ha
);
2686 str
= _link_ntoa(cp
, str
, arpreq
.xarp_ha
.sdl_alen
, IFT_OTHER
);
2688 dbglog("established proxy ARP for %I using %s", hisaddr
,
2692 proxy_arp_addr
= hisaddr
;
2699 * Delete the proxy ARP entry for the peer.
2703 cifproxyarp(unit
, hisaddr
)
2707 struct sockaddr_in sin
;
2708 struct xarpreq arpreq
;
2710 if (!IPCP_ENABLED
|| (ipmuxid
== -1)) {
2713 if (ipfd
== -1 && open_ipfd() == -1)
2716 BZERO(&sin
, sizeof (sin
));
2717 sin
.sin_family
= AF_INET
;
2718 sin
.sin_addr
.s_addr
= hisaddr
;
2720 BZERO(&arpreq
, sizeof (arpreq
));
2721 BCOPY(&sin
, &arpreq
.xarp_pa
, sizeof (sin
));
2722 arpreq
.xarp_ha
.sdl_family
= AF_LINK
;
2724 if (myioctl(ipfd
, SIOCDXARP
, (caddr_t
)&arpreq
) < 0) {
2725 error("Couldn't delete proxy ARP entry: %m");
2735 * Get the hardware address of an interface on the the same subnet as
2736 * ipaddr. This routine uses old-style interfaces for intentional
2737 * backward compatibility -- SIOCGLIF* isn't in older Solaris
2741 get_ether_addr(u_int32_t ipaddr
, struct sockaddr_dl
*hwaddr
, int quietflag
)
2743 struct ifreq
*ifr
, *ifend
, ifreq
;
2746 u_int32_t ina
, mask
;
2748 struct sockaddr_in sin
;
2750 if (ipfd
== -1 && open_ipfd() == -1)
2754 * Scan through the system's network interfaces.
2756 if (myioctl(ipfd
, SIOCGIFNUM
, &nif
) < 0) {
2761 ifc
.ifc_len
= nif
* sizeof (struct ifreq
);
2762 ifc
.ifc_buf
= (caddr_t
)malloc(ifc
.ifc_len
);
2763 if (ifc
.ifc_buf
== NULL
) {
2766 if (myioctl(ipfd
, SIOCGIFCONF
, &ifc
) < 0) {
2767 error("Couldn't get system interface list: %m");
2772 ifend
= (struct ifreq
*)(ifc
.ifc_buf
+ ifc
.ifc_len
);
2773 for (ifr
= ifc
.ifc_req
; ifr
< ifend
; ++ifr
) {
2774 if (ifr
->ifr_addr
.sa_family
!= AF_INET
) {
2778 * Check that the interface is up, and not
2779 * point-to-point or loopback.
2781 (void) strlcpy(ifreq
.ifr_name
, ifr
->ifr_name
,
2782 sizeof (ifreq
.ifr_name
));
2783 if (myioctl(ipfd
, SIOCGIFFLAGS
, &ifreq
) < 0) {
2786 if ((ifreq
.ifr_flags
& (IFF_UP
|IFF_BROADCAST
|IFF_POINTOPOINT
|
2787 IFF_LOOPBACK
|IFF_NOARP
)) != (IFF_UP
|IFF_BROADCAST
)) {
2791 * Get its netmask and check that it's on the right subnet.
2793 if (myioctl(ipfd
, SIOCGIFNETMASK
, &ifreq
) < 0) {
2796 (void) memcpy(&sin
, &ifr
->ifr_addr
, sizeof (sin
));
2797 ina
= sin
.sin_addr
.s_addr
;
2798 (void) memcpy(&sin
, &ifreq
.ifr_addr
, sizeof (sin
));
2799 mask
= sin
.sin_addr
.s_addr
;
2800 if ((ipaddr
& mask
) == (ina
& mask
)) {
2806 warn("No suitable interface found for proxy ARP of %I",
2811 info("found interface %s for proxy ARP of %I", ifr
->ifr_name
, ipaddr
);
2814 * New way - get the address by doing an arp request.
2816 s
= socket(AF_INET
, SOCK_DGRAM
, 0);
2818 error("get_ether_addr: error opening IP socket: %m");
2822 BZERO(&sin
, sizeof (sin
));
2823 sin
.sin_family
= AF_INET
;
2824 sin
.sin_addr
.s_addr
= ina
;
2826 BZERO(&req
, sizeof (req
));
2827 BCOPY(&sin
, &req
.xarp_pa
, sizeof (sin
));
2828 req
.xarp_ha
.sdl_family
= AF_LINK
;
2830 if (myioctl(s
, SIOCGXARP
, &req
) < 0) {
2831 error("Couldn't get ARP entry for %I: %m", ina
);
2834 (void) memcpy(hwaddr
, &req
.xarp_ha
,
2835 sizeof (struct sockaddr_dl
));
2846 * Return mask (bogus, but needed for compatibility with other platforms).
2853 return (0xffffffffUL
);
2859 * Write an accounting record to the /var/log/wtmp file.
2863 logwtmp(line
, name
, host
)
2868 static struct utmpx utmpx
;
2870 if (name
[0] != '\0') {
2874 (void) strncpy(utmpx
.ut_user
, name
, sizeof (utmpx
.ut_user
));
2875 (void) strncpy(utmpx
.ut_id
, ifname
, sizeof (utmpx
.ut_id
));
2876 (void) strncpy(utmpx
.ut_line
, line
, sizeof (utmpx
.ut_line
));
2878 utmpx
.ut_pid
= getpid();
2879 utmpx
.ut_type
= USER_PROCESS
;
2881 utmpx
.ut_type
= DEAD_PROCESS
;
2883 (void) gettimeofday(&utmpx
.ut_tv
, NULL
);
2884 updwtmpx("/var/log/wtmpx", &utmpx
);
2890 * Return the serial number of this machine.
2897 if (sysinfo(SI_HW_SERIAL
, buf
, sizeof (buf
)) < 0) {
2898 error("sysinfo: %m");
2901 return ((int)strtoul(buf
, NULL
, 16));
2907 * Wrapper for STREAMS I_STR ioctl. Masks out EINTR from caller.
2910 strioctl(int fd
, int cmd
, void *ptr
, int ilen
, int olen
)
2912 struct strioctl str
;
2915 str
.ic_timout
= PPPSTRTIMOUT
;
2919 if (myioctl(fd
, I_STR
, &str
) == -1) {
2922 if (str
.ic_len
!= olen
) {
2923 dbglog("strioctl: expected %d bytes, got %d for cmd %x\n",
2924 olen
, str
.ic_len
, cmd
);
2932 * Determine if the system has a route to the specified IP address.
2933 * Returns 0 if not, 1 if so, -1 if we can't tell. `addr' is in network
2934 * byte order. For demand mode to work properly, we have to ignore routes
2935 * through our own interface. XXX Would be nice to use routing socket.
2943 struct T_optmgmt_req req
;
2947 struct T_optmgmt_ack ack
;
2948 unsigned char space
[64];
2951 struct strbuf cbuf
, dbuf
;
2953 mib2_ipRouteEntry_t routes
[8];
2954 mib2_ipRouteEntry_t
*rp
;
2956 if (ipfd
== -1 && open_ipfd() == -1)
2959 req
.req
.PRIM_type
= T_OPTMGMT_REQ
;
2960 req
.req
.OPT_offset
= (caddr_t
)&req
.hdr
- (caddr_t
)&req
;
2961 req
.req
.OPT_length
= sizeof (req
.hdr
);
2963 req
.req
.MGMT_flags
= T_CURRENT
;
2966 req
.req
.MGMT_flags
= T_CHECK
;
2969 req
.hdr
.level
= MIB2_IP
;
2973 cbuf
.buf
= (caddr_t
)&req
;
2974 cbuf
.len
= sizeof (req
);
2976 if (putmsg(ipfd
, &cbuf
, NULL
, 0) == -1) {
2977 warn("have_route_to: putmsg: %m");
2982 cbuf
.buf
= (caddr_t
)&ack
;
2983 cbuf
.maxlen
= sizeof (ack
);
2984 dbuf
.buf
= (caddr_t
)routes
;
2985 dbuf
.maxlen
= sizeof (routes
);
2987 r
= getmsg(ipfd
, &cbuf
, &dbuf
, &flags
);
2989 warn("have_route_to: getmsg: %m");
2993 if (cbuf
.len
< sizeof (struct T_optmgmt_ack
) ||
2994 ack
.ack
.PRIM_type
!= T_OPTMGMT_ACK
||
2995 ack
.ack
.MGMT_flags
!= T_SUCCESS
||
2996 ack
.ack
.OPT_length
< sizeof (struct opthdr
)) {
2997 dbglog("have_route_to: bad message len=%d prim=%d",
2998 cbuf
.len
, ack
.ack
.PRIM_type
);
3002 rh
= (struct opthdr
*)((caddr_t
)&ack
+ ack
.ack
.OPT_offset
);
3003 if (rh
->level
== 0 && rh
->name
== 0) {
3006 if (rh
->level
!= MIB2_IP
|| rh
->name
!= MIB2_IP_21
) {
3007 while (r
== MOREDATA
) {
3008 r
= getmsg(ipfd
, NULL
, &dbuf
, &flags
);
3014 * Note that we have to skip routes to our own
3015 * interface in order for demand dial to work.
3017 * XXX awful hack here. We don't know our own
3018 * ifIndex, so we can't check ipRouteIfIndex here.
3019 * Instead, we check the next hop address.
3022 nroutes
= dbuf
.len
/ sizeof (mib2_ipRouteEntry_t
);
3023 for (rp
= routes
, i
= 0; i
< nroutes
; ++i
, ++rp
) {
3024 if (rp
->ipRouteNextHop
!= remote_addr
&&
3025 ((addr
^ rp
->ipRouteDest
) &
3026 rp
->ipRouteMask
) == 0) {
3027 dbglog("have route to %I/%I via %I",
3030 rp
->ipRouteNextHop
);
3037 r
= getmsg(ipfd
, NULL
, &dbuf
, &flags
);
3046 * Get a pty master/slave pair and chown the slave side to the uid given.
3047 * Assumes slave_name points to MAXPATHLEN bytes of space.
3050 get_pty(master_fdp
, slave_fdp
, slave_name
, uid
)
3060 mfd
= open("/dev/ptmx", O_NOCTTY
| O_RDWR
);
3062 error("Couldn't open pty master: %m");
3065 pty_name
= ptsname(mfd
);
3066 if (pty_name
== NULL
) {
3067 dbglog("Didn't get pty slave name on first try; sleeping.");
3068 /* In case "grow" operation is in progress; try again. */
3070 pty_name
= ptsname(mfd
);
3072 if (pty_name
== NULL
) {
3073 error("Couldn't get name of pty slave");
3077 if (chown(pty_name
, uid
, -1) < 0) {
3078 warn("Couldn't change owner of pty slave: %m");
3080 if (chmod(pty_name
, S_IRUSR
| S_IWUSR
) < 0) {
3081 warn("Couldn't change permissions on pty slave: %m");
3083 if (unlockpt(mfd
) < 0) {
3084 warn("Couldn't unlock pty slave: %m");
3086 sfd
= open(pty_name
, O_RDWR
);
3088 error("Couldn't open pty slave %s: %m", pty_name
);
3092 if (myioctl(sfd
, I_PUSH
, "ptem") < 0) {
3093 warn("Couldn't push ptem module on pty slave: %m");
3095 dbglog("Using %s; master fd %d, slave fd %d", pty_name
, mfd
, sfd
);
3097 (void) strlcpy(slave_name
, pty_name
, MAXPATHLEN
);
3111 udp6fd
= open(UDP6_DEV_NAME
, O_RDWR
| O_NONBLOCK
, 0);
3113 error("Couldn't open UDPv6 device (%s): %m", UDP6_DEV_NAME
);
3121 * Perform IPv6 interface plumbing.
3125 plumb_ip6if(int unit
)
3127 int udp6fd
= -1, tmpfd
;
3131 if (!IPV6CP_ENABLED
|| (ifunit
== -1) || (pppfd
== -1)) {
3136 if (ip6fd
== -1 && open_ip6fd() == -1)
3138 if (use_plink
&& (udp6fd
= open_udp6fd()) == -1)
3140 tmpfd
= open(drvnam
, O_RDWR
| O_NONBLOCK
, 0);
3142 error("Couldn't open PPP device (%s): %m", drvnam
);
3144 (void) close(udp6fd
);
3147 if (kdebugflag
& 1) {
3148 x
= PPPDBG_LOG
+ PPPDBG_DRIVER
;
3149 if (strioctl(tmpfd
, PPPIO_DEBUG
, &x
, sizeof (x
), 0) < 0) {
3150 warn("PPPIO_DEBUG ioctl for mux failed: %m");
3153 if (myioctl(tmpfd
, I_PUSH
, IP_MOD_NAME
) < 0) {
3154 error("Couldn't push IP module(%s): %m", IP_MOD_NAME
);
3158 * Sets interface ppa and flags (refer to comments in plumb_ipif for
3159 * the IF_UNITSEL ioctl). In addition, the IFF_IPV6 bit must be set in
3160 * order to declare this as an IPv6 interface.
3162 BZERO(&lifr
, sizeof (lifr
));
3163 if (myioctl(tmpfd
, SIOCGLIFFLAGS
, &lifr
) < 0) {
3164 error("Couldn't get IPv6 interface flags: %m");
3167 lifr
.lifr_flags
|= IFF_IPV6
;
3168 lifr
.lifr_flags
&= ~(IFF_BROADCAST
| IFF_IPV4
);
3169 lifr
.lifr_ppa
= ifunit
;
3170 (void) strlcpy(lifr
.lifr_name
, ifname
, sizeof (lifr
.lifr_name
));
3171 if (myioctl(tmpfd
, SIOCSLIFNAME
, &lifr
) < 0) {
3172 error("Can't set ifname for unit %d: %m", ifunit
);
3176 ip6muxid
= myioctl(udp6fd
, I_PLINK
, (void *)tmpfd
);
3178 error("Can't I_PLINK PPP device to IPv6: %m");
3182 ip6muxid
= myioctl(ip6fd
, I_LINK
, (void *)tmpfd
);
3184 error("Can't I_LINK PPP device to IPv6: %m");
3188 lifr
.lifr_ip_muxid
= ip6muxid
;
3189 lifr
.lifr_arp_muxid
= -1;
3190 if (myioctl(ip6fd
, SIOCSLIFMUXID
, (caddr_t
)&lifr
) < 0) {
3191 error("Can't set mux ID: SIOCSLIFMUXID: %m");
3194 (void) close(tmpfd
);
3196 (void) close(udp6fd
);
3200 (void) close(tmpfd
);
3202 (void) close(udp6fd
);
3209 * Perform IPv6 interface unplumbing. Possibly called from die(), so there
3210 * shouldn't be any call to die() here.
3213 unplumb_ip6if(int unit
)
3215 int udp6fd
= -1, fd
= -1;
3219 if (!IPV6CP_ENABLED
|| ifunit
== -1) {
3222 if (!plumbed
&& (ip6muxid
== -1 || (ip6fd
== -1 && !use_plink
))) {
3226 if (!plumbed
&& use_plink
) {
3227 if ((udp6fd
= open_udp6fd()) == -1)
3230 * Note: must re-get mux ID, since any intervening
3231 * ifconfigs will change this.
3233 BZERO(&lifr
, sizeof (lifr
));
3234 (void) strlcpy(lifr
.lifr_name
, ifname
,
3235 sizeof (lifr
.lifr_name
));
3236 if (myioctl(ip6fd
, SIOCGLIFMUXID
, (caddr_t
)&lifr
) < 0) {
3237 warn("Can't get mux fd: SIOCGLIFMUXID: %m");
3239 id
= lifr
.lifr_ip_muxid
;
3240 fd
= myioctl(udp6fd
, _I_MUXID2FD
, (void *)id
);
3242 warn("Can't get mux fd: _I_MUXID2FD: %m");
3247 * Mark down and unlink the IPv6 interface.
3249 (void) sif6down(unit
);
3254 if ((fd
= myioctl(udp6fd
, _I_MUXID2FD
, (void *)id
)) < 0) {
3255 error("Can't recapture mux fd: _I_MUXID2FD: %m");
3256 (void) close(udp6fd
);
3259 if (myioctl(udp6fd
, I_PUNLINK
, (void *)id
) < 0) {
3260 error("Can't I_PUNLINK PPP from IPv6: %m");
3262 (void) close(udp6fd
);
3266 (void) close(udp6fd
);
3268 if (myioctl(ip6fd
, I_UNLINK
, (void *)id
) < 0) {
3269 error("Can't I_UNLINK PPP from IPv6: %m");
3279 * Set or clear the IPv6 interface flags.
3289 if (!IPV6CP_ENABLED
|| (ip6muxid
== -1)) {
3292 fd
= socket(AF_INET6
, SOCK_DGRAM
, 0);
3294 error("sif6flags: error opening IPv6 socket: %m");
3297 BZERO(&lifr
, sizeof (lifr
));
3298 (void) strlcpy(lifr
.lifr_name
, ifname
, sizeof (lifr
.lifr_name
));
3299 if (myioctl(fd
, SIOCGLIFFLAGS
, &lifr
) < 0) {
3300 error("Couldn't get IPv6 interface flags: %m");
3305 lifr
.lifr_flags
|= f
;
3307 lifr
.lifr_flags
&= ~f
;
3309 (void) strlcpy(lifr
.lifr_name
, ifname
, sizeof (lifr
.lifr_name
));
3310 if (myioctl(fd
, SIOCSLIFFLAGS
, &lifr
) < 0) {
3311 error("Couldn't set IPv6 interface flags: %m");
3322 * Config the IPv6 interface up and enable IPv6 packets to pass.
3331 } else if (!IPV6CP_ENABLED
) {
3332 warn("sif6up called when IPV6CP is disabled");
3334 } else if (ip6muxid
== -1) {
3335 warn("sif6up called in wrong state");
3337 } else if (!sif6flags(IFF_UP
, 1)) {
3338 error("Unable to mark the IPv6 interface UP");
3348 * Config the IPv6 interface down and disable IPv6. Possibly called from
3349 * die(), so there shouldn't be any call to die() here.
3356 if (!IPV6CP_ENABLED
) {
3357 warn("sif6down called when IPV6CP is disabled");
3359 } else if (!if6_is_up
|| (ip6muxid
== -1)) {
3361 } else if (!sif6flags(IFF_UP
, 0)) {
3362 error("Unable to mark the IPv6 interface DOWN");
3372 * Config the IPv6 interface MTU.
3381 if (!IPV6CP_ENABLED
|| (ip6muxid
== -1)) {
3384 s
= socket(AF_INET6
, SOCK_DGRAM
, 0);
3386 error("sif6mtu: error opening IPv6 socket: %m");
3389 BZERO(&lifr
, sizeof (lifr
));
3390 (void) strlcpy(lifr
.lifr_name
, ifname
, sizeof (lifr
.lifr_name
));
3391 lifr
.lifr_mtu
= mtu
;
3392 if (myioctl(s
, SIOCSLIFMTU
, &lifr
) < 0) {
3393 error("Couldn't set IPv6 MTU (%s): %m", lifr
.lifr_name
);
3404 * Config the interface with an IPv6 link-local address.
3408 sif6addr(unit
, ourid
, hisid
)
3414 struct sockaddr_storage laddr
;
3415 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)&laddr
;
3418 if (!IPV6CP_ENABLED
|| (ip6muxid
== -1 && plumb_ip6if(unit
) == 0)) {
3421 fd
= socket(AF_INET6
, SOCK_DGRAM
, 0);
3423 error("sif6addr: error opening IPv6 socket: %m");
3427 * Set the IPv6 interface MTU.
3429 if (!sif6mtu(link_mtu
)) {
3434 * Set the interface address token. Do this because /dev/ppp responds
3435 * to DL_PHYS_ADDR_REQ with zero values, hence the interface token
3436 * came to be zero too, and without this, in.ndpd will complain.
3438 BZERO(&lifr
, sizeof (lifr
));
3439 (void) strlcpy(lifr
.lifr_name
, ifname
, sizeof (lifr
.lifr_name
));
3440 BZERO(sin6
, sizeof (struct sockaddr_in6
));
3441 IN6_LLTOKEN_FROM_EUI64(lifr
, sin6
, ourid
);
3442 if (myioctl(fd
, SIOCSLIFTOKEN
, &lifr
) < 0) {
3443 error("Couldn't set IPv6 token (%s): %m", lifr
.lifr_name
);
3448 * Set the IPv6 interface local point-to-point address.
3450 IN6_LLADDR_FROM_EUI64(lifr
, sin6
, ourid
);
3451 if (myioctl(fd
, SIOCSLIFADDR
, &lifr
) < 0) {
3452 error("Couldn't set local IPv6 address (%s): %m",
3458 * Set the IPv6 interface local point-to-point address.
3460 BZERO(&lifr
, sizeof (lifr
));
3461 (void) strlcpy(lifr
.lifr_name
, ifname
, sizeof (lifr
.lifr_name
));
3462 IN6_LLADDR_FROM_EUI64(lifr
, sin6
, hisid
);
3463 if (myioctl(fd
, SIOCSLIFDSTADDR
, &lifr
) < 0) {
3464 error("Couldn't set remote IPv6 address (%s): %m",
3483 if (!IPV6CP_ENABLED
) {
3487 * Do nothing here, as everything has been done in sif6down().
3495 * Convert 48-bit Ethernet address into 64-bit EUI. Walks the list of valid
3496 * ethernet interfaces, and convert the first found 48-bit MAC address into
3497 * EUI 64. caller also assumes that the system has a properly configured
3498 * Ethernet interface for this function to return non-zero.
3501 ether_to_eui64(p_eui64
)
3504 struct ether_addr eth_addr
;
3506 if (p_eui64
== NULL
) {
3509 if (!get_first_hwaddr(eth_addr
.ether_addr_octet
,
3510 sizeof (eth_addr
.ether_addr_octet
))) {
3514 * And convert the EUI-48 into EUI-64, per RFC 2472 [sec 4.1]
3516 p_eui64
->e8
[0] = (eth_addr
.ether_addr_octet
[0] & 0xFF) | 0x02;
3517 p_eui64
->e8
[1] = (eth_addr
.ether_addr_octet
[1] & 0xFF);
3518 p_eui64
->e8
[2] = (eth_addr
.ether_addr_octet
[2] & 0xFF);
3519 p_eui64
->e8
[3] = 0xFF;
3520 p_eui64
->e8
[4] = 0xFE;
3521 p_eui64
->e8
[5] = (eth_addr
.ether_addr_octet
[3] & 0xFF);
3522 p_eui64
->e8
[6] = (eth_addr
.ether_addr_octet
[4] & 0xFF);
3523 p_eui64
->e8
[7] = (eth_addr
.ether_addr_octet
[5] & 0xFF);
3533 /* see sbuf[] below if you change this list */
3534 static struct bit_ent bit_list
[] = {
3535 { TIOCM_DTR
, "dtr", "DTR" },
3536 { TIOCM_RTS
, "rts", "RTS" },
3537 { TIOCM_CTS
, "cts", "CTS" },
3538 { TIOCM_CD
, "dcd", "DCD" },
3539 { TIOCM_RI
, "ri", "RI" },
3540 { TIOCM_DSR
, "dsr", "DSR" },
3542 { TIOCM_LE
, "disabled", "ENABLED" },
3543 { TIOCM_ST
, NULL
, "2nd-XMIT" },
3544 { TIOCM_SR
, NULL
, "2nd-RECV" },
3550 getbits(int fd
, char *name
, FILE *strptr
)
3553 struct str_list strlist
;
3556 char sbuf
[50]; /* sum of string lengths in bit_list */
3559 nmods
= ioctl(fd
, I_LIST
, NULL
);
3561 error("unable to get module count: %m");
3563 strlist
.sl_nmods
= nmods
;
3564 strlist
.sl_modlist
= malloc(sizeof (struct str_mlist
) * nmods
);
3565 if (strlist
.sl_modlist
== NULL
)
3566 novm("module list");
3567 if (ioctl(fd
, I_LIST
, (caddr_t
)&strlist
) < 0) {
3568 error("unable to get module names: %m");
3570 for (i
= 0; i
< strlist
.sl_nmods
; i
++)
3571 (void) flprintf(strptr
, "%d: %s", i
,
3572 strlist
.sl_modlist
[i
].l_name
);
3573 free(strlist
.sl_modlist
);
3576 if (ioctl(fd
, TIOCMGET
, &mstate
) < 0) {
3577 error("unable to get modem state: %m");
3580 for (be
= bit_list
; be
->val
!= 0; be
++) {
3581 str
= (be
->val
& mstate
) ? be
->on
: be
->off
;
3583 if (sbuf
[0] != '\0')
3584 (void) strcat(sbuf
, " ");
3585 (void) strcat(sbuf
, str
);
3588 (void) flprintf(strptr
, "%s: %s\n", name
, sbuf
);
3593 * Print state of serial link. The stream might be linked under the
3594 * /dev/sppp driver. If it is, then it's necessary to unlink it first
3595 * and relink it when done. Otherwise, it's not possible to use
3596 * ioctl() on the stream.
3599 sys_print_state(FILE *strptr
)
3606 (void) flprintf(strptr
, "serial link is not active");
3609 was_linked
= fdmuxid
!= -1;
3610 if (was_linked
&& ioctl(pppfd
, I_UNLINK
, fdmuxid
) == -1) {
3611 error("I_UNLINK: %m");
3614 getbits(ttyfd
, devnam
, strptr
);
3616 (fdmuxid
= ioctl(pppfd
, I_LINK
, (void *)ttyfd
)) == -1)
3617 fatal("I_LINK: %m");
3622 * send ioctl to driver asking it to block packets with network protocol
3623 * proto in the control queue until the queue for proto is plumbed.
3626 sys_block_proto(uint16_t proto
)
3628 if (proto
> 0x7fff) {
3629 warn("cannot block: not a network proto 0x%lx\n", proto
);
3632 if (strioctl(pppfd
, PPPIO_BLOCKNP
, &proto
, sizeof (proto
), 0) < 0) {
3633 warn("PPPIO_BLOCKNP ioctl failed %m");
3637 * send ioctl to driver asking it to release packets with network protocol
3638 * proto from control queue to the protocol specific queue.
3641 sys_unblock_proto(uint16_t proto
)
3643 if (proto
> 0x7fff) {
3644 warn("cannot unblock: not a network proto 0x%lx\n", proto
);
3647 if (strioctl(pppfd
, PPPIO_UNBLOCKNP
, &proto
, sizeof (proto
), 0) < 0) {
3648 warn("PPPIO_UNBLOCKNP ioctl failed %m");