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.6 1995/08/16 01:40:51 paulus Exp $";
42 #include <sys/types.h>
43 #include <sys/ioccom.h>
44 #include <sys/stream.h>
45 #include <sys/stropts.h>
46 #include <sys/socket.h>
47 #include <sys/sockio.h>
48 #include <sys/syslog.h>
49 #include <sys/systeminfo.h>
52 #include <sys/mkdev.h>
54 #include <net/if_arp.h>
55 #include <net/route.h>
56 #include <net/ppp_defs.h>
57 #include <net/pppio.h>
58 #include <netinet/in.h>
63 static int fdmuxid = -1;
65 static int ipmuxid = -1;
67 static int restore_term;
68 static struct termios inittermios;
69 static struct winsize wsinfo; /* Initial window size info */
70 static pid_t tty_sid; /* original session ID for terminal */
72 static int link_mtu, link_mru;
75 static int tty_nmodules;
76 static char tty_modules[NMODULES][FMNAMESZ+1];
79 * sys_init - System-dependent initialization.
84 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
85 setlogmask(LOG_UPTO(LOG_INFO));
87 setlogmask(LOG_UPTO(LOG_DEBUG));
89 ipfd = open("/dev/ip", O_RDWR, 0);
91 syslog(LOG_ERR, "Couldn't open IP device: %m");
97 * daemon - Detach us from controlling terminal session.
100 daemon(nochdir, noclose)
101 int nochdir, noclose;
105 if ((pid = fork()) < 0)
108 exit(0); /* parent dies */
113 fclose(stdin); /* don't need stdin, stdout, stderr */
121 * note_debug_level - note a change in the debug level.
127 syslog(LOG_INFO, "Debug turned ON, Level %d", debug);
128 setlogmask(LOG_UPTO(LOG_DEBUG));
130 setlogmask(LOG_UPTO(LOG_WARNING));
135 * ppp_available - check whether the system has any ppp interfaces
142 return stat("/dev/ppp", &buf) >= 0;
146 * establish_ppp - Turn the serial port into a ppp interface.
154 tty_sid = getsid((pid_t)0);
156 pppfd = open("/dev/ppp", O_RDWR | O_NONBLOCK, 0);
158 syslog(LOG_ERR, "Can't open /dev/ppp: %m");
162 /* Assign a new PPA and get its unit number. */
163 if (strioctl(pppfd, PPPIO_NEWPPA, &ifunit, 0, sizeof(int)) < 0) {
164 syslog(LOG_ERR, "Can't create new PPP interface: %m");
169 * Open the ppp device again and link it under the ip multiplexor.
170 * IP will assign a unit number which hopefully is the same as ifunit.
171 * I don't know any way to be certain they will be the same. :-(
173 ifd = open("/dev/ppp", O_RDWR, 0);
175 syslog(LOG_ERR, "Can't open /dev/ppp (2): %m");
178 if (ioctl(ifd, I_PUSH, "ip") < 0) {
179 syslog(LOG_ERR, "Can't push IP module: %m");
183 ipmuxid = ioctl(ipfd, I_LINK, ifd);
186 syslog(LOG_ERR, "Can't link PPP device to IP: %m");
190 /* Pop any existing modules off the tty stream. */
192 if (ioctl(fd, I_LOOK, tty_modules[i]) < 0
193 || ioctl(fd, I_POP, 0) < 0)
197 /* Push the async hdlc module and the compressor module. */
198 if (ioctl(fd, I_PUSH, "ppp_ahdl") < 0) {
199 syslog(LOG_ERR, "Couldn't push PPP Async HDLC module: %m");
202 if (ioctl(fd, I_PUSH, "ppp_comp") < 0) {
203 syslog(LOG_ERR, "Couldn't push PPP compression module: %m");
207 /* Link the serial port under the PPP multiplexor. */
208 if ((fdmuxid = ioctl(pppfd, I_LINK, fd)) < 0) {
209 syslog(LOG_ERR, "Can't link tty to PPP mux: %m");
215 * disestablish_ppp - Restore the serial port to normal operation.
216 * This shouldn't call die() because it's called from die().
224 if (ioctl(pppfd, I_UNLINK, fdmuxid) < 0) {
226 syslog(LOG_ERR, "Can't unlink tty from PPP mux: %m");
231 while (ioctl(fd, I_POP, 0) >= 0)
233 for (i = tty_nmodules - 1; i >= 0; --i)
234 if (ioctl(fd, I_PUSH, tty_modules[i]) < 0)
235 syslog(LOG_ERR, "Couldn't restore tty module %s: %m",
238 if (hungup && default_device && tty_sid > 0) {
240 * If we have received a hangup, we need to send a SIGHUP
241 * to the terminal's controlling process. The reason is
242 * that the original stream head for the terminal hasn't
243 * seen the M_HANGUP message (it went up through the ppp
244 * driver to the stream head for our fd to /dev/ppp).
246 kill(tty_sid, SIGHUP);
252 * List of valid speeds.
255 int speed_int, speed_val;
327 * Translate from bits/second to a speed_t.
333 struct speed *speedp;
337 for (speedp = speeds; speedp->speed_int; speedp++)
338 if (bps == speedp->speed_int)
339 return speedp->speed_val;
340 syslog(LOG_WARNING, "speed %d not supported", bps);
345 * Translate from a speed_t to bits/second.
351 struct speed *speedp;
355 for (speedp = speeds; speedp->speed_int; speedp++)
356 if (speed == speedp->speed_val)
357 return speedp->speed_int;
362 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
363 * at the requested speed, etc. If `local' is true, set CLOCAL
364 * regardless of whether the modem option was specified.
366 set_up_tty(fd, local)
372 if (tcgetattr(fd, &tios) < 0) {
373 syslog(LOG_ERR, "tcgetattr: %m");
379 ioctl(fd, TIOCGWINSZ, &wsinfo);
382 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
384 tios.c_cflag |= CRTSCTS;
385 else if (crtscts < 0)
386 tios.c_cflag &= ~CRTSCTS;
388 tios.c_cflag |= CS8 | CREAD | HUPCL;
390 tios.c_cflag |= CLOCAL;
391 tios.c_iflag = IGNBRK | IGNPAR;
395 tios.c_cc[VTIME] = 0;
398 tios.c_iflag |= IXOFF;
399 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
400 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
403 speed = translate_speed(inspeed);
405 cfsetospeed(&tios, speed);
406 cfsetispeed(&tios, speed);
408 speed = cfgetospeed(&tios);
410 * We can't proceed if the serial port speed is 0,
411 * since that implies that the serial port is disabled.
414 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
420 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
421 syslog(LOG_ERR, "tcsetattr: %m");
425 baud_rate = inspeed = baud_rate_of(speed);
430 * restore_tty - restore the terminal to the saved settings.
436 if (!default_device) {
438 * Turn off echoing, because otherwise we can get into
439 * a loop with the tty and the modem echoing to each other.
440 * We presume we are the sole user of this tty device, so
441 * when we close it, it will revert to its defaults anyway.
443 inittermios.c_lflag &= ~(ECHO | ECHONL);
445 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
446 if (!hungup && errno != ENXIO)
447 syslog(LOG_WARNING, "tcsetattr: %m");
448 ioctl(fd, TIOCSWINSZ, &wsinfo);
454 * setdtr - control the DTR line on the serial port.
455 * This is called from die(), so it shouldn't call die().
460 int modembits = TIOCM_DTR;
462 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
466 * output - Output PPP packet.
477 log_packet(p, len, "sent ");
480 data.buf = (caddr_t) p;
481 if (putmsg(pppfd, NULL, &data, 0) < 0) {
482 if (errno != ENXIO) {
483 syslog(LOG_ERR, "Couldn't send packet: %m");
490 * wait_input - wait until there is data available on fd,
491 * for the length of time specified by *timo (indefinite
495 struct timeval *timo;
500 t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
502 pfd.events = POLLIN | POLLPRI | POLLHUP;
503 if (poll(&pfd, 1, t) < 0 && errno != EINTR) {
504 syslog(LOG_ERR, "poll: %m");
510 * read_packet - get a PPP packet from the serial device.
516 struct strbuf ctrl, data;
518 unsigned char ctrlbuf[sizeof(union DL_primitives) + 64];
521 data.maxlen = PPP_MRU + PPP_HDRLEN;
522 data.buf = (caddr_t) buf;
523 ctrl.maxlen = sizeof(ctrlbuf);
524 ctrl.buf = (caddr_t) ctrlbuf;
526 len = getmsg(pppfd, &ctrl, &data, &flags);
528 if (errno = EAGAIN || errno == EINTR)
530 syslog(LOG_ERR, "Error reading packet: %m");
538 * Got a M_PROTO or M_PCPROTO message. Interpret it
539 * as a DLPI primitive.
542 syslog(LOG_DEBUG, "got dlpi prim 0x%x, len=%d",
543 ((union DL_primitives *)ctrlbuf)->dl_primitive, ctrl.len);
549 * ppp_send_config - configure the transmit characteristics of
553 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
561 if (strioctl(pppfd, PPPIO_MTU, &mtu, sizeof(mtu), 0) < 0) {
562 if (hungup && errno == ENXIO)
564 syslog(LOG_ERR, "Couldn't set MTU: %m");
566 if (strioctl(pppfd, PPPIO_XACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
567 syslog(LOG_ERR, "Couldn't set transmit ACCM: %m");
569 cf[0] = (pcomp? COMP_PROT: 0) + (accomp? COMP_AC: 0);
570 cf[1] = COMP_PROT | COMP_AC;
571 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
572 syslog(LOG_ERR, "Couldn't set prot/AC compression: %m");
577 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
580 ppp_set_xaccm(unit, accm)
584 if (strioctl(pppfd, PPPIO_XACCM, accm, sizeof(ext_accm), 0) < 0) {
585 if (!hungup || errno != ENXIO)
586 syslog(LOG_WARNING, "Couldn't set extended ACCM: %m");
591 * ppp_recv_config - configure the receive-side characteristics of
595 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
603 if (strioctl(pppfd, PPPIO_MRU, &mru, sizeof(mru), 0) < 0) {
604 if (hungup && errno == ENXIO)
606 syslog(LOG_ERR, "Couldn't set MRU: %m");
608 if (strioctl(pppfd, PPPIO_RACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
609 syslog(LOG_ERR, "Couldn't set receive ACCM: %m");
611 cf[0] = (pcomp? DECOMP_PROT: 0) + (accomp? DECOMP_AC: 0);
612 cf[1] = DECOMP_PROT | DECOMP_AC;
613 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
614 syslog(LOG_ERR, "Couldn't set prot/AC decompression: %m");
619 * ccp_test - ask kernel whether a given compression method
620 * is acceptable for use.
622 ccp_test(unit, opt_ptr, opt_len, for_transmit)
623 int unit, opt_len, for_transmit;
626 return strioctl(pppfd, (for_transmit? PPPIO_XCOMP: PPPIO_RCOMP),
627 opt_ptr, opt_len, 0) >= 0;
631 * ccp_flags_set - inform kernel about the current state of CCP.
634 ccp_flags_set(unit, isopen, isup)
635 int unit, isopen, isup;
639 cf[0] = (isopen? CCP_ISOPEN: 0) + (isup? CCP_ISUP: 0);
640 cf[1] = CCP_ISOPEN | CCP_ISUP | CCP_ERROR | CCP_FATALERROR;
641 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
642 if (!hungup || errno != ENXIO)
643 syslog(LOG_ERR, "Couldn't set kernel CCP state: %m");
648 * ccp_fatal_error - returns 1 if decompression was disabled as a
649 * result of an error detected after decompression of a packet,
650 * 0 otherwise. This is necessary because of patent nonsense.
653 ccp_fatal_error(unit)
659 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
660 if (errno != ENXIO && errno != EINVAL)
661 syslog(LOG_ERR, "Couldn't get compression flags: %m");
664 return cf[0] & CCP_FATALERROR;
668 * sifvjcomp - config tcp header compression
671 sifvjcomp(u, vjcomp, xcidcomp, xmaxcid)
672 int u, vjcomp, xcidcomp, xmaxcid;
678 maxcid[0] = xcidcomp;
679 maxcid[1] = 15; /* XXX should be rmaxcid */
680 if (strioctl(pppfd, PPPIO_VJINIT, maxcid, sizeof(maxcid), 0) < 0) {
681 syslog(LOG_ERR, "Couldn't initialize VJ compression: %m");
685 cf[0] = (vjcomp? COMP_VJC + DECOMP_VJC: 0) /* XXX this is wrong */
686 + (xcidcomp? COMP_VJCCID + DECOMP_VJCCID: 0);
687 cf[1] = COMP_VJC + DECOMP_VJC + COMP_VJCCID + DECOMP_VJCCID;
688 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
690 syslog(LOG_ERR, "Couldn't enable VJ compression: %m");
697 * sifup - Config the interface up and enable IP packets to pass.
705 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
706 if (ioctl(ipfd, SIOCGIFFLAGS, &ifr) < 0) {
707 syslog(LOG_ERR, "Couldn't mark interface up (get): %m");
710 ifr.ifr_flags |= IFF_UP;
711 if (ioctl(ipfd, SIOCSIFFLAGS, &ifr) < 0) {
712 syslog(LOG_ERR, "Couldn't mark interface up (set): %m");
719 * sifdown - Config the interface down and disable IP.
729 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
730 if (ioctl(ipfd, SIOCGIFFLAGS, &ifr) < 0) {
731 syslog(LOG_ERR, "Couldn't mark interface down (get): %m");
734 ifr.ifr_flags &= ~IFF_UP;
735 if (ioctl(ipfd, SIOCSIFFLAGS, &ifr) < 0) {
736 syslog(LOG_ERR, "Couldn't mark interface down (set): %m");
742 #define INET_ADDR(x) (((struct sockaddr_in *) &(x))->sin_addr.s_addr)
745 * sifaddr - Config the interface IP addresses and netmask.
754 memset(&ifr, 0, sizeof(ifr));
755 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
756 ifr.ifr_addr.sa_family = AF_INET;
757 INET_ADDR(ifr.ifr_addr) = o;
758 if (ioctl(ipfd, SIOCSIFADDR, &ifr) < 0) {
759 syslog(LOG_ERR, "Couldn't set local IP address: %m");
761 ifr.ifr_dstaddr.sa_family = AF_INET;
762 INET_ADDR(ifr.ifr_dstaddr) = h;
763 if (ioctl(ipfd, SIOCSIFDSTADDR, &ifr) < 0) {
764 syslog(LOG_ERR, "Couldn't set remote IP address: %m");
766 ifr.ifr_addr.sa_family = AF_INET;
767 INET_ADDR(ifr.ifr_addr) = m;
768 if (ioctl(ipfd, SIOCSIFNETMASK, &ifr) < 0) {
769 syslog(LOG_ERR, "Couldn't set IP netmask: %m");
771 ifr.ifr_metric = link_mtu;
772 if (ioctl(ipfd, SIOCSIFMTU, &ifr) < 0) {
773 syslog(LOG_ERR, "Couldn't set IP MTU: %m");
780 * cifaddr - Clear the interface IP addresses, and delete routes
781 * through the interface if possible.
790 if (ioctl(ipfd, I_UNLINK, ipmuxid) < 0) {
791 syslog(LOG_ERR, "Can't remove ppp interface unit: %m");
801 * sifdefaultroute - assign a default route through the address given.
804 sifdefaultroute(u, g)
810 rt.rt_dst.sa_family = AF_INET;
811 INET_ADDR(rt.rt_dst) = 0;
812 rt.rt_gateway.sa_family = AF_INET;
813 INET_ADDR(rt.rt_gateway) = g;
814 rt.rt_flags = RTF_GATEWAY;
816 if (ioctl(ipfd, SIOCADDRT, &rt) < 0) {
817 syslog(LOG_ERR, "Can't add default route: %m");
825 * cifdefaultroute - delete a default route through the address given.
828 cifdefaultroute(u, g)
834 rt.rt_dst.sa_family = AF_INET;
835 INET_ADDR(rt.rt_dst) = 0;
836 rt.rt_gateway.sa_family = AF_INET;
837 INET_ADDR(rt.rt_gateway) = g;
838 rt.rt_flags = RTF_GATEWAY;
840 if (ioctl(ipfd, SIOCDELRT, &rt) < 0) {
841 syslog(LOG_ERR, "Can't delete default route: %m");
849 * sifproxyarp - Make a proxy ARP entry for the peer.
852 sifproxyarp(unit, hisaddr)
856 struct arpreq arpreq;
858 memset(&arpreq, 0, sizeof(arpreq));
859 if (!get_ether_addr(hisaddr, &arpreq.arp_ha))
862 arpreq.arp_pa.sa_family = AF_INET;
863 INET_ADDR(arpreq.arp_pa) = hisaddr;
864 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
865 if (ioctl(ipfd, SIOCSARP, (caddr_t) &arpreq) < 0) {
866 syslog(LOG_ERR, "Couldn't set proxy ARP entry: %m");
874 * cifproxyarp - Delete the proxy ARP entry for the peer.
877 cifproxyarp(unit, hisaddr)
881 struct arpreq arpreq;
883 memset(&arpreq, 0, sizeof(arpreq));
884 arpreq.arp_pa.sa_family = AF_INET;
885 INET_ADDR(arpreq.arp_pa) = hisaddr;
886 if (ioctl(ipfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
887 syslog(LOG_ERR, "Couldn't delete proxy ARP entry: %m");
895 * get_ether_addr - get the hardware address of an interface on the
896 * the same subnet as ipaddr.
901 get_ether_addr(ipaddr, hwaddr)
903 struct sockaddr *hwaddr;
905 struct ifreq *ifr, *ifend, ifreq;
911 * Scan through the system's network interfaces.
913 if (ioctl(ipfd, SIOCGIFNUM, &nif) < 0)
915 ifc.ifc_len = nif * sizeof(struct ifreq);
916 ifc.ifc_req = alloca(ifc.ifc_len);
917 if (ifc.ifc_req == 0)
919 if (ioctl(ipfd, SIOCGIFCONF, &ifc) < 0) {
920 syslog(LOG_WARNING, "Couldn't get system interface list: %m");
923 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
924 for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
925 if (ifr->ifr_addr.sa_family != AF_INET)
928 * Check that the interface is up, and not point-to-point or loopback.
930 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
931 if (ioctl(ipfd, SIOCGIFFLAGS, &ifreq) < 0)
933 if ((ifreq.ifr_flags &
934 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
935 != (IFF_UP|IFF_BROADCAST))
938 * Get its netmask and check that it's on the right subnet.
940 if (ioctl(ipfd, SIOCGIFNETMASK, &ifreq) < 0)
942 ina = INET_ADDR(ifr->ifr_addr);
943 mask = INET_ADDR(ifreq.ifr_addr);
944 if ((ipaddr & mask) == (ina & mask))
949 syslog(LOG_WARNING, "No suitable interface found for proxy ARP");
953 syslog(LOG_INFO, "found interface %s for proxy ARP", ifr->ifr_name);
954 if (!get_hw_addr(ifr->ifr_name, hwaddr)) {
955 syslog(LOG_ERR, "Couldn't get hardware address for %s", ifr->ifr_name);
963 * get_hw_addr - obtain the hardware address for a named interface.
966 get_hw_addr(name, hwaddr)
968 struct sockaddr *hwaddr;
971 int unit, iffd, adrlen;
975 union DL_primitives prim;
980 * We have to open the device and ask it for its hardware address.
981 * First split apart the device name and unit.
983 strcpy(ifdev, "/dev/");
984 q = ifdev + 5; /* strlen("/dev/") */
985 while (*name != 0 && !isdigit(*name))
991 * Open the device and do a DLPI attach and phys_addr_req.
993 iffd = open(ifdev, O_RDWR);
995 syslog(LOG_ERR, "Can't open %s: %m", ifdev);
998 if (dlpi_attach(iffd, unit) < 0
999 || dlpi_get_reply(iffd, &reply.prim, DL_OK_ACK, sizeof(reply)) < 0
1000 || dlpi_phys_addr_req(iffd) < 0
1001 || dlpi_get_reply(iffd, &reply.prim, DL_PHYS_ADDR_ACK,
1002 sizeof(reply)) < 0) {
1007 hwaddr->sa_family = AF_UNSPEC;
1008 adrlen = reply.prim.physaddr_ack.dl_addr_length;
1009 adrp = (unsigned char *)&reply + reply.prim.physaddr_ack.dl_addr_offset;
1010 memcpy(hwaddr->sa_data, adrp, adrlen);
1016 dlpi_attach(fd, ppa)
1019 dl_attach_req_t req;
1022 req.dl_primitive = DL_ATTACH_REQ;
1024 buf.len = sizeof(req);
1025 buf.buf = (void *) &req;
1026 return putmsg(fd, &buf, NULL, RS_HIPRI);
1030 dlpi_phys_addr_req(fd)
1033 dl_phys_addr_req_t req;
1036 req.dl_primitive = DL_PHYS_ADDR_REQ;
1037 req.dl_addr_type = DL_CURR_PHYS_ADDR;
1038 buf.len = sizeof(req);
1039 buf.buf = (void *) &req;
1040 return putmsg(fd, &buf, NULL, RS_HIPRI);
1044 dlpi_get_reply(fd, reply, expected_prim, maxlen)
1045 union DL_primitives *reply;
1046 int fd, expected_prim, maxlen;
1053 * Use poll to wait for a message with a timeout.
1056 pfd.events = POLLIN | POLLPRI;
1058 n = poll(&pfd, 1, 1000);
1059 } while (n == -1 && errno == EINTR);
1066 buf.maxlen = maxlen;
1067 buf.buf = (void *) reply;
1069 if (getmsg(fd, &buf, NULL, &flags) < 0)
1072 if (buf.len < sizeof(ulong)) {
1074 syslog(LOG_DEBUG, "dlpi response short (len=%d)\n", buf.len);
1078 if (reply->dl_primitive == expected_prim)
1082 if (reply->dl_primitive == DL_ERROR_ACK) {
1083 syslog(LOG_DEBUG, "dlpi error %d (unix errno %d) for prim %x\n",
1084 reply->error_ack.dl_errno, reply->error_ack.dl_unix_errno,
1085 reply->error_ack.dl_error_primitive);
1087 syslog(LOG_DEBUG, "dlpi unexpected response prim %x\n",
1088 reply->dl_primitive);
1096 * Return user specified netmask, modified by any mask we might determine
1097 * for address `addr' (in network byte order).
1098 * Here we scan through the system's list of interfaces, looking for
1099 * any non-point-to-point interfaces which might appear to be on the same
1100 * network as `addr'. If we find any, we OR in their netmask to the
1101 * user-specified netmask.
1107 u_int32_t mask, nmask, ina;
1108 struct ifreq *ifr, *ifend, ifreq;
1113 if (IN_CLASSA(addr)) /* determine network mask for address class */
1114 nmask = IN_CLASSA_NET;
1115 else if (IN_CLASSB(addr))
1116 nmask = IN_CLASSB_NET;
1118 nmask = IN_CLASSC_NET;
1119 /* class D nets are disallowed by bad_ip_adrs */
1120 mask = netmask | htonl(nmask);
1123 * Scan through the system's network interfaces.
1125 if (ioctl(ipfd, SIOCGIFNUM, &nif) < 0)
1127 ifc.ifc_len = nif * sizeof(struct ifreq);
1128 ifc.ifc_req = alloca(ifc.ifc_len);
1129 if (ifc.ifc_req == 0)
1131 if (ioctl(ipfd, SIOCGIFCONF, &ifc) < 0) {
1132 syslog(LOG_WARNING, "Couldn't get system interface list: %m");
1135 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1136 for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
1138 * Check the interface's internet address.
1140 if (ifr->ifr_addr.sa_family != AF_INET)
1142 ina = INET_ADDR(ifr->ifr_addr);
1143 if ((ntohl(ina) & nmask) != (addr & nmask))
1146 * Check that the interface is up, and not point-to-point or loopback.
1148 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1149 if (ioctl(ipfd, SIOCGIFFLAGS, &ifreq) < 0)
1151 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1155 * Get its netmask and OR it into our mask.
1157 if (ioctl(ipfd, SIOCGIFNETMASK, &ifreq) < 0)
1159 mask |= INET_ADDR(ifreq.ifr_addr);
1166 * logwtmp - write an accounting record to the /var/adm/wtmp file.
1169 logwtmp(line, name, host)
1170 char *line, *name, *host;
1172 static struct utmpx utmpx;
1176 strncpy(utmpx.ut_user, name, sizeof(utmpx.ut_user));
1177 strncpy(utmpx.ut_id, ifname, sizeof(utmpx.ut_id));
1178 strncpy(utmpx.ut_line, line, sizeof(utmpx.ut_line));
1179 utmpx.ut_pid = getpid();
1180 utmpx.ut_type = USER_PROCESS;
1182 utmpx.ut_type = DEAD_PROCESS;
1184 gettimeofday(&utmpx.ut_tv, NULL);
1185 updwtmpx("/var/adm/wtmpx", &utmpx);
1190 * gethostid - return the serial number of this machine.
1197 if (sysinfo(SI_HW_SERIAL, buf, sizeof(buf)) < 0) {
1198 syslog(LOG_ERR, "sysinfo: %m");
1201 return (int) strtoul(buf, NULL, 16);
1205 strioctl(fd, cmd, ptr, ilen, olen)
1206 int fd, cmd, ilen, olen;
1209 struct strioctl str;
1215 if (ioctl(fd, I_STR, &str) == -1)
1217 if (str.ic_len != olen)
1218 syslog(LOG_DEBUG, "strioctl: expected %d bytes, got %d for cmd %x\n",
1219 olen, str.ic_len, cmd);
1224 * lock - create a lock file for the named lock device
1226 * XXX Does anybody know what the "032" in the lock file name means?
1229 #define LOCK_PREFIX "/var/spool/locks/LK.032."
1230 static char lock_file[40]; /* name of lock file created */
1240 if (stat(dev, &sbuf) < 0) {
1241 syslog(LOG_ERR, "Can't get device number for %s: %m", dev);
1244 if ((sbuf.st_mode & S_IFMT) != S_IFCHR) {
1245 syslog(LOG_ERR, "Can't lock %s: not a character device", dev);
1248 sprintf(lock_file, "%s%03d.%03d", LOCK_PREFIX, major(sbuf.st_rdev),
1249 minor(sbuf.st_rdev));
1251 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1253 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1254 /* Read the lock file to find out who has the device locked */
1255 n = read(fd, ascii_pid, 11);
1257 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1261 pid = atoi(ascii_pid);
1262 if (pid > 0 && kill(pid, 0) == -1 && errno == ESRCH) {
1263 /* pid no longer exists - remove the lock file */
1264 if (unlink(lock_file) == 0) {
1266 syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1270 syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
1273 syslog(LOG_NOTICE, "Device %s is locked by pid %d",
1278 syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1283 sprintf(ascii_pid, "%10d\n", getpid());
1284 write(fd, ascii_pid, 11);
1291 * unlock - remove our lockfile