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.1 1995/08/10 06:48:34 paulus Exp $";
33 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <sys/ioctl.h>
38 #include <sys/errno.h>
40 #include <sys/fcntl.h>
43 #include <net/ppp_defs.h>
44 #include <net/if_ppp.h>
46 #include <netinet/in.h>
47 #include <netinet/in_systm.h>
48 #include <netinet/in_var.h>
49 #include <netinet/ip.h>
50 #include <netinet/if_ether.h>
51 #include <net/route.h>
52 #include <netinet/in.h>
54 #include <netinfo/ni.h>
58 static int initdisc = -1; /* Initial TTY discipline */
62 static int restore_term; /* 1 => we've munged the terminal */
63 static struct termios inittermios; /* Initial TTY termios */
65 static char *lock_file;
67 int sockfd; /* socket for doing interface ioctls */
69 #if defined(i386) && defined(HAS_BROKEN_IOCTL)
74 * sys_init - System-dependent initialization.
79 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
80 setlogmask(LOG_UPTO(LOG_INFO));
82 setlogmask(LOG_UPTO(LOG_DEBUG));
84 /* Get an internet socket for doing socket ioctl's on. */
85 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
86 syslog(LOG_ERR, "Couldn't create IP socket: %m");
92 * note_debug_level - note a change in the debug level.
98 syslog(LOG_INFO, "Debug turned ON, Level %d", debug);
99 setlogmask(LOG_UPTO(LOG_DEBUG));
101 setlogmask(LOG_UPTO(LOG_WARNING));
106 * ppp_available - check whether the system has any ppp interfaces
107 * (in fact we check whether we can do an ioctl on ppp0).
115 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
116 return 1; /* can't tell - maybe we're not root */
118 strncpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
119 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
126 * establish_ppp - Turn the serial port into a ppp interface.
131 int pppdisc = PPPDISC;
134 if (ioctl(fd, TIOCGETD, &initdisc) < 0) {
135 syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
138 if (ioctl(fd, TIOCSETD, &pppdisc) < 0) {
139 syslog(LOG_ERR, "ioctl(establish TIOCSETD): %m");
144 * Find out which interface we were given.
146 if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {
147 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
152 * Enable debug in the driver if requested.
155 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
156 syslog(LOG_WARNING, "ioctl(PPPIOCGFLAGS): %m");
158 x |= (kdebugflag & 0xFF) * SC_DEBUG;
159 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
160 syslog(LOG_WARNING, "ioctl(PPPIOCSFLAGS): %m");
167 * disestablish_ppp - Restore the serial port to normal operation.
168 * This shouldn't call die() because it's called from die().
178 * Check whether the link seems not to be 8-bit clean.
180 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
182 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
184 s = "bit 7 set to 1";
187 s = "bit 7 set to 0";
197 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
198 syslog(LOG_WARNING, "All received characters had %s", s);
201 if (ioctl(fd, TIOCSETD, &initdisc) < 0)
202 syslog(LOG_ERR, "ioctl(disestablish TIOCSETD): %m");
207 * List of valid speeds.
210 int speed_int, speed_val;
291 * Translate from bits/second to a speed_t.
297 struct speed *speedp;
301 for (speedp = speeds; speedp->speed_int; speedp++)
302 if (bps == speedp->speed_int)
303 return speedp->speed_val;
304 syslog(LOG_WARNING, "speed %d not supported", bps);
309 * Translate from a speed_t to bits/second.
315 struct speed *speedp;
319 for (speedp = speeds; speedp->speed_int; speedp++)
320 if (speed == speedp->speed_val)
321 return speedp->speed_int;
327 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
328 * at the requested speed, etc. If `local' is true, set CLOCAL
329 * regardless of whether the modem option was specified.
331 set_up_tty(fd, local)
334 int speed, x, modembits;
337 if (tcgetattr(fd, &tios) < 0) {
338 syslog(LOG_ERR, "tcgetattr: %m");
345 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
347 tios.c_cflag |= CS8 | CREAD | HUPCL;
349 tios.c_cflag |= CLOCAL;
350 tios.c_iflag = IGNBRK | IGNPAR;
354 tios.c_cc[VTIME] = 0;
357 tios.c_iflag |= IXOFF;
358 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
359 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
362 speed = translate_speed(inspeed);
364 cfsetospeed(&tios, speed);
365 cfsetispeed(&tios, speed);
367 speed = cfgetospeed(&tios);
371 modembits = TIOCM_RTS | TIOCM_CTS;
372 if (ioctl(fd, (crtscts ? TIOCMBIS : TIOCMBIC), &modembits) < 0)
373 syslog(LOG_ERR, "ioctl: TIOCMBIS/BIC: %m");
376 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
377 syslog(LOG_ERR, "tcsetattr: %m");
381 baud_rate = inspeed = baud_rate_of(speed);
386 * restore_tty - restore the terminal to the saved settings.
392 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
394 syslog(LOG_WARNING, "tcsetattr: %m");
400 * setdtr - control the DTR line on the serial port.
401 * This is called from die(), so it shouldn't call die().
403 * The write hack is to get NXFax to recognize that there is
404 * activity on the port. Not using the write nukes
405 * NXFax's capability to determine port usage.
411 int modembits = TIOCM_DTR;
419 /* ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits); */
420 ioctl(fd, (on? TIOCSDTR: TIOCCDTR), 0);
425 * output - Output PPP packet.
434 MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
436 log_packet(p, len, "sent ");
438 if (write(fd, p, len) < 0) {
439 syslog(LOG_ERR, "write: %m");
446 * wait_input - wait until there is data available on fd,
447 * for the length of time specified by *timo (indefinite
451 struct timeval *timo;
458 n = select(fd+1, &ready, NULL, &ready, timo);
459 if (n < 0 && errno != EINTR) {
460 syslog(LOG_ERR, "select: %m");
467 * read_packet - get a PPP packet from the serial device.
475 if ((len = read(fd, buf, PPP_MTU + PPP_HDRLEN)) < 0) {
476 if (errno == EWOULDBLOCK || errno == EINTR) {
477 MAINDEBUG((LOG_DEBUG, "read(fd): %m"));
480 syslog(LOG_ERR, "read(fd): %m");
488 * ppp_send_config - configure the transmit characteristics of
492 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
500 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
502 if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
503 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
507 if (ioctl(fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
508 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
512 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
513 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
516 x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
517 x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
518 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
519 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
526 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
529 ppp_set_xaccm(unit, accm)
533 if (ioctl(fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
534 syslog(LOG_WARNING, "ioctl(PPPIOCSXASYNCMAP): %m");
539 * ppp_recv_config - configure the receive-side characteristics of
543 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
550 if (ioctl(fd, PPPIOCSMRU, (caddr_t) &mru) < 0) {
551 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
554 if (ioctl(fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
555 syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m");
558 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
559 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
562 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
563 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
564 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
570 * ccp_test - ask kernel whether a given compression method
571 * is acceptable for use.
573 ccp_test(unit, opt_ptr, opt_len, for_transmit)
574 int unit, opt_len, for_transmit;
577 struct ppp_option_data data;
580 data.length = opt_len;
581 data.transmit = for_transmit;
582 return ioctl(fd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0;
586 * ccp_flags_set - inform kernel about the current state of CCP.
589 ccp_flags_set(unit, isopen, isup)
590 int unit, isopen, isup;
594 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
595 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
598 x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
599 x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
600 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
601 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
605 * ccp_fatal_error - returns 1 if decompression was disabled as a
606 * result of an error detected after decompression of a packet,
607 * 0 otherwise. This is necessary because of patent nonsense.
610 ccp_fatal_error(unit)
615 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
616 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
619 return x & SC_DC_FERROR;
623 * sifvjcomp - config tcp header compression
626 sifvjcomp(u, vjcomp, cidcomp, maxcid)
627 int u, vjcomp, cidcomp, maxcid;
631 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
632 syslog(LOG_ERR, "ioctl(PPIOCGFLAGS): %m");
635 x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
636 x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
637 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
638 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
641 if (ioctl(fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
642 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
649 * sifup - Config the interface up and enable IP packets to pass.
652 #define SC_ENABLE_IP 0x100 /* compat for old versions of kernel code */
663 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
664 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
665 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
668 ifr.ifr_flags |= IFF_UP;
669 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
670 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
673 npi.protocol = PPP_IP;
674 npi.mode = NPMODE_PASS;
675 if (ioctl(fd, PPPIOCSNPMODE, &npi) < 0) {
676 if (errno != ENOTTY) {
677 syslog(LOG_ERR, "ioctl(PPPIOCSNPMODE): %m");
680 /* for backwards compatibility */
681 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
682 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
686 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
687 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
695 * sifdown - Config the interface down and disable IP.
707 npi.protocol = PPP_IP;
708 npi.mode = NPMODE_ERROR;
709 if (ioctl(fd, PPPIOCSNPMODE, (caddr_t) &npi) < 0) {
710 if (errno != ENOTTY) {
711 syslog(LOG_ERR, "ioctl(PPPIOCSNPMODE): %m");
714 /* backwards compatibility */
715 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
716 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
720 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
721 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
728 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
729 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
730 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
733 ifr.ifr_flags &= ~IFF_UP;
734 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
735 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
743 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
746 #define SET_SA_FAMILY(addr, family) \
747 BZERO((char *) &(addr), sizeof(addr)); \
748 addr.sa_family = (family);
751 * sifaddr - Config the interface IP addresses and netmask.
762 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
763 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
764 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
765 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
766 syslog(LOG_ERR, "ioctl(SIOCAIFADDR): %m");
769 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
770 if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
771 syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
775 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
776 syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
777 if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
778 syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
786 * cifaddr - Clear the interface IP addresses, and delete routes
787 * through the interface if possible.
789 * N.B.: under NextStep, you can't *delete* an address on an interface,
790 * so we change it to 0.0.0.0... A real hack. But it simplifies
791 * reconnection on the server side.
802 (void) sifaddr(u, o, h, 0L);
804 SET_SA_FAMILY(rt.rt_dst, AF_INET);
805 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
806 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
807 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
808 rt.rt_flags = RTF_HOST;
809 if (ioctl(sockfd, SIOCDELRT, (caddr_t) &rt) < 0) {
810 syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
817 * sifdefaultroute - assign a default route through the address given.
820 sifdefaultroute(u, g)
824 return dodefaultroute(g, 's');
828 * cifdefaultroute - delete a default route through the address given.
831 cifdefaultroute(u, g)
835 return dodefaultroute(g, 'c');
839 * dodefaultroute - talk to a routing socket to add/delete a default route.
842 dodefaultroute(g, cmd)
848 SET_SA_FAMILY(rt.rt_dst, AF_INET);
849 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = 0L;
850 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
851 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
852 rt.rt_flags = RTF_GATEWAY;
853 if (ioctl(sockfd, (cmd == 's') ? SIOCADDRT : SIOCDELRT, &rt) < 0) {
854 syslog(LOG_ERR, "%cifdefaultroute: ioctl(%s): %m", cmd,
855 (cmd == 's') ? "SIOCADDRT" : "SIOCDELRT");
862 * sifproxyarp - Make a proxy ARP entry for the peer.
865 sifproxyarp(unit, hisaddr)
869 struct arpreq arpreq;
871 BZERO(&arpreq, sizeof(arpreq));
874 * Get the hardware address of an interface on the same subnet
875 * as our local address.
877 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
878 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
882 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
883 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
884 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
885 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
886 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
894 * cifproxyarp - Delete the proxy ARP entry for the peer.
897 cifproxyarp(unit, hisaddr)
901 struct arpreq arpreq;
903 BZERO(&arpreq, sizeof(arpreq));
904 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
905 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
906 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
907 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m");
914 * get_ether_addr - get the hardware address of an interface on the
915 * the same subnet as ipaddr.
920 get_ether_addr(ipaddr, hwaddr)
922 struct sockaddr *hwaddr;
924 struct ifreq *ifr, *ifend, *ifp;
926 struct ether_addr dla;
929 struct ifreq ifs[MAX_IFS];
930 struct hostent *hostent;
932 ifc.ifc_len = sizeof(ifs);
934 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
935 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
940 * Scan through looking for an interface with an Internet
941 * address on the same subnet as `ipaddr'.
943 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
944 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
945 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
946 if (ifr->ifr_addr.sa_family == AF_INET) {
947 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
948 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
950 * Check that the interface is up, and not point-to-point
953 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
955 if ((ifreq.ifr_flags &
956 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
957 != (IFF_UP|IFF_BROADCAST))
960 * Get its netmask and check that it's on the right subnet.
962 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
964 mask = ((struct sockaddr_in*)&ifreq.ifr_addr)->sin_addr.s_addr;
965 if ((ipaddr & mask) != (ina & mask))
974 syslog(LOG_INFO, "found interface %s for proxy arp", ifr->ifr_name);
977 * Get the hostname and look for an entry using the ethers database.
978 * Under NeXTStep this is the best we can do for now.
980 if ((hostent = gethostbyaddr((char*)&ina, sizeof(ina), AF_INET)) == NULL)
983 if (ether_by_host(hostent->h_name, &dla)) {
984 syslog(LOG_INFO, "Add entry for %s in /etc/ethers", hostent->h_name);
985 return 0; /* it's not there */
987 hwaddr->sa_family = AF_UNSPEC;
988 BCOPY(&dla, hwaddr->sa_data, sizeof(dla));
993 ether_by_host(hostname, etherptr)
995 struct ether_addr *etherptr;
997 struct ether_addr *thisptr;
1003 if (!ether_hostton(hostname, etherptr))
1006 * We shall now try and
1007 * find the address in the
1008 * top domain of netinfo.
1010 strcat(strcpy(path, "/machines/"), hostname);
1012 if (ni_open((void *)0, "/", &conn)
1013 || ni_root(conn, &root)
1014 || ni_pathsearch(conn, &root, path)
1015 || ni_lookupprop(conn, &root, "en_address", &val))
1019 * Now we can convert the returned string into an ethernet address.
1021 strcpy(path, val.ni_namelist_val[0]);
1023 if ((thisptr = (struct ether_addr*)ether_aton(path)) == NULL)
1025 BCOPY(thisptr, etherptr, sizeof(struct ether_addr));
1032 * Return user specified netmask, modified by any mask we might determine
1033 * for address `addr' (in network byte order).
1034 * Here we scan through the system's list of interfaces, looking for
1035 * any non-point-to-point interfaces which might appear to be on the same
1036 * network as `addr'. If we find any, we OR in their netmask to the
1037 * user-specified netmask.
1043 u_int32_t mask, nmask, ina;
1044 struct ifreq *ifr, *ifend, ifreq;
1046 struct ifreq ifs[MAX_IFS];
1049 if (IN_CLASSA(addr)) /* determine network mask for address class */
1050 nmask = IN_CLASSA_NET;
1051 else if (IN_CLASSB(addr))
1052 nmask = IN_CLASSB_NET;
1054 nmask = IN_CLASSC_NET;
1055 /* class D nets are disallowed by bad_ip_adrs */
1056 mask = netmask | htonl(nmask);
1059 * Scan through the system's network interfaces.
1061 ifc.ifc_len = sizeof(ifs);
1063 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1064 syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m");
1067 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1068 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1069 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1071 * Check the interface's internet address.
1073 if (ifr->ifr_addr.sa_family != AF_INET)
1075 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1076 if ((ntohl(ina) & nmask) != (addr & nmask))
1079 * Check that the interface is up, and not point-to-point or loopback.
1081 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1082 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1084 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1088 * Get its netmask and OR it into our mask.
1090 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1092 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1101 * daemon - Detach us from the terminal session.
1104 daemon(nochdir, noclose)
1105 int nochdir, noclose;
1109 if ((pid = fork()) < 0)
1112 exit(0); /* parent dies */
1117 fclose(stdin); /* don't need stdin, stdout, stderr */
1129 char *d = malloc(strlen(s) + 1);
1131 if (d) strcpy(d, s);
1136 * This logwtmp() implementation is subject to the following copyright:
1138 * Copyright (c) 1988 The Regents of the University of California.
1139 * All rights reserved.
1141 * Redistribution and use in source and binary forms are permitted
1142 * provided that the above copyright notice and this paragraph are
1143 * duplicated in all such forms and that any documentation,
1144 * advertising materials, and other materials related to such
1145 * distribution and use acknowledge that the software was developed
1146 * by the University of California, Berkeley. The name of the
1147 * University may not be used to endorse or promote products derived
1148 * from this software without specific prior written permission.
1149 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1150 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1151 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1154 #define WTMPFILE "/usr/adm/wtmp"
1157 logwtmp(line, name, host)
1158 char *line, *name, *host;
1164 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1166 if (!fstat(fd, &buf)) {
1167 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1168 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1169 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1170 (void)time(&ut.ut_time);
1171 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1172 (void)ftruncate(fd, buf.st_size);
1178 * Routines for locking and unlocking the serial device, moved here
1182 #define LOCK_PREFIX "/usr/spool/uucp/LCK/LCK.."
1185 * lock - create a lock file for the named device
1194 if ((p = strrchr(dev, '/')) != NULL)
1196 lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1197 if (lock_file == NULL)
1198 novm("lock file name");
1199 strcat(strcpy(lock_file, LOCK_PREFIX), dev);
1201 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1203 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1204 /* Read the lock file to find out who has the device locked */
1205 n = read(fd, &pid, sizeof(pid));
1207 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1210 if (kill(pid, 0) == -1 && errno == ESRCH) {
1211 /* pid no longer exists - remove the lock file */
1212 if (unlink(lock_file) == 0) {
1214 syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1218 syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
1221 syslog(LOG_NOTICE, "Device %s is locked by pid %d",
1226 syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1233 write(fd, &pid, sizeof pid);
1240 * unlock - remove our lockfile
1252 #if defined(i386) && defined(HAS_BROKEN_IOCTL)
1263 u_char let, code, size;
1265 size = (cmd >> 16) & IOCPARM_MASK;
1269 if (let == 't' && (75 <= code && code <= 90))
1270 syslog(LOG_INFO, "ioctl(%d, 0x%x ('%c', %d, %d), 0x%x)\n", fd, cmd,
1271 let, code, size, c);
1274 ret = ioctl(fd, cmd, c);
1279 syslog(LOG_INFO, "ioctl('%c', %d, %d) errno = %d (%m)\n",
1280 let, code, size, errno);
1281 if (let == 't' && (75 <= code && code <= 90) && (cmd & IOC_OUT)) {
1282 int i, len = ((cmd >> 16) & IOCPARM_MASK);
1283 for (i = 0; i < len / 4; ++i)
1284 syslog(LOG_INFO, "word[%d] @ 0x%06x = 0x%x\n",
1285 i, &((int *) c)[i],((int *)c)[i]);
1290 if (ret == -1 && errno == EPERM)
1294 #endif /* HAS_BROKEN_IOCTL */
1297 #if defined(FIXSIGS) && (defined (hppa) || defined(sparc))
1300 * These redefinitions of Posix functions are necessary
1301 * because HPPA systems have an OS bug that causes
1302 * sigaction to core dump:
1304 * AlainF 9-Nov-1994 HACK FOR HP-PA/NEXTSTEP
1305 * sigaction(3) seems broken in the HP-PA NeXTSTEP 3.2
1306 * Posix lib. This causes pppd to SIGBUS at the expiration
1307 * of the first timeout (_sigtramp seems to invoke
1308 * the SIGALRM handler at an unreasonably low address).
1309 * All calls so sigaction(3) have been changed to calls
1310 * to sigvec(2) and sigprocmask(SIG_BLOCK,...) to
1312 * This is kind of a hack, especially since there are
1313 * other routines of the Posix lib still used, but
1316 * Dave Hess <David-Hess@net.tamu.edu> noted that 3.3 Sparc seems to
1317 * have the same bug. Thus this fix has been enabled for SPARC also.
1322 int sigemptyset(sigset_t *mask)
1327 sigaddset(sigset_t *mask, int which_sig)
1329 *mask |= sigmask(which_sig);
1333 int sigaction(int sig, const struct sigaction *act, struct sigaction *oact)
1338 sv.sv_handler = act->sa_handler;
1339 sv.sv_mask = act->sa_mask;
1345 syslog(LOG_WARNING, "PPPD: Inside modified HP and SPARC sigaction\n");
1348 return sigvec(sig, &sv, NULL);