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.19.2.1 1995/06/01 07:01: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 */
56 static int ppp_fd = -1; /* fd which is set to PPP discipline */
59 static int restore_term; /* 1 => we've munged the terminal */
60 static struct termios inittermios; /* Initial TTY termios */
62 static char *lock_file; /* name of lock file created */
64 static int loop_slave = -1;
65 static int loop_master;
66 static char loop_name[20];
68 int sockfd; /* socket for doing interface ioctls */
71 * sys_init - System-dependent initialization.
76 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
77 setlogmask(LOG_UPTO(LOG_INFO));
79 setlogmask(LOG_UPTO(LOG_DEBUG));
81 /* Get an internet socket for doing socket ioctl's on. */
82 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
83 syslog(LOG_ERR, "Couldn't create IP socket: %m");
89 * sys_close - Clean up in a child process before execing.
100 * note_debug_level - note a change in the debug level.
106 syslog(LOG_INFO, "Debug turned ON, Level %d", debug);
107 setlogmask(LOG_UPTO(LOG_DEBUG));
109 setlogmask(LOG_UPTO(LOG_WARNING));
114 * ppp_available - check whether the system has any ppp interfaces
115 * (in fact we check whether we can do an ioctl on ppp0).
123 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
124 return 1; /* can't tell */
126 strncpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
127 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
134 * establish_ppp - Turn the serial port (or loopback) into a ppp interface.
140 int pppdisc = PPPDISC;
143 if (ioctl(fd, TIOCGETD, &initdisc) < 0) {
144 syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
147 if (ioctl(fd, TIOCSETD, &pppdisc) < 0) {
148 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
153 * Find out which interface we were given.
155 if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {
156 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
162 * Enable debug in the driver if requested.
165 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
166 syslog(LOG_WARNING, "ioctl (PPPIOCGFLAGS): %m");
168 x |= (kdebugflag & 0xFF) * SC_DEBUG;
169 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
170 syslog(LOG_WARNING, "ioctl(PPPIOCSFLAGS): %m");
177 * transfer_ppp - make the device on fd `fd' take over the PPP interface
178 * unit that we are using.
187 return; /* we can't get here */
190 * Prime the old ppp device to relinquish the unit.
192 if (ioctl(ppp_fd, PPPIOCXFERUNIT, 0) < 0) {
193 syslog(LOG_ERR, "ioctl(transfer ppp unit): %m");
198 * Save the old line discipline of fd, and set it to PPP.
200 if (ioctl(fd, TIOCGETD, &prevdisc) < 0) {
201 syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
205 if (ioctl(fd, TIOCSETD, &x) < 0) {
206 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
211 * Set the old ppp device back to its previous discipline.
213 ioctl(ppp_fd, TIOCSETD, &initdisc);
217 * Check that we got the same unit again.
219 if (ioctl(fd, PPPIOCGUNIT, &x) < 0) {
220 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
224 syslog(LOG_ERR, "transfer_ppp failed: wanted unit %d, got %d",
234 * disestablish_ppp - Restore the serial port to normal operation.
235 * This shouldn't call die() because it's called from die().
241 if (fd == ppp_fd && fd >= 0 && initdisc >= 0) {
242 if (ioctl(fd, TIOCSETD, &initdisc) < 0)
243 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
250 * Check whether the link seems not to be 8-bit clean.
258 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
260 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
262 s = "bit 7 set to 1";
265 s = "bit 7 set to 0";
275 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
276 syslog(LOG_WARNING, "All received characters had %s", s);
282 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
283 * at the requested speed, etc. If `local' is true, set CLOCAL
284 * regardless of whether the modem option was specified.
286 * For *BSD, we assume that speed_t values numerically equal bits/second.
288 set_up_tty(fd, local)
293 if (tcgetattr(fd, &tios) < 0) {
294 syslog(LOG_ERR, "tcgetattr: %m");
301 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
303 tios.c_cflag |= CRTSCTS;
304 else if (crtscts < 0)
305 tios.c_cflag &= ~CRTSCTS;
307 tios.c_cflag |= CS8 | CREAD | HUPCL;
309 tios.c_cflag |= CLOCAL;
310 tios.c_iflag = IGNBRK | IGNPAR;
314 tios.c_cc[VTIME] = 0;
317 tios.c_iflag |= IXOFF;
318 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
319 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
323 cfsetospeed(&tios, inspeed);
324 cfsetispeed(&tios, inspeed);
326 inspeed = cfgetospeed(&tios);
328 * We can't proceed if the serial port speed is 0,
329 * since that implies that the serial port is disabled.
332 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
339 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
340 syslog(LOG_ERR, "tcsetattr: %m");
348 * restore_tty - restore the terminal to the saved settings.
354 if (!default_device) {
356 * Turn off echoing, because otherwise we can get into
357 * a loop with the tty and the modem echoing to each other.
358 * We presume we are the sole user of this tty device, so
359 * when we close it, it will revert to its defaults anyway.
361 inittermios.c_lflag &= ~(ECHO | ECHONL);
363 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
365 syslog(LOG_WARNING, "tcsetattr: %m");
371 * setdtr - control the DTR line on the serial port.
372 * This is called from die(), so it shouldn't call die().
377 int modembits = TIOCM_DTR;
379 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
384 * open_loopback - open the device we use for getting packets
385 * in demand mode. Here we use a pty.
393 if (openpty(&loop_master, &loop_slave, loop_name, NULL, NULL) < 0) {
394 syslog(LOG_ERR, "No free pty for loopback");
397 SYSDEBUG((LOG_DEBUG, "using %s for loopback", loop_name));
398 if (tcgetattr(loop_slave, &tios) == 0) {
399 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB);
400 tios.c_cflag |= CS8 | CREAD;
401 tios.c_iflag = IGNPAR;
404 if (tcsetattr(loop_slave, TCSAFLUSH, &tios) < 0)
405 syslog(LOG_WARNING, "couldn't set attributes on loopback: %m");
407 if ((flags = fcntl(loop_slave, F_GETFL)) != -1)
408 if (fcntl(loop_slave, F_SETFL, flags | O_NONBLOCK) == -1)
409 syslog(LOG_WARNING, "couldn't set loopback to nonblock: %m");
410 if ((flags = fcntl(loop_master, F_GETFL)) != -1)
411 if (fcntl(loop_master, F_SETFL, flags | O_NONBLOCK) == -1)
412 syslog(LOG_WARNING, "couldn't set loopback(m) to nonblock: %m");
418 * output - Output PPP packet.
427 SYSDEBUG((LOG_WARNING, "output: unit != 0!"));
429 log_packet(p, len, "sent ");
431 if (write(fd, p, len) < 0) {
432 syslog(LOG_ERR, "write: %m");
439 * wait_input - wait until there is data available on fd,
440 * for the length of time specified by *timo (indefinite
444 struct timeval *timo;
451 n = select(fd+1, &ready, NULL, &ready, timo);
452 if (n < 0 && errno != EINTR) {
453 syslog(LOG_ERR, "select: %m");
460 * wait_loop_output - wait until there is data available on the,
461 * loopback, for the length of time specified by *timo (indefinite
464 wait_loop_output(timo)
465 struct timeval *timo;
471 FD_SET(loop_master, &ready);
472 n = select(loop_master + 1, &ready, NULL, &ready, timo);
473 if (n < 0 && errno != EINTR) {
474 syslog(LOG_ERR, "select: %m");
481 * wait_time - wait for a given length of time or until a
482 * signal is received.
485 struct timeval *timo;
489 n = select(0, NULL, NULL, NULL, timo);
490 if (n < 0 && errno != EINTR) {
491 syslog(LOG_ERR, "select: %m");
498 * read_packet - get a PPP packet from the serial device.
506 if ((len = read(fd, buf, PPP_MTU + PPP_HDRLEN)) < 0) {
507 if (errno == EWOULDBLOCK || errno == EINTR)
509 syslog(LOG_ERR, "read(fd): %m");
517 * read_loop_output - get characters from the "bottom" of the loopback.
520 read_loop_output(buf, maxlen)
526 if ((len = read(loop_master, buf, maxlen)) < 0) {
527 if (errno == EWOULDBLOCK)
529 syslog(LOG_ERR, "read from loopback: %m");
531 } else if (len == 0) {
532 syslog(LOG_ERR, "eof on loopback");
540 * ppp_send_config - configure the transmit characteristics of
544 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
552 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
554 if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
555 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
559 if (ioctl(fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
560 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
564 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
565 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
568 x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
569 x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
570 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
571 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
578 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
581 ppp_set_xaccm(unit, accm)
585 if (ioctl(fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
586 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
591 * ppp_recv_config - configure the receive-side characteristics of
595 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
602 if (ioctl(fd, PPPIOCSMRU, (caddr_t) &mru) < 0) {
603 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
606 if (ioctl(fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
607 syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m");
610 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
611 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
614 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
615 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
616 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
622 * ccp_test - ask kernel whether a given compression method
623 * is acceptable for use.
625 ccp_test(unit, opt_ptr, opt_len, for_transmit)
626 int unit, opt_len, for_transmit;
629 struct ppp_option_data data;
632 data.length = opt_len;
633 data.transmit = for_transmit;
634 return ioctl(fd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0;
638 * ccp_flags_set - inform kernel about the current state of CCP.
641 ccp_flags_set(unit, isopen, isup)
642 int unit, isopen, isup;
646 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
647 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
650 x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
651 x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
652 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
653 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
657 * ccp_fatal_error - returns 1 if decompression was disabled as a
658 * result of an error detected after decompression of a packet,
659 * 0 otherwise. This is necessary because of patent nonsense.
662 ccp_fatal_error(unit)
667 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
668 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
671 return x & SC_DC_FERROR;
675 * get_idle_time - return how long the link has been idle.
682 return ioctl(fd, PPPIOCGIDLE, ip) >= 0;
687 * sifvjcomp - config tcp header compression
690 sifvjcomp(u, vjcomp, cidcomp, maxcid)
691 int u, vjcomp, cidcomp, maxcid;
695 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
696 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
699 x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
700 x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
701 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
702 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
705 if (ioctl(fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
706 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
713 * sifup - Config the interface up and enable IP packets to pass.
722 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
723 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
724 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
727 ifr.ifr_flags |= IFF_UP;
728 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
729 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
732 npi.protocol = PPP_IP;
733 npi.mode = NPMODE_PASS;
734 if (ioctl(fd, PPPIOCSNPMODE, &npi) < 0) {
735 syslog(LOG_ERR, "ioctl(set IP mode to PASS): %m");
742 * sifnpmode - Set the mode for handling packets for a given NP.
745 sifnpmode(u, proto, mode)
752 npi.protocol = proto;
754 if (ioctl(fd, PPPIOCSNPMODE, &npi) < 0) {
755 syslog(LOG_ERR, "ioctl(set NP %d mode to %d): %m", proto, mode);
762 * sifdown - Config the interface down and disable IP.
773 npi.protocol = PPP_IP;
774 npi.mode = NPMODE_ERROR;
775 ioctl(ppp_fd, PPPIOCSNPMODE, (caddr_t) &npi);
776 /* ignore errors, because ppp_fd might have been closed by now. */
778 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
779 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
780 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
783 ifr.ifr_flags &= ~IFF_UP;
784 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
785 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
793 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
796 #define SET_SA_FAMILY(addr, family) \
797 BZERO((char *) &(addr), sizeof(addr)); \
798 addr.sa_family = (family); \
799 addr.sa_len = sizeof(addr);
802 * sifaddr - Config the interface IP addresses and netmask.
809 struct ifaliasreq ifra;
811 strncpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
812 SET_SA_FAMILY(ifra.ifra_addr, AF_INET);
813 ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;
814 SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);
815 ((struct sockaddr_in *) &ifra.ifra_broadaddr)->sin_addr.s_addr = h;
817 SET_SA_FAMILY(ifra.ifra_mask, AF_INET);
818 ((struct sockaddr_in *) &ifra.ifra_mask)->sin_addr.s_addr = m;
820 BZERO(&ifra.ifra_mask, sizeof(ifra.ifra_mask));
821 if (ioctl(sockfd, SIOCAIFADDR, (caddr_t) &ifra) < 0) {
822 if (errno != EEXIST) {
823 syslog(LOG_ERR, "ioctl(SIOCAIFADDR): %m");
826 syslog(LOG_WARNING, "ioctl(SIOCAIFADDR): Address already exists");
832 * cifaddr - Clear the interface IP addresses, and delete routes
833 * through the interface if possible.
840 struct ifaliasreq ifra;
842 strncpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
843 SET_SA_FAMILY(ifra.ifra_addr, AF_INET);
844 ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;
845 SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);
846 ((struct sockaddr_in *) &ifra.ifra_broadaddr)->sin_addr.s_addr = h;
847 BZERO(&ifra.ifra_mask, sizeof(ifra.ifra_mask));
848 if (ioctl(sockfd, SIOCDIFADDR, (caddr_t) &ifra) < 0) {
849 syslog(LOG_WARNING, "ioctl(SIOCDIFADDR): %m");
856 * sifdefaultroute - assign a default route through the address given.
859 sifdefaultroute(u, g)
863 return dodefaultroute(g, 's');
867 * cifdefaultroute - delete a default route through the address given.
870 cifdefaultroute(u, g)
874 return dodefaultroute(g, 'c');
878 * dodefaultroute - talk to a routing socket to add/delete a default route.
881 dodefaultroute(g, cmd)
887 struct rt_msghdr hdr;
888 struct sockaddr_in dst;
889 struct sockaddr_in gway;
890 struct sockaddr_in mask;
893 if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
894 syslog(LOG_ERR, "%cifdefaultroute: opening routing socket: %m", cmd);
898 memset(&rtmsg, 0, sizeof(rtmsg));
899 rtmsg.hdr.rtm_type = cmd == 's'? RTM_ADD: RTM_DELETE;
900 rtmsg.hdr.rtm_flags = RTF_UP | RTF_GATEWAY;
901 rtmsg.hdr.rtm_version = RTM_VERSION;
902 rtmsg.hdr.rtm_seq = ++rtm_seq;
903 rtmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
904 rtmsg.dst.sin_len = sizeof(rtmsg.dst);
905 rtmsg.dst.sin_family = AF_INET;
906 rtmsg.gway.sin_len = sizeof(rtmsg.gway);
907 rtmsg.gway.sin_family = AF_INET;
908 rtmsg.gway.sin_addr.s_addr = g;
909 rtmsg.mask.sin_len = sizeof(rtmsg.dst);
910 rtmsg.mask.sin_family = AF_INET;
912 rtmsg.hdr.rtm_msglen = sizeof(rtmsg);
913 if (write(routes, &rtmsg, sizeof(rtmsg)) < 0) {
914 syslog(LOG_ERR, "%s default route: %m", cmd=='s'? "add": "delete");
926 * sifproxyarp - Make a proxy ARP entry for the peer.
929 struct rt_msghdr hdr;
930 struct sockaddr_inarp dst;
931 struct sockaddr_dl hwa;
935 static int arpmsg_valid;
938 sifproxyarp(unit, hisaddr)
946 * Get the hardware address of an interface on the same subnet
947 * as our local address.
949 memset(&arpmsg, 0, sizeof(arpmsg));
950 if (!get_ether_addr(hisaddr, &arpmsg.hwa)) {
951 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
955 if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
956 syslog(LOG_ERR, "sifproxyarp: opening routing socket: %m");
960 arpmsg.hdr.rtm_type = RTM_ADD;
961 arpmsg.hdr.rtm_flags = RTF_ANNOUNCE | RTF_HOST | RTF_STATIC;
962 arpmsg.hdr.rtm_version = RTM_VERSION;
963 arpmsg.hdr.rtm_seq = ++rtm_seq;
964 arpmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY;
965 arpmsg.hdr.rtm_inits = RTV_EXPIRE;
966 arpmsg.dst.sin_len = sizeof(struct sockaddr_inarp);
967 arpmsg.dst.sin_family = AF_INET;
968 arpmsg.dst.sin_addr.s_addr = hisaddr;
969 arpmsg.dst.sin_other = SIN_PROXY;
971 arpmsg.hdr.rtm_msglen = (char *) &arpmsg.hwa - (char *) &arpmsg
972 + arpmsg.hwa.sdl_len;
973 if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0) {
974 syslog(LOG_ERR, "add proxy arp entry: %m");
985 * cifproxyarp - Delete the proxy ARP entry for the peer.
988 cifproxyarp(unit, hisaddr)
998 arpmsg.hdr.rtm_type = RTM_DELETE;
999 arpmsg.hdr.rtm_seq = ++rtm_seq;
1001 if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
1002 syslog(LOG_ERR, "sifproxyarp: opening routing socket: %m");
1006 if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0) {
1007 syslog(LOG_ERR, "delete proxy arp entry: %m");
1016 #else /* RTM_VERSION */
1019 * sifproxyarp - Make a proxy ARP entry for the peer.
1022 sifproxyarp(unit, hisaddr)
1026 struct arpreq arpreq;
1028 struct sockaddr_dl sdl;
1032 BZERO(&arpreq, sizeof(arpreq));
1035 * Get the hardware address of an interface on the same subnet
1036 * as our local address.
1038 if (!get_ether_addr(hisaddr, &dls.sdl)) {
1039 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
1043 arpreq.arp_ha.sa_len = sizeof(struct sockaddr);
1044 arpreq.arp_ha.sa_family = AF_UNSPEC;
1045 BCOPY(LLADDR(&dls.sdl), arpreq.arp_ha.sa_data, dls.sdl.sdl_alen);
1046 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1047 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1048 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1049 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
1050 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
1058 * cifproxyarp - Delete the proxy ARP entry for the peer.
1061 cifproxyarp(unit, hisaddr)
1065 struct arpreq arpreq;
1067 BZERO(&arpreq, sizeof(arpreq));
1068 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1069 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1070 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1071 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m");
1076 #endif /* RTM_VERSION */
1080 * get_ether_addr - get the hardware address of an interface on the
1081 * the same subnet as ipaddr.
1086 get_ether_addr(ipaddr, hwaddr)
1088 struct sockaddr_dl *hwaddr;
1090 struct ifreq *ifr, *ifend, *ifp;
1091 u_int32_t ina, mask;
1092 struct sockaddr_dl *dla;
1095 struct ifreq ifs[MAX_IFS];
1097 ifc.ifc_len = sizeof(ifs);
1099 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1100 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
1105 * Scan through looking for an interface with an Internet
1106 * address on the same subnet as `ipaddr'.
1108 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1109 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1110 ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len)) {
1111 if (ifr->ifr_addr.sa_family == AF_INET) {
1112 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1113 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1115 * Check that the interface is up, and not point-to-point
1118 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1120 if ((ifreq.ifr_flags &
1121 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1122 != (IFF_UP|IFF_BROADCAST))
1125 * Get its netmask and check that it's on the right subnet.
1127 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1129 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1130 if ((ipaddr & mask) != (ina & mask))
1139 syslog(LOG_INFO, "found interface %s for proxy arp", ifr->ifr_name);
1142 * Now scan through again looking for a link-level address
1143 * for this interface.
1146 for (ifr = ifc.ifc_req; ifr < ifend; ) {
1147 if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0
1148 && ifr->ifr_addr.sa_family == AF_LINK) {
1150 * Found the link-level address - copy it out
1152 dla = (struct sockaddr_dl *) &ifr->ifr_addr;
1153 BCOPY(dla, hwaddr, dla->sdl_len);
1156 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len);
1163 * Return user specified netmask, modified by any mask we might determine
1164 * for address `addr' (in network byte order).
1165 * Here we scan through the system's list of interfaces, looking for
1166 * any non-point-to-point interfaces which might appear to be on the same
1167 * network as `addr'. If we find any, we OR in their netmask to the
1168 * user-specified netmask.
1174 u_int32_t mask, nmask, ina;
1175 struct ifreq *ifr, *ifend, ifreq;
1177 struct ifreq ifs[MAX_IFS];
1180 if (IN_CLASSA(addr)) /* determine network mask for address class */
1181 nmask = IN_CLASSA_NET;
1182 else if (IN_CLASSB(addr))
1183 nmask = IN_CLASSB_NET;
1185 nmask = IN_CLASSC_NET;
1186 /* class D nets are disallowed by bad_ip_adrs */
1187 mask = netmask | htonl(nmask);
1190 * Scan through the system's network interfaces.
1192 ifc.ifc_len = sizeof(ifs);
1194 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1195 syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m");
1198 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1199 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1200 ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len)) {
1202 * Check the interface's internet address.
1204 if (ifr->ifr_addr.sa_family != AF_INET)
1206 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1207 if ((ntohl(ina) & nmask) != (addr & nmask))
1210 * Check that the interface is up, and not point-to-point or loopback.
1212 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1213 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1215 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1219 * Get its netmask and OR it into our mask.
1221 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1223 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1230 * lock - create a lock file for the named lock device
1232 #define LOCK_PREFIX "/var/spool/lock/LCK.."
1238 char hdb_lock_buffer[12];
1242 if ((p = strrchr(dev, '/')) != NULL)
1244 lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1245 if (lock_file == NULL)
1246 novm("lock file name");
1247 strcat(strcpy(lock_file, LOCK_PREFIX), dev);
1249 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1251 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1252 /* Read the lock file to find out who has the device locked */
1253 n = read(fd, hdb_lock_buffer, 11);
1255 hdb_lock_buffer[n] = 0;
1256 pid = atoi(hdb_lock_buffer);
1259 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1262 if (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);
1284 sprintf(hdb_lock_buffer, "%10d\n", getpid());
1285 write(fd, hdb_lock_buffer, 11);
1292 * unlock - remove our lockfile