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,
29 static char rcsid[] = "$Id: sys-svr4.c,v 1.26 1999/03/16 22:53:48 paulus Exp $";
42 #include <sys/termiox.h>
46 #include <sys/types.h>
47 #include <sys/ioccom.h>
48 #include <sys/stream.h>
49 #include <sys/stropts.h>
50 #include <sys/socket.h>
51 #include <sys/sockio.h>
52 #include <sys/sysmacros.h>
53 #include <sys/systeminfo.h>
56 #include <sys/mkdev.h>
58 #include <net/if_arp.h>
59 #include <net/route.h>
60 #include <net/ppp_defs.h>
61 #include <net/pppio.h>
62 #include <netinet/in.h>
63 #include <sys/tihdr.h>
64 #include <sys/tiuser.h>
65 #include <inet/common.h>
66 #include <inet/mib2.h>
71 static int fdmuxid = -1;
73 static int ipmuxid = -1;
75 static int restore_term;
76 static struct termios inittermios;
78 static struct termiox inittermiox;
79 static int termiox_ok;
81 static struct winsize wsinfo; /* Initial window size info */
82 static pid_t tty_sid; /* original session ID for terminal */
84 extern u_char inpacket_buf[]; /* borrowed from main.c */
86 #define MAX_POLLFDS 32
87 static struct pollfd pollfds[MAX_POLLFDS];
90 static int link_mtu, link_mru;
93 static int tty_nmodules;
94 static char tty_modules[NMODULES][FMNAMESZ+1];
96 static int if_is_up; /* Interface has been marked up */
97 static u_int32_t remote_addr; /* IP address of peer */
98 static u_int32_t default_route_gateway; /* Gateway for default route added */
99 static u_int32_t proxy_arp_addr; /* Addr for proxy arp entry added */
101 /* Prototypes for procedures local to this file. */
102 static int translate_speed __P((int));
103 static int baud_rate_of __P((int));
104 static int get_ether_addr __P((u_int32_t, struct sockaddr *));
105 static int get_hw_addr __P((char *, u_int32_t, struct sockaddr *));
106 static int dlpi_attach __P((int, int));
107 static int dlpi_info_req __P((int));
108 static int dlpi_get_reply __P((int, union DL_primitives *, int, int));
109 static int strioctl __P((int, int, void *, int, int));
113 * sys_init - System-dependent initialization.
122 union DL_primitives prim;
127 ipfd = open("/dev/ip", O_RDWR, 0);
129 fatal("Couldn't open IP device: %m");
132 tty_sid = getsid((pid_t)0);
134 pppfd = open("/dev/ppp", O_RDWR | O_NONBLOCK, 0);
136 fatal("Can't open /dev/ppp: %m");
137 if (kdebugflag & 1) {
138 x = PPPDBG_LOG + PPPDBG_DRIVER;
139 strioctl(pppfd, PPPIO_DEBUG, &x, sizeof(int), 0);
142 /* Assign a new PPA and get its unit number. */
143 if (strioctl(pppfd, PPPIO_NEWPPA, &ifunit, 0, sizeof(int)) < 0)
144 fatal("Can't create new PPP interface: %m");
147 * Open the ppp device again and link it under the ip multiplexor.
148 * IP will assign a unit number which hopefully is the same as ifunit.
149 * I don't know any way to be certain they will be the same. :-(
151 ifd = open("/dev/ppp", O_RDWR, 0);
153 fatal("Can't open /dev/ppp (2): %m");
154 if (kdebugflag & 1) {
155 x = PPPDBG_LOG + PPPDBG_DRIVER;
156 strioctl(ifd, PPPIO_DEBUG, &x, sizeof(int), 0);
159 if (ioctl(ifd, I_PUSH, "ip") < 0) {
161 fatal("Can't push IP module: %m");
164 if (dlpi_attach(ifd, ifunit) < 0 ||
165 dlpi_get_reply(ifd, &reply.prim, DL_OK_ACK, sizeof(reply)) < 0) {
167 fatal("Can't attach to ppp%d: %m", ifunit);
170 ipmuxid = ioctl(ipfd, I_LINK, ifd);
173 fatal("Can't link PPP device to IP: %m");
176 /* Set the interface name for the link. */
177 slprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "ppp%d", ifunit);
178 ifr.ifr_metric = ipmuxid;
179 if (strioctl(ipfd, SIOCSIFNAME, (char *)&ifr, sizeof ifr, 0) < 0)
180 fatal("Can't set interface name %s: %m", ifr.ifr_name);
187 * sys_cleanup - restore any system state we modified before exiting:
188 * mark the interface down, delete default route and/or proxy arp entry.
189 * This should call die() because it's called from die().
198 if (default_route_gateway)
199 cifdefaultroute(0, default_route_gateway, default_route_gateway);
201 cifproxyarp(0, proxy_arp_addr);
205 * sys_close - Clean up in a child process before execing.
216 * sys_check_options - check the options that the user specified
226 * daemon - Detach us from controlling terminal session.
229 daemon(nochdir, noclose)
230 int nochdir, noclose;
234 if ((pid = fork()) < 0)
237 exit(0); /* parent dies */
242 fclose(stdin); /* don't need stdin, stdout, stderr */
250 * ppp_available - check whether the system has any ppp interfaces
257 return stat("/dev/ppp", &buf) >= 0;
261 * establish_ppp - Turn the serial port into a ppp interface.
269 /* Pop any existing modules off the tty stream. */
271 if (ioctl(fd, I_LOOK, tty_modules[i]) < 0
272 || ioctl(fd, I_POP, 0) < 0)
276 /* Push the async hdlc module and the compressor module. */
277 if (ioctl(fd, I_PUSH, "ppp_ahdl") < 0)
278 fatal("Couldn't push PPP Async HDLC module: %m");
279 if (kdebugflag & 4) {
280 i = PPPDBG_LOG + PPPDBG_AHDLC;
281 strioctl(pppfd, PPPIO_DEBUG, &i, sizeof(int), 0);
283 if (ioctl(fd, I_PUSH, "ppp_comp") < 0)
284 error("Couldn't push PPP compression module: %m");
285 if (kdebugflag & 2) {
286 i = PPPDBG_LOG + PPPDBG_COMP;
287 strioctl(pppfd, PPPIO_DEBUG, &i, sizeof(int), 0);
290 /* Link the serial port under the PPP multiplexor. */
291 if ((fdmuxid = ioctl(pppfd, I_LINK, fd)) < 0)
292 fatal("Can't link tty to PPP mux: %m");
298 * restore_loop - reattach the ppp unit to the loopback.
299 * This doesn't need to do anything because disestablish_ppp does it.
307 * disestablish_ppp - Restore the serial port to normal operation.
308 * It attempts to reconstruct the stream with the previously popped
309 * modules. This shouldn't call die() because it's called from die().
318 if (ioctl(pppfd, I_UNLINK, fdmuxid) < 0) {
320 error("Can't unlink tty from PPP mux: %m");
325 while (ioctl(fd, I_POP, 0) >= 0)
327 for (i = tty_nmodules - 1; i >= 0; --i)
328 if (ioctl(fd, I_PUSH, tty_modules[i]) < 0)
329 error("Couldn't restore tty module %s: %m",
332 if (hungup && default_device && tty_sid > 0) {
334 * If we have received a hangup, we need to send a SIGHUP
335 * to the terminal's controlling process. The reason is
336 * that the original stream head for the terminal hasn't
337 * seen the M_HANGUP message (it went up through the ppp
338 * driver to the stream head for our fd to /dev/ppp).
340 kill(tty_sid, SIGHUP);
346 * Check whether the link seems not to be 8-bit clean.
354 if (strioctl(pppfd, PPPIO_GCLEAN, &x, 0, sizeof(x)) < 0)
359 s = "bit 7 set to 1";
362 s = "bit 7 set to 0";
372 warn("Serial link is not 8-bit clean:");
373 warn("All received characters had %s", s);
378 * List of valid speeds.
381 int speed_int, speed_val;
453 * Translate from bits/second to a speed_t.
459 struct speed *speedp;
463 for (speedp = speeds; speedp->speed_int; speedp++)
464 if (bps == speedp->speed_int)
465 return speedp->speed_val;
466 warn("speed %d not supported", bps);
471 * Translate from a speed_t to bits/second.
477 struct speed *speedp;
481 for (speedp = speeds; speedp->speed_int; speedp++)
482 if (speed == speedp->speed_val)
483 return speedp->speed_int;
488 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
489 * at the requested speed, etc. If `local' is true, set CLOCAL
490 * regardless of whether the modem option was specified.
493 set_up_tty(fd, local)
498 #if !defined (CRTSCTS)
502 if (tcgetattr(fd, &tios) < 0)
503 fatal("tcgetattr: %m");
507 if (ioctl (fd, TCGETX, &tiox) < 0) {
519 ioctl(fd, TIOCGWINSZ, &wsinfo);
522 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
525 tios.c_cflag |= CRTSCTS;
526 else if (crtscts < 0)
527 tios.c_cflag &= ~CRTSCTS;
529 if (crtscts != 0 && !termiox_ok) {
530 error("Can't set RTS/CTS flow control");
531 } else if (crtscts > 0) {
532 tiox.x_hflag |= RTSXOFF|CTSXON;
533 } else if (crtscts < 0) {
534 tiox.x_hflag &= ~(RTSXOFF|CTSXON);
538 tios.c_cflag |= CS8 | CREAD | HUPCL;
540 tios.c_cflag |= CLOCAL;
541 tios.c_iflag = IGNBRK | IGNPAR;
545 tios.c_cc[VTIME] = 0;
548 tios.c_iflag |= IXON | IXOFF;
549 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
550 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
553 speed = translate_speed(inspeed);
555 cfsetospeed(&tios, speed);
556 cfsetispeed(&tios, speed);
558 speed = cfgetospeed(&tios);
560 * We can't proceed if the serial port speed is 0,
561 * since that implies that the serial port is disabled.
564 fatal("Baud rate for %s is 0; need explicit baud rate", devnam);
567 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0)
568 fatal("tcsetattr: %m");
571 if (termiox_ok && ioctl (fd, TCSETXF, &tiox) < 0){
572 error("TCSETXF: %m");
576 baud_rate = inspeed = baud_rate_of(speed);
581 * restore_tty - restore the terminal to the saved settings.
588 if (!default_device) {
590 * Turn off echoing, because otherwise we can get into
591 * a loop with the tty and the modem echoing to each other.
592 * We presume we are the sole user of this tty device, so
593 * when we close it, it will revert to its defaults anyway.
595 inittermios.c_lflag &= ~(ECHO | ECHONL);
597 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
598 if (!hungup && errno != ENXIO)
599 warn("tcsetattr: %m");
601 if (ioctl (fd, TCSETXF, &inittermiox) < 0){
602 if (!hungup && errno != ENXIO)
603 error("TCSETXF: %m");
606 ioctl(fd, TIOCSWINSZ, &wsinfo);
612 * setdtr - control the DTR line on the serial port.
613 * This is called from die(), so it shouldn't call die().
619 int modembits = TIOCM_DTR;
621 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
625 * open_loopback - open the device we use for getting packets
626 * in demand mode. Under Solaris 2, we use our existing fd
636 * output - Output PPP packet.
649 dbglog("sent %P", p, len);
652 data.buf = (caddr_t) p;
654 while (putmsg(pppfd, NULL, &data, 0) < 0) {
655 if (--retries < 0 || (errno != EWOULDBLOCK && errno != EAGAIN)) {
657 error("Couldn't send packet: %m");
661 pfd.events = POLLOUT;
662 poll(&pfd, 1, 250); /* wait for up to 0.25 seconds */
668 * wait_input - wait until there is data available,
669 * for the length of time specified by *timo (indefinite
674 struct timeval *timo;
678 t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
679 if (poll(pollfds, n_pollfds, t) < 0 && errno != EINTR)
684 * add_fd - add an fd to the set that wait_input waits for.
690 for (n = 0; n < n_pollfds; ++n)
691 if (pollfds[n].fd == fd)
693 if (n_pollfds < MAX_POLLFDS) {
694 pollfds[n_pollfds].fd = fd;
695 pollfds[n_pollfds].events = POLLIN | POLLPRI | POLLHUP;
698 error("Too many inputs!");
702 * remove_fd - remove an fd from the set that wait_input waits for.
704 void remove_fd(int fd)
708 for (n = 0; n < n_pollfds; ++n) {
709 if (pollfds[n].fd == fd) {
710 while (++n < n_pollfds)
711 pollfds[n-1] = pollfds[n];
720 * wait_loop_output - wait until there is data available on the
721 * loopback, for the length of time specified by *timo (indefinite
725 wait_loop_output(timo)
726 struct timeval *timo;
732 * wait_time - wait for a given length of time or until a
733 * signal is received.
737 struct timeval *timo;
741 n = select(0, NULL, NULL, NULL, timo);
742 if (n < 0 && errno != EINTR)
749 * read_packet - get a PPP packet from the serial device.
755 struct strbuf ctrl, data;
757 unsigned char ctrlbuf[sizeof(union DL_primitives) + 64];
760 data.maxlen = PPP_MRU + PPP_HDRLEN;
761 data.buf = (caddr_t) buf;
762 ctrl.maxlen = sizeof(ctrlbuf);
763 ctrl.buf = (caddr_t) ctrlbuf;
765 len = getmsg(pppfd, &ctrl, &data, &flags);
767 if (errno == EAGAIN || errno == EINTR)
769 fatal("Error reading packet: %m");
776 * Got a M_PROTO or M_PCPROTO message. Interpret it
777 * as a DLPI primitive??
780 dbglog("got dlpi prim 0x%x, len=%d",
781 ((union DL_primitives *)ctrlbuf)->dl_primitive, ctrl.len);
787 * get_loop_output - get outgoing packets from the ppp device,
788 * and detect when we want to bring the real link up.
789 * Return value is 1 if we need to bring up the link, 0 otherwise.
797 while ((len = read_packet(inpacket_buf)) > 0) {
798 if (loop_frame(inpacket_buf, len))
805 * ppp_send_config - configure the transmit characteristics of
809 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
818 if (strioctl(pppfd, PPPIO_MTU, &mtu, sizeof(mtu), 0) < 0) {
819 if (hungup && errno == ENXIO)
821 error("Couldn't set MTU: %m");
824 /* can't set these if we don't have a stream attached below /dev/ppp */
825 if (strioctl(pppfd, PPPIO_XACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
826 error("Couldn't set transmit ACCM: %m");
828 cf[0] = (pcomp? COMP_PROT: 0) + (accomp? COMP_AC: 0);
829 cf[1] = COMP_PROT | COMP_AC;
830 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
831 error("Couldn't set prot/AC compression: %m");
835 /* set the MTU for IP as well */
836 memset(&ifr, 0, sizeof(ifr));
837 strlcpy(ifr.ifr_name, sizeof(ifr.ifr_name), ifname);
838 ifr.ifr_metric = link_mtu;
839 if (ioctl(ipfd, SIOCSIFMTU, &ifr) < 0) {
840 error("Couldn't set IP MTU: %m");
845 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
848 ppp_set_xaccm(unit, accm)
853 && strioctl(pppfd, PPPIO_XACCM, accm, sizeof(ext_accm), 0) < 0) {
854 if (!hungup || errno != ENXIO)
855 warn("Couldn't set extended ACCM: %m");
860 * ppp_recv_config - configure the receive-side characteristics of
864 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
872 if (strioctl(pppfd, PPPIO_MRU, &mru, sizeof(mru), 0) < 0) {
873 if (hungup && errno == ENXIO)
875 error("Couldn't set MRU: %m");
878 /* can't set these if we don't have a stream attached below /dev/ppp */
879 if (strioctl(pppfd, PPPIO_RACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
880 error("Couldn't set receive ACCM: %m");
882 cf[0] = (pcomp? DECOMP_PROT: 0) + (accomp? DECOMP_AC: 0);
883 cf[1] = DECOMP_PROT | DECOMP_AC;
884 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
885 error("Couldn't set prot/AC decompression: %m");
891 * ccp_test - ask kernel whether a given compression method
892 * is acceptable for use.
895 ccp_test(unit, opt_ptr, opt_len, for_transmit)
896 int unit, opt_len, for_transmit;
899 if (strioctl(pppfd, (for_transmit? PPPIO_XCOMP: PPPIO_RCOMP),
900 opt_ptr, opt_len, 0) >= 0)
902 return (errno == ENOSR)? 0: -1;
906 * ccp_flags_set - inform kernel about the current state of CCP.
909 ccp_flags_set(unit, isopen, isup)
910 int unit, isopen, isup;
914 cf[0] = (isopen? CCP_ISOPEN: 0) + (isup? CCP_ISUP: 0);
915 cf[1] = CCP_ISOPEN | CCP_ISUP | CCP_ERROR | CCP_FATALERROR;
916 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
917 if (!hungup || errno != ENXIO)
918 error("Couldn't set kernel CCP state: %m");
923 * get_idle_time - return how long the link has been idle.
930 return strioctl(pppfd, PPPIO_GIDLE, ip, 0, sizeof(struct ppp_idle)) >= 0;
935 * set_filters - transfer the pass and active filters to the kernel.
938 set_filters(pass, active)
939 struct bpf_program *pass, *active;
943 if (pass->bf_len > 0) {
944 if (strioctl(pppfd, PPPIO_PASSFILT, pass,
945 sizeof(struct bpf_program), 0) < 0) {
946 error("Couldn't set pass-filter in kernel: %m");
950 if (active->bf_len > 0) {
951 if (strioctl(pppfd, PPPIO_ACTIVEFILT, active,
952 sizeof(struct bpf_program), 0) < 0) {
953 error("Couldn't set active-filter in kernel: %m");
962 * ccp_fatal_error - returns 1 if decompression was disabled as a
963 * result of an error detected after decompression of a packet,
964 * 0 otherwise. This is necessary because of patent nonsense.
967 ccp_fatal_error(unit)
973 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
974 if (errno != ENXIO && errno != EINVAL)
975 error("Couldn't get compression flags: %m");
978 return cf[0] & CCP_FATALERROR;
982 * sifvjcomp - config tcp header compression
985 sifvjcomp(u, vjcomp, xcidcomp, xmaxcid)
986 int u, vjcomp, xcidcomp, xmaxcid;
992 maxcid[0] = xcidcomp;
993 maxcid[1] = 15; /* XXX should be rmaxcid */
994 if (strioctl(pppfd, PPPIO_VJINIT, maxcid, sizeof(maxcid), 0) < 0) {
995 error("Couldn't initialize VJ compression: %m");
999 cf[0] = (vjcomp? COMP_VJC + DECOMP_VJC: 0) /* XXX this is wrong */
1000 + (xcidcomp? COMP_VJCCID + DECOMP_VJCCID: 0);
1001 cf[1] = COMP_VJC + DECOMP_VJC + COMP_VJCCID + DECOMP_VJCCID;
1002 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
1004 error("Couldn't enable VJ compression: %m");
1011 * sifup - Config the interface up and enable IP packets to pass.
1019 strlcpy(ifr.ifr_name, sizeof(ifr.ifr_name), ifname);
1020 if (ioctl(ipfd, SIOCGIFFLAGS, &ifr) < 0) {
1021 error("Couldn't mark interface up (get): %m");
1024 ifr.ifr_flags |= IFF_UP;
1025 if (ioctl(ipfd, SIOCSIFFLAGS, &ifr) < 0) {
1026 error("Couldn't mark interface up (set): %m");
1034 * sifdown - Config the interface down and disable IP.
1044 strlcpy(ifr.ifr_name, sizeof(ifr.ifr_name), ifname);
1045 if (ioctl(ipfd, SIOCGIFFLAGS, &ifr) < 0) {
1046 error("Couldn't mark interface down (get): %m");
1049 ifr.ifr_flags &= ~IFF_UP;
1050 if (ioctl(ipfd, SIOCSIFFLAGS, &ifr) < 0) {
1051 error("Couldn't mark interface down (set): %m");
1059 * sifnpmode - Set the mode for handling packets for a given NP.
1062 sifnpmode(u, proto, mode)
1070 npi[1] = (int) mode;
1071 if (strioctl(pppfd, PPPIO_NPMODE, &npi, 2 * sizeof(int), 0) < 0) {
1072 error("ioctl(set NP %d mode to %d): %m", proto, mode);
1078 #define INET_ADDR(x) (((struct sockaddr_in *) &(x))->sin_addr.s_addr)
1081 * sifaddr - Config the interface IP addresses and netmask.
1091 memset(&ifr, 0, sizeof(ifr));
1092 strlcpy(ifr.ifr_name, sizeof(ifr.ifr_name), ifname);
1093 ifr.ifr_addr.sa_family = AF_INET;
1094 INET_ADDR(ifr.ifr_addr) = m;
1095 if (ioctl(ipfd, SIOCSIFNETMASK, &ifr) < 0) {
1096 error("Couldn't set IP netmask: %m");
1099 ifr.ifr_addr.sa_family = AF_INET;
1100 INET_ADDR(ifr.ifr_addr) = o;
1101 if (ioctl(ipfd, SIOCSIFADDR, &ifr) < 0) {
1102 error("Couldn't set local IP address: %m");
1107 * On some systems, we have to explicitly set the point-to-point
1108 * flag bit before we can set a destination address.
1110 if (ioctl(ipfd, SIOCGIFFLAGS, &ifr) >= 0
1111 && (ifr.ifr_flags & IFF_POINTOPOINT) == 0) {
1112 ifr.ifr_flags |= IFF_POINTOPOINT;
1113 if (ioctl(ipfd, SIOCSIFFLAGS, &ifr) < 0) {
1114 error("Couldn't mark interface pt-to-pt: %m");
1118 ifr.ifr_dstaddr.sa_family = AF_INET;
1119 INET_ADDR(ifr.ifr_dstaddr) = h;
1120 if (ioctl(ipfd, SIOCSIFDSTADDR, &ifr) < 0) {
1121 error("Couldn't set remote IP address: %m");
1124 #if 0 /* now done in ppp_send_config */
1125 ifr.ifr_metric = link_mtu;
1126 if (ioctl(ipfd, SIOCSIFMTU, &ifr) < 0) {
1127 error("Couldn't set IP MTU: %m");
1136 * cifaddr - Clear the interface IP addresses, and delete routes
1137 * through the interface if possible.
1144 #if defined(__USLC__) /* was: #if 0 */
1145 cifroute(unit, ouraddr, hisaddr);
1147 notice("Removing ppp interface unit");
1148 if (ioctl(ipfd, I_UNLINK, ipmuxid) < 0) {
1149 error("Can't remove ppp interface unit: %m");
1160 * sifdefaultroute - assign a default route through the address given.
1163 sifdefaultroute(u, l, g)
1169 #if defined(__USLC__)
1170 g = l; /* use the local address as gateway */
1172 memset(&rt, 0, sizeof(rt));
1173 rt.rt_dst.sa_family = AF_INET;
1174 INET_ADDR(rt.rt_dst) = 0;
1175 rt.rt_gateway.sa_family = AF_INET;
1176 INET_ADDR(rt.rt_gateway) = g;
1177 rt.rt_flags = RTF_GATEWAY;
1179 if (ioctl(ipfd, SIOCADDRT, &rt) < 0) {
1180 error("Can't add default route: %m");
1184 default_route_gateway = g;
1189 * cifdefaultroute - delete a default route through the address given.
1192 cifdefaultroute(u, l, g)
1198 #if defined(__USLC__)
1199 g = l; /* use the local address as gateway */
1201 memset(&rt, 0, sizeof(rt));
1202 rt.rt_dst.sa_family = AF_INET;
1203 INET_ADDR(rt.rt_dst) = 0;
1204 rt.rt_gateway.sa_family = AF_INET;
1205 INET_ADDR(rt.rt_gateway) = g;
1206 rt.rt_flags = RTF_GATEWAY;
1208 if (ioctl(ipfd, SIOCDELRT, &rt) < 0) {
1209 error("Can't delete default route: %m");
1213 default_route_gateway = 0;
1218 * sifproxyarp - Make a proxy ARP entry for the peer.
1221 sifproxyarp(unit, hisaddr)
1225 struct arpreq arpreq;
1227 memset(&arpreq, 0, sizeof(arpreq));
1228 if (!get_ether_addr(hisaddr, &arpreq.arp_ha))
1231 arpreq.arp_pa.sa_family = AF_INET;
1232 INET_ADDR(arpreq.arp_pa) = hisaddr;
1233 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1234 if (ioctl(ipfd, SIOCSARP, (caddr_t) &arpreq) < 0) {
1235 error("Couldn't set proxy ARP entry: %m");
1239 proxy_arp_addr = hisaddr;
1244 * cifproxyarp - Delete the proxy ARP entry for the peer.
1247 cifproxyarp(unit, hisaddr)
1251 struct arpreq arpreq;
1253 memset(&arpreq, 0, sizeof(arpreq));
1254 arpreq.arp_pa.sa_family = AF_INET;
1255 INET_ADDR(arpreq.arp_pa) = hisaddr;
1256 if (ioctl(ipfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1257 error("Couldn't delete proxy ARP entry: %m");
1266 * get_ether_addr - get the hardware address of an interface on the
1267 * the same subnet as ipaddr.
1272 get_ether_addr(ipaddr, hwaddr)
1274 struct sockaddr *hwaddr;
1276 struct ifreq *ifr, *ifend, ifreq;
1279 u_int32_t ina, mask;
1282 * Scan through the system's network interfaces.
1285 if (ioctl(ipfd, SIOCGIFNUM, &nif) < 0)
1288 ifc.ifc_len = nif * sizeof(struct ifreq);
1289 ifc.ifc_buf = (caddr_t) malloc(ifc.ifc_len);
1290 if (ifc.ifc_buf == 0)
1292 if (ioctl(ipfd, SIOCGIFCONF, &ifc) < 0) {
1293 warn("Couldn't get system interface list: %m");
1297 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1298 for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
1299 if (ifr->ifr_addr.sa_family != AF_INET)
1302 * Check that the interface is up, and not point-to-point or loopback.
1304 strlcpy(ifreq.ifr_name, sizeof(ifreq.ifr_name), ifr->ifr_name);
1305 if (ioctl(ipfd, SIOCGIFFLAGS, &ifreq) < 0)
1307 if ((ifreq.ifr_flags &
1308 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1309 != (IFF_UP|IFF_BROADCAST))
1312 * Get its netmask and check that it's on the right subnet.
1314 if (ioctl(ipfd, SIOCGIFNETMASK, &ifreq) < 0)
1316 ina = INET_ADDR(ifr->ifr_addr);
1317 mask = INET_ADDR(ifreq.ifr_addr);
1318 if ((ipaddr & mask) == (ina & mask))
1323 warn("No suitable interface found for proxy ARP");
1328 info("found interface %s for proxy ARP", ifr->ifr_name);
1329 if (!get_hw_addr(ifr->ifr_name, ina, hwaddr)) {
1330 error("Couldn't get hardware address for %s", ifr->ifr_name);
1340 * get_hw_addr - obtain the hardware address for a named interface.
1343 get_hw_addr(name, ina, hwaddr)
1346 struct sockaddr *hwaddr;
1349 /* New way - get the address by doing an arp request. */
1353 s = socket(AF_INET, SOCK_DGRAM, 0);
1356 memset(&req, 0, sizeof(req));
1357 req.arp_pa.sa_family = AF_INET;
1358 INET_ADDR(req.arp_pa) = ina;
1359 if (ioctl(s, SIOCGARP, &req) < 0) {
1360 error("Couldn't get ARP entry for %s: %m", ip_ntoa(ina));
1363 *hwaddr = req.arp_ha;
1364 hwaddr->sa_family = AF_UNSPEC;
1368 int unit, iffd, adrlen;
1369 unsigned char *adrp;
1372 union DL_primitives prim;
1377 * We have to open the device and ask it for its hardware address.
1378 * First split apart the device name and unit.
1380 slprintf(ifdev, sizeof(ifdev), "/dev/%s", name);
1381 for (q = ifdev + strlen(ifdev); --q >= ifdev; )
1388 * Open the device and do a DLPI attach and phys_addr_req.
1390 iffd = open(ifdev, O_RDWR);
1392 error("Can't open %s: %m", ifdev);
1395 if (dlpi_attach(iffd, unit) < 0
1396 || dlpi_get_reply(iffd, &reply.prim, DL_OK_ACK, sizeof(reply)) < 0
1397 || dlpi_info_req(iffd) < 0
1398 || dlpi_get_reply(iffd, &reply.prim, DL_INFO_ACK, sizeof(reply)) < 0) {
1403 adrlen = reply.prim.info_ack.dl_addr_length;
1404 adrp = (unsigned char *)&reply + reply.prim.info_ack.dl_addr_offset;
1405 #if DL_CURRENT_VERSION >= 2
1406 if (reply.prim.info_ack.dl_sap_length < 0)
1407 adrlen += reply.prim.info_ack.dl_sap_length;
1409 adrp += reply.prim.info_ack.dl_sap_length;
1411 hwaddr->sa_family = AF_UNSPEC;
1412 memcpy(hwaddr->sa_data, adrp, adrlen);
1419 dlpi_attach(fd, ppa)
1422 dl_attach_req_t req;
1425 req.dl_primitive = DL_ATTACH_REQ;
1427 buf.len = sizeof(req);
1428 buf.buf = (void *) &req;
1429 return putmsg(fd, &buf, NULL, RS_HIPRI);
1439 req.dl_primitive = DL_INFO_REQ;
1440 buf.len = sizeof(req);
1441 buf.buf = (void *) &req;
1442 return putmsg(fd, &buf, NULL, RS_HIPRI);
1446 dlpi_get_reply(fd, reply, expected_prim, maxlen)
1447 union DL_primitives *reply;
1448 int fd, expected_prim, maxlen;
1455 * Use poll to wait for a message with a timeout.
1458 pfd.events = POLLIN | POLLPRI;
1460 n = poll(&pfd, 1, 1000);
1461 } while (n == -1 && errno == EINTR);
1468 buf.maxlen = maxlen;
1469 buf.buf = (void *) reply;
1471 if (getmsg(fd, &buf, NULL, &flags) < 0)
1474 if (buf.len < sizeof(ulong)) {
1476 dbglog("dlpi response short (len=%d)\n", buf.len);
1480 if (reply->dl_primitive == expected_prim)
1484 if (reply->dl_primitive == DL_ERROR_ACK) {
1485 dbglog("dlpi error %d (unix errno %d) for prim %x\n",
1486 reply->error_ack.dl_errno, reply->error_ack.dl_unix_errno,
1487 reply->error_ack.dl_error_primitive);
1489 dbglog("dlpi unexpected response prim %x\n",
1490 reply->dl_primitive);
1498 * Return user specified netmask, modified by any mask we might determine
1499 * for address `addr' (in network byte order).
1500 * Here we scan through the system's list of interfaces, looking for
1501 * any non-point-to-point interfaces which might appear to be on the same
1502 * network as `addr'. If we find any, we OR in their netmask to the
1503 * user-specified netmask.
1509 u_int32_t mask, nmask, ina;
1510 struct ifreq *ifr, *ifend, ifreq;
1515 if (IN_CLASSA(addr)) /* determine network mask for address class */
1516 nmask = IN_CLASSA_NET;
1517 else if (IN_CLASSB(addr))
1518 nmask = IN_CLASSB_NET;
1520 nmask = IN_CLASSC_NET;
1521 /* class D nets are disallowed by bad_ip_adrs */
1522 mask = netmask | htonl(nmask);
1525 * Scan through the system's network interfaces.
1528 if (ioctl(ipfd, SIOCGIFNUM, &nif) < 0)
1531 ifc.ifc_len = nif * sizeof(struct ifreq);
1532 ifc.ifc_buf = (caddr_t) malloc(ifc.ifc_len);
1533 if (ifc.ifc_buf == 0)
1535 if (ioctl(ipfd, SIOCGIFCONF, &ifc) < 0) {
1536 warn("Couldn't get system interface list: %m");
1540 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1541 for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
1543 * Check the interface's internet address.
1545 if (ifr->ifr_addr.sa_family != AF_INET)
1547 ina = INET_ADDR(ifr->ifr_addr);
1548 if ((ntohl(ina) & nmask) != (addr & nmask))
1551 * Check that the interface is up, and not point-to-point or loopback.
1553 strlcpy(ifreq.ifr_name, sizeof(ifreq.ifr_name), ifr->ifr_name);
1554 if (ioctl(ipfd, SIOCGIFFLAGS, &ifreq) < 0)
1556 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1560 * Get its netmask and OR it into our mask.
1562 if (ioctl(ipfd, SIOCGIFNETMASK, &ifreq) < 0)
1564 mask |= INET_ADDR(ifreq.ifr_addr);
1572 * logwtmp - write an accounting record to the /var/adm/wtmp file.
1575 logwtmp(line, name, host)
1576 const char *line, *name, *host;
1578 static struct utmpx utmpx;
1582 strlcpy(utmpx.ut_user, sizeof(utmpx.ut_user), name);
1583 strlcpy(utmpx.ut_id, sizeof(utmpx.ut_id), ifname);
1584 strlcpy(utmpx.ut_line, sizeof(utmpx.ut_line), line);
1585 utmpx.ut_pid = getpid();
1586 utmpx.ut_type = USER_PROCESS;
1588 utmpx.ut_type = DEAD_PROCESS;
1590 gettimeofday(&utmpx.ut_tv, NULL);
1591 updwtmpx("/var/adm/wtmpx", &utmpx);
1595 * get_host_seed - return the serial number of this machine.
1602 if (sysinfo(SI_HW_SERIAL, buf, sizeof(buf)) < 0) {
1603 error("sysinfo: %m");
1606 return (int) strtoul(buf, NULL, 16);
1610 strioctl(fd, cmd, ptr, ilen, olen)
1611 int fd, cmd, ilen, olen;
1614 struct strioctl str;
1620 if (ioctl(fd, I_STR, &str) == -1)
1622 if (str.ic_len != olen)
1623 dbglog("strioctl: expected %d bytes, got %d for cmd %x\n",
1624 olen, str.ic_len, cmd);
1629 * lock - create a lock file for the named lock device
1632 #define LOCK_PREFIX "/var/spool/locks/LK."
1633 static char lock_file[40]; /* name of lock file created */
1643 if (stat(dev, &sbuf) < 0) {
1644 error("Can't get device number for %s: %m", dev);
1647 if ((sbuf.st_mode & S_IFMT) != S_IFCHR) {
1648 error("Can't lock %s: not a character device", dev);
1651 slprintf(lock_file, sizeof(lock_file), "%s%03d.%03d.%03d",
1652 LOCK_PREFIX, major(sbuf.st_dev),
1653 major(sbuf.st_rdev), minor(sbuf.st_rdev));
1655 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1657 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1658 /* Read the lock file to find out who has the device locked */
1659 n = read(fd, ascii_pid, 11);
1661 error("Can't read pid from lock file %s", lock_file);
1665 pid = atoi(ascii_pid);
1666 if (pid > 0 && kill(pid, 0) == -1 && errno == ESRCH) {
1667 /* pid no longer exists - remove the lock file */
1668 if (unlink(lock_file) == 0) {
1670 notice("Removed stale lock on %s (pid %d)",
1674 warn("Couldn't remove stale lock on %s",
1677 notice("Device %s is locked by pid %d",
1682 error("Can't create lock file %s: %m", lock_file);
1687 slprintf(ascii_pid, sizeof(ascii_pid), "%10d\n", getpid());
1688 write(fd, ascii_pid, 11);
1695 * unlock - remove our lockfile
1708 * cifroute - delete a route through the addresses given.
1711 cifroute(u, our, his)
1717 memset(&rt, 0, sizeof(rt));
1718 rt.rt_dst.sa_family = AF_INET;
1719 INET_ADDR(rt.rt_dst) = his;
1720 rt.rt_gateway.sa_family = AF_INET;
1721 INET_ADDR(rt.rt_gateway) = our;
1722 rt.rt_flags = RTF_HOST;
1724 if (ioctl(ipfd, SIOCDELRT, &rt) < 0) {
1725 error("Can't delete route: %m");
1733 * have_route_to - determine if the system has a route to the specified
1734 * IP address. Returns 0 if not, 1 if so, -1 if we can't tell.
1735 * `addr' is in network byte order.
1736 * For demand mode to work properly, we have to ignore routes
1737 * through our own interface.
1739 #ifndef T_CURRENT /* needed for Solaris 2.5 */
1740 #define T_CURRENT MI_T_CURRENT
1747 int fd, r, flags, i;
1749 struct T_optmgmt_req req;
1753 struct T_optmgmt_ack ack;
1754 unsigned char space[64];
1757 struct strbuf cbuf, dbuf;
1759 mib2_ipRouteEntry_t routes[8];
1760 mib2_ipRouteEntry_t *rp;
1762 fd = open("/dev/ip", O_RDWR);
1764 warn("have_route_to: couldn't open /dev/ip: %m");
1768 req.req.PRIM_type = T_OPTMGMT_REQ;
1769 req.req.OPT_offset = (char *) &req.hdr - (char *) &req;
1770 req.req.OPT_length = sizeof(req.hdr);
1771 req.req.MGMT_flags = T_CURRENT;
1773 req.hdr.level = MIB2_IP;
1777 cbuf.buf = (char *) &req;
1778 cbuf.len = sizeof(req);
1780 if (putmsg(fd, &cbuf, NULL, 0) == -1) {
1781 warn("have_route_to: putmsg: %m");
1787 cbuf.buf = (char *) &ack;
1788 cbuf.maxlen = sizeof(ack);
1789 dbuf.buf = (char *) routes;
1790 dbuf.maxlen = sizeof(routes);
1792 r = getmsg(fd, &cbuf, &dbuf, &flags);
1794 warn("have_route_to: getmsg: %m");
1799 if (cbuf.len < sizeof(struct T_optmgmt_ack)
1800 || ack.ack.PRIM_type != T_OPTMGMT_ACK
1801 || ack.ack.MGMT_flags != T_SUCCESS
1802 || ack.ack.OPT_length < sizeof(struct opthdr)) {
1803 dbglog("have_route_to: bad message len=%d prim=%d",
1804 cbuf.len, ack.ack.PRIM_type);
1809 rh = (struct opthdr *) ((char *)&ack + ack.ack.OPT_offset);
1810 if (rh->level == 0 && rh->name == 0)
1812 if (rh->level != MIB2_IP || rh->name != MIB2_IP_21) {
1813 while (r == MOREDATA)
1814 r = getmsg(fd, NULL, &dbuf, &flags);
1819 nroutes = dbuf.len / sizeof(mib2_ipRouteEntry_t);
1820 for (rp = routes, i = 0; i < nroutes; ++i, ++rp) {
1821 if (rp->ipRouteMask != ~0) {
1822 dbglog("have_route_to: dest=%x gw=%x mask=%x\n",
1823 rp->ipRouteDest, rp->ipRouteNextHop,
1825 if (((addr ^ rp->ipRouteDest) & rp->ipRouteMask) == 0
1826 && rp->ipRouteNextHop != remote_addr)
1832 r = getmsg(fd, NULL, &dbuf, &flags);