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.24 1999/03/12 06:07:23 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/syslog.h>
53 #include <sys/sysmacros.h>
54 #include <sys/systeminfo.h>
57 #include <sys/mkdev.h>
59 #include <net/if_arp.h>
60 #include <net/route.h>
61 #include <net/ppp_defs.h>
62 #include <net/pppio.h>
63 #include <netinet/in.h>
64 #include <sys/tihdr.h>
65 #include <sys/tiuser.h>
66 #include <inet/common.h>
67 #include <inet/mib2.h>
72 static int fdmuxid = -1;
74 static int ipmuxid = -1;
76 static int restore_term;
77 static struct termios inittermios;
79 static struct termiox inittermiox;
80 static int termiox_ok;
82 static struct winsize wsinfo; /* Initial window size info */
83 static pid_t tty_sid; /* original session ID for terminal */
85 extern u_char inpacket_buf[]; /* borrowed from main.c */
87 static int link_mtu, link_mru;
90 static int tty_nmodules;
91 static char tty_modules[NMODULES][FMNAMESZ+1];
93 static int if_is_up; /* Interface has been marked up */
94 static u_int32_t remote_addr; /* IP address of peer */
95 static u_int32_t default_route_gateway; /* Gateway for default route added */
96 static u_int32_t proxy_arp_addr; /* Addr for proxy arp entry added */
98 /* Prototypes for procedures local to this file. */
99 static int translate_speed __P((int));
100 static int baud_rate_of __P((int));
101 static int get_ether_addr __P((u_int32_t, struct sockaddr *));
102 static int get_hw_addr __P((char *, u_int32_t, struct sockaddr *));
103 static int dlpi_attach __P((int, int));
104 static int dlpi_info_req __P((int));
105 static int dlpi_get_reply __P((int, union DL_primitives *, int, int));
106 static int strioctl __P((int, int, void *, int, int));
110 * sys_init - System-dependent initialization.
119 union DL_primitives prim;
124 ipfd = open("/dev/ip", O_RDWR, 0);
126 syslog(LOG_ERR, "Couldn't open IP device: %m");
131 tty_sid = getsid((pid_t)0);
133 pppfd = open("/dev/ppp", O_RDWR | O_NONBLOCK, 0);
135 syslog(LOG_ERR, "Can't open /dev/ppp: %m");
138 if (kdebugflag & 1) {
139 x = PPPDBG_LOG + PPPDBG_DRIVER;
140 strioctl(pppfd, PPPIO_DEBUG, &x, sizeof(int), 0);
143 /* Assign a new PPA and get its unit number. */
144 if (strioctl(pppfd, PPPIO_NEWPPA, &ifunit, 0, sizeof(int)) < 0) {
145 syslog(LOG_ERR, "Can't create new PPP interface: %m");
150 * Open the ppp device again and link it under the ip multiplexor.
151 * IP will assign a unit number which hopefully is the same as ifunit.
152 * I don't know any way to be certain they will be the same. :-(
154 ifd = open("/dev/ppp", O_RDWR, 0);
156 syslog(LOG_ERR, "Can't open /dev/ppp (2): %m");
159 if (kdebugflag & 1) {
160 x = PPPDBG_LOG + PPPDBG_DRIVER;
161 strioctl(ifd, PPPIO_DEBUG, &x, sizeof(int), 0);
164 if (ioctl(ifd, I_PUSH, "ip") < 0) {
165 syslog(LOG_ERR, "Can't push IP module: %m");
170 if (dlpi_attach(ifd, ifunit) < 0 ||
171 dlpi_get_reply(ifd, &reply.prim, DL_OK_ACK, sizeof(reply)) < 0) {
172 syslog(LOG_ERR, "Can't attach to ppp%d: %m", ifunit);
177 ipmuxid = ioctl(ipfd, I_LINK, ifd);
180 syslog(LOG_ERR, "Can't link PPP device to IP: %m");
185 /* Set the interface name for the link. */
186 (void) sprintf (ifr.ifr_name, "ppp%d", ifunit);
187 ifr.ifr_metric = ipmuxid;
188 if (strioctl(ipfd, SIOCSIFNAME, (char *)&ifr, sizeof ifr, 0) < 0) {
189 syslog(LOG_ERR, "Can't set interface name %s: %m", ifr.ifr_name);
196 * sys_cleanup - restore any system state we modified before exiting:
197 * mark the interface down, delete default route and/or proxy arp entry.
198 * This should call die() because it's called from die().
207 if (default_route_gateway)
208 cifdefaultroute(0, default_route_gateway, default_route_gateway);
210 cifproxyarp(0, proxy_arp_addr);
214 * sys_close - Clean up in a child process before execing.
225 * sys_check_options - check the options that the user specified
235 * daemon - Detach us from controlling terminal session.
238 daemon(nochdir, noclose)
239 int nochdir, noclose;
243 if ((pid = fork()) < 0)
246 exit(0); /* parent dies */
251 fclose(stdin); /* don't need stdin, stdout, stderr */
259 * ppp_available - check whether the system has any ppp interfaces
266 return stat("/dev/ppp", &buf) >= 0;
270 * establish_ppp - Turn the serial port into a ppp interface.
278 /* Pop any existing modules off the tty stream. */
280 if (ioctl(fd, I_LOOK, tty_modules[i]) < 0
281 || ioctl(fd, I_POP, 0) < 0)
285 /* Push the async hdlc module and the compressor module. */
286 if (ioctl(fd, I_PUSH, "ppp_ahdl") < 0) {
287 syslog(LOG_ERR, "Couldn't push PPP Async HDLC module: %m");
290 if (kdebugflag & 4) {
291 i = PPPDBG_LOG + PPPDBG_AHDLC;
292 strioctl(pppfd, PPPIO_DEBUG, &i, sizeof(int), 0);
294 if (ioctl(fd, I_PUSH, "ppp_comp") < 0) {
295 syslog(LOG_ERR, "Couldn't push PPP compression module: %m");
298 if (kdebugflag & 2) {
299 i = PPPDBG_LOG + PPPDBG_COMP;
300 strioctl(pppfd, PPPIO_DEBUG, &i, sizeof(int), 0);
303 /* Link the serial port under the PPP multiplexor. */
304 if ((fdmuxid = ioctl(pppfd, I_LINK, fd)) < 0) {
305 syslog(LOG_ERR, "Can't link tty to PPP mux: %m");
311 * restore_loop - reattach the ppp unit to the loopback.
312 * This doesn't need to do anything because disestablish_ppp does it.
320 * disestablish_ppp - Restore the serial port to normal operation.
321 * It attempts to reconstruct the stream with the previously popped
322 * modules. This shouldn't call die() because it's called from die().
331 if (ioctl(pppfd, I_UNLINK, fdmuxid) < 0) {
333 syslog(LOG_ERR, "Can't unlink tty from PPP mux: %m");
338 while (ioctl(fd, I_POP, 0) >= 0)
340 for (i = tty_nmodules - 1; i >= 0; --i)
341 if (ioctl(fd, I_PUSH, tty_modules[i]) < 0)
342 syslog(LOG_ERR, "Couldn't restore tty module %s: %m",
345 if (hungup && default_device && tty_sid > 0) {
347 * If we have received a hangup, we need to send a SIGHUP
348 * to the terminal's controlling process. The reason is
349 * that the original stream head for the terminal hasn't
350 * seen the M_HANGUP message (it went up through the ppp
351 * driver to the stream head for our fd to /dev/ppp).
353 kill(tty_sid, SIGHUP);
359 * Check whether the link seems not to be 8-bit clean.
367 if (strioctl(pppfd, PPPIO_GCLEAN, &x, 0, sizeof(x)) < 0)
372 s = "bit 7 set to 1";
375 s = "bit 7 set to 0";
385 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
386 syslog(LOG_WARNING, "All received characters had %s", s);
391 * List of valid speeds.
394 int speed_int, speed_val;
466 * Translate from bits/second to a speed_t.
472 struct speed *speedp;
476 for (speedp = speeds; speedp->speed_int; speedp++)
477 if (bps == speedp->speed_int)
478 return speedp->speed_val;
479 syslog(LOG_WARNING, "speed %d not supported", bps);
484 * Translate from a speed_t to bits/second.
490 struct speed *speedp;
494 for (speedp = speeds; speedp->speed_int; speedp++)
495 if (speed == speedp->speed_val)
496 return speedp->speed_int;
501 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
502 * at the requested speed, etc. If `local' is true, set CLOCAL
503 * regardless of whether the modem option was specified.
506 set_up_tty(fd, local)
511 #if !defined (CRTSCTS)
515 if (tcgetattr(fd, &tios) < 0) {
516 syslog(LOG_ERR, "tcgetattr: %m");
522 if (ioctl (fd, TCGETX, &tiox) < 0) {
525 syslog (LOG_ERR, "TCGETX: %m");
534 ioctl(fd, TIOCGWINSZ, &wsinfo);
537 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
540 tios.c_cflag |= CRTSCTS;
541 else if (crtscts < 0)
542 tios.c_cflag &= ~CRTSCTS;
544 if (crtscts != 0 && !termiox_ok) {
545 syslog(LOG_ERR, "Can't set RTS/CTS flow control");
546 } else if (crtscts > 0) {
547 tiox.x_hflag |= RTSXOFF|CTSXON;
548 } else if (crtscts < 0) {
549 tiox.x_hflag &= ~(RTSXOFF|CTSXON);
553 tios.c_cflag |= CS8 | CREAD | HUPCL;
555 tios.c_cflag |= CLOCAL;
556 tios.c_iflag = IGNBRK | IGNPAR;
560 tios.c_cc[VTIME] = 0;
563 tios.c_iflag |= IXON | IXOFF;
564 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
565 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
568 speed = translate_speed(inspeed);
570 cfsetospeed(&tios, speed);
571 cfsetispeed(&tios, speed);
573 speed = cfgetospeed(&tios);
575 * We can't proceed if the serial port speed is 0,
576 * since that implies that the serial port is disabled.
579 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
585 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
586 syslog(LOG_ERR, "tcsetattr: %m");
591 if (termiox_ok && ioctl (fd, TCSETXF, &tiox) < 0){
592 syslog (LOG_ERR, "TCSETXF: %m");
596 baud_rate = inspeed = baud_rate_of(speed);
601 * restore_tty - restore the terminal to the saved settings.
608 if (!default_device) {
610 * Turn off echoing, because otherwise we can get into
611 * a loop with the tty and the modem echoing to each other.
612 * We presume we are the sole user of this tty device, so
613 * when we close it, it will revert to its defaults anyway.
615 inittermios.c_lflag &= ~(ECHO | ECHONL);
617 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
618 if (!hungup && errno != ENXIO)
619 syslog(LOG_WARNING, "tcsetattr: %m");
621 if (ioctl (fd, TCSETXF, &inittermiox) < 0){
622 if (!hungup && errno != ENXIO)
623 syslog (LOG_ERR, "TCSETXF: %m");
626 ioctl(fd, TIOCSWINSZ, &wsinfo);
632 * setdtr - control the DTR line on the serial port.
633 * This is called from die(), so it shouldn't call die().
639 int modembits = TIOCM_DTR;
641 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
645 * open_loopback - open the device we use for getting packets
646 * in demand mode. Under Solaris 2, we use our existing fd
655 * output - Output PPP packet.
668 log_packet(p, len, "sent ", LOG_DEBUG);
671 data.buf = (caddr_t) p;
673 while (putmsg(pppfd, NULL, &data, 0) < 0) {
674 if (--retries < 0 || (errno != EWOULDBLOCK && errno != EAGAIN)) {
676 syslog(LOG_ERR, "Couldn't send packet: %m");
680 pfd.events = POLLOUT;
681 poll(&pfd, 1, 250); /* wait for up to 0.25 seconds */
687 * wait_input - wait until there is data available on fd,
688 * for the length of time specified by *timo (indefinite
693 struct timeval *timo;
698 t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
700 pfd.events = POLLIN | POLLPRI | POLLHUP;
701 if (poll(&pfd, 1, t) < 0 && errno != EINTR) {
702 syslog(LOG_ERR, "poll: %m");
708 * wait_loop_output - wait until there is data available on the
709 * loopback, for the length of time specified by *timo (indefinite
713 wait_loop_output(timo)
714 struct timeval *timo;
720 * wait_time - wait for a given length of time or until a
721 * signal is received.
725 struct timeval *timo;
729 n = select(0, NULL, NULL, NULL, timo);
730 if (n < 0 && errno != EINTR) {
731 syslog(LOG_ERR, "select: %m");
738 * read_packet - get a PPP packet from the serial device.
744 struct strbuf ctrl, data;
746 unsigned char ctrlbuf[sizeof(union DL_primitives) + 64];
749 data.maxlen = PPP_MRU + PPP_HDRLEN;
750 data.buf = (caddr_t) buf;
751 ctrl.maxlen = sizeof(ctrlbuf);
752 ctrl.buf = (caddr_t) ctrlbuf;
754 len = getmsg(pppfd, &ctrl, &data, &flags);
756 if (errno == EAGAIN || errno == EINTR)
758 syslog(LOG_ERR, "Error reading packet: %m");
766 * Got a M_PROTO or M_PCPROTO message. Interpret it
767 * as a DLPI primitive??
770 syslog(LOG_DEBUG, "got dlpi prim 0x%x, len=%d",
771 ((union DL_primitives *)ctrlbuf)->dl_primitive, ctrl.len);
777 * get_loop_output - get outgoing packets from the ppp device,
778 * and detect when we want to bring the real link up.
779 * Return value is 1 if we need to bring up the link, 0 otherwise.
787 while ((len = read_packet(inpacket_buf)) > 0) {
788 if (loop_frame(inpacket_buf, len))
795 * ppp_send_config - configure the transmit characteristics of
799 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
808 if (strioctl(pppfd, PPPIO_MTU, &mtu, sizeof(mtu), 0) < 0) {
809 if (hungup && errno == ENXIO)
811 syslog(LOG_ERR, "Couldn't set MTU: %m");
814 /* can't set these if we don't have a stream attached below /dev/ppp */
815 if (strioctl(pppfd, PPPIO_XACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
816 syslog(LOG_ERR, "Couldn't set transmit ACCM: %m");
818 cf[0] = (pcomp? COMP_PROT: 0) + (accomp? COMP_AC: 0);
819 cf[1] = COMP_PROT | COMP_AC;
820 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
821 syslog(LOG_ERR, "Couldn't set prot/AC compression: %m");
825 /* set the MTU for IP as well */
826 memset(&ifr, 0, sizeof(ifr));
827 strlcpy(ifr.ifr_name, sizeof(ifr.ifr_name), ifname);
828 ifr.ifr_metric = link_mtu;
829 if (ioctl(ipfd, SIOCSIFMTU, &ifr) < 0) {
830 syslog(LOG_ERR, "Couldn't set IP MTU: %m");
835 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
838 ppp_set_xaccm(unit, accm)
843 && strioctl(pppfd, PPPIO_XACCM, accm, sizeof(ext_accm), 0) < 0) {
844 if (!hungup || errno != ENXIO)
845 syslog(LOG_WARNING, "Couldn't set extended ACCM: %m");
850 * ppp_recv_config - configure the receive-side characteristics of
854 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
862 if (strioctl(pppfd, PPPIO_MRU, &mru, sizeof(mru), 0) < 0) {
863 if (hungup && errno == ENXIO)
865 syslog(LOG_ERR, "Couldn't set MRU: %m");
868 /* can't set these if we don't have a stream attached below /dev/ppp */
869 if (strioctl(pppfd, PPPIO_RACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
870 syslog(LOG_ERR, "Couldn't set receive ACCM: %m");
872 cf[0] = (pcomp? DECOMP_PROT: 0) + (accomp? DECOMP_AC: 0);
873 cf[1] = DECOMP_PROT | DECOMP_AC;
874 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
875 syslog(LOG_ERR, "Couldn't set prot/AC decompression: %m");
881 * ccp_test - ask kernel whether a given compression method
882 * is acceptable for use.
885 ccp_test(unit, opt_ptr, opt_len, for_transmit)
886 int unit, opt_len, for_transmit;
889 if (strioctl(pppfd, (for_transmit? PPPIO_XCOMP: PPPIO_RCOMP),
890 opt_ptr, opt_len, 0) >= 0)
892 return (errno == ENOSR)? 0: -1;
896 * ccp_flags_set - inform kernel about the current state of CCP.
899 ccp_flags_set(unit, isopen, isup)
900 int unit, isopen, isup;
904 cf[0] = (isopen? CCP_ISOPEN: 0) + (isup? CCP_ISUP: 0);
905 cf[1] = CCP_ISOPEN | CCP_ISUP | CCP_ERROR | CCP_FATALERROR;
906 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
907 if (!hungup || errno != ENXIO)
908 syslog(LOG_ERR, "Couldn't set kernel CCP state: %m");
913 * get_idle_time - return how long the link has been idle.
920 return strioctl(pppfd, PPPIO_GIDLE, ip, 0, sizeof(struct ppp_idle)) >= 0;
925 * set_filters - transfer the pass and active filters to the kernel.
928 set_filters(pass, active)
929 struct bpf_program *pass, *active;
933 if (pass->bf_len > 0) {
934 if (strioctl(pppfd, PPPIO_PASSFILT, pass,
935 sizeof(struct bpf_program), 0) < 0) {
936 syslog(LOG_ERR, "Couldn't set pass-filter in kernel: %m");
940 if (active->bf_len > 0) {
941 if (strioctl(pppfd, PPPIO_ACTIVEFILT, active,
942 sizeof(struct bpf_program), 0) < 0) {
943 syslog(LOG_ERR, "Couldn't set active-filter in kernel: %m");
952 * ccp_fatal_error - returns 1 if decompression was disabled as a
953 * result of an error detected after decompression of a packet,
954 * 0 otherwise. This is necessary because of patent nonsense.
957 ccp_fatal_error(unit)
963 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
964 if (errno != ENXIO && errno != EINVAL)
965 syslog(LOG_ERR, "Couldn't get compression flags: %m");
968 return cf[0] & CCP_FATALERROR;
972 * sifvjcomp - config tcp header compression
975 sifvjcomp(u, vjcomp, xcidcomp, xmaxcid)
976 int u, vjcomp, xcidcomp, xmaxcid;
982 maxcid[0] = xcidcomp;
983 maxcid[1] = 15; /* XXX should be rmaxcid */
984 if (strioctl(pppfd, PPPIO_VJINIT, maxcid, sizeof(maxcid), 0) < 0) {
985 syslog(LOG_ERR, "Couldn't initialize VJ compression: %m");
989 cf[0] = (vjcomp? COMP_VJC + DECOMP_VJC: 0) /* XXX this is wrong */
990 + (xcidcomp? COMP_VJCCID + DECOMP_VJCCID: 0);
991 cf[1] = COMP_VJC + DECOMP_VJC + COMP_VJCCID + DECOMP_VJCCID;
992 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
994 syslog(LOG_ERR, "Couldn't enable VJ compression: %m");
1001 * sifup - Config the interface up and enable IP packets to pass.
1009 strlcpy(ifr.ifr_name, sizeof(ifr.ifr_name), ifname);
1010 if (ioctl(ipfd, SIOCGIFFLAGS, &ifr) < 0) {
1011 syslog(LOG_ERR, "Couldn't mark interface up (get): %m");
1014 ifr.ifr_flags |= IFF_UP;
1015 if (ioctl(ipfd, SIOCSIFFLAGS, &ifr) < 0) {
1016 syslog(LOG_ERR, "Couldn't mark interface up (set): %m");
1024 * sifdown - Config the interface down and disable IP.
1034 strlcpy(ifr.ifr_name, sizeof(ifr.ifr_name), ifname);
1035 if (ioctl(ipfd, SIOCGIFFLAGS, &ifr) < 0) {
1036 syslog(LOG_ERR, "Couldn't mark interface down (get): %m");
1039 ifr.ifr_flags &= ~IFF_UP;
1040 if (ioctl(ipfd, SIOCSIFFLAGS, &ifr) < 0) {
1041 syslog(LOG_ERR, "Couldn't mark interface down (set): %m");
1049 * sifnpmode - Set the mode for handling packets for a given NP.
1052 sifnpmode(u, proto, mode)
1060 npi[1] = (int) mode;
1061 if (strioctl(pppfd, PPPIO_NPMODE, &npi, 2 * sizeof(int), 0) < 0) {
1062 syslog(LOG_ERR, "ioctl(set NP %d mode to %d): %m", proto, mode);
1068 #define INET_ADDR(x) (((struct sockaddr_in *) &(x))->sin_addr.s_addr)
1071 * sifaddr - Config the interface IP addresses and netmask.
1081 memset(&ifr, 0, sizeof(ifr));
1082 strlcpy(ifr.ifr_name, sizeof(ifr.ifr_name), ifname);
1083 ifr.ifr_addr.sa_family = AF_INET;
1084 INET_ADDR(ifr.ifr_addr) = m;
1085 if (ioctl(ipfd, SIOCSIFNETMASK, &ifr) < 0) {
1086 syslog(LOG_ERR, "Couldn't set IP netmask: %m");
1089 ifr.ifr_addr.sa_family = AF_INET;
1090 INET_ADDR(ifr.ifr_addr) = o;
1091 if (ioctl(ipfd, SIOCSIFADDR, &ifr) < 0) {
1092 syslog(LOG_ERR, "Couldn't set local IP address: %m");
1097 * On some systems, we have to explicitly set the point-to-point
1098 * flag bit before we can set a destination address.
1100 if (ioctl(ipfd, SIOCGIFFLAGS, &ifr) >= 0
1101 && (ifr.ifr_flags & IFF_POINTOPOINT) == 0) {
1102 ifr.ifr_flags |= IFF_POINTOPOINT;
1103 if (ioctl(ipfd, SIOCSIFFLAGS, &ifr) < 0) {
1104 syslog(LOG_ERR, "Couldn't mark interface pt-to-pt: %m");
1108 ifr.ifr_dstaddr.sa_family = AF_INET;
1109 INET_ADDR(ifr.ifr_dstaddr) = h;
1110 if (ioctl(ipfd, SIOCSIFDSTADDR, &ifr) < 0) {
1111 syslog(LOG_ERR, "Couldn't set remote IP address: %m");
1114 #if 0 /* now done in ppp_send_config */
1115 ifr.ifr_metric = link_mtu;
1116 if (ioctl(ipfd, SIOCSIFMTU, &ifr) < 0) {
1117 syslog(LOG_ERR, "Couldn't set IP MTU: %m");
1126 * cifaddr - Clear the interface IP addresses, and delete routes
1127 * through the interface if possible.
1134 #if defined(__USLC__) /* was: #if 0 */
1135 cifroute(unit, ouraddr, hisaddr);
1137 syslog(LOG_NOTICE, "Removing ppp interface unit");
1138 if (ioctl(ipfd, I_UNLINK, ipmuxid) < 0) {
1139 syslog(LOG_ERR, "Can't remove ppp interface unit: %m");
1150 * sifdefaultroute - assign a default route through the address given.
1153 sifdefaultroute(u, l, g)
1159 #if defined(__USLC__)
1160 g = l; /* use the local address as gateway */
1162 memset(&rt, 0, sizeof(rt));
1163 rt.rt_dst.sa_family = AF_INET;
1164 INET_ADDR(rt.rt_dst) = 0;
1165 rt.rt_gateway.sa_family = AF_INET;
1166 INET_ADDR(rt.rt_gateway) = g;
1167 rt.rt_flags = RTF_GATEWAY;
1169 if (ioctl(ipfd, SIOCADDRT, &rt) < 0) {
1170 syslog(LOG_ERR, "Can't add default route: %m");
1174 default_route_gateway = g;
1179 * cifdefaultroute - delete a default route through the address given.
1182 cifdefaultroute(u, l, g)
1188 #if defined(__USLC__)
1189 g = l; /* use the local address as gateway */
1191 memset(&rt, 0, sizeof(rt));
1192 rt.rt_dst.sa_family = AF_INET;
1193 INET_ADDR(rt.rt_dst) = 0;
1194 rt.rt_gateway.sa_family = AF_INET;
1195 INET_ADDR(rt.rt_gateway) = g;
1196 rt.rt_flags = RTF_GATEWAY;
1198 if (ioctl(ipfd, SIOCDELRT, &rt) < 0) {
1199 syslog(LOG_ERR, "Can't delete default route: %m");
1203 default_route_gateway = 0;
1208 * sifproxyarp - Make a proxy ARP entry for the peer.
1211 sifproxyarp(unit, hisaddr)
1215 struct arpreq arpreq;
1217 memset(&arpreq, 0, sizeof(arpreq));
1218 if (!get_ether_addr(hisaddr, &arpreq.arp_ha))
1221 arpreq.arp_pa.sa_family = AF_INET;
1222 INET_ADDR(arpreq.arp_pa) = hisaddr;
1223 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1224 if (ioctl(ipfd, SIOCSARP, (caddr_t) &arpreq) < 0) {
1225 syslog(LOG_ERR, "Couldn't set proxy ARP entry: %m");
1229 proxy_arp_addr = hisaddr;
1234 * cifproxyarp - Delete the proxy ARP entry for the peer.
1237 cifproxyarp(unit, hisaddr)
1241 struct arpreq arpreq;
1243 memset(&arpreq, 0, sizeof(arpreq));
1244 arpreq.arp_pa.sa_family = AF_INET;
1245 INET_ADDR(arpreq.arp_pa) = hisaddr;
1246 if (ioctl(ipfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1247 syslog(LOG_ERR, "Couldn't delete proxy ARP entry: %m");
1256 * get_ether_addr - get the hardware address of an interface on the
1257 * the same subnet as ipaddr.
1262 get_ether_addr(ipaddr, hwaddr)
1264 struct sockaddr *hwaddr;
1266 struct ifreq *ifr, *ifend, ifreq;
1269 u_int32_t ina, mask;
1272 * Scan through the system's network interfaces.
1275 if (ioctl(ipfd, SIOCGIFNUM, &nif) < 0)
1278 ifc.ifc_len = nif * sizeof(struct ifreq);
1279 ifc.ifc_buf = (caddr_t) malloc(ifc.ifc_len);
1280 if (ifc.ifc_buf == 0)
1282 if (ioctl(ipfd, SIOCGIFCONF, &ifc) < 0) {
1283 syslog(LOG_WARNING, "Couldn't get system interface list: %m");
1287 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1288 for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
1289 if (ifr->ifr_addr.sa_family != AF_INET)
1292 * Check that the interface is up, and not point-to-point or loopback.
1294 strlcpy(ifreq.ifr_name, sizeof(ifreq.ifr_name), ifr->ifr_name);
1295 if (ioctl(ipfd, SIOCGIFFLAGS, &ifreq) < 0)
1297 if ((ifreq.ifr_flags &
1298 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1299 != (IFF_UP|IFF_BROADCAST))
1302 * Get its netmask and check that it's on the right subnet.
1304 if (ioctl(ipfd, SIOCGIFNETMASK, &ifreq) < 0)
1306 ina = INET_ADDR(ifr->ifr_addr);
1307 mask = INET_ADDR(ifreq.ifr_addr);
1308 if ((ipaddr & mask) == (ina & mask))
1313 syslog(LOG_WARNING, "No suitable interface found for proxy ARP");
1318 syslog(LOG_INFO, "found interface %s for proxy ARP", ifr->ifr_name);
1319 if (!get_hw_addr(ifr->ifr_name, ina, hwaddr)) {
1320 syslog(LOG_ERR, "Couldn't get hardware address for %s", ifr->ifr_name);
1330 * get_hw_addr - obtain the hardware address for a named interface.
1333 get_hw_addr(name, ina, hwaddr)
1336 struct sockaddr *hwaddr;
1339 /* New way - get the address by doing an arp request. */
1343 s = socket(AF_INET, SOCK_DGRAM, 0);
1346 memset(&req, 0, sizeof(req));
1347 req.arp_pa.sa_family = AF_INET;
1348 INET_ADDR(req.arp_pa) = ina;
1349 if (ioctl(s, SIOCGARP, &req) < 0) {
1350 syslog(LOG_ERR, "Couldn't get ARP entry for %s: %m", ip_ntoa(ina));
1353 *hwaddr = req.arp_ha;
1354 hwaddr->sa_family = AF_UNSPEC;
1358 int unit, iffd, adrlen;
1359 unsigned char *adrp;
1362 union DL_primitives prim;
1367 * We have to open the device and ask it for its hardware address.
1368 * First split apart the device name and unit.
1370 slprintf(ifdev, sizeof(ifdev), "/dev/%s", name);
1371 for (q = ifdev + strlen(ifdev); --q >= ifdev; )
1378 * Open the device and do a DLPI attach and phys_addr_req.
1380 iffd = open(ifdev, O_RDWR);
1382 syslog(LOG_ERR, "Can't open %s: %m", ifdev);
1385 if (dlpi_attach(iffd, unit) < 0
1386 || dlpi_get_reply(iffd, &reply.prim, DL_OK_ACK, sizeof(reply)) < 0
1387 || dlpi_info_req(iffd) < 0
1388 || dlpi_get_reply(iffd, &reply.prim, DL_INFO_ACK, sizeof(reply)) < 0) {
1393 adrlen = reply.prim.info_ack.dl_addr_length;
1394 adrp = (unsigned char *)&reply + reply.prim.info_ack.dl_addr_offset;
1395 #if DL_CURRENT_VERSION >= 2
1396 if (reply.prim.info_ack.dl_sap_length < 0)
1397 adrlen += reply.prim.info_ack.dl_sap_length;
1399 adrp += reply.prim.info_ack.dl_sap_length;
1401 hwaddr->sa_family = AF_UNSPEC;
1402 memcpy(hwaddr->sa_data, adrp, adrlen);
1409 dlpi_attach(fd, ppa)
1412 dl_attach_req_t req;
1415 req.dl_primitive = DL_ATTACH_REQ;
1417 buf.len = sizeof(req);
1418 buf.buf = (void *) &req;
1419 return putmsg(fd, &buf, NULL, RS_HIPRI);
1429 req.dl_primitive = DL_INFO_REQ;
1430 buf.len = sizeof(req);
1431 buf.buf = (void *) &req;
1432 return putmsg(fd, &buf, NULL, RS_HIPRI);
1436 dlpi_get_reply(fd, reply, expected_prim, maxlen)
1437 union DL_primitives *reply;
1438 int fd, expected_prim, maxlen;
1445 * Use poll to wait for a message with a timeout.
1448 pfd.events = POLLIN | POLLPRI;
1450 n = poll(&pfd, 1, 1000);
1451 } while (n == -1 && errno == EINTR);
1458 buf.maxlen = maxlen;
1459 buf.buf = (void *) reply;
1461 if (getmsg(fd, &buf, NULL, &flags) < 0)
1464 if (buf.len < sizeof(ulong)) {
1466 syslog(LOG_DEBUG, "dlpi response short (len=%d)\n", buf.len);
1470 if (reply->dl_primitive == expected_prim)
1474 if (reply->dl_primitive == DL_ERROR_ACK) {
1475 syslog(LOG_DEBUG, "dlpi error %d (unix errno %d) for prim %x\n",
1476 reply->error_ack.dl_errno, reply->error_ack.dl_unix_errno,
1477 reply->error_ack.dl_error_primitive);
1479 syslog(LOG_DEBUG, "dlpi unexpected response prim %x\n",
1480 reply->dl_primitive);
1488 * Return user specified netmask, modified by any mask we might determine
1489 * for address `addr' (in network byte order).
1490 * Here we scan through the system's list of interfaces, looking for
1491 * any non-point-to-point interfaces which might appear to be on the same
1492 * network as `addr'. If we find any, we OR in their netmask to the
1493 * user-specified netmask.
1499 u_int32_t mask, nmask, ina;
1500 struct ifreq *ifr, *ifend, ifreq;
1505 if (IN_CLASSA(addr)) /* determine network mask for address class */
1506 nmask = IN_CLASSA_NET;
1507 else if (IN_CLASSB(addr))
1508 nmask = IN_CLASSB_NET;
1510 nmask = IN_CLASSC_NET;
1511 /* class D nets are disallowed by bad_ip_adrs */
1512 mask = netmask | htonl(nmask);
1515 * Scan through the system's network interfaces.
1518 if (ioctl(ipfd, SIOCGIFNUM, &nif) < 0)
1521 ifc.ifc_len = nif * sizeof(struct ifreq);
1522 ifc.ifc_buf = (caddr_t) malloc(ifc.ifc_len);
1523 if (ifc.ifc_buf == 0)
1525 if (ioctl(ipfd, SIOCGIFCONF, &ifc) < 0) {
1526 syslog(LOG_WARNING, "Couldn't get system interface list: %m");
1530 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1531 for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
1533 * Check the interface's internet address.
1535 if (ifr->ifr_addr.sa_family != AF_INET)
1537 ina = INET_ADDR(ifr->ifr_addr);
1538 if ((ntohl(ina) & nmask) != (addr & nmask))
1541 * Check that the interface is up, and not point-to-point or loopback.
1543 strlcpy(ifreq.ifr_name, sizeof(ifreq.ifr_name), ifr->ifr_name);
1544 if (ioctl(ipfd, SIOCGIFFLAGS, &ifreq) < 0)
1546 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1550 * Get its netmask and OR it into our mask.
1552 if (ioctl(ipfd, SIOCGIFNETMASK, &ifreq) < 0)
1554 mask |= INET_ADDR(ifreq.ifr_addr);
1562 * logwtmp - write an accounting record to the /var/adm/wtmp file.
1565 logwtmp(line, name, host)
1566 const char *line, *name, *host;
1568 static struct utmpx utmpx;
1572 strlcpy(utmpx.ut_user, sizeof(utmpx.ut_user), name);
1573 strlcpy(utmpx.ut_id, sizeof(utmpx.ut_id), ifname);
1574 strlcpy(utmpx.ut_line, sizeof(utmpx.ut_line), line);
1575 utmpx.ut_pid = getpid();
1576 utmpx.ut_type = USER_PROCESS;
1578 utmpx.ut_type = DEAD_PROCESS;
1580 gettimeofday(&utmpx.ut_tv, NULL);
1581 updwtmpx("/var/adm/wtmpx", &utmpx);
1585 * get_host_seed - return the serial number of this machine.
1592 if (sysinfo(SI_HW_SERIAL, buf, sizeof(buf)) < 0) {
1593 syslog(LOG_ERR, "sysinfo: %m");
1596 return (int) strtoul(buf, NULL, 16);
1600 strioctl(fd, cmd, ptr, ilen, olen)
1601 int fd, cmd, ilen, olen;
1604 struct strioctl str;
1610 if (ioctl(fd, I_STR, &str) == -1)
1612 if (str.ic_len != olen)
1613 syslog(LOG_DEBUG, "strioctl: expected %d bytes, got %d for cmd %x\n",
1614 olen, str.ic_len, cmd);
1619 * lock - create a lock file for the named lock device
1622 #define LOCK_PREFIX "/var/spool/locks/LK."
1623 static char lock_file[40]; /* name of lock file created */
1633 if (stat(dev, &sbuf) < 0) {
1634 syslog(LOG_ERR, "Can't get device number for %s: %m", dev);
1637 if ((sbuf.st_mode & S_IFMT) != S_IFCHR) {
1638 syslog(LOG_ERR, "Can't lock %s: not a character device", dev);
1641 sprintf(lock_file, "%s%03d.%03d.%03d", LOCK_PREFIX, major(sbuf.st_dev),
1642 major(sbuf.st_rdev), minor(sbuf.st_rdev));
1644 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1646 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1647 /* Read the lock file to find out who has the device locked */
1648 n = read(fd, ascii_pid, 11);
1650 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1654 pid = atoi(ascii_pid);
1655 if (pid > 0 && kill(pid, 0) == -1 && errno == ESRCH) {
1656 /* pid no longer exists - remove the lock file */
1657 if (unlink(lock_file) == 0) {
1659 syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1663 syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
1666 syslog(LOG_NOTICE, "Device %s is locked by pid %d",
1671 syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1676 sprintf(ascii_pid, "%10d\n", getpid());
1677 write(fd, ascii_pid, 11);
1684 * unlock - remove our lockfile
1697 * cifroute - delete a route through the addresses given.
1700 cifroute(u, our, his)
1706 memset(&rt, 0, sizeof(rt));
1707 rt.rt_dst.sa_family = AF_INET;
1708 INET_ADDR(rt.rt_dst) = his;
1709 rt.rt_gateway.sa_family = AF_INET;
1710 INET_ADDR(rt.rt_gateway) = our;
1711 rt.rt_flags = RTF_HOST;
1713 if (ioctl(ipfd, SIOCDELRT, &rt) < 0) {
1714 syslog(LOG_ERR, "Can't delete route: %m");
1722 * have_route_to - determine if the system has a route to the specified
1723 * IP address. Returns 0 if not, 1 if so, -1 if we can't tell.
1724 * `addr' is in network byte order.
1725 * For demand mode to work properly, we have to ignore routes
1726 * through our own interface.
1728 #ifndef T_CURRENT /* needed for Solaris 2.5 */
1729 #define T_CURRENT MI_T_CURRENT
1736 int fd, r, flags, i;
1738 struct T_optmgmt_req req;
1742 struct T_optmgmt_ack ack;
1743 unsigned char space[64];
1746 struct strbuf cbuf, dbuf;
1748 mib2_ipRouteEntry_t routes[8];
1749 mib2_ipRouteEntry_t *rp;
1751 fd = open("/dev/ip", O_RDWR);
1753 syslog(LOG_WARNING, "have_route_to: couldn't open /dev/ip: %m");
1757 req.req.PRIM_type = T_OPTMGMT_REQ;
1758 req.req.OPT_offset = (char *) &req.hdr - (char *) &req;
1759 req.req.OPT_length = sizeof(req.hdr);
1760 req.req.MGMT_flags = T_CURRENT;
1762 req.hdr.level = MIB2_IP;
1766 cbuf.buf = (char *) &req;
1767 cbuf.len = sizeof(req);
1769 if (putmsg(fd, &cbuf, NULL, 0) == -1) {
1770 syslog(LOG_WARNING, "have_route_to: putmsg: %m");
1776 cbuf.buf = (char *) &ack;
1777 cbuf.maxlen = sizeof(ack);
1778 dbuf.buf = (char *) routes;
1779 dbuf.maxlen = sizeof(routes);
1781 r = getmsg(fd, &cbuf, &dbuf, &flags);
1783 syslog(LOG_WARNING, "have_route_to: getmsg: %m");
1788 if (cbuf.len < sizeof(struct T_optmgmt_ack)
1789 || ack.ack.PRIM_type != T_OPTMGMT_ACK
1790 || ack.ack.MGMT_flags != T_SUCCESS
1791 || ack.ack.OPT_length < sizeof(struct opthdr)) {
1792 syslog(LOG_DEBUG, "have_route_to: bad message len=%d prim=%d",
1793 cbuf.len, ack.ack.PRIM_type);
1798 rh = (struct opthdr *) ((char *)&ack + ack.ack.OPT_offset);
1799 if (rh->level == 0 && rh->name == 0)
1801 if (rh->level != MIB2_IP || rh->name != MIB2_IP_21) {
1802 while (r == MOREDATA)
1803 r = getmsg(fd, NULL, &dbuf, &flags);
1808 nroutes = dbuf.len / sizeof(mib2_ipRouteEntry_t);
1809 for (rp = routes, i = 0; i < nroutes; ++i, ++rp) {
1810 if (rp->ipRouteMask != ~0) {
1811 syslog(LOG_DEBUG, "have_route_to: dest=%x gw=%x mask=%x\n",
1812 rp->ipRouteDest, rp->ipRouteNextHop,
1814 if (((addr ^ rp->ipRouteDest) & rp->ipRouteMask) == 0
1815 && rp->ipRouteNextHop != remote_addr)
1821 r = getmsg(fd, NULL, &dbuf, &flags);