2 * System-dependent procedures for pppd under Solaris 2.
4 * Copyright (c) 1994 The Australian National University.
7 * Permission to use, copy, modify, and distribute this software and its
8 * documentation is hereby granted, provided that the above copyright
9 * notice appears in all copies. This software is provided without any
10 * warranty, express or implied. The Australian National University
11 * makes no representations about the suitability of this software for
14 * IN NO EVENT SHALL THE AUSTRALIAN NATIONAL UNIVERSITY BE LIABLE TO ANY
15 * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
16 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
17 * THE AUSTRALIAN NATIONAL UNIVERSITY HAVE BEEN ADVISED OF THE POSSIBILITY
20 * THE AUSTRALIAN NATIONAL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
21 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
23 * ON AN "AS IS" BASIS, AND THE AUSTRALIAN NATIONAL UNIVERSITY HAS NO
24 * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
28 #define RCSID "$Id: sys-svr4.c,v 1.35 1999/09/30 19:59:06 masputra Exp $"
40 #include <sys/termiox.h>
44 #include <sys/types.h>
45 #include <sys/ioccom.h>
46 #include <sys/stream.h>
47 #include <sys/stropts.h>
48 #include <sys/socket.h>
49 #include <sys/sockio.h>
50 #include <sys/sysmacros.h>
51 #include <sys/systeminfo.h>
54 #include <sys/mkdev.h>
56 #include <net/if_arp.h>
57 #include <net/route.h>
58 #include <net/ppp_defs.h>
59 #include <net/pppio.h>
60 #include <netinet/in.h>
62 #include <sys/tihdr.h>
63 #include <sys/tiuser.h>
64 #include <inet/common.h>
65 #include <inet/mib2.h>
66 #include <sys/ethernet.h>
71 static const char rcsid[] = RCSID;
74 static int fdmuxid = -1;
76 static int ipmuxid = -1;
78 #if defined(INET6) && defined(SOL2)
79 static int ip6fd; /* IP file descriptor */
80 static int ip6muxid = -1; /* Multiplexer file descriptor */
81 static int if6_is_up = 0; /* IPv6 interface has been marked up */
83 #define _IN6_LLX_FROM_EUI64(l, s, eui64, as) do { \
84 s->sin6_addr.s6_addr32[0] = htonl(as); \
85 eui64_copy(eui64, s->sin6_addr.s6_addr32[2]); \
86 s->sin6_family = AF_INET6; \
87 l.lifr_addr.ss_family = AF_INET6; \
88 l.lifr_addrlen = 10; \
89 l.lifr_addr = laddr; \
92 #define IN6_LLADDR_FROM_EUI64(l, s, eui64) \
93 _IN6_LLX_FROM_EUI64(l, s, eui64, 0xfe800000)
95 #define IN6_LLTOKEN_FROM_EUI64(l, s, eui64) \
96 _IN6_LLX_FROM_EUI64(l, s, eui64, 0)
98 #endif /* defined(INET6) && defined(SOL2) */
100 static int restore_term;
101 static struct termios inittermios;
103 static struct termiox inittermiox;
104 static int termiox_ok;
106 static struct winsize wsinfo; /* Initial window size info */
107 static pid_t tty_sid; /* original session ID for terminal */
109 extern u_char inpacket_buf[]; /* borrowed from main.c */
111 #define MAX_POLLFDS 32
112 static struct pollfd pollfds[MAX_POLLFDS];
113 static int n_pollfds;
115 static int link_mtu, link_mru;
118 static int tty_nmodules;
119 static char tty_modules[NMODULES][FMNAMESZ+1];
120 static int tty_npushed;
122 static int if_is_up; /* Interface has been marked up */
123 static u_int32_t remote_addr; /* IP address of peer */
124 static u_int32_t default_route_gateway; /* Gateway for default route added */
125 static u_int32_t proxy_arp_addr; /* Addr for proxy arp entry added */
127 /* Prototypes for procedures local to this file. */
128 static int translate_speed __P((int));
129 static int baud_rate_of __P((int));
130 static int get_ether_addr __P((u_int32_t, struct sockaddr *));
131 static int get_hw_addr __P((char *, u_int32_t, struct sockaddr *));
132 static int get_hw_addr_dlpi __P((char *, struct sockaddr *));
133 static int dlpi_attach __P((int, int));
134 static int dlpi_info_req __P((int));
135 static int dlpi_get_reply __P((int, union DL_primitives *, int, int));
136 static int strioctl __P((int, int, void *, int, int));
140 * sifppa - Sets interface ppa
142 * without setting the ppa, ip module will return EINVAL upon setting the
143 * interface UP (SIOCSxIFFLAGS). This is because ip module in 2.8 expects
144 * two DLPI_INFO_REQ to be sent down to the driver (below ip) before
145 * IFF_UP can be set. Plumbing the device causes one DLPI_INFO_REQ to
146 * be sent down, and the second DLPI_INFO_REQ is sent upon receiving
147 * IF_UNITSEL (old) or SIOCSLIFNAME (new) ioctls. Such setting of the ppa
148 * is required because the ppp DLPI provider advertises itself as
149 * a DLPI style 2 type, which requires a point of attachment to be
150 * specified. The only way the user can specify a point of attachment
151 * is via SIOCSLIFNAME or IF_UNITSEL.
153 * Such changes in the behavior of ip module was made to meet new or
154 * evolving standards requirements.
162 return (int)ioctl(fd, IF_UNITSEL, (char *)&ppa);
166 #if defined(SOL2) && defined(INET6)
168 * slifname - Sets interface ppa and flags
170 * in addition to the comments stated in sifppa(), IFF_IPV6 bit must
171 * be set in order to declare this as an IPv6 interface
181 memset(&lifr, 0, sizeof(lifr));
182 ret = ioctl(fd, SIOCGLIFFLAGS, &lifr);
186 lifr.lifr_flags |= IFF_IPV6;
187 lifr.lifr_flags &= ~(IFF_BROADCAST | IFF_IPV4);
189 strlcpy(lifr.lifr_name, ifname, sizeof(lifr.lifr_name));
191 ret = ioctl(fd, SIOCSLIFNAME, &lifr);
200 * ether_to_eui64 - Convert 48-bit Ethernet address into 64-bit EUI
202 * walks the list of valid ethernet interfaces, and convert the first
203 * found 48-bit MAC address into EUI 64. caller also assumes that
204 * the system has a properly configured Ethernet interface for this
205 * function to return non-zero.
208 ether_to_eui64(eui64_t *p_eui64)
212 struct lifreq *plifreq;
214 int fd, num_ifs, i, found;
217 struct sockaddr s_eth_addr;
218 struct ether_addr *eth_addr = (struct ether_addr *)&s_eth_addr.sa_data;
220 fd = socket(AF_INET, SOCK_DGRAM, 0);
226 * Find out how many interfaces are running
228 lifn.lifn_family = AF_UNSPEC;
229 lifn.lifn_flags = LIFC_NOXMIT;
230 if (ioctl(fd, SIOCGLIFNUM, &lifn) < 0) {
232 error("could not determine number of interfaces: %m");
236 num_ifs = lifn.lifn_count;
237 req_size = num_ifs * sizeof(struct lifreq);
238 req = malloc(req_size);
241 error("out of memory");
246 * Get interface configuration info for all interfaces
248 lifc.lifc_family = AF_UNSPEC;
249 lifc.lifc_flags = LIFC_NOXMIT;
250 lifc.lifc_len = req_size;
252 if (ioctl(fd, SIOCGLIFCONF, &lifc) < 0) {
255 error("SIOCGLIFCONF: %m");
260 * And traverse each interface to look specifically for the first
261 * occurence of an Ethernet interface which has been marked up
263 plifreq = lifc.lifc_req;
265 for (i = lifc.lifc_len / sizeof(struct lifreq); i>0; i--, plifreq++) {
267 if (strchr(plifreq->lifr_name, ':') != NULL)
270 memset(&lifr, 0, sizeof(lifr));
271 strncpy(lifr.lifr_name, plifreq->lifr_name, sizeof(lifr.lifr_name));
272 if (ioctl(fd, SIOCGLIFFLAGS, &lifr) < 0) {
275 error("SIOCGLIFFLAGS: %m");
278 fl = lifr.lifr_flags;
280 if ((fl & (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
281 != (IFF_UP | IFF_BROADCAST))
291 error("no persistent id can be found");
296 * Send DL_INFO_REQ to the driver to solicit its MAC address
298 if (!get_hw_addr_dlpi(plifreq->lifr_name, &s_eth_addr)) {
299 error("could not obtain hardware address for %s", plifreq->lifr_name);
304 * And convert the EUI-48 into EUI-64, per RFC 2472 [sec 4.1]
306 p_eui64->e8[0] = (eth_addr->ether_addr_octet[0] & 0xFF) | 0x02;
307 p_eui64->e8[1] = (eth_addr->ether_addr_octet[1] & 0xFF);
308 p_eui64->e8[2] = (eth_addr->ether_addr_octet[2] & 0xFF);
309 p_eui64->e8[3] = 0xFF;
310 p_eui64->e8[4] = 0xFE;
311 p_eui64->e8[5] = (eth_addr->ether_addr_octet[3] & 0xFF);
312 p_eui64->e8[6] = (eth_addr->ether_addr_octet[4] & 0xFF);
313 p_eui64->e8[7] = (eth_addr->ether_addr_octet[5] & 0xFF);
317 #endif /* defined(SOL2) && defined(INET6) */
320 * sys_init - System-dependent initialization.
326 #if defined(INET6) && defined(SOL2)
328 #endif /* defined(INET6) && defined(SOL2) */
332 union DL_primitives prim;
335 #endif /* !defined(SOL2) */
337 ipfd = open("/dev/ip", O_RDWR, 0);
339 fatal("Couldn't open IP device: %m");
341 #if defined(INET6) && defined(SOL2)
342 ip6fd = open("/dev/ip", O_RDWR, 0);
344 fatal("Couldn't open IP device (2): %m");
345 #endif /* defined(INET6) && defined(SOL2) */
347 if (default_device && !notty)
348 tty_sid = getsid((pid_t)0);
350 pppfd = open("/dev/ppp", O_RDWR | O_NONBLOCK, 0);
352 fatal("Can't open /dev/ppp: %m");
353 if (kdebugflag & 1) {
354 x = PPPDBG_LOG + PPPDBG_DRIVER;
355 strioctl(pppfd, PPPIO_DEBUG, &x, sizeof(int), 0);
358 /* Assign a new PPA and get its unit number. */
359 if (strioctl(pppfd, PPPIO_NEWPPA, &ifunit, 0, sizeof(int)) < 0)
360 fatal("Can't create new PPP interface: %m");
363 * Open the ppp device again and link it under the ip multiplexor.
364 * IP will assign a unit number which hopefully is the same as ifunit.
365 * I don't know any way to be certain they will be the same. :-(
367 ifd = open("/dev/ppp", O_RDWR, 0);
369 fatal("Can't open /dev/ppp (2): %m");
370 if (kdebugflag & 1) {
371 x = PPPDBG_LOG + PPPDBG_DRIVER;
372 strioctl(ifd, PPPIO_DEBUG, &x, sizeof(int), 0);
375 #if defined(INET6) && defined(SOL2)
377 * Since sys_init() is called prior to ifname being set in main(),
378 * we need to get the ifname now, otherwise slifname() will fail,
379 * or maybe, I should move slifname() to a later point ?
381 sprintf(ifname, "ppp%d", ifunit);
383 i6fd = open("/dev/ppp", O_RDWR, 0);
386 fatal("Can't open /dev/ppp (3): %m");
388 if (kdebugflag & 1) {
389 x = PPPDBG_LOG + PPPDBG_DRIVER;
390 strioctl(i6fd, PPPIO_DEBUG, &x, sizeof(int), 0);
392 #endif /* defined(INET6) && defined(SOL2) */
395 if (ioctl(ifd, I_PUSH, "ip") < 0) {
399 #endif /* defined(INET6) */
400 fatal("Can't push IP module: %m");
404 * Assign ppa according to the unit number returned by ppp device
405 * after plumbing is completed above.
407 if (sifppa(ifd, ifunit) < 0) {
411 #endif /* defined(INET6) */
412 fatal("Can't set ppa for unit %d: %m", ifunit);
417 * An IPv6 interface is created anyway, even when the user does not
418 * explicitly enable it. Note that the interface will be marked
419 * IPv6 during slifname().
421 if (ioctl(i6fd, I_PUSH, "ip") < 0) {
424 fatal("Can't push IP module (2): %m");
428 * Assign ppa according to the unit number returned by ppp device
429 * after plumbing is completed above. In addition, mark the interface
430 * as an IPv6 interface.
432 if (slifname(i6fd, ifunit) < 0) {
435 fatal("Can't set ifname for unit %d: %m", ifunit);
437 #endif /* defined(INET6) */
439 #else /* else if !defined(SOL2) */
441 if (dlpi_attach(ifd, ifunit) < 0 ||
442 dlpi_get_reply(ifd, &reply.prim, DL_OK_ACK, sizeof(reply)) < 0) {
444 fatal("Can't attach to ppp%d: %m", ifunit);
446 #endif /* defined(SOL2) */
448 ipmuxid = ioctl(ipfd, I_LINK, ifd);
451 fatal("Can't link PPP device to IP: %m");
453 #if defined(INET6) && defined(SOL2)
454 ip6muxid = ioctl(ip6fd, I_LINK, i6fd);
457 fatal("Can't link PPP device to IP (2): %m");
458 #endif /* defined(INET6) && defined(SOL2) */
461 /* Set the interface name for the link. */
462 slprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "ppp%d", ifunit);
463 ifr.ifr_metric = ipmuxid;
464 if (strioctl(ipfd, SIOCSIFNAME, (char *)&ifr, sizeof ifr, 0) < 0)
465 fatal("Can't set interface name %s: %m", ifr.ifr_name);
466 #endif /* !defined(SOL2) */
472 * sys_cleanup - restore any system state we modified before exiting:
473 * mark the interface down, delete default route and/or proxy arp entry.
474 * This should call die() because it's called from die().
481 #if defined(SOL2) && defined(INET6)
484 #endif /* defined(SOL2) && defined(INET6) */
487 if (default_route_gateway)
488 cifdefaultroute(0, default_route_gateway, default_route_gateway);
490 cifproxyarp(0, proxy_arp_addr);
494 * sys_close - Clean up in a child process before execing.
500 #if defined(INET6) && defined(SOL2)
502 #endif /* defined(INET6) && defined(SOL2) */
508 * sys_check_options - check the options that the user specified
518 * daemon - Detach us from controlling terminal session.
521 daemon(nochdir, noclose)
522 int nochdir, noclose;
526 if ((pid = fork()) < 0)
529 exit(0); /* parent dies */
534 fclose(stdin); /* don't need stdin, stdout, stderr */
543 * ppp_available - check whether the system has any ppp interfaces
550 return stat("/dev/ppp", &buf) >= 0;
554 * establish_ppp - Turn the serial port into a ppp interface.
562 /* Pop any existing modules off the tty stream. */
564 if (ioctl(fd, I_LOOK, tty_modules[i]) < 0
565 || strcmp(tty_modules[i], "ptem") == 0
566 || ioctl(fd, I_POP, 0) < 0)
570 /* Push the async hdlc module and the compressor module. */
574 if (ioctl(fd, I_PUSH, "ppp_ahdl") < 0) {
575 error("Couldn't push PPP Async HDLC module: %m");
580 if (kdebugflag & 4) {
581 i = PPPDBG_LOG + PPPDBG_AHDLC;
582 strioctl(pppfd, PPPIO_DEBUG, &i, sizeof(int), 0);
584 if (ioctl(fd, I_PUSH, "ppp_comp") < 0)
585 error("Couldn't push PPP compression module: %m");
588 if (kdebugflag & 2) {
589 i = PPPDBG_LOG + PPPDBG_COMP;
590 strioctl(pppfd, PPPIO_DEBUG, &i, sizeof(int), 0);
593 /* Link the serial port under the PPP multiplexor. */
594 if ((fdmuxid = ioctl(pppfd, I_LINK, fd)) < 0) {
595 error("Can't link tty to PPP mux: %m");
603 * restore_loop - reattach the ppp unit to the loopback.
604 * This doesn't need to do anything because disestablish_ppp does it.
612 * disestablish_ppp - Restore the serial port to normal operation.
613 * It attempts to reconstruct the stream with the previously popped
614 * modules. This shouldn't call die() because it's called from die().
623 if (ioctl(pppfd, I_UNLINK, fdmuxid) < 0) {
625 error("Can't unlink tty from PPP mux: %m");
630 while (tty_npushed > 0 && ioctl(fd, I_POP, 0) >= 0)
632 for (i = tty_nmodules - 1; i >= 0; --i)
633 if (ioctl(fd, I_PUSH, tty_modules[i]) < 0)
634 error("Couldn't restore tty module %s: %m",
637 if (hungup && default_device && tty_sid > 0) {
639 * If we have received a hangup, we need to send a SIGHUP
640 * to the terminal's controlling process. The reason is
641 * that the original stream head for the terminal hasn't
642 * seen the M_HANGUP message (it went up through the ppp
643 * driver to the stream head for our fd to /dev/ppp).
645 kill(tty_sid, SIGHUP);
651 * Check whether the link seems not to be 8-bit clean.
659 if (strioctl(pppfd, PPPIO_GCLEAN, &x, 0, sizeof(x)) < 0)
664 s = "bit 7 set to 1";
667 s = "bit 7 set to 0";
677 warn("Serial link is not 8-bit clean:");
678 warn("All received characters had %s", s);
683 * List of valid speeds.
686 int speed_int, speed_val;
761 * Translate from bits/second to a speed_t.
767 struct speed *speedp;
771 for (speedp = speeds; speedp->speed_int; speedp++)
772 if (bps == speedp->speed_int)
773 return speedp->speed_val;
774 warn("speed %d not supported", bps);
779 * Translate from a speed_t to bits/second.
785 struct speed *speedp;
789 for (speedp = speeds; speedp->speed_int; speedp++)
790 if (speed == speedp->speed_val)
791 return speedp->speed_int;
796 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
797 * at the requested speed, etc. If `local' is true, set CLOCAL
798 * regardless of whether the modem option was specified.
801 set_up_tty(fd, local)
806 #if !defined (CRTSCTS)
810 if (!sync_serial && tcgetattr(fd, &tios) < 0)
811 fatal("tcgetattr: %m");
815 if (ioctl (fd, TCGETX, &tiox) < 0) {
827 ioctl(fd, TIOCGWINSZ, &wsinfo);
830 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
833 tios.c_cflag |= CRTSCTS;
834 else if (crtscts < 0)
835 tios.c_cflag &= ~CRTSCTS;
837 if (crtscts != 0 && !termiox_ok) {
838 error("Can't set RTS/CTS flow control");
839 } else if (crtscts > 0) {
840 tiox.x_hflag |= RTSXOFF|CTSXON;
841 } else if (crtscts < 0) {
842 tiox.x_hflag &= ~(RTSXOFF|CTSXON);
846 tios.c_cflag |= CS8 | CREAD | HUPCL;
848 tios.c_cflag |= CLOCAL;
849 tios.c_iflag = IGNBRK | IGNPAR;
853 tios.c_cc[VTIME] = 0;
856 tios.c_iflag |= IXON | IXOFF;
857 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
858 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
861 speed = translate_speed(inspeed);
863 cfsetospeed(&tios, speed);
864 cfsetispeed(&tios, speed);
866 speed = cfgetospeed(&tios);
868 * We can't proceed if the serial port speed is 0,
869 * since that implies that the serial port is disabled.
872 fatal("Baud rate for %s is 0; need explicit baud rate", devnam);
875 if (!sync_serial && tcsetattr(fd, TCSAFLUSH, &tios) < 0)
876 fatal("tcsetattr: %m");
879 if (termiox_ok && ioctl (fd, TCSETXF, &tiox) < 0){
880 error("TCSETXF: %m");
884 baud_rate = inspeed = baud_rate_of(speed);
889 * restore_tty - restore the terminal to the saved settings.
896 if (!default_device) {
898 * Turn off echoing, because otherwise we can get into
899 * a loop with the tty and the modem echoing to each other.
900 * We presume we are the sole user of this tty device, so
901 * when we close it, it will revert to its defaults anyway.
903 inittermios.c_lflag &= ~(ECHO | ECHONL);
905 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
906 if (!hungup && errno != ENXIO)
907 warn("tcsetattr: %m");
909 if (ioctl (fd, TCSETXF, &inittermiox) < 0){
910 if (!hungup && errno != ENXIO)
911 error("TCSETXF: %m");
914 ioctl(fd, TIOCSWINSZ, &wsinfo);
920 * setdtr - control the DTR line on the serial port.
921 * This is called from die(), so it shouldn't call die().
927 int modembits = TIOCM_DTR;
929 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
933 * open_loopback - open the device we use for getting packets
934 * in demand mode. Under Solaris 2, we use our existing fd
944 * output - Output PPP packet.
957 dbglog("sent %P", p, len);
960 data.buf = (caddr_t) p;
962 while (putmsg(pppfd, NULL, &data, 0) < 0) {
963 if (--retries < 0 || (errno != EWOULDBLOCK && errno != EAGAIN)) {
965 error("Couldn't send packet: %m");
969 pfd.events = POLLOUT;
970 poll(&pfd, 1, 250); /* wait for up to 0.25 seconds */
976 * wait_input - wait until there is data available,
977 * for the length of time specified by *timo (indefinite
982 struct timeval *timo;
986 t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
987 if (poll(pollfds, n_pollfds, t) < 0 && errno != EINTR)
992 * add_fd - add an fd to the set that wait_input waits for.
999 for (n = 0; n < n_pollfds; ++n)
1000 if (pollfds[n].fd == fd)
1002 if (n_pollfds < MAX_POLLFDS) {
1003 pollfds[n_pollfds].fd = fd;
1004 pollfds[n_pollfds].events = POLLIN | POLLPRI | POLLHUP;
1007 error("Too many inputs!");
1011 * remove_fd - remove an fd from the set that wait_input waits for.
1018 for (n = 0; n < n_pollfds; ++n) {
1019 if (pollfds[n].fd == fd) {
1020 while (++n < n_pollfds)
1021 pollfds[n-1] = pollfds[n];
1030 * wait_loop_output - wait until there is data available on the
1031 * loopback, for the length of time specified by *timo (indefinite
1035 wait_loop_output(timo)
1036 struct timeval *timo;
1042 * wait_time - wait for a given length of time or until a
1043 * signal is received.
1047 struct timeval *timo;
1051 n = select(0, NULL, NULL, NULL, timo);
1052 if (n < 0 && errno != EINTR)
1053 fatal("select: %m");
1059 * read_packet - get a PPP packet from the serial device.
1065 struct strbuf ctrl, data;
1067 unsigned char ctrlbuf[sizeof(union DL_primitives) + 64];
1070 data.maxlen = PPP_MRU + PPP_HDRLEN;
1071 data.buf = (caddr_t) buf;
1072 ctrl.maxlen = sizeof(ctrlbuf);
1073 ctrl.buf = (caddr_t) ctrlbuf;
1075 len = getmsg(pppfd, &ctrl, &data, &flags);
1077 if (errno == EAGAIN || errno == EINTR)
1079 fatal("Error reading packet: %m");
1086 * Got a M_PROTO or M_PCPROTO message. Interpret it
1087 * as a DLPI primitive??
1090 dbglog("got dlpi prim 0x%x, len=%d",
1091 ((union DL_primitives *)ctrlbuf)->dl_primitive, ctrl.len);
1097 * get_loop_output - get outgoing packets from the ppp device,
1098 * and detect when we want to bring the real link up.
1099 * Return value is 1 if we need to bring up the link, 0 otherwise.
1107 while ((len = read_packet(inpacket_buf)) > 0) {
1108 if (loop_frame(inpacket_buf, len))
1115 * ppp_send_config - configure the transmit characteristics of
1116 * the ppp interface.
1119 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
1126 #if defined(INET6) && defined(SOL2)
1129 #endif /* defined(INET6) && defined(SOL2) */
1132 if (strioctl(pppfd, PPPIO_MTU, &mtu, sizeof(mtu), 0) < 0) {
1133 if (hungup && errno == ENXIO)
1135 error("Couldn't set MTU: %m");
1138 /* can't set these if we don't have a stream attached below /dev/ppp */
1139 if (strioctl(pppfd, PPPIO_XACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
1140 error("Couldn't set transmit ACCM: %m");
1142 cf[0] = (pcomp? COMP_PROT: 0) + (accomp? COMP_AC: 0);
1143 cf[1] = COMP_PROT | COMP_AC;
1144 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
1145 error("Couldn't set prot/AC compression: %m");
1149 /* set the MTU for IP as well */
1150 memset(&ifr, 0, sizeof(ifr));
1151 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1152 ifr.ifr_metric = link_mtu;
1153 if (ioctl(ipfd, SIOCSIFMTU, &ifr) < 0) {
1154 error("Couldn't set IP MTU: %m");
1157 #if defined(INET6) && defined(SOL2)
1158 fd = socket(AF_INET6, SOCK_DGRAM, 0);
1160 error("Couldn't open IPv6 socket: %m");
1162 memset(&lifr, 0, sizeof(lifr));
1163 strlcpy(lifr.lifr_name, ifname, sizeof(lifr.lifr_name));
1164 lifr.lifr_mtu = link_mtu;
1165 if (ioctl(fd, SIOCSLIFMTU, &lifr) < 0) {
1167 error("Couldn't set IPv6 MTU: %m");
1170 #endif /* defined(INET6) && defined(SOL2) */
1174 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
1177 ppp_set_xaccm(unit, accm)
1182 && strioctl(pppfd, PPPIO_XACCM, accm, sizeof(ext_accm), 0) < 0) {
1183 if (!hungup || errno != ENXIO)
1184 warn("Couldn't set extended ACCM: %m");
1189 * ppp_recv_config - configure the receive-side characteristics of
1190 * the ppp interface.
1193 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
1201 if (strioctl(pppfd, PPPIO_MRU, &mru, sizeof(mru), 0) < 0) {
1202 if (hungup && errno == ENXIO)
1204 error("Couldn't set MRU: %m");
1207 /* can't set these if we don't have a stream attached below /dev/ppp */
1208 if (strioctl(pppfd, PPPIO_RACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
1209 error("Couldn't set receive ACCM: %m");
1211 cf[0] = (pcomp? DECOMP_PROT: 0) + (accomp? DECOMP_AC: 0);
1212 cf[1] = DECOMP_PROT | DECOMP_AC;
1213 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
1214 error("Couldn't set prot/AC decompression: %m");
1220 * ccp_test - ask kernel whether a given compression method
1221 * is acceptable for use.
1224 ccp_test(unit, opt_ptr, opt_len, for_transmit)
1225 int unit, opt_len, for_transmit;
1228 if (strioctl(pppfd, (for_transmit? PPPIO_XCOMP: PPPIO_RCOMP),
1229 opt_ptr, opt_len, 0) >= 0)
1231 return (errno == ENOSR)? 0: -1;
1235 * ccp_flags_set - inform kernel about the current state of CCP.
1238 ccp_flags_set(unit, isopen, isup)
1239 int unit, isopen, isup;
1243 cf[0] = (isopen? CCP_ISOPEN: 0) + (isup? CCP_ISUP: 0);
1244 cf[1] = CCP_ISOPEN | CCP_ISUP | CCP_ERROR | CCP_FATALERROR;
1245 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
1246 if (!hungup || errno != ENXIO)
1247 error("Couldn't set kernel CCP state: %m");
1252 * get_idle_time - return how long the link has been idle.
1255 get_idle_time(u, ip)
1257 struct ppp_idle *ip;
1259 return strioctl(pppfd, PPPIO_GIDLE, ip, 0, sizeof(struct ppp_idle)) >= 0;
1263 * get_ppp_stats - return statistics for the link.
1266 get_ppp_stats(u, stats)
1268 struct pppd_stats *stats;
1272 if (strioctl(pppfd, PPPIO_GETSTAT, &s, 0, sizeof(s)) < 0) {
1273 error("Couldn't get link statistics: %m");
1276 stats->bytes_in = s.p.ppp_ibytes;
1277 stats->bytes_out = s.p.ppp_obytes;
1283 * set_filters - transfer the pass and active filters to the kernel.
1286 set_filters(pass, active)
1287 struct bpf_program *pass, *active;
1291 if (pass->bf_len > 0) {
1292 if (strioctl(pppfd, PPPIO_PASSFILT, pass,
1293 sizeof(struct bpf_program), 0) < 0) {
1294 error("Couldn't set pass-filter in kernel: %m");
1298 if (active->bf_len > 0) {
1299 if (strioctl(pppfd, PPPIO_ACTIVEFILT, active,
1300 sizeof(struct bpf_program), 0) < 0) {
1301 error("Couldn't set active-filter in kernel: %m");
1310 * ccp_fatal_error - returns 1 if decompression was disabled as a
1311 * result of an error detected after decompression of a packet,
1312 * 0 otherwise. This is necessary because of patent nonsense.
1315 ccp_fatal_error(unit)
1321 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
1322 if (errno != ENXIO && errno != EINVAL)
1323 error("Couldn't get compression flags: %m");
1326 return cf[0] & CCP_FATALERROR;
1330 * sifvjcomp - config tcp header compression
1333 sifvjcomp(u, vjcomp, xcidcomp, xmaxcid)
1334 int u, vjcomp, xcidcomp, xmaxcid;
1340 maxcid[0] = xcidcomp;
1341 maxcid[1] = 15; /* XXX should be rmaxcid */
1342 if (strioctl(pppfd, PPPIO_VJINIT, maxcid, sizeof(maxcid), 0) < 0) {
1343 error("Couldn't initialize VJ compression: %m");
1347 cf[0] = (vjcomp? COMP_VJC + DECOMP_VJC: 0) /* XXX this is wrong */
1348 + (xcidcomp? COMP_VJCCID + DECOMP_VJCCID: 0);
1349 cf[1] = COMP_VJC + DECOMP_VJC + COMP_VJCCID + DECOMP_VJCCID;
1350 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
1352 error("Couldn't enable VJ compression: %m");
1359 * sifup - Config the interface up and enable IP packets to pass.
1367 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1368 if (ioctl(ipfd, SIOCGIFFLAGS, &ifr) < 0) {
1369 error("Couldn't mark interface up (get): %m");
1372 ifr.ifr_flags |= IFF_UP;
1373 if (ioctl(ipfd, SIOCSIFFLAGS, &ifr) < 0) {
1374 error("Couldn't mark interface up (set): %m");
1382 * sifdown - Config the interface down and disable IP.
1392 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1393 if (ioctl(ipfd, SIOCGIFFLAGS, &ifr) < 0) {
1394 error("Couldn't mark interface down (get): %m");
1397 ifr.ifr_flags &= ~IFF_UP;
1398 if (ioctl(ipfd, SIOCSIFFLAGS, &ifr) < 0) {
1399 error("Couldn't mark interface down (set): %m");
1407 * sifnpmode - Set the mode for handling packets for a given NP.
1410 sifnpmode(u, proto, mode)
1418 npi[1] = (int) mode;
1419 if (strioctl(pppfd, PPPIO_NPMODE, &npi, 2 * sizeof(int), 0) < 0) {
1420 error("ioctl(set NP %d mode to %d): %m", proto, mode);
1426 #if defined(SOL2) && defined(INET6)
1428 * sif6up - Config the IPv6 interface up and enable IPv6 packets to pass.
1437 fd = socket(AF_INET6, SOCK_DGRAM, 0);
1442 memset(&lifr, 0, sizeof(lifr));
1443 strlcpy(lifr.lifr_name, ifname, sizeof(lifr.lifr_name));
1444 if (ioctl(fd, SIOCGLIFFLAGS, &lifr) < 0) {
1449 lifr.lifr_flags |= IFF_UP;
1450 strlcpy(lifr.lifr_name, ifname, sizeof(lifr.lifr_name));
1451 if (ioctl(fd, SIOCSLIFFLAGS, &lifr) < 0) {
1462 * sifdown - Config the IPv6 interface down and disable IPv6.
1471 fd = socket(AF_INET6, SOCK_DGRAM, 0);
1475 memset(&lifr, 0, sizeof(lifr));
1476 strlcpy(lifr.lifr_name, ifname, sizeof(lifr.lifr_name));
1477 if (ioctl(fd, SIOCGLIFFLAGS, &lifr) < 0) {
1482 lifr.lifr_flags &= ~IFF_UP;
1483 strlcpy(lifr.lifr_name, ifname, sizeof(lifr.lifr_name));
1484 if (ioctl(fd, SIOCGLIFFLAGS, &lifr) < 0) {
1495 * sif6addr - Config the interface with an IPv6 link-local address
1503 struct sockaddr_storage laddr;
1504 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&laddr;
1507 fd = socket(AF_INET6, SOCK_DGRAM, 0);
1511 memset(&lifr, 0, sizeof(lifr));
1512 strlcpy(lifr.lifr_name, ifname, sizeof(lifr.lifr_name));
1515 * Do this because /dev/ppp responds to DL_PHYS_ADDR_REQ with
1516 * zero values, hence the interface token came to be zero too,
1517 * and without this, in.ndpd will complain
1519 IN6_LLTOKEN_FROM_EUI64(lifr, sin6, o);
1520 if (ioctl(fd, SIOCSLIFTOKEN, &lifr) < 0) {
1526 * Set the interface address and destination address
1528 IN6_LLADDR_FROM_EUI64(lifr, sin6, o);
1529 if (ioctl(fd, SIOCSLIFADDR, &lifr) < 0) {
1534 memset(&lifr, 0, sizeof(lifr));
1535 strlcpy(lifr.lifr_name, ifname, sizeof(lifr.lifr_name));
1536 IN6_LLADDR_FROM_EUI64(lifr, sin6, h);
1537 if (ioctl(fd, SIOCSLIFDSTADDR, &lifr) < 0) {
1546 * cif6addr - Remove the IPv6 address from interface
1556 #endif /* defined(SOL2) && defined(INET6) */
1559 #define INET_ADDR(x) (((struct sockaddr_in *) &(x))->sin_addr.s_addr)
1562 * sifaddr - Config the interface IP addresses and netmask.
1572 memset(&ifr, 0, sizeof(ifr));
1573 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1574 ifr.ifr_addr.sa_family = AF_INET;
1575 INET_ADDR(ifr.ifr_addr) = m;
1576 if (ioctl(ipfd, SIOCSIFNETMASK, &ifr) < 0) {
1577 error("Couldn't set IP netmask: %m");
1580 ifr.ifr_addr.sa_family = AF_INET;
1581 INET_ADDR(ifr.ifr_addr) = o;
1582 if (ioctl(ipfd, SIOCSIFADDR, &ifr) < 0) {
1583 error("Couldn't set local IP address: %m");
1588 * On some systems, we have to explicitly set the point-to-point
1589 * flag bit before we can set a destination address.
1591 if (ioctl(ipfd, SIOCGIFFLAGS, &ifr) >= 0
1592 && (ifr.ifr_flags & IFF_POINTOPOINT) == 0) {
1593 ifr.ifr_flags |= IFF_POINTOPOINT;
1594 if (ioctl(ipfd, SIOCSIFFLAGS, &ifr) < 0) {
1595 error("Couldn't mark interface pt-to-pt: %m");
1599 ifr.ifr_dstaddr.sa_family = AF_INET;
1600 INET_ADDR(ifr.ifr_dstaddr) = h;
1601 if (ioctl(ipfd, SIOCSIFDSTADDR, &ifr) < 0) {
1602 error("Couldn't set remote IP address: %m");
1605 #if 0 /* now done in ppp_send_config */
1606 ifr.ifr_metric = link_mtu;
1607 if (ioctl(ipfd, SIOCSIFMTU, &ifr) < 0) {
1608 error("Couldn't set IP MTU: %m");
1617 * cifaddr - Clear the interface IP addresses, and delete routes
1618 * through the interface if possible.
1625 #if defined(__USLC__) /* was: #if 0 */
1626 cifroute(unit, ouraddr, hisaddr);
1628 notice("Removing ppp interface unit");
1629 if (ioctl(ipfd, I_UNLINK, ipmuxid) < 0) {
1630 error("Can't remove ppp interface unit: %m");
1641 * sifdefaultroute - assign a default route through the address given.
1644 sifdefaultroute(u, l, g)
1650 #if defined(__USLC__)
1651 g = l; /* use the local address as gateway */
1653 memset(&rt, 0, sizeof(rt));
1654 rt.rt_dst.sa_family = AF_INET;
1655 INET_ADDR(rt.rt_dst) = 0;
1656 rt.rt_gateway.sa_family = AF_INET;
1657 INET_ADDR(rt.rt_gateway) = g;
1658 rt.rt_flags = RTF_GATEWAY;
1660 if (ioctl(ipfd, SIOCADDRT, &rt) < 0) {
1661 error("Can't add default route: %m");
1665 default_route_gateway = g;
1670 * cifdefaultroute - delete a default route through the address given.
1673 cifdefaultroute(u, l, g)
1679 #if defined(__USLC__)
1680 g = l; /* use the local address as gateway */
1682 memset(&rt, 0, sizeof(rt));
1683 rt.rt_dst.sa_family = AF_INET;
1684 INET_ADDR(rt.rt_dst) = 0;
1685 rt.rt_gateway.sa_family = AF_INET;
1686 INET_ADDR(rt.rt_gateway) = g;
1687 rt.rt_flags = RTF_GATEWAY;
1689 if (ioctl(ipfd, SIOCDELRT, &rt) < 0) {
1690 error("Can't delete default route: %m");
1694 default_route_gateway = 0;
1699 * sifproxyarp - Make a proxy ARP entry for the peer.
1702 sifproxyarp(unit, hisaddr)
1706 struct arpreq arpreq;
1708 memset(&arpreq, 0, sizeof(arpreq));
1709 if (!get_ether_addr(hisaddr, &arpreq.arp_ha))
1712 arpreq.arp_pa.sa_family = AF_INET;
1713 INET_ADDR(arpreq.arp_pa) = hisaddr;
1714 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1715 if (ioctl(ipfd, SIOCSARP, (caddr_t) &arpreq) < 0) {
1716 error("Couldn't set proxy ARP entry: %m");
1720 proxy_arp_addr = hisaddr;
1725 * cifproxyarp - Delete the proxy ARP entry for the peer.
1728 cifproxyarp(unit, hisaddr)
1732 struct arpreq arpreq;
1734 memset(&arpreq, 0, sizeof(arpreq));
1735 arpreq.arp_pa.sa_family = AF_INET;
1736 INET_ADDR(arpreq.arp_pa) = hisaddr;
1737 if (ioctl(ipfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1738 error("Couldn't delete proxy ARP entry: %m");
1747 * get_ether_addr - get the hardware address of an interface on the
1748 * the same subnet as ipaddr.
1753 get_ether_addr(ipaddr, hwaddr)
1755 struct sockaddr *hwaddr;
1757 struct ifreq *ifr, *ifend, ifreq;
1760 u_int32_t ina, mask;
1763 * Scan through the system's network interfaces.
1766 if (ioctl(ipfd, SIOCGIFNUM, &nif) < 0)
1769 ifc.ifc_len = nif * sizeof(struct ifreq);
1770 ifc.ifc_buf = (caddr_t) malloc(ifc.ifc_len);
1771 if (ifc.ifc_buf == 0)
1773 if (ioctl(ipfd, SIOCGIFCONF, &ifc) < 0) {
1774 warn("Couldn't get system interface list: %m");
1778 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1779 for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
1780 if (ifr->ifr_addr.sa_family != AF_INET)
1783 * Check that the interface is up, and not point-to-point or loopback.
1785 strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1786 if (ioctl(ipfd, SIOCGIFFLAGS, &ifreq) < 0)
1788 if ((ifreq.ifr_flags &
1789 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1790 != (IFF_UP|IFF_BROADCAST))
1793 * Get its netmask and check that it's on the right subnet.
1795 if (ioctl(ipfd, SIOCGIFNETMASK, &ifreq) < 0)
1797 ina = INET_ADDR(ifr->ifr_addr);
1798 mask = INET_ADDR(ifreq.ifr_addr);
1799 if ((ipaddr & mask) == (ina & mask))
1804 warn("No suitable interface found for proxy ARP");
1809 info("found interface %s for proxy ARP", ifr->ifr_name);
1810 if (!get_hw_addr(ifr->ifr_name, ina, hwaddr)) {
1811 error("Couldn't get hardware address for %s", ifr->ifr_name);
1821 * get_hw_addr_dlpi - obtain the hardware address using DLPI
1824 get_hw_addr_dlpi(name, hwaddr)
1826 struct sockaddr *hwaddr;
1829 int unit, iffd, adrlen;
1830 unsigned char *adrp;
1833 union DL_primitives prim;
1838 * We have to open the device and ask it for its hardware address.
1839 * First split apart the device name and unit.
1841 slprintf(ifdev, sizeof(ifdev), "/dev/%s", name);
1842 for (q = ifdev + strlen(ifdev); --q >= ifdev; )
1849 * Open the device and do a DLPI attach and phys_addr_req.
1851 iffd = open(ifdev, O_RDWR);
1853 error("Can't open %s: %m", ifdev);
1856 if (dlpi_attach(iffd, unit) < 0
1857 || dlpi_get_reply(iffd, &reply.prim, DL_OK_ACK, sizeof(reply)) < 0
1858 || dlpi_info_req(iffd) < 0
1859 || dlpi_get_reply(iffd, &reply.prim, DL_INFO_ACK, sizeof(reply)) < 0) {
1864 adrlen = reply.prim.info_ack.dl_addr_length;
1865 adrp = (unsigned char *)&reply + reply.prim.info_ack.dl_addr_offset;
1867 #if DL_CURRENT_VERSION >= 2
1868 if (reply.prim.info_ack.dl_sap_length < 0)
1869 adrlen += reply.prim.info_ack.dl_sap_length;
1871 adrp += reply.prim.info_ack.dl_sap_length;
1874 hwaddr->sa_family = AF_UNSPEC;
1875 memcpy(hwaddr->sa_data, adrp, adrlen);
1880 * get_hw_addr - obtain the hardware address for a named interface.
1883 get_hw_addr(name, ina, hwaddr)
1886 struct sockaddr *hwaddr;
1888 /* New way - get the address by doing an arp request. */
1892 s = socket(AF_INET, SOCK_DGRAM, 0);
1895 memset(&req, 0, sizeof(req));
1896 req.arp_pa.sa_family = AF_INET;
1897 INET_ADDR(req.arp_pa) = ina;
1898 if (ioctl(s, SIOCGARP, &req) < 0) {
1899 error("Couldn't get ARP entry for %s: %m", ip_ntoa(ina));
1902 *hwaddr = req.arp_ha;
1903 hwaddr->sa_family = AF_UNSPEC;
1909 dlpi_attach(fd, ppa)
1912 dl_attach_req_t req;
1915 req.dl_primitive = DL_ATTACH_REQ;
1917 buf.len = sizeof(req);
1918 buf.buf = (void *) &req;
1919 return putmsg(fd, &buf, NULL, RS_HIPRI);
1929 req.dl_primitive = DL_INFO_REQ;
1930 buf.len = sizeof(req);
1931 buf.buf = (void *) &req;
1932 return putmsg(fd, &buf, NULL, RS_HIPRI);
1936 dlpi_get_reply(fd, reply, expected_prim, maxlen)
1937 union DL_primitives *reply;
1938 int fd, expected_prim, maxlen;
1945 * Use poll to wait for a message with a timeout.
1948 pfd.events = POLLIN | POLLPRI;
1950 n = poll(&pfd, 1, 1000);
1951 } while (n == -1 && errno == EINTR);
1958 buf.maxlen = maxlen;
1959 buf.buf = (void *) reply;
1961 if (getmsg(fd, &buf, NULL, &flags) < 0)
1964 if (buf.len < sizeof(ulong)) {
1966 dbglog("dlpi response short (len=%d)\n", buf.len);
1970 if (reply->dl_primitive == expected_prim)
1974 if (reply->dl_primitive == DL_ERROR_ACK) {
1975 dbglog("dlpi error %d (unix errno %d) for prim %x\n",
1976 reply->error_ack.dl_errno, reply->error_ack.dl_unix_errno,
1977 reply->error_ack.dl_error_primitive);
1979 dbglog("dlpi unexpected response prim %x\n",
1980 reply->dl_primitive);
1988 * Return user specified netmask, modified by any mask we might determine
1989 * for address `addr' (in network byte order).
1990 * Here we scan through the system's list of interfaces, looking for
1991 * any non-point-to-point interfaces which might appear to be on the same
1992 * network as `addr'. If we find any, we OR in their netmask to the
1993 * user-specified netmask.
1999 u_int32_t mask, nmask, ina;
2000 struct ifreq *ifr, *ifend, ifreq;
2005 if (IN_CLASSA(addr)) /* determine network mask for address class */
2006 nmask = IN_CLASSA_NET;
2007 else if (IN_CLASSB(addr))
2008 nmask = IN_CLASSB_NET;
2010 nmask = IN_CLASSC_NET;
2011 /* class D nets are disallowed by bad_ip_adrs */
2012 mask = netmask | htonl(nmask);
2015 * Scan through the system's network interfaces.
2018 if (ioctl(ipfd, SIOCGIFNUM, &nif) < 0)
2021 ifc.ifc_len = nif * sizeof(struct ifreq);
2022 ifc.ifc_buf = (caddr_t) malloc(ifc.ifc_len);
2023 if (ifc.ifc_buf == 0)
2025 if (ioctl(ipfd, SIOCGIFCONF, &ifc) < 0) {
2026 warn("Couldn't get system interface list: %m");
2030 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
2031 for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
2033 * Check the interface's internet address.
2035 if (ifr->ifr_addr.sa_family != AF_INET)
2037 ina = INET_ADDR(ifr->ifr_addr);
2038 if ((ntohl(ina) & nmask) != (addr & nmask))
2041 * Check that the interface is up, and not point-to-point or loopback.
2043 strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
2044 if (ioctl(ipfd, SIOCGIFFLAGS, &ifreq) < 0)
2046 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
2050 * Get its netmask and OR it into our mask.
2052 if (ioctl(ipfd, SIOCGIFNETMASK, &ifreq) < 0)
2054 mask |= INET_ADDR(ifreq.ifr_addr);
2062 * logwtmp - write an accounting record to the /var/adm/wtmp file.
2065 logwtmp(line, name, host)
2066 const char *line, *name, *host;
2068 static struct utmpx utmpx;
2072 strncpy(utmpx.ut_user, name, sizeof(utmpx.ut_user));
2073 strncpy(utmpx.ut_id, ifname, sizeof(utmpx.ut_id));
2074 strncpy(utmpx.ut_line, line, sizeof(utmpx.ut_line));
2075 utmpx.ut_pid = getpid();
2076 utmpx.ut_type = USER_PROCESS;
2078 utmpx.ut_type = DEAD_PROCESS;
2080 gettimeofday(&utmpx.ut_tv, NULL);
2081 updwtmpx("/var/adm/wtmpx", &utmpx);
2085 * get_host_seed - return the serial number of this machine.
2092 if (sysinfo(SI_HW_SERIAL, buf, sizeof(buf)) < 0) {
2093 error("sysinfo: %m");
2096 return (int) strtoul(buf, NULL, 16);
2100 strioctl(fd, cmd, ptr, ilen, olen)
2101 int fd, cmd, ilen, olen;
2104 struct strioctl str;
2110 if (ioctl(fd, I_STR, &str) == -1)
2112 if (str.ic_len != olen)
2113 dbglog("strioctl: expected %d bytes, got %d for cmd %x\n",
2114 olen, str.ic_len, cmd);
2120 * lock - create a lock file for the named lock device
2123 #define LOCK_PREFIX "/var/spool/locks/LK."
2124 static char lock_file[40]; /* name of lock file created */
2134 if (stat(dev, &sbuf) < 0) {
2135 error("Can't get device number for %s: %m", dev);
2138 if ((sbuf.st_mode & S_IFMT) != S_IFCHR) {
2139 error("Can't lock %s: not a character device", dev);
2142 slprintf(lock_file, sizeof(lock_file), "%s%03d.%03d.%03d",
2143 LOCK_PREFIX, major(sbuf.st_dev),
2144 major(sbuf.st_rdev), minor(sbuf.st_rdev));
2146 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
2148 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
2149 /* Read the lock file to find out who has the device locked */
2150 n = read(fd, ascii_pid, 11);
2152 error("Can't read pid from lock file %s", lock_file);
2156 pid = atoi(ascii_pid);
2157 if (pid > 0 && kill(pid, 0) == -1 && errno == ESRCH) {
2158 /* pid no longer exists - remove the lock file */
2159 if (unlink(lock_file) == 0) {
2161 notice("Removed stale lock on %s (pid %d)",
2165 warn("Couldn't remove stale lock on %s",
2168 notice("Device %s is locked by pid %d",
2173 error("Can't create lock file %s: %m", lock_file);
2178 slprintf(ascii_pid, sizeof(ascii_pid), "%10d\n", getpid());
2179 write(fd, ascii_pid, 11);
2186 * unlock - remove our lockfile
2199 * cifroute - delete a route through the addresses given.
2202 cifroute(u, our, his)
2208 memset(&rt, 0, sizeof(rt));
2209 rt.rt_dst.sa_family = AF_INET;
2210 INET_ADDR(rt.rt_dst) = his;
2211 rt.rt_gateway.sa_family = AF_INET;
2212 INET_ADDR(rt.rt_gateway) = our;
2213 rt.rt_flags = RTF_HOST;
2215 if (ioctl(ipfd, SIOCDELRT, &rt) < 0) {
2216 error("Can't delete route: %m");
2224 * have_route_to - determine if the system has a route to the specified
2225 * IP address. Returns 0 if not, 1 if so, -1 if we can't tell.
2226 * `addr' is in network byte order.
2227 * For demand mode to work properly, we have to ignore routes
2228 * through our own interface.
2230 #ifndef T_CURRENT /* needed for Solaris 2.5 */
2231 #define T_CURRENT MI_T_CURRENT
2239 int fd, r, flags, i;
2241 struct T_optmgmt_req req;
2245 struct T_optmgmt_ack ack;
2246 unsigned char space[64];
2249 struct strbuf cbuf, dbuf;
2251 mib2_ipRouteEntry_t routes[8];
2252 mib2_ipRouteEntry_t *rp;
2254 fd = open("/dev/ip", O_RDWR);
2256 warn("have_route_to: couldn't open /dev/ip: %m");
2260 req.req.PRIM_type = T_OPTMGMT_REQ;
2261 req.req.OPT_offset = (char *) &req.hdr - (char *) &req;
2262 req.req.OPT_length = sizeof(req.hdr);
2263 req.req.MGMT_flags = T_CURRENT;
2265 req.hdr.level = MIB2_IP;
2269 cbuf.buf = (char *) &req;
2270 cbuf.len = sizeof(req);
2272 if (putmsg(fd, &cbuf, NULL, 0) == -1) {
2273 warn("have_route_to: putmsg: %m");
2279 cbuf.buf = (char *) &ack;
2280 cbuf.maxlen = sizeof(ack);
2281 dbuf.buf = (char *) routes;
2282 dbuf.maxlen = sizeof(routes);
2284 r = getmsg(fd, &cbuf, &dbuf, &flags);
2286 warn("have_route_to: getmsg: %m");
2291 if (cbuf.len < sizeof(struct T_optmgmt_ack)
2292 || ack.ack.PRIM_type != T_OPTMGMT_ACK
2293 || ack.ack.MGMT_flags != T_SUCCESS
2294 || ack.ack.OPT_length < sizeof(struct opthdr)) {
2295 dbglog("have_route_to: bad message len=%d prim=%d",
2296 cbuf.len, ack.ack.PRIM_type);
2301 rh = (struct opthdr *) ((char *)&ack + ack.ack.OPT_offset);
2302 if (rh->level == 0 && rh->name == 0)
2304 if (rh->level != MIB2_IP || rh->name != MIB2_IP_21) {
2305 while (r == MOREDATA)
2306 r = getmsg(fd, NULL, &dbuf, &flags);
2311 nroutes = dbuf.len / sizeof(mib2_ipRouteEntry_t);
2312 for (rp = routes, i = 0; i < nroutes; ++i, ++rp) {
2313 if (rp->ipRouteMask != ~0) {
2314 dbglog("have_route_to: dest=%x gw=%x mask=%x\n",
2315 rp->ipRouteDest, rp->ipRouteNextHop,
2317 if (((addr ^ rp->ipRouteDest) & rp->ipRouteMask) == 0
2318 && rp->ipRouteNextHop != remote_addr)
2324 r = getmsg(fd, NULL, &dbuf, &flags);
2335 * get_pty - get a pty master/slave pair and chown the slave side to
2336 * the uid given. Assumes slave_name points to MAXPATHLEN bytes of space.
2339 get_pty(master_fdp, slave_fdp, slave_name, uid)
2347 struct termios tios;
2349 mfd = open("/dev/ptmx", O_RDWR);
2351 error("Couldn't open pty master: %m");
2355 pty_name = ptsname(mfd);
2356 if (pty_name == NULL) {
2357 error("Couldn't get name of pty slave");
2361 if (chown(pty_name, uid, -1) < 0)
2362 warn("Couldn't change owner of pty slave: %m");
2363 if (chmod(pty_name, S_IRUSR | S_IWUSR) < 0)
2364 warn("Couldn't change permissions on pty slave: %m");
2365 if (unlockpt(mfd) < 0)
2366 warn("Couldn't unlock pty slave: %m");
2368 sfd = open(pty_name, O_RDWR);
2370 error("Couldn't open pty slave %s: %m", pty_name);
2374 if (ioctl(sfd, I_PUSH, "ptem") < 0)
2375 warn("Couldn't push ptem module on pty slave: %m");
2377 dbglog("Using %s", pty_name);
2378 strlcpy(slave_name, pty_name, MAXPATHLEN);