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.2 1995/06/06 12:11:45 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.
95 if (loop_slave >= 0) {
104 * note_debug_level - note a change in the debug level.
110 syslog(LOG_INFO, "Debug turned ON, Level %d", debug);
111 setlogmask(LOG_UPTO(LOG_DEBUG));
113 setlogmask(LOG_UPTO(LOG_WARNING));
118 * ppp_available - check whether the system has any ppp interfaces
119 * (in fact we check whether we can do an ioctl on ppp0).
127 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
128 return 1; /* can't tell */
130 strncpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
131 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
138 * establish_ppp - Turn the serial port (or loopback) into a ppp interface.
144 int pppdisc = PPPDISC;
147 if (ioctl(fd, TIOCGETD, &initdisc) < 0) {
148 syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
151 if (ioctl(fd, TIOCSETD, &pppdisc) < 0) {
152 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
157 * Find out which interface we were given.
159 if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {
160 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
166 * Enable debug in the driver if requested.
169 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
170 syslog(LOG_WARNING, "ioctl (PPPIOCGFLAGS): %m");
172 x |= (kdebugflag & 0xFF) * SC_DEBUG;
173 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
174 syslog(LOG_WARNING, "ioctl(PPPIOCSFLAGS): %m");
181 * transfer_ppp - make the device on fd `fd' take over the PPP interface
182 * unit that we are using.
191 return; /* we can't get here */
194 * Prime the old ppp device to relinquish the unit.
196 if (ioctl(ppp_fd, PPPIOCXFERUNIT, 0) < 0) {
197 syslog(LOG_ERR, "ioctl(transfer ppp unit): %m");
202 * Save the old line discipline of fd, and set it to PPP.
204 if (ioctl(fd, TIOCGETD, &prevdisc) < 0) {
205 syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
209 if (ioctl(fd, TIOCSETD, &x) < 0) {
210 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
215 * Set the old ppp device back to its previous discipline.
217 ioctl(ppp_fd, TIOCSETD, &initdisc);
221 * Check that we got the same unit again.
223 if (ioctl(fd, PPPIOCGUNIT, &x) < 0) {
224 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
228 syslog(LOG_ERR, "transfer_ppp failed: wanted unit %d, got %d",
238 * disestablish_ppp - Restore the serial port to normal operation.
239 * This shouldn't call die() because it's called from die().
245 if (fd == ppp_fd && fd >= 0 && initdisc >= 0) {
246 if (ioctl(fd, TIOCSETD, &initdisc) < 0)
247 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
254 * Check whether the link seems not to be 8-bit clean.
262 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
264 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
266 s = "bit 7 set to 1";
269 s = "bit 7 set to 0";
279 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
280 syslog(LOG_WARNING, "All received characters had %s", s);
286 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
287 * at the requested speed, etc. If `local' is true, set CLOCAL
288 * regardless of whether the modem option was specified.
290 * For *BSD, we assume that speed_t values numerically equal bits/second.
292 set_up_tty(fd, local)
297 if (tcgetattr(fd, &tios) < 0) {
298 syslog(LOG_ERR, "tcgetattr: %m");
305 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
307 tios.c_cflag |= CRTSCTS;
308 else if (crtscts < 0)
309 tios.c_cflag &= ~CRTSCTS;
311 tios.c_cflag |= CS8 | CREAD | HUPCL;
313 tios.c_cflag |= CLOCAL;
314 tios.c_iflag = IGNBRK | IGNPAR;
318 tios.c_cc[VTIME] = 0;
321 tios.c_iflag |= IXOFF;
322 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
323 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
327 cfsetospeed(&tios, inspeed);
328 cfsetispeed(&tios, inspeed);
330 inspeed = cfgetospeed(&tios);
332 * We can't proceed if the serial port speed is 0,
333 * since that implies that the serial port is disabled.
336 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
343 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
344 syslog(LOG_ERR, "tcsetattr: %m");
352 * restore_tty - restore the terminal to the saved settings.
358 if (!default_device) {
360 * Turn off echoing, because otherwise we can get into
361 * a loop with the tty and the modem echoing to each other.
362 * We presume we are the sole user of this tty device, so
363 * when we close it, it will revert to its defaults anyway.
365 inittermios.c_lflag &= ~(ECHO | ECHONL);
367 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
369 syslog(LOG_WARNING, "tcsetattr: %m");
375 * setdtr - control the DTR line on the serial port.
376 * This is called from die(), so it shouldn't call die().
381 int modembits = TIOCM_DTR;
383 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
388 * open_loopback - open the device we use for getting packets
389 * in demand mode. Here we use a pty.
397 if (openpty(&loop_master, &loop_slave, loop_name, NULL, NULL) < 0) {
398 syslog(LOG_ERR, "No free pty for loopback");
401 SYSDEBUG((LOG_DEBUG, "using %s for loopback", loop_name));
402 if (tcgetattr(loop_slave, &tios) == 0) {
403 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB);
404 tios.c_cflag |= CS8 | CREAD;
405 tios.c_iflag = IGNPAR;
408 if (tcsetattr(loop_slave, TCSAFLUSH, &tios) < 0)
409 syslog(LOG_WARNING, "couldn't set attributes on loopback: %m");
411 if ((flags = fcntl(loop_slave, F_GETFL)) != -1)
412 if (fcntl(loop_slave, F_SETFL, flags | O_NONBLOCK) == -1)
413 syslog(LOG_WARNING, "couldn't set loopback to nonblock: %m");
414 if ((flags = fcntl(loop_master, F_GETFL)) != -1)
415 if (fcntl(loop_master, F_SETFL, flags | O_NONBLOCK) == -1)
416 syslog(LOG_WARNING, "couldn't set loopback(m) to nonblock: %m");
422 * output - Output PPP packet.
431 SYSDEBUG((LOG_WARNING, "output: unit != 0!"));
433 log_packet(p, len, "sent ");
435 if (write(ppp_fd, p, len) < 0) {
436 syslog(LOG_ERR, "write: %m");
443 * wait_input - wait until there is data available on fd,
444 * for the length of time specified by *timo (indefinite
448 struct timeval *timo;
455 n = select(fd+1, &ready, NULL, &ready, timo);
456 if (n < 0 && errno != EINTR) {
457 syslog(LOG_ERR, "select: %m");
464 * wait_loop_output - wait until there is data available on the,
465 * loopback, for the length of time specified by *timo (indefinite
468 wait_loop_output(timo)
469 struct timeval *timo;
475 FD_SET(loop_master, &ready);
476 n = select(loop_master + 1, &ready, NULL, &ready, timo);
477 if (n < 0 && errno != EINTR) {
478 syslog(LOG_ERR, "select: %m");
485 * wait_time - wait for a given length of time or until a
486 * signal is received.
489 struct timeval *timo;
493 n = select(0, NULL, NULL, NULL, timo);
494 if (n < 0 && errno != EINTR) {
495 syslog(LOG_ERR, "select: %m");
502 * read_packet - get a PPP packet from the serial device.
510 if ((len = read(fd, buf, PPP_MTU + PPP_HDRLEN)) < 0) {
511 if (errno == EWOULDBLOCK || errno == EINTR)
513 syslog(LOG_ERR, "read(fd): %m");
521 * read_loop_output - get characters from the "bottom" of the loopback.
524 read_loop_output(buf, maxlen)
530 if ((len = read(loop_master, buf, maxlen)) < 0) {
531 if (errno == EWOULDBLOCK)
533 syslog(LOG_ERR, "read from loopback: %m");
535 } else if (len == 0) {
536 syslog(LOG_ERR, "eof on loopback");
544 * ppp_send_config - configure the transmit characteristics of
548 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
556 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
558 if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
559 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
563 if (ioctl(ppp_fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
564 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
568 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
569 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
572 x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
573 x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
574 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
575 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
582 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
585 ppp_set_xaccm(unit, accm)
589 if (ioctl(ppp_fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
590 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
595 * ppp_recv_config - configure the receive-side characteristics of
599 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
606 if (ioctl(ppp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0) {
607 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
610 if (ioctl(ppp_fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
611 syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m");
614 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
615 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
618 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
619 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
620 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
626 * ccp_test - ask kernel whether a given compression method
627 * is acceptable for use.
629 ccp_test(unit, opt_ptr, opt_len, for_transmit)
630 int unit, opt_len, for_transmit;
633 struct ppp_option_data data;
636 data.length = opt_len;
637 data.transmit = for_transmit;
638 return ioctl(ppp_fd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0;
642 * ccp_flags_set - inform kernel about the current state of CCP.
645 ccp_flags_set(unit, isopen, isup)
646 int unit, isopen, isup;
650 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
651 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
654 x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
655 x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
656 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
657 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
661 * ccp_fatal_error - returns 1 if decompression was disabled as a
662 * result of an error detected after decompression of a packet,
663 * 0 otherwise. This is necessary because of patent nonsense.
666 ccp_fatal_error(unit)
671 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
672 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
675 return x & SC_DC_FERROR;
679 * get_idle_time - return how long the link has been idle.
686 return ioctl(ppp_fd, PPPIOCGIDLE, ip) >= 0;
691 * sifvjcomp - config tcp header compression
694 sifvjcomp(u, vjcomp, cidcomp, maxcid)
695 int u, vjcomp, cidcomp, maxcid;
699 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
700 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
703 x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
704 x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
705 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
706 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
709 if (ioctl(ppp_fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
710 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
717 * sifup - Config the interface up and enable IP packets to pass.
726 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
727 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
728 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
731 ifr.ifr_flags |= IFF_UP;
732 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
733 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
736 npi.protocol = PPP_IP;
737 npi.mode = NPMODE_PASS;
738 if (ioctl(ppp_fd, PPPIOCSNPMODE, &npi) < 0) {
739 syslog(LOG_ERR, "ioctl(set IP mode to PASS): %m");
746 * sifnpmode - Set the mode for handling packets for a given NP.
749 sifnpmode(u, proto, mode)
756 npi.protocol = proto;
758 if (ioctl(ppp_fd, PPPIOCSNPMODE, &npi) < 0) {
759 syslog(LOG_ERR, "ioctl(set NP %d mode to %d): %m", proto, mode);
766 * sifdown - Config the interface down and disable IP.
777 npi.protocol = PPP_IP;
778 npi.mode = NPMODE_ERROR;
779 ioctl(ppp_fd, PPPIOCSNPMODE, (caddr_t) &npi);
780 /* ignore errors, because ppp_fd might have been closed by now. */
782 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
783 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
784 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
787 ifr.ifr_flags &= ~IFF_UP;
788 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
789 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
797 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
800 #define SET_SA_FAMILY(addr, family) \
801 BZERO((char *) &(addr), sizeof(addr)); \
802 addr.sa_family = (family); \
803 addr.sa_len = sizeof(addr);
806 * sifaddr - Config the interface IP addresses and netmask.
813 struct ifaliasreq ifra;
815 strncpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
816 SET_SA_FAMILY(ifra.ifra_addr, AF_INET);
817 ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;
818 SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);
819 ((struct sockaddr_in *) &ifra.ifra_broadaddr)->sin_addr.s_addr = h;
821 SET_SA_FAMILY(ifra.ifra_mask, AF_INET);
822 ((struct sockaddr_in *) &ifra.ifra_mask)->sin_addr.s_addr = m;
824 BZERO(&ifra.ifra_mask, sizeof(ifra.ifra_mask));
825 if (ioctl(sockfd, SIOCAIFADDR, (caddr_t) &ifra) < 0) {
826 if (errno != EEXIST) {
827 syslog(LOG_ERR, "ioctl(SIOCAIFADDR): %m");
830 syslog(LOG_WARNING, "ioctl(SIOCAIFADDR): Address already exists");
836 * cifaddr - Clear the interface IP addresses, and delete routes
837 * through the interface if possible.
844 struct ifaliasreq ifra;
846 strncpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
847 SET_SA_FAMILY(ifra.ifra_addr, AF_INET);
848 ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;
849 SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);
850 ((struct sockaddr_in *) &ifra.ifra_broadaddr)->sin_addr.s_addr = h;
851 BZERO(&ifra.ifra_mask, sizeof(ifra.ifra_mask));
852 if (ioctl(sockfd, SIOCDIFADDR, (caddr_t) &ifra) < 0) {
853 if (errno != EADDRNOTAVAIL)
854 syslog(LOG_WARNING, "ioctl(SIOCDIFADDR): %m");
861 * sifdefaultroute - assign a default route through the address given.
864 sifdefaultroute(u, g)
868 return dodefaultroute(g, 's');
872 * cifdefaultroute - delete a default route through the address given.
875 cifdefaultroute(u, g)
879 return dodefaultroute(g, 'c');
883 * dodefaultroute - talk to a routing socket to add/delete a default route.
886 dodefaultroute(g, cmd)
892 struct rt_msghdr hdr;
893 struct sockaddr_in dst;
894 struct sockaddr_in gway;
895 struct sockaddr_in mask;
898 if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
899 syslog(LOG_ERR, "%cifdefaultroute: opening routing socket: %m", cmd);
903 memset(&rtmsg, 0, sizeof(rtmsg));
904 rtmsg.hdr.rtm_type = cmd == 's'? RTM_ADD: RTM_DELETE;
905 rtmsg.hdr.rtm_flags = RTF_UP | RTF_GATEWAY;
906 rtmsg.hdr.rtm_version = RTM_VERSION;
907 rtmsg.hdr.rtm_seq = ++rtm_seq;
908 rtmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
909 rtmsg.dst.sin_len = sizeof(rtmsg.dst);
910 rtmsg.dst.sin_family = AF_INET;
911 rtmsg.gway.sin_len = sizeof(rtmsg.gway);
912 rtmsg.gway.sin_family = AF_INET;
913 rtmsg.gway.sin_addr.s_addr = g;
914 rtmsg.mask.sin_len = sizeof(rtmsg.dst);
915 rtmsg.mask.sin_family = AF_INET;
917 rtmsg.hdr.rtm_msglen = sizeof(rtmsg);
918 if (write(routes, &rtmsg, sizeof(rtmsg)) < 0) {
919 syslog(LOG_ERR, "%s default route: %m", cmd=='s'? "add": "delete");
931 * sifproxyarp - Make a proxy ARP entry for the peer.
934 struct rt_msghdr hdr;
935 struct sockaddr_inarp dst;
936 struct sockaddr_dl hwa;
940 static int arpmsg_valid;
943 sifproxyarp(unit, hisaddr)
951 * Get the hardware address of an interface on the same subnet
952 * as our local address.
954 memset(&arpmsg, 0, sizeof(arpmsg));
955 if (!get_ether_addr(hisaddr, &arpmsg.hwa)) {
956 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
960 if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
961 syslog(LOG_ERR, "sifproxyarp: opening routing socket: %m");
965 arpmsg.hdr.rtm_type = RTM_ADD;
966 arpmsg.hdr.rtm_flags = RTF_ANNOUNCE | RTF_HOST | RTF_STATIC;
967 arpmsg.hdr.rtm_version = RTM_VERSION;
968 arpmsg.hdr.rtm_seq = ++rtm_seq;
969 arpmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY;
970 arpmsg.hdr.rtm_inits = RTV_EXPIRE;
971 arpmsg.dst.sin_len = sizeof(struct sockaddr_inarp);
972 arpmsg.dst.sin_family = AF_INET;
973 arpmsg.dst.sin_addr.s_addr = hisaddr;
974 arpmsg.dst.sin_other = SIN_PROXY;
976 arpmsg.hdr.rtm_msglen = (char *) &arpmsg.hwa - (char *) &arpmsg
977 + arpmsg.hwa.sdl_len;
978 if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0) {
979 syslog(LOG_ERR, "add proxy arp entry: %m");
990 * cifproxyarp - Delete the proxy ARP entry for the peer.
993 cifproxyarp(unit, hisaddr)
1003 arpmsg.hdr.rtm_type = RTM_DELETE;
1004 arpmsg.hdr.rtm_seq = ++rtm_seq;
1006 if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
1007 syslog(LOG_ERR, "sifproxyarp: opening routing socket: %m");
1011 if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0) {
1012 syslog(LOG_ERR, "delete proxy arp entry: %m");
1021 #else /* RTM_VERSION */
1024 * sifproxyarp - Make a proxy ARP entry for the peer.
1027 sifproxyarp(unit, hisaddr)
1031 struct arpreq arpreq;
1033 struct sockaddr_dl sdl;
1037 BZERO(&arpreq, sizeof(arpreq));
1040 * Get the hardware address of an interface on the same subnet
1041 * as our local address.
1043 if (!get_ether_addr(hisaddr, &dls.sdl)) {
1044 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
1048 arpreq.arp_ha.sa_len = sizeof(struct sockaddr);
1049 arpreq.arp_ha.sa_family = AF_UNSPEC;
1050 BCOPY(LLADDR(&dls.sdl), arpreq.arp_ha.sa_data, dls.sdl.sdl_alen);
1051 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1052 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1053 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1054 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
1055 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
1063 * cifproxyarp - Delete the proxy ARP entry for the peer.
1066 cifproxyarp(unit, hisaddr)
1070 struct arpreq arpreq;
1072 BZERO(&arpreq, sizeof(arpreq));
1073 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1074 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1075 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1076 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m");
1081 #endif /* RTM_VERSION */
1085 * get_ether_addr - get the hardware address of an interface on the
1086 * the same subnet as ipaddr.
1091 get_ether_addr(ipaddr, hwaddr)
1093 struct sockaddr_dl *hwaddr;
1095 struct ifreq *ifr, *ifend, *ifp;
1096 u_int32_t ina, mask;
1097 struct sockaddr_dl *dla;
1100 struct ifreq ifs[MAX_IFS];
1102 ifc.ifc_len = sizeof(ifs);
1104 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1105 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
1110 * Scan through looking for an interface with an Internet
1111 * address on the same subnet as `ipaddr'.
1113 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1114 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1115 ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len)) {
1116 if (ifr->ifr_addr.sa_family == AF_INET) {
1117 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1118 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1120 * Check that the interface is up, and not point-to-point
1123 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1125 if ((ifreq.ifr_flags &
1126 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1127 != (IFF_UP|IFF_BROADCAST))
1130 * Get its netmask and check that it's on the right subnet.
1132 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1134 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1135 if ((ipaddr & mask) != (ina & mask))
1144 syslog(LOG_INFO, "found interface %s for proxy arp", ifr->ifr_name);
1147 * Now scan through again looking for a link-level address
1148 * for this interface.
1151 for (ifr = ifc.ifc_req; ifr < ifend; ) {
1152 if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0
1153 && ifr->ifr_addr.sa_family == AF_LINK) {
1155 * Found the link-level address - copy it out
1157 dla = (struct sockaddr_dl *) &ifr->ifr_addr;
1158 BCOPY(dla, hwaddr, dla->sdl_len);
1161 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len);
1168 * Return user specified netmask, modified by any mask we might determine
1169 * for address `addr' (in network byte order).
1170 * Here we scan through the system's list of interfaces, looking for
1171 * any non-point-to-point interfaces which might appear to be on the same
1172 * network as `addr'. If we find any, we OR in their netmask to the
1173 * user-specified netmask.
1179 u_int32_t mask, nmask, ina;
1180 struct ifreq *ifr, *ifend, ifreq;
1182 struct ifreq ifs[MAX_IFS];
1185 if (IN_CLASSA(addr)) /* determine network mask for address class */
1186 nmask = IN_CLASSA_NET;
1187 else if (IN_CLASSB(addr))
1188 nmask = IN_CLASSB_NET;
1190 nmask = IN_CLASSC_NET;
1191 /* class D nets are disallowed by bad_ip_adrs */
1192 mask = netmask | htonl(nmask);
1195 * Scan through the system's network interfaces.
1197 ifc.ifc_len = sizeof(ifs);
1199 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1200 syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m");
1203 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1204 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1205 ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len)) {
1207 * Check the interface's internet address.
1209 if (ifr->ifr_addr.sa_family != AF_INET)
1211 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1212 if ((ntohl(ina) & nmask) != (addr & nmask))
1215 * Check that the interface is up, and not point-to-point or loopback.
1217 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1218 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1220 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1224 * Get its netmask and OR it into our mask.
1226 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1228 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1235 * lock - create a lock file for the named lock device
1237 #define LOCK_PREFIX "/var/spool/lock/LCK.."
1243 char hdb_lock_buffer[12];
1247 if ((p = strrchr(dev, '/')) != NULL)
1249 lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1250 if (lock_file == NULL)
1251 novm("lock file name");
1252 strcat(strcpy(lock_file, LOCK_PREFIX), dev);
1254 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1256 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1257 /* Read the lock file to find out who has the device locked */
1258 n = read(fd, hdb_lock_buffer, 11);
1260 hdb_lock_buffer[n] = 0;
1261 pid = atoi(hdb_lock_buffer);
1264 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1267 if (kill(pid, 0) == -1 && errno == ESRCH) {
1268 /* pid no longer exists - remove the lock file */
1269 if (unlink(lock_file) == 0) {
1271 syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1275 syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
1278 syslog(LOG_NOTICE, "Device %s is locked by pid %d",
1283 syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1289 sprintf(hdb_lock_buffer, "%10d\n", getpid());
1290 write(fd, hdb_lock_buffer, 11);
1297 * unlock - remove our lockfile