2 * sys-bsd.c - System-dependent procedures for setting up
3 * PPP interfaces on bsd-4.4-ish systems (including 386BSD, NetBSD, etc.)
5 * Copyright (c) 1989 Carnegie Mellon University.
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by Carnegie Mellon University. The name of the
14 * University may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 static char rcsid[] = "$Id: sys-bsd.c,v 1.21 1995/08/16 01:40:23 paulus Exp $";
36 #include <sys/ioctl.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
43 #include <net/ppp_defs.h>
44 #include <net/if_ppp.h>
45 #include <net/route.h>
46 #include <net/if_dl.h>
47 #include <netinet/in.h>
50 #include <netinet/if_ether.h>
55 static int initdisc = -1; /* Initial TTY discipline */
58 static int restore_term; /* 1 => we've munged the terminal */
59 static struct termios inittermios; /* Initial TTY termios */
60 static struct winsize wsinfo; /* Initial window size info */
62 static char *lock_file; /* name of lock file created */
64 int sockfd; /* socket for doing interface ioctls */
67 * sys_init - System-dependent initialization.
72 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
73 setlogmask(LOG_UPTO(LOG_INFO));
75 setlogmask(LOG_UPTO(LOG_DEBUG));
77 /* Get an internet socket for doing socket ioctl's on. */
78 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
79 syslog(LOG_ERR, "Couldn't create IP socket: %m");
85 * note_debug_level - note a change in the debug level.
91 syslog(LOG_INFO, "Debug turned ON, Level %d", debug);
92 setlogmask(LOG_UPTO(LOG_DEBUG));
94 setlogmask(LOG_UPTO(LOG_WARNING));
99 * ppp_available - check whether the system has any ppp interfaces
100 * (in fact we check whether we can do an ioctl on ppp0).
107 extern char *no_ppp_msg;
109 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
110 return 1; /* can't tell */
112 strncpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
113 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
117 This system lacks kernel support for PPP. To include PPP support\n\
118 in the kernel, please follow the steps detailed in the README.bsd\n\
119 file in the ppp-2.2 distribution.\n";
124 * establish_ppp - Turn the serial port into a ppp interface.
129 int pppdisc = PPPDISC;
132 if (ioctl(fd, TIOCGETD, &initdisc) < 0) {
133 syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
136 if (ioctl(fd, TIOCSETD, &pppdisc) < 0) {
137 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
142 * Find out which interface we were given.
144 if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {
145 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
150 * Enable debug in the driver if requested.
153 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
154 syslog(LOG_WARNING, "ioctl (PPPIOCGFLAGS): %m");
156 x |= (kdebugflag & 0xFF) * SC_DEBUG;
157 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
158 syslog(LOG_WARNING, "ioctl(PPPIOCSFLAGS): %m");
165 * disestablish_ppp - Restore the serial port to normal operation.
166 * This shouldn't call die() because it's called from die().
176 * Check whether the link seems not to be 8-bit clean.
178 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
180 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
182 s = "bit 7 set to 1";
185 s = "bit 7 set to 0";
195 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
196 syslog(LOG_WARNING, "All received characters had %s", s);
199 if (ioctl(fd, TIOCSETD, &initdisc) < 0)
200 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
206 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
207 * at the requested speed, etc. If `local' is true, set CLOCAL
208 * regardless of whether the modem option was specified.
210 * For *BSD, we assume that speed_t values numerically equal bits/second.
212 set_up_tty(fd, local)
217 if (tcgetattr(fd, &tios) < 0) {
218 syslog(LOG_ERR, "tcgetattr: %m");
224 ioctl(fd, TIOCGWINSZ, &wsinfo);
227 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
229 tios.c_cflag |= CRTSCTS;
230 else if (crtscts < 0)
231 tios.c_cflag &= ~CRTSCTS;
233 tios.c_cflag |= CS8 | CREAD | HUPCL;
235 tios.c_cflag |= CLOCAL;
236 tios.c_iflag = IGNBRK | IGNPAR;
240 tios.c_cc[VTIME] = 0;
243 tios.c_iflag |= IXOFF;
244 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
245 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
249 cfsetospeed(&tios, inspeed);
250 cfsetispeed(&tios, inspeed);
252 inspeed = cfgetospeed(&tios);
254 * We can't proceed if the serial port speed is 0,
255 * since that implies that the serial port is disabled.
258 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
265 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
266 syslog(LOG_ERR, "tcsetattr: %m");
274 * restore_tty - restore the terminal to the saved settings.
280 if (!default_device) {
282 * Turn off echoing, because otherwise we can get into
283 * a loop with the tty and the modem echoing to each other.
284 * We presume we are the sole user of this tty device, so
285 * when we close it, it will revert to its defaults anyway.
287 inittermios.c_lflag &= ~(ECHO | ECHONL);
289 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
291 syslog(LOG_WARNING, "tcsetattr: %m");
292 ioctl(fd, TIOCSWINSZ, &wsinfo);
298 * setdtr - control the DTR line on the serial port.
299 * This is called from die(), so it shouldn't call die().
304 int modembits = TIOCM_DTR;
306 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
311 * output - Output PPP packet.
320 MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
322 log_packet(p, len, "sent ");
324 if (write(fd, p, len) < 0) {
325 syslog(LOG_ERR, "write: %m");
332 * wait_input - wait until there is data available on fd,
333 * for the length of time specified by *timo (indefinite
337 struct timeval *timo;
344 n = select(fd+1, &ready, NULL, &ready, timo);
345 if (n < 0 && errno != EINTR) {
346 syslog(LOG_ERR, "select: %m");
353 * read_packet - get a PPP packet from the serial device.
361 if ((len = read(fd, buf, PPP_MTU + PPP_HDRLEN)) < 0) {
362 if (errno == EWOULDBLOCK || errno == EINTR)
364 syslog(LOG_ERR, "read(fd): %m");
372 * ppp_send_config - configure the transmit characteristics of
376 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
384 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
386 if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
387 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
391 if (ioctl(fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
392 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
396 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
397 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
400 x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
401 x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
402 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
403 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
410 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
413 ppp_set_xaccm(unit, accm)
417 if (ioctl(fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
418 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
423 * ppp_recv_config - configure the receive-side characteristics of
427 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
434 if (ioctl(fd, PPPIOCSMRU, (caddr_t) &mru) < 0) {
435 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
438 if (ioctl(fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
439 syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m");
442 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
443 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
446 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
447 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
448 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
454 * ccp_test - ask kernel whether a given compression method
455 * is acceptable for use.
457 ccp_test(unit, opt_ptr, opt_len, for_transmit)
458 int unit, opt_len, for_transmit;
461 struct ppp_option_data data;
464 data.length = opt_len;
465 data.transmit = for_transmit;
466 return ioctl(fd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0;
470 * ccp_flags_set - inform kernel about the current state of CCP.
473 ccp_flags_set(unit, isopen, isup)
474 int unit, isopen, isup;
478 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
479 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
482 x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
483 x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
484 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
485 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
489 * ccp_fatal_error - returns 1 if decompression was disabled as a
490 * result of an error detected after decompression of a packet,
491 * 0 otherwise. This is necessary because of patent nonsense.
494 ccp_fatal_error(unit)
499 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
500 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
503 return x & SC_DC_FERROR;
507 * sifvjcomp - config tcp header compression
510 sifvjcomp(u, vjcomp, cidcomp, maxcid)
511 int u, vjcomp, cidcomp, maxcid;
515 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
516 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
519 x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
520 x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
521 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
522 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
525 if (ioctl(fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
526 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
533 * sifup - Config the interface up and enable IP packets to pass.
536 #define SC_ENABLE_IP 0x100 /* compat for old versions of kernel code */
547 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
548 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
549 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
552 ifr.ifr_flags |= IFF_UP;
553 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
554 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
557 npi.protocol = PPP_IP;
558 npi.mode = NPMODE_PASS;
559 if (ioctl(fd, PPPIOCSNPMODE, &npi) < 0) {
560 if (errno != ENOTTY) {
561 syslog(LOG_ERR, "ioctl(PPPIOCSNPMODE): %m");
564 /* for backwards compatibility */
565 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
566 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
570 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
571 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
579 * sifdown - Config the interface down and disable IP.
591 npi.protocol = PPP_IP;
592 npi.mode = NPMODE_ERROR;
593 if (ioctl(fd, PPPIOCSNPMODE, (caddr_t) &npi) < 0) {
594 if (errno != ENOTTY) {
595 syslog(LOG_ERR, "ioctl(PPPIOCSNPMODE): %m");
598 /* backwards compatibility */
599 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
600 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
604 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
605 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
612 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
613 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
614 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
617 ifr.ifr_flags &= ~IFF_UP;
618 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
619 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
627 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
630 #define SET_SA_FAMILY(addr, family) \
631 BZERO((char *) &(addr), sizeof(addr)); \
632 addr.sa_family = (family); \
633 addr.sa_len = sizeof(addr);
636 * sifaddr - Config the interface IP addresses and netmask.
643 struct ifaliasreq ifra;
645 strncpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
646 SET_SA_FAMILY(ifra.ifra_addr, AF_INET);
647 ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;
648 SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);
649 ((struct sockaddr_in *) &ifra.ifra_broadaddr)->sin_addr.s_addr = h;
651 SET_SA_FAMILY(ifra.ifra_mask, AF_INET);
652 ((struct sockaddr_in *) &ifra.ifra_mask)->sin_addr.s_addr = m;
654 BZERO(&ifra.ifra_mask, sizeof(ifra.ifra_mask));
655 if (ioctl(sockfd, SIOCAIFADDR, (caddr_t) &ifra) < 0) {
656 if (errno != EEXIST) {
657 syslog(LOG_ERR, "ioctl(SIOCAIFADDR): %m");
660 syslog(LOG_WARNING, "ioctl(SIOCAIFADDR): Address already exists");
666 * cifaddr - Clear the interface IP addresses, and delete routes
667 * through the interface if possible.
674 struct ifaliasreq ifra;
676 strncpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
677 SET_SA_FAMILY(ifra.ifra_addr, AF_INET);
678 ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;
679 SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);
680 ((struct sockaddr_in *) &ifra.ifra_broadaddr)->sin_addr.s_addr = h;
681 BZERO(&ifra.ifra_mask, sizeof(ifra.ifra_mask));
682 if (ioctl(sockfd, SIOCDIFADDR, (caddr_t) &ifra) < 0) {
683 syslog(LOG_WARNING, "ioctl(SIOCDIFADDR): %m");
690 * sifdefaultroute - assign a default route through the address given.
693 sifdefaultroute(u, g)
697 return dodefaultroute(g, 's');
701 * cifdefaultroute - delete a default route through the address given.
704 cifdefaultroute(u, g)
708 return dodefaultroute(g, 'c');
712 * dodefaultroute - talk to a routing socket to add/delete a default route.
715 dodefaultroute(g, cmd)
721 struct rt_msghdr hdr;
722 struct sockaddr_in dst;
723 struct sockaddr_in gway;
724 struct sockaddr_in mask;
727 if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
728 syslog(LOG_ERR, "%cifdefaultroute: opening routing socket: %m", cmd);
732 memset(&rtmsg, 0, sizeof(rtmsg));
733 rtmsg.hdr.rtm_type = cmd == 's'? RTM_ADD: RTM_DELETE;
734 rtmsg.hdr.rtm_flags = RTF_UP | RTF_GATEWAY;
735 rtmsg.hdr.rtm_version = RTM_VERSION;
736 rtmsg.hdr.rtm_seq = ++rtm_seq;
737 rtmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
738 rtmsg.dst.sin_len = sizeof(rtmsg.dst);
739 rtmsg.dst.sin_family = AF_INET;
740 rtmsg.gway.sin_len = sizeof(rtmsg.gway);
741 rtmsg.gway.sin_family = AF_INET;
742 rtmsg.gway.sin_addr.s_addr = g;
743 rtmsg.mask.sin_len = sizeof(rtmsg.dst);
744 rtmsg.mask.sin_family = AF_INET;
746 rtmsg.hdr.rtm_msglen = sizeof(rtmsg);
747 if (write(routes, &rtmsg, sizeof(rtmsg)) < 0) {
748 syslog(LOG_ERR, "%s default route: %m", cmd=='s'? "add": "delete");
760 * sifproxyarp - Make a proxy ARP entry for the peer.
763 struct rt_msghdr hdr;
764 struct sockaddr_inarp dst;
765 struct sockaddr_dl hwa;
769 static int arpmsg_valid;
772 sifproxyarp(unit, hisaddr)
780 * Get the hardware address of an interface on the same subnet
781 * as our local address.
783 memset(&arpmsg, 0, sizeof(arpmsg));
784 if (!get_ether_addr(hisaddr, &arpmsg.hwa)) {
785 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
789 if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
790 syslog(LOG_ERR, "sifproxyarp: opening routing socket: %m");
794 arpmsg.hdr.rtm_type = RTM_ADD;
795 arpmsg.hdr.rtm_flags = RTF_ANNOUNCE | RTF_HOST | RTF_STATIC;
796 arpmsg.hdr.rtm_version = RTM_VERSION;
797 arpmsg.hdr.rtm_seq = ++rtm_seq;
798 arpmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY;
799 arpmsg.hdr.rtm_inits = RTV_EXPIRE;
800 arpmsg.dst.sin_len = sizeof(struct sockaddr_inarp);
801 arpmsg.dst.sin_family = AF_INET;
802 arpmsg.dst.sin_addr.s_addr = hisaddr;
803 arpmsg.dst.sin_other = SIN_PROXY;
805 arpmsg.hdr.rtm_msglen = (char *) &arpmsg.hwa - (char *) &arpmsg
806 + arpmsg.hwa.sdl_len;
807 if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0) {
808 syslog(LOG_ERR, "add proxy arp entry: %m");
819 * cifproxyarp - Delete the proxy ARP entry for the peer.
822 cifproxyarp(unit, hisaddr)
832 arpmsg.hdr.rtm_type = RTM_DELETE;
833 arpmsg.hdr.rtm_seq = ++rtm_seq;
835 if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
836 syslog(LOG_ERR, "sifproxyarp: opening routing socket: %m");
840 if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0) {
841 syslog(LOG_ERR, "delete proxy arp entry: %m");
850 #else /* RTM_VERSION */
853 * sifproxyarp - Make a proxy ARP entry for the peer.
856 sifproxyarp(unit, hisaddr)
860 struct arpreq arpreq;
862 struct sockaddr_dl sdl;
866 BZERO(&arpreq, sizeof(arpreq));
869 * Get the hardware address of an interface on the same subnet
870 * as our local address.
872 if (!get_ether_addr(hisaddr, &dls.sdl)) {
873 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
877 arpreq.arp_ha.sa_len = sizeof(struct sockaddr);
878 arpreq.arp_ha.sa_family = AF_UNSPEC;
879 BCOPY(LLADDR(&dls.sdl), arpreq.arp_ha.sa_data, dls.sdl.sdl_alen);
880 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
881 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
882 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
883 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
884 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
892 * cifproxyarp - Delete the proxy ARP entry for the peer.
895 cifproxyarp(unit, hisaddr)
899 struct arpreq arpreq;
901 BZERO(&arpreq, sizeof(arpreq));
902 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
903 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
904 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
905 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m");
910 #endif /* RTM_VERSION */
914 * get_ether_addr - get the hardware address of an interface on the
915 * the same subnet as ipaddr.
920 get_ether_addr(ipaddr, hwaddr)
922 struct sockaddr_dl *hwaddr;
924 struct ifreq *ifr, *ifend, *ifp;
926 struct sockaddr_dl *dla;
929 struct ifreq ifs[MAX_IFS];
931 ifc.ifc_len = sizeof(ifs);
933 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
934 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
939 * Scan through looking for an interface with an Internet
940 * address on the same subnet as `ipaddr'.
942 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
943 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
944 ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len)) {
945 if (ifr->ifr_addr.sa_family == AF_INET) {
946 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
947 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
949 * Check that the interface is up, and not point-to-point
952 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
954 if ((ifreq.ifr_flags &
955 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
956 != (IFF_UP|IFF_BROADCAST))
959 * Get its netmask and check that it's on the right subnet.
961 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
963 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
964 if ((ipaddr & mask) != (ina & mask))
973 syslog(LOG_INFO, "found interface %s for proxy arp", ifr->ifr_name);
976 * Now scan through again looking for a link-level address
977 * for this interface.
980 for (ifr = ifc.ifc_req; ifr < ifend; ) {
981 if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0
982 && ifr->ifr_addr.sa_family == AF_LINK) {
984 * Found the link-level address - copy it out
986 dla = (struct sockaddr_dl *) &ifr->ifr_addr;
987 BCOPY(dla, hwaddr, dla->sdl_len);
990 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len);
997 * Return user specified netmask, modified by any mask we might determine
998 * for address `addr' (in network byte order).
999 * Here we scan through the system's list of interfaces, looking for
1000 * any non-point-to-point interfaces which might appear to be on the same
1001 * network as `addr'. If we find any, we OR in their netmask to the
1002 * user-specified netmask.
1008 u_int32_t mask, nmask, ina;
1009 struct ifreq *ifr, *ifend, ifreq;
1011 struct ifreq ifs[MAX_IFS];
1014 if (IN_CLASSA(addr)) /* determine network mask for address class */
1015 nmask = IN_CLASSA_NET;
1016 else if (IN_CLASSB(addr))
1017 nmask = IN_CLASSB_NET;
1019 nmask = IN_CLASSC_NET;
1020 /* class D nets are disallowed by bad_ip_adrs */
1021 mask = netmask | htonl(nmask);
1024 * Scan through the system's network interfaces.
1026 ifc.ifc_len = sizeof(ifs);
1028 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1029 syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m");
1032 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1033 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1034 ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len)) {
1036 * Check the interface's internet address.
1038 if (ifr->ifr_addr.sa_family != AF_INET)
1040 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1041 if ((ntohl(ina) & nmask) != (addr & nmask))
1044 * Check that the interface is up, and not point-to-point or loopback.
1046 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1047 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1049 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1053 * Get its netmask and OR it into our mask.
1055 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1057 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1064 * lock - create a lock file for the named lock device
1066 #define LOCK_PREFIX "/var/spool/lock/LCK.."
1072 char hdb_lock_buffer[12];
1076 if ((p = strrchr(dev, '/')) != NULL)
1078 lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1079 if (lock_file == NULL)
1080 novm("lock file name");
1081 strcat(strcpy(lock_file, LOCK_PREFIX), dev);
1083 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1085 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1086 /* Read the lock file to find out who has the device locked */
1087 n = read(fd, hdb_lock_buffer, 11);
1089 hdb_lock_buffer[n] = 0;
1090 pid = atoi(hdb_lock_buffer);
1093 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1096 if (kill(pid, 0) == -1 && errno == ESRCH) {
1097 /* pid no longer exists - remove the lock file */
1098 if (unlink(lock_file) == 0) {
1100 syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1104 syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
1107 syslog(LOG_NOTICE, "Device %s is locked by pid %d",
1112 syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1118 sprintf(hdb_lock_buffer, "%10d\n", getpid());
1119 write(fd, hdb_lock_buffer, 11);
1126 * unlock - remove our lockfile