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.5 1996/04/04 04:04:17 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 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
104 setlogmask(LOG_UPTO(LOG_INFO));
106 setlogmask(LOG_UPTO(LOG_DEBUG));
108 /* Get an internet socket for doing socket ioctl's on. */
109 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
110 syslog(LOG_ERR, "Couldn't create IP socket: %m");
116 * sys_cleanup - restore any system state we modified before exiting:
117 * mark the interface down, delete default route and/or proxy arp entry.
118 * This should call die() because it's called from die().
126 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
127 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) >= 0
128 && ((ifr.ifr_flags & IFF_UP) != 0)) {
129 ifr.ifr_flags &= ~IFF_UP;
130 ioctl(sockfd, SIOCSIFFLAGS, &ifr);
134 cifaddr(0, ifaddrs[0], ifaddrs[1]);
135 if (default_route_gateway)
136 cifdefaultroute(0, default_route_gateway);
138 cifproxyarp(0, proxy_arp_addr);
142 * sys_close - Clean up in a child process before execing.
148 if (loop_slave >= 0) {
156 * sys_check_options - check the options that the user specified
165 * note_debug_level - note a change in the debug level.
171 syslog(LOG_INFO, "Debug turned ON, Level %d", debug);
172 setlogmask(LOG_UPTO(LOG_DEBUG));
174 setlogmask(LOG_UPTO(LOG_WARNING));
179 * ppp_available - check whether the system has any ppp interfaces
180 * (in fact we check whether we can do an ioctl on ppp0).
188 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
189 return 1; /* can't tell - maybe we're not root */
191 strncpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
192 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
199 * establish_ppp - Turn the serial port into a ppp interface.
205 int pppdisc = PPPDISC;
210 * Demand mode - prime the old ppp device to relinquish the unit.
212 if (ioctl(ppp_fd, PPPIOCXFERUNIT, 0) < 0) {
213 syslog(LOG_ERR, "ioctl(transfer ppp unit): %m");
219 * Save the old line discipline of fd, and set it to PPP.
221 if (ioctl(fd, TIOCGETD, &initdisc) < 0) {
222 syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
225 if (ioctl(fd, TIOCSETD, &pppdisc) < 0) {
226 syslog(LOG_ERR, "ioctl(establish TIOCSETD): %m");
232 * Find out which interface we were given.
234 if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {
235 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
240 * Check that we got the same unit again.
242 if (ioctl(fd, PPPIOCGUNIT, &x) < 0) {
243 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
247 syslog(LOG_ERR, "transfer_ppp failed: wanted unit %d, got %d",
252 ioctl(loop_slave, TIOCSETD, &x);
258 * Enable debug in the driver if requested.
261 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
262 syslog(LOG_WARNING, "ioctl(PPPIOCGFLAGS): %m");
264 x |= (kdebugflag & 0xFF) * SC_DEBUG;
265 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
266 syslog(LOG_WARNING, "ioctl(PPPIOCSFLAGS): %m");
271 * Set device for non-blocking reads.
273 if ((initfdflags = fcntl(fd, F_GETFL)) == -1
274 || fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
275 syslog(LOG_WARNING, "Couldn't set device to non-blocking mode: %m");
280 * restore_loop - reattach the ppp unit to the loopback.
288 * Transfer the ppp interface back to the loopback.
290 if (ioctl(ppp_fd, PPPIOCXFERUNIT, 0) < 0) {
291 syslog(LOG_ERR, "ioctl(transfer ppp unit): %m");
295 if (ioctl(loop_slave, TIOCSETD, &x) < 0) {
296 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
301 * Check that we got the same unit again.
303 if (ioctl(loop_slave, PPPIOCGUNIT, &x) < 0) {
304 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
308 syslog(LOG_ERR, "transfer_ppp failed: wanted unit %d, got %d",
317 * disestablish_ppp - Restore the serial port to normal operation.
318 * This shouldn't call die() because it's called from die().
324 /* Reset non-blocking mode on fd. */
325 if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
326 syslog(LOG_WARNING, "Couldn't restore device fd flags: %m");
329 /* Restore old line discipline. */
330 if (initdisc >= 0 && ioctl(fd, TIOCSETD, &initdisc) < 0)
331 syslog(LOG_ERR, "ioctl(disestablish TIOCSETD): %m");
339 * Check whether the link seems not to be 8-bit clean.
347 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
349 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
351 s = "bit 7 set to 1";
354 s = "bit 7 set to 0";
364 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
365 syslog(LOG_WARNING, "All received characters had %s", s);
371 * List of valid speeds.
374 int speed_int, speed_val;
450 #warning Defining B115200
461 * Translate from bits/second to a speed_t.
467 struct speed *speedp;
471 for (speedp = speeds; speedp->speed_int; speedp++)
472 if (bps == speedp->speed_int)
473 return speedp->speed_val;
474 syslog(LOG_WARNING, "speed %d not supported", bps);
479 * Translate from a speed_t to bits/second.
485 struct speed *speedp;
489 for (speedp = speeds; speedp->speed_int; speedp++)
490 if (speed == speedp->speed_val)
491 return speedp->speed_int;
497 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
498 * at the requested speed, etc. If `local' is true, set CLOCAL
499 * regardless of whether the modem option was specified.
502 set_up_tty(fd, local)
505 int speed, x, modembits;
508 if (tcgetattr(fd, &tios) < 0) {
509 syslog(LOG_ERR, "tcgetattr: %m");
515 ioctl(fd, TIOCGWINSZ, &wsinfo);
518 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
519 if (crtscts > 0 && !local)
520 tios.c_cflag |= CRTSCTS;
521 else if (crtscts < 0)
522 tios.c_cflag &= ~CRTSCTS;
524 tios.c_cflag |= CS8 | CREAD | HUPCL;
526 tios.c_cflag |= CLOCAL;
528 tios.c_iflag = IGNBRK | IGNPAR;
532 tios.c_cc[VTIME] = 0;
535 tios.c_iflag |= IXON | IXOFF;
536 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
537 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
540 speed = translate_speed(inspeed);
542 cfsetospeed(&tios, speed);
543 cfsetispeed(&tios, speed);
545 speed = cfgetospeed(&tios);
549 modembits = TIOCM_RTS | TIOCM_CTS;
550 if (ioctl(fd, (crtscts ? TIOCMBIS : TIOCMBIC), &modembits) < 0)
551 syslog(LOG_ERR, "ioctl: TIOCMBIS/BIC: %m");
554 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
555 syslog(LOG_ERR, "tcsetattr: %m");
559 baud_rate = inspeed = baud_rate_of(speed);
564 * restore_tty - restore the terminal to the saved settings.
571 if (!default_device) {
573 * Turn off echoing, because otherwise we can get into
574 * a loop with the tty and the modem echoing to each other.
575 * We presume we are the sole user of this tty device, so
576 * when we close it, it will revert to its defaults anyway.
578 inittermios.c_lflag &= ~(ECHO | ECHONL);
580 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
582 syslog(LOG_WARNING, "tcsetattr: %m");
583 ioctl(fd, TIOCSWINSZ, &wsinfo);
589 * setdtr - control the DTR line on the serial port.
590 * This is called from die(), so it shouldn't call die().
592 * The write hack is to get NXFax to recognize that there is
593 * activity on the port. Not using the write nukes
594 * NXFax's capability to determine port usage.
601 int modembits = TIOCM_DTR;
609 /* ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits); */
610 ioctl(fd, (on? TIOCSDTR: TIOCCDTR), 0);
615 * open_ppp_loopback - open the device we use for getting
616 * packets in demand mode, and connect it to a ppp interface.
624 int pppdisc = PPPDISC;
626 if (openpty(&loop_master, &loop_slave, loop_name, NULL, NULL) < 0) {
627 syslog(LOG_ERR, "No free pty for loopback");
630 SYSDEBUG((LOG_DEBUG, "using %s for loopback", loop_name));
632 if (tcgetattr(loop_slave, &tios) == 0) {
633 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB);
634 tios.c_cflag |= CS8 | CREAD;
635 tios.c_iflag = IGNPAR;
638 if (tcsetattr(loop_slave, TCSAFLUSH, &tios) < 0)
639 syslog(LOG_WARNING, "couldn't set attributes on loopback: %m");
642 if ((flags = fcntl(loop_master, F_GETFL)) != -1)
643 if (fcntl(loop_master, F_SETFL, flags | O_NONBLOCK) == -1)
644 syslog(LOG_WARNING, "couldn't set loopback to nonblock: %m");
647 if (ioctl(ppp_fd, TIOCSETD, &pppdisc) < 0) {
648 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
653 * Find out which interface we were given.
655 if (ioctl(ppp_fd, PPPIOCGUNIT, &ifunit) < 0) {
656 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
661 * Enable debug in the driver if requested.
664 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &flags) < 0) {
665 syslog(LOG_WARNING, "ioctl (PPPIOCGFLAGS): %m");
667 flags |= (kdebugflag & 0xFF) * SC_DEBUG;
668 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &flags) < 0)
669 syslog(LOG_WARNING, "ioctl(PPPIOCSFLAGS): %m");
677 * output - Output PPP packet.
686 log_packet(p, len, "sent ");
688 if (write(ttyfd, p, len) < 0) {
689 if (errno == EWOULDBLOCK || errno == ENOBUFS
690 || errno == ENXIO || errno == EIO) {
691 syslog(LOG_WARNING, "write: warning: %m");
693 syslog(LOG_ERR, "write: %m");
701 * wait_input - wait until there is data available on ttyfd,
702 * for the length of time specified by *timo (indefinite
707 struct timeval *timo;
713 FD_SET(ttyfd, &ready);
714 n = select(ttyfd+1, &ready, NULL, &ready, timo);
715 if (n < 0 && errno != EINTR) {
716 syslog(LOG_ERR, "select: %m");
723 * wait_loop_output - wait until there is data available on the
724 * loopback, for the length of time specified by *timo (indefinite
727 wait_loop_output(timo)
728 struct timeval *timo;
734 FD_SET(loop_master, &ready);
735 n = select(loop_master + 1, &ready, NULL, &ready, timo);
736 if (n < 0 && errno != EINTR) {
737 syslog(LOG_ERR, "select: %m");
744 * wait_time - wait for a given length of time or until a
745 * signal is received.
748 struct timeval *timo;
752 n = select(0, NULL, NULL, NULL, timo);
753 if (n < 0 && errno != EINTR) {
754 syslog(LOG_ERR, "select: %m");
761 * read_packet - get a PPP packet from the serial device.
769 if ((len = read(ttyfd, buf, PPP_MTU + PPP_HDRLEN)) < 0) {
770 if (errno == EWOULDBLOCK || errno == EINTR)
772 syslog(LOG_ERR, "read: %m");
780 * get_loop_output - read characters from the loopback, form them
781 * into frames, and detect when we want to bring the real link up.
782 * Return value is 1 if we need to bring up the link, 0 otherwise.
790 while ((n = read(loop_master, inbuf, sizeof(inbuf))) >= 0) {
791 if (loop_chars(inbuf, n))
796 syslog(LOG_ERR, "eof on loopback");
798 } else if (errno != EWOULDBLOCK){
799 syslog(LOG_ERR, "read from loopback: %m");
808 * ppp_send_config - configure the transmit characteristics of
812 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
820 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
822 if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
823 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
827 if (ioctl(ppp_fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
828 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
832 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
833 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
836 x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
837 x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
838 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
839 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
846 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
849 ppp_set_xaccm(unit, accm)
853 if (ioctl(ppp_fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
854 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
859 * ppp_recv_config - configure the receive-side characteristics of
863 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
870 if (ioctl(ppp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0) {
871 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
874 if (ioctl(ppp_fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
875 syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m");
878 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
879 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
882 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
883 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
884 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
890 * ccp_test - ask kernel whether a given compression method
891 * is acceptable for use. Returns 1 if the method and parameters
892 * are OK, 0 if the method is known but the parameters are not OK
893 * (e.g. code size should be reduced), or -1 if the method is unknown.
896 ccp_test(unit, opt_ptr, opt_len, for_transmit)
897 int unit, opt_len, for_transmit;
900 struct ppp_option_data data;
903 data.length = opt_len;
904 data.transmit = for_transmit;
905 if (ioctl(ttyfd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
907 return (errno == ENOBUFS)? 0: -1;
911 * ccp_flags_set - inform kernel about the current state of CCP.
914 ccp_flags_set(unit, isopen, isup)
915 int unit, isopen, isup;
919 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
920 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
923 x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
924 x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
925 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
926 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
930 * ccp_fatal_error - returns 1 if decompression was disabled as a
931 * result of an error detected after decompression of a packet,
932 * 0 otherwise. This is necessary because of patent nonsense.
935 ccp_fatal_error(unit)
940 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
941 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
944 return x & SC_DC_FERROR;
948 * get_idle_time - return how long the link has been idle.
955 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 (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.
1020 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
1021 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
1022 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
1025 ifr.ifr_flags |= IFF_UP;
1026 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
1027 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
1031 npi.protocol = PPP_IP;
1032 npi.mode = NPMODE_PASS;
1033 if (ioctl(ppp_fd, PPPIOCSNPMODE, &npi) < 0) {
1034 syslog(LOG_ERR, "ioctl(set IP mode to PASS): %m");
1041 * sifnpmode - Set the mode for handling packets for a given NP.
1044 sifnpmode(u, proto, mode)
1051 npi.protocol = proto;
1053 if (ioctl(ppp_fd, PPPIOCSNPMODE, &npi) < 0) {
1054 syslog(LOG_ERR, "ioctl(set NP %d mode to %d): %m", proto, mode);
1061 * sifdown - Config the interface down and disable IP.
1072 npi.protocol = PPP_IP;
1073 npi.mode = NPMODE_ERROR;
1074 ioctl(ppp_fd, PPPIOCSNPMODE, (caddr_t) &npi);
1075 /* ignore errors, because ppp_fd might have been closed by now. */
1077 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
1078 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
1079 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
1082 ifr.ifr_flags &= ~IFF_UP;
1083 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
1084 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
1093 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
1096 #define SET_SA_FAMILY(addr, family) \
1097 BZERO((char *) &(addr), sizeof(addr)); \
1098 addr.sa_family = (family);
1101 * sifaddr - Config the interface IP addresses and netmask.
1112 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1113 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
1115 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
1116 syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
1117 if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
1118 syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
1122 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
1123 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
1124 syslog(LOG_ERR, "ioctl(SIOCAIFADDR): %m");
1127 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
1128 if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
1129 syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
1138 * cifaddr - Clear the interface IP addresses, and delete routes
1139 * through the interface if possible.
1141 * N.B.: under NextStep, you can't *delete* an address on an interface,
1142 * so we change it to 0.0.0.0... A real hack. But it simplifies
1143 * reconnection on the server side.
1154 (void) sifaddr(u, o, h, 0L);
1157 BZERO(&rt, sizeof(rt));
1158 SET_SA_FAMILY(rt.rt_dst, AF_INET);
1159 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
1160 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1161 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
1162 rt.rt_flags = RTF_HOST;
1163 if (ioctl(sockfd, SIOCDELRT, (caddr_t) &rt) < 0) {
1164 syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
1171 * sifdefaultroute - assign a default route through the address given.
1174 sifdefaultroute(u, g)
1178 return dodefaultroute(g, 's');
1182 * cifdefaultroute - delete a default route through the address given.
1185 cifdefaultroute(u, g)
1189 return dodefaultroute(g, 'c');
1193 * dodefaultroute - talk to a routing socket to add/delete a default route.
1196 dodefaultroute(g, cmd)
1202 BZERO(&rt, sizeof(rt));
1203 SET_SA_FAMILY(rt.rt_dst, AF_INET);
1204 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = 0L;
1205 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1206 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
1207 rt.rt_flags = RTF_GATEWAY;
1208 if (ioctl(sockfd, (cmd == 's') ? SIOCADDRT : SIOCDELRT, &rt) < 0) {
1209 syslog(LOG_ERR, "%cifdefaultroute: ioctl(%s): %m", cmd,
1210 (cmd == 's') ? "SIOCADDRT" : "SIOCDELRT");
1213 default_route_gateway = (cmd == 's')? g: 0;
1218 * sifproxyarp - Make a proxy ARP entry for the peer.
1221 sifproxyarp(unit, hisaddr)
1225 struct arpreq arpreq;
1227 BZERO(&arpreq, sizeof(arpreq));
1230 * Get the hardware address of an interface on the same subnet
1231 * as our local address.
1233 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
1234 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
1238 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1239 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1240 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1241 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
1242 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
1246 proxy_arp_addr = hisaddr;
1251 * cifproxyarp - Delete the proxy ARP entry for the peer.
1254 cifproxyarp(unit, hisaddr)
1258 struct arpreq arpreq;
1260 BZERO(&arpreq, sizeof(arpreq));
1261 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1262 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1263 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1264 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m");
1272 * get_ether_addr - get the hardware address of an interface on the
1273 * the same subnet as ipaddr.
1278 get_ether_addr(ipaddr, hwaddr)
1280 struct sockaddr *hwaddr;
1282 struct ifreq *ifr, *ifend, *ifp;
1283 u_int32_t ina, mask;
1284 struct ether_addr dla;
1287 struct ifreq ifs[MAX_IFS];
1288 struct hostent *hostent;
1290 ifc.ifc_len = sizeof(ifs);
1292 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1293 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
1298 * Scan through looking for an interface with an Internet
1299 * address on the same subnet as `ipaddr'.
1301 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1302 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1303 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1304 if (ifr->ifr_addr.sa_family == AF_INET) {
1305 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1306 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1308 * Check that the interface is up, and not point-to-point
1311 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1313 if ((ifreq.ifr_flags &
1314 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1315 != (IFF_UP|IFF_BROADCAST))
1318 * Get its netmask and check that it's on the right subnet.
1320 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1322 mask = ((struct sockaddr_in*)&ifreq.ifr_addr)->sin_addr.s_addr;
1323 if ((ipaddr & mask) != (ina & mask))
1332 syslog(LOG_INFO, "found interface %s for proxy arp", ifr->ifr_name);
1335 * Get the hostname and look for an entry using the ethers database.
1336 * Under NeXTStep this is the best we can do for now.
1338 if ((hostent = gethostbyaddr((char*)&ina, sizeof(ina), AF_INET)) == NULL)
1341 if (ether_by_host(hostent->h_name, &dla)) {
1342 syslog(LOG_INFO, "Add entry for %s in /etc/ethers", hostent->h_name);
1343 return 0; /* it's not there */
1345 hwaddr->sa_family = AF_UNSPEC;
1346 BCOPY(&dla, hwaddr->sa_data, sizeof(dla));
1351 ether_by_host(hostname, etherptr)
1353 struct ether_addr *etherptr;
1355 struct ether_addr *thisptr;
1361 if (!ether_hostton(hostname, etherptr))
1364 * We shall now try and
1365 * find the address in the
1366 * top domain of netinfo.
1368 strcat(strcpy(path, "/machines/"), hostname);
1370 if (ni_open((void *)0, "/", &conn)
1371 || ni_root(conn, &root)
1372 || ni_pathsearch(conn, &root, path)
1373 || ni_lookupprop(conn, &root, "en_address", &val))
1377 * Now we can convert the returned string into an ethernet address.
1379 strcpy(path, val.ni_namelist_val[0]);
1381 if ((thisptr = (struct ether_addr*)ether_aton(path)) == NULL)
1383 BCOPY(thisptr, etherptr, sizeof(struct ether_addr));
1390 * Return user specified netmask, modified by any mask we might determine
1391 * for address `addr' (in network byte order).
1392 * Here we scan through the system's list of interfaces, looking for
1393 * any non-point-to-point interfaces which might appear to be on the same
1394 * network as `addr'. If we find any, we OR in their netmask to the
1395 * user-specified netmask.
1401 u_int32_t mask, nmask, ina;
1402 struct ifreq *ifr, *ifend, ifreq;
1404 struct ifreq ifs[MAX_IFS];
1407 if (IN_CLASSA(addr)) /* determine network mask for address class */
1408 nmask = IN_CLASSA_NET;
1409 else if (IN_CLASSB(addr))
1410 nmask = IN_CLASSB_NET;
1412 nmask = IN_CLASSC_NET;
1413 /* class D nets are disallowed by bad_ip_adrs */
1414 mask = netmask | htonl(nmask);
1417 * Scan through the system's network interfaces.
1419 ifc.ifc_len = sizeof(ifs);
1421 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1422 syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m");
1425 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1426 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1427 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1429 * Check the interface's internet address.
1431 if (ifr->ifr_addr.sa_family != AF_INET)
1433 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1434 if ((ntohl(ina) & nmask) != (addr & nmask))
1437 * Check that the interface is up, and not point-to-point or loopback.
1439 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1440 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1442 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1446 * Get its netmask and OR it into our mask.
1448 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1450 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1459 * daemon - Detach us from the terminal session.
1462 daemon(nochdir, noclose)
1463 int nochdir, noclose;
1467 if ((pid = fork()) < 0)
1470 exit(0); /* parent dies */
1475 fclose(stdin); /* don't need stdin, stdout, stderr */
1487 char *d = malloc(strlen(s) + 1);
1489 if (d) strcpy(d, s);
1494 * This logwtmp() implementation is subject to the following copyright:
1496 * Copyright (c) 1988 The Regents of the University of California.
1497 * All rights reserved.
1499 * Redistribution and use in source and binary forms are permitted
1500 * provided that the above copyright notice and this paragraph are
1501 * duplicated in all such forms and that any documentation,
1502 * advertising materials, and other materials related to such
1503 * distribution and use acknowledge that the software was developed
1504 * by the University of California, Berkeley. The name of the
1505 * University may not be used to endorse or promote products derived
1506 * from this software without specific prior written permission.
1507 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1508 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1509 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1512 #define WTMPFILE "/usr/adm/wtmp"
1515 logwtmp(line, name, host)
1516 char *line, *name, *host;
1522 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1524 if (!fstat(fd, &buf)) {
1525 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1526 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1527 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1528 (void)time(&ut.ut_time);
1529 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1530 (void)ftruncate(fd, buf.st_size);
1536 * Routines for locking and unlocking the serial device, moved here
1540 #define LOCK_PREFIX "/usr/spool/uucp/LCK/LCK.."
1543 * lock - create a lock file for the named device
1552 if ((p = strrchr(dev, '/')) != NULL)
1554 lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1555 if (lock_file == NULL)
1556 novm("lock file name");
1557 strcat(strcpy(lock_file, LOCK_PREFIX), dev);
1559 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1561 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1562 /* Read the lock file to find out who has the device locked */
1563 n = read(fd, &pid, sizeof(pid));
1565 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1568 if (kill(pid, 0) == -1 && errno == ESRCH) {
1569 /* pid no longer exists - remove the lock file */
1570 if (unlink(lock_file) == 0) {
1572 syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1576 syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
1579 syslog(LOG_NOTICE, "Device %s is locked by pid %d",
1584 syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1591 write(fd, &pid, sizeof pid);
1598 * unlock - remove our lockfile
1610 #if defined(i386) && defined(HAS_BROKEN_IOCTL)
1621 u_char let, code, size;
1623 size = (cmd >> 16) & IOCPARM_MASK;
1627 if (let == 't' && (75 <= code && code <= 90))
1628 syslog(LOG_INFO, "ioctl(%d, 0x%x ('%c', %d, %d), 0x%x)\n", fd, cmd,
1629 let, code, size, c);
1632 ret = ioctl(fd, cmd, c);
1637 syslog(LOG_INFO, "ioctl('%c', %d, %d) errno = %d (%m)\n",
1638 let, code, size, errno);
1639 if (let == 't' && (75 <= code && code <= 90) && (cmd & IOC_OUT)) {
1640 int i, len = ((cmd >> 16) & IOCPARM_MASK);
1641 for (i = 0; i < len / 4; ++i)
1642 syslog(LOG_INFO, "word[%d] @ 0x%06x = 0x%x\n",
1643 i, &((int *) c)[i],((int *)c)[i]);
1648 if (ret == -1 && errno == EPERM)
1652 #endif /* HAS_BROKEN_IOCTL */
1655 #if defined(FIXSIGS) && (defined (hppa) || defined(sparc))
1658 * These redefinitions of Posix functions are necessary
1659 * because HPPA systems have an OS bug that causes
1660 * sigaction to core dump:
1662 * AlainF 9-Nov-1994 HACK FOR HP-PA/NEXTSTEP
1663 * sigaction(3) seems broken in the HP-PA NeXTSTEP 3.2
1664 * Posix lib. This causes pppd to SIGBUS at the expiration
1665 * of the first timeout (_sigtramp seems to invoke
1666 * the SIGALRM handler at an unreasonably low address).
1667 * All calls so sigaction(3) have been changed to calls
1668 * to sigvec(2) and sigprocmask(SIG_BLOCK,...) to
1670 * This is kind of a hack, especially since there are
1671 * other routines of the Posix lib still used, but
1674 * Dave Hess <David-Hess@net.tamu.edu> noted that 3.3 Sparc seems to
1675 * have the same bug. Thus this fix has been enabled for SPARC also.
1680 int sigemptyset(sigset_t *mask)
1685 sigaddset(sigset_t *mask, int which_sig)
1687 *mask |= sigmask(which_sig);
1691 int sigaction(int sig, const struct sigaction *act, struct sigaction *oact)
1696 sv.sv_handler = act->sa_handler;
1697 sv.sv_mask = act->sa_mask;
1703 syslog(LOG_WARNING, "PPPD: Inside modified HP and SPARC sigaction\n");
1706 return sigvec(sig, &sv, NULL);