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.17 1995/04/27 00:33:39 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 */
61 static char *lock_file; /* name of lock file created */
63 int sockfd; /* socket for doing interface ioctls */
66 * sys_init - System-dependent initialization.
71 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
72 setlogmask(LOG_UPTO(LOG_INFO));
74 setlogmask(LOG_UPTO(LOG_DEBUG));
76 /* Get an internet socket for doing socket ioctl's on. */
77 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
78 syslog(LOG_ERR, "Couldn't create IP socket: %m");
84 * note_debug_level - note a change in the debug level.
90 syslog(LOG_INFO, "Debug turned ON, Level %d", debug);
91 setlogmask(LOG_UPTO(LOG_DEBUG));
93 setlogmask(LOG_UPTO(LOG_WARNING));
98 * ppp_available - check whether the system has any ppp interfaces
99 * (in fact we check whether we can do an ioctl on ppp0).
107 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
108 return 1; /* can't tell - maybe we're not root */
110 strncpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
111 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
118 * establish_ppp - Turn the serial port into a ppp interface.
123 int pppdisc = PPPDISC;
126 if (ioctl(fd, TIOCGETD, &initdisc) < 0) {
127 syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
130 if (ioctl(fd, TIOCSETD, &pppdisc) < 0) {
131 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
136 * Find out which interface we were given.
138 if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {
139 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
144 * Enable debug in the driver if requested.
147 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
148 syslog(LOG_WARNING, "ioctl (PPPIOCGFLAGS): %m");
150 x |= (kdebugflag & 0xFF) * SC_DEBUG;
151 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
152 syslog(LOG_WARNING, "ioctl(PPPIOCSFLAGS): %m");
159 * disestablish_ppp - Restore the serial port to normal operation.
160 * This shouldn't call die() because it's called from die().
170 * Check whether the link seems not to be 8-bit clean.
172 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
174 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
176 s = "bit 7 set to 1";
179 s = "bit 7 set to 0";
189 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
190 syslog(LOG_WARNING, "All received characters had %s", s);
193 if (ioctl(fd, TIOCSETD, &initdisc) < 0)
194 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
200 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
201 * at the requested speed, etc. If `local' is true, set CLOCAL
202 * regardless of whether the modem option was specified.
204 * For *BSD, we assume that speed_t values numerically equal bits/second.
206 set_up_tty(fd, local)
211 if (tcgetattr(fd, &tios) < 0) {
212 syslog(LOG_ERR, "tcgetattr: %m");
219 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
221 tios.c_cflag |= CRTSCTS;
222 else if (crtscts < 0)
223 tios.c_cflag &= ~CRTSCTS;
225 tios.c_cflag |= CS8 | CREAD | HUPCL;
227 tios.c_cflag |= CLOCAL;
228 tios.c_iflag = IGNBRK | IGNPAR;
232 tios.c_cc[VTIME] = 0;
235 tios.c_iflag |= IXOFF;
236 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
237 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
241 cfsetospeed(&tios, inspeed);
242 cfsetispeed(&tios, inspeed);
244 inspeed = cfgetospeed(&tios);
246 * We can't proceed if the serial port speed is 0,
247 * since that implies that the serial port is disabled.
250 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
257 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
258 syslog(LOG_ERR, "tcsetattr: %m");
266 * restore_tty - restore the terminal to the saved settings.
272 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
274 syslog(LOG_WARNING, "tcsetattr: %m");
280 * setdtr - control the DTR line on the serial port.
281 * This is called from die(), so it shouldn't call die().
286 int modembits = TIOCM_DTR;
288 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
293 * output - Output PPP packet.
302 MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
304 log_packet(p, len, "sent ");
306 if (write(fd, p, len) < 0) {
307 syslog(LOG_ERR, "write: %m");
314 * wait_input - wait until there is data available on fd,
315 * for the length of time specified by *timo (indefinite
319 struct timeval *timo;
326 n = select(fd+1, &ready, NULL, &ready, timo);
327 if (n < 0 && errno != EINTR) {
328 syslog(LOG_ERR, "select: %m");
335 * read_packet - get a PPP packet from the serial device.
343 if ((len = read(fd, buf, PPP_MTU + PPP_HDRLEN)) < 0) {
344 if (errno == EWOULDBLOCK || errno == EINTR) {
345 MAINDEBUG((LOG_DEBUG, "read(fd): %m"));
348 syslog(LOG_ERR, "read(fd): %m");
356 * ppp_send_config - configure the transmit characteristics of
360 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
368 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
370 if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
371 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
375 if (ioctl(fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
376 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
380 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
381 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
384 x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
385 x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
386 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
387 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
394 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
397 ppp_set_xaccm(unit, accm)
401 if (ioctl(fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
402 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
407 * ppp_recv_config - configure the receive-side characteristics of
411 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
418 if (ioctl(fd, PPPIOCSMRU, (caddr_t) &mru) < 0) {
419 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
422 if (ioctl(fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
423 syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m");
426 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
427 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
430 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
431 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
432 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
438 * ccp_test - ask kernel whether a given compression method
439 * is acceptable for use.
441 ccp_test(unit, opt_ptr, opt_len, for_transmit)
442 int unit, opt_len, for_transmit;
445 struct ppp_option_data data;
448 data.length = opt_len;
449 data.transmit = for_transmit;
450 return ioctl(fd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0;
454 * ccp_flags_set - inform kernel about the current state of CCP.
457 ccp_flags_set(unit, isopen, isup)
458 int unit, isopen, isup;
462 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
463 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
466 x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
467 x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
468 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
469 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
473 * ccp_fatal_error - returns 1 if decompression was disabled as a
474 * result of an error detected after decompression of a packet,
475 * 0 otherwise. This is necessary because of patent nonsense.
478 ccp_fatal_error(unit)
483 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
484 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
487 return x & SC_DC_FERROR;
491 * sifvjcomp - config tcp header compression
494 sifvjcomp(u, vjcomp, cidcomp, maxcid)
495 int u, vjcomp, cidcomp, maxcid;
499 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
500 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
503 x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
504 x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
505 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
506 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
509 if (ioctl(fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
510 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
517 * sifup - Config the interface up and enable IP packets to pass.
520 #define SC_ENABLE_IP 0x100 /* compat for old versions of kernel code */
531 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
532 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
533 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
536 ifr.ifr_flags |= IFF_UP;
537 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
538 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
541 npi.protocol = PPP_IP;
542 npi.mode = NPMODE_PASS;
543 if (ioctl(fd, PPPIOCSNPMODE, &npi) < 0) {
544 if (errno != ENOTTY) {
545 syslog(LOG_ERR, "ioctl(PPPIOCSNPMODE): %m");
548 /* for backwards compatibility */
549 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
550 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
554 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
555 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
563 * sifdown - Config the interface down and disable IP.
575 npi.protocol = PPP_IP;
576 npi.mode = NPMODE_ERROR;
577 if (ioctl(fd, PPPIOCSNPMODE, (caddr_t) &npi) < 0) {
578 if (errno != ENOTTY) {
579 syslog(LOG_ERR, "ioctl(PPPIOCSNPMODE): %m");
582 /* backwards compatibility */
583 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
584 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
588 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
589 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
596 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
597 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
598 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
601 ifr.ifr_flags &= ~IFF_UP;
602 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
603 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
611 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
614 #define SET_SA_FAMILY(addr, family) \
615 BZERO((char *) &(addr), sizeof(addr)); \
616 addr.sa_family = (family); \
617 addr.sa_len = sizeof(addr);
620 * sifaddr - Config the interface IP addresses and netmask.
627 struct ifaliasreq ifra;
629 strncpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
630 SET_SA_FAMILY(ifra.ifra_addr, AF_INET);
631 ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;
632 SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);
633 ((struct sockaddr_in *) &ifra.ifra_broadaddr)->sin_addr.s_addr = h;
635 SET_SA_FAMILY(ifra.ifra_mask, AF_INET);
636 ((struct sockaddr_in *) &ifra.ifra_mask)->sin_addr.s_addr = m;
638 BZERO(&ifra.ifra_mask, sizeof(ifra.ifra_mask));
639 if (ioctl(sockfd, SIOCAIFADDR, (caddr_t) &ifra) < 0) {
640 if (errno != EEXIST) {
641 syslog(LOG_ERR, "ioctl(SIOCAIFADDR): %m");
644 syslog(LOG_WARNING, "ioctl(SIOCAIFADDR): Address already exists");
650 * cifaddr - Clear the interface IP addresses, and delete routes
651 * through the interface if possible.
658 struct ifaliasreq ifra;
660 strncpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
661 SET_SA_FAMILY(ifra.ifra_addr, AF_INET);
662 ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;
663 SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);
664 ((struct sockaddr_in *) &ifra.ifra_broadaddr)->sin_addr.s_addr = h;
665 BZERO(&ifra.ifra_mask, sizeof(ifra.ifra_mask));
666 if (ioctl(sockfd, SIOCDIFADDR, (caddr_t) &ifra) < 0) {
667 syslog(LOG_WARNING, "ioctl(SIOCDIFADDR): %m");
674 * sifdefaultroute - assign a default route through the address given.
677 sifdefaultroute(u, g)
681 return dodefaultroute(g, 's');
685 * cifdefaultroute - delete a default route through the address given.
688 cifdefaultroute(u, g)
692 return dodefaultroute(g, 'c');
696 * dodefaultroute - talk to a routing socket to add/delete a default route.
699 dodefaultroute(g, cmd)
705 struct rt_msghdr hdr;
706 struct sockaddr_in dst;
707 struct sockaddr_in gway;
708 struct sockaddr_in mask;
711 if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
712 syslog(LOG_ERR, "%cifdefaultroute: opening routing socket: %m", cmd);
716 memset(&rtmsg, 0, sizeof(rtmsg));
717 rtmsg.hdr.rtm_type = cmd == 's'? RTM_ADD: RTM_DELETE;
718 rtmsg.hdr.rtm_flags = RTF_UP | RTF_GATEWAY;
719 rtmsg.hdr.rtm_version = RTM_VERSION;
720 rtmsg.hdr.rtm_seq = ++rtm_seq;
721 rtmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
722 rtmsg.dst.sin_len = sizeof(rtmsg.dst);
723 rtmsg.dst.sin_family = AF_INET;
724 rtmsg.gway.sin_len = sizeof(rtmsg.gway);
725 rtmsg.gway.sin_family = AF_INET;
726 rtmsg.gway.sin_addr.s_addr = g;
727 rtmsg.mask.sin_len = sizeof(rtmsg.dst);
728 rtmsg.mask.sin_family = AF_INET;
730 rtmsg.hdr.rtm_msglen = sizeof(rtmsg);
731 if (write(routes, &rtmsg, sizeof(rtmsg)) < 0) {
732 syslog(LOG_ERR, "%s default route: %m", cmd=='s'? "add": "delete");
744 * sifproxyarp - Make a proxy ARP entry for the peer.
747 struct rt_msghdr hdr;
748 struct sockaddr_inarp dst;
749 struct sockaddr_dl hwa;
753 static int arpmsg_valid;
756 sifproxyarp(unit, hisaddr)
764 * Get the hardware address of an interface on the same subnet
765 * as our local address.
767 memset(&arpmsg, 0, sizeof(arpmsg));
768 if (!get_ether_addr(hisaddr, &arpmsg.hwa)) {
769 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
773 if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
774 syslog(LOG_ERR, "sifproxyarp: opening routing socket: %m");
778 arpmsg.hdr.rtm_type = RTM_ADD;
779 arpmsg.hdr.rtm_flags = RTF_ANNOUNCE | RTF_HOST | RTF_STATIC;
780 arpmsg.hdr.rtm_version = RTM_VERSION;
781 arpmsg.hdr.rtm_seq = ++rtm_seq;
782 arpmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY;
783 arpmsg.hdr.rtm_inits = RTV_EXPIRE;
784 arpmsg.dst.sin_len = sizeof(struct sockaddr_inarp);
785 arpmsg.dst.sin_family = AF_INET;
786 arpmsg.dst.sin_addr.s_addr = hisaddr;
787 arpmsg.dst.sin_other = SIN_PROXY;
789 arpmsg.hdr.rtm_msglen = (char *) &arpmsg.hwa - (char *) &arpmsg
790 + arpmsg.hwa.sdl_len;
791 if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0) {
792 syslog(LOG_ERR, "add proxy arp entry: %m");
803 * cifproxyarp - Delete the proxy ARP entry for the peer.
806 cifproxyarp(unit, hisaddr)
816 arpmsg.hdr.rtm_type = RTM_DELETE;
817 arpmsg.hdr.rtm_seq = ++rtm_seq;
819 if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
820 syslog(LOG_ERR, "sifproxyarp: opening routing socket: %m");
824 if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0) {
825 syslog(LOG_ERR, "delete proxy arp entry: %m");
834 #else /* RTM_VERSION */
837 * sifproxyarp - Make a proxy ARP entry for the peer.
840 sifproxyarp(unit, hisaddr)
844 struct arpreq arpreq;
846 struct sockaddr_dl sdl;
850 BZERO(&arpreq, sizeof(arpreq));
853 * Get the hardware address of an interface on the same subnet
854 * as our local address.
856 if (!get_ether_addr(hisaddr, &dls.sdl)) {
857 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
861 arpreq.arp_ha.sa_len = sizeof(struct sockaddr);
862 arpreq.arp_ha.sa_family = AF_UNSPEC;
863 BCOPY(LLADDR(&dls.sdl), arpreq.arp_ha.sa_data, dls.sdl.sdl_alen);
864 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
865 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
866 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
867 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
868 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
876 * cifproxyarp - Delete the proxy ARP entry for the peer.
879 cifproxyarp(unit, hisaddr)
883 struct arpreq arpreq;
885 BZERO(&arpreq, sizeof(arpreq));
886 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
887 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
888 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
889 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m");
894 #endif /* RTM_VERSION */
898 * get_ether_addr - get the hardware address of an interface on the
899 * the same subnet as ipaddr.
904 get_ether_addr(ipaddr, hwaddr)
906 struct sockaddr_dl *hwaddr;
908 struct ifreq *ifr, *ifend, *ifp;
910 struct sockaddr_dl *dla;
913 struct ifreq ifs[MAX_IFS];
915 ifc.ifc_len = sizeof(ifs);
917 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
918 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
923 * Scan through looking for an interface with an Internet
924 * address on the same subnet as `ipaddr'.
926 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
927 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
928 ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len)) {
929 if (ifr->ifr_addr.sa_family == AF_INET) {
930 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
931 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
933 * Check that the interface is up, and not point-to-point
936 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
938 if ((ifreq.ifr_flags &
939 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
940 != (IFF_UP|IFF_BROADCAST))
943 * Get its netmask and check that it's on the right subnet.
945 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
947 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
948 if ((ipaddr & mask) != (ina & mask))
957 syslog(LOG_INFO, "found interface %s for proxy arp", ifr->ifr_name);
960 * Now scan through again looking for a link-level address
961 * for this interface.
964 for (ifr = ifc.ifc_req; ifr < ifend; ) {
965 if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0
966 && ifr->ifr_addr.sa_family == AF_LINK) {
968 * Found the link-level address - copy it out
970 dla = (struct sockaddr_dl *) &ifr->ifr_addr;
971 BCOPY(dla, hwaddr, dla->sdl_len);
974 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len);
981 * Return user specified netmask, modified by any mask we might determine
982 * for address `addr' (in network byte order).
983 * Here we scan through the system's list of interfaces, looking for
984 * any non-point-to-point interfaces which might appear to be on the same
985 * network as `addr'. If we find any, we OR in their netmask to the
986 * user-specified netmask.
992 u_int32_t mask, nmask, ina;
993 struct ifreq *ifr, *ifend, ifreq;
995 struct ifreq ifs[MAX_IFS];
998 if (IN_CLASSA(addr)) /* determine network mask for address class */
999 nmask = IN_CLASSA_NET;
1000 else if (IN_CLASSB(addr))
1001 nmask = IN_CLASSB_NET;
1003 nmask = IN_CLASSC_NET;
1004 /* class D nets are disallowed by bad_ip_adrs */
1005 mask = netmask | htonl(nmask);
1008 * Scan through the system's network interfaces.
1010 ifc.ifc_len = sizeof(ifs);
1012 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1013 syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m");
1016 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1017 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1018 ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len)) {
1020 * Check the interface's internet address.
1022 if (ifr->ifr_addr.sa_family != AF_INET)
1024 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1025 if ((ntohl(ina) & nmask) != (addr & nmask))
1028 * Check that the interface is up, and not point-to-point or loopback.
1030 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1031 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1033 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1037 * Get its netmask and OR it into our mask.
1039 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1041 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1048 * lock - create a lock file for the named lock device
1050 #define LOCK_PREFIX "/var/spool/lock/LCK.."
1056 char hdb_lock_buffer[12];
1060 if ((p = strrchr(dev, '/')) != NULL)
1062 lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1063 if (lock_file == NULL)
1064 novm("lock file name");
1065 strcat(strcpy(lock_file, LOCK_PREFIX), dev);
1067 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1069 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1070 /* Read the lock file to find out who has the device locked */
1071 n = read(fd, hdb_lock_buffer, 11);
1073 hdb_lock_buffer[n] = 0;
1074 pid = atoi(hdb_lock_buffer);
1077 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1080 if (kill(pid, 0) == -1 && errno == ESRCH) {
1081 /* pid no longer exists - remove the lock file */
1082 if (unlink(lock_file) == 0) {
1084 syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1088 syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
1091 syslog(LOG_NOTICE, "Device %s is locked by pid %d",
1096 syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1102 sprintf(hdb_lock_buffer, "%10d\n", getpid());
1103 write(fd, hdb_lock_buffer, 11);
1110 * unlock - remove our lockfile