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.34 1999/09/08 01:11:16 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>
70 static const char rcsid[] = RCSID;
73 static int fdmuxid = -1;
75 static int ipmuxid = -1;
77 static int restore_term;
78 static struct termios inittermios;
80 static struct termiox inittermiox;
81 static int termiox_ok;
83 static struct winsize wsinfo; /* Initial window size info */
84 static pid_t tty_sid; /* original session ID for terminal */
86 extern u_char inpacket_buf[]; /* borrowed from main.c */
88 #define MAX_POLLFDS 32
89 static struct pollfd pollfds[MAX_POLLFDS];
92 static int link_mtu, link_mru;
95 static int tty_nmodules;
96 static char tty_modules[NMODULES][FMNAMESZ+1];
97 static int tty_npushed;
99 static int if_is_up; /* Interface has been marked up */
100 static u_int32_t remote_addr; /* IP address of peer */
101 static u_int32_t default_route_gateway; /* Gateway for default route added */
102 static u_int32_t proxy_arp_addr; /* Addr for proxy arp entry added */
104 /* Prototypes for procedures local to this file. */
105 static int translate_speed __P((int));
106 static int baud_rate_of __P((int));
107 static int get_ether_addr __P((u_int32_t, struct sockaddr *));
108 static int get_hw_addr __P((char *, u_int32_t, struct sockaddr *));
109 static int dlpi_attach __P((int, int));
110 static int dlpi_info_req __P((int));
111 static int dlpi_get_reply __P((int, union DL_primitives *, int, int));
112 static int strioctl __P((int, int, void *, int, int));
116 * sifppa - Sets interface ppa
118 * without setting the ppa, ip module will return EINVAL upon setting the
119 * interface UP (SIOCSxIFFLAGS). This is because ip module in 2.8 expects
120 * two DLPI_INFO_REQ to be sent down to the driver (below ip) before
121 * IFF_UP can be set. Plumbing the device causes one DLPI_INFO_REQ to
122 * be sent down, and the second DLPI_INFO_REQ is sent upon receiving
123 * IF_UNITSEL (old) or SIOCSLIFNAME (new) ioctls. Such setting of the ppa
124 * is required because the ppp DLPI provider advertises itself as
125 * a DLPI style 2 type, which requires a point of attachment to be
126 * specified. The only way the user can specify a point of attachment
127 * is via SIOCSLIFNAME or IF_UNITSEL.
129 * Such changes in the behavior of ip module was made to meet new or
130 * evolving standards requirements.
138 if (ioctl(fd, IF_UNITSEL, (char *)&ppa) < 0) {
147 * sys_init - System-dependent initialization.
156 union DL_primitives prim;
161 ipfd = open("/dev/ip", O_RDWR, 0);
163 fatal("Couldn't open IP device: %m");
165 if (default_device && !notty)
166 tty_sid = getsid((pid_t)0);
168 pppfd = open("/dev/ppp", O_RDWR | O_NONBLOCK, 0);
170 fatal("Can't open /dev/ppp: %m");
171 if (kdebugflag & 1) {
172 x = PPPDBG_LOG + PPPDBG_DRIVER;
173 strioctl(pppfd, PPPIO_DEBUG, &x, sizeof(int), 0);
176 /* Assign a new PPA and get its unit number. */
177 if (strioctl(pppfd, PPPIO_NEWPPA, &ifunit, 0, sizeof(int)) < 0)
178 fatal("Can't create new PPP interface: %m");
181 * Open the ppp device again and link it under the ip multiplexor.
182 * IP will assign a unit number which hopefully is the same as ifunit.
183 * I don't know any way to be certain they will be the same. :-(
185 ifd = open("/dev/ppp", O_RDWR, 0);
187 fatal("Can't open /dev/ppp (2): %m");
188 if (kdebugflag & 1) {
189 x = PPPDBG_LOG + PPPDBG_DRIVER;
190 strioctl(ifd, PPPIO_DEBUG, &x, sizeof(int), 0);
193 if (ioctl(ifd, I_PUSH, "ip") < 0) {
195 fatal("Can't push IP module: %m");
200 * Assign ppa according to the unit number returned by ppp device
201 * after plumbing is completed above.
203 if (sifppa(ifd, ifunit) < 0) {
205 fatal("Can't set ppa for unit %d: %m", ifunit);
210 if (dlpi_attach(ifd, ifunit) < 0 ||
211 dlpi_get_reply(ifd, &reply.prim, DL_OK_ACK, sizeof(reply)) < 0) {
213 fatal("Can't attach to ppp%d: %m", ifunit);
216 ipmuxid = ioctl(ipfd, I_LINK, ifd);
219 fatal("Can't link PPP device to IP: %m");
222 /* Set the interface name for the link. */
223 slprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "ppp%d", ifunit);
224 ifr.ifr_metric = ipmuxid;
225 if (strioctl(ipfd, SIOCSIFNAME, (char *)&ifr, sizeof ifr, 0) < 0)
226 fatal("Can't set interface name %s: %m", ifr.ifr_name);
233 * sys_cleanup - restore any system state we modified before exiting:
234 * mark the interface down, delete default route and/or proxy arp entry.
235 * This should call die() because it's called from die().
244 if (default_route_gateway)
245 cifdefaultroute(0, default_route_gateway, default_route_gateway);
247 cifproxyarp(0, proxy_arp_addr);
251 * sys_close - Clean up in a child process before execing.
262 * sys_check_options - check the options that the user specified
272 * daemon - Detach us from controlling terminal session.
275 daemon(nochdir, noclose)
276 int nochdir, noclose;
280 if ((pid = fork()) < 0)
283 exit(0); /* parent dies */
288 fclose(stdin); /* don't need stdin, stdout, stderr */
297 * ppp_available - check whether the system has any ppp interfaces
304 return stat("/dev/ppp", &buf) >= 0;
308 * establish_ppp - Turn the serial port into a ppp interface.
316 /* Pop any existing modules off the tty stream. */
318 if (ioctl(fd, I_LOOK, tty_modules[i]) < 0
319 || strcmp(tty_modules[i], "ptem") == 0
320 || ioctl(fd, I_POP, 0) < 0)
324 /* Push the async hdlc module and the compressor module. */
328 if (ioctl(fd, I_PUSH, "ppp_ahdl") < 0) {
329 error("Couldn't push PPP Async HDLC module: %m");
334 if (kdebugflag & 4) {
335 i = PPPDBG_LOG + PPPDBG_AHDLC;
336 strioctl(pppfd, PPPIO_DEBUG, &i, sizeof(int), 0);
338 if (ioctl(fd, I_PUSH, "ppp_comp") < 0)
339 error("Couldn't push PPP compression module: %m");
342 if (kdebugflag & 2) {
343 i = PPPDBG_LOG + PPPDBG_COMP;
344 strioctl(pppfd, PPPIO_DEBUG, &i, sizeof(int), 0);
347 /* Link the serial port under the PPP multiplexor. */
348 if ((fdmuxid = ioctl(pppfd, I_LINK, fd)) < 0) {
349 error("Can't link tty to PPP mux: %m");
357 * restore_loop - reattach the ppp unit to the loopback.
358 * This doesn't need to do anything because disestablish_ppp does it.
366 * disestablish_ppp - Restore the serial port to normal operation.
367 * It attempts to reconstruct the stream with the previously popped
368 * modules. This shouldn't call die() because it's called from die().
377 if (ioctl(pppfd, I_UNLINK, fdmuxid) < 0) {
379 error("Can't unlink tty from PPP mux: %m");
384 while (tty_npushed > 0 && ioctl(fd, I_POP, 0) >= 0)
386 for (i = tty_nmodules - 1; i >= 0; --i)
387 if (ioctl(fd, I_PUSH, tty_modules[i]) < 0)
388 error("Couldn't restore tty module %s: %m",
391 if (hungup && default_device && tty_sid > 0) {
393 * If we have received a hangup, we need to send a SIGHUP
394 * to the terminal's controlling process. The reason is
395 * that the original stream head for the terminal hasn't
396 * seen the M_HANGUP message (it went up through the ppp
397 * driver to the stream head for our fd to /dev/ppp).
399 kill(tty_sid, SIGHUP);
405 * Check whether the link seems not to be 8-bit clean.
413 if (strioctl(pppfd, PPPIO_GCLEAN, &x, 0, sizeof(x)) < 0)
418 s = "bit 7 set to 1";
421 s = "bit 7 set to 0";
431 warn("Serial link is not 8-bit clean:");
432 warn("All received characters had %s", s);
437 * List of valid speeds.
440 int speed_int, speed_val;
515 * Translate from bits/second to a speed_t.
521 struct speed *speedp;
525 for (speedp = speeds; speedp->speed_int; speedp++)
526 if (bps == speedp->speed_int)
527 return speedp->speed_val;
528 warn("speed %d not supported", bps);
533 * Translate from a speed_t to bits/second.
539 struct speed *speedp;
543 for (speedp = speeds; speedp->speed_int; speedp++)
544 if (speed == speedp->speed_val)
545 return speedp->speed_int;
550 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
551 * at the requested speed, etc. If `local' is true, set CLOCAL
552 * regardless of whether the modem option was specified.
555 set_up_tty(fd, local)
560 #if !defined (CRTSCTS)
564 if (!sync_serial && tcgetattr(fd, &tios) < 0)
565 fatal("tcgetattr: %m");
569 if (ioctl (fd, TCGETX, &tiox) < 0) {
581 ioctl(fd, TIOCGWINSZ, &wsinfo);
584 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
587 tios.c_cflag |= CRTSCTS;
588 else if (crtscts < 0)
589 tios.c_cflag &= ~CRTSCTS;
591 if (crtscts != 0 && !termiox_ok) {
592 error("Can't set RTS/CTS flow control");
593 } else if (crtscts > 0) {
594 tiox.x_hflag |= RTSXOFF|CTSXON;
595 } else if (crtscts < 0) {
596 tiox.x_hflag &= ~(RTSXOFF|CTSXON);
600 tios.c_cflag |= CS8 | CREAD | HUPCL;
602 tios.c_cflag |= CLOCAL;
603 tios.c_iflag = IGNBRK | IGNPAR;
607 tios.c_cc[VTIME] = 0;
610 tios.c_iflag |= IXON | IXOFF;
611 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
612 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
615 speed = translate_speed(inspeed);
617 cfsetospeed(&tios, speed);
618 cfsetispeed(&tios, speed);
620 speed = cfgetospeed(&tios);
622 * We can't proceed if the serial port speed is 0,
623 * since that implies that the serial port is disabled.
626 fatal("Baud rate for %s is 0; need explicit baud rate", devnam);
629 if (!sync_serial && tcsetattr(fd, TCSAFLUSH, &tios) < 0)
630 fatal("tcsetattr: %m");
633 if (termiox_ok && ioctl (fd, TCSETXF, &tiox) < 0){
634 error("TCSETXF: %m");
638 baud_rate = inspeed = baud_rate_of(speed);
643 * restore_tty - restore the terminal to the saved settings.
650 if (!default_device) {
652 * Turn off echoing, because otherwise we can get into
653 * a loop with the tty and the modem echoing to each other.
654 * We presume we are the sole user of this tty device, so
655 * when we close it, it will revert to its defaults anyway.
657 inittermios.c_lflag &= ~(ECHO | ECHONL);
659 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
660 if (!hungup && errno != ENXIO)
661 warn("tcsetattr: %m");
663 if (ioctl (fd, TCSETXF, &inittermiox) < 0){
664 if (!hungup && errno != ENXIO)
665 error("TCSETXF: %m");
668 ioctl(fd, TIOCSWINSZ, &wsinfo);
674 * setdtr - control the DTR line on the serial port.
675 * This is called from die(), so it shouldn't call die().
681 int modembits = TIOCM_DTR;
683 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
687 * open_loopback - open the device we use for getting packets
688 * in demand mode. Under Solaris 2, we use our existing fd
698 * output - Output PPP packet.
711 dbglog("sent %P", p, len);
714 data.buf = (caddr_t) p;
716 while (putmsg(pppfd, NULL, &data, 0) < 0) {
717 if (--retries < 0 || (errno != EWOULDBLOCK && errno != EAGAIN)) {
719 error("Couldn't send packet: %m");
723 pfd.events = POLLOUT;
724 poll(&pfd, 1, 250); /* wait for up to 0.25 seconds */
730 * wait_input - wait until there is data available,
731 * for the length of time specified by *timo (indefinite
736 struct timeval *timo;
740 t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
741 if (poll(pollfds, n_pollfds, t) < 0 && errno != EINTR)
746 * add_fd - add an fd to the set that wait_input waits for.
753 for (n = 0; n < n_pollfds; ++n)
754 if (pollfds[n].fd == fd)
756 if (n_pollfds < MAX_POLLFDS) {
757 pollfds[n_pollfds].fd = fd;
758 pollfds[n_pollfds].events = POLLIN | POLLPRI | POLLHUP;
761 error("Too many inputs!");
765 * remove_fd - remove an fd from the set that wait_input waits for.
772 for (n = 0; n < n_pollfds; ++n) {
773 if (pollfds[n].fd == fd) {
774 while (++n < n_pollfds)
775 pollfds[n-1] = pollfds[n];
784 * wait_loop_output - wait until there is data available on the
785 * loopback, for the length of time specified by *timo (indefinite
789 wait_loop_output(timo)
790 struct timeval *timo;
796 * wait_time - wait for a given length of time or until a
797 * signal is received.
801 struct timeval *timo;
805 n = select(0, NULL, NULL, NULL, timo);
806 if (n < 0 && errno != EINTR)
813 * read_packet - get a PPP packet from the serial device.
819 struct strbuf ctrl, data;
821 unsigned char ctrlbuf[sizeof(union DL_primitives) + 64];
824 data.maxlen = PPP_MRU + PPP_HDRLEN;
825 data.buf = (caddr_t) buf;
826 ctrl.maxlen = sizeof(ctrlbuf);
827 ctrl.buf = (caddr_t) ctrlbuf;
829 len = getmsg(pppfd, &ctrl, &data, &flags);
831 if (errno == EAGAIN || errno == EINTR)
833 fatal("Error reading packet: %m");
840 * Got a M_PROTO or M_PCPROTO message. Interpret it
841 * as a DLPI primitive??
844 dbglog("got dlpi prim 0x%x, len=%d",
845 ((union DL_primitives *)ctrlbuf)->dl_primitive, ctrl.len);
851 * get_loop_output - get outgoing packets from the ppp device,
852 * and detect when we want to bring the real link up.
853 * Return value is 1 if we need to bring up the link, 0 otherwise.
861 while ((len = read_packet(inpacket_buf)) > 0) {
862 if (loop_frame(inpacket_buf, len))
869 * ppp_send_config - configure the transmit characteristics of
873 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
882 if (strioctl(pppfd, PPPIO_MTU, &mtu, sizeof(mtu), 0) < 0) {
883 if (hungup && errno == ENXIO)
885 error("Couldn't set MTU: %m");
888 /* can't set these if we don't have a stream attached below /dev/ppp */
889 if (strioctl(pppfd, PPPIO_XACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
890 error("Couldn't set transmit ACCM: %m");
892 cf[0] = (pcomp? COMP_PROT: 0) + (accomp? COMP_AC: 0);
893 cf[1] = COMP_PROT | COMP_AC;
894 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
895 error("Couldn't set prot/AC compression: %m");
899 /* set the MTU for IP as well */
900 memset(&ifr, 0, sizeof(ifr));
901 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
902 ifr.ifr_metric = link_mtu;
903 if (ioctl(ipfd, SIOCSIFMTU, &ifr) < 0) {
904 error("Couldn't set IP MTU: %m");
909 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
912 ppp_set_xaccm(unit, accm)
917 && strioctl(pppfd, PPPIO_XACCM, accm, sizeof(ext_accm), 0) < 0) {
918 if (!hungup || errno != ENXIO)
919 warn("Couldn't set extended ACCM: %m");
924 * ppp_recv_config - configure the receive-side characteristics of
928 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
936 if (strioctl(pppfd, PPPIO_MRU, &mru, sizeof(mru), 0) < 0) {
937 if (hungup && errno == ENXIO)
939 error("Couldn't set MRU: %m");
942 /* can't set these if we don't have a stream attached below /dev/ppp */
943 if (strioctl(pppfd, PPPIO_RACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
944 error("Couldn't set receive ACCM: %m");
946 cf[0] = (pcomp? DECOMP_PROT: 0) + (accomp? DECOMP_AC: 0);
947 cf[1] = DECOMP_PROT | DECOMP_AC;
948 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
949 error("Couldn't set prot/AC decompression: %m");
955 * ccp_test - ask kernel whether a given compression method
956 * is acceptable for use.
959 ccp_test(unit, opt_ptr, opt_len, for_transmit)
960 int unit, opt_len, for_transmit;
963 if (strioctl(pppfd, (for_transmit? PPPIO_XCOMP: PPPIO_RCOMP),
964 opt_ptr, opt_len, 0) >= 0)
966 return (errno == ENOSR)? 0: -1;
970 * ccp_flags_set - inform kernel about the current state of CCP.
973 ccp_flags_set(unit, isopen, isup)
974 int unit, isopen, isup;
978 cf[0] = (isopen? CCP_ISOPEN: 0) + (isup? CCP_ISUP: 0);
979 cf[1] = CCP_ISOPEN | CCP_ISUP | CCP_ERROR | CCP_FATALERROR;
980 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
981 if (!hungup || errno != ENXIO)
982 error("Couldn't set kernel CCP state: %m");
987 * get_idle_time - return how long the link has been idle.
994 return strioctl(pppfd, PPPIO_GIDLE, ip, 0, sizeof(struct ppp_idle)) >= 0;
998 * get_ppp_stats - return statistics for the link.
1001 get_ppp_stats(u, stats)
1003 struct pppd_stats *stats;
1007 if (strioctl(pppfd, PPPIO_GETSTAT, &s, 0, sizeof(s)) < 0) {
1008 error("Couldn't get link statistics: %m");
1011 stats->bytes_in = s.p.ppp_ibytes;
1012 stats->bytes_out = s.p.ppp_obytes;
1018 * set_filters - transfer the pass and active filters to the kernel.
1021 set_filters(pass, active)
1022 struct bpf_program *pass, *active;
1026 if (pass->bf_len > 0) {
1027 if (strioctl(pppfd, PPPIO_PASSFILT, pass,
1028 sizeof(struct bpf_program), 0) < 0) {
1029 error("Couldn't set pass-filter in kernel: %m");
1033 if (active->bf_len > 0) {
1034 if (strioctl(pppfd, PPPIO_ACTIVEFILT, active,
1035 sizeof(struct bpf_program), 0) < 0) {
1036 error("Couldn't set active-filter in kernel: %m");
1045 * ccp_fatal_error - returns 1 if decompression was disabled as a
1046 * result of an error detected after decompression of a packet,
1047 * 0 otherwise. This is necessary because of patent nonsense.
1050 ccp_fatal_error(unit)
1056 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
1057 if (errno != ENXIO && errno != EINVAL)
1058 error("Couldn't get compression flags: %m");
1061 return cf[0] & CCP_FATALERROR;
1065 * sifvjcomp - config tcp header compression
1068 sifvjcomp(u, vjcomp, xcidcomp, xmaxcid)
1069 int u, vjcomp, xcidcomp, xmaxcid;
1075 maxcid[0] = xcidcomp;
1076 maxcid[1] = 15; /* XXX should be rmaxcid */
1077 if (strioctl(pppfd, PPPIO_VJINIT, maxcid, sizeof(maxcid), 0) < 0) {
1078 error("Couldn't initialize VJ compression: %m");
1082 cf[0] = (vjcomp? COMP_VJC + DECOMP_VJC: 0) /* XXX this is wrong */
1083 + (xcidcomp? COMP_VJCCID + DECOMP_VJCCID: 0);
1084 cf[1] = COMP_VJC + DECOMP_VJC + COMP_VJCCID + DECOMP_VJCCID;
1085 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
1087 error("Couldn't enable VJ compression: %m");
1094 * sifup - Config the interface up and enable IP packets to pass.
1102 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1103 if (ioctl(ipfd, SIOCGIFFLAGS, &ifr) < 0) {
1104 error("Couldn't mark interface up (get): %m");
1107 ifr.ifr_flags |= IFF_UP;
1108 if (ioctl(ipfd, SIOCSIFFLAGS, &ifr) < 0) {
1109 error("Couldn't mark interface up (set): %m");
1117 * sifdown - Config the interface down and disable IP.
1127 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1128 if (ioctl(ipfd, SIOCGIFFLAGS, &ifr) < 0) {
1129 error("Couldn't mark interface down (get): %m");
1132 ifr.ifr_flags &= ~IFF_UP;
1133 if (ioctl(ipfd, SIOCSIFFLAGS, &ifr) < 0) {
1134 error("Couldn't mark interface down (set): %m");
1142 * sifnpmode - Set the mode for handling packets for a given NP.
1145 sifnpmode(u, proto, mode)
1153 npi[1] = (int) mode;
1154 if (strioctl(pppfd, PPPIO_NPMODE, &npi, 2 * sizeof(int), 0) < 0) {
1155 error("ioctl(set NP %d mode to %d): %m", proto, mode);
1161 #define INET_ADDR(x) (((struct sockaddr_in *) &(x))->sin_addr.s_addr)
1164 * sifaddr - Config the interface IP addresses and netmask.
1174 memset(&ifr, 0, sizeof(ifr));
1175 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1176 ifr.ifr_addr.sa_family = AF_INET;
1177 INET_ADDR(ifr.ifr_addr) = m;
1178 if (ioctl(ipfd, SIOCSIFNETMASK, &ifr) < 0) {
1179 error("Couldn't set IP netmask: %m");
1182 ifr.ifr_addr.sa_family = AF_INET;
1183 INET_ADDR(ifr.ifr_addr) = o;
1184 if (ioctl(ipfd, SIOCSIFADDR, &ifr) < 0) {
1185 error("Couldn't set local IP address: %m");
1190 * On some systems, we have to explicitly set the point-to-point
1191 * flag bit before we can set a destination address.
1193 if (ioctl(ipfd, SIOCGIFFLAGS, &ifr) >= 0
1194 && (ifr.ifr_flags & IFF_POINTOPOINT) == 0) {
1195 ifr.ifr_flags |= IFF_POINTOPOINT;
1196 if (ioctl(ipfd, SIOCSIFFLAGS, &ifr) < 0) {
1197 error("Couldn't mark interface pt-to-pt: %m");
1201 ifr.ifr_dstaddr.sa_family = AF_INET;
1202 INET_ADDR(ifr.ifr_dstaddr) = h;
1203 if (ioctl(ipfd, SIOCSIFDSTADDR, &ifr) < 0) {
1204 error("Couldn't set remote IP address: %m");
1207 #if 0 /* now done in ppp_send_config */
1208 ifr.ifr_metric = link_mtu;
1209 if (ioctl(ipfd, SIOCSIFMTU, &ifr) < 0) {
1210 error("Couldn't set IP MTU: %m");
1219 * cifaddr - Clear the interface IP addresses, and delete routes
1220 * through the interface if possible.
1227 #if defined(__USLC__) /* was: #if 0 */
1228 cifroute(unit, ouraddr, hisaddr);
1230 notice("Removing ppp interface unit");
1231 if (ioctl(ipfd, I_UNLINK, ipmuxid) < 0) {
1232 error("Can't remove ppp interface unit: %m");
1243 * sifdefaultroute - assign a default route through the address given.
1246 sifdefaultroute(u, l, g)
1252 #if defined(__USLC__)
1253 g = l; /* use the local address as gateway */
1255 memset(&rt, 0, sizeof(rt));
1256 rt.rt_dst.sa_family = AF_INET;
1257 INET_ADDR(rt.rt_dst) = 0;
1258 rt.rt_gateway.sa_family = AF_INET;
1259 INET_ADDR(rt.rt_gateway) = g;
1260 rt.rt_flags = RTF_GATEWAY;
1262 if (ioctl(ipfd, SIOCADDRT, &rt) < 0) {
1263 error("Can't add default route: %m");
1267 default_route_gateway = g;
1272 * cifdefaultroute - delete a default route through the address given.
1275 cifdefaultroute(u, l, g)
1281 #if defined(__USLC__)
1282 g = l; /* use the local address as gateway */
1284 memset(&rt, 0, sizeof(rt));
1285 rt.rt_dst.sa_family = AF_INET;
1286 INET_ADDR(rt.rt_dst) = 0;
1287 rt.rt_gateway.sa_family = AF_INET;
1288 INET_ADDR(rt.rt_gateway) = g;
1289 rt.rt_flags = RTF_GATEWAY;
1291 if (ioctl(ipfd, SIOCDELRT, &rt) < 0) {
1292 error("Can't delete default route: %m");
1296 default_route_gateway = 0;
1301 * sifproxyarp - Make a proxy ARP entry for the peer.
1304 sifproxyarp(unit, hisaddr)
1308 struct arpreq arpreq;
1310 memset(&arpreq, 0, sizeof(arpreq));
1311 if (!get_ether_addr(hisaddr, &arpreq.arp_ha))
1314 arpreq.arp_pa.sa_family = AF_INET;
1315 INET_ADDR(arpreq.arp_pa) = hisaddr;
1316 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1317 if (ioctl(ipfd, SIOCSARP, (caddr_t) &arpreq) < 0) {
1318 error("Couldn't set proxy ARP entry: %m");
1322 proxy_arp_addr = hisaddr;
1327 * cifproxyarp - Delete the proxy ARP entry for the peer.
1330 cifproxyarp(unit, hisaddr)
1334 struct arpreq arpreq;
1336 memset(&arpreq, 0, sizeof(arpreq));
1337 arpreq.arp_pa.sa_family = AF_INET;
1338 INET_ADDR(arpreq.arp_pa) = hisaddr;
1339 if (ioctl(ipfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1340 error("Couldn't delete proxy ARP entry: %m");
1349 * get_ether_addr - get the hardware address of an interface on the
1350 * the same subnet as ipaddr.
1355 get_ether_addr(ipaddr, hwaddr)
1357 struct sockaddr *hwaddr;
1359 struct ifreq *ifr, *ifend, ifreq;
1362 u_int32_t ina, mask;
1365 * Scan through the system's network interfaces.
1368 if (ioctl(ipfd, SIOCGIFNUM, &nif) < 0)
1371 ifc.ifc_len = nif * sizeof(struct ifreq);
1372 ifc.ifc_buf = (caddr_t) malloc(ifc.ifc_len);
1373 if (ifc.ifc_buf == 0)
1375 if (ioctl(ipfd, SIOCGIFCONF, &ifc) < 0) {
1376 warn("Couldn't get system interface list: %m");
1380 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1381 for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
1382 if (ifr->ifr_addr.sa_family != AF_INET)
1385 * Check that the interface is up, and not point-to-point or loopback.
1387 strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1388 if (ioctl(ipfd, SIOCGIFFLAGS, &ifreq) < 0)
1390 if ((ifreq.ifr_flags &
1391 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1392 != (IFF_UP|IFF_BROADCAST))
1395 * Get its netmask and check that it's on the right subnet.
1397 if (ioctl(ipfd, SIOCGIFNETMASK, &ifreq) < 0)
1399 ina = INET_ADDR(ifr->ifr_addr);
1400 mask = INET_ADDR(ifreq.ifr_addr);
1401 if ((ipaddr & mask) == (ina & mask))
1406 warn("No suitable interface found for proxy ARP");
1411 info("found interface %s for proxy ARP", ifr->ifr_name);
1412 if (!get_hw_addr(ifr->ifr_name, ina, hwaddr)) {
1413 error("Couldn't get hardware address for %s", ifr->ifr_name);
1423 * get_hw_addr - obtain the hardware address for a named interface.
1426 get_hw_addr(name, ina, hwaddr)
1429 struct sockaddr *hwaddr;
1432 /* New way - get the address by doing an arp request. */
1436 s = socket(AF_INET, SOCK_DGRAM, 0);
1439 memset(&req, 0, sizeof(req));
1440 req.arp_pa.sa_family = AF_INET;
1441 INET_ADDR(req.arp_pa) = ina;
1442 if (ioctl(s, SIOCGARP, &req) < 0) {
1443 error("Couldn't get ARP entry for %s: %m", ip_ntoa(ina));
1446 *hwaddr = req.arp_ha;
1447 hwaddr->sa_family = AF_UNSPEC;
1451 int unit, iffd, adrlen;
1452 unsigned char *adrp;
1455 union DL_primitives prim;
1460 * We have to open the device and ask it for its hardware address.
1461 * First split apart the device name and unit.
1463 slprintf(ifdev, sizeof(ifdev), "/dev/%s", name);
1464 for (q = ifdev + strlen(ifdev); --q >= ifdev; )
1471 * Open the device and do a DLPI attach and phys_addr_req.
1473 iffd = open(ifdev, O_RDWR);
1475 error("Can't open %s: %m", ifdev);
1478 if (dlpi_attach(iffd, unit) < 0
1479 || dlpi_get_reply(iffd, &reply.prim, DL_OK_ACK, sizeof(reply)) < 0
1480 || dlpi_info_req(iffd) < 0
1481 || dlpi_get_reply(iffd, &reply.prim, DL_INFO_ACK, sizeof(reply)) < 0) {
1486 adrlen = reply.prim.info_ack.dl_addr_length;
1487 adrp = (unsigned char *)&reply + reply.prim.info_ack.dl_addr_offset;
1488 #if DL_CURRENT_VERSION >= 2
1489 if (reply.prim.info_ack.dl_sap_length < 0)
1490 adrlen += reply.prim.info_ack.dl_sap_length;
1492 adrp += reply.prim.info_ack.dl_sap_length;
1494 hwaddr->sa_family = AF_UNSPEC;
1495 memcpy(hwaddr->sa_data, adrp, adrlen);
1502 dlpi_attach(fd, ppa)
1505 dl_attach_req_t req;
1508 req.dl_primitive = DL_ATTACH_REQ;
1510 buf.len = sizeof(req);
1511 buf.buf = (void *) &req;
1512 return putmsg(fd, &buf, NULL, RS_HIPRI);
1522 req.dl_primitive = DL_INFO_REQ;
1523 buf.len = sizeof(req);
1524 buf.buf = (void *) &req;
1525 return putmsg(fd, &buf, NULL, RS_HIPRI);
1529 dlpi_get_reply(fd, reply, expected_prim, maxlen)
1530 union DL_primitives *reply;
1531 int fd, expected_prim, maxlen;
1538 * Use poll to wait for a message with a timeout.
1541 pfd.events = POLLIN | POLLPRI;
1543 n = poll(&pfd, 1, 1000);
1544 } while (n == -1 && errno == EINTR);
1551 buf.maxlen = maxlen;
1552 buf.buf = (void *) reply;
1554 if (getmsg(fd, &buf, NULL, &flags) < 0)
1557 if (buf.len < sizeof(ulong)) {
1559 dbglog("dlpi response short (len=%d)\n", buf.len);
1563 if (reply->dl_primitive == expected_prim)
1567 if (reply->dl_primitive == DL_ERROR_ACK) {
1568 dbglog("dlpi error %d (unix errno %d) for prim %x\n",
1569 reply->error_ack.dl_errno, reply->error_ack.dl_unix_errno,
1570 reply->error_ack.dl_error_primitive);
1572 dbglog("dlpi unexpected response prim %x\n",
1573 reply->dl_primitive);
1581 * Return user specified netmask, modified by any mask we might determine
1582 * for address `addr' (in network byte order).
1583 * Here we scan through the system's list of interfaces, looking for
1584 * any non-point-to-point interfaces which might appear to be on the same
1585 * network as `addr'. If we find any, we OR in their netmask to the
1586 * user-specified netmask.
1592 u_int32_t mask, nmask, ina;
1593 struct ifreq *ifr, *ifend, ifreq;
1598 if (IN_CLASSA(addr)) /* determine network mask for address class */
1599 nmask = IN_CLASSA_NET;
1600 else if (IN_CLASSB(addr))
1601 nmask = IN_CLASSB_NET;
1603 nmask = IN_CLASSC_NET;
1604 /* class D nets are disallowed by bad_ip_adrs */
1605 mask = netmask | htonl(nmask);
1608 * Scan through the system's network interfaces.
1611 if (ioctl(ipfd, SIOCGIFNUM, &nif) < 0)
1614 ifc.ifc_len = nif * sizeof(struct ifreq);
1615 ifc.ifc_buf = (caddr_t) malloc(ifc.ifc_len);
1616 if (ifc.ifc_buf == 0)
1618 if (ioctl(ipfd, SIOCGIFCONF, &ifc) < 0) {
1619 warn("Couldn't get system interface list: %m");
1623 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1624 for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
1626 * Check the interface's internet address.
1628 if (ifr->ifr_addr.sa_family != AF_INET)
1630 ina = INET_ADDR(ifr->ifr_addr);
1631 if ((ntohl(ina) & nmask) != (addr & nmask))
1634 * Check that the interface is up, and not point-to-point or loopback.
1636 strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1637 if (ioctl(ipfd, SIOCGIFFLAGS, &ifreq) < 0)
1639 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1643 * Get its netmask and OR it into our mask.
1645 if (ioctl(ipfd, SIOCGIFNETMASK, &ifreq) < 0)
1647 mask |= INET_ADDR(ifreq.ifr_addr);
1655 * logwtmp - write an accounting record to the /var/adm/wtmp file.
1658 logwtmp(line, name, host)
1659 const char *line, *name, *host;
1661 static struct utmpx utmpx;
1665 strncpy(utmpx.ut_user, name, sizeof(utmpx.ut_user));
1666 strncpy(utmpx.ut_id, ifname, sizeof(utmpx.ut_id));
1667 strncpy(utmpx.ut_line, line, sizeof(utmpx.ut_line));
1668 utmpx.ut_pid = getpid();
1669 utmpx.ut_type = USER_PROCESS;
1671 utmpx.ut_type = DEAD_PROCESS;
1673 gettimeofday(&utmpx.ut_tv, NULL);
1674 updwtmpx("/var/adm/wtmpx", &utmpx);
1678 * get_host_seed - return the serial number of this machine.
1685 if (sysinfo(SI_HW_SERIAL, buf, sizeof(buf)) < 0) {
1686 error("sysinfo: %m");
1689 return (int) strtoul(buf, NULL, 16);
1693 strioctl(fd, cmd, ptr, ilen, olen)
1694 int fd, cmd, ilen, olen;
1697 struct strioctl str;
1703 if (ioctl(fd, I_STR, &str) == -1)
1705 if (str.ic_len != olen)
1706 dbglog("strioctl: expected %d bytes, got %d for cmd %x\n",
1707 olen, str.ic_len, cmd);
1713 * lock - create a lock file for the named lock device
1716 #define LOCK_PREFIX "/var/spool/locks/LK."
1717 static char lock_file[40]; /* name of lock file created */
1727 if (stat(dev, &sbuf) < 0) {
1728 error("Can't get device number for %s: %m", dev);
1731 if ((sbuf.st_mode & S_IFMT) != S_IFCHR) {
1732 error("Can't lock %s: not a character device", dev);
1735 slprintf(lock_file, sizeof(lock_file), "%s%03d.%03d.%03d",
1736 LOCK_PREFIX, major(sbuf.st_dev),
1737 major(sbuf.st_rdev), minor(sbuf.st_rdev));
1739 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1741 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1742 /* Read the lock file to find out who has the device locked */
1743 n = read(fd, ascii_pid, 11);
1745 error("Can't read pid from lock file %s", lock_file);
1749 pid = atoi(ascii_pid);
1750 if (pid > 0 && kill(pid, 0) == -1 && errno == ESRCH) {
1751 /* pid no longer exists - remove the lock file */
1752 if (unlink(lock_file) == 0) {
1754 notice("Removed stale lock on %s (pid %d)",
1758 warn("Couldn't remove stale lock on %s",
1761 notice("Device %s is locked by pid %d",
1766 error("Can't create lock file %s: %m", lock_file);
1771 slprintf(ascii_pid, sizeof(ascii_pid), "%10d\n", getpid());
1772 write(fd, ascii_pid, 11);
1779 * unlock - remove our lockfile
1792 * cifroute - delete a route through the addresses given.
1795 cifroute(u, our, his)
1801 memset(&rt, 0, sizeof(rt));
1802 rt.rt_dst.sa_family = AF_INET;
1803 INET_ADDR(rt.rt_dst) = his;
1804 rt.rt_gateway.sa_family = AF_INET;
1805 INET_ADDR(rt.rt_gateway) = our;
1806 rt.rt_flags = RTF_HOST;
1808 if (ioctl(ipfd, SIOCDELRT, &rt) < 0) {
1809 error("Can't delete route: %m");
1817 * have_route_to - determine if the system has a route to the specified
1818 * IP address. Returns 0 if not, 1 if so, -1 if we can't tell.
1819 * `addr' is in network byte order.
1820 * For demand mode to work properly, we have to ignore routes
1821 * through our own interface.
1823 #ifndef T_CURRENT /* needed for Solaris 2.5 */
1824 #define T_CURRENT MI_T_CURRENT
1832 int fd, r, flags, i;
1834 struct T_optmgmt_req req;
1838 struct T_optmgmt_ack ack;
1839 unsigned char space[64];
1842 struct strbuf cbuf, dbuf;
1844 mib2_ipRouteEntry_t routes[8];
1845 mib2_ipRouteEntry_t *rp;
1847 fd = open("/dev/ip", O_RDWR);
1849 warn("have_route_to: couldn't open /dev/ip: %m");
1853 req.req.PRIM_type = T_OPTMGMT_REQ;
1854 req.req.OPT_offset = (char *) &req.hdr - (char *) &req;
1855 req.req.OPT_length = sizeof(req.hdr);
1856 req.req.MGMT_flags = T_CURRENT;
1858 req.hdr.level = MIB2_IP;
1862 cbuf.buf = (char *) &req;
1863 cbuf.len = sizeof(req);
1865 if (putmsg(fd, &cbuf, NULL, 0) == -1) {
1866 warn("have_route_to: putmsg: %m");
1872 cbuf.buf = (char *) &ack;
1873 cbuf.maxlen = sizeof(ack);
1874 dbuf.buf = (char *) routes;
1875 dbuf.maxlen = sizeof(routes);
1877 r = getmsg(fd, &cbuf, &dbuf, &flags);
1879 warn("have_route_to: getmsg: %m");
1884 if (cbuf.len < sizeof(struct T_optmgmt_ack)
1885 || ack.ack.PRIM_type != T_OPTMGMT_ACK
1886 || ack.ack.MGMT_flags != T_SUCCESS
1887 || ack.ack.OPT_length < sizeof(struct opthdr)) {
1888 dbglog("have_route_to: bad message len=%d prim=%d",
1889 cbuf.len, ack.ack.PRIM_type);
1894 rh = (struct opthdr *) ((char *)&ack + ack.ack.OPT_offset);
1895 if (rh->level == 0 && rh->name == 0)
1897 if (rh->level != MIB2_IP || rh->name != MIB2_IP_21) {
1898 while (r == MOREDATA)
1899 r = getmsg(fd, NULL, &dbuf, &flags);
1904 nroutes = dbuf.len / sizeof(mib2_ipRouteEntry_t);
1905 for (rp = routes, i = 0; i < nroutes; ++i, ++rp) {
1906 if (rp->ipRouteMask != ~0) {
1907 dbglog("have_route_to: dest=%x gw=%x mask=%x\n",
1908 rp->ipRouteDest, rp->ipRouteNextHop,
1910 if (((addr ^ rp->ipRouteDest) & rp->ipRouteMask) == 0
1911 && rp->ipRouteNextHop != remote_addr)
1917 r = getmsg(fd, NULL, &dbuf, &flags);
1928 * get_pty - get a pty master/slave pair and chown the slave side to
1929 * the uid given. Assumes slave_name points to MAXPATHLEN bytes of space.
1932 get_pty(master_fdp, slave_fdp, slave_name, uid)
1940 struct termios tios;
1942 mfd = open("/dev/ptmx", O_RDWR);
1944 error("Couldn't open pty master: %m");
1948 pty_name = ptsname(mfd);
1949 if (pty_name == NULL) {
1950 error("Couldn't get name of pty slave");
1954 if (chown(pty_name, uid, -1) < 0)
1955 warn("Couldn't change owner of pty slave: %m");
1956 if (chmod(pty_name, S_IRUSR | S_IWUSR) < 0)
1957 warn("Couldn't change permissions on pty slave: %m");
1958 if (unlockpt(mfd) < 0)
1959 warn("Couldn't unlock pty slave: %m");
1961 sfd = open(pty_name, O_RDWR);
1963 error("Couldn't open pty slave %s: %m", pty_name);
1967 if (ioctl(sfd, I_PUSH, "ptem") < 0)
1968 warn("Couldn't push ptem module on pty slave: %m");
1970 dbglog("Using %s", pty_name);
1971 strlcpy(slave_name, pty_name, MAXPATHLEN);