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.
6 * Copyright (c) 1995 The Australian National University.
9 * Redistribution and use in source and binary forms are permitted
10 * provided that the above copyright notice and this paragraph are
11 * duplicated in all such forms and that any documentation,
12 * advertising materials, and other materials related to such
13 * distribution and use acknowledge that the software was developed
14 * by Carnegie Mellon University and The Australian National University.
15 * The names of the Universities may not be used to endorse or promote
16 * products derived from this software without specific prior written
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
20 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
24 static char rcsid[] = "$FreeBSD$";
26 /* $NetBSD: sys-bsd.c,v 1.1.1.3 1997/09/26 18:53:04 christos Exp $ */
41 #include <sys/ioctl.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
46 #include <sys/param.h>
47 #include <sys/module.h>
56 #include <net/ppp_defs.h>
57 #include <net/if_ppp.h>
58 #include <net/route.h>
59 #include <net/if_dl.h>
60 #include <netinet/in.h>
61 #include <net/if_var.h>
62 #include <netinet6/in6_var.h>
63 #include <netinet6/nd6.h>
67 #include <netipx/ipx.h>
71 #include <sys/param.h>
72 #if defined(NetBSD) && (NetBSD >= 199703)
73 #include <netinet/if_inarp.h>
74 #else /* NetBSD 1.2D or later */
76 #include <netinet/if_ether.h>
78 #include <net/if_ether.h>
89 static int initdisc = -1; /* Initial TTY discipline for ppp_fd */
90 static int initfdflags = -1; /* Initial file descriptor flags for ppp_fd */
91 static int ppp_fd = -1; /* fd which is set to PPP discipline */
94 static int restore_term; /* 1 => we've munged the terminal */
95 static struct termios inittermios; /* Initial TTY termios */
96 static struct winsize wsinfo; /* Initial window size info */
98 static char *lock_file; /* name of lock file created */
100 static int loop_slave = -1;
101 static int loop_master;
102 static char loop_name[20];
104 static unsigned char inbuf[512]; /* buffer for chars read from loopback */
106 static int sockfd; /* socket for doing interface ioctls */
108 static int sock6_fd = -1; /* socket for doing ipv6 interface ioctls */
111 static int if_is_up; /* the interface is currently up */
112 static u_int32_t ifaddrs[2]; /* local and remote addresses we set */
113 static u_int32_t default_route_gateway; /* gateway addr for default route */
114 static u_int32_t proxy_arp_addr; /* remote addr for proxy arp */
116 /* Prototypes for procedures local to this file. */
117 static int dodefaultroute __P((u_int32_t, int));
118 static int get_ether_addr __P((u_int32_t, struct sockaddr_dl *));
122 * sys_init - System-dependent initialization.
127 /* Get an internet socket for doing socket ioctl's on. */
128 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
129 syslog(LOG_ERR, "Couldn't create IP socket: %m");
134 if ((sock6_fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
135 /* check it at runtime */
142 * sys_cleanup - restore any system state we modified before exiting:
143 * mark the interface down, delete default route and/or proxy arp entry.
144 * This should call die() because it's called from die().
152 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
153 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) >= 0
154 && ((ifr.ifr_flags & IFF_UP) != 0)) {
155 ifr.ifr_flags &= ~IFF_UP;
156 ioctl(sockfd, SIOCSIFFLAGS, &ifr);
160 cifaddr(0, ifaddrs[0], ifaddrs[1]);
161 if (default_route_gateway)
162 cifdefaultroute(0, 0, default_route_gateway);
164 cifproxyarp(0, proxy_arp_addr);
168 * sys_close - Clean up in a child process before execing.
179 if (loop_slave >= 0) {
186 * sys_check_options - check the options that the user specified
194 * ppp_available - check whether the system has the ppp module loaded
195 * or compiled in. If it doesn't, and we're actually root (not just SUID
196 * root) try loading it before giving up.
201 const char *modname = "if_ppp";
202 extern char *no_ppp_msg;
204 if (modfind(modname) != -1) {
208 if (getuid() == 0 && kldload(modname) != -1)
212 This system lacks kernel support for PPP. To include PPP support\n\
213 in the kernel, please add \"device ppp\" to your kernel config or \n\
214 load the if_ppp module.\n";
220 * establish_ppp - Turn the serial port into a ppp interface.
226 int pppdisc = PPPDISC;
231 * Demand mode - prime the old ppp device to relinquish the unit.
233 if (ioctl(ppp_fd, PPPIOCXFERUNIT, 0) < 0) {
234 syslog(LOG_ERR, "ioctl(transfer ppp unit): %m");
240 * Save the old line discipline of fd, and set it to PPP.
242 if (ioctl(fd, TIOCGETD, &initdisc) < 0) {
243 syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
246 if (ioctl(fd, TIOCSETD, &pppdisc) < 0) {
247 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
253 * Find out which interface we were given.
255 if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {
256 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
261 * Check that we got the same unit again.
263 if (ioctl(fd, PPPIOCGUNIT, &x) < 0) {
264 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
268 syslog(LOG_ERR, "transfer_ppp failed: wanted unit %d, got %d",
273 ioctl(loop_slave, TIOCSETD, &x);
279 * Enable debug in the driver if requested.
282 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
283 syslog(LOG_WARNING, "ioctl (PPPIOCGFLAGS): %m");
285 x |= (kdebugflag & 0xFF) * SC_DEBUG;
286 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
287 syslog(LOG_WARNING, "ioctl(PPPIOCSFLAGS): %m");
292 * Set device for non-blocking reads.
294 if ((initfdflags = fcntl(fd, F_GETFL)) == -1
295 || fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
296 syslog(LOG_WARNING, "Couldn't set device to non-blocking mode: %m");
301 * restore_loop - reattach the ppp unit to the loopback.
309 * Transfer the ppp interface back to the loopback.
311 if (ioctl(ppp_fd, PPPIOCXFERUNIT, 0) < 0) {
312 syslog(LOG_ERR, "ioctl(transfer ppp unit): %m");
316 if (ioctl(loop_slave, TIOCSETD, &x) < 0) {
317 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
322 * Check that we got the same unit again.
324 if (ioctl(loop_slave, PPPIOCGUNIT, &x) < 0) {
325 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
329 syslog(LOG_ERR, "transfer_ppp failed: wanted unit %d, got %d",
338 * disestablish_ppp - Restore the serial port to normal operation.
339 * This shouldn't call die() because it's called from die().
345 /* Reset non-blocking mode on fd. */
346 if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
347 syslog(LOG_WARNING, "Couldn't restore device fd flags: %m");
350 /* Restore old line discipline. */
351 if (initdisc >= 0 && ioctl(fd, TIOCSETD, &initdisc) < 0)
352 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
360 * Check whether the link seems not to be 8-bit clean.
368 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
370 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
372 s = "bit 7 set to 1";
375 s = "bit 7 set to 0";
385 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
386 syslog(LOG_WARNING, "All received characters had %s", s);
392 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
393 * at the requested speed, etc. If `local' is true, set CLOCAL
394 * regardless of whether the modem option was specified.
396 * For *BSD, we assume that speed_t values numerically equal bits/second.
399 set_up_tty(fd, local)
404 if (tcgetattr(fd, &tios) < 0) {
405 syslog(LOG_ERR, "tcgetattr: %m");
411 ioctl(fd, TIOCGWINSZ, &wsinfo);
414 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
415 if (crtscts > 0 && !local)
416 tios.c_cflag |= CRTSCTS;
417 else if (crtscts < 0)
418 tios.c_cflag &= ~CRTSCTS;
420 tios.c_cflag |= CS8 | CREAD | HUPCL;
422 tios.c_cflag |= CLOCAL;
423 tios.c_iflag = IGNBRK | IGNPAR;
427 tios.c_cc[VTIME] = 0;
430 tios.c_iflag |= IXON | IXOFF;
431 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
432 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
436 cfsetospeed(&tios, inspeed);
437 cfsetispeed(&tios, inspeed);
439 inspeed = cfgetospeed(&tios);
441 * We can't proceed if the serial port speed is 0,
442 * since that implies that the serial port is disabled.
445 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
452 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
453 syslog(LOG_ERR, "tcsetattr: %m");
461 * restore_tty - restore the terminal to the saved settings.
468 if (!default_device) {
470 * Turn off echoing, because otherwise we can get into
471 * a loop with the tty and the modem echoing to each other.
472 * We presume we are the sole user of this tty device, so
473 * when we close it, it will revert to its defaults anyway.
475 inittermios.c_lflag &= ~(ECHO | ECHONL);
477 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
479 syslog(LOG_WARNING, "tcsetattr: %m");
480 ioctl(fd, TIOCSWINSZ, &wsinfo);
486 * setdtr - control the DTR line on the serial port.
487 * This is called from die(), so it shouldn't call die().
493 int modembits = TIOCM_DTR;
495 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
500 * sif6addr - Config the interface with an IPv6 link-local address
503 sif6addr(int unit, eui64_t our_eui64, eui64_t his_eui64)
506 struct in6_aliasreq addreq6;
509 syslog(LOG_ERR, "No IPv6 socket available");
514 /* actually, this part is not kame local - RFC2553 conformant */
515 ifindex = if_nametoindex(ifname);
517 syslog(LOG_ERR, "sifaddr6: no interface %s", ifname);
521 memset(&addreq6, 0, sizeof(addreq6));
522 strlcpy(addreq6.ifra_name, ifname, sizeof(addreq6.ifra_name));
525 addreq6.ifra_addr.sin6_family = AF_INET6;
526 addreq6.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
527 addreq6.ifra_addr.sin6_addr.s6_addr[0] = 0xfe;
528 addreq6.ifra_addr.sin6_addr.s6_addr[1] = 0x80;
529 memcpy(&addreq6.ifra_addr.sin6_addr.s6_addr[8], &our_eui64,
531 /* KAME ifindex hack */
532 *(u_int16_t *)&addreq6.ifra_addr.sin6_addr.s6_addr[2] = htons(ifindex);
535 addreq6.ifra_dstaddr.sin6_family = AF_INET6;
536 addreq6.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
537 addreq6.ifra_dstaddr.sin6_addr.s6_addr[0] = 0xfe;
538 addreq6.ifra_dstaddr.sin6_addr.s6_addr[1] = 0x80;
539 memcpy(&addreq6.ifra_dstaddr.sin6_addr.s6_addr[8], &his_eui64,
541 /* KAME ifindex hack */
542 *(u_int16_t *)&addreq6.ifra_dstaddr.sin6_addr.s6_addr[2] = htons(ifindex);
544 /* prefix mask: 128bit */
545 addreq6.ifra_prefixmask.sin6_family = AF_INET6;
546 addreq6.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
547 memset(&addreq6.ifra_prefixmask.sin6_addr, 0xff,
548 sizeof(addreq6.ifra_prefixmask.sin6_addr));
550 /* address lifetime (infty) */
551 addreq6.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
552 addreq6.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
554 if (ioctl(sock6_fd, SIOCAIFADDR_IN6, &addreq6) < 0) {
555 syslog(LOG_ERR, "sif6addr: ioctl(SIOCAIFADDR_IN6): %m");
564 * cif6addr - Remove IPv6 address from interface
567 cif6addr(int unit, eui64_t our_eui64, eui64_t his_eui64)
570 struct in6_ifreq delreq6;
573 syslog(LOG_ERR, "No IPv6 socket available");
578 /* actually, this part is not kame local - RFC2553 conformant */
579 ifindex = if_nametoindex(ifname);
581 syslog(LOG_ERR, "cifaddr6: no interface %s", ifname);
585 memset(&delreq6, 0, sizeof(delreq6));
586 strlcpy(delreq6.ifr_name, ifname, sizeof(delreq6.ifr_name));
589 delreq6.ifr_ifru.ifru_addr.sin6_family = AF_INET6;
590 delreq6.ifr_ifru.ifru_addr.sin6_len = sizeof(struct sockaddr_in6);
591 delreq6.ifr_ifru.ifru_addr.sin6_addr.s6_addr[0] = 0xfe;
592 delreq6.ifr_ifru.ifru_addr.sin6_addr.s6_addr[1] = 0x80;
593 memcpy(&delreq6.ifr_ifru.ifru_addr.sin6_addr.s6_addr[8], &our_eui64,
595 /* KAME ifindex hack */
596 *(u_int16_t *)&delreq6.ifr_ifru.ifru_addr.sin6_addr.s6_addr[2] =
599 if (ioctl(sock6_fd, SIOCDIFADDR_IN6, &delreq6) < 0) {
600 syslog(LOG_ERR, "cif6addr: ioctl(SIOCDIFADDR_IN6): %m");
609 * open_ppp_loopback - open the device we use for getting
610 * packets in demand mode, and connect it to a ppp interface.
618 int pppdisc = PPPDISC;
620 if (openpty(&loop_master, &loop_slave, loop_name, NULL, NULL) < 0) {
621 syslog(LOG_ERR, "No free pty for loopback");
624 SYSDEBUG((LOG_DEBUG, "using %s for loopback", loop_name));
626 if (tcgetattr(loop_slave, &tios) == 0) {
627 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB);
628 tios.c_cflag |= CS8 | CREAD;
629 tios.c_iflag = IGNPAR;
632 if (tcsetattr(loop_slave, TCSAFLUSH, &tios) < 0)
633 syslog(LOG_WARNING, "couldn't set attributes on loopback: %m");
636 if ((flags = fcntl(loop_master, F_GETFL)) != -1)
637 if (fcntl(loop_master, F_SETFL, flags | O_NONBLOCK) == -1)
638 syslog(LOG_WARNING, "couldn't set loopback to nonblock: %m");
641 if (ioctl(ppp_fd, TIOCSETD, &pppdisc) < 0) {
642 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
647 * Find out which interface we were given.
649 if (ioctl(ppp_fd, PPPIOCGUNIT, &ifunit) < 0) {
650 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
655 * Enable debug in the driver if requested.
658 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &flags) < 0) {
659 syslog(LOG_WARNING, "ioctl (PPPIOCGFLAGS): %m");
661 flags |= (kdebugflag & 0xFF) * SC_DEBUG;
662 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &flags) < 0)
663 syslog(LOG_WARNING, "ioctl(PPPIOCSFLAGS): %m");
671 * output - Output PPP packet.
680 log_packet(p, len, "sent ", LOG_DEBUG);
682 if (write(ttyfd, p, len) < 0) {
684 syslog(LOG_ERR, "write: %m");
690 * wait_input - wait until there is data available on ttyfd,
691 * for the length of time specified by *timo (indefinite
696 struct timeval *timo;
701 if (ttyfd >= FD_SETSIZE) {
702 syslog(LOG_ERR, "descriptor too big");
706 FD_SET(ttyfd, &ready);
707 n = select(ttyfd+1, &ready, NULL, &ready, timo);
708 if (n < 0 && errno != EINTR) {
709 syslog(LOG_ERR, "select: %m");
716 * wait_loop_output - wait until there is data available on the
717 * loopback, for the length of time specified by *timo (indefinite
721 wait_loop_output(timo)
722 struct timeval *timo;
727 if (loop_master >= FD_SETSIZE) {
728 syslog(LOG_ERR, "descriptor too big");
732 FD_SET(loop_master, &ready);
733 n = select(loop_master + 1, &ready, NULL, &ready, timo);
734 if (n < 0 && errno != EINTR) {
735 syslog(LOG_ERR, "select: %m");
742 * wait_time - wait for a given length of time or until a
743 * signal is received.
747 struct timeval *timo;
751 n = select(0, NULL, NULL, NULL, timo);
752 if (n < 0 && errno != EINTR) {
753 syslog(LOG_ERR, "select: %m");
760 * read_packet - get a PPP packet from the serial device.
768 if ((len = read(ttyfd, buf, PPP_MTU + PPP_HDRLEN)) < 0) {
769 if (errno == EWOULDBLOCK || errno == EINTR)
771 syslog(LOG_ERR, "read: %m");
779 * get_loop_output - read characters from the loopback, form them
780 * into frames, and detect when we want to bring the real link up.
781 * Return value is 1 if we need to bring up the link, 0 otherwise.
789 while ((n = read(loop_master, inbuf, sizeof(inbuf))) >= 0) {
790 if (loop_chars(inbuf, n))
795 syslog(LOG_ERR, "eof on loopback");
797 } else if (errno != EWOULDBLOCK){
798 syslog(LOG_ERR, "read from loopback: %m");
807 * ppp_send_config - configure the transmit characteristics of
811 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
819 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
821 if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
822 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
826 if (ioctl(ppp_fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
827 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
831 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
832 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
835 x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
836 x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
837 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
838 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
845 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
848 ppp_set_xaccm(unit, accm)
852 if (ioctl(ppp_fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
853 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
858 * ppp_recv_config - configure the receive-side characteristics of
862 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
869 if (ioctl(ppp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0) {
870 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
873 if (ioctl(ppp_fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
874 syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m");
877 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
878 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
881 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
882 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
883 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
889 * ccp_test - ask kernel whether a given compression method
890 * is acceptable for use. Returns 1 if the method and parameters
891 * are OK, 0 if the method is known but the parameters are not OK
892 * (e.g. code size should be reduced), or -1 if the method is unknown.
895 ccp_test(unit, opt_ptr, opt_len, for_transmit)
896 int unit, opt_len, for_transmit;
899 struct ppp_option_data data;
902 data.length = opt_len;
903 data.transmit = for_transmit;
904 if (ioctl(ttyfd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
906 return (errno == ENOBUFS)? 0: -1;
910 * ccp_flags_set - inform kernel about the current state of CCP.
913 ccp_flags_set(unit, isopen, isup)
914 int unit, isopen, isup;
918 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
919 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
922 x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
923 x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
924 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
925 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
929 * ccp_fatal_error - returns 1 if decompression was disabled as a
930 * result of an error detected after decompression of a packet,
931 * 0 otherwise. This is necessary because of patent nonsense.
934 ccp_fatal_error(unit)
939 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
940 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
943 return x & SC_DC_FERROR;
947 * get_idle_time - return how long the link has been idle.
954 return ioctl(ppp_fd, PPPIOCGIDLE, ip) >= 0;
960 * set_filters - transfer the pass and active filters to the kernel.
963 set_filters(pass, active)
964 struct bpf_program *pass, *active;
968 if (pass->bf_len > 0) {
969 if (ioctl(ppp_fd, PPPIOCSPASS, pass) < 0) {
970 syslog(LOG_ERR, "Couldn't set pass-filter in kernel: %m");
974 if (active->bf_len > 0) {
975 if (ioctl(ppp_fd, PPPIOCSACTIVE, active) < 0) {
976 syslog(LOG_ERR, "Couldn't set active-filter in kernel: %m");
985 * sifvjcomp - config tcp header compression
988 sifvjcomp(u, vjcomp, cidcomp, maxcid)
989 int u, vjcomp, cidcomp, maxcid;
993 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
994 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
997 x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
998 x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
999 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
1000 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
1003 if (vjcomp && ioctl(ppp_fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
1004 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
1011 * sifup - Config the interface up and enable IP packets to pass.
1019 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
1020 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
1021 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
1024 ifr.ifr_flags |= IFF_UP;
1025 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
1026 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
1034 * sifnpmode - Set the mode for handling packets for a given NP.
1037 sifnpmode(u, proto, mode)
1044 npi.protocol = proto;
1046 if (ioctl(ppp_fd, PPPIOCSNPMODE, &npi) < 0) {
1047 syslog(LOG_ERR, "ioctl(set NP %d mode to %d): %m", proto, mode);
1054 * sifdown - Config the interface down and disable IP.
1065 npi.protocol = PPP_IP;
1066 npi.mode = NPMODE_ERROR;
1067 ioctl(ppp_fd, PPPIOCSNPMODE, (caddr_t) &npi);
1068 /* ignore errors, because ppp_fd might have been closed by now. */
1070 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
1071 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
1072 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
1075 ifr.ifr_flags &= ~IFF_UP;
1076 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
1077 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
1086 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
1089 #define SET_SA_FAMILY(addr, family) \
1090 BZERO((char *) &(addr), sizeof(addr)); \
1091 addr.sa_family = (family); \
1092 addr.sa_len = sizeof(addr);
1095 * sifaddr - Config the interface IP addresses and netmask.
1102 struct ifaliasreq ifra;
1105 strncpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
1106 SET_SA_FAMILY(ifra.ifra_addr, AF_INET);
1107 ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;
1108 SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);
1109 ((struct sockaddr_in *) &ifra.ifra_broadaddr)->sin_addr.s_addr = h;
1111 SET_SA_FAMILY(ifra.ifra_mask, AF_INET);
1112 ((struct sockaddr_in *) &ifra.ifra_mask)->sin_addr.s_addr = m;
1114 BZERO(&ifra.ifra_mask, sizeof(ifra.ifra_mask));
1115 BZERO(&ifr, sizeof(ifr));
1116 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1117 if (ioctl(sockfd, SIOCDIFADDR, (caddr_t) &ifr) < 0) {
1118 if (errno != EADDRNOTAVAIL)
1119 syslog(LOG_WARNING, "Couldn't remove interface address: %m");
1121 if (ioctl(sockfd, SIOCAIFADDR, (caddr_t) &ifra) < 0) {
1122 if (errno != EEXIST) {
1123 syslog(LOG_ERR, "Couldn't set interface address: %m");
1127 "Couldn't set interface address: Address %s already exists",
1136 * cifaddr - Clear the interface IP addresses, and delete routes
1137 * through the interface if possible.
1144 struct ifaliasreq ifra;
1147 strncpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
1148 SET_SA_FAMILY(ifra.ifra_addr, AF_INET);
1149 ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;
1150 SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);
1151 ((struct sockaddr_in *) &ifra.ifra_broadaddr)->sin_addr.s_addr = h;
1152 BZERO(&ifra.ifra_mask, sizeof(ifra.ifra_mask));
1153 if (ioctl(sockfd, SIOCDIFADDR, (caddr_t) &ifra) < 0) {
1154 if (errno != EADDRNOTAVAIL)
1155 syslog(LOG_WARNING, "Couldn't delete interface address: %m");
1162 * sifdefaultroute - assign a default route through the address given.
1165 sifdefaultroute(u, l, g)
1169 return dodefaultroute(g, 's');
1173 * cifdefaultroute - delete a default route through the address given.
1176 cifdefaultroute(u, l, g)
1180 return dodefaultroute(g, 'c');
1184 * dodefaultroute - talk to a routing socket to add/delete a default route.
1187 dodefaultroute(g, cmd)
1193 struct rt_msghdr hdr;
1194 struct sockaddr_in dst;
1195 struct sockaddr_in gway;
1196 struct sockaddr_in mask;
1199 if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
1200 syslog(LOG_ERR, "Couldn't %s default route: socket: %m",
1201 cmd=='s'? "add": "delete");
1205 memset(&rtmsg, 0, sizeof(rtmsg));
1206 rtmsg.hdr.rtm_type = cmd == 's'? RTM_ADD: RTM_DELETE;
1207 rtmsg.hdr.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;
1208 rtmsg.hdr.rtm_version = RTM_VERSION;
1209 rtmsg.hdr.rtm_seq = ++rtm_seq;
1210 rtmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
1211 rtmsg.dst.sin_len = sizeof(rtmsg.dst);
1212 rtmsg.dst.sin_family = AF_INET;
1213 rtmsg.gway.sin_len = sizeof(rtmsg.gway);
1214 rtmsg.gway.sin_family = AF_INET;
1215 rtmsg.gway.sin_addr.s_addr = g;
1216 rtmsg.mask.sin_len = sizeof(rtmsg.dst);
1217 rtmsg.mask.sin_family = AF_INET;
1219 rtmsg.hdr.rtm_msglen = sizeof(rtmsg);
1220 if (write(routes, &rtmsg, sizeof(rtmsg)) < 0) {
1221 syslog(LOG_ERR, "Couldn't %s default route: %m",
1222 cmd=='s'? "add": "delete");
1228 default_route_gateway = (cmd == 's')? g: 0;
1232 #if RTM_VERSION >= 3
1235 * sifproxyarp - Make a proxy ARP entry for the peer.
1238 struct rt_msghdr hdr;
1239 struct sockaddr_inarp dst;
1240 struct sockaddr_dl hwa;
1244 static int arpmsg_valid;
1247 sifproxyarp(unit, hisaddr)
1254 * Get the hardware address of an interface on the same subnet
1255 * as our local address.
1257 memset(&arpmsg, 0, sizeof(arpmsg));
1258 if (!get_ether_addr(hisaddr, &arpmsg.hwa)) {
1259 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
1263 if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
1264 syslog(LOG_ERR, "Couldn't add proxy arp entry: socket: %m");
1268 arpmsg.hdr.rtm_type = RTM_ADD;
1269 arpmsg.hdr.rtm_flags = RTF_ANNOUNCE | RTF_HOST | RTF_STATIC;
1270 arpmsg.hdr.rtm_version = RTM_VERSION;
1271 arpmsg.hdr.rtm_seq = ++rtm_seq;
1272 arpmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY;
1273 arpmsg.hdr.rtm_inits = RTV_EXPIRE;
1274 arpmsg.dst.sin_len = sizeof(struct sockaddr_inarp);
1275 arpmsg.dst.sin_family = AF_INET;
1276 arpmsg.dst.sin_addr.s_addr = hisaddr;
1277 arpmsg.dst.sin_other = SIN_PROXY;
1279 arpmsg.hdr.rtm_msglen = (char *) &arpmsg.hwa - (char *) &arpmsg
1280 + arpmsg.hwa.sdl_len;
1281 if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0) {
1282 syslog(LOG_ERR, "Couldn't add proxy arp entry: %m");
1289 proxy_arp_addr = hisaddr;
1294 * cifproxyarp - Delete the proxy ARP entry for the peer.
1297 cifproxyarp(unit, hisaddr)
1307 arpmsg.hdr.rtm_type = RTM_DELETE;
1308 arpmsg.hdr.rtm_seq = ++rtm_seq;
1310 if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
1311 syslog(LOG_ERR, "Couldn't delete proxy arp entry: socket: %m");
1315 if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0) {
1316 syslog(LOG_ERR, "Couldn't delete proxy arp entry: %m");
1326 #else /* RTM_VERSION */
1329 * sifproxyarp - Make a proxy ARP entry for the peer.
1332 sifproxyarp(unit, hisaddr)
1336 struct arpreq arpreq;
1338 struct sockaddr_dl sdl;
1342 BZERO(&arpreq, sizeof(arpreq));
1345 * Get the hardware address of an interface on the same subnet
1346 * as our local address.
1348 if (!get_ether_addr(hisaddr, &dls.sdl)) {
1349 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
1353 arpreq.arp_ha.sa_len = sizeof(struct sockaddr);
1354 arpreq.arp_ha.sa_family = AF_UNSPEC;
1355 BCOPY(LLADDR(&dls.sdl), arpreq.arp_ha.sa_data, dls.sdl.sdl_alen);
1356 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1357 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1358 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1359 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
1360 syslog(LOG_ERR, "Couldn't add proxy arp entry: %m");
1364 proxy_arp_addr = hisaddr;
1369 * cifproxyarp - Delete the proxy ARP entry for the peer.
1372 cifproxyarp(unit, hisaddr)
1376 struct arpreq arpreq;
1378 BZERO(&arpreq, sizeof(arpreq));
1379 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1380 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1381 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1382 syslog(LOG_WARNING, "Couldn't delete proxy arp entry: %m");
1388 #endif /* RTM_VERSION */
1391 /********************************************************************
1393 * sipxfaddr - Config the interface IPX networknumber
1397 sipxfaddr (int unit, unsigned long int network, unsigned char * node )
1402 struct sockaddr_ipx ipx_addr;
1404 struct sockaddr_ipx *sipx = (struct sockaddr_ipx *) &ifr.ifr_addr;
1405 union ipx_net_u net;
1407 skfd = socket (AF_IPX, SOCK_DGRAM, 0);
1410 syslog (LOG_DEBUG, "socket(AF_IPX): %m(%d)", errno);
1415 memset (&ifr, '\0', sizeof (ifr));
1416 strcpy (ifr.ifr_name, ifname);
1418 memcpy (sipx->sipx_addr.x_host.c_host, node, 6);
1419 sipx->sipx_len = sizeof(sipx);
1420 sipx->sipx_family = AF_IPX;
1421 sipx->sipx_port = 0;
1422 memset(&net, 0, sizeof(net));
1423 net.long_e = htonl (network);
1424 sipx->sipx_addr.x_net = net.net_e;
1426 * Set the IPX device
1428 if (ioctl(skfd, SIOCSIFADDR, (caddr_t) &ifr) < 0)
1431 if (errno != EEXIST)
1434 "ioctl(SIOCAIFADDR, CRTITF): %m(%d)", errno);
1438 syslog (LOG_WARNING,
1439 "ioctl(SIOCAIFADDR, CRTITF): Address already exists");
1447 /********************************************************************
1449 * cipxfaddr - Clear the information for the IPX network. The IPX routes
1450 * are removed and the device is no longer able to pass IPX
1454 int cipxfaddr (int unit)
1459 struct sockaddr_ipx ipx_addr;
1461 struct sockaddr_ipx *sipx = (struct sockaddr_ipx *) &ifr.ifr_addr;
1463 skfd = socket (AF_IPX, SOCK_DGRAM, 0);
1466 syslog (LOG_DEBUG, "socket(AF_IPX): %m(%d)", errno);
1471 memset (&ifr, '\0', sizeof (ifr));
1472 strcpy (ifr.ifr_name, ifname);
1474 sipx->sipx_len = sizeof(sipx);
1475 sipx->sipx_family = AF_IPX;
1477 * Set the IPX device
1479 if (ioctl(skfd, SIOCSIFADDR, (caddr_t) &ifr) < 0)
1482 "ioctl(SIOCAIFADDR, IPX_DLTITF): %m(%d)", errno);
1492 * get_ether_addr - get the hardware address of an interface on the
1493 * the same subnet as ipaddr.
1496 get_ether_addr(ipaddr, hwaddr)
1498 struct sockaddr_dl *hwaddr;
1500 u_int32_t ina, mask;
1501 struct sockaddr_dl *dla;
1502 struct ifaddrs *ifap, *ifa, *ifp;
1505 * Scan through looking for an interface with an Internet
1506 * address on the same subnet as `ipaddr'.
1508 if (getifaddrs(&ifap) != 0) {
1509 syslog(LOG_ERR, "getifaddrs: %m");
1512 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1513 if (ifa->ifa_addr->sa_family != AF_INET)
1515 ina = ((struct sockaddr_in *) ifa->ifa_addr)->sin_addr.s_addr;
1517 * Check that the interface is up, and not point-to-point
1520 if ((ifa->ifa_flags &
1521 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1522 != (IFF_UP|IFF_BROADCAST))
1525 * Get its netmask and check that it's on the right subnet.
1527 mask = ((struct sockaddr_in *) ifa->ifa_netmask)->sin_addr.s_addr;
1528 if ((ipaddr & mask) != (ina & mask))
1536 syslog(LOG_INFO, "found interface %s for proxy arp", ifa->ifa_name);
1539 * Now scan through again looking for a link-level address
1540 * for this interface.
1543 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1544 if (strcmp(ifp->ifa_name, ifa->ifa_name) != 0)
1546 if (ifa->ifa_addr->sa_family != AF_LINK)
1549 * Found the link-level address - copy it out
1551 dla = (struct sockaddr_dl *) ifa->ifa_addr;
1552 BCOPY(dla, hwaddr, dla->sdl_len);
1562 * Return user specified netmask, modified by any mask we might determine
1563 * for address `addr' (in network byte order).
1564 * Here we scan through the system's list of interfaces, looking for
1565 * any non-point-to-point interfaces which might appear to be on the same
1566 * network as `addr'. If we find any, we OR in their netmask to the
1567 * user-specified netmask.
1573 u_int32_t mask, nmask, ina;
1574 struct ifaddrs *ifap, *ifa;
1577 if (IN_CLASSA(addr)) /* determine network mask for address class */
1578 nmask = IN_CLASSA_NET;
1579 else if (IN_CLASSB(addr))
1580 nmask = IN_CLASSB_NET;
1582 nmask = IN_CLASSC_NET;
1583 /* class D nets are disallowed by bad_ip_adrs */
1584 mask = netmask | htonl(nmask);
1587 * Scan through the system's network interfaces.
1589 if (getifaddrs(&ifap) != 0) {
1590 syslog(LOG_WARNING, "getifaddrs: %m");
1593 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1595 * Check the interface's internet address.
1597 if (ifa->ifa_addr->sa_family != AF_INET)
1599 ina = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
1600 if ((ntohl(ina) & nmask) != (addr & nmask))
1603 * Check that the interface is up, and not point-to-point or loopback.
1605 if ((ifa->ifa_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK)) != IFF_UP)
1608 * Get its netmask and OR it into our mask.
1610 mask |= ((struct sockaddr_in *)&ifa->ifa_netmask)->sin_addr.s_addr;
1618 * Use the hostid as part of the random number seed.
1627 * lock - create a lock file for the named lock device
1629 #define LOCK_PREFIX "/var/spool/lock/LCK.."
1635 char hdb_lock_buffer[12];
1639 if ((p = strrchr(dev, '/')) != NULL)
1641 lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1642 if (lock_file == NULL)
1643 novm("lock file name");
1644 strcat(strcpy(lock_file, LOCK_PREFIX), dev);
1646 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1648 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1649 /* Read the lock file to find out who has the device locked */
1650 n = read(fd, hdb_lock_buffer, 11);
1652 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1655 hdb_lock_buffer[n] = 0;
1656 pid = atoi(hdb_lock_buffer);
1657 if (kill(pid, 0) == -1 && errno == ESRCH) {
1658 /* pid no longer exists - remove the lock file */
1659 if (unlink(lock_file) == 0) {
1661 syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1665 syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
1668 syslog(LOG_NOTICE, "Device %s is locked by pid %d",
1673 syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1679 sprintf(hdb_lock_buffer, "%10d\n", getpid());
1680 write(fd, hdb_lock_buffer, 11);
1687 * unlock - remove our lockfile