2 * System-dependent procedures for pppd under SunOS 4.
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-sunos4.c,v 1.2 1995/12/19 02:04:40 paulus Exp $";
44 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <sys/socket.h>
47 #include <sys/sockio.h>
48 #include <sys/stream.h>
49 #include <sys/stropts.h>
50 #include <sys/syslog.h>
55 #include <net/if_arp.h>
56 #include <net/nit_if.h>
57 #include <net/route.h>
58 #include <net/ppp_defs.h>
59 #include <net/pppio.h>
60 #include <netinet/in.h>
64 #if defined(sun) && defined(sparc)
69 static int fdmuxid = -1;
73 static int restore_term;
74 static struct termios inittermios;
75 static struct winsize wsinfo; /* Initial window size info */
76 static pid_t parent_pid; /* PID of our parent */
78 static int link_mtu, link_mru;
81 static int tty_nmodules;
82 static char tty_modules[NMODULES][FMNAMESZ+1];
84 static int if_is_up; /* Interface has been marked up */
85 static u_int32_t default_route_gateway; /* Gateway for default route added */
86 static u_int32_t proxy_arp_addr; /* Addr for proxy arp entry added */
88 /* Prototypes for procedures local to this file. */
89 static int translate_speed __P((int));
90 static int baud_rate_of __P((int));
91 static int get_ether_addr __P((u_int32_t, struct sockaddr *));
92 static int strioctl __P((int, int, void *, int, int));
96 * sys_init - System-dependent initialization.
101 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
102 setlogmask(LOG_UPTO(LOG_INFO));
104 setlogmask(LOG_UPTO(LOG_DEBUG));
106 /* Get an internet socket for doing socket ioctl's on. */
107 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
108 syslog(LOG_ERR, "Couldn't create IP socket: %m");
113 * We may want to send a SIGHUP to the session leader associated
114 * with our controlling terminal later. Because SunOS doesn't
115 * have getsid(), we make do with sending the signal to our
118 parent_pid = getppid();
122 * sys_cleanup - restore any system state we modified before exiting:
123 * mark the interface down, delete default route and/or proxy arp entry.
124 * This should call die() because it's called from die().
133 if (default_route_gateway)
134 cifdefaultroute(0, default_route_gateway);
136 cifproxyarp(0, proxy_arp_addr);
140 * daemon - Detach us from controlling terminal session.
143 daemon(nochdir, noclose)
144 int nochdir, noclose;
148 if ((pid = fork()) < 0)
151 exit(0); /* parent dies */
156 fclose(stdin); /* don't need stdin, stdout, stderr */
164 * note_debug_level - note a change in the debug level.
170 syslog(LOG_INFO, "Debug turned ON, Level %d", debug);
171 setlogmask(LOG_UPTO(LOG_DEBUG));
173 setlogmask(LOG_UPTO(LOG_WARNING));
178 * ppp_available - check whether the system has any ppp interfaces
185 return stat("/dev/ppp", &buf) >= 0;
189 * establish_ppp - Turn the serial port into a ppp interface.
197 pppfd = open("/dev/ppp", O_RDWR | O_NONBLOCK, 0);
199 syslog(LOG_ERR, "Can't open /dev/ppp: %m");
203 x = PPPDBG_LOG + PPPDBG_DRIVER;
204 strioctl(pppfd, PPPIO_DEBUG, &x, sizeof(int), 0);
207 /* Assign a new PPA and get its unit number. */
208 if (strioctl(pppfd, PPPIO_NEWPPA, &ifunit, 0, sizeof(int)) < 0) {
209 syslog(LOG_ERR, "Can't create new PPP interface: %m");
214 * Open the ppp device again and push the if_ppp module on it.
216 iffd = open("/dev/ppp", O_RDWR, 0);
218 syslog(LOG_ERR, "Can't open /dev/ppp (2): %m");
222 x = PPPDBG_LOG + PPPDBG_DRIVER;
223 strioctl(iffd, PPPIO_DEBUG, &x, sizeof(int), 0);
225 if (strioctl(iffd, PPPIO_ATTACH, &ifunit, sizeof(int), 0) < 0) {
226 syslog(LOG_ERR, "Couldn't attach ppp interface to device: %m");
229 if (ioctl(iffd, I_PUSH, "if_ppp") < 0) {
230 syslog(LOG_ERR, "Can't push ppp interface module: %m");
234 x = PPPDBG_LOG + PPPDBG_IF;
235 strioctl(iffd, PPPIO_DEBUG, &x, sizeof(int), 0);
237 if (strioctl(iffd, PPPIO_NEWPPA, &ifunit, sizeof(int), 0) < 0) {
238 syslog(LOG_ERR, "Couldn't create ppp interface unit: %m");
242 if (strioctl(iffd, PPPIO_BIND, &x, sizeof(int), 0) < 0) {
243 syslog(LOG_ERR, "Couldn't bind ppp interface to IP SAP: %m");
247 /* Pop any existing modules off the tty stream. */
249 if (ioctl(fd, I_LOOK, tty_modules[i]) < 0
250 || ioctl(fd, I_POP, 0) < 0)
254 /* Push the async hdlc module and the compressor module. */
255 if (ioctl(fd, I_PUSH, "ppp_ahdl") < 0) {
256 syslog(LOG_ERR, "Couldn't push PPP Async HDLC module: %m");
259 if (ioctl(fd, I_PUSH, "ppp_comp") < 0) {
260 syslog(LOG_ERR, "Couldn't push PPP compression module: %m");
264 /* Link the serial port under the PPP multiplexor. */
265 if ((fdmuxid = ioctl(pppfd, I_LINK, fd)) < 0) {
266 syslog(LOG_ERR, "Can't link tty to PPP mux: %m");
273 i = read(fd, buf, PPP_MRU);
274 syslog(LOG_DEBUG, "read %d bytes from fd", i);
279 * disestablish_ppp - Restore the serial port to normal operation.
280 * This shouldn't call die() because it's called from die().
288 if (ioctl(pppfd, I_UNLINK, fdmuxid) < 0) {
290 syslog(LOG_ERR, "Can't unlink tty from PPP mux: %m");
295 while (ioctl(fd, I_POP, 0) >= 0)
297 for (i = tty_nmodules - 1; i >= 0; --i)
298 if (ioctl(fd, I_PUSH, tty_modules[i]) < 0)
299 syslog(LOG_ERR, "Couldn't restore tty module %s: %m",
302 if (hungup && default_device && parent_pid > 0) {
304 * If we have received a hangup, we need to send a SIGHUP
305 * to the terminal's controlling process. The reason is
306 * that the original stream head for the terminal hasn't
307 * seen the M_HANGUP message (it went up through the ppp
308 * driver to the stream head for our fd to /dev/ppp).
309 * Actually we send the signal to the process that invoked
310 * pppd, since SunOS doesn't have getsid().
312 kill(parent_pid, SIGHUP);
318 * List of valid speeds.
321 int speed_int, speed_val;
393 * Translate from bits/second to a speed_t.
399 struct speed *speedp;
403 for (speedp = speeds; speedp->speed_int; speedp++)
404 if (bps == speedp->speed_int)
405 return speedp->speed_val;
406 syslog(LOG_WARNING, "speed %d not supported", bps);
411 * Translate from a speed_t to bits/second.
417 struct speed *speedp;
421 for (speedp = speeds; speedp->speed_int; speedp++)
422 if (speed == speedp->speed_val)
423 return speedp->speed_int;
428 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
429 * at the requested speed, etc. If `local' is true, set CLOCAL
430 * regardless of whether the modem option was specified.
433 set_up_tty(fd, local)
439 if (tcgetattr(fd, &tios) < 0) {
440 syslog(LOG_ERR, "tcgetattr: %m");
446 ioctl(fd, TIOCGWINSZ, &wsinfo);
449 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
451 tios.c_cflag |= CRTSCTS;
452 else if (crtscts < 0)
453 tios.c_cflag &= ~CRTSCTS;
455 tios.c_cflag |= CS8 | CREAD | HUPCL;
457 tios.c_cflag |= CLOCAL;
458 tios.c_iflag = IGNBRK | IGNPAR;
462 tios.c_cc[VTIME] = 0;
465 tios.c_iflag |= IXON | IXOFF;
466 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
467 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
470 speed = translate_speed(inspeed);
472 cfsetospeed(&tios, speed);
473 cfsetispeed(&tios, speed);
475 speed = cfgetospeed(&tios);
477 * We can't proceed if the serial port speed is 0,
478 * since that implies that the serial port is disabled.
481 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
487 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
488 syslog(LOG_ERR, "tcsetattr: %m");
492 baud_rate = inspeed = baud_rate_of(speed);
497 * restore_tty - restore the terminal to the saved settings.
503 if (!default_device) {
505 * Turn off echoing, because otherwise we can get into
506 * a loop with the tty and the modem echoing to each other.
507 * We presume we are the sole user of this tty device, so
508 * when we close it, it will revert to its defaults anyway.
510 inittermios.c_lflag &= ~(ECHO | ECHONL);
512 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
513 if (!hungup && errno != ENXIO)
514 syslog(LOG_WARNING, "tcsetattr: %m");
515 ioctl(fd, TIOCSWINSZ, &wsinfo);
521 * setdtr - control the DTR line on the serial port.
522 * This is called from die(), so it shouldn't call die().
528 int modembits = TIOCM_DTR;
530 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
534 * output - Output PPP packet.
547 log_packet(p, len, "sent ");
550 data.buf = (caddr_t) p;
551 while (putmsg(pppfd, NULL, &data, 0) < 0) {
552 if (--retries < 0 || (errno != EWOULDBLOCK && errno != EAGAIN)) {
554 syslog(LOG_ERR, "Couldn't send packet: %m");
558 pfd.events = POLLOUT;
559 poll(&pfd, 1, 250); /* wait for up to 0.25 seconds */
564 * wait_input - wait until there is data available on fd,
565 * for the length of time specified by *timo (indefinite
570 struct timeval *timo;
575 t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
577 pfd.events = POLLIN | POLLPRI | POLLHUP;
578 if (poll(&pfd, 1, t) < 0 && errno != EINTR) {
579 syslog(LOG_ERR, "poll: %m");
585 * read_packet - get a PPP packet from the serial device.
591 struct strbuf ctrl, data;
593 unsigned char ctrlbuf[64];
596 data.maxlen = PPP_MRU + PPP_HDRLEN;
597 data.buf = (caddr_t) buf;
598 ctrl.maxlen = sizeof(ctrlbuf);
599 ctrl.buf = (caddr_t) ctrlbuf;
601 len = getmsg(pppfd, &ctrl, &data, &flags);
603 if (errno = EAGAIN || errno == EINTR)
605 syslog(LOG_ERR, "Error reading packet: %m");
613 * Got a M_PROTO or M_PCPROTO message. Huh?
616 syslog(LOG_DEBUG, "got ctrl msg len=%d", ctrl.len);
622 * ppp_send_config - configure the transmit characteristics of
626 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
634 if (strioctl(pppfd, PPPIO_MTU, &mtu, sizeof(mtu), 0) < 0) {
635 if (hungup && errno == ENXIO)
637 syslog(LOG_ERR, "Couldn't set MTU: %m");
639 if (strioctl(pppfd, PPPIO_XACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
640 syslog(LOG_ERR, "Couldn't set transmit ACCM: %m");
642 cf[0] = (pcomp? COMP_PROT: 0) + (accomp? COMP_AC: 0);
643 cf[1] = COMP_PROT | COMP_AC;
644 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
645 syslog(LOG_ERR, "Couldn't set prot/AC compression: %m");
650 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
653 ppp_set_xaccm(unit, accm)
657 if (strioctl(pppfd, PPPIO_XACCM, accm, sizeof(ext_accm), 0) < 0) {
658 if (!hungup || errno != ENXIO)
659 syslog(LOG_WARNING, "Couldn't set extended ACCM: %m");
664 * ppp_recv_config - configure the receive-side characteristics of
668 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
676 if (strioctl(pppfd, PPPIO_MRU, &mru, sizeof(mru), 0) < 0) {
677 if (hungup && errno == ENXIO)
679 syslog(LOG_ERR, "Couldn't set MRU: %m");
681 if (strioctl(pppfd, PPPIO_RACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
682 syslog(LOG_ERR, "Couldn't set receive ACCM: %m");
684 cf[0] = (pcomp? DECOMP_PROT: 0) + (accomp? DECOMP_AC: 0);
685 cf[1] = DECOMP_PROT | DECOMP_AC;
686 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
687 syslog(LOG_ERR, "Couldn't set prot/AC decompression: %m");
692 * ccp_test - ask kernel whether a given compression method
693 * is acceptable for use.
696 ccp_test(unit, opt_ptr, opt_len, for_transmit)
697 int unit, opt_len, for_transmit;
700 if (strioctl(pppfd, (for_transmit? PPPIO_XCOMP: PPPIO_RCOMP),
701 opt_ptr, opt_len, 0) >= 0)
703 return (errno == ENOSR)? 0: -1;
707 * ccp_flags_set - inform kernel about the current state of CCP.
710 ccp_flags_set(unit, isopen, isup)
711 int unit, isopen, isup;
715 cf[0] = (isopen? CCP_ISOPEN: 0) + (isup? CCP_ISUP: 0);
716 cf[1] = CCP_ISOPEN | CCP_ISUP | CCP_ERROR | CCP_FATALERROR;
717 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
718 if (!hungup || errno != ENXIO)
719 syslog(LOG_ERR, "Couldn't set kernel CCP state: %m");
724 * ccp_fatal_error - returns 1 if decompression was disabled as a
725 * result of an error detected after decompression of a packet,
726 * 0 otherwise. This is necessary because of patent nonsense.
729 ccp_fatal_error(unit)
735 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
736 if (errno != ENXIO && errno != EINVAL)
737 syslog(LOG_ERR, "Couldn't get compression flags: %m");
740 return cf[0] & CCP_FATALERROR;
744 * sifvjcomp - config tcp header compression
747 sifvjcomp(u, vjcomp, xcidcomp, xmaxcid)
748 int u, vjcomp, xcidcomp, xmaxcid;
754 maxcid[0] = xcidcomp;
755 maxcid[1] = 15; /* XXX should be rmaxcid */
756 if (strioctl(pppfd, PPPIO_VJINIT, maxcid, sizeof(maxcid), 0) < 0) {
757 syslog(LOG_ERR, "Couldn't initialize VJ compression: %m");
761 cf[0] = (vjcomp? COMP_VJC + DECOMP_VJC: 0) /* XXX this is wrong */
762 + (xcidcomp? COMP_VJCCID + DECOMP_VJCCID: 0);
763 cf[1] = COMP_VJC + DECOMP_VJC + COMP_VJCCID + DECOMP_VJCCID;
764 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
766 syslog(LOG_ERR, "Couldn't enable VJ compression: %m");
773 * sifup - Config the interface up and enable IP packets to pass.
781 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
782 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
783 syslog(LOG_ERR, "Couldn't mark interface up (get): %m");
786 ifr.ifr_flags |= IFF_UP;
787 if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
788 syslog(LOG_ERR, "Couldn't mark interface up (set): %m");
796 * sifdown - Config the interface down and disable IP.
804 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
805 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
806 syslog(LOG_ERR, "Couldn't mark interface down (get): %m");
809 if ((ifr.ifr_flags & IFF_UP) != 0) {
810 ifr.ifr_flags &= ~IFF_UP;
811 if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
812 syslog(LOG_ERR, "Couldn't mark interface down (set): %m");
820 #define INET_ADDR(x) (((struct sockaddr_in *) &(x))->sin_addr.s_addr)
823 * sifaddr - Config the interface IP addresses and netmask.
832 memset(&ifr, 0, sizeof(ifr));
833 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
834 ifr.ifr_addr.sa_family = AF_INET;
835 INET_ADDR(ifr.ifr_addr) = o;
836 if (ioctl(sockfd, SIOCSIFADDR, &ifr) < 0) {
837 syslog(LOG_ERR, "Couldn't set local IP address: %m");
839 ifr.ifr_dstaddr.sa_family = AF_INET;
840 INET_ADDR(ifr.ifr_dstaddr) = h;
841 if (ioctl(sockfd, SIOCSIFDSTADDR, &ifr) < 0) {
842 syslog(LOG_ERR, "Couldn't set remote IP address: %m");
844 ifr.ifr_addr.sa_family = AF_INET;
845 INET_ADDR(ifr.ifr_addr) = m;
846 if (ioctl(sockfd, SIOCSIFNETMASK, &ifr) < 0) {
847 syslog(LOG_ERR, "Couldn't set IP netmask: %m");
849 ifr.ifr_metric = link_mtu;
850 if (ioctl(sockfd, SIOCSIFMTU, &ifr) < 0) {
851 syslog(LOG_ERR, "Couldn't set IP MTU: %m");
858 * cifaddr - Clear the interface IP addresses, and delete routes
859 * through the interface if possible.
868 rt.rt_dst.sa_family = AF_INET;
869 INET_ADDR(rt.rt_dst) = h;
870 rt.rt_gateway.sa_family = AF_INET;
871 INET_ADDR(rt.rt_gateway) = o;
872 rt.rt_flags = RTF_HOST;
873 ioctl(sockfd, SIOCDELRT, &rt);
878 * sifdefaultroute - assign a default route through the address given.
881 sifdefaultroute(u, g)
887 rt.rt_dst.sa_family = AF_INET;
888 INET_ADDR(rt.rt_dst) = 0;
889 rt.rt_gateway.sa_family = AF_INET;
890 INET_ADDR(rt.rt_gateway) = g;
891 rt.rt_flags = RTF_GATEWAY;
893 if (ioctl(sockfd, SIOCADDRT, &rt) < 0) {
894 syslog(LOG_ERR, "Can't add default route: %m");
898 default_route_gateway = g;
903 * cifdefaultroute - delete a default route through the address given.
906 cifdefaultroute(u, g)
912 rt.rt_dst.sa_family = AF_INET;
913 INET_ADDR(rt.rt_dst) = 0;
914 rt.rt_gateway.sa_family = AF_INET;
915 INET_ADDR(rt.rt_gateway) = g;
916 rt.rt_flags = RTF_GATEWAY;
918 if (ioctl(sockfd, SIOCDELRT, &rt) < 0) {
919 syslog(LOG_ERR, "Can't delete default route: %m");
923 default_route_gateway = 0;
928 * sifproxyarp - Make a proxy ARP entry for the peer.
931 sifproxyarp(unit, hisaddr)
935 struct arpreq arpreq;
937 bzero(&arpreq, sizeof(arpreq));
938 if (!get_ether_addr(hisaddr, &arpreq.arp_ha))
941 arpreq.arp_pa.sa_family = AF_INET;
942 INET_ADDR(arpreq.arp_pa) = hisaddr;
943 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
944 if (ioctl(sockfd, SIOCSARP, (caddr_t) &arpreq) < 0) {
945 syslog(LOG_ERR, "Couldn't set proxy ARP entry: %m");
949 proxy_arp_addr = hisaddr;
954 * cifproxyarp - Delete the proxy ARP entry for the peer.
957 cifproxyarp(unit, hisaddr)
961 struct arpreq arpreq;
963 bzero(&arpreq, sizeof(arpreq));
964 arpreq.arp_pa.sa_family = AF_INET;
965 INET_ADDR(arpreq.arp_pa) = hisaddr;
966 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
967 syslog(LOG_ERR, "Couldn't delete proxy ARP entry: %m");
976 * get_ether_addr - get the hardware address of an interface on the
977 * the same subnet as ipaddr.
982 get_ether_addr(ipaddr, hwaddr)
984 struct sockaddr *hwaddr;
986 struct ifreq *ifr, *ifend;
990 struct ifreq ifs[MAX_IFS];
993 ifc.ifc_len = sizeof(ifs);
995 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
996 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
1001 * Scan through looking for an interface with an Internet
1002 * address on the same subnet as `ipaddr'.
1004 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1005 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1006 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1007 if (ifr->ifr_addr.sa_family == AF_INET) {
1010 * Check that the interface is up, and not point-to-point
1013 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1014 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1016 if ((ifreq.ifr_flags &
1017 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1018 != (IFF_UP|IFF_BROADCAST))
1022 * Get its netmask and check that it's on the right subnet.
1024 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1026 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1027 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1028 if ((ipaddr & mask) != (ina & mask))
1037 syslog(LOG_INFO, "found interface %s for proxy arp", ifr->ifr_name);
1040 * Grab the physical address for this interface.
1042 if ((nit_fd = open("/dev/nit", O_RDONLY)) < 0) {
1043 syslog(LOG_ERR, "Couldn't open /dev/nit: %m");
1046 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1047 if (ioctl(nit_fd, NIOCBIND, &ifreq) < 0
1048 || ioctl(nit_fd, SIOCGIFADDR, &ifreq) < 0) {
1049 syslog(LOG_ERR, "Couldn't get hardware address for %s: %m",
1055 hwaddr->sa_family = AF_UNSPEC;
1056 memcpy(hwaddr->sa_data, ifreq.ifr_addr.sa_data, 6);
1061 #define WTMPFILE "/usr/adm/wtmp"
1064 logwtmp(line, name, host)
1065 char *line, *name, *host;
1071 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1073 if (!fstat(fd, &buf)) {
1074 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1075 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1076 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1077 (void)time(&ut.ut_time);
1078 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1079 (void)ftruncate(fd, buf.st_size);
1085 * Return user specified netmask, modified by any mask we might determine
1086 * for address `addr' (in network byte order).
1087 * Here we scan through the system's list of interfaces, looking for
1088 * any non-point-to-point interfaces which might appear to be on the same
1089 * network as `addr'. If we find any, we OR in their netmask to the
1090 * user-specified netmask.
1096 u_int32_t mask, nmask, ina;
1097 struct ifreq *ifr, *ifend, ifreq;
1101 if (IN_CLASSA(addr)) /* determine network mask for address class */
1102 nmask = IN_CLASSA_NET;
1103 else if (IN_CLASSB(addr))
1104 nmask = IN_CLASSB_NET;
1106 nmask = IN_CLASSC_NET;
1107 /* class D nets are disallowed by bad_ip_adrs */
1108 mask = netmask | htonl(nmask);
1111 * Scan through the system's network interfaces.
1113 ifc.ifc_len = MAX_IFS * sizeof(struct ifreq);
1114 ifc.ifc_req = alloca(ifc.ifc_len);
1115 if (ifc.ifc_req == 0)
1117 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1118 syslog(LOG_WARNING, "Couldn't get system interface list: %m");
1121 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1122 for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
1124 * Check the interface's internet address.
1126 if (ifr->ifr_addr.sa_family != AF_INET)
1128 ina = INET_ADDR(ifr->ifr_addr);
1129 if ((ntohl(ina) & nmask) != (addr & nmask))
1132 * Check that the interface is up, and not point-to-point or loopback.
1134 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1135 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1137 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1141 * Get its netmask and OR it into our mask.
1143 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1145 mask |= INET_ADDR(ifreq.ifr_addr);
1152 strioctl(fd, cmd, ptr, ilen, olen)
1153 int fd, cmd, ilen, olen;
1156 struct strioctl str;
1162 if (ioctl(fd, I_STR, &str) == -1)
1164 if (str.ic_len != olen)
1165 syslog(LOG_DEBUG, "strioctl: expected %d bytes, got %d for cmd %x\n",
1166 olen, str.ic_len, cmd);
1171 * Code for locking/unlocking the serial device.
1172 * This code is derived from chat.c.
1175 #if !defined(HDB) && !defined(SUNOS3)
1176 #define HDB 1 /* ascii lock files are the default */
1182 # define LOCK_PREFIX "/usr/spool/locks/LCK.."
1184 # define LOCK_PREFIX "/usr/spool/uucp/LCK.."
1186 #endif /* LOCK_DIR */
1188 static char *lock_file; /* name of lock file created */
1191 * lock - create a lock file for the named device.
1197 char hdb_lock_buffer[12];
1201 if ((p = strrchr(dev, '/')) != NULL)
1203 lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1204 if (lock_file == NULL)
1205 novm("lock file name");
1206 strcat(strcpy(lock_file, LOCK_PREFIX), dev);
1208 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1210 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1211 /* Read the lock file to find out who has the device locked */
1213 n = read(fd, hdb_lock_buffer, 11);
1215 hdb_lock_buffer[n] = 0;
1216 pid = atoi(hdb_lock_buffer);
1219 n = read(fd, &pid, sizeof(pid));
1222 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1225 if (kill(pid, 0) == -1 && errno == ESRCH) {
1226 /* pid no longer exists - remove the lock file */
1227 if (unlink(lock_file) == 0) {
1229 syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1233 syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
1236 syslog(LOG_NOTICE, "Device %s is locked by pid %d",
1241 syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1248 sprintf(hdb_lock_buffer, "%10d\n", getpid());
1249 write(fd, hdb_lock_buffer, 11);
1252 write(fd, &pid, sizeof pid);
1260 * unlock - remove our lockfile
1273 * SunOS doesn't have strtoul :-(
1276 strtoul(str, ptr, base)
1280 return (unsigned long) strtol(str, ptr, base);