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.
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. The name of the
15 * University may not be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
19 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 #define RCSID "$Id: sys-NeXT.c,v 1.20 1999/08/13 06:46:17 paulus Exp $"
31 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <sys/ioctl.h>
36 #include <sys/errno.h>
38 #include <sys/fcntl.h>
41 #include <net/ppp_defs.h>
42 #include <net/if_ppp.h>
44 #include <netinet/in.h>
45 #include <netinet/in_systm.h>
46 #include <netinet/in_var.h>
47 #if !(NS_TARGET >= 40)
48 /* XXX get an error "duplicate member ip_v under 4.1 GAMMA */
49 #include <netinet/ip.h>
50 #endif /* NS_TARGET */
51 #include <netinet/if_ether.h>
52 #include <net/route.h>
53 #include <netinet/in.h>
55 #include <netinfo/ni.h>
59 static const char rcsid[] = RCSID;
61 static int initdisc = -1; /* Initial TTY discipline */
62 static int initfdflags = -1; /* Initial file descriptor flags for fd */
63 static int ppp_fd = -1; /* fd which is set to PPP discipline */
64 static int loop_slave = -1;
65 static int loop_master;
66 static char loop_name[20];
68 static fd_set in_fds; /* set of fds that wait_input waits for */
69 static int max_in_fd; /* highest fd set in in_fds */
73 static int restore_term; /* 1 => we've munged the terminal */
74 static struct termios inittermios; /* Initial TTY termios */
76 static int sockfd; /* socket for doing interface ioctls */
77 static int pppdev; /* +++ */
79 #if defined(i386) && defined(HAS_BROKEN_IOCTL)
83 static int if_is_up; /* the interface is currently up */
84 static u_int32_t default_route_gateway; /* gateway addr for default route */
85 static u_int32_t proxy_arp_addr; /* remote addr for proxy arp */
87 /* Prototypes for procedures local to this file. */
88 static int translate_speed __P((int));
89 static int baud_rate_of __P((int));
90 static int dodefaultroute __P((u_int32_t, int));
91 static int get_ether_addr __P((u_int32_t, struct sockaddr *));
92 static int ether_by_host __P((char *, struct ether_addr *));
96 * sys_init - System-dependent initialization.
101 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
102 setlogmask(LOG_UPTO(LOG_INFO));
104 /* Get an internet socket for doing socket ioctl's on. */
105 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
106 fatal("Couldn't create IP socket: %m");
108 if((pppdev = open("/dev/ppp0", O_RDWR, O_NONBLOCK)) == NULL)
109 fatal("Couldn't open /dev/ppp0: %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 strlcpy(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 if (default_route_gateway)
135 cifdefaultroute(0, 0, default_route_gateway);
137 cifproxyarp(0, proxy_arp_addr);
143 * ppp_available - check whether the system has any ppp interfaces
144 * (in fact we check whether we can do an ioctl on ppp0).
151 extern char *no_ppp_msg;
153 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
154 return 1; /* can't tell - maybe we're not root */
156 strlcpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
157 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
161 This system lacks kernel support for PPP. To include PPP support\n\
162 in the kernel, please follow the steps detailed in the README.NeXT\n\
163 file in the ppp-2.2 distribution.\n";
169 * establish_ppp - Turn the serial port into a ppp interface.
175 int pppdisc = PPPDISC;
178 if (ioctl(fd, TIOCGETD, &initdisc) < 0)
179 fatal("ioctl(TIOCGETD): %m");
180 if (ioctl(fd, TIOCSETD, &pppdisc) < 0)
181 fatal("ioctl(establish TIOCSETD): %m");
184 * Find out which interface we were given.
186 if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0)
187 fatal("ioctl(PPPIOCGUNIT): %m");
190 * Enable debug in the driver if requested.
193 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
194 warn("ioctl(PPPIOCGFLAGS): %m");
196 x |= (kdebugflag & 0xFF) * SC_DEBUG;
197 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
198 warn("ioctl(PPPIOCSFLAGS): %m");
203 * Set device for non-blocking reads so PPPD can poll for
204 * input from the kernel.
206 if ((initfdflags = fcntl(fd, F_GETFL)) == -1
207 || fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
208 warn("Couldn't set device to non-blocking mode: %m");
216 * disestablish_ppp - Restore the serial port to normal operation.
217 * This shouldn't call die() because it's called from die().
223 /* Reset non-blocking mode on fd. */
224 if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
225 warn("Couldn't restore device fd flags: %m");
228 /* Restore old line discipline. */
229 if (initdisc >= 0 && ioctl(fd, TIOCSETD, &initdisc) < 0)
230 error("ioctl(TIOCSETD): %m");
235 * Check whether the link seems not to be 8-bit clean.
243 if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
245 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
247 s = "bit 7 set to 1";
250 s = "bit 7 set to 0";
260 warn("Serial link is not 8-bit clean:");
261 warn("All received characters had %s", s);
267 * List of valid speeds.
270 int speed_int, speed_val;
346 #warning Defining B115200
357 * Translate from bits/second to a speed_t.
363 struct speed *speedp;
367 for (speedp = speeds; speedp->speed_int; speedp++)
368 if (bps == speedp->speed_int)
369 return speedp->speed_val;
370 warn("speed %d not supported", bps);
375 * Translate from a speed_t to bits/second.
381 struct speed *speedp;
385 for (speedp = speeds; speedp->speed_int; speedp++)
386 if (speed == speedp->speed_val)
387 return speedp->speed_int;
393 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
394 * at the requested speed, etc. If `local' is true, set CLOCAL
395 * regardless of whether the modem option was specified.
398 set_up_tty(fd, local)
401 int speed, x, modembits;
404 if (tcgetattr(fd, &tios) < 0)
405 fatal("tcgetattr: %m");
410 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
412 tios.c_cflag |= CS8 | CREAD | HUPCL;
414 tios.c_cflag |= CLOCAL;
416 tios.c_iflag = IGNBRK | IGNPAR;
420 tios.c_cc[VTIME] = 0;
423 tios.c_iflag |= IXON | IXOFF;
424 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
425 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
428 speed = translate_speed(inspeed);
430 cfsetospeed(&tios, speed);
431 cfsetispeed(&tios, speed);
433 speed = cfgetospeed(&tios);
435 * We can't proceed if the serial port speed is B0,
436 * since that implies that the serial port is disabled.
439 fatal("Baud rate for %s is 0; need explicit baud rate",
444 modembits = TIOCM_RTS | TIOCM_CTS;
445 if (ioctl(fd, (crtscts ? TIOCMBIS : TIOCMBIC), &modembits) < 0)
446 error("ioctl: TIOCMBIS/BIC: %m");
449 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0)
450 fatal("tcsetattr: %m");
452 baud_rate = inspeed = baud_rate_of(speed);
457 * restore_tty - restore the terminal to the saved settings.
464 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
466 warn("tcsetattr: %m");
472 * setdtr - control the DTR line on the serial port.
473 * This is called from die(), so it shouldn't call die().
475 * The write hack is to get NXFax to recognize that there is
476 * activity on the port. Not using the write nukes
477 * NXFax's capability to determine port usage.
484 int modembits = TIOCM_DTR;
492 /* ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits); */
493 ioctl(fd, (on? TIOCSDTR: TIOCCDTR), 0);
498 * output - Output PPP packet.
507 dbglog("sent %P", p, len);
509 if (write(ttyfd, p, len) < 0) {
510 if (errno == EWOULDBLOCK || errno == ENOBUFS
511 || errno == ENXIO || errno == EIO) {
512 warn("write: warning: %m");
521 * wait_input - wait until there is data available,
522 * for the length of time specified by *timo (indefinite
527 struct timeval *timo;
533 n = select(max_in_fd + 1, &ready, NULL, &ready, timo);
534 if (n < 0 && errno != EINTR)
540 * add_fd - add an fd to the set that wait_input waits for.
551 * remove_fd - remove an fd from the set that wait_input waits for.
560 * read_packet - get a PPP packet from the serial device.
568 if ((len = read(ttyfd, buf, PPP_MTU + PPP_HDRLEN)) < 0) {
569 if (errno == EWOULDBLOCK || errno == EINTR) {
570 SYSDEBUG(("read: %m"));
580 * ppp_send_config - configure the transmit characteristics of
584 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
592 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
594 if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0)
595 fatal("ioctl(SIOCSIFMTU): %m");
597 if (ioctl(ttyfd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0)
598 fatal("ioctl(PPPIOCSASYNCMAP): %m");
600 if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) < 0)
601 fatal("ioctl(PPPIOCGFLAGS): %m");
603 x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
604 x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
605 if (ioctl(ttyfd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
606 fatal("ioctl(PPPIOCSFLAGS): %m");
611 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
614 ppp_set_xaccm(unit, accm)
618 if (ioctl(ttyfd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
619 warn("ioctl(PPPIOCSXASYNCMAP): %m");
624 * ppp_recv_config - configure the receive-side characteristics of
628 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
635 if (ioctl(ttyfd, PPPIOCSMRU, (caddr_t) &mru) < 0)
636 fatal("ioctl(PPPIOCSMRU): %m");
637 if (ioctl(ttyfd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0)
638 fatal("ioctl(PPPIOCSRASYNCMAP): %m");
639 if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) < 0)
640 fatal("ioctl(PPPIOCGFLAGS): %m");
641 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
642 if (ioctl(ttyfd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
643 fatal("ioctl(PPPIOCSFLAGS): %m");
647 * ccp_test - ask kernel whether a given compression method
648 * is acceptable for use.
651 ccp_test(unit, opt_ptr, opt_len, for_transmit)
652 int unit, opt_len, for_transmit;
655 struct ppp_option_data data;
658 data.length = opt_len;
659 data.transmit = for_transmit;
660 if (ioctl(ttyfd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
662 return (errno == ENOBUFS)? 0: -1;
666 * ccp_flags_set - inform kernel about the current state of CCP.
669 ccp_flags_set(unit, isopen, isup)
670 int unit, isopen, isup;
674 if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
675 error("ioctl(PPPIOCGFLAGS): %m");
678 x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
679 x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
680 if (ioctl(ttyfd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
681 error("ioctl(PPPIOCSFLAGS): %m");
685 * ccp_fatal_error - returns 1 if decompression was disabled as a
686 * result of an error detected after decompression of a packet,
687 * 0 otherwise. This is necessary because of patent nonsense.
690 ccp_fatal_error(unit)
695 if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
696 error("ioctl(PPPIOCGFLAGS): %m");
699 return x & SC_DC_FERROR;
703 * sifvjcomp - config tcp header compression
706 sifvjcomp(u, vjcomp, cidcomp, maxcid)
707 int u, vjcomp, cidcomp, maxcid;
711 if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
712 error("ioctl(PPIOCGFLAGS): %m");
715 x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
716 x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
717 if (ioctl(ttyfd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
718 error("ioctl(PPPIOCSFLAGS): %m");
721 if (ioctl(ttyfd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
722 error("ioctl(PPPIOCSFLAGS): %m");
729 * sifup - Config the interface up and enable IP packets to pass.
732 #define SC_ENABLE_IP 0x100 /* compat for old versions of kernel code */
743 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
744 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
745 error("ioctl (SIOCGIFFLAGS): %m");
748 ifr.ifr_flags |= IFF_UP;
749 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
750 error("ioctl(SIOCSIFFLAGS): %m");
754 npi.protocol = PPP_IP;
755 npi.mode = NPMODE_PASS;
756 if (ioctl(ttyfd, PPPIOCSNPMODE, &npi) < 0) {
757 if (errno != ENOTTY) {
758 error("ioctl(PPPIOCSNPMODE): %m");
761 /* for backwards compatibility */
762 if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
763 error("ioctl (PPPIOCGFLAGS): %m");
767 if (ioctl(ttyfd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
768 error("ioctl(PPPIOCSFLAGS): %m");
776 * sifdown - Config the interface down and disable IP.
788 npi.protocol = PPP_IP;
789 npi.mode = NPMODE_ERROR;
790 ioctl(ttyfd, PPPIOCSNPMODE, (caddr_t) &npi);
791 /* ignore errors, because ttyfd might have been closed by now. */
794 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
795 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
796 error("ioctl (SIOCGIFFLAGS): %m");
799 ifr.ifr_flags &= ~IFF_UP;
800 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
801 error("ioctl(SIOCSIFFLAGS): %m");
810 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
813 #define SET_SA_FAMILY(addr, family) \
814 BZERO((char *) &(addr), sizeof(addr)); \
815 addr.sa_family = (family);
818 * sifaddr - Config the interface IP addresses and netmask.
829 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
830 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
831 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
832 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
833 error("ioctl(SIOCAIFADDR): %m");
836 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
837 if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
838 error("ioctl(SIOCSIFDSTADDR): %m");
842 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
843 info("Setting interface mask to %s\n", ip_ntoa(m));
844 if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
845 error("ioctl(SIOCSIFNETMASK): %m");
853 * cifaddr - Clear the interface IP addresses, and delete routes
854 * through the interface if possible.
856 * N.B.: under NextStep, you can't *delete* an address on an interface,
857 * so we change it to 0.0.0.0... A real hack. But it simplifies
858 * reconnection on the server side.
869 (void) sifaddr(u, o, h, 0L);
871 SET_SA_FAMILY(rt.rt_dst, AF_INET);
872 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
873 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
874 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
875 rt.rt_flags = RTF_HOST;
876 if (ioctl(sockfd, SIOCDELRT, (caddr_t) &rt) < 0) {
877 error("ioctl(SIOCDELRT): %m");
884 * sifdefaultroute - assign a default route through the address given.
887 sifdefaultroute(u, l, g)
891 return dodefaultroute(g, 's');
895 * cifdefaultroute - delete a default route through the address given.
898 cifdefaultroute(u, l, g)
902 return dodefaultroute(g, 'c');
906 * dodefaultroute - talk to a routing socket to add/delete a default route.
909 dodefaultroute(g, cmd)
915 SET_SA_FAMILY(rt.rt_dst, AF_INET);
916 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = 0L;
917 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
918 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
919 rt.rt_flags = RTF_GATEWAY;
920 if (ioctl(sockfd, (cmd == 's') ? SIOCADDRT : SIOCDELRT, &rt) < 0) {
921 error("%cifdefaultroute: ioctl(%s): %m", cmd,
922 (cmd == 's') ? "SIOCADDRT" : "SIOCDELRT");
925 default_route_gateway = (cmd == 's')? g: 0;
930 * sifproxyarp - Make a proxy ARP entry for the peer.
933 sifproxyarp(unit, hisaddr)
937 struct arpreq arpreq;
939 BZERO(&arpreq, sizeof(arpreq));
942 * Get the hardware address of an interface on the same subnet
943 * as our local address.
945 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
946 error("Cannot determine ethernet address for proxy ARP");
950 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
951 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
952 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
953 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
954 error("ioctl(SIOCSARP): %m");
958 proxy_arp_addr = hisaddr;
963 * cifproxyarp - Delete the proxy ARP entry for the peer.
966 cifproxyarp(unit, hisaddr)
970 struct arpreq arpreq;
972 BZERO(&arpreq, sizeof(arpreq));
973 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
974 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
975 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
976 warn("ioctl(SIOCDARP): %m");
984 * get_ether_addr - get the hardware address of an interface on the
985 * the same subnet as ipaddr.
990 get_ether_addr(ipaddr, hwaddr)
992 struct sockaddr *hwaddr;
994 struct ifreq *ifr, *ifend, *ifp;
996 struct ether_addr dla;
999 struct ifreq ifs[MAX_IFS];
1000 struct hostent *hostent;
1002 ifc.ifc_len = sizeof(ifs);
1004 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1005 error("ioctl(SIOCGIFCONF): %m");
1010 * Scan through looking for an interface with an Internet
1011 * address on the same subnet as `ipaddr'.
1013 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1014 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1015 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1016 if (ifr->ifr_addr.sa_family == AF_INET) {
1017 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1018 strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1020 * Check that the interface is up, and not point-to-point
1023 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1025 if ((ifreq.ifr_flags &
1026 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1027 != (IFF_UP|IFF_BROADCAST))
1030 * Get its netmask and check that it's on the right subnet.
1032 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1034 mask = ((struct sockaddr_in*)&ifreq.ifr_addr)->sin_addr.s_addr;
1035 if ((ipaddr & mask) != (ina & mask))
1044 info("found interface %s for proxy arp", ifr->ifr_name);
1047 * Get the hostname and look for an entry using the ethers database.
1048 * Under NeXTStep this is the best we can do for now.
1050 if ((hostent = gethostbyaddr((char*)&ina, sizeof(ina), AF_INET)) == NULL)
1053 if (ether_by_host(hostent->h_name, &dla)) {
1054 info("Add entry for %s in /etc/ethers", hostent->h_name);
1055 return 0; /* it's not there */
1057 hwaddr->sa_family = AF_UNSPEC;
1058 BCOPY(&dla, hwaddr->sa_data, sizeof(dla));
1063 ether_by_host(hostname, etherptr)
1065 struct ether_addr *etherptr;
1067 struct ether_addr *thisptr;
1073 if (!ether_hostton(hostname, etherptr))
1076 * We shall now try and
1077 * find the address in the
1078 * top domain of netinfo.
1080 slprintf(path, sizeof(path), "/machines/%s", hostname);
1082 if (ni_open((void *)0, "/", &conn)
1083 || ni_root(conn, &root)
1084 || ni_pathsearch(conn, &root, path)
1085 || ni_lookupprop(conn, &root, "en_address", &val))
1089 * Now we can convert the returned string into an ethernet address.
1091 strlcpy(path, val.ni_namelist_val[0], sizeof(path));
1093 if ((thisptr = (struct ether_addr*)ether_aton(path)) == NULL)
1095 BCOPY(thisptr, etherptr, sizeof(struct ether_addr));
1102 * Return user specified netmask, modified by any mask we might determine
1103 * for address `addr' (in network byte order).
1104 * Here we scan through the system's list of interfaces, looking for
1105 * any non-point-to-point interfaces which might appear to be on the same
1106 * network as `addr'. If we find any, we OR in their netmask to the
1107 * user-specified netmask.
1113 u_int32_t mask, nmask, ina;
1114 struct ifreq *ifr, *ifend, ifreq;
1116 struct ifreq ifs[MAX_IFS];
1119 if (IN_CLASSA(addr)) /* determine network mask for address class */
1120 nmask = IN_CLASSA_NET;
1121 else if (IN_CLASSB(addr))
1122 nmask = IN_CLASSB_NET;
1124 nmask = IN_CLASSC_NET;
1125 /* class D nets are disallowed by bad_ip_adrs */
1126 mask = netmask | htonl(nmask);
1129 * Scan through the system's network interfaces.
1131 ifc.ifc_len = sizeof(ifs);
1133 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1134 warn("ioctl(SIOCGIFCONF): %m");
1137 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1138 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1139 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1141 * Check the interface's internet address.
1143 if (ifr->ifr_addr.sa_family != AF_INET)
1145 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1146 if ((ntohl(ina) & nmask) != (addr & nmask))
1149 * Check that the interface is up, and not point-to-point or loopback.
1151 strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1152 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1154 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1158 * Get its netmask and OR it into our mask.
1160 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1162 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1169 * have_route_to - determine if the system has any route to
1170 * a given IP address.
1171 * For demand mode to work properly, we have to ignore routes
1172 * through our own interface.
1174 int have_route_to(u_int32_t addr)
1181 * daemon - Detach us from the terminal session.
1184 daemon(nochdir, noclose)
1185 int nochdir, noclose;
1189 if ((pid = fork()) < 0)
1192 exit(0); /* parent dies */
1197 fclose(stdin); /* don't need stdin, stdout, stderr */
1209 char *d = malloc(strlen(s) + 1);
1211 if (d) strcpy(d, s);
1216 * This logwtmp() implementation is subject to the following copyright:
1218 * Copyright (c) 1988 The Regents of the University of California.
1219 * All rights reserved.
1221 * Redistribution and use in source and binary forms are permitted
1222 * provided that the above copyright notice and this paragraph are
1223 * duplicated in all such forms and that any documentation,
1224 * advertising materials, and other materials related to such
1225 * distribution and use acknowledge that the software was developed
1226 * by the University of California, Berkeley. The name of the
1227 * University may not be used to endorse or promote products derived
1228 * from this software without specific prior written permission.
1229 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1230 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1231 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1234 #define WTMPFILE "/usr/adm/wtmp"
1237 logwtmp(line, name, host)
1238 const char *line, *name, *host;
1244 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1246 if (!fstat(fd, &buf)) {
1247 strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1248 strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1249 strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1250 (void)time(&ut.ut_time);
1251 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1252 (void)ftruncate(fd, buf.st_size);
1259 * Routines for locking and unlocking the serial device, moved here
1263 #define LOCK_PREFIX "/usr/spool/uucp/LCK/LCK.."
1265 static char *lock_file;
1268 * lock - create a lock file for the named device
1278 if ((p = strrchr(dev, '/')) != NULL)
1280 l = strlen(LOCK_PREFIX) + strlen(dev) + 1;
1281 lock_file = malloc(l);
1282 if (lock_file == NULL)
1283 novm("lock file name");
1284 slprintf(lock_file, l, "%s%s", LOCK_PREFIX, dev);
1286 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1288 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1289 /* Read the lock file to find out who has the device locked */
1290 n = read(fd, &pid, sizeof(pid));
1292 error("Can't read pid from lock file %s", lock_file);
1295 if (kill(pid, 0) == -1 && errno == ESRCH) {
1296 /* pid no longer exists - remove the lock file */
1297 if (unlink(lock_file) == 0) {
1299 notice("Removed stale lock on %s (pid %d)",
1303 warn("Couldn't remove stale lock on %s", dev);
1305 notice("Device %s is locked by pid %d",
1310 error("Can't create lock file %s: %m", lock_file);
1317 write(fd, &pid, sizeof pid);
1324 * unlock - remove our lockfile
1337 #if defined(i386) && defined(HAS_BROKEN_IOCTL)
1348 u_char let, code, size;
1350 size = (cmd >> 16) & IOCPARM_MASK;
1354 if (let == 't' && (75 <= code && code <= 90))
1355 info("ioctl(%d, 0x%x ('%c', %d, %d), 0x%x)\n", fd, cmd,
1356 let, code, size, c);
1359 ret = ioctl(fd, cmd, c);
1364 info("ioctl('%c', %d, %d) errno = %d (%m)\n",
1365 let, code, size, errno);
1366 if (let == 't' && (75 <= code && code <= 90) && (cmd & IOC_OUT)) {
1367 int i, len = ((cmd >> 16) & IOCPARM_MASK);
1368 for (i = 0; i < len / 4; ++i)
1369 info("word[%d] @ 0x%06x = 0x%x\n",
1370 i, &((int *) c)[i],((int *)c)[i]);
1375 if (ret == -1 && errno == EPERM)
1379 #endif /* HAS_BROKEN_IOCTL */
1382 #if defined(FIXSIGS) && (defined (hppa) || defined(sparc))
1385 * These redefinitions of Posix functions are necessary
1386 * because HPPA systems have an OS bug that causes
1387 * sigaction to core dump:
1389 * AlainF 9-Nov-1994 HACK FOR HP-PA/NEXTSTEP
1390 * sigaction(3) seems broken in the HP-PA NeXTSTEP 3.2
1391 * Posix lib. This causes pppd to SIGBUS at the expiration
1392 * of the first timeout (_sigtramp seems to invoke
1393 * the SIGALRM handler at an unreasonably low address).
1394 * All calls so sigaction(3) have been changed to calls
1395 * to sigvec(2) and sigprocmask(SIG_BLOCK,...) to
1397 * This is kind of a hack, especially since there are
1398 * other routines of the Posix lib still used, but
1401 * Dave Hess <David-Hess@net.tamu.edu> noted that 3.3 Sparc seems to
1402 * have the same bug. Thus this fix has been enabled for SPARC also.
1407 int sigemptyset(sigset_t *mask)
1412 sigaddset(sigset_t *mask, int which_sig)
1414 *mask |= sigmask(which_sig);
1418 int sigaction(int sig, const struct sigaction *act, struct sigaction *oact)
1423 sv.sv_handler = act->sa_handler;
1424 sv.sv_mask = act->sa_mask;
1430 warn("PPPD: Inside modified HP and SPARC sigaction\n");
1433 return sigvec(sig, &sv, NULL);
1440 * Code following is added for 2.3 compatibility
1444 * get_idle_time - return how long the link has been idle.
1447 get_idle_time(u, ip)
1449 struct ppp_idle *ip;
1451 return (ioctl(ttyfd, PPPIOCGIDLE, ip) >= 0);
1455 * get_ppp_stats - return statistics for the link.
1458 get_ppp_stats(u, stats)
1460 struct pppd_stats *stats;
1462 struct ifpppstatsreq req;
1464 memset (&req, 0, sizeof (req));
1465 strlcpy(req.ifr_name, interface, sizeof(req.ifr_name));
1466 if (ioctl(sockfd, SIOCGPPPSTATS, &req) < 0) {
1467 error("Couldn't get PPP statistics: %m");
1470 stats->bytes_in = req.stats.p.ppp_ibytes;
1471 stats->bytes_out = req.stats.p.ppp_obytes;
1477 * get_loop_output - read characters from the loopback, form them
1478 * into frames, and detect when we want to bring the real link up.
1479 * Return value is 1 if we need to bring up the link, 0 otherwise.
1489 while ((n = read(loop_master, inbuf, sizeof(inbuf))) >= 0) {
1490 if (loop_chars(inbuf, n))
1495 fatal("eof on loopback");
1496 if (errno != EWOULDBLOCK)
1497 fatal("read from loopback: %m");
1506 * sifnpmode - Set the mode for handling packets for a given NP.
1509 sifnpmode(u, proto, mode)
1516 npi.protocol = proto;
1518 if (ioctl(ttyfd, PPPIOCSNPMODE, &npi) < 0) {
1519 error("ioctl(set NP %d mode to %d): %m", proto, mode);
1527 * open_ppp_loopback - open the device we use for getting
1528 * packets in demand mode, and connect it to a ppp interface.
1529 * Here we use a pty.
1537 struct termios tios;
1538 int pppdisc = PPPDISC;
1540 fatal("open_ppp_loopback called!");
1542 if (openpty(&loop_master, &loop_slave, loop_name, NULL, NULL) < 0)
1543 fatal("No free pty for loopback");
1544 SYSDEBUG(("using %s for loopback", loop_name));
1546 if (tcgetattr(loop_slave, &tios) == 0) {
1547 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB);
1548 tios.c_cflag |= CS8 | CREAD;
1549 tios.c_iflag = IGNPAR;
1552 if (tcsetattr(loop_slave, TCSAFLUSH, &tios) < 0)
1553 warn("couldn't set attributes on loopback: %m");
1556 if ((flags = fcntl(loop_master, F_GETFL)) != -1)
1557 if (fcntl(loop_master, F_SETFL, flags | O_NONBLOCK) == -1)
1558 warn("couldn't set loopback to nonblock: %m");
1561 if (ioctl(ttyfd, TIOCSETD, &pppdisc) < 0)
1562 fatal("ioctl(TIOCSETD): %m");
1565 * Find out which interface we were given.
1567 if (ioctl(ttyfd, PPPIOCGUNIT, &ifunit) < 0)
1568 fatal("ioctl(PPPIOCGUNIT): %m");
1571 * Enable debug in the driver if requested.
1574 if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &flags) < 0) {
1575 warn("ioctl (PPPIOCGFLAGS): %m");
1577 flags |= (kdebugflag & 0xFF) * SC_DEBUG;
1578 if (ioctl(ttyfd, PPPIOCSFLAGS, (caddr_t) &flags) < 0)
1579 warn("ioctl(PPPIOCSFLAGS): %m");
1589 * restore_loop - reattach the ppp unit to the loopback.
1597 * Transfer the ppp interface back to the loopback.
1599 if (ioctl(ttyfd, PPPIOCXFERUNIT, 0) < 0)
1600 fatal("ioctl(transfer ppp unit): %m");
1602 if (ioctl(loop_slave, TIOCSETD, &x) < 0)
1603 fatal("ioctl(TIOCSETD): %m");
1606 * Check that we got the same unit again.
1608 if (ioctl(loop_slave, PPPIOCGUNIT, &x) < 0)
1609 fatal("ioctl(PPPIOCGUNIT): %m");
1611 fatal("transfer_ppp failed: wanted unit %d, got %d",
1618 * Use the hostid as part of the random number seed.
1628 * sys_check_options - check the options that the user specified
1634 * We don't support demand dialing yet.
1638 option_error("PPP-2.3 for NeXTSTEP does not yet support demand dialing");
1646 * sys_close - Clean up in a child process before execing.
1652 if (loop_slave >= 0) {
1661 * wait_loop_output - wait until there is data available on the
1662 * loopback, for the length of time specified by *timo (indefinite
1665 void wait_loop_output(timo)
1666 struct timeval *timo;
1672 FD_SET(loop_master, &ready);
1673 n = select(loop_master + 1, &ready, NULL, &ready, timo);
1674 if (n < 0 && errno != EINTR)
1675 fatal("select: %m");
1679 * wait_time - wait for a given length of time or until a
1680 * signal is received.
1682 void wait_time(timo)
1683 struct timeval *timo;
1687 n = select(0, NULL, NULL, NULL, timo);
1688 if (n < 0 && errno != EINTR)
1689 fatal("select: %m");