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.5 1995/08/11 02:36:24 paulus Exp $";
41 #include <sys/types.h>
42 #include <sys/ioccom.h>
43 #include <sys/stream.h>
44 #include <sys/stropts.h>
45 #include <sys/socket.h>
46 #include <sys/sockio.h>
47 #include <sys/syslog.h>
48 #include <sys/systeminfo.h>
52 #include <net/if_arp.h>
53 #include <net/route.h>
54 #include <net/ppp_defs.h>
55 #include <net/pppio.h>
56 #include <netinet/in.h>
61 static int fdmuxid = -1;
63 static int ipmuxid = -1;
65 static int restore_term;
66 static struct termios inittermios;
67 static struct winsize wsinfo; /* Initial window size info */
68 static pid_t tty_sid; /* original session ID for terminal */
70 static int link_mtu, link_mru;
73 static int tty_nmodules;
74 static char tty_modules[NMODULES][FMNAMESZ+1];
77 * sys_init - System-dependent initialization.
82 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
83 setlogmask(LOG_UPTO(LOG_INFO));
85 setlogmask(LOG_UPTO(LOG_DEBUG));
87 ipfd = open("/dev/ip", O_RDWR, 0);
89 syslog(LOG_ERR, "Couldn't open IP device: %m");
95 * daemon - Detach us from controlling terminal session.
98 daemon(nochdir, noclose)
103 if ((pid = fork()) < 0)
106 exit(0); /* parent dies */
111 fclose(stdin); /* don't need stdin, stdout, stderr */
119 * note_debug_level - note a change in the debug level.
125 syslog(LOG_INFO, "Debug turned ON, Level %d", debug);
126 setlogmask(LOG_UPTO(LOG_DEBUG));
128 setlogmask(LOG_UPTO(LOG_WARNING));
133 * ppp_available - check whether the system has any ppp interfaces
140 return stat("/dev/ppp", &buf) >= 0;
144 * establish_ppp - Turn the serial port into a ppp interface.
152 tty_sid = getsid((pid_t)0);
154 pppfd = open("/dev/ppp", O_RDWR | O_NONBLOCK, 0);
156 syslog(LOG_ERR, "Can't open /dev/ppp: %m");
160 /* Assign a new PPA and get its unit number. */
161 if (strioctl(pppfd, PPPIO_NEWPPA, &ifunit, 0, sizeof(int)) < 0) {
162 syslog(LOG_ERR, "Can't create new PPP interface: %m");
167 * Open the ppp device again and link it under the ip multiplexor.
168 * IP will assign a unit number which hopefully is the same as ifunit.
169 * I don't know any way to be certain they will be the same. :-(
171 ifd = open("/dev/ppp", O_RDWR, 0);
173 syslog(LOG_ERR, "Can't open /dev/ppp (2): %m");
176 if (ioctl(ifd, I_PUSH, "ip") < 0) {
177 syslog(LOG_ERR, "Can't push IP module: %m");
181 ipmuxid = ioctl(ipfd, I_LINK, ifd);
184 syslog(LOG_ERR, "Can't link PPP device to IP: %m");
188 /* Pop any existing modules off the tty stream. */
190 if (ioctl(fd, I_LOOK, tty_modules[i]) < 0
191 || ioctl(fd, I_POP, 0) < 0)
195 /* Push the async hdlc module and the compressor module. */
196 if (ioctl(fd, I_PUSH, "ppp_ahdl") < 0) {
197 syslog(LOG_ERR, "Couldn't push PPP Async HDLC module: %m");
200 if (ioctl(fd, I_PUSH, "ppp_comp") < 0) {
201 syslog(LOG_ERR, "Couldn't push PPP compression module: %m");
205 /* Link the serial port under the PPP multiplexor. */
206 if ((fdmuxid = ioctl(pppfd, I_LINK, fd)) < 0) {
207 syslog(LOG_ERR, "Can't link tty to PPP mux: %m");
213 * disestablish_ppp - Restore the serial port to normal operation.
214 * This shouldn't call die() because it's called from die().
222 if (ioctl(pppfd, I_UNLINK, fdmuxid) < 0) {
224 syslog(LOG_ERR, "Can't unlink tty from PPP mux: %m");
229 while (ioctl(fd, I_POP, 0) >= 0)
231 for (i = tty_nmodules - 1; i >= 0; --i)
232 if (ioctl(fd, I_PUSH, tty_modules[i]) < 0)
233 syslog(LOG_ERR, "Couldn't restore tty module %s: %m",
236 if (hungup && default_device && tty_sid > 0) {
238 * If we have received a hangup, we need to send a SIGHUP
239 * to the terminal's controlling process. The reason is
240 * that the original stream head for the terminal hasn't
241 * seen the M_HANGUP message (it went up through the ppp
242 * driver to the stream head for our fd to /dev/ppp).
244 kill(tty_sid, SIGHUP);
250 * List of valid speeds.
253 int speed_int, speed_val;
325 * Translate from bits/second to a speed_t.
331 struct speed *speedp;
335 for (speedp = speeds; speedp->speed_int; speedp++)
336 if (bps == speedp->speed_int)
337 return speedp->speed_val;
338 syslog(LOG_WARNING, "speed %d not supported", bps);
343 * Translate from a speed_t to bits/second.
349 struct speed *speedp;
353 for (speedp = speeds; speedp->speed_int; speedp++)
354 if (speed == speedp->speed_val)
355 return speedp->speed_int;
360 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
361 * at the requested speed, etc. If `local' is true, set CLOCAL
362 * regardless of whether the modem option was specified.
364 set_up_tty(fd, local)
370 if (tcgetattr(fd, &tios) < 0) {
371 syslog(LOG_ERR, "tcgetattr: %m");
377 ioctl(fd, TIOCGWINSZ, &wsinfo);
380 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
382 tios.c_cflag |= CRTSCTS;
383 else if (crtscts < 0)
384 tios.c_cflag &= ~CRTSCTS;
386 tios.c_cflag |= CS8 | CREAD | HUPCL;
388 tios.c_cflag |= CLOCAL;
389 tios.c_iflag = IGNBRK | IGNPAR;
393 tios.c_cc[VTIME] = 0;
396 tios.c_iflag |= IXOFF;
397 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
398 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
401 speed = translate_speed(inspeed);
403 cfsetospeed(&tios, speed);
404 cfsetispeed(&tios, speed);
406 speed = cfgetospeed(&tios);
408 * We can't proceed if the serial port speed is 0,
409 * since that implies that the serial port is disabled.
412 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
418 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
419 syslog(LOG_ERR, "tcsetattr: %m");
423 baud_rate = inspeed = baud_rate_of(speed);
428 * restore_tty - restore the terminal to the saved settings.
434 if (!default_device) {
436 * Turn off echoing, because otherwise we can get into
437 * a loop with the tty and the modem echoing to each other.
438 * We presume we are the sole user of this tty device, so
439 * when we close it, it will revert to its defaults anyway.
441 inittermios.c_lflag &= ~(ECHO | ECHONL);
443 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
444 if (!hungup && errno != ENXIO)
445 syslog(LOG_WARNING, "tcsetattr: %m");
446 ioctl(fd, TIOCSWINSZ, &wsinfo);
452 * setdtr - control the DTR line on the serial port.
453 * This is called from die(), so it shouldn't call die().
458 int modembits = TIOCM_DTR;
460 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
464 * output - Output PPP packet.
475 log_packet(p, len, "sent ");
478 data.buf = (caddr_t) p;
479 if (putmsg(pppfd, NULL, &data, 0) < 0) {
480 if (errno != ENXIO) {
481 syslog(LOG_ERR, "Couldn't send packet: %m");
488 * wait_input - wait until there is data available on fd,
489 * for the length of time specified by *timo (indefinite
493 struct timeval *timo;
498 t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
500 pfd.events = POLLIN | POLLPRI | POLLHUP;
501 if (poll(&pfd, 1, t) < 0 && errno != EINTR) {
502 syslog(LOG_ERR, "poll: %m");
508 * read_packet - get a PPP packet from the serial device.
514 struct strbuf ctrl, data;
516 unsigned char ctrlbuf[sizeof(union DL_primitives) + 64];
519 data.maxlen = PPP_MRU + PPP_HDRLEN;
520 data.buf = (caddr_t) buf;
521 ctrl.maxlen = sizeof(ctrlbuf);
522 ctrl.buf = (caddr_t) ctrlbuf;
524 len = getmsg(pppfd, &ctrl, &data, &flags);
526 if (errno = EAGAIN || errno == EINTR)
528 syslog(LOG_ERR, "Error reading packet: %m");
536 * Got a M_PROTO or M_PCPROTO message. Interpret it
537 * as a DLPI primitive.
540 syslog(LOG_DEBUG, "got dlpi prim 0x%x, len=%d",
541 ((union DL_primitives *)ctrlbuf)->dl_primitive, ctrl.len);
547 * ppp_send_config - configure the transmit characteristics of
551 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
559 if (strioctl(pppfd, PPPIO_MTU, &mtu, sizeof(mtu), 0) < 0) {
560 if (hungup && errno == ENXIO)
562 syslog(LOG_ERR, "Couldn't set MTU: %m");
564 if (strioctl(pppfd, PPPIO_XACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
565 syslog(LOG_ERR, "Couldn't set transmit ACCM: %m");
567 cf[0] = (pcomp? COMP_PROT: 0) + (accomp? COMP_AC: 0);
568 cf[1] = COMP_PROT | COMP_AC;
569 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
570 syslog(LOG_ERR, "Couldn't set prot/AC compression: %m");
575 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
578 ppp_set_xaccm(unit, accm)
582 if (strioctl(pppfd, PPPIO_XACCM, accm, sizeof(ext_accm), 0) < 0) {
583 if (!hungup || errno != ENXIO)
584 syslog(LOG_WARNING, "Couldn't set extended ACCM: %m");
589 * ppp_recv_config - configure the receive-side characteristics of
593 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
601 if (strioctl(pppfd, PPPIO_MRU, &mru, sizeof(mru), 0) < 0) {
602 if (hungup && errno == ENXIO)
604 syslog(LOG_ERR, "Couldn't set MRU: %m");
606 if (strioctl(pppfd, PPPIO_RACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
607 syslog(LOG_ERR, "Couldn't set receive ACCM: %m");
609 cf[0] = (pcomp? DECOMP_PROT: 0) + (accomp? DECOMP_AC: 0);
610 cf[1] = DECOMP_PROT | DECOMP_AC;
611 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
612 syslog(LOG_ERR, "Couldn't set prot/AC decompression: %m");
617 * ccp_test - ask kernel whether a given compression method
618 * is acceptable for use.
620 ccp_test(unit, opt_ptr, opt_len, for_transmit)
621 int unit, opt_len, for_transmit;
624 return strioctl(pppfd, (for_transmit? PPPIO_XCOMP: PPPIO_RCOMP),
625 opt_ptr, opt_len, 0) >= 0;
629 * ccp_flags_set - inform kernel about the current state of CCP.
632 ccp_flags_set(unit, isopen, isup)
633 int unit, isopen, isup;
637 cf[0] = (isopen? CCP_ISOPEN: 0) + (isup? CCP_ISUP: 0);
638 cf[1] = CCP_ISOPEN | CCP_ISUP | CCP_ERROR | CCP_FATALERROR;
639 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
640 if (!hungup || errno != ENXIO)
641 syslog(LOG_ERR, "Couldn't set kernel CCP state: %m");
646 * ccp_fatal_error - returns 1 if decompression was disabled as a
647 * result of an error detected after decompression of a packet,
648 * 0 otherwise. This is necessary because of patent nonsense.
651 ccp_fatal_error(unit)
657 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
658 if (errno != ENXIO && errno != EINVAL)
659 syslog(LOG_ERR, "Couldn't get compression flags: %m");
662 return cf[0] & CCP_FATALERROR;
666 * sifvjcomp - config tcp header compression
669 sifvjcomp(u, vjcomp, xcidcomp, xmaxcid)
670 int u, vjcomp, xcidcomp, xmaxcid;
676 maxcid[0] = xcidcomp;
677 maxcid[1] = 15; /* XXX should be rmaxcid */
678 if (strioctl(pppfd, PPPIO_VJINIT, maxcid, sizeof(maxcid), 0) < 0) {
679 syslog(LOG_ERR, "Couldn't initialize VJ compression: %m");
683 cf[0] = (vjcomp? COMP_VJC + DECOMP_VJC: 0) /* XXX this is wrong */
684 + (xcidcomp? COMP_VJCCID + DECOMP_VJCCID: 0);
685 cf[1] = COMP_VJC + DECOMP_VJC + COMP_VJCCID + DECOMP_VJCCID;
686 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
688 syslog(LOG_ERR, "Couldn't enable VJ compression: %m");
695 * sifup - Config the interface up and enable IP packets to pass.
703 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
704 if (ioctl(ipfd, SIOCGIFFLAGS, &ifr) < 0) {
705 syslog(LOG_ERR, "Couldn't mark interface up (get): %m");
708 ifr.ifr_flags |= IFF_UP;
709 if (ioctl(ipfd, SIOCSIFFLAGS, &ifr) < 0) {
710 syslog(LOG_ERR, "Couldn't mark interface up (set): %m");
717 * sifdown - Config the interface down and disable IP.
727 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
728 if (ioctl(ipfd, SIOCGIFFLAGS, &ifr) < 0) {
729 syslog(LOG_ERR, "Couldn't mark interface down (get): %m");
732 ifr.ifr_flags &= ~IFF_UP;
733 if (ioctl(ipfd, SIOCSIFFLAGS, &ifr) < 0) {
734 syslog(LOG_ERR, "Couldn't mark interface down (set): %m");
740 #define INET_ADDR(x) (((struct sockaddr_in *) &(x))->sin_addr.s_addr)
743 * sifaddr - Config the interface IP addresses and netmask.
752 memset(&ifr, 0, sizeof(ifr));
753 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
754 ifr.ifr_addr.sa_family = AF_INET;
755 INET_ADDR(ifr.ifr_addr) = o;
756 if (ioctl(ipfd, SIOCSIFADDR, &ifr) < 0) {
757 syslog(LOG_ERR, "Couldn't set local IP address: %m");
759 ifr.ifr_dstaddr.sa_family = AF_INET;
760 INET_ADDR(ifr.ifr_dstaddr) = h;
761 if (ioctl(ipfd, SIOCSIFDSTADDR, &ifr) < 0) {
762 syslog(LOG_ERR, "Couldn't set remote IP address: %m");
764 ifr.ifr_addr.sa_family = AF_INET;
765 INET_ADDR(ifr.ifr_addr) = m;
766 if (ioctl(ipfd, SIOCSIFNETMASK, &ifr) < 0) {
767 syslog(LOG_ERR, "Couldn't set IP netmask: %m");
769 ifr.ifr_metric = link_mtu;
770 if (ioctl(ipfd, SIOCSIFMTU, &ifr) < 0) {
771 syslog(LOG_ERR, "Couldn't set IP MTU: %m");
778 * cifaddr - Clear the interface IP addresses, and delete routes
779 * through the interface if possible.
788 if (ioctl(ipfd, I_UNLINK, ipmuxid) < 0) {
789 syslog(LOG_ERR, "Can't remove ppp interface unit: %m");
799 * sifdefaultroute - assign a default route through the address given.
802 sifdefaultroute(u, g)
808 rt.rt_dst.sa_family = AF_INET;
809 INET_ADDR(rt.rt_dst) = 0;
810 rt.rt_gateway.sa_family = AF_INET;
811 INET_ADDR(rt.rt_gateway) = g;
812 rt.rt_flags = RTF_GATEWAY;
814 if (ioctl(ipfd, SIOCADDRT, &rt) < 0) {
815 syslog(LOG_ERR, "Can't add default route: %m");
823 * cifdefaultroute - delete a default route through the address given.
826 cifdefaultroute(u, g)
832 rt.rt_dst.sa_family = AF_INET;
833 INET_ADDR(rt.rt_dst) = 0;
834 rt.rt_gateway.sa_family = AF_INET;
835 INET_ADDR(rt.rt_gateway) = g;
836 rt.rt_flags = RTF_GATEWAY;
838 if (ioctl(ipfd, SIOCDELRT, &rt) < 0) {
839 syslog(LOG_ERR, "Can't delete default route: %m");
847 * sifproxyarp - Make a proxy ARP entry for the peer.
850 sifproxyarp(unit, hisaddr)
854 struct arpreq arpreq;
856 memset(&arpreq, 0, sizeof(arpreq));
857 if (!get_ether_addr(hisaddr, &arpreq.arp_ha))
860 arpreq.arp_pa.sa_family = AF_INET;
861 INET_ADDR(arpreq.arp_pa) = hisaddr;
862 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
863 if (ioctl(ipfd, SIOCSARP, (caddr_t) &arpreq) < 0) {
864 syslog(LOG_ERR, "Couldn't set proxy ARP entry: %m");
872 * cifproxyarp - Delete the proxy ARP entry for the peer.
875 cifproxyarp(unit, hisaddr)
879 struct arpreq arpreq;
881 memset(&arpreq, 0, sizeof(arpreq));
882 arpreq.arp_pa.sa_family = AF_INET;
883 INET_ADDR(arpreq.arp_pa) = hisaddr;
884 if (ioctl(ipfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
885 syslog(LOG_ERR, "Couldn't delete proxy ARP entry: %m");
893 * get_ether_addr - get the hardware address of an interface on the
894 * the same subnet as ipaddr.
899 get_ether_addr(ipaddr, hwaddr)
901 struct sockaddr *hwaddr;
903 struct ifreq *ifr, *ifend, ifreq;
909 * Scan through the system's network interfaces.
911 if (ioctl(ipfd, SIOCGIFNUM, &nif) < 0)
913 ifc.ifc_len = nif * sizeof(struct ifreq);
914 ifc.ifc_req = alloca(ifc.ifc_len);
915 if (ifc.ifc_req == 0)
917 if (ioctl(ipfd, SIOCGIFCONF, &ifc) < 0) {
918 syslog(LOG_WARNING, "Couldn't get system interface list: %m");
921 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
922 for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
923 if (ifr->ifr_addr.sa_family != AF_INET)
926 * Check that the interface is up, and not point-to-point or loopback.
928 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
929 if (ioctl(ipfd, SIOCGIFFLAGS, &ifreq) < 0)
931 if ((ifreq.ifr_flags &
932 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
933 != (IFF_UP|IFF_BROADCAST))
936 * Get its netmask and check that it's on the right subnet.
938 if (ioctl(ipfd, SIOCGIFNETMASK, &ifreq) < 0)
940 ina = INET_ADDR(ifr->ifr_addr);
941 mask = INET_ADDR(ifreq.ifr_addr);
942 if ((ipaddr & mask) == (ina & mask))
947 syslog(LOG_WARNING, "No suitable interface found for proxy ARP");
951 syslog(LOG_INFO, "found interface %s for proxy ARP", ifr->ifr_name);
952 if (!get_hw_addr(ifr->ifr_name, hwaddr)) {
953 syslog(LOG_ERR, "Couldn't get hardware address for %s", ifr->ifr_name);
961 * get_hw_addr - obtain the hardware address for a named interface.
964 get_hw_addr(name, hwaddr)
966 struct sockaddr *hwaddr;
969 int unit, iffd, adrlen;
973 union DL_primitives prim;
978 * We have to open the device and ask it for its hardware address.
979 * First split apart the device name and unit.
981 strcpy(ifdev, "/dev/");
982 q = ifdev + 5; /* strlen("/dev/") */
983 while (*name != 0 && !isdigit(*name))
989 * Open the device and do a DLPI attach and phys_addr_req.
991 iffd = open(ifdev, O_RDWR);
993 syslog(LOG_ERR, "Can't open %s: %m", ifdev);
996 if (dlpi_attach(iffd, unit) < 0
997 || dlpi_get_reply(iffd, &reply.prim, DL_OK_ACK, sizeof(reply)) < 0
998 || dlpi_phys_addr_req(iffd) < 0
999 || dlpi_get_reply(iffd, &reply.prim, DL_PHYS_ADDR_ACK,
1000 sizeof(reply)) < 0) {
1005 hwaddr->sa_family = AF_UNSPEC;
1006 adrlen = reply.prim.physaddr_ack.dl_addr_length;
1007 adrp = (unsigned char *)&reply + reply.prim.physaddr_ack.dl_addr_offset;
1008 memcpy(hwaddr->sa_data, adrp, adrlen);
1014 dlpi_attach(fd, ppa)
1017 dl_attach_req_t req;
1020 req.dl_primitive = DL_ATTACH_REQ;
1022 buf.len = sizeof(req);
1023 buf.buf = (void *) &req;
1024 return putmsg(fd, &buf, NULL, RS_HIPRI);
1028 dlpi_phys_addr_req(fd)
1031 dl_phys_addr_req_t req;
1034 req.dl_primitive = DL_PHYS_ADDR_REQ;
1035 req.dl_addr_type = DL_CURR_PHYS_ADDR;
1036 buf.len = sizeof(req);
1037 buf.buf = (void *) &req;
1038 return putmsg(fd, &buf, NULL, RS_HIPRI);
1042 dlpi_get_reply(fd, reply, expected_prim, maxlen)
1043 union DL_primitives *reply;
1044 int fd, expected_prim, maxlen;
1051 * Use poll to wait for a message with a timeout.
1054 pfd.events = POLLIN | POLLPRI;
1056 n = poll(&pfd, 1, 1000);
1057 } while (n == -1 && errno == EINTR);
1064 buf.maxlen = maxlen;
1065 buf.buf = (void *) reply;
1067 if (getmsg(fd, &buf, NULL, &flags) < 0)
1070 if (buf.len < sizeof(ulong)) {
1072 syslog(LOG_DEBUG, "dlpi response short (len=%d)\n", buf.len);
1076 if (reply->dl_primitive == expected_prim)
1080 if (reply->dl_primitive == DL_ERROR_ACK) {
1081 syslog(LOG_DEBUG, "dlpi error %d (unix errno %d) for prim %x\n",
1082 reply->error_ack.dl_errno, reply->error_ack.dl_unix_errno,
1083 reply->error_ack.dl_error_primitive);
1085 syslog(LOG_DEBUG, "dlpi unexpected response prim %x\n",
1086 reply->dl_primitive);
1094 * Return user specified netmask, modified by any mask we might determine
1095 * for address `addr' (in network byte order).
1096 * Here we scan through the system's list of interfaces, looking for
1097 * any non-point-to-point interfaces which might appear to be on the same
1098 * network as `addr'. If we find any, we OR in their netmask to the
1099 * user-specified netmask.
1105 u_int32_t mask, nmask, ina;
1106 struct ifreq *ifr, *ifend, ifreq;
1111 if (IN_CLASSA(addr)) /* determine network mask for address class */
1112 nmask = IN_CLASSA_NET;
1113 else if (IN_CLASSB(addr))
1114 nmask = IN_CLASSB_NET;
1116 nmask = IN_CLASSC_NET;
1117 /* class D nets are disallowed by bad_ip_adrs */
1118 mask = netmask | htonl(nmask);
1121 * Scan through the system's network interfaces.
1123 if (ioctl(ipfd, SIOCGIFNUM, &nif) < 0)
1125 ifc.ifc_len = nif * sizeof(struct ifreq);
1126 ifc.ifc_req = alloca(ifc.ifc_len);
1127 if (ifc.ifc_req == 0)
1129 if (ioctl(ipfd, SIOCGIFCONF, &ifc) < 0) {
1130 syslog(LOG_WARNING, "Couldn't get system interface list: %m");
1133 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1134 for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
1136 * Check the interface's internet address.
1138 if (ifr->ifr_addr.sa_family != AF_INET)
1140 ina = INET_ADDR(ifr->ifr_addr);
1141 if ((ntohl(ina) & nmask) != (addr & nmask))
1144 * Check that the interface is up, and not point-to-point or loopback.
1146 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1147 if (ioctl(ipfd, SIOCGIFFLAGS, &ifreq) < 0)
1149 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1153 * Get its netmask and OR it into our mask.
1155 if (ioctl(ipfd, SIOCGIFNETMASK, &ifreq) < 0)
1157 mask |= INET_ADDR(ifreq.ifr_addr);
1164 * logwtmp - write an accounting record to the /var/adm/wtmp file.
1167 logwtmp(line, name, host)
1168 char *line, *name, *host;
1173 * gethostid - return the serial number of this machine.
1180 if (sysinfo(SI_HW_SERIAL, buf, sizeof(buf)) < 0) {
1181 syslog(LOG_ERR, "sysinfo: %m");
1184 return (int) strtoul(buf, NULL, 16);
1188 strioctl(fd, cmd, ptr, ilen, olen)
1189 int fd, cmd, ilen, olen;
1192 struct strioctl str;
1198 if (ioctl(fd, I_STR, &str) == -1)
1200 if (str.ic_len != olen)
1201 syslog(LOG_DEBUG, "strioctl: expected %d bytes, got %d for cmd %x\n",
1202 olen, str.ic_len, cmd);