2 * sys-next.c - System-dependent procedures for setting up
3 * PPP interfaces on NeXT 3.2/3.3 systems
5 * Copyright (c) 1989 Carnegie Mellon University.
6 * Copyright (c) 1994 Philippe-Andre Prindeville.
7 * Copyright (c) 1995 The Australian National University.
10 * Redistribution and use in source and binary forms are permitted
11 * provided that the above copyright notice and this paragraph are
12 * duplicated in all such forms and that any documentation,
13 * advertising materials, and other materials related to such
14 * distribution and use acknowledge that the software was developed
15 * by Carnegie Mellon University, Philippe-Andre Prindeville and
16 * The Australian National University. The names of the developers
17 * may not be used to endorse or promote products derived from this
18 * software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
21 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
25 static char rcsid[] = "$Id: sys-NeXT.c,v 1.6 1996/05/26 23:59:36 paulus Exp $";
35 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <sys/ioctl.h>
40 #include <sys/errno.h>
42 #include <sys/fcntl.h>
45 #include <net/ppp_defs.h>
46 #include <net/if_ppp.h>
48 #include <netinet/in.h>
49 #include <netinet/in_systm.h>
50 #include <netinet/in_var.h>
51 #include <netinet/ip.h>
52 #include <netinet/if_ether.h>
53 #include <net/route.h>
54 #include <netinet/in.h>
56 #include <netinfo/ni.h>
60 static int initdisc = -1; /* Initial TTY discipline for ppp_fd */
61 static int initfdflags = -1; /* Initial file descriptor flags for ppp_fd */
62 static int ppp_fd = -1; /* fd which is set to PPP discipline */
66 static int restore_term; /* 1 => we've munged the terminal */
67 static struct termios inittermios; /* Initial TTY termios */
68 static struct winsize wsinfo; /* Initial window size info */
70 static char *lock_file; /* name of lock file created */
72 static int loop_slave = -1;
73 static int loop_master;
74 static char loop_name[20];
76 static unsigned char inbuf[512]; /* buffer for chars read from loopback */
78 static int sockfd; /* socket for doing interface ioctls */
80 #if defined(i386) && defined(HAS_BROKEN_IOCTL)
84 static int if_is_up; /* the interface is currently up */
85 static u_int32_t ifaddrs[2]; /* local and remote addresses */
86 static u_int32_t default_route_gateway; /* gateway addr for default route */
87 static u_int32_t proxy_arp_addr; /* remote addr for proxy arp */
89 /* Prototypes for procedures local to this file. */
90 static int translate_speed __P((int));
91 static int baud_rate_of __P((int));
92 static int dodefaultroute __P((u_int32_t, int));
93 static int get_ether_addr __P((u_int32_t, struct sockaddr *));
94 static int ether_by_host __P((char *, struct ether_addr *));
98 * sys_init - System-dependent initialization.
103 /* Get an internet socket for doing socket ioctl's on. */
104 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
105 syslog(LOG_ERR, "Couldn't create IP socket: %m");
111 * sys_cleanup - restore any system state we modified before exiting:
112 * mark the interface down, delete default route and/or proxy arp entry.
113 * This should call die() because it's called from die().
121 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
122 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) >= 0
123 && ((ifr.ifr_flags & IFF_UP) != 0)) {
124 ifr.ifr_flags &= ~IFF_UP;
125 ioctl(sockfd, SIOCSIFFLAGS, &ifr);
129 cifaddr(0, ifaddrs[0], ifaddrs[1]);
130 if (default_route_gateway)
131 cifdefaultroute(0, default_route_gateway);
133 cifproxyarp(0, proxy_arp_addr);
137 * sys_close - Clean up in a child process before execing.
143 if (loop_slave >= 0) {
150 * sys_check_options - check the options that the user specified
159 * ppp_available - check whether the system has any ppp interfaces
160 * (in fact we check whether we can do an ioctl on ppp0).
168 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
169 return 1; /* can't tell - maybe we're not root */
171 strncpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
172 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
179 * establish_ppp - Turn the serial port into a ppp interface.
185 int pppdisc = PPPDISC;
190 * Demand mode - 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");
199 * Save the old line discipline of fd, and set it to PPP.
201 if (ioctl(fd, TIOCGETD, &initdisc) < 0) {
202 syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
205 if (ioctl(fd, TIOCSETD, &pppdisc) < 0) {
206 syslog(LOG_ERR, "ioctl(establish TIOCSETD): %m");
212 * Find out which interface we were given.
214 if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {
215 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
220 * Check that we got the same unit again.
222 if (ioctl(fd, PPPIOCGUNIT, &x) < 0) {
223 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
227 syslog(LOG_ERR, "transfer_ppp failed: wanted unit %d, got %d",
232 ioctl(loop_slave, TIOCSETD, &x);
238 * Enable debug in the driver if requested.
241 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
242 syslog(LOG_WARNING, "ioctl(PPPIOCGFLAGS): %m");
244 x |= (kdebugflag & 0xFF) * SC_DEBUG;
245 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
246 syslog(LOG_WARNING, "ioctl(PPPIOCSFLAGS): %m");
251 * Set device for non-blocking reads.
253 if ((initfdflags = fcntl(fd, F_GETFL)) == -1
254 || fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
255 syslog(LOG_WARNING, "Couldn't set device to non-blocking mode: %m");
260 * restore_loop - reattach the ppp unit to the loopback.
268 * Transfer the ppp interface back to the loopback.
270 if (ioctl(ppp_fd, PPPIOCXFERUNIT, 0) < 0) {
271 syslog(LOG_ERR, "ioctl(transfer ppp unit): %m");
275 if (ioctl(loop_slave, TIOCSETD, &x) < 0) {
276 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
281 * Check that we got the same unit again.
283 if (ioctl(loop_slave, PPPIOCGUNIT, &x) < 0) {
284 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
288 syslog(LOG_ERR, "transfer_ppp failed: wanted unit %d, got %d",
297 * disestablish_ppp - Restore the serial port to normal operation.
298 * This shouldn't call die() because it's called from die().
304 /* Reset non-blocking mode on fd. */
305 if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
306 syslog(LOG_WARNING, "Couldn't restore device fd flags: %m");
309 /* Restore old line discipline. */
310 if (initdisc >= 0 && ioctl(fd, TIOCSETD, &initdisc) < 0)
311 syslog(LOG_ERR, "ioctl(disestablish TIOCSETD): %m");
319 * Check whether the link seems not to be 8-bit clean.
327 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
329 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
331 s = "bit 7 set to 1";
334 s = "bit 7 set to 0";
344 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
345 syslog(LOG_WARNING, "All received characters had %s", s);
351 * List of valid speeds.
354 int speed_int, speed_val;
430 #warning Defining B115200
441 * Translate from bits/second to a speed_t.
447 struct speed *speedp;
451 for (speedp = speeds; speedp->speed_int; speedp++)
452 if (bps == speedp->speed_int)
453 return speedp->speed_val;
454 syslog(LOG_WARNING, "speed %d not supported", bps);
459 * Translate from a speed_t to bits/second.
465 struct speed *speedp;
469 for (speedp = speeds; speedp->speed_int; speedp++)
470 if (speed == speedp->speed_val)
471 return speedp->speed_int;
477 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
478 * at the requested speed, etc. If `local' is true, set CLOCAL
479 * regardless of whether the modem option was specified.
482 set_up_tty(fd, local)
485 int speed, x, modembits;
488 if (tcgetattr(fd, &tios) < 0) {
489 syslog(LOG_ERR, "tcgetattr: %m");
495 ioctl(fd, TIOCGWINSZ, &wsinfo);
498 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
499 if (crtscts > 0 && !local)
500 tios.c_cflag |= CRTSCTS;
501 else if (crtscts < 0)
502 tios.c_cflag &= ~CRTSCTS;
504 tios.c_cflag |= CS8 | CREAD | HUPCL;
506 tios.c_cflag |= CLOCAL;
508 tios.c_iflag = IGNBRK | IGNPAR;
512 tios.c_cc[VTIME] = 0;
515 tios.c_iflag |= IXON | IXOFF;
516 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
517 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
520 speed = translate_speed(inspeed);
522 cfsetospeed(&tios, speed);
523 cfsetispeed(&tios, speed);
525 speed = cfgetospeed(&tios);
529 modembits = TIOCM_RTS | TIOCM_CTS;
530 if (ioctl(fd, (crtscts ? TIOCMBIS : TIOCMBIC), &modembits) < 0)
531 syslog(LOG_ERR, "ioctl: TIOCMBIS/BIC: %m");
534 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
535 syslog(LOG_ERR, "tcsetattr: %m");
539 baud_rate = inspeed = baud_rate_of(speed);
544 * restore_tty - restore the terminal to the saved settings.
551 if (!default_device) {
553 * Turn off echoing, because otherwise we can get into
554 * a loop with the tty and the modem echoing to each other.
555 * We presume we are the sole user of this tty device, so
556 * when we close it, it will revert to its defaults anyway.
558 inittermios.c_lflag &= ~(ECHO | ECHONL);
560 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
562 syslog(LOG_WARNING, "tcsetattr: %m");
563 ioctl(fd, TIOCSWINSZ, &wsinfo);
569 * setdtr - control the DTR line on the serial port.
570 * This is called from die(), so it shouldn't call die().
572 * The write hack is to get NXFax to recognize that there is
573 * activity on the port. Not using the write nukes
574 * NXFax's capability to determine port usage.
581 int modembits = TIOCM_DTR;
589 /* ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits); */
590 ioctl(fd, (on? TIOCSDTR: TIOCCDTR), 0);
595 * open_ppp_loopback - open the device we use for getting
596 * packets in demand mode, and connect it to a ppp interface.
604 int pppdisc = PPPDISC;
606 if (openpty(&loop_master, &loop_slave, loop_name, NULL, NULL) < 0) {
607 syslog(LOG_ERR, "No free pty for loopback");
610 SYSDEBUG((LOG_DEBUG, "using %s for loopback", loop_name));
612 if (tcgetattr(loop_slave, &tios) == 0) {
613 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB);
614 tios.c_cflag |= CS8 | CREAD;
615 tios.c_iflag = IGNPAR;
618 if (tcsetattr(loop_slave, TCSAFLUSH, &tios) < 0)
619 syslog(LOG_WARNING, "couldn't set attributes on loopback: %m");
622 if ((flags = fcntl(loop_master, F_GETFL)) != -1)
623 if (fcntl(loop_master, F_SETFL, flags | O_NONBLOCK) == -1)
624 syslog(LOG_WARNING, "couldn't set loopback to nonblock: %m");
627 if (ioctl(ppp_fd, TIOCSETD, &pppdisc) < 0) {
628 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
633 * Find out which interface we were given.
635 if (ioctl(ppp_fd, PPPIOCGUNIT, &ifunit) < 0) {
636 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
641 * Enable debug in the driver if requested.
644 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &flags) < 0) {
645 syslog(LOG_WARNING, "ioctl (PPPIOCGFLAGS): %m");
647 flags |= (kdebugflag & 0xFF) * SC_DEBUG;
648 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &flags) < 0)
649 syslog(LOG_WARNING, "ioctl(PPPIOCSFLAGS): %m");
657 * output - Output PPP packet.
666 log_packet(p, len, "sent ");
668 if (write(ttyfd, p, len) < 0) {
669 if (errno == EWOULDBLOCK || errno == ENOBUFS
670 || errno == ENXIO || errno == EIO) {
671 syslog(LOG_WARNING, "write: warning: %m");
673 syslog(LOG_ERR, "write: %m");
681 * wait_input - wait until there is data available on ttyfd,
682 * for the length of time specified by *timo (indefinite
687 struct timeval *timo;
693 FD_SET(ttyfd, &ready);
694 n = select(ttyfd+1, &ready, NULL, &ready, timo);
695 if (n < 0 && errno != EINTR) {
696 syslog(LOG_ERR, "select: %m");
703 * wait_loop_output - wait until there is data available on the
704 * loopback, for the length of time specified by *timo (indefinite
707 wait_loop_output(timo)
708 struct timeval *timo;
714 FD_SET(loop_master, &ready);
715 n = select(loop_master + 1, &ready, NULL, &ready, timo);
716 if (n < 0 && errno != EINTR) {
717 syslog(LOG_ERR, "select: %m");
724 * wait_time - wait for a given length of time or until a
725 * signal is received.
728 struct timeval *timo;
732 n = select(0, NULL, NULL, NULL, timo);
733 if (n < 0 && errno != EINTR) {
734 syslog(LOG_ERR, "select: %m");
741 * read_packet - get a PPP packet from the serial device.
749 if ((len = read(ttyfd, buf, PPP_MTU + PPP_HDRLEN)) < 0) {
750 if (errno == EWOULDBLOCK || errno == EINTR)
752 syslog(LOG_ERR, "read: %m");
760 * get_loop_output - read characters from the loopback, form them
761 * into frames, and detect when we want to bring the real link up.
762 * Return value is 1 if we need to bring up the link, 0 otherwise.
770 while ((n = read(loop_master, inbuf, sizeof(inbuf))) >= 0) {
771 if (loop_chars(inbuf, n))
776 syslog(LOG_ERR, "eof on loopback");
778 } else if (errno != EWOULDBLOCK){
779 syslog(LOG_ERR, "read from loopback: %m");
788 * ppp_send_config - configure the transmit characteristics of
792 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
800 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
802 if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
803 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
807 if (ioctl(ppp_fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
808 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
812 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
813 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
816 x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
817 x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
818 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
819 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
826 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
829 ppp_set_xaccm(unit, accm)
833 if (ioctl(ppp_fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
834 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
839 * ppp_recv_config - configure the receive-side characteristics of
843 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
850 if (ioctl(ppp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0) {
851 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
854 if (ioctl(ppp_fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
855 syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m");
858 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
859 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
862 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
863 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
864 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
870 * ccp_test - ask kernel whether a given compression method
871 * is acceptable for use. Returns 1 if the method and parameters
872 * are OK, 0 if the method is known but the parameters are not OK
873 * (e.g. code size should be reduced), or -1 if the method is unknown.
876 ccp_test(unit, opt_ptr, opt_len, for_transmit)
877 int unit, opt_len, for_transmit;
880 struct ppp_option_data data;
883 data.length = opt_len;
884 data.transmit = for_transmit;
885 if (ioctl(ttyfd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
887 return (errno == ENOBUFS)? 0: -1;
891 * ccp_flags_set - inform kernel about the current state of CCP.
894 ccp_flags_set(unit, isopen, isup)
895 int unit, isopen, isup;
899 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
900 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
903 x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
904 x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
905 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
906 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
910 * ccp_fatal_error - returns 1 if decompression was disabled as a
911 * result of an error detected after decompression of a packet,
912 * 0 otherwise. This is necessary because of patent nonsense.
915 ccp_fatal_error(unit)
920 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
921 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
924 return x & SC_DC_FERROR;
928 * get_idle_time - return how long the link has been idle.
935 return ioctl(ppp_fd, PPPIOCGIDLE, ip) >= 0;
940 * set_filters - transfer the pass and active filters to the kernel.
943 set_filters(pass, active)
944 struct bpf_program *pass, *active;
948 if (pass->bf_len > 0) {
949 if (ioctl(ppp_fd, PPPIOCSPASS, pass) < 0) {
950 syslog(LOG_ERR, "Couldn't set pass-filter in kernel: %m");
954 if (active->bf_len > 0) {
955 if (ioctl(ppp_fd, PPPIOCSACTIVE, active) < 0) {
956 syslog(LOG_ERR, "Couldn't set active-filter in kernel: %m");
965 * sifvjcomp - config tcp header compression
968 sifvjcomp(u, vjcomp, cidcomp, maxcid)
969 int u, vjcomp, cidcomp, maxcid;
973 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
974 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
977 x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
978 x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
979 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
980 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
983 if (ioctl(ppp_fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
984 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
991 * sifup - Config the interface up and enable IP packets to pass.
999 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
1000 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
1001 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
1004 ifr.ifr_flags |= IFF_UP;
1005 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
1006 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
1014 * sifnpmode - Set the mode for handling packets for a given NP.
1017 sifnpmode(u, proto, mode)
1024 npi.protocol = proto;
1026 if (ioctl(ppp_fd, PPPIOCSNPMODE, &npi) < 0) {
1027 syslog(LOG_ERR, "ioctl(set NP %d mode to %d): %m", proto, mode);
1034 * sifdown - Config the interface down and disable IP.
1045 npi.protocol = PPP_IP;
1046 npi.mode = NPMODE_ERROR;
1047 ioctl(ppp_fd, PPPIOCSNPMODE, (caddr_t) &npi);
1048 /* ignore errors, because ppp_fd might have been closed by now. */
1050 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
1051 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
1052 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
1055 ifr.ifr_flags &= ~IFF_UP;
1056 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
1057 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
1066 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
1069 #define SET_SA_FAMILY(addr, family) \
1070 BZERO((char *) &(addr), sizeof(addr)); \
1071 addr.sa_family = (family);
1074 * sifaddr - Config the interface IP addresses and netmask.
1085 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1086 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
1088 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
1089 syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
1090 if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
1091 syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
1095 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
1096 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
1097 syslog(LOG_ERR, "ioctl(SIOCAIFADDR): %m");
1100 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
1101 if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
1102 syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
1111 * cifaddr - Clear the interface IP addresses, and delete routes
1112 * through the interface if possible.
1114 * N.B.: under NextStep, you can't *delete* an address on an interface,
1115 * so we change it to 0.0.0.0... A real hack. But it simplifies
1116 * reconnection on the server side.
1127 (void) sifaddr(u, o, h, 0L);
1130 BZERO(&rt, sizeof(rt));
1131 SET_SA_FAMILY(rt.rt_dst, AF_INET);
1132 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
1133 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1134 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
1135 rt.rt_flags = RTF_HOST;
1136 if (ioctl(sockfd, SIOCDELRT, (caddr_t) &rt) < 0) {
1137 syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
1144 * sifdefaultroute - assign a default route through the address given.
1147 sifdefaultroute(u, g)
1151 return dodefaultroute(g, 's');
1155 * cifdefaultroute - delete a default route through the address given.
1158 cifdefaultroute(u, g)
1162 return dodefaultroute(g, 'c');
1166 * dodefaultroute - talk to a routing socket to add/delete a default route.
1169 dodefaultroute(g, cmd)
1175 BZERO(&rt, sizeof(rt));
1176 SET_SA_FAMILY(rt.rt_dst, AF_INET);
1177 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = 0L;
1178 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1179 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
1180 rt.rt_flags = RTF_GATEWAY;
1181 if (ioctl(sockfd, (cmd == 's') ? SIOCADDRT : SIOCDELRT, &rt) < 0) {
1182 syslog(LOG_ERR, "%cifdefaultroute: ioctl(%s): %m", cmd,
1183 (cmd == 's') ? "SIOCADDRT" : "SIOCDELRT");
1186 default_route_gateway = (cmd == 's')? g: 0;
1191 * sifproxyarp - Make a proxy ARP entry for the peer.
1194 sifproxyarp(unit, hisaddr)
1198 struct arpreq arpreq;
1200 BZERO(&arpreq, sizeof(arpreq));
1203 * Get the hardware address of an interface on the same subnet
1204 * as our local address.
1206 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
1207 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
1211 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1212 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1213 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1214 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
1215 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
1219 proxy_arp_addr = hisaddr;
1224 * cifproxyarp - Delete the proxy ARP entry for the peer.
1227 cifproxyarp(unit, hisaddr)
1231 struct arpreq arpreq;
1233 BZERO(&arpreq, sizeof(arpreq));
1234 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1235 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1236 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1237 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m");
1245 * get_ether_addr - get the hardware address of an interface on the
1246 * the same subnet as ipaddr.
1251 get_ether_addr(ipaddr, hwaddr)
1253 struct sockaddr *hwaddr;
1255 struct ifreq *ifr, *ifend, *ifp;
1256 u_int32_t ina, mask;
1257 struct ether_addr dla;
1260 struct ifreq ifs[MAX_IFS];
1261 struct hostent *hostent;
1263 ifc.ifc_len = sizeof(ifs);
1265 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1266 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
1271 * Scan through looking for an interface with an Internet
1272 * address on the same subnet as `ipaddr'.
1274 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1275 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1276 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1277 if (ifr->ifr_addr.sa_family == AF_INET) {
1278 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1279 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1281 * Check that the interface is up, and not point-to-point
1284 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1286 if ((ifreq.ifr_flags &
1287 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1288 != (IFF_UP|IFF_BROADCAST))
1291 * Get its netmask and check that it's on the right subnet.
1293 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1295 mask = ((struct sockaddr_in*)&ifreq.ifr_addr)->sin_addr.s_addr;
1296 if ((ipaddr & mask) != (ina & mask))
1305 syslog(LOG_INFO, "found interface %s for proxy arp", ifr->ifr_name);
1308 * Get the hostname and look for an entry using the ethers database.
1309 * Under NeXTStep this is the best we can do for now.
1311 if ((hostent = gethostbyaddr((char*)&ina, sizeof(ina), AF_INET)) == NULL)
1314 if (ether_by_host(hostent->h_name, &dla)) {
1315 syslog(LOG_INFO, "Add entry for %s in /etc/ethers", hostent->h_name);
1316 return 0; /* it's not there */
1318 hwaddr->sa_family = AF_UNSPEC;
1319 BCOPY(&dla, hwaddr->sa_data, sizeof(dla));
1324 ether_by_host(hostname, etherptr)
1326 struct ether_addr *etherptr;
1328 struct ether_addr *thisptr;
1334 if (!ether_hostton(hostname, etherptr))
1337 * We shall now try and
1338 * find the address in the
1339 * top domain of netinfo.
1341 strcat(strcpy(path, "/machines/"), hostname);
1343 if (ni_open((void *)0, "/", &conn)
1344 || ni_root(conn, &root)
1345 || ni_pathsearch(conn, &root, path)
1346 || ni_lookupprop(conn, &root, "en_address", &val))
1350 * Now we can convert the returned string into an ethernet address.
1352 strcpy(path, val.ni_namelist_val[0]);
1354 if ((thisptr = (struct ether_addr*)ether_aton(path)) == NULL)
1356 BCOPY(thisptr, etherptr, sizeof(struct ether_addr));
1363 * Return user specified netmask, modified by any mask we might determine
1364 * for address `addr' (in network byte order).
1365 * Here we scan through the system's list of interfaces, looking for
1366 * any non-point-to-point interfaces which might appear to be on the same
1367 * network as `addr'. If we find any, we OR in their netmask to the
1368 * user-specified netmask.
1374 u_int32_t mask, nmask, ina;
1375 struct ifreq *ifr, *ifend, ifreq;
1377 struct ifreq ifs[MAX_IFS];
1380 if (IN_CLASSA(addr)) /* determine network mask for address class */
1381 nmask = IN_CLASSA_NET;
1382 else if (IN_CLASSB(addr))
1383 nmask = IN_CLASSB_NET;
1385 nmask = IN_CLASSC_NET;
1386 /* class D nets are disallowed by bad_ip_adrs */
1387 mask = netmask | htonl(nmask);
1390 * Scan through the system's network interfaces.
1392 ifc.ifc_len = sizeof(ifs);
1394 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1395 syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m");
1398 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1399 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1400 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1402 * Check the interface's internet address.
1404 if (ifr->ifr_addr.sa_family != AF_INET)
1406 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1407 if ((ntohl(ina) & nmask) != (addr & nmask))
1410 * Check that the interface is up, and not point-to-point or loopback.
1412 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1413 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1415 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1419 * Get its netmask and OR it into our mask.
1421 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1423 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1432 * daemon - Detach us from the terminal session.
1435 daemon(nochdir, noclose)
1436 int nochdir, noclose;
1440 if ((pid = fork()) < 0)
1443 exit(0); /* parent dies */
1448 fclose(stdin); /* don't need stdin, stdout, stderr */
1460 char *d = malloc(strlen(s) + 1);
1462 if (d) strcpy(d, s);
1467 * This logwtmp() implementation is subject to the following copyright:
1469 * Copyright (c) 1988 The Regents of the University of California.
1470 * All rights reserved.
1472 * Redistribution and use in source and binary forms are permitted
1473 * provided that the above copyright notice and this paragraph are
1474 * duplicated in all such forms and that any documentation,
1475 * advertising materials, and other materials related to such
1476 * distribution and use acknowledge that the software was developed
1477 * by the University of California, Berkeley. The name of the
1478 * University may not be used to endorse or promote products derived
1479 * from this software without specific prior written permission.
1480 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1481 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1482 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1485 #define WTMPFILE "/usr/adm/wtmp"
1488 logwtmp(line, name, host)
1489 char *line, *name, *host;
1495 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1497 if (!fstat(fd, &buf)) {
1498 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1499 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1500 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1501 (void)time(&ut.ut_time);
1502 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1503 (void)ftruncate(fd, buf.st_size);
1509 * Routines for locking and unlocking the serial device, moved here
1513 #define LOCK_PREFIX "/usr/spool/uucp/LCK/LCK.."
1516 * lock - create a lock file for the named device
1525 if ((p = strrchr(dev, '/')) != NULL)
1527 lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1528 if (lock_file == NULL)
1529 novm("lock file name");
1530 strcat(strcpy(lock_file, LOCK_PREFIX), dev);
1532 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1534 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1535 /* Read the lock file to find out who has the device locked */
1536 n = read(fd, &pid, sizeof(pid));
1538 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1541 if (kill(pid, 0) == -1 && errno == ESRCH) {
1542 /* pid no longer exists - remove the lock file */
1543 if (unlink(lock_file) == 0) {
1545 syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1549 syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
1552 syslog(LOG_NOTICE, "Device %s is locked by pid %d",
1557 syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1564 write(fd, &pid, sizeof pid);
1571 * unlock - remove our lockfile
1583 #if defined(i386) && defined(HAS_BROKEN_IOCTL)
1594 u_char let, code, size;
1596 size = (cmd >> 16) & IOCPARM_MASK;
1600 if (let == 't' && (75 <= code && code <= 90))
1601 syslog(LOG_INFO, "ioctl(%d, 0x%x ('%c', %d, %d), 0x%x)\n", fd, cmd,
1602 let, code, size, c);
1605 ret = ioctl(fd, cmd, c);
1610 syslog(LOG_INFO, "ioctl('%c', %d, %d) errno = %d (%m)\n",
1611 let, code, size, errno);
1612 if (let == 't' && (75 <= code && code <= 90) && (cmd & IOC_OUT)) {
1613 int i, len = ((cmd >> 16) & IOCPARM_MASK);
1614 for (i = 0; i < len / 4; ++i)
1615 syslog(LOG_INFO, "word[%d] @ 0x%06x = 0x%x\n",
1616 i, &((int *) c)[i],((int *)c)[i]);
1621 if (ret == -1 && errno == EPERM)
1625 #endif /* HAS_BROKEN_IOCTL */
1628 #if defined(FIXSIGS) && (defined (hppa) || defined(sparc))
1631 * These redefinitions of Posix functions are necessary
1632 * because HPPA systems have an OS bug that causes
1633 * sigaction to core dump:
1635 * AlainF 9-Nov-1994 HACK FOR HP-PA/NEXTSTEP
1636 * sigaction(3) seems broken in the HP-PA NeXTSTEP 3.2
1637 * Posix lib. This causes pppd to SIGBUS at the expiration
1638 * of the first timeout (_sigtramp seems to invoke
1639 * the SIGALRM handler at an unreasonably low address).
1640 * All calls so sigaction(3) have been changed to calls
1641 * to sigvec(2) and sigprocmask(SIG_BLOCK,...) to
1643 * This is kind of a hack, especially since there are
1644 * other routines of the Posix lib still used, but
1647 * Dave Hess <David-Hess@net.tamu.edu> noted that 3.3 Sparc seems to
1648 * have the same bug. Thus this fix has been enabled for SPARC also.
1653 int sigemptyset(sigset_t *mask)
1658 sigaddset(sigset_t *mask, int which_sig)
1660 *mask |= sigmask(which_sig);
1664 int sigaction(int sig, const struct sigaction *act, struct sigaction *oact)
1669 sv.sv_handler = act->sa_handler;
1670 sv.sv_mask = act->sa_mask;
1676 syslog(LOG_WARNING, "PPPD: Inside modified HP and SPARC sigaction\n");
1679 return sigvec(sig, &sv, NULL);