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.
23 static char rcsid[] = "$Id: sys-NeXT.c,v 1.10 1998/11/07 06:59:30 paulus Exp $";
34 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <sys/ioctl.h>
39 #include <sys/errno.h>
41 #include <sys/fcntl.h>
44 #include <net/ppp_defs.h>
45 #include <net/if_ppp.h>
47 #include <netinet/in.h>
48 #include <netinet/in_systm.h>
49 #include <netinet/in_var.h>
50 #if !(NS_TARGET >= 40)
51 /* XXX get an error "duplicate member ip_v under 4.1 GAMMA */
52 #include <netinet/ip.h>
53 #endif /* NS_TARGET */
54 #include <netinet/if_ether.h>
55 #include <net/route.h>
56 #include <netinet/in.h>
58 #include <netinfo/ni.h>
62 static int initdisc = -1; /* Initial TTY discipline */
63 static int initfdflags = -1; /* Initial file descriptor flags for fd */
64 static int ppp_fd = -1; /* fd which is set to PPP discipline */
65 static int loop_slave = -1;
66 static int loop_master;
67 static char loop_name[20];
71 static int restore_term; /* 1 => we've munged the terminal */
72 static struct termios inittermios; /* Initial TTY termios */
74 static char *lock_file;
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 syslog(LOG_ERR, "Couldn't create IP socket: %m");
110 if((pppdev = open("/dev/ppp0", O_RDWR, O_NONBLOCK)) == NULL)
112 syslog(LOG_ERR, "Couldn't open /dev/ppp0: %m");
119 * sys_cleanup - restore any system state we modified before exiting:
120 * mark the interface down, delete default route and/or proxy arp entry.
121 * This should call die() because it's called from die().
129 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
130 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) >= 0
131 && ((ifr.ifr_flags & IFF_UP) != 0)) {
132 ifr.ifr_flags &= ~IFF_UP;
133 ioctl(sockfd, SIOCSIFFLAGS, &ifr);
137 if (default_route_gateway)
138 cifdefaultroute(0, 0, default_route_gateway);
140 cifproxyarp(0, proxy_arp_addr);
146 * note_debug_level - note a change in the debug level.
152 syslog(LOG_INFO, "Debug turned ON, Level %d", debug);
153 setlogmask(LOG_UPTO(LOG_DEBUG));
155 setlogmask(LOG_UPTO(LOG_WARNING));
160 * ppp_available - check whether the system has any ppp interfaces
161 * (in fact we check whether we can do an ioctl on ppp0).
168 extern char *no_ppp_msg;
170 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
171 return 1; /* can't tell - maybe we're not root */
173 strncpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
174 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
178 This system lacks kernel support for PPP. To include PPP support\n\
179 in the kernel, please follow the steps detailed in the README.NeXT\n\
180 file in the ppp-2.2 distribution.\n";
186 * establish_ppp - Turn the serial port into a ppp interface.
192 int pppdisc = PPPDISC;
195 if (ioctl(fd, TIOCGETD, &initdisc) < 0) {
196 syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
199 if (ioctl(fd, TIOCSETD, &pppdisc) < 0) {
200 syslog(LOG_ERR, "ioctl(establish TIOCSETD): %m");
205 * Find out which interface we were given.
207 if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {
208 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
213 * Enable debug in the driver if requested.
216 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
217 syslog(LOG_WARNING, "ioctl(PPPIOCGFLAGS): %m");
219 x |= (kdebugflag & 0xFF) * SC_DEBUG;
220 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
221 syslog(LOG_WARNING, "ioctl(PPPIOCSFLAGS): %m");
226 * Set device for non-blocking reads so PPPD can poll for
227 * input from the kernel.
229 if ((initfdflags = fcntl(fd, F_GETFL)) == -1
230 || fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
231 syslog(LOG_WARNING, "Couldn't set device to non-blocking mode: %m");
238 * disestablish_ppp - Restore the serial port to normal operation.
239 * This shouldn't call die() because it's called from die().
245 /* Reset non-blocking mode on fd. */
246 if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
247 syslog(LOG_WARNING, "Couldn't restore device fd flags: %m");
250 /* Restore old line discipline. */
251 if (initdisc >= 0 && ioctl(fd, TIOCSETD, &initdisc) < 0)
252 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
257 * Check whether the link seems not to be 8-bit clean.
265 if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
267 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
269 s = "bit 7 set to 1";
272 s = "bit 7 set to 0";
282 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
283 syslog(LOG_WARNING, "All received characters had %s", s);
289 * List of valid speeds.
292 int speed_int, speed_val;
368 #warning Defining B115200
379 * Translate from bits/second to a speed_t.
385 struct speed *speedp;
389 for (speedp = speeds; speedp->speed_int; speedp++)
390 if (bps == speedp->speed_int)
391 return speedp->speed_val;
392 syslog(LOG_WARNING, "speed %d not supported", bps);
397 * Translate from a speed_t to bits/second.
403 struct speed *speedp;
407 for (speedp = speeds; speedp->speed_int; speedp++)
408 if (speed == speedp->speed_val)
409 return speedp->speed_int;
415 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
416 * at the requested speed, etc. If `local' is true, set CLOCAL
417 * regardless of whether the modem option was specified.
420 set_up_tty(fd, local)
423 int speed, x, modembits;
426 if (tcgetattr(fd, &tios) < 0) {
427 syslog(LOG_ERR, "tcgetattr: %m");
434 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
436 tios.c_cflag |= CS8 | CREAD | HUPCL;
438 tios.c_cflag |= CLOCAL;
440 tios.c_iflag = IGNBRK | IGNPAR;
444 tios.c_cc[VTIME] = 0;
447 tios.c_iflag |= IXON | IXOFF;
448 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
449 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
452 speed = translate_speed(inspeed);
454 cfsetospeed(&tios, speed);
455 cfsetispeed(&tios, speed);
457 speed = cfgetospeed(&tios);
459 * We can't proceed if the serial port speed is B0,
460 * since that implies that the serial port is disabled.
463 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
470 modembits = TIOCM_RTS | TIOCM_CTS;
471 if (ioctl(fd, (crtscts ? TIOCMBIS : TIOCMBIC), &modembits) < 0)
472 syslog(LOG_ERR, "ioctl: TIOCMBIS/BIC: %m");
475 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
476 syslog(LOG_ERR, "tcsetattr: %m");
480 baud_rate = inspeed = baud_rate_of(speed);
485 * restore_tty - restore the terminal to the saved settings.
492 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
494 syslog(LOG_WARNING, "tcsetattr: %m");
500 * setdtr - control the DTR line on the serial port.
501 * This is called from die(), so it shouldn't call die().
503 * The write hack is to get NXFax to recognize that there is
504 * activity on the port. Not using the write nukes
505 * NXFax's capability to determine port usage.
512 int modembits = TIOCM_DTR;
520 /* ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits); */
521 ioctl(fd, (on? TIOCSDTR: TIOCCDTR), 0);
526 * output - Output PPP packet.
535 log_packet(p, len, "sent ", LOG_DEBUG);
537 if (write(ttyfd, p, len) < 0) {
538 if (errno == EWOULDBLOCK || errno == ENOBUFS
539 || errno == ENXIO || errno == EIO) {
540 syslog(LOG_WARNING, "write: warning: %m");
542 syslog(LOG_ERR, "write: %m");
550 * wait_input - wait until there is data available on ttyfd,
551 * for the length of time specified by *timo (indefinite
556 struct timeval *timo;
562 FD_SET(ttyfd, &ready);
563 n = select(ttyfd+1, &ready, NULL, &ready, timo);
564 if (n < 0 && errno != EINTR) {
565 syslog(LOG_ERR, "select: %m");
572 * read_packet - get a PPP packet from the serial device.
580 if ((len = read(ttyfd, buf, PPP_MTU + PPP_HDRLEN)) < 0) {
581 if (errno == EWOULDBLOCK || errno == EINTR) {
582 MAINDEBUG((LOG_DEBUG, "read: %m"));
585 syslog(LOG_ERR, "read: %m");
593 * ppp_send_config - configure the transmit characteristics of
597 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
605 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
607 if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
608 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
612 if (ioctl(ttyfd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
613 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
617 if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
618 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
621 x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
622 x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
623 if (ioctl(ttyfd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
624 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
631 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
634 ppp_set_xaccm(unit, accm)
638 if (ioctl(ttyfd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
639 syslog(LOG_WARNING, "ioctl(PPPIOCSXASYNCMAP): %m");
644 * ppp_recv_config - configure the receive-side characteristics of
648 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
655 if (ioctl(ttyfd, PPPIOCSMRU, (caddr_t) &mru) < 0) {
656 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
659 if (ioctl(ttyfd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
660 syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m");
663 if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
664 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
667 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
668 if (ioctl(ttyfd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
669 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
675 * ccp_test - ask kernel whether a given compression method
676 * is acceptable for use.
679 ccp_test(unit, opt_ptr, opt_len, for_transmit)
680 int unit, opt_len, for_transmit;
683 struct ppp_option_data data;
686 data.length = opt_len;
687 data.transmit = for_transmit;
688 if (ioctl(ttyfd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
690 return (errno == ENOBUFS)? 0: -1;
694 * ccp_flags_set - inform kernel about the current state of CCP.
697 ccp_flags_set(unit, isopen, isup)
698 int unit, isopen, isup;
702 if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
703 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
706 x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
707 x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
708 if (ioctl(ttyfd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
709 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
713 * ccp_fatal_error - returns 1 if decompression was disabled as a
714 * result of an error detected after decompression of a packet,
715 * 0 otherwise. This is necessary because of patent nonsense.
718 ccp_fatal_error(unit)
723 if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
724 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
727 return x & SC_DC_FERROR;
731 * sifvjcomp - config tcp header compression
734 sifvjcomp(u, vjcomp, cidcomp, maxcid)
735 int u, vjcomp, cidcomp, maxcid;
739 if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
740 syslog(LOG_ERR, "ioctl(PPIOCGFLAGS): %m");
743 x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
744 x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
745 if (ioctl(ttyfd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
746 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
749 if (ioctl(ttyfd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
750 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
757 * sifup - Config the interface up and enable IP packets to pass.
760 #define SC_ENABLE_IP 0x100 /* compat for old versions of kernel code */
771 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
772 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
773 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
776 ifr.ifr_flags |= IFF_UP;
777 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
778 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
782 npi.protocol = PPP_IP;
783 npi.mode = NPMODE_PASS;
784 if (ioctl(ttyfd, PPPIOCSNPMODE, &npi) < 0) {
785 if (errno != ENOTTY) {
786 syslog(LOG_ERR, "ioctl(PPPIOCSNPMODE): %m");
789 /* for backwards compatibility */
790 if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
791 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
795 if (ioctl(ttyfd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
796 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
804 * sifdown - Config the interface down and disable IP.
816 npi.protocol = PPP_IP;
817 npi.mode = NPMODE_ERROR;
818 ioctl(ttyfd, PPPIOCSNPMODE, (caddr_t) &npi);
819 /* ignore errors, because ttyfd might have been closed by now. */
822 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
823 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
824 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
827 ifr.ifr_flags &= ~IFF_UP;
828 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
829 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
838 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
841 #define SET_SA_FAMILY(addr, family) \
842 BZERO((char *) &(addr), sizeof(addr)); \
843 addr.sa_family = (family);
846 * sifaddr - Config the interface IP addresses and netmask.
857 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
858 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
859 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
860 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
861 syslog(LOG_ERR, "ioctl(SIOCAIFADDR): %m");
864 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
865 if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
866 syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
870 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
871 syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
872 if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
873 syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
881 * cifaddr - Clear the interface IP addresses, and delete routes
882 * through the interface if possible.
884 * N.B.: under NextStep, you can't *delete* an address on an interface,
885 * so we change it to 0.0.0.0... A real hack. But it simplifies
886 * reconnection on the server side.
897 (void) sifaddr(u, o, h, 0L);
899 SET_SA_FAMILY(rt.rt_dst, AF_INET);
900 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
901 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
902 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
903 rt.rt_flags = RTF_HOST;
904 if (ioctl(sockfd, SIOCDELRT, (caddr_t) &rt) < 0) {
905 syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
912 * sifdefaultroute - assign a default route through the address given.
915 sifdefaultroute(u, l, g)
919 return dodefaultroute(g, 's');
923 * cifdefaultroute - delete a default route through the address given.
926 cifdefaultroute(u, l, g)
930 return dodefaultroute(g, 'c');
934 * dodefaultroute - talk to a routing socket to add/delete a default route.
937 dodefaultroute(g, cmd)
943 SET_SA_FAMILY(rt.rt_dst, AF_INET);
944 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = 0L;
945 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
946 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
947 rt.rt_flags = RTF_GATEWAY;
948 if (ioctl(sockfd, (cmd == 's') ? SIOCADDRT : SIOCDELRT, &rt) < 0) {
949 syslog(LOG_ERR, "%cifdefaultroute: ioctl(%s): %m", cmd,
950 (cmd == 's') ? "SIOCADDRT" : "SIOCDELRT");
953 default_route_gateway = (cmd == 's')? g: 0;
958 * sifproxyarp - Make a proxy ARP entry for the peer.
961 sifproxyarp(unit, hisaddr)
965 struct arpreq arpreq;
967 BZERO(&arpreq, sizeof(arpreq));
970 * Get the hardware address of an interface on the same subnet
971 * as our local address.
973 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
974 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
978 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
979 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
980 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
981 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
982 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
986 proxy_arp_addr = hisaddr;
991 * cifproxyarp - Delete the proxy ARP entry for the peer.
994 cifproxyarp(unit, hisaddr)
998 struct arpreq arpreq;
1000 BZERO(&arpreq, sizeof(arpreq));
1001 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1002 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1003 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1004 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m");
1012 * get_ether_addr - get the hardware address of an interface on the
1013 * the same subnet as ipaddr.
1018 get_ether_addr(ipaddr, hwaddr)
1020 struct sockaddr *hwaddr;
1022 struct ifreq *ifr, *ifend, *ifp;
1023 u_int32_t ina, mask;
1024 struct ether_addr dla;
1027 struct ifreq ifs[MAX_IFS];
1028 struct hostent *hostent;
1030 ifc.ifc_len = sizeof(ifs);
1032 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1033 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
1038 * Scan through looking for an interface with an Internet
1039 * address on the same subnet as `ipaddr'.
1041 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1042 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1043 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1044 if (ifr->ifr_addr.sa_family == AF_INET) {
1045 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1046 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1048 * Check that the interface is up, and not point-to-point
1051 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1053 if ((ifreq.ifr_flags &
1054 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1055 != (IFF_UP|IFF_BROADCAST))
1058 * Get its netmask and check that it's on the right subnet.
1060 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1062 mask = ((struct sockaddr_in*)&ifreq.ifr_addr)->sin_addr.s_addr;
1063 if ((ipaddr & mask) != (ina & mask))
1072 syslog(LOG_INFO, "found interface %s for proxy arp", ifr->ifr_name);
1075 * Get the hostname and look for an entry using the ethers database.
1076 * Under NeXTStep this is the best we can do for now.
1078 if ((hostent = gethostbyaddr((char*)&ina, sizeof(ina), AF_INET)) == NULL)
1081 if (ether_by_host(hostent->h_name, &dla)) {
1082 syslog(LOG_INFO, "Add entry for %s in /etc/ethers", hostent->h_name);
1083 return 0; /* it's not there */
1085 hwaddr->sa_family = AF_UNSPEC;
1086 BCOPY(&dla, hwaddr->sa_data, sizeof(dla));
1091 ether_by_host(hostname, etherptr)
1093 struct ether_addr *etherptr;
1095 struct ether_addr *thisptr;
1101 if (!ether_hostton(hostname, etherptr))
1104 * We shall now try and
1105 * find the address in the
1106 * top domain of netinfo.
1108 strcat(strcpy(path, "/machines/"), hostname);
1110 if (ni_open((void *)0, "/", &conn)
1111 || ni_root(conn, &root)
1112 || ni_pathsearch(conn, &root, path)
1113 || ni_lookupprop(conn, &root, "en_address", &val))
1117 * Now we can convert the returned string into an ethernet address.
1119 strcpy(path, val.ni_namelist_val[0]);
1121 if ((thisptr = (struct ether_addr*)ether_aton(path)) == NULL)
1123 BCOPY(thisptr, etherptr, sizeof(struct ether_addr));
1130 * Return user specified netmask, modified by any mask we might determine
1131 * for address `addr' (in network byte order).
1132 * Here we scan through the system's list of interfaces, looking for
1133 * any non-point-to-point interfaces which might appear to be on the same
1134 * network as `addr'. If we find any, we OR in their netmask to the
1135 * user-specified netmask.
1141 u_int32_t mask, nmask, ina;
1142 struct ifreq *ifr, *ifend, ifreq;
1144 struct ifreq ifs[MAX_IFS];
1147 if (IN_CLASSA(addr)) /* determine network mask for address class */
1148 nmask = IN_CLASSA_NET;
1149 else if (IN_CLASSB(addr))
1150 nmask = IN_CLASSB_NET;
1152 nmask = IN_CLASSC_NET;
1153 /* class D nets are disallowed by bad_ip_adrs */
1154 mask = netmask | htonl(nmask);
1157 * Scan through the system's network interfaces.
1159 ifc.ifc_len = sizeof(ifs);
1161 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1162 syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m");
1165 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1166 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1167 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1169 * Check the interface's internet address.
1171 if (ifr->ifr_addr.sa_family != AF_INET)
1173 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1174 if ((ntohl(ina) & nmask) != (addr & nmask))
1177 * Check that the interface is up, and not point-to-point or loopback.
1179 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1180 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1182 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1186 * Get its netmask and OR it into our mask.
1188 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1190 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1199 * daemon - Detach us from the terminal session.
1202 daemon(nochdir, noclose)
1203 int nochdir, noclose;
1207 if ((pid = fork()) < 0)
1210 exit(0); /* parent dies */
1215 fclose(stdin); /* don't need stdin, stdout, stderr */
1227 char *d = malloc(strlen(s) + 1);
1229 if (d) strcpy(d, s);
1234 * This logwtmp() implementation is subject to the following copyright:
1236 * Copyright (c) 1988 The Regents of the University of California.
1237 * All rights reserved.
1239 * Redistribution and use in source and binary forms are permitted
1240 * provided that the above copyright notice and this paragraph are
1241 * duplicated in all such forms and that any documentation,
1242 * advertising materials, and other materials related to such
1243 * distribution and use acknowledge that the software was developed
1244 * by the University of California, Berkeley. The name of the
1245 * University may not be used to endorse or promote products derived
1246 * from this software without specific prior written permission.
1247 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1248 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1249 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1252 #define WTMPFILE "/usr/adm/wtmp"
1255 logwtmp(line, name, host)
1256 const char *line, *name, *host;
1262 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1264 if (!fstat(fd, &buf)) {
1265 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1266 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1267 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1268 (void)time(&ut.ut_time);
1269 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1270 (void)ftruncate(fd, buf.st_size);
1276 * Routines for locking and unlocking the serial device, moved here
1280 #define LOCK_PREFIX "/usr/spool/uucp/LCK/LCK.."
1283 * lock - create a lock file for the named device
1292 if ((p = strrchr(dev, '/')) != NULL)
1294 lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1295 if (lock_file == NULL)
1296 novm("lock file name");
1297 strcat(strcpy(lock_file, LOCK_PREFIX), dev);
1299 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1301 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1302 /* Read the lock file to find out who has the device locked */
1303 n = read(fd, &pid, sizeof(pid));
1305 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1308 if (kill(pid, 0) == -1 && errno == ESRCH) {
1309 /* pid no longer exists - remove the lock file */
1310 if (unlink(lock_file) == 0) {
1312 syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1316 syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
1319 syslog(LOG_NOTICE, "Device %s is locked by pid %d",
1324 syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1331 write(fd, &pid, sizeof pid);
1338 * unlock - remove our lockfile
1350 #if defined(i386) && defined(HAS_BROKEN_IOCTL)
1361 u_char let, code, size;
1363 size = (cmd >> 16) & IOCPARM_MASK;
1367 if (let == 't' && (75 <= code && code <= 90))
1368 syslog(LOG_INFO, "ioctl(%d, 0x%x ('%c', %d, %d), 0x%x)\n", fd, cmd,
1369 let, code, size, c);
1372 ret = ioctl(fd, cmd, c);
1377 syslog(LOG_INFO, "ioctl('%c', %d, %d) errno = %d (%m)\n",
1378 let, code, size, errno);
1379 if (let == 't' && (75 <= code && code <= 90) && (cmd & IOC_OUT)) {
1380 int i, len = ((cmd >> 16) & IOCPARM_MASK);
1381 for (i = 0; i < len / 4; ++i)
1382 syslog(LOG_INFO, "word[%d] @ 0x%06x = 0x%x\n",
1383 i, &((int *) c)[i],((int *)c)[i]);
1388 if (ret == -1 && errno == EPERM)
1392 #endif /* HAS_BROKEN_IOCTL */
1395 #if defined(FIXSIGS) && (defined (hppa) || defined(sparc))
1398 * These redefinitions of Posix functions are necessary
1399 * because HPPA systems have an OS bug that causes
1400 * sigaction to core dump:
1402 * AlainF 9-Nov-1994 HACK FOR HP-PA/NEXTSTEP
1403 * sigaction(3) seems broken in the HP-PA NeXTSTEP 3.2
1404 * Posix lib. This causes pppd to SIGBUS at the expiration
1405 * of the first timeout (_sigtramp seems to invoke
1406 * the SIGALRM handler at an unreasonably low address).
1407 * All calls so sigaction(3) have been changed to calls
1408 * to sigvec(2) and sigprocmask(SIG_BLOCK,...) to
1410 * This is kind of a hack, especially since there are
1411 * other routines of the Posix lib still used, but
1414 * Dave Hess <David-Hess@net.tamu.edu> noted that 3.3 Sparc seems to
1415 * have the same bug. Thus this fix has been enabled for SPARC also.
1420 int sigemptyset(sigset_t *mask)
1425 sigaddset(sigset_t *mask, int which_sig)
1427 *mask |= sigmask(which_sig);
1431 int sigaction(int sig, const struct sigaction *act, struct sigaction *oact)
1436 sv.sv_handler = act->sa_handler;
1437 sv.sv_mask = act->sa_mask;
1443 syslog(LOG_WARNING, "PPPD: Inside modified HP and SPARC sigaction\n");
1446 return sigvec(sig, &sv, NULL);
1453 * Code following is added for 2.3 compatibility
1457 * get_idle_time - return how long the link has been idle.
1460 get_idle_time(u, ip)
1462 struct ppp_idle *ip;
1464 return (ioctl(ttyfd, PPPIOCGIDLE, ip) >= 0);
1469 * get_loop_output - read characters from the loopback, form them
1470 * into frames, and detect when we want to bring the real link up.
1471 * Return value is 1 if we need to bring up the link, 0 otherwise.
1481 while ((n = read(loop_master, inbuf, sizeof(inbuf))) >= 0) {
1482 if (loop_chars(inbuf, n))
1487 syslog(LOG_ERR, "eof on loopback");
1489 } else if (errno != EWOULDBLOCK){
1490 syslog(LOG_ERR, "read from loopback: %m");
1501 * sifnpmode - Set the mode for handling packets for a given NP.
1504 sifnpmode(u, proto, mode)
1511 npi.protocol = proto;
1513 if (ioctl(ttyfd, PPPIOCSNPMODE, &npi) < 0) {
1514 syslog(LOG_ERR, "ioctl(set NP %d mode to %d): %m", proto, mode);
1522 * open_ppp_loopback - open the device we use for getting
1523 * packets in demand mode, and connect it to a ppp interface.
1524 * Here we use a pty.
1532 struct termios tios;
1533 int pppdisc = PPPDISC;
1535 syslog(LOG_ERR, "open_ppp_loopback called!");
1538 if (openpty(&loop_master, &loop_slave, loop_name, NULL, NULL) < 0) {
1539 syslog(LOG_ERR, "No free pty for loopback");
1542 SYSDEBUG((LOG_DEBUG, "using %s for loopback", loop_name));
1544 if (tcgetattr(loop_slave, &tios) == 0) {
1545 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB);
1546 tios.c_cflag |= CS8 | CREAD;
1547 tios.c_iflag = IGNPAR;
1550 if (tcsetattr(loop_slave, TCSAFLUSH, &tios) < 0)
1551 syslog(LOG_WARNING, "couldn't set attributes on loopback: %m");
1554 if ((flags = fcntl(loop_master, F_GETFL)) != -1)
1555 if (fcntl(loop_master, F_SETFL, flags | O_NONBLOCK) == -1)
1556 syslog(LOG_WARNING, "couldn't set loopback to nonblock: %m");
1559 if (ioctl(ttyfd, TIOCSETD, &pppdisc) < 0) {
1560 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
1565 * Find out which interface we were given.
1567 if (ioctl(ttyfd, PPPIOCGUNIT, &ifunit) < 0) {
1568 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
1573 * Enable debug in the driver if requested.
1576 if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &flags) < 0) {
1577 syslog(LOG_WARNING, "ioctl (PPPIOCGFLAGS): %m");
1579 flags |= (kdebugflag & 0xFF) * SC_DEBUG;
1580 if (ioctl(ttyfd, PPPIOCSFLAGS, (caddr_t) &flags) < 0)
1581 syslog(LOG_WARNING, "ioctl(PPPIOCSFLAGS): %m");
1590 * restore_loop - reattach the ppp unit to the loopback.
1598 * Transfer the ppp interface back to the loopback.
1600 if (ioctl(ttyfd, PPPIOCXFERUNIT, 0) < 0) {
1601 syslog(LOG_ERR, "ioctl(transfer ppp unit): %m");
1605 if (ioctl(loop_slave, TIOCSETD, &x) < 0) {
1606 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
1611 * Check that we got the same unit again.
1613 if (ioctl(loop_slave, PPPIOCGUNIT, &x) < 0) {
1614 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
1618 syslog(LOG_ERR, "transfer_ppp failed: wanted unit %d, got %d",
1627 * Use the hostid as part of the random number seed.
1637 * sys_check_options - check the options that the user specified
1643 * We don't support demand dialing yet.
1647 syslog(LOG_WARNING, "PPP-2.3 for NeXTSTEP does not yet support demand dialing\n");
1655 * sys_close - Clean up in a child process before execing.
1661 if (loop_slave >= 0) {
1670 * wait_loop_output - wait until there is data available on the
1671 * loopback, for the length of time specified by *timo (indefinite
1674 void wait_loop_output(timo)
1675 struct timeval *timo;
1681 FD_SET(loop_master, &ready);
1682 n = select(loop_master + 1, &ready, NULL, &ready, timo);
1683 if (n < 0 && errno != EINTR) {
1684 syslog(LOG_ERR, "select: %m");
1691 * wait_time - wait for a given length of time or until a
1692 * signal is received.
1694 void wait_time(timo)
1695 struct timeval *timo;
1699 n = select(0, NULL, NULL, NULL, timo);
1700 if (n < 0 && errno != EINTR) {
1701 syslog(LOG_ERR, "select: %m");