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.14 1996/09/26 06:23:50 paulus Exp $";
36 #if defined(SNI) || defined(__USLC__)
37 extern void *alloca(size_t);
47 #include <sys/termiox.h>
51 #include <sys/types.h>
52 #include <sys/ioccom.h>
53 #include <sys/stream.h>
54 #include <sys/stropts.h>
55 #include <sys/socket.h>
56 #include <sys/sockio.h>
57 #include <sys/syslog.h>
58 #include <sys/sysmacros.h>
59 #include <sys/systeminfo.h>
62 #include <sys/mkdev.h>
64 #include <net/if_arp.h>
65 #include <net/route.h>
66 #include <net/ppp_defs.h>
67 #include <net/pppio.h>
68 #include <netinet/in.h>
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;
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 default_route_gateway; /* Gateway for default route added */
95 static u_int32_t proxy_arp_addr; /* Addr for proxy arp entry added */
97 /* Prototypes for procedures local to this file. */
98 static int translate_speed __P((int));
99 static int baud_rate_of __P((int));
100 static int get_ether_addr __P((u_int32_t, struct sockaddr *));
101 static int get_hw_addr __P((char *, struct sockaddr *));
102 static int dlpi_attach __P((int, int));
103 static int dlpi_info_req __P((int));
104 static int dlpi_get_reply __P((int, union DL_primitives *, int, int));
105 static int strioctl __P((int, int, void *, int, int));
109 * sys_init - System-dependent initialization.
118 union DL_primitives prim;
123 ipfd = open("/dev/ip", O_RDWR, 0);
125 syslog(LOG_ERR, "Couldn't open IP device: %m");
130 tty_sid = getsid((pid_t)0);
132 pppfd = open("/dev/ppp", O_RDWR | O_NONBLOCK, 0);
134 syslog(LOG_ERR, "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 syslog(LOG_ERR, "Can't create new PPP interface: %m");
149 * Open the ppp device again and link it under the ip multiplexor.
150 * IP will assign a unit number which hopefully is the same as ifunit.
151 * I don't know any way to be certain they will be the same. :-(
153 ifd = open("/dev/ppp", O_RDWR, 0);
155 syslog(LOG_ERR, "Can't open /dev/ppp (2): %m");
158 if (kdebugflag & 1) {
159 x = PPPDBG_LOG + PPPDBG_DRIVER;
160 strioctl(ifd, PPPIO_DEBUG, &x, sizeof(int), 0);
163 if (ioctl(ifd, I_PUSH, "ip") < 0) {
164 syslog(LOG_ERR, "Can't push IP module: %m");
169 if (dlpi_attach(ifd, ifunit) < 0 ||
170 dlpi_get_reply(ifd, &reply.prim, DL_OK_ACK, sizeof(reply)) < 0) {
171 syslog(LOG_ERR, "Can't attach to ppp%d: %m", ifunit);
176 ipmuxid = ioctl(ipfd, I_LINK, ifd);
179 syslog(LOG_ERR, "Can't link PPP device to IP: %m");
184 /* Set the interface name for the link. */
185 (void) sprintf (ifr.ifr_name, "ppp%d", ifunit);
186 ifr.ifr_metric = ipmuxid;
187 if (strioctl(ipfd, SIOCSIFNAME, (char *)&ifr, sizeof ifr, 0) < 0) {
188 syslog(LOG_ERR, "Can't set interface name %s: %m", ifr.ifr_name);
195 * sys_cleanup - restore any system state we modified before exiting:
196 * mark the interface down, delete default route and/or proxy arp entry.
197 * This should call die() because it's called from die().
206 if (default_route_gateway)
207 cifdefaultroute(0, default_route_gateway);
209 cifproxyarp(0, proxy_arp_addr);
213 * sys_close - Clean up in a child process before execing.
224 * sys_check_options - check the options that the user specified
233 * daemon - Detach us from controlling terminal session.
236 daemon(nochdir, noclose)
237 int nochdir, noclose;
241 if ((pid = fork()) < 0)
244 exit(0); /* parent dies */
249 fclose(stdin); /* don't need stdin, stdout, stderr */
257 * ppp_available - check whether the system has any ppp interfaces
264 return stat("/dev/ppp", &buf) >= 0;
268 * establish_ppp - Turn the serial port into a ppp interface.
276 /* Pop any existing modules off the tty stream. */
278 if (ioctl(fd, I_LOOK, tty_modules[i]) < 0
279 || ioctl(fd, I_POP, 0) < 0)
283 /* Push the async hdlc module and the compressor module. */
284 if (ioctl(fd, I_PUSH, "ppp_ahdl") < 0) {
285 syslog(LOG_ERR, "Couldn't push PPP Async HDLC module: %m");
288 if (kdebugflag & 4) {
289 i = PPPDBG_LOG + PPPDBG_AHDLC;
290 strioctl(pppfd, PPPIO_DEBUG, &i, sizeof(int), 0);
292 if (ioctl(fd, I_PUSH, "ppp_comp") < 0) {
293 syslog(LOG_ERR, "Couldn't push PPP compression module: %m");
296 if (kdebugflag & 2) {
297 i = PPPDBG_LOG + PPPDBG_COMP;
298 strioctl(pppfd, PPPIO_DEBUG, &i, sizeof(int), 0);
301 /* Link the serial port under the PPP multiplexor. */
302 if ((fdmuxid = ioctl(pppfd, I_LINK, fd)) < 0) {
303 syslog(LOG_ERR, "Can't link tty to PPP mux: %m");
309 * restore_loop - reattach the ppp unit to the loopback.
310 * This doesn't need to do anything because disestablish_ppp does it.
318 * disestablish_ppp - Restore the serial port to normal operation.
319 * It attempts to reconstruct the stream with the previously popped
320 * modules. This shouldn't call die() because it's called from die().
329 if (ioctl(pppfd, I_UNLINK, fdmuxid) < 0) {
331 syslog(LOG_ERR, "Can't unlink tty from PPP mux: %m");
336 while (ioctl(fd, I_POP, 0) >= 0)
338 for (i = tty_nmodules - 1; i >= 0; --i)
339 if (ioctl(fd, I_PUSH, tty_modules[i]) < 0)
340 syslog(LOG_ERR, "Couldn't restore tty module %s: %m",
343 if (hungup && default_device && tty_sid > 0) {
345 * If we have received a hangup, we need to send a SIGHUP
346 * to the terminal's controlling process. The reason is
347 * that the original stream head for the terminal hasn't
348 * seen the M_HANGUP message (it went up through the ppp
349 * driver to the stream head for our fd to /dev/ppp).
351 kill(tty_sid, SIGHUP);
357 * Check whether the link seems not to be 8-bit clean.
365 if (strioctl(pppfd, PPPIO_GCLEAN, &x, 0, sizeof(x)) < 0)
370 s = "bit 7 set to 1";
373 s = "bit 7 set to 0";
383 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
384 syslog(LOG_WARNING, "All received characters had %s", s);
389 * List of valid speeds.
392 int speed_int, speed_val;
464 * Translate from bits/second to a speed_t.
470 struct speed *speedp;
474 for (speedp = speeds; speedp->speed_int; speedp++)
475 if (bps == speedp->speed_int)
476 return speedp->speed_val;
477 syslog(LOG_WARNING, "speed %d not supported", bps);
482 * Translate from a speed_t to bits/second.
488 struct speed *speedp;
492 for (speedp = speeds; speedp->speed_int; speedp++)
493 if (speed == speedp->speed_val)
494 return speedp->speed_int;
499 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
500 * at the requested speed, etc. If `local' is true, set CLOCAL
501 * regardless of whether the modem option was specified.
504 set_up_tty(fd, local)
509 #if !defined (CRTSCTS)
513 if (tcgetattr(fd, &tios) < 0) {
514 syslog(LOG_ERR, "tcgetattr: %m");
519 if (ioctl (fd, TCGETX, &tiox) < 0) {
520 syslog (LOG_ERR, "TCGETX: %m");
530 ioctl(fd, TIOCGWINSZ, &wsinfo);
533 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
536 tios.c_cflag |= CRTSCTS;
537 else if (crtscts < 0)
538 tios.c_cflag &= ~CRTSCTS;
541 tiox.x_hflag |= RTSXOFF|CTSXON;
543 else if (crtscts < 0) {
544 tiox.x_hflag &= ~(RTSXOFF|CTSXON);
548 tios.c_cflag |= CS8 | CREAD | HUPCL;
550 tios.c_cflag |= CLOCAL;
551 tios.c_iflag = IGNBRK | IGNPAR;
555 tios.c_cc[VTIME] = 0;
558 tios.c_iflag |= IXON | IXOFF;
559 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
560 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
563 speed = translate_speed(inspeed);
565 cfsetospeed(&tios, speed);
566 cfsetispeed(&tios, speed);
568 speed = cfgetospeed(&tios);
570 * We can't proceed if the serial port speed is 0,
571 * since that implies that the serial port is disabled.
574 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
580 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
581 syslog(LOG_ERR, "tcsetattr: %m");
586 if (ioctl (fd, TCSETXF, &tiox) < 0){
587 syslog (LOG_ERR, "TCSETXF: %m");
592 baud_rate = inspeed = baud_rate_of(speed);
597 * restore_tty - restore the terminal to the saved settings.
604 if (!default_device) {
606 * Turn off echoing, because otherwise we can get into
607 * a loop with the tty and the modem echoing to each other.
608 * We presume we are the sole user of this tty device, so
609 * when we close it, it will revert to its defaults anyway.
611 inittermios.c_lflag &= ~(ECHO | ECHONL);
613 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
614 if (!hungup && errno != ENXIO)
615 syslog(LOG_WARNING, "tcsetattr: %m");
617 if (ioctl (fd, TCSETXF, &inittermiox) < 0){
618 if (!hungup && errno != ENXIO)
619 syslog (LOG_ERR, "TCSETXF: %m");
622 ioctl(fd, TIOCSWINSZ, &wsinfo);
628 * setdtr - control the DTR line on the serial port.
629 * This is called from die(), so it shouldn't call die().
635 int modembits = TIOCM_DTR;
637 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
641 * open_loopback - open the device we use for getting packets
642 * in demand mode. Under Solaris 2, we use our existing fd
651 * output - Output PPP packet.
664 log_packet(p, len, "sent ");
667 data.buf = (caddr_t) p;
669 while (putmsg(pppfd, NULL, &data, 0) < 0) {
670 if (--retries < 0 || (errno != EWOULDBLOCK && errno != EAGAIN)) {
672 syslog(LOG_ERR, "Couldn't send packet: %m");
676 pfd.events = POLLOUT;
677 poll(&pfd, 1, 250); /* wait for up to 0.25 seconds */
683 * wait_input - wait until there is data available on fd,
684 * for the length of time specified by *timo (indefinite
689 struct timeval *timo;
694 t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
696 pfd.events = POLLIN | POLLPRI | POLLHUP;
697 if (poll(&pfd, 1, t) < 0 && errno != EINTR) {
698 syslog(LOG_ERR, "poll: %m");
704 * wait_loop_output - wait until there is data available on the
705 * loopback, for the length of time specified by *timo (indefinite
709 wait_loop_output(timo)
710 struct timeval *timo;
716 * wait_time - wait for a given length of time or until a
717 * signal is received.
721 struct timeval *timo;
725 n = select(0, NULL, NULL, NULL, timo);
726 if (n < 0 && errno != EINTR) {
727 syslog(LOG_ERR, "select: %m");
734 * read_packet - get a PPP packet from the serial device.
740 struct strbuf ctrl, data;
742 unsigned char ctrlbuf[sizeof(union DL_primitives) + 64];
745 data.maxlen = PPP_MRU + PPP_HDRLEN;
746 data.buf = (caddr_t) buf;
747 ctrl.maxlen = sizeof(ctrlbuf);
748 ctrl.buf = (caddr_t) ctrlbuf;
750 len = getmsg(pppfd, &ctrl, &data, &flags);
752 if (errno = EAGAIN || errno == EINTR)
754 syslog(LOG_ERR, "Error reading packet: %m");
762 * Got a M_PROTO or M_PCPROTO message. Interpret it
763 * as a DLPI primitive??
766 syslog(LOG_DEBUG, "got dlpi prim 0x%x, len=%d",
767 ((union DL_primitives *)ctrlbuf)->dl_primitive, ctrl.len);
773 * get_loop_output - get outgoing packets from the ppp device,
774 * and detect when we want to bring the real link up.
775 * Return value is 1 if we need to bring up the link, 0 otherwise.
783 while ((len = read_packet(inpacket_buf)) > 0) {
784 if (loop_frame(inpacket_buf, len))
791 * ppp_send_config - configure the transmit characteristics of
795 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
803 if (strioctl(pppfd, PPPIO_MTU, &mtu, sizeof(mtu), 0) < 0) {
804 if (hungup && errno == ENXIO)
806 syslog(LOG_ERR, "Couldn't set MTU: %m");
809 /* can't set these if we don't have a stream attached below /dev/ppp */
810 if (strioctl(pppfd, PPPIO_XACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
811 syslog(LOG_ERR, "Couldn't set transmit ACCM: %m");
813 cf[0] = (pcomp? COMP_PROT: 0) + (accomp? COMP_AC: 0);
814 cf[1] = COMP_PROT | COMP_AC;
815 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
816 syslog(LOG_ERR, "Couldn't set prot/AC compression: %m");
822 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
825 ppp_set_xaccm(unit, accm)
830 && strioctl(pppfd, PPPIO_XACCM, accm, sizeof(ext_accm), 0) < 0) {
831 if (!hungup || errno != ENXIO)
832 syslog(LOG_WARNING, "Couldn't set extended ACCM: %m");
837 * ppp_recv_config - configure the receive-side characteristics of
841 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
849 if (strioctl(pppfd, PPPIO_MRU, &mru, sizeof(mru), 0) < 0) {
850 if (hungup && errno == ENXIO)
852 syslog(LOG_ERR, "Couldn't set MRU: %m");
855 /* can't set these if we don't have a stream attached below /dev/ppp */
856 if (strioctl(pppfd, PPPIO_RACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
857 syslog(LOG_ERR, "Couldn't set receive ACCM: %m");
859 cf[0] = (pcomp? DECOMP_PROT: 0) + (accomp? DECOMP_AC: 0);
860 cf[1] = DECOMP_PROT | DECOMP_AC;
861 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
862 syslog(LOG_ERR, "Couldn't set prot/AC decompression: %m");
868 * ccp_test - ask kernel whether a given compression method
869 * is acceptable for use.
872 ccp_test(unit, opt_ptr, opt_len, for_transmit)
873 int unit, opt_len, for_transmit;
876 if (strioctl(pppfd, (for_transmit? PPPIO_XCOMP: PPPIO_RCOMP),
877 opt_ptr, opt_len, 0) >= 0)
879 return (errno == ENOSR)? 0: -1;
883 * ccp_flags_set - inform kernel about the current state of CCP.
886 ccp_flags_set(unit, isopen, isup)
887 int unit, isopen, isup;
891 cf[0] = (isopen? CCP_ISOPEN: 0) + (isup? CCP_ISUP: 0);
892 cf[1] = CCP_ISOPEN | CCP_ISUP | CCP_ERROR | CCP_FATALERROR;
893 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
894 if (!hungup || errno != ENXIO)
895 syslog(LOG_ERR, "Couldn't set kernel CCP state: %m");
900 * get_idle_time - return how long the link has been idle.
907 return strioctl(pppfd, PPPIO_GIDLE, ip, 0, sizeof(struct ppp_idle)) >= 0;
912 * set_filters - transfer the pass and active filters to the kernel.
915 set_filters(pass, active)
916 struct bpf_program *pass, *active;
920 if (pass->bf_len > 0) {
921 if (strioctl(pppfd, PPPIO_PASSFILT, pass,
922 sizeof(struct bpf_program), 0) < 0) {
923 syslog(LOG_ERR, "Couldn't set pass-filter in kernel: %m");
927 if (active->bf_len > 0) {
928 if (strioctl(pppfd, PPPIO_ACTIVEFILT, active,
929 sizeof(struct bpf_program), 0) < 0) {
930 syslog(LOG_ERR, "Couldn't set active-filter in kernel: %m");
939 * ccp_fatal_error - returns 1 if decompression was disabled as a
940 * result of an error detected after decompression of a packet,
941 * 0 otherwise. This is necessary because of patent nonsense.
944 ccp_fatal_error(unit)
950 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
951 if (errno != ENXIO && errno != EINVAL)
952 syslog(LOG_ERR, "Couldn't get compression flags: %m");
955 return cf[0] & CCP_FATALERROR;
959 * sifvjcomp - config tcp header compression
962 sifvjcomp(u, vjcomp, xcidcomp, xmaxcid)
963 int u, vjcomp, xcidcomp, xmaxcid;
969 maxcid[0] = xcidcomp;
970 maxcid[1] = 15; /* XXX should be rmaxcid */
971 if (strioctl(pppfd, PPPIO_VJINIT, maxcid, sizeof(maxcid), 0) < 0) {
972 syslog(LOG_ERR, "Couldn't initialize VJ compression: %m");
976 cf[0] = (vjcomp? COMP_VJC + DECOMP_VJC: 0) /* XXX this is wrong */
977 + (xcidcomp? COMP_VJCCID + DECOMP_VJCCID: 0);
978 cf[1] = COMP_VJC + DECOMP_VJC + COMP_VJCCID + DECOMP_VJCCID;
979 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
981 syslog(LOG_ERR, "Couldn't enable VJ compression: %m");
988 * sifup - Config the interface up and enable IP packets to pass.
996 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
997 if (ioctl(ipfd, SIOCGIFFLAGS, &ifr) < 0) {
998 syslog(LOG_ERR, "Couldn't mark interface up (get): %m");
1001 ifr.ifr_flags |= IFF_UP;
1002 if (ioctl(ipfd, SIOCSIFFLAGS, &ifr) < 0) {
1003 syslog(LOG_ERR, "Couldn't mark interface up (set): %m");
1011 * sifdown - Config the interface down and disable IP.
1021 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1022 if (ioctl(ipfd, SIOCGIFFLAGS, &ifr) < 0) {
1023 syslog(LOG_ERR, "Couldn't mark interface down (get): %m");
1026 ifr.ifr_flags &= ~IFF_UP;
1027 if (ioctl(ipfd, SIOCSIFFLAGS, &ifr) < 0) {
1028 syslog(LOG_ERR, "Couldn't mark interface down (set): %m");
1036 * sifnpmode - Set the mode for handling packets for a given NP.
1039 sifnpmode(u, proto, mode)
1047 npi[1] = (int) mode;
1048 if (strioctl(pppfd, PPPIO_NPMODE, &npi, 2 * sizeof(int), 0) < 0) {
1049 syslog(LOG_ERR, "ioctl(set NP %d mode to %d): %m", proto, mode);
1055 #define INET_ADDR(x) (((struct sockaddr_in *) &(x))->sin_addr.s_addr)
1058 * sifaddr - Config the interface IP addresses and netmask.
1067 memset(&ifr, 0, sizeof(ifr));
1068 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1069 ifr.ifr_addr.sa_family = AF_INET;
1070 INET_ADDR(ifr.ifr_addr) = m;
1071 if (ioctl(ipfd, SIOCSIFNETMASK, &ifr) < 0) {
1072 syslog(LOG_ERR, "Couldn't set IP netmask: %m");
1074 ifr.ifr_addr.sa_family = AF_INET;
1075 INET_ADDR(ifr.ifr_addr) = o;
1076 if (ioctl(ipfd, SIOCSIFADDR, &ifr) < 0) {
1077 syslog(LOG_ERR, "Couldn't set local IP address: %m");
1079 ifr.ifr_dstaddr.sa_family = AF_INET;
1080 INET_ADDR(ifr.ifr_dstaddr) = h;
1081 if (ioctl(ipfd, SIOCSIFDSTADDR, &ifr) < 0) {
1082 syslog(LOG_ERR, "Couldn't set remote IP address: %m");
1084 ifr.ifr_metric = link_mtu;
1085 if (ioctl(ipfd, SIOCSIFMTU, &ifr) < 0) {
1086 syslog(LOG_ERR, "Couldn't set IP MTU: %m");
1093 * cifaddr - Clear the interface IP addresses, and delete routes
1094 * through the interface if possible.
1103 if (ioctl(ipfd, I_UNLINK, ipmuxid) < 0) {
1104 syslog(LOG_ERR, "Can't remove ppp interface unit: %m");
1114 * sifdefaultroute - assign a default route through the address given.
1117 sifdefaultroute(u, g)
1123 memset(&rt, 0, sizeof(rt));
1124 rt.rt_dst.sa_family = AF_INET;
1125 INET_ADDR(rt.rt_dst) = 0;
1126 rt.rt_gateway.sa_family = AF_INET;
1127 INET_ADDR(rt.rt_gateway) = g;
1128 rt.rt_flags = RTF_GATEWAY;
1130 if (ioctl(ipfd, SIOCADDRT, &rt) < 0) {
1131 syslog(LOG_ERR, "Can't add default route: %m");
1135 default_route_gateway = g;
1140 * cifdefaultroute - delete a default route through the address given.
1143 cifdefaultroute(u, g)
1149 memset(&rt, 0, sizeof(rt));
1150 rt.rt_dst.sa_family = AF_INET;
1151 INET_ADDR(rt.rt_dst) = 0;
1152 rt.rt_gateway.sa_family = AF_INET;
1153 INET_ADDR(rt.rt_gateway) = g;
1154 rt.rt_flags = RTF_GATEWAY;
1156 if (ioctl(ipfd, SIOCDELRT, &rt) < 0) {
1157 syslog(LOG_ERR, "Can't delete default route: %m");
1161 default_route_gateway = 0;
1166 * sifproxyarp - Make a proxy ARP entry for the peer.
1169 sifproxyarp(unit, hisaddr)
1173 struct arpreq arpreq;
1175 memset(&arpreq, 0, sizeof(arpreq));
1176 if (!get_ether_addr(hisaddr, &arpreq.arp_ha))
1179 arpreq.arp_pa.sa_family = AF_INET;
1180 INET_ADDR(arpreq.arp_pa) = hisaddr;
1181 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1182 if (ioctl(ipfd, SIOCSARP, (caddr_t) &arpreq) < 0) {
1183 syslog(LOG_ERR, "Couldn't set proxy ARP entry: %m");
1187 proxy_arp_addr = hisaddr;
1192 * cifproxyarp - Delete the proxy ARP entry for the peer.
1195 cifproxyarp(unit, hisaddr)
1199 struct arpreq arpreq;
1201 memset(&arpreq, 0, sizeof(arpreq));
1202 arpreq.arp_pa.sa_family = AF_INET;
1203 INET_ADDR(arpreq.arp_pa) = hisaddr;
1204 if (ioctl(ipfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1205 syslog(LOG_ERR, "Couldn't delete proxy ARP entry: %m");
1214 * get_ether_addr - get the hardware address of an interface on the
1215 * the same subnet as ipaddr.
1220 get_ether_addr(ipaddr, hwaddr)
1222 struct sockaddr *hwaddr;
1224 struct ifreq *ifr, *ifend, ifreq;
1227 u_int32_t ina, mask;
1230 * Scan through the system's network interfaces.
1233 if (ioctl(ipfd, SIOCGIFNUM, &nif) < 0)
1236 ifc.ifc_len = nif * sizeof(struct ifreq);
1237 ifc.ifc_buf = (caddr_t) alloca(ifc.ifc_len);
1238 if (ifc.ifc_req == 0)
1240 if (ioctl(ipfd, SIOCGIFCONF, &ifc) < 0) {
1241 syslog(LOG_WARNING, "Couldn't get system interface list: %m");
1244 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1245 for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
1246 if (ifr->ifr_addr.sa_family != AF_INET)
1249 * Check that the interface is up, and not point-to-point or loopback.
1251 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1252 if (ioctl(ipfd, SIOCGIFFLAGS, &ifreq) < 0)
1254 if ((ifreq.ifr_flags &
1255 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1256 != (IFF_UP|IFF_BROADCAST))
1259 * Get its netmask and check that it's on the right subnet.
1261 if (ioctl(ipfd, SIOCGIFNETMASK, &ifreq) < 0)
1263 ina = INET_ADDR(ifr->ifr_addr);
1264 mask = INET_ADDR(ifreq.ifr_addr);
1265 if ((ipaddr & mask) == (ina & mask))
1270 syslog(LOG_WARNING, "No suitable interface found for proxy ARP");
1274 syslog(LOG_INFO, "found interface %s for proxy ARP", ifr->ifr_name);
1275 if (!get_hw_addr(ifr->ifr_name, hwaddr)) {
1276 syslog(LOG_ERR, "Couldn't get hardware address for %s", ifr->ifr_name);
1284 * get_hw_addr - obtain the hardware address for a named interface.
1287 get_hw_addr(name, hwaddr)
1289 struct sockaddr *hwaddr;
1292 int unit, iffd, adrlen;
1293 unsigned char *adrp;
1296 union DL_primitives prim;
1301 * We have to open the device and ask it for its hardware address.
1302 * First split apart the device name and unit.
1304 strcpy(ifdev, "/dev/");
1305 q = ifdev + 5; /* strlen("/dev/") */
1306 while (*name != 0 && !isdigit(*name))
1312 * Open the device and do a DLPI attach and phys_addr_req.
1314 iffd = open(ifdev, O_RDWR);
1316 syslog(LOG_ERR, "Can't open %s: %m", ifdev);
1319 if (dlpi_attach(iffd, unit) < 0
1320 || dlpi_get_reply(iffd, &reply.prim, DL_OK_ACK, sizeof(reply)) < 0
1321 || dlpi_info_req(iffd) < 0
1322 || dlpi_get_reply(iffd, &reply.prim, DL_INFO_ACK, sizeof(reply)) < 0) {
1327 adrlen = reply.prim.info_ack.dl_addr_length;
1328 adrp = (unsigned char *)&reply + reply.prim.info_ack.dl_addr_offset;
1329 #if DL_CURRENT_VERSION >= 2
1330 if (reply.prim.info_ack.dl_sap_length < 0)
1331 adrlen += reply.prim.info_ack.dl_sap_length;
1333 adrp += reply.prim.info_ack.dl_sap_length;
1335 hwaddr->sa_family = AF_UNSPEC;
1336 memcpy(hwaddr->sa_data, adrp, adrlen);
1342 dlpi_attach(fd, ppa)
1345 dl_attach_req_t req;
1348 req.dl_primitive = DL_ATTACH_REQ;
1350 buf.len = sizeof(req);
1351 buf.buf = (void *) &req;
1352 return putmsg(fd, &buf, NULL, RS_HIPRI);
1362 req.dl_primitive = DL_INFO_REQ;
1363 buf.len = sizeof(req);
1364 buf.buf = (void *) &req;
1365 return putmsg(fd, &buf, NULL, RS_HIPRI);
1369 dlpi_get_reply(fd, reply, expected_prim, maxlen)
1370 union DL_primitives *reply;
1371 int fd, expected_prim, maxlen;
1378 * Use poll to wait for a message with a timeout.
1381 pfd.events = POLLIN | POLLPRI;
1383 n = poll(&pfd, 1, 1000);
1384 } while (n == -1 && errno == EINTR);
1391 buf.maxlen = maxlen;
1392 buf.buf = (void *) reply;
1394 if (getmsg(fd, &buf, NULL, &flags) < 0)
1397 if (buf.len < sizeof(ulong)) {
1399 syslog(LOG_DEBUG, "dlpi response short (len=%d)\n", buf.len);
1403 if (reply->dl_primitive == expected_prim)
1407 if (reply->dl_primitive == DL_ERROR_ACK) {
1408 syslog(LOG_DEBUG, "dlpi error %d (unix errno %d) for prim %x\n",
1409 reply->error_ack.dl_errno, reply->error_ack.dl_unix_errno,
1410 reply->error_ack.dl_error_primitive);
1412 syslog(LOG_DEBUG, "dlpi unexpected response prim %x\n",
1413 reply->dl_primitive);
1421 * Return user specified netmask, modified by any mask we might determine
1422 * for address `addr' (in network byte order).
1423 * Here we scan through the system's list of interfaces, looking for
1424 * any non-point-to-point interfaces which might appear to be on the same
1425 * network as `addr'. If we find any, we OR in their netmask to the
1426 * user-specified netmask.
1432 u_int32_t mask, nmask, ina;
1433 struct ifreq *ifr, *ifend, ifreq;
1438 if (IN_CLASSA(addr)) /* determine network mask for address class */
1439 nmask = IN_CLASSA_NET;
1440 else if (IN_CLASSB(addr))
1441 nmask = IN_CLASSB_NET;
1443 nmask = IN_CLASSC_NET;
1444 /* class D nets are disallowed by bad_ip_adrs */
1445 mask = netmask | htonl(nmask);
1448 * Scan through the system's network interfaces.
1451 if (ioctl(ipfd, SIOCGIFNUM, &nif) < 0)
1454 ifc.ifc_len = nif * sizeof(struct ifreq);
1455 ifc.ifc_buf = (caddr_t) alloca(ifc.ifc_len);
1456 if (ifc.ifc_req == 0)
1458 if (ioctl(ipfd, SIOCGIFCONF, &ifc) < 0) {
1459 syslog(LOG_WARNING, "Couldn't get system interface list: %m");
1462 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1463 for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
1465 * Check the interface's internet address.
1467 if (ifr->ifr_addr.sa_family != AF_INET)
1469 ina = INET_ADDR(ifr->ifr_addr);
1470 if ((ntohl(ina) & nmask) != (addr & nmask))
1473 * Check that the interface is up, and not point-to-point or loopback.
1475 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1476 if (ioctl(ipfd, SIOCGIFFLAGS, &ifreq) < 0)
1478 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1482 * Get its netmask and OR it into our mask.
1484 if (ioctl(ipfd, SIOCGIFNETMASK, &ifreq) < 0)
1486 mask |= INET_ADDR(ifreq.ifr_addr);
1493 * logwtmp - write an accounting record to the /var/adm/wtmp file.
1496 logwtmp(line, name, host)
1497 char *line, *name, *host;
1499 static struct utmpx utmpx;
1503 strncpy(utmpx.ut_user, name, sizeof(utmpx.ut_user));
1504 strncpy(utmpx.ut_id, ifname, sizeof(utmpx.ut_id));
1505 strncpy(utmpx.ut_line, line, sizeof(utmpx.ut_line));
1506 utmpx.ut_pid = getpid();
1507 utmpx.ut_type = USER_PROCESS;
1509 utmpx.ut_type = DEAD_PROCESS;
1511 gettimeofday(&utmpx.ut_tv, NULL);
1512 updwtmpx("/var/adm/wtmpx", &utmpx);
1517 * gethostid - return the serial number of this machine.
1524 if (sysinfo(SI_HW_SERIAL, buf, sizeof(buf)) < 0) {
1525 syslog(LOG_ERR, "sysinfo: %m");
1528 return (int) strtoul(buf, NULL, 16);
1532 strioctl(fd, cmd, ptr, ilen, olen)
1533 int fd, cmd, ilen, olen;
1536 struct strioctl str;
1542 if (ioctl(fd, I_STR, &str) == -1)
1544 if (str.ic_len != olen)
1545 syslog(LOG_DEBUG, "strioctl: expected %d bytes, got %d for cmd %x\n",
1546 olen, str.ic_len, cmd);
1551 * lock - create a lock file for the named lock device
1554 #define LOCK_PREFIX "/var/spool/locks/LK."
1555 static char lock_file[40]; /* name of lock file created */
1565 if (stat(dev, &sbuf) < 0) {
1566 syslog(LOG_ERR, "Can't get device number for %s: %m", dev);
1569 if ((sbuf.st_mode & S_IFMT) != S_IFCHR) {
1570 syslog(LOG_ERR, "Can't lock %s: not a character device", dev);
1573 sprintf(lock_file, "%s%03d.%03d.%03d", LOCK_PREFIX, major(sbuf.st_dev),
1574 major(sbuf.st_rdev), minor(sbuf.st_rdev));
1576 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1578 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1579 /* Read the lock file to find out who has the device locked */
1580 n = read(fd, ascii_pid, 11);
1582 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1586 pid = atoi(ascii_pid);
1587 if (pid > 0 && kill(pid, 0) == -1 && errno == ESRCH) {
1588 /* pid no longer exists - remove the lock file */
1589 if (unlink(lock_file) == 0) {
1591 syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1595 syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
1598 syslog(LOG_NOTICE, "Device %s is locked by pid %d",
1603 syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1608 sprintf(ascii_pid, "%10d\n", getpid());
1609 write(fd, ascii_pid, 11);
1616 * unlock - remove our lockfile