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.4 1995/08/10 06:53:39 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 pid_t tty_sid; /* original session ID for terminal */
69 static int link_mtu, link_mru;
72 static int tty_nmodules;
73 static char tty_modules[NMODULES][FMNAMESZ+1];
76 * sys_init - System-dependent initialization.
81 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
82 setlogmask(LOG_UPTO(LOG_INFO));
84 setlogmask(LOG_UPTO(LOG_DEBUG));
86 ipfd = open("/dev/ip", O_RDWR, 0);
88 syslog(LOG_ERR, "Couldn't open IP device: %m");
94 * daemon - Detach us from controlling terminal session.
97 daemon(nochdir, noclose)
102 if ((pid = fork()) < 0)
105 exit(0); /* parent dies */
110 fclose(stdin); /* don't need stdin, stdout, stderr */
118 * note_debug_level - note a change in the debug level.
124 syslog(LOG_INFO, "Debug turned ON, Level %d", debug);
125 setlogmask(LOG_UPTO(LOG_DEBUG));
127 setlogmask(LOG_UPTO(LOG_WARNING));
132 * ppp_available - check whether the system has any ppp interfaces
139 return stat("/dev/ppp", &buf) >= 0;
143 * establish_ppp - Turn the serial port into a ppp interface.
151 tty_sid = getsid((pid_t)0);
153 pppfd = open("/dev/ppp", O_RDWR | O_NONBLOCK, 0);
155 syslog(LOG_ERR, "Can't open /dev/ppp: %m");
159 /* Assign a new PPA and get its unit number. */
160 if (strioctl(pppfd, PPPIO_NEWPPA, &ifunit, 0, sizeof(int)) < 0) {
161 syslog(LOG_ERR, "Can't create new PPP interface: %m");
166 * Open the ppp device again and link it under the ip multiplexor.
167 * IP will assign a unit number which hopefully is the same as ifunit.
168 * I don't know any way to be certain they will be the same. :-(
170 ifd = open("/dev/ppp", O_RDWR, 0);
172 syslog(LOG_ERR, "Can't open /dev/ppp (2): %m");
175 if (ioctl(ifd, I_PUSH, "ip") < 0) {
176 syslog(LOG_ERR, "Can't push IP module: %m");
180 ipmuxid = ioctl(ipfd, I_LINK, ifd);
183 syslog(LOG_ERR, "Can't link PPP device to IP: %m");
187 /* Pop any existing modules off the tty stream. */
189 if (ioctl(fd, I_LOOK, tty_modules[i]) < 0
190 || ioctl(fd, I_POP, 0) < 0)
194 /* Push the async hdlc module and the compressor module. */
195 if (ioctl(fd, I_PUSH, "ppp_ahdl") < 0) {
196 syslog(LOG_ERR, "Couldn't push PPP Async HDLC module: %m");
199 if (ioctl(fd, I_PUSH, "ppp_comp") < 0) {
200 syslog(LOG_ERR, "Couldn't push PPP compression module: %m");
204 /* Link the serial port under the PPP multiplexor. */
205 if ((fdmuxid = ioctl(pppfd, I_LINK, fd)) < 0) {
206 syslog(LOG_ERR, "Can't link tty to PPP mux: %m");
212 * disestablish_ppp - Restore the serial port to normal operation.
213 * This shouldn't call die() because it's called from die().
221 if (ioctl(pppfd, I_UNLINK, fdmuxid) < 0) {
223 syslog(LOG_ERR, "Can't unlink tty from PPP mux: %m");
228 while (ioctl(fd, I_POP, 0) >= 0)
230 for (i = tty_nmodules - 1; i >= 0; --i)
231 if (ioctl(fd, I_PUSH, tty_modules[i]) < 0)
232 syslog(LOG_ERR, "Couldn't restore tty module %s: %m",
235 if (hungup && default_device && tty_sid > 0) {
237 * If we have received a hangup, we need to send a SIGHUP
238 * to the terminal's controlling process. The reason is
239 * that the original stream head for the terminal hasn't
240 * seen the M_HANGUP message (it went up through the ppp
241 * driver to the stream head for our fd to /dev/ppp).
243 kill(tty_sid, SIGHUP);
249 * List of valid speeds.
252 int speed_int, speed_val;
324 * Translate from bits/second to a speed_t.
330 struct speed *speedp;
334 for (speedp = speeds; speedp->speed_int; speedp++)
335 if (bps == speedp->speed_int)
336 return speedp->speed_val;
337 syslog(LOG_WARNING, "speed %d not supported", bps);
342 * Translate from a speed_t to bits/second.
348 struct speed *speedp;
352 for (speedp = speeds; speedp->speed_int; speedp++)
353 if (speed == speedp->speed_val)
354 return speedp->speed_int;
359 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
360 * at the requested speed, etc. If `local' is true, set CLOCAL
361 * regardless of whether the modem option was specified.
363 set_up_tty(fd, local)
369 if (tcgetattr(fd, &tios) < 0) {
370 syslog(LOG_ERR, "tcgetattr: %m");
377 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
379 tios.c_cflag |= CRTSCTS;
380 else if (crtscts < 0)
381 tios.c_cflag &= ~CRTSCTS;
383 tios.c_cflag |= CS8 | CREAD | HUPCL;
385 tios.c_cflag |= CLOCAL;
386 tios.c_iflag = IGNBRK | IGNPAR;
390 tios.c_cc[VTIME] = 0;
393 tios.c_iflag |= IXOFF;
394 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
395 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
398 speed = translate_speed(inspeed);
400 cfsetospeed(&tios, speed);
401 cfsetispeed(&tios, speed);
403 speed = cfgetospeed(&tios);
405 * We can't proceed if the serial port speed is 0,
406 * since that implies that the serial port is disabled.
409 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
415 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
416 syslog(LOG_ERR, "tcsetattr: %m");
420 baud_rate = inspeed = baud_rate_of(speed);
425 * restore_tty - restore the terminal to the saved settings.
431 if (!default_device) {
433 * Turn off echoing, because otherwise we can get into
434 * a loop with the tty and the modem echoing to each other.
435 * We presume we are the sole user of this tty device, so
436 * when we close it, it will revert to its defaults anyway.
438 inittermios.c_lflag &= ~(ECHO | ECHONL);
440 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
441 if (!hungup && errno != ENXIO)
442 syslog(LOG_WARNING, "tcsetattr: %m");
448 * setdtr - control the DTR line on the serial port.
449 * This is called from die(), so it shouldn't call die().
454 int modembits = TIOCM_DTR;
456 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
460 * output - Output PPP packet.
471 log_packet(p, len, "sent ");
474 data.buf = (caddr_t) p;
475 if (putmsg(pppfd, NULL, &data, 0) < 0) {
476 if (errno != ENXIO) {
477 syslog(LOG_ERR, "Couldn't send packet: %m");
484 * wait_input - wait until there is data available on fd,
485 * for the length of time specified by *timo (indefinite
489 struct timeval *timo;
494 t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
496 pfd.events = POLLIN | POLLPRI | POLLHUP;
497 if (poll(&pfd, 1, t) < 0 && errno != EINTR) {
498 syslog(LOG_ERR, "poll: %m");
504 * read_packet - get a PPP packet from the serial device.
510 struct strbuf ctrl, data;
512 unsigned char ctrlbuf[sizeof(union DL_primitives) + 64];
515 data.maxlen = PPP_MRU + PPP_HDRLEN;
516 data.buf = (caddr_t) buf;
517 ctrl.maxlen = sizeof(ctrlbuf);
518 ctrl.buf = (caddr_t) ctrlbuf;
520 len = getmsg(pppfd, &ctrl, &data, &flags);
522 if (errno = EAGAIN || errno == EINTR)
524 syslog(LOG_ERR, "Error reading packet: %m");
532 * Got a M_PROTO or M_PCPROTO message. Interpret it
533 * as a DLPI primitive.
536 syslog(LOG_DEBUG, "got dlpi prim 0x%x, len=%d",
537 ((union DL_primitives *)ctrlbuf)->dl_primitive, ctrl.len);
543 * ppp_send_config - configure the transmit characteristics of
547 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
555 if (strioctl(pppfd, PPPIO_MTU, &mtu, sizeof(mtu), 0) < 0) {
556 if (hungup && errno == ENXIO)
558 syslog(LOG_ERR, "Couldn't set MTU: %m");
560 if (strioctl(pppfd, PPPIO_XACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
561 syslog(LOG_ERR, "Couldn't set transmit ACCM: %m");
563 cf[0] = (pcomp? COMP_PROT: 0) + (accomp? COMP_AC: 0);
564 cf[1] = COMP_PROT | COMP_AC;
565 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
566 syslog(LOG_ERR, "Couldn't set prot/AC compression: %m");
571 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
574 ppp_set_xaccm(unit, accm)
578 if (strioctl(pppfd, PPPIO_XACCM, accm, sizeof(ext_accm), 0) < 0) {
579 if (!hungup || errno != ENXIO)
580 syslog(LOG_WARNING, "Couldn't set extended ACCM: %m");
585 * ppp_recv_config - configure the receive-side characteristics of
589 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
597 if (strioctl(pppfd, PPPIO_MRU, &mru, sizeof(mru), 0) < 0) {
598 if (hungup && errno == ENXIO)
600 syslog(LOG_ERR, "Couldn't set MRU: %m");
602 if (strioctl(pppfd, PPPIO_RACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
603 syslog(LOG_ERR, "Couldn't set receive ACCM: %m");
605 cf[0] = (pcomp? DECOMP_PROT: 0) + (accomp? DECOMP_AC: 0);
606 cf[1] = DECOMP_PROT | DECOMP_AC;
607 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
608 syslog(LOG_ERR, "Couldn't set prot/AC decompression: %m");
613 * ccp_test - ask kernel whether a given compression method
614 * is acceptable for use.
616 ccp_test(unit, opt_ptr, opt_len, for_transmit)
617 int unit, opt_len, for_transmit;
620 return strioctl(pppfd, (for_transmit? PPPIO_XCOMP: PPPIO_RCOMP),
621 opt_ptr, opt_len, 0) >= 0;
625 * ccp_flags_set - inform kernel about the current state of CCP.
628 ccp_flags_set(unit, isopen, isup)
629 int unit, isopen, isup;
633 cf[0] = (isopen? CCP_ISOPEN: 0) + (isup? CCP_ISUP: 0);
634 cf[1] = CCP_ISOPEN | CCP_ISUP | CCP_ERROR | CCP_FATALERROR;
635 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
636 if (!hungup || errno != ENXIO)
637 syslog(LOG_ERR, "Couldn't set kernel CCP state: %m");
642 * ccp_fatal_error - returns 1 if decompression was disabled as a
643 * result of an error detected after decompression of a packet,
644 * 0 otherwise. This is necessary because of patent nonsense.
647 ccp_fatal_error(unit)
653 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
654 if (errno != ENXIO && errno != EINVAL)
655 syslog(LOG_ERR, "Couldn't get compression flags: %m");
658 return cf[0] & CCP_FATALERROR;
662 * sifvjcomp - config tcp header compression
665 sifvjcomp(u, vjcomp, xcidcomp, xmaxcid)
666 int u, vjcomp, xcidcomp, xmaxcid;
672 maxcid[0] = xcidcomp;
673 maxcid[1] = 15; /* XXX should be rmaxcid */
674 if (strioctl(pppfd, PPPIO_VJINIT, maxcid, sizeof(maxcid), 0) < 0) {
675 syslog(LOG_ERR, "Couldn't initialize VJ compression: %m");
679 cf[0] = (vjcomp? COMP_VJC + DECOMP_VJC: 0) /* XXX this is wrong */
680 + (xcidcomp? COMP_VJCCID + DECOMP_VJCCID: 0);
681 cf[1] = COMP_VJC + DECOMP_VJC + COMP_VJCCID + DECOMP_VJCCID;
682 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
684 syslog(LOG_ERR, "Couldn't enable VJ compression: %m");
691 * sifup - Config the interface up and enable IP packets to pass.
699 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
700 if (ioctl(ipfd, SIOCGIFFLAGS, &ifr) < 0) {
701 syslog(LOG_ERR, "Couldn't mark interface up (get): %m");
704 ifr.ifr_flags |= IFF_UP;
705 if (ioctl(ipfd, SIOCSIFFLAGS, &ifr) < 0) {
706 syslog(LOG_ERR, "Couldn't mark interface up (set): %m");
713 * sifdown - Config the interface down and disable IP.
723 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
724 if (ioctl(ipfd, SIOCGIFFLAGS, &ifr) < 0) {
725 syslog(LOG_ERR, "Couldn't mark interface down (get): %m");
728 ifr.ifr_flags &= ~IFF_UP;
729 if (ioctl(ipfd, SIOCSIFFLAGS, &ifr) < 0) {
730 syslog(LOG_ERR, "Couldn't mark interface down (set): %m");
736 #define INET_ADDR(x) (((struct sockaddr_in *) &(x))->sin_addr.s_addr)
739 * sifaddr - Config the interface IP addresses and netmask.
748 memset(&ifr, 0, sizeof(ifr));
749 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
750 ifr.ifr_addr.sa_family = AF_INET;
751 INET_ADDR(ifr.ifr_addr) = o;
752 if (ioctl(ipfd, SIOCSIFADDR, &ifr) < 0) {
753 syslog(LOG_ERR, "Couldn't set local IP address: %m");
755 ifr.ifr_dstaddr.sa_family = AF_INET;
756 INET_ADDR(ifr.ifr_dstaddr) = h;
757 if (ioctl(ipfd, SIOCSIFDSTADDR, &ifr) < 0) {
758 syslog(LOG_ERR, "Couldn't set remote IP address: %m");
760 ifr.ifr_addr.sa_family = AF_INET;
761 INET_ADDR(ifr.ifr_addr) = m;
762 if (ioctl(ipfd, SIOCSIFNETMASK, &ifr) < 0) {
763 syslog(LOG_ERR, "Couldn't set IP netmask: %m");
765 ifr.ifr_metric = link_mtu;
766 if (ioctl(ipfd, SIOCSIFMTU, &ifr) < 0) {
767 syslog(LOG_ERR, "Couldn't set IP MTU: %m");
774 * cifaddr - Clear the interface IP addresses, and delete routes
775 * through the interface if possible.
784 if (ioctl(ipfd, I_UNLINK, ipmuxid) < 0) {
785 syslog(LOG_ERR, "Can't remove ppp interface unit: %m");
795 * sifdefaultroute - assign a default route through the address given.
798 sifdefaultroute(u, g)
804 rt.rt_dst.sa_family = AF_INET;
805 INET_ADDR(rt.rt_dst) = 0;
806 rt.rt_gateway.sa_family = AF_INET;
807 INET_ADDR(rt.rt_gateway) = g;
808 rt.rt_flags = RTF_GATEWAY;
810 if (ioctl(ipfd, SIOCADDRT, &rt) < 0) {
811 syslog(LOG_ERR, "Can't add default route: %m");
819 * cifdefaultroute - delete a default route through the address given.
822 cifdefaultroute(u, g)
828 rt.rt_dst.sa_family = AF_INET;
829 INET_ADDR(rt.rt_dst) = 0;
830 rt.rt_gateway.sa_family = AF_INET;
831 INET_ADDR(rt.rt_gateway) = g;
832 rt.rt_flags = RTF_GATEWAY;
834 if (ioctl(ipfd, SIOCDELRT, &rt) < 0) {
835 syslog(LOG_ERR, "Can't delete default route: %m");
843 * sifproxyarp - Make a proxy ARP entry for the peer.
846 sifproxyarp(unit, hisaddr)
850 struct arpreq arpreq;
852 memset(&arpreq, 0, sizeof(arpreq));
853 if (!get_ether_addr(hisaddr, &arpreq.arp_ha))
856 arpreq.arp_pa.sa_family = AF_INET;
857 INET_ADDR(arpreq.arp_pa) = hisaddr;
858 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
859 if (ioctl(ipfd, SIOCSARP, (caddr_t) &arpreq) < 0) {
860 syslog(LOG_ERR, "Couldn't set proxy ARP entry: %m");
868 * cifproxyarp - Delete the proxy ARP entry for the peer.
871 cifproxyarp(unit, hisaddr)
875 struct arpreq arpreq;
877 memset(&arpreq, 0, sizeof(arpreq));
878 arpreq.arp_pa.sa_family = AF_INET;
879 INET_ADDR(arpreq.arp_pa) = hisaddr;
880 if (ioctl(ipfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
881 syslog(LOG_ERR, "Couldn't delete proxy ARP entry: %m");
889 * get_ether_addr - get the hardware address of an interface on the
890 * the same subnet as ipaddr.
895 get_ether_addr(ipaddr, hwaddr)
897 struct sockaddr *hwaddr;
899 struct ifreq *ifr, *ifend, ifreq;
905 * Scan through the system's network interfaces.
907 if (ioctl(ipfd, SIOCGIFNUM, &nif) < 0)
909 ifc.ifc_len = nif * sizeof(struct ifreq);
910 ifc.ifc_req = alloca(ifc.ifc_len);
911 if (ifc.ifc_req == 0)
913 if (ioctl(ipfd, SIOCGIFCONF, &ifc) < 0) {
914 syslog(LOG_WARNING, "Couldn't get system interface list: %m");
917 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
918 for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
919 if (ifr->ifr_addr.sa_family != AF_INET)
922 * Check that the interface is up, and not point-to-point or loopback.
924 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
925 if (ioctl(ipfd, SIOCGIFFLAGS, &ifreq) < 0)
927 if ((ifreq.ifr_flags &
928 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
929 != (IFF_UP|IFF_BROADCAST))
932 * Get its netmask and check that it's on the right subnet.
934 if (ioctl(ipfd, SIOCGIFNETMASK, &ifreq) < 0)
936 ina = INET_ADDR(ifr->ifr_addr);
937 mask = INET_ADDR(ifreq.ifr_addr);
938 if ((ipaddr & mask) == (ina & mask))
943 syslog(LOG_WARNING, "No suitable interface found for proxy ARP");
947 syslog(LOG_INFO, "found interface %s for proxy ARP", ifr->ifr_name);
948 if (!get_hw_addr(ifr->ifr_name, hwaddr)) {
949 syslog(LOG_ERR, "Couldn't get hardware address for %s", ifr->ifr_name);
957 * get_hw_addr - obtain the hardware address for a named interface.
960 get_hw_addr(name, hwaddr)
962 struct sockaddr *hwaddr;
965 int unit, iffd, adrlen;
969 union DL_primitives prim;
974 * We have to open the device and ask it for its hardware address.
975 * First split apart the device name and unit.
977 strcpy(ifdev, "/dev/");
978 q = ifdev + 5; /* strlen("/dev/") */
979 while (*name != 0 && !isdigit(*name))
985 * Open the device and do a DLPI attach and phys_addr_req.
987 iffd = open(ifdev, O_RDWR);
989 syslog(LOG_ERR, "Can't open %s: %m", ifdev);
992 if (dlpi_attach(iffd, unit) < 0
993 || dlpi_get_reply(iffd, &reply.prim, DL_OK_ACK, sizeof(reply)) < 0
994 || dlpi_phys_addr_req(iffd) < 0
995 || dlpi_get_reply(iffd, &reply.prim, DL_PHYS_ADDR_ACK,
996 sizeof(reply)) < 0) {
1001 hwaddr->sa_family = AF_UNSPEC;
1002 adrlen = reply.prim.physaddr_ack.dl_addr_length;
1003 adrp = (unsigned char *)&reply + reply.prim.physaddr_ack.dl_addr_offset;
1004 memcpy(hwaddr->sa_data, adrp, adrlen);
1010 dlpi_attach(fd, ppa)
1013 dl_attach_req_t req;
1016 req.dl_primitive = DL_ATTACH_REQ;
1018 buf.len = sizeof(req);
1019 buf.buf = (void *) &req;
1020 return putmsg(fd, &buf, NULL, RS_HIPRI);
1024 dlpi_phys_addr_req(fd)
1027 dl_phys_addr_req_t req;
1030 req.dl_primitive = DL_PHYS_ADDR_REQ;
1031 req.dl_addr_type = DL_CURR_PHYS_ADDR;
1032 buf.len = sizeof(req);
1033 buf.buf = (void *) &req;
1034 return putmsg(fd, &buf, NULL, RS_HIPRI);
1038 dlpi_get_reply(fd, reply, expected_prim, maxlen)
1039 union DL_primitives *reply;
1040 int fd, expected_prim, maxlen;
1047 * Use poll to wait for a message with a timeout.
1050 pfd.events = POLLIN | POLLPRI;
1052 n = poll(&pfd, 1, 1000);
1053 } while (n == -1 && errno == EINTR);
1060 buf.maxlen = maxlen;
1061 buf.buf = (void *) reply;
1063 if (getmsg(fd, &buf, NULL, &flags) < 0)
1066 if (buf.len < sizeof(ulong)) {
1068 syslog(LOG_DEBUG, "dlpi response short (len=%d)\n", buf.len);
1072 if (reply->dl_primitive == expected_prim)
1076 if (reply->dl_primitive == DL_ERROR_ACK) {
1077 syslog(LOG_DEBUG, "dlpi error %d (unix errno %d) for prim %x\n",
1078 reply->error_ack.dl_errno, reply->error_ack.dl_unix_errno,
1079 reply->error_ack.dl_error_primitive);
1081 syslog(LOG_DEBUG, "dlpi unexpected response prim %x\n",
1082 reply->dl_primitive);
1090 * Return user specified netmask, modified by any mask we might determine
1091 * for address `addr' (in network byte order).
1092 * Here we scan through the system's list of interfaces, looking for
1093 * any non-point-to-point interfaces which might appear to be on the same
1094 * network as `addr'. If we find any, we OR in their netmask to the
1095 * user-specified netmask.
1101 u_int32_t mask, nmask, ina;
1102 struct ifreq *ifr, *ifend, ifreq;
1107 if (IN_CLASSA(addr)) /* determine network mask for address class */
1108 nmask = IN_CLASSA_NET;
1109 else if (IN_CLASSB(addr))
1110 nmask = IN_CLASSB_NET;
1112 nmask = IN_CLASSC_NET;
1113 /* class D nets are disallowed by bad_ip_adrs */
1114 mask = netmask | htonl(nmask);
1117 * Scan through the system's network interfaces.
1119 if (ioctl(ipfd, SIOCGIFNUM, &nif) < 0)
1121 ifc.ifc_len = nif * sizeof(struct ifreq);
1122 ifc.ifc_req = alloca(ifc.ifc_len);
1123 if (ifc.ifc_req == 0)
1125 if (ioctl(ipfd, SIOCGIFCONF, &ifc) < 0) {
1126 syslog(LOG_WARNING, "Couldn't get system interface list: %m");
1129 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1130 for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
1132 * Check the interface's internet address.
1134 if (ifr->ifr_addr.sa_family != AF_INET)
1136 ina = INET_ADDR(ifr->ifr_addr);
1137 if ((ntohl(ina) & nmask) != (addr & nmask))
1140 * Check that the interface is up, and not point-to-point or loopback.
1142 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1143 if (ioctl(ipfd, SIOCGIFFLAGS, &ifreq) < 0)
1145 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1149 * Get its netmask and OR it into our mask.
1151 if (ioctl(ipfd, SIOCGIFNETMASK, &ifreq) < 0)
1153 mask |= INET_ADDR(ifreq.ifr_addr);
1160 * logwtmp - write an accounting record to the /var/adm/wtmp file.
1163 logwtmp(line, name, host)
1164 char *line, *name, *host;
1169 * gethostid - return the serial number of this machine.
1176 if (sysinfo(SI_HW_SERIAL, buf, sizeof(buf)) < 0) {
1177 syslog(LOG_ERR, "sysinfo: %m");
1180 return (int) strtoul(buf, NULL, 16);
1184 strioctl(fd, cmd, ptr, ilen, olen)
1185 int fd, cmd, ilen, olen;
1188 struct strioctl str;
1194 if (ioctl(fd, I_STR, &str) == -1)
1196 if (str.ic_len != olen)
1197 syslog(LOG_DEBUG, "strioctl: expected %d bytes, got %d for cmd %x\n",
1198 olen, str.ic_len, cmd);