2 * sys-ultrix.c - System-dependent procedures for setting up
3 * PPP interfaces on Ultrix systems.
5 * Copyright (c) 1989 Carnegie Mellon University.
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by Carnegie Mellon University. The name of the
14 * University may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 static char rcsid[] = "$Id: sys-ultrix.c,v 1.15 1995/10/27 03:51:52 paulus Exp $";
35 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <sys/ioctl.h>
40 #include <sys/errno.h>
44 #include <net/ppp_defs.h>
45 #include <net/if_ppp.h>
46 #include <net/route.h>
47 #include <netinet/in.h>
51 static int initdisc = -1; /* Initial TTY discipline */
52 static int initfdflags = -1; /* Initial file descriptor flags for fd */
54 static int restore_term; /* 1 => we've munged the terminal */
55 static struct termios inittermios; /* Initial TTY termios */
56 static struct winsize wsinfo; /* Initial window size info */
58 static char *lock_file; /* name of lock file created */
60 static int sockfd; /* socket for doing interface ioctls */
62 static int if_is_up; /* the interface is currently up */
63 static u_int32_t default_route_gateway; /* gateway addr for default route */
64 static u_int32_t proxy_arp_addr; /* remote addr for proxy arp */
66 /* Prototypes for procedures local to this file. */
67 static int translate_speed __P((int));
68 static int baud_rate_of __P((int));
69 static int get_ether_addr __P((u_int32_t, struct sockaddr *));
73 * sys_init - System-dependent initialization.
78 openlog("pppd", LOG_PID);
80 /* Get an internet socket for doing socket ioctl's on. */
81 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
82 syslog(LOG_ERR, "Couldn't create IP socket: %m");
88 * sys_cleanup - restore any system state we modified before exiting:
89 * mark the interface down, delete default route and/or proxy arp entry.
90 * This should call die() because it's called from die().
98 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
99 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) >= 0
100 && ((ifr.ifr_flags & IFF_UP) != 0)) {
101 ifr.ifr_flags &= ~IFF_UP;
102 ioctl(sockfd, SIOCSIFFLAGS, &ifr);
106 if (default_route_gateway)
107 cifdefaultroute(0, default_route_gateway);
109 cifproxyarp(0, proxy_arp_addr);
113 * note_debug_level - note a change in the debug level.
122 * daemon - Detach us from the terminal session.
125 daemon(nochdir, noclose)
126 int nochdir, noclose;
130 if ((pid = fork()) < 0)
133 exit(0); /* parent dies */
138 fclose(stdin); /* don't need stdin, stdout, stderr */
146 * ppp_available - check whether the system has any ppp interfaces
147 * (in fact we check whether we can do an ioctl on ppp0).
154 extern char *no_ppp_msg;
156 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
157 return 1; /* can't tell */
159 strncpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
160 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
164 This system lacks kernel support for PPP. To include PPP support\n\
165 in the kernel, please follow the steps detailed in the README.ultrix\n\
166 file in the ppp-2.2 distribution.\n";
171 * establish_ppp - Turn the serial port into a ppp interface.
176 int pppdisc = PPPDISC;
179 if (ioctl(fd, TIOCGETD, &initdisc) < 0) {
180 syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
183 if (ioctl(fd, TIOCSETD, &pppdisc) < 0) {
184 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
189 * Find out which interface we were given.
191 if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {
192 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
197 * Enable debug in the driver if requested.
200 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
201 syslog(LOG_WARNING, "ioctl (PPPIOCGFLAGS): %m");
203 x |= (kdebugflag & 0xFF) * SC_DEBUG;
204 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
205 syslog(LOG_WARNING, "ioctl(PPPIOCSFLAGS): %m");
210 * Set device for non-blocking reads.
212 if ((initfdflags = fcntl(fd, F_GETFL)) == -1
213 || fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
214 syslog(LOG_WARNING, "Couldn't set device to non-blocking mode: %m");
220 * disestablish_ppp - Restore the serial port to normal operation.
221 * This shouldn't call die() because it's called from die().
229 /* Reset non-blocking mode on the file descriptor. */
230 if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
231 syslog(LOG_WARNING, "Couldn't restore device fd flags: %m");
236 * Check whether the link seems not to be 8-bit clean.
238 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
240 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
242 s = "bit 7 set to 1";
245 s = "bit 7 set to 0";
255 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
256 syslog(LOG_WARNING, "All received characters had %s", s);
259 if (ioctl(fd, TIOCSETD, &initdisc) < 0)
260 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
266 * List of valid speeds.
269 int speed_int, speed_val;
341 * Translate from bits/second to a speed_t.
347 struct speed *speedp;
351 for (speedp = speeds; speedp->speed_int; speedp++)
352 if (bps == speedp->speed_int)
353 return speedp->speed_val;
354 syslog(LOG_WARNING, "speed %d not supported", bps);
359 * Translate from a speed_t to bits/second.
365 struct speed *speedp;
369 for (speedp = speeds; speedp->speed_int; speedp++)
370 if (speed == speedp->speed_val)
371 return speedp->speed_int;
376 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
377 * at the requested speed, etc. If `local' is true, set CLOCAL
378 * regardless of whether the modem option was specified.
381 set_up_tty(fd, local)
387 if (tcgetattr(fd, &tios) < 0) {
388 syslog(LOG_ERR, "tcgetattr: %m");
394 ioctl(fd, TIOCGWINSZ, &wsinfo);
397 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
399 if (crtscts > 0 && !local)
400 tios.c_cflag |= CRTSCTS;
401 else if (crtscts < 0)
402 tios.c_cflag &= ~CRTSCTS;
405 tios.c_cflag |= CS8 | CREAD | HUPCL;
407 tios.c_cflag |= CLOCAL;
408 tios.c_iflag = IGNBRK | IGNPAR;
412 tios.c_cc[VTIME] = 0;
415 tios.c_iflag |= IXON | IXOFF;
416 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
417 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
420 speed = translate_speed(inspeed);
422 cfsetospeed(&tios, speed);
423 cfsetispeed(&tios, speed);
425 speed = cfgetospeed(&tios);
427 * We can't proceed if the serial port speed is B0,
428 * since that implies that the serial port is disabled.
431 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
437 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
438 syslog(LOG_ERR, "tcsetattr: %m");
443 if (ioctl(fd, (crtscts > 0 || modem)? TIOCMODEM: TIOCNMODEM, &x) < 0)
444 syslog(LOG_WARNING, "TIOC(N)MODEM: %m");
445 if (ioctl(fd, (local || !modem)? TIOCNCAR: TIOCCAR) < 0)
446 syslog(LOG_WARNING, "TIOC(N)CAR: %m");
448 baud_rate = inspeed = baud_rate_of(speed);
453 * restore_tty - restore the terminal to the saved settings.
459 if (!default_device) {
461 * Turn off echoing, because otherwise we can get into
462 * a loop with the tty and the modem echoing to each other.
463 * We presume we are the sole user of this tty device, so
464 * when we close it, it will revert to its defaults anyway.
466 inittermios.c_lflag &= ~(ECHO | ECHONL);
468 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
470 syslog(LOG_WARNING, "tcsetattr: %m");
471 ioctl(fd, TIOCSWINSZ, &wsinfo);
472 restore_term = FALSE;
477 * setdtr - control the DTR line on the serial port.
478 * This is called from die(), so it shouldn't call die().
484 int modembits = TIOCM_DTR;
486 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
491 * output - Output PPP packet.
500 log_packet(p, len, "sent ");
502 if (write(fd, p, len) < 0) {
504 syslog(LOG_ERR, "write: %m");
510 * wait_input - wait until there is data available on fd,
511 * for the length of time specified by *timo (indefinite
516 struct timeval *timo;
523 n = select(fd+1, &ready, NULL, &ready, timo);
524 if (n < 0 && errno != EINTR) {
525 syslog(LOG_ERR, "select: %m");
532 * read_packet - get a PPP packet from the serial device.
540 if ((len = read(fd, buf, PPP_MTU + PPP_HDRLEN)) < 0) {
541 if (errno == EWOULDBLOCK || errno == EINTR)
543 syslog(LOG_ERR, "read(fd): %m");
551 * ppp_send_config - configure the transmit characteristics of
555 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
562 if (ioctl(fd, PPPIOCSMTU, &mtu) < 0) {
563 syslog(LOG_ERR, "ioctl(PPPIOCSMTU): %m");
567 if (ioctl(fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
568 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
572 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
573 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
576 x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
577 x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
578 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
579 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
586 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
589 ppp_set_xaccm(unit, accm)
593 if (ioctl(fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
594 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
599 * ppp_recv_config - configure the receive-side characteristics of
603 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
610 if (ioctl(fd, PPPIOCSMRU, (caddr_t) &mru) < 0) {
611 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
614 if (ioctl(fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
615 syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m");
618 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
619 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
622 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
623 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
624 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
630 * ccp_test - ask kernel whether a given compression method
631 * is acceptable for use. Returns 1 if the method and parameters
632 * are OK, 0 if the method is known but the parameters are not OK
633 * (e.g. code size should be reduced), or -1 if the method is unknown.
636 ccp_test(unit, opt_ptr, opt_len, for_transmit)
637 int unit, opt_len, for_transmit;
640 struct ppp_option_data data;
643 data.length = opt_len;
644 data.transmit = for_transmit;
645 if (ioctl(fd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
647 return (errno == ENOBUFS)? 0: -1;
651 * ccp_flags_set - inform kernel about the current state of CCP.
654 ccp_flags_set(unit, isopen, isup)
655 int unit, isopen, isup;
659 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
660 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
663 x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
664 x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
665 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
666 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
670 * ccp_fatal_error - returns 1 if decompression was disabled as a
671 * result of an error detected after decompression of a packet,
672 * 0 otherwise. This is necessary because of patent nonsense.
675 ccp_fatal_error(unit)
680 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
681 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
684 return x & SC_DC_FERROR;
688 * sifvjcomp - config tcp header compression
691 sifvjcomp(u, vjcomp, cidcomp, maxcid)
692 int u, vjcomp, cidcomp, maxcid;
696 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
697 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
700 x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
701 x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
702 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
703 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
706 if (ioctl(fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
707 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
714 * sifup - Config the interface up and enable IP packets to pass.
717 #define SC_ENABLE_IP 0x100 /* compat for old versions of kernel code */
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");
739 npi.protocol = PPP_IP;
740 npi.mode = NPMODE_PASS;
741 if (ioctl(fd, PPPIOCSNPMODE, &npi) < 0) {
742 if (errno != ENOTTY) {
743 syslog(LOG_ERR, "ioctl(PPPIOCSNPMODE): %m");
746 /* for backwards compatibility */
747 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
748 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
752 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
753 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
761 * sifdown - Config the interface down and disable IP.
773 npi.protocol = PPP_IP;
774 npi.mode = NPMODE_ERROR;
775 if (ioctl(fd, PPPIOCSNPMODE, (caddr_t) &npi) < 0) {
776 if (errno != ENOTTY) {
777 syslog(LOG_ERR, "ioctl(PPPIOCSNPMODE): %m");
780 /* backwards compatibility */
781 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
782 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
786 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
787 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
794 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
795 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
796 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
799 ifr.ifr_flags &= ~IFF_UP;
800 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
801 syslog(LOG_ERR, "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 strncpy(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 syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
836 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
837 if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
838 syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
842 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
843 syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
844 if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
845 syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
853 * cifaddr - Clear the interface IP addresses, and delete routes
854 * through the interface if possible.
863 SET_SA_FAMILY(rt.rt_dst, AF_INET);
864 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
865 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
866 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
867 rt.rt_flags = RTF_HOST;
868 if (ioctl(sockfd, SIOCDELRT, (caddr_t) &rt) < 0) {
869 syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
876 * sifdefaultroute - assign a default route through the address given.
879 sifdefaultroute(u, g)
883 SET_SA_FAMILY(rt.rt_dst, AF_INET);
884 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
885 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
886 rt.rt_flags = RTF_GATEWAY;
887 if (ioctl(sockfd, SIOCADDRT, &rt) < 0) {
888 syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
891 default_route_gateway = g;
896 * cifdefaultroute - delete a default route through the address given.
899 cifdefaultroute(u, g)
903 SET_SA_FAMILY(rt.rt_dst, AF_INET);
904 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
905 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
906 rt.rt_flags = RTF_GATEWAY;
907 if (ioctl(sockfd, SIOCDELRT, &rt) < 0)
908 syslog(LOG_WARNING, "default route ioctl(SIOCDELRT): %m");
909 default_route_gateway = 0;
914 * sifproxyarp - Make a proxy ARP entry for the peer.
917 sifproxyarp(unit, hisaddr)
921 struct arpreq arpreq;
923 BZERO(&arpreq, sizeof(arpreq));
926 * Get the hardware address of an interface on the same subnet
927 * as our local address.
929 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
930 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
934 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
935 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
936 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
937 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
938 syslog(LOG_ERR, "Couldn't add proxy arp entry: %m");
942 proxy_arp_addr = hisaddr;
947 * cifproxyarp - Delete the proxy ARP entry for the peer.
950 cifproxyarp(unit, hisaddr)
954 struct arpreq arpreq;
956 BZERO(&arpreq, sizeof(arpreq));
957 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
958 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
959 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
960 syslog(LOG_WARNING, "Couldn't delete proxy arp entry: %m");
968 * get_ether_addr - get the hardware address of an interface on the
969 * the same subnet as ipaddr.
974 get_ether_addr(ipaddr, hwaddr)
976 struct sockaddr *hwaddr;
978 struct ifreq *ifr, *ifend;
982 struct ifreq ifs[MAX_IFS];
983 struct ifdevea ifdevea;
985 ifc.ifc_len = sizeof(ifs);
987 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
988 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
993 * Scan through looking for an interface with an Internet
994 * address on the same subnet as `ipaddr'.
996 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
997 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
998 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
999 if (ifr->ifr_addr.sa_family == AF_INET) {
1000 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1001 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1003 * Check that the interface is up, and not point-to-point
1006 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1008 if ((ifreq.ifr_flags &
1009 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1010 != (IFF_UP|IFF_BROADCAST))
1013 * Get its netmask and check that it's on the right subnet.
1015 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1017 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1018 if ((ipaddr & mask) != (ina & mask))
1027 syslog(LOG_INFO, "found interface %s for proxy arp", ifr->ifr_name);
1030 * Grab the physical address for this interface.
1032 strncpy(ifdevea.ifr_name, ifr->ifr_name, sizeof(ifdevea.ifr_name));
1033 if (ioctl(sockfd, SIOCRPHYSADDR, &ifdevea) < 0) {
1034 syslog(LOG_ERR, "Couldn't get h/w address for %s: %m", ifr->ifr_name);
1038 hwaddr->sa_family = AF_UNSPEC;
1039 BCOPY(ifdevea.current_pa, hwaddr->sa_data, 6);
1044 * Return user specified netmask, modified by any mask we might determine
1045 * for address `addr' (in network byte order).
1046 * Here we scan through the system's list of interfaces, looking for
1047 * any non-point-to-point interfaces which might appear to be on the same
1048 * network as `addr'. If we find any, we OR in their netmask to the
1049 * user-specified netmask.
1055 u_int32_t mask, nmask, ina;
1056 struct ifreq *ifr, *ifend, ifreq;
1058 struct ifreq ifs[MAX_IFS];
1061 if (IN_CLASSA(addr)) /* determine network mask for address class */
1062 nmask = IN_CLASSA_NET;
1063 else if (IN_CLASSB(addr))
1064 nmask = IN_CLASSB_NET;
1066 nmask = IN_CLASSC_NET;
1067 /* class D nets are disallowed by bad_ip_adrs */
1068 mask = netmask | htonl(nmask);
1071 * Scan through the system's network interfaces.
1073 ifc.ifc_len = sizeof(ifs);
1075 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1076 syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m");
1079 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1080 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1081 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1083 * Check the interface's internet address.
1085 if (ifr->ifr_addr.sa_family != AF_INET)
1087 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1088 if ((ntohl(ina) & nmask) != (addr & nmask))
1091 * Check that the interface is up, and not point-to-point or loopback.
1093 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1094 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1096 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1100 * Get its netmask and OR it into our mask.
1102 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1104 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1112 Seems like strdup() is not part of string package in Ultrix.
1113 If I understood the man-page on the sun this should work.
1118 char *strdup( in ) char *in;
1121 if(! (dup = (char *) malloc( strlen( in ) +1 ))) return NULL;
1122 (void) strcpy( dup, in );
1127 * This logwtmp() implementation is subject to the following copyright:
1129 * Copyright (c) 1988 The Regents of the University of California.
1130 * All rights reserved.
1132 * Redistribution and use in source and binary forms are permitted
1133 * provided that the above copyright notice and this paragraph are
1134 * duplicated in all such forms and that any documentation,
1135 * advertising materials, and other materials related to such
1136 * distribution and use acknowledge that the software was developed
1137 * by the University of California, Berkeley. The name of the
1138 * University may not be used to endorse or promote products derived
1139 * from this software without specific prior written permission.
1140 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1141 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1142 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1145 #define WTMPFILE "/usr/adm/wtmp"
1148 logwtmp(line, name, host)
1149 char *line, *name, *host;
1155 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1157 if (!fstat(fd, &buf)) {
1158 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1159 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1160 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1161 (void)time(&ut.ut_time);
1162 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1163 (void)ftruncate(fd, buf.st_size);
1169 * Routines for locking and unlocking the serial device, moved here
1173 #define LOCK_PREFIX "/usr/spool/uucp/LCK.."
1176 * lock - create a lock file for the named device
1185 if ((p = strrchr(dev, '/')) != NULL)
1187 lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1188 if (lock_file == NULL)
1189 novm("lock file name");
1190 strcat(strcpy(lock_file, LOCK_PREFIX), dev);
1192 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1194 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1195 /* Read the lock file to find out who has the device locked */
1196 n = read(fd, &pid, sizeof(pid));
1198 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1201 if (kill(pid, 0) == -1 && errno == ESRCH) {
1202 /* pid no longer exists - remove the lock file */
1203 if (unlink(lock_file) == 0) {
1205 syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1209 syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
1212 syslog(LOG_NOTICE, "Device %s is locked by pid %d",
1217 syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1224 write(fd, &pid, sizeof pid);
1231 * unlock - remove our lockfile